Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / gcc / go / gofrontend / types.h
blob057fa014cee5d578fd3c56af829be29a03a34017
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();
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 Type* receiver_type,
1359 const Typed_identifier_list*, bool is_varargs,
1360 const Typed_identifier_list*, Location);
1362 // Build direct interface stub methods for a type.
1363 static void
1364 build_direct_iface_stub_methods(Gogo*, const Type*, Methods*, Location);
1366 static void
1367 build_one_iface_stub_method(Gogo*, Method*, const char*,
1368 const Typed_identifier_list*, bool,
1369 const Typed_identifier_list*, Location);
1371 static void
1372 add_return_from_results(Gogo*, Call_expression*,
1373 const Typed_identifier_list*, Location);
1375 static Expression*
1376 apply_field_indexes(Expression*, const Method::Field_indexes*,
1377 Location, const Type**);
1379 // Look for a field or method named NAME in TYPE.
1380 static bool
1381 find_field_or_method(const Type* type, const std::string& name,
1382 bool receiver_can_be_pointer,
1383 std::vector<const Named_type*>*, int* level,
1384 bool* is_method, bool* found_pointer_method,
1385 std::string* ambig1, std::string* ambig2);
1387 // Helper function for is_direct_iface_type, to prevent infinite
1388 // recursion.
1389 bool
1390 is_direct_iface_type_helper(Unordered_set(const Type*)*) const;
1392 // Get the backend representation for a type without looking in the
1393 // hash table for identical types.
1394 Btype*
1395 get_btype_without_hash(Gogo*);
1397 // A backend type that may be a placeholder.
1398 struct Type_btype_entry
1400 Btype *btype;
1401 bool is_placeholder;
1404 // A mapping from Type to Btype*, used to ensure that the backend
1405 // representation of identical types is identical. This is only
1406 // used for unnamed types.
1407 typedef Unordered_map_hash(const Type*, Type_btype_entry,
1408 Type_hash_identical, Type_identical) Type_btypes;
1410 static Type_btypes type_btypes;
1412 // A list of builtin named types.
1413 static std::vector<Named_type*> named_builtin_types;
1415 // A map from types that need a specific hash or equality function
1416 // to the hash or equality function.
1417 typedef Unordered_map_hash(const Type*, Named_object*, Type_hash_identical,
1418 Type_identical) Type_function;
1420 static Type_function type_hash_functions_table;
1421 static Type_function type_equal_functions_table;
1423 // Cache for reusing existing pointer types; maps from pointed-to-type
1424 // to pointer type.
1425 typedef Unordered_map(Type*, Pointer_type*) Pointer_type_table;
1427 static Pointer_type_table pointer_types;
1429 // List of placeholder pointer types.
1430 static std::vector<Type*> placeholder_pointers;
1432 // The type classification.
1433 Type_classification classification_;
1434 // The backend representation of the type, once it has been
1435 // determined.
1436 Btype* btype_;
1437 // The type descriptor for this type. This starts out as NULL and
1438 // is filled in as needed.
1439 Bvariable* type_descriptor_var_;
1440 // The GC symbol for this type. This starts out as NULL and
1441 // is filled in as needed.
1442 Bvariable* gc_symbol_var_;
1445 // Type hash table operations, treating aliases as identical to the
1446 // types that they alias.
1448 class Type_hash_identical
1450 public:
1451 unsigned int
1452 operator()(const Type* type) const
1454 return type->hash_for_method(NULL,
1455 Type::COMPARE_ERRORS | Type::COMPARE_TAGS);
1459 class Type_identical
1461 public:
1462 bool
1463 operator()(const Type* t1, const Type* t2) const
1465 return Type::are_identical(t1, t2,
1466 Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
1467 NULL);
1471 // An identifier with a type.
1473 class Typed_identifier
1475 public:
1476 Typed_identifier(const std::string& name, Type* type,
1477 Location location)
1478 : name_(name), type_(type), location_(location), note_(NULL)
1481 // Get the name.
1482 const std::string&
1483 name() const
1484 { return this->name_; }
1486 // Get the type.
1487 Type*
1488 type() const
1489 { return this->type_; }
1491 // Return the location where the name was seen. This is not always
1492 // meaningful.
1493 Location
1494 location() const
1495 { return this->location_; }
1497 // Set the type--sometimes we see the identifier before the type.
1498 void
1499 set_type(Type* type)
1501 go_assert(this->type_ == NULL || type->is_error_type());
1502 this->type_ = type;
1505 // Get the escape note.
1506 std::string*
1507 note() const
1508 { return this->note_; }
1510 // Set the escape note.
1511 void
1512 set_note(const std::string& note)
1514 if (this->note_ != NULL)
1515 go_assert(*this->note_ == note);
1516 else
1517 this->note_ = new std::string(note);
1520 private:
1521 // Identifier name.
1522 std::string name_;
1523 // Type.
1524 Type* type_;
1525 // The location where the name was seen.
1526 Location location_;
1527 // Escape note for this typed identifier. Used when importing and exporting
1528 // functions.
1529 std::string* note_;
1532 // A list of Typed_identifiers.
1534 class Typed_identifier_list
1536 public:
1537 Typed_identifier_list()
1538 : entries_()
1541 // Whether the list is empty.
1542 bool
1543 empty() const
1544 { return this->entries_.empty(); }
1546 // Return the number of entries in the list.
1547 size_t
1548 size() const
1549 { return this->entries_.size(); }
1551 // Add an entry to the end of the list.
1552 void
1553 push_back(const Typed_identifier& td)
1554 { this->entries_.push_back(td); }
1556 // Remove an entry from the end of the list.
1557 void
1558 pop_back()
1559 { this->entries_.pop_back(); }
1561 // Set the type of entry I to TYPE.
1562 void
1563 set_type(size_t i, Type* type)
1565 go_assert(i < this->entries_.size());
1566 this->entries_[i].set_type(type);
1569 // Sort the entries by name.
1570 void
1571 sort_by_name();
1573 // Traverse types.
1575 traverse(Traverse*) const;
1577 // Return the first and last elements.
1578 Typed_identifier&
1579 front()
1580 { return this->entries_.front(); }
1582 const Typed_identifier&
1583 front() const
1584 { return this->entries_.front(); }
1586 Typed_identifier&
1587 back()
1588 { return this->entries_.back(); }
1590 const Typed_identifier&
1591 back() const
1592 { return this->entries_.back(); }
1594 Typed_identifier&
1595 at(size_t i)
1596 { return this->entries_.at(i); }
1598 const Typed_identifier&
1599 at(size_t i) const
1600 { return this->entries_.at(i); }
1602 void
1603 set(size_t i, const Typed_identifier& t)
1604 { this->entries_.at(i) = t; }
1606 void
1607 resize(size_t c)
1609 go_assert(c <= this->entries_.size());
1610 this->entries_.resize(c, Typed_identifier("", NULL,
1611 Linemap::unknown_location()));
1614 void
1615 reserve(size_t c)
1616 { this->entries_.reserve(c); }
1618 // Iterators.
1620 typedef std::vector<Typed_identifier>::iterator iterator;
1621 typedef std::vector<Typed_identifier>::const_iterator const_iterator;
1623 iterator
1624 begin()
1625 { return this->entries_.begin(); }
1627 const_iterator
1628 begin() const
1629 { return this->entries_.begin(); }
1631 iterator
1632 end()
1633 { return this->entries_.end(); }
1635 const_iterator
1636 end() const
1637 { return this->entries_.end(); }
1639 // Return a copy of this list. This returns an independent copy of
1640 // the vector, but does not copy the types.
1641 Typed_identifier_list*
1642 copy() const;
1644 private:
1645 std::vector<Typed_identifier> entries_;
1648 // A type used to indicate a parsing error. This exists to simplify
1649 // later error detection.
1651 class Error_type : public Type
1653 public:
1654 Error_type()
1655 : Type(TYPE_ERROR)
1658 protected:
1659 bool
1660 do_compare_is_identity(Gogo*)
1661 { return false; }
1663 Btype*
1664 do_get_backend(Gogo* gogo);
1666 Expression*
1667 do_type_descriptor(Gogo*, Named_type*);
1669 void
1670 do_reflection(Gogo*, std::string*) const;
1672 void
1673 do_mangled_name(Gogo*, std::string*, bool*) const;
1676 // The void type.
1678 class Void_type : public Type
1680 public:
1681 Void_type()
1682 : Type(TYPE_VOID)
1685 protected:
1686 bool
1687 do_compare_is_identity(Gogo*)
1688 { return false; }
1690 Btype*
1691 do_get_backend(Gogo* gogo);
1693 Expression*
1694 do_type_descriptor(Gogo*, Named_type*)
1695 { go_unreachable(); }
1697 void
1698 do_reflection(Gogo*, std::string*) const
1701 void
1702 do_mangled_name(Gogo*, std::string*, bool*) const;
1705 // The boolean type.
1707 class Boolean_type : public Type
1709 public:
1710 Boolean_type()
1711 : Type(TYPE_BOOLEAN)
1714 protected:
1715 bool
1716 do_compare_is_identity(Gogo*)
1717 { return true; }
1719 Btype*
1720 do_get_backend(Gogo* gogo);
1722 Expression*
1723 do_type_descriptor(Gogo*, Named_type* name);
1725 // We should not be asked for the reflection string of a basic type.
1726 void
1727 do_reflection(Gogo*, std::string* ret) const
1728 { ret->append("bool"); }
1730 void
1731 do_mangled_name(Gogo*, std::string*, bool*) const;
1734 // The type of an integer.
1736 class Integer_type : public Type
1738 public:
1739 // Create a new integer type.
1740 static Named_type*
1741 create_integer_type(const char* name, bool is_unsigned, int bits,
1742 int runtime_type_kind);
1744 // Look up an existing integer type.
1745 static Named_type*
1746 lookup_integer_type(const char* name);
1748 // Create an abstract integer type.
1749 static Integer_type*
1750 create_abstract_integer_type();
1752 // Create an abstract character type.
1753 static Integer_type*
1754 create_abstract_character_type();
1756 // Create an alias to an integer type.
1757 static Named_type*
1758 create_integer_type_alias(const char* name, Named_type* real_type);
1760 // Whether this is an abstract integer type.
1761 bool
1762 is_abstract() const
1763 { return this->is_abstract_; }
1765 // Whether this is an unsigned type.
1766 bool
1767 is_unsigned() const
1768 { return this->is_unsigned_; }
1770 // The number of bits.
1772 bits() const
1773 { return this->bits_; }
1775 // Whether this type is the same as T.
1776 bool
1777 is_identical(const Integer_type* t) const;
1779 // Whether this is the type "byte" or another name for "byte".
1780 bool
1781 is_byte() const
1782 { return this->is_byte_; }
1784 // Mark this as the "byte" type.
1785 void
1786 set_is_byte()
1787 { this->is_byte_ = true; }
1789 // Whether this is the type "rune" or another name for "rune".
1790 bool
1791 is_rune() const
1792 { return this->is_rune_; }
1794 // Mark this as the "rune" type.
1795 void
1796 set_is_rune()
1797 { this->is_rune_ = true; }
1799 protected:
1800 bool
1801 do_compare_is_identity(Gogo*)
1802 { return true; }
1804 unsigned int
1805 do_hash_for_method(Gogo*, int) const;
1807 Btype*
1808 do_get_backend(Gogo*);
1810 Expression*
1811 do_type_descriptor(Gogo*, Named_type*);
1813 void
1814 do_reflection(Gogo*, std::string*) const;
1816 void
1817 do_mangled_name(Gogo*, std::string*, bool*) const;
1819 private:
1820 Integer_type(bool is_abstract, bool is_unsigned, int bits,
1821 int runtime_type_kind)
1822 : Type(TYPE_INTEGER),
1823 is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
1824 is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
1827 // Map names of integer types to the types themselves.
1828 typedef std::map<std::string, Named_type*> Named_integer_types;
1829 static Named_integer_types named_integer_types;
1831 // True if this is an abstract type.
1832 bool is_abstract_;
1833 // True if this is an unsigned type.
1834 bool is_unsigned_;
1835 // True if this is the byte type.
1836 bool is_byte_;
1837 // True if this is the rune type.
1838 bool is_rune_;
1839 // The number of bits.
1840 int bits_;
1841 // The runtime type code used in the type descriptor for this type.
1842 int runtime_type_kind_;
1845 // The type of a floating point number.
1847 class Float_type : public Type
1849 public:
1850 // Create a new float type.
1851 static Named_type*
1852 create_float_type(const char* name, int bits, int runtime_type_kind);
1854 // Look up an existing float type.
1855 static Named_type*
1856 lookup_float_type(const char* name);
1858 // Create an abstract float type.
1859 static Float_type*
1860 create_abstract_float_type();
1862 // Whether this is an abstract float type.
1863 bool
1864 is_abstract() const
1865 { return this->is_abstract_; }
1867 // The number of bits.
1869 bits() const
1870 { return this->bits_; }
1872 // Whether this type is the same as T.
1873 bool
1874 is_identical(const Float_type* t) const;
1876 protected:
1877 bool
1878 do_compare_is_identity(Gogo*)
1879 { return false; }
1881 bool
1882 do_is_reflexive()
1883 { return false; }
1885 // Distinction between +0 and -0 requires a key update.
1886 bool
1887 do_needs_key_update()
1888 { return true; }
1890 unsigned int
1891 do_hash_for_method(Gogo*, int) const;
1893 Btype*
1894 do_get_backend(Gogo*);
1896 Expression*
1897 do_type_descriptor(Gogo*, Named_type*);
1899 void
1900 do_reflection(Gogo*, std::string*) const;
1902 void
1903 do_mangled_name(Gogo*, std::string*, bool*) const;
1905 private:
1906 Float_type(bool is_abstract, int bits, int runtime_type_kind)
1907 : Type(TYPE_FLOAT),
1908 is_abstract_(is_abstract), bits_(bits),
1909 runtime_type_kind_(runtime_type_kind)
1912 // Map names of float types to the types themselves.
1913 typedef std::map<std::string, Named_type*> Named_float_types;
1914 static Named_float_types named_float_types;
1916 // True if this is an abstract type.
1917 bool is_abstract_;
1918 // The number of bits in the floating point value.
1919 int bits_;
1920 // The runtime type code used in the type descriptor for this type.
1921 int runtime_type_kind_;
1924 // The type of a complex number.
1926 class Complex_type : public Type
1928 public:
1929 // Create a new complex type.
1930 static Named_type*
1931 create_complex_type(const char* name, int bits, int runtime_type_kind);
1933 // Look up an existing complex type.
1934 static Named_type*
1935 lookup_complex_type(const char* name);
1937 // Create an abstract complex type.
1938 static Complex_type*
1939 create_abstract_complex_type();
1941 // Whether this is an abstract complex type.
1942 bool
1943 is_abstract() const
1944 { return this->is_abstract_; }
1946 // The number of bits: 64 or 128.
1947 int bits() const
1948 { return this->bits_; }
1950 // Whether this type is the same as T.
1951 bool
1952 is_identical(const Complex_type* t) const;
1954 protected:
1955 bool
1956 do_compare_is_identity(Gogo*)
1957 { return false; }
1959 bool
1960 do_is_reflexive()
1961 { return false; }
1963 // Distinction between +0 and -0 requires a key update.
1964 bool
1965 do_needs_key_update()
1966 { return true; }
1968 unsigned int
1969 do_hash_for_method(Gogo*, int) const;
1971 Btype*
1972 do_get_backend(Gogo*);
1974 Expression*
1975 do_type_descriptor(Gogo*, Named_type*);
1977 void
1978 do_reflection(Gogo*, std::string*) const;
1980 void
1981 do_mangled_name(Gogo*, std::string*, bool*) const;
1983 private:
1984 Complex_type(bool is_abstract, int bits, int runtime_type_kind)
1985 : Type(TYPE_COMPLEX),
1986 is_abstract_(is_abstract), bits_(bits),
1987 runtime_type_kind_(runtime_type_kind)
1990 // Map names of complex types to the types themselves.
1991 typedef std::map<std::string, Named_type*> Named_complex_types;
1992 static Named_complex_types named_complex_types;
1994 // True if this is an abstract type.
1995 bool is_abstract_;
1996 // The number of bits in the complex value--64 or 128.
1997 int bits_;
1998 // The runtime type code used in the type descriptor for this type.
1999 int runtime_type_kind_;
2002 // The type of a string.
2004 class String_type : public Type
2006 public:
2007 String_type()
2008 : Type(TYPE_STRING)
2011 protected:
2012 bool
2013 do_has_pointer() const
2014 { return true; }
2016 bool
2017 do_compare_is_identity(Gogo*)
2018 { return false; }
2020 // New string might have a smaller backing store.
2021 bool
2022 do_needs_key_update()
2023 { return true; }
2025 Btype*
2026 do_get_backend(Gogo*);
2028 Expression*
2029 do_type_descriptor(Gogo*, Named_type*);
2031 void
2032 do_reflection(Gogo*, std::string*) const;
2034 void
2035 do_mangled_name(Gogo*, std::string*, bool*) const;
2037 private:
2038 // The named string type.
2039 static Named_type* string_type_;
2042 // The type of a function.
2044 class Function_type : public Type
2046 public:
2047 Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
2048 Typed_identifier_list* results, Location location)
2049 : Type(TYPE_FUNCTION),
2050 receiver_(receiver), parameters_(parameters), results_(results),
2051 location_(location), is_varargs_(false), is_builtin_(false),
2052 fnbtype_(NULL), is_tagged_(false)
2055 // Get the receiver.
2056 const Typed_identifier*
2057 receiver() const
2058 { return this->receiver_; }
2060 // Add an escape note for the receiver.
2061 void
2062 add_receiver_note(int encoding)
2063 { this->receiver_->set_note(Escape_note::make_tag(encoding)); }
2065 // Get the return names and types.
2066 const Typed_identifier_list*
2067 results() const
2068 { return this->results_; }
2070 // Get the parameter names and types.
2071 const Typed_identifier_list*
2072 parameters() const
2073 { return this->parameters_; }
2075 // Add an escape note for the ith parameter.
2076 void
2077 add_parameter_note(int index, int encoding)
2078 { this->parameters_->at(index).set_note(Escape_note::make_tag(encoding)); }
2080 // Whether this function has been tagged during escape analysis.
2081 bool
2082 is_tagged() const
2083 { return this->is_tagged_; }
2085 // Mark this function as tagged after analyzing its escape.
2086 void
2087 set_is_tagged()
2088 { this->is_tagged_ = true; }
2090 // Whether this is a varargs function.
2091 bool
2092 is_varargs() const
2093 { return this->is_varargs_; }
2095 // Whether this is a builtin function.
2096 bool
2097 is_builtin() const
2098 { return this->is_builtin_; }
2100 // The location where this type was defined.
2101 Location
2102 location() const
2103 { return this->location_; }
2105 // Return whether this is a method type.
2106 bool
2107 is_method() const
2108 { return this->receiver_ != NULL; }
2110 // Whether T is a valid redeclaration of this type. This is called
2111 // when a function is declared more than once.
2112 bool
2113 is_valid_redeclaration(const Function_type* t, std::string*) const;
2115 // Whether this type is the same as T.
2116 bool
2117 is_identical(const Function_type* t, bool ignore_receiver, int flags,
2118 std::string*) const;
2120 // Record that this is a varargs function.
2121 void
2122 set_is_varargs()
2123 { this->is_varargs_ = true; }
2125 // Record that this is a builtin function.
2126 void
2127 set_is_builtin()
2128 { this->is_builtin_ = true; }
2130 // Import a function type.
2131 static Function_type*
2132 do_import(Import*);
2134 // Return a copy of this type without a receiver. This is only
2135 // valid for a method type.
2136 Function_type*
2137 copy_without_receiver() const;
2139 // Return a copy of this type with a receiver. This is used when an
2140 // interface method is attached to a named or struct type.
2141 Function_type*
2142 copy_with_receiver(Type*) const;
2144 // Return a copy of this type with the receiver treated as the first
2145 // parameter. If WANT_POINTER_RECEIVER is true, the receiver is
2146 // forced to be a pointer.
2147 Function_type*
2148 copy_with_receiver_as_param(bool want_pointer_receiver) const;
2150 // Return a copy of this type ignoring any receiver and using dummy
2151 // names for all parameters. This is used for thunks for method
2152 // values.
2153 Function_type*
2154 copy_with_names() const;
2156 static Type*
2157 make_function_type_descriptor_type();
2159 // Return the backend representation of this function type. This is used
2160 // as the real type of a backend function declaration or defintion.
2161 Btype*
2162 get_backend_fntype(Gogo*);
2164 // Return whether this is a Backend_function_type.
2165 virtual bool
2166 is_backend_function_type() const
2167 { return false; }
2169 protected:
2171 do_traverse(Traverse*);
2173 // A function descriptor may be allocated on the heap.
2174 bool
2175 do_has_pointer() const
2176 { return true; }
2178 bool
2179 do_compare_is_identity(Gogo*)
2180 { return false; }
2182 unsigned int
2183 do_hash_for_method(Gogo*, int) const;
2185 Btype*
2186 do_get_backend(Gogo*);
2188 Expression*
2189 do_type_descriptor(Gogo*, Named_type*);
2191 void
2192 do_reflection(Gogo*, std::string*) const;
2194 void
2195 do_mangled_name(Gogo*, std::string*, bool*) const;
2197 void
2198 do_export(Export*) const;
2200 private:
2201 Expression*
2202 type_descriptor_params(Type*, const Typed_identifier*,
2203 const Typed_identifier_list*);
2205 // A mapping from a list of result types to a backend struct type.
2206 class Results_hash
2208 public:
2209 unsigned int
2210 operator()(const Typed_identifier_list*) const;
2213 class Results_equal
2215 public:
2216 bool
2217 operator()(const Typed_identifier_list*,
2218 const Typed_identifier_list*) const;
2221 typedef Unordered_map_hash(Typed_identifier_list*, Btype*,
2222 Results_hash, Results_equal) Results_structs;
2224 static Results_structs results_structs;
2226 // The receiver name and type. This will be NULL for a normal
2227 // function, non-NULL for a method.
2228 Typed_identifier* receiver_;
2229 // The parameter names and types.
2230 Typed_identifier_list* parameters_;
2231 // The result names and types. This will be NULL if no result was
2232 // specified.
2233 Typed_identifier_list* results_;
2234 // The location where this type was defined. This exists solely to
2235 // give a location for the fields of the struct if this function
2236 // returns multiple values.
2237 Location location_;
2238 // Whether this function takes a variable number of arguments.
2239 bool is_varargs_;
2240 // Whether this is a special builtin function which can not simply
2241 // be called. This is used for len, cap, etc.
2242 bool is_builtin_;
2243 // The backend representation of this type for backend function
2244 // declarations and definitions.
2245 Btype* fnbtype_;
2246 // Whether this function has been analyzed by escape analysis. If this is
2247 // TRUE, this function type's parameters contain a summary of the analysis.
2248 bool is_tagged_;
2251 // The type of a function's backend representation.
2253 class Backend_function_type : public Function_type
2255 public:
2256 Backend_function_type(Typed_identifier* receiver,
2257 Typed_identifier_list* parameters,
2258 Typed_identifier_list* results, Location location)
2259 : Function_type(receiver, parameters, results, location)
2262 // Return whether this is a Backend_function_type. This overrides
2263 // Function_type::is_backend_function_type.
2264 bool
2265 is_backend_function_type() const
2266 { return true; }
2268 protected:
2269 Btype*
2270 do_get_backend(Gogo* gogo)
2271 { return this->get_backend_fntype(gogo); }
2274 // The type of a pointer.
2276 class Pointer_type : public Type
2278 public:
2279 Pointer_type(Type* to_type)
2280 : Type(TYPE_POINTER),
2281 to_type_(to_type)
2284 Type*
2285 points_to() const
2286 { return this->to_type_; }
2288 // Import a pointer type.
2289 static Pointer_type*
2290 do_import(Import*);
2292 static Type*
2293 make_pointer_type_descriptor_type();
2295 protected:
2297 do_traverse(Traverse*);
2299 bool
2300 do_verify()
2301 { return this->to_type_->verify(); }
2303 // If this is a pointer to a type that can't be in the heap, then
2304 // the garbage collector does not have to look at this, so pretend
2305 // that this is not a pointer at all.
2306 bool
2307 do_has_pointer() const
2308 { return this->to_type_->in_heap(); }
2310 bool
2311 do_compare_is_identity(Gogo*)
2312 { return true; }
2314 unsigned int
2315 do_hash_for_method(Gogo*, int) const;
2317 Btype*
2318 do_get_backend(Gogo*);
2320 Expression*
2321 do_type_descriptor(Gogo*, Named_type*);
2323 void
2324 do_reflection(Gogo*, std::string*) const;
2326 void
2327 do_mangled_name(Gogo*, std::string*, bool*) const;
2329 void
2330 do_export(Export*) const;
2332 private:
2333 // The type to which this type points.
2334 Type* to_type_;
2337 // The nil type. We use a special type for nil because it is not the
2338 // same as any other type. In C term nil has type void*, but there is
2339 // no such type in Go.
2341 class Nil_type : public Type
2343 public:
2344 Nil_type()
2345 : Type(TYPE_NIL)
2348 protected:
2349 bool
2350 do_compare_is_identity(Gogo*)
2351 { return false; }
2353 Btype*
2354 do_get_backend(Gogo* gogo);
2356 Expression*
2357 do_type_descriptor(Gogo*, Named_type*)
2358 { go_unreachable(); }
2360 void
2361 do_reflection(Gogo*, std::string*) const
2362 { go_unreachable(); }
2364 void
2365 do_mangled_name(Gogo*, std::string*, bool*) const;
2368 // The type of a field in a struct.
2370 class Struct_field
2372 public:
2373 explicit Struct_field(const Typed_identifier& typed_identifier)
2374 : typed_identifier_(typed_identifier), tag_(NULL), is_imported_(false)
2377 // The field name.
2378 const std::string&
2379 field_name() const;
2381 // Return whether this struct field is named NAME.
2382 bool
2383 is_field_name(const std::string& name) const;
2385 // Return whether this struct field is an unexported field named NAME.
2386 bool
2387 is_unexported_field_name(Gogo*, const std::string& name) const;
2389 // Return whether this struct field is an embedded built-in type.
2390 bool
2391 is_embedded_builtin(Gogo*) const;
2393 // The field type.
2394 Type*
2395 type() const
2396 { return this->typed_identifier_.type(); }
2398 // The field location.
2399 Location
2400 location() const
2401 { return this->typed_identifier_.location(); }
2403 // Whether the field has a tag.
2404 bool
2405 has_tag() const
2406 { return this->tag_ != NULL; }
2408 // The tag.
2409 const std::string&
2410 tag() const
2412 go_assert(this->tag_ != NULL);
2413 return *this->tag_;
2416 // Whether this is an anonymous field.
2417 bool
2418 is_anonymous() const
2419 { return this->typed_identifier_.name().empty(); }
2421 // Set the tag. FIXME: This is never freed.
2422 void
2423 set_tag(const std::string& tag)
2424 { this->tag_ = new std::string(tag); }
2426 // Record that this field is defined in an imported struct.
2427 void
2428 set_is_imported()
2429 { this->is_imported_ = true; }
2431 // Set the type. This is only used in error cases.
2432 void
2433 set_type(Type* type)
2434 { this->typed_identifier_.set_type(type); }
2436 private:
2437 // The field name, type, and location.
2438 Typed_identifier typed_identifier_;
2439 // The field tag. This is NULL if the field has no tag.
2440 std::string* tag_;
2441 // Whether this field is defined in an imported struct.
2442 bool is_imported_;
2445 // A list of struct fields.
2447 class Struct_field_list
2449 public:
2450 Struct_field_list()
2451 : entries_()
2454 // Whether the list is empty.
2455 bool
2456 empty() const
2457 { return this->entries_.empty(); }
2459 // Return the number of entries.
2460 size_t
2461 size() const
2462 { return this->entries_.size(); }
2464 // Add an entry to the end of the list.
2465 void
2466 push_back(const Struct_field& sf)
2467 { this->entries_.push_back(sf); }
2469 // Index into the list.
2470 const Struct_field&
2471 at(size_t i) const
2472 { return this->entries_.at(i); }
2474 // Last entry in list.
2475 Struct_field&
2476 back()
2477 { return this->entries_.back(); }
2479 // Iterators.
2481 typedef std::vector<Struct_field>::iterator iterator;
2482 typedef std::vector<Struct_field>::const_iterator const_iterator;
2484 iterator
2485 begin()
2486 { return this->entries_.begin(); }
2488 const_iterator
2489 begin() const
2490 { return this->entries_.begin(); }
2492 iterator
2493 end()
2494 { return this->entries_.end(); }
2496 const_iterator
2497 end() const
2498 { return this->entries_.end(); }
2500 private:
2501 std::vector<Struct_field> entries_;
2504 // The type of a struct.
2506 class Struct_type : public Type
2508 public:
2509 Struct_type(Struct_field_list* fields, Location location)
2510 : Type(TYPE_STRUCT),
2511 fields_(fields), location_(location), all_methods_(NULL),
2512 is_struct_incomparable_(false), has_padding_(false),
2513 is_results_struct_(false)
2516 // Return the field NAME. This only looks at local fields, not at
2517 // embedded types. If the field is found, and PINDEX is not NULL,
2518 // this sets *PINDEX to the field index. If the field is not found,
2519 // this returns NULL.
2520 const Struct_field*
2521 find_local_field(const std::string& name, unsigned int *pindex) const;
2523 // Return the field number INDEX.
2524 const Struct_field*
2525 field(unsigned int index) const
2526 { return &this->fields_->at(index); }
2528 // Get the struct fields.
2529 const Struct_field_list*
2530 fields() const
2531 { return this->fields_; }
2533 // Return the number of fields.
2534 size_t
2535 field_count() const
2536 { return this->fields_->size(); }
2538 // Location of struct definition.
2539 Location
2540 location() const
2541 { return this->location_; }
2543 // Push a new field onto the end of the struct. This is used when
2544 // building a closure variable.
2545 void
2546 push_field(const Struct_field& sf)
2547 { this->fields_->push_back(sf); }
2549 // Return an expression referring to field NAME in STRUCT_EXPR, or
2550 // NULL if there is no field with that name.
2551 Field_reference_expression*
2552 field_reference(Expression* struct_expr, const std::string& name,
2553 Location) const;
2555 // Return the total number of fields, including embedded fields.
2556 // This is the number of values that can appear in a conversion to
2557 // this type.
2558 unsigned int
2559 total_field_count() const;
2561 // Whether this type is identical with T.
2562 bool
2563 is_identical(const Struct_type* t, int) const;
2565 // Return whether NAME is a local field which is not exported. This
2566 // is only used for better error reporting.
2567 bool
2568 is_unexported_local_field(Gogo*, const std::string& name) const;
2570 // If this is an unnamed struct, build the complete list of methods,
2571 // including those from anonymous fields, and build methods stubs if
2572 // needed.
2573 void
2574 finalize_methods(Gogo*);
2576 // Return whether this type has any methods. This should only be
2577 // called after the finalize_methods pass.
2578 bool
2579 has_any_methods() const
2580 { return this->all_methods_ != NULL; }
2582 // Return the methods for this type. This should only be called
2583 // after the finalize_methods pass.
2584 const Methods*
2585 methods() const
2586 { return this->all_methods_; }
2588 // Return the method to use for NAME. This returns NULL if there is
2589 // no such method or if the method is ambiguous. When it returns
2590 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2591 Method*
2592 method_function(const std::string& name, bool* is_ambiguous) const;
2594 // Return a pointer to the interface method table for this type for
2595 // the interface INTERFACE. If IS_POINTER is true, set the type
2596 // descriptor to a pointer to this type, otherwise set it to this
2597 // type.
2598 Expression*
2599 interface_method_table(Interface_type* interface, bool is_pointer);
2601 // Traverse just the field types of a struct type.
2603 traverse_field_types(Traverse* traverse)
2604 { return this->do_traverse(traverse); }
2606 // If the offset of field INDEX in the backend implementation can be
2607 // determined, set *POFFSET to the offset in bytes and return true.
2608 // Otherwise, return false.
2609 bool
2610 backend_field_offset(Gogo*, unsigned int index, int64_t* poffset);
2612 // Finish the backend representation of all the fields.
2613 void
2614 finish_backend_fields(Gogo*);
2616 // Import a struct type.
2617 static Struct_type*
2618 do_import(Import*);
2620 static Type*
2621 make_struct_type_descriptor_type();
2623 // Return whether this is a generated struct that is not comparable.
2624 bool
2625 is_struct_incomparable() const
2626 { return this->is_struct_incomparable_; }
2628 // Record that this is a generated struct that is not comparable.
2629 void
2630 set_is_struct_incomparable()
2631 { this->is_struct_incomparable_ = true; }
2633 // Return whether this struct's backend type has padding, due to
2634 // trailing zero-sized field.
2635 bool
2636 has_padding() const
2637 { return this->has_padding_; }
2639 // Record that this struct's backend type has padding.
2640 void
2641 set_has_padding()
2642 { this->has_padding_ = true; }
2644 // Return whether this is a results struct created to hold the
2645 // results of a function that returns multiple results.
2646 bool
2647 is_results_struct() const
2648 { return this->is_results_struct_; }
2650 // Record that this is a results struct.
2651 void
2652 set_is_results_struct()
2653 { this->is_results_struct_ = true; }
2655 // Write the hash function for this type.
2656 void
2657 write_hash_function(Gogo*, Function_type*);
2659 // Write the equality function for this type.
2660 void
2661 write_equal_function(Gogo*, Named_type*);
2663 // Whether we can write this type to a C header file, to implement
2664 // -fgo-c-header.
2665 bool
2666 can_write_to_c_header(std::vector<const Named_object*>*,
2667 std::vector<const Named_object*>*) const;
2669 // Write this type to a C header file, to implement -fgo-c-header.
2670 void
2671 write_to_c_header(std::ostream&) const;
2673 protected:
2675 do_traverse(Traverse*);
2677 bool
2678 do_verify();
2680 bool
2681 do_has_pointer() const;
2683 bool
2684 do_compare_is_identity(Gogo*);
2686 bool
2687 do_is_reflexive();
2689 bool
2690 do_needs_key_update();
2692 bool
2693 do_hash_might_panic();
2695 bool
2696 do_in_heap() const;
2698 unsigned int
2699 do_hash_for_method(Gogo*, int) const;
2701 Btype*
2702 do_get_backend(Gogo*);
2704 Expression*
2705 do_type_descriptor(Gogo*, Named_type*);
2707 void
2708 do_reflection(Gogo*, std::string*) const;
2710 void
2711 do_mangled_name(Gogo*, std::string*, bool*) const;
2713 void
2714 do_export(Export*) const;
2716 private:
2717 bool
2718 can_write_type_to_c_header(const Type*,
2719 std::vector<const Named_object*>*,
2720 std::vector<const Named_object*>*) const;
2722 void
2723 write_field_to_c_header(std::ostream&, const std::string&, const Type*) const;
2725 // Used to merge method sets of identical unnamed structs.
2726 typedef Unordered_map_hash(Struct_type*, Struct_type*, Type_hash_identical,
2727 Type_identical) Identical_structs;
2729 static Identical_structs identical_structs;
2731 // Used to manage method tables for identical unnamed structs.
2732 typedef std::pair<Interface_method_tables*, Interface_method_tables*>
2733 Struct_method_table_pair;
2735 typedef Unordered_map_hash(Struct_type*, Struct_method_table_pair*,
2736 Type_hash_identical, Type_identical)
2737 Struct_method_tables;
2739 static Struct_method_tables struct_method_tables;
2741 // Used to avoid infinite loops in field_reference_depth.
2742 struct Saw_named_type
2744 Saw_named_type* next;
2745 Named_type* nt;
2748 Field_reference_expression*
2749 field_reference_depth(Expression* struct_expr, const std::string& name,
2750 Location, Saw_named_type*,
2751 unsigned int* depth) const;
2753 // The fields of the struct.
2754 Struct_field_list* fields_;
2755 // The place where the struct was declared.
2756 Location location_;
2757 // If this struct is unnamed, a list of methods.
2758 Methods* all_methods_;
2759 // True if this is a generated struct that is not considered to be
2760 // comparable.
2761 bool is_struct_incomparable_;
2762 // True if this struct's backend type has padding, due to trailing
2763 // zero-sized field.
2764 bool has_padding_;
2765 // True if this is a results struct created to hold the results of a
2766 // function that returns multiple results.
2767 bool is_results_struct_;
2770 // The type of an array.
2772 class Array_type : public Type
2774 public:
2775 Array_type(Type* element_type, Expression* length)
2776 : Type(TYPE_ARRAY),
2777 element_type_(element_type), length_(length), blength_(NULL),
2778 issued_length_error_(false), is_array_incomparable_(false)
2781 // Return the element type.
2782 Type*
2783 element_type() const
2784 { return this->element_type_; }
2786 // Return the length. This will return NULL for a slice.
2787 Expression*
2788 length() const
2789 { return this->length_; }
2791 // Store the length as an int64_t into *PLEN. Return false if the
2792 // length can not be determined. This will assert if called for a
2793 // slice.
2794 bool
2795 int_length(int64_t* plen) const;
2797 // Whether this type is identical with T.
2798 bool
2799 is_identical(const Array_type* t, int) const;
2801 // Return an expression for the pointer to the values in an array.
2802 Expression*
2803 get_value_pointer(Gogo*, Expression* array) const;
2805 // Return an expression for the length of an array with this type.
2806 Expression*
2807 get_length(Gogo*, Expression* array) const;
2809 // Return an expression for the capacity of an array with this type.
2810 Expression*
2811 get_capacity(Gogo*, Expression* array) const;
2813 // Import an array type.
2814 static Array_type*
2815 do_import(Import*);
2817 // Return the backend representation of the element type.
2818 Btype*
2819 get_backend_element(Gogo*, bool use_placeholder);
2821 // Return the backend representation of the length.
2822 Bexpression*
2823 get_backend_length(Gogo*);
2825 // Finish the backend representation of the element type.
2826 void
2827 finish_backend_element(Gogo*);
2829 static Type*
2830 make_array_type_descriptor_type();
2832 static Type*
2833 make_slice_type_descriptor_type();
2835 // Return whether this is a generated array that is not comparable.
2836 bool
2837 is_array_incomparable() const
2838 { return this->is_array_incomparable_; }
2840 // Record that this is a generated array that is not comparable.
2841 void
2842 set_is_array_incomparable()
2843 { this->is_array_incomparable_ = true; }
2845 // Write the hash function for this type.
2846 void
2847 write_hash_function(Gogo*, Function_type*);
2849 // Write the equality function for this type.
2850 void
2851 write_equal_function(Gogo*, Named_type*);
2853 protected:
2855 do_traverse(Traverse* traverse);
2857 bool
2858 do_verify();
2860 bool
2861 do_has_pointer() const;
2863 bool
2864 do_compare_is_identity(Gogo*);
2866 bool
2867 do_is_reflexive()
2869 return this->length_ != NULL && this->element_type_->is_reflexive();
2872 bool
2873 do_needs_key_update()
2874 { return this->element_type_->needs_key_update(); }
2876 bool
2877 do_hash_might_panic()
2878 { return this->length_ != NULL && this->element_type_->hash_might_panic(); }
2880 bool
2881 do_in_heap() const
2882 { return this->length_ == NULL || this->element_type_->in_heap(); }
2884 unsigned int
2885 do_hash_for_method(Gogo*, int) const;
2887 Btype*
2888 do_get_backend(Gogo*);
2890 Expression*
2891 do_type_descriptor(Gogo*, Named_type*);
2893 void
2894 do_reflection(Gogo*, std::string*) const;
2896 void
2897 do_mangled_name(Gogo*, std::string*, bool*) const;
2899 void
2900 do_export(Export*) const;
2902 private:
2903 bool
2904 verify_length();
2906 Expression*
2907 array_type_descriptor(Gogo*, Named_type*);
2909 Expression*
2910 slice_type_descriptor(Gogo*, Named_type*);
2912 // The type of elements of the array.
2913 Type* element_type_;
2914 // The number of elements. This may be NULL.
2915 Expression* length_;
2916 // The backend representation of the length.
2917 // We only want to compute this once.
2918 Bexpression* blength_;
2919 // Whether or not an invalid length error has been issued for this type,
2920 // to avoid knock-on errors.
2921 mutable bool issued_length_error_;
2922 // True if this is a generated array that is not considered to be
2923 // comparable.
2924 bool is_array_incomparable_;
2927 // The type of a map.
2929 class Map_type : public Type
2931 public:
2932 Map_type(Type* key_type, Type* val_type, Location location)
2933 : Type(TYPE_MAP),
2934 key_type_(key_type), val_type_(val_type), hmap_type_(NULL),
2935 bucket_type_(NULL), hiter_type_(NULL), location_(location)
2938 // Return the key type.
2939 Type*
2940 key_type() const
2941 { return this->key_type_; }
2943 // Return the value type.
2944 Type*
2945 val_type() const
2946 { return this->val_type_; }
2948 // Return the type used for an iteration over this map.
2949 Type*
2950 hiter_type(Gogo*);
2952 // If this map requires the "fat" functions, returns the pointer to
2953 // pass as the zero value to those functions. Otherwise, in the
2954 // normal case, returns NULL.
2955 Expression*
2956 fat_zero_value(Gogo*);
2958 // Map algorithm to use for this map type. We may use specialized
2959 // fast map routines for certain key types.
2960 enum Map_alg
2962 // 32-bit key.
2963 MAP_ALG_FAST32,
2964 // 32-bit pointer key.
2965 MAP_ALG_FAST32PTR,
2966 // 64-bit key.
2967 MAP_ALG_FAST64,
2968 // 64-bit pointer key.
2969 MAP_ALG_FAST64PTR,
2970 // String key.
2971 MAP_ALG_FASTSTR,
2972 // Anything else.
2973 MAP_ALG_SLOW,
2976 Map_alg
2977 algorithm(Gogo*);
2979 // Return whether VAR is the map zero value.
2980 static bool
2981 is_zero_value(Variable* var);
2983 // Return the backend representation of the map zero value.
2984 static Bvariable*
2985 backend_zero_value(Gogo*);
2987 // Whether this type is identical with T.
2988 bool
2989 is_identical(const Map_type* t, int) const;
2991 // Import a map type.
2992 static Map_type*
2993 do_import(Import*);
2995 static Type*
2996 make_map_type_descriptor_type();
2998 // This must be in sync with libgo/go/runtime/map.go.
2999 static const int bucket_size = 8;
3001 protected:
3003 do_traverse(Traverse*);
3005 bool
3006 do_verify();
3008 bool
3009 do_has_pointer() const
3010 { return true; }
3012 bool
3013 do_compare_is_identity(Gogo*)
3014 { return false; }
3016 bool
3017 do_is_reflexive()
3019 return this->key_type_->is_reflexive() && this->val_type_->is_reflexive();
3022 unsigned int
3023 do_hash_for_method(Gogo*, int) const;
3025 Btype*
3026 do_get_backend(Gogo*);
3028 Expression*
3029 do_type_descriptor(Gogo*, Named_type*);
3031 void
3032 do_reflection(Gogo*, std::string*) const;
3034 void
3035 do_mangled_name(Gogo*, std::string*, bool*) const;
3037 void
3038 do_export(Export*) const;
3040 private:
3041 // These must be in sync with libgo/go/runtime/map.go.
3042 static const int max_key_size = 128;
3043 static const int max_val_size = 128;
3044 static const int max_zero_size = 1024;
3046 // Maps with value types larger than max_zero_size require passing a
3047 // zero value pointer to the map functions.
3049 // The zero value variable.
3050 static Named_object* zero_value;
3052 // The current size of the zero value.
3053 static int64_t zero_value_size;
3055 // The current alignment of the zero value.
3056 static int64_t zero_value_align;
3058 Type*
3059 bucket_type(Gogo*, int64_t, int64_t);
3061 Type*
3062 hmap_type(Type*);
3064 // The key type.
3065 Type* key_type_;
3066 // The value type.
3067 Type* val_type_;
3068 // The hashmap type. At run time a map is represented as a pointer
3069 // to this type.
3070 Type* hmap_type_;
3071 // The bucket type, the type used to hold keys and values at run time.
3072 Type* bucket_type_;
3073 // The iterator type.
3074 Type* hiter_type_;
3075 // Where the type was defined.
3076 Location location_;
3079 // The type of a channel.
3081 class Channel_type : public Type
3083 public:
3084 Channel_type(bool may_send, bool may_receive, Type* element_type)
3085 : Type(TYPE_CHANNEL),
3086 may_send_(may_send), may_receive_(may_receive),
3087 element_type_(element_type)
3088 { go_assert(may_send || may_receive); }
3090 // Whether this channel can send data.
3091 bool
3092 may_send() const
3093 { return this->may_send_; }
3095 // Whether this channel can receive data.
3096 bool
3097 may_receive() const
3098 { return this->may_receive_; }
3100 // The type of the values that may be sent on this channel. This is
3101 // NULL if any type may be sent.
3102 Type*
3103 element_type() const
3104 { return this->element_type_; }
3106 // Whether this type is identical with T.
3107 bool
3108 is_identical(const Channel_type* t, int) const;
3110 // Import a channel type.
3111 static Channel_type*
3112 do_import(Import*);
3114 static Type*
3115 make_chan_type_descriptor_type();
3117 static Type*
3118 select_case_type();
3120 protected:
3122 do_traverse(Traverse* traverse)
3123 { return Type::traverse(this->element_type_, traverse); }
3125 bool
3126 do_verify();
3128 bool
3129 do_has_pointer() const
3130 { return true; }
3132 bool
3133 do_compare_is_identity(Gogo*)
3134 { return true; }
3136 unsigned int
3137 do_hash_for_method(Gogo*, int) const;
3139 Btype*
3140 do_get_backend(Gogo*);
3142 Expression*
3143 do_type_descriptor(Gogo*, Named_type*);
3145 void
3146 do_reflection(Gogo*, std::string*) const;
3148 void
3149 do_mangled_name(Gogo*, std::string*, bool*) const;
3151 void
3152 do_export(Export*) const;
3154 private:
3155 // Whether this channel can send data.
3156 bool may_send_;
3157 // Whether this channel can receive data.
3158 bool may_receive_;
3159 // The types of elements which may be sent on this channel. If this
3160 // is NULL, it means that any type may be sent.
3161 Type* element_type_;
3164 // An interface type.
3166 class Interface_type : public Type
3168 public:
3169 Interface_type(Typed_identifier_list* methods, Location location)
3170 : Type(TYPE_INTERFACE),
3171 parse_methods_(methods), all_methods_(NULL), location_(location),
3172 package_(NULL), interface_btype_(NULL), bmethods_(NULL),
3173 assume_identical_(NULL), methods_are_finalized_(false),
3174 bmethods_is_placeholder_(false), seen_(false)
3175 { go_assert(methods == NULL || !methods->empty()); }
3177 // The location where the interface type was defined.
3178 Location
3179 location() const
3180 { return this->location_; }
3182 // The package where the interface type was defined. Returns NULL
3183 // for the package currently being compiled.
3184 Package*
3185 package() const
3186 { return this->package_; }
3188 // Return whether this is an empty interface.
3189 bool
3190 is_empty() const
3192 go_assert(this->methods_are_finalized_);
3193 return this->all_methods_ == NULL;
3196 // Return the list of locally defined methods. This will return NULL
3197 // for an empty interface. Embedded interfaces will appear in this
3198 // list as an entry with no name.
3199 const Typed_identifier_list*
3200 local_methods() const
3201 { return this->parse_methods_; }
3203 // Return the list of all methods. This will return NULL for an
3204 // empty interface.
3205 const Typed_identifier_list*
3206 methods() const;
3208 // Return the number of methods.
3209 size_t
3210 method_count() const;
3212 // Return the method NAME, or NULL.
3213 const Typed_identifier*
3214 find_method(const std::string& name) const;
3216 // Return the zero-based index of method NAME.
3217 size_t
3218 method_index(const std::string& name) const;
3220 // Finalize the methods. This sets all_methods_. This handles
3221 // interface inheritance.
3222 void
3223 finalize_methods();
3225 // Return true if T implements this interface. If this returns
3226 // false, and REASON is not NULL, it sets *REASON to the reason that
3227 // it fails.
3228 bool
3229 implements_interface(const Type* t, std::string* reason) const;
3231 // Whether this type is identical with T. REASON is as in
3232 // implements_interface.
3233 bool
3234 is_identical(const Interface_type* t, int) const;
3236 // Whether we can assign T to this type. is_identical is known to
3237 // be false.
3238 bool
3239 is_compatible_for_assign(const Interface_type*, std::string* reason) const;
3241 // Return whether NAME is a method which is not exported. This is
3242 // only used for better error reporting.
3243 bool
3244 is_unexported_method(Gogo*, const std::string& name) const;
3246 // Import an interface type.
3247 static Interface_type*
3248 do_import(Import*);
3250 // Make a struct for an empty interface type.
3251 static Btype*
3252 get_backend_empty_interface_type(Gogo*);
3254 // Get a pointer to the backend representation of the method table.
3255 Btype*
3256 get_backend_methods(Gogo*);
3258 // Return a placeholder for the backend representation of the
3259 // pointer to the method table.
3260 Btype*
3261 get_backend_methods_placeholder(Gogo*);
3263 // Finish the backend representation of the method types.
3264 void
3265 finish_backend_methods(Gogo*);
3267 static Type*
3268 make_interface_type_descriptor_type();
3270 // Return whether methods are finalized for this interface.
3271 bool
3272 methods_are_finalized() const
3273 { return this->methods_are_finalized_; }
3275 protected:
3277 do_traverse(Traverse*);
3279 bool
3280 do_has_pointer() const
3281 { return true; }
3283 bool
3284 do_compare_is_identity(Gogo*)
3285 { return false; }
3287 // Not reflexive if it contains a float.
3288 bool
3289 do_is_reflexive()
3290 { return false; }
3292 // Distinction between +0 and -0 requires a key update if it
3293 // contains a float.
3294 bool
3295 do_needs_key_update()
3296 { return true; }
3298 // Hashing an unhashable type stored in an interface might panic.
3299 bool
3300 do_hash_might_panic()
3301 { return true; }
3303 unsigned int
3304 do_hash_for_method(Gogo*, int) const;
3306 Btype*
3307 do_get_backend(Gogo*);
3309 Expression*
3310 do_type_descriptor(Gogo*, Named_type*);
3312 void
3313 do_reflection(Gogo*, std::string*) const;
3315 void
3316 do_mangled_name(Gogo*, std::string*, bool*) const;
3318 void
3319 do_export(Export*) const;
3321 private:
3322 // This type guards against infinite recursion when comparing
3323 // interface types. We keep a list of interface types assumed to be
3324 // identical during comparison. We just keep the list on the stack.
3325 // This permits us to compare cases like
3326 // type I1 interface { F() interface{I1} }
3327 // type I2 interface { F() interface{I2} }
3328 struct Assume_identical
3330 Assume_identical* next;
3331 const Interface_type* t1;
3332 const Interface_type* t2;
3335 bool
3336 assume_identical(const Interface_type*, const Interface_type*) const;
3338 struct Bmethods_map_entry
3340 Btype *btype;
3341 bool is_placeholder;
3344 // A mapping from Interface_type to the backend type of its bmethods_,
3345 // used to ensure that the backend representation of identical types
3346 // is identical.
3347 typedef Unordered_map_hash(const Interface_type*, Bmethods_map_entry,
3348 Type_hash_identical, Type_identical) Bmethods_map;
3350 static Bmethods_map bmethods_map;
3352 // The list of methods associated with the interface from the
3353 // parser. This will be NULL for the empty interface. This may
3354 // include unnamed interface types.
3355 Typed_identifier_list* parse_methods_;
3356 // The list of all methods associated with the interface. This
3357 // expands any interface types listed in methods_. It is set by
3358 // finalize_methods. This will be NULL for the empty interface.
3359 Typed_identifier_list* all_methods_;
3360 // The location where the interface was defined.
3361 Location location_;
3362 // The package where the interface was defined. This is NULL for
3363 // the package being compiled.
3364 Package* package_;
3365 // The backend representation of this type during backend conversion.
3366 Btype* interface_btype_;
3367 // The backend representation of the pointer to the method table.
3368 Btype* bmethods_;
3369 // A list of interface types assumed to be identical during
3370 // interface comparison.
3371 mutable Assume_identical* assume_identical_;
3372 // Whether the methods have been finalized.
3373 bool methods_are_finalized_;
3374 // Whether the bmethods_ field is a placeholder.
3375 bool bmethods_is_placeholder_;
3376 // Used to avoid endless recursion in do_mangled_name.
3377 mutable bool seen_;
3380 // The value we keep for a named type. This lets us get the right
3381 // name when we convert to backend. Note that we don't actually keep
3382 // the name here; the name is in the Named_object which points to
3383 // this. This object exists to hold a unique backend representation for
3384 // the type.
3386 class Named_type : public Type
3388 public:
3389 Named_type(Named_object* named_object, Type* type, Location location)
3390 : Type(TYPE_NAMED),
3391 named_object_(named_object), in_function_(NULL), in_function_index_(0),
3392 type_(type), local_methods_(NULL), all_methods_(NULL),
3393 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
3394 location_(location), named_btype_(NULL), dependencies_(),
3395 is_alias_(false), is_visible_(true), is_error_(false), in_heap_(true),
3396 is_placeholder_(false), is_converted_(false), is_verified_(false),
3397 seen_(false), seen_in_compare_is_identity_(false),
3398 seen_in_get_backend_(false), seen_alias_(false)
3401 // Return the associated Named_object. This holds the actual name.
3402 Named_object*
3403 named_object()
3404 { return this->named_object_; }
3406 const Named_object*
3407 named_object() const
3408 { return this->named_object_; }
3410 // Set the Named_object. This is used when we see a type
3411 // declaration followed by a type.
3412 void
3413 set_named_object(Named_object* no)
3414 { this->named_object_ = no; }
3416 // Whether this is an alias (type T1 = T2) rather than an ordinary
3417 // named type (type T1 T2).
3418 bool
3419 is_alias() const
3420 { return this->is_alias_; }
3422 // Record that this type is an alias.
3423 void
3424 set_is_alias()
3425 { this->is_alias_ = true; }
3427 // Mark this type as not permitted in the heap.
3428 void
3429 set_not_in_heap()
3430 { this->in_heap_ = false; }
3432 // Return the function in which this type is defined. This will
3433 // return NULL for a type defined in global scope.
3434 const Named_object*
3435 in_function(unsigned int *pindex) const
3437 *pindex = this->in_function_index_;
3438 return this->in_function_;
3441 // Set the function in which this type is defined.
3442 void
3443 set_in_function(Named_object* f, unsigned int index)
3445 this->in_function_ = f;
3446 this->in_function_index_ = index;
3449 // Return the name of the type.
3450 const std::string&
3451 name() const;
3453 // Return the name of the type for an error message. The difference
3454 // is that if the type is defined in a different package, this will
3455 // return PACKAGE.NAME.
3456 std::string
3457 message_name() const;
3459 // Return the underlying type.
3460 Type*
3461 real_type()
3462 { return this->type_; }
3464 const Type*
3465 real_type() const
3466 { return this->type_; }
3468 // Return the location.
3469 Location
3470 location() const
3471 { return this->location_; }
3473 // Whether this type is visible. This only matters when parsing.
3474 bool
3475 is_visible() const
3476 { return this->is_visible_; }
3478 // Mark this type as visible.
3479 void
3480 set_is_visible()
3481 { this->is_visible_ = true; }
3483 // Mark this type as invisible.
3484 void
3485 clear_is_visible()
3486 { this->is_visible_ = false; }
3488 // Whether this is a builtin type.
3489 bool
3490 is_builtin() const
3491 { return Linemap::is_predeclared_location(this->location_); }
3493 // Whether this named type is valid. A recursive named type is invalid.
3494 bool
3495 is_valid() const
3496 { return !this->is_error_; }
3498 // Return the base type for this type.
3499 Type*
3500 named_base();
3502 const Type*
3503 named_base() const;
3505 // Return whether this is an error type.
3506 bool
3507 is_named_error_type() const;
3509 // Return whether this type is comparable. If REASON is not NULL,
3510 // set *REASON when returning false.
3511 bool
3512 named_type_is_comparable(std::string* reason) const;
3514 // Add a method to this type.
3515 Named_object*
3516 add_method(const std::string& name, Function*);
3518 // Add a method declaration to this type.
3519 Named_object*
3520 add_method_declaration(const std::string& name, Package* package,
3521 Function_type* type, Location location);
3523 // Add an existing method--one defined before the type itself was
3524 // defined--to a type.
3525 void
3526 add_existing_method(Named_object*);
3528 // Look up a local method.
3529 Named_object*
3530 find_local_method(const std::string& name) const;
3532 // Return the list of local methods.
3533 const Bindings*
3534 local_methods() const;
3536 // Build the complete list of methods, including those from
3537 // anonymous fields, and build method stubs if needed.
3538 void
3539 finalize_methods(Gogo*);
3541 // Return whether this type has any methods. This should only be
3542 // called after the finalize_methods pass.
3543 bool
3544 has_any_methods() const;
3546 // Return the methods for this type. This should only be called
3547 // after the finalized_methods pass.
3548 const Methods*
3549 methods() const;
3551 // Return the method to use for NAME. This returns NULL if there is
3552 // no such method or if the method is ambiguous. When it returns
3553 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
3554 Method*
3555 method_function(const std::string& name, bool *is_ambiguous) const;
3557 // Return whether NAME is a known field or method which is not
3558 // exported. This is only used for better error reporting.
3559 bool
3560 is_unexported_local_method(Gogo*, const std::string& name) const;
3562 // Return a pointer to the interface method table for this type for
3563 // the interface INTERFACE. If IS_POINTER is true, set the type
3564 // descriptor to a pointer to this type, otherwise set it to this
3565 // type.
3566 Expression*
3567 interface_method_table(Interface_type* interface, bool is_pointer);
3569 // Note that a type must be converted to the backend representation
3570 // before we convert this type.
3571 void
3572 add_dependency(Named_type* nt)
3573 { this->dependencies_.push_back(nt); }
3575 // Return true if the size and alignment of the backend
3576 // representation of this type is known. This is always true after
3577 // types have been converted, but may be false beforehand.
3578 bool
3579 is_named_backend_type_size_known() const
3580 { return this->named_btype_ != NULL && !this->is_placeholder_; }
3582 // Add to the reflection string as for Type::append_reflection, but
3583 // if USE_ALIAS use the alias name rather than the alias target.
3584 void
3585 append_reflection_type_name(Gogo*, bool use_alias, std::string*) const;
3587 // Append the symbol type name as for Type::append_mangled_name,
3588 // but if USE_ALIAS use the alias name rather than the alias target.
3589 void
3590 append_symbol_type_name(Gogo*, bool use_alias, std::string*,
3591 bool* is_non_identifier) const;
3593 // Import a named type.
3594 static void
3595 import_named_type(Import*, Named_type**);
3597 // Initial conversion to backend representation.
3598 void
3599 convert(Gogo*);
3601 protected:
3603 do_traverse(Traverse* traverse)
3604 { return Type::traverse(this->type_, traverse); }
3606 bool
3607 do_verify();
3609 bool
3610 do_has_pointer() const;
3612 bool
3613 do_compare_is_identity(Gogo*);
3615 bool
3616 do_is_reflexive();
3618 bool
3619 do_needs_key_update();
3621 bool
3622 do_in_heap() const;
3624 unsigned int
3625 do_hash_for_method(Gogo*, int) const;
3627 Btype*
3628 do_get_backend(Gogo*);
3630 Expression*
3631 do_type_descriptor(Gogo*, Named_type*);
3633 void
3634 do_reflection(Gogo*, std::string*) const;
3636 void
3637 do_mangled_name(Gogo*, std::string*, bool*) const;
3639 void
3640 do_export(Export*) const;
3642 private:
3643 // Create the placeholder during conversion.
3644 void
3645 create_placeholder(Gogo*);
3647 // A pointer back to the Named_object for this type.
3648 Named_object* named_object_;
3649 // If this type is defined in a function, a pointer back to the
3650 // function in which it is defined.
3651 Named_object* in_function_;
3652 // The index of this type in IN_FUNCTION_.
3653 unsigned int in_function_index_;
3654 // The actual type.
3655 Type* type_;
3656 // The list of methods defined for this type. Any named type can
3657 // have methods.
3658 Bindings* local_methods_;
3659 // The full list of methods for this type, including methods
3660 // declared for anonymous fields.
3661 Methods* all_methods_;
3662 // A mapping from interfaces to the associated interface method
3663 // tables for this type.
3664 Interface_method_tables* interface_method_tables_;
3665 // A mapping from interfaces to the associated interface method
3666 // tables for pointers to this type.
3667 Interface_method_tables* pointer_interface_method_tables_;
3668 // The location where this type was defined.
3669 Location location_;
3670 // The backend representation of this type during backend
3671 // conversion. This is used to avoid endless recursion when a named
3672 // type refers to itself.
3673 Btype* named_btype_;
3674 // A list of types which must be converted to the backend
3675 // representation before this type can be converted. This is for
3676 // cases like
3677 // type S1 { p *S2 }
3678 // type S2 { s S1 }
3679 // where we can't convert S2 to the backend representation unless we
3680 // have converted S1.
3681 std::vector<Named_type*> dependencies_;
3682 // Whether this is an alias type.
3683 bool is_alias_;
3684 // Whether this type is visible. This is false if this type was
3685 // created because it was referenced by an imported object, but the
3686 // type itself was not exported. This will always be true for types
3687 // created in the current package.
3688 bool is_visible_;
3689 // Whether this type is erroneous.
3690 bool is_error_;
3691 // Whether this type is permitted in the heap. This is true by
3692 // default, false if there is a magic //go:notinheap comment.
3693 bool in_heap_;
3694 // Whether the current value of named_btype_ is a placeholder for
3695 // which the final size of the type is not known.
3696 bool is_placeholder_;
3697 // Whether this type has been converted to the backend
3698 // representation. Implies that is_placeholder_ is false.
3699 bool is_converted_;
3700 // Whether this type has been verified.
3701 bool is_verified_;
3702 // In a recursive operation such as has_pointer, this flag is used
3703 // to prevent infinite recursion when a type refers to itself. This
3704 // is mutable because it is always reset to false when the function
3705 // exits.
3706 mutable bool seen_;
3707 // Like seen_, but used only by do_compare_is_identity.
3708 bool seen_in_compare_is_identity_;
3709 // Like seen_, but used only by do_get_backend.
3710 bool seen_in_get_backend_;
3711 // Like seen_, but used when resolving aliases.
3712 mutable bool seen_alias_;
3715 // A forward declaration. This handles a type which has been declared
3716 // but not defined.
3718 class Forward_declaration_type : public Type
3720 public:
3721 Forward_declaration_type(Named_object* named_object);
3723 // The named object associated with this type declaration. This
3724 // will be resolved.
3725 Named_object*
3726 named_object();
3728 const Named_object*
3729 named_object() const;
3731 // Return the name of the type.
3732 const std::string&
3733 name() const;
3735 // Return the type to which this points. Give an error if the type
3736 // has not yet been defined.
3737 Type*
3738 real_type();
3740 const Type*
3741 real_type() const;
3743 // Whether the base type has been defined.
3744 bool
3745 is_defined() const;
3747 // Add a method to this type.
3748 Named_object*
3749 add_method(const std::string& name, Function*);
3751 // Add a method declaration to this type.
3752 Named_object*
3753 add_method_declaration(const std::string& name, Package*, Function_type*,
3754 Location);
3756 // Add an already created object as a method to this type.
3757 void
3758 add_existing_method(Named_object*);
3760 protected:
3762 do_traverse(Traverse* traverse);
3764 bool
3765 do_verify();
3767 bool
3768 do_has_pointer() const
3769 { return this->real_type()->has_pointer(); }
3771 bool
3772 do_compare_is_identity(Gogo* gogo)
3773 { return this->real_type()->compare_is_identity(gogo); }
3775 bool
3776 do_is_reflexive()
3777 { return this->real_type()->is_reflexive(); }
3779 bool
3780 do_needs_key_update()
3781 { return this->real_type()->needs_key_update(); }
3783 bool
3784 do_in_heap() const
3785 { return this->real_type()->in_heap(); }
3787 unsigned int
3788 do_hash_for_method(Gogo* gogo, int flags) const
3789 { return this->real_type()->hash_for_method(gogo, flags); }
3791 Btype*
3792 do_get_backend(Gogo* gogo);
3794 Expression*
3795 do_type_descriptor(Gogo*, Named_type*);
3797 void
3798 do_reflection(Gogo*, std::string*) const;
3800 void
3801 do_mangled_name(Gogo*, std::string*, bool*) const;
3803 void
3804 do_export(Export*) const;
3806 private:
3807 // Issue a warning about a use of an undefined type.
3808 void
3809 warn() const;
3811 // The type declaration.
3812 Named_object* named_object_;
3813 // Whether we have issued a warning about this type.
3814 mutable bool warned_;
3817 // The Type_context struct describes what we expect for the type of an
3818 // expression.
3820 struct Type_context
3822 // The exact type we expect, if known. This may be NULL.
3823 Type* type;
3824 // Whether an abstract type is permitted.
3825 bool may_be_abstract;
3827 // Constructors.
3828 Type_context()
3829 : type(NULL), may_be_abstract(false)
3832 Type_context(Type* a_type, bool a_may_be_abstract)
3833 : type(a_type), may_be_abstract(a_may_be_abstract)
3837 #endif // !defined(GO_TYPES_H)