Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / gcc / go / gofrontend / types.h
blob2a713b067d4a68fa09fb1eb85fb3f922270690a2
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 class Gogo;
11 class Package;
12 class Traverse;
13 class Typed_identifier;
14 class Typed_identifier_list;
15 class Integer_type;
16 class Float_type;
17 class Complex_type;
18 class String_type;
19 class Function_type;
20 class Struct_field;
21 class Struct_field_list;
22 class Struct_type;
23 class Pointer_type;
24 class Array_type;
25 class Map_type;
26 class Channel_type;
27 class Interface_type;
28 class Named_type;
29 class Forward_declaration_type;
30 class Method;
31 class Methods;
32 class Type_hash_identical;
33 class Type_identical;
34 class Expression;
35 class Expression_list;
36 class Call_expression;
37 class Field_reference_expression;
38 class Bound_method_expression;
39 class Bindings;
40 class Named_object;
41 class Function;
42 class Translate_context;
43 class Export;
44 class Import;
46 // Type codes used in type descriptors. These must match the values
47 // in libgo/runtime/go-type.h. They also match the values in the gc
48 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
49 // although this is not required.
51 static const int RUNTIME_TYPE_KIND_BOOL = 1;
52 static const int RUNTIME_TYPE_KIND_INT = 2;
53 static const int RUNTIME_TYPE_KIND_INT8 = 3;
54 static const int RUNTIME_TYPE_KIND_INT16 = 4;
55 static const int RUNTIME_TYPE_KIND_INT32 = 5;
56 static const int RUNTIME_TYPE_KIND_INT64 = 6;
57 static const int RUNTIME_TYPE_KIND_UINT = 7;
58 static const int RUNTIME_TYPE_KIND_UINT8 = 8;
59 static const int RUNTIME_TYPE_KIND_UINT16 = 9;
60 static const int RUNTIME_TYPE_KIND_UINT32 = 10;
61 static const int RUNTIME_TYPE_KIND_UINT64 = 11;
62 static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
63 static const int RUNTIME_TYPE_KIND_FLOAT = 13;
64 static const int RUNTIME_TYPE_KIND_FLOAT32 = 14;
65 static const int RUNTIME_TYPE_KIND_FLOAT64 = 15;
66 static const int RUNTIME_TYPE_KIND_COMPLEX = 16;
67 static const int RUNTIME_TYPE_KIND_COMPLEX64 = 17;
68 static const int RUNTIME_TYPE_KIND_COMPLEX128 = 18;
69 static const int RUNTIME_TYPE_KIND_ARRAY = 19;
70 static const int RUNTIME_TYPE_KIND_CHAN = 20;
71 static const int RUNTIME_TYPE_KIND_FUNC = 21;
72 static const int RUNTIME_TYPE_KIND_INTERFACE = 22;
73 static const int RUNTIME_TYPE_KIND_MAP = 23;
74 static const int RUNTIME_TYPE_KIND_PTR = 24;
75 static const int RUNTIME_TYPE_KIND_SLICE = 25;
76 static const int RUNTIME_TYPE_KIND_STRING = 26;
77 static const int RUNTIME_TYPE_KIND_STRUCT = 27;
78 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 28;
80 // To build the complete list of methods for a named type we need to
81 // gather all methods from anonymous fields. Those methods may
82 // require an arbitrary set of indirections and field offsets. There
83 // is also the possibility of ambiguous methods, which we could ignore
84 // except that we want to give a better error message for that case.
85 // This is a base class. There are two types of methods: named
86 // methods, and methods which are inherited from an anonymous field of
87 // interface type.
89 class Method
91 public:
92 // For methods in anonymous types we need to know the sequence of
93 // field references used to extract the pointer to pass to the
94 // method. Since each method for a particular anonymous field will
95 // have the sequence of field indexes, and since the indexes can be
96 // shared going down the chain, we use a manually managed linked
97 // list. The first entry in the list is the field index for the
98 // last field, the one passed to the method.
100 struct Field_indexes
102 const Field_indexes* next;
103 unsigned int field_index;
106 virtual ~Method()
109 // Get the list of field indexes.
110 const Field_indexes*
111 field_indexes() const
112 { return this->field_indexes_; }
114 // Get the depth.
115 unsigned int
116 depth() const
117 { return this->depth_; }
119 // Return whether this is a value method--a method which does not
120 // require a pointer expression.
121 bool
122 is_value_method() const
123 { return this->is_value_method_; }
125 // Return whether we need a stub method--this is true if we can't
126 // just pass the main object to the method.
127 bool
128 needs_stub_method() const
129 { return this->needs_stub_method_; }
131 // Return whether this is an ambiguous method name.
132 bool
133 is_ambiguous() const
134 { return this->is_ambiguous_; }
136 // Note that this method is ambiguous.
137 void
138 set_is_ambiguous()
139 { this->is_ambiguous_ = true; }
141 // Return the type of the method.
142 Function_type*
143 type() const
144 { return this->do_type(); }
146 // Return the location of the method receiver.
147 source_location
148 receiver_location() const
149 { return this->do_receiver_location(); }
151 // Return an expression which binds this method to EXPR. This is
152 // something which can be used with a function call.
153 Expression*
154 bind_method(Expression* expr, source_location location) const;
156 // Return the named object for this method. This may only be called
157 // after methods are finalized.
158 Named_object*
159 named_object() const;
161 // Get the stub object.
162 Named_object*
163 stub_object() const
165 gcc_assert(this->stub_ != NULL);
166 return this->stub_;
169 // Set the stub object.
170 void
171 set_stub_object(Named_object* no)
173 gcc_assert(this->stub_ == NULL);
174 this->stub_ = no;
177 protected:
178 // These objects are only built by the child classes.
179 Method(const Field_indexes* field_indexes, unsigned int depth,
180 bool is_value_method, bool needs_stub_method)
181 : field_indexes_(field_indexes), depth_(depth), stub_(NULL),
182 is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
183 is_ambiguous_(false)
186 // The named object for this method.
187 virtual Named_object*
188 do_named_object() const = 0;
190 // The type of the method.
191 virtual Function_type*
192 do_type() const = 0;
194 // Return the location of the method receiver.
195 virtual source_location
196 do_receiver_location() const = 0;
198 // Bind a method to an object.
199 virtual Expression*
200 do_bind_method(Expression* expr, source_location location) const = 0;
202 private:
203 // The sequence of field indexes used for this method. If this is
204 // NULL, then the method is defined for the current type.
205 const Field_indexes* field_indexes_;
206 // The depth at which this method was found.
207 unsigned int depth_;
208 // If a stub method is required, this is its object. This is only
209 // set after stub methods are built in finalize_methods.
210 Named_object* stub_;
211 // Whether this is a value method--a method that does not require a
212 // pointer.
213 bool is_value_method_;
214 // Whether a stub method is required.
215 bool needs_stub_method_;
216 // Whether this method is ambiguous.
217 bool is_ambiguous_;
220 // A named method. This is what you get with a method declaration,
221 // either directly on the type, or inherited from some anonymous
222 // embedded field.
224 class Named_method : public Method
226 public:
227 Named_method(Named_object* named_object, const Field_indexes* field_indexes,
228 unsigned int depth, bool is_value_method,
229 bool needs_stub_method)
230 : Method(field_indexes, depth, is_value_method, needs_stub_method),
231 named_object_(named_object)
234 protected:
235 // Get the Named_object for the method.
236 Named_object*
237 do_named_object() const
238 { return this->named_object_; }
240 // The type of the method.
241 Function_type*
242 do_type() const;
244 // Return the location of the method receiver.
245 source_location
246 do_receiver_location() const;
248 // Bind a method to an object.
249 Expression*
250 do_bind_method(Expression* expr, source_location location) const;
252 private:
253 // The method itself. For a method which needs a stub, this starts
254 // out as the underlying method, and is later replaced with the stub
255 // method.
256 Named_object* named_object_;
259 // An interface method. This is used when an interface appears as an
260 // anonymous field in a named struct.
262 class Interface_method : public Method
264 public:
265 Interface_method(const std::string& name, source_location location,
266 Function_type* fntype, const Field_indexes* field_indexes,
267 unsigned int depth)
268 : Method(field_indexes, depth, true, true),
269 name_(name), location_(location), fntype_(fntype)
272 protected:
273 // Get the Named_object for the method. This should never be
274 // called, as we always create a stub.
275 Named_object*
276 do_named_object() const
277 { gcc_unreachable(); }
279 // The type of the method.
280 Function_type*
281 do_type() const
282 { return this->fntype_; }
284 // Return the location of the method receiver.
285 source_location
286 do_receiver_location() const
287 { return this->location_; }
289 // Bind a method to an object.
290 Expression*
291 do_bind_method(Expression* expr, source_location location) const;
293 private:
294 // The name of the interface method to call.
295 std::string name_;
296 // The location of the definition of the interface method.
297 source_location location_;
298 // The type of the interface method.
299 Function_type* fntype_;
302 // A mapping from method name to Method. This is a wrapper around a
303 // hash table.
305 class Methods
307 private:
308 typedef Unordered_map(std::string, Method*) Method_map;
310 public:
311 typedef Method_map::const_iterator const_iterator;
313 Methods()
314 : methods_()
317 // Insert a new method. Returns true if it was inserted, false if
318 // it was overidden or ambiguous.
319 bool
320 insert(const std::string& name, Method* m);
322 // The number of (unambiguous) methods.
323 size_t
324 count() const;
326 // Iterate.
327 const_iterator
328 begin() const
329 { return this->methods_.begin(); }
331 const_iterator
332 end() const
333 { return this->methods_.end(); }
335 // Lookup.
336 const_iterator
337 find(const std::string& name) const
338 { return this->methods_.find(name); }
340 private:
341 Method_map methods_;
344 // The base class for all types.
346 class Type
348 public:
349 // The types of types.
350 enum Type_classification
352 TYPE_ERROR,
353 TYPE_VOID,
354 TYPE_BOOLEAN,
355 TYPE_INTEGER,
356 TYPE_FLOAT,
357 TYPE_COMPLEX,
358 TYPE_STRING,
359 TYPE_SINK,
360 TYPE_FUNCTION,
361 TYPE_POINTER,
362 TYPE_NIL,
363 TYPE_CALL_MULTIPLE_RESULT,
364 TYPE_STRUCT,
365 TYPE_ARRAY,
366 TYPE_MAP,
367 TYPE_CHANNEL,
368 TYPE_INTERFACE,
369 TYPE_NAMED,
370 TYPE_FORWARD
373 virtual ~Type();
375 // Creators.
377 static Type*
378 make_error_type();
380 static Type*
381 make_void_type();
383 // Get the unnamed bool type.
384 static Type*
385 make_boolean_type();
387 // Get the named type "bool".
388 static Named_type*
389 lookup_bool_type();
391 // Make the named type "bool".
392 static Named_type*
393 make_named_bool_type();
395 // Make an abstract integer type.
396 static Integer_type*
397 make_abstract_integer_type();
399 // Make a named integer type with a specified size.
400 // RUNTIME_TYPE_KIND is the code to use in reflection information,
401 // to distinguish int and int32.
402 static Named_type*
403 make_integer_type(const char* name, bool is_unsigned, int bits,
404 int runtime_type_kind);
406 // Look up a named integer type.
407 static Named_type*
408 lookup_integer_type(const char* name);
410 // Make an abstract floating point type.
411 static Float_type*
412 make_abstract_float_type();
414 // Make a named floating point type with a specific size.
415 // RUNTIME_TYPE_KIND is the code to use in reflection information,
416 // to distinguish float and float32.
417 static Named_type*
418 make_float_type(const char* name, int bits, int runtime_type_kind);
420 // Look up a named float type.
421 static Named_type*
422 lookup_float_type(const char* name);
424 // Make an abstract complex type.
425 static Complex_type*
426 make_abstract_complex_type();
428 // Make a named complex type with a specific size.
429 // RUNTIME_TYPE_KIND is the code to use in reflection information,
430 // to distinguish complex and complex64.
431 static Named_type*
432 make_complex_type(const char* name, int bits, int runtime_type_kind);
434 // Look up a named complex type.
435 static Named_type*
436 lookup_complex_type(const char* name);
438 // Get the unnamed string type.
439 static Type*
440 make_string_type();
442 // Get the named type "string".
443 static Named_type*
444 lookup_string_type();
446 // Make the named type "string".
447 static Named_type*
448 make_named_string_type();
450 static Type*
451 make_sink_type();
453 static Function_type*
454 make_function_type(Typed_identifier* receiver,
455 Typed_identifier_list* parameters,
456 Typed_identifier_list* results,
457 source_location);
459 static Pointer_type*
460 make_pointer_type(Type*);
462 static Type*
463 make_nil_type();
465 static Type*
466 make_call_multiple_result_type(Call_expression*);
468 static Struct_type*
469 make_struct_type(Struct_field_list* fields, source_location);
471 static Array_type*
472 make_array_type(Type* element_type, Expression* length);
474 static Map_type*
475 make_map_type(Type* key_type, Type* value_type, source_location);
477 static Channel_type*
478 make_channel_type(bool send, bool receive, Type*);
480 static Interface_type*
481 make_interface_type(Typed_identifier_list* methods, source_location);
483 static Type*
484 make_type_descriptor_type();
486 static Type*
487 make_type_descriptor_ptr_type();
489 static Named_type*
490 make_named_type(Named_object*, Type*, source_location);
492 static Type*
493 make_forward_declaration(Named_object*);
495 // Traverse a type.
496 static int
497 traverse(Type*, Traverse*);
499 // Verify the type. This is called after parsing, and verifies that
500 // types are complete and meet the language requirements. This
501 // returns false if the type is invalid.
502 bool
503 verify()
504 { return this->do_verify(); }
506 // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
507 // returns that an erroneous type is identical to any other type;
508 // this is used to avoid cascading errors. If this returns false,
509 // and REASON is not NULL, it may set *REASON.
510 static bool
511 are_identical(const Type* lhs, const Type* rhs, bool errors_are_identical,
512 std::string* reason);
514 // Return true if two types are compatible for use in a binary
515 // operation, other than a shift, comparison, or channel send. This
516 // is an equivalence relation.
517 static bool
518 are_compatible_for_binop(const Type* t1, const Type* t2);
520 // Return true if a value with type RHS is assignable to a variable
521 // with type LHS. This is not an equivalence relation. If this
522 // returns false, and REASON is not NULL, it sets *REASON.
523 static bool
524 are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
526 // Return true if a value with type RHS may be converted to type
527 // LHS. If this returns false, and REASON is not NULL, it sets
528 // *REASON.
529 static bool
530 are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
532 // Whether this type has any hidden fields which are not visible in
533 // the current compilation, such as a field whose name begins with a
534 // lower case letter in a struct imported from a different package.
535 // WITHIN is not NULL if we are looking at fields in a named type.
536 bool
537 has_hidden_fields(const Named_type* within, std::string* reason) const;
539 // Return a hash code for this type for the method hash table.
540 // Types which are equivalent according to are_identical will have
541 // the same hash code.
542 unsigned int
543 hash_for_method(Gogo*) const;
545 // Return the type classification.
546 Type_classification
547 classification() const
548 { return this->classification_; }
550 // Return the base type for this type. This looks through forward
551 // declarations and names. Using this with a forward declaration
552 // which has not been defined will return an error type.
553 Type*
554 base();
556 const Type*
557 base() const;
559 // Return the type skipping defined forward declarations. If this
560 // type is a forward declaration which has not been defined, it will
561 // return the Forward_declaration_type. This differs from base() in
562 // that it will return a Named_type, and for a
563 // Forward_declaration_type which is not defined it will return that
564 // type rather than an error type.
565 Type*
566 forwarded();
568 const Type*
569 forwarded() const;
571 // Return true if this is a basic type: a type which is not composed
572 // of other types, and is not void.
573 bool
574 is_basic_type() const;
576 // Return true if this is an abstract type--an integer, floating
577 // point, or complex type whose size has not been determined.
578 bool
579 is_abstract() const;
581 // Return a non-abstract version of an abstract type.
582 Type*
583 make_non_abstract_type();
585 // Return true if this type is or contains a pointer. This
586 // determines whether the garbage collector needs to look at a value
587 // of this type.
588 bool
589 has_pointer() const
590 { return this->do_has_pointer(); }
592 // Return true if this is an error type. An error type indicates a
593 // parsing error.
594 bool
595 is_error_type() const;
597 // Return true if this is a void type.
598 bool
599 is_void_type() const
600 { return this->classification_ == TYPE_VOID; }
602 // If this is an integer type, return the Integer_type. Otherwise,
603 // return NULL. This is a controlled dynamic_cast.
604 Integer_type*
605 integer_type()
606 { return this->convert<Integer_type, TYPE_INTEGER>(); }
608 const Integer_type*
609 integer_type() const
610 { return this->convert<const Integer_type, TYPE_INTEGER>(); }
612 // If this is a floating point type, return the Float_type.
613 // Otherwise, return NULL. This is a controlled dynamic_cast.
614 Float_type*
615 float_type()
616 { return this->convert<Float_type, TYPE_FLOAT>(); }
618 const Float_type*
619 float_type() const
620 { return this->convert<const Float_type, TYPE_FLOAT>(); }
622 // If this is a complex type, return the Complex_type. Otherwise,
623 // return NULL.
624 Complex_type*
625 complex_type()
626 { return this->convert<Complex_type, TYPE_COMPLEX>(); }
628 const Complex_type*
629 complex_type() const
630 { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
632 // Return true if this is a boolean type.
633 bool
634 is_boolean_type() const
635 { return this->base()->classification_ == TYPE_BOOLEAN; }
637 // Return true if this is an abstract boolean type.
638 bool
639 is_abstract_boolean_type() const
640 { return this->classification_ == TYPE_BOOLEAN; }
642 // Return true if this is a string type.
643 bool
644 is_string_type() const
645 { return this->base()->classification_ == TYPE_STRING; }
647 // Return true if this is an abstract string type.
648 bool
649 is_abstract_string_type() const
650 { return this->classification_ == TYPE_STRING; }
652 // Return true if this is the sink type. This is the type of the
653 // blank identifier _.
654 bool
655 is_sink_type() const
656 { return this->base()->classification_ == TYPE_SINK; }
658 // If this is a function type, return it. Otherwise, return NULL.
659 Function_type*
660 function_type()
661 { return this->convert<Function_type, TYPE_FUNCTION>(); }
663 const Function_type*
664 function_type() const
665 { return this->convert<const Function_type, TYPE_FUNCTION>(); }
667 // If this is a pointer type, return the type to which it points.
668 // Otherwise, return NULL.
669 Type*
670 points_to() const;
672 // If this is a pointer type, return the type to which it points.
673 // Otherwise, return the type itself.
674 Type*
675 deref()
677 Type* pt = this->points_to();
678 return pt != NULL ? pt : this;
681 const Type*
682 deref() const
684 const Type* pt = this->points_to();
685 return pt != NULL ? pt : this;
688 // Return true if this is the nil type. We don't use base() here,
689 // because this can be called during parse, and there is no way to
690 // name the nil type anyhow.
691 bool
692 is_nil_type() const
693 { return this->classification_ == TYPE_NIL; }
695 // Return true if this is the predeclared constant nil being used as
696 // a type. This is what the parser produces for type switches which
697 // use "case nil".
698 bool
699 is_nil_constant_as_type() const;
701 // Return true if this is the return type of a function which
702 // returns multiple values.
703 bool
704 is_call_multiple_result_type() const
705 { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
707 // If this is a struct type, return it. Otherwise, return NULL.
708 Struct_type*
709 struct_type()
710 { return this->convert<Struct_type, TYPE_STRUCT>(); }
712 const Struct_type*
713 struct_type() const
714 { return this->convert<const Struct_type, TYPE_STRUCT>(); }
716 // If this is an array type, return it. Otherwise, return NULL.
717 Array_type*
718 array_type()
719 { return this->convert<Array_type, TYPE_ARRAY>(); }
721 const Array_type*
722 array_type() const
723 { return this->convert<const Array_type, TYPE_ARRAY>(); }
725 // Return whether if this is an open array type.
726 bool
727 is_open_array_type() const;
729 // If this is a map type, return it. Otherwise, return NULL.
730 Map_type*
731 map_type()
732 { return this->convert<Map_type, TYPE_MAP>(); }
734 const Map_type*
735 map_type() const
736 { return this->convert<const Map_type, TYPE_MAP>(); }
738 // If this is a channel type, return it. Otherwise, return NULL.
739 Channel_type*
740 channel_type()
741 { return this->convert<Channel_type, TYPE_CHANNEL>(); }
743 const Channel_type*
744 channel_type() const
745 { return this->convert<const Channel_type, TYPE_CHANNEL>(); }
747 // If this is an interface type, return it. Otherwise, return NULL.
748 Interface_type*
749 interface_type()
750 { return this->convert<Interface_type, TYPE_INTERFACE>(); }
752 const Interface_type*
753 interface_type() const
754 { return this->convert<const Interface_type, TYPE_INTERFACE>(); }
756 // If this is a named type, return it. Otherwise, return NULL.
757 Named_type*
758 named_type();
760 const Named_type*
761 named_type() const;
763 // If this is a forward declaration, return it. Otherwise, return
764 // NULL.
765 Forward_declaration_type*
766 forward_declaration_type()
767 { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
769 const Forward_declaration_type*
770 forward_declaration_type() const
772 return this->convert_no_base<const Forward_declaration_type,
773 TYPE_FORWARD>();
776 // Return true if this type is not yet defined.
777 bool
778 is_undefined() const;
780 // Return true if this is the unsafe.pointer type. We currently
781 // represent that as pointer-to-void.
782 bool
783 is_unsafe_pointer_type() const
784 { return this->points_to() != NULL && this->points_to()->is_void_type(); }
786 // Look for field or method NAME for TYPE. Return an expression for
787 // it, bound to EXPR.
788 static Expression*
789 bind_field_or_method(Gogo*, const Type* type, Expression* expr,
790 const std::string& name, source_location);
792 // Return true if NAME is an unexported field or method of TYPE.
793 static bool
794 is_unexported_field_or_method(Gogo*, const Type*, const std::string&);
796 // This type was passed to the builtin function make. ARGS are the
797 // arguments passed to make after the type; this may be NULL if
798 // there were none. Issue any required errors.
799 bool
800 check_make_expression(Expression_list* args, source_location location)
801 { return this->do_check_make_expression(args, location); }
803 // Return a tree representing this type.
804 tree
805 get_tree(Gogo*);
807 // Return a tree representing a zero initialization for this type.
808 // This will be something like an INTEGER_CST or a CONSTRUCTOR. If
809 // IS_CLEAR is true, then the memory is known to be zeroed; in that
810 // case, this will return NULL if there is nothing to be done.
811 tree
812 get_init_tree(Gogo*, bool is_clear);
814 // Like get_init_tree, but passing in the type to use for the
815 // initializer.
816 tree
817 get_typed_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
818 { return this->do_get_init_tree(gogo, type_tree, is_clear); }
820 // Return a tree for a make expression applied to this type.
821 tree
822 make_expression_tree(Translate_context* context, Expression_list* args,
823 source_location location)
824 { return this->do_make_expression_tree(context, args, location); }
826 // Build a type descriptor entry for this type. Return a pointer to
827 // it.
828 tree
829 type_descriptor_pointer(Gogo* gogo);
831 // Return the type reflection string for this type.
832 std::string
833 reflection(Gogo*) const;
835 // Return a mangled name for the type. This is a name which can be
836 // used in assembler code. Identical types should have the same
837 // manged name.
838 std::string
839 mangled_name(Gogo*) const;
841 // Export the type.
842 void
843 export_type(Export* exp) const
844 { this->do_export(exp); }
846 // Import a type.
847 static Type*
848 import_type(Import*);
850 protected:
851 Type(Type_classification);
853 // Functions implemented by the child class.
855 // Traverse the subtypes.
856 virtual int
857 do_traverse(Traverse*);
859 // Verify the type.
860 virtual bool
861 do_verify()
862 { return true; }
864 virtual bool
865 do_has_pointer() const
866 { return false; }
868 virtual unsigned int
869 do_hash_for_method(Gogo*) const;
871 virtual bool
872 do_check_make_expression(Expression_list* args, source_location);
875 virtual tree
876 do_get_tree(Gogo*) = 0;
878 virtual tree
879 do_get_init_tree(Gogo*, tree, bool) = 0;
881 virtual tree
882 do_make_expression_tree(Translate_context*, Expression_list*,
883 source_location);
885 virtual Expression*
886 do_type_descriptor(Gogo*, Named_type* name) = 0;
888 virtual void
889 do_reflection(Gogo*, std::string*) const = 0;
892 virtual void
893 do_mangled_name(Gogo*, std::string*) const = 0;
895 virtual void
896 do_export(Export*) const;
898 // Return whether an expression is an integer.
899 static bool
900 check_int_value(Expression*, const char*, source_location);
902 // Return whether a method expects a pointer as the receiver.
903 static bool
904 method_expects_pointer(const Named_object*);
906 // Finalize the methods for a type.
907 static void
908 finalize_methods(Gogo*, const Type*, source_location, Methods**);
910 // Return a method from a set of methods.
911 static Method*
912 method_function(const Methods*, const std::string& name,
913 bool* is_ambiguous);
915 // Return a composite literal for the type descriptor entry for a
916 // type.
917 static Expression*
918 type_descriptor(Gogo*, Type*);
920 // Return a composite literal for the type descriptor entry for
921 // TYPE, using NAME as the name of the type.
922 static Expression*
923 named_type_descriptor(Gogo*, Type* type, Named_type* name);
925 // Return a composite literal for a plain type descriptor for this
926 // type with the given kind and name.
927 Expression*
928 plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
930 // Build a composite literal for the basic type descriptor.
931 Expression*
932 type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
933 const Methods*, bool only_value_methods);
935 // Make a builtin struct type from a list of fields.
936 static Struct_type*
937 make_builtin_struct_type(int nfields, ...);
939 // Make a builtin named type.
940 static Named_type*
941 make_builtin_named_type(const char* name, Type* type);
943 // For the benefit of child class reflection string generation.
944 void
945 append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
946 { type->do_reflection(gogo, ret); }
948 // For the benefit of child class mangling.
949 void
950 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
951 { type->do_mangled_name(gogo, ret); }
953 // Incorporate a string into a hash code.
954 static unsigned int
955 hash_string(const std::string&, unsigned int);
957 // Return a tree for the underlying type of a named type.
958 static tree
959 get_named_type_tree(Gogo* gogo, Type* base_type)
960 { return base_type->get_tree_without_hash(gogo); }
962 private:
963 // Convert to the desired type classification, or return NULL. This
964 // is a controlled dynamic_cast.
965 template<typename Type_class, Type_classification type_classification>
966 Type_class*
967 convert()
969 Type* base = this->base();
970 return (base->classification_ == type_classification
971 ? static_cast<Type_class*>(base)
972 : NULL);
975 template<typename Type_class, Type_classification type_classification>
976 const Type_class*
977 convert() const
979 const Type* base = this->base();
980 return (base->classification_ == type_classification
981 ? static_cast<Type_class*>(base)
982 : NULL);
985 template<typename Type_class, Type_classification type_classification>
986 Type_class*
987 convert_no_base()
989 return (this->classification_ == type_classification
990 ? static_cast<Type_class*>(this)
991 : NULL);
994 template<typename Type_class, Type_classification type_classification>
995 const Type_class*
996 convert_no_base() const
998 return (this->classification_ == type_classification
999 ? static_cast<Type_class*>(this)
1000 : NULL);
1003 // Get the hash and equality functions for a type.
1004 void
1005 type_functions(const char** hash_fn, const char** equal_fn) const;
1007 // Build a composite literal for the uncommon type information.
1008 Expression*
1009 uncommon_type_constructor(Gogo*, Type* uncommon_type,
1010 Named_type*, const Methods*,
1011 bool only_value_methods) const;
1013 // Build a composite literal for the methods.
1014 Expression*
1015 methods_constructor(Gogo*, Type* methods_type, const Methods*,
1016 bool only_value_methods) const;
1018 // Build a composite literal for one method.
1019 Expression*
1020 method_constructor(Gogo*, Type* method_type, const std::string& name,
1021 const Method*) const;
1023 static tree
1024 build_receive_return_type(tree type);
1026 // A hash table we use to avoid infinite recursion.
1027 typedef Unordered_set_hash(const Named_type*, Type_hash_identical,
1028 Type_identical) Types_seen;
1030 // Add all methods for TYPE to the list of methods for THIS.
1031 static void
1032 add_methods_for_type(const Type* type, const Method::Field_indexes*,
1033 unsigned int depth, bool, bool, Types_seen*,
1034 Methods**);
1036 static void
1037 add_local_methods_for_type(const Named_type* type,
1038 const Method::Field_indexes*,
1039 unsigned int depth, bool, bool, Methods**);
1041 static void
1042 add_embedded_methods_for_type(const Type* type,
1043 const Method::Field_indexes*,
1044 unsigned int depth, bool, bool, Types_seen*,
1045 Methods**);
1047 static void
1048 add_interface_methods_for_type(const Type* type,
1049 const Method::Field_indexes*,
1050 unsigned int depth, Methods**);
1052 // Build stub methods for a type.
1053 static void
1054 build_stub_methods(Gogo*, const Type* type, const Methods* methods,
1055 source_location);
1057 static void
1058 build_one_stub_method(Gogo*, Method*, const char* receiver_name,
1059 const Typed_identifier_list*, bool is_varargs,
1060 source_location);
1062 static Expression*
1063 apply_field_indexes(Expression*, const Method::Field_indexes*,
1064 source_location);
1066 // Look for a field or method named NAME in TYPE.
1067 static bool
1068 find_field_or_method(const Type* type, const std::string& name,
1069 bool receiver_can_be_pointer,
1070 int* level, bool* is_method,
1071 bool* found_pointer_method,
1072 std::string* ambig1, std::string* ambig2);
1074 // Get a tree for a type without looking in the hash table for
1075 // identical types.
1076 tree
1077 get_tree_without_hash(Gogo*);
1079 // A mapping from Type to tree, used to ensure that the GIMPLE
1080 // representation of identical types is identical.
1081 typedef Unordered_map_hash(const Type*, tree, Type_hash_identical,
1082 Type_identical) Type_trees;
1084 static Type_trees type_trees;
1086 // The type classification.
1087 Type_classification classification_;
1088 // The tree representation of the type, once it has been determined.
1089 tree tree_;
1090 // The decl for the type descriptor for this type. This starts out
1091 // as NULL and is filled in as needed.
1092 tree type_descriptor_decl_;
1095 // Type hash table operations.
1097 class Type_hash_identical
1099 public:
1100 unsigned int
1101 operator()(const Type* type) const
1102 { return type->hash_for_method(NULL); }
1105 class Type_identical
1107 public:
1108 bool
1109 operator()(const Type* t1, const Type* t2) const
1110 { return Type::are_identical(t1, t2, false, NULL); }
1113 // An identifier with a type.
1115 class Typed_identifier
1117 public:
1118 Typed_identifier(const std::string& name, Type* type,
1119 source_location location)
1120 : name_(name), type_(type), location_(location)
1123 // Get the name.
1124 const std::string&
1125 name() const
1126 { return this->name_; }
1128 // Get the type.
1129 Type*
1130 type() const
1131 { return this->type_; }
1133 // Return the location where the name was seen. This is not always
1134 // meaningful.
1135 source_location
1136 location() const
1137 { return this->location_; }
1139 // Set the type--sometimes we see the identifier before the type.
1140 void
1141 set_type(Type* type)
1143 gcc_assert(this->type_ == NULL || type->is_error_type());
1144 this->type_ = type;
1147 private:
1148 // Identifier name.
1149 std::string name_;
1150 // Type.
1151 Type* type_;
1152 // The location where the name was seen.
1153 source_location location_;
1156 // A list of Typed_identifiers.
1158 class Typed_identifier_list
1160 public:
1161 Typed_identifier_list()
1162 : entries_()
1165 // Whether the list is empty.
1166 bool
1167 empty() const
1168 { return this->entries_.empty(); }
1170 // Return the number of entries in the list.
1171 size_t
1172 size() const
1173 { return this->entries_.size(); }
1175 // Add an entry to the end of the list.
1176 void
1177 push_back(const Typed_identifier& td)
1178 { this->entries_.push_back(td); }
1180 // Remove an entry from the end of the list.
1181 void
1182 pop_back()
1183 { this->entries_.pop_back(); }
1185 // Set the type of entry I to TYPE.
1186 void
1187 set_type(size_t i, Type* type)
1189 gcc_assert(i < this->entries_.size());
1190 this->entries_[i].set_type(type);
1193 // Sort the entries by name.
1194 void
1195 sort_by_name();
1197 // Traverse types.
1199 traverse(Traverse*);
1201 // Return the first and last elements.
1202 Typed_identifier&
1203 front()
1204 { return this->entries_.front(); }
1206 const Typed_identifier&
1207 front() const
1208 { return this->entries_.front(); }
1210 Typed_identifier&
1211 back()
1212 { return this->entries_.back(); }
1214 const Typed_identifier&
1215 back() const
1216 { return this->entries_.back(); }
1218 const Typed_identifier&
1219 at(size_t i) const
1220 { return this->entries_.at(i); }
1222 void
1223 set(size_t i, const Typed_identifier& t)
1224 { this->entries_.at(i) = t; }
1226 void
1227 resize(size_t c)
1229 gcc_assert(c <= this->entries_.size());
1230 this->entries_.resize(c, Typed_identifier("", NULL, UNKNOWN_LOCATION));
1233 // Iterators.
1235 typedef std::vector<Typed_identifier>::iterator iterator;
1236 typedef std::vector<Typed_identifier>::const_iterator const_iterator;
1238 iterator
1239 begin()
1240 { return this->entries_.begin(); }
1242 const_iterator
1243 begin() const
1244 { return this->entries_.begin(); }
1246 iterator
1247 end()
1248 { return this->entries_.end(); }
1250 const_iterator
1251 end() const
1252 { return this->entries_.end(); }
1254 // Return a copy of this list. This returns an independent copy of
1255 // the vector, but does not copy the types.
1256 Typed_identifier_list*
1257 copy() const;
1259 private:
1260 std::vector<Typed_identifier> entries_;
1263 // The type of an integer.
1265 class Integer_type : public Type
1267 public:
1268 // Create a new integer type.
1269 static Named_type*
1270 create_integer_type(const char* name, bool is_unsigned, int bits,
1271 int runtime_type_kind);
1273 // Look up an existing integer type.
1274 static Named_type*
1275 lookup_integer_type(const char* name);
1277 // Create an abstract integer type.
1278 static Integer_type*
1279 create_abstract_integer_type();
1281 // Whether this is an abstract integer type.
1282 bool
1283 is_abstract() const
1284 { return this->is_abstract_; }
1286 // Whether this is an unsigned type.
1287 bool
1288 is_unsigned() const
1289 { return this->is_unsigned_; }
1291 // The number of bits.
1293 bits() const
1294 { return this->bits_; }
1296 // Whether this type is the same as T.
1297 bool
1298 is_identical(const Integer_type* t) const;
1300 protected:
1301 unsigned int
1302 do_hash_for_method(Gogo*) const;
1304 tree
1305 do_get_tree(Gogo*);
1307 tree
1308 do_get_init_tree(Gogo*, tree, bool);
1310 Expression*
1311 do_type_descriptor(Gogo*, Named_type*);
1313 void
1314 do_reflection(Gogo*, std::string*) const;
1316 void
1317 do_mangled_name(Gogo*, std::string*) const;
1319 private:
1320 Integer_type(bool is_abstract, bool is_unsigned, int bits,
1321 int runtime_type_kind)
1322 : Type(TYPE_INTEGER),
1323 is_abstract_(is_abstract), is_unsigned_(is_unsigned), bits_(bits),
1324 runtime_type_kind_(runtime_type_kind)
1327 // Map names of integer types to the types themselves.
1328 typedef std::map<std::string, Named_type*> Named_integer_types;
1329 static Named_integer_types named_integer_types;
1331 // True if this is an abstract type.
1332 bool is_abstract_;
1333 // True if this is an unsigned type.
1334 bool is_unsigned_;
1335 // The number of bits.
1336 int bits_;
1337 // The runtime type code used in the type descriptor for this type.
1338 int runtime_type_kind_;
1341 // The type of a floating point number.
1343 class Float_type : public Type
1345 public:
1346 // Create a new float type.
1347 static Named_type*
1348 create_float_type(const char* name, int bits, int runtime_type_kind);
1350 // Look up an existing float type.
1351 static Named_type*
1352 lookup_float_type(const char* name);
1354 // Create an abstract float type.
1355 static Float_type*
1356 create_abstract_float_type();
1358 // Whether this is an abstract float type.
1359 bool
1360 is_abstract() const
1361 { return this->is_abstract_; }
1363 // The number of bits.
1365 bits() const
1366 { return this->bits_; }
1368 // Whether this type is the same as T.
1369 bool
1370 is_identical(const Float_type* t) const;
1372 // Return a tree for this type without using a Gogo*.
1373 tree
1374 type_tree() const;
1376 protected:
1377 unsigned int
1378 do_hash_for_method(Gogo*) const;
1380 tree
1381 do_get_tree(Gogo*);
1383 tree
1384 do_get_init_tree(Gogo*, tree, bool);
1386 Expression*
1387 do_type_descriptor(Gogo*, Named_type*);
1389 void
1390 do_reflection(Gogo*, std::string*) const;
1392 void
1393 do_mangled_name(Gogo*, std::string*) const;
1395 private:
1396 Float_type(bool is_abstract, int bits, int runtime_type_kind)
1397 : Type(TYPE_FLOAT),
1398 is_abstract_(is_abstract), bits_(bits),
1399 runtime_type_kind_(runtime_type_kind)
1402 // Map names of float types to the types themselves.
1403 typedef std::map<std::string, Named_type*> Named_float_types;
1404 static Named_float_types named_float_types;
1406 // True if this is an abstract type.
1407 bool is_abstract_;
1408 // The number of bits in the floating point value.
1409 int bits_;
1410 // The runtime type code used in the type descriptor for this type.
1411 int runtime_type_kind_;
1414 // The type of a complex number.
1416 class Complex_type : public Type
1418 public:
1419 // Create a new complex type.
1420 static Named_type*
1421 create_complex_type(const char* name, int bits, int runtime_type_kind);
1423 // Look up an existing complex type.
1424 static Named_type*
1425 lookup_complex_type(const char* name);
1427 // Create an abstract complex type.
1428 static Complex_type*
1429 create_abstract_complex_type();
1431 // Whether this is an abstract complex type.
1432 bool
1433 is_abstract() const
1434 { return this->is_abstract_; }
1436 // The number of bits: 64 or 128.
1437 int bits() const
1438 { return this->bits_; }
1440 // Whether this type is the same as T.
1441 bool
1442 is_identical(const Complex_type* t) const;
1444 // Return a tree for this type without using a Gogo*.
1445 tree
1446 type_tree() const;
1448 protected:
1449 unsigned int
1450 do_hash_for_method(Gogo*) const;
1452 tree
1453 do_get_tree(Gogo*);
1455 tree
1456 do_get_init_tree(Gogo*, tree, bool);
1458 Expression*
1459 do_type_descriptor(Gogo*, Named_type*);
1461 void
1462 do_reflection(Gogo*, std::string*) const;
1464 void
1465 do_mangled_name(Gogo*, std::string*) const;
1467 private:
1468 Complex_type(bool is_abstract, int bits, int runtime_type_kind)
1469 : Type(TYPE_COMPLEX),
1470 is_abstract_(is_abstract), bits_(bits),
1471 runtime_type_kind_(runtime_type_kind)
1474 // Map names of complex types to the types themselves.
1475 typedef std::map<std::string, Named_type*> Named_complex_types;
1476 static Named_complex_types named_complex_types;
1478 // True if this is an abstract type.
1479 bool is_abstract_;
1480 // The number of bits in the complex value--64 or 128.
1481 int bits_;
1482 // The runtime type code used in the type descriptor for this type.
1483 int runtime_type_kind_;
1486 // The type of a string.
1488 class String_type : public Type
1490 public:
1491 String_type()
1492 : Type(TYPE_STRING)
1495 // Return a tree for the length of STRING.
1496 static tree
1497 length_tree(Gogo*, tree string);
1499 // Return a tree which points to the bytes of STRING.
1500 static tree
1501 bytes_tree(Gogo*, tree string);
1503 protected:
1504 bool
1505 do_has_pointer() const
1506 { return true; }
1508 tree
1509 do_get_tree(Gogo*);
1511 tree
1512 do_get_init_tree(Gogo* gogo, tree, bool);
1514 Expression*
1515 do_type_descriptor(Gogo*, Named_type*);
1517 void
1518 do_reflection(Gogo*, std::string*) const;
1520 void
1521 do_mangled_name(Gogo*, std::string* ret) const;
1523 private:
1524 // The named string type.
1525 static Named_type* string_type_;
1528 // The type of a function.
1530 class Function_type : public Type
1532 public:
1533 Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
1534 Typed_identifier_list* results, source_location location)
1535 : Type(TYPE_FUNCTION),
1536 receiver_(receiver), parameters_(parameters), results_(results),
1537 location_(location), is_varargs_(false), is_builtin_(false)
1540 // Get the receiver.
1541 const Typed_identifier*
1542 receiver() const
1543 { return this->receiver_; }
1545 // Get the return names and types.
1546 const Typed_identifier_list*
1547 results() const
1548 { return this->results_; }
1550 // Get the parameter names and types.
1551 const Typed_identifier_list*
1552 parameters() const
1553 { return this->parameters_; }
1555 // Whether this is a varargs function.
1556 bool
1557 is_varargs() const
1558 { return this->is_varargs_; }
1560 // Whether this is a builtin function.
1561 bool
1562 is_builtin() const
1563 { return this->is_builtin_; }
1565 // The location where this type was defined.
1566 source_location
1567 location() const
1568 { return this->location_; }
1570 // Return whether this is a method type.
1571 bool
1572 is_method() const
1573 { return this->receiver_ != NULL; }
1575 // Whether T is a valid redeclaration of this type. This is called
1576 // when a function is declared more than once.
1577 bool
1578 is_valid_redeclaration(const Function_type* t, std::string*) const;
1580 // Whether this type is the same as T.
1581 bool
1582 is_identical(const Function_type* t, bool ignore_receiver,
1583 bool errors_are_identical, std::string*) const;
1585 // Record that this is a varargs function.
1586 void
1587 set_is_varargs()
1588 { this->is_varargs_ = true; }
1590 // Record that this is a builtin function.
1591 void
1592 set_is_builtin()
1593 { this->is_builtin_ = true; }
1595 // Import a function type.
1596 static Function_type*
1597 do_import(Import*);
1599 // Return a copy of this type without a receiver. This is only
1600 // valid for a method type.
1601 Function_type*
1602 copy_without_receiver() const;
1604 // Return a copy of this type with a receiver. This is used when an
1605 // interface method is attached to a named or struct type.
1606 Function_type*
1607 copy_with_receiver(Type*) const;
1609 protected:
1611 do_traverse(Traverse*);
1613 // A trampoline function has a pointer which matters for GC.
1614 bool
1615 do_has_pointer() const
1616 { return true; }
1618 unsigned int
1619 do_hash_for_method(Gogo*) const;
1621 tree
1622 do_get_tree(Gogo*);
1624 tree
1625 do_get_init_tree(Gogo*, tree, bool);
1627 Expression*
1628 do_type_descriptor(Gogo*, Named_type*);
1630 void
1631 do_reflection(Gogo*, std::string*) const;
1633 void
1634 do_mangled_name(Gogo*, std::string*) const;
1636 void
1637 do_export(Export*) const;
1639 private:
1640 static Type*
1641 make_function_type_descriptor_type();
1643 Expression*
1644 type_descriptor_params(Type*, const Typed_identifier*,
1645 const Typed_identifier_list*);
1647 // The receiver name and type. This will be NULL for a normal
1648 // function, non-NULL for a method.
1649 Typed_identifier* receiver_;
1650 // The parameter names and types.
1651 Typed_identifier_list* parameters_;
1652 // The result names and types. This will be NULL if no result was
1653 // specified.
1654 Typed_identifier_list* results_;
1655 // The location where this type was defined. This exists solely to
1656 // give a location for the fields of the struct if this function
1657 // returns multiple values.
1658 source_location location_;
1659 // Whether this function takes a variable number of arguments.
1660 bool is_varargs_;
1661 // Whether this is a special builtin function which can not simply
1662 // be called. This is used for len, cap, etc.
1663 bool is_builtin_;
1666 // The type of a pointer.
1668 class Pointer_type : public Type
1670 public:
1671 Pointer_type(Type* to_type)
1672 : Type(TYPE_POINTER),
1673 to_type_(to_type)
1676 Type*
1677 points_to() const
1678 { return this->to_type_; }
1680 // Import a pointer type.
1681 static Pointer_type*
1682 do_import(Import*);
1684 protected:
1686 do_traverse(Traverse*);
1688 bool
1689 do_has_pointer() const
1690 { return true; }
1692 unsigned int
1693 do_hash_for_method(Gogo*) const;
1695 tree
1696 do_get_tree(Gogo*);
1698 tree
1699 do_get_init_tree(Gogo*, tree, bool);
1701 Expression*
1702 do_type_descriptor(Gogo*, Named_type*);
1704 void
1705 do_reflection(Gogo*, std::string*) const;
1707 void
1708 do_mangled_name(Gogo*, std::string*) const;
1710 void
1711 do_export(Export*) const;
1713 private:
1714 static Type*
1715 make_pointer_type_descriptor_type();
1717 // The type to which this type points.
1718 Type* to_type_;
1721 // The type of a field in a struct.
1723 class Struct_field
1725 public:
1726 explicit Struct_field(const Typed_identifier& typed_identifier)
1727 : typed_identifier_(typed_identifier), tag_(NULL)
1730 // The field name.
1731 const std::string&
1732 field_name() const;
1734 // The field type.
1735 Type*
1736 type() const
1737 { return this->typed_identifier_.type(); }
1739 // The field location.
1740 source_location
1741 location() const
1742 { return this->typed_identifier_.location(); }
1744 // Whether the field has a tag.
1745 bool
1746 has_tag() const
1747 { return this->tag_ != NULL; }
1749 // The tag.
1750 const std::string&
1751 tag() const
1753 gcc_assert(this->tag_ != NULL);
1754 return *this->tag_;
1757 // Whether this is an anonymous field.
1758 bool
1759 is_anonymous() const
1760 { return this->typed_identifier_.name().empty(); }
1762 // Set the tag. FIXME: This is never freed.
1763 void
1764 set_tag(const std::string& tag)
1765 { this->tag_ = new std::string(tag); }
1767 // Set the type. This is only used in error cases.
1768 void
1769 set_type(Type* type)
1770 { this->typed_identifier_.set_type(type); }
1772 private:
1773 // The field name, type, and location.
1774 Typed_identifier typed_identifier_;
1775 // The field tag. This is NULL if the field has no tag.
1776 std::string* tag_;
1779 // A list of struct fields.
1781 class Struct_field_list
1783 public:
1784 Struct_field_list()
1785 : entries_()
1788 // Whether the list is empty.
1789 bool
1790 empty() const
1791 { return this->entries_.empty(); }
1793 // Return the number of entries.
1794 size_t
1795 size() const
1796 { return this->entries_.size(); }
1798 // Add an entry to the end of the list.
1799 void
1800 push_back(const Struct_field& sf)
1801 { this->entries_.push_back(sf); }
1803 // Index into the list.
1804 const Struct_field&
1805 at(size_t i) const
1806 { return this->entries_.at(i); }
1808 // Last entry in list.
1809 Struct_field&
1810 back()
1811 { return this->entries_.back(); }
1813 // Iterators.
1815 typedef std::vector<Struct_field>::iterator iterator;
1816 typedef std::vector<Struct_field>::const_iterator const_iterator;
1818 iterator
1819 begin()
1820 { return this->entries_.begin(); }
1822 const_iterator
1823 begin() const
1824 { return this->entries_.begin(); }
1826 iterator
1827 end()
1828 { return this->entries_.end(); }
1830 const_iterator
1831 end() const
1832 { return this->entries_.end(); }
1834 private:
1835 std::vector<Struct_field> entries_;
1838 // The type of a struct.
1840 class Struct_type : public Type
1842 public:
1843 Struct_type(Struct_field_list* fields, source_location location)
1844 : Type(TYPE_STRUCT),
1845 fields_(fields), location_(location), all_methods_(NULL)
1848 // Return the field NAME. This only looks at local fields, not at
1849 // embedded types. If the field is found, and PINDEX is not NULL,
1850 // this sets *PINDEX to the field index. If the field is not found,
1851 // this returns NULL.
1852 const Struct_field*
1853 find_local_field(const std::string& name, unsigned int *pindex) const;
1855 // Return the field number INDEX.
1856 const Struct_field*
1857 field(unsigned int index) const
1858 { return &this->fields_->at(index); }
1860 // Get the struct fields.
1861 const Struct_field_list*
1862 fields() const
1863 { return this->fields_; }
1865 // Return the number of fields.
1866 size_t
1867 field_count() const
1868 { return this->fields_->size(); }
1870 // Push a new field onto the end of the struct. This is used when
1871 // building a closure variable.
1872 void
1873 push_field(const Struct_field& sf)
1874 { this->fields_->push_back(sf); }
1876 // Return an expression referring to field NAME in STRUCT_EXPR, or
1877 // NULL if there is no field with that name.
1878 Field_reference_expression*
1879 field_reference(Expression* struct_expr, const std::string& name,
1880 source_location) const;
1882 // Return the total number of fields, including embedded fields.
1883 // This is the number of values which can appear in a conversion to
1884 // this type.
1885 unsigned int
1886 total_field_count() const;
1888 // Whether this type is identical with T.
1889 bool
1890 is_identical(const Struct_type* t, bool errors_are_identical) const;
1892 // Whether this struct type has any hidden fields. This returns
1893 // true if any fields have hidden names, or if any non-pointer
1894 // anonymous fields have types with hidden fields.
1895 bool
1896 struct_has_hidden_fields(const Named_type* within, std::string*) const;
1898 // Return whether NAME is a local field which is not exported. This
1899 // is only used for better error reporting.
1900 bool
1901 is_unexported_local_field(Gogo*, const std::string& name) const;
1903 // If this is an unnamed struct, build the complete list of methods,
1904 // including those from anonymous fields, and build methods stubs if
1905 // needed.
1906 void
1907 finalize_methods(Gogo*);
1909 // Return whether this type has any methods. This should only be
1910 // called after the finalize_methods pass.
1911 bool
1912 has_any_methods() const
1913 { return this->all_methods_ != NULL; }
1915 // Return the methods for tihs type. This should only be called
1916 // after the finalize_methods pass.
1917 const Methods*
1918 methods() const
1919 { return this->all_methods_; }
1921 // Return the method to use for NAME. This returns NULL if there is
1922 // no such method or if the method is ambiguous. When it returns
1923 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
1924 Method*
1925 method_function(const std::string& name, bool* is_ambiguous) const;
1927 // Traverse just the field types of a struct type.
1929 traverse_field_types(Traverse* traverse)
1930 { return this->do_traverse(traverse); }
1932 // Import a struct type.
1933 static Struct_type*
1934 do_import(Import*);
1936 // Fill in the fields for a named struct type.
1937 tree
1938 fill_in_tree(Gogo*, tree);
1940 protected:
1942 do_traverse(Traverse*);
1944 bool
1945 do_verify();
1947 bool
1948 do_has_pointer() const;
1950 unsigned int
1951 do_hash_for_method(Gogo*) const;
1953 tree
1954 do_get_tree(Gogo*);
1956 tree
1957 do_get_init_tree(Gogo*, tree, bool);
1959 Expression*
1960 do_type_descriptor(Gogo*, Named_type*);
1962 void
1963 do_reflection(Gogo*, std::string*) const;
1965 void
1966 do_mangled_name(Gogo*, std::string*) const;
1968 void
1969 do_export(Export*) const;
1971 private:
1972 Field_reference_expression*
1973 field_reference_depth(Expression* struct_expr, const std::string& name,
1974 source_location, unsigned int* depth) const;
1976 static Type*
1977 make_struct_type_descriptor_type();
1979 // The fields of the struct.
1980 Struct_field_list* fields_;
1981 // The place where the struct was declared.
1982 source_location location_;
1983 // If this struct is unnamed, a list of methods.
1984 Methods* all_methods_;
1987 // The type of an array.
1989 class Array_type : public Type
1991 public:
1992 Array_type(Type* element_type, Expression* length)
1993 : Type(TYPE_ARRAY),
1994 element_type_(element_type), length_(length), length_tree_(NULL)
1997 // Return the element type.
1998 Type*
1999 element_type() const
2000 { return this->element_type_; }
2002 // Return the length. This will return NULL for an open array.
2003 Expression*
2004 length() const
2005 { return this->length_; }
2007 // Whether this type is identical with T.
2008 bool
2009 is_identical(const Array_type* t, bool errors_are_identical) const;
2011 // Whether this type has any hidden fields.
2012 bool
2013 array_has_hidden_fields(const Named_type* within, std::string* reason) const
2014 { return this->element_type_->has_hidden_fields(within, reason); }
2016 // Return a tree for the pointer to the values in an array.
2017 tree
2018 value_pointer_tree(Gogo*, tree array) const;
2020 // Return a tree for the length of an array with this type.
2021 tree
2022 length_tree(Gogo*, tree array);
2024 // Return a tree for the capacity of an array with this type.
2025 tree
2026 capacity_tree(Gogo*, tree array);
2028 // Import an array type.
2029 static Array_type*
2030 do_import(Import*);
2032 // Fill in the fields for a named slice type.
2033 tree
2034 fill_in_tree(Gogo*, tree);
2036 protected:
2038 do_traverse(Traverse* traverse);
2040 bool
2041 do_verify();
2043 bool
2044 do_has_pointer() const
2046 return this->length_ == NULL || this->element_type_->has_pointer();
2049 unsigned int
2050 do_hash_for_method(Gogo*) const;
2052 bool
2053 do_check_make_expression(Expression_list*, source_location);
2055 tree
2056 do_get_tree(Gogo*);
2058 tree
2059 do_get_init_tree(Gogo*, tree, bool);
2061 tree
2062 do_make_expression_tree(Translate_context*, Expression_list*,
2063 source_location);
2065 Expression*
2066 do_type_descriptor(Gogo*, Named_type*);
2068 void
2069 do_reflection(Gogo*, std::string*) const;
2071 void
2072 do_mangled_name(Gogo*, std::string*) const;
2074 void
2075 do_export(Export*) const;
2077 private:
2078 bool
2079 verify_length();
2081 tree
2082 get_length_tree(Gogo*);
2084 Type*
2085 make_array_type_descriptor_type();
2087 Type*
2088 make_slice_type_descriptor_type();
2090 Expression*
2091 array_type_descriptor(Gogo*, Named_type*);
2093 Expression*
2094 slice_type_descriptor(Gogo*, Named_type*);
2096 // The type of elements of the array.
2097 Type* element_type_;
2098 // The number of elements. This may be NULL.
2099 Expression* length_;
2100 // The length as a tree. We only want to compute this once.
2101 tree length_tree_;
2104 // The type of a map.
2106 class Map_type : public Type
2108 public:
2109 Map_type(Type* key_type, Type* val_type, source_location location)
2110 : Type(TYPE_MAP),
2111 key_type_(key_type), val_type_(val_type), location_(location)
2114 // Return the key type.
2115 Type*
2116 key_type() const
2117 { return this->key_type_; }
2119 // Return the value type.
2120 Type*
2121 val_type() const
2122 { return this->val_type_; }
2124 // Whether this type is identical with T.
2125 bool
2126 is_identical(const Map_type* t, bool errors_are_identical) const;
2128 // Import a map type.
2129 static Map_type*
2130 do_import(Import*);
2132 protected:
2134 do_traverse(Traverse*);
2136 bool
2137 do_verify();
2139 bool
2140 do_has_pointer() const
2141 { return true; }
2143 unsigned int
2144 do_hash_for_method(Gogo*) const;
2146 bool
2147 do_check_make_expression(Expression_list*, source_location);
2149 tree
2150 do_get_tree(Gogo*);
2152 tree
2153 do_get_init_tree(Gogo*, tree, bool);
2155 tree
2156 do_make_expression_tree(Translate_context*, Expression_list*,
2157 source_location);
2159 Expression*
2160 do_type_descriptor(Gogo*, Named_type*);
2162 void
2163 do_reflection(Gogo*, std::string*) const;
2165 void
2166 do_mangled_name(Gogo*, std::string*) const;
2168 void
2169 do_export(Export*) const;
2171 private:
2172 static Type*
2173 make_map_type_descriptor_type();
2175 // The key type.
2176 Type* key_type_;
2177 // The value type.
2178 Type* val_type_;
2179 // Where the type was defined.
2180 source_location location_;
2183 // The type of a channel.
2185 class Channel_type : public Type
2187 public:
2188 Channel_type(bool may_send, bool may_receive, Type* element_type)
2189 : Type(TYPE_CHANNEL),
2190 may_send_(may_send), may_receive_(may_receive),
2191 element_type_(element_type)
2192 { gcc_assert(may_send || may_receive); }
2194 // Whether this channel can send data.
2195 bool
2196 may_send() const
2197 { return this->may_send_; }
2199 // Whether this channel can receive data.
2200 bool
2201 may_receive() const
2202 { return this->may_receive_; }
2204 // The type of the values that may be sent on this channel. This is
2205 // NULL if any type may be sent.
2206 Type*
2207 element_type() const
2208 { return this->element_type_; }
2210 // Whether this type is identical with T.
2211 bool
2212 is_identical(const Channel_type* t, bool errors_are_identical) const;
2214 // Import a channel type.
2215 static Channel_type*
2216 do_import(Import*);
2218 protected:
2220 do_traverse(Traverse* traverse)
2221 { return Type::traverse(this->element_type_, traverse); }
2223 bool
2224 do_has_pointer() const
2225 { return true; }
2227 unsigned int
2228 do_hash_for_method(Gogo*) const;
2230 bool
2231 do_check_make_expression(Expression_list*, source_location);
2233 tree
2234 do_get_tree(Gogo*);
2236 tree
2237 do_get_init_tree(Gogo*, tree, bool);
2239 tree
2240 do_make_expression_tree(Translate_context*, Expression_list*,
2241 source_location);
2243 Expression*
2244 do_type_descriptor(Gogo*, Named_type*);
2246 void
2247 do_reflection(Gogo*, std::string*) const;
2249 void
2250 do_mangled_name(Gogo*, std::string*) const;
2252 void
2253 do_export(Export*) const;
2255 private:
2256 static Type*
2257 make_chan_type_descriptor_type();
2259 // Whether this channel can send data.
2260 bool may_send_;
2261 // Whether this channel can receive data.
2262 bool may_receive_;
2263 // The types of elements which may be sent on this channel. If this
2264 // is NULL, it means that any type may be sent.
2265 Type* element_type_;
2268 // An interface type.
2270 class Interface_type : public Type
2272 public:
2273 Interface_type(Typed_identifier_list* methods, source_location location)
2274 : Type(TYPE_INTERFACE),
2275 methods_(methods), location_(location)
2276 { gcc_assert(methods == NULL || !methods->empty()); }
2278 // Return whether this is an empty interface.
2279 bool
2280 is_empty() const
2281 { return this->methods_ == NULL; }
2283 // Return the list of methods. This will return NULL for an empty
2284 // interface.
2285 const Typed_identifier_list*
2286 methods() const
2287 { return this->methods_; }
2289 // Return the number of methods.
2290 size_t
2291 method_count() const
2292 { return this->methods_ == NULL ? 0 : this->methods_->size(); }
2294 // Return the method NAME, or NULL.
2295 const Typed_identifier*
2296 find_method(const std::string& name) const;
2298 // Return the zero-based index of method NAME.
2299 size_t
2300 method_index(const std::string& name) const;
2302 // Finalize the methods. This handles interface inheritance.
2303 void
2304 finalize_methods();
2306 // Return true if T implements this interface. If this returns
2307 // false, and REASON is not NULL, it sets *REASON to the reason that
2308 // it fails.
2309 bool
2310 implements_interface(const Type* t, std::string* reason) const;
2312 // Whether this type is identical with T. REASON is as in
2313 // implements_interface.
2314 bool
2315 is_identical(const Interface_type* t, bool errors_are_identical) const;
2317 // Whether we can assign T to this type. is_identical is known to
2318 // be false.
2319 bool
2320 is_compatible_for_assign(const Interface_type*, std::string* reason) const;
2322 // Return whether NAME is a method which is not exported. This is
2323 // only used for better error reporting.
2324 bool
2325 is_unexported_method(Gogo*, const std::string& name) const;
2327 // Import an interface type.
2328 static Interface_type*
2329 do_import(Import*);
2331 // Fill in the fields for a named interface type.
2332 tree
2333 fill_in_tree(Gogo*, tree);
2335 protected:
2337 do_traverse(Traverse*);
2339 bool
2340 do_has_pointer() const
2341 { return true; }
2343 unsigned int
2344 do_hash_for_method(Gogo*) const;
2346 tree
2347 do_get_tree(Gogo*);
2349 tree
2350 do_get_init_tree(Gogo* gogo, tree, bool);
2352 Expression*
2353 do_type_descriptor(Gogo*, Named_type*);
2355 void
2356 do_reflection(Gogo*, std::string*) const;
2358 void
2359 do_mangled_name(Gogo*, std::string*) const;
2361 void
2362 do_export(Export*) const;
2364 private:
2365 static Type*
2366 make_interface_type_descriptor_type();
2368 // The list of methods associated with the interface. This will be
2369 // NULL for the empty interface.
2370 Typed_identifier_list* methods_;
2371 // The location where the interface was defined.
2372 source_location location_;
2375 // The value we keep for a named type. This lets us get the right
2376 // name when we convert to trees. Note that we don't actually keep
2377 // the name here; the name is in the Named_object which points to
2378 // this. This object exists to hold a unique tree which represents
2379 // the type.
2381 class Named_type : public Type
2383 public:
2384 Named_type(Named_object* named_object, Type* type, source_location location)
2385 : Type(TYPE_NAMED),
2386 named_object_(named_object), in_function_(NULL), type_(type),
2387 local_methods_(NULL), all_methods_(NULL),
2388 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
2389 location_(location), named_tree_(NULL), is_visible_(true),
2390 is_error_(false), seen_(false)
2393 // Return the associated Named_object. This holds the actual name.
2394 Named_object*
2395 named_object()
2396 { return this->named_object_; }
2398 const Named_object*
2399 named_object() const
2400 { return this->named_object_; }
2402 // Set the Named_object. This is used when we see a type
2403 // declaration followed by a type.
2404 void
2405 set_named_object(Named_object* no)
2406 { this->named_object_ = no; }
2408 // Return the function in which this type is defined. This will
2409 // return NULL for a type defined in global scope.
2410 const Named_object*
2411 in_function() const
2412 { return this->in_function_; }
2414 // Set the function in which this type is defined.
2415 void
2416 set_in_function(Named_object* f)
2417 { this->in_function_ = f; }
2419 // Return the name of the type.
2420 const std::string&
2421 name() const;
2423 // Return the name of the type for an error message. The difference
2424 // is that if the type is defined in a different package, this will
2425 // return PACKAGE.NAME.
2426 std::string
2427 message_name() const;
2429 // Return the underlying type.
2430 Type*
2431 real_type()
2432 { return this->type_; }
2434 const Type*
2435 real_type() const
2436 { return this->type_; }
2438 // Return the location.
2439 source_location
2440 location() const
2441 { return this->location_; }
2443 // Whether this type is visible. This only matters when parsing.
2444 bool
2445 is_visible() const
2446 { return this->is_visible_; }
2448 // Mark this type as visible.
2449 void
2450 set_is_visible()
2451 { this->is_visible_ = true; }
2453 // Mark this type as invisible.
2454 void
2455 clear_is_visible()
2456 { this->is_visible_ = false; }
2458 // Whether this is a builtin type.
2459 bool
2460 is_builtin() const
2461 { return this->location_ == BUILTINS_LOCATION; }
2463 // Return the base type for this type.
2464 Type*
2465 named_base();
2467 const Type*
2468 named_base() const;
2470 // Return whether this is an error type.
2471 bool
2472 is_named_error_type() const;
2474 // Add a method to this type.
2475 Named_object*
2476 add_method(const std::string& name, Function*);
2478 // Add a method declaration to this type.
2479 Named_object*
2480 add_method_declaration(const std::string& name, Package* package,
2481 Function_type* type, source_location location);
2483 // Add an existing method--one defined before the type itself was
2484 // defined--to a type.
2485 void
2486 add_existing_method(Named_object*);
2488 // Look up a local method.
2489 Named_object*
2490 find_local_method(const std::string& name) const;
2492 // Return the list of local methods.
2493 const Bindings*
2494 local_methods() const
2495 { return this->local_methods_; }
2497 // Build the complete list of methods, including those from
2498 // anonymous fields, and build method stubs if needed.
2499 void
2500 finalize_methods(Gogo*);
2502 // Return whether this type has any methods. This should only be
2503 // called after the finalize_methods pass.
2504 bool
2505 has_any_methods() const
2506 { return this->all_methods_ != NULL; }
2508 // Return the methods for this type. This should only be called
2509 // after the finalized_methods pass.
2510 const Methods*
2511 methods() const
2512 { return this->all_methods_; }
2514 // Return the method to use for NAME. This returns NULL if there is
2515 // no such method or if the method is ambiguous. When it returns
2516 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
2517 Method*
2518 method_function(const std::string& name, bool *is_ambiguous) const;
2520 // Return whether NAME is a known field or method which is not
2521 // exported. This is only used for better error reporting.
2522 bool
2523 is_unexported_local_method(Gogo*, const std::string& name) const;
2525 // Return a pointer to the interface method table for this type for
2526 // the interface INTERFACE. If IS_POINTER is true, set the type
2527 // descriptor to a pointer to this type, otherwise set it to this
2528 // type.
2529 tree
2530 interface_method_table(Gogo*, const Interface_type* interface,
2531 bool is_pointer);
2533 // Whether this type has any hidden fields.
2534 bool
2535 named_type_has_hidden_fields(std::string* reason) const;
2537 // Export the type.
2538 void
2539 export_named_type(Export*, const std::string& name) const;
2541 // Import a named type.
2542 static void
2543 import_named_type(Import*, Named_type**);
2545 protected:
2547 do_traverse(Traverse* traverse)
2548 { return Type::traverse(this->type_, traverse); }
2550 bool
2551 do_verify();
2553 bool
2554 do_has_pointer() const
2555 { return this->type_->has_pointer(); }
2557 unsigned int
2558 do_hash_for_method(Gogo*) const;
2560 bool
2561 do_check_make_expression(Expression_list* args, source_location location)
2562 { return this->type_->check_make_expression(args, location); }
2564 tree
2565 do_get_tree(Gogo*);
2567 tree
2568 do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
2569 { return this->type_->get_typed_init_tree(gogo, type_tree, is_clear); }
2571 tree
2572 do_make_expression_tree(Translate_context* context, Expression_list* args,
2573 source_location location)
2574 { return this->type_->make_expression_tree(context, args, location); }
2576 Expression*
2577 do_type_descriptor(Gogo*, Named_type*);
2579 void
2580 do_reflection(Gogo*, std::string*) const;
2582 void
2583 do_mangled_name(Gogo*, std::string* ret) const;
2585 void
2586 do_export(Export*) const;
2588 private:
2589 // A mapping from interfaces to the associated interface method
2590 // tables for this type. This maps to a decl.
2591 typedef Unordered_map_hash(const Interface_type*, tree, Type_hash_identical,
2592 Type_identical) Interface_method_tables;
2594 // A pointer back to the Named_object for this type.
2595 Named_object* named_object_;
2596 // If this type is defined in a function, a pointer back to the
2597 // function in which it is defined.
2598 Named_object* in_function_;
2599 // The actual type.
2600 Type* type_;
2601 // The list of methods defined for this type. Any named type can
2602 // have methods.
2603 Bindings* local_methods_;
2604 // The full list of methods for this type, including methods
2605 // declared for anonymous fields.
2606 Methods* all_methods_;
2607 // A mapping from interfaces to the associated interface method
2608 // tables for this type.
2609 Interface_method_tables* interface_method_tables_;
2610 // A mapping from interfaces to the associated interface method
2611 // tables for pointers to this type.
2612 Interface_method_tables* pointer_interface_method_tables_;
2613 // The location where this type was defined.
2614 source_location location_;
2615 // The tree for this type while converting to GENERIC. This is used
2616 // to avoid endless recursion when a named type refers to itself.
2617 tree named_tree_;
2618 // Whether this type is visible. This is false if this type was
2619 // created because it was referenced by an imported object, but the
2620 // type itself was not exported. This will always be true for types
2621 // created in the current package.
2622 bool is_visible_;
2623 // Whether this type is erroneous.
2624 bool is_error_;
2625 // In a recursive operation such as has_hidden_fields, this flag is
2626 // used to prevent infinite recursion when a type refers to itself.
2627 // This is mutable because it is always reset to false when the
2628 // function exits.
2629 mutable bool seen_;
2632 // A forward declaration. This handles a type which has been declared
2633 // but not defined.
2635 class Forward_declaration_type : public Type
2637 public:
2638 Forward_declaration_type(Named_object* named_object);
2640 // The named object associated with this type declaration. This
2641 // will be resolved.
2642 Named_object*
2643 named_object();
2645 const Named_object*
2646 named_object() const;
2648 // Return the name of the type.
2649 const std::string&
2650 name() const;
2652 // Return the type to which this points. Give an error if the type
2653 // has not yet been defined.
2654 Type*
2655 real_type();
2657 const Type*
2658 real_type() const;
2660 // Whether the base type has been defined.
2661 bool
2662 is_defined() const;
2664 // Add a method to this type.
2665 Named_object*
2666 add_method(const std::string& name, Function*);
2668 // Add a method declaration to this type.
2669 Named_object*
2670 add_method_declaration(const std::string& name, Function_type*,
2671 source_location);
2673 protected:
2675 do_traverse(Traverse* traverse);
2677 bool
2678 do_has_pointer() const
2679 { return this->base()->has_pointer(); }
2681 unsigned int
2682 do_hash_for_method(Gogo* gogo) const
2683 { return this->real_type()->hash_for_method(gogo); }
2685 bool
2686 do_check_make_expression(Expression_list* args, source_location location)
2687 { return this->base()->check_make_expression(args, location); }
2689 tree
2690 do_get_tree(Gogo* gogo);
2692 tree
2693 do_get_init_tree(Gogo* gogo, tree type_tree, bool is_clear)
2694 { return this->base()->get_typed_init_tree(gogo, type_tree, is_clear); }
2696 tree
2697 do_make_expression_tree(Translate_context* context, Expression_list* args,
2698 source_location location)
2699 { return this->base()->make_expression_tree(context, args, location); }
2701 Expression*
2702 do_type_descriptor(Gogo*, Named_type*);
2704 void
2705 do_reflection(Gogo*, std::string*) const;
2707 void
2708 do_mangled_name(Gogo*, std::string* ret) const;
2710 void
2711 do_export(Export*) const;
2713 private:
2714 // Issue a warning about a use of an undefined type.
2715 void
2716 warn() const;
2718 // The type declaration.
2719 Named_object* named_object_;
2720 // Whether we have issued a warning about this type.
2721 mutable bool warned_;
2724 // The Type_context struct describes what we expect for the type of an
2725 // expression.
2727 struct Type_context
2729 // The exact type we expect, if known. This may be NULL.
2730 Type* type;
2731 // Whether an abstract type is permitted.
2732 bool may_be_abstract;
2734 // Constructors.
2735 Type_context()
2736 : type(NULL), may_be_abstract(false)
2739 Type_context(Type* a_type, bool a_may_be_abstract)
2740 : type(a_type), may_be_abstract(a_may_be_abstract)
2744 #endif // !defined(GO_TYPES_H)