annotate gcc/go/gofrontend/types.h @ 111:04ced10e8804

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