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