OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [go/] [gofrontend/] [types.h] - Blame information for rev 714

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.