Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / gcc / go / gofrontend / types.cc
blob02165371503aa33f7ad4c423c5652f1c6e3b8136
1 // types.cc -- Go frontend types.
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.
7 #include "go-system.h"
9 #include <gmp.h>
11 #ifndef ENABLE_BUILD_WITH_CXX
12 extern "C"
14 #endif
16 #include "toplev.h"
17 #include "intl.h"
18 #include "tree.h"
19 #include "gimple.h"
20 #include "real.h"
21 #include "convert.h"
23 #ifndef ENABLE_BUILD_WITH_CXX
25 #endif
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"
36 // Class Type.
38 Type::Type(Type_classification classification)
39 : classification_(classification), tree_(NULL_TREE),
40 type_descriptor_decl_(NULL_TREE)
44 Type::~Type()
48 // Get the base type for a type--skip names and forward declarations.
50 Type*
51 Type::base()
53 switch (this->classification_)
55 case TYPE_NAMED:
56 return this->named_type()->named_base();
57 case TYPE_FORWARD:
58 return this->forward_declaration_type()->real_type()->base();
59 default:
60 return this;
64 const Type*
65 Type::base() const
67 switch (this->classification_)
69 case TYPE_NAMED:
70 return this->named_type()->named_base();
71 case TYPE_FORWARD:
72 return this->forward_declaration_type()->real_type()->base();
73 default:
74 return this;
78 // Skip defined forward declarations.
80 Type*
81 Type::forwarded()
83 Type* t = this;
84 Forward_declaration_type* ftype = t->forward_declaration_type();
85 while (ftype != NULL && ftype->is_defined())
87 t = ftype->real_type();
88 ftype = t->forward_declaration_type();
90 return t;
93 const Type*
94 Type::forwarded() const
96 const Type* t = this;
97 const Forward_declaration_type* ftype = t->forward_declaration_type();
98 while (ftype != NULL && ftype->is_defined())
100 t = ftype->real_type();
101 ftype = t->forward_declaration_type();
103 return t;
106 // If this is a named type, return it. Otherwise, return NULL.
108 Named_type*
109 Type::named_type()
111 return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
114 const Named_type*
115 Type::named_type() const
117 return this->forwarded()->convert_no_base<const Named_type, TYPE_NAMED>();
120 // Return true if this type is not defined.
122 bool
123 Type::is_undefined() const
125 return this->forwarded()->forward_declaration_type() != NULL;
128 // Return true if this is a basic type: a type which is not composed
129 // of other types, and is not void.
131 bool
132 Type::is_basic_type() const
134 switch (this->classification_)
136 case TYPE_INTEGER:
137 case TYPE_FLOAT:
138 case TYPE_COMPLEX:
139 case TYPE_BOOLEAN:
140 case TYPE_STRING:
141 case TYPE_NIL:
142 return true;
144 case TYPE_ERROR:
145 case TYPE_VOID:
146 case TYPE_FUNCTION:
147 case TYPE_POINTER:
148 case TYPE_STRUCT:
149 case TYPE_ARRAY:
150 case TYPE_MAP:
151 case TYPE_CHANNEL:
152 case TYPE_INTERFACE:
153 return false;
155 case TYPE_NAMED:
156 case TYPE_FORWARD:
157 return this->base()->is_basic_type();
159 default:
160 gcc_unreachable();
164 // Return true if this is an abstract type.
166 bool
167 Type::is_abstract() const
169 switch (this->classification())
171 case TYPE_INTEGER:
172 return this->integer_type()->is_abstract();
173 case TYPE_FLOAT:
174 return this->float_type()->is_abstract();
175 case TYPE_COMPLEX:
176 return this->complex_type()->is_abstract();
177 case TYPE_STRING:
178 return this->is_abstract_string_type();
179 case TYPE_BOOLEAN:
180 return this->is_abstract_boolean_type();
181 default:
182 return false;
186 // Return a non-abstract version of an abstract type.
188 Type*
189 Type::make_non_abstract_type()
191 gcc_assert(this->is_abstract());
192 switch (this->classification())
194 case TYPE_INTEGER:
195 return Type::lookup_integer_type("int");
196 case TYPE_FLOAT:
197 return Type::lookup_float_type("float");
198 case TYPE_COMPLEX:
199 return Type::lookup_complex_type("complex");
200 case TYPE_STRING:
201 return Type::lookup_string_type();
202 case TYPE_BOOLEAN:
203 return Type::lookup_bool_type();
204 default:
205 gcc_unreachable();
209 // Return true if this is an error type. Don't give an error if we
210 // try to dereference an undefined forwarding type, as this is called
211 // in the parser when the type may legitimately be undefined.
213 bool
214 Type::is_error_type() const
216 const Type* t = this->forwarded();
217 // Note that we return false for an undefined forward type.
218 switch (t->classification_)
220 case TYPE_ERROR:
221 return true;
222 case TYPE_NAMED:
223 return t->named_type()->is_named_error_type();
224 default:
225 return false;
229 // If this is a pointer type, return the type to which it points.
230 // Otherwise, return NULL.
232 Type*
233 Type::points_to() const
235 const Pointer_type* ptype = this->convert<const Pointer_type,
236 TYPE_POINTER>();
237 return ptype == NULL ? NULL : ptype->points_to();
240 // Return whether this is an open array type.
242 bool
243 Type::is_open_array_type() const
245 return this->array_type() != NULL && this->array_type()->length() == NULL;
248 // Return whether this is the predeclared constant nil being used as a
249 // type.
251 bool
252 Type::is_nil_constant_as_type() const
254 const Type* t = this->forwarded();
255 if (t->forward_declaration_type() != NULL)
257 const Named_object* no = t->forward_declaration_type()->named_object();
258 if (no->is_unknown())
259 no = no->unknown_value()->real_named_object();
260 if (no != NULL
261 && no->is_const()
262 && no->const_value()->expr()->is_nil_expression())
263 return true;
265 return false;
268 // Traverse a type.
271 Type::traverse(Type* type, Traverse* traverse)
273 gcc_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
274 || (traverse->traverse_mask()
275 & Traverse::traverse_expressions) != 0);
276 if (traverse->remember_type(type))
278 // We have already traversed this type.
279 return TRAVERSE_CONTINUE;
281 if ((traverse->traverse_mask() & Traverse::traverse_types) != 0)
283 int t = traverse->type(type);
284 if (t == TRAVERSE_EXIT)
285 return TRAVERSE_EXIT;
286 else if (t == TRAVERSE_SKIP_COMPONENTS)
287 return TRAVERSE_CONTINUE;
289 // An array type has an expression which we need to traverse if
290 // traverse_expressions is set.
291 if (type->do_traverse(traverse) == TRAVERSE_EXIT)
292 return TRAVERSE_EXIT;
293 return TRAVERSE_CONTINUE;
296 // Default implementation for do_traverse for child class.
299 Type::do_traverse(Traverse*)
301 return TRAVERSE_CONTINUE;
304 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
305 // then return true for all erroneous types; this is used to avoid
306 // cascading errors. If REASON is not NULL, optionally set *REASON to
307 // the reason the types are not identical.
309 bool
310 Type::are_identical(const Type* t1, const Type* t2, bool errors_are_identical,
311 std::string* reason)
313 if (t1 == NULL || t2 == NULL)
315 // Something is wrong.
316 return errors_are_identical ? true : t1 == t2;
319 // Skip defined forward declarations.
320 t1 = t1->forwarded();
321 t2 = t2->forwarded();
323 if (t1 == t2)
324 return true;
326 // An undefined forward declaration is an error.
327 if (t1->forward_declaration_type() != NULL
328 || t2->forward_declaration_type() != NULL)
329 return errors_are_identical;
331 // Avoid cascading errors with error types.
332 if (t1->is_error_type() || t2->is_error_type())
334 if (errors_are_identical)
335 return true;
336 return t1->is_error_type() && t2->is_error_type();
339 // Get a good reason for the sink type. Note that the sink type on
340 // the left hand side of an assignment is handled in are_assignable.
341 if (t1->is_sink_type() || t2->is_sink_type())
343 if (reason != NULL)
344 *reason = "invalid use of _";
345 return false;
348 // A named type is only identical to itself.
349 if (t1->named_type() != NULL || t2->named_type() != NULL)
350 return false;
352 // Check type shapes.
353 if (t1->classification() != t2->classification())
354 return false;
356 switch (t1->classification())
358 case TYPE_VOID:
359 case TYPE_BOOLEAN:
360 case TYPE_STRING:
361 case TYPE_NIL:
362 // These types are always identical.
363 return true;
365 case TYPE_INTEGER:
366 return t1->integer_type()->is_identical(t2->integer_type());
368 case TYPE_FLOAT:
369 return t1->float_type()->is_identical(t2->float_type());
371 case TYPE_COMPLEX:
372 return t1->complex_type()->is_identical(t2->complex_type());
374 case TYPE_FUNCTION:
375 return t1->function_type()->is_identical(t2->function_type(),
376 false,
377 errors_are_identical,
378 reason);
380 case TYPE_POINTER:
381 return Type::are_identical(t1->points_to(), t2->points_to(),
382 errors_are_identical, reason);
384 case TYPE_STRUCT:
385 return t1->struct_type()->is_identical(t2->struct_type(),
386 errors_are_identical);
388 case TYPE_ARRAY:
389 return t1->array_type()->is_identical(t2->array_type(),
390 errors_are_identical);
392 case TYPE_MAP:
393 return t1->map_type()->is_identical(t2->map_type(),
394 errors_are_identical);
396 case TYPE_CHANNEL:
397 return t1->channel_type()->is_identical(t2->channel_type(),
398 errors_are_identical);
400 case TYPE_INTERFACE:
401 return t1->interface_type()->is_identical(t2->interface_type(),
402 errors_are_identical);
404 default:
405 gcc_unreachable();
409 // Return true if it's OK to have a binary operation with types LHS
410 // and RHS. This is not used for shifts or comparisons.
412 bool
413 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
415 if (Type::are_identical(lhs, rhs, true, NULL))
416 return true;
418 // A constant of abstract bool type may be mixed with any bool type.
419 if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
420 || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
421 return true;
423 // A constant of abstract string type may be mixed with any string
424 // type.
425 if ((rhs->is_abstract_string_type() && lhs->is_string_type())
426 || (lhs->is_abstract_string_type() && rhs->is_string_type()))
427 return true;
429 lhs = lhs->base();
430 rhs = rhs->base();
432 // A constant of abstract integer, float, or complex type may be
433 // mixed with an integer, float, or complex type.
434 if ((rhs->is_abstract()
435 && (rhs->integer_type() != NULL
436 || rhs->float_type() != NULL
437 || rhs->complex_type() != NULL)
438 && (lhs->integer_type() != NULL
439 || lhs->float_type() != NULL
440 || lhs->complex_type() != NULL))
441 || (lhs->is_abstract()
442 && (lhs->integer_type() != NULL
443 || lhs->float_type() != NULL
444 || lhs->complex_type() != NULL)
445 && (rhs->integer_type() != NULL
446 || rhs->float_type() != NULL
447 || rhs->complex_type() != NULL)))
448 return true;
450 // The nil type may be compared to a pointer, an interface type, a
451 // slice type, a channel type, a map type, or a function type.
452 if (lhs->is_nil_type()
453 && (rhs->points_to() != NULL
454 || rhs->interface_type() != NULL
455 || rhs->is_open_array_type()
456 || rhs->map_type() != NULL
457 || rhs->channel_type() != NULL
458 || rhs->function_type() != NULL))
459 return true;
460 if (rhs->is_nil_type()
461 && (lhs->points_to() != NULL
462 || lhs->interface_type() != NULL
463 || lhs->is_open_array_type()
464 || lhs->map_type() != NULL
465 || lhs->channel_type() != NULL
466 || lhs->function_type() != NULL))
467 return true;
469 return false;
472 // Return true if a value with type RHS may be assigned to a variable
473 // with type LHS. If REASON is not NULL, set *REASON to the reason
474 // the types are not assignable.
476 bool
477 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
479 // Do some checks first. Make sure the types are defined.
480 if (lhs != NULL && lhs->forwarded()->forward_declaration_type() == NULL)
482 // Any value may be assigned to the blank identifier.
483 if (lhs->is_sink_type())
484 return true;
486 // All fields of a struct must be exported, or the assignment
487 // must be in the same package.
488 if (rhs != NULL && rhs->forwarded()->forward_declaration_type() == NULL)
490 if (lhs->has_hidden_fields(NULL, reason)
491 || rhs->has_hidden_fields(NULL, reason))
492 return false;
496 // Identical types are assignable.
497 if (Type::are_identical(lhs, rhs, true, reason))
498 return true;
500 // The types are assignable if they have identical underlying types
501 // and either LHS or RHS is not a named type.
502 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
503 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
504 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
505 return true;
507 // The types are assignable if LHS is an interface type and RHS
508 // implements the required methods.
509 const Interface_type* lhs_interface_type = lhs->interface_type();
510 if (lhs_interface_type != NULL)
512 if (lhs_interface_type->implements_interface(rhs, reason))
513 return true;
514 const Interface_type* rhs_interface_type = rhs->interface_type();
515 if (rhs_interface_type != NULL
516 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
517 reason))
518 return true;
521 // The type are assignable if RHS is a bidirectional channel type,
522 // LHS is a channel type, they have identical element types, and
523 // either LHS or RHS is not a named type.
524 if (lhs->channel_type() != NULL
525 && rhs->channel_type() != NULL
526 && rhs->channel_type()->may_send()
527 && rhs->channel_type()->may_receive()
528 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
529 && Type::are_identical(lhs->channel_type()->element_type(),
530 rhs->channel_type()->element_type(),
531 true,
532 reason))
533 return true;
535 // The nil type may be assigned to a pointer, function, slice, map,
536 // channel, or interface type.
537 if (rhs->is_nil_type()
538 && (lhs->points_to() != NULL
539 || lhs->function_type() != NULL
540 || lhs->is_open_array_type()
541 || lhs->map_type() != NULL
542 || lhs->channel_type() != NULL
543 || lhs->interface_type() != NULL))
544 return true;
546 // An untyped numeric constant may be assigned to a numeric type if
547 // it is representable in that type.
548 if ((rhs->is_abstract()
549 && (rhs->integer_type() != NULL
550 || rhs->float_type() != NULL
551 || rhs->complex_type() != NULL))
552 && (lhs->integer_type() != NULL
553 || lhs->float_type() != NULL
554 || lhs->complex_type() != NULL))
555 return true;
557 // Give some better error messages.
558 if (reason != NULL && reason->empty())
560 if (rhs->interface_type() != NULL)
561 reason->assign(_("need explicit conversion"));
562 else if (rhs->is_call_multiple_result_type())
563 reason->assign(_("multiple value function call in "
564 "single value context"));
565 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
567 size_t len = (lhs->named_type()->name().length()
568 + rhs->named_type()->name().length()
569 + 100);
570 char* buf = new char[len];
571 snprintf(buf, len, _("cannot use type %s as type %s"),
572 rhs->named_type()->message_name().c_str(),
573 lhs->named_type()->message_name().c_str());
574 reason->assign(buf);
575 delete[] buf;
579 return false;
582 // Return true if a value with type RHS may be converted to type LHS.
583 // If REASON is not NULL, set *REASON to the reason the types are not
584 // convertible.
586 bool
587 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
589 // The types are convertible if they are assignable.
590 if (Type::are_assignable(lhs, rhs, reason))
591 return true;
593 // The types are convertible if they have identical underlying
594 // types.
595 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
596 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
597 return true;
599 // The types are convertible if they are both unnamed pointer types
600 // and their pointer base types have identical underlying types.
601 if (lhs->named_type() == NULL
602 && rhs->named_type() == NULL
603 && lhs->points_to() != NULL
604 && rhs->points_to() != NULL
605 && (lhs->points_to()->named_type() != NULL
606 || rhs->points_to()->named_type() != NULL)
607 && Type::are_identical(lhs->points_to()->base(),
608 rhs->points_to()->base(),
609 true,
610 reason))
611 return true;
613 // Integer and floating point types are convertible to each other.
614 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
615 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
616 return true;
618 // Complex types are convertible to each other.
619 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
620 return true;
622 // An integer, or []byte, or []int, may be converted to a string.
623 if (lhs->is_string_type())
625 if (rhs->integer_type() != NULL)
626 return true;
627 if (rhs->is_open_array_type() && rhs->named_type() == NULL)
629 const Type* e = rhs->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;
637 // A string may be converted to []byte or []int.
638 if (rhs->is_string_type()
639 && lhs->is_open_array_type()
640 && lhs->named_type() == NULL)
642 const Type* e = lhs->array_type()->element_type()->forwarded();
643 if (e->integer_type() != NULL
644 && (e == Type::lookup_integer_type("uint8")
645 || e == Type::lookup_integer_type("int")))
646 return true;
649 // An unsafe.Pointer type may be converted to any pointer type or to
650 // uintptr, and vice-versa.
651 if (lhs->is_unsafe_pointer_type()
652 && (rhs->points_to() != NULL
653 || (rhs->integer_type() != NULL
654 && rhs->forwarded() == Type::lookup_integer_type("uintptr"))))
655 return true;
656 if (rhs->is_unsafe_pointer_type()
657 && (lhs->points_to() != NULL
658 || (lhs->integer_type() != NULL
659 && lhs->forwarded() == Type::lookup_integer_type("uintptr"))))
660 return true;
662 // Give a better error message.
663 if (reason != NULL)
665 if (reason->empty())
666 *reason = "invalid type conversion";
667 else
669 std::string s = "invalid type conversion (";
670 s += *reason;
671 s += ')';
672 *reason = s;
676 return false;
679 // Return whether this type has any hidden fields. This is only a
680 // possibility for a few types.
682 bool
683 Type::has_hidden_fields(const Named_type* within, std::string* reason) const
685 switch (this->forwarded()->classification_)
687 case TYPE_NAMED:
688 return this->named_type()->named_type_has_hidden_fields(reason);
689 case TYPE_STRUCT:
690 return this->struct_type()->struct_has_hidden_fields(within, reason);
691 case TYPE_ARRAY:
692 return this->array_type()->array_has_hidden_fields(within, reason);
693 default:
694 return false;
698 // Return a hash code for the type to be used for method lookup.
700 unsigned int
701 Type::hash_for_method(Gogo* gogo) const
703 unsigned int ret = 0;
704 if (this->classification_ != TYPE_FORWARD)
705 ret += this->classification_;
706 return ret + this->do_hash_for_method(gogo);
709 // Default implementation of do_hash_for_method. This is appropriate
710 // for types with no subfields.
712 unsigned int
713 Type::do_hash_for_method(Gogo*) const
715 return 0;
718 // Return a hash code for a string, given a starting hash.
720 unsigned int
721 Type::hash_string(const std::string& s, unsigned int h)
723 const char* p = s.data();
724 size_t len = s.length();
725 for (; len > 0; --len)
727 h ^= *p++;
728 h*= 16777619;
730 return h;
733 // Default check for the expression passed to make. Any type which
734 // may be used with make implements its own version of this.
736 bool
737 Type::do_check_make_expression(Expression_list*, source_location)
739 gcc_unreachable();
742 // Return whether an expression has an integer value. Report an error
743 // if not. This is used when handling calls to the predeclared make
744 // function.
746 bool
747 Type::check_int_value(Expression* e, const char* errmsg,
748 source_location location)
750 if (e->type()->integer_type() != NULL)
751 return true;
753 // Check for a floating point constant with integer value.
754 mpfr_t fval;
755 mpfr_init(fval);
757 Type* dummy;
758 if (e->float_constant_value(fval, &dummy))
760 mpz_t ival;
761 mpz_init(ival);
763 bool ok = false;
765 mpfr_clear_overflow();
766 mpfr_clear_erangeflag();
767 mpfr_get_z(ival, fval, GMP_RNDN);
768 if (!mpfr_overflow_p()
769 && !mpfr_erangeflag_p()
770 && mpz_sgn(ival) >= 0)
772 Named_type* ntype = Type::lookup_integer_type("int");
773 Integer_type* inttype = ntype->integer_type();
774 mpz_t max;
775 mpz_init_set_ui(max, 1);
776 mpz_mul_2exp(max, max, inttype->bits() - 1);
777 ok = mpz_cmp(ival, max) < 0;
778 mpz_clear(max);
780 mpz_clear(ival);
782 if (ok)
784 mpfr_clear(fval);
785 return true;
789 mpfr_clear(fval);
791 error_at(location, "%s", errmsg);
792 return false;
795 // A hash table mapping unnamed types to trees.
797 Type::Type_trees Type::type_trees;
799 // Return a tree representing this type.
801 tree
802 Type::get_tree(Gogo* gogo)
804 if (this->tree_ != NULL)
805 return this->tree_;
807 if (this->forward_declaration_type() != NULL
808 || this->named_type() != NULL)
809 return this->get_tree_without_hash(gogo);
811 if (this->is_error_type())
812 return error_mark_node;
814 // To avoid confusing GIMPLE, we need to translate all identical Go
815 // types to the same GIMPLE type. We use a hash table to do that.
816 // There is no need to use the hash table for named types, as named
817 // types are only identical to themselves.
819 std::pair<Type*, tree> val(this, NULL);
820 std::pair<Type_trees::iterator, bool> ins =
821 Type::type_trees.insert(val);
822 if (!ins.second && ins.first->second != NULL_TREE)
824 this->tree_ = ins.first->second;
825 return this->tree_;
828 tree t = this->get_tree_without_hash(gogo);
830 if (ins.first->second == NULL_TREE)
831 ins.first->second = t;
832 else
834 // We have already created a tree for this type. This can
835 // happen when an unnamed type is defined using a named type
836 // which in turns uses an identical unnamed type. Use the tree
837 // we created earlier and ignore the one we just built.
838 t = ins.first->second;
839 this->tree_ = t;
842 return t;
845 // Return a tree for a type without looking in the hash table for
846 // identical types. This is used for named types, since there is no
847 // point to looking in the hash table for them.
849 tree
850 Type::get_tree_without_hash(Gogo* gogo)
852 if (this->tree_ == NULL_TREE)
854 tree t = this->do_get_tree(gogo);
856 // For a recursive function or pointer type, we will temporarily
857 // return ptr_type_node during the recursion. We don't want to
858 // record that for a forwarding type, as it may confuse us
859 // later.
860 if (t == ptr_type_node && this->forward_declaration_type() != NULL)
861 return t;
863 this->tree_ = t;
864 go_preserve_from_gc(t);
867 return this->tree_;
870 // Return a tree representing a zero initialization for this type.
872 tree
873 Type::get_init_tree(Gogo* gogo, bool is_clear)
875 tree type_tree = this->get_tree(gogo);
876 if (type_tree == error_mark_node)
877 return error_mark_node;
878 return this->do_get_init_tree(gogo, type_tree, is_clear);
881 // Any type which supports the builtin make function must implement
882 // this.
884 tree
885 Type::do_make_expression_tree(Translate_context*, Expression_list*,
886 source_location)
888 gcc_unreachable();
891 // Return a pointer to the type descriptor for this type.
893 tree
894 Type::type_descriptor_pointer(Gogo* gogo)
896 Type* t = this->forwarded();
897 if (t->type_descriptor_decl_ == NULL_TREE)
899 Expression* e = t->do_type_descriptor(gogo, NULL);
900 gogo->build_type_descriptor_decl(t, e, &t->type_descriptor_decl_);
901 gcc_assert(t->type_descriptor_decl_ != NULL_TREE
902 && (t->type_descriptor_decl_ == error_mark_node
903 || DECL_P(t->type_descriptor_decl_)));
905 if (t->type_descriptor_decl_ == error_mark_node)
906 return error_mark_node;
907 return build_fold_addr_expr(t->type_descriptor_decl_);
910 // Return a composite literal for a type descriptor.
912 Expression*
913 Type::type_descriptor(Gogo* gogo, Type* type)
915 return type->do_type_descriptor(gogo, NULL);
918 // Return a composite literal for a type descriptor with a name.
920 Expression*
921 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
923 gcc_assert(name != NULL && type->named_type() != name);
924 return type->do_type_descriptor(gogo, name);
927 // Make a builtin struct type from a list of fields. The fields are
928 // pairs of a name and a type.
930 Struct_type*
931 Type::make_builtin_struct_type(int nfields, ...)
933 va_list ap;
934 va_start(ap, nfields);
936 source_location bloc = BUILTINS_LOCATION;
937 Struct_field_list* sfl = new Struct_field_list();
938 for (int i = 0; i < nfields; i++)
940 const char* field_name = va_arg(ap, const char *);
941 Type* type = va_arg(ap, Type*);
942 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
945 va_end(ap);
947 return Type::make_struct_type(sfl, bloc);
950 // Make a builtin named type.
952 Named_type*
953 Type::make_builtin_named_type(const char* name, Type* type)
955 source_location bloc = BUILTINS_LOCATION;
956 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
957 return no->type_value();
960 // Return the type of a type descriptor. We should really tie this to
961 // runtime.Type rather than copying it. This must match commonType in
962 // libgo/go/runtime/type.go.
964 Type*
965 Type::make_type_descriptor_type()
967 static Type* ret;
968 if (ret == NULL)
970 source_location bloc = BUILTINS_LOCATION;
972 Type* uint8_type = Type::lookup_integer_type("uint8");
973 Type* uint32_type = Type::lookup_integer_type("uint32");
974 Type* uintptr_type = Type::lookup_integer_type("uintptr");
975 Type* string_type = Type::lookup_string_type();
976 Type* pointer_string_type = Type::make_pointer_type(string_type);
978 // This is an unnamed version of unsafe.Pointer. Perhaps we
979 // should use the named version instead, although that would
980 // require us to create the unsafe package if it has not been
981 // imported. It probably doesn't matter.
982 Type* void_type = Type::make_void_type();
983 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
985 // Forward declaration for the type descriptor type.
986 Named_object* named_type_descriptor_type =
987 Named_object::make_type_declaration("commonType", NULL, bloc);
988 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
989 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
991 // The type of a method on a concrete type.
992 Struct_type* method_type =
993 Type::make_builtin_struct_type(5,
994 "name", pointer_string_type,
995 "pkgPath", pointer_string_type,
996 "mtyp", pointer_type_descriptor_type,
997 "typ", pointer_type_descriptor_type,
998 "tfn", unsafe_pointer_type);
999 Named_type* named_method_type =
1000 Type::make_builtin_named_type("method", method_type);
1002 // Information for types with a name or methods.
1003 Type* slice_named_method_type =
1004 Type::make_array_type(named_method_type, NULL);
1005 Struct_type* uncommon_type =
1006 Type::make_builtin_struct_type(3,
1007 "name", pointer_string_type,
1008 "pkgPath", pointer_string_type,
1009 "methods", slice_named_method_type);
1010 Named_type* named_uncommon_type =
1011 Type::make_builtin_named_type("uncommonType", uncommon_type);
1013 Type* pointer_uncommon_type =
1014 Type::make_pointer_type(named_uncommon_type);
1016 // The type descriptor type.
1018 Typed_identifier_list* params = new Typed_identifier_list();
1019 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1020 params->push_back(Typed_identifier("", uintptr_type, bloc));
1022 Typed_identifier_list* results = new Typed_identifier_list();
1023 results->push_back(Typed_identifier("", uintptr_type, bloc));
1025 Type* hashfn_type = Type::make_function_type(NULL, params, results, bloc);
1027 params = new Typed_identifier_list();
1028 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1029 params->push_back(Typed_identifier("", unsafe_pointer_type, bloc));
1030 params->push_back(Typed_identifier("", uintptr_type, bloc));
1032 results = new Typed_identifier_list();
1033 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1035 Type* equalfn_type = Type::make_function_type(NULL, params, results,
1036 bloc);
1038 Struct_type* type_descriptor_type =
1039 Type::make_builtin_struct_type(9,
1040 "Kind", uint8_type,
1041 "align", uint8_type,
1042 "fieldAlign", uint8_type,
1043 "size", uintptr_type,
1044 "hash", uint32_type,
1045 "hashfn", hashfn_type,
1046 "equalfn", equalfn_type,
1047 "string", pointer_string_type,
1048 "", pointer_uncommon_type);
1050 Named_type* named = Type::make_builtin_named_type("commonType",
1051 type_descriptor_type);
1053 named_type_descriptor_type->set_type_value(named);
1055 ret = named;
1058 return ret;
1061 // Make the type of a pointer to a type descriptor as represented in
1062 // Go.
1064 Type*
1065 Type::make_type_descriptor_ptr_type()
1067 static Type* ret;
1068 if (ret == NULL)
1069 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1070 return ret;
1073 // Return the names of runtime functions which compute a hash code for
1074 // this type and which compare whether two values of this type are
1075 // equal.
1077 void
1078 Type::type_functions(const char** hash_fn, const char** equal_fn) const
1080 switch (this->base()->classification())
1082 case Type::TYPE_ERROR:
1083 case Type::TYPE_VOID:
1084 case Type::TYPE_NIL:
1085 // These types can not be hashed or compared.
1086 *hash_fn = "__go_type_hash_error";
1087 *equal_fn = "__go_type_equal_error";
1088 break;
1090 case Type::TYPE_BOOLEAN:
1091 case Type::TYPE_INTEGER:
1092 case Type::TYPE_FLOAT:
1093 case Type::TYPE_COMPLEX:
1094 case Type::TYPE_POINTER:
1095 case Type::TYPE_FUNCTION:
1096 case Type::TYPE_MAP:
1097 case Type::TYPE_CHANNEL:
1098 *hash_fn = "__go_type_hash_identity";
1099 *equal_fn = "__go_type_equal_identity";
1100 break;
1102 case Type::TYPE_STRING:
1103 *hash_fn = "__go_type_hash_string";
1104 *equal_fn = "__go_type_equal_string";
1105 break;
1107 case Type::TYPE_STRUCT:
1108 case Type::TYPE_ARRAY:
1109 // These types can not be hashed or compared.
1110 *hash_fn = "__go_type_hash_error";
1111 *equal_fn = "__go_type_equal_error";
1112 break;
1114 case Type::TYPE_INTERFACE:
1115 if (this->interface_type()->is_empty())
1117 *hash_fn = "__go_type_hash_empty_interface";
1118 *equal_fn = "__go_type_equal_empty_interface";
1120 else
1122 *hash_fn = "__go_type_hash_interface";
1123 *equal_fn = "__go_type_equal_interface";
1125 break;
1127 case Type::TYPE_NAMED:
1128 case Type::TYPE_FORWARD:
1129 gcc_unreachable();
1131 default:
1132 gcc_unreachable();
1136 // Return a composite literal for the type descriptor for a plain type
1137 // of kind RUNTIME_TYPE_KIND named NAME.
1139 Expression*
1140 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1141 Named_type* name, const Methods* methods,
1142 bool only_value_methods)
1144 source_location bloc = BUILTINS_LOCATION;
1146 Type* td_type = Type::make_type_descriptor_type();
1147 const Struct_field_list* fields = td_type->struct_type()->fields();
1149 Expression_list* vals = new Expression_list();
1150 vals->reserve(9);
1152 Struct_field_list::const_iterator p = fields->begin();
1153 gcc_assert(p->field_name() == "Kind");
1154 mpz_t iv;
1155 mpz_init_set_ui(iv, runtime_type_kind);
1156 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1158 ++p;
1159 gcc_assert(p->field_name() == "align");
1160 Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1161 vals->push_back(Expression::make_type_info(this, type_info));
1163 ++p;
1164 gcc_assert(p->field_name() == "fieldAlign");
1165 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1166 vals->push_back(Expression::make_type_info(this, type_info));
1168 ++p;
1169 gcc_assert(p->field_name() == "size");
1170 type_info = Expression::TYPE_INFO_SIZE;
1171 vals->push_back(Expression::make_type_info(this, type_info));
1173 ++p;
1174 gcc_assert(p->field_name() == "hash");
1175 mpz_set_ui(iv, this->hash_for_method(gogo));
1176 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1178 const char* hash_fn;
1179 const char* equal_fn;
1180 this->type_functions(&hash_fn, &equal_fn);
1182 ++p;
1183 gcc_assert(p->field_name() == "hashfn");
1184 Function_type* fntype = p->type()->function_type();
1185 Named_object* no = Named_object::make_function_declaration(hash_fn, NULL,
1186 fntype,
1187 bloc);
1188 no->func_declaration_value()->set_asm_name(hash_fn);
1189 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1191 ++p;
1192 gcc_assert(p->field_name() == "equalfn");
1193 fntype = p->type()->function_type();
1194 no = Named_object::make_function_declaration(equal_fn, NULL, fntype, bloc);
1195 no->func_declaration_value()->set_asm_name(equal_fn);
1196 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1198 ++p;
1199 gcc_assert(p->field_name() == "string");
1200 Expression* s = Expression::make_string((name != NULL
1201 ? name->reflection(gogo)
1202 : this->reflection(gogo)),
1203 bloc);
1204 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1206 ++p;
1207 gcc_assert(p->field_name() == "uncommonType");
1208 if (name == NULL && methods == NULL)
1209 vals->push_back(Expression::make_nil(bloc));
1210 else
1212 if (methods == NULL)
1213 methods = name->methods();
1214 vals->push_back(this->uncommon_type_constructor(gogo,
1215 p->type()->deref(),
1216 name, methods,
1217 only_value_methods));
1220 ++p;
1221 gcc_assert(p == fields->end());
1223 mpz_clear(iv);
1225 return Expression::make_struct_composite_literal(td_type, vals, bloc);
1228 // Return a composite literal for the uncommon type information for
1229 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1230 // struct. If name is not NULL, it is the name of the type. If
1231 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
1232 // is true if only value methods should be included. At least one of
1233 // NAME and METHODS must not be NULL.
1235 Expression*
1236 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1237 Named_type* name, const Methods* methods,
1238 bool only_value_methods) const
1240 source_location bloc = BUILTINS_LOCATION;
1242 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1244 Expression_list* vals = new Expression_list();
1245 vals->reserve(3);
1247 Struct_field_list::const_iterator p = fields->begin();
1248 gcc_assert(p->field_name() == "name");
1250 ++p;
1251 gcc_assert(p->field_name() == "pkgPath");
1253 if (name == NULL)
1255 vals->push_back(Expression::make_nil(bloc));
1256 vals->push_back(Expression::make_nil(bloc));
1258 else
1260 Named_object* no = name->named_object();
1261 std::string n = Gogo::unpack_hidden_name(no->name());
1262 Expression* s = Expression::make_string(n, bloc);
1263 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1265 if (name->is_builtin())
1266 vals->push_back(Expression::make_nil(bloc));
1267 else
1269 const Package* package = no->package();
1270 const std::string& unique_prefix(package == NULL
1271 ? gogo->unique_prefix()
1272 : package->unique_prefix());
1273 const std::string& package_name(package == NULL
1274 ? gogo->package_name()
1275 : package->name());
1276 n.assign(unique_prefix);
1277 n.append(1, '.');
1278 n.append(package_name);
1279 if (name->in_function() != NULL)
1281 n.append(1, '.');
1282 n.append(Gogo::unpack_hidden_name(name->in_function()->name()));
1284 s = Expression::make_string(n, bloc);
1285 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1289 ++p;
1290 gcc_assert(p->field_name() == "methods");
1291 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
1292 only_value_methods));
1294 ++p;
1295 gcc_assert(p == fields->end());
1297 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
1298 vals, bloc);
1299 return Expression::make_unary(OPERATOR_AND, r, bloc);
1302 // Sort methods by name.
1304 class Sort_methods
1306 public:
1307 bool
1308 operator()(const std::pair<std::string, const Method*>& m1,
1309 const std::pair<std::string, const Method*>& m2) const
1310 { return m1.first < m2.first; }
1313 // Return a composite literal for the type method table for this type.
1314 // METHODS_TYPE is the type of the table, and is a slice type.
1315 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
1316 // then only value methods are used.
1318 Expression*
1319 Type::methods_constructor(Gogo* gogo, Type* methods_type,
1320 const Methods* methods,
1321 bool only_value_methods) const
1323 source_location bloc = BUILTINS_LOCATION;
1325 std::vector<std::pair<std::string, const Method*> > smethods;
1326 if (methods != NULL)
1328 smethods.reserve(methods->count());
1329 for (Methods::const_iterator p = methods->begin();
1330 p != methods->end();
1331 ++p)
1333 if (p->second->is_ambiguous())
1334 continue;
1335 if (only_value_methods && !p->second->is_value_method())
1336 continue;
1337 smethods.push_back(std::make_pair(p->first, p->second));
1341 if (smethods.empty())
1342 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
1344 std::sort(smethods.begin(), smethods.end(), Sort_methods());
1346 Type* method_type = methods_type->array_type()->element_type();
1348 Expression_list* vals = new Expression_list();
1349 vals->reserve(smethods.size());
1350 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
1351 = smethods.begin();
1352 p != smethods.end();
1353 ++p)
1354 vals->push_back(this->method_constructor(gogo, method_type, p->first,
1355 p->second));
1357 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
1360 // Return a composite literal for a single method. METHOD_TYPE is the
1361 // type of the entry. METHOD_NAME is the name of the method and M is
1362 // the method information.
1364 Expression*
1365 Type::method_constructor(Gogo*, Type* method_type,
1366 const std::string& method_name,
1367 const Method* m) const
1369 source_location bloc = BUILTINS_LOCATION;
1371 const Struct_field_list* fields = method_type->struct_type()->fields();
1373 Expression_list* vals = new Expression_list();
1374 vals->reserve(5);
1376 Struct_field_list::const_iterator p = fields->begin();
1377 gcc_assert(p->field_name() == "name");
1378 const std::string n = Gogo::unpack_hidden_name(method_name);
1379 Expression* s = Expression::make_string(n, bloc);
1380 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1382 ++p;
1383 gcc_assert(p->field_name() == "pkgPath");
1384 if (!Gogo::is_hidden_name(method_name))
1385 vals->push_back(Expression::make_nil(bloc));
1386 else
1388 s = Expression::make_string(Gogo::hidden_name_prefix(method_name), bloc);
1389 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1392 Named_object* no = (m->needs_stub_method()
1393 ? m->stub_object()
1394 : m->named_object());
1396 Function_type* mtype;
1397 if (no->is_function())
1398 mtype = no->func_value()->type();
1399 else
1400 mtype = no->func_declaration_value()->type();
1401 gcc_assert(mtype->is_method());
1402 Type* nonmethod_type = mtype->copy_without_receiver();
1404 ++p;
1405 gcc_assert(p->field_name() == "mtyp");
1406 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
1408 ++p;
1409 gcc_assert(p->field_name() == "typ");
1410 vals->push_back(Expression::make_type_descriptor(mtype, bloc));
1412 ++p;
1413 gcc_assert(p->field_name() == "tfn");
1414 vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1416 ++p;
1417 gcc_assert(p == fields->end());
1419 return Expression::make_struct_composite_literal(method_type, vals, bloc);
1422 // Return a composite literal for the type descriptor of a plain type.
1423 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
1424 // NULL, it is the name to use as well as the list of methods.
1426 Expression*
1427 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
1428 Named_type* name)
1430 return this->type_descriptor_constructor(gogo, runtime_type_kind,
1431 name, NULL, true);
1434 // Return the type reflection string for this type.
1436 std::string
1437 Type::reflection(Gogo* gogo) const
1439 std::string ret;
1441 // The do_reflection virtual function should set RET to the
1442 // reflection string.
1443 this->do_reflection(gogo, &ret);
1445 return ret;
1448 // Return a mangled name for the type.
1450 std::string
1451 Type::mangled_name(Gogo* gogo) const
1453 std::string ret;
1455 // The do_mangled_name virtual function should set RET to the
1456 // mangled name. For a composite type it should append a code for
1457 // the composition and then call do_mangled_name on the components.
1458 this->do_mangled_name(gogo, &ret);
1460 return ret;
1463 // Default function to export a type.
1465 void
1466 Type::do_export(Export*) const
1468 gcc_unreachable();
1471 // Import a type.
1473 Type*
1474 Type::import_type(Import* imp)
1476 if (imp->match_c_string("("))
1477 return Function_type::do_import(imp);
1478 else if (imp->match_c_string("*"))
1479 return Pointer_type::do_import(imp);
1480 else if (imp->match_c_string("struct "))
1481 return Struct_type::do_import(imp);
1482 else if (imp->match_c_string("["))
1483 return Array_type::do_import(imp);
1484 else if (imp->match_c_string("map "))
1485 return Map_type::do_import(imp);
1486 else if (imp->match_c_string("chan "))
1487 return Channel_type::do_import(imp);
1488 else if (imp->match_c_string("interface"))
1489 return Interface_type::do_import(imp);
1490 else
1492 error_at(imp->location(), "import error: expected type");
1493 return Type::make_error_type();
1497 // A type used to indicate a parsing error. This exists to simplify
1498 // later error detection.
1500 class Error_type : public Type
1502 public:
1503 Error_type()
1504 : Type(TYPE_ERROR)
1507 protected:
1508 tree
1509 do_get_tree(Gogo*)
1510 { return error_mark_node; }
1512 tree
1513 do_get_init_tree(Gogo*, tree, bool)
1514 { return error_mark_node; }
1516 Expression*
1517 do_type_descriptor(Gogo*, Named_type*)
1518 { return Expression::make_error(BUILTINS_LOCATION); }
1520 void
1521 do_reflection(Gogo*, std::string*) const
1522 { gcc_assert(saw_errors()); }
1524 void
1525 do_mangled_name(Gogo*, std::string* ret) const
1526 { ret->push_back('E'); }
1529 Type*
1530 Type::make_error_type()
1532 static Error_type singleton_error_type;
1533 return &singleton_error_type;
1536 // The void type.
1538 class Void_type : public Type
1540 public:
1541 Void_type()
1542 : Type(TYPE_VOID)
1545 protected:
1546 tree
1547 do_get_tree(Gogo*)
1548 { return void_type_node; }
1550 tree
1551 do_get_init_tree(Gogo*, tree, bool)
1552 { gcc_unreachable(); }
1554 Expression*
1555 do_type_descriptor(Gogo*, Named_type*)
1556 { gcc_unreachable(); }
1558 void
1559 do_reflection(Gogo*, std::string*) const
1562 void
1563 do_mangled_name(Gogo*, std::string* ret) const
1564 { ret->push_back('v'); }
1567 Type*
1568 Type::make_void_type()
1570 static Void_type singleton_void_type;
1571 return &singleton_void_type;
1574 // The boolean type.
1576 class Boolean_type : public Type
1578 public:
1579 Boolean_type()
1580 : Type(TYPE_BOOLEAN)
1583 protected:
1584 tree
1585 do_get_tree(Gogo*)
1586 { return boolean_type_node; }
1588 tree
1589 do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
1590 { return is_clear ? NULL : fold_convert(type_tree, boolean_false_node); }
1592 Expression*
1593 do_type_descriptor(Gogo*, Named_type* name);
1595 // We should not be asked for the reflection string of a basic type.
1596 void
1597 do_reflection(Gogo*, std::string* ret) const
1598 { ret->append("bool"); }
1600 void
1601 do_mangled_name(Gogo*, std::string* ret) const
1602 { ret->push_back('b'); }
1605 // Make the type descriptor.
1607 Expression*
1608 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1610 if (name != NULL)
1611 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
1612 else
1614 Named_object* no = gogo->lookup_global("bool");
1615 gcc_assert(no != NULL);
1616 return Type::type_descriptor(gogo, no->type_value());
1620 Type*
1621 Type::make_boolean_type()
1623 static Boolean_type boolean_type;
1624 return &boolean_type;
1627 // The named type "bool".
1629 static Named_type* named_bool_type;
1631 // Get the named type "bool".
1633 Named_type*
1634 Type::lookup_bool_type()
1636 return named_bool_type;
1639 // Make the named type "bool".
1641 Named_type*
1642 Type::make_named_bool_type()
1644 Type* bool_type = Type::make_boolean_type();
1645 Named_object* named_object = Named_object::make_type("bool", NULL,
1646 bool_type,
1647 BUILTINS_LOCATION);
1648 Named_type* named_type = named_object->type_value();
1649 named_bool_type = named_type;
1650 return named_type;
1653 // Class Integer_type.
1655 Integer_type::Named_integer_types Integer_type::named_integer_types;
1657 // Create a new integer type. Non-abstract integer types always have
1658 // names.
1660 Named_type*
1661 Integer_type::create_integer_type(const char* name, bool is_unsigned,
1662 int bits, int runtime_type_kind)
1664 Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
1665 runtime_type_kind);
1666 std::string sname(name);
1667 Named_object* named_object = Named_object::make_type(sname, NULL,
1668 integer_type,
1669 BUILTINS_LOCATION);
1670 Named_type* named_type = named_object->type_value();
1671 std::pair<Named_integer_types::iterator, bool> ins =
1672 Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
1673 gcc_assert(ins.second);
1674 return named_type;
1677 // Look up an existing integer type.
1679 Named_type*
1680 Integer_type::lookup_integer_type(const char* name)
1682 Named_integer_types::const_iterator p =
1683 Integer_type::named_integer_types.find(name);
1684 gcc_assert(p != Integer_type::named_integer_types.end());
1685 return p->second;
1688 // Create a new abstract integer type.
1690 Integer_type*
1691 Integer_type::create_abstract_integer_type()
1693 static Integer_type* abstract_type;
1694 if (abstract_type == NULL)
1695 abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
1696 RUNTIME_TYPE_KIND_INT);
1697 return abstract_type;
1700 // Integer type compatibility.
1702 bool
1703 Integer_type::is_identical(const Integer_type* t) const
1705 if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
1706 return false;
1707 return this->is_abstract_ == t->is_abstract_;
1710 // Hash code.
1712 unsigned int
1713 Integer_type::do_hash_for_method(Gogo*) const
1715 return ((this->bits_ << 4)
1716 + ((this->is_unsigned_ ? 1 : 0) << 8)
1717 + ((this->is_abstract_ ? 1 : 0) << 9));
1720 // Get the tree for an Integer_type.
1722 tree
1723 Integer_type::do_get_tree(Gogo*)
1725 gcc_assert(!this->is_abstract_);
1726 if (this->is_unsigned_)
1728 if (this->bits_ == INT_TYPE_SIZE)
1729 return unsigned_type_node;
1730 else if (this->bits_ == CHAR_TYPE_SIZE)
1731 return unsigned_char_type_node;
1732 else if (this->bits_ == SHORT_TYPE_SIZE)
1733 return short_unsigned_type_node;
1734 else if (this->bits_ == LONG_TYPE_SIZE)
1735 return long_unsigned_type_node;
1736 else if (this->bits_ == LONG_LONG_TYPE_SIZE)
1737 return long_long_unsigned_type_node;
1738 else
1739 return make_unsigned_type(this->bits_);
1741 else
1743 if (this->bits_ == INT_TYPE_SIZE)
1744 return integer_type_node;
1745 else if (this->bits_ == CHAR_TYPE_SIZE)
1746 return signed_char_type_node;
1747 else if (this->bits_ == SHORT_TYPE_SIZE)
1748 return short_integer_type_node;
1749 else if (this->bits_ == LONG_TYPE_SIZE)
1750 return long_integer_type_node;
1751 else if (this->bits_ == LONG_LONG_TYPE_SIZE)
1752 return long_long_integer_type_node;
1753 else
1754 return make_signed_type(this->bits_);
1758 tree
1759 Integer_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
1761 return is_clear ? NULL : build_int_cst(type_tree, 0);
1764 // The type descriptor for an integer type. Integer types are always
1765 // named.
1767 Expression*
1768 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1770 gcc_assert(name != NULL);
1771 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
1774 // We should not be asked for the reflection string of a basic type.
1776 void
1777 Integer_type::do_reflection(Gogo*, std::string*) const
1779 gcc_unreachable();
1782 // Mangled name.
1784 void
1785 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
1787 char buf[100];
1788 snprintf(buf, sizeof buf, "i%s%s%de",
1789 this->is_abstract_ ? "a" : "",
1790 this->is_unsigned_ ? "u" : "",
1791 this->bits_);
1792 ret->append(buf);
1795 // Make an integer type.
1797 Named_type*
1798 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
1799 int runtime_type_kind)
1801 return Integer_type::create_integer_type(name, is_unsigned, bits,
1802 runtime_type_kind);
1805 // Make an abstract integer type.
1807 Integer_type*
1808 Type::make_abstract_integer_type()
1810 return Integer_type::create_abstract_integer_type();
1813 // Look up an integer type.
1815 Named_type*
1816 Type::lookup_integer_type(const char* name)
1818 return Integer_type::lookup_integer_type(name);
1821 // Class Float_type.
1823 Float_type::Named_float_types Float_type::named_float_types;
1825 // Create a new float type. Non-abstract float types always have
1826 // names.
1828 Named_type*
1829 Float_type::create_float_type(const char* name, int bits,
1830 int runtime_type_kind)
1832 Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
1833 std::string sname(name);
1834 Named_object* named_object = Named_object::make_type(sname, NULL, float_type,
1835 BUILTINS_LOCATION);
1836 Named_type* named_type = named_object->type_value();
1837 std::pair<Named_float_types::iterator, bool> ins =
1838 Float_type::named_float_types.insert(std::make_pair(sname, named_type));
1839 gcc_assert(ins.second);
1840 return named_type;
1843 // Look up an existing float type.
1845 Named_type*
1846 Float_type::lookup_float_type(const char* name)
1848 Named_float_types::const_iterator p =
1849 Float_type::named_float_types.find(name);
1850 gcc_assert(p != Float_type::named_float_types.end());
1851 return p->second;
1854 // Create a new abstract float type.
1856 Float_type*
1857 Float_type::create_abstract_float_type()
1859 static Float_type* abstract_type;
1860 if (abstract_type == NULL)
1861 abstract_type = new Float_type(true, FLOAT_TYPE_SIZE,
1862 RUNTIME_TYPE_KIND_FLOAT);
1863 return abstract_type;
1866 // Whether this type is identical with T.
1868 bool
1869 Float_type::is_identical(const Float_type* t) const
1871 if (this->bits_ != t->bits_)
1872 return false;
1873 return this->is_abstract_ == t->is_abstract_;
1876 // Hash code.
1878 unsigned int
1879 Float_type::do_hash_for_method(Gogo*) const
1881 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
1884 // Get a tree without using a Gogo*.
1886 tree
1887 Float_type::type_tree() const
1889 if (this->bits_ == FLOAT_TYPE_SIZE)
1890 return float_type_node;
1891 else if (this->bits_ == DOUBLE_TYPE_SIZE)
1892 return double_type_node;
1893 else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE)
1894 return long_double_type_node;
1895 else
1897 tree ret = make_node(REAL_TYPE);
1898 TYPE_PRECISION(ret) = this->bits_;
1899 layout_type(ret);
1900 return ret;
1904 // Get a tree.
1906 tree
1907 Float_type::do_get_tree(Gogo*)
1909 return this->type_tree();
1912 tree
1913 Float_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
1915 if (is_clear)
1916 return NULL;
1917 REAL_VALUE_TYPE r;
1918 real_from_integer(&r, TYPE_MODE(type_tree), 0, 0, 0);
1919 return build_real(type_tree, r);
1922 // The type descriptor for a float type. Float types are always named.
1924 Expression*
1925 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
1927 gcc_assert(name != NULL);
1928 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
1931 // We should not be asked for the reflection string of a basic type.
1933 void
1934 Float_type::do_reflection(Gogo*, std::string*) const
1936 gcc_unreachable();
1939 // Mangled name.
1941 void
1942 Float_type::do_mangled_name(Gogo*, std::string* ret) const
1944 char buf[100];
1945 snprintf(buf, sizeof buf, "f%s%de",
1946 this->is_abstract_ ? "a" : "",
1947 this->bits_);
1948 ret->append(buf);
1951 // Make a floating point type.
1953 Named_type*
1954 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
1956 return Float_type::create_float_type(name, bits, runtime_type_kind);
1959 // Make an abstract float type.
1961 Float_type*
1962 Type::make_abstract_float_type()
1964 return Float_type::create_abstract_float_type();
1967 // Look up a float type.
1969 Named_type*
1970 Type::lookup_float_type(const char* name)
1972 return Float_type::lookup_float_type(name);
1975 // Class Complex_type.
1977 Complex_type::Named_complex_types Complex_type::named_complex_types;
1979 // Create a new complex type. Non-abstract complex types always have
1980 // names.
1982 Named_type*
1983 Complex_type::create_complex_type(const char* name, int bits,
1984 int runtime_type_kind)
1986 Complex_type* complex_type = new Complex_type(false, bits,
1987 runtime_type_kind);
1988 std::string sname(name);
1989 Named_object* named_object = Named_object::make_type(sname, NULL,
1990 complex_type,
1991 BUILTINS_LOCATION);
1992 Named_type* named_type = named_object->type_value();
1993 std::pair<Named_complex_types::iterator, bool> ins =
1994 Complex_type::named_complex_types.insert(std::make_pair(sname,
1995 named_type));
1996 gcc_assert(ins.second);
1997 return named_type;
2000 // Look up an existing complex type.
2002 Named_type*
2003 Complex_type::lookup_complex_type(const char* name)
2005 Named_complex_types::const_iterator p =
2006 Complex_type::named_complex_types.find(name);
2007 gcc_assert(p != Complex_type::named_complex_types.end());
2008 return p->second;
2011 // Create a new abstract complex type.
2013 Complex_type*
2014 Complex_type::create_abstract_complex_type()
2016 static Complex_type* abstract_type;
2017 if (abstract_type == NULL)
2018 abstract_type = new Complex_type(true, FLOAT_TYPE_SIZE * 2,
2019 RUNTIME_TYPE_KIND_FLOAT);
2020 return abstract_type;
2023 // Whether this type is identical with T.
2025 bool
2026 Complex_type::is_identical(const Complex_type *t) const
2028 if (this->bits_ != t->bits_)
2029 return false;
2030 return this->is_abstract_ == t->is_abstract_;
2033 // Hash code.
2035 unsigned int
2036 Complex_type::do_hash_for_method(Gogo*) const
2038 return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2041 // Get a tree without using a Gogo*.
2043 tree
2044 Complex_type::type_tree() const
2046 if (this->bits_ == FLOAT_TYPE_SIZE * 2)
2047 return complex_float_type_node;
2048 else if (this->bits_ == DOUBLE_TYPE_SIZE * 2)
2049 return complex_double_type_node;
2050 else if (this->bits_ == LONG_DOUBLE_TYPE_SIZE * 2)
2051 return complex_long_double_type_node;
2052 else
2054 tree ret = make_node(REAL_TYPE);
2055 TYPE_PRECISION(ret) = this->bits_ / 2;
2056 layout_type(ret);
2057 return build_complex_type(ret);
2061 // Get a tree.
2063 tree
2064 Complex_type::do_get_tree(Gogo*)
2066 return this->type_tree();
2069 // Zero initializer.
2071 tree
2072 Complex_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2074 if (is_clear)
2075 return NULL;
2076 REAL_VALUE_TYPE r;
2077 real_from_integer(&r, TYPE_MODE(TREE_TYPE(type_tree)), 0, 0, 0);
2078 return build_complex(type_tree, build_real(TREE_TYPE(type_tree), r),
2079 build_real(TREE_TYPE(type_tree), r));
2082 // The type descriptor for a complex type. Complex types are always
2083 // named.
2085 Expression*
2086 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2088 gcc_assert(name != NULL);
2089 return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2092 // We should not be asked for the reflection string of a basic type.
2094 void
2095 Complex_type::do_reflection(Gogo*, std::string*) const
2097 gcc_unreachable();
2100 // Mangled name.
2102 void
2103 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2105 char buf[100];
2106 snprintf(buf, sizeof buf, "c%s%de",
2107 this->is_abstract_ ? "a" : "",
2108 this->bits_);
2109 ret->append(buf);
2112 // Make a complex type.
2114 Named_type*
2115 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2117 return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2120 // Make an abstract complex type.
2122 Complex_type*
2123 Type::make_abstract_complex_type()
2125 return Complex_type::create_abstract_complex_type();
2128 // Look up a complex type.
2130 Named_type*
2131 Type::lookup_complex_type(const char* name)
2133 return Complex_type::lookup_complex_type(name);
2136 // Class String_type.
2138 // Return the tree for String_type. A string is a struct with two
2139 // fields: a pointer to the characters and a length.
2141 tree
2142 String_type::do_get_tree(Gogo*)
2144 static tree struct_type;
2145 return Gogo::builtin_struct(&struct_type, "__go_string", NULL_TREE, 2,
2146 "__data",
2147 build_pointer_type(unsigned_char_type_node),
2148 "__length",
2149 integer_type_node);
2152 // Return a tree for the length of STRING.
2154 tree
2155 String_type::length_tree(Gogo*, tree string)
2157 tree string_type = TREE_TYPE(string);
2158 gcc_assert(TREE_CODE(string_type) == RECORD_TYPE);
2159 tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2160 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2161 "__length") == 0);
2162 return fold_build3(COMPONENT_REF, integer_type_node, string,
2163 length_field, NULL_TREE);
2166 // Return a tree for a pointer to the bytes of STRING.
2168 tree
2169 String_type::bytes_tree(Gogo*, tree string)
2171 tree string_type = TREE_TYPE(string);
2172 gcc_assert(TREE_CODE(string_type) == RECORD_TYPE);
2173 tree bytes_field = TYPE_FIELDS(string_type);
2174 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
2175 "__data") == 0);
2176 return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
2177 bytes_field, NULL_TREE);
2180 // We initialize a string to { NULL, 0 }.
2182 tree
2183 String_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2185 if (is_clear)
2186 return NULL_TREE;
2188 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
2190 VEC(constructor_elt, gc)* init = VEC_alloc(constructor_elt, gc, 2);
2192 for (tree field = TYPE_FIELDS(type_tree);
2193 field != NULL_TREE;
2194 field = DECL_CHAIN(field))
2196 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
2197 elt->index = field;
2198 elt->value = fold_convert(TREE_TYPE(field), size_zero_node);
2201 tree ret = build_constructor(type_tree, init);
2202 TREE_CONSTANT(ret) = 1;
2203 return ret;
2206 // The type descriptor for the string type.
2208 Expression*
2209 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2211 if (name != NULL)
2212 return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
2213 else
2215 Named_object* no = gogo->lookup_global("string");
2216 gcc_assert(no != NULL);
2217 return Type::type_descriptor(gogo, no->type_value());
2221 // We should not be asked for the reflection string of a basic type.
2223 void
2224 String_type::do_reflection(Gogo*, std::string* ret) const
2226 ret->append("string");
2229 // Mangled name of a string type.
2231 void
2232 String_type::do_mangled_name(Gogo*, std::string* ret) const
2234 ret->push_back('z');
2237 // Make a string type.
2239 Type*
2240 Type::make_string_type()
2242 static String_type string_type;
2243 return &string_type;
2246 // The named type "string".
2248 static Named_type* named_string_type;
2250 // Get the named type "string".
2252 Named_type*
2253 Type::lookup_string_type()
2255 return named_string_type;
2258 // Make the named type string.
2260 Named_type*
2261 Type::make_named_string_type()
2263 Type* string_type = Type::make_string_type();
2264 Named_object* named_object = Named_object::make_type("string", NULL,
2265 string_type,
2266 BUILTINS_LOCATION);
2267 Named_type* named_type = named_object->type_value();
2268 named_string_type = named_type;
2269 return named_type;
2272 // The sink type. This is the type of the blank identifier _. Any
2273 // type may be assigned to it.
2275 class Sink_type : public Type
2277 public:
2278 Sink_type()
2279 : Type(TYPE_SINK)
2282 protected:
2283 tree
2284 do_get_tree(Gogo*)
2285 { gcc_unreachable(); }
2287 tree
2288 do_get_init_tree(Gogo*, tree, bool)
2289 { gcc_unreachable(); }
2291 Expression*
2292 do_type_descriptor(Gogo*, Named_type*)
2293 { gcc_unreachable(); }
2295 void
2296 do_reflection(Gogo*, std::string*) const
2297 { gcc_unreachable(); }
2299 void
2300 do_mangled_name(Gogo*, std::string*) const
2301 { gcc_unreachable(); }
2304 // Make the sink type.
2306 Type*
2307 Type::make_sink_type()
2309 static Sink_type sink_type;
2310 return &sink_type;
2313 // Class Function_type.
2315 // Traversal.
2318 Function_type::do_traverse(Traverse* traverse)
2320 if (this->receiver_ != NULL
2321 && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
2322 return TRAVERSE_EXIT;
2323 if (this->parameters_ != NULL
2324 && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
2325 return TRAVERSE_EXIT;
2326 if (this->results_ != NULL
2327 && this->results_->traverse(traverse) == TRAVERSE_EXIT)
2328 return TRAVERSE_EXIT;
2329 return TRAVERSE_CONTINUE;
2332 // Returns whether T is a valid redeclaration of this type. If this
2333 // returns false, and REASON is not NULL, *REASON may be set to a
2334 // brief explanation of why it returned false.
2336 bool
2337 Function_type::is_valid_redeclaration(const Function_type* t,
2338 std::string* reason) const
2340 if (!this->is_identical(t, false, true, reason))
2341 return false;
2343 // A redeclaration of a function is required to use the same names
2344 // for the receiver and parameters.
2345 if (this->receiver() != NULL
2346 && this->receiver()->name() != t->receiver()->name()
2347 && this->receiver()->name() != Import::import_marker
2348 && t->receiver()->name() != Import::import_marker)
2350 if (reason != NULL)
2351 *reason = "receiver name changed";
2352 return false;
2355 const Typed_identifier_list* parms1 = this->parameters();
2356 const Typed_identifier_list* parms2 = t->parameters();
2357 if (parms1 != NULL)
2359 Typed_identifier_list::const_iterator p1 = parms1->begin();
2360 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2361 p2 != parms2->end();
2362 ++p2, ++p1)
2364 if (p1->name() != p2->name()
2365 && p1->name() != Import::import_marker
2366 && p2->name() != Import::import_marker)
2368 if (reason != NULL)
2369 *reason = "parameter name changed";
2370 return false;
2373 // This is called at parse time, so we may have unknown
2374 // types.
2375 Type* t1 = p1->type()->forwarded();
2376 Type* t2 = p2->type()->forwarded();
2377 if (t1 != t2
2378 && t1->forward_declaration_type() != NULL
2379 && (t2->forward_declaration_type() == NULL
2380 || (t1->forward_declaration_type()->named_object()
2381 != t2->forward_declaration_type()->named_object())))
2382 return false;
2386 const Typed_identifier_list* results1 = this->results();
2387 const Typed_identifier_list* results2 = t->results();
2388 if (results1 != NULL)
2390 Typed_identifier_list::const_iterator res1 = results1->begin();
2391 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2392 res2 != results2->end();
2393 ++res2, ++res1)
2395 if (res1->name() != res2->name()
2396 && res1->name() != Import::import_marker
2397 && res2->name() != Import::import_marker)
2399 if (reason != NULL)
2400 *reason = "result name changed";
2401 return false;
2404 // This is called at parse time, so we may have unknown
2405 // types.
2406 Type* t1 = res1->type()->forwarded();
2407 Type* t2 = res2->type()->forwarded();
2408 if (t1 != t2
2409 && t1->forward_declaration_type() != NULL
2410 && (t2->forward_declaration_type() == NULL
2411 || (t1->forward_declaration_type()->named_object()
2412 != t2->forward_declaration_type()->named_object())))
2413 return false;
2417 return true;
2420 // Check whether T is the same as this type.
2422 bool
2423 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
2424 bool errors_are_identical,
2425 std::string* reason) const
2427 if (!ignore_receiver)
2429 const Typed_identifier* r1 = this->receiver();
2430 const Typed_identifier* r2 = t->receiver();
2431 if ((r1 != NULL) != (r2 != NULL))
2433 if (reason != NULL)
2434 *reason = _("different receiver types");
2435 return false;
2437 if (r1 != NULL)
2439 if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
2440 reason))
2442 if (reason != NULL && !reason->empty())
2443 *reason = "receiver: " + *reason;
2444 return false;
2449 const Typed_identifier_list* parms1 = this->parameters();
2450 const Typed_identifier_list* parms2 = t->parameters();
2451 if ((parms1 != NULL) != (parms2 != NULL))
2453 if (reason != NULL)
2454 *reason = _("different number of parameters");
2455 return false;
2457 if (parms1 != NULL)
2459 Typed_identifier_list::const_iterator p1 = parms1->begin();
2460 for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2461 p2 != parms2->end();
2462 ++p2, ++p1)
2464 if (p1 == parms1->end())
2466 if (reason != NULL)
2467 *reason = _("different number of parameters");
2468 return false;
2471 if (!Type::are_identical(p1->type(), p2->type(),
2472 errors_are_identical, NULL))
2474 if (reason != NULL)
2475 *reason = _("different parameter types");
2476 return false;
2479 if (p1 != parms1->end())
2481 if (reason != NULL)
2482 *reason = _("different number of parameters");
2483 return false;
2487 if (this->is_varargs() != t->is_varargs())
2489 if (reason != NULL)
2490 *reason = _("different varargs");
2491 return false;
2494 const Typed_identifier_list* results1 = this->results();
2495 const Typed_identifier_list* results2 = t->results();
2496 if ((results1 != NULL) != (results2 != NULL))
2498 if (reason != NULL)
2499 *reason = _("different number of results");
2500 return false;
2502 if (results1 != NULL)
2504 Typed_identifier_list::const_iterator res1 = results1->begin();
2505 for (Typed_identifier_list::const_iterator res2 = results2->begin();
2506 res2 != results2->end();
2507 ++res2, ++res1)
2509 if (res1 == results1->end())
2511 if (reason != NULL)
2512 *reason = _("different number of results");
2513 return false;
2516 if (!Type::are_identical(res1->type(), res2->type(),
2517 errors_are_identical, NULL))
2519 if (reason != NULL)
2520 *reason = _("different result types");
2521 return false;
2524 if (res1 != results1->end())
2526 if (reason != NULL)
2527 *reason = _("different number of results");
2528 return false;
2532 return true;
2535 // Hash code.
2537 unsigned int
2538 Function_type::do_hash_for_method(Gogo* gogo) const
2540 unsigned int ret = 0;
2541 // We ignore the receiver type for hash codes, because we need to
2542 // get the same hash code for a method in an interface and a method
2543 // declared for a type. The former will not have a receiver.
2544 if (this->parameters_ != NULL)
2546 int shift = 1;
2547 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
2548 p != this->parameters_->end();
2549 ++p, ++shift)
2550 ret += p->type()->hash_for_method(gogo) << shift;
2552 if (this->results_ != NULL)
2554 int shift = 2;
2555 for (Typed_identifier_list::const_iterator p = this->results_->begin();
2556 p != this->results_->end();
2557 ++p, ++shift)
2558 ret += p->type()->hash_for_method(gogo) << shift;
2560 if (this->is_varargs_)
2561 ret += 1;
2562 ret <<= 4;
2563 return ret;
2566 // Get the tree for a function type.
2568 tree
2569 Function_type::do_get_tree(Gogo* gogo)
2571 tree args = NULL_TREE;
2572 tree* pp = &args;
2574 if (this->receiver_ != NULL)
2576 Type* rtype = this->receiver_->type();
2577 tree ptype = rtype->get_tree(gogo);
2578 if (ptype == error_mark_node)
2579 return error_mark_node;
2581 // We always pass the address of the receiver parameter, in
2582 // order to make interface calls work with unknown types.
2583 if (rtype->points_to() == NULL)
2584 ptype = build_pointer_type(ptype);
2586 *pp = tree_cons (NULL_TREE, ptype, NULL_TREE);
2587 pp = &TREE_CHAIN (*pp);
2590 if (this->parameters_ != NULL)
2592 for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
2593 p != this->parameters_->end();
2594 ++p)
2596 tree ptype = p->type()->get_tree(gogo);
2597 if (ptype == error_mark_node)
2598 return error_mark_node;
2599 *pp = tree_cons (NULL_TREE, ptype, NULL_TREE);
2600 pp = &TREE_CHAIN (*pp);
2604 // Varargs is handled entirely at the Go level. At the tree level,
2605 // functions are not varargs.
2606 *pp = void_list_node;
2608 tree result;
2609 if (this->results_ == NULL)
2610 result = void_type_node;
2611 else if (this->results_->size() == 1)
2612 result = this->results_->begin()->type()->get_tree(gogo);
2613 else
2615 result = make_node(RECORD_TYPE);
2616 tree field_trees = NULL_TREE;
2617 tree* pp = &field_trees;
2618 for (Typed_identifier_list::const_iterator p = this->results_->begin();
2619 p != this->results_->end();
2620 ++p)
2622 const std::string name = (p->name().empty()
2623 ? "UNNAMED"
2624 : Gogo::unpack_hidden_name(p->name()));
2625 tree name_tree = get_identifier_with_length(name.data(),
2626 name.length());
2627 tree field_type_tree = p->type()->get_tree(gogo);
2628 if (field_type_tree == error_mark_node)
2629 return error_mark_node;
2630 tree field = build_decl(this->location_, FIELD_DECL, name_tree,
2631 field_type_tree);
2632 DECL_CONTEXT(field) = result;
2633 *pp = field;
2634 pp = &DECL_CHAIN(field);
2636 TYPE_FIELDS(result) = field_trees;
2637 layout_type(result);
2640 if (result == error_mark_node)
2641 return error_mark_node;
2643 tree fntype = build_function_type(result, args);
2644 if (fntype == error_mark_node)
2645 return fntype;
2647 return build_pointer_type(fntype);
2650 // Functions are initialized to NULL.
2652 tree
2653 Function_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
2655 if (is_clear)
2656 return NULL;
2657 return fold_convert(type_tree, null_pointer_node);
2660 // The type of a function type descriptor.
2662 Type*
2663 Function_type::make_function_type_descriptor_type()
2665 static Type* ret;
2666 if (ret == NULL)
2668 Type* tdt = Type::make_type_descriptor_type();
2669 Type* ptdt = Type::make_type_descriptor_ptr_type();
2671 Type* bool_type = Type::lookup_bool_type();
2673 Type* slice_type = Type::make_array_type(ptdt, NULL);
2675 Struct_type* s = Type::make_builtin_struct_type(4,
2676 "", tdt,
2677 "dotdotdot", bool_type,
2678 "in", slice_type,
2679 "out", slice_type);
2681 ret = Type::make_builtin_named_type("FuncType", s);
2684 return ret;
2687 // The type descriptor for a function type.
2689 Expression*
2690 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2692 source_location bloc = BUILTINS_LOCATION;
2694 Type* ftdt = Function_type::make_function_type_descriptor_type();
2696 const Struct_field_list* fields = ftdt->struct_type()->fields();
2698 Expression_list* vals = new Expression_list();
2699 vals->reserve(4);
2701 Struct_field_list::const_iterator p = fields->begin();
2702 gcc_assert(p->field_name() == "commonType");
2703 vals->push_back(this->type_descriptor_constructor(gogo,
2704 RUNTIME_TYPE_KIND_FUNC,
2705 name, NULL, true));
2707 ++p;
2708 gcc_assert(p->field_name() == "dotdotdot");
2709 vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
2711 ++p;
2712 gcc_assert(p->field_name() == "in");
2713 vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
2714 this->parameters()));
2716 ++p;
2717 gcc_assert(p->field_name() == "out");
2718 vals->push_back(this->type_descriptor_params(p->type(), NULL,
2719 this->results()));
2721 ++p;
2722 gcc_assert(p == fields->end());
2724 return Expression::make_struct_composite_literal(ftdt, vals, bloc);
2727 // Return a composite literal for the parameters or results of a type
2728 // descriptor.
2730 Expression*
2731 Function_type::type_descriptor_params(Type* params_type,
2732 const Typed_identifier* receiver,
2733 const Typed_identifier_list* params)
2735 source_location bloc = BUILTINS_LOCATION;
2737 if (receiver == NULL && params == NULL)
2738 return Expression::make_slice_composite_literal(params_type, NULL, bloc);
2740 Expression_list* vals = new Expression_list();
2741 vals->reserve((params == NULL ? 0 : params->size())
2742 + (receiver != NULL ? 1 : 0));
2744 if (receiver != NULL)
2746 Type* rtype = receiver->type();
2747 // The receiver is always passed as a pointer. FIXME: Is this
2748 // right? Should that fact affect the type descriptor?
2749 if (rtype->points_to() == NULL)
2750 rtype = Type::make_pointer_type(rtype);
2751 vals->push_back(Expression::make_type_descriptor(rtype, bloc));
2754 if (params != NULL)
2756 for (Typed_identifier_list::const_iterator p = params->begin();
2757 p != params->end();
2758 ++p)
2759 vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
2762 return Expression::make_slice_composite_literal(params_type, vals, bloc);
2765 // The reflection string.
2767 void
2768 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
2770 // FIXME: Turn this off until we straighten out the type of the
2771 // struct field used in a go statement which calls a method.
2772 // gcc_assert(this->receiver_ == NULL);
2774 ret->append("func");
2776 if (this->receiver_ != NULL)
2778 ret->push_back('(');
2779 this->append_reflection(this->receiver_->type(), gogo, ret);
2780 ret->push_back(')');
2783 ret->push_back('(');
2784 const Typed_identifier_list* params = this->parameters();
2785 if (params != NULL)
2787 bool is_varargs = this->is_varargs_;
2788 for (Typed_identifier_list::const_iterator p = params->begin();
2789 p != params->end();
2790 ++p)
2792 if (p != params->begin())
2793 ret->append(", ");
2794 if (!is_varargs || p + 1 != params->end())
2795 this->append_reflection(p->type(), gogo, ret);
2796 else
2798 ret->append("...");
2799 this->append_reflection(p->type()->array_type()->element_type(),
2800 gogo, ret);
2804 ret->push_back(')');
2806 const Typed_identifier_list* results = this->results();
2807 if (results != NULL && !results->empty())
2809 if (results->size() == 1)
2810 ret->push_back(' ');
2811 else
2812 ret->append(" (");
2813 for (Typed_identifier_list::const_iterator p = results->begin();
2814 p != results->end();
2815 ++p)
2817 if (p != results->begin())
2818 ret->append(", ");
2819 this->append_reflection(p->type(), gogo, ret);
2821 if (results->size() > 1)
2822 ret->push_back(')');
2826 // Mangled name.
2828 void
2829 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
2831 ret->push_back('F');
2833 if (this->receiver_ != NULL)
2835 ret->push_back('m');
2836 this->append_mangled_name(this->receiver_->type(), gogo, ret);
2839 const Typed_identifier_list* params = this->parameters();
2840 if (params != NULL)
2842 ret->push_back('p');
2843 for (Typed_identifier_list::const_iterator p = params->begin();
2844 p != params->end();
2845 ++p)
2846 this->append_mangled_name(p->type(), gogo, ret);
2847 if (this->is_varargs_)
2848 ret->push_back('V');
2849 ret->push_back('e');
2852 const Typed_identifier_list* results = this->results();
2853 if (results != NULL)
2855 ret->push_back('r');
2856 for (Typed_identifier_list::const_iterator p = results->begin();
2857 p != results->end();
2858 ++p)
2859 this->append_mangled_name(p->type(), gogo, ret);
2860 ret->push_back('e');
2863 ret->push_back('e');
2866 // Export a function type.
2868 void
2869 Function_type::do_export(Export* exp) const
2871 // We don't write out the receiver. The only function types which
2872 // should have a receiver are the ones associated with explicitly
2873 // defined methods. For those the receiver type is written out by
2874 // Function::export_func.
2876 exp->write_c_string("(");
2877 bool first = true;
2878 if (this->parameters_ != NULL)
2880 bool is_varargs = this->is_varargs_;
2881 for (Typed_identifier_list::const_iterator p =
2882 this->parameters_->begin();
2883 p != this->parameters_->end();
2884 ++p)
2886 if (first)
2887 first = false;
2888 else
2889 exp->write_c_string(", ");
2890 if (!is_varargs || p + 1 != this->parameters_->end())
2891 exp->write_type(p->type());
2892 else
2894 exp->write_c_string("...");
2895 exp->write_type(p->type()->array_type()->element_type());
2899 exp->write_c_string(")");
2901 const Typed_identifier_list* results = this->results_;
2902 if (results != NULL)
2904 exp->write_c_string(" ");
2905 if (results->size() == 1)
2906 exp->write_type(results->begin()->type());
2907 else
2909 first = true;
2910 exp->write_c_string("(");
2911 for (Typed_identifier_list::const_iterator p = results->begin();
2912 p != results->end();
2913 ++p)
2915 if (first)
2916 first = false;
2917 else
2918 exp->write_c_string(", ");
2919 exp->write_type(p->type());
2921 exp->write_c_string(")");
2926 // Import a function type.
2928 Function_type*
2929 Function_type::do_import(Import* imp)
2931 imp->require_c_string("(");
2932 Typed_identifier_list* parameters;
2933 bool is_varargs = false;
2934 if (imp->peek_char() == ')')
2935 parameters = NULL;
2936 else
2938 parameters = new Typed_identifier_list();
2939 while (true)
2941 if (imp->match_c_string("..."))
2943 imp->advance(3);
2944 is_varargs = true;
2947 Type* ptype = imp->read_type();
2948 if (is_varargs)
2949 ptype = Type::make_array_type(ptype, NULL);
2950 parameters->push_back(Typed_identifier(Import::import_marker,
2951 ptype, imp->location()));
2952 if (imp->peek_char() != ',')
2953 break;
2954 gcc_assert(!is_varargs);
2955 imp->require_c_string(", ");
2958 imp->require_c_string(")");
2960 Typed_identifier_list* results;
2961 if (imp->peek_char() != ' ')
2962 results = NULL;
2963 else
2965 imp->advance(1);
2966 results = new Typed_identifier_list;
2967 if (imp->peek_char() != '(')
2969 Type* rtype = imp->read_type();
2970 results->push_back(Typed_identifier(Import::import_marker, rtype,
2971 imp->location()));
2973 else
2975 imp->advance(1);
2976 while (true)
2978 Type* rtype = imp->read_type();
2979 results->push_back(Typed_identifier(Import::import_marker,
2980 rtype, imp->location()));
2981 if (imp->peek_char() != ',')
2982 break;
2983 imp->require_c_string(", ");
2985 imp->require_c_string(")");
2989 Function_type* ret = Type::make_function_type(NULL, parameters, results,
2990 imp->location());
2991 if (is_varargs)
2992 ret->set_is_varargs();
2993 return ret;
2996 // Make a copy of a function type without a receiver.
2998 Function_type*
2999 Function_type::copy_without_receiver() const
3001 gcc_assert(this->is_method());
3002 Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3003 this->results_,
3004 this->location_);
3005 if (this->is_varargs())
3006 ret->set_is_varargs();
3007 if (this->is_builtin())
3008 ret->set_is_builtin();
3009 return ret;
3012 // Make a copy of a function type with a receiver.
3014 Function_type*
3015 Function_type::copy_with_receiver(Type* receiver_type) const
3017 gcc_assert(!this->is_method());
3018 Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3019 this->location_);
3020 return Type::make_function_type(receiver, this->parameters_,
3021 this->results_, this->location_);
3024 // Make a function type.
3026 Function_type*
3027 Type::make_function_type(Typed_identifier* receiver,
3028 Typed_identifier_list* parameters,
3029 Typed_identifier_list* results,
3030 source_location location)
3032 return new Function_type(receiver, parameters, results, location);
3035 // Class Pointer_type.
3037 // Traversal.
3040 Pointer_type::do_traverse(Traverse* traverse)
3042 return Type::traverse(this->to_type_, traverse);
3045 // Hash code.
3047 unsigned int
3048 Pointer_type::do_hash_for_method(Gogo* gogo) const
3050 return this->to_type_->hash_for_method(gogo) << 4;
3053 // The tree for a pointer type.
3055 tree
3056 Pointer_type::do_get_tree(Gogo* gogo)
3058 return build_pointer_type(this->to_type_->get_tree(gogo));
3061 // Initialize a pointer type.
3063 tree
3064 Pointer_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
3066 if (is_clear)
3067 return NULL;
3068 return fold_convert(type_tree, null_pointer_node);
3071 // The type of a pointer type descriptor.
3073 Type*
3074 Pointer_type::make_pointer_type_descriptor_type()
3076 static Type* ret;
3077 if (ret == NULL)
3079 Type* tdt = Type::make_type_descriptor_type();
3080 Type* ptdt = Type::make_type_descriptor_ptr_type();
3082 Struct_type* s = Type::make_builtin_struct_type(2,
3083 "", tdt,
3084 "elem", ptdt);
3086 ret = Type::make_builtin_named_type("PtrType", s);
3089 return ret;
3092 // The type descriptor for a pointer type.
3094 Expression*
3095 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3097 if (this->is_unsafe_pointer_type())
3099 gcc_assert(name != NULL);
3100 return this->plain_type_descriptor(gogo,
3101 RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3102 name);
3104 else
3106 source_location bloc = BUILTINS_LOCATION;
3108 const Methods* methods;
3109 Type* deref = this->points_to();
3110 if (deref->named_type() != NULL)
3111 methods = deref->named_type()->methods();
3112 else if (deref->struct_type() != NULL)
3113 methods = deref->struct_type()->methods();
3114 else
3115 methods = NULL;
3117 Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3119 const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3121 Expression_list* vals = new Expression_list();
3122 vals->reserve(2);
3124 Struct_field_list::const_iterator p = fields->begin();
3125 gcc_assert(p->field_name() == "commonType");
3126 vals->push_back(this->type_descriptor_constructor(gogo,
3127 RUNTIME_TYPE_KIND_PTR,
3128 name, methods, false));
3130 ++p;
3131 gcc_assert(p->field_name() == "elem");
3132 vals->push_back(Expression::make_type_descriptor(deref, bloc));
3134 return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3138 // Reflection string.
3140 void
3141 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3143 ret->push_back('*');
3144 this->append_reflection(this->to_type_, gogo, ret);
3147 // Mangled name.
3149 void
3150 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3152 ret->push_back('p');
3153 this->append_mangled_name(this->to_type_, gogo, ret);
3156 // Export.
3158 void
3159 Pointer_type::do_export(Export* exp) const
3161 exp->write_c_string("*");
3162 if (this->is_unsafe_pointer_type())
3163 exp->write_c_string("any");
3164 else
3165 exp->write_type(this->to_type_);
3168 // Import.
3170 Pointer_type*
3171 Pointer_type::do_import(Import* imp)
3173 imp->require_c_string("*");
3174 if (imp->match_c_string("any"))
3176 imp->advance(3);
3177 return Type::make_pointer_type(Type::make_void_type());
3179 Type* to = imp->read_type();
3180 return Type::make_pointer_type(to);
3183 // Make a pointer type.
3185 Pointer_type*
3186 Type::make_pointer_type(Type* to_type)
3188 typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3189 static Hashtable pointer_types;
3190 Hashtable::const_iterator p = pointer_types.find(to_type);
3191 if (p != pointer_types.end())
3192 return p->second;
3193 Pointer_type* ret = new Pointer_type(to_type);
3194 pointer_types[to_type] = ret;
3195 return ret;
3198 // The nil type. We use a special type for nil because it is not the
3199 // same as any other type. In C term nil has type void*, but there is
3200 // no such type in Go.
3202 class Nil_type : public Type
3204 public:
3205 Nil_type()
3206 : Type(TYPE_NIL)
3209 protected:
3210 tree
3211 do_get_tree(Gogo*)
3212 { return ptr_type_node; }
3214 tree
3215 do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
3216 { return is_clear ? NULL : fold_convert(type_tree, null_pointer_node); }
3218 Expression*
3219 do_type_descriptor(Gogo*, Named_type*)
3220 { gcc_unreachable(); }
3222 void
3223 do_reflection(Gogo*, std::string*) const
3224 { gcc_unreachable(); }
3226 void
3227 do_mangled_name(Gogo*, std::string* ret) const
3228 { ret->push_back('n'); }
3231 // Make the nil type.
3233 Type*
3234 Type::make_nil_type()
3236 static Nil_type singleton_nil_type;
3237 return &singleton_nil_type;
3240 // The type of a function call which returns multiple values. This is
3241 // really a struct, but we don't want to confuse a function call which
3242 // returns a struct with a function call which returns multiple
3243 // values.
3245 class Call_multiple_result_type : public Type
3247 public:
3248 Call_multiple_result_type(Call_expression* call)
3249 : Type(TYPE_CALL_MULTIPLE_RESULT),
3250 call_(call)
3253 protected:
3254 bool
3255 do_has_pointer() const
3257 gcc_assert(saw_errors());
3258 return false;
3261 tree
3262 do_get_tree(Gogo*);
3264 tree
3265 do_get_init_tree(Gogo*, tree, bool)
3267 gcc_assert(saw_errors());
3268 return error_mark_node;
3271 Expression*
3272 do_type_descriptor(Gogo*, Named_type*)
3274 gcc_assert(saw_errors());
3275 return Expression::make_error(UNKNOWN_LOCATION);
3278 void
3279 do_reflection(Gogo*, std::string*) const
3280 { gcc_assert(saw_errors()); }
3282 void
3283 do_mangled_name(Gogo*, std::string*) const
3284 { gcc_assert(saw_errors()); }
3286 private:
3287 // The expression being called.
3288 Call_expression* call_;
3291 // Return the tree for a call result.
3293 tree
3294 Call_multiple_result_type::do_get_tree(Gogo* gogo)
3296 Function_type* fntype = this->call_->get_function_type();
3297 gcc_assert(fntype != NULL);
3298 const Typed_identifier_list* results = fntype->results();
3299 gcc_assert(results != NULL && results->size() > 1);
3301 Struct_field_list* sfl = new Struct_field_list;
3302 for (Typed_identifier_list::const_iterator p = results->begin();
3303 p != results->end();
3304 ++p)
3306 const std::string name = ((p->name().empty()
3307 || p->name() == Import::import_marker)
3308 ? "UNNAMED"
3309 : p->name());
3310 sfl->push_back(Struct_field(Typed_identifier(name, p->type(),
3311 this->call_->location())));
3313 return Type::make_struct_type(sfl, this->call_->location())->get_tree(gogo);
3316 // Make a call result type.
3318 Type*
3319 Type::make_call_multiple_result_type(Call_expression* call)
3321 return new Call_multiple_result_type(call);
3324 // Class Struct_field.
3326 // Get the name of a field.
3328 const std::string&
3329 Struct_field::field_name() const
3331 const std::string& name(this->typed_identifier_.name());
3332 if (!name.empty())
3333 return name;
3334 else
3336 // This is called during parsing, before anything is lowered, so
3337 // we have to be pretty careful to avoid dereferencing an
3338 // unknown type name.
3339 Type* t = this->typed_identifier_.type();
3340 Type* dt = t;
3341 if (t->classification() == Type::TYPE_POINTER)
3343 // Very ugly.
3344 Pointer_type* ptype = static_cast<Pointer_type*>(t);
3345 dt = ptype->points_to();
3347 if (dt->forward_declaration_type() != NULL)
3348 return dt->forward_declaration_type()->name();
3349 else if (dt->named_type() != NULL)
3350 return dt->named_type()->name();
3351 else if (t->is_error_type() || dt->is_error_type())
3353 static const std::string error_string = "*error*";
3354 return error_string;
3356 else
3358 // Avoid crashing in the erroneous case where T is named but
3359 // DT is not.
3360 gcc_assert(t != dt);
3361 if (t->forward_declaration_type() != NULL)
3362 return t->forward_declaration_type()->name();
3363 else if (t->named_type() != NULL)
3364 return t->named_type()->name();
3365 else
3366 gcc_unreachable();
3371 // Class Struct_type.
3373 // Traversal.
3376 Struct_type::do_traverse(Traverse* traverse)
3378 Struct_field_list* fields = this->fields_;
3379 if (fields != NULL)
3381 for (Struct_field_list::iterator p = fields->begin();
3382 p != fields->end();
3383 ++p)
3385 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
3386 return TRAVERSE_EXIT;
3389 return TRAVERSE_CONTINUE;
3392 // Verify that the struct type is complete and valid.
3394 bool
3395 Struct_type::do_verify()
3397 Struct_field_list* fields = this->fields_;
3398 if (fields == NULL)
3399 return true;
3400 bool ret = true;
3401 for (Struct_field_list::iterator p = fields->begin();
3402 p != fields->end();
3403 ++p)
3405 Type* t = p->type();
3406 if (t->is_undefined())
3408 error_at(p->location(), "struct field type is incomplete");
3409 p->set_type(Type::make_error_type());
3410 ret = false;
3412 else if (p->is_anonymous())
3414 if (t->named_type() != NULL && t->points_to() != NULL)
3416 error_at(p->location(), "embedded type may not be a pointer");
3417 p->set_type(Type::make_error_type());
3418 return false;
3422 return ret;
3425 // Whether this contains a pointer.
3427 bool
3428 Struct_type::do_has_pointer() const
3430 const Struct_field_list* fields = this->fields();
3431 if (fields == NULL)
3432 return false;
3433 for (Struct_field_list::const_iterator p = fields->begin();
3434 p != fields->end();
3435 ++p)
3437 if (p->type()->has_pointer())
3438 return true;
3440 return false;
3443 // Whether this type is identical to T.
3445 bool
3446 Struct_type::is_identical(const Struct_type* t,
3447 bool errors_are_identical) const
3449 const Struct_field_list* fields1 = this->fields();
3450 const Struct_field_list* fields2 = t->fields();
3451 if (fields1 == NULL || fields2 == NULL)
3452 return fields1 == fields2;
3453 Struct_field_list::const_iterator pf2 = fields2->begin();
3454 for (Struct_field_list::const_iterator pf1 = fields1->begin();
3455 pf1 != fields1->end();
3456 ++pf1, ++pf2)
3458 if (pf2 == fields2->end())
3459 return false;
3460 if (pf1->field_name() != pf2->field_name())
3461 return false;
3462 if (pf1->is_anonymous() != pf2->is_anonymous()
3463 || !Type::are_identical(pf1->type(), pf2->type(),
3464 errors_are_identical, NULL))
3465 return false;
3466 if (!pf1->has_tag())
3468 if (pf2->has_tag())
3469 return false;
3471 else
3473 if (!pf2->has_tag())
3474 return false;
3475 if (pf1->tag() != pf2->tag())
3476 return false;
3479 if (pf2 != fields2->end())
3480 return false;
3481 return true;
3484 // Whether this struct type has any hidden fields.
3486 bool
3487 Struct_type::struct_has_hidden_fields(const Named_type* within,
3488 std::string* reason) const
3490 const Struct_field_list* fields = this->fields();
3491 if (fields == NULL)
3492 return false;
3493 const Package* within_package = (within == NULL
3494 ? NULL
3495 : within->named_object()->package());
3496 for (Struct_field_list::const_iterator pf = fields->begin();
3497 pf != fields->end();
3498 ++pf)
3500 if (within_package != NULL
3501 && !pf->is_anonymous()
3502 && Gogo::is_hidden_name(pf->field_name()))
3504 if (reason != NULL)
3506 std::string within_name = within->named_object()->message_name();
3507 std::string name = Gogo::message_name(pf->field_name());
3508 size_t bufsize = 200 + within_name.length() + name.length();
3509 char* buf = new char[bufsize];
3510 snprintf(buf, bufsize,
3511 _("implicit assignment of %s%s%s hidden field %s%s%s"),
3512 open_quote, within_name.c_str(), close_quote,
3513 open_quote, name.c_str(), close_quote);
3514 reason->assign(buf);
3515 delete[] buf;
3517 return true;
3520 if (pf->type()->has_hidden_fields(within, reason))
3521 return true;
3524 return false;
3527 // Hash code.
3529 unsigned int
3530 Struct_type::do_hash_for_method(Gogo* gogo) const
3532 unsigned int ret = 0;
3533 if (this->fields() != NULL)
3535 for (Struct_field_list::const_iterator pf = this->fields()->begin();
3536 pf != this->fields()->end();
3537 ++pf)
3538 ret = (ret << 1) + pf->type()->hash_for_method(gogo);
3540 return ret <<= 2;
3543 // Find the local field NAME.
3545 const Struct_field*
3546 Struct_type::find_local_field(const std::string& name,
3547 unsigned int *pindex) const
3549 const Struct_field_list* fields = this->fields_;
3550 if (fields == NULL)
3551 return NULL;
3552 unsigned int i = 0;
3553 for (Struct_field_list::const_iterator pf = fields->begin();
3554 pf != fields->end();
3555 ++pf, ++i)
3557 if (pf->field_name() == name)
3559 if (pindex != NULL)
3560 *pindex = i;
3561 return &*pf;
3564 return NULL;
3567 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
3569 Field_reference_expression*
3570 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
3571 source_location location) const
3573 unsigned int depth;
3574 return this->field_reference_depth(struct_expr, name, location, &depth);
3577 // Return an expression for a field, along with the depth at which it
3578 // was found.
3580 Field_reference_expression*
3581 Struct_type::field_reference_depth(Expression* struct_expr,
3582 const std::string& name,
3583 source_location location,
3584 unsigned int* depth) const
3586 const Struct_field_list* fields = this->fields_;
3587 if (fields == NULL)
3588 return NULL;
3590 // Look for a field with this name.
3591 unsigned int i = 0;
3592 for (Struct_field_list::const_iterator pf = fields->begin();
3593 pf != fields->end();
3594 ++pf, ++i)
3596 if (pf->field_name() == name)
3598 *depth = 0;
3599 return Expression::make_field_reference(struct_expr, i, location);
3603 // Look for an anonymous field which contains a field with this
3604 // name.
3605 unsigned int found_depth = 0;
3606 Field_reference_expression* ret = NULL;
3607 i = 0;
3608 for (Struct_field_list::const_iterator pf = fields->begin();
3609 pf != fields->end();
3610 ++pf, ++i)
3612 if (!pf->is_anonymous())
3613 continue;
3615 Struct_type* st = pf->type()->deref()->struct_type();
3616 if (st == NULL)
3617 continue;
3619 // Look for a reference using a NULL struct expression. If we
3620 // find one, fill in the struct expression with a reference to
3621 // this field.
3622 unsigned int subdepth;
3623 Field_reference_expression* sub = st->field_reference_depth(NULL, name,
3624 location,
3625 &subdepth);
3626 if (sub == NULL)
3627 continue;
3629 if (ret == NULL || subdepth < found_depth)
3631 if (ret != NULL)
3632 delete ret;
3633 ret = sub;
3634 found_depth = subdepth;
3635 Expression* here = Expression::make_field_reference(struct_expr, i,
3636 location);
3637 if (pf->type()->points_to() != NULL)
3638 here = Expression::make_unary(OPERATOR_MULT, here, location);
3639 while (sub->expr() != NULL)
3641 sub = sub->expr()->deref()->field_reference_expression();
3642 gcc_assert(sub != NULL);
3644 sub->set_struct_expression(here);
3646 else if (subdepth > found_depth)
3647 delete sub;
3648 else
3650 // We do not handle ambiguity here--it should be handled by
3651 // Type::bind_field_or_method.
3652 delete sub;
3653 found_depth = 0;
3654 ret = NULL;
3658 if (ret != NULL)
3659 *depth = found_depth + 1;
3661 return ret;
3664 // Return the total number of fields, including embedded fields.
3666 unsigned int
3667 Struct_type::total_field_count() const
3669 if (this->fields_ == NULL)
3670 return 0;
3671 unsigned int ret = 0;
3672 for (Struct_field_list::const_iterator pf = this->fields_->begin();
3673 pf != this->fields_->end();
3674 ++pf)
3676 if (!pf->is_anonymous() || pf->type()->deref()->struct_type() == NULL)
3677 ++ret;
3678 else
3679 ret += pf->type()->struct_type()->total_field_count();
3681 return ret;
3684 // Return whether NAME is an unexported field, for better error reporting.
3686 bool
3687 Struct_type::is_unexported_local_field(Gogo* gogo,
3688 const std::string& name) const
3690 const Struct_field_list* fields = this->fields_;
3691 if (fields != NULL)
3693 for (Struct_field_list::const_iterator pf = fields->begin();
3694 pf != fields->end();
3695 ++pf)
3697 const std::string& field_name(pf->field_name());
3698 if (Gogo::is_hidden_name(field_name)
3699 && name == Gogo::unpack_hidden_name(field_name)
3700 && gogo->pack_hidden_name(name, false) != field_name)
3701 return true;
3704 return false;
3707 // Finalize the methods of an unnamed struct.
3709 void
3710 Struct_type::finalize_methods(Gogo* gogo)
3712 if (this->all_methods_ != NULL)
3713 return;
3714 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
3717 // Return the method NAME, or NULL if there isn't one or if it is
3718 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
3719 // ambiguous.
3721 Method*
3722 Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
3724 return Type::method_function(this->all_methods_, name, is_ambiguous);
3727 // Get the tree for a struct type.
3729 tree
3730 Struct_type::do_get_tree(Gogo* gogo)
3732 tree type = make_node(RECORD_TYPE);
3733 return this->fill_in_tree(gogo, type);
3736 // Fill in the fields for a struct type.
3738 tree
3739 Struct_type::fill_in_tree(Gogo* gogo, tree type)
3741 tree field_trees = NULL_TREE;
3742 tree* pp = &field_trees;
3743 for (Struct_field_list::const_iterator p = this->fields_->begin();
3744 p != this->fields_->end();
3745 ++p)
3747 std::string name = Gogo::unpack_hidden_name(p->field_name());
3748 tree name_tree = get_identifier_with_length(name.data(), name.length());
3749 tree field_type_tree = p->type()->get_tree(gogo);
3750 if (field_type_tree == error_mark_node)
3751 return error_mark_node;
3752 tree field = build_decl(p->location(), FIELD_DECL, name_tree,
3753 field_type_tree);
3754 DECL_CONTEXT(field) = type;
3755 *pp = field;
3756 pp = &DECL_CHAIN(field);
3759 TYPE_FIELDS(type) = field_trees;
3761 layout_type(type);
3763 return type;
3766 // Initialize struct fields.
3768 tree
3769 Struct_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
3771 if (this->fields_ == NULL || this->fields_->empty())
3773 if (is_clear)
3774 return NULL;
3775 else
3777 tree ret = build_constructor(type_tree,
3778 VEC_alloc(constructor_elt, gc, 0));
3779 TREE_CONSTANT(ret) = 1;
3780 return ret;
3784 bool is_constant = true;
3785 bool any_fields_set = false;
3786 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc,
3787 this->fields_->size());
3789 tree field = TYPE_FIELDS(type_tree);
3790 for (Struct_field_list::const_iterator p = this->fields_->begin();
3791 p != this->fields_->end();
3792 ++p, field = DECL_CHAIN(field))
3794 tree value = p->type()->get_init_tree(gogo, is_clear);
3795 if (value == error_mark_node)
3796 return error_mark_node;
3797 gcc_assert(field != NULL_TREE);
3798 if (value != NULL)
3800 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
3801 elt->index = field;
3802 elt->value = value;
3803 any_fields_set = true;
3804 if (!TREE_CONSTANT(value))
3805 is_constant = false;
3808 gcc_assert(field == NULL_TREE);
3810 if (!any_fields_set)
3812 gcc_assert(is_clear);
3813 VEC_free(constructor_elt, gc, init);
3814 return NULL;
3817 tree ret = build_constructor(type_tree, init);
3818 if (is_constant)
3819 TREE_CONSTANT(ret) = 1;
3820 return ret;
3823 // The type of a struct type descriptor.
3825 Type*
3826 Struct_type::make_struct_type_descriptor_type()
3828 static Type* ret;
3829 if (ret == NULL)
3831 Type* tdt = Type::make_type_descriptor_type();
3832 Type* ptdt = Type::make_type_descriptor_ptr_type();
3834 Type* uintptr_type = Type::lookup_integer_type("uintptr");
3835 Type* string_type = Type::lookup_string_type();
3836 Type* pointer_string_type = Type::make_pointer_type(string_type);
3838 Struct_type* sf =
3839 Type::make_builtin_struct_type(5,
3840 "name", pointer_string_type,
3841 "pkgPath", pointer_string_type,
3842 "typ", ptdt,
3843 "tag", pointer_string_type,
3844 "offset", uintptr_type);
3845 Type* nsf = Type::make_builtin_named_type("structField", sf);
3847 Type* slice_type = Type::make_array_type(nsf, NULL);
3849 Struct_type* s = Type::make_builtin_struct_type(2,
3850 "", tdt,
3851 "fields", slice_type);
3853 ret = Type::make_builtin_named_type("StructType", s);
3856 return ret;
3859 // Build a type descriptor for a struct type.
3861 Expression*
3862 Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3864 source_location bloc = BUILTINS_LOCATION;
3866 Type* stdt = Struct_type::make_struct_type_descriptor_type();
3868 const Struct_field_list* fields = stdt->struct_type()->fields();
3870 Expression_list* vals = new Expression_list();
3871 vals->reserve(2);
3873 const Methods* methods = this->methods();
3874 // A named struct should not have methods--the methods should attach
3875 // to the named type.
3876 gcc_assert(methods == NULL || name == NULL);
3878 Struct_field_list::const_iterator ps = fields->begin();
3879 gcc_assert(ps->field_name() == "commonType");
3880 vals->push_back(this->type_descriptor_constructor(gogo,
3881 RUNTIME_TYPE_KIND_STRUCT,
3882 name, methods, true));
3884 ++ps;
3885 gcc_assert(ps->field_name() == "fields");
3887 Expression_list* elements = new Expression_list();
3888 elements->reserve(this->fields_->size());
3889 Type* element_type = ps->type()->array_type()->element_type();
3890 for (Struct_field_list::const_iterator pf = this->fields_->begin();
3891 pf != this->fields_->end();
3892 ++pf)
3894 const Struct_field_list* f = element_type->struct_type()->fields();
3896 Expression_list* fvals = new Expression_list();
3897 fvals->reserve(5);
3899 Struct_field_list::const_iterator q = f->begin();
3900 gcc_assert(q->field_name() == "name");
3901 if (pf->is_anonymous())
3902 fvals->push_back(Expression::make_nil(bloc));
3903 else
3905 std::string n = Gogo::unpack_hidden_name(pf->field_name());
3906 Expression* s = Expression::make_string(n, bloc);
3907 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3910 ++q;
3911 gcc_assert(q->field_name() == "pkgPath");
3912 if (!Gogo::is_hidden_name(pf->field_name()))
3913 fvals->push_back(Expression::make_nil(bloc));
3914 else
3916 std::string n = Gogo::hidden_name_prefix(pf->field_name());
3917 Expression* s = Expression::make_string(n, bloc);
3918 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3921 ++q;
3922 gcc_assert(q->field_name() == "typ");
3923 fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
3925 ++q;
3926 gcc_assert(q->field_name() == "tag");
3927 if (!pf->has_tag())
3928 fvals->push_back(Expression::make_nil(bloc));
3929 else
3931 Expression* s = Expression::make_string(pf->tag(), bloc);
3932 fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3935 ++q;
3936 gcc_assert(q->field_name() == "offset");
3937 fvals->push_back(Expression::make_struct_field_offset(this, &*pf));
3939 Expression* v = Expression::make_struct_composite_literal(element_type,
3940 fvals, bloc);
3941 elements->push_back(v);
3944 vals->push_back(Expression::make_slice_composite_literal(ps->type(),
3945 elements, bloc));
3947 return Expression::make_struct_composite_literal(stdt, vals, bloc);
3950 // Reflection string.
3952 void
3953 Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
3955 ret->append("struct { ");
3957 for (Struct_field_list::const_iterator p = this->fields_->begin();
3958 p != this->fields_->end();
3959 ++p)
3961 if (p != this->fields_->begin())
3962 ret->append("; ");
3963 if (p->is_anonymous())
3964 ret->push_back('?');
3965 else
3966 ret->append(Gogo::unpack_hidden_name(p->field_name()));
3967 ret->push_back(' ');
3968 this->append_reflection(p->type(), gogo, ret);
3970 if (p->has_tag())
3972 const std::string& tag(p->tag());
3973 ret->append(" \"");
3974 for (std::string::const_iterator p = tag.begin();
3975 p != tag.end();
3976 ++p)
3978 if (*p == '\0')
3979 ret->append("\\x00");
3980 else if (*p == '\n')
3981 ret->append("\\n");
3982 else if (*p == '\t')
3983 ret->append("\\t");
3984 else if (*p == '"')
3985 ret->append("\\\"");
3986 else if (*p == '\\')
3987 ret->append("\\\\");
3988 else
3989 ret->push_back(*p);
3991 ret->push_back('"');
3995 ret->append(" }");
3998 // Mangled name.
4000 void
4001 Struct_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4003 ret->push_back('S');
4005 const Struct_field_list* fields = this->fields_;
4006 if (fields != NULL)
4008 for (Struct_field_list::const_iterator p = fields->begin();
4009 p != fields->end();
4010 ++p)
4012 if (p->is_anonymous())
4013 ret->append("0_");
4014 else
4016 std::string n = Gogo::unpack_hidden_name(p->field_name());
4017 char buf[20];
4018 snprintf(buf, sizeof buf, "%u_",
4019 static_cast<unsigned int>(n.length()));
4020 ret->append(buf);
4021 ret->append(n);
4023 this->append_mangled_name(p->type(), gogo, ret);
4024 if (p->has_tag())
4026 const std::string& tag(p->tag());
4027 std::string out;
4028 for (std::string::const_iterator p = tag.begin();
4029 p != tag.end();
4030 ++p)
4032 if (ISALNUM(*p) || *p == '_')
4033 out.push_back(*p);
4034 else
4036 char buf[20];
4037 snprintf(buf, sizeof buf, ".%x.",
4038 static_cast<unsigned int>(*p));
4039 out.append(buf);
4042 char buf[20];
4043 snprintf(buf, sizeof buf, "T%u_",
4044 static_cast<unsigned int>(out.length()));
4045 ret->append(buf);
4046 ret->append(out);
4051 ret->push_back('e');
4054 // Export.
4056 void
4057 Struct_type::do_export(Export* exp) const
4059 exp->write_c_string("struct { ");
4060 const Struct_field_list* fields = this->fields_;
4061 gcc_assert(fields != NULL);
4062 for (Struct_field_list::const_iterator p = fields->begin();
4063 p != fields->end();
4064 ++p)
4066 if (p->is_anonymous())
4067 exp->write_string("? ");
4068 else
4070 exp->write_string(p->field_name());
4071 exp->write_c_string(" ");
4073 exp->write_type(p->type());
4075 if (p->has_tag())
4077 exp->write_c_string(" ");
4078 Expression* expr = Expression::make_string(p->tag(),
4079 BUILTINS_LOCATION);
4080 expr->export_expression(exp);
4081 delete expr;
4084 exp->write_c_string("; ");
4086 exp->write_c_string("}");
4089 // Import.
4091 Struct_type*
4092 Struct_type::do_import(Import* imp)
4094 imp->require_c_string("struct { ");
4095 Struct_field_list* fields = new Struct_field_list;
4096 if (imp->peek_char() != '}')
4098 while (true)
4100 std::string name;
4101 if (imp->match_c_string("? "))
4102 imp->advance(2);
4103 else
4105 name = imp->read_identifier();
4106 imp->require_c_string(" ");
4108 Type* ftype = imp->read_type();
4110 Struct_field sf(Typed_identifier(name, ftype, imp->location()));
4112 if (imp->peek_char() == ' ')
4114 imp->advance(1);
4115 Expression* expr = Expression::import_expression(imp);
4116 String_expression* sexpr = expr->string_expression();
4117 gcc_assert(sexpr != NULL);
4118 sf.set_tag(sexpr->val());
4119 delete sexpr;
4122 imp->require_c_string("; ");
4123 fields->push_back(sf);
4124 if (imp->peek_char() == '}')
4125 break;
4128 imp->require_c_string("}");
4130 return Type::make_struct_type(fields, imp->location());
4133 // Make a struct type.
4135 Struct_type*
4136 Type::make_struct_type(Struct_field_list* fields,
4137 source_location location)
4139 return new Struct_type(fields, location);
4142 // Class Array_type.
4144 // Whether two array types are identical.
4146 bool
4147 Array_type::is_identical(const Array_type* t, bool errors_are_identical) const
4149 if (!Type::are_identical(this->element_type(), t->element_type(),
4150 errors_are_identical, NULL))
4151 return false;
4153 Expression* l1 = this->length();
4154 Expression* l2 = t->length();
4156 // Slices of the same element type are identical.
4157 if (l1 == NULL && l2 == NULL)
4158 return true;
4160 // Arrays of the same element type are identical if they have the
4161 // same length.
4162 if (l1 != NULL && l2 != NULL)
4164 if (l1 == l2)
4165 return true;
4167 // Try to determine the lengths. If we can't, assume the arrays
4168 // are not identical.
4169 bool ret = false;
4170 mpz_t v1;
4171 mpz_init(v1);
4172 Type* type1;
4173 mpz_t v2;
4174 mpz_init(v2);
4175 Type* type2;
4176 if (l1->integer_constant_value(true, v1, &type1)
4177 && l2->integer_constant_value(true, v2, &type2))
4178 ret = mpz_cmp(v1, v2) == 0;
4179 mpz_clear(v1);
4180 mpz_clear(v2);
4181 return ret;
4184 // Otherwise the arrays are not identical.
4185 return false;
4188 // Traversal.
4191 Array_type::do_traverse(Traverse* traverse)
4193 if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
4194 return TRAVERSE_EXIT;
4195 if (this->length_ != NULL
4196 && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
4197 return TRAVERSE_EXIT;
4198 return TRAVERSE_CONTINUE;
4201 // Check that the length is valid.
4203 bool
4204 Array_type::verify_length()
4206 if (this->length_ == NULL)
4207 return true;
4208 if (!this->length_->is_constant())
4210 error_at(this->length_->location(), "array bound is not constant");
4211 return false;
4214 mpz_t val;
4216 Type* t = this->length_->type();
4217 if (t->integer_type() != NULL)
4219 Type* vt;
4220 mpz_init(val);
4221 if (!this->length_->integer_constant_value(true, val, &vt))
4223 error_at(this->length_->location(),
4224 "array bound is not constant");
4225 mpz_clear(val);
4226 return false;
4229 else if (t->float_type() != NULL)
4231 Type* vt;
4232 mpfr_t fval;
4233 mpfr_init(fval);
4234 if (!this->length_->float_constant_value(fval, &vt))
4236 error_at(this->length_->location(),
4237 "array bound is not constant");
4238 mpfr_clear(fval);
4239 return false;
4241 if (!mpfr_integer_p(fval))
4243 error_at(this->length_->location(),
4244 "array bound truncated to integer");
4245 mpfr_clear(fval);
4246 return false;
4248 mpz_init(val);
4249 mpfr_get_z(val, fval, GMP_RNDN);
4250 mpfr_clear(fval);
4252 else
4254 if (!t->is_error_type())
4255 error_at(this->length_->location(), "array bound is not numeric");
4256 return false;
4259 if (mpz_sgn(val) < 0)
4261 error_at(this->length_->location(), "negative array bound");
4262 mpz_clear(val);
4263 return false;
4266 Type* int_type = Type::lookup_integer_type("int");
4267 int tbits = int_type->integer_type()->bits();
4268 int vbits = mpz_sizeinbase(val, 2);
4269 if (vbits + 1 > tbits)
4271 error_at(this->length_->location(), "array bound overflows");
4272 mpz_clear(val);
4273 return false;
4276 mpz_clear(val);
4278 return true;
4281 // Verify the type.
4283 bool
4284 Array_type::do_verify()
4286 if (!this->verify_length())
4288 this->length_ = Expression::make_error(this->length_->location());
4289 return false;
4291 return true;
4294 // Array type hash code.
4296 unsigned int
4297 Array_type::do_hash_for_method(Gogo* gogo) const
4299 // There is no very convenient way to get a hash code for the
4300 // length.
4301 return this->element_type_->hash_for_method(gogo) + 1;
4304 // See if the expression passed to make is suitable. The first
4305 // argument is required, and gives the length. An optional second
4306 // argument is permitted for the capacity.
4308 bool
4309 Array_type::do_check_make_expression(Expression_list* args,
4310 source_location location)
4312 gcc_assert(this->length_ == NULL);
4313 if (args == NULL || args->empty())
4315 error_at(location, "length required when allocating a slice");
4316 return false;
4318 else if (args->size() > 2)
4320 error_at(location, "too many expressions passed to make");
4321 return false;
4323 else
4325 if (!Type::check_int_value(args->front(),
4326 _("bad length when making slice"), location))
4327 return false;
4329 if (args->size() > 1)
4331 if (!Type::check_int_value(args->back(),
4332 _("bad capacity when making slice"),
4333 location))
4334 return false;
4337 return true;
4341 // Get a tree for the length of a fixed array. The length may be
4342 // computed using a function call, so we must only evaluate it once.
4344 tree
4345 Array_type::get_length_tree(Gogo* gogo)
4347 gcc_assert(this->length_ != NULL);
4348 if (this->length_tree_ == NULL_TREE)
4350 mpz_t val;
4351 mpz_init(val);
4352 Type* t;
4353 if (this->length_->integer_constant_value(true, val, &t))
4355 if (t == NULL)
4356 t = Type::lookup_integer_type("int");
4357 else if (t->is_abstract())
4358 t = t->make_non_abstract_type();
4359 tree tt = t->get_tree(gogo);
4360 this->length_tree_ = Expression::integer_constant_tree(val, tt);
4361 mpz_clear(val);
4363 else
4365 mpz_clear(val);
4367 // Make up a translation context for the array length
4368 // expression. FIXME: This won't work in general.
4369 Translate_context context(gogo, NULL, NULL, NULL_TREE);
4370 tree len = this->length_->get_tree(&context);
4371 if (len != error_mark_node)
4373 len = convert_to_integer(integer_type_node, len);
4374 len = save_expr(len);
4376 this->length_tree_ = len;
4379 return this->length_tree_;
4382 // Get a tree for the type of this array. A fixed array is simply
4383 // represented as ARRAY_TYPE with the appropriate index--i.e., it is
4384 // just like an array in C. An open array is a struct with three
4385 // fields: a data pointer, the length, and the capacity.
4387 tree
4388 Array_type::do_get_tree(Gogo* gogo)
4390 if (this->length_ == NULL)
4392 tree struct_type = gogo->slice_type_tree(void_type_node);
4393 return this->fill_in_tree(gogo, struct_type);
4395 else
4397 tree element_type_tree = this->element_type_->get_tree(gogo);
4398 tree length_tree = this->get_length_tree(gogo);
4399 if (element_type_tree == error_mark_node
4400 || length_tree == error_mark_node)
4401 return error_mark_node;
4403 length_tree = fold_convert(sizetype, length_tree);
4405 // build_index_type takes the maximum index, which is one less
4406 // than the length.
4407 tree index_type = build_index_type(fold_build2(MINUS_EXPR, sizetype,
4408 length_tree,
4409 size_one_node));
4411 return build_array_type(element_type_tree, index_type);
4415 // Fill in the fields for a slice type. This is used for named slice
4416 // types.
4418 tree
4419 Array_type::fill_in_tree(Gogo* gogo, tree struct_type)
4421 gcc_assert(this->length_ == NULL);
4423 tree element_type_tree = this->element_type_->get_tree(gogo);
4424 tree field = TYPE_FIELDS(struct_type);
4425 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
4426 gcc_assert(POINTER_TYPE_P(TREE_TYPE(field))
4427 && TREE_TYPE(TREE_TYPE(field)) == void_type_node);
4428 TREE_TYPE(field) = build_pointer_type(element_type_tree);
4430 return struct_type;
4433 // Return an initializer for an array type.
4435 tree
4436 Array_type::do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
4438 if (this->length_ == NULL)
4440 // Open array.
4442 if (is_clear)
4443 return NULL;
4445 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
4447 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
4449 for (tree field = TYPE_FIELDS(type_tree);
4450 field != NULL_TREE;
4451 field = DECL_CHAIN(field))
4453 constructor_elt* elt = VEC_quick_push(constructor_elt, init,
4454 NULL);
4455 elt->index = field;
4456 elt->value = fold_convert(TREE_TYPE(field), size_zero_node);
4459 tree ret = build_constructor(type_tree, init);
4460 TREE_CONSTANT(ret) = 1;
4461 return ret;
4463 else
4465 // Fixed array.
4467 tree value = this->element_type_->get_init_tree(gogo, is_clear);
4468 if (value == NULL)
4469 return NULL;
4470 if (value == error_mark_node)
4471 return error_mark_node;
4473 tree length_tree = this->get_length_tree(gogo);
4474 if (length_tree == error_mark_node)
4475 return error_mark_node;
4477 length_tree = fold_convert(sizetype, length_tree);
4478 tree range = build2(RANGE_EXPR, sizetype, size_zero_node,
4479 fold_build2(MINUS_EXPR, sizetype,
4480 length_tree, size_one_node));
4481 tree ret = build_constructor_single(type_tree, range, value);
4482 if (TREE_CONSTANT(value))
4483 TREE_CONSTANT(ret) = 1;
4484 return ret;
4488 // Handle the builtin make function for a slice.
4490 tree
4491 Array_type::do_make_expression_tree(Translate_context* context,
4492 Expression_list* args,
4493 source_location location)
4495 gcc_assert(this->length_ == NULL);
4497 Gogo* gogo = context->gogo();
4498 tree type_tree = this->get_tree(gogo);
4499 if (type_tree == error_mark_node)
4500 return error_mark_node;
4502 tree values_field = TYPE_FIELDS(type_tree);
4503 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(values_field)),
4504 "__values") == 0);
4506 tree count_field = DECL_CHAIN(values_field);
4507 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(count_field)),
4508 "__count") == 0);
4510 tree element_type_tree = this->element_type_->get_tree(gogo);
4511 if (element_type_tree == error_mark_node)
4512 return error_mark_node;
4513 tree element_size_tree = TYPE_SIZE_UNIT(element_type_tree);
4515 tree value = this->element_type_->get_init_tree(gogo, true);
4517 // The first argument is the number of elements, the optional second
4518 // argument is the capacity.
4519 gcc_assert(args != NULL && args->size() >= 1 && args->size() <= 2);
4521 tree length_tree = args->front()->get_tree(context);
4522 if (length_tree == error_mark_node)
4523 return error_mark_node;
4524 if (!DECL_P(length_tree))
4525 length_tree = save_expr(length_tree);
4526 if (!INTEGRAL_TYPE_P(TREE_TYPE(length_tree)))
4527 length_tree = convert_to_integer(TREE_TYPE(count_field), length_tree);
4529 tree bad_index = Expression::check_bounds(length_tree,
4530 TREE_TYPE(count_field),
4531 NULL_TREE, location);
4533 length_tree = fold_convert_loc(location, TREE_TYPE(count_field), length_tree);
4534 tree capacity_tree;
4535 if (args->size() == 1)
4536 capacity_tree = length_tree;
4537 else
4539 capacity_tree = args->back()->get_tree(context);
4540 if (capacity_tree == error_mark_node)
4541 return error_mark_node;
4542 if (!DECL_P(capacity_tree))
4543 capacity_tree = save_expr(capacity_tree);
4544 if (!INTEGRAL_TYPE_P(TREE_TYPE(capacity_tree)))
4545 capacity_tree = convert_to_integer(TREE_TYPE(count_field),
4546 capacity_tree);
4548 bad_index = Expression::check_bounds(capacity_tree,
4549 TREE_TYPE(count_field),
4550 bad_index, location);
4552 tree chktype = (((TYPE_SIZE(TREE_TYPE(capacity_tree))
4553 > TYPE_SIZE(TREE_TYPE(length_tree)))
4554 || ((TYPE_SIZE(TREE_TYPE(capacity_tree))
4555 == TYPE_SIZE(TREE_TYPE(length_tree)))
4556 && TYPE_UNSIGNED(TREE_TYPE(capacity_tree))))
4557 ? TREE_TYPE(capacity_tree)
4558 : TREE_TYPE(length_tree));
4559 tree chk = fold_build2_loc(location, LT_EXPR, boolean_type_node,
4560 fold_convert_loc(location, chktype,
4561 capacity_tree),
4562 fold_convert_loc(location, chktype,
4563 length_tree));
4564 if (bad_index == NULL_TREE)
4565 bad_index = chk;
4566 else
4567 bad_index = fold_build2_loc(location, TRUTH_OR_EXPR, boolean_type_node,
4568 bad_index, chk);
4570 capacity_tree = fold_convert_loc(location, TREE_TYPE(count_field),
4571 capacity_tree);
4574 tree size_tree = fold_build2_loc(location, MULT_EXPR, sizetype,
4575 element_size_tree,
4576 fold_convert_loc(location, sizetype,
4577 capacity_tree));
4579 tree chk = fold_build2_loc(location, TRUTH_AND_EXPR, boolean_type_node,
4580 fold_build2_loc(location, GT_EXPR,
4581 boolean_type_node,
4582 fold_convert_loc(location,
4583 sizetype,
4584 capacity_tree),
4585 size_zero_node),
4586 fold_build2_loc(location, LT_EXPR,
4587 boolean_type_node,
4588 size_tree, element_size_tree));
4589 if (bad_index == NULL_TREE)
4590 bad_index = chk;
4591 else
4592 bad_index = fold_build2_loc(location, TRUTH_OR_EXPR, boolean_type_node,
4593 bad_index, chk);
4595 tree space = context->gogo()->allocate_memory(this->element_type_,
4596 size_tree, location);
4598 if (value != NULL_TREE)
4599 space = save_expr(space);
4601 space = fold_convert(TREE_TYPE(values_field), space);
4603 if (bad_index != NULL_TREE && bad_index != boolean_false_node)
4605 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS,
4606 location);
4607 space = build2(COMPOUND_EXPR, TREE_TYPE(space),
4608 build3(COND_EXPR, void_type_node,
4609 bad_index, crash, NULL_TREE),
4610 space);
4613 tree constructor = gogo->slice_constructor(type_tree, space, length_tree,
4614 capacity_tree);
4616 if (value == NULL_TREE)
4618 // The array contents are zero initialized.
4619 return constructor;
4622 // The elements must be initialized.
4624 tree max = fold_build2_loc(location, MINUS_EXPR, TREE_TYPE(count_field),
4625 capacity_tree,
4626 fold_convert_loc(location, TREE_TYPE(count_field),
4627 integer_one_node));
4629 tree array_type = build_array_type(element_type_tree,
4630 build_index_type(max));
4632 tree value_pointer = fold_convert_loc(location,
4633 build_pointer_type(array_type),
4634 space);
4636 tree range = build2(RANGE_EXPR, sizetype, size_zero_node, max);
4637 tree space_init = build_constructor_single(array_type, range, value);
4639 return build2(COMPOUND_EXPR, TREE_TYPE(space),
4640 build2(MODIFY_EXPR, void_type_node,
4641 build_fold_indirect_ref(value_pointer),
4642 space_init),
4643 constructor);
4646 // Return a tree for a pointer to the values in ARRAY.
4648 tree
4649 Array_type::value_pointer_tree(Gogo*, tree array) const
4651 tree ret;
4652 if (this->length() != NULL)
4654 // Fixed array.
4655 ret = fold_convert(build_pointer_type(TREE_TYPE(TREE_TYPE(array))),
4656 build_fold_addr_expr(array));
4658 else
4660 // Open array.
4661 tree field = TYPE_FIELDS(TREE_TYPE(array));
4662 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
4663 "__values") == 0);
4664 ret = fold_build3(COMPONENT_REF, TREE_TYPE(field), array, field,
4665 NULL_TREE);
4667 if (TREE_CONSTANT(array))
4668 TREE_CONSTANT(ret) = 1;
4669 return ret;
4672 // Return a tree for the length of the array ARRAY which has this
4673 // type.
4675 tree
4676 Array_type::length_tree(Gogo* gogo, tree array)
4678 if (this->length_ != NULL)
4680 if (TREE_CODE(array) == SAVE_EXPR)
4681 return fold_convert(integer_type_node, this->get_length_tree(gogo));
4682 else
4683 return omit_one_operand(integer_type_node,
4684 this->get_length_tree(gogo), array);
4687 // This is an open array. We need to read the length field.
4689 tree type = TREE_TYPE(array);
4690 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
4692 tree field = DECL_CHAIN(TYPE_FIELDS(type));
4693 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
4695 tree ret = build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
4696 if (TREE_CONSTANT(array))
4697 TREE_CONSTANT(ret) = 1;
4698 return ret;
4701 // Return a tree for the capacity of the array ARRAY which has this
4702 // type.
4704 tree
4705 Array_type::capacity_tree(Gogo* gogo, tree array)
4707 if (this->length_ != NULL)
4708 return omit_one_operand(sizetype, this->get_length_tree(gogo), array);
4710 // This is an open array. We need to read the capacity field.
4712 tree type = TREE_TYPE(array);
4713 gcc_assert(TREE_CODE(type) == RECORD_TYPE);
4715 tree field = DECL_CHAIN(DECL_CHAIN(TYPE_FIELDS(type)));
4716 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
4718 return build3(COMPONENT_REF, TREE_TYPE(field), array, field, NULL_TREE);
4721 // Export.
4723 void
4724 Array_type::do_export(Export* exp) const
4726 exp->write_c_string("[");
4727 if (this->length_ != NULL)
4728 this->length_->export_expression(exp);
4729 exp->write_c_string("] ");
4730 exp->write_type(this->element_type_);
4733 // Import.
4735 Array_type*
4736 Array_type::do_import(Import* imp)
4738 imp->require_c_string("[");
4739 Expression* length;
4740 if (imp->peek_char() == ']')
4741 length = NULL;
4742 else
4743 length = Expression::import_expression(imp);
4744 imp->require_c_string("] ");
4745 Type* element_type = imp->read_type();
4746 return Type::make_array_type(element_type, length);
4749 // The type of an array type descriptor.
4751 Type*
4752 Array_type::make_array_type_descriptor_type()
4754 static Type* ret;
4755 if (ret == NULL)
4757 Type* tdt = Type::make_type_descriptor_type();
4758 Type* ptdt = Type::make_type_descriptor_ptr_type();
4760 Type* uintptr_type = Type::lookup_integer_type("uintptr");
4762 Struct_type* sf =
4763 Type::make_builtin_struct_type(3,
4764 "", tdt,
4765 "elem", ptdt,
4766 "len", uintptr_type);
4768 ret = Type::make_builtin_named_type("ArrayType", sf);
4771 return ret;
4774 // The type of an slice type descriptor.
4776 Type*
4777 Array_type::make_slice_type_descriptor_type()
4779 static Type* ret;
4780 if (ret == NULL)
4782 Type* tdt = Type::make_type_descriptor_type();
4783 Type* ptdt = Type::make_type_descriptor_ptr_type();
4785 Struct_type* sf =
4786 Type::make_builtin_struct_type(2,
4787 "", tdt,
4788 "elem", ptdt);
4790 ret = Type::make_builtin_named_type("SliceType", sf);
4793 return ret;
4796 // Build a type descriptor for an array/slice type.
4798 Expression*
4799 Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4801 if (this->length_ != NULL)
4802 return this->array_type_descriptor(gogo, name);
4803 else
4804 return this->slice_type_descriptor(gogo, name);
4807 // Build a type descriptor for an array type.
4809 Expression*
4810 Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
4812 source_location bloc = BUILTINS_LOCATION;
4814 Type* atdt = Array_type::make_array_type_descriptor_type();
4816 const Struct_field_list* fields = atdt->struct_type()->fields();
4818 Expression_list* vals = new Expression_list();
4819 vals->reserve(3);
4821 Struct_field_list::const_iterator p = fields->begin();
4822 gcc_assert(p->field_name() == "commonType");
4823 vals->push_back(this->type_descriptor_constructor(gogo,
4824 RUNTIME_TYPE_KIND_ARRAY,
4825 name, NULL, true));
4827 ++p;
4828 gcc_assert(p->field_name() == "elem");
4829 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
4831 ++p;
4832 gcc_assert(p->field_name() == "len");
4833 vals->push_back(this->length_);
4835 ++p;
4836 gcc_assert(p == fields->end());
4838 return Expression::make_struct_composite_literal(atdt, vals, bloc);
4841 // Build a type descriptor for a slice type.
4843 Expression*
4844 Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
4846 source_location bloc = BUILTINS_LOCATION;
4848 Type* stdt = Array_type::make_slice_type_descriptor_type();
4850 const Struct_field_list* fields = stdt->struct_type()->fields();
4852 Expression_list* vals = new Expression_list();
4853 vals->reserve(2);
4855 Struct_field_list::const_iterator p = fields->begin();
4856 gcc_assert(p->field_name() == "commonType");
4857 vals->push_back(this->type_descriptor_constructor(gogo,
4858 RUNTIME_TYPE_KIND_SLICE,
4859 name, NULL, true));
4861 ++p;
4862 gcc_assert(p->field_name() == "elem");
4863 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
4865 ++p;
4866 gcc_assert(p == fields->end());
4868 return Expression::make_struct_composite_literal(stdt, vals, bloc);
4871 // Reflection string.
4873 void
4874 Array_type::do_reflection(Gogo* gogo, std::string* ret) const
4876 ret->push_back('[');
4877 if (this->length_ != NULL)
4879 mpz_t val;
4880 mpz_init(val);
4881 Type* type;
4882 if (!this->length_->integer_constant_value(true, val, &type))
4883 error_at(this->length_->location(),
4884 "array length must be integer constant expression");
4885 else if (mpz_cmp_si(val, 0) < 0)
4886 error_at(this->length_->location(), "array length is negative");
4887 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
4888 error_at(this->length_->location(), "array length is too large");
4889 else
4891 char buf[50];
4892 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
4893 ret->append(buf);
4895 mpz_clear(val);
4897 ret->push_back(']');
4899 this->append_reflection(this->element_type_, gogo, ret);
4902 // Mangled name.
4904 void
4905 Array_type::do_mangled_name(Gogo* gogo, std::string* ret) const
4907 ret->push_back('A');
4908 this->append_mangled_name(this->element_type_, gogo, ret);
4909 if (this->length_ != NULL)
4911 mpz_t val;
4912 mpz_init(val);
4913 Type* type;
4914 if (!this->length_->integer_constant_value(true, val, &type))
4915 error_at(this->length_->location(),
4916 "array length must be integer constant expression");
4917 else if (mpz_cmp_si(val, 0) < 0)
4918 error_at(this->length_->location(), "array length is negative");
4919 else if (mpz_cmp_ui(val, mpz_get_ui(val)) != 0)
4920 error_at(this->length_->location(), "array size is too large");
4921 else
4923 char buf[50];
4924 snprintf(buf, sizeof buf, "%lu", mpz_get_ui(val));
4925 ret->append(buf);
4927 mpz_clear(val);
4929 ret->push_back('e');
4932 // Make an array type.
4934 Array_type*
4935 Type::make_array_type(Type* element_type, Expression* length)
4937 return new Array_type(element_type, length);
4940 // Class Map_type.
4942 // Traversal.
4945 Map_type::do_traverse(Traverse* traverse)
4947 if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
4948 || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
4949 return TRAVERSE_EXIT;
4950 return TRAVERSE_CONTINUE;
4953 // Check that the map type is OK.
4955 bool
4956 Map_type::do_verify()
4958 if (this->key_type_->struct_type() != NULL
4959 || this->key_type_->array_type() != NULL)
4961 error_at(this->location_, "invalid map key type");
4962 return false;
4964 return true;
4967 // Whether two map types are identical.
4969 bool
4970 Map_type::is_identical(const Map_type* t, bool errors_are_identical) const
4972 return (Type::are_identical(this->key_type(), t->key_type(),
4973 errors_are_identical, NULL)
4974 && Type::are_identical(this->val_type(), t->val_type(),
4975 errors_are_identical, NULL));
4978 // Hash code.
4980 unsigned int
4981 Map_type::do_hash_for_method(Gogo* gogo) const
4983 return (this->key_type_->hash_for_method(gogo)
4984 + this->val_type_->hash_for_method(gogo)
4985 + 2);
4988 // Check that a call to the builtin make function is valid. For a map
4989 // the optional argument is the number of spaces to preallocate for
4990 // values.
4992 bool
4993 Map_type::do_check_make_expression(Expression_list* args,
4994 source_location location)
4996 if (args != NULL && !args->empty())
4998 if (!Type::check_int_value(args->front(), _("bad size when making map"),
4999 location))
5000 return false;
5001 else if (args->size() > 1)
5003 error_at(location, "too many arguments when making map");
5004 return false;
5007 return true;
5010 // Get a tree for a map type. A map type is represented as a pointer
5011 // to a struct. The struct is __go_map in libgo/map.h.
5013 tree
5014 Map_type::do_get_tree(Gogo* gogo)
5016 static tree type_tree;
5017 if (type_tree == NULL_TREE)
5019 tree struct_type = make_node(RECORD_TYPE);
5021 tree map_descriptor_type = gogo->map_descriptor_type();
5022 tree const_map_descriptor_type =
5023 build_qualified_type(map_descriptor_type, TYPE_QUAL_CONST);
5024 tree name = get_identifier("__descriptor");
5025 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
5026 build_pointer_type(const_map_descriptor_type));
5027 DECL_CONTEXT(field) = struct_type;
5028 TYPE_FIELDS(struct_type) = field;
5029 tree last_field = field;
5031 name = get_identifier("__element_count");
5032 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, sizetype);
5033 DECL_CONTEXT(field) = struct_type;
5034 DECL_CHAIN(last_field) = field;
5035 last_field = field;
5037 name = get_identifier("__bucket_count");
5038 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, sizetype);
5039 DECL_CONTEXT(field) = struct_type;
5040 DECL_CHAIN(last_field) = field;
5041 last_field = field;
5043 name = get_identifier("__buckets");
5044 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
5045 build_pointer_type(ptr_type_node));
5046 DECL_CONTEXT(field) = struct_type;
5047 DECL_CHAIN(last_field) = field;
5049 layout_type(struct_type);
5051 // Give the struct a name for better debugging info.
5052 name = get_identifier("__go_map");
5053 tree type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
5054 struct_type);
5055 DECL_ARTIFICIAL(type_decl) = 1;
5056 TYPE_NAME(struct_type) = type_decl;
5057 go_preserve_from_gc(type_decl);
5058 rest_of_decl_compilation(type_decl, 1, 0);
5060 type_tree = build_pointer_type(struct_type);
5061 go_preserve_from_gc(type_tree);
5064 return type_tree;
5067 // Initialize a map.
5069 tree
5070 Map_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
5072 if (is_clear)
5073 return NULL;
5074 return fold_convert(type_tree, null_pointer_node);
5077 // Return an expression for a newly allocated map.
5079 tree
5080 Map_type::do_make_expression_tree(Translate_context* context,
5081 Expression_list* args,
5082 source_location location)
5084 tree bad_index = NULL_TREE;
5086 tree expr_tree;
5087 if (args == NULL || args->empty())
5088 expr_tree = size_zero_node;
5089 else
5091 expr_tree = args->front()->get_tree(context);
5092 if (expr_tree == error_mark_node)
5093 return error_mark_node;
5094 if (!DECL_P(expr_tree))
5095 expr_tree = save_expr(expr_tree);
5096 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
5097 expr_tree = convert_to_integer(sizetype, expr_tree);
5098 bad_index = Expression::check_bounds(expr_tree, sizetype, bad_index,
5099 location);
5102 tree map_type = this->get_tree(context->gogo());
5104 static tree new_map_fndecl;
5105 tree ret = Gogo::call_builtin(&new_map_fndecl,
5106 location,
5107 "__go_new_map",
5109 map_type,
5110 TREE_TYPE(TYPE_FIELDS(TREE_TYPE(map_type))),
5111 context->gogo()->map_descriptor(this),
5112 sizetype,
5113 expr_tree);
5114 if (ret == error_mark_node)
5115 return error_mark_node;
5116 // This can panic if the capacity is out of range.
5117 TREE_NOTHROW(new_map_fndecl) = 0;
5119 if (bad_index == NULL_TREE)
5120 return ret;
5121 else
5123 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS,
5124 location);
5125 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
5126 build3(COND_EXPR, void_type_node,
5127 bad_index, crash, NULL_TREE),
5128 ret);
5132 // The type of a map type descriptor.
5134 Type*
5135 Map_type::make_map_type_descriptor_type()
5137 static Type* ret;
5138 if (ret == NULL)
5140 Type* tdt = Type::make_type_descriptor_type();
5141 Type* ptdt = Type::make_type_descriptor_ptr_type();
5143 Struct_type* sf =
5144 Type::make_builtin_struct_type(3,
5145 "", tdt,
5146 "key", ptdt,
5147 "elem", ptdt);
5149 ret = Type::make_builtin_named_type("MapType", sf);
5152 return ret;
5155 // Build a type descriptor for a map type.
5157 Expression*
5158 Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5160 source_location bloc = BUILTINS_LOCATION;
5162 Type* mtdt = Map_type::make_map_type_descriptor_type();
5164 const Struct_field_list* fields = mtdt->struct_type()->fields();
5166 Expression_list* vals = new Expression_list();
5167 vals->reserve(3);
5169 Struct_field_list::const_iterator p = fields->begin();
5170 gcc_assert(p->field_name() == "commonType");
5171 vals->push_back(this->type_descriptor_constructor(gogo,
5172 RUNTIME_TYPE_KIND_MAP,
5173 name, NULL, true));
5175 ++p;
5176 gcc_assert(p->field_name() == "key");
5177 vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
5179 ++p;
5180 gcc_assert(p->field_name() == "elem");
5181 vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
5183 ++p;
5184 gcc_assert(p == fields->end());
5186 return Expression::make_struct_composite_literal(mtdt, vals, bloc);
5189 // Reflection string for a map.
5191 void
5192 Map_type::do_reflection(Gogo* gogo, std::string* ret) const
5194 ret->append("map[");
5195 this->append_reflection(this->key_type_, gogo, ret);
5196 ret->append("] ");
5197 this->append_reflection(this->val_type_, gogo, ret);
5200 // Mangled name for a map.
5202 void
5203 Map_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5205 ret->push_back('M');
5206 this->append_mangled_name(this->key_type_, gogo, ret);
5207 ret->append("__");
5208 this->append_mangled_name(this->val_type_, gogo, ret);
5211 // Export a map type.
5213 void
5214 Map_type::do_export(Export* exp) const
5216 exp->write_c_string("map [");
5217 exp->write_type(this->key_type_);
5218 exp->write_c_string("] ");
5219 exp->write_type(this->val_type_);
5222 // Import a map type.
5224 Map_type*
5225 Map_type::do_import(Import* imp)
5227 imp->require_c_string("map [");
5228 Type* key_type = imp->read_type();
5229 imp->require_c_string("] ");
5230 Type* val_type = imp->read_type();
5231 return Type::make_map_type(key_type, val_type, imp->location());
5234 // Make a map type.
5236 Map_type*
5237 Type::make_map_type(Type* key_type, Type* val_type, source_location location)
5239 return new Map_type(key_type, val_type, location);
5242 // Class Channel_type.
5244 // Hash code.
5246 unsigned int
5247 Channel_type::do_hash_for_method(Gogo* gogo) const
5249 unsigned int ret = 0;
5250 if (this->may_send_)
5251 ret += 1;
5252 if (this->may_receive_)
5253 ret += 2;
5254 if (this->element_type_ != NULL)
5255 ret += this->element_type_->hash_for_method(gogo) << 2;
5256 return ret << 3;
5259 // Whether this type is the same as T.
5261 bool
5262 Channel_type::is_identical(const Channel_type* t,
5263 bool errors_are_identical) const
5265 if (!Type::are_identical(this->element_type(), t->element_type(),
5266 errors_are_identical, NULL))
5267 return false;
5268 return (this->may_send_ == t->may_send_
5269 && this->may_receive_ == t->may_receive_);
5272 // Check whether the parameters for a call to the builtin function
5273 // make are OK for a channel. A channel can take an optional single
5274 // parameter which is the buffer size.
5276 bool
5277 Channel_type::do_check_make_expression(Expression_list* args,
5278 source_location location)
5280 if (args != NULL && !args->empty())
5282 if (!Type::check_int_value(args->front(),
5283 _("bad buffer size when making channel"),
5284 location))
5285 return false;
5286 else if (args->size() > 1)
5288 error_at(location, "too many arguments when making channel");
5289 return false;
5292 return true;
5295 // Return the tree for a channel type. A channel is a pointer to a
5296 // __go_channel struct. The __go_channel struct is defined in
5297 // libgo/runtime/channel.h.
5299 tree
5300 Channel_type::do_get_tree(Gogo*)
5302 static tree type_tree;
5303 if (type_tree == NULL_TREE)
5305 tree ret = make_node(RECORD_TYPE);
5306 TYPE_NAME(ret) = get_identifier("__go_channel");
5307 TYPE_STUB_DECL(ret) = build_decl(BUILTINS_LOCATION, TYPE_DECL, NULL_TREE,
5308 ret);
5309 type_tree = build_pointer_type(ret);
5310 go_preserve_from_gc(type_tree);
5312 return type_tree;
5315 // Initialize a channel variable.
5317 tree
5318 Channel_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
5320 if (is_clear)
5321 return NULL;
5322 return fold_convert(type_tree, null_pointer_node);
5325 // Handle the builtin function make for a channel.
5327 tree
5328 Channel_type::do_make_expression_tree(Translate_context* context,
5329 Expression_list* args,
5330 source_location location)
5332 Gogo* gogo = context->gogo();
5333 tree channel_type = this->get_tree(gogo);
5335 tree element_tree = this->element_type_->get_tree(gogo);
5336 tree element_size_tree = size_in_bytes(element_tree);
5338 tree bad_index = NULL_TREE;
5340 tree expr_tree;
5341 if (args == NULL || args->empty())
5342 expr_tree = size_zero_node;
5343 else
5345 expr_tree = args->front()->get_tree(context);
5346 if (expr_tree == error_mark_node)
5347 return error_mark_node;
5348 if (!DECL_P(expr_tree))
5349 expr_tree = save_expr(expr_tree);
5350 if (!INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
5351 expr_tree = convert_to_integer(sizetype, expr_tree);
5352 bad_index = Expression::check_bounds(expr_tree, sizetype, bad_index,
5353 location);
5356 static tree new_channel_fndecl;
5357 tree ret = Gogo::call_builtin(&new_channel_fndecl,
5358 location,
5359 "__go_new_channel",
5361 channel_type,
5362 sizetype,
5363 element_size_tree,
5364 sizetype,
5365 expr_tree);
5366 if (ret == error_mark_node)
5367 return error_mark_node;
5368 // This can panic if the capacity is out of range.
5369 TREE_NOTHROW(new_channel_fndecl) = 0;
5371 if (bad_index == NULL_TREE)
5372 return ret;
5373 else
5375 tree crash = Gogo::runtime_error(RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS,
5376 location);
5377 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
5378 build3(COND_EXPR, void_type_node,
5379 bad_index, crash, NULL_TREE),
5380 ret);
5384 // Build a type descriptor for a channel type.
5386 Type*
5387 Channel_type::make_chan_type_descriptor_type()
5389 static Type* ret;
5390 if (ret == NULL)
5392 Type* tdt = Type::make_type_descriptor_type();
5393 Type* ptdt = Type::make_type_descriptor_ptr_type();
5395 Type* uintptr_type = Type::lookup_integer_type("uintptr");
5397 Struct_type* sf =
5398 Type::make_builtin_struct_type(3,
5399 "", tdt,
5400 "elem", ptdt,
5401 "dir", uintptr_type);
5403 ret = Type::make_builtin_named_type("ChanType", sf);
5406 return ret;
5409 // Build a type descriptor for a map type.
5411 Expression*
5412 Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5414 source_location bloc = BUILTINS_LOCATION;
5416 Type* ctdt = Channel_type::make_chan_type_descriptor_type();
5418 const Struct_field_list* fields = ctdt->struct_type()->fields();
5420 Expression_list* vals = new Expression_list();
5421 vals->reserve(3);
5423 Struct_field_list::const_iterator p = fields->begin();
5424 gcc_assert(p->field_name() == "commonType");
5425 vals->push_back(this->type_descriptor_constructor(gogo,
5426 RUNTIME_TYPE_KIND_CHAN,
5427 name, NULL, true));
5429 ++p;
5430 gcc_assert(p->field_name() == "elem");
5431 vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
5433 ++p;
5434 gcc_assert(p->field_name() == "dir");
5435 // These bits must match the ones in libgo/runtime/go-type.h.
5436 int val = 0;
5437 if (this->may_receive_)
5438 val |= 1;
5439 if (this->may_send_)
5440 val |= 2;
5441 mpz_t iv;
5442 mpz_init_set_ui(iv, val);
5443 vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
5444 mpz_clear(iv);
5446 ++p;
5447 gcc_assert(p == fields->end());
5449 return Expression::make_struct_composite_literal(ctdt, vals, bloc);
5452 // Reflection string.
5454 void
5455 Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
5457 if (!this->may_send_)
5458 ret->append("<-");
5459 ret->append("chan");
5460 if (!this->may_receive_)
5461 ret->append("<-");
5462 ret->push_back(' ');
5463 this->append_reflection(this->element_type_, gogo, ret);
5466 // Mangled name.
5468 void
5469 Channel_type::do_mangled_name(Gogo* gogo, std::string* ret) const
5471 ret->push_back('C');
5472 this->append_mangled_name(this->element_type_, gogo, ret);
5473 if (this->may_send_)
5474 ret->push_back('s');
5475 if (this->may_receive_)
5476 ret->push_back('r');
5477 ret->push_back('e');
5480 // Export.
5482 void
5483 Channel_type::do_export(Export* exp) const
5485 exp->write_c_string("chan ");
5486 if (this->may_send_ && !this->may_receive_)
5487 exp->write_c_string("-< ");
5488 else if (this->may_receive_ && !this->may_send_)
5489 exp->write_c_string("<- ");
5490 exp->write_type(this->element_type_);
5493 // Import.
5495 Channel_type*
5496 Channel_type::do_import(Import* imp)
5498 imp->require_c_string("chan ");
5500 bool may_send;
5501 bool may_receive;
5502 if (imp->match_c_string("-< "))
5504 imp->advance(3);
5505 may_send = true;
5506 may_receive = false;
5508 else if (imp->match_c_string("<- "))
5510 imp->advance(3);
5511 may_receive = true;
5512 may_send = false;
5514 else
5516 may_send = true;
5517 may_receive = true;
5520 Type* element_type = imp->read_type();
5522 return Type::make_channel_type(may_send, may_receive, element_type);
5525 // Make a new channel type.
5527 Channel_type*
5528 Type::make_channel_type(bool send, bool receive, Type* element_type)
5530 return new Channel_type(send, receive, element_type);
5533 // Class Interface_type.
5535 // Traversal.
5538 Interface_type::do_traverse(Traverse* traverse)
5540 if (this->methods_ == NULL)
5541 return TRAVERSE_CONTINUE;
5542 return this->methods_->traverse(traverse);
5545 // Finalize the methods. This handles interface inheritance.
5547 void
5548 Interface_type::finalize_methods()
5550 if (this->methods_ == NULL)
5551 return;
5552 bool is_recursive = false;
5553 size_t from = 0;
5554 size_t to = 0;
5555 while (from < this->methods_->size())
5557 const Typed_identifier* p = &this->methods_->at(from);
5558 if (!p->name().empty())
5560 size_t i;
5561 for (i = 0; i < to; ++i)
5563 if (this->methods_->at(i).name() == p->name())
5565 error_at(p->location(), "duplicate method %qs",
5566 Gogo::message_name(p->name()).c_str());
5567 break;
5570 if (i == to)
5572 if (from != to)
5573 this->methods_->set(to, *p);
5574 ++to;
5576 ++from;
5577 continue;
5579 Interface_type* it = p->type()->interface_type();
5580 if (it == NULL)
5582 error_at(p->location(), "interface contains embedded non-interface");
5583 ++from;
5584 continue;
5586 if (it == this)
5588 if (!is_recursive)
5590 error_at(p->location(), "invalid recursive interface");
5591 is_recursive = true;
5593 ++from;
5594 continue;
5596 const Typed_identifier_list* methods = it->methods();
5597 if (methods == NULL)
5599 ++from;
5600 continue;
5602 for (Typed_identifier_list::const_iterator q = methods->begin();
5603 q != methods->end();
5604 ++q)
5606 if (q->name().empty())
5608 if (q->type() == p->type())
5609 error_at(p->location(), "interface inheritance loop");
5610 else
5612 size_t i;
5613 for (i = from + 1; i < this->methods_->size(); ++i)
5615 const Typed_identifier* r = &this->methods_->at(i);
5616 if (r->name().empty() && r->type() == q->type())
5618 error_at(p->location(),
5619 "inherited interface listed twice");
5620 break;
5623 if (i == this->methods_->size())
5624 this->methods_->push_back(Typed_identifier(q->name(),
5625 q->type(),
5626 p->location()));
5629 else if (this->find_method(q->name()) == NULL)
5630 this->methods_->push_back(Typed_identifier(q->name(), q->type(),
5631 p->location()));
5632 else
5634 if (!is_recursive)
5635 error_at(p->location(), "inherited method %qs is ambiguous",
5636 Gogo::message_name(q->name()).c_str());
5639 ++from;
5641 if (to == 0)
5643 delete this->methods_;
5644 this->methods_ = NULL;
5646 else
5648 this->methods_->resize(to);
5649 this->methods_->sort_by_name();
5653 // Return the method NAME, or NULL.
5655 const Typed_identifier*
5656 Interface_type::find_method(const std::string& name) const
5658 if (this->methods_ == NULL)
5659 return NULL;
5660 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5661 p != this->methods_->end();
5662 ++p)
5663 if (p->name() == name)
5664 return &*p;
5665 return NULL;
5668 // Return the method index.
5670 size_t
5671 Interface_type::method_index(const std::string& name) const
5673 gcc_assert(this->methods_ != NULL);
5674 size_t ret = 0;
5675 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5676 p != this->methods_->end();
5677 ++p, ++ret)
5678 if (p->name() == name)
5679 return ret;
5680 gcc_unreachable();
5683 // Return whether NAME is an unexported method, for better error
5684 // reporting.
5686 bool
5687 Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
5689 if (this->methods_ == NULL)
5690 return false;
5691 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5692 p != this->methods_->end();
5693 ++p)
5695 const std::string& method_name(p->name());
5696 if (Gogo::is_hidden_name(method_name)
5697 && name == Gogo::unpack_hidden_name(method_name)
5698 && gogo->pack_hidden_name(name, false) != method_name)
5699 return true;
5701 return false;
5704 // Whether this type is identical with T.
5706 bool
5707 Interface_type::is_identical(const Interface_type* t,
5708 bool errors_are_identical) const
5710 // We require the same methods with the same types. The methods
5711 // have already been sorted.
5712 if (this->methods() == NULL || t->methods() == NULL)
5713 return this->methods() == t->methods();
5715 Typed_identifier_list::const_iterator p1 = this->methods()->begin();
5716 for (Typed_identifier_list::const_iterator p2 = t->methods()->begin();
5717 p2 != t->methods()->end();
5718 ++p1, ++p2)
5720 if (p1 == this->methods()->end())
5721 return false;
5722 if (p1->name() != p2->name()
5723 || !Type::are_identical(p1->type(), p2->type(),
5724 errors_are_identical, NULL))
5725 return false;
5727 if (p1 != this->methods()->end())
5728 return false;
5729 return true;
5732 // Whether we can assign the interface type T to this type. The types
5733 // are known to not be identical. An interface assignment is only
5734 // permitted if T is known to implement all methods in THIS.
5735 // Otherwise a type guard is required.
5737 bool
5738 Interface_type::is_compatible_for_assign(const Interface_type* t,
5739 std::string* reason) const
5741 if (this->methods() == NULL)
5742 return true;
5743 for (Typed_identifier_list::const_iterator p = this->methods()->begin();
5744 p != this->methods()->end();
5745 ++p)
5747 const Typed_identifier* m = t->find_method(p->name());
5748 if (m == NULL)
5750 if (reason != NULL)
5752 char buf[200];
5753 snprintf(buf, sizeof buf,
5754 _("need explicit conversion; missing method %s%s%s"),
5755 open_quote, Gogo::message_name(p->name()).c_str(),
5756 close_quote);
5757 reason->assign(buf);
5759 return false;
5762 std::string subreason;
5763 if (!Type::are_identical(p->type(), m->type(), true, &subreason))
5765 if (reason != NULL)
5767 std::string n = Gogo::message_name(p->name());
5768 size_t len = 100 + n.length() + subreason.length();
5769 char* buf = new char[len];
5770 if (subreason.empty())
5771 snprintf(buf, len, _("incompatible type for method %s%s%s"),
5772 open_quote, n.c_str(), close_quote);
5773 else
5774 snprintf(buf, len,
5775 _("incompatible type for method %s%s%s (%s)"),
5776 open_quote, n.c_str(), close_quote,
5777 subreason.c_str());
5778 reason->assign(buf);
5779 delete[] buf;
5781 return false;
5785 return true;
5788 // Hash code.
5790 unsigned int
5791 Interface_type::do_hash_for_method(Gogo* gogo) const
5793 unsigned int ret = 0;
5794 if (this->methods_ != NULL)
5796 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5797 p != this->methods_->end();
5798 ++p)
5800 ret = Type::hash_string(p->name(), ret);
5801 ret += p->type()->hash_for_method(gogo);
5802 ret <<= 1;
5805 return ret;
5808 // Return true if T implements the interface. If it does not, and
5809 // REASON is not NULL, set *REASON to a useful error message.
5811 bool
5812 Interface_type::implements_interface(const Type* t, std::string* reason) const
5814 if (this->methods_ == NULL)
5815 return true;
5817 bool is_pointer = false;
5818 const Named_type* nt = t->named_type();
5819 const Struct_type* st = t->struct_type();
5820 // If we start with a named type, we don't dereference it to find
5821 // methods.
5822 if (nt == NULL)
5824 const Type* pt = t->points_to();
5825 if (pt != NULL)
5827 // If T is a pointer to a named type, then we need to look at
5828 // the type to which it points.
5829 is_pointer = true;
5830 nt = pt->named_type();
5831 st = pt->struct_type();
5835 // If we have a named type, get the methods from it rather than from
5836 // any struct type.
5837 if (nt != NULL)
5838 st = NULL;
5840 // Only named and struct types have methods.
5841 if (nt == NULL && st == NULL)
5843 if (reason != NULL)
5845 if (t->points_to() != NULL
5846 && t->points_to()->interface_type() != NULL)
5847 reason->assign(_("pointer to interface type has no methods"));
5848 else
5849 reason->assign(_("type has no methods"));
5851 return false;
5854 if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
5856 if (reason != NULL)
5858 if (t->points_to() != NULL
5859 && t->points_to()->interface_type() != NULL)
5860 reason->assign(_("pointer to interface type has no methods"));
5861 else
5862 reason->assign(_("type has no methods"));
5864 return false;
5867 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5868 p != this->methods_->end();
5869 ++p)
5871 bool is_ambiguous = false;
5872 Method* m = (nt != NULL
5873 ? nt->method_function(p->name(), &is_ambiguous)
5874 : st->method_function(p->name(), &is_ambiguous));
5875 if (m == NULL)
5877 if (reason != NULL)
5879 std::string n = Gogo::message_name(p->name());
5880 size_t len = n.length() + 100;
5881 char* buf = new char[len];
5882 if (is_ambiguous)
5883 snprintf(buf, len, _("ambiguous method %s%s%s"),
5884 open_quote, n.c_str(), close_quote);
5885 else
5886 snprintf(buf, len, _("missing method %s%s%s"),
5887 open_quote, n.c_str(), close_quote);
5888 reason->assign(buf);
5889 delete[] buf;
5891 return false;
5894 Function_type *p_fn_type = p->type()->function_type();
5895 Function_type* m_fn_type = m->type()->function_type();
5896 gcc_assert(p_fn_type != NULL && m_fn_type != NULL);
5897 std::string subreason;
5898 if (!p_fn_type->is_identical(m_fn_type, true, true, &subreason))
5900 if (reason != NULL)
5902 std::string n = Gogo::message_name(p->name());
5903 size_t len = 100 + n.length() + subreason.length();
5904 char* buf = new char[len];
5905 if (subreason.empty())
5906 snprintf(buf, len, _("incompatible type for method %s%s%s"),
5907 open_quote, n.c_str(), close_quote);
5908 else
5909 snprintf(buf, len,
5910 _("incompatible type for method %s%s%s (%s)"),
5911 open_quote, n.c_str(), close_quote,
5912 subreason.c_str());
5913 reason->assign(buf);
5914 delete[] buf;
5916 return false;
5919 if (!is_pointer && !m->is_value_method())
5921 if (reason != NULL)
5923 std::string n = Gogo::message_name(p->name());
5924 size_t len = 100 + n.length();
5925 char* buf = new char[len];
5926 snprintf(buf, len, _("method %s%s%s requires a pointer"),
5927 open_quote, n.c_str(), close_quote);
5928 reason->assign(buf);
5929 delete[] buf;
5931 return false;
5935 return true;
5938 // Return a tree for an interface type. An interface is a pointer to
5939 // a struct. The struct has three fields. The first field is a
5940 // pointer to the type descriptor for the dynamic type of the object.
5941 // The second field is a pointer to a table of methods for the
5942 // interface to be used with the object. The third field is the value
5943 // of the object itself.
5945 tree
5946 Interface_type::do_get_tree(Gogo* gogo)
5948 if (this->methods_ == NULL)
5950 // At the tree level, use the same type for all empty
5951 // interfaces. This lets us assign them to each other directly
5952 // without triggering GIMPLE type errors.
5953 tree dtype = Type::make_type_descriptor_type()->get_tree(gogo);
5954 dtype = build_pointer_type(build_qualified_type(dtype, TYPE_QUAL_CONST));
5955 static tree empty_interface;
5956 return Gogo::builtin_struct(&empty_interface, "__go_empty_interface",
5957 NULL_TREE, 2,
5958 "__type_descriptor",
5959 dtype,
5960 "__object",
5961 ptr_type_node);
5964 return this->fill_in_tree(gogo, make_node(RECORD_TYPE));
5967 // Fill in the tree for an interface type. This is used for named
5968 // interface types.
5970 tree
5971 Interface_type::fill_in_tree(Gogo* gogo, tree type)
5973 gcc_assert(this->methods_ != NULL);
5975 // Build the type of the table of methods.
5977 tree method_table = make_node(RECORD_TYPE);
5979 // The first field is a pointer to the type descriptor.
5980 tree name_tree = get_identifier("__type_descriptor");
5981 tree dtype = Type::make_type_descriptor_type()->get_tree(gogo);
5982 dtype = build_pointer_type(build_qualified_type(dtype, TYPE_QUAL_CONST));
5983 tree field = build_decl(this->location_, FIELD_DECL, name_tree, dtype);
5984 DECL_CONTEXT(field) = method_table;
5985 TYPE_FIELDS(method_table) = field;
5987 std::string last_name = "";
5988 tree* pp = &DECL_CHAIN(field);
5989 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
5990 p != this->methods_->end();
5991 ++p)
5993 std::string name = Gogo::unpack_hidden_name(p->name());
5994 name_tree = get_identifier_with_length(name.data(), name.length());
5995 tree field_type = p->type()->get_tree(gogo);
5996 if (field_type == error_mark_node)
5997 return error_mark_node;
5998 field = build_decl(this->location_, FIELD_DECL, name_tree, field_type);
5999 DECL_CONTEXT(field) = method_table;
6000 *pp = field;
6001 pp = &DECL_CHAIN(field);
6002 // Sanity check: the names should be sorted.
6003 gcc_assert(p->name() > last_name);
6004 last_name = p->name();
6006 layout_type(method_table);
6008 tree mtype = build_pointer_type(method_table);
6010 tree field_trees = NULL_TREE;
6011 pp = &field_trees;
6013 name_tree = get_identifier("__methods");
6014 field = build_decl(this->location_, FIELD_DECL, name_tree, mtype);
6015 DECL_CONTEXT(field) = type;
6016 *pp = field;
6017 pp = &DECL_CHAIN(field);
6019 name_tree = get_identifier("__object");
6020 field = build_decl(this->location_, FIELD_DECL, name_tree, ptr_type_node);
6021 DECL_CONTEXT(field) = type;
6022 *pp = field;
6024 TYPE_FIELDS(type) = field_trees;
6026 layout_type(type);
6028 return type;
6031 // Initialization value.
6033 tree
6034 Interface_type::do_get_init_tree(Gogo*, tree type_tree, bool is_clear)
6036 if (is_clear)
6037 return NULL;
6039 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
6040 for (tree field = TYPE_FIELDS(type_tree);
6041 field != NULL_TREE;
6042 field = DECL_CHAIN(field))
6044 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
6045 elt->index = field;
6046 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
6049 tree ret = build_constructor(type_tree, init);
6050 TREE_CONSTANT(ret) = 1;
6051 return ret;
6054 // The type of an interface type descriptor.
6056 Type*
6057 Interface_type::make_interface_type_descriptor_type()
6059 static Type* ret;
6060 if (ret == NULL)
6062 Type* tdt = Type::make_type_descriptor_type();
6063 Type* ptdt = Type::make_type_descriptor_ptr_type();
6065 Type* string_type = Type::lookup_string_type();
6066 Type* pointer_string_type = Type::make_pointer_type(string_type);
6068 Struct_type* sm =
6069 Type::make_builtin_struct_type(3,
6070 "name", pointer_string_type,
6071 "pkgPath", pointer_string_type,
6072 "typ", ptdt);
6074 Type* nsm = Type::make_builtin_named_type("imethod", sm);
6076 Type* slice_nsm = Type::make_array_type(nsm, NULL);
6078 Struct_type* s = Type::make_builtin_struct_type(2,
6079 "", tdt,
6080 "methods", slice_nsm);
6082 ret = Type::make_builtin_named_type("InterfaceType", s);
6085 return ret;
6088 // Build a type descriptor for an interface type.
6090 Expression*
6091 Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6093 source_location bloc = BUILTINS_LOCATION;
6095 Type* itdt = Interface_type::make_interface_type_descriptor_type();
6097 const Struct_field_list* ifields = itdt->struct_type()->fields();
6099 Expression_list* ivals = new Expression_list();
6100 ivals->reserve(2);
6102 Struct_field_list::const_iterator pif = ifields->begin();
6103 gcc_assert(pif->field_name() == "commonType");
6104 ivals->push_back(this->type_descriptor_constructor(gogo,
6105 RUNTIME_TYPE_KIND_INTERFACE,
6106 name, NULL, true));
6108 ++pif;
6109 gcc_assert(pif->field_name() == "methods");
6111 Expression_list* methods = new Expression_list();
6112 if (this->methods_ != NULL && !this->methods_->empty())
6114 Type* elemtype = pif->type()->array_type()->element_type();
6116 methods->reserve(this->methods_->size());
6117 for (Typed_identifier_list::const_iterator pm = this->methods_->begin();
6118 pm != this->methods_->end();
6119 ++pm)
6121 const Struct_field_list* mfields = elemtype->struct_type()->fields();
6123 Expression_list* mvals = new Expression_list();
6124 mvals->reserve(3);
6126 Struct_field_list::const_iterator pmf = mfields->begin();
6127 gcc_assert(pmf->field_name() == "name");
6128 std::string s = Gogo::unpack_hidden_name(pm->name());
6129 Expression* e = Expression::make_string(s, bloc);
6130 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6132 ++pmf;
6133 gcc_assert(pmf->field_name() == "pkgPath");
6134 if (!Gogo::is_hidden_name(pm->name()))
6135 mvals->push_back(Expression::make_nil(bloc));
6136 else
6138 s = Gogo::hidden_name_prefix(pm->name());
6139 e = Expression::make_string(s, bloc);
6140 mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
6143 ++pmf;
6144 gcc_assert(pmf->field_name() == "typ");
6145 mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
6147 ++pmf;
6148 gcc_assert(pmf == mfields->end());
6150 e = Expression::make_struct_composite_literal(elemtype, mvals,
6151 bloc);
6152 methods->push_back(e);
6156 ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
6157 methods, bloc));
6159 ++pif;
6160 gcc_assert(pif == ifields->end());
6162 return Expression::make_struct_composite_literal(itdt, ivals, bloc);
6165 // Reflection string.
6167 void
6168 Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
6170 ret->append("interface {");
6171 if (this->methods_ != NULL)
6173 for (Typed_identifier_list::const_iterator p = this->methods_->begin();
6174 p != this->methods_->end();
6175 ++p)
6177 if (p != this->methods_->begin())
6178 ret->append(";");
6179 ret->push_back(' ');
6180 ret->append(Gogo::unpack_hidden_name(p->name()));
6181 std::string sub = p->type()->reflection(gogo);
6182 gcc_assert(sub.compare(0, 4, "func") == 0);
6183 sub = sub.substr(4);
6184 ret->append(sub);
6187 ret->append(" }");
6190 // Mangled name.
6192 void
6193 Interface_type::do_mangled_name(Gogo* gogo, std::string* ret) const
6195 ret->push_back('I');
6197 const Typed_identifier_list* methods = this->methods_;
6198 if (methods != NULL)
6200 for (Typed_identifier_list::const_iterator p = methods->begin();
6201 p != methods->end();
6202 ++p)
6204 std::string n = Gogo::unpack_hidden_name(p->name());
6205 char buf[20];
6206 snprintf(buf, sizeof buf, "%u_",
6207 static_cast<unsigned int>(n.length()));
6208 ret->append(buf);
6209 ret->append(n);
6210 this->append_mangled_name(p->type(), gogo, ret);
6214 ret->push_back('e');
6217 // Export.
6219 void
6220 Interface_type::do_export(Export* exp) const
6222 exp->write_c_string("interface { ");
6224 const Typed_identifier_list* methods = this->methods_;
6225 if (methods != NULL)
6227 for (Typed_identifier_list::const_iterator pm = methods->begin();
6228 pm != methods->end();
6229 ++pm)
6231 exp->write_string(pm->name());
6232 exp->write_c_string(" (");
6234 const Function_type* fntype = pm->type()->function_type();
6236 bool first = true;
6237 const Typed_identifier_list* parameters = fntype->parameters();
6238 if (parameters != NULL)
6240 bool is_varargs = fntype->is_varargs();
6241 for (Typed_identifier_list::const_iterator pp =
6242 parameters->begin();
6243 pp != parameters->end();
6244 ++pp)
6246 if (first)
6247 first = false;
6248 else
6249 exp->write_c_string(", ");
6250 if (!is_varargs || pp + 1 != parameters->end())
6251 exp->write_type(pp->type());
6252 else
6254 exp->write_c_string("...");
6255 Type *pptype = pp->type();
6256 exp->write_type(pptype->array_type()->element_type());
6261 exp->write_c_string(")");
6263 const Typed_identifier_list* results = fntype->results();
6264 if (results != NULL)
6266 exp->write_c_string(" ");
6267 if (results->size() == 1)
6268 exp->write_type(results->begin()->type());
6269 else
6271 first = true;
6272 exp->write_c_string("(");
6273 for (Typed_identifier_list::const_iterator p =
6274 results->begin();
6275 p != results->end();
6276 ++p)
6278 if (first)
6279 first = false;
6280 else
6281 exp->write_c_string(", ");
6282 exp->write_type(p->type());
6284 exp->write_c_string(")");
6288 exp->write_c_string("; ");
6292 exp->write_c_string("}");
6295 // Import an interface type.
6297 Interface_type*
6298 Interface_type::do_import(Import* imp)
6300 imp->require_c_string("interface { ");
6302 Typed_identifier_list* methods = new Typed_identifier_list;
6303 while (imp->peek_char() != '}')
6305 std::string name = imp->read_identifier();
6306 imp->require_c_string(" (");
6308 Typed_identifier_list* parameters;
6309 bool is_varargs = false;
6310 if (imp->peek_char() == ')')
6311 parameters = NULL;
6312 else
6314 parameters = new Typed_identifier_list;
6315 while (true)
6317 if (imp->match_c_string("..."))
6319 imp->advance(3);
6320 is_varargs = true;
6323 Type* ptype = imp->read_type();
6324 if (is_varargs)
6325 ptype = Type::make_array_type(ptype, NULL);
6326 parameters->push_back(Typed_identifier(Import::import_marker,
6327 ptype, imp->location()));
6328 if (imp->peek_char() != ',')
6329 break;
6330 gcc_assert(!is_varargs);
6331 imp->require_c_string(", ");
6334 imp->require_c_string(")");
6336 Typed_identifier_list* results;
6337 if (imp->peek_char() != ' ')
6338 results = NULL;
6339 else
6341 results = new Typed_identifier_list;
6342 imp->advance(1);
6343 if (imp->peek_char() != '(')
6345 Type* rtype = imp->read_type();
6346 results->push_back(Typed_identifier(Import::import_marker,
6347 rtype, imp->location()));
6349 else
6351 imp->advance(1);
6352 while (true)
6354 Type* rtype = imp->read_type();
6355 results->push_back(Typed_identifier(Import::import_marker,
6356 rtype, imp->location()));
6357 if (imp->peek_char() != ',')
6358 break;
6359 imp->require_c_string(", ");
6361 imp->require_c_string(")");
6365 Function_type* fntype = Type::make_function_type(NULL, parameters,
6366 results,
6367 imp->location());
6368 if (is_varargs)
6369 fntype->set_is_varargs();
6370 methods->push_back(Typed_identifier(name, fntype, imp->location()));
6372 imp->require_c_string("; ");
6375 imp->require_c_string("}");
6377 if (methods->empty())
6379 delete methods;
6380 methods = NULL;
6383 return Type::make_interface_type(methods, imp->location());
6386 // Make an interface type.
6388 Interface_type*
6389 Type::make_interface_type(Typed_identifier_list* methods,
6390 source_location location)
6392 return new Interface_type(methods, location);
6395 // Class Method.
6397 // Bind a method to an object.
6399 Expression*
6400 Method::bind_method(Expression* expr, source_location location) const
6402 if (this->stub_ == NULL)
6404 // When there is no stub object, the binding is determined by
6405 // the child class.
6406 return this->do_bind_method(expr, location);
6409 Expression* func = Expression::make_func_reference(this->stub_, NULL,
6410 location);
6411 return Expression::make_bound_method(expr, func, location);
6414 // Return the named object associated with a method. This may only be
6415 // called after methods are finalized.
6417 Named_object*
6418 Method::named_object() const
6420 if (this->stub_ != NULL)
6421 return this->stub_;
6422 return this->do_named_object();
6425 // Class Named_method.
6427 // The type of the method.
6429 Function_type*
6430 Named_method::do_type() const
6432 if (this->named_object_->is_function())
6433 return this->named_object_->func_value()->type();
6434 else if (this->named_object_->is_function_declaration())
6435 return this->named_object_->func_declaration_value()->type();
6436 else
6437 gcc_unreachable();
6440 // Return the location of the method receiver.
6442 source_location
6443 Named_method::do_receiver_location() const
6445 return this->do_type()->receiver()->location();
6448 // Bind a method to an object.
6450 Expression*
6451 Named_method::do_bind_method(Expression* expr, source_location location) const
6453 Expression* func = Expression::make_func_reference(this->named_object_, NULL,
6454 location);
6455 Bound_method_expression* bme = Expression::make_bound_method(expr, func,
6456 location);
6457 // If this is not a local method, and it does not use a stub, then
6458 // the real method expects a different type. We need to cast the
6459 // first argument.
6460 if (this->depth() > 0 && !this->needs_stub_method())
6462 Function_type* ftype = this->do_type();
6463 gcc_assert(ftype->is_method());
6464 Type* frtype = ftype->receiver()->type();
6465 bme->set_first_argument_type(frtype);
6467 return bme;
6470 // Class Interface_method.
6472 // Bind a method to an object.
6474 Expression*
6475 Interface_method::do_bind_method(Expression* expr,
6476 source_location location) const
6478 return Expression::make_interface_field_reference(expr, this->name_,
6479 location);
6482 // Class Methods.
6484 // Insert a new method. Return true if it was inserted, false
6485 // otherwise.
6487 bool
6488 Methods::insert(const std::string& name, Method* m)
6490 std::pair<Method_map::iterator, bool> ins =
6491 this->methods_.insert(std::make_pair(name, m));
6492 if (ins.second)
6493 return true;
6494 else
6496 Method* old_method = ins.first->second;
6497 if (m->depth() < old_method->depth())
6499 delete old_method;
6500 ins.first->second = m;
6501 return true;
6503 else
6505 if (m->depth() == old_method->depth())
6506 old_method->set_is_ambiguous();
6507 return false;
6512 // Return the number of unambiguous methods.
6514 size_t
6515 Methods::count() const
6517 size_t ret = 0;
6518 for (Method_map::const_iterator p = this->methods_.begin();
6519 p != this->methods_.end();
6520 ++p)
6521 if (!p->second->is_ambiguous())
6522 ++ret;
6523 return ret;
6526 // Class Named_type.
6528 // Return the name of the type.
6530 const std::string&
6531 Named_type::name() const
6533 return this->named_object_->name();
6536 // Return the name of the type to use in an error message.
6538 std::string
6539 Named_type::message_name() const
6541 return this->named_object_->message_name();
6544 // Return the base type for this type. We have to be careful about
6545 // circular type definitions, which are invalid but may be seen here.
6547 Type*
6548 Named_type::named_base()
6550 if (this->seen_ > 0)
6551 return this;
6552 ++this->seen_;
6553 Type* ret = this->type_->base();
6554 --this->seen_;
6555 return ret;
6558 const Type*
6559 Named_type::named_base() const
6561 if (this->seen_ > 0)
6562 return this;
6563 ++this->seen_;
6564 const Type* ret = this->type_->base();
6565 --this->seen_;
6566 return ret;
6569 // Return whether this is an error type. We have to be careful about
6570 // circular type definitions, which are invalid but may be seen here.
6572 bool
6573 Named_type::is_named_error_type() const
6575 if (this->seen_ > 0)
6576 return false;
6577 ++this->seen_;
6578 bool ret = this->type_->is_error_type();
6579 --this->seen_;
6580 return ret;
6583 // Add a method to this type.
6585 Named_object*
6586 Named_type::add_method(const std::string& name, Function* function)
6588 if (this->local_methods_ == NULL)
6589 this->local_methods_ = new Bindings(NULL);
6590 return this->local_methods_->add_function(name, NULL, function);
6593 // Add a method declaration to this type.
6595 Named_object*
6596 Named_type::add_method_declaration(const std::string& name, Package* package,
6597 Function_type* type,
6598 source_location location)
6600 if (this->local_methods_ == NULL)
6601 this->local_methods_ = new Bindings(NULL);
6602 return this->local_methods_->add_function_declaration(name, package, type,
6603 location);
6606 // Add an existing method to this type.
6608 void
6609 Named_type::add_existing_method(Named_object* no)
6611 if (this->local_methods_ == NULL)
6612 this->local_methods_ = new Bindings(NULL);
6613 this->local_methods_->add_named_object(no);
6616 // Look for a local method NAME, and returns its named object, or NULL
6617 // if not there.
6619 Named_object*
6620 Named_type::find_local_method(const std::string& name) const
6622 if (this->local_methods_ == NULL)
6623 return NULL;
6624 return this->local_methods_->lookup(name);
6627 // Return whether NAME is an unexported field or method, for better
6628 // error reporting.
6630 bool
6631 Named_type::is_unexported_local_method(Gogo* gogo,
6632 const std::string& name) const
6634 Bindings* methods = this->local_methods_;
6635 if (methods != NULL)
6637 for (Bindings::const_declarations_iterator p =
6638 methods->begin_declarations();
6639 p != methods->end_declarations();
6640 ++p)
6642 if (Gogo::is_hidden_name(p->first)
6643 && name == Gogo::unpack_hidden_name(p->first)
6644 && gogo->pack_hidden_name(name, false) != p->first)
6645 return true;
6648 return false;
6651 // Build the complete list of methods for this type, which means
6652 // recursively including all methods for anonymous fields. Create all
6653 // stub methods.
6655 void
6656 Named_type::finalize_methods(Gogo* gogo)
6658 if (this->all_methods_ != NULL)
6659 return;
6661 if (this->local_methods_ != NULL
6662 && (this->points_to() != NULL || this->interface_type() != NULL))
6664 const Bindings* lm = this->local_methods_;
6665 for (Bindings::const_declarations_iterator p = lm->begin_declarations();
6666 p != lm->end_declarations();
6667 ++p)
6668 error_at(p->second->location(),
6669 "invalid pointer or interface receiver type");
6670 delete this->local_methods_;
6671 this->local_methods_ = NULL;
6672 return;
6675 Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
6678 // Return the method NAME, or NULL if there isn't one or if it is
6679 // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6680 // ambiguous.
6682 Method*
6683 Named_type::method_function(const std::string& name, bool* is_ambiguous) const
6685 return Type::method_function(this->all_methods_, name, is_ambiguous);
6688 // Return a pointer to the interface method table for this type for
6689 // the interface INTERFACE. IS_POINTER is true if this is for a
6690 // pointer to THIS.
6692 tree
6693 Named_type::interface_method_table(Gogo* gogo, const Interface_type* interface,
6694 bool is_pointer)
6696 gcc_assert(!interface->is_empty());
6698 Interface_method_tables** pimt = (is_pointer
6699 ? &this->interface_method_tables_
6700 : &this->pointer_interface_method_tables_);
6702 if (*pimt == NULL)
6703 *pimt = new Interface_method_tables(5);
6705 std::pair<const Interface_type*, tree> val(interface, NULL_TREE);
6706 std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
6708 if (ins.second)
6710 // This is a new entry in the hash table.
6711 gcc_assert(ins.first->second == NULL_TREE);
6712 ins.first->second = gogo->interface_method_table_for_type(interface,
6713 this,
6714 is_pointer);
6717 tree decl = ins.first->second;
6718 if (decl == error_mark_node)
6719 return error_mark_node;
6720 gcc_assert(decl != NULL_TREE && TREE_CODE(decl) == VAR_DECL);
6721 return build_fold_addr_expr(decl);
6724 // Return whether a named type has any hidden fields.
6726 bool
6727 Named_type::named_type_has_hidden_fields(std::string* reason) const
6729 if (this->seen_ > 0)
6730 return false;
6731 ++this->seen_;
6732 bool ret = this->type_->has_hidden_fields(this, reason);
6733 --this->seen_;
6734 return ret;
6737 // Look for a use of a complete type within another type. This is
6738 // used to check that we don't try to use a type within itself.
6740 class Find_type_use : public Traverse
6742 public:
6743 Find_type_use(Type* find_type)
6744 : Traverse(traverse_types),
6745 find_type_(find_type), found_(false)
6748 // Whether we found the type.
6749 bool
6750 found() const
6751 { return this->found_; }
6753 protected:
6755 type(Type*);
6757 private:
6758 // The type we are looking for.
6759 Type* find_type_;
6760 // Whether we found the type.
6761 bool found_;
6764 // Check for FIND_TYPE in TYPE.
6767 Find_type_use::type(Type* type)
6769 if (this->find_type_ == type)
6771 this->found_ = true;
6772 return TRAVERSE_EXIT;
6774 // It's OK if we see a reference to the type in any type which is
6775 // essentially a pointer: a pointer, a slice, a function, a map, or
6776 // a channel.
6777 if (type->points_to() != NULL
6778 || type->is_open_array_type()
6779 || type->function_type() != NULL
6780 || type->map_type() != NULL
6781 || type->channel_type() != NULL)
6782 return TRAVERSE_SKIP_COMPONENTS;
6784 // For an interface, a reference to the type in a method type should
6785 // be ignored, but we have to consider direct inheritance. When
6786 // this is called, there may be cases of direct inheritance
6787 // represented as a method with no name.
6788 if (type->interface_type() != NULL)
6790 const Typed_identifier_list* methods = type->interface_type()->methods();
6791 if (methods != NULL)
6793 for (Typed_identifier_list::const_iterator p = methods->begin();
6794 p != methods->end();
6795 ++p)
6797 if (p->name().empty())
6799 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
6800 return TRAVERSE_EXIT;
6804 return TRAVERSE_SKIP_COMPONENTS;
6807 return TRAVERSE_CONTINUE;
6810 // Verify that a named type does not refer to itself.
6812 bool
6813 Named_type::do_verify()
6815 Find_type_use find(this);
6816 Type::traverse(this->type_, &find);
6817 if (find.found())
6819 error_at(this->location_, "invalid recursive type %qs",
6820 this->message_name().c_str());
6821 this->is_error_ = true;
6822 return false;
6825 // Check whether any of the local methods overloads an existing
6826 // struct field or interface method. We don't need to check the
6827 // list of methods against itself: that is handled by the Bindings
6828 // code.
6829 if (this->local_methods_ != NULL)
6831 Struct_type* st = this->type_->struct_type();
6832 Interface_type* it = this->type_->interface_type();
6833 bool found_dup = false;
6834 if (st != NULL || it != NULL)
6836 for (Bindings::const_declarations_iterator p =
6837 this->local_methods_->begin_declarations();
6838 p != this->local_methods_->end_declarations();
6839 ++p)
6841 const std::string& name(p->first);
6842 if (st != NULL && st->find_local_field(name, NULL) != NULL)
6844 error_at(p->second->location(),
6845 "method %qs redeclares struct field name",
6846 Gogo::message_name(name).c_str());
6847 found_dup = true;
6849 if (it != NULL && it->find_method(name) != NULL)
6851 error_at(p->second->location(),
6852 "method %qs redeclares interface method name",
6853 Gogo::message_name(name).c_str());
6854 found_dup = true;
6858 if (found_dup)
6859 return false;
6862 return true;
6865 // Return whether this type is or contains a pointer.
6867 bool
6868 Named_type::do_has_pointer() const
6870 if (this->seen_ > 0)
6871 return false;
6872 ++this->seen_;
6873 bool ret = this->type_->has_pointer();
6874 --this->seen_;
6875 return ret;
6878 // Return a hash code. This is used for method lookup. We simply
6879 // hash on the name itself.
6881 unsigned int
6882 Named_type::do_hash_for_method(Gogo* gogo) const
6884 const std::string& name(this->named_object()->name());
6885 unsigned int ret = Type::hash_string(name, 0);
6887 // GOGO will be NULL here when called from Type_hash_identical.
6888 // That is OK because that is only used for internal hash tables
6889 // where we are going to be comparing named types for equality. In
6890 // other cases, which are cases where the runtime is going to
6891 // compare hash codes to see if the types are the same, we need to
6892 // include the package prefix and name in the hash.
6893 if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
6895 const Package* package = this->named_object()->package();
6896 if (package == NULL)
6898 ret = Type::hash_string(gogo->unique_prefix(), ret);
6899 ret = Type::hash_string(gogo->package_name(), ret);
6901 else
6903 ret = Type::hash_string(package->unique_prefix(), ret);
6904 ret = Type::hash_string(package->name(), ret);
6908 return ret;
6911 // Get a tree for a named type.
6913 tree
6914 Named_type::do_get_tree(Gogo* gogo)
6916 if (this->is_error_)
6917 return error_mark_node;
6919 // Go permits types to refer to themselves in various ways. Break
6920 // the recursion here.
6921 tree t;
6922 switch (this->type_->forwarded()->classification())
6924 case TYPE_ERROR:
6925 return error_mark_node;
6927 case TYPE_VOID:
6928 case TYPE_BOOLEAN:
6929 case TYPE_INTEGER:
6930 case TYPE_FLOAT:
6931 case TYPE_COMPLEX:
6932 case TYPE_STRING:
6933 case TYPE_NIL:
6934 // These types can not refer to themselves.
6935 case TYPE_MAP:
6936 case TYPE_CHANNEL:
6937 // All maps and channels have the same type in GENERIC.
6938 t = Type::get_named_type_tree(gogo, this->type_);
6939 if (t == error_mark_node)
6940 return error_mark_node;
6941 // Build a copy to set TYPE_NAME.
6942 t = build_variant_type_copy(t);
6943 break;
6945 case TYPE_FUNCTION:
6946 // GENERIC can't handle a pointer to a function type whose
6947 // return type is a pointer to the function type itself. It
6948 // goes into an infinite loop when walking the types.
6949 if (this->seen_ > 0)
6951 Function_type* fntype = this->type_->function_type();
6952 if (fntype->results() != NULL
6953 && fntype->results()->size() == 1
6954 && fntype->results()->front().type()->forwarded() == this)
6955 return ptr_type_node;
6957 // We can legitimately see ourselves here twice when a named
6958 // type is defined using a struct which refers to the named
6959 // type. If we see ourselves too often we are in a loop.
6960 if (this->seen_ > 3)
6961 return ptr_type_node;
6963 ++this->seen_;
6964 t = Type::get_named_type_tree(gogo, this->type_);
6965 --this->seen_;
6966 if (t == error_mark_node)
6967 return error_mark_node;
6968 t = build_variant_type_copy(t);
6969 break;
6971 case TYPE_POINTER:
6972 // Don't recur infinitely if a pointer type refers to itself.
6973 // Ideally we would build a circular data structure here, but
6974 // GENERIC can't handle them.
6975 if (this->seen_ > 0)
6977 if (this->type_->points_to()->forwarded() == this)
6978 return ptr_type_node;
6980 if (this->seen_ > 3)
6981 return ptr_type_node;
6983 ++this->seen_;
6984 t = Type::get_named_type_tree(gogo, this->type_);
6985 --this->seen_;
6986 if (t == error_mark_node)
6987 return error_mark_node;
6988 t = build_variant_type_copy(t);
6989 break;
6991 case TYPE_STRUCT:
6992 if (this->named_tree_ != NULL_TREE)
6993 return this->named_tree_;
6994 t = make_node(RECORD_TYPE);
6995 this->named_tree_ = t;
6996 t = this->type_->struct_type()->fill_in_tree(gogo, t);
6997 if (t == error_mark_node)
6998 return error_mark_node;
6999 break;
7001 case TYPE_ARRAY:
7002 if (!this->is_open_array_type())
7003 t = Type::get_named_type_tree(gogo, this->type_);
7004 else
7006 if (this->named_tree_ != NULL_TREE)
7007 return this->named_tree_;
7008 t = gogo->slice_type_tree(void_type_node);
7009 this->named_tree_ = t;
7010 t = this->type_->array_type()->fill_in_tree(gogo, t);
7012 if (t == error_mark_node)
7013 return error_mark_node;
7014 t = build_variant_type_copy(t);
7015 break;
7017 case TYPE_INTERFACE:
7018 if (this->type_->interface_type()->is_empty())
7020 t = Type::get_named_type_tree(gogo, this->type_);
7021 if (t == error_mark_node)
7022 return error_mark_node;
7023 t = build_variant_type_copy(t);
7025 else
7027 if (this->named_tree_ != NULL_TREE)
7028 return this->named_tree_;
7029 t = make_node(RECORD_TYPE);
7030 this->named_tree_ = t;
7031 t = this->type_->interface_type()->fill_in_tree(gogo, t);
7032 if (t == error_mark_node)
7033 return error_mark_node;
7035 break;
7037 case TYPE_NAMED:
7039 // When a named type T1 is defined as another named type T2,
7040 // the definition must simply be "type T1 T2". If the
7041 // definition of T2 may refer to T1, then we must simply
7042 // return the type for T2 here. It's not precisely correct,
7043 // but it's as close as we can get with GENERIC.
7044 ++this->seen_;
7045 t = Type::get_named_type_tree(gogo, this->type_);
7046 --this->seen_;
7047 if (this->seen_ > 0)
7048 return t;
7049 if (t == error_mark_node)
7050 return error_mark_node;
7051 t = build_variant_type_copy(t);
7053 break;
7055 case TYPE_FORWARD:
7056 // An undefined forwarding type. Make sure the error is
7057 // emitted.
7058 this->type_->forward_declaration_type()->real_type();
7059 return error_mark_node;
7061 default:
7062 case TYPE_SINK:
7063 case TYPE_CALL_MULTIPLE_RESULT:
7064 gcc_unreachable();
7067 tree id = this->named_object_->get_id(gogo);
7068 tree decl = build_decl(this->location_, TYPE_DECL, id, t);
7069 TYPE_NAME(t) = decl;
7071 return t;
7074 // Build a type descriptor for a named type.
7076 Expression*
7077 Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
7079 // If NAME is not NULL, then we don't really want the type
7080 // descriptor for this type; we want the descriptor for the
7081 // underlying type, giving it the name NAME.
7082 return this->named_type_descriptor(gogo, this->type_,
7083 name == NULL ? this : name);
7086 // Add to the reflection string. This is used mostly for the name of
7087 // the type used in a type descriptor, not for actual reflection
7088 // strings.
7090 void
7091 Named_type::do_reflection(Gogo* gogo, std::string* ret) const
7093 if (this->location() != BUILTINS_LOCATION)
7095 const Package* package = this->named_object_->package();
7096 if (package != NULL)
7097 ret->append(package->name());
7098 else
7099 ret->append(gogo->package_name());
7100 ret->push_back('.');
7102 if (this->in_function_ != NULL)
7104 ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
7105 ret->push_back('$');
7107 ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
7110 // Get the mangled name.
7112 void
7113 Named_type::do_mangled_name(Gogo* gogo, std::string* ret) const
7115 Named_object* no = this->named_object_;
7116 std::string name;
7117 if (this->location() == BUILTINS_LOCATION)
7118 gcc_assert(this->in_function_ == NULL);
7119 else
7121 const std::string& unique_prefix(no->package() == NULL
7122 ? gogo->unique_prefix()
7123 : no->package()->unique_prefix());
7124 const std::string& package_name(no->package() == NULL
7125 ? gogo->package_name()
7126 : no->package()->name());
7127 name = unique_prefix;
7128 name.append(1, '.');
7129 name.append(package_name);
7130 name.append(1, '.');
7131 if (this->in_function_ != NULL)
7133 name.append(Gogo::unpack_hidden_name(this->in_function_->name()));
7134 name.append(1, '$');
7137 name.append(Gogo::unpack_hidden_name(no->name()));
7138 char buf[20];
7139 snprintf(buf, sizeof buf, "N%u_", static_cast<unsigned int>(name.length()));
7140 ret->append(buf);
7141 ret->append(name);
7144 // Export the type. This is called to export a global type.
7146 void
7147 Named_type::export_named_type(Export* exp, const std::string&) const
7149 // We don't need to write the name of the type here, because it will
7150 // be written by Export::write_type anyhow.
7151 exp->write_c_string("type ");
7152 exp->write_type(this);
7153 exp->write_c_string(";\n");
7156 // Import a named type.
7158 void
7159 Named_type::import_named_type(Import* imp, Named_type** ptype)
7161 imp->require_c_string("type ");
7162 Type *type = imp->read_type();
7163 *ptype = type->named_type();
7164 gcc_assert(*ptype != NULL);
7165 imp->require_c_string(";\n");
7168 // Export the type when it is referenced by another type. In this
7169 // case Export::export_type will already have issued the name.
7171 void
7172 Named_type::do_export(Export* exp) const
7174 exp->write_type(this->type_);
7176 // To save space, we only export the methods directly attached to
7177 // this type.
7178 Bindings* methods = this->local_methods_;
7179 if (methods == NULL)
7180 return;
7182 exp->write_c_string("\n");
7183 for (Bindings::const_definitions_iterator p = methods->begin_definitions();
7184 p != methods->end_definitions();
7185 ++p)
7187 exp->write_c_string(" ");
7188 (*p)->export_named_object(exp);
7191 for (Bindings::const_declarations_iterator p = methods->begin_declarations();
7192 p != methods->end_declarations();
7193 ++p)
7195 if (p->second->is_function_declaration())
7197 exp->write_c_string(" ");
7198 p->second->export_named_object(exp);
7203 // Make a named type.
7205 Named_type*
7206 Type::make_named_type(Named_object* named_object, Type* type,
7207 source_location location)
7209 return new Named_type(named_object, type, location);
7212 // Finalize the methods for TYPE. It will be a named type or a struct
7213 // type. This sets *ALL_METHODS to the list of methods, and builds
7214 // all required stubs.
7216 void
7217 Type::finalize_methods(Gogo* gogo, const Type* type, source_location location,
7218 Methods** all_methods)
7220 *all_methods = NULL;
7221 Types_seen types_seen;
7222 Type::add_methods_for_type(type, NULL, 0, false, false, &types_seen,
7223 all_methods);
7224 Type::build_stub_methods(gogo, type, *all_methods, location);
7227 // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
7228 // build up the struct field indexes as we go. DEPTH is the depth of
7229 // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
7230 // adding these methods for an anonymous field with pointer type.
7231 // NEEDS_STUB_METHOD is true if we need to use a stub method which
7232 // calls the real method. TYPES_SEEN is used to avoid infinite
7233 // recursion.
7235 void
7236 Type::add_methods_for_type(const Type* type,
7237 const Method::Field_indexes* field_indexes,
7238 unsigned int depth,
7239 bool is_embedded_pointer,
7240 bool needs_stub_method,
7241 Types_seen* types_seen,
7242 Methods** methods)
7244 // Pointer types may not have methods.
7245 if (type->points_to() != NULL)
7246 return;
7248 const Named_type* nt = type->named_type();
7249 if (nt != NULL)
7251 std::pair<Types_seen::iterator, bool> ins = types_seen->insert(nt);
7252 if (!ins.second)
7253 return;
7256 if (nt != NULL)
7257 Type::add_local_methods_for_type(nt, field_indexes, depth,
7258 is_embedded_pointer, needs_stub_method,
7259 methods);
7261 Type::add_embedded_methods_for_type(type, field_indexes, depth,
7262 is_embedded_pointer, needs_stub_method,
7263 types_seen, methods);
7265 // If we are called with depth > 0, then we are looking at an
7266 // anonymous field of a struct. If such a field has interface type,
7267 // then we need to add the interface methods. We don't want to add
7268 // them when depth == 0, because we will already handle them
7269 // following the usual rules for an interface type.
7270 if (depth > 0)
7271 Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
7274 // Add the local methods for the named type NT to *METHODS. The
7275 // parameters are as for add_methods_to_type.
7277 void
7278 Type::add_local_methods_for_type(const Named_type* nt,
7279 const Method::Field_indexes* field_indexes,
7280 unsigned int depth,
7281 bool is_embedded_pointer,
7282 bool needs_stub_method,
7283 Methods** methods)
7285 const Bindings* local_methods = nt->local_methods();
7286 if (local_methods == NULL)
7287 return;
7289 if (*methods == NULL)
7290 *methods = new Methods();
7292 for (Bindings::const_declarations_iterator p =
7293 local_methods->begin_declarations();
7294 p != local_methods->end_declarations();
7295 ++p)
7297 Named_object* no = p->second;
7298 bool is_value_method = (is_embedded_pointer
7299 || !Type::method_expects_pointer(no));
7300 Method* m = new Named_method(no, field_indexes, depth, is_value_method,
7301 (needs_stub_method
7302 || (depth > 0 && is_value_method)));
7303 if (!(*methods)->insert(no->name(), m))
7304 delete m;
7308 // Add the embedded methods for TYPE to *METHODS. These are the
7309 // methods attached to anonymous fields. The parameters are as for
7310 // add_methods_to_type.
7312 void
7313 Type::add_embedded_methods_for_type(const Type* type,
7314 const Method::Field_indexes* field_indexes,
7315 unsigned int depth,
7316 bool is_embedded_pointer,
7317 bool needs_stub_method,
7318 Types_seen* types_seen,
7319 Methods** methods)
7321 // Look for anonymous fields in TYPE. TYPE has fields if it is a
7322 // struct.
7323 const Struct_type* st = type->struct_type();
7324 if (st == NULL)
7325 return;
7327 const Struct_field_list* fields = st->fields();
7328 if (fields == NULL)
7329 return;
7331 unsigned int i = 0;
7332 for (Struct_field_list::const_iterator pf = fields->begin();
7333 pf != fields->end();
7334 ++pf, ++i)
7336 if (!pf->is_anonymous())
7337 continue;
7339 Type* ftype = pf->type();
7340 bool is_pointer = false;
7341 if (ftype->points_to() != NULL)
7343 ftype = ftype->points_to();
7344 is_pointer = true;
7346 Named_type* fnt = ftype->named_type();
7347 if (fnt == NULL)
7349 // This is an error, but it will be diagnosed elsewhere.
7350 continue;
7353 Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
7354 sub_field_indexes->next = field_indexes;
7355 sub_field_indexes->field_index = i;
7357 Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
7358 (is_embedded_pointer || is_pointer),
7359 (needs_stub_method
7360 || is_pointer
7361 || i > 0),
7362 types_seen,
7363 methods);
7367 // If TYPE is an interface type, then add its method to *METHODS.
7368 // This is for interface methods attached to an anonymous field. The
7369 // parameters are as for add_methods_for_type.
7371 void
7372 Type::add_interface_methods_for_type(const Type* type,
7373 const Method::Field_indexes* field_indexes,
7374 unsigned int depth,
7375 Methods** methods)
7377 const Interface_type* it = type->interface_type();
7378 if (it == NULL)
7379 return;
7381 const Typed_identifier_list* imethods = it->methods();
7382 if (imethods == NULL)
7383 return;
7385 if (*methods == NULL)
7386 *methods = new Methods();
7388 for (Typed_identifier_list::const_iterator pm = imethods->begin();
7389 pm != imethods->end();
7390 ++pm)
7392 Function_type* fntype = pm->type()->function_type();
7393 gcc_assert(fntype != NULL && !fntype->is_method());
7394 fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
7395 Method* m = new Interface_method(pm->name(), pm->location(), fntype,
7396 field_indexes, depth);
7397 if (!(*methods)->insert(pm->name(), m))
7398 delete m;
7402 // Build stub methods for TYPE as needed. METHODS is the set of
7403 // methods for the type. A stub method may be needed when a type
7404 // inherits a method from an anonymous field. When we need the
7405 // address of the method, as in a type descriptor, we need to build a
7406 // little stub which does the required field dereferences and jumps to
7407 // the real method. LOCATION is the location of the type definition.
7409 void
7410 Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
7411 source_location location)
7413 if (methods == NULL)
7414 return;
7415 for (Methods::const_iterator p = methods->begin();
7416 p != methods->end();
7417 ++p)
7419 Method* m = p->second;
7420 if (m->is_ambiguous() || !m->needs_stub_method())
7421 continue;
7423 const std::string& name(p->first);
7425 // Build a stub method.
7427 const Function_type* fntype = m->type();
7429 static unsigned int counter;
7430 char buf[100];
7431 snprintf(buf, sizeof buf, "$this%u", counter);
7432 ++counter;
7434 Type* receiver_type = const_cast<Type*>(type);
7435 if (!m->is_value_method())
7436 receiver_type = Type::make_pointer_type(receiver_type);
7437 source_location receiver_location = m->receiver_location();
7438 Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
7439 receiver_location);
7441 const Typed_identifier_list* fnparams = fntype->parameters();
7442 Typed_identifier_list* stub_params;
7443 if (fnparams == NULL || fnparams->empty())
7444 stub_params = NULL;
7445 else
7447 // We give each stub parameter a unique name.
7448 stub_params = new Typed_identifier_list();
7449 for (Typed_identifier_list::const_iterator pp = fnparams->begin();
7450 pp != fnparams->end();
7451 ++pp)
7453 char pbuf[100];
7454 snprintf(pbuf, sizeof pbuf, "$p%u", counter);
7455 stub_params->push_back(Typed_identifier(pbuf, pp->type(),
7456 pp->location()));
7457 ++counter;
7461 const Typed_identifier_list* fnresults = fntype->results();
7462 Typed_identifier_list* stub_results;
7463 if (fnresults == NULL || fnresults->empty())
7464 stub_results = NULL;
7465 else
7467 // We create the result parameters without any names, since
7468 // we won't refer to them.
7469 stub_results = new Typed_identifier_list();
7470 for (Typed_identifier_list::const_iterator pr = fnresults->begin();
7471 pr != fnresults->end();
7472 ++pr)
7473 stub_results->push_back(Typed_identifier("", pr->type(),
7474 pr->location()));
7477 Function_type* stub_type = Type::make_function_type(receiver,
7478 stub_params,
7479 stub_results,
7480 fntype->location());
7481 if (fntype->is_varargs())
7482 stub_type->set_is_varargs();
7484 // We only create the function in the package which creates the
7485 // type.
7486 const Package* package;
7487 if (type->named_type() == NULL)
7488 package = NULL;
7489 else
7490 package = type->named_type()->named_object()->package();
7491 Named_object* stub;
7492 if (package != NULL)
7493 stub = Named_object::make_function_declaration(name, package,
7494 stub_type, location);
7495 else
7497 stub = gogo->start_function(name, stub_type, false,
7498 fntype->location());
7499 Type::build_one_stub_method(gogo, m, buf, stub_params,
7500 fntype->is_varargs(), location);
7501 gogo->finish_function(fntype->location());
7504 m->set_stub_object(stub);
7508 // Build a stub method which adjusts the receiver as required to call
7509 // METHOD. RECEIVER_NAME is the name we used for the receiver.
7510 // PARAMS is the list of function parameters.
7512 void
7513 Type::build_one_stub_method(Gogo* gogo, Method* method,
7514 const char* receiver_name,
7515 const Typed_identifier_list* params,
7516 bool is_varargs,
7517 source_location location)
7519 Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
7520 gcc_assert(receiver_object != NULL);
7522 Expression* expr = Expression::make_var_reference(receiver_object, location);
7523 expr = Type::apply_field_indexes(expr, method->field_indexes(), location);
7524 if (expr->type()->points_to() == NULL)
7525 expr = Expression::make_unary(OPERATOR_AND, expr, location);
7527 Expression_list* arguments;
7528 if (params == NULL || params->empty())
7529 arguments = NULL;
7530 else
7532 arguments = new Expression_list();
7533 for (Typed_identifier_list::const_iterator p = params->begin();
7534 p != params->end();
7535 ++p)
7537 Named_object* param = gogo->lookup(p->name(), NULL);
7538 gcc_assert(param != NULL);
7539 Expression* param_ref = Expression::make_var_reference(param,
7540 location);
7541 arguments->push_back(param_ref);
7545 Expression* func = method->bind_method(expr, location);
7546 gcc_assert(func != NULL);
7547 Call_expression* call = Expression::make_call(func, arguments, is_varargs,
7548 location);
7549 size_t count = call->result_count();
7550 if (count == 0)
7551 gogo->add_statement(Statement::make_statement(call));
7552 else
7554 Expression_list* retvals = new Expression_list();
7555 if (count <= 1)
7556 retvals->push_back(call);
7557 else
7559 for (size_t i = 0; i < count; ++i)
7560 retvals->push_back(Expression::make_call_result(call, i));
7562 const Function* function = gogo->current_function()->func_value();
7563 const Typed_identifier_list* results = function->type()->results();
7564 Statement* retstat = Statement::make_return_statement(results, retvals,
7565 location);
7566 gogo->add_statement(retstat);
7570 // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
7571 // in reverse order.
7573 Expression*
7574 Type::apply_field_indexes(Expression* expr,
7575 const Method::Field_indexes* field_indexes,
7576 source_location location)
7578 if (field_indexes == NULL)
7579 return expr;
7580 expr = Type::apply_field_indexes(expr, field_indexes->next, location);
7581 Struct_type* stype = expr->type()->deref()->struct_type();
7582 gcc_assert(stype != NULL
7583 && field_indexes->field_index < stype->field_count());
7584 if (expr->type()->struct_type() == NULL)
7586 gcc_assert(expr->type()->points_to() != NULL);
7587 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
7588 gcc_assert(expr->type()->struct_type() == stype);
7590 return Expression::make_field_reference(expr, field_indexes->field_index,
7591 location);
7594 // Return whether NO is a method for which the receiver is a pointer.
7596 bool
7597 Type::method_expects_pointer(const Named_object* no)
7599 const Function_type *fntype;
7600 if (no->is_function())
7601 fntype = no->func_value()->type();
7602 else if (no->is_function_declaration())
7603 fntype = no->func_declaration_value()->type();
7604 else
7605 gcc_unreachable();
7606 return fntype->receiver()->type()->points_to() != NULL;
7609 // Given a set of methods for a type, METHODS, return the method NAME,
7610 // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
7611 // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
7612 // but is ambiguous (and return NULL).
7614 Method*
7615 Type::method_function(const Methods* methods, const std::string& name,
7616 bool* is_ambiguous)
7618 if (is_ambiguous != NULL)
7619 *is_ambiguous = false;
7620 if (methods == NULL)
7621 return NULL;
7622 Methods::const_iterator p = methods->find(name);
7623 if (p == methods->end())
7624 return NULL;
7625 Method* m = p->second;
7626 if (m->is_ambiguous())
7628 if (is_ambiguous != NULL)
7629 *is_ambiguous = true;
7630 return NULL;
7632 return m;
7635 // Look for field or method NAME for TYPE. Return an Expression for
7636 // the field or method bound to EXPR. If there is no such field or
7637 // method, give an appropriate error and return an error expression.
7639 Expression*
7640 Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
7641 const std::string& name,
7642 source_location location)
7644 if (type->deref()->is_error_type())
7645 return Expression::make_error(location);
7647 const Named_type* nt = type->named_type();
7648 if (nt == NULL)
7649 nt = type->deref()->named_type();
7650 const Struct_type* st = type->deref()->struct_type();
7651 const Interface_type* it = type->deref()->interface_type();
7653 // If this is a pointer to a pointer, then it is possible that the
7654 // pointed-to type has methods.
7655 if (nt == NULL
7656 && st == NULL
7657 && it == NULL
7658 && type->points_to() != NULL
7659 && type->points_to()->points_to() != NULL)
7661 expr = Expression::make_unary(OPERATOR_MULT, expr, location);
7662 type = type->points_to();
7663 nt = type->points_to()->named_type();
7664 st = type->points_to()->struct_type();
7665 it = type->points_to()->interface_type();
7668 bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
7669 || expr->is_addressable());
7670 std::vector<const Named_type*> seen;
7671 bool is_method = false;
7672 bool found_pointer_method = false;
7673 std::string ambig1;
7674 std::string ambig2;
7675 if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
7676 &seen, NULL, &is_method,
7677 &found_pointer_method, &ambig1, &ambig2))
7679 Expression* ret;
7680 if (!is_method)
7682 gcc_assert(st != NULL);
7683 if (type->struct_type() == NULL)
7685 gcc_assert(type->points_to() != NULL);
7686 expr = Expression::make_unary(OPERATOR_MULT, expr,
7687 location);
7688 gcc_assert(expr->type()->struct_type() == st);
7690 ret = st->field_reference(expr, name, location);
7692 else if (it != NULL && it->find_method(name) != NULL)
7693 ret = Expression::make_interface_field_reference(expr, name,
7694 location);
7695 else
7697 Method* m;
7698 if (nt != NULL)
7699 m = nt->method_function(name, NULL);
7700 else if (st != NULL)
7701 m = st->method_function(name, NULL);
7702 else
7703 gcc_unreachable();
7704 gcc_assert(m != NULL);
7705 if (!m->is_value_method() && expr->type()->points_to() == NULL)
7706 expr = Expression::make_unary(OPERATOR_AND, expr, location);
7707 ret = m->bind_method(expr, location);
7709 gcc_assert(ret != NULL);
7710 return ret;
7712 else
7714 if (!ambig1.empty())
7715 error_at(location, "%qs is ambiguous via %qs and %qs",
7716 Gogo::message_name(name).c_str(),
7717 Gogo::message_name(ambig1).c_str(),
7718 Gogo::message_name(ambig2).c_str());
7719 else if (found_pointer_method)
7720 error_at(location, "method requires a pointer");
7721 else if (nt == NULL && st == NULL && it == NULL)
7722 error_at(location,
7723 ("reference to field %qs in object which "
7724 "has no fields or methods"),
7725 Gogo::message_name(name).c_str());
7726 else
7728 bool is_unexported;
7729 if (!Gogo::is_hidden_name(name))
7730 is_unexported = false;
7731 else
7733 std::string unpacked = Gogo::unpack_hidden_name(name);
7734 seen.clear();
7735 is_unexported = Type::is_unexported_field_or_method(gogo, type,
7736 unpacked,
7737 &seen);
7739 if (is_unexported)
7740 error_at(location, "reference to unexported field or method %qs",
7741 Gogo::message_name(name).c_str());
7742 else
7743 error_at(location, "reference to undefined field or method %qs",
7744 Gogo::message_name(name).c_str());
7746 return Expression::make_error(location);
7750 // Look in TYPE for a field or method named NAME, return true if one
7751 // is found. This looks through embedded anonymous fields and handles
7752 // ambiguity. If a method is found, sets *IS_METHOD to true;
7753 // otherwise, if a field is found, set it to false. If
7754 // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
7755 // whose address can not be taken. SEEN is used to avoid infinite
7756 // recursion on invalid types.
7758 // When returning false, this sets *FOUND_POINTER_METHOD if we found a
7759 // method we couldn't use because it requires a pointer. LEVEL is
7760 // used for recursive calls, and can be NULL for a non-recursive call.
7761 // When this function returns false because it finds that the name is
7762 // ambiguous, it will store a path to the ambiguous names in *AMBIG1
7763 // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
7764 // will be unchanged.
7766 // This function just returns whether or not there is a field or
7767 // method, and whether it is a field or method. It doesn't build an
7768 // expression to refer to it. If it is a method, we then look in the
7769 // list of all methods for the type. If it is a field, the search has
7770 // to be done again, looking only for fields, and building up the
7771 // expression as we go.
7773 bool
7774 Type::find_field_or_method(const Type* type,
7775 const std::string& name,
7776 bool receiver_can_be_pointer,
7777 std::vector<const Named_type*>* seen,
7778 int* level,
7779 bool* is_method,
7780 bool* found_pointer_method,
7781 std::string* ambig1,
7782 std::string* ambig2)
7784 // Named types can have locally defined methods.
7785 const Named_type* nt = type->named_type();
7786 if (nt == NULL && type->points_to() != NULL)
7787 nt = type->points_to()->named_type();
7788 if (nt != NULL)
7790 Named_object* no = nt->find_local_method(name);
7791 if (no != NULL)
7793 if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
7795 *is_method = true;
7796 return true;
7799 // Record that we have found a pointer method in order to
7800 // give a better error message if we don't find anything
7801 // else.
7802 *found_pointer_method = true;
7805 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
7806 p != seen->end();
7807 ++p)
7809 if (*p == nt)
7811 // We've already seen this type when searching for methods.
7812 return false;
7817 // Interface types can have methods.
7818 const Interface_type* it = type->deref()->interface_type();
7819 if (it != NULL && it->find_method(name) != NULL)
7821 *is_method = true;
7822 return true;
7825 // Struct types can have fields. They can also inherit fields and
7826 // methods from anonymous fields.
7827 const Struct_type* st = type->deref()->struct_type();
7828 if (st == NULL)
7829 return false;
7830 const Struct_field_list* fields = st->fields();
7831 if (fields == NULL)
7832 return false;
7834 if (nt != NULL)
7835 seen->push_back(nt);
7837 int found_level = 0;
7838 bool found_is_method = false;
7839 std::string found_ambig1;
7840 std::string found_ambig2;
7841 const Struct_field* found_parent = NULL;
7842 for (Struct_field_list::const_iterator pf = fields->begin();
7843 pf != fields->end();
7844 ++pf)
7846 if (pf->field_name() == name)
7848 *is_method = false;
7849 if (nt != NULL)
7850 seen->pop_back();
7851 return true;
7854 if (!pf->is_anonymous())
7855 continue;
7857 if (pf->type()->deref()->is_error_type()
7858 || pf->type()->deref()->is_undefined())
7859 continue;
7861 Named_type* fnt = pf->type()->deref()->named_type();
7862 gcc_assert(fnt != NULL);
7864 int sublevel = level == NULL ? 1 : *level + 1;
7865 bool sub_is_method;
7866 std::string subambig1;
7867 std::string subambig2;
7868 bool subfound = Type::find_field_or_method(fnt,
7869 name,
7870 receiver_can_be_pointer,
7871 seen,
7872 &sublevel,
7873 &sub_is_method,
7874 found_pointer_method,
7875 &subambig1,
7876 &subambig2);
7877 if (!subfound)
7879 if (!subambig1.empty())
7881 // The name was found via this field, but is ambiguous.
7882 // if the ambiguity is lower or at the same level as
7883 // anything else we have already found, then we want to
7884 // pass the ambiguity back to the caller.
7885 if (found_level == 0 || sublevel <= found_level)
7887 found_ambig1 = pf->field_name() + '.' + subambig1;
7888 found_ambig2 = pf->field_name() + '.' + subambig2;
7889 found_level = sublevel;
7893 else
7895 // The name was found via this field. Use the level to see
7896 // if we want to use this one, or whether it introduces an
7897 // ambiguity.
7898 if (found_level == 0 || sublevel < found_level)
7900 found_level = sublevel;
7901 found_is_method = sub_is_method;
7902 found_ambig1.clear();
7903 found_ambig2.clear();
7904 found_parent = &*pf;
7906 else if (sublevel > found_level)
7908 else if (found_ambig1.empty())
7910 // We found an ambiguity.
7911 gcc_assert(found_parent != NULL);
7912 found_ambig1 = found_parent->field_name();
7913 found_ambig2 = pf->field_name();
7915 else
7917 // We found an ambiguity, but we already know of one.
7918 // Just report the earlier one.
7923 // Here if we didn't find anything FOUND_LEVEL is 0. If we found
7924 // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
7925 // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
7926 // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
7928 if (nt != NULL)
7929 seen->pop_back();
7931 if (found_level == 0)
7932 return false;
7933 else if (!found_ambig1.empty())
7935 gcc_assert(!found_ambig1.empty());
7936 ambig1->assign(found_ambig1);
7937 ambig2->assign(found_ambig2);
7938 if (level != NULL)
7939 *level = found_level;
7940 return false;
7942 else
7944 if (level != NULL)
7945 *level = found_level;
7946 *is_method = found_is_method;
7947 return true;
7951 // Return whether NAME is an unexported field or method for TYPE.
7953 bool
7954 Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
7955 const std::string& name,
7956 std::vector<const Named_type*>* seen)
7958 type = type->deref();
7960 const Named_type* nt = type->named_type();
7961 if (nt != NULL)
7963 if (nt->is_unexported_local_method(gogo, name))
7964 return true;
7966 for (std::vector<const Named_type*>::const_iterator p = seen->begin();
7967 p != seen->end();
7968 ++p)
7970 if (*p == nt)
7972 // We've already seen this type.
7973 return false;
7978 const Interface_type* it = type->interface_type();
7979 if (it != NULL && it->is_unexported_method(gogo, name))
7980 return true;
7982 const Struct_type* st = type->struct_type();
7983 if (st != NULL && st->is_unexported_local_field(gogo, name))
7984 return true;
7986 if (st == NULL)
7987 return false;
7989 const Struct_field_list* fields = st->fields();
7990 if (fields == NULL)
7991 return false;
7993 if (nt != NULL)
7994 seen->push_back(nt);
7996 for (Struct_field_list::const_iterator pf = fields->begin();
7997 pf != fields->end();
7998 ++pf)
8000 if (pf->is_anonymous()
8001 && (!pf->type()->deref()->is_error_type()
8002 && !pf->type()->deref()->is_undefined()))
8004 Named_type* subtype = pf->type()->deref()->named_type();
8005 gcc_assert(subtype != NULL);
8006 if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
8008 if (nt != NULL)
8009 seen->pop_back();
8010 return true;
8015 if (nt != NULL)
8016 seen->pop_back();
8018 return false;
8021 // Class Forward_declaration.
8023 Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
8024 : Type(TYPE_FORWARD),
8025 named_object_(named_object->resolve()), warned_(false)
8027 gcc_assert(this->named_object_->is_unknown()
8028 || this->named_object_->is_type_declaration());
8031 // Return the named object.
8033 Named_object*
8034 Forward_declaration_type::named_object()
8036 return this->named_object_->resolve();
8039 const Named_object*
8040 Forward_declaration_type::named_object() const
8042 return this->named_object_->resolve();
8045 // Return the name of the forward declared type.
8047 const std::string&
8048 Forward_declaration_type::name() const
8050 return this->named_object()->name();
8053 // Warn about a use of a type which has been declared but not defined.
8055 void
8056 Forward_declaration_type::warn() const
8058 Named_object* no = this->named_object_->resolve();
8059 if (no->is_unknown())
8061 // The name was not defined anywhere.
8062 if (!this->warned_)
8064 error_at(this->named_object_->location(),
8065 "use of undefined type %qs",
8066 no->message_name().c_str());
8067 this->warned_ = true;
8070 else if (no->is_type_declaration())
8072 // The name was seen as a type, but the type was never defined.
8073 if (no->type_declaration_value()->using_type())
8075 error_at(this->named_object_->location(),
8076 "use of undefined type %qs",
8077 no->message_name().c_str());
8078 this->warned_ = true;
8081 else
8083 // The name was defined, but not as a type.
8084 if (!this->warned_)
8086 error_at(this->named_object_->location(), "expected type");
8087 this->warned_ = true;
8092 // Get the base type of a declaration. This gives an error if the
8093 // type has not yet been defined.
8095 Type*
8096 Forward_declaration_type::real_type()
8098 if (this->is_defined())
8099 return this->named_object()->type_value();
8100 else
8102 this->warn();
8103 return Type::make_error_type();
8107 const Type*
8108 Forward_declaration_type::real_type() const
8110 if (this->is_defined())
8111 return this->named_object()->type_value();
8112 else
8114 this->warn();
8115 return Type::make_error_type();
8119 // Return whether the base type is defined.
8121 bool
8122 Forward_declaration_type::is_defined() const
8124 return this->named_object()->is_type();
8127 // Add a method. This is used when methods are defined before the
8128 // type.
8130 Named_object*
8131 Forward_declaration_type::add_method(const std::string& name,
8132 Function* function)
8134 Named_object* no = this->named_object();
8135 if (no->is_unknown())
8136 no->declare_as_type();
8137 return no->type_declaration_value()->add_method(name, function);
8140 // Add a method declaration. This is used when methods are declared
8141 // before the type.
8143 Named_object*
8144 Forward_declaration_type::add_method_declaration(const std::string& name,
8145 Function_type* type,
8146 source_location location)
8148 Named_object* no = this->named_object();
8149 if (no->is_unknown())
8150 no->declare_as_type();
8151 Type_declaration* td = no->type_declaration_value();
8152 return td->add_method_declaration(name, type, location);
8155 // Traversal.
8158 Forward_declaration_type::do_traverse(Traverse* traverse)
8160 if (this->is_defined()
8161 && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
8162 return TRAVERSE_EXIT;
8163 return TRAVERSE_CONTINUE;
8166 // Get a tree for the type.
8168 tree
8169 Forward_declaration_type::do_get_tree(Gogo* gogo)
8171 if (this->is_defined())
8172 return Type::get_named_type_tree(gogo, this->real_type());
8174 if (this->warned_)
8175 return error_mark_node;
8177 // We represent an undefined type as a struct with no fields. That
8178 // should work fine for the middle-end, since the same case can
8179 // arise in C.
8180 Named_object* no = this->named_object();
8181 tree type_tree = make_node(RECORD_TYPE);
8182 tree id = no->get_id(gogo);
8183 tree decl = build_decl(no->location(), TYPE_DECL, id, type_tree);
8184 TYPE_NAME(type_tree) = decl;
8185 layout_type(type_tree);
8186 return type_tree;
8189 // Build a type descriptor for a forwarded type.
8191 Expression*
8192 Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8194 if (!this->is_defined())
8195 return Expression::make_nil(BUILTINS_LOCATION);
8196 else
8198 Type* t = this->real_type();
8199 if (name != NULL)
8200 return this->named_type_descriptor(gogo, t, name);
8201 else
8202 return Expression::make_type_descriptor(t, BUILTINS_LOCATION);
8206 // The reflection string.
8208 void
8209 Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
8211 this->append_reflection(this->real_type(), gogo, ret);
8214 // The mangled name.
8216 void
8217 Forward_declaration_type::do_mangled_name(Gogo* gogo, std::string* ret) const
8219 if (this->is_defined())
8220 this->append_mangled_name(this->real_type(), gogo, ret);
8221 else
8223 const Named_object* no = this->named_object();
8224 std::string name;
8225 if (no->package() == NULL)
8226 name = gogo->package_name();
8227 else
8228 name = no->package()->name();
8229 name += '.';
8230 name += Gogo::unpack_hidden_name(no->name());
8231 char buf[20];
8232 snprintf(buf, sizeof buf, "N%u_",
8233 static_cast<unsigned int>(name.length()));
8234 ret->append(buf);
8235 ret->append(name);
8239 // Export a forward declaration. This can happen when a defined type
8240 // refers to a type which is only declared (and is presumably defined
8241 // in some other file in the same package).
8243 void
8244 Forward_declaration_type::do_export(Export*) const
8246 // If there is a base type, that should be exported instead of this.
8247 gcc_assert(!this->is_defined());
8249 // We don't output anything.
8252 // Make a forward declaration.
8254 Type*
8255 Type::make_forward_declaration(Named_object* named_object)
8257 return new Forward_declaration_type(named_object);
8260 // Class Typed_identifier_list.
8262 // Sort the entries by name.
8264 struct Typed_identifier_list_sort
8266 public:
8267 bool
8268 operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
8269 { return t1.name() < t2.name(); }
8272 void
8273 Typed_identifier_list::sort_by_name()
8275 std::sort(this->entries_.begin(), this->entries_.end(),
8276 Typed_identifier_list_sort());
8279 // Traverse types.
8282 Typed_identifier_list::traverse(Traverse* traverse)
8284 for (Typed_identifier_list::const_iterator p = this->begin();
8285 p != this->end();
8286 ++p)
8288 if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
8289 return TRAVERSE_EXIT;
8291 return TRAVERSE_CONTINUE;
8294 // Copy the list.
8296 Typed_identifier_list*
8297 Typed_identifier_list::copy() const
8299 Typed_identifier_list* ret = new Typed_identifier_list();
8300 for (Typed_identifier_list::const_iterator p = this->begin();
8301 p != this->end();
8302 ++p)
8303 ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));
8304 return ret;