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