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.
13 class Typed_identifier
;
14 class Typed_identifier_list
;
21 class Struct_field_list
;
29 class Forward_declaration_type
;
32 class Type_hash_identical
;
35 class Expression_list
;
36 class Call_expression
;
37 class Field_reference_expression
;
38 class Bound_method_expression
;
42 class Translate_context
;
46 // Type codes used in type descriptors. These must match the values
47 // in libgo/runtime/go-type.h. They also match the values in the gc
48 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
49 // although this is not required.
51 static const int RUNTIME_TYPE_KIND_BOOL
= 1;
52 static const int RUNTIME_TYPE_KIND_INT
= 2;
53 static const int RUNTIME_TYPE_KIND_INT8
= 3;
54 static const int RUNTIME_TYPE_KIND_INT16
= 4;
55 static const int RUNTIME_TYPE_KIND_INT32
= 5;
56 static const int RUNTIME_TYPE_KIND_INT64
= 6;
57 static const int RUNTIME_TYPE_KIND_UINT
= 7;
58 static const int RUNTIME_TYPE_KIND_UINT8
= 8;
59 static const int RUNTIME_TYPE_KIND_UINT16
= 9;
60 static const int RUNTIME_TYPE_KIND_UINT32
= 10;
61 static const int RUNTIME_TYPE_KIND_UINT64
= 11;
62 static const int RUNTIME_TYPE_KIND_UINTPTR
= 12;
63 static const int RUNTIME_TYPE_KIND_FLOAT32
= 13;
64 static const int RUNTIME_TYPE_KIND_FLOAT64
= 14;
65 static const int RUNTIME_TYPE_KIND_COMPLEX64
= 15;
66 static const int RUNTIME_TYPE_KIND_COMPLEX128
= 16;
67 static const int RUNTIME_TYPE_KIND_ARRAY
= 17;
68 static const int RUNTIME_TYPE_KIND_CHAN
= 18;
69 static const int RUNTIME_TYPE_KIND_FUNC
= 19;
70 static const int RUNTIME_TYPE_KIND_INTERFACE
= 20;
71 static const int RUNTIME_TYPE_KIND_MAP
= 21;
72 static const int RUNTIME_TYPE_KIND_PTR
= 22;
73 static const int RUNTIME_TYPE_KIND_SLICE
= 23;
74 static const int RUNTIME_TYPE_KIND_STRING
= 24;
75 static const int RUNTIME_TYPE_KIND_STRUCT
= 25;
76 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER
= 26;
78 // To build the complete list of methods for a named type we need to
79 // gather all methods from anonymous fields. Those methods may
80 // require an arbitrary set of indirections and field offsets. There
81 // is also the possibility of ambiguous methods, which we could ignore
82 // except that we want to give a better error message for that case.
83 // This is a base class. There are two types of methods: named
84 // methods, and methods which are inherited from an anonymous field of
90 // For methods in anonymous types we need to know the sequence of
91 // field references used to extract the pointer to pass to the
92 // method. Since each method for a particular anonymous field will
93 // have the sequence of field indexes, and since the indexes can be
94 // shared going down the chain, we use a manually managed linked
95 // list. The first entry in the list is the field index for the
96 // last field, the one passed to the method.
100 const Field_indexes
* next
;
101 unsigned int field_index
;
107 // Get the list of field indexes.
109 field_indexes() const
110 { return this->field_indexes_
; }
115 { return this->depth_
; }
117 // Return whether this is a value method--a method which does not
118 // require a pointer expression.
120 is_value_method() const
121 { return this->is_value_method_
; }
123 // Return whether we need a stub method--this is true if we can't
124 // just pass the main object to the method.
126 needs_stub_method() const
127 { return this->needs_stub_method_
; }
129 // Return whether this is an ambiguous method name.
132 { return this->is_ambiguous_
; }
134 // Note that this method is ambiguous.
137 { this->is_ambiguous_
= true; }
139 // Return the type of the method.
142 { return this->do_type(); }
144 // Return the location of the method receiver.
146 receiver_location() const
147 { return this->do_receiver_location(); }
149 // Return an expression which binds this method to EXPR. This is
150 // something which can be used with a function call.
152 bind_method(Expression
* expr
, source_location location
) const;
154 // Return the named object for this method. This may only be called
155 // after methods are finalized.
157 named_object() const;
159 // Get the stub object.
163 gcc_assert(this->stub_
!= NULL
);
167 // Set the stub object.
169 set_stub_object(Named_object
* no
)
171 gcc_assert(this->stub_
== NULL
);
176 // These objects are only built by the child classes.
177 Method(const Field_indexes
* field_indexes
, unsigned int depth
,
178 bool is_value_method
, bool needs_stub_method
)
179 : field_indexes_(field_indexes
), depth_(depth
), stub_(NULL
),
180 is_value_method_(is_value_method
), needs_stub_method_(needs_stub_method
),
184 // The named object for this method.
185 virtual Named_object
*
186 do_named_object() const = 0;
188 // The type of the method.
189 virtual Function_type
*
192 // Return the location of the method receiver.
193 virtual source_location
194 do_receiver_location() const = 0;
196 // Bind a method to an object.
198 do_bind_method(Expression
* expr
, source_location location
) const = 0;
201 // The sequence of field indexes used for this method. If this is
202 // NULL, then the method is defined for the current type.
203 const Field_indexes
* field_indexes_
;
204 // The depth at which this method was found.
206 // If a stub method is required, this is its object. This is only
207 // set after stub methods are built in finalize_methods.
209 // Whether this is a value method--a method that does not require a
211 bool is_value_method_
;
212 // Whether a stub method is required.
213 bool needs_stub_method_
;
214 // Whether this method is ambiguous.
218 // A named method. This is what you get with a method declaration,
219 // either directly on the type, or inherited from some anonymous
222 class Named_method
: public Method
225 Named_method(Named_object
* named_object
, const Field_indexes
* field_indexes
,
226 unsigned int depth
, bool is_value_method
,
227 bool needs_stub_method
)
228 : Method(field_indexes
, depth
, is_value_method
, needs_stub_method
),
229 named_object_(named_object
)
233 // Get the Named_object for the method.
235 do_named_object() const
236 { return this->named_object_
; }
238 // The type of the method.
242 // Return the location of the method receiver.
244 do_receiver_location() const;
246 // Bind a method to an object.
248 do_bind_method(Expression
* expr
, source_location location
) const;
251 // The method itself. For a method which needs a stub, this starts
252 // out as the underlying method, and is later replaced with the stub
254 Named_object
* named_object_
;
257 // An interface method. This is used when an interface appears as an
258 // anonymous field in a named struct.
260 class Interface_method
: public Method
263 Interface_method(const std::string
& name
, source_location location
,
264 Function_type
* fntype
, const Field_indexes
* field_indexes
,
266 : Method(field_indexes
, depth
, true, true),
267 name_(name
), location_(location
), fntype_(fntype
)
271 // Get the Named_object for the method. This should never be
272 // called, as we always create a stub.
274 do_named_object() const
275 { gcc_unreachable(); }
277 // The type of the method.
280 { return this->fntype_
; }
282 // Return the location of the method receiver.
284 do_receiver_location() const
285 { return this->location_
; }
287 // Bind a method to an object.
289 do_bind_method(Expression
* expr
, source_location location
) const;
292 // The name of the interface method to call.
294 // The location of the definition of the interface method.
295 source_location location_
;
296 // The type of the interface method.
297 Function_type
* fntype_
;
300 // A mapping from method name to Method. This is a wrapper around a
306 typedef Unordered_map(std::string
, Method
*) Method_map
;
309 typedef Method_map::const_iterator const_iterator
;
315 // Insert a new method. Returns true if it was inserted, false if
316 // it was overidden or ambiguous.
318 insert(const std::string
& name
, Method
* m
);
320 // The number of (unambiguous) methods.
327 { return this->methods_
.begin(); }
331 { return this->methods_
.end(); }
335 find(const std::string
& name
) const
336 { return this->methods_
.find(name
); }
342 // The base class for all types.
347 // The types of types.
348 enum Type_classification
361 TYPE_CALL_MULTIPLE_RESULT
,
381 // Get the unnamed bool type.
385 // Get the named type "bool".
389 // Make the named type "bool".
391 make_named_bool_type();
393 // Make an abstract integer type.
395 make_abstract_integer_type();
397 // Make a named integer type with a specified size.
398 // RUNTIME_TYPE_KIND is the code to use in reflection information,
399 // to distinguish int and int32.
401 make_integer_type(const char* name
, bool is_unsigned
, int bits
,
402 int runtime_type_kind
);
404 // Look up a named integer type.
406 lookup_integer_type(const char* name
);
408 // Make an abstract floating point type.
410 make_abstract_float_type();
412 // Make a named floating point type with a specific size.
413 // RUNTIME_TYPE_KIND is the code to use in reflection information,
414 // to distinguish float and float32.
416 make_float_type(const char* name
, int bits
, int runtime_type_kind
);
418 // Look up a named float type.
420 lookup_float_type(const char* name
);
422 // Make an abstract complex type.
424 make_abstract_complex_type();
426 // Make a named complex type with a specific size.
427 // RUNTIME_TYPE_KIND is the code to use in reflection information,
428 // to distinguish complex and complex64.
430 make_complex_type(const char* name
, int bits
, int runtime_type_kind
);
432 // Look up a named complex type.
434 lookup_complex_type(const char* name
);
436 // Get the unnamed string type.
440 // Get the named type "string".
442 lookup_string_type();
444 // Make the named type "string".
446 make_named_string_type();
451 static Function_type
*
452 make_function_type(Typed_identifier
* receiver
,
453 Typed_identifier_list
* parameters
,
454 Typed_identifier_list
* results
,
458 make_pointer_type(Type
*);
464 make_call_multiple_result_type(Call_expression
*);
467 make_struct_type(Struct_field_list
* fields
, source_location
);
470 make_array_type(Type
* element_type
, Expression
* length
);
473 make_map_type(Type
* key_type
, Type
* value_type
, source_location
);
476 make_channel_type(bool send
, bool receive
, Type
*);
478 static Interface_type
*
479 make_interface_type(Typed_identifier_list
* methods
, source_location
);
482 make_type_descriptor_type();
485 make_type_descriptor_ptr_type();
488 make_named_type(Named_object
*, Type
*, source_location
);
491 make_forward_declaration(Named_object
*);
495 traverse(Type
*, Traverse
*);
497 // Verify the type. This is called after parsing, and verifies that
498 // types are complete and meet the language requirements. This
499 // returns false if the type is invalid.
502 { return this->do_verify(); }
504 // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
505 // returns that an erroneous type is identical to any other type;
506 // this is used to avoid cascading errors. If this returns false,
507 // and REASON is not NULL, it may set *REASON.
509 are_identical(const Type
* lhs
, const Type
* rhs
, bool errors_are_identical
,
510 std::string
* reason
);
512 // Return true if two types are compatible for use in a binary
513 // operation, other than a shift, comparison, or channel send. This
514 // is an equivalence relation.
516 are_compatible_for_binop(const Type
* t1
, const Type
* t2
);
518 // Return true if a value with type RHS is assignable to a variable
519 // with type LHS. This is not an equivalence relation. If this
520 // returns false, and REASON is not NULL, it sets *REASON.
522 are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
);
524 // Return true if a value with type RHS may be converted to type
525 // LHS. If this returns false, and REASON is not NULL, it sets
528 are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
);
530 // Whether this type has any hidden fields which are not visible in
531 // the current compilation, such as a field whose name begins with a
532 // lower case letter in a struct imported from a different package.
533 // WITHIN is not NULL if we are looking at fields in a named type.
535 has_hidden_fields(const Named_type
* within
, std::string
* reason
) const;
537 // Return a hash code for this type for the method hash table.
538 // Types which are equivalent according to are_identical will have
539 // the same hash code.
541 hash_for_method(Gogo
*) const;
543 // Return the type classification.
545 classification() const
546 { return this->classification_
; }
548 // Return the base type for this type. This looks through forward
549 // declarations and names. Using this with a forward declaration
550 // which has not been defined will return an error type.
557 // Return the type skipping defined forward declarations. If this
558 // type is a forward declaration which has not been defined, it will
559 // return the Forward_declaration_type. This differs from base() in
560 // that it will return a Named_type, and for a
561 // Forward_declaration_type which is not defined it will return that
562 // type rather than an error type.
569 // Return true if this is a basic type: a type which is not composed
570 // of other types, and is not void.
572 is_basic_type() const;
574 // Return true if this is an abstract type--an integer, floating
575 // point, or complex type whose size has not been determined.
579 // Return a non-abstract version of an abstract type.
581 make_non_abstract_type();
583 // Return true if this type is or contains a pointer. This
584 // determines whether the garbage collector needs to look at a value
588 { return this->do_has_pointer(); }
590 // Return true if this is an error type. An error type indicates a
593 is_error_type() const;
595 // Return true if this is a void type.
598 { return this->classification_
== TYPE_VOID
; }
600 // If this is an integer type, return the Integer_type. Otherwise,
601 // return NULL. This is a controlled dynamic_cast.
604 { return this->convert
<Integer_type
, TYPE_INTEGER
>(); }
608 { return this->convert
<const Integer_type
, TYPE_INTEGER
>(); }
610 // If this is a floating point type, return the Float_type.
611 // Otherwise, return NULL. This is a controlled dynamic_cast.
614 { return this->convert
<Float_type
, TYPE_FLOAT
>(); }
618 { return this->convert
<const Float_type
, TYPE_FLOAT
>(); }
620 // If this is a complex type, return the Complex_type. Otherwise,
624 { return this->convert
<Complex_type
, TYPE_COMPLEX
>(); }
628 { return this->convert
<const Complex_type
, TYPE_COMPLEX
>(); }
630 // Return true if this is a boolean type.
632 is_boolean_type() const
633 { return this->base()->classification_
== TYPE_BOOLEAN
; }
635 // Return true if this is an abstract boolean type.
637 is_abstract_boolean_type() const
638 { return this->classification_
== TYPE_BOOLEAN
; }
640 // Return true if this is a string type.
642 is_string_type() const
643 { return this->base()->classification_
== TYPE_STRING
; }
645 // Return true if this is an abstract string type.
647 is_abstract_string_type() const
648 { return this->classification_
== TYPE_STRING
; }
650 // Return true if this is the sink type. This is the type of the
651 // blank identifier _.
654 { return this->base()->classification_
== TYPE_SINK
; }
656 // If this is a function type, return it. Otherwise, return NULL.
659 { return this->convert
<Function_type
, TYPE_FUNCTION
>(); }
662 function_type() const
663 { return this->convert
<const Function_type
, TYPE_FUNCTION
>(); }
665 // If this is a pointer type, return the type to which it points.
666 // Otherwise, return NULL.
670 // If this is a pointer type, return the type to which it points.
671 // Otherwise, return the type itself.
675 Type
* pt
= this->points_to();
676 return pt
!= NULL
? pt
: this;
682 const Type
* pt
= this->points_to();
683 return pt
!= NULL
? pt
: this;
686 // Return true if this is the nil type. We don't use base() here,
687 // because this can be called during parse, and there is no way to
688 // name the nil type anyhow.
691 { return this->classification_
== TYPE_NIL
; }
693 // Return true if this is the predeclared constant nil being used as
694 // a type. This is what the parser produces for type switches which
697 is_nil_constant_as_type() const;
699 // Return true if this is the return type of a function which
700 // returns multiple values.
702 is_call_multiple_result_type() const
703 { return this->base()->classification_
== TYPE_CALL_MULTIPLE_RESULT
; }
705 // If this is a struct type, return it. Otherwise, return NULL.
708 { return this->convert
<Struct_type
, TYPE_STRUCT
>(); }
712 { return this->convert
<const Struct_type
, TYPE_STRUCT
>(); }
714 // If this is an array type, return it. Otherwise, return NULL.
717 { return this->convert
<Array_type
, TYPE_ARRAY
>(); }
721 { return this->convert
<const Array_type
, TYPE_ARRAY
>(); }
723 // Return whether if this is an open array type.
725 is_open_array_type() const;
727 // If this is a map type, return it. Otherwise, return NULL.
730 { return this->convert
<Map_type
, TYPE_MAP
>(); }
734 { return this->convert
<const Map_type
, TYPE_MAP
>(); }
736 // If this is a channel type, return it. Otherwise, return NULL.
739 { return this->convert
<Channel_type
, TYPE_CHANNEL
>(); }
743 { return this->convert
<const Channel_type
, TYPE_CHANNEL
>(); }
745 // If this is an interface type, return it. Otherwise, return NULL.
748 { return this->convert
<Interface_type
, TYPE_INTERFACE
>(); }
750 const Interface_type
*
751 interface_type() const
752 { return this->convert
<const Interface_type
, TYPE_INTERFACE
>(); }
754 // If this is a named type, return it. Otherwise, return NULL.
761 // If this is a forward declaration, return it. Otherwise, return
763 Forward_declaration_type
*
764 forward_declaration_type()
765 { return this->convert_no_base
<Forward_declaration_type
, TYPE_FORWARD
>(); }
767 const Forward_declaration_type
*
768 forward_declaration_type() const
770 return this->convert_no_base
<const Forward_declaration_type
,
774 // Return true if this type is not yet defined.
776 is_undefined() const;
778 // Return true if this is the unsafe.pointer type. We currently
779 // represent that as pointer-to-void.
781 is_unsafe_pointer_type() const
782 { return this->points_to() != NULL
&& this->points_to()->is_void_type(); }
784 // Look for field or method NAME for TYPE. Return an expression for
785 // it, bound to EXPR.
787 bind_field_or_method(Gogo
*, const Type
* type
, Expression
* expr
,
788 const std::string
& name
, source_location
);
790 // Return true if NAME is an unexported field or method of TYPE.
792 is_unexported_field_or_method(Gogo
*, const Type
*, const std::string
&,
793 std::vector
<const Named_type
*>*);
795 // This type was passed to the builtin function make. ARGS are the
796 // arguments passed to make after the type; this may be NULL if
797 // there were none. Issue any required errors.
799 check_make_expression(Expression_list
* args
, source_location location
)
800 { return this->do_check_make_expression(args
, location
); }
802 // Return a tree representing this type.
806 // Return a tree representing a zero initialization for this type.
807 // This will be something like an INTEGER_CST or a CONSTRUCTOR. If
808 // IS_CLEAR is true, then the memory is known to be zeroed; in that
809 // case, this will return NULL if there is nothing to be done.
811 get_init_tree(Gogo
*, bool is_clear
);
813 // Like get_init_tree, but passing in the type to use for the
816 get_typed_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
817 { return this->do_get_init_tree(gogo
, type_tree
, is_clear
); }
819 // Return a tree for a make expression applied to this type.
821 make_expression_tree(Translate_context
* context
, Expression_list
* args
,
822 source_location location
)
823 { return this->do_make_expression_tree(context
, args
, location
); }
825 // Build a type descriptor entry for this type. Return a pointer to
828 type_descriptor_pointer(Gogo
* gogo
);
830 // Return the type reflection string for this type.
832 reflection(Gogo
*) const;
834 // Return a mangled name for the type. This is a name which can be
835 // used in assembler code. Identical types should have the same
838 mangled_name(Gogo
*) const;
842 export_type(Export
* exp
) const
843 { this->do_export(exp
); }
847 import_type(Import
*);
850 Type(Type_classification
);
852 // Functions implemented by the child class.
854 // Traverse the subtypes.
856 do_traverse(Traverse
*);
864 do_has_pointer() const
868 do_hash_for_method(Gogo
*) const;
871 do_check_make_expression(Expression_list
* args
, source_location
);
875 do_get_tree(Gogo
*) = 0;
878 do_get_init_tree(Gogo
*, tree
, bool) = 0;
881 do_make_expression_tree(Translate_context
*, Expression_list
*,
885 do_type_descriptor(Gogo
*, Named_type
* name
) = 0;
888 do_reflection(Gogo
*, std::string
*) const = 0;
892 do_mangled_name(Gogo
*, std::string
*) const = 0;
895 do_export(Export
*) const;
897 // Return whether an expression is an integer.
899 check_int_value(Expression
*, const char*, source_location
);
901 // Return whether a method expects a pointer as the receiver.
903 method_expects_pointer(const Named_object
*);
905 // Finalize the methods for a type.
907 finalize_methods(Gogo
*, const Type
*, source_location
, Methods
**);
909 // Return a method from a set of methods.
911 method_function(const Methods
*, const std::string
& name
,
914 // Return a composite literal for the type descriptor entry for a
917 type_descriptor(Gogo
*, Type
*);
919 // Return a composite literal for the type descriptor entry for
920 // TYPE, using NAME as the name of the type.
922 named_type_descriptor(Gogo
*, Type
* type
, Named_type
* name
);
924 // Return a composite literal for a plain type descriptor for this
925 // type with the given kind and name.
927 plain_type_descriptor(Gogo
*, int runtime_type_kind
, Named_type
* name
);
929 // Build a composite literal for the basic type descriptor.
931 type_descriptor_constructor(Gogo
*, int runtime_type_kind
, Named_type
*,
932 const Methods
*, bool only_value_methods
);
934 // Make a builtin struct type from a list of fields.
936 make_builtin_struct_type(int nfields
, ...);
938 // Make a builtin named type.
940 make_builtin_named_type(const char* name
, Type
* type
);
942 // For the benefit of child class reflection string generation.
944 append_reflection(const Type
* type
, Gogo
* gogo
, std::string
* ret
) const
945 { type
->do_reflection(gogo
, ret
); }
947 // For the benefit of child class mangling.
949 append_mangled_name(const Type
* type
, Gogo
* gogo
, std::string
* ret
) const
950 { type
->do_mangled_name(gogo
, ret
); }
952 // Incorporate a string into a hash code.
954 hash_string(const std::string
&, unsigned int);
956 // Return a tree for the underlying type of a named type.
958 get_named_type_tree(Gogo
* gogo
, Type
* base_type
)
959 { return base_type
->get_tree_without_hash(gogo
); }
962 // Convert to the desired type classification, or return NULL. This
963 // is a controlled dynamic_cast.
964 template<typename Type_class
, Type_classification type_classification
>
968 Type
* base
= this->base();
969 return (base
->classification_
== type_classification
970 ? static_cast<Type_class
*>(base
)
974 template<typename Type_class
, Type_classification type_classification
>
978 const Type
* base
= this->base();
979 return (base
->classification_
== type_classification
980 ? static_cast<Type_class
*>(base
)
984 template<typename Type_class
, Type_classification type_classification
>
988 return (this->classification_
== type_classification
989 ? static_cast<Type_class
*>(this)
993 template<typename Type_class
, Type_classification type_classification
>
995 convert_no_base() const
997 return (this->classification_
== type_classification
998 ? static_cast<Type_class
*>(this)
1002 // Get the hash and equality functions for a type.
1004 type_functions(const char** hash_fn
, const char** equal_fn
) const;
1006 // Build a composite literal for the uncommon type information.
1008 uncommon_type_constructor(Gogo
*, Type
* uncommon_type
,
1009 Named_type
*, const Methods
*,
1010 bool only_value_methods
) const;
1012 // Build a composite literal for the methods.
1014 methods_constructor(Gogo
*, Type
* methods_type
, const Methods
*,
1015 bool only_value_methods
) const;
1017 // Build a composite literal for one method.
1019 method_constructor(Gogo
*, Type
* method_type
, const std::string
& name
,
1020 const Method
*) const;
1023 build_receive_return_type(tree type
);
1025 // A hash table we use to avoid infinite recursion.
1026 typedef Unordered_set_hash(const Named_type
*, Type_hash_identical
,
1027 Type_identical
) Types_seen
;
1029 // Add all methods for TYPE to the list of methods for THIS.
1031 add_methods_for_type(const Type
* type
, const Method::Field_indexes
*,
1032 unsigned int depth
, bool, bool, Types_seen
*,
1036 add_local_methods_for_type(const Named_type
* type
,
1037 const Method::Field_indexes
*,
1038 unsigned int depth
, bool, bool, Methods
**);
1041 add_embedded_methods_for_type(const Type
* type
,
1042 const Method::Field_indexes
*,
1043 unsigned int depth
, bool, bool, Types_seen
*,
1047 add_interface_methods_for_type(const Type
* type
,
1048 const Method::Field_indexes
*,
1049 unsigned int depth
, Methods
**);
1051 // Build stub methods for a type.
1053 build_stub_methods(Gogo
*, const Type
* type
, const Methods
* methods
,
1057 build_one_stub_method(Gogo
*, Method
*, const char* receiver_name
,
1058 const Typed_identifier_list
*, bool is_varargs
,
1062 apply_field_indexes(Expression
*, const Method::Field_indexes
*,
1065 // Look for a field or method named NAME in TYPE.
1067 find_field_or_method(const Type
* type
, const std::string
& name
,
1068 bool receiver_can_be_pointer
,
1069 std::vector
<const Named_type
*>*, int* level
,
1070 bool* is_method
, bool* found_pointer_method
,
1071 std::string
* ambig1
, std::string
* ambig2
);
1073 // Get a tree for a type without looking in the hash table for
1076 get_tree_without_hash(Gogo
*);
1078 // A mapping from Type to tree, used to ensure that the GIMPLE
1079 // representation of identical types is identical.
1080 typedef Unordered_map_hash(const Type
*, tree
, Type_hash_identical
,
1081 Type_identical
) Type_trees
;
1083 static Type_trees type_trees
;
1085 // The type classification.
1086 Type_classification classification_
;
1087 // The tree representation of the type, once it has been determined.
1089 // The decl for the type descriptor for this type. This starts out
1090 // as NULL and is filled in as needed.
1091 tree type_descriptor_decl_
;
1094 // Type hash table operations.
1096 class Type_hash_identical
1100 operator()(const Type
* type
) const
1101 { return type
->hash_for_method(NULL
); }
1104 class Type_identical
1108 operator()(const Type
* t1
, const Type
* t2
) const
1109 { return Type::are_identical(t1
, t2
, false, NULL
); }
1112 // An identifier with a type.
1114 class Typed_identifier
1117 Typed_identifier(const std::string
& name
, Type
* type
,
1118 source_location location
)
1119 : name_(name
), type_(type
), location_(location
)
1125 { return this->name_
; }
1130 { return this->type_
; }
1132 // Return the location where the name was seen. This is not always
1136 { return this->location_
; }
1138 // Set the type--sometimes we see the identifier before the type.
1140 set_type(Type
* type
)
1142 gcc_assert(this->type_
== NULL
|| type
->is_error_type());
1151 // The location where the name was seen.
1152 source_location location_
;
1155 // A list of Typed_identifiers.
1157 class Typed_identifier_list
1160 Typed_identifier_list()
1164 // Whether the list is empty.
1167 { return this->entries_
.empty(); }
1169 // Return the number of entries in the list.
1172 { return this->entries_
.size(); }
1174 // Add an entry to the end of the list.
1176 push_back(const Typed_identifier
& td
)
1177 { this->entries_
.push_back(td
); }
1179 // Remove an entry from the end of the list.
1182 { this->entries_
.pop_back(); }
1184 // Set the type of entry I to TYPE.
1186 set_type(size_t i
, Type
* type
)
1188 gcc_assert(i
< this->entries_
.size());
1189 this->entries_
[i
].set_type(type
);
1192 // Sort the entries by name.
1198 traverse(Traverse
*);
1200 // Return the first and last elements.
1203 { return this->entries_
.front(); }
1205 const Typed_identifier
&
1207 { return this->entries_
.front(); }
1211 { return this->entries_
.back(); }
1213 const Typed_identifier
&
1215 { return this->entries_
.back(); }
1217 const Typed_identifier
&
1219 { return this->entries_
.at(i
); }
1222 set(size_t i
, const Typed_identifier
& t
)
1223 { this->entries_
.at(i
) = t
; }
1228 gcc_assert(c
<= this->entries_
.size());
1229 this->entries_
.resize(c
, Typed_identifier("", NULL
, UNKNOWN_LOCATION
));
1234 typedef std::vector
<Typed_identifier
>::iterator iterator
;
1235 typedef std::vector
<Typed_identifier
>::const_iterator const_iterator
;
1239 { return this->entries_
.begin(); }
1243 { return this->entries_
.begin(); }
1247 { return this->entries_
.end(); }
1251 { return this->entries_
.end(); }
1253 // Return a copy of this list. This returns an independent copy of
1254 // the vector, but does not copy the types.
1255 Typed_identifier_list
*
1259 std::vector
<Typed_identifier
> entries_
;
1262 // The type of an integer.
1264 class Integer_type
: public Type
1267 // Create a new integer type.
1269 create_integer_type(const char* name
, bool is_unsigned
, int bits
,
1270 int runtime_type_kind
);
1272 // Look up an existing integer type.
1274 lookup_integer_type(const char* name
);
1276 // Create an abstract integer type.
1277 static Integer_type
*
1278 create_abstract_integer_type();
1280 // Whether this is an abstract integer type.
1283 { return this->is_abstract_
; }
1285 // Whether this is an unsigned type.
1288 { return this->is_unsigned_
; }
1290 // The number of bits.
1293 { return this->bits_
; }
1295 // Whether this type is the same as T.
1297 is_identical(const Integer_type
* t
) const;
1301 do_hash_for_method(Gogo
*) const;
1307 do_get_init_tree(Gogo
*, tree
, bool);
1310 do_type_descriptor(Gogo
*, Named_type
*);
1313 do_reflection(Gogo
*, std::string
*) const;
1316 do_mangled_name(Gogo
*, std::string
*) const;
1319 Integer_type(bool is_abstract
, bool is_unsigned
, int bits
,
1320 int runtime_type_kind
)
1321 : Type(TYPE_INTEGER
),
1322 is_abstract_(is_abstract
), is_unsigned_(is_unsigned
), bits_(bits
),
1323 runtime_type_kind_(runtime_type_kind
)
1326 // Map names of integer types to the types themselves.
1327 typedef std::map
<std::string
, Named_type
*> Named_integer_types
;
1328 static Named_integer_types named_integer_types
;
1330 // True if this is an abstract type.
1332 // True if this is an unsigned type.
1334 // The number of bits.
1336 // The runtime type code used in the type descriptor for this type.
1337 int runtime_type_kind_
;
1340 // The type of a floating point number.
1342 class Float_type
: public Type
1345 // Create a new float type.
1347 create_float_type(const char* name
, int bits
, int runtime_type_kind
);
1349 // Look up an existing float type.
1351 lookup_float_type(const char* name
);
1353 // Create an abstract float type.
1355 create_abstract_float_type();
1357 // Whether this is an abstract float type.
1360 { return this->is_abstract_
; }
1362 // The number of bits.
1365 { return this->bits_
; }
1367 // Whether this type is the same as T.
1369 is_identical(const Float_type
* t
) const;
1371 // Return a tree for this type without using a Gogo*.
1377 do_hash_for_method(Gogo
*) const;
1383 do_get_init_tree(Gogo
*, tree
, bool);
1386 do_type_descriptor(Gogo
*, Named_type
*);
1389 do_reflection(Gogo
*, std::string
*) const;
1392 do_mangled_name(Gogo
*, std::string
*) const;
1395 Float_type(bool is_abstract
, int bits
, int runtime_type_kind
)
1397 is_abstract_(is_abstract
), bits_(bits
),
1398 runtime_type_kind_(runtime_type_kind
)
1401 // Map names of float types to the types themselves.
1402 typedef std::map
<std::string
, Named_type
*> Named_float_types
;
1403 static Named_float_types named_float_types
;
1405 // True if this is an abstract type.
1407 // The number of bits in the floating point value.
1409 // The runtime type code used in the type descriptor for this type.
1410 int runtime_type_kind_
;
1413 // The type of a complex number.
1415 class Complex_type
: public Type
1418 // Create a new complex type.
1420 create_complex_type(const char* name
, int bits
, int runtime_type_kind
);
1422 // Look up an existing complex type.
1424 lookup_complex_type(const char* name
);
1426 // Create an abstract complex type.
1427 static Complex_type
*
1428 create_abstract_complex_type();
1430 // Whether this is an abstract complex type.
1433 { return this->is_abstract_
; }
1435 // The number of bits: 64 or 128.
1437 { return this->bits_
; }
1439 // Whether this type is the same as T.
1441 is_identical(const Complex_type
* t
) const;
1443 // Return a tree for this type without using a Gogo*.
1449 do_hash_for_method(Gogo
*) const;
1455 do_get_init_tree(Gogo
*, tree
, bool);
1458 do_type_descriptor(Gogo
*, Named_type
*);
1461 do_reflection(Gogo
*, std::string
*) const;
1464 do_mangled_name(Gogo
*, std::string
*) const;
1467 Complex_type(bool is_abstract
, int bits
, int runtime_type_kind
)
1468 : Type(TYPE_COMPLEX
),
1469 is_abstract_(is_abstract
), bits_(bits
),
1470 runtime_type_kind_(runtime_type_kind
)
1473 // Map names of complex types to the types themselves.
1474 typedef std::map
<std::string
, Named_type
*> Named_complex_types
;
1475 static Named_complex_types named_complex_types
;
1477 // True if this is an abstract type.
1479 // The number of bits in the complex value--64 or 128.
1481 // The runtime type code used in the type descriptor for this type.
1482 int runtime_type_kind_
;
1485 // The type of a string.
1487 class String_type
: public Type
1494 // Return a tree for the length of STRING.
1496 length_tree(Gogo
*, tree string
);
1498 // Return a tree which points to the bytes of STRING.
1500 bytes_tree(Gogo
*, tree string
);
1504 do_has_pointer() const
1511 do_get_init_tree(Gogo
* gogo
, tree
, bool);
1514 do_type_descriptor(Gogo
*, Named_type
*);
1517 do_reflection(Gogo
*, std::string
*) const;
1520 do_mangled_name(Gogo
*, std::string
* ret
) const;
1523 // The named string type.
1524 static Named_type
* string_type_
;
1527 // The type of a function.
1529 class Function_type
: public Type
1532 Function_type(Typed_identifier
* receiver
, Typed_identifier_list
* parameters
,
1533 Typed_identifier_list
* results
, source_location location
)
1534 : Type(TYPE_FUNCTION
),
1535 receiver_(receiver
), parameters_(parameters
), results_(results
),
1536 location_(location
), is_varargs_(false), is_builtin_(false)
1539 // Get the receiver.
1540 const Typed_identifier
*
1542 { return this->receiver_
; }
1544 // Get the return names and types.
1545 const Typed_identifier_list
*
1547 { return this->results_
; }
1549 // Get the parameter names and types.
1550 const Typed_identifier_list
*
1552 { return this->parameters_
; }
1554 // Whether this is a varargs function.
1557 { return this->is_varargs_
; }
1559 // Whether this is a builtin function.
1562 { return this->is_builtin_
; }
1564 // The location where this type was defined.
1567 { return this->location_
; }
1569 // Return whether this is a method type.
1572 { return this->receiver_
!= NULL
; }
1574 // Whether T is a valid redeclaration of this type. This is called
1575 // when a function is declared more than once.
1577 is_valid_redeclaration(const Function_type
* t
, std::string
*) const;
1579 // Whether this type is the same as T.
1581 is_identical(const Function_type
* t
, bool ignore_receiver
,
1582 bool errors_are_identical
, std::string
*) const;
1584 // Record that this is a varargs function.
1587 { this->is_varargs_
= true; }
1589 // Record that this is a builtin function.
1592 { this->is_builtin_
= true; }
1594 // Import a function type.
1595 static Function_type
*
1598 // Return a copy of this type without a receiver. This is only
1599 // valid for a method type.
1601 copy_without_receiver() const;
1603 // Return a copy of this type with a receiver. This is used when an
1604 // interface method is attached to a named or struct type.
1606 copy_with_receiver(Type
*) const;
1610 do_traverse(Traverse
*);
1612 // A trampoline function has a pointer which matters for GC.
1614 do_has_pointer() const
1618 do_hash_for_method(Gogo
*) const;
1624 do_get_init_tree(Gogo
*, tree
, bool);
1627 do_type_descriptor(Gogo
*, Named_type
*);
1630 do_reflection(Gogo
*, std::string
*) const;
1633 do_mangled_name(Gogo
*, std::string
*) const;
1636 do_export(Export
*) const;
1640 make_function_type_descriptor_type();
1643 type_descriptor_params(Type
*, const Typed_identifier
*,
1644 const Typed_identifier_list
*);
1646 // The receiver name and type. This will be NULL for a normal
1647 // function, non-NULL for a method.
1648 Typed_identifier
* receiver_
;
1649 // The parameter names and types.
1650 Typed_identifier_list
* parameters_
;
1651 // The result names and types. This will be NULL if no result was
1653 Typed_identifier_list
* results_
;
1654 // The location where this type was defined. This exists solely to
1655 // give a location for the fields of the struct if this function
1656 // returns multiple values.
1657 source_location location_
;
1658 // Whether this function takes a variable number of arguments.
1660 // Whether this is a special builtin function which can not simply
1661 // be called. This is used for len, cap, etc.
1665 // The type of a pointer.
1667 class Pointer_type
: public Type
1670 Pointer_type(Type
* to_type
)
1671 : Type(TYPE_POINTER
),
1677 { return this->to_type_
; }
1679 // Import a pointer type.
1680 static Pointer_type
*
1685 do_traverse(Traverse
*);
1688 do_has_pointer() const
1692 do_hash_for_method(Gogo
*) const;
1698 do_get_init_tree(Gogo
*, tree
, bool);
1701 do_type_descriptor(Gogo
*, Named_type
*);
1704 do_reflection(Gogo
*, std::string
*) const;
1707 do_mangled_name(Gogo
*, std::string
*) const;
1710 do_export(Export
*) const;
1714 make_pointer_type_descriptor_type();
1716 // The type to which this type points.
1720 // The type of a field in a struct.
1725 explicit Struct_field(const Typed_identifier
& typed_identifier
)
1726 : typed_identifier_(typed_identifier
), tag_(NULL
)
1736 { return this->typed_identifier_
.type(); }
1738 // The field location.
1741 { return this->typed_identifier_
.location(); }
1743 // Whether the field has a tag.
1746 { return this->tag_
!= NULL
; }
1752 gcc_assert(this->tag_
!= NULL
);
1756 // Whether this is an anonymous field.
1758 is_anonymous() const
1759 { return this->typed_identifier_
.name().empty(); }
1761 // Set the tag. FIXME: This is never freed.
1763 set_tag(const std::string
& tag
)
1764 { this->tag_
= new std::string(tag
); }
1766 // Set the type. This is only used in error cases.
1768 set_type(Type
* type
)
1769 { this->typed_identifier_
.set_type(type
); }
1772 // The field name, type, and location.
1773 Typed_identifier typed_identifier_
;
1774 // The field tag. This is NULL if the field has no tag.
1778 // A list of struct fields.
1780 class Struct_field_list
1787 // Whether the list is empty.
1790 { return this->entries_
.empty(); }
1792 // Return the number of entries.
1795 { return this->entries_
.size(); }
1797 // Add an entry to the end of the list.
1799 push_back(const Struct_field
& sf
)
1800 { this->entries_
.push_back(sf
); }
1802 // Index into the list.
1805 { return this->entries_
.at(i
); }
1807 // Last entry in list.
1810 { return this->entries_
.back(); }
1814 typedef std::vector
<Struct_field
>::iterator iterator
;
1815 typedef std::vector
<Struct_field
>::const_iterator const_iterator
;
1819 { return this->entries_
.begin(); }
1823 { return this->entries_
.begin(); }
1827 { return this->entries_
.end(); }
1831 { return this->entries_
.end(); }
1834 std::vector
<Struct_field
> entries_
;
1837 // The type of a struct.
1839 class Struct_type
: public Type
1842 Struct_type(Struct_field_list
* fields
, source_location location
)
1843 : Type(TYPE_STRUCT
),
1844 fields_(fields
), location_(location
), all_methods_(NULL
),
1848 // Return the field NAME. This only looks at local fields, not at
1849 // embedded types. If the field is found, and PINDEX is not NULL,
1850 // this sets *PINDEX to the field index. If the field is not found,
1851 // this returns NULL.
1853 find_local_field(const std::string
& name
, unsigned int *pindex
) const;
1855 // Return the field number INDEX.
1857 field(unsigned int index
) const
1858 { return &this->fields_
->at(index
); }
1860 // Get the struct fields.
1861 const Struct_field_list
*
1863 { return this->fields_
; }
1865 // Return the number of fields.
1868 { return this->fields_
->size(); }
1870 // Push a new field onto the end of the struct. This is used when
1871 // building a closure variable.
1873 push_field(const Struct_field
& sf
)
1874 { this->fields_
->push_back(sf
); }
1876 // Return an expression referring to field NAME in STRUCT_EXPR, or
1877 // NULL if there is no field with that name.
1878 Field_reference_expression
*
1879 field_reference(Expression
* struct_expr
, const std::string
& name
,
1880 source_location
) const;
1882 // Return the total number of fields, including embedded fields.
1883 // This is the number of values which can appear in a conversion to
1886 total_field_count() const;
1888 // Whether this type is identical with T.
1890 is_identical(const Struct_type
* t
, bool errors_are_identical
) const;
1892 // Whether this struct type has any hidden fields. This returns
1893 // true if any fields have hidden names, or if any non-pointer
1894 // anonymous fields have types with hidden fields.
1896 struct_has_hidden_fields(const Named_type
* within
, std::string
*) const;
1898 // Return whether NAME is a local field which is not exported. This
1899 // is only used for better error reporting.
1901 is_unexported_local_field(Gogo
*, const std::string
& name
) const;
1903 // If this is an unnamed struct, build the complete list of methods,
1904 // including those from anonymous fields, and build methods stubs if
1907 finalize_methods(Gogo
*);
1909 // Return whether this type has any methods. This should only be
1910 // called after the finalize_methods pass.
1912 has_any_methods() const
1913 { return this->all_methods_
!= NULL
; }
1915 // Return the methods for tihs type. This should only be called
1916 // after the finalize_methods pass.
1919 { return this->all_methods_
; }
1921 // Return the method to use for NAME. This returns NULL if there is
1922 // no such method or if the method is ambiguous. When it returns
1923 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
1925 method_function(const std::string
& name
, bool* is_ambiguous
) const;
1927 // Traverse just the field types of a struct type.
1929 traverse_field_types(Traverse
* traverse
)
1930 { return this->do_traverse(traverse
); }
1932 // Import a struct type.
1936 // Fill in the fields for a named struct type.
1938 fill_in_tree(Gogo
*, tree
);
1940 // Note that a struct must be converted to the backend
1941 // representation before we convert this struct.
1943 add_prerequisite(Named_type
* nt
)
1944 { this->prerequisites_
.push_back(nt
); }
1946 // If there are any structs which must be converted to the backend
1947 // representation before this one, convert them.
1949 convert_prerequisites(Gogo
*);
1953 do_traverse(Traverse
*);
1959 do_has_pointer() const;
1962 do_hash_for_method(Gogo
*) const;
1968 do_get_init_tree(Gogo
*, tree
, bool);
1971 do_type_descriptor(Gogo
*, Named_type
*);
1974 do_reflection(Gogo
*, std::string
*) const;
1977 do_mangled_name(Gogo
*, std::string
*) const;
1980 do_export(Export
*) const;
1983 Field_reference_expression
*
1984 field_reference_depth(Expression
* struct_expr
, const std::string
& name
,
1985 source_location
, unsigned int* depth
) const;
1988 make_struct_type_descriptor_type();
1990 // The fields of the struct.
1991 Struct_field_list
* fields_
;
1992 // The place where the struct was declared.
1993 source_location location_
;
1994 // If this struct is unnamed, a list of methods.
1995 Methods
* all_methods_
;
1996 // A list of structs which must be converted to the backend
1997 // representation before this struct can be converted. This is for
1999 // type S1 { p *S2 }
2001 // where we must start converting S2 before we start converting S1.
2002 // That is because we can fully convert S1 before S2 is complete,
2003 // but we can not fully convert S2 before S1 is complete. If we
2004 // start converting S1 first, we won't be able to convert S2.
2005 std::vector
<Named_type
*> prerequisites_
;
2008 // The type of an array.
2010 class Array_type
: public Type
2013 Array_type(Type
* element_type
, Expression
* length
)
2015 element_type_(element_type
), length_(length
), length_tree_(NULL
)
2018 // Return the element type.
2020 element_type() const
2021 { return this->element_type_
; }
2023 // Return the length. This will return NULL for an open array.
2026 { return this->length_
; }
2028 // Whether this type is identical with T.
2030 is_identical(const Array_type
* t
, bool errors_are_identical
) const;
2032 // Whether this type has any hidden fields.
2034 array_has_hidden_fields(const Named_type
* within
, std::string
* reason
) const
2035 { return this->element_type_
->has_hidden_fields(within
, reason
); }
2037 // Return a tree for the pointer to the values in an array.
2039 value_pointer_tree(Gogo
*, tree array
) const;
2041 // Return a tree for the length of an array with this type.
2043 length_tree(Gogo
*, tree array
);
2045 // Return a tree for the capacity of an array with this type.
2047 capacity_tree(Gogo
*, tree array
);
2049 // Import an array type.
2053 // Fill in the fields for a named slice type.
2055 fill_in_tree(Gogo
*, tree
);
2059 do_traverse(Traverse
* traverse
);
2065 do_has_pointer() const
2067 return this->length_
== NULL
|| this->element_type_
->has_pointer();
2071 do_hash_for_method(Gogo
*) const;
2074 do_check_make_expression(Expression_list
*, source_location
);
2080 do_get_init_tree(Gogo
*, tree
, bool);
2083 do_make_expression_tree(Translate_context
*, Expression_list
*,
2087 do_type_descriptor(Gogo
*, Named_type
*);
2090 do_reflection(Gogo
*, std::string
*) const;
2093 do_mangled_name(Gogo
*, std::string
*) const;
2096 do_export(Export
*) const;
2103 get_length_tree(Gogo
*);
2106 make_array_type_descriptor_type();
2109 make_slice_type_descriptor_type();
2112 array_type_descriptor(Gogo
*, Named_type
*);
2115 slice_type_descriptor(Gogo
*, Named_type
*);
2117 // The type of elements of the array.
2118 Type
* element_type_
;
2119 // The number of elements. This may be NULL.
2120 Expression
* length_
;
2121 // The length as a tree. We only want to compute this once.
2125 // The type of a map.
2127 class Map_type
: public Type
2130 Map_type(Type
* key_type
, Type
* val_type
, source_location location
)
2132 key_type_(key_type
), val_type_(val_type
), location_(location
)
2135 // Return the key type.
2138 { return this->key_type_
; }
2140 // Return the value type.
2143 { return this->val_type_
; }
2145 // Whether this type is identical with T.
2147 is_identical(const Map_type
* t
, bool errors_are_identical
) const;
2149 // Import a map type.
2155 do_traverse(Traverse
*);
2161 do_has_pointer() const
2165 do_hash_for_method(Gogo
*) const;
2168 do_check_make_expression(Expression_list
*, source_location
);
2174 do_get_init_tree(Gogo
*, tree
, bool);
2177 do_make_expression_tree(Translate_context
*, Expression_list
*,
2181 do_type_descriptor(Gogo
*, Named_type
*);
2184 do_reflection(Gogo
*, std::string
*) const;
2187 do_mangled_name(Gogo
*, std::string
*) const;
2190 do_export(Export
*) const;
2194 make_map_type_descriptor_type();
2200 // Where the type was defined.
2201 source_location location_
;
2204 // The type of a channel.
2206 class Channel_type
: public Type
2209 Channel_type(bool may_send
, bool may_receive
, Type
* element_type
)
2210 : Type(TYPE_CHANNEL
),
2211 may_send_(may_send
), may_receive_(may_receive
),
2212 element_type_(element_type
)
2213 { gcc_assert(may_send
|| may_receive
); }
2215 // Whether this channel can send data.
2218 { return this->may_send_
; }
2220 // Whether this channel can receive data.
2223 { return this->may_receive_
; }
2225 // The type of the values that may be sent on this channel. This is
2226 // NULL if any type may be sent.
2228 element_type() const
2229 { return this->element_type_
; }
2231 // Whether this type is identical with T.
2233 is_identical(const Channel_type
* t
, bool errors_are_identical
) const;
2235 // Import a channel type.
2236 static Channel_type
*
2241 do_traverse(Traverse
* traverse
)
2242 { return Type::traverse(this->element_type_
, traverse
); }
2245 do_has_pointer() const
2249 do_hash_for_method(Gogo
*) const;
2252 do_check_make_expression(Expression_list
*, source_location
);
2258 do_get_init_tree(Gogo
*, tree
, bool);
2261 do_make_expression_tree(Translate_context
*, Expression_list
*,
2265 do_type_descriptor(Gogo
*, Named_type
*);
2268 do_reflection(Gogo
*, std::string
*) const;
2271 do_mangled_name(Gogo
*, std::string
*) const;
2274 do_export(Export
*) const;
2278 make_chan_type_descriptor_type();
2280 // Whether this channel can send data.
2282 // Whether this channel can receive data.
2284 // The types of elements which may be sent on this channel. If this
2285 // is NULL, it means that any type may be sent.
2286 Type
* element_type_
;
2289 // An interface type.
2291 class Interface_type
: public Type
2294 Interface_type(Typed_identifier_list
* methods
, source_location location
)
2295 : Type(TYPE_INTERFACE
),
2296 methods_(methods
), location_(location
)
2297 { gcc_assert(methods
== NULL
|| !methods
->empty()); }
2299 // Return whether this is an empty interface.
2302 { return this->methods_
== NULL
; }
2304 // Return the list of methods. This will return NULL for an empty
2306 const Typed_identifier_list
*
2308 { return this->methods_
; }
2310 // Return the number of methods.
2312 method_count() const
2313 { return this->methods_
== NULL
? 0 : this->methods_
->size(); }
2315 // Return the method NAME, or NULL.
2316 const Typed_identifier
*
2317 find_method(const std::string
& name
) const;
2319 // Return the zero-based index of method NAME.
2321 method_index(const std::string
& name
) const;
2323 // Finalize the methods. This handles interface inheritance.
2327 // Return true if T implements this interface. If this returns
2328 // false, and REASON is not NULL, it sets *REASON to the reason that
2331 implements_interface(const Type
* t
, std::string
* reason
) const;
2333 // Whether this type is identical with T. REASON is as in
2334 // implements_interface.
2336 is_identical(const Interface_type
* t
, bool errors_are_identical
) const;
2338 // Whether we can assign T to this type. is_identical is known to
2341 is_compatible_for_assign(const Interface_type
*, std::string
* reason
) const;
2343 // Return whether NAME is a method which is not exported. This is
2344 // only used for better error reporting.
2346 is_unexported_method(Gogo
*, const std::string
& name
) const;
2348 // Import an interface type.
2349 static Interface_type
*
2352 // Fill in the fields for a named interface type.
2354 fill_in_tree(Gogo
*, tree
);
2358 do_traverse(Traverse
*);
2361 do_has_pointer() const
2365 do_hash_for_method(Gogo
*) const;
2371 do_get_init_tree(Gogo
* gogo
, tree
, bool);
2374 do_type_descriptor(Gogo
*, Named_type
*);
2377 do_reflection(Gogo
*, std::string
*) const;
2380 do_mangled_name(Gogo
*, std::string
*) const;
2383 do_export(Export
*) const;
2387 make_interface_type_descriptor_type();
2389 // The list of methods associated with the interface. This will be
2390 // NULL for the empty interface.
2391 Typed_identifier_list
* methods_
;
2392 // The location where the interface was defined.
2393 source_location location_
;
2396 // The value we keep for a named type. This lets us get the right
2397 // name when we convert to trees. Note that we don't actually keep
2398 // the name here; the name is in the Named_object which points to
2399 // this. This object exists to hold a unique tree which represents
2402 class Named_type
: public Type
2405 Named_type(Named_object
* named_object
, Type
* type
, source_location location
)
2407 named_object_(named_object
), in_function_(NULL
), type_(type
),
2408 local_methods_(NULL
), all_methods_(NULL
),
2409 interface_method_tables_(NULL
), pointer_interface_method_tables_(NULL
),
2410 location_(location
), named_tree_(NULL
), is_visible_(true),
2411 is_error_(false), seen_(0)
2414 // Return the associated Named_object. This holds the actual name.
2417 { return this->named_object_
; }
2420 named_object() const
2421 { return this->named_object_
; }
2423 // Set the Named_object. This is used when we see a type
2424 // declaration followed by a type.
2426 set_named_object(Named_object
* no
)
2427 { this->named_object_
= no
; }
2429 // Return the function in which this type is defined. This will
2430 // return NULL for a type defined in global scope.
2433 { return this->in_function_
; }
2435 // Set the function in which this type is defined.
2437 set_in_function(Named_object
* f
)
2438 { this->in_function_
= f
; }
2440 // Return the name of the type.
2444 // Return the name of the type for an error message. The difference
2445 // is that if the type is defined in a different package, this will
2446 // return PACKAGE.NAME.
2448 message_name() const;
2450 // Return the underlying type.
2453 { return this->type_
; }
2457 { return this->type_
; }
2459 // Return the location.
2462 { return this->location_
; }
2464 // Whether this type is visible. This only matters when parsing.
2467 { return this->is_visible_
; }
2469 // Mark this type as visible.
2472 { this->is_visible_
= true; }
2474 // Mark this type as invisible.
2477 { this->is_visible_
= false; }
2479 // Whether this is a builtin type.
2482 { return this->location_
== BUILTINS_LOCATION
; }
2484 // Return the base type for this type.
2491 // Return whether this is an error type.
2493 is_named_error_type() const;
2495 // Add a method to this type.
2497 add_method(const std::string
& name
, Function
*);
2499 // Add a method declaration to this type.
2501 add_method_declaration(const std::string
& name
, Package
* package
,
2502 Function_type
* type
, source_location location
);
2504 // Add an existing method--one defined before the type itself was
2505 // defined--to a type.
2507 add_existing_method(Named_object
*);
2509 // Look up a local method.
2511 find_local_method(const std::string
& name
) const;
2513 // Return the list of local methods.
2515 local_methods() const
2516 { return this->local_methods_
; }
2518 // Build the complete list of methods, including those from
2519 // anonymous fields, and build method stubs if needed.
2521 finalize_methods(Gogo
*);
2523 // Return whether this type has any methods. This should only be
2524 // called after the finalize_methods pass.
2526 has_any_methods() const
2527 { return this->all_methods_
!= NULL
; }
2529 // Return the methods for this type. This should only be called
2530 // after the finalized_methods pass.
2533 { return this->all_methods_
; }
2535 // Return the method to use for NAME. This returns NULL if there is
2536 // no such method or if the method is ambiguous. When it returns
2537 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2539 method_function(const std::string
& name
, bool *is_ambiguous
) const;
2541 // Return whether NAME is a known field or method which is not
2542 // exported. This is only used for better error reporting.
2544 is_unexported_local_method(Gogo
*, const std::string
& name
) const;
2546 // Return a pointer to the interface method table for this type for
2547 // the interface INTERFACE. If IS_POINTER is true, set the type
2548 // descriptor to a pointer to this type, otherwise set it to this
2551 interface_method_table(Gogo
*, const Interface_type
* interface
,
2554 // Whether this type has any hidden fields.
2556 named_type_has_hidden_fields(std::string
* reason
) const;
2560 export_named_type(Export
*, const std::string
& name
) const;
2562 // Import a named type.
2564 import_named_type(Import
*, Named_type
**);
2568 do_traverse(Traverse
* traverse
)
2569 { return Type::traverse(this->type_
, traverse
); }
2575 do_has_pointer() const;
2578 do_hash_for_method(Gogo
*) const;
2581 do_check_make_expression(Expression_list
* args
, source_location location
)
2582 { return this->type_
->check_make_expression(args
, location
); }
2588 do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
2589 { return this->type_
->get_typed_init_tree(gogo
, type_tree
, is_clear
); }
2592 do_make_expression_tree(Translate_context
* context
, Expression_list
* args
,
2593 source_location location
)
2594 { return this->type_
->make_expression_tree(context
, args
, location
); }
2597 do_type_descriptor(Gogo
*, Named_type
*);
2600 do_reflection(Gogo
*, std::string
*) const;
2603 do_mangled_name(Gogo
*, std::string
* ret
) const;
2606 do_export(Export
*) const;
2609 // A mapping from interfaces to the associated interface method
2610 // tables for this type. This maps to a decl.
2611 typedef Unordered_map_hash(const Interface_type
*, tree
, Type_hash_identical
,
2612 Type_identical
) Interface_method_tables
;
2614 // A pointer back to the Named_object for this type.
2615 Named_object
* named_object_
;
2616 // If this type is defined in a function, a pointer back to the
2617 // function in which it is defined.
2618 Named_object
* in_function_
;
2621 // The list of methods defined for this type. Any named type can
2623 Bindings
* local_methods_
;
2624 // The full list of methods for this type, including methods
2625 // declared for anonymous fields.
2626 Methods
* all_methods_
;
2627 // A mapping from interfaces to the associated interface method
2628 // tables for this type.
2629 Interface_method_tables
* interface_method_tables_
;
2630 // A mapping from interfaces to the associated interface method
2631 // tables for pointers to this type.
2632 Interface_method_tables
* pointer_interface_method_tables_
;
2633 // The location where this type was defined.
2634 source_location location_
;
2635 // The tree for this type while converting to GENERIC. This is used
2636 // to avoid endless recursion when a named type refers to itself.
2638 // Whether this type is visible. This is false if this type was
2639 // created because it was referenced by an imported object, but the
2640 // type itself was not exported. This will always be true for types
2641 // created in the current package.
2643 // Whether this type is erroneous.
2645 // In a recursive operation such as has_hidden_fields, this flag is
2646 // used to prevent infinite recursion when a type refers to itself.
2647 // This is mutable because it is always reset to false when the
2652 // A forward declaration. This handles a type which has been declared
2655 class Forward_declaration_type
: public Type
2658 Forward_declaration_type(Named_object
* named_object
);
2660 // The named object associated with this type declaration. This
2661 // will be resolved.
2666 named_object() const;
2668 // Return the name of the type.
2672 // Return the type to which this points. Give an error if the type
2673 // has not yet been defined.
2680 // Whether the base type has been defined.
2684 // Add a method to this type.
2686 add_method(const std::string
& name
, Function
*);
2688 // Add a method declaration to this type.
2690 add_method_declaration(const std::string
& name
, Function_type
*,
2695 do_traverse(Traverse
* traverse
);
2698 do_has_pointer() const
2699 { return this->real_type()->has_pointer(); }
2702 do_hash_for_method(Gogo
* gogo
) const
2703 { return this->real_type()->hash_for_method(gogo
); }
2706 do_check_make_expression(Expression_list
* args
, source_location location
)
2707 { return this->base()->check_make_expression(args
, location
); }
2710 do_get_tree(Gogo
* gogo
);
2713 do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
2714 { return this->base()->get_typed_init_tree(gogo
, type_tree
, is_clear
); }
2717 do_make_expression_tree(Translate_context
* context
, Expression_list
* args
,
2718 source_location location
)
2719 { return this->base()->make_expression_tree(context
, args
, location
); }
2722 do_type_descriptor(Gogo
*, Named_type
*);
2725 do_reflection(Gogo
*, std::string
*) const;
2728 do_mangled_name(Gogo
*, std::string
* ret
) const;
2731 do_export(Export
*) const;
2734 // Issue a warning about a use of an undefined type.
2738 // The type declaration.
2739 Named_object
* named_object_
;
2740 // Whether we have issued a warning about this type.
2741 mutable bool warned_
;
2744 // The Type_context struct describes what we expect for the type of an
2749 // The exact type we expect, if known. This may be NULL.
2751 // Whether an abstract type is permitted.
2752 bool may_be_abstract
;
2756 : type(NULL
), may_be_abstract(false)
2759 Type_context(Type
* a_type
, bool a_may_be_abstract
)
2760 : type(a_type
), may_be_abstract(a_may_be_abstract
)
2764 #endif // !defined(GO_TYPES_H)