OSDN Git Service

Fix bug with multiple results returning structs with invalid sizes.
[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 the backend representation of this type.
825   Btype*
826   get_backend(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   virtual Btype*
896   do_get_backend(Gogo*) = 0;
897
898   virtual tree
899   do_get_init_tree(Gogo*, tree, bool) = 0;
900
901   virtual tree
902   do_make_expression_tree(Translate_context*, Expression_list*,
903                           source_location);
904
905   virtual Expression*
906   do_type_descriptor(Gogo*, Named_type* name) = 0;
907
908   virtual void
909   do_reflection(Gogo*, std::string*) const = 0;
910
911
912   virtual void
913   do_mangled_name(Gogo*, std::string*) const = 0;
914
915   virtual void
916   do_export(Export*) const;
917
918   // Return whether an expression is an integer.
919   static bool
920   check_int_value(Expression*, const char*, source_location);
921
922   // Return whether a method expects a pointer as the receiver.
923   static bool
924   method_expects_pointer(const Named_object*);
925
926   // Finalize the methods for a type.
927   static void
928   finalize_methods(Gogo*, const Type*, source_location, Methods**);
929
930   // Return a method from a set of methods.
931   static Method*
932   method_function(const Methods*, const std::string& name,
933                   bool* is_ambiguous);
934
935   // Return a composite literal for the type descriptor entry for a
936   // type.
937   static Expression*
938   type_descriptor(Gogo*, Type*);
939
940   // Return a composite literal for the type descriptor entry for
941   // TYPE, using NAME as the name of the type.
942   static Expression*
943   named_type_descriptor(Gogo*, Type* type, Named_type* name);
944
945   // Return a composite literal for a plain type descriptor for this
946   // type with the given kind and name.
947   Expression*
948   plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
949
950   // Build a composite literal for the basic type descriptor.
951   Expression*
952   type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
953                               const Methods*, bool only_value_methods);
954
955   // Make a builtin struct type from a list of fields.
956   static Struct_type*
957   make_builtin_struct_type(int nfields, ...);
958
959   // Make a builtin named type.
960   static Named_type*
961   make_builtin_named_type(const char* name, Type* type);
962
963   // For the benefit of child class reflection string generation.
964   void
965   append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
966   { type->do_reflection(gogo, ret); }
967
968   // For the benefit of child class mangling.
969   void
970   append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
971   { type->do_mangled_name(gogo, ret); }
972
973   // Incorporate a string into a hash code.
974   static unsigned int
975   hash_string(const std::string&, unsigned int);
976
977   // Return the backend representation for the underlying type of a
978   // named type.
979   static Btype*
980   get_named_base_btype(Gogo* gogo, Type* base_type)
981   { return base_type->get_btype_without_hash(gogo); }
982
983  private:
984   // Convert to the desired type classification, or return NULL.  This
985   // is a controlled dynamic_cast.
986   template<typename Type_class, Type_classification type_classification>
987   Type_class*
988   convert()
989   {
990     Type* base = this->base();
991     return (base->classification_ == type_classification
992             ? static_cast<Type_class*>(base)
993             : NULL);
994   }
995
996   template<typename Type_class, Type_classification type_classification>
997   const Type_class*
998   convert() const
999   {
1000     const Type* base = this->base();
1001     return (base->classification_ == type_classification
1002             ? static_cast<Type_class*>(base)
1003             : NULL);
1004   }
1005
1006   template<typename Type_class, Type_classification type_classification>
1007   Type_class*
1008   convert_no_base()
1009   {
1010     return (this->classification_ == type_classification
1011             ? static_cast<Type_class*>(this)
1012             : NULL);
1013   }
1014
1015   template<typename Type_class, Type_classification type_classification>
1016   const Type_class*
1017   convert_no_base() const
1018   {
1019     return (this->classification_ == type_classification
1020             ? static_cast<Type_class*>(this)
1021             : NULL);
1022   }
1023
1024   // Support for are_assignable and are_assignable_hidden_ok.
1025   static bool
1026   are_assignable_check_hidden(const Type* lhs, const Type* rhs,
1027                               bool check_hidden_fields, std::string* reason);
1028
1029   // Get the hash and equality functions for a type.
1030   void
1031   type_functions(const char** hash_fn, const char** equal_fn) const;
1032
1033   // Build a composite literal for the uncommon type information.
1034   Expression*
1035   uncommon_type_constructor(Gogo*, Type* uncommon_type,
1036                             Named_type*, const Methods*,
1037                             bool only_value_methods) const;
1038
1039   // Build a composite literal for the methods.
1040   Expression*
1041   methods_constructor(Gogo*, Type* methods_type, const Methods*,
1042                       bool only_value_methods) const;
1043
1044   // Build a composite literal for one method.
1045   Expression*
1046   method_constructor(Gogo*, Type* method_type, const std::string& name,
1047                      const Method*) const;
1048
1049   static tree
1050   build_receive_return_type(tree type);
1051
1052   // A hash table we use to avoid infinite recursion.
1053   typedef Unordered_set_hash(const Named_type*, Type_hash_identical,
1054                              Type_identical) Types_seen;
1055
1056   // Add all methods for TYPE to the list of methods for THIS.
1057   static void
1058   add_methods_for_type(const Type* type, const Method::Field_indexes*,
1059                        unsigned int depth, bool, bool, Types_seen*,
1060                        Methods**);
1061
1062   static void
1063   add_local_methods_for_type(const Named_type* type,
1064                              const Method::Field_indexes*,
1065                              unsigned int depth, bool, bool, Methods**);
1066
1067   static void
1068   add_embedded_methods_for_type(const Type* type,
1069                                 const Method::Field_indexes*,
1070                                 unsigned int depth, bool, bool, Types_seen*,
1071                                 Methods**);
1072
1073   static void
1074   add_interface_methods_for_type(const Type* type,
1075                                  const Method::Field_indexes*,
1076                                  unsigned int depth, Methods**);
1077
1078   // Build stub methods for a type.
1079   static void
1080   build_stub_methods(Gogo*, const Type* type, const Methods* methods,
1081                      source_location);
1082
1083   static void
1084   build_one_stub_method(Gogo*, Method*, const char* receiver_name,
1085                         const Typed_identifier_list*, bool is_varargs,
1086                         source_location);
1087
1088   static Expression*
1089   apply_field_indexes(Expression*, const Method::Field_indexes*,
1090                       source_location);
1091
1092   // Look for a field or method named NAME in TYPE.
1093   static bool
1094   find_field_or_method(const Type* type, const std::string& name,
1095                        bool receiver_can_be_pointer,
1096                        std::vector<const Named_type*>*, int* level,
1097                        bool* is_method, bool* found_pointer_method,
1098                        std::string* ambig1, std::string* ambig2);
1099
1100   // Get the backend representation for a type without looking in the
1101   // hash table for identical types.
1102   Btype*
1103   get_btype_without_hash(Gogo*);
1104
1105   // A mapping from Type to Btype*, used to ensure that the backend
1106   // representation of identical types is identical.
1107   typedef Unordered_map_hash(const Type*, Btype*, Type_hash_identical,
1108                              Type_identical) Type_btypes;
1109
1110   static Type_btypes type_btypes;
1111
1112   // A list of builtin named types.
1113   static std::vector<Named_type*> named_builtin_types;
1114
1115   // The type classification.
1116   Type_classification classification_;
1117   // The backend representation of the type, once it has been
1118   // determined.
1119   Btype* btype_;
1120   // The decl for the type descriptor for this type.  This starts out
1121   // as NULL and is filled in as needed.
1122   tree type_descriptor_decl_;
1123 };
1124
1125 // Type hash table operations.
1126
1127 class Type_hash_identical
1128 {
1129  public:
1130   unsigned int
1131   operator()(const Type* type) const
1132   { return type->hash_for_method(NULL); }
1133 };
1134
1135 class Type_identical
1136 {
1137  public:
1138   bool
1139   operator()(const Type* t1, const Type* t2) const
1140   { return Type::are_identical(t1, t2, false, NULL); }
1141 };
1142
1143 // An identifier with a type.
1144
1145 class Typed_identifier
1146 {
1147  public:
1148   Typed_identifier(const std::string& name, Type* type,
1149                    source_location location)
1150     : name_(name), type_(type), location_(location)
1151   { }
1152
1153   // Get the name.
1154   const std::string&
1155   name() const
1156   { return this->name_; }
1157
1158   // Get the type.
1159   Type*
1160   type() const
1161   { return this->type_; }
1162
1163   // Return the location where the name was seen.  This is not always
1164   // meaningful.
1165   source_location
1166   location() const
1167   { return this->location_; }
1168
1169   // Set the type--sometimes we see the identifier before the type.
1170   void
1171   set_type(Type* type)
1172   {
1173     go_assert(this->type_ == NULL || type->is_error_type());
1174     this->type_ = type;
1175   }
1176
1177  private:
1178   // Identifier name.
1179   std::string name_;
1180   // Type.
1181   Type* type_;
1182   // The location where the name was seen.
1183   source_location location_;
1184 };
1185
1186 // A list of Typed_identifiers.
1187
1188 class Typed_identifier_list
1189 {
1190  public:
1191   Typed_identifier_list()
1192     : entries_()
1193   { }
1194
1195   // Whether the list is empty.
1196   bool
1197   empty() const
1198   { return this->entries_.empty(); }
1199
1200   // Return the number of entries in the list.
1201   size_t
1202   size() const
1203   { return this->entries_.size(); }
1204
1205   // Add an entry to the end of the list.
1206   void
1207   push_back(const Typed_identifier& td)
1208   { this->entries_.push_back(td); }
1209
1210   // Remove an entry from the end of the list.
1211   void
1212   pop_back()
1213   { this->entries_.pop_back(); }
1214
1215   // Set the type of entry I to TYPE.
1216   void
1217   set_type(size_t i, Type* type)
1218   {
1219     go_assert(i < this->entries_.size());
1220     this->entries_[i].set_type(type);
1221   }
1222
1223   // Sort the entries by name.
1224   void
1225   sort_by_name();
1226
1227   // Traverse types.
1228   int
1229   traverse(Traverse*);
1230
1231   // Return the first and last elements.
1232   Typed_identifier&
1233   front()
1234   { return this->entries_.front(); }
1235
1236   const Typed_identifier&
1237   front() const
1238   { return this->entries_.front(); }
1239
1240   Typed_identifier&
1241   back()
1242   { return this->entries_.back(); }
1243
1244   const Typed_identifier&
1245   back() const
1246   { return this->entries_.back(); }
1247
1248   const Typed_identifier&
1249   at(size_t i) const
1250   { return this->entries_.at(i); }
1251
1252   void
1253   set(size_t i, const Typed_identifier& t)
1254   { this->entries_.at(i) = t; }
1255
1256   void
1257   resize(size_t c)
1258   {
1259     go_assert(c <= this->entries_.size());
1260     this->entries_.resize(c, Typed_identifier("", NULL, UNKNOWN_LOCATION));
1261   }
1262
1263   // Iterators.
1264
1265   typedef std::vector<Typed_identifier>::iterator iterator;
1266   typedef std::vector<Typed_identifier>::const_iterator const_iterator;
1267
1268   iterator
1269   begin()
1270   { return this->entries_.begin(); }
1271
1272   const_iterator
1273   begin() const
1274   { return this->entries_.begin(); }
1275
1276   iterator
1277   end()
1278   { return this->entries_.end(); }
1279
1280   const_iterator
1281   end() const
1282   { return this->entries_.end(); }
1283
1284   // Return a copy of this list.  This returns an independent copy of
1285   // the vector, but does not copy the types.
1286   Typed_identifier_list*
1287   copy() const;
1288
1289  private:
1290   std::vector<Typed_identifier> entries_;
1291 };
1292
1293 // The type of an integer.
1294
1295 class Integer_type : public Type
1296 {
1297  public:
1298   // Create a new integer type.
1299   static Named_type*
1300   create_integer_type(const char* name, bool is_unsigned, int bits,
1301                       int runtime_type_kind);
1302
1303   // Look up an existing integer type.
1304   static Named_type*
1305   lookup_integer_type(const char* name);
1306
1307   // Create an abstract integer type.
1308   static Integer_type*
1309   create_abstract_integer_type();
1310
1311   // Whether this is an abstract integer type.
1312   bool
1313   is_abstract() const
1314   { return this->is_abstract_; }
1315
1316   // Whether this is an unsigned type.
1317   bool
1318   is_unsigned() const
1319   { return this->is_unsigned_; }
1320
1321   // The number of bits.
1322   int
1323   bits() const
1324   { return this->bits_; }
1325
1326   // Whether this type is the same as T.
1327   bool
1328   is_identical(const Integer_type* t) const;
1329
1330  protected:
1331   unsigned int
1332   do_hash_for_method(Gogo*) const;
1333
1334   Btype*
1335   do_get_backend(Gogo*);
1336
1337   tree
1338   do_get_init_tree(Gogo*, tree, bool);
1339
1340   Expression*
1341   do_type_descriptor(Gogo*, Named_type*);
1342
1343   void
1344   do_reflection(Gogo*, std::string*) const;
1345
1346   void
1347   do_mangled_name(Gogo*, std::string*) const;
1348
1349  private:
1350   Integer_type(bool is_abstract, bool is_unsigned, int bits,
1351                int runtime_type_kind)
1352     : Type(TYPE_INTEGER),
1353       is_abstract_(is_abstract), is_unsigned_(is_unsigned), bits_(bits),
1354       runtime_type_kind_(runtime_type_kind)
1355   { }
1356
1357   // Map names of integer types to the types themselves.
1358   typedef std::map<std::string, Named_type*> Named_integer_types;
1359   static Named_integer_types named_integer_types;
1360
1361   // True if this is an abstract type.
1362   bool is_abstract_;
1363   // True if this is an unsigned type.
1364   bool is_unsigned_;
1365   // The number of bits.
1366   int bits_;
1367   // The runtime type code used in the type descriptor for this type.
1368   int runtime_type_kind_;
1369 };
1370
1371 // The type of a floating point number.
1372
1373 class Float_type : public Type
1374 {
1375  public:
1376   // Create a new float type.
1377   static Named_type*
1378   create_float_type(const char* name, int bits, int runtime_type_kind);
1379
1380   // Look up an existing float type.
1381   static Named_type*
1382   lookup_float_type(const char* name);
1383
1384   // Create an abstract float type.
1385   static Float_type*
1386   create_abstract_float_type();
1387
1388   // Whether this is an abstract float type.
1389   bool
1390   is_abstract() const
1391   { return this->is_abstract_; }
1392
1393   // The number of bits.
1394   int
1395   bits() const
1396   { return this->bits_; }
1397
1398   // Whether this type is the same as T.
1399   bool
1400   is_identical(const Float_type* t) const;
1401
1402  protected:
1403   unsigned int
1404   do_hash_for_method(Gogo*) const;
1405
1406   Btype*
1407   do_get_backend(Gogo*);
1408
1409   tree
1410   do_get_init_tree(Gogo*, tree, bool);
1411
1412   Expression*
1413   do_type_descriptor(Gogo*, Named_type*);
1414
1415   void
1416   do_reflection(Gogo*, std::string*) const;
1417
1418   void
1419   do_mangled_name(Gogo*, std::string*) const;
1420
1421  private:
1422   Float_type(bool is_abstract, int bits, int runtime_type_kind)
1423     : Type(TYPE_FLOAT),
1424       is_abstract_(is_abstract), bits_(bits),
1425       runtime_type_kind_(runtime_type_kind)
1426   { }
1427
1428   // Map names of float types to the types themselves.
1429   typedef std::map<std::string, Named_type*> Named_float_types;
1430   static Named_float_types named_float_types;
1431
1432   // True if this is an abstract type.
1433   bool is_abstract_;
1434   // The number of bits in the floating point value.
1435   int bits_;
1436   // The runtime type code used in the type descriptor for this type.
1437   int runtime_type_kind_;
1438 };
1439
1440 // The type of a complex number.
1441
1442 class Complex_type : public Type
1443 {
1444  public:
1445   // Create a new complex type.
1446   static Named_type*
1447   create_complex_type(const char* name, int bits, int runtime_type_kind);
1448
1449   // Look up an existing complex type.
1450   static Named_type*
1451   lookup_complex_type(const char* name);
1452
1453   // Create an abstract complex type.
1454   static Complex_type*
1455   create_abstract_complex_type();
1456
1457   // Whether this is an abstract complex type.
1458   bool
1459   is_abstract() const
1460   { return this->is_abstract_; }
1461
1462   // The number of bits: 64 or 128.
1463   int bits() const
1464   { return this->bits_; }
1465
1466   // Whether this type is the same as T.
1467   bool
1468   is_identical(const Complex_type* t) const;
1469
1470  protected:
1471   unsigned int
1472   do_hash_for_method(Gogo*) const;
1473
1474   Btype*
1475   do_get_backend(Gogo*);
1476
1477   tree
1478   do_get_init_tree(Gogo*, tree, bool);
1479
1480   Expression*
1481   do_type_descriptor(Gogo*, Named_type*);
1482
1483   void
1484   do_reflection(Gogo*, std::string*) const;
1485
1486   void
1487   do_mangled_name(Gogo*, std::string*) const;
1488
1489  private:
1490   Complex_type(bool is_abstract, int bits, int runtime_type_kind)
1491     : Type(TYPE_COMPLEX),
1492       is_abstract_(is_abstract), bits_(bits),
1493       runtime_type_kind_(runtime_type_kind)
1494   { }
1495
1496   // Map names of complex types to the types themselves.
1497   typedef std::map<std::string, Named_type*> Named_complex_types;
1498   static Named_complex_types named_complex_types;
1499
1500   // True if this is an abstract type.
1501   bool is_abstract_;
1502   // The number of bits in the complex value--64 or 128.
1503   int bits_;
1504   // The runtime type code used in the type descriptor for this type.
1505   int runtime_type_kind_;
1506 };
1507
1508 // The type of a string.
1509
1510 class String_type : public Type
1511 {
1512  public:
1513   String_type()
1514     : Type(TYPE_STRING)
1515   { }
1516
1517   // Return a tree for the length of STRING.
1518   static tree
1519   length_tree(Gogo*, tree string);
1520
1521   // Return a tree which points to the bytes of STRING.
1522   static tree
1523   bytes_tree(Gogo*, tree string);
1524
1525  protected:
1526   bool
1527   do_has_pointer() const
1528   { return true; }
1529
1530   Btype*
1531   do_get_backend(Gogo*);
1532
1533   tree
1534   do_get_init_tree(Gogo* gogo, tree, bool);
1535
1536   Expression*
1537   do_type_descriptor(Gogo*, Named_type*);
1538
1539   void
1540   do_reflection(Gogo*, std::string*) const;
1541
1542   void
1543   do_mangled_name(Gogo*, std::string* ret) const;
1544
1545  private:
1546   // The named string type.
1547   static Named_type* string_type_;
1548 };
1549
1550 // The type of a function.
1551
1552 class Function_type : public Type
1553 {
1554  public:
1555   Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
1556                 Typed_identifier_list* results, source_location location)
1557     : Type(TYPE_FUNCTION),
1558       receiver_(receiver), parameters_(parameters), results_(results),
1559       location_(location), is_varargs_(false), is_builtin_(false)
1560   { }
1561
1562   // Get the receiver.
1563   const Typed_identifier*
1564   receiver() const
1565   { return this->receiver_; }
1566
1567   // Get the return names and types.
1568   const Typed_identifier_list*
1569   results() const
1570   { return this->results_; }
1571
1572   // Get the parameter names and types.
1573   const Typed_identifier_list*
1574   parameters() const
1575   { return this->parameters_; }
1576
1577   // Whether this is a varargs function.
1578   bool
1579   is_varargs() const
1580   { return this->is_varargs_; }
1581
1582   // Whether this is a builtin function.
1583   bool
1584   is_builtin() const
1585   { return this->is_builtin_; }
1586
1587   // The location where this type was defined.
1588   source_location
1589   location() const
1590   { return this->location_; }
1591
1592   // Return whether this is a method type.
1593   bool
1594   is_method() const
1595   { return this->receiver_ != NULL; }
1596
1597   // Whether T is a valid redeclaration of this type.  This is called
1598   // when a function is declared more than once.
1599   bool
1600   is_valid_redeclaration(const Function_type* t, std::string*) const;
1601
1602   // Whether this type is the same as T.
1603   bool
1604   is_identical(const Function_type* t, bool ignore_receiver,
1605                bool errors_are_identical, std::string*) const;
1606
1607   // Record that this is a varargs function.
1608   void
1609   set_is_varargs()
1610   { this->is_varargs_ = true; }
1611
1612   // Record that this is a builtin function.
1613   void
1614   set_is_builtin()
1615   { this->is_builtin_ = true; }
1616
1617   // Import a function type.
1618   static Function_type*
1619   do_import(Import*);
1620
1621   // Return a copy of this type without a receiver.  This is only
1622   // valid for a method type.
1623   Function_type*
1624   copy_without_receiver() const;
1625
1626   // Return a copy of this type with a receiver.  This is used when an
1627   // interface method is attached to a named or struct type.
1628   Function_type*
1629   copy_with_receiver(Type*) const;
1630
1631   // Finishing converting function types.
1632   static void
1633   convert_types(Gogo*);
1634
1635   static Type*
1636   make_function_type_descriptor_type();
1637
1638  protected:
1639   int
1640   do_traverse(Traverse*);
1641
1642   // A trampoline function has a pointer which matters for GC.
1643   bool
1644   do_has_pointer() const
1645   { return true; }
1646
1647   unsigned int
1648   do_hash_for_method(Gogo*) const;
1649
1650   Btype*
1651   do_get_backend(Gogo*);
1652
1653   tree
1654   do_get_init_tree(Gogo*, tree, bool);
1655
1656   Expression*
1657   do_type_descriptor(Gogo*, Named_type*);
1658
1659   void
1660   do_reflection(Gogo*, std::string*) const;
1661
1662   void
1663   do_mangled_name(Gogo*, std::string*) const;
1664
1665   void
1666   do_export(Export*) const;
1667
1668  private:
1669   Expression*
1670   type_descriptor_params(Type*, const Typed_identifier*,
1671                          const Typed_identifier_list*);
1672
1673   Btype*
1674   get_function_backend(Gogo*);
1675
1676   // A list of function types with multiple results and their
1677   // placeholder backend representations, used to postpone building
1678   // the structs we use for multiple results until all types are
1679   // converted.
1680   typedef std::vector<std::pair<Function_type*, Btype*> > Placeholders;
1681   static Placeholders placeholders;
1682
1683   // The receiver name and type.  This will be NULL for a normal
1684   // function, non-NULL for a method.
1685   Typed_identifier* receiver_;
1686   // The parameter names and types.
1687   Typed_identifier_list* parameters_;
1688   // The result names and types.  This will be NULL if no result was
1689   // specified.
1690   Typed_identifier_list* results_;
1691   // The location where this type was defined.  This exists solely to
1692   // give a location for the fields of the struct if this function
1693   // returns multiple values.
1694   source_location location_;
1695   // Whether this function takes a variable number of arguments.
1696   bool is_varargs_;
1697   // Whether this is a special builtin function which can not simply
1698   // be called.  This is used for len, cap, etc.
1699   bool is_builtin_;
1700 };
1701
1702 // The type of a pointer.
1703
1704 class Pointer_type : public Type
1705 {
1706  public:
1707   Pointer_type(Type* to_type)
1708     : Type(TYPE_POINTER),
1709       to_type_(to_type)
1710   {}
1711
1712   Type*
1713   points_to() const
1714   { return this->to_type_; }
1715
1716   // Import a pointer type.
1717   static Pointer_type*
1718   do_import(Import*);
1719
1720   static Type*
1721   make_pointer_type_descriptor_type();
1722
1723  protected:
1724   int
1725   do_traverse(Traverse*);
1726
1727   bool
1728   do_has_pointer() const
1729   { return true; }
1730
1731   unsigned int
1732   do_hash_for_method(Gogo*) const;
1733
1734   Btype*
1735   do_get_backend(Gogo*);
1736
1737   tree
1738   do_get_init_tree(Gogo*, tree, bool);
1739
1740   Expression*
1741   do_type_descriptor(Gogo*, Named_type*);
1742
1743   void
1744   do_reflection(Gogo*, std::string*) const;
1745
1746   void
1747   do_mangled_name(Gogo*, std::string*) const;
1748
1749   void
1750   do_export(Export*) const;
1751
1752  private:
1753   // The type to which this type points.
1754   Type* to_type_;
1755 };
1756
1757 // The type of a field in a struct.
1758
1759 class Struct_field
1760 {
1761  public:
1762   explicit Struct_field(const Typed_identifier& typed_identifier)
1763     : typed_identifier_(typed_identifier), tag_(NULL)
1764   { }
1765
1766   // The field name.
1767   const std::string&
1768   field_name() const;
1769
1770   // The field type.
1771   Type*
1772   type() const
1773   { return this->typed_identifier_.type(); }
1774
1775   // The field location.
1776   source_location
1777   location() const
1778   { return this->typed_identifier_.location(); }
1779
1780   // Whether the field has a tag.
1781   bool
1782   has_tag() const
1783   { return this->tag_ != NULL; }
1784
1785   // The tag.
1786   const std::string&
1787   tag() const
1788   {
1789     go_assert(this->tag_ != NULL);
1790     return *this->tag_;
1791   }
1792
1793   // Whether this is an anonymous field.
1794   bool
1795   is_anonymous() const
1796   { return this->typed_identifier_.name().empty(); }
1797
1798   // Set the tag.  FIXME: This is never freed.
1799   void
1800   set_tag(const std::string& tag)
1801   { this->tag_ = new std::string(tag); }
1802
1803   // Set the type.  This is only used in error cases.
1804   void
1805   set_type(Type* type)
1806   { this->typed_identifier_.set_type(type); }
1807
1808  private:
1809   // The field name, type, and location.
1810   Typed_identifier typed_identifier_;
1811   // The field tag.  This is NULL if the field has no tag.
1812   std::string* tag_;
1813 };
1814
1815 // A list of struct fields.
1816
1817 class Struct_field_list
1818 {
1819  public:
1820   Struct_field_list()
1821     : entries_()
1822   { }
1823
1824   // Whether the list is empty.
1825   bool
1826   empty() const
1827   { return this->entries_.empty(); }
1828
1829   // Return the number of entries.
1830   size_t
1831   size() const
1832   { return this->entries_.size(); }
1833
1834   // Add an entry to the end of the list.
1835   void
1836   push_back(const Struct_field& sf)
1837   { this->entries_.push_back(sf); }
1838
1839   // Index into the list.
1840   const Struct_field&
1841   at(size_t i) const
1842   { return this->entries_.at(i); }
1843
1844   // Last entry in list.
1845   Struct_field&
1846   back()
1847   { return this->entries_.back(); }
1848
1849   // Iterators.
1850
1851   typedef std::vector<Struct_field>::iterator iterator;
1852   typedef std::vector<Struct_field>::const_iterator const_iterator;
1853
1854   iterator
1855   begin()
1856   { return this->entries_.begin(); }
1857
1858   const_iterator
1859   begin() const
1860   { return this->entries_.begin(); }
1861
1862   iterator
1863   end()
1864   { return this->entries_.end(); }
1865
1866   const_iterator
1867   end() const
1868   { return this->entries_.end(); }
1869
1870  private:
1871   std::vector<Struct_field> entries_;
1872 };
1873
1874 // The type of a struct.
1875
1876 class Struct_type : public Type
1877 {
1878  public:
1879   Struct_type(Struct_field_list* fields, source_location location)
1880     : Type(TYPE_STRUCT),
1881       fields_(fields), location_(location), all_methods_(NULL)
1882   { }
1883
1884   // Return the field NAME.  This only looks at local fields, not at
1885   // embedded types.  If the field is found, and PINDEX is not NULL,
1886   // this sets *PINDEX to the field index.  If the field is not found,
1887   // this returns NULL.
1888   const Struct_field*
1889   find_local_field(const std::string& name, unsigned int *pindex) const;
1890
1891   // Return the field number INDEX.
1892   const Struct_field*
1893   field(unsigned int index) const
1894   { return &this->fields_->at(index); }
1895
1896   // Get the struct fields.
1897   const Struct_field_list*
1898   fields() const
1899   { return this->fields_; }
1900
1901   // Return the number of fields.
1902   size_t
1903   field_count() const
1904   { return this->fields_->size(); }
1905
1906   // Push a new field onto the end of the struct.  This is used when
1907   // building a closure variable.
1908   void
1909   push_field(const Struct_field& sf)
1910   { this->fields_->push_back(sf); }
1911
1912   // Return an expression referring to field NAME in STRUCT_EXPR, or
1913   // NULL if there is no field with that name.
1914   Field_reference_expression*
1915   field_reference(Expression* struct_expr, const std::string& name,
1916                   source_location) const;
1917
1918   // Return the total number of fields, including embedded fields.
1919   // This is the number of values which can appear in a conversion to
1920   // this type.
1921   unsigned int
1922   total_field_count() const;
1923
1924   // Whether this type is identical with T.
1925   bool
1926   is_identical(const Struct_type* t, bool errors_are_identical) const;
1927
1928   // Whether this struct type has any hidden fields.  This returns
1929   // true if any fields have hidden names, or if any non-pointer
1930   // anonymous fields have types with hidden fields.
1931   bool
1932   struct_has_hidden_fields(const Named_type* within, std::string*) const;
1933
1934   // Return whether NAME is a local field which is not exported.  This
1935   // is only used for better error reporting.
1936   bool
1937   is_unexported_local_field(Gogo*, const std::string& name) const;
1938
1939   // If this is an unnamed struct, build the complete list of methods,
1940   // including those from anonymous fields, and build methods stubs if
1941   // needed.
1942   void
1943   finalize_methods(Gogo*);
1944
1945   // Return whether this type has any methods.  This should only be
1946   // called after the finalize_methods pass.
1947   bool
1948   has_any_methods() const
1949   { return this->all_methods_ != NULL; }
1950
1951   // Return the methods for tihs type.  This should only be called
1952   // after the finalize_methods pass.
1953   const Methods*
1954   methods() const
1955   { return this->all_methods_; }
1956
1957   // Return the method to use for NAME.  This returns NULL if there is
1958   // no such method or if the method is ambiguous.  When it returns
1959   // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
1960   Method*
1961   method_function(const std::string& name, bool* is_ambiguous) const;
1962
1963   // Traverse just the field types of a struct type.
1964   int
1965   traverse_field_types(Traverse* traverse)
1966   { return this->do_traverse(traverse); }
1967
1968   // Import a struct type.
1969   static Struct_type*
1970   do_import(Import*);
1971
1972   static Type*
1973   make_struct_type_descriptor_type();
1974
1975  protected:
1976   int
1977   do_traverse(Traverse*);
1978
1979   bool
1980   do_verify();
1981
1982   bool
1983   do_has_pointer() const;
1984
1985   unsigned int
1986   do_hash_for_method(Gogo*) const;
1987
1988   Btype*
1989   do_get_backend(Gogo*);
1990
1991   tree
1992   do_get_init_tree(Gogo*, tree, bool);
1993
1994   Expression*
1995   do_type_descriptor(Gogo*, Named_type*);
1996
1997   void
1998   do_reflection(Gogo*, std::string*) const;
1999
2000   void
2001   do_mangled_name(Gogo*, std::string*) const;
2002
2003   void
2004   do_export(Export*) const;
2005
2006  private:
2007   // Used to avoid infinite loops in field_reference_depth.
2008   struct Saw_named_type
2009   {
2010     Saw_named_type* next;
2011     Named_type* nt;
2012   };
2013
2014   Field_reference_expression*
2015   field_reference_depth(Expression* struct_expr, const std::string& name,
2016                         source_location, Saw_named_type*,
2017                         unsigned int* depth) const;
2018
2019   // The fields of the struct.
2020   Struct_field_list* fields_;
2021   // The place where the struct was declared.
2022   source_location location_;
2023   // If this struct is unnamed, a list of methods.
2024   Methods* all_methods_;
2025 };
2026
2027 // The type of an array.
2028
2029 class Array_type : public Type
2030 {
2031  public:
2032   Array_type(Type* element_type, Expression* length)
2033     : Type(TYPE_ARRAY),
2034       element_type_(element_type), length_(length), length_tree_(NULL)
2035   { }
2036
2037   // Return the element type.
2038   Type*
2039   element_type() const
2040   { return this->element_type_; }
2041
2042   // Return the length.  This will return NULL for an open array.
2043   Expression*
2044   length() const
2045   { return this->length_; }
2046
2047   // Whether this type is identical with T.
2048   bool
2049   is_identical(const Array_type* t, bool errors_are_identical) const;
2050
2051   // Whether this type has any hidden fields.
2052   bool
2053   array_has_hidden_fields(const Named_type* within, std::string* reason) const
2054   { return this->element_type_->has_hidden_fields(within, reason); }
2055
2056   // Return a tree for the pointer to the values in an array.
2057   tree
2058   value_pointer_tree(Gogo*, tree array) const;
2059
2060   // Return a tree for the length of an array with this type.
2061   tree
2062   length_tree(Gogo*, tree array);
2063
2064   // Return a tree for the capacity of an array with this type.
2065   tree
2066   capacity_tree(Gogo*, tree array);
2067
2068   // Import an array type.
2069   static Array_type*
2070   do_import(Import*);
2071
2072   // Return the backend representation of the element type.
2073   Btype*
2074   get_backend_element(Gogo*);
2075
2076   // Return the backend representation of the length.
2077   Bexpression*
2078   get_backend_length(Gogo*);
2079
2080   static Type*
2081   make_array_type_descriptor_type();
2082
2083   static Type*
2084   make_slice_type_descriptor_type();
2085
2086  protected:
2087   int
2088   do_traverse(Traverse* traverse);
2089
2090   bool
2091   do_verify();
2092
2093   bool
2094   do_has_pointer() const
2095   {
2096     return this->length_ == NULL || this->element_type_->has_pointer();
2097   }
2098
2099   unsigned int
2100   do_hash_for_method(Gogo*) const;
2101
2102   bool
2103   do_check_make_expression(Expression_list*, source_location);
2104
2105   Btype*
2106   do_get_backend(Gogo*);
2107
2108   tree
2109   do_get_init_tree(Gogo*, tree, bool);
2110
2111   tree
2112   do_make_expression_tree(Translate_context*, Expression_list*,
2113                           source_location);
2114
2115   Expression*
2116   do_type_descriptor(Gogo*, Named_type*);
2117
2118   void
2119   do_reflection(Gogo*, std::string*) const;
2120
2121   void
2122   do_mangled_name(Gogo*, std::string*) const;
2123
2124   void
2125   do_export(Export*) const;
2126
2127  private:
2128   bool
2129   verify_length();
2130
2131   tree
2132   get_length_tree(Gogo*);
2133
2134   Expression*
2135   array_type_descriptor(Gogo*, Named_type*);
2136
2137   Expression*
2138   slice_type_descriptor(Gogo*, Named_type*);
2139
2140   // The type of elements of the array.
2141   Type* element_type_;
2142   // The number of elements.  This may be NULL.
2143   Expression* length_;
2144   // The length as a tree.  We only want to compute this once.
2145   tree length_tree_;
2146 };
2147
2148 // The type of a map.
2149
2150 class Map_type : public Type
2151 {
2152  public:
2153   Map_type(Type* key_type, Type* val_type, source_location location)
2154     : Type(TYPE_MAP),
2155       key_type_(key_type), val_type_(val_type), location_(location)
2156   { }
2157
2158   // Return the key type.
2159   Type*
2160   key_type() const
2161   { return this->key_type_; }
2162
2163   // Return the value type.
2164   Type*
2165   val_type() const
2166   { return this->val_type_; }
2167
2168   // Whether this type is identical with T.
2169   bool
2170   is_identical(const Map_type* t, bool errors_are_identical) const;
2171
2172   // Import a map type.
2173   static Map_type*
2174   do_import(Import*);
2175
2176   static Type*
2177   make_map_type_descriptor_type();
2178
2179  protected:
2180   int
2181   do_traverse(Traverse*);
2182
2183   bool
2184   do_verify();
2185
2186   bool
2187   do_has_pointer() const
2188   { return true; }
2189
2190   unsigned int
2191   do_hash_for_method(Gogo*) const;
2192
2193   bool
2194   do_check_make_expression(Expression_list*, source_location);
2195
2196   Btype*
2197   do_get_backend(Gogo*);
2198
2199   tree
2200   do_get_init_tree(Gogo*, tree, bool);
2201
2202   tree
2203   do_make_expression_tree(Translate_context*, Expression_list*,
2204                           source_location);
2205
2206   Expression*
2207   do_type_descriptor(Gogo*, Named_type*);
2208
2209   void
2210   do_reflection(Gogo*, std::string*) const;
2211
2212   void
2213   do_mangled_name(Gogo*, std::string*) const;
2214
2215   void
2216   do_export(Export*) const;
2217
2218  private:
2219   // The key type.
2220   Type* key_type_;
2221   // The value type.
2222   Type* val_type_;
2223   // Where the type was defined.
2224   source_location location_;
2225 };
2226
2227 // The type of a channel.
2228
2229 class Channel_type : public Type
2230 {
2231  public:
2232   Channel_type(bool may_send, bool may_receive, Type* element_type)
2233     : Type(TYPE_CHANNEL),
2234       may_send_(may_send), may_receive_(may_receive),
2235       element_type_(element_type)
2236   { go_assert(may_send || may_receive); }
2237
2238   // Whether this channel can send data.
2239   bool
2240   may_send() const
2241   { return this->may_send_; }
2242
2243   // Whether this channel can receive data.
2244   bool
2245   may_receive() const
2246   { return this->may_receive_; }
2247
2248   // The type of the values that may be sent on this channel.  This is
2249   // NULL if any type may be sent.
2250   Type*
2251   element_type() const
2252   { return this->element_type_; }
2253
2254   // Whether this type is identical with T.
2255   bool
2256   is_identical(const Channel_type* t, bool errors_are_identical) const;
2257
2258   // Import a channel type.
2259   static Channel_type*
2260   do_import(Import*);
2261
2262   static Type*
2263   make_chan_type_descriptor_type();
2264
2265  protected:
2266   int
2267   do_traverse(Traverse* traverse)
2268   { return Type::traverse(this->element_type_, traverse); }
2269
2270   bool
2271   do_has_pointer() const
2272   { return true; }
2273
2274   unsigned int
2275   do_hash_for_method(Gogo*) const;
2276
2277   bool
2278   do_check_make_expression(Expression_list*, source_location);
2279
2280   Btype*
2281   do_get_backend(Gogo*);
2282
2283   tree
2284   do_get_init_tree(Gogo*, tree, bool);
2285
2286   tree
2287   do_make_expression_tree(Translate_context*, Expression_list*,
2288                           source_location);
2289
2290   Expression*
2291   do_type_descriptor(Gogo*, Named_type*);
2292
2293   void
2294   do_reflection(Gogo*, std::string*) const;
2295
2296   void
2297   do_mangled_name(Gogo*, std::string*) const;
2298
2299   void
2300   do_export(Export*) const;
2301
2302  private:
2303   // Whether this channel can send data.
2304   bool may_send_;
2305   // Whether this channel can receive data.
2306   bool may_receive_;
2307   // The types of elements which may be sent on this channel.  If this
2308   // is NULL, it means that any type may be sent.
2309   Type* element_type_;
2310 };
2311
2312 // An interface type.
2313
2314 class Interface_type : public Type
2315 {
2316  public:
2317   Interface_type(Typed_identifier_list* methods, source_location location)
2318     : Type(TYPE_INTERFACE),
2319       methods_(methods), location_(location)
2320   { go_assert(methods == NULL || !methods->empty()); }
2321
2322   // The location where the interface type was defined.
2323   source_location
2324   location() const
2325   { return this->location_; }
2326
2327   // Return whether this is an empty interface.
2328   bool
2329   is_empty() const
2330   { return this->methods_ == NULL; }
2331
2332   // Return the list of methods.  This will return NULL for an empty
2333   // interface.
2334   const Typed_identifier_list*
2335   methods() const
2336   { return this->methods_; }
2337
2338   // Return the number of methods.
2339   size_t
2340   method_count() const
2341   { return this->methods_ == NULL ? 0 : this->methods_->size(); }
2342
2343   // Return the method NAME, or NULL.
2344   const Typed_identifier*
2345   find_method(const std::string& name) const;
2346
2347   // Return the zero-based index of method NAME.
2348   size_t
2349   method_index(const std::string& name) const;
2350
2351   // Finalize the methods.  This handles interface inheritance.
2352   void
2353   finalize_methods();
2354
2355   // Return true if T implements this interface.  If this returns
2356   // false, and REASON is not NULL, it sets *REASON to the reason that
2357   // it fails.
2358   bool
2359   implements_interface(const Type* t, std::string* reason) const;
2360
2361   // Whether this type is identical with T.  REASON is as in
2362   // implements_interface.
2363   bool
2364   is_identical(const Interface_type* t, bool errors_are_identical) const;
2365
2366   // Whether we can assign T to this type.  is_identical is known to
2367   // be false.
2368   bool
2369   is_compatible_for_assign(const Interface_type*, std::string* reason) const;
2370
2371   // Return whether NAME is a method which is not exported.  This is
2372   // only used for better error reporting.
2373   bool
2374   is_unexported_method(Gogo*, const std::string& name) const;
2375
2376   // Import an interface type.
2377   static Interface_type*
2378   do_import(Import*);
2379
2380   // Make a struct for an empty interface type.
2381   static Btype*
2382   get_backend_empty_interface_type(Gogo*);
2383
2384   static Type*
2385   make_interface_type_descriptor_type();
2386
2387  protected:
2388   int
2389   do_traverse(Traverse*);
2390
2391   bool
2392   do_has_pointer() const
2393   { return true; }
2394
2395   unsigned int
2396   do_hash_for_method(Gogo*) const;
2397
2398   Btype*
2399   do_get_backend(Gogo*);
2400
2401   tree
2402   do_get_init_tree(Gogo* gogo, tree, bool);
2403
2404   Expression*
2405   do_type_descriptor(Gogo*, Named_type*);
2406
2407   void
2408   do_reflection(Gogo*, std::string*) const;
2409
2410   void
2411   do_mangled_name(Gogo*, std::string*) const;
2412
2413   void
2414   do_export(Export*) const;
2415
2416  private:
2417   // The list of methods associated with the interface.  This will be
2418   // NULL for the empty interface.
2419   Typed_identifier_list* methods_;
2420   // The location where the interface was defined.
2421   source_location location_;
2422 };
2423
2424 // The value we keep for a named type.  This lets us get the right
2425 // name when we convert to trees.  Note that we don't actually keep
2426 // the name here; the name is in the Named_object which points to
2427 // this.  This object exists to hold a unique tree which represents
2428 // the type.
2429
2430 class Named_type : public Type
2431 {
2432  public:
2433   Named_type(Named_object* named_object, Type* type, source_location location)
2434     : Type(TYPE_NAMED),
2435       named_object_(named_object), in_function_(NULL), type_(type),
2436       local_methods_(NULL), all_methods_(NULL),
2437       interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
2438       location_(location), named_btype_(NULL), dependencies_(),
2439       is_visible_(true), is_error_(false), is_converted_(false),
2440       is_circular_(false), seen_(0)
2441   { }
2442
2443   // Return the associated Named_object.  This holds the actual name.
2444   Named_object*
2445   named_object()
2446   { return this->named_object_; }
2447
2448   const Named_object*
2449   named_object() const
2450   { return this->named_object_; }
2451
2452   // Set the Named_object.  This is used when we see a type
2453   // declaration followed by a type.
2454   void
2455   set_named_object(Named_object* no)
2456   { this->named_object_ = no; }
2457
2458   // Return the function in which this type is defined.  This will
2459   // return NULL for a type defined in global scope.
2460   const Named_object*
2461   in_function() const
2462   { return this->in_function_; }
2463
2464   // Set the function in which this type is defined.
2465   void
2466   set_in_function(Named_object* f)
2467   { this->in_function_ = f; }
2468
2469   // Return the name of the type.
2470   const std::string&
2471   name() const;
2472
2473   // Return the name of the type for an error message.  The difference
2474   // is that if the type is defined in a different package, this will
2475   // return PACKAGE.NAME.
2476   std::string
2477   message_name() const;
2478
2479   // Return the underlying type.
2480   Type*
2481   real_type()
2482   { return this->type_; }
2483
2484   const Type*
2485   real_type() const
2486   { return this->type_; }
2487
2488   // Return the location.
2489   source_location
2490   location() const
2491   { return this->location_; }
2492
2493   // Whether this type is visible.  This only matters when parsing.
2494   bool
2495   is_visible() const
2496   { return this->is_visible_; }
2497
2498   // Mark this type as visible.
2499   void
2500   set_is_visible()
2501   { this->is_visible_ = true; }
2502
2503   // Mark this type as invisible.
2504   void
2505   clear_is_visible()
2506   { this->is_visible_ = false; }
2507
2508   // Whether this is a builtin type.
2509   bool
2510   is_builtin() const
2511   { return this->location_ == BUILTINS_LOCATION; }
2512
2513   // Whether this is a circular type: a pointer or function type that
2514   // refers to itself, which is not possible in C.
2515   bool
2516   is_circular() const
2517   { return this->is_circular_; }
2518
2519   // Return the base type for this type.
2520   Type*
2521   named_base();
2522
2523   const Type*
2524   named_base() const;
2525
2526   // Return whether this is an error type.
2527   bool
2528   is_named_error_type() const;
2529
2530   // Add a method to this type.
2531   Named_object*
2532   add_method(const std::string& name, Function*);
2533
2534   // Add a method declaration to this type.
2535   Named_object*
2536   add_method_declaration(const std::string& name, Package* package,
2537                          Function_type* type, source_location location);
2538
2539   // Add an existing method--one defined before the type itself was
2540   // defined--to a type.
2541   void
2542   add_existing_method(Named_object*);
2543
2544   // Look up a local method.
2545   Named_object*
2546   find_local_method(const std::string& name) const;
2547
2548   // Return the list of local methods.
2549   const Bindings*
2550   local_methods() const
2551   { return this->local_methods_; }
2552
2553   // Build the complete list of methods, including those from
2554   // anonymous fields, and build method stubs if needed.
2555   void
2556   finalize_methods(Gogo*);
2557
2558   // Return whether this type has any methods.  This should only be
2559   // called after the finalize_methods pass.
2560   bool
2561   has_any_methods() const
2562   { return this->all_methods_ != NULL; }
2563
2564   // Return the methods for this type.  This should only be called
2565   // after the finalized_methods pass.
2566   const Methods*
2567   methods() const
2568   { return this->all_methods_; }
2569
2570   // Return the method to use for NAME.  This returns NULL if there is
2571   // no such method or if the method is ambiguous.  When it returns
2572   // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2573   Method*
2574   method_function(const std::string& name, bool *is_ambiguous) const;
2575
2576   // Return whether NAME is a known field or method which is not
2577   // exported.  This is only used for better error reporting.
2578   bool
2579   is_unexported_local_method(Gogo*, const std::string& name) const;
2580
2581   // Return a pointer to the interface method table for this type for
2582   // the interface INTERFACE.  If IS_POINTER is true, set the type
2583   // descriptor to a pointer to this type, otherwise set it to this
2584   // type.
2585   tree
2586   interface_method_table(Gogo*, const Interface_type* interface,
2587                          bool is_pointer);
2588
2589   // Whether this type has any hidden fields.
2590   bool
2591   named_type_has_hidden_fields(std::string* reason) const;
2592
2593   // Note that a type must be converted to the backend representation
2594   // before we convert this type.
2595   void
2596   add_dependency(Named_type* nt)
2597   { this->dependencies_.push_back(nt); }
2598
2599   // Export the type.
2600   void
2601   export_named_type(Export*, const std::string& name) const;
2602
2603   // Import a named type.
2604   static void
2605   import_named_type(Import*, Named_type**);
2606
2607   // Initial conversion to backend representation.
2608   void
2609   convert(Gogo*);
2610
2611  protected:
2612   int
2613   do_traverse(Traverse* traverse)
2614   { return Type::traverse(this->type_, traverse); }
2615
2616   bool
2617   do_verify();
2618
2619   bool
2620   do_has_pointer() const;
2621
2622   unsigned int
2623   do_hash_for_method(Gogo*) const;
2624
2625   bool
2626   do_check_make_expression(Expression_list* args, source_location location)
2627   { return this->type_->check_make_expression(args, location); }
2628
2629   Btype*
2630   do_get_backend(Gogo*);
2631
2632   tree
2633   do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
2634   { return this->type_->get_typed_init_tree(gogo, type_tree, is_clear); }
2635
2636   tree
2637   do_make_expression_tree(Translate_context* context, Expression_list* args,
2638                           source_location location)
2639   { return this->type_->make_expression_tree(context, args, location); }
2640
2641   Expression*
2642   do_type_descriptor(Gogo*, Named_type*);
2643
2644   void
2645   do_reflection(Gogo*, std::string*) const;
2646
2647   void
2648   do_mangled_name(Gogo*, std::string* ret) const;
2649
2650   void
2651   do_export(Export*) const;
2652
2653  private:
2654   // Create the placeholder during conversion.
2655   void
2656   create_placeholder(Gogo*);
2657
2658   // A mapping from interfaces to the associated interface method
2659   // tables for this type.  This maps to a decl.
2660   typedef Unordered_map_hash(const Interface_type*, tree, Type_hash_identical,
2661                              Type_identical) Interface_method_tables;
2662
2663   // A pointer back to the Named_object for this type.
2664   Named_object* named_object_;
2665   // If this type is defined in a function, a pointer back to the
2666   // function in which it is defined.
2667   Named_object* in_function_;
2668   // The actual type.
2669   Type* type_;
2670   // The list of methods defined for this type.  Any named type can
2671   // have methods.
2672   Bindings* local_methods_;
2673   // The full list of methods for this type, including methods
2674   // declared for anonymous fields.
2675   Methods* all_methods_;
2676   // A mapping from interfaces to the associated interface method
2677   // tables for this type.
2678   Interface_method_tables* interface_method_tables_;
2679   // A mapping from interfaces to the associated interface method
2680   // tables for pointers to this type.
2681   Interface_method_tables* pointer_interface_method_tables_;
2682   // The location where this type was defined.
2683   source_location location_;
2684   // The backend representation of this type during backend
2685   // conversion.  This is used to avoid endless recursion when a named
2686   // type refers to itself.
2687   Btype* named_btype_;
2688   // A list of types which must be converted to the backend
2689   // representation before this type can be converted.  This is for
2690   // cases like
2691   //   type S1 { p *S2 }
2692   //   type S2 { s S1 }
2693   // where we can't convert S2 to the backend representation unless we
2694   // have converted S1.
2695   std::vector<Named_type*> dependencies_;
2696   // Whether this type is visible.  This is false if this type was
2697   // created because it was referenced by an imported object, but the
2698   // type itself was not exported.  This will always be true for types
2699   // created in the current package.
2700   bool is_visible_;
2701   // Whether this type is erroneous.
2702   bool is_error_;
2703   // Whether this type has been converted to the backend
2704   // representation.
2705   bool is_converted_;
2706   // Whether this is a pointer or function type which refers to the
2707   // type itself.
2708   bool is_circular_;
2709   // In a recursive operation such as has_hidden_fields, this flag is
2710   // used to prevent infinite recursion when a type refers to itself.
2711   // This is mutable because it is always reset to false when the
2712   // function exits.
2713   mutable int seen_;
2714 };
2715
2716 // A forward declaration.  This handles a type which has been declared
2717 // but not defined.
2718
2719 class Forward_declaration_type : public Type
2720 {
2721  public:
2722   Forward_declaration_type(Named_object* named_object);
2723
2724   // The named object associated with this type declaration.  This
2725   // will be resolved.
2726   Named_object*
2727   named_object();
2728
2729   const Named_object*
2730   named_object() const;
2731
2732   // Return the name of the type.
2733   const std::string&
2734   name() const;
2735
2736   // Return the type to which this points.  Give an error if the type
2737   // has not yet been defined.
2738   Type*
2739   real_type();
2740
2741   const Type*
2742   real_type() const;
2743
2744   // Whether the base type has been defined.
2745   bool
2746   is_defined() const;
2747
2748   // Add a method to this type.
2749   Named_object*
2750   add_method(const std::string& name, Function*);
2751
2752   // Add a method declaration to this type.
2753   Named_object*
2754   add_method_declaration(const std::string& name, Function_type*,
2755                          source_location);
2756
2757  protected:
2758   int
2759   do_traverse(Traverse* traverse);
2760
2761   bool
2762   do_has_pointer() const
2763   { return this->real_type()->has_pointer(); }
2764
2765   unsigned int
2766   do_hash_for_method(Gogo* gogo) const
2767   { return this->real_type()->hash_for_method(gogo); }
2768
2769   bool
2770   do_check_make_expression(Expression_list* args, source_location location)
2771   { return this->base()->check_make_expression(args, location); }
2772
2773   Btype*
2774   do_get_backend(Gogo* gogo);
2775
2776   tree
2777   do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
2778   { return this->base()->get_typed_init_tree(gogo, type_tree, is_clear); }
2779
2780   tree
2781   do_make_expression_tree(Translate_context* context, Expression_list* args,
2782                           source_location location)
2783   { return this->base()->make_expression_tree(context, args, location); }
2784
2785   Expression*
2786   do_type_descriptor(Gogo*, Named_type*);
2787
2788   void
2789   do_reflection(Gogo*, std::string*) const;
2790
2791   void
2792   do_mangled_name(Gogo*, std::string* ret) const;
2793
2794   void
2795   do_export(Export*) const;
2796
2797  private:
2798   // Issue a warning about a use of an undefined type.
2799   void
2800   warn() const;
2801
2802   // The type declaration.
2803   Named_object* named_object_;
2804   // Whether we have issued a warning about this type.
2805   mutable bool warned_;
2806 };
2807
2808 // The Type_context struct describes what we expect for the type of an
2809 // expression.
2810
2811 struct Type_context
2812 {
2813   // The exact type we expect, if known.  This may be NULL.
2814   Type* type;
2815   // Whether an abstract type is permitted.
2816   bool may_be_abstract;
2817
2818   // Constructors.
2819   Type_context()
2820     : type(NULL), may_be_abstract(false)
2821   { }
2822
2823   Type_context(Type* a_type, bool a_may_be_abstract)
2824     : type(a_type), may_be_abstract(a_may_be_abstract)
2825   { }
2826 };
2827
2828 #endif // !defined(GO_TYPES_H)