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