OSDN Git Service

d93b68bfc109939ccab76b6a903ce0f97f8a2d16
[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()->element_type()->is_comparable())
575             {
576               if (reason != NULL)
577                 *reason = _("invalid comparison of non-comparable array");
578               return false;
579             }
580         }
581     }
582
583   return true;
584 }
585
586 // Return true if a value with type RHS may be assigned to a variable
587 // with type LHS.  If CHECK_HIDDEN_FIELDS is true, check whether any
588 // hidden fields are modified.  If REASON is not NULL, set *REASON to
589 // the reason the types are not assignable.
590
591 bool
592 Type::are_assignable_check_hidden(const Type* lhs, const Type* rhs,
593                                   bool check_hidden_fields,
594                                   std::string* reason)
595 {
596   // Do some checks first.  Make sure the types are defined.
597   if (rhs != NULL
598       && rhs->forwarded()->forward_declaration_type() == NULL
599       && rhs->is_void_type())
600     {
601       if (reason != NULL)
602         *reason = "non-value used as value";
603       return false;
604     }
605
606   if (lhs != NULL && lhs->forwarded()->forward_declaration_type() == NULL)
607     {
608       // Any value may be assigned to the blank identifier.
609       if (lhs->is_sink_type())
610         return true;
611
612       // All fields of a struct must be exported, or the assignment
613       // must be in the same package.
614       if (check_hidden_fields
615           && rhs != NULL
616           && rhs->forwarded()->forward_declaration_type() == NULL)
617         {
618           if (lhs->has_hidden_fields(NULL, reason)
619               || rhs->has_hidden_fields(NULL, reason))
620             return false;
621         }
622     }
623
624   // Identical types are assignable.
625   if (Type::are_identical(lhs, rhs, true, reason))
626     return true;
627
628   // The types are assignable if they have identical underlying types
629   // and either LHS or RHS is not a named type.
630   if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
631        || (rhs->named_type() != NULL && lhs->named_type() == NULL))
632       && Type::are_identical(lhs->base(), rhs->base(), true, reason))
633     return true;
634
635   // The types are assignable if LHS is an interface type and RHS
636   // implements the required methods.
637   const Interface_type* lhs_interface_type = lhs->interface_type();
638   if (lhs_interface_type != NULL)
639     {
640       if (lhs_interface_type->implements_interface(rhs, reason))
641         return true;
642       const Interface_type* rhs_interface_type = rhs->interface_type();
643       if (rhs_interface_type != NULL
644           && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
645                                                           reason))
646         return true;
647     }
648
649   // The type are assignable if RHS is a bidirectional channel type,
650   // LHS is a channel type, they have identical element types, and
651   // either LHS or RHS is not a named type.
652   if (lhs->channel_type() != NULL
653       && rhs->channel_type() != NULL
654       && rhs->channel_type()->may_send()
655       && rhs->channel_type()->may_receive()
656       && (lhs->named_type() == NULL || rhs->named_type() == NULL)
657       && Type::are_identical(lhs->channel_type()->element_type(),
658                              rhs->channel_type()->element_type(),
659                              true,
660                              reason))
661     return true;
662
663   // The nil type may be assigned to a pointer, function, slice, map,
664   // channel, or interface type.
665   if (rhs->is_nil_type()
666       && (lhs->points_to() != NULL
667           || lhs->function_type() != NULL
668           || lhs->is_slice_type()
669           || lhs->map_type() != NULL
670           || lhs->channel_type() != NULL
671           || lhs->interface_type() != NULL))
672     return true;
673
674   // An untyped numeric constant may be assigned to a numeric type if
675   // it is representable in that type.
676   if ((rhs->is_abstract()
677        && (rhs->integer_type() != NULL
678            || rhs->float_type() != NULL
679            || rhs->complex_type() != NULL))
680       && (lhs->integer_type() != NULL
681           || lhs->float_type() != NULL
682           || lhs->complex_type() != NULL))
683     return true;
684
685   // Give some better error messages.
686   if (reason != NULL && reason->empty())
687     {
688       if (rhs->interface_type() != NULL)
689         reason->assign(_("need explicit conversion"));
690       else if (rhs->is_call_multiple_result_type())
691         reason->assign(_("multiple value function call in "
692                          "single value context"));
693       else if (lhs->named_type() != NULL && rhs->named_type() != NULL)
694         {
695           size_t len = (lhs->named_type()->name().length()
696                         + rhs->named_type()->name().length()
697                         + 100);
698           char* buf = new char[len];
699           snprintf(buf, len, _("cannot use type %s as type %s"),
700                    rhs->named_type()->message_name().c_str(),
701                    lhs->named_type()->message_name().c_str());
702           reason->assign(buf);
703           delete[] buf;
704         }
705     }
706
707   return false;
708 }
709
710 // Return true if a value with type RHS may be assigned to a variable
711 // with type LHS.  If REASON is not NULL, set *REASON to the reason
712 // the types are not assignable.
713
714 bool
715 Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
716 {
717   return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
718 }
719
720 // Like are_assignable but don't check for hidden fields.
721
722 bool
723 Type::are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
724                                std::string* reason)
725 {
726   return Type::are_assignable_check_hidden(lhs, rhs, false, reason);
727 }
728
729 // Return true if a value with type RHS may be converted to type LHS.
730 // If REASON is not NULL, set *REASON to the reason the types are not
731 // convertible.
732
733 bool
734 Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
735 {
736   // The types are convertible if they are assignable.
737   if (Type::are_assignable(lhs, rhs, reason))
738     return true;
739
740   // The types are convertible if they have identical underlying
741   // types.
742   if ((lhs->named_type() != NULL || rhs->named_type() != NULL)
743       && Type::are_identical(lhs->base(), rhs->base(), true, reason))
744     return true;
745
746   // The types are convertible if they are both unnamed pointer types
747   // and their pointer base types have identical underlying types.
748   if (lhs->named_type() == NULL
749       && rhs->named_type() == NULL
750       && lhs->points_to() != NULL
751       && rhs->points_to() != NULL
752       && (lhs->points_to()->named_type() != NULL
753           || rhs->points_to()->named_type() != NULL)
754       && Type::are_identical(lhs->points_to()->base(),
755                              rhs->points_to()->base(),
756                              true,
757                              reason))
758     return true;
759
760   // Integer and floating point types are convertible to each other.
761   if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
762       && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
763     return true;
764
765   // Complex types are convertible to each other.
766   if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
767     return true;
768
769   // An integer, or []byte, or []int, may be converted to a string.
770   if (lhs->is_string_type())
771     {
772       if (rhs->integer_type() != NULL)
773         return true;
774       if (rhs->is_slice_type())
775         {
776           const Type* e = rhs->array_type()->element_type()->forwarded();
777           if (e->integer_type() != NULL
778               && (e == Type::lookup_integer_type("uint8")
779                   || e == Type::lookup_integer_type("int")))
780             return true;
781         }
782     }
783
784   // A string may be converted to []byte or []int.
785   if (rhs->is_string_type() && lhs->is_slice_type())
786     {
787       const Type* e = lhs->array_type()->element_type()->forwarded();
788       if (e->integer_type() != NULL
789           && (e == Type::lookup_integer_type("uint8")
790               || e == Type::lookup_integer_type("int")))
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())
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     base_name = gogo->pack_hidden_name(this->mangled_name(gogo), false);
1508   else
1509     {
1510       // This name is already hidden or not as appropriate.
1511       base_name = name->name();
1512       const Named_object* in_function = name->in_function();
1513       if (in_function != NULL)
1514         base_name += '$' + in_function->name();
1515     }
1516   std::string hash_name = base_name + "$hash";
1517   std::string equal_name = base_name + "$equal";
1518
1519   Location bloc = Linemap::predeclared_location();
1520
1521   const Package* package = NULL;
1522   bool is_defined_elsewhere =
1523     this->type_descriptor_defined_elsewhere(name, &package);
1524   if (is_defined_elsewhere)
1525     {
1526       *hash_fn = Named_object::make_function_declaration(hash_name, package,
1527                                                          hash_fntype, bloc);
1528       *equal_fn = Named_object::make_function_declaration(equal_name, package,
1529                                                           equal_fntype, bloc);
1530     }
1531   else
1532     {
1533       *hash_fn = gogo->declare_package_function(hash_name, hash_fntype, bloc);
1534       *equal_fn = gogo->declare_package_function(equal_name, equal_fntype,
1535                                                  bloc);
1536     }
1537
1538   ins.first->second.first = *hash_fn;
1539   ins.first->second.second = *equal_fn;
1540
1541   if (!is_defined_elsewhere)
1542     {
1543       if (gogo->in_global_scope())
1544         this->write_specific_type_functions(gogo, name, hash_name, hash_fntype,
1545                                             equal_name, equal_fntype);
1546       else
1547         gogo->queue_specific_type_function(this, name, hash_name, hash_fntype,
1548                                            equal_name, equal_fntype);
1549     }
1550 }
1551
1552 // Write the hash and equality functions for a type which needs to be
1553 // written specially.
1554
1555 void
1556 Type::write_specific_type_functions(Gogo* gogo, Named_type* name,
1557                                     const std::string& hash_name,
1558                                     Function_type* hash_fntype,
1559                                     const std::string& equal_name,
1560                                     Function_type* equal_fntype)
1561 {
1562   Location bloc = Linemap::predeclared_location();
1563
1564   Named_object* hash_fn = gogo->start_function(hash_name, hash_fntype, false,
1565                                                bloc);
1566   gogo->start_block(bloc);
1567
1568   if (this->struct_type() != NULL)
1569     this->struct_type()->write_hash_function(gogo, name, hash_fntype,
1570                                              equal_fntype);
1571   else if (this->array_type() != NULL)
1572     this->array_type()->write_hash_function(gogo, name, hash_fntype,
1573                                             equal_fntype);
1574   else
1575     go_unreachable();
1576
1577   Block* b = gogo->finish_block(bloc);
1578   gogo->add_block(b, bloc);
1579   gogo->lower_block(hash_fn, b);
1580   gogo->finish_function(bloc);
1581
1582   Named_object *equal_fn = gogo->start_function(equal_name, equal_fntype,
1583                                                 false, bloc);
1584   gogo->start_block(bloc);
1585
1586   if (this->struct_type() != NULL)
1587     this->struct_type()->write_equal_function(gogo, name);
1588   else if (this->array_type() != NULL)
1589     this->array_type()->write_equal_function(gogo, name);
1590   else
1591     go_unreachable();
1592
1593   b = gogo->finish_block(bloc);
1594   gogo->add_block(b, bloc);
1595   gogo->lower_block(equal_fn, b);
1596   gogo->finish_function(bloc);
1597 }
1598
1599 // Return a composite literal for the type descriptor for a plain type
1600 // of kind RUNTIME_TYPE_KIND named NAME.
1601
1602 Expression*
1603 Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
1604                                   Named_type* name, const Methods* methods,
1605                                   bool only_value_methods)
1606 {
1607   Location bloc = Linemap::predeclared_location();
1608
1609   Type* td_type = Type::make_type_descriptor_type();
1610   const Struct_field_list* fields = td_type->struct_type()->fields();
1611
1612   Expression_list* vals = new Expression_list();
1613   vals->reserve(9);
1614
1615   if (!this->has_pointer())
1616     runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
1617   Struct_field_list::const_iterator p = fields->begin();
1618   go_assert(p->is_field_name("Kind"));
1619   mpz_t iv;
1620   mpz_init_set_ui(iv, runtime_type_kind);
1621   vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1622
1623   ++p;
1624   go_assert(p->is_field_name("align"));
1625   Expression::Type_info type_info = Expression::TYPE_INFO_ALIGNMENT;
1626   vals->push_back(Expression::make_type_info(this, type_info));
1627
1628   ++p;
1629   go_assert(p->is_field_name("fieldAlign"));
1630   type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
1631   vals->push_back(Expression::make_type_info(this, type_info));
1632
1633   ++p;
1634   go_assert(p->is_field_name("size"));
1635   type_info = Expression::TYPE_INFO_SIZE;
1636   vals->push_back(Expression::make_type_info(this, type_info));
1637
1638   ++p;
1639   go_assert(p->is_field_name("hash"));
1640   mpz_set_ui(iv, this->hash_for_method(gogo));
1641   vals->push_back(Expression::make_integer(&iv, p->type(), bloc));
1642
1643   ++p;
1644   go_assert(p->is_field_name("hashfn"));
1645   Function_type* hash_fntype = p->type()->function_type();
1646
1647   ++p;
1648   go_assert(p->is_field_name("equalfn"));
1649   Function_type* equal_fntype = p->type()->function_type();
1650
1651   Named_object* hash_fn;
1652   Named_object* equal_fn;
1653   this->type_functions(gogo, name, hash_fntype, equal_fntype, &hash_fn,
1654                        &equal_fn);
1655   vals->push_back(Expression::make_func_reference(hash_fn, NULL, bloc));
1656   vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
1657
1658   ++p;
1659   go_assert(p->is_field_name("string"));
1660   Expression* s = Expression::make_string((name != NULL
1661                                            ? name->reflection(gogo)
1662                                            : this->reflection(gogo)),
1663                                           bloc);
1664   vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1665
1666   ++p;
1667   go_assert(p->is_field_name("uncommonType"));
1668   if (name == NULL && methods == NULL)
1669     vals->push_back(Expression::make_nil(bloc));
1670   else
1671     {
1672       if (methods == NULL)
1673         methods = name->methods();
1674       vals->push_back(this->uncommon_type_constructor(gogo,
1675                                                       p->type()->deref(),
1676                                                       name, methods,
1677                                                       only_value_methods));
1678     }
1679
1680   ++p;
1681   go_assert(p->is_field_name("ptrToThis"));
1682   if (name == NULL)
1683     vals->push_back(Expression::make_nil(bloc));
1684   else
1685     {
1686       Type* pt = Type::make_pointer_type(name);
1687       vals->push_back(Expression::make_type_descriptor(pt, bloc));
1688     }
1689
1690   ++p;
1691   go_assert(p == fields->end());
1692
1693   mpz_clear(iv);
1694
1695   return Expression::make_struct_composite_literal(td_type, vals, bloc);
1696 }
1697
1698 // Return a composite literal for the uncommon type information for
1699 // this type.  UNCOMMON_STRUCT_TYPE is the type of the uncommon type
1700 // struct.  If name is not NULL, it is the name of the type.  If
1701 // METHODS is not NULL, it is the list of methods.  ONLY_VALUE_METHODS
1702 // is true if only value methods should be included.  At least one of
1703 // NAME and METHODS must not be NULL.
1704
1705 Expression*
1706 Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
1707                                 Named_type* name, const Methods* methods,
1708                                 bool only_value_methods) const
1709 {
1710   Location bloc = Linemap::predeclared_location();
1711
1712   const Struct_field_list* fields = uncommon_type->struct_type()->fields();
1713
1714   Expression_list* vals = new Expression_list();
1715   vals->reserve(3);
1716
1717   Struct_field_list::const_iterator p = fields->begin();
1718   go_assert(p->is_field_name("name"));
1719
1720   ++p;
1721   go_assert(p->is_field_name("pkgPath"));
1722
1723   if (name == NULL)
1724     {
1725       vals->push_back(Expression::make_nil(bloc));
1726       vals->push_back(Expression::make_nil(bloc));
1727     }
1728   else
1729     {
1730       Named_object* no = name->named_object();
1731       std::string n = Gogo::unpack_hidden_name(no->name());
1732       Expression* s = Expression::make_string(n, bloc);
1733       vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1734
1735       if (name->is_builtin())
1736         vals->push_back(Expression::make_nil(bloc));
1737       else
1738         {
1739           const Package* package = no->package();
1740           const std::string& unique_prefix(package == NULL
1741                                            ? gogo->unique_prefix()
1742                                            : package->unique_prefix());
1743           const std::string& package_name(package == NULL
1744                                           ? gogo->package_name()
1745                                           : package->name());
1746           n.assign(unique_prefix);
1747           n.append(1, '.');
1748           n.append(package_name);
1749           if (name->in_function() != NULL)
1750             {
1751               n.append(1, '.');
1752               n.append(Gogo::unpack_hidden_name(name->in_function()->name()));
1753             }
1754           s = Expression::make_string(n, bloc);
1755           vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1756         }
1757     }
1758
1759   ++p;
1760   go_assert(p->is_field_name("methods"));
1761   vals->push_back(this->methods_constructor(gogo, p->type(), methods,
1762                                             only_value_methods));
1763
1764   ++p;
1765   go_assert(p == fields->end());
1766
1767   Expression* r = Expression::make_struct_composite_literal(uncommon_type,
1768                                                             vals, bloc);
1769   return Expression::make_unary(OPERATOR_AND, r, bloc);
1770 }
1771
1772 // Sort methods by name.
1773
1774 class Sort_methods
1775 {
1776  public:
1777   bool
1778   operator()(const std::pair<std::string, const Method*>& m1,
1779              const std::pair<std::string, const Method*>& m2) const
1780   { return m1.first < m2.first; }
1781 };
1782
1783 // Return a composite literal for the type method table for this type.
1784 // METHODS_TYPE is the type of the table, and is a slice type.
1785 // METHODS is the list of methods.  If ONLY_VALUE_METHODS is true,
1786 // then only value methods are used.
1787
1788 Expression*
1789 Type::methods_constructor(Gogo* gogo, Type* methods_type,
1790                           const Methods* methods,
1791                           bool only_value_methods) const
1792 {
1793   Location bloc = Linemap::predeclared_location();
1794
1795   std::vector<std::pair<std::string, const Method*> > smethods;
1796   if (methods != NULL)
1797     {
1798       smethods.reserve(methods->count());
1799       for (Methods::const_iterator p = methods->begin();
1800            p != methods->end();
1801            ++p)
1802         {
1803           if (p->second->is_ambiguous())
1804             continue;
1805           if (only_value_methods && !p->second->is_value_method())
1806             continue;
1807           smethods.push_back(std::make_pair(p->first, p->second));
1808         }
1809     }
1810
1811   if (smethods.empty())
1812     return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
1813
1814   std::sort(smethods.begin(), smethods.end(), Sort_methods());
1815
1816   Type* method_type = methods_type->array_type()->element_type();
1817
1818   Expression_list* vals = new Expression_list();
1819   vals->reserve(smethods.size());
1820   for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
1821          = smethods.begin();
1822        p != smethods.end();
1823        ++p)
1824     vals->push_back(this->method_constructor(gogo, method_type, p->first,
1825                                              p->second, only_value_methods));
1826
1827   return Expression::make_slice_composite_literal(methods_type, vals, bloc);
1828 }
1829
1830 // Return a composite literal for a single method.  METHOD_TYPE is the
1831 // type of the entry.  METHOD_NAME is the name of the method and M is
1832 // the method information.
1833
1834 Expression*
1835 Type::method_constructor(Gogo*, Type* method_type,
1836                          const std::string& method_name,
1837                          const Method* m,
1838                          bool only_value_methods) const
1839 {
1840   Location bloc = Linemap::predeclared_location();
1841
1842   const Struct_field_list* fields = method_type->struct_type()->fields();
1843
1844   Expression_list* vals = new Expression_list();
1845   vals->reserve(5);
1846
1847   Struct_field_list::const_iterator p = fields->begin();
1848   go_assert(p->is_field_name("name"));
1849   const std::string n = Gogo::unpack_hidden_name(method_name);
1850   Expression* s = Expression::make_string(n, bloc);
1851   vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1852
1853   ++p;
1854   go_assert(p->is_field_name("pkgPath"));
1855   if (!Gogo::is_hidden_name(method_name))
1856     vals->push_back(Expression::make_nil(bloc));
1857   else
1858     {
1859       s = Expression::make_string(Gogo::hidden_name_prefix(method_name), bloc);
1860       vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
1861     }
1862
1863   Named_object* no = (m->needs_stub_method()
1864                       ? m->stub_object()
1865                       : m->named_object());
1866
1867   Function_type* mtype;
1868   if (no->is_function())
1869     mtype = no->func_value()->type();
1870   else
1871     mtype = no->func_declaration_value()->type();
1872   go_assert(mtype->is_method());
1873   Type* nonmethod_type = mtype->copy_without_receiver();
1874
1875   ++p;
1876   go_assert(p->is_field_name("mtyp"));
1877   vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
1878
1879   ++p;
1880   go_assert(p->is_field_name("typ"));
1881   if (!only_value_methods && m->is_value_method())
1882     {
1883       // This is a value method on a pointer type.  Change the type of
1884       // the method to use a pointer receiver.  The implementation
1885       // always uses a pointer receiver anyhow.
1886       Type* rtype = mtype->receiver()->type();
1887       Type* prtype = Type::make_pointer_type(rtype);
1888       Typed_identifier* receiver =
1889         new Typed_identifier(mtype->receiver()->name(), prtype,
1890                              mtype->receiver()->location());
1891       mtype = Type::make_function_type(receiver,
1892                                        (mtype->parameters() == NULL
1893                                         ? NULL
1894                                         : mtype->parameters()->copy()),
1895                                        (mtype->results() == NULL
1896                                         ? NULL
1897                                         : mtype->results()->copy()),
1898                                        mtype->location());
1899     }
1900   vals->push_back(Expression::make_type_descriptor(mtype, bloc));
1901
1902   ++p;
1903   go_assert(p->is_field_name("tfn"));
1904   vals->push_back(Expression::make_func_reference(no, NULL, bloc));
1905
1906   ++p;
1907   go_assert(p == fields->end());
1908
1909   return Expression::make_struct_composite_literal(method_type, vals, bloc);
1910 }
1911
1912 // Return a composite literal for the type descriptor of a plain type.
1913 // RUNTIME_TYPE_KIND is the value of the kind field.  If NAME is not
1914 // NULL, it is the name to use as well as the list of methods.
1915
1916 Expression*
1917 Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
1918                             Named_type* name)
1919 {
1920   return this->type_descriptor_constructor(gogo, runtime_type_kind,
1921                                            name, NULL, true);
1922 }
1923
1924 // Return the type reflection string for this type.
1925
1926 std::string
1927 Type::reflection(Gogo* gogo) const
1928 {
1929   std::string ret;
1930
1931   // The do_reflection virtual function should set RET to the
1932   // reflection string.
1933   this->do_reflection(gogo, &ret);
1934
1935   return ret;
1936 }
1937
1938 // Return a mangled name for the type.
1939
1940 std::string
1941 Type::mangled_name(Gogo* gogo) const
1942 {
1943   std::string ret;
1944
1945   // The do_mangled_name virtual function should set RET to the
1946   // mangled name.  For a composite type it should append a code for
1947   // the composition and then call do_mangled_name on the components.
1948   this->do_mangled_name(gogo, &ret);
1949
1950   return ret;
1951 }
1952
1953 // Return whether the backend size of the type is known.
1954
1955 bool
1956 Type::is_backend_type_size_known(Gogo* gogo) const
1957 {
1958   switch (this->classification_)
1959     {
1960     case TYPE_ERROR:
1961     case TYPE_VOID:
1962     case TYPE_BOOLEAN:
1963     case TYPE_INTEGER:
1964     case TYPE_FLOAT:
1965     case TYPE_COMPLEX:
1966     case TYPE_STRING:
1967     case TYPE_FUNCTION:
1968     case TYPE_POINTER:
1969     case TYPE_NIL:
1970     case TYPE_MAP:
1971     case TYPE_CHANNEL:
1972     case TYPE_INTERFACE:
1973       return true;
1974
1975     case TYPE_STRUCT:
1976       {
1977         const Struct_field_list* fields = this->struct_type()->fields();
1978         for (Struct_field_list::const_iterator pf = fields->begin();
1979              pf != fields->end();
1980              ++pf)
1981           if (!pf->type()->is_backend_type_size_known(gogo))
1982             return false;
1983         return true;
1984       }
1985
1986     case TYPE_ARRAY:
1987       {
1988         const Array_type* at = this->array_type();
1989         if (at->length() == NULL)
1990           return true;
1991         else
1992           {
1993             mpz_t ival;
1994             mpz_init(ival);
1995             Type* dummy;
1996             bool length_known = at->length()->integer_constant_value(true,
1997                                                                      ival,
1998                                                                      &dummy);
1999             mpz_clear(ival);
2000             if (!length_known)
2001               return false;
2002             return at->element_type()->is_backend_type_size_known(gogo);
2003           }
2004       }
2005
2006     case TYPE_NAMED:
2007       return this->named_type()->is_named_backend_type_size_known();
2008
2009     case TYPE_FORWARD:
2010       {
2011         const Forward_declaration_type* fdt = this->forward_declaration_type();
2012         return fdt->real_type()->is_backend_type_size_known(gogo);
2013       }
2014
2015     case TYPE_SINK:
2016     case TYPE_CALL_MULTIPLE_RESULT:
2017       go_unreachable();
2018
2019     default:
2020       go_unreachable();
2021     }
2022 }
2023
2024 // If the size of the type can be determined, set *PSIZE to the size
2025 // in bytes and return true.  Otherwise, return false.  This queries
2026 // the backend.
2027
2028 bool
2029 Type::backend_type_size(Gogo* gogo, unsigned int *psize)
2030 {
2031   Btype* btype = this->get_backend(gogo);
2032   if (!this->is_backend_type_size_known(gogo))
2033     return false;
2034   size_t size = gogo->backend()->type_size(btype);
2035   *psize = static_cast<unsigned int>(size);
2036   if (*psize != size)
2037     return false;
2038   return true;
2039 }
2040
2041 // If the alignment of the type can be determined, set *PALIGN to
2042 // the alignment in bytes and return true.  Otherwise, return false.
2043
2044 bool
2045 Type::backend_type_align(Gogo* gogo, unsigned int *palign)
2046 {
2047   Btype* btype = this->get_backend(gogo);
2048   if (!this->is_backend_type_size_known(gogo))
2049     return false;
2050   size_t align = gogo->backend()->type_alignment(btype);
2051   *palign = static_cast<unsigned int>(align);
2052   if (*palign != align)
2053     return false;
2054   return true;
2055 }
2056
2057 // Like backend_type_align, but return the alignment when used as a
2058 // field.
2059
2060 bool
2061 Type::backend_type_field_align(Gogo* gogo, unsigned int *palign)
2062 {
2063   Btype* btype = this->get_backend(gogo);
2064   if (!this->is_backend_type_size_known(gogo))
2065     return false;
2066   size_t a = gogo->backend()->type_field_alignment(btype);
2067   *palign = static_cast<unsigned int>(a);
2068   if (*palign != a)
2069     return false;
2070   return true;
2071 }
2072
2073 // Default function to export a type.
2074
2075 void
2076 Type::do_export(Export*) const
2077 {
2078   go_unreachable();
2079 }
2080
2081 // Import a type.
2082
2083 Type*
2084 Type::import_type(Import* imp)
2085 {
2086   if (imp->match_c_string("("))
2087     return Function_type::do_import(imp);
2088   else if (imp->match_c_string("*"))
2089     return Pointer_type::do_import(imp);
2090   else if (imp->match_c_string("struct "))
2091     return Struct_type::do_import(imp);
2092   else if (imp->match_c_string("["))
2093     return Array_type::do_import(imp);
2094   else if (imp->match_c_string("map "))
2095     return Map_type::do_import(imp);
2096   else if (imp->match_c_string("chan "))
2097     return Channel_type::do_import(imp);
2098   else if (imp->match_c_string("interface"))
2099     return Interface_type::do_import(imp);
2100   else
2101     {
2102       error_at(imp->location(), "import error: expected type");
2103       return Type::make_error_type();
2104     }
2105 }
2106
2107 // A type used to indicate a parsing error.  This exists to simplify
2108 // later error detection.
2109
2110 class Error_type : public Type
2111 {
2112  public:
2113   Error_type()
2114     : Type(TYPE_ERROR)
2115   { }
2116
2117  protected:
2118   bool
2119   do_compare_is_identity() const
2120   { return false; }
2121
2122   Btype*
2123   do_get_backend(Gogo* gogo)
2124   { return gogo->backend()->error_type(); }
2125
2126   Expression*
2127   do_type_descriptor(Gogo*, Named_type*)
2128   { return Expression::make_error(Linemap::predeclared_location()); }
2129
2130   void
2131   do_reflection(Gogo*, std::string*) const
2132   { go_assert(saw_errors()); }
2133
2134   void
2135   do_mangled_name(Gogo*, std::string* ret) const
2136   { ret->push_back('E'); }
2137 };
2138
2139 Type*
2140 Type::make_error_type()
2141 {
2142   static Error_type singleton_error_type;
2143   return &singleton_error_type;
2144 }
2145
2146 // The void type.
2147
2148 class Void_type : public Type
2149 {
2150  public:
2151   Void_type()
2152     : Type(TYPE_VOID)
2153   { }
2154
2155  protected:
2156   bool
2157   do_compare_is_identity() const
2158   { return false; }
2159
2160   Btype*
2161   do_get_backend(Gogo* gogo)
2162   { return gogo->backend()->void_type(); }
2163
2164   Expression*
2165   do_type_descriptor(Gogo*, Named_type*)
2166   { go_unreachable(); }
2167
2168   void
2169   do_reflection(Gogo*, std::string*) const
2170   { }
2171
2172   void
2173   do_mangled_name(Gogo*, std::string* ret) const
2174   { ret->push_back('v'); }
2175 };
2176
2177 Type*
2178 Type::make_void_type()
2179 {
2180   static Void_type singleton_void_type;
2181   return &singleton_void_type;
2182 }
2183
2184 // The boolean type.
2185
2186 class Boolean_type : public Type
2187 {
2188  public:
2189   Boolean_type()
2190     : Type(TYPE_BOOLEAN)
2191   { }
2192
2193  protected:
2194   bool
2195   do_compare_is_identity() const
2196   { return true; }
2197
2198   Btype*
2199   do_get_backend(Gogo* gogo)
2200   { return gogo->backend()->bool_type(); }
2201
2202   Expression*
2203   do_type_descriptor(Gogo*, Named_type* name);
2204
2205   // We should not be asked for the reflection string of a basic type.
2206   void
2207   do_reflection(Gogo*, std::string* ret) const
2208   { ret->append("bool"); }
2209
2210   void
2211   do_mangled_name(Gogo*, std::string* ret) const
2212   { ret->push_back('b'); }
2213 };
2214
2215 // Make the type descriptor.
2216
2217 Expression*
2218 Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2219 {
2220   if (name != NULL)
2221     return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
2222   else
2223     {
2224       Named_object* no = gogo->lookup_global("bool");
2225       go_assert(no != NULL);
2226       return Type::type_descriptor(gogo, no->type_value());
2227     }
2228 }
2229
2230 Type*
2231 Type::make_boolean_type()
2232 {
2233   static Boolean_type boolean_type;
2234   return &boolean_type;
2235 }
2236
2237 // The named type "bool".
2238
2239 static Named_type* named_bool_type;
2240
2241 // Get the named type "bool".
2242
2243 Named_type*
2244 Type::lookup_bool_type()
2245 {
2246   return named_bool_type;
2247 }
2248
2249 // Make the named type "bool".
2250
2251 Named_type*
2252 Type::make_named_bool_type()
2253 {
2254   Type* bool_type = Type::make_boolean_type();
2255   Named_object* named_object =
2256     Named_object::make_type("bool", NULL, bool_type,
2257                             Linemap::predeclared_location());
2258   Named_type* named_type = named_object->type_value();
2259   named_bool_type = named_type;
2260   return named_type;
2261 }
2262
2263 // Class Integer_type.
2264
2265 Integer_type::Named_integer_types Integer_type::named_integer_types;
2266
2267 // Create a new integer type.  Non-abstract integer types always have
2268 // names.
2269
2270 Named_type*
2271 Integer_type::create_integer_type(const char* name, bool is_unsigned,
2272                                   int bits, int runtime_type_kind)
2273 {
2274   Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
2275                                                 runtime_type_kind);
2276   std::string sname(name);
2277   Named_object* named_object =
2278     Named_object::make_type(sname, NULL, integer_type,
2279                             Linemap::predeclared_location());
2280   Named_type* named_type = named_object->type_value();
2281   std::pair<Named_integer_types::iterator, bool> ins =
2282     Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
2283   go_assert(ins.second);
2284   return named_type;
2285 }
2286
2287 // Look up an existing integer type.
2288
2289 Named_type*
2290 Integer_type::lookup_integer_type(const char* name)
2291 {
2292   Named_integer_types::const_iterator p =
2293     Integer_type::named_integer_types.find(name);
2294   go_assert(p != Integer_type::named_integer_types.end());
2295   return p->second;
2296 }
2297
2298 // Create a new abstract integer type.
2299
2300 Integer_type*
2301 Integer_type::create_abstract_integer_type()
2302 {
2303   static Integer_type* abstract_type;
2304   if (abstract_type == NULL)
2305     abstract_type = new Integer_type(true, false, INT_TYPE_SIZE,
2306                                      RUNTIME_TYPE_KIND_INT);
2307   return abstract_type;
2308 }
2309
2310 // Integer type compatibility.
2311
2312 bool
2313 Integer_type::is_identical(const Integer_type* t) const
2314 {
2315   if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
2316     return false;
2317   return this->is_abstract_ == t->is_abstract_;
2318 }
2319
2320 // Hash code.
2321
2322 unsigned int
2323 Integer_type::do_hash_for_method(Gogo*) const
2324 {
2325   return ((this->bits_ << 4)
2326           + ((this->is_unsigned_ ? 1 : 0) << 8)
2327           + ((this->is_abstract_ ? 1 : 0) << 9));
2328 }
2329
2330 // Convert an Integer_type to the backend representation.
2331
2332 Btype*
2333 Integer_type::do_get_backend(Gogo* gogo)
2334 {
2335   if (this->is_abstract_)
2336     {
2337       go_assert(saw_errors());
2338       return gogo->backend()->error_type();
2339     }
2340   return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
2341 }
2342
2343 // The type descriptor for an integer type.  Integer types are always
2344 // named.
2345
2346 Expression*
2347 Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2348 {
2349   go_assert(name != NULL);
2350   return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2351 }
2352
2353 // We should not be asked for the reflection string of a basic type.
2354
2355 void
2356 Integer_type::do_reflection(Gogo*, std::string*) const
2357 {
2358   go_assert(saw_errors());
2359 }
2360
2361 // Mangled name.
2362
2363 void
2364 Integer_type::do_mangled_name(Gogo*, std::string* ret) const
2365 {
2366   char buf[100];
2367   snprintf(buf, sizeof buf, "i%s%s%de",
2368            this->is_abstract_ ? "a" : "",
2369            this->is_unsigned_ ? "u" : "",
2370            this->bits_);
2371   ret->append(buf);
2372 }
2373
2374 // Make an integer type.
2375
2376 Named_type*
2377 Type::make_integer_type(const char* name, bool is_unsigned, int bits,
2378                         int runtime_type_kind)
2379 {
2380   return Integer_type::create_integer_type(name, is_unsigned, bits,
2381                                            runtime_type_kind);
2382 }
2383
2384 // Make an abstract integer type.
2385
2386 Integer_type*
2387 Type::make_abstract_integer_type()
2388 {
2389   return Integer_type::create_abstract_integer_type();
2390 }
2391
2392 // Look up an integer type.
2393
2394 Named_type*
2395 Type::lookup_integer_type(const char* name)
2396 {
2397   return Integer_type::lookup_integer_type(name);
2398 }
2399
2400 // Class Float_type.
2401
2402 Float_type::Named_float_types Float_type::named_float_types;
2403
2404 // Create a new float type.  Non-abstract float types always have
2405 // names.
2406
2407 Named_type*
2408 Float_type::create_float_type(const char* name, int bits,
2409                               int runtime_type_kind)
2410 {
2411   Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
2412   std::string sname(name);
2413   Named_object* named_object =
2414     Named_object::make_type(sname, NULL, float_type,
2415                             Linemap::predeclared_location());
2416   Named_type* named_type = named_object->type_value();
2417   std::pair<Named_float_types::iterator, bool> ins =
2418     Float_type::named_float_types.insert(std::make_pair(sname, named_type));
2419   go_assert(ins.second);
2420   return named_type;
2421 }
2422
2423 // Look up an existing float type.
2424
2425 Named_type*
2426 Float_type::lookup_float_type(const char* name)
2427 {
2428   Named_float_types::const_iterator p =
2429     Float_type::named_float_types.find(name);
2430   go_assert(p != Float_type::named_float_types.end());
2431   return p->second;
2432 }
2433
2434 // Create a new abstract float type.
2435
2436 Float_type*
2437 Float_type::create_abstract_float_type()
2438 {
2439   static Float_type* abstract_type;
2440   if (abstract_type == NULL)
2441     abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
2442   return abstract_type;
2443 }
2444
2445 // Whether this type is identical with T.
2446
2447 bool
2448 Float_type::is_identical(const Float_type* t) const
2449 {
2450   if (this->bits_ != t->bits_)
2451     return false;
2452   return this->is_abstract_ == t->is_abstract_;
2453 }
2454
2455 // Hash code.
2456
2457 unsigned int
2458 Float_type::do_hash_for_method(Gogo*) const
2459 {
2460   return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2461 }
2462
2463 // Convert to the backend representation.
2464
2465 Btype*
2466 Float_type::do_get_backend(Gogo* gogo)
2467 {
2468   return gogo->backend()->float_type(this->bits_);
2469 }
2470
2471 // The type descriptor for a float type.  Float types are always named.
2472
2473 Expression*
2474 Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2475 {
2476   go_assert(name != NULL);
2477   return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2478 }
2479
2480 // We should not be asked for the reflection string of a basic type.
2481
2482 void
2483 Float_type::do_reflection(Gogo*, std::string*) const
2484 {
2485   go_assert(saw_errors());
2486 }
2487
2488 // Mangled name.
2489
2490 void
2491 Float_type::do_mangled_name(Gogo*, std::string* ret) const
2492 {
2493   char buf[100];
2494   snprintf(buf, sizeof buf, "f%s%de",
2495            this->is_abstract_ ? "a" : "",
2496            this->bits_);
2497   ret->append(buf);
2498 }
2499
2500 // Make a floating point type.
2501
2502 Named_type*
2503 Type::make_float_type(const char* name, int bits, int runtime_type_kind)
2504 {
2505   return Float_type::create_float_type(name, bits, runtime_type_kind);
2506 }
2507
2508 // Make an abstract float type.
2509
2510 Float_type*
2511 Type::make_abstract_float_type()
2512 {
2513   return Float_type::create_abstract_float_type();
2514 }
2515
2516 // Look up a float type.
2517
2518 Named_type*
2519 Type::lookup_float_type(const char* name)
2520 {
2521   return Float_type::lookup_float_type(name);
2522 }
2523
2524 // Class Complex_type.
2525
2526 Complex_type::Named_complex_types Complex_type::named_complex_types;
2527
2528 // Create a new complex type.  Non-abstract complex types always have
2529 // names.
2530
2531 Named_type*
2532 Complex_type::create_complex_type(const char* name, int bits,
2533                                   int runtime_type_kind)
2534 {
2535   Complex_type* complex_type = new Complex_type(false, bits,
2536                                                 runtime_type_kind);
2537   std::string sname(name);
2538   Named_object* named_object =
2539     Named_object::make_type(sname, NULL, complex_type,
2540                             Linemap::predeclared_location());
2541   Named_type* named_type = named_object->type_value();
2542   std::pair<Named_complex_types::iterator, bool> ins =
2543     Complex_type::named_complex_types.insert(std::make_pair(sname,
2544                                                             named_type));
2545   go_assert(ins.second);
2546   return named_type;
2547 }
2548
2549 // Look up an existing complex type.
2550
2551 Named_type*
2552 Complex_type::lookup_complex_type(const char* name)
2553 {
2554   Named_complex_types::const_iterator p =
2555     Complex_type::named_complex_types.find(name);
2556   go_assert(p != Complex_type::named_complex_types.end());
2557   return p->second;
2558 }
2559
2560 // Create a new abstract complex type.
2561
2562 Complex_type*
2563 Complex_type::create_abstract_complex_type()
2564 {
2565   static Complex_type* abstract_type;
2566   if (abstract_type == NULL)
2567     abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
2568   return abstract_type;
2569 }
2570
2571 // Whether this type is identical with T.
2572
2573 bool
2574 Complex_type::is_identical(const Complex_type *t) const
2575 {
2576   if (this->bits_ != t->bits_)
2577     return false;
2578   return this->is_abstract_ == t->is_abstract_;
2579 }
2580
2581 // Hash code.
2582
2583 unsigned int
2584 Complex_type::do_hash_for_method(Gogo*) const
2585 {
2586   return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
2587 }
2588
2589 // Convert to the backend representation.
2590
2591 Btype*
2592 Complex_type::do_get_backend(Gogo* gogo)
2593 {
2594   return gogo->backend()->complex_type(this->bits_);
2595 }
2596
2597 // The type descriptor for a complex type.  Complex types are always
2598 // named.
2599
2600 Expression*
2601 Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2602 {
2603   go_assert(name != NULL);
2604   return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
2605 }
2606
2607 // We should not be asked for the reflection string of a basic type.
2608
2609 void
2610 Complex_type::do_reflection(Gogo*, std::string*) const
2611 {
2612   go_assert(saw_errors());
2613 }
2614
2615 // Mangled name.
2616
2617 void
2618 Complex_type::do_mangled_name(Gogo*, std::string* ret) const
2619 {
2620   char buf[100];
2621   snprintf(buf, sizeof buf, "c%s%de",
2622            this->is_abstract_ ? "a" : "",
2623            this->bits_);
2624   ret->append(buf);
2625 }
2626
2627 // Make a complex type.
2628
2629 Named_type*
2630 Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
2631 {
2632   return Complex_type::create_complex_type(name, bits, runtime_type_kind);
2633 }
2634
2635 // Make an abstract complex type.
2636
2637 Complex_type*
2638 Type::make_abstract_complex_type()
2639 {
2640   return Complex_type::create_abstract_complex_type();
2641 }
2642
2643 // Look up a complex type.
2644
2645 Named_type*
2646 Type::lookup_complex_type(const char* name)
2647 {
2648   return Complex_type::lookup_complex_type(name);
2649 }
2650
2651 // Class String_type.
2652
2653 // Convert String_type to the backend representation.  A string is a
2654 // struct with two fields: a pointer to the characters and a length.
2655
2656 Btype*
2657 String_type::do_get_backend(Gogo* gogo)
2658 {
2659   static Btype* backend_string_type;
2660   if (backend_string_type == NULL)
2661     {
2662       std::vector<Backend::Btyped_identifier> fields(2);
2663
2664       Type* b = gogo->lookup_global("byte")->type_value();
2665       Type* pb = Type::make_pointer_type(b);
2666       fields[0].name = "__data";
2667       fields[0].btype = pb->get_backend(gogo);
2668       fields[0].location = Linemap::predeclared_location();
2669
2670       Type* int_type = Type::lookup_integer_type("int");
2671       fields[1].name = "__length";
2672       fields[1].btype = int_type->get_backend(gogo);
2673       fields[1].location = fields[0].location;
2674
2675       backend_string_type = gogo->backend()->struct_type(fields);
2676     }
2677   return backend_string_type;
2678 }
2679
2680 // Return a tree for the length of STRING.
2681
2682 tree
2683 String_type::length_tree(Gogo*, tree string)
2684 {
2685   tree string_type = TREE_TYPE(string);
2686   go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2687   tree length_field = DECL_CHAIN(TYPE_FIELDS(string_type));
2688   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(length_field)),
2689                     "__length") == 0);
2690   return fold_build3(COMPONENT_REF, integer_type_node, string,
2691                      length_field, NULL_TREE);
2692 }
2693
2694 // Return a tree for a pointer to the bytes of STRING.
2695
2696 tree
2697 String_type::bytes_tree(Gogo*, tree string)
2698 {
2699   tree string_type = TREE_TYPE(string);
2700   go_assert(TREE_CODE(string_type) == RECORD_TYPE);
2701   tree bytes_field = TYPE_FIELDS(string_type);
2702   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(bytes_field)),
2703                     "__data") == 0);
2704   return fold_build3(COMPONENT_REF, TREE_TYPE(bytes_field), string,
2705                      bytes_field, NULL_TREE);
2706 }
2707
2708 // The type descriptor for the string type.
2709
2710 Expression*
2711 String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
2712 {
2713   if (name != NULL)
2714     return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
2715   else
2716     {
2717       Named_object* no = gogo->lookup_global("string");
2718       go_assert(no != NULL);
2719       return Type::type_descriptor(gogo, no->type_value());
2720     }
2721 }
2722
2723 // We should not be asked for the reflection string of a basic type.
2724
2725 void
2726 String_type::do_reflection(Gogo*, std::string* ret) const
2727 {
2728   ret->append("string");
2729 }
2730
2731 // Mangled name of a string type.
2732
2733 void
2734 String_type::do_mangled_name(Gogo*, std::string* ret) const
2735 {
2736   ret->push_back('z');
2737 }
2738
2739 // Make a string type.
2740
2741 Type*
2742 Type::make_string_type()
2743 {
2744   static String_type string_type;
2745   return &string_type;
2746 }
2747
2748 // The named type "string".
2749
2750 static Named_type* named_string_type;
2751
2752 // Get the named type "string".
2753
2754 Named_type*
2755 Type::lookup_string_type()
2756 {
2757   return named_string_type;
2758 }
2759
2760 // Make the named type string.
2761
2762 Named_type*
2763 Type::make_named_string_type()
2764 {
2765   Type* string_type = Type::make_string_type();
2766   Named_object* named_object =
2767     Named_object::make_type("string", NULL, string_type,
2768                             Linemap::predeclared_location());
2769   Named_type* named_type = named_object->type_value();
2770   named_string_type = named_type;
2771   return named_type;
2772 }
2773
2774 // The sink type.  This is the type of the blank identifier _.  Any
2775 // type may be assigned to it.
2776
2777 class Sink_type : public Type
2778 {
2779  public:
2780   Sink_type()
2781     : Type(TYPE_SINK)
2782   { }
2783
2784  protected:
2785   bool
2786   do_compare_is_identity() const
2787   { return false; }
2788
2789   Btype*
2790   do_get_backend(Gogo*)
2791   { go_unreachable(); }
2792
2793   Expression*
2794   do_type_descriptor(Gogo*, Named_type*)
2795   { go_unreachable(); }
2796
2797   void
2798   do_reflection(Gogo*, std::string*) const
2799   { go_unreachable(); }
2800
2801   void
2802   do_mangled_name(Gogo*, std::string*) const
2803   { go_unreachable(); }
2804 };
2805
2806 // Make the sink type.
2807
2808 Type*
2809 Type::make_sink_type()
2810 {
2811   static Sink_type sink_type;
2812   return &sink_type;
2813 }
2814
2815 // Class Function_type.
2816
2817 // Traversal.
2818
2819 int
2820 Function_type::do_traverse(Traverse* traverse)
2821 {
2822   if (this->receiver_ != NULL
2823       && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
2824     return TRAVERSE_EXIT;
2825   if (this->parameters_ != NULL
2826       && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
2827     return TRAVERSE_EXIT;
2828   if (this->results_ != NULL
2829       && this->results_->traverse(traverse) == TRAVERSE_EXIT)
2830     return TRAVERSE_EXIT;
2831   return TRAVERSE_CONTINUE;
2832 }
2833
2834 // Returns whether T is a valid redeclaration of this type.  If this
2835 // returns false, and REASON is not NULL, *REASON may be set to a
2836 // brief explanation of why it returned false.
2837
2838 bool
2839 Function_type::is_valid_redeclaration(const Function_type* t,
2840                                       std::string* reason) const
2841 {
2842   if (!this->is_identical(t, false, true, reason))
2843     return false;
2844
2845   // A redeclaration of a function is required to use the same names
2846   // for the receiver and parameters.
2847   if (this->receiver() != NULL
2848       && this->receiver()->name() != t->receiver()->name()
2849       && this->receiver()->name() != Import::import_marker
2850       && t->receiver()->name() != Import::import_marker)
2851     {
2852       if (reason != NULL)
2853         *reason = "receiver name changed";
2854       return false;
2855     }
2856
2857   const Typed_identifier_list* parms1 = this->parameters();
2858   const Typed_identifier_list* parms2 = t->parameters();
2859   if (parms1 != NULL)
2860     {
2861       Typed_identifier_list::const_iterator p1 = parms1->begin();
2862       for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2863            p2 != parms2->end();
2864            ++p2, ++p1)
2865         {
2866           if (p1->name() != p2->name()
2867               && p1->name() != Import::import_marker
2868               && p2->name() != Import::import_marker)
2869             {
2870               if (reason != NULL)
2871                 *reason = "parameter name changed";
2872               return false;
2873             }
2874
2875           // This is called at parse time, so we may have unknown
2876           // types.
2877           Type* t1 = p1->type()->forwarded();
2878           Type* t2 = p2->type()->forwarded();
2879           if (t1 != t2
2880               && t1->forward_declaration_type() != NULL
2881               && (t2->forward_declaration_type() == NULL
2882                   || (t1->forward_declaration_type()->named_object()
2883                       != t2->forward_declaration_type()->named_object())))
2884             return false;
2885         }
2886     }
2887
2888   const Typed_identifier_list* results1 = this->results();
2889   const Typed_identifier_list* results2 = t->results();
2890   if (results1 != NULL)
2891     {
2892       Typed_identifier_list::const_iterator res1 = results1->begin();
2893       for (Typed_identifier_list::const_iterator res2 = results2->begin();
2894            res2 != results2->end();
2895            ++res2, ++res1)
2896         {
2897           if (res1->name() != res2->name()
2898               && res1->name() != Import::import_marker
2899               && res2->name() != Import::import_marker)
2900             {
2901               if (reason != NULL)
2902                 *reason = "result name changed";
2903               return false;
2904             }
2905
2906           // This is called at parse time, so we may have unknown
2907           // types.
2908           Type* t1 = res1->type()->forwarded();
2909           Type* t2 = res2->type()->forwarded();
2910           if (t1 != t2
2911               && t1->forward_declaration_type() != NULL
2912               && (t2->forward_declaration_type() == NULL
2913                   || (t1->forward_declaration_type()->named_object()
2914                       != t2->forward_declaration_type()->named_object())))
2915             return false;
2916         }
2917     }
2918
2919   return true;
2920 }
2921
2922 // Check whether T is the same as this type.
2923
2924 bool
2925 Function_type::is_identical(const Function_type* t, bool ignore_receiver,
2926                             bool errors_are_identical,
2927                             std::string* reason) const
2928 {
2929   if (!ignore_receiver)
2930     {
2931       const Typed_identifier* r1 = this->receiver();
2932       const Typed_identifier* r2 = t->receiver();
2933       if ((r1 != NULL) != (r2 != NULL))
2934         {
2935           if (reason != NULL)
2936             *reason = _("different receiver types");
2937           return false;
2938         }
2939       if (r1 != NULL)
2940         {
2941           if (!Type::are_identical(r1->type(), r2->type(), errors_are_identical,
2942                                    reason))
2943             {
2944               if (reason != NULL && !reason->empty())
2945                 *reason = "receiver: " + *reason;
2946               return false;
2947             }
2948         }
2949     }
2950
2951   const Typed_identifier_list* parms1 = this->parameters();
2952   const Typed_identifier_list* parms2 = t->parameters();
2953   if ((parms1 != NULL) != (parms2 != NULL))
2954     {
2955       if (reason != NULL)
2956         *reason = _("different number of parameters");
2957       return false;
2958     }
2959   if (parms1 != NULL)
2960     {
2961       Typed_identifier_list::const_iterator p1 = parms1->begin();
2962       for (Typed_identifier_list::const_iterator p2 = parms2->begin();
2963            p2 != parms2->end();
2964            ++p2, ++p1)
2965         {
2966           if (p1 == parms1->end())
2967             {
2968               if (reason != NULL)
2969                 *reason = _("different number of parameters");
2970               return false;
2971             }
2972
2973           if (!Type::are_identical(p1->type(), p2->type(),
2974                                    errors_are_identical, NULL))
2975             {
2976               if (reason != NULL)
2977                 *reason = _("different parameter types");
2978               return false;
2979             }
2980         }
2981       if (p1 != parms1->end())
2982         {
2983           if (reason != NULL)
2984             *reason = _("different number of parameters");
2985         return false;
2986         }
2987     }
2988
2989   if (this->is_varargs() != t->is_varargs())
2990     {
2991       if (reason != NULL)
2992         *reason = _("different varargs");
2993       return false;
2994     }
2995
2996   const Typed_identifier_list* results1 = this->results();
2997   const Typed_identifier_list* results2 = t->results();
2998   if ((results1 != NULL) != (results2 != NULL))
2999     {
3000       if (reason != NULL)
3001         *reason = _("different number of results");
3002       return false;
3003     }
3004   if (results1 != NULL)
3005     {
3006       Typed_identifier_list::const_iterator res1 = results1->begin();
3007       for (Typed_identifier_list::const_iterator res2 = results2->begin();
3008            res2 != results2->end();
3009            ++res2, ++res1)
3010         {
3011           if (res1 == results1->end())
3012             {
3013               if (reason != NULL)
3014                 *reason = _("different number of results");
3015               return false;
3016             }
3017
3018           if (!Type::are_identical(res1->type(), res2->type(),
3019                                    errors_are_identical, NULL))
3020             {
3021               if (reason != NULL)
3022                 *reason = _("different result types");
3023               return false;
3024             }
3025         }
3026       if (res1 != results1->end())
3027         {
3028           if (reason != NULL)
3029             *reason = _("different number of results");
3030           return false;
3031         }
3032     }
3033
3034   return true;
3035 }
3036
3037 // Hash code.
3038
3039 unsigned int
3040 Function_type::do_hash_for_method(Gogo* gogo) const
3041 {
3042   unsigned int ret = 0;
3043   // We ignore the receiver type for hash codes, because we need to
3044   // get the same hash code for a method in an interface and a method
3045   // declared for a type.  The former will not have a receiver.
3046   if (this->parameters_ != NULL)
3047     {
3048       int shift = 1;
3049       for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3050            p != this->parameters_->end();
3051            ++p, ++shift)
3052         ret += p->type()->hash_for_method(gogo) << shift;
3053     }
3054   if (this->results_ != NULL)
3055     {
3056       int shift = 2;
3057       for (Typed_identifier_list::const_iterator p = this->results_->begin();
3058            p != this->results_->end();
3059            ++p, ++shift)
3060         ret += p->type()->hash_for_method(gogo) << shift;
3061     }
3062   if (this->is_varargs_)
3063     ret += 1;
3064   ret <<= 4;
3065   return ret;
3066 }
3067
3068 // Get the backend representation for a function type.
3069
3070 Btype*
3071 Function_type::get_function_backend(Gogo* gogo)
3072 {
3073   Backend::Btyped_identifier breceiver;
3074   if (this->receiver_ != NULL)
3075     {
3076       breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
3077
3078       // We always pass the address of the receiver parameter, in
3079       // order to make interface calls work with unknown types.
3080       Type* rtype = this->receiver_->type();
3081       if (rtype->points_to() == NULL)
3082         rtype = Type::make_pointer_type(rtype);
3083       breceiver.btype = rtype->get_backend(gogo);
3084       breceiver.location = this->receiver_->location();
3085     }
3086
3087   std::vector<Backend::Btyped_identifier> bparameters;
3088   if (this->parameters_ != NULL)
3089     {
3090       bparameters.resize(this->parameters_->size());
3091       size_t i = 0;
3092       for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
3093            p != this->parameters_->end();
3094            ++p, ++i)
3095         {
3096           bparameters[i].name = Gogo::unpack_hidden_name(p->name());
3097           bparameters[i].btype = p->type()->get_backend(gogo);
3098           bparameters[i].location = p->location();
3099         }
3100       go_assert(i == bparameters.size());
3101     }
3102
3103   std::vector<Backend::Btyped_identifier> bresults;
3104   if (this->results_ != NULL)
3105     {
3106       bresults.resize(this->results_->size());
3107       size_t i = 0;
3108       for (Typed_identifier_list::const_iterator p = this->results_->begin();
3109            p != this->results_->end();
3110            ++p, ++i)
3111         {
3112           bresults[i].name = Gogo::unpack_hidden_name(p->name());
3113           bresults[i].btype = p->type()->get_backend(gogo);
3114           bresults[i].location = p->location();
3115         }
3116       go_assert(i == bresults.size());
3117     }
3118
3119   return gogo->backend()->function_type(breceiver, bparameters, bresults,
3120                                         this->location());
3121 }
3122
3123 // A hash table mapping function types to their backend placeholders.
3124
3125 Function_type::Placeholders Function_type::placeholders;
3126
3127 // Get the backend representation for a function type.  If we are
3128 // still converting types, and this types has multiple results, return
3129 // a placeholder instead.  We do this because for multiple results we
3130 // build a struct, and we need to make sure that all the types in the
3131 // struct are valid before we create the struct.
3132
3133 Btype*
3134 Function_type::do_get_backend(Gogo* gogo)
3135 {
3136   if (!gogo->named_types_are_converted()
3137       && this->results_ != NULL
3138       && this->results_->size() > 1)
3139     {
3140       Btype* placeholder =
3141         gogo->backend()->placeholder_pointer_type("", this->location(), true);
3142       Function_type::placeholders.push_back(std::make_pair(this, placeholder));
3143       return placeholder;
3144     }
3145   return this->get_function_backend(gogo);
3146 }
3147
3148 // Convert function types after all named types are converted.
3149
3150 void
3151 Function_type::convert_types(Gogo* gogo)
3152 {
3153   for (Placeholders::const_iterator p = Function_type::placeholders.begin();
3154        p != Function_type::placeholders.end();
3155        ++p)
3156     {
3157       Btype* bt = p->first->get_function_backend(gogo);
3158       if (!gogo->backend()->set_placeholder_function_type(p->second, bt))
3159         go_assert(saw_errors());
3160     }
3161 }
3162
3163 // The type of a function type descriptor.
3164
3165 Type*
3166 Function_type::make_function_type_descriptor_type()
3167 {
3168   static Type* ret;
3169   if (ret == NULL)
3170     {
3171       Type* tdt = Type::make_type_descriptor_type();
3172       Type* ptdt = Type::make_type_descriptor_ptr_type();
3173
3174       Type* bool_type = Type::lookup_bool_type();
3175
3176       Type* slice_type = Type::make_array_type(ptdt, NULL);
3177
3178       Struct_type* s = Type::make_builtin_struct_type(4,
3179                                                       "", tdt,
3180                                                       "dotdotdot", bool_type,
3181                                                       "in", slice_type,
3182                                                       "out", slice_type);
3183
3184       ret = Type::make_builtin_named_type("FuncType", s);
3185     }
3186
3187   return ret;
3188 }
3189
3190 // The type descriptor for a function type.
3191
3192 Expression*
3193 Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3194 {
3195   Location bloc = Linemap::predeclared_location();
3196
3197   Type* ftdt = Function_type::make_function_type_descriptor_type();
3198
3199   const Struct_field_list* fields = ftdt->struct_type()->fields();
3200
3201   Expression_list* vals = new Expression_list();
3202   vals->reserve(4);
3203
3204   Struct_field_list::const_iterator p = fields->begin();
3205   go_assert(p->is_field_name("commonType"));
3206   vals->push_back(this->type_descriptor_constructor(gogo,
3207                                                     RUNTIME_TYPE_KIND_FUNC,
3208                                                     name, NULL, true));
3209
3210   ++p;
3211   go_assert(p->is_field_name("dotdotdot"));
3212   vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
3213
3214   ++p;
3215   go_assert(p->is_field_name("in"));
3216   vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
3217                                                this->parameters()));
3218
3219   ++p;
3220   go_assert(p->is_field_name("out"));
3221   vals->push_back(this->type_descriptor_params(p->type(), NULL,
3222                                                this->results()));
3223
3224   ++p;
3225   go_assert(p == fields->end());
3226
3227   return Expression::make_struct_composite_literal(ftdt, vals, bloc);
3228 }
3229
3230 // Return a composite literal for the parameters or results of a type
3231 // descriptor.
3232
3233 Expression*
3234 Function_type::type_descriptor_params(Type* params_type,
3235                                       const Typed_identifier* receiver,
3236                                       const Typed_identifier_list* params)
3237 {
3238   Location bloc = Linemap::predeclared_location();
3239
3240   if (receiver == NULL && params == NULL)
3241     return Expression::make_slice_composite_literal(params_type, NULL, bloc);
3242
3243   Expression_list* vals = new Expression_list();
3244   vals->reserve((params == NULL ? 0 : params->size())
3245                 + (receiver != NULL ? 1 : 0));
3246
3247   if (receiver != NULL)
3248     vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
3249
3250   if (params != NULL)
3251     {
3252       for (Typed_identifier_list::const_iterator p = params->begin();
3253            p != params->end();
3254            ++p)
3255         vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
3256     }
3257
3258   return Expression::make_slice_composite_literal(params_type, vals, bloc);
3259 }
3260
3261 // The reflection string.
3262
3263 void
3264 Function_type::do_reflection(Gogo* gogo, std::string* ret) const
3265 {
3266   // FIXME: Turn this off until we straighten out the type of the
3267   // struct field used in a go statement which calls a method.
3268   // go_assert(this->receiver_ == NULL);
3269
3270   ret->append("func");
3271
3272   if (this->receiver_ != NULL)
3273     {
3274       ret->push_back('(');
3275       this->append_reflection(this->receiver_->type(), gogo, ret);
3276       ret->push_back(')');
3277     }
3278
3279   ret->push_back('(');
3280   const Typed_identifier_list* params = this->parameters();
3281   if (params != NULL)
3282     {
3283       bool is_varargs = this->is_varargs_;
3284       for (Typed_identifier_list::const_iterator p = params->begin();
3285            p != params->end();
3286            ++p)
3287         {
3288           if (p != params->begin())
3289             ret->append(", ");
3290           if (!is_varargs || p + 1 != params->end())
3291             this->append_reflection(p->type(), gogo, ret);
3292           else
3293             {
3294               ret->append("...");
3295               this->append_reflection(p->type()->array_type()->element_type(),
3296                                       gogo, ret);
3297             }
3298         }
3299     }
3300   ret->push_back(')');
3301
3302   const Typed_identifier_list* results = this->results();
3303   if (results != NULL && !results->empty())
3304     {
3305       if (results->size() == 1)
3306         ret->push_back(' ');
3307       else
3308         ret->append(" (");
3309       for (Typed_identifier_list::const_iterator p = results->begin();
3310            p != results->end();
3311            ++p)
3312         {
3313           if (p != results->begin())
3314             ret->append(", ");
3315           this->append_reflection(p->type(), gogo, ret);
3316         }
3317       if (results->size() > 1)
3318         ret->push_back(')');
3319     }
3320 }
3321
3322 // Mangled name.
3323
3324 void
3325 Function_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3326 {
3327   ret->push_back('F');
3328
3329   if (this->receiver_ != NULL)
3330     {
3331       ret->push_back('m');
3332       this->append_mangled_name(this->receiver_->type(), gogo, ret);
3333     }
3334
3335   const Typed_identifier_list* params = this->parameters();
3336   if (params != NULL)
3337     {
3338       ret->push_back('p');
3339       for (Typed_identifier_list::const_iterator p = params->begin();
3340            p != params->end();
3341            ++p)
3342         this->append_mangled_name(p->type(), gogo, ret);
3343       if (this->is_varargs_)
3344         ret->push_back('V');
3345       ret->push_back('e');
3346     }
3347
3348   const Typed_identifier_list* results = this->results();
3349   if (results != NULL)
3350     {
3351       ret->push_back('r');
3352       for (Typed_identifier_list::const_iterator p = results->begin();
3353            p != results->end();
3354            ++p)
3355         this->append_mangled_name(p->type(), gogo, ret);
3356       ret->push_back('e');
3357     }
3358
3359   ret->push_back('e');
3360 }
3361
3362 // Export a function type.
3363
3364 void
3365 Function_type::do_export(Export* exp) const
3366 {
3367   // We don't write out the receiver.  The only function types which
3368   // should have a receiver are the ones associated with explicitly
3369   // defined methods.  For those the receiver type is written out by
3370   // Function::export_func.
3371
3372   exp->write_c_string("(");
3373   bool first = true;
3374   if (this->parameters_ != NULL)
3375     {
3376       bool is_varargs = this->is_varargs_;
3377       for (Typed_identifier_list::const_iterator p =
3378              this->parameters_->begin();
3379            p != this->parameters_->end();
3380            ++p)
3381         {
3382           if (first)
3383             first = false;
3384           else
3385             exp->write_c_string(", ");
3386           if (!is_varargs || p + 1 != this->parameters_->end())
3387             exp->write_type(p->type());
3388           else
3389             {
3390               exp->write_c_string("...");
3391               exp->write_type(p->type()->array_type()->element_type());
3392             }
3393         }
3394     }
3395   exp->write_c_string(")");
3396
3397   const Typed_identifier_list* results = this->results_;
3398   if (results != NULL)
3399     {
3400       exp->write_c_string(" ");
3401       if (results->size() == 1)
3402         exp->write_type(results->begin()->type());
3403       else
3404         {
3405           first = true;
3406           exp->write_c_string("(");
3407           for (Typed_identifier_list::const_iterator p = results->begin();
3408                p != results->end();
3409                ++p)
3410             {
3411               if (first)
3412                 first = false;
3413               else
3414                 exp->write_c_string(", ");
3415               exp->write_type(p->type());
3416             }
3417           exp->write_c_string(")");
3418         }
3419     }
3420 }
3421
3422 // Import a function type.
3423
3424 Function_type*
3425 Function_type::do_import(Import* imp)
3426 {
3427   imp->require_c_string("(");
3428   Typed_identifier_list* parameters;
3429   bool is_varargs = false;
3430   if (imp->peek_char() == ')')
3431     parameters = NULL;
3432   else
3433     {
3434       parameters = new Typed_identifier_list();
3435       while (true)
3436         {
3437           if (imp->match_c_string("..."))
3438             {
3439               imp->advance(3);
3440               is_varargs = true;
3441             }
3442
3443           Type* ptype = imp->read_type();
3444           if (is_varargs)
3445             ptype = Type::make_array_type(ptype, NULL);
3446           parameters->push_back(Typed_identifier(Import::import_marker,
3447                                                  ptype, imp->location()));
3448           if (imp->peek_char() != ',')
3449             break;
3450           go_assert(!is_varargs);
3451           imp->require_c_string(", ");
3452         }
3453     }
3454   imp->require_c_string(")");
3455
3456   Typed_identifier_list* results;
3457   if (imp->peek_char() != ' ')
3458     results = NULL;
3459   else
3460     {
3461       imp->advance(1);
3462       results = new Typed_identifier_list;
3463       if (imp->peek_char() != '(')
3464         {
3465           Type* rtype = imp->read_type();
3466           results->push_back(Typed_identifier(Import::import_marker, rtype,
3467                                               imp->location()));
3468         }
3469       else
3470         {
3471           imp->advance(1);
3472           while (true)
3473             {
3474               Type* rtype = imp->read_type();
3475               results->push_back(Typed_identifier(Import::import_marker,
3476                                                   rtype, imp->location()));
3477               if (imp->peek_char() != ',')
3478                 break;
3479               imp->require_c_string(", ");
3480             }
3481           imp->require_c_string(")");
3482         }
3483     }
3484
3485   Function_type* ret = Type::make_function_type(NULL, parameters, results,
3486                                                 imp->location());
3487   if (is_varargs)
3488     ret->set_is_varargs();
3489   return ret;
3490 }
3491
3492 // Make a copy of a function type without a receiver.
3493
3494 Function_type*
3495 Function_type::copy_without_receiver() const
3496 {
3497   go_assert(this->is_method());
3498   Function_type *ret = Type::make_function_type(NULL, this->parameters_,
3499                                                 this->results_,
3500                                                 this->location_);
3501   if (this->is_varargs())
3502     ret->set_is_varargs();
3503   if (this->is_builtin())
3504     ret->set_is_builtin();
3505   return ret;
3506 }
3507
3508 // Make a copy of a function type with a receiver.
3509
3510 Function_type*
3511 Function_type::copy_with_receiver(Type* receiver_type) const
3512 {
3513   go_assert(!this->is_method());
3514   Typed_identifier* receiver = new Typed_identifier("", receiver_type,
3515                                                     this->location_);
3516   return Type::make_function_type(receiver, this->parameters_,
3517                                   this->results_, this->location_);
3518 }
3519
3520 // Make a function type.
3521
3522 Function_type*
3523 Type::make_function_type(Typed_identifier* receiver,
3524                          Typed_identifier_list* parameters,
3525                          Typed_identifier_list* results,
3526                          Location location)
3527 {
3528   return new Function_type(receiver, parameters, results, location);
3529 }
3530
3531 // Class Pointer_type.
3532
3533 // Traversal.
3534
3535 int
3536 Pointer_type::do_traverse(Traverse* traverse)
3537 {
3538   return Type::traverse(this->to_type_, traverse);
3539 }
3540
3541 // Hash code.
3542
3543 unsigned int
3544 Pointer_type::do_hash_for_method(Gogo* gogo) const
3545 {
3546   return this->to_type_->hash_for_method(gogo) << 4;
3547 }
3548
3549 // The tree for a pointer type.
3550
3551 Btype*
3552 Pointer_type::do_get_backend(Gogo* gogo)
3553 {
3554   Btype* to_btype = this->to_type_->get_backend(gogo);
3555   return gogo->backend()->pointer_type(to_btype);
3556 }
3557
3558 // The type of a pointer type descriptor.
3559
3560 Type*
3561 Pointer_type::make_pointer_type_descriptor_type()
3562 {
3563   static Type* ret;
3564   if (ret == NULL)
3565     {
3566       Type* tdt = Type::make_type_descriptor_type();
3567       Type* ptdt = Type::make_type_descriptor_ptr_type();
3568
3569       Struct_type* s = Type::make_builtin_struct_type(2,
3570                                                       "", tdt,
3571                                                       "elem", ptdt);
3572
3573       ret = Type::make_builtin_named_type("PtrType", s);
3574     }
3575
3576   return ret;
3577 }
3578
3579 // The type descriptor for a pointer type.
3580
3581 Expression*
3582 Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
3583 {
3584   if (this->is_unsafe_pointer_type())
3585     {
3586       go_assert(name != NULL);
3587       return this->plain_type_descriptor(gogo,
3588                                          RUNTIME_TYPE_KIND_UNSAFE_POINTER,
3589                                          name);
3590     }
3591   else
3592     {
3593       Location bloc = Linemap::predeclared_location();
3594
3595       const Methods* methods;
3596       Type* deref = this->points_to();
3597       if (deref->named_type() != NULL)
3598         methods = deref->named_type()->methods();
3599       else if (deref->struct_type() != NULL)
3600         methods = deref->struct_type()->methods();
3601       else
3602         methods = NULL;
3603
3604       Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
3605
3606       const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
3607
3608       Expression_list* vals = new Expression_list();
3609       vals->reserve(2);
3610
3611       Struct_field_list::const_iterator p = fields->begin();
3612       go_assert(p->is_field_name("commonType"));
3613       vals->push_back(this->type_descriptor_constructor(gogo,
3614                                                         RUNTIME_TYPE_KIND_PTR,
3615                                                         name, methods, false));
3616
3617       ++p;
3618       go_assert(p->is_field_name("elem"));
3619       vals->push_back(Expression::make_type_descriptor(deref, bloc));
3620
3621       return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
3622     }
3623 }
3624
3625 // Reflection string.
3626
3627 void
3628 Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
3629 {
3630   ret->push_back('*');
3631   this->append_reflection(this->to_type_, gogo, ret);
3632 }
3633
3634 // Mangled name.
3635
3636 void
3637 Pointer_type::do_mangled_name(Gogo* gogo, std::string* ret) const
3638 {
3639   ret->push_back('p');
3640   this->append_mangled_name(this->to_type_, gogo, ret);
3641 }
3642
3643 // Export.
3644
3645 void
3646 Pointer_type::do_export(Export* exp) const
3647 {
3648   exp->write_c_string("*");
3649   if (this->is_unsafe_pointer_type())
3650     exp->write_c_string("any");
3651   else
3652     exp->write_type(this->to_type_);
3653 }
3654
3655 // Import.
3656
3657 Pointer_type*
3658 Pointer_type::do_import(Import* imp)
3659 {
3660   imp->require_c_string("*");
3661   if (imp->match_c_string("any"))
3662     {
3663       imp->advance(3);
3664       return Type::make_pointer_type(Type::make_void_type());
3665     }
3666   Type* to = imp->read_type();
3667   return Type::make_pointer_type(to);
3668 }
3669
3670 // Make a pointer type.
3671
3672 Pointer_type*
3673 Type::make_pointer_type(Type* to_type)
3674 {
3675   typedef Unordered_map(Type*, Pointer_type*) Hashtable;
3676   static Hashtable pointer_types;
3677   Hashtable::const_iterator p = pointer_types.find(to_type);
3678   if (p != pointer_types.end())
3679     return p->second;
3680   Pointer_type* ret = new Pointer_type(to_type);
3681   pointer_types[to_type] = ret;
3682   return ret;
3683 }
3684
3685 // The nil type.  We use a special type for nil because it is not the
3686 // same as any other type.  In C term nil has type void*, but there is
3687 // no such type in Go.
3688
3689 class Nil_type : public Type
3690 {
3691  public:
3692   Nil_type()
3693     : Type(TYPE_NIL)
3694   { }
3695
3696  protected:
3697   bool
3698   do_compare_is_identity() const
3699   { return false; }
3700
3701   Btype*
3702   do_get_backend(Gogo* gogo)
3703   { return gogo->backend()->pointer_type(gogo->backend()->void_type()); }
3704
3705   Expression*
3706   do_type_descriptor(Gogo*, Named_type*)
3707   { go_unreachable(); }
3708
3709   void
3710   do_reflection(Gogo*, std::string*) const
3711   { go_unreachable(); }
3712
3713   void
3714   do_mangled_name(Gogo*, std::string* ret) const
3715   { ret->push_back('n'); }
3716 };
3717
3718 // Make the nil type.
3719
3720 Type*
3721 Type::make_nil_type()
3722 {
3723   static Nil_type singleton_nil_type;
3724   return &singleton_nil_type;
3725 }
3726
3727 // The type of a function call which returns multiple values.  This is
3728 // really a struct, but we don't want to confuse a function call which
3729 // returns a struct with a function call which returns multiple
3730 // values.
3731
3732 class Call_multiple_result_type : public Type
3733 {
3734  public:
3735   Call_multiple_result_type(Call_expression* call)
3736     : Type(TYPE_CALL_MULTIPLE_RESULT),
3737       call_(call)
3738   { }
3739
3740  protected:
3741   bool
3742   do_has_pointer() const
3743   {
3744     go_assert(saw_errors());
3745     return false;
3746   }
3747
3748   bool
3749   do_compare_is_identity() const
3750   { return false; }
3751
3752   Btype*
3753   do_get_backend(Gogo* gogo)
3754   {
3755     go_assert(saw_errors());
3756     return gogo->backend()->error_type();
3757   }
3758
3759   Expression*
3760   do_type_descriptor(Gogo*, Named_type*)
3761   {
3762     go_assert(saw_errors());
3763     return Expression::make_error(Linemap::unknown_location());
3764   }
3765
3766   void
3767   do_reflection(Gogo*, std::string*) const
3768   { go_assert(saw_errors()); }
3769
3770   void
3771   do_mangled_name(Gogo*, std::string*) const
3772   { go_assert(saw_errors()); }
3773
3774  private:
3775   // The expression being called.
3776   Call_expression* call_;
3777 };
3778
3779 // Make a call result type.
3780
3781 Type*
3782 Type::make_call_multiple_result_type(Call_expression* call)
3783 {
3784   return new Call_multiple_result_type(call);
3785 }
3786
3787 // Class Struct_field.
3788
3789 // Get the name of a field.
3790
3791 const std::string&
3792 Struct_field::field_name() const
3793 {
3794   const std::string& name(this->typed_identifier_.name());
3795   if (!name.empty())
3796     return name;
3797   else
3798     {
3799       // This is called during parsing, before anything is lowered, so
3800       // we have to be pretty careful to avoid dereferencing an
3801       // unknown type name.
3802       Type* t = this->typed_identifier_.type();
3803       Type* dt = t;
3804       if (t->classification() == Type::TYPE_POINTER)
3805         {
3806           // Very ugly.
3807           Pointer_type* ptype = static_cast<Pointer_type*>(t);
3808           dt = ptype->points_to();
3809         }
3810       if (dt->forward_declaration_type() != NULL)
3811         return dt->forward_declaration_type()->name();
3812       else if (dt->named_type() != NULL)
3813         return dt->named_type()->name();
3814       else if (t->is_error_type() || dt->is_error_type())
3815         {
3816           static const std::string error_string = "*error*";
3817           return error_string;
3818         }
3819       else
3820         {
3821           // Avoid crashing in the erroneous case where T is named but
3822           // DT is not.
3823           go_assert(t != dt);
3824           if (t->forward_declaration_type() != NULL)
3825             return t->forward_declaration_type()->name();
3826           else if (t->named_type() != NULL)
3827             return t->named_type()->name();
3828           else
3829             go_unreachable();
3830         }
3831     }
3832 }
3833
3834 // Return whether this field is named NAME.
3835
3836 bool
3837 Struct_field::is_field_name(const std::string& name) const
3838 {
3839   const std::string& me(this->typed_identifier_.name());
3840   if (!me.empty())
3841     return me == name;
3842   else
3843     {
3844       Type* t = this->typed_identifier_.type();
3845       if (t->points_to() != NULL)
3846         t = t->points_to();
3847       Named_type* nt = t->named_type();
3848       if (nt != NULL && nt->name() == name)
3849         return true;
3850
3851       // This is a horrible hack caused by the fact that we don't pack
3852       // the names of builtin types.  FIXME.
3853       if (nt != NULL
3854           && nt->is_builtin()
3855           && nt->name() == Gogo::unpack_hidden_name(name))
3856         return true;
3857
3858       return false;
3859     }
3860 }
3861
3862 // Class Struct_type.
3863
3864 // Traversal.
3865
3866 int
3867 Struct_type::do_traverse(Traverse* traverse)
3868 {
3869   Struct_field_list* fields = this->fields_;
3870   if (fields != NULL)
3871     {
3872       for (Struct_field_list::iterator p = fields->begin();
3873            p != fields->end();
3874            ++p)
3875         {
3876           if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
3877             return TRAVERSE_EXIT;
3878         }
3879     }
3880   return TRAVERSE_CONTINUE;
3881 }
3882
3883 // Verify that the struct type is complete and valid.
3884
3885 bool
3886 Struct_type::do_verify()
3887 {
3888   Struct_field_list* fields = this->fields_;
3889   if (fields == NULL)
3890     return true;
3891   bool ret = true;
3892   for (Struct_field_list::iterator p = fields->begin();
3893        p != fields->end();
3894        ++p)
3895     {
3896       Type* t = p->type();
3897       if (t->is_undefined())
3898         {
3899           error_at(p->location(), "struct field type is incomplete");
3900           p->set_type(Type::make_error_type());
3901           ret = false;
3902         }
3903       else if (p->is_anonymous())
3904         {
3905           if (t->named_type() != NULL && t->points_to() != NULL)
3906             {
3907               error_at(p->location(), "embedded type may not be a pointer");
3908               p->set_type(Type::make_error_type());
3909               return false;
3910             }
3911           if (t->points_to() != NULL
3912               && t->points_to()->interface_type() != NULL)
3913             {
3914               error_at(p->location(),
3915                        "embedded type may not be pointer to interface");
3916               p->set_type(Type::make_error_type());
3917               return false;
3918             }
3919         }
3920     }
3921   return ret;
3922 }
3923
3924 // Whether this contains a pointer.
3925
3926 bool
3927 Struct_type::do_has_pointer() const
3928 {
3929   const Struct_field_list* fields = this->fields();
3930   if (fields == NULL)
3931     return false;
3932   for (Struct_field_list::const_iterator p = fields->begin();
3933        p != fields->end();
3934        ++p)
3935     {
3936       if (p->type()->has_pointer())
3937         return true;
3938     }
3939   return false;
3940 }
3941
3942 // Whether this type is identical to T.
3943
3944 bool
3945 Struct_type::is_identical(const Struct_type* t,
3946                           bool errors_are_identical) const
3947 {
3948   const Struct_field_list* fields1 = this->fields();
3949   const Struct_field_list* fields2 = t->fields();
3950   if (fields1 == NULL || fields2 == NULL)
3951     return fields1 == fields2;
3952   Struct_field_list::const_iterator pf2 = fields2->begin();
3953   for (Struct_field_list::const_iterator pf1 = fields1->begin();
3954        pf1 != fields1->end();
3955        ++pf1, ++pf2)
3956     {
3957       if (pf2 == fields2->end())
3958         return false;
3959       if (pf1->field_name() != pf2->field_name())
3960         return false;
3961       if (pf1->is_anonymous() != pf2->is_anonymous()
3962           || !Type::are_identical(pf1->type(), pf2->type(),
3963                                   errors_are_identical, NULL))
3964         return false;
3965       if (!pf1->has_tag())
3966         {
3967           if (pf2->has_tag())
3968             return false;
3969         }
3970       else
3971         {
3972           if (!pf2->has_tag())
3973             return false;
3974           if (pf1->tag() != pf2->tag())
3975             return false;
3976         }
3977     }
3978   if (pf2 != fields2->end())
3979     return false;
3980   return true;
3981 }
3982
3983 // Whether this struct type has any hidden fields.
3984
3985 bool
3986 Struct_type::struct_has_hidden_fields(const Named_type* within,
3987                                       std::string* reason) const
3988 {
3989   const Struct_field_list* fields = this->fields();
3990   if (fields == NULL)
3991     return false;
3992   const Package* within_package = (within == NULL
3993                                    ? NULL
3994                                    : within->named_object()->package());
3995   for (Struct_field_list::const_iterator pf = fields->begin();
3996        pf != fields->end();
3997        ++pf)
3998     {
3999       if (within_package != NULL
4000           && !pf->is_anonymous()
4001           && Gogo::is_hidden_name(pf->field_name()))
4002         {
4003           if (reason != NULL)
4004             {
4005               std::string within_name = within->named_object()->message_name();
4006               std::string name = Gogo::message_name(pf->field_name());
4007               size_t bufsize = 200 + within_name.length() + name.length();
4008               char* buf = new char[bufsize];
4009               snprintf(buf, bufsize,
4010                        _("implicit assignment of %s%s%s hidden field %s%s%s"),
4011                        open_quote, within_name.c_str(), close_quote,
4012                        open_quote, name.c_str(), close_quote);
4013               reason->assign(buf);
4014               delete[] buf;
4015             }
4016           return true;
4017         }
4018
4019       if (pf->type()->has_hidden_fields(within, reason))
4020         return true;
4021     }
4022
4023   return false;
4024 }
4025
4026 // Whether comparisons of this struct type are simple identity
4027 // comparisons.
4028
4029 bool
4030 Struct_type::do_compare_is_identity() const
4031 {
4032   const Struct_field_list* fields = this->fields_;
4033   if (fields == NULL)
4034     return true;
4035   for (Struct_field_list::const_iterator pf = fields->begin();
4036        pf != fields->end();
4037        ++pf)
4038     if (!pf->type()->compare_is_identity())
4039       return false;
4040   return true;
4041 }
4042
4043 // Build identity and hash functions for this struct.
4044
4045 // Hash code.
4046
4047 unsigned int
4048 Struct_type::do_hash_for_method(Gogo* gogo) const
4049 {
4050   unsigned int ret = 0;
4051   if (this->fields() != NULL)
4052     {
4053       for (Struct_field_list::const_iterator pf = this->fields()->begin();
4054            pf != this->fields()->end();
4055            ++pf)
4056         ret = (ret << 1) + pf->type()->hash_for_method(gogo);
4057     }
4058   return ret <<= 2;
4059 }
4060
4061 // Find the local field NAME.
4062
4063 const Struct_field*
4064 Struct_type::find_local_field(const std::string& name,
4065                               unsigned int *pindex) const
4066 {
4067   const Struct_field_list* fields = this->fields_;
4068   if (fields == NULL)
4069     return NULL;
4070   unsigned int i = 0;
4071   for (Struct_field_list::const_iterator pf = fields->begin();
4072        pf != fields->end();
4073        ++pf, ++i)
4074     {
4075       if (pf->is_field_name(name))
4076         {
4077           if (pindex != NULL)
4078             *pindex = i;
4079           return &*pf;
4080         }
4081     }
4082   return NULL;
4083 }
4084
4085 // Return an expression for field NAME in STRUCT_EXPR, or NULL.
4086
4087 Field_reference_expression*
4088 Struct_type::field_reference(Expression* struct_expr, const std::string& name,
4089                              Location location) const
4090 {
4091   unsigned int depth;
4092   return this->field_reference_depth(struct_expr, name, location, NULL,
4093                                      &depth);
4094 }
4095
4096 // Return an expression for a field, along with the depth at which it
4097 // was found.
4098
4099 Field_reference_expression*
4100 Struct_type::field_reference_depth(Expression* struct_expr,
4101                                    const std::string& name,
4102                                    Location location,
4103                                    Saw_named_type* saw,
4104                                    unsigned int* depth) const
4105 {
4106   const Struct_field_list* fields = this->fields_;
4107   if (fields == NULL)
4108     return NULL;
4109
4110   // Look for a field with this name.
4111   unsigned int i = 0;
4112   for (Struct_field_list::const_iterator pf = fields->begin();
4113        pf != fields->end();
4114        ++pf, ++i)
4115     {
4116       if (pf->is_field_name(name))
4117         {
4118           *depth = 0;
4119           return Expression::make_field_reference(struct_expr, i, location);
4120         }
4121     }
4122
4123   // Look for an anonymous field which contains a field with this
4124   // name.
4125   unsigned int found_depth = 0;
4126   Field_reference_expression* ret = NULL;
4127   i = 0;
4128   for (Struct_field_list::const_iterator pf = fields->begin();
4129        pf != fields->end();
4130        ++pf, ++i)
4131     {
4132       if (!pf->is_anonymous())
4133         continue;
4134
4135       Struct_type* st = pf->type()->deref()->struct_type();
4136       if (st == NULL)
4137         continue;
4138
4139       Saw_named_type* hold_saw = saw;
4140       Saw_named_type saw_here;
4141       Named_type* nt = pf->type()->named_type();
4142       if (nt == NULL)
4143         nt = pf->type()->deref()->named_type();
4144       if (nt != NULL)
4145         {
4146           Saw_named_type* q;
4147           for (q = saw; q != NULL; q = q->next)
4148             {
4149               if (q->nt == nt)
4150                 {
4151                   // If this is an error, it will be reported
4152                   // elsewhere.
4153                   break;
4154                 }
4155             }