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 // Convert the builtin named types.
804 convert_builtin_named_types(Gogo
*);
806 // Return a tree representing this type.
810 // Return a tree representing a zero initialization for this type.
811 // This will be something like an INTEGER_CST or a CONSTRUCTOR. If
812 // IS_CLEAR is true, then the memory is known to be zeroed; in that
813 // case, this will return NULL if there is nothing to be done.
815 get_init_tree(Gogo
*, bool is_clear
);
817 // Like get_init_tree, but passing in the type to use for the
820 get_typed_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
821 { return this->do_get_init_tree(gogo
, type_tree
, is_clear
); }
823 // Return a tree for a make expression applied to this type.
825 make_expression_tree(Translate_context
* context
, Expression_list
* args
,
826 source_location location
)
827 { return this->do_make_expression_tree(context
, args
, location
); }
829 // Build a type descriptor entry for this type. Return a pointer to
832 type_descriptor_pointer(Gogo
* gogo
);
834 // Return the type reflection string for this type.
836 reflection(Gogo
*) const;
838 // Return a mangled name for the type. This is a name which can be
839 // used in assembler code. Identical types should have the same
842 mangled_name(Gogo
*) const;
846 export_type(Export
* exp
) const
847 { this->do_export(exp
); }
851 import_type(Import
*);
854 Type(Type_classification
);
856 // Functions implemented by the child class.
858 // Traverse the subtypes.
860 do_traverse(Traverse
*);
868 do_has_pointer() const
872 do_hash_for_method(Gogo
*) const;
875 do_check_make_expression(Expression_list
* args
, source_location
);
879 do_get_tree(Gogo
*) = 0;
882 do_get_init_tree(Gogo
*, tree
, bool) = 0;
885 do_make_expression_tree(Translate_context
*, Expression_list
*,
889 do_type_descriptor(Gogo
*, Named_type
* name
) = 0;
892 do_reflection(Gogo
*, std::string
*) const = 0;
896 do_mangled_name(Gogo
*, std::string
*) const = 0;
899 do_export(Export
*) const;
901 // Return whether an expression is an integer.
903 check_int_value(Expression
*, const char*, source_location
);
905 // Return whether a method expects a pointer as the receiver.
907 method_expects_pointer(const Named_object
*);
909 // Finalize the methods for a type.
911 finalize_methods(Gogo
*, const Type
*, source_location
, Methods
**);
913 // Return a method from a set of methods.
915 method_function(const Methods
*, const std::string
& name
,
918 // Return a composite literal for the type descriptor entry for a
921 type_descriptor(Gogo
*, Type
*);
923 // Return a composite literal for the type descriptor entry for
924 // TYPE, using NAME as the name of the type.
926 named_type_descriptor(Gogo
*, Type
* type
, Named_type
* name
);
928 // Return a composite literal for a plain type descriptor for this
929 // type with the given kind and name.
931 plain_type_descriptor(Gogo
*, int runtime_type_kind
, Named_type
* name
);
933 // Build a composite literal for the basic type descriptor.
935 type_descriptor_constructor(Gogo
*, int runtime_type_kind
, Named_type
*,
936 const Methods
*, bool only_value_methods
);
938 // Make a builtin struct type from a list of fields.
940 make_builtin_struct_type(int nfields
, ...);
942 // Make a builtin named type.
944 make_builtin_named_type(const char* name
, Type
* type
);
946 // For the benefit of child class reflection string generation.
948 append_reflection(const Type
* type
, Gogo
* gogo
, std::string
* ret
) const
949 { type
->do_reflection(gogo
, ret
); }
951 // For the benefit of child class mangling.
953 append_mangled_name(const Type
* type
, Gogo
* gogo
, std::string
* ret
) const
954 { type
->do_mangled_name(gogo
, ret
); }
956 // Incorporate a string into a hash code.
958 hash_string(const std::string
&, unsigned int);
960 // Return a tree for the underlying type of a named type.
962 get_named_type_tree(Gogo
* gogo
, Type
* base_type
)
963 { return base_type
->get_tree_without_hash(gogo
); }
966 // Convert to the desired type classification, or return NULL. This
967 // is a controlled dynamic_cast.
968 template<typename Type_class
, Type_classification type_classification
>
972 Type
* base
= this->base();
973 return (base
->classification_
== type_classification
974 ? static_cast<Type_class
*>(base
)
978 template<typename Type_class
, Type_classification type_classification
>
982 const Type
* base
= this->base();
983 return (base
->classification_
== type_classification
984 ? static_cast<Type_class
*>(base
)
988 template<typename Type_class
, Type_classification type_classification
>
992 return (this->classification_
== type_classification
993 ? static_cast<Type_class
*>(this)
997 template<typename Type_class
, Type_classification type_classification
>
999 convert_no_base() const
1001 return (this->classification_
== type_classification
1002 ? static_cast<Type_class
*>(this)
1006 // Get the hash and equality functions for a type.
1008 type_functions(const char** hash_fn
, const char** equal_fn
) const;
1010 // Build a composite literal for the uncommon type information.
1012 uncommon_type_constructor(Gogo
*, Type
* uncommon_type
,
1013 Named_type
*, const Methods
*,
1014 bool only_value_methods
) const;
1016 // Build a composite literal for the methods.
1018 methods_constructor(Gogo
*, Type
* methods_type
, const Methods
*,
1019 bool only_value_methods
) const;
1021 // Build a composite literal for one method.
1023 method_constructor(Gogo
*, Type
* method_type
, const std::string
& name
,
1024 const Method
*) const;
1027 build_receive_return_type(tree type
);
1029 // A hash table we use to avoid infinite recursion.
1030 typedef Unordered_set_hash(const Named_type
*, Type_hash_identical
,
1031 Type_identical
) Types_seen
;
1033 // Add all methods for TYPE to the list of methods for THIS.
1035 add_methods_for_type(const Type
* type
, const Method::Field_indexes
*,
1036 unsigned int depth
, bool, bool, Types_seen
*,
1040 add_local_methods_for_type(const Named_type
* type
,
1041 const Method::Field_indexes
*,
1042 unsigned int depth
, bool, bool, Methods
**);
1045 add_embedded_methods_for_type(const Type
* type
,
1046 const Method::Field_indexes
*,
1047 unsigned int depth
, bool, bool, Types_seen
*,
1051 add_interface_methods_for_type(const Type
* type
,
1052 const Method::Field_indexes
*,
1053 unsigned int depth
, Methods
**);
1055 // Build stub methods for a type.
1057 build_stub_methods(Gogo
*, const Type
* type
, const Methods
* methods
,
1061 build_one_stub_method(Gogo
*, Method
*, const char* receiver_name
,
1062 const Typed_identifier_list
*, bool is_varargs
,
1066 apply_field_indexes(Expression
*, const Method::Field_indexes
*,
1069 // Look for a field or method named NAME in TYPE.
1071 find_field_or_method(const Type
* type
, const std::string
& name
,
1072 bool receiver_can_be_pointer
,
1073 std::vector
<const Named_type
*>*, int* level
,
1074 bool* is_method
, bool* found_pointer_method
,
1075 std::string
* ambig1
, std::string
* ambig2
);
1077 // Get a tree for a type without looking in the hash table for
1080 get_tree_without_hash(Gogo
*);
1082 // A mapping from Type to tree, used to ensure that the GIMPLE
1083 // representation of identical types is identical.
1084 typedef Unordered_map_hash(const Type
*, tree
, Type_hash_identical
,
1085 Type_identical
) Type_trees
;
1087 static Type_trees type_trees
;
1089 // A list of builtin named types.
1090 static std::vector
<Named_type
*> named_builtin_types
;
1092 // The type classification.
1093 Type_classification classification_
;
1094 // The tree representation of the type, once it has been determined.
1096 // The decl for the type descriptor for this type. This starts out
1097 // as NULL and is filled in as needed.
1098 tree type_descriptor_decl_
;
1101 // Type hash table operations.
1103 class Type_hash_identical
1107 operator()(const Type
* type
) const
1108 { return type
->hash_for_method(NULL
); }
1111 class Type_identical
1115 operator()(const Type
* t1
, const Type
* t2
) const
1116 { return Type::are_identical(t1
, t2
, false, NULL
); }
1119 // An identifier with a type.
1121 class Typed_identifier
1124 Typed_identifier(const std::string
& name
, Type
* type
,
1125 source_location location
)
1126 : name_(name
), type_(type
), location_(location
)
1132 { return this->name_
; }
1137 { return this->type_
; }
1139 // Return the location where the name was seen. This is not always
1143 { return this->location_
; }
1145 // Set the type--sometimes we see the identifier before the type.
1147 set_type(Type
* type
)
1149 gcc_assert(this->type_
== NULL
|| type
->is_error_type());
1158 // The location where the name was seen.
1159 source_location location_
;
1162 // A list of Typed_identifiers.
1164 class Typed_identifier_list
1167 Typed_identifier_list()
1171 // Whether the list is empty.
1174 { return this->entries_
.empty(); }
1176 // Return the number of entries in the list.
1179 { return this->entries_
.size(); }
1181 // Add an entry to the end of the list.
1183 push_back(const Typed_identifier
& td
)
1184 { this->entries_
.push_back(td
); }
1186 // Remove an entry from the end of the list.
1189 { this->entries_
.pop_back(); }
1191 // Set the type of entry I to TYPE.
1193 set_type(size_t i
, Type
* type
)
1195 gcc_assert(i
< this->entries_
.size());
1196 this->entries_
[i
].set_type(type
);
1199 // Sort the entries by name.
1205 traverse(Traverse
*);
1207 // Return the first and last elements.
1210 { return this->entries_
.front(); }
1212 const Typed_identifier
&
1214 { return this->entries_
.front(); }
1218 { return this->entries_
.back(); }
1220 const Typed_identifier
&
1222 { return this->entries_
.back(); }
1224 const Typed_identifier
&
1226 { return this->entries_
.at(i
); }
1229 set(size_t i
, const Typed_identifier
& t
)
1230 { this->entries_
.at(i
) = t
; }
1235 gcc_assert(c
<= this->entries_
.size());
1236 this->entries_
.resize(c
, Typed_identifier("", NULL
, UNKNOWN_LOCATION
));
1241 typedef std::vector
<Typed_identifier
>::iterator iterator
;
1242 typedef std::vector
<Typed_identifier
>::const_iterator const_iterator
;
1246 { return this->entries_
.begin(); }
1250 { return this->entries_
.begin(); }
1254 { return this->entries_
.end(); }
1258 { return this->entries_
.end(); }
1260 // Return a copy of this list. This returns an independent copy of
1261 // the vector, but does not copy the types.
1262 Typed_identifier_list
*
1266 std::vector
<Typed_identifier
> entries_
;
1269 // The type of an integer.
1271 class Integer_type
: public Type
1274 // Create a new integer type.
1276 create_integer_type(const char* name
, bool is_unsigned
, int bits
,
1277 int runtime_type_kind
);
1279 // Look up an existing integer type.
1281 lookup_integer_type(const char* name
);
1283 // Create an abstract integer type.
1284 static Integer_type
*
1285 create_abstract_integer_type();
1287 // Whether this is an abstract integer type.
1290 { return this->is_abstract_
; }
1292 // Whether this is an unsigned type.
1295 { return this->is_unsigned_
; }
1297 // The number of bits.
1300 { return this->bits_
; }
1302 // Whether this type is the same as T.
1304 is_identical(const Integer_type
* t
) const;
1308 do_hash_for_method(Gogo
*) const;
1314 do_get_init_tree(Gogo
*, tree
, bool);
1317 do_type_descriptor(Gogo
*, Named_type
*);
1320 do_reflection(Gogo
*, std::string
*) const;
1323 do_mangled_name(Gogo
*, std::string
*) const;
1326 Integer_type(bool is_abstract
, bool is_unsigned
, int bits
,
1327 int runtime_type_kind
)
1328 : Type(TYPE_INTEGER
),
1329 is_abstract_(is_abstract
), is_unsigned_(is_unsigned
), bits_(bits
),
1330 runtime_type_kind_(runtime_type_kind
)
1333 // Map names of integer types to the types themselves.
1334 typedef std::map
<std::string
, Named_type
*> Named_integer_types
;
1335 static Named_integer_types named_integer_types
;
1337 // True if this is an abstract type.
1339 // True if this is an unsigned type.
1341 // The number of bits.
1343 // The runtime type code used in the type descriptor for this type.
1344 int runtime_type_kind_
;
1347 // The type of a floating point number.
1349 class Float_type
: public Type
1352 // Create a new float type.
1354 create_float_type(const char* name
, int bits
, int runtime_type_kind
);
1356 // Look up an existing float type.
1358 lookup_float_type(const char* name
);
1360 // Create an abstract float type.
1362 create_abstract_float_type();
1364 // Whether this is an abstract float type.
1367 { return this->is_abstract_
; }
1369 // The number of bits.
1372 { return this->bits_
; }
1374 // Whether this type is the same as T.
1376 is_identical(const Float_type
* t
) const;
1378 // Return a tree for this type without using a Gogo*.
1384 do_hash_for_method(Gogo
*) const;
1390 do_get_init_tree(Gogo
*, tree
, bool);
1393 do_type_descriptor(Gogo
*, Named_type
*);
1396 do_reflection(Gogo
*, std::string
*) const;
1399 do_mangled_name(Gogo
*, std::string
*) const;
1402 Float_type(bool is_abstract
, int bits
, int runtime_type_kind
)
1404 is_abstract_(is_abstract
), bits_(bits
),
1405 runtime_type_kind_(runtime_type_kind
)
1408 // Map names of float types to the types themselves.
1409 typedef std::map
<std::string
, Named_type
*> Named_float_types
;
1410 static Named_float_types named_float_types
;
1412 // True if this is an abstract type.
1414 // The number of bits in the floating point value.
1416 // The runtime type code used in the type descriptor for this type.
1417 int runtime_type_kind_
;
1420 // The type of a complex number.
1422 class Complex_type
: public Type
1425 // Create a new complex type.
1427 create_complex_type(const char* name
, int bits
, int runtime_type_kind
);
1429 // Look up an existing complex type.
1431 lookup_complex_type(const char* name
);
1433 // Create an abstract complex type.
1434 static Complex_type
*
1435 create_abstract_complex_type();
1437 // Whether this is an abstract complex type.
1440 { return this->is_abstract_
; }
1442 // The number of bits: 64 or 128.
1444 { return this->bits_
; }
1446 // Whether this type is the same as T.
1448 is_identical(const Complex_type
* t
) const;
1450 // Return a tree for this type without using a Gogo*.
1456 do_hash_for_method(Gogo
*) const;
1462 do_get_init_tree(Gogo
*, tree
, bool);
1465 do_type_descriptor(Gogo
*, Named_type
*);
1468 do_reflection(Gogo
*, std::string
*) const;
1471 do_mangled_name(Gogo
*, std::string
*) const;
1474 Complex_type(bool is_abstract
, int bits
, int runtime_type_kind
)
1475 : Type(TYPE_COMPLEX
),
1476 is_abstract_(is_abstract
), bits_(bits
),
1477 runtime_type_kind_(runtime_type_kind
)
1480 // Map names of complex types to the types themselves.
1481 typedef std::map
<std::string
, Named_type
*> Named_complex_types
;
1482 static Named_complex_types named_complex_types
;
1484 // True if this is an abstract type.
1486 // The number of bits in the complex value--64 or 128.
1488 // The runtime type code used in the type descriptor for this type.
1489 int runtime_type_kind_
;
1492 // The type of a string.
1494 class String_type
: public Type
1501 // Return a tree for the length of STRING.
1503 length_tree(Gogo
*, tree string
);
1505 // Return a tree which points to the bytes of STRING.
1507 bytes_tree(Gogo
*, tree string
);
1511 do_has_pointer() const
1518 do_get_init_tree(Gogo
* gogo
, tree
, bool);
1521 do_type_descriptor(Gogo
*, Named_type
*);
1524 do_reflection(Gogo
*, std::string
*) const;
1527 do_mangled_name(Gogo
*, std::string
* ret
) const;
1530 // The named string type.
1531 static Named_type
* string_type_
;
1534 // The type of a function.
1536 class Function_type
: public Type
1539 Function_type(Typed_identifier
* receiver
, Typed_identifier_list
* parameters
,
1540 Typed_identifier_list
* results
, source_location location
)
1541 : Type(TYPE_FUNCTION
),
1542 receiver_(receiver
), parameters_(parameters
), results_(results
),
1543 location_(location
), is_varargs_(false), is_builtin_(false)
1546 // Get the receiver.
1547 const Typed_identifier
*
1549 { return this->receiver_
; }
1551 // Get the return names and types.
1552 const Typed_identifier_list
*
1554 { return this->results_
; }
1556 // Get the parameter names and types.
1557 const Typed_identifier_list
*
1559 { return this->parameters_
; }
1561 // Whether this is a varargs function.
1564 { return this->is_varargs_
; }
1566 // Whether this is a builtin function.
1569 { return this->is_builtin_
; }
1571 // The location where this type was defined.
1574 { return this->location_
; }
1576 // Return whether this is a method type.
1579 { return this->receiver_
!= NULL
; }
1581 // Whether T is a valid redeclaration of this type. This is called
1582 // when a function is declared more than once.
1584 is_valid_redeclaration(const Function_type
* t
, std::string
*) const;
1586 // Whether this type is the same as T.
1588 is_identical(const Function_type
* t
, bool ignore_receiver
,
1589 bool errors_are_identical
, std::string
*) const;
1591 // Record that this is a varargs function.
1594 { this->is_varargs_
= true; }
1596 // Record that this is a builtin function.
1599 { this->is_builtin_
= true; }
1601 // Import a function type.
1602 static Function_type
*
1605 // Return a copy of this type without a receiver. This is only
1606 // valid for a method type.
1608 copy_without_receiver() const;
1610 // Return a copy of this type with a receiver. This is used when an
1611 // interface method is attached to a named or struct type.
1613 copy_with_receiver(Type
*) const;
1616 make_function_type_descriptor_type();
1620 do_traverse(Traverse
*);
1622 // A trampoline function has a pointer which matters for GC.
1624 do_has_pointer() const
1628 do_hash_for_method(Gogo
*) const;
1634 do_get_init_tree(Gogo
*, tree
, bool);
1637 do_type_descriptor(Gogo
*, Named_type
*);
1640 do_reflection(Gogo
*, std::string
*) const;
1643 do_mangled_name(Gogo
*, std::string
*) const;
1646 do_export(Export
*) const;
1650 type_descriptor_params(Type
*, const Typed_identifier
*,
1651 const Typed_identifier_list
*);
1653 // The receiver name and type. This will be NULL for a normal
1654 // function, non-NULL for a method.
1655 Typed_identifier
* receiver_
;
1656 // The parameter names and types.
1657 Typed_identifier_list
* parameters_
;
1658 // The result names and types. This will be NULL if no result was
1660 Typed_identifier_list
* results_
;
1661 // The location where this type was defined. This exists solely to
1662 // give a location for the fields of the struct if this function
1663 // returns multiple values.
1664 source_location location_
;
1665 // Whether this function takes a variable number of arguments.
1667 // Whether this is a special builtin function which can not simply
1668 // be called. This is used for len, cap, etc.
1672 // The type of a pointer.
1674 class Pointer_type
: public Type
1677 Pointer_type(Type
* to_type
)
1678 : Type(TYPE_POINTER
),
1684 { return this->to_type_
; }
1686 // Import a pointer type.
1687 static Pointer_type
*
1691 make_pointer_type_descriptor_type();
1695 do_traverse(Traverse
*);
1698 do_has_pointer() const
1702 do_hash_for_method(Gogo
*) const;
1708 do_get_init_tree(Gogo
*, tree
, bool);
1711 do_type_descriptor(Gogo
*, Named_type
*);
1714 do_reflection(Gogo
*, std::string
*) const;
1717 do_mangled_name(Gogo
*, std::string
*) const;
1720 do_export(Export
*) const;
1723 // The type to which this type points.
1727 // The type of a field in a struct.
1732 explicit Struct_field(const Typed_identifier
& typed_identifier
)
1733 : typed_identifier_(typed_identifier
), tag_(NULL
)
1743 { return this->typed_identifier_
.type(); }
1745 // The field location.
1748 { return this->typed_identifier_
.location(); }
1750 // Whether the field has a tag.
1753 { return this->tag_
!= NULL
; }
1759 gcc_assert(this->tag_
!= NULL
);
1763 // Whether this is an anonymous field.
1765 is_anonymous() const
1766 { return this->typed_identifier_
.name().empty(); }
1768 // Set the tag. FIXME: This is never freed.
1770 set_tag(const std::string
& tag
)
1771 { this->tag_
= new std::string(tag
); }
1773 // Set the type. This is only used in error cases.
1775 set_type(Type
* type
)
1776 { this->typed_identifier_
.set_type(type
); }
1779 // The field name, type, and location.
1780 Typed_identifier typed_identifier_
;
1781 // The field tag. This is NULL if the field has no tag.
1785 // A list of struct fields.
1787 class Struct_field_list
1794 // Whether the list is empty.
1797 { return this->entries_
.empty(); }
1799 // Return the number of entries.
1802 { return this->entries_
.size(); }
1804 // Add an entry to the end of the list.
1806 push_back(const Struct_field
& sf
)
1807 { this->entries_
.push_back(sf
); }
1809 // Index into the list.
1812 { return this->entries_
.at(i
); }
1814 // Last entry in list.
1817 { return this->entries_
.back(); }
1821 typedef std::vector
<Struct_field
>::iterator iterator
;
1822 typedef std::vector
<Struct_field
>::const_iterator const_iterator
;
1826 { return this->entries_
.begin(); }
1830 { return this->entries_
.begin(); }
1834 { return this->entries_
.end(); }
1838 { return this->entries_
.end(); }
1841 std::vector
<Struct_field
> entries_
;
1844 // The type of a struct.
1846 class Struct_type
: public Type
1849 Struct_type(Struct_field_list
* fields
, source_location location
)
1850 : Type(TYPE_STRUCT
),
1851 fields_(fields
), location_(location
), all_methods_(NULL
)
1854 // Return the field NAME. This only looks at local fields, not at
1855 // embedded types. If the field is found, and PINDEX is not NULL,
1856 // this sets *PINDEX to the field index. If the field is not found,
1857 // this returns NULL.
1859 find_local_field(const std::string
& name
, unsigned int *pindex
) const;
1861 // Return the field number INDEX.
1863 field(unsigned int index
) const
1864 { return &this->fields_
->at(index
); }
1866 // Get the struct fields.
1867 const Struct_field_list
*
1869 { return this->fields_
; }
1871 // Return the number of fields.
1874 { return this->fields_
->size(); }
1876 // Push a new field onto the end of the struct. This is used when
1877 // building a closure variable.
1879 push_field(const Struct_field
& sf
)
1880 { this->fields_
->push_back(sf
); }
1882 // Return an expression referring to field NAME in STRUCT_EXPR, or
1883 // NULL if there is no field with that name.
1884 Field_reference_expression
*
1885 field_reference(Expression
* struct_expr
, const std::string
& name
,
1886 source_location
) const;
1888 // Return the total number of fields, including embedded fields.
1889 // This is the number of values which can appear in a conversion to
1892 total_field_count() const;
1894 // Whether this type is identical with T.
1896 is_identical(const Struct_type
* t
, bool errors_are_identical
) const;
1898 // Whether this struct type has any hidden fields. This returns
1899 // true if any fields have hidden names, or if any non-pointer
1900 // anonymous fields have types with hidden fields.
1902 struct_has_hidden_fields(const Named_type
* within
, std::string
*) const;
1904 // Return whether NAME is a local field which is not exported. This
1905 // is only used for better error reporting.
1907 is_unexported_local_field(Gogo
*, const std::string
& name
) const;
1909 // If this is an unnamed struct, build the complete list of methods,
1910 // including those from anonymous fields, and build methods stubs if
1913 finalize_methods(Gogo
*);
1915 // Return whether this type has any methods. This should only be
1916 // called after the finalize_methods pass.
1918 has_any_methods() const
1919 { return this->all_methods_
!= NULL
; }
1921 // Return the methods for tihs type. This should only be called
1922 // after the finalize_methods pass.
1925 { return this->all_methods_
; }
1927 // Return the method to use for NAME. This returns NULL if there is
1928 // no such method or if the method is ambiguous. When it returns
1929 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
1931 method_function(const std::string
& name
, bool* is_ambiguous
) const;
1933 // Traverse just the field types of a struct type.
1935 traverse_field_types(Traverse
* traverse
)
1936 { return this->do_traverse(traverse
); }
1938 // Import a struct type.
1942 // Fill in the fields for a named struct type.
1944 fill_in_tree(Gogo
*, tree
);
1947 make_struct_type_descriptor_type();
1951 do_traverse(Traverse
*);
1957 do_has_pointer() const;
1960 do_hash_for_method(Gogo
*) const;
1966 do_get_init_tree(Gogo
*, tree
, bool);
1969 do_type_descriptor(Gogo
*, Named_type
*);
1972 do_reflection(Gogo
*, std::string
*) const;
1975 do_mangled_name(Gogo
*, std::string
*) const;
1978 do_export(Export
*) const;
1981 // Used to avoid infinite loops in field_reference_depth.
1982 struct Saw_named_type
1984 Saw_named_type
* next
;
1988 Field_reference_expression
*
1989 field_reference_depth(Expression
* struct_expr
, const std::string
& name
,
1990 source_location
, Saw_named_type
*,
1991 unsigned int* depth
) const;
1993 // The fields of the struct.
1994 Struct_field_list
* fields_
;
1995 // The place where the struct was declared.
1996 source_location location_
;
1997 // If this struct is unnamed, a list of methods.
1998 Methods
* all_methods_
;
2001 // The type of an array.
2003 class Array_type
: public Type
2006 Array_type(Type
* element_type
, Expression
* length
)
2008 element_type_(element_type
), length_(length
), length_tree_(NULL
)
2011 // Return the element type.
2013 element_type() const
2014 { return this->element_type_
; }
2016 // Return the length. This will return NULL for an open array.
2019 { return this->length_
; }
2021 // Whether this type is identical with T.
2023 is_identical(const Array_type
* t
, bool errors_are_identical
) const;
2025 // Whether this type has any hidden fields.
2027 array_has_hidden_fields(const Named_type
* within
, std::string
* reason
) const
2028 { return this->element_type_
->has_hidden_fields(within
, reason
); }
2030 // Return a tree for the pointer to the values in an array.
2032 value_pointer_tree(Gogo
*, tree array
) const;
2034 // Return a tree for the length of an array with this type.
2036 length_tree(Gogo
*, tree array
);
2038 // Return a tree for the capacity of an array with this type.
2040 capacity_tree(Gogo
*, tree array
);
2042 // Import an array type.
2046 // Fill in the fields for a named array type.
2048 fill_in_array_tree(Gogo
*, tree
);
2050 // Fill in the fields for a named slice type.
2052 fill_in_slice_tree(Gogo
*, tree
);
2055 make_array_type_descriptor_type();
2058 make_slice_type_descriptor_type();
2062 do_traverse(Traverse
* traverse
);
2068 do_has_pointer() const
2070 return this->length_
== NULL
|| this->element_type_
->has_pointer();
2074 do_hash_for_method(Gogo
*) const;
2077 do_check_make_expression(Expression_list
*, source_location
);
2083 do_get_init_tree(Gogo
*, tree
, bool);
2086 do_make_expression_tree(Translate_context
*, Expression_list
*,
2090 do_type_descriptor(Gogo
*, Named_type
*);
2093 do_reflection(Gogo
*, std::string
*) const;
2096 do_mangled_name(Gogo
*, std::string
*) const;
2099 do_export(Export
*) const;
2106 get_length_tree(Gogo
*);
2109 array_type_descriptor(Gogo
*, Named_type
*);
2112 slice_type_descriptor(Gogo
*, Named_type
*);
2114 // The type of elements of the array.
2115 Type
* element_type_
;
2116 // The number of elements. This may be NULL.
2117 Expression
* length_
;
2118 // The length as a tree. We only want to compute this once.
2122 // The type of a map.
2124 class Map_type
: public Type
2127 Map_type(Type
* key_type
, Type
* val_type
, source_location location
)
2129 key_type_(key_type
), val_type_(val_type
), location_(location
)
2132 // Return the key type.
2135 { return this->key_type_
; }
2137 // Return the value type.
2140 { return this->val_type_
; }
2142 // Whether this type is identical with T.
2144 is_identical(const Map_type
* t
, bool errors_are_identical
) const;
2146 // Import a map type.
2151 make_map_type_descriptor_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;
2197 // Where the type was defined.
2198 source_location location_
;
2201 // The type of a channel.
2203 class Channel_type
: public Type
2206 Channel_type(bool may_send
, bool may_receive
, Type
* element_type
)
2207 : Type(TYPE_CHANNEL
),
2208 may_send_(may_send
), may_receive_(may_receive
),
2209 element_type_(element_type
)
2210 { gcc_assert(may_send
|| may_receive
); }
2212 // Whether this channel can send data.
2215 { return this->may_send_
; }
2217 // Whether this channel can receive data.
2220 { return this->may_receive_
; }
2222 // The type of the values that may be sent on this channel. This is
2223 // NULL if any type may be sent.
2225 element_type() const
2226 { return this->element_type_
; }
2228 // Whether this type is identical with T.
2230 is_identical(const Channel_type
* t
, bool errors_are_identical
) const;
2232 // Import a channel type.
2233 static Channel_type
*
2237 make_chan_type_descriptor_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;
2277 // Whether this channel can send data.
2279 // Whether this channel can receive data.
2281 // The types of elements which may be sent on this channel. If this
2282 // is NULL, it means that any type may be sent.
2283 Type
* element_type_
;
2286 // An interface type.
2288 class Interface_type
: public Type
2291 Interface_type(Typed_identifier_list
* methods
, source_location location
)
2292 : Type(TYPE_INTERFACE
),
2293 methods_(methods
), location_(location
)
2294 { gcc_assert(methods
== NULL
|| !methods
->empty()); }
2296 // The location where the interface type was defined.
2299 { return this->location_
; }
2301 // Return whether this is an empty interface.
2304 { return this->methods_
== NULL
; }
2306 // Return the list of methods. This will return NULL for an empty
2308 const Typed_identifier_list
*
2310 { return this->methods_
; }
2312 // Return the number of methods.
2314 method_count() const
2315 { return this->methods_
== NULL
? 0 : this->methods_
->size(); }
2317 // Return the method NAME, or NULL.
2318 const Typed_identifier
*
2319 find_method(const std::string
& name
) const;
2321 // Return the zero-based index of method NAME.
2323 method_index(const std::string
& name
) const;
2325 // Finalize the methods. This handles interface inheritance.
2329 // Return true if T implements this interface. If this returns
2330 // false, and REASON is not NULL, it sets *REASON to the reason that
2333 implements_interface(const Type
* t
, std::string
* reason
) const;
2335 // Whether this type is identical with T. REASON is as in
2336 // implements_interface.
2338 is_identical(const Interface_type
* t
, bool errors_are_identical
) const;
2340 // Whether we can assign T to this type. is_identical is known to
2343 is_compatible_for_assign(const Interface_type
*, std::string
* reason
) const;
2345 // Return whether NAME is a method which is not exported. This is
2346 // only used for better error reporting.
2348 is_unexported_method(Gogo
*, const std::string
& name
) const;
2350 // Import an interface type.
2351 static Interface_type
*
2354 // Make a struct for an empty interface type.
2356 empty_type_tree(Gogo
*);
2358 // Make a struct for non-empty interface type.
2360 non_empty_type_tree(source_location
);
2362 // Fill in the fields for a named interface type.
2364 fill_in_tree(Gogo
*, tree
);
2367 make_interface_type_descriptor_type();
2371 do_traverse(Traverse
*);
2374 do_has_pointer() const
2378 do_hash_for_method(Gogo
*) const;
2384 do_get_init_tree(Gogo
* gogo
, tree
, bool);
2387 do_type_descriptor(Gogo
*, Named_type
*);
2390 do_reflection(Gogo
*, std::string
*) const;
2393 do_mangled_name(Gogo
*, std::string
*) const;
2396 do_export(Export
*) const;
2399 // The list of methods associated with the interface. This will be
2400 // NULL for the empty interface.
2401 Typed_identifier_list
* methods_
;
2402 // The location where the interface was defined.
2403 source_location location_
;
2406 // The value we keep for a named type. This lets us get the right
2407 // name when we convert to trees. Note that we don't actually keep
2408 // the name here; the name is in the Named_object which points to
2409 // this. This object exists to hold a unique tree which represents
2412 class Named_type
: public Type
2415 Named_type(Named_object
* named_object
, Type
* type
, source_location location
)
2417 named_object_(named_object
), in_function_(NULL
), type_(type
),
2418 local_methods_(NULL
), all_methods_(NULL
),
2419 interface_method_tables_(NULL
), pointer_interface_method_tables_(NULL
),
2420 location_(location
), named_tree_(NULL
), dependencies_(),
2421 is_visible_(true), is_error_(false), is_converted_(false),
2422 is_circular_(false), seen_(0)
2425 // Return the associated Named_object. This holds the actual name.
2428 { return this->named_object_
; }
2431 named_object() const
2432 { return this->named_object_
; }
2434 // Set the Named_object. This is used when we see a type
2435 // declaration followed by a type.
2437 set_named_object(Named_object
* no
)
2438 { this->named_object_
= no
; }
2440 // Return the function in which this type is defined. This will
2441 // return NULL for a type defined in global scope.
2444 { return this->in_function_
; }
2446 // Set the function in which this type is defined.
2448 set_in_function(Named_object
* f
)
2449 { this->in_function_
= f
; }
2451 // Return the name of the type.
2455 // Return the name of the type for an error message. The difference
2456 // is that if the type is defined in a different package, this will
2457 // return PACKAGE.NAME.
2459 message_name() const;
2461 // Return the underlying type.
2464 { return this->type_
; }
2468 { return this->type_
; }
2470 // Return the location.
2473 { return this->location_
; }
2475 // Whether this type is visible. This only matters when parsing.
2478 { return this->is_visible_
; }
2480 // Mark this type as visible.
2483 { this->is_visible_
= true; }
2485 // Mark this type as invisible.
2488 { this->is_visible_
= false; }
2490 // Whether this is a builtin type.
2493 { return this->location_
== BUILTINS_LOCATION
; }
2495 // Whether this is a circular type: a pointer or function type that
2496 // refers to itself, which is not possible in C.
2499 { return this->is_circular_
; }
2501 // Return the base type for this type.
2508 // Return whether this is an error type.
2510 is_named_error_type() const;
2512 // Add a method to this type.
2514 add_method(const std::string
& name
, Function
*);
2516 // Add a method declaration to this type.
2518 add_method_declaration(const std::string
& name
, Package
* package
,
2519 Function_type
* type
, source_location location
);
2521 // Add an existing method--one defined before the type itself was
2522 // defined--to a type.
2524 add_existing_method(Named_object
*);
2526 // Look up a local method.
2528 find_local_method(const std::string
& name
) const;
2530 // Return the list of local methods.
2532 local_methods() const
2533 { return this->local_methods_
; }
2535 // Build the complete list of methods, including those from
2536 // anonymous fields, and build method stubs if needed.
2538 finalize_methods(Gogo
*);
2540 // Return whether this type has any methods. This should only be
2541 // called after the finalize_methods pass.
2543 has_any_methods() const
2544 { return this->all_methods_
!= NULL
; }
2546 // Return the methods for this type. This should only be called
2547 // after the finalized_methods pass.
2550 { return this->all_methods_
; }
2552 // Return the method to use for NAME. This returns NULL if there is
2553 // no such method or if the method is ambiguous. When it returns
2554 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2556 method_function(const std::string
& name
, bool *is_ambiguous
) const;
2558 // Return whether NAME is a known field or method which is not
2559 // exported. This is only used for better error reporting.
2561 is_unexported_local_method(Gogo
*, const std::string
& name
) const;
2563 // Return a pointer to the interface method table for this type for
2564 // the interface INTERFACE. If IS_POINTER is true, set the type
2565 // descriptor to a pointer to this type, otherwise set it to this
2568 interface_method_table(Gogo
*, const Interface_type
* interface
,
2571 // Whether this type has any hidden fields.
2573 named_type_has_hidden_fields(std::string
* reason
) const;
2575 // Note that a type must be converted to the backend representation
2576 // before we convert this type.
2578 add_dependency(Named_type
* nt
)
2579 { this->dependencies_
.push_back(nt
); }
2583 export_named_type(Export
*, const std::string
& name
) const;
2585 // Import a named type.
2587 import_named_type(Import
*, Named_type
**);
2589 // Initial conversion to backend representation.
2595 do_traverse(Traverse
* traverse
)
2596 { return Type::traverse(this->type_
, traverse
); }
2602 do_has_pointer() const;
2605 do_hash_for_method(Gogo
*) const;
2608 do_check_make_expression(Expression_list
* args
, source_location location
)
2609 { return this->type_
->check_make_expression(args
, location
); }
2615 do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
2616 { return this->type_
->get_typed_init_tree(gogo
, type_tree
, is_clear
); }
2619 do_make_expression_tree(Translate_context
* context
, Expression_list
* args
,
2620 source_location location
)
2621 { return this->type_
->make_expression_tree(context
, args
, location
); }
2624 do_type_descriptor(Gogo
*, Named_type
*);
2627 do_reflection(Gogo
*, std::string
*) const;
2630 do_mangled_name(Gogo
*, std::string
* ret
) const;
2633 do_export(Export
*) const;
2636 // Create the placeholder during conversion.
2638 create_placeholder(Gogo
*);
2640 // A mapping from interfaces to the associated interface method
2641 // tables for this type. This maps to a decl.
2642 typedef Unordered_map_hash(const Interface_type
*, tree
, Type_hash_identical
,
2643 Type_identical
) Interface_method_tables
;
2645 // A pointer back to the Named_object for this type.
2646 Named_object
* named_object_
;
2647 // If this type is defined in a function, a pointer back to the
2648 // function in which it is defined.
2649 Named_object
* in_function_
;
2652 // The list of methods defined for this type. Any named type can
2654 Bindings
* local_methods_
;
2655 // The full list of methods for this type, including methods
2656 // declared for anonymous fields.
2657 Methods
* all_methods_
;
2658 // A mapping from interfaces to the associated interface method
2659 // tables for this type.
2660 Interface_method_tables
* interface_method_tables_
;
2661 // A mapping from interfaces to the associated interface method
2662 // tables for pointers to this type.
2663 Interface_method_tables
* pointer_interface_method_tables_
;
2664 // The location where this type was defined.
2665 source_location location_
;
2666 // The tree for this type while converting to GENERIC. This is used
2667 // to avoid endless recursion when a named type refers to itself.
2669 // A list of types which must be converted to the backend
2670 // representation before this type can be converted. This is for
2672 // type S1 { p *S2 }
2674 // where we can't convert S2 to the backend representation unless we
2675 // have converted S1.
2676 std::vector
<Named_type
*> dependencies_
;
2677 // Whether this type is visible. This is false if this type was
2678 // created because it was referenced by an imported object, but the
2679 // type itself was not exported. This will always be true for types
2680 // created in the current package.
2682 // Whether this type is erroneous.
2684 // Whether this type has been converted to the backend
2687 // Whether this is a pointer or function type which refers to the
2690 // In a recursive operation such as has_hidden_fields, this flag is
2691 // used to prevent infinite recursion when a type refers to itself.
2692 // This is mutable because it is always reset to false when the
2697 // A forward declaration. This handles a type which has been declared
2700 class Forward_declaration_type
: public Type
2703 Forward_declaration_type(Named_object
* named_object
);
2705 // The named object associated with this type declaration. This
2706 // will be resolved.
2711 named_object() const;
2713 // Return the name of the type.
2717 // Return the type to which this points. Give an error if the type
2718 // has not yet been defined.
2725 // Whether the base type has been defined.
2729 // Add a method to this type.
2731 add_method(const std::string
& name
, Function
*);
2733 // Add a method declaration to this type.
2735 add_method_declaration(const std::string
& name
, Function_type
*,
2740 do_traverse(Traverse
* traverse
);
2743 do_has_pointer() const
2744 { return this->real_type()->has_pointer(); }
2747 do_hash_for_method(Gogo
* gogo
) const
2748 { return this->real_type()->hash_for_method(gogo
); }
2751 do_check_make_expression(Expression_list
* args
, source_location location
)
2752 { return this->base()->check_make_expression(args
, location
); }
2755 do_get_tree(Gogo
* gogo
);
2758 do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
2759 { return this->base()->get_typed_init_tree(gogo
, type_tree
, is_clear
); }
2762 do_make_expression_tree(Translate_context
* context
, Expression_list
* args
,
2763 source_location location
)
2764 { return this->base()->make_expression_tree(context
, args
, location
); }
2767 do_type_descriptor(Gogo
*, Named_type
*);
2770 do_reflection(Gogo
*, std::string
*) const;
2773 do_mangled_name(Gogo
*, std::string
* ret
) const;
2776 do_export(Export
*) const;
2779 // Issue a warning about a use of an undefined type.
2783 // The type declaration.
2784 Named_object
* named_object_
;
2785 // Whether we have issued a warning about this type.
2786 mutable bool warned_
;
2789 // The Type_context struct describes what we expect for the type of an
2794 // The exact type we expect, if known. This may be NULL.
2796 // Whether an abstract type is permitted.
2797 bool may_be_abstract
;
2801 : type(NULL
), may_be_abstract(false)
2804 Type_context(Type
* a_type
, bool a_may_be_abstract
)
2805 : type(a_type
), may_be_abstract(a_may_be_abstract
)
2809 #endif // !defined(GO_TYPES_H)