OSDN Git Service

055bd67863d4896e57df60be834418903fe5fca8
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / types.cc
1 // types.cc -- Go frontend types.
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 #include "go-system.h"
8
9 #include <gmp.h>
10
11 #ifndef ENABLE_BUILD_WITH_CXX
12 extern "C"
13 {
14 #endif
15
16 #include "toplev.h"
17 #include "intl.h"
18 #include "tree.h"
19 #include "gimple.h"
20 #include "real.h"
21 #include "convert.h"
22
23 #ifndef ENABLE_BUILD_WITH_CXX
24 }
25 #endif
26
27 #include "go-c.h"
28 #include "gogo.h"
29 #include "operator.h"
30 #include "expressions.h"
31 #include "statements.h"
32 #include "export.h"
33 #include "import.h"
34 #include "backend.h"
35 #include "types.h"
36
37 // Class Type.
38
39 Type::Type(Type_classification classification)
40   : classification_(classification), btype_(NULL), type_descriptor_var_(NULL)
41 {
42 }
43
44 Type::~Type()
45 {
46 }
47
48 // Get the base type for a type--skip names and forward declarations.
49
50 Type*
51 Type::base()
52 {
53   switch (this->classification_)
54     {
55     case TYPE_NAMED:
56       return this->named_type()->named_base();
57     case TYPE_FORWARD:
58       return this->forward_declaration_type()->real_type()->base();
59     default:
60       return this;
61     }
62 }
63
64 const Type*
65 Type::base() const
66 {
67   switch (this->classification_)
68     {
69     case TYPE_NAMED:
70       return this->named_type()->named_base();
71     case TYPE_FORWARD:
72       return this->forward_declaration_type()->real_type()->base();
73     default:
74       return this;
75     }
76 }
77
78 // Skip defined forward declarations.
79
80 Type*
81 Type::forwarded()
82 {
83   Type* t = this;
84   Forward_declaration_type* ftype = t->forward_declaration_type();
85   while (ftype != NULL && ftype->is_defined())
86     {
87       t = ftype->real_type();
88       ftype = t->forward_declaration_type();
89     }
90   return t;
91 }
92
93 const Type*
94 Type::forwarded() const
95 {
96   const Type* t = this;
97   const Forward_declaration_type* ftype = t->forward_declaration_type();
98   while (ftype != NULL && ftype->is_defined())
99     {
100       t = ftype->real_type();
101       ftype = t->forward_declaration_type();
102     }
103   return t;
104 }
105
106 // If this is a named type, return it.  Otherwise, return NULL.
107
108 Named_type*
109 Type::named_type()
110 {
111   return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
112 }
113
114 const Named_type*
115 Type::named_type() const
116 {
117   return this->forwarded()->convert_no_base<const Named_type, TYPE_NAMED>();
118 }
119
120 // Return true if this type is not defined.
121
122 bool
123 Type::is_undefined() const
124 {
125   return this->forwarded()->forward_declaration_type() != NULL;
126 }
127
128 // Return true if this is a basic type: a type which is not composed
129 // of other types, and is not void.
130
131 bool
132 Type::is_basic_type() const
133 {
134   switch (this->classification_)
135     {
136     case TYPE_INTEGER:
137     case TYPE_FLOAT:
138     case TYPE_COMPLEX:
139     case TYPE_BOOLEAN:
140     case TYPE_STRING:
141     case TYPE_NIL:
142       return true;
143
144     case TYPE_ERROR:
145     case TYPE_VOID:
146     case TYPE_FUNCTION:
147     case TYPE_POINTER:
148     case TYPE_STRUCT:
149     case TYPE_ARRAY:
150     case TYPE_MAP:
151     case TYPE_CHANNEL:
152     case TYPE_INTERFACE:
153       return false;
154
155     case TYPE_NAMED:
156     case TYPE_FORWARD:
157       return this->base()->is_basic_type();
158
159     default:
160       go_unreachable();
161     }
162 }
163
164 // Return true if this is an abstract type.
165
166 bool
167 Type::is_abstract() const
168 {
169   switch (this->classification())
170     {
171     case TYPE_INTEGER:
172       return this->integer_type()->is_abstract();
173     case TYPE_FLOAT:
174       return this->float_type()->is_abstract();
175     case TYPE_COMPLEX:
176       return this->complex_type()->is_abstract();
177     case TYPE_STRING:
178       return this->is_abstract_string_type();
179     case TYPE_BOOLEAN:
180       return this->is_abstract_boolean_type();
181     default:
182       return false;
183     }
184 }
185
186 // Return a non-abstract version of an abstract type.
187
188 Type*
189 Type::make_non_abstract_type()
190 {
191   go_assert(this->is_abstract());
192   switch (this->classification())
193     {
194     case TYPE_INTEGER:
195       return Type::lookup_integer_type("int");
196     case TYPE_FLOAT:
197       return Type::lookup_float_type("float64");
198     case TYPE_COMPLEX:
199       return Type::lookup_complex_type("complex128");
200     case TYPE_STRING:
201       return Type::lookup_string_type();
202     case TYPE_BOOLEAN:
203       return Type::lookup_bool_type();
204     default:
205       go_unreachable();
206     }
207 }
208
209 // Return true if this is an error type.  Don't give an error if we
210 // try to dereference an undefined forwarding type, as this is called
211 // in the parser when the type may legitimately be undefined.
212
213 bool
214 Type::is_error_type() const
215 {
216   const Type* t = this->forwarded();
217   // Note that we return false for an undefined forward type.
218   switch (t->classification_)
219     {
220     case TYPE_ERROR:
221       return true;
222     case TYPE_NAMED:
223       return t->named_type()->is_named_error_type();
224     default:
225       return false;
226     }
227 }
228
229 // If this is a pointer type, return the type to which it points.
230 // Otherwise, return NULL.
231
232 Type*
233 Type::points_to() const
234 {
235   const Pointer_type* ptype = this->convert<const Pointer_type,
236                                             TYPE_POINTER>();
237   return ptype == NULL ? NULL : ptype->points_to();
238 }
239
240 // Return whether this is an open array type.
241
242 bool
243 Type::is_slice_type() const
244 {
245   return this->array_type() != NULL && this->array_type()->length() == NULL;
246 }
247
248 // Return whether this is the predeclared constant nil being used as a
249 // type.
250
251 bool
252 Type::is_nil_constant_as_type() const
253 {
254   const Type* t = this->forwarded();
255   if (t->forward_declaration_type() != NULL)
256     {
257       const Named_object* no = t->forward_declaration_type()->named_object();
258       if (no->is_unknown())
259         no = no->unknown_value()->real_named_object();
260       if (no != NULL
261           && no->is_const()
262           && no->const_value()->expr()->is_nil_expression())
263         return true;
264     }
265   return false;
266 }
267
268 // Traverse a type.
269
270 int
271 Type::traverse(Type* type, Traverse* traverse)
272 {
273   go_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
274              || (traverse->traverse_mask()
275                  & Traverse::traverse_expressions) != 0);
276   if (traverse->remember_type(type))
277     {
278       // We have already traversed this type.
279       return TRAVERSE_CONTINUE;
280     }
281   if ((traverse->traverse_mask() & Traverse::traverse_types) != 0)
282     {
283       int t = traverse->type(type);
284       if (t == TRAVERSE_EXIT)
285         return TRAVERSE_EXIT;
286       else if (t == TRAVERSE_SKIP_COMPONENTS)
287         return TRAVERSE_CONTINUE;
288     }
289   // An array type has an expression which we need to traverse if
290   // traverse_expressions is set.
291   if (type->do_traverse(traverse) == TRAVERSE_EXIT)
292     return TRAVERSE_EXIT;
293   return TRAVERSE_CONTINUE;
294 }
295
296 // Default implementation for do_traverse for child class.
297
298 int
299 Type::do_traverse(Traverse*)
300 {
301   return TRAVERSE_CONTINUE;
302 }
303
304 // Return whether two types are identical.  If ERRORS_ARE_IDENTICAL,
305 // then return true for all erroneous types; this is used to avoid
306 // cascading errors.  If REASON is not NULL, optionally set *REASON to
307 // the reason the types are not identical.
308
309 bool
310 Type::are_identical(const Type* t1, const Type* t2, bool errors_are_identical,
311                     std::string* reason)
312 {
313   if (t1 == NULL || t2 == NULL)
314     {
315       // Something is wrong.
316       return errors_are_identical ? true : t1 == t2;
317     }
318
319   // Skip defined forward declarations.
320   t1 = t1->forwarded();
321   t2 = t2->forwarded();
322
323   if (t1 == t2)
324     return true;
325
326   // An undefined forward declaration is an error.
327   if (t1->forward_declaration_type() != NULL
328       || t2->forward_declaration_type() != NULL)
329     return errors_are_identical;
330
331   // Avoid cascading errors with error types.
332   if (t1->is_error_type() || t2->is_error_type())
333     {
334       if (errors_are_identical)
335         return true;
336       return t1->is_error_type() && t2->is_error_type();
337     }
338
339   // Get a good reason for the sink type.  Note that the sink type on
340   // the left hand side of an assignment is handled in are_assignable.
341   if (t1->is_sink_type() || t2->is_sink_type())
342     {
343       if (reason != NULL)
344         *reason = "invalid use of _";
345       return false;
346     }
347
348   // A named type is only identical to itself.
349   if (t1->named_type() != NULL || t2->named_type() != NULL)
350     return false;
351
352   // Check type shapes.
353   if (t1->classification() != t2->classification())
354     return false;
355
356   switch (t1->classification())
357     {
358     case TYPE_VOID:
359     case TYPE_BOOLEAN:
360     case TYPE_STRING:
361     case TYPE_NIL:
362       // These types are always identical.
363       return true;
364
365     case TYPE_INTEGER:
366       return t1->integer_type()->is_identical(t2->integer_type());
367
368     case TYPE_FLOAT:
369       return t1->float_type()->is_identical(t2->float_type());
370
371     case TYPE_COMPLEX:
372       return t1->complex_type()->is_identical(t2->complex_type());
373
374     case TYPE_FUNCTION:
375       return t1->function_type()->is_identical(t2->function_type(),
376                                                false,
377                                                errors_are_identical,
378                                                reason);
379
380     case TYPE_POINTER:
381       return Type::are_identical(t1->points_to(), t2->points_to(),
382                                  errors_are_identical, reason);
383
384     case TYPE_STRUCT:
385       return t1->struct_type()->is_identical(t2->struct_type(),
386                                              errors_are_identical);
387
388     case TYPE_ARRAY:
389       return t1->array_type()->is_identical(t2->array_type(),
390                                             errors_are_identical);
391
392     case TYPE_MAP:
393       return t1->map_type()->is_identical(t2->map_type(),
394                                           errors_are_identical);
395
396     case TYPE_CHANNEL:
397       return t1->channel_type()->is_identical(t2->channel_type(),
398                                               errors_are_identical);
399
400     case TYPE_INTERFACE:
401       return t1->interface_type()->is_identical(t2->interface_type(),
402                                                 errors_are_identical);
403
404     case TYPE_CALL_MULTIPLE_RESULT:
405       if (reason != NULL)
406         *reason = "invalid use of multiple value function call";
407       return false;
408
409     default:
410       go_unreachable();
411     }
412 }
413
414 // Return true if it's OK to have a binary operation with types LHS
415 // and RHS.  This is not used for shifts or comparisons.
416
417 bool
418 Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
419 {
420   if (Type::are_identical(lhs, rhs, true, NULL))
421     return true;
422
423   // A constant of abstract bool type may be mixed with any bool type.
424   if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
425       || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
426     return true;
427
428   // A constant of abstract string type may be mixed with any string
429   // type.
430   if ((rhs->is_abstract_string_type() && lhs->is_string_type())
431       || (lhs->is_abstract_string_type() && rhs->is_string_type()))
432     return true;
433
434   lhs = lhs->base();
435   rhs = rhs->base();
436
437   // A constant of abstract integer, float, or complex type may be
438   // mixed with an integer, float, or complex type.
439   if ((rhs->is_abstract()
440        && (rhs->integer_type() != NULL
441            || rhs->float_type() != NULL
442            || rhs->complex_type() != NULL)
443        && (lhs->integer_type() != NULL
444            || lhs->float_type() != NULL
445            || lhs->complex_type() != NULL))
446       || (lhs->is_abstract()
447           && (lhs->integer_type() != NULL
448               || lhs->float_type() != NULL
449               || lhs->complex_type() != NULL)
450           && (rhs->integer_type() != NULL
451               || rhs->float_type() != NULL
452               || rhs->complex_type() != NULL)))
453     return true;
454
455   // The nil type may be compared to a pointer, an interface type, a
456   // slice type, a channel type, a map type, or a function type.
457   if (lhs->is_nil_type()
458       && (rhs->points_to() != NULL
459           || rhs->interface_type() != NULL
460           || rhs->is_slice_type()
461           || rhs->map_type() != NULL
462           || rhs->channel_type() != NULL
463           || rhs->function_type() != NULL))
464     return true;
465   if (rhs->is_nil_type()
466       && (lhs->points_to() != NULL
467           || lhs->interface_type() != NULL
468           || lhs->is_slice_type()
469           || lhs->map_type() != NULL
470           || lhs->channel_type() != NULL
471           || lhs->function_type() != NULL))
472     return true;
473
474   return false;
475 }
476
477 // Return true if a value with type T1 may be compared with a value of
478 // type T2.  IS_EQUALITY_OP is true for == or !=, false for <, etc.
479
480 bool
481 Type::are_compatible_for_comparison(bool is_equality_op, const Type *t1,
482                                     const Type *t2, std::string *reason)
483 {
484   if (t1 != t2
485       && !Type::are_assignable(t1, t2, NULL)
486       && !Type::are_assignable(t2, t1, NULL))
487     {
488       if (reason != NULL)
489         *reason = "incompatible types in binary expression";
490       return false;
491     }
492
493   if (!is_equality_op)
494     {
495       if (t1->integer_type() == NULL
496           && t1->float_type() == NULL
497           && !t1->is_string_type())
498         {
499           if (reason != NULL)
500             *reason = _("invalid comparison of non-ordered type");
501           return false;
502         }
503     }
504   else if (t1->is_slice_type()
505            || t1->map_type() != NULL
506            || t1->function_type() != NULL
507            || t2->is_slice_type()
508            || t2->map_type() != NULL
509            || t2->function_type() != NULL)
510     {
511       if (!t1->is_nil_type() && !t2->is_nil_type())
512         {
513           if (reason != NULL)
514             {
515               if (t1->is_slice_type() || t2->is_slice_type())
516                 *reason = _("slice can only be compared to nil");
517               else if (t1->map_type() != NULL || t2->map_type() != NULL)
518                 *reason = _("map can only be compared to nil");
519               else
520                 *reason = _("func can only be compared to nil");
521
522               // Match 6g error messages.
523               if (t1->interface_type() != NULL || t2->interface_type() != NULL)
524                 {
525                   char buf[200];
526                   snprintf(buf, sizeof buf, _("invalid operation (%s)"),
527                            reason->c_str());
528                   *reason = buf;
529                 }
530             }
531           return false;
532         }
533     }
534   else
535     {
536       if (!t1->is_boolean_type()
537           && t1->integer_type() == NULL
538           && t1->float_type() == NULL
539           && t1->complex_type() == NULL
540           && !t1->is_string_type()
541           && t1->points_to() == NULL
542           && t1->channel_type() == NULL
543           && t1->interface_type() == NULL
544           && t1->struct_type() == NULL
545           && t1->array_type() == NULL
546           && !t1->is_nil_type())
547         {
548           if (reason != NULL)
549             *reason = _("invalid comparison of non-comparable type");
550           return false;
551         }
552
553       if (t1->named_type() != NULL)
554         return t1->named_type()->named_type_is_comparable(reason);
555       else if (t2->named_type() != NULL)
556         return t2->named_type()->named_type_is_comparable(reason);
557       else if (t1->struct_type() != NULL)
558         {
559           const Struct_field_list* fields = t1->struct_type()->fields();
560           for (Struct_field_list::const_iterator p = fields->begin();
561                p != fields->end();
562                ++p)
563             {
564               if (!p->type()->is_comparable())
565                 {
566                   if (reason != NULL)
567                     *reason = _("invalid comparison of non-comparable struct");
568                   return false;
569                 }
570             }
571         }
572       else if (t1->array_type() != NULL)
573         {
574           if (t1->array_type()->length()->is_nil_expression()
575               || !t1->array_type()->element_type()->is_comparable())
576             {
577               if (reason != NULL)
578                 *reason = _("invalid comparison of non-comparable array");
579               return false;
580             }
581         }
582     }
583
584   return true;
585 }
586
587 // Return true if a value with type RHS may be assigned to a variable
588 // with type LHS.  If CHECK_HIDDEN_FIELDS is true, check whether any
589 // hidden fields are modified.  If REASON is not NULL, set *REASON to
590 // the reason the types are not assignable.
591
592 bool
593 Type::are_assignable_check_hidden(const Type* lhs, const Type* rhs,
594                                   bool check_hidden_fields,
595                                   std::string* reason)
596 {
597   // Do some checks first.  Make sure the types are defined.
598   if (rhs != NULL
599       && rhs->forwarded()->forward_declaration_type() == NULL
600       && rhs->is_void_type())
601     {
602       if (reason != NULL)
603         *reason = "non-value used as value";
604       return false;
605     }
606
607   if (lhs != NULL && lhs->forwarded()->forward_declaration_type() == NULL)
608     {
609       // Any value may be assigned to the blank identifier.
610       if (lhs->is_sink_type())
611         return true;
612
613       // All fields of a struct must be exported, or the assignment
614       // must be in the same package.
615       if (check_hidden_fields
616           && rhs != NULL
617           && rhs->forwarded()->forward_declaration_type() == NULL)
618         {
619           if (lhs->has_hidden_fields(NULL, reason)
620               || rhs->has_hidden_fields(NULL, reason))
621             return false;
622         }
623     }
624
625   // Identical types are assignable.
626   if (Type::are_identical(lhs, rhs, true, reason))
627     return true;
628
629   // The types are assignable if they have identical underlying types
630   // and either LHS or RHS is not a named type.
631   if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
632        || (rhs->named_type() != NULL && lhs->named_type() == NULL))
633       && Type::are_identical(lhs->base(), rhs->base(), true, reason))
634     return true;
635
636   // The types are assignable if LHS is an interface type and RHS
637   // implements the required methods.
638   const Interface_type* lhs_interface_type = lhs->interface_type();
639   if (lhs_interface_type != NULL)
640     {
641       if (lhs_interface_type->implements_interface(rhs, reason))
642         return true;
643       const Interface_type* rhs_interface_type = rhs->interface_type();
644       if (rhs_interface_type != NULL
645           && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
646                                                           reason))
647         return true;
648     }
649
650   // The type are assignable if RHS is a bidirectional channel type,
651   // LHS is a channel type, they have identical element types, and
652   // either LHS or RHS is not a named type.
653   if (lhs->channel_type() != NULL
654       && rhs->channel_type() != NULL
655       && rhs->channel_type()->may_send()
656       && rhs->channel_type()->may_receive()
657       && (lhs->named_type() == NULL || rhs->named_type() == NULL)
658       && Type::are_identical(lhs->channel_type()->element_type(),
659                              rhs->channel_type()->element_type(),
660                              true,
661                              reason))
662     return true;
663
664   // The nil type may be assigned to a pointer, function, slice, map,
665   // channel, or interface type.
666   if (rhs->is_nil_type()
667       && (lhs->points_to() != NULL
668           || lhs->function_type() != NULL
669           || lhs->is_slice_type()
670           || lhs->map_type() != NULL
671           || lhs->channel_type() != NULL
672           || lhs->interface_type() != NULL))
673     return true;
674
675   // An untyped numeric constant may be assigned to a numeric type if
676   // it is representable in that type.
677   if ((rhs->is_abstract()
678        && (rhs->integer_type() != NULL
679            || rhs->float_type() != NULL
680            || rhs->complex_type() != NULL))
681       && (lhs->integer_type() != NULL
682           || lhs->float_type() != NULL
683           || lhs->complex_type() != NULL))
684     return true;
685
686   // Give some better error messages.
687   if (reason != NULL && reason->empty())
688     {
689       if (rhs->interface_type() != NULL)
690         reason->assign(_("need explicit conversion"));
691       else if (rhs->is_call_multiple_result_type())
692         reason->assign(_("multiple value function call in "
693                          "single value context"));
694       else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
695         {
696           size_t len = (lhs->named_type()->name().length()
697                         + rhs->named_type()->name().length()
698                         + 100);
699           char* buf = new char[len];
700           snprintf(buf, len, _("cannot use type %s as type %s"),
701                    rhs->named_type()->message_name().c_str(),
702                    lhs->named_type()->message_name().c_str());
703           reason->assign(buf);
704           delete[] buf;
705         }
706     }
707
708   return false;
709 }
710
711 // Return true if a value with type RHS may be assigned to a variable
712 // with type LHS.  If REASON is not NULL, set *REASON to the reason
713 // the types are not assignable.
714
715 bool
716 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
717 {
718   return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
719 }
720
721 // Like are_assignable but don't check for hidden fields.
722
723 bool
724 Type::are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
725                                std::string* reason)
726 {
727   return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
728 }
729
730 // Return true if a value with type RHS may be converted to type LHS.
731 // If REASON is not NULL, set *REASON to the reason the types are not
732 // convertible.
733
734 bool
735 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
736 {
737   // The types are convertible if they are assignable.
738   if (Type::are_assignable(lhs, rhs, reason))
739     return true;
740
741   // The types are convertible if they have identical underlying
742   // types.
743   if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
744       && Type::are_identical(lhs->base(), rhs->base(), true, reason))
745     return true;
746
747   // The types are convertible if they are both unnamed pointer types
748   // and their pointer base types have identical underlying types.
749   if (lhs->named_type() == NULL
750       && rhs->named_type() == NULL
751       && lhs->points_to() != NULL
752       && rhs->points_to() != NULL
753       && (lhs->points_to()->named_type() != NULL
754           || rhs->points_to()->named_type() != NULL)
755       && Type::are_identical(lhs->points_to()->base(),
756                              rhs->points_to()->base(),
757                              true,
758                              reason))
759     return true;
760
761   // Integer and floating point types are convertible to each other.
762   if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
763       && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
764     return true;
765
766   // Complex types are convertible to each other.
767   if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
768     return true;
769
770   // An integer, or []byte, or []rune, may be converted to a string.
771   if (lhs->is_string_type())
772     {
773       if (rhs->integer_type() != NULL)
774         return true;
775       if (rhs->is_slice_type())
776         {
777           const Type* e = rhs->array_type()->element_type()->forwarded();
778           if (e->integer_type() != NULL
779               && (e->integer_type()->is_byte()
780                   || e->integer_type()->is_rune()))
781             return true;
782         }
783     }
784
785   // A string may be converted to []byte or []rune.
786   if (rhs->is_string_type() && lhs->is_slice_type())
787     {
788       const Type* e = lhs->array_type()->element_type()->forwarded();
789       if (e->integer_type() != NULL
790           && (e->integer_type()->is_byte() || e->integer_type()->is_rune()))
791         return true;
792     }
793
794   // An unsafe.Pointer type may be converted to any pointer type or to
795   // uintptr, and vice-versa.
796   if (lhs->is_unsafe_pointer_type()
797       && (rhs->points_to() != NULL
798           || (rhs->integer_type() != NULL
799               && rhs->forwarded() == Type::lookup_integer_type("uintptr"))))
800     return true;
801   if (rhs->is_unsafe_pointer_type()
802       && (lhs->points_to() != NULL
803           || (lhs->integer_type() != NULL
804               && lhs->forwarded() == Type::lookup_integer_type("uintptr"))))
805     return true;
806
807   // Give a better error message.
808   if (reason != NULL)
809     {
810       if (reason->empty())
811         *reason = "invalid type conversion";
812       else
813         {
814           std::string s = "invalid type conversion (";
815           s += *reason;
816           s += ')';
817           *reason = s;
818         }
819     }
820
821   return false;
822 }
823
824 // Return whether this type has any hidden fields.  This is only a
825 // possibility for a few types.
826
827 bool
828 Type::has_hidden_fields(const Named_type* within, std::string* reason) const
829 {
830   switch (this->forwarded()->classification_)
831     {
832     case TYPE_NAMED:
833       return this->named_type()->named_type_has_hidden_fields(reason);
834     case TYPE_STRUCT:
835       return this->struct_type()->struct_has_hidden_fields(within, reason);
836     case TYPE_ARRAY:
837       return this->array_type()->array_has_hidden_fields(within, reason);
838     default:
839       return false;
840     }
841 }
842
843 // Return a hash code for the type to be used for method lookup.
844
845 unsigned int
846 Type::hash_for_method(Gogo* gogo) const
847 {
848   unsigned int ret = 0;
849   if (this->classification_ != TYPE_FORWARD)
850     ret += this->classification_;
851   return ret + this->do_hash_for_method(gogo);
852 }
853
854 // Default implementation of do_hash_for_method.  This is appropriate
855 // for types with no subfields.
856
857 unsigned int
858 Type::do_hash_for_method(Gogo*) const
859 {
860   return 0;
861 }
862
863 // Return a hash code for a string, given a starting hash.
864
865 unsigned int
866 Type::hash_string(const std::string& s, unsigned int h)
867 {
868   const char* p = s.data();
869   size_t len = s.length();
870   for (; len > 0; --len)
871     {
872       h ^= *p++;
873       h*= 16777619;
874     }
875   return h;
876 }
877
878 // A hash table mapping unnamed types to the backend representation of
879 // those types.
880
881 Type::Type_btypes Type::type_btypes;
882
883 // Return a tree representing this type.
884
885 Btype*
886 Type::get_backend(Gogo* gogo)
887 {
888   if (this->btype_ != NULL)
889     return this->btype_;
890
891   if (this->forward_declaration_type() != NULL
892       || this->named_type() != NULL)
893     return this->get_btype_without_hash(gogo);
894
895   if (this->is_error_type())
896     return gogo->backend()->error_type();
897
898   // To avoid confusing the backend, translate all identical Go types
899   // to the same backend representation.  We use a hash table to do
900   // that.  There is no need to use the hash table for named types, as
901   // named types are only identical to themselves.
902
903   std::pair<Type*, Btype*> val(this, NULL);
904   std::pair<Type_btypes::iterator, bool> ins =
905     Type::type_btypes.insert(val);
906   if (!ins.second && ins.first->second != NULL)
907     {
908       if (gogo != NULL && gogo->named_types_are_converted())
909         this->btype_ = ins.first->second;
910       return ins.first->second;
911     }
912
913   Btype* bt = this->get_btype_without_hash(gogo);
914
915   if (ins.first->second == NULL)
916     ins.first->second = bt;
917   else
918     {
919       // We have already created a backend representation for this
920       // type.  This can happen when an unnamed type is defined using
921       // a named type which in turns uses an identical unnamed type.
922       // Use the tree we created earlier and ignore the one we just
923       // built.
924       bt = ins.first->second;
925       if (gogo == NULL || !gogo->named_types_are_converted())
926         return bt;
927       this->btype_ = bt;
928     }
929
930   return bt;
931 }
932
933 // Return the backend representation for a type without looking in the
934 // hash table for identical types.  This is used for named types,
935 // since a named type is never identical to any other type.
936
937 Btype*
938 Type::get_btype_without_hash(Gogo* gogo)
939 {
940   if (this->btype_ == NULL)
941     {
942       Btype* bt = this->do_get_backend(gogo);
943
944       // For a recursive function or pointer type, we will temporarily
945       // return a circular pointer type during the recursion.  We
946       // don't want to record that for a forwarding type, as it may
947       // confuse us later.
948       if (this->forward_declaration_type() != NULL
949           && gogo->backend()->is_circular_pointer_type(bt))
950         return bt;
951
952       if (gogo == NULL || !gogo->named_types_are_converted())
953         return bt;
954
955       this->btype_ = bt;
956     }
957   return this->btype_;
958 }
959
960 // Return a pointer to the type descriptor for this type.
961
962 tree
963 Type::type_descriptor_pointer(Gogo* gogo, Location location)
964 {
965   Type* t = this->forwarded();
966   if (t->type_descriptor_var_ == NULL)
967     {
968       t->make_type_descriptor_var(gogo);
969       go_assert(t->type_descriptor_var_ != NULL);
970     }
971   tree var_tree = var_to_tree(t->type_descriptor_var_);
972   if (var_tree == error_mark_node)
973     return error_mark_node;
974   return build_fold_addr_expr_loc(location.gcc_location(), var_tree);
975 }
976
977 // A mapping from unnamed types to type descriptor variables.
978
979 Type::Type_descriptor_vars Type::type_descriptor_vars;
980
981 // Build the type descriptor for this type.
982
983 void
984 Type::make_type_descriptor_var(Gogo* gogo)
985 {
986   go_assert(this->type_descriptor_var_ == NULL);
987
988   Named_type* nt = this->named_type();
989
990   // We can have multiple instances of unnamed types, but we only want
991   // to emit the type descriptor once.  We use a hash table.  This is
992   // not necessary for named types, as they are unique, and we store
993   // the type descriptor in the type itself.
994   Bvariable** phash = NULL;
995   if (nt == NULL)
996     {
997       Bvariable* bvnull = NULL;
998       std::pair<Type_descriptor_vars::iterator, bool> ins =
999         Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
1000       if (!ins.second)
1001         {
1002           // We've already build a type descriptor for this type.
1003           this->type_descriptor_var_ = ins.first->second;
1004           return;
1005         }
1006       phash = &ins.first->second;
1007     }
1008
1009   std::string var_name = this->type_descriptor_var_name(gogo, nt);
1010
1011   // Build the contents of the type descriptor.
1012   Expression* initializer = this->do_type_descriptor(gogo, NULL);
1013
1014   Btype* initializer_btype = initializer->type()->get_backend(gogo);
1015
1016   Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
1017
1018   const Package* dummy;
1019   if (this->type_descriptor_defined_elsewhere(nt, &dummy))
1020     {
1021       this->type_descriptor_var_ =
1022         gogo->backend()->immutable_struct_reference(var_name,
1023                                                     initializer_btype,
1024                                                     loc);
1025       if (phash != NULL)
1026         *phash = this->type_descriptor_var_;
1027       return;
1028     }
1029
1030   // See if this type descriptor can appear in multiple packages.
1031   bool is_common = false;
1032   if (nt != NULL)
1033     {
1034       // We create the descriptor for a builtin type whenever we need
1035       // it.
1036       is_common = nt->is_builtin();
1037     }
1038   else
1039     {
1040       // This is an unnamed type.  The descriptor could be defined in
1041       // any package where it is needed, and the linker will pick one
1042       // descriptor to keep.
1043       is_common = true;
1044     }
1045
1046   // We are going to build the type descriptor in this package.  We
1047   // must create the variable before we convert the initializer to the
1048   // backend representation, because the initializer may refer to the
1049   // type descriptor of this type.  By setting type_descriptor_var_ we
1050   // ensure that type_descriptor_pointer will work if called while
1051   // converting INITIALIZER.
1052
1053   this->type_descriptor_var_ =
1054     gogo->backend()->immutable_struct(var_name, is_common, initializer_btype,
1055                                       loc);
1056   if (phash != NULL)
1057     *phash = this->type_descriptor_var_;
1058
1059   Translate_context context(gogo, NULL, NULL, NULL);
1060   context.set_is_const();
1061   Bexpression* binitializer = tree_to_expr(initializer->get_tree(&context));
1062
1063   gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
1064                                              var_name, is_common,
1065                                              initializer_btype, loc,
1066                                              binitializer);
1067 }
1068
1069 // Return the name of the type descriptor variable.  If NT is not
1070 // NULL, use it to get the name.  Otherwise this is an unnamed type.
1071
1072 std::string
1073 Type::type_descriptor_var_name(Gogo* gogo, Named_type* nt)
1074 {
1075   if (nt == NULL)
1076     return "__go_td_" + this->mangled_name(gogo);
1077
1078   Named_object* no = nt->named_object();
1079   const Named_object* in_function = nt->in_function();
1080   std::string ret = "__go_tdn_";
1081   if (nt->is_builtin())
1082     go_assert(in_function == NULL);
1083   else
1084     {
1085       const std::string& unique_prefix(no->package() == NULL
1086                                        ? gogo->unique_prefix()
1087                                        : no->package()->unique_prefix());
1088       const std::string& package_name(no->package() == NULL
1089                                       ? gogo->package_name()
1090                                       : no->package()->name());
1091       ret.append(unique_prefix);
1092       ret.append(1, '.');
1093       ret.append(package_name);
1094       ret.append(1, '.');
1095       if (in_function != NULL)
1096         {
1097           ret.append(Gogo::unpack_hidden_name(in_function->name()));
1098           ret.append(1, '.');
1099         }
1100     }
1101   ret.append(no->name());
1102   return ret;
1103 }
1104
1105 // Return true if this type descriptor is defined in a different
1106 // package.  If this returns true it sets *PACKAGE to the package.
1107
1108 bool
1109 Type::type_descriptor_defined_elsewhere(Named_type* nt,
1110                                         const Package** package)
1111 {
1112   if (nt != NULL)
1113     {
1114       if (nt->named_object()->package() != NULL)
1115         {
1116           // This is a named type defined in a different package.  The
1117           // type descriptor should be defined in that package.
1118           *package = nt->named_object()->package();
1119           return true;
1120         }
1121     }
1122   else
1123     {
1124       if (this->points_to() != NULL
1125           && this->points_to()->named_type() != NULL
1126           && this->points_to()->named_type()->named_object()->package() != NULL)
1127         {
1128           // This is an unnamed pointer to a named type defined in a
1129           // different package.  The descriptor should be defined in
1130           // that package.
1131           *package = this->points_to()->named_type()->named_object()->package();
1132           return true;
1133         }
1134     }
1135   return false;
1136 }
1137
1138 // Return a composite literal for a type descriptor.
1139
1140 Expression*
1141 Type::type_descriptor(Gogo* gogo, Type* type)
1142 {
1143   return type->do_type_descriptor(gogo, NULL);
1144 }
1145
1146 // Return a composite literal for a type descriptor with a name.
1147
1148 Expression*
1149 Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
1150 {
1151   go_assert(name != NULL && type->named_type() != name);
1152   return type->do_type_descriptor(gogo, name);
1153 }
1154
1155 // Make a builtin struct type from a list of fields.  The fields are
1156 // pairs of a name and a type.
1157
1158 Struct_type*
1159 Type::make_builtin_struct_type(int nfields, ...)
1160 {
1161   va_list ap;
1162   va_start(ap, nfields);
1163
1164   Location bloc = Linemap::predeclared_location();
1165   Struct_field_list* sfl = new Struct_field_list();
1166   for (int i = 0; i < nfields; i++)
1167     {
1168       const char* field_name = va_arg(ap, const char *);
1169       Type* type = va_arg(ap, Type*);
1170       sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
1171     }
1172
1173   va_end(ap);
1174
1175   return Type::make_struct_type(sfl, bloc);
1176 }
1177
1178 // A list of builtin named types.
1179
1180 std::vector<Named_type*> Type::named_builtin_types;
1181
1182 // Make a builtin named type.
1183
1184 Named_type*
1185 Type::make_builtin_named_type(const char* name, Type* type)
1186 {
1187   Location bloc = Linemap::predeclared_location();
1188   Named_object* no = Named_object::make_type(name, NULL, type, bloc);
1189   Named_type* ret = no->type_value();
1190   Type::named_builtin_types.push_back(ret);
1191   return ret;
1192 }
1193
1194 // Convert the named builtin types.
1195
1196 void
1197 Type::convert_builtin_named_types(Gogo* gogo)
1198 {
1199   for (std::vector<Named_type*>::const_iterator p =
1200          Type::named_builtin_types.begin();
1201        p != Type::named_builtin_types.end();
1202        ++p)
1203     {
1204       bool r = (*p)->verify();
1205       go_assert(r);
1206       (*p)->convert(gogo);
1207     }
1208 }
1209
1210 // Return the type of a type descriptor.  We should really tie this to
1211 // runtime.Type rather than copying it.  This must match commonType in
1212 // libgo/go/runtime/type.go.
1213
1214 Type*
1215 Type::make_type_descriptor_type()
1216 {
1217   static Type* ret;
1218   if (ret == NULL)
1219     {
1220       Location bloc = Linemap::predeclared_location();
1221
1222       Type* uint8_type = Type::lookup_integer_type("uint8");
1223       Type* uint32_type = Type::lookup_integer_type("uint32");
1224       Type* uintptr_type = Type::lookup_integer_type("uintptr");
1225       Type* string_type = Type::lookup_string_type();
1226       Type* pointer_string_type = Type::make_pointer_type(string_type);
1227
1228       // This is an unnamed version of unsafe.Pointer.  Perhaps we
1229       // should use the named version instead, although that would
1230       // require us to create the unsafe package if it has not been
1231       // imported.  It probably doesn't matter.
1232       Type* void_type = Type::make_void_type();
1233       Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1234
1235       // Forward declaration for the type descriptor type.
1236       Named_object* named_type_descriptor_type =
1237         Named_object::make_type_declaration("commonType", NULL, bloc);
1238       Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1239       Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1240
1241       // The type of a method on a concrete type.
1242       Struct_type* method_type =
1243         Type::make_builtin_struct_type(5,
1244                                        "name", pointer_string_type,
1245                                        "pkgPath", pointer_string_type,
1246                                        "mtyp", pointer_type_descriptor_type,
1247                                        "typ", pointer_type_descriptor_type,
1248                                        "tfn", unsafe_pointer_type);
1249       Named_type* named_method_type =
1250         Type::make_builtin_named_type("method", method_type);
1251
1252       // Information for types with a name or methods.
1253       Type* slice_named_method_type =
1254         Type::make_array_type(named_method_type, NULL);
1255       Struct_type* uncommon_type =
1256         Type::make_builtin_struct_type(3,
1257                                        "name", pointer_string_type,
1258                                        "pkgPath", pointer_string_type,
1259                                        "methods", slice_named_method_type);
1260       Named_type* named_uncommon_type =
1261         Type::make_builtin_named_type("uncommonType", uncommon_type);
1262
1263       Type* pointer_uncommon_type =
1264         Type::make_pointer_type(named_uncommon_type);
1265
1266       // The type descriptor type.
1267
1268       Typed_identifier_list* params = new Typed_identifier_list();
1269       params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
1270       params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1271
1272       Typed_identifier_list* results = new Typed_identifier_list();
1273       results->push_back(Typed_identifier("", uintptr_type, bloc));
1274
1275       Type* hashfn_type = Type::make_function_type(NULL, params, results, bloc);
1276
1277       params = new Typed_identifier_list();
1278       params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
1279       params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
1280       params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1281
1282       results = new Typed_identifier_list();
1283       results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1284
1285       Type* equalfn_type = Type::make_function_type(NULL, params, results,
1286                                                     bloc);
1287
1288       Struct_type* type_descriptor_type =
1289         Type::make_builtin_struct_type(10,
1290                                        "Kind", uint8_type,
1291                                        "align", uint8_type,
1292                                        "fieldAlign", uint8_type,
1293                                        "size", uintptr_type,
1294                                        "hash", uint32_type,
1295                                        "hashfn", hashfn_type,
1296                                        "equalfn", equalfn_type,
1297                                        "string", pointer_string_type,
1298                                        "", pointer_uncommon_type,
1299                                        "ptrToThis",
1300                                        pointer_type_descriptor_type);
1301
1302       Named_type* named = Type::make_builtin_named_type("commonType",
1303                                                         type_descriptor_type);
1304
1305       named_type_descriptor_type->set_type_value(named);
1306
1307       ret = named;
1308     }
1309
1310   return ret;
1311 }
1312
1313 // Make the type of a pointer to a type descriptor as represented in
1314 // Go.
1315
1316 Type*
1317 Type::make_type_descriptor_ptr_type()
1318 {
1319   static Type* ret;
1320   if (ret == NULL)
1321     ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1322   return ret;
1323 }
1324
1325 // Set *HASH_FN and *EQUAL_FN to the runtime functions which compute a
1326 // hash code for this type and which compare whether two values of
1327 // this type are equal.  If NAME is not NULL it is the name of this
1328 // type.  HASH_FNTYPE and EQUAL_FNTYPE are the types of these
1329 // functions, for convenience; they may be NULL.
1330
1331 void
1332 Type::type_functions(Gogo* gogo, Named_type* name, Function_type* hash_fntype,
1333                      Function_type* equal_fntype, Named_object** hash_fn,
1334                      Named_object** equal_fn)
1335 {
1336   if (hash_fntype == NULL || equal_fntype == NULL)
1337     {
1338       Location bloc = Linemap::predeclared_location();
1339
1340       Type* uintptr_type = Type::lookup_integer_type("uintptr");
1341       Type* void_type = Type::make_void_type();
1342       Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1343
1344       if (hash_fntype == NULL)
1345         {
1346           Typed_identifier_list* params = new Typed_identifier_list();
1347           params->push_back(Typed_identifier("key", unsafe_pointer_type,
1348                                              bloc));
1349           params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1350
1351           Typed_identifier_list* results = new Typed_identifier_list();
1352           results->push_back(Typed_identifier("", uintptr_type, bloc));
1353
1354           hash_fntype = Type::make_function_type(NULL, params, results, bloc);
1355         }
1356       if (equal_fntype == NULL)
1357         {
1358           Typed_identifier_list* params = new Typed_identifier_list();
1359           params->push_back(Typed_identifier("key1", unsafe_pointer_type,
1360                                              bloc));
1361           params->push_back(Typed_identifier("key2", unsafe_pointer_type,
1362                                              bloc));
1363           params->push_back(Typed_identifier("key_size", uintptr_type, bloc));
1364
1365           Typed_identifier_list* results = new Typed_identifier_list();
1366           results->push_back(Typed_identifier("", Type::lookup_bool_type(),
1367                                               bloc));
1368
1369           equal_fntype = Type::make_function_type(NULL, params, results, bloc);
1370         }
1371     }
1372
1373   const char* hash_fnname;
1374   const char* equal_fnname;
1375   if (this->compare_is_identity(gogo))
1376     {
1377       hash_fnname = "__go_type_hash_identity";
1378       equal_fnname = "__go_type_equal_identity";
1379     }
1380   else if (!this->is_comparable())
1381     {
1382       hash_fnname = "__go_type_hash_error";
1383       equal_fnname = "__go_type_equal_error";
1384     }
1385   else
1386     {
1387       switch (this->base()->classification())
1388         {
1389         case Type::TYPE_ERROR:
1390         case Type::TYPE_VOID:
1391         case Type::TYPE_NIL:
1392         case Type::TYPE_FUNCTION:
1393         case Type::TYPE_MAP:
1394           // For these types is_comparable should have returned false.
1395           go_unreachable();
1396
1397         case Type::TYPE_BOOLEAN:
1398         case Type::TYPE_INTEGER:
1399         case Type::TYPE_POINTER:
1400         case Type::TYPE_CHANNEL:
1401           // For these types compare_is_identity should have returned true.
1402           go_unreachable();
1403
1404         case Type::TYPE_FLOAT:
1405           hash_fnname = "__go_type_hash_float";
1406           equal_fnname = "__go_type_equal_float";
1407           break;
1408
1409         case Type::TYPE_COMPLEX:
1410           hash_fnname = "__go_type_hash_complex";
1411           equal_fnname = "__go_type_equal_complex";
1412           break;
1413
1414         case Type::TYPE_STRING:
1415           hash_fnname = "__go_type_hash_string";
1416           equal_fnname = "__go_type_equal_string";
1417           break;
1418
1419         case Type::TYPE_STRUCT:
1420           {
1421             // This is a struct which can not be compared using a
1422             // simple identity function.  We need to build a function
1423             // for comparison.
1424             this->specific_type_functions(gogo, name, hash_fntype,
1425                                           equal_fntype, hash_fn, equal_fn);
1426             return;
1427           }
1428
1429         case Type::TYPE_ARRAY:
1430           if (this->is_slice_type())
1431             {
1432               // Type::is_compatible_for_comparison should have
1433               // returned false.
1434               go_unreachable();
1435             }
1436           else
1437             {
1438               // This is an array which can not be compared using a
1439               // simple identity function.  We need to build a
1440               // function for comparison.
1441               this->specific_type_functions(gogo, name, hash_fntype,
1442                                             equal_fntype, hash_fn, equal_fn);
1443               return;
1444             }
1445           break;
1446
1447         case Type::TYPE_INTERFACE:
1448           if (this->interface_type()->is_empty())
1449             {
1450               hash_fnname = "__go_type_hash_empty_interface";
1451               equal_fnname = "__go_type_equal_empty_interface";
1452             }
1453           else
1454             {
1455               hash_fnname = "__go_type_hash_interface";
1456               equal_fnname = "__go_type_equal_interface";
1457             }
1458           break;
1459
1460         case Type::TYPE_NAMED:
1461         case Type::TYPE_FORWARD:
1462           go_unreachable();
1463
1464         default:
1465           go_unreachable();
1466         }
1467     }
1468
1469
1470   Location bloc = Linemap::predeclared_location();
1471   *hash_fn = Named_object::make_function_declaration(hash_fnname, NULL,
1472                                                      hash_fntype, bloc);
1473   (*hash_fn)->func_declaration_value()->set_asm_name(hash_fnname);
1474   *equal_fn = Named_object::make_function_declaration(equal_fnname, NULL,
1475                                                       equal_fntype, bloc);
1476   (*equal_fn)->func_declaration_value()->set_asm_name(equal_fnname);
1477 }
1478
1479 // A hash table mapping types to the specific hash functions.
1480
1481 Type::Type_functions Type::type_functions_table;
1482
1483 // Handle a type function which is specific to a type: a struct or
1484 // array which can not use an identity comparison.
1485
1486 void
1487 Type::specific_type_functions(Gogo* gogo, Named_type* name,
1488                               Function_type* hash_fntype,
1489                               Function_type* equal_fntype,
1490                               Named_object** hash_fn,
1491                               Named_object** equal_fn)
1492 {
1493   Hash_equal_fn fnull(NULL, NULL);
1494   std::pair<Type*, Hash_equal_fn> val(name != NULL ? name : this, fnull);
1495   std::pair<Type_functions::iterator, bool> ins =
1496     Type::type_functions_table.insert(val);
1497   if (!ins.second)
1498     {
1499       // We already have functions for this type
1500       *hash_fn = ins.first->second.first;
1501       *equal_fn = ins.first->second.second;
1502       return;
1503     }
1504
1505   std::string base_name;
1506   if (name == NULL)
1507     {
1508       // Mangled names can have '.' if they happen to refer to named
1509       // types in some way.  That's fine if this is simply a named
1510       // type, but otherwise it will confuse the code that builds
1511       // function identifiers.  Remove '.' when necessary.
1512       base_name = this->mangled_name(gogo);
1513       size_t i;
1514       while ((i = base_name.find('.')) != std::string::npos)
1515         base_name[i] = '$';
1516       base_name = gogo->pack_hidden_name(base_name, false);
1517     }
1518   else
1519     {
1520       // This name is already hidden or not as appropriate.
1521       base_name = name->name();
1522       const Named_object* in_function = name->in_function();
1523       if (in_function != NULL)
1524         base_name += '$' + in_function->name();
1525     }
1526   std::string hash_name = base_name + "$hash";
1527   std::string equal_name = base_name + "$equal";
1528
1529   Location bloc = Linemap::predeclared_location();
1530
1531   const Package* package = NULL;
1532   bool is_defined_elsewhere =
1533     this->type_descriptor_defined_elsewhere(name, &package);
1534   if (is_defined_elsewhere)
1535     {
1536       *hash_fn = Named_object::make_function_declaration(hash_name, package,
1537                                                          hash_fntype, bloc);
1538       *equal_fn = Named_object::make_function_declaration(equal_name, package,
1539                                                           equal_fntype, bloc);
1540     }
1541   else
1542     {
1543       *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
1544       *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
1545                                                  bloc);
1546     }
1547
1548   ins.first->second.first = *hash_fn;
1549   ins.first->second.second = *equal_fn;
1550
1551   if (!is_defined_elsewhere)
1552     {
1553       if (gogo->in_global_scope())
1554         this->write_specific_type_functions(gogo, name, hash_name, hash_fntype,
1555                                             equal_name, equal_fntype);
1556       else
1557         gogo->queue_specific_type_function(this, name, hash_name, hash_fntype,
1558                                            equal_name, equal_fntype);
1559     }
1560 }
1561
1562 // Write the hash and equality functions for a type which needs to be
1563 // written specially.
1564
1565 void
1566 Type::write_specific_type_functions(Gogo* gogo, Named_type* name,
1567                                     const std::string& hash_name,
1568                                     Function_type* hash_fntype,
1569                                     const std::string& equal_name,
1570                                     Function_type* equal_fntype)
1571 {
1572   Location bloc = Linemap::predeclared_location();
1573
1574   Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
1575                                                bloc);
1576   gogo->start_block(bloc);
1577
1578   if (this->struct_type() != NULL)
1579     this->struct_type()->write_hash_function(gogo, name, hash_fntype,
1580                                              equal_fntype);
1581   else if (this->array_type() != NULL)
1582     this->array_type()->write_hash_function(gogo, name, hash_fntype,
1583                                             equal_fntype);
1584   else
1585     go_unreachable();
1586
1587   Block* b = gogo->finish_block(bloc);
1588   gogo->add_block(b, bloc);
1589   gogo->lower_block(hash_fn, b);
1590   gogo->finish_function(bloc);
1591
1592   Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
1593                                                 false, bloc);
1594   gogo->start_block(bloc);
1595
1596   if (this->struct_type() != NULL)
1597     this->struct_type()->write_equal_function(gogo, name);
1598   else if (this->array_type() != NULL)
1599     this->array_type()->write_equal_function(gogo, name);
1600   else
1601     go_unreachable();
1602
1603   b = gogo->finish_block(bloc);
1604   gogo->add_block(b, bloc);
1605   gogo->lower_block(equal_fn, b);
1606   gogo->finish_function(bloc);
1607 }
1608
1609 // Return a composite literal for the type descriptor for a plain type
1610 // of kind RUNTIME_TYPE_KIND named NAME.
1611
1612 Expression*
1613 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1614                                   Named_type* name, const Methods* methods,
1615                                   bool only_value_methods)
1616 {
1617   Location bloc = Linemap::predeclared_location();
1618
1619   Type* td_type = Type::make_type_descriptor_type();
1620   const Struct_field_list* fields = td_type->struct_type()->fields();
1621
1622   Expression_list* vals = new Expression_list();
1623   vals->reserve(9);
1624
1625   if (!this->has_pointer())
1626     runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
1627   Struct_field_list::const_iterator p = fields->begin();
1628   go_assert(p->is_field_name("Kind"));
1629   mpz_t iv;
1630   mpz_init_set_ui(iv, runtime_type_kind);
1631   vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1632
1633   ++p;
1634   go_assert(p->is_field_name("align"));
1635   Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1636   vals->push_back(Expression::make_type_info(this, type_info));
1637
1638   ++p;
1639   go_assert(p->is_field_name("fieldAlign"));
1640   type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1641   vals->push_back(Expression::make_type_info(this, type_info));
1642
1643   ++p;
1644   go_assert(p->is_field_name("size"));
1645   type_info = Expression::TYPE_INFO_SIZE;
1646   vals->push_back(Expression::make_type_info(this, type_info));
1647
1648   ++p;
1649   go_assert(p->is_field_name("hash"));
1650   mpz_set_ui(iv, this->hash_for_method(gogo));
1651   vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1652
1653   ++p;
1654   go_assert(p->is_field_name("hashfn"));
1655   Function_type* hash_fntype = p->type()->function_type();
1656
1657   ++p;
1658   go_assert(p->is_field_name("equalfn"));
1659   Function_type* equal_fntype = p->type()->function_type();
1660
1661   Named_object* hash_fn;
1662   Named_object* equal_fn;
1663   this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
1664                        &equal_fn);
1665   vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
1666   vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
1667
1668   ++p;
1669   go_assert(p->is_field_name("string"));
1670   Expression* s = Expression::make_string((name != NULL
1671                                            ? name->reflection(gogo)
1672                                            : this->reflection(gogo)),
1673                                           bloc);
1674   vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1675
1676   ++p;
1677   go_assert(p->is_field_name("uncommonType"));
1678   if (name == NULL && methods == NULL)
1679     vals->push_back(Expression::make_nil(bloc));
1680   else
1681     {
1682       if (methods == NULL)
1683         methods = name->methods();
1684       vals->push_back(this->uncommon_type_constructor(gogo,
1685                                                       p->type()->deref(),
1686                                                       name, methods,
1687                                                       only_value_methods));
1688     }
1689
1690   ++p;
1691   go_assert(p->is_field_name("ptrToThis"));
1692   if (name == NULL)
1693     vals->push_back(Expression::make_nil(bloc));
1694   else
1695     {
1696       Type* pt = Type::make_pointer_type(name);
1697       vals->push_back(Expression::make_type_descriptor(pt, bloc));
1698     }
1699
1700   ++p;
1701   go_assert(p == fields->end());
1702
1703   mpz_clear(iv);
1704
1705   return Expression::make_struct_composite_literal(td_type, vals, bloc);
1706 }
1707
1708 // Return a composite literal for the uncommon type information for
1709 // this type.  UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1710 // struct.  If name is not NULL, it is the name of the type.  If
1711 // METHODS is not NULL, it is the list of methods.  ONLY_VALUE_METHODS
1712 // is true if only value methods should be included.  At least one of
1713 // NAME and METHODS must not be NULL.
1714
1715 Expression*
1716 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1717                                 Named_type* name, const Methods* methods,
1718                                 bool only_value_methods) const
1719 {
1720   Location bloc = Linemap::predeclared_location();
1721
1722   const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1723
1724   Expression_list* vals = new Expression_list();
1725   vals->reserve(3);
1726
1727   Struct_field_list::const_iterator p = fields->begin();
1728   go_assert(p->is_field_name("name"));
1729
1730   ++p;
1731   go_assert(p->is_field_name("pkgPath"));
1732
1733   if (name == NULL)
1734     {
1735       vals->push_back(Expression::make_nil(bloc));
1736       vals->push_back(Expression::make_nil(bloc));
1737     }
1738   else
1739     {
1740       Named_object* no = name->named_object();
1741       std::string n = Gogo::unpack_hidden_name(no->name());
1742       Expression* s = Expression::make_string(n, bloc);
1743       vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1744
1745       if (name->is_builtin())
1746         vals->push_back(Expression::make_nil(bloc));
1747       else
1748         {
1749           const Package* package = no->package();
1750           const std::string& unique_prefix(package == NULL
1751                                            ? gogo->unique_prefix()
1752                                            : package->unique_prefix());
1753           const std::string& package_name(package == NULL
1754                                           ? gogo->package_name()
1755                                           : package->name());
1756           n.assign(unique_prefix);
1757           n.append(1, '.');
1758           n.append(package_name);
1759           if (name->in_function() != NULL)
1760             {
1761               n.append(1, '.');
1762               n.append(Gogo::unpack_hidden_name(name->in_function()->name()));
1763             }
1764           s = Expression::make_string(n, bloc);
1765           vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1766         }
1767     }
1768
1769   ++p;
1770   go_assert(p->is_field_name("methods"));
1771   vals->push_back(this->methods_constructor(gogo, p->type(), methods,
1772                                             only_value_methods));
1773
1774   ++p;
1775   go_assert(p == fields->end());
1776
1777   Expression* r = Expression::make_struct_composite_literal(uncommon_type,
1778                                                             vals, bloc);
1779   return Expression::make_unary(OPERATOR_AND, r, bloc);
1780 }
1781
1782 // Sort methods by name.
1783
1784 class Sort_methods
1785 {
1786  public:
1787   bool
1788   operator()(const std::pair<std::string, const Method*>& m1,
1789              const std::pair<std::string, const Method*>& m2) const
1790   { return m1.first < m2.first; }
1791 };
1792
1793 // Return a composite literal for the type method table for this type.
1794 // METHODS_TYPE is the type of the table, and is a slice type.
1795 // METHODS is the list of methods.  If ONLY_VALUE_METHODS is true,
1796 // then only value methods are used.
1797
1798 Expression*
1799 Type::methods_constructor(Gogo* gogo, Type* methods_type,
1800                           const Methods* methods,
1801                           bool only_value_methods) const
1802 {
1803   Location bloc = Linemap::predeclared_location();
1804
1805   std::vector<std::pair<std::string, const Method*> > smethods;
1806   if (methods != NULL)
1807     {
1808       smethods.reserve(methods->count());
1809       for (Methods::const_iterator p = methods->begin();
1810            p != methods->end();
1811            ++p)
1812         {
1813           if (p->second->is_ambiguous())
1814             continue;
1815           if (only_value_methods && !p->second->is_value_method())
1816             continue;
1817           smethods.push_back(std::make_pair(p->first, p->second));
1818         }
1819     }
1820
1821   if (smethods.empty())
1822     return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
1823
1824   std::sort(smethods.begin(), smethods.end(), Sort_methods());
1825
1826   Type* method_type = methods_type->array_type()->element_type();
1827
1828   Expression_list* vals = new Expression_list();
1829   vals->reserve(smethods.size());
1830   for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
1831          = smethods.begin();
1832        p != smethods.end();
1833        ++p)
1834     vals->push_back(this->method_constructor(gogo, method_type, p->first,
1835                                              p->second, only_value_methods));
1836
1837   return Expression::make_slice_composite_literal(methods_type, vals, bloc);
1838 }
1839
1840 // Return a composite literal for a single method.  METHOD_TYPE is the
1841 // type of the entry.  METHOD_NAME is the name of the method and M is
1842 // the method information.
1843
1844 Expression*
1845 Type::method_constructor(Gogo*, Type* method_type,
1846                          const std::string& method_name,
1847                          const Method* m,
1848                          bool only_value_methods) const
1849 {
1850   Location bloc = Linemap::predeclared_location();
1851
1852   const Struct_field_list* fields = method_type->struct_type()->fields();
1853
1854   Expression_list* vals = new Expression_list();
1855   vals->reserve(5);
1856
1857   Struct_field_list::const_iterator p = fields->begin();
1858   go_assert(p->is_field_name("name"));
1859   const std::string n = Gogo::unpack_hidden_name(method_name);
1860   Expression* s = Expression::make_string(n, bloc);
1861   vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1862
1863   ++p;
1864   go_assert(p->is_field_name("pkgPath"));
1865   if (!Gogo::is_hidden_name(method_name))
1866     vals->push_back(Expression::make_nil(bloc));
1867   else
1868     {
1869       s = Expression::make_string(Gogo::hidden_name_prefix(method_name), bloc);
1870       vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1871     }
1872
1873   Named_object* no = (m->needs_stub_method()
1874                       ? m->stub_object()
1875                       : m->named_object());
1876
1877   Function_type* mtype;
1878   if (no->is_function())
1879     mtype = no->func_value()->type();
1880   else
1881     mtype = no->func_declaration_value()->type();
1882   go_assert(mtype->is_method());
1883   Type* nonmethod_type = mtype->copy_without_receiver();
1884
1885   ++p;
1886   go_assert(p->is_field_name("mtyp"));
1887   vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
1888
1889   ++p;
1890   go_assert(p->is_field_name("typ"));
1891   if (!only_value_methods && m->is_value_method())
1892     {
1893       // This is a value method on a pointer type.  Change the type of
1894       // the method to use a pointer receiver.  The implementation
1895       // always uses a pointer receiver anyhow.
1896       Type* rtype = mtype->receiver()->type();
1897       Type* prtype = Type::make_pointer_type(rtype);
1898       Typed_identifier* receiver =
1899         new Typed_identifier(mtype->receiver()->name(), prtype,
1900                              mtype->receiver()->location());
1901       mtype = Type::make_function_type(receiver,
1902                                        (mtype->parameters() == NULL
1903                                         ? NULL
1904                                         : mtype->parameters()->copy()),
1905                                        (mtype->results() == NULL
1906                                         ? NULL
1907                                         : mtype->results()->copy()),
1908                                        mtype->location());
1909     }
1910   vals->push_back(Expression::make_type_descriptor(mtype, bloc));
1911
1912   ++p;
1913   go_assert(p->is_field_name("tfn"));
1914   vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1915
1916   ++p;
1917   go_assert(p == fields->end());
1918
1919   return Expression::make_struct_composite_literal(method_type, vals, bloc);
1920 }
1921
1922 // Return a composite literal for the type descriptor of a plain type.
1923 // RUNTIME_TYPE_KIND is the value of the kind field.  If NAME is not
1924 // NULL, it is the name to use as well as the list of methods.
1925
1926 Expression*
1927 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
1928                             Named_type* name)
1929 {
1930   return this->type_descriptor_constructor(gogo, runtime_type_kind,
1931                                            name, NULL, true);
1932 }
1933
1934 // Return the type reflection string for this type.
1935
1936 std::string
1937 Type::reflection(Gogo* gogo) const
1938 {
1939   std::string ret;
1940
1941   // The do_reflection virtual function should set RET to the
1942   // reflection string.
1943   this->do_reflection(gogo, &ret);
1944
1945   return ret;
1946 }
1947
1948 // Return a mangled name for the type.
1949
1950 std::string
1951 Type::mangled_name(Gogo* gogo) const
1952 {
1953   std::string ret;
1954
1955   // The do_mangled_name virtual function should set RET to the
1956   // mangled name.  For a composite type it should append a code for
1957   // the composition and then call do_mangled_name on the components.
1958   this->do_mangled_name(gogo, &ret);
1959
1960   return ret;
1961 }
1962
1963 // Return whether the backend size of the type is known.
1964
1965 bool
1966 Type::is_backend_type_size_known(Gogo* gogo)
1967 {
1968   switch (this->classification_)
1969     {
1970     case TYPE_ERROR:
1971     case TYPE_VOID:
1972     case TYPE_BOOLEAN:
1973     case TYPE_INTEGER:
1974     case TYPE_FLOAT:
1975     case TYPE_COMPLEX:
1976     case TYPE_STRING:
1977     case TYPE_FUNCTION:
1978     case TYPE_POINTER:
1979     case TYPE_NIL:
1980     case TYPE_MAP:
1981     case TYPE_CHANNEL:
1982     case TYPE_INTERFACE:
1983       return true;
1984
1985     case TYPE_STRUCT:
1986       {
1987         const Struct_field_list* fields = this->struct_type()->fields();
1988         for (Struct_field_list::const_iterator pf = fields->begin();
1989              pf != fields->end();
1990              ++pf)
1991           if (!pf->type()->is_backend_type_size_known(gogo))
1992             return false;
1993         return true;
1994       }
1995
1996     case TYPE_ARRAY:
1997       {
1998         const Array_type* at = this->array_type();
1999         if (at->length() == NULL)
2000           return true;
2001         else
2002           {
2003             mpz_t ival;
2004             mpz_init(ival);
2005             Type* dummy;
2006             bool length_known = at->length()->integer_constant_value(true,
2007                                                                      ival,
2008                                                                      &dummy);
2009             mpz_clear(ival);
2010             if (!length_known)
2011               return false;
2012             return at->element_type()->is_backend_type_size_known(gogo);
2013           }
2014       }
2015
2016     case TYPE_NAMED:
2017       // Begin converting this type to the backend representation.
2018       // This will create a placeholder if necessary.
2019       this->get_backend(gogo);
2020       return this->named_type()->is_named_backend_type_size_known();
2021
2022     case TYPE_FORWARD:
2023       {
2024         Forward_declaration_type* fdt = this->forward_declaration_type();
2025         return fdt->real_type()->is_backend_type_size_known(gogo);
2026       }
2027
2028     case TYPE_SINK:
2029     case TYPE_CALL_MULTIPLE_RESULT:
2030       go_unreachable();
2031
2032     default:
2033       go_unreachable();
2034     }
2035 }
2036
2037 // If the size of the type can be determined, set *PSIZE to the size
2038 // in bytes and return true.  Otherwise, return false.  This queries
2039 // the backend.
2040
2041 bool
2042 Type::backend_type_size(Gogo* gogo, unsigned int *psize)
2043 {
2044   if (!this->is_backend_type_size_known(gogo))
2045     return false;
2046   size_t size = gogo->backend()->type_size(this->get_backend(gogo));
2047   *psize = static_cast<unsigned int>(size);
2048   if (*psize != size)
2049     return false;
2050   return true;
2051 }
2052
2053 // If the alignment of the type can be determined, set *PALIGN to
2054 // the alignment in bytes and return true.  Otherwise, return false.
2055
2056 bool
2057 Type::backend_type_align(Gogo* gogo, unsigned int *palign)
2058 {
2059   if (!this->is_backend_type_size_known(gogo))
2060     return false;
2061   size_t align = gogo->backend()->type_alignment(this->get_backend(gogo));
2062   *palign = static_cast<unsigned int>(align);
2063   if (*palign != align)
2064     return false;
2065   return true;
2066 }
2067
2068 // Like backend_type_align, but return the alignment when used as a
2069 // field.
2070
2071 bool
2072 Type::backend_type_field_align(Gogo* gogo, unsigned int *palign)
2073 {
2074   if (!this->is_backend_type_size_known(gogo))
2075     return false;
2076   size_t a = gogo->backend()->type_field_alignment(this->get_backend(gogo));
2077   *palign = static_cast<unsigned int>(a);
2078   if (*palign != a)
2079     return false;
2080   return true;
2081 }
2082
2083 // Default function to export a type.
2084
2085 void
2086 Type::do_export(Export*) const
2087 {
2088   go_unreachable();
2089 }
2090
2091 // Import a type.
2092
2093 Type*
2094 Type::import_type(Import* imp)
2095 {
2096   if (imp->match_c_string("("))
2097     return Function_type::do_import(imp);
2098   else if (imp->match_c_string("*"))
2099     return Pointer_type::do_import(imp);
2100   else if (imp->match_c_string("struct "))
2101     return Struct_type::do_import(imp);
2102   else if (imp->match_c_string("["))
2103     return Array_type::do_import(imp);
2104   else if (imp->match_c_string("map "))
2105     return Map_type::do_import(imp);
2106   else if (imp->match_c_string("chan "))
2107     return Channel_type::do_import(imp);
2108   else if (imp->match_c_string("interface"))
2109     return Interface_type::do_import(imp);
2110   else
2111     {
2112       error_at(imp->location(), "import error: expected type");
2113       return Type::make_error_type();
2114     }
2115 }
2116
2117 // A type used to indicate a parsing error.  This exists to simplify
2118 // later error detection.
2119
2120 class Error_type : public Type
2121 {
2122  public:
2123   Error_type()
2124     : Type(TYPE_ERROR)
2125   { }
2126
2127  protected:
2128   bool
2129   do_compare_is_identity(Gogo*) const
2130   { return false; }
2131
2132   Btype*
2133   do_get_backend(Gogo* gogo)
2134   { return gogo->backend()->error_type(); }
2135
2136   Expression*
2137   do_type_descriptor(Gogo*, Named_type*)
2138   { return Expression::make_error(Linemap::predeclared_location()); }
2139
2140   void
2141   do_reflection(Gogo*, std::string*) const
2142   { go_assert(saw_errors()); }
2143
2144   void
2145   do_mangled_name(Gogo*, std::string* ret) const
2146   { ret->push_back('E'); }
2147 };
2148
2149 Type*
2150 Type::make_error_type()
2151 {
2152   static Error_type singleton_error_type;
2153   return &singleton_error_type;
2154 }
2155
2156 // The void type.
2157
2158 class Void_type : public Type
2159 {
2160  public:
2161   Void_type()
2162     : Type(TYPE_VOID)
2163   { }
2164
2165  protected:
2166   bool
2167   do_compare_is_identity(Gogo*) const
2168   { return false; }
2169
2170   Btype*
2171   do_get_backend(Gogo* gogo)
2172   { return gogo->backend()->void_type(); }
2173
2174   Expression*
2175   do_type_descriptor(Gogo*, Named_type*)
2176   { go_unreachable(); }
2177
2178   void
2179   do_reflection(Gogo*, std::string*) const
2180   { }
2181
2182   void
2183   do_mangled_name(Gogo*, std::string* ret) const
2184   { ret->push_back('v'); }
2185 };
2186
2187 Type*
2188 Type::make_void_type()
2189 {
2190   static Void_type singleton_void_type;
2191   return &singleton_void_type;
2192 }
2193
2194 // The boolean type.
2195
2196 class Boolean_type : public Type
2197 {
2198  public:
2199   Boolean_type()
2200     : Type(TYPE_BOOLEAN)
2201   { }
2202
2203  protected:
2204   bool
2205   do_compare_is_identity(Gogo*) const
2206   { return true; }
2207
2208   Btype*
2209   do_get_backend(Gogo* gogo)
2210   { return gogo->backend()->bool_type(); }
2211
2212   Expression*
2213   do_type_descriptor(Gogo*, Named_type* name);
2214
2215   // We should not be asked for the reflection string of a basic type.
2216   void
2217   do_reflection(Gogo*, std::string* ret) const
2218   { ret->append("bool"); }
2219
2220   void
2221   do_mangled_name(Gogo*, std::string* ret) const
2222   { ret->push_back('b'); }
2223 };
2224
2225 // Make the type descriptor.
2226
2227 Expression*
2228 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2229 {
2230   if (name != NULL)
2231     return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
2232   else
2233     {
2234       Named_object* no = gogo->lookup_global("bool");
2235       go_assert(no != NULL);
2236       return Type::type_descriptor(gogo, no->type_value());
2237     }
2238 }
2239
2240 Type*
2241 Type::make_boolean_type()
2242 {
2243   static Boolean_type boolean_type;
2244   return &boolean_type;
2245 }
2246
2247 // The named type "bool".
2248
2249 static Named_type* named_bool_type;
2250
2251 // Get the named type "bool".
2252
2253 Named_type*
2254 Type::lookup_bool_type()
2255 {
2256   return named_bool_type;
2257 }
2258
2259 // Make the named type "bool".
2260
2261 Named_type*
2262 Type::make_named_bool_type()
2263 {
2264   Type* bool_type = Type::make_boolean_type();
2265   Named_object* named_object =
2266     Named_object::make_type("bool", NULL, bool_type,
2267                             Linemap::predeclared_location());
2268   Named_type* named_type = named_object->type_value();
2269   named_bool_type = named_type;
2270   return named_type;
2271 }
2272
2273 // Class Integer_type.
2274
2275 Integer_type::Named_integer_types Integer_type::named_integer_types;
2276
2277 // Create a new integer type.  Non-abstract integer types always have
2278 // names.
2279
2280 Named_type*
2281 Integer_type::create_integer_type(const char* name, bool is_unsigned,
2282                                   int bits, int runtime_type_kind)
2283 {
2284   Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
2285                                                 runtime_type_kind);
2286   std::string sname(name);
2287   Named_object* named_object =
2288     Named_object::make_type(sname, NULL, integer_type,
2289                             Linemap::predeclared_location());
2290   Named_type* named_type = named_object->type_value();
2291   std::pair<Named_integer_types::iterator, bool> ins =
2292     Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
2293   go_assert(ins.second);
2294   return named_type;
2295 }
2296
2297 // Look up an existing integer type.
2298
2299 Named_type*
2300 Integer_type::lookup_integer_type(const char* name)
2301 {
2302   Named_integer_types::const_iterator p =
2303     Integer_type::named_integer_types.find(name);
2304   go_assert(p != Integer_type::named_integer_types.end());
2305   return p->second;
2306 }
2307
2308 // Create a new abstract integer type.
2309
2310 Integer_type*
2311 Integer_type::create_abstract_integer_type()
2312 {
2313   static Integer_type* abstract_type;
2314   if (abstract_type == NULL)
2315     abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
2316                                      RUNTIME_TYPE_KIND_INT);
2317   return abstract_type;
2318 }
2319
2320 // Integer type compatibility.
2321
2322 bool
2323 Integer_type::is_identical(const Integer_type* t) const
2324 {
2325   if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
2326     return false;
2327   return this->is_abstract_ == t->is_abstract_;
2328 }
2329
2330 // Hash code.
2331
2332 unsigned int
2333 Integer_type::do_hash_for_method(Gogo*) const
2334 {
2335   return ((this->bits_ << 4)
2336           + ((this->is_unsigned_ ? 1 : 0) << 8)
2337           + ((this->is_abstract_ ? 1 : 0) << 9));
2338 }
2339
2340 // Convert an Integer_type to the backend representation.
2341
2342 Btype*
2343 Integer_type::do_get_backend(Gogo* gogo)
2344 {
2345   if (this->is_abstract_)
2346     {
2347       go_assert(saw_errors());
2348       return gogo->backend()->error_type();
2349     }
2350   return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
2351 }
2352
2353 // The type descriptor for an integer type.  Integer types are always
2354 // named.
2355
2356 Expression*
2357 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2358 {
2359   go_assert(name != NULL);
2360   return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2361 }
2362
2363 // We should not be asked for the reflection string of a basic type.
2364
2365 void
2366 Integer_type::do_reflection(Gogo*, std::string*) const
2367 {
2368   go_assert(saw_errors());
2369 }
2370
2371 // Mangled name.
2372
2373 void
2374 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
2375 {
2376   char buf[100];
2377   snprintf(buf, sizeof buf, "i%s%s%de",
2378            this->is_abstract_ ? "a" : "",
2379            this->is_unsigned_ ? "u" : "",
2380            this->bits_);
2381   ret->append(buf);
2382 }
2383
2384 // Make an integer type.
2385
2386 Named_type*
2387 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
2388                         int runtime_type_kind)
2389 {
2390   return Integer_type::create_integer_type(name, is_unsigned, bits,
2391                                            runtime_type_kind);
2392 }
2393
2394 // Make an abstract integer type.
2395
2396 Integer_type*
2397 Type::make_abstract_integer_type()
2398 {
2399   return Integer_type::create_abstract_integer_type();
2400 }
2401
2402 // Look up an integer type.
2403
2404 Named_type*
2405 Type::lookup_integer_type(const char* name)
2406 {
2407   return Integer_type::lookup_integer_type(name);
2408 }
2409
2410 // Class Float_type.
2411
2412 Float_type::Named_float_types Float_type::named_float_types;
2413
2414 // Create a new float type.  Non-abstract float types always have
2415 // names.
2416
2417 Named_type*
2418 Float_type::create_float_type(const char* name, int bits,
2419                               int runtime_type_kind)
2420 {
2421   Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
2422   std::string sname(name);
2423   Named_object* named_object =
2424     Named_object::make_type(sname, NULL, float_type,
2425                             Linemap::predeclared_location());
2426   Named_type* named_type = named_object->type_value();
2427   std::pair<Named_float_types::iterator, bool> ins =
2428     Float_type::named_float_types.insert(std::make_pair(sname, named_type));
2429   go_assert(ins.second);
2430   return named_type;
2431 }
2432
2433 // Look up an existing float type.
2434
2435 Named_type*
2436 Float_type::lookup_float_type(const char* name)
2437 {
2438   Named_float_types::const_iterator p =
2439     Float_type::named_float_types.find(name);
2440   go_assert(p != Float_type::named_float_types.end());
2441   return p->second;
2442 }
2443
2444 // Create a new abstract float type.
2445
2446 Float_type*
2447 Float_type::create_abstract_float_type()
2448 {
2449   static Float_type* abstract_type;
2450   if (abstract_type == NULL)
2451     abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
2452   return abstract_type;
2453 }
2454
2455 // Whether this type is identical with T.
2456
2457 bool
2458 Float_type::is_identical(const Float_type* t) const
2459 {
2460   if (this->bits_ != t->bits_)
2461     return false;
2462   return this->is_abstract_ == t->is_abstract_;
2463 }
2464
2465 // Hash code.
2466
2467 unsigned int
2468 Float_type::do_hash_for_method(Gogo*) const
2469 {
2470   return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2471 }
2472
2473 // Convert to the backend representation.
2474
2475 Btype*
2476 Float_type::do_get_backend(Gogo* gogo)
2477 {
2478   return gogo->backend()->float_type(this->bits_);
2479 }
2480
2481 // The type descriptor for a float type.  Float types are always named.
2482
2483 Expression*
2484 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2485 {
2486   go_assert(name != NULL);
2487   return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2488 }
2489
2490 // We should not be asked for the reflection string of a basic type.
2491
2492 void
2493 Float_type::do_reflection(Gogo*, std::string*) const
2494 {
2495   go_assert(saw_errors());
2496 }
2497
2498 // Mangled name.
2499
2500 void
2501 Float_type::do_mangled_name(Gogo*, std::string* ret) const
2502 {
2503   char buf[100];
2504   snprintf(buf, sizeof buf, "f%s%de",
2505            this->is_abstract_ ? "a" : "",
2506            this->bits_);
2507   ret->append(buf);
2508 }
2509
2510 // Make a floating point type.
2511
2512 Named_type*
2513 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
2514 {
2515   return Float_type::create_float_type(name, bits, runtime_type_kind);
2516 }
2517
2518 // Make an abstract float type.
2519
2520 Float_type*
2521 Type::make_abstract_float_type()
2522 {
2523   return Float_type::create_abstract_float_type();
2524 }
2525
2526 // Look up a float type.
2527
2528 Named_type*
2529 Type::lookup_float_type(const char* name)
2530 {
2531   return Float_type::lookup_float_type(name);
2532 }
2533
2534 // Class Complex_type.
2535
2536 Complex_type::Named_complex_types Complex_type::named_complex_types;
2537
2538 // Create a new complex type.  Non-abstract complex types always have
2539 // names.
2540
2541 Named_type*
2542 Complex_type::create_complex_type(const char* name, int bits,
2543                                   int runtime_type_kind)
2544 {
2545   Complex_type* complex_type = new Complex_type(false, bits,
2546                                                 runtime_type_kind);
2547   std::string sname(name);
2548   Named_object* named_object =
2549     Named_object::make_type(sname, NULL, complex_type,
2550                             Linemap::predeclared_location());
2551   Named_type* named_type = named_object->type_value();
2552   std::pair<Named_complex_types::iterator, bool> ins =
2553     Complex_type::named_complex_types.insert(std::make_pair(sname,
2554                                                             named_type));
2555   go_assert(ins.second);
2556   return named_type;
2557 }
2558
2559 // Look up an existing complex type.
2560
2561 Named_type*
2562 Complex_type::lookup_complex_type(const char* name)
2563 {
2564   Named_complex_types::const_iterator p =
2565     Complex_type::named_complex_types.find(name);
2566   go_assert(p != Complex_type::named_complex_types.end());
2567   return p->second;
2568 }
2569
2570 // Create a new abstract complex type.
2571
2572 Complex_type*
2573 Complex_type::create_abstract_complex_type()
2574 {
2575   static Complex_type* abstract_type;
2576   if (abstract_type == NULL)
2577     abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
2578   return abstract_type;
2579 }
2580
2581 // Whether this type is identical with T.
2582
2583 bool
2584 Complex_type::is_identical(const Complex_type *t) const
2585 {
2586   if (this->bits_ != t->bits_)
2587     return false;
2588   return this->is_abstract_ == t->is_abstract_;
2589 }
2590
2591 // Hash code.
2592
2593 unsigned int
2594 Complex_type::do_hash_for_method(Gogo*) const
2595 {
2596   return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2597 }
2598
2599 // Convert to the backend representation.
2600
2601 Btype*
2602 Complex_type::do_get_backend(Gogo* gogo)
2603 {
2604   return gogo->backend()->complex_type(this->bits_);
2605 }
2606
2607 // The type descriptor for a complex type.  Complex types are always
2608 // named.
2609
2610 Expression*
2611 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2612 {
2613   go_assert(name != NULL);
2614   return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2615 }
2616
2617 // We should not be asked for the reflection string of a basic type.
2618
2619 void
2620 Complex_type::do_reflection(Gogo*, std::string*) const
2621 {
2622   go_assert(saw_errors());
2623 }
2624
2625 // Mangled name.
2626
2627 void
2628 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2629 {
2630   char buf[100];
2631   snprintf(buf, sizeof buf, "c%s%de",
2632            this->is_abstract_ ? "a" : "",
2633            this->bits_);
2634   ret->append(buf);
2635 }
2636
2637 // Make a complex type.
2638
2639 Named_type*
2640 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2641 {
2642   return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2643 }
2644
2645 // Make an abstract complex type.
2646
2647 Complex_type*
2648 Type::make_abstract_complex_type()
2649 {
2650   return Complex_type::create_abstract_complex_type();
2651 }
2652
2653 // Look up a complex type.
2654
2655 Named_type*
2656 Type::lookup_complex_type(const char* name)
2657 {
2658   return Complex_type::lookup_complex_type(name);
2659 }
2660
2661 // Class String_type.
2662
2663 // Convert String_type to the backend representation.  A string is a
2664 // struct with two fields: a pointer to the characters and a length.
2665
2666 Btype*
2667 String_type::do_get_backend(Gogo* gogo)
2668 {
2669   static Btype* backend_string_type;
2670   if (backend_string_type == NULL)
2671     {
2672       std::vector<Backend::Btyped_identifier> fields(2);
2673
2674       Type* b = gogo->lookup_global("byte")->type_value();
2675       Type* pb = Type::make_pointer_type(b);
2676       fields[0].name = "__data";
2677       fields[0].btype = pb->get_backend(gogo);
2678       fields[0].location = Linemap::predeclared_location();
2679
2680       Type* int_type = Type::lookup_integer_type("int");
2681       fields[1].name = "__length";
2682       fields[1].btype = int_type->get_backend(gogo);
2683       fields[1].location = fields[0].location;
2684
2685       backend_string_type = gogo->backend()->struct_type(fields);
2686     }
2687   return backend_string_type;
2688 }
2689
2690 // Return a tree for the length of STRING.
2691
2692 tree
2693 String_type::length_tree(Gogo*, tree string)
2694 {
2695   tree string_type = TREE_TYPE(string);
2696   go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2697   tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2698   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2699                     "__length") == 0);
2700   return fold_build3(COMPONENT_REF, integer_type_node, string,
2701                      length_field, NULL_TREE);
2702 }
2703
2704 // Return a tree for a pointer to the bytes of STRING.
2705
2706 tree
2707 String_type::bytes_tree(Gogo*, tree string)
2708 {
2709   tree string_type = TREE_TYPE(string);
2710   go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2711   tree bytes_field = TYPE_FIELDS(string_type);
2712   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
2713                     "__data") == 0);
2714   return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
2715                      bytes_field, NULL_TREE);
2716 }
2717
2718 // The type descriptor for the string type.
2719
2720 Expression*
2721 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2722 {
2723   if (name != NULL)
2724     return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
2725   else
2726     {
2727       Named_object* no = gogo->lookup_global("string");
2728       go_assert(no != NULL);
2729       return Type::type_descriptor(gogo, no->type_value());
2730     }
2731 }
2732
2733 // We should not be asked for the reflection string of a basic type.
2734
2735 void
2736 String_type::do_reflection(Gogo*, std::string* ret) const
2737 {
2738   ret->append("string");
2739 }
2740
2741 // Mangled name of a string type.
2742
2743 void
2744 String_type::do_mangled_name(Gogo*, std::string* ret) const
2745 {
2746   ret->push_back('z');
2747 }
2748
2749 // Make a string type.
2750
2751 Type*
2752 Type::make_string_type()
2753 {
2754   static String_type string_type;
2755   return &string_type;
2756 }
2757
2758 // The named type "string".
2759
2760 static Named_type* named_string_type;
2761
2762 // Get the named type "string".
2763
2764 Named_type*
2765 Type::lookup_string_type()
2766 {
2767   return named_string_type;
2768 }
2769
2770 // Make the named type string.
2771
2772 Named_type*
2773 Type::make_named_string_type()
2774 {
2775   Type* string_type = Type::make_string_type();
2776   Named_object* named_object =
2777     Named_object::make_type("string", NULL, string_type,
2778                             Linemap::predeclared_location());
2779   Named_type* named_type = named_object->type_value();
2780   named_string_type = named_type;
2781   return named_type;
2782 }
2783
2784 // The sink type.  This is the type of the blank identifier _.  Any
2785 // type may be assigned to it.
2786
2787 class Sink_type : public Type
2788 {
2789  public:
2790   Sink_type()
2791     : Type(TYPE_SINK)
2792   { }
2793
2794  protected:
2795   bool
2796   do_compare_is_identity(Gogo*) const
2797   { return false; }
2798
2799   Btype*
2800   do_get_backend(Gogo*)
2801   { go_unreachable(); }
2802
2803   Expression*
2804   do_type_descriptor(Gogo*, Named_type*)
2805   { go_unreachable(); }
2806
2807   void
2808   do_reflection(Gogo*, std::string*) const
2809   { go_unreachable(); }
2810
2811   void
2812   do_mangled_name(Gogo*, std::string*) const
2813   { go_unreachable(); }
2814 };
2815
2816 // Make the sink type.
2817
2818 Type*
2819 Type::make_sink_type()
2820 {
2821   static Sink_type sink_type;
2822   return &sink_type;
2823 }
2824
2825 // Class Function_type.
2826
2827 // Traversal.
2828
2829 int
2830 Function_type::do_traverse(Traverse* traverse)
2831 {
2832   if (this->receiver_ != NULL
2833       && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
2834     return TRAVERSE_EXIT;
2835   if (this->parameters_ != NULL
2836       && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
2837     return TRAVERSE_EXIT;
2838   if (this->results_ != NULL
2839       && this->results_->traverse(traverse) == TRAVERSE_EXIT)
2840     return TRAVERSE_EXIT;
2841   return TRAVERSE_CONTINUE;
2842 }
2843
2844 // Returns whether T is a valid redeclaration of this type.  If this
2845 // returns false, and REASON is not NULL, *REASON may be set to a
2846 // brief explanation of why it returned false.
2847
2848 bool
2849 Function_type::is_valid_redeclaration(const Function_type* t,
2850                                       std::string* reason) const
2851 {
2852   if (!this->is_identical(t, false, true, reason))
2853     return false;
2854
2855   // A redeclaration of a function is required to use the same names
2856   // for the receiver and parameters.
2857   if (this->receiver() != NULL
2858       && this->receiver()->name() != t->receiver()->name()
2859       && this->receiver()->name() != Import::import_marker
2860       && t->receiver()->name() != Import::import_marker)
2861     {
2862       if (reason != NULL)
2863         *reason = "receiver name changed";
2864       return false;
2865     }
2866
2867   const Typed_identifier_list* parms1 = this->parameters();
2868   const Typed_identifier_list* parms2 = t->parameters();
2869   if (parms1 != NULL)
2870     {
2871       Typed_identifier_list::const_iterator p1 = parms1->begin();
2872       for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2873            p2 != parms2->end();
2874            ++p2, ++p1)
2875         {
2876           if (p1->name() != p2->name()
2877               && p1->name() != Import::import_marker
2878               && p2->name() != Import::import_marker)
2879             {
2880               if (reason != NULL)
2881                 *reason = "parameter name changed";
2882               return false;
2883             }
2884
2885           // This is called at parse time, so we may have unknown
2886           // types.
2887           Type* t1 = p1->type()->forwarded();
2888           Type* t2 = p2->type()->forwarded();
2889           if (t1 != t2
2890               && t1->forward_declaration_type() != NULL
2891               && (t2->forward_declaration_type() == NULL
2892                   || (t1->forward_declaration_type()->named_object()
2893                       != t2->forward_declaration_type()->named_object())))
2894             return false;
2895         }
2896     }
2897
2898   const Typed_identifier_list* results1 = this->results();
2899   const Typed_identifier_list* results2 = t->results();
2900   if (results1 != NULL)
2901     {
2902       Typed_identifier_list::const_iterator res1 = results1->begin();
2903       for (Typed_identifier_list::const_iterator res2 = results2->begin();
2904            res2 != results2->end();
2905            ++res2, ++res1)
2906         {
2907           if (res1->name() != res2->name()
2908               && res1->name() != Import::import_marker
2909               && res2->name() != Import::import_marker)
2910             {
2911               if (reason != NULL)
2912                 *reason = "result name changed";
2913               return false;
2914             }
2915
2916           // This is called at parse time, so we may have unknown
2917           // types.
2918           Type* t1 = res1->type()->forwarded();
2919           Type* t2 = res2->type()->forwarded();
2920           if (t1 != t2
2921               && t1->forward_declaration_type() != NULL
2922               && (t2->forward_declaration_type() == NULL
2923                   || (t1->forward_declaration_type()->named_object()
2924                       != t2->forward_declaration_type()->named_object())))
2925             return false;
2926         }
2927     }
2928
2929   return true;
2930 }
2931
2932 // Check whether T is the same as this type.
2933
2934 bool
2935 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
2936                             bool errors_are_identical,
2937                             std::string* reason) const
2938 {
2939   if (!ignore_receiver)
2940     {
2941       const Typed_identifier* r1 = this->receiver();
2942       const Typed_identifier* r2 = t->receiver();
2943       if ((r1 != NULL) != (r2 != NULL))
2944         {
2945           if (reason != NULL)
2946             *reason = _("different receiver types");
2947           return false;
2948         }
2949       if (r1 != NULL)
2950         {
2951           if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
2952                                    reason))
2953             {
2954               if (reason != NULL && !reason->empty())
2955                 *reason = "receiver: " + *reason;
2956               return false;
2957             }
2958         }
2959     }
2960
2961   const Typed_identifier_list* parms1 = this->parameters();
2962   const Typed_identifier_list* parms2 = t->parameters();
2963   if ((parms1 != NULL) != (parms2 != NULL))
2964     {
2965       if (reason != NULL)
2966         *reason = _("different number of parameters");
2967       return false;
2968     }
2969   if (parms1 != NULL)
2970     {
2971       Typed_identifier_list::const_iterator p1 = parms1->begin();
2972       for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2973            p2 != parms2->end();
2974            ++p2, ++p1)
2975         {
2976           if (p1 == parms1->end())
2977             {
2978               if (reason != NULL)
2979                 *reason = _("different number of parameters");
2980               return false;
2981             }
2982
2983           if (!Type::are_identical(p1->type(), p2->type(),
2984                                    errors_are_identical, NULL))
2985             {
2986               if (reason != NULL)
2987                 *reason = _("different parameter types");
2988               return false;
2989             }
2990         }
2991       if (p1 != parms1->end())
2992         {
2993           if (reason != NULL)
2994             *reason = _("different number of parameters");
2995         return false;
2996         }
2997     }
2998
2999   if (this->is_varargs() != t->is_varargs())
3000     {
3001       if (reason != NULL)
3002         *reason = _("different varargs");
3003       return false;
3004     }
3005
3006   const Typed_identifier_list* results1 = this->results();
3007   const Typed_identifier_list* results2 = t->results();
3008   if ((results1 != NULL) != (results2 != NULL))
3009     {
3010       if (reason != NULL)
3011         *reason = _("different number of results");
3012       return false;
3013     }
3014   if (results1 != NULL)
3015     {
3016       Typed_identifier_list::const_iterator res1 = results1->begin();
3017       for (Typed_identifier_list::const_iterator res2 = results2->begin();
3018            res2 != results2->end();
3019            ++res2, ++res1)
3020         {
3021           if (res1 == results1->end())
3022             {
3023               if (reason != NULL)
3024                 *reason = _("different number of results");
3025               return false;
3026             }
3027
3028           if (!Type::are_identical(res1->type(), res2->type(),
3029                                    errors_are_identical, NULL))
3030             {
3031               if (reason != NULL)
3032                 *reason = _("different result types");
3033               return false;
3034             }
3035         }
3036       if (res1 != results1->end())
3037         {
3038           if (reason != NULL)
3039             *reason = _("different number of results");
3040           return false;
3041         }
3042     }
3043
3044   return true;
3045 }
3046
3047 // Hash code.
3048
3049 unsigned int
3050 Function_type::do_hash_for_method(Gogo* gogo) const
3051 {
3052   unsigned int ret = 0;
3053   // We ignore the receiver type for hash codes, because we need to
3054   // get the same hash code for a method in an interface and a method
3055   // declared for a type.  The former will not have a receiver.
3056   if (this->parameters_ != NULL)
3057     {
3058       int shift = 1;
3059       for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3060            p != this->parameters_->end();
3061            ++p, ++shift)
3062         ret += p->type()->hash_for_method(gogo) << shift;
3063     }
3064   if (this->results_ != NULL)
3065     {
3066       int shift = 2;
3067       for (Typed_identifier_list::const_iterator p = this->results_->begin();
3068            p != this->results_->end();
3069            ++p, ++shift)
3070         ret += p->type()->hash_for_method(gogo) << shift;
3071     }
3072   if (this->is_varargs_)
3073     ret += 1;
3074   ret <<= 4;
3075   return ret;
3076 }
3077
3078 // Get the backend representation for a function type.
3079
3080 Btype*
3081 Function_type::get_function_backend(Gogo* gogo)
3082 {
3083   Backend::Btyped_identifier breceiver;
3084   if (this->receiver_ != NULL)
3085     {
3086       breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
3087
3088       // We always pass the address of the receiver parameter, in
3089       // order to make interface calls work with unknown types.
3090       Type* rtype = this->receiver_->type();
3091       if (rtype->points_to() == NULL)
3092         rtype = Type::make_pointer_type(rtype);
3093       breceiver.btype = rtype->get_backend(gogo);
3094       breceiver.location = this->receiver_->location();
3095     }
3096
3097   std::vector<Backend::Btyped_identifier> bparameters;
3098   if (this->parameters_ != NULL)
3099     {
3100       bparameters.resize(this->parameters_->size());
3101       size_t i = 0;
3102       for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3103            p != this->parameters_->end();
3104            ++p, ++i)
3105         {
3106           bparameters[i].name = Gogo::unpack_hidden_name(p->name());
3107           bparameters[i].btype = p->type()->get_backend(gogo);
3108           bparameters[i].location = p->location();
3109         }
3110       go_assert(i == bparameters.size());
3111     }
3112
3113   std::vector<Backend::Btyped_identifier> bresults;
3114   if (this->results_ != NULL)
3115     {
3116       bresults.resize(this->results_->size());
3117       size_t i = 0;
3118       for (Typed_identifier_list::const_iterator p = this->results_->begin();
3119            p != this->results_->end();
3120            ++p, ++i)
3121         {
3122           bresults[i].name = Gogo::unpack_hidden_name(p->name());
3123           bresults[i].btype = p->type()->get_backend(gogo);
3124           bresults[i].location = p->location();
3125         }
3126       go_assert(i == bresults.size());
3127     }
3128
3129   return gogo->backend()->function_type(breceiver, bparameters, bresults,
3130                                         this->location());
3131 }
3132
3133 // A hash table mapping function types to their backend placeholders.
3134
3135 Function_type::Placeholders Function_type::placeholders;
3136
3137 // Get the backend representation for a function type.  If we are
3138 // still converting types, and this types has multiple results, return
3139 // a placeholder instead.  We do this because for multiple results we
3140 // build a struct, and we need to make sure that all the types in the
3141 // struct are valid before we create the struct.
3142
3143 Btype*
3144 Function_type::do_get_backend(Gogo* gogo)
3145 {
3146   if (!gogo->named_types_are_converted()
3147       && this->results_ != NULL
3148       && this->results_->size() > 1)
3149     {
3150       Btype* placeholder =
3151         gogo->backend()->placeholder_pointer_type("", this->location(), true);
3152       Function_type::placeholders.push_back(std::make_pair(this, placeholder));
3153       return placeholder;
3154     }
3155   return this->get_function_backend(gogo);
3156 }
3157
3158 // Convert function types after all named types are converted.
3159
3160 void
3161 Function_type::convert_types(Gogo* gogo)
3162 {
3163   for (Placeholders::const_iterator p = Function_type::placeholders.begin();
3164        p != Function_type::placeholders.end();
3165        ++p)
3166     {
3167       Btype* bt = p->first->get_function_backend(gogo);
3168       if (!gogo->backend()->set_placeholder_function_type(p->second, bt))
3169         go_assert(saw_errors());
3170     }
3171 }
3172
3173 // The type of a function type descriptor.
3174
3175 Type*
3176 Function_type::make_function_type_descriptor_type()
3177 {
3178   static Type* ret;
3179   if (ret == NULL)
3180     {
3181       Type* tdt = Type::make_type_descriptor_type();
3182       Type* ptdt = Type::make_type_descriptor_ptr_type();
3183
3184       Type* bool_type = Type::lookup_bool_type();
3185
3186       Type* slice_type = Type::make_array_type(ptdt, NULL);
3187
3188       Struct_type* s = Type::make_builtin_struct_type(4,
3189                                                       "", tdt,
3190                                                       "dotdotdot", bool_type,
3191                                                       "in", slice_type,
3192                                                       "out", slice_type);
3193
3194       ret = Type::make_builtin_named_type("FuncType", s);
3195     }
3196
3197   return ret;
3198 }
3199
3200 // The type descriptor for a function type.
3201
3202 Expression*
3203 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3204 {
3205   Location bloc = Linemap::predeclared_location();
3206
3207   Type* ftdt = Function_type::make_function_type_descriptor_type();
3208
3209   const Struct_field_list* fields = ftdt->struct_type()->fields();
3210
3211   Expression_list* vals = new Expression_list();
3212   vals->reserve(4);
3213
3214   Struct_field_list::const_iterator p = fields->begin();
3215   go_assert(p->is_field_name("commonType"));
3216   vals->push_back(this->type_descriptor_constructor(gogo,
3217                                                     RUNTIME_TYPE_KIND_FUNC,
3218                                                     name, NULL, true));
3219
3220   ++p;
3221   go_assert(p->is_field_name("dotdotdot"));
3222   vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
3223
3224   ++p;
3225   go_assert(p->is_field_name("in"));
3226   vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
3227                                                this->parameters()));
3228
3229   ++p;
3230   go_assert(p->is_field_name("out"));
3231   vals->push_back(this->type_descriptor_params(p->type(), NULL,
3232                                                this->results()));
3233
3234   ++p;
3235   go_assert(p == fields->end());
3236
3237   return Expression::make_struct_composite_literal(ftdt, vals, bloc);
3238 }
3239
3240 // Return a composite literal for the parameters or results of a type
3241 // descriptor.
3242
3243 Expression*
3244 Function_type::type_descriptor_params(Type* params_type,
3245                                       const Typed_identifier* receiver,
3246                                       const Typed_identifier_list* params)
3247 {
3248   Location bloc = Linemap::predeclared_location();
3249
3250   if (receiver == NULL && params == NULL)
3251     return Expression::make_slice_composite_literal(params_type, NULL, bloc);
3252
3253   Expression_list* vals = new Expression_list();
3254   vals->reserve((params == NULL ? 0 : params->size())
3255                 + (receiver != NULL ? 1 : 0));
3256
3257   if (receiver != NULL)
3258     vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
3259
3260   if (params != NULL)
3261     {
3262       for (Typed_identifier_list::const_iterator p = params->begin();
3263            p != params->end();
3264            ++p)
3265         vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
3266     }
3267
3268   return Expression::make_slice_composite_literal(params_type, vals, bloc);
3269 }
3270
3271 // The reflection string.
3272
3273 void
3274 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
3275 {
3276   // FIXME: Turn this off until we straighten out the type of the
3277   // struct field used in a go statement which calls a method.
3278   // go_assert(this->receiver_ == NULL);
3279
3280   ret->append("func");
3281
3282   if (this->receiver_ != NULL)
3283     {
3284       ret->push_back('(');
3285       this->append_reflection(this->receiver_->type(), gogo, ret);
3286       ret->push_back(')');
3287     }
3288
3289   ret->push_back('(');
3290   const Typed_identifier_list* params = this->parameters();
3291   if (params != NULL)
3292     {
3293       bool is_varargs = this->is_varargs_;
3294       for (Typed_identifier_list::const_iterator p = params->begin();
3295            p != params->end();
3296            ++p)
3297         {
3298           if (p != params->begin())
3299             ret->append(", ");
3300           if (!is_varargs || p + 1 != params->end())
3301             this->append_reflection(p->type(), gogo, ret);
3302           else
3303             {
3304               ret->append("...");
3305               this->append_reflection(p->type()->array_type()->element_type(),
3306                                       gogo, ret);
3307             }
3308         }
3309     }
3310   ret->push_back(')');
3311
3312   const Typed_identifier_list* results = this->results();
3313   if (results != NULL && !results->empty())
3314     {
3315       if (results->size() == 1)
3316         ret->push_back(' ');
3317       else
3318         ret->append(" (");
3319       for (Typed_identifier_list::const_iterator p = results->begin();
3320            p != results->end();
3321            ++p)
3322         {
3323           if (p != results->begin())
3324             ret->append(", ");
3325           this->append_reflection(p->type(), gogo, ret);
3326         }
3327       if (results->size() > 1)
3328         ret->push_back(')');
3329     }
3330 }
3331
3332 // Mangled name.
3333
3334 void
3335 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3336 {
3337   ret->push_back('F');
3338
3339   if (this->receiver_ != NULL)
3340     {
3341       ret->push_back('m');
3342       this->append_mangled_name(this->receiver_->type(), gogo, ret);
3343     }
3344
3345   const Typed_identifier_list* params = this->parameters();
3346   if (params != NULL)
3347     {
3348       ret->push_back('p');
3349       for (Typed_identifier_list::const_iterator p = params->begin();
3350            p != params->end();
3351            ++p)
3352         this->append_mangled_name(p->type(), gogo, ret);
3353       if (this->is_varargs_)
3354         ret->push_back('V');
3355       ret->push_back('e');
3356     }
3357
3358   const Typed_identifier_list* results = this->results();
3359   if (results != NULL)
3360     {
3361       ret->push_back('r');
3362       for (Typed_identifier_list::const_iterator p = results->begin();
3363            p != results->end();
3364            ++p)
3365         this->append_mangled_name(p->type(), gogo, ret);
3366       ret->push_back('e');
3367     }
3368
3369   ret->push_back('e');
3370 }
3371
3372 // Export a function type.
3373
3374 void
3375 Function_type::do_export(Export* exp) const
3376 {
3377   // We don't write out the receiver.  The only function types which
3378   // should have a receiver are the ones associated with explicitly
3379   // defined methods.  For those the receiver type is written out by
3380   // Function::export_func.
3381
3382   exp->write_c_string("(");
3383   bool first = true;
3384   if (this->parameters_ != NULL)
3385     {
3386       bool is_varargs = this->is_varargs_;
3387       for (Typed_identifier_list::const_iterator p =
3388              this->parameters_->begin();
3389            p != this->parameters_->end();
3390            ++p)
3391         {
3392           if (first)
3393             first = false;
3394           else
3395             exp->write_c_string(", ");
3396           if (!is_varargs || p + 1 != this->parameters_->end())
3397             exp->write_type(p->type());
3398           else
3399             {
3400               exp->write_c_string("...");
3401               exp->write_type(p->type()->array_type()->element_type());
3402             }
3403         }
3404     }
3405   exp->write_c_string(")");
3406
3407   const Typed_identifier_list* results = this->results_;
3408   if (results != NULL)
3409     {
3410       exp->write_c_string(" ");
3411       if (results->size() == 1)
3412         exp->write_type(results->begin()->type());
3413       else
3414         {
3415           first = true;
3416           exp->write_c_string("(");
3417           for (Typed_identifier_list::const_iterator p = results->begin();
3418                p != results->end();
3419                ++p)
3420             {
3421               if (first)
3422                 first = false;
3423               else
3424                 exp->write_c_string(", ");
3425               exp->write_type(p->type());
3426             }
3427           exp->write_c_string(")");
3428         }
3429     }
3430 }
3431
3432 // Import a function type.
3433
3434 Function_type*
3435 Function_type::do_import(Import* imp)
3436 {
3437   imp->require_c_string("(");
3438   Typed_identifier_list* parameters;
3439   bool is_varargs = false;
3440   if (imp->peek_char() == ')')
3441     parameters = NULL;
3442   else
3443     {
3444       parameters = new Typed_identifier_list();
3445       while (true)
3446         {
3447           if (imp->match_c_string("..."))
3448             {
3449               imp->advance(3);
3450               is_varargs = true;
3451             }
3452
3453           Type* ptype = imp->read_type();
3454           if (is_varargs)
3455             ptype = Type::make_array_type(ptype, NULL);
3456           parameters->push_back(Typed_identifier(Import::import_marker,
3457                                                  ptype, imp->location()));
3458           if (imp->peek_char() != ',')
3459             break;
3460           go_assert(!is_varargs);
3461           imp->require_c_string(", ");
3462         }
3463     }
3464   imp->require_c_string(")");
3465
3466   Typed_identifier_list* results;
3467   if (imp->peek_char() != ' ')
3468     results = NULL;
3469   else
3470     {
3471       imp->advance(1);
3472       results = new Typed_identifier_list;
3473       if (imp->peek_char() != '(')
3474         {
3475           Type* rtype = imp->read_type();
3476           results->push_back(Typed_identifier(Import::import_marker, rtype,
3477                                               imp->location()));
3478         }
3479       else
3480         {
3481           imp->advance(1);
3482           while (true)
3483             {
3484               Type* rtype = imp->read_type();
3485               results->push_back(Typed_identifier(Import::import_marker,
3486                                                   rtype, imp->location()));
3487               if (imp->peek_char() != ',')
3488                 break;
3489               imp->require_c_string(", ");
3490             }
3491           imp->require_c_string(")");
3492         }
3493     }
3494
3495   Function_type* ret = Type::make_function_type(NULL, parameters, results,
3496                                                 imp->location());
3497   if (is_varargs)
3498     ret->set_is_varargs();
3499   return ret;
3500 }
3501
3502 // Make a copy of a function type without a receiver.
3503
3504 Function_type*
3505 Function_type::copy_without_receiver() const
3506 {
3507   go_assert(this->is_method());
3508   Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3509                                                 this->results_,
3510                                                 this->location_);
3511   if (this->is_varargs())
3512     ret->set_is_varargs();
3513   if (this->is_builtin())
3514     ret->set_is_builtin();
3515   return ret;
3516 }
3517
3518 // Make a copy of a function type with a receiver.
3519
3520 Function_type*
3521 Function_type::copy_with_receiver(Type* receiver_type) const
3522 {
3523   go_assert(!this->is_method());
3524   Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3525                                                     this->location_);
3526   return Type::make_function_type(receiver, this->parameters_,
3527                                   this->results_, this->location_);
3528 }
3529
3530 // Make a function type.
3531
3532 Function_type*
3533 Type::make_function_type(Typed_identifier* receiver,
3534                          Typed_identifier_list* parameters,
3535                          Typed_identifier_list* results,
3536                          Location location)
3537 {
3538   return new Function_type(receiver, parameters, results, location);
3539 }
3540
3541 // Class Pointer_type.
3542
3543 // Traversal.
3544
3545 int
3546 Pointer_type::do_traverse(Traverse* traverse)
3547 {
3548   return Type::traverse(this->to_type_, traverse);
3549 }
3550
3551 // Hash code.
3552
3553 unsigned int
3554 Pointer_type::do_hash_for_method(Gogo* gogo) const
3555 {
3556   return this->to_type_->hash_for_method(gogo) << 4;
3557 }
3558
3559 // The tree for a pointer type.
3560
3561 Btype*
3562 Pointer_type::do_get_backend(Gogo* gogo)
3563 {
3564   Btype* to_btype = this->to_type_->get_backend(gogo);
3565   return gogo->backend()->pointer_type(to_btype);
3566 }
3567
3568 // The type of a pointer type descriptor.
3569
3570 Type*
3571 Pointer_type::make_pointer_type_descriptor_type()
3572 {
3573   static Type* ret;
3574   if (ret == NULL)
3575     {
3576       Type* tdt = Type::make_type_descriptor_type();
3577       Type* ptdt = Type::make_type_descriptor_ptr_type();
3578
3579       Struct_type* s = Type::make_builtin_struct_type(2,
3580                                                       "", tdt,
3581                                                       "elem", ptdt);
3582
3583       ret = Type::make_builtin_named_type("PtrType", s);
3584     }
3585
3586   return ret;
3587 }
3588
3589 // The type descriptor for a pointer type.
3590
3591 Expression*
3592 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3593 {
3594   if (this->is_unsafe_pointer_type())
3595     {
3596       go_assert(name != NULL);
3597       return this->plain_type_descriptor(gogo,
3598                                          RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3599                                          name);
3600     }
3601   else
3602     {
3603       Location bloc = Linemap::predeclared_location();
3604
3605       const Methods* methods;
3606       Type* deref = this->points_to();
3607       if (deref->named_type() != NULL)
3608         methods = deref->named_type()->methods();
3609       else if (deref->struct_type() != NULL)
3610         methods = deref->struct_type()->methods();
3611       else
3612         methods = NULL;
3613
3614       Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3615
3616       const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3617
3618       Expression_list* vals = new Expression_list();
3619       vals->reserve(2);
3620
3621       Struct_field_list::const_iterator p = fields->begin();
3622       go_assert(p->is_field_name("commonType"));
3623       vals->push_back(this->type_descriptor_constructor(gogo,
3624                                                         RUNTIME_TYPE_KIND_PTR,
3625                                                         name, methods, false));
3626
3627       ++p;
3628       go_assert(p->is_field_name("elem"));
3629       vals->push_back(Expression::make_type_descriptor(deref, bloc));
3630
3631       return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3632     }
3633 }
3634
3635 // Reflection string.
3636
3637 void
3638 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3639 {
3640   ret->push_back('*');
3641   this->append_reflection(this->to_type_, gogo, ret);
3642 }
3643
3644 // Mangled name.
3645
3646 void
3647 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3648 {
3649   ret->push_back('p');
3650   this->append_mangled_name(this->to_type_, gogo, ret);
3651 }
3652
3653 // Export.
3654
3655 void
3656 Pointer_type::do_export(Export* exp) const
3657 {
3658   exp->write_c_string("*");
3659   if (this->is_unsafe_pointer_type())
3660     exp->write_c_string("any");
3661   else
3662     exp->write_type(this->to_type_);
3663 }
3664
3665 // Import.
3666
3667 Pointer_type*
3668 Pointer_type::do_import(Import* imp)
3669 {
3670   imp->require_c_string("*");
3671   if (imp->match_c_string("any"))
3672     {
3673       imp->advance(3);
3674       return Type::make_pointer_type(Type::make_void_type());
3675     }
3676   Type* to = imp->read_type();
3677   return Type::make_pointer_type(to);
3678 }
3679
3680 // Make a pointer type.
3681
3682 Pointer_type*
3683 Type::make_pointer_type(Type* to_type)
3684 {
3685   typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3686   static Hashtable pointer_types;
3687   Hashtable::const_iterator p = pointer_types.find(to_type);
3688   if (p != pointer_types.end())
3689     return p->second;
3690   Pointer_type* ret = new Pointer_type(to_type);
3691   pointer_types[to_type] = ret;
3692   return ret;
3693 }
3694
3695 // The nil type.  We use a special type for nil because it is not the
3696 // same as any other type.  In C term nil has type void*, but there is
3697 // no such type in Go.
3698
3699 class Nil_type : public Type
3700 {
3701  public:
3702   Nil_type()
3703     : Type(TYPE_NIL)
3704   { }
3705
3706  protected:
3707   bool
3708   do_compare_is_identity(Gogo*) const
3709   { return false; }
3710
3711   Btype*
3712   do_get_backend(Gogo* gogo)
3713   { return gogo->backend()->pointer_type(gogo->backend()->void_type()); }
3714
3715   Expression*
3716   do_type_descriptor(Gogo*, Named_type*)
3717   { go_unreachable(); }
3718
3719   void
3720   do_reflection(Gogo*, std::string*) const
3721   { go_unreachable(); }
3722
3723   void
3724   do_mangled_name(Gogo*, std::string* ret) const
3725   { ret->push_back('n'); }
3726 };
3727
3728 // Make the nil type.
3729
3730 Type*
3731 Type::make_nil_type()
3732 {
3733   static Nil_type singleton_nil_type;
3734   return &singleton_nil_type;
3735 }
3736
3737 // The type of a function call which returns multiple values.  This is
3738 // really a struct, but we don't want to confuse a function call which
3739 // returns a struct with a function call which returns multiple
3740 // values.
3741
3742 class Call_multiple_result_type : public Type
3743 {
3744  public:
3745   Call_multiple_result_type(Call_expression* call)
3746     : Type(TYPE_CALL_MULTIPLE_RESULT),
3747       call_(call)
3748   { }
3749
3750  protected:
3751   bool
3752   do_has_pointer() const
3753   {
3754     go_assert(saw_errors());
3755     return false;
3756   }
3757
3758   bool
3759   do_compare_is_identity(Gogo*) const
3760   { return false; }
3761
3762   Btype*
3763   do_get_backend(Gogo* gogo)
3764   {
3765     go_assert(saw_errors());
3766     return gogo->backend()->error_type();
3767   }
3768
3769   Expression*
3770   do_type_descriptor(Gogo*, Named_type*)
3771   {
3772     go_assert(saw_errors());
3773     return Expression::make_error(Linemap::unknown_location());
3774   }
3775
3776   void
3777   do_reflection(Gogo*, std::string*) const
3778   { go_assert(saw_errors()); }
3779
3780   void
3781   do_mangled_name(Gogo*, std::string*) const
3782   { go_assert(saw_errors()); }
3783
3784  private:
3785   // The expression being called.
3786   Call_expression* call_;
3787 };
3788
3789 // Make a call result type.
3790
3791 Type*
3792 Type::make_call_multiple_result_type(Call_expression* call)
3793 {
3794   return new Call_multiple_result_type(call);
3795 }
3796
3797 // Class Struct_field.
3798
3799 // Get the name of a field.
3800
3801 const std::string&
3802 Struct_field::field_name() const
3803 {
3804   const std::string& name(this->typed_identifier_.name());
3805   if (!name.empty())
3806     return name;
3807   else
3808     {
3809       // This is called during parsing, before anything is lowered, so
3810       // we have to be pretty careful to avoid dereferencing an
3811       // unknown type name.
3812       Type* t = this->typed_identifier_.type();
3813       Type* dt = t;
3814       if (t->classification() == Type::TYPE_POINTER)
3815         {
3816           // Very ugly.
3817           Pointer_type* ptype = static_cast<Pointer_type*>(t);
3818           dt = ptype->points_to();
3819         }
3820       if (dt->forward_declaration_type() != NULL)
3821         return dt->forward_declaration_type()->name();
3822       else if (dt->named_type() != NULL)
3823         return dt->named_type()->name();
3824       else if (t->is_error_type() || dt->is_error_type())
3825         {
3826           static const std::string error_string = "*error*";
3827           return error_string;
3828         }
3829       else
3830         {
3831           // Avoid crashing in the erroneous case where T is named but
3832           // DT is not.
3833           go_assert(t != dt);
3834           if (t->forward_declaration_type() != NULL)
3835             return t->forward_declaration_type()->name();
3836           else if (t->named_type() != NULL)
3837             return t->named_type()->name();
3838           else
3839             go_unreachable();
3840         }
3841     }
3842 }
3843
3844 // Return whether this field is named NAME.
3845
3846 bool
3847 Struct_field::is_field_name(const std::string& name) const
3848 {
3849   const std::string& me(this->typed_identifier_.name());
3850   if (!me.empty())
3851     return me == name;
3852   else
3853     {
3854       Type* t = this->typed_identifier_.type();
3855       if (t->points_to() != NULL)
3856         t = t->points_to();
3857       Named_type* nt = t->named_type();
3858       if (nt != NULL && nt->name() == name)
3859         return true;
3860
3861       // This is a horrible hack caused by the fact that we don't pack
3862       // the names of builtin types.  FIXME.
3863       if (nt != NULL
3864           && nt->is_builtin()
3865           && nt->name() == Gogo::unpack_hidden_name(name))
3866         return true;
3867
3868       return false;
3869     }
3870 }
3871
3872 // Class Struct_type.
3873
3874 // Traversal.
3875
3876 int
3877 Struct_type::do_traverse(Traverse* traverse)
3878 {
3879   Struct_field_list* fields = this->fields_;
3880   if (fields != NULL)
3881     {
3882       for (Struct_field_list::iterator p = fields->begin();
3883            p != fields->end();
3884            ++p)
3885         {
3886           if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
3887             return TRAVERSE_EXIT;
3888         }
3889     }
3890   return TRAVERSE_CONTINUE;
3891 }
3892
3893 // Verify that the struct type is complete and valid.
3894
3895 bool
3896 Struct_type::do_verify()
3897 {
3898   Struct_field_list* fields = this->fields_;
3899   if (fields == NULL)
3900     return true;
3901   bool ret = true;
3902   for (Struct_field_list::iterator p = fields->begin();
3903        p != fields->end();
3904        ++p)
3905     {
3906       Type* t = p->type();
3907       if (t->is_undefined())
3908         {
3909           error_at(p->location(), "struct field type is incomplete");
3910           p->set_type(Type::make_error_type());
3911           ret = false;
3912         }
3913       else if (p->is_anonymous())
3914         {
3915           if (t->named_type() != NULL && t->points_to() != NULL)
3916             {
3917               error_at(p->location(), "embedded type may not be a pointer");
3918               p->set_type(Type::make_error_type());
3919               return false;
3920             }
3921           if (t->points_to() != NULL
3922               && t->points_to()->interface_type() != NULL)
3923             {
3924               error_at(p->location(),
3925                        "embedded type may not be pointer to interface");
3926               p->set_type(Type::make_error_type());
3927               return false;
3928             }
3929         }
3930     }
3931   return ret;
3932 }
3933
3934 // Whether this contains a pointer.
3935
3936 bool
3937 Struct_type::do_has_pointer() const
3938 {
3939   const Struct_field_list* fields = this->fields();
3940   if (fields == NULL)
3941     return false;
3942   for (Struct_field_list::const_iterator p = fields->begin();
3943        p != fields->end();
3944        ++p)
3945     {
3946       if (p->type()->has_pointer())
3947         return true;
3948     }
3949   return false;
3950 }
3951
3952 // Whether this type is identical to T.
3953
3954 bool
3955 Struct_type::is_identical(const Struct_type* t,
3956                           bool errors_are_identical) const
3957 {
3958   const Struct_field_list* fields1 = this->fields();
3959   const Struct_field_list* fields2 = t->fields();
3960   if (fields1 == NULL || fields2 == NULL)
3961     return fields1 == fields2;
3962   Struct_field_list::const_iterator pf2 = fields2->begin();
3963   for (Struct_field_list::const_iterator pf1 = fields1->begin();
3964        pf1 != fields1->end();
3965        ++pf1, ++pf2)
3966     {
3967       if (pf2 == fields2->end())
3968         return false;
3969       if (pf1->field_name() != pf2->field_name())
3970         return false;
3971       if (pf1->is_anonymous() != pf2->is_anonymous()
3972           || !Type::are_identical(pf1->type(), pf2->type(),
3973                                   errors_are_identical, NULL))
3974         return false;
3975       if (!pf1->has_tag())
3976         {
3977           if (pf2->has_tag())
3978             return false;
3979         }
3980       else
3981         {
3982           if (!pf2->has_tag())
3983             return false;
3984           if (pf1->tag() != pf2->tag())
3985             return false;
3986         }
3987     }
3988   if (pf2 != fields2->end())
3989     return false;
3990   return true;
3991 }
3992
3993 // Whether this struct type has any hidden fields.
3994
3995 bool
3996 Struct_type::struct_has_hidden_fields(const Named_type* within,
3997                                       std::string* reason) const
3998 {
3999   const Struct_field_list* fields = this->fields();
4000   if (fields == NULL)
4001     return false;
4002   const Package* within_package = (within == NULL
4003                                    ? NULL
4004                                    : within->named_object()->package());
4005   for (Struct_field_list::const_iterator pf = fields->begin();
4006        pf != fields->end();
4007        ++pf)
4008     {
4009       if (within_package != NULL
4010           && !pf->is_anonymous()
4011           && Gogo::is_hidden_name(pf->field_name()))
4012         {
4013           if (reason != NULL)
4014             {
4015               std::string within_name = within->named_object()->message_name();
4016               std::string name = Gogo::message_name(pf->field_name());
4017               size_t bufsize = 200 + within_name.length() + name.length();
4018               char* buf = new char[bufsize];
4019               snprintf(buf, bufsize,
4020                        _("implicit assignment of %s%s%s hidden field %s%s%s"),
4021                        open_quote, within_name.c_str(), close_quote,
4022                        open_quote, name.c_str(), close_quote);
4023               reason->assign(buf);
4024               delete[] buf;
4025             }
4026           return true;
4027         }
4028
4029       if (pf->type()->has_hidden_fields(within, reason))
4030         return true;
4031     }
4032
4033   return false;
4034 }
4035
4036 // Whether comparisons of this struct type are simple identity
4037 // comparisons.
4038
4039 bool
4040 Struct_type::do_compare_is_identity(Gogo* gogo) const
4041 {
4042   const Struct_field_list* fields = this->fields_;
4043   if (fields == NULL)
4044     return true;
4045   unsigned int offset = 0;
4046   for (Struct_field_list::const_iterator pf = fields->begin();
4047        pf != fields->end();
4048        ++pf)
4049     {
4050       if (!pf->type()->compare_is_identity(gogo))
4051         return false;
4052
4053       unsigned int field_align;
4054       if (!pf->type()->backend_type_align(gogo, &field_align))
4055         return false;
4056       if ((offset & (field_align - 1)) != 0)
4057         {
4058           // This struct has padding.  We don't guarantee that that
4059           // padding is zero-initialized for a stack variable, so we
4060           // can't use memcmp to compare struct values.
4061           return false;
4062         }
4063
4064       unsigned int field_size;
4065       if (!pf->type()->backend_type_size(gogo, &field_size))
4066         return false;
4067       offset += field_size;
4068     }
4069   return true;
4070 }
4071
4072 // Build identity and hash functions for this struct.
4073
4074 // Hash code.
4075
4076 unsigned int
4077 Struct_type::do_hash_for_method(Gogo* gogo) const
4078 {
4079   unsigned int ret = 0;
4080   if (this->fields() != NULL)
4081     {
4082       for (Struct_field_list::const_iterator pf = this->fields()->begin();
4083            pf != this->fields()->end();
4084            ++pf)
4085         ret = (ret << 1) + pf->type()->hash_for_method(gogo);
4086     }
4087   return ret <<= 2;
4088 }
4089
4090 // Find the local field NAME.
4091
4092 const Struct_field*
4093 Struct_type::find_local_field(const std::string& name,
4094                               unsigned int *pindex) const
4095 {
4096   const Struct_field_list* fields = this->fields_;
4097   if (fields == NULL)
4098     return NULL;
4099   unsigned int i = 0;
4100   for (Struct_field_list::const_iterator pf = fields->begin();
4101        pf != fields->end();
4102        ++pf, ++i)
4103     {
4104       if (pf->is_field_name(name))
4105         {
4106           if (pindex != NULL)
4107             *pindex = i;
4108           return &*pf;
4109         }
4110     }
4111   return NULL;
4112 }
4113
4114 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
4115
4116 Field_reference_expression*
4117 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
4118                              Location location) const
4119 {
4120   unsigned int depth;
4121   return this->field_reference_depth(struct_expr, name, location, NULL,
4122                                      &depth);
4123 }
4124
4125 // Return an expression for a field, along with the depth at which it
4126 // was found.
4127
4128 Field_reference_expression*
4129 Struct_type::field_reference_depth(Expression* struct_expr,
4130                                    const std::string& name,
4131                                    Location location,
4132                                    Saw_named_type* saw,
4133                                    unsigned int* depth) const
4134 {
4135   const Struct_field_list* fields = this->fields_;
4136   if (fields == NULL)
4137     return NULL;
4138
4139   // Look for a field with this name.
4140   unsigned int i = 0;
4141   for (Struct_field_list::const_iterator pf = fields->begin();
4142        pf != fields->end();
4143        ++pf, ++i)
4144     {
4145       if (pf->is_field_name(name))
4146         {
4147           *depth = 0;
4148           return Expression::make_field_reference(struct_expr, i, location);
4149         }
4150     }
4151
4152   // Look for an anonymous field which contains a field with this
4153   // name.
4154   unsigned int found_depth = 0;