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