compiler: Use backend interface for expressions.
[official-gcc.git] / gcc / go / gofrontend / types.h
blob09e215578f8e2f38f3a5654235bfbc4c060df22c
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 Backend_function_type;
23 class Struct_field;
24 class Struct_field_list;
25 class Struct_type;
26 class Pointer_type;
27 class Array_type;
28 class Map_type;
29 class Channel_type;
30 class Interface_type;
31 class Named_type;
32 class Forward_declaration_type;
33 class Method;
34 class Methods;
35 class Type_hash_identical;
36 class Type_identical;
37 class Expression;
38 class Expression_list;
39 class Call_expression;
40 class Field_reference_expression;
41 class Bound_method_expression;
42 class Bindings;
43 class Named_object;
44 class Function;
45 class Translate_context;
46 class Export;
47 class Import;
48 class Btype;
49 class Bexpression;
50 class Bvariable;
52 // Type codes used in type descriptors. These must match the values
53 // in libgo/runtime/go-type.h. They also match the values in the gc
54 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
55 // although this is not required.
57 static const int RUNTIME_TYPE_KIND_BOOL = 1;
58 static const int RUNTIME_TYPE_KIND_INT = 2;
59 static const int RUNTIME_TYPE_KIND_INT8 = 3;
60 static const int RUNTIME_TYPE_KIND_INT16 = 4;
61 static const int RUNTIME_TYPE_KIND_INT32 = 5;
62 static const int RUNTIME_TYPE_KIND_INT64 = 6;
63 static const int RUNTIME_TYPE_KIND_UINT = 7;
64 static const int RUNTIME_TYPE_KIND_UINT8 = 8;
65 static const int RUNTIME_TYPE_KIND_UINT16 = 9;
66 static const int RUNTIME_TYPE_KIND_UINT32 = 10;
67 static const int RUNTIME_TYPE_KIND_UINT64 = 11;
68 static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
69 static const int RUNTIME_TYPE_KIND_FLOAT32 = 13;
70 static const int RUNTIME_TYPE_KIND_FLOAT64 = 14;
71 static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15;
72 static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16;
73 static const int RUNTIME_TYPE_KIND_ARRAY = 17;
74 static const int RUNTIME_TYPE_KIND_CHAN = 18;
75 static const int RUNTIME_TYPE_KIND_FUNC = 19;
76 static const int RUNTIME_TYPE_KIND_INTERFACE = 20;
77 static const int RUNTIME_TYPE_KIND_MAP = 21;
78 static const int RUNTIME_TYPE_KIND_PTR = 22;
79 static const int RUNTIME_TYPE_KIND_SLICE = 23;
80 static const int RUNTIME_TYPE_KIND_STRING = 24;
81 static const int RUNTIME_TYPE_KIND_STRUCT = 25;
82 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26;
84 static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7);
86 // To build the complete list of methods for a named type we need to
87 // gather all methods from anonymous fields. Those methods may
88 // require an arbitrary set of indirections and field offsets. There
89 // is also the possibility of ambiguous methods, which we could ignore
90 // except that we want to give a better error message for that case.
91 // This is a base class. There are two types of methods: named
92 // methods, and methods which are inherited from an anonymous field of
93 // interface type.
95 class Method
97 public:
98 // For methods in anonymous types we need to know the sequence of
99 // field references used to extract the pointer to pass to the
100 // method. Since each method for a particular anonymous field will
101 // have the sequence of field indexes, and since the indexes can be
102 // shared going down the chain, we use a manually managed linked
103 // list. The first entry in the list is the field index for the
104 // last field, the one passed to the method.
106 struct Field_indexes
108 const Field_indexes* next;
109 unsigned int field_index;
112 virtual ~Method()
115 // Get the list of field indexes.
116 const Field_indexes*
117 field_indexes() const
118 { return this->field_indexes_; }
120 // Get the depth.
121 unsigned int
122 depth() const
123 { return this->depth_; }
125 // Return whether this is a value method--a method which does not
126 // require a pointer expression.
127 bool
128 is_value_method() const
129 { return this->is_value_method_; }
131 // Return whether we need a stub method--this is true if we can't
132 // just pass the main object to the method.
133 bool
134 needs_stub_method() const
135 { return this->needs_stub_method_; }
137 // Return whether this is an ambiguous method name.
138 bool
139 is_ambiguous() const
140 { return this->is_ambiguous_; }
142 // Note that this method is ambiguous.
143 void
144 set_is_ambiguous()
145 { this->is_ambiguous_ = true; }
147 // Return the type of the method.
148 Function_type*
149 type() const
150 { return this->do_type(); }
152 // Return the location of the method receiver.
153 Location
154 receiver_location() const
155 { return this->do_receiver_location(); }
157 // Return an expression which binds this method to EXPR. This is
158 // something which can be used with a function call.
159 Expression*
160 bind_method(Expression* expr, Location location) const;
162 // Return the named object for this method. This may only be called
163 // after methods are finalized.
164 Named_object*
165 named_object() const;
167 // Get the stub object.
168 Named_object*
169 stub_object() const
171 go_assert(this->stub_ != NULL);
172 return this->stub_;
175 // Set the stub object.
176 void
177 set_stub_object(Named_object* no)
179 go_assert(this->stub_ == NULL);
180 this->stub_ = no;
183 // Return true if this method should not participate in any
184 // interfaces.
185 bool
186 nointerface() const
187 { return this->do_nointerface(); }
189 protected:
190 // These objects are only built by the child classes.
191 Method(const Field_indexes* field_indexes, unsigned int depth,
192 bool is_value_method, bool needs_stub_method)
193 : field_indexes_(field_indexes), depth_(depth), stub_(NULL),
194 is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
195 is_ambiguous_(false)
198 // The named object for this method.
199 virtual Named_object*
200 do_named_object() const = 0;
202 // The type of the method.
203 virtual Function_type*
204 do_type() const = 0;
206 // Return the location of the method receiver.
207 virtual Location
208 do_receiver_location() const = 0;
210 // Bind a method to an object.
211 virtual Expression*
212 do_bind_method(Expression* expr, Location location) const = 0;
214 // Return whether this method should not participate in interfaces.
215 virtual bool
216 do_nointerface() const = 0;
218 private:
219 // The sequence of field indexes used for this method. If this is
220 // NULL, then the method is defined for the current type.
221 const Field_indexes* field_indexes_;
222 // The depth at which this method was found.
223 unsigned int depth_;
224 // If a stub method is required, this is its object. This is only
225 // set after stub methods are built in finalize_methods.
226 Named_object* stub_;
227 // Whether this is a value method--a method that does not require a
228 // pointer.
229 bool is_value_method_;
230 // Whether a stub method is required.
231 bool needs_stub_method_;
232 // Whether this method is ambiguous.
233 bool is_ambiguous_;
236 // A named method. This is what you get with a method declaration,
237 // either directly on the type, or inherited from some anonymous
238 // embedded field.
240 class Named_method : public Method
242 public:
243 Named_method(Named_object* named_object, const Field_indexes* field_indexes,
244 unsigned int depth, bool is_value_method,
245 bool needs_stub_method)
246 : Method(field_indexes, depth, is_value_method, needs_stub_method),
247 named_object_(named_object)
250 protected:
251 // Get the Named_object for the method.
252 Named_object*
253 do_named_object() const
254 { return this->named_object_; }
256 // The type of the method.
257 Function_type*
258 do_type() const;
260 // Return the location of the method receiver.
261 Location
262 do_receiver_location() const;
264 // Bind a method to an object.
265 Expression*
266 do_bind_method(Expression* expr, Location location) const;
268 // Return whether this method should not participate in interfaces.
269 bool
270 do_nointerface() const;
272 private:
273 // The method itself. For a method which needs a stub, this starts
274 // out as the underlying method, and is later replaced with the stub
275 // method.
276 Named_object* named_object_;
279 // An interface method. This is used when an interface appears as an
280 // anonymous field in a named struct.
282 class Interface_method : public Method
284 public:
285 Interface_method(const std::string& name, Location location,
286 Function_type* fntype, const Field_indexes* field_indexes,
287 unsigned int depth)
288 : Method(field_indexes, depth, true, true),
289 name_(name), location_(location), fntype_(fntype)
292 protected:
293 // Get the Named_object for the method. This should never be
294 // called, as we always create a stub.
295 Named_object*
296 do_named_object() const
297 { go_unreachable(); }
299 // The type of the method.
300 Function_type*
301 do_type() const
302 { return this->fntype_; }
304 // Return the location of the method receiver.
305 Location
306 do_receiver_location() const
307 { return this->location_; }
309 // Bind a method to an object.
310 Expression*
311 do_bind_method(Expression* expr, Location location) const;
313 // Return whether this method should not participate in interfaces.
314 bool
315 do_nointerface() const
316 { return false; }
318 private:
319 // The name of the interface method to call.
320 std::string name_;
321 // The location of the definition of the interface method.
322 Location location_;
323 // The type of the interface method.
324 Function_type* fntype_;
327 // A mapping from method name to Method. This is a wrapper around a
328 // hash table.
330 class Methods
332 private:
333 typedef Unordered_map(std::string, Method*) Method_map;
335 public:
336 typedef Method_map::const_iterator const_iterator;
338 Methods()
339 : methods_()
342 // Insert a new method. Returns true if it was inserted, false if
343 // it was overidden or ambiguous.
344 bool
345 insert(const std::string& name, Method* m);
347 // The number of (unambiguous) methods.
348 size_t
349 count() const;
351 // Iterate.
352 const_iterator
353 begin() const
354 { return this->methods_.begin(); }
356 const_iterator
357 end() const
358 { return this->methods_.end(); }
360 // Lookup.
361 const_iterator
362 find(const std::string& name) const
363 { return this->methods_.find(name); }
365 private:
366 Method_map methods_;
369 // The base class for all types.
371 class Type
373 public:
374 // The types of types.
375 enum Type_classification
377 TYPE_ERROR,
378 TYPE_VOID,
379 TYPE_BOOLEAN,
380 TYPE_INTEGER,
381 TYPE_FLOAT,
382 TYPE_COMPLEX,
383 TYPE_STRING,
384 TYPE_SINK,
385 TYPE_FUNCTION,
386 TYPE_POINTER,
387 TYPE_NIL,
388 TYPE_CALL_MULTIPLE_RESULT,
389 TYPE_STRUCT,
390 TYPE_ARRAY,
391 TYPE_MAP,
392 TYPE_CHANNEL,
393 TYPE_INTERFACE,
394 TYPE_NAMED,
395 TYPE_FORWARD
398 virtual ~Type();
400 // Creators.
402 static Type*
403 make_error_type();
405 static Type*
406 make_void_type();
408 // Get the unnamed bool type.
409 static Type*
410 make_boolean_type();
412 // Get the named type "bool".
413 static Named_type*
414 lookup_bool_type();
416 // Make the named type "bool".
417 static Named_type*
418 make_named_bool_type();
420 // Make an abstract integer type.
421 static Integer_type*
422 make_abstract_integer_type();
424 // Make an abstract type for a character constant.
425 static Integer_type*
426 make_abstract_character_type();
428 // Make a named integer type with a specified size.
429 // RUNTIME_TYPE_KIND is the code to use in reflection information,
430 // to distinguish int and int32.
431 static Named_type*
432 make_integer_type(const char* name, bool is_unsigned, int bits,
433 int runtime_type_kind);
435 // Look up a named integer type.
436 static Named_type*
437 lookup_integer_type(const char* name);
439 // Make an abstract floating point type.
440 static Float_type*
441 make_abstract_float_type();
443 // Make a named floating point type with a specific size.
444 // RUNTIME_TYPE_KIND is the code to use in reflection information,
445 // to distinguish float and float32.
446 static Named_type*
447 make_float_type(const char* name, int bits, int runtime_type_kind);
449 // Look up a named float type.
450 static Named_type*
451 lookup_float_type(const char* name);
453 // Make an abstract complex type.
454 static Complex_type*
455 make_abstract_complex_type();
457 // Make a named complex type with a specific size.
458 // RUNTIME_TYPE_KIND is the code to use in reflection information,
459 // to distinguish complex and complex64.
460 static Named_type*
461 make_complex_type(const char* name, int bits, int runtime_type_kind);
463 // Look up a named complex type.
464 static Named_type*
465 lookup_complex_type(const char* name);
467 // Get the unnamed string type.
468 static Type*
469 make_string_type();
471 // Get the named type "string".
472 static Named_type*
473 lookup_string_type();
475 // Make the named type "string".
476 static Named_type*
477 make_named_string_type();
479 static Type*
480 make_sink_type();
482 static Function_type*
483 make_function_type(Typed_identifier* receiver,
484 Typed_identifier_list* parameters,
485 Typed_identifier_list* results,
486 Location);
488 static Backend_function_type*
489 make_backend_function_type(Typed_identifier* receiver,
490 Typed_identifier_list* parameters,
491 Typed_identifier_list* results,
492 Location);
494 static Pointer_type*
495 make_pointer_type(Type*);
497 static Type*
498 make_nil_type();
500 static Type*
501 make_call_multiple_result_type(Call_expression*);
503 static Struct_type*
504 make_struct_type(Struct_field_list* fields, Location);
506 static Array_type*
507 make_array_type(Type* element_type, Expression* length);
509 static Map_type*
510 make_map_type(Type* key_type, Type* value_type, Location);
512 static Channel_type*
513 make_channel_type(bool send, bool receive, Type*);
515 static Interface_type*
516 make_interface_type(Typed_identifier_list* methods, Location);
518 static Interface_type*
519 make_empty_interface_type(Location);
521 static Type*
522 make_type_descriptor_type();
524 static Type*
525 make_type_descriptor_ptr_type();
527 static Named_type*
528 make_named_type(Named_object*, Type*, Location);
530 static Type*
531 make_forward_declaration(Named_object*);
533 // Make a builtin struct type from a list of fields.
534 static Struct_type*
535 make_builtin_struct_type(int nfields, ...);
537 // Make a builtin named type.
538 static Named_type*
539 make_builtin_named_type(const char* name, Type* type);
541 // Traverse a type.
542 static int
543 traverse(Type*, Traverse*);
545 // Verify the type. This is called after parsing, and verifies that
546 // types are complete and meet the language requirements. This
547 // returns false if the type is invalid and we should not continue
548 // traversing it.
549 bool
550 verify()
551 { return this->do_verify(); }
553 // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
554 // returns that an erroneous type is identical to any other type;
555 // this is used to avoid cascading errors. If this returns false,
556 // and REASON is not NULL, it may set *REASON.
557 static bool
558 are_identical(const Type* lhs, const Type* rhs, bool errors_are_identical,
559 std::string* reason);
561 // Return true if two types are compatible for use in a binary
562 // operation, other than a shift, comparison, or channel send. This
563 // is an equivalence relation.
564 static bool
565 are_compatible_for_binop(const Type* t1, const Type* t2);
567 // Return true if two types are compatible for use with the
568 // comparison operator. IS_EQUALITY_OP is true if this is an
569 // equality comparison, false if it is an ordered comparison. This
570 // is an equivalence relation. If this returns false, and REASON is
571 // not NULL, it sets *REASON.
572 static bool
573 are_compatible_for_comparison(bool is_equality_op, const Type *t1,
574 const Type *t2, std::string* reason);
576 // Return true if a type is comparable with itself. This is true of
577 // most types, but false for, e.g., function types.
578 bool
579 is_comparable() const
580 { return Type::are_compatible_for_comparison(true, this, this, NULL); }
582 // Return true if a value with type RHS is assignable to a variable
583 // with type LHS. This is not an equivalence relation. If this
584 // returns false, and REASON is not NULL, it sets *REASON.
585 static bool
586 are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
588 // Return true if a value with type RHS is assignable to a variable
589 // with type LHS, ignoring any assignment of hidden fields
590 // (unexported fields of a type imported from another package).
591 // This is like the are_assignable method.
592 static bool
593 are_assignable_hidden_ok(const Type* lhs, const Type* rhs,
594 std::string* reason);
596 // Return true if a value with type RHS may be converted to type
597 // LHS. If this returns false, and REASON is not NULL, it sets
598 // *REASON.
599 static bool
600 are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
602 // Whether this type has any hidden fields which are not visible in
603 // the current compilation, such as a field whose name begins with a
604 // lower case letter in a struct imported from a different package.
605 // WITHIN is not NULL if we are looking at fields in a named type.
606 bool
607 has_hidden_fields(const Named_type* within, std::string* reason) const;
609 // Return true if values of this type can be compared using an
610 // identity function which gets nothing but a pointer to the value
611 // and a size.
612 bool
613 compare_is_identity(Gogo* gogo)
614 { return this->do_compare_is_identity(gogo); }
616 // Return a hash code for this type for the method hash table.
617 // Types which are equivalent according to are_identical will have
618 // the same hash code.
619 unsigned int
620 hash_for_method(Gogo*) const;
622 // Return the type classification.
623 Type_classification
624 classification() const
625 { return this->classification_; }
627 // Return the base type for this type. This looks through forward
628 // declarations and names. Using this with a forward declaration
629 // which has not been defined will return an error type.
630 Type*
631 base();
633 const Type*
634 base() const;
636 // Return the type skipping defined forward declarations. If this
637 // type is a forward declaration which has not been defined, it will
638 // return the Forward_declaration_type. This differs from base() in
639 // that it will return a Named_type, and for a
640 // Forward_declaration_type which is not defined it will return that
641 // type rather than an error type.
642 Type*
643 forwarded();
645 const Type*
646 forwarded() const;
648 // Return true if this is a basic type: a type which is not composed
649 // of other types, and is not void.
650 bool
651 is_basic_type() const;
653 // Return true if this is an abstract type--an integer, floating
654 // point, or complex type whose size has not been determined.
655 bool
656 is_abstract() const;
658 // Return a non-abstract version of an abstract type.
659 Type*
660 make_non_abstract_type();
662 // Return true if this type is or contains a pointer. This
663 // determines whether the garbage collector needs to look at a value
664 // of this type.
665 bool
666 has_pointer() const
667 { return this->do_has_pointer(); }
669 // Return true if this is the error type. This returns false for a
670 // type which is not defined, as it is called by the parser before
671 // all types are defined.
672 bool
673 is_error_type() const;
675 // Return true if this is the error type or if the type is
676 // undefined. If the type is undefined, this will give an error.
677 // This should only be called after parsing is complete.
678 bool
679 is_error() const
680 { return this->base()->is_error_type(); }
682 // Return true if this is a void type.
683 bool
684 is_void_type() const
685 { return this->classification_ == TYPE_VOID; }
687 // If this is an integer type, return the Integer_type. Otherwise,
688 // return NULL. This is a controlled dynamic_cast.
689 Integer_type*
690 integer_type()
691 { return this->convert<Integer_type, TYPE_INTEGER>(); }
693 const Integer_type*
694 integer_type() const
695 { return this->convert<const Integer_type, TYPE_INTEGER>(); }
697 // If this is a floating point type, return the Float_type.
698 // Otherwise, return NULL. This is a controlled dynamic_cast.
699 Float_type*
700 float_type()
701 { return this->convert<Float_type, TYPE_FLOAT>(); }
703 const Float_type*
704 float_type() const
705 { return this->convert<const Float_type, TYPE_FLOAT>(); }
707 // If this is a complex type, return the Complex_type. Otherwise,
708 // return NULL.
709 Complex_type*
710 complex_type()
711 { return this->convert<Complex_type, TYPE_COMPLEX>(); }
713 const Complex_type*
714 complex_type() const
715 { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
717 // Return whether this is a numeric type.
718 bool
719 is_numeric_type() const
721 Type_classification tc = this->base()->classification_;
722 return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX;
725 // Return true if this is a boolean type.
726 bool
727 is_boolean_type() const
728 { return this->base()->classification_ == TYPE_BOOLEAN; }
730 // Return true if this is an abstract boolean type.
731 bool
732 is_abstract_boolean_type() const
733 { return this->classification_ == TYPE_BOOLEAN; }
735 // Return true if this is a string type.
736 bool
737 is_string_type() const
738 { return this->base()->classification_ == TYPE_STRING; }
740 // Return true if this is an abstract string type.
741 bool
742 is_abstract_string_type() const
743 { return this->classification_ == TYPE_STRING; }
745 // Return true if this is the sink type. This is the type of the
746 // blank identifier _.
747 bool
748 is_sink_type() const
749 { return this->base()->classification_ == TYPE_SINK; }
751 // If this is a function type, return it. Otherwise, return NULL.
752 Function_type*
753 function_type()
754 { return this->convert<Function_type, TYPE_FUNCTION>(); }
756 const Function_type*
757 function_type() const
758 { return this->convert<const Function_type, TYPE_FUNCTION>(); }
760 // If this is a pointer type, return the type to which it points.
761 // Otherwise, return NULL.
762 Type*
763 points_to() const;
765 // If this is a pointer type, return the type to which it points.
766 // Otherwise, return the type itself.
767 Type*
768 deref()
770 Type* pt = this->points_to();
771 return pt != NULL ? pt : this;
774 const Type*
775 deref() const
777 const Type* pt = this->points_to();
778 return pt != NULL ? pt : this;
781 // Return true if this is the nil type. We don't use base() here,
782 // because this can be called during parse, and there is no way to
783 // name the nil type anyhow.
784 bool
785 is_nil_type() const
786 { return this->classification_ == TYPE_NIL; }
788 // Return true if this is the predeclared constant nil being used as
789 // a type. This is what the parser produces for type switches which
790 // use "case nil".
791 bool
792 is_nil_constant_as_type() const;
794 // Return true if this is the return type of a function which
795 // returns multiple values.
796 bool
797 is_call_multiple_result_type() const
798 { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
800 // If this is a struct type, return it. Otherwise, return NULL.
801 Struct_type*
802 struct_type()
803 { return this->convert<Struct_type, TYPE_STRUCT>(); }
805 const Struct_type*
806 struct_type() const
807 { return this->convert<const Struct_type, TYPE_STRUCT>(); }
809 // If this is an array type, return it. Otherwise, return NULL.
810 Array_type*
811 array_type()
812 { return this->convert<Array_type, TYPE_ARRAY>(); }
814 const Array_type*
815 array_type() const
816 { return this->convert<const Array_type, TYPE_ARRAY>(); }
818 // Return whether if this is a slice type.
819 bool
820 is_slice_type() const;
822 // If this is a map type, return it. Otherwise, return NULL.
823 Map_type*
824 map_type()
825 { return this->convert<Map_type, TYPE_MAP>(); }
827 const Map_type*
828 map_type() const
829 { return this->convert<const Map_type, TYPE_MAP>(); }
831 // If this is a channel type, return it. Otherwise, return NULL.
832 Channel_type*
833 channel_type()
834 { return this->convert<Channel_type, TYPE_CHANNEL>(); }
836 const Channel_type*
837 channel_type() const
838 { return this->convert<const Channel_type, TYPE_CHANNEL>(); }
840 // If this is an interface type, return it. Otherwise, return NULL.
841 Interface_type*
842 interface_type()
843 { return this->convert<Interface_type, TYPE_INTERFACE>(); }
845 const Interface_type*
846 interface_type() const
847 { return this->convert<const Interface_type, TYPE_INTERFACE>(); }
849 // If this is a named type, return it. Otherwise, return NULL.
850 Named_type*
851 named_type();
853 const Named_type*
854 named_type() const;
856 // If this is a forward declaration, return it. Otherwise, return
857 // NULL.
858 Forward_declaration_type*
859 forward_declaration_type()
860 { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
862 const Forward_declaration_type*
863 forward_declaration_type() const
865 return this->convert_no_base<const Forward_declaration_type,
866 TYPE_FORWARD>();
869 // Return true if this type is not yet defined.
870 bool
871 is_undefined() const;
873 // Return true if this is the unsafe.pointer type. We currently
874 // represent that as pointer-to-void.
875 bool
876 is_unsafe_pointer_type() const
877 { return this->points_to() != NULL && this->points_to()->is_void_type(); }
879 // Look for field or method NAME for TYPE. Return an expression for
880 // it, bound to EXPR.
881 static Expression*
882 bind_field_or_method(Gogo*, const Type* type, Expression* expr,
883 const std::string& name, Location);
885 // Return true if NAME is an unexported field or method of TYPE.
886 static bool
887 is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
888 std::vector<const Named_type*>*);
890 // Convert the builtin named types.
891 static void
892 convert_builtin_named_types(Gogo*);
894 // Return the backend representation of this type.
895 Btype*
896 get_backend(Gogo*);
898 // Return a placeholder for the backend representation of the type.
899 // This will return a type of the correct size, but for which some
900 // of the fields may still need to be completed.
901 Btype*
902 get_backend_placeholder(Gogo*);
904 // Finish the backend representation of a placeholder.
905 void
906 finish_backend(Gogo*, Btype*);
908 // Build a type descriptor entry for this type. Return a pointer to
909 // it. The location is the location which causes us to need the
910 // entry.
911 Bexpression*
912 type_descriptor_pointer(Gogo* gogo, Location);
914 // Return the type reflection string for this type.
915 std::string
916 reflection(Gogo*) const;
918 // Return a mangled name for the type. This is a name which can be
919 // used in assembler code. Identical types should have the same
920 // manged name.
921 std::string
922 mangled_name(Gogo*) const;
924 // If the size of the type can be determined, set *PSIZE to the size
925 // in bytes and return true. Otherwise, return false. This queries
926 // the backend.
927 bool
928 backend_type_size(Gogo*, unsigned int* psize);
930 // If the alignment of the type can be determined, set *PALIGN to
931 // the alignment in bytes and return true. Otherwise, return false.
932 bool
933 backend_type_align(Gogo*, unsigned int* palign);
935 // If the alignment of a struct field of this type can be
936 // determined, set *PALIGN to the alignment in bytes and return
937 // true. Otherwise, return false.
938 bool
939 backend_type_field_align(Gogo*, unsigned int* palign);
941 // Whether the backend size is known.
942 bool
943 is_backend_type_size_known(Gogo*);
945 // Get the hash and equality functions for a type.
946 void
947 type_functions(Gogo*, Named_type* name, Function_type* hash_fntype,
948 Function_type* equal_fntype, Named_object** hash_fn,
949 Named_object** equal_fn);
951 // Write the hash and equality type functions.
952 void
953 write_specific_type_functions(Gogo*, Named_type*,
954 const std::string& hash_name,
955 Function_type* hash_fntype,
956 const std::string& equal_name,
957 Function_type* equal_fntype);
959 // Export the type.
960 void
961 export_type(Export* exp) const
962 { this->do_export(exp); }
964 // Import a type.
965 static Type*
966 import_type(Import*);
968 protected:
969 Type(Type_classification);
971 // Functions implemented by the child class.
973 // Traverse the subtypes.
974 virtual int
975 do_traverse(Traverse*);
977 // Verify the type.
978 virtual bool
979 do_verify()
980 { return true; }
982 virtual bool
983 do_has_pointer() const
984 { return false; }
986 virtual bool
987 do_compare_is_identity(Gogo*) = 0;
989 virtual unsigned int
990 do_hash_for_method(Gogo*) const;
992 virtual Btype*
993 do_get_backend(Gogo*) = 0;
995 virtual Expression*
996 do_type_descriptor(Gogo*, Named_type* name) = 0;
998 virtual void
999 do_reflection(Gogo*, std::string*) const = 0;
1001 virtual void
1002 do_mangled_name(Gogo*, std::string*) const = 0;
1004 virtual void
1005 do_export(Export*) const;
1007 // Return whether a method expects a pointer as the receiver.
1008 static bool
1009 method_expects_pointer(const Named_object*);
1011 // Finalize the methods for a type.
1012 static void
1013 finalize_methods(Gogo*, const Type*, Location, Methods**);
1015 // Return a method from a set of methods.
1016 static Method*
1017 method_function(const Methods*, const std::string& name,
1018 bool* is_ambiguous);
1020 // A mapping from interfaces to the associated interface method
1021 // tables for this type. This maps to a decl.
1022 typedef Unordered_map_hash(Interface_type*, Expression*, Type_hash_identical,
1023 Type_identical) Interface_method_tables;
1025 // Return a pointer to the interface method table for TYPE for the
1026 // interface INTERFACE.
1027 static Expression*
1028 interface_method_table(Type* type,
1029 Interface_type *interface, bool is_pointer,
1030 Interface_method_tables** method_tables,
1031 Interface_method_tables** pointer_tables);
1033 // Return a composite literal for the type descriptor entry for a
1034 // type.
1035 static Expression*
1036 type_descriptor(Gogo*, Type*);
1038 // Return a composite literal for the type descriptor entry for
1039 // TYPE, using NAME as the name of the type.
1040 static Expression*
1041 named_type_descriptor(Gogo*, Type* type, Named_type* name);
1043 // Return a composite literal for a plain type descriptor for this
1044 // type with the given kind and name.
1045 Expression*
1046 plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
1048 // Build a composite literal for the basic type descriptor.
1049 Expression*
1050 type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
1051 const Methods*, bool only_value_methods);
1053 // For the benefit of child class reflection string generation.
1054 void
1055 append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
1056 { type->do_reflection(gogo, ret); }
1058 // For the benefit of child class mangling.
1059 void
1060 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
1061 { type->do_mangled_name(gogo, ret); }
1063 // Incorporate a string into a hash code.
1064 static unsigned int
1065 hash_string(const std::string&, unsigned int);
1067 // Return the backend representation for the underlying type of a
1068 // named type.
1069 static Btype*
1070 get_named_base_btype(Gogo* gogo, Type* base_type)
1071 { return base_type->get_btype_without_hash(gogo); }
1073 private:
1074 // Convert to the desired type classification, or return NULL. This
1075 // is a controlled dynamic_cast.
1076 template<typename Type_class, Type_classification type_classification>
1077 Type_class*
1078 convert()
1080 Type* base = this->base();
1081 return (base->classification_ == type_classification
1082 ? static_cast<Type_class*>(base)
1083 : NULL);
1086 template<typename Type_class, Type_classification type_classification>
1087 const Type_class*
1088 convert() const
1090 const Type* base = this->base();
1091 return (base->classification_ == type_classification
1092 ? static_cast<Type_class*>(base)
1093 : NULL);
1096 template<typename Type_class, Type_classification type_classification>
1097 Type_class*
1098 convert_no_base()
1100 return (this->classification_ == type_classification
1101 ? static_cast<Type_class*>(this)
1102 : NULL);
1105 template<typename Type_class, Type_classification type_classification>
1106 const Type_class*
1107 convert_no_base() const
1109 return (this->classification_ == type_classification
1110 ? static_cast<Type_class*>(this)
1111 : NULL);
1114 // Support for are_assignable and are_assignable_hidden_ok.
1115 static bool
1116 are_assignable_check_hidden(const Type* lhs, const Type* rhs,
1117 bool check_hidden_fields, std::string* reason);
1119 // Map unnamed types to type descriptor decls.
1120 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
1121 Type_identical) Type_descriptor_vars;
1123 static Type_descriptor_vars type_descriptor_vars;
1125 // Build the type descriptor variable for this type.
1126 void
1127 make_type_descriptor_var(Gogo*);
1129 // Return the name of the type descriptor variable. If NAME is not
1130 // NULL, it is the name to use.
1131 std::string
1132 type_descriptor_var_name(Gogo*, Named_type* name);
1134 // Return true if the type descriptor for this type should be
1135 // defined in some other package. If NAME is not NULL, it is the
1136 // name of this type. If this returns true it sets *PACKAGE to the
1137 // package where the type descriptor is defined.
1138 bool
1139 type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
1141 // Build the hash and equality type functions for a type which needs
1142 // specific functions.
1143 void
1144 specific_type_functions(Gogo*, Named_type*, Function_type* hash_fntype,
1145 Function_type* equal_fntype, Named_object** hash_fn,
1146 Named_object** equal_fn);
1148 void
1149 write_named_hash(Gogo*, Named_type*, Function_type* hash_fntype,
1150 Function_type* equal_fntype);
1152 void
1153 write_named_equal(Gogo*, Named_type*);
1155 // Build a composite literal for the uncommon type information.
1156 Expression*
1157 uncommon_type_constructor(Gogo*, Type* uncommon_type,
1158 Named_type*, const Methods*,
1159 bool only_value_methods) const;
1161 // Build a composite literal for the methods.
1162 Expression*
1163 methods_constructor(Gogo*, Type* methods_type, const Methods*,
1164 bool only_value_methods) const;
1166 // Build a composite literal for one method.
1167 Expression*
1168 method_constructor(Gogo*, Type* method_type, const std::string& name,
1169 const Method*, bool only_value_methods) const;
1171 // Add all methods for TYPE to the list of methods for THIS.
1172 static void
1173 add_methods_for_type(const Type* type, const Method::Field_indexes*,
1174 unsigned int depth, bool, bool,
1175 std::vector<const Named_type*>*,
1176 Methods**);
1178 static void
1179 add_local_methods_for_type(const Named_type* type,
1180 const Method::Field_indexes*,
1181 unsigned int depth, bool, bool, Methods**);
1183 static void
1184 add_embedded_methods_for_type(const Type* type,
1185 const Method::Field_indexes*,
1186 unsigned int depth, bool, bool,
1187 std::vector<const Named_type*>*,
1188 Methods**);
1190 static void
1191 add_interface_methods_for_type(const Type* type,
1192 const Method::Field_indexes*,
1193 unsigned int depth, Methods**);
1195 // Build stub methods for a type.
1196 static void
1197 build_stub_methods(Gogo*, const Type* type, const Methods* methods,
1198 Location);
1200 static void
1201 build_one_stub_method(Gogo*, Method*, const char* receiver_name,
1202 const Typed_identifier_list*, bool is_varargs,
1203 Location);
1205 static Expression*
1206 apply_field_indexes(Expression*, const Method::Field_indexes*,
1207 Location);
1209 // Look for a field or method named NAME in TYPE.
1210 static bool
1211 find_field_or_method(const Type* type, const std::string& name,
1212 bool receiver_can_be_pointer,
1213 std::vector<const Named_type*>*, int* level,
1214 bool* is_method, bool* found_pointer_method,
1215 std::string* ambig1, std::string* ambig2);
1217 // Get the backend representation for a type without looking in the
1218 // hash table for identical types.
1219 Btype*
1220 get_btype_without_hash(Gogo*);
1222 // A backend type that may be a placeholder.
1223 struct Type_btype_entry
1225 Btype *btype;
1226 bool is_placeholder;
1229 // A mapping from Type to Btype*, used to ensure that the backend
1230 // representation of identical types is identical. This is only
1231 // used for unnamed types.
1232 typedef Unordered_map_hash(const Type*, Type_btype_entry,
1233 Type_hash_identical, Type_identical) Type_btypes;
1235 static Type_btypes type_btypes;
1237 // A list of builtin named types.
1238 static std::vector<Named_type*> named_builtin_types;
1240 // A map from types which need specific type functions to the type
1241 // functions themselves.
1242 typedef std::pair<Named_object*, Named_object*> Hash_equal_fn;
1243 typedef Unordered_map_hash(const Type*, Hash_equal_fn, Type_hash_identical,
1244 Type_identical) Type_functions;
1246 static Type_functions type_functions_table;
1248 // The type classification.
1249 Type_classification classification_;
1250 // The backend representation of the type, once it has been
1251 // determined.
1252 Btype* btype_;
1253 // The type descriptor for this type. This starts out as NULL and
1254 // is filled in as needed.
1255 Bvariable* type_descriptor_var_;
1258 // Type hash table operations.
1260 class Type_hash_identical
1262 public:
1263 unsigned int
1264 operator()(const Type* type) const
1265 { return type->hash_for_method(NULL); }
1268 class Type_identical
1270 public:
1271 bool
1272 operator()(const Type* t1, const Type* t2) const
1273 { return Type::are_identical(t1, t2, false, NULL); }
1276 // An identifier with a type.
1278 class Typed_identifier
1280 public:
1281 Typed_identifier(const std::string& name, Type* type,
1282 Location location)
1283 : name_(name), type_(type), location_(location)
1286 // Get the name.
1287 const std::string&
1288 name() const
1289 { return this->name_; }
1291 // Get the type.
1292 Type*
1293 type() const
1294 { return this->type_; }
1296 // Return the location where the name was seen. This is not always
1297 // meaningful.
1298 Location
1299 location() const
1300 { return this->location_; }
1302 // Set the type--sometimes we see the identifier before the type.
1303 void
1304 set_type(Type* type)
1306 go_assert(this->type_ == NULL || type->is_error_type());
1307 this->type_ = type;
1310 private:
1311 // Identifier name.
1312 std::string name_;
1313 // Type.
1314 Type* type_;
1315 // The location where the name was seen.
1316 Location location_;
1319 // A list of Typed_identifiers.
1321 class Typed_identifier_list
1323 public:
1324 Typed_identifier_list()
1325 : entries_()
1328 // Whether the list is empty.
1329 bool
1330 empty() const
1331 { return this->entries_.empty(); }
1333 // Return the number of entries in the list.
1334 size_t
1335 size() const
1336 { return this->entries_.size(); }
1338 // Add an entry to the end of the list.
1339 void
1340 push_back(const Typed_identifier& td)
1341 { this->entries_.push_back(td); }
1343 // Remove an entry from the end of the list.
1344 void
1345 pop_back()
1346 { this->entries_.pop_back(); }
1348 // Set the type of entry I to TYPE.
1349 void
1350 set_type(size_t i, Type* type)
1352 go_assert(i < this->entries_.size());
1353 this->entries_[i].set_type(type);
1356 // Sort the entries by name.
1357 void
1358 sort_by_name();
1360 // Traverse types.
1362 traverse(Traverse*);
1364 // Return the first and last elements.
1365 Typed_identifier&
1366 front()
1367 { return this->entries_.front(); }
1369 const Typed_identifier&
1370 front() const
1371 { return this->entries_.front(); }
1373 Typed_identifier&
1374 back()
1375 { return this->entries_.back(); }
1377 const Typed_identifier&
1378 back() const
1379 { return this->entries_.back(); }
1381 const Typed_identifier&
1382 at(size_t i) const
1383 { return this->entries_.at(i); }
1385 void
1386 set(size_t i, const Typed_identifier& t)
1387 { this->entries_.at(i) = t; }
1389 void
1390 resize(size_t c)
1392 go_assert(c <= this->entries_.size());
1393 this->entries_.resize(c, Typed_identifier("", NULL,
1394 Linemap::unknown_location()));
1397 void
1398 reserve(size_t c)
1399 { this->entries_.reserve(c); }
1401 // Iterators.
1403 typedef std::vector<Typed_identifier>::iterator iterator;
1404 typedef std::vector<Typed_identifier>::const_iterator const_iterator;
1406 iterator
1407 begin()
1408 { return this->entries_.begin(); }
1410 const_iterator
1411 begin() const
1412 { return this->entries_.begin(); }
1414 iterator
1415 end()
1416 { return this->entries_.end(); }
1418 const_iterator
1419 end() const
1420 { return this->entries_.end(); }
1422 // Return a copy of this list. This returns an independent copy of
1423 // the vector, but does not copy the types.
1424 Typed_identifier_list*
1425 copy() const;
1427 private:
1428 std::vector<Typed_identifier> entries_;
1431 // The type of an integer.
1433 class Integer_type : public Type
1435 public:
1436 // Create a new integer type.
1437 static Named_type*
1438 create_integer_type(const char* name, bool is_unsigned, int bits,
1439 int runtime_type_kind);
1441 // Look up an existing integer type.
1442 static Named_type*
1443 lookup_integer_type(const char* name);
1445 // Create an abstract integer type.
1446 static Integer_type*
1447 create_abstract_integer_type();
1449 // Create an abstract character type.
1450 static Integer_type*
1451 create_abstract_character_type();
1453 // Whether this is an abstract integer type.
1454 bool
1455 is_abstract() const
1456 { return this->is_abstract_; }
1458 // Whether this is an unsigned type.
1459 bool
1460 is_unsigned() const
1461 { return this->is_unsigned_; }
1463 // The number of bits.
1465 bits() const
1466 { return this->bits_; }
1468 // Whether this type is the same as T.
1469 bool
1470 is_identical(const Integer_type* t) const;
1472 // Whether this is the type "byte" or another name for "byte".
1473 bool
1474 is_byte() const
1475 { return this->is_byte_; }
1477 // Mark this as the "byte" type.
1478 void
1479 set_is_byte()
1480 { this->is_byte_ = true; }
1482 // Whether this is the type "rune" or another name for "rune".
1483 bool
1484 is_rune() const
1485 { return this->is_rune_; }
1487 // Mark this as the "rune" type.
1488 void
1489 set_is_rune()
1490 { this->is_rune_ = true; }
1492 protected:
1493 bool
1494 do_compare_is_identity(Gogo*)
1495 { return true; }
1497 unsigned int
1498 do_hash_for_method(Gogo*) const;
1500 Btype*
1501 do_get_backend(Gogo*);
1503 Expression*
1504 do_type_descriptor(Gogo*, Named_type*);
1506 void
1507 do_reflection(Gogo*, std::string*) const;
1509 void
1510 do_mangled_name(Gogo*, std::string*) const;
1512 private:
1513 Integer_type(bool is_abstract, bool is_unsigned, int bits,
1514 int runtime_type_kind)
1515 : Type(TYPE_INTEGER),
1516 is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
1517 is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
1520 // Map names of integer types to the types themselves.
1521 typedef std::map<std::string, Named_type*> Named_integer_types;
1522 static Named_integer_types named_integer_types;
1524 // True if this is an abstract type.
1525 bool is_abstract_;
1526 // True if this is an unsigned type.
1527 bool is_unsigned_;
1528 // True if this is the byte type.
1529 bool is_byte_;
1530 // True if this is the rune type.
1531 bool is_rune_;
1532 // The number of bits.
1533 int bits_;
1534 // The runtime type code used in the type descriptor for this type.
1535 int runtime_type_kind_;
1538 // The type of a floating point number.
1540 class Float_type : public Type
1542 public:
1543 // Create a new float type.
1544 static Named_type*
1545 create_float_type(const char* name, int bits, int runtime_type_kind);
1547 // Look up an existing float type.
1548 static Named_type*
1549 lookup_float_type(const char* name);
1551 // Create an abstract float type.
1552 static Float_type*
1553 create_abstract_float_type();
1555 // Whether this is an abstract float type.
1556 bool
1557 is_abstract() const
1558 { return this->is_abstract_; }
1560 // The number of bits.
1562 bits() const
1563 { return this->bits_; }
1565 // Whether this type is the same as T.
1566 bool
1567 is_identical(const Float_type* t) const;
1569 protected:
1570 bool
1571 do_compare_is_identity(Gogo*)
1572 { return false; }
1574 unsigned int
1575 do_hash_for_method(Gogo*) const;
1577 Btype*
1578 do_get_backend(Gogo*);
1580 Expression*
1581 do_type_descriptor(Gogo*, Named_type*);
1583 void
1584 do_reflection(Gogo*, std::string*) const;
1586 void
1587 do_mangled_name(Gogo*, std::string*) const;
1589 private:
1590 Float_type(bool is_abstract, int bits, int runtime_type_kind)
1591 : Type(TYPE_FLOAT),
1592 is_abstract_(is_abstract), bits_(bits),
1593 runtime_type_kind_(runtime_type_kind)
1596 // Map names of float types to the types themselves.
1597 typedef std::map<std::string, Named_type*> Named_float_types;
1598 static Named_float_types named_float_types;
1600 // True if this is an abstract type.
1601 bool is_abstract_;
1602 // The number of bits in the floating point value.
1603 int bits_;
1604 // The runtime type code used in the type descriptor for this type.
1605 int runtime_type_kind_;
1608 // The type of a complex number.
1610 class Complex_type : public Type
1612 public:
1613 // Create a new complex type.
1614 static Named_type*
1615 create_complex_type(const char* name, int bits, int runtime_type_kind);
1617 // Look up an existing complex type.
1618 static Named_type*
1619 lookup_complex_type(const char* name);
1621 // Create an abstract complex type.
1622 static Complex_type*
1623 create_abstract_complex_type();
1625 // Whether this is an abstract complex type.
1626 bool
1627 is_abstract() const
1628 { return this->is_abstract_; }
1630 // The number of bits: 64 or 128.
1631 int bits() const
1632 { return this->bits_; }
1634 // Whether this type is the same as T.
1635 bool
1636 is_identical(const Complex_type* t) const;
1638 protected:
1639 bool
1640 do_compare_is_identity(Gogo*)
1641 { return false; }
1643 unsigned int
1644 do_hash_for_method(Gogo*) const;
1646 Btype*
1647 do_get_backend(Gogo*);
1649 Expression*
1650 do_type_descriptor(Gogo*, Named_type*);
1652 void
1653 do_reflection(Gogo*, std::string*) const;
1655 void
1656 do_mangled_name(Gogo*, std::string*) const;
1658 private:
1659 Complex_type(bool is_abstract, int bits, int runtime_type_kind)
1660 : Type(TYPE_COMPLEX),
1661 is_abstract_(is_abstract), bits_(bits),
1662 runtime_type_kind_(runtime_type_kind)
1665 // Map names of complex types to the types themselves.
1666 typedef std::map<std::string, Named_type*> Named_complex_types;
1667 static Named_complex_types named_complex_types;
1669 // True if this is an abstract type.
1670 bool is_abstract_;
1671 // The number of bits in the complex value--64 or 128.
1672 int bits_;
1673 // The runtime type code used in the type descriptor for this type.
1674 int runtime_type_kind_;
1677 // The type of a string.
1679 class String_type : public Type
1681 public:
1682 String_type()
1683 : Type(TYPE_STRING)
1686 protected:
1687 bool
1688 do_has_pointer() const
1689 { return true; }
1691 bool
1692 do_compare_is_identity(Gogo*)
1693 { return false; }
1695 Btype*
1696 do_get_backend(Gogo*);
1698 Expression*
1699 do_type_descriptor(Gogo*, Named_type*);
1701 void
1702 do_reflection(Gogo*, std::string*) const;
1704 void
1705 do_mangled_name(Gogo*, std::string* ret) const;
1707 private:
1708 // The named string type.
1709 static Named_type* string_type_;
1712 // The type of a function.
1714 class Function_type : public Type
1716 public:
1717 Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
1718 Typed_identifier_list* results, Location location)
1719 : Type(TYPE_FUNCTION),
1720 receiver_(receiver), parameters_(parameters), results_(results),
1721 location_(location), is_varargs_(false), is_builtin_(false),
1722 fnbtype_(NULL)
1725 // Get the receiver.
1726 const Typed_identifier*
1727 receiver() const
1728 { return this->receiver_; }
1730 // Get the return names and types.
1731 const Typed_identifier_list*
1732 results() const
1733 { return this->results_; }
1735 // Get the parameter names and types.
1736 const Typed_identifier_list*
1737 parameters() const
1738 { return this->parameters_; }
1740 // Whether this is a varargs function.
1741 bool
1742 is_varargs() const
1743 { return this->is_varargs_; }
1745 // Whether this is a builtin function.
1746 bool
1747 is_builtin() const
1748 { return this->is_builtin_; }
1750 // The location where this type was defined.
1751 Location
1752 location() const
1753 { return this->location_; }
1755 // Return whether this is a method type.
1756 bool
1757 is_method() const
1758 { return this->receiver_ != NULL; }
1760 // Whether T is a valid redeclaration of this type. This is called
1761 // when a function is declared more than once.
1762 bool
1763 is_valid_redeclaration(const Function_type* t, std::string*) const;
1765 // Whether this type is the same as T.
1766 bool
1767 is_identical(const Function_type* t, bool ignore_receiver,
1768 bool errors_are_identical, std::string*) const;
1770 // Record that this is a varargs function.
1771 void
1772 set_is_varargs()
1773 { this->is_varargs_ = true; }
1775 // Record that this is a builtin function.
1776 void
1777 set_is_builtin()
1778 { this->is_builtin_ = true; }
1780 // Import a function type.
1781 static Function_type*
1782 do_import(Import*);
1784 // Return a copy of this type without a receiver. This is only
1785 // valid for a method type.
1786 Function_type*
1787 copy_without_receiver() const;
1789 // Return a copy of this type with a receiver. This is used when an
1790 // interface method is attached to a named or struct type.
1791 Function_type*
1792 copy_with_receiver(Type*) const;
1794 // Return a copy of this type with the receiver treated as the first
1795 // parameter. If WANT_POINTER_RECEIVER is true, the receiver is
1796 // forced to be a pointer.
1797 Function_type*
1798 copy_with_receiver_as_param(bool want_pointer_receiver) const;
1800 // Return a copy of this type ignoring any receiver and using dummy
1801 // names for all parameters. This is used for thunks for method
1802 // values.
1803 Function_type*
1804 copy_with_names() const;
1806 static Type*
1807 make_function_type_descriptor_type();
1809 // Return the backend representation of this function type. This is used
1810 // as the real type of a backend function declaration or defintion.
1811 Btype*
1812 get_backend_fntype(Gogo*);
1814 protected:
1816 do_traverse(Traverse*);
1818 // A function descriptor may be allocated on the heap.
1819 bool
1820 do_has_pointer() const
1821 { return true; }
1823 bool
1824 do_compare_is_identity(Gogo*)
1825 { return false; }
1827 unsigned int
1828 do_hash_for_method(Gogo*) const;
1830 Btype*
1831 do_get_backend(Gogo*);
1833 Expression*
1834 do_type_descriptor(Gogo*, Named_type*);
1836 void
1837 do_reflection(Gogo*, std::string*) const;
1839 void
1840 do_mangled_name(Gogo*, std::string*) const;
1842 void
1843 do_export(Export*) const;
1845 private:
1846 Expression*
1847 type_descriptor_params(Type*, const Typed_identifier*,
1848 const Typed_identifier_list*);
1850 // A mapping from a list of result types to a backend struct type.
1851 class Results_hash
1853 public:
1854 unsigned int
1855 operator()(const Typed_identifier_list*) const;
1858 class Results_equal
1860 public:
1861 bool
1862 operator()(const Typed_identifier_list*,
1863 const Typed_identifier_list*) const;
1866 typedef Unordered_map_hash(Typed_identifier_list*, Btype*,
1867 Results_hash, Results_equal) Results_structs;
1869 static Results_structs results_structs;
1871 // The receiver name and type. This will be NULL for a normal
1872 // function, non-NULL for a method.
1873 Typed_identifier* receiver_;
1874 // The parameter names and types.
1875 Typed_identifier_list* parameters_;
1876 // The result names and types. This will be NULL if no result was
1877 // specified.
1878 Typed_identifier_list* results_;
1879 // The location where this type was defined. This exists solely to
1880 // give a location for the fields of the struct if this function
1881 // returns multiple values.
1882 Location location_;
1883 // Whether this function takes a variable number of arguments.
1884 bool is_varargs_;
1885 // Whether this is a special builtin function which can not simply
1886 // be called. This is used for len, cap, etc.
1887 bool is_builtin_;
1888 // The backend representation of this type for backend function
1889 // declarations and definitions.
1890 Btype* fnbtype_;
1893 // The type of a function's backend representation.
1895 class Backend_function_type : public Function_type
1897 public:
1898 Backend_function_type(Typed_identifier* receiver,
1899 Typed_identifier_list* parameters,
1900 Typed_identifier_list* results, Location location)
1901 : Function_type(receiver, parameters, results, location)
1904 protected:
1905 Btype*
1906 do_get_backend(Gogo* gogo)
1907 { return this->get_backend_fntype(gogo); }
1910 // The type of a pointer.
1912 class Pointer_type : public Type
1914 public:
1915 Pointer_type(Type* to_type)
1916 : Type(TYPE_POINTER),
1917 to_type_(to_type)
1920 Type*
1921 points_to() const
1922 { return this->to_type_; }
1924 // Import a pointer type.
1925 static Pointer_type*
1926 do_import(Import*);
1928 static Type*
1929 make_pointer_type_descriptor_type();
1931 protected:
1933 do_traverse(Traverse*);
1935 bool
1936 do_has_pointer() const
1937 { return true; }
1939 bool
1940 do_compare_is_identity(Gogo*)
1941 { return true; }
1943 unsigned int
1944 do_hash_for_method(Gogo*) const;
1946 Btype*
1947 do_get_backend(Gogo*);
1949 Expression*
1950 do_type_descriptor(Gogo*, Named_type*);
1952 void
1953 do_reflection(Gogo*, std::string*) const;
1955 void
1956 do_mangled_name(Gogo*, std::string*) const;
1958 void
1959 do_export(Export*) const;
1961 private:
1962 // The type to which this type points.
1963 Type* to_type_;
1966 // The type of a field in a struct.
1968 class Struct_field
1970 public:
1971 explicit Struct_field(const Typed_identifier& typed_identifier)
1972 : typed_identifier_(typed_identifier), tag_(NULL), is_imported_(false)
1975 // The field name.
1976 const std::string&
1977 field_name() const;
1979 // Return whether this struct field is named NAME.
1980 bool
1981 is_field_name(const std::string& name) const;
1983 // Return whether this struct field is an unexported field named NAME.
1984 bool
1985 is_unexported_field_name(Gogo*, const std::string& name) const;
1987 // Return whether this struct field is an embedded built-in type.
1988 bool
1989 is_embedded_builtin(Gogo*) const;
1991 // The field type.
1992 Type*
1993 type() const
1994 { return this->typed_identifier_.type(); }
1996 // The field location.
1997 Location
1998 location() const
1999 { return this->typed_identifier_.location(); }
2001 // Whether the field has a tag.
2002 bool
2003 has_tag() const
2004 { return this->tag_ != NULL; }
2006 // The tag.
2007 const std::string&
2008 tag() const
2010 go_assert(this->tag_ != NULL);
2011 return *this->tag_;
2014 // Whether this is an anonymous field.
2015 bool
2016 is_anonymous() const
2017 { return this->typed_identifier_.name().empty(); }
2019 // Set the tag. FIXME: This is never freed.
2020 void
2021 set_tag(const std::string& tag)
2022 { this->tag_ = new std::string(tag); }
2024 // Record that this field is defined in an imported struct.
2025 void
2026 set_is_imported()
2027 { this->is_imported_ = true; }
2029 // Set the type. This is only used in error cases.
2030 void
2031 set_type(Type* type)
2032 { this->typed_identifier_.set_type(type); }
2034 private:
2035 // The field name, type, and location.
2036 Typed_identifier typed_identifier_;
2037 // The field tag. This is NULL if the field has no tag.
2038 std::string* tag_;
2039 // Whether this field is defined in an imported struct.
2040 bool is_imported_;
2043 // A list of struct fields.
2045 class Struct_field_list
2047 public:
2048 Struct_field_list()
2049 : entries_()
2052 // Whether the list is empty.
2053 bool
2054 empty() const
2055 { return this->entries_.empty(); }
2057 // Return the number of entries.
2058 size_t
2059 size() const
2060 { return this->entries_.size(); }
2062 // Add an entry to the end of the list.
2063 void
2064 push_back(const Struct_field& sf)
2065 { this->entries_.push_back(sf); }
2067 // Index into the list.
2068 const Struct_field&
2069 at(size_t i) const
2070 { return this->entries_.at(i); }
2072 // Last entry in list.
2073 Struct_field&
2074 back()
2075 { return this->entries_.back(); }
2077 // Iterators.
2079 typedef std::vector<Struct_field>::iterator iterator;
2080 typedef std::vector<Struct_field>::const_iterator const_iterator;
2082 iterator
2083 begin()
2084 { return this->entries_.begin(); }
2086 const_iterator
2087 begin() const
2088 { return this->entries_.begin(); }
2090 iterator
2091 end()
2092 { return this->entries_.end(); }
2094 const_iterator
2095 end() const
2096 { return this->entries_.end(); }
2098 private:
2099 std::vector<Struct_field> entries_;
2102 // The type of a struct.
2104 class Struct_type : public Type
2106 public:
2107 Struct_type(Struct_field_list* fields, Location location)
2108 : Type(TYPE_STRUCT),
2109 fields_(fields), location_(location), all_methods_(NULL)
2112 // Return the field NAME. This only looks at local fields, not at
2113 // embedded types. If the field is found, and PINDEX is not NULL,
2114 // this sets *PINDEX to the field index. If the field is not found,
2115 // this returns NULL.
2116 const Struct_field*
2117 find_local_field(const std::string& name, unsigned int *pindex) const;
2119 // Return the field number INDEX.
2120 const Struct_field*
2121 field(unsigned int index) const
2122 { return &this->fields_->at(index); }
2124 // Get the struct fields.
2125 const Struct_field_list*
2126 fields() const
2127 { return this->fields_; }
2129 // Return the number of fields.
2130 size_t
2131 field_count() const
2132 { return this->fields_->size(); }
2134 // Push a new field onto the end of the struct. This is used when
2135 // building a closure variable.
2136 void
2137 push_field(const Struct_field& sf)
2138 { this->fields_->push_back(sf); }
2140 // Return an expression referring to field NAME in STRUCT_EXPR, or
2141 // NULL if there is no field with that name.
2142 Field_reference_expression*
2143 field_reference(Expression* struct_expr, const std::string& name,
2144 Location) const;
2146 // Return the total number of fields, including embedded fields.
2147 // This is the number of values that can appear in a conversion to
2148 // this type.
2149 unsigned int
2150 total_field_count() const;
2152 // Whether this type is identical with T.
2153 bool
2154 is_identical(const Struct_type* t, bool errors_are_identical) const;
2156 // Whether this struct type has any hidden fields. This returns
2157 // true if any fields have hidden names, or if any non-pointer
2158 // anonymous fields have types with hidden fields.
2159 bool
2160 struct_has_hidden_fields(const Named_type* within, std::string*) const;
2162 // Return whether NAME is a local field which is not exported. This
2163 // is only used for better error reporting.
2164 bool
2165 is_unexported_local_field(Gogo*, const std::string& name) const;
2167 // If this is an unnamed struct, build the complete list of methods,
2168 // including those from anonymous fields, and build methods stubs if
2169 // needed.
2170 void
2171 finalize_methods(Gogo*);
2173 // Return whether this type has any methods. This should only be
2174 // called after the finalize_methods pass.
2175 bool
2176 has_any_methods() const
2177 { return this->all_methods_ != NULL; }
2179 // Return the methods for tihs type. This should only be called
2180 // after the finalize_methods pass.
2181 const Methods*
2182 methods() const
2183 { return this->all_methods_; }
2185 // Return the method to use for NAME. This returns NULL if there is
2186 // no such method or if the method is ambiguous. When it returns
2187 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2188 Method*
2189 method_function(const std::string& name, bool* is_ambiguous) const;
2191 // Return a pointer to the interface method table for this type for
2192 // the interface INTERFACE. If IS_POINTER is true, set the type
2193 // descriptor to a pointer to this type, otherwise set it to this
2194 // type.
2195 Expression*
2196 interface_method_table(Interface_type* interface, bool is_pointer);
2198 // Traverse just the field types of a struct type.
2200 traverse_field_types(Traverse* traverse)
2201 { return this->do_traverse(traverse); }
2203 // If the offset of field INDEX in the backend implementation can be
2204 // determined, set *POFFSET to the offset in bytes and return true.
2205 // Otherwise, return false.
2206 bool
2207 backend_field_offset(Gogo*, unsigned int index, unsigned int* poffset);
2209 // Finish the backend representation of all the fields.
2210 void
2211 finish_backend_fields(Gogo*);
2213 // Import a struct type.
2214 static Struct_type*
2215 do_import(Import*);
2217 static Type*
2218 make_struct_type_descriptor_type();
2220 // Write the hash function for this type.
2221 void
2222 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2224 // Write the equality function for this type.
2225 void
2226 write_equal_function(Gogo*, Named_type*);
2228 protected:
2230 do_traverse(Traverse*);
2232 bool
2233 do_verify();
2235 bool
2236 do_has_pointer() const;
2238 bool
2239 do_compare_is_identity(Gogo*);
2241 unsigned int
2242 do_hash_for_method(Gogo*) const;
2244 Btype*
2245 do_get_backend(Gogo*);
2247 Expression*
2248 do_type_descriptor(Gogo*, Named_type*);
2250 void
2251 do_reflection(Gogo*, std::string*) const;
2253 void
2254 do_mangled_name(Gogo*, std::string*) const;
2256 void
2257 do_export(Export*) const;
2259 private:
2260 // Used to merge method sets of identical unnamed structs.
2261 typedef Unordered_map_hash(Struct_type*, Struct_type*, Type_hash_identical,
2262 Type_identical) Identical_structs;
2264 static Identical_structs identical_structs;
2266 // Used to manage method tables for identical unnamed structs.
2267 typedef std::pair<Interface_method_tables*, Interface_method_tables*>
2268 Struct_method_table_pair;
2270 typedef Unordered_map_hash(Struct_type*, Struct_method_table_pair*,
2271 Type_hash_identical, Type_identical)
2272 Struct_method_tables;
2274 static Struct_method_tables struct_method_tables;
2276 // Used to avoid infinite loops in field_reference_depth.
2277 struct Saw_named_type
2279 Saw_named_type* next;
2280 Named_type* nt;
2283 Field_reference_expression*
2284 field_reference_depth(Expression* struct_expr, const std::string& name,
2285 Location, Saw_named_type*,
2286 unsigned int* depth) const;
2288 // The fields of the struct.
2289 Struct_field_list* fields_;
2290 // The place where the struct was declared.
2291 Location location_;
2292 // If this struct is unnamed, a list of methods.
2293 Methods* all_methods_;
2296 // The type of an array.
2298 class Array_type : public Type
2300 public:
2301 Array_type(Type* element_type, Expression* length)
2302 : Type(TYPE_ARRAY),
2303 element_type_(element_type), length_(length), blength_(NULL)
2306 // Return the element type.
2307 Type*
2308 element_type() const
2309 { return this->element_type_; }
2311 // Return the length. This will return NULL for a slice.
2312 Expression*
2313 length() const
2314 { return this->length_; }
2316 // Whether this type is identical with T.
2317 bool
2318 is_identical(const Array_type* t, bool errors_are_identical) const;
2320 // Whether this type has any hidden fields.
2321 bool
2322 array_has_hidden_fields(const Named_type* within, std::string* reason) const
2323 { return this->element_type_->has_hidden_fields(within, reason); }
2325 // Return an expression for the pointer to the values in an array.
2326 Expression*
2327 get_value_pointer(Gogo*, Expression* array) const;
2329 // Return an expression for the length of an array with this type.
2330 Expression*
2331 get_length(Gogo*, Expression* array) const;
2333 // Return an expression for the capacity of an array with this type.
2334 Expression*
2335 get_capacity(Gogo*, Expression* array) const;
2337 // Import an array type.
2338 static Array_type*
2339 do_import(Import*);
2341 // Return the backend representation of the element type.
2342 Btype*
2343 get_backend_element(Gogo*, bool use_placeholder);
2345 // Return the backend representation of the length.
2346 Bexpression*
2347 get_backend_length(Gogo*);
2349 // Finish the backend representation of the element type.
2350 void
2351 finish_backend_element(Gogo*);
2353 static Type*
2354 make_array_type_descriptor_type();
2356 static Type*
2357 make_slice_type_descriptor_type();
2359 // Write the hash function for this type.
2360 void
2361 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
2363 // Write the equality function for this type.
2364 void
2365 write_equal_function(Gogo*, Named_type*);
2367 protected:
2369 do_traverse(Traverse* traverse);
2371 bool
2372 do_verify();
2374 bool
2375 do_has_pointer() const
2377 return this->length_ == NULL || this->element_type_->has_pointer();
2380 bool
2381 do_compare_is_identity(Gogo*);
2383 unsigned int
2384 do_hash_for_method(Gogo*) const;
2386 Btype*
2387 do_get_backend(Gogo*);
2389 Expression*
2390 do_type_descriptor(Gogo*, Named_type*);
2392 void
2393 do_reflection(Gogo*, std::string*) const;
2395 void
2396 do_mangled_name(Gogo*, std::string*) const;
2398 void
2399 do_export(Export*) const;
2401 private:
2402 bool
2403 verify_length();
2405 Expression*
2406 array_type_descriptor(Gogo*, Named_type*);
2408 Expression*
2409 slice_type_descriptor(Gogo*, Named_type*);
2411 // The type of elements of the array.
2412 Type* element_type_;
2413 // The number of elements. This may be NULL.
2414 Expression* length_;
2415 // The backend representation of the length.
2416 // We only want to compute this once.
2417 Bexpression* blength_;
2420 // The type of a map.
2422 class Map_type : public Type
2424 public:
2425 Map_type(Type* key_type, Type* val_type, Location location)
2426 : Type(TYPE_MAP),
2427 key_type_(key_type), val_type_(val_type), location_(location)
2430 // Return the key type.
2431 Type*
2432 key_type() const
2433 { return this->key_type_; }
2435 // Return the value type.
2436 Type*
2437 val_type() const
2438 { return this->val_type_; }
2440 // Whether this type is identical with T.
2441 bool
2442 is_identical(const Map_type* t, bool errors_are_identical) const;
2444 // Import a map type.
2445 static Map_type*
2446 do_import(Import*);
2448 static Type*
2449 make_map_type_descriptor_type();
2451 static Type*
2452 make_map_descriptor_type();
2454 // Build a map descriptor for this type. Return a pointer to it.
2455 // The location is the location which causes us to need the
2456 // descriptor.
2457 Bexpression*
2458 map_descriptor_pointer(Gogo* gogo, Location);
2460 protected:
2462 do_traverse(Traverse*);
2464 bool
2465 do_verify();
2467 bool
2468 do_has_pointer() const
2469 { return true; }
2471 bool
2472 do_compare_is_identity(Gogo*)
2473 { return false; }
2475 unsigned int
2476 do_hash_for_method(Gogo*) const;
2478 Btype*
2479 do_get_backend(Gogo*);
2481 Expression*
2482 do_type_descriptor(Gogo*, Named_type*);
2484 void
2485 do_reflection(Gogo*, std::string*) const;
2487 void
2488 do_mangled_name(Gogo*, std::string*) const;
2490 void
2491 do_export(Export*) const;
2493 private:
2494 // Mapping from map types to map descriptors.
2495 typedef Unordered_map_hash(const Map_type*, Bvariable*, Type_hash_identical,
2496 Type_identical) Map_descriptors;
2497 static Map_descriptors map_descriptors;
2499 Bvariable*
2500 map_descriptor(Gogo*);
2502 // The key type.
2503 Type* key_type_;
2504 // The value type.
2505 Type* val_type_;
2506 // Where the type was defined.
2507 Location location_;
2510 // The type of a channel.
2512 class Channel_type : public Type
2514 public:
2515 Channel_type(bool may_send, bool may_receive, Type* element_type)
2516 : Type(TYPE_CHANNEL),
2517 may_send_(may_send), may_receive_(may_receive),
2518 element_type_(element_type)
2519 { go_assert(may_send || may_receive); }
2521 // Whether this channel can send data.
2522 bool
2523 may_send() const
2524 { return this->may_send_; }
2526 // Whether this channel can receive data.
2527 bool
2528 may_receive() const
2529 { return this->may_receive_; }
2531 // The type of the values that may be sent on this channel. This is
2532 // NULL if any type may be sent.
2533 Type*
2534 element_type() const
2535 { return this->element_type_; }
2537 // Whether this type is identical with T.
2538 bool
2539 is_identical(const Channel_type* t, bool errors_are_identical) const;
2541 // Import a channel type.
2542 static Channel_type*
2543 do_import(Import*);
2545 static Type*
2546 make_chan_type_descriptor_type();
2548 protected:
2550 do_traverse(Traverse* traverse)
2551 { return Type::traverse(this->element_type_, traverse); }
2553 bool
2554 do_has_pointer() const
2555 { return true; }
2557 bool
2558 do_compare_is_identity(Gogo*)
2559 { return true; }
2561 unsigned int
2562 do_hash_for_method(Gogo*) const;
2564 Btype*
2565 do_get_backend(Gogo*);
2567 Expression*
2568 do_type_descriptor(Gogo*, Named_type*);
2570 void
2571 do_reflection(Gogo*, std::string*) const;
2573 void
2574 do_mangled_name(Gogo*, std::string*) const;
2576 void
2577 do_export(Export*) const;
2579 private:
2580 // Whether this channel can send data.
2581 bool may_send_;
2582 // Whether this channel can receive data.
2583 bool may_receive_;
2584 // The types of elements which may be sent on this channel. If this
2585 // is NULL, it means that any type may be sent.
2586 Type* element_type_;
2589 // An interface type.
2591 class Interface_type : public Type
2593 public:
2594 Interface_type(Typed_identifier_list* methods, Location location)
2595 : Type(TYPE_INTERFACE),
2596 parse_methods_(methods), all_methods_(NULL), location_(location),
2597 interface_btype_(NULL), bmethods_(NULL), assume_identical_(NULL),
2598 methods_are_finalized_(false), bmethods_is_placeholder_(false),
2599 seen_(false)
2600 { go_assert(methods == NULL || !methods->empty()); }
2602 // The location where the interface type was defined.
2603 Location
2604 location() const
2605 { return this->location_; }
2607 // Return whether this is an empty interface.
2608 bool
2609 is_empty() const
2611 go_assert(this->methods_are_finalized_);
2612 return this->all_methods_ == NULL;
2615 // Return the list of methods. This will return NULL for an empty
2616 // interface.
2617 const Typed_identifier_list*
2618 methods() const;
2620 // Return the number of methods.
2621 size_t
2622 method_count() const;
2624 // Return the method NAME, or NULL.
2625 const Typed_identifier*
2626 find_method(const std::string& name) const;
2628 // Return the zero-based index of method NAME.
2629 size_t
2630 method_index(const std::string& name) const;
2632 // Finalize the methods. This sets all_methods_. This handles
2633 // interface inheritance.
2634 void
2635 finalize_methods();
2637 // Return true if T implements this interface. If this returns
2638 // false, and REASON is not NULL, it sets *REASON to the reason that
2639 // it fails.
2640 bool
2641 implements_interface(const Type* t, std::string* reason) const;
2643 // Whether this type is identical with T. REASON is as in
2644 // implements_interface.
2645 bool
2646 is_identical(const Interface_type* t, bool errors_are_identical) const;
2648 // Whether we can assign T to this type. is_identical is known to
2649 // be false.
2650 bool
2651 is_compatible_for_assign(const Interface_type*, std::string* reason) const;
2653 // Return whether NAME is a method which is not exported. This is
2654 // only used for better error reporting.
2655 bool
2656 is_unexported_method(Gogo*, const std::string& name) const;
2658 // Import an interface type.
2659 static Interface_type*
2660 do_import(Import*);
2662 // Make a struct for an empty interface type.
2663 static Btype*
2664 get_backend_empty_interface_type(Gogo*);
2666 // Get a pointer to the backend representation of the method table.
2667 Btype*
2668 get_backend_methods(Gogo*);
2670 // Return a placeholder for the backend representation of the
2671 // pointer to the method table.
2672 Btype*
2673 get_backend_methods_placeholder(Gogo*);
2675 // Finish the backend representation of the method types.
2676 void
2677 finish_backend_methods(Gogo*);
2679 static Type*
2680 make_interface_type_descriptor_type();
2682 protected:
2684 do_traverse(Traverse*);
2686 bool
2687 do_has_pointer() const
2688 { return true; }
2690 bool
2691 do_compare_is_identity(Gogo*)
2692 { return false; }
2694 unsigned int
2695 do_hash_for_method(Gogo*) const;
2697 Btype*
2698 do_get_backend(Gogo*);
2700 Expression*
2701 do_type_descriptor(Gogo*, Named_type*);
2703 void
2704 do_reflection(Gogo*, std::string*) const;
2706 void
2707 do_mangled_name(Gogo*, std::string*) const;
2709 void
2710 do_export(Export*) const;
2712 private:
2713 // This type guards against infinite recursion when comparing
2714 // interface types. We keep a list of interface types assumed to be
2715 // identical during comparison. We just keep the list on the stack.
2716 // This permits us to compare cases like
2717 // type I1 interface { F() interface{I1} }
2718 // type I2 interface { F() interface{I2} }
2719 struct Assume_identical
2721 Assume_identical* next;
2722 const Interface_type* t1;
2723 const Interface_type* t2;
2726 bool
2727 assume_identical(const Interface_type*, const Interface_type*) const;
2729 // The list of methods associated with the interface from the
2730 // parser. This will be NULL for the empty interface. This may
2731 // include unnamed interface types.
2732 Typed_identifier_list* parse_methods_;
2733 // The list of all methods associated with the interface. This
2734 // expands any interface types listed in methods_. It is set by
2735 // finalize_methods. This will be NULL for the empty interface.
2736 Typed_identifier_list* all_methods_;
2737 // The location where the interface was defined.
2738 Location location_;
2739 // The backend representation of this type during backend conversion.
2740 Btype* interface_btype_;
2741 // The backend representation of the pointer to the method table.
2742 Btype* bmethods_;
2743 // A list of interface types assumed to be identical during
2744 // interface comparison.
2745 mutable Assume_identical* assume_identical_;
2746 // Whether the methods have been finalized.
2747 bool methods_are_finalized_;
2748 // Whether the bmethods_ field is a placeholder.
2749 bool bmethods_is_placeholder_;
2750 // Used to avoid endless recursion in do_mangled_name.
2751 mutable bool seen_;
2754 // The value we keep for a named type. This lets us get the right
2755 // name when we convert to backend. Note that we don't actually keep
2756 // the name here; the name is in the Named_object which points to
2757 // this. This object exists to hold a unique backend representation for
2758 // the type.
2760 class Named_type : public Type
2762 public:
2763 Named_type(Named_object* named_object, Type* type, Location location)
2764 : Type(TYPE_NAMED),
2765 named_object_(named_object), in_function_(NULL), in_function_index_(0),
2766 type_(type), local_methods_(NULL), all_methods_(NULL),
2767 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
2768 location_(location), named_btype_(NULL), dependencies_(),
2769 is_visible_(true), is_error_(false), is_placeholder_(false),
2770 is_converted_(false), is_circular_(false), is_verified_(false),
2771 seen_(false), seen_in_compare_is_identity_(false),
2772 seen_in_get_backend_(false)
2775 // Return the associated Named_object. This holds the actual name.
2776 Named_object*
2777 named_object()
2778 { return this->named_object_; }
2780 const Named_object*
2781 named_object() const
2782 { return this->named_object_; }
2784 // Set the Named_object. This is used when we see a type
2785 // declaration followed by a type.
2786 void
2787 set_named_object(Named_object* no)
2788 { this->named_object_ = no; }
2790 // Return the function in which this type is defined. This will
2791 // return NULL for a type defined in global scope.
2792 const Named_object*
2793 in_function(unsigned int *pindex) const
2795 *pindex = this->in_function_index_;
2796 return this->in_function_;
2799 // Set the function in which this type is defined.
2800 void
2801 set_in_function(Named_object* f, unsigned int index)
2803 this->in_function_ = f;
2804 this->in_function_index_ = index;
2807 // Return the name of the type.
2808 const std::string&
2809 name() const;
2811 // Return the name of the type for an error message. The difference
2812 // is that if the type is defined in a different package, this will
2813 // return PACKAGE.NAME.
2814 std::string
2815 message_name() const;
2817 // Return the underlying type.
2818 Type*
2819 real_type()
2820 { return this->type_; }
2822 const Type*
2823 real_type() const
2824 { return this->type_; }
2826 // Return the location.
2827 Location
2828 location() const
2829 { return this->location_; }
2831 // Whether this type is visible. This only matters when parsing.
2832 bool
2833 is_visible() const
2834 { return this->is_visible_; }
2836 // Mark this type as visible.
2837 void
2838 set_is_visible()
2839 { this->is_visible_ = true; }
2841 // Mark this type as invisible.
2842 void
2843 clear_is_visible()
2844 { this->is_visible_ = false; }
2846 // Whether this is a builtin type.
2847 bool
2848 is_builtin() const
2849 { return Linemap::is_predeclared_location(this->location_); }
2851 // Whether this is an alias. There are currently two aliases: byte
2852 // and rune.
2853 bool
2854 is_alias() const;
2856 // Whether this is a circular type: a pointer or function type that
2857 // refers to itself, which is not possible in C.
2858 bool
2859 is_circular() const
2860 { return this->is_circular_; }
2862 // Return the base type for this type.
2863 Type*
2864 named_base();
2866 const Type*
2867 named_base() const;
2869 // Return whether this is an error type.
2870 bool
2871 is_named_error_type() const;
2873 // Return whether this type is comparable. If REASON is not NULL,
2874 // set *REASON when returning false.
2875 bool
2876 named_type_is_comparable(std::string* reason) const;
2878 // Add a method to this type.
2879 Named_object*
2880 add_method(const std::string& name, Function*);
2882 // Add a method declaration to this type.
2883 Named_object*
2884 add_method_declaration(const std::string& name, Package* package,
2885 Function_type* type, Location location);
2887 // Add an existing method--one defined before the type itself was
2888 // defined--to a type.
2889 void
2890 add_existing_method(Named_object*);
2892 // Look up a local method.
2893 Named_object*
2894 find_local_method(const std::string& name) const;
2896 // Return the list of local methods.
2897 const Bindings*
2898 local_methods() const
2899 { return this->local_methods_; }
2901 // Build the complete list of methods, including those from
2902 // anonymous fields, and build method stubs if needed.
2903 void
2904 finalize_methods(Gogo*);
2906 // Return whether this type has any methods. This should only be
2907 // called after the finalize_methods pass.
2908 bool
2909 has_any_methods() const
2910 { return this->all_methods_ != NULL; }
2912 // Return the methods for this type. This should only be called
2913 // after the finalized_methods pass.
2914 const Methods*
2915 methods() const
2916 { return this->all_methods_; }
2918 // Return the method to use for NAME. This returns NULL if there is
2919 // no such method or if the method is ambiguous. When it returns
2920 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2921 Method*
2922 method_function(const std::string& name, bool *is_ambiguous) const;
2924 // Return whether NAME is a known field or method which is not
2925 // exported. This is only used for better error reporting.
2926 bool
2927 is_unexported_local_method(Gogo*, const std::string& name) const;
2929 // Return a pointer to the interface method table for this type for
2930 // the interface INTERFACE. If IS_POINTER is true, set the type
2931 // descriptor to a pointer to this type, otherwise set it to this
2932 // type.
2933 Expression*
2934 interface_method_table(Interface_type* interface, bool is_pointer);
2936 // Whether this type has any hidden fields.
2937 bool
2938 named_type_has_hidden_fields(std::string* reason) const;
2940 // Note that a type must be converted to the backend representation
2941 // before we convert this type.
2942 void
2943 add_dependency(Named_type* nt)
2944 { this->dependencies_.push_back(nt); }
2946 // Return true if the size and alignment of the backend
2947 // representation of this type is known. This is always true after
2948 // types have been converted, but may be false beforehand.
2949 bool
2950 is_named_backend_type_size_known() const
2951 { return this->named_btype_ != NULL && !this->is_placeholder_; }
2953 // Export the type.
2954 void
2955 export_named_type(Export*, const std::string& name) const;
2957 // Import a named type.
2958 static void
2959 import_named_type(Import*, Named_type**);
2961 // Initial conversion to backend representation.
2962 void
2963 convert(Gogo*);
2965 protected:
2967 do_traverse(Traverse* traverse)
2968 { return Type::traverse(this->type_, traverse); }
2970 bool
2971 do_verify();
2973 bool
2974 do_has_pointer() const;
2976 bool
2977 do_compare_is_identity(Gogo*);
2979 unsigned int
2980 do_hash_for_method(Gogo*) const;
2982 Btype*
2983 do_get_backend(Gogo*);
2985 Expression*
2986 do_type_descriptor(Gogo*, Named_type*);
2988 void
2989 do_reflection(Gogo*, std::string*) const;
2991 void
2992 do_mangled_name(Gogo*, std::string* ret) const;
2994 void
2995 do_export(Export*) const;
2997 private:
2998 // Create the placeholder during conversion.
2999 void
3000 create_placeholder(Gogo*);
3002 // A pointer back to the Named_object for this type.
3003 Named_object* named_object_;
3004 // If this type is defined in a function, a pointer back to the
3005 // function in which it is defined.
3006 Named_object* in_function_;
3007 // The index of this type in IN_FUNCTION_.
3008 unsigned int in_function_index_;
3009 // The actual type.
3010 Type* type_;
3011 // The list of methods defined for this type. Any named type can
3012 // have methods.
3013 Bindings* local_methods_;
3014 // The full list of methods for this type, including methods
3015 // declared for anonymous fields.
3016 Methods* all_methods_;
3017 // A mapping from interfaces to the associated interface method
3018 // tables for this type.
3019 Interface_method_tables* interface_method_tables_;
3020 // A mapping from interfaces to the associated interface method
3021 // tables for pointers to this type.
3022 Interface_method_tables* pointer_interface_method_tables_;
3023 // The location where this type was defined.
3024 Location location_;
3025 // The backend representation of this type during backend
3026 // conversion. This is used to avoid endless recursion when a named
3027 // type refers to itself.
3028 Btype* named_btype_;
3029 // A list of types which must be converted to the backend
3030 // representation before this type can be converted. This is for
3031 // cases like
3032 // type S1 { p *S2 }
3033 // type S2 { s S1 }
3034 // where we can't convert S2 to the backend representation unless we
3035 // have converted S1.
3036 std::vector<Named_type*> dependencies_;
3037 // Whether this type is visible. This is false if this type was
3038 // created because it was referenced by an imported object, but the
3039 // type itself was not exported. This will always be true for types
3040 // created in the current package.
3041 bool is_visible_;
3042 // Whether this type is erroneous.
3043 bool is_error_;
3044 // Whether the current value of named_btype_ is a placeholder for
3045 // which the final size of the type is not known.
3046 bool is_placeholder_;
3047 // Whether this type has been converted to the backend
3048 // representation. Implies that is_placeholder_ is false.
3049 bool is_converted_;
3050 // Whether this is a pointer or function type which refers to the
3051 // type itself.
3052 bool is_circular_;
3053 // Whether this type has been verified.
3054 bool is_verified_;
3055 // In a recursive operation such as has_hidden_fields, this flag is
3056 // used to prevent infinite recursion when a type refers to itself.
3057 // This is mutable because it is always reset to false when the
3058 // function exits.
3059 mutable bool seen_;
3060 // Like seen_, but used only by do_compare_is_identity.
3061 bool seen_in_compare_is_identity_;
3062 // Like seen_, but used only by do_get_backend.
3063 bool seen_in_get_backend_;
3066 // A forward declaration. This handles a type which has been declared
3067 // but not defined.
3069 class Forward_declaration_type : public Type
3071 public:
3072 Forward_declaration_type(Named_object* named_object);
3074 // The named object associated with this type declaration. This
3075 // will be resolved.
3076 Named_object*
3077 named_object();
3079 const Named_object*
3080 named_object() const;
3082 // Return the name of the type.
3083 const std::string&
3084 name() const;
3086 // Return the type to which this points. Give an error if the type
3087 // has not yet been defined.
3088 Type*
3089 real_type();
3091 const Type*
3092 real_type() const;
3094 // Whether the base type has been defined.
3095 bool
3096 is_defined() const;
3098 // Add a method to this type.
3099 Named_object*
3100 add_method(const std::string& name, Function*);
3102 // Add a method declaration to this type.
3103 Named_object*
3104 add_method_declaration(const std::string& name, Package*, Function_type*,
3105 Location);
3107 protected:
3109 do_traverse(Traverse* traverse);
3111 bool
3112 do_verify();
3114 bool
3115 do_has_pointer() const
3116 { return this->real_type()->has_pointer(); }
3118 bool
3119 do_compare_is_identity(Gogo* gogo)
3120 { return this->real_type()->compare_is_identity(gogo); }
3122 unsigned int
3123 do_hash_for_method(Gogo* gogo) const
3124 { return this->real_type()->hash_for_method(gogo); }
3126 Btype*
3127 do_get_backend(Gogo* gogo);
3129 Expression*
3130 do_type_descriptor(Gogo*, Named_type*);
3132 void
3133 do_reflection(Gogo*, std::string*) const;
3135 void
3136 do_mangled_name(Gogo*, std::string* ret) const;
3138 void
3139 do_export(Export*) const;
3141 private:
3142 // Issue a warning about a use of an undefined type.
3143 void
3144 warn() const;
3146 // The type declaration.
3147 Named_object* named_object_;
3148 // Whether we have issued a warning about this type.
3149 mutable bool warned_;
3152 // The Type_context struct describes what we expect for the type of an
3153 // expression.
3155 struct Type_context
3157 // The exact type we expect, if known. This may be NULL.
3158 Type* type;
3159 // Whether an abstract type is permitted.
3160 bool may_be_abstract;
3162 // Constructors.
3163 Type_context()
3164 : type(NULL), may_be_abstract(false)
3167 Type_context(Type* a_type, bool a_may_be_abstract)
3168 : type(a_type), may_be_abstract(a_may_be_abstract)
3172 #endif // !defined(GO_TYPES_H)