compiler: track //go:nointerface in export data
[official-gcc.git] / gcc / go / gofrontend / types.cc
blob40eccfcadcaf8b70a7b04abf55d7adf1a6c4faf9
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 <ostream>
11 #include "go-c.h"
12 #include "gogo.h"
13 #include "go-diagnostics.h"
14 #include "go-encode-id.h"
15 #include "operator.h"
16 #include "expressions.h"
17 #include "statements.h"
18 #include "export.h"
19 #include "import.h"
20 #include "backend.h"
21 #include "types.h"
23 // Forward declarations so that we don't have to make types.h #include
24 // backend.h.
26 static void
27 get_backend_struct_fields(Gogo* gogo, const Struct_field_list* fields,
28 bool use_placeholder,
29 std::vector<Backend::Btyped_identifier>* bfields);
31 static void
32 get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
33 std::vector<Backend::Btyped_identifier>* bfields);
35 static void
36 get_backend_interface_fields(Gogo* gogo, Interface_type* type,
37 bool use_placeholder,
38 std::vector<Backend::Btyped_identifier>* bfields);
40 // Class Type.
42 Type::Type(Type_classification classification)
43 : classification_(classification), btype_(NULL), type_descriptor_var_(NULL),
44 gc_symbol_var_(NULL)
48 Type::~Type()
52 // Get the base type for a type--skip names and forward declarations.
54 Type*
55 Type::base()
57 switch (this->classification_)
59 case TYPE_NAMED:
60 return this->named_type()->named_base();
61 case TYPE_FORWARD:
62 return this->forward_declaration_type()->real_type()->base();
63 default:
64 return this;
68 const Type*
69 Type::base() const
71 switch (this->classification_)
73 case TYPE_NAMED:
74 return this->named_type()->named_base();
75 case TYPE_FORWARD:
76 return this->forward_declaration_type()->real_type()->base();
77 default:
78 return this;
82 // Skip defined forward declarations.
84 Type*
85 Type::forwarded()
87 Type* t = this;
88 Forward_declaration_type* ftype = t->forward_declaration_type();
89 while (ftype != NULL && ftype->is_defined())
91 t = ftype->real_type();
92 ftype = t->forward_declaration_type();
94 return t;
97 const Type*
98 Type::forwarded() const
100 const Type* t = this;
101 const Forward_declaration_type* ftype = t->forward_declaration_type();
102 while (ftype != NULL && ftype->is_defined())
104 t = ftype->real_type();
105 ftype = t->forward_declaration_type();
107 return t;
110 // Skip alias definitions.
112 Type*
113 Type::unalias()
115 Type* t = this->forwarded();
116 Named_type* nt = t->named_type();
117 while (nt != NULL && nt->is_alias())
119 t = nt->real_type()->forwarded();
120 nt = t->named_type();
122 return t;
125 const Type*
126 Type::unalias() const
128 const Type* t = this->forwarded();
129 const Named_type* nt = t->named_type();
130 while (nt != NULL && nt->is_alias())
132 t = nt->real_type()->forwarded();
133 nt = t->named_type();
135 return t;
138 // If this is a named type, return it. Otherwise, return NULL.
140 Named_type*
141 Type::named_type()
143 return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
146 const Named_type*
147 Type::named_type() const
149 return this->forwarded()->convert_no_base<const Named_type, TYPE_NAMED>();
152 // Return true if this type is not defined.
154 bool
155 Type::is_undefined() const
157 return this->forwarded()->forward_declaration_type() != NULL;
160 // Return true if this is a basic type: a type which is not composed
161 // of other types, and is not void.
163 bool
164 Type::is_basic_type() const
166 switch (this->classification_)
168 case TYPE_INTEGER:
169 case TYPE_FLOAT:
170 case TYPE_COMPLEX:
171 case TYPE_BOOLEAN:
172 case TYPE_STRING:
173 case TYPE_NIL:
174 return true;
176 case TYPE_ERROR:
177 case TYPE_VOID:
178 case TYPE_FUNCTION:
179 case TYPE_POINTER:
180 case TYPE_STRUCT:
181 case TYPE_ARRAY:
182 case TYPE_MAP:
183 case TYPE_CHANNEL:
184 case TYPE_INTERFACE:
185 return false;
187 case TYPE_NAMED:
188 case TYPE_FORWARD:
189 return this->base()->is_basic_type();
191 default:
192 go_unreachable();
196 // Return true if this is an abstract type.
198 bool
199 Type::is_abstract() const
201 switch (this->classification())
203 case TYPE_INTEGER:
204 return this->integer_type()->is_abstract();
205 case TYPE_FLOAT:
206 return this->float_type()->is_abstract();
207 case TYPE_COMPLEX:
208 return this->complex_type()->is_abstract();
209 case TYPE_STRING:
210 return this->is_abstract_string_type();
211 case TYPE_BOOLEAN:
212 return this->is_abstract_boolean_type();
213 default:
214 return false;
218 // Return a non-abstract version of an abstract type.
220 Type*
221 Type::make_non_abstract_type()
223 go_assert(this->is_abstract());
224 switch (this->classification())
226 case TYPE_INTEGER:
227 if (this->integer_type()->is_rune())
228 return Type::lookup_integer_type("int32");
229 else
230 return Type::lookup_integer_type("int");
231 case TYPE_FLOAT:
232 return Type::lookup_float_type("float64");
233 case TYPE_COMPLEX:
234 return Type::lookup_complex_type("complex128");
235 case TYPE_STRING:
236 return Type::lookup_string_type();
237 case TYPE_BOOLEAN:
238 return Type::lookup_bool_type();
239 default:
240 go_unreachable();
244 // Return true if this is an error type. Don't give an error if we
245 // try to dereference an undefined forwarding type, as this is called
246 // in the parser when the type may legitimately be undefined.
248 bool
249 Type::is_error_type() const
251 const Type* t = this->forwarded();
252 // Note that we return false for an undefined forward type.
253 switch (t->classification_)
255 case TYPE_ERROR:
256 return true;
257 case TYPE_NAMED:
258 return t->named_type()->is_named_error_type();
259 default:
260 return false;
264 // If this is a pointer type, return the type to which it points.
265 // Otherwise, return NULL.
267 Type*
268 Type::points_to() const
270 const Pointer_type* ptype = this->convert<const Pointer_type,
271 TYPE_POINTER>();
272 return ptype == NULL ? NULL : ptype->points_to();
275 // Return whether this is a slice type.
277 bool
278 Type::is_slice_type() const
280 return this->array_type() != NULL && this->array_type()->length() == NULL;
283 // Return whether this is the predeclared constant nil being used as a
284 // type.
286 bool
287 Type::is_nil_constant_as_type() const
289 const Type* t = this->forwarded();
290 if (t->forward_declaration_type() != NULL)
292 const Named_object* no = t->forward_declaration_type()->named_object();
293 if (no->is_unknown())
294 no = no->unknown_value()->real_named_object();
295 if (no != NULL
296 && no->is_const()
297 && no->const_value()->expr()->is_nil_expression())
298 return true;
300 return false;
303 // Traverse a type.
306 Type::traverse(Type* type, Traverse* traverse)
308 go_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
309 || (traverse->traverse_mask()
310 & Traverse::traverse_expressions) != 0);
311 if (traverse->remember_type(type))
313 // We have already traversed this type.
314 return TRAVERSE_CONTINUE;
316 if ((traverse->traverse_mask() & Traverse::traverse_types) != 0)
318 int t = traverse->type(type);
319 if (t == TRAVERSE_EXIT)
320 return TRAVERSE_EXIT;
321 else if (t == TRAVERSE_SKIP_COMPONENTS)
322 return TRAVERSE_CONTINUE;
324 // An array type has an expression which we need to traverse if
325 // traverse_expressions is set.
326 if (type->do_traverse(traverse) == TRAVERSE_EXIT)
327 return TRAVERSE_EXIT;
328 return TRAVERSE_CONTINUE;
331 // Default implementation for do_traverse for child class.
334 Type::do_traverse(Traverse*)
336 return TRAVERSE_CONTINUE;
339 // Return whether two types are identical. If ERRORS_ARE_IDENTICAL,
340 // then return true for all erroneous types; this is used to avoid
341 // cascading errors. If REASON is not NULL, optionally set *REASON to
342 // the reason the types are not identical.
344 bool
345 Type::are_identical(const Type* t1, const Type* t2, bool errors_are_identical,
346 std::string* reason)
348 return Type::are_identical_cmp_tags(t1, t2, COMPARE_TAGS,
349 errors_are_identical, reason);
352 // Like are_identical, but with a CMP_TAGS parameter.
354 bool
355 Type::are_identical_cmp_tags(const Type* t1, const Type* t2, Cmp_tags cmp_tags,
356 bool errors_are_identical, std::string* reason)
358 if (t1 == NULL || t2 == NULL)
360 // Something is wrong.
361 return errors_are_identical ? true : t1 == t2;
364 // Skip defined forward declarations. Ignore aliases.
365 t1 = t1->unalias();
366 t2 = t2->unalias();
368 if (t1 == t2)
369 return true;
371 // An undefined forward declaration is an error.
372 if (t1->forward_declaration_type() != NULL
373 || t2->forward_declaration_type() != NULL)
374 return errors_are_identical;
376 // Avoid cascading errors with error types.
377 if (t1->is_error_type() || t2->is_error_type())
379 if (errors_are_identical)
380 return true;
381 return t1->is_error_type() && t2->is_error_type();
384 // Get a good reason for the sink type. Note that the sink type on
385 // the left hand side of an assignment is handled in are_assignable.
386 if (t1->is_sink_type() || t2->is_sink_type())
388 if (reason != NULL)
389 *reason = "invalid use of _";
390 return false;
393 // A named type is only identical to itself.
394 if (t1->named_type() != NULL || t2->named_type() != NULL)
395 return false;
397 // Check type shapes.
398 if (t1->classification() != t2->classification())
399 return false;
401 switch (t1->classification())
403 case TYPE_VOID:
404 case TYPE_BOOLEAN:
405 case TYPE_STRING:
406 case TYPE_NIL:
407 // These types are always identical.
408 return true;
410 case TYPE_INTEGER:
411 return t1->integer_type()->is_identical(t2->integer_type());
413 case TYPE_FLOAT:
414 return t1->float_type()->is_identical(t2->float_type());
416 case TYPE_COMPLEX:
417 return t1->complex_type()->is_identical(t2->complex_type());
419 case TYPE_FUNCTION:
420 return t1->function_type()->is_identical(t2->function_type(),
421 false,
422 cmp_tags,
423 errors_are_identical,
424 reason);
426 case TYPE_POINTER:
427 return Type::are_identical_cmp_tags(t1->points_to(), t2->points_to(),
428 cmp_tags, errors_are_identical,
429 reason);
431 case TYPE_STRUCT:
432 return t1->struct_type()->is_identical(t2->struct_type(), cmp_tags,
433 errors_are_identical);
435 case TYPE_ARRAY:
436 return t1->array_type()->is_identical(t2->array_type(), cmp_tags,
437 errors_are_identical);
439 case TYPE_MAP:
440 return t1->map_type()->is_identical(t2->map_type(), cmp_tags,
441 errors_are_identical);
443 case TYPE_CHANNEL:
444 return t1->channel_type()->is_identical(t2->channel_type(), cmp_tags,
445 errors_are_identical);
447 case TYPE_INTERFACE:
448 return t1->interface_type()->is_identical(t2->interface_type(), cmp_tags,
449 errors_are_identical);
451 case TYPE_CALL_MULTIPLE_RESULT:
452 if (reason != NULL)
453 *reason = "invalid use of multiple-value function call";
454 return false;
456 default:
457 go_unreachable();
461 // Return true if it's OK to have a binary operation with types LHS
462 // and RHS. This is not used for shifts or comparisons.
464 bool
465 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
467 if (Type::are_identical(lhs, rhs, true, NULL))
468 return true;
470 // A constant of abstract bool type may be mixed with any bool type.
471 if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
472 || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
473 return true;
475 // A constant of abstract string type may be mixed with any string
476 // type.
477 if ((rhs->is_abstract_string_type() && lhs->is_string_type())
478 || (lhs->is_abstract_string_type() && rhs->is_string_type()))
479 return true;
481 lhs = lhs->base();
482 rhs = rhs->base();
484 // A constant of abstract integer, float, or complex type may be
485 // mixed with an integer, float, or complex type.
486 if ((rhs->is_abstract()
487 && (rhs->integer_type() != NULL
488 || rhs->float_type() != NULL
489 || rhs->complex_type() != NULL)
490 && (lhs->integer_type() != NULL
491 || lhs->float_type() != NULL
492 || lhs->complex_type() != NULL))
493 || (lhs->is_abstract()
494 && (lhs->integer_type() != NULL
495 || lhs->float_type() != NULL
496 || lhs->complex_type() != NULL)
497 && (rhs->integer_type() != NULL
498 || rhs->float_type() != NULL
499 || rhs->complex_type() != NULL)))
500 return true;
502 // The nil type may be compared to a pointer, an interface type, a
503 // slice type, a channel type, a map type, or a function type.
504 if (lhs->is_nil_type()
505 && (rhs->points_to() != NULL
506 || rhs->interface_type() != NULL
507 || rhs->is_slice_type()
508 || rhs->map_type() != NULL
509 || rhs->channel_type() != NULL
510 || rhs->function_type() != NULL))
511 return true;
512 if (rhs->is_nil_type()
513 && (lhs->points_to() != NULL
514 || lhs->interface_type() != NULL
515 || lhs->is_slice_type()
516 || lhs->map_type() != NULL
517 || lhs->channel_type() != NULL
518 || lhs->function_type() != NULL))
519 return true;
521 return false;
524 // Return true if a value with type T1 may be compared with a value of
525 // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
527 bool
528 Type::are_compatible_for_comparison(bool is_equality_op, const Type *t1,
529 const Type *t2, std::string *reason)
531 if (t1 != t2
532 && !Type::are_assignable(t1, t2, NULL)
533 && !Type::are_assignable(t2, t1, NULL))
535 if (reason != NULL)
536 *reason = "incompatible types in binary expression";
537 return false;
540 if (!is_equality_op)
542 if (t1->integer_type() == NULL
543 && t1->float_type() == NULL
544 && !t1->is_string_type())
546 if (reason != NULL)
547 *reason = _("invalid comparison of non-ordered type");
548 return false;
551 else if (t1->is_slice_type()
552 || t1->map_type() != NULL
553 || t1->function_type() != NULL
554 || t2->is_slice_type()
555 || t2->map_type() != NULL
556 || t2->function_type() != NULL)
558 if (!t1->is_nil_type() && !t2->is_nil_type())
560 if (reason != NULL)
562 if (t1->is_slice_type() || t2->is_slice_type())
563 *reason = _("slice can only be compared to nil");
564 else if (t1->map_type() != NULL || t2->map_type() != NULL)
565 *reason = _("map can only be compared to nil");
566 else
567 *reason = _("func can only be compared to nil");
569 // Match 6g error messages.
570 if (t1->interface_type() != NULL || t2->interface_type() != NULL)
572 char buf[200];
573 snprintf(buf, sizeof buf, _("invalid operation (%s)"),
574 reason->c_str());
575 *reason = buf;
578 return false;
581 else
583 if (!t1->is_boolean_type()
584 && t1->integer_type() == NULL
585 && t1->float_type() == NULL
586 && t1->complex_type() == NULL
587 && !t1->is_string_type()
588 && t1->points_to() == NULL
589 && t1->channel_type() == NULL
590 && t1->interface_type() == NULL
591 && t1->struct_type() == NULL
592 && t1->array_type() == NULL
593 && !t1->is_nil_type())
595 if (reason != NULL)
596 *reason = _("invalid comparison of non-comparable type");
597 return false;
600 if (t1->named_type() != NULL)
601 return t1->named_type()->named_type_is_comparable(reason);
602 else if (t2->named_type() != NULL)
603 return t2->named_type()->named_type_is_comparable(reason);
604 else if (t1->struct_type() != NULL)
606 if (t1->struct_type()->is_struct_incomparable())
608 if (reason != NULL)
609 *reason = _("invalid comparison of generated struct");
610 return false;
612 const Struct_field_list* fields = t1->struct_type()->fields();
613 for (Struct_field_list::const_iterator p = fields->begin();
614 p != fields->end();
615 ++p)
617 if (!p->type()->is_comparable())
619 if (reason != NULL)
620 *reason = _("invalid comparison of non-comparable struct");
621 return false;
625 else if (t1->array_type() != NULL)
627 if (t1->array_type()->is_array_incomparable())
629 if (reason != NULL)
630 *reason = _("invalid comparison of generated array");
631 return false;
633 if (t1->array_type()->length()->is_nil_expression()
634 || !t1->array_type()->element_type()->is_comparable())
636 if (reason != NULL)
637 *reason = _("invalid comparison of non-comparable array");
638 return false;
643 return true;
646 // Return true if a value with type RHS may be assigned to a variable
647 // with type LHS. If REASON is not NULL, set *REASON to the reason
648 // the types are not assignable.
650 bool
651 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
653 // Do some checks first. Make sure the types are defined.
654 if (rhs != NULL && !rhs->is_undefined())
656 if (rhs->is_void_type())
658 if (reason != NULL)
659 *reason = "non-value used as value";
660 return false;
662 if (rhs->is_call_multiple_result_type())
664 if (reason != NULL)
665 reason->assign(_("multiple-value function call in "
666 "single-value context"));
667 return false;
671 // Any value may be assigned to the blank identifier.
672 if (lhs != NULL
673 && !lhs->is_undefined()
674 && lhs->is_sink_type())
675 return true;
677 // Identical types are assignable.
678 if (Type::are_identical(lhs, rhs, true, reason))
679 return true;
681 // The types are assignable if they have identical underlying types
682 // and either LHS or RHS is not a named type.
683 if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
684 || (rhs->named_type() != NULL && lhs->named_type() == NULL))
685 && Type::are_identical(lhs->base(), rhs->base(), true, reason))
686 return true;
688 // The types are assignable if LHS is an interface type and RHS
689 // implements the required methods.
690 const Interface_type* lhs_interface_type = lhs->interface_type();
691 if (lhs_interface_type != NULL)
693 if (lhs_interface_type->implements_interface(rhs, reason))
694 return true;
695 const Interface_type* rhs_interface_type = rhs->interface_type();
696 if (rhs_interface_type != NULL
697 && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
698 reason))
699 return true;
702 // The type are assignable if RHS is a bidirectional channel type,
703 // LHS is a channel type, they have identical element types, and
704 // either LHS or RHS is not a named type.
705 if (lhs->channel_type() != NULL
706 && rhs->channel_type() != NULL
707 && rhs->channel_type()->may_send()
708 && rhs->channel_type()->may_receive()
709 && (lhs->named_type() == NULL || rhs->named_type() == NULL)
710 && Type::are_identical(lhs->channel_type()->element_type(),
711 rhs->channel_type()->element_type(),
712 true,
713 reason))
714 return true;
716 // The nil type may be assigned to a pointer, function, slice, map,
717 // channel, or interface type.
718 if (rhs->is_nil_type()
719 && (lhs->points_to() != NULL
720 || lhs->function_type() != NULL
721 || lhs->is_slice_type()
722 || lhs->map_type() != NULL
723 || lhs->channel_type() != NULL
724 || lhs->interface_type() != NULL))
725 return true;
727 // An untyped numeric constant may be assigned to a numeric type if
728 // it is representable in that type.
729 if ((rhs->is_abstract()
730 && (rhs->integer_type() != NULL
731 || rhs->float_type() != NULL
732 || rhs->complex_type() != NULL))
733 && (lhs->integer_type() != NULL
734 || lhs->float_type() != NULL
735 || lhs->complex_type() != NULL))
736 return true;
738 // Give some better error messages.
739 if (reason != NULL && reason->empty())
741 if (rhs->interface_type() != NULL)
742 reason->assign(_("need explicit conversion"));
743 else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
745 size_t len = (lhs->named_type()->name().length()
746 + rhs->named_type()->name().length()
747 + 100);
748 char* buf = new char[len];
749 snprintf(buf, len, _("cannot use type %s as type %s"),
750 rhs->named_type()->message_name().c_str(),
751 lhs->named_type()->message_name().c_str());
752 reason->assign(buf);
753 delete[] buf;
757 return false;
760 // Return true if a value with type RHS may be converted to type LHS.
761 // If REASON is not NULL, set *REASON to the reason the types are not
762 // convertible.
764 bool
765 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
767 // The types are convertible if they are assignable.
768 if (Type::are_assignable(lhs, rhs, reason))
769 return true;
771 // A pointer to a regular type may not be converted to a pointer to
772 // a type that may not live in the heap, except when converting from
773 // unsafe.Pointer.
774 if (lhs->points_to() != NULL
775 && rhs->points_to() != NULL
776 && !lhs->points_to()->in_heap()
777 && rhs->points_to()->in_heap()
778 && !rhs->is_unsafe_pointer_type())
780 if (reason != NULL)
781 reason->assign(_("conversion from normal type to notinheap type"));
782 return false;
785 // The types are convertible if they have identical underlying
786 // types, ignoring struct field tags.
787 if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
788 && Type::are_identical_cmp_tags(lhs->base(), rhs->base(), IGNORE_TAGS,
789 true, reason))
790 return true;
792 // The types are convertible if they are both unnamed pointer types
793 // and their pointer base types have identical underlying types,
794 // ignoring struct field tags.
795 if (lhs->named_type() == NULL
796 && rhs->named_type() == NULL
797 && lhs->points_to() != NULL
798 && rhs->points_to() != NULL
799 && (lhs->points_to()->named_type() != NULL
800 || rhs->points_to()->named_type() != NULL)
801 && Type::are_identical_cmp_tags(lhs->points_to()->base(),
802 rhs->points_to()->base(),
803 IGNORE_TAGS,
804 true,
805 reason))
806 return true;
808 // Integer and floating point types are convertible to each other.
809 if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
810 && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
811 return true;
813 // Complex types are convertible to each other.
814 if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
815 return true;
817 // An integer, or []byte, or []rune, may be converted to a string.
818 if (lhs->is_string_type())
820 if (rhs->integer_type() != NULL)
821 return true;
822 if (rhs->is_slice_type())
824 const Type* e = rhs->array_type()->element_type()->forwarded();
825 if (e->integer_type() != NULL
826 && (e->integer_type()->is_byte()
827 || e->integer_type()->is_rune()))
828 return true;
832 // A string may be converted to []byte or []rune.
833 if (rhs->is_string_type() && lhs->is_slice_type())
835 const Type* e = lhs->array_type()->element_type()->forwarded();
836 if (e->integer_type() != NULL
837 && (e->integer_type()->is_byte() || e->integer_type()->is_rune()))
838 return true;
841 // An unsafe.Pointer type may be converted to any pointer type or to
842 // a type whose underlying type is uintptr, and vice-versa.
843 if (lhs->is_unsafe_pointer_type()
844 && (rhs->points_to() != NULL
845 || (rhs->integer_type() != NULL
846 && rhs->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
847 return true;
848 if (rhs->is_unsafe_pointer_type()
849 && (lhs->points_to() != NULL
850 || (lhs->integer_type() != NULL
851 && lhs->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
852 return true;
854 // Give a better error message.
855 if (reason != NULL)
857 if (reason->empty())
858 *reason = "invalid type conversion";
859 else
861 std::string s = "invalid type conversion (";
862 s += *reason;
863 s += ')';
864 *reason = s;
868 return false;
871 // Copy expressions if it may change the size.
873 // The only type that has an expression is an array type. The only
874 // types whose size can be changed by the size of an array type are an
875 // array type itself, or a struct type with an array field.
876 Type*
877 Type::copy_expressions()
879 // This is run during parsing, so types may not be valid yet.
880 // We only have to worry about array type literals.
881 switch (this->classification_)
883 default:
884 return this;
886 case TYPE_ARRAY:
888 Array_type* at = this->array_type();
889 if (at->length() == NULL)
890 return this;
891 Expression* len = at->length()->copy();
892 if (at->length() == len)
893 return this;
894 return Type::make_array_type(at->element_type(), len);
897 case TYPE_STRUCT:
899 Struct_type* st = this->struct_type();
900 const Struct_field_list* sfl = st->fields();
901 if (sfl == NULL)
902 return this;
903 bool changed = false;
904 Struct_field_list *nsfl = new Struct_field_list();
905 for (Struct_field_list::const_iterator pf = sfl->begin();
906 pf != sfl->end();
907 ++pf)
909 Type* ft = pf->type()->copy_expressions();
910 Struct_field nf(Typed_identifier((pf->is_anonymous()
911 ? ""
912 : pf->field_name()),
914 pf->location()));
915 if (pf->has_tag())
916 nf.set_tag(pf->tag());
917 nsfl->push_back(nf);
918 if (ft != pf->type())
919 changed = true;
921 if (!changed)
923 delete(nsfl);
924 return this;
926 return Type::make_struct_type(nsfl, st->location());
930 go_unreachable();
933 // Return a hash code for the type to be used for method lookup.
935 unsigned int
936 Type::hash_for_method(Gogo* gogo) const
938 if (this->named_type() != NULL && this->named_type()->is_alias())
939 return this->named_type()->real_type()->hash_for_method(gogo);
940 unsigned int ret = 0;
941 if (this->classification_ != TYPE_FORWARD)
942 ret += this->classification_;
943 return ret + this->do_hash_for_method(gogo);
946 // Default implementation of do_hash_for_method. This is appropriate
947 // for types with no subfields.
949 unsigned int
950 Type::do_hash_for_method(Gogo*) const
952 return 0;
955 // Return a hash code for a string, given a starting hash.
957 unsigned int
958 Type::hash_string(const std::string& s, unsigned int h)
960 const char* p = s.data();
961 size_t len = s.length();
962 for (; len > 0; --len)
964 h ^= *p++;
965 h*= 16777619;
967 return h;
970 // A hash table mapping unnamed types to the backend representation of
971 // those types.
973 Type::Type_btypes Type::type_btypes;
975 // Return the backend representation for this type.
977 Btype*
978 Type::get_backend(Gogo* gogo)
980 if (this->btype_ != NULL)
981 return this->btype_;
983 if (this->forward_declaration_type() != NULL
984 || this->named_type() != NULL)
985 return this->get_btype_without_hash(gogo);
987 if (this->is_error_type())
988 return gogo->backend()->error_type();
990 // To avoid confusing the backend, translate all identical Go types
991 // to the same backend representation. We use a hash table to do
992 // that. There is no need to use the hash table for named types, as
993 // named types are only identical to themselves.
995 std::pair<Type*, Type_btype_entry> val;
996 val.first = this;
997 val.second.btype = NULL;
998 val.second.is_placeholder = false;
999 std::pair<Type_btypes::iterator, bool> ins =
1000 Type::type_btypes.insert(val);
1001 if (!ins.second && ins.first->second.btype != NULL)
1003 // Note that GOGO can be NULL here, but only when the GCC
1004 // middle-end is asking for a frontend type. That will only
1005 // happen for simple types, which should never require
1006 // placeholders.
1007 if (!ins.first->second.is_placeholder)
1008 this->btype_ = ins.first->second.btype;
1009 else if (gogo->named_types_are_converted())
1011 this->finish_backend(gogo, ins.first->second.btype);
1012 ins.first->second.is_placeholder = false;
1015 return ins.first->second.btype;
1018 Btype* bt = this->get_btype_without_hash(gogo);
1020 if (ins.first->second.btype == NULL)
1022 ins.first->second.btype = bt;
1023 ins.first->second.is_placeholder = false;
1025 else
1027 // We have already created a backend representation for this
1028 // type. This can happen when an unnamed type is defined using
1029 // a named type which in turns uses an identical unnamed type.
1030 // Use the representation we created earlier and ignore the one we just
1031 // built.
1032 if (this->btype_ == bt)
1033 this->btype_ = ins.first->second.btype;
1034 bt = ins.first->second.btype;
1037 return bt;
1040 // Return the backend representation for a type without looking in the
1041 // hash table for identical types. This is used for named types,
1042 // since a named type is never identical to any other type.
1044 Btype*
1045 Type::get_btype_without_hash(Gogo* gogo)
1047 if (this->btype_ == NULL)
1049 Btype* bt = this->do_get_backend(gogo);
1051 // For a recursive function or pointer type, we will temporarily
1052 // return a circular pointer type during the recursion. We
1053 // don't want to record that for a forwarding type, as it may
1054 // confuse us later.
1055 if (this->forward_declaration_type() != NULL
1056 && gogo->backend()->is_circular_pointer_type(bt))
1057 return bt;
1059 if (gogo == NULL || !gogo->named_types_are_converted())
1060 return bt;
1062 this->btype_ = bt;
1064 return this->btype_;
1067 // Get the backend representation of a type without forcing the
1068 // creation of the backend representation of all supporting types.
1069 // This will return a backend type that has the correct size but may
1070 // be incomplete. E.g., a pointer will just be a placeholder pointer,
1071 // and will not contain the final representation of the type to which
1072 // it points. This is used while converting all named types to the
1073 // backend representation, to avoid problems with indirect references
1074 // to types which are not yet complete. When this is called, the
1075 // sizes of all direct references (e.g., a struct field) should be
1076 // known, but the sizes of indirect references (e.g., the type to
1077 // which a pointer points) may not.
1079 Btype*
1080 Type::get_backend_placeholder(Gogo* gogo)
1082 if (gogo->named_types_are_converted())
1083 return this->get_backend(gogo);
1084 if (this->btype_ != NULL)
1085 return this->btype_;
1087 Btype* bt;
1088 switch (this->classification_)
1090 case TYPE_ERROR:
1091 case TYPE_VOID:
1092 case TYPE_BOOLEAN:
1093 case TYPE_INTEGER:
1094 case TYPE_FLOAT:
1095 case TYPE_COMPLEX:
1096 case TYPE_STRING:
1097 case TYPE_NIL:
1098 // These are simple types that can just be created directly.
1099 return this->get_backend(gogo);
1101 case TYPE_MAP:
1102 case TYPE_CHANNEL:
1103 // All maps and channels have the same backend representation.
1104 return this->get_backend(gogo);
1106 case TYPE_NAMED:
1107 case TYPE_FORWARD:
1108 // Named types keep track of their own dependencies and manage
1109 // their own placeholders.
1110 return this->get_backend(gogo);
1112 case TYPE_INTERFACE:
1113 if (this->interface_type()->is_empty())
1114 return Interface_type::get_backend_empty_interface_type(gogo);
1115 break;
1117 default:
1118 break;
1121 std::pair<Type*, Type_btype_entry> val;
1122 val.first = this;
1123 val.second.btype = NULL;
1124 val.second.is_placeholder = false;
1125 std::pair<Type_btypes::iterator, bool> ins =
1126 Type::type_btypes.insert(val);
1127 if (!ins.second && ins.first->second.btype != NULL)
1128 return ins.first->second.btype;
1130 switch (this->classification_)
1132 case TYPE_FUNCTION:
1134 // A Go function type is a pointer to a struct type.
1135 Location loc = this->function_type()->location();
1136 bt = gogo->backend()->placeholder_pointer_type("", loc, false);
1138 break;
1140 case TYPE_POINTER:
1142 Location loc = Linemap::unknown_location();
1143 bt = gogo->backend()->placeholder_pointer_type("", loc, false);
1144 Pointer_type* pt = this->convert<Pointer_type, TYPE_POINTER>();
1145 Type::placeholder_pointers.push_back(pt);
1147 break;
1149 case TYPE_STRUCT:
1150 // We don't have to make the struct itself be a placeholder. We
1151 // are promised that we know the sizes of the struct fields.
1152 // But we may have to use a placeholder for any particular
1153 // struct field.
1155 std::vector<Backend::Btyped_identifier> bfields;
1156 get_backend_struct_fields(gogo, this->struct_type()->fields(),
1157 true, &bfields);
1158 bt = gogo->backend()->struct_type(bfields);
1160 break;
1162 case TYPE_ARRAY:
1163 if (this->is_slice_type())
1165 std::vector<Backend::Btyped_identifier> bfields;
1166 get_backend_slice_fields(gogo, this->array_type(), true, &bfields);
1167 bt = gogo->backend()->struct_type(bfields);
1169 else
1171 Btype* element = this->array_type()->get_backend_element(gogo, true);
1172 Bexpression* len = this->array_type()->get_backend_length(gogo);
1173 bt = gogo->backend()->array_type(element, len);
1175 break;
1177 case TYPE_INTERFACE:
1179 go_assert(!this->interface_type()->is_empty());
1180 std::vector<Backend::Btyped_identifier> bfields;
1181 get_backend_interface_fields(gogo, this->interface_type(), true,
1182 &bfields);
1183 bt = gogo->backend()->struct_type(bfields);
1185 break;
1187 case TYPE_SINK:
1188 case TYPE_CALL_MULTIPLE_RESULT:
1189 /* Note that various classifications were handled in the earlier
1190 switch. */
1191 default:
1192 go_unreachable();
1195 if (ins.first->second.btype == NULL)
1197 ins.first->second.btype = bt;
1198 ins.first->second.is_placeholder = true;
1200 else
1202 // A placeholder for this type got created along the way. Use
1203 // that one and ignore the one we just built.
1204 bt = ins.first->second.btype;
1207 return bt;
1210 // Complete the backend representation. This is called for a type
1211 // using a placeholder type.
1213 void
1214 Type::finish_backend(Gogo* gogo, Btype *placeholder)
1216 switch (this->classification_)
1218 case TYPE_ERROR:
1219 case TYPE_VOID:
1220 case TYPE_BOOLEAN:
1221 case TYPE_INTEGER:
1222 case TYPE_FLOAT:
1223 case TYPE_COMPLEX:
1224 case TYPE_STRING:
1225 case TYPE_NIL:
1226 go_unreachable();
1228 case TYPE_FUNCTION:
1230 Btype* bt = this->do_get_backend(gogo);
1231 if (!gogo->backend()->set_placeholder_pointer_type(placeholder, bt))
1232 go_assert(saw_errors());
1234 break;
1236 case TYPE_POINTER:
1238 Btype* bt = this->do_get_backend(gogo);
1239 if (!gogo->backend()->set_placeholder_pointer_type(placeholder, bt))
1240 go_assert(saw_errors());
1242 break;
1244 case TYPE_STRUCT:
1245 // The struct type itself is done, but we have to make sure that
1246 // all the field types are converted.
1247 this->struct_type()->finish_backend_fields(gogo);
1248 break;
1250 case TYPE_ARRAY:
1251 // The array type itself is done, but make sure the element type
1252 // is converted.
1253 this->array_type()->finish_backend_element(gogo);
1254 break;
1256 case TYPE_MAP:
1257 case TYPE_CHANNEL:
1258 go_unreachable();
1260 case TYPE_INTERFACE:
1261 // The interface type itself is done, but make sure the method
1262 // types are converted.
1263 this->interface_type()->finish_backend_methods(gogo);
1264 break;
1266 case TYPE_NAMED:
1267 case TYPE_FORWARD:
1268 go_unreachable();
1270 case TYPE_SINK:
1271 case TYPE_CALL_MULTIPLE_RESULT:
1272 default:
1273 go_unreachable();
1276 this->btype_ = placeholder;
1279 // Return a pointer to the type descriptor for this type.
1281 Bexpression*
1282 Type::type_descriptor_pointer(Gogo* gogo, Location location)
1284 Type* t = this->unalias();
1285 if (t->type_descriptor_var_ == NULL)
1287 t->make_type_descriptor_var(gogo);
1288 go_assert(t->type_descriptor_var_ != NULL);
1290 Bexpression* var_expr =
1291 gogo->backend()->var_expression(t->type_descriptor_var_, location);
1292 Bexpression* var_addr =
1293 gogo->backend()->address_expression(var_expr, location);
1294 Type* td_type = Type::make_type_descriptor_type();
1295 Btype* td_btype = td_type->get_backend(gogo);
1296 Btype* ptd_btype = gogo->backend()->pointer_type(td_btype);
1297 return gogo->backend()->convert_expression(ptd_btype, var_addr, location);
1300 // A mapping from unnamed types to type descriptor variables.
1302 Type::Type_descriptor_vars Type::type_descriptor_vars;
1304 // Build the type descriptor for this type.
1306 void
1307 Type::make_type_descriptor_var(Gogo* gogo)
1309 go_assert(this->type_descriptor_var_ == NULL);
1311 Named_type* nt = this->named_type();
1313 // We can have multiple instances of unnamed types, but we only want
1314 // to emit the type descriptor once. We use a hash table. This is
1315 // not necessary for named types, as they are unique, and we store
1316 // the type descriptor in the type itself.
1317 Bvariable** phash = NULL;
1318 if (nt == NULL)
1320 Bvariable* bvnull = NULL;
1321 std::pair<Type_descriptor_vars::iterator, bool> ins =
1322 Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
1323 if (!ins.second)
1325 // We've already built a type descriptor for this type.
1326 this->type_descriptor_var_ = ins.first->second;
1327 return;
1329 phash = &ins.first->second;
1332 // The type descriptor symbol for the unsafe.Pointer type is defined in
1333 // libgo/go-unsafe-pointer.c, so we just return a reference to that
1334 // symbol if necessary.
1335 if (this->is_unsafe_pointer_type())
1337 Location bloc = Linemap::predeclared_location();
1339 Type* td_type = Type::make_type_descriptor_type();
1340 Btype* td_btype = td_type->get_backend(gogo);
1341 std::string name = gogo->type_descriptor_name(this, nt);
1342 std::string asm_name(go_selectively_encode_id(name));
1343 this->type_descriptor_var_ =
1344 gogo->backend()->immutable_struct_reference(name, asm_name,
1345 td_btype,
1346 bloc);
1348 if (phash != NULL)
1349 *phash = this->type_descriptor_var_;
1350 return;
1353 std::string var_name = gogo->type_descriptor_name(this, nt);
1355 // Build the contents of the type descriptor.
1356 Expression* initializer = this->do_type_descriptor(gogo, NULL);
1358 Btype* initializer_btype = initializer->type()->get_backend(gogo);
1360 Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
1362 const Package* dummy;
1363 if (this->type_descriptor_defined_elsewhere(nt, &dummy))
1365 std::string asm_name(go_selectively_encode_id(var_name));
1366 this->type_descriptor_var_ =
1367 gogo->backend()->immutable_struct_reference(var_name, asm_name,
1368 initializer_btype,
1369 loc);
1370 if (phash != NULL)
1371 *phash = this->type_descriptor_var_;
1372 return;
1375 // See if this type descriptor can appear in multiple packages.
1376 bool is_common = false;
1377 if (nt != NULL)
1379 // We create the descriptor for a builtin type whenever we need
1380 // it.
1381 is_common = nt->is_builtin();
1383 else
1385 // This is an unnamed type. The descriptor could be defined in
1386 // any package where it is needed, and the linker will pick one
1387 // descriptor to keep.
1388 is_common = true;
1391 // We are going to build the type descriptor in this package. We
1392 // must create the variable before we convert the initializer to the
1393 // backend representation, because the initializer may refer to the
1394 // type descriptor of this type. By setting type_descriptor_var_ we
1395 // ensure that type_descriptor_pointer will work if called while
1396 // converting INITIALIZER.
1398 std::string asm_name(go_selectively_encode_id(var_name));
1399 this->type_descriptor_var_ =
1400 gogo->backend()->immutable_struct(var_name, asm_name, false, is_common,
1401 initializer_btype, loc);
1402 if (phash != NULL)
1403 *phash = this->type_descriptor_var_;
1405 Translate_context context(gogo, NULL, NULL, NULL);
1406 context.set_is_const();
1407 Bexpression* binitializer = initializer->get_backend(&context);
1409 gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
1410 var_name, false, is_common,
1411 initializer_btype, loc,
1412 binitializer);
1415 // Return true if this type descriptor is defined in a different
1416 // package. If this returns true it sets *PACKAGE to the package.
1418 bool
1419 Type::type_descriptor_defined_elsewhere(Named_type* nt,
1420 const Package** package)
1422 if (nt != NULL)
1424 if (nt->named_object()->package() != NULL)
1426 // This is a named type defined in a different package. The
1427 // type descriptor should be defined in that package.
1428 *package = nt->named_object()->package();
1429 return true;
1432 else
1434 if (this->points_to() != NULL
1435 && this->points_to()->named_type() != NULL
1436 && this->points_to()->named_type()->named_object()->package() != NULL)
1438 // This is an unnamed pointer to a named type defined in a
1439 // different package. The descriptor should be defined in
1440 // that package.
1441 *package = this->points_to()->named_type()->named_object()->package();
1442 return true;
1445 return false;
1448 // Return a composite literal for a type descriptor.
1450 Expression*
1451 Type::type_descriptor(Gogo* gogo, Type* type)
1453 return type->do_type_descriptor(gogo, NULL);
1456 // Return a composite literal for a type descriptor with a name.
1458 Expression*
1459 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
1461 go_assert(name != NULL && type->named_type() != name);
1462 return type->do_type_descriptor(gogo, name);
1465 // Make a builtin struct type from a list of fields. The fields are
1466 // pairs of a name and a type.
1468 Struct_type*
1469 Type::make_builtin_struct_type(int nfields, ...)
1471 va_list ap;
1472 va_start(ap, nfields);
1474 Location bloc = Linemap::predeclared_location();
1475 Struct_field_list* sfl = new Struct_field_list();
1476 for (int i = 0; i < nfields; i++)
1478 const char* field_name = va_arg(ap, const char *);
1479 Type* type = va_arg(ap, Type*);
1480 sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
1483 va_end(ap);
1485 Struct_type* ret = Type::make_struct_type(sfl, bloc);
1486 ret->set_is_struct_incomparable();
1487 return ret;
1490 // A list of builtin named types.
1492 std::vector<Named_type*> Type::named_builtin_types;
1494 // Make a builtin named type.
1496 Named_type*
1497 Type::make_builtin_named_type(const char* name, Type* type)
1499 Location bloc = Linemap::predeclared_location();
1500 Named_object* no = Named_object::make_type(name, NULL, type, bloc);
1501 Named_type* ret = no->type_value();
1502 Type::named_builtin_types.push_back(ret);
1503 return ret;
1506 // Convert the named builtin types.
1508 void
1509 Type::convert_builtin_named_types(Gogo* gogo)
1511 for (std::vector<Named_type*>::const_iterator p =
1512 Type::named_builtin_types.begin();
1513 p != Type::named_builtin_types.end();
1514 ++p)
1516 bool r = (*p)->verify();
1517 go_assert(r);
1518 (*p)->convert(gogo);
1522 // Return the type of a type descriptor. We should really tie this to
1523 // runtime.Type rather than copying it. This must match the struct "_type"
1524 // declared in libgo/go/runtime/type.go.
1526 Type*
1527 Type::make_type_descriptor_type()
1529 static Type* ret;
1530 if (ret == NULL)
1532 Location bloc = Linemap::predeclared_location();
1534 Type* uint8_type = Type::lookup_integer_type("uint8");
1535 Type* pointer_uint8_type = Type::make_pointer_type(uint8_type);
1536 Type* uint32_type = Type::lookup_integer_type("uint32");
1537 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1538 Type* string_type = Type::lookup_string_type();
1539 Type* pointer_string_type = Type::make_pointer_type(string_type);
1541 // This is an unnamed version of unsafe.Pointer. Perhaps we
1542 // should use the named version instead, although that would
1543 // require us to create the unsafe package if it has not been
1544 // imported. It probably doesn't matter.
1545 Type* void_type = Type::make_void_type();
1546 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1548 Typed_identifier_list *params = new Typed_identifier_list();
1549 params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
1550 params->push_back(Typed_identifier("seed", uintptr_type, bloc));
1552 Typed_identifier_list* results = new Typed_identifier_list();
1553 results->push_back(Typed_identifier("", uintptr_type, bloc));
1555 Type* hash_fntype = Type::make_function_type(NULL, params, results,
1556 bloc);
1558 params = new Typed_identifier_list();
1559 params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
1560 params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
1562 results = new Typed_identifier_list();
1563 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1565 Type* equal_fntype = Type::make_function_type(NULL, params, results,
1566 bloc);
1568 // Forward declaration for the type descriptor type.
1569 Named_object* named_type_descriptor_type =
1570 Named_object::make_type_declaration("_type", NULL, bloc);
1571 Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1572 Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1574 // The type of a method on a concrete type.
1575 Struct_type* method_type =
1576 Type::make_builtin_struct_type(5,
1577 "name", pointer_string_type,
1578 "pkgPath", pointer_string_type,
1579 "mtyp", pointer_type_descriptor_type,
1580 "typ", pointer_type_descriptor_type,
1581 "tfn", unsafe_pointer_type);
1582 Named_type* named_method_type =
1583 Type::make_builtin_named_type("method", method_type);
1585 // Information for types with a name or methods.
1586 Type* slice_named_method_type =
1587 Type::make_array_type(named_method_type, NULL);
1588 Struct_type* uncommon_type =
1589 Type::make_builtin_struct_type(3,
1590 "name", pointer_string_type,
1591 "pkgPath", pointer_string_type,
1592 "methods", slice_named_method_type);
1593 Named_type* named_uncommon_type =
1594 Type::make_builtin_named_type("uncommonType", uncommon_type);
1596 Type* pointer_uncommon_type =
1597 Type::make_pointer_type(named_uncommon_type);
1599 // The type descriptor type.
1601 Struct_type* type_descriptor_type =
1602 Type::make_builtin_struct_type(12,
1603 "size", uintptr_type,
1604 "ptrdata", uintptr_type,
1605 "hash", uint32_type,
1606 "kind", uint8_type,
1607 "align", uint8_type,
1608 "fieldAlign", uint8_type,
1609 "hashfn", hash_fntype,
1610 "equalfn", equal_fntype,
1611 "gcdata", pointer_uint8_type,
1612 "string", pointer_string_type,
1613 "", pointer_uncommon_type,
1614 "ptrToThis",
1615 pointer_type_descriptor_type);
1617 Named_type* named = Type::make_builtin_named_type("_type",
1618 type_descriptor_type);
1620 named_type_descriptor_type->set_type_value(named);
1622 ret = named;
1625 return ret;
1628 // Make the type of a pointer to a type descriptor as represented in
1629 // Go.
1631 Type*
1632 Type::make_type_descriptor_ptr_type()
1634 static Type* ret;
1635 if (ret == NULL)
1636 ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1637 return ret;
1640 // Return the alignment required by the memequalN function. N is a
1641 // type size: 16, 32, 64, or 128. The memequalN functions are defined
1642 // in libgo/go/runtime/alg.go.
1644 int64_t
1645 Type::memequal_align(Gogo* gogo, int size)
1647 const char* tn;
1648 switch (size)
1650 case 16:
1651 tn = "int16";
1652 break;
1653 case 32:
1654 tn = "int32";
1655 break;
1656 case 64:
1657 tn = "int64";
1658 break;
1659 case 128:
1660 // The code uses [2]int64, which must have the same alignment as
1661 // int64.
1662 tn = "int64";
1663 break;
1664 default:
1665 go_unreachable();
1668 Type* t = Type::lookup_integer_type(tn);
1670 int64_t ret;
1671 if (!t->backend_type_align(gogo, &ret))
1672 go_unreachable();
1673 return ret;
1676 // Return whether this type needs specially built type functions.
1677 // This returns true for types that are comparable and either can not
1678 // use an identity comparison, or are a non-standard size.
1680 bool
1681 Type::needs_specific_type_functions(Gogo* gogo)
1683 Named_type* nt = this->named_type();
1684 if (nt != NULL && nt->is_alias())
1685 return false;
1686 if (!this->is_comparable())
1687 return false;
1688 if (!this->compare_is_identity(gogo))
1689 return true;
1691 // We create a few predeclared types for type descriptors; they are
1692 // really just for the backend and don't need hash or equality
1693 // functions.
1694 if (nt != NULL && Linemap::is_predeclared_location(nt->location()))
1695 return false;
1697 int64_t size, align;
1698 if (!this->backend_type_size(gogo, &size)
1699 || !this->backend_type_align(gogo, &align))
1701 go_assert(saw_errors());
1702 return false;
1704 // This switch matches the one in Type::type_functions.
1705 switch (size)
1707 case 0:
1708 case 1:
1709 case 2:
1710 return align < Type::memequal_align(gogo, 16);
1711 case 4:
1712 return align < Type::memequal_align(gogo, 32);
1713 case 8:
1714 return align < Type::memequal_align(gogo, 64);
1715 case 16:
1716 return align < Type::memequal_align(gogo, 128);
1717 default:
1718 return true;
1722 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1723 // hash code for this type and which compare whether two values of
1724 // this type are equal. If NAME is not NULL it is the name of this
1725 // type. HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1726 // functions, for convenience; they may be NULL.
1728 void
1729 Type::type_functions(Gogo* gogo, Named_type* name, Function_type* hash_fntype,
1730 Function_type* equal_fntype, Named_object** hash_fn,
1731 Named_object** equal_fn)
1733 // If the unaliased type is not a named type, then the type does not
1734 // have a name after all.
1735 if (name != NULL)
1736 name = name->unalias()->named_type();
1738 if (!this->is_comparable())
1740 *hash_fn = NULL;
1741 *equal_fn = NULL;
1742 return;
1745 if (hash_fntype == NULL || equal_fntype == NULL)
1747 Location bloc = Linemap::predeclared_location();
1749 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1750 Type* void_type = Type::make_void_type();
1751 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1753 if (hash_fntype == NULL)
1755 Typed_identifier_list* params = new Typed_identifier_list();
1756 params->push_back(Typed_identifier("key", unsafe_pointer_type,
1757 bloc));
1758 params->push_back(Typed_identifier("seed", uintptr_type, bloc));
1760 Typed_identifier_list* results = new Typed_identifier_list();
1761 results->push_back(Typed_identifier("", uintptr_type, bloc));
1763 hash_fntype = Type::make_function_type(NULL, params, results, bloc);
1765 if (equal_fntype == NULL)
1767 Typed_identifier_list* params = new Typed_identifier_list();
1768 params->push_back(Typed_identifier("key1", unsafe_pointer_type,
1769 bloc));
1770 params->push_back(Typed_identifier("key2", unsafe_pointer_type,
1771 bloc));
1773 Typed_identifier_list* results = new Typed_identifier_list();
1774 results->push_back(Typed_identifier("", Type::lookup_bool_type(),
1775 bloc));
1777 equal_fntype = Type::make_function_type(NULL, params, results, bloc);
1781 const char* hash_fnname;
1782 const char* equal_fnname;
1783 if (this->compare_is_identity(gogo))
1785 int64_t size, align;
1786 if (!this->backend_type_size(gogo, &size)
1787 || !this->backend_type_align(gogo, &align))
1789 go_assert(saw_errors());
1790 return;
1792 bool build_functions = false;
1793 // This switch matches the one in Type::needs_specific_type_functions.
1794 // The alignment tests are because of the memequal functions,
1795 // which assume that the values are aligned as required for an
1796 // integer of that size.
1797 switch (size)
1799 case 0:
1800 hash_fnname = "runtime.memhash0";
1801 equal_fnname = "runtime.memequal0";
1802 break;
1803 case 1:
1804 hash_fnname = "runtime.memhash8";
1805 equal_fnname = "runtime.memequal8";
1806 break;
1807 case 2:
1808 if (align < Type::memequal_align(gogo, 16))
1809 build_functions = true;
1810 else
1812 hash_fnname = "runtime.memhash16";
1813 equal_fnname = "runtime.memequal16";
1815 break;
1816 case 4:
1817 if (align < Type::memequal_align(gogo, 32))
1818 build_functions = true;
1819 else
1821 hash_fnname = "runtime.memhash32";
1822 equal_fnname = "runtime.memequal32";
1824 break;
1825 case 8:
1826 if (align < Type::memequal_align(gogo, 64))
1827 build_functions = true;
1828 else
1830 hash_fnname = "runtime.memhash64";
1831 equal_fnname = "runtime.memequal64";
1833 break;
1834 case 16:
1835 if (align < Type::memequal_align(gogo, 128))
1836 build_functions = true;
1837 else
1839 hash_fnname = "runtime.memhash128";
1840 equal_fnname = "runtime.memequal128";
1842 break;
1843 default:
1844 build_functions = true;
1845 break;
1847 if (build_functions)
1849 // We don't have a built-in function for a type of this size
1850 // and alignment. Build a function to use that calls the
1851 // generic hash/equality functions for identity, passing the size.
1852 this->specific_type_functions(gogo, name, size, hash_fntype,
1853 equal_fntype, hash_fn, equal_fn);
1854 return;
1857 else
1859 switch (this->base()->classification())
1861 case Type::TYPE_ERROR:
1862 case Type::TYPE_VOID:
1863 case Type::TYPE_NIL:
1864 case Type::TYPE_FUNCTION:
1865 case Type::TYPE_MAP:
1866 // For these types is_comparable should have returned false.
1867 go_unreachable();
1869 case Type::TYPE_BOOLEAN:
1870 case Type::TYPE_INTEGER:
1871 case Type::TYPE_POINTER:
1872 case Type::TYPE_CHANNEL:
1873 // For these types compare_is_identity should have returned true.
1874 go_unreachable();
1876 case Type::TYPE_FLOAT:
1877 switch (this->float_type()->bits())
1879 case 32:
1880 hash_fnname = "runtime.f32hash";
1881 equal_fnname = "runtime.f32equal";
1882 break;
1883 case 64:
1884 hash_fnname = "runtime.f64hash";
1885 equal_fnname = "runtime.f64equal";
1886 break;
1887 default:
1888 go_unreachable();
1890 break;
1892 case Type::TYPE_COMPLEX:
1893 switch (this->complex_type()->bits())
1895 case 64:
1896 hash_fnname = "runtime.c64hash";
1897 equal_fnname = "runtime.c64equal";
1898 break;
1899 case 128:
1900 hash_fnname = "runtime.c128hash";
1901 equal_fnname = "runtime.c128equal";
1902 break;
1903 default:
1904 go_unreachable();
1906 break;
1908 case Type::TYPE_STRING:
1909 hash_fnname = "runtime.strhash";
1910 equal_fnname = "runtime.strequal";
1911 break;
1913 case Type::TYPE_STRUCT:
1915 // This is a struct which can not be compared using a
1916 // simple identity function. We need to build a function
1917 // for comparison.
1918 this->specific_type_functions(gogo, name, -1, hash_fntype,
1919 equal_fntype, hash_fn, equal_fn);
1920 return;
1923 case Type::TYPE_ARRAY:
1924 if (this->is_slice_type())
1926 // Type::is_compatible_for_comparison should have
1927 // returned false.
1928 go_unreachable();
1930 else
1932 // This is an array which can not be compared using a
1933 // simple identity function. We need to build a
1934 // function for comparison.
1935 this->specific_type_functions(gogo, name, -1, hash_fntype,
1936 equal_fntype, hash_fn, equal_fn);
1937 return;
1939 break;
1941 case Type::TYPE_INTERFACE:
1942 if (this->interface_type()->is_empty())
1944 hash_fnname = "runtime.nilinterhash";
1945 equal_fnname = "runtime.nilinterequal";
1947 else
1949 hash_fnname = "runtime.interhash";
1950 equal_fnname = "runtime.interequal";
1952 break;
1954 case Type::TYPE_NAMED:
1955 case Type::TYPE_FORWARD:
1956 go_unreachable();
1958 default:
1959 go_unreachable();
1964 Location bloc = Linemap::predeclared_location();
1965 *hash_fn = Named_object::make_function_declaration(hash_fnname, NULL,
1966 hash_fntype, bloc);
1967 (*hash_fn)->func_declaration_value()->set_asm_name(hash_fnname);
1968 *equal_fn = Named_object::make_function_declaration(equal_fnname, NULL,
1969 equal_fntype, bloc);
1970 (*equal_fn)->func_declaration_value()->set_asm_name(equal_fnname);
1973 // A hash table mapping types to the specific hash functions.
1975 Type::Type_functions Type::type_functions_table;
1977 // Handle a type function which is specific to a type: if SIZE == -1,
1978 // this is a struct or array that can not use an identity comparison.
1979 // Otherwise, it is a type that uses an identity comparison but is not
1980 // one of the standard supported sizes.
1982 void
1983 Type::specific_type_functions(Gogo* gogo, Named_type* name, int64_t size,
1984 Function_type* hash_fntype,
1985 Function_type* equal_fntype,
1986 Named_object** hash_fn,
1987 Named_object** equal_fn)
1989 Hash_equal_fn fnull(NULL, NULL);
1990 std::pair<Type*, Hash_equal_fn> val(name != NULL ? name : this, fnull);
1991 std::pair<Type_functions::iterator, bool> ins =
1992 Type::type_functions_table.insert(val);
1993 if (!ins.second)
1995 // We already have functions for this type
1996 *hash_fn = ins.first->second.first;
1997 *equal_fn = ins.first->second.second;
1998 return;
2001 std::string hash_name;
2002 std::string equal_name;
2003 gogo->specific_type_function_names(this, name, &hash_name, &equal_name);
2005 Location bloc = Linemap::predeclared_location();
2007 const Package* package = NULL;
2008 bool is_defined_elsewhere =
2009 this->type_descriptor_defined_elsewhere(name, &package);
2010 if (is_defined_elsewhere)
2012 *hash_fn = Named_object::make_function_declaration(hash_name, package,
2013 hash_fntype, bloc);
2014 *equal_fn = Named_object::make_function_declaration(equal_name, package,
2015 equal_fntype, bloc);
2017 else
2019 *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
2020 *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
2021 bloc);
2024 ins.first->second.first = *hash_fn;
2025 ins.first->second.second = *equal_fn;
2027 if (!is_defined_elsewhere)
2029 if (gogo->in_global_scope())
2030 this->write_specific_type_functions(gogo, name, size, hash_name,
2031 hash_fntype, equal_name,
2032 equal_fntype);
2033 else
2034 gogo->queue_specific_type_function(this, name, size, hash_name,
2035 hash_fntype, equal_name,
2036 equal_fntype);
2040 // Write the hash and equality functions for a type which needs to be
2041 // written specially.
2043 void
2044 Type::write_specific_type_functions(Gogo* gogo, Named_type* name, int64_t size,
2045 const std::string& hash_name,
2046 Function_type* hash_fntype,
2047 const std::string& equal_name,
2048 Function_type* equal_fntype)
2050 Location bloc = Linemap::predeclared_location();
2052 if (gogo->specific_type_functions_are_written())
2054 go_assert(saw_errors());
2055 return;
2058 go_assert(this->is_comparable());
2060 Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
2061 bloc);
2062 hash_fn->func_value()->set_is_type_specific_function();
2063 gogo->start_block(bloc);
2065 if (size != -1)
2066 this->write_identity_hash(gogo, size);
2067 else if (name != NULL && name->real_type()->named_type() != NULL)
2068 this->write_named_hash(gogo, name, hash_fntype, equal_fntype);
2069 else if (this->struct_type() != NULL)
2070 this->struct_type()->write_hash_function(gogo, name, hash_fntype,
2071 equal_fntype);
2072 else if (this->array_type() != NULL)
2073 this->array_type()->write_hash_function(gogo, name, hash_fntype,
2074 equal_fntype);
2075 else
2076 go_unreachable();
2078 Block* b = gogo->finish_block(bloc);
2079 gogo->add_block(b, bloc);
2080 gogo->lower_block(hash_fn, b);
2081 gogo->finish_function(bloc);
2083 Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
2084 false, bloc);
2085 equal_fn->func_value()->set_is_type_specific_function();
2086 gogo->start_block(bloc);
2088 if (size != -1)
2089 this->write_identity_equal(gogo, size);
2090 else if (name != NULL && name->real_type()->named_type() != NULL)
2091 this->write_named_equal(gogo, name);
2092 else if (this->struct_type() != NULL)
2093 this->struct_type()->write_equal_function(gogo, name);
2094 else if (this->array_type() != NULL)
2095 this->array_type()->write_equal_function(gogo, name);
2096 else
2097 go_unreachable();
2099 b = gogo->finish_block(bloc);
2100 gogo->add_block(b, bloc);
2101 gogo->lower_block(equal_fn, b);
2102 gogo->finish_function(bloc);
2104 // Build the function descriptors for the type descriptor to refer to.
2105 hash_fn->func_value()->descriptor(gogo, hash_fn);
2106 equal_fn->func_value()->descriptor(gogo, equal_fn);
2109 // Write a hash function for a type that can use an identity hash but
2110 // is not one of the standard supported sizes. For example, this
2111 // would be used for the type [3]byte. This builds a return statement
2112 // that returns a call to the memhash function, passing the key and
2113 // seed from the function arguments (already constructed before this
2114 // is called), and the constant size.
2116 void
2117 Type::write_identity_hash(Gogo* gogo, int64_t size)
2119 Location bloc = Linemap::predeclared_location();
2121 Type* unsafe_pointer_type = Type::make_pointer_type(Type::make_void_type());
2122 Type* uintptr_type = Type::lookup_integer_type("uintptr");
2124 Typed_identifier_list* params = new Typed_identifier_list();
2125 params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
2126 params->push_back(Typed_identifier("seed", uintptr_type, bloc));
2127 params->push_back(Typed_identifier("size", uintptr_type, bloc));
2129 Typed_identifier_list* results = new Typed_identifier_list();
2130 results->push_back(Typed_identifier("", uintptr_type, bloc));
2132 Function_type* memhash_fntype = Type::make_function_type(NULL, params,
2133 results, bloc);
2135 Named_object* memhash =
2136 Named_object::make_function_declaration("runtime.memhash", NULL,
2137 memhash_fntype, bloc);
2138 memhash->func_declaration_value()->set_asm_name("runtime.memhash");
2140 Named_object* key_arg = gogo->lookup("key", NULL);
2141 go_assert(key_arg != NULL);
2142 Named_object* seed_arg = gogo->lookup("seed", NULL);
2143 go_assert(seed_arg != NULL);
2145 Expression* key_ref = Expression::make_var_reference(key_arg, bloc);
2146 Expression* seed_ref = Expression::make_var_reference(seed_arg, bloc);
2147 Expression* size_arg = Expression::make_integer_int64(size, uintptr_type,
2148 bloc);
2149 Expression_list* args = new Expression_list();
2150 args->push_back(key_ref);
2151 args->push_back(seed_ref);
2152 args->push_back(size_arg);
2153 Expression* func = Expression::make_func_reference(memhash, NULL, bloc);
2154 Expression* call = Expression::make_call(func, args, false, bloc);
2156 Expression_list* vals = new Expression_list();
2157 vals->push_back(call);
2158 Statement* s = Statement::make_return_statement(vals, bloc);
2159 gogo->add_statement(s);
2162 // Write an equality function for a type that can use an identity
2163 // equality comparison but is not one of the standard supported sizes.
2164 // For example, this would be used for the type [3]byte. This builds
2165 // a return statement that returns a call to the memequal function,
2166 // passing the two keys from the function arguments (already
2167 // constructed before this is called), and the constant size.
2169 void
2170 Type::write_identity_equal(Gogo* gogo, int64_t size)
2172 Location bloc = Linemap::predeclared_location();
2174 Type* unsafe_pointer_type = Type::make_pointer_type(Type::make_void_type());
2175 Type* uintptr_type = Type::lookup_integer_type("uintptr");
2177 Typed_identifier_list* params = new Typed_identifier_list();
2178 params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
2179 params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
2180 params->push_back(Typed_identifier("size", uintptr_type, bloc));
2182 Typed_identifier_list* results = new Typed_identifier_list();
2183 results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
2185 Function_type* memequal_fntype = Type::make_function_type(NULL, params,
2186 results, bloc);
2188 Named_object* memequal =
2189 Named_object::make_function_declaration("runtime.memequal", NULL,
2190 memequal_fntype, bloc);
2191 memequal->func_declaration_value()->set_asm_name("runtime.memequal");
2193 Named_object* key1_arg = gogo->lookup("key1", NULL);
2194 go_assert(key1_arg != NULL);
2195 Named_object* key2_arg = gogo->lookup("key2", NULL);
2196 go_assert(key2_arg != NULL);
2198 Expression* key1_ref = Expression::make_var_reference(key1_arg, bloc);
2199 Expression* key2_ref = Expression::make_var_reference(key2_arg, bloc);
2200 Expression* size_arg = Expression::make_integer_int64(size, uintptr_type,
2201 bloc);
2202 Expression_list* args = new Expression_list();
2203 args->push_back(key1_ref);
2204 args->push_back(key2_ref);
2205 args->push_back(size_arg);
2206 Expression* func = Expression::make_func_reference(memequal, NULL, bloc);
2207 Expression* call = Expression::make_call(func, args, false, bloc);
2209 Expression_list* vals = new Expression_list();
2210 vals->push_back(call);
2211 Statement* s = Statement::make_return_statement(vals, bloc);
2212 gogo->add_statement(s);
2215 // Write a hash function that simply calls the hash function for a
2216 // named type. This is used when one named type is defined as
2217 // another. This ensures that this case works when the other named
2218 // type is defined in another package and relies on calling hash
2219 // functions defined only in that package.
2221 void
2222 Type::write_named_hash(Gogo* gogo, Named_type* name,
2223 Function_type* hash_fntype, Function_type* equal_fntype)
2225 Location bloc = Linemap::predeclared_location();
2227 Named_type* base_type = name->real_type()->named_type();
2228 while (base_type->is_alias())
2230 base_type = base_type->real_type()->named_type();
2231 go_assert(base_type != NULL);
2233 go_assert(base_type != NULL);
2235 // The pointer to the type we are going to hash. This is an
2236 // unsafe.Pointer.
2237 Named_object* key_arg = gogo->lookup("key", NULL);
2238 go_assert(key_arg != NULL);
2240 // The seed argument to the hash function.
2241 Named_object* seed_arg = gogo->lookup("seed", NULL);
2242 go_assert(seed_arg != NULL);
2244 Named_object* hash_fn;
2245 Named_object* equal_fn;
2246 name->real_type()->type_functions(gogo, base_type, hash_fntype, equal_fntype,
2247 &hash_fn, &equal_fn);
2249 // Call the hash function for the base type.
2250 Expression* key_ref = Expression::make_var_reference(key_arg, bloc);
2251 Expression* seed_ref = Expression::make_var_reference(seed_arg, bloc);
2252 Expression_list* args = new Expression_list();
2253 args->push_back(key_ref);
2254 args->push_back(seed_ref);
2255 Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
2256 Expression* call = Expression::make_call(func, args, false, bloc);
2258 // Return the hash of the base type.
2259 Expression_list* vals = new Expression_list();
2260 vals->push_back(call);
2261 Statement* s = Statement::make_return_statement(vals, bloc);
2262 gogo->add_statement(s);
2265 // Write an equality function that simply calls the equality function
2266 // for a named type. This is used when one named type is defined as
2267 // another. This ensures that this case works when the other named
2268 // type is defined in another package and relies on calling equality
2269 // functions defined only in that package.
2271 void
2272 Type::write_named_equal(Gogo* gogo, Named_type* name)
2274 Location bloc = Linemap::predeclared_location();
2276 // The pointers to the types we are going to compare. These have
2277 // type unsafe.Pointer.
2278 Named_object* key1_arg = gogo->lookup("key1", NULL);
2279 Named_object* key2_arg = gogo->lookup("key2", NULL);
2280 go_assert(key1_arg != NULL && key2_arg != NULL);
2282 Named_type* base_type = name->real_type()->named_type();
2283 go_assert(base_type != NULL);
2285 // Build temporaries with the base type.
2286 Type* pt = Type::make_pointer_type(base_type);
2288 Expression* ref = Expression::make_var_reference(key1_arg, bloc);
2289 ref = Expression::make_cast(pt, ref, bloc);
2290 Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
2291 gogo->add_statement(p1);
2293 ref = Expression::make_var_reference(key2_arg, bloc);
2294 ref = Expression::make_cast(pt, ref, bloc);
2295 Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
2296 gogo->add_statement(p2);
2298 // Compare the values for equality.
2299 Expression* t1 = Expression::make_temporary_reference(p1, bloc);
2300 t1 = Expression::make_dereference(t1, Expression::NIL_CHECK_NOT_NEEDED, bloc);
2302 Expression* t2 = Expression::make_temporary_reference(p2, bloc);
2303 t2 = Expression::make_dereference(t2, Expression::NIL_CHECK_NOT_NEEDED, bloc);
2305 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, t1, t2, bloc);
2307 // Return the equality comparison.
2308 Expression_list* vals = new Expression_list();
2309 vals->push_back(cond);
2310 Statement* s = Statement::make_return_statement(vals, bloc);
2311 gogo->add_statement(s);
2314 // Return a composite literal for the type descriptor for a plain type
2315 // of kind RUNTIME_TYPE_KIND named NAME.
2317 Expression*
2318 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
2319 Named_type* name, const Methods* methods,
2320 bool only_value_methods)
2322 Location bloc = Linemap::predeclared_location();
2324 Type* td_type = Type::make_type_descriptor_type();
2325 const Struct_field_list* fields = td_type->struct_type()->fields();
2327 Expression_list* vals = new Expression_list();
2328 vals->reserve(12);
2330 if (!this->has_pointer())
2331 runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
2332 if (this->points_to() != NULL)
2333 runtime_type_kind |= RUNTIME_TYPE_KIND_DIRECT_IFACE;
2334 int64_t ptrsize;
2335 int64_t ptrdata;
2336 if (this->needs_gcprog(gogo, &ptrsize, &ptrdata))
2337 runtime_type_kind |= RUNTIME_TYPE_KIND_GC_PROG;
2339 Struct_field_list::const_iterator p = fields->begin();
2340 go_assert(p->is_field_name("size"));
2341 Expression::Type_info type_info = Expression::TYPE_INFO_SIZE;
2342 vals->push_back(Expression::make_type_info(this, type_info));
2344 ++p;
2345 go_assert(p->is_field_name("ptrdata"));
2346 type_info = Expression::TYPE_INFO_DESCRIPTOR_PTRDATA;
2347 vals->push_back(Expression::make_type_info(this, type_info));
2349 ++p;
2350 go_assert(p->is_field_name("hash"));
2351 unsigned int h;
2352 if (name != NULL)
2353 h = name->hash_for_method(gogo);
2354 else
2355 h = this->hash_for_method(gogo);
2356 vals->push_back(Expression::make_integer_ul(h, p->type(), bloc));
2358 ++p;
2359 go_assert(p->is_field_name("kind"));
2360 vals->push_back(Expression::make_integer_ul(runtime_type_kind, p->type(),
2361 bloc));
2363 ++p;
2364 go_assert(p->is_field_name("align"));
2365 type_info = Expression::TYPE_INFO_ALIGNMENT;
2366 vals->push_back(Expression::make_type_info(this, type_info));
2368 ++p;
2369 go_assert(p->is_field_name("fieldAlign"));
2370 type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
2371 vals->push_back(Expression::make_type_info(this, type_info));
2373 ++p;
2374 go_assert(p->is_field_name("hashfn"));
2375 Function_type* hash_fntype = p->type()->function_type();
2377 ++p;
2378 go_assert(p->is_field_name("equalfn"));
2379 Function_type* equal_fntype = p->type()->function_type();
2381 Named_object* hash_fn;
2382 Named_object* equal_fn;
2383 this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
2384 &equal_fn);
2385 if (hash_fn == NULL)
2386 vals->push_back(Expression::make_cast(hash_fntype,
2387 Expression::make_nil(bloc),
2388 bloc));
2389 else
2390 vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
2391 if (equal_fn == NULL)
2392 vals->push_back(Expression::make_cast(equal_fntype,
2393 Expression::make_nil(bloc),
2394 bloc));
2395 else
2396 vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
2398 ++p;
2399 go_assert(p->is_field_name("gcdata"));
2400 vals->push_back(Expression::make_gc_symbol(this));
2402 ++p;
2403 go_assert(p->is_field_name("string"));
2404 Expression* s = Expression::make_string((name != NULL
2405 ? name->reflection(gogo)
2406 : this->reflection(gogo)),
2407 bloc);
2408 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2410 ++p;
2411 go_assert(p->is_field_name("uncommonType"));
2412 if (name == NULL && methods == NULL)
2413 vals->push_back(Expression::make_nil(bloc));
2414 else
2416 if (methods == NULL)
2417 methods = name->methods();
2418 vals->push_back(this->uncommon_type_constructor(gogo,
2419 p->type()->deref(),
2420 name, methods,
2421 only_value_methods));
2424 ++p;
2425 go_assert(p->is_field_name("ptrToThis"));
2426 if (name == NULL && methods == NULL)
2427 vals->push_back(Expression::make_nil(bloc));
2428 else
2430 Type* pt;
2431 if (name != NULL)
2432 pt = Type::make_pointer_type(name);
2433 else
2434 pt = Type::make_pointer_type(this);
2435 vals->push_back(Expression::make_type_descriptor(pt, bloc));
2438 ++p;
2439 go_assert(p == fields->end());
2441 return Expression::make_struct_composite_literal(td_type, vals, bloc);
2444 // The maximum length of a GC ptrmask bitmap. This corresponds to the
2445 // length used by the gc toolchain, and also appears in
2446 // libgo/go/reflect/type.go.
2448 static const int64_t max_ptrmask_bytes = 2048;
2450 // Return a pointer to the Garbage Collection information for this type.
2452 Bexpression*
2453 Type::gc_symbol_pointer(Gogo* gogo)
2455 Type* t = this->unalias();
2457 if (!t->has_pointer())
2458 return gogo->backend()->nil_pointer_expression();
2460 if (t->gc_symbol_var_ == NULL)
2462 t->make_gc_symbol_var(gogo);
2463 go_assert(t->gc_symbol_var_ != NULL);
2465 Location bloc = Linemap::predeclared_location();
2466 Bexpression* var_expr =
2467 gogo->backend()->var_expression(t->gc_symbol_var_, bloc);
2468 Bexpression* addr_expr =
2469 gogo->backend()->address_expression(var_expr, bloc);
2471 Type* uint8_type = Type::lookup_integer_type("uint8");
2472 Type* pointer_uint8_type = Type::make_pointer_type(uint8_type);
2473 Btype* ubtype = pointer_uint8_type->get_backend(gogo);
2474 return gogo->backend()->convert_expression(ubtype, addr_expr, bloc);
2477 // A mapping from unnamed types to GC symbol variables.
2479 Type::GC_symbol_vars Type::gc_symbol_vars;
2481 // Build the GC symbol for this type.
2483 void
2484 Type::make_gc_symbol_var(Gogo* gogo)
2486 go_assert(this->gc_symbol_var_ == NULL);
2488 Named_type* nt = this->named_type();
2490 // We can have multiple instances of unnamed types and similar to type
2491 // descriptors, we only want to the emit the GC data once, so we use a
2492 // hash table.
2493 Bvariable** phash = NULL;
2494 if (nt == NULL)
2496 Bvariable* bvnull = NULL;
2497 std::pair<GC_symbol_vars::iterator, bool> ins =
2498 Type::gc_symbol_vars.insert(std::make_pair(this, bvnull));
2499 if (!ins.second)
2501 // We've already built a gc symbol for this type.
2502 this->gc_symbol_var_ = ins.first->second;
2503 return;
2505 phash = &ins.first->second;
2508 int64_t ptrsize;
2509 int64_t ptrdata;
2510 if (!this->needs_gcprog(gogo, &ptrsize, &ptrdata))
2512 this->gc_symbol_var_ = this->gc_ptrmask_var(gogo, ptrsize, ptrdata);
2513 if (phash != NULL)
2514 *phash = this->gc_symbol_var_;
2515 return;
2518 std::string sym_name = gogo->gc_symbol_name(this);
2520 // Build the contents of the gc symbol.
2521 Expression* sym_init = this->gcprog_constructor(gogo, ptrsize, ptrdata);
2522 Btype* sym_btype = sym_init->type()->get_backend(gogo);
2524 // If the type descriptor for this type is defined somewhere else, so is the
2525 // GC symbol.
2526 const Package* dummy;
2527 if (this->type_descriptor_defined_elsewhere(nt, &dummy))
2529 std::string asm_name(go_selectively_encode_id(sym_name));
2530 this->gc_symbol_var_ =
2531 gogo->backend()->implicit_variable_reference(sym_name, asm_name,
2532 sym_btype);
2533 if (phash != NULL)
2534 *phash = this->gc_symbol_var_;
2535 return;
2538 // See if this gc symbol can appear in multiple packages.
2539 bool is_common = false;
2540 if (nt != NULL)
2542 // We create the symbol for a builtin type whenever we need
2543 // it.
2544 is_common = nt->is_builtin();
2546 else
2548 // This is an unnamed type. The descriptor could be defined in
2549 // any package where it is needed, and the linker will pick one
2550 // descriptor to keep.
2551 is_common = true;
2554 // Since we are building the GC symbol in this package, we must create the
2555 // variable before converting the initializer to its backend representation
2556 // because the initializer may refer to the GC symbol for this type.
2557 std::string asm_name(go_selectively_encode_id(sym_name));
2558 this->gc_symbol_var_ =
2559 gogo->backend()->implicit_variable(sym_name, asm_name,
2560 sym_btype, false, true, is_common, 0);
2561 if (phash != NULL)
2562 *phash = this->gc_symbol_var_;
2564 Translate_context context(gogo, NULL, NULL, NULL);
2565 context.set_is_const();
2566 Bexpression* sym_binit = sym_init->get_backend(&context);
2567 gogo->backend()->implicit_variable_set_init(this->gc_symbol_var_, sym_name,
2568 sym_btype, false, true, is_common,
2569 sym_binit);
2572 // Return whether this type needs a GC program, and set *PTRDATA to
2573 // the size of the pointer data in bytes and *PTRSIZE to the size of a
2574 // pointer.
2576 bool
2577 Type::needs_gcprog(Gogo* gogo, int64_t* ptrsize, int64_t* ptrdata)
2579 Type* voidptr = Type::make_pointer_type(Type::make_void_type());
2580 if (!voidptr->backend_type_size(gogo, ptrsize))
2581 go_unreachable();
2583 if (!this->backend_type_ptrdata(gogo, ptrdata))
2585 go_assert(saw_errors());
2586 return false;
2589 return *ptrdata / *ptrsize > max_ptrmask_bytes;
2592 // A simple class used to build a GC ptrmask for a type.
2594 class Ptrmask
2596 public:
2597 Ptrmask(size_t count)
2598 : bits_((count + 7) / 8, 0)
2601 void
2602 set_from(Gogo*, Type*, int64_t ptrsize, int64_t offset);
2604 std::string
2605 symname() const;
2607 Expression*
2608 constructor(Gogo* gogo) const;
2610 private:
2611 void
2612 set(size_t index)
2613 { this->bits_.at(index / 8) |= 1 << (index % 8); }
2615 // The actual bits.
2616 std::vector<unsigned char> bits_;
2619 // Set bits in ptrmask starting from OFFSET based on TYPE. OFFSET
2620 // counts in bytes. PTRSIZE is the size of a pointer on the target
2621 // system.
2623 void
2624 Ptrmask::set_from(Gogo* gogo, Type* type, int64_t ptrsize, int64_t offset)
2626 switch (type->base()->classification())
2628 default:
2629 case Type::TYPE_NIL:
2630 case Type::TYPE_CALL_MULTIPLE_RESULT:
2631 case Type::TYPE_NAMED:
2632 case Type::TYPE_FORWARD:
2633 go_unreachable();
2635 case Type::TYPE_ERROR:
2636 case Type::TYPE_VOID:
2637 case Type::TYPE_BOOLEAN:
2638 case Type::TYPE_INTEGER:
2639 case Type::TYPE_FLOAT:
2640 case Type::TYPE_COMPLEX:
2641 case Type::TYPE_SINK:
2642 break;
2644 case Type::TYPE_FUNCTION:
2645 case Type::TYPE_POINTER:
2646 case Type::TYPE_MAP:
2647 case Type::TYPE_CHANNEL:
2648 // These types are all a single pointer.
2649 go_assert((offset % ptrsize) == 0);
2650 this->set(offset / ptrsize);
2651 break;
2653 case Type::TYPE_STRING:
2654 // A string starts with a single pointer.
2655 go_assert((offset % ptrsize) == 0);
2656 this->set(offset / ptrsize);
2657 break;
2659 case Type::TYPE_INTERFACE:
2660 // An interface is two pointers.
2661 go_assert((offset % ptrsize) == 0);
2662 this->set(offset / ptrsize);
2663 this->set((offset / ptrsize) + 1);
2664 break;
2666 case Type::TYPE_STRUCT:
2668 if (!type->has_pointer())
2669 return;
2671 const Struct_field_list* fields = type->struct_type()->fields();
2672 int64_t soffset = 0;
2673 for (Struct_field_list::const_iterator pf = fields->begin();
2674 pf != fields->end();
2675 ++pf)
2677 int64_t field_align;
2678 if (!pf->type()->backend_type_field_align(gogo, &field_align))
2680 go_assert(saw_errors());
2681 return;
2683 soffset = (soffset + (field_align - 1)) &~ (field_align - 1);
2685 this->set_from(gogo, pf->type(), ptrsize, offset + soffset);
2687 int64_t field_size;
2688 if (!pf->type()->backend_type_size(gogo, &field_size))
2690 go_assert(saw_errors());
2691 return;
2693 soffset += field_size;
2696 break;
2698 case Type::TYPE_ARRAY:
2699 if (type->is_slice_type())
2701 // A slice starts with a single pointer.
2702 go_assert((offset % ptrsize) == 0);
2703 this->set(offset / ptrsize);
2704 break;
2706 else
2708 if (!type->has_pointer())
2709 return;
2711 int64_t len;
2712 if (!type->array_type()->int_length(&len))
2714 go_assert(saw_errors());
2715 return;
2718 Type* element_type = type->array_type()->element_type();
2719 int64_t ele_size;
2720 if (!element_type->backend_type_size(gogo, &ele_size))
2722 go_assert(saw_errors());
2723 return;
2726 int64_t eoffset = 0;
2727 for (int64_t i = 0; i < len; i++, eoffset += ele_size)
2728 this->set_from(gogo, element_type, ptrsize, offset + eoffset);
2729 break;
2734 // Return a symbol name for this ptrmask. This is used to coalesce
2735 // identical ptrmasks, which are common. The symbol name must use
2736 // only characters that are valid in symbols. It's nice if it's
2737 // short. We convert it to a string that uses only 32 characters,
2738 // avoiding digits and u and U.
2740 std::string
2741 Ptrmask::symname() const
2743 const char chars[33] = "abcdefghijklmnopqrstvwxyzABCDEFG";
2744 go_assert(chars[32] == '\0');
2745 std::string ret;
2746 unsigned int b = 0;
2747 int remaining = 0;
2748 for (std::vector<unsigned char>::const_iterator p = this->bits_.begin();
2749 p != this->bits_.end();
2750 ++p)
2752 b |= *p << remaining;
2753 remaining += 8;
2754 while (remaining >= 5)
2756 ret += chars[b & 0x1f];
2757 b >>= 5;
2758 remaining -= 5;
2761 while (remaining > 0)
2763 ret += chars[b & 0x1f];
2764 b >>= 5;
2765 remaining -= 5;
2767 return ret;
2770 // Return a constructor for this ptrmask. This will be used to
2771 // initialize the runtime ptrmask value.
2773 Expression*
2774 Ptrmask::constructor(Gogo* gogo) const
2776 Location bloc = Linemap::predeclared_location();
2777 Type* byte_type = gogo->lookup_global("byte")->type_value();
2778 Expression* len = Expression::make_integer_ul(this->bits_.size(), NULL,
2779 bloc);
2780 Array_type* at = Type::make_array_type(byte_type, len);
2781 Expression_list* vals = new Expression_list();
2782 vals->reserve(this->bits_.size());
2783 for (std::vector<unsigned char>::const_iterator p = this->bits_.begin();
2784 p != this->bits_.end();
2785 ++p)
2786 vals->push_back(Expression::make_integer_ul(*p, byte_type, bloc));
2787 return Expression::make_array_composite_literal(at, vals, bloc);
2790 // The hash table mapping a ptrmask symbol name to the ptrmask variable.
2791 Type::GC_gcbits_vars Type::gc_gcbits_vars;
2793 // Return a ptrmask variable for a type. For a type descriptor this
2794 // is only used for variables that are small enough to not need a
2795 // gcprog, but for a global variable this is used for a variable of
2796 // any size. PTRDATA is the number of bytes of the type that contain
2797 // pointer data. PTRSIZE is the size of a pointer on the target
2798 // system.
2800 Bvariable*
2801 Type::gc_ptrmask_var(Gogo* gogo, int64_t ptrsize, int64_t ptrdata)
2803 Ptrmask ptrmask(ptrdata / ptrsize);
2804 if (ptrdata >= ptrsize)
2805 ptrmask.set_from(gogo, this, ptrsize, 0);
2806 else
2808 // This can happen in error cases. Just build an empty gcbits.
2809 go_assert(saw_errors());
2812 std::string sym_name = gogo->ptrmask_symbol_name(ptrmask.symname());
2813 Bvariable* bvnull = NULL;
2814 std::pair<GC_gcbits_vars::iterator, bool> ins =
2815 Type::gc_gcbits_vars.insert(std::make_pair(sym_name, bvnull));
2816 if (!ins.second)
2818 // We've already built a GC symbol for this set of gcbits.
2819 return ins.first->second;
2822 Expression* val = ptrmask.constructor(gogo);
2823 Translate_context context(gogo, NULL, NULL, NULL);
2824 context.set_is_const();
2825 Bexpression* bval = val->get_backend(&context);
2827 std::string asm_name(go_selectively_encode_id(sym_name));
2828 Btype *btype = val->type()->get_backend(gogo);
2829 Bvariable* ret = gogo->backend()->implicit_variable(sym_name, asm_name,
2830 btype, false, true,
2831 true, 0);
2832 gogo->backend()->implicit_variable_set_init(ret, sym_name, btype, false,
2833 true, true, bval);
2834 ins.first->second = ret;
2835 return ret;
2838 // A GCProg is used to build a program for the garbage collector.
2839 // This is used for types with a lot of pointer data, to reduce the
2840 // size of the data in the compiled program. The program is expanded
2841 // at runtime. For the format, see runGCProg in libgo/go/runtime/mbitmap.go.
2843 class GCProg
2845 public:
2846 GCProg()
2847 : bytes_(), index_(0), nb_(0)
2850 // The number of bits described so far.
2851 int64_t
2852 bit_index() const
2853 { return this->index_; }
2855 void
2856 set_from(Gogo*, Type*, int64_t ptrsize, int64_t offset);
2858 void
2859 end();
2861 Expression*
2862 constructor(Gogo* gogo) const;
2864 private:
2865 void
2866 ptr(int64_t);
2868 bool
2869 should_repeat(int64_t, int64_t);
2871 void
2872 repeat(int64_t, int64_t);
2874 void
2875 zero_until(int64_t);
2877 void
2878 lit(unsigned char);
2880 void
2881 varint(int64_t);
2883 void
2884 flushlit();
2886 // Add a byte to the program.
2887 void
2888 byte(unsigned char x)
2889 { this->bytes_.push_back(x); }
2891 // The maximum number of bytes of literal bits.
2892 static const int max_literal = 127;
2894 // The program.
2895 std::vector<unsigned char> bytes_;
2896 // The index of the last bit described.
2897 int64_t index_;
2898 // The current set of literal bits.
2899 unsigned char b_[max_literal];
2900 // The current number of literal bits.
2901 int nb_;
2904 // Set data in gcprog starting from OFFSET based on TYPE. OFFSET
2905 // counts in bytes. PTRSIZE is the size of a pointer on the target
2906 // system.
2908 void
2909 GCProg::set_from(Gogo* gogo, Type* type, int64_t ptrsize, int64_t offset)
2911 switch (type->base()->classification())
2913 default:
2914 case Type::TYPE_NIL:
2915 case Type::TYPE_CALL_MULTIPLE_RESULT:
2916 case Type::TYPE_NAMED:
2917 case Type::TYPE_FORWARD:
2918 go_unreachable();
2920 case Type::TYPE_ERROR:
2921 case Type::TYPE_VOID:
2922 case Type::TYPE_BOOLEAN:
2923 case Type::TYPE_INTEGER:
2924 case Type::TYPE_FLOAT:
2925 case Type::TYPE_COMPLEX:
2926 case Type::TYPE_SINK:
2927 break;
2929 case Type::TYPE_FUNCTION:
2930 case Type::TYPE_POINTER:
2931 case Type::TYPE_MAP:
2932 case Type::TYPE_CHANNEL:
2933 // These types are all a single pointer.
2934 go_assert((offset % ptrsize) == 0);
2935 this->ptr(offset / ptrsize);
2936 break;
2938 case Type::TYPE_STRING:
2939 // A string starts with a single pointer.
2940 go_assert((offset % ptrsize) == 0);
2941 this->ptr(offset / ptrsize);
2942 break;
2944 case Type::TYPE_INTERFACE:
2945 // An interface is two pointers.
2946 go_assert((offset % ptrsize) == 0);
2947 this->ptr(offset / ptrsize);
2948 this->ptr((offset / ptrsize) + 1);
2949 break;
2951 case Type::TYPE_STRUCT:
2953 if (!type->has_pointer())
2954 return;
2956 const Struct_field_list* fields = type->struct_type()->fields();
2957 int64_t soffset = 0;
2958 for (Struct_field_list::const_iterator pf = fields->begin();
2959 pf != fields->end();
2960 ++pf)
2962 int64_t field_align;
2963 if (!pf->type()->backend_type_field_align(gogo, &field_align))
2965 go_assert(saw_errors());
2966 return;
2968 soffset = (soffset + (field_align - 1)) &~ (field_align - 1);
2970 this->set_from(gogo, pf->type(), ptrsize, offset + soffset);
2972 int64_t field_size;
2973 if (!pf->type()->backend_type_size(gogo, &field_size))
2975 go_assert(saw_errors());
2976 return;
2978 soffset += field_size;
2981 break;
2983 case Type::TYPE_ARRAY:
2984 if (type->is_slice_type())
2986 // A slice starts with a single pointer.
2987 go_assert((offset % ptrsize) == 0);
2988 this->ptr(offset / ptrsize);
2989 break;
2991 else
2993 if (!type->has_pointer())
2994 return;
2996 int64_t len;
2997 if (!type->array_type()->int_length(&len))
2999 go_assert(saw_errors());
3000 return;
3003 Type* element_type = type->array_type()->element_type();
3005 // Flatten array of array to a big array by multiplying counts.
3006 while (element_type->array_type() != NULL
3007 && !element_type->is_slice_type())
3009 int64_t ele_len;
3010 if (!element_type->array_type()->int_length(&ele_len))
3012 go_assert(saw_errors());
3013 return;
3016 len *= ele_len;
3017 element_type = element_type->array_type()->element_type();
3020 int64_t ele_size;
3021 if (!element_type->backend_type_size(gogo, &ele_size))
3023 go_assert(saw_errors());
3024 return;
3027 go_assert(len > 0 && ele_size > 0);
3029 if (!this->should_repeat(ele_size / ptrsize, len))
3031 // Cheaper to just emit the bits.
3032 int64_t eoffset = 0;
3033 for (int64_t i = 0; i < len; i++, eoffset += ele_size)
3034 this->set_from(gogo, element_type, ptrsize, offset + eoffset);
3036 else
3038 go_assert((offset % ptrsize) == 0);
3039 go_assert((ele_size % ptrsize) == 0);
3040 this->set_from(gogo, element_type, ptrsize, offset);
3041 this->zero_until((offset + ele_size) / ptrsize);
3042 this->repeat(ele_size / ptrsize, len - 1);
3045 break;
3050 // Emit a 1 into the bit stream of a GC program at the given bit index.
3052 void
3053 GCProg::ptr(int64_t index)
3055 go_assert(index >= this->index_);
3056 this->zero_until(index);
3057 this->lit(1);
3060 // Return whether it is worthwhile to use a repeat to describe c
3061 // elements of n bits each, compared to just emitting c copies of the
3062 // n-bit description.
3064 bool
3065 GCProg::should_repeat(int64_t n, int64_t c)
3067 // Repeat if there is more than 1 item and if the total data doesn't
3068 // fit into four bytes.
3069 return c > 1 && c * n > 4 * 8;
3072 // Emit an instruction to repeat the description of the last n words c
3073 // times (including the initial description, so c + 1 times in total).
3075 void
3076 GCProg::repeat(int64_t n, int64_t c)
3078 if (n == 0 || c == 0)
3079 return;
3080 this->flushlit();
3081 if (n < 128)
3082 this->byte(0x80 | static_cast<unsigned char>(n & 0x7f));
3083 else
3085 this->byte(0x80);
3086 this->varint(n);
3088 this->varint(c);
3089 this->index_ += n * c;
3092 // Add zeros to the bit stream up to the given index.
3094 void
3095 GCProg::zero_until(int64_t index)
3097 go_assert(index >= this->index_);
3098 int64_t skip = index - this->index_;
3099 if (skip == 0)
3100 return;
3101 if (skip < 4 * 8)
3103 for (int64_t i = 0; i < skip; ++i)
3104 this->lit(0);
3105 return;
3107 this->lit(0);
3108 this->flushlit();
3109 this->repeat(1, skip - 1);
3112 // Add a single literal bit to the program.
3114 void
3115 GCProg::lit(unsigned char x)
3117 if (this->nb_ == GCProg::max_literal)
3118 this->flushlit();
3119 this->b_[this->nb_] = x;
3120 ++this->nb_;
3121 ++this->index_;
3124 // Emit the varint encoding of x.
3126 void
3127 GCProg::varint(int64_t x)
3129 go_assert(x >= 0);
3130 while (x >= 0x80)
3132 this->byte(0x80 | static_cast<unsigned char>(x & 0x7f));
3133 x >>= 7;
3135 this->byte(static_cast<unsigned char>(x & 0x7f));
3138 // Flush any pending literal bits.
3140 void
3141 GCProg::flushlit()
3143 if (this->nb_ == 0)
3144 return;
3145 this->byte(static_cast<unsigned char>(this->nb_));
3146 unsigned char bits = 0;
3147 for (int i = 0; i < this->nb_; ++i)
3149 bits |= this->b_[i] << (i % 8);
3150 if ((i + 1) % 8 == 0)
3152 this->byte(bits);
3153 bits = 0;
3156 if (this->nb_ % 8 != 0)
3157 this->byte(bits);
3158 this->nb_ = 0;
3161 // Mark the end of a GC program.
3163 void
3164 GCProg::end()
3166 this->flushlit();
3167 this->byte(0);
3170 // Return an Expression for the bytes in a GC program.
3172 Expression*
3173 GCProg::constructor(Gogo* gogo) const
3175 Location bloc = Linemap::predeclared_location();
3177 // The first four bytes are the length of the program in target byte
3178 // order. Build a struct whose first type is uint32 to make this
3179 // work.
3181 Type* uint32_type = Type::lookup_integer_type("uint32");
3183 Type* byte_type = gogo->lookup_global("byte")->type_value();
3184 Expression* len = Expression::make_integer_ul(this->bytes_.size(), NULL,
3185 bloc);
3186 Array_type* at = Type::make_array_type(byte_type, len);
3188 Struct_type* st = Type::make_builtin_struct_type(2, "len", uint32_type,
3189 "bytes", at);
3191 Expression_list* vals = new Expression_list();
3192 vals->reserve(this->bytes_.size());
3193 for (std::vector<unsigned char>::const_iterator p = this->bytes_.begin();
3194 p != this->bytes_.end();
3195 ++p)
3196 vals->push_back(Expression::make_integer_ul(*p, byte_type, bloc));
3197 Expression* bytes = Expression::make_array_composite_literal(at, vals, bloc);
3199 vals = new Expression_list();
3200 vals->push_back(Expression::make_integer_ul(this->bytes_.size(), uint32_type,
3201 bloc));
3202 vals->push_back(bytes);
3204 return Expression::make_struct_composite_literal(st, vals, bloc);
3207 // Return a composite literal for the garbage collection program for
3208 // this type. This is only used for types that are too large to use a
3209 // ptrmask.
3211 Expression*
3212 Type::gcprog_constructor(Gogo* gogo, int64_t ptrsize, int64_t ptrdata)
3214 Location bloc = Linemap::predeclared_location();
3216 GCProg prog;
3217 prog.set_from(gogo, this, ptrsize, 0);
3218 int64_t offset = prog.bit_index() * ptrsize;
3219 prog.end();
3221 int64_t type_size;
3222 if (!this->backend_type_size(gogo, &type_size))
3224 go_assert(saw_errors());
3225 return Expression::make_error(bloc);
3228 go_assert(offset >= ptrdata && offset <= type_size);
3230 return prog.constructor(gogo);
3233 // Return a composite literal for the uncommon type information for
3234 // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
3235 // struct. If name is not NULL, it is the name of the type. If
3236 // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
3237 // is true if only value methods should be included. At least one of
3238 // NAME and METHODS must not be NULL.
3240 Expression*
3241 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
3242 Named_type* name, const Methods* methods,
3243 bool only_value_methods) const
3245 Location bloc = Linemap::predeclared_location();
3247 const Struct_field_list* fields = uncommon_type->struct_type()->fields();
3249 Expression_list* vals = new Expression_list();
3250 vals->reserve(3);
3252 Struct_field_list::const_iterator p = fields->begin();
3253 go_assert(p->is_field_name("name"));
3255 ++p;
3256 go_assert(p->is_field_name("pkgPath"));
3258 if (name == NULL)
3260 vals->push_back(Expression::make_nil(bloc));
3261 vals->push_back(Expression::make_nil(bloc));
3263 else
3265 Named_object* no = name->named_object();
3266 std::string n = Gogo::unpack_hidden_name(no->name());
3267 Expression* s = Expression::make_string(n, bloc);
3268 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3270 if (name->is_builtin())
3271 vals->push_back(Expression::make_nil(bloc));
3272 else
3274 const Package* package = no->package();
3275 const std::string& pkgpath(package == NULL
3276 ? gogo->pkgpath()
3277 : package->pkgpath());
3278 s = Expression::make_string(pkgpath, bloc);
3279 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3283 ++p;
3284 go_assert(p->is_field_name("methods"));
3285 vals->push_back(this->methods_constructor(gogo, p->type(), methods,
3286 only_value_methods));
3288 ++p;
3289 go_assert(p == fields->end());
3291 Expression* r = Expression::make_struct_composite_literal(uncommon_type,
3292 vals, bloc);
3293 return Expression::make_unary(OPERATOR_AND, r, bloc);
3296 // Sort methods by name.
3298 class Sort_methods
3300 public:
3301 bool
3302 operator()(const std::pair<std::string, const Method*>& m1,
3303 const std::pair<std::string, const Method*>& m2) const
3305 return (Gogo::unpack_hidden_name(m1.first)
3306 < Gogo::unpack_hidden_name(m2.first));
3310 // Return a composite literal for the type method table for this type.
3311 // METHODS_TYPE is the type of the table, and is a slice type.
3312 // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
3313 // then only value methods are used.
3315 Expression*
3316 Type::methods_constructor(Gogo* gogo, Type* methods_type,
3317 const Methods* methods,
3318 bool only_value_methods) const
3320 Location bloc = Linemap::predeclared_location();
3322 std::vector<std::pair<std::string, const Method*> > smethods;
3323 if (methods != NULL)
3325 smethods.reserve(methods->count());
3326 for (Methods::const_iterator p = methods->begin();
3327 p != methods->end();
3328 ++p)
3330 if (p->second->is_ambiguous())
3331 continue;
3332 if (only_value_methods && !p->second->is_value_method())
3333 continue;
3335 // This is where we implement the magic //go:nointerface
3336 // comment. If we saw that comment, we don't add this
3337 // method to the type descriptor.
3338 if (p->second->nointerface())
3339 continue;
3341 smethods.push_back(std::make_pair(p->first, p->second));
3345 if (smethods.empty())
3346 return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
3348 std::sort(smethods.begin(), smethods.end(), Sort_methods());
3350 Type* method_type = methods_type->array_type()->element_type();
3352 Expression_list* vals = new Expression_list();
3353 vals->reserve(smethods.size());
3354 for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
3355 = smethods.begin();
3356 p != smethods.end();
3357 ++p)
3358 vals->push_back(this->method_constructor(gogo, method_type, p->first,
3359 p->second, only_value_methods));
3361 return Expression::make_slice_composite_literal(methods_type, vals, bloc);
3364 // Return a composite literal for a single method. METHOD_TYPE is the
3365 // type of the entry. METHOD_NAME is the name of the method and M is
3366 // the method information.
3368 Expression*
3369 Type::method_constructor(Gogo*, Type* method_type,
3370 const std::string& method_name,
3371 const Method* m,
3372 bool only_value_methods) const
3374 Location bloc = Linemap::predeclared_location();
3376 const Struct_field_list* fields = method_type->struct_type()->fields();
3378 Expression_list* vals = new Expression_list();
3379 vals->reserve(5);
3381 Struct_field_list::const_iterator p = fields->begin();
3382 go_assert(p->is_field_name("name"));
3383 const std::string n = Gogo::unpack_hidden_name(method_name);
3384 Expression* s = Expression::make_string(n, bloc);
3385 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3387 ++p;
3388 go_assert(p->is_field_name("pkgPath"));
3389 if (!Gogo::is_hidden_name(method_name))
3390 vals->push_back(Expression::make_nil(bloc));
3391 else
3393 s = Expression::make_string(Gogo::hidden_name_pkgpath(method_name),
3394 bloc);
3395 vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3398 Named_object* no = (m->needs_stub_method()
3399 ? m->stub_object()
3400 : m->named_object());
3402 Function_type* mtype;
3403 if (no->is_function())
3404 mtype = no->func_value()->type();
3405 else
3406 mtype = no->func_declaration_value()->type();
3407 go_assert(mtype->is_method());
3408 Type* nonmethod_type = mtype->copy_without_receiver();
3410 ++p;
3411 go_assert(p->is_field_name("mtyp"));
3412 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
3414 ++p;
3415 go_assert(p->is_field_name("typ"));
3416 bool want_pointer_receiver = !only_value_methods && m->is_value_method();
3417 nonmethod_type = mtype->copy_with_receiver_as_param(want_pointer_receiver);
3418 vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
3420 ++p;
3421 go_assert(p->is_field_name("tfn"));
3422 vals->push_back(Expression::make_func_code_reference(no, bloc));
3424 ++p;
3425 go_assert(p == fields->end());
3427 return Expression::make_struct_composite_literal(method_type, vals, bloc);
3430 // Return a composite literal for the type descriptor of a plain type.
3431 // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
3432 // NULL, it is the name to use as well as the list of methods.
3434 Expression*
3435 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
3436 Named_type* name)
3438 return this->type_descriptor_constructor(gogo, runtime_type_kind,
3439 name, NULL, true);
3442 // Return the type reflection string for this type.
3444 std::string
3445 Type::reflection(Gogo* gogo) const
3447 std::string ret;
3449 // The do_reflection virtual function should set RET to the
3450 // reflection string.
3451 this->do_reflection(gogo, &ret);
3453 return ret;
3456 // Return whether the backend size of the type is known.
3458 bool
3459 Type::is_backend_type_size_known(Gogo* gogo)
3461 switch (this->classification_)
3463 case TYPE_ERROR:
3464 case TYPE_VOID:
3465 case TYPE_BOOLEAN:
3466 case TYPE_INTEGER:
3467 case TYPE_FLOAT:
3468 case TYPE_COMPLEX:
3469 case TYPE_STRING:
3470 case TYPE_FUNCTION:
3471 case TYPE_POINTER:
3472 case TYPE_NIL:
3473 case TYPE_MAP:
3474 case TYPE_CHANNEL:
3475 case TYPE_INTERFACE:
3476 return true;
3478 case TYPE_STRUCT:
3480 const Struct_field_list* fields = this->struct_type()->fields();
3481 for (Struct_field_list::const_iterator pf = fields->begin();
3482 pf != fields->end();
3483 ++pf)
3484 if (!pf->type()->is_backend_type_size_known(gogo))
3485 return false;
3486 return true;
3489 case TYPE_ARRAY:
3491 const Array_type* at = this->array_type();
3492 if (at->length() == NULL)
3493 return true;
3494 else
3496 Numeric_constant nc;
3497 if (!at->length()->numeric_constant_value(&nc))
3498 return false;
3499 mpz_t ival;
3500 if (!nc.to_int(&ival))
3501 return false;
3502 mpz_clear(ival);
3503 return at->element_type()->is_backend_type_size_known(gogo);
3507 case TYPE_NAMED:
3508 this->named_type()->convert(gogo);
3509 return this->named_type()->is_named_backend_type_size_known();
3511 case TYPE_FORWARD:
3513 Forward_declaration_type* fdt = this->forward_declaration_type();
3514 return fdt->real_type()->is_backend_type_size_known(gogo);
3517 case TYPE_SINK:
3518 case TYPE_CALL_MULTIPLE_RESULT:
3519 go_unreachable();
3521 default:
3522 go_unreachable();
3526 // If the size of the type can be determined, set *PSIZE to the size
3527 // in bytes and return true. Otherwise, return false. This queries
3528 // the backend.
3530 bool
3531 Type::backend_type_size(Gogo* gogo, int64_t *psize)
3533 if (!this->is_backend_type_size_known(gogo))
3534 return false;
3535 if (this->is_error_type())
3536 return false;
3537 Btype* bt = this->get_backend_placeholder(gogo);
3538 *psize = gogo->backend()->type_size(bt);
3539 if (*psize == -1)
3541 if (this->named_type() != NULL)
3542 go_error_at(this->named_type()->location(),
3543 "type %s larger than address space",
3544 Gogo::message_name(this->named_type()->name()).c_str());
3545 else
3546 go_error_at(Linemap::unknown_location(),
3547 "type %s larger than address space",
3548 this->reflection(gogo).c_str());
3550 // Make this an error type to avoid knock-on errors.
3551 this->classification_ = TYPE_ERROR;
3552 return false;
3554 return true;
3557 // If the alignment of the type can be determined, set *PALIGN to
3558 // the alignment in bytes and return true. Otherwise, return false.
3560 bool
3561 Type::backend_type_align(Gogo* gogo, int64_t *palign)
3563 if (!this->is_backend_type_size_known(gogo))
3564 return false;
3565 Btype* bt = this->get_backend_placeholder(gogo);
3566 *palign = gogo->backend()->type_alignment(bt);
3567 return true;
3570 // Like backend_type_align, but return the alignment when used as a
3571 // field.
3573 bool
3574 Type::backend_type_field_align(Gogo* gogo, int64_t *palign)
3576 if (!this->is_backend_type_size_known(gogo))
3577 return false;
3578 Btype* bt = this->get_backend_placeholder(gogo);
3579 *palign = gogo->backend()->type_field_alignment(bt);
3580 return true;
3583 // Get the ptrdata value for a type. This is the size of the prefix
3584 // of the type that contains all pointers. Store the ptrdata in
3585 // *PPTRDATA and return whether we found it.
3587 bool
3588 Type::backend_type_ptrdata(Gogo* gogo, int64_t* pptrdata)
3590 *pptrdata = 0;
3592 if (!this->has_pointer())
3593 return true;
3595 if (!this->is_backend_type_size_known(gogo))
3596 return false;
3598 switch (this->classification_)
3600 case TYPE_ERROR:
3601 return true;
3603 case TYPE_FUNCTION:
3604 case TYPE_POINTER:
3605 case TYPE_MAP:
3606 case TYPE_CHANNEL:
3607 // These types are nothing but a pointer.
3608 return this->backend_type_size(gogo, pptrdata);
3610 case TYPE_INTERFACE:
3611 // An interface is a struct of two pointers.
3612 return this->backend_type_size(gogo, pptrdata);
3614 case TYPE_STRING:
3616 // A string is a struct whose first field is a pointer, and
3617 // whose second field is not.
3618 Type* uint8_type = Type::lookup_integer_type("uint8");
3619 Type* ptr = Type::make_pointer_type(uint8_type);
3620 return ptr->backend_type_size(gogo, pptrdata);
3623 case TYPE_NAMED:
3624 case TYPE_FORWARD:
3625 return this->base()->backend_type_ptrdata(gogo, pptrdata);
3627 case TYPE_STRUCT:
3629 const Struct_field_list* fields = this->struct_type()->fields();
3630 int64_t offset = 0;
3631 const Struct_field *ptr = NULL;
3632 int64_t ptr_offset = 0;
3633 for (Struct_field_list::const_iterator pf = fields->begin();
3634 pf != fields->end();
3635 ++pf)
3637 int64_t field_align;
3638 if (!pf->type()->backend_type_field_align(gogo, &field_align))
3639 return false;
3640 offset = (offset + (field_align - 1)) &~ (field_align - 1);
3642 if (pf->type()->has_pointer())
3644 ptr = &*pf;
3645 ptr_offset = offset;
3648 int64_t field_size;
3649 if (!pf->type()->backend_type_size(gogo, &field_size))
3650 return false;
3651 offset += field_size;
3654 if (ptr != NULL)
3656 int64_t ptr_ptrdata;
3657 if (!ptr->type()->backend_type_ptrdata(gogo, &ptr_ptrdata))
3658 return false;
3659 *pptrdata = ptr_offset + ptr_ptrdata;
3661 return true;
3664 case TYPE_ARRAY:
3665 if (this->is_slice_type())
3667 // A slice is a struct whose first field is a pointer, and
3668 // whose remaining fields are not.
3669 Type* element_type = this->array_type()->element_type();
3670 Type* ptr = Type::make_pointer_type(element_type);
3671 return ptr->backend_type_size(gogo, pptrdata);
3673 else
3675 Numeric_constant nc;
3676 if (!this->array_type()->length()->numeric_constant_value(&nc))
3677 return false;
3678 int64_t len;
3679 if (!nc.to_memory_size(&len))
3680 return false;
3682 Type* element_type = this->array_type()->element_type();
3683 int64_t ele_size;
3684 int64_t ele_ptrdata;
3685 if (!element_type->backend_type_size(gogo, &ele_size)
3686 || !element_type->backend_type_ptrdata(gogo, &ele_ptrdata))
3687 return false;
3688 go_assert(ele_size > 0 && ele_ptrdata > 0);
3690 *pptrdata = (len - 1) * ele_size + ele_ptrdata;
3691 return true;
3694 default:
3695 case TYPE_VOID:
3696 case TYPE_BOOLEAN:
3697 case TYPE_INTEGER:
3698 case TYPE_FLOAT:
3699 case TYPE_COMPLEX:
3700 case TYPE_SINK:
3701 case TYPE_NIL:
3702 case TYPE_CALL_MULTIPLE_RESULT:
3703 go_unreachable();
3707 // Get the ptrdata value to store in a type descriptor. This is
3708 // normally the same as backend_type_ptrdata, but for a type that is
3709 // large enough to use a gcprog we may need to store a different value
3710 // if it ends with an array. If the gcprog u