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