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
;
56 // Type codes used in type descriptors. These must match the values
57 // in libgo/runtime/go-type.h. They also match the values in the gc
58 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
59 // although this is not required.
61 static const int RUNTIME_TYPE_KIND_BOOL
= 1;
62 static const int RUNTIME_TYPE_KIND_INT
= 2;
63 static const int RUNTIME_TYPE_KIND_INT8
= 3;
64 static const int RUNTIME_TYPE_KIND_INT16
= 4;
65 static const int RUNTIME_TYPE_KIND_INT32
= 5;
66 static const int RUNTIME_TYPE_KIND_INT64
= 6;
67 static const int RUNTIME_TYPE_KIND_UINT
= 7;
68 static const int RUNTIME_TYPE_KIND_UINT8
= 8;
69 static const int RUNTIME_TYPE_KIND_UINT16
= 9;
70 static const int RUNTIME_TYPE_KIND_UINT32
= 10;
71 static const int RUNTIME_TYPE_KIND_UINT64
= 11;
72 static const int RUNTIME_TYPE_KIND_UINTPTR
= 12;
73 static const int RUNTIME_TYPE_KIND_FLOAT32
= 13;
74 static const int RUNTIME_TYPE_KIND_FLOAT64
= 14;
75 static const int RUNTIME_TYPE_KIND_COMPLEX64
= 15;
76 static const int RUNTIME_TYPE_KIND_COMPLEX128
= 16;
77 static const int RUNTIME_TYPE_KIND_ARRAY
= 17;
78 static const int RUNTIME_TYPE_KIND_CHAN
= 18;
79 static const int RUNTIME_TYPE_KIND_FUNC
= 19;
80 static const int RUNTIME_TYPE_KIND_INTERFACE
= 20;
81 static const int RUNTIME_TYPE_KIND_MAP
= 21;
82 static const int RUNTIME_TYPE_KIND_PTR
= 22;
83 static const int RUNTIME_TYPE_KIND_SLICE
= 23;
84 static const int RUNTIME_TYPE_KIND_STRING
= 24;
85 static const int RUNTIME_TYPE_KIND_STRUCT
= 25;
86 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER
= 26;
88 static const int RUNTIME_TYPE_KIND_DIRECT_IFACE
= (1 << 5);
89 static const int RUNTIME_TYPE_KIND_GC_PROG
= (1 << 6);
90 static const int RUNTIME_TYPE_KIND_NO_POINTERS
= (1 << 7);
92 // To build the complete list of methods for a named type we need to
93 // gather all methods from anonymous fields. Those methods may
94 // require an arbitrary set of indirections and field offsets. There
95 // is also the possibility of ambiguous methods, which we could ignore
96 // except that we want to give a better error message for that case.
97 // This is a base class. There are two types of methods: named
98 // methods, and methods which are inherited from an anonymous field of
104 // For methods in anonymous types we need to know the sequence of
105 // field references used to extract the pointer to pass to the
106 // method. Since each method for a particular anonymous field will
107 // have the sequence of field indexes, and since the indexes can be
108 // shared going down the chain, we use a manually managed linked
109 // list. The first entry in the list is the field index for the
110 // last field, the one passed to the method.
114 const Field_indexes
* next
;
115 unsigned int field_index
;
121 // Get the list of field indexes.
123 field_indexes() const
124 { return this->field_indexes_
; }
129 { return this->depth_
; }
131 // Return whether this is a value method--a method which does not
132 // require a pointer expression.
134 is_value_method() const
135 { return this->is_value_method_
; }
137 // Return whether we need a stub method--this is true if we can't
138 // just pass the main object to the method.
140 needs_stub_method() const
141 { return this->needs_stub_method_
; }
143 // Return whether this is an ambiguous method name.
146 { return this->is_ambiguous_
; }
148 // Note that this method is ambiguous.
151 { this->is_ambiguous_
= true; }
153 // Return the type of the method.
156 { return this->do_type(); }
158 // Return the location of the method receiver.
160 receiver_location() const
161 { return this->do_receiver_location(); }
163 // Return an expression which binds this method to EXPR. This is
164 // something which can be used with a function call.
166 bind_method(Expression
* expr
, Location location
) const;
168 // Return the named object for this method. This may only be called
169 // after methods are finalized.
171 named_object() const;
173 // Get the stub object.
177 go_assert(this->stub_
!= NULL
);
181 // Set the stub object.
183 set_stub_object(Named_object
* no
)
185 go_assert(this->stub_
== NULL
);
189 // Return true if this method should not participate in any
193 { return this->do_nointerface(); }
196 // These objects are only built by the child classes.
197 Method(const Field_indexes
* field_indexes
, unsigned int depth
,
198 bool is_value_method
, bool needs_stub_method
)
199 : field_indexes_(field_indexes
), depth_(depth
), stub_(NULL
),
200 is_value_method_(is_value_method
), needs_stub_method_(needs_stub_method
),
204 // The named object for this method.
205 virtual Named_object
*
206 do_named_object() const = 0;
208 // The type of the method.
209 virtual Function_type
*
212 // Return the location of the method receiver.
214 do_receiver_location() const = 0;
216 // Bind a method to an object.
218 do_bind_method(Expression
* expr
, Location location
) const = 0;
220 // Return whether this method should not participate in interfaces.
222 do_nointerface() const = 0;
225 // The sequence of field indexes used for this method. If this is
226 // NULL, then the method is defined for the current type.
227 const Field_indexes
* field_indexes_
;
228 // The depth at which this method was found.
230 // If a stub method is required, this is its object. This is only
231 // set after stub methods are built in finalize_methods.
233 // Whether this is a value method--a method that does not require a
235 bool is_value_method_
;
236 // Whether a stub method is required.
237 bool needs_stub_method_
;
238 // Whether this method is ambiguous.
242 // A named method. This is what you get with a method declaration,
243 // either directly on the type, or inherited from some anonymous
246 class Named_method
: public Method
249 Named_method(Named_object
* named_object
, const Field_indexes
* field_indexes
,
250 unsigned int depth
, bool is_value_method
,
251 bool needs_stub_method
)
252 : Method(field_indexes
, depth
, is_value_method
, needs_stub_method
),
253 named_object_(named_object
)
257 // Get the Named_object for the method.
259 do_named_object() const
260 { return this->named_object_
; }
262 // The type of the method.
266 // Return the location of the method receiver.
268 do_receiver_location() const;
270 // Bind a method to an object.
272 do_bind_method(Expression
* expr
, Location location
) const;
274 // Return whether this method should not participate in interfaces.
276 do_nointerface() const;
279 // The method itself. For a method which needs a stub, this starts
280 // out as the underlying method, and is later replaced with the stub
282 Named_object
* named_object_
;
285 // An interface method. This is used when an interface appears as an
286 // anonymous field in a named struct.
288 class Interface_method
: public Method
291 Interface_method(const std::string
& name
, Location location
,
292 Function_type
* fntype
, const Field_indexes
* field_indexes
,
294 : Method(field_indexes
, depth
, true, true),
295 name_(name
), location_(location
), fntype_(fntype
)
299 // Get the Named_object for the method. This should never be
300 // called, as we always create a stub.
302 do_named_object() const
303 { go_unreachable(); }
305 // The type of the method.
308 { return this->fntype_
; }
310 // Return the location of the method receiver.
312 do_receiver_location() const
313 { return this->location_
; }
315 // Bind a method to an object.
317 do_bind_method(Expression
* expr
, Location location
) const;
319 // Return whether this method should not participate in interfaces.
321 do_nointerface() const
325 // The name of the interface method to call.
327 // The location of the definition of the interface method.
329 // The type of the interface method.
330 Function_type
* fntype_
;
333 // A mapping from method name to Method. This is a wrapper around a
339 typedef Unordered_map(std::string
, Method
*) Method_map
;
342 typedef Method_map::const_iterator const_iterator
;
348 // Insert a new method. Returns true if it was inserted, false if
349 // it was overidden or ambiguous.
351 insert(const std::string
& name
, Method
* m
);
353 // The number of (unambiguous) methods.
360 { return this->methods_
.begin(); }
364 { return this->methods_
.end(); }
368 find(const std::string
& name
) const
369 { return this->methods_
.find(name
); }
373 { return this->methods_
.empty(); }
379 // The base class for all types.
384 // The types of types.
385 enum Type_classification
398 TYPE_CALL_MULTIPLE_RESULT
,
418 // Get the unnamed bool type.
422 // Get the named type "bool".
426 // Make the named type "bool".
428 make_named_bool_type();
430 // Make an abstract integer type.
432 make_abstract_integer_type();
434 // Make an abstract type for a character constant.
436 make_abstract_character_type();
438 // Make a named integer type with a specified size.
439 // RUNTIME_TYPE_KIND is the code to use in reflection information,
440 // to distinguish int and int32.
442 make_integer_type(const char* name
, bool is_unsigned
, int bits
,
443 int runtime_type_kind
);
445 // Look up a named integer type.
447 lookup_integer_type(const char* name
);
449 // Make an abstract floating point type.
451 make_abstract_float_type();
453 // Make a named floating point type with a specific size.
454 // RUNTIME_TYPE_KIND is the code to use in reflection information,
455 // to distinguish float and float32.
457 make_float_type(const char* name
, int bits
, int runtime_type_kind
);
459 // Look up a named float type.
461 lookup_float_type(const char* name
);
463 // Make an abstract complex type.
465 make_abstract_complex_type();
467 // Make a named complex type with a specific size.
468 // RUNTIME_TYPE_KIND is the code to use in reflection information,
469 // to distinguish complex and complex64.
471 make_complex_type(const char* name
, int bits
, int runtime_type_kind
);
473 // Look up a named complex type.
475 lookup_complex_type(const char* name
);
477 // Get the unnamed string type.
481 // Get the named type "string".
483 lookup_string_type();
485 // Make the named type "string".
487 make_named_string_type();
492 static Function_type
*
493 make_function_type(Typed_identifier
* receiver
,
494 Typed_identifier_list
* parameters
,
495 Typed_identifier_list
* results
,
498 static Backend_function_type
*
499 make_backend_function_type(Typed_identifier
* receiver
,
500 Typed_identifier_list
* parameters
,
501 Typed_identifier_list
* results
,
505 make_pointer_type(Type
*);
508 finish_pointer_types(Gogo
* gogo
);
514 make_call_multiple_result_type(Call_expression
*);
517 make_struct_type(Struct_field_list
* fields
, Location
);
520 make_array_type(Type
* element_type
, Expression
* length
);
523 make_map_type(Type
* key_type
, Type
* value_type
, Location
);
526 make_channel_type(bool send
, bool receive
, Type
*);
528 static Interface_type
*
529 make_interface_type(Typed_identifier_list
* methods
, Location
);
531 static Interface_type
*
532 make_empty_interface_type(Location
);
535 make_type_descriptor_type();
538 make_type_descriptor_ptr_type();
541 make_named_type(Named_object
*, Type
*, Location
);
544 make_forward_declaration(Named_object
*);
546 // Make a builtin struct type from a list of fields.
548 make_builtin_struct_type(int nfields
, ...);
550 // Make a builtin named type.
552 make_builtin_named_type(const char* name
, Type
* type
);
556 traverse(Type
*, Traverse
*);
558 // Verify the type. This is called after parsing, and verifies that
559 // types are complete and meet the language requirements. This
560 // returns false if the type is invalid and we should not continue
564 { return this->do_verify(); }
566 // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
567 // returns that an erroneous type is identical to any other type;
568 // this is used to avoid cascading errors. If this returns false,
569 // and REASON is not NULL, it may set *REASON.
571 are_identical(const Type
* lhs
, const Type
* rhs
, bool errors_are_identical
,
572 std::string
* reason
);
574 // An argument to are_identical_cmp_tags, indicating whether or not
575 // to compare struct field tags.
581 // Return true if two types are identical. This is like the
582 // are_identical function, but also takes a CMP_TAGS argument
583 // indicating whether to compare struct tags. Otherwise the
584 // parameters are as for are_identical.
586 are_identical_cmp_tags(const Type
* lhs
, const Type
* rhs
,
587 Cmp_tags
, bool errors_are_identical
,
588 std::string
* reason
);
590 // Return true if two types are compatible for use in a binary
591 // operation, other than a shift, comparison, or channel send. This
592 // is an equivalence relation.
594 are_compatible_for_binop(const Type
* t1
, const Type
* t2
);
596 // Return true if two types are compatible for use with the
597 // comparison operator. IS_EQUALITY_OP is true if this is an
598 // equality comparison, false if it is an ordered comparison. This
599 // is an equivalence relation. If this returns false, and REASON is
600 // not NULL, it sets *REASON.
602 are_compatible_for_comparison(bool is_equality_op
, const Type
*t1
,
603 const Type
*t2
, std::string
* reason
);
605 // Return true if a type is comparable with itself. This is true of
606 // most types, but false for, e.g., function types.
608 is_comparable() const
609 { return Type::are_compatible_for_comparison(true, this, this, NULL
); }
611 // Return true if a value with type RHS is assignable to a variable
612 // with type LHS. This is not an equivalence relation. If this
613 // returns false, and REASON is not NULL, it sets *REASON.
615 are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
);
617 // Return true if a value with type RHS may be converted to type
618 // LHS. If this returns false, and REASON is not NULL, it sets
621 are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
);
623 // Return true if values of this type can be compared using an
624 // identity function which gets nothing but a pointer to the value
627 compare_is_identity(Gogo
* gogo
)
628 { return this->do_compare_is_identity(gogo
); }
630 // Return whether values of this type are reflexive: if a comparison
631 // of a value with itself always returns true.
634 { return this->do_is_reflexive(); }
636 // Return whether values of this, when used as a key in map,
637 // requires the key to be updated when an assignment is made.
640 { return this->do_needs_key_update(); }
642 // Whether the type is permitted in the heap.
645 { return this->do_in_heap(); }
647 // Return a hash code for this type for the method hash table.
648 // Types which are equivalent according to are_identical will have
649 // the same hash code.
651 hash_for_method(Gogo
*) const;
653 // Return the type classification.
655 classification() const
656 { return this->classification_
; }
658 // Return the base type for this type. This looks through forward
659 // declarations and names. Using this with a forward declaration
660 // which has not been defined will return an error type.
667 // Return the type skipping defined forward declarations. If this
668 // type is a forward declaration which has not been defined, it will
669 // return the Forward_declaration_type. This differs from base() in
670 // that it will return a Named_type, and for a
671 // Forward_declaration_type which is not defined it will return that
672 // type rather than an error type.
679 // Return true if this is a basic type: a type which is not composed
680 // of other types, and is not void.
682 is_basic_type() const;
684 // Return true if this is an abstract type--an integer, floating
685 // point, or complex type whose size has not been determined.
689 // Return a non-abstract version of an abstract type.
691 make_non_abstract_type();
693 // Return true if this type is or contains a pointer. This
694 // determines whether the garbage collector needs to look at a value
698 { return this->do_has_pointer(); }
700 // Return true if this is the error type. This returns false for a
701 // type which is not defined, as it is called by the parser before
702 // all types are defined.
704 is_error_type() const;
706 // Return true if this is the error type or if the type is
707 // undefined. If the type is undefined, this will give an error.
708 // This should only be called after parsing is complete.
711 { return this->base()->is_error_type(); }
713 // Return true if this is a void type.
716 { return this->classification_
== TYPE_VOID
; }
718 // If this is an integer type, return the Integer_type. Otherwise,
719 // return NULL. This is a controlled dynamic_cast.
722 { return this->convert
<Integer_type
, TYPE_INTEGER
>(); }
726 { return this->convert
<const Integer_type
, TYPE_INTEGER
>(); }
728 // If this is a floating point type, return the Float_type.
729 // Otherwise, return NULL. This is a controlled dynamic_cast.
732 { return this->convert
<Float_type
, TYPE_FLOAT
>(); }
736 { return this->convert
<const Float_type
, TYPE_FLOAT
>(); }
738 // If this is a complex type, return the Complex_type. Otherwise,
742 { return this->convert
<Complex_type
, TYPE_COMPLEX
>(); }
746 { return this->convert
<const Complex_type
, TYPE_COMPLEX
>(); }
748 // Return whether this is a numeric type.
750 is_numeric_type() const
752 Type_classification tc
= this->base()->classification_
;
753 return tc
== TYPE_INTEGER
|| tc
== TYPE_FLOAT
|| tc
== TYPE_COMPLEX
;
756 // Return true if this is a boolean type.
758 is_boolean_type() const
759 { return this->base()->classification_
== TYPE_BOOLEAN
; }
761 // Return true if this is an abstract boolean type.
763 is_abstract_boolean_type() const
764 { return this->classification_
== TYPE_BOOLEAN
; }
766 // Return true if this is a string type.
768 is_string_type() const
769 { return this->base()->classification_
== TYPE_STRING
; }
771 // Return true if this is an abstract string type.
773 is_abstract_string_type() const
774 { return this->classification_
== TYPE_STRING
; }
776 // Return true if this is the sink type. This is the type of the
777 // blank identifier _.
780 { return this->base()->classification_
== TYPE_SINK
; }
782 // If this is a function type, return it. Otherwise, return NULL.
785 { return this->convert
<Function_type
, TYPE_FUNCTION
>(); }
788 function_type() const
789 { return this->convert
<const Function_type
, TYPE_FUNCTION
>(); }
791 // If this is a pointer type, return the type to which it points.
792 // Otherwise, return NULL.
796 // If this is a pointer type, return the type to which it points.
797 // Otherwise, return the type itself.
801 Type
* pt
= this->points_to();
802 return pt
!= NULL
? pt
: this;
808 const Type
* pt
= this->points_to();
809 return pt
!= NULL
? pt
: this;
812 // Return true if this is the nil type. We don't use base() here,
813 // because this can be called during parse, and there is no way to
814 // name the nil type anyhow.
817 { return this->classification_
== TYPE_NIL
; }
819 // Return true if this is the predeclared constant nil being used as
820 // a type. This is what the parser produces for type switches which
823 is_nil_constant_as_type() const;
825 // Return true if this is the return type of a function which
826 // returns multiple values.
828 is_call_multiple_result_type() const
829 { return this->base()->classification_
== TYPE_CALL_MULTIPLE_RESULT
; }
831 // If this is a struct type, return it. Otherwise, return NULL.
834 { return this->convert
<Struct_type
, TYPE_STRUCT
>(); }
838 { return this->convert
<const Struct_type
, TYPE_STRUCT
>(); }
840 // If this is an array type, return it. Otherwise, return NULL.
843 { return this->convert
<Array_type
, TYPE_ARRAY
>(); }
847 { return this->convert
<const Array_type
, TYPE_ARRAY
>(); }
849 // Return whether if this is a slice type.
851 is_slice_type() const;
853 // If this is a map type, return it. Otherwise, return NULL.
856 { return this->convert
<Map_type
, TYPE_MAP
>(); }
860 { return this->convert
<const Map_type
, TYPE_MAP
>(); }
862 // If this is a channel type, return it. Otherwise, return NULL.
865 { return this->convert
<Channel_type
, TYPE_CHANNEL
>(); }
869 { return this->convert
<const Channel_type
, TYPE_CHANNEL
>(); }
871 // If this is an interface type, return it. Otherwise, return NULL.
874 { return this->convert
<Interface_type
, TYPE_INTERFACE
>(); }
876 const Interface_type
*
877 interface_type() const
878 { return this->convert
<const Interface_type
, TYPE_INTERFACE
>(); }
880 // If this is a named type, return it. Otherwise, return NULL.
887 // If this is a forward declaration, return it. Otherwise, return
889 Forward_declaration_type
*
890 forward_declaration_type()
891 { return this->convert_no_base
<Forward_declaration_type
, TYPE_FORWARD
>(); }
893 const Forward_declaration_type
*
894 forward_declaration_type() const
896 return this->convert_no_base
<const Forward_declaration_type
,
900 // Return true if this type is not yet defined.
902 is_undefined() const;
904 // Return true if this is the unsafe.pointer type. We currently
905 // represent that as pointer-to-void.
907 is_unsafe_pointer_type() const
908 { return this->points_to() != NULL
&& this->points_to()->is_void_type(); }
910 // Look for field or method NAME for TYPE. Return an expression for
911 // it, bound to EXPR.
913 bind_field_or_method(Gogo
*, const Type
* type
, Expression
* expr
,
914 const std::string
& name
, Location
);
916 // Return true if NAME is an unexported field or method of TYPE.
918 is_unexported_field_or_method(Gogo
*, const Type
*, const std::string
&,
919 std::vector
<const Named_type
*>*);
921 // Convert the builtin named types.
923 convert_builtin_named_types(Gogo
*);
925 // Return the backend representation of this type.
929 // Return a placeholder for the backend representation of the type.
930 // This will return a type of the correct size, but for which some
931 // of the fields may still need to be completed.
933 get_backend_placeholder(Gogo
*);
935 // Finish the backend representation of a placeholder.
937 finish_backend(Gogo
*, Btype
*);
939 // Build a type descriptor entry for this type. Return a pointer to
940 // it. The location is the location which causes us to need the
943 type_descriptor_pointer(Gogo
* gogo
, Location
);
945 // Build the Garbage Collection symbol for this type. Return a pointer to it.
947 gc_symbol_pointer(Gogo
* gogo
);
949 // Return whether this type needs a garbage collection program.
950 // Sets *PTRSIZE and *PTRDATA.
952 needs_gcprog(Gogo
*, int64_t* ptrsize
, int64_t* ptrdata
);
954 // Return a ptrmask variable for this type.
956 gc_ptrmask_var(Gogo
*, int64_t ptrsize
, int64_t ptrdata
);
958 // Return the type reflection string for this type.
960 reflection(Gogo
*) const;
962 // Return a mangled name for the type. This is a name which can be
963 // used in assembler code. Identical types should have the same
966 mangled_name(Gogo
*) const;
968 // If the size of the type can be determined, set *PSIZE to the size
969 // in bytes and return true. Otherwise, return false. This queries
972 backend_type_size(Gogo
*, int64_t* psize
);
974 // If the alignment of the type can be determined, set *PALIGN to
975 // the alignment in bytes and return true. Otherwise, return false.
977 backend_type_align(Gogo
*, int64_t* palign
);
979 // If the alignment of a struct field of this type can be
980 // determined, set *PALIGN to the alignment in bytes and return
981 // true. Otherwise, return false.
983 backend_type_field_align(Gogo
*, int64_t* palign
);
985 // Determine the ptrdata size for the backend version of this type:
986 // the length of the prefix of the type that can contain a pointer
987 // value. If it can be determined, set *PPTRDATA to the value in
988 // bytes and return true. Otherwise, return false.
990 backend_type_ptrdata(Gogo
*, int64_t* pptrdata
);
992 // Determine the ptrdata size that we are going to set in the type
993 // descriptor. This is normally the same as backend_type_ptrdata,
994 // but differs if we use a gcprog for an array. The arguments and
995 // results are as for backend_type_ptrdata.
997 descriptor_ptrdata(Gogo
*, int64_t* pptrdata
);
999 // Whether the backend size is known.
1001 is_backend_type_size_known(Gogo
*);
1003 // Return whether the type needs specially built type functions.
1005 needs_specific_type_functions(Gogo
*);
1007 // Get the hash and equality functions for a type.
1009 type_functions(Gogo
*, Named_type
* name
, Function_type
* hash_fntype
,
1010 Function_type
* equal_fntype
, Named_object
** hash_fn
,
1011 Named_object
** equal_fn
);
1013 // Write the hash and equality type functions.
1015 write_specific_type_functions(Gogo
*, Named_type
*, int64_t size
,
1016 const std::string
& hash_name
,
1017 Function_type
* hash_fntype
,
1018 const std::string
& equal_name
,
1019 Function_type
* equal_fntype
);
1021 // Return the alignment required by the memequalN function.
1022 static int64_t memequal_align(Gogo
*, int size
);
1026 export_type(Export
* exp
) const
1027 { this->do_export(exp
); }
1031 import_type(Import
*);
1034 Type(Type_classification
);
1036 // Functions implemented by the child class.
1038 // Traverse the subtypes.
1040 do_traverse(Traverse
*);
1048 do_has_pointer() const
1052 do_compare_is_identity(Gogo
*) = 0;
1059 do_needs_key_update()
1066 virtual unsigned int
1067 do_hash_for_method(Gogo
*) const;
1070 do_get_backend(Gogo
*) = 0;
1073 do_type_descriptor(Gogo
*, Named_type
* name
) = 0;
1076 do_reflection(Gogo
*, std::string
*) const = 0;
1079 do_mangled_name(Gogo
*, std::string
*) const = 0;
1082 do_export(Export
*) const;
1084 // Return whether a method expects a pointer as the receiver.
1086 method_expects_pointer(const Named_object
*);
1088 // Finalize the methods for a type.
1090 finalize_methods(Gogo
*, const Type
*, Location
, Methods
**);
1092 // Return a method from a set of methods.
1094 method_function(const Methods
*, const std::string
& name
,
1095 bool* is_ambiguous
);
1097 // A mapping from interfaces to the associated interface method
1098 // tables for this type. This maps to a decl.
1099 typedef Unordered_map_hash(Interface_type
*, Expression
*, Type_hash_identical
,
1100 Type_identical
) Interface_method_tables
;
1102 // Return a pointer to the interface method table for TYPE for the
1103 // interface INTERFACE.
1105 interface_method_table(Type
* type
,
1106 Interface_type
*interface
, bool is_pointer
,
1107 Interface_method_tables
** method_tables
,
1108 Interface_method_tables
** pointer_tables
);
1110 // Return a composite literal for the type descriptor entry for a
1113 type_descriptor(Gogo
*, Type
*);
1115 // Return a composite literal for the type descriptor entry for
1116 // TYPE, using NAME as the name of the type.
1118 named_type_descriptor(Gogo
*, Type
* type
, Named_type
* name
);
1120 // Return a composite literal for a plain type descriptor for this
1121 // type with the given kind and name.
1123 plain_type_descriptor(Gogo
*, int runtime_type_kind
, Named_type
* name
);
1125 // Build a composite literal for the basic type descriptor.
1127 type_descriptor_constructor(Gogo
*, int runtime_type_kind
, Named_type
*,
1128 const Methods
*, bool only_value_methods
);
1130 // For the benefit of child class reflection string generation.
1132 append_reflection(const Type
* type
, Gogo
* gogo
, std::string
* ret
) const
1133 { type
->do_reflection(gogo
, ret
); }
1135 // For the benefit of child class mangling.
1137 append_mangled_name(const Type
* type
, Gogo
* gogo
, std::string
* ret
) const
1138 { type
->do_mangled_name(gogo
, ret
); }
1140 // Incorporate a string into a hash code.
1142 hash_string(const std::string
&, unsigned int);
1144 // Return the backend representation for the underlying type of a
1147 get_named_base_btype(Gogo
* gogo
, Type
* base_type
)
1148 { return base_type
->get_btype_without_hash(gogo
); }
1151 // Convert to the desired type classification, or return NULL. This
1152 // is a controlled dynamic_cast.
1153 template<typename Type_class
, Type_classification type_classification
>
1157 Type
* base
= this->base();
1158 return (base
->classification_
== type_classification
1159 ? static_cast<Type_class
*>(base
)
1163 template<typename Type_class
, Type_classification type_classification
>
1167 const Type
* base
= this->base();
1168 return (base
->classification_
== type_classification
1169 ? static_cast<Type_class
*>(base
)
1173 template<typename Type_class
, Type_classification type_classification
>
1177 return (this->classification_
== type_classification
1178 ? static_cast<Type_class
*>(this)
1182 template<typename Type_class
, Type_classification type_classification
>
1184 convert_no_base() const
1186 return (this->classification_
== type_classification
1187 ? static_cast<Type_class
*>(this)
1191 // Map unnamed types to type descriptor decls.
1192 typedef Unordered_map_hash(const Type
*, Bvariable
*, Type_hash_identical
,
1193 Type_identical
) Type_descriptor_vars
;
1195 static Type_descriptor_vars type_descriptor_vars
;
1197 // Build the type descriptor variable for this type.
1199 make_type_descriptor_var(Gogo
*);
1201 // Map unnamed types to type descriptor decls.
1202 typedef Unordered_map_hash(const Type
*, Bvariable
*, Type_hash_identical
,
1203 Type_identical
) GC_symbol_vars
;
1205 static GC_symbol_vars gc_symbol_vars
;
1207 // Map ptrmask symbol names to the ptrmask variable.
1208 typedef Unordered_map(std::string
, Bvariable
*) GC_gcbits_vars
;
1210 static GC_gcbits_vars gc_gcbits_vars
;
1212 // Build the GC symbol for this type.
1214 make_gc_symbol_var(Gogo
*);
1216 // Return the name of the type descriptor variable. If NAME is not
1217 // NULL, it is the name to use.
1219 type_descriptor_var_name(Gogo
*, Named_type
* name
);
1221 // Return true if the type descriptor for this type should be
1222 // defined in some other package. If NAME is not NULL, it is the
1223 // name of this type. If this returns true it sets *PACKAGE to the
1224 // package where the type descriptor is defined.
1226 type_descriptor_defined_elsewhere(Named_type
* name
, const Package
** package
);
1228 // Make a composite literal for the garbage collection program for
1231 gcprog_constructor(Gogo
*, int64_t ptrsize
, int64_t ptrdata
);
1233 // Build the hash and equality type functions for a type which needs
1234 // specific functions.
1236 specific_type_functions(Gogo
*, Named_type
*, int64_t size
,
1237 Function_type
* hash_fntype
,
1238 Function_type
* equal_fntype
, Named_object
** hash_fn
,
1239 Named_object
** equal_fn
);
1242 write_identity_hash(Gogo
*, int64_t size
);
1245 write_identity_equal(Gogo
*, int64_t size
);
1248 write_named_hash(Gogo
*, Named_type
*, Function_type
* hash_fntype
,
1249 Function_type
* equal_fntype
);
1252 write_named_equal(Gogo
*, Named_type
*);
1254 // Build a composite literal for the uncommon type information.
1256 uncommon_type_constructor(Gogo
*, Type
* uncommon_type
,
1257 Named_type
*, const Methods
*,
1258 bool only_value_methods
) const;
1260 // Build a composite literal for the methods.
1262 methods_constructor(Gogo
*, Type
* methods_type
, const Methods
*,
1263 bool only_value_methods
) const;
1265 // Build a composite literal for one method.
1267 method_constructor(Gogo
*, Type
* method_type
, const std::string
& name
,
1268 const Method
*, bool only_value_methods
) const;
1270 // Add all methods for TYPE to the list of methods for THIS.
1272 add_methods_for_type(const Type
* type
, const Method::Field_indexes
*,
1273 unsigned int depth
, bool, bool,
1274 std::vector
<const Named_type
*>*,
1278 add_local_methods_for_type(const Named_type
* type
,
1279 const Method::Field_indexes
*,
1280 unsigned int depth
, bool, bool, Methods
*);
1283 add_embedded_methods_for_type(const Type
* type
,
1284 const Method::Field_indexes
*,
1285 unsigned int depth
, bool, bool,
1286 std::vector
<const Named_type
*>*,
1290 add_interface_methods_for_type(const Type
* type
,
1291 const Method::Field_indexes
*,
1292 unsigned int depth
, Methods
*);
1294 // Build stub methods for a type.
1296 build_stub_methods(Gogo
*, const Type
* type
, const Methods
* methods
,
1300 build_one_stub_method(Gogo
*, Method
*, const char* receiver_name
,
1301 const Typed_identifier_list
*, bool is_varargs
,
1305 apply_field_indexes(Expression
*, const Method::Field_indexes
*,
1308 // Look for a field or method named NAME in TYPE.
1310 find_field_or_method(const Type
* type
, const std::string
& name
,
1311 bool receiver_can_be_pointer
,
1312 std::vector
<const Named_type
*>*, int* level
,
1313 bool* is_method
, bool* found_pointer_method
,
1314 std::string
* ambig1
, std::string
* ambig2
);
1316 // Get the backend representation for a type without looking in the
1317 // hash table for identical types.
1319 get_btype_without_hash(Gogo
*);
1321 // A backend type that may be a placeholder.
1322 struct Type_btype_entry
1325 bool is_placeholder
;
1328 // A mapping from Type to Btype*, used to ensure that the backend
1329 // representation of identical types is identical. This is only
1330 // used for unnamed types.
1331 typedef Unordered_map_hash(const Type
*, Type_btype_entry
,
1332 Type_hash_identical
, Type_identical
) Type_btypes
;
1334 static Type_btypes type_btypes
;
1336 // A list of builtin named types.
1337 static std::vector
<Named_type
*> named_builtin_types
;
1339 // A map from types which need specific type functions to the type
1340 // functions themselves.
1341 typedef std::pair
<Named_object
*, Named_object
*> Hash_equal_fn
;
1342 typedef Unordered_map_hash(const Type
*, Hash_equal_fn
, Type_hash_identical
,
1343 Type_identical
) Type_functions
;
1345 static Type_functions type_functions_table
;
1347 // Cache for reusing existing pointer types; maps from pointed-to-type
1349 typedef Unordered_map(Type
*, Pointer_type
*) Pointer_type_table
;
1351 static Pointer_type_table pointer_types
;
1353 // List of placeholder pointer types.
1354 static std::vector
<Pointer_type
*> placeholder_pointers
;
1356 // The type classification.
1357 Type_classification classification_
;
1358 // The backend representation of the type, once it has been
1361 // The type descriptor for this type. This starts out as NULL and
1362 // is filled in as needed.
1363 Bvariable
* type_descriptor_var_
;
1364 // The GC symbol for this type. This starts out as NULL and
1365 // is filled in as needed.
1366 Bvariable
* gc_symbol_var_
;
1367 // Whether this type can appear in the heap.
1371 // Type hash table operations.
1373 class Type_hash_identical
1377 operator()(const Type
* type
) const
1378 { return type
->hash_for_method(NULL
); }
1381 class Type_identical
1385 operator()(const Type
* t1
, const Type
* t2
) const
1386 { return Type::are_identical(t1
, t2
, false, NULL
); }
1389 // An identifier with a type.
1391 class Typed_identifier
1394 Typed_identifier(const std::string
& name
, Type
* type
,
1396 : name_(name
), type_(type
), location_(location
), note_(NULL
)
1402 { return this->name_
; }
1407 { return this->type_
; }
1409 // Return the location where the name was seen. This is not always
1413 { return this->location_
; }
1415 // Set the type--sometimes we see the identifier before the type.
1417 set_type(Type
* type
)
1419 go_assert(this->type_
== NULL
|| type
->is_error_type());
1423 // Get the escape note.
1426 { return this->note_
; }
1428 // Set the escape note.
1430 set_note(const std::string
& note
)
1431 { this->note_
= new std::string(note
); }
1438 // The location where the name was seen.
1440 // Escape note for this typed identifier. Used when importing and exporting
1445 // A list of Typed_identifiers.
1447 class Typed_identifier_list
1450 Typed_identifier_list()
1454 // Whether the list is empty.
1457 { return this->entries_
.empty(); }
1459 // Return the number of entries in the list.
1462 { return this->entries_
.size(); }
1464 // Add an entry to the end of the list.
1466 push_back(const Typed_identifier
& td
)
1467 { this->entries_
.push_back(td
); }
1469 // Remove an entry from the end of the list.
1472 { this->entries_
.pop_back(); }
1474 // Set the type of entry I to TYPE.
1476 set_type(size_t i
, Type
* type
)
1478 go_assert(i
< this->entries_
.size());
1479 this->entries_
[i
].set_type(type
);
1482 // Sort the entries by name.
1488 traverse(Traverse
*);
1490 // Return the first and last elements.
1493 { return this->entries_
.front(); }
1495 const Typed_identifier
&
1497 { return this->entries_
.front(); }
1501 { return this->entries_
.back(); }
1503 const Typed_identifier
&
1505 { return this->entries_
.back(); }
1509 { return this->entries_
.at(i
); }
1511 const Typed_identifier
&
1513 { return this->entries_
.at(i
); }
1516 set(size_t i
, const Typed_identifier
& t
)
1517 { this->entries_
.at(i
) = t
; }
1522 go_assert(c
<= this->entries_
.size());
1523 this->entries_
.resize(c
, Typed_identifier("", NULL
,
1524 Linemap::unknown_location()));
1529 { this->entries_
.reserve(c
); }
1533 typedef std::vector
<Typed_identifier
>::iterator iterator
;
1534 typedef std::vector
<Typed_identifier
>::const_iterator const_iterator
;
1538 { return this->entries_
.begin(); }
1542 { return this->entries_
.begin(); }
1546 { return this->entries_
.end(); }
1550 { return this->entries_
.end(); }
1552 // Return a copy of this list. This returns an independent copy of
1553 // the vector, but does not copy the types.
1554 Typed_identifier_list
*
1558 std::vector
<Typed_identifier
> entries_
;
1561 // The type of an integer.
1563 class Integer_type
: public Type
1566 // Create a new integer type.
1568 create_integer_type(const char* name
, bool is_unsigned
, int bits
,
1569 int runtime_type_kind
);
1571 // Look up an existing integer type.
1573 lookup_integer_type(const char* name
);
1575 // Create an abstract integer type.
1576 static Integer_type
*
1577 create_abstract_integer_type();
1579 // Create an abstract character type.
1580 static Integer_type
*
1581 create_abstract_character_type();
1583 // Whether this is an abstract integer type.
1586 { return this->is_abstract_
; }
1588 // Whether this is an unsigned type.
1591 { return this->is_unsigned_
; }
1593 // The number of bits.
1596 { return this->bits_
; }
1598 // Whether this type is the same as T.
1600 is_identical(const Integer_type
* t
) const;
1602 // Whether this is the type "byte" or another name for "byte".
1605 { return this->is_byte_
; }
1607 // Mark this as the "byte" type.
1610 { this->is_byte_
= true; }
1612 // Whether this is the type "rune" or another name for "rune".
1615 { return this->is_rune_
; }
1617 // Mark this as the "rune" type.
1620 { this->is_rune_
= true; }
1624 do_compare_is_identity(Gogo
*)
1628 do_hash_for_method(Gogo
*) const;
1631 do_get_backend(Gogo
*);
1634 do_type_descriptor(Gogo
*, Named_type
*);
1637 do_reflection(Gogo
*, std::string
*) const;
1640 do_mangled_name(Gogo
*, std::string
*) const;
1643 Integer_type(bool is_abstract
, bool is_unsigned
, int bits
,
1644 int runtime_type_kind
)
1645 : Type(TYPE_INTEGER
),
1646 is_abstract_(is_abstract
), is_unsigned_(is_unsigned
), is_byte_(false),
1647 is_rune_(false), bits_(bits
), runtime_type_kind_(runtime_type_kind
)
1650 // Map names of integer types to the types themselves.
1651 typedef std::map
<std::string
, Named_type
*> Named_integer_types
;
1652 static Named_integer_types named_integer_types
;
1654 // True if this is an abstract type.
1656 // True if this is an unsigned type.
1658 // True if this is the byte type.
1660 // True if this is the rune type.
1662 // The number of bits.
1664 // The runtime type code used in the type descriptor for this type.
1665 int runtime_type_kind_
;
1668 // The type of a floating point number.
1670 class Float_type
: public Type
1673 // Create a new float type.
1675 create_float_type(const char* name
, int bits
, int runtime_type_kind
);
1677 // Look up an existing float type.
1679 lookup_float_type(const char* name
);
1681 // Create an abstract float type.
1683 create_abstract_float_type();
1685 // Whether this is an abstract float type.
1688 { return this->is_abstract_
; }
1690 // The number of bits.
1693 { return this->bits_
; }
1695 // Whether this type is the same as T.
1697 is_identical(const Float_type
* t
) const;
1701 do_compare_is_identity(Gogo
*)
1708 // Distinction between +0 and -0 requires a key update.
1710 do_needs_key_update()
1714 do_hash_for_method(Gogo
*) const;
1717 do_get_backend(Gogo
*);
1720 do_type_descriptor(Gogo
*, Named_type
*);
1723 do_reflection(Gogo
*, std::string
*) const;
1726 do_mangled_name(Gogo
*, std::string
*) const;
1729 Float_type(bool is_abstract
, int bits
, int runtime_type_kind
)
1731 is_abstract_(is_abstract
), bits_(bits
),
1732 runtime_type_kind_(runtime_type_kind
)
1735 // Map names of float types to the types themselves.
1736 typedef std::map
<std::string
, Named_type
*> Named_float_types
;
1737 static Named_float_types named_float_types
;
1739 // True if this is an abstract type.
1741 // The number of bits in the floating point value.
1743 // The runtime type code used in the type descriptor for this type.
1744 int runtime_type_kind_
;
1747 // The type of a complex number.
1749 class Complex_type
: public Type
1752 // Create a new complex type.
1754 create_complex_type(const char* name
, int bits
, int runtime_type_kind
);
1756 // Look up an existing complex type.
1758 lookup_complex_type(const char* name
);
1760 // Create an abstract complex type.
1761 static Complex_type
*
1762 create_abstract_complex_type();
1764 // Whether this is an abstract complex type.
1767 { return this->is_abstract_
; }
1769 // The number of bits: 64 or 128.
1771 { return this->bits_
; }
1773 // Whether this type is the same as T.
1775 is_identical(const Complex_type
* t
) const;
1779 do_compare_is_identity(Gogo
*)
1786 // Distinction between +0 and -0 requires a key update.
1788 do_needs_key_update()
1792 do_hash_for_method(Gogo
*) const;
1795 do_get_backend(Gogo
*);
1798 do_type_descriptor(Gogo
*, Named_type
*);
1801 do_reflection(Gogo
*, std::string
*) const;
1804 do_mangled_name(Gogo
*, std::string
*) const;
1807 Complex_type(bool is_abstract
, int bits
, int runtime_type_kind
)
1808 : Type(TYPE_COMPLEX
),
1809 is_abstract_(is_abstract
), bits_(bits
),
1810 runtime_type_kind_(runtime_type_kind
)
1813 // Map names of complex types to the types themselves.
1814 typedef std::map
<std::string
, Named_type
*> Named_complex_types
;
1815 static Named_complex_types named_complex_types
;
1817 // True if this is an abstract type.
1819 // The number of bits in the complex value--64 or 128.
1821 // The runtime type code used in the type descriptor for this type.
1822 int runtime_type_kind_
;
1825 // The type of a string.
1827 class String_type
: public Type
1836 do_has_pointer() const
1840 do_compare_is_identity(Gogo
*)
1843 // New string might have a smaller backing store.
1845 do_needs_key_update()
1849 do_get_backend(Gogo
*);
1852 do_type_descriptor(Gogo
*, Named_type
*);
1855 do_reflection(Gogo
*, std::string
*) const;
1858 do_mangled_name(Gogo
*, std::string
* ret
) const;
1861 // The named string type.
1862 static Named_type
* string_type_
;
1865 // The type of a function.
1867 class Function_type
: public Type
1870 Function_type(Typed_identifier
* receiver
, Typed_identifier_list
* parameters
,
1871 Typed_identifier_list
* results
, Location location
)
1872 : Type(TYPE_FUNCTION
),
1873 receiver_(receiver
), parameters_(parameters
), results_(results
),
1874 location_(location
), is_varargs_(false), is_builtin_(false),
1875 fnbtype_(NULL
), is_tagged_(false)
1878 // Get the receiver.
1879 const Typed_identifier
*
1881 { return this->receiver_
; }
1883 // Add an escape note for the receiver.
1885 add_receiver_note(int encoding
)
1886 { this->receiver_
->set_note(Escape_note::make_tag(encoding
)); }
1888 // Get the return names and types.
1889 const Typed_identifier_list
*
1891 { return this->results_
; }
1893 // Get the parameter names and types.
1894 const Typed_identifier_list
*
1896 { return this->parameters_
; }
1898 // Add an escape note for the ith parameter.
1900 add_parameter_note(int index
, int encoding
)
1901 { this->parameters_
->at(index
).set_note(Escape_note::make_tag(encoding
)); }
1903 // Whether this function has been tagged during escape analysis.
1906 { return this->is_tagged_
; }
1908 // Mark this function as tagged after analyzing its escape.
1911 { this->is_tagged_
= true; }
1913 // Whether this is a varargs function.
1916 { return this->is_varargs_
; }
1918 // Whether this is a builtin function.
1921 { return this->is_builtin_
; }
1923 // The location where this type was defined.
1926 { return this->location_
; }
1928 // Return whether this is a method type.
1931 { return this->receiver_
!= NULL
; }
1933 // Whether T is a valid redeclaration of this type. This is called
1934 // when a function is declared more than once.
1936 is_valid_redeclaration(const Function_type
* t
, std::string
*) const;
1938 // Whether this type is the same as T.
1940 is_identical(const Function_type
* t
, bool ignore_receiver
,
1941 Cmp_tags
, bool errors_are_identical
, std::string
*) const;
1943 // Record that this is a varargs function.
1946 { this->is_varargs_
= true; }
1948 // Record that this is a builtin function.
1951 { this->is_builtin_
= true; }
1953 // Import a function type.
1954 static Function_type
*
1957 // Return a copy of this type without a receiver. This is only
1958 // valid for a method type.
1960 copy_without_receiver() const;
1962 // Return a copy of this type with a receiver. This is used when an
1963 // interface method is attached to a named or struct type.
1965 copy_with_receiver(Type
*) const;
1967 // Return a copy of this type with the receiver treated as the first
1968 // parameter. If WANT_POINTER_RECEIVER is true, the receiver is
1969 // forced to be a pointer.
1971 copy_with_receiver_as_param(bool want_pointer_receiver
) const;
1973 // Return a copy of this type ignoring any receiver and using dummy
1974 // names for all parameters. This is used for thunks for method
1977 copy_with_names() const;
1980 make_function_type_descriptor_type();
1982 // Return the backend representation of this function type. This is used
1983 // as the real type of a backend function declaration or defintion.
1985 get_backend_fntype(Gogo
*);
1989 do_traverse(Traverse
*);
1991 // A function descriptor may be allocated on the heap.
1993 do_has_pointer() const
1997 do_compare_is_identity(Gogo
*)
2001 do_hash_for_method(Gogo
*) const;
2004 do_get_backend(Gogo
*);
2007 do_type_descriptor(Gogo
*, Named_type
*);
2010 do_reflection(Gogo
*, std::string
*) const;
2013 do_mangled_name(Gogo
*, std::string
*) const;
2016 do_export(Export
*) const;
2020 type_descriptor_params(Type
*, const Typed_identifier
*,
2021 const Typed_identifier_list
*);
2023 // A mapping from a list of result types to a backend struct type.
2028 operator()(const Typed_identifier_list
*) const;
2035 operator()(const Typed_identifier_list
*,
2036 const Typed_identifier_list
*) const;
2039 typedef Unordered_map_hash(Typed_identifier_list
*, Btype
*,
2040 Results_hash
, Results_equal
) Results_structs
;
2042 static Results_structs results_structs
;
2044 // The receiver name and type. This will be NULL for a normal
2045 // function, non-NULL for a method.
2046 Typed_identifier
* receiver_
;
2047 // The parameter names and types.
2048 Typed_identifier_list
* parameters_
;
2049 // The result names and types. This will be NULL if no result was
2051 Typed_identifier_list
* results_
;
2052 // The location where this type was defined. This exists solely to
2053 // give a location for the fields of the struct if this function
2054 // returns multiple values.
2056 // Whether this function takes a variable number of arguments.
2058 // Whether this is a special builtin function which can not simply
2059 // be called. This is used for len, cap, etc.
2061 // The backend representation of this type for backend function
2062 // declarations and definitions.
2064 // Whether this function has been analyzed by escape analysis. If this is
2065 // TRUE, this function type's parameters contain a summary of the analysis.
2069 // The type of a function's backend representation.
2071 class Backend_function_type
: public Function_type
2074 Backend_function_type(Typed_identifier
* receiver
,
2075 Typed_identifier_list
* parameters
,
2076 Typed_identifier_list
* results
, Location location
)
2077 : Function_type(receiver
, parameters
, results
, location
)
2082 do_get_backend(Gogo
* gogo
)
2083 { return this->get_backend_fntype(gogo
); }
2086 // The type of a pointer.
2088 class Pointer_type
: public Type
2091 Pointer_type(Type
* to_type
)
2092 : Type(TYPE_POINTER
),
2098 { return this->to_type_
; }
2100 // Import a pointer type.
2101 static Pointer_type
*
2105 make_pointer_type_descriptor_type();
2109 do_traverse(Traverse
*);
2113 { return this->to_type_
->verify(); }
2116 do_has_pointer() const
2120 do_compare_is_identity(Gogo
*)
2124 do_hash_for_method(Gogo
*) const;
2127 do_get_backend(Gogo
*);
2130 do_type_descriptor(Gogo
*, Named_type
*);
2133 do_reflection(Gogo
*, std::string
*) const;
2136 do_mangled_name(Gogo
*, std::string
*) const;
2139 do_export(Export
*) const;
2142 // The type to which this type points.
2146 // The type of a field in a struct.
2151 explicit Struct_field(const Typed_identifier
& typed_identifier
)
2152 : typed_identifier_(typed_identifier
), tag_(NULL
), is_imported_(false)
2159 // Return whether this struct field is named NAME.
2161 is_field_name(const std::string
& name
) const;
2163 // Return whether this struct field is an unexported field named NAME.
2165 is_unexported_field_name(Gogo
*, const std::string
& name
) const;
2167 // Return whether this struct field is an embedded built-in type.
2169 is_embedded_builtin(Gogo
*) const;
2174 { return this->typed_identifier_
.type(); }
2176 // The field location.
2179 { return this->typed_identifier_
.location(); }
2181 // Whether the field has a tag.
2184 { return this->tag_
!= NULL
; }
2190 go_assert(this->tag_
!= NULL
);
2194 // Whether this is an anonymous field.
2196 is_anonymous() const
2197 { return this->typed_identifier_
.name().empty(); }
2199 // Set the tag. FIXME: This is never freed.
2201 set_tag(const std::string
& tag
)
2202 { this->tag_
= new std::string(tag
); }
2204 // Record that this field is defined in an imported struct.
2207 { this->is_imported_
= true; }
2209 // Set the type. This is only used in error cases.
2211 set_type(Type
* type
)
2212 { this->typed_identifier_
.set_type(type
); }
2215 // The field name, type, and location.
2216 Typed_identifier typed_identifier_
;
2217 // The field tag. This is NULL if the field has no tag.
2219 // Whether this field is defined in an imported struct.
2223 // A list of struct fields.
2225 class Struct_field_list
2232 // Whether the list is empty.
2235 { return this->entries_
.empty(); }
2237 // Return the number of entries.
2240 { return this->entries_
.size(); }
2242 // Add an entry to the end of the list.
2244 push_back(const Struct_field
& sf
)
2245 { this->entries_
.push_back(sf
); }
2247 // Index into the list.
2250 { return this->entries_
.at(i
); }
2252 // Last entry in list.
2255 { return this->entries_
.back(); }
2259 typedef std::vector
<Struct_field
>::iterator iterator
;
2260 typedef std::vector
<Struct_field
>::const_iterator const_iterator
;
2264 { return this->entries_
.begin(); }
2268 { return this->entries_
.begin(); }
2272 { return this->entries_
.end(); }
2276 { return this->entries_
.end(); }
2279 std::vector
<Struct_field
> entries_
;
2282 // The type of a struct.
2284 class Struct_type
: public Type
2287 Struct_type(Struct_field_list
* fields
, Location location
)
2288 : Type(TYPE_STRUCT
),
2289 fields_(fields
), location_(location
), all_methods_(NULL
),
2290 is_struct_incomparable_(false)
2293 // Return the field NAME. This only looks at local fields, not at
2294 // embedded types. If the field is found, and PINDEX is not NULL,
2295 // this sets *PINDEX to the field index. If the field is not found,
2296 // this returns NULL.
2298 find_local_field(const std::string
& name
, unsigned int *pindex
) const;
2300 // Return the field number INDEX.
2302 field(unsigned int index
) const
2303 { return &this->fields_
->at(index
); }
2305 // Get the struct fields.
2306 const Struct_field_list
*
2308 { return this->fields_
; }
2310 // Return the number of fields.
2313 { return this->fields_
->size(); }
2315 // Push a new field onto the end of the struct. This is used when
2316 // building a closure variable.
2318 push_field(const Struct_field
& sf
)
2319 { this->fields_
->push_back(sf
); }
2321 // Return an expression referring to field NAME in STRUCT_EXPR, or
2322 // NULL if there is no field with that name.
2323 Field_reference_expression
*
2324 field_reference(Expression
* struct_expr
, const std::string
& name
,
2327 // Return the total number of fields, including embedded fields.
2328 // This is the number of values that can appear in a conversion to
2331 total_field_count() const;
2333 // Whether this type is identical with T.
2335 is_identical(const Struct_type
* t
, Cmp_tags
,
2336 bool errors_are_identical
) const;
2338 // Return whether NAME is a local field which is not exported. This
2339 // is only used for better error reporting.
2341 is_unexported_local_field(Gogo
*, const std::string
& name
) const;
2343 // If this is an unnamed struct, build the complete list of methods,
2344 // including those from anonymous fields, and build methods stubs if
2347 finalize_methods(Gogo
*);
2349 // Return whether this type has any methods. This should only be
2350 // called after the finalize_methods pass.
2352 has_any_methods() const
2353 { return this->all_methods_
!= NULL
; }
2355 // Return the methods for tihs type. This should only be called
2356 // after the finalize_methods pass.
2359 { return this->all_methods_
; }
2361 // Return the method to use for NAME. This returns NULL if there is
2362 // no such method or if the method is ambiguous. When it returns
2363 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2365 method_function(const std::string
& name
, bool* is_ambiguous
) const;
2367 // Return a pointer to the interface method table for this type for
2368 // the interface INTERFACE. If IS_POINTER is true, set the type
2369 // descriptor to a pointer to this type, otherwise set it to this
2372 interface_method_table(Interface_type
* interface
, bool is_pointer
);
2374 // Traverse just the field types of a struct type.
2376 traverse_field_types(Traverse
* traverse
)
2377 { return this->do_traverse(traverse
); }
2379 // If the offset of field INDEX in the backend implementation can be
2380 // determined, set *POFFSET to the offset in bytes and return true.
2381 // Otherwise, return false.
2383 backend_field_offset(Gogo
*, unsigned int index
, int64_t* poffset
);
2385 // Finish the backend representation of all the fields.
2387 finish_backend_fields(Gogo
*);
2389 // Import a struct type.
2394 make_struct_type_descriptor_type();
2396 // Return whether this is a generated struct that is not comparable.
2398 is_struct_incomparable() const
2399 { return this->is_struct_incomparable_
; }
2401 // Record that this is a generated struct that is not comparable.
2403 set_is_struct_incomparable()
2404 { this->is_struct_incomparable_
= true; }
2406 // Write the hash function for this type.
2408 write_hash_function(Gogo
*, Named_type
*, Function_type
*, Function_type
*);
2410 // Write the equality function for this type.
2412 write_equal_function(Gogo
*, Named_type
*);
2414 // Whether we can write this type to a C header file, to implement
2417 can_write_to_c_header(std::vector
<const Named_object
*>*,
2418 std::vector
<const Named_object
*>*) const;
2420 // Write this type to a C header file, to implement -fgo-c-header.
2422 write_to_c_header(std::ostream
&) const;
2426 do_traverse(Traverse
*);
2432 do_has_pointer() const;
2435 do_compare_is_identity(Gogo
*);
2441 do_needs_key_update();
2447 do_hash_for_method(Gogo
*) const;
2450 do_get_backend(Gogo
*);
2453 do_type_descriptor(Gogo
*, Named_type
*);
2456 do_reflection(Gogo
*, std::string
*) const;
2459 do_mangled_name(Gogo
*, std::string
*) const;
2462 do_export(Export
*) const;
2466 can_write_type_to_c_header(const Type
*,
2467 std::vector
<const Named_object
*>*,
2468 std::vector
<const Named_object
*>*) const;
2471 write_field_to_c_header(std::ostream
&, const std::string
&, const Type
*) const;
2473 // Used to merge method sets of identical unnamed structs.
2474 typedef Unordered_map_hash(Struct_type
*, Struct_type
*, Type_hash_identical
,
2475 Type_identical
) Identical_structs
;
2477 static Identical_structs identical_structs
;
2479 // Used to manage method tables for identical unnamed structs.
2480 typedef std::pair
<Interface_method_tables
*, Interface_method_tables
*>
2481 Struct_method_table_pair
;
2483 typedef Unordered_map_hash(Struct_type
*, Struct_method_table_pair
*,
2484 Type_hash_identical
, Type_identical
)
2485 Struct_method_tables
;
2487 static Struct_method_tables struct_method_tables
;
2489 // Used to avoid infinite loops in field_reference_depth.
2490 struct Saw_named_type
2492 Saw_named_type
* next
;
2496 Field_reference_expression
*
2497 field_reference_depth(Expression
* struct_expr
, const std::string
& name
,
2498 Location
, Saw_named_type
*,
2499 unsigned int* depth
) const;
2501 // The fields of the struct.
2502 Struct_field_list
* fields_
;
2503 // The place where the struct was declared.
2505 // If this struct is unnamed, a list of methods.
2506 Methods
* all_methods_
;
2507 // True if this is a generated struct that is not considered to be
2509 bool is_struct_incomparable_
;
2512 // The type of an array.
2514 class Array_type
: public Type
2517 Array_type(Type
* element_type
, Expression
* length
)
2519 element_type_(element_type
), length_(length
), blength_(NULL
),
2520 issued_length_error_(false), is_array_incomparable_(false)
2523 // Return the element type.
2525 element_type() const
2526 { return this->element_type_
; }
2528 // Return the length. This will return NULL for a slice.
2531 { return this->length_
; }
2533 // Store the length as an int64_t into *PLEN. Return false if the
2534 // length can not be determined. This will assert if called for a
2537 int_length(int64_t* plen
);
2539 // Whether this type is identical with T.
2541 is_identical(const Array_type
* t
, Cmp_tags
,
2542 bool errors_are_identical
) const;
2544 // Return an expression for the pointer to the values in an array.
2546 get_value_pointer(Gogo
*, Expression
* array
, bool is_lvalue
) const;
2548 // Return an expression for the length of an array with this type.
2550 get_length(Gogo
*, Expression
* array
) const;
2552 // Return an expression for the capacity of an array with this type.
2554 get_capacity(Gogo
*, Expression
* array
) const;
2556 // Import an array type.
2560 // Return the backend representation of the element type.
2562 get_backend_element(Gogo
*, bool use_placeholder
);
2564 // Return the backend representation of the length.
2566 get_backend_length(Gogo
*);
2568 // Finish the backend representation of the element type.
2570 finish_backend_element(Gogo
*);
2573 make_array_type_descriptor_type();
2576 make_slice_type_descriptor_type();
2578 // Return whether this is a generated array that is not comparable.
2580 is_array_incomparable() const
2581 { return this->is_array_incomparable_
; }
2583 // Record that this is a generated array that is not comparable.
2585 set_is_array_incomparable()
2586 { this->is_array_incomparable_
= true; }
2588 // Write the hash function for this type.
2590 write_hash_function(Gogo
*, Named_type
*, Function_type
*, Function_type
*);
2592 // Write the equality function for this type.
2594 write_equal_function(Gogo
*, Named_type
*);
2598 do_traverse(Traverse
* traverse
);
2604 do_has_pointer() const;
2607 do_compare_is_identity(Gogo
*);
2612 return this->length_
!= NULL
&& this->element_type_
->is_reflexive();
2616 do_needs_key_update()
2617 { return this->element_type_
->needs_key_update(); }
2621 { return this->length_
== NULL
|| this->element_type_
->in_heap(); }
2624 do_hash_for_method(Gogo
*) const;
2627 do_get_backend(Gogo
*);
2630 do_type_descriptor(Gogo
*, Named_type
*);
2633 do_reflection(Gogo
*, std::string
*) const;
2636 do_mangled_name(Gogo
*, std::string
*) const;
2639 do_export(Export
*) const;
2646 array_type_descriptor(Gogo
*, Named_type
*);
2649 slice_type_descriptor(Gogo
*, Named_type
*);
2651 // The type of elements of the array.
2652 Type
* element_type_
;
2653 // The number of elements. This may be NULL.
2654 Expression
* length_
;
2655 // The backend representation of the length.
2656 // We only want to compute this once.
2657 Bexpression
* blength_
;
2658 // Whether or not an invalid length error has been issued for this type,
2659 // to avoid knock-on errors.
2660 mutable bool issued_length_error_
;
2661 // True if this is a generated array that is not considered to be
2663 bool is_array_incomparable_
;
2666 // The type of a map.
2668 class Map_type
: public Type
2671 Map_type(Type
* key_type
, Type
* val_type
, Location location
)
2673 key_type_(key_type
), val_type_(val_type
), hmap_type_(NULL
),
2674 bucket_type_(NULL
), hiter_type_(NULL
), location_(location
)
2677 // Return the key type.
2680 { return this->key_type_
; }
2682 // Return the value type.
2685 { return this->val_type_
; }
2687 // Return the type used for an iteration over this map.
2691 // If this map requires the "fat" functions, returns the pointer to
2692 // pass as the zero value to those functions. Otherwise, in the
2693 // normal case, returns NULL.
2695 fat_zero_value(Gogo
*);
2697 // Return whether VAR is the map zero value.
2699 is_zero_value(Variable
* var
);
2701 // Return the backend representation of the map zero value.
2703 backend_zero_value(Gogo
*);
2705 // Whether this type is identical with T.
2707 is_identical(const Map_type
* t
, Cmp_tags
,
2708 bool errors_are_identical
) const;
2710 // Import a map type.
2715 make_map_type_descriptor_type();
2719 do_traverse(Traverse
*);
2725 do_has_pointer() const
2729 do_compare_is_identity(Gogo
*)
2735 return this->key_type_
->is_reflexive() && this->val_type_
->is_reflexive();
2739 do_hash_for_method(Gogo
*) const;
2742 do_get_backend(Gogo
*);
2745 do_type_descriptor(Gogo
*, Named_type
*);
2748 do_reflection(Gogo
*, std::string
*) const;
2751 do_mangled_name(Gogo
*, std::string
*) const;
2754 do_export(Export
*) const;
2757 // These must be in sync with libgo/go/runtime/hashmap.go.
2758 static const int bucket_size
= 8;
2759 static const int max_key_size
= 128;
2760 static const int max_val_size
= 128;
2761 static const int max_zero_size
= 1024;
2763 // Maps with value types larger than max_zero_size require passing a
2764 // zero value pointer to the map functions.
2766 // The zero value variable.
2767 static Named_object
* zero_value
;
2769 // The current size of the zero value.
2770 static int64_t zero_value_size
;
2772 // The current alignment of the zero value.
2773 static int64_t zero_value_align
;
2776 bucket_type(Gogo
*, int64_t, int64_t);
2785 // The hashmap type. At run time a map is represented as a pointer
2788 // The bucket type, the type used to hold keys and values at run time.
2790 // The iterator type.
2792 // Where the type was defined.
2796 // The type of a channel.
2798 class Channel_type
: public Type
2801 Channel_type(bool may_send
, bool may_receive
, Type
* element_type
)
2802 : Type(TYPE_CHANNEL
),
2803 may_send_(may_send
), may_receive_(may_receive
),
2804 element_type_(element_type
)
2805 { go_assert(may_send
|| may_receive
); }
2807 // Whether this channel can send data.
2810 { return this->may_send_
; }
2812 // Whether this channel can receive data.
2815 { return this->may_receive_
; }
2817 // The type of the values that may be sent on this channel. This is
2818 // NULL if any type may be sent.
2820 element_type() const
2821 { return this->element_type_
; }
2823 // Whether this type is identical with T.
2825 is_identical(const Channel_type
* t
, Cmp_tags
,
2826 bool errors_are_identical
) const;
2828 // Import a channel type.
2829 static Channel_type
*
2833 make_chan_type_descriptor_type();
2836 select_type(int ncases
);
2840 do_traverse(Traverse
* traverse
)
2841 { return Type::traverse(this->element_type_
, traverse
); }
2847 do_has_pointer() const
2851 do_compare_is_identity(Gogo
*)
2855 do_hash_for_method(Gogo
*) const;
2858 do_get_backend(Gogo
*);
2861 do_type_descriptor(Gogo
*, Named_type
*);
2864 do_reflection(Gogo
*, std::string
*) const;
2867 do_mangled_name(Gogo
*, std::string
*) const;
2870 do_export(Export
*) const;
2873 // Whether this channel can send data.
2875 // Whether this channel can receive data.
2877 // The types of elements which may be sent on this channel. If this
2878 // is NULL, it means that any type may be sent.
2879 Type
* element_type_
;
2882 // An interface type.
2884 class Interface_type
: public Type
2887 Interface_type(Typed_identifier_list
* methods
, Location location
)
2888 : Type(TYPE_INTERFACE
),
2889 parse_methods_(methods
), all_methods_(NULL
), location_(location
),
2890 package_(NULL
), interface_btype_(NULL
), bmethods_(NULL
),
2891 assume_identical_(NULL
), methods_are_finalized_(false),
2892 bmethods_is_placeholder_(false), seen_(false)
2893 { go_assert(methods
== NULL
|| !methods
->empty()); }
2895 // The location where the interface type was defined.
2898 { return this->location_
; }
2900 // The package where the interface type was defined. Returns NULL
2901 // for the package currently being compiled.
2904 { return this->package_
; }
2906 // Return whether this is an empty interface.
2910 go_assert(this->methods_are_finalized_
);
2911 return this->all_methods_
== NULL
;
2914 // Return the list of methods. This will return NULL for an empty
2916 const Typed_identifier_list
*
2919 // Return the number of methods.
2921 method_count() const;
2923 // Return the method NAME, or NULL.
2924 const Typed_identifier
*
2925 find_method(const std::string
& name
) const;
2927 // Return the zero-based index of method NAME.
2929 method_index(const std::string
& name
) const;
2931 // Finalize the methods. This sets all_methods_. This handles
2932 // interface inheritance.
2936 // Return true if T implements this interface. If this returns
2937 // false, and REASON is not NULL, it sets *REASON to the reason that
2940 implements_interface(const Type
* t
, std::string
* reason
) const;
2942 // Whether this type is identical with T. REASON is as in
2943 // implements_interface.
2945 is_identical(const Interface_type
* t
, Cmp_tags
,
2946 bool errors_are_identical
) const;
2948 // Whether we can assign T to this type. is_identical is known to
2951 is_compatible_for_assign(const Interface_type
*, std::string
* reason
) const;
2953 // Return whether NAME is a method which is not exported. This is
2954 // only used for better error reporting.
2956 is_unexported_method(Gogo
*, const std::string
& name
) const;
2958 // Import an interface type.
2959 static Interface_type
*
2962 // Make a struct for an empty interface type.
2964 get_backend_empty_interface_type(Gogo
*);
2966 // Get a pointer to the backend representation of the method table.
2968 get_backend_methods(Gogo
*);
2970 // Return a placeholder for the backend representation of the
2971 // pointer to the method table.
2973 get_backend_methods_placeholder(Gogo
*);
2975 // Finish the backend representation of the method types.
2977 finish_backend_methods(Gogo
*);
2980 make_interface_type_descriptor_type();
2984 do_traverse(Traverse
*);
2987 do_has_pointer() const
2991 do_compare_is_identity(Gogo
*)
2994 // Not reflexive if it contains a float.
2999 // Distinction between +0 and -0 requires a key update if it
3000 // contains a float.
3002 do_needs_key_update()
3006 do_hash_for_method(Gogo
*) const;
3009 do_get_backend(Gogo
*);
3012 do_type_descriptor(Gogo
*, Named_type
*);
3015 do_reflection(Gogo
*, std::string
*) const;
3018 do_mangled_name(Gogo
*, std::string
*) const;
3021 do_export(Export
*) const;
3024 // This type guards against infinite recursion when comparing
3025 // interface types. We keep a list of interface types assumed to be
3026 // identical during comparison. We just keep the list on the stack.
3027 // This permits us to compare cases like
3028 // type I1 interface { F() interface{I1} }
3029 // type I2 interface { F() interface{I2} }
3030 struct Assume_identical
3032 Assume_identical
* next
;
3033 const Interface_type
* t1
;
3034 const Interface_type
* t2
;
3038 assume_identical(const Interface_type
*, const Interface_type
*) const;
3040 // The list of methods associated with the interface from the
3041 // parser. This will be NULL for the empty interface. This may
3042 // include unnamed interface types.
3043 Typed_identifier_list
* parse_methods_
;
3044 // The list of all methods associated with the interface. This
3045 // expands any interface types listed in methods_. It is set by
3046 // finalize_methods. This will be NULL for the empty interface.
3047 Typed_identifier_list
* all_methods_
;
3048 // The location where the interface was defined.
3050 // The package where the interface was defined. This is NULL for
3051 // the package being compiled.
3053 // The backend representation of this type during backend conversion.
3054 Btype
* interface_btype_
;
3055 // The backend representation of the pointer to the method table.
3057 // A list of interface types assumed to be identical during
3058 // interface comparison.
3059 mutable Assume_identical
* assume_identical_
;
3060 // Whether the methods have been finalized.
3061 bool methods_are_finalized_
;
3062 // Whether the bmethods_ field is a placeholder.
3063 bool bmethods_is_placeholder_
;
3064 // Used to avoid endless recursion in do_mangled_name.
3068 // The value we keep for a named type. This lets us get the right
3069 // name when we convert to backend. Note that we don't actually keep
3070 // the name here; the name is in the Named_object which points to
3071 // this. This object exists to hold a unique backend representation for
3074 class Named_type
: public Type
3077 Named_type(Named_object
* named_object
, Type
* type
, Location location
)
3079 named_object_(named_object
), in_function_(NULL
), in_function_index_(0),
3080 type_(type
), local_methods_(NULL
), all_methods_(NULL
),
3081 interface_method_tables_(NULL
), pointer_interface_method_tables_(NULL
),
3082 location_(location
), named_btype_(NULL
), dependencies_(),
3083 is_alias_(false), is_visible_(true), is_error_(false), in_heap_(true),
3084 is_placeholder_(false), is_converted_(false), is_circular_(false),
3085 is_verified_(false), seen_(false), seen_in_compare_is_identity_(false),
3086 seen_in_get_backend_(false), seen_alias_(false)
3089 // Return the associated Named_object. This holds the actual name.
3092 { return this->named_object_
; }
3095 named_object() const
3096 { return this->named_object_
; }
3098 // Set the Named_object. This is used when we see a type
3099 // declaration followed by a type.
3101 set_named_object(Named_object
* no
)
3102 { this->named_object_
= no
; }
3104 // Whether this is an alias (type T1 = T2) rather than an ordinary
3105 // named type (type T1 T2).
3108 { return this->is_alias_
; }
3110 // Record that this type is an alias.
3113 { this->is_alias_
= true; }
3115 // Mark this type as not permitted in the heap.
3118 { this->in_heap_
= false; }
3120 // Return the function in which this type is defined. This will
3121 // return NULL for a type defined in global scope.
3123 in_function(unsigned int *pindex
) const
3125 *pindex
= this->in_function_index_
;
3126 return this->in_function_
;
3129 // Set the function in which this type is defined.
3131 set_in_function(Named_object
* f
, unsigned int index
)
3133 this->in_function_
= f
;
3134 this->in_function_index_
= index
;
3137 // Return the name of the type.
3141 // Return the name of the type for an error message. The difference
3142 // is that if the type is defined in a different package, this will
3143 // return PACKAGE.NAME.
3145 message_name() const;
3147 // Return the underlying type.
3150 { return this->type_
; }
3154 { return this->type_
; }
3156 // Return the location.
3159 { return this->location_
; }
3161 // Whether this type is visible. This only matters when parsing.
3164 { return this->is_visible_
; }
3166 // Mark this type as visible.
3169 { this->is_visible_
= true; }
3171 // Mark this type as invisible.
3174 { this->is_visible_
= false; }
3176 // Whether this is a builtin type.
3179 { return Linemap::is_predeclared_location(this->location_
); }
3181 // Whether this named type is valid. A recursive named type is invalid.
3184 { return !this->is_error_
; }
3186 // Whether this is a circular type: a pointer or function type that
3187 // refers to itself, which is not possible in C.
3190 { return this->is_circular_
; }
3192 // Return the base type for this type.
3199 // Return whether this is an error type.
3201 is_named_error_type() const;
3203 // Return whether this type is comparable. If REASON is not NULL,
3204 // set *REASON when returning false.
3206 named_type_is_comparable(std::string
* reason
) const;
3208 // Add a method to this type.
3210 add_method(const std::string
& name
, Function
*);
3212 // Add a method declaration to this type.
3214 add_method_declaration(const std::string
& name
, Package
* package
,
3215 Function_type
* type
, Location location
);
3217 // Add an existing method--one defined before the type itself was
3218 // defined--to a type.
3220 add_existing_method(Named_object
*);
3222 // Look up a local method.
3224 find_local_method(const std::string
& name
) const;
3226 // Return the list of local methods.
3228 local_methods() const;
3230 // Build the complete list of methods, including those from
3231 // anonymous fields, and build method stubs if needed.
3233 finalize_methods(Gogo
*);
3235 // Return whether this type has any methods. This should only be
3236 // called after the finalize_methods pass.
3238 has_any_methods() const;
3240 // Return the methods for this type. This should only be called
3241 // after the finalized_methods pass.
3245 // Return the method to use for NAME. This returns NULL if there is
3246 // no such method or if the method is ambiguous. When it returns
3247 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
3249 method_function(const std::string
& name
, bool *is_ambiguous
) const;
3251 // Return whether NAME is a known field or method which is not
3252 // exported. This is only used for better error reporting.
3254 is_unexported_local_method(Gogo
*, const std::string
& name
) const;
3256 // Return a pointer to the interface method table for this type for
3257 // the interface INTERFACE. If IS_POINTER is true, set the type
3258 // descriptor to a pointer to this type, otherwise set it to this
3261 interface_method_table(Interface_type
* interface
, bool is_pointer
);
3263 // Note that a type must be converted to the backend representation
3264 // before we convert this type.
3266 add_dependency(Named_type
* nt
)
3267 { this->dependencies_
.push_back(nt
); }
3269 // Return true if the size and alignment of the backend
3270 // representation of this type is known. This is always true after
3271 // types have been converted, but may be false beforehand.
3273 is_named_backend_type_size_known() const
3274 { return this->named_btype_
!= NULL
&& !this->is_placeholder_
; }
3276 // Add to the reflection string as for Type::append_reflection, but
3277 // if USE_ALIAS use the alias name rather than the alias target.
3279 append_reflection_type_name(Gogo
*, bool use_alias
, std::string
*) const;
3281 // Append the mangled type name as for Type::append_mangled_name,
3282 // but if USE_ALIAS use the alias name rather than the alias target.
3284 append_mangled_type_name(Gogo
*, bool use_alias
, std::string
*) const;
3288 export_named_type(Export
*, const std::string
& name
) const;
3290 // Import a named type.
3292 import_named_type(Import
*, Named_type
**);
3294 // Initial conversion to backend representation.
3300 do_traverse(Traverse
* traverse
)
3301 { return Type::traverse(this->type_
, traverse
); }
3307 do_has_pointer() const;
3310 do_compare_is_identity(Gogo
*);
3316 do_needs_key_update();
3320 { return this->in_heap_
&& this->type_
->in_heap(); }
3323 do_hash_for_method(Gogo
*) const;
3326 do_get_backend(Gogo
*);
3329 do_type_descriptor(Gogo
*, Named_type
*);
3332 do_reflection(Gogo
*, std::string
*) const;
3335 do_mangled_name(Gogo
*, std::string
* ret
) const;
3338 do_export(Export
*) const;
3341 // Create the placeholder during conversion.
3343 create_placeholder(Gogo
*);
3345 // A pointer back to the Named_object for this type.
3346 Named_object
* named_object_
;
3347 // If this type is defined in a function, a pointer back to the
3348 // function in which it is defined.
3349 Named_object
* in_function_
;
3350 // The index of this type in IN_FUNCTION_.
3351 unsigned int in_function_index_
;
3354 // The list of methods defined for this type. Any named type can
3356 Bindings
* local_methods_
;
3357 // The full list of methods for this type, including methods
3358 // declared for anonymous fields.
3359 Methods
* all_methods_
;
3360 // A mapping from interfaces to the associated interface method
3361 // tables for this type.
3362 Interface_method_tables
* interface_method_tables_
;
3363 // A mapping from interfaces to the associated interface method
3364 // tables for pointers to this type.
3365 Interface_method_tables
* pointer_interface_method_tables_
;
3366 // The location where this type was defined.
3368 // The backend representation of this type during backend
3369 // conversion. This is used to avoid endless recursion when a named
3370 // type refers to itself.
3371 Btype
* named_btype_
;
3372 // A list of types which must be converted to the backend
3373 // representation before this type can be converted. This is for
3375 // type S1 { p *S2 }
3377 // where we can't convert S2 to the backend representation unless we
3378 // have converted S1.
3379 std::vector
<Named_type
*> dependencies_
;
3380 // Whether this is an alias type.
3382 // Whether this type is visible. This is false if this type was
3383 // created because it was referenced by an imported object, but the
3384 // type itself was not exported. This will always be true for types
3385 // created in the current package.
3387 // Whether this type is erroneous.
3389 // Whether this type is permitted in the heap. This is true by
3390 // default, false if there is a magic //go:notinheap comment.
3392 // Whether the current value of named_btype_ is a placeholder for
3393 // which the final size of the type is not known.
3394 bool is_placeholder_
;
3395 // Whether this type has been converted to the backend
3396 // representation. Implies that is_placeholder_ is false.
3398 // Whether this is a pointer or function type which refers to the
3401 // Whether this type has been verified.
3403 // In a recursive operation such as has_pointer, this flag is used
3404 // to prevent infinite recursion when a type refers to itself. This
3405 // is mutable because it is always reset to false when the function
3408 // Like seen_, but used only by do_compare_is_identity.
3409 bool seen_in_compare_is_identity_
;
3410 // Like seen_, but used only by do_get_backend.
3411 bool seen_in_get_backend_
;
3412 // Like seen_, but used when resolving aliases.
3413 mutable bool seen_alias_
;
3416 // A forward declaration. This handles a type which has been declared
3419 class Forward_declaration_type
: public Type
3422 Forward_declaration_type(Named_object
* named_object
);
3424 // The named object associated with this type declaration. This
3425 // will be resolved.
3430 named_object() const;
3432 // Return the name of the type.
3436 // Return the type to which this points. Give an error if the type
3437 // has not yet been defined.
3444 // Whether the base type has been defined.
3448 // Add a method to this type.
3450 add_method(const std::string
& name
, Function
*);
3452 // Add a method declaration to this type.
3454 add_method_declaration(const std::string
& name
, Package
*, Function_type
*,
3457 // Add an already created object as a method to this type.
3459 add_existing_method(Named_object
*);
3463 do_traverse(Traverse
* traverse
);
3469 do_has_pointer() const
3470 { return this->real_type()->has_pointer(); }
3473 do_compare_is_identity(Gogo
* gogo
)
3474 { return this->real_type()->compare_is_identity(gogo
); }
3478 { return this->real_type()->is_reflexive(); }
3481 do_needs_key_update()
3482 { return this->real_type()->needs_key_update(); }
3486 { return this->real_type()->in_heap(); }
3489 do_hash_for_method(Gogo
* gogo
) const
3490 { return this->real_type()->hash_for_method(gogo
); }
3493 do_get_backend(Gogo
* gogo
);
3496 do_type_descriptor(Gogo
*, Named_type
*);
3499 do_reflection(Gogo
*, std::string
*) const;
3502 do_mangled_name(Gogo
*, std::string
* ret
) const;
3505 do_export(Export
*) const;
3508 // Issue a warning about a use of an undefined type.
3512 // The type declaration.
3513 Named_object
* named_object_
;
3514 // Whether we have issued a warning about this type.
3515 mutable bool warned_
;
3518 // The Type_context struct describes what we expect for the type of an
3523 // The exact type we expect, if known. This may be NULL.
3525 // Whether an abstract type is permitted.
3526 bool may_be_abstract
;
3530 : type(NULL
), may_be_abstract(false)
3533 Type_context(Type
* a_type
, bool a_may_be_abstract
)
3534 : type(a_type
), may_be_abstract(a_may_be_abstract
)
3538 #endif // !defined(GO_TYPES_H)