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
;
48 // Type codes used in type descriptors. These must match the values
49 // in libgo/runtime/go-type.h. They also match the values in the gc
50 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
51 // although this is not required.
53 static const int RUNTIME_TYPE_KIND_BOOL
= 1;
54 static const int RUNTIME_TYPE_KIND_INT
= 2;
55 static const int RUNTIME_TYPE_KIND_INT8
= 3;
56 static const int RUNTIME_TYPE_KIND_INT16
= 4;
57 static const int RUNTIME_TYPE_KIND_INT32
= 5;
58 static const int RUNTIME_TYPE_KIND_INT64
= 6;
59 static const int RUNTIME_TYPE_KIND_UINT
= 7;
60 static const int RUNTIME_TYPE_KIND_UINT8
= 8;
61 static const int RUNTIME_TYPE_KIND_UINT16
= 9;
62 static const int RUNTIME_TYPE_KIND_UINT32
= 10;
63 static const int RUNTIME_TYPE_KIND_UINT64
= 11;
64 static const int RUNTIME_TYPE_KIND_UINTPTR
= 12;
65 static const int RUNTIME_TYPE_KIND_FLOAT32
= 13;
66 static const int RUNTIME_TYPE_KIND_FLOAT64
= 14;
67 static const int RUNTIME_TYPE_KIND_COMPLEX64
= 15;
68 static const int RUNTIME_TYPE_KIND_COMPLEX128
= 16;
69 static const int RUNTIME_TYPE_KIND_ARRAY
= 17;
70 static const int RUNTIME_TYPE_KIND_CHAN
= 18;
71 static const int RUNTIME_TYPE_KIND_FUNC
= 19;
72 static const int RUNTIME_TYPE_KIND_INTERFACE
= 20;
73 static const int RUNTIME_TYPE_KIND_MAP
= 21;
74 static const int RUNTIME_TYPE_KIND_PTR
= 22;
75 static const int RUNTIME_TYPE_KIND_SLICE
= 23;
76 static const int RUNTIME_TYPE_KIND_STRING
= 24;
77 static const int RUNTIME_TYPE_KIND_STRUCT
= 25;
78 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER
= 26;
80 // To build the complete list of methods for a named type we need to
81 // gather all methods from anonymous fields. Those methods may
82 // require an arbitrary set of indirections and field offsets. There
83 // is also the possibility of ambiguous methods, which we could ignore
84 // except that we want to give a better error message for that case.
85 // This is a base class. There are two types of methods: named
86 // methods, and methods which are inherited from an anonymous field of
92 // For methods in anonymous types we need to know the sequence of
93 // field references used to extract the pointer to pass to the
94 // method. Since each method for a particular anonymous field will
95 // have the sequence of field indexes, and since the indexes can be
96 // shared going down the chain, we use a manually managed linked
97 // list. The first entry in the list is the field index for the
98 // last field, the one passed to the method.
102 const Field_indexes
* next
;
103 unsigned int field_index
;
109 // Get the list of field indexes.
111 field_indexes() const
112 { return this->field_indexes_
; }
117 { return this->depth_
; }
119 // Return whether this is a value method--a method which does not
120 // require a pointer expression.
122 is_value_method() const
123 { return this->is_value_method_
; }
125 // Return whether we need a stub method--this is true if we can't
126 // just pass the main object to the method.
128 needs_stub_method() const
129 { return this->needs_stub_method_
; }
131 // Return whether this is an ambiguous method name.
134 { return this->is_ambiguous_
; }
136 // Note that this method is ambiguous.
139 { this->is_ambiguous_
= true; }
141 // Return the type of the method.
144 { return this->do_type(); }
146 // Return the location of the method receiver.
148 receiver_location() const
149 { return this->do_receiver_location(); }
151 // Return an expression which binds this method to EXPR. This is
152 // something which can be used with a function call.
154 bind_method(Expression
* expr
, source_location location
) const;
156 // Return the named object for this method. This may only be called
157 // after methods are finalized.
159 named_object() const;
161 // Get the stub object.
165 go_assert(this->stub_
!= NULL
);
169 // Set the stub object.
171 set_stub_object(Named_object
* no
)
173 go_assert(this->stub_
== NULL
);
178 // These objects are only built by the child classes.
179 Method(const Field_indexes
* field_indexes
, unsigned int depth
,
180 bool is_value_method
, bool needs_stub_method
)
181 : field_indexes_(field_indexes
), depth_(depth
), stub_(NULL
),
182 is_value_method_(is_value_method
), needs_stub_method_(needs_stub_method
),
186 // The named object for this method.
187 virtual Named_object
*
188 do_named_object() const = 0;
190 // The type of the method.
191 virtual Function_type
*
194 // Return the location of the method receiver.
195 virtual source_location
196 do_receiver_location() const = 0;
198 // Bind a method to an object.
200 do_bind_method(Expression
* expr
, source_location location
) const = 0;
203 // The sequence of field indexes used for this method. If this is
204 // NULL, then the method is defined for the current type.
205 const Field_indexes
* field_indexes_
;
206 // The depth at which this method was found.
208 // If a stub method is required, this is its object. This is only
209 // set after stub methods are built in finalize_methods.
211 // Whether this is a value method--a method that does not require a
213 bool is_value_method_
;
214 // Whether a stub method is required.
215 bool needs_stub_method_
;
216 // Whether this method is ambiguous.
220 // A named method. This is what you get with a method declaration,
221 // either directly on the type, or inherited from some anonymous
224 class Named_method
: public Method
227 Named_method(Named_object
* named_object
, const Field_indexes
* field_indexes
,
228 unsigned int depth
, bool is_value_method
,
229 bool needs_stub_method
)
230 : Method(field_indexes
, depth
, is_value_method
, needs_stub_method
),
231 named_object_(named_object
)
235 // Get the Named_object for the method.
237 do_named_object() const
238 { return this->named_object_
; }
240 // The type of the method.
244 // Return the location of the method receiver.
246 do_receiver_location() const;
248 // Bind a method to an object.
250 do_bind_method(Expression
* expr
, source_location location
) const;
253 // The method itself. For a method which needs a stub, this starts
254 // out as the underlying method, and is later replaced with the stub
256 Named_object
* named_object_
;
259 // An interface method. This is used when an interface appears as an
260 // anonymous field in a named struct.
262 class Interface_method
: public Method
265 Interface_method(const std::string
& name
, source_location location
,
266 Function_type
* fntype
, const Field_indexes
* field_indexes
,
268 : Method(field_indexes
, depth
, true, true),
269 name_(name
), location_(location
), fntype_(fntype
)
273 // Get the Named_object for the method. This should never be
274 // called, as we always create a stub.
276 do_named_object() const
277 { go_unreachable(); }
279 // The type of the method.
282 { return this->fntype_
; }
284 // Return the location of the method receiver.
286 do_receiver_location() const
287 { return this->location_
; }
289 // Bind a method to an object.
291 do_bind_method(Expression
* expr
, source_location location
) const;
294 // The name of the interface method to call.
296 // The location of the definition of the interface method.
297 source_location location_
;
298 // The type of the interface method.
299 Function_type
* fntype_
;
302 // A mapping from method name to Method. This is a wrapper around a
308 typedef Unordered_map(std::string
, Method
*) Method_map
;
311 typedef Method_map::const_iterator const_iterator
;
317 // Insert a new method. Returns true if it was inserted, false if
318 // it was overidden or ambiguous.
320 insert(const std::string
& name
, Method
* m
);
322 // The number of (unambiguous) methods.
329 { return this->methods_
.begin(); }
333 { return this->methods_
.end(); }
337 find(const std::string
& name
) const
338 { return this->methods_
.find(name
); }
344 // The base class for all types.
349 // The types of types.
350 enum Type_classification
363 TYPE_CALL_MULTIPLE_RESULT
,
383 // Get the unnamed bool type.
387 // Get the named type "bool".
391 // Make the named type "bool".
393 make_named_bool_type();
395 // Make an abstract integer type.
397 make_abstract_integer_type();
399 // Make a named integer type with a specified size.
400 // RUNTIME_TYPE_KIND is the code to use in reflection information,
401 // to distinguish int and int32.
403 make_integer_type(const char* name
, bool is_unsigned
, int bits
,
404 int runtime_type_kind
);
406 // Look up a named integer type.
408 lookup_integer_type(const char* name
);
410 // Make an abstract floating point type.
412 make_abstract_float_type();
414 // Make a named floating point type with a specific size.
415 // RUNTIME_TYPE_KIND is the code to use in reflection information,
416 // to distinguish float and float32.
418 make_float_type(const char* name
, int bits
, int runtime_type_kind
);
420 // Look up a named float type.
422 lookup_float_type(const char* name
);
424 // Make an abstract complex type.
426 make_abstract_complex_type();
428 // Make a named complex type with a specific size.
429 // RUNTIME_TYPE_KIND is the code to use in reflection information,
430 // to distinguish complex and complex64.
432 make_complex_type(const char* name
, int bits
, int runtime_type_kind
);
434 // Look up a named complex type.
436 lookup_complex_type(const char* name
);
438 // Get the unnamed string type.
442 // Get the named type "string".
444 lookup_string_type();
446 // Make the named type "string".
448 make_named_string_type();
453 static Function_type
*
454 make_function_type(Typed_identifier
* receiver
,
455 Typed_identifier_list
* parameters
,
456 Typed_identifier_list
* results
,
460 make_pointer_type(Type
*);
466 make_call_multiple_result_type(Call_expression
*);
469 make_struct_type(Struct_field_list
* fields
, source_location
);
472 make_array_type(Type
* element_type
, Expression
* length
);
475 make_map_type(Type
* key_type
, Type
* value_type
, source_location
);
478 make_channel_type(bool send
, bool receive
, Type
*);
480 static Interface_type
*
481 make_interface_type(Typed_identifier_list
* methods
, source_location
);
484 make_type_descriptor_type();
487 make_type_descriptor_ptr_type();
490 make_named_type(Named_object
*, Type
*, source_location
);
493 make_forward_declaration(Named_object
*);
497 traverse(Type
*, Traverse
*);
499 // Verify the type. This is called after parsing, and verifies that
500 // types are complete and meet the language requirements. This
501 // returns false if the type is invalid.
504 { return this->do_verify(); }
506 // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
507 // returns that an erroneous type is identical to any other type;
508 // this is used to avoid cascading errors. If this returns false,
509 // and REASON is not NULL, it may set *REASON.
511 are_identical(const Type
* lhs
, const Type
* rhs
, bool errors_are_identical
,
512 std::string
* reason
);
514 // Return true if two types are compatible for use in a binary
515 // operation, other than a shift, comparison, or channel send. This
516 // is an equivalence relation.
518 are_compatible_for_binop(const Type
* t1
, const Type
* t2
);
520 // Return true if a value with type RHS is assignable to a variable
521 // with type LHS. This is not an equivalence relation. If this
522 // returns false, and REASON is not NULL, it sets *REASON.
524 are_assignable(const Type
* lhs
, const Type
* rhs
, std::string
* reason
);
526 // Return true if a value with type RHS is assignable to a variable
527 // with type LHS, ignoring any assignment of hidden fields
528 // (unexported fields of a type imported from another package).
529 // This is like the are_assignable method.
531 are_assignable_hidden_ok(const Type
* lhs
, const Type
* rhs
,
532 std::string
* reason
);
534 // Return true if a value with type RHS may be converted to type
535 // LHS. If this returns false, and REASON is not NULL, it sets
538 are_convertible(const Type
* lhs
, const Type
* rhs
, std::string
* reason
);
540 // Whether this type has any hidden fields which are not visible in
541 // the current compilation, such as a field whose name begins with a
542 // lower case letter in a struct imported from a different package.
543 // WITHIN is not NULL if we are looking at fields in a named type.
545 has_hidden_fields(const Named_type
* within
, std::string
* reason
) const;
547 // Return a hash code for this type for the method hash table.
548 // Types which are equivalent according to are_identical will have
549 // the same hash code.
551 hash_for_method(Gogo
*) const;
553 // Return the type classification.
555 classification() const
556 { return this->classification_
; }
558 // Return the base type for this type. This looks through forward
559 // declarations and names. Using this with a forward declaration
560 // which has not been defined will return an error type.
567 // Return the type skipping defined forward declarations. If this
568 // type is a forward declaration which has not been defined, it will
569 // return the Forward_declaration_type. This differs from base() in
570 // that it will return a Named_type, and for a
571 // Forward_declaration_type which is not defined it will return that
572 // type rather than an error type.
579 // Return true if this is a basic type: a type which is not composed
580 // of other types, and is not void.
582 is_basic_type() const;
584 // Return true if this is an abstract type--an integer, floating
585 // point, or complex type whose size has not been determined.
589 // Return a non-abstract version of an abstract type.
591 make_non_abstract_type();
593 // Return true if this type is or contains a pointer. This
594 // determines whether the garbage collector needs to look at a value
598 { return this->do_has_pointer(); }
600 // Return true if this is the error type. This returns false for a
601 // type which is not defined, as it is called by the parser before
602 // all types are defined.
604 is_error_type() const;
606 // Return true if this is the error type or if the type is
607 // undefined. If the type is undefined, this will give an error.
608 // This should only be called after parsing is complete.
611 { return this->base()->is_error_type(); }
613 // Return true if this is a void type.
616 { return this->classification_
== TYPE_VOID
; }
618 // If this is an integer type, return the Integer_type. Otherwise,
619 // return NULL. This is a controlled dynamic_cast.
622 { return this->convert
<Integer_type
, TYPE_INTEGER
>(); }
626 { return this->convert
<const Integer_type
, TYPE_INTEGER
>(); }
628 // If this is a floating point type, return the Float_type.
629 // Otherwise, return NULL. This is a controlled dynamic_cast.
632 { return this->convert
<Float_type
, TYPE_FLOAT
>(); }
636 { return this->convert
<const Float_type
, TYPE_FLOAT
>(); }
638 // If this is a complex type, return the Complex_type. Otherwise,
642 { return this->convert
<Complex_type
, TYPE_COMPLEX
>(); }
646 { return this->convert
<const Complex_type
, TYPE_COMPLEX
>(); }
648 // Return true if this is a boolean type.
650 is_boolean_type() const
651 { return this->base()->classification_
== TYPE_BOOLEAN
; }
653 // Return true if this is an abstract boolean type.
655 is_abstract_boolean_type() const
656 { return this->classification_
== TYPE_BOOLEAN
; }
658 // Return true if this is a string type.
660 is_string_type() const
661 { return this->base()->classification_
== TYPE_STRING
; }
663 // Return true if this is an abstract string type.
665 is_abstract_string_type() const
666 { return this->classification_
== TYPE_STRING
; }
668 // Return true if this is the sink type. This is the type of the
669 // blank identifier _.
672 { return this->base()->classification_
== TYPE_SINK
; }
674 // If this is a function type, return it. Otherwise, return NULL.
677 { return this->convert
<Function_type
, TYPE_FUNCTION
>(); }
680 function_type() const
681 { return this->convert
<const Function_type
, TYPE_FUNCTION
>(); }
683 // If this is a pointer type, return the type to which it points.
684 // Otherwise, return NULL.
688 // If this is a pointer type, return the type to which it points.
689 // Otherwise, return the type itself.
693 Type
* pt
= this->points_to();
694 return pt
!= NULL
? pt
: this;
700 const Type
* pt
= this->points_to();
701 return pt
!= NULL
? pt
: this;
704 // Return true if this is the nil type. We don't use base() here,
705 // because this can be called during parse, and there is no way to
706 // name the nil type anyhow.
709 { return this->classification_
== TYPE_NIL
; }
711 // Return true if this is the predeclared constant nil being used as
712 // a type. This is what the parser produces for type switches which
715 is_nil_constant_as_type() const;
717 // Return true if this is the return type of a function which
718 // returns multiple values.
720 is_call_multiple_result_type() const
721 { return this->base()->classification_
== TYPE_CALL_MULTIPLE_RESULT
; }
723 // If this is a struct type, return it. Otherwise, return NULL.
726 { return this->convert
<Struct_type
, TYPE_STRUCT
>(); }
730 { return this->convert
<const Struct_type
, TYPE_STRUCT
>(); }
732 // If this is an array type, return it. Otherwise, return NULL.
735 { return this->convert
<Array_type
, TYPE_ARRAY
>(); }
739 { return this->convert
<const Array_type
, TYPE_ARRAY
>(); }
741 // Return whether if this is an open array type.
743 is_open_array_type() const;
745 // If this is a map type, return it. Otherwise, return NULL.
748 { return this->convert
<Map_type
, TYPE_MAP
>(); }
752 { return this->convert
<const Map_type
, TYPE_MAP
>(); }
754 // If this is a channel type, return it. Otherwise, return NULL.
757 { return this->convert
<Channel_type
, TYPE_CHANNEL
>(); }
761 { return this->convert
<const Channel_type
, TYPE_CHANNEL
>(); }
763 // If this is an interface type, return it. Otherwise, return NULL.
766 { return this->convert
<Interface_type
, TYPE_INTERFACE
>(); }
768 const Interface_type
*
769 interface_type() const
770 { return this->convert
<const Interface_type
, TYPE_INTERFACE
>(); }
772 // If this is a named type, return it. Otherwise, return NULL.
779 // If this is a forward declaration, return it. Otherwise, return
781 Forward_declaration_type
*
782 forward_declaration_type()
783 { return this->convert_no_base
<Forward_declaration_type
, TYPE_FORWARD
>(); }
785 const Forward_declaration_type
*
786 forward_declaration_type() const
788 return this->convert_no_base
<const Forward_declaration_type
,
792 // Return true if this type is not yet defined.
794 is_undefined() const;
796 // Return true if this is the unsafe.pointer type. We currently
797 // represent that as pointer-to-void.
799 is_unsafe_pointer_type() const
800 { return this->points_to() != NULL
&& this->points_to()->is_void_type(); }
802 // Look for field or method NAME for TYPE. Return an expression for
803 // it, bound to EXPR.
805 bind_field_or_method(Gogo
*, const Type
* type
, Expression
* expr
,
806 const std::string
& name
, source_location
);
808 // Return true if NAME is an unexported field or method of TYPE.
810 is_unexported_field_or_method(Gogo
*, const Type
*, const std::string
&,
811 std::vector
<const Named_type
*>*);
813 // This type was passed to the builtin function make. ARGS are the
814 // arguments passed to make after the type; this may be NULL if
815 // there were none. Issue any required errors.
817 check_make_expression(Expression_list
* args
, source_location location
)
818 { return this->do_check_make_expression(args
, location
); }
820 // Convert the builtin named types.
822 convert_builtin_named_types(Gogo
*);
824 // Return the backend representation of this type.
828 // Return a tree representing a zero initialization for this type.
829 // This will be something like an INTEGER_CST or a CONSTRUCTOR. If
830 // IS_CLEAR is true, then the memory is known to be zeroed; in that
831 // case, this will return NULL if there is nothing to be done.
833 get_init_tree(Gogo
*, bool is_clear
);
835 // Like get_init_tree, but passing in the type to use for the
838 get_typed_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
839 { return this->do_get_init_tree(gogo
, type_tree
, is_clear
); }
841 // Return a tree for a make expression applied to this type.
843 make_expression_tree(Translate_context
* context
, Expression_list
* args
,
844 source_location location
)
845 { return this->do_make_expression_tree(context
, args
, location
); }
847 // Build a type descriptor entry for this type. Return a pointer to
850 type_descriptor_pointer(Gogo
* gogo
);
852 // Return the type reflection string for this type.
854 reflection(Gogo
*) const;
856 // Return a mangled name for the type. This is a name which can be
857 // used in assembler code. Identical types should have the same
860 mangled_name(Gogo
*) const;
864 export_type(Export
* exp
) const
865 { this->do_export(exp
); }
869 import_type(Import
*);
872 Type(Type_classification
);
874 // Functions implemented by the child class.
876 // Traverse the subtypes.
878 do_traverse(Traverse
*);
886 do_has_pointer() const
890 do_hash_for_method(Gogo
*) const;
893 do_check_make_expression(Expression_list
* args
, source_location
);
896 do_get_backend(Gogo
*) = 0;
899 do_get_init_tree(Gogo
*, tree
, bool) = 0;
902 do_make_expression_tree(Translate_context
*, Expression_list
*,
906 do_type_descriptor(Gogo
*, Named_type
* name
) = 0;
909 do_reflection(Gogo
*, std::string
*) const = 0;
913 do_mangled_name(Gogo
*, std::string
*) const = 0;
916 do_export(Export
*) const;
918 // Return whether an expression is an integer.
920 check_int_value(Expression
*, const char*, source_location
);
922 // Return whether a method expects a pointer as the receiver.
924 method_expects_pointer(const Named_object
*);
926 // Finalize the methods for a type.
928 finalize_methods(Gogo
*, const Type
*, source_location
, Methods
**);
930 // Return a method from a set of methods.
932 method_function(const Methods
*, const std::string
& name
,
935 // Return a composite literal for the type descriptor entry for a
938 type_descriptor(Gogo
*, Type
*);
940 // Return a composite literal for the type descriptor entry for
941 // TYPE, using NAME as the name of the type.
943 named_type_descriptor(Gogo
*, Type
* type
, Named_type
* name
);
945 // Return a composite literal for a plain type descriptor for this
946 // type with the given kind and name.
948 plain_type_descriptor(Gogo
*, int runtime_type_kind
, Named_type
* name
);
950 // Build a composite literal for the basic type descriptor.
952 type_descriptor_constructor(Gogo
*, int runtime_type_kind
, Named_type
*,
953 const Methods
*, bool only_value_methods
);
955 // Make a builtin struct type from a list of fields.
957 make_builtin_struct_type(int nfields
, ...);
959 // Make a builtin named type.
961 make_builtin_named_type(const char* name
, Type
* type
);
963 // For the benefit of child class reflection string generation.
965 append_reflection(const Type
* type
, Gogo
* gogo
, std::string
* ret
) const
966 { type
->do_reflection(gogo
, ret
); }
968 // For the benefit of child class mangling.
970 append_mangled_name(const Type
* type
, Gogo
* gogo
, std::string
* ret
) const
971 { type
->do_mangled_name(gogo
, ret
); }
973 // Incorporate a string into a hash code.
975 hash_string(const std::string
&, unsigned int);
977 // Return the backend representation for the underlying type of a
980 get_named_base_btype(Gogo
* gogo
, Type
* base_type
)
981 { return base_type
->get_btype_without_hash(gogo
); }
984 // Convert to the desired type classification, or return NULL. This
985 // is a controlled dynamic_cast.
986 template<typename Type_class
, Type_classification type_classification
>
990 Type
* base
= this->base();
991 return (base
->classification_
== type_classification
992 ? static_cast<Type_class
*>(base
)
996 template<typename Type_class
, Type_classification type_classification
>
1000 const Type
* base
= this->base();
1001 return (base
->classification_
== type_classification
1002 ? static_cast<Type_class
*>(base
)
1006 template<typename Type_class
, Type_classification type_classification
>
1010 return (this->classification_
== type_classification
1011 ? static_cast<Type_class
*>(this)
1015 template<typename Type_class
, Type_classification type_classification
>
1017 convert_no_base() const
1019 return (this->classification_
== type_classification
1020 ? static_cast<Type_class
*>(this)
1024 // Support for are_assignable and are_assignable_hidden_ok.
1026 are_assignable_check_hidden(const Type
* lhs
, const Type
* rhs
,
1027 bool check_hidden_fields
, std::string
* reason
);
1029 // Get the hash and equality functions for a type.
1031 type_functions(const char** hash_fn
, const char** equal_fn
) const;
1033 // Build a composite literal for the uncommon type information.
1035 uncommon_type_constructor(Gogo
*, Type
* uncommon_type
,
1036 Named_type
*, const Methods
*,
1037 bool only_value_methods
) const;
1039 // Build a composite literal for the methods.
1041 methods_constructor(Gogo
*, Type
* methods_type
, const Methods
*,
1042 bool only_value_methods
) const;
1044 // Build a composite literal for one method.
1046 method_constructor(Gogo
*, Type
* method_type
, const std::string
& name
,
1047 const Method
*) const;
1050 build_receive_return_type(tree type
);
1052 // A hash table we use to avoid infinite recursion.
1053 typedef Unordered_set_hash(const Named_type
*, Type_hash_identical
,
1054 Type_identical
) Types_seen
;
1056 // Add all methods for TYPE to the list of methods for THIS.
1058 add_methods_for_type(const Type
* type
, const Method::Field_indexes
*,
1059 unsigned int depth
, bool, bool, Types_seen
*,
1063 add_local_methods_for_type(const Named_type
* type
,
1064 const Method::Field_indexes
*,
1065 unsigned int depth
, bool, bool, Methods
**);
1068 add_embedded_methods_for_type(const Type
* type
,
1069 const Method::Field_indexes
*,
1070 unsigned int depth
, bool, bool, Types_seen
*,
1074 add_interface_methods_for_type(const Type
* type
,
1075 const Method::Field_indexes
*,
1076 unsigned int depth
, Methods
**);
1078 // Build stub methods for a type.
1080 build_stub_methods(Gogo
*, const Type
* type
, const Methods
* methods
,
1084 build_one_stub_method(Gogo
*, Method
*, const char* receiver_name
,
1085 const Typed_identifier_list
*, bool is_varargs
,
1089 apply_field_indexes(Expression
*, const Method::Field_indexes
*,
1092 // Look for a field or method named NAME in TYPE.
1094 find_field_or_method(const Type
* type
, const std::string
& name
,
1095 bool receiver_can_be_pointer
,
1096 std::vector
<const Named_type
*>*, int* level
,
1097 bool* is_method
, bool* found_pointer_method
,
1098 std::string
* ambig1
, std::string
* ambig2
);
1100 // Get the backend representation for a type without looking in the
1101 // hash table for identical types.
1103 get_btype_without_hash(Gogo
*);
1105 // A mapping from Type to Btype*, used to ensure that the backend
1106 // representation of identical types is identical.
1107 typedef Unordered_map_hash(const Type
*, Btype
*, Type_hash_identical
,
1108 Type_identical
) Type_btypes
;
1110 static Type_btypes type_btypes
;
1112 // A list of builtin named types.
1113 static std::vector
<Named_type
*> named_builtin_types
;
1115 // The type classification.
1116 Type_classification classification_
;
1117 // The backend representation of the type, once it has been
1120 // The decl for the type descriptor for this type. This starts out
1121 // as NULL and is filled in as needed.
1122 tree type_descriptor_decl_
;
1125 // Type hash table operations.
1127 class Type_hash_identical
1131 operator()(const Type
* type
) const
1132 { return type
->hash_for_method(NULL
); }
1135 class Type_identical
1139 operator()(const Type
* t1
, const Type
* t2
) const
1140 { return Type::are_identical(t1
, t2
, false, NULL
); }
1143 // An identifier with a type.
1145 class Typed_identifier
1148 Typed_identifier(const std::string
& name
, Type
* type
,
1149 source_location location
)
1150 : name_(name
), type_(type
), location_(location
)
1156 { return this->name_
; }
1161 { return this->type_
; }
1163 // Return the location where the name was seen. This is not always
1167 { return this->location_
; }
1169 // Set the type--sometimes we see the identifier before the type.
1171 set_type(Type
* type
)
1173 go_assert(this->type_
== NULL
|| type
->is_error_type());
1182 // The location where the name was seen.
1183 source_location location_
;
1186 // A list of Typed_identifiers.
1188 class Typed_identifier_list
1191 Typed_identifier_list()
1195 // Whether the list is empty.
1198 { return this->entries_
.empty(); }
1200 // Return the number of entries in the list.
1203 { return this->entries_
.size(); }
1205 // Add an entry to the end of the list.
1207 push_back(const Typed_identifier
& td
)
1208 { this->entries_
.push_back(td
); }
1210 // Remove an entry from the end of the list.
1213 { this->entries_
.pop_back(); }
1215 // Set the type of entry I to TYPE.
1217 set_type(size_t i
, Type
* type
)
1219 go_assert(i
< this->entries_
.size());
1220 this->entries_
[i
].set_type(type
);
1223 // Sort the entries by name.
1229 traverse(Traverse
*);
1231 // Return the first and last elements.
1234 { return this->entries_
.front(); }
1236 const Typed_identifier
&
1238 { return this->entries_
.front(); }
1242 { return this->entries_
.back(); }
1244 const Typed_identifier
&
1246 { return this->entries_
.back(); }
1248 const Typed_identifier
&
1250 { return this->entries_
.at(i
); }
1253 set(size_t i
, const Typed_identifier
& t
)
1254 { this->entries_
.at(i
) = t
; }
1259 go_assert(c
<= this->entries_
.size());
1260 this->entries_
.resize(c
, Typed_identifier("", NULL
, UNKNOWN_LOCATION
));
1265 typedef std::vector
<Typed_identifier
>::iterator iterator
;
1266 typedef std::vector
<Typed_identifier
>::const_iterator const_iterator
;
1270 { return this->entries_
.begin(); }
1274 { return this->entries_
.begin(); }
1278 { return this->entries_
.end(); }
1282 { return this->entries_
.end(); }
1284 // Return a copy of this list. This returns an independent copy of
1285 // the vector, but does not copy the types.
1286 Typed_identifier_list
*
1290 std::vector
<Typed_identifier
> entries_
;
1293 // The type of an integer.
1295 class Integer_type
: public Type
1298 // Create a new integer type.
1300 create_integer_type(const char* name
, bool is_unsigned
, int bits
,
1301 int runtime_type_kind
);
1303 // Look up an existing integer type.
1305 lookup_integer_type(const char* name
);
1307 // Create an abstract integer type.
1308 static Integer_type
*
1309 create_abstract_integer_type();
1311 // Whether this is an abstract integer type.
1314 { return this->is_abstract_
; }
1316 // Whether this is an unsigned type.
1319 { return this->is_unsigned_
; }
1321 // The number of bits.
1324 { return this->bits_
; }
1326 // Whether this type is the same as T.
1328 is_identical(const Integer_type
* t
) const;
1332 do_hash_for_method(Gogo
*) const;
1335 do_get_backend(Gogo
*);
1338 do_get_init_tree(Gogo
*, tree
, bool);
1341 do_type_descriptor(Gogo
*, Named_type
*);
1344 do_reflection(Gogo
*, std::string
*) const;
1347 do_mangled_name(Gogo
*, std::string
*) const;
1350 Integer_type(bool is_abstract
, bool is_unsigned
, int bits
,
1351 int runtime_type_kind
)
1352 : Type(TYPE_INTEGER
),
1353 is_abstract_(is_abstract
), is_unsigned_(is_unsigned
), bits_(bits
),
1354 runtime_type_kind_(runtime_type_kind
)
1357 // Map names of integer types to the types themselves.
1358 typedef std::map
<std::string
, Named_type
*> Named_integer_types
;
1359 static Named_integer_types named_integer_types
;
1361 // True if this is an abstract type.
1363 // True if this is an unsigned type.
1365 // The number of bits.
1367 // The runtime type code used in the type descriptor for this type.
1368 int runtime_type_kind_
;
1371 // The type of a floating point number.
1373 class Float_type
: public Type
1376 // Create a new float type.
1378 create_float_type(const char* name
, int bits
, int runtime_type_kind
);
1380 // Look up an existing float type.
1382 lookup_float_type(const char* name
);
1384 // Create an abstract float type.
1386 create_abstract_float_type();
1388 // Whether this is an abstract float type.
1391 { return this->is_abstract_
; }
1393 // The number of bits.
1396 { return this->bits_
; }
1398 // Whether this type is the same as T.
1400 is_identical(const Float_type
* t
) const;
1404 do_hash_for_method(Gogo
*) const;
1407 do_get_backend(Gogo
*);
1410 do_get_init_tree(Gogo
*, tree
, bool);
1413 do_type_descriptor(Gogo
*, Named_type
*);
1416 do_reflection(Gogo
*, std::string
*) const;
1419 do_mangled_name(Gogo
*, std::string
*) const;
1422 Float_type(bool is_abstract
, int bits
, int runtime_type_kind
)
1424 is_abstract_(is_abstract
), bits_(bits
),
1425 runtime_type_kind_(runtime_type_kind
)
1428 // Map names of float types to the types themselves.
1429 typedef std::map
<std::string
, Named_type
*> Named_float_types
;
1430 static Named_float_types named_float_types
;
1432 // True if this is an abstract type.
1434 // The number of bits in the floating point value.
1436 // The runtime type code used in the type descriptor for this type.
1437 int runtime_type_kind_
;
1440 // The type of a complex number.
1442 class Complex_type
: public Type
1445 // Create a new complex type.
1447 create_complex_type(const char* name
, int bits
, int runtime_type_kind
);
1449 // Look up an existing complex type.
1451 lookup_complex_type(const char* name
);
1453 // Create an abstract complex type.
1454 static Complex_type
*
1455 create_abstract_complex_type();
1457 // Whether this is an abstract complex type.
1460 { return this->is_abstract_
; }
1462 // The number of bits: 64 or 128.
1464 { return this->bits_
; }
1466 // Whether this type is the same as T.
1468 is_identical(const Complex_type
* t
) const;
1472 do_hash_for_method(Gogo
*) const;
1475 do_get_backend(Gogo
*);
1478 do_get_init_tree(Gogo
*, tree
, bool);
1481 do_type_descriptor(Gogo
*, Named_type
*);
1484 do_reflection(Gogo
*, std::string
*) const;
1487 do_mangled_name(Gogo
*, std::string
*) const;
1490 Complex_type(bool is_abstract
, int bits
, int runtime_type_kind
)
1491 : Type(TYPE_COMPLEX
),
1492 is_abstract_(is_abstract
), bits_(bits
),
1493 runtime_type_kind_(runtime_type_kind
)
1496 // Map names of complex types to the types themselves.
1497 typedef std::map
<std::string
, Named_type
*> Named_complex_types
;
1498 static Named_complex_types named_complex_types
;
1500 // True if this is an abstract type.
1502 // The number of bits in the complex value--64 or 128.
1504 // The runtime type code used in the type descriptor for this type.
1505 int runtime_type_kind_
;
1508 // The type of a string.
1510 class String_type
: public Type
1517 // Return a tree for the length of STRING.
1519 length_tree(Gogo
*, tree string
);
1521 // Return a tree which points to the bytes of STRING.
1523 bytes_tree(Gogo
*, tree string
);
1527 do_has_pointer() const
1531 do_get_backend(Gogo
*);
1534 do_get_init_tree(Gogo
* gogo
, tree
, bool);
1537 do_type_descriptor(Gogo
*, Named_type
*);
1540 do_reflection(Gogo
*, std::string
*) const;
1543 do_mangled_name(Gogo
*, std::string
* ret
) const;
1546 // The named string type.
1547 static Named_type
* string_type_
;
1550 // The type of a function.
1552 class Function_type
: public Type
1555 Function_type(Typed_identifier
* receiver
, Typed_identifier_list
* parameters
,
1556 Typed_identifier_list
* results
, source_location location
)
1557 : Type(TYPE_FUNCTION
),
1558 receiver_(receiver
), parameters_(parameters
), results_(results
),
1559 location_(location
), is_varargs_(false), is_builtin_(false)
1562 // Get the receiver.
1563 const Typed_identifier
*
1565 { return this->receiver_
; }
1567 // Get the return names and types.
1568 const Typed_identifier_list
*
1570 { return this->results_
; }
1572 // Get the parameter names and types.
1573 const Typed_identifier_list
*
1575 { return this->parameters_
; }
1577 // Whether this is a varargs function.
1580 { return this->is_varargs_
; }
1582 // Whether this is a builtin function.
1585 { return this->is_builtin_
; }
1587 // The location where this type was defined.
1590 { return this->location_
; }
1592 // Return whether this is a method type.
1595 { return this->receiver_
!= NULL
; }
1597 // Whether T is a valid redeclaration of this type. This is called
1598 // when a function is declared more than once.
1600 is_valid_redeclaration(const Function_type
* t
, std::string
*) const;
1602 // Whether this type is the same as T.
1604 is_identical(const Function_type
* t
, bool ignore_receiver
,
1605 bool errors_are_identical
, std::string
*) const;
1607 // Record that this is a varargs function.
1610 { this->is_varargs_
= true; }
1612 // Record that this is a builtin function.
1615 { this->is_builtin_
= true; }
1617 // Import a function type.
1618 static Function_type
*
1621 // Return a copy of this type without a receiver. This is only
1622 // valid for a method type.
1624 copy_without_receiver() const;
1626 // Return a copy of this type with a receiver. This is used when an
1627 // interface method is attached to a named or struct type.
1629 copy_with_receiver(Type
*) const;
1631 // Finishing converting function types.
1633 convert_types(Gogo
*);
1636 make_function_type_descriptor_type();
1640 do_traverse(Traverse
*);
1642 // A trampoline function has a pointer which matters for GC.
1644 do_has_pointer() const
1648 do_hash_for_method(Gogo
*) const;
1651 do_get_backend(Gogo
*);
1654 do_get_init_tree(Gogo
*, tree
, bool);
1657 do_type_descriptor(Gogo
*, Named_type
*);
1660 do_reflection(Gogo
*, std::string
*) const;
1663 do_mangled_name(Gogo
*, std::string
*) const;
1666 do_export(Export
*) const;
1670 type_descriptor_params(Type
*, const Typed_identifier
*,
1671 const Typed_identifier_list
*);
1674 get_function_backend(Gogo
*);
1676 // A list of function types with multiple results and their
1677 // placeholder backend representations, used to postpone building
1678 // the structs we use for multiple results until all types are
1680 typedef std::vector
<std::pair
<Function_type
*, Btype
*> > Placeholders
;
1681 static Placeholders placeholders
;
1683 // The receiver name and type. This will be NULL for a normal
1684 // function, non-NULL for a method.
1685 Typed_identifier
* receiver_
;
1686 // The parameter names and types.
1687 Typed_identifier_list
* parameters_
;
1688 // The result names and types. This will be NULL if no result was
1690 Typed_identifier_list
* results_
;
1691 // The location where this type was defined. This exists solely to
1692 // give a location for the fields of the struct if this function
1693 // returns multiple values.
1694 source_location location_
;
1695 // Whether this function takes a variable number of arguments.
1697 // Whether this is a special builtin function which can not simply
1698 // be called. This is used for len, cap, etc.
1702 // The type of a pointer.
1704 class Pointer_type
: public Type
1707 Pointer_type(Type
* to_type
)
1708 : Type(TYPE_POINTER
),
1714 { return this->to_type_
; }
1716 // Import a pointer type.
1717 static Pointer_type
*
1721 make_pointer_type_descriptor_type();
1725 do_traverse(Traverse
*);
1728 do_has_pointer() const
1732 do_hash_for_method(Gogo
*) const;
1735 do_get_backend(Gogo
*);
1738 do_get_init_tree(Gogo
*, tree
, bool);
1741 do_type_descriptor(Gogo
*, Named_type
*);
1744 do_reflection(Gogo
*, std::string
*) const;
1747 do_mangled_name(Gogo
*, std::string
*) const;
1750 do_export(Export
*) const;
1753 // The type to which this type points.
1757 // The type of a field in a struct.
1762 explicit Struct_field(const Typed_identifier
& typed_identifier
)
1763 : typed_identifier_(typed_identifier
), tag_(NULL
)
1773 { return this->typed_identifier_
.type(); }
1775 // The field location.
1778 { return this->typed_identifier_
.location(); }
1780 // Whether the field has a tag.
1783 { return this->tag_
!= NULL
; }
1789 go_assert(this->tag_
!= NULL
);
1793 // Whether this is an anonymous field.
1795 is_anonymous() const
1796 { return this->typed_identifier_
.name().empty(); }
1798 // Set the tag. FIXME: This is never freed.
1800 set_tag(const std::string
& tag
)
1801 { this->tag_
= new std::string(tag
); }
1803 // Set the type. This is only used in error cases.
1805 set_type(Type
* type
)
1806 { this->typed_identifier_
.set_type(type
); }
1809 // The field name, type, and location.
1810 Typed_identifier typed_identifier_
;
1811 // The field tag. This is NULL if the field has no tag.
1815 // A list of struct fields.
1817 class Struct_field_list
1824 // Whether the list is empty.
1827 { return this->entries_
.empty(); }
1829 // Return the number of entries.
1832 { return this->entries_
.size(); }
1834 // Add an entry to the end of the list.
1836 push_back(const Struct_field
& sf
)
1837 { this->entries_
.push_back(sf
); }
1839 // Index into the list.
1842 { return this->entries_
.at(i
); }
1844 // Last entry in list.
1847 { return this->entries_
.back(); }
1851 typedef std::vector
<Struct_field
>::iterator iterator
;
1852 typedef std::vector
<Struct_field
>::const_iterator const_iterator
;
1856 { return this->entries_
.begin(); }
1860 { return this->entries_
.begin(); }
1864 { return this->entries_
.end(); }
1868 { return this->entries_
.end(); }
1871 std::vector
<Struct_field
> entries_
;
1874 // The type of a struct.
1876 class Struct_type
: public Type
1879 Struct_type(Struct_field_list
* fields
, source_location location
)
1880 : Type(TYPE_STRUCT
),
1881 fields_(fields
), location_(location
), all_methods_(NULL
)
1884 // Return the field NAME. This only looks at local fields, not at
1885 // embedded types. If the field is found, and PINDEX is not NULL,
1886 // this sets *PINDEX to the field index. If the field is not found,
1887 // this returns NULL.
1889 find_local_field(const std::string
& name
, unsigned int *pindex
) const;
1891 // Return the field number INDEX.
1893 field(unsigned int index
) const
1894 { return &this->fields_
->at(index
); }
1896 // Get the struct fields.
1897 const Struct_field_list
*
1899 { return this->fields_
; }
1901 // Return the number of fields.
1904 { return this->fields_
->size(); }
1906 // Push a new field onto the end of the struct. This is used when
1907 // building a closure variable.
1909 push_field(const Struct_field
& sf
)
1910 { this->fields_
->push_back(sf
); }
1912 // Return an expression referring to field NAME in STRUCT_EXPR, or
1913 // NULL if there is no field with that name.
1914 Field_reference_expression
*
1915 field_reference(Expression
* struct_expr
, const std::string
& name
,
1916 source_location
) const;
1918 // Return the total number of fields, including embedded fields.
1919 // This is the number of values which can appear in a conversion to
1922 total_field_count() const;
1924 // Whether this type is identical with T.
1926 is_identical(const Struct_type
* t
, bool errors_are_identical
) const;
1928 // Whether this struct type has any hidden fields. This returns
1929 // true if any fields have hidden names, or if any non-pointer
1930 // anonymous fields have types with hidden fields.
1932 struct_has_hidden_fields(const Named_type
* within
, std::string
*) const;
1934 // Return whether NAME is a local field which is not exported. This
1935 // is only used for better error reporting.
1937 is_unexported_local_field(Gogo
*, const std::string
& name
) const;
1939 // If this is an unnamed struct, build the complete list of methods,
1940 // including those from anonymous fields, and build methods stubs if
1943 finalize_methods(Gogo
*);
1945 // Return whether this type has any methods. This should only be
1946 // called after the finalize_methods pass.
1948 has_any_methods() const
1949 { return this->all_methods_
!= NULL
; }
1951 // Return the methods for tihs type. This should only be called
1952 // after the finalize_methods pass.
1955 { return this->all_methods_
; }
1957 // Return the method to use for NAME. This returns NULL if there is
1958 // no such method or if the method is ambiguous. When it returns
1959 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
1961 method_function(const std::string
& name
, bool* is_ambiguous
) const;
1963 // Traverse just the field types of a struct type.
1965 traverse_field_types(Traverse
* traverse
)
1966 { return this->do_traverse(traverse
); }
1968 // Import a struct type.
1973 make_struct_type_descriptor_type();
1977 do_traverse(Traverse
*);
1983 do_has_pointer() const;
1986 do_hash_for_method(Gogo
*) const;
1989 do_get_backend(Gogo
*);
1992 do_get_init_tree(Gogo
*, tree
, bool);
1995 do_type_descriptor(Gogo
*, Named_type
*);
1998 do_reflection(Gogo
*, std::string
*) const;
2001 do_mangled_name(Gogo
*, std::string
*) const;
2004 do_export(Export
*) const;
2007 // Used to avoid infinite loops in field_reference_depth.
2008 struct Saw_named_type
2010 Saw_named_type
* next
;
2014 Field_reference_expression
*
2015 field_reference_depth(Expression
* struct_expr
, const std::string
& name
,
2016 source_location
, Saw_named_type
*,
2017 unsigned int* depth
) const;
2019 // The fields of the struct.
2020 Struct_field_list
* fields_
;
2021 // The place where the struct was declared.
2022 source_location location_
;
2023 // If this struct is unnamed, a list of methods.
2024 Methods
* all_methods_
;
2027 // The type of an array.
2029 class Array_type
: public Type
2032 Array_type(Type
* element_type
, Expression
* length
)
2034 element_type_(element_type
), length_(length
), length_tree_(NULL
)
2037 // Return the element type.
2039 element_type() const
2040 { return this->element_type_
; }
2042 // Return the length. This will return NULL for an open array.
2045 { return this->length_
; }
2047 // Whether this type is identical with T.
2049 is_identical(const Array_type
* t
, bool errors_are_identical
) const;
2051 // Whether this type has any hidden fields.
2053 array_has_hidden_fields(const Named_type
* within
, std::string
* reason
) const
2054 { return this->element_type_
->has_hidden_fields(within
, reason
); }
2056 // Return a tree for the pointer to the values in an array.
2058 value_pointer_tree(Gogo
*, tree array
) const;
2060 // Return a tree for the length of an array with this type.
2062 length_tree(Gogo
*, tree array
);
2064 // Return a tree for the capacity of an array with this type.
2066 capacity_tree(Gogo
*, tree array
);
2068 // Import an array type.
2072 // Return the backend representation of the element type.
2074 get_backend_element(Gogo
*);
2076 // Return the backend representation of the length.
2078 get_backend_length(Gogo
*);
2081 make_array_type_descriptor_type();
2084 make_slice_type_descriptor_type();
2088 do_traverse(Traverse
* traverse
);
2094 do_has_pointer() const
2096 return this->length_
== NULL
|| this->element_type_
->has_pointer();
2100 do_hash_for_method(Gogo
*) const;
2103 do_check_make_expression(Expression_list
*, source_location
);
2106 do_get_backend(Gogo
*);
2109 do_get_init_tree(Gogo
*, tree
, bool);
2112 do_make_expression_tree(Translate_context
*, Expression_list
*,
2116 do_type_descriptor(Gogo
*, Named_type
*);
2119 do_reflection(Gogo
*, std::string
*) const;
2122 do_mangled_name(Gogo
*, std::string
*) const;
2125 do_export(Export
*) const;
2132 get_length_tree(Gogo
*);
2135 array_type_descriptor(Gogo
*, Named_type
*);
2138 slice_type_descriptor(Gogo
*, Named_type
*);
2140 // The type of elements of the array.
2141 Type
* element_type_
;
2142 // The number of elements. This may be NULL.
2143 Expression
* length_
;
2144 // The length as a tree. We only want to compute this once.
2148 // The type of a map.
2150 class Map_type
: public Type
2153 Map_type(Type
* key_type
, Type
* val_type
, source_location location
)
2155 key_type_(key_type
), val_type_(val_type
), location_(location
)
2158 // Return the key type.
2161 { return this->key_type_
; }
2163 // Return the value type.
2166 { return this->val_type_
; }
2168 // Whether this type is identical with T.
2170 is_identical(const Map_type
* t
, bool errors_are_identical
) const;
2172 // Import a map type.
2177 make_map_type_descriptor_type();
2181 do_traverse(Traverse
*);
2187 do_has_pointer() const
2191 do_hash_for_method(Gogo
*) const;
2194 do_check_make_expression(Expression_list
*, source_location
);
2197 do_get_backend(Gogo
*);
2200 do_get_init_tree(Gogo
*, tree
, bool);
2203 do_make_expression_tree(Translate_context
*, Expression_list
*,
2207 do_type_descriptor(Gogo
*, Named_type
*);
2210 do_reflection(Gogo
*, std::string
*) const;
2213 do_mangled_name(Gogo
*, std::string
*) const;
2216 do_export(Export
*) const;
2223 // Where the type was defined.
2224 source_location location_
;
2227 // The type of a channel.
2229 class Channel_type
: public Type
2232 Channel_type(bool may_send
, bool may_receive
, Type
* element_type
)
2233 : Type(TYPE_CHANNEL
),
2234 may_send_(may_send
), may_receive_(may_receive
),
2235 element_type_(element_type
)
2236 { go_assert(may_send
|| may_receive
); }
2238 // Whether this channel can send data.
2241 { return this->may_send_
; }
2243 // Whether this channel can receive data.
2246 { return this->may_receive_
; }
2248 // The type of the values that may be sent on this channel. This is
2249 // NULL if any type may be sent.
2251 element_type() const
2252 { return this->element_type_
; }
2254 // Whether this type is identical with T.
2256 is_identical(const Channel_type
* t
, bool errors_are_identical
) const;
2258 // Import a channel type.
2259 static Channel_type
*
2263 make_chan_type_descriptor_type();
2267 do_traverse(Traverse
* traverse
)
2268 { return Type::traverse(this->element_type_
, traverse
); }
2271 do_has_pointer() const
2275 do_hash_for_method(Gogo
*) const;
2278 do_check_make_expression(Expression_list
*, source_location
);
2281 do_get_backend(Gogo
*);
2284 do_get_init_tree(Gogo
*, tree
, bool);
2287 do_make_expression_tree(Translate_context
*, Expression_list
*,
2291 do_type_descriptor(Gogo
*, Named_type
*);
2294 do_reflection(Gogo
*, std::string
*) const;
2297 do_mangled_name(Gogo
*, std::string
*) const;
2300 do_export(Export
*) const;
2303 // Whether this channel can send data.
2305 // Whether this channel can receive data.
2307 // The types of elements which may be sent on this channel. If this
2308 // is NULL, it means that any type may be sent.
2309 Type
* element_type_
;
2312 // An interface type.
2314 class Interface_type
: public Type
2317 Interface_type(Typed_identifier_list
* methods
, source_location location
)
2318 : Type(TYPE_INTERFACE
),
2319 methods_(methods
), location_(location
)
2320 { go_assert(methods
== NULL
|| !methods
->empty()); }
2322 // The location where the interface type was defined.
2325 { return this->location_
; }
2327 // Return whether this is an empty interface.
2330 { return this->methods_
== NULL
; }
2332 // Return the list of methods. This will return NULL for an empty
2334 const Typed_identifier_list
*
2336 { return this->methods_
; }
2338 // Return the number of methods.
2340 method_count() const
2341 { return this->methods_
== NULL
? 0 : this->methods_
->size(); }
2343 // Return the method NAME, or NULL.
2344 const Typed_identifier
*
2345 find_method(const std::string
& name
) const;
2347 // Return the zero-based index of method NAME.
2349 method_index(const std::string
& name
) const;
2351 // Finalize the methods. This handles interface inheritance.
2355 // Return true if T implements this interface. If this returns
2356 // false, and REASON is not NULL, it sets *REASON to the reason that
2359 implements_interface(const Type
* t
, std::string
* reason
) const;
2361 // Whether this type is identical with T. REASON is as in
2362 // implements_interface.
2364 is_identical(const Interface_type
* t
, bool errors_are_identical
) const;
2366 // Whether we can assign T to this type. is_identical is known to
2369 is_compatible_for_assign(const Interface_type
*, std::string
* reason
) const;
2371 // Return whether NAME is a method which is not exported. This is
2372 // only used for better error reporting.
2374 is_unexported_method(Gogo
*, const std::string
& name
) const;
2376 // Import an interface type.
2377 static Interface_type
*
2380 // Make a struct for an empty interface type.
2382 get_backend_empty_interface_type(Gogo
*);
2385 make_interface_type_descriptor_type();
2389 do_traverse(Traverse
*);
2392 do_has_pointer() const
2396 do_hash_for_method(Gogo
*) const;
2399 do_get_backend(Gogo
*);
2402 do_get_init_tree(Gogo
* gogo
, tree
, bool);
2405 do_type_descriptor(Gogo
*, Named_type
*);
2408 do_reflection(Gogo
*, std::string
*) const;
2411 do_mangled_name(Gogo
*, std::string
*) const;
2414 do_export(Export
*) const;
2417 // The list of methods associated with the interface. This will be
2418 // NULL for the empty interface.
2419 Typed_identifier_list
* methods_
;
2420 // The location where the interface was defined.
2421 source_location location_
;
2424 // The value we keep for a named type. This lets us get the right
2425 // name when we convert to trees. Note that we don't actually keep
2426 // the name here; the name is in the Named_object which points to
2427 // this. This object exists to hold a unique tree which represents
2430 class Named_type
: public Type
2433 Named_type(Named_object
* named_object
, Type
* type
, source_location location
)
2435 named_object_(named_object
), in_function_(NULL
), type_(type
),
2436 local_methods_(NULL
), all_methods_(NULL
),
2437 interface_method_tables_(NULL
), pointer_interface_method_tables_(NULL
),
2438 location_(location
), named_btype_(NULL
), dependencies_(),
2439 is_visible_(true), is_error_(false), is_converted_(false),
2440 is_circular_(false), seen_(0)
2443 // Return the associated Named_object. This holds the actual name.
2446 { return this->named_object_
; }
2449 named_object() const
2450 { return this->named_object_
; }
2452 // Set the Named_object. This is used when we see a type
2453 // declaration followed by a type.
2455 set_named_object(Named_object
* no
)
2456 { this->named_object_
= no
; }
2458 // Return the function in which this type is defined. This will
2459 // return NULL for a type defined in global scope.
2462 { return this->in_function_
; }
2464 // Set the function in which this type is defined.
2466 set_in_function(Named_object
* f
)
2467 { this->in_function_
= f
; }
2469 // Return the name of the type.
2473 // Return the name of the type for an error message. The difference
2474 // is that if the type is defined in a different package, this will
2475 // return PACKAGE.NAME.
2477 message_name() const;
2479 // Return the underlying type.
2482 { return this->type_
; }
2486 { return this->type_
; }
2488 // Return the location.
2491 { return this->location_
; }
2493 // Whether this type is visible. This only matters when parsing.
2496 { return this->is_visible_
; }
2498 // Mark this type as visible.
2501 { this->is_visible_
= true; }
2503 // Mark this type as invisible.
2506 { this->is_visible_
= false; }
2508 // Whether this is a builtin type.
2511 { return this->location_
== BUILTINS_LOCATION
; }
2513 // Whether this is a circular type: a pointer or function type that
2514 // refers to itself, which is not possible in C.
2517 { return this->is_circular_
; }
2519 // Return the base type for this type.
2526 // Return whether this is an error type.
2528 is_named_error_type() const;
2530 // Add a method to this type.
2532 add_method(const std::string
& name
, Function
*);
2534 // Add a method declaration to this type.
2536 add_method_declaration(const std::string
& name
, Package
* package
,
2537 Function_type
* type
, source_location location
);
2539 // Add an existing method--one defined before the type itself was
2540 // defined--to a type.
2542 add_existing_method(Named_object
*);
2544 // Look up a local method.
2546 find_local_method(const std::string
& name
) const;
2548 // Return the list of local methods.
2550 local_methods() const
2551 { return this->local_methods_
; }
2553 // Build the complete list of methods, including those from
2554 // anonymous fields, and build method stubs if needed.
2556 finalize_methods(Gogo
*);
2558 // Return whether this type has any methods. This should only be
2559 // called after the finalize_methods pass.
2561 has_any_methods() const
2562 { return this->all_methods_
!= NULL
; }
2564 // Return the methods for this type. This should only be called
2565 // after the finalized_methods pass.
2568 { return this->all_methods_
; }
2570 // Return the method to use for NAME. This returns NULL if there is
2571 // no such method or if the method is ambiguous. When it returns
2572 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2574 method_function(const std::string
& name
, bool *is_ambiguous
) const;
2576 // Return whether NAME is a known field or method which is not
2577 // exported. This is only used for better error reporting.
2579 is_unexported_local_method(Gogo
*, const std::string
& name
) const;
2581 // Return a pointer to the interface method table for this type for
2582 // the interface INTERFACE. If IS_POINTER is true, set the type
2583 // descriptor to a pointer to this type, otherwise set it to this
2586 interface_method_table(Gogo
*, const Interface_type
* interface
,
2589 // Whether this type has any hidden fields.
2591 named_type_has_hidden_fields(std::string
* reason
) const;
2593 // Note that a type must be converted to the backend representation
2594 // before we convert this type.
2596 add_dependency(Named_type
* nt
)
2597 { this->dependencies_
.push_back(nt
); }
2601 export_named_type(Export
*, const std::string
& name
) const;
2603 // Import a named type.
2605 import_named_type(Import
*, Named_type
**);
2607 // Initial conversion to backend representation.
2613 do_traverse(Traverse
* traverse
)
2614 { return Type::traverse(this->type_
, traverse
); }
2620 do_has_pointer() const;
2623 do_hash_for_method(Gogo
*) const;
2626 do_check_make_expression(Expression_list
* args
, source_location location
)
2627 { return this->type_
->check_make_expression(args
, location
); }
2630 do_get_backend(Gogo
*);
2633 do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
2634 { return this->type_
->get_typed_init_tree(gogo
, type_tree
, is_clear
); }
2637 do_make_expression_tree(Translate_context
* context
, Expression_list
* args
,
2638 source_location location
)
2639 { return this->type_
->make_expression_tree(context
, args
, location
); }
2642 do_type_descriptor(Gogo
*, Named_type
*);
2645 do_reflection(Gogo
*, std::string
*) const;
2648 do_mangled_name(Gogo
*, std::string
* ret
) const;
2651 do_export(Export
*) const;
2654 // Create the placeholder during conversion.
2656 create_placeholder(Gogo
*);
2658 // A mapping from interfaces to the associated interface method
2659 // tables for this type. This maps to a decl.
2660 typedef Unordered_map_hash(const Interface_type
*, tree
, Type_hash_identical
,
2661 Type_identical
) Interface_method_tables
;
2663 // A pointer back to the Named_object for this type.
2664 Named_object
* named_object_
;
2665 // If this type is defined in a function, a pointer back to the
2666 // function in which it is defined.
2667 Named_object
* in_function_
;
2670 // The list of methods defined for this type. Any named type can
2672 Bindings
* local_methods_
;
2673 // The full list of methods for this type, including methods
2674 // declared for anonymous fields.
2675 Methods
* all_methods_
;
2676 // A mapping from interfaces to the associated interface method
2677 // tables for this type.
2678 Interface_method_tables
* interface_method_tables_
;
2679 // A mapping from interfaces to the associated interface method
2680 // tables for pointers to this type.
2681 Interface_method_tables
* pointer_interface_method_tables_
;
2682 // The location where this type was defined.
2683 source_location location_
;
2684 // The backend representation of this type during backend
2685 // conversion. This is used to avoid endless recursion when a named
2686 // type refers to itself.
2687 Btype
* named_btype_
;
2688 // A list of types which must be converted to the backend
2689 // representation before this type can be converted. This is for
2691 // type S1 { p *S2 }
2693 // where we can't convert S2 to the backend representation unless we
2694 // have converted S1.
2695 std::vector
<Named_type
*> dependencies_
;
2696 // Whether this type is visible. This is false if this type was
2697 // created because it was referenced by an imported object, but the
2698 // type itself was not exported. This will always be true for types
2699 // created in the current package.
2701 // Whether this type is erroneous.
2703 // Whether this type has been converted to the backend
2706 // Whether this is a pointer or function type which refers to the
2709 // In a recursive operation such as has_hidden_fields, this flag is
2710 // used to prevent infinite recursion when a type refers to itself.
2711 // This is mutable because it is always reset to false when the
2716 // A forward declaration. This handles a type which has been declared
2719 class Forward_declaration_type
: public Type
2722 Forward_declaration_type(Named_object
* named_object
);
2724 // The named object associated with this type declaration. This
2725 // will be resolved.
2730 named_object() const;
2732 // Return the name of the type.
2736 // Return the type to which this points. Give an error if the type
2737 // has not yet been defined.
2744 // Whether the base type has been defined.
2748 // Add a method to this type.
2750 add_method(const std::string
& name
, Function
*);
2752 // Add a method declaration to this type.
2754 add_method_declaration(const std::string
& name
, Function_type
*,
2759 do_traverse(Traverse
* traverse
);
2762 do_has_pointer() const
2763 { return this->real_type()->has_pointer(); }
2766 do_hash_for_method(Gogo
* gogo
) const
2767 { return this->real_type()->hash_for_method(gogo
); }
2770 do_check_make_expression(Expression_list
* args
, source_location location
)
2771 { return this->base()->check_make_expression(args
, location
); }
2774 do_get_backend(Gogo
* gogo
);
2777 do_get_init_tree(Gogo
* gogo
, tree type_tree
, bool is_clear
)
2778 { return this->base()->get_typed_init_tree(gogo
, type_tree
, is_clear
); }
2781 do_make_expression_tree(Translate_context
* context
, Expression_list
* args
,
2782 source_location location
)
2783 { return this->base()->make_expression_tree(context
, args
, location
); }
2786 do_type_descriptor(Gogo
*, Named_type
*);
2789 do_reflection(Gogo
*, std::string
*) const;
2792 do_mangled_name(Gogo
*, std::string
* ret
) const;
2795 do_export(Export
*) const;
2798 // Issue a warning about a use of an undefined type.
2802 // The type declaration.
2803 Named_object
* named_object_
;
2804 // Whether we have issued a warning about this type.
2805 mutable bool warned_
;
2808 // The Type_context struct describes what we expect for the type of an
2813 // The exact type we expect, if known. This may be NULL.
2815 // Whether an abstract type is permitted.
2816 bool may_be_abstract
;
2820 : type(NULL
), may_be_abstract(false)
2823 Type_context(Type
* a_type
, bool a_may_be_abstract
)
2824 : type(a_type
), may_be_abstract(a_may_be_abstract
)
2828 #endif // !defined(GO_TYPES_H)