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