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 a tree representing 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);
897 do_get_tree(Gogo*) = 0;
900 do_get_init_tree(Gogo*, tree, bool) = 0;
903 do_make_expression_tree(Translate_context*, Expression_list*,
907 do_type_descriptor(Gogo*, Named_type* name) = 0;
910 do_reflection(Gogo*, std::string*) const = 0;
914 do_mangled_name(Gogo*, std::string*) const = 0;
917 do_export(Export*) const;
919 // Return whether an expression is an integer.
921 check_int_value(Expression*, const char*, source_location);
923 // Return whether a method expects a pointer as the receiver.
925 method_expects_pointer(const Named_object*);
927 // Finalize the methods for a type.
929 finalize_methods(Gogo*, const Type*, source_location, Methods**);
931 // Return a method from a set of methods.
933 method_function(const Methods*, const std::string& name,
936 // Return a composite literal for the type descriptor entry for a
939 type_descriptor(Gogo*, Type*);
941 // Return a composite literal for the type descriptor entry for
942 // TYPE, using NAME as the name of the type.
944 named_type_descriptor(Gogo*, Type* type, Named_type* name);
946 // Return a composite literal for a plain type descriptor for this
947 // type with the given kind and name.
949 plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
951 // Build a composite literal for the basic type descriptor.
953 type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
954 const Methods*, bool only_value_methods);
956 // Make a builtin struct type from a list of fields.
958 make_builtin_struct_type(int nfields, ...);
960 // Make a builtin named type.
962 make_builtin_named_type(const char* name, Type* type);
964 // For the benefit of child class reflection string generation.
966 append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
967 { type->do_reflection(gogo, ret); }
969 // For the benefit of child class mangling.
971 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
972 { type->do_mangled_name(gogo, ret); }
974 // Incorporate a string into a hash code.
976 hash_string(const std::string&, unsigned int);
978 // Return the backend representation for the underlying type of a
981 get_named_base_btype(Gogo* gogo, Type* base_type)
982 { return base_type->get_btype_without_hash(gogo); }
985 // Convert to the desired type classification, or return NULL. This
986 // is a controlled dynamic_cast.
987 template<typename Type_class, Type_classification type_classification>
991 Type* base = this->base();
992 return (base->classification_ == type_classification
993 ? static_cast<Type_class*>(base)
997 template<typename Type_class, Type_classification type_classification>
1001 const Type* base = this->base();
1002 return (base->classification_ == type_classification
1003 ? static_cast<Type_class*>(base)
1007 template<typename Type_class, Type_classification type_classification>
1011 return (this->classification_ == type_classification
1012 ? static_cast<Type_class*>(this)
1016 template<typename Type_class, Type_classification type_classification>
1018 convert_no_base() const
1020 return (this->classification_ == type_classification
1021 ? static_cast<Type_class*>(this)
1025 // Support for are_assignable and are_assignable_hidden_ok.
1027 are_assignable_check_hidden(const Type* lhs, const Type* rhs,
1028 bool check_hidden_fields, std::string* reason);
1030 // Get the hash and equality functions for a type.
1032 type_functions(const char** hash_fn, const char** equal_fn) const;
1034 // Build a composite literal for the uncommon type information.
1036 uncommon_type_constructor(Gogo*, Type* uncommon_type,
1037 Named_type*, const Methods*,
1038 bool only_value_methods) const;
1040 // Build a composite literal for the methods.
1042 methods_constructor(Gogo*, Type* methods_type, const Methods*,
1043 bool only_value_methods) const;
1045 // Build a composite literal for one method.
1047 method_constructor(Gogo*, Type* method_type, const std::string& name,
1048 const Method*) const;
1051 build_receive_return_type(tree type);
1053 // A hash table we use to avoid infinite recursion.
1054 typedef Unordered_set_hash(const Named_type*, Type_hash_identical,
1055 Type_identical) Types_seen;
1057 // Add all methods for TYPE to the list of methods for THIS.
1059 add_methods_for_type(const Type* type, const Method::Field_indexes*,
1060 unsigned int depth, bool, bool, Types_seen*,
1064 add_local_methods_for_type(const Named_type* type,
1065 const Method::Field_indexes*,
1066 unsigned int depth, bool, bool, Methods**);
1069 add_embedded_methods_for_type(const Type* type,
1070 const Method::Field_indexes*,
1071 unsigned int depth, bool, bool, Types_seen*,
1075 add_interface_methods_for_type(const Type* type,
1076 const Method::Field_indexes*,
1077 unsigned int depth, Methods**);
1079 // Build stub methods for a type.
1081 build_stub_methods(Gogo*, const Type* type, const Methods* methods,
1085 build_one_stub_method(Gogo*, Method*, const char* receiver_name,
1086 const Typed_identifier_list*, bool is_varargs,
1090 apply_field_indexes(Expression*, const Method::Field_indexes*,
1093 // Look for a field or method named NAME in TYPE.
1095 find_field_or_method(const Type* type, const std::string& name,
1096 bool receiver_can_be_pointer,
1097 std::vector<const Named_type*>*, int* level,
1098 bool* is_method, bool* found_pointer_method,
1099 std::string* ambig1, std::string* ambig2);
1101 // Get a tree for a type without looking in the hash table for
1104 get_tree_without_hash(Gogo*);
1106 // Get the backend representation for a type without looking in the
1107 // hash table for identical types.
1109 get_btype_without_hash(Gogo*);
1111 // A mapping from Type to tree, used to ensure that the GIMPLE
1112 // representation of identical types is identical.
1113 typedef Unordered_map_hash(const Type*, tree, Type_hash_identical,
1114 Type_identical) Type_trees;
1116 static Type_trees type_trees;
1118 // A list of builtin named types.
1119 static std::vector<Named_type*> named_builtin_types;
1121 // The type classification.
1122 Type_classification classification_;
1123 // The tree representation of the type, once it has been determined.
1125 // The decl for the type descriptor for this type. This starts out
1126 // as NULL and is filled in as needed.
1127 tree type_descriptor_decl_;
1130 // Type hash table operations.
1132 class Type_hash_identical
1136 operator()(const Type* type) const
1137 { return type->hash_for_method(NULL); }
1140 class Type_identical
1144 operator()(const Type* t1, const Type* t2) const
1145 { return Type::are_identical(t1, t2, false, NULL); }
1148 // An identifier with a type.
1150 class Typed_identifier
1153 Typed_identifier(const std::string& name, Type* type,
1154 source_location location)
1155 : name_(name), type_(type), location_(location)
1161 { return this->name_; }
1166 { return this->type_; }
1168 // Return the location where the name was seen. This is not always
1172 { return this->location_; }
1174 // Set the type--sometimes we see the identifier before the type.
1176 set_type(Type* type)
1178 go_assert(this->type_ == NULL || type->is_error_type());
1187 // The location where the name was seen.
1188 source_location location_;
1191 // A list of Typed_identifiers.
1193 class Typed_identifier_list
1196 Typed_identifier_list()
1200 // Whether the list is empty.
1203 { return this->entries_.empty(); }
1205 // Return the number of entries in the list.
1208 { return this->entries_.size(); }
1210 // Add an entry to the end of the list.
1212 push_back(const Typed_identifier& td)
1213 { this->entries_.push_back(td); }
1215 // Remove an entry from the end of the list.
1218 { this->entries_.pop_back(); }
1220 // Set the type of entry I to TYPE.
1222 set_type(size_t i, Type* type)
1224 go_assert(i < this->entries_.size());
1225 this->entries_[i].set_type(type);
1228 // Sort the entries by name.
1234 traverse(Traverse*);
1236 // Return the first and last elements.
1239 { return this->entries_.front(); }
1241 const Typed_identifier&
1243 { return this->entries_.front(); }
1247 { return this->entries_.back(); }
1249 const Typed_identifier&
1251 { return this->entries_.back(); }
1253 const Typed_identifier&
1255 { return this->entries_.at(i); }
1258 set(size_t i, const Typed_identifier& t)
1259 { this->entries_.at(i) = t; }
1264 go_assert(c <= this->entries_.size());
1265 this->entries_.resize(c, Typed_identifier("", NULL, UNKNOWN_LOCATION));
1270 typedef std::vector<Typed_identifier>::iterator iterator;
1271 typedef std::vector<Typed_identifier>::const_iterator const_iterator;
1275 { return this->entries_.begin(); }
1279 { return this->entries_.begin(); }
1283 { return this->entries_.end(); }
1287 { return this->entries_.end(); }
1289 // Return a copy of this list. This returns an independent copy of
1290 // the vector, but does not copy the types.
1291 Typed_identifier_list*
1295 std::vector<Typed_identifier> entries_;
1298 // The type of an integer.
1300 class Integer_type : public Type
1303 // Create a new integer type.
1305 create_integer_type(const char* name, bool is_unsigned, int bits,
1306 int runtime_type_kind);
1308 // Look up an existing integer type.
1310 lookup_integer_type(const char* name);
1312 // Create an abstract integer type.
1313 static Integer_type*
1314 create_abstract_integer_type();
1316 // Whether this is an abstract integer type.
1319 { return this->is_abstract_; }
1321 // Whether this is an unsigned type.
1324 { return this->is_unsigned_; }
1326 // The number of bits.
1329 { return this->bits_; }
1331 // Whether this type is the same as T.
1333 is_identical(const Integer_type* t) const;
1337 do_hash_for_method(Gogo*) const;
1343 do_get_init_tree(Gogo*, tree, bool);
1346 do_type_descriptor(Gogo*, Named_type*);
1349 do_reflection(Gogo*, std::string*) const;
1352 do_mangled_name(Gogo*, std::string*) const;
1355 Integer_type(bool is_abstract, bool is_unsigned, int bits,
1356 int runtime_type_kind)
1357 : Type(TYPE_INTEGER),
1358 is_abstract_(is_abstract), is_unsigned_(is_unsigned), bits_(bits),
1359 runtime_type_kind_(runtime_type_kind)
1362 // Map names of integer types to the types themselves.
1363 typedef std::map<std::string, Named_type*> Named_integer_types;
1364 static Named_integer_types named_integer_types;
1366 // True if this is an abstract type.
1368 // True if this is an unsigned type.
1370 // The number of bits.
1372 // The runtime type code used in the type descriptor for this type.
1373 int runtime_type_kind_;
1376 // The type of a floating point number.
1378 class Float_type : public Type
1381 // Create a new float type.
1383 create_float_type(const char* name, int bits, int runtime_type_kind);
1385 // Look up an existing float type.
1387 lookup_float_type(const char* name);
1389 // Create an abstract float type.
1391 create_abstract_float_type();
1393 // Whether this is an abstract float type.
1396 { return this->is_abstract_; }
1398 // The number of bits.
1401 { return this->bits_; }
1403 // Whether this type is the same as T.
1405 is_identical(const Float_type* t) const;
1409 do_hash_for_method(Gogo*) const;
1415 do_get_init_tree(Gogo*, tree, bool);
1418 do_type_descriptor(Gogo*, Named_type*);
1421 do_reflection(Gogo*, std::string*) const;
1424 do_mangled_name(Gogo*, std::string*) const;
1427 Float_type(bool is_abstract, int bits, int runtime_type_kind)
1429 is_abstract_(is_abstract), bits_(bits),
1430 runtime_type_kind_(runtime_type_kind)
1433 // Map names of float types to the types themselves.
1434 typedef std::map<std::string, Named_type*> Named_float_types;
1435 static Named_float_types named_float_types;
1437 // True if this is an abstract type.
1439 // The number of bits in the floating point value.
1441 // The runtime type code used in the type descriptor for this type.
1442 int runtime_type_kind_;
1445 // The type of a complex number.
1447 class Complex_type : public Type
1450 // Create a new complex type.
1452 create_complex_type(const char* name, int bits, int runtime_type_kind);
1454 // Look up an existing complex type.
1456 lookup_complex_type(const char* name);
1458 // Create an abstract complex type.
1459 static Complex_type*
1460 create_abstract_complex_type();
1462 // Whether this is an abstract complex type.
1465 { return this->is_abstract_; }
1467 // The number of bits: 64 or 128.
1469 { return this->bits_; }
1471 // Whether this type is the same as T.
1473 is_identical(const Complex_type* t) const;
1477 do_hash_for_method(Gogo*) const;
1483 do_get_init_tree(Gogo*, tree, bool);
1486 do_type_descriptor(Gogo*, Named_type*);
1489 do_reflection(Gogo*, std::string*) const;
1492 do_mangled_name(Gogo*, std::string*) const;
1495 Complex_type(bool is_abstract, int bits, int runtime_type_kind)
1496 : Type(TYPE_COMPLEX),
1497 is_abstract_(is_abstract), bits_(bits),
1498 runtime_type_kind_(runtime_type_kind)
1501 // Map names of complex types to the types themselves.
1502 typedef std::map<std::string, Named_type*> Named_complex_types;
1503 static Named_complex_types named_complex_types;
1505 // True if this is an abstract type.
1507 // The number of bits in the complex value--64 or 128.
1509 // The runtime type code used in the type descriptor for this type.
1510 int runtime_type_kind_;
1513 // The type of a string.
1515 class String_type : public Type
1522 // Return a tree for the length of STRING.
1524 length_tree(Gogo*, tree string);
1526 // Return a tree which points to the bytes of STRING.
1528 bytes_tree(Gogo*, tree string);
1532 do_has_pointer() const
1539 do_get_init_tree(Gogo* gogo, tree, bool);
1542 do_type_descriptor(Gogo*, Named_type*);
1545 do_reflection(Gogo*, std::string*) const;
1548 do_mangled_name(Gogo*, std::string* ret) const;
1551 // The named string type.
1552 static Named_type* string_type_;
1555 // The type of a function.
1557 class Function_type : public Type
1560 Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
1561 Typed_identifier_list* results, source_location location)
1562 : Type(TYPE_FUNCTION),
1563 receiver_(receiver), parameters_(parameters), results_(results),
1564 location_(location), is_varargs_(false), is_builtin_(false)
1567 // Get the receiver.
1568 const Typed_identifier*
1570 { return this->receiver_; }
1572 // Get the return names and types.
1573 const Typed_identifier_list*
1575 { return this->results_; }
1577 // Get the parameter names and types.
1578 const Typed_identifier_list*
1580 { return this->parameters_; }
1582 // Whether this is a varargs function.
1585 { return this->is_varargs_; }
1587 // Whether this is a builtin function.
1590 { return this->is_builtin_; }
1592 // The location where this type was defined.
1595 { return this->location_; }
1597 // Return whether this is a method type.
1600 { return this->receiver_ != NULL; }
1602 // Whether T is a valid redeclaration of this type. This is called
1603 // when a function is declared more than once.
1605 is_valid_redeclaration(const Function_type* t, std::string*) const;
1607 // Whether this type is the same as T.
1609 is_identical(const Function_type* t, bool ignore_receiver,
1610 bool errors_are_identical, std::string*) const;
1612 // Record that this is a varargs function.
1615 { this->is_varargs_ = true; }
1617 // Record that this is a builtin function.
1620 { this->is_builtin_ = true; }
1622 // Import a function type.
1623 static Function_type*
1626 // Return a copy of this type without a receiver. This is only
1627 // valid for a method type.
1629 copy_without_receiver() const;
1631 // Return a copy of this type with a receiver. This is used when an
1632 // interface method is attached to a named or struct type.
1634 copy_with_receiver(Type*) const;
1637 make_function_type_descriptor_type();
1641 do_traverse(Traverse*);
1643 // A trampoline function has a pointer which matters for GC.
1645 do_has_pointer() const
1649 do_hash_for_method(Gogo*) const;
1655 do_get_init_tree(Gogo*, tree, bool);
1658 do_type_descriptor(Gogo*, Named_type*);
1661 do_reflection(Gogo*, std::string*) const;
1664 do_mangled_name(Gogo*, std::string*) const;
1667 do_export(Export*) const;
1671 type_descriptor_params(Type*, const Typed_identifier*,
1672 const Typed_identifier_list*);
1674 // The receiver name and type. This will be NULL for a normal
1675 // function, non-NULL for a method.
1676 Typed_identifier* receiver_;
1677 // The parameter names and types.
1678 Typed_identifier_list* parameters_;
1679 // The result names and types. This will be NULL if no result was
1681 Typed_identifier_list* results_;
1682 // The location where this type was defined. This exists solely to
1683 // give a location for the fields of the struct if this function
1684 // returns multiple values.
1685 source_location location_;
1686 // Whether this function takes a variable number of arguments.
1688 // Whether this is a special builtin function which can not simply
1689 // be called. This is used for len, cap, etc.
1693 // The type of a pointer.
1695 class Pointer_type : public Type
1698 Pointer_type(Type* to_type)
1699 : Type(TYPE_POINTER),
1705 { return this->to_type_; }
1707 // Import a pointer type.
1708 static Pointer_type*
1712 make_pointer_type_descriptor_type();
1716 do_traverse(Traverse*);
1719 do_has_pointer() const
1723 do_hash_for_method(Gogo*) const;
1729 do_get_init_tree(Gogo*, tree, bool);
1732 do_type_descriptor(Gogo*, Named_type*);
1735 do_reflection(Gogo*, std::string*) const;
1738 do_mangled_name(Gogo*, std::string*) const;
1741 do_export(Export*) const;
1744 // The type to which this type points.
1748 // The type of a field in a struct.
1753 explicit Struct_field(const Typed_identifier& typed_identifier)
1754 : typed_identifier_(typed_identifier), tag_(NULL)
1764 { return this->typed_identifier_.type(); }
1766 // The field location.
1769 { return this->typed_identifier_.location(); }
1771 // Whether the field has a tag.
1774 { return this->tag_ != NULL; }
1780 go_assert(this->tag_ != NULL);
1784 // Whether this is an anonymous field.
1786 is_anonymous() const
1787 { return this->typed_identifier_.name().empty(); }
1789 // Set the tag. FIXME: This is never freed.
1791 set_tag(const std::string& tag)
1792 { this->tag_ = new std::string(tag); }
1794 // Set the type. This is only used in error cases.
1796 set_type(Type* type)
1797 { this->typed_identifier_.set_type(type); }
1800 // The field name, type, and location.
1801 Typed_identifier typed_identifier_;
1802 // The field tag. This is NULL if the field has no tag.
1806 // A list of struct fields.
1808 class Struct_field_list
1815 // Whether the list is empty.
1818 { return this->entries_.empty(); }
1820 // Return the number of entries.
1823 { return this->entries_.size(); }
1825 // Add an entry to the end of the list.
1827 push_back(const Struct_field& sf)
1828 { this->entries_.push_back(sf); }
1830 // Index into the list.
1833 { return this->entries_.at(i); }
1835 // Last entry in list.
1838 { return this->entries_.back(); }
1842 typedef std::vector<Struct_field>::iterator iterator;
1843 typedef std::vector<Struct_field>::const_iterator const_iterator;
1847 { return this->entries_.begin(); }
1851 { return this->entries_.begin(); }
1855 { return this->entries_.end(); }
1859 { return this->entries_.end(); }
1862 std::vector<Struct_field> entries_;
1865 // The type of a struct.
1867 class Struct_type : public Type
1870 Struct_type(Struct_field_list* fields, source_location location)
1871 : Type(TYPE_STRUCT),
1872 fields_(fields), location_(location), all_methods_(NULL)
1875 // Return the field NAME. This only looks at local fields, not at
1876 // embedded types. If the field is found, and PINDEX is not NULL,
1877 // this sets *PINDEX to the field index. If the field is not found,
1878 // this returns NULL.
1880 find_local_field(const std::string& name, unsigned int *pindex) const;
1882 // Return the field number INDEX.
1884 field(unsigned int index) const
1885 { return &this->fields_->at(index); }
1887 // Get the struct fields.
1888 const Struct_field_list*
1890 { return this->fields_; }
1892 // Return the number of fields.
1895 { return this->fields_->size(); }
1897 // Push a new field onto the end of the struct. This is used when
1898 // building a closure variable.
1900 push_field(const Struct_field& sf)
1901 { this->fields_->push_back(sf); }
1903 // Return an expression referring to field NAME in STRUCT_EXPR, or
1904 // NULL if there is no field with that name.
1905 Field_reference_expression*
1906 field_reference(Expression* struct_expr, const std::string& name,
1907 source_location) const;
1909 // Return the total number of fields, including embedded fields.
1910 // This is the number of values which can appear in a conversion to
1913 total_field_count() const;
1915 // Whether this type is identical with T.
1917 is_identical(const Struct_type* t, bool errors_are_identical) const;
1919 // Whether this struct type has any hidden fields. This returns
1920 // true if any fields have hidden names, or if any non-pointer
1921 // anonymous fields have types with hidden fields.
1923 struct_has_hidden_fields(const Named_type* within, std::string*) const;
1925 // Return whether NAME is a local field which is not exported. This
1926 // is only used for better error reporting.
1928 is_unexported_local_field(Gogo*, const std::string& name) const;
1930 // If this is an unnamed struct, build the complete list of methods,
1931 // including those from anonymous fields, and build methods stubs if
1934 finalize_methods(Gogo*);
1936 // Return whether this type has any methods. This should only be
1937 // called after the finalize_methods pass.
1939 has_any_methods() const
1940 { return this->all_methods_ != NULL; }
1942 // Return the methods for tihs type. This should only be called
1943 // after the finalize_methods pass.
1946 { return this->all_methods_; }
1948 // Return the method to use for NAME. This returns NULL if there is
1949 // no such method or if the method is ambiguous. When it returns
1950 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
1952 method_function(const std::string& name, bool* is_ambiguous) const;
1954 // Traverse just the field types of a struct type.
1956 traverse_field_types(Traverse* traverse)
1957 { return this->do_traverse(traverse); }
1959 // Import a struct type.
1964 make_struct_type_descriptor_type();
1968 do_traverse(Traverse*);
1974 do_has_pointer() const;
1977 do_hash_for_method(Gogo*) const;
1983 do_get_init_tree(Gogo*, tree, bool);
1986 do_type_descriptor(Gogo*, Named_type*);
1989 do_reflection(Gogo*, std::string*) const;
1992 do_mangled_name(Gogo*, std::string*) const;
1995 do_export(Export*) const;
1998 // Used to avoid infinite loops in field_reference_depth.
1999 struct Saw_named_type
2001 Saw_named_type* next;
2005 Field_reference_expression*
2006 field_reference_depth(Expression* struct_expr, const std::string& name,
2007 source_location, Saw_named_type*,
2008 unsigned int* depth) const;
2010 // The fields of the struct.
2011 Struct_field_list* fields_;
2012 // The place where the struct was declared.
2013 source_location location_;
2014 // If this struct is unnamed, a list of methods.
2015 Methods* all_methods_;
2018 // The type of an array.
2020 class Array_type : public Type
2023 Array_type(Type* element_type, Expression* length)
2025 element_type_(element_type), length_(length), length_tree_(NULL)
2028 // Return the element type.
2030 element_type() const
2031 { return this->element_type_; }
2033 // Return the length. This will return NULL for an open array.
2036 { return this->length_; }
2038 // Whether this type is identical with T.
2040 is_identical(const Array_type* t, bool errors_are_identical) const;
2042 // Whether this type has any hidden fields.
2044 array_has_hidden_fields(const Named_type* within, std::string* reason) const
2045 { return this->element_type_->has_hidden_fields(within, reason); }
2047 // Return a tree for the pointer to the values in an array.
2049 value_pointer_tree(Gogo*, tree array) const;
2051 // Return a tree for the length of an array with this type.
2053 length_tree(Gogo*, tree array);
2055 // Return a tree for the capacity of an array with this type.
2057 capacity_tree(Gogo*, tree array);
2059 // Import an array type.
2063 // Return the backend representation of the element type.
2065 get_backend_element(Gogo*);
2067 // Return the backend representation of the length.
2069 get_backend_length(Gogo*);
2072 make_array_type_descriptor_type();
2075 make_slice_type_descriptor_type();
2079 do_traverse(Traverse* traverse);
2085 do_has_pointer() const
2087 return this->length_ == NULL || this->element_type_->has_pointer();
2091 do_hash_for_method(Gogo*) const;
2094 do_check_make_expression(Expression_list*, source_location);
2100 do_get_init_tree(Gogo*, tree, bool);
2103 do_make_expression_tree(Translate_context*, Expression_list*,
2107 do_type_descriptor(Gogo*, Named_type*);
2110 do_reflection(Gogo*, std::string*) const;
2113 do_mangled_name(Gogo*, std::string*) const;
2116 do_export(Export*) const;
2123 get_length_tree(Gogo*);
2126 array_type_descriptor(Gogo*, Named_type*);
2129 slice_type_descriptor(Gogo*, Named_type*);
2131 // The type of elements of the array.
2132 Type* element_type_;
2133 // The number of elements. This may be NULL.
2134 Expression* length_;
2135 // The length as a tree. We only want to compute this once.
2139 // The type of a map.
2141 class Map_type : public Type
2144 Map_type(Type* key_type, Type* val_type, source_location location)
2146 key_type_(key_type), val_type_(val_type), location_(location)
2149 // Return the key type.
2152 { return this->key_type_; }
2154 // Return the value type.
2157 { return this->val_type_; }
2159 // Whether this type is identical with T.
2161 is_identical(const Map_type* t, bool errors_are_identical) const;
2163 // Import a map type.
2168 make_map_type_descriptor_type();
2172 do_traverse(Traverse*);
2178 do_has_pointer() const
2182 do_hash_for_method(Gogo*) const;
2185 do_check_make_expression(Expression_list*, source_location);
2191 do_get_init_tree(Gogo*, tree, bool);
2194 do_make_expression_tree(Translate_context*, Expression_list*,
2198 do_type_descriptor(Gogo*, Named_type*);
2201 do_reflection(Gogo*, std::string*) const;
2204 do_mangled_name(Gogo*, std::string*) const;
2207 do_export(Export*) const;
2214 // Where the type was defined.
2215 source_location location_;
2218 // The type of a channel.
2220 class Channel_type : public Type
2223 Channel_type(bool may_send, bool may_receive, Type* element_type)
2224 : Type(TYPE_CHANNEL),
2225 may_send_(may_send), may_receive_(may_receive),
2226 element_type_(element_type)
2227 { go_assert(may_send || may_receive); }
2229 // Whether this channel can send data.
2232 { return this->may_send_; }
2234 // Whether this channel can receive data.
2237 { return this->may_receive_; }
2239 // The type of the values that may be sent on this channel. This is
2240 // NULL if any type may be sent.
2242 element_type() const
2243 { return this->element_type_; }
2245 // Whether this type is identical with T.
2247 is_identical(const Channel_type* t, bool errors_are_identical) const;
2249 // Import a channel type.
2250 static Channel_type*
2254 make_chan_type_descriptor_type();
2258 do_traverse(Traverse* traverse)
2259 { return Type::traverse(this->element_type_, traverse); }
2262 do_has_pointer() const
2266 do_hash_for_method(Gogo*) const;
2269 do_check_make_expression(Expression_list*, source_location);
2275 do_get_init_tree(Gogo*, tree, bool);
2278 do_make_expression_tree(Translate_context*, Expression_list*,
2282 do_type_descriptor(Gogo*, Named_type*);
2285 do_reflection(Gogo*, std::string*) const;
2288 do_mangled_name(Gogo*, std::string*) const;
2291 do_export(Export*) const;
2294 // Whether this channel can send data.
2296 // Whether this channel can receive data.
2298 // The types of elements which may be sent on this channel. If this
2299 // is NULL, it means that any type may be sent.
2300 Type* element_type_;
2303 // An interface type.
2305 class Interface_type : public Type
2308 Interface_type(Typed_identifier_list* methods, source_location location)
2309 : Type(TYPE_INTERFACE),
2310 methods_(methods), location_(location)
2311 { go_assert(methods == NULL || !methods->empty()); }
2313 // The location where the interface type was defined.
2316 { return this->location_; }
2318 // Return whether this is an empty interface.
2321 { return this->methods_ == NULL; }
2323 // Return the list of methods. This will return NULL for an empty
2325 const Typed_identifier_list*
2327 { return this->methods_; }
2329 // Return the number of methods.
2331 method_count() const
2332 { return this->methods_ == NULL ? 0 : this->methods_->size(); }
2334 // Return the method NAME, or NULL.
2335 const Typed_identifier*
2336 find_method(const std::string& name) const;
2338 // Return the zero-based index of method NAME.
2340 method_index(const std::string& name) const;
2342 // Finalize the methods. This handles interface inheritance.
2346 // Return true if T implements this interface. If this returns
2347 // false, and REASON is not NULL, it sets *REASON to the reason that
2350 implements_interface(const Type* t, std::string* reason) const;
2352 // Whether this type is identical with T. REASON is as in
2353 // implements_interface.
2355 is_identical(const Interface_type* t, bool errors_are_identical) const;
2357 // Whether we can assign T to this type. is_identical is known to
2360 is_compatible_for_assign(const Interface_type*, std::string* reason) const;
2362 // Return whether NAME is a method which is not exported. This is
2363 // only used for better error reporting.
2365 is_unexported_method(Gogo*, const std::string& name) const;
2367 // Import an interface type.
2368 static Interface_type*
2371 // Make a struct for an empty interface type.
2373 get_backend_empty_interface_type(Gogo*);
2376 make_interface_type_descriptor_type();
2380 do_traverse(Traverse*);
2383 do_has_pointer() const
2387 do_hash_for_method(Gogo*) const;
2393 do_get_init_tree(Gogo* gogo, tree, bool);
2396 do_type_descriptor(Gogo*, Named_type*);
2399 do_reflection(Gogo*, std::string*) const;
2402 do_mangled_name(Gogo*, std::string*) const;
2405 do_export(Export*) const;
2408 // The list of methods associated with the interface. This will be
2409 // NULL for the empty interface.
2410 Typed_identifier_list* methods_;
2411 // The location where the interface was defined.
2412 source_location location_;
2415 // The value we keep for a named type. This lets us get the right
2416 // name when we convert to trees. Note that we don't actually keep
2417 // the name here; the name is in the Named_object which points to
2418 // this. This object exists to hold a unique tree which represents
2421 class Named_type : public Type
2424 Named_type(Named_object* named_object, Type* type, source_location location)
2426 named_object_(named_object), in_function_(NULL), type_(type),
2427 local_methods_(NULL), all_methods_(NULL),
2428 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
2429 location_(location), named_btype_(NULL), dependencies_(),
2430 is_visible_(true), is_error_(false), is_converted_(false),
2431 is_circular_(false), seen_(0)
2434 // Return the associated Named_object. This holds the actual name.
2437 { return this->named_object_; }
2440 named_object() const
2441 { return this->named_object_; }
2443 // Set the Named_object. This is used when we see a type
2444 // declaration followed by a type.
2446 set_named_object(Named_object* no)
2447 { this->named_object_ = no; }
2449 // Return the function in which this type is defined. This will
2450 // return NULL for a type defined in global scope.
2453 { return this->in_function_; }
2455 // Set the function in which this type is defined.
2457 set_in_function(Named_object* f)
2458 { this->in_function_ = f; }
2460 // Return the name of the type.
2464 // Return the name of the type for an error message. The difference
2465 // is that if the type is defined in a different package, this will
2466 // return PACKAGE.NAME.
2468 message_name() const;
2470 // Return the underlying type.
2473 { return this->type_; }
2477 { return this->type_; }
2479 // Return the location.
2482 { return this->location_; }
2484 // Whether this type is visible. This only matters when parsing.
2487 { return this->is_visible_; }
2489 // Mark this type as visible.
2492 { this->is_visible_ = true; }
2494 // Mark this type as invisible.
2497 { this->is_visible_ = false; }
2499 // Whether this is a builtin type.
2502 { return this->location_ == BUILTINS_LOCATION; }
2504 // Whether this is a circular type: a pointer or function type that
2505 // refers to itself, which is not possible in C.
2508 { return this->is_circular_; }
2510 // Return the base type for this type.
2517 // Return whether this is an error type.
2519 is_named_error_type() const;
2521 // Add a method to this type.
2523 add_method(const std::string& name, Function*);
2525 // Add a method declaration to this type.
2527 add_method_declaration(const std::string& name, Package* package,
2528 Function_type* type, source_location location);
2530 // Add an existing method--one defined before the type itself was
2531 // defined--to a type.
2533 add_existing_method(Named_object*);
2535 // Look up a local method.
2537 find_local_method(const std::string& name) const;
2539 // Return the list of local methods.
2541 local_methods() const
2542 { return this->local_methods_; }
2544 // Build the complete list of methods, including those from
2545 // anonymous fields, and build method stubs if needed.
2547 finalize_methods(Gogo*);
2549 // Return whether this type has any methods. This should only be
2550 // called after the finalize_methods pass.
2552 has_any_methods() const
2553 { return this->all_methods_ != NULL; }
2555 // Return the methods for this type. This should only be called
2556 // after the finalized_methods pass.
2559 { return this->all_methods_; }
2561 // Return the method to use for NAME. This returns NULL if there is
2562 // no such method or if the method is ambiguous. When it returns
2563 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2565 method_function(const std::string& name, bool *is_ambiguous) const;
2567 // Return whether NAME is a known field or method which is not
2568 // exported. This is only used for better error reporting.
2570 is_unexported_local_method(Gogo*, const std::string& name) const;
2572 // Return a pointer to the interface method table for this type for
2573 // the interface INTERFACE. If IS_POINTER is true, set the type
2574 // descriptor to a pointer to this type, otherwise set it to this
2577 interface_method_table(Gogo*, const Interface_type* interface,
2580 // Whether this type has any hidden fields.
2582 named_type_has_hidden_fields(std::string* reason) const;
2584 // Note that a type must be converted to the backend representation
2585 // before we convert this type.
2587 add_dependency(Named_type* nt)
2588 { this->dependencies_.push_back(nt); }
2592 export_named_type(Export*, const std::string& name) const;
2594 // Import a named type.
2596 import_named_type(Import*, Named_type**);
2598 // Initial conversion to backend representation.
2604 do_traverse(Traverse* traverse)
2605 { return Type::traverse(this->type_, traverse); }
2611 do_has_pointer() const;
2614 do_hash_for_method(Gogo*) const;
2617 do_check_make_expression(Expression_list* args, source_location location)
2618 { return this->type_->check_make_expression(args, location); }
2624 do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
2625 { return this->type_->get_typed_init_tree(gogo, type_tree, is_clear); }
2628 do_make_expression_tree(Translate_context* context, Expression_list* args,
2629 source_location location)
2630 { return this->type_->make_expression_tree(context, args, location); }
2633 do_type_descriptor(Gogo*, Named_type*);
2636 do_reflection(Gogo*, std::string*) const;
2639 do_mangled_name(Gogo*, std::string* ret) const;
2642 do_export(Export*) const;
2645 // Create the placeholder during conversion.
2647 create_placeholder(Gogo*);
2649 // A mapping from interfaces to the associated interface method
2650 // tables for this type. This maps to a decl.
2651 typedef Unordered_map_hash(const Interface_type*, tree, Type_hash_identical,
2652 Type_identical) Interface_method_tables;
2654 // A pointer back to the Named_object for this type.
2655 Named_object* named_object_;
2656 // If this type is defined in a function, a pointer back to the
2657 // function in which it is defined.
2658 Named_object* in_function_;
2661 // The list of methods defined for this type. Any named type can
2663 Bindings* local_methods_;
2664 // The full list of methods for this type, including methods
2665 // declared for anonymous fields.
2666 Methods* all_methods_;
2667 // A mapping from interfaces to the associated interface method
2668 // tables for this type.
2669 Interface_method_tables* interface_method_tables_;
2670 // A mapping from interfaces to the associated interface method
2671 // tables for pointers to this type.
2672 Interface_method_tables* pointer_interface_method_tables_;
2673 // The location where this type was defined.
2674 source_location location_;
2675 // The backend representation of this type during backend
2676 // conversion. This is used to avoid endless recursion when a named
2677 // type refers to itself.
2678 Btype* named_btype_;
2679 // A list of types which must be converted to the backend
2680 // representation before this type can be converted. This is for
2682 // type S1 { p *S2 }
2684 // where we can't convert S2 to the backend representation unless we
2685 // have converted S1.
2686 std::vector<Named_type*> dependencies_;
2687 // Whether this type is visible. This is false if this type was
2688 // created because it was referenced by an imported object, but the
2689 // type itself was not exported. This will always be true for types
2690 // created in the current package.
2692 // Whether this type is erroneous.
2694 // Whether this type has been converted to the backend
2697 // Whether this is a pointer or function type which refers to the
2700 // In a recursive operation such as has_hidden_fields, this flag is
2701 // used to prevent infinite recursion when a type refers to itself.
2702 // This is mutable because it is always reset to false when the
2707 // A forward declaration. This handles a type which has been declared
2710 class Forward_declaration_type : public Type
2713 Forward_declaration_type(Named_object* named_object);
2715 // The named object associated with this type declaration. This
2716 // will be resolved.
2721 named_object() const;
2723 // Return the name of the type.
2727 // Return the type to which this points. Give an error if the type
2728 // has not yet been defined.
2735 // Whether the base type has been defined.
2739 // Add a method to this type.
2741 add_method(const std::string& name, Function*);
2743 // Add a method declaration to this type.
2745 add_method_declaration(const std::string& name, Function_type*,
2750 do_traverse(Traverse* traverse);
2753 do_has_pointer() const
2754 { return this->real_type()->has_pointer(); }
2757 do_hash_for_method(Gogo* gogo) const
2758 { return this->real_type()->hash_for_method(gogo); }
2761 do_check_make_expression(Expression_list* args, source_location location)
2762 { return this->base()->check_make_expression(args, location); }
2765 do_get_tree(Gogo* gogo);
2768 do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
2769 { return this->base()->get_typed_init_tree(gogo, type_tree, is_clear); }
2772 do_make_expression_tree(Translate_context* context, Expression_list* args,
2773 source_location location)
2774 { return this->base()->make_expression_tree(context, args, location); }
2777 do_type_descriptor(Gogo*, Named_type*);
2780 do_reflection(Gogo*, std::string*) const;
2783 do_mangled_name(Gogo*, std::string* ret) const;
2786 do_export(Export*) const;
2789 // Issue a warning about a use of an undefined type.
2793 // The type declaration.
2794 Named_object* named_object_;
2795 // Whether we have issued a warning about this type.
2796 mutable bool warned_;
2799 // The Type_context struct describes what we expect for the type of an
2804 // The exact type we expect, if known. This may be NULL.
2806 // Whether an abstract type is permitted.
2807 bool may_be_abstract;
2811 : type(NULL), may_be_abstract(false)
2814 Type_context(Type* a_type, bool a_may_be_abstract)
2815 : type(a_type), may_be_abstract(a_may_be_abstract)
2819 #endif // !defined(GO_TYPES_H)