Fix order in which recursive structs are converted to GENERIC.
[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 // Make sure that all structs which must be converted to the backend
3772 // representation before this one are in fact converted.
3773
3774 void
3775 Struct_type::convert_prerequisites(Gogo* gogo)
3776 {
3777 for (std::vector<Named_type*>::const_iterator p
3778 = this->prerequisites_.begin();
3779 p != this->prerequisites_.end();
3780 ++p)
3781 (*p)->get_tree(gogo);
3782 }
3783
3784 // Initialize struct fields.
3785
3786 tree
3787 Struct_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
3788 {
3789 if (this->fields_ == NULL || this->fields_->empty())
3790 {
3791 if (is_clear)
3792 return NULL;
3793 else
3794 {
3795 tree ret = build_constructor(type_tree,
3796 VEC_alloc(constructor_elt, gc, 0));
3797 TREE_CONSTANT(ret) = 1;
3798 return ret;
3799 }
3800 }
3801
3802 bool is_constant = true;
3803 bool any_fields_set = false;
3804 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc,
3805 this->fields_->size());
3806
3807 tree field = TYPE_FIELDS(type_tree);
3808 for (Struct_field_list::const_iterator p = this->fields_->begin();
3809 p != this->fields_->end();
3810 ++p, field = DECL_CHAIN(field))
3811 {
3812 tree value = p->type()->get_init_tree(gogo, is_clear);
3813 if (value == error_mark_node)
3814 return error_mark_node;
3815 gcc_assert(field != NULL_TREE);
3816 if (value != NULL)
3817 {
3818 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
3819 elt->index = field;
3820 elt->value = value;
3821 any_fields_set = true;
3822 if (!TREE_CONSTANT(value))
3823 is_constant = false;
3824 }
3825 }
3826 gcc_assert(field == NULL_TREE);
3827
3828 if (!any_fields_set)
3829 {
3830 gcc_assert(is_clear);
3831 VEC_free(constructor_elt, gc, init);
3832 return NULL;
3833 }
3834
3835 tree ret = build_constructor(type_tree, init);
3836 if (is_constant)
3837 TREE_CONSTANT(ret) = 1;
3838 return ret;
3839 }
3840
3841 // The type of a struct type descriptor.
3842
3843 Type*
3844 Struct_type::make_struct_type_descriptor_type()
3845 {
3846 static Type* ret;
3847 if (ret == NULL)
3848 {
3849 Type* tdt = Type::make_type_descriptor_type();
3850 Type* ptdt = Type::make_type_descriptor_ptr_type();
3851
3852 Type* uintptr_type = Type::lookup_integer_type("uintptr");
3853 Type* string_type = Type::lookup_string_type();
3854 Type* pointer_string_type = Type::make_pointer_type(string_type);
3855
3856 Struct_type* sf =
3857 Type::make_builtin_struct_type(5,
3858 "name", pointer_string_type,
3859 "pkgPath", pointer_string_type,
3860 "typ", ptdt,
3861 "tag", pointer_string_type,
3862 "offset", uintptr_type);
3863 Type* nsf = Type::make_builtin_named_type("structField", sf);
3864
3865 Type* slice_type = Type::make_array_type(nsf, NULL);
3866
3867 Struct_type* s = Type::make_builtin_struct_type(2,
3868 "", tdt,
3869 "fields", slice_type);
3870
3871 ret = Type::make_builtin_named_type("StructType", s);
3872 }
3873
3874 return ret;
3875 }
3876
3877 // Build a type descriptor for a struct type.
3878
3879 Expression*
3880 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3881 {
3882 source_location bloc = BUILTINS_LOCATION;
3883
3884 Type* stdt = Struct_type::make_struct_type_descriptor_type();
3885
3886 const Struct_field_list* fields = stdt->struct_type()->fields();
3887
3888 Expression_list* vals = new Expression_list();
3889 vals->reserve(2);
3890
3891 const Methods* methods = this->methods();
3892 // A named struct should not have methods--the methods should attach
3893 // to the named type.
3894 gcc_assert(methods == NULL || name == NULL);
3895
3896 Struct_field_list::const_iterator ps = fields->begin();
3897 gcc_assert(ps->field_name() == "commonType");
3898 vals->push_back(this->type_descriptor_constructor(gogo,
3899 RUNTIME_TYPE_KIND_STRUCT,
3900 name, methods, true));
3901
3902 ++ps;
3903 gcc_assert(ps->field_name() == "fields");
3904
3905 Expression_list* elements = new Expression_list();
3906 elements->reserve(this->fields_->size());
3907 Type* element_type = ps->type()->array_type()->element_type();
3908 for (Struct_field_list::const_iterator pf = this->fields_->begin();
3909 pf != this->fields_->end();
3910 ++pf)
3911 {
3912 const Struct_field_list* f = element_type->struct_type()->fields();
3913
3914 Expression_list* fvals = new Expression_list();
3915 fvals->reserve(5);
3916
3917 Struct_field_list::const_iterator q = f->begin();
3918 gcc_assert(q->field_name() == "name");
3919 if (pf->is_anonymous())
3920 fvals->push_back(Expression::make_nil(bloc));
3921 else
3922 {
3923 std::string n = Gogo::unpack_hidden_name(pf->field_name());
3924 Expression* s = Expression::make_string(n, bloc);
3925 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3926 }
3927
3928 ++q;
3929 gcc_assert(q->field_name() == "pkgPath");
3930 if (!Gogo::is_hidden_name(pf->field_name()))
3931 fvals->push_back(Expression::make_nil(bloc));
3932 else
3933 {
3934 std::string n = Gogo::hidden_name_prefix(pf->field_name());
3935 Expression* s = Expression::make_string(n, bloc);
3936 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3937 }
3938
3939 ++q;
3940 gcc_assert(q->field_name() == "typ");
3941 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
3942
3943 ++q;
3944 gcc_assert(q->field_name() == "tag");
3945 if (!pf->has_tag())
3946 fvals->push_back(Expression::make_nil(bloc));
3947 else
3948 {
3949 Expression* s = Expression::make_string(pf->tag(), bloc);
3950 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3951 }
3952
3953 ++q;
3954 gcc_assert(q->field_name() == "offset");
3955 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
3956
3957 Expression* v = Expression::make_struct_composite_literal(element_type,
3958 fvals, bloc);
3959 elements->push_back(v);
3960 }
3961
3962 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
3963 elements, bloc));
3964
3965 return Expression::make_struct_composite_literal(stdt, vals, bloc);
3966 }
3967
3968 // Reflection string.
3969
3970 void
3971 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
3972 {
3973 ret->append("struct { ");
3974
3975 for (Struct_field_list::const_iterator p = this->fields_->begin();
3976 p != this->fields_->end();
3977 ++p)
3978 {
3979 if (p != this->fields_->begin())
3980 ret->append("; ");
3981 if (p->is_anonymous())
3982 ret->push_back('?');
3983 else
3984 ret->append(Gogo::unpack_hidden_name(p->field_name()));
3985 ret->push_back(' ');
3986 this->append_reflection(p->type(), gogo, ret);
3987
3988 if (p->has_tag())
3989 {
3990 const std::string& tag(p->tag());
3991 ret->append(" \"");
3992 for (std::string::const_iterator p = tag.begin();
3993 p != tag.end();
3994 ++p)
3995 {
3996 if (*p == '\0')
3997 ret->append("\\x00");
3998 else if (*p == '\n')
3999 ret->append("\\n");
4000 else if (*p == '\t')
4001 ret->append("\\t");
4002 else if (*p == '"')
4003 ret->append("\\\"");
4004 else if (*p == '\\')
4005 ret->append("\\\\");
4006 else
4007 ret->push_back(*p);
4008 }
4009 ret->push_back('"');
4010 }
4011 }
4012
4013 ret->append(" }");
4014 }
4015
4016 // Mangled name.
4017
4018 void
4019 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4020 {
4021 ret->push_back('S');
4022
4023 const Struct_field_list* fields = this->fields_;
4024 if (fields != NULL)
4025 {
4026 for (Struct_field_list::const_iterator p = fields->begin();
4027 p != fields->end();
4028 ++p)
4029 {
4030 if (p->is_anonymous())
4031 ret->append("0_");
4032 else
4033 {
4034 std::string n = Gogo::unpack_hidden_name(p->field_name());
4035 char buf[20];
4036 snprintf(buf, sizeof buf, "%u_",
4037 static_cast<unsigned int>(n.length()));
4038 ret->append(buf);
4039 ret->append(n);
4040 }
4041 this->append_mangled_name(p->type(), gogo, ret);
4042 if (p->has_tag())
4043 {
4044 const std::string& tag(p->tag());
4045 std::string out;
4046 for (std::string::const_iterator p = tag.begin();
4047 p != tag.end();
4048 ++p)
4049 {
4050 if (ISALNUM(*p) || *p == '_')
4051 out.push_back(*p);
4052 else
4053 {
4054 char buf[20];
4055 snprintf(buf, sizeof buf, ".%x.",
4056 static_cast<unsigned int>(*p));
4057 out.append(buf);
4058 }
4059 }
4060 char buf[20];
4061 snprintf(buf, sizeof buf, "T%u_",
4062 static_cast<unsigned int>(out.length()));
4063 ret->append(buf);
4064 ret->append(out);
4065 }
4066 }
4067 }
4068
4069 ret->push_back('e');
4070 }
4071
4072 // Export.
4073
4074 void
4075 Struct_type::do_export(Export* exp) const
4076 {
4077 exp->write_c_string("struct { ");
4078 const Struct_field_list* fields = this->fields_;
4079 gcc_assert(fields != NULL);
4080 for (Struct_field_list::const_iterator p = fields->begin();
4081 p != fields->end();
4082 ++p)
4083 {
4084 if (p->is_anonymous())
4085 exp->write_string("? ");
4086 else
4087 {
4088 exp->write_string(p->field_name());
4089 exp->write_c_string(" ");
4090 }
4091 exp->write_type(p->type());
4092
4093 if (p->has_tag())
4094 {
4095 exp->write_c_string(" ");
4096 Expression* expr = Expression::make_string(p->tag(),
4097 BUILTINS_LOCATION);
4098 expr->export_expression(exp);
4099 delete expr;
4100 }
4101
4102 exp->write_c_string("; ");
4103 }
4104 exp->write_c_string("}");
4105 }
4106
4107 // Import.
4108
4109 Struct_type*
4110 Struct_type::do_import(Import* imp)
4111 {
4112 imp->require_c_string("struct { ");
4113 Struct_field_list* fields = new Struct_field_list;
4114 if (imp->peek_char() != '}')
4115 {
4116 while (true)
4117 {
4118 std::string name;
4119 if (imp->match_c_string("? "))
4120 imp->advance(2);
4121 else
4122 {
4123 name = imp->read_identifier();
4124 imp->require_c_string(" ");
4125 }
4126 Type* ftype = imp->read_type();
4127
4128 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
4129
4130 if (imp->peek_char() == ' ')
4131 {
4132 imp->advance(1);
4133 Expression* expr = Expression::import_expression(imp);
4134 String_expression* sexpr = expr->string_expression();
4135 gcc_assert(sexpr != NULL);
4136 sf.set_tag(sexpr->val());
4137 delete sexpr;
4138 }
4139
4140 imp->require_c_string("; ");
4141 fields->push_back(sf);
4142 if (imp->peek_char() == '}')
4143 break;
4144 }
4145 }
4146 imp->require_c_string("}");
4147
4148 return Type::make_struct_type(fields, imp->location());
4149 }
4150
4151 // Make a struct type.
4152
4153 Struct_type*
4154 Type::make_struct_type(Struct_field_list* fields,
4155 source_location location)
4156 {
4157 return new Struct_type(fields, location);
4158 }
4159
4160 // Class Array_type.
4161
4162 // Whether two array types are identical.
4163
4164 bool
4165 Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
4166 {
4167 if (!Type::are_identical(this->element_type(), t->element_type(),
4168 errors_are_identical, NULL))
4169 return false;
4170
4171 Expression* l1 = this->length();
4172 Expression* l2 = t->length();
4173
4174 // Slices of the same element type are identical.
4175 if (l1 == NULL && l2 == NULL)
4176 return true;
4177
4178 // Arrays of the same element type are identical if they have the
4179 // same length.
4180 if (l1 != NULL && l2 != NULL)
4181 {
4182 if (l1 == l2)
4183 return true;
4184
4185 // Try to determine the lengths. If we can't, assume the arrays
4186 // are not identical.
4187 bool ret = false;
4188 mpz_t v1;
4189 mpz_init(v1);
4190 Type* type1;
4191 mpz_t v2;
4192 mpz_init(v2);
4193 Type* type2;
4194 if (l1->integer_constant_value(true, v1, &type1)
4195 && l2->integer_constant_value(true, v2, &type2))
4196 ret = mpz_cmp(v1, v2) == 0;
4197 mpz_clear(v1);
4198 mpz_clear(v2);
4199 return ret;
4200 }
4201
4202 // Otherwise the arrays are not identical.
4203 return false;
4204 }
4205
4206 // Traversal.
4207
4208 int
4209 Array_type::do_traverse(Traverse* traverse)
4210 {
4211 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
4212 return TRAVERSE_EXIT;
4213 if (this->length_ != NULL
4214 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
4215 return TRAVERSE_EXIT;
4216 return TRAVERSE_CONTINUE;
4217 }
4218
4219 // Check that the length is valid.
4220
4221 bool
4222 Array_type::verify_length()
4223 {
4224 if (this->length_ == NULL)
4225 return true;
4226 if (!this->length_->is_constant())
4227 {
4228 error_at(this->length_->location(), "array bound is not constant");
4229 return false;
4230 }
4231
4232 mpz_t val;
4233
4234 Type* t = this->length_->type();
4235 if (t->integer_type() != NULL)
4236 {
4237 Type* vt;
4238 mpz_init(val);
4239 if (!this->length_->integer_constant_value(true, val, &vt))
4240 {
4241 error_at(this->length_->location(),
4242 "array bound is not constant");
4243 mpz_clear(val);
4244 return false;
4245 }
4246 }
4247 else if (t->float_type() != NULL)
4248 {
4249 Type* vt;
4250 mpfr_t fval;
4251 mpfr_init(fval);
4252 if (!this->length_->float_constant_value(fval, &vt))
4253 {
4254 error_at(this->length_->location(),
4255 "array bound is not constant");
4256 mpfr_clear(fval);
4257 return false;
4258 }
4259 if (!mpfr_integer_p(fval))
4260 {
4261 error_at(this->length_->location(),
4262 "array bound truncated to integer");
4263 mpfr_clear(fval);
4264 return false;
4265 }
4266 mpz_init(val);
4267 mpfr_get_z(val, fval, GMP_RNDN);
4268 mpfr_clear(fval);
4269 }
4270 else
4271 {
4272 if (!t->is_error_type())
4273 error_at(this->length_->location(), "array bound is not numeric");
4274 return false;
4275 }
4276
4277 if (mpz_sgn(val) < 0)
4278 {
4279 error_at(this->length_->location(), "negative array bound");
4280 mpz_clear(val);
4281 return false;
4282 }
4283
4284 Type* int_type = Type::lookup_integer_type("int");
4285 int tbits = int_type->integer_type()->bits();
4286 int vbits = mpz_sizeinbase(val, 2);
4287 if (vbits + 1 > tbits)
4288 {
4289 error_at(this->length_->location(), "array bound overflows");
4290 mpz_clear(val);
4291 return false;
4292 }
4293
4294 mpz_clear(val);
4295
4296 return true;
4297 }
4298
4299 // Verify the type.
4300
4301 bool
4302 Array_type::do_verify()
4303 {
4304 if (!this->verify_length())
4305 {
4306 this->length_ = Expression::make_error(this->length_->location());
4307 return false;
4308 }
4309 return true;
4310 }
4311
4312 // Array type hash code.
4313
4314 unsigned int
4315 Array_type::do_hash_for_method(Gogo* gogo) const
4316 {
4317 // There is no very convenient way to get a hash code for the
4318 // length.
4319 return this->element_type_->hash_for_method(gogo) + 1;
4320 }
4321
4322 // See if the expression passed to make is suitable. The first
4323 // argument is required, and gives the length. An optional second
4324 // argument is permitted for the capacity.
4325
4326 bool
4327 Array_type::do_check_make_expression(Expression_list* args,
4328 source_location location)
4329 {
4330 gcc_assert(this->length_ == NULL);
4331 if (args == NULL || args->empty())
4332 {
4333 error_at(location, "length required when allocating a slice");
4334 return false;
4335 }
4336 else if (args->size() > 2)
4337 {
4338 error_at(location, "too many expressions passed to make");
4339 return false;
4340 }
4341 else
4342 {
4343 if (!Type::check_int_value(args->front(),
4344 _("bad length when making slice"), location))
4345 return false;
4346
4347 if (args->size() > 1)
4348 {
4349 if (!Type::check_int_value(args->back(),
4350 _("bad capacity when making slice"),
4351 location))
4352 return false;
4353 }
4354
4355 return true;
4356 }
4357 }
4358
4359 // Get a tree for the length of a fixed array. The length may be
4360 // computed using a function call, so we must only evaluate it once.
4361
4362 tree
4363 Array_type::get_length_tree(Gogo* gogo)
4364 {
4365 gcc_assert(this->length_ != NULL);
4366 if (this->length_tree_ == NULL_TREE)
4367 {
4368 mpz_t val;
4369 mpz_init(val);
4370 Type* t;
4371 if (this->length_->integer_constant_value(true, val, &t))
4372 {
4373 if (t == NULL)
4374 t = Type::lookup_integer_type("int");
4375 else if (t->is_abstract())
4376 t = t->make_non_abstract_type();
4377 tree tt = t->get_tree(gogo);
4378 this->length_tree_ = Expression::integer_constant_tree(val, tt);
4379 mpz_clear(val);
4380 }
4381 else
4382 {
4383 mpz_clear(val);
4384
4385 // Make up a translation context for the array length
4386 // expression. FIXME: This won't work in general.
4387 Translate_context context(gogo, NULL, NULL, NULL_TREE);
4388 tree len = this->length_->get_tree(&context);
4389 if (len != error_mark_node)
4390 {
4391 len = convert_to_integer(integer_type_node, len);
4392 len = save_expr(len);
4393 }
4394 this->length_tree_ = len;
4395 }
4396 }
4397 return this->length_tree_;
4398 }
4399
4400 // Get a tree for the type of this array. A fixed array is simply
4401 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
4402 // just like an array in C. An open array is a struct with three
4403 // fields: a data pointer, the length, and the capacity.
4404
4405 tree
4406 Array_type::do_get_tree(Gogo* gogo)
4407 {
4408 if (this->length_ == NULL)
4409 {
4410 tree struct_type = gogo->slice_type_tree(void_type_node);
4411 return this->fill_in_tree(gogo, struct_type);
4412 }
4413 else
4414 {
4415 tree element_type_tree = this->element_type_->get_tree(gogo);
4416 tree length_tree = this->get_length_tree(gogo);
4417 if (element_type_tree == error_mark_node
4418 || length_tree == error_mark_node)
4419 return error_mark_node;
4420
4421 length_tree = fold_convert(sizetype, length_tree);
4422
4423 // build_index_type takes the maximum index, which is one less
4424 // than the length.
4425 tree index_type = build_index_type(fold_build2(MINUS_EXPR, sizetype,
4426 length_tree,
4427 size_one_node));
4428
4429 return build_array_type(element_type_tree, index_type);
4430 }
4431 }
4432
4433 // Fill in the fields for a slice type. This is used for named slice
4434 // types.
4435
4436 tree
4437 Array_type::fill_in_tree(Gogo* gogo, tree struct_type)
4438 {
4439 gcc_assert(this->length_ == NULL);
4440
4441 tree element_type_tree = this->element_type_->get_tree(gogo);
4442 tree field = TYPE_FIELDS(struct_type);
4443 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
4444 gcc_assert(POINTER_TYPE_P(TREE_TYPE(field))
4445 && TREE_TYPE(TREE_TYPE(field)) == void_type_node);
4446 TREE_TYPE(field) = build_pointer_type(element_type_tree);
4447
4448 return struct_type;
4449 }
4450
4451 // Return an initializer for an array type.
4452
4453 tree
4454 Array_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
4455 {
4456 if (this->length_ == NULL)
4457 {
4458 // Open array.
4459
4460 if (is_clear)
4461 return NULL;
4462
4463 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
4464
4465 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
4466
4467 for (tree field = TYPE_FIELDS(type_tree);
4468 field != NULL_TREE;
4469 field = DECL_CHAIN(field))
4470 {
4471 constructor_elt* elt = VEC_quick_push(constructor_elt, init,
4472 NULL);
4473 elt->index = field;
4474 elt->value = fold_convert(TREE_TYPE(field), size_zero_node);
4475 }
4476
4477 tree ret = build_constructor(type_tree, init);
4478 TREE_CONSTANT(ret) = 1;
4479 return ret;
4480 }
4481 else
4482 {
4483 // Fixed array.
4484
4485 tree value = this->element_type_->get_init_tree(gogo, is_clear);
4486 if (value == NULL)
4487 return NULL;
4488 if (value == error_mark_node)
4489 return error_mark_node;
4490
4491 tree length_tree = this->get_length_tree(gogo);
4492 if (length_tree == error_mark_node)
4493 return error_mark_node;
4494
4495 length_tree = fold_convert(sizetype, length_tree);
4496 tree range = build2(RANGE_EXPR, sizetype, size_zero_node,
4497 fold_build2(MINUS_EXPR, sizetype,
4498 length_tree, size_one_node));
4499 tree ret = build_constructor_single(type_tree, range, value);
4500 if (TREE_CONSTANT(value))
4501 TREE_CONSTANT(ret) = 1;
4502 return ret;
4503 }
4504 }
4505
4506 // Handle the builtin make function for a slice.
4507
4508 tree
4509 Array_type::do_make_expression_tree(Translate_context* context,
4510 Expression_list* args,
4511 source_location location)
4512 {
4513 gcc_assert(this->length_ == NULL);
4514
4515 Gogo* gogo = context->gogo();
4516 tree type_tree = this->get_tree(gogo);
4517 if (type_tree == error_mark_node)
4518 return error_mark_node;
4519
4520 tree values_field = TYPE_FIELDS(type_tree);
4521 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field)),
4522 "__values") == 0);
4523
4524 tree count_field = DECL_CHAIN(values_field);
4525 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field)),
4526 "__count") == 0);
4527
4528 tree element_type_tree = this->element_type_->get_tree(gogo);
4529 if (element_type_tree == error_mark_node)
4530 return error_mark_node;
4531 tree element_size_tree = TYPE_SIZE_UNIT(element_type_tree);
4532
4533 tree value = this->element_type_->get_init_tree(gogo, true);
4534
4535 // The first argument is the number of elements, the optional second
4536 // argument is the capacity.
4537 gcc_assert(args != NULL && args->size() >= 1 && args->size() <= 2);
4538
4539 tree length_tree = args->front()->get_tree(context);
4540 if (length_tree == error_mark_node)
4541 return error_mark_node;
4542 if (!DECL_P(length_tree))
4543 length_tree = save_expr(length_tree);
4544 if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree)))
4545 length_tree = convert_to_integer(TREE_TYPE(count_field), length_tree);
4546
4547 tree bad_index = Expression::check_bounds(length_tree,
4548 TREE_TYPE(count_field),
4549 NULL_TREE, location);
4550
4551 length_tree = fold_convert_loc(location, TREE_TYPE(count_field), length_tree);
4552 tree capacity_tree;
4553 if (args->size() == 1)
4554 capacity_tree = length_tree;
4555 else
4556 {
4557 capacity_tree = args->back()->get_tree(context);
4558 if (capacity_tree == error_mark_node)
4559 return error_mark_node;
4560 if (!DECL_P(capacity_tree))
4561 capacity_tree = save_expr(capacity_tree);
4562 if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree)))
4563 capacity_tree = convert_to_integer(TREE_TYPE(count_field),
4564 capacity_tree);
4565
4566 bad_index = Expression::check_bounds(capacity_tree,
4567 TREE_TYPE(count_field),
4568 bad_index, location);
4569
4570 tree chktype = (((TYPE_SIZE(TREE_TYPE(capacity_tree))
4571 > TYPE_SIZE(TREE_TYPE(length_tree)))
4572 || ((TYPE_SIZE(TREE_TYPE(capacity_tree))
4573 == TYPE_SIZE(TREE_TYPE(length_tree)))
4574 && TYPE_UNSIGNED(TREE_TYPE(capacity_tree))))
4575 ? TREE_TYPE(capacity_tree)
4576 : TREE_TYPE(length_tree));
4577 tree chk = fold_build2_loc(location, LT_EXPR, boolean_type_node,
4578 fold_convert_loc(location, chktype,
4579 capacity_tree),
4580 fold_convert_loc(location, chktype,
4581 length_tree));
4582 if (bad_index == NULL_TREE)
4583 bad_index = chk;
4584 else
4585 bad_index = fold_build2_loc(location, TRUTH_OR_EXPR, boolean_type_node,
4586 bad_index, chk);
4587
4588 capacity_tree = fold_convert_loc(location, TREE_TYPE(count_field),
4589 capacity_tree);
4590 }
4591
4592 tree size_tree = fold_build2_loc(location, MULT_EXPR, sizetype,
4593 element_size_tree,
4594 fold_convert_loc(location, sizetype,
4595 capacity_tree));
4596
4597 tree chk = fold_build2_loc(location, TRUTH_AND_EXPR, boolean_type_node,
4598 fold_build2_loc(location, GT_EXPR,
4599 boolean_type_node,
4600 fold_convert_loc(location,
4601 sizetype,
4602 capacity_tree),
4603 size_zero_node),
4604 fold_build2_loc(location, LT_EXPR,
4605 boolean_type_node,
4606 size_tree, element_size_tree));
4607 if (bad_index == NULL_TREE)
4608 bad_index = chk;
4609 else
4610 bad_index = fold_build2_loc(location, TRUTH_OR_EXPR, boolean_type_node,
4611 bad_index, chk);
4612
4613 tree space = context->gogo()->allocate_memory(this->element_type_,
4614 size_tree, location);
4615
4616 if (value != NULL_TREE)
4617 space = save_expr(space);
4618
4619 space = fold_convert(TREE_TYPE(values_field), space);
4620
4621 if (bad_index != NULL_TREE && bad_index != boolean_false_node)
4622 {
4623 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS,
4624 location);
4625 space = build2(COMPOUND_EXPR, TREE_TYPE(space),
4626 build3(COND_EXPR, void_type_node,
4627 bad_index, crash, NULL_TREE),
4628 space);
4629 }
4630
4631 tree constructor = gogo->slice_constructor(type_tree, space, length_tree,
4632 capacity_tree);
4633
4634 if (value == NULL_TREE)
4635 {
4636 // The array contents are zero initialized.
4637 return constructor;
4638 }
4639
4640 // The elements must be initialized.
4641
4642 tree max = fold_build2_loc(location, MINUS_EXPR, TREE_TYPE(count_field),
4643 capacity_tree,
4644 fold_convert_loc(location, TREE_TYPE(count_field),
4645 integer_one_node));
4646
4647 tree array_type = build_array_type(element_type_tree,
4648 build_index_type(max));
4649
4650 tree value_pointer = fold_convert_loc(location,
4651 build_pointer_type(array_type),
4652 space);
4653
4654 tree range = build2(RANGE_EXPR, sizetype, size_zero_node, max);
4655 tree space_init = build_constructor_single(array_type, range, value);
4656
4657 return build2(COMPOUND_EXPR, TREE_TYPE(space),
4658 build2(MODIFY_EXPR, void_type_node,
4659 build_fold_indirect_ref(value_pointer),
4660 space_init),
4661 constructor);
4662 }
4663
4664 // Return a tree for a pointer to the values in ARRAY.
4665
4666 tree
4667 Array_type::value_pointer_tree(Gogo*, tree array) const
4668 {
4669 tree ret;
4670 if (this->length() != NULL)
4671 {
4672 // Fixed array.
4673 ret = fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array))),
4674 build_fold_addr_expr(array));
4675 }
4676 else
4677 {
4678 // Open array.
4679 tree field = TYPE_FIELDS(TREE_TYPE(array));
4680 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
4681 "__values") == 0);
4682 ret = fold_build3(COMPONENT_REF, TREE_TYPE(field), array, field,
4683 NULL_TREE);
4684 }
4685 if (TREE_CONSTANT(array))
4686 TREE_CONSTANT(ret) = 1;
4687 return ret;
4688 }
4689
4690 // Return a tree for the length of the array ARRAY which has this
4691 // type.
4692
4693 tree
4694 Array_type::length_tree(Gogo* gogo, tree array)
4695 {
4696 if (this->length_ != NULL)
4697 {
4698 if (TREE_CODE(array) == SAVE_EXPR)
4699 return fold_convert(integer_type_node, this->get_length_tree(gogo));
4700 else
4701 return omit_one_operand(integer_type_node,
4702 this->get_length_tree(gogo), array);
4703 }
4704
4705 // This is an open array. We need to read the length field.
4706
4707 tree type = TREE_TYPE(array);
4708 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
4709
4710 tree field = DECL_CHAIN(TYPE_FIELDS(type));
4711 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
4712
4713 tree ret = build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
4714 if (TREE_CONSTANT(array))
4715 TREE_CONSTANT(ret) = 1;
4716 return ret;
4717 }
4718
4719 // Return a tree for the capacity of the array ARRAY which has this
4720 // type.
4721
4722 tree
4723 Array_type::capacity_tree(Gogo* gogo, tree array)
4724 {
4725 if (this->length_ != NULL)
4726 return omit_one_operand(sizetype, this->get_length_tree(gogo), array);
4727
4728 // This is an open array. We need to read the capacity field.
4729
4730 tree type = TREE_TYPE(array);
4731 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
4732
4733 tree field = DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type)));
4734 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
4735
4736 return build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
4737 }
4738
4739 // Export.
4740
4741 void
4742 Array_type::do_export(Export* exp) const
4743 {
4744 exp->write_c_string("[");
4745 if (this->length_ != NULL)
4746 this->length_->export_expression(exp);
4747 exp->write_c_string("] ");
4748 exp->write_type(this->element_type_);
4749 }
4750
4751 // Import.
4752
4753 Array_type*
4754 Array_type::do_import(Import* imp)
4755 {
4756 imp->require_c_string("[");
4757 Expression* length;
4758 if (imp->peek_char() == ']')
4759 length = NULL;
4760 else
4761 length = Expression::import_expression(imp);
4762 imp->require_c_string("] ");
4763 Type* element_type = imp->read_type();
4764 return Type::make_array_type(element_type, length);
4765 }
4766
4767 // The type of an array type descriptor.
4768
4769 Type*
4770 Array_type::make_array_type_descriptor_type()
4771 {
4772 static Type* ret;
4773 if (ret == NULL)
4774 {
4775 Type* tdt = Type::make_type_descriptor_type();
4776 Type* ptdt = Type::make_type_descriptor_ptr_type();
4777
4778 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4779
4780 Struct_type* sf =
4781 Type::make_builtin_struct_type(3,
4782 "", tdt,
4783 "elem", ptdt,
4784 "len", uintptr_type);
4785
4786 ret = Type::make_builtin_named_type("ArrayType", sf);
4787 }
4788
4789 return ret;
4790 }
4791
4792 // The type of an slice type descriptor.
4793
4794 Type*
4795 Array_type::make_slice_type_descriptor_type()
4796 {
4797 static Type* ret;
4798 if (ret == NULL)
4799 {
4800 Type* tdt = Type::make_type_descriptor_type();
4801 Type* ptdt = Type::make_type_descriptor_ptr_type();
4802
4803 Struct_type* sf =
4804 Type::make_builtin_struct_type(2,
4805 "", tdt,
4806 "elem", ptdt);
4807
4808 ret = Type::make_builtin_named_type("SliceType", sf);
4809 }
4810
4811 return ret;
4812 }
4813
4814 // Build a type descriptor for an array/slice type.
4815
4816 Expression*
4817 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4818 {
4819 if (this->length_ != NULL)
4820 return this->array_type_descriptor(gogo, name);
4821 else
4822 return this->slice_type_descriptor(gogo, name);
4823 }
4824
4825 // Build a type descriptor for an array type.
4826
4827 Expression*
4828 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
4829 {
4830 source_location bloc = BUILTINS_LOCATION;
4831
4832 Type* atdt = Array_type::make_array_type_descriptor_type();
4833
4834 const Struct_field_list* fields = atdt->struct_type()->fields();
4835
4836 Expression_list* vals = new Expression_list();
4837 vals->reserve(3);
4838
4839 Struct_field_list::const_iterator p = fields->begin();
4840 gcc_assert(p->field_name() == "commonType");
4841 vals->push_back(this->type_descriptor_constructor(gogo,
4842 RUNTIME_TYPE_KIND_ARRAY,
4843 name, NULL, true));
4844
4845 ++p;
4846 gcc_assert(p->field_name() == "elem");
4847 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
4848
4849 ++p;
4850 gcc_assert(p->field_name() == "len");
4851 vals->push_back(this->length_);
4852
4853 ++p;
4854 gcc_assert(p == fields->end());
4855
4856 return Expression::make_struct_composite_literal(atdt, vals, bloc);
4857 }
4858
4859 // Build a type descriptor for a slice type.
4860
4861 Expression*
4862 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
4863 {
4864 source_location bloc = BUILTINS_LOCATION;
4865
4866 Type* stdt = Array_type::make_slice_type_descriptor_type();
4867
4868 const Struct_field_list* fields = stdt->struct_type()->fields();
4869
4870 Expression_list* vals = new Expression_list();
4871 vals->reserve(2);
4872
4873 Struct_field_list::const_iterator p = fields->begin();
4874 gcc_assert(p->field_name() == "commonType");
4875 vals->push_back(this->type_descriptor_constructor(gogo,
4876 RUNTIME_TYPE_KIND_SLICE,
4877 name, NULL, true));
4878
4879 ++p;
4880 gcc_assert(p->field_name() == "elem");
4881 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
4882
4883 ++p;
4884 gcc_assert(p == fields->end());
4885
4886 return Expression::make_struct_composite_literal(stdt, vals, bloc);
4887 }
4888
4889 // Reflection string.
4890
4891 void
4892 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
4893 {
4894 ret->push_back('[');
4895 if (this->length_ != NULL)
4896 {
4897 mpz_t val;
4898 mpz_init(val);
4899 Type* type;
4900 if (!this->length_->integer_constant_value(true, val, &type))
4901 error_at(this->length_->location(),
4902 "array length must be integer constant expression");
4903 else if (mpz_cmp_si(val, 0) < 0)
4904 error_at(this->length_->location(), "array length is negative");
4905 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
4906 error_at(this->length_->location(), "array length is too large");
4907 else
4908 {
4909 char buf[50];
4910 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
4911 ret->append(buf);
4912 }
4913 mpz_clear(val);
4914 }
4915 ret->push_back(']');
4916
4917 this->append_reflection(this->element_type_, gogo, ret);
4918 }
4919
4920 // Mangled name.
4921
4922 void
4923 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4924 {
4925 ret->push_back('A');
4926 this->append_mangled_name(this->element_type_, gogo, ret);
4927 if (this->length_ != NULL)
4928 {
4929 mpz_t val;
4930 mpz_init(val);
4931 Type* type;
4932 if (!this->length_->integer_constant_value(true, val, &type))
4933 error_at(this->length_->location(),
4934 "array length must be integer constant expression");
4935 else if (mpz_cmp_si(val, 0) < 0)
4936 error_at(this->length_->location(), "array length is negative");
4937 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
4938 error_at(this->length_->location(), "array size is too large");
4939 else
4940 {
4941 char buf[50];
4942 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
4943 ret->append(buf);
4944 }
4945 mpz_clear(val);
4946 }
4947 ret->push_back('e');
4948 }
4949
4950 // Make an array type.
4951
4952 Array_type*
4953 Type::make_array_type(Type* element_type, Expression* length)
4954 {
4955 return new Array_type(element_type, length);
4956 }
4957
4958 // Class Map_type.
4959
4960 // Traversal.
4961
4962 int
4963 Map_type::do_traverse(Traverse* traverse)
4964 {
4965 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
4966 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
4967 return TRAVERSE_EXIT;
4968 return TRAVERSE_CONTINUE;
4969 }
4970
4971 // Check that the map type is OK.
4972
4973 bool
4974 Map_type::do_verify()
4975 {
4976 if (this->key_type_->struct_type() != NULL
4977 || this->key_type_->array_type() != NULL)
4978 {
4979 error_at(this->location_, "invalid map key type");
4980 return false;
4981 }
4982 return true;
4983 }
4984
4985 // Whether two map types are identical.
4986
4987 bool
4988 Map_type::is_identical(const Map_type* t, bool errors_are_identical) const
4989 {
4990 return (Type::are_identical(this->key_type(), t->key_type(),
4991 errors_are_identical, NULL)
4992 && Type::are_identical(this->val_type(), t->val_type(),
4993 errors_are_identical, NULL));
4994 }
4995
4996 // Hash code.
4997
4998 unsigned int
4999 Map_type::do_hash_for_method(Gogo* gogo) const
5000 {
5001 return (this->key_type_->hash_for_method(gogo)
5002 + this->val_type_->hash_for_method(gogo)
5003 + 2);
5004 }
5005
5006 // Check that a call to the builtin make function is valid. For a map
5007 // the optional argument is the number of spaces to preallocate for
5008 // values.
5009
5010 bool
5011 Map_type::do_check_make_expression(Expression_list* args,
5012 source_location location)
5013 {
5014 if (args != NULL && !args->empty())
5015 {
5016 if (!Type::check_int_value(args->front(), _("bad size when making map"),
5017 location))
5018 return false;
5019 else if (args->size() > 1)
5020 {
5021 error_at(location, "too many arguments when making map");
5022 return false;
5023 }
5024 }
5025 return true;
5026 }
5027
5028 // Get a tree for a map type. A map type is represented as a pointer
5029 // to a struct. The struct is __go_map in libgo/map.h.
5030
5031 tree
5032 Map_type::do_get_tree(Gogo* gogo)
5033 {
5034 static tree type_tree;
5035 if (type_tree == NULL_TREE)
5036 {
5037 tree struct_type = make_node(RECORD_TYPE);
5038
5039 tree map_descriptor_type = gogo->map_descriptor_type();
5040 tree const_map_descriptor_type =
5041 build_qualified_type(map_descriptor_type, TYPE_QUAL_CONST);
5042 tree name = get_identifier("__descriptor");
5043 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
5044 build_pointer_type(const_map_descriptor_type));
5045 DECL_CONTEXT(field) = struct_type;
5046 TYPE_FIELDS(struct_type) = field;
5047 tree last_field = field;
5048
5049 name = get_identifier("__element_count");
5050 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, sizetype);
5051 DECL_CONTEXT(field) = struct_type;
5052 DECL_CHAIN(last_field) = field;
5053 last_field = field;
5054
5055 name = get_identifier("__bucket_count");
5056 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, sizetype);
5057 DECL_CONTEXT(field) = struct_type;
5058 DECL_CHAIN(last_field) = field;
5059 last_field = field;
5060
5061 name = get_identifier("__buckets");
5062 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
5063 build_pointer_type(ptr_type_node));
5064 DECL_CONTEXT(field) = struct_type;
5065 DECL_CHAIN(last_field) = field;
5066
5067 layout_type(struct_type);
5068
5069 // Give the struct a name for better debugging info.
5070 name = get_identifier("__go_map");
5071 tree type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
5072 struct_type);
5073 DECL_ARTIFICIAL(type_decl) = 1;
5074 TYPE_NAME(struct_type) = type_decl;
5075 go_preserve_from_gc(type_decl);
5076 rest_of_decl_compilation(type_decl, 1, 0);
5077
5078 type_tree = build_pointer_type(struct_type);
5079 go_preserve_from_gc(type_tree);
5080 }
5081
5082 return type_tree;
5083 }
5084
5085 // Initialize a map.
5086
5087 tree
5088 Map_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
5089 {
5090 if (is_clear)
5091 return NULL;
5092 return fold_convert(type_tree, null_pointer_node);
5093 }
5094
5095 // Return an expression for a newly allocated map.
5096
5097 tree
5098 Map_type::do_make_expression_tree(Translate_context* context,
5099 Expression_list* args,
5100 source_location location)
5101 {
5102 tree bad_index = NULL_TREE;
5103
5104 tree expr_tree;
5105 if (args == NULL || args->empty())
5106 expr_tree = size_zero_node;
5107 else
5108 {
5109 expr_tree = args->front()->get_tree(context);
5110 if (expr_tree == error_mark_node)
5111 return error_mark_node;
5112 if (!DECL_P(expr_tree))
5113 expr_tree = save_expr(expr_tree);
5114 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
5115 expr_tree = convert_to_integer(sizetype, expr_tree);
5116 bad_index = Expression::check_bounds(expr_tree, sizetype, bad_index,
5117 location);
5118 }
5119
5120 tree map_type = this->get_tree(context->gogo());
5121
5122 static tree new_map_fndecl;
5123 tree ret = Gogo::call_builtin(&new_map_fndecl,
5124 location,
5125 "__go_new_map",
5126 2,
5127 map_type,
5128 TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type))),
5129 context->gogo()->map_descriptor(this),
5130 sizetype,
5131 expr_tree);
5132 if (ret == error_mark_node)
5133 return error_mark_node;
5134 // This can panic if the capacity is out of range.
5135 TREE_NOTHROW(new_map_fndecl) = 0;
5136
5137 if (bad_index == NULL_TREE)
5138 return ret;
5139 else
5140 {
5141 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS,
5142 location);
5143 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
5144 build3(COND_EXPR, void_type_node,
5145 bad_index, crash, NULL_TREE),
5146 ret);
5147 }
5148 }
5149
5150 // The type of a map type descriptor.
5151
5152 Type*
5153 Map_type::make_map_type_descriptor_type()
5154 {
5155 static Type* ret;
5156 if (ret == NULL)
5157 {
5158 Type* tdt = Type::make_type_descriptor_type();
5159 Type* ptdt = Type::make_type_descriptor_ptr_type();
5160
5161 Struct_type* sf =
5162 Type::make_builtin_struct_type(3,
5163 "", tdt,
5164 "key", ptdt,
5165 "elem", ptdt);
5166
5167 ret = Type::make_builtin_named_type("MapType", sf);
5168 }
5169
5170 return ret;
5171 }
5172
5173 // Build a type descriptor for a map type.
5174
5175 Expression*
5176 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5177 {
5178 source_location bloc = BUILTINS_LOCATION;
5179
5180 Type* mtdt = Map_type::make_map_type_descriptor_type();
5181
5182 const Struct_field_list* fields = mtdt->struct_type()->fields();
5183
5184 Expression_list* vals = new Expression_list();
5185 vals->reserve(3);
5186
5187 Struct_field_list::const_iterator p = fields->begin();
5188 gcc_assert(p->field_name() == "commonType");
5189 vals->push_back(this->type_descriptor_constructor(gogo,
5190 RUNTIME_TYPE_KIND_MAP,
5191 name, NULL, true));
5192
5193 ++p;
5194 gcc_assert(p->field_name() == "key");
5195 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
5196
5197 ++p;
5198 gcc_assert(p->field_name() == "elem");
5199 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
5200
5201 ++p;
5202 gcc_assert(p == fields->end());
5203
5204 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
5205 }
5206
5207 // Reflection string for a map.
5208
5209 void
5210 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
5211 {
5212 ret->append("map[");
5213 this->append_reflection(this->key_type_, gogo, ret);
5214 ret->append("] ");
5215 this->append_reflection(this->val_type_, gogo, ret);
5216 }
5217
5218 // Mangled name for a map.
5219
5220 void
5221 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5222 {
5223 ret->push_back('M');
5224 this->append_mangled_name(this->key_type_, gogo, ret);
5225 ret->append("__");
5226 this->append_mangled_name(this->val_type_, gogo, ret);
5227 }
5228
5229 // Export a map type.
5230
5231 void
5232 Map_type::do_export(Export* exp) const
5233 {
5234 exp->write_c_string("map [");
5235 exp->write_type(this->key_type_);
5236 exp->write_c_string("] ");
5237 exp->write_type(this->val_type_);
5238 }
5239
5240 // Import a map type.
5241
5242 Map_type*
5243 Map_type::do_import(Import* imp)
5244 {
5245 imp->require_c_string("map [");
5246 Type* key_type = imp->read_type();
5247 imp->require_c_string("] ");
5248 Type* val_type = imp->read_type();
5249 return Type::make_map_type(key_type, val_type, imp->location());
5250 }
5251
5252 // Make a map type.
5253
5254 Map_type*
5255 Type::make_map_type(Type* key_type, Type* val_type, source_location location)
5256 {
5257 return new Map_type(key_type, val_type, location);
5258 }
5259
5260 // Class Channel_type.
5261
5262 // Hash code.
5263
5264 unsigned int
5265 Channel_type::do_hash_for_method(Gogo* gogo) const
5266 {
5267 unsigned int ret = 0;
5268 if (this->may_send_)
5269 ret += 1;
5270 if (this->may_receive_)
5271 ret += 2;
5272 if (this->element_type_ != NULL)
5273 ret += this->element_type_->hash_for_method(gogo) << 2;
5274 return ret << 3;
5275 }
5276
5277 // Whether this type is the same as T.
5278
5279 bool
5280 Channel_type::is_identical(const Channel_type* t,
5281 bool errors_are_identical) const
5282 {
5283 if (!Type::are_identical(this->element_type(), t->element_type(),
5284 errors_are_identical, NULL))
5285 return false;
5286 return (this->may_send_ == t->may_send_
5287 && this->may_receive_ == t->may_receive_);
5288 }
5289
5290 // Check whether the parameters for a call to the builtin function
5291 // make are OK for a channel. A channel can take an optional single
5292 // parameter which is the buffer size.
5293
5294 bool
5295 Channel_type::do_check_make_expression(Expression_list* args,
5296 source_location location)
5297 {
5298 if (args != NULL && !args->empty())
5299 {
5300 if (!Type::check_int_value(args->front(),
5301 _("bad buffer size when making channel"),
5302 location))
5303 return false;
5304 else if (args->size() > 1)
5305 {
5306 error_at(location, "too many arguments when making channel");
5307 return false;
5308 }
5309 }
5310 return true;
5311 }
5312
5313 // Return the tree for a channel type. A channel is a pointer to a
5314 // __go_channel struct. The __go_channel struct is defined in
5315 // libgo/runtime/channel.h.
5316
5317 tree
5318 Channel_type::do_get_tree(Gogo*)
5319 {
5320 static tree type_tree;
5321 if (type_tree == NULL_TREE)
5322 {
5323 tree ret = make_node(RECORD_TYPE);
5324 TYPE_NAME(ret) = get_identifier("__go_channel");
5325 TYPE_STUB_DECL(ret) = build_decl(BUILTINS_LOCATION, TYPE_DECL, NULL_TREE,
5326 ret);
5327 type_tree = build_pointer_type(ret);
5328 go_preserve_from_gc(type_tree);
5329 }
5330 return type_tree;
5331 }
5332
5333 // Initialize a channel variable.
5334
5335 tree
5336 Channel_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
5337 {
5338 if (is_clear)
5339 return NULL;
5340 return fold_convert(type_tree, null_pointer_node);
5341 }
5342
5343 // Handle the builtin function make for a channel.
5344
5345 tree
5346 Channel_type::do_make_expression_tree(Translate_context* context,
5347 Expression_list* args,
5348 source_location location)
5349 {
5350 Gogo* gogo = context->gogo();
5351 tree channel_type = this->get_tree(gogo);
5352
5353 tree element_tree = this->element_type_->get_tree(gogo);
5354 tree element_size_tree = size_in_bytes(element_tree);
5355
5356 tree bad_index = NULL_TREE;
5357
5358 tree expr_tree;
5359 if (args == NULL || args->empty())
5360 expr_tree = size_zero_node;
5361 else
5362 {
5363 expr_tree = args->front()->get_tree(context);
5364 if (expr_tree == error_mark_node)
5365 return error_mark_node;
5366 if (!DECL_P(expr_tree))
5367 expr_tree = save_expr(expr_tree);
5368 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
5369 expr_tree = convert_to_integer(sizetype, expr_tree);
5370 bad_index = Expression::check_bounds(expr_tree, sizetype, bad_index,
5371 location);
5372 }
5373
5374 static tree new_channel_fndecl;
5375 tree ret = Gogo::call_builtin(&new_channel_fndecl,
5376 location,
5377 "__go_new_channel",
5378 2,
5379 channel_type,
5380 sizetype,
5381 element_size_tree,
5382 sizetype,
5383 expr_tree);
5384 if (ret == error_mark_node)
5385 return error_mark_node;
5386 // This can panic if the capacity is out of range.
5387 TREE_NOTHROW(new_channel_fndecl) = 0;
5388
5389 if (bad_index == NULL_TREE)
5390 return ret;
5391 else
5392 {
5393 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS,
5394 location);
5395 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
5396 build3(COND_EXPR, void_type_node,
5397 bad_index, crash, NULL_TREE),
5398 ret);
5399 }
5400 }
5401
5402 // Build a type descriptor for a channel type.
5403
5404 Type*
5405 Channel_type::make_chan_type_descriptor_type()
5406 {
5407 static Type* ret;
5408 if (ret == NULL)
5409 {
5410 Type* tdt = Type::make_type_descriptor_type();
5411 Type* ptdt = Type::make_type_descriptor_ptr_type();
5412
5413 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5414
5415 Struct_type* sf =
5416 Type::make_builtin_struct_type(3,
5417 "", tdt,
5418 "elem", ptdt,
5419 "dir", uintptr_type);
5420
5421 ret = Type::make_builtin_named_type("ChanType", sf);
5422 }
5423
5424 return ret;
5425 }
5426
5427 // Build a type descriptor for a map type.
5428
5429 Expression*
5430 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5431 {
5432 source_location bloc = BUILTINS_LOCATION;
5433
5434 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
5435
5436 const Struct_field_list* fields = ctdt->struct_type()->fields();
5437
5438 Expression_list* vals = new Expression_list();
5439 vals->reserve(3);
5440
5441 Struct_field_list::const_iterator p = fields->begin();
5442 gcc_assert(p->field_name() == "commonType");
5443 vals->push_back(this->type_descriptor_constructor(gogo,
5444 RUNTIME_TYPE_KIND_CHAN,
5445 name, NULL, true));
5446
5447 ++p;
5448 gcc_assert(p->field_name() == "elem");
5449 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5450
5451 ++p;
5452 gcc_assert(p->field_name() == "dir");
5453 // These bits must match the ones in libgo/runtime/go-type.h.
5454 int val = 0;
5455 if (this->may_receive_)
5456 val |= 1;
5457 if (this->may_send_)
5458 val |= 2;
5459 mpz_t iv;
5460 mpz_init_set_ui(iv, val);
5461 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
5462 mpz_clear(iv);
5463
5464 ++p;
5465 gcc_assert(p == fields->end());
5466
5467 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
5468 }
5469
5470 // Reflection string.
5471
5472 void
5473 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
5474 {
5475 if (!this->may_send_)
5476 ret->append("<-");
5477 ret->append("chan");
5478 if (!this->may_receive_)
5479 ret->append("<-");
5480 ret->push_back(' ');
5481 this->append_reflection(this->element_type_, gogo, ret);
5482 }
5483
5484 // Mangled name.
5485
5486 void
5487 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5488 {
5489 ret->push_back('C');
5490 this->append_mangled_name(this->element_type_, gogo, ret);
5491 if (this->may_send_)
5492 ret->push_back('s');
5493 if (this->may_receive_)
5494 ret->push_back('r');
5495 ret->push_back('e');
5496 }
5497
5498 // Export.
5499
5500 void
5501 Channel_type::do_export(Export* exp) const
5502 {
5503 exp->write_c_string("chan ");
5504 if (this->may_send_ && !this->may_receive_)
5505 exp->write_c_string("-< ");
5506 else if (this->may_receive_ && !this->may_send_)
5507 exp->write_c_string("<- ");
5508 exp->write_type(this->element_type_);
5509 }
5510
5511 // Import.
5512
5513 Channel_type*
5514 Channel_type::do_import(Import* imp)
5515 {
5516 imp->require_c_string("chan ");
5517
5518 bool may_send;
5519 bool may_receive;
5520 if (imp->match_c_string("-< "))
5521 {
5522 imp->advance(3);
5523 may_send = true;
5524 may_receive = false;
5525 }
5526 else if (imp->match_c_string("<- "))
5527 {
5528 imp->advance(3);
5529 may_receive = true;
5530 may_send = false;
5531 }
5532 else
5533 {
5534 may_send = true;
5535 may_receive = true;
5536 }
5537
5538 Type* element_type = imp->read_type();
5539
5540 return Type::make_channel_type(may_send, may_receive, element_type);
5541 }
5542
5543 // Make a new channel type.
5544
5545 Channel_type*
5546 Type::make_channel_type(bool send, bool receive, Type* element_type)
5547 {
5548 return new Channel_type(send, receive, element_type);
5549 }
5550
5551 // Class Interface_type.
5552
5553 // Traversal.
5554
5555 int
5556 Interface_type::do_traverse(Traverse* traverse)
5557 {
5558 if (this->methods_ == NULL)
5559 return TRAVERSE_CONTINUE;
5560 return this->methods_->traverse(traverse);
5561 }
5562
5563 // Finalize the methods. This handles interface inheritance.
5564
5565 void
5566 Interface_type::finalize_methods()
5567 {
5568 if (this->methods_ == NULL)
5569 return;
5570 bool is_recursive = false;
5571 size_t from = 0;
5572 size_t to = 0;
5573 while (from < this->methods_->size())
5574 {
5575 const Typed_identifier* p = &this->methods_->at(from);
5576 if (!p->name().empty())
5577 {
5578 size_t i;
5579 for (i = 0; i < to; ++i)
5580 {
5581 if (this->methods_->at(i).name() == p->name())
5582 {
5583 error_at(p->location(), "duplicate method %qs",
5584 Gogo::message_name(p->name()).c_str());
5585 break;
5586 }
5587 }
5588 if (i == to)
5589 {
5590 if (from != to)
5591 this->methods_->set(to, *p);
5592 ++to;
5593 }
5594 ++from;
5595 continue;
5596 }
5597 Interface_type* it = p->type()->interface_type();
5598 if (it == NULL)
5599 {
5600 error_at(p->location(), "interface contains embedded non-interface");
5601 ++from;
5602 continue;
5603 }
5604 if (it == this)
5605 {
5606 if (!is_recursive)
5607 {
5608 error_at(p->location(), "invalid recursive interface");
5609 is_recursive = true;
5610 }
5611 ++from;
5612 continue;
5613 }
5614 const Typed_identifier_list* methods = it->methods();
5615 if (methods == NULL)
5616 {
5617 ++from;
5618 continue;
5619 }
5620 for (Typed_identifier_list::const_iterator q = methods->begin();
5621 q != methods->end();
5622 ++q)
5623 {
5624 if (q->name().empty())
5625 {
5626 if (q->type() == p->type())
5627 error_at(p->location(), "interface inheritance loop");
5628 else
5629 {
5630 size_t i;
5631 for (i = from + 1; i < this->methods_->size(); ++i)
5632 {
5633 const Typed_identifier* r = &this->methods_->at(i);
5634 if (r->name().empty() && r->type() == q->type())
5635 {
5636 error_at(p->location(),
5637 "inherited interface listed twice");
5638 break;
5639 }
5640 }
5641 if (i == this->methods_->size())
5642 this->methods_->push_back(Typed_identifier(q->name(),
5643 q->type(),
5644 p->location()));
5645 }
5646 }
5647 else if (this->find_method(q->name()) == NULL)
5648 this->methods_->push_back(Typed_identifier(q->name(), q->type(),
5649 p->location()));
5650 else
5651 {
5652 if (!is_recursive)
5653 error_at(p->location(), "inherited method %qs is ambiguous",
5654 Gogo::message_name(q->name()).c_str());
5655 }
5656 }
5657 ++from;
5658 }
5659 if (to == 0)
5660 {
5661 delete this->methods_;
5662 this->methods_ = NULL;
5663 }
5664 else
5665 {
5666 this->methods_->resize(to);
5667 this->methods_->sort_by_name();
5668 }
5669 }
5670
5671 // Return the method NAME, or NULL.
5672
5673 const Typed_identifier*
5674 Interface_type::find_method(const std::string& name) const
5675 {
5676 if (this->methods_ == NULL)
5677 return NULL;
5678 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5679 p != this->methods_->end();
5680 ++p)
5681 if (p->name() == name)
5682 return &*p;
5683 return NULL;
5684 }
5685
5686 // Return the method index.
5687
5688 size_t
5689 Interface_type::method_index(const std::string& name) const
5690 {
5691 gcc_assert(this->methods_ != NULL);
5692 size_t ret = 0;
5693 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5694 p != this->methods_->end();
5695 ++p, ++ret)
5696 if (p->name() == name)
5697 return ret;
5698 gcc_unreachable();
5699 }
5700
5701 // Return whether NAME is an unexported method, for better error
5702 // reporting.
5703
5704 bool
5705 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
5706 {
5707 if (this->methods_ == NULL)
5708 return false;
5709 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5710 p != this->methods_->end();
5711 ++p)
5712 {
5713 const std::string& method_name(p->name());
5714 if (Gogo::is_hidden_name(method_name)
5715 && name == Gogo::unpack_hidden_name(method_name)
5716 && gogo->pack_hidden_name(name, false) != method_name)
5717 return true;
5718 }
5719 return false;
5720 }
5721
5722 // Whether this type is identical with T.
5723
5724 bool
5725 Interface_type::is_identical(const Interface_type* t,
5726 bool errors_are_identical) const
5727 {
5728 // We require the same methods with the same types. The methods
5729 // have already been sorted.
5730 if (this->methods() == NULL || t->methods() == NULL)
5731 return this->methods() == t->methods();
5732
5733 Typed_identifier_list::const_iterator p1 = this->methods()->begin();
5734 for (Typed_identifier_list::const_iterator p2 = t->methods()->begin();
5735 p2 != t->methods()->end();
5736 ++p1, ++p2)
5737 {
5738 if (p1 == this->methods()->end())
5739 return false;
5740 if (p1->name() != p2->name()
5741 || !Type::are_identical(p1->type(), p2->type(),
5742 errors_are_identical, NULL))
5743 return false;
5744 }
5745 if (p1 != this->methods()->end())
5746 return false;
5747 return true;
5748 }
5749
5750 // Whether we can assign the interface type T to this type. The types
5751 // are known to not be identical. An interface assignment is only
5752 // permitted if T is known to implement all methods in THIS.
5753 // Otherwise a type guard is required.
5754
5755 bool
5756 Interface_type::is_compatible_for_assign(const Interface_type* t,
5757 std::string* reason) const
5758 {
5759 if (this->methods() == NULL)
5760 return true;
5761 for (Typed_identifier_list::const_iterator p = this->methods()->begin();
5762 p != this->methods()->end();
5763 ++p)
5764 {
5765 const Typed_identifier* m = t->find_method(p->name());
5766 if (m == NULL)
5767 {
5768 if (reason != NULL)
5769 {
5770 char buf[200];
5771 snprintf(buf, sizeof buf,
5772 _("need explicit conversion; missing method %s%s%s"),
5773 open_quote, Gogo::message_name(p->name()).c_str(),
5774 close_quote);
5775 reason->assign(buf);
5776 }
5777 return false;
5778 }
5779
5780 std::string subreason;
5781 if (!Type::are_identical(p->type(), m->type(), true, &subreason))
5782 {
5783 if (reason != NULL)
5784 {
5785 std::string n = Gogo::message_name(p->name());
5786 size_t len = 100 + n.length() + subreason.length();
5787 char* buf = new char[len];
5788 if (subreason.empty())
5789 snprintf(buf, len, _("incompatible type for method %s%s%s"),
5790 open_quote, n.c_str(), close_quote);
5791 else
5792 snprintf(buf, len,
5793 _("incompatible type for method %s%s%s (%s)"),
5794 open_quote, n.c_str(), close_quote,
5795 subreason.c_str());
5796 reason->assign(buf);
5797 delete[] buf;
5798 }
5799 return false;
5800 }
5801 }
5802
5803 return true;
5804 }
5805
5806 // Hash code.
5807
5808 unsigned int
5809 Interface_type::do_hash_for_method(Gogo* gogo) const
5810 {
5811 unsigned int ret = 0;
5812 if (this->methods_ != NULL)
5813 {
5814 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5815 p != this->methods_->end();
5816 ++p)
5817 {
5818 ret = Type::hash_string(p->name(), ret);
5819 ret += p->type()->hash_for_method(gogo);
5820 ret <<= 1;
5821 }
5822 }
5823 return ret;
5824 }
5825
5826 // Return true if T implements the interface. If it does not, and
5827 // REASON is not NULL, set *REASON to a useful error message.
5828
5829 bool
5830 Interface_type::implements_interface(const Type* t, std::string* reason) const
5831 {
5832 if (this->methods_ == NULL)
5833 return true;
5834
5835 bool is_pointer = false;
5836 const Named_type* nt = t->named_type();
5837 const Struct_type* st = t->struct_type();
5838 // If we start with a named type, we don't dereference it to find
5839 // methods.
5840 if (nt == NULL)
5841 {
5842 const Type* pt = t->points_to();
5843 if (pt != NULL)
5844 {
5845 // If T is a pointer to a named type, then we need to look at
5846 // the type to which it points.
5847 is_pointer = true;
5848 nt = pt->named_type();
5849 st = pt->struct_type();
5850 }
5851 }
5852
5853 // If we have a named type, get the methods from it rather than from
5854 // any struct type.
5855 if (nt != NULL)
5856 st = NULL;
5857
5858 // Only named and struct types have methods.
5859 if (nt == NULL && st == NULL)
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 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
5873 {
5874 if (reason != NULL)
5875 {
5876 if (t->points_to() != NULL
5877 && t->points_to()->interface_type() != NULL)
5878 reason->assign(_("pointer to interface type has no methods"));
5879 else
5880 reason->assign(_("type has no methods"));
5881 }
5882 return false;
5883 }
5884
5885 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5886 p != this->methods_->end();
5887 ++p)
5888 {
5889 bool is_ambiguous = false;
5890 Method* m = (nt != NULL
5891 ? nt->method_function(p->name(), &is_ambiguous)
5892 : st->method_function(p->name(), &is_ambiguous));
5893 if (m == NULL)
5894 {
5895 if (reason != NULL)
5896 {
5897 std::string n = Gogo::message_name(p->name());
5898 size_t len = n.length() + 100;
5899 char* buf = new char[len];
5900 if (is_ambiguous)
5901 snprintf(buf, len, _("ambiguous method %s%s%s"),
5902 open_quote, n.c_str(), close_quote);
5903 else
5904 snprintf(buf, len, _("missing method %s%s%s"),
5905 open_quote, n.c_str(), close_quote);
5906 reason->assign(buf);
5907 delete[] buf;
5908 }
5909 return false;
5910 }
5911
5912 Function_type *p_fn_type = p->type()->function_type();
5913 Function_type* m_fn_type = m->type()->function_type();
5914 gcc_assert(p_fn_type != NULL && m_fn_type != NULL);
5915 std::string subreason;
5916 if (!p_fn_type->is_identical(m_fn_type, true, true, &subreason))
5917 {
5918 if (reason != NULL)
5919 {
5920 std::string n = Gogo::message_name(p->name());
5921 size_t len = 100 + n.length() + subreason.length();
5922 char* buf = new char[len];
5923 if (subreason.empty())
5924 snprintf(buf, len, _("incompatible type for method %s%s%s"),
5925 open_quote, n.c_str(), close_quote);
5926 else
5927 snprintf(buf, len,
5928 _("incompatible type for method %s%s%s (%s)"),
5929 open_quote, n.c_str(), close_quote,
5930 subreason.c_str());
5931 reason->assign(buf);
5932 delete[] buf;
5933 }
5934 return false;
5935 }
5936
5937 if (!is_pointer && !m->is_value_method())
5938 {
5939 if (reason != NULL)
5940 {
5941 std::string n = Gogo::message_name(p->name());
5942 size_t len = 100 + n.length();
5943 char* buf = new char[len];
5944 snprintf(buf, len, _("method %s%s%s requires a pointer"),
5945 open_quote, n.c_str(), close_quote);
5946 reason->assign(buf);
5947 delete[] buf;
5948 }
5949 return false;
5950 }
5951 }
5952
5953 return true;
5954 }
5955
5956 // Return a tree for an interface type. An interface is a pointer to
5957 // a struct. The struct has three fields. The first field is a
5958 // pointer to the type descriptor for the dynamic type of the object.
5959 // The second field is a pointer to a table of methods for the
5960 // interface to be used with the object. The third field is the value
5961 // of the object itself.
5962
5963 tree
5964 Interface_type::do_get_tree(Gogo* gogo)
5965 {
5966 if (this->methods_ == NULL)
5967 {
5968 // At the tree level, use the same type for all empty
5969 // interfaces. This lets us assign them to each other directly
5970 // without triggering GIMPLE type errors.
5971 tree dtype = Type::make_type_descriptor_type()->get_tree(gogo);
5972 dtype = build_pointer_type(build_qualified_type(dtype, TYPE_QUAL_CONST));
5973 static tree empty_interface;
5974 return Gogo::builtin_struct(&empty_interface, "__go_empty_interface",
5975 NULL_TREE, 2,
5976 "__type_descriptor",
5977 dtype,
5978 "__object",
5979 ptr_type_node);
5980 }
5981
5982 return this->fill_in_tree(gogo, make_node(RECORD_TYPE));
5983 }
5984
5985 // Fill in the tree for an interface type. This is used for named
5986 // interface types.
5987
5988 tree
5989 Interface_type::fill_in_tree(Gogo* gogo, tree type)
5990 {
5991 gcc_assert(this->methods_ != NULL);
5992
5993 // Because the methods may refer to the interface type itself, we
5994 // need to build the interface type first, and then update the
5995 // method pointer later.
5996
5997 tree field_trees = NULL_TREE;
5998 tree* pp = &field_trees;
5999
6000 tree name_tree = get_identifier("__methods");
6001 tree methods_field = build_decl(this->location_, FIELD_DECL, name_tree,
6002 ptr_type_node);
6003 DECL_CONTEXT(methods_field) = type;
6004 *pp = methods_field;
6005 pp = &DECL_CHAIN(methods_field);
6006
6007 name_tree = get_identifier("__object");
6008 tree field = build_decl(this->location_, FIELD_DECL, name_tree,
6009 ptr_type_node);
6010 DECL_CONTEXT(field) = type;
6011 *pp = field;
6012
6013 TYPE_FIELDS(type) = field_trees;
6014
6015 layout_type(type);
6016
6017 // Build the type of the table of methods.
6018
6019 tree method_table = make_node(RECORD_TYPE);
6020
6021 // The first field is a pointer to the type descriptor.
6022 name_tree = get_identifier("__type_descriptor");
6023 tree dtype = Type::make_type_descriptor_type()->get_tree(gogo);
6024 dtype = build_pointer_type(build_qualified_type(dtype, TYPE_QUAL_CONST));
6025 field = build_decl(this->location_, FIELD_DECL, name_tree, dtype);
6026 DECL_CONTEXT(field) = method_table;
6027 TYPE_FIELDS(method_table) = field;
6028
6029 std::string last_name = "";
6030 pp = &DECL_CHAIN(field);
6031 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6032 p != this->methods_->end();
6033 ++p)
6034 {
6035 std::string name = Gogo::unpack_hidden_name(p->name());
6036 name_tree = get_identifier_with_length(name.data(), name.length());
6037 tree field_type = p->type()->get_tree(gogo);
6038 if (field_type == error_mark_node)
6039 return error_mark_node;
6040 field = build_decl(this->location_, FIELD_DECL, name_tree, field_type);
6041 DECL_CONTEXT(field) = method_table;
6042 *pp = field;
6043 pp = &DECL_CHAIN(field);
6044 // Sanity check: the names should be sorted.
6045 gcc_assert(p->name() > last_name);
6046 last_name = p->name();
6047 }
6048 layout_type(method_table);
6049
6050 // Update the type of the __methods field from a generic pointer to
6051 // a pointer to the method table.
6052 TREE_TYPE(methods_field) = build_pointer_type(method_table);
6053
6054 return type;
6055 }
6056
6057 // Initialization value.
6058
6059 tree
6060 Interface_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
6061 {
6062 if (is_clear)
6063 return NULL;
6064
6065 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
6066 for (tree field = TYPE_FIELDS(type_tree);
6067 field != NULL_TREE;
6068 field = DECL_CHAIN(field))
6069 {
6070 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
6071 elt->index = field;
6072 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
6073 }
6074
6075 tree ret = build_constructor(type_tree, init);
6076 TREE_CONSTANT(ret) = 1;
6077 return ret;
6078 }
6079
6080 // The type of an interface type descriptor.
6081
6082 Type*
6083 Interface_type::make_interface_type_descriptor_type()
6084 {
6085 static Type* ret;
6086 if (ret == NULL)
6087 {
6088 Type* tdt = Type::make_type_descriptor_type();
6089 Type* ptdt = Type::make_type_descriptor_ptr_type();
6090
6091 Type* string_type = Type::lookup_string_type();
6092 Type* pointer_string_type = Type::make_pointer_type(string_type);
6093
6094 Struct_type* sm =
6095 Type::make_builtin_struct_type(3,
6096 "name", pointer_string_type,
6097 "pkgPath", pointer_string_type,
6098 "typ", ptdt);
6099
6100 Type* nsm = Type::make_builtin_named_type("imethod", sm);
6101
6102 Type* slice_nsm = Type::make_array_type(nsm, NULL);
6103
6104 Struct_type* s = Type::make_builtin_struct_type(2,
6105 "", tdt,
6106 "methods", slice_nsm);
6107
6108 ret = Type::make_builtin_named_type("InterfaceType", s);
6109 }
6110
6111 return ret;
6112 }
6113
6114 // Build a type descriptor for an interface type.
6115
6116 Expression*
6117 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6118 {
6119 source_location bloc = BUILTINS_LOCATION;
6120
6121 Type* itdt = Interface_type::make_interface_type_descriptor_type();
6122
6123 const Struct_field_list* ifields = itdt->struct_type()->fields();
6124
6125 Expression_list* ivals = new Expression_list();
6126 ivals->reserve(2);
6127
6128 Struct_field_list::const_iterator pif = ifields->begin();
6129 gcc_assert(pif->field_name() == "commonType");
6130 ivals->push_back(this->type_descriptor_constructor(gogo,
6131 RUNTIME_TYPE_KIND_INTERFACE,
6132 name, NULL, true));
6133
6134 ++pif;
6135 gcc_assert(pif->field_name() == "methods");
6136
6137 Expression_list* methods = new Expression_list();
6138 if (this->methods_ != NULL && !this->methods_->empty())
6139 {
6140 Type* elemtype = pif->type()->array_type()->element_type();
6141
6142 methods->reserve(this->methods_->size());
6143 for (Typed_identifier_list::const_iterator pm = this->methods_->begin();
6144 pm != this->methods_->end();
6145 ++pm)
6146 {
6147 const Struct_field_list* mfields = elemtype->struct_type()->fields();
6148
6149 Expression_list* mvals = new Expression_list();
6150 mvals->reserve(3);
6151
6152 Struct_field_list::const_iterator pmf = mfields->begin();
6153 gcc_assert(pmf->field_name() == "name");
6154 std::string s = Gogo::unpack_hidden_name(pm->name());
6155 Expression* e = Expression::make_string(s, bloc);
6156 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6157
6158 ++pmf;
6159 gcc_assert(pmf->field_name() == "pkgPath");
6160 if (!Gogo::is_hidden_name(pm->name()))
6161 mvals->push_back(Expression::make_nil(bloc));
6162 else
6163 {
6164 s = Gogo::hidden_name_prefix(pm->name());
6165 e = Expression::make_string(s, bloc);
6166 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6167 }
6168
6169 ++pmf;
6170 gcc_assert(pmf->field_name() == "typ");
6171 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
6172
6173 ++pmf;
6174 gcc_assert(pmf == mfields->end());
6175
6176 e = Expression::make_struct_composite_literal(elemtype, mvals,
6177 bloc);
6178 methods->push_back(e);
6179 }
6180 }
6181
6182 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
6183 methods, bloc));
6184
6185 ++pif;
6186 gcc_assert(pif == ifields->end());
6187
6188 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
6189 }
6190
6191 // Reflection string.
6192
6193 void
6194 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
6195 {
6196 ret->append("interface {");
6197 if (this->methods_ != NULL)
6198 {
6199 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6200 p != this->methods_->end();
6201 ++p)
6202 {
6203 if (p != this->methods_->begin())
6204 ret->append(";");
6205 ret->push_back(' ');
6206 ret->append(Gogo::unpack_hidden_name(p->name()));
6207 std::string sub = p->type()->reflection(gogo);
6208 gcc_assert(sub.compare(0, 4, "func") == 0);
6209 sub = sub.substr(4);
6210 ret->append(sub);
6211 }
6212 }
6213 ret->append(" }");
6214 }
6215
6216 // Mangled name.
6217
6218 void
6219 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6220 {
6221 ret->push_back('I');
6222
6223 const Typed_identifier_list* methods = this->methods_;
6224 if (methods != NULL)
6225 {
6226 for (Typed_identifier_list::const_iterator p = methods->begin();
6227 p != methods->end();
6228 ++p)
6229 {
6230 std::string n = Gogo::unpack_hidden_name(p->name());
6231 char buf[20];
6232 snprintf(buf, sizeof buf, "%u_",
6233 static_cast<unsigned int>(n.length()));
6234 ret->append(buf);
6235 ret->append(n);
6236 this->append_mangled_name(p->type(), gogo, ret);
6237 }
6238 }
6239
6240 ret->push_back('e');
6241 }
6242
6243 // Export.
6244
6245 void
6246 Interface_type::do_export(Export* exp) const
6247 {
6248 exp->write_c_string("interface { ");
6249
6250 const Typed_identifier_list* methods = this->methods_;
6251 if (methods != NULL)
6252 {
6253 for (Typed_identifier_list::const_iterator pm = methods->begin();
6254 pm != methods->end();
6255 ++pm)
6256 {
6257 exp->write_string(pm->name());
6258 exp->write_c_string(" (");
6259
6260 const Function_type* fntype = pm->type()->function_type();
6261
6262 bool first = true;
6263 const Typed_identifier_list* parameters = fntype->parameters();
6264 if (parameters != NULL)
6265 {
6266 bool is_varargs = fntype->is_varargs();
6267 for (Typed_identifier_list::const_iterator pp =
6268 parameters->begin();
6269 pp != parameters->end();
6270 ++pp)
6271 {
6272 if (first)
6273 first = false;
6274 else
6275 exp->write_c_string(", ");
6276 if (!is_varargs || pp + 1 != parameters->end())
6277 exp->write_type(pp->type());
6278 else
6279 {
6280 exp->write_c_string("...");
6281 Type *pptype = pp->type();
6282 exp->write_type(pptype->array_type()->element_type());
6283 }
6284 }
6285 }
6286
6287 exp->write_c_string(")");
6288
6289 const Typed_identifier_list* results = fntype->results();
6290 if (results != NULL)
6291 {
6292 exp->write_c_string(" ");
6293 if (results->size() == 1)
6294 exp->write_type(results->begin()->type());
6295 else
6296 {
6297 first = true;
6298 exp->write_c_string("(");
6299 for (Typed_identifier_list::const_iterator p =
6300 results->begin();
6301 p != results->end();
6302 ++p)
6303 {
6304 if (first)
6305 first = false;
6306 else
6307 exp->write_c_string(", ");
6308 exp->write_type(p->type());
6309 }
6310 exp->write_c_string(")");
6311 }
6312 }
6313
6314 exp->write_c_string("; ");
6315 }
6316 }
6317
6318 exp->write_c_string("}");
6319 }
6320
6321 // Import an interface type.
6322
6323 Interface_type*
6324 Interface_type::do_import(Import* imp)
6325 {
6326 imp->require_c_string("interface { ");
6327
6328 Typed_identifier_list* methods = new Typed_identifier_list;
6329 while (imp->peek_char() != '}')
6330 {
6331 std::string name = imp->read_identifier();
6332 imp->require_c_string(" (");
6333
6334 Typed_identifier_list* parameters;
6335 bool is_varargs = false;
6336 if (imp->peek_char() == ')')
6337 parameters = NULL;
6338 else
6339 {
6340 parameters = new Typed_identifier_list;
6341 while (true)
6342 {
6343 if (imp->match_c_string("..."))
6344 {
6345 imp->advance(3);
6346 is_varargs = true;
6347 }
6348
6349 Type* ptype = imp->read_type();
6350 if (is_varargs)
6351 ptype = Type::make_array_type(ptype, NULL);
6352 parameters->push_back(Typed_identifier(Import::import_marker,
6353 ptype, imp->location()));
6354 if (imp->peek_char() != ',')
6355 break;
6356 gcc_assert(!is_varargs);
6357 imp->require_c_string(", ");
6358 }
6359 }
6360 imp->require_c_string(")");
6361
6362 Typed_identifier_list* results;
6363 if (imp->peek_char() != ' ')
6364 results = NULL;
6365 else
6366 {
6367 results = new Typed_identifier_list;
6368 imp->advance(1);
6369 if (imp->peek_char() != '(')
6370 {
6371 Type* rtype = imp->read_type();
6372 results->push_back(Typed_identifier(Import::import_marker,
6373 rtype, imp->location()));
6374 }
6375 else
6376 {
6377 imp->advance(1);
6378 while (true)
6379 {
6380 Type* rtype = imp->read_type();
6381 results->push_back(Typed_identifier(Import::import_marker,
6382 rtype, imp->location()));
6383 if (imp->peek_char() != ',')
6384 break;
6385 imp->require_c_string(", ");
6386 }
6387 imp->require_c_string(")");
6388 }
6389 }
6390
6391 Function_type* fntype = Type::make_function_type(NULL, parameters,
6392 results,
6393 imp->location());
6394 if (is_varargs)
6395 fntype->set_is_varargs();
6396 methods->push_back(Typed_identifier(name, fntype, imp->location()));
6397
6398 imp->require_c_string("; ");
6399 }
6400
6401 imp->require_c_string("}");
6402
6403 if (methods->empty())
6404 {
6405 delete methods;
6406 methods = NULL;
6407 }
6408
6409 return Type::make_interface_type(methods, imp->location());
6410 }
6411
6412 // Make an interface type.
6413
6414 Interface_type*
6415 Type::make_interface_type(Typed_identifier_list* methods,
6416 source_location location)
6417 {
6418 return new Interface_type(methods, location);
6419 }
6420
6421 // Class Method.
6422
6423 // Bind a method to an object.
6424
6425 Expression*
6426 Method::bind_method(Expression* expr, source_location location) const
6427 {
6428 if (this->stub_ == NULL)
6429 {
6430 // When there is no stub object, the binding is determined by
6431 // the child class.
6432 return this->do_bind_method(expr, location);
6433 }
6434
6435 Expression* func = Expression::make_func_reference(this->stub_, NULL,
6436 location);
6437 return Expression::make_bound_method(expr, func, location);
6438 }
6439
6440 // Return the named object associated with a method. This may only be
6441 // called after methods are finalized.
6442
6443 Named_object*
6444 Method::named_object() const
6445 {
6446 if (this->stub_ != NULL)
6447 return this->stub_;
6448 return this->do_named_object();
6449 }
6450
6451 // Class Named_method.
6452
6453 // The type of the method.
6454
6455 Function_type*
6456 Named_method::do_type() const
6457 {
6458 if (this->named_object_->is_function())
6459 return this->named_object_->func_value()->type();
6460 else if (this->named_object_->is_function_declaration())
6461 return this->named_object_->func_declaration_value()->type();
6462 else
6463 gcc_unreachable();
6464 }
6465
6466 // Return the location of the method receiver.
6467
6468 source_location
6469 Named_method::do_receiver_location() const
6470 {
6471 return this->do_type()->receiver()->location();
6472 }
6473
6474 // Bind a method to an object.
6475
6476 Expression*
6477 Named_method::do_bind_method(Expression* expr, source_location location) const
6478 {
6479 Expression* func = Expression::make_func_reference(this->named_object_, NULL,
6480 location);
6481 Bound_method_expression* bme = Expression::make_bound_method(expr, func,
6482 location);
6483 // If this is not a local method, and it does not use a stub, then
6484 // the real method expects a different type. We need to cast the
6485 // first argument.
6486 if (this->depth() > 0 && !this->needs_stub_method())
6487 {
6488 Function_type* ftype = this->do_type();
6489 gcc_assert(ftype->is_method());
6490 Type* frtype = ftype->receiver()->type();
6491 bme->set_first_argument_type(frtype);
6492 }
6493 return bme;
6494 }
6495
6496 // Class Interface_method.
6497
6498 // Bind a method to an object.
6499
6500 Expression*
6501 Interface_method::do_bind_method(Expression* expr,
6502 source_location location) const
6503 {
6504 return Expression::make_interface_field_reference(expr, this->name_,
6505 location);
6506 }
6507
6508 // Class Methods.
6509
6510 // Insert a new method. Return true if it was inserted, false
6511 // otherwise.
6512
6513 bool
6514 Methods::insert(const std::string& name, Method* m)
6515 {
6516 std::pair<Method_map::iterator, bool> ins =
6517 this->methods_.insert(std::make_pair(name, m));
6518 if (ins.second)
6519 return true;
6520 else
6521 {
6522 Method* old_method = ins.first->second;
6523 if (m->depth() < old_method->depth())
6524 {
6525 delete old_method;
6526 ins.first->second = m;
6527 return true;
6528 }
6529 else
6530 {
6531 if (m->depth() == old_method->depth())
6532 old_method->set_is_ambiguous();
6533 return false;
6534 }
6535 }
6536 }
6537
6538 // Return the number of unambiguous methods.
6539
6540 size_t
6541 Methods::count() const
6542 {
6543 size_t ret = 0;
6544 for (Method_map::const_iterator p = this->methods_.begin();
6545 p != this->methods_.end();
6546 ++p)
6547 if (!p->second->is_ambiguous())
6548 ++ret;
6549 return ret;
6550 }
6551
6552 // Class Named_type.
6553
6554 // Return the name of the type.
6555
6556 const std::string&
6557 Named_type::name() const
6558 {
6559 return this->named_object_->name();
6560 }
6561
6562 // Return the name of the type to use in an error message.
6563
6564 std::string
6565 Named_type::message_name() const
6566 {
6567 return this->named_object_->message_name();
6568 }
6569
6570 // Return the base type for this type. We have to be careful about
6571 // circular type definitions, which are invalid but may be seen here.
6572
6573 Type*
6574 Named_type::named_base()
6575 {
6576 if (this->seen_ > 0)
6577 return this;
6578 ++this->seen_;
6579 Type* ret = this->type_->base();
6580 --this->seen_;
6581 return ret;
6582 }
6583
6584 const Type*
6585 Named_type::named_base() const
6586 {
6587 if (this->seen_ > 0)
6588 return this;
6589 ++this->seen_;
6590 const Type* ret = this->type_->base();
6591 --this->seen_;
6592 return ret;
6593 }
6594
6595 // Return whether this is an error type. We have to be careful about
6596 // circular type definitions, which are invalid but may be seen here.
6597
6598 bool
6599 Named_type::is_named_error_type() const
6600 {
6601 if (this->seen_ > 0)
6602 return false;
6603 ++this->seen_;
6604 bool ret = this->type_->is_error_type();
6605 --this->seen_;
6606 return ret;
6607 }
6608
6609 // Add a method to this type.
6610
6611 Named_object*
6612 Named_type::add_method(const std::string& name, Function* function)
6613 {
6614 if (this->local_methods_ == NULL)
6615 this->local_methods_ = new Bindings(NULL);
6616 return this->local_methods_->add_function(name, NULL, function);
6617 }
6618
6619 // Add a method declaration to this type.
6620
6621 Named_object*
6622 Named_type::add_method_declaration(const std::string& name, Package* package,
6623 Function_type* type,
6624 source_location location)
6625 {
6626 if (this->local_methods_ == NULL)
6627 this->local_methods_ = new Bindings(NULL);
6628 return this->local_methods_->add_function_declaration(name, package, type,
6629 location);
6630 }
6631
6632 // Add an existing method to this type.
6633
6634 void
6635 Named_type::add_existing_method(Named_object* no)
6636 {
6637 if (this->local_methods_ == NULL)
6638 this->local_methods_ = new Bindings(NULL);
6639 this->local_methods_->add_named_object(no);
6640 }
6641
6642 // Look for a local method NAME, and returns its named object, or NULL
6643 // if not there.
6644
6645 Named_object*
6646 Named_type::find_local_method(const std::string& name) const
6647 {
6648 if (this->local_methods_ == NULL)
6649 return NULL;
6650 return this->local_methods_->lookup(name);
6651 }
6652
6653 // Return whether NAME is an unexported field or method, for better
6654 // error reporting.
6655
6656 bool
6657 Named_type::is_unexported_local_method(Gogo* gogo,
6658 const std::string& name) const
6659 {
6660 Bindings* methods = this->local_methods_;
6661 if (methods != NULL)
6662 {
6663 for (Bindings::const_declarations_iterator p =
6664 methods->begin_declarations();
6665 p != methods->end_declarations();
6666 ++p)
6667 {
6668 if (Gogo::is_hidden_name(p->first)
6669 && name == Gogo::unpack_hidden_name(p->first)
6670 && gogo->pack_hidden_name(name, false) != p->first)
6671 return true;
6672 }
6673 }
6674 return false;
6675 }
6676
6677 // Build the complete list of methods for this type, which means
6678 // recursively including all methods for anonymous fields. Create all
6679 // stub methods.
6680
6681 void
6682 Named_type::finalize_methods(Gogo* gogo)
6683 {
6684 if (this->all_methods_ != NULL)
6685 return;
6686
6687 if (this->local_methods_ != NULL
6688 && (this->points_to() != NULL || this->interface_type() != NULL))
6689 {
6690 const Bindings* lm = this->local_methods_;
6691 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
6692 p != lm->end_declarations();
6693 ++p)
6694 error_at(p->second->location(),
6695 "invalid pointer or interface receiver type");
6696 delete this->local_methods_;
6697 this->local_methods_ = NULL;
6698 return;
6699 }
6700
6701 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
6702 }
6703
6704 // Return the method NAME, or NULL if there isn't one or if it is
6705 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6706 // ambiguous.
6707
6708 Method*
6709 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
6710 {
6711 return Type::method_function(this->all_methods_, name, is_ambiguous);
6712 }
6713
6714 // Return a pointer to the interface method table for this type for
6715 // the interface INTERFACE. IS_POINTER is true if this is for a
6716 // pointer to THIS.
6717
6718 tree
6719 Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
6720 bool is_pointer)
6721 {
6722 gcc_assert(!interface->is_empty());
6723
6724 Interface_method_tables** pimt = (is_pointer
6725 ? &this->interface_method_tables_
6726 : &this->pointer_interface_method_tables_);
6727
6728 if (*pimt == NULL)
6729 *pimt = new Interface_method_tables(5);
6730
6731 std::pair<const Interface_type*, tree> val(interface, NULL_TREE);
6732 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
6733
6734 if (ins.second)
6735 {
6736 // This is a new entry in the hash table.
6737 gcc_assert(ins.first->second == NULL_TREE);
6738 ins.first->second = gogo->interface_method_table_for_type(interface,
6739 this,
6740 is_pointer);
6741 }
6742
6743 tree decl = ins.first->second;
6744 if (decl == error_mark_node)
6745 return error_mark_node;
6746 gcc_assert(decl != NULL_TREE && TREE_CODE(decl) == VAR_DECL);
6747 return build_fold_addr_expr(decl);
6748 }
6749
6750 // Return whether a named type has any hidden fields.
6751
6752 bool
6753 Named_type::named_type_has_hidden_fields(std::string* reason) const
6754 {
6755 if (this->seen_ > 0)
6756 return false;
6757 ++this->seen_;
6758 bool ret = this->type_->has_hidden_fields(this, reason);
6759 --this->seen_;
6760 return ret;
6761 }
6762
6763 // Look for a use of a complete type within another type. This is
6764 // used to check that we don't try to use a type within itself.
6765
6766 class Find_type_use : public Traverse
6767 {
6768 public:
6769 Find_type_use(Type* find_type)
6770 : Traverse(traverse_types),
6771 find_type_(find_type), found_(false)
6772 { }
6773
6774 // Whether we found the type.
6775 bool
6776 found() const
6777 { return this->found_; }
6778
6779 protected:
6780 int
6781 type(Type*);
6782
6783 private:
6784 // The type we are looking for.
6785 Type* find_type_;
6786 // Whether we found the type.
6787 bool found_;
6788 };
6789
6790 // Check for FIND_TYPE in TYPE.
6791
6792 int
6793 Find_type_use::type(Type* type)
6794 {
6795 if (this->find_type_ == type)
6796 {
6797 this->found_ = true;
6798 return TRAVERSE_EXIT;
6799 }
6800 // It's OK if we see a reference to the type in any type which is
6801 // essentially a pointer: a pointer, a slice, a function, a map, or
6802 // a channel.
6803 if (type->points_to() != NULL
6804 || type->is_open_array_type()
6805 || type->function_type() != NULL
6806 || type->map_type() != NULL
6807 || type->channel_type() != NULL)
6808 return TRAVERSE_SKIP_COMPONENTS;
6809
6810 // For an interface, a reference to the type in a method type should
6811 // be ignored, but we have to consider direct inheritance. When
6812 // this is called, there may be cases of direct inheritance
6813 // represented as a method with no name.
6814 if (type->interface_type() != NULL)
6815 {
6816 const Typed_identifier_list* methods = type->interface_type()->methods();
6817 if (methods != NULL)
6818 {
6819 for (Typed_identifier_list::const_iterator p = methods->begin();
6820 p != methods->end();
6821 ++p)
6822 {
6823 if (p->name().empty())
6824 {
6825 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
6826 return TRAVERSE_EXIT;
6827 }
6828 }
6829 }
6830 return TRAVERSE_SKIP_COMPONENTS;
6831 }
6832
6833 return TRAVERSE_CONTINUE;
6834 }
6835
6836 // Verify that a named type does not refer to itself.
6837
6838 bool
6839 Named_type::do_verify()
6840 {
6841 Find_type_use find(this);
6842 Type::traverse(this->type_, &find);
6843 if (find.found())
6844 {
6845 error_at(this->location_, "invalid recursive type %qs",
6846 this->message_name().c_str());
6847 this->is_error_ = true;
6848 return false;
6849 }
6850
6851 // Check whether any of the local methods overloads an existing
6852 // struct field or interface method. We don't need to check the
6853 // list of methods against itself: that is handled by the Bindings
6854 // code.
6855 if (this->local_methods_ != NULL)
6856 {
6857 Struct_type* st = this->type_->struct_type();
6858 Interface_type* it = this->type_->interface_type();
6859 bool found_dup = false;
6860 if (st != NULL || it != NULL)
6861 {
6862 for (Bindings::const_declarations_iterator p =
6863 this->local_methods_->begin_declarations();
6864 p != this->local_methods_->end_declarations();
6865 ++p)
6866 {
6867 const std::string& name(p->first);
6868 if (st != NULL && st->find_local_field(name, NULL) != NULL)
6869 {
6870 error_at(p->second->location(),
6871 "method %qs redeclares struct field name",
6872 Gogo::message_name(name).c_str());
6873 found_dup = true;
6874 }
6875 if (it != NULL && it->find_method(name) != NULL)
6876 {
6877 error_at(p->second->location(),
6878 "method %qs redeclares interface method name",
6879 Gogo::message_name(name).c_str());
6880 found_dup = true;
6881 }
6882 }
6883 }
6884 if (found_dup)
6885 return false;
6886 }
6887
6888 // If this is a struct, then if any of the fields of the struct
6889 // themselves have struct type, then this struct must be converted
6890 // to the backend representation before the field's type is
6891 // converted. That may seem backward, but it works because if the
6892 // field's type refers to this one, e.g., via a pointer, then the
6893 // conversion process will pick up the half-built struct and do the
6894 // right thing.
6895 if (this->struct_type() != NULL)
6896 {
6897 const Struct_field_list* fields = this->struct_type()->fields();
6898 for (Struct_field_list::const_iterator p = fields->begin();
6899 p != fields->end();
6900 ++p)
6901 {
6902 Struct_type* st = p->type()->struct_type();
6903 if (st != NULL)
6904 st->add_prerequisite(this);
6905 }
6906 }
6907
6908 return true;
6909 }
6910
6911 // Return whether this type is or contains a pointer.
6912
6913 bool
6914 Named_type::do_has_pointer() const
6915 {
6916 if (this->seen_ > 0)
6917 return false;
6918 ++this->seen_;
6919 bool ret = this->type_->has_pointer();
6920 --this->seen_;
6921 return ret;
6922 }
6923
6924 // Return a hash code. This is used for method lookup. We simply
6925 // hash on the name itself.
6926
6927 unsigned int
6928 Named_type::do_hash_for_method(Gogo* gogo) const
6929 {
6930 const std::string& name(this->named_object()->name());
6931 unsigned int ret = Type::hash_string(name, 0);
6932
6933 // GOGO will be NULL here when called from Type_hash_identical.
6934 // That is OK because that is only used for internal hash tables
6935 // where we are going to be comparing named types for equality. In
6936 // other cases, which are cases where the runtime is going to
6937 // compare hash codes to see if the types are the same, we need to
6938 // include the package prefix and name in the hash.
6939 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
6940 {
6941 const Package* package = this->named_object()->package();
6942 if (package == NULL)
6943 {
6944 ret = Type::hash_string(gogo->unique_prefix(), ret);
6945 ret = Type::hash_string(gogo->package_name(), ret);
6946 }
6947 else
6948 {
6949 ret = Type::hash_string(package->unique_prefix(), ret);
6950 ret = Type::hash_string(package->name(), ret);
6951 }
6952 }
6953
6954 return ret;
6955 }
6956
6957 // Get a tree for a named type.
6958
6959 tree
6960 Named_type::do_get_tree(Gogo* gogo)
6961 {
6962 if (this->is_error_)
6963 return error_mark_node;
6964
6965 // Go permits types to refer to themselves in various ways. Break
6966 // the recursion here.
6967 tree t;
6968 switch (this->type_->forwarded()->classification())
6969 {
6970 case TYPE_ERROR:
6971 return error_mark_node;
6972
6973 case TYPE_VOID:
6974 case TYPE_BOOLEAN:
6975 case TYPE_INTEGER:
6976 case TYPE_FLOAT:
6977 case TYPE_COMPLEX:
6978 case TYPE_STRING:
6979 case TYPE_NIL:
6980 // These types can not refer to themselves.
6981 case TYPE_MAP:
6982 case TYPE_CHANNEL:
6983 // All maps and channels have the same type in GENERIC.
6984 t = Type::get_named_type_tree(gogo, this->type_);
6985 if (t == error_mark_node)
6986 return error_mark_node;
6987 // Build a copy to set TYPE_NAME.
6988 t = build_variant_type_copy(t);
6989 break;
6990
6991 case TYPE_FUNCTION:
6992 // GENERIC can't handle a pointer to a function type whose
6993 // return type is a pointer to the function type itself. It
6994 // goes into an infinite loop when walking the types.
6995 if (this->seen_ > 0)
6996 {
6997 Function_type* fntype = this->type_->function_type();
6998 if (fntype->results() != NULL
6999 && fntype->results()->size() == 1
7000 && fntype->results()->front().type()->forwarded() == this)
7001 return ptr_type_node;
7002
7003 // We can legitimately see ourselves here twice when a named
7004 // type is defined using a struct which refers to the named
7005 // type. If we see ourselves too often we are in a loop.
7006 if (this->seen_ > 3)
7007 return ptr_type_node;
7008 }
7009 ++this->seen_;
7010 t = Type::get_named_type_tree(gogo, this->type_);
7011 --this->seen_;
7012 if (t == error_mark_node)
7013 return error_mark_node;
7014 t = build_variant_type_copy(t);
7015 break;
7016
7017 case TYPE_POINTER:
7018 // Don't recur infinitely if a pointer type refers to itself.
7019 // Ideally we would build a circular data structure here, but
7020 // GENERIC can't handle them.
7021 if (this->seen_ > 0)
7022 {
7023 if (this->type_->points_to()->forwarded() == this)
7024 return ptr_type_node;
7025
7026 if (this->seen_ > 3)
7027 return ptr_type_node;
7028 }
7029 ++this->seen_;
7030 t = Type::get_named_type_tree(gogo, this->type_);
7031 --this->seen_;
7032 if (t == error_mark_node)
7033 return error_mark_node;
7034 t = build_variant_type_copy(t);
7035 break;
7036
7037 case TYPE_STRUCT:
7038 // If there are structs which must be converted first, do them.
7039 if (this->seen_ == 0)
7040 {
7041 ++this->seen_;
7042 this->type_->struct_type()->convert_prerequisites(gogo);
7043 --this->seen_;
7044 }
7045
7046 if (this->named_tree_ != NULL_TREE)
7047 return this->named_tree_;
7048
7049 t = make_node(RECORD_TYPE);
7050 this->named_tree_ = t;
7051 t = this->type_->struct_type()->fill_in_tree(gogo, t);
7052 if (t == error_mark_node)
7053 return error_mark_node;
7054 break;
7055
7056 case TYPE_ARRAY:
7057 if (!this->is_open_array_type())
7058 t = Type::get_named_type_tree(gogo, this->type_);
7059 else
7060 {
7061 if (this->named_tree_ != NULL_TREE)
7062 return this->named_tree_;
7063 t = gogo->slice_type_tree(void_type_node);
7064 this->named_tree_ = t;
7065 t = this->type_->array_type()->fill_in_tree(gogo, t);
7066 }
7067 if (t == error_mark_node)
7068 return error_mark_node;
7069 t = build_variant_type_copy(t);
7070 break;
7071
7072 case TYPE_INTERFACE:
7073 if (this->type_->interface_type()->is_empty())
7074 {
7075 t = Type::get_named_type_tree(gogo, this->type_);
7076 if (t == error_mark_node)
7077 return error_mark_node;
7078 t = build_variant_type_copy(t);
7079 }
7080 else
7081 {
7082 if (this->named_tree_ != NULL_TREE)
7083 return this->named_tree_;
7084 t = make_node(RECORD_TYPE);
7085 this->named_tree_ = t;
7086 t = this->type_->interface_type()->fill_in_tree(gogo, t);
7087 if (t == error_mark_node)
7088 return error_mark_node;
7089 }
7090 break;
7091
7092 case TYPE_NAMED:
7093 {
7094 // When a named type T1 is defined as another named type T2,
7095 // the definition must simply be "type T1 T2". If the
7096 // definition of T2 may refer to T1, then we must simply
7097 // return the type for T2 here. It's not precisely correct,
7098 // but it's as close as we can get with GENERIC.
7099 ++this->seen_;
7100 t = Type::get_named_type_tree(gogo, this->type_);
7101 --this->seen_;
7102 if (this->seen_ > 0)
7103 return t;
7104 if (t == error_mark_node)
7105 return error_mark_node;
7106 t = build_variant_type_copy(t);
7107 }
7108 break;
7109
7110 case TYPE_FORWARD:
7111 // An undefined forwarding type. Make sure the error is
7112 // emitted.
7113 this->type_->forward_declaration_type()->real_type();
7114 return error_mark_node;
7115
7116 default:
7117 case TYPE_SINK:
7118 case TYPE_CALL_MULTIPLE_RESULT:
7119 gcc_unreachable();
7120 }
7121
7122 tree id = this->named_object_->get_id(gogo);
7123 tree decl = build_decl(this->location_, TYPE_DECL, id, t);
7124 TYPE_NAME(t) = decl;
7125
7126 return t;
7127 }
7128
7129 // Build a type descriptor for a named type.
7130
7131 Expression*
7132 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7133 {
7134 // If NAME is not NULL, then we don't really want the type
7135 // descriptor for this type; we want the descriptor for the
7136 // underlying type, giving it the name NAME.
7137 return this->named_type_descriptor(gogo, this->type_,
7138 name == NULL ? this : name);
7139 }
7140
7141 // Add to the reflection string. This is used mostly for the name of
7142 // the type used in a type descriptor, not for actual reflection
7143 // strings.
7144
7145 void
7146 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
7147 {
7148 if (this->location() != BUILTINS_LOCATION)
7149 {
7150 const Package* package = this->named_object_->package();
7151 if (package != NULL)
7152 ret->append(package->name());
7153 else
7154 ret->append(gogo->package_name());
7155 ret->push_back('.');
7156 }
7157 if (this->in_function_ != NULL)
7158 {
7159 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
7160 ret->push_back('$');
7161 }
7162 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
7163 }
7164
7165 // Get the mangled name.
7166
7167 void
7168 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
7169 {
7170 Named_object* no = this->named_object_;
7171 std::string name;
7172 if (this->location() == BUILTINS_LOCATION)
7173 gcc_assert(this->in_function_ == NULL);
7174 else
7175 {
7176 const std::string& unique_prefix(no->package() == NULL
7177 ? gogo->unique_prefix()
7178 : no->package()->unique_prefix());
7179 const std::string& package_name(no->package() == NULL
7180 ? gogo->package_name()
7181 : no->package()->name());
7182 name = unique_prefix;
7183 name.append(1, '.');
7184 name.append(package_name);
7185 name.append(1, '.');
7186 if (this->in_function_ != NULL)
7187 {
7188 name.append(Gogo::unpack_hidden_name(this->in_function_->name()));
7189 name.append(1, '$');
7190 }
7191 }
7192 name.append(Gogo::unpack_hidden_name(no->name()));
7193 char buf[20];
7194 snprintf(buf, sizeof buf, "N%u_", static_cast<unsigned int>(name.length()));
7195 ret->append(buf);
7196 ret->append(name);
7197 }
7198
7199 // Export the type. This is called to export a global type.
7200
7201 void
7202 Named_type::export_named_type(Export* exp, const std::string&) const
7203 {
7204 // We don't need to write the name of the type here, because it will
7205 // be written by Export::write_type anyhow.
7206 exp->write_c_string("type ");
7207 exp->write_type(this);
7208 exp->write_c_string(";\n");
7209 }
7210
7211 // Import a named type.
7212
7213 void
7214 Named_type::import_named_type(Import* imp, Named_type** ptype)
7215 {
7216 imp->require_c_string("type ");
7217 Type *type = imp->read_type();
7218 *ptype = type->named_type();
7219 gcc_assert(*ptype != NULL);
7220 imp->require_c_string(";\n");
7221 }
7222
7223 // Export the type when it is referenced by another type. In this
7224 // case Export::export_type will already have issued the name.
7225
7226 void
7227 Named_type::do_export(Export* exp) const
7228 {
7229 exp->write_type(this->type_);
7230
7231 // To save space, we only export the methods directly attached to
7232 // this type.
7233 Bindings* methods = this->local_methods_;
7234 if (methods == NULL)
7235 return;
7236
7237 exp->write_c_string("\n");
7238 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
7239 p != methods->end_definitions();
7240 ++p)
7241 {
7242 exp->write_c_string(" ");
7243 (*p)->export_named_object(exp);
7244 }
7245
7246 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
7247 p != methods->end_declarations();
7248 ++p)
7249 {
7250 if (p->second->is_function_declaration())
7251 {
7252 exp->write_c_string(" ");
7253 p->second->export_named_object(exp);
7254 }
7255 }
7256 }
7257
7258 // Make a named type.
7259
7260 Named_type*
7261 Type::make_named_type(Named_object* named_object, Type* type,
7262 source_location location)
7263 {
7264 return new Named_type(named_object, type, location);
7265 }
7266
7267 // Finalize the methods for TYPE. It will be a named type or a struct
7268 // type. This sets *ALL_METHODS to the list of methods, and builds
7269 // all required stubs.
7270
7271 void
7272 Type::finalize_methods(Gogo* gogo, const Type* type, source_location location,
7273 Methods** all_methods)
7274 {
7275 *all_methods = NULL;
7276 Types_seen types_seen;
7277 Type::add_methods_for_type(type, NULL, 0, false, false, &types_seen,
7278 all_methods);
7279 Type::build_stub_methods(gogo, type, *all_methods, location);
7280 }
7281
7282 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
7283 // build up the struct field indexes as we go. DEPTH is the depth of
7284 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
7285 // adding these methods for an anonymous field with pointer type.
7286 // NEEDS_STUB_METHOD is true if we need to use a stub method which
7287 // calls the real method. TYPES_SEEN is used to avoid infinite
7288 // recursion.
7289
7290 void
7291 Type::add_methods_for_type(const Type* type,
7292 const Method::Field_indexes* field_indexes,
7293 unsigned int depth,
7294 bool is_embedded_pointer,
7295 bool needs_stub_method,
7296 Types_seen* types_seen,
7297 Methods** methods)
7298 {
7299 // Pointer types may not have methods.
7300 if (type->points_to() != NULL)
7301 return;
7302
7303 const Named_type* nt = type->named_type();
7304 if (nt != NULL)
7305 {
7306 std::pair<Types_seen::iterator, bool> ins = types_seen->insert(nt);
7307 if (!ins.second)
7308 return;
7309 }
7310
7311 if (nt != NULL)
7312 Type::add_local_methods_for_type(nt, field_indexes, depth,
7313 is_embedded_pointer, needs_stub_method,
7314 methods);
7315
7316 Type::add_embedded_methods_for_type(type, field_indexes, depth,
7317 is_embedded_pointer, needs_stub_method,
7318 types_seen, methods);
7319
7320 // If we are called with depth > 0, then we are looking at an
7321 // anonymous field of a struct. If such a field has interface type,
7322 // then we need to add the interface methods. We don't want to add
7323 // them when depth == 0, because we will already handle them
7324 // following the usual rules for an interface type.
7325 if (depth > 0)
7326 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
7327 }
7328
7329 // Add the local methods for the named type NT to *METHODS. The
7330 // parameters are as for add_methods_to_type.
7331
7332 void
7333 Type::add_local_methods_for_type(const Named_type* nt,
7334 const Method::Field_indexes* field_indexes,
7335 unsigned int depth,
7336 bool is_embedded_pointer,
7337 bool needs_stub_method,
7338 Methods** methods)
7339 {
7340 const Bindings* local_methods = nt->local_methods();
7341 if (local_methods == NULL)
7342 return;
7343
7344 if (*methods == NULL)
7345 *methods = new Methods();
7346
7347 for (Bindings::const_declarations_iterator p =
7348 local_methods->begin_declarations();
7349 p != local_methods->end_declarations();
7350 ++p)
7351 {
7352 Named_object* no = p->second;
7353 bool is_value_method = (is_embedded_pointer
7354 || !Type::method_expects_pointer(no));
7355 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
7356 (needs_stub_method
7357 || (depth > 0 && is_value_method)));
7358 if (!(*methods)->insert(no->name(), m))
7359 delete m;
7360 }
7361 }
7362
7363 // Add the embedded methods for TYPE to *METHODS. These are the
7364 // methods attached to anonymous fields. The parameters are as for
7365 // add_methods_to_type.
7366
7367 void
7368 Type::add_embedded_methods_for_type(const Type* type,
7369 const Method::Field_indexes* field_indexes,
7370 unsigned int depth,
7371 bool is_embedded_pointer,
7372 bool needs_stub_method,
7373 Types_seen* types_seen,
7374 Methods** methods)
7375 {
7376 // Look for anonymous fields in TYPE. TYPE has fields if it is a
7377 // struct.
7378 const Struct_type* st = type->struct_type();
7379 if (st == NULL)
7380 return;
7381
7382 const Struct_field_list* fields = st->fields();
7383 if (fields == NULL)
7384 return;
7385
7386 unsigned int i = 0;
7387 for (Struct_field_list::const_iterator pf = fields->begin();
7388 pf != fields->end();
7389 ++pf, ++i)
7390 {
7391 if (!pf->is_anonymous())
7392 continue;
7393
7394 Type* ftype = pf->type();
7395 bool is_pointer = false;
7396 if (ftype->points_to() != NULL)
7397 {
7398 ftype = ftype->points_to();
7399 is_pointer = true;
7400 }
7401 Named_type* fnt = ftype->named_type();
7402 if (fnt == NULL)
7403 {
7404 // This is an error, but it will be diagnosed elsewhere.
7405 continue;
7406 }
7407
7408 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
7409 sub_field_indexes->next = field_indexes;
7410 sub_field_indexes->field_index = i;
7411
7412 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
7413 (is_embedded_pointer || is_pointer),
7414 (needs_stub_method
7415 || is_pointer
7416 || i > 0),
7417 types_seen,
7418 methods);
7419 }
7420 }
7421
7422 // If TYPE is an interface type, then add its method to *METHODS.
7423 // This is for interface methods attached to an anonymous field. The
7424 // parameters are as for add_methods_for_type.
7425
7426 void
7427 Type::add_interface_methods_for_type(const Type* type,
7428 const Method::Field_indexes* field_indexes,
7429 unsigned int depth,
7430 Methods** methods)
7431 {
7432 const Interface_type* it = type->interface_type();
7433 if (it == NULL)
7434 return;
7435
7436 const Typed_identifier_list* imethods = it->methods();
7437 if (imethods == NULL)
7438 return;
7439
7440 if (*methods == NULL)
7441 *methods = new Methods();
7442
7443 for (Typed_identifier_list::const_iterator pm = imethods->begin();
7444 pm != imethods->end();
7445 ++pm)
7446 {
7447 Function_type* fntype = pm->type()->function_type();
7448 gcc_assert(fntype != NULL && !fntype->is_method());
7449 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
7450 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
7451 field_indexes, depth);
7452 if (!(*methods)->insert(pm->name(), m))
7453 delete m;
7454 }
7455 }
7456
7457 // Build stub methods for TYPE as needed. METHODS is the set of
7458 // methods for the type. A stub method may be needed when a type
7459 // inherits a method from an anonymous field. When we need the
7460 // address of the method, as in a type descriptor, we need to build a
7461 // little stub which does the required field dereferences and jumps to
7462 // the real method. LOCATION is the location of the type definition.
7463
7464 void
7465 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
7466 source_location location)
7467 {
7468 if (methods == NULL)
7469 return;
7470 for (Methods::const_iterator p = methods->begin();
7471 p != methods->end();
7472 ++p)
7473 {
7474 Method* m = p->second;
7475 if (m->is_ambiguous() || !m->needs_stub_method())
7476 continue;
7477
7478 const std::string& name(p->first);
7479
7480 // Build a stub method.
7481
7482 const Function_type* fntype = m->type();
7483
7484 static unsigned int counter;
7485 char buf[100];
7486 snprintf(buf, sizeof buf, "$this%u", counter);
7487 ++counter;
7488
7489 Type* receiver_type = const_cast<Type*>(type);
7490 if (!m->is_value_method())
7491 receiver_type = Type::make_pointer_type(receiver_type);
7492 source_location receiver_location = m->receiver_location();
7493 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
7494 receiver_location);
7495
7496 const Typed_identifier_list* fnparams = fntype->parameters();
7497 Typed_identifier_list* stub_params;
7498 if (fnparams == NULL || fnparams->empty())
7499 stub_params = NULL;
7500 else
7501 {
7502 // We give each stub parameter a unique name.
7503 stub_params = new Typed_identifier_list();
7504 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
7505 pp != fnparams->end();
7506 ++pp)
7507 {
7508 char pbuf[100];
7509 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
7510 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
7511 pp->location()));
7512 ++counter;
7513 }
7514 }
7515
7516 const Typed_identifier_list* fnresults = fntype->results();
7517 Typed_identifier_list* stub_results;
7518 if (fnresults == NULL || fnresults->empty())
7519 stub_results = NULL;
7520 else
7521 {
7522 // We create the result parameters without any names, since
7523 // we won't refer to them.
7524 stub_results = new Typed_identifier_list();
7525 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
7526 pr != fnresults->end();
7527 ++pr)
7528 stub_results->push_back(Typed_identifier("", pr->type(),
7529 pr->location()));
7530 }
7531
7532 Function_type* stub_type = Type::make_function_type(receiver,
7533 stub_params,
7534 stub_results,
7535 fntype->location());
7536 if (fntype->is_varargs())
7537 stub_type->set_is_varargs();
7538
7539 // We only create the function in the package which creates the
7540 // type.
7541 const Package* package;
7542 if (type->named_type() == NULL)
7543 package = NULL;
7544 else
7545 package = type->named_type()->named_object()->package();
7546 Named_object* stub;
7547 if (package != NULL)
7548 stub = Named_object::make_function_declaration(name, package,
7549 stub_type, location);
7550 else
7551 {
7552 stub = gogo->start_function(name, stub_type, false,
7553 fntype->location());
7554 Type::build_one_stub_method(gogo, m, buf, stub_params,
7555 fntype->is_varargs(), location);
7556 gogo->finish_function(fntype->location());
7557 }
7558
7559 m->set_stub_object(stub);
7560 }
7561 }
7562
7563 // Build a stub method which adjusts the receiver as required to call
7564 // METHOD. RECEIVER_NAME is the name we used for the receiver.
7565 // PARAMS is the list of function parameters.
7566
7567 void
7568 Type::build_one_stub_method(Gogo* gogo, Method* method,
7569 const char* receiver_name,
7570 const Typed_identifier_list* params,
7571 bool is_varargs,
7572 source_location location)
7573 {
7574 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
7575 gcc_assert(receiver_object != NULL);
7576
7577 Expression* expr = Expression::make_var_reference(receiver_object, location);
7578 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
7579 if (expr->type()->points_to() == NULL)
7580 expr = Expression::make_unary(OPERATOR_AND, expr, location);
7581
7582 Expression_list* arguments;
7583 if (params == NULL || params->empty())
7584 arguments = NULL;
7585 else
7586 {
7587 arguments = new Expression_list();
7588 for (Typed_identifier_list::const_iterator p = params->begin();
7589 p != params->end();
7590 ++p)
7591 {
7592 Named_object* param = gogo->lookup(p->name(), NULL);
7593 gcc_assert(param != NULL);
7594 Expression* param_ref = Expression::make_var_reference(param,
7595 location);
7596 arguments->push_back(param_ref);
7597 }
7598 }
7599
7600 Expression* func = method->bind_method(expr, location);
7601 gcc_assert(func != NULL);
7602 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
7603 location);
7604 size_t count = call->result_count();
7605 if (count == 0)
7606 gogo->add_statement(Statement::make_statement(call));
7607 else
7608 {
7609 Expression_list* retvals = new Expression_list();
7610 if (count <= 1)
7611 retvals->push_back(call);
7612 else
7613 {
7614 for (size_t i = 0; i < count; ++i)
7615 retvals->push_back(Expression::make_call_result(call, i));
7616 }
7617 const Function* function = gogo->current_function()->func_value();
7618 const Typed_identifier_list* results = function->type()->results();
7619 Statement* retstat = Statement::make_return_statement(results, retvals,
7620 location);
7621 gogo->add_statement(retstat);
7622 }
7623 }
7624
7625 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
7626 // in reverse order.
7627
7628 Expression*
7629 Type::apply_field_indexes(Expression* expr,
7630 const Method::Field_indexes* field_indexes,
7631 source_location location)
7632 {
7633 if (field_indexes == NULL)
7634 return expr;
7635 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
7636 Struct_type* stype = expr->type()->deref()->struct_type();
7637 gcc_assert(stype != NULL
7638 && field_indexes->field_index < stype->field_count());
7639 if (expr->type()->struct_type() == NULL)
7640 {
7641 gcc_assert(expr->type()->points_to() != NULL);
7642 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
7643 gcc_assert(expr->type()->struct_type() == stype);
7644 }
7645 return Expression::make_field_reference(expr, field_indexes->field_index,
7646 location);
7647 }
7648
7649 // Return whether NO is a method for which the receiver is a pointer.
7650
7651 bool
7652 Type::method_expects_pointer(const Named_object* no)
7653 {
7654 const Function_type *fntype;
7655 if (no->is_function())
7656 fntype = no->func_value()->type();
7657 else if (no->is_function_declaration())
7658 fntype = no->func_declaration_value()->type();
7659 else
7660 gcc_unreachable();
7661 return fntype->receiver()->type()->points_to() != NULL;
7662 }
7663
7664 // Given a set of methods for a type, METHODS, return the method NAME,
7665 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
7666 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
7667 // but is ambiguous (and return NULL).
7668
7669 Method*
7670 Type::method_function(const Methods* methods, const std::string& name,
7671 bool* is_ambiguous)
7672 {
7673 if (is_ambiguous != NULL)
7674 *is_ambiguous = false;
7675 if (methods == NULL)
7676 return NULL;
7677 Methods::const_iterator p = methods->find(name);
7678 if (p == methods->end())
7679 return NULL;
7680 Method* m = p->second;
7681 if (m->is_ambiguous())
7682 {
7683 if (is_ambiguous != NULL)
7684 *is_ambiguous = true;
7685 return NULL;
7686 }
7687 return m;
7688 }
7689
7690 // Look for field or method NAME for TYPE. Return an Expression for
7691 // the field or method bound to EXPR. If there is no such field or
7692 // method, give an appropriate error and return an error expression.
7693
7694 Expression*
7695 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
7696 const std::string& name,
7697 source_location location)
7698 {
7699 if (type->deref()->is_error_type())
7700 return Expression::make_error(location);
7701
7702 const Named_type* nt = type->named_type();
7703 if (nt == NULL)
7704 nt = type->deref()->named_type();
7705 const Struct_type* st = type->deref()->struct_type();
7706 const Interface_type* it = type->deref()->interface_type();
7707
7708 // If this is a pointer to a pointer, then it is possible that the
7709 // pointed-to type has methods.
7710 if (nt == NULL
7711 && st == NULL
7712 && it == NULL
7713 && type->points_to() != NULL
7714 && type->points_to()->points_to() != NULL)
7715 {
7716 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
7717 type = type->points_to();
7718 nt = type->points_to()->named_type();
7719 st = type->points_to()->struct_type();
7720 it = type->points_to()->interface_type();
7721 }
7722
7723 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
7724 || expr->is_addressable());
7725 std::vector<const Named_type*> seen;
7726 bool is_method = false;
7727 bool found_pointer_method = false;
7728 std::string ambig1;
7729 std::string ambig2;
7730 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
7731 &seen, NULL, &is_method,
7732 &found_pointer_method, &ambig1, &ambig2))
7733 {
7734 Expression* ret;
7735 if (!is_method)
7736 {
7737 gcc_assert(st != NULL);
7738 if (type->struct_type() == NULL)
7739 {
7740 gcc_assert(type->points_to() != NULL);
7741 expr = Expression::make_unary(OPERATOR_MULT, expr,
7742 location);
7743 gcc_assert(expr->type()->struct_type() == st);
7744 }
7745 ret = st->field_reference(expr, name, location);
7746 }
7747 else if (it != NULL && it->find_method(name) != NULL)
7748 ret = Expression::make_interface_field_reference(expr, name,
7749 location);
7750 else
7751 {
7752 Method* m;
7753 if (nt != NULL)
7754 m = nt->method_function(name, NULL);
7755 else if (st != NULL)
7756 m = st->method_function(name, NULL);
7757 else
7758 gcc_unreachable();
7759 gcc_assert(m != NULL);
7760 if (!m->is_value_method() && expr->type()->points_to() == NULL)
7761 expr = Expression::make_unary(OPERATOR_AND, expr, location);
7762 ret = m->bind_method(expr, location);
7763 }
7764 gcc_assert(ret != NULL);
7765 return ret;
7766 }
7767 else
7768 {
7769 if (!ambig1.empty())
7770 error_at(location, "%qs is ambiguous via %qs and %qs",
7771 Gogo::message_name(name).c_str(),
7772 Gogo::message_name(ambig1).c_str(),
7773 Gogo::message_name(ambig2).c_str());
7774 else if (found_pointer_method)
7775 error_at(location, "method requires a pointer");
7776 else if (nt == NULL && st == NULL && it == NULL)
7777 error_at(location,
7778 ("reference to field %qs in object which "
7779 "has no fields or methods"),
7780 Gogo::message_name(name).c_str());
7781 else
7782 {
7783 bool is_unexported;
7784 if (!Gogo::is_hidden_name(name))
7785 is_unexported = false;
7786 else
7787 {
7788 std::string unpacked = Gogo::unpack_hidden_name(name);
7789 seen.clear();
7790 is_unexported = Type::is_unexported_field_or_method(gogo, type,
7791 unpacked,
7792 &seen);
7793 }
7794 if (is_unexported)
7795 error_at(location, "reference to unexported field or method %qs",
7796 Gogo::message_name(name).c_str());
7797 else
7798 error_at(location, "reference to undefined field or method %qs",
7799 Gogo::message_name(name).c_str());
7800 }
7801 return Expression::make_error(location);
7802 }
7803 }
7804
7805 // Look in TYPE for a field or method named NAME, return true if one
7806 // is found. This looks through embedded anonymous fields and handles
7807 // ambiguity. If a method is found, sets *IS_METHOD to true;
7808 // otherwise, if a field is found, set it to false. If
7809 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
7810 // whose address can not be taken. SEEN is used to avoid infinite
7811 // recursion on invalid types.
7812
7813 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
7814 // method we couldn't use because it requires a pointer. LEVEL is
7815 // used for recursive calls, and can be NULL for a non-recursive call.
7816 // When this function returns false because it finds that the name is
7817 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
7818 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
7819 // will be unchanged.
7820
7821 // This function just returns whether or not there is a field or
7822 // method, and whether it is a field or method. It doesn't build an
7823 // expression to refer to it. If it is a method, we then look in the
7824 // list of all methods for the type. If it is a field, the search has
7825 // to be done again, looking only for fields, and building up the
7826 // expression as we go.
7827
7828 bool
7829 Type::find_field_or_method(const Type* type,
7830 const std::string& name,
7831 bool receiver_can_be_pointer,
7832 std::vector<const Named_type*>* seen,
7833 int* level,
7834 bool* is_method,
7835 bool* found_pointer_method,
7836 std::string* ambig1,
7837 std::string* ambig2)
7838 {
7839 // Named types can have locally defined methods.
7840 const Named_type* nt = type->named_type();
7841 if (nt == NULL && type->points_to() != NULL)
7842 nt = type->points_to()->named_type();
7843 if (nt != NULL)
7844 {
7845 Named_object* no = nt->find_local_method(name);
7846 if (no != NULL)
7847 {
7848 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
7849 {
7850 *is_method = true;
7851 return true;
7852 }
7853
7854 // Record that we have found a pointer method in order to
7855 // give a better error message if we don't find anything
7856 // else.
7857 *found_pointer_method = true;
7858 }
7859
7860 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
7861 p != seen->end();
7862 ++p)
7863 {
7864 if (*p == nt)
7865 {
7866 // We've already seen this type when searching for methods.
7867 return false;
7868 }
7869 }
7870 }
7871
7872 // Interface types can have methods.
7873 const Interface_type* it = type->deref()->interface_type();
7874 if (it != NULL && it->find_method(name) != NULL)
7875 {
7876 *is_method = true;
7877 return true;
7878 }
7879
7880 // Struct types can have fields. They can also inherit fields and
7881 // methods from anonymous fields.
7882 const Struct_type* st = type->deref()->struct_type();
7883 if (st == NULL)
7884 return false;
7885 const Struct_field_list* fields = st->fields();
7886 if (fields == NULL)
7887 return false;
7888
7889 if (nt != NULL)
7890 seen->push_back(nt);
7891
7892 int found_level = 0;
7893 bool found_is_method = false;
7894 std::string found_ambig1;
7895 std::string found_ambig2;
7896 const Struct_field* found_parent = NULL;
7897 for (Struct_field_list::const_iterator pf = fields->begin();
7898 pf != fields->end();
7899 ++pf)
7900 {
7901 if (pf->field_name() == name)
7902 {
7903 *is_method = false;
7904 if (nt != NULL)
7905 seen->pop_back();
7906 return true;
7907 }
7908
7909 if (!pf->is_anonymous())
7910 continue;
7911
7912 if (pf->type()->deref()->is_error_type()
7913 || pf->type()->deref()->is_undefined())
7914 continue;
7915
7916 Named_type* fnt = pf->type()->deref()->named_type();
7917 gcc_assert(fnt != NULL);
7918
7919 int sublevel = level == NULL ? 1 : *level + 1;
7920 bool sub_is_method;
7921 std::string subambig1;
7922 std::string subambig2;
7923 bool subfound = Type::find_field_or_method(fnt,
7924 name,
7925 receiver_can_be_pointer,
7926 seen,
7927 &sublevel,
7928 &sub_is_method,
7929 found_pointer_method,
7930 &subambig1,
7931 &subambig2);
7932 if (!subfound)
7933 {
7934 if (!subambig1.empty())
7935 {
7936 // The name was found via this field, but is ambiguous.
7937 // if the ambiguity is lower or at the same level as
7938 // anything else we have already found, then we want to
7939 // pass the ambiguity back to the caller.
7940 if (found_level == 0 || sublevel <= found_level)
7941 {
7942 found_ambig1 = pf->field_name() + '.' + subambig1;
7943 found_ambig2 = pf->field_name() + '.' + subambig2;
7944 found_level = sublevel;
7945 }
7946 }
7947 }
7948 else
7949 {
7950 // The name was found via this field. Use the level to see
7951 // if we want to use this one, or whether it introduces an
7952 // ambiguity.
7953 if (found_level == 0 || sublevel < found_level)
7954 {
7955 found_level = sublevel;
7956 found_is_method = sub_is_method;
7957 found_ambig1.clear();
7958 found_ambig2.clear();
7959 found_parent = &*pf;
7960 }
7961 else if (sublevel > found_level)
7962 ;
7963 else if (found_ambig1.empty())
7964 {
7965 // We found an ambiguity.
7966 gcc_assert(found_parent != NULL);
7967 found_ambig1 = found_parent->field_name();
7968 found_ambig2 = pf->field_name();
7969 }
7970 else
7971 {
7972 // We found an ambiguity, but we already know of one.
7973 // Just report the earlier one.
7974 }
7975 }
7976 }
7977
7978 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
7979 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
7980 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
7981 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
7982
7983 if (nt != NULL)
7984 seen->pop_back();
7985
7986 if (found_level == 0)
7987 return false;
7988 else if (!found_ambig1.empty())
7989 {
7990 gcc_assert(!found_ambig1.empty());
7991 ambig1->assign(found_ambig1);
7992 ambig2->assign(found_ambig2);
7993 if (level != NULL)
7994 *level = found_level;
7995 return false;
7996 }
7997 else
7998 {
7999 if (level != NULL)
8000 *level = found_level;
8001 *is_method = found_is_method;
8002 return true;
8003 }
8004 }
8005
8006 // Return whether NAME is an unexported field or method for TYPE.
8007
8008 bool
8009 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
8010 const std::string& name,
8011 std::vector<const Named_type*>* seen)
8012 {
8013 type = type->deref();
8014
8015 const Named_type* nt = type->named_type();
8016 if (nt != NULL)
8017 {
8018 if (nt->is_unexported_local_method(gogo, name))
8019 return true;
8020
8021 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
8022 p != seen->end();
8023 ++p)
8024 {
8025 if (*p == nt)
8026 {
8027 // We've already seen this type.
8028 return false;
8029 }
8030 }
8031 }
8032
8033 const Interface_type* it = type->interface_type();
8034 if (it != NULL && it->is_unexported_method(gogo, name))
8035 return true;
8036
8037 const Struct_type* st = type->struct_type();
8038 if (st != NULL && st->is_unexported_local_field(gogo, name))
8039 return true;
8040
8041 if (st == NULL)
8042 return false;
8043
8044 const Struct_field_list* fields = st->fields();
8045 if (fields == NULL)
8046 return false;
8047
8048 if (nt != NULL)
8049 seen->push_back(nt);
8050
8051 for (Struct_field_list::const_iterator pf = fields->begin();
8052 pf != fields->end();
8053 ++pf)
8054 {
8055 if (pf->is_anonymous()
8056 && (!pf->type()->deref()->is_error_type()
8057 && !pf->type()->deref()->is_undefined()))
8058 {
8059 Named_type* subtype = pf->type()->deref()->named_type();
8060 gcc_assert(subtype != NULL);
8061 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
8062 {
8063 if (nt != NULL)
8064 seen->pop_back();
8065 return true;
8066 }
8067 }
8068 }
8069
8070 if (nt != NULL)
8071 seen->pop_back();
8072
8073 return false;
8074 }
8075
8076 // Class Forward_declaration.
8077
8078 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
8079 : Type(TYPE_FORWARD),
8080 named_object_(named_object->resolve()), warned_(false)
8081 {
8082 gcc_assert(this->named_object_->is_unknown()
8083 || this->named_object_->is_type_declaration());
8084 }
8085
8086 // Return the named object.
8087
8088 Named_object*
8089 Forward_declaration_type::named_object()
8090 {
8091 return this->named_object_->resolve();
8092 }
8093
8094 const Named_object*
8095 Forward_declaration_type::named_object() const
8096 {
8097 return this->named_object_->resolve();
8098 }
8099
8100 // Return the name of the forward declared type.
8101
8102 const std::string&
8103 Forward_declaration_type::name() const
8104 {
8105 return this->named_object()->name();
8106 }
8107
8108 // Warn about a use of a type which has been declared but not defined.
8109
8110 void
8111 Forward_declaration_type::warn() const
8112 {
8113 Named_object* no = this->named_object_->resolve();
8114 if (no->is_unknown())
8115 {
8116 // The name was not defined anywhere.
8117 if (!this->warned_)
8118 {
8119 error_at(this->named_object_->location(),
8120 "use of undefined type %qs",
8121 no->message_name().c_str());
8122 this->warned_ = true;
8123 }
8124 }
8125 else if (no->is_type_declaration())
8126 {
8127 // The name was seen as a type, but the type was never defined.
8128 if (no->type_declaration_value()->using_type())
8129 {
8130 error_at(this->named_object_->location(),
8131 "use of undefined type %qs",
8132 no->message_name().c_str());
8133 this->warned_ = true;
8134 }
8135 }
8136 else
8137 {
8138 // The name was defined, but not as a type.
8139 if (!this->warned_)
8140 {
8141 error_at(this->named_object_->location(), "expected type");
8142 this->warned_ = true;
8143 }
8144 }
8145 }
8146
8147 // Get the base type of a declaration. This gives an error if the
8148 // type has not yet been defined.
8149
8150 Type*
8151 Forward_declaration_type::real_type()
8152 {
8153 if (this->is_defined())
8154 return this->named_object()->type_value();
8155 else
8156 {
8157 this->warn();
8158 return Type::make_error_type();
8159 }
8160 }
8161
8162 const Type*
8163 Forward_declaration_type::real_type() const
8164 {
8165 if (this->is_defined())
8166 return this->named_object()->type_value();
8167 else
8168 {
8169 this->warn();
8170 return Type::make_error_type();
8171 }
8172 }
8173
8174 // Return whether the base type is defined.
8175
8176 bool
8177 Forward_declaration_type::is_defined() const
8178 {
8179 return this->named_object()->is_type();
8180 }
8181
8182 // Add a method. This is used when methods are defined before the
8183 // type.
8184
8185 Named_object*
8186 Forward_declaration_type::add_method(const std::string& name,
8187 Function* function)
8188 {
8189 Named_object* no = this->named_object();
8190 if (no->is_unknown())
8191 no->declare_as_type();
8192 return no->type_declaration_value()->add_method(name, function);
8193 }
8194
8195 // Add a method declaration. This is used when methods are declared
8196 // before the type.
8197
8198 Named_object*
8199 Forward_declaration_type::add_method_declaration(const std::string& name,
8200 Function_type* type,
8201 source_location location)
8202 {
8203 Named_object* no = this->named_object();
8204 if (no->is_unknown())
8205 no->declare_as_type();
8206 Type_declaration* td = no->type_declaration_value();
8207 return td->add_method_declaration(name, type, location);
8208 }
8209
8210 // Traversal.
8211
8212 int
8213 Forward_declaration_type::do_traverse(Traverse* traverse)
8214 {
8215 if (this->is_defined()
8216 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
8217 return TRAVERSE_EXIT;
8218 return TRAVERSE_CONTINUE;
8219 }
8220
8221 // Get a tree for the type.
8222
8223 tree
8224 Forward_declaration_type::do_get_tree(Gogo* gogo)
8225 {
8226 if (this->is_defined())
8227 return Type::get_named_type_tree(gogo, this->real_type());
8228
8229 if (this->warned_)
8230 return error_mark_node;
8231
8232 // We represent an undefined type as a struct with no fields. That
8233 // should work fine for the middle-end, since the same case can
8234 // arise in C.
8235 Named_object* no = this->named_object();
8236 tree type_tree = make_node(RECORD_TYPE);
8237 tree id = no->get_id(gogo);
8238 tree decl = build_decl(no->location(), TYPE_DECL, id, type_tree);
8239 TYPE_NAME(type_tree) = decl;
8240 layout_type(type_tree);
8241 return type_tree;
8242 }
8243
8244 // Build a type descriptor for a forwarded type.
8245
8246 Expression*
8247 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8248 {
8249 if (!this->is_defined())
8250 return Expression::make_nil(BUILTINS_LOCATION);
8251 else
8252 {
8253 Type* t = this->real_type();
8254 if (name != NULL)
8255 return this->named_type_descriptor(gogo, t, name);
8256 else
8257 return Expression::make_type_descriptor(t, BUILTINS_LOCATION);
8258 }
8259 }
8260
8261 // The reflection string.
8262
8263 void
8264 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
8265 {
8266 this->append_reflection(this->real_type(), gogo, ret);
8267 }
8268
8269 // The mangled name.
8270
8271 void
8272 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
8273 {
8274 if (this->is_defined())
8275 this->append_mangled_name(this->real_type(), gogo, ret);
8276 else
8277 {
8278 const Named_object* no = this->named_object();
8279 std::string name;
8280 if (no->package() == NULL)
8281 name = gogo->package_name();
8282 else
8283 name = no->package()->name();
8284 name += '.';
8285 name += Gogo::unpack_hidden_name(no->name());
8286 char buf[20];
8287 snprintf(buf, sizeof buf, "N%u_",
8288 static_cast<unsigned int>(name.length()));
8289 ret->append(buf);
8290 ret->append(name);
8291 }
8292 }
8293
8294 // Export a forward declaration. This can happen when a defined type
8295 // refers to a type which is only declared (and is presumably defined
8296 // in some other file in the same package).
8297
8298 void
8299 Forward_declaration_type::do_export(Export*) const
8300 {
8301 // If there is a base type, that should be exported instead of this.
8302 gcc_assert(!this->is_defined());
8303
8304 // We don't output anything.
8305 }
8306
8307 // Make a forward declaration.
8308
8309 Type*
8310 Type::make_forward_declaration(Named_object* named_object)
8311 {
8312 return new Forward_declaration_type(named_object);
8313 }
8314
8315 // Class Typed_identifier_list.
8316
8317 // Sort the entries by name.
8318
8319 struct Typed_identifier_list_sort
8320 {
8321 public:
8322 bool
8323 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
8324 { return t1.name() < t2.name(); }
8325 };
8326
8327 void
8328 Typed_identifier_list::sort_by_name()
8329 {
8330 std::sort(this->entries_.begin(), this->entries_.end(),
8331 Typed_identifier_list_sort());
8332 }
8333
8334 // Traverse types.
8335
8336 int
8337 Typed_identifier_list::traverse(Traverse* traverse)
8338 {
8339 for (Typed_identifier_list::const_iterator p = this->begin();
8340 p != this->end();
8341 ++p)
8342 {
8343 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
8344 return TRAVERSE_EXIT;
8345 }
8346 return TRAVERSE_CONTINUE;
8347 }
8348
8349 // Copy the list.
8350
8351 Typed_identifier_list*
8352 Typed_identifier_list::copy() const
8353 {
8354 Typed_identifier_list* ret = new Typed_identifier_list();
8355 for (Typed_identifier_list::const_iterator p = this->begin();
8356 p != this->end();
8357 ++p)
8358 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));
8359 return ret;
8360 }