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(Call_expression
*);
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
);
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
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
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.
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.
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.
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.
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.
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
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
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.
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.
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.
671 { return this->do_hash_might_panic(); }
673 // Whether the type is permitted in the heap.
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.
682 hash_for_method(Gogo
*, int) const;
684 // Return the 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.
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.
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.
719 // Return true if this is a basic type: a type which is not composed
720 // of other types, and is not void.
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.
729 // Return a non-abstract version of an abstract 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
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.
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.
751 { return this->base()->is_error_type(); }
753 // Return true if this is a void type.
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.
762 { return this->convert
<Integer_type
, TYPE_INTEGER
>(); }
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.
772 { return this->convert
<Float_type
, TYPE_FLOAT
>(); }
776 { return this->convert
<const Float_type
, TYPE_FLOAT
>(); }
778 // If this is a complex type, return the Complex_type. Otherwise,
782 { return this->convert
<Complex_type
, TYPE_COMPLEX
>(); }
786 { return this->convert
<const Complex_type
, TYPE_COMPLEX
>(); }
788 // Return whether this is a numeric type.
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.
798 is_boolean_type() const
799 { return this->base()->classification_
== TYPE_BOOLEAN
; }
801 // Return true if this is an abstract boolean type.
803 is_abstract_boolean_type() const
804 { return this->classification_
== TYPE_BOOLEAN
; }
806 // Return true if this is a string type.
808 is_string_type() const
809 { return this->base()->classification_
== TYPE_STRING
; }
811 // Return true if this is an abstract string type.
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 _.
820 { return this->base()->classification_
== TYPE_SINK
; }
822 // If this is a function type, return it. Otherwise, return NULL.
825 { return this->convert
<Function_type
, TYPE_FUNCTION
>(); }
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.
836 // If this is a pointer type, return the type to which it points.
837 // Otherwise, return the type itself.
841 Type
* pt
= this->points_to();
842 return pt
!= NULL
? pt
: this;
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.
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
863 is_nil_constant_as_type() const;
865 // Return true if this is the return type of a function which
866 // returns multiple values.
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.
874 { return this->convert
<Struct_type
, TYPE_STRUCT
>(); }
878 { return this->convert
<const Struct_type
, TYPE_STRUCT
>(); }
880 // If this is an array type, return it. Otherwise, return NULL.
883 { return this->convert
<Array_type
, TYPE_ARRAY
>(); }
887 { return this->convert
<const Array_type
, TYPE_ARRAY
>(); }
889 // Return whether if this is a slice type.
891 is_slice_type() const;
893 // If this is a map type, return it. Otherwise, return NULL.
896 { return this->convert
<Map_type
, TYPE_MAP
>(); }
900 { return this->convert
<const Map_type
, TYPE_MAP
>(); }
902 // If this is a channel type, return it. Otherwise, return NULL.
905 { return this->convert
<Channel_type
, TYPE_CHANNEL
>(); }
909 { return this->convert
<const Channel_type
, TYPE_CHANNEL
>(); }
911 // If this is an interface type, return it. Otherwise, return NULL.
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.
927 // If this is a forward declaration, return it. Otherwise, return
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
,
940 // Return true if this type is not yet defined.
942 is_undefined() const;
944 // Return true if this is the unsafe.pointer type. We currently
945 // represent that as pointer-to-void.
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
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
964 // Look for field or method NAME for TYPE. Return an expression for
965 // it, bound to EXPR.
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.
972 is_unexported_field_or_method(Gogo
*, const Type
*, const std::string
&,
973 std::vector
<const Named_type
*>*);
975 // Convert the builtin named types.
977 convert_builtin_named_types(Gogo
*);
979 // Return the backend representation of this type.
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.
987 get_backend_placeholder(Gogo
*);
989 // Finish the backend representation of a placeholder.
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
997 type_descriptor_pointer(Gogo
* gogo
, Location
);
999 // Build the Garbage Collection symbol for this type. Return a pointer to it.
1001 gc_symbol_pointer(Gogo
* gogo
);
1003 // Return whether this type needs a garbage collection program.
1004 // Sets *PTRSIZE and *PTRDATA.
1006 needs_gcprog(Gogo
*, int64_t* ptrsize
, int64_t* ptrdata
);
1008 // Return a ptrmask variable for this type.
1010 gc_ptrmask_var(Gogo
*, int64_t ptrsize
, int64_t ptrdata
);
1012 // Return the type reflection string for this type.
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
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
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.
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.
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.
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.
1051 descriptor_ptrdata(Gogo
*, int64_t* pptrdata
);
1053 // Whether the backend size is known.
1055 is_backend_type_size_known(Gogo
*);
1057 // Return whether the type needs specially built type functions.
1059 needs_specific_type_functions(Gogo
*);
1061 // Get the equality function for a type. Returns NULL if the type
1062 // is not comparable.
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
1069 hash_function(Gogo
*, Function_type
* hash_fntype
);
1071 // Write the equal function for a type.
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.
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
);
1086 export_type(Export
* exp
) const
1087 { this->do_export(exp
); }
1091 import_type(Import
*);
1094 Type(Type_classification
);
1096 // Functions implemented by the child class.
1098 // Traverse the subtypes.
1100 do_traverse(Traverse
*);
1108 do_has_pointer() const
1112 do_compare_is_identity(Gogo
*) = 0;
1119 do_needs_key_update()
1123 do_hash_might_panic()
1130 virtual unsigned int
1131 do_hash_for_method(Gogo
*, int) const;
1134 do_get_backend(Gogo
*) = 0;
1137 do_type_descriptor(Gogo
*, Named_type
* name
) = 0;
1140 do_reflection(Gogo
*, std::string
*) const = 0;
1143 do_mangled_name(Gogo
*, std::string
*, bool*) const = 0;
1146 do_export(Export
*) const;
1148 // For children to call when they detect that they are in error.
1152 // Return whether a method expects a pointer as the receiver.
1154 method_expects_pointer(const Named_object
*);
1156 // Finalize the methods for a type.
1158 finalize_methods(Gogo
*, const Type
*, Location
, Methods
**);
1160 // Return a method from a set of methods.
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.
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
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.
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.
1191 plain_type_descriptor(Gogo
*, int runtime_type_kind
, Named_type
* name
);
1193 // Build a composite literal for the basic type descriptor.
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.
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.
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
1212 get_named_base_btype(Gogo
* gogo
, Type
* base_type
)
1213 { return base_type
->get_btype_without_hash(gogo
); }
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
>
1222 Type
* base
= this->base();
1223 return (base
->classification_
== type_classification
1224 ? static_cast<Type_class
*>(base
)
1228 template<typename Type_class
, Type_classification type_classification
>
1232 const Type
* base
= this->base();
1233 return (base
->classification_
== type_classification
1234 ? static_cast<Type_class
*>(base
)
1238 template<typename Type_class
, Type_classification type_classification
>
1242 return (this->classification_
== type_classification
1243 ? static_cast<Type_class
*>(this)
1247 template<typename Type_class
, Type_classification type_classification
>
1249 convert_no_base() const
1251 return (this->classification_
== type_classification
1252 ? static_cast<Type_class
*>(this)
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.
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.
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.
1286 type_descriptor_defined_elsewhere(Named_type
* name
, const Package
** package
);
1288 // Make a composite literal for the garbage collection program for
1291 gcprog_constructor(Gogo
*, int64_t ptrsize
, int64_t ptrdata
);
1293 // Build the hash function for a type that needs specific functions.
1295 build_hash_function(Gogo
*, int64_t size
, Function_type
* hash_fntype
);
1297 // Build the equal function for a type that needs specific functions.
1299 build_equal_function(Gogo
*, Named_type
*, int64_t size
,
1300 Function_type
* equal_fntype
);
1303 write_identity_hash(Gogo
*, int64_t size
);
1306 write_identity_equal(Gogo
*, int64_t size
);
1309 write_named_equal(Gogo
*, Named_type
*);
1311 // Build a composite literal for the uncommon type information.
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.
1319 methods_constructor(Gogo
*, Type
* methods_type
, const Methods
*,
1320 bool only_value_methods
) const;
1322 // Build a composite literal for one method.
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.
1329 add_methods_for_type(const Type
* type
, const Method::Field_indexes
*,
1330 unsigned int depth
, bool, bool,
1331 std::vector
<const Named_type
*>*,
1335 add_local_methods_for_type(const Named_type
* type
,
1336 const Method::Field_indexes
*,
1337 unsigned int depth
, bool, bool, Methods
*);
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
*>*,
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.
1353 build_stub_methods(Gogo
*, const Type
* type
, const Methods
* methods
,
1357 build_one_stub_method(Gogo
*, Method
*, const char* receiver_name
,
1358 const Typed_identifier_list
*, bool is_varargs
,
1361 // Build direct interface stub methods for a type.
1363 build_direct_iface_stub_methods(Gogo
*, const Type
*, Methods
*, Location
);
1366 build_one_iface_stub_method(Gogo
*, Method
*, const char*,
1367 const Typed_identifier_list
*,
1371 apply_field_indexes(Expression
*, const Method::Field_indexes
*,
1374 // Look for a field or method named NAME in TYPE.
1376 find_field_or_method(const Type
* type
, const std::string
& name
,
1377 bool receiver_can_be_pointer
,
1378 std::vector
<const Named_type
*>*, int* level
,
1379 bool* is_method
, bool* found_pointer_method
,
1380 std::string
* ambig1
, std::string
* ambig2
);
1382 // Helper function for is_direct_iface_type, to prevent infinite
1385 is_direct_iface_type_helper(Unordered_set(const Type
*)*) const;
1387 // Get the backend representation for a type without looking in the
1388 // hash table for identical types.
1390 get_btype_without_hash(Gogo
*);
1392 // A backend type that may be a placeholder.
1393 struct Type_btype_entry
1396 bool is_placeholder
;
1399 // A mapping from Type to Btype*, used to ensure that the backend
1400 // representation of identical types is identical. This is only
1401 // used for unnamed types.
1402 typedef Unordered_map_hash(const Type
*, Type_btype_entry
,
1403 Type_hash_identical
, Type_identical
) Type_btypes
;
1405 static Type_btypes type_btypes
;
1407 // A list of builtin named types.
1408 static std::vector
<Named_type
*> named_builtin_types
;
1410 // A map from types that need a specific hash or equality function
1411 // to the hash or equality function.
1412 typedef Unordered_map_hash(const Type
*, Named_object
*, Type_hash_identical
,
1413 Type_identical
) Type_function
;
1415 static Type_function type_hash_functions_table
;
1416 static Type_function type_equal_functions_table
;
1418 // Cache for reusing existing pointer types; maps from pointed-to-type
1420 typedef Unordered_map(Type
*, Pointer_type
*) Pointer_type_table
;
1422 static Pointer_type_table pointer_types
;
1424 // List of placeholder pointer types.
1425 static std::vector
<Type
*> placeholder_pointers
;
1427 // The type classification.
1428 Type_classification classification_
;
1429 // The backend representation of the type, once it has been
1432 // The type descriptor for this type. This starts out as NULL and
1433 // is filled in as needed.
1434 Bvariable
* type_descriptor_var_
;
1435 // The GC symbol for this type. This starts out as NULL and
1436 // is filled in as needed.
1437 Bvariable
* gc_symbol_var_
;
1440 // Type hash table operations, treating aliases as identical to the
1441 // types that they alias.
1443 class Type_hash_identical
1447 operator()(const Type
* type
) const
1449 return type
->hash_for_method(NULL
,
1450 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
);
1454 class Type_identical
1458 operator()(const Type
* t1
, const Type
* t2
) const
1460 return Type::are_identical(t1
, t2
,
1461 Type::COMPARE_ERRORS
| Type::COMPARE_TAGS
,
1466 // An identifier with a type.
1468 class Typed_identifier
1471 Typed_identifier(const std::string
& name
, Type
* type
,
1473 : name_(name
), type_(type
), location_(location
), note_(NULL
)
1479 { return this->name_
; }
1484 { return this->type_
; }
1486 // Return the location where the name was seen. This is not always
1490 { return this->location_
; }
1492 // Set the type--sometimes we see the identifier before the type.
1494 set_type(Type
* type
)
1496 go_assert(this->type_
== NULL
|| type
->is_error_type());
1500 // Get the escape note.
1503 { return this->note_
; }
1505 // Set the escape note.
1507 set_note(const std::string
& note
)
1509 if (this->note_
!= NULL
)
1510 go_assert(*this->note_
== note
);
1512 this->note_
= new std::string(note
);
1520 // The location where the name was seen.
1522 // Escape note for this typed identifier. Used when importing and exporting
1527 // A list of Typed_identifiers.
1529 class Typed_identifier_list
1532 Typed_identifier_list()
1536 // Whether the list is empty.
1539 { return this->entries_
.empty(); }
1541 // Return the number of entries in the list.
1544 { return this->entries_
.size(); }
1546 // Add an entry to the end of the list.
1548 push_back(const Typed_identifier
& td
)
1549 { this->entries_
.push_back(td
); }
1551 // Remove an entry from the end of the list.
1554 { this->entries_
.pop_back(); }
1556 // Set the type of entry I to TYPE.
1558 set_type(size_t i
, Type
* type
)
1560 go_assert(i
< this->entries_
.size());
1561 this->entries_
[i
].set_type(type
);
1564 // Sort the entries by name.
1570 traverse(Traverse
*) const;
1572 // Return the first and last elements.
1575 { return this->entries_
.front(); }
1577 const Typed_identifier
&
1579 { return this->entries_
.front(); }
1583 { return this->entries_
.back(); }
1585 const Typed_identifier
&
1587 { return this->entries_
.back(); }
1591 { return this->entries_
.at(i
); }
1593 const Typed_identifier
&
1595 { return this->entries_
.at(i
); }
1598 set(size_t i
, const Typed_identifier
& t
)
1599 { this->entries_
.at(i
) = t
; }
1604 go_assert(c
<= this->entries_
.size());
1605 this->entries_
.resize(c
, Typed_identifier("", NULL
,
1606 Linemap::unknown_location()));
1611 { this->entries_
.reserve(c
); }
1615 typedef std::vector
<Typed_identifier
>::iterator iterator
;
1616 typedef std::vector
<Typed_identifier
>::const_iterator const_iterator
;
1620 { return this->entries_
.begin(); }
1624 { return this->entries_
.begin(); }
1628 { return this->entries_
.end(); }
1632 { return this->entries_
.end(); }
1634 // Return a copy of this list. This returns an independent copy of
1635 // the vector, but does not copy the types.
1636 Typed_identifier_list
*
1640 std::vector
<Typed_identifier
> entries_
;
1643 // A type used to indicate a parsing error. This exists to simplify
1644 // later error detection.
1646 class Error_type
: public Type
1655 do_compare_is_identity(Gogo
*)
1659 do_get_backend(Gogo
* gogo
);
1662 do_type_descriptor(Gogo
*, Named_type
*);
1665 do_reflection(Gogo
*, std::string
*) const;
1668 do_mangled_name(Gogo
*, std::string
*, bool*) const;
1673 class Void_type
: public Type
1682 do_compare_is_identity(Gogo
*)
1686 do_get_backend(Gogo
* gogo
);
1689 do_type_descriptor(Gogo
*, Named_type
*)
1690 { go_unreachable(); }
1693 do_reflection(Gogo
*, std::string
*) const
1697 do_mangled_name(Gogo
*, std::string
*, bool*) const;
1700 // The boolean type.
1702 class Boolean_type
: public Type
1706 : Type(TYPE_BOOLEAN
)
1711 do_compare_is_identity(Gogo
*)
1715 do_get_backend(Gogo
* gogo
);
1718 do_type_descriptor(Gogo
*, Named_type
* name
);
1720 // We should not be asked for the reflection string of a basic type.
1722 do_reflection(Gogo
*, std::string
* ret
) const
1723 { ret
->append("bool"); }
1726 do_mangled_name(Gogo
*, std::string
*, bool*) const;
1729 // The type of an integer.
1731 class Integer_type
: public Type
1734 // Create a new integer type.
1736 create_integer_type(const char* name
, bool is_unsigned
, int bits
,
1737 int runtime_type_kind
);
1739 // Look up an existing integer type.
1741 lookup_integer_type(const char* name
);
1743 // Create an abstract integer type.
1744 static Integer_type
*
1745 create_abstract_integer_type();
1747 // Create an abstract character type.
1748 static Integer_type
*
1749 create_abstract_character_type();
1751 // Create an alias to an integer type.
1753 create_integer_type_alias(const char* name
, Named_type
* real_type
);
1755 // Whether this is an abstract integer type.
1758 { return this->is_abstract_
; }
1760 // Whether this is an unsigned type.
1763 { return this->is_unsigned_
; }
1765 // The number of bits.
1768 { return this->bits_
; }
1770 // Whether this type is the same as T.
1772 is_identical(const Integer_type
* t
) const;
1774 // Whether this is the type "byte" or another name for "byte".
1777 { return this->is_byte_
; }
1779 // Mark this as the "byte" type.
1782 { this->is_byte_
= true; }
1784 // Whether this is the type "rune" or another name for "rune".
1787 { return this->is_rune_
; }
1789 // Mark this as the "rune" type.
1792 { this->is_rune_
= true; }
1796 do_compare_is_identity(Gogo
*)
1800 do_hash_for_method(Gogo
*, int) const;
1803 do_get_backend(Gogo
*);
1806 do_type_descriptor(Gogo
*, Named_type
*);
1809 do_reflection(Gogo
*, std::string
*) const;
1812 do_mangled_name(Gogo
*, std::string
*, bool*) const;
1815 Integer_type(bool is_abstract
, bool is_unsigned
, int bits
,
1816 int runtime_type_kind
)
1817 : Type(TYPE_INTEGER
),
1818 is_abstract_(is_abstract
), is_unsigned_(is_unsigned
), is_byte_(false),
1819 is_rune_(false), bits_(bits
), runtime_type_kind_(runtime_type_kind
)
1822 // Map names of integer types to the types themselves.
1823 typedef std::map
<std::string
, Named_type
*> Named_integer_types
;
1824 static Named_integer_types named_integer_types
;
1826 // True if this is an abstract type.
1828 // True if this is an unsigned type.
1830 // True if this is the byte type.
1832 // True if this is the rune type.
1834 // The number of bits.
1836 // The runtime type code used in the type descriptor for this type.
1837 int runtime_type_kind_
;
1840 // The type of a floating point number.
1842 class Float_type
: public Type
1845 // Create a new float type.
1847 create_float_type(const char* name
, int bits
, int runtime_type_kind
);
1849 // Look up an existing float type.
1851 lookup_float_type(const char* name
);
1853 // Create an abstract float type.
1855 create_abstract_float_type();
1857 // Whether this is an abstract float type.
1860 { return this->is_abstract_
; }
1862 // The number of bits.
1865 { return this->bits_
; }
1867 // Whether this type is the same as T.
1869 is_identical(const Float_type
* t
) const;
1873 do_compare_is_identity(Gogo
*)
1880 // Distinction between +0 and -0 requires a key update.
1882 do_needs_key_update()
1886 do_hash_for_method(Gogo
*, int) const;
1889 do_get_backend(Gogo
*);
1892 do_type_descriptor(Gogo
*, Named_type
*);
1895 do_reflection(Gogo
*, std::string
*) const;
1898 do_mangled_name(Gogo
*, std::string
*, bool*) const;
1901 Float_type(bool is_abstract
, int bits
, int runtime_type_kind
)
1903 is_abstract_(is_abstract
), bits_(bits
),
1904 runtime_type_kind_(runtime_type_kind
)
1907 // Map names of float types to the types themselves.
1908 typedef std::map
<std::string
, Named_type
*> Named_float_types
;
1909 static Named_float_types named_float_types
;
1911 // True if this is an abstract type.
1913 // The number of bits in the floating point value.
1915 // The runtime type code used in the type descriptor for this type.
1916 int runtime_type_kind_
;
1919 // The type of a complex number.
1921 class Complex_type
: public Type
1924 // Create a new complex type.
1926 create_complex_type(const char* name
, int bits
, int runtime_type_kind
);
1928 // Look up an existing complex type.
1930 lookup_complex_type(const char* name
);
1932 // Create an abstract complex type.
1933 static Complex_type
*
1934 create_abstract_complex_type();
1936 // Whether this is an abstract complex type.
1939 { return this->is_abstract_
; }
1941 // The number of bits: 64 or 128.
1943 { return this->bits_
; }
1945 // Whether this type is the same as T.
1947 is_identical(const Complex_type
* t
) const;
1951 do_compare_is_identity(Gogo
*)
1958 // Distinction between +0 and -0 requires a key update.
1960 do_needs_key_update()
1964 do_hash_for_method(Gogo
*, int) const;
1967 do_get_backend(Gogo
*);
1970 do_type_descriptor(Gogo
*, Named_type
*);
1973 do_reflection(Gogo
*, std::string
*) const;
1976 do_mangled_name(Gogo
*, std::string
*, bool*) const;
1979 Complex_type(bool is_abstract
, int bits
, int runtime_type_kind
)
1980 : Type(TYPE_COMPLEX
),
1981 is_abstract_(is_abstract
), bits_(bits
),
1982 runtime_type_kind_(runtime_type_kind
)
1985 // Map names of complex types to the types themselves.
1986 typedef std::map
<std::string
, Named_type
*> Named_complex_types
;
1987 static Named_complex_types named_complex_types
;
1989 // True if this is an abstract type.
1991 // The number of bits in the complex value--64 or 128.
1993 // The runtime type code used in the type descriptor for this type.
1994 int runtime_type_kind_
;
1997 // The type of a string.
1999 class String_type
: public Type
2008 do_has_pointer() const
2012 do_compare_is_identity(Gogo
*)
2015 // New string might have a smaller backing store.
2017 do_needs_key_update()
2021 do_get_backend(Gogo
*);
2024 do_type_descriptor(Gogo
*, Named_type
*);
2027 do_reflection(Gogo
*, std::string
*) const;
2030 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2033 // The named string type.
2034 static Named_type
* string_type_
;
2037 // The type of a function.
2039 class Function_type
: public Type
2042 Function_type(Typed_identifier
* receiver
, Typed_identifier_list
* parameters
,
2043 Typed_identifier_list
* results
, Location location
)
2044 : Type(TYPE_FUNCTION
),
2045 receiver_(receiver
), parameters_(parameters
), results_(results
),
2046 location_(location
), is_varargs_(false), is_builtin_(false),
2047 fnbtype_(NULL
), is_tagged_(false)
2050 // Get the receiver.
2051 const Typed_identifier
*
2053 { return this->receiver_
; }
2055 // Add an escape note for the receiver.
2057 add_receiver_note(int encoding
)
2058 { this->receiver_
->set_note(Escape_note::make_tag(encoding
)); }
2060 // Get the return names and types.
2061 const Typed_identifier_list
*
2063 { return this->results_
; }
2065 // Get the parameter names and types.
2066 const Typed_identifier_list
*
2068 { return this->parameters_
; }
2070 // Add an escape note for the ith parameter.
2072 add_parameter_note(int index
, int encoding
)
2073 { this->parameters_
->at(index
).set_note(Escape_note::make_tag(encoding
)); }
2075 // Whether this function has been tagged during escape analysis.
2078 { return this->is_tagged_
; }
2080 // Mark this function as tagged after analyzing its escape.
2083 { this->is_tagged_
= true; }
2085 // Whether this is a varargs function.
2088 { return this->is_varargs_
; }
2090 // Whether this is a builtin function.
2093 { return this->is_builtin_
; }
2095 // The location where this type was defined.
2098 { return this->location_
; }
2100 // Return whether this is a method type.
2103 { return this->receiver_
!= NULL
; }
2105 // Whether T is a valid redeclaration of this type. This is called
2106 // when a function is declared more than once.
2108 is_valid_redeclaration(const Function_type
* t
, std::string
*) const;
2110 // Whether this type is the same as T.
2112 is_identical(const Function_type
* t
, bool ignore_receiver
, int flags
,
2113 std::string
*) const;
2115 // Record that this is a varargs function.
2118 { this->is_varargs_
= true; }
2120 // Record that this is a builtin function.
2123 { this->is_builtin_
= true; }
2125 // Import a function type.
2126 static Function_type
*
2129 // Return a copy of this type without a receiver. This is only
2130 // valid for a method type.
2132 copy_without_receiver() const;
2134 // Return a copy of this type with a receiver. This is used when an
2135 // interface method is attached to a named or struct type.
2137 copy_with_receiver(Type
*) const;
2139 // Return a copy of this type with the receiver treated as the first
2140 // parameter. If WANT_POINTER_RECEIVER is true, the receiver is
2141 // forced to be a pointer.
2143 copy_with_receiver_as_param(bool want_pointer_receiver
) const;
2145 // Return a copy of this type ignoring any receiver and using dummy
2146 // names for all parameters. This is used for thunks for method
2149 copy_with_names() const;
2152 make_function_type_descriptor_type();
2154 // Return the backend representation of this function type. This is used
2155 // as the real type of a backend function declaration or defintion.
2157 get_backend_fntype(Gogo
*);
2159 // Return whether this is a Backend_function_type.
2161 is_backend_function_type() const
2166 do_traverse(Traverse
*);
2168 // A function descriptor may be allocated on the heap.
2170 do_has_pointer() const
2174 do_compare_is_identity(Gogo
*)
2178 do_hash_for_method(Gogo
*, int) const;
2181 do_get_backend(Gogo
*);
2184 do_type_descriptor(Gogo
*, Named_type
*);
2187 do_reflection(Gogo
*, std::string
*) const;
2190 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2193 do_export(Export
*) const;
2197 type_descriptor_params(Type
*, const Typed_identifier
*,
2198 const Typed_identifier_list
*);
2200 // A mapping from a list of result types to a backend struct type.
2205 operator()(const Typed_identifier_list
*) const;
2212 operator()(const Typed_identifier_list
*,
2213 const Typed_identifier_list
*) const;
2216 typedef Unordered_map_hash(Typed_identifier_list
*, Btype
*,
2217 Results_hash
, Results_equal
) Results_structs
;
2219 static Results_structs results_structs
;
2221 // The receiver name and type. This will be NULL for a normal
2222 // function, non-NULL for a method.
2223 Typed_identifier
* receiver_
;
2224 // The parameter names and types.
2225 Typed_identifier_list
* parameters_
;
2226 // The result names and types. This will be NULL if no result was
2228 Typed_identifier_list
* results_
;
2229 // The location where this type was defined. This exists solely to
2230 // give a location for the fields of the struct if this function
2231 // returns multiple values.
2233 // Whether this function takes a variable number of arguments.
2235 // Whether this is a special builtin function which can not simply
2236 // be called. This is used for len, cap, etc.
2238 // The backend representation of this type for backend function
2239 // declarations and definitions.
2241 // Whether this function has been analyzed by escape analysis. If this is
2242 // TRUE, this function type's parameters contain a summary of the analysis.
2246 // The type of a function's backend representation.
2248 class Backend_function_type
: public Function_type
2251 Backend_function_type(Typed_identifier
* receiver
,
2252 Typed_identifier_list
* parameters
,
2253 Typed_identifier_list
* results
, Location location
)
2254 : Function_type(receiver
, parameters
, results
, location
)
2257 // Return whether this is a Backend_function_type. This overrides
2258 // Function_type::is_backend_function_type.
2260 is_backend_function_type() const
2265 do_get_backend(Gogo
* gogo
)
2266 { return this->get_backend_fntype(gogo
); }
2269 // The type of a pointer.
2271 class Pointer_type
: public Type
2274 Pointer_type(Type
* to_type
)
2275 : Type(TYPE_POINTER
),
2281 { return this->to_type_
; }
2283 // Import a pointer type.
2284 static Pointer_type
*
2288 make_pointer_type_descriptor_type();
2292 do_traverse(Traverse
*);
2296 { return this->to_type_
->verify(); }
2299 do_has_pointer() const
2303 do_compare_is_identity(Gogo
*)
2307 do_hash_for_method(Gogo
*, int) const;
2310 do_get_backend(Gogo
*);
2313 do_type_descriptor(Gogo
*, Named_type
*);
2316 do_reflection(Gogo
*, std::string
*) const;
2319 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2322 do_export(Export
*) const;
2325 // The type to which this type points.
2329 // The nil type. We use a special type for nil because it is not the
2330 // same as any other type. In C term nil has type void*, but there is
2331 // no such type in Go.
2333 class Nil_type
: public Type
2342 do_compare_is_identity(Gogo
*)
2346 do_get_backend(Gogo
* gogo
);
2349 do_type_descriptor(Gogo
*, Named_type
*)
2350 { go_unreachable(); }
2353 do_reflection(Gogo
*, std::string
*) const
2354 { go_unreachable(); }
2357 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2360 // The type of a field in a struct.
2365 explicit Struct_field(const Typed_identifier
& typed_identifier
)
2366 : typed_identifier_(typed_identifier
), tag_(NULL
), is_imported_(false)
2373 // Return whether this struct field is named NAME.
2375 is_field_name(const std::string
& name
) const;
2377 // Return whether this struct field is an unexported field named NAME.
2379 is_unexported_field_name(Gogo
*, const std::string
& name
) const;
2381 // Return whether this struct field is an embedded built-in type.
2383 is_embedded_builtin(Gogo
*) const;
2388 { return this->typed_identifier_
.type(); }
2390 // The field location.
2393 { return this->typed_identifier_
.location(); }
2395 // Whether the field has a tag.
2398 { return this->tag_
!= NULL
; }
2404 go_assert(this->tag_
!= NULL
);
2408 // Whether this is an anonymous field.
2410 is_anonymous() const
2411 { return this->typed_identifier_
.name().empty(); }
2413 // Set the tag. FIXME: This is never freed.
2415 set_tag(const std::string
& tag
)
2416 { this->tag_
= new std::string(tag
); }
2418 // Record that this field is defined in an imported struct.
2421 { this->is_imported_
= true; }
2423 // Set the type. This is only used in error cases.
2425 set_type(Type
* type
)
2426 { this->typed_identifier_
.set_type(type
); }
2429 // The field name, type, and location.
2430 Typed_identifier typed_identifier_
;
2431 // The field tag. This is NULL if the field has no tag.
2433 // Whether this field is defined in an imported struct.
2437 // A list of struct fields.
2439 class Struct_field_list
2446 // Whether the list is empty.
2449 { return this->entries_
.empty(); }
2451 // Return the number of entries.
2454 { return this->entries_
.size(); }
2456 // Add an entry to the end of the list.
2458 push_back(const Struct_field
& sf
)
2459 { this->entries_
.push_back(sf
); }
2461 // Index into the list.
2464 { return this->entries_
.at(i
); }
2466 // Last entry in list.
2469 { return this->entries_
.back(); }
2473 typedef std::vector
<Struct_field
>::iterator iterator
;
2474 typedef std::vector
<Struct_field
>::const_iterator const_iterator
;
2478 { return this->entries_
.begin(); }
2482 { return this->entries_
.begin(); }
2486 { return this->entries_
.end(); }
2490 { return this->entries_
.end(); }
2493 std::vector
<Struct_field
> entries_
;
2496 // The type of a struct.
2498 class Struct_type
: public Type
2501 Struct_type(Struct_field_list
* fields
, Location location
)
2502 : Type(TYPE_STRUCT
),
2503 fields_(fields
), location_(location
), all_methods_(NULL
),
2504 is_struct_incomparable_(false), has_padding_(false)
2507 // Return the field NAME. This only looks at local fields, not at
2508 // embedded types. If the field is found, and PINDEX is not NULL,
2509 // this sets *PINDEX to the field index. If the field is not found,
2510 // this returns NULL.
2512 find_local_field(const std::string
& name
, unsigned int *pindex
) const;
2514 // Return the field number INDEX.
2516 field(unsigned int index
) const
2517 { return &this->fields_
->at(index
); }
2519 // Get the struct fields.
2520 const Struct_field_list
*
2522 { return this->fields_
; }
2524 // Return the number of fields.
2527 { return this->fields_
->size(); }
2529 // Location of struct definition.
2532 { return this->location_
; }
2534 // Push a new field onto the end of the struct. This is used when
2535 // building a closure variable.
2537 push_field(const Struct_field
& sf
)
2538 { this->fields_
->push_back(sf
); }
2540 // Return an expression referring to field NAME in STRUCT_EXPR, or
2541 // NULL if there is no field with that name.
2542 Field_reference_expression
*
2543 field_reference(Expression
* struct_expr
, const std::string
& name
,
2546 // Return the total number of fields, including embedded fields.
2547 // This is the number of values that can appear in a conversion to
2550 total_field_count() const;
2552 // Whether this type is identical with T.
2554 is_identical(const Struct_type
* t
, int) const;
2556 // Return whether NAME is a local field which is not exported. This
2557 // is only used for better error reporting.
2559 is_unexported_local_field(Gogo
*, const std::string
& name
) const;
2561 // If this is an unnamed struct, build the complete list of methods,
2562 // including those from anonymous fields, and build methods stubs if
2565 finalize_methods(Gogo
*);
2567 // Return whether this type has any methods. This should only be
2568 // called after the finalize_methods pass.
2570 has_any_methods() const
2571 { return this->all_methods_
!= NULL
; }
2573 // Return the methods for this type. This should only be called
2574 // after the finalize_methods pass.
2577 { return this->all_methods_
; }
2579 // Return the method to use for NAME. This returns NULL if there is
2580 // no such method or if the method is ambiguous. When it returns
2581 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2583 method_function(const std::string
& name
, bool* is_ambiguous
) const;
2585 // Return a pointer to the interface method table for this type for
2586 // the interface INTERFACE. If IS_POINTER is true, set the type
2587 // descriptor to a pointer to this type, otherwise set it to this
2590 interface_method_table(Interface_type
* interface
, bool is_pointer
);
2592 // Traverse just the field types of a struct type.
2594 traverse_field_types(Traverse
* traverse
)
2595 { return this->do_traverse(traverse
); }
2597 // If the offset of field INDEX in the backend implementation can be
2598 // determined, set *POFFSET to the offset in bytes and return true.
2599 // Otherwise, return false.
2601 backend_field_offset(Gogo
*, unsigned int index
, int64_t* poffset
);
2603 // Finish the backend representation of all the fields.
2605 finish_backend_fields(Gogo
*);
2607 // Import a struct type.
2612 make_struct_type_descriptor_type();
2614 // Return whether this is a generated struct that is not comparable.
2616 is_struct_incomparable() const
2617 { return this->is_struct_incomparable_
; }
2619 // Record that this is a generated struct that is not comparable.
2621 set_is_struct_incomparable()
2622 { this->is_struct_incomparable_
= true; }
2624 // Return whether this struct's backend type has padding, due to
2625 // trailing zero-sized field.
2628 { return this->has_padding_
; }
2630 // Record that this struct's backend type has padding.
2633 { this->has_padding_
= true; }
2635 // Write the hash function for this type.
2637 write_hash_function(Gogo
*, Function_type
*);
2639 // Write the equality function for this type.
2641 write_equal_function(Gogo
*, Named_type
*);
2643 // Whether we can write this type to a C header file, to implement
2646 can_write_to_c_header(std::vector
<const Named_object
*>*,
2647 std::vector
<const Named_object
*>*) const;
2649 // Write this type to a C header file, to implement -fgo-c-header.
2651 write_to_c_header(std::ostream
&) const;
2655 do_traverse(Traverse
*);
2661 do_has_pointer() const;
2664 do_compare_is_identity(Gogo
*);
2670 do_needs_key_update();
2673 do_hash_might_panic();
2679 do_hash_for_method(Gogo
*, int) const;
2682 do_get_backend(Gogo
*);
2685 do_type_descriptor(Gogo
*, Named_type
*);
2688 do_reflection(Gogo
*, std::string
*) const;
2691 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2694 do_export(Export
*) const;
2698 can_write_type_to_c_header(const Type
*,
2699 std::vector
<const Named_object
*>*,
2700 std::vector
<const Named_object
*>*) const;
2703 write_field_to_c_header(std::ostream
&, const std::string
&, const Type
*) const;
2705 // Used to merge method sets of identical unnamed structs.
2706 typedef Unordered_map_hash(Struct_type
*, Struct_type
*, Type_hash_identical
,
2707 Type_identical
) Identical_structs
;
2709 static Identical_structs identical_structs
;
2711 // Used to manage method tables for identical unnamed structs.
2712 typedef std::pair
<Interface_method_tables
*, Interface_method_tables
*>
2713 Struct_method_table_pair
;
2715 typedef Unordered_map_hash(Struct_type
*, Struct_method_table_pair
*,
2716 Type_hash_identical
, Type_identical
)
2717 Struct_method_tables
;
2719 static Struct_method_tables struct_method_tables
;
2721 // Used to avoid infinite loops in field_reference_depth.
2722 struct Saw_named_type
2724 Saw_named_type
* next
;
2728 Field_reference_expression
*
2729 field_reference_depth(Expression
* struct_expr
, const std::string
& name
,
2730 Location
, Saw_named_type
*,
2731 unsigned int* depth
) const;
2733 // The fields of the struct.
2734 Struct_field_list
* fields_
;
2735 // The place where the struct was declared.
2737 // If this struct is unnamed, a list of methods.
2738 Methods
* all_methods_
;
2739 // True if this is a generated struct that is not considered to be
2741 bool is_struct_incomparable_
;
2742 // True if this struct's backend type has padding, due to trailing
2743 // zero-sized field.
2747 // The type of an array.
2749 class Array_type
: public Type
2752 Array_type(Type
* element_type
, Expression
* length
)
2754 element_type_(element_type
), length_(length
), blength_(NULL
),
2755 issued_length_error_(false), is_array_incomparable_(false)
2758 // Return the element type.
2760 element_type() const
2761 { return this->element_type_
; }
2763 // Return the length. This will return NULL for a slice.
2766 { return this->length_
; }
2768 // Store the length as an int64_t into *PLEN. Return false if the
2769 // length can not be determined. This will assert if called for a
2772 int_length(int64_t* plen
) const;
2774 // Whether this type is identical with T.
2776 is_identical(const Array_type
* t
, int) const;
2778 // Return an expression for the pointer to the values in an array.
2780 get_value_pointer(Gogo
*, Expression
* array
, bool is_lvalue
) const;
2782 // Return an expression for the length of an array with this type.
2784 get_length(Gogo
*, Expression
* array
) const;
2786 // Return an expression for the capacity of an array with this type.
2788 get_capacity(Gogo
*, Expression
* array
) const;
2790 // Import an array type.
2794 // Return the backend representation of the element type.
2796 get_backend_element(Gogo
*, bool use_placeholder
);
2798 // Return the backend representation of the length.
2800 get_backend_length(Gogo
*);
2802 // Finish the backend representation of the element type.
2804 finish_backend_element(Gogo
*);
2807 make_array_type_descriptor_type();
2810 make_slice_type_descriptor_type();
2812 // Return whether this is a generated array that is not comparable.
2814 is_array_incomparable() const
2815 { return this->is_array_incomparable_
; }
2817 // Record that this is a generated array that is not comparable.
2819 set_is_array_incomparable()
2820 { this->is_array_incomparable_
= true; }
2822 // Write the hash function for this type.
2824 write_hash_function(Gogo
*, Function_type
*);
2826 // Write the equality function for this type.
2828 write_equal_function(Gogo
*, Named_type
*);
2832 do_traverse(Traverse
* traverse
);
2838 do_has_pointer() const;
2841 do_compare_is_identity(Gogo
*);
2846 return this->length_
!= NULL
&& this->element_type_
->is_reflexive();
2850 do_needs_key_update()
2851 { return this->element_type_
->needs_key_update(); }
2854 do_hash_might_panic()
2855 { return this->length_
!= NULL
&& this->element_type_
->hash_might_panic(); }
2859 { return this->length_
== NULL
|| this->element_type_
->in_heap(); }
2862 do_hash_for_method(Gogo
*, int) const;
2865 do_get_backend(Gogo
*);
2868 do_type_descriptor(Gogo
*, Named_type
*);
2871 do_reflection(Gogo
*, std::string
*) const;
2874 do_mangled_name(Gogo
*, std::string
*, bool*) const;
2877 do_export(Export
*) const;
2884 array_type_descriptor(Gogo
*, Named_type
*);
2887 slice_type_descriptor(Gogo
*, Named_type
*);
2889 // The type of elements of the array.
2890 Type
* element_type_
;
2891 // The number of elements. This may be NULL.
2892 Expression
* length_
;
2893 // The backend representation of the length.
2894 // We only want to compute this once.
2895 Bexpression
* blength_
;
2896 // Whether or not an invalid length error has been issued for this type,
2897 // to avoid knock-on errors.
2898 mutable bool issued_length_error_
;
2899 // True if this is a generated array that is not considered to be
2901 bool is_array_incomparable_
;
2904 // The type of a map.
2906 class Map_type
: public Type
2909 Map_type(Type
* key_type
, Type
* val_type
, Location location
)
2911 key_type_(key_type
), val_type_(val_type
), hmap_type_(NULL
),
2912 bucket_type_(NULL
), hiter_type_(NULL
), location_(location
)
2915 // Return the key type.
2918 { return this->key_type_
; }
2920 // Return the value type.
2923 { return this->val_type_
; }
2925 // Return the type used for an iteration over this map.
2929 // If this map requires the "fat" functions, returns the pointer to
2930 // pass as the zero value to those functions. Otherwise, in the
2931 // normal case, returns NULL.
2933 fat_zero_value(Gogo
*);
2935 // Map algorithm to use for this map type. We may use specialized
2936 // fast map routines for certain key types.
2941 // 32-bit pointer key.
2945 // 64-bit pointer key.
2956 // Return whether VAR is the map zero value.
2958 is_zero_value(Variable
* var
);
2960 // Return the backend representation of the map zero value.
2962 backend_zero_value(Gogo
*);
2964 // Whether this type is identical with T.
2966 is_identical(const Map_type
* t
, int) const;
2968 // Import a map type.
2973 make_map_type_descriptor_type();
2975 // This must be in sync with libgo/go/runtime/map.go.
2976 static const int bucket_size
= 8;
2980 do_traverse(Traverse
*);
2986 do_has_pointer() const
2990 do_compare_is_identity(Gogo
*)
2996 return this->key_type_
->is_reflexive() && this->val_type_
->is_reflexive();
3000 do_hash_for_method(Gogo
*, int) const;
3003 do_get_backend(Gogo
*);
3006 do_type_descriptor(Gogo
*, Named_type
*);
3009 do_reflection(Gogo
*, std::string
*) const;
3012 do_mangled_name(Gogo
*, std::string
*, bool*) const;
3015 do_export(Export
*) const;
3018 // These must be in sync with libgo/go/runtime/map.go.
3019 static const int max_key_size
= 128;
3020 static const int max_val_size
= 128;
3021 static const int max_zero_size
= 1024;
3023 // Maps with value types larger than max_zero_size require passing a
3024 // zero value pointer to the map functions.
3026 // The zero value variable.
3027 static Named_object
* zero_value
;
3029 // The current size of the zero value.
3030 static int64_t zero_value_size
;
3032 // The current alignment of the zero value.
3033 static int64_t zero_value_align
;
3036 bucket_type(Gogo
*, int64_t, int64_t);
3045 // The hashmap type. At run time a map is represented as a pointer
3048 // The bucket type, the type used to hold keys and values at run time.
3050 // The iterator type.
3052 // Where the type was defined.
3056 // The type of a channel.
3058 class Channel_type
: public Type
3061 Channel_type(bool may_send
, bool may_receive
, Type
* element_type
)
3062 : Type(TYPE_CHANNEL
),
3063 may_send_(may_send
), may_receive_(may_receive
),
3064 element_type_(element_type
)
3065 { go_assert(may_send
|| may_receive
); }
3067 // Whether this channel can send data.
3070 { return this->may_send_
; }
3072 // Whether this channel can receive data.
3075 { return this->may_receive_
; }
3077 // The type of the values that may be sent on this channel. This is
3078 // NULL if any type may be sent.
3080 element_type() const
3081 { return this->element_type_
; }
3083 // Whether this type is identical with T.
3085 is_identical(const Channel_type
* t
, int) const;
3087 // Import a channel type.
3088 static Channel_type
*
3092 make_chan_type_descriptor_type();
3099 do_traverse(Traverse
* traverse
)
3100 { return Type::traverse(this->element_type_
, traverse
); }
3106 do_has_pointer() const
3110 do_compare_is_identity(Gogo
*)
3114 do_hash_for_method(Gogo
*, int) const;
3117 do_get_backend(Gogo
*);
3120 do_type_descriptor(Gogo
*, Named_type
*);
3123 do_reflection(Gogo
*, std::string
*) const;
3126 do_mangled_name(Gogo
*, std::string
*, bool*) const;
3129 do_export(Export
*) const;
3132 // Whether this channel can send data.
3134 // Whether this channel can receive data.
3136 // The types of elements which may be sent on this channel. If this
3137 // is NULL, it means that any type may be sent.
3138 Type
* element_type_
;
3141 // An interface type.
3143 class Interface_type
: public Type
3146 Interface_type(Typed_identifier_list
* methods
, Location location
)
3147 : Type(TYPE_INTERFACE
),
3148 parse_methods_(methods
), all_methods_(NULL
), location_(location
),
3149 package_(NULL
), interface_btype_(NULL
), bmethods_(NULL
),
3150 assume_identical_(NULL
), methods_are_finalized_(false),
3151 bmethods_is_placeholder_(false), seen_(false)
3152 { go_assert(methods
== NULL
|| !methods
->empty()); }
3154 // The location where the interface type was defined.
3157 { return this->location_
; }
3159 // The package where the interface type was defined. Returns NULL
3160 // for the package currently being compiled.
3163 { return this->package_
; }
3165 // Return whether this is an empty interface.
3169 go_assert(this->methods_are_finalized_
);
3170 return this->all_methods_
== NULL
;
3173 // Return the list of locally defined methods. This will return NULL
3174 // for an empty interface. Embedded interfaces will appear in this
3175 // list as an entry with no name.
3176 const Typed_identifier_list
*
3177 local_methods() const
3178 { return this->parse_methods_
; }
3180 // Return the list of all methods. This will return NULL for an
3182 const Typed_identifier_list
*
3185 // Return the number of methods.
3187 method_count() const;
3189 // Return the method NAME, or NULL.
3190 const Typed_identifier
*
3191 find_method(const std::string
& name
) const;
3193 // Return the zero-based index of method NAME.
3195 method_index(const std::string
& name
) const;
3197 // Finalize the methods. This sets all_methods_. This handles
3198 // interface inheritance.
3202 // Return true if T implements this interface. If this returns
3203 // false, and REASON is not NULL, it sets *REASON to the reason that
3206 implements_interface(const Type
* t
, std::string
* reason
) const;
3208 // Whether this type is identical with T. REASON is as in
3209 // implements_interface.
3211 is_identical(const Interface_type
* t
, int) const;
3213 // Whether we can assign T to this type. is_identical is known to
3216 is_compatible_for_assign(const Interface_type
*, std::string
* reason
) const;
3218 // Return whether NAME is a method which is not exported. This is
3219 // only used for better error reporting.
3221 is_unexported_method(Gogo
*, const std::string
& name
) const;
3223 // Import an interface type.
3224 static Interface_type
*
3227 // Make a struct for an empty interface type.
3229 get_backend_empty_interface_type(Gogo
*);
3231 // Get a pointer to the backend representation of the method table.
3233 get_backend_methods(Gogo
*);
3235 // Return a placeholder for the backend representation of the
3236 // pointer to the method table.
3238 get_backend_methods_placeholder(Gogo
*);
3240 // Finish the backend representation of the method types.
3242 finish_backend_methods(Gogo
*);
3245 make_interface_type_descriptor_type();
3247 // Return whether methods are finalized for this interface.
3249 methods_are_finalized() const
3250 { return this->methods_are_finalized_
; }
3252 // Sort embedded interfaces by name. Needed when we are preparing
3253 // to emit types into the export data.
3257 if (parse_methods_
!= NULL
)
3258 parse_methods_
->sort_by_name();
3263 do_traverse(Traverse
*);
3266 do_has_pointer() const
3270 do_compare_is_identity(Gogo
*)
3273 // Not reflexive if it contains a float.
3278 // Distinction between +0 and -0 requires a key update if it
3279 // contains a float.
3281 do_needs_key_update()
3284 // Hashing an unhashable type stored in an interface might panic.
3286 do_hash_might_panic()
3290 do_hash_for_method(Gogo
*, int) const;
3293 do_get_backend(Gogo
*);
3296 do_type_descriptor(Gogo
*, Named_type
*);
3299 do_reflection(Gogo
*, std::string
*) const;
3302 do_mangled_name(Gogo
*, std::string
*, bool*) const;
3305 do_export(Export
*) const;
3308 // This type guards against infinite recursion when comparing
3309 // interface types. We keep a list of interface types assumed to be
3310 // identical during comparison. We just keep the list on the stack.
3311 // This permits us to compare cases like
3312 // type I1 interface { F() interface{I1} }
3313 // type I2 interface { F() interface{I2} }
3314 struct Assume_identical
3316 Assume_identical
* next
;
3317 const Interface_type
* t1
;
3318 const Interface_type
* t2
;
3322 assume_identical(const Interface_type
*, const Interface_type
*) const;
3324 struct Bmethods_map_entry
3327 bool is_placeholder
;
3330 // A mapping from Interface_type to the backend type of its bmethods_,
3331 // used to ensure that the backend representation of identical types
3333 typedef Unordered_map_hash(const Interface_type
*, Bmethods_map_entry
,
3334 Type_hash_identical
, Type_identical
) Bmethods_map
;
3336 static Bmethods_map bmethods_map
;
3338 // The list of methods associated with the interface from the
3339 // parser. This will be NULL for the empty interface. This may
3340 // include unnamed interface types.
3341 Typed_identifier_list
* parse_methods_
;
3342 // The list of all methods associated with the interface. This
3343 // expands any interface types listed in methods_. It is set by
3344 // finalize_methods. This will be NULL for the empty interface.
3345 Typed_identifier_list
* all_methods_
;
3346 // The location where the interface was defined.
3348 // The package where the interface was defined. This is NULL for
3349 // the package being compiled.
3351 // The backend representation of this type during backend conversion.
3352 Btype
* interface_btype_
;
3353 // The backend representation of the pointer to the method table.
3355 // A list of interface types assumed to be identical during
3356 // interface comparison.
3357 mutable Assume_identical
* assume_identical_
;
3358 // Whether the methods have been finalized.
3359 bool methods_are_finalized_
;
3360 // Whether the bmethods_ field is a placeholder.
3361 bool bmethods_is_placeholder_
;
3362 // Used to avoid endless recursion in do_mangled_name.
3366 // The value we keep for a named type. This lets us get the right
3367 // name when we convert to backend. Note that we don't actually keep
3368 // the name here; the name is in the Named_object which points to
3369 // this. This object exists to hold a unique backend representation for
3372 class Named_type
: public Type
3375 Named_type(Named_object
* named_object
, Type
* type
, Location location
)
3377 named_object_(named_object
), in_function_(NULL
), in_function_index_(0),
3378 type_(type
), local_methods_(NULL
), all_methods_(NULL
),
3379 interface_method_tables_(NULL
), pointer_interface_method_tables_(NULL
),
3380 location_(location
), named_btype_(NULL
), dependencies_(),
3381 is_alias_(false), is_visible_(true), is_error_(false), in_heap_(true),
3382 is_placeholder_(false), is_converted_(false), is_verified_(false),
3383 seen_(false), seen_in_compare_is_identity_(false),
3384 seen_in_get_backend_(false), seen_alias_(false)
3387 // Return the associated Named_object. This holds the actual name.
3390 { return this->named_object_
; }
3393 named_object() const
3394 { return this->named_object_
; }
3396 // Set the Named_object. This is used when we see a type
3397 // declaration followed by a type.
3399 set_named_object(Named_object
* no
)
3400 { this->named_object_
= no
; }
3402 // Whether this is an alias (type T1 = T2) rather than an ordinary
3403 // named type (type T1 T2).
3406 { return this->is_alias_
; }
3408 // Record that this type is an alias.
3411 { this->is_alias_
= true; }
3413 // Mark this type as not permitted in the heap.
3416 { this->in_heap_
= false; }
3418 // Return the function in which this type is defined. This will
3419 // return NULL for a type defined in global scope.
3421 in_function(unsigned int *pindex
) const
3423 *pindex
= this->in_function_index_
;
3424 return this->in_function_
;
3427 // Set the function in which this type is defined.
3429 set_in_function(Named_object
* f
, unsigned int index
)
3431 this->in_function_
= f
;
3432 this->in_function_index_
= index
;
3435 // Return the name of the type.
3439 // Return the name of the type for an error message. The difference
3440 // is that if the type is defined in a different package, this will
3441 // return PACKAGE.NAME.
3443 message_name() const;
3445 // Return the underlying type.
3448 { return this->type_
; }
3452 { return this->type_
; }
3454 // Return the location.
3457 { return this->location_
; }
3459 // Whether this type is visible. This only matters when parsing.
3462 { return this->is_visible_
; }
3464 // Mark this type as visible.
3467 { this->is_visible_
= true; }
3469 // Mark this type as invisible.
3472 { this->is_visible_
= false; }
3474 // Whether this is a builtin type.
3477 { return Linemap::is_predeclared_location(this->location_
); }
3479 // Whether this named type is valid. A recursive named type is invalid.
3482 { return !this->is_error_
; }
3484 // Return the base type for this type.
3491 // Return whether this is an error type.
3493 is_named_error_type() const;
3495 // Return whether this type is comparable. If REASON is not NULL,
3496 // set *REASON when returning false.
3498 named_type_is_comparable(std::string
* reason
) const;
3500 // Add a method to this type.
3502 add_method(const std::string
& name
, Function
*);
3504 // Add a method declaration to this type.
3506 add_method_declaration(const std::string
& name
, Package
* package
,
3507 Function_type
* type
, Location location
);
3509 // Add an existing method--one defined before the type itself was
3510 // defined--to a type.
3512 add_existing_method(Named_object
*);
3514 // Look up a local method.
3516 find_local_method(const std::string
& name
) const;
3518 // Return the list of local methods.
3520 local_methods() const;
3522 // Build the complete list of methods, including those from
3523 // anonymous fields, and build method stubs if needed.
3525 finalize_methods(Gogo
*);
3527 // Return whether this type has any methods. This should only be
3528 // called after the finalize_methods pass.
3530 has_any_methods() const;
3532 // Return the methods for this type. This should only be called
3533 // after the finalized_methods pass.
3537 // Return the method to use for NAME. This returns NULL if there is
3538 // no such method or if the method is ambiguous. When it returns
3539 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
3541 method_function(const std::string
& name
, bool *is_ambiguous
) const;
3543 // Return whether NAME is a known field or method which is not
3544 // exported. This is only used for better error reporting.
3546 is_unexported_local_method(Gogo
*, const std::string
& name
) const;
3548 // Return a pointer to the interface method table for this type for
3549 // the interface INTERFACE. If IS_POINTER is true, set the type
3550 // descriptor to a pointer to this type, otherwise set it to this
3553 interface_method_table(Interface_type
* interface
, bool is_pointer
);
3555 // Note that a type must be converted to the backend representation
3556 // before we convert this type.
3558 add_dependency(Named_type
* nt
)
3559 { this->dependencies_
.push_back(nt
); }
3561 // Return true if the size and alignment of the backend
3562 // representation of this type is known. This is always true after
3563 // types have been converted, but may be false beforehand.
3565 is_named_backend_type_size_known() const
3566 { return this->named_btype_
!= NULL
&& !this->is_placeholder_
; }
3568 // Add to the reflection string as for Type::append_reflection, but
3569 // if USE_ALIAS use the alias name rather than the alias target.
3571 append_reflection_type_name(Gogo
*, bool use_alias
, std::string
*) const;
3573 // Append the symbol type name as for Type::append_mangled_name,
3574 // but if USE_ALIAS use the alias name rather than the alias target.
3576 append_symbol_type_name(Gogo
*, bool use_alias
, std::string
*,
3577 bool* is_non_identifier
) const;
3579 // Import a named type.
3581 import_named_type(Import
*, Named_type
**);
3583 // Initial conversion to backend representation.
3589 do_traverse(Traverse
* traverse
)
3590 { return Type::traverse(this->type_
, traverse
); }
3596 do_has_pointer() const;
3599 do_compare_is_identity(Gogo
*);
3605 do_needs_key_update();
3609 { return this->in_heap_
&& this->type_
->in_heap(); }
3612 do_hash_for_method(Gogo
*, int) const;
3615 do_get_backend(Gogo
*);
3618 do_type_descriptor(Gogo
*, Named_type
*);
3621 do_reflection(Gogo
*, std::string
*) const;
3624 do_mangled_name(Gogo
*, std::string
*, bool*) const;
3627 do_export(Export
*) const;
3630 // Create the placeholder during conversion.
3632 create_placeholder(Gogo
*);
3634 // A pointer back to the Named_object for this type.
3635 Named_object
* named_object_
;
3636 // If this type is defined in a function, a pointer back to the
3637 // function in which it is defined.
3638 Named_object
* in_function_
;
3639 // The index of this type in IN_FUNCTION_.
3640 unsigned int in_function_index_
;
3643 // The list of methods defined for this type. Any named type can
3645 Bindings
* local_methods_
;
3646 // The full list of methods for this type, including methods
3647 // declared for anonymous fields.
3648 Methods
* all_methods_
;
3649 // A mapping from interfaces to the associated interface method
3650 // tables for this type.
3651 Interface_method_tables
* interface_method_tables_
;
3652 // A mapping from interfaces to the associated interface method
3653 // tables for pointers to this type.
3654 Interface_method_tables
* pointer_interface_method_tables_
;
3655 // The location where this type was defined.
3657 // The backend representation of this type during backend
3658 // conversion. This is used to avoid endless recursion when a named
3659 // type refers to itself.
3660 Btype
* named_btype_
;
3661 // A list of types which must be converted to the backend
3662 // representation before this type can be converted. This is for
3664 // type S1 { p *S2 }
3666 // where we can't convert S2 to the backend representation unless we
3667 // have converted S1.
3668 std::vector
<Named_type
*> dependencies_
;
3669 // Whether this is an alias type.
3671 // Whether this type is visible. This is false if this type was
3672 // created because it was referenced by an imported object, but the
3673 // type itself was not exported. This will always be true for types
3674 // created in the current package.
3676 // Whether this type is erroneous.
3678 // Whether this type is permitted in the heap. This is true by
3679 // default, false if there is a magic //go:notinheap comment.
3681 // Whether the current value of named_btype_ is a placeholder for
3682 // which the final size of the type is not known.
3683 bool is_placeholder_
;
3684 // Whether this type has been converted to the backend
3685 // representation. Implies that is_placeholder_ is false.
3687 // Whether this type has been verified.
3689 // In a recursive operation such as has_pointer, this flag is used
3690 // to prevent infinite recursion when a type refers to itself. This
3691 // is mutable because it is always reset to false when the function
3694 // Like seen_, but used only by do_compare_is_identity.
3695 bool seen_in_compare_is_identity_
;
3696 // Like seen_, but used only by do_get_backend.
3697 bool seen_in_get_backend_
;
3698 // Like seen_, but used when resolving aliases.
3699 mutable bool seen_alias_
;
3702 // A forward declaration. This handles a type which has been declared
3705 class Forward_declaration_type
: public Type
3708 Forward_declaration_type(Named_object
* named_object
);
3710 // The named object associated with this type declaration. This
3711 // will be resolved.
3716 named_object() const;
3718 // Return the name of the type.
3722 // Return the type to which this points. Give an error if the type
3723 // has not yet been defined.
3730 // Whether the base type has been defined.
3734 // Add a method to this type.
3736 add_method(const std::string
& name
, Function
*);
3738 // Add a method declaration to this type.
3740 add_method_declaration(const std::string
& name
, Package
*, Function_type
*,
3743 // Add an already created object as a method to this type.
3745 add_existing_method(Named_object
*);
3749 do_traverse(Traverse
* traverse
);
3755 do_has_pointer() const
3756 { return this->real_type()->has_pointer(); }
3759 do_compare_is_identity(Gogo
* gogo
)
3760 { return this->real_type()->compare_is_identity(gogo
); }
3764 { return this->real_type()->is_reflexive(); }
3767 do_needs_key_update()
3768 { return this->real_type()->needs_key_update(); }
3772 { return this->real_type()->in_heap(); }
3775 do_hash_for_method(Gogo
* gogo
, int flags
) const
3776 { return this->real_type()->hash_for_method(gogo
, flags
); }
3779 do_get_backend(Gogo
* gogo
);
3782 do_type_descriptor(Gogo
*, Named_type
*);
3785 do_reflection(Gogo
*, std::string
*) const;
3788 do_mangled_name(Gogo
*, std::string
*, bool*) const;
3791 do_export(Export
*) const;
3794 // Issue a warning about a use of an undefined type.
3798 // The type declaration.
3799 Named_object
* named_object_
;
3800 // Whether we have issued a warning about this type.
3801 mutable bool warned_
;
3804 // The Type_context struct describes what we expect for the type of an
3809 // The exact type we expect, if known. This may be NULL.
3811 // Whether an abstract type is permitted.
3812 bool may_be_abstract
;
3816 : type(NULL
), may_be_abstract(false)
3819 Type_context(Type
* a_type
, bool a_may_be_abstract
)
3820 : type(a_type
), may_be_abstract(a_may_be_abstract
)
3824 #endif // !defined(GO_TYPES_H)