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.
12 #include "go-linemap.h"
19 class Typed_identifier
;
20 class Typed_identifier_list
;
26 class Backend_function_type
;
28 class Struct_field_list
;
36 class Forward_declaration_type
;
39 class Type_hash_identical
;
42 class Expression_list
;
43 class Call_expression
;
44 class Field_reference_expression
;
45 class Bound_method_expression
;
49 class Translate_context
;
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
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.
115 const Field_indexes
* next
;
116 unsigned int field_index
;
122 // Get the list of field indexes.
124 field_indexes() const
125 { return this->field_indexes_
; }
130 { return this->depth_
; }
132 // Return whether this is a value method--a method which does not
133 // require a pointer expression.
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.
141 needs_stub_method() const
142 { return this->needs_stub_method_
; }
144 // Return whether this is an ambiguous method name.
147 { return this->is_ambiguous_
; }
149 // Note that this method is ambiguous.
152 { this->is_ambiguous_
= true; }
154 // Return the type of the method.
157 { return this->do_type(); }
159 // Return the location of the method receiver.
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.
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.
172 named_object() const;
174 // Get the stub object.
178 go_assert(this->stub_
!= NULL
);
182 // Set the stub object.
184 set_stub_object(Named_object
* no
)
186 go_assert(this->stub_
== NULL
);
190 // Get the direct interface method stub 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.
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
210 { return this->do_nointerface(); }
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
),
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
*
229 // Return the location of the method receiver.
231 do_receiver_location() const = 0;
233 // Bind a method to an object.
235 do_bind_method(Expression
* expr
, Location location
) const = 0;
237 // Return whether this method should not participate in interfaces.
239 do_nointerface() const = 0;
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.
247 // If a stub method is required, this is its object. This is only
248 // set after stub methods are built in finalize_methods.
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
255 bool is_value_method_
;
256 // Whether a stub method is required.
257 bool needs_stub_method_
;
258 // Whether this method 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
266 class Named_method
: public Method
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
)
277 // Get the Named_object for the method.
279 do_named_object() const
280 { return this->named_object_
; }
282 // The type of the method.
286 // Return the location of the method receiver.
288 do_receiver_location() const;
290 // Bind a method to an object.
292 do_bind_method(Expression
* expr
, Location location
) const;
294 // Return whether this method should not participate in interfaces.
296 do_nointerface() const;
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
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
311 Interface_method(const std::string
& name
, Location location
,
312 Function_type
* fntype
, const Field_indexes
* field_indexes
,
314 : Method(field_indexes
, depth
, true, true),
315 name_(name
), location_(location
), fntype_(fntype
)
319 // Get the Named_object for the method. This should never be
320 // called, as we always create a stub.
322 do_named_object() const
323 { go_unreachable(); }
325 // The type of the method.
328 { return this->fntype_
; }
330 // Return the location of the method receiver.
332 do_receiver_location() const
333 { return this->location_
; }
335 // Bind a method to an object.
337 do_bind_method(Expression
* expr
, Location location
) const;
339 // Return whether this method should not participate in interfaces.
341 do_nointerface() const
345 // The name of the interface method to call.
347 // The location of the definition of the interface method.
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
359 typedef Unordered_map(std::string
, Method
*) Method_map
;
362 typedef Method_map::const_iterator const_iterator
;
368 // Insert a new method. Returns true if it was inserted, false if
369 // it was overidden or ambiguous.
371 insert(const std::string
& name
, Method
* m
);
373 // The number of (unambiguous) methods.
380 { return this->methods_
.begin(); }
384 { return this->methods_
.end(); }
388 find(const std::string
& name
) const
389 { return this->methods_
.find(name
); }
393 { return this->methods_
.empty(); }
399 // The base class for all types.
404 // The types of types.
405 enum Type_classification
418 TYPE_CALL_MULTIPLE_RESULT
,
438 // Get the unnamed bool type.
442 // Get the named type "bool".
446 // Make the named type "bool".
448 make_named_bool_type();
450 // Make an abstract integer type.
452 make_abstract_integer_type();
454 // Make an abstract type for a character constant.
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.
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.
467 make_integer_type_alias(const char* name
, Named_type
* real_type
);
469 // Look up a named integer type.
471 lookup_integer_type(const char* name
);
473 // Make an abstract floating point 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.
481 make_float_type(const char* name
, int bits
, int runtime_type_kind
);
483 // Look up a named float type.
485 lookup_float_type(const char* name
);
487 // Make an abstract 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.
495 make_complex_type(const char* name
, int bits
, int runtime_type_kind
);
497 // Look up a named complex type.
499 lookup_complex_type(const char* name
);
501 // Get the unnamed string type.
505 // Get the named type "string".
507 lookup_string_type();
509 // Make the named type "string".
511 make_named_string_type();
516 static Function_type
*
517 make_function_type(Typed_identifier
* receiver
,
518 Typed_identifier_list
* parameters
,
519 Typed_identifier_list
* results
,
522 static Backend_function_type
*
523 make_backend_function_type(Typed_identifier
* receiver
,
524 Typed_identifier_list
* parameters
,
525 Typed_identifier_list
* results
,
529 make_pointer_type(Type
*);
532 finish_pointer_types(Gogo
* gogo
);
538 make_call_multiple_result_type();
541 make_struct_type(Struct_field_list
* fields
, Location
);
544 make_array_type(Type
* element_type
, Expression
* length
);
547 make_map_type(Type
* key_type
, Type
* value_type
, Location
);
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
);
559 make_type_descriptor_type();
562 make_type_descriptor_ptr_type();
565 make_named_type(Named_object
*, Type
*, Location
);
568 make_forward_declaration(Named_object
*);
570 // Make a builtin struct type from a list of fields.
572 make_builtin_struct_type(int nfields
, ...);
574 // Make a builtin named type.
576 make_builtin_named_type(const char* name
, Type
* type
);
578 // Return a string version of this type to use in an error message.
580 message_name() const;
584 traverse(Type
*, Traverse
*);
586 // Verify the type. This is called after parsing, and verifies that
587 // types are complete and meet the language requirements. This
588 // returns false if the type is invalid and we should not continue
592 { return this->do_verify(gogo
); }
594 // Bit flags to pass to are_identical and friends.
596 // Treat error types as their own distinct type. Sometimes we
597 // ignore error types--treat them as identical to every other
598 // type--to avoid cascading errors.
599 static const int COMPARE_ERRORS
= 1;
601 // Compare struct field tags when comparing structs. We ignore
602 // struct field tags for purposes of type conversion.
603 static const int COMPARE_TAGS
= 2;
605 // Compare aliases: treat an alias to T as distinct from T.
606 static const int COMPARE_ALIASES
= 4;
608 // When comparing interface types compare the interface embedding heirarchy,
609 // if any, rather than only comparing method sets. Useful primarily when
611 static const int COMPARE_EMBEDDED_INTERFACES
= 8;
613 // Return true if two types are identical. If this returns false,
614 // and REASON is not NULL, it may set *REASON.
616 are_identical(const Type
* lhs
, const Type
* rhs
, int flags
,
617 std::string
* reason
);
619 // Return true if two types are compatible for use in a binary
620 // operation, other than a shift, comparison, or channel send. This
621 // is an equivalence relation.
623 are_compatible_for_binop(const Type
* t1
, const Type
* t2
);
625 // Return true if two types are compatible for use with the
626 // comparison operator. IS_EQUALITY_OP is true if this is an
627 // equality comparison, false if it is an ordered comparison. This
628 // is an equivalence relation. If this returns false, and REASON is
629 // not NULL, it sets *REASON.
631 are_compatible_for_comparison(bool is_equality_op
, const Type
*t1
,
632 const Type
*t2
, std::string
* reason
);
634 // Return true if a type is comparable with itself. This is true of
635 // most types, but false for, e.g., function types.
637 is_comparable() const
638 { return Type::are_compatible_for_comparison(true, this, this, NULL
); }
640 // Return true if a value with type RHS is assignable to a variable
641 // with type LHS. This is not an equivalence relation. If this
642 // returns false, and REASON is not NULL, it sets *REASON.
644 are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
);
646 // Return true if a value with type RHS may be converted to type
647 // LHS. If this returns false, and REASON is not NULL, it sets
650 are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
);
652 // Return true if values of this type can be compared using an
653 // identity function which gets nothing but a pointer to the value
656 compare_is_identity(Gogo
* gogo
)
657 { return this->do_compare_is_identity(gogo
); }
659 // Return whether values of this type are reflexive: if a comparison
660 // of a value with itself always returns true.
663 { return this->do_is_reflexive(); }
665 // Return whether values of this, when used as a key in map,
666 // requires the key to be updated when an assignment is made.
669 { return this->do_needs_key_update(); }
671 // Return whether the hash function of this type might panic. This
672 // is only called for types used as a key in a map type.
675 { return this->do_hash_might_panic(); }
677 // Whether the type is permitted in the heap.
680 { return this->do_in_heap(); }
682 // Return a hash code for this type for the method hash table.
683 // Types which are equivalent according to are_identical will have
684 // the same hash code.
686 hash_for_method(Gogo
*, int) const;
688 // Return the type classification.
690 classification() const
691 { return this->classification_
; }
693 // Return the base type for this type. This looks through forward
694 // declarations and names. Using this with a forward declaration
695 // which has not been defined will return an error type.
702 // Return the type skipping defined forward declarations. If this
703 // type is a forward declaration which has not been defined, it will
704 // return the Forward_declaration_type. This differs from base() in
705 // that it will return a Named_type, and for a
706 // Forward_declaration_type which is not defined it will return that
707 // type rather than an error type.
714 // Return the type skipping any alias definitions and any defined
715 // forward declarations. This is like forwarded, but also
716 // recursively expands alias definitions to the aliased type.
723 // Return true if this is a basic type: a type which is not composed
724 // of other types, and is not void.
726 is_basic_type() const;
728 // Return true if this is an abstract type--an integer, floating
729 // point, or complex type whose size has not been determined.
733 // Return a non-abstract version of an abstract type.
735 make_non_abstract_type();
737 // Return true if this type is or contains a pointer. This
738 // determines whether the garbage collector needs to look at a value
742 { return this->do_has_pointer(); }
744 // Return true if this is the error type. This returns false for a
745 // type which is not defined, as it is called by the parser before
746 // all types are defined.
748 is_error_type() const;
750 // Return true if this is the error type or if the type is
751 // undefined. If the type is undefined, this will give an error.
752 // This should only be called after parsing is complete.
755 { return this->base()->is_error_type(); }
757 // Return true if this is a void type.
760 { return this->classification_
== TYPE_VOID
; }
762 // If this is an integer type, return the Integer_type. Otherwise,
763 // return NULL. This is a controlled dynamic_cast.
766 { return this->convert
<Integer_type
, TYPE_INTEGER
>(); }
770 { return this->convert
<const Integer_type
, TYPE_INTEGER
>(); }
772 // If this is a floating point type, return the Float_type.
773 // Otherwise, return NULL. This is a controlled dynamic_cast.
776 { return this->convert
<Float_type
, TYPE_FLOAT
>(); }
780 { return this->convert
<const Float_type
, TYPE_FLOAT
>(); }
782 // If this is a complex type, return the Complex_type. Otherwise,
786 { return this->convert
<Complex_type
, TYPE_COMPLEX
>(); }
790 { return this->convert
<const Complex_type
, TYPE_COMPLEX
>(); }
792 // Return whether this is a numeric type.
794 is_numeric_type() const
796 Type_classification tc
= this->base()->classification_
;
797 return tc
== TYPE_INTEGER
|| tc
== TYPE_FLOAT
|| tc
== TYPE_COMPLEX
;
800 // Return true if this is a boolean type.
802 is_boolean_type() const
803 { return this->base()->classification_
== TYPE_BOOLEAN
; }
805 // Return true if this is an abstract boolean type.
807 is_abstract_boolean_type() const
808 { return this->classification_
== TYPE_BOOLEAN
; }
810 // Return true if this is a string type.
812 is_string_type() const
813 { return this->base()->classification_
== TYPE_STRING
; }
815 // Return true if this is an abstract string type.
817 is_abstract_string_type() const
818 { return this->classification_
== TYPE_STRING
; }
820 // Return true if this is the sink type. This is the type of the
821 // blank identifier _.
824 { return this->base()->classification_
== TYPE_SINK
; }
826 // If this is a function type, return it. Otherwise, return NULL.
829 { return this->convert
<Function_type
, TYPE_FUNCTION
>(); }
832 function_type() const
833 { return this->convert
<const Function_type
, TYPE_FUNCTION
>(); }
835 // If this is a pointer type, return the type to which it points.
836 // Otherwise, return NULL.
840 // If this is a pointer type, return the type to which it points.
841 // Otherwise, return the type itself.
845 Type
* pt
= this->points_to();
846 return pt
!= NULL
? pt
: this;
852 const Type
* pt
= this->points_to();
853 return pt
!= NULL
? pt
: this;
856 // Return true if this is the nil type. We don't use base() here,
857 // because this can be called during parse, and there is no way to
858 // name the nil type anyhow.
861 { return this->classification_
== TYPE_NIL
; }
863 // Return true if this is the predeclared constant nil being used as
864 // a type. This is what the parser produces for type switches which
867 is_nil_constant_as_type() const;
869 // Return true if this is the return type of a function which
870 // returns multiple values.
872 is_call_multiple_result_type() const
873 { return this->base()->classification_
== TYPE_CALL_MULTIPLE_RESULT
; }
875 // If this is a struct type, return it. Otherwise, return NULL.
878 { return this->convert
<Struct_type
, TYPE_STRUCT
>(); }
882 { return this->convert
<const Struct_type
, TYPE_STRUCT
>(); }
884 // If this is an array type, return it. Otherwise, return NULL.
887 { return this->convert
<Array_type
, TYPE_ARRAY
>(); }
891 { return this->convert
<const Array_type
, TYPE_ARRAY
>(); }
893 // Return whether if this is a slice type.
895 is_slice_type() const;
897 // If this is a map type, return it. Otherwise, return NULL.
900 { return this->convert
<Map_type
, TYPE_MAP
>(); }
904 { return this->convert
<const Map_type
, TYPE_MAP
>(); }
906 // If this is a channel type, return it. Otherwise, return NULL.
909 { return this->convert
<Channel_type
, TYPE_CHANNEL
>(); }
913 { return this->convert
<const Channel_type
, TYPE_CHANNEL
>(); }
915 // If this is an interface type, return it. Otherwise, return NULL.
918 { return this->convert
<Interface_type
, TYPE_INTERFACE
>(); }
920 const Interface_type
*
921 interface_type() const
922 { return this->convert
<const Interface_type
, TYPE_INTERFACE
>(); }
924 // If this is a named type, return it. Otherwise, return NULL.
931 // If this is a forward declaration, return it. Otherwise, return
933 Forward_declaration_type
*
934 forward_declaration_type()
935 { return this->convert_no_base
<Forward_declaration_type
, TYPE_FORWARD
>(); }
937 const Forward_declaration_type
*
938 forward_declaration_type() const
940 return this->convert_no_base
<const Forward_declaration_type
,
944 // Return true if this type is not yet defined.
946 is_undefined() const;
948 // Return true if this is the unsafe.pointer type. We currently
949 // represent that as pointer-to-void.
951 is_unsafe_pointer_type() const
952 { return this->points_to() != NULL
&& this->points_to()->is_void_type(); }
954 // Return whether this type is stored directly in an interface's
957 is_direct_iface_type() const;
959 // Return a version of this type with any expressions copied, but
960 // only if copying the expressions will affect the size of the type.
961 // If there are no such expressions in the type (expressions can
962 // only occur in array types), just return the same type. If any
963 // expressions can not affect the size of the type, just return the
968 // Look for field or method NAME for TYPE. Return an expression for
969 // it, bound to EXPR.
971 bind_field_or_method(Gogo
*, const Type
* type
, Expression
* expr
,
972 const std::string
& name
, Location
);
974 // Return true if NAME is an unexported field or method of TYPE.
976 is_unexported_field_or_method(Gogo
*, const Type
*, const std::string
&,
977 std::vector
<const Named_type
*>*);
979 // Convert the builtin named types.
981 convert_builtin_named_types(Gogo
*);
983 // Return the backend representation of this type.
987 // Return a placeholder for the backend representation of the type.
988 // This will return a type of the correct size, but for which some
989 // of the fields may still need to be completed.
991 get_backend_placeholder(Gogo
*);
993 // Finish the backend representation of a placeholder.
995 finish_backend(Gogo
*, Btype
*);
997 // Build a type descriptor entry for this type. Return a pointer to
998 // it. The location is the location which causes us to need the
1001 type_descriptor_pointer(Gogo
* gogo
, Location
);
1003 // Build the Garbage Collection symbol for this type. Return a pointer to it.
1005 gc_symbol_pointer(Gogo
* gogo
);
1007 // Return whether this type needs a garbage collection program.
1008 // Sets *PTRSIZE and *PTRDATA.
1010 needs_gcprog(Gogo
*, int64_t* ptrsize
, int64_t* ptrdata
);
1012 // Return a ptrmask variable for this type.
1014 gc_ptrmask_var(Gogo
*, int64_t ptrsize
, int64_t ptrdata
);
1016 // Return the type reflection string for this type.
1018 reflection(Gogo
*) const;
1020 // Add the backend name for the type to BNAME. This will add one or
1021 // two name components. Identical types should have the same
1024 backend_name(Gogo
*, Backend_name
* bname
) const;
1026 // If the size of the type can be determined, set *PSIZE to the size
1027 // in bytes and return true. Otherwise, return false. This queries
1030 backend_type_size(Gogo
*, int64_t* psize
);
1032 // If the alignment of the type can be determined, set *PALIGN to
1033 // the alignment in bytes and return true. Otherwise, return false.
1035 backend_type_align(Gogo
*, int64_t* palign
);
1037 // If the alignment of a struct field of this type can be
1038 // determined, set *PALIGN to the alignment in bytes and return
1039 // true. Otherwise, return false.
1041 backend_type_field_align(Gogo
*, int64_t* palign
);
1043 // Determine the ptrdata size for the backend version of this type:
1044 // the length of the prefix of the type that can contain a pointer
1045 // value. If it can be determined, set *PPTRDATA to the value in
1046 // bytes and return true. Otherwise, return false.
1048 backend_type_ptrdata(Gogo
*, int64_t* pptrdata
);
1050 // Determine the ptrdata size that we are going to set in the type
1051 // descriptor. This is normally the same as backend_type_ptrdata,
1052 // but differs if we use a gcprog for an array. The arguments and
1053 // results are as for backend_type_ptrdata.
1055 descriptor_ptrdata(Gogo
*, int64_t* pptrdata
);
1057 // Whether the backend size is known.
1059 is_backend_type_size_known(Gogo
*);
1061 // Return whether the type needs specially built type functions.
1063 needs_specific_type_functions(Gogo
*);
1065 // Get the equality function for a type. Returns NULL if the type
1066 // is not comparable.
1068 equal_function(Gogo
*, Named_type
* name
, Function_type
* equal_fntype
);
1070 // Get the hash function for a type. Returns NULL if the type is
1073 hash_function(Gogo
*, Function_type
* hash_fntype
);
1075 // Write the equal function for a type.
1077 write_equal_function(Gogo
*, Named_type
*, int64_t size
,
1078 const Backend_name
*, Function_type
* equal_fntype
);
1080 // Write the hash function for a type.
1082 write_hash_function(Gogo
*, int64_t size
, const Backend_name
*,
1083 Function_type
* hash_fntype
);
1085 // Return the alignment required by the memequalN function.
1086 static int64_t memequal_align(Gogo
*, int size
);
1090 export_type(Export
* exp
) const
1091 { this->do_export(exp
); }
1095 import_type(Import
*);
1098 Type(Type_classification
);
1100 // Functions implemented by the child class.
1104 do_message_name(std::string
*) const = 0;
1106 // Traverse the subtypes.
1108 do_traverse(Traverse
*);
1116 do_has_pointer() const
1120 do_compare_is_identity(Gogo
*) = 0;
1127 do_needs_key_update()
1131 do_hash_might_panic()
1138 virtual unsigned int
1139 do_hash_for_method(Gogo
*, int) const;
1142 do_get_backend(Gogo
*) = 0;
1145 do_type_descriptor(Gogo
*, Named_type
* name
) = 0;
1148 do_reflection(Gogo
*, std::string
*) const = 0;
1151 do_mangled_name(Gogo
*, std::string
*, bool*) const = 0;
1154 do_export(Export
*) const;
1156 // For children to call when they detect that they are in error.
1160 // Return whether a method expects a pointer as the receiver.
1162 method_expects_pointer(const Named_object
*);
1164 // Finalize the methods for a type.
1166 finalize_methods(Gogo
*, const Type
*, Location
, Methods
**);
1168 // Return a method from a set of methods.
1170 method_function(const Methods
*, const std::string
& name
,
1171 bool* is_ambiguous
);
1173 // A mapping from interfaces to the associated interface method
1174 // tables for this type. This maps to a decl.
1175 typedef Unordered_map_hash(Interface_type
*, Expression
*, Type_hash_identical
,
1176 Type_identical
) Interface_method_tables
;
1178 // Return a pointer to the interface method table for TYPE for the
1179 // interface INTERFACE.
1181 interface_method_table(Type
* type
,
1182 Interface_type
*interface
, bool is_pointer
,
1183 Interface_method_tables
** method_tables
,
1184 Interface_method_tables
** pointer_tables
);
1186 // Return a composite literal for the type descriptor entry for a
1189 type_descriptor(Gogo
*, Type
*);
1191 // Return a composite literal for the type descriptor entry for
1192 // TYPE, using NAME as the name of the type.
1194 named_type_descriptor(Gogo
*, Type
* type
, Named_type
* name
);
1196 // Return a composite literal for a plain type descriptor for this
1197 // type with the given kind and name.
1199 plain_type_descriptor(Gogo
*, int runtime_type_kind
, Named_type
* name
);
1201 // Build a composite literal for the basic type descriptor.
1203 type_descriptor_constructor(Gogo
*, int runtime_type_kind
, Named_type
*,
1204 const Methods
*, bool only_value_methods
);
1206 // For the benefit of child class message name construction.
1208 append_message_name(const Type
* type
, std::string
* ret
) const
1209 { type
->do_message_name(ret
); }
1211 // For the benefit of child class reflection string generation.
1213 append_reflection(const Type
* type
, Gogo
* gogo
, std::string
* ret
) const
1214 { type
->do_reflection(gogo
, ret
); }
1216 // For the benefit of child class mangling.
1218 append_mangled_name(const Type
* type
, Gogo
* gogo
, std::string
* ret
,
1219 bool *is_non_identifier
) const
1220 { type
->do_mangled_name(gogo
, ret
, is_non_identifier
); }
1222 // Return the backend representation for the underlying type of a
1225 get_named_base_btype(Gogo
* gogo
, Type
* base_type
)
1226 { return base_type
->get_btype_without_hash(gogo
); }
1229 // Convert to the desired type classification, or return NULL. This
1230 // is a controlled dynamic_cast.
1231 template<typename Type_class
, Type_classification type_classification
>
1235 Type
* base
= this->base();
1236 return (base
->classification_
== type_classification
1237 ? static_cast<Type_class
*>(base
)
1241 template<typename Type_class
, Type_classification type_classification
>
1245 const Type
* base
= this->base();
1246 return (base
->classification_
== type_classification
1247 ? static_cast<Type_class
*>(base
)
1251 template<typename Type_class
, Type_classification type_classification
>
1255 return (this->classification_
== type_classification
1256 ? static_cast<Type_class
*>(this)
1260 template<typename Type_class
, Type_classification type_classification
>
1262 convert_no_base() const
1264 return (this->classification_
== type_classification
1265 ? static_cast<Type_class
*>(this)
1269 // Map unnamed types to type descriptor decls.
1270 typedef Unordered_map_hash(const Type
*, Bvariable
*, Type_hash_identical
,
1271 Type_identical
) Type_descriptor_vars
;
1273 static Type_descriptor_vars type_descriptor_vars
;
1275 // Build the type descriptor variable for this type.
1277 make_type_descriptor_var(Gogo
*);
1279 // Map unnamed types to type descriptor decls.
1280 typedef Unordered_map_hash(const Type
*, Bvariable
*, Type_hash_identical
,
1281 Type_identical
) GC_symbol_vars
;
1283 static GC_symbol_vars gc_symbol_vars
;
1285 // Map ptrmask symbol names to the ptrmask variable.
1286 typedef Unordered_map(std::string
, Bvariable
*) GC_gcbits_vars
;
1288 static GC_gcbits_vars gc_gcbits_vars
;
1290 // Build the GC symbol for this type.
1292 make_gc_symbol_var(Gogo
*);
1294 // Return true if the type descriptor for this type should be
1295 // defined in some other package. If NAME is not NULL, it is the
1296 // name of this type. If this returns true it sets *PACKAGE to the
1297 // package where the type descriptor is defined.
1299 type_descriptor_defined_elsewhere(Named_type
* name
, const Package
** package
);
1301 // Make a composite literal for the garbage collection program for
1304 gcprog_constructor(Gogo
*, int64_t ptrsize
, int64_t ptrdata
);
1306 // Build the hash function for a type that needs specific functions.
1308 build_hash_function(Gogo
*, int64_t size
, Function_type
* hash_fntype
);
1310 // Build the equal function for a type that needs specific functions.
1312 build_equal_function(Gogo
*, Named_type
*, int64_t size
,
1313 Function_type
* equal_fntype
);
1316 write_identity_hash(Gogo
*, Named_object
* function
, int64_t size
);
1319 write_identity_equal(Gogo
*, Named_object
* function
, int64_t size
);
1322 write_named_equal(Gogo
*, Named_object
* function
, Named_type
*);
1324 // Build a composite literal for the uncommon type information.
1326 uncommon_type_constructor(Gogo
*, Type
* uncommon_type
,
1327 Named_type
*, const Methods
*,
1328 bool only_value_methods
) const;
1330 // Build a composite literal for the methods.
1332 methods_constructor(Gogo
*, Type
* methods_type
, const Methods
*,
1333 bool only_value_methods
) const;
1335 // Build a composite literal for one method.
1337 method_constructor(Gogo
*, Type
* method_type
, const std::string
& name
,
1338 const Method
*, bool only_value_methods
) const;
1340 // Add all methods for TYPE to the list of methods for THIS.
1342 add_methods_for_type(const Type
* type
, const Method::Field_indexes
*,
1343 unsigned int depth
, bool, bool,
1344 std::vector
<const Named_type
*>*,
1348 add_local_methods_for_type(const Named_type
* type
,
1349 const Method::Field_indexes
*,
1350 unsigned int depth
, bool, bool, Methods
*);
1353 add_embedded_methods_for_type(const Type
* type
,
1354 const Method::Field_indexes
*,
1355 unsigned int depth
, bool, bool,
1356 std::vector
<const Named_type
*>*,
1360 add_interface_methods_for_type(const Type
* type
,
1361 const Method::Field_indexes
*,
1362 unsigned int depth
, Methods
*);
1364 // Build stub methods for a type.
1366 build_stub_methods(Gogo
*, const Type
* type
, const Methods
* methods
,
1370 build_one_stub_method(Gogo
*, Method
*, Named_object
* stub
,
1371 const char* receiver_name
, const Type
* receiver_type
,
1372 const Typed_identifier_list
*, bool is_varargs
,
1373 const Typed_identifier_list
*, Location
);
1375 // Build direct interface stub methods for a type.
1377 build_direct_iface_stub_methods(Gogo
*, const Type
*, Methods
*, Location
);
1380 build_one_iface_stub_method(Gogo
*, Method
*, Named_object
* stub
, const char*,
1381 const Typed_identifier_list
*, bool,
1382 const Typed_identifier_list
*, Location
);
1385 add_return_from_results(Gogo
*, Named_object
* stub
, Call_expression
*,
1386 const Typed_identifier_list
*, Location
);
1389 apply_field_indexes(Expression
*, const Method::Field_indexes
*,
1390 Location
, const Type
**);
1392 // Look for a field or method named NAME in TYPE.
1394 find_field_or_method(const Type
* type
, const std::string
& name
,
1395 bool receiver_can_be_pointer
,
1396 std::vector
<const Named_type
*>*, int* level
,
1397 bool* is_method
, bool* found_pointer_method
,
1398 std::string
* ambig1
, std::string
* ambig2
);
1400 // Helper function for is_direct_iface_type, to prevent infinite
1403 is_direct_iface_type_helper(Unordered_set(const Type
*)*) const;
1405 // Get the backend representation for a type without looking in the
1406 // hash table for identical types.
1408 get_btype_without_hash(Gogo
*);
1410 // A backend type that may be a placeholder.
1411 struct Type_btype_entry
1414 bool is_placeholder
;
1417 // A mapping from Type to Btype*, used to ensure that the backend
1418 // representation of identical types is identical. This is only
1419 // used for unnamed types.
1420 typedef Unordered_map_hash(const Type
*, Type_btype_entry
,
1421 Type_hash_identical
, Type_identical
) Type_btypes
;
1423 static Type_btypes type_btypes
;
1425 // A list of builtin named types.
1426 static std::vector
<Named_type
*> named_builtin_types
;
1428 // A map from types that need a specific hash or equality function
1429 // to the hash or equality function.
1430 typedef Unordered_map_hash(const Type
*, Named_object
*, Type_hash_identical
,
1431 Type_identical
) Type_function
;
1433 static Type_function type_hash_functions_table
;
1434 static Type_function type_equal_functions_table
;
1436 // Cache for reusing existing pointer types; maps from pointed-to-type
1438 typedef Unordered_map(Type
*, Pointer_type
*) Pointer_type_table
;
1440 static Pointer_type_table pointer_types
;
1442 // List of placeholder pointer types.
1443 static std::vector
<Type
*> placeholder_pointers
;
1445 // The type classification.
1446 Type_classification classification_
;
1447 // The backend representation of the type, once it has been
1450 // The type descriptor for this type. This starts out as NULL and
1451 // is filled in as needed.
1452 Bvariable
* type_descriptor_var_
;
1453 // The GC symbol for this type. This starts out as NULL and
1454 // is filled in as needed.
1455 Bvariable
* gc_symbol_var_
;
1458 // Type hash table operations, treating aliases as identical to the
1459 // types that they alias.
1461 class Type_hash_identical
1465 operator()(const Type
* type
) const
1467 return type
->hash_for_method(NULL
,
1468 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
);
1472 class Type_identical
1476 operator()(const Type
* t1
, const Type
* t2
) const
1478 return Type::are_identical(t1
, t2
,
1479 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
1484 // An identifier with a type.
1486 class Typed_identifier
1489 Typed_identifier(const std::string
& name
, Type
* type
,
1491 : name_(name
), type_(type
), location_(location
), note_(NULL
)
1497 { return this->name_
; }
1502 { return this->type_
; }
1504 // Return the location where the name was seen. This is not always
1508 { return this->location_
; }
1510 // Set the type--sometimes we see the identifier before the type.
1512 set_type(Type
* type
)
1514 go_assert(this->type_
== NULL
|| type
->is_error_type());
1518 // Get the escape note.
1521 { return this->note_
; }
1523 // Set the escape note.
1525 set_note(const std::string
& note
)
1527 if (this->note_
!= NULL
)
1528 go_assert(*this->note_
== note
);
1530 this->note_
= new std::string(note
);
1538 // The location where the name was seen.
1540 // Escape note for this typed identifier. Used when importing and exporting
1545 // A list of Typed_identifiers.
1547 class Typed_identifier_list
1550 Typed_identifier_list()
1554 // Whether the list is empty.
1557 { return this->entries_
.empty(); }
1559 // Return the number of entries in the list.
1562 { return this->entries_
.size(); }
1564 // Add an entry to the end of the list.
1566 push_back(const Typed_identifier
& td
)
1567 { this->entries_
.push_back(td
); }
1569 // Remove an entry from the end of the list.
1572 { this->entries_
.pop_back(); }
1574 // Set the type of entry I to TYPE.
1576 set_type(size_t i
, Type
* type
)
1578 go_assert(i
< this->entries_
.size());
1579 this->entries_
[i
].set_type(type
);
1582 // Sort the entries by name.
1588 traverse(Traverse
*) const;
1590 // Return the first and last elements.
1593 { return this->entries_
.front(); }
1595 const Typed_identifier
&
1597 { return this->entries_
.front(); }
1601 { return this->entries_
.back(); }
1603 const Typed_identifier
&
1605 { return this->entries_
.back(); }
1609 { return this->entries_
.at(i
); }
1611 const Typed_identifier
&
1613 { return this->entries_
.at(i
); }
1616 set(size_t i
, const Typed_identifier
& t
)
1617 { this->entries_
.at(i
) = t
; }
1622 go_assert(c
<= this->entries_
.size());
1623 this->entries_
.resize(c
, Typed_identifier("", NULL
,
1624 Linemap::unknown_location()));
1629 { this->entries_
.reserve(c
); }
1633 typedef std::vector
<Typed_identifier
>::iterator iterator
;
1634 typedef std::vector
<Typed_identifier
>::const_iterator const_iterator
;
1638 { return this->entries_
.begin(); }
1642 { return this->entries_
.begin(); }
1646 { return this->entries_
.end(); }
1650 { return this->entries_
.end(); }
1652 // Return a copy of this list. This returns an independent copy of
1653 // the vector, but does not copy the types.
1654 Typed_identifier_list
*
1658 std::vector
<Typed_identifier
> entries_
;
1661 // A type used to indicate a parsing error. This exists to simplify
1662 // later error detection.
1664 class Error_type
: public Type
1673 do_message_name(std::string
* ret
) const
1674 { ret
->append("<ERROR>"); }
1677 do_compare_is_identity(Gogo
*)
1681 do_get_backend(Gogo
* gogo
);
1684 do_type_descriptor(Gogo
*, Named_type
*);
1687 do_reflection(Gogo
*, std::string
*) const;
1690 do_mangled_name(Gogo
*, std::string
*, bool*) const;
1695 class Void_type
: public Type
1704 do_message_name(std::string
* ret
) const
1705 { ret
->append("void"); }
1708 do_compare_is_identity(Gogo
*)
1712 do_get_backend(Gogo
* gogo
);
1715 do_type_descriptor(Gogo
*, Named_type
*)
1716 { go_unreachable(); }
1719 do_reflection(Gogo
*, std::string
*) const
1723 do_mangled_name(Gogo
*, std::string
*, bool*) const;
1726 // The boolean type.
1728 class Boolean_type
: public Type
1732 : Type(TYPE_BOOLEAN
)
1737 do_message_name(std::string
* ret
) const
1738 { ret
->append("<untyped bool>"); }
1741 do_compare_is_identity(Gogo
*)
1745 do_get_backend(Gogo
* gogo
);
1748 do_type_descriptor(Gogo
*, Named_type
* name
);
1750 // We should not be asked for the reflection string of a basic type.
1752 do_reflection(Gogo
*, std::string
* ret
) const
1753 { ret
->append("bool"); }
1756 do_mangled_name(Gogo
*, std::string
*, bool*) const;
1759 // The type of an integer.
1761 class Integer_type
: public Type
1764 // Create a new integer type.
1766 create_integer_type(const char* name
, bool is_unsigned
, int bits
,
1767 int runtime_type_kind
);
1769 // Look up an existing integer type.
1771 lookup_integer_type(const char* name
);
1773 // Create an abstract integer type.
1774 static Integer_type
*
1775 create_abstract_integer_type();
1777 // Create an abstract character type.
1778 static Integer_type
*
1779 create_abstract_character_type();
1781 // Create an alias to an integer type.
1783 create_integer_type_alias(const char* name
, Named_type
* real_type
);
1785 // Whether this is an abstract integer type.
1788 { return this->is_abstract_
; }
1790 // Whether this is an unsigned type.
1793 { return this->is_unsigned_
; }
1795 // The number of bits.
1798 { return this->bits_
; }
1800 // Whether this type is the same as T.
1802 is_identical(const Integer_type
* t
) const;
1804 // Whether this is the type "byte" or another name for "byte".
1807 { return this->is_byte_
; }
1809 // Mark this as the "byte" type.
1812 { this->is_byte_
= true; }
1814 // Whether this is the type "rune" or another name for "rune".
1817 { return this->is_rune_
; }
1819 // Mark this as the "rune" type.
1822 { this->is_rune_
= true; }
1826 do_message_name(std::string
* ret
) const;
1829 do_compare_is_identity(Gogo
*)
1833 do_hash_for_method(Gogo
*, int) const;
1836 do_get_backend(Gogo
*);
1839 do_type_descriptor(Gogo
*, Named_type
*);
1842 do_reflection(Gogo
*, std::string
*) const;
1845 do_mangled_name(Gogo
*, std::string
*, bool*) const;
1848 Integer_type(bool is_abstract
, bool is_unsigned
, int bits
,
1849 int runtime_type_kind
)
1850 : Type(TYPE_INTEGER
),
1851 is_abstract_(is_abstract
), is_unsigned_(is_unsigned
), is_byte_(false),
1852 is_rune_(false), bits_(bits
), runtime_type_kind_(runtime_type_kind
)
1855 // Map names of integer types to the types themselves.
1856 typedef std::map
<std::string
, Named_type
*> Named_integer_types
;
1857 static Named_integer_types named_integer_types
;
1859 // True if this is an abstract type.
1861 // True if this is an unsigned type.
1863 // True if this is the byte type.
1865 // True if this is the rune type.
1867 // The number of bits.
1869 // The runtime type code used in the type descriptor for this type.
1870 int runtime_type_kind_
;
1873 // The type of a floating point number.
1875 class Float_type
: public Type
1878 // Create a new float type.
1880 create_float_type(const char* name
, int bits
, int runtime_type_kind
);
1882 // Look up an existing float type.
1884 lookup_float_type(const char* name
);
1886 // Create an abstract float type.
1888 create_abstract_float_type();
1890 // Whether this is an abstract float type.
1893 { return this->is_abstract_
; }
1895 // The number of bits.
1898 { return this->bits_
; }
1900 // Whether this type is the same as T.
1902 is_identical(const Float_type
* t
) const;
1906 do_message_name(std::string
* ret
) const;
1909 do_compare_is_identity(Gogo
*)
1916 // Distinction between +0 and -0 requires a key update.
1918 do_needs_key_update()
1922 do_hash_for_method(Gogo
*, int) const;
1925 do_get_backend(Gogo
*);
1928 do_type_descriptor(Gogo
*, Named_type
*);
1931 do_reflection(Gogo
*, std::string
*) const;
1934 do_mangled_name(Gogo
*, std::string
*, bool*) const;
1937 Float_type(bool is_abstract
, int bits
, int runtime_type_kind
)
1939 is_abstract_(is_abstract
), bits_(bits
),
1940 runtime_type_kind_(runtime_type_kind
)
1943 // Map names of float types to the types themselves.
1944 typedef std::map
<std::string
, Named_type
*> Named_float_types
;
1945 static Named_float_types named_float_types
;
1947 // True if this is an abstract type.
1949 // The number of bits in the floating point value.
1951 // The runtime type code used in the type descriptor for this type.
1952 int runtime_type_kind_
;
1955 // The type of a complex number.
1957 class Complex_type
: public Type
1960 // Create a new complex type.
1962 create_complex_type(const char* name
, int bits
, int runtime_type_kind
);
1964 // Look up an existing complex type.
1966 lookup_complex_type(const char* name
);
1968 // Create an abstract complex type.
1969 static Complex_type
*
1970 create_abstract_complex_type();
1972 // Whether this is an abstract complex type.
1975 { return this->is_abstract_
; }
1977 // The number of bits: 64 or 128.
1979 { return this->bits_
; }
1981 // Whether this type is the same as T.
1983 is_identical(const Complex_type
* t
) const;
1987 do_message_name(std::string
*) const;
1990 do_compare_is_identity(Gogo
*)
1997 // Distinction between +0 and -0 requires a key update.
1999 do_needs_key_update()
2003 do_hash_for_method(Gogo
*, int) const;
2006 do_get_backend(Gogo
*);
2009 do_type_descriptor(Gogo
*, Named_type
*);
2012 do_reflection(Gogo
*, std::string
*) const;
2015 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2018 Complex_type(bool is_abstract
, int bits
, int runtime_type_kind
)
2019 : Type(TYPE_COMPLEX
),
2020 is_abstract_(is_abstract
), bits_(bits
),
2021 runtime_type_kind_(runtime_type_kind
)
2024 // Map names of complex types to the types themselves.
2025 typedef std::map
<std::string
, Named_type
*> Named_complex_types
;
2026 static Named_complex_types named_complex_types
;
2028 // True if this is an abstract type.
2030 // The number of bits in the complex value--64 or 128.
2032 // The runtime type code used in the type descriptor for this type.
2033 int runtime_type_kind_
;
2036 // The type of a string.
2038 class String_type
: public Type
2047 do_message_name(std::string
* ret
) const
2048 { ret
->append("<untyped string>"); }
2051 do_has_pointer() const
2055 do_compare_is_identity(Gogo
*)
2058 // New string might have a smaller backing store.
2060 do_needs_key_update()
2064 do_get_backend(Gogo
*);
2067 do_type_descriptor(Gogo
*, Named_type
*);
2070 do_reflection(Gogo
*, std::string
*) const;
2073 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2076 // The named string type.
2077 static Named_type
* string_type_
;
2080 // The type of a function.
2082 class Function_type
: public Type
2085 Function_type(Typed_identifier
* receiver
, Typed_identifier_list
* parameters
,
2086 Typed_identifier_list
* results
, Location location
)
2087 : Type(TYPE_FUNCTION
),
2088 receiver_(receiver
), parameters_(parameters
), results_(results
),
2089 location_(location
), is_varargs_(false), is_builtin_(false),
2090 fnbtype_(NULL
), is_tagged_(false)
2093 // Get the receiver.
2094 const Typed_identifier
*
2096 { return this->receiver_
; }
2098 // Add an escape note for the receiver.
2100 add_receiver_note(int encoding
)
2101 { this->receiver_
->set_note(Escape_note::make_tag(encoding
)); }
2103 // Get the return names and types.
2104 const Typed_identifier_list
*
2106 { return this->results_
; }
2108 // Get the parameter names and types.
2109 const Typed_identifier_list
*
2111 { return this->parameters_
; }
2113 // Add an escape note for the ith parameter.
2115 add_parameter_note(int index
, int encoding
)
2116 { this->parameters_
->at(index
).set_note(Escape_note::make_tag(encoding
)); }
2118 // Whether this function has been tagged during escape analysis.
2121 { return this->is_tagged_
; }
2123 // Mark this function as tagged after analyzing its escape.
2126 { this->is_tagged_
= true; }
2128 // Whether this is a varargs function.
2131 { return this->is_varargs_
; }
2133 // Whether this is a builtin function.
2136 { return this->is_builtin_
; }
2138 // The location where this type was defined.
2141 { return this->location_
; }
2143 // Return whether this is a method type.
2146 { return this->receiver_
!= NULL
; }
2148 // Whether T is a valid redeclaration of this type. This is called
2149 // when a function is declared more than once.
2151 is_valid_redeclaration(const Function_type
* t
, std::string
*) const;
2153 // Whether this type is the same as T.
2155 is_identical(const Function_type
* t
, bool ignore_receiver
, int flags
,
2156 std::string
*) const;
2158 // Record that this is a varargs function.
2161 { this->is_varargs_
= true; }
2163 // Record that this is a builtin function.
2166 { this->is_builtin_
= true; }
2168 // Import a function type.
2169 static Function_type
*
2172 // Return a copy of this type without a receiver. This is only
2173 // valid for a method type.
2175 copy_without_receiver() const;
2177 // Return a copy of this type with a receiver. This is used when an
2178 // interface method is attached to a named or struct type.
2180 copy_with_receiver(Type
*) const;
2182 // Return a copy of this type with the receiver treated as the first
2183 // parameter. If WANT_POINTER_RECEIVER is true, the receiver is
2184 // forced to be a pointer.
2186 copy_with_receiver_as_param(bool want_pointer_receiver
) const;
2188 // Return a copy of this type ignoring any receiver and using dummy
2189 // names for all parameters. This is used for thunks for method
2192 copy_with_names() const;
2195 make_function_type_descriptor_type();
2197 // Return the backend representation of this function type. This is used
2198 // as the real type of a backend function declaration or defintion.
2200 get_backend_fntype(Gogo
*);
2202 // Return whether this is a Backend_function_type.
2204 is_backend_function_type() const
2207 // Append just the signature of the function type.
2209 append_signature(std::string
*) const;
2213 do_message_name(std::string
*) const;
2216 do_traverse(Traverse
*);
2218 // A function descriptor may be allocated on the heap.
2220 do_has_pointer() const
2224 do_compare_is_identity(Gogo
*)
2228 do_hash_for_method(Gogo
*, int) const;
2231 do_get_backend(Gogo
*);
2234 do_type_descriptor(Gogo
*, Named_type
*);
2237 do_reflection(Gogo
*, std::string
*) const;
2240 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2243 do_export(Export
*) const;
2247 type_descriptor_params(Type
*, const Typed_identifier
*,
2248 const Typed_identifier_list
*);
2250 // A mapping from a list of result types to a backend struct type.
2255 operator()(const Typed_identifier_list
*) const;
2262 operator()(const Typed_identifier_list
*,
2263 const Typed_identifier_list
*) const;
2266 typedef Unordered_map_hash(Typed_identifier_list
*, Btype
*,
2267 Results_hash
, Results_equal
) Results_structs
;
2269 static Results_structs results_structs
;
2271 // The receiver name and type. This will be NULL for a normal
2272 // function, non-NULL for a method.
2273 Typed_identifier
* receiver_
;
2274 // The parameter names and types.
2275 Typed_identifier_list
* parameters_
;
2276 // The result names and types. This will be NULL if no result was
2278 Typed_identifier_list
* results_
;
2279 // The location where this type was defined. This exists solely to
2280 // give a location for the fields of the struct if this function
2281 // returns multiple values.
2283 // Whether this function takes a variable number of arguments.
2285 // Whether this is a special builtin function which can not simply
2286 // be called. This is used for len, cap, etc.
2288 // The backend representation of this type for backend function
2289 // declarations and definitions.
2291 // Whether this function has been analyzed by escape analysis. If this is
2292 // TRUE, this function type's parameters contain a summary of the analysis.
2296 // The type of a function's backend representation.
2298 class Backend_function_type
: public Function_type
2301 Backend_function_type(Typed_identifier
* receiver
,
2302 Typed_identifier_list
* parameters
,
2303 Typed_identifier_list
* results
, Location location
)
2304 : Function_type(receiver
, parameters
, results
, location
)
2307 // Return whether this is a Backend_function_type. This overrides
2308 // Function_type::is_backend_function_type.
2310 is_backend_function_type() const
2315 do_get_backend(Gogo
* gogo
)
2316 { return this->get_backend_fntype(gogo
); }
2319 // The type of a pointer.
2321 class Pointer_type
: public Type
2324 Pointer_type(Type
* to_type
)
2325 : Type(TYPE_POINTER
),
2331 { return this->to_type_
; }
2333 // Import a pointer type.
2334 static Pointer_type
*
2338 make_pointer_type_descriptor_type();
2342 do_message_name(std::string
*) const;
2345 do_traverse(Traverse
*);
2348 do_verify(Gogo
* gogo
)
2349 { return this->to_type_
->verify(gogo
); }
2351 // If this is a pointer to a type that can't be in the heap, then
2352 // the garbage collector does not have to look at this, so pretend
2353 // that this is not a pointer at all.
2355 do_has_pointer() const
2356 { return this->to_type_
->in_heap(); }
2359 do_compare_is_identity(Gogo
*)
2363 do_hash_for_method(Gogo
*, int) const;
2366 do_get_backend(Gogo
*);
2369 do_type_descriptor(Gogo
*, Named_type
*);
2372 do_reflection(Gogo
*, std::string
*) const;
2375 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2378 do_export(Export
*) const;
2381 // The type to which this type points.
2385 // The nil type. We use a special type for nil because it is not the
2386 // same as any other type. In C term nil has type void*, but there is
2387 // no such type in Go.
2389 class Nil_type
: public Type
2398 do_message_name(std::string
* ret
) const
2399 { ret
->append("<NIL>"); }
2402 do_compare_is_identity(Gogo
*)
2406 do_get_backend(Gogo
* gogo
);
2409 do_type_descriptor(Gogo
*, Named_type
*)
2410 { go_unreachable(); }
2413 do_reflection(Gogo
*, std::string
*) const
2414 { go_unreachable(); }
2417 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2420 // The type of a field in a struct.
2425 explicit Struct_field(const Typed_identifier
& typed_identifier
)
2426 : typed_identifier_(typed_identifier
), tag_(NULL
), is_imported_(false)
2433 // Return whether this struct field is named NAME.
2435 is_field_name(const std::string
& name
) const;
2437 // Return whether this struct field is an unexported field named NAME.
2439 is_unexported_field_name(Gogo
*, const std::string
& name
) const;
2441 // Return whether this struct field is an embedded built-in type.
2443 is_embedded_builtin(Gogo
*) const;
2448 { return this->typed_identifier_
.type(); }
2450 // The field location.
2453 { return this->typed_identifier_
.location(); }
2455 // Whether the field has a tag.
2458 { return this->tag_
!= NULL
; }
2464 go_assert(this->tag_
!= NULL
);
2468 // Whether this is an anonymous field.
2470 is_anonymous() const
2471 { return this->typed_identifier_
.name().empty(); }
2473 // Set the tag. FIXME: This is never freed.
2475 set_tag(const std::string
& tag
)
2476 { this->tag_
= new std::string(tag
); }
2478 // Record that this field is defined in an imported struct.
2481 { this->is_imported_
= true; }
2483 // Set the type. This is only used in error cases.
2485 set_type(Type
* type
)
2486 { this->typed_identifier_
.set_type(type
); }
2489 // The field name, type, and location.
2490 Typed_identifier typed_identifier_
;
2491 // The field tag. This is NULL if the field has no tag.
2493 // Whether this field is defined in an imported struct.
2497 // A list of struct fields.
2499 class Struct_field_list
2506 // Whether the list is empty.
2509 { return this->entries_
.empty(); }
2511 // Return the number of entries.
2514 { return this->entries_
.size(); }
2516 // Add an entry to the end of the list.
2518 push_back(const Struct_field
& sf
)
2519 { this->entries_
.push_back(sf
); }
2521 // Index into the list.
2524 { return this->entries_
.at(i
); }
2526 // Last entry in list.
2529 { return this->entries_
.back(); }
2533 typedef std::vector
<Struct_field
>::iterator iterator
;
2534 typedef std::vector
<Struct_field
>::const_iterator const_iterator
;
2538 { return this->entries_
.begin(); }
2542 { return this->entries_
.begin(); }
2546 { return this->entries_
.end(); }
2550 { return this->entries_
.end(); }
2553 std::vector
<Struct_field
> entries_
;
2556 // The type of a struct.
2558 class Struct_type
: public Type
2561 Struct_type(Struct_field_list
* fields
, Location location
)
2562 : Type(TYPE_STRUCT
),
2563 fields_(fields
), location_(location
), all_methods_(NULL
),
2564 is_struct_incomparable_(false), has_padding_(false),
2565 is_results_struct_(false)
2568 // Return the field NAME. This only looks at local fields, not at
2569 // embedded types. If the field is found, and PINDEX is not NULL,
2570 // this sets *PINDEX to the field index. If the field is not found,
2571 // this returns NULL.
2573 find_local_field(const std::string
& name
, unsigned int *pindex
) const;
2575 // Return the field number INDEX.
2577 field(unsigned int index
) const
2578 { return &this->fields_
->at(index
); }
2580 // Get the struct fields.
2581 const Struct_field_list
*
2583 { return this->fields_
; }
2585 // Return the number of fields.
2588 { return this->fields_
->size(); }
2590 // Location of struct definition.
2593 { return this->location_
; }
2595 // Push a new field onto the end of the struct. This is used when
2596 // building a closure variable.
2598 push_field(const Struct_field
& sf
)
2599 { this->fields_
->push_back(sf
); }
2601 // Return an expression referring to field NAME in STRUCT_EXPR, or
2602 // NULL if there is no field with that name.
2603 Field_reference_expression
*
2604 field_reference(Expression
* struct_expr
, const std::string
& name
,
2607 // Return the total number of fields, including embedded fields.
2608 // This is the number of values that can appear in a conversion to
2611 total_field_count() const;
2613 // Whether this type is identical with T.
2615 is_identical(const Struct_type
* t
, int) const;
2617 // Return whether NAME is a local field which is not exported. This
2618 // is only used for better error reporting.
2620 is_unexported_local_field(Gogo
*, const std::string
& name
) const;
2622 // If this is an unnamed struct, build the complete list of methods,
2623 // including those from anonymous fields, and build methods stubs if
2626 finalize_methods(Gogo
*);
2628 // Return whether this type has any methods. This should only be
2629 // called after the finalize_methods pass.
2631 has_any_methods() const
2632 { return this->all_methods_
!= NULL
; }
2634 // Return the methods for this type. This should only be called
2635 // after the finalize_methods pass.
2638 { return this->all_methods_
; }
2640 // Return the method to use for NAME. This returns NULL if there is
2641 // no such method or if the method is ambiguous. When it returns
2642 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2644 method_function(const std::string
& name
, bool* is_ambiguous
) const;
2646 // Return a pointer to the interface method table for this type for
2647 // the interface INTERFACE. If IS_POINTER is true, set the type
2648 // descriptor to a pointer to this type, otherwise set it to this
2651 interface_method_table(Interface_type
* interface
, bool is_pointer
);
2653 // Traverse just the field types of a struct type.
2655 traverse_field_types(Traverse
* traverse
)
2656 { return this->do_traverse(traverse
); }
2658 // If the offset of field INDEX in the backend implementation can be
2659 // determined, set *POFFSET to the offset in bytes and return true.
2660 // Otherwise, return false.
2662 backend_field_offset(Gogo
*, unsigned int index
, int64_t* poffset
);
2664 // Finish the backend representation of all the fields.
2666 finish_backend_fields(Gogo
*);
2668 // Import a struct type.
2673 make_struct_type_descriptor_type();
2675 // Return whether this is a generated struct that is not comparable.
2677 is_struct_incomparable() const
2678 { return this->is_struct_incomparable_
; }
2680 // Record that this is a generated struct that is not comparable.
2682 set_is_struct_incomparable()
2683 { this->is_struct_incomparable_
= true; }
2685 // Return whether this struct's backend type has padding, due to
2686 // trailing zero-sized field.
2689 { return this->has_padding_
; }
2691 // Record that this struct's backend type has padding.
2694 { this->has_padding_
= true; }
2696 // Return whether this is a results struct created to hold the
2697 // results of a function that returns multiple results.
2699 is_results_struct() const
2700 { return this->is_results_struct_
; }
2702 // Record that this is a results struct.
2704 set_is_results_struct()
2705 { this->is_results_struct_
= true; }
2707 // Write the hash function for this type.
2709 write_hash_function(Gogo
*, Named_object
* function
, Function_type
*);
2711 // Write the equality function for this type.
2713 write_equal_function(Gogo
*, Named_object
* function
, Named_type
*);
2715 // Whether we can write this type to a C header file, to implement
2718 can_write_to_c_header(std::vector
<const Named_object
*>*,
2719 std::vector
<const Named_object
*>*) const;
2721 // Write this type to a C header file, to implement -fgo-c-header.
2723 write_to_c_header(std::ostream
&) const;
2727 do_message_name(std::string
*) const;
2730 do_traverse(Traverse
*);
2736 do_has_pointer() const;
2739 do_compare_is_identity(Gogo
*);
2745 do_needs_key_update();
2748 do_hash_might_panic();
2754 do_hash_for_method(Gogo
*, int) const;
2757 do_get_backend(Gogo
*);
2760 do_type_descriptor(Gogo
*, Named_type
*);
2763 do_reflection(Gogo
*, std::string
*) const;
2766 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2769 do_export(Export
*) const;
2773 can_write_type_to_c_header(const Type
*,
2774 std::vector
<const Named_object
*>*,
2775 std::vector
<const Named_object
*>*) const;
2778 write_field_to_c_header(std::ostream
&, const std::string
&, const Type
*) const;
2780 // Used to merge method sets of identical unnamed structs.
2781 typedef Unordered_map_hash(Struct_type
*, Struct_type
*, Type_hash_identical
,
2782 Type_identical
) Identical_structs
;
2784 static Identical_structs identical_structs
;
2786 // Used to manage method tables for identical unnamed structs.
2787 typedef std::pair
<Interface_method_tables
*, Interface_method_tables
*>
2788 Struct_method_table_pair
;
2790 typedef Unordered_map_hash(Struct_type
*, Struct_method_table_pair
*,
2791 Type_hash_identical
, Type_identical
)
2792 Struct_method_tables
;
2794 static Struct_method_tables struct_method_tables
;
2796 // Used to avoid infinite loops in field_reference_depth.
2797 struct Saw_named_type
2799 Saw_named_type
* next
;
2803 Field_reference_expression
*
2804 field_reference_depth(Expression
* struct_expr
, const std::string
& name
,
2805 Location
, Saw_named_type
*,
2806 unsigned int* depth
) const;
2808 // The fields of the struct.
2809 Struct_field_list
* fields_
;
2810 // The place where the struct was declared.
2812 // If this struct is unnamed, a list of methods.
2813 Methods
* all_methods_
;
2814 // True if this is a generated struct that is not considered to be
2816 bool is_struct_incomparable_
;
2817 // True if this struct's backend type has padding, due to trailing
2818 // zero-sized field.
2820 // True if this is a results struct created to hold the results of a
2821 // function that returns multiple results.
2822 bool is_results_struct_
;
2825 // The type of an array.
2827 class Array_type
: public Type
2830 Array_type(Type
* element_type
, Expression
* length
)
2832 element_type_(element_type
), length_(length
), blength_(NULL
),
2833 issued_length_error_(false), is_array_incomparable_(false)
2836 // Return the element type.
2838 element_type() const
2839 { return this->element_type_
; }
2841 // Return the length. This will return NULL for a slice.
2844 { return this->length_
; }
2846 // Store the length as an int64_t into *PLEN. Return false if the
2847 // length can not be determined. This will assert if called for a
2850 int_length(int64_t* plen
) const;
2852 // Whether this type is identical with T.
2854 is_identical(const Array_type
* t
, int) const;
2856 // Return an expression for the pointer to the values in an array.
2858 get_value_pointer(Gogo
*, Expression
* array
) const;
2860 // Return an expression for the length of an array with this type.
2862 get_length(Gogo
*, Expression
* array
) const;
2864 // Return an expression for the capacity of an array with this type.
2866 get_capacity(Gogo
*, Expression
* array
) const;
2868 // Import an array type.
2872 // Return the backend representation of the element type.
2874 get_backend_element(Gogo
*, bool use_placeholder
);
2876 // Return the backend representation of the length.
2878 get_backend_length(Gogo
*);
2880 // Finish the backend representation of the element type.
2882 finish_backend_element(Gogo
*);
2885 make_array_type_descriptor_type();
2888 make_slice_type_descriptor_type();
2890 // Return whether this is a generated array that is not comparable.
2892 is_array_incomparable() const
2893 { return this->is_array_incomparable_
; }
2895 // Record that this is a generated array that is not comparable.
2897 set_is_array_incomparable()
2898 { this->is_array_incomparable_
= true; }
2900 // Write the hash function for this type.
2902 write_hash_function(Gogo
*, Named_object
* function
, Function_type
*);
2904 // Write the equality function for this type.
2906 write_equal_function(Gogo
*, Named_object
* function
, Named_type
*);
2910 do_message_name(std::string
*) const;
2913 do_traverse(Traverse
* traverse
);
2919 do_has_pointer() const;
2922 do_compare_is_identity(Gogo
*);
2927 return this->length_
!= NULL
&& this->element_type_
->is_reflexive();
2931 do_needs_key_update()
2932 { return this->element_type_
->needs_key_update(); }
2935 do_hash_might_panic()
2936 { return this->length_
!= NULL
&& this->element_type_
->hash_might_panic(); }
2940 { return this->length_
== NULL
|| this->element_type_
->in_heap(); }
2943 do_hash_for_method(Gogo
*, int) const;
2946 do_get_backend(Gogo
*);
2949 do_type_descriptor(Gogo
*, Named_type
*);
2952 do_reflection(Gogo
*, std::string
*) const;
2955 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2958 do_export(Export
*) const;
2962 verify_length(Gogo
*);
2965 array_type_descriptor(Gogo
*, Named_type
*);
2968 slice_type_descriptor(Gogo
*, Named_type
*);
2970 // The type of elements of the array.
2971 Type
* element_type_
;
2972 // The number of elements. This may be NULL.
2973 Expression
* length_
;
2974 // The backend representation of the length.
2975 // We only want to compute this once.
2976 Bexpression
* blength_
;
2977 // Whether or not an invalid length error has been issued for this type,
2978 // to avoid knock-on errors.
2979 mutable bool issued_length_error_
;
2980 // True if this is a generated array that is not considered to be
2982 bool is_array_incomparable_
;
2985 // The type of a map.
2987 class Map_type
: public Type
2990 Map_type(Type
* key_type
, Type
* val_type
, Location location
)
2992 key_type_(key_type
), val_type_(val_type
), hmap_type_(NULL
),
2993 bucket_type_(NULL
), hiter_type_(NULL
), location_(location
)
2996 // Return the key type.
2999 { return this->key_type_
; }
3001 // Return the value type.
3004 { return this->val_type_
; }
3006 // Return the type used for an iteration over this map.
3010 // If this map requires the "fat" functions, returns the pointer to
3011 // pass as the zero value to those functions. Otherwise, in the
3012 // normal case, returns NULL.
3014 fat_zero_value(Gogo
*);
3016 // Map algorithm to use for this map type. We may use specialized
3017 // fast map routines for certain key types.
3022 // 32-bit pointer key.
3026 // 64-bit pointer key.
3037 // Return whether VAR is the map zero value.
3039 is_zero_value(Variable
* var
);
3041 // Return the backend representation of the map zero value.
3043 backend_zero_value(Gogo
*);
3045 // Whether this type is identical with T.
3047 is_identical(const Map_type
* t
, int) const;
3049 // Import a map type.
3054 make_map_type_descriptor_type();
3056 // This must be in sync with libgo/go/runtime/map.go.
3057 static const int bucket_size
= 8;
3061 do_message_name(std::string
*) const;
3064 do_traverse(Traverse
*);
3070 do_has_pointer() const
3074 do_compare_is_identity(Gogo
*)
3080 return this->key_type_
->is_reflexive() && this->val_type_
->is_reflexive();
3084 do_hash_for_method(Gogo
*, int) const;
3087 do_get_backend(Gogo
*);
3090 do_type_descriptor(Gogo
*, Named_type
*);
3093 do_reflection(Gogo
*, std::string
*) const;
3096 do_mangled_name(Gogo
*, std::string
*, bool*) const;
3099 do_export(Export
*) const;
3102 // These must be in sync with libgo/go/runtime/map.go.
3103 static const int max_key_size
= 128;
3104 static const int max_val_size
= 128;
3105 static const int max_zero_size
= 1024;
3107 // Maps with value types larger than max_zero_size require passing a
3108 // zero value pointer to the map functions.
3110 // The zero value variable.
3111 static Named_object
* zero_value
;
3113 // The current size of the zero value.
3114 static int64_t zero_value_size
;
3116 // The current alignment of the zero value.
3117 static int64_t zero_value_align
;
3120 bucket_type(Gogo
*, int64_t, int64_t);
3129 // The hashmap type. At run time a map is represented as a pointer
3132 // The bucket type, the type used to hold keys and values at run time.
3134 // The iterator type.
3136 // Where the type was defined.
3140 // The type of a channel.
3142 class Channel_type
: public Type
3145 Channel_type(bool may_send
, bool may_receive
, Type
* element_type
)
3146 : Type(TYPE_CHANNEL
),
3147 may_send_(may_send
), may_receive_(may_receive
),
3148 element_type_(element_type
)
3149 { go_assert(may_send
|| may_receive
); }
3151 // Whether this channel can send data.
3154 { return this->may_send_
; }
3156 // Whether this channel can receive data.
3159 { return this->may_receive_
; }
3161 // The type of the values that may be sent on this channel. This is
3162 // NULL if any type may be sent.
3164 element_type() const
3165 { return this->element_type_
; }
3167 // Whether this type is identical with T.
3169 is_identical(const Channel_type
* t
, int) const;
3171 // Import a channel type.
3172 static Channel_type
*
3176 make_chan_type_descriptor_type();
3183 do_message_name(std::string
*) const;
3186 do_traverse(Traverse
* traverse
)
3187 { return Type::traverse(this->element_type_
, traverse
); }
3193 do_has_pointer() const
3197 do_compare_is_identity(Gogo
*)
3201 do_hash_for_method(Gogo
*, int) const;
3204 do_get_backend(Gogo
*);
3207 do_type_descriptor(Gogo
*, Named_type
*);
3210 do_reflection(Gogo
*, std::string
*) const;
3213 do_mangled_name(Gogo
*, std::string
*, bool*) const;
3216 do_export(Export
*) const;
3219 // Whether this channel can send data.
3221 // Whether this channel can receive data.
3223 // The types of elements which may be sent on this channel. If this
3224 // is NULL, it means that any type may be sent.
3225 Type
* element_type_
;
3228 // An interface type.
3230 class Interface_type
: public Type
3233 Interface_type(Typed_identifier_list
* methods
, Location location
)
3234 : Type(TYPE_INTERFACE
),
3235 parse_methods_(methods
), all_methods_(NULL
), location_(location
),
3236 package_(NULL
), interface_btype_(NULL
), bmethods_(NULL
),
3237 assume_identical_(NULL
), methods_are_finalized_(false),
3238 bmethods_is_placeholder_(false), seen_(false)
3239 { go_assert(methods
== NULL
|| !methods
->empty()); }
3241 // The location where the interface type was defined.
3244 { return this->location_
; }
3246 // The package where the interface type was defined. Returns NULL
3247 // for the package currently being compiled.
3250 { return this->package_
; }
3252 // Return whether this is an empty interface.
3256 go_assert(this->methods_are_finalized_
);
3257 return this->all_methods_
== NULL
;
3260 // Return the list of locally defined methods. This will return NULL
3261 // for an empty interface. Embedded interfaces will appear in this
3262 // list as an entry with no name.
3263 const Typed_identifier_list
*
3264 local_methods() const
3265 { return this->parse_methods_
; }
3267 // Return the list of all methods. This will return NULL for an
3269 const Typed_identifier_list
*
3272 // Return the number of methods.
3274 method_count() const;
3276 // Return the method NAME, or NULL.
3277 const Typed_identifier
*
3278 find_method(const std::string
& name
) const;
3280 // Return the zero-based index of method NAME.
3282 method_index(const std::string
& name
) const;
3284 // Finalize the methods. This sets all_methods_. This handles
3285 // interface inheritance.
3289 // Return true if T implements this interface. If this returns
3290 // false, and REASON is not NULL, it sets *REASON to the reason that
3293 implements_interface(const Type
* t
, std::string
* reason
) const;
3295 // Whether this type is identical with T. REASON is as in
3296 // implements_interface.
3298 is_identical(const Interface_type
* t
, int) const;
3300 // Whether we can assign T to this type. is_identical is known to
3303 is_compatible_for_assign(const Interface_type
*, std::string
* reason
) const;
3305 // Return whether NAME is a method which is not exported. This is
3306 // only used for better error reporting.
3308 is_unexported_method(Gogo
*, const std::string
& name
) const;
3310 // Import an interface type.
3311 static Interface_type
*
3314 // Make a struct for an empty interface type.
3316 get_backend_empty_interface_type(Gogo
*);
3318 // Get a pointer to the backend representation of the method table.
3320 get_backend_methods(Gogo
*);
3322 // Return a placeholder for the backend representation of the
3323 // pointer to the method table.
3325 get_backend_methods_placeholder(Gogo
*);
3327 // Finish the backend representation of the method types.
3329 finish_backend_methods(Gogo
*);
3332 make_interface_type_descriptor_type();
3334 // Return whether methods are finalized for this interface.
3336 methods_are_finalized() const
3337 { return this->methods_are_finalized_
; }
3341 do_message_name(std::string
*) const;
3344 do_traverse(Traverse
*);
3347 do_has_pointer() const
3351 do_compare_is_identity(Gogo
*)
3354 // Not reflexive if it contains a float.
3359 // Distinction between +0 and -0 requires a key update if it
3360 // contains a float.
3362 do_needs_key_update()
3365 // Hashing an unhashable type stored in an interface might panic.
3367 do_hash_might_panic()
3371 do_hash_for_method(Gogo
*, int) const;
3374 do_get_backend(Gogo
*);
3377 do_type_descriptor(Gogo
*, Named_type
*);
3380 do_reflection(Gogo
*, std::string
*) const;
3383 do_mangled_name(Gogo
*, std::string
*, bool*) const;
3386 do_export(Export
*) const;
3389 // This type guards against infinite recursion when comparing
3390 // interface types. We keep a list of interface types assumed to be
3391 // identical during comparison. We just keep the list on the stack.
3392 // This permits us to compare cases like
3393 // type I1 interface { F() interface{I1} }
3394 // type I2 interface { F() interface{I2} }
3395 struct Assume_identical
3397 Assume_identical
* next
;
3398 const Interface_type
* t1
;
3399 const Interface_type
* t2
;
3403 assume_identical(const Interface_type
*, const Interface_type
*) const;
3405 struct Bmethods_map_entry
3408 bool is_placeholder
;
3411 // A mapping from Interface_type to the backend type of its bmethods_,
3412 // used to ensure that the backend representation of identical types
3414 typedef Unordered_map_hash(const Interface_type
*, Bmethods_map_entry
,
3415 Type_hash_identical
, Type_identical
) Bmethods_map
;
3417 static Bmethods_map bmethods_map
;
3419 // The list of methods associated with the interface from the
3420 // parser. This will be NULL for the empty interface. This may
3421 // include unnamed interface types.
3422 Typed_identifier_list
* parse_methods_
;
3423 // The list of all methods associated with the interface. This
3424 // expands any interface types listed in methods_. It is set by
3425 // finalize_methods. This will be NULL for the empty interface.
3426 Typed_identifier_list
* all_methods_
;
3427 // The location where the interface was defined.
3429 // The package where the interface was defined. This is NULL for
3430 // the package being compiled.
3432 // The backend representation of this type during backend conversion.
3433 Btype
* interface_btype_
;
3434 // The backend representation of the pointer to the method table.
3436 // A list of interface types assumed to be identical during
3437 // interface comparison.
3438 mutable Assume_identical
* assume_identical_
;
3439 // Whether the methods have been finalized.
3440 bool methods_are_finalized_
;
3441 // Whether the bmethods_ field is a placeholder.
3442 bool bmethods_is_placeholder_
;
3443 // Used to avoid endless recursion in do_mangled_name.
3447 // The value we keep for a named type. This lets us get the right
3448 // name when we convert to backend. Note that we don't actually keep
3449 // the name here; the name is in the Named_object which points to
3450 // this. This object exists to hold a unique backend representation for
3453 class Named_type
: public Type
3456 Named_type(Named_object
* named_object
, Type
* type
, Location location
)
3458 named_object_(named_object
), in_function_(NULL
), in_function_index_(0),
3459 type_(type
), local_methods_(NULL
), all_methods_(NULL
),
3460 interface_method_tables_(NULL
), pointer_interface_method_tables_(NULL
),
3461 location_(location
), named_btype_(NULL
), dependencies_(),
3462 is_alias_(false), is_visible_(true), is_error_(false), in_heap_(true),
3463 is_placeholder_(false), is_converted_(false), is_verified_(false),
3464 seen_(false), seen_in_compare_is_identity_(false),
3465 seen_in_get_backend_(false), seen_alias_(false)
3468 // Return the associated Named_object. This holds the actual name.
3471 { return this->named_object_
; }
3474 named_object() const
3475 { return this->named_object_
; }
3477 // Set the Named_object. This is used when we see a type
3478 // declaration followed by a type.
3480 set_named_object(Named_object
* no
)
3481 { this->named_object_
= no
; }
3483 // Whether this is an alias (type T1 = T2) rather than an ordinary
3484 // named type (type T1 T2).
3487 { return this->is_alias_
; }
3489 // Record that this type is an alias.
3492 { this->is_alias_
= true; }
3494 // Mark this type as not permitted in the heap.
3497 { this->in_heap_
= false; }
3499 // Return the function in which this type is defined. This will
3500 // return NULL for a type defined in global scope.
3502 in_function(unsigned int *pindex
) const
3504 *pindex
= this->in_function_index_
;
3505 return this->in_function_
;
3508 // Set the function in which this type is defined.
3510 set_in_function(Named_object
* f
, unsigned int index
)
3512 this->in_function_
= f
;
3513 this->in_function_index_
= index
;
3516 // Return the name of the type.
3520 // Return the underlying type.
3523 { return this->type_
; }
3527 { return this->type_
; }
3529 // Return the location.
3532 { return this->location_
; }
3534 // Whether this type is visible. This only matters when parsing.
3537 { return this->is_visible_
; }
3539 // Mark this type as visible.
3542 { this->is_visible_
= true; }
3544 // Mark this type as invisible.
3547 { this->is_visible_
= false; }
3549 // Whether this is a builtin type.
3552 { return Linemap::is_predeclared_location(this->location_
); }
3554 // Whether this named type is valid. A recursive named type is invalid.
3557 { return !this->is_error_
; }
3559 // Return the base type for this type.
3566 // Return whether this is an error type.
3568 is_named_error_type() const;
3570 // Return whether this type is comparable. If REASON is not NULL,
3571 // set *REASON when returning false.
3573 named_type_is_comparable(std::string
* reason
) const;
3575 // Add a method to this type.
3577 add_method(const std::string
& name
, Function
*);
3579 // Add a method declaration to this type.
3581 add_method_declaration(const std::string
& name
, Package
* package
,
3582 Function_type
* type
, Location location
);
3584 // Add an existing method--one defined before the type itself was
3585 // defined--to a type.
3587 add_existing_method(Named_object
*);
3589 // Look up a local method.
3591 find_local_method(const std::string
& name
) const;
3593 // Return the list of local methods.
3595 local_methods() const;
3597 // Build the complete list of methods, including those from
3598 // anonymous fields, and build method stubs if needed.
3600 finalize_methods(Gogo
*);
3602 // Return whether this type has any methods. This should only be
3603 // called after the finalize_methods pass.
3605 has_any_methods() const;
3607 // Return the methods for this type. This should only be called
3608 // after the finalized_methods pass.
3612 // Return the method to use for NAME. This returns NULL if there is
3613 // no such method or if the method is ambiguous. When it returns
3614 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
3616 method_function(const std::string
& name
, bool *is_ambiguous
) const;
3618 // Return whether NAME is a known field or method which is not
3619 // exported. This is only used for better error reporting.
3621 is_unexported_local_method(Gogo
*, const std::string
& name
) const;
3623 // Return a pointer to the interface method table for this type for
3624 // the interface INTERFACE. If IS_POINTER is true, set the type
3625 // descriptor to a pointer to this type, otherwise set it to this
3628 interface_method_table(Interface_type
* interface
, bool is_pointer
);
3630 // Note that a type must be converted to the backend representation
3631 // before we convert this type.
3633 add_dependency(Named_type
* nt
)
3634 { this->dependencies_
.push_back(nt
); }
3636 // Return true if the size and alignment of the backend
3637 // representation of this type is known. This is always true after
3638 // types have been converted, but may be false beforehand.
3640 is_named_backend_type_size_known() const
3641 { return this->named_btype_
!= NULL
&& !this->is_placeholder_
; }
3643 // Add to the reflection string as for Type::append_reflection, but
3644 // if USE_ALIAS use the alias name rather than the alias target.
3646 append_reflection_type_name(Gogo
*, bool use_alias
, std::string
*) const;
3648 // Append the symbol type name as for Type::append_mangled_name,
3649 // but if USE_ALIAS use the alias name rather than the alias target.
3651 append_symbol_type_name(Gogo
*, bool use_alias
, std::string
*,
3652 bool* is_non_identifier
) const;
3654 // Import a named type.
3656 import_named_type(Import
*, Named_type
**);
3658 // Initial conversion to backend representation.
3664 do_message_name(std::string
* ret
) const;
3667 do_traverse(Traverse
* traverse
)
3668 { return Type::traverse(this->type_
, traverse
); }
3674 do_has_pointer() const;
3677 do_compare_is_identity(Gogo
*);
3683 do_needs_key_update();
3689 do_hash_for_method(Gogo
*, int) const;
3692 do_get_backend(Gogo
*);
3695 do_type_descriptor(Gogo
*, Named_type
*);
3698 do_reflection(Gogo
*, std::string
*) const;
3701 do_mangled_name(Gogo
*, std::string
*, bool*) const;
3704 do_export(Export
*) const;
3707 // Create the placeholder during conversion.
3709 create_placeholder(Gogo
*);
3711 // A pointer back to the Named_object for this type.
3712 Named_object
* named_object_
;
3713 // If this type is defined in a function, a pointer back to the
3714 // function in which it is defined.
3715 Named_object
* in_function_
;
3716 // The index of this type in IN_FUNCTION_.
3717 unsigned int in_function_index_
;
3720 // The list of methods defined for this type. Any named type can
3722 Bindings
* local_methods_
;
3723 // The full list of methods for this type, including methods
3724 // declared for anonymous fields.
3725 Methods
* all_methods_
;
3726 // A mapping from interfaces to the associated interface method
3727 // tables for this type.
3728 Interface_method_tables
* interface_method_tables_
;
3729 // A mapping from interfaces to the associated interface method
3730 // tables for pointers to this type.
3731 Interface_method_tables
* pointer_interface_method_tables_
;
3732 // The location where this type was defined.
3734 // The backend representation of this type during backend
3735 // conversion. This is used to avoid endless recursion when a named
3736 // type refers to itself.
3737 Btype
* named_btype_
;
3738 // A list of types which must be converted to the backend
3739 // representation before this type can be converted. This is for
3741 // type S1 { p *S2 }
3743 // where we can't convert S2 to the backend representation unless we
3744 // have converted S1.
3745 std::vector
<Named_type
*> dependencies_
;
3746 // Whether this is an alias type.
3748 // Whether this type is visible. This is false if this type was
3749 // created because it was referenced by an imported object, but the
3750 // type itself was not exported. This will always be true for types
3751 // created in the current package.
3753 // Whether this type is erroneous.
3755 // Whether this type is permitted in the heap. This is true by
3756 // default, false if there is a magic //go:notinheap comment.
3758 // Whether the current value of named_btype_ is a placeholder for
3759 // which the final size of the type is not known.
3760 bool is_placeholder_
;
3761 // Whether this type has been converted to the backend
3762 // representation. Implies that is_placeholder_ is false.
3764 // Whether this type has been verified.
3766 // In a recursive operation such as has_pointer, this flag is used
3767 // to prevent infinite recursion when a type refers to itself. This
3768 // is mutable because it is always reset to false when the function
3771 // Like seen_, but used only by do_compare_is_identity.
3772 bool seen_in_compare_is_identity_
;
3773 // Like seen_, but used only by do_get_backend.
3774 bool seen_in_get_backend_
;
3775 // Like seen_, but used when resolving aliases.
3776 mutable bool seen_alias_
;
3779 // A forward declaration. This handles a type which has been declared
3782 class Forward_declaration_type
: public Type
3785 Forward_declaration_type(Named_object
* named_object
);
3787 // The named object associated with this type declaration. This
3788 // will be resolved.
3793 named_object() const;
3795 // Return the name of the type.
3799 // Return the type to which this points. Give an error if the type
3800 // has not yet been defined.
3807 // Whether the base type has been defined.
3811 // Add a method to this type.
3813 add_method(const std::string
& name
, Function
*);
3815 // Add a method declaration to this type.
3817 add_method_declaration(const std::string
& name
, Package
*, Function_type
*,
3820 // Add an already created object as a method to this type.
3822 add_existing_method(Named_object
*);
3826 do_message_name(std::string
*) const;
3829 do_traverse(Traverse
* traverse
);
3835 do_has_pointer() const
3836 { return this->real_type()->has_pointer(); }
3839 do_compare_is_identity(Gogo
* gogo
)
3840 { return this->real_type()->compare_is_identity(gogo
); }
3844 { return this->real_type()->is_reflexive(); }
3847 do_needs_key_update()
3848 { return this->real_type()->needs_key_update(); }
3852 { return this->real_type()->in_heap(); }
3855 do_hash_for_method(Gogo
* gogo
, int flags
) const
3856 { return this->real_type()->hash_for_method(gogo
, flags
); }
3859 do_get_backend(Gogo
* gogo
);
3862 do_type_descriptor(Gogo
*, Named_type
*);
3865 do_reflection(Gogo
*, std::string
*) const;
3868 do_mangled_name(Gogo
*, std::string
*, bool*) const;
3871 do_export(Export
*) const;
3874 // Issue a warning about a use of an undefined type.
3878 // The type declaration.
3879 Named_object
* named_object_
;
3880 // Whether we have issued a warning about this type.
3881 mutable bool warned_
;
3884 // The Type_context struct describes what we expect for the type of an
3889 // The exact type we expect, if known. This may be NULL.
3891 // Whether an abstract type is permitted.
3892 bool may_be_abstract
;
3896 : type(NULL
), may_be_abstract(false)
3899 Type_context(Type
* a_type
, bool a_may_be_abstract
)
3900 : type(a_type
), may_be_abstract(a_may_be_abstract
)
3904 #endif // !defined(GO_TYPES_H)