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