Fix handling of anonymous field in struct with pointer type.
[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("float");
198 case TYPE_COMPLEX:
199 return Type::lookup_complex_type("complex");
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, FLOAT_TYPE_SIZE,
1876 RUNTIME_TYPE_KIND_FLOAT);
1877 return abstract_type;
1878 }
1879
1880 // Whether this type is identical with T.
1881
1882 bool
1883 Float_type::is_identical(const Float_type* t) const
1884 {
1885 if (this->bits_ != t->bits_)
1886 return false;
1887 return this->is_abstract_ == t->is_abstract_;
1888 }
1889
1890 // Hash code.
1891
1892 unsigned int
1893 Float_type::do_hash_for_method(Gogo*) const
1894 {
1895 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
1896 }
1897
1898 // Get a tree without using a Gogo*.
1899
1900 tree
1901 Float_type::type_tree() const
1902 {
1903 if (this->bits_ == FLOAT_TYPE_SIZE)
1904 return float_type_node;
1905 else if (this->bits_ == DOUBLE_TYPE_SIZE)
1906 return double_type_node;
1907 else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE)
1908 return long_double_type_node;
1909 else
1910 {
1911 tree ret = make_node(REAL_TYPE);
1912 TYPE_PRECISION(ret) = this->bits_;
1913 layout_type(ret);
1914 return ret;
1915 }
1916 }
1917
1918 // Get a tree.
1919
1920 tree
1921 Float_type::do_get_tree(Gogo*)
1922 {
1923 return this->type_tree();
1924 }
1925
1926 tree
1927 Float_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
1928 {
1929 if (is_clear)
1930 return NULL;
1931 REAL_VALUE_TYPE r;
1932 real_from_integer(&r, TYPE_MODE(type_tree), 0, 0, 0);
1933 return build_real(type_tree, r);
1934 }
1935
1936 // The type descriptor for a float type. Float types are always named.
1937
1938 Expression*
1939 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1940 {
1941 gcc_assert(name != NULL);
1942 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
1943 }
1944
1945 // We should not be asked for the reflection string of a basic type.
1946
1947 void
1948 Float_type::do_reflection(Gogo*, std::string*) const
1949 {
1950 gcc_unreachable();
1951 }
1952
1953 // Mangled name.
1954
1955 void
1956 Float_type::do_mangled_name(Gogo*, std::string* ret) const
1957 {
1958 char buf[100];
1959 snprintf(buf, sizeof buf, "f%s%de",
1960 this->is_abstract_ ? "a" : "",
1961 this->bits_);
1962 ret->append(buf);
1963 }
1964
1965 // Make a floating point type.
1966
1967 Named_type*
1968 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
1969 {
1970 return Float_type::create_float_type(name, bits, runtime_type_kind);
1971 }
1972
1973 // Make an abstract float type.
1974
1975 Float_type*
1976 Type::make_abstract_float_type()
1977 {
1978 return Float_type::create_abstract_float_type();
1979 }
1980
1981 // Look up a float type.
1982
1983 Named_type*
1984 Type::lookup_float_type(const char* name)
1985 {
1986 return Float_type::lookup_float_type(name);
1987 }
1988
1989 // Class Complex_type.
1990
1991 Complex_type::Named_complex_types Complex_type::named_complex_types;
1992
1993 // Create a new complex type. Non-abstract complex types always have
1994 // names.
1995
1996 Named_type*
1997 Complex_type::create_complex_type(const char* name, int bits,
1998 int runtime_type_kind)
1999 {
2000 Complex_type* complex_type = new Complex_type(false, bits,
2001 runtime_type_kind);
2002 std::string sname(name);
2003 Named_object* named_object = Named_object::make_type(sname, NULL,
2004 complex_type,
2005 BUILTINS_LOCATION);
2006 Named_type* named_type = named_object->type_value();
2007 std::pair<Named_complex_types::iterator, bool> ins =
2008 Complex_type::named_complex_types.insert(std::make_pair(sname,
2009 named_type));
2010 gcc_assert(ins.second);
2011 return named_type;
2012 }
2013
2014 // Look up an existing complex type.
2015
2016 Named_type*
2017 Complex_type::lookup_complex_type(const char* name)
2018 {
2019 Named_complex_types::const_iterator p =
2020 Complex_type::named_complex_types.find(name);
2021 gcc_assert(p != Complex_type::named_complex_types.end());
2022 return p->second;
2023 }
2024
2025 // Create a new abstract complex type.
2026
2027 Complex_type*
2028 Complex_type::create_abstract_complex_type()
2029 {
2030 static Complex_type* abstract_type;
2031 if (abstract_type == NULL)
2032 abstract_type = new Complex_type(true, FLOAT_TYPE_SIZE * 2,
2033 RUNTIME_TYPE_KIND_FLOAT);
2034 return abstract_type;
2035 }
2036
2037 // Whether this type is identical with T.
2038
2039 bool
2040 Complex_type::is_identical(const Complex_type *t) const
2041 {
2042 if (this->bits_ != t->bits_)
2043 return false;
2044 return this->is_abstract_ == t->is_abstract_;
2045 }
2046
2047 // Hash code.
2048
2049 unsigned int
2050 Complex_type::do_hash_for_method(Gogo*) const
2051 {
2052 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2053 }
2054
2055 // Get a tree without using a Gogo*.
2056
2057 tree
2058 Complex_type::type_tree() const
2059 {
2060 if (this->bits_ == FLOAT_TYPE_SIZE * 2)
2061 return complex_float_type_node;
2062 else if (this->bits_ == DOUBLE_TYPE_SIZE * 2)
2063 return complex_double_type_node;
2064 else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE * 2)
2065 return complex_long_double_type_node;
2066 else
2067 {
2068 tree ret = make_node(REAL_TYPE);
2069 TYPE_PRECISION(ret) = this->bits_ / 2;
2070 layout_type(ret);
2071 return build_complex_type(ret);
2072 }
2073 }
2074
2075 // Get a tree.
2076
2077 tree
2078 Complex_type::do_get_tree(Gogo*)
2079 {
2080 return this->type_tree();
2081 }
2082
2083 // Zero initializer.
2084
2085 tree
2086 Complex_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2087 {
2088 if (is_clear)
2089 return NULL;
2090 REAL_VALUE_TYPE r;
2091 real_from_integer(&r, TYPE_MODE(TREE_TYPE(type_tree)), 0, 0, 0);
2092 return build_complex(type_tree, build_real(TREE_TYPE(type_tree), r),
2093 build_real(TREE_TYPE(type_tree), r));
2094 }
2095
2096 // The type descriptor for a complex type. Complex types are always
2097 // named.
2098
2099 Expression*
2100 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2101 {
2102 gcc_assert(name != NULL);
2103 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2104 }
2105
2106 // We should not be asked for the reflection string of a basic type.
2107
2108 void
2109 Complex_type::do_reflection(Gogo*, std::string*) const
2110 {
2111 gcc_unreachable();
2112 }
2113
2114 // Mangled name.
2115
2116 void
2117 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2118 {
2119 char buf[100];
2120 snprintf(buf, sizeof buf, "c%s%de",
2121 this->is_abstract_ ? "a" : "",
2122 this->bits_);
2123 ret->append(buf);
2124 }
2125
2126 // Make a complex type.
2127
2128 Named_type*
2129 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2130 {
2131 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2132 }
2133
2134 // Make an abstract complex type.
2135
2136 Complex_type*
2137 Type::make_abstract_complex_type()
2138 {
2139 return Complex_type::create_abstract_complex_type();
2140 }
2141
2142 // Look up a complex type.
2143
2144 Named_type*
2145 Type::lookup_complex_type(const char* name)
2146 {
2147 return Complex_type::lookup_complex_type(name);
2148 }
2149
2150 // Class String_type.
2151
2152 // Return the tree for String_type. A string is a struct with two
2153 // fields: a pointer to the characters and a length.
2154
2155 tree
2156 String_type::do_get_tree(Gogo*)
2157 {
2158 static tree struct_type;
2159 return Gogo::builtin_struct(&struct_type, "__go_string", NULL_TREE, 2,
2160 "__data",
2161 build_pointer_type(unsigned_char_type_node),
2162 "__length",
2163 integer_type_node);
2164 }
2165
2166 // Return a tree for the length of STRING.
2167
2168 tree
2169 String_type::length_tree(Gogo*, tree string)
2170 {
2171 tree string_type = TREE_TYPE(string);
2172 gcc_assert(TREE_CODE(string_type) == RECORD_TYPE);
2173 tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2174 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2175 "__length") == 0);
2176 return fold_build3(COMPONENT_REF, integer_type_node, string,
2177 length_field, NULL_TREE);
2178 }
2179
2180 // Return a tree for a pointer to the bytes of STRING.
2181
2182 tree
2183 String_type::bytes_tree(Gogo*, tree string)
2184 {
2185 tree string_type = TREE_TYPE(string);
2186 gcc_assert(TREE_CODE(string_type) == RECORD_TYPE);
2187 tree bytes_field = TYPE_FIELDS(string_type);
2188 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
2189 "__data") == 0);
2190 return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
2191 bytes_field, NULL_TREE);
2192 }
2193
2194 // We initialize a string to { NULL, 0 }.
2195
2196 tree
2197 String_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2198 {
2199 if (is_clear)
2200 return NULL_TREE;
2201
2202 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2203
2204 VEC(constructor_elt, gc)* init = VEC_alloc(constructor_elt, gc, 2);
2205
2206 for (tree field = TYPE_FIELDS(type_tree);
2207 field != NULL_TREE;
2208 field = DECL_CHAIN(field))
2209 {
2210 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
2211 elt->index = field;
2212 elt->value = fold_convert(TREE_TYPE(field), size_zero_node);
2213 }
2214
2215 tree ret = build_constructor(type_tree, init);
2216 TREE_CONSTANT(ret) = 1;
2217 return ret;
2218 }
2219
2220 // The type descriptor for the string type.
2221
2222 Expression*
2223 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2224 {
2225 if (name != NULL)
2226 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
2227 else
2228 {
2229 Named_object* no = gogo->lookup_global("string");
2230 gcc_assert(no != NULL);
2231 return Type::type_descriptor(gogo, no->type_value());
2232 }
2233 }
2234
2235 // We should not be asked for the reflection string of a basic type.
2236
2237 void
2238 String_type::do_reflection(Gogo*, std::string* ret) const
2239 {
2240 ret->append("string");
2241 }
2242
2243 // Mangled name of a string type.
2244
2245 void
2246 String_type::do_mangled_name(Gogo*, std::string* ret) const
2247 {
2248 ret->push_back('z');
2249 }
2250
2251 // Make a string type.
2252
2253 Type*
2254 Type::make_string_type()
2255 {
2256 static String_type string_type;
2257 return &string_type;
2258 }
2259
2260 // The named type "string".
2261
2262 static Named_type* named_string_type;
2263
2264 // Get the named type "string".
2265
2266 Named_type*
2267 Type::lookup_string_type()
2268 {
2269 return named_string_type;
2270 }
2271
2272 // Make the named type string.
2273
2274 Named_type*
2275 Type::make_named_string_type()
2276 {
2277 Type* string_type = Type::make_string_type();
2278 Named_object* named_object = Named_object::make_type("string", NULL,
2279 string_type,
2280 BUILTINS_LOCATION);
2281 Named_type* named_type = named_object->type_value();
2282 named_string_type = named_type;
2283 return named_type;
2284 }
2285
2286 // The sink type. This is the type of the blank identifier _. Any
2287 // type may be assigned to it.
2288
2289 class Sink_type : public Type
2290 {
2291 public:
2292 Sink_type()
2293 : Type(TYPE_SINK)
2294 { }
2295
2296 protected:
2297 tree
2298 do_get_tree(Gogo*)
2299 { gcc_unreachable(); }
2300
2301 tree
2302 do_get_init_tree(Gogo*, tree, bool)
2303 { gcc_unreachable(); }
2304
2305 Expression*
2306 do_type_descriptor(Gogo*, Named_type*)
2307 { gcc_unreachable(); }
2308
2309 void
2310 do_reflection(Gogo*, std::string*) const
2311 { gcc_unreachable(); }
2312
2313 void
2314 do_mangled_name(Gogo*, std::string*) const
2315 { gcc_unreachable(); }
2316 };
2317
2318 // Make the sink type.
2319
2320 Type*
2321 Type::make_sink_type()
2322 {
2323 static Sink_type sink_type;
2324 return &sink_type;
2325 }
2326
2327 // Class Function_type.
2328
2329 // Traversal.
2330
2331 int
2332 Function_type::do_traverse(Traverse* traverse)
2333 {
2334 if (this->receiver_ != NULL
2335 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
2336 return TRAVERSE_EXIT;
2337 if (this->parameters_ != NULL
2338 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
2339 return TRAVERSE_EXIT;
2340 if (this->results_ != NULL
2341 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
2342 return TRAVERSE_EXIT;
2343 return TRAVERSE_CONTINUE;
2344 }
2345
2346 // Returns whether T is a valid redeclaration of this type. If this
2347 // returns false, and REASON is not NULL, *REASON may be set to a
2348 // brief explanation of why it returned false.
2349
2350 bool
2351 Function_type::is_valid_redeclaration(const Function_type* t,
2352 std::string* reason) const
2353 {
2354 if (!this->is_identical(t, false, true, reason))
2355 return false;
2356
2357 // A redeclaration of a function is required to use the same names
2358 // for the receiver and parameters.
2359 if (this->receiver() != NULL
2360 && this->receiver()->name() != t->receiver()->name()
2361 && this->receiver()->name() != Import::import_marker
2362 && t->receiver()->name() != Import::import_marker)
2363 {
2364 if (reason != NULL)
2365 *reason = "receiver name changed";
2366 return false;
2367 }
2368
2369 const Typed_identifier_list* parms1 = this->parameters();
2370 const Typed_identifier_list* parms2 = t->parameters();
2371 if (parms1 != NULL)
2372 {
2373 Typed_identifier_list::const_iterator p1 = parms1->begin();
2374 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2375 p2 != parms2->end();
2376 ++p2, ++p1)
2377 {
2378 if (p1->name() != p2->name()
2379 && p1->name() != Import::import_marker
2380 && p2->name() != Import::import_marker)
2381 {
2382 if (reason != NULL)
2383 *reason = "parameter name changed";
2384 return false;
2385 }
2386
2387 // This is called at parse time, so we may have unknown
2388 // types.
2389 Type* t1 = p1->type()->forwarded();
2390 Type* t2 = p2->type()->forwarded();
2391 if (t1 != t2
2392 && t1->forward_declaration_type() != NULL
2393 && (t2->forward_declaration_type() == NULL
2394 || (t1->forward_declaration_type()->named_object()
2395 != t2->forward_declaration_type()->named_object())))
2396 return false;
2397 }
2398 }
2399
2400 const Typed_identifier_list* results1 = this->results();
2401 const Typed_identifier_list* results2 = t->results();
2402 if (results1 != NULL)
2403 {
2404 Typed_identifier_list::const_iterator res1 = results1->begin();
2405 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2406 res2 != results2->end();
2407 ++res2, ++res1)
2408 {
2409 if (res1->name() != res2->name()
2410 && res1->name() != Import::import_marker
2411 && res2->name() != Import::import_marker)
2412 {
2413 if (reason != NULL)
2414 *reason = "result name changed";
2415 return false;
2416 }
2417
2418 // This is called at parse time, so we may have unknown
2419 // types.
2420 Type* t1 = res1->type()->forwarded();
2421 Type* t2 = res2->type()->forwarded();
2422 if (t1 != t2
2423 && t1->forward_declaration_type() != NULL
2424 && (t2->forward_declaration_type() == NULL
2425 || (t1->forward_declaration_type()->named_object()
2426 != t2->forward_declaration_type()->named_object())))
2427 return false;
2428 }
2429 }
2430
2431 return true;
2432 }
2433
2434 // Check whether T is the same as this type.
2435
2436 bool
2437 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
2438 bool errors_are_identical,
2439 std::string* reason) const
2440 {
2441 if (!ignore_receiver)
2442 {
2443 const Typed_identifier* r1 = this->receiver();
2444 const Typed_identifier* r2 = t->receiver();
2445 if ((r1 != NULL) != (r2 != NULL))
2446 {
2447 if (reason != NULL)
2448 *reason = _("different receiver types");
2449 return false;
2450 }
2451 if (r1 != NULL)
2452 {
2453 if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
2454 reason))
2455 {
2456 if (reason != NULL && !reason->empty())
2457 *reason = "receiver: " + *reason;
2458 return false;
2459 }
2460 }
2461 }
2462
2463 const Typed_identifier_list* parms1 = this->parameters();
2464 const Typed_identifier_list* parms2 = t->parameters();
2465 if ((parms1 != NULL) != (parms2 != NULL))
2466 {
2467 if (reason != NULL)
2468 *reason = _("different number of parameters");
2469 return false;
2470 }
2471 if (parms1 != NULL)
2472 {
2473 Typed_identifier_list::const_iterator p1 = parms1->begin();
2474 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2475 p2 != parms2->end();
2476 ++p2, ++p1)
2477 {
2478 if (p1 == parms1->end())
2479 {
2480 if (reason != NULL)
2481 *reason = _("different number of parameters");
2482 return false;
2483 }
2484
2485 if (!Type::are_identical(p1->type(), p2->type(),
2486 errors_are_identical, NULL))
2487 {
2488 if (reason != NULL)
2489 *reason = _("different parameter types");
2490 return false;
2491 }
2492 }
2493 if (p1 != parms1->end())
2494 {
2495 if (reason != NULL)
2496 *reason = _("different number of parameters");
2497 return false;
2498 }
2499 }
2500
2501 if (this->is_varargs() != t->is_varargs())
2502 {
2503 if (reason != NULL)
2504 *reason = _("different varargs");
2505 return false;
2506 }
2507
2508 const Typed_identifier_list* results1 = this->results();
2509 const Typed_identifier_list* results2 = t->results();
2510 if ((results1 != NULL) != (results2 != NULL))
2511 {
2512 if (reason != NULL)
2513 *reason = _("different number of results");
2514 return false;
2515 }
2516 if (results1 != NULL)
2517 {
2518 Typed_identifier_list::const_iterator res1 = results1->begin();
2519 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2520 res2 != results2->end();
2521 ++res2, ++res1)
2522 {
2523 if (res1 == results1->end())
2524 {
2525 if (reason != NULL)
2526 *reason = _("different number of results");
2527 return false;
2528 }
2529
2530 if (!Type::are_identical(res1->type(), res2->type(),
2531 errors_are_identical, NULL))
2532 {
2533 if (reason != NULL)
2534 *reason = _("different result types");
2535 return false;
2536 }
2537 }
2538 if (res1 != results1->end())
2539 {
2540 if (reason != NULL)
2541 *reason = _("different number of results");
2542 return false;
2543 }
2544 }
2545
2546 return true;
2547 }
2548
2549 // Hash code.
2550
2551 unsigned int
2552 Function_type::do_hash_for_method(Gogo* gogo) const
2553 {
2554 unsigned int ret = 0;
2555 // We ignore the receiver type for hash codes, because we need to
2556 // get the same hash code for a method in an interface and a method
2557 // declared for a type. The former will not have a receiver.
2558 if (this->parameters_ != NULL)
2559 {
2560 int shift = 1;
2561 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
2562 p != this->parameters_->end();
2563 ++p, ++shift)
2564 ret += p->type()->hash_for_method(gogo) << shift;
2565 }
2566 if (this->results_ != NULL)
2567 {
2568 int shift = 2;
2569 for (Typed_identifier_list::const_iterator p = this->results_->begin();
2570 p != this->results_->end();
2571 ++p, ++shift)
2572 ret += p->type()->hash_for_method(gogo) << shift;
2573 }
2574 if (this->is_varargs_)
2575 ret += 1;
2576 ret <<= 4;
2577 return ret;
2578 }
2579
2580 // Get the tree for a function type.
2581
2582 tree
2583 Function_type::do_get_tree(Gogo* gogo)
2584 {
2585 tree args = NULL_TREE;
2586 tree* pp = &args;
2587
2588 if (this->receiver_ != NULL)
2589 {
2590 Type* rtype = this->receiver_->type();
2591 tree ptype = rtype->get_tree(gogo);
2592 if (ptype == error_mark_node)
2593 return error_mark_node;
2594
2595 // We always pass the address of the receiver parameter, in
2596 // order to make interface calls work with unknown types.
2597 if (rtype->points_to() == NULL)
2598 ptype = build_pointer_type(ptype);
2599
2600 *pp = tree_cons (NULL_TREE, ptype, NULL_TREE);
2601 pp = &TREE_CHAIN (*pp);
2602 }
2603
2604 if (this->parameters_ != NULL)
2605 {
2606 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
2607 p != this->parameters_->end();
2608 ++p)
2609 {
2610 tree ptype = p->type()->get_tree(gogo);
2611 if (ptype == error_mark_node)
2612 return error_mark_node;
2613 *pp = tree_cons (NULL_TREE, ptype, NULL_TREE);
2614 pp = &TREE_CHAIN (*pp);
2615 }
2616 }
2617
2618 // Varargs is handled entirely at the Go level. At the tree level,
2619 // functions are not varargs.
2620 *pp = void_list_node;
2621
2622 tree result;
2623 if (this->results_ == NULL)
2624 result = void_type_node;
2625 else if (this->results_->size() == 1)
2626 result = this->results_->begin()->type()->get_tree(gogo);
2627 else
2628 {
2629 result = make_node(RECORD_TYPE);
2630 tree field_trees = NULL_TREE;
2631 tree* pp = &field_trees;
2632 for (Typed_identifier_list::const_iterator p = this->results_->begin();
2633 p != this->results_->end();
2634 ++p)
2635 {
2636 const std::string name = (p->name().empty()
2637 ? "UNNAMED"
2638 : Gogo::unpack_hidden_name(p->name()));
2639 tree name_tree = get_identifier_with_length(name.data(),
2640 name.length());
2641 tree field_type_tree = p->type()->get_tree(gogo);
2642 if (field_type_tree == error_mark_node)
2643 return error_mark_node;
2644 tree field = build_decl(this->location_, FIELD_DECL, name_tree,
2645 field_type_tree);
2646 DECL_CONTEXT(field) = result;
2647 *pp = field;
2648 pp = &DECL_CHAIN(field);
2649 }
2650 TYPE_FIELDS(result) = field_trees;
2651 layout_type(result);
2652 }
2653
2654 if (result == error_mark_node)
2655 return error_mark_node;
2656
2657 tree fntype = build_function_type(result, args);
2658 if (fntype == error_mark_node)
2659 return fntype;
2660
2661 return build_pointer_type(fntype);
2662 }
2663
2664 // Functions are initialized to NULL.
2665
2666 tree
2667 Function_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2668 {
2669 if (is_clear)
2670 return NULL;
2671 return fold_convert(type_tree, null_pointer_node);
2672 }
2673
2674 // The type of a function type descriptor.
2675
2676 Type*
2677 Function_type::make_function_type_descriptor_type()
2678 {
2679 static Type* ret;
2680 if (ret == NULL)
2681 {
2682 Type* tdt = Type::make_type_descriptor_type();
2683 Type* ptdt = Type::make_type_descriptor_ptr_type();
2684
2685 Type* bool_type = Type::lookup_bool_type();
2686
2687 Type* slice_type = Type::make_array_type(ptdt, NULL);
2688
2689 Struct_type* s = Type::make_builtin_struct_type(4,
2690 "", tdt,
2691 "dotdotdot", bool_type,
2692 "in", slice_type,
2693 "out", slice_type);
2694
2695 ret = Type::make_builtin_named_type("FuncType", s);
2696 }
2697
2698 return ret;
2699 }
2700
2701 // The type descriptor for a function type.
2702
2703 Expression*
2704 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2705 {
2706 source_location bloc = BUILTINS_LOCATION;
2707
2708 Type* ftdt = Function_type::make_function_type_descriptor_type();
2709
2710 const Struct_field_list* fields = ftdt->struct_type()->fields();
2711
2712 Expression_list* vals = new Expression_list();
2713 vals->reserve(4);
2714
2715 Struct_field_list::const_iterator p = fields->begin();
2716 gcc_assert(p->field_name() == "commonType");
2717 vals->push_back(this->type_descriptor_constructor(gogo,
2718 RUNTIME_TYPE_KIND_FUNC,
2719 name, NULL, true));
2720
2721 ++p;
2722 gcc_assert(p->field_name() == "dotdotdot");
2723 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
2724
2725 ++p;
2726 gcc_assert(p->field_name() == "in");
2727 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
2728 this->parameters()));
2729
2730 ++p;
2731 gcc_assert(p->field_name() == "out");
2732 vals->push_back(this->type_descriptor_params(p->type(), NULL,
2733 this->results()));
2734
2735 ++p;
2736 gcc_assert(p == fields->end());
2737
2738 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
2739 }
2740
2741 // Return a composite literal for the parameters or results of a type
2742 // descriptor.
2743
2744 Expression*
2745 Function_type::type_descriptor_params(Type* params_type,
2746 const Typed_identifier* receiver,
2747 const Typed_identifier_list* params)
2748 {
2749 source_location bloc = BUILTINS_LOCATION;
2750
2751 if (receiver == NULL && params == NULL)
2752 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
2753
2754 Expression_list* vals = new Expression_list();
2755 vals->reserve((params == NULL ? 0 : params->size())
2756 + (receiver != NULL ? 1 : 0));
2757
2758 if (receiver != NULL)
2759 {
2760 Type* rtype = receiver->type();
2761 // The receiver is always passed as a pointer. FIXME: Is this
2762 // right? Should that fact affect the type descriptor?
2763 if (rtype->points_to() == NULL)
2764 rtype = Type::make_pointer_type(rtype);
2765 vals->push_back(Expression::make_type_descriptor(rtype, bloc));
2766 }
2767
2768 if (params != NULL)
2769 {
2770 for (Typed_identifier_list::const_iterator p = params->begin();
2771 p != params->end();
2772 ++p)
2773 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
2774 }
2775
2776 return Expression::make_slice_composite_literal(params_type, vals, bloc);
2777 }
2778
2779 // The reflection string.
2780
2781 void
2782 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
2783 {
2784 // FIXME: Turn this off until we straighten out the type of the
2785 // struct field used in a go statement which calls a method.
2786 // gcc_assert(this->receiver_ == NULL);
2787
2788 ret->append("func");
2789
2790 if (this->receiver_ != NULL)
2791 {
2792 ret->push_back('(');
2793 this->append_reflection(this->receiver_->type(), gogo, ret);
2794 ret->push_back(')');
2795 }
2796
2797 ret->push_back('(');
2798 const Typed_identifier_list* params = this->parameters();
2799 if (params != NULL)
2800 {
2801 bool is_varargs = this->is_varargs_;
2802 for (Typed_identifier_list::const_iterator p = params->begin();
2803 p != params->end();
2804 ++p)
2805 {
2806 if (p != params->begin())
2807 ret->append(", ");
2808 if (!is_varargs || p + 1 != params->end())
2809 this->append_reflection(p->type(), gogo, ret);
2810 else
2811 {
2812 ret->append("...");
2813 this->append_reflection(p->type()->array_type()->element_type(),
2814 gogo, ret);
2815 }
2816 }
2817 }
2818 ret->push_back(')');
2819
2820 const Typed_identifier_list* results = this->results();
2821 if (results != NULL && !results->empty())
2822 {
2823 if (results->size() == 1)
2824 ret->push_back(' ');
2825 else
2826 ret->append(" (");
2827 for (Typed_identifier_list::const_iterator p = results->begin();
2828 p != results->end();
2829 ++p)
2830 {
2831 if (p != results->begin())
2832 ret->append(", ");
2833 this->append_reflection(p->type(), gogo, ret);
2834 }
2835 if (results->size() > 1)
2836 ret->push_back(')');
2837 }
2838 }
2839
2840 // Mangled name.
2841
2842 void
2843 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
2844 {
2845 ret->push_back('F');
2846
2847 if (this->receiver_ != NULL)
2848 {
2849 ret->push_back('m');
2850 this->append_mangled_name(this->receiver_->type(), gogo, ret);
2851 }
2852
2853 const Typed_identifier_list* params = this->parameters();
2854 if (params != NULL)
2855 {
2856 ret->push_back('p');
2857 for (Typed_identifier_list::const_iterator p = params->begin();
2858 p != params->end();
2859 ++p)
2860 this->append_mangled_name(p->type(), gogo, ret);
2861 if (this->is_varargs_)
2862 ret->push_back('V');
2863 ret->push_back('e');
2864 }
2865
2866 const Typed_identifier_list* results = this->results();
2867 if (results != NULL)
2868 {
2869 ret->push_back('r');
2870 for (Typed_identifier_list::const_iterator p = results->begin();
2871 p != results->end();
2872 ++p)
2873 this->append_mangled_name(p->type(), gogo, ret);
2874 ret->push_back('e');
2875 }
2876
2877 ret->push_back('e');
2878 }
2879
2880 // Export a function type.
2881
2882 void
2883 Function_type::do_export(Export* exp) const
2884 {
2885 // We don't write out the receiver. The only function types which
2886 // should have a receiver are the ones associated with explicitly
2887 // defined methods. For those the receiver type is written out by
2888 // Function::export_func.
2889
2890 exp->write_c_string("(");
2891 bool first = true;
2892 if (this->parameters_ != NULL)
2893 {
2894 bool is_varargs = this->is_varargs_;
2895 for (Typed_identifier_list::const_iterator p =
2896 this->parameters_->begin();
2897 p != this->parameters_->end();
2898 ++p)
2899 {
2900 if (first)
2901 first = false;
2902 else
2903 exp->write_c_string(", ");
2904 if (!is_varargs || p + 1 != this->parameters_->end())
2905 exp->write_type(p->type());
2906 else
2907 {
2908 exp->write_c_string("...");
2909 exp->write_type(p->type()->array_type()->element_type());
2910 }
2911 }
2912 }
2913 exp->write_c_string(")");
2914
2915 const Typed_identifier_list* results = this->results_;
2916 if (results != NULL)
2917 {
2918 exp->write_c_string(" ");
2919 if (results->size() == 1)
2920 exp->write_type(results->begin()->type());
2921 else
2922 {
2923 first = true;
2924 exp->write_c_string("(");
2925 for (Typed_identifier_list::const_iterator p = results->begin();
2926 p != results->end();
2927 ++p)
2928 {
2929 if (first)
2930 first = false;
2931 else
2932 exp->write_c_string(", ");
2933 exp->write_type(p->type());
2934 }
2935 exp->write_c_string(")");
2936 }
2937 }
2938 }
2939
2940 // Import a function type.
2941
2942 Function_type*
2943 Function_type::do_import(Import* imp)
2944 {
2945 imp->require_c_string("(");
2946 Typed_identifier_list* parameters;
2947 bool is_varargs = false;
2948 if (imp->peek_char() == ')')
2949 parameters = NULL;
2950 else
2951 {
2952 parameters = new Typed_identifier_list();
2953 while (true)
2954 {
2955 if (imp->match_c_string("..."))
2956 {
2957 imp->advance(3);
2958 is_varargs = true;
2959 }
2960
2961 Type* ptype = imp->read_type();
2962 if (is_varargs)
2963 ptype = Type::make_array_type(ptype, NULL);
2964 parameters->push_back(Typed_identifier(Import::import_marker,
2965 ptype, imp->location()));
2966 if (imp->peek_char() != ',')
2967 break;
2968 gcc_assert(!is_varargs);
2969 imp->require_c_string(", ");
2970 }
2971 }
2972 imp->require_c_string(")");
2973
2974 Typed_identifier_list* results;
2975 if (imp->peek_char() != ' ')
2976 results = NULL;
2977 else
2978 {
2979 imp->advance(1);
2980 results = new Typed_identifier_list;
2981 if (imp->peek_char() != '(')
2982 {
2983 Type* rtype = imp->read_type();
2984 results->push_back(Typed_identifier(Import::import_marker, rtype,
2985 imp->location()));
2986 }
2987 else
2988 {
2989 imp->advance(1);
2990 while (true)
2991 {
2992 Type* rtype = imp->read_type();
2993 results->push_back(Typed_identifier(Import::import_marker,
2994 rtype, imp->location()));
2995 if (imp->peek_char() != ',')
2996 break;
2997 imp->require_c_string(", ");
2998 }
2999 imp->require_c_string(")");
3000 }
3001 }
3002
3003 Function_type* ret = Type::make_function_type(NULL, parameters, results,
3004 imp->location());
3005 if (is_varargs)
3006 ret->set_is_varargs();
3007 return ret;
3008 }
3009
3010 // Make a copy of a function type without a receiver.
3011
3012 Function_type*
3013 Function_type::copy_without_receiver() const
3014 {
3015 gcc_assert(this->is_method());
3016 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3017 this->results_,
3018 this->location_);
3019 if (this->is_varargs())
3020 ret->set_is_varargs();
3021 if (this->is_builtin())
3022 ret->set_is_builtin();
3023 return ret;
3024 }
3025
3026 // Make a copy of a function type with a receiver.
3027
3028 Function_type*
3029 Function_type::copy_with_receiver(Type* receiver_type) const
3030 {
3031 gcc_assert(!this->is_method());
3032 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3033 this->location_);
3034 return Type::make_function_type(receiver, this->parameters_,
3035 this->results_, this->location_);
3036 }
3037
3038 // Make a function type.
3039
3040 Function_type*
3041 Type::make_function_type(Typed_identifier* receiver,
3042 Typed_identifier_list* parameters,
3043 Typed_identifier_list* results,
3044 source_location location)
3045 {
3046 return new Function_type(receiver, parameters, results, location);
3047 }
3048
3049 // Class Pointer_type.
3050
3051 // Traversal.
3052
3053 int
3054 Pointer_type::do_traverse(Traverse* traverse)
3055 {
3056 return Type::traverse(this->to_type_, traverse);
3057 }
3058
3059 // Hash code.
3060
3061 unsigned int
3062 Pointer_type::do_hash_for_method(Gogo* gogo) const
3063 {
3064 return this->to_type_->hash_for_method(gogo) << 4;
3065 }
3066
3067 // The tree for a pointer type.
3068
3069 tree
3070 Pointer_type::do_get_tree(Gogo* gogo)
3071 {
3072 return build_pointer_type(this->to_type_->get_tree(gogo));
3073 }
3074
3075 // Initialize a pointer type.
3076
3077 tree
3078 Pointer_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
3079 {
3080 if (is_clear)
3081 return NULL;
3082 return fold_convert(type_tree, null_pointer_node);
3083 }
3084
3085 // The type of a pointer type descriptor.
3086
3087 Type*
3088 Pointer_type::make_pointer_type_descriptor_type()
3089 {
3090 static Type* ret;
3091 if (ret == NULL)
3092 {
3093 Type* tdt = Type::make_type_descriptor_type();
3094 Type* ptdt = Type::make_type_descriptor_ptr_type();
3095
3096 Struct_type* s = Type::make_builtin_struct_type(2,
3097 "", tdt,
3098 "elem", ptdt);
3099
3100 ret = Type::make_builtin_named_type("PtrType", s);
3101 }
3102
3103 return ret;
3104 }
3105
3106 // The type descriptor for a pointer type.
3107
3108 Expression*
3109 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3110 {
3111 if (this->is_unsafe_pointer_type())
3112 {
3113 gcc_assert(name != NULL);
3114 return this->plain_type_descriptor(gogo,
3115 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3116 name);
3117 }
3118 else
3119 {
3120 source_location bloc = BUILTINS_LOCATION;
3121
3122 const Methods* methods;
3123 Type* deref = this->points_to();
3124 if (deref->named_type() != NULL)
3125 methods = deref->named_type()->methods();
3126 else if (deref->struct_type() != NULL)
3127 methods = deref->struct_type()->methods();
3128 else
3129 methods = NULL;
3130
3131 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3132
3133 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3134
3135 Expression_list* vals = new Expression_list();
3136 vals->reserve(2);
3137
3138 Struct_field_list::const_iterator p = fields->begin();
3139 gcc_assert(p->field_name() == "commonType");
3140 vals->push_back(this->type_descriptor_constructor(gogo,
3141 RUNTIME_TYPE_KIND_PTR,
3142 name, methods, false));
3143
3144 ++p;
3145 gcc_assert(p->field_name() == "elem");
3146 vals->push_back(Expression::make_type_descriptor(deref, bloc));
3147
3148 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3149 }
3150 }
3151
3152 // Reflection string.
3153
3154 void
3155 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3156 {
3157 ret->push_back('*');
3158 this->append_reflection(this->to_type_, gogo, ret);
3159 }
3160
3161 // Mangled name.
3162
3163 void
3164 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3165 {
3166 ret->push_back('p');
3167 this->append_mangled_name(this->to_type_, gogo, ret);
3168 }
3169
3170 // Export.
3171
3172 void
3173 Pointer_type::do_export(Export* exp) const
3174 {
3175 exp->write_c_string("*");
3176 if (this->is_unsafe_pointer_type())
3177 exp->write_c_string("any");
3178 else
3179 exp->write_type(this->to_type_);
3180 }
3181
3182 // Import.
3183
3184 Pointer_type*
3185 Pointer_type::do_import(Import* imp)
3186 {
3187 imp->require_c_string("*");
3188 if (imp->match_c_string("any"))
3189 {
3190 imp->advance(3);
3191 return Type::make_pointer_type(Type::make_void_type());
3192 }
3193 Type* to = imp->read_type();
3194 return Type::make_pointer_type(to);
3195 }
3196
3197 // Make a pointer type.
3198
3199 Pointer_type*
3200 Type::make_pointer_type(Type* to_type)
3201 {
3202 typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3203 static Hashtable pointer_types;
3204 Hashtable::const_iterator p = pointer_types.find(to_type);
3205 if (p != pointer_types.end())
3206 return p->second;
3207 Pointer_type* ret = new Pointer_type(to_type);
3208 pointer_types[to_type] = ret;
3209 return ret;
3210 }
3211
3212 // The nil type. We use a special type for nil because it is not the
3213 // same as any other type. In C term nil has type void*, but there is
3214 // no such type in Go.
3215
3216 class Nil_type : public Type
3217 {
3218 public:
3219 Nil_type()
3220 : Type(TYPE_NIL)
3221 { }
3222
3223 protected:
3224 tree
3225 do_get_tree(Gogo*)
3226 { return ptr_type_node; }
3227
3228 tree
3229 do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
3230 { return is_clear ? NULL : fold_convert(type_tree, null_pointer_node); }
3231
3232 Expression*
3233 do_type_descriptor(Gogo*, Named_type*)
3234 { gcc_unreachable(); }
3235
3236 void
3237 do_reflection(Gogo*, std::string*) const
3238 { gcc_unreachable(); }
3239
3240 void
3241 do_mangled_name(Gogo*, std::string* ret) const
3242 { ret->push_back('n'); }
3243 };
3244
3245 // Make the nil type.
3246
3247 Type*
3248 Type::make_nil_type()
3249 {
3250 static Nil_type singleton_nil_type;
3251 return &singleton_nil_type;
3252 }
3253
3254 // The type of a function call which returns multiple values. This is
3255 // really a struct, but we don't want to confuse a function call which
3256 // returns a struct with a function call which returns multiple
3257 // values.
3258
3259 class Call_multiple_result_type : public Type
3260 {
3261 public:
3262 Call_multiple_result_type(Call_expression* call)
3263 : Type(TYPE_CALL_MULTIPLE_RESULT),
3264 call_(call)
3265 { }
3266
3267 protected:
3268 bool
3269 do_has_pointer() const
3270 {
3271 gcc_assert(saw_errors());
3272 return false;
3273 }
3274
3275 tree
3276 do_get_tree(Gogo*);
3277
3278 tree
3279 do_get_init_tree(Gogo*, tree, bool)
3280 {
3281 gcc_assert(saw_errors());
3282 return error_mark_node;
3283 }
3284
3285 Expression*
3286 do_type_descriptor(Gogo*, Named_type*)
3287 {
3288 gcc_assert(saw_errors());
3289 return Expression::make_error(UNKNOWN_LOCATION);
3290 }
3291
3292 void
3293 do_reflection(Gogo*, std::string*) const
3294 { gcc_assert(saw_errors()); }
3295
3296 void
3297 do_mangled_name(Gogo*, std::string*) const
3298 { gcc_assert(saw_errors()); }
3299
3300 private:
3301 // The expression being called.
3302 Call_expression* call_;
3303 };
3304
3305 // Return the tree for a call result.
3306
3307 tree
3308 Call_multiple_result_type::do_get_tree(Gogo* gogo)
3309 {
3310 Function_type* fntype = this->call_->get_function_type();
3311 gcc_assert(fntype != NULL);
3312 const Typed_identifier_list* results = fntype->results();
3313 gcc_assert(results != NULL && results->size() > 1);
3314
3315 Struct_field_list* sfl = new Struct_field_list;
3316 for (Typed_identifier_list::const_iterator p = results->begin();
3317 p != results->end();
3318 ++p)
3319 {
3320 const std::string name = ((p->name().empty()
3321 || p->name() == Import::import_marker)
3322 ? "UNNAMED"
3323 : p->name());
3324 sfl->push_back(Struct_field(Typed_identifier(name, p->type(),
3325 this->call_->location())));
3326 }
3327 return Type::make_struct_type(sfl, this->call_->location())->get_tree(gogo);
3328 }
3329
3330 // Make a call result type.
3331
3332 Type*
3333 Type::make_call_multiple_result_type(Call_expression* call)
3334 {
3335 return new Call_multiple_result_type(call);
3336 }
3337
3338 // Class Struct_field.
3339
3340 // Get the name of a field.
3341
3342 const std::string&
3343 Struct_field::field_name() const
3344 {
3345 const std::string& name(this->typed_identifier_.name());
3346 if (!name.empty())
3347 return name;
3348 else
3349 {
3350 // This is called during parsing, before anything is lowered, so
3351 // we have to be pretty careful to avoid dereferencing an
3352 // unknown type name.
3353 Type* t = this->typed_identifier_.type();
3354 Type* dt = t;
3355 if (t->classification() == Type::TYPE_POINTER)
3356 {
3357 // Very ugly.
3358 Pointer_type* ptype = static_cast<Pointer_type*>(t);
3359 dt = ptype->points_to();
3360 }
3361 if (dt->forward_declaration_type() != NULL)
3362 return dt->forward_declaration_type()->name();
3363 else if (dt->named_type() != NULL)
3364 return dt->named_type()->name();
3365 else if (t->is_error_type() || dt->is_error_type())
3366 {
3367 static const std::string error_string = "*error*";
3368 return error_string;
3369 }
3370 else
3371 {
3372 // Avoid crashing in the erroneous case where T is named but
3373 // DT is not.
3374 gcc_assert(t != dt);
3375 if (t->forward_declaration_type() != NULL)
3376 return t->forward_declaration_type()->name();
3377 else if (t->named_type() != NULL)
3378 return t->named_type()->name();
3379 else
3380 gcc_unreachable();
3381 }
3382 }
3383 }
3384
3385 // Class Struct_type.
3386
3387 // Traversal.
3388
3389 int
3390 Struct_type::do_traverse(Traverse* traverse)
3391 {
3392 Struct_field_list* fields = this->fields_;
3393 if (fields != NULL)
3394 {
3395 for (Struct_field_list::iterator p = fields->begin();
3396 p != fields->end();
3397 ++p)
3398 {
3399 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
3400 return TRAVERSE_EXIT;
3401 }
3402 }
3403 return TRAVERSE_CONTINUE;
3404 }
3405
3406 // Verify that the struct type is complete and valid.
3407
3408 bool
3409 Struct_type::do_verify()
3410 {
3411 Struct_field_list* fields = this->fields_;
3412 if (fields == NULL)
3413 return true;
3414 bool ret = true;
3415 for (Struct_field_list::iterator p = fields->begin();
3416 p != fields->end();
3417 ++p)
3418 {
3419 Type* t = p->type();
3420 if (t->is_undefined())
3421 {
3422 error_at(p->location(), "struct field type is incomplete");
3423 p->set_type(Type::make_error_type());
3424 ret = false;
3425 }
3426 else if (p->is_anonymous())
3427 {
3428 if (t->named_type() != NULL && t->points_to() != NULL)
3429 {
3430 error_at(p->location(), "embedded type may not be a pointer");
3431 p->set_type(Type::make_error_type());
3432 return false;
3433 }
3434 }
3435 }
3436 return ret;
3437 }
3438
3439 // Whether this contains a pointer.
3440
3441 bool
3442 Struct_type::do_has_pointer() const
3443 {
3444 const Struct_field_list* fields = this->fields();
3445 if (fields == NULL)
3446 return false;
3447 for (Struct_field_list::const_iterator p = fields->begin();
3448 p != fields->end();
3449 ++p)
3450 {
3451 if (p->type()->has_pointer())
3452 return true;
3453 }
3454 return false;
3455 }
3456
3457 // Whether this type is identical to T.
3458
3459 bool
3460 Struct_type::is_identical(const Struct_type* t,
3461 bool errors_are_identical) const
3462 {
3463 const Struct_field_list* fields1 = this->fields();
3464 const Struct_field_list* fields2 = t->fields();
3465 if (fields1 == NULL || fields2 == NULL)
3466 return fields1 == fields2;
3467 Struct_field_list::const_iterator pf2 = fields2->begin();
3468 for (Struct_field_list::const_iterator pf1 = fields1->begin();
3469 pf1 != fields1->end();
3470 ++pf1, ++pf2)
3471 {
3472 if (pf2 == fields2->end())
3473 return false;
3474 if (pf1->field_name() != pf2->field_name())
3475 return false;
3476 if (pf1->is_anonymous() != pf2->is_anonymous()
3477 || !Type::are_identical(pf1->type(), pf2->type(),
3478 errors_are_identical, NULL))
3479 return false;
3480 if (!pf1->has_tag())
3481 {
3482 if (pf2->has_tag())
3483 return false;
3484 }
3485 else
3486 {
3487 if (!pf2->has_tag())
3488 return false;
3489 if (pf1->tag() != pf2->tag())
3490 return false;
3491 }
3492 }
3493 if (pf2 != fields2->end())
3494 return false;
3495 return true;
3496 }
3497
3498 // Whether this struct type has any hidden fields.
3499
3500 bool
3501 Struct_type::struct_has_hidden_fields(const Named_type* within,
3502 std::string* reason) const
3503 {
3504 const Struct_field_list* fields = this->fields();
3505 if (fields == NULL)
3506 return false;
3507 const Package* within_package = (within == NULL
3508 ? NULL
3509 : within->named_object()->package());
3510 for (Struct_field_list::const_iterator pf = fields->begin();
3511 pf != fields->end();
3512 ++pf)
3513 {
3514 if (within_package != NULL
3515 && !pf->is_anonymous()
3516 && Gogo::is_hidden_name(pf->field_name()))
3517 {
3518 if (reason != NULL)
3519 {
3520 std::string within_name = within->named_object()->message_name();
3521 std::string name = Gogo::message_name(pf->field_name());
3522 size_t bufsize = 200 + within_name.length() + name.length();
3523 char* buf = new char[bufsize];
3524 snprintf(buf, bufsize,
3525 _("implicit assignment of %s%s%s hidden field %s%s%s"),
3526 open_quote, within_name.c_str(), close_quote,
3527 open_quote, name.c_str(), close_quote);
3528 reason->assign(buf);
3529 delete[] buf;
3530 }
3531 return true;
3532 }
3533
3534 if (pf->type()->has_hidden_fields(within, reason))
3535 return true;
3536 }
3537
3538 return false;
3539 }
3540
3541 // Hash code.
3542
3543 unsigned int
3544 Struct_type::do_hash_for_method(Gogo* gogo) const
3545 {
3546 unsigned int ret = 0;
3547 if (this->fields() != NULL)
3548 {
3549 for (Struct_field_list::const_iterator pf = this->fields()->begin();
3550 pf != this->fields()->end();
3551 ++pf)
3552 ret = (ret << 1) + pf->type()->hash_for_method(gogo);
3553 }
3554 return ret <<= 2;
3555 }
3556
3557 // Find the local field NAME.
3558
3559 const Struct_field*
3560 Struct_type::find_local_field(const std::string& name,
3561 unsigned int *pindex) const
3562 {
3563 const Struct_field_list* fields = this->fields_;
3564 if (fields == NULL)
3565 return NULL;
3566 unsigned int i = 0;
3567 for (Struct_field_list::const_iterator pf = fields->begin();
3568 pf != fields->end();
3569 ++pf, ++i)
3570 {
3571 if (pf->field_name() == name)
3572 {
3573 if (pindex != NULL)
3574 *pindex = i;
3575 return &*pf;
3576 }
3577 }
3578 return NULL;
3579 }
3580
3581 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
3582
3583 Field_reference_expression*
3584 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
3585 source_location location) const
3586 {
3587 unsigned int depth;
3588 return this->field_reference_depth(struct_expr, name, location, &depth);
3589 }
3590
3591 // Return an expression for a field, along with the depth at which it
3592 // was found.
3593
3594 Field_reference_expression*
3595 Struct_type::field_reference_depth(Expression* struct_expr,
3596 const std::string& name,
3597 source_location location,
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 // Look for a reference using a NULL struct expression. If we
3634 // find one, fill in the struct expression with a reference to
3635 // this field.
3636 unsigned int subdepth;
3637 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
3638 location,
3639 &subdepth);
3640 if (sub == NULL)
3641 continue;
3642
3643 if (ret == NULL || subdepth < found_depth)
3644 {
3645 if (ret != NULL)
3646 delete ret;
3647 ret = sub;
3648 found_depth = subdepth;
3649 Expression* here = Expression::make_field_reference(struct_expr, i,
3650 location);
3651 if (pf->type()->points_to() != NULL)
3652 here = Expression::make_unary(OPERATOR_MULT, here, location);
3653 while (sub->expr() != NULL)
3654 {
3655 sub = sub->expr()->deref()->field_reference_expression();
3656 gcc_assert(sub != NULL);
3657 }
3658 sub->set_struct_expression(here);
3659 }
3660 else if (subdepth > found_depth)
3661 delete sub;
3662 else
3663 {
3664 // We do not handle ambiguity here--it should be handled by
3665 // Type::bind_field_or_method.
3666 delete sub;
3667 found_depth = 0;
3668 ret = NULL;
3669 }
3670 }
3671
3672 if (ret != NULL)
3673 *depth = found_depth + 1;
3674
3675 return ret;
3676 }
3677
3678 // Return the total number of fields, including embedded fields.
3679
3680 unsigned int
3681 Struct_type::total_field_count() const
3682 {
3683 if (this->fields_ == NULL)
3684 return 0;
3685 unsigned int ret = 0;
3686 for (Struct_field_list::const_iterator pf = this->fields_->begin();
3687 pf != this->fields_->end();
3688 ++pf)
3689 {
3690 if (!pf->is_anonymous() || pf->type()->deref()->struct_type() == NULL)
3691 ++ret;
3692 else
3693 ret += pf->type()->struct_type()->total_field_count();
3694 }
3695 return ret;
3696 }
3697
3698 // Return whether NAME is an unexported field, for better error reporting.
3699
3700 bool
3701 Struct_type::is_unexported_local_field(Gogo* gogo,
3702 const std::string& name) const
3703 {
3704 const Struct_field_list* fields = this->fields_;
3705 if (fields != NULL)
3706 {
3707 for (Struct_field_list::const_iterator pf = fields->begin();
3708 pf != fields->end();
3709 ++pf)
3710 {
3711 const std::string& field_name(pf->field_name());
3712 if (Gogo::is_hidden_name(field_name)
3713 && name == Gogo::unpack_hidden_name(field_name)
3714 && gogo->pack_hidden_name(name, false) != field_name)
3715 return true;
3716 }
3717 }
3718 return false;
3719 }
3720
3721 // Finalize the methods of an unnamed struct.
3722
3723 void
3724 Struct_type::finalize_methods(Gogo* gogo)
3725 {
3726 if (this->all_methods_ != NULL)
3727 return;
3728 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
3729 }
3730
3731 // Return the method NAME, or NULL if there isn't one or if it is
3732 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
3733 // ambiguous.
3734
3735 Method*
3736 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
3737 {
3738 return Type::method_function(this->all_methods_, name, is_ambiguous);
3739 }
3740
3741 // Get the tree for a struct type.
3742
3743 tree
3744 Struct_type::do_get_tree(Gogo* gogo)
3745 {
3746 tree type = make_node(RECORD_TYPE);
3747 return this->fill_in_tree(gogo, type);
3748 }
3749
3750 // Fill in the fields for a struct type.
3751
3752 tree
3753 Struct_type::fill_in_tree(Gogo* gogo, tree type)
3754 {
3755 tree field_trees = NULL_TREE;
3756 tree* pp = &field_trees;
3757 bool has_pointer = false;
3758 for (Struct_field_list::const_iterator p = this->fields_->begin();
3759 p != this->fields_->end();
3760 ++p)
3761 {
3762 std::string name = Gogo::unpack_hidden_name(p->field_name());
3763 tree name_tree = get_identifier_with_length(name.data(), name.length());
3764
3765 // Don't follow pointers yet, so that we don't get confused by a
3766 // pointer to an array of this struct type.
3767 tree field_type_tree;
3768 if (p->type()->points_to() != NULL)
3769 {
3770 field_type_tree = ptr_type_node;
3771 has_pointer = true;
3772 }
3773 else
3774 {
3775 field_type_tree = p->type()->get_tree(gogo);
3776 if (field_type_tree == error_mark_node)
3777 return error_mark_node;
3778 }
3779
3780 tree field = build_decl(p->location(), FIELD_DECL, name_tree,
3781 field_type_tree);
3782 DECL_CONTEXT(field) = type;
3783 *pp = field;
3784 pp = &DECL_CHAIN(field);
3785 }
3786
3787 TYPE_FIELDS(type) = field_trees;
3788
3789 layout_type(type);
3790
3791 if (has_pointer)
3792 {
3793 tree field = field_trees;
3794 for (Struct_field_list::const_iterator p = this->fields_->begin();
3795 p != this->fields_->end();
3796 ++p, field = DECL_CHAIN(field))
3797 {
3798 if (p->type()->points_to() != NULL)
3799 TREE_TYPE(field) = p->type()->get_tree(gogo);
3800 }
3801 }
3802
3803 return type;
3804 }
3805
3806 // Make sure that all structs which must be converted to the backend
3807 // representation before this one are in fact converted.
3808
3809 void
3810 Struct_type::convert_prerequisites(Gogo* gogo)
3811 {
3812 for (std::vector<Named_type*>::const_iterator p
3813 = this->prerequisites_.begin();
3814 p != this->prerequisites_.end();
3815 ++p)
3816 (*p)->get_tree(gogo);
3817 }
3818
3819 // Initialize struct fields.
3820
3821 tree
3822 Struct_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
3823 {
3824 if (this->fields_ == NULL || this->fields_->empty())
3825 {
3826 if (is_clear)
3827 return NULL;
3828 else
3829 {
3830 tree ret = build_constructor(type_tree,
3831 VEC_alloc(constructor_elt, gc, 0));
3832 TREE_CONSTANT(ret) = 1;
3833 return ret;
3834 }
3835 }
3836
3837 bool is_constant = true;
3838 bool any_fields_set = false;
3839 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc,
3840 this->fields_->size());
3841
3842 tree field = TYPE_FIELDS(type_tree);
3843 for (Struct_field_list::const_iterator p = this->fields_->begin();
3844 p != this->fields_->end();
3845 ++p, field = DECL_CHAIN(field))
3846 {
3847 tree value = p->type()->get_init_tree(gogo, is_clear);
3848 if (value == error_mark_node)
3849 return error_mark_node;
3850 gcc_assert(field != NULL_TREE);
3851 if (value != NULL)
3852 {
3853 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
3854 elt->index = field;
3855 elt->value = value;
3856 any_fields_set = true;
3857 if (!TREE_CONSTANT(value))
3858 is_constant = false;
3859 }
3860 }
3861 gcc_assert(field == NULL_TREE);
3862
3863 if (!any_fields_set)
3864 {
3865 gcc_assert(is_clear);
3866 VEC_free(constructor_elt, gc, init);
3867 return NULL;
3868 }
3869
3870 tree ret = build_constructor(type_tree, init);
3871 if (is_constant)
3872 TREE_CONSTANT(ret) = 1;
3873 return ret;
3874 }
3875
3876 // The type of a struct type descriptor.
3877
3878 Type*
3879 Struct_type::make_struct_type_descriptor_type()
3880 {
3881 static Type* ret;
3882 if (ret == NULL)
3883 {
3884 Type* tdt = Type::make_type_descriptor_type();
3885 Type* ptdt = Type::make_type_descriptor_ptr_type();
3886
3887 Type* uintptr_type = Type::lookup_integer_type("uintptr");
3888 Type* string_type = Type::lookup_string_type();
3889 Type* pointer_string_type = Type::make_pointer_type(string_type);
3890
3891 Struct_type* sf =
3892 Type::make_builtin_struct_type(5,
3893 "name", pointer_string_type,
3894 "pkgPath", pointer_string_type,
3895 "typ", ptdt,
3896 "tag", pointer_string_type,
3897 "offset", uintptr_type);
3898 Type* nsf = Type::make_builtin_named_type("structField", sf);
3899
3900 Type* slice_type = Type::make_array_type(nsf, NULL);
3901
3902 Struct_type* s = Type::make_builtin_struct_type(2,
3903 "", tdt,
3904 "fields", slice_type);
3905
3906 ret = Type::make_builtin_named_type("StructType", s);
3907 }
3908
3909 return ret;
3910 }
3911
3912 // Build a type descriptor for a struct type.
3913
3914 Expression*
3915 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3916 {
3917 source_location bloc = BUILTINS_LOCATION;
3918
3919 Type* stdt = Struct_type::make_struct_type_descriptor_type();
3920
3921 const Struct_field_list* fields = stdt->struct_type()->fields();
3922
3923 Expression_list* vals = new Expression_list();
3924 vals->reserve(2);
3925
3926 const Methods* methods = this->methods();
3927 // A named struct should not have methods--the methods should attach
3928 // to the named type.
3929 gcc_assert(methods == NULL || name == NULL);
3930
3931 Struct_field_list::const_iterator ps = fields->begin();
3932 gcc_assert(ps->field_name() == "commonType");
3933 vals->push_back(this->type_descriptor_constructor(gogo,
3934 RUNTIME_TYPE_KIND_STRUCT,
3935 name, methods, true));
3936
3937 ++ps;
3938 gcc_assert(ps->field_name() == "fields");
3939
3940 Expression_list* elements = new Expression_list();
3941 elements->reserve(this->fields_->size());
3942 Type* element_type = ps->type()->array_type()->element_type();
3943 for (Struct_field_list::const_iterator pf = this->fields_->begin();
3944 pf != this->fields_->end();
3945 ++pf)
3946 {
3947 const Struct_field_list* f = element_type->struct_type()->fields();
3948
3949 Expression_list* fvals = new Expression_list();
3950 fvals->reserve(5);
3951
3952 Struct_field_list::const_iterator q = f->begin();
3953 gcc_assert(q->field_name() == "name");
3954 if (pf->is_anonymous())
3955 fvals->push_back(Expression::make_nil(bloc));
3956 else
3957 {
3958 std::string n = Gogo::unpack_hidden_name(pf->field_name());
3959 Expression* s = Expression::make_string(n, bloc);
3960 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3961 }
3962
3963 ++q;
3964 gcc_assert(q->field_name() == "pkgPath");
3965 if (!Gogo::is_hidden_name(pf->field_name()))
3966 fvals->push_back(Expression::make_nil(bloc));
3967 else
3968 {
3969 std::string n = Gogo::hidden_name_prefix(pf->field_name());
3970 Expression* s = Expression::make_string(n, bloc);
3971 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3972 }
3973
3974 ++q;
3975 gcc_assert(q->field_name() == "typ");
3976 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
3977
3978 ++q;
3979 gcc_assert(q->field_name() == "tag");
3980 if (!pf->has_tag())
3981 fvals->push_back(Expression::make_nil(bloc));
3982 else
3983 {
3984 Expression* s = Expression::make_string(pf->tag(), bloc);
3985 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3986 }
3987
3988 ++q;
3989 gcc_assert(q->field_name() == "offset");
3990 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
3991
3992 Expression* v = Expression::make_struct_composite_literal(element_type,
3993 fvals, bloc);
3994 elements->push_back(v);
3995 }
3996
3997 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
3998 elements, bloc));
3999
4000 return Expression::make_struct_composite_literal(stdt, vals, bloc);
4001 }
4002
4003 // Reflection string.
4004
4005 void
4006 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
4007 {
4008 ret->append("struct { ");
4009
4010 for (Struct_field_list::const_iterator p = this->fields_->begin();
4011 p != this->fields_->end();
4012 ++p)
4013 {
4014 if (p != this->fields_->begin())
4015 ret->append("; ");
4016 if (p->is_anonymous())
4017 ret->push_back('?');
4018 else
4019 ret->append(Gogo::unpack_hidden_name(p->field_name()));
4020 ret->push_back(' ');
4021 this->append_reflection(p->type(), gogo, ret);
4022
4023 if (p->has_tag())
4024 {
4025 const std::string& tag(p->tag());
4026 ret->append(" \"");
4027 for (std::string::const_iterator p = tag.begin();
4028 p != tag.end();
4029 ++p)
4030 {
4031 if (*p == '\0')
4032 ret->append("\\x00");
4033 else if (*p == '\n')
4034 ret->append("\\n");
4035 else if (*p == '\t')
4036 ret->append("\\t");
4037 else if (*p == '"')
4038 ret->append("\\\"");
4039 else if (*p == '\\')
4040 ret->append("\\\\");
4041 else
4042 ret->push_back(*p);
4043 }
4044 ret->push_back('"');
4045 }
4046 }
4047
4048 ret->append(" }");
4049 }
4050
4051 // Mangled name.
4052
4053 void
4054 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4055 {
4056 ret->push_back('S');
4057
4058 const Struct_field_list* fields = this->fields_;
4059 if (fields != NULL)
4060 {
4061 for (Struct_field_list::const_iterator p = fields->begin();
4062 p != fields->end();
4063 ++p)
4064 {
4065 if (p->is_anonymous())
4066 ret->append("0_");
4067 else
4068 {
4069 std::string n = Gogo::unpack_hidden_name(p->field_name());
4070 char buf[20];
4071 snprintf(buf, sizeof buf, "%u_",
4072 static_cast<unsigned int>(n.length()));
4073 ret->append(buf);
4074 ret->append(n);
4075 }
4076 this->append_mangled_name(p->type(), gogo, ret);
4077 if (p->has_tag())
4078 {
4079 const std::string& tag(p->tag());
4080 std::string out;
4081 for (std::string::const_iterator p = tag.begin();
4082 p != tag.end();
4083 ++p)
4084 {
4085 if (ISALNUM(*p) || *p == '_')
4086 out.push_back(*p);
4087 else
4088 {
4089 char buf[20];
4090 snprintf(buf, sizeof buf, ".%x.",
4091 static_cast<unsigned int>(*p));
4092 out.append(buf);
4093 }
4094 }
4095 char buf[20];
4096 snprintf(buf, sizeof buf, "T%u_",
4097 static_cast<unsigned int>(out.length()));
4098 ret->append(buf);
4099 ret->append(out);
4100 }
4101 }
4102 }
4103
4104 ret->push_back('e');
4105 }
4106
4107 // Export.
4108
4109 void
4110 Struct_type::do_export(Export* exp) const
4111 {
4112 exp->write_c_string("struct { ");
4113 const Struct_field_list* fields = this->fields_;
4114 gcc_assert(fields != NULL);
4115 for (Struct_field_list::const_iterator p = fields->begin();
4116 p != fields->end();
4117 ++p)
4118 {
4119 if (p->is_anonymous())
4120 exp->write_string("? ");
4121 else
4122 {
4123 exp->write_string(p->field_name());
4124 exp->write_c_string(" ");
4125 }
4126 exp->write_type(p->type());
4127
4128 if (p->has_tag())
4129 {
4130 exp->write_c_string(" ");
4131 Expression* expr = Expression::make_string(p->tag(),
4132 BUILTINS_LOCATION);
4133 expr->export_expression(exp);
4134 delete expr;
4135 }
4136
4137 exp->write_c_string("; ");
4138 }
4139 exp->write_c_string("}");
4140 }
4141
4142 // Import.
4143
4144 Struct_type*
4145 Struct_type::do_import(Import* imp)
4146 {
4147 imp->require_c_string("struct { ");
4148 Struct_field_list* fields = new Struct_field_list;
4149 if (imp->peek_char() != '}')
4150 {
4151 while (true)
4152 {
4153 std::string name;
4154 if (imp->match_c_string("? "))
4155 imp->advance(2);
4156 else
4157 {
4158 name = imp->read_identifier();
4159 imp->require_c_string(" ");
4160 }
4161 Type* ftype = imp->read_type();
4162
4163 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
4164
4165 if (imp->peek_char() == ' ')
4166 {
4167 imp->advance(1);
4168 Expression* expr = Expression::import_expression(imp);
4169 String_expression* sexpr = expr->string_expression();
4170 gcc_assert(sexpr != NULL);
4171 sf.set_tag(sexpr->val());
4172 delete sexpr;
4173 }
4174
4175 imp->require_c_string("; ");
4176 fields->push_back(sf);
4177 if (imp->peek_char() == '}')
4178 break;
4179 }
4180 }
4181 imp->require_c_string("}");
4182
4183 return Type::make_struct_type(fields, imp->location());
4184 }
4185
4186 // Make a struct type.
4187
4188 Struct_type*
4189 Type::make_struct_type(Struct_field_list* fields,
4190 source_location location)
4191 {
4192 return new Struct_type(fields, location);
4193 }
4194
4195 // Class Array_type.
4196
4197 // Whether two array types are identical.
4198
4199 bool
4200 Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
4201 {
4202 if (!Type::are_identical(this->element_type(), t->element_type(),
4203 errors_are_identical, NULL))
4204 return false;
4205
4206 Expression* l1 = this->length();
4207 Expression* l2 = t->length();
4208
4209 // Slices of the same element type are identical.
4210 if (l1 == NULL && l2 == NULL)
4211 return true;
4212
4213 // Arrays of the same element type are identical if they have the
4214 // same length.
4215 if (l1 != NULL && l2 != NULL)
4216 {
4217 if (l1 == l2)
4218 return true;
4219
4220 // Try to determine the lengths. If we can't, assume the arrays
4221 // are not identical.
4222 bool ret = false;
4223 mpz_t v1;
4224 mpz_init(v1);
4225 Type* type1;
4226 mpz_t v2;
4227 mpz_init(v2);
4228 Type* type2;
4229 if (l1->integer_constant_value(true, v1, &type1)
4230 && l2->integer_constant_value(true, v2, &type2))
4231 ret = mpz_cmp(v1, v2) == 0;
4232 mpz_clear(v1);
4233 mpz_clear(v2);
4234 return ret;
4235 }
4236
4237 // Otherwise the arrays are not identical.
4238 return false;
4239 }
4240
4241 // Traversal.
4242
4243 int
4244 Array_type::do_traverse(Traverse* traverse)
4245 {
4246 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
4247 return TRAVERSE_EXIT;
4248 if (this->length_ != NULL
4249 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
4250 return TRAVERSE_EXIT;
4251 return TRAVERSE_CONTINUE;
4252 }
4253
4254 // Check that the length is valid.
4255
4256 bool
4257 Array_type::verify_length()
4258 {
4259 if (this->length_ == NULL)
4260 return true;
4261 if (!this->length_->is_constant())
4262 {
4263 error_at(this->length_->location(), "array bound is not constant");
4264 return false;
4265 }
4266
4267 mpz_t val;
4268
4269 Type* t = this->length_->type();
4270 if (t->integer_type() != NULL)
4271 {
4272 Type* vt;
4273 mpz_init(val);
4274 if (!this->length_->integer_constant_value(true, val, &vt))
4275 {
4276 error_at(this->length_->location(),
4277 "array bound is not constant");
4278 mpz_clear(val);
4279 return false;
4280 }
4281 }
4282 else if (t->float_type() != NULL)
4283 {
4284 Type* vt;
4285 mpfr_t fval;
4286 mpfr_init(fval);
4287 if (!this->length_->float_constant_value(fval, &vt))
4288 {
4289 error_at(this->length_->location(),
4290 "array bound is not constant");
4291 mpfr_clear(fval);
4292 return false;
4293 }
4294 if (!mpfr_integer_p(fval))
4295 {
4296 error_at(this->length_->location(),
4297 "array bound truncated to integer");
4298 mpfr_clear(fval);
4299 return false;
4300 }
4301 mpz_init(val);
4302 mpfr_get_z(val, fval, GMP_RNDN);
4303 mpfr_clear(fval);
4304 }
4305 else
4306 {
4307 if (!t->is_error_type())
4308 error_at(this->length_->location(), "array bound is not numeric");
4309 return false;
4310 }
4311
4312 if (mpz_sgn(val) < 0)
4313 {
4314 error_at(this->length_->location(), "negative array bound");
4315 mpz_clear(val);
4316 return false;
4317 }
4318
4319 Type* int_type = Type::lookup_integer_type("int");
4320 int tbits = int_type->integer_type()->bits();
4321 int vbits = mpz_sizeinbase(val, 2);
4322 if (vbits + 1 > tbits)
4323 {
4324 error_at(this->length_->location(), "array bound overflows");
4325 mpz_clear(val);
4326 return false;
4327 }
4328
4329 mpz_clear(val);
4330
4331 return true;
4332 }
4333
4334 // Verify the type.
4335
4336 bool
4337 Array_type::do_verify()
4338 {
4339 if (!this->verify_length())
4340 {
4341 this->length_ = Expression::make_error(this->length_->location());
4342 return false;
4343 }
4344 return true;
4345 }
4346
4347 // Array type hash code.
4348
4349 unsigned int
4350 Array_type::do_hash_for_method(Gogo* gogo) const
4351 {
4352 // There is no very convenient way to get a hash code for the
4353 // length.
4354 return this->element_type_->hash_for_method(gogo) + 1;
4355 }
4356
4357 // See if the expression passed to make is suitable. The first
4358 // argument is required, and gives the length. An optional second
4359 // argument is permitted for the capacity.
4360
4361 bool
4362 Array_type::do_check_make_expression(Expression_list* args,
4363 source_location location)
4364 {
4365 gcc_assert(this->length_ == NULL);
4366 if (args == NULL || args->empty())
4367 {
4368 error_at(location, "length required when allocating a slice");
4369 return false;
4370 }
4371 else if (args->size() > 2)
4372 {
4373 error_at(location, "too many expressions passed to make");
4374 return false;
4375 }
4376 else
4377 {
4378 if (!Type::check_int_value(args->front(),
4379 _("bad length when making slice"), location))
4380 return false;
4381
4382 if (args->size() > 1)
4383 {
4384 if (!Type::check_int_value(args->back(),
4385 _("bad capacity when making slice"),
4386 location))
4387 return false;
4388 }
4389
4390 return true;
4391 }
4392 }
4393
4394 // Get a tree for the length of a fixed array. The length may be
4395 // computed using a function call, so we must only evaluate it once.
4396
4397 tree
4398 Array_type::get_length_tree(Gogo* gogo)
4399 {
4400 gcc_assert(this->length_ != NULL);
4401 if (this->length_tree_ == NULL_TREE)
4402 {
4403 mpz_t val;
4404 mpz_init(val);
4405 Type* t;
4406 if (this->length_->integer_constant_value(true, val, &t))
4407 {
4408 if (t == NULL)
4409 t = Type::lookup_integer_type("int");
4410 else if (t->is_abstract())
4411 t = t->make_non_abstract_type();
4412 tree tt = t->get_tree(gogo);
4413 this->length_tree_ = Expression::integer_constant_tree(val, tt);
4414 mpz_clear(val);
4415 }
4416 else
4417 {
4418 mpz_clear(val);
4419
4420 // Make up a translation context for the array length
4421 // expression. FIXME: This won't work in general.
4422 Translate_context context(gogo, NULL, NULL, NULL_TREE);
4423 tree len = this->length_->get_tree(&context);
4424 if (len != error_mark_node)
4425 {
4426 len = convert_to_integer(integer_type_node, len);
4427 len = save_expr(len);
4428 }
4429 this->length_tree_ = len;
4430 }
4431 }
4432 return this->length_tree_;
4433 }
4434
4435 // Get a tree for the type of this array. A fixed array is simply
4436 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
4437 // just like an array in C. An open array is a struct with three
4438 // fields: a data pointer, the length, and the capacity.
4439
4440 tree
4441 Array_type::do_get_tree(Gogo* gogo)
4442 {
4443 if (this->length_ == NULL)
4444 {
4445 tree struct_type = gogo->slice_type_tree(void_type_node);
4446 return this->fill_in_tree(gogo, struct_type);
4447 }
4448 else
4449 {
4450 tree element_type_tree = this->element_type_->get_tree(gogo);
4451 tree length_tree = this->get_length_tree(gogo);
4452 if (element_type_tree == error_mark_node
4453 || length_tree == error_mark_node)
4454 return error_mark_node;
4455
4456 length_tree = fold_convert(sizetype, length_tree);
4457
4458 // build_index_type takes the maximum index, which is one less
4459 // than the length.
4460 tree index_type = build_index_type(fold_build2(MINUS_EXPR, sizetype,
4461 length_tree,
4462 size_one_node));
4463
4464 return build_array_type(element_type_tree, index_type);
4465 }
4466 }
4467
4468 // Fill in the fields for a slice type. This is used for named slice
4469 // types.
4470
4471 tree
4472 Array_type::fill_in_tree(Gogo* gogo, tree struct_type)
4473 {
4474 gcc_assert(this->length_ == NULL);
4475
4476 tree element_type_tree = this->element_type_->get_tree(gogo);
4477 tree field = TYPE_FIELDS(struct_type);
4478 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
4479 gcc_assert(POINTER_TYPE_P(TREE_TYPE(field))
4480 && TREE_TYPE(TREE_TYPE(field)) == void_type_node);
4481 TREE_TYPE(field) = build_pointer_type(element_type_tree);
4482
4483 return struct_type;
4484 }
4485
4486 // Return an initializer for an array type.
4487
4488 tree
4489 Array_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
4490 {
4491 if (this->length_ == NULL)
4492 {
4493 // Open array.
4494
4495 if (is_clear)
4496 return NULL;
4497
4498 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
4499
4500 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
4501
4502 for (tree field = TYPE_FIELDS(type_tree);
4503 field != NULL_TREE;
4504 field = DECL_CHAIN(field))
4505 {
4506 constructor_elt* elt = VEC_quick_push(constructor_elt, init,
4507 NULL);
4508 elt->index = field;
4509 elt->value = fold_convert(TREE_TYPE(field), size_zero_node);
4510 }
4511
4512 tree ret = build_constructor(type_tree, init);
4513 TREE_CONSTANT(ret) = 1;
4514 return ret;
4515 }
4516 else
4517 {
4518 // Fixed array.
4519
4520 tree value = this->element_type_->get_init_tree(gogo, is_clear);
4521 if (value == NULL)
4522 return NULL;
4523 if (value == error_mark_node)
4524 return error_mark_node;
4525
4526 tree length_tree = this->get_length_tree(gogo);
4527 if (length_tree == error_mark_node)
4528 return error_mark_node;
4529
4530 length_tree = fold_convert(sizetype, length_tree);
4531 tree range = build2(RANGE_EXPR, sizetype, size_zero_node,
4532 fold_build2(MINUS_EXPR, sizetype,
4533 length_tree, size_one_node));
4534 tree ret = build_constructor_single(type_tree, range, value);
4535 if (TREE_CONSTANT(value))
4536 TREE_CONSTANT(ret) = 1;
4537 return ret;
4538 }
4539 }
4540
4541 // Handle the builtin make function for a slice.
4542
4543 tree
4544 Array_type::do_make_expression_tree(Translate_context* context,
4545 Expression_list* args,
4546 source_location location)
4547 {
4548 gcc_assert(this->length_ == NULL);
4549
4550 Gogo* gogo = context->gogo();
4551 tree type_tree = this->get_tree(gogo);
4552 if (type_tree == error_mark_node)
4553 return error_mark_node;
4554
4555 tree values_field = TYPE_FIELDS(type_tree);
4556 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field)),
4557 "__values") == 0);
4558
4559 tree count_field = DECL_CHAIN(values_field);
4560 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field)),
4561 "__count") == 0);
4562
4563 tree element_type_tree = this->element_type_->get_tree(gogo);
4564 if (element_type_tree == error_mark_node)
4565 return error_mark_node;
4566 tree element_size_tree = TYPE_SIZE_UNIT(element_type_tree);
4567
4568 tree value = this->element_type_->get_init_tree(gogo, true);
4569
4570 // The first argument is the number of elements, the optional second
4571 // argument is the capacity.
4572 gcc_assert(args != NULL && args->size() >= 1 && args->size() <= 2);
4573
4574 tree length_tree = args->front()->get_tree(context);
4575 if (length_tree == error_mark_node)
4576 return error_mark_node;
4577 if (!DECL_P(length_tree))
4578 length_tree = save_expr(length_tree);
4579 if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree)))
4580 length_tree = convert_to_integer(TREE_TYPE(count_field), length_tree);
4581
4582 tree bad_index = Expression::check_bounds(length_tree,
4583 TREE_TYPE(count_field),
4584 NULL_TREE, location);
4585
4586 length_tree = fold_convert_loc(location, TREE_TYPE(count_field), length_tree);
4587 tree capacity_tree;
4588 if (args->size() == 1)
4589 capacity_tree = length_tree;
4590 else
4591 {
4592 capacity_tree = args->back()->get_tree(context);
4593 if (capacity_tree == error_mark_node)
4594 return error_mark_node;
4595 if (!DECL_P(capacity_tree))
4596 capacity_tree = save_expr(capacity_tree);
4597 if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree)))
4598 capacity_tree = convert_to_integer(TREE_TYPE(count_field),
4599 capacity_tree);
4600
4601 bad_index = Expression::check_bounds(capacity_tree,
4602 TREE_TYPE(count_field),
4603 bad_index, location);
4604
4605 tree chktype = (((TYPE_SIZE(TREE_TYPE(capacity_tree))
4606 > TYPE_SIZE(TREE_TYPE(length_tree)))
4607 || ((TYPE_SIZE(TREE_TYPE(capacity_tree))
4608 == TYPE_SIZE(TREE_TYPE(length_tree)))
4609 && TYPE_UNSIGNED(TREE_TYPE(capacity_tree))))
4610 ? TREE_TYPE(capacity_tree)
4611 : TREE_TYPE(length_tree));
4612 tree chk = fold_build2_loc(location, LT_EXPR, boolean_type_node,
4613 fold_convert_loc(location, chktype,
4614 capacity_tree),
4615 fold_convert_loc(location, chktype,
4616 length_tree));
4617 if (bad_index == NULL_TREE)
4618 bad_index = chk;
4619 else
4620 bad_index = fold_build2_loc(location, TRUTH_OR_EXPR, boolean_type_node,
4621 bad_index, chk);
4622
4623 capacity_tree = fold_convert_loc(location, TREE_TYPE(count_field),
4624 capacity_tree);
4625 }
4626
4627 tree size_tree = fold_build2_loc(location, MULT_EXPR, sizetype,
4628 element_size_tree,
4629 fold_convert_loc(location, sizetype,
4630 capacity_tree));
4631
4632 tree chk = fold_build2_loc(location, TRUTH_AND_EXPR, boolean_type_node,
4633 fold_build2_loc(location, GT_EXPR,
4634 boolean_type_node,
4635 fold_convert_loc(location,
4636 sizetype,
4637 capacity_tree),
4638 size_zero_node),
4639 fold_build2_loc(location, LT_EXPR,
4640 boolean_type_node,
4641 size_tree, element_size_tree));
4642 if (bad_index == NULL_TREE)
4643 bad_index = chk;
4644 else
4645 bad_index = fold_build2_loc(location, TRUTH_OR_EXPR, boolean_type_node,
4646 bad_index, chk);
4647
4648 tree space = context->gogo()->allocate_memory(this->element_type_,
4649 size_tree, location);
4650
4651 if (value != NULL_TREE)
4652 space = save_expr(space);
4653
4654 space = fold_convert(TREE_TYPE(values_field), space);
4655
4656 if (bad_index != NULL_TREE && bad_index != boolean_false_node)
4657 {
4658 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS,
4659 location);
4660 space = build2(COMPOUND_EXPR, TREE_TYPE(space),
4661 build3(COND_EXPR, void_type_node,
4662 bad_index, crash, NULL_TREE),
4663 space);
4664 }
4665
4666 tree constructor = gogo->slice_constructor(type_tree, space, length_tree,
4667 capacity_tree);
4668
4669 if (value == NULL_TREE)
4670 {
4671 // The array contents are zero initialized.
4672 return constructor;
4673 }
4674
4675 // The elements must be initialized.
4676
4677 tree max = fold_build2_loc(location, MINUS_EXPR, TREE_TYPE(count_field),
4678 capacity_tree,
4679 fold_convert_loc(location, TREE_TYPE(count_field),
4680 integer_one_node));
4681
4682 tree array_type = build_array_type(element_type_tree,
4683 build_index_type(max));
4684
4685 tree value_pointer = fold_convert_loc(location,
4686 build_pointer_type(array_type),
4687 space);
4688
4689 tree range = build2(RANGE_EXPR, sizetype, size_zero_node, max);
4690 tree space_init = build_constructor_single(array_type, range, value);
4691
4692 return build2(COMPOUND_EXPR, TREE_TYPE(space),
4693 build2(MODIFY_EXPR, void_type_node,
4694 build_fold_indirect_ref(value_pointer),
4695 space_init),
4696 constructor);
4697 }
4698
4699 // Return a tree for a pointer to the values in ARRAY.
4700
4701 tree
4702 Array_type::value_pointer_tree(Gogo*, tree array) const
4703 {
4704 tree ret;
4705 if (this->length() != NULL)
4706 {
4707 // Fixed array.
4708 ret = fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array))),
4709 build_fold_addr_expr(array));
4710 }
4711 else
4712 {
4713 // Open array.
4714 tree field = TYPE_FIELDS(TREE_TYPE(array));
4715 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
4716 "__values") == 0);
4717 ret = fold_build3(COMPONENT_REF, TREE_TYPE(field), array, field,
4718 NULL_TREE);
4719 }
4720 if (TREE_CONSTANT(array))
4721 TREE_CONSTANT(ret) = 1;
4722 return ret;
4723 }
4724
4725 // Return a tree for the length of the array ARRAY which has this
4726 // type.
4727
4728 tree
4729 Array_type::length_tree(Gogo* gogo, tree array)
4730 {
4731 if (this->length_ != NULL)
4732 {
4733 if (TREE_CODE(array) == SAVE_EXPR)
4734 return fold_convert(integer_type_node, this->get_length_tree(gogo));
4735 else
4736 return omit_one_operand(integer_type_node,
4737 this->get_length_tree(gogo), array);
4738 }
4739
4740 // This is an open array. We need to read the length field.
4741
4742 tree type = TREE_TYPE(array);
4743 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
4744
4745 tree field = DECL_CHAIN(TYPE_FIELDS(type));
4746 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
4747
4748 tree ret = build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
4749 if (TREE_CONSTANT(array))
4750 TREE_CONSTANT(ret) = 1;
4751 return ret;
4752 }
4753
4754 // Return a tree for the capacity of the array ARRAY which has this
4755 // type.
4756
4757 tree
4758 Array_type::capacity_tree(Gogo* gogo, tree array)
4759 {
4760 if (this->length_ != NULL)
4761 return omit_one_operand(sizetype, this->get_length_tree(gogo), array);
4762
4763 // This is an open array. We need to read the capacity field.
4764
4765 tree type = TREE_TYPE(array);
4766 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
4767
4768 tree field = DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type)));
4769 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
4770
4771 return build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
4772 }
4773
4774 // Export.
4775
4776 void
4777 Array_type::do_export(Export* exp) const
4778 {
4779 exp->write_c_string("[");
4780 if (this->length_ != NULL)
4781 this->length_->export_expression(exp);
4782 exp->write_c_string("] ");
4783 exp->write_type(this->element_type_);
4784 }
4785
4786 // Import.
4787
4788 Array_type*
4789 Array_type::do_import(Import* imp)
4790 {
4791 imp->require_c_string("[");
4792 Expression* length;
4793 if (imp->peek_char() == ']')
4794 length = NULL;
4795 else
4796 length = Expression::import_expression(imp);
4797 imp->require_c_string("] ");
4798 Type* element_type = imp->read_type();
4799 return Type::make_array_type(element_type, length);
4800 }
4801
4802 // The type of an array type descriptor.
4803
4804 Type*
4805 Array_type::make_array_type_descriptor_type()
4806 {
4807 static Type* ret;
4808 if (ret == NULL)
4809 {
4810 Type* tdt = Type::make_type_descriptor_type();
4811 Type* ptdt = Type::make_type_descriptor_ptr_type();
4812
4813 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4814
4815 Struct_type* sf =
4816 Type::make_builtin_struct_type(3,
4817 "", tdt,
4818 "elem", ptdt,
4819 "len", uintptr_type);
4820
4821 ret = Type::make_builtin_named_type("ArrayType", sf);
4822 }
4823
4824 return ret;
4825 }
4826
4827 // The type of an slice type descriptor.
4828
4829 Type*
4830 Array_type::make_slice_type_descriptor_type()
4831 {
4832 static Type* ret;
4833 if (ret == NULL)
4834 {
4835 Type* tdt = Type::make_type_descriptor_type();
4836 Type* ptdt = Type::make_type_descriptor_ptr_type();
4837
4838 Struct_type* sf =
4839 Type::make_builtin_struct_type(2,
4840 "", tdt,
4841 "elem", ptdt);
4842
4843 ret = Type::make_builtin_named_type("SliceType", sf);
4844 }
4845
4846 return ret;
4847 }
4848
4849 // Build a type descriptor for an array/slice type.
4850
4851 Expression*
4852 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4853 {
4854 if (this->length_ != NULL)
4855 return this->array_type_descriptor(gogo, name);
4856 else
4857 return this->slice_type_descriptor(gogo, name);
4858 }
4859
4860 // Build a type descriptor for an array type.
4861
4862 Expression*
4863 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
4864 {
4865 source_location bloc = BUILTINS_LOCATION;
4866
4867 Type* atdt = Array_type::make_array_type_descriptor_type();
4868
4869 const Struct_field_list* fields = atdt->struct_type()->fields();
4870
4871 Expression_list* vals = new Expression_list();
4872 vals->reserve(3);
4873
4874 Struct_field_list::const_iterator p = fields->begin();
4875 gcc_assert(p->field_name() == "commonType");
4876 vals->push_back(this->type_descriptor_constructor(gogo,
4877 RUNTIME_TYPE_KIND_ARRAY,
4878 name, NULL, true));
4879
4880 ++p;
4881 gcc_assert(p->field_name() == "elem");
4882 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
4883
4884 ++p;
4885 gcc_assert(p->field_name() == "len");
4886 vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
4887
4888 ++p;
4889 gcc_assert(p == fields->end());
4890
4891 return Expression::make_struct_composite_literal(atdt, vals, bloc);
4892 }
4893
4894 // Build a type descriptor for a slice type.
4895
4896 Expression*
4897 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
4898 {
4899 source_location bloc = BUILTINS_LOCATION;
4900
4901 Type* stdt = Array_type::make_slice_type_descriptor_type();
4902
4903 const Struct_field_list* fields = stdt->struct_type()->fields();
4904
4905 Expression_list* vals = new Expression_list();
4906 vals->reserve(2);
4907
4908 Struct_field_list::const_iterator p = fields->begin();
4909 gcc_assert(p->field_name() == "commonType");
4910 vals->push_back(this->type_descriptor_constructor(gogo,
4911 RUNTIME_TYPE_KIND_SLICE,
4912 name, NULL, true));
4913
4914 ++p;
4915 gcc_assert(p->field_name() == "elem");
4916 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
4917
4918 ++p;
4919 gcc_assert(p == fields->end());
4920
4921 return Expression::make_struct_composite_literal(stdt, vals, bloc);
4922 }
4923
4924 // Reflection string.
4925
4926 void
4927 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
4928 {
4929 ret->push_back('[');
4930 if (this->length_ != NULL)
4931 {
4932 mpz_t val;
4933 mpz_init(val);
4934 Type* type;
4935 if (!this->length_->integer_constant_value(true, val, &type))
4936 error_at(this->length_->location(),
4937 "array length must be integer constant expression");
4938 else if (mpz_cmp_si(val, 0) < 0)
4939 error_at(this->length_->location(), "array length is negative");
4940 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
4941 error_at(this->length_->location(), "array length is too large");
4942 else
4943 {
4944 char buf[50];
4945 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
4946 ret->append(buf);
4947 }
4948 mpz_clear(val);
4949 }
4950 ret->push_back(']');
4951
4952 this->append_reflection(this->element_type_, gogo, ret);
4953 }
4954
4955 // Mangled name.
4956
4957 void
4958 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4959 {
4960 ret->push_back('A');
4961 this->append_mangled_name(this->element_type_, gogo, ret);
4962 if (this->length_ != NULL)
4963 {
4964 mpz_t val;
4965 mpz_init(val);
4966 Type* type;
4967 if (!this->length_->integer_constant_value(true, val, &type))
4968 error_at(this->length_->location(),
4969 "array length must be integer constant expression");
4970 else if (mpz_cmp_si(val, 0) < 0)
4971 error_at(this->length_->location(), "array length is negative");
4972 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
4973 error_at(this->length_->location(), "array size is too large");
4974 else
4975 {
4976 char buf[50];
4977 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
4978 ret->append(buf);
4979 }
4980 mpz_clear(val);
4981 }
4982 ret->push_back('e');
4983 }
4984
4985 // Make an array type.
4986
4987 Array_type*
4988 Type::make_array_type(Type* element_type, Expression* length)
4989 {
4990 return new Array_type(element_type, length);
4991 }
4992
4993 // Class Map_type.
4994
4995 // Traversal.
4996
4997 int
4998 Map_type::do_traverse(Traverse* traverse)
4999 {
5000 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
5001 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
5002 return TRAVERSE_EXIT;
5003 return TRAVERSE_CONTINUE;
5004 }
5005
5006 // Check that the map type is OK.
5007
5008 bool
5009 Map_type::do_verify()
5010 {
5011 if (this->key_type_->struct_type() != NULL
5012 || this->key_type_->array_type() != NULL)
5013 {
5014 error_at(this->location_, "invalid map key type");
5015 return false;
5016 }
5017 return true;
5018 }
5019
5020 // Whether two map types are identical.
5021
5022 bool
5023 Map_type::is_identical(const Map_type* t, bool errors_are_identical) const
5024 {
5025 return (Type::are_identical(this->key_type(), t->key_type(),
5026 errors_are_identical, NULL)
5027 && Type::are_identical(this->val_type(), t->val_type(),
5028 errors_are_identical, NULL));
5029 }
5030
5031 // Hash code.
5032
5033 unsigned int
5034 Map_type::do_hash_for_method(Gogo* gogo) const
5035 {
5036 return (this->key_type_->hash_for_method(gogo)
5037 + this->val_type_->hash_for_method(gogo)
5038 + 2);
5039 }
5040
5041 // Check that a call to the builtin make function is valid. For a map
5042 // the optional argument is the number of spaces to preallocate for
5043 // values.
5044
5045 bool
5046 Map_type::do_check_make_expression(Expression_list* args,
5047 source_location location)
5048 {
5049 if (args != NULL && !args->empty())
5050 {
5051 if (!Type::check_int_value(args->front(), _("bad size when making map"),
5052 location))
5053 return false;
5054 else if (args->size() > 1)
5055 {
5056 error_at(location, "too many arguments when making map");
5057 return false;
5058 }
5059 }
5060 return true;
5061 }
5062
5063 // Get a tree for a map type. A map type is represented as a pointer
5064 // to a struct. The struct is __go_map in libgo/map.h.
5065
5066 tree
5067 Map_type::do_get_tree(Gogo* gogo)
5068 {
5069 static tree type_tree;
5070 if (type_tree == NULL_TREE)
5071 {
5072 tree struct_type = make_node(RECORD_TYPE);
5073
5074 tree map_descriptor_type = gogo->map_descriptor_type();
5075 tree const_map_descriptor_type =
5076 build_qualified_type(map_descriptor_type, TYPE_QUAL_CONST);
5077 tree name = get_identifier("__descriptor");
5078 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
5079 build_pointer_type(const_map_descriptor_type));
5080 DECL_CONTEXT(field) = struct_type;
5081 TYPE_FIELDS(struct_type) = field;
5082 tree last_field = field;
5083
5084 name = get_identifier("__element_count");
5085 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, sizetype);
5086 DECL_CONTEXT(field) = struct_type;
5087 DECL_CHAIN(last_field) = field;
5088 last_field = field;
5089
5090 name = get_identifier("__bucket_count");
5091 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, sizetype);
5092 DECL_CONTEXT(field) = struct_type;
5093 DECL_CHAIN(last_field) = field;
5094 last_field = field;
5095
5096 name = get_identifier("__buckets");
5097 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
5098 build_pointer_type(ptr_type_node));
5099 DECL_CONTEXT(field) = struct_type;
5100 DECL_CHAIN(last_field) = field;
5101
5102 layout_type(struct_type);
5103
5104 // Give the struct a name for better debugging info.
5105 name = get_identifier("__go_map");
5106 tree type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
5107 struct_type);
5108 DECL_ARTIFICIAL(type_decl) = 1;
5109 TYPE_NAME(struct_type) = type_decl;
5110 go_preserve_from_gc(type_decl);
5111 rest_of_decl_compilation(type_decl, 1, 0);
5112
5113 type_tree = build_pointer_type(struct_type);
5114 go_preserve_from_gc(type_tree);
5115 }
5116
5117 return type_tree;
5118 }
5119
5120 // Initialize a map.
5121
5122 tree
5123 Map_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
5124 {
5125 if (is_clear)
5126 return NULL;
5127 return fold_convert(type_tree, null_pointer_node);
5128 }
5129
5130 // Return an expression for a newly allocated map.
5131
5132 tree
5133 Map_type::do_make_expression_tree(Translate_context* context,
5134 Expression_list* args,
5135 source_location location)
5136 {
5137 tree bad_index = NULL_TREE;
5138
5139 tree expr_tree;
5140 if (args == NULL || args->empty())
5141 expr_tree = size_zero_node;
5142 else
5143 {
5144 expr_tree = args->front()->get_tree(context);
5145 if (expr_tree == error_mark_node)
5146 return error_mark_node;
5147 if (!DECL_P(expr_tree))
5148 expr_tree = save_expr(expr_tree);
5149 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
5150 expr_tree = convert_to_integer(sizetype, expr_tree);
5151 bad_index = Expression::check_bounds(expr_tree, sizetype, bad_index,
5152 location);
5153 }
5154
5155 tree map_type = this->get_tree(context->gogo());
5156
5157 static tree new_map_fndecl;
5158 tree ret = Gogo::call_builtin(&new_map_fndecl,
5159 location,
5160 "__go_new_map",
5161 2,
5162 map_type,
5163 TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type))),
5164 context->gogo()->map_descriptor(this),
5165 sizetype,
5166 expr_tree);
5167 if (ret == error_mark_node)
5168 return error_mark_node;
5169 // This can panic if the capacity is out of range.
5170 TREE_NOTHROW(new_map_fndecl) = 0;
5171
5172 if (bad_index == NULL_TREE)
5173 return ret;
5174 else
5175 {
5176 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS,
5177 location);
5178 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
5179 build3(COND_EXPR, void_type_node,
5180 bad_index, crash, NULL_TREE),
5181 ret);
5182 }
5183 }
5184
5185 // The type of a map type descriptor.
5186
5187 Type*
5188 Map_type::make_map_type_descriptor_type()
5189 {
5190 static Type* ret;
5191 if (ret == NULL)
5192 {
5193 Type* tdt = Type::make_type_descriptor_type();
5194 Type* ptdt = Type::make_type_descriptor_ptr_type();
5195
5196 Struct_type* sf =
5197 Type::make_builtin_struct_type(3,
5198 "", tdt,
5199 "key", ptdt,
5200 "elem", ptdt);
5201
5202 ret = Type::make_builtin_named_type("MapType", sf);
5203 }
5204
5205 return ret;
5206 }
5207
5208 // Build a type descriptor for a map type.
5209
5210 Expression*
5211 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5212 {
5213 source_location bloc = BUILTINS_LOCATION;
5214
5215 Type* mtdt = Map_type::make_map_type_descriptor_type();
5216
5217 const Struct_field_list* fields = mtdt->struct_type()->fields();
5218
5219 Expression_list* vals = new Expression_list();
5220 vals->reserve(3);
5221
5222 Struct_field_list::const_iterator p = fields->begin();
5223 gcc_assert(p->field_name() == "commonType");
5224 vals->push_back(this->type_descriptor_constructor(gogo,
5225 RUNTIME_TYPE_KIND_MAP,
5226 name, NULL, true));
5227
5228 ++p;
5229 gcc_assert(p->field_name() == "key");
5230 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
5231
5232 ++p;
5233 gcc_assert(p->field_name() == "elem");
5234 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
5235
5236 ++p;
5237 gcc_assert(p == fields->end());
5238
5239 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
5240 }
5241
5242 // Reflection string for a map.
5243
5244 void
5245 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
5246 {
5247 ret->append("map[");
5248 this->append_reflection(this->key_type_, gogo, ret);
5249 ret->append("] ");
5250 this->append_reflection(this->val_type_, gogo, ret);
5251 }
5252
5253 // Mangled name for a map.
5254
5255 void
5256 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5257 {
5258 ret->push_back('M');
5259 this->append_mangled_name(this->key_type_, gogo, ret);
5260 ret->append("__");
5261 this->append_mangled_name(this->val_type_, gogo, ret);
5262 }
5263
5264 // Export a map type.
5265
5266 void
5267 Map_type::do_export(Export* exp) const
5268 {
5269 exp->write_c_string("map [");
5270 exp->write_type(this->key_type_);
5271 exp->write_c_string("] ");
5272 exp->write_type(this->val_type_);
5273 }
5274
5275 // Import a map type.
5276
5277 Map_type*
5278 Map_type::do_import(Import* imp)
5279 {
5280 imp->require_c_string("map [");
5281 Type* key_type = imp->read_type();
5282 imp->require_c_string("] ");
5283 Type* val_type = imp->read_type();
5284 return Type::make_map_type(key_type, val_type, imp->location());
5285 }
5286
5287 // Make a map type.
5288
5289 Map_type*
5290 Type::make_map_type(Type* key_type, Type* val_type, source_location location)
5291 {
5292 return new Map_type(key_type, val_type, location);
5293 }
5294
5295 // Class Channel_type.
5296
5297 // Hash code.
5298
5299 unsigned int
5300 Channel_type::do_hash_for_method(Gogo* gogo) const
5301 {
5302 unsigned int ret = 0;
5303 if (this->may_send_)
5304 ret += 1;
5305 if (this->may_receive_)
5306 ret += 2;
5307 if (this->element_type_ != NULL)
5308 ret += this->element_type_->hash_for_method(gogo) << 2;
5309 return ret << 3;
5310 }
5311
5312 // Whether this type is the same as T.
5313
5314 bool
5315 Channel_type::is_identical(const Channel_type* t,
5316 bool errors_are_identical) const
5317 {
5318 if (!Type::are_identical(this->element_type(), t->element_type(),
5319 errors_are_identical, NULL))
5320 return false;
5321 return (this->may_send_ == t->may_send_
5322 && this->may_receive_ == t->may_receive_);
5323 }
5324
5325 // Check whether the parameters for a call to the builtin function
5326 // make are OK for a channel. A channel can take an optional single
5327 // parameter which is the buffer size.
5328
5329 bool
5330 Channel_type::do_check_make_expression(Expression_list* args,
5331 source_location location)
5332 {
5333 if (args != NULL && !args->empty())
5334 {
5335 if (!Type::check_int_value(args->front(),
5336 _("bad buffer size when making channel"),
5337 location))
5338 return false;
5339 else if (args->size() > 1)
5340 {
5341 error_at(location, "too many arguments when making channel");
5342 return false;
5343 }
5344 }
5345 return true;
5346 }
5347
5348 // Return the tree for a channel type. A channel is a pointer to a
5349 // __go_channel struct. The __go_channel struct is defined in
5350 // libgo/runtime/channel.h.
5351
5352 tree
5353 Channel_type::do_get_tree(Gogo*)
5354 {
5355 static tree type_tree;
5356 if (type_tree == NULL_TREE)
5357 {
5358 tree ret = make_node(RECORD_TYPE);
5359 TYPE_NAME(ret) = get_identifier("__go_channel");
5360 TYPE_STUB_DECL(ret) = build_decl(BUILTINS_LOCATION, TYPE_DECL, NULL_TREE,
5361 ret);
5362 type_tree = build_pointer_type(ret);
5363 go_preserve_from_gc(type_tree);
5364 }
5365 return type_tree;
5366 }
5367
5368 // Initialize a channel variable.
5369
5370 tree
5371 Channel_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
5372 {
5373 if (is_clear)
5374 return NULL;
5375 return fold_convert(type_tree, null_pointer_node);
5376 }
5377
5378 // Handle the builtin function make for a channel.
5379
5380 tree
5381 Channel_type::do_make_expression_tree(Translate_context* context,
5382 Expression_list* args,
5383 source_location location)
5384 {
5385 Gogo* gogo = context->gogo();
5386 tree channel_type = this->get_tree(gogo);
5387
5388 tree element_tree = this->element_type_->get_tree(gogo);
5389 tree element_size_tree = size_in_bytes(element_tree);
5390
5391 tree bad_index = NULL_TREE;
5392
5393 tree expr_tree;
5394 if (args == NULL || args->empty())
5395 expr_tree = size_zero_node;
5396 else
5397 {
5398 expr_tree = args->front()->get_tree(context);
5399 if (expr_tree == error_mark_node)
5400 return error_mark_node;
5401 if (!DECL_P(expr_tree))
5402 expr_tree = save_expr(expr_tree);
5403 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
5404 expr_tree = convert_to_integer(sizetype, expr_tree);
5405 bad_index = Expression::check_bounds(expr_tree, sizetype, bad_index,
5406 location);
5407 }
5408
5409 static tree new_channel_fndecl;
5410 tree ret = Gogo::call_builtin(&new_channel_fndecl,
5411 location,
5412 "__go_new_channel",
5413 2,
5414 channel_type,
5415 sizetype,
5416 element_size_tree,
5417 sizetype,
5418 expr_tree);
5419 if (ret == error_mark_node)
5420 return error_mark_node;
5421 // This can panic if the capacity is out of range.
5422 TREE_NOTHROW(new_channel_fndecl) = 0;
5423
5424 if (bad_index == NULL_TREE)
5425 return ret;
5426 else
5427 {
5428 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS,
5429 location);
5430 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
5431 build3(COND_EXPR, void_type_node,
5432 bad_index, crash, NULL_TREE),
5433 ret);
5434 }
5435 }
5436
5437 // Build a type descriptor for a channel type.
5438
5439 Type*
5440 Channel_type::make_chan_type_descriptor_type()
5441 {
5442 static Type* ret;
5443 if (ret == NULL)
5444 {
5445 Type* tdt = Type::make_type_descriptor_type();
5446 Type* ptdt = Type::make_type_descriptor_ptr_type();
5447
5448 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5449
5450 Struct_type* sf =
5451 Type::make_builtin_struct_type(3,
5452 "", tdt,
5453 "elem", ptdt,
5454 "dir", uintptr_type);
5455
5456 ret = Type::make_builtin_named_type("ChanType", sf);
5457 }
5458
5459 return ret;
5460 }
5461
5462 // Build a type descriptor for a map type.
5463
5464 Expression*
5465 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5466 {
5467 source_location bloc = BUILTINS_LOCATION;
5468
5469 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
5470
5471 const Struct_field_list* fields = ctdt->struct_type()->fields();
5472
5473 Expression_list* vals = new Expression_list();
5474 vals->reserve(3);
5475
5476 Struct_field_list::const_iterator p = fields->begin();
5477 gcc_assert(p->field_name() == "commonType");
5478 vals->push_back(this->type_descriptor_constructor(gogo,
5479 RUNTIME_TYPE_KIND_CHAN,
5480 name, NULL, true));
5481
5482 ++p;
5483 gcc_assert(p->field_name() == "elem");
5484 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5485
5486 ++p;
5487 gcc_assert(p->field_name() == "dir");
5488 // These bits must match the ones in libgo/runtime/go-type.h.
5489 int val = 0;
5490 if (this->may_receive_)
5491 val |= 1;
5492 if (this->may_send_)
5493 val |= 2;
5494 mpz_t iv;
5495 mpz_init_set_ui(iv, val);
5496 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
5497 mpz_clear(iv);
5498
5499 ++p;
5500 gcc_assert(p == fields->end());
5501
5502 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
5503 }
5504
5505 // Reflection string.
5506
5507 void
5508 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
5509 {
5510 if (!this->may_send_)
5511 ret->append("<-");
5512 ret->append("chan");
5513 if (!this->may_receive_)
5514 ret->append("<-");
5515 ret->push_back(' ');
5516 this->append_reflection(this->element_type_, gogo, ret);
5517 }
5518
5519 // Mangled name.
5520
5521 void
5522 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5523 {
5524 ret->push_back('C');
5525 this->append_mangled_name(this->element_type_, gogo, ret);
5526 if (this->may_send_)
5527 ret->push_back('s');
5528 if (this->may_receive_)
5529 ret->push_back('r');
5530 ret->push_back('e');
5531 }
5532
5533 // Export.
5534
5535 void
5536 Channel_type::do_export(Export* exp) const
5537 {
5538 exp->write_c_string("chan ");
5539 if (this->may_send_ && !this->may_receive_)
5540 exp->write_c_string("-< ");
5541 else if (this->may_receive_ && !this->may_send_)
5542 exp->write_c_string("<- ");
5543 exp->write_type(this->element_type_);
5544 }
5545
5546 // Import.
5547
5548 Channel_type*
5549 Channel_type::do_import(Import* imp)
5550 {
5551 imp->require_c_string("chan ");
5552
5553 bool may_send;
5554 bool may_receive;
5555 if (imp->match_c_string("-< "))
5556 {
5557 imp->advance(3);
5558 may_send = true;
5559 may_receive = false;
5560 }
5561 else if (imp->match_c_string("<- "))
5562 {
5563 imp->advance(3);
5564 may_receive = true;
5565 may_send = false;
5566 }
5567 else
5568 {
5569 may_send = true;
5570 may_receive = true;
5571 }
5572
5573 Type* element_type = imp->read_type();
5574
5575 return Type::make_channel_type(may_send, may_receive, element_type);
5576 }
5577
5578 // Make a new channel type.
5579
5580 Channel_type*
5581 Type::make_channel_type(bool send, bool receive, Type* element_type)
5582 {
5583 return new Channel_type(send, receive, element_type);
5584 }
5585
5586 // Class Interface_type.
5587
5588 // Traversal.
5589
5590 int
5591 Interface_type::do_traverse(Traverse* traverse)
5592 {
5593 if (this->methods_ == NULL)
5594 return TRAVERSE_CONTINUE;
5595 return this->methods_->traverse(traverse);
5596 }
5597
5598 // Finalize the methods. This handles interface inheritance.
5599
5600 void
5601 Interface_type::finalize_methods()
5602 {
5603 if (this->methods_ == NULL)
5604 return;
5605 bool is_recursive = false;
5606 size_t from = 0;
5607 size_t to = 0;
5608 while (from < this->methods_->size())
5609 {
5610 const Typed_identifier* p = &this->methods_->at(from);
5611 if (!p->name().empty())
5612 {
5613 size_t i;
5614 for (i = 0; i < to; ++i)
5615 {
5616 if (this->methods_->at(i).name() == p->name())
5617 {
5618 error_at(p->location(), "duplicate method %qs",
5619 Gogo::message_name(p->name()).c_str());
5620 break;
5621 }
5622 }
5623 if (i == to)
5624 {
5625 if (from != to)
5626 this->methods_->set(to, *p);
5627 ++to;
5628 }
5629 ++from;
5630 continue;
5631 }
5632 Interface_type* it = p->type()->interface_type();
5633 if (it == NULL)
5634 {
5635 error_at(p->location(), "interface contains embedded non-interface");
5636 ++from;
5637 continue;
5638 }
5639 if (it == this)
5640 {
5641 if (!is_recursive)
5642 {
5643 error_at(p->location(), "invalid recursive interface");
5644 is_recursive = true;
5645 }
5646 ++from;
5647 continue;
5648 }
5649 const Typed_identifier_list* methods = it->methods();
5650 if (methods == NULL)
5651 {
5652 ++from;
5653 continue;
5654 }
5655 for (Typed_identifier_list::const_iterator q = methods->begin();
5656 q != methods->end();
5657 ++q)
5658 {
5659 if (q->name().empty())
5660 {
5661 if (q->type() == p->type())
5662 error_at(p->location(), "interface inheritance loop");
5663 else
5664 {
5665 size_t i;
5666 for (i = from + 1; i < this->methods_->size(); ++i)
5667 {
5668 const Typed_identifier* r = &this->methods_->at(i);
5669 if (r->name().empty() && r->type() == q->type())
5670 {
5671 error_at(p->location(),
5672 "inherited interface listed twice");
5673 break;
5674 }
5675 }
5676 if (i == this->methods_->size())
5677 this->methods_->push_back(Typed_identifier(q->name(),
5678 q->type(),
5679 p->location()));
5680 }
5681 }
5682 else if (this->find_method(q->name()) == NULL)
5683 this->methods_->push_back(Typed_identifier(q->name(), q->type(),
5684 p->location()));
5685 else
5686 {
5687 if (!is_recursive)
5688 error_at(p->location(), "inherited method %qs is ambiguous",
5689 Gogo::message_name(q->name()).c_str());
5690 }
5691 }
5692 ++from;
5693 }
5694 if (to == 0)
5695 {
5696 delete this->methods_;
5697 this->methods_ = NULL;
5698 }
5699 else
5700 {
5701 this->methods_->resize(to);
5702 this->methods_->sort_by_name();
5703 }
5704 }
5705
5706 // Return the method NAME, or NULL.
5707
5708 const Typed_identifier*
5709 Interface_type::find_method(const std::string& name) const
5710 {
5711 if (this->methods_ == NULL)
5712 return NULL;
5713 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5714 p != this->methods_->end();
5715 ++p)
5716 if (p->name() == name)
5717 return &*p;
5718 return NULL;
5719 }
5720
5721 // Return the method index.
5722
5723 size_t
5724 Interface_type::method_index(const std::string& name) const
5725 {
5726 gcc_assert(this->methods_ != NULL);
5727 size_t ret = 0;
5728 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5729 p != this->methods_->end();
5730 ++p, ++ret)
5731 if (p->name() == name)
5732 return ret;
5733 gcc_unreachable();
5734 }
5735
5736 // Return whether NAME is an unexported method, for better error
5737 // reporting.
5738
5739 bool
5740 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
5741 {
5742 if (this->methods_ == NULL)
5743 return false;
5744 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5745 p != this->methods_->end();
5746 ++p)
5747 {
5748 const std::string& method_name(p->name());
5749 if (Gogo::is_hidden_name(method_name)
5750 && name == Gogo::unpack_hidden_name(method_name)
5751 && gogo->pack_hidden_name(name, false) != method_name)
5752 return true;
5753 }
5754 return false;
5755 }
5756
5757 // Whether this type is identical with T.
5758
5759 bool
5760 Interface_type::is_identical(const Interface_type* t,
5761 bool errors_are_identical) const
5762 {
5763 // We require the same methods with the same types. The methods
5764 // have already been sorted.
5765 if (this->methods() == NULL || t->methods() == NULL)
5766 return this->methods() == t->methods();
5767
5768 Typed_identifier_list::const_iterator p1 = this->methods()->begin();
5769 for (Typed_identifier_list::const_iterator p2 = t->methods()->begin();
5770 p2 != t->methods()->end();
5771 ++p1, ++p2)
5772 {
5773 if (p1 == this->methods()->end())
5774 return false;
5775 if (p1->name() != p2->name()
5776 || !Type::are_identical(p1->type(), p2->type(),
5777 errors_are_identical, NULL))
5778 return false;
5779 }
5780 if (p1 != this->methods()->end())
5781 return false;
5782 return true;
5783 }
5784
5785 // Whether we can assign the interface type T to this type. The types
5786 // are known to not be identical. An interface assignment is only
5787 // permitted if T is known to implement all methods in THIS.
5788 // Otherwise a type guard is required.
5789
5790 bool
5791 Interface_type::is_compatible_for_assign(const Interface_type* t,
5792 std::string* reason) const
5793 {
5794 if (this->methods() == NULL)
5795 return true;
5796 for (Typed_identifier_list::const_iterator p = this->methods()->begin();
5797 p != this->methods()->end();
5798 ++p)
5799 {
5800 const Typed_identifier* m = t->find_method(p->name());
5801 if (m == NULL)
5802 {
5803 if (reason != NULL)
5804 {
5805 char buf[200];
5806 snprintf(buf, sizeof buf,
5807 _("need explicit conversion; missing method %s%s%s"),
5808 open_quote, Gogo::message_name(p->name()).c_str(),
5809 close_quote);
5810 reason->assign(buf);
5811 }
5812 return false;
5813 }
5814
5815 std::string subreason;
5816 if (!Type::are_identical(p->type(), m->type(), true, &subreason))
5817 {
5818 if (reason != NULL)
5819 {
5820 std::string n = Gogo::message_name(p->name());
5821 size_t len = 100 + n.length() + subreason.length();
5822 char* buf = new char[len];
5823 if (subreason.empty())
5824 snprintf(buf, len, _("incompatible type for method %s%s%s"),
5825 open_quote, n.c_str(), close_quote);
5826 else
5827 snprintf(buf, len,
5828 _("incompatible type for method %s%s%s (%s)"),
5829 open_quote, n.c_str(), close_quote,
5830 subreason.c_str());
5831 reason->assign(buf);
5832 delete[] buf;
5833 }
5834 return false;
5835 }
5836 }
5837
5838 return true;
5839 }
5840
5841 // Hash code.
5842
5843 unsigned int
5844 Interface_type::do_hash_for_method(Gogo* gogo) const
5845 {
5846 unsigned int ret = 0;
5847 if (this->methods_ != NULL)
5848 {
5849 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5850 p != this->methods_->end();
5851 ++p)
5852 {
5853 ret = Type::hash_string(p->name(), ret);
5854 ret += p->type()->hash_for_method(gogo);
5855 ret <<= 1;
5856 }
5857 }
5858 return ret;
5859 }
5860
5861 // Return true if T implements the interface. If it does not, and
5862 // REASON is not NULL, set *REASON to a useful error message.
5863
5864 bool
5865 Interface_type::implements_interface(const Type* t, std::string* reason) const
5866 {
5867 if (this->methods_ == NULL)
5868 return true;
5869
5870 bool is_pointer = false;
5871 const Named_type* nt = t->named_type();
5872 const Struct_type* st = t->struct_type();
5873 // If we start with a named type, we don't dereference it to find
5874 // methods.
5875 if (nt == NULL)
5876 {
5877 const Type* pt = t->points_to();
5878 if (pt != NULL)
5879 {
5880 // If T is a pointer to a named type, then we need to look at
5881 // the type to which it points.
5882 is_pointer = true;
5883 nt = pt->named_type();
5884 st = pt->struct_type();
5885 }
5886 }
5887
5888 // If we have a named type, get the methods from it rather than from
5889 // any struct type.
5890 if (nt != NULL)
5891 st = NULL;
5892
5893 // Only named and struct types have methods.
5894 if (nt == NULL && st == NULL)
5895 {
5896 if (reason != NULL)
5897 {
5898 if (t->points_to() != NULL
5899 && t->points_to()->interface_type() != NULL)
5900 reason->assign(_("pointer to interface type has no methods"));
5901 else
5902 reason->assign(_("type has no methods"));
5903 }
5904 return false;
5905 }
5906
5907 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
5908 {
5909 if (reason != NULL)
5910 {
5911 if (t->points_to() != NULL
5912 && t->points_to()->interface_type() != NULL)
5913 reason->assign(_("pointer to interface type has no methods"));
5914 else
5915 reason->assign(_("type has no methods"));
5916 }
5917 return false;
5918 }
5919
5920 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5921 p != this->methods_->end();
5922 ++p)
5923 {
5924 bool is_ambiguous = false;
5925 Method* m = (nt != NULL
5926 ? nt->method_function(p->name(), &is_ambiguous)
5927 : st->method_function(p->name(), &is_ambiguous));
5928 if (m == NULL)
5929 {
5930 if (reason != NULL)
5931 {
5932 std::string n = Gogo::message_name(p->name());
5933 size_t len = n.length() + 100;
5934 char* buf = new char[len];
5935 if (is_ambiguous)
5936 snprintf(buf, len, _("ambiguous method %s%s%s"),
5937 open_quote, n.c_str(), close_quote);
5938 else
5939 snprintf(buf, len, _("missing method %s%s%s"),
5940 open_quote, n.c_str(), close_quote);
5941 reason->assign(buf);
5942 delete[] buf;
5943 }
5944 return false;
5945 }
5946
5947 Function_type *p_fn_type = p->type()->function_type();
5948 Function_type* m_fn_type = m->type()->function_type();
5949 gcc_assert(p_fn_type != NULL && m_fn_type != NULL);
5950 std::string subreason;
5951 if (!p_fn_type->is_identical(m_fn_type, true, true, &subreason))
5952 {
5953 if (reason != NULL)
5954 {
5955 std::string n = Gogo::message_name(p->name());
5956 size_t len = 100 + n.length() + subreason.length();
5957 char* buf = new char[len];
5958 if (subreason.empty())
5959 snprintf(buf, len, _("incompatible type for method %s%s%s"),
5960 open_quote, n.c_str(), close_quote);
5961 else
5962 snprintf(buf, len,
5963 _("incompatible type for method %s%s%s (%s)"),
5964 open_quote, n.c_str(), close_quote,
5965 subreason.c_str());
5966 reason->assign(buf);
5967 delete[] buf;
5968 }
5969 return false;
5970 }
5971
5972 if (!is_pointer && !m->is_value_method())
5973 {
5974 if (reason != NULL)
5975 {
5976 std::string n = Gogo::message_name(p->name());
5977 size_t len = 100 + n.length();
5978 char* buf = new char[len];
5979 snprintf(buf, len, _("method %s%s%s requires a pointer"),
5980 open_quote, n.c_str(), close_quote);
5981 reason->assign(buf);
5982 delete[] buf;
5983 }
5984 return false;
5985 }
5986 }
5987
5988 return true;
5989 }
5990
5991 // Return a tree for an interface type. An interface is a pointer to
5992 // a struct. The struct has three fields. The first field is a
5993 // pointer to the type descriptor for the dynamic type of the object.
5994 // The second field is a pointer to a table of methods for the
5995 // interface to be used with the object. The third field is the value
5996 // of the object itself.
5997
5998 tree
5999 Interface_type::do_get_tree(Gogo* gogo)
6000 {
6001 if (this->methods_ == NULL)
6002 {
6003 // At the tree level, use the same type for all empty
6004 // interfaces. This lets us assign them to each other directly
6005 // without triggering GIMPLE type errors.
6006 tree dtype = Type::make_type_descriptor_type()->get_tree(gogo);
6007 dtype = build_pointer_type(build_qualified_type(dtype, TYPE_QUAL_CONST));
6008 static tree empty_interface;
6009 return Gogo::builtin_struct(&empty_interface, "__go_empty_interface",
6010 NULL_TREE, 2,
6011 "__type_descriptor",
6012 dtype,
6013 "__object",
6014 ptr_type_node);
6015 }
6016
6017 return this->fill_in_tree(gogo, make_node(RECORD_TYPE));
6018 }
6019
6020 // Fill in the tree for an interface type. This is used for named
6021 // interface types.
6022
6023 tree
6024 Interface_type::fill_in_tree(Gogo* gogo, tree type)
6025 {
6026 gcc_assert(this->methods_ != NULL);
6027
6028 // Because the methods may refer to the interface type itself, we
6029 // need to build the interface type first, and then update the
6030 // method pointer later.
6031
6032 tree field_trees = NULL_TREE;
6033 tree* pp = &field_trees;
6034
6035 tree name_tree = get_identifier("__methods");
6036 tree methods_field = build_decl(this->location_, FIELD_DECL, name_tree,
6037 ptr_type_node);
6038 DECL_CONTEXT(methods_field) = type;
6039 *pp = methods_field;
6040 pp = &DECL_CHAIN(methods_field);
6041
6042 name_tree = get_identifier("__object");
6043 tree field = build_decl(this->location_, FIELD_DECL, name_tree,
6044 ptr_type_node);
6045 DECL_CONTEXT(field) = type;
6046 *pp = field;
6047
6048 TYPE_FIELDS(type) = field_trees;
6049
6050 layout_type(type);
6051
6052 // Build the type of the table of methods.
6053
6054 tree method_table = make_node(RECORD_TYPE);
6055
6056 // The first field is a pointer to the type descriptor.
6057 name_tree = get_identifier("__type_descriptor");
6058 tree dtype = Type::make_type_descriptor_type()->get_tree(gogo);
6059 dtype = build_pointer_type(build_qualified_type(dtype, TYPE_QUAL_CONST));
6060 field = build_decl(this->location_, FIELD_DECL, name_tree, dtype);
6061 DECL_CONTEXT(field) = method_table;
6062 TYPE_FIELDS(method_table) = field;
6063
6064 std::string last_name = "";
6065 pp = &DECL_CHAIN(field);
6066 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6067 p != this->methods_->end();
6068 ++p)
6069 {
6070 std::string name = Gogo::unpack_hidden_name(p->name());
6071 name_tree = get_identifier_with_length(name.data(), name.length());
6072 tree field_type = p->type()->get_tree(gogo);
6073 if (field_type == error_mark_node)
6074 return error_mark_node;
6075 field = build_decl(this->location_, FIELD_DECL, name_tree, field_type);
6076 DECL_CONTEXT(field) = method_table;
6077 *pp = field;
6078 pp = &DECL_CHAIN(field);
6079 // Sanity check: the names should be sorted.
6080 gcc_assert(p->name() > last_name);
6081 last_name = p->name();
6082 }
6083 layout_type(method_table);
6084
6085 // Update the type of the __methods field from a generic pointer to
6086 // a pointer to the method table.
6087 TREE_TYPE(methods_field) = build_pointer_type(method_table);
6088
6089 return type;
6090 }
6091
6092 // Initialization value.
6093
6094 tree
6095 Interface_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
6096 {
6097 if (is_clear)
6098 return NULL;
6099
6100 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
6101 for (tree field = TYPE_FIELDS(type_tree);
6102 field != NULL_TREE;
6103 field = DECL_CHAIN(field))
6104 {
6105 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
6106 elt->index = field;
6107 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
6108 }
6109
6110 tree ret = build_constructor(type_tree, init);
6111 TREE_CONSTANT(ret) = 1;
6112 return ret;
6113 }
6114
6115 // The type of an interface type descriptor.
6116
6117 Type*
6118 Interface_type::make_interface_type_descriptor_type()
6119 {
6120 static Type* ret;
6121 if (ret == NULL)
6122 {
6123 Type* tdt = Type::make_type_descriptor_type();
6124 Type* ptdt = Type::make_type_descriptor_ptr_type();
6125
6126 Type* string_type = Type::lookup_string_type();
6127 Type* pointer_string_type = Type::make_pointer_type(string_type);
6128
6129 Struct_type* sm =
6130 Type::make_builtin_struct_type(3,
6131 "name", pointer_string_type,
6132 "pkgPath", pointer_string_type,
6133 "typ", ptdt);
6134
6135 Type* nsm = Type::make_builtin_named_type("imethod", sm);
6136
6137 Type* slice_nsm = Type::make_array_type(nsm, NULL);
6138
6139 Struct_type* s = Type::make_builtin_struct_type(2,
6140 "", tdt,
6141 "methods", slice_nsm);
6142
6143 ret = Type::make_builtin_named_type("InterfaceType", s);
6144 }
6145
6146 return ret;
6147 }
6148
6149 // Build a type descriptor for an interface type.
6150
6151 Expression*
6152 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6153 {
6154 source_location bloc = BUILTINS_LOCATION;
6155
6156 Type* itdt = Interface_type::make_interface_type_descriptor_type();
6157
6158 const Struct_field_list* ifields = itdt->struct_type()->fields();
6159
6160 Expression_list* ivals = new Expression_list();
6161 ivals->reserve(2);
6162
6163 Struct_field_list::const_iterator pif = ifields->begin();
6164 gcc_assert(pif->field_name() == "commonType");
6165 ivals->push_back(this->type_descriptor_constructor(gogo,
6166 RUNTIME_TYPE_KIND_INTERFACE,
6167 name, NULL, true));
6168
6169 ++pif;
6170 gcc_assert(pif->field_name() == "methods");
6171
6172 Expression_list* methods = new Expression_list();
6173 if (this->methods_ != NULL && !this->methods_->empty())
6174 {
6175 Type* elemtype = pif->type()->array_type()->element_type();
6176
6177 methods->reserve(this->methods_->size());
6178 for (Typed_identifier_list::const_iterator pm = this->methods_->begin();
6179 pm != this->methods_->end();
6180 ++pm)
6181 {
6182 const Struct_field_list* mfields = elemtype->struct_type()->fields();
6183
6184 Expression_list* mvals = new Expression_list();
6185 mvals->reserve(3);
6186
6187 Struct_field_list::const_iterator pmf = mfields->begin();
6188 gcc_assert(pmf->field_name() == "name");
6189 std::string s = Gogo::unpack_hidden_name(pm->name());
6190 Expression* e = Expression::make_string(s, bloc);
6191 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6192
6193 ++pmf;
6194 gcc_assert(pmf->field_name() == "pkgPath");
6195 if (!Gogo::is_hidden_name(pm->name()))
6196 mvals->push_back(Expression::make_nil(bloc));
6197 else
6198 {
6199 s = Gogo::hidden_name_prefix(pm->name());
6200 e = Expression::make_string(s, bloc);
6201 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6202 }
6203
6204 ++pmf;
6205 gcc_assert(pmf->field_name() == "typ");
6206 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
6207
6208 ++pmf;
6209 gcc_assert(pmf == mfields->end());
6210
6211 e = Expression::make_struct_composite_literal(elemtype, mvals,
6212 bloc);
6213 methods->push_back(e);
6214 }
6215 }
6216
6217 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
6218 methods, bloc));
6219
6220 ++pif;
6221 gcc_assert(pif == ifields->end());
6222
6223 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
6224 }
6225
6226 // Reflection string.
6227
6228 void
6229 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
6230 {
6231 ret->append("interface {");
6232 if (this->methods_ != NULL)
6233 {
6234 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6235 p != this->methods_->end();
6236 ++p)
6237 {
6238 if (p != this->methods_->begin())
6239 ret->append(";");
6240 ret->push_back(' ');
6241 ret->append(Gogo::unpack_hidden_name(p->name()));
6242 std::string sub = p->type()->reflection(gogo);
6243 gcc_assert(sub.compare(0, 4, "func") == 0);
6244 sub = sub.substr(4);
6245 ret->append(sub);
6246 }
6247 }
6248 ret->append(" }");
6249 }
6250
6251 // Mangled name.
6252
6253 void
6254 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6255 {
6256 ret->push_back('I');
6257
6258 const Typed_identifier_list* methods = this->methods_;
6259 if (methods != NULL)
6260 {
6261 for (Typed_identifier_list::const_iterator p = methods->begin();
6262 p != methods->end();
6263 ++p)
6264 {
6265 std::string n = Gogo::unpack_hidden_name(p->name());
6266 char buf[20];
6267 snprintf(buf, sizeof buf, "%u_",
6268 static_cast<unsigned int>(n.length()));
6269 ret->append(buf);
6270 ret->append(n);
6271 this->append_mangled_name(p->type(), gogo, ret);
6272 }
6273 }
6274
6275 ret->push_back('e');
6276 }
6277
6278 // Export.
6279
6280 void
6281 Interface_type::do_export(Export* exp) const
6282 {
6283 exp->write_c_string("interface { ");
6284
6285 const Typed_identifier_list* methods = this->methods_;
6286 if (methods != NULL)
6287 {
6288 for (Typed_identifier_list::const_iterator pm = methods->begin();
6289 pm != methods->end();
6290 ++pm)
6291 {
6292 exp->write_string(pm->name());
6293 exp->write_c_string(" (");
6294
6295 const Function_type* fntype = pm->type()->function_type();
6296
6297 bool first = true;
6298 const Typed_identifier_list* parameters = fntype->parameters();
6299 if (parameters != NULL)
6300 {
6301 bool is_varargs = fntype->is_varargs();
6302 for (Typed_identifier_list::const_iterator pp =
6303 parameters->begin();
6304 pp != parameters->end();
6305 ++pp)
6306 {
6307 if (first)
6308 first = false;
6309 else
6310 exp->write_c_string(", ");
6311 if (!is_varargs || pp + 1 != parameters->end())
6312 exp->write_type(pp->type());
6313 else
6314 {
6315 exp->write_c_string("...");
6316 Type *pptype = pp->type();
6317 exp->write_type(pptype->array_type()->element_type());
6318 }
6319 }
6320 }
6321
6322 exp->write_c_string(")");
6323
6324 const Typed_identifier_list* results = fntype->results();
6325 if (results != NULL)
6326 {
6327 exp->write_c_string(" ");
6328 if (results->size() == 1)
6329 exp->write_type(results->begin()->type());
6330 else
6331 {
6332 first = true;
6333 exp->write_c_string("(");
6334 for (Typed_identifier_list::const_iterator p =
6335 results->begin();
6336 p != results->end();
6337 ++p)
6338 {
6339 if (first)
6340 first = false;
6341 else
6342 exp->write_c_string(", ");
6343 exp->write_type(p->type());
6344 }
6345 exp->write_c_string(")");
6346 }
6347 }
6348
6349 exp->write_c_string("; ");
6350 }
6351 }
6352
6353 exp->write_c_string("}");
6354 }
6355
6356 // Import an interface type.
6357
6358 Interface_type*
6359 Interface_type::do_import(Import* imp)
6360 {
6361 imp->require_c_string("interface { ");
6362
6363 Typed_identifier_list* methods = new Typed_identifier_list;
6364 while (imp->peek_char() != '}')
6365 {
6366 std::string name = imp->read_identifier();
6367 imp->require_c_string(" (");
6368
6369 Typed_identifier_list* parameters;
6370 bool is_varargs = false;
6371 if (imp->peek_char() == ')')
6372 parameters = NULL;
6373 else
6374 {
6375 parameters = new Typed_identifier_list;
6376 while (true)
6377 {
6378 if (imp->match_c_string("..."))
6379 {
6380 imp->advance(3);
6381 is_varargs = true;
6382 }
6383
6384 Type* ptype = imp->read_type();
6385 if (is_varargs)
6386 ptype = Type::make_array_type(ptype, NULL);
6387 parameters->push_back(Typed_identifier(Import::import_marker,
6388 ptype, imp->location()));
6389 if (imp->peek_char() != ',')
6390 break;
6391 gcc_assert(!is_varargs);
6392 imp->require_c_string(", ");
6393 }
6394 }
6395 imp->require_c_string(")");
6396
6397 Typed_identifier_list* results;
6398 if (imp->peek_char() != ' ')
6399 results = NULL;
6400 else
6401 {
6402 results = new Typed_identifier_list;
6403 imp->advance(1);
6404 if (imp->peek_char() != '(')
6405 {
6406 Type* rtype = imp->read_type();
6407 results->push_back(Typed_identifier(Import::import_marker,
6408 rtype, imp->location()));
6409 }
6410 else
6411 {
6412 imp->advance(1);
6413 while (true)
6414 {
6415 Type* rtype = imp->read_type();
6416 results->push_back(Typed_identifier(Import::import_marker,
6417 rtype, imp->location()));
6418 if (imp->peek_char() != ',')
6419 break;
6420 imp->require_c_string(", ");
6421 }
6422 imp->require_c_string(")");
6423 }
6424 }
6425
6426 Function_type* fntype = Type::make_function_type(NULL, parameters,
6427 results,
6428 imp->location());
6429 if (is_varargs)
6430 fntype->set_is_varargs();
6431 methods->push_back(Typed_identifier(name, fntype, imp->location()));
6432
6433 imp->require_c_string("; ");
6434 }
6435
6436 imp->require_c_string("}");
6437
6438 if (methods->empty())
6439 {
6440 delete methods;
6441 methods = NULL;
6442 }
6443
6444 return Type::make_interface_type(methods, imp->location());
6445 }
6446
6447 // Make an interface type.
6448
6449 Interface_type*
6450 Type::make_interface_type(Typed_identifier_list* methods,
6451 source_location location)
6452 {
6453 return new Interface_type(methods, location);
6454 }
6455
6456 // Class Method.
6457
6458 // Bind a method to an object.
6459
6460 Expression*
6461 Method::bind_method(Expression* expr, source_location location) const
6462 {
6463 if (this->stub_ == NULL)
6464 {
6465 // When there is no stub object, the binding is determined by
6466 // the child class.
6467 return this->do_bind_method(expr, location);
6468 }
6469
6470 Expression* func = Expression::make_func_reference(this->stub_, NULL,
6471 location);
6472 return Expression::make_bound_method(expr, func, location);
6473 }
6474
6475 // Return the named object associated with a method. This may only be
6476 // called after methods are finalized.
6477
6478 Named_object*
6479 Method::named_object() const
6480 {
6481 if (this->stub_ != NULL)
6482 return this->stub_;
6483 return this->do_named_object();
6484 }
6485
6486 // Class Named_method.
6487
6488 // The type of the method.
6489
6490 Function_type*
6491 Named_method::do_type() const
6492 {
6493 if (this->named_object_->is_function())
6494 return this->named_object_->func_value()->type();
6495 else if (this->named_object_->is_function_declaration())
6496 return this->named_object_->func_declaration_value()->type();
6497 else
6498 gcc_unreachable();
6499 }
6500
6501 // Return the location of the method receiver.
6502
6503 source_location
6504 Named_method::do_receiver_location() const
6505 {
6506 return this->do_type()->receiver()->location();
6507 }
6508
6509 // Bind a method to an object.
6510
6511 Expression*
6512 Named_method::do_bind_method(Expression* expr, source_location location) const
6513 {
6514 Expression* func = Expression::make_func_reference(this->named_object_, NULL,
6515 location);
6516 Bound_method_expression* bme = Expression::make_bound_method(expr, func,
6517 location);
6518 // If this is not a local method, and it does not use a stub, then
6519 // the real method expects a different type. We need to cast the
6520 // first argument.
6521 if (this->depth() > 0 && !this->needs_stub_method())
6522 {
6523 Function_type* ftype = this->do_type();
6524 gcc_assert(ftype->is_method());
6525 Type* frtype = ftype->receiver()->type();
6526 bme->set_first_argument_type(frtype);
6527 }
6528 return bme;
6529 }
6530
6531 // Class Interface_method.
6532
6533 // Bind a method to an object.
6534
6535 Expression*
6536 Interface_method::do_bind_method(Expression* expr,
6537 source_location location) const
6538 {
6539 return Expression::make_interface_field_reference(expr, this->name_,
6540 location);
6541 }
6542
6543 // Class Methods.
6544
6545 // Insert a new method. Return true if it was inserted, false
6546 // otherwise.
6547
6548 bool
6549 Methods::insert(const std::string& name, Method* m)
6550 {
6551 std::pair<Method_map::iterator, bool> ins =
6552 this->methods_.insert(std::make_pair(name, m));
6553 if (ins.second)
6554 return true;
6555 else
6556 {
6557 Method* old_method = ins.first->second;
6558 if (m->depth() < old_method->depth())
6559 {
6560 delete old_method;
6561 ins.first->second = m;
6562 return true;
6563 }
6564 else
6565 {
6566 if (m->depth() == old_method->depth())
6567 old_method->set_is_ambiguous();
6568 return false;
6569 }
6570 }
6571 }
6572
6573 // Return the number of unambiguous methods.
6574
6575 size_t
6576 Methods::count() const
6577 {
6578 size_t ret = 0;
6579 for (Method_map::const_iterator p = this->methods_.begin();
6580 p != this->methods_.end();
6581 ++p)
6582 if (!p->second->is_ambiguous())
6583 ++ret;
6584 return ret;
6585 }
6586
6587 // Class Named_type.
6588
6589 // Return the name of the type.
6590
6591 const std::string&
6592 Named_type::name() const
6593 {
6594 return this->named_object_->name();
6595 }
6596
6597 // Return the name of the type to use in an error message.
6598
6599 std::string
6600 Named_type::message_name() const
6601 {
6602 return this->named_object_->message_name();
6603 }
6604
6605 // Return the base type for this type. We have to be careful about
6606 // circular type definitions, which are invalid but may be seen here.
6607
6608 Type*
6609 Named_type::named_base()
6610 {
6611 if (this->seen_ > 0)
6612 return this;
6613 ++this->seen_;
6614 Type* ret = this->type_->base();
6615 --this->seen_;
6616 return ret;
6617 }
6618
6619 const Type*
6620 Named_type::named_base() const
6621 {
6622 if (this->seen_ > 0)
6623 return this;
6624 ++this->seen_;
6625 const Type* ret = this->type_->base();
6626 --this->seen_;
6627 return ret;
6628 }
6629
6630 // Return whether this is an error type. We have to be careful about
6631 // circular type definitions, which are invalid but may be seen here.
6632
6633 bool
6634 Named_type::is_named_error_type() const
6635 {
6636 if (this->seen_ > 0)
6637 return false;
6638 ++this->seen_;
6639 bool ret = this->type_->is_error_type();
6640 --this->seen_;
6641 return ret;
6642 }
6643
6644 // Add a method to this type.
6645
6646 Named_object*
6647 Named_type::add_method(const std::string& name, Function* function)
6648 {
6649 if (this->local_methods_ == NULL)
6650 this->local_methods_ = new Bindings(NULL);
6651 return this->local_methods_->add_function(name, NULL, function);
6652 }
6653
6654 // Add a method declaration to this type.
6655
6656 Named_object*
6657 Named_type::add_method_declaration(const std::string& name, Package* package,
6658 Function_type* type,
6659 source_location location)
6660 {
6661 if (this->local_methods_ == NULL)
6662 this->local_methods_ = new Bindings(NULL);
6663 return this->local_methods_->add_function_declaration(name, package, type,
6664 location);
6665 }
6666
6667 // Add an existing method to this type.
6668
6669 void
6670 Named_type::add_existing_method(Named_object* no)
6671 {
6672 if (this->local_methods_ == NULL)
6673 this->local_methods_ = new Bindings(NULL);
6674 this->local_methods_->add_named_object(no);
6675 }
6676
6677 // Look for a local method NAME, and returns its named object, or NULL
6678 // if not there.
6679
6680 Named_object*
6681 Named_type::find_local_method(const std::string& name) const
6682 {
6683 if (this->local_methods_ == NULL)
6684 return NULL;
6685 return this->local_methods_->lookup(name);
6686 }
6687
6688 // Return whether NAME is an unexported field or method, for better
6689 // error reporting.
6690
6691 bool
6692 Named_type::is_unexported_local_method(Gogo* gogo,
6693 const std::string& name) const
6694 {
6695 Bindings* methods = this->local_methods_;
6696 if (methods != NULL)
6697 {
6698 for (Bindings::const_declarations_iterator p =
6699 methods->begin_declarations();
6700 p != methods->end_declarations();
6701 ++p)
6702 {
6703 if (Gogo::is_hidden_name(p->first)
6704 && name == Gogo::unpack_hidden_name(p->first)
6705 && gogo->pack_hidden_name(name, false) != p->first)
6706 return true;
6707 }
6708 }
6709 return false;
6710 }
6711
6712 // Build the complete list of methods for this type, which means
6713 // recursively including all methods for anonymous fields. Create all
6714 // stub methods.
6715
6716 void
6717 Named_type::finalize_methods(Gogo* gogo)
6718 {
6719 if (this->all_methods_ != NULL)
6720 return;
6721
6722 if (this->local_methods_ != NULL
6723 && (this->points_to() != NULL || this->interface_type() != NULL))
6724 {
6725 const Bindings* lm = this->local_methods_;
6726 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
6727 p != lm->end_declarations();
6728 ++p)
6729 error_at(p->second->location(),
6730 "invalid pointer or interface receiver type");
6731 delete this->local_methods_;
6732 this->local_methods_ = NULL;
6733 return;
6734 }
6735
6736 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
6737 }
6738
6739 // Return the method NAME, or NULL if there isn't one or if it is
6740 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6741 // ambiguous.
6742
6743 Method*
6744 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
6745 {
6746 return Type::method_function(this->all_methods_, name, is_ambiguous);
6747 }
6748
6749 // Return a pointer to the interface method table for this type for
6750 // the interface INTERFACE. IS_POINTER is true if this is for a
6751 // pointer to THIS.
6752
6753 tree
6754 Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
6755 bool is_pointer)
6756 {
6757 gcc_assert(!interface->is_empty());
6758
6759 Interface_method_tables** pimt = (is_pointer
6760 ? &this->interface_method_tables_
6761 : &this->pointer_interface_method_tables_);
6762
6763 if (*pimt == NULL)
6764 *pimt = new Interface_method_tables(5);
6765
6766 std::pair<const Interface_type*, tree> val(interface, NULL_TREE);
6767 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
6768
6769 if (ins.second)
6770 {
6771 // This is a new entry in the hash table.
6772 gcc_assert(ins.first->second == NULL_TREE);
6773 ins.first->second = gogo->interface_method_table_for_type(interface,
6774 this,
6775 is_pointer);
6776 }
6777
6778 tree decl = ins.first->second;
6779 if (decl == error_mark_node)
6780 return error_mark_node;
6781 gcc_assert(decl != NULL_TREE && TREE_CODE(decl) == VAR_DECL);
6782 return build_fold_addr_expr(decl);
6783 }
6784
6785 // Return whether a named type has any hidden fields.
6786
6787 bool
6788 Named_type::named_type_has_hidden_fields(std::string* reason) const
6789 {
6790 if (this->seen_ > 0)
6791 return false;
6792 ++this->seen_;
6793 bool ret = this->type_->has_hidden_fields(this, reason);
6794 --this->seen_;
6795 return ret;
6796 }
6797
6798 // Look for a use of a complete type within another type. This is
6799 // used to check that we don't try to use a type within itself.
6800
6801 class Find_type_use : public Traverse
6802 {
6803 public:
6804 Find_type_use(Type* find_type)
6805 : Traverse(traverse_types),
6806 find_type_(find_type), found_(false)
6807 { }
6808
6809 // Whether we found the type.
6810 bool
6811 found() const
6812 { return this->found_; }
6813
6814 protected:
6815 int
6816 type(Type*);
6817
6818 private:
6819 // The type we are looking for.
6820 Type* find_type_;
6821 // Whether we found the type.
6822 bool found_;
6823 };
6824
6825 // Check for FIND_TYPE in TYPE.
6826
6827 int
6828 Find_type_use::type(Type* type)
6829 {
6830 if (this->find_type_ == type)
6831 {
6832 this->found_ = true;
6833 return TRAVERSE_EXIT;
6834 }
6835 // It's OK if we see a reference to the type in any type which is
6836 // essentially a pointer: a pointer, a slice, a function, a map, or
6837 // a channel.
6838 if (type->points_to() != NULL
6839 || type->is_open_array_type()
6840 || type->function_type() != NULL
6841 || type->map_type() != NULL
6842 || type->channel_type() != NULL)
6843 return TRAVERSE_SKIP_COMPONENTS;
6844
6845 // For an interface, a reference to the type in a method type should
6846 // be ignored, but we have to consider direct inheritance. When
6847 // this is called, there may be cases of direct inheritance
6848 // represented as a method with no name.
6849 if (type->interface_type() != NULL)
6850 {
6851 const Typed_identifier_list* methods = type->interface_type()->methods();
6852 if (methods != NULL)
6853 {
6854 for (Typed_identifier_list::const_iterator p = methods->begin();
6855 p != methods->end();
6856 ++p)
6857 {
6858 if (p->name().empty())
6859 {
6860 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
6861 return TRAVERSE_EXIT;
6862 }
6863 }
6864 }
6865 return TRAVERSE_SKIP_COMPONENTS;
6866 }
6867
6868 return TRAVERSE_CONTINUE;
6869 }
6870
6871 // Verify that a named type does not refer to itself.
6872
6873 bool
6874 Named_type::do_verify()
6875 {
6876 Find_type_use find(this);
6877 Type::traverse(this->type_, &find);
6878 if (find.found())
6879 {
6880 error_at(this->location_, "invalid recursive type %qs",
6881 this->message_name().c_str());
6882 this->is_error_ = true;
6883 return false;
6884 }
6885
6886 // Check whether any of the local methods overloads an existing
6887 // struct field or interface method. We don't need to check the
6888 // list of methods against itself: that is handled by the Bindings
6889 // code.
6890 if (this->local_methods_ != NULL)
6891 {
6892 Struct_type* st = this->type_->struct_type();
6893 Interface_type* it = this->type_->interface_type();
6894 bool found_dup = false;
6895 if (st != NULL || it != NULL)
6896 {
6897 for (Bindings::const_declarations_iterator p =
6898 this->local_methods_->begin_declarations();
6899 p != this->local_methods_->end_declarations();
6900 ++p)
6901 {
6902 const std::string& name(p->first);
6903 if (st != NULL && st->find_local_field(name, NULL) != NULL)
6904 {
6905 error_at(p->second->location(),
6906 "method %qs redeclares struct field name",
6907 Gogo::message_name(name).c_str());
6908 found_dup = true;
6909 }
6910 if (it != NULL && it->find_method(name) != NULL)
6911 {
6912 error_at(p->second->location(),
6913 "method %qs redeclares interface method name",
6914 Gogo::message_name(name).c_str());
6915 found_dup = true;
6916 }
6917 }
6918 }
6919 if (found_dup)
6920 return false;
6921 }
6922
6923 // If this is a struct, then if any of the fields of the struct
6924 // themselves have struct type, then this struct must be converted
6925 // to the backend representation before the field's type is
6926 // converted. That may seem backward, but it works because if the
6927 // field's type refers to this one, e.g., via a pointer, then the
6928 // conversion process will pick up the half-built struct and do the
6929 // right thing.
6930 if (this->struct_type() != NULL)
6931 {
6932 const Struct_field_list* fields = this->struct_type()->fields();
6933 for (Struct_field_list::const_iterator p = fields->begin();
6934 p != fields->end();
6935 ++p)
6936 {
6937 Struct_type* st = p->type()->struct_type();
6938 if (st != NULL)
6939 st->add_prerequisite(this);
6940 }
6941 }
6942
6943 return true;
6944 }
6945
6946 // Return whether this type is or contains a pointer.
6947
6948 bool
6949 Named_type::do_has_pointer() const
6950 {
6951 if (this->seen_ > 0)
6952 return false;
6953 ++this->seen_;
6954 bool ret = this->type_->has_pointer();
6955 --this->seen_;
6956 return ret;
6957 }
6958
6959 // Return a hash code. This is used for method lookup. We simply
6960 // hash on the name itself.
6961
6962 unsigned int
6963 Named_type::do_hash_for_method(Gogo* gogo) const
6964 {
6965 const std::string& name(this->named_object()->name());
6966 unsigned int ret = Type::hash_string(name, 0);
6967
6968 // GOGO will be NULL here when called from Type_hash_identical.
6969 // That is OK because that is only used for internal hash tables
6970 // where we are going to be comparing named types for equality. In
6971 // other cases, which are cases where the runtime is going to
6972 // compare hash codes to see if the types are the same, we need to
6973 // include the package prefix and name in the hash.
6974 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
6975 {
6976 const Package* package = this->named_object()->package();
6977 if (package == NULL)
6978 {
6979 ret = Type::hash_string(gogo->unique_prefix(), ret);
6980 ret = Type::hash_string(gogo->package_name(), ret);
6981 }
6982 else
6983 {
6984 ret = Type::hash_string(package->unique_prefix(), ret);
6985 ret = Type::hash_string(package->name(), ret);
6986 }
6987 }
6988
6989 return ret;
6990 }
6991
6992 // Get a tree for a named type.
6993
6994 tree
6995 Named_type::do_get_tree(Gogo* gogo)
6996 {
6997 if (this->is_error_)
6998 return error_mark_node;
6999
7000 // Go permits types to refer to themselves in various ways. Break
7001 // the recursion here.
7002 tree t;
7003 switch (this->type_->forwarded()->classification())
7004 {
7005 case TYPE_ERROR:
7006 return error_mark_node;
7007
7008 case TYPE_VOID:
7009 case TYPE_BOOLEAN:
7010 case TYPE_INTEGER:
7011 case TYPE_FLOAT:
7012 case TYPE_COMPLEX:
7013 case TYPE_STRING:
7014 case TYPE_NIL:
7015 // These types can not refer to themselves.
7016 case TYPE_MAP:
7017 case TYPE_CHANNEL:
7018 // All maps and channels have the same type in GENERIC.
7019 t = Type::get_named_type_tree(gogo, this->type_);
7020 if (t == error_mark_node)
7021 return error_mark_node;
7022 // Build a copy to set TYPE_NAME.
7023 t = build_variant_type_copy(t);
7024 break;
7025
7026 case TYPE_FUNCTION:
7027 // GENERIC can't handle a pointer to a function type whose
7028 // return type is a pointer to the function type itself. It
7029 // goes into an infinite loop when walking the types.
7030 if (this->seen_ > 0)
7031 {
7032 Function_type* fntype = this->type_->function_type();
7033 if (fntype->results() != NULL
7034 && fntype->results()->size() == 1
7035 && fntype->results()->front().type()->forwarded() == this)
7036 return ptr_type_node;
7037
7038 // We can legitimately see ourselves here twice when a named
7039 // type is defined using a struct which refers to the named
7040 // type. If we see ourselves too often we are in a loop.
7041 if (this->seen_ > 3)
7042 return ptr_type_node;
7043 }
7044 ++this->seen_;
7045 t = Type::get_named_type_tree(gogo, this->type_);
7046 --this->seen_;
7047 if (t == error_mark_node)
7048 return error_mark_node;
7049 t = build_variant_type_copy(t);
7050 break;
7051
7052 case TYPE_POINTER:
7053 // Don't recur infinitely if a pointer type refers to itself.
7054 // Ideally we would build a circular data structure here, but
7055 // GENERIC can't handle them.
7056 if (this->seen_ > 0)
7057 {
7058 if (this->type_->points_to()->forwarded() == this)
7059 return ptr_type_node;
7060
7061 if (this->seen_ > 3)
7062 return ptr_type_node;
7063 }
7064 ++this->seen_;
7065 t = Type::get_named_type_tree(gogo, this->type_);
7066 --this->seen_;
7067 if (t == error_mark_node)
7068 return error_mark_node;
7069 t = build_variant_type_copy(t);
7070 break;
7071
7072 case TYPE_STRUCT:
7073 // If there are structs which must be converted first, do them.
7074 if (this->seen_ == 0)
7075 {
7076 ++this->seen_;
7077 this->type_->struct_type()->convert_prerequisites(gogo);
7078 --this->seen_;
7079 }
7080
7081 if (this->named_tree_ != NULL_TREE)
7082 return this->named_tree_;
7083
7084 t = make_node(RECORD_TYPE);
7085 this->named_tree_ = t;
7086 t = this->type_->struct_type()->fill_in_tree(gogo, t);
7087 if (t == error_mark_node)
7088 return error_mark_node;
7089 break;
7090
7091 case TYPE_ARRAY:
7092 if (!this->is_open_array_type())
7093 t = Type::get_named_type_tree(gogo, this->type_);
7094 else
7095 {
7096 if (this->named_tree_ != NULL_TREE)
7097 return this->named_tree_;
7098 t = gogo->slice_type_tree(void_type_node);
7099 this->named_tree_ = t;
7100 t = this->type_->array_type()->fill_in_tree(gogo, t);
7101 }
7102 if (t == error_mark_node)
7103 return error_mark_node;
7104 t = build_variant_type_copy(t);
7105 break;
7106
7107 case TYPE_INTERFACE:
7108 if (this->type_->interface_type()->is_empty())
7109 {
7110 t = Type::get_named_type_tree(gogo, this->type_);
7111 if (t == error_mark_node)
7112 return error_mark_node;
7113 t = build_variant_type_copy(t);
7114 }
7115 else
7116 {
7117 if (this->named_tree_ != NULL_TREE)
7118 return this->named_tree_;
7119 t = make_node(RECORD_TYPE);
7120 this->named_tree_ = t;
7121 t = this->type_->interface_type()->fill_in_tree(gogo, t);
7122 if (t == error_mark_node)
7123 return error_mark_node;
7124 }
7125 break;
7126
7127 case TYPE_NAMED:
7128 {
7129 // When a named type T1 is defined as another named type T2,
7130 // the definition must simply be "type T1 T2". If the
7131 // definition of T2 may refer to T1, then we must simply
7132 // return the type for T2 here. It's not precisely correct,
7133 // but it's as close as we can get with GENERIC.
7134 ++this->seen_;
7135 t = Type::get_named_type_tree(gogo, this->type_);
7136 --this->seen_;
7137 if (this->seen_ > 0)
7138 return t;
7139 if (t == error_mark_node)
7140 return error_mark_node;
7141 t = build_variant_type_copy(t);
7142 }
7143 break;
7144
7145 case TYPE_FORWARD:
7146 // An undefined forwarding type. Make sure the error is
7147 // emitted.
7148 this->type_->forward_declaration_type()->real_type();
7149 return error_mark_node;
7150
7151 default:
7152 case TYPE_SINK:
7153 case TYPE_CALL_MULTIPLE_RESULT:
7154 gcc_unreachable();
7155 }
7156
7157 tree id = this->named_object_->get_id(gogo);
7158 tree decl = build_decl(this->location_, TYPE_DECL, id, t);
7159 TYPE_NAME(t) = decl;
7160
7161 return t;
7162 }
7163
7164 // Build a type descriptor for a named type.
7165
7166 Expression*
7167 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7168 {
7169 // If NAME is not NULL, then we don't really want the type
7170 // descriptor for this type; we want the descriptor for the
7171 // underlying type, giving it the name NAME.
7172 return this->named_type_descriptor(gogo, this->type_,
7173 name == NULL ? this : name);
7174 }
7175
7176 // Add to the reflection string. This is used mostly for the name of
7177 // the type used in a type descriptor, not for actual reflection
7178 // strings.
7179
7180 void
7181 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
7182 {
7183 if (this->location() != BUILTINS_LOCATION)
7184 {
7185 const Package* package = this->named_object_->package();
7186 if (package != NULL)
7187 ret->append(package->name());
7188 else
7189 ret->append(gogo->package_name());
7190 ret->push_back('.');
7191 }
7192 if (this->in_function_ != NULL)
7193 {
7194 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
7195 ret->push_back('$');
7196 }
7197 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
7198 }
7199
7200 // Get the mangled name.
7201
7202 void
7203 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
7204 {
7205 Named_object* no = this->named_object_;
7206 std::string name;
7207 if (this->location() == BUILTINS_LOCATION)
7208 gcc_assert(this->in_function_ == NULL);
7209 else
7210 {
7211 const std::string& unique_prefix(no->package() == NULL
7212 ? gogo->unique_prefix()
7213 : no->package()->unique_prefix());
7214 const std::string& package_name(no->package() == NULL
7215 ? gogo->package_name()
7216 : no->package()->name());
7217 name = unique_prefix;
7218 name.append(1, '.');
7219 name.append(package_name);
7220 name.append(1, '.');
7221 if (this->in_function_ != NULL)
7222 {
7223 name.append(Gogo::unpack_hidden_name(this->in_function_->name()));
7224 name.append(1, '$');
7225 }
7226 }
7227 name.append(Gogo::unpack_hidden_name(no->name()));
7228 char buf[20];
7229 snprintf(buf, sizeof buf, "N%u_", static_cast<unsigned int>(name.length()));
7230 ret->append(buf);
7231 ret->append(name);
7232 }
7233
7234 // Export the type. This is called to export a global type.
7235
7236 void
7237 Named_type::export_named_type(Export* exp, const std::string&) const
7238 {
7239 // We don't need to write the name of the type here, because it will
7240 // be written by Export::write_type anyhow.
7241 exp->write_c_string("type ");
7242 exp->write_type(this);
7243 exp->write_c_string(";\n");
7244 }
7245
7246 // Import a named type.
7247
7248 void
7249 Named_type::import_named_type(Import* imp, Named_type** ptype)
7250 {
7251 imp->require_c_string("type ");
7252 Type *type = imp->read_type();
7253 *ptype = type->named_type();
7254 gcc_assert(*ptype != NULL);
7255 imp->require_c_string(";\n");
7256 }
7257
7258 // Export the type when it is referenced by another type. In this
7259 // case Export::export_type will already have issued the name.
7260
7261 void
7262 Named_type::do_export(Export* exp) const
7263 {
7264 exp->write_type(this->type_);
7265
7266 // To save space, we only export the methods directly attached to
7267 // this type.
7268 Bindings* methods = this->local_methods_;
7269 if (methods == NULL)
7270 return;
7271
7272 exp->write_c_string("\n");
7273 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
7274 p != methods->end_definitions();
7275 ++p)
7276 {
7277 exp->write_c_string(" ");
7278 (*p)->export_named_object(exp);
7279 }
7280
7281 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
7282 p != methods->end_declarations();
7283 ++p)
7284 {
7285 if (p->second->is_function_declaration())
7286 {
7287 exp->write_c_string(" ");
7288 p->second->export_named_object(exp);
7289 }
7290 }
7291 }
7292
7293 // Make a named type.
7294
7295 Named_type*
7296 Type::make_named_type(Named_object* named_object, Type* type,
7297 source_location location)
7298 {
7299 return new Named_type(named_object, type, location);
7300 }
7301
7302 // Finalize the methods for TYPE. It will be a named type or a struct
7303 // type. This sets *ALL_METHODS to the list of methods, and builds
7304 // all required stubs.
7305
7306 void
7307 Type::finalize_methods(Gogo* gogo, const Type* type, source_location location,
7308 Methods** all_methods)
7309 {
7310 *all_methods = NULL;
7311 Types_seen types_seen;
7312 Type::add_methods_for_type(type, NULL, 0, false, false, &types_seen,
7313 all_methods);
7314 Type::build_stub_methods(gogo, type, *all_methods, location);
7315 }
7316
7317 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
7318 // build up the struct field indexes as we go. DEPTH is the depth of
7319 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
7320 // adding these methods for an anonymous field with pointer type.
7321 // NEEDS_STUB_METHOD is true if we need to use a stub method which
7322 // calls the real method. TYPES_SEEN is used to avoid infinite
7323 // recursion.
7324
7325 void
7326 Type::add_methods_for_type(const Type* type,
7327 const Method::Field_indexes* field_indexes,
7328 unsigned int depth,
7329 bool is_embedded_pointer,
7330 bool needs_stub_method,
7331 Types_seen* types_seen,
7332 Methods** methods)
7333 {
7334 // Pointer types may not have methods.
7335 if (type->points_to() != NULL)
7336 return;
7337
7338 const Named_type* nt = type->named_type();
7339 if (nt != NULL)
7340 {
7341 std::pair<Types_seen::iterator, bool> ins = types_seen->insert(nt);
7342 if (!ins.second)
7343 return;
7344 }
7345
7346 if (nt != NULL)
7347 Type::add_local_methods_for_type(nt, field_indexes, depth,
7348 is_embedded_pointer, needs_stub_method,
7349 methods);
7350
7351 Type::add_embedded_methods_for_type(type, field_indexes, depth,
7352 is_embedded_pointer, needs_stub_method,
7353 types_seen, methods);
7354
7355 // If we are called with depth > 0, then we are looking at an
7356 // anonymous field of a struct. If such a field has interface type,
7357 // then we need to add the interface methods. We don't want to add
7358 // them when depth == 0, because we will already handle them
7359 // following the usual rules for an interface type.
7360 if (depth > 0)
7361 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
7362 }
7363
7364 // Add the local methods for the named type NT to *METHODS. The
7365 // parameters are as for add_methods_to_type.
7366
7367 void
7368 Type::add_local_methods_for_type(const Named_type* nt,
7369 const Method::Field_indexes* field_indexes,
7370 unsigned int depth,
7371 bool is_embedded_pointer,
7372 bool needs_stub_method,
7373 Methods** methods)
7374 {
7375 const Bindings* local_methods = nt->local_methods();
7376 if (local_methods == NULL)
7377 return;
7378
7379 if (*methods == NULL)
7380 *methods = new Methods();
7381
7382 for (Bindings::const_declarations_iterator p =
7383 local_methods->begin_declarations();
7384 p != local_methods->end_declarations();
7385 ++p)
7386 {
7387 Named_object* no = p->second;
7388 bool is_value_method = (is_embedded_pointer
7389 || !Type::method_expects_pointer(no));
7390 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
7391 (needs_stub_method
7392 || (depth > 0 && is_value_method)));
7393 if (!(*methods)->insert(no->name(), m))
7394 delete m;
7395 }
7396 }
7397
7398 // Add the embedded methods for TYPE to *METHODS. These are the
7399 // methods attached to anonymous fields. The parameters are as for
7400 // add_methods_to_type.
7401
7402 void
7403 Type::add_embedded_methods_for_type(const Type* type,
7404 const Method::Field_indexes* field_indexes,
7405 unsigned int depth,
7406 bool is_embedded_pointer,
7407 bool needs_stub_method,
7408 Types_seen* types_seen,
7409 Methods** methods)
7410 {
7411 // Look for anonymous fields in TYPE. TYPE has fields if it is a
7412 // struct.
7413 const Struct_type* st = type->struct_type();
7414 if (st == NULL)
7415 return;
7416
7417 const Struct_field_list* fields = st->fields();
7418 if (fields == NULL)
7419 return;
7420
7421 unsigned int i = 0;
7422 for (Struct_field_list::const_iterator pf = fields->begin();
7423 pf != fields->end();
7424 ++pf, ++i)
7425 {
7426 if (!pf->is_anonymous())
7427 continue;
7428
7429 Type* ftype = pf->type();
7430 bool is_pointer = false;
7431 if (ftype->points_to() != NULL)
7432 {
7433 ftype = ftype->points_to();
7434 is_pointer = true;
7435 }
7436 Named_type* fnt = ftype->named_type();
7437 if (fnt == NULL)
7438 {
7439 // This is an error, but it will be diagnosed elsewhere.
7440 continue;
7441 }
7442
7443 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
7444 sub_field_indexes->next = field_indexes;
7445 sub_field_indexes->field_index = i;
7446
7447 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
7448 (is_embedded_pointer || is_pointer),
7449 (needs_stub_method
7450 || is_pointer
7451 || i > 0),
7452 types_seen,
7453 methods);
7454 }
7455 }
7456
7457 // If TYPE is an interface type, then add its method to *METHODS.
7458 // This is for interface methods attached to an anonymous field. The
7459 // parameters are as for add_methods_for_type.
7460
7461 void
7462 Type::add_interface_methods_for_type(const Type* type,
7463 const Method::Field_indexes* field_indexes,
7464 unsigned int depth,
7465 Methods** methods)
7466 {
7467 const Interface_type* it = type->interface_type();
7468 if (it == NULL)
7469 return;
7470
7471 const Typed_identifier_list* imethods = it->methods();
7472 if (imethods == NULL)
7473 return;
7474
7475 if (*methods == NULL)
7476 *methods = new Methods();
7477
7478 for (Typed_identifier_list::const_iterator pm = imethods->begin();
7479 pm != imethods->end();
7480 ++pm)
7481 {
7482 Function_type* fntype = pm->type()->function_type();
7483 gcc_assert(fntype != NULL && !fntype->is_method());
7484 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
7485 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
7486 field_indexes, depth);
7487 if (!(*methods)->insert(pm->name(), m))
7488 delete m;
7489 }
7490 }
7491
7492 // Build stub methods for TYPE as needed. METHODS is the set of
7493 // methods for the type. A stub method may be needed when a type
7494 // inherits a method from an anonymous field. When we need the
7495 // address of the method, as in a type descriptor, we need to build a
7496 // little stub which does the required field dereferences and jumps to
7497 // the real method. LOCATION is the location of the type definition.
7498
7499 void
7500 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
7501 source_location location)
7502 {
7503 if (methods == NULL)
7504 return;
7505 for (Methods::const_iterator p = methods->begin();
7506 p != methods->end();
7507 ++p)
7508 {
7509 Method* m = p->second;
7510 if (m->is_ambiguous() || !m->needs_stub_method())
7511 continue;
7512
7513 const std::string& name(p->first);
7514
7515 // Build a stub method.
7516
7517 const Function_type* fntype = m->type();
7518
7519 static unsigned int counter;
7520 char buf[100];
7521 snprintf(buf, sizeof buf, "$this%u", counter);
7522 ++counter;
7523
7524 Type* receiver_type = const_cast<Type*>(type);
7525 if (!m->is_value_method())
7526 receiver_type = Type::make_pointer_type(receiver_type);
7527 source_location receiver_location = m->receiver_location();
7528 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
7529 receiver_location);
7530
7531 const Typed_identifier_list* fnparams = fntype->parameters();
7532 Typed_identifier_list* stub_params;
7533 if (fnparams == NULL || fnparams->empty())
7534 stub_params = NULL;
7535 else
7536 {
7537 // We give each stub parameter a unique name.
7538 stub_params = new Typed_identifier_list();
7539 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
7540 pp != fnparams->end();
7541 ++pp)
7542 {
7543 char pbuf[100];
7544 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
7545 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
7546 pp->location()));
7547 ++counter;
7548 }
7549 }
7550
7551 const Typed_identifier_list* fnresults = fntype->results();
7552 Typed_identifier_list* stub_results;
7553 if (fnresults == NULL || fnresults->empty())
7554 stub_results = NULL;
7555 else
7556 {
7557 // We create the result parameters without any names, since
7558 // we won't refer to them.
7559 stub_results = new Typed_identifier_list();
7560 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
7561 pr != fnresults->end();
7562 ++pr)
7563 stub_results->push_back(Typed_identifier("", pr->type(),
7564 pr->location()));
7565 }
7566
7567 Function_type* stub_type = Type::make_function_type(receiver,
7568 stub_params,
7569 stub_results,
7570 fntype->location());
7571 if (fntype->is_varargs())
7572 stub_type->set_is_varargs();
7573
7574 // We only create the function in the package which creates the
7575 // type.
7576 const Package* package;
7577 if (type->named_type() == NULL)
7578 package = NULL;
7579 else
7580 package = type->named_type()->named_object()->package();
7581 Named_object* stub;
7582 if (package != NULL)
7583 stub = Named_object::make_function_declaration(name, package,
7584 stub_type, location);
7585 else
7586 {
7587 stub = gogo->start_function(name, stub_type, false,
7588 fntype->location());
7589 Type::build_one_stub_method(gogo, m, buf, stub_params,
7590 fntype->is_varargs(), location);
7591 gogo->finish_function(fntype->location());
7592 }
7593
7594 m->set_stub_object(stub);
7595 }
7596 }
7597
7598 // Build a stub method which adjusts the receiver as required to call
7599 // METHOD. RECEIVER_NAME is the name we used for the receiver.
7600 // PARAMS is the list of function parameters.
7601
7602 void
7603 Type::build_one_stub_method(Gogo* gogo, Method* method,
7604 const char* receiver_name,
7605 const Typed_identifier_list* params,
7606 bool is_varargs,
7607 source_location location)
7608 {
7609 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
7610 gcc_assert(receiver_object != NULL);
7611
7612 Expression* expr = Expression::make_var_reference(receiver_object, location);
7613 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
7614 if (expr->type()->points_to() == NULL)
7615 expr = Expression::make_unary(OPERATOR_AND, expr, location);
7616
7617 Expression_list* arguments;
7618 if (params == NULL || params->empty())
7619 arguments = NULL;
7620 else
7621 {
7622 arguments = new Expression_list();
7623 for (Typed_identifier_list::const_iterator p = params->begin();
7624 p != params->end();
7625 ++p)
7626 {
7627 Named_object* param = gogo->lookup(p->name(), NULL);
7628 gcc_assert(param != NULL);
7629 Expression* param_ref = Expression::make_var_reference(param,
7630 location);
7631 arguments->push_back(param_ref);
7632 }
7633 }
7634
7635 Expression* func = method->bind_method(expr, location);
7636 gcc_assert(func != NULL);
7637 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
7638 location);
7639 size_t count = call->result_count();
7640 if (count == 0)
7641 gogo->add_statement(Statement::make_statement(call));
7642 else
7643 {
7644 Expression_list* retvals = new Expression_list();
7645 if (count <= 1)
7646 retvals->push_back(call);
7647 else
7648 {
7649 for (size_t i = 0; i < count; ++i)
7650 retvals->push_back(Expression::make_call_result(call, i));
7651 }
7652 const Function* function = gogo->current_function()->func_value();
7653 const Typed_identifier_list* results = function->type()->results();
7654 Statement* retstat = Statement::make_return_statement(results, retvals,
7655 location);
7656 gogo->add_statement(retstat);
7657 }
7658 }
7659
7660 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
7661 // in reverse order.
7662
7663 Expression*
7664 Type::apply_field_indexes(Expression* expr,
7665 const Method::Field_indexes* field_indexes,
7666 source_location location)
7667 {
7668 if (field_indexes == NULL)
7669 return expr;
7670 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
7671 Struct_type* stype = expr->type()->deref()->struct_type();
7672 gcc_assert(stype != NULL
7673 && field_indexes->field_index < stype->field_count());
7674 if (expr->type()->struct_type() == NULL)
7675 {
7676 gcc_assert(expr->type()->points_to() != NULL);
7677 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
7678 gcc_assert(expr->type()->struct_type() == stype);
7679 }
7680 return Expression::make_field_reference(expr, field_indexes->field_index,
7681 location);
7682 }
7683
7684 // Return whether NO is a method for which the receiver is a pointer.
7685
7686 bool
7687 Type::method_expects_pointer(const Named_object* no)
7688 {
7689 const Function_type *fntype;
7690 if (no->is_function())
7691 fntype = no->func_value()->type();
7692 else if (no->is_function_declaration())
7693 fntype = no->func_declaration_value()->type();
7694 else
7695 gcc_unreachable();
7696 return fntype->receiver()->type()->points_to() != NULL;
7697 }
7698
7699 // Given a set of methods for a type, METHODS, return the method NAME,
7700 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
7701 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
7702 // but is ambiguous (and return NULL).
7703
7704 Method*
7705 Type::method_function(const Methods* methods, const std::string& name,
7706 bool* is_ambiguous)
7707 {
7708 if (is_ambiguous != NULL)
7709 *is_ambiguous = false;
7710 if (methods == NULL)
7711 return NULL;
7712 Methods::const_iterator p = methods->find(name);
7713 if (p == methods->end())
7714 return NULL;
7715 Method* m = p->second;
7716 if (m->is_ambiguous())
7717 {
7718 if (is_ambiguous != NULL)
7719 *is_ambiguous = true;
7720 return NULL;
7721 }
7722 return m;
7723 }
7724
7725 // Look for field or method NAME for TYPE. Return an Expression for
7726 // the field or method bound to EXPR. If there is no such field or
7727 // method, give an appropriate error and return an error expression.
7728
7729 Expression*
7730 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
7731 const std::string& name,
7732 source_location location)
7733 {
7734 if (type->deref()->is_error_type())
7735 return Expression::make_error(location);
7736
7737 const Named_type* nt = type->named_type();
7738 if (nt == NULL)
7739 nt = type->deref()->named_type();
7740 const Struct_type* st = type->deref()->struct_type();
7741 const Interface_type* it = type->deref()->interface_type();
7742
7743 // If this is a pointer to a pointer, then it is possible that the
7744 // pointed-to type has methods.
7745 if (nt == NULL
7746 && st == NULL
7747 && it == NULL
7748 && type->points_to() != NULL
7749 && type->points_to()->points_to() != NULL)
7750 {
7751 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
7752 type = type->points_to();
7753 nt = type->points_to()->named_type();
7754 st = type->points_to()->struct_type();
7755 it = type->points_to()->interface_type();
7756 }
7757
7758 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
7759 || expr->is_addressable());
7760 std::vector<const Named_type*> seen;
7761 bool is_method = false;
7762 bool found_pointer_method = false;
7763 std::string ambig1;
7764 std::string ambig2;
7765 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
7766 &seen, NULL, &is_method,
7767 &found_pointer_method, &ambig1, &ambig2))
7768 {
7769 Expression* ret;
7770 if (!is_method)
7771 {
7772 gcc_assert(st != NULL);
7773 if (type->struct_type() == NULL)
7774 {
7775 gcc_assert(type->points_to() != NULL);
7776 expr = Expression::make_unary(OPERATOR_MULT, expr,
7777 location);
7778 gcc_assert(expr->type()->struct_type() == st);
7779 }
7780 ret = st->field_reference(expr, name, location);
7781 }
7782 else if (it != NULL && it->find_method(name) != NULL)
7783 ret = Expression::make_interface_field_reference(expr, name,
7784 location);
7785 else
7786 {
7787 Method* m;
7788 if (nt != NULL)
7789 m = nt->method_function(name, NULL);
7790 else if (st != NULL)
7791 m = st->method_function(name, NULL);
7792 else
7793 gcc_unreachable();
7794 gcc_assert(m != NULL);
7795 if (!m->is_value_method() && expr->type()->points_to() == NULL)
7796 expr = Expression::make_unary(OPERATOR_AND, expr, location);
7797 ret = m->bind_method(expr, location);
7798 }
7799 gcc_assert(ret != NULL);
7800 return ret;
7801 }
7802 else
7803 {
7804 if (!ambig1.empty())
7805 error_at(location, "%qs is ambiguous via %qs and %qs",
7806 Gogo::message_name(name).c_str(),
7807 Gogo::message_name(ambig1).c_str(),
7808 Gogo::message_name(ambig2).c_str());
7809 else if (found_pointer_method)
7810 error_at(location, "method requires a pointer");
7811 else if (nt == NULL && st == NULL && it == NULL)
7812 error_at(location,
7813 ("reference to field %qs in object which "
7814 "has no fields or methods"),
7815 Gogo::message_name(name).c_str());
7816 else
7817 {
7818 bool is_unexported;
7819 if (!Gogo::is_hidden_name(name))
7820 is_unexported = false;
7821 else
7822 {
7823 std::string unpacked = Gogo::unpack_hidden_name(name);
7824 seen.clear();
7825 is_unexported = Type::is_unexported_field_or_method(gogo, type,
7826 unpacked,
7827 &seen);
7828 }
7829 if (is_unexported)
7830 error_at(location, "reference to unexported field or method %qs",
7831 Gogo::message_name(name).c_str());
7832 else
7833 error_at(location, "reference to undefined field or method %qs",
7834 Gogo::message_name(name).c_str());
7835 }
7836 return Expression::make_error(location);
7837 }
7838 }
7839
7840 // Look in TYPE for a field or method named NAME, return true if one
7841 // is found. This looks through embedded anonymous fields and handles
7842 // ambiguity. If a method is found, sets *IS_METHOD to true;
7843 // otherwise, if a field is found, set it to false. If
7844 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
7845 // whose address can not be taken. SEEN is used to avoid infinite
7846 // recursion on invalid types.
7847
7848 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
7849 // method we couldn't use because it requires a pointer. LEVEL is
7850 // used for recursive calls, and can be NULL for a non-recursive call.
7851 // When this function returns false because it finds that the name is
7852 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
7853 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
7854 // will be unchanged.
7855
7856 // This function just returns whether or not there is a field or
7857 // method, and whether it is a field or method. It doesn't build an
7858 // expression to refer to it. If it is a method, we then look in the
7859 // list of all methods for the type. If it is a field, the search has
7860 // to be done again, looking only for fields, and building up the
7861 // expression as we go.
7862
7863 bool
7864 Type::find_field_or_method(const Type* type,
7865 const std::string& name,
7866 bool receiver_can_be_pointer,
7867 std::vector<const Named_type*>* seen,
7868 int* level,
7869 bool* is_method,
7870 bool* found_pointer_method,
7871 std::string* ambig1,
7872 std::string* ambig2)
7873 {
7874 // Named types can have locally defined methods.
7875 const Named_type* nt = type->named_type();
7876 if (nt == NULL && type->points_to() != NULL)
7877 nt = type->points_to()->named_type();
7878 if (nt != NULL)
7879 {
7880 Named_object* no = nt->find_local_method(name);
7881 if (no != NULL)
7882 {
7883 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
7884 {
7885 *is_method = true;
7886 return true;
7887 }
7888
7889 // Record that we have found a pointer method in order to
7890 // give a better error message if we don't find anything
7891 // else.
7892 *found_pointer_method = true;
7893 }
7894
7895 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
7896 p != seen->end();
7897 ++p)
7898 {
7899 if (*p == nt)
7900 {
7901 // We've already seen this type when searching for methods.
7902 return false;
7903 }
7904 }
7905 }
7906
7907 // Interface types can have methods.
7908 const Interface_type* it = type->deref()->interface_type();
7909 if (it != NULL && it->find_method(name) != NULL)
7910 {
7911 *is_method = true;
7912 return true;
7913 }
7914
7915 // Struct types can have fields. They can also inherit fields and
7916 // methods from anonymous fields.
7917 const Struct_type* st = type->deref()->struct_type();
7918 if (st == NULL)
7919 return false;
7920 const Struct_field_list* fields = st->fields();
7921 if (fields == NULL)
7922 return false;
7923
7924 if (nt != NULL)
7925 seen->push_back(nt);
7926
7927 int found_level = 0;
7928 bool found_is_method = false;
7929 std::string found_ambig1;
7930 std::string found_ambig2;
7931 const Struct_field* found_parent = NULL;
7932 for (Struct_field_list::const_iterator pf = fields->begin();
7933 pf != fields->end();
7934 ++pf)
7935 {
7936 if (pf->field_name() == name)
7937 {
7938 *is_method = false;
7939 if (nt != NULL)
7940 seen->pop_back();
7941 return true;
7942 }
7943
7944 if (!pf->is_anonymous())
7945 continue;
7946
7947 if (pf->type()->deref()->is_error_type()
7948 || pf->type()->deref()->is_undefined())
7949 continue;
7950
7951 Named_type* fnt = pf->type()->named_type();
7952 if (fnt == NULL)
7953 fnt = pf->type()->deref()->named_type();
7954 gcc_assert(fnt != NULL);
7955
7956 int sublevel = level == NULL ? 1 : *level + 1;
7957 bool sub_is_method;
7958 std::string subambig1;
7959 std::string subambig2;
7960 bool subfound = Type::find_field_or_method(fnt,
7961 name,
7962 receiver_can_be_pointer,
7963 seen,
7964 &sublevel,
7965 &sub_is_method,
7966 found_pointer_method,
7967 &subambig1,
7968 &subambig2);
7969 if (!subfound)
7970 {
7971 if (!subambig1.empty())
7972 {
7973 // The name was found via this field, but is ambiguous.
7974 // if the ambiguity is lower or at the same level as
7975 // anything else we have already found, then we want to
7976 // pass the ambiguity back to the caller.
7977 if (found_level == 0 || sublevel <= found_level)
7978 {
7979 found_ambig1 = pf->field_name() + '.' + subambig1;
7980 found_ambig2 = pf->field_name() + '.' + subambig2;
7981 found_level = sublevel;
7982 }
7983 }
7984 }
7985 else
7986 {
7987 // The name was found via this field. Use the level to see
7988 // if we want to use this one, or whether it introduces an
7989 // ambiguity.
7990 if (found_level == 0 || sublevel < found_level)
7991 {
7992 found_level = sublevel;
7993 found_is_method = sub_is_method;
7994 found_ambig1.clear();
7995 found_ambig2.clear();
7996 found_parent = &*pf;
7997 }
7998 else if (sublevel > found_level)
7999 ;
8000 else if (found_ambig1.empty())
8001 {
8002 // We found an ambiguity.
8003 gcc_assert(found_parent != NULL);
8004 found_ambig1 = found_parent->field_name();
8005 found_ambig2 = pf->field_name();
8006 }
8007 else
8008 {
8009 // We found an ambiguity, but we already know of one.
8010 // Just report the earlier one.
8011 }
8012 }
8013 }
8014
8015 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
8016 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
8017 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
8018 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
8019
8020 if (nt != NULL)
8021 seen->pop_back();
8022
8023 if (found_level == 0)
8024 return false;
8025 else if (!found_ambig1.empty())
8026 {
8027 gcc_assert(!found_ambig1.empty());
8028 ambig1->assign(found_ambig1);
8029 ambig2->assign(found_ambig2);
8030 if (level != NULL)
8031 *level = found_level;
8032 return false;
8033 }
8034 else
8035 {
8036 if (level != NULL)
8037 *level = found_level;
8038 *is_method = found_is_method;
8039 return true;
8040 }
8041 }
8042
8043 // Return whether NAME is an unexported field or method for TYPE.
8044
8045 bool
8046 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
8047 const std::string& name,
8048 std::vector<const Named_type*>* seen)
8049 {
8050 type = type->deref();
8051
8052 const Named_type* nt = type->named_type();
8053 if (nt != NULL)
8054 {
8055 if (nt->is_unexported_local_method(gogo, name))
8056 return true;
8057
8058 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
8059 p != seen->end();
8060 ++p)
8061 {
8062 if (*p == nt)
8063 {
8064 // We've already seen this type.
8065 return false;
8066 }
8067 }
8068 }
8069
8070 const Interface_type* it = type->interface_type();
8071 if (it != NULL && it->is_unexported_method(gogo, name))
8072 return true;
8073
8074 const Struct_type* st = type->struct_type();
8075 if (st != NULL && st->is_unexported_local_field(gogo, name))
8076 return true;
8077
8078 if (st == NULL)
8079 return false;
8080
8081 const Struct_field_list* fields = st->fields();
8082 if (fields == NULL)
8083 return false;
8084
8085 if (nt != NULL)
8086 seen->push_back(nt);
8087
8088 for (Struct_field_list::const_iterator pf = fields->begin();
8089 pf != fields->end();
8090 ++pf)
8091 {
8092 if (pf->is_anonymous()
8093 && (!pf->type()->deref()->is_error_type()
8094 && !pf->type()->deref()->is_undefined()))
8095 {
8096 Named_type* subtype = pf->type()->deref()->named_type();
8097 gcc_assert(subtype != NULL);
8098 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
8099 {
8100 if (nt != NULL)
8101 seen->pop_back();
8102 return true;
8103 }
8104 }
8105 }
8106
8107 if (nt != NULL)
8108 seen->pop_back();
8109
8110 return false;
8111 }
8112
8113 // Class Forward_declaration.
8114
8115 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
8116 : Type(TYPE_FORWARD),
8117 named_object_(named_object->resolve()), warned_(false)
8118 {
8119 gcc_assert(this->named_object_->is_unknown()
8120 || this->named_object_->is_type_declaration());
8121 }
8122
8123 // Return the named object.
8124
8125 Named_object*
8126 Forward_declaration_type::named_object()
8127 {
8128 return this->named_object_->resolve();
8129 }
8130
8131 const Named_object*
8132 Forward_declaration_type::named_object() const
8133 {
8134 return this->named_object_->resolve();
8135 }
8136
8137 // Return the name of the forward declared type.
8138
8139 const std::string&
8140 Forward_declaration_type::name() const
8141 {
8142 return this->named_object()->name();
8143 }
8144
8145 // Warn about a use of a type which has been declared but not defined.
8146
8147 void
8148 Forward_declaration_type::warn() const
8149 {
8150 Named_object* no = this->named_object_->resolve();
8151 if (no->is_unknown())
8152 {
8153 // The name was not defined anywhere.
8154 if (!this->warned_)
8155 {
8156 error_at(this->named_object_->location(),
8157 "use of undefined type %qs",
8158 no->message_name().c_str());
8159 this->warned_ = true;
8160 }
8161 }
8162 else if (no->is_type_declaration())
8163 {
8164 // The name was seen as a type, but the type was never defined.
8165 if (no->type_declaration_value()->using_type())
8166 {
8167 error_at(this->named_object_->location(),
8168 "use of undefined type %qs",
8169 no->message_name().c_str());
8170 this->warned_ = true;
8171 }
8172 }
8173 else
8174 {
8175 // The name was defined, but not as a type.
8176 if (!this->warned_)
8177 {
8178 error_at(this->named_object_->location(), "expected type");
8179 this->warned_ = true;
8180 }
8181 }
8182 }
8183
8184 // Get the base type of a declaration. This gives an error if the
8185 // type has not yet been defined.
8186
8187 Type*
8188 Forward_declaration_type::real_type()
8189 {
8190 if (this->is_defined())
8191 return this->named_object()->type_value();
8192 else
8193 {
8194 this->warn();
8195 return Type::make_error_type();
8196 }
8197 }
8198
8199 const Type*
8200 Forward_declaration_type::real_type() const
8201 {
8202 if (this->is_defined())
8203 return this->named_object()->type_value();
8204 else
8205 {
8206 this->warn();
8207 return Type::make_error_type();
8208 }
8209 }
8210
8211 // Return whether the base type is defined.
8212
8213 bool
8214 Forward_declaration_type::is_defined() const
8215 {
8216 return this->named_object()->is_type();
8217 }
8218
8219 // Add a method. This is used when methods are defined before the
8220 // type.
8221
8222 Named_object*
8223 Forward_declaration_type::add_method(const std::string& name,
8224 Function* function)
8225 {
8226 Named_object* no = this->named_object();
8227 if (no->is_unknown())
8228 no->declare_as_type();
8229 return no->type_declaration_value()->add_method(name, function);
8230 }
8231
8232 // Add a method declaration. This is used when methods are declared
8233 // before the type.
8234
8235 Named_object*
8236 Forward_declaration_type::add_method_declaration(const std::string& name,
8237 Function_type* type,
8238 source_location location)
8239 {
8240 Named_object* no = this->named_object();
8241 if (no->is_unknown())
8242 no->declare_as_type();
8243 Type_declaration* td = no->type_declaration_value();
8244 return td->add_method_declaration(name, type, location);
8245 }
8246
8247 // Traversal.
8248
8249 int
8250 Forward_declaration_type::do_traverse(Traverse* traverse)
8251 {
8252 if (this->is_defined()
8253 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
8254 return TRAVERSE_EXIT;
8255 return TRAVERSE_CONTINUE;
8256 }
8257
8258 // Get a tree for the type.
8259
8260 tree
8261 Forward_declaration_type::do_get_tree(Gogo* gogo)
8262 {
8263 if (this->is_defined())
8264 return Type::get_named_type_tree(gogo, this->real_type());
8265
8266 if (this->warned_)
8267 return error_mark_node;
8268
8269 // We represent an undefined type as a struct with no fields. That
8270 // should work fine for the middle-end, since the same case can
8271 // arise in C.
8272 Named_object* no = this->named_object();
8273 tree type_tree = make_node(RECORD_TYPE);
8274 tree id = no->get_id(gogo);
8275 tree decl = build_decl(no->location(), TYPE_DECL, id, type_tree);
8276 TYPE_NAME(type_tree) = decl;
8277 layout_type(type_tree);
8278 return type_tree;
8279 }
8280
8281 // Build a type descriptor for a forwarded type.
8282
8283 Expression*
8284 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8285 {
8286 if (!this->is_defined())
8287 return Expression::make_nil(BUILTINS_LOCATION);
8288 else
8289 {
8290 Type* t = this->real_type();
8291 if (name != NULL)
8292 return this->named_type_descriptor(gogo, t, name);
8293 else
8294 return Expression::make_type_descriptor(t, BUILTINS_LOCATION);
8295 }
8296 }
8297
8298 // The reflection string.
8299
8300 void
8301 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
8302 {
8303 this->append_reflection(this->real_type(), gogo, ret);
8304 }
8305
8306 // The mangled name.
8307
8308 void
8309 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
8310 {
8311 if (this->is_defined())
8312 this->append_mangled_name(this->real_type(), gogo, ret);
8313 else
8314 {
8315 const Named_object* no = this->named_object();
8316 std::string name;
8317 if (no->package() == NULL)
8318 name = gogo->package_name();
8319 else
8320 name = no->package()->name();
8321 name += '.';
8322 name += Gogo::unpack_hidden_name(no->name());
8323 char buf[20];
8324 snprintf(buf, sizeof buf, "N%u_",
8325 static_cast<unsigned int>(name.length()));
8326 ret->append(buf);
8327 ret->append(name);
8328 }
8329 }
8330
8331 // Export a forward declaration. This can happen when a defined type
8332 // refers to a type which is only declared (and is presumably defined
8333 // in some other file in the same package).
8334
8335 void
8336 Forward_declaration_type::do_export(Export*) const
8337 {
8338 // If there is a base type, that should be exported instead of this.
8339 gcc_assert(!this->is_defined());
8340
8341 // We don't output anything.
8342 }
8343
8344 // Make a forward declaration.
8345
8346 Type*
8347 Type::make_forward_declaration(Named_object* named_object)
8348 {
8349 return new Forward_declaration_type(named_object);
8350 }
8351
8352 // Class Typed_identifier_list.
8353
8354 // Sort the entries by name.
8355
8356 struct Typed_identifier_list_sort
8357 {
8358 public:
8359 bool
8360 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
8361 { return t1.name() < t2.name(); }
8362 };
8363
8364 void
8365 Typed_identifier_list::sort_by_name()
8366 {
8367 std::sort(this->entries_.begin(), this->entries_.end(),
8368 Typed_identifier_list_sort());
8369 }
8370
8371 // Traverse types.
8372
8373 int
8374 Typed_identifier_list::traverse(Traverse* traverse)
8375 {
8376 for (Typed_identifier_list::const_iterator p = this->begin();
8377 p != this->end();
8378 ++p)
8379 {
8380 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
8381 return TRAVERSE_EXIT;
8382 }
8383 return TRAVERSE_CONTINUE;
8384 }
8385
8386 // Copy the list.
8387
8388 Typed_identifier_list*
8389 Typed_identifier_list::copy() const
8390 {
8391 Typed_identifier_list* ret = new Typed_identifier_list();
8392 for (Typed_identifier_list::const_iterator p = this->begin();
8393 p != this->end();
8394 ++p)
8395 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));
8396 return ret;
8397 }