OSDN Git Service

compiler: Permit type B byte; type S []B; var v = S("x").
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / types.h
1 // types.h -- Go frontend types.     -*- C++ -*-
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 #ifndef GO_TYPES_H
8 #define GO_TYPES_H
9
10 #include "go-linemap.h"
11
12 class Gogo;
13 class Package;
14 class Traverse;
15 class Typed_identifier;
16 class Typed_identifier_list;
17 class Integer_type;
18 class Float_type;
19 class Complex_type;
20 class String_type;
21 class Function_type;
22 class Struct_field;
23 class Struct_field_list;
24 class Struct_type;
25 class Pointer_type;
26 class Array_type;
27 class Map_type;
28 class Channel_type;
29 class Interface_type;
30 class Named_type;
31 class Forward_declaration_type;
32 class Method;
33 class Methods;
34 class Type_hash_identical;
35 class Type_identical;
36 class Expression;
37 class Expression_list;
38 class Call_expression;
39 class Field_reference_expression;
40 class Bound_method_expression;
41 class Bindings;
42 class Named_object;
43 class Function;
44 class Translate_context;
45 class Export;
46 class Import;
47 class Btype;
48 class Bexpression;
49 class Bvariable;
50
51 // Type codes used in type descriptors.  These must match the values
52 // in libgo/runtime/go-type.h.  They also match the values in the gc
53 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
54 // although this is not required.
55
56 static const int RUNTIME_TYPE_KIND_BOOL = 1;
57 static const int RUNTIME_TYPE_KIND_INT = 2;
58 static const int RUNTIME_TYPE_KIND_INT8 = 3;
59 static const int RUNTIME_TYPE_KIND_INT16 = 4;
60 static const int RUNTIME_TYPE_KIND_INT32 = 5;
61 static const int RUNTIME_TYPE_KIND_INT64 = 6;
62 static const int RUNTIME_TYPE_KIND_UINT = 7;
63 static const int RUNTIME_TYPE_KIND_UINT8 = 8;
64 static const int RUNTIME_TYPE_KIND_UINT16 = 9;
65 static const int RUNTIME_TYPE_KIND_UINT32 = 10;
66 static const int RUNTIME_TYPE_KIND_UINT64 = 11;
67 static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
68 static const int RUNTIME_TYPE_KIND_FLOAT32 = 13;
69 static const int RUNTIME_TYPE_KIND_FLOAT64 = 14;
70 static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15;
71 static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16;
72 static const int RUNTIME_TYPE_KIND_ARRAY = 17;
73 static const int RUNTIME_TYPE_KIND_CHAN = 18;
74 static const int RUNTIME_TYPE_KIND_FUNC = 19;
75 static const int RUNTIME_TYPE_KIND_INTERFACE = 20;
76 static const int RUNTIME_TYPE_KIND_MAP = 21;
77 static const int RUNTIME_TYPE_KIND_PTR = 22;
78 static const int RUNTIME_TYPE_KIND_SLICE = 23;
79 static const int RUNTIME_TYPE_KIND_STRING = 24;
80 static const int RUNTIME_TYPE_KIND_STRUCT = 25;
81 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26;
82
83 static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7);
84
85 // To build the complete list of methods for a named type we need to
86 // gather all methods from anonymous fields.  Those methods may
87 // require an arbitrary set of indirections and field offsets.  There
88 // is also the possibility of ambiguous methods, which we could ignore
89 // except that we want to give a better error message for that case.
90 // This is a base class.  There are two types of methods: named
91 // methods, and methods which are inherited from an anonymous field of
92 // interface type.
93
94 class Method
95 {
96  public:
97   // For methods in anonymous types we need to know the sequence of
98   // field references used to extract the pointer to pass to the
99   // method.  Since each method for a particular anonymous field will
100   // have the sequence of field indexes, and since the indexes can be
101   // shared going down the chain, we use a manually managed linked
102   // list.  The first entry in the list is the field index for the
103   // last field, the one passed to the method.
104
105   struct Field_indexes
106   {
107     const Field_indexes* next;
108     unsigned int field_index;
109   };
110
111   virtual ~Method()
112   { }
113
114   // Get the list of field indexes.
115   const Field_indexes*
116   field_indexes() const
117   { return this->field_indexes_; }
118
119   // Get the depth.
120   unsigned int
121   depth() const
122   { return this->depth_; }
123
124   // Return whether this is a value method--a method which does not
125   // require a pointer expression.
126   bool
127   is_value_method() const
128   { return this->is_value_method_; }
129
130   // Return whether we need a stub method--this is true if we can't
131   // just pass the main object to the method.
132   bool
133   needs_stub_method() const
134   { return this->needs_stub_method_; }
135
136   // Return whether this is an ambiguous method name.
137   bool
138   is_ambiguous() const
139   { return this->is_ambiguous_; }
140
141   // Note that this method is ambiguous.
142   void
143   set_is_ambiguous()
144   { this->is_ambiguous_ = true; }
145
146   // Return the type of the method.
147   Function_type*
148   type() const
149   { return this->do_type(); }
150
151   // Return the location of the method receiver.
152   Location
153   receiver_location() const
154   { return this->do_receiver_location(); }
155
156   // Return an expression which binds this method to EXPR.  This is
157   // something which can be used with a function call.
158   Expression*
159   bind_method(Expression* expr, Location location) const;
160
161   // Return the named object for this method.  This may only be called
162   // after methods are finalized.
163   Named_object*
164   named_object() const;
165
166   // Get the stub object.
167   Named_object*
168   stub_object() const
169   {
170     go_assert(this->stub_ != NULL);
171     return this->stub_;
172   }
173
174   // Set the stub object.
175   void
176   set_stub_object(Named_object* no)
177   {
178     go_assert(this->stub_ == NULL);
179     this->stub_ = no;
180   }
181
182  protected:
183   // These objects are only built by the child classes.
184   Method(const Field_indexes* field_indexes, unsigned int depth,
185          bool is_value_method, bool needs_stub_method)
186     : field_indexes_(field_indexes), depth_(depth), stub_(NULL),
187       is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
188       is_ambiguous_(false)
189   { }
190
191   // The named object for this method.
192   virtual Named_object*
193   do_named_object() const = 0;
194
195   // The type of the method.
196   virtual Function_type*
197   do_type() const = 0;
198
199   // Return the location of the method receiver.
200   virtual Location
201   do_receiver_location() const = 0;
202
203   // Bind a method to an object.
204   virtual Expression*
205   do_bind_method(Expression* expr, Location location) const = 0;
206
207  private:
208   // The sequence of field indexes used for this method.  If this is
209   // NULL, then the method is defined for the current type.
210   const Field_indexes* field_indexes_;
211   // The depth at which this method was found.
212   unsigned int depth_;
213   // If a stub method is required, this is its object.  This is only
214   // set after stub methods are built in finalize_methods.
215   Named_object* stub_;
216   // Whether this is a value method--a method that does not require a
217   // pointer.
218   bool is_value_method_;
219   // Whether a stub method is required.
220   bool needs_stub_method_;
221   // Whether this method is ambiguous.
222   bool is_ambiguous_;
223 };
224
225 // A named method.  This is what you get with a method declaration,
226 // either directly on the type, or inherited from some anonymous
227 // embedded field.
228
229 class Named_method : public Method
230 {
231  public:
232   Named_method(Named_object* named_object, const Field_indexes* field_indexes,
233                unsigned int depth, bool is_value_method,
234                bool needs_stub_method)
235     : Method(field_indexes, depth, is_value_method, needs_stub_method),
236       named_object_(named_object)
237   { }
238
239  protected:
240   // Get the Named_object for the method.
241   Named_object*
242   do_named_object() const
243   { return this->named_object_; }
244
245   // The type of the method.
246   Function_type*
247   do_type() const;
248
249   // Return the location of the method receiver.
250   Location
251   do_receiver_location() const;
252
253   // Bind a method to an object.
254   Expression*
255   do_bind_method(Expression* expr, Location location) const;
256
257  private:
258   // The method itself.  For a method which needs a stub, this starts
259   // out as the underlying method, and is later replaced with the stub
260   // method.
261   Named_object* named_object_;
262 };
263
264 // An interface method.  This is used when an interface appears as an
265 // anonymous field in a named struct.
266
267 class Interface_method : public Method
268 {
269  public:
270   Interface_method(const std::string& name, Location location,
271                    Function_type* fntype, const Field_indexes* field_indexes,
272                    unsigned int depth)
273     : Method(field_indexes, depth, true, true),
274       name_(name), location_(location), fntype_(fntype)
275   { }
276
277  protected:
278   // Get the Named_object for the method.  This should never be
279   // called, as we always create a stub.
280   Named_object*
281   do_named_object() const
282   { go_unreachable(); }
283
284   // The type of the method.
285   Function_type*
286   do_type() const
287   { return this->fntype_; }
288
289   // Return the location of the method receiver.
290   Location
291   do_receiver_location() const
292   { return this->location_; }
293
294   // Bind a method to an object.
295   Expression*
296   do_bind_method(Expression* expr, Location location) const;
297
298  private:
299   // The name of the interface method to call.
300   std::string name_;
301   // The location of the definition of the interface method.
302   Location location_;
303   // The type of the interface method.
304   Function_type* fntype_;
305 };
306
307 // A mapping from method name to Method.  This is a wrapper around a
308 // hash table.
309
310 class Methods
311 {
312  private:
313   typedef Unordered_map(std::string, Method*) Method_map;
314
315  public:
316   typedef Method_map::const_iterator const_iterator;
317
318   Methods()
319     : methods_()
320   { }
321
322   // Insert a new method.  Returns true if it was inserted, false if
323   // it was overidden or ambiguous.
324   bool
325   insert(const std::string& name, Method* m);
326
327   // The number of (unambiguous) methods.
328   size_t
329   count() const;
330
331   // Iterate.
332   const_iterator
333   begin() const
334   { return this->methods_.begin(); }
335
336   const_iterator
337   end() const
338   { return this->methods_.end(); }
339
340   // Lookup.
341   const_iterator
342   find(const std::string& name) const
343   { return this->methods_.find(name); }
344
345  private:
346   Method_map methods_;
347 };
348
349 // The base class for all types.
350
351 class Type
352 {
353  public:
354   // The types of types.
355   enum Type_classification
356   {
357     TYPE_ERROR,
358     TYPE_VOID,
359     TYPE_BOOLEAN,
360     TYPE_INTEGER,
361     TYPE_FLOAT,
362     TYPE_COMPLEX,
363     TYPE_STRING,
364     TYPE_SINK,
365     TYPE_FUNCTION,
366     TYPE_POINTER,
367     TYPE_NIL,
368     TYPE_CALL_MULTIPLE_RESULT,
369     TYPE_STRUCT,
370     TYPE_ARRAY,
371     TYPE_MAP,
372     TYPE_CHANNEL,
373     TYPE_INTERFACE,
374     TYPE_NAMED,
375     TYPE_FORWARD
376   };
377
378   virtual ~Type();
379
380   // Creators.
381
382   static Type*
383   make_error_type();
384
385   static Type*
386   make_void_type();
387
388   // Get the unnamed bool type.
389   static Type*
390   make_boolean_type();
391
392   // Get the named type "bool".
393   static Named_type*
394   lookup_bool_type();
395
396   // Make the named type "bool".
397   static Named_type*
398   make_named_bool_type();
399
400   // Make an abstract integer type.
401   static Integer_type*
402   make_abstract_integer_type();
403
404   // Make a named integer type with a specified size.
405   // RUNTIME_TYPE_KIND is the code to use in reflection information,
406   // to distinguish int and int32.
407   static Named_type*
408   make_integer_type(const char* name, bool is_unsigned, int bits,
409                     int runtime_type_kind);
410
411   // Look up a named integer type.
412   static Named_type*
413   lookup_integer_type(const char* name);
414
415   // Make an abstract floating point type.
416   static Float_type*
417   make_abstract_float_type();
418
419   // Make a named floating point type with a specific size.
420   // RUNTIME_TYPE_KIND is the code to use in reflection information,
421   // to distinguish float and float32.
422   static Named_type*
423   make_float_type(const char* name, int bits, int runtime_type_kind);
424
425   // Look up a named float type.
426   static Named_type*
427   lookup_float_type(const char* name);
428
429   // Make an abstract complex type.
430   static Complex_type*
431   make_abstract_complex_type();
432
433   // Make a named complex type with a specific size.
434   // RUNTIME_TYPE_KIND is the code to use in reflection information,
435   // to distinguish complex and complex64.
436   static Named_type*
437   make_complex_type(const char* name, int bits, int runtime_type_kind);
438
439   // Look up a named complex type.
440   static Named_type*
441   lookup_complex_type(const char* name);
442
443   // Get the unnamed string type.
444   static Type*
445   make_string_type();
446
447   // Get the named type "string".
448   static Named_type*
449   lookup_string_type();
450
451   // Make the named type "string".
452   static Named_type*
453   make_named_string_type();
454
455   static Type*
456   make_sink_type();
457
458   static Function_type*
459   make_function_type(Typed_identifier* receiver,
460                      Typed_identifier_list* parameters,
461                      Typed_identifier_list* results,
462                      Location);
463
464   static Pointer_type*
465   make_pointer_type(Type*);
466
467   static Type*
468   make_nil_type();
469
470   static Type*
471   make_call_multiple_result_type(Call_expression*);
472
473   static Struct_type*
474   make_struct_type(Struct_field_list* fields, Location);
475
476   static Array_type*
477   make_array_type(Type* element_type, Expression* length);
478
479   static Map_type*
480   make_map_type(Type* key_type, Type* value_type, Location);
481
482   static Channel_type*
483   make_channel_type(bool send, bool receive, Type*);
484
485   static Interface_type*
486   make_interface_type(Typed_identifier_list* methods, Location);
487
488   static Type*
489   make_type_descriptor_type();
490
491   static Type*
492   make_type_descriptor_ptr_type();
493
494   static Named_type*
495   make_named_type(Named_object*, Type*, Location);
496
497   static Type*
498   make_forward_declaration(Named_object*);
499
500   // Traverse a type.
501   static int
502   traverse(Type*, Traverse*);
503
504   // Verify the type.  This is called after parsing, and verifies that
505   // types are complete and meet the language requirements.  This
506   // returns false if the type is invalid.
507   bool
508   verify()
509   { return this->do_verify(); }
510
511   // Return true if two types are identical.  If ERRORS_ARE_IDENTICAL,
512   // returns that an erroneous type is identical to any other type;
513   // this is used to avoid cascading errors.  If this returns false,
514   // and REASON is not NULL, it may set *REASON.
515   static bool
516   are_identical(const Type* lhs, const Type* rhs, bool errors_are_identical,
517                 std::string* reason);
518
519   // Return true if two types are compatible for use in a binary
520   // operation, other than a shift, comparison, or channel send.  This
521   // is an equivalence relation.
522   static bool
523   are_compatible_for_binop(const Type* t1, const Type* t2);
524
525   // Return true if two types are compatible for use with the
526   // comparison operator.  IS_EQUALITY_OP is true if this is an
527   // equality comparison, false if it is an ordered comparison.  This
528   // is an equivalence relation.  If this returns false, and REASON is
529   // not NULL, it sets *REASON.
530   static bool
531   are_compatible_for_comparison(bool is_equality_op, const Type *t1,
532                                 const Type *t2, std::string* reason);
533
534   // Return true if a type is comparable with itself.  This is true of
535   // most types, but false for, e.g., function types.
536   bool
537   is_comparable() const
538   { return Type::are_compatible_for_comparison(true, this, this, NULL); }
539
540   // Return true if a value with type RHS is assignable to a variable
541   // with type LHS.  This is not an equivalence relation.  If this
542   // returns false, and REASON is not NULL, it sets *REASON.
543   static bool
544   are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
545
546   // Return true if a value with type RHS is assignable to a variable
547   // with type LHS, ignoring any assignment of hidden fields
548   // (unexported fields of a type imported from another package).
549   // This is like the are_assignable method.
550   static bool
551   are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
552                            std::string* reason);
553
554   // Return true if a value with type RHS may be converted to type
555   // LHS.  If this returns false, and REASON is not NULL, it sets
556   // *REASON.
557   static bool
558   are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
559
560   // Whether this type has any hidden fields which are not visible in
561   // the current compilation, such as a field whose name begins with a
562   // lower case letter in a struct imported from a different package.
563   // WITHIN is not NULL if we are looking at fields in a named type.
564   bool
565   has_hidden_fields(const Named_type* within, std::string* reason) const;
566
567   // Return true if values of this type can be compared using an
568   // identity function which gets nothing but a pointer to the value
569   // and a size.
570   bool
571   compare_is_identity(Gogo* gogo) const
572   { return this->do_compare_is_identity(gogo); }
573
574   // Return a hash code for this type for the method hash table.
575   // Types which are equivalent according to are_identical will have
576   // the same hash code.
577   unsigned int
578   hash_for_method(Gogo*) const;
579
580   // Return the type classification.
581   Type_classification
582   classification() const
583   { return this->classification_; }
584
585   // Return the base type for this type.  This looks through forward
586   // declarations and names.  Using this with a forward declaration
587   // which has not been defined will return an error type.
588   Type*
589   base();
590
591   const Type*
592   base() const;
593
594   // Return the type skipping defined forward declarations.  If this
595   // type is a forward declaration which has not been defined, it will
596   // return the Forward_declaration_type.  This differs from base() in
597   // that it will return a Named_type, and for a
598   // Forward_declaration_type which is not defined it will return that
599   // type rather than an error type.
600   Type*
601   forwarded();
602
603   const Type*
604   forwarded() const;
605
606   // Return true if this is a basic type: a type which is not composed
607   // of other types, and is not void.
608   bool
609   is_basic_type() const;
610
611   // Return true if this is an abstract type--an integer, floating
612   // point, or complex type whose size has not been determined.
613   bool
614   is_abstract() const;
615
616   // Return a non-abstract version of an abstract type.
617   Type*
618   make_non_abstract_type();
619
620   // Return true if this type is or contains a pointer.  This
621   // determines whether the garbage collector needs to look at a value
622   // of this type.
623   bool
624   has_pointer() const
625   { return this->do_has_pointer(); }
626
627   // Return true if this is the error type.  This returns false for a
628   // type which is not defined, as it is called by the parser before
629   // all types are defined.
630   bool
631   is_error_type() const;
632
633   // Return true if this is the error type or if the type is
634   // undefined.  If the type is undefined, this will give an error.
635   // This should only be called after parsing is complete.
636   bool
637   is_error() const
638   { return this->base()->is_error_type(); }
639
640   // Return true if this is a void type.
641   bool
642   is_void_type() const
643   { return this->classification_ == TYPE_VOID; }
644
645   // If this is an integer type, return the Integer_type.  Otherwise,
646   // return NULL.  This is a controlled dynamic_cast.
647   Integer_type*
648   integer_type()
649   { return this->convert<Integer_type, TYPE_INTEGER>(); }
650
651   const Integer_type*
652   integer_type() const
653   { return this->convert<const Integer_type, TYPE_INTEGER>(); }
654
655   // If this is a floating point type, return the Float_type.
656   // Otherwise, return NULL.  This is a controlled dynamic_cast.
657   Float_type*
658   float_type()
659   { return this->convert<Float_type, TYPE_FLOAT>(); }
660
661   const Float_type*
662   float_type() const
663   { return this->convert<const Float_type, TYPE_FLOAT>(); }
664
665   // If this is a complex type, return the Complex_type.  Otherwise,
666   // return NULL.
667   Complex_type*
668   complex_type()
669   { return this->convert<Complex_type, TYPE_COMPLEX>(); }
670
671   const Complex_type*
672   complex_type() const
673   { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
674
675   // Return true if this is a boolean type.
676   bool
677   is_boolean_type() const
678   { return this->base()->classification_ == TYPE_BOOLEAN; }
679
680   // Return true if this is an abstract boolean type.
681   bool
682   is_abstract_boolean_type() const
683   { return this->classification_ == TYPE_BOOLEAN; }
684
685   // Return true if this is a string type.
686   bool
687   is_string_type() const
688   { return this->base()->classification_ == TYPE_STRING; }
689
690   // Return true if this is an abstract string type.
691   bool
692   is_abstract_string_type() const
693   { return this->classification_ == TYPE_STRING; }
694
695   // Return true if this is the sink type.  This is the type of the
696   // blank identifier _.
697   bool
698   is_sink_type() const
699   { return this->base()->classification_ == TYPE_SINK; }
700
701   // If this is a function type, return it.  Otherwise, return NULL.
702   Function_type*
703   function_type()
704   { return this->convert<Function_type, TYPE_FUNCTION>(); }
705
706   const Function_type*
707   function_type() const
708   { return this->convert<const Function_type, TYPE_FUNCTION>(); }
709
710   // If this is a pointer type, return the type to which it points.
711   // Otherwise, return NULL.
712   Type*
713   points_to() const;
714
715   // If this is a pointer type, return the type to which it points.
716   // Otherwise, return the type itself.
717   Type*
718   deref()
719   {
720     Type* pt = this->points_to();
721     return pt != NULL ? pt : this;
722   }
723
724   const Type*
725   deref() const
726   {
727     const Type* pt = this->points_to();
728     return pt != NULL ? pt : this;
729   }
730
731   // Return true if this is the nil type.  We don't use base() here,
732   // because this can be called during parse, and there is no way to
733   // name the nil type anyhow.
734   bool
735   is_nil_type() const
736   { return this->classification_ == TYPE_NIL; }
737
738   // Return true if this is the predeclared constant nil being used as
739   // a type.  This is what the parser produces for type switches which
740   // use "case nil".
741   bool
742   is_nil_constant_as_type() const;
743
744   // Return true if this is the return type of a function which
745   // returns multiple values.
746   bool
747   is_call_multiple_result_type() const
748   { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
749
750   // If this is a struct type, return it.  Otherwise, return NULL.
751   Struct_type*
752   struct_type()
753   { return this->convert<Struct_type, TYPE_STRUCT>(); }
754
755   const Struct_type*
756   struct_type() const
757   { return this->convert<const Struct_type, TYPE_STRUCT>(); }
758
759   // If this is an array type, return it.  Otherwise, return NULL.
760   Array_type*
761   array_type()
762   { return this->convert<Array_type, TYPE_ARRAY>(); }
763
764   const Array_type*
765   array_type() const
766   { return this->convert<const Array_type, TYPE_ARRAY>(); }
767
768   // Return whether if this is a slice type.
769   bool
770   is_slice_type() const;
771
772   // If this is a map type, return it.  Otherwise, return NULL.
773   Map_type*
774   map_type()
775   { return this->convert<Map_type, TYPE_MAP>(); }
776
777   const Map_type*
778   map_type() const
779   { return this->convert<const Map_type, TYPE_MAP>(); }
780
781   // If this is a channel type, return it.  Otherwise, return NULL.
782   Channel_type*
783   channel_type()
784   { return this->convert<Channel_type, TYPE_CHANNEL>(); }
785
786   const Channel_type*
787   channel_type() const
788   { return this->convert<const Channel_type, TYPE_CHANNEL>(); }
789
790   // If this is an interface type, return it.  Otherwise, return NULL.
791   Interface_type*
792   interface_type()
793   { return this->convert<Interface_type, TYPE_INTERFACE>(); }
794
795   const Interface_type*
796   interface_type() const
797   { return this->convert<const Interface_type, TYPE_INTERFACE>(); }
798
799   // If this is a named type, return it.  Otherwise, return NULL.
800   Named_type*
801   named_type();
802
803   const Named_type*
804   named_type() const;
805
806   // If this is a forward declaration, return it.  Otherwise, return
807   // NULL.
808   Forward_declaration_type*
809   forward_declaration_type()
810   { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
811
812   const Forward_declaration_type*
813   forward_declaration_type() const
814   {
815     return this->convert_no_base<const Forward_declaration_type,
816                                  TYPE_FORWARD>();
817   }
818
819   // Return true if this type is not yet defined.
820   bool
821   is_undefined() const;
822
823   // Return true if this is the unsafe.pointer type.  We currently
824   // represent that as pointer-to-void.
825   bool
826   is_unsafe_pointer_type() const
827   { return this->points_to() != NULL && this->points_to()->is_void_type(); }
828
829   // Look for field or method NAME for TYPE.  Return an expression for
830   // it, bound to EXPR.
831   static Expression*
832   bind_field_or_method(Gogo*, const Type* type, Expression* expr,
833                        const std::string& name, Location);
834
835   // Return true if NAME is an unexported field or method of TYPE.
836   static bool
837   is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
838                                 std::vector<const Named_type*>*);
839
840   // Convert the builtin named types.
841   static void
842   convert_builtin_named_types(Gogo*);
843
844   // Return the backend representation of this type.
845   Btype*
846   get_backend(Gogo*);
847
848   // Build a type descriptor entry for this type.  Return a pointer to
849   // it.  The location is the location which causes us to need the
850   // entry.
851   tree
852   type_descriptor_pointer(Gogo* gogo, Location);
853
854   // Return the type reflection string for this type.
855   std::string
856   reflection(Gogo*) const;
857
858   // Return a mangled name for the type.  This is a name which can be
859   // used in assembler code.  Identical types should have the same
860   // manged name.
861   std::string
862   mangled_name(Gogo*) const;
863
864   // If the size of the type can be determined, set *PSIZE to the size
865   // in bytes and return true.  Otherwise, return false.  This queries
866   // the backend.
867   bool
868   backend_type_size(Gogo*, unsigned int* psize);
869
870   // If the alignment of the type can be determined, set *PALIGN to
871   // the alignment in bytes and return true.  Otherwise, return false.
872   bool
873   backend_type_align(Gogo*, unsigned int* palign);
874
875   // If the alignment of a struct field of this type can be
876   // determined, set *PALIGN to the alignment in bytes and return
877   // true.  Otherwise, return false.
878   bool
879   backend_type_field_align(Gogo*, unsigned int* palign);
880
881   // Whether the backend size is known.
882   bool
883   is_backend_type_size_known(Gogo*);
884
885   // Get the hash and equality functions for a type.
886   void
887   type_functions(Gogo*, Named_type* name, Function_type* hash_fntype,
888                  Function_type* equal_fntype, Named_object** hash_fn,
889                  Named_object** equal_fn);
890
891   // Write the hash and equality type functions.
892   void
893   write_specific_type_functions(Gogo*, Named_type*,
894                                 const std::string& hash_name,
895                                 Function_type* hash_fntype,
896                                 const std::string& equal_name,
897                                 Function_type* equal_fntype);
898
899   // Export the type.
900   void
901   export_type(Export* exp) const
902   { this->do_export(exp); }
903
904   // Import a type.
905   static Type*
906   import_type(Import*);
907
908  protected:
909   Type(Type_classification);
910
911   // Functions implemented by the child class.
912
913   // Traverse the subtypes.
914   virtual int
915   do_traverse(Traverse*);
916
917   // Verify the type.
918   virtual bool
919   do_verify()
920   { return true; }
921
922   virtual bool
923   do_has_pointer() const
924   { return false; }
925
926   virtual bool
927   do_compare_is_identity(Gogo*) const = 0;
928
929   virtual unsigned int
930   do_hash_for_method(Gogo*) const;
931
932   virtual Btype*
933   do_get_backend(Gogo*) = 0;
934
935   virtual Expression*
936   do_type_descriptor(Gogo*, Named_type* name) = 0;
937
938   virtual void
939   do_reflection(Gogo*, std::string*) const = 0;
940
941   virtual void
942   do_mangled_name(Gogo*, std::string*) const = 0;
943
944   virtual void
945   do_export(Export*) const;
946
947   // Return whether a method expects a pointer as the receiver.
948   static bool
949   method_expects_pointer(const Named_object*);
950
951   // Finalize the methods for a type.
952   static void
953   finalize_methods(Gogo*, const Type*, Location, Methods**);
954
955   // Return a method from a set of methods.
956   static Method*
957   method_function(const Methods*, const std::string& name,
958                   bool* is_ambiguous);
959
960   // Return a composite literal for the type descriptor entry for a
961   // type.
962   static Expression*
963   type_descriptor(Gogo*, Type*);
964
965   // Return a composite literal for the type descriptor entry for
966   // TYPE, using NAME as the name of the type.
967   static Expression*
968   named_type_descriptor(Gogo*, Type* type, Named_type* name);
969
970   // Return a composite literal for a plain type descriptor for this
971   // type with the given kind and name.
972   Expression*
973   plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
974
975   // Build a composite literal for the basic type descriptor.
976   Expression*
977   type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
978                               const Methods*, bool only_value_methods);
979
980   // Make a builtin struct type from a list of fields.
981   static Struct_type*
982   make_builtin_struct_type(int nfields, ...);
983
984   // Make a builtin named type.
985   static Named_type*
986   make_builtin_named_type(const char* name, Type* type);
987
988   // For the benefit of child class reflection string generation.
989   void
990   append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
991   { type->do_reflection(gogo, ret); }
992
993   // For the benefit of child class mangling.
994   void
995   append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
996   { type->do_mangled_name(gogo, ret); }
997
998   // Incorporate a string into a hash code.
999   static unsigned int
1000   hash_string(const std::string&, unsigned int);
1001
1002   // Return the backend representation for the underlying type of a
1003   // named type.
1004   static Btype*
1005   get_named_base_btype(Gogo* gogo, Type* base_type)
1006   { return base_type->get_btype_without_hash(gogo); }
1007
1008  private:
1009   // Convert to the desired type classification, or return NULL.  This
1010   // is a controlled dynamic_cast.
1011   template<typename Type_class, Type_classification type_classification>
1012   Type_class*
1013   convert()
1014   {
1015     Type* base = this->base();
1016     return (base->classification_ == type_classification
1017             ? static_cast<Type_class*>(base)
1018             : NULL);
1019   }
1020
1021   template<typename Type_class, Type_classification type_classification>
1022   const Type_class*
1023   convert() const
1024   {
1025     const Type* base = this->base();
1026     return (base->classification_ == type_classification
1027             ? static_cast<Type_class*>(base)
1028             : NULL);
1029   }
1030
1031   template<typename Type_class, Type_classification type_classification>
1032   Type_class*
1033   convert_no_base()
1034   {
1035     return (this->classification_ == type_classification
1036             ? static_cast<Type_class*>(this)
1037             : NULL);
1038   }
1039
1040   template<typename Type_class, Type_classification type_classification>
1041   const Type_class*
1042   convert_no_base() const
1043   {
1044     return (this->classification_ == type_classification
1045             ? static_cast<Type_class*>(this)
1046             : NULL);
1047   }
1048
1049   // Support for are_assignable and are_assignable_hidden_ok.
1050   static bool
1051   are_assignable_check_hidden(const Type* lhs, const Type* rhs,
1052                               bool check_hidden_fields, std::string* reason);
1053
1054   // Map unnamed types to type descriptor decls.
1055   typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
1056                              Type_identical) Type_descriptor_vars;
1057
1058   static Type_descriptor_vars type_descriptor_vars;
1059
1060   // Build the type descriptor variable for this type.
1061   void
1062   make_type_descriptor_var(Gogo*);
1063
1064   // Return the name of the type descriptor variable.  If NAME is not
1065   // NULL, it is the name to use.
1066   std::string
1067   type_descriptor_var_name(Gogo*, Named_type* name);
1068
1069   // Return true if the type descriptor for this type should be
1070   // defined in some other package.  If NAME is not NULL, it is the
1071   // name of this type.  If this returns true it sets *PACKAGE to the
1072   // package where the type descriptor is defined.
1073   bool
1074   type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
1075
1076   // Build the hash and equality type functions for a type which needs
1077   // specific functions.
1078   void
1079   specific_type_functions(Gogo*, Named_type*, Function_type* hash_fntype,
1080                           Function_type* equal_fntype, Named_object** hash_fn,
1081                           Named_object** equal_fn);
1082
1083   // Build a composite literal for the uncommon type information.
1084   Expression*
1085   uncommon_type_constructor(Gogo*, Type* uncommon_type,
1086                             Named_type*, const Methods*,
1087                             bool only_value_methods) const;
1088
1089   // Build a composite literal for the methods.
1090   Expression*
1091   methods_constructor(Gogo*, Type* methods_type, const Methods*,
1092                       bool only_value_methods) const;
1093
1094   // Build a composite literal for one method.
1095   Expression*
1096   method_constructor(Gogo*, Type* method_type, const std::string& name,
1097                      const Method*, bool only_value_methods) const;
1098
1099   static tree
1100   build_receive_return_type(tree type);
1101
1102   // A hash table we use to avoid infinite recursion.
1103   typedef Unordered_set_hash(const Named_type*, Type_hash_identical,
1104                              Type_identical) Types_seen;
1105
1106   // Add all methods for TYPE to the list of methods for THIS.
1107   static void
1108   add_methods_for_type(const Type* type, const Method::Field_indexes*,
1109                        unsigned int depth, bool, bool, Types_seen*,
1110                        Methods**);
1111
1112   static void
1113   add_local_methods_for_type(const Named_type* type,
1114                              const Method::Field_indexes*,
1115                              unsigned int depth, bool, bool, Methods**);
1116
1117   static void
1118   add_embedded_methods_for_type(const Type* type,
1119                                 const Method::Field_indexes*,
1120                                 unsigned int depth, bool, bool, Types_seen*,
1121                                 Methods**);
1122
1123   static void
1124   add_interface_methods_for_type(const Type* type,
1125                                  const Method::Field_indexes*,
1126                                  unsigned int depth, Methods**);
1127
1128   // Build stub methods for a type.
1129   static void
1130   build_stub_methods(Gogo*, const Type* type, const Methods* methods,
1131                      Location);
1132
1133   static void
1134   build_one_stub_method(Gogo*, Method*, const char* receiver_name,
1135                         const Typed_identifier_list*, bool is_varargs,
1136                         Location);
1137
1138   static Expression*
1139   apply_field_indexes(Expression*, const Method::Field_indexes*,
1140                       Location);
1141
1142   // Look for a field or method named NAME in TYPE.
1143   static bool
1144   find_field_or_method(const Type* type, const std::string& name,
1145                        bool receiver_can_be_pointer,
1146                        std::vector<const Named_type*>*, int* level,
1147                        bool* is_method, bool* found_pointer_method,
1148                        std::string* ambig1, std::string* ambig2);
1149
1150   // Get the backend representation for a type without looking in the
1151   // hash table for identical types.
1152   Btype*
1153   get_btype_without_hash(Gogo*);
1154
1155   // A mapping from Type to Btype*, used to ensure that the backend
1156   // representation of identical types is identical.
1157   typedef Unordered_map_hash(const Type*, Btype*, Type_hash_identical,
1158                              Type_identical) Type_btypes;
1159
1160   static Type_btypes type_btypes;
1161
1162   // A list of builtin named types.
1163   static std::vector<Named_type*> named_builtin_types;
1164
1165   // A map from types which need specific type functions to the type
1166   // functions themselves.
1167   typedef std::pair<Named_object*, Named_object*> Hash_equal_fn;
1168   typedef Unordered_map_hash(const Type*, Hash_equal_fn, Type_hash_identical,
1169                              Type_identical) Type_functions;
1170
1171   static Type_functions type_functions_table;
1172
1173   // The type classification.
1174   Type_classification classification_;
1175   // The backend representation of the type, once it has been
1176   // determined.
1177   Btype* btype_;
1178   // The type descriptor for this type.  This starts out as NULL and
1179   // is filled in as needed.
1180   Bvariable* type_descriptor_var_;
1181 };
1182
1183 // Type hash table operations.
1184
1185 class Type_hash_identical
1186 {
1187  public:
1188   unsigned int
1189   operator()(const Type* type) const
1190   { return type->hash_for_method(NULL); }
1191 };
1192
1193 class Type_identical
1194 {
1195  public:
1196   bool
1197   operator()(const Type* t1, const Type* t2) const
1198   { return Type::are_identical(t1, t2, false, NULL); }
1199 };
1200
1201 // An identifier with a type.
1202
1203 class Typed_identifier
1204 {
1205  public:
1206   Typed_identifier(const std::string& name, Type* type,
1207                    Location location)
1208     : name_(name), type_(type), location_(location)
1209   { }
1210
1211   // Get the name.
1212   const std::string&
1213   name() const
1214   { return this->name_; }
1215
1216   // Get the type.
1217   Type*
1218   type() const
1219   { return this->type_; }
1220
1221   // Return the location where the name was seen.  This is not always
1222   // meaningful.
1223   Location
1224   location() const
1225   { return this->location_; }
1226
1227   // Set the type--sometimes we see the identifier before the type.
1228   void
1229   set_type(Type* type)
1230   {
1231     go_assert(this->type_ == NULL || type->is_error_type());
1232     this->type_ = type;
1233   }
1234
1235  private:
1236   // Identifier name.
1237   std::string name_;
1238   // Type.
1239   Type* type_;
1240   // The location where the name was seen.
1241   Location location_;
1242 };
1243
1244 // A list of Typed_identifiers.
1245
1246 class Typed_identifier_list
1247 {
1248  public:
1249   Typed_identifier_list()
1250     : entries_()
1251   { }
1252
1253   // Whether the list is empty.
1254   bool
1255   empty() const
1256   { return this->entries_.empty(); }
1257
1258   // Return the number of entries in the list.
1259   size_t
1260   size() const
1261   { return this->entries_.size(); }
1262
1263   // Add an entry to the end of the list.
1264   void
1265   push_back(const Typed_identifier& td)
1266   { this->entries_.push_back(td); }
1267
1268   // Remove an entry from the end of the list.
1269   void
1270   pop_back()
1271   { this->entries_.pop_back(); }
1272
1273   // Set the type of entry I to TYPE.
1274   void
1275   set_type(size_t i, Type* type)
1276   {
1277     go_assert(i < this->entries_.size());
1278     this->entries_[i].set_type(type);
1279   }
1280
1281   // Sort the entries by name.
1282   void
1283   sort_by_name();
1284
1285   // Traverse types.
1286   int
1287   traverse(Traverse*);
1288
1289   // Return the first and last elements.
1290   Typed_identifier&
1291   front()
1292   { return this->entries_.front(); }
1293
1294   const Typed_identifier&
1295   front() const
1296   { return this->entries_.front(); }
1297
1298   Typed_identifier&
1299   back()
1300   { return this->entries_.back(); }
1301
1302   const Typed_identifier&
1303   back() const
1304   { return this->entries_.back(); }
1305
1306   const Typed_identifier&
1307   at(size_t i) const
1308   { return this->entries_.at(i); }
1309
1310   void
1311   set(size_t i, const Typed_identifier& t)
1312   { this->entries_.at(i) = t; }
1313
1314   void
1315   resize(size_t c)
1316   {
1317     go_assert(c <= this->entries_.size());
1318     this->entries_.resize(c, Typed_identifier("", NULL,
1319                                               Linemap::unknown_location()));
1320   }
1321
1322   // Iterators.
1323
1324   typedef std::vector<Typed_identifier>::iterator iterator;
1325   typedef std::vector<Typed_identifier>::const_iterator const_iterator;
1326
1327   iterator
1328   begin()
1329   { return this->entries_.begin(); }
1330
1331   const_iterator
1332   begin() const
1333   { return this->entries_.begin(); }
1334
1335   iterator
1336   end()
1337   { return this->entries_.end(); }
1338
1339   const_iterator
1340   end() const
1341   { return this->entries_.end(); }
1342
1343   // Return a copy of this list.  This returns an independent copy of
1344   // the vector, but does not copy the types.
1345   Typed_identifier_list*
1346   copy() const;
1347
1348  private:
1349   std::vector<Typed_identifier> entries_;
1350 };
1351
1352 // The type of an integer.
1353
1354 class Integer_type : public Type
1355 {
1356  public:
1357   // Create a new integer type.
1358   static Named_type*
1359   create_integer_type(const char* name, bool is_unsigned, int bits,
1360                       int runtime_type_kind);
1361
1362   // Look up an existing integer type.
1363   static Named_type*
1364   lookup_integer_type(const char* name);
1365
1366   // Create an abstract integer type.
1367   static Integer_type*
1368   create_abstract_integer_type();
1369
1370   // Whether this is an abstract integer type.
1371   bool
1372   is_abstract() const
1373   { return this->is_abstract_; }
1374
1375   // Whether this is an unsigned type.
1376   bool
1377   is_unsigned() const
1378   { return this->is_unsigned_; }
1379
1380   // The number of bits.
1381   int
1382   bits() const
1383   { return this->bits_; }
1384
1385   // Whether this type is the same as T.
1386   bool
1387   is_identical(const Integer_type* t) const;
1388
1389   // Whether this is the type "byte" or another name for "byte".
1390   bool
1391   is_byte() const
1392   { return this->is_byte_; }
1393
1394   // Mark this as the "byte" type.
1395   void
1396   set_is_byte()
1397   { this->is_byte_ = true; }
1398
1399   // Whether this is the type "rune" or another name for "rune".
1400   bool
1401   is_rune() const
1402   { return this->is_rune_; }
1403
1404   // Mark this as the "rune" type.
1405   void
1406   set_is_rune()
1407   { this->is_rune_ = true; }
1408
1409 protected:
1410   bool
1411   do_compare_is_identity(Gogo*) const
1412   { return true; }
1413
1414   unsigned int
1415   do_hash_for_method(Gogo*) const;
1416
1417   Btype*
1418   do_get_backend(Gogo*);
1419
1420   Expression*
1421   do_type_descriptor(Gogo*, Named_type*);
1422
1423   void
1424   do_reflection(Gogo*, std::string*) const;
1425
1426   void
1427   do_mangled_name(Gogo*, std::string*) const;
1428
1429  private:
1430   Integer_type(bool is_abstract, bool is_unsigned, int bits,
1431                int runtime_type_kind)
1432     : Type(TYPE_INTEGER),
1433       is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
1434       is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
1435   { }
1436
1437   // Map names of integer types to the types themselves.
1438   typedef std::map<std::string, Named_type*> Named_integer_types;
1439   static Named_integer_types named_integer_types;
1440
1441   // True if this is an abstract type.
1442   bool is_abstract_;
1443   // True if this is an unsigned type.
1444   bool is_unsigned_;
1445   // True if this is the byte type.
1446   bool is_byte_;
1447   // True if this is the rune type.
1448   bool is_rune_;
1449   // The number of bits.
1450   int bits_;
1451   // The runtime type code used in the type descriptor for this type.
1452   int runtime_type_kind_;
1453 };
1454
1455 // The type of a floating point number.
1456
1457 class Float_type : public Type
1458 {
1459  public:
1460   // Create a new float type.
1461   static Named_type*
1462   create_float_type(const char* name, int bits, int runtime_type_kind);
1463
1464   // Look up an existing float type.
1465   static Named_type*
1466   lookup_float_type(const char* name);
1467
1468   // Create an abstract float type.
1469   static Float_type*
1470   create_abstract_float_type();
1471
1472   // Whether this is an abstract float type.
1473   bool
1474   is_abstract() const
1475   { return this->is_abstract_; }
1476
1477   // The number of bits.
1478   int
1479   bits() const
1480   { return this->bits_; }
1481
1482   // Whether this type is the same as T.
1483   bool
1484   is_identical(const Float_type* t) const;
1485
1486  protected:
1487   bool
1488   do_compare_is_identity(Gogo*) const
1489   { return false; }
1490
1491   unsigned int
1492   do_hash_for_method(Gogo*) const;
1493
1494   Btype*
1495   do_get_backend(Gogo*);
1496
1497   Expression*
1498   do_type_descriptor(Gogo*, Named_type*);
1499
1500   void
1501   do_reflection(Gogo*, std::string*) const;
1502
1503   void
1504   do_mangled_name(Gogo*, std::string*) const;
1505
1506  private:
1507   Float_type(bool is_abstract, int bits, int runtime_type_kind)
1508     : Type(TYPE_FLOAT),
1509       is_abstract_(is_abstract), bits_(bits),
1510       runtime_type_kind_(runtime_type_kind)
1511   { }
1512
1513   // Map names of float types to the types themselves.
1514   typedef std::map<std::string, Named_type*> Named_float_types;
1515   static Named_float_types named_float_types;
1516
1517   // True if this is an abstract type.
1518   bool is_abstract_;
1519   // The number of bits in the floating point value.
1520   int bits_;
1521   // The runtime type code used in the type descriptor for this type.
1522   int runtime_type_kind_;
1523 };
1524
1525 // The type of a complex number.
1526
1527 class Complex_type : public Type
1528 {
1529  public:
1530   // Create a new complex type.
1531   static Named_type*
1532   create_complex_type(const char* name, int bits, int runtime_type_kind);
1533
1534   // Look up an existing complex type.
1535   static Named_type*
1536   lookup_complex_type(const char* name);
1537
1538   // Create an abstract complex type.
1539   static Complex_type*
1540   create_abstract_complex_type();
1541
1542   // Whether this is an abstract complex type.
1543   bool
1544   is_abstract() const
1545   { return this->is_abstract_; }
1546
1547   // The number of bits: 64 or 128.
1548   int bits() const
1549   { return this->bits_; }
1550
1551   // Whether this type is the same as T.
1552   bool
1553   is_identical(const Complex_type* t) const;
1554
1555  protected:
1556   bool
1557   do_compare_is_identity(Gogo*) const
1558   { return false; }
1559
1560   unsigned int
1561   do_hash_for_method(Gogo*) const;
1562
1563   Btype*
1564   do_get_backend(Gogo*);
1565
1566   Expression*
1567   do_type_descriptor(Gogo*, Named_type*);
1568
1569   void
1570   do_reflection(Gogo*, std::string*) const;
1571
1572   void
1573   do_mangled_name(Gogo*, std::string*) const;
1574
1575  private:
1576   Complex_type(bool is_abstract, int bits, int runtime_type_kind)
1577     : Type(TYPE_COMPLEX),
1578       is_abstract_(is_abstract), bits_(bits),
1579       runtime_type_kind_(runtime_type_kind)
1580   { }
1581
1582   // Map names of complex types to the types themselves.
1583   typedef std::map<std::string, Named_type*> Named_complex_types;
1584   static Named_complex_types named_complex_types;
1585
1586   // True if this is an abstract type.
1587   bool is_abstract_;
1588   // The number of bits in the complex value--64 or 128.
1589   int bits_;
1590   // The runtime type code used in the type descriptor for this type.
1591   int runtime_type_kind_;
1592 };
1593
1594 // The type of a string.
1595
1596 class String_type : public Type
1597 {
1598  public:
1599   String_type()
1600     : Type(TYPE_STRING)
1601   { }
1602
1603   // Return a tree for the length of STRING.
1604   static tree
1605   length_tree(Gogo*, tree string);
1606
1607   // Return a tree which points to the bytes of STRING.
1608   static tree
1609   bytes_tree(Gogo*, tree string);
1610
1611  protected:
1612   bool
1613   do_has_pointer() const
1614   { return true; }
1615
1616   bool
1617   do_compare_is_identity(Gogo*) const
1618   { return false; }
1619
1620   Btype*
1621   do_get_backend(Gogo*);
1622
1623   Expression*
1624   do_type_descriptor(Gogo*, Named_type*);
1625
1626   void
1627   do_reflection(Gogo*, std::string*) const;
1628
1629   void
1630   do_mangled_name(Gogo*, std::string* ret) const;
1631
1632  private:
1633   // The named string type.
1634   static Named_type* string_type_;
1635 };
1636
1637 // The type of a function.
1638
1639 class Function_type : public Type
1640 {
1641  public:
1642   Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
1643                 Typed_identifier_list* results, Location location)
1644     : Type(TYPE_FUNCTION),
1645       receiver_(receiver), parameters_(parameters), results_(results),
1646       location_(location), is_varargs_(false), is_builtin_(false)
1647   { }
1648
1649   // Get the receiver.
1650   const Typed_identifier*
1651   receiver() const
1652   { return this->receiver_; }
1653
1654   // Get the return names and types.
1655   const Typed_identifier_list*
1656   results() const
1657   { return this->results_; }
1658
1659   // Get the parameter names and types.
1660   const Typed_identifier_list*
1661   parameters() const
1662   { return this->parameters_; }
1663
1664   // Whether this is a varargs function.
1665   bool
1666   is_varargs() const
1667   { return this->is_varargs_; }
1668
1669   // Whether this is a builtin function.
1670   bool
1671   is_builtin() const
1672   { return this->is_builtin_; }
1673
1674   // The location where this type was defined.
1675   Location
1676   location() const
1677   { return this->location_; }
1678
1679   // Return whether this is a method type.
1680   bool
1681   is_method() const
1682   { return this->receiver_ != NULL; }
1683
1684   // Whether T is a valid redeclaration of this type.  This is called
1685   // when a function is declared more than once.
1686   bool
1687   is_valid_redeclaration(const Function_type* t, std::string*) const;
1688
1689   // Whether this type is the same as T.
1690   bool
1691   is_identical(const Function_type* t, bool ignore_receiver,
1692                bool errors_are_identical, std::string*) const;
1693
1694   // Record that this is a varargs function.
1695   void
1696   set_is_varargs()
1697   { this->is_varargs_ = true; }
1698
1699   // Record that this is a builtin function.
1700   void
1701   set_is_builtin()
1702   { this->is_builtin_ = true; }
1703
1704   // Import a function type.
1705   static Function_type*
1706   do_import(Import*);
1707
1708   // Return a copy of this type without a receiver.  This is only
1709   // valid for a method type.
1710   Function_type*
1711   copy_without_receiver() const;
1712
1713   // Return a copy of this type with a receiver.  This is used when an
1714   // interface method is attached to a named or struct type.
1715   Function_type*
1716   copy_with_receiver(Type*) const;
1717
1718   // Finishing converting function types.
1719   static void
1720   convert_types(Gogo*);
1721
1722   static Type*
1723   make_function_type_descriptor_type();
1724
1725  protected:
1726   int
1727   do_traverse(Traverse*);
1728
1729   // A trampoline function has a pointer which matters for GC.
1730   bool
1731   do_has_pointer() const
1732   { return true; }
1733
1734   bool
1735   do_compare_is_identity(Gogo*) const
1736   { return false; }
1737
1738   unsigned int
1739   do_hash_for_method(Gogo*) const;
1740
1741   Btype*
1742   do_get_backend(Gogo*);
1743
1744   Expression*
1745   do_type_descriptor(Gogo*, Named_type*);
1746
1747   void
1748   do_reflection(Gogo*, std::string*) const;
1749
1750   void
1751   do_mangled_name(Gogo*, std::string*) const;
1752
1753   void
1754   do_export(Export*) const;
1755
1756  private:
1757   Expression*
1758   type_descriptor_params(Type*, const Typed_identifier*,
1759                          const Typed_identifier_list*);
1760
1761   Btype*
1762   get_function_backend(Gogo*);
1763
1764   // A list of function types with multiple results and their
1765   // placeholder backend representations, used to postpone building
1766   // the structs we use for multiple results until all types are
1767   // converted.
1768   typedef std::vector<std::pair<Function_type*, Btype*> > Placeholders;
1769   static Placeholders placeholders;
1770
1771   // The receiver name and type.  This will be NULL for a normal
1772   // function, non-NULL for a method.
1773   Typed_identifier* receiver_;
1774   // The parameter names and types.
1775   Typed_identifier_list* parameters_;
1776   // The result names and types.  This will be NULL if no result was
1777   // specified.
1778   Typed_identifier_list* results_;
1779   // The location where this type was defined.  This exists solely to
1780   // give a location for the fields of the struct if this function
1781   // returns multiple values.
1782   Location location_;
1783   // Whether this function takes a variable number of arguments.
1784   bool is_varargs_;
1785   // Whether this is a special builtin function which can not simply
1786   // be called.  This is used for len, cap, etc.
1787   bool is_builtin_;
1788 };
1789
1790 // The type of a pointer.
1791
1792 class Pointer_type : public Type
1793 {
1794  public:
1795   Pointer_type(Type* to_type)
1796     : Type(TYPE_POINTER),
1797       to_type_(to_type)
1798   {}
1799
1800   Type*
1801   points_to() const
1802   { return this->to_type_; }
1803
1804   // Import a pointer type.
1805   static Pointer_type*
1806   do_import(Import*);
1807
1808   static Type*
1809   make_pointer_type_descriptor_type();
1810
1811  protected:
1812   int
1813   do_traverse(Traverse*);
1814
1815   bool
1816   do_has_pointer() const
1817   { return true; }
1818
1819   bool
1820   do_compare_is_identity(Gogo*) const
1821   { return true; }
1822
1823   unsigned int
1824   do_hash_for_method(Gogo*) const;
1825
1826   Btype*
1827   do_get_backend(Gogo*);
1828
1829   Expression*
1830   do_type_descriptor(Gogo*, Named_type*);
1831
1832   void
1833   do_reflection(Gogo*, std::string*) const;
1834
1835   void
1836   do_mangled_name(Gogo*, std::string*) const;
1837
1838   void
1839   do_export(Export*) const;
1840
1841  private:
1842   // The type to which this type points.
1843   Type* to_type_;
1844 };
1845
1846 // The type of a field in a struct.
1847
1848 class Struct_field
1849 {
1850  public:
1851   explicit Struct_field(const Typed_identifier& typed_identifier)
1852     : typed_identifier_(typed_identifier), tag_(NULL)
1853   { }
1854
1855   // The field name.
1856   const std::string&
1857   field_name() const;
1858
1859   // Return whether this struct field is named NAME.
1860   bool
1861   is_field_name(const std::string& name) const;
1862
1863   // The field type.
1864   Type*
1865   type() const
1866   { return this->typed_identifier_.type(); }
1867
1868   // The field location.
1869   Location
1870   location() const
1871   { return this->typed_identifier_.location(); }
1872
1873   // Whether the field has a tag.
1874   bool
1875   has_tag() const
1876   { return this->tag_ != NULL; }
1877
1878   // The tag.
1879   const std::string&
1880   tag() const
1881   {
1882     go_assert(this->tag_ != NULL);
1883     return *this->tag_;
1884   }
1885
1886   // Whether this is an anonymous field.
1887   bool
1888   is_anonymous() const
1889   { return this->typed_identifier_.name().empty(); }
1890
1891   // Set the tag.  FIXME: This is never freed.
1892   void
1893   set_tag(const std::string& tag)
1894   { this->tag_ = new std::string(tag); }
1895
1896   // Set the type.  This is only used in error cases.
1897   void
1898   set_type(Type* type)
1899   { this->typed_identifier_.set_type(type); }
1900
1901  private:
1902   // The field name, type, and location.
1903   Typed_identifier typed_identifier_;
1904   // The field tag.  This is NULL if the field has no tag.
1905   std::string* tag_;
1906 };
1907
1908 // A list of struct fields.
1909
1910 class Struct_field_list
1911 {
1912  public:
1913   Struct_field_list()
1914     : entries_()
1915   { }
1916
1917   // Whether the list is empty.
1918   bool
1919   empty() const
1920   { return this->entries_.empty(); }
1921
1922   // Return the number of entries.
1923   size_t
1924   size() const
1925   { return this->entries_.size(); }
1926
1927   // Add an entry to the end of the list.
1928   void
1929   push_back(const Struct_field& sf)
1930   { this->entries_.push_back(sf); }
1931
1932   // Index into the list.
1933   const Struct_field&
1934   at(size_t i) const
1935   { return this->entries_.at(i); }
1936
1937   // Last entry in list.
1938   Struct_field&
1939   back()
1940   { return this->entries_.back(); }
1941
1942   // Iterators.
1943
1944   typedef std::vector<Struct_field>::iterator iterator;
1945   typedef std::vector<Struct_field>::const_iterator const_iterator;
1946
1947   iterator
1948   begin()
1949   { return this->entries_.begin(); }
1950
1951   const_iterator
1952   begin() const
1953   { return this->entries_.begin(); }
1954
1955   iterator
1956   end()
1957   { return this->entries_.end(); }
1958
1959   const_iterator
1960   end() const
1961   { return this->entries_.end(); }
1962
1963  private:
1964   std::vector<Struct_field> entries_;
1965 };
1966
1967 // The type of a struct.
1968
1969 class Struct_type : public Type
1970 {
1971  public:
1972   Struct_type(Struct_field_list* fields, Location location)
1973     : Type(TYPE_STRUCT),
1974       fields_(fields), location_(location), all_methods_(NULL)
1975   { }
1976
1977   // Return the field NAME.  This only looks at local fields, not at
1978   // embedded types.  If the field is found, and PINDEX is not NULL,
1979   // this sets *PINDEX to the field index.  If the field is not found,
1980   // this returns NULL.
1981   const Struct_field*
1982   find_local_field(const std::string& name, unsigned int *pindex) const;
1983
1984   // Return the field number INDEX.
1985   const Struct_field*
1986   field(unsigned int index) const
1987   { return &this->fields_->at(index); }
1988
1989   // Get the struct fields.
1990   const Struct_field_list*
1991   fields() const
1992   { return this->fields_; }
1993
1994   // Return the number of fields.
1995   size_t
1996   field_count() const
1997   { return this->fields_->size(); }
1998
1999   // Push a new field onto the end of the struct.  This is used when
2000   // building a closure variable.
2001   void
2002   push_field(const Struct_field& sf)
2003   { this->fields_->push_back(sf); }
2004
2005   // Return an expression referring to field NAME in STRUCT_EXPR, or
2006   // NULL if there is no field with that name.
2007   Field_reference_expression*
2008   field_reference(Expression* struct_expr, const std::string& name,
2009                   Location) const;
2010
2011   // Return the total number of fields, including embedded fields.
2012   // This is the number of values that can appear in a conversion to
2013   // this type.
2014   unsigned int
2015   total_field_count() const;
2016
2017   // Whether this type is identical with T.
2018   bool
2019   is_identical(const Struct_type* t, bool errors_are_identical) const;
2020
2021   // Whether this struct type has any hidden fields.  This returns
2022   // true if any fields have hidden names, or if any non-pointer
2023   // anonymous fields have types with hidden fields.
2024   bool
2025   struct_has_hidden_fields(const Named_type* within, std::string*) const;
2026
2027   // Return whether NAME is a local field which is not exported.  This
2028   // is only used for better error reporting.
2029   bool
2030   is_unexported_local_field(Gogo*, const std::string& name) const;
2031
2032   // If this is an unnamed struct, build the complete list of methods,
2033   // including those from anonymous fields, and build methods stubs if
2034   // needed.
2035   void
2036   finalize_methods(Gogo*);
2037
2038   // Return whether this type has any methods.  This should only be
2039   // called after the finalize_methods pass.
2040   bool
2041   has_any_methods() const
2042   { return this->all_methods_ != NULL; }
2043
2044   // Return the methods for tihs type.  This should only be called
2045   // after the finalize_methods pass.
2046   const Methods*
2047   methods() const
2048   { return this->all_methods_; }
2049
2050   // Return the method to use for NAME.  This returns NULL if there is
2051   // no such method or if the method is ambiguous.  When it returns
2052   // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2053   Method*
2054   method_function(const std::string& name, bool* is_ambiguous) const;
2055
2056   // Traverse just the field types of a struct type.
2057   int
2058   traverse_field_types(Traverse* traverse)
2059   { return this->do_traverse(traverse); }
2060
2061   // If the offset of field INDEX in the backend implementation can be
2062   // determined, set *POFFSET to the offset in bytes and return true.
2063   // Otherwise, return false.
2064   bool
2065   backend_field_offset(Gogo*, unsigned int index, unsigned int* poffset);
2066
2067   // Import a struct type.
2068   static Struct_type*
2069   do_import(Import*);
2070
2071   static Type*
2072   make_struct_type_descriptor_type();
2073
2074   // Write the hash function for this type.
2075   void
2076   write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2077
2078   // Write the equality function for this type.
2079   void
2080   write_equal_function(Gogo*, Named_type*);
2081
2082  protected:
2083   int
2084   do_traverse(Traverse*);
2085
2086   bool
2087   do_verify();
2088
2089   bool
2090   do_has_pointer() const;
2091
2092   bool
2093   do_compare_is_identity(Gogo*) const;
2094
2095   unsigned int
2096   do_hash_for_method(Gogo*) const;
2097
2098   Btype*
2099   do_get_backend(Gogo*);
2100
2101   Expression*
2102   do_type_descriptor(Gogo*, Named_type*);
2103
2104   void
2105   do_reflection(Gogo*, std::string*) const;
2106
2107   void
2108   do_mangled_name(Gogo*, std::string*) const;
2109
2110   void
2111   do_export(Export*) const;
2112
2113  private:
2114   // Used to avoid infinite loops in field_reference_depth.
2115   struct Saw_named_type
2116   {
2117     Saw_named_type* next;
2118     Named_type* nt;
2119   };
2120
2121   Field_reference_expression*
2122   field_reference_depth(Expression* struct_expr, const std::string& name,
2123                         Location, Saw_named_type*,
2124                         unsigned int* depth) const;
2125
2126   // The fields of the struct.
2127   Struct_field_list* fields_;
2128   // The place where the struct was declared.
2129   Location location_;
2130   // If this struct is unnamed, a list of methods.
2131   Methods* all_methods_;
2132 };
2133
2134 // The type of an array.
2135
2136 class Array_type : public Type
2137 {
2138  public:
2139   Array_type(Type* element_type, Expression* length)
2140     : Type(TYPE_ARRAY),
2141       element_type_(element_type), length_(length), length_tree_(NULL)
2142   { }
2143
2144   // Return the element type.
2145   Type*
2146   element_type() const
2147   { return this->element_type_; }
2148
2149   // Return the length.  This will return NULL for an open array.
2150   Expression*
2151   length() const
2152   { return this->length_; }
2153
2154   // Whether this type is identical with T.
2155   bool
2156   is_identical(const Array_type* t, bool errors_are_identical) const;
2157
2158   // Whether this type has any hidden fields.
2159   bool
2160   array_has_hidden_fields(const Named_type* within, std::string* reason) const
2161   { return this->element_type_->has_hidden_fields(within, reason); }
2162
2163   // Return a tree for the pointer to the values in an array.
2164   tree
2165   value_pointer_tree(Gogo*, tree array) const;
2166
2167   // Return a tree for the length of an array with this type.
2168   tree
2169   length_tree(Gogo*, tree array);
2170
2171   // Return a tree for the capacity of an array with this type.
2172   tree
2173   capacity_tree(Gogo*, tree array);
2174
2175   // Import an array type.
2176   static Array_type*
2177   do_import(Import*);
2178
2179   // Return the backend representation of the element type.
2180   Btype*
2181   get_backend_element(Gogo*);
2182
2183   // Return the backend representation of the length.
2184   Bexpression*
2185   get_backend_length(Gogo*);
2186
2187   static Type*
2188   make_array_type_descriptor_type();
2189
2190   static Type*
2191   make_slice_type_descriptor_type();
2192
2193   // Write the hash function for this type.
2194   void
2195   write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2196
2197   // Write the equality function for this type.
2198   void
2199   write_equal_function(Gogo*, Named_type*);
2200
2201  protected:
2202   int
2203   do_traverse(Traverse* traverse);
2204
2205   bool
2206   do_verify();
2207
2208   bool
2209   do_has_pointer() const
2210   {
2211     return this->length_ == NULL || this->element_type_->has_pointer();
2212   }
2213
2214   bool
2215   do_compare_is_identity(Gogo*) const;
2216
2217   unsigned int
2218   do_hash_for_method(Gogo*) const;
2219
2220   Btype*
2221   do_get_backend(Gogo*);
2222
2223   Expression*
2224   do_type_descriptor(Gogo*, Named_type*);
2225
2226   void
2227   do_reflection(Gogo*, std::string*) const;
2228
2229   void
2230   do_mangled_name(Gogo*, std::string*) const;
2231
2232   void
2233   do_export(Export*) const;
2234
2235  private:
2236   bool
2237   verify_length();
2238
2239   tree
2240   get_length_tree(Gogo*);
2241
2242   Expression*
2243   array_type_descriptor(Gogo*, Named_type*);
2244
2245   Expression*
2246   slice_type_descriptor(Gogo*, Named_type*);
2247
2248   // The type of elements of the array.
2249   Type* element_type_;
2250   // The number of elements.  This may be NULL.
2251   Expression* length_;
2252   // The length as a tree.  We only want to compute this once.
2253   tree length_tree_;
2254 };
2255
2256 // The type of a map.
2257
2258 class Map_type : public Type
2259 {
2260  public:
2261   Map_type(Type* key_type, Type* val_type, Location location)
2262     : Type(TYPE_MAP),
2263       key_type_(key_type), val_type_(val_type), location_(location)
2264   { }
2265
2266   // Return the key type.
2267   Type*
2268   key_type() const
2269   { return this->key_type_; }
2270
2271   // Return the value type.
2272   Type*
2273   val_type() const
2274   { return this->val_type_; }
2275
2276   // Whether this type is identical with T.
2277   bool
2278   is_identical(const Map_type* t, bool errors_are_identical) const;
2279
2280   // Import a map type.
2281   static Map_type*
2282   do_import(Import*);
2283
2284   static Type*
2285   make_map_type_descriptor_type();
2286
2287   static Type*
2288   make_map_descriptor_type();
2289
2290   // Build a map descriptor for this type.  Return a pointer to it.
2291   // The location is the location which causes us to need the
2292   // descriptor.
2293   tree
2294   map_descriptor_pointer(Gogo* gogo, Location);
2295
2296  protected:
2297   int
2298   do_traverse(Traverse*);
2299
2300   bool
2301   do_verify();
2302
2303   bool
2304   do_has_pointer() const
2305   { return true; }
2306
2307   bool
2308   do_compare_is_identity(Gogo*) const
2309   { return false; }
2310
2311   unsigned int
2312   do_hash_for_method(Gogo*) const;
2313
2314   Btype*
2315   do_get_backend(Gogo*);
2316
2317   Expression*
2318   do_type_descriptor(Gogo*, Named_type*);
2319
2320   void
2321   do_reflection(Gogo*, std::string*) const;
2322
2323   void
2324   do_mangled_name(Gogo*, std::string*) const;
2325
2326   void
2327   do_export(Export*) const;
2328
2329  private:
2330   // Mapping from map types to map descriptors.
2331   typedef Unordered_map_hash(const Map_type*, Bvariable*, Type_hash_identical,
2332                              Type_identical) Map_descriptors;
2333   static Map_descriptors map_descriptors;
2334
2335   Bvariable*
2336   map_descriptor(Gogo*);
2337
2338   // The key type.
2339   Type* key_type_;
2340   // The value type.
2341   Type* val_type_;
2342   // Where the type was defined.
2343   Location location_;
2344 };
2345
2346 // The type of a channel.
2347
2348 class Channel_type : public Type
2349 {
2350  public:
2351   Channel_type(bool may_send, bool may_receive, Type* element_type)
2352     : Type(TYPE_CHANNEL),
2353       may_send_(may_send), may_receive_(may_receive),
2354       element_type_(element_type)
2355   { go_assert(may_send || may_receive); }
2356
2357   // Whether this channel can send data.
2358   bool
2359   may_send() const
2360   { return this->may_send_; }
2361
2362   // Whether this channel can receive data.
2363   bool
2364   may_receive() const
2365   { return this->may_receive_; }
2366
2367   // The type of the values that may be sent on this channel.  This is
2368   // NULL if any type may be sent.
2369   Type*
2370   element_type() const
2371   { return this->element_type_; }
2372
2373   // Whether this type is identical with T.
2374   bool
2375   is_identical(const Channel_type* t, bool errors_are_identical) const;
2376
2377   // Import a channel type.
2378   static Channel_type*
2379   do_import(Import*);
2380
2381   static Type*
2382   make_chan_type_descriptor_type();
2383
2384  protected:
2385   int
2386   do_traverse(Traverse* traverse)
2387   { return Type::traverse(this->element_type_, traverse); }
2388
2389   bool
2390   do_has_pointer() const
2391   { return true; }
2392
2393   bool
2394   do_compare_is_identity(Gogo*) const
2395   { return true; }
2396
2397   unsigned int
2398   do_hash_for_method(Gogo*) const;
2399
2400   Btype*
2401   do_get_backend(Gogo*);
2402
2403   Expression*
2404   do_type_descriptor(Gogo*, Named_type*);
2405
2406   void
2407   do_reflection(Gogo*, std::string*) const;
2408
2409   void
2410   do_mangled_name(Gogo*, std::string*) const;
2411
2412   void
2413   do_export(Export*) const;
2414
2415  private:
2416   // Whether this channel can send data.
2417   bool may_send_;
2418   // Whether this channel can receive data.
2419   bool may_receive_;
2420   // The types of elements which may be sent on this channel.  If this
2421   // is NULL, it means that any type may be sent.
2422   Type* element_type_;
2423 };
2424
2425 // An interface type.
2426
2427 class Interface_type : public Type
2428 {
2429  public:
2430   Interface_type(Typed_identifier_list* methods, Location location)
2431     : Type(TYPE_INTERFACE),
2432       methods_(methods), location_(location)
2433   { go_assert(methods == NULL || !methods->empty()); }
2434
2435   // The location where the interface type was defined.
2436   Location
2437   location() const
2438   { return this->location_; }
2439
2440   // Return whether this is an empty interface.
2441   bool
2442   is_empty() const
2443   { return this->methods_ == NULL; }
2444
2445   // Return the list of methods.  This will return NULL for an empty
2446   // interface.
2447   const Typed_identifier_list*
2448   methods() const
2449   { return this->methods_; }
2450
2451   // Return the number of methods.
2452   size_t
2453   method_count() const
2454   { return this->methods_ == NULL ? 0 : this->methods_->size(); }
2455
2456   // Return the method NAME, or NULL.
2457   const Typed_identifier*
2458   find_method(const std::string& name) const;
2459
2460   // Return the zero-based index of method NAME.
2461   size_t
2462   method_index(const std::string& name) const;
2463
2464   // Finalize the methods.  This handles interface inheritance.
2465   void
2466   finalize_methods();
2467
2468   // Return true if T implements this interface.  If this returns
2469   // false, and REASON is not NULL, it sets *REASON to the reason that
2470   // it fails.
2471   bool
2472   implements_interface(const Type* t, std::string* reason) const;
2473
2474   // Whether this type is identical with T.  REASON is as in
2475   // implements_interface.
2476   bool
2477   is_identical(const Interface_type* t, bool errors_are_identical) const;
2478
2479   // Whether we can assign T to this type.  is_identical is known to
2480   // be false.
2481   bool
2482   is_compatible_for_assign(const Interface_type*, std::string* reason) const;
2483
2484   // Return whether NAME is a method which is not exported.  This is
2485   // only used for better error reporting.
2486   bool
2487   is_unexported_method(Gogo*, const std::string& name) const;
2488
2489   // Import an interface type.
2490   static Interface_type*
2491   do_import(Import*);
2492
2493   // Make a struct for an empty interface type.
2494   static Btype*
2495   get_backend_empty_interface_type(Gogo*);
2496
2497   static Type*
2498   make_interface_type_descriptor_type();
2499
2500  protected:
2501   int
2502   do_traverse(Traverse*);
2503
2504   bool
2505   do_has_pointer() const
2506   { return true; }
2507
2508   bool
2509   do_compare_is_identity(Gogo*) const
2510   { return false; }
2511
2512   unsigned int
2513   do_hash_for_method(Gogo*) const;
2514
2515   Btype*
2516   do_get_backend(Gogo*);
2517
2518   Expression*
2519   do_type_descriptor(Gogo*, Named_type*);
2520
2521   void
2522   do_reflection(Gogo*, std::string*) const;
2523
2524   void
2525   do_mangled_name(Gogo*, std::string*) const;
2526
2527   void
2528   do_export(Export*) const;
2529
2530  private:
2531   // The list of methods associated with the interface.  This will be
2532   // NULL for the empty interface.
2533   Typed_identifier_list* methods_;
2534   // The location where the interface was defined.
2535   Location location_;
2536 };
2537
2538 // The value we keep for a named type.  This lets us get the right
2539 // name when we convert to trees.  Note that we don't actually keep
2540 // the name here; the name is in the Named_object which points to
2541 // this.  This object exists to hold a unique tree which represents
2542 // the type.
2543
2544 class Named_type : public Type
2545 {
2546  public:
2547   Named_type(Named_object* named_object, Type* type, Location location)
2548     : Type(TYPE_NAMED),
2549       named_object_(named_object), in_function_(NULL), type_(type),
2550       local_methods_(NULL), all_methods_(NULL),
2551       interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
2552       location_(location), named_btype_(NULL), dependencies_(),
2553       is_visible_(true), is_error_(false), is_placeholder_(false),
2554       is_converted_(false), is_circular_(false), seen_(false),
2555       seen_in_compare_is_identity_(false), seen_in_get_backend_(false)
2556   { }
2557
2558   // Return the associated Named_object.  This holds the actual name.
2559   Named_object*
2560   named_object()
2561   { return this->named_object_; }
2562
2563   const Named_object*
2564   named_object() const
2565   { return this->named_object_; }
2566
2567   // Set the Named_object.  This is used when we see a type
2568   // declaration followed by a type.
2569   void
2570   set_named_object(Named_object* no)
2571   { this->named_object_ = no; }
2572
2573   // Return the function in which this type is defined.  This will
2574   // return NULL for a type defined in global scope.
2575   const Named_object*
2576   in_function() const
2577   { return this->in_function_; }
2578
2579   // Set the function in which this type is defined.
2580   void
2581   set_in_function(Named_object* f)
2582   { this->in_function_ = f; }
2583
2584   // Return the name of the type.
2585   const std::string&
2586   name() const;
2587
2588   // Return the name of the type for an error message.  The difference
2589   // is that if the type is defined in a different package, this will
2590   // return PACKAGE.NAME.
2591   std::string
2592   message_name() const;
2593
2594   // Return the underlying type.
2595   Type*
2596   real_type()
2597   { return this->type_; }
2598
2599   const Type*
2600   real_type() const
2601   { return this->type_; }
2602
2603   // Return the location.
2604   Location
2605   location() const
2606   { return this->location_; }
2607
2608   // Whether this type is visible.  This only matters when parsing.
2609   bool
2610   is_visible() const
2611   { return this->is_visible_; }
2612
2613   // Mark this type as visible.
2614   void
2615   set_is_visible()
2616   { this->is_visible_ = true; }
2617
2618   // Mark this type as invisible.
2619   void
2620   clear_is_visible()
2621   { this->is_visible_ = false; }
2622
2623   // Whether this is a builtin type.
2624   bool
2625   is_builtin() const
2626   { return Linemap::is_predeclared_location(this->location_); }
2627
2628   // Whether this is a circular type: a pointer or function type that
2629   // refers to itself, which is not possible in C.
2630   bool
2631   is_circular() const
2632   { return this->is_circular_; }
2633
2634   // Return the base type for this type.
2635   Type*
2636   named_base();
2637
2638   const Type*
2639   named_base() const;
2640
2641   // Return whether this is an error type.
2642   bool
2643   is_named_error_type() const;
2644
2645   // Return whether this type is comparable.  If REASON is not NULL,
2646   // set *REASON when returning false.
2647   bool
2648   named_type_is_comparable(std::string* reason) const;
2649
2650   // Add a method to this type.
2651   Named_object*
2652   add_method(const std::string& name, Function*);
2653
2654   // Add a method declaration to this type.
2655   Named_object*
2656   add_method_declaration(const std::string& name, Package* package,
2657                          Function_type* type, Location location);
2658
2659   // Add an existing method--one defined before the type itself was
2660   // defined--to a type.
2661   void
2662   add_existing_method(Named_object*);
2663
2664   // Look up a local method.
2665   Named_object*
2666   find_local_method(const std::string& name) const;
2667
2668   // Return the list of local methods.
2669   const Bindings*
2670   local_methods() const
2671   { return this->local_methods_; }
2672
2673   // Build the complete list of methods, including those from
2674   // anonymous fields, and build method stubs if needed.
2675   void
2676   finalize_methods(Gogo*);
2677
2678   // Return whether this type has any methods.  This should only be
2679   // called after the finalize_methods pass.
2680   bool
2681   has_any_methods() const
2682   { return this->all_methods_ != NULL; }
2683
2684   // Return the methods for this type.  This should only be called
2685   // after the finalized_methods pass.
2686   const Methods*
2687   methods() const
2688   { return this->all_methods_; }
2689
2690   // Return the method to use for NAME.  This returns NULL if there is
2691   // no such method or if the method is ambiguous.  When it returns
2692   // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2693   Method*
2694   method_function(const std::string& name, bool *is_ambiguous) const;
2695
2696   // Return whether NAME is a known field or method which is not
2697   // exported.  This is only used for better error reporting.
2698   bool
2699   is_unexported_local_method(Gogo*, const std::string& name) const;
2700
2701   // Return a pointer to the interface method table for this type for
2702   // the interface INTERFACE.  If IS_POINTER is true, set the type
2703   // descriptor to a pointer to this type, otherwise set it to this
2704   // type.
2705   tree
2706   interface_method_table(Gogo*, const Interface_type* interface,
2707                          bool is_pointer);
2708
2709   // Whether this type has any hidden fields.
2710   bool
2711   named_type_has_hidden_fields(std::string* reason) const;
2712
2713   // Note that a type must be converted to the backend representation
2714   // before we convert this type.
2715   void
2716   add_dependency(Named_type* nt)
2717   { this->dependencies_.push_back(nt); }
2718
2719   // Return true if the size and alignment of the backend
2720   // representation of this type is known.  This is always true after
2721   // types have been converted, but may be false beforehand.
2722   bool
2723   is_named_backend_type_size_known() const
2724   { return this->named_btype_ != NULL && !this->is_placeholder_; }
2725
2726   // Export the type.
2727   void
2728   export_named_type(Export*, const std::string& name) const;
2729
2730   // Import a named type.
2731   static void
2732   import_named_type(Import*, Named_type**);
2733
2734   // Initial conversion to backend representation.
2735   void
2736   convert(Gogo*);
2737
2738  protected:
2739   int
2740   do_traverse(Traverse* traverse)
2741   { return Type::traverse(this->type_, traverse); }
2742
2743   bool
2744   do_verify();
2745
2746   bool
2747   do_has_pointer() const;
2748
2749   bool
2750   do_compare_is_identity(Gogo*) const;
2751
2752   unsigned int
2753   do_hash_for_method(Gogo*) const;
2754
2755   Btype*
2756   do_get_backend(Gogo*);
2757
2758   Expression*
2759   do_type_descriptor(Gogo*, Named_type*);
2760
2761   void
2762   do_reflection(Gogo*, std::string*) const;
2763
2764   void
2765   do_mangled_name(Gogo*, std::string* ret) const;
2766
2767   void
2768   do_export(Export*) const;
2769
2770  private:
2771   // Create the placeholder during conversion.
2772   void
2773   create_placeholder(Gogo*);
2774
2775   // A mapping from interfaces to the associated interface method
2776   // tables for this type.  This maps to a decl.
2777   typedef Unordered_map_hash(const Interface_type*, tree, Type_hash_identical,
2778                              Type_identical) Interface_method_tables;
2779
2780   // A pointer back to the Named_object for this type.
2781   Named_object* named_object_;
2782   // If this type is defined in a function, a pointer back to the
2783   // function in which it is defined.
2784   Named_object* in_function_;
2785   // The actual type.
2786   Type* type_;
2787   // The list of methods defined for this type.  Any named type can
2788   // have methods.
2789   Bindings* local_methods_;
2790   // The full list of methods for this type, including methods
2791   // declared for anonymous fields.
2792   Methods* all_methods_;
2793   // A mapping from interfaces to the associated interface method
2794   // tables for this type.
2795   Interface_method_tables* interface_method_tables_;
2796   // A mapping from interfaces to the associated interface method
2797   // tables for pointers to this type.
2798   Interface_method_tables* pointer_interface_method_tables_;
2799   // The location where this type was defined.
2800   Location location_;
2801   // The backend representation of this type during backend
2802   // conversion.  This is used to avoid endless recursion when a named
2803   // type refers to itself.
2804   Btype* named_btype_;
2805   // A list of types which must be converted to the backend
2806   // representation before this type can be converted.  This is for
2807   // cases like
2808   //   type S1 { p *S2 }
2809   //   type S2 { s S1 }
2810   // where we can't convert S2 to the backend representation unless we
2811   // have converted S1.
2812   std::vector<Named_type*> dependencies_;
2813   // Whether this type is visible.  This is false if this type was
2814   // created because it was referenced by an imported object, but the
2815   // type itself was not exported.  This will always be true for types
2816   // created in the current package.
2817   bool is_visible_;
2818   // Whether this type is erroneous.
2819   bool is_error_;
2820   // Whether the current value of named_btype_ is a placeholder for
2821   // which the final size of the type is not known.
2822   bool is_placeholder_;
2823   // Whether this type has been converted to the backend
2824   // representation.  Implies that is_placeholder_ is false.
2825   bool is_converted_;
2826   // Whether this is a pointer or function type which refers to the
2827   // type itself.
2828   bool is_circular_;
2829   // In a recursive operation such as has_hidden_fields, this flag is
2830   // used to prevent infinite recursion when a type refers to itself.
2831   // This is mutable because it is always reset to false when the
2832   // function exits.
2833   mutable bool seen_;
2834   // Like seen_, but used only by do_compare_is_identity.
2835   mutable bool seen_in_compare_is_identity_;
2836   // Like seen_, but used only by do_get_backend.
2837   bool seen_in_get_backend_;
2838 };
2839
2840 // A forward declaration.  This handles a type which has been declared
2841 // but not defined.
2842
2843 class Forward_declaration_type : public Type
2844 {
2845  public:
2846   Forward_declaration_type(Named_object* named_object);
2847
2848   // The named object associated with this type declaration.  This
2849   // will be resolved.
2850   Named_object*
2851   named_object();
2852
2853   const Named_object*
2854   named_object() const;
2855
2856   // Return the name of the type.
2857   const std::string&
2858   name() const;
2859
2860   // Return the type to which this points.  Give an error if the type
2861   // has not yet been defined.
2862   Type*
2863   real_type();
2864
2865   const Type*
2866   real_type() const;
2867
2868   // Whether the base type has been defined.
2869   bool
2870   is_defined() const;
2871
2872   // Add a method to this type.
2873   Named_object*
2874   add_method(const std::string& name, Function*);
2875
2876   // Add a method declaration to this type.
2877   Named_object*
2878   add_method_declaration(const std::string& name, Function_type*,
2879                          Location);
2880
2881  protected:
2882   int
2883   do_traverse(Traverse* traverse);
2884
2885   bool
2886   do_has_pointer() const
2887   { return this->real_type()->has_pointer(); }
2888
2889   bool
2890   do_compare_is_identity(Gogo* gogo) const
2891   { return this->real_type()->compare_is_identity(gogo); }
2892
2893   unsigned int
2894   do_hash_for_method(Gogo* gogo) const
2895   { return this->real_type()->hash_for_method(gogo); }
2896
2897   Btype*
2898   do_get_backend(Gogo* gogo);
2899
2900   Expression*
2901   do_type_descriptor(Gogo*, Named_type*);
2902
2903   void
2904   do_reflection(Gogo*, std::string*) const;
2905
2906   void
2907   do_mangled_name(Gogo*, std::string* ret) const;
2908
2909   void
2910   do_export(Export*) const;
2911
2912  private:
2913   // Issue a warning about a use of an undefined type.
2914   void
2915   warn() const;
2916
2917   // The type declaration.
2918   Named_object* named_object_;
2919   // Whether we have issued a warning about this type.
2920   mutable bool warned_;
2921 };
2922
2923 // The Type_context struct describes what we expect for the type of an
2924 // expression.
2925
2926 struct Type_context
2927 {
2928   // The exact type we expect, if known.  This may be NULL.
2929   Type* type;
2930   // Whether an abstract type is permitted.
2931   bool may_be_abstract;
2932
2933   // Constructors.
2934   Type_context()
2935     : type(NULL), may_be_abstract(false)
2936   { }
2937
2938   Type_context(Type* a_type, bool a_may_be_abstract)
2939     : type(a_type), may_be_abstract(a_may_be_abstract)
2940   { }
2941 };
2942
2943 #endif // !defined(GO_TYPES_H)