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