OSDN Git Service

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