Remove the types float and complex.
[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 Field_reference_expression*
1984 field_reference_depth(Expression* struct_expr, const std::string& name,
1985 source_location, unsigned int* depth) const;
1986
1987 static Type*
1988 make_struct_type_descriptor_type();
1989
1990 // The fields of the struct.
1991 Struct_field_list* fields_;
1992 // The place where the struct was declared.
1993 source_location location_;
1994 // If this struct is unnamed, a list of methods.
1995 Methods* all_methods_;
1996 // A list of structs which must be converted to the backend
1997 // representation before this struct can be converted. This is for
1998 // cases like
1999 // type S1 { p *S2 }
2000 // type S2 { s S1 }
2001 // where we must start converting S2 before we start converting S1.
2002 // That is because we can fully convert S1 before S2 is complete,
2003 // but we can not fully convert S2 before S1 is complete. If we
2004 // start converting S1 first, we won't be able to convert S2.
2005 std::vector<Named_type*> prerequisites_;
2006 };
2007
2008 // The type of an array.
2009
2010 class Array_type : public Type
2011 {
2012 public:
2013 Array_type(Type* element_type, Expression* length)
2014 : Type(TYPE_ARRAY),
2015 element_type_(element_type), length_(length), length_tree_(NULL)
2016 { }
2017
2018 // Return the element type.
2019 Type*
2020 element_type() const
2021 { return this->element_type_; }
2022
2023 // Return the length. This will return NULL for an open array.
2024 Expression*
2025 length() const
2026 { return this->length_; }
2027
2028 // Whether this type is identical with T.
2029 bool
2030 is_identical(const Array_type* t, bool errors_are_identical) const;
2031
2032 // Whether this type has any hidden fields.
2033 bool
2034 array_has_hidden_fields(const Named_type* within, std::string* reason) const
2035 { return this->element_type_->has_hidden_fields(within, reason); }
2036
2037 // Return a tree for the pointer to the values in an array.
2038 tree
2039 value_pointer_tree(Gogo*, tree array) const;
2040
2041 // Return a tree for the length of an array with this type.
2042 tree
2043 length_tree(Gogo*, tree array);
2044
2045 // Return a tree for the capacity of an array with this type.
2046 tree
2047 capacity_tree(Gogo*, tree array);
2048
2049 // Import an array type.
2050 static Array_type*
2051 do_import(Import*);
2052
2053 // Fill in the fields for a named slice type.
2054 tree
2055 fill_in_tree(Gogo*, tree);
2056
2057 protected:
2058 int
2059 do_traverse(Traverse* traverse);
2060
2061 bool
2062 do_verify();
2063
2064 bool
2065 do_has_pointer() const
2066 {
2067 return this->length_ == NULL || this->element_type_->has_pointer();
2068 }
2069
2070 unsigned int
2071 do_hash_for_method(Gogo*) const;
2072
2073 bool
2074 do_check_make_expression(Expression_list*, source_location);
2075
2076 tree
2077 do_get_tree(Gogo*);
2078
2079 tree
2080 do_get_init_tree(Gogo*, tree, bool);
2081
2082 tree
2083 do_make_expression_tree(Translate_context*, Expression_list*,
2084 source_location);
2085
2086 Expression*
2087 do_type_descriptor(Gogo*, Named_type*);
2088
2089 void
2090 do_reflection(Gogo*, std::string*) const;
2091
2092 void
2093 do_mangled_name(Gogo*, std::string*) const;
2094
2095 void
2096 do_export(Export*) const;
2097
2098 private:
2099 bool
2100 verify_length();
2101
2102 tree
2103 get_length_tree(Gogo*);
2104
2105 Type*
2106 make_array_type_descriptor_type();
2107
2108 Type*
2109 make_slice_type_descriptor_type();
2110
2111 Expression*
2112 array_type_descriptor(Gogo*, Named_type*);
2113
2114 Expression*
2115 slice_type_descriptor(Gogo*, Named_type*);
2116
2117 // The type of elements of the array.
2118 Type* element_type_;
2119 // The number of elements. This may be NULL.
2120 Expression* length_;
2121 // The length as a tree. We only want to compute this once.
2122 tree length_tree_;
2123 };
2124
2125 // The type of a map.
2126
2127 class Map_type : public Type
2128 {
2129 public:
2130 Map_type(Type* key_type, Type* val_type, source_location location)
2131 : Type(TYPE_MAP),
2132 key_type_(key_type), val_type_(val_type), location_(location)
2133 { }
2134
2135 // Return the key type.
2136 Type*
2137 key_type() const
2138 { return this->key_type_; }
2139
2140 // Return the value type.
2141 Type*
2142 val_type() const
2143 { return this->val_type_; }
2144
2145 // Whether this type is identical with T.
2146 bool
2147 is_identical(const Map_type* t, bool errors_are_identical) const;
2148
2149 // Import a map type.
2150 static Map_type*
2151 do_import(Import*);
2152
2153 protected:
2154 int
2155 do_traverse(Traverse*);
2156
2157 bool
2158 do_verify();
2159
2160 bool
2161 do_has_pointer() const
2162 { return true; }
2163
2164 unsigned int
2165 do_hash_for_method(Gogo*) const;
2166
2167 bool
2168 do_check_make_expression(Expression_list*, source_location);
2169
2170 tree
2171 do_get_tree(Gogo*);
2172
2173 tree
2174 do_get_init_tree(Gogo*, tree, bool);
2175
2176 tree
2177 do_make_expression_tree(Translate_context*, Expression_list*,
2178 source_location);
2179
2180 Expression*
2181 do_type_descriptor(Gogo*, Named_type*);
2182
2183 void
2184 do_reflection(Gogo*, std::string*) const;
2185
2186 void
2187 do_mangled_name(Gogo*, std::string*) const;
2188
2189 void
2190 do_export(Export*) const;
2191
2192 private:
2193 static Type*
2194 make_map_type_descriptor_type();
2195
2196 // The key type.
2197 Type* key_type_;
2198 // The value type.
2199 Type* val_type_;
2200 // Where the type was defined.
2201 source_location location_;
2202 };
2203
2204 // The type of a channel.
2205
2206 class Channel_type : public Type
2207 {
2208 public:
2209 Channel_type(bool may_send, bool may_receive, Type* element_type)
2210 : Type(TYPE_CHANNEL),
2211 may_send_(may_send), may_receive_(may_receive),
2212 element_type_(element_type)
2213 { gcc_assert(may_send || may_receive); }
2214
2215 // Whether this channel can send data.
2216 bool
2217 may_send() const
2218 { return this->may_send_; }
2219
2220 // Whether this channel can receive data.
2221 bool
2222 may_receive() const
2223 { return this->may_receive_; }
2224
2225 // The type of the values that may be sent on this channel. This is
2226 // NULL if any type may be sent.
2227 Type*
2228 element_type() const
2229 { return this->element_type_; }
2230
2231 // Whether this type is identical with T.
2232 bool
2233 is_identical(const Channel_type* t, bool errors_are_identical) const;
2234
2235 // Import a channel type.
2236 static Channel_type*
2237 do_import(Import*);
2238
2239 protected:
2240 int
2241 do_traverse(Traverse* traverse)
2242 { return Type::traverse(this->element_type_, traverse); }
2243
2244 bool
2245 do_has_pointer() const
2246 { return true; }
2247
2248 unsigned int
2249 do_hash_for_method(Gogo*) const;
2250
2251 bool
2252 do_check_make_expression(Expression_list*, source_location);
2253
2254 tree
2255 do_get_tree(Gogo*);
2256
2257 tree
2258 do_get_init_tree(Gogo*, tree, bool);
2259
2260 tree
2261 do_make_expression_tree(Translate_context*, Expression_list*,
2262 source_location);
2263
2264 Expression*
2265 do_type_descriptor(Gogo*, Named_type*);
2266
2267 void
2268 do_reflection(Gogo*, std::string*) const;
2269
2270 void
2271 do_mangled_name(Gogo*, std::string*) const;
2272
2273 void
2274 do_export(Export*) const;
2275
2276 private:
2277 static Type*
2278 make_chan_type_descriptor_type();
2279
2280 // Whether this channel can send data.
2281 bool may_send_;
2282 // Whether this channel can receive data.
2283 bool may_receive_;
2284 // The types of elements which may be sent on this channel. If this
2285 // is NULL, it means that any type may be sent.
2286 Type* element_type_;
2287 };
2288
2289 // An interface type.
2290
2291 class Interface_type : public Type
2292 {
2293 public:
2294 Interface_type(Typed_identifier_list* methods, source_location location)
2295 : Type(TYPE_INTERFACE),
2296 methods_(methods), location_(location)
2297 { gcc_assert(methods == NULL || !methods->empty()); }
2298
2299 // Return whether this is an empty interface.
2300 bool
2301 is_empty() const
2302 { return this->methods_ == NULL; }
2303
2304 // Return the list of methods. This will return NULL for an empty
2305 // interface.
2306 const Typed_identifier_list*
2307 methods() const
2308 { return this->methods_; }
2309
2310 // Return the number of methods.
2311 size_t
2312 method_count() const
2313 { return this->methods_ == NULL ? 0 : this->methods_->size(); }
2314
2315 // Return the method NAME, or NULL.
2316 const Typed_identifier*
2317 find_method(const std::string& name) const;
2318
2319 // Return the zero-based index of method NAME.
2320 size_t
2321 method_index(const std::string& name) const;
2322
2323 // Finalize the methods. This handles interface inheritance.
2324 void
2325 finalize_methods();
2326
2327 // Return true if T implements this interface. If this returns
2328 // false, and REASON is not NULL, it sets *REASON to the reason that
2329 // it fails.
2330 bool
2331 implements_interface(const Type* t, std::string* reason) const;
2332
2333 // Whether this type is identical with T. REASON is as in
2334 // implements_interface.
2335 bool
2336 is_identical(const Interface_type* t, bool errors_are_identical) const;
2337
2338 // Whether we can assign T to this type. is_identical is known to
2339 // be false.
2340 bool
2341 is_compatible_for_assign(const Interface_type*, std::string* reason) const;
2342
2343 // Return whether NAME is a method which is not exported. This is
2344 // only used for better error reporting.
2345 bool
2346 is_unexported_method(Gogo*, const std::string& name) const;
2347
2348 // Import an interface type.
2349 static Interface_type*
2350 do_import(Import*);
2351
2352 // Fill in the fields for a named interface type.
2353 tree
2354 fill_in_tree(Gogo*, tree);
2355
2356 protected:
2357 int
2358 do_traverse(Traverse*);
2359
2360 bool
2361 do_has_pointer() const
2362 { return true; }
2363
2364 unsigned int
2365 do_hash_for_method(Gogo*) const;
2366
2367 tree
2368 do_get_tree(Gogo*);
2369
2370 tree
2371 do_get_init_tree(Gogo* gogo, tree, bool);
2372
2373 Expression*
2374 do_type_descriptor(Gogo*, Named_type*);
2375
2376 void
2377 do_reflection(Gogo*, std::string*) const;
2378
2379 void
2380 do_mangled_name(Gogo*, std::string*) const;
2381
2382 void
2383 do_export(Export*) const;
2384
2385 private:
2386 static Type*
2387 make_interface_type_descriptor_type();
2388
2389 // The list of methods associated with the interface. This will be
2390 // NULL for the empty interface.
2391 Typed_identifier_list* methods_;
2392 // The location where the interface was defined.
2393 source_location location_;
2394 };
2395
2396 // The value we keep for a named type. This lets us get the right
2397 // name when we convert to trees. Note that we don't actually keep
2398 // the name here; the name is in the Named_object which points to
2399 // this. This object exists to hold a unique tree which represents
2400 // the type.
2401
2402 class Named_type : public Type
2403 {
2404 public:
2405 Named_type(Named_object* named_object, Type* type, source_location location)
2406 : Type(TYPE_NAMED),
2407 named_object_(named_object), in_function_(NULL), type_(type),
2408 local_methods_(NULL), all_methods_(NULL),
2409 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
2410 location_(location), named_tree_(NULL), is_visible_(true),
2411 is_error_(false), seen_(0)
2412 { }
2413
2414 // Return the associated Named_object. This holds the actual name.
2415 Named_object*
2416 named_object()
2417 { return this->named_object_; }
2418
2419 const Named_object*
2420 named_object() const
2421 { return this->named_object_; }
2422
2423 // Set the Named_object. This is used when we see a type
2424 // declaration followed by a type.
2425 void
2426 set_named_object(Named_object* no)
2427 { this->named_object_ = no; }
2428
2429 // Return the function in which this type is defined. This will
2430 // return NULL for a type defined in global scope.
2431 const Named_object*
2432 in_function() const
2433 { return this->in_function_; }
2434
2435 // Set the function in which this type is defined.
2436 void
2437 set_in_function(Named_object* f)
2438 { this->in_function_ = f; }
2439
2440 // Return the name of the type.
2441 const std::string&
2442 name() const;
2443
2444 // Return the name of the type for an error message. The difference
2445 // is that if the type is defined in a different package, this will
2446 // return PACKAGE.NAME.
2447 std::string
2448 message_name() const;
2449
2450 // Return the underlying type.
2451 Type*
2452 real_type()
2453 { return this->type_; }
2454
2455 const Type*
2456 real_type() const
2457 { return this->type_; }
2458
2459 // Return the location.
2460 source_location
2461 location() const
2462 { return this->location_; }
2463
2464 // Whether this type is visible. This only matters when parsing.
2465 bool
2466 is_visible() const
2467 { return this->is_visible_; }
2468
2469 // Mark this type as visible.
2470 void
2471 set_is_visible()
2472 { this->is_visible_ = true; }
2473
2474 // Mark this type as invisible.
2475 void
2476 clear_is_visible()
2477 { this->is_visible_ = false; }
2478
2479 // Whether this is a builtin type.
2480 bool
2481 is_builtin() const
2482 { return this->location_ == BUILTINS_LOCATION; }
2483
2484 // Return the base type for this type.
2485 Type*
2486 named_base();
2487
2488 const Type*
2489 named_base() const;
2490
2491 // Return whether this is an error type.
2492 bool
2493 is_named_error_type() const;
2494
2495 // Add a method to this type.
2496 Named_object*
2497 add_method(const std::string& name, Function*);
2498
2499 // Add a method declaration to this type.
2500 Named_object*
2501 add_method_declaration(const std::string& name, Package* package,
2502 Function_type* type, source_location location);
2503
2504 // Add an existing method--one defined before the type itself was
2505 // defined--to a type.
2506 void
2507 add_existing_method(Named_object*);
2508
2509 // Look up a local method.
2510 Named_object*
2511 find_local_method(const std::string& name) const;
2512
2513 // Return the list of local methods.
2514 const Bindings*
2515 local_methods() const
2516 { return this->local_methods_; }
2517
2518 // Build the complete list of methods, including those from
2519 // anonymous fields, and build method stubs if needed.
2520 void
2521 finalize_methods(Gogo*);
2522
2523 // Return whether this type has any methods. This should only be
2524 // called after the finalize_methods pass.
2525 bool
2526 has_any_methods() const
2527 { return this->all_methods_ != NULL; }
2528
2529 // Return the methods for this type. This should only be called
2530 // after the finalized_methods pass.
2531 const Methods*
2532 methods() const
2533 { return this->all_methods_; }
2534
2535 // Return the method to use for NAME. This returns NULL if there is
2536 // no such method or if the method is ambiguous. When it returns
2537 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2538 Method*
2539 method_function(const std::string& name, bool *is_ambiguous) const;
2540
2541 // Return whether NAME is a known field or method which is not
2542 // exported. This is only used for better error reporting.
2543 bool
2544 is_unexported_local_method(Gogo*, const std::string& name) const;
2545
2546 // Return a pointer to the interface method table for this type for
2547 // the interface INTERFACE. If IS_POINTER is true, set the type
2548 // descriptor to a pointer to this type, otherwise set it to this
2549 // type.
2550 tree
2551 interface_method_table(Gogo*, const Interface_type* interface,
2552 bool is_pointer);
2553
2554 // Whether this type has any hidden fields.
2555 bool
2556 named_type_has_hidden_fields(std::string* reason) const;
2557
2558 // Export the type.
2559 void
2560 export_named_type(Export*, const std::string& name) const;
2561
2562 // Import a named type.
2563 static void
2564 import_named_type(Import*, Named_type**);
2565
2566 protected:
2567 int
2568 do_traverse(Traverse* traverse)
2569 { return Type::traverse(this->type_, traverse); }
2570
2571 bool
2572 do_verify();
2573
2574 bool
2575 do_has_pointer() const;
2576
2577 unsigned int
2578 do_hash_for_method(Gogo*) const;
2579
2580 bool
2581 do_check_make_expression(Expression_list* args, source_location location)
2582 { return this->type_->check_make_expression(args, location); }
2583
2584 tree
2585 do_get_tree(Gogo*);
2586
2587 tree
2588 do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
2589 { return this->type_->get_typed_init_tree(gogo, type_tree, is_clear); }
2590
2591 tree
2592 do_make_expression_tree(Translate_context* context, Expression_list* args,
2593 source_location location)
2594 { return this->type_->make_expression_tree(context, args, location); }
2595
2596 Expression*
2597 do_type_descriptor(Gogo*, Named_type*);
2598
2599 void
2600 do_reflection(Gogo*, std::string*) const;
2601
2602 void
2603 do_mangled_name(Gogo*, std::string* ret) const;
2604
2605 void
2606 do_export(Export*) const;
2607
2608 private:
2609 // A mapping from interfaces to the associated interface method
2610 // tables for this type. This maps to a decl.
2611 typedef Unordered_map_hash(const Interface_type*, tree, Type_hash_identical,
2612 Type_identical) Interface_method_tables;
2613
2614 // A pointer back to the Named_object for this type.
2615 Named_object* named_object_;
2616 // If this type is defined in a function, a pointer back to the
2617 // function in which it is defined.
2618 Named_object* in_function_;
2619 // The actual type.
2620 Type* type_;
2621 // The list of methods defined for this type. Any named type can
2622 // have methods.
2623 Bindings* local_methods_;
2624 // The full list of methods for this type, including methods
2625 // declared for anonymous fields.
2626 Methods* all_methods_;
2627 // A mapping from interfaces to the associated interface method
2628 // tables for this type.
2629 Interface_method_tables* interface_method_tables_;
2630 // A mapping from interfaces to the associated interface method
2631 // tables for pointers to this type.
2632 Interface_method_tables* pointer_interface_method_tables_;
2633 // The location where this type was defined.
2634 source_location location_;
2635 // The tree for this type while converting to GENERIC. This is used
2636 // to avoid endless recursion when a named type refers to itself.
2637 tree named_tree_;
2638 // Whether this type is visible. This is false if this type was
2639 // created because it was referenced by an imported object, but the
2640 // type itself was not exported. This will always be true for types
2641 // created in the current package.
2642 bool is_visible_;
2643 // Whether this type is erroneous.
2644 bool is_error_;
2645 // In a recursive operation such as has_hidden_fields, this flag is
2646 // used to prevent infinite recursion when a type refers to itself.
2647 // This is mutable because it is always reset to false when the
2648 // function exits.
2649 mutable int seen_;
2650 };
2651
2652 // A forward declaration. This handles a type which has been declared
2653 // but not defined.
2654
2655 class Forward_declaration_type : public Type
2656 {
2657 public:
2658 Forward_declaration_type(Named_object* named_object);
2659
2660 // The named object associated with this type declaration. This
2661 // will be resolved.
2662 Named_object*
2663 named_object();
2664
2665 const Named_object*
2666 named_object() const;
2667
2668 // Return the name of the type.
2669 const std::string&
2670 name() const;
2671
2672 // Return the type to which this points. Give an error if the type
2673 // has not yet been defined.
2674 Type*
2675 real_type();
2676
2677 const Type*
2678 real_type() const;
2679
2680 // Whether the base type has been defined.
2681 bool
2682 is_defined() const;
2683
2684 // Add a method to this type.
2685 Named_object*
2686 add_method(const std::string& name, Function*);
2687
2688 // Add a method declaration to this type.
2689 Named_object*
2690 add_method_declaration(const std::string& name, Function_type*,
2691 source_location);
2692
2693 protected:
2694 int
2695 do_traverse(Traverse* traverse);
2696
2697 bool
2698 do_has_pointer() const
2699 { return this->real_type()->has_pointer(); }
2700
2701 unsigned int
2702 do_hash_for_method(Gogo* gogo) const
2703 { return this->real_type()->hash_for_method(gogo); }
2704
2705 bool
2706 do_check_make_expression(Expression_list* args, source_location location)
2707 { return this->base()->check_make_expression(args, location); }
2708
2709 tree
2710 do_get_tree(Gogo* gogo);
2711
2712 tree
2713 do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
2714 { return this->base()->get_typed_init_tree(gogo, type_tree, is_clear); }
2715
2716 tree
2717 do_make_expression_tree(Translate_context* context, Expression_list* args,
2718 source_location location)
2719 { return this->base()->make_expression_tree(context, args, location); }
2720
2721 Expression*
2722 do_type_descriptor(Gogo*, Named_type*);
2723
2724 void
2725 do_reflection(Gogo*, std::string*) const;
2726
2727 void
2728 do_mangled_name(Gogo*, std::string* ret) const;
2729
2730 void
2731 do_export(Export*) const;
2732
2733 private:
2734 // Issue a warning about a use of an undefined type.
2735 void
2736 warn() const;
2737
2738 // The type declaration.
2739 Named_object* named_object_;
2740 // Whether we have issued a warning about this type.
2741 mutable bool warned_;
2742 };
2743
2744 // The Type_context struct describes what we expect for the type of an
2745 // expression.
2746
2747 struct Type_context
2748 {
2749 // The exact type we expect, if known. This may be NULL.
2750 Type* type;
2751 // Whether an abstract type is permitted.
2752 bool may_be_abstract;
2753
2754 // Constructors.
2755 Type_context()
2756 : type(NULL), may_be_abstract(false)
2757 { }
2758
2759 Type_context(Type* a_type, bool a_may_be_abstract)
2760 : type(a_type), may_be_abstract(a_may_be_abstract)
2761 { }
2762 };
2763
2764 #endif // !defined(GO_TYPES_H)