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