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