Merge from branches/gcc-4_8-branch up to rev 204657.
[official-gcc.git] / gcc-4_8-branch / gcc / go / gofrontend / types.h
blobed01d9cdc38942d313f742c7e3d69ada63d1485d
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.
7 #ifndef GO_TYPES_H
8 #define GO_TYPES_H
10 #include "go-linemap.h"
12 class Gogo;
13 class Package;
14 class Traverse;
15 class Typed_identifier;
16 class Typed_identifier_list;
17 class Integer_type;
18 class Float_type;
19 class Complex_type;
20 class String_type;
21 class Function_type;
22 class Struct_field;
23 class Struct_field_list;
24 class Struct_type;
25 class Pointer_type;
26 class Array_type;
27 class Map_type;
28 class Channel_type;
29 class Interface_type;
30 class Named_type;
31 class Forward_declaration_type;
32 class Method;
33 class Methods;
34 class Type_hash_identical;
35 class Type_identical;
36 class Expression;
37 class Expression_list;
38 class Call_expression;
39 class Field_reference_expression;
40 class Bound_method_expression;
41 class Bindings;
42 class Named_object;
43 class Function;
44 class Translate_context;
45 class Export;
46 class Import;
47 class Btype;
48 class Bexpression;
49 class Bvariable;
51 // Type codes used in type descriptors. These must match the values
52 // in libgo/runtime/go-type.h. They also match the values in the gc
53 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
54 // although this is not required.
56 static const int RUNTIME_TYPE_KIND_BOOL = 1;
57 static const int RUNTIME_TYPE_KIND_INT = 2;
58 static const int RUNTIME_TYPE_KIND_INT8 = 3;
59 static const int RUNTIME_TYPE_KIND_INT16 = 4;
60 static const int RUNTIME_TYPE_KIND_INT32 = 5;
61 static const int RUNTIME_TYPE_KIND_INT64 = 6;
62 static const int RUNTIME_TYPE_KIND_UINT = 7;
63 static const int RUNTIME_TYPE_KIND_UINT8 = 8;
64 static const int RUNTIME_TYPE_KIND_UINT16 = 9;
65 static const int RUNTIME_TYPE_KIND_UINT32 = 10;
66 static const int RUNTIME_TYPE_KIND_UINT64 = 11;
67 static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
68 static const int RUNTIME_TYPE_KIND_FLOAT32 = 13;
69 static const int RUNTIME_TYPE_KIND_FLOAT64 = 14;
70 static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15;
71 static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16;
72 static const int RUNTIME_TYPE_KIND_ARRAY = 17;
73 static const int RUNTIME_TYPE_KIND_CHAN = 18;
74 static const int RUNTIME_TYPE_KIND_FUNC = 19;
75 static const int RUNTIME_TYPE_KIND_INTERFACE = 20;
76 static const int RUNTIME_TYPE_KIND_MAP = 21;
77 static const int RUNTIME_TYPE_KIND_PTR = 22;
78 static const int RUNTIME_TYPE_KIND_SLICE = 23;
79 static const int RUNTIME_TYPE_KIND_STRING = 24;
80 static const int RUNTIME_TYPE_KIND_STRUCT = 25;
81 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26;
83 static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7);
85 // To build the complete list of methods for a named type we need to
86 // gather all methods from anonymous fields. Those methods may
87 // require an arbitrary set of indirections and field offsets. There
88 // is also the possibility of ambiguous methods, which we could ignore
89 // except that we want to give a better error message for that case.
90 // This is a base class. There are two types of methods: named
91 // methods, and methods which are inherited from an anonymous field of
92 // interface type.
94 class Method
96 public:
97 // For methods in anonymous types we need to know the sequence of
98 // field references used to extract the pointer to pass to the
99 // method. Since each method for a particular anonymous field will
100 // have the sequence of field indexes, and since the indexes can be
101 // shared going down the chain, we use a manually managed linked
102 // list. The first entry in the list is the field index for the
103 // last field, the one passed to the method.
105 struct Field_indexes
107 const Field_indexes* next;
108 unsigned int field_index;
111 virtual ~Method()
114 // Get the list of field indexes.
115 const Field_indexes*
116 field_indexes() const
117 { return this->field_indexes_; }
119 // Get the depth.
120 unsigned int
121 depth() const
122 { return this->depth_; }
124 // Return whether this is a value method--a method which does not
125 // require a pointer expression.
126 bool
127 is_value_method() const
128 { return this->is_value_method_; }
130 // Return whether we need a stub method--this is true if we can't
131 // just pass the main object to the method.
132 bool
133 needs_stub_method() const
134 { return this->needs_stub_method_; }
136 // Return whether this is an ambiguous method name.
137 bool
138 is_ambiguous() const
139 { return this->is_ambiguous_; }
141 // Note that this method is ambiguous.
142 void
143 set_is_ambiguous()
144 { this->is_ambiguous_ = true; }
146 // Return the type of the method.
147 Function_type*
148 type() const
149 { return this->do_type(); }
151 // Return the location of the method receiver.
152 Location
153 receiver_location() const
154 { return this->do_receiver_location(); }
156 // Return an expression which binds this method to EXPR. This is
157 // something which can be used with a function call.
158 Expression*
159 bind_method(Expression* expr, Location location) const;
161 // Return the named object for this method. This may only be called
162 // after methods are finalized.
163 Named_object*
164 named_object() const;
166 // Get the stub object.
167 Named_object*
168 stub_object() const
170 go_assert(this->stub_ != NULL);
171 return this->stub_;
174 // Set the stub object.
175 void
176 set_stub_object(Named_object* no)
178 go_assert(this->stub_ == NULL);
179 this->stub_ = no;
182 // Return true if this method should not participate in any
183 // interfaces.
184 bool
185 nointerface() const
186 { return this->do_nointerface(); }
188 protected:
189 // These objects are only built by the child classes.
190 Method(const Field_indexes* field_indexes, unsigned int depth,
191 bool is_value_method, bool needs_stub_method)
192 : field_indexes_(field_indexes), depth_(depth), stub_(NULL),
193 is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
194 is_ambiguous_(false)
197 // The named object for this method.
198 virtual Named_object*
199 do_named_object() const = 0;
201 // The type of the method.
202 virtual Function_type*
203 do_type() const = 0;
205 // Return the location of the method receiver.
206 virtual Location
207 do_receiver_location() const = 0;
209 // Bind a method to an object.
210 virtual Expression*
211 do_bind_method(Expression* expr, Location location) const = 0;
213 // Return whether this method should not participate in interfaces.
214 virtual bool
215 do_nointerface() const = 0;
217 private:
218 // The sequence of field indexes used for this method. If this is
219 // NULL, then the method is defined for the current type.
220 const Field_indexes* field_indexes_;
221 // The depth at which this method was found.
222 unsigned int depth_;
223 // If a stub method is required, this is its object. This is only
224 // set after stub methods are built in finalize_methods.
225 Named_object* stub_;
226 // Whether this is a value method--a method that does not require a
227 // pointer.
228 bool is_value_method_;
229 // Whether a stub method is required.
230 bool needs_stub_method_;
231 // Whether this method is ambiguous.
232 bool is_ambiguous_;
235 // A named method. This is what you get with a method declaration,
236 // either directly on the type, or inherited from some anonymous
237 // embedded field.
239 class Named_method : public Method
241 public:
242 Named_method(Named_object* named_object, const Field_indexes* field_indexes,
243 unsigned int depth, bool is_value_method,
244 bool needs_stub_method)
245 : Method(field_indexes, depth, is_value_method, needs_stub_method),
246 named_object_(named_object)
249 protected:
250 // Get the Named_object for the method.
251 Named_object*
252 do_named_object() const
253 { return this->named_object_; }
255 // The type of the method.
256 Function_type*
257 do_type() const;
259 // Return the location of the method receiver.
260 Location
261 do_receiver_location() const;
263 // Bind a method to an object.
264 Expression*
265 do_bind_method(Expression* expr, Location location) const;
267 // Return whether this method should not participate in interfaces.
268 bool
269 do_nointerface() const;
271 private:
272 // The method itself. For a method which needs a stub, this starts
273 // out as the underlying method, and is later replaced with the stub
274 // method.
275 Named_object* named_object_;
278 // An interface method. This is used when an interface appears as an
279 // anonymous field in a named struct.
281 class Interface_method : public Method
283 public:
284 Interface_method(const std::string& name, Location location,
285 Function_type* fntype, const Field_indexes* field_indexes,
286 unsigned int depth)
287 : Method(field_indexes, depth, true, true),
288 name_(name), location_(location), fntype_(fntype)
291 protected:
292 // Get the Named_object for the method. This should never be
293 // called, as we always create a stub.
294 Named_object*
295 do_named_object() const
296 { go_unreachable(); }
298 // The type of the method.
299 Function_type*
300 do_type() const
301 { return this->fntype_; }
303 // Return the location of the method receiver.
304 Location
305 do_receiver_location() const
306 { return this->location_; }
308 // Bind a method to an object.
309 Expression*
310 do_bind_method(Expression* expr, Location location) const;
312 // Return whether this method should not participate in interfaces.
313 bool
314 do_nointerface() const
315 { return false; }
317 private:
318 // The name of the interface method to call.
319 std::string name_;
320 // The location of the definition of the interface method.
321 Location location_;
322 // The type of the interface method.
323 Function_type* fntype_;
326 // A mapping from method name to Method. This is a wrapper around a
327 // hash table.
329 class Methods
331 private:
332 typedef Unordered_map(std::string, Method*) Method_map;
334 public:
335 typedef Method_map::const_iterator const_iterator;
337 Methods()
338 : methods_()
341 // Insert a new method. Returns true if it was inserted, false if
342 // it was overidden or ambiguous.
343 bool
344 insert(const std::string& name, Method* m);
346 // The number of (unambiguous) methods.
347 size_t
348 count() const;
350 // Iterate.
351 const_iterator
352 begin() const
353 { return this->methods_.begin(); }
355 const_iterator
356 end() const
357 { return this->methods_.end(); }
359 // Lookup.
360 const_iterator
361 find(const std::string& name) const
362 { return this->methods_.find(name); }
364 private:
365 Method_map methods_;
368 // The base class for all types.
370 class Type
372 public:
373 // The types of types.
374 enum Type_classification
376 TYPE_ERROR,
377 TYPE_VOID,
378 TYPE_BOOLEAN,
379 TYPE_INTEGER,
380 TYPE_FLOAT,
381 TYPE_COMPLEX,
382 TYPE_STRING,
383 TYPE_SINK,
384 TYPE_FUNCTION,
385 TYPE_POINTER,
386 TYPE_NIL,
387 TYPE_CALL_MULTIPLE_RESULT,
388 TYPE_STRUCT,
389 TYPE_ARRAY,
390 TYPE_MAP,
391 TYPE_CHANNEL,
392 TYPE_INTERFACE,
393 TYPE_NAMED,
394 TYPE_FORWARD
397 virtual ~Type();
399 // Creators.
401 static Type*
402 make_error_type();
404 static Type*
405 make_void_type();
407 // Get the unnamed bool type.
408 static Type*
409 make_boolean_type();
411 // Get the named type "bool".
412 static Named_type*
413 lookup_bool_type();
415 // Make the named type "bool".
416 static Named_type*
417 make_named_bool_type();
419 // Make an abstract integer type.
420 static Integer_type*
421 make_abstract_integer_type();
423 // Make an abstract type for a character constant.
424 static Integer_type*
425 make_abstract_character_type();
427 // Make a named integer type with a specified size.
428 // RUNTIME_TYPE_KIND is the code to use in reflection information,
429 // to distinguish int and int32.
430 static Named_type*
431 make_integer_type(const char* name, bool is_unsigned, int bits,
432 int runtime_type_kind);
434 // Look up a named integer type.
435 static Named_type*
436 lookup_integer_type(const char* name);
438 // Make an abstract floating point type.
439 static Float_type*
440 make_abstract_float_type();
442 // Make a named floating point type with a specific size.
443 // RUNTIME_TYPE_KIND is the code to use in reflection information,
444 // to distinguish float and float32.
445 static Named_type*
446 make_float_type(const char* name, int bits, int runtime_type_kind);
448 // Look up a named float type.
449 static Named_type*
450 lookup_float_type(const char* name);
452 // Make an abstract complex type.
453 static Complex_type*
454 make_abstract_complex_type();
456 // Make a named complex type with a specific size.
457 // RUNTIME_TYPE_KIND is the code to use in reflection information,
458 // to distinguish complex and complex64.
459 static Named_type*
460 make_complex_type(const char* name, int bits, int runtime_type_kind);
462 // Look up a named complex type.
463 static Named_type*
464 lookup_complex_type(const char* name);
466 // Get the unnamed string type.
467 static Type*
468 make_string_type();
470 // Get the named type "string".
471 static Named_type*
472 lookup_string_type();
474 // Make the named type "string".
475 static Named_type*
476 make_named_string_type();
478 static Type*
479 make_sink_type();
481 static Function_type*
482 make_function_type(Typed_identifier* receiver,
483 Typed_identifier_list* parameters,
484 Typed_identifier_list* results,
485 Location);
487 static Pointer_type*
488 make_pointer_type(Type*);
490 static Type*
491 make_nil_type();
493 static Type*
494 make_call_multiple_result_type(Call_expression*);
496 static Struct_type*
497 make_struct_type(Struct_field_list* fields, Location);
499 static Array_type*
500 make_array_type(Type* element_type, Expression* length);
502 static Map_type*
503 make_map_type(Type* key_type, Type* value_type, Location);
505 static Channel_type*
506 make_channel_type(bool send, bool receive, Type*);
508 static Interface_type*
509 make_interface_type(Typed_identifier_list* methods, Location);
511 static Interface_type*
512 make_empty_interface_type(Location);
514 static Type*
515 make_type_descriptor_type();
517 static Type*
518 make_type_descriptor_ptr_type();
520 static Named_type*
521 make_named_type(Named_object*, Type*, Location);
523 static Type*
524 make_forward_declaration(Named_object*);
526 // Make a builtin struct type from a list of fields.
527 static Struct_type*
528 make_builtin_struct_type(int nfields, ...);
530 // Make a builtin named type.
531 static Named_type*
532 make_builtin_named_type(const char* name, Type* type);
534 // Traverse a type.
535 static int
536 traverse(Type*, Traverse*);
538 // Verify the type. This is called after parsing, and verifies that
539 // types are complete and meet the language requirements. This
540 // returns false if the type is invalid and we should not continue
541 // traversing it.
542 bool
543 verify()
544 { return this->do_verify(); }
546 // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
547 // returns that an erroneous type is identical to any other type;
548 // this is used to avoid cascading errors. If this returns false,
549 // and REASON is not NULL, it may set *REASON.
550 static bool
551 are_identical(const Type* lhs, const Type* rhs, bool errors_are_identical,
552 std::string* reason);
554 // Return true if two types are compatible for use in a binary
555 // operation, other than a shift, comparison, or channel send. This
556 // is an equivalence relation.
557 static bool
558 are_compatible_for_binop(const Type* t1, const Type* t2);
560 // Return true if two types are compatible for use with the
561 // comparison operator. IS_EQUALITY_OP is true if this is an
562 // equality comparison, false if it is an ordered comparison. This
563 // is an equivalence relation. If this returns false, and REASON is
564 // not NULL, it sets *REASON.
565 static bool
566 are_compatible_for_comparison(bool is_equality_op, const Type *t1,
567 const Type *t2, std::string* reason);
569 // Return true if a type is comparable with itself. This is true of
570 // most types, but false for, e.g., function types.
571 bool
572 is_comparable() const
573 { return Type::are_compatible_for_comparison(true, this, this, NULL); }
575 // Return true if a value with type RHS is assignable to a variable
576 // with type LHS. This is not an equivalence relation. If this
577 // returns false, and REASON is not NULL, it sets *REASON.
578 static bool
579 are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
581 // Return true if a value with type RHS is assignable to a variable
582 // with type LHS, ignoring any assignment of hidden fields
583 // (unexported fields of a type imported from another package).
584 // This is like the are_assignable method.
585 static bool
586 are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
587 std::string* reason);
589 // Return true if a value with type RHS may be converted to type
590 // LHS. If this returns false, and REASON is not NULL, it sets
591 // *REASON.
592 static bool
593 are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
595 // Whether this type has any hidden fields which are not visible in
596 // the current compilation, such as a field whose name begins with a
597 // lower case letter in a struct imported from a different package.
598 // WITHIN is not NULL if we are looking at fields in a named type.
599 bool
600 has_hidden_fields(const Named_type* within, std::string* reason) const;
602 // Return true if values of this type can be compared using an
603 // identity function which gets nothing but a pointer to the value
604 // and a size.
605 bool
606 compare_is_identity(Gogo* gogo)
607 { return this->do_compare_is_identity(gogo); }
609 // Return a hash code for this type for the method hash table.
610 // Types which are equivalent according to are_identical will have
611 // the same hash code.
612 unsigned int
613 hash_for_method(Gogo*) const;
615 // Return the type classification.
616 Type_classification
617 classification() const
618 { return this->classification_; }
620 // Return the base type for this type. This looks through forward
621 // declarations and names. Using this with a forward declaration
622 // which has not been defined will return an error type.
623 Type*
624 base();
626 const Type*
627 base() const;
629 // Return the type skipping defined forward declarations. If this
630 // type is a forward declaration which has not been defined, it will
631 // return the Forward_declaration_type. This differs from base() in
632 // that it will return a Named_type, and for a
633 // Forward_declaration_type which is not defined it will return that
634 // type rather than an error type.
635 Type*
636 forwarded();
638 const Type*
639 forwarded() const;
641 // Return true if this is a basic type: a type which is not composed
642 // of other types, and is not void.
643 bool
644 is_basic_type() const;
646 // Return true if this is an abstract type--an integer, floating
647 // point, or complex type whose size has not been determined.
648 bool
649 is_abstract() const;
651 // Return a non-abstract version of an abstract type.
652 Type*
653 make_non_abstract_type();
655 // Return true if this type is or contains a pointer. This
656 // determines whether the garbage collector needs to look at a value
657 // of this type.
658 bool
659 has_pointer() const
660 { return this->do_has_pointer(); }
662 // Return true if this is the error type. This returns false for a
663 // type which is not defined, as it is called by the parser before
664 // all types are defined.
665 bool
666 is_error_type() const;
668 // Return true if this is the error type or if the type is
669 // undefined. If the type is undefined, this will give an error.
670 // This should only be called after parsing is complete.
671 bool
672 is_error() const
673 { return this->base()->is_error_type(); }
675 // Return true if this is a void type.
676 bool
677 is_void_type() const
678 { return this->classification_ == TYPE_VOID; }
680 // If this is an integer type, return the Integer_type. Otherwise,
681 // return NULL. This is a controlled dynamic_cast.
682 Integer_type*
683 integer_type()
684 { return this->convert<Integer_type, TYPE_INTEGER>(); }
686 const Integer_type*
687 integer_type() const
688 { return this->convert<const Integer_type, TYPE_INTEGER>(); }
690 // If this is a floating point type, return the Float_type.
691 // Otherwise, return NULL. This is a controlled dynamic_cast.
692 Float_type*
693 float_type()
694 { return this->convert<Float_type, TYPE_FLOAT>(); }
696 const Float_type*
697 float_type() const
698 { return this->convert<const Float_type, TYPE_FLOAT>(); }
700 // If this is a complex type, return the Complex_type. Otherwise,
701 // return NULL.
702 Complex_type*
703 complex_type()
704 { return this->convert<Complex_type, TYPE_COMPLEX>(); }
706 const Complex_type*
707 complex_type() const
708 { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
710 // Return whether this is a numeric type.
711 bool
712 is_numeric_type() const
714 Type_classification tc = this->base()->classification_;
715 return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX;
718 // Return true if this is a boolean type.
719 bool
720 is_boolean_type() const
721 { return this->base()->classification_ == TYPE_BOOLEAN; }
723 // Return true if this is an abstract boolean type.
724 bool
725 is_abstract_boolean_type() const
726 { return this->classification_ == TYPE_BOOLEAN; }
728 // Return true if this is a string type.
729 bool
730 is_string_type() const
731 { return this->base()->classification_ == TYPE_STRING; }
733 // Return true if this is an abstract string type.
734 bool
735 is_abstract_string_type() const
736 { return this->classification_ == TYPE_STRING; }
738 // Return true if this is the sink type. This is the type of the
739 // blank identifier _.
740 bool
741 is_sink_type() const
742 { return this->base()->classification_ == TYPE_SINK; }
744 // If this is a function type, return it. Otherwise, return NULL.
745 Function_type*
746 function_type()
747 { return this->convert<Function_type, TYPE_FUNCTION>(); }
749 const Function_type*
750 function_type() const
751 { return this->convert<const Function_type, TYPE_FUNCTION>(); }
753 // If this is a pointer type, return the type to which it points.
754 // Otherwise, return NULL.
755 Type*
756 points_to() const;
758 // If this is a pointer type, return the type to which it points.
759 // Otherwise, return the type itself.
760 Type*
761 deref()
763 Type* pt = this->points_to();
764 return pt != NULL ? pt : this;
767 const Type*
768 deref() const
770 const Type* pt = this->points_to();
771 return pt != NULL ? pt : this;
774 // Return true if this is the nil type. We don't use base() here,
775 // because this can be called during parse, and there is no way to
776 // name the nil type anyhow.
777 bool
778 is_nil_type() const
779 { return this->classification_ == TYPE_NIL; }
781 // Return true if this is the predeclared constant nil being used as
782 // a type. This is what the parser produces for type switches which
783 // use "case nil".
784 bool
785 is_nil_constant_as_type() const;
787 // Return true if this is the return type of a function which
788 // returns multiple values.
789 bool
790 is_call_multiple_result_type() const
791 { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
793 // If this is a struct type, return it. Otherwise, return NULL.
794 Struct_type*
795 struct_type()
796 { return this->convert<Struct_type, TYPE_STRUCT>(); }
798 const Struct_type*
799 struct_type() const
800 { return this->convert<const Struct_type, TYPE_STRUCT>(); }
802 // If this is an array type, return it. Otherwise, return NULL.
803 Array_type*
804 array_type()
805 { return this->convert<Array_type, TYPE_ARRAY>(); }
807 const Array_type*
808 array_type() const
809 { return this->convert<const Array_type, TYPE_ARRAY>(); }
811 // Return whether if this is a slice type.
812 bool
813 is_slice_type() const;
815 // If this is a map type, return it. Otherwise, return NULL.
816 Map_type*
817 map_type()
818 { return this->convert<Map_type, TYPE_MAP>(); }
820 const Map_type*
821 map_type() const
822 { return this->convert<const Map_type, TYPE_MAP>(); }
824 // If this is a channel type, return it. Otherwise, return NULL.
825 Channel_type*
826 channel_type()
827 { return this->convert<Channel_type, TYPE_CHANNEL>(); }
829 const Channel_type*
830 channel_type() const
831 { return this->convert<const Channel_type, TYPE_CHANNEL>(); }
833 // If this is an interface type, return it. Otherwise, return NULL.
834 Interface_type*
835 interface_type()
836 { return this->convert<Interface_type, TYPE_INTERFACE>(); }
838 const Interface_type*
839 interface_type() const
840 { return this->convert<const Interface_type, TYPE_INTERFACE>(); }
842 // If this is a named type, return it. Otherwise, return NULL.
843 Named_type*
844 named_type();
846 const Named_type*
847 named_type() const;
849 // If this is a forward declaration, return it. Otherwise, return
850 // NULL.
851 Forward_declaration_type*
852 forward_declaration_type()
853 { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
855 const Forward_declaration_type*
856 forward_declaration_type() const
858 return this->convert_no_base<const Forward_declaration_type,
859 TYPE_FORWARD>();
862 // Return true if this type is not yet defined.
863 bool
864 is_undefined() const;
866 // Return true if this is the unsafe.pointer type. We currently
867 // represent that as pointer-to-void.
868 bool
869 is_unsafe_pointer_type() const
870 { return this->points_to() != NULL && this->points_to()->is_void_type(); }
872 // Look for field or method NAME for TYPE. Return an expression for
873 // it, bound to EXPR.
874 static Expression*
875 bind_field_or_method(Gogo*, const Type* type, Expression* expr,
876 const std::string& name, Location);
878 // Return true if NAME is an unexported field or method of TYPE.
879 static bool
880 is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
881 std::vector<const Named_type*>*);
883 // Convert the builtin named types.
884 static void
885 convert_builtin_named_types(Gogo*);
887 // Return the backend representation of this type.
888 Btype*
889 get_backend(Gogo*);
891 // Return a placeholder for the backend representation of the type.
892 // This will return a type of the correct size, but for which some
893 // of the fields may still need to be completed.
894 Btype*
895 get_backend_placeholder(Gogo*);
897 // Finish the backend representation of a placeholder.
898 void
899 finish_backend(Gogo*, Btype*);
901 // Build a type descriptor entry for this type. Return a pointer to
902 // it. The location is the location which causes us to need the
903 // entry.
904 tree
905 type_descriptor_pointer(Gogo* gogo, Location);
907 // Return the type reflection string for this type.
908 std::string
909 reflection(Gogo*) const;
911 // Return a mangled name for the type. This is a name which can be
912 // used in assembler code. Identical types should have the same
913 // manged name.
914 std::string
915 mangled_name(Gogo*) const;
917 // If the size of the type can be determined, set *PSIZE to the size
918 // in bytes and return true. Otherwise, return false. This queries
919 // the backend.
920 bool
921 backend_type_size(Gogo*, unsigned int* psize);
923 // If the alignment of the type can be determined, set *PALIGN to
924 // the alignment in bytes and return true. Otherwise, return false.
925 bool
926 backend_type_align(Gogo*, unsigned int* palign);
928 // If the alignment of a struct field of this type can be
929 // determined, set *PALIGN to the alignment in bytes and return
930 // true. Otherwise, return false.
931 bool
932 backend_type_field_align(Gogo*, unsigned int* palign);
934 // Whether the backend size is known.
935 bool
936 is_backend_type_size_known(Gogo*);
938 // Get the hash and equality functions for a type.
939 void
940 type_functions(Gogo*, Named_type* name, Function_type* hash_fntype,
941 Function_type* equal_fntype, Named_object** hash_fn,
942 Named_object** equal_fn);
944 // Write the hash and equality type functions.
945 void
946 write_specific_type_functions(Gogo*, Named_type*,
947 const std::string& hash_name,
948 Function_type* hash_fntype,
949 const std::string& equal_name,
950 Function_type* equal_fntype);
952 // Export the type.
953 void
954 export_type(Export* exp) const
955 { this->do_export(exp); }
957 // Import a type.
958 static Type*
959 import_type(Import*);
961 protected:
962 Type(Type_classification);
964 // Functions implemented by the child class.
966 // Traverse the subtypes.
967 virtual int
968 do_traverse(Traverse*);
970 // Verify the type.
971 virtual bool
972 do_verify()
973 { return true; }
975 virtual bool
976 do_has_pointer() const
977 { return false; }
979 virtual bool
980 do_compare_is_identity(Gogo*) = 0;
982 virtual unsigned int
983 do_hash_for_method(Gogo*) const;
985 virtual Btype*
986 do_get_backend(Gogo*) = 0;
988 virtual Expression*
989 do_type_descriptor(Gogo*, Named_type* name) = 0;
991 virtual void
992 do_reflection(Gogo*, std::string*) const = 0;
994 virtual void
995 do_mangled_name(Gogo*, std::string*) const = 0;
997 virtual void
998 do_export(Export*) const;
1000 // Return whether a method expects a pointer as the receiver.
1001 static bool
1002 method_expects_pointer(const Named_object*);
1004 // Finalize the methods for a type.
1005 static void
1006 finalize_methods(Gogo*, const Type*, Location, Methods**);
1008 // Return a method from a set of methods.
1009 static Method*
1010 method_function(const Methods*, const std::string& name,
1011 bool* is_ambiguous);
1013 // A mapping from interfaces to the associated interface method
1014 // tables for this type. This maps to a decl.
1015 typedef Unordered_map_hash(const Interface_type*, tree, Type_hash_identical,
1016 Type_identical) Interface_method_tables;
1018 // Return a pointer to the interface method table for TYPE for the
1019 // interface INTERFACE.
1020 static tree
1021 interface_method_table(Gogo* gogo, Type* type,
1022 const Interface_type *interface, bool is_pointer,
1023 Interface_method_tables** method_tables,
1024 Interface_method_tables** pointer_tables);
1026 // Return a composite literal for the type descriptor entry for a
1027 // type.
1028 static Expression*
1029 type_descriptor(Gogo*, Type*);
1031 // Return a composite literal for the type descriptor entry for
1032 // TYPE, using NAME as the name of the type.
1033 static Expression*
1034 named_type_descriptor(Gogo*, Type* type, Named_type* name);
1036 // Return a composite literal for a plain type descriptor for this
1037 // type with the given kind and name.
1038 Expression*
1039 plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
1041 // Build a composite literal for the basic type descriptor.
1042 Expression*
1043 type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
1044 const Methods*, bool only_value_methods);
1046 // For the benefit of child class reflection string generation.
1047 void
1048 append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
1049 { type->do_reflection(gogo, ret); }
1051 // For the benefit of child class mangling.
1052 void
1053 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
1054 { type->do_mangled_name(gogo, ret); }
1056 // Incorporate a string into a hash code.
1057 static unsigned int
1058 hash_string(const std::string&, unsigned int);
1060 // Return the backend representation for the underlying type of a
1061 // named type.
1062 static Btype*
1063 get_named_base_btype(Gogo* gogo, Type* base_type)
1064 { return base_type->get_btype_without_hash(gogo); }
1066 private:
1067 // Convert to the desired type classification, or return NULL. This
1068 // is a controlled dynamic_cast.
1069 template<typename Type_class, Type_classification type_classification>
1070 Type_class*
1071 convert()
1073 Type* base = this->base();
1074 return (base->classification_ == type_classification
1075 ? static_cast<Type_class*>(base)
1076 : NULL);
1079 template<typename Type_class, Type_classification type_classification>
1080 const Type_class*
1081 convert() const
1083 const Type* base = this->base();
1084 return (base->classification_ == type_classification
1085 ? static_cast<Type_class*>(base)
1086 : NULL);
1089 template<typename Type_class, Type_classification type_classification>
1090 Type_class*
1091 convert_no_base()
1093 return (this->classification_ == type_classification
1094 ? static_cast<Type_class*>(this)
1095 : NULL);
1098 template<typename Type_class, Type_classification type_classification>
1099 const Type_class*
1100 convert_no_base() const
1102 return (this->classification_ == type_classification
1103 ? static_cast<Type_class*>(this)
1104 : NULL);
1107 // Support for are_assignable and are_assignable_hidden_ok.
1108 static bool
1109 are_assignable_check_hidden(const Type* lhs, const Type* rhs,
1110 bool check_hidden_fields, std::string* reason);
1112 // Map unnamed types to type descriptor decls.
1113 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
1114 Type_identical) Type_descriptor_vars;
1116 static Type_descriptor_vars type_descriptor_vars;
1118 // Build the type descriptor variable for this type.
1119 void
1120 make_type_descriptor_var(Gogo*);
1122 // Return the name of the type descriptor variable. If NAME is not
1123 // NULL, it is the name to use.
1124 std::string
1125 type_descriptor_var_name(Gogo*, Named_type* name);
1127 // Return true if the type descriptor for this type should be
1128 // defined in some other package. If NAME is not NULL, it is the
1129 // name of this type. If this returns true it sets *PACKAGE to the
1130 // package where the type descriptor is defined.
1131 bool
1132 type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
1134 // Build the hash and equality type functions for a type which needs
1135 // specific functions.
1136 void
1137 specific_type_functions(Gogo*, Named_type*, Function_type* hash_fntype,
1138 Function_type* equal_fntype, Named_object** hash_fn,
1139 Named_object** equal_fn);
1141 // Build a composite literal for the uncommon type information.
1142 Expression*
1143 uncommon_type_constructor(Gogo*, Type* uncommon_type,
1144 Named_type*, const Methods*,
1145 bool only_value_methods) const;
1147 // Build a composite literal for the methods.
1148 Expression*
1149 methods_constructor(Gogo*, Type* methods_type, const Methods*,
1150 bool only_value_methods) const;
1152 // Build a composite literal for one method.
1153 Expression*
1154 method_constructor(Gogo*, Type* method_type, const std::string& name,
1155 const Method*, bool only_value_methods) const;
1157 static tree
1158 build_receive_return_type(tree type);
1160 // A hash table we use to avoid infinite recursion.
1161 typedef Unordered_set_hash(const Named_type*, Type_hash_identical,
1162 Type_identical) Types_seen;
1164 // Add all methods for TYPE to the list of methods for THIS.
1165 static void
1166 add_methods_for_type(const Type* type, const Method::Field_indexes*,
1167 unsigned int depth, bool, bool, Types_seen*,
1168 Methods**);
1170 static void
1171 add_local_methods_for_type(const Named_type* type,
1172 const Method::Field_indexes*,
1173 unsigned int depth, bool, bool, Methods**);
1175 static void
1176 add_embedded_methods_for_type(const Type* type,
1177 const Method::Field_indexes*,
1178 unsigned int depth, bool, bool, Types_seen*,
1179 Methods**);
1181 static void
1182 add_interface_methods_for_type(const Type* type,
1183 const Method::Field_indexes*,
1184 unsigned int depth, Methods**);
1186 // Build stub methods for a type.
1187 static void
1188 build_stub_methods(Gogo*, const Type* type, const Methods* methods,
1189 Location);
1191 static void
1192 build_one_stub_method(Gogo*, Method*, const char* receiver_name,
1193 const Typed_identifier_list*, bool is_varargs,
1194 Location);
1196 static Expression*
1197 apply_field_indexes(Expression*, const Method::Field_indexes*,
1198 Location);
1200 // Look for a field or method named NAME in TYPE.
1201 static bool
1202 find_field_or_method(const Type* type, const std::string& name,
1203 bool receiver_can_be_pointer,
1204 std::vector<const Named_type*>*, int* level,
1205 bool* is_method, bool* found_pointer_method,
1206 std::string* ambig1, std::string* ambig2);
1208 // Get the backend representation for a type without looking in the
1209 // hash table for identical types.
1210 Btype*
1211 get_btype_without_hash(Gogo*);
1213 // A backend type that may be a placeholder.
1214 struct Type_btype_entry
1216 Btype *btype;
1217 bool is_placeholder;
1220 // A mapping from Type to Btype*, used to ensure that the backend
1221 // representation of identical types is identical. This is only
1222 // used for unnamed types.
1223 typedef Unordered_map_hash(const Type*, Type_btype_entry,
1224 Type_hash_identical, Type_identical) Type_btypes;
1226 static Type_btypes type_btypes;
1228 // A list of builtin named types.
1229 static std::vector<Named_type*> named_builtin_types;
1231 // A map from types which need specific type functions to the type
1232 // functions themselves.
1233 typedef std::pair<Named_object*, Named_object*> Hash_equal_fn;
1234 typedef Unordered_map_hash(const Type*, Hash_equal_fn, Type_hash_identical,
1235 Type_identical) Type_functions;
1237 static Type_functions type_functions_table;
1239 // The type classification.
1240 Type_classification classification_;
1241 // The backend representation of the type, once it has been
1242 // determined.
1243 Btype* btype_;
1244 // The type descriptor for this type. This starts out as NULL and
1245 // is filled in as needed.
1246 Bvariable* type_descriptor_var_;
1249 // Type hash table operations.
1251 class Type_hash_identical
1253 public:
1254 unsigned int
1255 operator()(const Type* type) const
1256 { return type->hash_for_method(NULL); }
1259 class Type_identical
1261 public:
1262 bool
1263 operator()(const Type* t1, const Type* t2) const
1264 { return Type::are_identical(t1, t2, false, NULL); }
1267 // An identifier with a type.
1269 class Typed_identifier
1271 public:
1272 Typed_identifier(const std::string& name, Type* type,
1273 Location location)
1274 : name_(name), type_(type), location_(location)
1277 // Get the name.
1278 const std::string&
1279 name() const
1280 { return this->name_; }
1282 // Get the type.
1283 Type*
1284 type() const
1285 { return this->type_; }
1287 // Return the location where the name was seen. This is not always
1288 // meaningful.
1289 Location
1290 location() const
1291 { return this->location_; }
1293 // Set the type--sometimes we see the identifier before the type.
1294 void
1295 set_type(Type* type)
1297 go_assert(this->type_ == NULL || type->is_error_type());
1298 this->type_ = type;
1301 private:
1302 // Identifier name.
1303 std::string name_;
1304 // Type.
1305 Type* type_;
1306 // The location where the name was seen.
1307 Location location_;
1310 // A list of Typed_identifiers.
1312 class Typed_identifier_list
1314 public:
1315 Typed_identifier_list()
1316 : entries_()
1319 // Whether the list is empty.
1320 bool
1321 empty() const
1322 { return this->entries_.empty(); }
1324 // Return the number of entries in the list.
1325 size_t
1326 size() const
1327 { return this->entries_.size(); }
1329 // Add an entry to the end of the list.
1330 void
1331 push_back(const Typed_identifier& td)
1332 { this->entries_.push_back(td); }
1334 // Remove an entry from the end of the list.
1335 void
1336 pop_back()
1337 { this->entries_.pop_back(); }
1339 // Set the type of entry I to TYPE.
1340 void
1341 set_type(size_t i, Type* type)
1343 go_assert(i < this->entries_.size());
1344 this->entries_[i].set_type(type);
1347 // Sort the entries by name.
1348 void
1349 sort_by_name();
1351 // Traverse types.
1353 traverse(Traverse*);
1355 // Return the first and last elements.
1356 Typed_identifier&
1357 front()
1358 { return this->entries_.front(); }
1360 const Typed_identifier&
1361 front() const
1362 { return this->entries_.front(); }
1364 Typed_identifier&
1365 back()
1366 { return this->entries_.back(); }
1368 const Typed_identifier&
1369 back() const
1370 { return this->entries_.back(); }
1372 const Typed_identifier&
1373 at(size_t i) const
1374 { return this->entries_.at(i); }
1376 void
1377 set(size_t i, const Typed_identifier& t)
1378 { this->entries_.at(i) = t; }
1380 void
1381 resize(size_t c)
1383 go_assert(c <= this->entries_.size());
1384 this->entries_.resize(c, Typed_identifier("", NULL,
1385 Linemap::unknown_location()));
1388 void
1389 reserve(size_t c)
1390 { this->entries_.reserve(c); }
1392 // Iterators.
1394 typedef std::vector<Typed_identifier>::iterator iterator;
1395 typedef std::vector<Typed_identifier>::const_iterator const_iterator;
1397 iterator
1398 begin()
1399 { return this->entries_.begin(); }
1401 const_iterator
1402 begin() const
1403 { return this->entries_.begin(); }
1405 iterator
1406 end()
1407 { return this->entries_.end(); }
1409 const_iterator
1410 end() const
1411 { return this->entries_.end(); }
1413 // Return a copy of this list. This returns an independent copy of
1414 // the vector, but does not copy the types.
1415 Typed_identifier_list*
1416 copy() const;
1418 private:
1419 std::vector<Typed_identifier> entries_;
1422 // The type of an integer.
1424 class Integer_type : public Type
1426 public:
1427 // Create a new integer type.
1428 static Named_type*
1429 create_integer_type(const char* name, bool is_unsigned, int bits,
1430 int runtime_type_kind);
1432 // Look up an existing integer type.
1433 static Named_type*
1434 lookup_integer_type(const char* name);
1436 // Create an abstract integer type.
1437 static Integer_type*
1438 create_abstract_integer_type();
1440 // Create an abstract character type.
1441 static Integer_type*
1442 create_abstract_character_type();
1444 // Whether this is an abstract integer type.
1445 bool
1446 is_abstract() const
1447 { return this->is_abstract_; }
1449 // Whether this is an unsigned type.
1450 bool
1451 is_unsigned() const
1452 { return this->is_unsigned_; }
1454 // The number of bits.
1456 bits() const
1457 { return this->bits_; }
1459 // Whether this type is the same as T.
1460 bool
1461 is_identical(const Integer_type* t) const;
1463 // Whether this is the type "byte" or another name for "byte".
1464 bool
1465 is_byte() const
1466 { return this->is_byte_; }
1468 // Mark this as the "byte" type.
1469 void
1470 set_is_byte()
1471 { this->is_byte_ = true; }
1473 // Whether this is the type "rune" or another name for "rune".
1474 bool
1475 is_rune() const
1476 { return this->is_rune_; }
1478 // Mark this as the "rune" type.
1479 void
1480 set_is_rune()
1481 { this->is_rune_ = true; }
1483 protected:
1484 bool
1485 do_compare_is_identity(Gogo*)
1486 { return true; }
1488 unsigned int
1489 do_hash_for_method(Gogo*) const;
1491 Btype*
1492 do_get_backend(Gogo*);
1494 Expression*
1495 do_type_descriptor(Gogo*, Named_type*);
1497 void
1498 do_reflection(Gogo*, std::string*) const;
1500 void
1501 do_mangled_name(Gogo*, std::string*) const;
1503 private:
1504 Integer_type(bool is_abstract, bool is_unsigned, int bits,
1505 int runtime_type_kind)
1506 : Type(TYPE_INTEGER),
1507 is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
1508 is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
1511 // Map names of integer types to the types themselves.
1512 typedef std::map<std::string, Named_type*> Named_integer_types;
1513 static Named_integer_types named_integer_types;
1515 // True if this is an abstract type.
1516 bool is_abstract_;
1517 // True if this is an unsigned type.
1518 bool is_unsigned_;
1519 // True if this is the byte type.
1520 bool is_byte_;
1521 // True if this is the rune type.
1522 bool is_rune_;
1523 // The number of bits.
1524 int bits_;
1525 // The runtime type code used in the type descriptor for this type.
1526 int runtime_type_kind_;
1529 // The type of a floating point number.
1531 class Float_type : public Type
1533 public:
1534 // Create a new float type.
1535 static Named_type*
1536 create_float_type(const char* name, int bits, int runtime_type_kind);
1538 // Look up an existing float type.
1539 static Named_type*
1540 lookup_float_type(const char* name);
1542 // Create an abstract float type.
1543 static Float_type*
1544 create_abstract_float_type();
1546 // Whether this is an abstract float type.
1547 bool
1548 is_abstract() const
1549 { return this->is_abstract_; }
1551 // The number of bits.
1553 bits() const
1554 { return this->bits_; }
1556 // Whether this type is the same as T.
1557 bool
1558 is_identical(const Float_type* t) const;
1560 protected:
1561 bool
1562 do_compare_is_identity(Gogo*)
1563 { return false; }
1565 unsigned int
1566 do_hash_for_method(Gogo*) const;
1568 Btype*
1569 do_get_backend(Gogo*);
1571 Expression*
1572 do_type_descriptor(Gogo*, Named_type*);
1574 void
1575 do_reflection(Gogo*, std::string*) const;
1577 void
1578 do_mangled_name(Gogo*, std::string*) const;
1580 private:
1581 Float_type(bool is_abstract, int bits, int runtime_type_kind)
1582 : Type(TYPE_FLOAT),
1583 is_abstract_(is_abstract), bits_(bits),
1584 runtime_type_kind_(runtime_type_kind)
1587 // Map names of float types to the types themselves.
1588 typedef std::map<std::string, Named_type*> Named_float_types;
1589 static Named_float_types named_float_types;
1591 // True if this is an abstract type.
1592 bool is_abstract_;
1593 // The number of bits in the floating point value.
1594 int bits_;
1595 // The runtime type code used in the type descriptor for this type.
1596 int runtime_type_kind_;
1599 // The type of a complex number.
1601 class Complex_type : public Type
1603 public:
1604 // Create a new complex type.
1605 static Named_type*
1606 create_complex_type(const char* name, int bits, int runtime_type_kind);
1608 // Look up an existing complex type.
1609 static Named_type*
1610 lookup_complex_type(const char* name);
1612 // Create an abstract complex type.
1613 static Complex_type*
1614 create_abstract_complex_type();
1616 // Whether this is an abstract complex type.
1617 bool
1618 is_abstract() const
1619 { return this->is_abstract_; }
1621 // The number of bits: 64 or 128.
1622 int bits() const
1623 { return this->bits_; }
1625 // Whether this type is the same as T.
1626 bool
1627 is_identical(const Complex_type* t) const;
1629 protected:
1630 bool
1631 do_compare_is_identity(Gogo*)
1632 { return false; }
1634 unsigned int
1635 do_hash_for_method(Gogo*) const;
1637 Btype*
1638 do_get_backend(Gogo*);
1640 Expression*
1641 do_type_descriptor(Gogo*, Named_type*);
1643 void
1644 do_reflection(Gogo*, std::string*) const;
1646 void
1647 do_mangled_name(Gogo*, std::string*) const;
1649 private:
1650 Complex_type(bool is_abstract, int bits, int runtime_type_kind)
1651 : Type(TYPE_COMPLEX),
1652 is_abstract_(is_abstract), bits_(bits),
1653 runtime_type_kind_(runtime_type_kind)
1656 // Map names of complex types to the types themselves.
1657 typedef std::map<std::string, Named_type*> Named_complex_types;
1658 static Named_complex_types named_complex_types;
1660 // True if this is an abstract type.
1661 bool is_abstract_;
1662 // The number of bits in the complex value--64 or 128.
1663 int bits_;
1664 // The runtime type code used in the type descriptor for this type.
1665 int runtime_type_kind_;
1668 // The type of a string.
1670 class String_type : public Type
1672 public:
1673 String_type()
1674 : Type(TYPE_STRING)
1677 // Return a tree for the length of STRING.
1678 static tree
1679 length_tree(Gogo*, tree string);
1681 // Return a tree which points to the bytes of STRING.
1682 static tree
1683 bytes_tree(Gogo*, tree string);
1685 protected:
1686 bool
1687 do_has_pointer() const
1688 { return true; }
1690 bool
1691 do_compare_is_identity(Gogo*)
1692 { return false; }
1694 Btype*
1695 do_get_backend(Gogo*);
1697 Expression*
1698 do_type_descriptor(Gogo*, Named_type*);
1700 void
1701 do_reflection(Gogo*, std::string*) const;
1703 void
1704 do_mangled_name(Gogo*, std::string* ret) const;
1706 private:
1707 // The named string type.
1708 static Named_type* string_type_;
1711 // The type of a function.
1713 class Function_type : public Type
1715 public:
1716 Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
1717 Typed_identifier_list* results, Location location)
1718 : Type(TYPE_FUNCTION),
1719 receiver_(receiver), parameters_(parameters), results_(results),
1720 location_(location), is_varargs_(false), is_builtin_(false),
1721 fnbtype_(NULL)
1724 // Get the receiver.
1725 const Typed_identifier*
1726 receiver() const
1727 { return this->receiver_; }
1729 // Get the return names and types.
1730 const Typed_identifier_list*
1731 results() const
1732 { return this->results_; }
1734 // Get the parameter names and types.
1735 const Typed_identifier_list*
1736 parameters() const
1737 { return this->parameters_; }
1739 // Whether this is a varargs function.
1740 bool
1741 is_varargs() const
1742 { return this->is_varargs_; }
1744 // Whether this is a builtin function.
1745 bool
1746 is_builtin() const
1747 { return this->is_builtin_; }
1749 // The location where this type was defined.
1750 Location
1751 location() const
1752 { return this->location_; }
1754 // Return whether this is a method type.
1755 bool
1756 is_method() const
1757 { return this->receiver_ != NULL; }
1759 // Whether T is a valid redeclaration of this type. This is called
1760 // when a function is declared more than once.
1761 bool
1762 is_valid_redeclaration(const Function_type* t, std::string*) const;
1764 // Whether this type is the same as T.
1765 bool
1766 is_identical(const Function_type* t, bool ignore_receiver,
1767 bool errors_are_identical, std::string*) const;
1769 // Record that this is a varargs function.
1770 void
1771 set_is_varargs()
1772 { this->is_varargs_ = true; }
1774 // Record that this is a builtin function.
1775 void
1776 set_is_builtin()
1777 { this->is_builtin_ = true; }
1779 // Import a function type.
1780 static Function_type*
1781 do_import(Import*);
1783 // Return a copy of this type without a receiver. This is only
1784 // valid for a method type.
1785 Function_type*
1786 copy_without_receiver() const;
1788 // Return a copy of this type with a receiver. This is used when an
1789 // interface method is attached to a named or struct type.
1790 Function_type*
1791 copy_with_receiver(Type*) const;
1793 // Return a copy of this type ignoring any receiver and using dummy
1794 // names for all parameters. This is used for thunks for method
1795 // values.
1796 Function_type*
1797 copy_with_names() const;
1799 static Type*
1800 make_function_type_descriptor_type();
1802 // Return the backend representation of this function type. This is used
1803 // as the real type of a backend function declaration or defintion.
1804 Btype*
1805 get_backend_fntype(Gogo*);
1807 protected:
1809 do_traverse(Traverse*);
1811 // A function descriptor may be allocated on the heap.
1812 bool
1813 do_has_pointer() const
1814 { return true; }
1816 bool
1817 do_compare_is_identity(Gogo*)
1818 { return false; }
1820 unsigned int
1821 do_hash_for_method(Gogo*) const;
1823 Btype*
1824 do_get_backend(Gogo*);
1826 Expression*
1827 do_type_descriptor(Gogo*, Named_type*);
1829 void
1830 do_reflection(Gogo*, std::string*) const;
1832 void
1833 do_mangled_name(Gogo*, std::string*) const;
1835 void
1836 do_export(Export*) const;
1838 private:
1839 Expression*
1840 type_descriptor_params(Type*, const Typed_identifier*,
1841 const Typed_identifier_list*);
1843 // The receiver name and type. This will be NULL for a normal
1844 // function, non-NULL for a method.
1845 Typed_identifier* receiver_;
1846 // The parameter names and types.
1847 Typed_identifier_list* parameters_;
1848 // The result names and types. This will be NULL if no result was
1849 // specified.
1850 Typed_identifier_list* results_;
1851 // The location where this type was defined. This exists solely to
1852 // give a location for the fields of the struct if this function
1853 // returns multiple values.
1854 Location location_;
1855 // Whether this function takes a variable number of arguments.
1856 bool is_varargs_;
1857 // Whether this is a special builtin function which can not simply
1858 // be called. This is used for len, cap, etc.
1859 bool is_builtin_;
1860 // The backend representation of this type for backend function
1861 // declarations and definitions.
1862 Btype* fnbtype_;
1865 // The type of a pointer.
1867 class Pointer_type : public Type
1869 public:
1870 Pointer_type(Type* to_type)
1871 : Type(TYPE_POINTER),
1872 to_type_(to_type)
1875 Type*
1876 points_to() const
1877 { return this->to_type_; }
1879 // Import a pointer type.
1880 static Pointer_type*
1881 do_import(Import*);
1883 static Type*
1884 make_pointer_type_descriptor_type();
1886 protected:
1888 do_traverse(Traverse*);
1890 bool
1891 do_has_pointer() const
1892 { return true; }
1894 bool
1895 do_compare_is_identity(Gogo*)
1896 { return true; }
1898 unsigned int
1899 do_hash_for_method(Gogo*) const;
1901 Btype*
1902 do_get_backend(Gogo*);
1904 Expression*
1905 do_type_descriptor(Gogo*, Named_type*);
1907 void
1908 do_reflection(Gogo*, std::string*) const;
1910 void
1911 do_mangled_name(Gogo*, std::string*) const;
1913 void
1914 do_export(Export*) const;
1916 private:
1917 // The type to which this type points.
1918 Type* to_type_;
1921 // The type of a field in a struct.
1923 class Struct_field
1925 public:
1926 explicit Struct_field(const Typed_identifier& typed_identifier)
1927 : typed_identifier_(typed_identifier), tag_(NULL), is_imported_(false)
1930 // The field name.
1931 const std::string&
1932 field_name() const;
1934 // Return whether this struct field is named NAME.
1935 bool
1936 is_field_name(const std::string& name) const;
1938 // Return whether this struct field is an unexported field named NAME.
1939 bool
1940 is_unexported_field_name(Gogo*, const std::string& name) const;
1942 // Return whether this struct field is an embedded built-in type.
1943 bool
1944 is_embedded_builtin(Gogo*) const;
1946 // The field type.
1947 Type*
1948 type() const
1949 { return this->typed_identifier_.type(); }
1951 // The field location.
1952 Location
1953 location() const
1954 { return this->typed_identifier_.location(); }
1956 // Whether the field has a tag.
1957 bool
1958 has_tag() const
1959 { return this->tag_ != NULL; }
1961 // The tag.
1962 const std::string&
1963 tag() const
1965 go_assert(this->tag_ != NULL);
1966 return *this->tag_;
1969 // Whether this is an anonymous field.
1970 bool
1971 is_anonymous() const
1972 { return this->typed_identifier_.name().empty(); }
1974 // Set the tag. FIXME: This is never freed.
1975 void
1976 set_tag(const std::string& tag)
1977 { this->tag_ = new std::string(tag); }
1979 // Record that this field is defined in an imported struct.
1980 void
1981 set_is_imported()
1982 { this->is_imported_ = true; }
1984 // Set the type. This is only used in error cases.
1985 void
1986 set_type(Type* type)
1987 { this->typed_identifier_.set_type(type); }
1989 private:
1990 // The field name, type, and location.
1991 Typed_identifier typed_identifier_;
1992 // The field tag. This is NULL if the field has no tag.
1993 std::string* tag_;
1994 // Whether this field is defined in an imported struct.
1995 bool is_imported_;
1998 // A list of struct fields.
2000 class Struct_field_list
2002 public:
2003 Struct_field_list()
2004 : entries_()
2007 // Whether the list is empty.
2008 bool
2009 empty() const
2010 { return this->entries_.empty(); }
2012 // Return the number of entries.
2013 size_t
2014 size() const
2015 { return this->entries_.size(); }
2017 // Add an entry to the end of the list.
2018 void
2019 push_back(const Struct_field& sf)
2020 { this->entries_.push_back(sf); }
2022 // Index into the list.
2023 const Struct_field&
2024 at(size_t i) const
2025 { return this->entries_.at(i); }
2027 // Last entry in list.
2028 Struct_field&
2029 back()
2030 { return this->entries_.back(); }
2032 // Iterators.
2034 typedef std::vector<Struct_field>::iterator iterator;
2035 typedef std::vector<Struct_field>::const_iterator const_iterator;
2037 iterator
2038 begin()
2039 { return this->entries_.begin(); }
2041 const_iterator
2042 begin() const
2043 { return this->entries_.begin(); }
2045 iterator
2046 end()
2047 { return this->entries_.end(); }
2049 const_iterator
2050 end() const
2051 { return this->entries_.end(); }
2053 private:
2054 std::vector<Struct_field> entries_;
2057 // The type of a struct.
2059 class Struct_type : public Type
2061 public:
2062 Struct_type(Struct_field_list* fields, Location location)
2063 : Type(TYPE_STRUCT),
2064 fields_(fields), location_(location), all_methods_(NULL)
2067 // Return the field NAME. This only looks at local fields, not at
2068 // embedded types. If the field is found, and PINDEX is not NULL,
2069 // this sets *PINDEX to the field index. If the field is not found,
2070 // this returns NULL.
2071 const Struct_field*
2072 find_local_field(const std::string& name, unsigned int *pindex) const;
2074 // Return the field number INDEX.
2075 const Struct_field*
2076 field(unsigned int index) const
2077 { return &this->fields_->at(index); }
2079 // Get the struct fields.
2080 const Struct_field_list*
2081 fields() const
2082 { return this->fields_; }
2084 // Return the number of fields.
2085 size_t
2086 field_count() const
2087 { return this->fields_->size(); }
2089 // Push a new field onto the end of the struct. This is used when
2090 // building a closure variable.
2091 void
2092 push_field(const Struct_field& sf)
2093 { this->fields_->push_back(sf); }
2095 // Return an expression referring to field NAME in STRUCT_EXPR, or
2096 // NULL if there is no field with that name.
2097 Field_reference_expression*
2098 field_reference(Expression* struct_expr, const std::string& name,
2099 Location) const;
2101 // Return the total number of fields, including embedded fields.
2102 // This is the number of values that can appear in a conversion to
2103 // this type.
2104 unsigned int
2105 total_field_count() const;
2107 // Whether this type is identical with T.
2108 bool
2109 is_identical(const Struct_type* t, bool errors_are_identical) const;
2111 // Whether this struct type has any hidden fields. This returns
2112 // true if any fields have hidden names, or if any non-pointer
2113 // anonymous fields have types with hidden fields.
2114 bool
2115 struct_has_hidden_fields(const Named_type* within, std::string*) const;
2117 // Return whether NAME is a local field which is not exported. This
2118 // is only used for better error reporting.
2119 bool
2120 is_unexported_local_field(Gogo*, const std::string& name) const;
2122 // If this is an unnamed struct, build the complete list of methods,
2123 // including those from anonymous fields, and build methods stubs if
2124 // needed.
2125 void
2126 finalize_methods(Gogo*);
2128 // Return whether this type has any methods. This should only be
2129 // called after the finalize_methods pass.
2130 bool
2131 has_any_methods() const
2132 { return this->all_methods_ != NULL; }
2134 // Return the methods for tihs type. This should only be called
2135 // after the finalize_methods pass.
2136 const Methods*
2137 methods() const
2138 { return this->all_methods_; }
2140 // Return the method to use for NAME. This returns NULL if there is
2141 // no such method or if the method is ambiguous. When it returns
2142 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2143 Method*
2144 method_function(const std::string& name, bool* is_ambiguous) const;
2146 // Return a pointer to the interface method table for this type for
2147 // the interface INTERFACE. If IS_POINTER is true, set the type
2148 // descriptor to a pointer to this type, otherwise set it to this
2149 // type.
2150 tree
2151 interface_method_table(Gogo*, const Interface_type* interface,
2152 bool is_pointer);
2154 // Traverse just the field types of a struct type.
2156 traverse_field_types(Traverse* traverse)
2157 { return this->do_traverse(traverse); }
2159 // If the offset of field INDEX in the backend implementation can be
2160 // determined, set *POFFSET to the offset in bytes and return true.
2161 // Otherwise, return false.
2162 bool
2163 backend_field_offset(Gogo*, unsigned int index, unsigned int* poffset);
2165 // Finish the backend representation of all the fields.
2166 void
2167 finish_backend_fields(Gogo*);
2169 // Import a struct type.
2170 static Struct_type*
2171 do_import(Import*);
2173 static Type*
2174 make_struct_type_descriptor_type();
2176 // Write the hash function for this type.
2177 void
2178 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2180 // Write the equality function for this type.
2181 void
2182 write_equal_function(Gogo*, Named_type*);
2184 protected:
2186 do_traverse(Traverse*);
2188 bool
2189 do_verify();
2191 bool
2192 do_has_pointer() const;
2194 bool
2195 do_compare_is_identity(Gogo*);
2197 unsigned int
2198 do_hash_for_method(Gogo*) const;
2200 Btype*
2201 do_get_backend(Gogo*);
2203 Expression*
2204 do_type_descriptor(Gogo*, Named_type*);
2206 void
2207 do_reflection(Gogo*, std::string*) const;
2209 void
2210 do_mangled_name(Gogo*, std::string*) const;
2212 void
2213 do_export(Export*) const;
2215 private:
2216 // Used to merge method sets of identical unnamed structs.
2217 typedef Unordered_map_hash(Struct_type*, Struct_type*, Type_hash_identical,
2218 Type_identical) Identical_structs;
2220 static Identical_structs identical_structs;
2222 // Used to manage method tables for identical unnamed structs.
2223 typedef std::pair<Interface_method_tables*, Interface_method_tables*>
2224 Struct_method_table_pair;
2226 typedef Unordered_map_hash(Struct_type*, Struct_method_table_pair*,
2227 Type_hash_identical, Type_identical)
2228 Struct_method_tables;
2230 static Struct_method_tables struct_method_tables;
2232 // Used to avoid infinite loops in field_reference_depth.
2233 struct Saw_named_type
2235 Saw_named_type* next;
2236 Named_type* nt;
2239 Field_reference_expression*
2240 field_reference_depth(Expression* struct_expr, const std::string& name,
2241 Location, Saw_named_type*,
2242 unsigned int* depth) const;
2244 // The fields of the struct.
2245 Struct_field_list* fields_;
2246 // The place where the struct was declared.
2247 Location location_;
2248 // If this struct is unnamed, a list of methods.
2249 Methods* all_methods_;
2252 // The type of an array.
2254 class Array_type : public Type
2256 public:
2257 Array_type(Type* element_type, Expression* length)
2258 : Type(TYPE_ARRAY),
2259 element_type_(element_type), length_(length), length_tree_(NULL)
2262 // Return the element type.
2263 Type*
2264 element_type() const
2265 { return this->element_type_; }
2267 // Return the length. This will return NULL for an open array.
2268 Expression*
2269 length() const
2270 { return this->length_; }
2272 // Whether this type is identical with T.
2273 bool
2274 is_identical(const Array_type* t, bool errors_are_identical) const;
2276 // Whether this type has any hidden fields.
2277 bool
2278 array_has_hidden_fields(const Named_type* within, std::string* reason) const
2279 { return this->element_type_->has_hidden_fields(within, reason); }
2281 // Return a tree for the pointer to the values in an array.
2282 tree
2283 value_pointer_tree(Gogo*, tree array) const;
2285 // Return a tree for the length of an array with this type.
2286 tree
2287 length_tree(Gogo*, tree array);
2289 // Return a tree for the capacity of an array with this type.
2290 tree
2291 capacity_tree(Gogo*, tree array);
2293 // Import an array type.
2294 static Array_type*
2295 do_import(Import*);
2297 // Return the backend representation of the element type.
2298 Btype*
2299 get_backend_element(Gogo*, bool use_placeholder);
2301 // Return the backend representation of the length.
2302 Bexpression*
2303 get_backend_length(Gogo*);
2305 // Finish the backend representation of the element type.
2306 void
2307 finish_backend_element(Gogo*);
2309 static Type*
2310 make_array_type_descriptor_type();
2312 static Type*
2313 make_slice_type_descriptor_type();
2315 // Write the hash function for this type.
2316 void
2317 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2319 // Write the equality function for this type.
2320 void
2321 write_equal_function(Gogo*, Named_type*);
2323 protected:
2325 do_traverse(Traverse* traverse);
2327 bool
2328 do_verify();
2330 bool
2331 do_has_pointer() const
2333 return this->length_ == NULL || this->element_type_->has_pointer();
2336 bool
2337 do_compare_is_identity(Gogo*);
2339 unsigned int
2340 do_hash_for_method(Gogo*) const;
2342 Btype*
2343 do_get_backend(Gogo*);
2345 Expression*
2346 do_type_descriptor(Gogo*, Named_type*);
2348 void
2349 do_reflection(Gogo*, std::string*) const;
2351 void
2352 do_mangled_name(Gogo*, std::string*) const;
2354 void
2355 do_export(Export*) const;
2357 private:
2358 bool
2359 verify_length();
2361 tree
2362 get_length_tree(Gogo*);
2364 Expression*
2365 array_type_descriptor(Gogo*, Named_type*);
2367 Expression*
2368 slice_type_descriptor(Gogo*, Named_type*);
2370 // The type of elements of the array.
2371 Type* element_type_;
2372 // The number of elements. This may be NULL.
2373 Expression* length_;
2374 // The length as a tree. We only want to compute this once.
2375 tree length_tree_;
2378 // The type of a map.
2380 class Map_type : public Type
2382 public:
2383 Map_type(Type* key_type, Type* val_type, Location location)
2384 : Type(TYPE_MAP),
2385 key_type_(key_type), val_type_(val_type), location_(location)
2388 // Return the key type.
2389 Type*
2390 key_type() const
2391 { return this->key_type_; }
2393 // Return the value type.
2394 Type*
2395 val_type() const
2396 { return this->val_type_; }
2398 // Whether this type is identical with T.
2399 bool
2400 is_identical(const Map_type* t, bool errors_are_identical) const;
2402 // Import a map type.
2403 static Map_type*
2404 do_import(Import*);
2406 static Type*
2407 make_map_type_descriptor_type();
2409 static Type*
2410 make_map_descriptor_type();
2412 // Build a map descriptor for this type. Return a pointer to it.
2413 // The location is the location which causes us to need the
2414 // descriptor.
2415 tree
2416 map_descriptor_pointer(Gogo* gogo, Location);
2418 protected:
2420 do_traverse(Traverse*);
2422 bool
2423 do_verify();
2425 bool
2426 do_has_pointer() const
2427 { return true; }
2429 bool
2430 do_compare_is_identity(Gogo*)
2431 { return false; }
2433 unsigned int
2434 do_hash_for_method(Gogo*) const;
2436 Btype*
2437 do_get_backend(Gogo*);
2439 Expression*
2440 do_type_descriptor(Gogo*, Named_type*);
2442 void
2443 do_reflection(Gogo*, std::string*) const;
2445 void
2446 do_mangled_name(Gogo*, std::string*) const;
2448 void
2449 do_export(Export*) const;
2451 private:
2452 // Mapping from map types to map descriptors.
2453 typedef Unordered_map_hash(const Map_type*, Bvariable*, Type_hash_identical,
2454 Type_identical) Map_descriptors;
2455 static Map_descriptors map_descriptors;
2457 Bvariable*
2458 map_descriptor(Gogo*);
2460 // The key type.
2461 Type* key_type_;
2462 // The value type.
2463 Type* val_type_;
2464 // Where the type was defined.
2465 Location location_;
2468 // The type of a channel.
2470 class Channel_type : public Type
2472 public:
2473 Channel_type(bool may_send, bool may_receive, Type* element_type)
2474 : Type(TYPE_CHANNEL),
2475 may_send_(may_send), may_receive_(may_receive),
2476 element_type_(element_type)
2477 { go_assert(may_send || may_receive); }
2479 // Whether this channel can send data.
2480 bool
2481 may_send() const
2482 { return this->may_send_; }
2484 // Whether this channel can receive data.
2485 bool
2486 may_receive() const
2487 { return this->may_receive_; }
2489 // The type of the values that may be sent on this channel. This is
2490 // NULL if any type may be sent.
2491 Type*
2492 element_type() const
2493 { return this->element_type_; }
2495 // Whether this type is identical with T.
2496 bool
2497 is_identical(const Channel_type* t, bool errors_are_identical) const;
2499 // Import a channel type.
2500 static Channel_type*
2501 do_import(Import*);
2503 static Type*
2504 make_chan_type_descriptor_type();
2506 protected:
2508 do_traverse(Traverse* traverse)
2509 { return Type::traverse(this->element_type_, traverse); }
2511 bool
2512 do_has_pointer() const
2513 { return true; }
2515 bool
2516 do_compare_is_identity(Gogo*)
2517 { return true; }
2519 unsigned int
2520 do_hash_for_method(Gogo*) const;
2522 Btype*
2523 do_get_backend(Gogo*);
2525 Expression*
2526 do_type_descriptor(Gogo*, Named_type*);
2528 void
2529 do_reflection(Gogo*, std::string*) const;
2531 void
2532 do_mangled_name(Gogo*, std::string*) const;
2534 void
2535 do_export(Export*) const;
2537 private:
2538 // Whether this channel can send data.
2539 bool may_send_;
2540 // Whether this channel can receive data.
2541 bool may_receive_;
2542 // The types of elements which may be sent on this channel. If this
2543 // is NULL, it means that any type may be sent.
2544 Type* element_type_;
2547 // An interface type.
2549 class Interface_type : public Type
2551 public:
2552 Interface_type(Typed_identifier_list* methods, Location location)
2553 : Type(TYPE_INTERFACE),
2554 parse_methods_(methods), all_methods_(NULL), location_(location),
2555 interface_btype_(NULL), assume_identical_(NULL),
2556 methods_are_finalized_(false), seen_(false)
2557 { go_assert(methods == NULL || !methods->empty()); }
2559 // The location where the interface type was defined.
2560 Location
2561 location() const
2562 { return this->location_; }
2564 // Return whether this is an empty interface.
2565 bool
2566 is_empty() const
2568 go_assert(this->methods_are_finalized_);
2569 return this->all_methods_ == NULL;
2572 // Return the list of methods. This will return NULL for an empty
2573 // interface.
2574 const Typed_identifier_list*
2575 methods() const;
2577 // Return the number of methods.
2578 size_t
2579 method_count() const;
2581 // Return the method NAME, or NULL.
2582 const Typed_identifier*
2583 find_method(const std::string& name) const;
2585 // Return the zero-based index of method NAME.
2586 size_t
2587 method_index(const std::string& name) const;
2589 // Finalize the methods. This sets all_methods_. This handles
2590 // interface inheritance.
2591 void
2592 finalize_methods();
2594 // Return true if T implements this interface. If this returns
2595 // false, and REASON is not NULL, it sets *REASON to the reason that
2596 // it fails.
2597 bool
2598 implements_interface(const Type* t, std::string* reason) const;
2600 // Whether this type is identical with T. REASON is as in
2601 // implements_interface.
2602 bool
2603 is_identical(const Interface_type* t, bool errors_are_identical) const;
2605 // Whether we can assign T to this type. is_identical is known to
2606 // be false.
2607 bool
2608 is_compatible_for_assign(const Interface_type*, std::string* reason) const;
2610 // Return whether NAME is a method which is not exported. This is
2611 // only used for better error reporting.
2612 bool
2613 is_unexported_method(Gogo*, const std::string& name) const;
2615 // Import an interface type.
2616 static Interface_type*
2617 do_import(Import*);
2619 // Make a struct for an empty interface type.
2620 static Btype*
2621 get_backend_empty_interface_type(Gogo*);
2623 // Finish the backend representation of the method types.
2624 void
2625 finish_backend_methods(Gogo*);
2627 static Type*
2628 make_interface_type_descriptor_type();
2630 protected:
2632 do_traverse(Traverse*);
2634 bool
2635 do_has_pointer() const
2636 { return true; }
2638 bool
2639 do_compare_is_identity(Gogo*)
2640 { return false; }
2642 unsigned int
2643 do_hash_for_method(Gogo*) const;
2645 Btype*
2646 do_get_backend(Gogo*);
2648 Expression*
2649 do_type_descriptor(Gogo*, Named_type*);
2651 void
2652 do_reflection(Gogo*, std::string*) const;
2654 void
2655 do_mangled_name(Gogo*, std::string*) const;
2657 void
2658 do_export(Export*) const;
2660 private:
2661 // This type guards against infinite recursion when comparing
2662 // interface types. We keep a list of interface types assumed to be
2663 // identical during comparison. We just keep the list on the stack.
2664 // This permits us to compare cases like
2665 // type I1 interface { F() interface{I1} }
2666 // type I2 interface { F() interface{I2} }
2667 struct Assume_identical
2669 Assume_identical* next;
2670 const Interface_type* t1;
2671 const Interface_type* t2;
2674 bool
2675 assume_identical(const Interface_type*, const Interface_type*) const;
2677 // The list of methods associated with the interface from the
2678 // parser. This will be NULL for the empty interface. This may
2679 // include unnamed interface types.
2680 Typed_identifier_list* parse_methods_;
2681 // The list of all methods associated with the interface. This
2682 // expands any interface types listed in methods_. It is set by
2683 // finalize_methods. This will be NULL for the empty interface.
2684 Typed_identifier_list* all_methods_;
2685 // The location where the interface was defined.
2686 Location location_;
2687 // The backend representation of this type during backend conversion.
2688 Btype* interface_btype_;
2689 // A list of interface types assumed to be identical during
2690 // interface comparison.
2691 mutable Assume_identical* assume_identical_;
2692 // Whether the methods have been finalized.
2693 bool methods_are_finalized_;
2694 // Used to avoid endless recursion in do_mangled_name.
2695 mutable bool seen_;
2698 // The value we keep for a named type. This lets us get the right
2699 // name when we convert to trees. Note that we don't actually keep
2700 // the name here; the name is in the Named_object which points to
2701 // this. This object exists to hold a unique tree which represents
2702 // the type.
2704 class Named_type : public Type
2706 public:
2707 Named_type(Named_object* named_object, Type* type, Location location)
2708 : Type(TYPE_NAMED),
2709 named_object_(named_object), in_function_(NULL), in_function_index_(0),
2710 type_(type), local_methods_(NULL), all_methods_(NULL),
2711 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
2712 location_(location), named_btype_(NULL), dependencies_(),
2713 is_visible_(true), is_error_(false), is_placeholder_(false),
2714 is_converted_(false), is_circular_(false), is_verified_(false),
2715 seen_(false), seen_in_compare_is_identity_(false),
2716 seen_in_get_backend_(false)
2719 // Return the associated Named_object. This holds the actual name.
2720 Named_object*
2721 named_object()
2722 { return this->named_object_; }
2724 const Named_object*
2725 named_object() const
2726 { return this->named_object_; }
2728 // Set the Named_object. This is used when we see a type
2729 // declaration followed by a type.
2730 void
2731 set_named_object(Named_object* no)
2732 { this->named_object_ = no; }
2734 // Return the function in which this type is defined. This will
2735 // return NULL for a type defined in global scope.
2736 const Named_object*
2737 in_function(unsigned int *pindex) const
2739 *pindex = this->in_function_index_;
2740 return this->in_function_;
2743 // Set the function in which this type is defined.
2744 void
2745 set_in_function(Named_object* f, unsigned int index)
2747 this->in_function_ = f;
2748 this->in_function_index_ = index;
2751 // Return the name of the type.
2752 const std::string&
2753 name() const;
2755 // Return the name of the type for an error message. The difference
2756 // is that if the type is defined in a different package, this will
2757 // return PACKAGE.NAME.
2758 std::string
2759 message_name() const;
2761 // Return the underlying type.
2762 Type*
2763 real_type()
2764 { return this->type_; }
2766 const Type*
2767 real_type() const
2768 { return this->type_; }
2770 // Return the location.
2771 Location
2772 location() const
2773 { return this->location_; }
2775 // Whether this type is visible. This only matters when parsing.
2776 bool
2777 is_visible() const
2778 { return this->is_visible_; }
2780 // Mark this type as visible.
2781 void
2782 set_is_visible()
2783 { this->is_visible_ = true; }
2785 // Mark this type as invisible.
2786 void
2787 clear_is_visible()
2788 { this->is_visible_ = false; }
2790 // Whether this is a builtin type.
2791 bool
2792 is_builtin() const
2793 { return Linemap::is_predeclared_location(this->location_); }
2795 // Whether this is an alias. There are currently two aliases: byte
2796 // and rune.
2797 bool
2798 is_alias() const;
2800 // Whether this is a circular type: a pointer or function type that
2801 // refers to itself, which is not possible in C.
2802 bool
2803 is_circular() const
2804 { return this->is_circular_; }
2806 // Return the base type for this type.
2807 Type*
2808 named_base();
2810 const Type*
2811 named_base() const;
2813 // Return whether this is an error type.
2814 bool
2815 is_named_error_type() const;
2817 // Return whether this type is comparable. If REASON is not NULL,
2818 // set *REASON when returning false.
2819 bool
2820 named_type_is_comparable(std::string* reason) const;
2822 // Add a method to this type.
2823 Named_object*
2824 add_method(const std::string& name, Function*);
2826 // Add a method declaration to this type.
2827 Named_object*
2828 add_method_declaration(const std::string& name, Package* package,
2829 Function_type* type, Location location);
2831 // Add an existing method--one defined before the type itself was
2832 // defined--to a type.
2833 void
2834 add_existing_method(Named_object*);
2836 // Look up a local method.
2837 Named_object*
2838 find_local_method(const std::string& name) const;
2840 // Return the list of local methods.
2841 const Bindings*
2842 local_methods() const
2843 { return this->local_methods_; }
2845 // Build the complete list of methods, including those from
2846 // anonymous fields, and build method stubs if needed.
2847 void
2848 finalize_methods(Gogo*);
2850 // Return whether this type has any methods. This should only be
2851 // called after the finalize_methods pass.
2852 bool
2853 has_any_methods() const
2854 { return this->all_methods_ != NULL; }
2856 // Return the methods for this type. This should only be called
2857 // after the finalized_methods pass.
2858 const Methods*
2859 methods() const
2860 { return this->all_methods_; }
2862 // Return the method to use for NAME. This returns NULL if there is
2863 // no such method or if the method is ambiguous. When it returns
2864 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2865 Method*
2866 method_function(const std::string& name, bool *is_ambiguous) const;
2868 // Return whether NAME is a known field or method which is not
2869 // exported. This is only used for better error reporting.
2870 bool
2871 is_unexported_local_method(Gogo*, const std::string& name) const;
2873 // Return a pointer to the interface method table for this type for
2874 // the interface INTERFACE. If IS_POINTER is true, set the type
2875 // descriptor to a pointer to this type, otherwise set it to this
2876 // type.
2877 tree
2878 interface_method_table(Gogo*, const Interface_type* interface,
2879 bool is_pointer);
2881 // Whether this type has any hidden fields.
2882 bool
2883 named_type_has_hidden_fields(std::string* reason) const;
2885 // Note that a type must be converted to the backend representation
2886 // before we convert this type.
2887 void
2888 add_dependency(Named_type* nt)
2889 { this->dependencies_.push_back(nt); }
2891 // Return true if the size and alignment of the backend
2892 // representation of this type is known. This is always true after
2893 // types have been converted, but may be false beforehand.
2894 bool
2895 is_named_backend_type_size_known() const
2896 { return this->named_btype_ != NULL && !this->is_placeholder_; }
2898 // Export the type.
2899 void
2900 export_named_type(Export*, const std::string& name) const;
2902 // Import a named type.
2903 static void
2904 import_named_type(Import*, Named_type**);
2906 // Initial conversion to backend representation.
2907 void
2908 convert(Gogo*);
2910 protected:
2912 do_traverse(Traverse* traverse)
2913 { return Type::traverse(this->type_, traverse); }
2915 bool
2916 do_verify();
2918 bool
2919 do_has_pointer() const;
2921 bool
2922 do_compare_is_identity(Gogo*);
2924 unsigned int
2925 do_hash_for_method(Gogo*) const;
2927 Btype*
2928 do_get_backend(Gogo*);
2930 Expression*
2931 do_type_descriptor(Gogo*, Named_type*);
2933 void
2934 do_reflection(Gogo*, std::string*) const;
2936 void
2937 do_mangled_name(Gogo*, std::string* ret) const;
2939 void
2940 do_export(Export*) const;
2942 private:
2943 // Create the placeholder during conversion.
2944 void
2945 create_placeholder(Gogo*);
2947 // A pointer back to the Named_object for this type.
2948 Named_object* named_object_;
2949 // If this type is defined in a function, a pointer back to the
2950 // function in which it is defined.
2951 Named_object* in_function_;
2952 // The index of this type in IN_FUNCTION_.
2953 unsigned int in_function_index_;
2954 // The actual type.
2955 Type* type_;
2956 // The list of methods defined for this type. Any named type can
2957 // have methods.
2958 Bindings* local_methods_;
2959 // The full list of methods for this type, including methods
2960 // declared for anonymous fields.
2961 Methods* all_methods_;
2962 // A mapping from interfaces to the associated interface method
2963 // tables for this type.
2964 Interface_method_tables* interface_method_tables_;
2965 // A mapping from interfaces to the associated interface method
2966 // tables for pointers to this type.
2967 Interface_method_tables* pointer_interface_method_tables_;
2968 // The location where this type was defined.
2969 Location location_;
2970 // The backend representation of this type during backend
2971 // conversion. This is used to avoid endless recursion when a named
2972 // type refers to itself.
2973 Btype* named_btype_;
2974 // A list of types which must be converted to the backend
2975 // representation before this type can be converted. This is for
2976 // cases like
2977 // type S1 { p *S2 }
2978 // type S2 { s S1 }
2979 // where we can't convert S2 to the backend representation unless we
2980 // have converted S1.
2981 std::vector<Named_type*> dependencies_;
2982 // Whether this type is visible. This is false if this type was
2983 // created because it was referenced by an imported object, but the
2984 // type itself was not exported. This will always be true for types
2985 // created in the current package.
2986 bool is_visible_;
2987 // Whether this type is erroneous.
2988 bool is_error_;
2989 // Whether the current value of named_btype_ is a placeholder for
2990 // which the final size of the type is not known.
2991 bool is_placeholder_;
2992 // Whether this type has been converted to the backend
2993 // representation. Implies that is_placeholder_ is false.
2994 bool is_converted_;
2995 // Whether this is a pointer or function type which refers to the
2996 // type itself.
2997 bool is_circular_;
2998 // Whether this type has been verified.
2999 bool is_verified_;
3000 // In a recursive operation such as has_hidden_fields, this flag is
3001 // used to prevent infinite recursion when a type refers to itself.
3002 // This is mutable because it is always reset to false when the
3003 // function exits.
3004 mutable bool seen_;
3005 // Like seen_, but used only by do_compare_is_identity.
3006 bool seen_in_compare_is_identity_;
3007 // Like seen_, but used only by do_get_backend.
3008 bool seen_in_get_backend_;
3011 // A forward declaration. This handles a type which has been declared
3012 // but not defined.
3014 class Forward_declaration_type : public Type
3016 public:
3017 Forward_declaration_type(Named_object* named_object);
3019 // The named object associated with this type declaration. This
3020 // will be resolved.
3021 Named_object*
3022 named_object();
3024 const Named_object*
3025 named_object() const;
3027 // Return the name of the type.
3028 const std::string&
3029 name() const;
3031 // Return the type to which this points. Give an error if the type
3032 // has not yet been defined.
3033 Type*
3034 real_type();
3036 const Type*
3037 real_type() const;
3039 // Whether the base type has been defined.
3040 bool
3041 is_defined() const;
3043 // Add a method to this type.
3044 Named_object*
3045 add_method(const std::string& name, Function*);
3047 // Add a method declaration to this type.
3048 Named_object*
3049 add_method_declaration(const std::string& name, Package*, Function_type*,
3050 Location);
3052 protected:
3054 do_traverse(Traverse* traverse);
3056 bool
3057 do_verify();
3059 bool
3060 do_has_pointer() const
3061 { return this->real_type()->has_pointer(); }
3063 bool
3064 do_compare_is_identity(Gogo* gogo)
3065 { return this->real_type()->compare_is_identity(gogo); }
3067 unsigned int
3068 do_hash_for_method(Gogo* gogo) const
3069 { return this->real_type()->hash_for_method(gogo); }
3071 Btype*
3072 do_get_backend(Gogo* gogo);
3074 Expression*
3075 do_type_descriptor(Gogo*, Named_type*);
3077 void
3078 do_reflection(Gogo*, std::string*) const;
3080 void
3081 do_mangled_name(Gogo*, std::string* ret) const;
3083 void
3084 do_export(Export*) const;
3086 private:
3087 // Issue a warning about a use of an undefined type.
3088 void
3089 warn() const;
3091 // The type declaration.
3092 Named_object* named_object_;
3093 // Whether we have issued a warning about this type.
3094 mutable bool warned_;
3097 // The Type_context struct describes what we expect for the type of an
3098 // expression.
3100 struct Type_context
3102 // The exact type we expect, if known. This may be NULL.
3103 Type* type;
3104 // Whether an abstract type is permitted.
3105 bool may_be_abstract;
3107 // Constructors.
3108 Type_context()
3109 : type(NULL), may_be_abstract(false)
3112 Type_context(Type* a_type, bool a_may_be_abstract)
3113 : type(a_type), may_be_abstract(a_may_be_abstract)
3117 #endif // !defined(GO_TYPES_H)