compiler: look through aliases when looking for methods
[official-gcc.git] / gcc / go / gofrontend / types.h
blob2703319554f7ffd129c20b4635d44e2dff2e0e7c
1 // types.h -- Go frontend types. -*- C++ -*-
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.
7 #ifndef GO_TYPES_H
8 #define GO_TYPES_H
10 #include <ostream>
12 #include "go-linemap.h"
13 #include "escape.h"
15 class Gogo;
16 class Package;
17 class Variable;
18 class Traverse;
19 class Typed_identifier;
20 class Typed_identifier_list;
21 class Integer_type;
22 class Float_type;
23 class Complex_type;
24 class String_type;
25 class Function_type;
26 class Backend_function_type;
27 class Struct_field;
28 class Struct_field_list;
29 class Struct_type;
30 class Pointer_type;
31 class Array_type;
32 class Map_type;
33 class Channel_type;
34 class Interface_type;
35 class Named_type;
36 class Forward_declaration_type;
37 class Method;
38 class Methods;
39 class Type_hash_identical;
40 class Type_identical;
41 class Expression;
42 class Expression_list;
43 class Call_expression;
44 class Field_reference_expression;
45 class Bound_method_expression;
46 class Bindings;
47 class Named_object;
48 class Function;
49 class Translate_context;
50 class Export;
51 class Import;
52 class Btype;
53 class Bexpression;
54 class Bvariable;
56 // Type codes used in type descriptors. These must match the values
57 // in libgo/runtime/go-type.h. They also match the values in the gc
58 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
59 // although this is not required.
61 static const int RUNTIME_TYPE_KIND_BOOL = 1;
62 static const int RUNTIME_TYPE_KIND_INT = 2;
63 static const int RUNTIME_TYPE_KIND_INT8 = 3;
64 static const int RUNTIME_TYPE_KIND_INT16 = 4;
65 static const int RUNTIME_TYPE_KIND_INT32 = 5;
66 static const int RUNTIME_TYPE_KIND_INT64 = 6;
67 static const int RUNTIME_TYPE_KIND_UINT = 7;
68 static const int RUNTIME_TYPE_KIND_UINT8 = 8;
69 static const int RUNTIME_TYPE_KIND_UINT16 = 9;
70 static const int RUNTIME_TYPE_KIND_UINT32 = 10;
71 static const int RUNTIME_TYPE_KIND_UINT64 = 11;
72 static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
73 static const int RUNTIME_TYPE_KIND_FLOAT32 = 13;
74 static const int RUNTIME_TYPE_KIND_FLOAT64 = 14;
75 static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15;
76 static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16;
77 static const int RUNTIME_TYPE_KIND_ARRAY = 17;
78 static const int RUNTIME_TYPE_KIND_CHAN = 18;
79 static const int RUNTIME_TYPE_KIND_FUNC = 19;
80 static const int RUNTIME_TYPE_KIND_INTERFACE = 20;
81 static const int RUNTIME_TYPE_KIND_MAP = 21;
82 static const int RUNTIME_TYPE_KIND_PTR = 22;
83 static const int RUNTIME_TYPE_KIND_SLICE = 23;
84 static const int RUNTIME_TYPE_KIND_STRING = 24;
85 static const int RUNTIME_TYPE_KIND_STRUCT = 25;
86 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26;
88 static const int RUNTIME_TYPE_KIND_DIRECT_IFACE = (1 << 5);
89 static const int RUNTIME_TYPE_KIND_GC_PROG = (1 << 6);
90 static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7);
92 // To build the complete list of methods for a named type we need to
93 // gather all methods from anonymous fields. Those methods may
94 // require an arbitrary set of indirections and field offsets. There
95 // is also the possibility of ambiguous methods, which we could ignore
96 // except that we want to give a better error message for that case.
97 // This is a base class. There are two types of methods: named
98 // methods, and methods which are inherited from an anonymous field of
99 // interface type.
101 class Method
103 public:
104 // For methods in anonymous types we need to know the sequence of
105 // field references used to extract the pointer to pass to the
106 // method. Since each method for a particular anonymous field will
107 // have the sequence of field indexes, and since the indexes can be
108 // shared going down the chain, we use a manually managed linked
109 // list. The first entry in the list is the field index for the
110 // last field, the one passed to the method.
112 struct Field_indexes
114 const Field_indexes* next;
115 unsigned int field_index;
118 virtual ~Method()
121 // Get the list of field indexes.
122 const Field_indexes*
123 field_indexes() const
124 { return this->field_indexes_; }
126 // Get the depth.
127 unsigned int
128 depth() const
129 { return this->depth_; }
131 // Return whether this is a value method--a method which does not
132 // require a pointer expression.
133 bool
134 is_value_method() const
135 { return this->is_value_method_; }
137 // Return whether we need a stub method--this is true if we can't
138 // just pass the main object to the method.
139 bool
140 needs_stub_method() const
141 { return this->needs_stub_method_; }
143 // Return whether this is an ambiguous method name.
144 bool
145 is_ambiguous() const
146 { return this->is_ambiguous_; }
148 // Note that this method is ambiguous.
149 void
150 set_is_ambiguous()
151 { this->is_ambiguous_ = true; }
153 // Return the type of the method.
154 Function_type*
155 type() const
156 { return this->do_type(); }
158 // Return the location of the method receiver.
159 Location
160 receiver_location() const
161 { return this->do_receiver_location(); }
163 // Return an expression which binds this method to EXPR. This is
164 // something which can be used with a function call.
165 Expression*
166 bind_method(Expression* expr, Location location) const;
168 // Return the named object for this method. This may only be called
169 // after methods are finalized.
170 Named_object*
171 named_object() const;
173 // Get the stub object.
174 Named_object*
175 stub_object() const
177 go_assert(this->stub_ != NULL);
178 return this->stub_;
181 // Set the stub object.
182 void
183 set_stub_object(Named_object* no)
185 go_assert(this->stub_ == NULL);
186 this->stub_ = no;
189 // Return true if this method should not participate in any
190 // interfaces.
191 bool
192 nointerface() const
193 { return this->do_nointerface(); }
195 protected:
196 // These objects are only built by the child classes.
197 Method(const Field_indexes* field_indexes, unsigned int depth,
198 bool is_value_method, bool needs_stub_method)
199 : field_indexes_(field_indexes), depth_(depth), stub_(NULL),
200 is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
201 is_ambiguous_(false)
204 // The named object for this method.
205 virtual Named_object*
206 do_named_object() const = 0;
208 // The type of the method.
209 virtual Function_type*
210 do_type() const = 0;
212 // Return the location of the method receiver.
213 virtual Location
214 do_receiver_location() const = 0;
216 // Bind a method to an object.
217 virtual Expression*
218 do_bind_method(Expression* expr, Location location) const = 0;
220 // Return whether this method should not participate in interfaces.
221 virtual bool
222 do_nointerface() const = 0;
224 private:
225 // The sequence of field indexes used for this method. If this is
226 // NULL, then the method is defined for the current type.
227 const Field_indexes* field_indexes_;
228 // The depth at which this method was found.
229 unsigned int depth_;
230 // If a stub method is required, this is its object. This is only
231 // set after stub methods are built in finalize_methods.
232 Named_object* stub_;
233 // Whether this is a value method--a method that does not require a
234 // pointer.
235 bool is_value_method_;
236 // Whether a stub method is required.
237 bool needs_stub_method_;
238 // Whether this method is ambiguous.
239 bool is_ambiguous_;
242 // A named method. This is what you get with a method declaration,
243 // either directly on the type, or inherited from some anonymous
244 // embedded field.
246 class Named_method : public Method
248 public:
249 Named_method(Named_object* named_object, const Field_indexes* field_indexes,
250 unsigned int depth, bool is_value_method,
251 bool needs_stub_method)
252 : Method(field_indexes, depth, is_value_method, needs_stub_method),
253 named_object_(named_object)
256 protected:
257 // Get the Named_object for the method.
258 Named_object*
259 do_named_object() const
260 { return this->named_object_; }
262 // The type of the method.
263 Function_type*
264 do_type() const;
266 // Return the location of the method receiver.
267 Location
268 do_receiver_location() const;
270 // Bind a method to an object.
271 Expression*
272 do_bind_method(Expression* expr, Location location) const;
274 // Return whether this method should not participate in interfaces.
275 bool
276 do_nointerface() const;
278 private:
279 // The method itself. For a method which needs a stub, this starts
280 // out as the underlying method, and is later replaced with the stub
281 // method.
282 Named_object* named_object_;
285 // An interface method. This is used when an interface appears as an
286 // anonymous field in a named struct.
288 class Interface_method : public Method
290 public:
291 Interface_method(const std::string& name, Location location,
292 Function_type* fntype, const Field_indexes* field_indexes,
293 unsigned int depth)
294 : Method(field_indexes, depth, true, true),
295 name_(name), location_(location), fntype_(fntype)
298 protected:
299 // Get the Named_object for the method. This should never be
300 // called, as we always create a stub.
301 Named_object*
302 do_named_object() const
303 { go_unreachable(); }
305 // The type of the method.
306 Function_type*
307 do_type() const
308 { return this->fntype_; }
310 // Return the location of the method receiver.
311 Location
312 do_receiver_location() const
313 { return this->location_; }
315 // Bind a method to an object.
316 Expression*
317 do_bind_method(Expression* expr, Location location) const;
319 // Return whether this method should not participate in interfaces.
320 bool
321 do_nointerface() const
322 { return false; }
324 private:
325 // The name of the interface method to call.
326 std::string name_;
327 // The location of the definition of the interface method.
328 Location location_;
329 // The type of the interface method.
330 Function_type* fntype_;
333 // A mapping from method name to Method. This is a wrapper around a
334 // hash table.
336 class Methods
338 private:
339 typedef Unordered_map(std::string, Method*) Method_map;
341 public:
342 typedef Method_map::const_iterator const_iterator;
344 Methods()
345 : methods_()
348 // Insert a new method. Returns true if it was inserted, false if
349 // it was overidden or ambiguous.
350 bool
351 insert(const std::string& name, Method* m);
353 // The number of (unambiguous) methods.
354 size_t
355 count() const;
357 // Iterate.
358 const_iterator
359 begin() const
360 { return this->methods_.begin(); }
362 const_iterator
363 end() const
364 { return this->methods_.end(); }
366 // Lookup.
367 const_iterator
368 find(const std::string& name) const
369 { return this->methods_.find(name); }
371 bool
372 empty() const
373 { return this->methods_.empty(); }
375 private:
376 Method_map methods_;
379 // The base class for all types.
381 class Type
383 public:
384 // The types of types.
385 enum Type_classification
387 TYPE_ERROR,
388 TYPE_VOID,
389 TYPE_BOOLEAN,
390 TYPE_INTEGER,
391 TYPE_FLOAT,
392 TYPE_COMPLEX,
393 TYPE_STRING,
394 TYPE_SINK,
395 TYPE_FUNCTION,
396 TYPE_POINTER,
397 TYPE_NIL,
398 TYPE_CALL_MULTIPLE_RESULT,
399 TYPE_STRUCT,
400 TYPE_ARRAY,
401 TYPE_MAP,
402 TYPE_CHANNEL,
403 TYPE_INTERFACE,
404 TYPE_NAMED,
405 TYPE_FORWARD
408 virtual ~Type();
410 // Creators.
412 static Type*
413 make_error_type();
415 static Type*
416 make_void_type();
418 // Get the unnamed bool type.
419 static Type*
420 make_boolean_type();
422 // Get the named type "bool".
423 static Named_type*
424 lookup_bool_type();
426 // Make the named type "bool".
427 static Named_type*
428 make_named_bool_type();
430 // Make an abstract integer type.
431 static Integer_type*
432 make_abstract_integer_type();
434 // Make an abstract type for a character constant.
435 static Integer_type*
436 make_abstract_character_type();
438 // Make a named integer type with a specified size.
439 // RUNTIME_TYPE_KIND is the code to use in reflection information,
440 // to distinguish int and int32.
441 static Named_type*
442 make_integer_type(const char* name, bool is_unsigned, int bits,
443 int runtime_type_kind);
445 // Look up a named integer type.
446 static Named_type*
447 lookup_integer_type(const char* name);
449 // Make an abstract floating point type.
450 static Float_type*
451 make_abstract_float_type();
453 // Make a named floating point type with a specific size.
454 // RUNTIME_TYPE_KIND is the code to use in reflection information,
455 // to distinguish float and float32.
456 static Named_type*
457 make_float_type(const char* name, int bits, int runtime_type_kind);
459 // Look up a named float type.
460 static Named_type*
461 lookup_float_type(const char* name);
463 // Make an abstract complex type.
464 static Complex_type*
465 make_abstract_complex_type();
467 // Make a named complex type with a specific size.
468 // RUNTIME_TYPE_KIND is the code to use in reflection information,
469 // to distinguish complex and complex64.
470 static Named_type*
471 make_complex_type(const char* name, int bits, int runtime_type_kind);
473 // Look up a named complex type.
474 static Named_type*
475 lookup_complex_type(const char* name);
477 // Get the unnamed string type.
478 static Type*
479 make_string_type();
481 // Get the named type "string".
482 static Named_type*
483 lookup_string_type();
485 // Make the named type "string".
486 static Named_type*
487 make_named_string_type();
489 static Type*
490 make_sink_type();
492 static Function_type*
493 make_function_type(Typed_identifier* receiver,
494 Typed_identifier_list* parameters,
495 Typed_identifier_list* results,
496 Location);
498 static Backend_function_type*
499 make_backend_function_type(Typed_identifier* receiver,
500 Typed_identifier_list* parameters,
501 Typed_identifier_list* results,
502 Location);
504 static Pointer_type*
505 make_pointer_type(Type*);
507 static void
508 finish_pointer_types(Gogo* gogo);
510 static Type*
511 make_nil_type();
513 static Type*
514 make_call_multiple_result_type(Call_expression*);
516 static Struct_type*
517 make_struct_type(Struct_field_list* fields, Location);
519 static Array_type*
520 make_array_type(Type* element_type, Expression* length);
522 static Map_type*
523 make_map_type(Type* key_type, Type* value_type, Location);
525 static Channel_type*
526 make_channel_type(bool send, bool receive, Type*);
528 static Interface_type*
529 make_interface_type(Typed_identifier_list* methods, Location);
531 static Interface_type*
532 make_empty_interface_type(Location);
534 static Type*
535 make_type_descriptor_type();
537 static Type*
538 make_type_descriptor_ptr_type();
540 static Named_type*
541 make_named_type(Named_object*, Type*, Location);
543 static Type*
544 make_forward_declaration(Named_object*);
546 // Make a builtin struct type from a list of fields.
547 static Struct_type*
548 make_builtin_struct_type(int nfields, ...);
550 // Make a builtin named type.
551 static Named_type*
552 make_builtin_named_type(const char* name, Type* type);
554 // Traverse a type.
555 static int
556 traverse(Type*, Traverse*);
558 // Verify the type. This is called after parsing, and verifies that
559 // types are complete and meet the language requirements. This
560 // returns false if the type is invalid and we should not continue
561 // traversing it.
562 bool
563 verify()
564 { return this->do_verify(); }
566 // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
567 // returns that an erroneous type is identical to any other type;
568 // this is used to avoid cascading errors. If this returns false,
569 // and REASON is not NULL, it may set *REASON.
570 static bool
571 are_identical(const Type* lhs, const Type* rhs, bool errors_are_identical,
572 std::string* reason);
574 // An argument to are_identical_cmp_tags, indicating whether or not
575 // to compare struct field tags.
576 enum Cmp_tags {
577 COMPARE_TAGS,
578 IGNORE_TAGS
581 // Return true if two types are identical. This is like the
582 // are_identical function, but also takes a CMP_TAGS argument
583 // indicating whether to compare struct tags. Otherwise the
584 // parameters are as for are_identical.
585 static bool
586 are_identical_cmp_tags(const Type* lhs, const Type* rhs,
587 Cmp_tags, bool errors_are_identical,
588 std::string* reason);
590 // Return true if two types are compatible for use in a binary
591 // operation, other than a shift, comparison, or channel send. This
592 // is an equivalence relation.
593 static bool
594 are_compatible_for_binop(const Type* t1, const Type* t2);
596 // Return true if two types are compatible for use with the
597 // comparison operator. IS_EQUALITY_OP is true if this is an
598 // equality comparison, false if it is an ordered comparison. This
599 // is an equivalence relation. If this returns false, and REASON is
600 // not NULL, it sets *REASON.
601 static bool
602 are_compatible_for_comparison(bool is_equality_op, const Type *t1,
603 const Type *t2, std::string* reason);
605 // Return true if a type is comparable with itself. This is true of
606 // most types, but false for, e.g., function types.
607 bool
608 is_comparable() const
609 { return Type::are_compatible_for_comparison(true, this, this, NULL); }
611 // Return true if a value with type RHS is assignable to a variable
612 // with type LHS. This is not an equivalence relation. If this
613 // returns false, and REASON is not NULL, it sets *REASON.
614 static bool
615 are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
617 // Return true if a value with type RHS may be converted to type
618 // LHS. If this returns false, and REASON is not NULL, it sets
619 // *REASON.
620 static bool
621 are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
623 // Return true if values of this type can be compared using an
624 // identity function which gets nothing but a pointer to the value
625 // and a size.
626 bool
627 compare_is_identity(Gogo* gogo)
628 { return this->do_compare_is_identity(gogo); }
630 // Return whether values of this type are reflexive: if a comparison
631 // of a value with itself always returns true.
632 bool
633 is_reflexive()
634 { return this->do_is_reflexive(); }
636 // Return whether values of this, when used as a key in map,
637 // requires the key to be updated when an assignment is made.
638 bool
639 needs_key_update()
640 { return this->do_needs_key_update(); }
642 // Whether the type is permitted in the heap.
643 bool
644 in_heap()
645 { return this->do_in_heap(); }
647 // Return a hash code for this type for the method hash table.
648 // Types which are equivalent according to are_identical will have
649 // the same hash code.
650 unsigned int
651 hash_for_method(Gogo*) const;
653 // Return the type classification.
654 Type_classification
655 classification() const
656 { return this->classification_; }
658 // Return the base type for this type. This looks through forward
659 // declarations and names. Using this with a forward declaration
660 // which has not been defined will return an error type.
661 Type*
662 base();
664 const Type*
665 base() const;
667 // Return the type skipping defined forward declarations. If this
668 // type is a forward declaration which has not been defined, it will
669 // return the Forward_declaration_type. This differs from base() in
670 // that it will return a Named_type, and for a
671 // Forward_declaration_type which is not defined it will return that
672 // type rather than an error type.
673 Type*
674 forwarded();
676 const Type*
677 forwarded() const;
679 // Return the type skipping any alias definitions and any defined
680 // forward declarations. This is like forwarded, but also
681 // recursively expands alias definitions to the aliased type.
682 Type*
683 unalias();
685 const Type*
686 unalias() const;
688 // Return true if this is a basic type: a type which is not composed
689 // of other types, and is not void.
690 bool
691 is_basic_type() const;
693 // Return true if this is an abstract type--an integer, floating
694 // point, or complex type whose size has not been determined.
695 bool
696 is_abstract() const;
698 // Return a non-abstract version of an abstract type.
699 Type*
700 make_non_abstract_type();
702 // Return true if this type is or contains a pointer. This
703 // determines whether the garbage collector needs to look at a value
704 // of this type.
705 bool
706 has_pointer() const
707 { return this->do_has_pointer(); }
709 // Return true if this is the error type. This returns false for a
710 // type which is not defined, as it is called by the parser before
711 // all types are defined.
712 bool
713 is_error_type() const;
715 // Return true if this is the error type or if the type is
716 // undefined. If the type is undefined, this will give an error.
717 // This should only be called after parsing is complete.
718 bool
719 is_error() const
720 { return this->base()->is_error_type(); }
722 // Return true if this is a void type.
723 bool
724 is_void_type() const
725 { return this->classification_ == TYPE_VOID; }
727 // If this is an integer type, return the Integer_type. Otherwise,
728 // return NULL. This is a controlled dynamic_cast.
729 Integer_type*
730 integer_type()
731 { return this->convert<Integer_type, TYPE_INTEGER>(); }
733 const Integer_type*
734 integer_type() const
735 { return this->convert<const Integer_type, TYPE_INTEGER>(); }
737 // If this is a floating point type, return the Float_type.
738 // Otherwise, return NULL. This is a controlled dynamic_cast.
739 Float_type*
740 float_type()
741 { return this->convert<Float_type, TYPE_FLOAT>(); }
743 const Float_type*
744 float_type() const
745 { return this->convert<const Float_type, TYPE_FLOAT>(); }
747 // If this is a complex type, return the Complex_type. Otherwise,
748 // return NULL.
749 Complex_type*
750 complex_type()
751 { return this->convert<Complex_type, TYPE_COMPLEX>(); }
753 const Complex_type*
754 complex_type() const
755 { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
757 // Return whether this is a numeric type.
758 bool
759 is_numeric_type() const
761 Type_classification tc = this->base()->classification_;
762 return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX;
765 // Return true if this is a boolean type.
766 bool
767 is_boolean_type() const
768 { return this->base()->classification_ == TYPE_BOOLEAN; }
770 // Return true if this is an abstract boolean type.
771 bool
772 is_abstract_boolean_type() const
773 { return this->classification_ == TYPE_BOOLEAN; }
775 // Return true if this is a string type.
776 bool
777 is_string_type() const
778 { return this->base()->classification_ == TYPE_STRING; }
780 // Return true if this is an abstract string type.
781 bool
782 is_abstract_string_type() const
783 { return this->classification_ == TYPE_STRING; }
785 // Return true if this is the sink type. This is the type of the
786 // blank identifier _.
787 bool
788 is_sink_type() const
789 { return this->base()->classification_ == TYPE_SINK; }
791 // If this is a function type, return it. Otherwise, return NULL.
792 Function_type*
793 function_type()
794 { return this->convert<Function_type, TYPE_FUNCTION>(); }
796 const Function_type*
797 function_type() const
798 { return this->convert<const Function_type, TYPE_FUNCTION>(); }
800 // If this is a pointer type, return the type to which it points.
801 // Otherwise, return NULL.
802 Type*
803 points_to() const;
805 // If this is a pointer type, return the type to which it points.
806 // Otherwise, return the type itself.
807 Type*
808 deref()
810 Type* pt = this->points_to();
811 return pt != NULL ? pt : this;
814 const Type*
815 deref() const
817 const Type* pt = this->points_to();
818 return pt != NULL ? pt : this;
821 // Return true if this is the nil type. We don't use base() here,
822 // because this can be called during parse, and there is no way to
823 // name the nil type anyhow.
824 bool
825 is_nil_type() const
826 { return this->classification_ == TYPE_NIL; }
828 // Return true if this is the predeclared constant nil being used as
829 // a type. This is what the parser produces for type switches which
830 // use "case nil".
831 bool
832 is_nil_constant_as_type() const;
834 // Return true if this is the return type of a function which
835 // returns multiple values.
836 bool
837 is_call_multiple_result_type() const
838 { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
840 // If this is a struct type, return it. Otherwise, return NULL.
841 Struct_type*
842 struct_type()
843 { return this->convert<Struct_type, TYPE_STRUCT>(); }
845 const Struct_type*
846 struct_type() const
847 { return this->convert<const Struct_type, TYPE_STRUCT>(); }
849 // If this is an array type, return it. Otherwise, return NULL.
850 Array_type*
851 array_type()
852 { return this->convert<Array_type, TYPE_ARRAY>(); }
854 const Array_type*
855 array_type() const
856 { return this->convert<const Array_type, TYPE_ARRAY>(); }
858 // Return whether if this is a slice type.
859 bool
860 is_slice_type() const;
862 // If this is a map type, return it. Otherwise, return NULL.
863 Map_type*
864 map_type()
865 { return this->convert<Map_type, TYPE_MAP>(); }
867 const Map_type*
868 map_type() const
869 { return this->convert<const Map_type, TYPE_MAP>(); }
871 // If this is a channel type, return it. Otherwise, return NULL.
872 Channel_type*
873 channel_type()
874 { return this->convert<Channel_type, TYPE_CHANNEL>(); }
876 const Channel_type*
877 channel_type() const
878 { return this->convert<const Channel_type, TYPE_CHANNEL>(); }
880 // If this is an interface type, return it. Otherwise, return NULL.
881 Interface_type*
882 interface_type()
883 { return this->convert<Interface_type, TYPE_INTERFACE>(); }
885 const Interface_type*
886 interface_type() const
887 { return this->convert<const Interface_type, TYPE_INTERFACE>(); }
889 // If this is a named type, return it. Otherwise, return NULL.
890 Named_type*
891 named_type();
893 const Named_type*
894 named_type() const;
896 // If this is a forward declaration, return it. Otherwise, return
897 // NULL.
898 Forward_declaration_type*
899 forward_declaration_type()
900 { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
902 const Forward_declaration_type*
903 forward_declaration_type() const
905 return this->convert_no_base<const Forward_declaration_type,
906 TYPE_FORWARD>();
909 // Return true if this type is not yet defined.
910 bool
911 is_undefined() const;
913 // Return true if this is the unsafe.pointer type. We currently
914 // represent that as pointer-to-void.
915 bool
916 is_unsafe_pointer_type() const
917 { return this->points_to() != NULL && this->points_to()->is_void_type(); }
919 // Look for field or method NAME for TYPE. Return an expression for
920 // it, bound to EXPR.
921 static Expression*
922 bind_field_or_method(Gogo*, const Type* type, Expression* expr,
923 const std::string& name, Location);
925 // Return true if NAME is an unexported field or method of TYPE.
926 static bool
927 is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
928 std::vector<const Named_type*>*);
930 // Convert the builtin named types.
931 static void
932 convert_builtin_named_types(Gogo*);
934 // Return the backend representation of this type.
935 Btype*
936 get_backend(Gogo*);
938 // Return a placeholder for the backend representation of the type.
939 // This will return a type of the correct size, but for which some
940 // of the fields may still need to be completed.
941 Btype*
942 get_backend_placeholder(Gogo*);
944 // Finish the backend representation of a placeholder.
945 void
946 finish_backend(Gogo*, Btype*);
948 // Build a type descriptor entry for this type. Return a pointer to
949 // it. The location is the location which causes us to need the
950 // entry.
951 Bexpression*
952 type_descriptor_pointer(Gogo* gogo, Location);
954 // Build the Garbage Collection symbol for this type. Return a pointer to it.
955 Bexpression*
956 gc_symbol_pointer(Gogo* gogo);
958 // Return whether this type needs a garbage collection program.
959 // Sets *PTRSIZE and *PTRDATA.
960 bool
961 needs_gcprog(Gogo*, int64_t* ptrsize, int64_t* ptrdata);
963 // Return a ptrmask variable for this type.
964 Bvariable*
965 gc_ptrmask_var(Gogo*, int64_t ptrsize, int64_t ptrdata);
967 // Return the type reflection string for this type.
968 std::string
969 reflection(Gogo*) const;
971 // Return a mangled name for the type. This is a name which can be
972 // used in assembler code. Identical types should have the same
973 // manged name.
974 std::string
975 mangled_name(Gogo*) const;
977 // If the size of the type can be determined, set *PSIZE to the size
978 // in bytes and return true. Otherwise, return false. This queries
979 // the backend.
980 bool
981 backend_type_size(Gogo*, int64_t* psize);
983 // If the alignment of the type can be determined, set *PALIGN to
984 // the alignment in bytes and return true. Otherwise, return false.
985 bool
986 backend_type_align(Gogo*, int64_t* palign);
988 // If the alignment of a struct field of this type can be
989 // determined, set *PALIGN to the alignment in bytes and return
990 // true. Otherwise, return false.
991 bool
992 backend_type_field_align(Gogo*, int64_t* palign);
994 // Determine the ptrdata size for the backend version of this type:
995 // the length of the prefix of the type that can contain a pointer
996 // value. If it can be determined, set *PPTRDATA to the value in
997 // bytes and return true. Otherwise, return false.
998 bool
999 backend_type_ptrdata(Gogo*, int64_t* pptrdata);
1001 // Determine the ptrdata size that we are going to set in the type
1002 // descriptor. This is normally the same as backend_type_ptrdata,
1003 // but differs if we use a gcprog for an array. The arguments and
1004 // results are as for backend_type_ptrdata.
1005 bool
1006 descriptor_ptrdata(Gogo*, int64_t* pptrdata);
1008 // Whether the backend size is known.
1009 bool
1010 is_backend_type_size_known(Gogo*);
1012 // Return whether the type needs specially built type functions.
1013 bool
1014 needs_specific_type_functions(Gogo*);
1016 // Get the hash and equality functions for a type.
1017 void
1018 type_functions(Gogo*, Named_type* name, Function_type* hash_fntype,
1019 Function_type* equal_fntype, Named_object** hash_fn,
1020 Named_object** equal_fn);
1022 // Write the hash and equality type functions.
1023 void
1024 write_specific_type_functions(Gogo*, Named_type*, int64_t size,
1025 const std::string& hash_name,
1026 Function_type* hash_fntype,
1027 const std::string& equal_name,
1028 Function_type* equal_fntype);
1030 // Return the alignment required by the memequalN function.
1031 static int64_t memequal_align(Gogo*, int size);
1033 // Export the type.
1034 void
1035 export_type(Export* exp) const
1036 { this->do_export(exp); }
1038 // Import a type.
1039 static Type*
1040 import_type(Import*);
1042 protected:
1043 Type(Type_classification);
1045 // Functions implemented by the child class.
1047 // Traverse the subtypes.
1048 virtual int
1049 do_traverse(Traverse*);
1051 // Verify the type.
1052 virtual bool
1053 do_verify()
1054 { return true; }
1056 virtual bool
1057 do_has_pointer() const
1058 { return false; }
1060 virtual bool
1061 do_compare_is_identity(Gogo*) = 0;
1063 virtual bool
1064 do_is_reflexive()
1065 { return true; }
1067 virtual bool
1068 do_needs_key_update()
1069 { return false; }
1071 virtual bool
1072 do_in_heap()
1073 { return true; }
1075 virtual unsigned int
1076 do_hash_for_method(Gogo*) const;
1078 virtual Btype*
1079 do_get_backend(Gogo*) = 0;
1081 virtual Expression*
1082 do_type_descriptor(Gogo*, Named_type* name) = 0;
1084 virtual void
1085 do_reflection(Gogo*, std::string*) const = 0;
1087 virtual void
1088 do_mangled_name(Gogo*, std::string*) const = 0;
1090 virtual void
1091 do_export(Export*) const;
1093 // Return whether a method expects a pointer as the receiver.
1094 static bool
1095 method_expects_pointer(const Named_object*);
1097 // Finalize the methods for a type.
1098 static void
1099 finalize_methods(Gogo*, const Type*, Location, Methods**);
1101 // Return a method from a set of methods.
1102 static Method*
1103 method_function(const Methods*, const std::string& name,
1104 bool* is_ambiguous);
1106 // A mapping from interfaces to the associated interface method
1107 // tables for this type. This maps to a decl.
1108 typedef Unordered_map_hash(Interface_type*, Expression*, Type_hash_identical,
1109 Type_identical) Interface_method_tables;
1111 // Return a pointer to the interface method table for TYPE for the
1112 // interface INTERFACE.
1113 static Expression*
1114 interface_method_table(Type* type,
1115 Interface_type *interface, bool is_pointer,
1116 Interface_method_tables** method_tables,
1117 Interface_method_tables** pointer_tables);
1119 // Return a composite literal for the type descriptor entry for a
1120 // type.
1121 static Expression*
1122 type_descriptor(Gogo*, Type*);
1124 // Return a composite literal for the type descriptor entry for
1125 // TYPE, using NAME as the name of the type.
1126 static Expression*
1127 named_type_descriptor(Gogo*, Type* type, Named_type* name);
1129 // Return a composite literal for a plain type descriptor for this
1130 // type with the given kind and name.
1131 Expression*
1132 plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
1134 // Build a composite literal for the basic type descriptor.
1135 Expression*
1136 type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
1137 const Methods*, bool only_value_methods);
1139 // For the benefit of child class reflection string generation.
1140 void
1141 append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
1142 { type->do_reflection(gogo, ret); }
1144 // For the benefit of child class mangling.
1145 void
1146 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
1147 { type->do_mangled_name(gogo, ret); }
1149 // Incorporate a string into a hash code.
1150 static unsigned int
1151 hash_string(const std::string&, unsigned int);
1153 // Return the backend representation for the underlying type of a
1154 // named type.
1155 static Btype*
1156 get_named_base_btype(Gogo* gogo, Type* base_type)
1157 { return base_type->get_btype_without_hash(gogo); }
1159 private:
1160 // Convert to the desired type classification, or return NULL. This
1161 // is a controlled dynamic_cast.
1162 template<typename Type_class, Type_classification type_classification>
1163 Type_class*
1164 convert()
1166 Type* base = this->base();
1167 return (base->classification_ == type_classification
1168 ? static_cast<Type_class*>(base)
1169 : NULL);
1172 template<typename Type_class, Type_classification type_classification>
1173 const Type_class*
1174 convert() const
1176 const Type* base = this->base();
1177 return (base->classification_ == type_classification
1178 ? static_cast<Type_class*>(base)
1179 : NULL);
1182 template<typename Type_class, Type_classification type_classification>
1183 Type_class*
1184 convert_no_base()
1186 return (this->classification_ == type_classification
1187 ? static_cast<Type_class*>(this)
1188 : NULL);
1191 template<typename Type_class, Type_classification type_classification>
1192 const Type_class*
1193 convert_no_base() const
1195 return (this->classification_ == type_classification
1196 ? static_cast<Type_class*>(this)
1197 : NULL);
1200 // Map unnamed types to type descriptor decls.
1201 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
1202 Type_identical) Type_descriptor_vars;
1204 static Type_descriptor_vars type_descriptor_vars;
1206 // Build the type descriptor variable for this type.
1207 void
1208 make_type_descriptor_var(Gogo*);
1210 // Map unnamed types to type descriptor decls.
1211 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
1212 Type_identical) GC_symbol_vars;
1214 static GC_symbol_vars gc_symbol_vars;
1216 // Map ptrmask symbol names to the ptrmask variable.
1217 typedef Unordered_map(std::string, Bvariable*) GC_gcbits_vars;
1219 static GC_gcbits_vars gc_gcbits_vars;
1221 // Build the GC symbol for this type.
1222 void
1223 make_gc_symbol_var(Gogo*);
1225 // Return true if the type descriptor for this type should be
1226 // defined in some other package. If NAME is not NULL, it is the
1227 // name of this type. If this returns true it sets *PACKAGE to the
1228 // package where the type descriptor is defined.
1229 bool
1230 type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
1232 // Make a composite literal for the garbage collection program for
1233 // this type.
1234 Expression*
1235 gcprog_constructor(Gogo*, int64_t ptrsize, int64_t ptrdata);
1237 // Build the hash and equality type functions for a type which needs
1238 // specific functions.
1239 void
1240 specific_type_functions(Gogo*, Named_type*, int64_t size,
1241 Function_type* hash_fntype,
1242 Function_type* equal_fntype, Named_object** hash_fn,
1243 Named_object** equal_fn);
1245 void
1246 write_identity_hash(Gogo*, int64_t size);
1248 void
1249 write_identity_equal(Gogo*, int64_t size);
1251 void
1252 write_named_hash(Gogo*, Named_type*, Function_type* hash_fntype,
1253 Function_type* equal_fntype);
1255 void
1256 write_named_equal(Gogo*, Named_type*);
1258 // Build a composite literal for the uncommon type information.
1259 Expression*
1260 uncommon_type_constructor(Gogo*, Type* uncommon_type,
1261 Named_type*, const Methods*,
1262 bool only_value_methods) const;
1264 // Build a composite literal for the methods.
1265 Expression*
1266 methods_constructor(Gogo*, Type* methods_type, const Methods*,
1267 bool only_value_methods) const;
1269 // Build a composite literal for one method.
1270 Expression*
1271 method_constructor(Gogo*, Type* method_type, const std::string& name,
1272 const Method*, bool only_value_methods) const;
1274 // Add all methods for TYPE to the list of methods for THIS.
1275 static void
1276 add_methods_for_type(const Type* type, const Method::Field_indexes*,
1277 unsigned int depth, bool, bool,
1278 std::vector<const Named_type*>*,
1279 Methods*);
1281 static void
1282 add_local_methods_for_type(const Named_type* type,
1283 const Method::Field_indexes*,
1284 unsigned int depth, bool, bool, Methods*);
1286 static void
1287 add_embedded_methods_for_type(const Type* type,
1288 const Method::Field_indexes*,
1289 unsigned int depth, bool, bool,
1290 std::vector<const Named_type*>*,
1291 Methods*);
1293 static void
1294 add_interface_methods_for_type(const Type* type,
1295 const Method::Field_indexes*,
1296 unsigned int depth, Methods*);
1298 // Build stub methods for a type.
1299 static void
1300 build_stub_methods(Gogo*, const Type* type, const Methods* methods,
1301 Location);
1303 static void
1304 build_one_stub_method(Gogo*, Method*, const char* receiver_name,
1305 const Typed_identifier_list*, bool is_varargs,
1306 Location);
1308 static Expression*
1309 apply_field_indexes(Expression*, const Method::Field_indexes*,
1310 Location);
1312 // Look for a field or method named NAME in TYPE.
1313 static bool
1314 find_field_or_method(const Type* type, const std::string& name,
1315 bool receiver_can_be_pointer,
1316 std::vector<const Named_type*>*, int* level,
1317 bool* is_method, bool* found_pointer_method,
1318 std::string* ambig1, std::string* ambig2);
1320 // Get the backend representation for a type without looking in the
1321 // hash table for identical types.
1322 Btype*
1323 get_btype_without_hash(Gogo*);
1325 // A backend type that may be a placeholder.
1326 struct Type_btype_entry
1328 Btype *btype;
1329 bool is_placeholder;
1332 // A mapping from Type to Btype*, used to ensure that the backend
1333 // representation of identical types is identical. This is only
1334 // used for unnamed types.
1335 typedef Unordered_map_hash(const Type*, Type_btype_entry,
1336 Type_hash_identical, Type_identical) Type_btypes;
1338 static Type_btypes type_btypes;
1340 // A list of builtin named types.
1341 static std::vector<Named_type*> named_builtin_types;
1343 // A map from types which need specific type functions to the type
1344 // functions themselves.
1345 typedef std::pair<Named_object*, Named_object*> Hash_equal_fn;
1346 typedef Unordered_map_hash(const Type*, Hash_equal_fn, Type_hash_identical,
1347 Type_identical) Type_functions;
1349 static Type_functions type_functions_table;
1351 // Cache for reusing existing pointer types; maps from pointed-to-type
1352 // to pointer type.
1353 typedef Unordered_map(Type*, Pointer_type*) Pointer_type_table;
1355 static Pointer_type_table pointer_types;
1357 // List of placeholder pointer types.
1358 static std::vector<Pointer_type*> placeholder_pointers;
1360 // The type classification.
1361 Type_classification classification_;
1362 // The backend representation of the type, once it has been
1363 // determined.
1364 Btype* btype_;
1365 // The type descriptor for this type. This starts out as NULL and
1366 // is filled in as needed.
1367 Bvariable* type_descriptor_var_;
1368 // The GC symbol for this type. This starts out as NULL and
1369 // is filled in as needed.
1370 Bvariable* gc_symbol_var_;
1371 // Whether this type can appear in the heap.
1372 bool in_heap_;
1375 // Type hash table operations.
1377 class Type_hash_identical
1379 public:
1380 unsigned int
1381 operator()(const Type* type) const
1382 { return type->hash_for_method(NULL); }
1385 class Type_identical
1387 public:
1388 bool
1389 operator()(const Type* t1, const Type* t2) const
1390 { return Type::are_identical(t1, t2, false, NULL); }
1393 // An identifier with a type.
1395 class Typed_identifier
1397 public:
1398 Typed_identifier(const std::string& name, Type* type,
1399 Location location)
1400 : name_(name), type_(type), location_(location), note_(NULL)
1403 // Get the name.
1404 const std::string&
1405 name() const
1406 { return this->name_; }
1408 // Get the type.
1409 Type*
1410 type() const
1411 { return this->type_; }
1413 // Return the location where the name was seen. This is not always
1414 // meaningful.
1415 Location
1416 location() const
1417 { return this->location_; }
1419 // Set the type--sometimes we see the identifier before the type.
1420 void
1421 set_type(Type* type)
1423 go_assert(this->type_ == NULL || type->is_error_type());
1424 this->type_ = type;
1427 // Get the escape note.
1428 std::string*
1429 note() const
1430 { return this->note_; }
1432 // Set the escape note.
1433 void
1434 set_note(const std::string& note)
1435 { this->note_ = new std::string(note); }
1437 private:
1438 // Identifier name.
1439 std::string name_;
1440 // Type.
1441 Type* type_;
1442 // The location where the name was seen.
1443 Location location_;
1444 // Escape note for this typed identifier. Used when importing and exporting
1445 // functions.
1446 std::string* note_;
1449 // A list of Typed_identifiers.
1451 class Typed_identifier_list
1453 public:
1454 Typed_identifier_list()
1455 : entries_()
1458 // Whether the list is empty.
1459 bool
1460 empty() const
1461 { return this->entries_.empty(); }
1463 // Return the number of entries in the list.
1464 size_t
1465 size() const
1466 { return this->entries_.size(); }
1468 // Add an entry to the end of the list.
1469 void
1470 push_back(const Typed_identifier& td)
1471 { this->entries_.push_back(td); }
1473 // Remove an entry from the end of the list.
1474 void
1475 pop_back()
1476 { this->entries_.pop_back(); }
1478 // Set the type of entry I to TYPE.
1479 void
1480 set_type(size_t i, Type* type)
1482 go_assert(i < this->entries_.size());
1483 this->entries_[i].set_type(type);
1486 // Sort the entries by name.
1487 void
1488 sort_by_name();
1490 // Traverse types.
1492 traverse(Traverse*);
1494 // Return the first and last elements.
1495 Typed_identifier&
1496 front()
1497 { return this->entries_.front(); }
1499 const Typed_identifier&
1500 front() const
1501 { return this->entries_.front(); }
1503 Typed_identifier&
1504 back()
1505 { return this->entries_.back(); }
1507 const Typed_identifier&
1508 back() const
1509 { return this->entries_.back(); }
1511 Typed_identifier&
1512 at(size_t i)
1513 { return this->entries_.at(i); }
1515 const Typed_identifier&
1516 at(size_t i) const
1517 { return this->entries_.at(i); }
1519 void
1520 set(size_t i, const Typed_identifier& t)
1521 { this->entries_.at(i) = t; }
1523 void
1524 resize(size_t c)
1526 go_assert(c <= this->entries_.size());
1527 this->entries_.resize(c, Typed_identifier("", NULL,
1528 Linemap::unknown_location()));
1531 void
1532 reserve(size_t c)
1533 { this->entries_.reserve(c); }
1535 // Iterators.
1537 typedef std::vector<Typed_identifier>::iterator iterator;
1538 typedef std::vector<Typed_identifier>::const_iterator const_iterator;
1540 iterator
1541 begin()
1542 { return this->entries_.begin(); }
1544 const_iterator
1545 begin() const
1546 { return this->entries_.begin(); }
1548 iterator
1549 end()
1550 { return this->entries_.end(); }
1552 const_iterator
1553 end() const
1554 { return this->entries_.end(); }
1556 // Return a copy of this list. This returns an independent copy of
1557 // the vector, but does not copy the types.
1558 Typed_identifier_list*
1559 copy() const;
1561 private:
1562 std::vector<Typed_identifier> entries_;
1565 // A type used to indicate a parsing error. This exists to simplify
1566 // later error detection.
1568 class Error_type : public Type
1570 public:
1571 Error_type()
1572 : Type(TYPE_ERROR)
1575 protected:
1576 bool
1577 do_compare_is_identity(Gogo*)
1578 { return false; }
1580 Btype*
1581 do_get_backend(Gogo* gogo);
1583 Expression*
1584 do_type_descriptor(Gogo*, Named_type*);
1586 void
1587 do_reflection(Gogo*, std::string*) const;
1589 void
1590 do_mangled_name(Gogo*, std::string* ret) const;
1593 // The void type.
1595 class Void_type : public Type
1597 public:
1598 Void_type()
1599 : Type(TYPE_VOID)
1602 protected:
1603 bool
1604 do_compare_is_identity(Gogo*)
1605 { return false; }
1607 Btype*
1608 do_get_backend(Gogo* gogo);
1610 Expression*
1611 do_type_descriptor(Gogo*, Named_type*)
1612 { go_unreachable(); }
1614 void
1615 do_reflection(Gogo*, std::string*) const
1618 void
1619 do_mangled_name(Gogo*, std::string* ret) const;
1622 // The boolean type.
1624 class Boolean_type : public Type
1626 public:
1627 Boolean_type()
1628 : Type(TYPE_BOOLEAN)
1631 protected:
1632 bool
1633 do_compare_is_identity(Gogo*)
1634 { return true; }
1636 Btype*
1637 do_get_backend(Gogo* gogo);
1639 Expression*
1640 do_type_descriptor(Gogo*, Named_type* name);
1642 // We should not be asked for the reflection string of a basic type.
1643 void
1644 do_reflection(Gogo*, std::string* ret) const
1645 { ret->append("bool"); }
1647 void
1648 do_mangled_name(Gogo*, std::string* ret) const;
1651 // The type of an integer.
1653 class Integer_type : public Type
1655 public:
1656 // Create a new integer type.
1657 static Named_type*
1658 create_integer_type(const char* name, bool is_unsigned, int bits,
1659 int runtime_type_kind);
1661 // Look up an existing integer type.
1662 static Named_type*
1663 lookup_integer_type(const char* name);
1665 // Create an abstract integer type.
1666 static Integer_type*
1667 create_abstract_integer_type();
1669 // Create an abstract character type.
1670 static Integer_type*
1671 create_abstract_character_type();
1673 // Whether this is an abstract integer type.
1674 bool
1675 is_abstract() const
1676 { return this->is_abstract_; }
1678 // Whether this is an unsigned type.
1679 bool
1680 is_unsigned() const
1681 { return this->is_unsigned_; }
1683 // The number of bits.
1685 bits() const
1686 { return this->bits_; }
1688 // Whether this type is the same as T.
1689 bool
1690 is_identical(const Integer_type* t) const;
1692 // Whether this is the type "byte" or another name for "byte".
1693 bool
1694 is_byte() const
1695 { return this->is_byte_; }
1697 // Mark this as the "byte" type.
1698 void
1699 set_is_byte()
1700 { this->is_byte_ = true; }
1702 // Whether this is the type "rune" or another name for "rune".
1703 bool
1704 is_rune() const
1705 { return this->is_rune_; }
1707 // Mark this as the "rune" type.
1708 void
1709 set_is_rune()
1710 { this->is_rune_ = true; }
1712 protected:
1713 bool
1714 do_compare_is_identity(Gogo*)
1715 { return true; }
1717 unsigned int
1718 do_hash_for_method(Gogo*) const;
1720 Btype*
1721 do_get_backend(Gogo*);
1723 Expression*
1724 do_type_descriptor(Gogo*, Named_type*);
1726 void
1727 do_reflection(Gogo*, std::string*) const;
1729 void
1730 do_mangled_name(Gogo*, std::string*) const;
1732 private:
1733 Integer_type(bool is_abstract, bool is_unsigned, int bits,
1734 int runtime_type_kind)
1735 : Type(TYPE_INTEGER),
1736 is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
1737 is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
1740 // Map names of integer types to the types themselves.
1741 typedef std::map<std::string, Named_type*> Named_integer_types;
1742 static Named_integer_types named_integer_types;
1744 // True if this is an abstract type.
1745 bool is_abstract_;
1746 // True if this is an unsigned type.
1747 bool is_unsigned_;
1748 // True if this is the byte type.
1749 bool is_byte_;
1750 // True if this is the rune type.
1751 bool is_rune_;
1752 // The number of bits.
1753 int bits_;
1754 // The runtime type code used in the type descriptor for this type.
1755 int runtime_type_kind_;
1758 // The type of a floating point number.
1760 class Float_type : public Type
1762 public:
1763 // Create a new float type.
1764 static Named_type*
1765 create_float_type(const char* name, int bits, int runtime_type_kind);
1767 // Look up an existing float type.
1768 static Named_type*
1769 lookup_float_type(const char* name);
1771 // Create an abstract float type.
1772 static Float_type*
1773 create_abstract_float_type();
1775 // Whether this is an abstract float type.
1776 bool
1777 is_abstract() const
1778 { return this->is_abstract_; }
1780 // The number of bits.
1782 bits() const
1783 { return this->bits_; }
1785 // Whether this type is the same as T.
1786 bool
1787 is_identical(const Float_type* t) const;
1789 protected:
1790 bool
1791 do_compare_is_identity(Gogo*)
1792 { return false; }
1794 bool
1795 do_is_reflexive()
1796 { return false; }
1798 // Distinction between +0 and -0 requires a key update.
1799 bool
1800 do_needs_key_update()
1801 { return true; }
1803 unsigned int
1804 do_hash_for_method(Gogo*) const;
1806 Btype*
1807 do_get_backend(Gogo*);
1809 Expression*
1810 do_type_descriptor(Gogo*, Named_type*);
1812 void
1813 do_reflection(Gogo*, std::string*) const;
1815 void
1816 do_mangled_name(Gogo*, std::string*) const;
1818 private:
1819 Float_type(bool is_abstract, int bits, int runtime_type_kind)
1820 : Type(TYPE_FLOAT),
1821 is_abstract_(is_abstract), bits_(bits),
1822 runtime_type_kind_(runtime_type_kind)
1825 // Map names of float types to the types themselves.
1826 typedef std::map<std::string, Named_type*> Named_float_types;
1827 static Named_float_types named_float_types;
1829 // True if this is an abstract type.
1830 bool is_abstract_;
1831 // The number of bits in the floating point value.
1832 int bits_;
1833 // The runtime type code used in the type descriptor for this type.
1834 int runtime_type_kind_;
1837 // The type of a complex number.
1839 class Complex_type : public Type
1841 public:
1842 // Create a new complex type.
1843 static Named_type*
1844 create_complex_type(const char* name, int bits, int runtime_type_kind);
1846 // Look up an existing complex type.
1847 static Named_type*
1848 lookup_complex_type(const char* name);
1850 // Create an abstract complex type.
1851 static Complex_type*
1852 create_abstract_complex_type();
1854 // Whether this is an abstract complex type.
1855 bool
1856 is_abstract() const
1857 { return this->is_abstract_; }
1859 // The number of bits: 64 or 128.
1860 int bits() const
1861 { return this->bits_; }
1863 // Whether this type is the same as T.
1864 bool
1865 is_identical(const Complex_type* t) const;
1867 protected:
1868 bool
1869 do_compare_is_identity(Gogo*)
1870 { return false; }
1872 bool
1873 do_is_reflexive()
1874 { return false; }
1876 // Distinction between +0 and -0 requires a key update.
1877 bool
1878 do_needs_key_update()
1879 { return true; }
1881 unsigned int
1882 do_hash_for_method(Gogo*) const;
1884 Btype*
1885 do_get_backend(Gogo*);
1887 Expression*
1888 do_type_descriptor(Gogo*, Named_type*);
1890 void
1891 do_reflection(Gogo*, std::string*) const;
1893 void
1894 do_mangled_name(Gogo*, std::string*) const;
1896 private:
1897 Complex_type(bool is_abstract, int bits, int runtime_type_kind)
1898 : Type(TYPE_COMPLEX),
1899 is_abstract_(is_abstract), bits_(bits),
1900 runtime_type_kind_(runtime_type_kind)
1903 // Map names of complex types to the types themselves.
1904 typedef std::map<std::string, Named_type*> Named_complex_types;
1905 static Named_complex_types named_complex_types;
1907 // True if this is an abstract type.
1908 bool is_abstract_;
1909 // The number of bits in the complex value--64 or 128.
1910 int bits_;
1911 // The runtime type code used in the type descriptor for this type.
1912 int runtime_type_kind_;
1915 // The type of a string.
1917 class String_type : public Type
1919 public:
1920 String_type()
1921 : Type(TYPE_STRING)
1924 protected:
1925 bool
1926 do_has_pointer() const
1927 { return true; }
1929 bool
1930 do_compare_is_identity(Gogo*)
1931 { return false; }
1933 // New string might have a smaller backing store.
1934 bool
1935 do_needs_key_update()
1936 { return true; }
1938 Btype*
1939 do_get_backend(Gogo*);
1941 Expression*
1942 do_type_descriptor(Gogo*, Named_type*);
1944 void
1945 do_reflection(Gogo*, std::string*) const;
1947 void
1948 do_mangled_name(Gogo*, std::string* ret) const;
1950 private:
1951 // The named string type.
1952 static Named_type* string_type_;
1955 // The type of a function.
1957 class Function_type : public Type
1959 public:
1960 Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
1961 Typed_identifier_list* results, Location location)
1962 : Type(TYPE_FUNCTION),
1963 receiver_(receiver), parameters_(parameters), results_(results),
1964 location_(location), is_varargs_(false), is_builtin_(false),
1965 fnbtype_(NULL), is_tagged_(false)
1968 // Get the receiver.
1969 const Typed_identifier*
1970 receiver() const
1971 { return this->receiver_; }
1973 // Add an escape note for the receiver.
1974 void
1975 add_receiver_note(int encoding)
1976 { this->receiver_->set_note(Escape_note::make_tag(encoding)); }
1978 // Get the return names and types.
1979 const Typed_identifier_list*
1980 results() const
1981 { return this->results_; }
1983 // Get the parameter names and types.
1984 const Typed_identifier_list*
1985 parameters() const
1986 { return this->parameters_; }
1988 // Add an escape note for the ith parameter.
1989 void
1990 add_parameter_note(int index, int encoding)
1991 { this->parameters_->at(index).set_note(Escape_note::make_tag(encoding)); }
1993 // Whether this function has been tagged during escape analysis.
1994 bool
1995 is_tagged() const
1996 { return this->is_tagged_; }
1998 // Mark this function as tagged after analyzing its escape.
1999 void
2000 set_is_tagged()
2001 { this->is_tagged_ = true; }
2003 // Whether this is a varargs function.
2004 bool
2005 is_varargs() const
2006 { return this->is_varargs_; }
2008 // Whether this is a builtin function.
2009 bool
2010 is_builtin() const
2011 { return this->is_builtin_; }
2013 // The location where this type was defined.
2014 Location
2015 location() const
2016 { return this->location_; }
2018 // Return whether this is a method type.
2019 bool
2020 is_method() const
2021 { return this->receiver_ != NULL; }
2023 // Whether T is a valid redeclaration of this type. This is called
2024 // when a function is declared more than once.
2025 bool
2026 is_valid_redeclaration(const Function_type* t, std::string*) const;
2028 // Whether this type is the same as T.
2029 bool
2030 is_identical(const Function_type* t, bool ignore_receiver,
2031 Cmp_tags, bool errors_are_identical, std::string*) const;
2033 // Record that this is a varargs function.
2034 void
2035 set_is_varargs()
2036 { this->is_varargs_ = true; }
2038 // Record that this is a builtin function.
2039 void
2040 set_is_builtin()
2041 { this->is_builtin_ = true; }
2043 // Import a function type.
2044 static Function_type*
2045 do_import(Import*);
2047 // Return a copy of this type without a receiver. This is only
2048 // valid for a method type.
2049 Function_type*
2050 copy_without_receiver() const;
2052 // Return a copy of this type with a receiver. This is used when an
2053 // interface method is attached to a named or struct type.
2054 Function_type*
2055 copy_with_receiver(Type*) const;
2057 // Return a copy of this type with the receiver treated as the first
2058 // parameter. If WANT_POINTER_RECEIVER is true, the receiver is
2059 // forced to be a pointer.
2060 Function_type*
2061 copy_with_receiver_as_param(bool want_pointer_receiver) const;
2063 // Return a copy of this type ignoring any receiver and using dummy
2064 // names for all parameters. This is used for thunks for method
2065 // values.
2066 Function_type*
2067 copy_with_names() const;
2069 static Type*
2070 make_function_type_descriptor_type();
2072 // Return the backend representation of this function type. This is used
2073 // as the real type of a backend function declaration or defintion.
2074 Btype*
2075 get_backend_fntype(Gogo*);
2077 protected:
2079 do_traverse(Traverse*);
2081 // A function descriptor may be allocated on the heap.
2082 bool
2083 do_has_pointer() const
2084 { return true; }
2086 bool
2087 do_compare_is_identity(Gogo*)
2088 { return false; }
2090 unsigned int
2091 do_hash_for_method(Gogo*) const;
2093 Btype*
2094 do_get_backend(Gogo*);
2096 Expression*
2097 do_type_descriptor(Gogo*, Named_type*);
2099 void
2100 do_reflection(Gogo*, std::string*) const;
2102 void
2103 do_mangled_name(Gogo*, std::string*) const;
2105 void
2106 do_export(Export*) const;
2108 private:
2109 Expression*
2110 type_descriptor_params(Type*, const Typed_identifier*,
2111 const Typed_identifier_list*);
2113 // A mapping from a list of result types to a backend struct type.
2114 class Results_hash
2116 public:
2117 unsigned int
2118 operator()(const Typed_identifier_list*) const;
2121 class Results_equal
2123 public:
2124 bool
2125 operator()(const Typed_identifier_list*,
2126 const Typed_identifier_list*) const;
2129 typedef Unordered_map_hash(Typed_identifier_list*, Btype*,
2130 Results_hash, Results_equal) Results_structs;
2132 static Results_structs results_structs;
2134 // The receiver name and type. This will be NULL for a normal
2135 // function, non-NULL for a method.
2136 Typed_identifier* receiver_;
2137 // The parameter names and types.
2138 Typed_identifier_list* parameters_;
2139 // The result names and types. This will be NULL if no result was
2140 // specified.
2141 Typed_identifier_list* results_;
2142 // The location where this type was defined. This exists solely to
2143 // give a location for the fields of the struct if this function
2144 // returns multiple values.
2145 Location location_;
2146 // Whether this function takes a variable number of arguments.
2147 bool is_varargs_;
2148 // Whether this is a special builtin function which can not simply
2149 // be called. This is used for len, cap, etc.
2150 bool is_builtin_;
2151 // The backend representation of this type for backend function
2152 // declarations and definitions.
2153 Btype* fnbtype_;
2154 // Whether this function has been analyzed by escape analysis. If this is
2155 // TRUE, this function type's parameters contain a summary of the analysis.
2156 bool is_tagged_;
2159 // The type of a function's backend representation.
2161 class Backend_function_type : public Function_type
2163 public:
2164 Backend_function_type(Typed_identifier* receiver,
2165 Typed_identifier_list* parameters,
2166 Typed_identifier_list* results, Location location)
2167 : Function_type(receiver, parameters, results, location)
2170 protected:
2171 Btype*
2172 do_get_backend(Gogo* gogo)
2173 { return this->get_backend_fntype(gogo); }
2176 // The type of a pointer.
2178 class Pointer_type : public Type
2180 public:
2181 Pointer_type(Type* to_type)
2182 : Type(TYPE_POINTER),
2183 to_type_(to_type)
2186 Type*
2187 points_to() const
2188 { return this->to_type_; }
2190 // Import a pointer type.
2191 static Pointer_type*
2192 do_import(Import*);
2194 static Type*
2195 make_pointer_type_descriptor_type();
2197 protected:
2199 do_traverse(Traverse*);
2201 bool
2202 do_verify()
2203 { return this->to_type_->verify(); }
2205 bool
2206 do_has_pointer() const
2207 { return true; }
2209 bool
2210 do_compare_is_identity(Gogo*)
2211 { return true; }
2213 unsigned int
2214 do_hash_for_method(Gogo*) const;
2216 Btype*
2217 do_get_backend(Gogo*);
2219 Expression*
2220 do_type_descriptor(Gogo*, Named_type*);
2222 void
2223 do_reflection(Gogo*, std::string*) const;
2225 void
2226 do_mangled_name(Gogo*, std::string*) const;
2228 void
2229 do_export(Export*) const;
2231 private:
2232 // The type to which this type points.
2233 Type* to_type_;
2236 // The nil type. We use a special type for nil because it is not the
2237 // same as any other type. In C term nil has type void*, but there is
2238 // no such type in Go.
2240 class Nil_type : public Type
2242 public:
2243 Nil_type()
2244 : Type(TYPE_NIL)
2247 protected:
2248 bool
2249 do_compare_is_identity(Gogo*)
2250 { return false; }
2252 Btype*
2253 do_get_backend(Gogo* gogo);
2255 Expression*
2256 do_type_descriptor(Gogo*, Named_type*)
2257 { go_unreachable(); }
2259 void
2260 do_reflection(Gogo*, std::string*) const
2261 { go_unreachable(); }
2263 void
2264 do_mangled_name(Gogo*, std::string* ret) const;
2267 // The type of a field in a struct.
2269 class Struct_field
2271 public:
2272 explicit Struct_field(const Typed_identifier& typed_identifier)
2273 : typed_identifier_(typed_identifier), tag_(NULL), is_imported_(false)
2276 // The field name.
2277 const std::string&
2278 field_name() const;
2280 // Return whether this struct field is named NAME.
2281 bool
2282 is_field_name(const std::string& name) const;
2284 // Return whether this struct field is an unexported field named NAME.
2285 bool
2286 is_unexported_field_name(Gogo*, const std::string& name) const;
2288 // Return whether this struct field is an embedded built-in type.
2289 bool
2290 is_embedded_builtin(Gogo*) const;
2292 // The field type.
2293 Type*
2294 type() const
2295 { return this->typed_identifier_.type(); }
2297 // The field location.
2298 Location
2299 location() const
2300 { return this->typed_identifier_.location(); }
2302 // Whether the field has a tag.
2303 bool
2304 has_tag() const
2305 { return this->tag_ != NULL; }
2307 // The tag.
2308 const std::string&
2309 tag() const
2311 go_assert(this->tag_ != NULL);
2312 return *this->tag_;
2315 // Whether this is an anonymous field.
2316 bool
2317 is_anonymous() const
2318 { return this->typed_identifier_.name().empty(); }
2320 // Set the tag. FIXME: This is never freed.
2321 void
2322 set_tag(const std::string& tag)
2323 { this->tag_ = new std::string(tag); }
2325 // Record that this field is defined in an imported struct.
2326 void
2327 set_is_imported()
2328 { this->is_imported_ = true; }
2330 // Set the type. This is only used in error cases.
2331 void
2332 set_type(Type* type)
2333 { this->typed_identifier_.set_type(type); }
2335 private:
2336 // The field name, type, and location.
2337 Typed_identifier typed_identifier_;
2338 // The field tag. This is NULL if the field has no tag.
2339 std::string* tag_;
2340 // Whether this field is defined in an imported struct.
2341 bool is_imported_;
2344 // A list of struct fields.
2346 class Struct_field_list
2348 public:
2349 Struct_field_list()
2350 : entries_()
2353 // Whether the list is empty.
2354 bool
2355 empty() const
2356 { return this->entries_.empty(); }
2358 // Return the number of entries.
2359 size_t
2360 size() const
2361 { return this->entries_.size(); }
2363 // Add an entry to the end of the list.
2364 void
2365 push_back(const Struct_field& sf)
2366 { this->entries_.push_back(sf); }
2368 // Index into the list.
2369 const Struct_field&
2370 at(size_t i) const
2371 { return this->entries_.at(i); }
2373 // Last entry in list.
2374 Struct_field&
2375 back()
2376 { return this->entries_.back(); }
2378 // Iterators.
2380 typedef std::vector<Struct_field>::iterator iterator;
2381 typedef std::vector<Struct_field>::const_iterator const_iterator;
2383 iterator
2384 begin()
2385 { return this->entries_.begin(); }
2387 const_iterator
2388 begin() const
2389 { return this->entries_.begin(); }
2391 iterator
2392 end()
2393 { return this->entries_.end(); }
2395 const_iterator
2396 end() const
2397 { return this->entries_.end(); }
2399 private:
2400 std::vector<Struct_field> entries_;
2403 // The type of a struct.
2405 class Struct_type : public Type
2407 public:
2408 Struct_type(Struct_field_list* fields, Location location)
2409 : Type(TYPE_STRUCT),
2410 fields_(fields), location_(location), all_methods_(NULL),
2411 is_struct_incomparable_(false)
2414 // Return the field NAME. This only looks at local fields, not at
2415 // embedded types. If the field is found, and PINDEX is not NULL,
2416 // this sets *PINDEX to the field index. If the field is not found,
2417 // this returns NULL.
2418 const Struct_field*
2419 find_local_field(const std::string& name, unsigned int *pindex) const;
2421 // Return the field number INDEX.
2422 const Struct_field*
2423 field(unsigned int index) const
2424 { return &this->fields_->at(index); }
2426 // Get the struct fields.
2427 const Struct_field_list*
2428 fields() const
2429 { return this->fields_; }
2431 // Return the number of fields.
2432 size_t
2433 field_count() const
2434 { return this->fields_->size(); }
2436 // Push a new field onto the end of the struct. This is used when
2437 // building a closure variable.
2438 void
2439 push_field(const Struct_field& sf)
2440 { this->fields_->push_back(sf); }
2442 // Return an expression referring to field NAME in STRUCT_EXPR, or
2443 // NULL if there is no field with that name.
2444 Field_reference_expression*
2445 field_reference(Expression* struct_expr, const std::string& name,
2446 Location) const;
2448 // Return the total number of fields, including embedded fields.
2449 // This is the number of values that can appear in a conversion to
2450 // this type.
2451 unsigned int
2452 total_field_count() const;
2454 // Whether this type is identical with T.
2455 bool
2456 is_identical(const Struct_type* t, Cmp_tags,
2457 bool errors_are_identical) const;
2459 // Return whether NAME is a local field which is not exported. This
2460 // is only used for better error reporting.
2461 bool
2462 is_unexported_local_field(Gogo*, const std::string& name) const;
2464 // If this is an unnamed struct, build the complete list of methods,
2465 // including those from anonymous fields, and build methods stubs if
2466 // needed.
2467 void
2468 finalize_methods(Gogo*);
2470 // Return whether this type has any methods. This should only be
2471 // called after the finalize_methods pass.
2472 bool
2473 has_any_methods() const
2474 { return this->all_methods_ != NULL; }
2476 // Return the methods for tihs type. This should only be called
2477 // after the finalize_methods pass.
2478 const Methods*
2479 methods() const
2480 { return this->all_methods_; }
2482 // Return the method to use for NAME. This returns NULL if there is
2483 // no such method or if the method is ambiguous. When it returns
2484 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2485 Method*
2486 method_function(const std::string& name, bool* is_ambiguous) const;
2488 // Return a pointer to the interface method table for this type for
2489 // the interface INTERFACE. If IS_POINTER is true, set the type
2490 // descriptor to a pointer to this type, otherwise set it to this
2491 // type.
2492 Expression*
2493 interface_method_table(Interface_type* interface, bool is_pointer);
2495 // Traverse just the field types of a struct type.
2497 traverse_field_types(Traverse* traverse)
2498 { return this->do_traverse(traverse); }
2500 // If the offset of field INDEX in the backend implementation can be
2501 // determined, set *POFFSET to the offset in bytes and return true.
2502 // Otherwise, return false.
2503 bool
2504 backend_field_offset(Gogo*, unsigned int index, int64_t* poffset);
2506 // Finish the backend representation of all the fields.
2507 void
2508 finish_backend_fields(Gogo*);
2510 // Import a struct type.
2511 static Struct_type*
2512 do_import(Import*);
2514 static Type*
2515 make_struct_type_descriptor_type();
2517 // Return whether this is a generated struct that is not comparable.
2518 bool
2519 is_struct_incomparable() const
2520 { return this->is_struct_incomparable_; }
2522 // Record that this is a generated struct that is not comparable.
2523 void
2524 set_is_struct_incomparable()
2525 { this->is_struct_incomparable_ = true; }
2527 // Write the hash function for this type.
2528 void
2529 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2531 // Write the equality function for this type.
2532 void
2533 write_equal_function(Gogo*, Named_type*);
2535 // Whether we can write this type to a C header file, to implement
2536 // -fgo-c-header.
2537 bool
2538 can_write_to_c_header(std::vector<const Named_object*>*,
2539 std::vector<const Named_object*>*) const;
2541 // Write this type to a C header file, to implement -fgo-c-header.
2542 void
2543 write_to_c_header(std::ostream&) const;
2545 protected:
2547 do_traverse(Traverse*);
2549 bool
2550 do_verify();
2552 bool
2553 do_has_pointer() const;
2555 bool
2556 do_compare_is_identity(Gogo*);
2558 bool
2559 do_is_reflexive();
2561 bool
2562 do_needs_key_update();
2564 bool
2565 do_in_heap();
2567 unsigned int
2568 do_hash_for_method(Gogo*) const;
2570 Btype*
2571 do_get_backend(Gogo*);
2573 Expression*
2574 do_type_descriptor(Gogo*, Named_type*);
2576 void
2577 do_reflection(Gogo*, std::string*) const;
2579 void
2580 do_mangled_name(Gogo*, std::string*) const;
2582 void
2583 do_export(Export*) const;
2585 private:
2586 bool
2587 can_write_type_to_c_header(const Type*,
2588 std::vector<const Named_object*>*,
2589 std::vector<const Named_object*>*) const;
2591 void
2592 write_field_to_c_header(std::ostream&, const std::string&, const Type*) const;
2594 // Used to merge method sets of identical unnamed structs.
2595 typedef Unordered_map_hash(Struct_type*, Struct_type*, Type_hash_identical,
2596 Type_identical) Identical_structs;
2598 static Identical_structs identical_structs;
2600 // Used to manage method tables for identical unnamed structs.
2601 typedef std::pair<Interface_method_tables*, Interface_method_tables*>
2602 Struct_method_table_pair;
2604 typedef Unordered_map_hash(Struct_type*, Struct_method_table_pair*,
2605 Type_hash_identical, Type_identical)
2606 Struct_method_tables;
2608 static Struct_method_tables struct_method_tables;
2610 // Used to avoid infinite loops in field_reference_depth.
2611 struct Saw_named_type
2613 Saw_named_type* next;
2614 Named_type* nt;
2617 Field_reference_expression*
2618 field_reference_depth(Expression* struct_expr, const std::string& name,
2619 Location, Saw_named_type*,
2620 unsigned int* depth) const;
2622 // The fields of the struct.
2623 Struct_field_list* fields_;
2624 // The place where the struct was declared.
2625 Location location_;
2626 // If this struct is unnamed, a list of methods.
2627 Methods* all_methods_;
2628 // True if this is a generated struct that is not considered to be
2629 // comparable.
2630 bool is_struct_incomparable_;
2633 // The type of an array.
2635 class Array_type : public Type
2637 public:
2638 Array_type(Type* element_type, Expression* length)
2639 : Type(TYPE_ARRAY),
2640 element_type_(element_type), length_(length), blength_(NULL),
2641 issued_length_error_(false), is_array_incomparable_(false)
2644 // Return the element type.
2645 Type*
2646 element_type() const
2647 { return this->element_type_; }
2649 // Return the length. This will return NULL for a slice.
2650 Expression*
2651 length() const
2652 { return this->length_; }
2654 // Store the length as an int64_t into *PLEN. Return false if the
2655 // length can not be determined. This will assert if called for a
2656 // slice.
2657 bool
2658 int_length(int64_t* plen);
2660 // Whether this type is identical with T.
2661 bool
2662 is_identical(const Array_type* t, Cmp_tags,
2663 bool errors_are_identical) const;
2665 // Return an expression for the pointer to the values in an array.
2666 Expression*
2667 get_value_pointer(Gogo*, Expression* array, bool is_lvalue) const;
2669 // Return an expression for the length of an array with this type.
2670 Expression*
2671 get_length(Gogo*, Expression* array) const;
2673 // Return an expression for the capacity of an array with this type.
2674 Expression*
2675 get_capacity(Gogo*, Expression* array) const;
2677 // Import an array type.
2678 static Array_type*
2679 do_import(Import*);
2681 // Return the backend representation of the element type.
2682 Btype*
2683 get_backend_element(Gogo*, bool use_placeholder);
2685 // Return the backend representation of the length.
2686 Bexpression*
2687 get_backend_length(Gogo*);
2689 // Finish the backend representation of the element type.
2690 void
2691 finish_backend_element(Gogo*);
2693 static Type*
2694 make_array_type_descriptor_type();
2696 static Type*
2697 make_slice_type_descriptor_type();
2699 // Return whether this is a generated array that is not comparable.
2700 bool
2701 is_array_incomparable() const
2702 { return this->is_array_incomparable_; }
2704 // Record that this is a generated array that is not comparable.
2705 void
2706 set_is_array_incomparable()
2707 { this->is_array_incomparable_ = true; }
2709 // Write the hash function for this type.
2710 void
2711 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2713 // Write the equality function for this type.
2714 void
2715 write_equal_function(Gogo*, Named_type*);
2717 protected:
2719 do_traverse(Traverse* traverse);
2721 bool
2722 do_verify();
2724 bool
2725 do_has_pointer() const;
2727 bool
2728 do_compare_is_identity(Gogo*);
2730 bool
2731 do_is_reflexive()
2733 return this->length_ != NULL && this->element_type_->is_reflexive();
2736 bool
2737 do_needs_key_update()
2738 { return this->element_type_->needs_key_update(); }
2740 bool
2741 do_in_heap()
2742 { return this->length_ == NULL || this->element_type_->in_heap(); }
2744 unsigned int
2745 do_hash_for_method(Gogo*) const;
2747 Btype*
2748 do_get_backend(Gogo*);
2750 Expression*
2751 do_type_descriptor(Gogo*, Named_type*);
2753 void
2754 do_reflection(Gogo*, std::string*) const;
2756 void
2757 do_mangled_name(Gogo*, std::string*) const;
2759 void
2760 do_export(Export*) const;
2762 private:
2763 bool
2764 verify_length();
2766 Expression*
2767 array_type_descriptor(Gogo*, Named_type*);
2769 Expression*
2770 slice_type_descriptor(Gogo*, Named_type*);
2772 // The type of elements of the array.
2773 Type* element_type_;
2774 // The number of elements. This may be NULL.
2775 Expression* length_;
2776 // The backend representation of the length.
2777 // We only want to compute this once.
2778 Bexpression* blength_;
2779 // Whether or not an invalid length error has been issued for this type,
2780 // to avoid knock-on errors.
2781 mutable bool issued_length_error_;
2782 // True if this is a generated array that is not considered to be
2783 // comparable.
2784 bool is_array_incomparable_;
2787 // The type of a map.
2789 class Map_type : public Type
2791 public:
2792 Map_type(Type* key_type, Type* val_type, Location location)
2793 : Type(TYPE_MAP),
2794 key_type_(key_type), val_type_(val_type), hmap_type_(NULL),
2795 bucket_type_(NULL), hiter_type_(NULL), location_(location)
2798 // Return the key type.
2799 Type*
2800 key_type() const
2801 { return this->key_type_; }
2803 // Return the value type.
2804 Type*
2805 val_type() const
2806 { return this->val_type_; }
2808 // Return the type used for an iteration over this map.
2809 Type*
2810 hiter_type(Gogo*);
2812 // If this map requires the "fat" functions, returns the pointer to
2813 // pass as the zero value to those functions. Otherwise, in the
2814 // normal case, returns NULL.
2815 Expression*
2816 fat_zero_value(Gogo*);
2818 // Return whether VAR is the map zero value.
2819 static bool
2820 is_zero_value(Variable* var);
2822 // Return the backend representation of the map zero value.
2823 static Bvariable*
2824 backend_zero_value(Gogo*);
2826 // Whether this type is identical with T.
2827 bool
2828 is_identical(const Map_type* t, Cmp_tags,
2829 bool errors_are_identical) const;
2831 // Import a map type.
2832 static Map_type*
2833 do_import(Import*);
2835 static Type*
2836 make_map_type_descriptor_type();
2838 // This must be in sync with libgo/go/runtime/hashmap.go.
2839 static const int bucket_size = 8;
2841 protected:
2843 do_traverse(Traverse*);
2845 bool
2846 do_verify();
2848 bool
2849 do_has_pointer() const
2850 { return true; }
2852 bool
2853 do_compare_is_identity(Gogo*)
2854 { return false; }
2856 bool
2857 do_is_reflexive()
2859 return this->key_type_->is_reflexive() && this->val_type_->is_reflexive();
2862 unsigned int
2863 do_hash_for_method(Gogo*) const;
2865 Btype*
2866 do_get_backend(Gogo*);
2868 Expression*
2869 do_type_descriptor(Gogo*, Named_type*);
2871 void
2872 do_reflection(Gogo*, std::string*) const;
2874 void
2875 do_mangled_name(Gogo*, std::string*) const;
2877 void
2878 do_export(Export*) const;
2880 private:
2881 // These must be in sync with libgo/go/runtime/hashmap.go.
2882 static const int max_key_size = 128;
2883 static const int max_val_size = 128;
2884 static const int max_zero_size = 1024;
2886 // Maps with value types larger than max_zero_size require passing a
2887 // zero value pointer to the map functions.
2889 // The zero value variable.
2890 static Named_object* zero_value;
2892 // The current size of the zero value.
2893 static int64_t zero_value_size;
2895 // The current alignment of the zero value.
2896 static int64_t zero_value_align;
2898 Type*
2899 bucket_type(Gogo*, int64_t, int64_t);
2901 Type*
2902 hmap_type(Type*);
2904 // The key type.
2905 Type* key_type_;
2906 // The value type.
2907 Type* val_type_;
2908 // The hashmap type. At run time a map is represented as a pointer
2909 // to this type.
2910 Type* hmap_type_;
2911 // The bucket type, the type used to hold keys and values at run time.
2912 Type* bucket_type_;
2913 // The iterator type.
2914 Type* hiter_type_;
2915 // Where the type was defined.
2916 Location location_;
2919 // The type of a channel.
2921 class Channel_type : public Type
2923 public:
2924 Channel_type(bool may_send, bool may_receive, Type* element_type)
2925 : Type(TYPE_CHANNEL),
2926 may_send_(may_send), may_receive_(may_receive),
2927 element_type_(element_type)
2928 { go_assert(may_send || may_receive); }
2930 // Whether this channel can send data.
2931 bool
2932 may_send() const
2933 { return this->may_send_; }
2935 // Whether this channel can receive data.
2936 bool
2937 may_receive() const
2938 { return this->may_receive_; }
2940 // The type of the values that may be sent on this channel. This is
2941 // NULL if any type may be sent.
2942 Type*
2943 element_type() const
2944 { return this->element_type_; }
2946 // Whether this type is identical with T.
2947 bool
2948 is_identical(const Channel_type* t, Cmp_tags,
2949 bool errors_are_identical) const;
2951 // Import a channel type.
2952 static Channel_type*
2953 do_import(Import*);
2955 static Type*
2956 make_chan_type_descriptor_type();
2958 static Type*
2959 select_type(int ncases);
2961 protected:
2963 do_traverse(Traverse* traverse)
2964 { return Type::traverse(this->element_type_, traverse); }
2966 bool
2967 do_verify();
2969 bool
2970 do_has_pointer() const
2971 { return true; }
2973 bool
2974 do_compare_is_identity(Gogo*)
2975 { return true; }
2977 unsigned int
2978 do_hash_for_method(Gogo*) const;
2980 Btype*
2981 do_get_backend(Gogo*);
2983 Expression*
2984 do_type_descriptor(Gogo*, Named_type*);
2986 void
2987 do_reflection(Gogo*, std::string*) const;
2989 void
2990 do_mangled_name(Gogo*, std::string*) const;
2992 void
2993 do_export(Export*) const;
2995 private:
2996 // Whether this channel can send data.
2997 bool may_send_;
2998 // Whether this channel can receive data.
2999 bool may_receive_;
3000 // The types of elements which may be sent on this channel. If this
3001 // is NULL, it means that any type may be sent.
3002 Type* element_type_;
3005 // An interface type.
3007 class Interface_type : public Type
3009 public:
3010 Interface_type(Typed_identifier_list* methods, Location location)
3011 : Type(TYPE_INTERFACE),
3012 parse_methods_(methods), all_methods_(NULL), location_(location),
3013 package_(NULL), interface_btype_(NULL), bmethods_(NULL),
3014 assume_identical_(NULL), methods_are_finalized_(false),
3015 bmethods_is_placeholder_(false), seen_(false)
3016 { go_assert(methods == NULL || !methods->empty()); }
3018 // The location where the interface type was defined.
3019 Location
3020 location() const
3021 { return this->location_; }
3023 // The package where the interface type was defined. Returns NULL
3024 // for the package currently being compiled.
3025 Package*
3026 package() const
3027 { return this->package_; }
3029 // Return whether this is an empty interface.
3030 bool
3031 is_empty() const
3033 go_assert(this->methods_are_finalized_);
3034 return this->all_methods_ == NULL;
3037 // Return the list of methods. This will return NULL for an empty
3038 // interface.
3039 const Typed_identifier_list*
3040 methods() const;
3042 // Return the number of methods.
3043 size_t
3044 method_count() const;
3046 // Return the method NAME, or NULL.
3047 const Typed_identifier*
3048 find_method(const std::string& name) const;
3050 // Return the zero-based index of method NAME.
3051 size_t
3052 method_index(const std::string& name) const;
3054 // Finalize the methods. This sets all_methods_. This handles
3055 // interface inheritance.
3056 void
3057 finalize_methods();
3059 // Return true if T implements this interface. If this returns
3060 // false, and REASON is not NULL, it sets *REASON to the reason that
3061 // it fails.
3062 bool
3063 implements_interface(const Type* t, std::string* reason) const;
3065 // Whether this type is identical with T. REASON is as in
3066 // implements_interface.
3067 bool
3068 is_identical(const Interface_type* t, Cmp_tags,
3069 bool errors_are_identical) const;
3071 // Whether we can assign T to this type. is_identical is known to
3072 // be false.
3073 bool
3074 is_compatible_for_assign(const Interface_type*, std::string* reason) const;
3076 // Return whether NAME is a method which is not exported. This is
3077 // only used for better error reporting.
3078 bool
3079 is_unexported_method(Gogo*, const std::string& name) const;
3081 // Import an interface type.
3082 static Interface_type*
3083 do_import(Import*);
3085 // Make a struct for an empty interface type.
3086 static Btype*
3087 get_backend_empty_interface_type(Gogo*);
3089 // Get a pointer to the backend representation of the method table.
3090 Btype*
3091 get_backend_methods(Gogo*);
3093 // Return a placeholder for the backend representation of the
3094 // pointer to the method table.
3095 Btype*
3096 get_backend_methods_placeholder(Gogo*);
3098 // Finish the backend representation of the method types.
3099 void
3100 finish_backend_methods(Gogo*);
3102 static Type*
3103 make_interface_type_descriptor_type();
3105 protected:
3107 do_traverse(Traverse*);
3109 bool
3110 do_has_pointer() const
3111 { return true; }
3113 bool
3114 do_compare_is_identity(Gogo*)
3115 { return false; }
3117 // Not reflexive if it contains a float.
3118 bool
3119 do_is_reflexive()
3120 { return false; }
3122 // Distinction between +0 and -0 requires a key update if it
3123 // contains a float.
3124 bool
3125 do_needs_key_update()
3126 { return true; }
3128 unsigned int
3129 do_hash_for_method(Gogo*) const;
3131 Btype*
3132 do_get_backend(Gogo*);
3134 Expression*
3135 do_type_descriptor(Gogo*, Named_type*);
3137 void
3138 do_reflection(Gogo*, std::string*) const;
3140 void
3141 do_mangled_name(Gogo*, std::string*) const;
3143 void
3144 do_export(Export*) const;
3146 private:
3147 // This type guards against infinite recursion when comparing
3148 // interface types. We keep a list of interface types assumed to be
3149 // identical during comparison. We just keep the list on the stack.
3150 // This permits us to compare cases like
3151 // type I1 interface { F() interface{I1} }
3152 // type I2 interface { F() interface{I2} }
3153 struct Assume_identical
3155 Assume_identical* next;
3156 const Interface_type* t1;
3157 const Interface_type* t2;
3160 bool
3161 assume_identical(const Interface_type*, const Interface_type*) const;
3163 // The list of methods associated with the interface from the
3164 // parser. This will be NULL for the empty interface. This may
3165 // include unnamed interface types.
3166 Typed_identifier_list* parse_methods_;
3167 // The list of all methods associated with the interface. This
3168 // expands any interface types listed in methods_. It is set by
3169 // finalize_methods. This will be NULL for the empty interface.
3170 Typed_identifier_list* all_methods_;
3171 // The location where the interface was defined.
3172 Location location_;
3173 // The package where the interface was defined. This is NULL for
3174 // the package being compiled.
3175 Package* package_;
3176 // The backend representation of this type during backend conversion.
3177 Btype* interface_btype_;
3178 // The backend representation of the pointer to the method table.
3179 Btype* bmethods_;
3180 // A list of interface types assumed to be identical during
3181 // interface comparison.
3182 mutable Assume_identical* assume_identical_;
3183 // Whether the methods have been finalized.
3184 bool methods_are_finalized_;
3185 // Whether the bmethods_ field is a placeholder.
3186 bool bmethods_is_placeholder_;
3187 // Used to avoid endless recursion in do_mangled_name.
3188 mutable bool seen_;
3191 // The value we keep for a named type. This lets us get the right
3192 // name when we convert to backend. Note that we don't actually keep
3193 // the name here; the name is in the Named_object which points to
3194 // this. This object exists to hold a unique backend representation for
3195 // the type.
3197 class Named_type : public Type
3199 public:
3200 Named_type(Named_object* named_object, Type* type, Location location)
3201 : Type(TYPE_NAMED),
3202 named_object_(named_object), in_function_(NULL), in_function_index_(0),
3203 type_(type), local_methods_(NULL), all_methods_(NULL),
3204 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
3205 location_(location), named_btype_(NULL), dependencies_(),
3206 is_alias_(false), is_visible_(true), is_error_(false), in_heap_(true),
3207 is_placeholder_(false), is_converted_(false), is_circular_(false),
3208 is_verified_(false), seen_(false), seen_in_compare_is_identity_(false),
3209 seen_in_get_backend_(false), seen_alias_(false)
3212 // Return the associated Named_object. This holds the actual name.
3213 Named_object*
3214 named_object()
3215 { return this->named_object_; }
3217 const Named_object*
3218 named_object() const
3219 { return this->named_object_; }
3221 // Set the Named_object. This is used when we see a type
3222 // declaration followed by a type.
3223 void
3224 set_named_object(Named_object* no)
3225 { this->named_object_ = no; }
3227 // Whether this is an alias (type T1 = T2) rather than an ordinary
3228 // named type (type T1 T2).
3229 bool
3230 is_alias() const
3231 { return this->is_alias_; }
3233 // Record that this type is an alias.
3234 void
3235 set_is_alias()
3236 { this->is_alias_ = true; }
3238 // Mark this type as not permitted in the heap.
3239 void
3240 set_not_in_heap()
3241 { this->in_heap_ = false; }
3243 // Return the function in which this type is defined. This will
3244 // return NULL for a type defined in global scope.
3245 const Named_object*
3246 in_function(unsigned int *pindex) const
3248 *pindex = this->in_function_index_;
3249 return this->in_function_;
3252 // Set the function in which this type is defined.
3253 void
3254 set_in_function(Named_object* f, unsigned int index)
3256 this->in_function_ = f;
3257 this->in_function_index_ = index;
3260 // Return the name of the type.
3261 const std::string&
3262 name() const;
3264 // Return the name of the type for an error message. The difference
3265 // is that if the type is defined in a different package, this will
3266 // return PACKAGE.NAME.
3267 std::string
3268 message_name() const;
3270 // Return the underlying type.
3271 Type*
3272 real_type()
3273 { return this->type_; }
3275 const Type*
3276 real_type() const
3277 { return this->type_; }
3279 // Return the location.
3280 Location
3281 location() const
3282 { return this->location_; }
3284 // Whether this type is visible. This only matters when parsing.
3285 bool
3286 is_visible() const
3287 { return this->is_visible_; }
3289 // Mark this type as visible.
3290 void
3291 set_is_visible()
3292 { this->is_visible_ = true; }
3294 // Mark this type as invisible.
3295 void
3296 clear_is_visible()
3297 { this->is_visible_ = false; }
3299 // Whether this is a builtin type.
3300 bool
3301 is_builtin() const
3302 { return Linemap::is_predeclared_location(this->location_); }
3304 // Whether this named type is valid. A recursive named type is invalid.
3305 bool
3306 is_valid() const
3307 { return !this->is_error_; }
3309 // Whether this is a circular type: a pointer or function type that
3310 // refers to itself, which is not possible in C.
3311 bool
3312 is_circular() const
3313 { return this->is_circular_; }
3315 // Return the base type for this type.
3316 Type*
3317 named_base();
3319 const Type*
3320 named_base() const;
3322 // Return whether this is an error type.
3323 bool
3324 is_named_error_type() const;
3326 // Return whether this type is comparable. If REASON is not NULL,
3327 // set *REASON when returning false.
3328 bool
3329 named_type_is_comparable(std::string* reason) const;
3331 // Add a method to this type.
3332 Named_object*
3333 add_method(const std::string& name, Function*);
3335 // Add a method declaration to this type.
3336 Named_object*
3337 add_method_declaration(const std::string& name, Package* package,
3338 Function_type* type, Location location);
3340 // Add an existing method--one defined before the type itself was
3341 // defined--to a type.
3342 void
3343 add_existing_method(Named_object*);
3345 // Look up a local method.
3346 Named_object*
3347 find_local_method(const std::string& name) const;
3349 // Return the list of local methods.
3350 const Bindings*
3351 local_methods() const;
3353 // Build the complete list of methods, including those from
3354 // anonymous fields, and build method stubs if needed.
3355 void
3356 finalize_methods(Gogo*);
3358 // Return whether this type has any methods. This should only be
3359 // called after the finalize_methods pass.
3360 bool
3361 has_any_methods() const;
3363 // Return the methods for this type. This should only be called
3364 // after the finalized_methods pass.
3365 const Methods*
3366 methods() const;
3368 // Return the method to use for NAME. This returns NULL if there is
3369 // no such method or if the method is ambiguous. When it returns
3370 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
3371 Method*
3372 method_function(const std::string& name, bool *is_ambiguous) const;
3374 // Return whether NAME is a known field or method which is not
3375 // exported. This is only used for better error reporting.
3376 bool
3377 is_unexported_local_method(Gogo*, const std::string& name) const;
3379 // Return a pointer to the interface method table for this type for
3380 // the interface INTERFACE. If IS_POINTER is true, set the type
3381 // descriptor to a pointer to this type, otherwise set it to this
3382 // type.
3383 Expression*
3384 interface_method_table(Interface_type* interface, bool is_pointer);
3386 // Note that a type must be converted to the backend representation
3387 // before we convert this type.
3388 void
3389 add_dependency(Named_type* nt)
3390 { this->dependencies_.push_back(nt); }
3392 // Return true if the size and alignment of the backend
3393 // representation of this type is known. This is always true after
3394 // types have been converted, but may be false beforehand.
3395 bool
3396 is_named_backend_type_size_known() const
3397 { return this->named_btype_ != NULL && !this->is_placeholder_; }
3399 // Add to the reflection string as for Type::append_reflection, but
3400 // if USE_ALIAS use the alias name rather than the alias target.
3401 void
3402 append_reflection_type_name(Gogo*, bool use_alias, std::string*) const;
3404 // Append the mangled type name as for Type::append_mangled_name,
3405 // but if USE_ALIAS use the alias name rather than the alias target.
3406 void
3407 append_mangled_type_name(Gogo*, bool use_alias, std::string*) const;
3409 // Export the type.
3410 void
3411 export_named_type(Export*, const std::string& name) const;
3413 // Import a named type.
3414 static void
3415 import_named_type(Import*, Named_type**);
3417 // Initial conversion to backend representation.
3418 void
3419 convert(Gogo*);
3421 protected:
3423 do_traverse(Traverse* traverse)
3424 { return Type::traverse(this->type_, traverse); }
3426 bool
3427 do_verify();
3429 bool
3430 do_has_pointer() const;
3432 bool
3433 do_compare_is_identity(Gogo*);
3435 bool
3436 do_is_reflexive();
3438 bool
3439 do_needs_key_update();
3441 bool
3442 do_in_heap()
3443 { return this->in_heap_ && this->type_->in_heap(); }
3445 unsigned int
3446 do_hash_for_method(Gogo*) const;
3448 Btype*
3449 do_get_backend(Gogo*);
3451 Expression*
3452 do_type_descriptor(Gogo*, Named_type*);
3454 void
3455 do_reflection(Gogo*, std::string*) const;
3457 void
3458 do_mangled_name(Gogo*, std::string* ret) const;
3460 void
3461 do_export(Export*) const;
3463 private:
3464 // Create the placeholder during conversion.
3465 void
3466 create_placeholder(Gogo*);
3468 // A pointer back to the Named_object for this type.
3469 Named_object* named_object_;
3470 // If this type is defined in a function, a pointer back to the
3471 // function in which it is defined.
3472 Named_object* in_function_;
3473 // The index of this type in IN_FUNCTION_.
3474 unsigned int in_function_index_;
3475 // The actual type.
3476 Type* type_;
3477 // The list of methods defined for this type. Any named type can
3478 // have methods.
3479 Bindings* local_methods_;
3480 // The full list of methods for this type, including methods
3481 // declared for anonymous fields.
3482 Methods* all_methods_;
3483 // A mapping from interfaces to the associated interface method
3484 // tables for this type.
3485 Interface_method_tables* interface_method_tables_;
3486 // A mapping from interfaces to the associated interface method
3487 // tables for pointers to this type.
3488 Interface_method_tables* pointer_interface_method_tables_;
3489 // The location where this type was defined.
3490 Location location_;
3491 // The backend representation of this type during backend
3492 // conversion. This is used to avoid endless recursion when a named
3493 // type refers to itself.
3494 Btype* named_btype_;
3495 // A list of types which must be converted to the backend
3496 // representation before this type can be converted. This is for
3497 // cases like
3498 // type S1 { p *S2 }
3499 // type S2 { s S1 }
3500 // where we can't convert S2 to the backend representation unless we
3501 // have converted S1.
3502 std::vector<Named_type*> dependencies_;
3503 // Whether this is an alias type.
3504 bool is_alias_;
3505 // Whether this type is visible. This is false if this type was
3506 // created because it was referenced by an imported object, but the
3507 // type itself was not exported. This will always be true for types
3508 // created in the current package.
3509 bool is_visible_;
3510 // Whether this type is erroneous.
3511 bool is_error_;
3512 // Whether this type is permitted in the heap. This is true by
3513 // default, false if there is a magic //go:notinheap comment.
3514 bool in_heap_;
3515 // Whether the current value of named_btype_ is a placeholder for
3516 // which the final size of the type is not known.
3517 bool is_placeholder_;
3518 // Whether this type has been converted to the backend
3519 // representation. Implies that is_placeholder_ is false.
3520 bool is_converted_;
3521 // Whether this is a pointer or function type which refers to the
3522 // type itself.
3523 bool is_circular_;
3524 // Whether this type has been verified.
3525 bool is_verified_;
3526 // In a recursive operation such as has_pointer, this flag is used
3527 // to prevent infinite recursion when a type refers to itself. This
3528 // is mutable because it is always reset to false when the function
3529 // exits.
3530 mutable bool seen_;
3531 // Like seen_, but used only by do_compare_is_identity.
3532 bool seen_in_compare_is_identity_;
3533 // Like seen_, but used only by do_get_backend.
3534 bool seen_in_get_backend_;
3535 // Like seen_, but used when resolving aliases.
3536 mutable bool seen_alias_;
3539 // A forward declaration. This handles a type which has been declared
3540 // but not defined.
3542 class Forward_declaration_type : public Type
3544 public:
3545 Forward_declaration_type(Named_object* named_object);
3547 // The named object associated with this type declaration. This
3548 // will be resolved.
3549 Named_object*
3550 named_object();
3552 const Named_object*
3553 named_object() const;
3555 // Return the name of the type.
3556 const std::string&
3557 name() const;
3559 // Return the type to which this points. Give an error if the type
3560 // has not yet been defined.
3561 Type*
3562 real_type();
3564 const Type*
3565 real_type() const;
3567 // Whether the base type has been defined.
3568 bool
3569 is_defined() const;
3571 // Add a method to this type.
3572 Named_object*
3573 add_method(const std::string& name, Function*);
3575 // Add a method declaration to this type.
3576 Named_object*
3577 add_method_declaration(const std::string& name, Package*, Function_type*,
3578 Location);
3580 // Add an already created object as a method to this type.
3581 void
3582 add_existing_method(Named_object*);
3584 protected:
3586 do_traverse(Traverse* traverse);
3588 bool
3589 do_verify();
3591 bool
3592 do_has_pointer() const
3593 { return this->real_type()->has_pointer(); }
3595 bool
3596 do_compare_is_identity(Gogo* gogo)
3597 { return this->real_type()->compare_is_identity(gogo); }
3599 bool
3600 do_is_reflexive()
3601 { return this->real_type()->is_reflexive(); }
3603 bool
3604 do_needs_key_update()
3605 { return this->real_type()->needs_key_update(); }
3607 bool
3608 do_in_heap()
3609 { return this->real_type()->in_heap(); }
3611 unsigned int
3612 do_hash_for_method(Gogo* gogo) const
3613 { return this->real_type()->hash_for_method(gogo); }
3615 Btype*
3616 do_get_backend(Gogo* gogo);
3618 Expression*
3619 do_type_descriptor(Gogo*, Named_type*);
3621 void
3622 do_reflection(Gogo*, std::string*) const;
3624 void
3625 do_mangled_name(Gogo*, std::string* ret) const;
3627 void
3628 do_export(Export*) const;
3630 private:
3631 // Issue a warning about a use of an undefined type.
3632 void
3633 warn() const;
3635 // The type declaration.
3636 Named_object* named_object_;
3637 // Whether we have issued a warning about this type.
3638 mutable bool warned_;
3641 // The Type_context struct describes what we expect for the type of an
3642 // expression.
3644 struct Type_context
3646 // The exact type we expect, if known. This may be NULL.
3647 Type* type;
3648 // Whether an abstract type is permitted.
3649 bool may_be_abstract;
3651 // Constructors.
3652 Type_context()
3653 : type(NULL), may_be_abstract(false)
3656 Type_context(Type* a_type, bool a_may_be_abstract)
3657 : type(a_type), may_be_abstract(a_may_be_abstract)
3661 #endif // !defined(GO_TYPES_H)