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