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