111
|
1 // types.h -- Go frontend types. -*- C++ -*-
|
|
2
|
|
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.
|
|
6
|
|
7 #ifndef GO_TYPES_H
|
|
8 #define GO_TYPES_H
|
|
9
|
|
10 #include <ostream>
|
|
11
|
|
12 #include "go-linemap.h"
|
|
13 #include "escape.h"
|
|
14
|
|
15 class Gogo;
|
|
16 class Package;
|
|
17 class Variable;
|
|
18 class Traverse;
|
|
19 class Typed_identifier;
|
|
20 class Typed_identifier_list;
|
|
21 class Integer_type;
|
|
22 class Float_type;
|
|
23 class Complex_type;
|
|
24 class String_type;
|
|
25 class Function_type;
|
|
26 class Backend_function_type;
|
|
27 class Struct_field;
|
|
28 class Struct_field_list;
|
|
29 class Struct_type;
|
|
30 class Pointer_type;
|
|
31 class Array_type;
|
|
32 class Map_type;
|
|
33 class Channel_type;
|
|
34 class Interface_type;
|
|
35 class Named_type;
|
|
36 class Forward_declaration_type;
|
|
37 class Method;
|
|
38 class Methods;
|
|
39 class Type_hash_identical;
|
|
40 class Type_identical;
|
|
41 class Expression;
|
|
42 class Expression_list;
|
|
43 class Call_expression;
|
|
44 class Field_reference_expression;
|
|
45 class Bound_method_expression;
|
|
46 class Bindings;
|
|
47 class Named_object;
|
|
48 class Function;
|
|
49 class Translate_context;
|
|
50 class Export;
|
|
51 class Import;
|
|
52 class Btype;
|
|
53 class Bexpression;
|
|
54 class Bvariable;
|
|
55
|
|
56 // Type codes used in type descriptors. These must match the values
|
|
57 // in libgo/runtime/go-type.h. They also match the values in the gc
|
|
58 // compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
|
|
59 // although this is not required.
|
|
60
|
|
61 static const int RUNTIME_TYPE_KIND_BOOL = 1;
|
|
62 static const int RUNTIME_TYPE_KIND_INT = 2;
|
|
63 static const int RUNTIME_TYPE_KIND_INT8 = 3;
|
|
64 static const int RUNTIME_TYPE_KIND_INT16 = 4;
|
|
65 static const int RUNTIME_TYPE_KIND_INT32 = 5;
|
|
66 static const int RUNTIME_TYPE_KIND_INT64 = 6;
|
|
67 static const int RUNTIME_TYPE_KIND_UINT = 7;
|
|
68 static const int RUNTIME_TYPE_KIND_UINT8 = 8;
|
|
69 static const int RUNTIME_TYPE_KIND_UINT16 = 9;
|
|
70 static const int RUNTIME_TYPE_KIND_UINT32 = 10;
|
|
71 static const int RUNTIME_TYPE_KIND_UINT64 = 11;
|
|
72 static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
|
|
73 static const int RUNTIME_TYPE_KIND_FLOAT32 = 13;
|
|
74 static const int RUNTIME_TYPE_KIND_FLOAT64 = 14;
|
|
75 static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15;
|
|
76 static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16;
|
|
77 static const int RUNTIME_TYPE_KIND_ARRAY = 17;
|
|
78 static const int RUNTIME_TYPE_KIND_CHAN = 18;
|
|
79 static const int RUNTIME_TYPE_KIND_FUNC = 19;
|
|
80 static const int RUNTIME_TYPE_KIND_INTERFACE = 20;
|
|
81 static const int RUNTIME_TYPE_KIND_MAP = 21;
|
|
82 static const int RUNTIME_TYPE_KIND_PTR = 22;
|
|
83 static const int RUNTIME_TYPE_KIND_SLICE = 23;
|
|
84 static const int RUNTIME_TYPE_KIND_STRING = 24;
|
|
85 static const int RUNTIME_TYPE_KIND_STRUCT = 25;
|
|
86 static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26;
|
|
87
|
|
88 static const int RUNTIME_TYPE_KIND_DIRECT_IFACE = (1 << 5);
|
|
89 static const int RUNTIME_TYPE_KIND_GC_PROG = (1 << 6);
|
|
90 static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7);
|
|
91
|
|
92 // To build the complete list of methods for a named type we need to
|
|
93 // gather all methods from anonymous fields. Those methods may
|
|
94 // require an arbitrary set of indirections and field offsets. There
|
|
95 // is also the possibility of ambiguous methods, which we could ignore
|
|
96 // except that we want to give a better error message for that case.
|
|
97 // This is a base class. There are two types of methods: named
|
|
98 // methods, and methods which are inherited from an anonymous field of
|
|
99 // interface type.
|
|
100
|
|
101 class Method
|
|
102 {
|
|
103 public:
|
|
104 // For methods in anonymous types we need to know the sequence of
|
|
105 // field references used to extract the pointer to pass to the
|
|
106 // method. Since each method for a particular anonymous field will
|
|
107 // have the sequence of field indexes, and since the indexes can be
|
|
108 // shared going down the chain, we use a manually managed linked
|
|
109 // list. The first entry in the list is the field index for the
|
|
110 // last field, the one passed to the method.
|
|
111
|
|
112 struct Field_indexes
|
|
113 {
|
|
114 const Field_indexes* next;
|
|
115 unsigned int field_index;
|
|
116 };
|
|
117
|
|
118 virtual ~Method()
|
|
119 { }
|
|
120
|
|
121 // Get the list of field indexes.
|
|
122 const Field_indexes*
|
|
123 field_indexes() const
|
|
124 { return this->field_indexes_; }
|
|
125
|
|
126 // Get the depth.
|
|
127 unsigned int
|
|
128 depth() const
|
|
129 { return this->depth_; }
|
|
130
|
|
131 // Return whether this is a value method--a method which does not
|
|
132 // require a pointer expression.
|
|
133 bool
|
|
134 is_value_method() const
|
|
135 { return this->is_value_method_; }
|
|
136
|
|
137 // Return whether we need a stub method--this is true if we can't
|
|
138 // just pass the main object to the method.
|
|
139 bool
|
|
140 needs_stub_method() const
|
|
141 { return this->needs_stub_method_; }
|
|
142
|
|
143 // Return whether this is an ambiguous method name.
|
|
144 bool
|
|
145 is_ambiguous() const
|
|
146 { return this->is_ambiguous_; }
|
|
147
|
|
148 // Note that this method is ambiguous.
|
|
149 void
|
|
150 set_is_ambiguous()
|
|
151 { this->is_ambiguous_ = true; }
|
|
152
|
|
153 // Return the type of the method.
|
|
154 Function_type*
|
|
155 type() const
|
|
156 { return this->do_type(); }
|
|
157
|
|
158 // Return the location of the method receiver.
|
|
159 Location
|
|
160 receiver_location() const
|
|
161 { return this->do_receiver_location(); }
|
|
162
|
|
163 // Return an expression which binds this method to EXPR. This is
|
|
164 // something which can be used with a function call.
|
|
165 Expression*
|
|
166 bind_method(Expression* expr, Location location) const;
|
|
167
|
|
168 // Return the named object for this method. This may only be called
|
|
169 // after methods are finalized.
|
|
170 Named_object*
|
|
171 named_object() const;
|
|
172
|
|
173 // Get the stub object.
|
|
174 Named_object*
|
|
175 stub_object() const
|
|
176 {
|
|
177 go_assert(this->stub_ != NULL);
|
|
178 return this->stub_;
|
|
179 }
|
|
180
|
|
181 // Set the stub object.
|
|
182 void
|
|
183 set_stub_object(Named_object* no)
|
|
184 {
|
|
185 go_assert(this->stub_ == NULL);
|
|
186 this->stub_ = no;
|
|
187 }
|
|
188
|
|
189 // Return true if this method should not participate in any
|
|
190 // interfaces.
|
|
191 bool
|
|
192 nointerface() const
|
|
193 { return this->do_nointerface(); }
|
|
194
|
|
195 protected:
|
|
196 // These objects are only built by the child classes.
|
|
197 Method(const Field_indexes* field_indexes, unsigned int depth,
|
|
198 bool is_value_method, bool needs_stub_method)
|
|
199 : field_indexes_(field_indexes), depth_(depth), stub_(NULL),
|
|
200 is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
|
|
201 is_ambiguous_(false)
|
|
202 { }
|
|
203
|
|
204 // The named object for this method.
|
|
205 virtual Named_object*
|
|
206 do_named_object() const = 0;
|
|
207
|
|
208 // The type of the method.
|
|
209 virtual Function_type*
|
|
210 do_type() const = 0;
|
|
211
|
|
212 // Return the location of the method receiver.
|
|
213 virtual Location
|
|
214 do_receiver_location() const = 0;
|
|
215
|
|
216 // Bind a method to an object.
|
|
217 virtual Expression*
|
|
218 do_bind_method(Expression* expr, Location location) const = 0;
|
|
219
|
|
220 // Return whether this method should not participate in interfaces.
|
|
221 virtual bool
|
|
222 do_nointerface() const = 0;
|
|
223
|
|
224 private:
|
|
225 // The sequence of field indexes used for this method. If this is
|
|
226 // NULL, then the method is defined for the current type.
|
|
227 const Field_indexes* field_indexes_;
|
|
228 // The depth at which this method was found.
|
|
229 unsigned int depth_;
|
|
230 // If a stub method is required, this is its object. This is only
|
|
231 // set after stub methods are built in finalize_methods.
|
|
232 Named_object* stub_;
|
|
233 // Whether this is a value method--a method that does not require a
|
|
234 // pointer.
|
|
235 bool is_value_method_;
|
|
236 // Whether a stub method is required.
|
|
237 bool needs_stub_method_;
|
|
238 // Whether this method is ambiguous.
|
|
239 bool is_ambiguous_;
|
|
240 };
|
|
241
|
|
242 // A named method. This is what you get with a method declaration,
|
|
243 // either directly on the type, or inherited from some anonymous
|
|
244 // embedded field.
|
|
245
|
|
246 class Named_method : public Method
|
|
247 {
|
|
248 public:
|
|
249 Named_method(Named_object* named_object, const Field_indexes* field_indexes,
|
|
250 unsigned int depth, bool is_value_method,
|
|
251 bool needs_stub_method)
|
|
252 : Method(field_indexes, depth, is_value_method, needs_stub_method),
|
|
253 named_object_(named_object)
|
|
254 { }
|
|
255
|
|
256 protected:
|
|
257 // Get the Named_object for the method.
|
|
258 Named_object*
|
|
259 do_named_object() const
|
|
260 { return this->named_object_; }
|
|
261
|
|
262 // The type of the method.
|
|
263 Function_type*
|
|
264 do_type() const;
|
|
265
|
|
266 // Return the location of the method receiver.
|
|
267 Location
|
|
268 do_receiver_location() const;
|
|
269
|
|
270 // Bind a method to an object.
|
|
271 Expression*
|
|
272 do_bind_method(Expression* expr, Location location) const;
|
|
273
|
|
274 // Return whether this method should not participate in interfaces.
|
|
275 bool
|
|
276 do_nointerface() const;
|
|
277
|
|
278 private:
|
|
279 // The method itself. For a method which needs a stub, this starts
|
|
280 // out as the underlying method, and is later replaced with the stub
|
|
281 // method.
|
|
282 Named_object* named_object_;
|
|
283 };
|
|
284
|
|
285 // An interface method. This is used when an interface appears as an
|
|
286 // anonymous field in a named struct.
|
|
287
|
|
288 class Interface_method : public Method
|
|
289 {
|
|
290 public:
|
|
291 Interface_method(const std::string& name, Location location,
|
|
292 Function_type* fntype, const Field_indexes* field_indexes,
|
|
293 unsigned int depth)
|
|
294 : Method(field_indexes, depth, true, true),
|
|
295 name_(name), location_(location), fntype_(fntype)
|
|
296 { }
|
|
297
|
|
298 protected:
|
|
299 // Get the Named_object for the method. This should never be
|
|
300 // called, as we always create a stub.
|
|
301 Named_object*
|
|
302 do_named_object() const
|
|
303 { go_unreachable(); }
|
|
304
|
|
305 // The type of the method.
|
|
306 Function_type*
|
|
307 do_type() const
|
|
308 { return this->fntype_; }
|
|
309
|
|
310 // Return the location of the method receiver.
|
|
311 Location
|
|
312 do_receiver_location() const
|
|
313 { return this->location_; }
|
|
314
|
|
315 // Bind a method to an object.
|
|
316 Expression*
|
|
317 do_bind_method(Expression* expr, Location location) const;
|
|
318
|
|
319 // Return whether this method should not participate in interfaces.
|
|
320 bool
|
|
321 do_nointerface() const
|
|
322 { return false; }
|
|
323
|
|
324 private:
|
|
325 // The name of the interface method to call.
|
|
326 std::string name_;
|
|
327 // The location of the definition of the interface method.
|
|
328 Location location_;
|
|
329 // The type of the interface method.
|
|
330 Function_type* fntype_;
|
|
331 };
|
|
332
|
|
333 // A mapping from method name to Method. This is a wrapper around a
|
|
334 // hash table.
|
|
335
|
|
336 class Methods
|
|
337 {
|
|
338 private:
|
|
339 typedef Unordered_map(std::string, Method*) Method_map;
|
|
340
|
|
341 public:
|
|
342 typedef Method_map::const_iterator const_iterator;
|
|
343
|
|
344 Methods()
|
|
345 : methods_()
|
|
346 { }
|
|
347
|
|
348 // Insert a new method. Returns true if it was inserted, false if
|
|
349 // it was overidden or ambiguous.
|
|
350 bool
|
|
351 insert(const std::string& name, Method* m);
|
|
352
|
|
353 // The number of (unambiguous) methods.
|
|
354 size_t
|
|
355 count() const;
|
|
356
|
|
357 // Iterate.
|
|
358 const_iterator
|
|
359 begin() const
|
|
360 { return this->methods_.begin(); }
|
|
361
|
|
362 const_iterator
|
|
363 end() const
|
|
364 { return this->methods_.end(); }
|
|
365
|
|
366 // Lookup.
|
|
367 const_iterator
|
|
368 find(const std::string& name) const
|
|
369 { return this->methods_.find(name); }
|
|
370
|
|
371 bool
|
|
372 empty() const
|
|
373 { return this->methods_.empty(); }
|
|
374
|
|
375 private:
|
|
376 Method_map methods_;
|
|
377 };
|
|
378
|
|
379 // The base class for all types.
|
|
380
|
|
381 class Type
|
|
382 {
|
|
383 public:
|
|
384 // The types of types.
|
|
385 enum Type_classification
|
|
386 {
|
|
387 TYPE_ERROR,
|
|
388 TYPE_VOID,
|
|
389 TYPE_BOOLEAN,
|
|
390 TYPE_INTEGER,
|
|
391 TYPE_FLOAT,
|
|
392 TYPE_COMPLEX,
|
|
393 TYPE_STRING,
|
|
394 TYPE_SINK,
|
|
395 TYPE_FUNCTION,
|
|
396 TYPE_POINTER,
|
|
397 TYPE_NIL,
|
|
398 TYPE_CALL_MULTIPLE_RESULT,
|
|
399 TYPE_STRUCT,
|
|
400 TYPE_ARRAY,
|
|
401 TYPE_MAP,
|
|
402 TYPE_CHANNEL,
|
|
403 TYPE_INTERFACE,
|
|
404 TYPE_NAMED,
|
|
405 TYPE_FORWARD
|
|
406 };
|
|
407
|
|
408 virtual ~Type();
|
|
409
|
|
410 // Creators.
|
|
411
|
|
412 static Type*
|
|
413 make_error_type();
|
|
414
|
|
415 static Type*
|
|
416 make_void_type();
|
|
417
|
|
418 // Get the unnamed bool type.
|
|
419 static Type*
|
|
420 make_boolean_type();
|
|
421
|
|
422 // Get the named type "bool".
|
|
423 static Named_type*
|
|
424 lookup_bool_type();
|
|
425
|
|
426 // Make the named type "bool".
|
|
427 static Named_type*
|
|
428 make_named_bool_type();
|
|
429
|
|
430 // Make an abstract integer type.
|
|
431 static Integer_type*
|
|
432 make_abstract_integer_type();
|
|
433
|
|
434 // Make an abstract type for a character constant.
|
|
435 static Integer_type*
|
|
436 make_abstract_character_type();
|
|
437
|
|
438 // Make a named integer type with a specified size.
|
|
439 // RUNTIME_TYPE_KIND is the code to use in reflection information,
|
|
440 // to distinguish int and int32.
|
|
441 static Named_type*
|
|
442 make_integer_type(const char* name, bool is_unsigned, int bits,
|
|
443 int runtime_type_kind);
|
|
444
|
|
445 // Look up a named integer type.
|
|
446 static Named_type*
|
|
447 lookup_integer_type(const char* name);
|
|
448
|
|
449 // Make an abstract floating point type.
|
|
450 static Float_type*
|
|
451 make_abstract_float_type();
|
|
452
|
|
453 // Make a named floating point type with a specific size.
|
|
454 // RUNTIME_TYPE_KIND is the code to use in reflection information,
|
|
455 // to distinguish float and float32.
|
|
456 static Named_type*
|
|
457 make_float_type(const char* name, int bits, int runtime_type_kind);
|
|
458
|
|
459 // Look up a named float type.
|
|
460 static Named_type*
|
|
461 lookup_float_type(const char* name);
|
|
462
|
|
463 // Make an abstract complex type.
|
|
464 static Complex_type*
|
|
465 make_abstract_complex_type();
|
|
466
|
|
467 // Make a named complex type with a specific size.
|
|
468 // RUNTIME_TYPE_KIND is the code to use in reflection information,
|
|
469 // to distinguish complex and complex64.
|
|
470 static Named_type*
|
|
471 make_complex_type(const char* name, int bits, int runtime_type_kind);
|
|
472
|
|
473 // Look up a named complex type.
|
|
474 static Named_type*
|
|
475 lookup_complex_type(const char* name);
|
|
476
|
|
477 // Get the unnamed string type.
|
|
478 static Type*
|
|
479 make_string_type();
|
|
480
|
|
481 // Get the named type "string".
|
|
482 static Named_type*
|
|
483 lookup_string_type();
|
|
484
|
|
485 // Make the named type "string".
|
|
486 static Named_type*
|
|
487 make_named_string_type();
|
|
488
|
|
489 static Type*
|
|
490 make_sink_type();
|
|
491
|
|
492 static Function_type*
|
|
493 make_function_type(Typed_identifier* receiver,
|
|
494 Typed_identifier_list* parameters,
|
|
495 Typed_identifier_list* results,
|
|
496 Location);
|
|
497
|
|
498 static Backend_function_type*
|
|
499 make_backend_function_type(Typed_identifier* receiver,
|
|
500 Typed_identifier_list* parameters,
|
|
501 Typed_identifier_list* results,
|
|
502 Location);
|
|
503
|
|
504 static Pointer_type*
|
|
505 make_pointer_type(Type*);
|
|
506
|
|
507 static void
|
|
508 finish_pointer_types(Gogo* gogo);
|
|
509
|
|
510 static Type*
|
|
511 make_nil_type();
|
|
512
|
|
513 static Type*
|
|
514 make_call_multiple_result_type(Call_expression*);
|
|
515
|
|
516 static Struct_type*
|
|
517 make_struct_type(Struct_field_list* fields, Location);
|
|
518
|
|
519 static Array_type*
|
|
520 make_array_type(Type* element_type, Expression* length);
|
|
521
|
|
522 static Map_type*
|
|
523 make_map_type(Type* key_type, Type* value_type, Location);
|
|
524
|
|
525 static Channel_type*
|
|
526 make_channel_type(bool send, bool receive, Type*);
|
|
527
|
|
528 static Interface_type*
|
|
529 make_interface_type(Typed_identifier_list* methods, Location);
|
|
530
|
|
531 static Interface_type*
|
|
532 make_empty_interface_type(Location);
|
|
533
|
|
534 static Type*
|
|
535 make_type_descriptor_type();
|
|
536
|
|
537 static Type*
|
|
538 make_type_descriptor_ptr_type();
|
|
539
|
|
540 static Named_type*
|
|
541 make_named_type(Named_object*, Type*, Location);
|
|
542
|
|
543 static Type*
|
|
544 make_forward_declaration(Named_object*);
|
|
545
|
|
546 // Make a builtin struct type from a list of fields.
|
|
547 static Struct_type*
|
|
548 make_builtin_struct_type(int nfields, ...);
|
|
549
|
|
550 // Make a builtin named type.
|
|
551 static Named_type*
|
|
552 make_builtin_named_type(const char* name, Type* type);
|
|
553
|
|
554 // Traverse a type.
|
|
555 static int
|
|
556 traverse(Type*, Traverse*);
|
|
557
|
|
558 // Verify the type. This is called after parsing, and verifies that
|
|
559 // types are complete and meet the language requirements. This
|
|
560 // returns false if the type is invalid and we should not continue
|
|
561 // traversing it.
|
|
562 bool
|
|
563 verify()
|
|
564 { return this->do_verify(); }
|
|
565
|
|
566 // Return true if two types are identical. If ERRORS_ARE_IDENTICAL,
|
|
567 // returns that an erroneous type is identical to any other type;
|
|
568 // this is used to avoid cascading errors. If this returns false,
|
|
569 // and REASON is not NULL, it may set *REASON.
|
|
570 static bool
|
|
571 are_identical(const Type* lhs, const Type* rhs, bool errors_are_identical,
|
|
572 std::string* reason);
|
|
573
|
|
574 // An argument to are_identical_cmp_tags, indicating whether or not
|
|
575 // to compare struct field tags.
|
|
576 enum Cmp_tags {
|
|
577 COMPARE_TAGS,
|
|
578 IGNORE_TAGS
|
|
579 };
|
|
580
|
|
581 // Return true if two types are identical. This is like the
|
|
582 // are_identical function, but also takes a CMP_TAGS argument
|
|
583 // indicating whether to compare struct tags. Otherwise the
|
|
584 // parameters are as for are_identical.
|
|
585 static bool
|
|
586 are_identical_cmp_tags(const Type* lhs, const Type* rhs,
|
|
587 Cmp_tags, bool errors_are_identical,
|
|
588 std::string* reason);
|
|
589
|
|
590 // Return true if two types are compatible for use in a binary
|
|
591 // operation, other than a shift, comparison, or channel send. This
|
|
592 // is an equivalence relation.
|
|
593 static bool
|
|
594 are_compatible_for_binop(const Type* t1, const Type* t2);
|
|
595
|
|
596 // Return true if two types are compatible for use with the
|
|
597 // comparison operator. IS_EQUALITY_OP is true if this is an
|
|
598 // equality comparison, false if it is an ordered comparison. This
|
|
599 // is an equivalence relation. If this returns false, and REASON is
|
|
600 // not NULL, it sets *REASON.
|
|
601 static bool
|
|
602 are_compatible_for_comparison(bool is_equality_op, const Type *t1,
|
|
603 const Type *t2, std::string* reason);
|
|
604
|
|
605 // Return true if a type is comparable with itself. This is true of
|
|
606 // most types, but false for, e.g., function types.
|
|
607 bool
|
|
608 is_comparable() const
|
|
609 { return Type::are_compatible_for_comparison(true, this, this, NULL); }
|
|
610
|
|
611 // Return true if a value with type RHS is assignable to a variable
|
|
612 // with type LHS. This is not an equivalence relation. If this
|
|
613 // returns false, and REASON is not NULL, it sets *REASON.
|
|
614 static bool
|
|
615 are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
|
|
616
|
|
617 // Return true if a value with type RHS may be converted to type
|
|
618 // LHS. If this returns false, and REASON is not NULL, it sets
|
|
619 // *REASON.
|
|
620 static bool
|
|
621 are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
|
|
622
|
|
623 // Return true if values of this type can be compared using an
|
|
624 // identity function which gets nothing but a pointer to the value
|
|
625 // and a size.
|
|
626 bool
|
|
627 compare_is_identity(Gogo* gogo)
|
|
628 { return this->do_compare_is_identity(gogo); }
|
|
629
|
|
630 // Return whether values of this type are reflexive: if a comparison
|
|
631 // of a value with itself always returns true.
|
|
632 bool
|
|
633 is_reflexive()
|
|
634 { return this->do_is_reflexive(); }
|
|
635
|
|
636 // Return whether values of this, when used as a key in map,
|
|
637 // requires the key to be updated when an assignment is made.
|
|
638 bool
|
|
639 needs_key_update()
|
|
640 { return this->do_needs_key_update(); }
|
|
641
|
|
642 // Whether the type is permitted in the heap.
|
|
643 bool
|
|
644 in_heap()
|
|
645 { return this->do_in_heap(); }
|
|
646
|
|
647 // Return a hash code for this type for the method hash table.
|
|
648 // Types which are equivalent according to are_identical will have
|
|
649 // the same hash code.
|
|
650 unsigned int
|
|
651 hash_for_method(Gogo*) const;
|
|
652
|
|
653 // Return the type classification.
|
|
654 Type_classification
|
|
655 classification() const
|
|
656 { return this->classification_; }
|
|
657
|
|
658 // Return the base type for this type. This looks through forward
|
|
659 // declarations and names. Using this with a forward declaration
|
|
660 // which has not been defined will return an error type.
|
|
661 Type*
|
|
662 base();
|
|
663
|
|
664 const Type*
|
|
665 base() const;
|
|
666
|
|
667 // Return the type skipping defined forward declarations. If this
|
|
668 // type is a forward declaration which has not been defined, it will
|
|
669 // return the Forward_declaration_type. This differs from base() in
|
|
670 // that it will return a Named_type, and for a
|
|
671 // Forward_declaration_type which is not defined it will return that
|
|
672 // type rather than an error type.
|
|
673 Type*
|
|
674 forwarded();
|
|
675
|
|
676 const Type*
|
|
677 forwarded() const;
|
|
678
|
|
679 // Return true if this is a basic type: a type which is not composed
|
|
680 // of other types, and is not void.
|
|
681 bool
|
|
682 is_basic_type() const;
|
|
683
|
|
684 // Return true if this is an abstract type--an integer, floating
|
|
685 // point, or complex type whose size has not been determined.
|
|
686 bool
|
|
687 is_abstract() const;
|
|
688
|
|
689 // Return a non-abstract version of an abstract type.
|
|
690 Type*
|
|
691 make_non_abstract_type();
|
|
692
|
|
693 // Return true if this type is or contains a pointer. This
|
|
694 // determines whether the garbage collector needs to look at a value
|
|
695 // of this type.
|
|
696 bool
|
|
697 has_pointer() const
|
|
698 { return this->do_has_pointer(); }
|
|
699
|
|
700 // Return true if this is the error type. This returns false for a
|
|
701 // type which is not defined, as it is called by the parser before
|
|
702 // all types are defined.
|
|
703 bool
|
|
704 is_error_type() const;
|
|
705
|
|
706 // Return true if this is the error type or if the type is
|
|
707 // undefined. If the type is undefined, this will give an error.
|
|
708 // This should only be called after parsing is complete.
|
|
709 bool
|
|
710 is_error() const
|
|
711 { return this->base()->is_error_type(); }
|
|
712
|
|
713 // Return true if this is a void type.
|
|
714 bool
|
|
715 is_void_type() const
|
|
716 { return this->classification_ == TYPE_VOID; }
|
|
717
|
|
718 // If this is an integer type, return the Integer_type. Otherwise,
|
|
719 // return NULL. This is a controlled dynamic_cast.
|
|
720 Integer_type*
|
|
721 integer_type()
|
|
722 { return this->convert<Integer_type, TYPE_INTEGER>(); }
|
|
723
|
|
724 const Integer_type*
|
|
725 integer_type() const
|
|
726 { return this->convert<const Integer_type, TYPE_INTEGER>(); }
|
|
727
|
|
728 // If this is a floating point type, return the Float_type.
|
|
729 // Otherwise, return NULL. This is a controlled dynamic_cast.
|
|
730 Float_type*
|
|
731 float_type()
|
|
732 { return this->convert<Float_type, TYPE_FLOAT>(); }
|
|
733
|
|
734 const Float_type*
|
|
735 float_type() const
|
|
736 { return this->convert<const Float_type, TYPE_FLOAT>(); }
|
|
737
|
|
738 // If this is a complex type, return the Complex_type. Otherwise,
|
|
739 // return NULL.
|
|
740 Complex_type*
|
|
741 complex_type()
|
|
742 { return this->convert<Complex_type, TYPE_COMPLEX>(); }
|
|
743
|
|
744 const Complex_type*
|
|
745 complex_type() const
|
|
746 { return this->convert<const Complex_type, TYPE_COMPLEX>(); }
|
|
747
|
|
748 // Return whether this is a numeric type.
|
|
749 bool
|
|
750 is_numeric_type() const
|
|
751 {
|
|
752 Type_classification tc = this->base()->classification_;
|
|
753 return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX;
|
|
754 }
|
|
755
|
|
756 // Return true if this is a boolean type.
|
|
757 bool
|
|
758 is_boolean_type() const
|
|
759 { return this->base()->classification_ == TYPE_BOOLEAN; }
|
|
760
|
|
761 // Return true if this is an abstract boolean type.
|
|
762 bool
|
|
763 is_abstract_boolean_type() const
|
|
764 { return this->classification_ == TYPE_BOOLEAN; }
|
|
765
|
|
766 // Return true if this is a string type.
|
|
767 bool
|
|
768 is_string_type() const
|
|
769 { return this->base()->classification_ == TYPE_STRING; }
|
|
770
|
|
771 // Return true if this is an abstract string type.
|
|
772 bool
|
|
773 is_abstract_string_type() const
|
|
774 { return this->classification_ == TYPE_STRING; }
|
|
775
|
|
776 // Return true if this is the sink type. This is the type of the
|
|
777 // blank identifier _.
|
|
778 bool
|
|
779 is_sink_type() const
|
|
780 { return this->base()->classification_ == TYPE_SINK; }
|
|
781
|
|
782 // If this is a function type, return it. Otherwise, return NULL.
|
|
783 Function_type*
|
|
784 function_type()
|
|
785 { return this->convert<Function_type, TYPE_FUNCTION>(); }
|
|
786
|
|
787 const Function_type*
|
|
788 function_type() const
|
|
789 { return this->convert<const Function_type, TYPE_FUNCTION>(); }
|
|
790
|
|
791 // If this is a pointer type, return the type to which it points.
|
|
792 // Otherwise, return NULL.
|
|
793 Type*
|
|
794 points_to() const;
|
|
795
|
|
796 // If this is a pointer type, return the type to which it points.
|
|
797 // Otherwise, return the type itself.
|
|
798 Type*
|
|
799 deref()
|
|
800 {
|
|
801 Type* pt = this->points_to();
|
|
802 return pt != NULL ? pt : this;
|
|
803 }
|
|
804
|
|
805 const Type*
|
|
806 deref() const
|
|
807 {
|
|
808 const Type* pt = this->points_to();
|
|
809 return pt != NULL ? pt : this;
|
|
810 }
|
|
811
|
|
812 // Return true if this is the nil type. We don't use base() here,
|
|
813 // because this can be called during parse, and there is no way to
|
|
814 // name the nil type anyhow.
|
|
815 bool
|
|
816 is_nil_type() const
|
|
817 { return this->classification_ == TYPE_NIL; }
|
|
818
|
|
819 // Return true if this is the predeclared constant nil being used as
|
|
820 // a type. This is what the parser produces for type switches which
|
|
821 // use "case nil".
|
|
822 bool
|
|
823 is_nil_constant_as_type() const;
|
|
824
|
|
825 // Return true if this is the return type of a function which
|
|
826 // returns multiple values.
|
|
827 bool
|
|
828 is_call_multiple_result_type() const
|
|
829 { return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
|
|
830
|
|
831 // If this is a struct type, return it. Otherwise, return NULL.
|
|
832 Struct_type*
|
|
833 struct_type()
|
|
834 { return this->convert<Struct_type, TYPE_STRUCT>(); }
|
|
835
|
|
836 const Struct_type*
|
|
837 struct_type() const
|
|
838 { return this->convert<const Struct_type, TYPE_STRUCT>(); }
|
|
839
|
|
840 // If this is an array type, return it. Otherwise, return NULL.
|
|
841 Array_type*
|
|
842 array_type()
|
|
843 { return this->convert<Array_type, TYPE_ARRAY>(); }
|
|
844
|
|
845 const Array_type*
|
|
846 array_type() const
|
|
847 { return this->convert<const Array_type, TYPE_ARRAY>(); }
|
|
848
|
|
849 // Return whether if this is a slice type.
|
|
850 bool
|
|
851 is_slice_type() const;
|
|
852
|
|
853 // If this is a map type, return it. Otherwise, return NULL.
|
|
854 Map_type*
|
|
855 map_type()
|
|
856 { return this->convert<Map_type, TYPE_MAP>(); }
|
|
857
|
|
858 const Map_type*
|
|
859 map_type() const
|
|
860 { return this->convert<const Map_type, TYPE_MAP>(); }
|
|
861
|
|
862 // If this is a channel type, return it. Otherwise, return NULL.
|
|
863 Channel_type*
|
|
864 channel_type()
|
|
865 { return this->convert<Channel_type, TYPE_CHANNEL>(); }
|
|
866
|
|
867 const Channel_type*
|
|
868 channel_type() const
|
|
869 { return this->convert<const Channel_type, TYPE_CHANNEL>(); }
|
|
870
|
|
871 // If this is an interface type, return it. Otherwise, return NULL.
|
|
872 Interface_type*
|
|
873 interface_type()
|
|
874 { return this->convert<Interface_type, TYPE_INTERFACE>(); }
|
|
875
|
|
876 const Interface_type*
|
|
877 interface_type() const
|
|
878 { return this->convert<const Interface_type, TYPE_INTERFACE>(); }
|
|
879
|
|
880 // If this is a named type, return it. Otherwise, return NULL.
|
|
881 Named_type*
|
|
882 named_type();
|
|
883
|
|
884 const Named_type*
|
|
885 named_type() const;
|
|
886
|
|
887 // If this is a forward declaration, return it. Otherwise, return
|
|
888 // NULL.
|
|
889 Forward_declaration_type*
|
|
890 forward_declaration_type()
|
|
891 { return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
|
|
892
|
|
893 const Forward_declaration_type*
|
|
894 forward_declaration_type() const
|
|
895 {
|
|
896 return this->convert_no_base<const Forward_declaration_type,
|
|
897 TYPE_FORWARD>();
|
|
898 }
|
|
899
|
|
900 // Return true if this type is not yet defined.
|
|
901 bool
|
|
902 is_undefined() const;
|
|
903
|
|
904 // Return true if this is the unsafe.pointer type. We currently
|
|
905 // represent that as pointer-to-void.
|
|
906 bool
|
|
907 is_unsafe_pointer_type() const
|
|
908 { return this->points_to() != NULL && this->points_to()->is_void_type(); }
|
|
909
|
|
910 // Look for field or method NAME for TYPE. Return an expression for
|
|
911 // it, bound to EXPR.
|
|
912 static Expression*
|
|
913 bind_field_or_method(Gogo*, const Type* type, Expression* expr,
|
|
914 const std::string& name, Location);
|
|
915
|
|
916 // Return true if NAME is an unexported field or method of TYPE.
|
|
917 static bool
|
|
918 is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
|
|
919 std::vector<const Named_type*>*);
|
|
920
|
|
921 // Convert the builtin named types.
|
|
922 static void
|
|
923 convert_builtin_named_types(Gogo*);
|
|
924
|
|
925 // Return the backend representation of this type.
|
|
926 Btype*
|
|
927 get_backend(Gogo*);
|
|
928
|
|
929 // Return a placeholder for the backend representation of the type.
|
|
930 // This will return a type of the correct size, but for which some
|
|
931 // of the fields may still need to be completed.
|
|
932 Btype*
|
|
933 get_backend_placeholder(Gogo*);
|
|
934
|
|
935 // Finish the backend representation of a placeholder.
|
|
936 void
|
|
937 finish_backend(Gogo*, Btype*);
|
|
938
|
|
939 // Build a type descriptor entry for this type. Return a pointer to
|
|
940 // it. The location is the location which causes us to need the
|
|
941 // entry.
|
|
942 Bexpression*
|
|
943 type_descriptor_pointer(Gogo* gogo, Location);
|
|
944
|
|
945 // Build the Garbage Collection symbol for this type. Return a pointer to it.
|
|
946 Bexpression*
|
|
947 gc_symbol_pointer(Gogo* gogo);
|
|
948
|
|
949 // Return whether this type needs a garbage collection program.
|
|
950 // Sets *PTRSIZE and *PTRDATA.
|
|
951 bool
|
|
952 needs_gcprog(Gogo*, int64_t* ptrsize, int64_t* ptrdata);
|
|
953
|
|
954 // Return a ptrmask variable for this type.
|
|
955 Bvariable*
|
|
956 gc_ptrmask_var(Gogo*, int64_t ptrsize, int64_t ptrdata);
|
|
957
|
|
958 // Return the type reflection string for this type.
|
|
959 std::string
|
|
960 reflection(Gogo*) const;
|
|
961
|
|
962 // Return a mangled name for the type. This is a name which can be
|
|
963 // used in assembler code. Identical types should have the same
|
|
964 // manged name.
|
|
965 std::string
|
|
966 mangled_name(Gogo*) const;
|
|
967
|
|
968 // If the size of the type can be determined, set *PSIZE to the size
|
|
969 // in bytes and return true. Otherwise, return false. This queries
|
|
970 // the backend.
|
|
971 bool
|
|
972 backend_type_size(Gogo*, int64_t* psize);
|
|
973
|
|
974 // If the alignment of the type can be determined, set *PALIGN to
|
|
975 // the alignment in bytes and return true. Otherwise, return false.
|
|
976 bool
|
|
977 backend_type_align(Gogo*, int64_t* palign);
|
|
978
|
|
979 // If the alignment of a struct field of this type can be
|
|
980 // determined, set *PALIGN to the alignment in bytes and return
|
|
981 // true. Otherwise, return false.
|
|
982 bool
|
|
983 backend_type_field_align(Gogo*, int64_t* palign);
|
|
984
|
|
985 // Determine the ptrdata size for the backend version of this type:
|
|
986 // the length of the prefix of the type that can contain a pointer
|
|
987 // value. If it can be determined, set *PPTRDATA to the value in
|
|
988 // bytes and return true. Otherwise, return false.
|
|
989 bool
|
|
990 backend_type_ptrdata(Gogo*, int64_t* pptrdata);
|
|
991
|
|
992 // Determine the ptrdata size that we are going to set in the type
|
|
993 // descriptor. This is normally the same as backend_type_ptrdata,
|
|
994 // but differs if we use a gcprog for an array. The arguments and
|
|
995 // results are as for backend_type_ptrdata.
|
|
996 bool
|
|
997 descriptor_ptrdata(Gogo*, int64_t* pptrdata);
|
|
998
|
|
999 // Whether the backend size is known.
|
|
1000 bool
|
|
1001 is_backend_type_size_known(Gogo*);
|
|
1002
|
|
1003 // Return whether the type needs specially built type functions.
|
|
1004 bool
|
|
1005 needs_specific_type_functions(Gogo*);
|
|
1006
|
|
1007 // Get the hash and equality functions for a type.
|
|
1008 void
|
|
1009 type_functions(Gogo*, Named_type* name, Function_type* hash_fntype,
|
|
1010 Function_type* equal_fntype, Named_object** hash_fn,
|
|
1011 Named_object** equal_fn);
|
|
1012
|
|
1013 // Write the hash and equality type functions.
|
|
1014 void
|
|
1015 write_specific_type_functions(Gogo*, Named_type*, int64_t size,
|
|
1016 const std::string& hash_name,
|
|
1017 Function_type* hash_fntype,
|
|
1018 const std::string& equal_name,
|
|
1019 Function_type* equal_fntype);
|
|
1020
|
|
1021 // Return the alignment required by the memequalN function.
|
|
1022 static int64_t memequal_align(Gogo*, int size);
|
|
1023
|
|
1024 // Export the type.
|
|
1025 void
|
|
1026 export_type(Export* exp) const
|
|
1027 { this->do_export(exp); }
|
|
1028
|
|
1029 // Import a type.
|
|
1030 static Type*
|
|
1031 import_type(Import*);
|
|
1032
|
|
1033 protected:
|
|
1034 Type(Type_classification);
|
|
1035
|
|
1036 // Functions implemented by the child class.
|
|
1037
|
|
1038 // Traverse the subtypes.
|
|
1039 virtual int
|
|
1040 do_traverse(Traverse*);
|
|
1041
|
|
1042 // Verify the type.
|
|
1043 virtual bool
|
|
1044 do_verify()
|
|
1045 { return true; }
|
|
1046
|
|
1047 virtual bool
|
|
1048 do_has_pointer() const
|
|
1049 { return false; }
|
|
1050
|
|
1051 virtual bool
|
|
1052 do_compare_is_identity(Gogo*) = 0;
|
|
1053
|
|
1054 virtual bool
|
|
1055 do_is_reflexive()
|
|
1056 { return true; }
|
|
1057
|
|
1058 virtual bool
|
|
1059 do_needs_key_update()
|
|
1060 { return false; }
|
|
1061
|
|
1062 virtual bool
|
|
1063 do_in_heap()
|
|
1064 { return true; }
|
|
1065
|
|
1066 virtual unsigned int
|
|
1067 do_hash_for_method(Gogo*) const;
|
|
1068
|
|
1069 virtual Btype*
|
|
1070 do_get_backend(Gogo*) = 0;
|
|
1071
|
|
1072 virtual Expression*
|
|
1073 do_type_descriptor(Gogo*, Named_type* name) = 0;
|
|
1074
|
|
1075 virtual void
|
|
1076 do_reflection(Gogo*, std::string*) const = 0;
|
|
1077
|
|
1078 virtual void
|
|
1079 do_mangled_name(Gogo*, std::string*) const = 0;
|
|
1080
|
|
1081 virtual void
|
|
1082 do_export(Export*) const;
|
|
1083
|
|
1084 // Return whether a method expects a pointer as the receiver.
|
|
1085 static bool
|
|
1086 method_expects_pointer(const Named_object*);
|
|
1087
|
|
1088 // Finalize the methods for a type.
|
|
1089 static void
|
|
1090 finalize_methods(Gogo*, const Type*, Location, Methods**);
|
|
1091
|
|
1092 // Return a method from a set of methods.
|
|
1093 static Method*
|
|
1094 method_function(const Methods*, const std::string& name,
|
|
1095 bool* is_ambiguous);
|
|
1096
|
|
1097 // A mapping from interfaces to the associated interface method
|
|
1098 // tables for this type. This maps to a decl.
|
|
1099 typedef Unordered_map_hash(Interface_type*, Expression*, Type_hash_identical,
|
|
1100 Type_identical) Interface_method_tables;
|
|
1101
|
|
1102 // Return a pointer to the interface method table for TYPE for the
|
|
1103 // interface INTERFACE.
|
|
1104 static Expression*
|
|
1105 interface_method_table(Type* type,
|
|
1106 Interface_type *interface, bool is_pointer,
|
|
1107 Interface_method_tables** method_tables,
|
|
1108 Interface_method_tables** pointer_tables);
|
|
1109
|
|
1110 // Return a composite literal for the type descriptor entry for a
|
|
1111 // type.
|
|
1112 static Expression*
|
|
1113 type_descriptor(Gogo*, Type*);
|
|
1114
|
|
1115 // Return a composite literal for the type descriptor entry for
|
|
1116 // TYPE, using NAME as the name of the type.
|
|
1117 static Expression*
|
|
1118 named_type_descriptor(Gogo*, Type* type, Named_type* name);
|
|
1119
|
|
1120 // Return a composite literal for a plain type descriptor for this
|
|
1121 // type with the given kind and name.
|
|
1122 Expression*
|
|
1123 plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
|
|
1124
|
|
1125 // Build a composite literal for the basic type descriptor.
|
|
1126 Expression*
|
|
1127 type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
|
|
1128 const Methods*, bool only_value_methods);
|
|
1129
|
|
1130 // For the benefit of child class reflection string generation.
|
|
1131 void
|
|
1132 append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
|
|
1133 { type->do_reflection(gogo, ret); }
|
|
1134
|
|
1135 // For the benefit of child class mangling.
|
|
1136 void
|
|
1137 append_mangled_name(const Type* type, Gogo* gogo, std::string* ret) const
|
|
1138 { type->do_mangled_name(gogo, ret); }
|
|
1139
|
|
1140 // Incorporate a string into a hash code.
|
|
1141 static unsigned int
|
|
1142 hash_string(const std::string&, unsigned int);
|
|
1143
|
|
1144 // Return the backend representation for the underlying type of a
|
|
1145 // named type.
|
|
1146 static Btype*
|
|
1147 get_named_base_btype(Gogo* gogo, Type* base_type)
|
|
1148 { return base_type->get_btype_without_hash(gogo); }
|
|
1149
|
|
1150 private:
|
|
1151 // Convert to the desired type classification, or return NULL. This
|
|
1152 // is a controlled dynamic_cast.
|
|
1153 template<typename Type_class, Type_classification type_classification>
|
|
1154 Type_class*
|
|
1155 convert()
|
|
1156 {
|
|
1157 Type* base = this->base();
|
|
1158 return (base->classification_ == type_classification
|
|
1159 ? static_cast<Type_class*>(base)
|
|
1160 : NULL);
|
|
1161 }
|
|
1162
|
|
1163 template<typename Type_class, Type_classification type_classification>
|
|
1164 const Type_class*
|
|
1165 convert() const
|
|
1166 {
|
|
1167 const Type* base = this->base();
|
|
1168 return (base->classification_ == type_classification
|
|
1169 ? static_cast<Type_class*>(base)
|
|
1170 : NULL);
|
|
1171 }
|
|
1172
|
|
1173 template<typename Type_class, Type_classification type_classification>
|
|
1174 Type_class*
|
|
1175 convert_no_base()
|
|
1176 {
|
|
1177 return (this->classification_ == type_classification
|
|
1178 ? static_cast<Type_class*>(this)
|
|
1179 : NULL);
|
|
1180 }
|
|
1181
|
|
1182 template<typename Type_class, Type_classification type_classification>
|
|
1183 const Type_class*
|
|
1184 convert_no_base() const
|
|
1185 {
|
|
1186 return (this->classification_ == type_classification
|
|
1187 ? static_cast<Type_class*>(this)
|
|
1188 : NULL);
|
|
1189 }
|
|
1190
|
|
1191 // Map unnamed types to type descriptor decls.
|
|
1192 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
|
|
1193 Type_identical) Type_descriptor_vars;
|
|
1194
|
|
1195 static Type_descriptor_vars type_descriptor_vars;
|
|
1196
|
|
1197 // Build the type descriptor variable for this type.
|
|
1198 void
|
|
1199 make_type_descriptor_var(Gogo*);
|
|
1200
|
|
1201 // Map unnamed types to type descriptor decls.
|
|
1202 typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
|
|
1203 Type_identical) GC_symbol_vars;
|
|
1204
|
|
1205 static GC_symbol_vars gc_symbol_vars;
|
|
1206
|
|
1207 // Map ptrmask symbol names to the ptrmask variable.
|
|
1208 typedef Unordered_map(std::string, Bvariable*) GC_gcbits_vars;
|
|
1209
|
|
1210 static GC_gcbits_vars gc_gcbits_vars;
|
|
1211
|
|
1212 // Build the GC symbol for this type.
|
|
1213 void
|
|
1214 make_gc_symbol_var(Gogo*);
|
|
1215
|
|
1216 // Return true if the type descriptor for this type should be
|
|
1217 // defined in some other package. If NAME is not NULL, it is the
|
|
1218 // name of this type. If this returns true it sets *PACKAGE to the
|
|
1219 // package where the type descriptor is defined.
|
|
1220 bool
|
|
1221 type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
|
|
1222
|
|
1223 // Make a composite literal for the garbage collection program for
|
|
1224 // this type.
|
|
1225 Expression*
|
|
1226 gcprog_constructor(Gogo*, int64_t ptrsize, int64_t ptrdata);
|
|
1227
|
|
1228 // Build the hash and equality type functions for a type which needs
|
|
1229 // specific functions.
|
|
1230 void
|
|
1231 specific_type_functions(Gogo*, Named_type*, int64_t size,
|
|
1232 Function_type* hash_fntype,
|
|
1233 Function_type* equal_fntype, Named_object** hash_fn,
|
|
1234 Named_object** equal_fn);
|
|
1235
|
|
1236 void
|
|
1237 write_identity_hash(Gogo*, int64_t size);
|
|
1238
|
|
1239 void
|
|
1240 write_identity_equal(Gogo*, int64_t size);
|
|
1241
|
|
1242 void
|
|
1243 write_named_hash(Gogo*, Named_type*, Function_type* hash_fntype,
|
|
1244 Function_type* equal_fntype);
|
|
1245
|
|
1246 void
|
|
1247 write_named_equal(Gogo*, Named_type*);
|
|
1248
|
|
1249 // Build a composite literal for the uncommon type information.
|
|
1250 Expression*
|
|
1251 uncommon_type_constructor(Gogo*, Type* uncommon_type,
|
|
1252 Named_type*, const Methods*,
|
|
1253 bool only_value_methods) const;
|
|
1254
|
|
1255 // Build a composite literal for the methods.
|
|
1256 Expression*
|
|
1257 methods_constructor(Gogo*, Type* methods_type, const Methods*,
|
|
1258 bool only_value_methods) const;
|
|
1259
|
|
1260 // Build a composite literal for one method.
|
|
1261 Expression*
|
|
1262 method_constructor(Gogo*, Type* method_type, const std::string& name,
|
|
1263 const Method*, bool only_value_methods) const;
|
|
1264
|
|
1265 // Add all methods for TYPE to the list of methods for THIS.
|
|
1266 static void
|
|
1267 add_methods_for_type(const Type* type, const Method::Field_indexes*,
|
|
1268 unsigned int depth, bool, bool,
|
|
1269 std::vector<const Named_type*>*,
|
|
1270 Methods*);
|
|
1271
|
|
1272 static void
|
|
1273 add_local_methods_for_type(const Named_type* type,
|
|
1274 const Method::Field_indexes*,
|
|
1275 unsigned int depth, bool, bool, Methods*);
|
|
1276
|
|
1277 static void
|
|
1278 add_embedded_methods_for_type(const Type* type,
|
|
1279 const Method::Field_indexes*,
|
|
1280 unsigned int depth, bool, bool,
|
|
1281 std::vector<const Named_type*>*,
|
|
1282 Methods*);
|
|
1283
|
|
1284 static void
|
|
1285 add_interface_methods_for_type(const Type* type,
|
|
1286 const Method::Field_indexes*,
|
|
1287 unsigned int depth, Methods*);
|
|
1288
|
|
1289 // Build stub methods for a type.
|
|
1290 static void
|
|
1291 build_stub_methods(Gogo*, const Type* type, const Methods* methods,
|
|
1292 Location);
|
|
1293
|
|
1294 static void
|
|
1295 build_one_stub_method(Gogo*, Method*, const char* receiver_name,
|
|
1296 const Typed_identifier_list*, bool is_varargs,
|
|
1297 Location);
|
|
1298
|
|
1299 static Expression*
|
|
1300 apply_field_indexes(Expression*, const Method::Field_indexes*,
|
|
1301 Location);
|
|
1302
|
|
1303 // Look for a field or method named NAME in TYPE.
|
|
1304 static bool
|
|
1305 find_field_or_method(const Type* type, const std::string& name,
|
|
1306 bool receiver_can_be_pointer,
|
|
1307 std::vector<const Named_type*>*, int* level,
|
|
1308 bool* is_method, bool* found_pointer_method,
|
|
1309 std::string* ambig1, std::string* ambig2);
|
|
1310
|
|
1311 // Get the backend representation for a type without looking in the
|
|
1312 // hash table for identical types.
|
|
1313 Btype*
|
|
1314 get_btype_without_hash(Gogo*);
|
|
1315
|
|
1316 // A backend type that may be a placeholder.
|
|
1317 struct Type_btype_entry
|
|
1318 {
|
|
1319 Btype *btype;
|
|
1320 bool is_placeholder;
|
|
1321 };
|
|
1322
|
|
1323 // A mapping from Type to Btype*, used to ensure that the backend
|
|
1324 // representation of identical types is identical. This is only
|
|
1325 // used for unnamed types.
|
|
1326 typedef Unordered_map_hash(const Type*, Type_btype_entry,
|
|
1327 Type_hash_identical, Type_identical) Type_btypes;
|
|
1328
|
|
1329 static Type_btypes type_btypes;
|
|
1330
|
|
1331 // A list of builtin named types.
|
|
1332 static std::vector<Named_type*> named_builtin_types;
|
|
1333
|
|
1334 // A map from types which need specific type functions to the type
|
|
1335 // functions themselves.
|
|
1336 typedef std::pair<Named_object*, Named_object*> Hash_equal_fn;
|
|
1337 typedef Unordered_map_hash(const Type*, Hash_equal_fn, Type_hash_identical,
|
|
1338 Type_identical) Type_functions;
|
|
1339
|
|
1340 static Type_functions type_functions_table;
|
|
1341
|
|
1342 // Cache for reusing existing pointer types; maps from pointed-to-type
|
|
1343 // to pointer type.
|
|
1344 typedef Unordered_map(Type*, Pointer_type*) Pointer_type_table;
|
|
1345
|
|
1346 static Pointer_type_table pointer_types;
|
|
1347
|
|
1348 // List of placeholder pointer types.
|
|
1349 static std::vector<Pointer_type*> placeholder_pointers;
|
|
1350
|
|
1351 // The type classification.
|
|
1352 Type_classification classification_;
|
|
1353 // The backend representation of the type, once it has been
|
|
1354 // determined.
|
|
1355 Btype* btype_;
|
|
1356 // The type descriptor for this type. This starts out as NULL and
|
|
1357 // is filled in as needed.
|
|
1358 Bvariable* type_descriptor_var_;
|
|
1359 // The GC symbol for this type. This starts out as NULL and
|
|
1360 // is filled in as needed.
|
|
1361 Bvariable* gc_symbol_var_;
|
|
1362 // Whether this type can appear in the heap.
|
|
1363 bool in_heap_;
|
|
1364 };
|
|
1365
|
|
1366 // Type hash table operations.
|
|
1367
|
|
1368 class Type_hash_identical
|
|
1369 {
|
|
1370 public:
|
|
1371 unsigned int
|
|
1372 operator()(const Type* type) const
|
|
1373 { return type->hash_for_method(NULL); }
|
|
1374 };
|
|
1375
|
|
1376 class Type_identical
|
|
1377 {
|
|
1378 public:
|
|
1379 bool
|
|
1380 operator()(const Type* t1, const Type* t2) const
|
|
1381 { return Type::are_identical(t1, t2, false, NULL); }
|
|
1382 };
|
|
1383
|
|
1384 // An identifier with a type.
|
|
1385
|
|
1386 class Typed_identifier
|
|
1387 {
|
|
1388 public:
|
|
1389 Typed_identifier(const std::string& name, Type* type,
|
|
1390 Location location)
|
|
1391 : name_(name), type_(type), location_(location), note_(NULL)
|
|
1392 { }
|
|
1393
|
|
1394 // Get the name.
|
|
1395 const std::string&
|
|
1396 name() const
|
|
1397 { return this->name_; }
|
|
1398
|
|
1399 // Get the type.
|
|
1400 Type*
|
|
1401 type() const
|
|
1402 { return this->type_; }
|
|
1403
|
|
1404 // Return the location where the name was seen. This is not always
|
|
1405 // meaningful.
|
|
1406 Location
|
|
1407 location() const
|
|
1408 { return this->location_; }
|
|
1409
|
|
1410 // Set the type--sometimes we see the identifier before the type.
|
|
1411 void
|
|
1412 set_type(Type* type)
|
|
1413 {
|
|
1414 go_assert(this->type_ == NULL || type->is_error_type());
|
|
1415 this->type_ = type;
|
|
1416 }
|
|
1417
|
|
1418 // Get the escape note.
|
|
1419 std::string*
|
|
1420 note() const
|
|
1421 { return this->note_; }
|
|
1422
|
|
1423 // Set the escape note.
|
|
1424 void
|
|
1425 set_note(const std::string& note)
|
|
1426 { this->note_ = new std::string(note); }
|
|
1427
|
|
1428 private:
|
|
1429 // Identifier name.
|
|
1430 std::string name_;
|
|
1431 // Type.
|
|
1432 Type* type_;
|
|
1433 // The location where the name was seen.
|
|
1434 Location location_;
|
|
1435 // Escape note for this typed identifier. Used when importing and exporting
|
|
1436 // functions.
|
|
1437 std::string* note_;
|
|
1438 };
|
|
1439
|
|
1440 // A list of Typed_identifiers.
|
|
1441
|
|
1442 class Typed_identifier_list
|
|
1443 {
|
|
1444 public:
|
|
1445 Typed_identifier_list()
|
|
1446 : entries_()
|
|
1447 { }
|
|
1448
|
|
1449 // Whether the list is empty.
|
|
1450 bool
|
|
1451 empty() const
|
|
1452 { return this->entries_.empty(); }
|
|
1453
|
|
1454 // Return the number of entries in the list.
|
|
1455 size_t
|
|
1456 size() const
|
|
1457 { return this->entries_.size(); }
|
|
1458
|
|
1459 // Add an entry to the end of the list.
|
|
1460 void
|
|
1461 push_back(const Typed_identifier& td)
|
|
1462 { this->entries_.push_back(td); }
|
|
1463
|
|
1464 // Remove an entry from the end of the list.
|
|
1465 void
|
|
1466 pop_back()
|
|
1467 { this->entries_.pop_back(); }
|
|
1468
|
|
1469 // Set the type of entry I to TYPE.
|
|
1470 void
|
|
1471 set_type(size_t i, Type* type)
|
|
1472 {
|
|
1473 go_assert(i < this->entries_.size());
|
|
1474 this->entries_[i].set_type(type);
|
|
1475 }
|
|
1476
|
|
1477 // Sort the entries by name.
|
|
1478 void
|
|
1479 sort_by_name();
|
|
1480
|
|
1481 // Traverse types.
|
|
1482 int
|
|
1483 traverse(Traverse*);
|
|
1484
|
|
1485 // Return the first and last elements.
|
|
1486 Typed_identifier&
|
|
1487 front()
|
|
1488 { return this->entries_.front(); }
|
|
1489
|
|
1490 const Typed_identifier&
|
|
1491 front() const
|
|
1492 { return this->entries_.front(); }
|
|
1493
|
|
1494 Typed_identifier&
|
|
1495 back()
|
|
1496 { return this->entries_.back(); }
|
|
1497
|
|
1498 const Typed_identifier&
|
|
1499 back() const
|
|
1500 { return this->entries_.back(); }
|
|
1501
|
|
1502 Typed_identifier&
|
|
1503 at(size_t i)
|
|
1504 { return this->entries_.at(i); }
|
|
1505
|
|
1506 const Typed_identifier&
|
|
1507 at(size_t i) const
|
|
1508 { return this->entries_.at(i); }
|
|
1509
|
|
1510 void
|
|
1511 set(size_t i, const Typed_identifier& t)
|
|
1512 { this->entries_.at(i) = t; }
|
|
1513
|
|
1514 void
|
|
1515 resize(size_t c)
|
|
1516 {
|
|
1517 go_assert(c <= this->entries_.size());
|
|
1518 this->entries_.resize(c, Typed_identifier("", NULL,
|
|
1519 Linemap::unknown_location()));
|
|
1520 }
|
|
1521
|
|
1522 void
|
|
1523 reserve(size_t c)
|
|
1524 { this->entries_.reserve(c); }
|
|
1525
|
|
1526 // Iterators.
|
|
1527
|
|
1528 typedef std::vector<Typed_identifier>::iterator iterator;
|
|
1529 typedef std::vector<Typed_identifier>::const_iterator const_iterator;
|
|
1530
|
|
1531 iterator
|
|
1532 begin()
|
|
1533 { return this->entries_.begin(); }
|
|
1534
|
|
1535 const_iterator
|
|
1536 begin() const
|
|
1537 { return this->entries_.begin(); }
|
|
1538
|
|
1539 iterator
|
|
1540 end()
|
|
1541 { return this->entries_.end(); }
|
|
1542
|
|
1543 const_iterator
|
|
1544 end() const
|
|
1545 { return this->entries_.end(); }
|
|
1546
|
|
1547 // Return a copy of this list. This returns an independent copy of
|
|
1548 // the vector, but does not copy the types.
|
|
1549 Typed_identifier_list*
|
|
1550 copy() const;
|
|
1551
|
|
1552 private:
|
|
1553 std::vector<Typed_identifier> entries_;
|
|
1554 };
|
|
1555
|
|
1556 // A type used to indicate a parsing error. This exists to simplify
|
|
1557 // later error detection.
|
|
1558
|
|
1559 class Error_type : public Type
|
|
1560 {
|
|
1561 public:
|
|
1562 Error_type()
|
|
1563 : Type(TYPE_ERROR)
|
|
1564 { }
|
|
1565
|
|
1566 protected:
|
|
1567 bool
|
|
1568 do_compare_is_identity(Gogo*)
|
|
1569 { return false; }
|
|
1570
|
|
1571 Btype*
|
|
1572 do_get_backend(Gogo* gogo);
|
|
1573
|
|
1574 Expression*
|
|
1575 do_type_descriptor(Gogo*, Named_type*);
|
|
1576
|
|
1577 void
|
|
1578 do_reflection(Gogo*, std::string*) const;
|
|
1579
|
|
1580 void
|
|
1581 do_mangled_name(Gogo*, std::string* ret) const;
|
|
1582 };
|
|
1583
|
|
1584 // The void type.
|
|
1585
|
|
1586 class Void_type : public Type
|
|
1587 {
|
|
1588 public:
|
|
1589 Void_type()
|
|
1590 : Type(TYPE_VOID)
|
|
1591 { }
|
|
1592
|
|
1593 protected:
|
|
1594 bool
|
|
1595 do_compare_is_identity(Gogo*)
|
|
1596 { return false; }
|
|
1597
|
|
1598 Btype*
|
|
1599 do_get_backend(Gogo* gogo);
|
|
1600
|
|
1601 Expression*
|
|
1602 do_type_descriptor(Gogo*, Named_type*)
|
|
1603 { go_unreachable(); }
|
|
1604
|
|
1605 void
|
|
1606 do_reflection(Gogo*, std::string*) const
|
|
1607 { }
|
|
1608
|
|
1609 void
|
|
1610 do_mangled_name(Gogo*, std::string* ret) const;
|
|
1611 };
|
|
1612
|
|
1613 // The boolean type.
|
|
1614
|
|
1615 class Boolean_type : public Type
|
|
1616 {
|
|
1617 public:
|
|
1618 Boolean_type()
|
|
1619 : Type(TYPE_BOOLEAN)
|
|
1620 { }
|
|
1621
|
|
1622 protected:
|
|
1623 bool
|
|
1624 do_compare_is_identity(Gogo*)
|
|
1625 { return true; }
|
|
1626
|
|
1627 Btype*
|
|
1628 do_get_backend(Gogo* gogo);
|
|
1629
|
|
1630 Expression*
|
|
1631 do_type_descriptor(Gogo*, Named_type* name);
|
|
1632
|
|
1633 // We should not be asked for the reflection string of a basic type.
|
|
1634 void
|
|
1635 do_reflection(Gogo*, std::string* ret) const
|
|
1636 { ret->append("bool"); }
|
|
1637
|
|
1638 void
|
|
1639 do_mangled_name(Gogo*, std::string* ret) const;
|
|
1640 };
|
|
1641
|
|
1642 // The type of an integer.
|
|
1643
|
|
1644 class Integer_type : public Type
|
|
1645 {
|
|
1646 public:
|
|
1647 // Create a new integer type.
|
|
1648 static Named_type*
|
|
1649 create_integer_type(const char* name, bool is_unsigned, int bits,
|
|
1650 int runtime_type_kind);
|
|
1651
|
|
1652 // Look up an existing integer type.
|
|
1653 static Named_type*
|
|
1654 lookup_integer_type(const char* name);
|
|
1655
|
|
1656 // Create an abstract integer type.
|
|
1657 static Integer_type*
|
|
1658 create_abstract_integer_type();
|
|
1659
|
|
1660 // Create an abstract character type.
|
|
1661 static Integer_type*
|
|
1662 create_abstract_character_type();
|
|
1663
|
|
1664 // Whether this is an abstract integer type.
|
|
1665 bool
|
|
1666 is_abstract() const
|
|
1667 { return this->is_abstract_; }
|
|
1668
|
|
1669 // Whether this is an unsigned type.
|
|
1670 bool
|
|
1671 is_unsigned() const
|
|
1672 { return this->is_unsigned_; }
|
|
1673
|
|
1674 // The number of bits.
|
|
1675 int
|
|
1676 bits() const
|
|
1677 { return this->bits_; }
|
|
1678
|
|
1679 // Whether this type is the same as T.
|
|
1680 bool
|
|
1681 is_identical(const Integer_type* t) const;
|
|
1682
|
|
1683 // Whether this is the type "byte" or another name for "byte".
|
|
1684 bool
|
|
1685 is_byte() const
|
|
1686 { return this->is_byte_; }
|
|
1687
|
|
1688 // Mark this as the "byte" type.
|
|
1689 void
|
|
1690 set_is_byte()
|
|
1691 { this->is_byte_ = true; }
|
|
1692
|
|
1693 // Whether this is the type "rune" or another name for "rune".
|
|
1694 bool
|
|
1695 is_rune() const
|
|
1696 { return this->is_rune_; }
|
|
1697
|
|
1698 // Mark this as the "rune" type.
|
|
1699 void
|
|
1700 set_is_rune()
|
|
1701 { this->is_rune_ = true; }
|
|
1702
|
|
1703 protected:
|
|
1704 bool
|
|
1705 do_compare_is_identity(Gogo*)
|
|
1706 { return true; }
|
|
1707
|
|
1708 unsigned int
|
|
1709 do_hash_for_method(Gogo*) const;
|
|
1710
|
|
1711 Btype*
|
|
1712 do_get_backend(Gogo*);
|
|
1713
|
|
1714 Expression*
|
|
1715 do_type_descriptor(Gogo*, Named_type*);
|
|
1716
|
|
1717 void
|
|
1718 do_reflection(Gogo*, std::string*) const;
|
|
1719
|
|
1720 void
|
|
1721 do_mangled_name(Gogo*, std::string*) const;
|
|
1722
|
|
1723 private:
|
|
1724 Integer_type(bool is_abstract, bool is_unsigned, int bits,
|
|
1725 int runtime_type_kind)
|
|
1726 : Type(TYPE_INTEGER),
|
|
1727 is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
|
|
1728 is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
|
|
1729 { }
|
|
1730
|
|
1731 // Map names of integer types to the types themselves.
|
|
1732 typedef std::map<std::string, Named_type*> Named_integer_types;
|
|
1733 static Named_integer_types named_integer_types;
|
|
1734
|
|
1735 // True if this is an abstract type.
|
|
1736 bool is_abstract_;
|
|
1737 // True if this is an unsigned type.
|
|
1738 bool is_unsigned_;
|
|
1739 // True if this is the byte type.
|
|
1740 bool is_byte_;
|
|
1741 // True if this is the rune type.
|
|
1742 bool is_rune_;
|
|
1743 // The number of bits.
|
|
1744 int bits_;
|
|
1745 // The runtime type code used in the type descriptor for this type.
|
|
1746 int runtime_type_kind_;
|
|
1747 };
|
|
1748
|
|
1749 // The type of a floating point number.
|
|
1750
|
|
1751 class Float_type : public Type
|
|
1752 {
|
|
1753 public:
|
|
1754 // Create a new float type.
|
|
1755 static Named_type*
|
|
1756 create_float_type(const char* name, int bits, int runtime_type_kind);
|
|
1757
|
|
1758 // Look up an existing float type.
|
|
1759 static Named_type*
|
|
1760 lookup_float_type(const char* name);
|
|
1761
|
|
1762 // Create an abstract float type.
|
|
1763 static Float_type*
|
|
1764 create_abstract_float_type();
|
|
1765
|
|
1766 // Whether this is an abstract float type.
|
|
1767 bool
|
|
1768 is_abstract() const
|
|
1769 { return this->is_abstract_; }
|
|
1770
|
|
1771 // The number of bits.
|
|
1772 int
|
|
1773 bits() const
|
|
1774 { return this->bits_; }
|
|
1775
|
|
1776 // Whether this type is the same as T.
|
|
1777 bool
|
|
1778 is_identical(const Float_type* t) const;
|
|
1779
|
|
1780 protected:
|
|
1781 bool
|
|
1782 do_compare_is_identity(Gogo*)
|
|
1783 { return false; }
|
|
1784
|
|
1785 bool
|
|
1786 do_is_reflexive()
|
|
1787 { return false; }
|
|
1788
|
|
1789 // Distinction between +0 and -0 requires a key update.
|
|
1790 bool
|
|
1791 do_needs_key_update()
|
|
1792 { return true; }
|
|
1793
|
|
1794 unsigned int
|
|
1795 do_hash_for_method(Gogo*) const;
|
|
1796
|
|
1797 Btype*
|
|
1798 do_get_backend(Gogo*);
|
|
1799
|
|
1800 Expression*
|
|
1801 do_type_descriptor(Gogo*, Named_type*);
|
|
1802
|
|
1803 void
|
|
1804 do_reflection(Gogo*, std::string*) const;
|
|
1805
|
|
1806 void
|
|
1807 do_mangled_name(Gogo*, std::string*) const;
|
|
1808
|
|
1809 private:
|
|
1810 Float_type(bool is_abstract, int bits, int runtime_type_kind)
|
|
1811 : Type(TYPE_FLOAT),
|
|
1812 is_abstract_(is_abstract), bits_(bits),
|
|
1813 runtime_type_kind_(runtime_type_kind)
|
|
1814 { }
|
|
1815
|
|
1816 // Map names of float types to the types themselves.
|
|
1817 typedef std::map<std::string, Named_type*> Named_float_types;
|
|
1818 static Named_float_types named_float_types;
|
|
1819
|
|
1820 // True if this is an abstract type.
|
|
1821 bool is_abstract_;
|
|
1822 // The number of bits in the floating point value.
|
|
1823 int bits_;
|
|
1824 // The runtime type code used in the type descriptor for this type.
|
|
1825 int runtime_type_kind_;
|
|
1826 };
|
|
1827
|
|
1828 // The type of a complex number.
|
|
1829
|
|
1830 class Complex_type : public Type
|
|
1831 {
|
|
1832 public:
|
|
1833 // Create a new complex type.
|
|
1834 static Named_type*
|
|
1835 create_complex_type(const char* name, int bits, int runtime_type_kind);
|
|
1836
|
|
1837 // Look up an existing complex type.
|
|
1838 static Named_type*
|
|
1839 lookup_complex_type(const char* name);
|
|
1840
|
|
1841 // Create an abstract complex type.
|
|
1842 static Complex_type*
|
|
1843 create_abstract_complex_type();
|
|
1844
|
|
1845 // Whether this is an abstract complex type.
|
|
1846 bool
|
|
1847 is_abstract() const
|
|
1848 { return this->is_abstract_; }
|
|
1849
|
|
1850 // The number of bits: 64 or 128.
|
|
1851 int bits() const
|
|
1852 { return this->bits_; }
|
|
1853
|
|
1854 // Whether this type is the same as T.
|
|
1855 bool
|
|
1856 is_identical(const Complex_type* t) const;
|
|
1857
|
|
1858 protected:
|
|
1859 bool
|
|
1860 do_compare_is_identity(Gogo*)
|
|
1861 { return false; }
|
|
1862
|
|
1863 bool
|
|
1864 do_is_reflexive()
|
|
1865 { return false; }
|
|
1866
|
|
1867 // Distinction between +0 and -0 requires a key update.
|
|
1868 bool
|
|
1869 do_needs_key_update()
|
|
1870 { return true; }
|
|
1871
|
|
1872 unsigned int
|
|
1873 do_hash_for_method(Gogo*) const;
|
|
1874
|
|
1875 Btype*
|
|
1876 do_get_backend(Gogo*);
|
|
1877
|
|
1878 Expression*
|
|
1879 do_type_descriptor(Gogo*, Named_type*);
|
|
1880
|
|
1881 void
|
|
1882 do_reflection(Gogo*, std::string*) const;
|
|
1883
|
|
1884 void
|
|
1885 do_mangled_name(Gogo*, std::string*) const;
|
|
1886
|
|
1887 private:
|
|
1888 Complex_type(bool is_abstract, int bits, int runtime_type_kind)
|
|
1889 : Type(TYPE_COMPLEX),
|
|
1890 is_abstract_(is_abstract), bits_(bits),
|
|
1891 runtime_type_kind_(runtime_type_kind)
|
|
1892 { }
|
|
1893
|
|
1894 // Map names of complex types to the types themselves.
|
|
1895 typedef std::map<std::string, Named_type*> Named_complex_types;
|
|
1896 static Named_complex_types named_complex_types;
|
|
1897
|
|
1898 // True if this is an abstract type.
|
|
1899 bool is_abstract_;
|
|
1900 // The number of bits in the complex value--64 or 128.
|
|
1901 int bits_;
|
|
1902 // The runtime type code used in the type descriptor for this type.
|
|
1903 int runtime_type_kind_;
|
|
1904 };
|
|
1905
|
|
1906 // The type of a string.
|
|
1907
|
|
1908 class String_type : public Type
|
|
1909 {
|
|
1910 public:
|
|
1911 String_type()
|
|
1912 : Type(TYPE_STRING)
|
|
1913 { }
|
|
1914
|
|
1915 protected:
|
|
1916 bool
|
|
1917 do_has_pointer() const
|
|
1918 { return true; }
|
|
1919
|
|
1920 bool
|
|
1921 do_compare_is_identity(Gogo*)
|
|
1922 { return false; }
|
|
1923
|
|
1924 // New string might have a smaller backing store.
|
|
1925 bool
|
|
1926 do_needs_key_update()
|
|
1927 { return true; }
|
|
1928
|
|
1929 Btype*
|
|
1930 do_get_backend(Gogo*);
|
|
1931
|
|
1932 Expression*
|
|
1933 do_type_descriptor(Gogo*, Named_type*);
|
|
1934
|
|
1935 void
|
|
1936 do_reflection(Gogo*, std::string*) const;
|
|
1937
|
|
1938 void
|
|
1939 do_mangled_name(Gogo*, std::string* ret) const;
|
|
1940
|
|
1941 private:
|
|
1942 // The named string type.
|
|
1943 static Named_type* string_type_;
|
|
1944 };
|
|
1945
|
|
1946 // The type of a function.
|
|
1947
|
|
1948 class Function_type : public Type
|
|
1949 {
|
|
1950 public:
|
|
1951 Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
|
|
1952 Typed_identifier_list* results, Location location)
|
|
1953 : Type(TYPE_FUNCTION),
|
|
1954 receiver_(receiver), parameters_(parameters), results_(results),
|
|
1955 location_(location), is_varargs_(false), is_builtin_(false),
|
|
1956 fnbtype_(NULL), is_tagged_(false)
|
|
1957 { }
|
|
1958
|
|
1959 // Get the receiver.
|
|
1960 const Typed_identifier*
|
|
1961 receiver() const
|
|
1962 { return this->receiver_; }
|
|
1963
|
|
1964 // Add an escape note for the receiver.
|
|
1965 void
|
|
1966 add_receiver_note(int encoding)
|
|
1967 { this->receiver_->set_note(Escape_note::make_tag(encoding)); }
|
|
1968
|
|
1969 // Get the return names and types.
|
|
1970 const Typed_identifier_list*
|
|
1971 results() const
|
|
1972 { return this->results_; }
|
|
1973
|
|
1974 // Get the parameter names and types.
|
|
1975 const Typed_identifier_list*
|
|
1976 parameters() const
|
|
1977 { return this->parameters_; }
|
|
1978
|
|
1979 // Add an escape note for the ith parameter.
|
|
1980 void
|
|
1981 add_parameter_note(int index, int encoding)
|
|
1982 { this->parameters_->at(index).set_note(Escape_note::make_tag(encoding)); }
|
|
1983
|
|
1984 // Whether this function has been tagged during escape analysis.
|
|
1985 bool
|
|
1986 is_tagged() const
|
|
1987 { return this->is_tagged_; }
|
|
1988
|
|
1989 // Mark this function as tagged after analyzing its escape.
|
|
1990 void
|
|
1991 set_is_tagged()
|
|
1992 { this->is_tagged_ = true; }
|
|
1993
|
|
1994 // Whether this is a varargs function.
|
|
1995 bool
|
|
1996 is_varargs() const
|
|
1997 { return this->is_varargs_; }
|
|
1998
|
|
1999 // Whether this is a builtin function.
|
|
2000 bool
|
|
2001 is_builtin() const
|
|
2002 { return this->is_builtin_; }
|
|
2003
|
|
2004 // The location where this type was defined.
|
|
2005 Location
|
|
2006 location() const
|
|
2007 { return this->location_; }
|
|
2008
|
|
2009 // Return whether this is a method type.
|
|
2010 bool
|
|
2011 is_method() const
|
|
2012 { return this->receiver_ != NULL; }
|
|
2013
|
|
2014 // Whether T is a valid redeclaration of this type. This is called
|
|
2015 // when a function is declared more than once.
|
|
2016 bool
|
|
2017 is_valid_redeclaration(const Function_type* t, std::string*) const;
|
|
2018
|
|
2019 // Whether this type is the same as T.
|
|
2020 bool
|
|
2021 is_identical(const Function_type* t, bool ignore_receiver,
|
|
2022 Cmp_tags, bool errors_are_identical, std::string*) const;
|
|
2023
|
|
2024 // Record that this is a varargs function.
|
|
2025 void
|
|
2026 set_is_varargs()
|
|
2027 { this->is_varargs_ = true; }
|
|
2028
|
|
2029 // Record that this is a builtin function.
|
|
2030 void
|
|
2031 set_is_builtin()
|
|
2032 { this->is_builtin_ = true; }
|
|
2033
|
|
2034 // Import a function type.
|
|
2035 static Function_type*
|
|
2036 do_import(Import*);
|
|
2037
|
|
2038 // Return a copy of this type without a receiver. This is only
|
|
2039 // valid for a method type.
|
|
2040 Function_type*
|
|
2041 copy_without_receiver() const;
|
|
2042
|
|
2043 // Return a copy of this type with a receiver. This is used when an
|
|
2044 // interface method is attached to a named or struct type.
|
|
2045 Function_type*
|
|
2046 copy_with_receiver(Type*) const;
|
|
2047
|
|
2048 // Return a copy of this type with the receiver treated as the first
|
|
2049 // parameter. If WANT_POINTER_RECEIVER is true, the receiver is
|
|
2050 // forced to be a pointer.
|
|
2051 Function_type*
|
|
2052 copy_with_receiver_as_param(bool want_pointer_receiver) const;
|
|
2053
|
|
2054 // Return a copy of this type ignoring any receiver and using dummy
|
|
2055 // names for all parameters. This is used for thunks for method
|
|
2056 // values.
|
|
2057 Function_type*
|
|
2058 copy_with_names() const;
|
|
2059
|
|
2060 static Type*
|
|
2061 make_function_type_descriptor_type();
|
|
2062
|
|
2063 // Return the backend representation of this function type. This is used
|
|
2064 // as the real type of a backend function declaration or defintion.
|
|
2065 Btype*
|
|
2066 get_backend_fntype(Gogo*);
|
|
2067
|
|
2068 protected:
|
|
2069 int
|
|
2070 do_traverse(Traverse*);
|
|
2071
|
|
2072 // A function descriptor may be allocated on the heap.
|
|
2073 bool
|
|
2074 do_has_pointer() const
|
|
2075 { return true; }
|
|
2076
|
|
2077 bool
|
|
2078 do_compare_is_identity(Gogo*)
|
|
2079 { return false; }
|
|
2080
|
|
2081 unsigned int
|
|
2082 do_hash_for_method(Gogo*) const;
|
|
2083
|
|
2084 Btype*
|
|
2085 do_get_backend(Gogo*);
|
|
2086
|
|
2087 Expression*
|
|
2088 do_type_descriptor(Gogo*, Named_type*);
|
|
2089
|
|
2090 void
|
|
2091 do_reflection(Gogo*, std::string*) const;
|
|
2092
|
|
2093 void
|
|
2094 do_mangled_name(Gogo*, std::string*) const;
|
|
2095
|
|
2096 void
|
|
2097 do_export(Export*) const;
|
|
2098
|
|
2099 private:
|
|
2100 Expression*
|
|
2101 type_descriptor_params(Type*, const Typed_identifier*,
|
|
2102 const Typed_identifier_list*);
|
|
2103
|
|
2104 // A mapping from a list of result types to a backend struct type.
|
|
2105 class Results_hash
|
|
2106 {
|
|
2107 public:
|
|
2108 unsigned int
|
|
2109 operator()(const Typed_identifier_list*) const;
|
|
2110 };
|
|
2111
|
|
2112 class Results_equal
|
|
2113 {
|
|
2114 public:
|
|
2115 bool
|
|
2116 operator()(const Typed_identifier_list*,
|
|
2117 const Typed_identifier_list*) const;
|
|
2118 };
|
|
2119
|
|
2120 typedef Unordered_map_hash(Typed_identifier_list*, Btype*,
|
|
2121 Results_hash, Results_equal) Results_structs;
|
|
2122
|
|
2123 static Results_structs results_structs;
|
|
2124
|
|
2125 // The receiver name and type. This will be NULL for a normal
|
|
2126 // function, non-NULL for a method.
|
|
2127 Typed_identifier* receiver_;
|
|
2128 // The parameter names and types.
|
|
2129 Typed_identifier_list* parameters_;
|
|
2130 // The result names and types. This will be NULL if no result was
|
|
2131 // specified.
|
|
2132 Typed_identifier_list* results_;
|
|
2133 // The location where this type was defined. This exists solely to
|
|
2134 // give a location for the fields of the struct if this function
|
|
2135 // returns multiple values.
|
|
2136 Location location_;
|
|
2137 // Whether this function takes a variable number of arguments.
|
|
2138 bool is_varargs_;
|
|
2139 // Whether this is a special builtin function which can not simply
|
|
2140 // be called. This is used for len, cap, etc.
|
|
2141 bool is_builtin_;
|
|
2142 // The backend representation of this type for backend function
|
|
2143 // declarations and definitions.
|
|
2144 Btype* fnbtype_;
|
|
2145 // Whether this function has been analyzed by escape analysis. If this is
|
|
2146 // TRUE, this function type's parameters contain a summary of the analysis.
|
|
2147 bool is_tagged_;
|
|
2148 };
|
|
2149
|
|
2150 // The type of a function's backend representation.
|
|
2151
|
|
2152 class Backend_function_type : public Function_type
|
|
2153 {
|
|
2154 public:
|
|
2155 Backend_function_type(Typed_identifier* receiver,
|
|
2156 Typed_identifier_list* parameters,
|
|
2157 Typed_identifier_list* results, Location location)
|
|
2158 : Function_type(receiver, parameters, results, location)
|
|
2159 { }
|
|
2160
|
|
2161 protected:
|
|
2162 Btype*
|
|
2163 do_get_backend(Gogo* gogo)
|
|
2164 { return this->get_backend_fntype(gogo); }
|
|
2165 };
|
|
2166
|
|
2167 // The type of a pointer.
|
|
2168
|
|
2169 class Pointer_type : public Type
|
|
2170 {
|
|
2171 public:
|
|
2172 Pointer_type(Type* to_type)
|
|
2173 : Type(TYPE_POINTER),
|
|
2174 to_type_(to_type)
|
|
2175 {}
|
|
2176
|
|
2177 Type*
|
|
2178 points_to() const
|
|
2179 { return this->to_type_; }
|
|
2180
|
|
2181 // Import a pointer type.
|
|
2182 static Pointer_type*
|
|
2183 do_import(Import*);
|
|
2184
|
|
2185 static Type*
|
|
2186 make_pointer_type_descriptor_type();
|
|
2187
|
|
2188 protected:
|
|
2189 int
|
|
2190 do_traverse(Traverse*);
|
|
2191
|
|
2192 bool
|
|
2193 do_verify()
|
|
2194 { return this->to_type_->verify(); }
|
|
2195
|
|
2196 bool
|
|
2197 do_has_pointer() const
|
|
2198 { return true; }
|
|
2199
|
|
2200 bool
|
|
2201 do_compare_is_identity(Gogo*)
|
|
2202 { return true; }
|
|
2203
|
|
2204 unsigned int
|
|
2205 do_hash_for_method(Gogo*) const;
|
|
2206
|
|
2207 Btype*
|
|
2208 do_get_backend(Gogo*);
|
|
2209
|
|
2210 Expression*
|
|
2211 do_type_descriptor(Gogo*, Named_type*);
|
|
2212
|
|
2213 void
|
|
2214 do_reflection(Gogo*, std::string*) const;
|
|
2215
|
|
2216 void
|
|
2217 do_mangled_name(Gogo*, std::string*) const;
|
|
2218
|
|
2219 void
|
|
2220 do_export(Export*) const;
|
|
2221
|
|
2222 private:
|
|
2223 // The type to which this type points.
|
|
2224 Type* to_type_;
|
|
2225 };
|
|
2226
|
|
2227 // The nil type. We use a special type for nil because it is not the
|
|
2228 // same as any other type. In C term nil has type void*, but there is
|
|
2229 // no such type in Go.
|
|
2230
|
|
2231 class Nil_type : public Type
|
|
2232 {
|
|
2233 public:
|
|
2234 Nil_type()
|
|
2235 : Type(TYPE_NIL)
|
|
2236 { }
|
|
2237
|
|
2238 protected:
|
|
2239 bool
|
|
2240 do_compare_is_identity(Gogo*)
|
|
2241 { return false; }
|
|
2242
|
|
2243 Btype*
|
|
2244 do_get_backend(Gogo* gogo);
|
|
2245
|
|
2246 Expression*
|
|
2247 do_type_descriptor(Gogo*, Named_type*)
|
|
2248 { go_unreachable(); }
|
|
2249
|
|
2250 void
|
|
2251 do_reflection(Gogo*, std::string*) const
|
|
2252 { go_unreachable(); }
|
|
2253
|
|
2254 void
|
|
2255 do_mangled_name(Gogo*, std::string* ret) const;
|
|
2256 };
|
|
2257
|
|
2258 // The type of a field in a struct.
|
|
2259
|
|
2260 class Struct_field
|
|
2261 {
|
|
2262 public:
|
|
2263 explicit Struct_field(const Typed_identifier& typed_identifier)
|
|
2264 : typed_identifier_(typed_identifier), tag_(NULL), is_imported_(false)
|
|
2265 { }
|
|
2266
|
|
2267 // The field name.
|
|
2268 const std::string&
|
|
2269 field_name() const;
|
|
2270
|
|
2271 // Return whether this struct field is named NAME.
|
|
2272 bool
|
|
2273 is_field_name(const std::string& name) const;
|
|
2274
|
|
2275 // Return whether this struct field is an unexported field named NAME.
|
|
2276 bool
|
|
2277 is_unexported_field_name(Gogo*, const std::string& name) const;
|
|
2278
|
|
2279 // Return whether this struct field is an embedded built-in type.
|
|
2280 bool
|
|
2281 is_embedded_builtin(Gogo*) const;
|
|
2282
|
|
2283 // The field type.
|
|
2284 Type*
|
|
2285 type() const
|
|
2286 { return this->typed_identifier_.type(); }
|
|
2287
|
|
2288 // The field location.
|
|
2289 Location
|
|
2290 location() const
|
|
2291 { return this->typed_identifier_.location(); }
|
|
2292
|
|
2293 // Whether the field has a tag.
|
|
2294 bool
|
|
2295 has_tag() const
|
|
2296 { return this->tag_ != NULL; }
|
|
2297
|
|
2298 // The tag.
|
|
2299 const std::string&
|
|
2300 tag() const
|
|
2301 {
|
|
2302 go_assert(this->tag_ != NULL);
|
|
2303 return *this->tag_;
|
|
2304 }
|
|
2305
|
|
2306 // Whether this is an anonymous field.
|
|
2307 bool
|
|
2308 is_anonymous() const
|
|
2309 { return this->typed_identifier_.name().empty(); }
|
|
2310
|
|
2311 // Set the tag. FIXME: This is never freed.
|
|
2312 void
|
|
2313 set_tag(const std::string& tag)
|
|
2314 { this->tag_ = new std::string(tag); }
|
|
2315
|
|
2316 // Record that this field is defined in an imported struct.
|
|
2317 void
|
|
2318 set_is_imported()
|
|
2319 { this->is_imported_ = true; }
|
|
2320
|
|
2321 // Set the type. This is only used in error cases.
|
|
2322 void
|
|
2323 set_type(Type* type)
|
|
2324 { this->typed_identifier_.set_type(type); }
|
|
2325
|
|
2326 private:
|
|
2327 // The field name, type, and location.
|
|
2328 Typed_identifier typed_identifier_;
|
|
2329 // The field tag. This is NULL if the field has no tag.
|
|
2330 std::string* tag_;
|
|
2331 // Whether this field is defined in an imported struct.
|
|
2332 bool is_imported_;
|
|
2333 };
|
|
2334
|
|
2335 // A list of struct fields.
|
|
2336
|
|
2337 class Struct_field_list
|
|
2338 {
|
|
2339 public:
|
|
2340 Struct_field_list()
|
|
2341 : entries_()
|
|
2342 { }
|
|
2343
|
|
2344 // Whether the list is empty.
|
|
2345 bool
|
|
2346 empty() const
|
|
2347 { return this->entries_.empty(); }
|
|
2348
|
|
2349 // Return the number of entries.
|
|
2350 size_t
|
|
2351 size() const
|
|
2352 { return this->entries_.size(); }
|
|
2353
|
|
2354 // Add an entry to the end of the list.
|
|
2355 void
|
|
2356 push_back(const Struct_field& sf)
|
|
2357 { this->entries_.push_back(sf); }
|
|
2358
|
|
2359 // Index into the list.
|
|
2360 const Struct_field&
|
|
2361 at(size_t i) const
|
|
2362 { return this->entries_.at(i); }
|
|
2363
|
|
2364 // Last entry in list.
|
|
2365 Struct_field&
|
|
2366 back()
|
|
2367 { return this->entries_.back(); }
|
|
2368
|
|
2369 // Iterators.
|
|
2370
|
|
2371 typedef std::vector<Struct_field>::iterator iterator;
|
|
2372 typedef std::vector<Struct_field>::const_iterator const_iterator;
|
|
2373
|
|
2374 iterator
|
|
2375 begin()
|
|
2376 { return this->entries_.begin(); }
|
|
2377
|
|
2378 const_iterator
|
|
2379 begin() const
|
|
2380 { return this->entries_.begin(); }
|
|
2381
|
|
2382 iterator
|
|
2383 end()
|
|
2384 { return this->entries_.end(); }
|
|
2385
|
|
2386 const_iterator
|
|
2387 end() const
|
|
2388 { return this->entries_.end(); }
|
|
2389
|
|
2390 private:
|
|
2391 std::vector<Struct_field> entries_;
|
|
2392 };
|
|
2393
|
|
2394 // The type of a struct.
|
|
2395
|
|
2396 class Struct_type : public Type
|
|
2397 {
|
|
2398 public:
|
|
2399 Struct_type(Struct_field_list* fields, Location location)
|
|
2400 : Type(TYPE_STRUCT),
|
|
2401 fields_(fields), location_(location), all_methods_(NULL),
|
|
2402 is_struct_incomparable_(false)
|
|
2403 { }
|
|
2404
|
|
2405 // Return the field NAME. This only looks at local fields, not at
|
|
2406 // embedded types. If the field is found, and PINDEX is not NULL,
|
|
2407 // this sets *PINDEX to the field index. If the field is not found,
|
|
2408 // this returns NULL.
|
|
2409 const Struct_field*
|
|
2410 find_local_field(const std::string& name, unsigned int *pindex) const;
|
|
2411
|
|
2412 // Return the field number INDEX.
|
|
2413 const Struct_field*
|
|
2414 field(unsigned int index) const
|
|
2415 { return &this->fields_->at(index); }
|
|
2416
|
|
2417 // Get the struct fields.
|
|
2418 const Struct_field_list*
|
|
2419 fields() const
|
|
2420 { return this->fields_; }
|
|
2421
|
|
2422 // Return the number of fields.
|
|
2423 size_t
|
|
2424 field_count() const
|
|
2425 { return this->fields_->size(); }
|
|
2426
|
|
2427 // Push a new field onto the end of the struct. This is used when
|
|
2428 // building a closure variable.
|
|
2429 void
|
|
2430 push_field(const Struct_field& sf)
|
|
2431 { this->fields_->push_back(sf); }
|
|
2432
|
|
2433 // Return an expression referring to field NAME in STRUCT_EXPR, or
|
|
2434 // NULL if there is no field with that name.
|
|
2435 Field_reference_expression*
|
|
2436 field_reference(Expression* struct_expr, const std::string& name,
|
|
2437 Location) const;
|
|
2438
|
|
2439 // Return the total number of fields, including embedded fields.
|
|
2440 // This is the number of values that can appear in a conversion to
|
|
2441 // this type.
|
|
2442 unsigned int
|
|
2443 total_field_count() const;
|
|
2444
|
|
2445 // Whether this type is identical with T.
|
|
2446 bool
|
|
2447 is_identical(const Struct_type* t, Cmp_tags,
|
|
2448 bool errors_are_identical) const;
|
|
2449
|
|
2450 // Return whether NAME is a local field which is not exported. This
|
|
2451 // is only used for better error reporting.
|
|
2452 bool
|
|
2453 is_unexported_local_field(Gogo*, const std::string& name) const;
|
|
2454
|
|
2455 // If this is an unnamed struct, build the complete list of methods,
|
|
2456 // including those from anonymous fields, and build methods stubs if
|
|
2457 // needed.
|
|
2458 void
|
|
2459 finalize_methods(Gogo*);
|
|
2460
|
|
2461 // Return whether this type has any methods. This should only be
|
|
2462 // called after the finalize_methods pass.
|
|
2463 bool
|
|
2464 has_any_methods() const
|
|
2465 { return this->all_methods_ != NULL; }
|
|
2466
|
|
2467 // Return the methods for tihs type. This should only be called
|
|
2468 // after the finalize_methods pass.
|
|
2469 const Methods*
|
|
2470 methods() const
|
|
2471 { return this->all_methods_; }
|
|
2472
|
|
2473 // Return the method to use for NAME. This returns NULL if there is
|
|
2474 // no such method or if the method is ambiguous. When it returns
|
|
2475 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
|
|
2476 Method*
|
|
2477 method_function(const std::string& name, bool* is_ambiguous) const;
|
|
2478
|
|
2479 // Return a pointer to the interface method table for this type for
|
|
2480 // the interface INTERFACE. If IS_POINTER is true, set the type
|
|
2481 // descriptor to a pointer to this type, otherwise set it to this
|
|
2482 // type.
|
|
2483 Expression*
|
|
2484 interface_method_table(Interface_type* interface, bool is_pointer);
|
|
2485
|
|
2486 // Traverse just the field types of a struct type.
|
|
2487 int
|
|
2488 traverse_field_types(Traverse* traverse)
|
|
2489 { return this->do_traverse(traverse); }
|
|
2490
|
|
2491 // If the offset of field INDEX in the backend implementation can be
|
|
2492 // determined, set *POFFSET to the offset in bytes and return true.
|
|
2493 // Otherwise, return false.
|
|
2494 bool
|
|
2495 backend_field_offset(Gogo*, unsigned int index, int64_t* poffset);
|
|
2496
|
|
2497 // Finish the backend representation of all the fields.
|
|
2498 void
|
|
2499 finish_backend_fields(Gogo*);
|
|
2500
|
|
2501 // Import a struct type.
|
|
2502 static Struct_type*
|
|
2503 do_import(Import*);
|
|
2504
|
|
2505 static Type*
|
|
2506 make_struct_type_descriptor_type();
|
|
2507
|
|
2508 // Return whether this is a generated struct that is not comparable.
|
|
2509 bool
|
|
2510 is_struct_incomparable() const
|
|
2511 { return this->is_struct_incomparable_; }
|
|
2512
|
|
2513 // Record that this is a generated struct that is not comparable.
|
|
2514 void
|
|
2515 set_is_struct_incomparable()
|
|
2516 { this->is_struct_incomparable_ = true; }
|
|
2517
|
|
2518 // Write the hash function for this type.
|
|
2519 void
|
|
2520 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
|
|
2521
|
|
2522 // Write the equality function for this type.
|
|
2523 void
|
|
2524 write_equal_function(Gogo*, Named_type*);
|
|
2525
|
|
2526 // Whether we can write this type to a C header file, to implement
|
|
2527 // -fgo-c-header.
|
|
2528 bool
|
|
2529 can_write_to_c_header(std::vector<const Named_object*>*,
|
|
2530 std::vector<const Named_object*>*) const;
|
|
2531
|
|
2532 // Write this type to a C header file, to implement -fgo-c-header.
|
|
2533 void
|
|
2534 write_to_c_header(std::ostream&) const;
|
|
2535
|
|
2536 protected:
|
|
2537 int
|
|
2538 do_traverse(Traverse*);
|
|
2539
|
|
2540 bool
|
|
2541 do_verify();
|
|
2542
|
|
2543 bool
|
|
2544 do_has_pointer() const;
|
|
2545
|
|
2546 bool
|
|
2547 do_compare_is_identity(Gogo*);
|
|
2548
|
|
2549 bool
|
|
2550 do_is_reflexive();
|
|
2551
|
|
2552 bool
|
|
2553 do_needs_key_update();
|
|
2554
|
|
2555 bool
|
|
2556 do_in_heap();
|
|
2557
|
|
2558 unsigned int
|
|
2559 do_hash_for_method(Gogo*) const;
|
|
2560
|
|
2561 Btype*
|
|
2562 do_get_backend(Gogo*);
|
|
2563
|
|
2564 Expression*
|
|
2565 do_type_descriptor(Gogo*, Named_type*);
|
|
2566
|
|
2567 void
|
|
2568 do_reflection(Gogo*, std::string*) const;
|
|
2569
|
|
2570 void
|
|
2571 do_mangled_name(Gogo*, std::string*) const;
|
|
2572
|
|
2573 void
|
|
2574 do_export(Export*) const;
|
|
2575
|
|
2576 private:
|
|
2577 bool
|
|
2578 can_write_type_to_c_header(const Type*,
|
|
2579 std::vector<const Named_object*>*,
|
|
2580 std::vector<const Named_object*>*) const;
|
|
2581
|
|
2582 void
|
|
2583 write_field_to_c_header(std::ostream&, const std::string&, const Type*) const;
|
|
2584
|
|
2585 // Used to merge method sets of identical unnamed structs.
|
|
2586 typedef Unordered_map_hash(Struct_type*, Struct_type*, Type_hash_identical,
|
|
2587 Type_identical) Identical_structs;
|
|
2588
|
|
2589 static Identical_structs identical_structs;
|
|
2590
|
|
2591 // Used to manage method tables for identical unnamed structs.
|
|
2592 typedef std::pair<Interface_method_tables*, Interface_method_tables*>
|
|
2593 Struct_method_table_pair;
|
|
2594
|
|
2595 typedef Unordered_map_hash(Struct_type*, Struct_method_table_pair*,
|
|
2596 Type_hash_identical, Type_identical)
|
|
2597 Struct_method_tables;
|
|
2598
|
|
2599 static Struct_method_tables struct_method_tables;
|
|
2600
|
|
2601 // Used to avoid infinite loops in field_reference_depth.
|
|
2602 struct Saw_named_type
|
|
2603 {
|
|
2604 Saw_named_type* next;
|
|
2605 Named_type* nt;
|
|
2606 };
|
|
2607
|
|
2608 Field_reference_expression*
|
|
2609 field_reference_depth(Expression* struct_expr, const std::string& name,
|
|
2610 Location, Saw_named_type*,
|
|
2611 unsigned int* depth) const;
|
|
2612
|
|
2613 // The fields of the struct.
|
|
2614 Struct_field_list* fields_;
|
|
2615 // The place where the struct was declared.
|
|
2616 Location location_;
|
|
2617 // If this struct is unnamed, a list of methods.
|
|
2618 Methods* all_methods_;
|
|
2619 // True if this is a generated struct that is not considered to be
|
|
2620 // comparable.
|
|
2621 bool is_struct_incomparable_;
|
|
2622 };
|
|
2623
|
|
2624 // The type of an array.
|
|
2625
|
|
2626 class Array_type : public Type
|
|
2627 {
|
|
2628 public:
|
|
2629 Array_type(Type* element_type, Expression* length)
|
|
2630 : Type(TYPE_ARRAY),
|
|
2631 element_type_(element_type), length_(length), blength_(NULL),
|
|
2632 issued_length_error_(false), is_array_incomparable_(false)
|
|
2633 { }
|
|
2634
|
|
2635 // Return the element type.
|
|
2636 Type*
|
|
2637 element_type() const
|
|
2638 { return this->element_type_; }
|
|
2639
|
|
2640 // Return the length. This will return NULL for a slice.
|
|
2641 Expression*
|
|
2642 length() const
|
|
2643 { return this->length_; }
|
|
2644
|
|
2645 // Store the length as an int64_t into *PLEN. Return false if the
|
|
2646 // length can not be determined. This will assert if called for a
|
|
2647 // slice.
|
|
2648 bool
|
|
2649 int_length(int64_t* plen);
|
|
2650
|
|
2651 // Whether this type is identical with T.
|
|
2652 bool
|
|
2653 is_identical(const Array_type* t, Cmp_tags,
|
|
2654 bool errors_are_identical) const;
|
|
2655
|
|
2656 // Return an expression for the pointer to the values in an array.
|
|
2657 Expression*
|
|
2658 get_value_pointer(Gogo*, Expression* array, bool is_lvalue) const;
|
|
2659
|
|
2660 // Return an expression for the length of an array with this type.
|
|
2661 Expression*
|
|
2662 get_length(Gogo*, Expression* array) const;
|
|
2663
|
|
2664 // Return an expression for the capacity of an array with this type.
|
|
2665 Expression*
|
|
2666 get_capacity(Gogo*, Expression* array) const;
|
|
2667
|
|
2668 // Import an array type.
|
|
2669 static Array_type*
|
|
2670 do_import(Import*);
|
|
2671
|
|
2672 // Return the backend representation of the element type.
|
|
2673 Btype*
|
|
2674 get_backend_element(Gogo*, bool use_placeholder);
|
|
2675
|
|
2676 // Return the backend representation of the length.
|
|
2677 Bexpression*
|
|
2678 get_backend_length(Gogo*);
|
|
2679
|
|
2680 // Finish the backend representation of the element type.
|
|
2681 void
|
|
2682 finish_backend_element(Gogo*);
|
|
2683
|
|
2684 static Type*
|
|
2685 make_array_type_descriptor_type();
|
|
2686
|
|
2687 static Type*
|
|
2688 make_slice_type_descriptor_type();
|
|
2689
|
|
2690 // Return whether this is a generated array that is not comparable.
|
|
2691 bool
|
|
2692 is_array_incomparable() const
|
|
2693 { return this->is_array_incomparable_; }
|
|
2694
|
|
2695 // Record that this is a generated array that is not comparable.
|
|
2696 void
|
|
2697 set_is_array_incomparable()
|
|
2698 { this->is_array_incomparable_ = true; }
|
|
2699
|
|
2700 // Write the hash function for this type.
|
|
2701 void
|
|
2702 write_hash_function(Gogo*, Named_type*, Function_type*, Function_type*);
|
|
2703
|
|
2704 // Write the equality function for this type.
|
|
2705 void
|
|
2706 write_equal_function(Gogo*, Named_type*);
|
|
2707
|
|
2708 protected:
|
|
2709 int
|
|
2710 do_traverse(Traverse* traverse);
|
|
2711
|
|
2712 bool
|
|
2713 do_verify();
|
|
2714
|
|
2715 bool
|
|
2716 do_has_pointer() const;
|
|
2717
|
|
2718 bool
|
|
2719 do_compare_is_identity(Gogo*);
|
|
2720
|
|
2721 bool
|
|
2722 do_is_reflexive()
|
|
2723 {
|
|
2724 return this->length_ != NULL && this->element_type_->is_reflexive();
|
|
2725 }
|
|
2726
|
|
2727 bool
|
|
2728 do_needs_key_update()
|
|
2729 { return this->element_type_->needs_key_update(); }
|
|
2730
|
|
2731 bool
|
|
2732 do_in_heap()
|
|
2733 { return this->length_ == NULL || this->element_type_->in_heap(); }
|
|
2734
|
|
2735 unsigned int
|
|
2736 do_hash_for_method(Gogo*) const;
|
|
2737
|
|
2738 Btype*
|
|
2739 do_get_backend(Gogo*);
|
|
2740
|
|
2741 Expression*
|
|
2742 do_type_descriptor(Gogo*, Named_type*);
|
|
2743
|
|
2744 void
|
|
2745 do_reflection(Gogo*, std::string*) const;
|
|
2746
|
|
2747 void
|
|
2748 do_mangled_name(Gogo*, std::string*) const;
|
|
2749
|
|
2750 void
|
|
2751 do_export(Export*) const;
|
|
2752
|
|
2753 private:
|
|
2754 bool
|
|
2755 verify_length();
|
|
2756
|
|
2757 Expression*
|
|
2758 array_type_descriptor(Gogo*, Named_type*);
|
|
2759
|
|
2760 Expression*
|
|
2761 slice_type_descriptor(Gogo*, Named_type*);
|
|
2762
|
|
2763 // The type of elements of the array.
|
|
2764 Type* element_type_;
|
|
2765 // The number of elements. This may be NULL.
|
|
2766 Expression* length_;
|
|
2767 // The backend representation of the length.
|
|
2768 // We only want to compute this once.
|
|
2769 Bexpression* blength_;
|
|
2770 // Whether or not an invalid length error has been issued for this type,
|
|
2771 // to avoid knock-on errors.
|
|
2772 mutable bool issued_length_error_;
|
|
2773 // True if this is a generated array that is not considered to be
|
|
2774 // comparable.
|
|
2775 bool is_array_incomparable_;
|
|
2776 };
|
|
2777
|
|
2778 // The type of a map.
|
|
2779
|
|
2780 class Map_type : public Type
|
|
2781 {
|
|
2782 public:
|
|
2783 Map_type(Type* key_type, Type* val_type, Location location)
|
|
2784 : Type(TYPE_MAP),
|
|
2785 key_type_(key_type), val_type_(val_type), hmap_type_(NULL),
|
|
2786 bucket_type_(NULL), hiter_type_(NULL), location_(location)
|
|
2787 { }
|
|
2788
|
|
2789 // Return the key type.
|
|
2790 Type*
|
|
2791 key_type() const
|
|
2792 { return this->key_type_; }
|
|
2793
|
|
2794 // Return the value type.
|
|
2795 Type*
|
|
2796 val_type() const
|
|
2797 { return this->val_type_; }
|
|
2798
|
|
2799 // Return the type used for an iteration over this map.
|
|
2800 Type*
|
|
2801 hiter_type(Gogo*);
|
|
2802
|
|
2803 // If this map requires the "fat" functions, returns the pointer to
|
|
2804 // pass as the zero value to those functions. Otherwise, in the
|
|
2805 // normal case, returns NULL.
|
|
2806 Expression*
|
|
2807 fat_zero_value(Gogo*);
|
|
2808
|
|
2809 // Return whether VAR is the map zero value.
|
|
2810 static bool
|
|
2811 is_zero_value(Variable* var);
|
|
2812
|
|
2813 // Return the backend representation of the map zero value.
|
|
2814 static Bvariable*
|
|
2815 backend_zero_value(Gogo*);
|
|
2816
|
|
2817 // Whether this type is identical with T.
|
|
2818 bool
|
|
2819 is_identical(const Map_type* t, Cmp_tags,
|
|
2820 bool errors_are_identical) const;
|
|
2821
|
|
2822 // Import a map type.
|
|
2823 static Map_type*
|
|
2824 do_import(Import*);
|
|
2825
|
|
2826 static Type*
|
|
2827 make_map_type_descriptor_type();
|
|
2828
|
|
2829 protected:
|
|
2830 int
|
|
2831 do_traverse(Traverse*);
|
|
2832
|
|
2833 bool
|
|
2834 do_verify();
|
|
2835
|
|
2836 bool
|
|
2837 do_has_pointer() const
|
|
2838 { return true; }
|
|
2839
|
|
2840 bool
|
|
2841 do_compare_is_identity(Gogo*)
|
|
2842 { return false; }
|
|
2843
|
|
2844 bool
|
|
2845 do_is_reflexive()
|
|
2846 {
|
|
2847 return this->key_type_->is_reflexive() && this->val_type_->is_reflexive();
|
|
2848 }
|
|
2849
|
|
2850 unsigned int
|
|
2851 do_hash_for_method(Gogo*) const;
|
|
2852
|
|
2853 Btype*
|
|
2854 do_get_backend(Gogo*);
|
|
2855
|
|
2856 Expression*
|
|
2857 do_type_descriptor(Gogo*, Named_type*);
|
|
2858
|
|
2859 void
|
|
2860 do_reflection(Gogo*, std::string*) const;
|
|
2861
|
|
2862 void
|
|
2863 do_mangled_name(Gogo*, std::string*) const;
|
|
2864
|
|
2865 void
|
|
2866 do_export(Export*) const;
|
|
2867
|
|
2868 private:
|
|
2869 // These must be in sync with libgo/go/runtime/hashmap.go.
|
|
2870 static const int bucket_size = 8;
|
|
2871 static const int max_key_size = 128;
|
|
2872 static const int max_val_size = 128;
|
|
2873 static const int max_zero_size = 1024;
|
|
2874
|
|
2875 // Maps with value types larger than max_zero_size require passing a
|
|
2876 // zero value pointer to the map functions.
|
|
2877
|
|
2878 // The zero value variable.
|
|
2879 static Named_object* zero_value;
|
|
2880
|
|
2881 // The current size of the zero value.
|
|
2882 static int64_t zero_value_size;
|
|
2883
|
|
2884 // The current alignment of the zero value.
|
|
2885 static int64_t zero_value_align;
|
|
2886
|
|
2887 Type*
|
|
2888 bucket_type(Gogo*, int64_t, int64_t);
|
|
2889
|
|
2890 Type*
|
|
2891 hmap_type(Type*);
|
|
2892
|
|
2893 // The key type.
|
|
2894 Type* key_type_;
|
|
2895 // The value type.
|
|
2896 Type* val_type_;
|
|
2897 // The hashmap type. At run time a map is represented as a pointer
|
|
2898 // to this type.
|
|
2899 Type* hmap_type_;
|
|
2900 // The bucket type, the type used to hold keys and values at run time.
|
|
2901 Type* bucket_type_;
|
|
2902 // The iterator type.
|
|
2903 Type* hiter_type_;
|
|
2904 // Where the type was defined.
|
|
2905 Location location_;
|
|
2906 };
|
|
2907
|
|
2908 // The type of a channel.
|
|
2909
|
|
2910 class Channel_type : public Type
|
|
2911 {
|
|
2912 public:
|
|
2913 Channel_type(bool may_send, bool may_receive, Type* element_type)
|
|
2914 : Type(TYPE_CHANNEL),
|
|
2915 may_send_(may_send), may_receive_(may_receive),
|
|
2916 element_type_(element_type)
|
|
2917 { go_assert(may_send || may_receive); }
|
|
2918
|
|
2919 // Whether this channel can send data.
|
|
2920 bool
|
|
2921 may_send() const
|
|
2922 { return this->may_send_; }
|
|
2923
|
|
2924 // Whether this channel can receive data.
|
|
2925 bool
|
|
2926 may_receive() const
|
|
2927 { return this->may_receive_; }
|
|
2928
|
|
2929 // The type of the values that may be sent on this channel. This is
|
|
2930 // NULL if any type may be sent.
|
|
2931 Type*
|
|
2932 element_type() const
|
|
2933 { return this->element_type_; }
|
|
2934
|
|
2935 // Whether this type is identical with T.
|
|
2936 bool
|
|
2937 is_identical(const Channel_type* t, Cmp_tags,
|
|
2938 bool errors_are_identical) const;
|
|
2939
|
|
2940 // Import a channel type.
|
|
2941 static Channel_type*
|
|
2942 do_import(Import*);
|
|
2943
|
|
2944 static Type*
|
|
2945 make_chan_type_descriptor_type();
|
|
2946
|
|
2947 static Type*
|
|
2948 select_type(int ncases);
|
|
2949
|
|
2950 protected:
|
|
2951 int
|
|
2952 do_traverse(Traverse* traverse)
|
|
2953 { return Type::traverse(this->element_type_, traverse); }
|
|
2954
|
|
2955 bool
|
|
2956 do_verify();
|
|
2957
|
|
2958 bool
|
|
2959 do_has_pointer() const
|
|
2960 { return true; }
|
|
2961
|
|
2962 bool
|
|
2963 do_compare_is_identity(Gogo*)
|
|
2964 { return true; }
|
|
2965
|
|
2966 unsigned int
|
|
2967 do_hash_for_method(Gogo*) const;
|
|
2968
|
|
2969 Btype*
|
|
2970 do_get_backend(Gogo*);
|
|
2971
|
|
2972 Expression*
|
|
2973 do_type_descriptor(Gogo*, Named_type*);
|
|
2974
|
|
2975 void
|
|
2976 do_reflection(Gogo*, std::string*) const;
|
|
2977
|
|
2978 void
|
|
2979 do_mangled_name(Gogo*, std::string*) const;
|
|
2980
|
|
2981 void
|
|
2982 do_export(Export*) const;
|
|
2983
|
|
2984 private:
|
|
2985 // Whether this channel can send data.
|
|
2986 bool may_send_;
|
|
2987 // Whether this channel can receive data.
|
|
2988 bool may_receive_;
|
|
2989 // The types of elements which may be sent on this channel. If this
|
|
2990 // is NULL, it means that any type may be sent.
|
|
2991 Type* element_type_;
|
|
2992 };
|
|
2993
|
|
2994 // An interface type.
|
|
2995
|
|
2996 class Interface_type : public Type
|
|
2997 {
|
|
2998 public:
|
|
2999 Interface_type(Typed_identifier_list* methods, Location location)
|
|
3000 : Type(TYPE_INTERFACE),
|
|
3001 parse_methods_(methods), all_methods_(NULL), location_(location),
|
|
3002 package_(NULL), interface_btype_(NULL), bmethods_(NULL),
|
|
3003 assume_identical_(NULL), methods_are_finalized_(false),
|
|
3004 bmethods_is_placeholder_(false), seen_(false)
|
|
3005 { go_assert(methods == NULL || !methods->empty()); }
|
|
3006
|
|
3007 // The location where the interface type was defined.
|
|
3008 Location
|
|
3009 location() const
|
|
3010 { return this->location_; }
|
|
3011
|
|
3012 // The package where the interface type was defined. Returns NULL
|
|
3013 // for the package currently being compiled.
|
|
3014 Package*
|
|
3015 package() const
|
|
3016 { return this->package_; }
|
|
3017
|
|
3018 // Return whether this is an empty interface.
|
|
3019 bool
|
|
3020 is_empty() const
|
|
3021 {
|
|
3022 go_assert(this->methods_are_finalized_);
|
|
3023 return this->all_methods_ == NULL;
|
|
3024 }
|
|
3025
|
|
3026 // Return the list of methods. This will return NULL for an empty
|
|
3027 // interface.
|
|
3028 const Typed_identifier_list*
|
|
3029 methods() const;
|
|
3030
|
|
3031 // Return the number of methods.
|
|
3032 size_t
|
|
3033 method_count() const;
|
|
3034
|
|
3035 // Return the method NAME, or NULL.
|
|
3036 const Typed_identifier*
|
|
3037 find_method(const std::string& name) const;
|
|
3038
|
|
3039 // Return the zero-based index of method NAME.
|
|
3040 size_t
|
|
3041 method_index(const std::string& name) const;
|
|
3042
|
|
3043 // Finalize the methods. This sets all_methods_. This handles
|
|
3044 // interface inheritance.
|
|
3045 void
|
|
3046 finalize_methods();
|
|
3047
|
|
3048 // Return true if T implements this interface. If this returns
|
|
3049 // false, and REASON is not NULL, it sets *REASON to the reason that
|
|
3050 // it fails.
|
|
3051 bool
|
|
3052 implements_interface(const Type* t, std::string* reason) const;
|
|
3053
|
|
3054 // Whether this type is identical with T. REASON is as in
|
|
3055 // implements_interface.
|
|
3056 bool
|
|
3057 is_identical(const Interface_type* t, Cmp_tags,
|
|
3058 bool errors_are_identical) const;
|
|
3059
|
|
3060 // Whether we can assign T to this type. is_identical is known to
|
|
3061 // be false.
|
|
3062 bool
|
|
3063 is_compatible_for_assign(const Interface_type*, std::string* reason) const;
|
|
3064
|
|
3065 // Return whether NAME is a method which is not exported. This is
|
|
3066 // only used for better error reporting.
|
|
3067 bool
|
|
3068 is_unexported_method(Gogo*, const std::string& name) const;
|
|
3069
|
|
3070 // Import an interface type.
|
|
3071 static Interface_type*
|
|
3072 do_import(Import*);
|
|
3073
|
|
3074 // Make a struct for an empty interface type.
|
|
3075 static Btype*
|
|
3076 get_backend_empty_interface_type(Gogo*);
|
|
3077
|
|
3078 // Get a pointer to the backend representation of the method table.
|
|
3079 Btype*
|
|
3080 get_backend_methods(Gogo*);
|
|
3081
|
|
3082 // Return a placeholder for the backend representation of the
|
|
3083 // pointer to the method table.
|
|
3084 Btype*
|
|
3085 get_backend_methods_placeholder(Gogo*);
|
|
3086
|
|
3087 // Finish the backend representation of the method types.
|
|
3088 void
|
|
3089 finish_backend_methods(Gogo*);
|
|
3090
|
|
3091 static Type*
|
|
3092 make_interface_type_descriptor_type();
|
|
3093
|
|
3094 protected:
|
|
3095 int
|
|
3096 do_traverse(Traverse*);
|
|
3097
|
|
3098 bool
|
|
3099 do_has_pointer() const
|
|
3100 { return true; }
|
|
3101
|
|
3102 bool
|
|
3103 do_compare_is_identity(Gogo*)
|
|
3104 { return false; }
|
|
3105
|
|
3106 // Not reflexive if it contains a float.
|
|
3107 bool
|
|
3108 do_is_reflexive()
|
|
3109 { return false; }
|
|
3110
|
|
3111 // Distinction between +0 and -0 requires a key update if it
|
|
3112 // contains a float.
|
|
3113 bool
|
|
3114 do_needs_key_update()
|
|
3115 { return true; }
|
|
3116
|
|
3117 unsigned int
|
|
3118 do_hash_for_method(Gogo*) const;
|
|
3119
|
|
3120 Btype*
|
|
3121 do_get_backend(Gogo*);
|
|
3122
|
|
3123 Expression*
|
|
3124 do_type_descriptor(Gogo*, Named_type*);
|
|
3125
|
|
3126 void
|
|
3127 do_reflection(Gogo*, std::string*) const;
|
|
3128
|
|
3129 void
|
|
3130 do_mangled_name(Gogo*, std::string*) const;
|
|
3131
|
|
3132 void
|
|
3133 do_export(Export*) const;
|
|
3134
|
|
3135 private:
|
|
3136 // This type guards against infinite recursion when comparing
|
|
3137 // interface types. We keep a list of interface types assumed to be
|
|
3138 // identical during comparison. We just keep the list on the stack.
|
|
3139 // This permits us to compare cases like
|
|
3140 // type I1 interface { F() interface{I1} }
|
|
3141 // type I2 interface { F() interface{I2} }
|
|
3142 struct Assume_identical
|
|
3143 {
|
|
3144 Assume_identical* next;
|
|
3145 const Interface_type* t1;
|
|
3146 const Interface_type* t2;
|
|
3147 };
|
|
3148
|
|
3149 bool
|
|
3150 assume_identical(const Interface_type*, const Interface_type*) const;
|
|
3151
|
|
3152 // The list of methods associated with the interface from the
|
|
3153 // parser. This will be NULL for the empty interface. This may
|
|
3154 // include unnamed interface types.
|
|
3155 Typed_identifier_list* parse_methods_;
|
|
3156 // The list of all methods associated with the interface. This
|
|
3157 // expands any interface types listed in methods_. It is set by
|
|
3158 // finalize_methods. This will be NULL for the empty interface.
|
|
3159 Typed_identifier_list* all_methods_;
|
|
3160 // The location where the interface was defined.
|
|
3161 Location location_;
|
|
3162 // The package where the interface was defined. This is NULL for
|
|
3163 // the package being compiled.
|
|
3164 Package* package_;
|
|
3165 // The backend representation of this type during backend conversion.
|
|
3166 Btype* interface_btype_;
|
|
3167 // The backend representation of the pointer to the method table.
|
|
3168 Btype* bmethods_;
|
|
3169 // A list of interface types assumed to be identical during
|
|
3170 // interface comparison.
|
|
3171 mutable Assume_identical* assume_identical_;
|
|
3172 // Whether the methods have been finalized.
|
|
3173 bool methods_are_finalized_;
|
|
3174 // Whether the bmethods_ field is a placeholder.
|
|
3175 bool bmethods_is_placeholder_;
|
|
3176 // Used to avoid endless recursion in do_mangled_name.
|
|
3177 mutable bool seen_;
|
|
3178 };
|
|
3179
|
|
3180 // The value we keep for a named type. This lets us get the right
|
|
3181 // name when we convert to backend. Note that we don't actually keep
|
|
3182 // the name here; the name is in the Named_object which points to
|
|
3183 // this. This object exists to hold a unique backend representation for
|
|
3184 // the type.
|
|
3185
|
|
3186 class Named_type : public Type
|
|
3187 {
|
|
3188 public:
|
|
3189 Named_type(Named_object* named_object, Type* type, Location location)
|
|
3190 : Type(TYPE_NAMED),
|
|
3191 named_object_(named_object), in_function_(NULL), in_function_index_(0),
|
|
3192 type_(type), local_methods_(NULL), all_methods_(NULL),
|
|
3193 interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
|
|
3194 location_(location), named_btype_(NULL), dependencies_(),
|
|
3195 is_alias_(false), is_visible_(true), is_error_(false), in_heap_(true),
|
|
3196 is_placeholder_(false), is_converted_(false), is_circular_(false),
|
|
3197 is_verified_(false), seen_(false), seen_in_compare_is_identity_(false),
|
|
3198 seen_in_get_backend_(false), seen_alias_(false)
|
|
3199 { }
|
|
3200
|
|
3201 // Return the associated Named_object. This holds the actual name.
|
|
3202 Named_object*
|
|
3203 named_object()
|
|
3204 { return this->named_object_; }
|
|
3205
|
|
3206 const Named_object*
|
|
3207 named_object() const
|
|
3208 { return this->named_object_; }
|
|
3209
|
|
3210 // Set the Named_object. This is used when we see a type
|
|
3211 // declaration followed by a type.
|
|
3212 void
|
|
3213 set_named_object(Named_object* no)
|
|
3214 { this->named_object_ = no; }
|
|
3215
|
|
3216 // Whether this is an alias (type T1 = T2) rather than an ordinary
|
|
3217 // named type (type T1 T2).
|
|
3218 bool
|
|
3219 is_alias() const
|
|
3220 { return this->is_alias_; }
|
|
3221
|
|
3222 // Record that this type is an alias.
|
|
3223 void
|
|
3224 set_is_alias()
|
|
3225 { this->is_alias_ = true; }
|
|
3226
|
|
3227 // Mark this type as not permitted in the heap.
|
|
3228 void
|
|
3229 set_not_in_heap()
|
|
3230 { this->in_heap_ = false; }
|
|
3231
|
|
3232 // Return the function in which this type is defined. This will
|
|
3233 // return NULL for a type defined in global scope.
|
|
3234 const Named_object*
|
|
3235 in_function(unsigned int *pindex) const
|
|
3236 {
|
|
3237 *pindex = this->in_function_index_;
|
|
3238 return this->in_function_;
|
|
3239 }
|
|
3240
|
|
3241 // Set the function in which this type is defined.
|
|
3242 void
|
|
3243 set_in_function(Named_object* f, unsigned int index)
|
|
3244 {
|
|
3245 this->in_function_ = f;
|
|
3246 this->in_function_index_ = index;
|
|
3247 }
|
|
3248
|
|
3249 // Return the name of the type.
|
|
3250 const std::string&
|
|
3251 name() const;
|
|
3252
|
|
3253 // Return the name of the type for an error message. The difference
|
|
3254 // is that if the type is defined in a different package, this will
|
|
3255 // return PACKAGE.NAME.
|
|
3256 std::string
|
|
3257 message_name() const;
|
|
3258
|
|
3259 // Return the underlying type.
|
|
3260 Type*
|
|
3261 real_type()
|
|
3262 { return this->type_; }
|
|
3263
|
|
3264 const Type*
|
|
3265 real_type() const
|
|
3266 { return this->type_; }
|
|
3267
|
|
3268 // Return the location.
|
|
3269 Location
|
|
3270 location() const
|
|
3271 { return this->location_; }
|
|
3272
|
|
3273 // Whether this type is visible. This only matters when parsing.
|
|
3274 bool
|
|
3275 is_visible() const
|
|
3276 { return this->is_visible_; }
|
|
3277
|
|
3278 // Mark this type as visible.
|
|
3279 void
|
|
3280 set_is_visible()
|
|
3281 { this->is_visible_ = true; }
|
|
3282
|
|
3283 // Mark this type as invisible.
|
|
3284 void
|
|
3285 clear_is_visible()
|
|
3286 { this->is_visible_ = false; }
|
|
3287
|
|
3288 // Whether this is a builtin type.
|
|
3289 bool
|
|
3290 is_builtin() const
|
|
3291 { return Linemap::is_predeclared_location(this->location_); }
|
|
3292
|
|
3293 // Whether this named type is valid. A recursive named type is invalid.
|
|
3294 bool
|
|
3295 is_valid() const
|
|
3296 { return !this->is_error_; }
|
|
3297
|
|
3298 // Whether this is a circular type: a pointer or function type that
|
|
3299 // refers to itself, which is not possible in C.
|
|
3300 bool
|
|
3301 is_circular() const
|
|
3302 { return this->is_circular_; }
|
|
3303
|
|
3304 // Return the base type for this type.
|
|
3305 Type*
|
|
3306 named_base();
|
|
3307
|
|
3308 const Type*
|
|
3309 named_base() const;
|
|
3310
|
|
3311 // Return whether this is an error type.
|
|
3312 bool
|
|
3313 is_named_error_type() const;
|
|
3314
|
|
3315 // Return whether this type is comparable. If REASON is not NULL,
|
|
3316 // set *REASON when returning false.
|
|
3317 bool
|
|
3318 named_type_is_comparable(std::string* reason) const;
|
|
3319
|
|
3320 // Add a method to this type.
|
|
3321 Named_object*
|
|
3322 add_method(const std::string& name, Function*);
|
|
3323
|
|
3324 // Add a method declaration to this type.
|
|
3325 Named_object*
|
|
3326 add_method_declaration(const std::string& name, Package* package,
|
|
3327 Function_type* type, Location location);
|
|
3328
|
|
3329 // Add an existing method--one defined before the type itself was
|
|
3330 // defined--to a type.
|
|
3331 void
|
|
3332 add_existing_method(Named_object*);
|
|
3333
|
|
3334 // Look up a local method.
|
|
3335 Named_object*
|
|
3336 find_local_method(const std::string& name) const;
|
|
3337
|
|
3338 // Return the list of local methods.
|
|
3339 const Bindings*
|
|
3340 local_methods() const;
|
|
3341
|
|
3342 // Build the complete list of methods, including those from
|
|
3343 // anonymous fields, and build method stubs if needed.
|
|
3344 void
|
|
3345 finalize_methods(Gogo*);
|
|
3346
|
|
3347 // Return whether this type has any methods. This should only be
|
|
3348 // called after the finalize_methods pass.
|
|
3349 bool
|
|
3350 has_any_methods() const;
|
|
3351
|
|
3352 // Return the methods for this type. This should only be called
|
|
3353 // after the finalized_methods pass.
|
|
3354 const Methods*
|
|
3355 methods() const;
|
|
3356
|
|
3357 // Return the method to use for NAME. This returns NULL if there is
|
|
3358 // no such method or if the method is ambiguous. When it returns
|
|
3359 // NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
|
|
3360 Method*
|
|
3361 method_function(const std::string& name, bool *is_ambiguous) const;
|
|
3362
|
|
3363 // Return whether NAME is a known field or method which is not
|
|
3364 // exported. This is only used for better error reporting.
|
|
3365 bool
|
|
3366 is_unexported_local_method(Gogo*, const std::string& name) const;
|
|
3367
|
|
3368 // Return a pointer to the interface method table for this type for
|
|
3369 // the interface INTERFACE. If IS_POINTER is true, set the type
|
|
3370 // descriptor to a pointer to this type, otherwise set it to this
|
|
3371 // type.
|
|
3372 Expression*
|
|
3373 interface_method_table(Interface_type* interface, bool is_pointer);
|
|
3374
|
|
3375 // Note that a type must be converted to the backend representation
|
|
3376 // before we convert this type.
|
|
3377 void
|
|
3378 add_dependency(Named_type* nt)
|
|
3379 { this->dependencies_.push_back(nt); }
|
|
3380
|
|
3381 // Return true if the size and alignment of the backend
|
|
3382 // representation of this type is known. This is always true after
|
|
3383 // types have been converted, but may be false beforehand.
|
|
3384 bool
|
|
3385 is_named_backend_type_size_known() const
|
|
3386 { return this->named_btype_ != NULL && !this->is_placeholder_; }
|
|
3387
|
|
3388 // Add to the reflection string as for Type::append_reflection, but
|
|
3389 // if USE_ALIAS use the alias name rather than the alias target.
|
|
3390 void
|
|
3391 append_reflection_type_name(Gogo*, bool use_alias, std::string*) const;
|
|
3392
|
|
3393 // Append the mangled type name as for Type::append_mangled_name,
|
|
3394 // but if USE_ALIAS use the alias name rather than the alias target.
|
|
3395 void
|
|
3396 append_mangled_type_name(Gogo*, bool use_alias, std::string*) const;
|
|
3397
|
|
3398 // Export the type.
|
|
3399 void
|
|
3400 export_named_type(Export*, const std::string& name) const;
|
|
3401
|
|
3402 // Import a named type.
|
|
3403 static void
|
|
3404 import_named_type(Import*, Named_type**);
|
|
3405
|
|
3406 // Initial conversion to backend representation.
|
|
3407 void
|
|
3408 convert(Gogo*);
|
|
3409
|
|
3410 protected:
|
|
3411 int
|
|
3412 do_traverse(Traverse* traverse)
|
|
3413 { return Type::traverse(this->type_, traverse); }
|
|
3414
|
|
3415 bool
|
|
3416 do_verify();
|
|
3417
|
|
3418 bool
|
|
3419 do_has_pointer() const;
|
|
3420
|
|
3421 bool
|
|
3422 do_compare_is_identity(Gogo*);
|
|
3423
|
|
3424 bool
|
|
3425 do_is_reflexive();
|
|
3426
|
|
3427 bool
|
|
3428 do_needs_key_update();
|
|
3429
|
|
3430 bool
|
|
3431 do_in_heap()
|
|
3432 { return this->in_heap_ && this->type_->in_heap(); }
|
|
3433
|
|
3434 unsigned int
|
|
3435 do_hash_for_method(Gogo*) const;
|
|
3436
|
|
3437 Btype*
|
|
3438 do_get_backend(Gogo*);
|
|
3439
|
|
3440 Expression*
|
|
3441 do_type_descriptor(Gogo*, Named_type*);
|
|
3442
|
|
3443 void
|
|
3444 do_reflection(Gogo*, std::string*) const;
|
|
3445
|
|
3446 void
|
|
3447 do_mangled_name(Gogo*, std::string* ret) const;
|
|
3448
|
|
3449 void
|
|
3450 do_export(Export*) const;
|
|
3451
|
|
3452 private:
|
|
3453 // Create the placeholder during conversion.
|
|
3454 void
|
|
3455 create_placeholder(Gogo*);
|
|
3456
|
|
3457 // A pointer back to the Named_object for this type.
|
|
3458 Named_object* named_object_;
|
|
3459 // If this type is defined in a function, a pointer back to the
|
|
3460 // function in which it is defined.
|
|
3461 Named_object* in_function_;
|
|
3462 // The index of this type in IN_FUNCTION_.
|
|
3463 unsigned int in_function_index_;
|
|
3464 // The actual type.
|
|
3465 Type* type_;
|
|
3466 // The list of methods defined for this type. Any named type can
|
|
3467 // have methods.
|
|
3468 Bindings* local_methods_;
|
|
3469 // The full list of methods for this type, including methods
|
|
3470 // declared for anonymous fields.
|
|
3471 Methods* all_methods_;
|
|
3472 // A mapping from interfaces to the associated interface method
|
|
3473 // tables for this type.
|
|
3474 Interface_method_tables* interface_method_tables_;
|
|
3475 // A mapping from interfaces to the associated interface method
|
|
3476 // tables for pointers to this type.
|
|
3477 Interface_method_tables* pointer_interface_method_tables_;
|
|
3478 // The location where this type was defined.
|
|
3479 Location location_;
|
|
3480 // The backend representation of this type during backend
|
|
3481 // conversion. This is used to avoid endless recursion when a named
|
|
3482 // type refers to itself.
|
|
3483 Btype* named_btype_;
|
|
3484 // A list of types which must be converted to the backend
|
|
3485 // representation before this type can be converted. This is for
|
|
3486 // cases like
|
|
3487 // type S1 { p *S2 }
|
|
3488 // type S2 { s S1 }
|
|
3489 // where we can't convert S2 to the backend representation unless we
|
|
3490 // have converted S1.
|
|
3491 std::vector<Named_type*> dependencies_;
|
|
3492 // Whether this is an alias type.
|
|
3493 bool is_alias_;
|
|
3494 // Whether this type is visible. This is false if this type was
|
|
3495 // created because it was referenced by an imported object, but the
|
|
3496 // type itself was not exported. This will always be true for types
|
|
3497 // created in the current package.
|
|
3498 bool is_visible_;
|
|
3499 // Whether this type is erroneous.
|
|
3500 bool is_error_;
|
|
3501 // Whether this type is permitted in the heap. This is true by
|
|
3502 // default, false if there is a magic //go:notinheap comment.
|
|
3503 bool in_heap_;
|
|
3504 // Whether the current value of named_btype_ is a placeholder for
|
|
3505 // which the final size of the type is not known.
|
|
3506 bool is_placeholder_;
|
|
3507 // Whether this type has been converted to the backend
|
|
3508 // representation. Implies that is_placeholder_ is false.
|
|
3509 bool is_converted_;
|
|
3510 // Whether this is a pointer or function type which refers to the
|
|
3511 // type itself.
|
|
3512 bool is_circular_;
|
|
3513 // Whether this type has been verified.
|
|
3514 bool is_verified_;
|
|
3515 // In a recursive operation such as has_pointer, this flag is used
|
|
3516 // to prevent infinite recursion when a type refers to itself. This
|
|
3517 // is mutable because it is always reset to false when the function
|
|
3518 // exits.
|
|
3519 mutable bool seen_;
|
|
3520 // Like seen_, but used only by do_compare_is_identity.
|
|
3521 bool seen_in_compare_is_identity_;
|
|
3522 // Like seen_, but used only by do_get_backend.
|
|
3523 bool seen_in_get_backend_;
|
|
3524 // Like seen_, but used when resolving aliases.
|
|
3525 mutable bool seen_alias_;
|
|
3526 };
|
|
3527
|
|
3528 // A forward declaration. This handles a type which has been declared
|
|
3529 // but not defined.
|
|
3530
|
|
3531 class Forward_declaration_type : public Type
|
|
3532 {
|
|
3533 public:
|
|
3534 Forward_declaration_type(Named_object* named_object);
|
|
3535
|
|
3536 // The named object associated with this type declaration. This
|
|
3537 // will be resolved.
|
|
3538 Named_object*
|
|
3539 named_object();
|
|
3540
|
|
3541 const Named_object*
|
|
3542 named_object() const;
|
|
3543
|
|
3544 // Return the name of the type.
|
|
3545 const std::string&
|
|
3546 name() const;
|
|
3547
|
|
3548 // Return the type to which this points. Give an error if the type
|
|
3549 // has not yet been defined.
|
|
3550 Type*
|
|
3551 real_type();
|
|
3552
|
|
3553 const Type*
|
|
3554 real_type() const;
|
|
3555
|
|
3556 // Whether the base type has been defined.
|
|
3557 bool
|
|
3558 is_defined() const;
|
|
3559
|
|
3560 // Add a method to this type.
|
|
3561 Named_object*
|
|
3562 add_method(const std::string& name, Function*);
|
|
3563
|
|
3564 // Add a method declaration to this type.
|
|
3565 Named_object*
|
|
3566 add_method_declaration(const std::string& name, Package*, Function_type*,
|
|
3567 Location);
|
|
3568
|
|
3569 // Add an already created object as a method to this type.
|
|
3570 void
|
|
3571 add_existing_method(Named_object*);
|
|
3572
|
|
3573 protected:
|
|
3574 int
|
|
3575 do_traverse(Traverse* traverse);
|
|
3576
|
|
3577 bool
|
|
3578 do_verify();
|
|
3579
|
|
3580 bool
|
|
3581 do_has_pointer() const
|
|
3582 { return this->real_type()->has_pointer(); }
|
|
3583
|
|
3584 bool
|
|
3585 do_compare_is_identity(Gogo* gogo)
|
|
3586 { return this->real_type()->compare_is_identity(gogo); }
|
|
3587
|
|
3588 bool
|
|
3589 do_is_reflexive()
|
|
3590 { return this->real_type()->is_reflexive(); }
|
|
3591
|
|
3592 bool
|
|
3593 do_needs_key_update()
|
|
3594 { return this->real_type()->needs_key_update(); }
|
|
3595
|
|
3596 bool
|
|
3597 do_in_heap()
|
|
3598 { return this->real_type()->in_heap(); }
|
|
3599
|
|
3600 unsigned int
|
|
3601 do_hash_for_method(Gogo* gogo) const
|
|
3602 { return this->real_type()->hash_for_method(gogo); }
|
|
3603
|
|
3604 Btype*
|
|
3605 do_get_backend(Gogo* gogo);
|
|
3606
|
|
3607 Expression*
|
|
3608 do_type_descriptor(Gogo*, Named_type*);
|
|
3609
|
|
3610 void
|
|
3611 do_reflection(Gogo*, std::string*) const;
|
|
3612
|
|
3613 void
|
|
3614 do_mangled_name(Gogo*, std::string* ret) const;
|
|
3615
|
|
3616 void
|
|
3617 do_export(Export*) const;
|
|
3618
|
|
3619 private:
|
|
3620 // Issue a warning about a use of an undefined type.
|
|
3621 void
|
|
3622 warn() const;
|
|
3623
|
|
3624 // The type declaration.
|
|
3625 Named_object* named_object_;
|
|
3626 // Whether we have issued a warning about this type.
|
|
3627 mutable bool warned_;
|
|
3628 };
|
|
3629
|
|
3630 // The Type_context struct describes what we expect for the type of an
|
|
3631 // expression.
|
|
3632
|
|
3633 struct Type_context
|
|
3634 {
|
|
3635 // The exact type we expect, if known. This may be NULL.
|
|
3636 Type* type;
|
|
3637 // Whether an abstract type is permitted.
|
|
3638 bool may_be_abstract;
|
|
3639
|
|
3640 // Constructors.
|
|
3641 Type_context()
|
|
3642 : type(NULL), may_be_abstract(false)
|
|
3643 { }
|
|
3644
|
|
3645 Type_context(Type* a_type, bool a_may_be_abstract)
|
|
3646 : type(a_type), may_be_abstract(a_may_be_abstract)
|
|
3647 { }
|
|
3648 };
|
|
3649
|
|
3650 #endif // !defined(GO_TYPES_H)
|