aix: Use lcomm for TLS static data.
[official-gcc.git] / gcc / go / gofrontend / types.h
blobf2880f9c5d80e098d61e93c524afcbfa227047b4
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 Backend_name;
53 class Btype;
54 class Bexpression;
55 class Bvariable;
57 // Type codes used in type descriptors. These must match the values
58 // in libgo/runtime/go-type.h. They also match the values in the gc
59 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
60 // although this is not required.
62 static const int RUNTIME_TYPE_KIND_BOOL = 1;
63 static const int RUNTIME_TYPE_KIND_INT = 2;
64 static const int RUNTIME_TYPE_KIND_INT8 = 3;
65 static const int RUNTIME_TYPE_KIND_INT16 = 4;
66 static const int RUNTIME_TYPE_KIND_INT32 = 5;
67 static const int RUNTIME_TYPE_KIND_INT64 = 6;
68 static const int RUNTIME_TYPE_KIND_UINT = 7;
69 static const int RUNTIME_TYPE_KIND_UINT8 = 8;
70 static const int RUNTIME_TYPE_KIND_UINT16 = 9;
71 static const int RUNTIME_TYPE_KIND_UINT32 = 10;
72 static const int RUNTIME_TYPE_KIND_UINT64 = 11;
73 static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
74 static const int RUNTIME_TYPE_KIND_FLOAT32 = 13;
75 static const int RUNTIME_TYPE_KIND_FLOAT64 = 14;
76 static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15;
77 static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16;
78 static const int RUNTIME_TYPE_KIND_ARRAY = 17;
79 static const int RUNTIME_TYPE_KIND_CHAN = 18;
80 static const int RUNTIME_TYPE_KIND_FUNC = 19;
81 static const int RUNTIME_TYPE_KIND_INTERFACE = 20;
82 static const int RUNTIME_TYPE_KIND_MAP = 21;
83 static const int RUNTIME_TYPE_KIND_PTR = 22;
84 static const int RUNTIME_TYPE_KIND_SLICE = 23;
85 static const int RUNTIME_TYPE_KIND_STRING = 24;
86 static const int RUNTIME_TYPE_KIND_STRUCT = 25;
87 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26;
89 static const int RUNTIME_TYPE_KIND_DIRECT_IFACE = (1 << 5);
90 static const int RUNTIME_TYPE_KIND_GC_PROG = (1 << 6);
91 static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7);
93 // To build the complete list of methods for a named type we need to
94 // gather all methods from anonymous fields. Those methods may
95 // require an arbitrary set of indirections and field offsets. There
96 // is also the possibility of ambiguous methods, which we could ignore
97 // except that we want to give a better error message for that case.
98 // This is a base class. There are two types of methods: named
99 // methods, and methods which are inherited from an anonymous field of
100 // interface type.
102 class Method
104 public:
105 // For methods in anonymous types we need to know the sequence of
106 // field references used to extract the pointer to pass to the
107 // method. Since each method for a particular anonymous field will
108 // have the sequence of field indexes, and since the indexes can be
109 // shared going down the chain, we use a manually managed linked
110 // list. The first entry in the list is the field index for the
111 // last field, the one passed to the method.
113 struct Field_indexes
115 const Field_indexes* next;
116 unsigned int field_index;
119 virtual ~Method()
122 // Get the list of field indexes.
123 const Field_indexes*
124 field_indexes() const
125 { return this->field_indexes_; }
127 // Get the depth.
128 unsigned int
129 depth() const
130 { return this->depth_; }
132 // Return whether this is a value method--a method which does not
133 // require a pointer expression.
134 bool
135 is_value_method() const
136 { return this->is_value_method_; }
138 // Return whether we need a stub method--this is true if we can't
139 // just pass the main object to the method.
140 bool
141 needs_stub_method() const
142 { return this->needs_stub_method_; }
144 // Return whether this is an ambiguous method name.
145 bool
146 is_ambiguous() const
147 { return this->is_ambiguous_; }
149 // Note that this method is ambiguous.
150 void
151 set_is_ambiguous()
152 { this->is_ambiguous_ = true; }
154 // Return the type of the method.
155 Function_type*
156 type() const
157 { return this->do_type(); }
159 // Return the location of the method receiver.
160 Location
161 receiver_location() const
162 { return this->do_receiver_location(); }
164 // Return an expression which binds this method to EXPR. This is
165 // something which can be used with a function call.
166 Expression*
167 bind_method(Expression* expr, Location location) const;
169 // Return the named object for this method. This may only be called
170 // after methods are finalized.
171 Named_object*
172 named_object() const;
174 // Get the stub object.
175 Named_object*
176 stub_object() const
178 go_assert(this->stub_ != NULL);
179 return this->stub_;
182 // Set the stub object.
183 void
184 set_stub_object(Named_object* no)
186 go_assert(this->stub_ == NULL);
187 this->stub_ = no;
190 // Get the direct interface method stub object.
191 Named_object*
192 iface_stub_object() const
194 go_assert(this->iface_stub_ != NULL);
195 return this->iface_stub_;
198 // Set the direct interface method stub object.
199 void
200 set_iface_stub_object(Named_object* no)
202 go_assert(this->iface_stub_ == NULL);
203 this->iface_stub_ = no;
206 // Return true if this method should not participate in any
207 // interfaces.
208 bool
209 nointerface() const
210 { return this->do_nointerface(); }
212 protected:
213 // These objects are only built by the child classes.
214 Method(const Field_indexes* field_indexes, unsigned int depth,
215 bool is_value_method, bool needs_stub_method)
216 : field_indexes_(field_indexes), depth_(depth), stub_(NULL), iface_stub_(NULL),
217 is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
218 is_ambiguous_(false)
221 // The named object for this method.
222 virtual Named_object*
223 do_named_object() const = 0;
225 // The type of the method.
226 virtual Function_type*
227 do_type() const = 0;
229 // Return the location of the method receiver.
230 virtual Location
231 do_receiver_location() const = 0;
233 // Bind a method to an object.
234 virtual Expression*
235 do_bind_method(Expression* expr, Location location) const = 0;
237 // Return whether this method should not participate in interfaces.
238 virtual bool
239 do_nointerface() const = 0;
241 private:
242 // The sequence of field indexes used for this method. If this is
243 // NULL, then the method is defined for the current type.
244 const Field_indexes* field_indexes_;
245 // The depth at which this method was found.
246 unsigned int depth_;
247 // If a stub method is required, this is its object. This is only
248 // set after stub methods are built in finalize_methods.
249 Named_object* stub_;
250 // Stub object for direct interface type. This is only set after
251 // stub methods are built in finalize_methods.
252 Named_object* iface_stub_;
253 // Whether this is a value method--a method that does not require a
254 // pointer.
255 bool is_value_method_;
256 // Whether a stub method is required.
257 bool needs_stub_method_;
258 // Whether this method is ambiguous.
259 bool is_ambiguous_;
262 // A named method. This is what you get with a method declaration,
263 // either directly on the type, or inherited from some anonymous
264 // embedded field.
266 class Named_method : public Method
268 public:
269 Named_method(Named_object* named_object, const Field_indexes* field_indexes,
270 unsigned int depth, bool is_value_method,
271 bool needs_stub_method)
272 : Method(field_indexes, depth, is_value_method, needs_stub_method),
273 named_object_(named_object)
276 protected:
277 // Get the Named_object for the method.
278 Named_object*
279 do_named_object() const
280 { return this->named_object_; }
282 // The type of the method.
283 Function_type*
284 do_type() const;
286 // Return the location of the method receiver.
287 Location
288 do_receiver_location() const;
290 // Bind a method to an object.
291 Expression*
292 do_bind_method(Expression* expr, Location location) const;
294 // Return whether this method should not participate in interfaces.
295 bool
296 do_nointerface() const;
298 private:
299 // The method itself. For a method which needs a stub, this starts
300 // out as the underlying method, and is later replaced with the stub
301 // method.
302 Named_object* named_object_;
305 // An interface method. This is used when an interface appears as an
306 // anonymous field in a named struct.
308 class Interface_method : public Method
310 public:
311 Interface_method(const std::string& name, Location location,
312 Function_type* fntype, const Field_indexes* field_indexes,
313 unsigned int depth)
314 : Method(field_indexes, depth, true, true),
315 name_(name), location_(location), fntype_(fntype)
318 protected:
319 // Get the Named_object for the method. This should never be
320 // called, as we always create a stub.
321 Named_object*
322 do_named_object() const
323 { go_unreachable(); }
325 // The type of the method.
326 Function_type*
327 do_type() const
328 { return this->fntype_; }
330 // Return the location of the method receiver.
331 Location
332 do_receiver_location() const
333 { return this->location_; }
335 // Bind a method to an object.
336 Expression*
337 do_bind_method(Expression* expr, Location location) const;
339 // Return whether this method should not participate in interfaces.
340 bool
341 do_nointerface() const
342 { return false; }
344 private:
345 // The name of the interface method to call.
346 std::string name_;
347 // The location of the definition of the interface method.
348 Location location_;
349 // The type of the interface method.
350 Function_type* fntype_;
353 // A mapping from method name to Method. This is a wrapper around a
354 // hash table.
356 class Methods
358 private:
359 typedef Unordered_map(std::string, Method*) Method_map;
361 public:
362 typedef Method_map::const_iterator const_iterator;
364 Methods()
365 : methods_()
368 // Insert a new method. Returns true if it was inserted, false if
369 // it was overidden or ambiguous.
370 bool
371 insert(const std::string& name, Method* m);
373 // The number of (unambiguous) methods.
374 size_t
375 count() const;
377 // Iterate.
378 const_iterator
379 begin() const
380 { return this->methods_.begin(); }
382 const_iterator
383 end() const
384 { return this->methods_.end(); }
386 // Lookup.
387 const_iterator
388 find(const std::string& name) const
389 { return this->methods_.find(name); }
391 bool
392 empty() const
393 { return this->methods_.empty(); }
395 private:
396 Method_map methods_;
399 // The base class for all types.
401 class Type
403 public:
404 // The types of types.
405 enum Type_classification
407 TYPE_ERROR,
408 TYPE_VOID,
409 TYPE_BOOLEAN,
410 TYPE_INTEGER,
411 TYPE_FLOAT,
412 TYPE_COMPLEX,
413 TYPE_STRING,
414 TYPE_SINK,
415 TYPE_FUNCTION,
416 TYPE_POINTER,
417 TYPE_NIL,
418 TYPE_CALL_MULTIPLE_RESULT,
419 TYPE_STRUCT,
420 TYPE_ARRAY,
421 TYPE_MAP,
422 TYPE_CHANNEL,
423 TYPE_INTERFACE,
424 TYPE_NAMED,
425 TYPE_FORWARD
428 virtual ~Type();
430 // Creators.
432 static Type*
433 make_error_type();
435 static Type*
436 make_void_type();
438 // Get the unnamed bool type.
439 static Type*
440 make_boolean_type();
442 // Get the named type "bool".
443 static Named_type*
444 lookup_bool_type();
446 // Make the named type "bool".
447 static Named_type*
448 make_named_bool_type();
450 // Make an abstract integer type.
451 static Integer_type*
452 make_abstract_integer_type();
454 // Make an abstract type for a character constant.
455 static Integer_type*
456 make_abstract_character_type();
458 // Make a named integer type with a specified size.
459 // RUNTIME_TYPE_KIND is the code to use in reflection information,
460 // to distinguish int and int32.
461 static Named_type*
462 make_integer_type(const char* name, bool is_unsigned, int bits,
463 int runtime_type_kind);
465 // Make a named integer type alias. This is used for byte and rune.
466 static Named_type*
467 make_integer_type_alias(const char* name, Named_type* real_type);
469 // Look up a named integer type.
470 static Named_type*
471 lookup_integer_type(const char* name);
473 // Make an abstract floating point type.
474 static Float_type*
475 make_abstract_float_type();
477 // Make a named floating point type with a specific size.
478 // RUNTIME_TYPE_KIND is the code to use in reflection information,
479 // to distinguish float and float32.
480 static Named_type*
481 make_float_type(const char* name, int bits, int runtime_type_kind);
483 // Look up a named float type.
484 static Named_type*
485 lookup_float_type(const char* name);
487 // Make an abstract complex type.
488 static Complex_type*
489 make_abstract_complex_type();
491 // Make a named complex type with a specific size.
492 // RUNTIME_TYPE_KIND is the code to use in reflection information,
493 // to distinguish complex and complex64.
494 static Named_type*
495 make_complex_type(const char* name, int bits, int runtime_type_kind);
497 // Look up a named complex type.
498 static Named_type*
499 lookup_complex_type(const char* name);
501 // Get the unnamed string type.
502 static Type*
503 make_string_type();
505 // Get the named type "string".
506 static Named_type*
507 lookup_string_type();
509 // Make the named type "string".
510 static Named_type*
511 make_named_string_type();
513 static Type*
514 make_sink_type();
516 static Function_type*
517 make_function_type(Typed_identifier* receiver,
518 Typed_identifier_list* parameters,
519 Typed_identifier_list* results,
520 Location);
522 static Backend_function_type*
523 make_backend_function_type(Typed_identifier* receiver,
524 Typed_identifier_list* parameters,
525 Typed_identifier_list* results,
526 Location);
528 static Pointer_type*
529 make_pointer_type(Type*);
531 static void
532 finish_pointer_types(Gogo* gogo);
534 static Type*
535 make_nil_type();
537 static Type*
538 make_call_multiple_result_type(Call_expression*);
540 static Struct_type*
541 make_struct_type(Struct_field_list* fields, Location);
543 static Array_type*
544 make_array_type(Type* element_type, Expression* length);
546 static Map_type*
547 make_map_type(Type* key_type, Type* value_type, Location);
549 static Channel_type*
550 make_channel_type(bool send, bool receive, Type*);
552 static Interface_type*
553 make_interface_type(Typed_identifier_list* methods, Location);
555 static Interface_type*
556 make_empty_interface_type(Location);
558 static Type*
559 make_type_descriptor_type();
561 static Type*
562 make_type_descriptor_ptr_type();
564 static Named_type*
565 make_named_type(Named_object*, Type*, Location);
567 static Type*
568 make_forward_declaration(Named_object*);
570 // Make a builtin struct type from a list of fields.
571 static Struct_type*
572 make_builtin_struct_type(int nfields, ...);
574 // Make a builtin named type.
575 static Named_type*
576 make_builtin_named_type(const char* name, Type* type);
578 // Traverse a type.
579 static int
580 traverse(Type*, Traverse*);
582 // Verify the type. This is called after parsing, and verifies that
583 // types are complete and meet the language requirements. This
584 // returns false if the type is invalid and we should not continue
585 // traversing it.
586 bool
587 verify()
588 { return this->do_verify(); }
590 // Bit flags to pass to are_identical and friends.
592 // Treat error types as their own distinct type. Sometimes we
593 // ignore error types--treat them as identical to every other
594 // type--to avoid cascading errors.
595 static const int COMPARE_ERRORS = 1;
597 // Compare struct field tags when comparing structs. We ignore
598 // struct field tags for purposes of type conversion.
599 static const int COMPARE_TAGS = 2;
601 // Compare aliases: treat an alias to T as distinct from T.
602 static const int COMPARE_ALIASES = 4;
604 // When comparing interface types compare the interface embedding heirarchy,
605 // if any, rather than only comparing method sets. Useful primarily when
606 // exporting types.
607 static const int COMPARE_EMBEDDED_INTERFACES = 8;
609 // Return true if two types are identical. If this returns false,
610 // and REASON is not NULL, it may set *REASON.
611 static bool
612 are_identical(const Type* lhs, const Type* rhs, int flags,
613 std::string* reason);
615 // Return true if two types are compatible for use in a binary
616 // operation, other than a shift, comparison, or channel send. This
617 // is an equivalence relation.
618 static bool
619 are_compatible_for_binop(const Type* t1, const Type* t2);
621 // Return true if two types are compatible for use with the
622 // comparison operator. IS_EQUALITY_OP is true if this is an
623 // equality comparison, false if it is an ordered comparison. This
624 // is an equivalence relation. If this returns false, and REASON is
625 // not NULL, it sets *REASON.
626 static bool
627 are_compatible_for_comparison(bool is_equality_op, const Type *t1,
628 const Type *t2, std::string* reason);
630 // Return true if a type is comparable with itself. This is true of
631 // most types, but false for, e.g., function types.
632 bool
633 is_comparable() const
634 { return Type::are_compatible_for_comparison(true, this, this, NULL); }
636 // Return true if a value with type RHS is assignable to a variable
637 // with type LHS. This is not an equivalence relation. If this
638 // returns false, and REASON is not NULL, it sets *REASON.
639 static bool
640 are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
642 // Return true if a value with type RHS may be converted to type
643 // LHS. If this returns false, and REASON is not NULL, it sets
644 // *REASON.
645 static bool
646 are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
648 // Return true if values of this type can be compared using an
649 // identity function which gets nothing but a pointer to the value
650 // and a size.
651 bool
652 compare_is_identity(Gogo* gogo)
653 { return this->do_compare_is_identity(gogo); }
655 // Return whether values of this type are reflexive: if a comparison
656 // of a value with itself always returns true.
657 bool
658 is_reflexive()
659 { return this->do_is_reflexive(); }
661 // Return whether values of this, when used as a key in map,
662 // requires the key to be updated when an assignment is made.
663 bool
664 needs_key_update()
665 { return this->do_needs_key_update(); }
667 // Return whether the hash function of this type might panic. This
668 // is only called for types used as a key in a map type.
669 bool
670 hash_might_panic()
671 { return this->do_hash_might_panic(); }
673 // Whether the type is permitted in the heap.
674 bool
675 in_heap() const
676 { return this->do_in_heap(); }
678 // Return a hash code for this type for the method hash table.
679 // Types which are equivalent according to are_identical will have
680 // the same hash code.
681 unsigned int
682 hash_for_method(Gogo*, int) const;
684 // Return the type classification.
685 Type_classification
686 classification() const
687 { return this->classification_; }
689 // Return the base type for this type. This looks through forward
690 // declarations and names. Using this with a forward declaration
691 // which has not been defined will return an error type.
692 Type*
693 base();
695 const Type*
696 base() const;
698 // Return the type skipping defined forward declarations. If this
699 // type is a forward declaration which has not been defined, it will
700 // return the Forward_declaration_type. This differs from base() in
701 // that it will return a Named_type, and for a
702 // Forward_declaration_type which is not defined it will return that
703 // type rather than an error type.
704 Type*
705 forwarded();
707 const Type*
708 forwarded() const;
710 // Return the type skipping any alias definitions and any defined
711 // forward declarations. This is like forwarded, but also
712 // recursively expands alias definitions to the aliased type.
713 Type*
714 unalias();
716 const Type*
717 unalias() const;
719 // Return true if this is a basic type: a type which is not composed
720 // of other types, and is not void.
721 bool
722 is_basic_type() const;
724 // Return true if this is an abstract type--an integer, floating
725 // point, or complex type whose size has not been determined.
726 bool
727 is_abstract() const;
729 // Return a non-abstract version of an abstract type.
730 Type*
731 make_non_abstract_type();
733 // Return true if this type is or contains a pointer. This
734 // determines whether the garbage collector needs to look at a value
735 // of this type.
736 bool
737 has_pointer() const
738 { return this->do_has_pointer(); }
740 // Return true if this is the error type. This returns false for a
741 // type which is not defined, as it is called by the parser before
742 // all types are defined.
743 bool
744 is_error_type() const;
746 // Return true if this is the error type or if the type is
747 // undefined. If the type is undefined, this will give an error.
748 // This should only be called after parsing is complete.
749 bool
750 is_error() const
751 { return this->base()->is_error_type(); }
753 // Return true if this is a void type.
754 bool
755 is_void_type() const
756 { return this->classification_ == TYPE_VOID; }
758 // If this is an integer type, return the Integer_type. Otherwise,
759 // return NULL. This is a controlled dynamic_cast.
760 Integer_type*
761 integer_type()
762 { return this->convert<Integer_type, TYPE_INTEGER>(); }
764 const Integer_type*
765 integer_type() const
766 { return this->convert<const Integer_type, TYPE_INTEGER>(); }
768 // If this is a floating point type, return the Float_type.
769 // Otherwise, return NULL. This is a controlled dynamic_cast.
770 Float_type*
771 float_type()
772 { return this->convert<Float_type, TYPE_FLOAT>(); }
774 const Float_type*
775 float_type() const
776 { return this->convert<const Float_type, TYPE_FLOAT>(); }
778 // If this is a complex type, return the Complex_type. Otherwise,
779 // return NULL.
780 Complex_type*
781 complex_type()
782 { return this->convert<Complex_type, TYPE_COMPLEX>(); }
784 const Complex_type*
785 complex_type() const
786 { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
788 // Return whether this is a numeric type.
789 bool
790 is_numeric_type() const
792 Type_classification tc = this->base()->classification_;
793 return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX;
796 // Return true if this is a boolean type.
797 bool
798 is_boolean_type() const
799 { return this->base()->classification_ == TYPE_BOOLEAN; }
801 // Return true if this is an abstract boolean type.
802 bool
803 is_abstract_boolean_type() const
804 { return this->classification_ == TYPE_BOOLEAN; }
806 // Return true if this is a string type.
807 bool
808 is_string_type() const
809 { return this->base()->classification_ == TYPE_STRING; }
811 // Return true if this is an abstract string type.
812 bool
813 is_abstract_string_type() const
814 { return this->classification_ == TYPE_STRING; }
816 // Return true if this is the sink type. This is the type of the
817 // blank identifier _.
818 bool
819 is_sink_type() const
820 { return this->base()->classification_ == TYPE_SINK; }
822 // If this is a function type, return it. Otherwise, return NULL.
823 Function_type*
824 function_type()
825 { return this->convert<Function_type, TYPE_FUNCTION>(); }
827 const Function_type*
828 function_type() const
829 { return this->convert<const Function_type, TYPE_FUNCTION>(); }
831 // If this is a pointer type, return the type to which it points.
832 // Otherwise, return NULL.
833 Type*
834 points_to() const;
836 // If this is a pointer type, return the type to which it points.
837 // Otherwise, return the type itself.
838 Type*
839 deref()
841 Type* pt = this->points_to();
842 return pt != NULL ? pt : this;
845 const Type*
846 deref() const
848 const Type* pt = this->points_to();
849 return pt != NULL ? pt : this;
852 // Return true if this is the nil type. We don't use base() here,
853 // because this can be called during parse, and there is no way to
854 // name the nil type anyhow.
855 bool
856 is_nil_type() const
857 { return this->classification_ == TYPE_NIL; }
859 // Return true if this is the predeclared constant nil being used as
860 // a type. This is what the parser produces for type switches which
861 // use "case nil".
862 bool
863 is_nil_constant_as_type() const;
865 // Return true if this is the return type of a function which
866 // returns multiple values.
867 bool
868 is_call_multiple_result_type() const
869 { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
871 // If this is a struct type, return it. Otherwise, return NULL.
872 Struct_type*
873 struct_type()
874 { return this->convert<Struct_type, TYPE_STRUCT>(); }
876 const Struct_type*
877 struct_type() const
878 { return this->convert<const Struct_type, TYPE_STRUCT>(); }
880 // If this is an array type, return it. Otherwise, return NULL.
881 Array_type*
882 array_type()
883 { return this->convert<Array_type, TYPE_ARRAY>(); }
885 const Array_type*
886 array_type() const
887 { return this->convert<const Array_type, TYPE_ARRAY>(); }
889 // Return whether if this is a slice type.
890 bool
891 is_slice_type() const;
893 // If this is a map type, return it. Otherwise, return NULL.
894 Map_type*
895 map_type()
896 { return this->convert<Map_type, TYPE_MAP>(); }
898 const Map_type*
899 map_type() const
900 { return this->convert<const Map_type, TYPE_MAP>(); }
902 // If this is a channel type, return it. Otherwise, return NULL.
903 Channel_type*
904 channel_type()
905 { return this->convert<Channel_type, TYPE_CHANNEL>(); }
907 const Channel_type*
908 channel_type() const
909 { return this->convert<const Channel_type, TYPE_CHANNEL>(); }
911 // If this is an interface type, return it. Otherwise, return NULL.
912 Interface_type*
913 interface_type()
914 { return this->convert<Interface_type, TYPE_INTERFACE>(); }
916 const Interface_type*
917 interface_type() const
918 { return this->convert<const Interface_type, TYPE_INTERFACE>(); }
920 // If this is a named type, return it. Otherwise, return NULL.
921 Named_type*
922 named_type();
924 const Named_type*
925 named_type() const;
927 // If this is a forward declaration, return it. Otherwise, return
928 // NULL.
929 Forward_declaration_type*
930 forward_declaration_type()
931 { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
933 const Forward_declaration_type*
934 forward_declaration_type() const
936 return this->convert_no_base<const Forward_declaration_type,
937 TYPE_FORWARD>();
940 // Return true if this type is not yet defined.
941 bool
942 is_undefined() const;
944 // Return true if this is the unsafe.pointer type. We currently
945 // represent that as pointer-to-void.
946 bool
947 is_unsafe_pointer_type() const
948 { return this->points_to() != NULL && this->points_to()->is_void_type(); }
950 // Return whether this type is stored directly in an interface's
951 // data word.
952 bool
953 is_direct_iface_type() const;
955 // Return a version of this type with any expressions copied, but
956 // only if copying the expressions will affect the size of the type.
957 // If there are no such expressions in the type (expressions can
958 // only occur in array types), just return the same type. If any
959 // expressions can not affect the size of the type, just return the
960 // same type.
961 Type*
962 copy_expressions();
964 // Look for field or method NAME for TYPE. Return an expression for
965 // it, bound to EXPR.
966 static Expression*
967 bind_field_or_method(Gogo*, const Type* type, Expression* expr,
968 const std::string& name, Location);
970 // Return true if NAME is an unexported field or method of TYPE.
971 static bool
972 is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
973 std::vector<const Named_type*>*);
975 // Convert the builtin named types.
976 static void
977 convert_builtin_named_types(Gogo*);
979 // Return the backend representation of this type.
980 Btype*
981 get_backend(Gogo*);
983 // Return a placeholder for the backend representation of the type.
984 // This will return a type of the correct size, but for which some
985 // of the fields may still need to be completed.
986 Btype*
987 get_backend_placeholder(Gogo*);
989 // Finish the backend representation of a placeholder.
990 void
991 finish_backend(Gogo*, Btype*);
993 // Build a type descriptor entry for this type. Return a pointer to
994 // it. The location is the location which causes us to need the
995 // entry.
996 Bexpression*
997 type_descriptor_pointer(Gogo* gogo, Location);
999 // Build the Garbage Collection symbol for this type. Return a pointer to it.
1000 Bexpression*
1001 gc_symbol_pointer(Gogo* gogo);
1003 // Return whether this type needs a garbage collection program.
1004 // Sets *PTRSIZE and *PTRDATA.
1005 bool
1006 needs_gcprog(Gogo*, int64_t* ptrsize, int64_t* ptrdata);
1008 // Return a ptrmask variable for this type.
1009 Bvariable*
1010 gc_ptrmask_var(Gogo*, int64_t ptrsize, int64_t ptrdata);
1012 // Return the type reflection string for this type.
1013 std::string
1014 reflection(Gogo*) const;
1016 // Add the backend name for the type to BNAME. This will add one or
1017 // two name components. Identical types should have the same
1018 // backend name.
1019 void
1020 backend_name(Gogo*, Backend_name* bname) const;
1022 // If the size of the type can be determined, set *PSIZE to the size
1023 // in bytes and return true. Otherwise, return false. This queries
1024 // the backend.
1025 bool
1026 backend_type_size(Gogo*, int64_t* psize);
1028 // If the alignment of the type can be determined, set *PALIGN to
1029 // the alignment in bytes and return true. Otherwise, return false.
1030 bool
1031 backend_type_align(Gogo*, int64_t* palign);
1033 // If the alignment of a struct field of this type can be
1034 // determined, set *PALIGN to the alignment in bytes and return
1035 // true. Otherwise, return false.
1036 bool
1037 backend_type_field_align(Gogo*, int64_t* palign);
1039 // Determine the ptrdata size for the backend version of this type:
1040 // the length of the prefix of the type that can contain a pointer
1041 // value. If it can be determined, set *PPTRDATA to the value in
1042 // bytes and return true. Otherwise, return false.
1043 bool
1044 backend_type_ptrdata(Gogo*, int64_t* pptrdata);
1046 // Determine the ptrdata size that we are going to set in the type
1047 // descriptor. This is normally the same as backend_type_ptrdata,
1048 // but differs if we use a gcprog for an array. The arguments and
1049 // results are as for backend_type_ptrdata.
1050 bool
1051 descriptor_ptrdata(Gogo*, int64_t* pptrdata);
1053 // Whether the backend size is known.
1054 bool
1055 is_backend_type_size_known(Gogo*);
1057 // Return whether the type needs specially built type functions.
1058 bool
1059 needs_specific_type_functions(Gogo*);
1061 // Get the equality function for a type. Returns NULL if the type
1062 // is not comparable.
1063 Named_object*
1064 equal_function(Gogo*, Named_type* name, Function_type* equal_fntype);
1066 // Get the hash function for a type. Returns NULL if the type is
1067 // not comparable.
1068 Named_object*
1069 hash_function(Gogo*, Function_type* hash_fntype);
1071 // Write the equal function for a type.
1072 void
1073 write_equal_function(Gogo*, Named_type*, int64_t size,
1074 const Backend_name*, Function_type* equal_fntype);
1076 // Write the hash function for a type.
1077 void
1078 write_hash_function(Gogo*, int64_t size, const Backend_name*,
1079 Function_type* hash_fntype);
1081 // Return the alignment required by the memequalN function.
1082 static int64_t memequal_align(Gogo*, int size);
1084 // Export the type.
1085 void
1086 export_type(Export* exp) const
1087 { this->do_export(exp); }
1089 // Import a type.
1090 static Type*
1091 import_type(Import*);
1093 protected:
1094 Type(Type_classification);
1096 // Functions implemented by the child class.
1098 // Traverse the subtypes.
1099 virtual int
1100 do_traverse(Traverse*);
1102 // Verify the type.
1103 virtual bool
1104 do_verify()
1105 { return true; }
1107 virtual bool
1108 do_has_pointer() const
1109 { return false; }
1111 virtual bool
1112 do_compare_is_identity(Gogo*) = 0;
1114 virtual bool
1115 do_is_reflexive()
1116 { return true; }
1118 virtual bool
1119 do_needs_key_update()
1120 { return false; }
1122 virtual bool
1123 do_hash_might_panic()
1124 { return false; }
1126 virtual bool
1127 do_in_heap() const
1128 { return true; }
1130 virtual unsigned int
1131 do_hash_for_method(Gogo*, int) const;
1133 virtual Btype*
1134 do_get_backend(Gogo*) = 0;
1136 virtual Expression*
1137 do_type_descriptor(Gogo*, Named_type* name) = 0;
1139 virtual void
1140 do_reflection(Gogo*, std::string*) const = 0;
1142 virtual void
1143 do_mangled_name(Gogo*, std::string*, bool*) const = 0;
1145 virtual void
1146 do_export(Export*) const;
1148 // For children to call when they detect that they are in error.
1149 void
1150 set_is_error();
1152 // Return whether a method expects a pointer as the receiver.
1153 static bool
1154 method_expects_pointer(const Named_object*);
1156 // Finalize the methods for a type.
1157 static void
1158 finalize_methods(Gogo*, const Type*, Location, Methods**);
1160 // Return a method from a set of methods.
1161 static Method*
1162 method_function(const Methods*, const std::string& name,
1163 bool* is_ambiguous);
1165 // A mapping from interfaces to the associated interface method
1166 // tables for this type. This maps to a decl.
1167 typedef Unordered_map_hash(Interface_type*, Expression*, Type_hash_identical,
1168 Type_identical) Interface_method_tables;
1170 // Return a pointer to the interface method table for TYPE for the
1171 // interface INTERFACE.
1172 static Expression*
1173 interface_method_table(Type* type,
1174 Interface_type *interface, bool is_pointer,
1175 Interface_method_tables** method_tables,
1176 Interface_method_tables** pointer_tables);
1178 // Return a composite literal for the type descriptor entry for a
1179 // type.
1180 static Expression*
1181 type_descriptor(Gogo*, Type*);
1183 // Return a composite literal for the type descriptor entry for
1184 // TYPE, using NAME as the name of the type.
1185 static Expression*
1186 named_type_descriptor(Gogo*, Type* type, Named_type* name);
1188 // Return a composite literal for a plain type descriptor for this
1189 // type with the given kind and name.
1190 Expression*
1191 plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
1193 // Build a composite literal for the basic type descriptor.
1194 Expression*
1195 type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
1196 const Methods*, bool only_value_methods);
1198 // For the benefit of child class reflection string generation.
1199 void
1200 append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
1201 { type->do_reflection(gogo, ret); }
1203 // For the benefit of child class mangling.
1204 void
1205 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret,
1206 bool *is_non_identifier) const
1207 { type->do_mangled_name(gogo, ret, is_non_identifier); }
1209 // Return the backend representation for the underlying type of a
1210 // named type.
1211 static Btype*
1212 get_named_base_btype(Gogo* gogo, Type* base_type)
1213 { return base_type->get_btype_without_hash(gogo); }
1215 private:
1216 // Convert to the desired type classification, or return NULL. This
1217 // is a controlled dynamic_cast.
1218 template<typename Type_class, Type_classification type_classification>
1219 Type_class*
1220 convert()
1222 Type* base = this->base();
1223 return (base->classification_ == type_classification
1224 ? static_cast<Type_class*>(base)
1225 : NULL);
1228 template<typename Type_class, Type_classification type_classification>
1229 const Type_class*
1230 convert() const
1232 const Type* base = this->base();
1233 return (base->classification_ == type_classification
1234 ? static_cast<Type_class*>(base)
1235 : NULL);
1238 template<typename Type_class, Type_classification type_classification>
1239 Type_class*
1240 convert_no_base()
1242 return (this->classification_ == type_classification
1243 ? static_cast<Type_class*>(this)
1244 : NULL);
1247 template<typename Type_class, Type_classification type_classification>
1248 const Type_class*
1249 convert_no_base() const
1251 return (this->classification_ == type_classification
1252 ? static_cast<Type_class*>(this)
1253 : NULL);
1256 // Map unnamed types to type descriptor decls.
1257 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
1258 Type_identical) Type_descriptor_vars;
1260 static Type_descriptor_vars type_descriptor_vars;
1262 // Build the type descriptor variable for this type.
1263 void
1264 make_type_descriptor_var(Gogo*);
1266 // Map unnamed types to type descriptor decls.
1267 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
1268 Type_identical) GC_symbol_vars;
1270 static GC_symbol_vars gc_symbol_vars;
1272 // Map ptrmask symbol names to the ptrmask variable.
1273 typedef Unordered_map(std::string, Bvariable*) GC_gcbits_vars;
1275 static GC_gcbits_vars gc_gcbits_vars;
1277 // Build the GC symbol for this type.
1278 void
1279 make_gc_symbol_var(Gogo*);
1281 // Return true if the type descriptor for this type should be
1282 // defined in some other package. If NAME is not NULL, it is the
1283 // name of this type. If this returns true it sets *PACKAGE to the
1284 // package where the type descriptor is defined.
1285 bool
1286 type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
1288 // Make a composite literal for the garbage collection program for
1289 // this type.
1290 Expression*
1291 gcprog_constructor(Gogo*, int64_t ptrsize, int64_t ptrdata);
1293 // Build the hash function for a type that needs specific functions.
1294 Named_object*
1295 build_hash_function(Gogo*, int64_t size, Function_type* hash_fntype);
1297 // Build the equal function for a type that needs specific functions.
1298 Named_object*
1299 build_equal_function(Gogo*, Named_type*, int64_t size,
1300 Function_type* equal_fntype);
1302 void
1303 write_identity_hash(Gogo*, int64_t size);
1305 void
1306 write_identity_equal(Gogo*, int64_t size);
1308 void
1309 write_named_equal(Gogo*, Named_type*);
1311 // Build a composite literal for the uncommon type information.
1312 Expression*
1313 uncommon_type_constructor(Gogo*, Type* uncommon_type,
1314 Named_type*, const Methods*,
1315 bool only_value_methods) const;
1317 // Build a composite literal for the methods.
1318 Expression*
1319 methods_constructor(Gogo*, Type* methods_type, const Methods*,
1320 bool only_value_methods) const;
1322 // Build a composite literal for one method.
1323 Expression*
1324 method_constructor(Gogo*, Type* method_type, const std::string& name,
1325 const Method*, bool only_value_methods) const;
1327 // Add all methods for TYPE to the list of methods for THIS.
1328 static void
1329 add_methods_for_type(const Type* type, const Method::Field_indexes*,
1330 unsigned int depth, bool, bool,
1331 std::vector<const Named_type*>*,
1332 Methods*);
1334 static void
1335 add_local_methods_for_type(const Named_type* type,
1336 const Method::Field_indexes*,
1337 unsigned int depth, bool, bool, Methods*);
1339 static void
1340 add_embedded_methods_for_type(const Type* type,
1341 const Method::Field_indexes*,
1342 unsigned int depth, bool, bool,
1343 std::vector<const Named_type*>*,
1344 Methods*);
1346 static void
1347 add_interface_methods_for_type(const Type* type,
1348 const Method::Field_indexes*,
1349 unsigned int depth, Methods*);
1351 // Build stub methods for a type.
1352 static void
1353 build_stub_methods(Gogo*, const Type* type, const Methods* methods,
1354 Location);
1356 static void
1357 build_one_stub_method(Gogo*, Method*, const char* receiver_name,
1358 const Typed_identifier_list*, bool is_varargs,
1359 Location);
1361 // Build direct interface stub methods for a type.
1362 static void
1363 build_direct_iface_stub_methods(Gogo*, const Type*, Methods*, Location);
1365 static void
1366 build_one_iface_stub_method(Gogo*, Method*, const char*,
1367 const Typed_identifier_list*,
1368 bool, Location);
1370 static Expression*
1371 apply_field_indexes(Expression*, const Method::Field_indexes*,
1372 Location);
1374 // Look for a field or method named NAME in TYPE.
1375 static bool
1376 find_field_or_method(const Type* type, const std::string& name,
1377 bool receiver_can_be_pointer,
1378 std::vector<const Named_type*>*, int* level,
1379 bool* is_method, bool* found_pointer_method,
1380 std::string* ambig1, std::string* ambig2);
1382 // Helper function for is_direct_iface_type, to prevent infinite
1383 // recursion.
1384 bool
1385 is_direct_iface_type_helper(Unordered_set(const Type*)*) const;
1387 // Get the backend representation for a type without looking in the
1388 // hash table for identical types.
1389 Btype*
1390 get_btype_without_hash(Gogo*);
1392 // A backend type that may be a placeholder.
1393 struct Type_btype_entry
1395 Btype *btype;
1396 bool is_placeholder;
1399 // A mapping from Type to Btype*, used to ensure that the backend
1400 // representation of identical types is identical. This is only
1401 // used for unnamed types.
1402 typedef Unordered_map_hash(const Type*, Type_btype_entry,
1403 Type_hash_identical, Type_identical) Type_btypes;
1405 static Type_btypes type_btypes;
1407 // A list of builtin named types.
1408 static std::vector<Named_type*> named_builtin_types;
1410 // A map from types that need a specific hash or equality function
1411 // to the hash or equality function.
1412 typedef Unordered_map_hash(const Type*, Named_object*, Type_hash_identical,
1413 Type_identical) Type_function;
1415 static Type_function type_hash_functions_table;
1416 static Type_function type_equal_functions_table;
1418 // Cache for reusing existing pointer types; maps from pointed-to-type
1419 // to pointer type.
1420 typedef Unordered_map(Type*, Pointer_type*) Pointer_type_table;
1422 static Pointer_type_table pointer_types;
1424 // List of placeholder pointer types.
1425 static std::vector<Type*> placeholder_pointers;
1427 // The type classification.
1428 Type_classification classification_;
1429 // The backend representation of the type, once it has been
1430 // determined.
1431 Btype* btype_;
1432 // The type descriptor for this type. This starts out as NULL and
1433 // is filled in as needed.
1434 Bvariable* type_descriptor_var_;
1435 // The GC symbol for this type. This starts out as NULL and
1436 // is filled in as needed.
1437 Bvariable* gc_symbol_var_;
1440 // Type hash table operations, treating aliases as identical to the
1441 // types that they alias.
1443 class Type_hash_identical
1445 public:
1446 unsigned int
1447 operator()(const Type* type) const
1449 return type->hash_for_method(NULL,
1450 Type::COMPARE_ERRORS | Type::COMPARE_TAGS);
1454 class Type_identical
1456 public:
1457 bool
1458 operator()(const Type* t1, const Type* t2) const
1460 return Type::are_identical(t1, t2,
1461 Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
1462 NULL);
1466 // An identifier with a type.
1468 class Typed_identifier
1470 public:
1471 Typed_identifier(const std::string& name, Type* type,
1472 Location location)
1473 : name_(name), type_(type), location_(location), note_(NULL)
1476 // Get the name.
1477 const std::string&
1478 name() const
1479 { return this->name_; }
1481 // Get the type.
1482 Type*
1483 type() const
1484 { return this->type_; }
1486 // Return the location where the name was seen. This is not always
1487 // meaningful.
1488 Location
1489 location() const
1490 { return this->location_; }
1492 // Set the type--sometimes we see the identifier before the type.
1493 void
1494 set_type(Type* type)
1496 go_assert(this->type_ == NULL || type->is_error_type());
1497 this->type_ = type;
1500 // Get the escape note.
1501 std::string*
1502 note() const
1503 { return this->note_; }
1505 // Set the escape note.
1506 void
1507 set_note(const std::string& note)
1509 if (this->note_ != NULL)
1510 go_assert(*this->note_ == note);
1511 else
1512 this->note_ = new std::string(note);
1515 private:
1516 // Identifier name.
1517 std::string name_;
1518 // Type.
1519 Type* type_;
1520 // The location where the name was seen.
1521 Location location_;
1522 // Escape note for this typed identifier. Used when importing and exporting
1523 // functions.
1524 std::string* note_;
1527 // A list of Typed_identifiers.
1529 class Typed_identifier_list
1531 public:
1532 Typed_identifier_list()
1533 : entries_()
1536 // Whether the list is empty.
1537 bool
1538 empty() const
1539 { return this->entries_.empty(); }
1541 // Return the number of entries in the list.
1542 size_t
1543 size() const
1544 { return this->entries_.size(); }
1546 // Add an entry to the end of the list.
1547 void
1548 push_back(const Typed_identifier& td)
1549 { this->entries_.push_back(td); }
1551 // Remove an entry from the end of the list.
1552 void
1553 pop_back()
1554 { this->entries_.pop_back(); }
1556 // Set the type of entry I to TYPE.
1557 void
1558 set_type(size_t i, Type* type)
1560 go_assert(i < this->entries_.size());
1561 this->entries_[i].set_type(type);
1564 // Sort the entries by name.
1565 void
1566 sort_by_name();
1568 // Traverse types.
1570 traverse(Traverse*) const;
1572 // Return the first and last elements.
1573 Typed_identifier&
1574 front()
1575 { return this->entries_.front(); }
1577 const Typed_identifier&
1578 front() const
1579 { return this->entries_.front(); }
1581 Typed_identifier&
1582 back()
1583 { return this->entries_.back(); }
1585 const Typed_identifier&
1586 back() const
1587 { return this->entries_.back(); }
1589 Typed_identifier&
1590 at(size_t i)
1591 { return this->entries_.at(i); }
1593 const Typed_identifier&
1594 at(size_t i) const
1595 { return this->entries_.at(i); }
1597 void
1598 set(size_t i, const Typed_identifier& t)
1599 { this->entries_.at(i) = t; }
1601 void
1602 resize(size_t c)
1604 go_assert(c <= this->entries_.size());
1605 this->entries_.resize(c, Typed_identifier("", NULL,
1606 Linemap::unknown_location()));
1609 void
1610 reserve(size_t c)
1611 { this->entries_.reserve(c); }
1613 // Iterators.
1615 typedef std::vector<Typed_identifier>::iterator iterator;
1616 typedef std::vector<Typed_identifier>::const_iterator const_iterator;
1618 iterator
1619 begin()
1620 { return this->entries_.begin(); }
1622 const_iterator
1623 begin() const
1624 { return this->entries_.begin(); }
1626 iterator
1627 end()
1628 { return this->entries_.end(); }
1630 const_iterator
1631 end() const
1632 { return this->entries_.end(); }
1634 // Return a copy of this list. This returns an independent copy of
1635 // the vector, but does not copy the types.
1636 Typed_identifier_list*
1637 copy() const;
1639 private:
1640 std::vector<Typed_identifier> entries_;
1643 // A type used to indicate a parsing error. This exists to simplify
1644 // later error detection.
1646 class Error_type : public Type
1648 public:
1649 Error_type()
1650 : Type(TYPE_ERROR)
1653 protected:
1654 bool
1655 do_compare_is_identity(Gogo*)
1656 { return false; }
1658 Btype*
1659 do_get_backend(Gogo* gogo);
1661 Expression*
1662 do_type_descriptor(Gogo*, Named_type*);
1664 void
1665 do_reflection(Gogo*, std::string*) const;
1667 void
1668 do_mangled_name(Gogo*, std::string*, bool*) const;
1671 // The void type.
1673 class Void_type : public Type
1675 public:
1676 Void_type()
1677 : Type(TYPE_VOID)
1680 protected:
1681 bool
1682 do_compare_is_identity(Gogo*)
1683 { return false; }
1685 Btype*
1686 do_get_backend(Gogo* gogo);
1688 Expression*
1689 do_type_descriptor(Gogo*, Named_type*)
1690 { go_unreachable(); }
1692 void
1693 do_reflection(Gogo*, std::string*) const
1696 void
1697 do_mangled_name(Gogo*, std::string*, bool*) const;
1700 // The boolean type.
1702 class Boolean_type : public Type
1704 public:
1705 Boolean_type()
1706 : Type(TYPE_BOOLEAN)
1709 protected:
1710 bool
1711 do_compare_is_identity(Gogo*)
1712 { return true; }
1714 Btype*
1715 do_get_backend(Gogo* gogo);
1717 Expression*
1718 do_type_descriptor(Gogo*, Named_type* name);
1720 // We should not be asked for the reflection string of a basic type.
1721 void
1722 do_reflection(Gogo*, std::string* ret) const
1723 { ret->append("bool"); }
1725 void
1726 do_mangled_name(Gogo*, std::string*, bool*) const;
1729 // The type of an integer.
1731 class Integer_type : public Type
1733 public:
1734 // Create a new integer type.
1735 static Named_type*
1736 create_integer_type(const char* name, bool is_unsigned, int bits,
1737 int runtime_type_kind);
1739 // Look up an existing integer type.
1740 static Named_type*
1741 lookup_integer_type(const char* name);
1743 // Create an abstract integer type.
1744 static Integer_type*
1745 create_abstract_integer_type();
1747 // Create an abstract character type.
1748 static Integer_type*
1749 create_abstract_character_type();
1751 // Create an alias to an integer type.
1752 static Named_type*
1753 create_integer_type_alias(const char* name, Named_type* real_type);
1755 // Whether this is an abstract integer type.
1756 bool
1757 is_abstract() const
1758 { return this->is_abstract_; }
1760 // Whether this is an unsigned type.
1761 bool
1762 is_unsigned() const
1763 { return this->is_unsigned_; }
1765 // The number of bits.
1767 bits() const
1768 { return this->bits_; }
1770 // Whether this type is the same as T.
1771 bool
1772 is_identical(const Integer_type* t) const;
1774 // Whether this is the type "byte" or another name for "byte".
1775 bool
1776 is_byte() const
1777 { return this->is_byte_; }
1779 // Mark this as the "byte" type.
1780 void
1781 set_is_byte()
1782 { this->is_byte_ = true; }
1784 // Whether this is the type "rune" or another name for "rune".
1785 bool
1786 is_rune() const
1787 { return this->is_rune_; }
1789 // Mark this as the "rune" type.
1790 void
1791 set_is_rune()
1792 { this->is_rune_ = true; }
1794 protected:
1795 bool
1796 do_compare_is_identity(Gogo*)
1797 { return true; }
1799 unsigned int
1800 do_hash_for_method(Gogo*, int) const;
1802 Btype*
1803 do_get_backend(Gogo*);
1805 Expression*
1806 do_type_descriptor(Gogo*, Named_type*);
1808 void
1809 do_reflection(Gogo*, std::string*) const;
1811 void
1812 do_mangled_name(Gogo*, std::string*, bool*) const;
1814 private:
1815 Integer_type(bool is_abstract, bool is_unsigned, int bits,
1816 int runtime_type_kind)
1817 : Type(TYPE_INTEGER),
1818 is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
1819 is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
1822 // Map names of integer types to the types themselves.
1823 typedef std::map<std::string, Named_type*> Named_integer_types;
1824 static Named_integer_types named_integer_types;
1826 // True if this is an abstract type.
1827 bool is_abstract_;
1828 // True if this is an unsigned type.
1829 bool is_unsigned_;
1830 // True if this is the byte type.
1831 bool is_byte_;
1832 // True if this is the rune type.
1833 bool is_rune_;
1834 // The number of bits.
1835 int bits_;
1836 // The runtime type code used in the type descriptor for this type.
1837 int runtime_type_kind_;
1840 // The type of a floating point number.
1842 class Float_type : public Type
1844 public:
1845 // Create a new float type.
1846 static Named_type*
1847 create_float_type(const char* name, int bits, int runtime_type_kind);
1849 // Look up an existing float type.
1850 static Named_type*
1851 lookup_float_type(const char* name);
1853 // Create an abstract float type.
1854 static Float_type*
1855 create_abstract_float_type();
1857 // Whether this is an abstract float type.
1858 bool
1859 is_abstract() const
1860 { return this->is_abstract_; }
1862 // The number of bits.
1864 bits() const
1865 { return this->bits_; }
1867 // Whether this type is the same as T.
1868 bool
1869 is_identical(const Float_type* t) const;
1871 protected:
1872 bool
1873 do_compare_is_identity(Gogo*)
1874 { return false; }
1876 bool
1877 do_is_reflexive()
1878 { return false; }
1880 // Distinction between +0 and -0 requires a key update.
1881 bool
1882 do_needs_key_update()
1883 { return true; }
1885 unsigned int
1886 do_hash_for_method(Gogo*, int) const;
1888 Btype*
1889 do_get_backend(Gogo*);
1891 Expression*
1892 do_type_descriptor(Gogo*, Named_type*);
1894 void
1895 do_reflection(Gogo*, std::string*) const;
1897 void
1898 do_mangled_name(Gogo*, std::string*, bool*) const;
1900 private:
1901 Float_type(bool is_abstract, int bits, int runtime_type_kind)
1902 : Type(TYPE_FLOAT),
1903 is_abstract_(is_abstract), bits_(bits),
1904 runtime_type_kind_(runtime_type_kind)
1907 // Map names of float types to the types themselves.
1908 typedef std::map<std::string, Named_type*> Named_float_types;
1909 static Named_float_types named_float_types;
1911 // True if this is an abstract type.
1912 bool is_abstract_;
1913 // The number of bits in the floating point value.
1914 int bits_;
1915 // The runtime type code used in the type descriptor for this type.
1916 int runtime_type_kind_;
1919 // The type of a complex number.
1921 class Complex_type : public Type
1923 public:
1924 // Create a new complex type.
1925 static Named_type*
1926 create_complex_type(const char* name, int bits, int runtime_type_kind);
1928 // Look up an existing complex type.
1929 static Named_type*
1930 lookup_complex_type(const char* name);
1932 // Create an abstract complex type.
1933 static Complex_type*
1934 create_abstract_complex_type();
1936 // Whether this is an abstract complex type.
1937 bool
1938 is_abstract() const
1939 { return this->is_abstract_; }
1941 // The number of bits: 64 or 128.
1942 int bits() const
1943 { return this->bits_; }
1945 // Whether this type is the same as T.
1946 bool
1947 is_identical(const Complex_type* t) const;
1949 protected:
1950 bool
1951 do_compare_is_identity(Gogo*)
1952 { return false; }
1954 bool
1955 do_is_reflexive()
1956 { return false; }
1958 // Distinction between +0 and -0 requires a key update.
1959 bool
1960 do_needs_key_update()
1961 { return true; }
1963 unsigned int
1964 do_hash_for_method(Gogo*, int) const;
1966 Btype*
1967 do_get_backend(Gogo*);
1969 Expression*
1970 do_type_descriptor(Gogo*, Named_type*);
1972 void
1973 do_reflection(Gogo*, std::string*) const;
1975 void
1976 do_mangled_name(Gogo*, std::string*, bool*) const;
1978 private:
1979 Complex_type(bool is_abstract, int bits, int runtime_type_kind)
1980 : Type(TYPE_COMPLEX),
1981 is_abstract_(is_abstract), bits_(bits),
1982 runtime_type_kind_(runtime_type_kind)
1985 // Map names of complex types to the types themselves.
1986 typedef std::map<std::string, Named_type*> Named_complex_types;
1987 static Named_complex_types named_complex_types;
1989 // True if this is an abstract type.
1990 bool is_abstract_;
1991 // The number of bits in the complex value--64 or 128.
1992 int bits_;
1993 // The runtime type code used in the type descriptor for this type.
1994 int runtime_type_kind_;
1997 // The type of a string.
1999 class String_type : public Type
2001 public:
2002 String_type()
2003 : Type(TYPE_STRING)
2006 protected:
2007 bool
2008 do_has_pointer() const
2009 { return true; }
2011 bool
2012 do_compare_is_identity(Gogo*)
2013 { return false; }
2015 // New string might have a smaller backing store.
2016 bool
2017 do_needs_key_update()
2018 { return true; }
2020 Btype*
2021 do_get_backend(Gogo*);
2023 Expression*
2024 do_type_descriptor(Gogo*, Named_type*);
2026 void
2027 do_reflection(Gogo*, std::string*) const;
2029 void
2030 do_mangled_name(Gogo*, std::string*, bool*) const;
2032 private:
2033 // The named string type.
2034 static Named_type* string_type_;
2037 // The type of a function.
2039 class Function_type : public Type
2041 public:
2042 Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
2043 Typed_identifier_list* results, Location location)
2044 : Type(TYPE_FUNCTION),
2045 receiver_(receiver), parameters_(parameters), results_(results),
2046 location_(location), is_varargs_(false), is_builtin_(false),
2047 fnbtype_(NULL), is_tagged_(false)
2050 // Get the receiver.
2051 const Typed_identifier*
2052 receiver() const
2053 { return this->receiver_; }
2055 // Add an escape note for the receiver.
2056 void
2057 add_receiver_note(int encoding)
2058 { this->receiver_->set_note(Escape_note::make_tag(encoding)); }
2060 // Get the return names and types.
2061 const Typed_identifier_list*
2062 results() const
2063 { return this->results_; }
2065 // Get the parameter names and types.
2066 const Typed_identifier_list*
2067 parameters() const
2068 { return this->parameters_; }
2070 // Add an escape note for the ith parameter.
2071 void
2072 add_parameter_note(int index, int encoding)
2073 { this->parameters_->at(index).set_note(Escape_note::make_tag(encoding)); }
2075 // Whether this function has been tagged during escape analysis.
2076 bool
2077 is_tagged() const
2078 { return this->is_tagged_; }
2080 // Mark this function as tagged after analyzing its escape.
2081 void
2082 set_is_tagged()
2083 { this->is_tagged_ = true; }
2085 // Whether this is a varargs function.
2086 bool
2087 is_varargs() const
2088 { return this->is_varargs_; }
2090 // Whether this is a builtin function.
2091 bool
2092 is_builtin() const
2093 { return this->is_builtin_; }
2095 // The location where this type was defined.
2096 Location
2097 location() const
2098 { return this->location_; }
2100 // Return whether this is a method type.
2101 bool
2102 is_method() const
2103 { return this->receiver_ != NULL; }
2105 // Whether T is a valid redeclaration of this type. This is called
2106 // when a function is declared more than once.
2107 bool
2108 is_valid_redeclaration(const Function_type* t, std::string*) const;
2110 // Whether this type is the same as T.
2111 bool
2112 is_identical(const Function_type* t, bool ignore_receiver, int flags,
2113 std::string*) const;
2115 // Record that this is a varargs function.
2116 void
2117 set_is_varargs()
2118 { this->is_varargs_ = true; }
2120 // Record that this is a builtin function.
2121 void
2122 set_is_builtin()
2123 { this->is_builtin_ = true; }
2125 // Import a function type.
2126 static Function_type*
2127 do_import(Import*);
2129 // Return a copy of this type without a receiver. This is only
2130 // valid for a method type.
2131 Function_type*
2132 copy_without_receiver() const;
2134 // Return a copy of this type with a receiver. This is used when an
2135 // interface method is attached to a named or struct type.
2136 Function_type*
2137 copy_with_receiver(Type*) const;
2139 // Return a copy of this type with the receiver treated as the first
2140 // parameter. If WANT_POINTER_RECEIVER is true, the receiver is
2141 // forced to be a pointer.
2142 Function_type*
2143 copy_with_receiver_as_param(bool want_pointer_receiver) const;
2145 // Return a copy of this type ignoring any receiver and using dummy
2146 // names for all parameters. This is used for thunks for method
2147 // values.
2148 Function_type*
2149 copy_with_names() const;
2151 static Type*
2152 make_function_type_descriptor_type();
2154 // Return the backend representation of this function type. This is used
2155 // as the real type of a backend function declaration or defintion.
2156 Btype*
2157 get_backend_fntype(Gogo*);
2159 // Return whether this is a Backend_function_type.
2160 virtual bool
2161 is_backend_function_type() const
2162 { return false; }
2164 protected:
2166 do_traverse(Traverse*);
2168 // A function descriptor may be allocated on the heap.
2169 bool
2170 do_has_pointer() const
2171 { return true; }
2173 bool
2174 do_compare_is_identity(Gogo*)
2175 { return false; }
2177 unsigned int
2178 do_hash_for_method(Gogo*, int) const;
2180 Btype*
2181 do_get_backend(Gogo*);
2183 Expression*
2184 do_type_descriptor(Gogo*, Named_type*);
2186 void
2187 do_reflection(Gogo*, std::string*) const;
2189 void
2190 do_mangled_name(Gogo*, std::string*, bool*) const;
2192 void
2193 do_export(Export*) const;
2195 private:
2196 Expression*
2197 type_descriptor_params(Type*, const Typed_identifier*,
2198 const Typed_identifier_list*);
2200 // A mapping from a list of result types to a backend struct type.
2201 class Results_hash
2203 public:
2204 unsigned int
2205 operator()(const Typed_identifier_list*) const;
2208 class Results_equal
2210 public:
2211 bool
2212 operator()(const Typed_identifier_list*,
2213 const Typed_identifier_list*) const;
2216 typedef Unordered_map_hash(Typed_identifier_list*, Btype*,
2217 Results_hash, Results_equal) Results_structs;
2219 static Results_structs results_structs;
2221 // The receiver name and type. This will be NULL for a normal
2222 // function, non-NULL for a method.
2223 Typed_identifier* receiver_;
2224 // The parameter names and types.
2225 Typed_identifier_list* parameters_;
2226 // The result names and types. This will be NULL if no result was
2227 // specified.
2228 Typed_identifier_list* results_;
2229 // The location where this type was defined. This exists solely to
2230 // give a location for the fields of the struct if this function
2231 // returns multiple values.
2232 Location location_;
2233 // Whether this function takes a variable number of arguments.
2234 bool is_varargs_;
2235 // Whether this is a special builtin function which can not simply
2236 // be called. This is used for len, cap, etc.
2237 bool is_builtin_;
2238 // The backend representation of this type for backend function
2239 // declarations and definitions.
2240 Btype* fnbtype_;
2241 // Whether this function has been analyzed by escape analysis. If this is
2242 // TRUE, this function type's parameters contain a summary of the analysis.
2243 bool is_tagged_;
2246 // The type of a function's backend representation.
2248 class Backend_function_type : public Function_type
2250 public:
2251 Backend_function_type(Typed_identifier* receiver,
2252 Typed_identifier_list* parameters,
2253 Typed_identifier_list* results, Location location)
2254 : Function_type(receiver, parameters, results, location)
2257 // Return whether this is a Backend_function_type. This overrides
2258 // Function_type::is_backend_function_type.
2259 bool
2260 is_backend_function_type() const
2261 { return true; }
2263 protected:
2264 Btype*
2265 do_get_backend(Gogo* gogo)
2266 { return this->get_backend_fntype(gogo); }
2269 // The type of a pointer.
2271 class Pointer_type : public Type
2273 public:
2274 Pointer_type(Type* to_type)
2275 : Type(TYPE_POINTER),
2276 to_type_(to_type)
2279 Type*
2280 points_to() const
2281 { return this->to_type_; }
2283 // Import a pointer type.
2284 static Pointer_type*
2285 do_import(Import*);
2287 static Type*
2288 make_pointer_type_descriptor_type();
2290 protected:
2292 do_traverse(Traverse*);
2294 bool
2295 do_verify()
2296 { return this->to_type_->verify(); }
2298 bool
2299 do_has_pointer() const
2300 { return true; }
2302 bool
2303 do_compare_is_identity(Gogo*)
2304 { return true; }
2306 unsigned int
2307 do_hash_for_method(Gogo*, int) const;
2309 Btype*
2310 do_get_backend(Gogo*);
2312 Expression*
2313 do_type_descriptor(Gogo*, Named_type*);
2315 void
2316 do_reflection(Gogo*, std::string*) const;
2318 void
2319 do_mangled_name(Gogo*, std::string*, bool*) const;
2321 void
2322 do_export(Export*) const;
2324 private:
2325 // The type to which this type points.
2326 Type* to_type_;
2329 // The nil type. We use a special type for nil because it is not the
2330 // same as any other type. In C term nil has type void*, but there is
2331 // no such type in Go.
2333 class Nil_type : public Type
2335 public:
2336 Nil_type()
2337 : Type(TYPE_NIL)
2340 protected:
2341 bool
2342 do_compare_is_identity(Gogo*)
2343 { return false; }
2345 Btype*
2346 do_get_backend(Gogo* gogo);
2348 Expression*
2349 do_type_descriptor(Gogo*, Named_type*)
2350 { go_unreachable(); }
2352 void
2353 do_reflection(Gogo*, std::string*) const
2354 { go_unreachable(); }
2356 void
2357 do_mangled_name(Gogo*, std::string*, bool*) const;
2360 // The type of a field in a struct.
2362 class Struct_field
2364 public:
2365 explicit Struct_field(const Typed_identifier& typed_identifier)
2366 : typed_identifier_(typed_identifier), tag_(NULL), is_imported_(false)
2369 // The field name.
2370 const std::string&
2371 field_name() const;
2373 // Return whether this struct field is named NAME.
2374 bool
2375 is_field_name(const std::string& name) const;
2377 // Return whether this struct field is an unexported field named NAME.
2378 bool
2379 is_unexported_field_name(Gogo*, const std::string& name) const;
2381 // Return whether this struct field is an embedded built-in type.
2382 bool
2383 is_embedded_builtin(Gogo*) const;
2385 // The field type.
2386 Type*
2387 type() const
2388 { return this->typed_identifier_.type(); }
2390 // The field location.
2391 Location
2392 location() const
2393 { return this->typed_identifier_.location(); }
2395 // Whether the field has a tag.
2396 bool
2397 has_tag() const
2398 { return this->tag_ != NULL; }
2400 // The tag.
2401 const std::string&
2402 tag() const
2404 go_assert(this->tag_ != NULL);
2405 return *this->tag_;
2408 // Whether this is an anonymous field.
2409 bool
2410 is_anonymous() const
2411 { return this->typed_identifier_.name().empty(); }
2413 // Set the tag. FIXME: This is never freed.
2414 void
2415 set_tag(const std::string& tag)
2416 { this->tag_ = new std::string(tag); }
2418 // Record that this field is defined in an imported struct.
2419 void
2420 set_is_imported()
2421 { this->is_imported_ = true; }
2423 // Set the type. This is only used in error cases.
2424 void
2425 set_type(Type* type)
2426 { this->typed_identifier_.set_type(type); }
2428 private:
2429 // The field name, type, and location.
2430 Typed_identifier typed_identifier_;
2431 // The field tag. This is NULL if the field has no tag.
2432 std::string* tag_;
2433 // Whether this field is defined in an imported struct.
2434 bool is_imported_;
2437 // A list of struct fields.
2439 class Struct_field_list
2441 public:
2442 Struct_field_list()
2443 : entries_()
2446 // Whether the list is empty.
2447 bool
2448 empty() const
2449 { return this->entries_.empty(); }
2451 // Return the number of entries.
2452 size_t
2453 size() const
2454 { return this->entries_.size(); }
2456 // Add an entry to the end of the list.
2457 void
2458 push_back(const Struct_field& sf)
2459 { this->entries_.push_back(sf); }
2461 // Index into the list.
2462 const Struct_field&
2463 at(size_t i) const
2464 { return this->entries_.at(i); }
2466 // Last entry in list.
2467 Struct_field&
2468 back()
2469 { return this->entries_.back(); }
2471 // Iterators.
2473 typedef std::vector<Struct_field>::iterator iterator;
2474 typedef std::vector<Struct_field>::const_iterator const_iterator;
2476 iterator
2477 begin()
2478 { return this->entries_.begin(); }
2480 const_iterator
2481 begin() const
2482 { return this->entries_.begin(); }
2484 iterator
2485 end()
2486 { return this->entries_.end(); }
2488 const_iterator
2489 end() const
2490 { return this->entries_.end(); }
2492 private:
2493 std::vector<Struct_field> entries_;
2496 // The type of a struct.
2498 class Struct_type : public Type
2500 public:
2501 Struct_type(Struct_field_list* fields, Location location)
2502 : Type(TYPE_STRUCT),
2503 fields_(fields), location_(location), all_methods_(NULL),
2504 is_struct_incomparable_(false), has_padding_(false)
2507 // Return the field NAME. This only looks at local fields, not at
2508 // embedded types. If the field is found, and PINDEX is not NULL,
2509 // this sets *PINDEX to the field index. If the field is not found,
2510 // this returns NULL.
2511 const Struct_field*
2512 find_local_field(const std::string& name, unsigned int *pindex) const;
2514 // Return the field number INDEX.
2515 const Struct_field*
2516 field(unsigned int index) const
2517 { return &this->fields_->at(index); }
2519 // Get the struct fields.
2520 const Struct_field_list*
2521 fields() const
2522 { return this->fields_; }
2524 // Return the number of fields.
2525 size_t
2526 field_count() const
2527 { return this->fields_->size(); }
2529 // Location of struct definition.
2530 Location
2531 location() const
2532 { return this->location_; }
2534 // Push a new field onto the end of the struct. This is used when
2535 // building a closure variable.
2536 void
2537 push_field(const Struct_field& sf)
2538 { this->fields_->push_back(sf); }
2540 // Return an expression referring to field NAME in STRUCT_EXPR, or
2541 // NULL if there is no field with that name.
2542 Field_reference_expression*
2543 field_reference(Expression* struct_expr, const std::string& name,
2544 Location) const;
2546 // Return the total number of fields, including embedded fields.
2547 // This is the number of values that can appear in a conversion to
2548 // this type.
2549 unsigned int
2550 total_field_count() const;
2552 // Whether this type is identical with T.
2553 bool
2554 is_identical(const Struct_type* t, int) const;
2556 // Return whether NAME is a local field which is not exported. This
2557 // is only used for better error reporting.
2558 bool
2559 is_unexported_local_field(Gogo*, const std::string& name) const;
2561 // If this is an unnamed struct, build the complete list of methods,
2562 // including those from anonymous fields, and build methods stubs if
2563 // needed.
2564 void
2565 finalize_methods(Gogo*);
2567 // Return whether this type has any methods. This should only be
2568 // called after the finalize_methods pass.
2569 bool
2570 has_any_methods() const
2571 { return this->all_methods_ != NULL; }
2573 // Return the methods for this type. This should only be called
2574 // after the finalize_methods pass.
2575 const Methods*
2576 methods() const
2577 { return this->all_methods_; }
2579 // Return the method to use for NAME. This returns NULL if there is
2580 // no such method or if the method is ambiguous. When it returns
2581 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2582 Method*
2583 method_function(const std::string& name, bool* is_ambiguous) const;
2585 // Return a pointer to the interface method table for this type for
2586 // the interface INTERFACE. If IS_POINTER is true, set the type
2587 // descriptor to a pointer to this type, otherwise set it to this
2588 // type.
2589 Expression*
2590 interface_method_table(Interface_type* interface, bool is_pointer);
2592 // Traverse just the field types of a struct type.
2594 traverse_field_types(Traverse* traverse)
2595 { return this->do_traverse(traverse); }
2597 // If the offset of field INDEX in the backend implementation can be
2598 // determined, set *POFFSET to the offset in bytes and return true.
2599 // Otherwise, return false.
2600 bool
2601 backend_field_offset(Gogo*, unsigned int index, int64_t* poffset);
2603 // Finish the backend representation of all the fields.
2604 void
2605 finish_backend_fields(Gogo*);
2607 // Import a struct type.
2608 static Struct_type*
2609 do_import(Import*);
2611 static Type*
2612 make_struct_type_descriptor_type();
2614 // Return whether this is a generated struct that is not comparable.
2615 bool
2616 is_struct_incomparable() const
2617 { return this->is_struct_incomparable_; }
2619 // Record that this is a generated struct that is not comparable.
2620 void
2621 set_is_struct_incomparable()
2622 { this->is_struct_incomparable_ = true; }
2624 // Return whether this struct's backend type has padding, due to
2625 // trailing zero-sized field.
2626 bool
2627 has_padding() const
2628 { return this->has_padding_; }
2630 // Record that this struct's backend type has padding.
2631 void
2632 set_has_padding()
2633 { this->has_padding_ = true; }
2635 // Write the hash function for this type.
2636 void
2637 write_hash_function(Gogo*, Function_type*);
2639 // Write the equality function for this type.
2640 void
2641 write_equal_function(Gogo*, Named_type*);
2643 // Whether we can write this type to a C header file, to implement
2644 // -fgo-c-header.
2645 bool
2646 can_write_to_c_header(std::vector<const Named_object*>*,
2647 std::vector<const Named_object*>*) const;
2649 // Write this type to a C header file, to implement -fgo-c-header.
2650 void
2651 write_to_c_header(std::ostream&) const;
2653 protected:
2655 do_traverse(Traverse*);
2657 bool
2658 do_verify();
2660 bool
2661 do_has_pointer() const;
2663 bool
2664 do_compare_is_identity(Gogo*);
2666 bool
2667 do_is_reflexive();
2669 bool
2670 do_needs_key_update();
2672 bool
2673 do_hash_might_panic();
2675 bool
2676 do_in_heap() const;
2678 unsigned int
2679 do_hash_for_method(Gogo*, int) const;
2681 Btype*
2682 do_get_backend(Gogo*);
2684 Expression*
2685 do_type_descriptor(Gogo*, Named_type*);
2687 void
2688 do_reflection(Gogo*, std::string*) const;
2690 void
2691 do_mangled_name(Gogo*, std::string*, bool*) const;
2693 void
2694 do_export(Export*) const;
2696 private:
2697 bool
2698 can_write_type_to_c_header(const Type*,
2699 std::vector<const Named_object*>*,
2700 std::vector<const Named_object*>*) const;
2702 void
2703 write_field_to_c_header(std::ostream&, const std::string&, const Type*) const;
2705 // Used to merge method sets of identical unnamed structs.
2706 typedef Unordered_map_hash(Struct_type*, Struct_type*, Type_hash_identical,
2707 Type_identical) Identical_structs;
2709 static Identical_structs identical_structs;
2711 // Used to manage method tables for identical unnamed structs.
2712 typedef std::pair<Interface_method_tables*, Interface_method_tables*>
2713 Struct_method_table_pair;
2715 typedef Unordered_map_hash(Struct_type*, Struct_method_table_pair*,
2716 Type_hash_identical, Type_identical)
2717 Struct_method_tables;
2719 static Struct_method_tables struct_method_tables;
2721 // Used to avoid infinite loops in field_reference_depth.
2722 struct Saw_named_type
2724 Saw_named_type* next;
2725 Named_type* nt;
2728 Field_reference_expression*
2729 field_reference_depth(Expression* struct_expr, const std::string& name,
2730 Location, Saw_named_type*,
2731 unsigned int* depth) const;
2733 // The fields of the struct.
2734 Struct_field_list* fields_;
2735 // The place where the struct was declared.
2736 Location location_;
2737 // If this struct is unnamed, a list of methods.
2738 Methods* all_methods_;
2739 // True if this is a generated struct that is not considered to be
2740 // comparable.
2741 bool is_struct_incomparable_;
2742 // True if this struct's backend type has padding, due to trailing
2743 // zero-sized field.
2744 bool has_padding_;
2747 // The type of an array.
2749 class Array_type : public Type
2751 public:
2752 Array_type(Type* element_type, Expression* length)
2753 : Type(TYPE_ARRAY),
2754 element_type_(element_type), length_(length), blength_(NULL),
2755 issued_length_error_(false), is_array_incomparable_(false)
2758 // Return the element type.
2759 Type*
2760 element_type() const
2761 { return this->element_type_; }
2763 // Return the length. This will return NULL for a slice.
2764 Expression*
2765 length() const
2766 { return this->length_; }
2768 // Store the length as an int64_t into *PLEN. Return false if the
2769 // length can not be determined. This will assert if called for a
2770 // slice.
2771 bool
2772 int_length(int64_t* plen) const;
2774 // Whether this type is identical with T.
2775 bool
2776 is_identical(const Array_type* t, int) const;
2778 // Return an expression for the pointer to the values in an array.
2779 Expression*
2780 get_value_pointer(Gogo*, Expression* array, bool is_lvalue) const;
2782 // Return an expression for the length of an array with this type.
2783 Expression*
2784 get_length(Gogo*, Expression* array) const;
2786 // Return an expression for the capacity of an array with this type.
2787 Expression*
2788 get_capacity(Gogo*, Expression* array) const;
2790 // Import an array type.
2791 static Array_type*
2792 do_import(Import*);
2794 // Return the backend representation of the element type.
2795 Btype*
2796 get_backend_element(Gogo*, bool use_placeholder);
2798 // Return the backend representation of the length.
2799 Bexpression*
2800 get_backend_length(Gogo*);
2802 // Finish the backend representation of the element type.
2803 void
2804 finish_backend_element(Gogo*);
2806 static Type*
2807 make_array_type_descriptor_type();
2809 static Type*
2810 make_slice_type_descriptor_type();
2812 // Return whether this is a generated array that is not comparable.
2813 bool
2814 is_array_incomparable() const
2815 { return this->is_array_incomparable_; }
2817 // Record that this is a generated array that is not comparable.
2818 void
2819 set_is_array_incomparable()
2820 { this->is_array_incomparable_ = true; }
2822 // Write the hash function for this type.
2823 void
2824 write_hash_function(Gogo*, Function_type*);
2826 // Write the equality function for this type.
2827 void
2828 write_equal_function(Gogo*, Named_type*);
2830 protected:
2832 do_traverse(Traverse* traverse);
2834 bool
2835 do_verify();
2837 bool
2838 do_has_pointer() const;
2840 bool
2841 do_compare_is_identity(Gogo*);
2843 bool
2844 do_is_reflexive()
2846 return this->length_ != NULL && this->element_type_->is_reflexive();
2849 bool
2850 do_needs_key_update()
2851 { return this->element_type_->needs_key_update(); }
2853 bool
2854 do_hash_might_panic()
2855 { return this->length_ != NULL && this->element_type_->hash_might_panic(); }
2857 bool
2858 do_in_heap() const
2859 { return this->length_ == NULL || this->element_type_->in_heap(); }
2861 unsigned int
2862 do_hash_for_method(Gogo*, int) const;
2864 Btype*
2865 do_get_backend(Gogo*);
2867 Expression*
2868 do_type_descriptor(Gogo*, Named_type*);
2870 void
2871 do_reflection(Gogo*, std::string*) const;
2873 void
2874 do_mangled_name(Gogo*, std::string*, bool*) const;
2876 void
2877 do_export(Export*) const;
2879 private:
2880 bool
2881 verify_length();
2883 Expression*
2884 array_type_descriptor(Gogo*, Named_type*);
2886 Expression*
2887 slice_type_descriptor(Gogo*, Named_type*);
2889 // The type of elements of the array.
2890 Type* element_type_;
2891 // The number of elements. This may be NULL.
2892 Expression* length_;
2893 // The backend representation of the length.
2894 // We only want to compute this once.
2895 Bexpression* blength_;
2896 // Whether or not an invalid length error has been issued for this type,
2897 // to avoid knock-on errors.
2898 mutable bool issued_length_error_;
2899 // True if this is a generated array that is not considered to be
2900 // comparable.
2901 bool is_array_incomparable_;
2904 // The type of a map.
2906 class Map_type : public Type
2908 public:
2909 Map_type(Type* key_type, Type* val_type, Location location)
2910 : Type(TYPE_MAP),
2911 key_type_(key_type), val_type_(val_type), hmap_type_(NULL),
2912 bucket_type_(NULL), hiter_type_(NULL), location_(location)
2915 // Return the key type.
2916 Type*
2917 key_type() const
2918 { return this->key_type_; }
2920 // Return the value type.
2921 Type*
2922 val_type() const
2923 { return this->val_type_; }
2925 // Return the type used for an iteration over this map.
2926 Type*
2927 hiter_type(Gogo*);
2929 // If this map requires the "fat" functions, returns the pointer to
2930 // pass as the zero value to those functions. Otherwise, in the
2931 // normal case, returns NULL.
2932 Expression*
2933 fat_zero_value(Gogo*);
2935 // Map algorithm to use for this map type. We may use specialized
2936 // fast map routines for certain key types.
2937 enum Map_alg
2939 // 32-bit key.
2940 MAP_ALG_FAST32,
2941 // 32-bit pointer key.
2942 MAP_ALG_FAST32PTR,
2943 // 64-bit key.
2944 MAP_ALG_FAST64,
2945 // 64-bit pointer key.
2946 MAP_ALG_FAST64PTR,
2947 // String key.
2948 MAP_ALG_FASTSTR,
2949 // Anything else.
2950 MAP_ALG_SLOW,
2953 Map_alg
2954 algorithm(Gogo*);
2956 // Return whether VAR is the map zero value.
2957 static bool
2958 is_zero_value(Variable* var);
2960 // Return the backend representation of the map zero value.
2961 static Bvariable*
2962 backend_zero_value(Gogo*);
2964 // Whether this type is identical with T.
2965 bool
2966 is_identical(const Map_type* t, int) const;
2968 // Import a map type.
2969 static Map_type*
2970 do_import(Import*);
2972 static Type*
2973 make_map_type_descriptor_type();
2975 // This must be in sync with libgo/go/runtime/map.go.
2976 static const int bucket_size = 8;
2978 protected:
2980 do_traverse(Traverse*);
2982 bool
2983 do_verify();
2985 bool
2986 do_has_pointer() const
2987 { return true; }
2989 bool
2990 do_compare_is_identity(Gogo*)
2991 { return false; }
2993 bool
2994 do_is_reflexive()
2996 return this->key_type_->is_reflexive() && this->val_type_->is_reflexive();
2999 unsigned int
3000 do_hash_for_method(Gogo*, int) const;
3002 Btype*
3003 do_get_backend(Gogo*);
3005 Expression*
3006 do_type_descriptor(Gogo*, Named_type*);
3008 void
3009 do_reflection(Gogo*, std::string*) const;
3011 void
3012 do_mangled_name(Gogo*, std::string*, bool*) const;
3014 void
3015 do_export(Export*) const;
3017 private:
3018 // These must be in sync with libgo/go/runtime/map.go.
3019 static const int max_key_size = 128;
3020 static const int max_val_size = 128;
3021 static const int max_zero_size = 1024;
3023 // Maps with value types larger than max_zero_size require passing a
3024 // zero value pointer to the map functions.
3026 // The zero value variable.
3027 static Named_object* zero_value;
3029 // The current size of the zero value.
3030 static int64_t zero_value_size;
3032 // The current alignment of the zero value.
3033 static int64_t zero_value_align;
3035 Type*
3036 bucket_type(Gogo*, int64_t, int64_t);
3038 Type*
3039 hmap_type(Type*);
3041 // The key type.
3042 Type* key_type_;
3043 // The value type.
3044 Type* val_type_;
3045 // The hashmap type. At run time a map is represented as a pointer
3046 // to this type.
3047 Type* hmap_type_;
3048 // The bucket type, the type used to hold keys and values at run time.
3049 Type* bucket_type_;
3050 // The iterator type.
3051 Type* hiter_type_;
3052 // Where the type was defined.
3053 Location location_;
3056 // The type of a channel.
3058 class Channel_type : public Type
3060 public:
3061 Channel_type(bool may_send, bool may_receive, Type* element_type)
3062 : Type(TYPE_CHANNEL),
3063 may_send_(may_send), may_receive_(may_receive),
3064 element_type_(element_type)
3065 { go_assert(may_send || may_receive); }
3067 // Whether this channel can send data.
3068 bool
3069 may_send() const
3070 { return this->may_send_; }
3072 // Whether this channel can receive data.
3073 bool
3074 may_receive() const
3075 { return this->may_receive_; }
3077 // The type of the values that may be sent on this channel. This is
3078 // NULL if any type may be sent.
3079 Type*
3080 element_type() const
3081 { return this->element_type_; }
3083 // Whether this type is identical with T.
3084 bool
3085 is_identical(const Channel_type* t, int) const;
3087 // Import a channel type.
3088 static Channel_type*
3089 do_import(Import*);
3091 static Type*
3092 make_chan_type_descriptor_type();
3094 static Type*
3095 select_case_type();
3097 protected:
3099 do_traverse(Traverse* traverse)
3100 { return Type::traverse(this->element_type_, traverse); }
3102 bool
3103 do_verify();
3105 bool
3106 do_has_pointer() const
3107 { return true; }
3109 bool
3110 do_compare_is_identity(Gogo*)
3111 { return true; }
3113 unsigned int
3114 do_hash_for_method(Gogo*, int) const;
3116 Btype*
3117 do_get_backend(Gogo*);
3119 Expression*
3120 do_type_descriptor(Gogo*, Named_type*);
3122 void
3123 do_reflection(Gogo*, std::string*) const;
3125 void
3126 do_mangled_name(Gogo*, std::string*, bool*) const;
3128 void
3129 do_export(Export*) const;
3131 private:
3132 // Whether this channel can send data.
3133 bool may_send_;
3134 // Whether this channel can receive data.
3135 bool may_receive_;
3136 // The types of elements which may be sent on this channel. If this
3137 // is NULL, it means that any type may be sent.
3138 Type* element_type_;
3141 // An interface type.
3143 class Interface_type : public Type
3145 public:
3146 Interface_type(Typed_identifier_list* methods, Location location)
3147 : Type(TYPE_INTERFACE),
3148 parse_methods_(methods), all_methods_(NULL), location_(location),
3149 package_(NULL), interface_btype_(NULL), bmethods_(NULL),
3150 assume_identical_(NULL), methods_are_finalized_(false),
3151 bmethods_is_placeholder_(false), seen_(false)
3152 { go_assert(methods == NULL || !methods->empty()); }
3154 // The location where the interface type was defined.
3155 Location
3156 location() const
3157 { return this->location_; }
3159 // The package where the interface type was defined. Returns NULL
3160 // for the package currently being compiled.
3161 Package*
3162 package() const
3163 { return this->package_; }
3165 // Return whether this is an empty interface.
3166 bool
3167 is_empty() const
3169 go_assert(this->methods_are_finalized_);
3170 return this->all_methods_ == NULL;
3173 // Return the list of locally defined methods. This will return NULL
3174 // for an empty interface. Embedded interfaces will appear in this
3175 // list as an entry with no name.
3176 const Typed_identifier_list*
3177 local_methods() const
3178 { return this->parse_methods_; }
3180 // Return the list of all methods. This will return NULL for an
3181 // empty interface.
3182 const Typed_identifier_list*
3183 methods() const;
3185 // Return the number of methods.
3186 size_t
3187 method_count() const;
3189 // Return the method NAME, or NULL.
3190 const Typed_identifier*
3191 find_method(const std::string& name) const;
3193 // Return the zero-based index of method NAME.
3194 size_t
3195 method_index(const std::string& name) const;
3197 // Finalize the methods. This sets all_methods_. This handles
3198 // interface inheritance.
3199 void
3200 finalize_methods();
3202 // Return true if T implements this interface. If this returns
3203 // false, and REASON is not NULL, it sets *REASON to the reason that
3204 // it fails.
3205 bool
3206 implements_interface(const Type* t, std::string* reason) const;
3208 // Whether this type is identical with T. REASON is as in
3209 // implements_interface.
3210 bool
3211 is_identical(const Interface_type* t, int) const;
3213 // Whether we can assign T to this type. is_identical is known to
3214 // be false.
3215 bool
3216 is_compatible_for_assign(const Interface_type*, std::string* reason) const;
3218 // Return whether NAME is a method which is not exported. This is
3219 // only used for better error reporting.
3220 bool
3221 is_unexported_method(Gogo*, const std::string& name) const;
3223 // Import an interface type.
3224 static Interface_type*
3225 do_import(Import*);
3227 // Make a struct for an empty interface type.
3228 static Btype*
3229 get_backend_empty_interface_type(Gogo*);
3231 // Get a pointer to the backend representation of the method table.
3232 Btype*
3233 get_backend_methods(Gogo*);
3235 // Return a placeholder for the backend representation of the
3236 // pointer to the method table.
3237 Btype*
3238 get_backend_methods_placeholder(Gogo*);
3240 // Finish the backend representation of the method types.
3241 void
3242 finish_backend_methods(Gogo*);
3244 static Type*
3245 make_interface_type_descriptor_type();
3247 // Return whether methods are finalized for this interface.
3248 bool
3249 methods_are_finalized() const
3250 { return this->methods_are_finalized_; }
3252 // Sort embedded interfaces by name. Needed when we are preparing
3253 // to emit types into the export data.
3254 void
3255 sort_embedded()
3257 if (parse_methods_ != NULL)
3258 parse_methods_->sort_by_name();
3261 protected:
3263 do_traverse(Traverse*);
3265 bool
3266 do_has_pointer() const
3267 { return true; }
3269 bool
3270 do_compare_is_identity(Gogo*)
3271 { return false; }
3273 // Not reflexive if it contains a float.
3274 bool
3275 do_is_reflexive()
3276 { return false; }
3278 // Distinction between +0 and -0 requires a key update if it
3279 // contains a float.
3280 bool
3281 do_needs_key_update()
3282 { return true; }
3284 // Hashing an unhashable type stored in an interface might panic.
3285 bool
3286 do_hash_might_panic()
3287 { return true; }
3289 unsigned int
3290 do_hash_for_method(Gogo*, int) const;
3292 Btype*
3293 do_get_backend(Gogo*);
3295 Expression*
3296 do_type_descriptor(Gogo*, Named_type*);
3298 void
3299 do_reflection(Gogo*, std::string*) const;
3301 void
3302 do_mangled_name(Gogo*, std::string*, bool*) const;
3304 void
3305 do_export(Export*) const;
3307 private:
3308 // This type guards against infinite recursion when comparing
3309 // interface types. We keep a list of interface types assumed to be
3310 // identical during comparison. We just keep the list on the stack.
3311 // This permits us to compare cases like
3312 // type I1 interface { F() interface{I1} }
3313 // type I2 interface { F() interface{I2} }
3314 struct Assume_identical
3316 Assume_identical* next;
3317 const Interface_type* t1;
3318 const Interface_type* t2;
3321 bool
3322 assume_identical(const Interface_type*, const Interface_type*) const;
3324 struct Bmethods_map_entry
3326 Btype *btype;
3327 bool is_placeholder;
3330 // A mapping from Interface_type to the backend type of its bmethods_,
3331 // used to ensure that the backend representation of identical types
3332 // is identical.
3333 typedef Unordered_map_hash(const Interface_type*, Bmethods_map_entry,
3334 Type_hash_identical, Type_identical) Bmethods_map;
3336 static Bmethods_map bmethods_map;
3338 // The list of methods associated with the interface from the
3339 // parser. This will be NULL for the empty interface. This may
3340 // include unnamed interface types.
3341 Typed_identifier_list* parse_methods_;
3342 // The list of all methods associated with the interface. This
3343 // expands any interface types listed in methods_. It is set by
3344 // finalize_methods. This will be NULL for the empty interface.
3345 Typed_identifier_list* all_methods_;
3346 // The location where the interface was defined.
3347 Location location_;
3348 // The package where the interface was defined. This is NULL for
3349 // the package being compiled.
3350 Package* package_;
3351 // The backend representation of this type during backend conversion.
3352 Btype* interface_btype_;
3353 // The backend representation of the pointer to the method table.
3354 Btype* bmethods_;
3355 // A list of interface types assumed to be identical during
3356 // interface comparison.
3357 mutable Assume_identical* assume_identical_;
3358 // Whether the methods have been finalized.
3359 bool methods_are_finalized_;
3360 // Whether the bmethods_ field is a placeholder.
3361 bool bmethods_is_placeholder_;
3362 // Used to avoid endless recursion in do_mangled_name.
3363 mutable bool seen_;
3366 // The value we keep for a named type. This lets us get the right
3367 // name when we convert to backend. Note that we don't actually keep
3368 // the name here; the name is in the Named_object which points to
3369 // this. This object exists to hold a unique backend representation for
3370 // the type.
3372 class Named_type : public Type
3374 public:
3375 Named_type(Named_object* named_object, Type* type, Location location)
3376 : Type(TYPE_NAMED),
3377 named_object_(named_object), in_function_(NULL), in_function_index_(0),
3378 type_(type), local_methods_(NULL), all_methods_(NULL),
3379 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
3380 location_(location), named_btype_(NULL), dependencies_(),
3381 is_alias_(false), is_visible_(true), is_error_(false), in_heap_(true),
3382 is_placeholder_(false), is_converted_(false), is_verified_(false),
3383 seen_(false), seen_in_compare_is_identity_(false),
3384 seen_in_get_backend_(false), seen_alias_(false)
3387 // Return the associated Named_object. This holds the actual name.
3388 Named_object*
3389 named_object()
3390 { return this->named_object_; }
3392 const Named_object*
3393 named_object() const
3394 { return this->named_object_; }
3396 // Set the Named_object. This is used when we see a type
3397 // declaration followed by a type.
3398 void
3399 set_named_object(Named_object* no)
3400 { this->named_object_ = no; }
3402 // Whether this is an alias (type T1 = T2) rather than an ordinary
3403 // named type (type T1 T2).
3404 bool
3405 is_alias() const
3406 { return this->is_alias_; }
3408 // Record that this type is an alias.
3409 void
3410 set_is_alias()
3411 { this->is_alias_ = true; }
3413 // Mark this type as not permitted in the heap.
3414 void
3415 set_not_in_heap()
3416 { this->in_heap_ = false; }
3418 // Return the function in which this type is defined. This will
3419 // return NULL for a type defined in global scope.
3420 const Named_object*
3421 in_function(unsigned int *pindex) const
3423 *pindex = this->in_function_index_;
3424 return this->in_function_;
3427 // Set the function in which this type is defined.
3428 void
3429 set_in_function(Named_object* f, unsigned int index)
3431 this->in_function_ = f;
3432 this->in_function_index_ = index;
3435 // Return the name of the type.
3436 const std::string&
3437 name() const;
3439 // Return the name of the type for an error message. The difference
3440 // is that if the type is defined in a different package, this will
3441 // return PACKAGE.NAME.
3442 std::string
3443 message_name() const;
3445 // Return the underlying type.
3446 Type*
3447 real_type()
3448 { return this->type_; }
3450 const Type*
3451 real_type() const
3452 { return this->type_; }
3454 // Return the location.
3455 Location
3456 location() const
3457 { return this->location_; }
3459 // Whether this type is visible. This only matters when parsing.
3460 bool
3461 is_visible() const
3462 { return this->is_visible_; }
3464 // Mark this type as visible.
3465 void
3466 set_is_visible()
3467 { this->is_visible_ = true; }
3469 // Mark this type as invisible.
3470 void
3471 clear_is_visible()
3472 { this->is_visible_ = false; }
3474 // Whether this is a builtin type.
3475 bool
3476 is_builtin() const
3477 { return Linemap::is_predeclared_location(this->location_); }
3479 // Whether this named type is valid. A recursive named type is invalid.
3480 bool
3481 is_valid() const
3482 { return !this->is_error_; }
3484 // Return the base type for this type.
3485 Type*
3486 named_base();
3488 const Type*
3489 named_base() const;
3491 // Return whether this is an error type.
3492 bool
3493 is_named_error_type() const;
3495 // Return whether this type is comparable. If REASON is not NULL,
3496 // set *REASON when returning false.
3497 bool
3498 named_type_is_comparable(std::string* reason) const;
3500 // Add a method to this type.
3501 Named_object*
3502 add_method(const std::string& name, Function*);
3504 // Add a method declaration to this type.
3505 Named_object*
3506 add_method_declaration(const std::string& name, Package* package,
3507 Function_type* type, Location location);
3509 // Add an existing method--one defined before the type itself was
3510 // defined--to a type.
3511 void
3512 add_existing_method(Named_object*);
3514 // Look up a local method.
3515 Named_object*
3516 find_local_method(const std::string& name) const;
3518 // Return the list of local methods.
3519 const Bindings*
3520 local_methods() const;
3522 // Build the complete list of methods, including those from
3523 // anonymous fields, and build method stubs if needed.
3524 void
3525 finalize_methods(Gogo*);
3527 // Return whether this type has any methods. This should only be
3528 // called after the finalize_methods pass.
3529 bool
3530 has_any_methods() const;
3532 // Return the methods for this type. This should only be called
3533 // after the finalized_methods pass.
3534 const Methods*
3535 methods() const;
3537 // Return the method to use for NAME. This returns NULL if there is
3538 // no such method or if the method is ambiguous. When it returns
3539 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
3540 Method*
3541 method_function(const std::string& name, bool *is_ambiguous) const;
3543 // Return whether NAME is a known field or method which is not
3544 // exported. This is only used for better error reporting.
3545 bool
3546 is_unexported_local_method(Gogo*, const std::string& name) const;
3548 // Return a pointer to the interface method table for this type for
3549 // the interface INTERFACE. If IS_POINTER is true, set the type
3550 // descriptor to a pointer to this type, otherwise set it to this
3551 // type.
3552 Expression*
3553 interface_method_table(Interface_type* interface, bool is_pointer);
3555 // Note that a type must be converted to the backend representation
3556 // before we convert this type.
3557 void
3558 add_dependency(Named_type* nt)
3559 { this->dependencies_.push_back(nt); }
3561 // Return true if the size and alignment of the backend
3562 // representation of this type is known. This is always true after
3563 // types have been converted, but may be false beforehand.
3564 bool
3565 is_named_backend_type_size_known() const
3566 { return this->named_btype_ != NULL && !this->is_placeholder_; }
3568 // Add to the reflection string as for Type::append_reflection, but
3569 // if USE_ALIAS use the alias name rather than the alias target.
3570 void
3571 append_reflection_type_name(Gogo*, bool use_alias, std::string*) const;
3573 // Append the symbol type name as for Type::append_mangled_name,
3574 // but if USE_ALIAS use the alias name rather than the alias target.
3575 void
3576 append_symbol_type_name(Gogo*, bool use_alias, std::string*,
3577 bool* is_non_identifier) const;
3579 // Import a named type.
3580 static void
3581 import_named_type(Import*, Named_type**);
3583 // Initial conversion to backend representation.
3584 void
3585 convert(Gogo*);
3587 protected:
3589 do_traverse(Traverse* traverse)
3590 { return Type::traverse(this->type_, traverse); }
3592 bool
3593 do_verify();
3595 bool
3596 do_has_pointer() const;
3598 bool
3599 do_compare_is_identity(Gogo*);
3601 bool
3602 do_is_reflexive();
3604 bool
3605 do_needs_key_update();
3607 bool
3608 do_in_heap() const
3609 { return this->in_heap_ && this->type_->in_heap(); }
3611 unsigned int
3612 do_hash_for_method(Gogo*, int) const;
3614 Btype*
3615 do_get_backend(Gogo*);
3617 Expression*
3618 do_type_descriptor(Gogo*, Named_type*);
3620 void
3621 do_reflection(Gogo*, std::string*) const;
3623 void
3624 do_mangled_name(Gogo*, std::string*, bool*) const;
3626 void
3627 do_export(Export*) const;
3629 private:
3630 // Create the placeholder during conversion.
3631 void
3632 create_placeholder(Gogo*);
3634 // A pointer back to the Named_object for this type.
3635 Named_object* named_object_;
3636 // If this type is defined in a function, a pointer back to the
3637 // function in which it is defined.
3638 Named_object* in_function_;
3639 // The index of this type in IN_FUNCTION_.
3640 unsigned int in_function_index_;
3641 // The actual type.
3642 Type* type_;
3643 // The list of methods defined for this type. Any named type can
3644 // have methods.
3645 Bindings* local_methods_;
3646 // The full list of methods for this type, including methods
3647 // declared for anonymous fields.
3648 Methods* all_methods_;
3649 // A mapping from interfaces to the associated interface method
3650 // tables for this type.
3651 Interface_method_tables* interface_method_tables_;
3652 // A mapping from interfaces to the associated interface method
3653 // tables for pointers to this type.
3654 Interface_method_tables* pointer_interface_method_tables_;
3655 // The location where this type was defined.
3656 Location location_;
3657 // The backend representation of this type during backend
3658 // conversion. This is used to avoid endless recursion when a named
3659 // type refers to itself.
3660 Btype* named_btype_;
3661 // A list of types which must be converted to the backend
3662 // representation before this type can be converted. This is for
3663 // cases like
3664 // type S1 { p *S2 }
3665 // type S2 { s S1 }
3666 // where we can't convert S2 to the backend representation unless we
3667 // have converted S1.
3668 std::vector<Named_type*> dependencies_;
3669 // Whether this is an alias type.
3670 bool is_alias_;
3671 // Whether this type is visible. This is false if this type was
3672 // created because it was referenced by an imported object, but the
3673 // type itself was not exported. This will always be true for types
3674 // created in the current package.
3675 bool is_visible_;
3676 // Whether this type is erroneous.
3677 bool is_error_;
3678 // Whether this type is permitted in the heap. This is true by
3679 // default, false if there is a magic //go:notinheap comment.
3680 bool in_heap_;
3681 // Whether the current value of named_btype_ is a placeholder for
3682 // which the final size of the type is not known.
3683 bool is_placeholder_;
3684 // Whether this type has been converted to the backend
3685 // representation. Implies that is_placeholder_ is false.
3686 bool is_converted_;
3687 // Whether this type has been verified.
3688 bool is_verified_;
3689 // In a recursive operation such as has_pointer, this flag is used
3690 // to prevent infinite recursion when a type refers to itself. This
3691 // is mutable because it is always reset to false when the function
3692 // exits.
3693 mutable bool seen_;
3694 // Like seen_, but used only by do_compare_is_identity.
3695 bool seen_in_compare_is_identity_;
3696 // Like seen_, but used only by do_get_backend.
3697 bool seen_in_get_backend_;
3698 // Like seen_, but used when resolving aliases.
3699 mutable bool seen_alias_;
3702 // A forward declaration. This handles a type which has been declared
3703 // but not defined.
3705 class Forward_declaration_type : public Type
3707 public:
3708 Forward_declaration_type(Named_object* named_object);
3710 // The named object associated with this type declaration. This
3711 // will be resolved.
3712 Named_object*
3713 named_object();
3715 const Named_object*
3716 named_object() const;
3718 // Return the name of the type.
3719 const std::string&
3720 name() const;
3722 // Return the type to which this points. Give an error if the type
3723 // has not yet been defined.
3724 Type*
3725 real_type();
3727 const Type*
3728 real_type() const;
3730 // Whether the base type has been defined.
3731 bool
3732 is_defined() const;
3734 // Add a method to this type.
3735 Named_object*
3736 add_method(const std::string& name, Function*);
3738 // Add a method declaration to this type.
3739 Named_object*
3740 add_method_declaration(const std::string& name, Package*, Function_type*,
3741 Location);
3743 // Add an already created object as a method to this type.
3744 void
3745 add_existing_method(Named_object*);
3747 protected:
3749 do_traverse(Traverse* traverse);
3751 bool
3752 do_verify();
3754 bool
3755 do_has_pointer() const
3756 { return this->real_type()->has_pointer(); }
3758 bool
3759 do_compare_is_identity(Gogo* gogo)
3760 { return this->real_type()->compare_is_identity(gogo); }
3762 bool
3763 do_is_reflexive()
3764 { return this->real_type()->is_reflexive(); }
3766 bool
3767 do_needs_key_update()
3768 { return this->real_type()->needs_key_update(); }
3770 bool
3771 do_in_heap() const
3772 { return this->real_type()->in_heap(); }
3774 unsigned int
3775 do_hash_for_method(Gogo* gogo, int flags) const
3776 { return this->real_type()->hash_for_method(gogo, flags); }
3778 Btype*
3779 do_get_backend(Gogo* gogo);
3781 Expression*
3782 do_type_descriptor(Gogo*, Named_type*);
3784 void
3785 do_reflection(Gogo*, std::string*) const;
3787 void
3788 do_mangled_name(Gogo*, std::string*, bool*) const;
3790 void
3791 do_export(Export*) const;
3793 private:
3794 // Issue a warning about a use of an undefined type.
3795 void
3796 warn() const;
3798 // The type declaration.
3799 Named_object* named_object_;
3800 // Whether we have issued a warning about this type.
3801 mutable bool warned_;
3804 // The Type_context struct describes what we expect for the type of an
3805 // expression.
3807 struct Type_context
3809 // The exact type we expect, if known. This may be NULL.
3810 Type* type;
3811 // Whether an abstract type is permitted.
3812 bool may_be_abstract;
3814 // Constructors.
3815 Type_context()
3816 : type(NULL), may_be_abstract(false)
3819 Type_context(Type* a_type, bool a_may_be_abstract)
3820 : type(a_type), may_be_abstract(a_may_be_abstract)
3824 #endif // !defined(GO_TYPES_H)