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