]>
Commit | Line | Data |
---|---|---|
e440a328 | 1 | // expressions.cc -- Go frontend expression handling. |
2 | ||
3 | // Copyright 2009 The Go Authors. All rights reserved. | |
4 | // Use of this source code is governed by a BSD-style | |
5 | // license that can be found in the LICENSE file. | |
6 | ||
7 | #include "go-system.h" | |
8 | ||
9 | #include <gmp.h> | |
10 | ||
11 | #ifndef ENABLE_BUILD_WITH_CXX | |
12 | extern "C" | |
13 | { | |
14 | #endif | |
15 | ||
16 | #include "toplev.h" | |
17 | #include "intl.h" | |
18 | #include "tree.h" | |
19 | #include "gimple.h" | |
20 | #include "tree-iterator.h" | |
21 | #include "convert.h" | |
22 | #include "real.h" | |
23 | #include "realmpfr.h" | |
24 | #include "tm.h" | |
25 | #include "tm_p.h" | |
26 | ||
27 | #ifndef ENABLE_BUILD_WITH_CXX | |
28 | } | |
29 | #endif | |
30 | ||
31 | #include "go-c.h" | |
32 | #include "gogo.h" | |
33 | #include "types.h" | |
34 | #include "export.h" | |
35 | #include "import.h" | |
36 | #include "statements.h" | |
37 | #include "lex.h" | |
38 | #include "expressions.h" | |
39 | ||
40 | // Class Expression. | |
41 | ||
42 | Expression::Expression(Expression_classification classification, | |
43 | source_location location) | |
44 | : classification_(classification), location_(location) | |
45 | { | |
46 | } | |
47 | ||
48 | Expression::~Expression() | |
49 | { | |
50 | } | |
51 | ||
52 | // If this expression has a constant integer value, return it. | |
53 | ||
54 | bool | |
55 | Expression::integer_constant_value(bool iota_is_constant, mpz_t val, | |
56 | Type** ptype) const | |
57 | { | |
58 | *ptype = NULL; | |
59 | return this->do_integer_constant_value(iota_is_constant, val, ptype); | |
60 | } | |
61 | ||
62 | // If this expression has a constant floating point value, return it. | |
63 | ||
64 | bool | |
65 | Expression::float_constant_value(mpfr_t val, Type** ptype) const | |
66 | { | |
67 | *ptype = NULL; | |
68 | if (this->do_float_constant_value(val, ptype)) | |
69 | return true; | |
70 | mpz_t ival; | |
71 | mpz_init(ival); | |
72 | Type* t; | |
73 | bool ret; | |
74 | if (!this->do_integer_constant_value(false, ival, &t)) | |
75 | ret = false; | |
76 | else | |
77 | { | |
78 | mpfr_set_z(val, ival, GMP_RNDN); | |
79 | ret = true; | |
80 | } | |
81 | mpz_clear(ival); | |
82 | return ret; | |
83 | } | |
84 | ||
85 | // If this expression has a constant complex value, return it. | |
86 | ||
87 | bool | |
88 | Expression::complex_constant_value(mpfr_t real, mpfr_t imag, | |
89 | Type** ptype) const | |
90 | { | |
91 | *ptype = NULL; | |
92 | if (this->do_complex_constant_value(real, imag, ptype)) | |
93 | return true; | |
94 | Type *t; | |
95 | if (this->float_constant_value(real, &t)) | |
96 | { | |
97 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
98 | return true; | |
99 | } | |
100 | return false; | |
101 | } | |
102 | ||
103 | // Traverse the expressions. | |
104 | ||
105 | int | |
106 | Expression::traverse(Expression** pexpr, Traverse* traverse) | |
107 | { | |
108 | Expression* expr = *pexpr; | |
109 | if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0) | |
110 | { | |
111 | int t = traverse->expression(pexpr); | |
112 | if (t == TRAVERSE_EXIT) | |
113 | return TRAVERSE_EXIT; | |
114 | else if (t == TRAVERSE_SKIP_COMPONENTS) | |
115 | return TRAVERSE_CONTINUE; | |
116 | } | |
117 | return expr->do_traverse(traverse); | |
118 | } | |
119 | ||
120 | // Traverse subexpressions of this expression. | |
121 | ||
122 | int | |
123 | Expression::traverse_subexpressions(Traverse* traverse) | |
124 | { | |
125 | return this->do_traverse(traverse); | |
126 | } | |
127 | ||
128 | // Default implementation for do_traverse for child classes. | |
129 | ||
130 | int | |
131 | Expression::do_traverse(Traverse*) | |
132 | { | |
133 | return TRAVERSE_CONTINUE; | |
134 | } | |
135 | ||
136 | // This virtual function is called by the parser if the value of this | |
137 | // expression is being discarded. By default, we warn. Expressions | |
138 | // with side effects override. | |
139 | ||
140 | void | |
141 | Expression::do_discarding_value() | |
142 | { | |
143 | this->warn_about_unused_value(); | |
144 | } | |
145 | ||
146 | // This virtual function is called to export expressions. This will | |
147 | // only be used by expressions which may be constant. | |
148 | ||
149 | void | |
150 | Expression::do_export(Export*) const | |
151 | { | |
152 | gcc_unreachable(); | |
153 | } | |
154 | ||
155 | // Warn that the value of the expression is not used. | |
156 | ||
157 | void | |
158 | Expression::warn_about_unused_value() | |
159 | { | |
160 | warning_at(this->location(), OPT_Wunused_value, "value computed is not used"); | |
161 | } | |
162 | ||
163 | // Note that this expression is an error. This is called by children | |
164 | // when they discover an error. | |
165 | ||
166 | void | |
167 | Expression::set_is_error() | |
168 | { | |
169 | this->classification_ = EXPRESSION_ERROR; | |
170 | } | |
171 | ||
172 | // For children to call to report an error conveniently. | |
173 | ||
174 | void | |
175 | Expression::report_error(const char* msg) | |
176 | { | |
177 | error_at(this->location_, "%s", msg); | |
178 | this->set_is_error(); | |
179 | } | |
180 | ||
181 | // Set types of variables and constants. This is implemented by the | |
182 | // child class. | |
183 | ||
184 | void | |
185 | Expression::determine_type(const Type_context* context) | |
186 | { | |
187 | this->do_determine_type(context); | |
188 | } | |
189 | ||
190 | // Set types when there is no context. | |
191 | ||
192 | void | |
193 | Expression::determine_type_no_context() | |
194 | { | |
195 | Type_context context; | |
196 | this->do_determine_type(&context); | |
197 | } | |
198 | ||
199 | // Return a tree handling any conversions which must be done during | |
200 | // assignment. | |
201 | ||
202 | tree | |
203 | Expression::convert_for_assignment(Translate_context* context, Type* lhs_type, | |
204 | Type* rhs_type, tree rhs_tree, | |
205 | source_location location) | |
206 | { | |
207 | if (lhs_type == rhs_type) | |
208 | return rhs_tree; | |
209 | ||
210 | if (lhs_type->is_error_type() || rhs_type->is_error_type()) | |
211 | return error_mark_node; | |
212 | ||
213 | if (lhs_type->is_undefined() || rhs_type->is_undefined()) | |
214 | { | |
215 | // Make sure we report the error. | |
216 | lhs_type->base(); | |
217 | rhs_type->base(); | |
218 | return error_mark_node; | |
219 | } | |
220 | ||
221 | if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node) | |
222 | return error_mark_node; | |
223 | ||
224 | Gogo* gogo = context->gogo(); | |
225 | ||
226 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
227 | if (lhs_type_tree == error_mark_node) | |
228 | return error_mark_node; | |
229 | ||
230 | if (lhs_type->interface_type() != NULL) | |
231 | { | |
232 | if (rhs_type->interface_type() == NULL) | |
233 | return Expression::convert_type_to_interface(context, lhs_type, | |
234 | rhs_type, rhs_tree, | |
235 | location); | |
236 | else | |
237 | return Expression::convert_interface_to_interface(context, lhs_type, | |
238 | rhs_type, rhs_tree, | |
239 | false, location); | |
240 | } | |
241 | else if (rhs_type->interface_type() != NULL) | |
242 | return Expression::convert_interface_to_type(context, lhs_type, rhs_type, | |
243 | rhs_tree, location); | |
244 | else if (lhs_type->is_open_array_type() | |
245 | && rhs_type->is_nil_type()) | |
246 | { | |
247 | // Assigning nil to an open array. | |
248 | gcc_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE); | |
249 | ||
250 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
251 | ||
252 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
253 | tree field = TYPE_FIELDS(lhs_type_tree); | |
254 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
255 | "__values") == 0); | |
256 | elt->index = field; | |
257 | elt->value = fold_convert(TREE_TYPE(field), null_pointer_node); | |
258 | ||
259 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
260 | field = DECL_CHAIN(field); | |
261 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
262 | "__count") == 0); | |
263 | elt->index = field; | |
264 | elt->value = fold_convert(TREE_TYPE(field), integer_zero_node); | |
265 | ||
266 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
267 | field = DECL_CHAIN(field); | |
268 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
269 | "__capacity") == 0); | |
270 | elt->index = field; | |
271 | elt->value = fold_convert(TREE_TYPE(field), integer_zero_node); | |
272 | ||
273 | tree val = build_constructor(lhs_type_tree, init); | |
274 | TREE_CONSTANT(val) = 1; | |
275 | ||
276 | return val; | |
277 | } | |
278 | else if (rhs_type->is_nil_type()) | |
279 | { | |
280 | // The left hand side should be a pointer type at the tree | |
281 | // level. | |
282 | gcc_assert(POINTER_TYPE_P(lhs_type_tree)); | |
283 | return fold_convert(lhs_type_tree, null_pointer_node); | |
284 | } | |
285 | else if (lhs_type_tree == TREE_TYPE(rhs_tree)) | |
286 | { | |
287 | // No conversion is needed. | |
288 | return rhs_tree; | |
289 | } | |
290 | else if (POINTER_TYPE_P(lhs_type_tree) | |
291 | || INTEGRAL_TYPE_P(lhs_type_tree) | |
292 | || SCALAR_FLOAT_TYPE_P(lhs_type_tree) | |
293 | || COMPLEX_FLOAT_TYPE_P(lhs_type_tree)) | |
294 | return fold_convert_loc(location, lhs_type_tree, rhs_tree); | |
295 | else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE | |
296 | && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE) | |
297 | { | |
298 | // This conversion must be permitted by Go, or we wouldn't have | |
299 | // gotten here. | |
300 | gcc_assert(int_size_in_bytes(lhs_type_tree) | |
301 | == int_size_in_bytes(TREE_TYPE(rhs_tree))); | |
302 | return fold_build1_loc(location, VIEW_CONVERT_EXPR, lhs_type_tree, | |
303 | rhs_tree); | |
304 | } | |
305 | else | |
306 | { | |
307 | gcc_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree))); | |
308 | return rhs_tree; | |
309 | } | |
310 | } | |
311 | ||
312 | // Return a tree for a conversion from a non-interface type to an | |
313 | // interface type. | |
314 | ||
315 | tree | |
316 | Expression::convert_type_to_interface(Translate_context* context, | |
317 | Type* lhs_type, Type* rhs_type, | |
318 | tree rhs_tree, source_location location) | |
319 | { | |
320 | Gogo* gogo = context->gogo(); | |
321 | Interface_type* lhs_interface_type = lhs_type->interface_type(); | |
322 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
323 | ||
324 | // Since RHS_TYPE is a static type, we can create the interface | |
325 | // method table at compile time. | |
326 | ||
327 | // When setting an interface to nil, we just set both fields to | |
328 | // NULL. | |
329 | if (rhs_type->is_nil_type()) | |
330 | return lhs_type->get_init_tree(gogo, false); | |
331 | ||
332 | // This should have been checked already. | |
333 | gcc_assert(lhs_interface_type->implements_interface(rhs_type, NULL)); | |
334 | ||
335 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
336 | if (lhs_type_tree == error_mark_node) | |
337 | return error_mark_node; | |
338 | ||
339 | // An interface is a tuple. If LHS_TYPE is an empty interface type, | |
340 | // then the first field is the type descriptor for RHS_TYPE. | |
341 | // Otherwise it is the interface method table for RHS_TYPE. | |
342 | tree first_field_value; | |
343 | if (lhs_is_empty) | |
344 | first_field_value = rhs_type->type_descriptor_pointer(gogo); | |
345 | else | |
346 | { | |
347 | // Build the interface method table for this interface and this | |
348 | // object type: a list of function pointers for each interface | |
349 | // method. | |
350 | Named_type* rhs_named_type = rhs_type->named_type(); | |
351 | bool is_pointer = false; | |
352 | if (rhs_named_type == NULL) | |
353 | { | |
354 | rhs_named_type = rhs_type->deref()->named_type(); | |
355 | is_pointer = true; | |
356 | } | |
357 | tree method_table; | |
358 | if (rhs_named_type == NULL) | |
359 | method_table = null_pointer_node; | |
360 | else | |
361 | method_table = | |
362 | rhs_named_type->interface_method_table(gogo, lhs_interface_type, | |
363 | is_pointer); | |
364 | first_field_value = fold_convert_loc(location, const_ptr_type_node, | |
365 | method_table); | |
366 | } | |
367 | ||
368 | // Start building a constructor for the value we will return. | |
369 | ||
370 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2); | |
371 | ||
372 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
373 | tree field = TYPE_FIELDS(lhs_type_tree); | |
374 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
375 | (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0); | |
376 | elt->index = field; | |
377 | elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value); | |
378 | ||
379 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
380 | field = DECL_CHAIN(field); | |
381 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); | |
382 | elt->index = field; | |
383 | ||
384 | if (rhs_type->points_to() != NULL) | |
385 | { | |
386 | // We are assigning a pointer to the interface; the interface | |
387 | // holds the pointer itself. | |
388 | elt->value = rhs_tree; | |
389 | return build_constructor(lhs_type_tree, init); | |
390 | } | |
391 | ||
392 | // We are assigning a non-pointer value to the interface; the | |
393 | // interface gets a copy of the value in the heap. | |
394 | ||
395 | tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree)); | |
396 | ||
397 | tree space = gogo->allocate_memory(rhs_type, object_size, location); | |
398 | space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)), | |
399 | space); | |
400 | space = save_expr(space); | |
401 | ||
402 | tree ref = build_fold_indirect_ref_loc(location, space); | |
403 | TREE_THIS_NOTRAP(ref) = 1; | |
404 | tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node, | |
405 | ref, rhs_tree); | |
406 | ||
407 | elt->value = fold_convert_loc(location, TREE_TYPE(field), space); | |
408 | ||
409 | return build2(COMPOUND_EXPR, lhs_type_tree, set, | |
410 | build_constructor(lhs_type_tree, init)); | |
411 | } | |
412 | ||
413 | // Return a tree for the type descriptor of RHS_TREE, which has | |
414 | // interface type RHS_TYPE. If RHS_TREE is nil the result will be | |
415 | // NULL. | |
416 | ||
417 | tree | |
418 | Expression::get_interface_type_descriptor(Translate_context*, | |
419 | Type* rhs_type, tree rhs_tree, | |
420 | source_location location) | |
421 | { | |
422 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
423 | gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); | |
424 | tree rhs_field = TYPE_FIELDS(rhs_type_tree); | |
425 | tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
426 | NULL_TREE); | |
427 | if (rhs_type->interface_type()->is_empty()) | |
428 | { | |
429 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), | |
430 | "__type_descriptor") == 0); | |
431 | return v; | |
432 | } | |
433 | ||
434 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods") | |
435 | == 0); | |
436 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(v))); | |
437 | v = save_expr(v); | |
438 | tree v1 = build_fold_indirect_ref_loc(location, v); | |
439 | gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE); | |
440 | tree f = TYPE_FIELDS(TREE_TYPE(v1)); | |
441 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor") | |
442 | == 0); | |
443 | v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE); | |
444 | ||
445 | tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v, | |
446 | fold_convert_loc(location, TREE_TYPE(v), | |
447 | null_pointer_node)); | |
448 | tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node); | |
449 | return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1), | |
450 | eq, n, v1); | |
451 | } | |
452 | ||
453 | // Return a tree for the conversion of an interface type to an | |
454 | // interface type. | |
455 | ||
456 | tree | |
457 | Expression::convert_interface_to_interface(Translate_context* context, | |
458 | Type *lhs_type, Type *rhs_type, | |
459 | tree rhs_tree, bool for_type_guard, | |
460 | source_location location) | |
461 | { | |
462 | Gogo* gogo = context->gogo(); | |
463 | Interface_type* lhs_interface_type = lhs_type->interface_type(); | |
464 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
465 | ||
466 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
467 | if (lhs_type_tree == error_mark_node) | |
468 | return error_mark_node; | |
469 | ||
470 | // In the general case this requires runtime examination of the type | |
471 | // method table to match it up with the interface methods. | |
472 | ||
473 | // FIXME: If all of the methods in the right hand side interface | |
474 | // also appear in the left hand side interface, then we don't need | |
475 | // to do a runtime check, although we still need to build a new | |
476 | // method table. | |
477 | ||
478 | // Get the type descriptor for the right hand side. This will be | |
479 | // NULL for a nil interface. | |
480 | ||
481 | if (!DECL_P(rhs_tree)) | |
482 | rhs_tree = save_expr(rhs_tree); | |
483 | ||
484 | tree rhs_type_descriptor = | |
485 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
486 | location); | |
487 | ||
488 | // The result is going to be a two element constructor. | |
489 | ||
490 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2); | |
491 | ||
492 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
493 | tree field = TYPE_FIELDS(lhs_type_tree); | |
494 | elt->index = field; | |
495 | ||
496 | if (for_type_guard) | |
497 | { | |
498 | // A type assertion fails when converting a nil interface. | |
499 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo); | |
500 | static tree assert_interface_decl; | |
501 | tree call = Gogo::call_builtin(&assert_interface_decl, | |
502 | location, | |
503 | "__go_assert_interface", | |
504 | 2, | |
505 | ptr_type_node, | |
506 | TREE_TYPE(lhs_type_descriptor), | |
507 | lhs_type_descriptor, | |
508 | TREE_TYPE(rhs_type_descriptor), | |
509 | rhs_type_descriptor); | |
510 | // This will panic if the interface conversion fails. | |
511 | TREE_NOTHROW(assert_interface_decl) = 0; | |
512 | elt->value = fold_convert_loc(location, TREE_TYPE(field), call); | |
513 | } | |
514 | else if (lhs_is_empty) | |
515 | { | |
516 | // A convertion to an empty interface always succeeds, and the | |
517 | // first field is just the type descriptor of the object. | |
518 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
519 | "__type_descriptor") == 0); | |
520 | gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor)); | |
521 | elt->value = rhs_type_descriptor; | |
522 | } | |
523 | else | |
524 | { | |
525 | // A conversion to a non-empty interface may fail, but unlike a | |
526 | // type assertion converting nil will always succeed. | |
527 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") | |
528 | == 0); | |
529 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo); | |
530 | static tree convert_interface_decl; | |
531 | tree call = Gogo::call_builtin(&convert_interface_decl, | |
532 | location, | |
533 | "__go_convert_interface", | |
534 | 2, | |
535 | ptr_type_node, | |
536 | TREE_TYPE(lhs_type_descriptor), | |
537 | lhs_type_descriptor, | |
538 | TREE_TYPE(rhs_type_descriptor), | |
539 | rhs_type_descriptor); | |
540 | // This will panic if the interface conversion fails. | |
541 | TREE_NOTHROW(convert_interface_decl) = 0; | |
542 | elt->value = fold_convert_loc(location, TREE_TYPE(field), call); | |
543 | } | |
544 | ||
545 | // The second field is simply the object pointer. | |
546 | ||
547 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
548 | field = DECL_CHAIN(field); | |
549 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); | |
550 | elt->index = field; | |
551 | ||
552 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
553 | gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); | |
554 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); | |
555 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); | |
556 | elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
557 | NULL_TREE); | |
558 | ||
559 | return build_constructor(lhs_type_tree, init); | |
560 | } | |
561 | ||
562 | // Return a tree for the conversion of an interface type to a | |
563 | // non-interface type. | |
564 | ||
565 | tree | |
566 | Expression::convert_interface_to_type(Translate_context* context, | |
567 | Type *lhs_type, Type* rhs_type, | |
568 | tree rhs_tree, source_location location) | |
569 | { | |
570 | Gogo* gogo = context->gogo(); | |
571 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
572 | ||
573 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
574 | if (lhs_type_tree == error_mark_node) | |
575 | return error_mark_node; | |
576 | ||
577 | // Call a function to check that the type is valid. The function | |
578 | // will panic with an appropriate runtime type error if the type is | |
579 | // not valid. | |
580 | ||
581 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo); | |
582 | ||
583 | if (!DECL_P(rhs_tree)) | |
584 | rhs_tree = save_expr(rhs_tree); | |
585 | ||
586 | tree rhs_type_descriptor = | |
587 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
588 | location); | |
589 | ||
590 | tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo); | |
591 | ||
592 | static tree check_interface_type_decl; | |
593 | tree call = Gogo::call_builtin(&check_interface_type_decl, | |
594 | location, | |
595 | "__go_check_interface_type", | |
596 | 3, | |
597 | void_type_node, | |
598 | TREE_TYPE(lhs_type_descriptor), | |
599 | lhs_type_descriptor, | |
600 | TREE_TYPE(rhs_type_descriptor), | |
601 | rhs_type_descriptor, | |
602 | TREE_TYPE(rhs_inter_descriptor), | |
603 | rhs_inter_descriptor); | |
604 | // This call will panic if the conversion is invalid. | |
605 | TREE_NOTHROW(check_interface_type_decl) = 0; | |
606 | ||
607 | // If the call succeeds, pull out the value. | |
608 | gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); | |
609 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); | |
610 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); | |
611 | tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
612 | NULL_TREE); | |
613 | ||
614 | // If the value is a pointer, then it is the value we want. | |
615 | // Otherwise it points to the value. | |
616 | if (lhs_type->points_to() == NULL) | |
617 | { | |
618 | val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val); | |
619 | val = build_fold_indirect_ref_loc(location, val); | |
620 | } | |
621 | ||
622 | return build2(COMPOUND_EXPR, lhs_type_tree, call, | |
623 | fold_convert_loc(location, lhs_type_tree, val)); | |
624 | } | |
625 | ||
626 | // Convert an expression to a tree. This is implemented by the child | |
627 | // class. Not that it is not in general safe to call this multiple | |
628 | // times for a single expression, but that we don't catch such errors. | |
629 | ||
630 | tree | |
631 | Expression::get_tree(Translate_context* context) | |
632 | { | |
633 | // The child may have marked this expression as having an error. | |
634 | if (this->classification_ == EXPRESSION_ERROR) | |
635 | return error_mark_node; | |
636 | ||
637 | return this->do_get_tree(context); | |
638 | } | |
639 | ||
640 | // Return a tree for VAL in TYPE. | |
641 | ||
642 | tree | |
643 | Expression::integer_constant_tree(mpz_t val, tree type) | |
644 | { | |
645 | if (type == error_mark_node) | |
646 | return error_mark_node; | |
647 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
648 | return double_int_to_tree(type, | |
649 | mpz_get_double_int(type, val, true)); | |
650 | else if (TREE_CODE(type) == REAL_TYPE) | |
651 | { | |
652 | mpfr_t fval; | |
653 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
654 | tree ret = Expression::float_constant_tree(fval, type); | |
655 | mpfr_clear(fval); | |
656 | return ret; | |
657 | } | |
658 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
659 | { | |
660 | mpfr_t fval; | |
661 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
662 | tree real = Expression::float_constant_tree(fval, TREE_TYPE(type)); | |
663 | mpfr_clear(fval); | |
664 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
665 | integer_zero_node); | |
666 | return build_complex(type, real, imag); | |
667 | } | |
668 | else | |
669 | gcc_unreachable(); | |
670 | } | |
671 | ||
672 | // Return a tree for VAL in TYPE. | |
673 | ||
674 | tree | |
675 | Expression::float_constant_tree(mpfr_t val, tree type) | |
676 | { | |
677 | if (type == error_mark_node) | |
678 | return error_mark_node; | |
679 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
680 | { | |
681 | mpz_t ival; | |
682 | mpz_init(ival); | |
683 | mpfr_get_z(ival, val, GMP_RNDN); | |
684 | tree ret = Expression::integer_constant_tree(ival, type); | |
685 | mpz_clear(ival); | |
686 | return ret; | |
687 | } | |
688 | else if (TREE_CODE(type) == REAL_TYPE) | |
689 | { | |
690 | REAL_VALUE_TYPE r1; | |
691 | real_from_mpfr(&r1, val, type, GMP_RNDN); | |
692 | REAL_VALUE_TYPE r2; | |
693 | real_convert(&r2, TYPE_MODE(type), &r1); | |
694 | return build_real(type, r2); | |
695 | } | |
696 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
697 | { | |
698 | REAL_VALUE_TYPE r1; | |
699 | real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN); | |
700 | REAL_VALUE_TYPE r2; | |
701 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
702 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
703 | integer_zero_node); | |
704 | return build_complex(type, build_real(TREE_TYPE(type), r2), imag); | |
705 | } | |
706 | else | |
707 | gcc_unreachable(); | |
708 | } | |
709 | ||
710 | // Return a tree for REAL/IMAG in TYPE. | |
711 | ||
712 | tree | |
713 | Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type) | |
714 | { | |
715 | if (TREE_CODE(type) == COMPLEX_TYPE) | |
716 | { | |
717 | REAL_VALUE_TYPE r1; | |
718 | real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN); | |
719 | REAL_VALUE_TYPE r2; | |
720 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
721 | ||
722 | REAL_VALUE_TYPE r3; | |
723 | real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN); | |
724 | REAL_VALUE_TYPE r4; | |
725 | real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3); | |
726 | ||
727 | return build_complex(type, build_real(TREE_TYPE(type), r2), | |
728 | build_real(TREE_TYPE(type), r4)); | |
729 | } | |
730 | else | |
731 | gcc_unreachable(); | |
732 | } | |
733 | ||
734 | // Return a tree which evaluates to true if VAL, of arbitrary integer | |
735 | // type, is negative or is more than the maximum value of BOUND_TYPE. | |
736 | // If SOFAR is not NULL, it is or'red into the result. The return | |
737 | // value may be NULL if SOFAR is NULL. | |
738 | ||
739 | tree | |
740 | Expression::check_bounds(tree val, tree bound_type, tree sofar, | |
741 | source_location loc) | |
742 | { | |
743 | tree val_type = TREE_TYPE(val); | |
744 | tree ret = NULL_TREE; | |
745 | ||
746 | if (!TYPE_UNSIGNED(val_type)) | |
747 | { | |
748 | ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val, | |
749 | build_int_cst(val_type, 0)); | |
750 | if (ret == boolean_false_node) | |
751 | ret = NULL_TREE; | |
752 | } | |
753 | ||
754 | if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type)) | |
755 | || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type)) | |
756 | { | |
757 | tree max = TYPE_MAX_VALUE(bound_type); | |
758 | tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val, | |
759 | fold_convert_loc(loc, val_type, max)); | |
760 | if (big == boolean_false_node) | |
761 | ; | |
762 | else if (ret == NULL_TREE) | |
763 | ret = big; | |
764 | else | |
765 | ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
766 | ret, big); | |
767 | } | |
768 | ||
769 | if (ret == NULL_TREE) | |
770 | return sofar; | |
771 | else if (sofar == NULL_TREE) | |
772 | return ret; | |
773 | else | |
774 | return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
775 | sofar, ret); | |
776 | } | |
777 | ||
778 | // Error expressions. This are used to avoid cascading errors. | |
779 | ||
780 | class Error_expression : public Expression | |
781 | { | |
782 | public: | |
783 | Error_expression(source_location location) | |
784 | : Expression(EXPRESSION_ERROR, location) | |
785 | { } | |
786 | ||
787 | protected: | |
788 | bool | |
789 | do_is_constant() const | |
790 | { return true; } | |
791 | ||
792 | bool | |
793 | do_integer_constant_value(bool, mpz_t val, Type**) const | |
794 | { | |
795 | mpz_set_ui(val, 0); | |
796 | return true; | |
797 | } | |
798 | ||
799 | bool | |
800 | do_float_constant_value(mpfr_t val, Type**) const | |
801 | { | |
802 | mpfr_set_ui(val, 0, GMP_RNDN); | |
803 | return true; | |
804 | } | |
805 | ||
806 | bool | |
807 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const | |
808 | { | |
809 | mpfr_set_ui(real, 0, GMP_RNDN); | |
810 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
811 | return true; | |
812 | } | |
813 | ||
814 | void | |
815 | do_discarding_value() | |
816 | { } | |
817 | ||
818 | Type* | |
819 | do_type() | |
820 | { return Type::make_error_type(); } | |
821 | ||
822 | void | |
823 | do_determine_type(const Type_context*) | |
824 | { } | |
825 | ||
826 | Expression* | |
827 | do_copy() | |
828 | { return this; } | |
829 | ||
830 | bool | |
831 | do_is_addressable() const | |
832 | { return true; } | |
833 | ||
834 | tree | |
835 | do_get_tree(Translate_context*) | |
836 | { return error_mark_node; } | |
837 | }; | |
838 | ||
839 | Expression* | |
840 | Expression::make_error(source_location location) | |
841 | { | |
842 | return new Error_expression(location); | |
843 | } | |
844 | ||
845 | // An expression which is really a type. This is used during parsing. | |
846 | // It is an error if these survive after lowering. | |
847 | ||
848 | class | |
849 | Type_expression : public Expression | |
850 | { | |
851 | public: | |
852 | Type_expression(Type* type, source_location location) | |
853 | : Expression(EXPRESSION_TYPE, location), | |
854 | type_(type) | |
855 | { } | |
856 | ||
857 | protected: | |
858 | int | |
859 | do_traverse(Traverse* traverse) | |
860 | { return Type::traverse(this->type_, traverse); } | |
861 | ||
862 | Type* | |
863 | do_type() | |
864 | { return this->type_; } | |
865 | ||
866 | void | |
867 | do_determine_type(const Type_context*) | |
868 | { } | |
869 | ||
870 | void | |
871 | do_check_types(Gogo*) | |
872 | { this->report_error(_("invalid use of type")); } | |
873 | ||
874 | Expression* | |
875 | do_copy() | |
876 | { return this; } | |
877 | ||
878 | tree | |
879 | do_get_tree(Translate_context*) | |
880 | { gcc_unreachable(); } | |
881 | ||
882 | private: | |
883 | // The type which we are representing as an expression. | |
884 | Type* type_; | |
885 | }; | |
886 | ||
887 | Expression* | |
888 | Expression::make_type(Type* type, source_location location) | |
889 | { | |
890 | return new Type_expression(type, location); | |
891 | } | |
892 | ||
893 | // Class Var_expression. | |
894 | ||
895 | // Lower a variable expression. Here we just make sure that the | |
896 | // initialization expression of the variable has been lowered. This | |
897 | // ensures that we will be able to determine the type of the variable | |
898 | // if necessary. | |
899 | ||
900 | Expression* | |
901 | Var_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
902 | { | |
903 | if (this->variable_->is_variable()) | |
904 | { | |
905 | Variable* var = this->variable_->var_value(); | |
906 | // This is either a local variable or a global variable. A | |
907 | // reference to a variable which is local to an enclosing | |
908 | // function will be a reference to a field in a closure. | |
909 | if (var->is_global()) | |
910 | function = NULL; | |
911 | var->lower_init_expression(gogo, function); | |
912 | } | |
913 | return this; | |
914 | } | |
915 | ||
916 | // Return the name of the variable. | |
917 | ||
918 | const std::string& | |
919 | Var_expression::name() const | |
920 | { | |
921 | return this->variable_->name(); | |
922 | } | |
923 | ||
924 | // Return the type of a reference to a variable. | |
925 | ||
926 | Type* | |
927 | Var_expression::do_type() | |
928 | { | |
929 | if (this->variable_->is_variable()) | |
930 | return this->variable_->var_value()->type(); | |
931 | else if (this->variable_->is_result_variable()) | |
932 | return this->variable_->result_var_value()->type(); | |
933 | else | |
934 | gcc_unreachable(); | |
935 | } | |
936 | ||
937 | // Something takes the address of this variable. This means that we | |
938 | // may want to move the variable onto the heap. | |
939 | ||
940 | void | |
941 | Var_expression::do_address_taken(bool escapes) | |
942 | { | |
943 | if (!escapes) | |
944 | ; | |
945 | else if (this->variable_->is_variable()) | |
946 | this->variable_->var_value()->set_address_taken(); | |
947 | else if (this->variable_->is_result_variable()) | |
948 | this->variable_->result_var_value()->set_address_taken(); | |
949 | else | |
950 | gcc_unreachable(); | |
951 | } | |
952 | ||
953 | // Get the tree for a reference to a variable. | |
954 | ||
955 | tree | |
956 | Var_expression::do_get_tree(Translate_context* context) | |
957 | { | |
958 | return this->variable_->get_tree(context->gogo(), context->function()); | |
959 | } | |
960 | ||
961 | // Make a reference to a variable in an expression. | |
962 | ||
963 | Expression* | |
964 | Expression::make_var_reference(Named_object* var, source_location location) | |
965 | { | |
966 | if (var->is_sink()) | |
967 | return Expression::make_sink(location); | |
968 | ||
969 | // FIXME: Creating a new object for each reference to a variable is | |
970 | // wasteful. | |
971 | return new Var_expression(var, location); | |
972 | } | |
973 | ||
974 | // Class Temporary_reference_expression. | |
975 | ||
976 | // The type. | |
977 | ||
978 | Type* | |
979 | Temporary_reference_expression::do_type() | |
980 | { | |
981 | return this->statement_->type(); | |
982 | } | |
983 | ||
984 | // Called if something takes the address of this temporary variable. | |
985 | // We never have to move temporary variables to the heap, but we do | |
986 | // need to know that they must live in the stack rather than in a | |
987 | // register. | |
988 | ||
989 | void | |
990 | Temporary_reference_expression::do_address_taken(bool) | |
991 | { | |
992 | this->statement_->set_is_address_taken(); | |
993 | } | |
994 | ||
995 | // Get a tree referring to the variable. | |
996 | ||
997 | tree | |
998 | Temporary_reference_expression::do_get_tree(Translate_context*) | |
999 | { | |
1000 | return this->statement_->get_decl(); | |
1001 | } | |
1002 | ||
1003 | // Make a reference to a temporary variable. | |
1004 | ||
1005 | Expression* | |
1006 | Expression::make_temporary_reference(Temporary_statement* statement, | |
1007 | source_location location) | |
1008 | { | |
1009 | return new Temporary_reference_expression(statement, location); | |
1010 | } | |
1011 | ||
1012 | // A sink expression--a use of the blank identifier _. | |
1013 | ||
1014 | class Sink_expression : public Expression | |
1015 | { | |
1016 | public: | |
1017 | Sink_expression(source_location location) | |
1018 | : Expression(EXPRESSION_SINK, location), | |
1019 | type_(NULL), var_(NULL_TREE) | |
1020 | { } | |
1021 | ||
1022 | protected: | |
1023 | void | |
1024 | do_discarding_value() | |
1025 | { } | |
1026 | ||
1027 | Type* | |
1028 | do_type(); | |
1029 | ||
1030 | void | |
1031 | do_determine_type(const Type_context*); | |
1032 | ||
1033 | Expression* | |
1034 | do_copy() | |
1035 | { return new Sink_expression(this->location()); } | |
1036 | ||
1037 | tree | |
1038 | do_get_tree(Translate_context*); | |
1039 | ||
1040 | private: | |
1041 | // The type of this sink variable. | |
1042 | Type* type_; | |
1043 | // The temporary variable we generate. | |
1044 | tree var_; | |
1045 | }; | |
1046 | ||
1047 | // Return the type of a sink expression. | |
1048 | ||
1049 | Type* | |
1050 | Sink_expression::do_type() | |
1051 | { | |
1052 | if (this->type_ == NULL) | |
1053 | return Type::make_sink_type(); | |
1054 | return this->type_; | |
1055 | } | |
1056 | ||
1057 | // Determine the type of a sink expression. | |
1058 | ||
1059 | void | |
1060 | Sink_expression::do_determine_type(const Type_context* context) | |
1061 | { | |
1062 | if (context->type != NULL) | |
1063 | this->type_ = context->type; | |
1064 | } | |
1065 | ||
1066 | // Return a temporary variable for a sink expression. This will | |
1067 | // presumably be a write-only variable which the middle-end will drop. | |
1068 | ||
1069 | tree | |
1070 | Sink_expression::do_get_tree(Translate_context* context) | |
1071 | { | |
1072 | if (this->var_ == NULL_TREE) | |
1073 | { | |
1074 | gcc_assert(this->type_ != NULL && !this->type_->is_sink_type()); | |
1075 | this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()), | |
1076 | "blank"); | |
1077 | } | |
1078 | return this->var_; | |
1079 | } | |
1080 | ||
1081 | // Make a sink expression. | |
1082 | ||
1083 | Expression* | |
1084 | Expression::make_sink(source_location location) | |
1085 | { | |
1086 | return new Sink_expression(location); | |
1087 | } | |
1088 | ||
1089 | // Class Func_expression. | |
1090 | ||
1091 | // FIXME: Can a function expression appear in a constant expression? | |
1092 | // The value is unchanging. Initializing a constant to the address of | |
1093 | // a function seems like it could work, though there might be little | |
1094 | // point to it. | |
1095 | ||
1096 | // Return the name of the function. | |
1097 | ||
1098 | const std::string& | |
1099 | Func_expression::name() const | |
1100 | { | |
1101 | return this->function_->name(); | |
1102 | } | |
1103 | ||
1104 | // Traversal. | |
1105 | ||
1106 | int | |
1107 | Func_expression::do_traverse(Traverse* traverse) | |
1108 | { | |
1109 | return (this->closure_ == NULL | |
1110 | ? TRAVERSE_CONTINUE | |
1111 | : Expression::traverse(&this->closure_, traverse)); | |
1112 | } | |
1113 | ||
1114 | // Return the type of a function expression. | |
1115 | ||
1116 | Type* | |
1117 | Func_expression::do_type() | |
1118 | { | |
1119 | if (this->function_->is_function()) | |
1120 | return this->function_->func_value()->type(); | |
1121 | else if (this->function_->is_function_declaration()) | |
1122 | return this->function_->func_declaration_value()->type(); | |
1123 | else | |
1124 | gcc_unreachable(); | |
1125 | } | |
1126 | ||
1127 | // Get the tree for a function expression without evaluating the | |
1128 | // closure. | |
1129 | ||
1130 | tree | |
1131 | Func_expression::get_tree_without_closure(Gogo* gogo) | |
1132 | { | |
1133 | Function_type* fntype; | |
1134 | if (this->function_->is_function()) | |
1135 | fntype = this->function_->func_value()->type(); | |
1136 | else if (this->function_->is_function_declaration()) | |
1137 | fntype = this->function_->func_declaration_value()->type(); | |
1138 | else | |
1139 | gcc_unreachable(); | |
1140 | ||
1141 | // Builtin functions are handled specially by Call_expression. We | |
1142 | // can't take their address. | |
1143 | if (fntype->is_builtin()) | |
1144 | { | |
1145 | error_at(this->location(), "invalid use of special builtin function %qs", | |
1146 | this->function_->name().c_str()); | |
1147 | return error_mark_node; | |
1148 | } | |
1149 | ||
1150 | Named_object* no = this->function_; | |
1151 | tree id = this->function_->get_id(gogo); | |
1152 | tree fndecl; | |
1153 | if (no->is_function()) | |
1154 | fndecl = no->func_value()->get_or_make_decl(gogo, no, id); | |
1155 | else if (no->is_function_declaration()) | |
1156 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id); | |
1157 | else | |
1158 | gcc_unreachable(); | |
1159 | ||
1160 | return build_fold_addr_expr_loc(this->location(), fndecl); | |
1161 | } | |
1162 | ||
1163 | // Get the tree for a function expression. This is used when we take | |
1164 | // the address of a function rather than simply calling it. If the | |
1165 | // function has a closure, we must use a trampoline. | |
1166 | ||
1167 | tree | |
1168 | Func_expression::do_get_tree(Translate_context* context) | |
1169 | { | |
1170 | Gogo* gogo = context->gogo(); | |
1171 | ||
1172 | tree fnaddr = this->get_tree_without_closure(gogo); | |
1173 | if (fnaddr == error_mark_node) | |
1174 | return error_mark_node; | |
1175 | ||
1176 | gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR | |
1177 | && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL); | |
1178 | TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1; | |
1179 | ||
1180 | // For a normal non-nested function call, that is all we have to do. | |
1181 | if (!this->function_->is_function() | |
1182 | || this->function_->func_value()->enclosing() == NULL) | |
1183 | { | |
1184 | gcc_assert(this->closure_ == NULL); | |
1185 | return fnaddr; | |
1186 | } | |
1187 | ||
1188 | // For a nested function call, we have to always allocate a | |
1189 | // trampoline. If we don't always allocate, then closures will not | |
1190 | // be reliably distinct. | |
1191 | Expression* closure = this->closure_; | |
1192 | tree closure_tree; | |
1193 | if (closure == NULL) | |
1194 | closure_tree = null_pointer_node; | |
1195 | else | |
1196 | { | |
1197 | // Get the value of the closure. This will be a pointer to | |
1198 | // space allocated on the heap. | |
1199 | closure_tree = closure->get_tree(context); | |
1200 | if (closure_tree == error_mark_node) | |
1201 | return error_mark_node; | |
1202 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree))); | |
1203 | } | |
1204 | ||
1205 | // Now we need to build some code on the heap. This code will load | |
1206 | // the static chain pointer with the closure and then jump to the | |
1207 | // body of the function. The normal gcc approach is to build the | |
1208 | // code on the stack. Unfortunately we can not do that, as Go | |
1209 | // permits us to return the function pointer. | |
1210 | ||
1211 | return gogo->make_trampoline(fnaddr, closure_tree, this->location()); | |
1212 | } | |
1213 | ||
1214 | // Make a reference to a function in an expression. | |
1215 | ||
1216 | Expression* | |
1217 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
1218 | source_location location) | |
1219 | { | |
1220 | return new Func_expression(function, closure, location); | |
1221 | } | |
1222 | ||
1223 | // Class Unknown_expression. | |
1224 | ||
1225 | // Return the name of an unknown expression. | |
1226 | ||
1227 | const std::string& | |
1228 | Unknown_expression::name() const | |
1229 | { | |
1230 | return this->named_object_->name(); | |
1231 | } | |
1232 | ||
1233 | // Lower a reference to an unknown name. | |
1234 | ||
1235 | Expression* | |
1236 | Unknown_expression::do_lower(Gogo*, Named_object*, int) | |
1237 | { | |
1238 | source_location location = this->location(); | |
1239 | Named_object* no = this->named_object_; | |
1240 | Named_object* real = no->unknown_value()->real_named_object(); | |
1241 | if (real == NULL) | |
1242 | { | |
1243 | if (this->is_composite_literal_key_) | |
1244 | return this; | |
1245 | error_at(location, "reference to undefined name %qs", | |
1246 | this->named_object_->message_name().c_str()); | |
1247 | return Expression::make_error(location); | |
1248 | } | |
1249 | switch (real->classification()) | |
1250 | { | |
1251 | case Named_object::NAMED_OBJECT_CONST: | |
1252 | return Expression::make_const_reference(real, location); | |
1253 | case Named_object::NAMED_OBJECT_TYPE: | |
1254 | return Expression::make_type(real->type_value(), location); | |
1255 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1256 | if (this->is_composite_literal_key_) | |
1257 | return this; | |
1258 | error_at(location, "reference to undefined type %qs", | |
1259 | real->message_name().c_str()); | |
1260 | return Expression::make_error(location); | |
1261 | case Named_object::NAMED_OBJECT_VAR: | |
1262 | return Expression::make_var_reference(real, location); | |
1263 | case Named_object::NAMED_OBJECT_FUNC: | |
1264 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1265 | return Expression::make_func_reference(real, NULL, location); | |
1266 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1267 | if (this->is_composite_literal_key_) | |
1268 | return this; | |
1269 | error_at(location, "unexpected reference to package"); | |
1270 | return Expression::make_error(location); | |
1271 | default: | |
1272 | gcc_unreachable(); | |
1273 | } | |
1274 | } | |
1275 | ||
1276 | // Make a reference to an unknown name. | |
1277 | ||
1278 | Expression* | |
1279 | Expression::make_unknown_reference(Named_object* no, source_location location) | |
1280 | { | |
1281 | gcc_assert(no->resolve()->is_unknown()); | |
1282 | return new Unknown_expression(no, location); | |
1283 | } | |
1284 | ||
1285 | // A boolean expression. | |
1286 | ||
1287 | class Boolean_expression : public Expression | |
1288 | { | |
1289 | public: | |
1290 | Boolean_expression(bool val, source_location location) | |
1291 | : Expression(EXPRESSION_BOOLEAN, location), | |
1292 | val_(val), type_(NULL) | |
1293 | { } | |
1294 | ||
1295 | static Expression* | |
1296 | do_import(Import*); | |
1297 | ||
1298 | protected: | |
1299 | bool | |
1300 | do_is_constant() const | |
1301 | { return true; } | |
1302 | ||
1303 | Type* | |
1304 | do_type(); | |
1305 | ||
1306 | void | |
1307 | do_determine_type(const Type_context*); | |
1308 | ||
1309 | Expression* | |
1310 | do_copy() | |
1311 | { return this; } | |
1312 | ||
1313 | tree | |
1314 | do_get_tree(Translate_context*) | |
1315 | { return this->val_ ? boolean_true_node : boolean_false_node; } | |
1316 | ||
1317 | void | |
1318 | do_export(Export* exp) const | |
1319 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1320 | ||
1321 | private: | |
1322 | // The constant. | |
1323 | bool val_; | |
1324 | // The type as determined by context. | |
1325 | Type* type_; | |
1326 | }; | |
1327 | ||
1328 | // Get the type. | |
1329 | ||
1330 | Type* | |
1331 | Boolean_expression::do_type() | |
1332 | { | |
1333 | if (this->type_ == NULL) | |
1334 | this->type_ = Type::make_boolean_type(); | |
1335 | return this->type_; | |
1336 | } | |
1337 | ||
1338 | // Set the type from the context. | |
1339 | ||
1340 | void | |
1341 | Boolean_expression::do_determine_type(const Type_context* context) | |
1342 | { | |
1343 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1344 | ; | |
1345 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1346 | this->type_ = context->type; | |
1347 | else if (!context->may_be_abstract) | |
1348 | this->type_ = Type::lookup_bool_type(); | |
1349 | } | |
1350 | ||
1351 | // Import a boolean constant. | |
1352 | ||
1353 | Expression* | |
1354 | Boolean_expression::do_import(Import* imp) | |
1355 | { | |
1356 | if (imp->peek_char() == 't') | |
1357 | { | |
1358 | imp->require_c_string("true"); | |
1359 | return Expression::make_boolean(true, imp->location()); | |
1360 | } | |
1361 | else | |
1362 | { | |
1363 | imp->require_c_string("false"); | |
1364 | return Expression::make_boolean(false, imp->location()); | |
1365 | } | |
1366 | } | |
1367 | ||
1368 | // Make a boolean expression. | |
1369 | ||
1370 | Expression* | |
1371 | Expression::make_boolean(bool val, source_location location) | |
1372 | { | |
1373 | return new Boolean_expression(val, location); | |
1374 | } | |
1375 | ||
1376 | // Class String_expression. | |
1377 | ||
1378 | // Get the type. | |
1379 | ||
1380 | Type* | |
1381 | String_expression::do_type() | |
1382 | { | |
1383 | if (this->type_ == NULL) | |
1384 | this->type_ = Type::make_string_type(); | |
1385 | return this->type_; | |
1386 | } | |
1387 | ||
1388 | // Set the type from the context. | |
1389 | ||
1390 | void | |
1391 | String_expression::do_determine_type(const Type_context* context) | |
1392 | { | |
1393 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1394 | ; | |
1395 | else if (context->type != NULL && context->type->is_string_type()) | |
1396 | this->type_ = context->type; | |
1397 | else if (!context->may_be_abstract) | |
1398 | this->type_ = Type::lookup_string_type(); | |
1399 | } | |
1400 | ||
1401 | // Build a string constant. | |
1402 | ||
1403 | tree | |
1404 | String_expression::do_get_tree(Translate_context* context) | |
1405 | { | |
1406 | return context->gogo()->go_string_constant_tree(this->val_); | |
1407 | } | |
1408 | ||
1409 | // Export a string expression. | |
1410 | ||
1411 | void | |
1412 | String_expression::do_export(Export* exp) const | |
1413 | { | |
1414 | std::string s; | |
1415 | s.reserve(this->val_.length() * 4 + 2); | |
1416 | s += '"'; | |
1417 | for (std::string::const_iterator p = this->val_.begin(); | |
1418 | p != this->val_.end(); | |
1419 | ++p) | |
1420 | { | |
1421 | if (*p == '\\' || *p == '"') | |
1422 | { | |
1423 | s += '\\'; | |
1424 | s += *p; | |
1425 | } | |
1426 | else if (*p >= 0x20 && *p < 0x7f) | |
1427 | s += *p; | |
1428 | else if (*p == '\n') | |
1429 | s += "\\n"; | |
1430 | else if (*p == '\t') | |
1431 | s += "\\t"; | |
1432 | else | |
1433 | { | |
1434 | s += "\\x"; | |
1435 | unsigned char c = *p; | |
1436 | unsigned int dig = c >> 4; | |
1437 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1438 | dig = c & 0xf; | |
1439 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1440 | } | |
1441 | } | |
1442 | s += '"'; | |
1443 | exp->write_string(s); | |
1444 | } | |
1445 | ||
1446 | // Import a string expression. | |
1447 | ||
1448 | Expression* | |
1449 | String_expression::do_import(Import* imp) | |
1450 | { | |
1451 | imp->require_c_string("\""); | |
1452 | std::string val; | |
1453 | while (true) | |
1454 | { | |
1455 | int c = imp->get_char(); | |
1456 | if (c == '"' || c == -1) | |
1457 | break; | |
1458 | if (c != '\\') | |
1459 | val += static_cast<char>(c); | |
1460 | else | |
1461 | { | |
1462 | c = imp->get_char(); | |
1463 | if (c == '\\' || c == '"') | |
1464 | val += static_cast<char>(c); | |
1465 | else if (c == 'n') | |
1466 | val += '\n'; | |
1467 | else if (c == 't') | |
1468 | val += '\t'; | |
1469 | else if (c == 'x') | |
1470 | { | |
1471 | c = imp->get_char(); | |
1472 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1473 | c = imp->get_char(); | |
1474 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1475 | char v = (vh << 4) | vl; | |
1476 | val += v; | |
1477 | } | |
1478 | else | |
1479 | { | |
1480 | error_at(imp->location(), "bad string constant"); | |
1481 | return Expression::make_error(imp->location()); | |
1482 | } | |
1483 | } | |
1484 | } | |
1485 | return Expression::make_string(val, imp->location()); | |
1486 | } | |
1487 | ||
1488 | // Make a string expression. | |
1489 | ||
1490 | Expression* | |
1491 | Expression::make_string(const std::string& val, source_location location) | |
1492 | { | |
1493 | return new String_expression(val, location); | |
1494 | } | |
1495 | ||
1496 | // Make an integer expression. | |
1497 | ||
1498 | class Integer_expression : public Expression | |
1499 | { | |
1500 | public: | |
1501 | Integer_expression(const mpz_t* val, Type* type, source_location location) | |
1502 | : Expression(EXPRESSION_INTEGER, location), | |
1503 | type_(type) | |
1504 | { mpz_init_set(this->val_, *val); } | |
1505 | ||
1506 | static Expression* | |
1507 | do_import(Import*); | |
1508 | ||
1509 | // Return whether VAL fits in the type. | |
1510 | static bool | |
1511 | check_constant(mpz_t val, Type*, source_location); | |
1512 | ||
1513 | // Write VAL to export data. | |
1514 | static void | |
1515 | export_integer(Export* exp, const mpz_t val); | |
1516 | ||
1517 | protected: | |
1518 | bool | |
1519 | do_is_constant() const | |
1520 | { return true; } | |
1521 | ||
1522 | bool | |
1523 | do_integer_constant_value(bool, mpz_t val, Type** ptype) const; | |
1524 | ||
1525 | Type* | |
1526 | do_type(); | |
1527 | ||
1528 | void | |
1529 | do_determine_type(const Type_context* context); | |
1530 | ||
1531 | void | |
1532 | do_check_types(Gogo*); | |
1533 | ||
1534 | tree | |
1535 | do_get_tree(Translate_context*); | |
1536 | ||
1537 | Expression* | |
1538 | do_copy() | |
1539 | { return Expression::make_integer(&this->val_, this->type_, | |
1540 | this->location()); } | |
1541 | ||
1542 | void | |
1543 | do_export(Export*) const; | |
1544 | ||
1545 | private: | |
1546 | // The integer value. | |
1547 | mpz_t val_; | |
1548 | // The type so far. | |
1549 | Type* type_; | |
1550 | }; | |
1551 | ||
1552 | // Return an integer constant value. | |
1553 | ||
1554 | bool | |
1555 | Integer_expression::do_integer_constant_value(bool, mpz_t val, | |
1556 | Type** ptype) const | |
1557 | { | |
1558 | if (this->type_ != NULL) | |
1559 | *ptype = this->type_; | |
1560 | mpz_set(val, this->val_); | |
1561 | return true; | |
1562 | } | |
1563 | ||
1564 | // Return the current type. If we haven't set the type yet, we return | |
1565 | // an abstract integer type. | |
1566 | ||
1567 | Type* | |
1568 | Integer_expression::do_type() | |
1569 | { | |
1570 | if (this->type_ == NULL) | |
1571 | this->type_ = Type::make_abstract_integer_type(); | |
1572 | return this->type_; | |
1573 | } | |
1574 | ||
1575 | // Set the type of the integer value. Here we may switch from an | |
1576 | // abstract type to a real type. | |
1577 | ||
1578 | void | |
1579 | Integer_expression::do_determine_type(const Type_context* context) | |
1580 | { | |
1581 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1582 | ; | |
1583 | else if (context->type != NULL | |
1584 | && (context->type->integer_type() != NULL | |
1585 | || context->type->float_type() != NULL | |
1586 | || context->type->complex_type() != NULL)) | |
1587 | this->type_ = context->type; | |
1588 | else if (!context->may_be_abstract) | |
1589 | this->type_ = Type::lookup_integer_type("int"); | |
1590 | } | |
1591 | ||
1592 | // Return true if the integer VAL fits in the range of the type TYPE. | |
1593 | // Otherwise give an error and return false. TYPE may be NULL. | |
1594 | ||
1595 | bool | |
1596 | Integer_expression::check_constant(mpz_t val, Type* type, | |
1597 | source_location location) | |
1598 | { | |
1599 | if (type == NULL) | |
1600 | return true; | |
1601 | Integer_type* itype = type->integer_type(); | |
1602 | if (itype == NULL || itype->is_abstract()) | |
1603 | return true; | |
1604 | ||
1605 | int bits = mpz_sizeinbase(val, 2); | |
1606 | ||
1607 | if (itype->is_unsigned()) | |
1608 | { | |
1609 | // For an unsigned type we can only accept a nonnegative number, | |
1610 | // and we must be able to represent at least BITS. | |
1611 | if (mpz_sgn(val) >= 0 | |
1612 | && bits <= itype->bits()) | |
1613 | return true; | |
1614 | } | |
1615 | else | |
1616 | { | |
1617 | // For a signed type we need an extra bit to indicate the sign. | |
1618 | // We have to handle the most negative integer specially. | |
1619 | if (bits + 1 <= itype->bits() | |
1620 | || (bits <= itype->bits() | |
1621 | && mpz_sgn(val) < 0 | |
1622 | && (mpz_scan1(val, 0) | |
1623 | == static_cast<unsigned long>(itype->bits() - 1)) | |
1624 | && mpz_scan0(val, itype->bits()) == ULONG_MAX)) | |
1625 | return true; | |
1626 | } | |
1627 | ||
1628 | error_at(location, "integer constant overflow"); | |
1629 | return false; | |
1630 | } | |
1631 | ||
1632 | // Check the type of an integer constant. | |
1633 | ||
1634 | void | |
1635 | Integer_expression::do_check_types(Gogo*) | |
1636 | { | |
1637 | if (this->type_ == NULL) | |
1638 | return; | |
1639 | if (!Integer_expression::check_constant(this->val_, this->type_, | |
1640 | this->location())) | |
1641 | this->set_is_error(); | |
1642 | } | |
1643 | ||
1644 | // Get a tree for an integer constant. | |
1645 | ||
1646 | tree | |
1647 | Integer_expression::do_get_tree(Translate_context* context) | |
1648 | { | |
1649 | Gogo* gogo = context->gogo(); | |
1650 | tree type; | |
1651 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1652 | type = this->type_->get_tree(gogo); | |
1653 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
1654 | { | |
1655 | // We are converting to an abstract floating point type. | |
1656 | type = Type::lookup_float_type("float64")->get_tree(gogo); | |
1657 | } | |
1658 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
1659 | { | |
1660 | // We are converting to an abstract complex type. | |
1661 | type = Type::lookup_complex_type("complex128")->get_tree(gogo); | |
1662 | } | |
1663 | else | |
1664 | { | |
1665 | // If we still have an abstract type here, then this is being | |
1666 | // used in a constant expression which didn't get reduced for | |
1667 | // some reason. Use a type which will fit the value. We use <, | |
1668 | // not <=, because we need an extra bit for the sign bit. | |
1669 | int bits = mpz_sizeinbase(this->val_, 2); | |
1670 | if (bits < INT_TYPE_SIZE) | |
1671 | type = Type::lookup_integer_type("int")->get_tree(gogo); | |
1672 | else if (bits < 64) | |
1673 | type = Type::lookup_integer_type("int64")->get_tree(gogo); | |
1674 | else | |
1675 | type = long_long_integer_type_node; | |
1676 | } | |
1677 | return Expression::integer_constant_tree(this->val_, type); | |
1678 | } | |
1679 | ||
1680 | // Write VAL to export data. | |
1681 | ||
1682 | void | |
1683 | Integer_expression::export_integer(Export* exp, const mpz_t val) | |
1684 | { | |
1685 | char* s = mpz_get_str(NULL, 10, val); | |
1686 | exp->write_c_string(s); | |
1687 | free(s); | |
1688 | } | |
1689 | ||
1690 | // Export an integer in a constant expression. | |
1691 | ||
1692 | void | |
1693 | Integer_expression::do_export(Export* exp) const | |
1694 | { | |
1695 | Integer_expression::export_integer(exp, this->val_); | |
1696 | // A trailing space lets us reliably identify the end of the number. | |
1697 | exp->write_c_string(" "); | |
1698 | } | |
1699 | ||
1700 | // Import an integer, floating point, or complex value. This handles | |
1701 | // all these types because they all start with digits. | |
1702 | ||
1703 | Expression* | |
1704 | Integer_expression::do_import(Import* imp) | |
1705 | { | |
1706 | std::string num = imp->read_identifier(); | |
1707 | imp->require_c_string(" "); | |
1708 | if (!num.empty() && num[num.length() - 1] == 'i') | |
1709 | { | |
1710 | mpfr_t real; | |
1711 | size_t plus_pos = num.find('+', 1); | |
1712 | size_t minus_pos = num.find('-', 1); | |
1713 | size_t pos; | |
1714 | if (plus_pos == std::string::npos) | |
1715 | pos = minus_pos; | |
1716 | else if (minus_pos == std::string::npos) | |
1717 | pos = plus_pos; | |
1718 | else | |
1719 | { | |
1720 | error_at(imp->location(), "bad number in import data: %qs", | |
1721 | num.c_str()); | |
1722 | return Expression::make_error(imp->location()); | |
1723 | } | |
1724 | if (pos == std::string::npos) | |
1725 | mpfr_set_ui(real, 0, GMP_RNDN); | |
1726 | else | |
1727 | { | |
1728 | std::string real_str = num.substr(0, pos); | |
1729 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
1730 | { | |
1731 | error_at(imp->location(), "bad number in import data: %qs", | |
1732 | real_str.c_str()); | |
1733 | return Expression::make_error(imp->location()); | |
1734 | } | |
1735 | } | |
1736 | ||
1737 | std::string imag_str; | |
1738 | if (pos == std::string::npos) | |
1739 | imag_str = num; | |
1740 | else | |
1741 | imag_str = num.substr(pos); | |
1742 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
1743 | mpfr_t imag; | |
1744 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
1745 | { | |
1746 | error_at(imp->location(), "bad number in import data: %qs", | |
1747 | imag_str.c_str()); | |
1748 | return Expression::make_error(imp->location()); | |
1749 | } | |
1750 | Expression* ret = Expression::make_complex(&real, &imag, NULL, | |
1751 | imp->location()); | |
1752 | mpfr_clear(real); | |
1753 | mpfr_clear(imag); | |
1754 | return ret; | |
1755 | } | |
1756 | else if (num.find('.') == std::string::npos | |
1757 | && num.find('E') == std::string::npos) | |
1758 | { | |
1759 | mpz_t val; | |
1760 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
1761 | { | |
1762 | error_at(imp->location(), "bad number in import data: %qs", | |
1763 | num.c_str()); | |
1764 | return Expression::make_error(imp->location()); | |
1765 | } | |
1766 | Expression* ret = Expression::make_integer(&val, NULL, imp->location()); | |
1767 | mpz_clear(val); | |
1768 | return ret; | |
1769 | } | |
1770 | else | |
1771 | { | |
1772 | mpfr_t val; | |
1773 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
1774 | { | |
1775 | error_at(imp->location(), "bad number in import data: %qs", | |
1776 | num.c_str()); | |
1777 | return Expression::make_error(imp->location()); | |
1778 | } | |
1779 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
1780 | mpfr_clear(val); | |
1781 | return ret; | |
1782 | } | |
1783 | } | |
1784 | ||
1785 | // Build a new integer value. | |
1786 | ||
1787 | Expression* | |
1788 | Expression::make_integer(const mpz_t* val, Type* type, | |
1789 | source_location location) | |
1790 | { | |
1791 | return new Integer_expression(val, type, location); | |
1792 | } | |
1793 | ||
1794 | // Floats. | |
1795 | ||
1796 | class Float_expression : public Expression | |
1797 | { | |
1798 | public: | |
1799 | Float_expression(const mpfr_t* val, Type* type, source_location location) | |
1800 | : Expression(EXPRESSION_FLOAT, location), | |
1801 | type_(type) | |
1802 | { | |
1803 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
1804 | } | |
1805 | ||
1806 | // Constrain VAL to fit into TYPE. | |
1807 | static void | |
1808 | constrain_float(mpfr_t val, Type* type); | |
1809 | ||
1810 | // Return whether VAL fits in the type. | |
1811 | static bool | |
1812 | check_constant(mpfr_t val, Type*, source_location); | |
1813 | ||
1814 | // Write VAL to export data. | |
1815 | static void | |
1816 | export_float(Export* exp, const mpfr_t val); | |
1817 | ||
1818 | protected: | |
1819 | bool | |
1820 | do_is_constant() const | |
1821 | { return true; } | |
1822 | ||
1823 | bool | |
1824 | do_float_constant_value(mpfr_t val, Type**) const; | |
1825 | ||
1826 | Type* | |
1827 | do_type(); | |
1828 | ||
1829 | void | |
1830 | do_determine_type(const Type_context*); | |
1831 | ||
1832 | void | |
1833 | do_check_types(Gogo*); | |
1834 | ||
1835 | Expression* | |
1836 | do_copy() | |
1837 | { return Expression::make_float(&this->val_, this->type_, | |
1838 | this->location()); } | |
1839 | ||
1840 | tree | |
1841 | do_get_tree(Translate_context*); | |
1842 | ||
1843 | void | |
1844 | do_export(Export*) const; | |
1845 | ||
1846 | private: | |
1847 | // The floating point value. | |
1848 | mpfr_t val_; | |
1849 | // The type so far. | |
1850 | Type* type_; | |
1851 | }; | |
1852 | ||
1853 | // Constrain VAL to fit into TYPE. | |
1854 | ||
1855 | void | |
1856 | Float_expression::constrain_float(mpfr_t val, Type* type) | |
1857 | { | |
1858 | Float_type* ftype = type->float_type(); | |
1859 | if (ftype != NULL && !ftype->is_abstract()) | |
1860 | { | |
1861 | tree type_tree = ftype->type_tree(); | |
1862 | REAL_VALUE_TYPE rvt; | |
1863 | real_from_mpfr(&rvt, val, type_tree, GMP_RNDN); | |
1864 | real_convert(&rvt, TYPE_MODE(type_tree), &rvt); | |
1865 | mpfr_from_real(val, &rvt, GMP_RNDN); | |
1866 | } | |
1867 | } | |
1868 | ||
1869 | // Return a floating point constant value. | |
1870 | ||
1871 | bool | |
1872 | Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
1873 | { | |
1874 | if (this->type_ != NULL) | |
1875 | *ptype = this->type_; | |
1876 | mpfr_set(val, this->val_, GMP_RNDN); | |
1877 | return true; | |
1878 | } | |
1879 | ||
1880 | // Return the current type. If we haven't set the type yet, we return | |
1881 | // an abstract float type. | |
1882 | ||
1883 | Type* | |
1884 | Float_expression::do_type() | |
1885 | { | |
1886 | if (this->type_ == NULL) | |
1887 | this->type_ = Type::make_abstract_float_type(); | |
1888 | return this->type_; | |
1889 | } | |
1890 | ||
1891 | // Set the type of the float value. Here we may switch from an | |
1892 | // abstract type to a real type. | |
1893 | ||
1894 | void | |
1895 | Float_expression::do_determine_type(const Type_context* context) | |
1896 | { | |
1897 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1898 | ; | |
1899 | else if (context->type != NULL | |
1900 | && (context->type->integer_type() != NULL | |
1901 | || context->type->float_type() != NULL | |
1902 | || context->type->complex_type() != NULL)) | |
1903 | this->type_ = context->type; | |
1904 | else if (!context->may_be_abstract) | |
1905 | this->type_ = Type::lookup_float_type("float"); | |
1906 | } | |
1907 | ||
1908 | // Return true if the floating point value VAL fits in the range of | |
1909 | // the type TYPE. Otherwise give an error and return false. TYPE may | |
1910 | // be NULL. | |
1911 | ||
1912 | bool | |
1913 | Float_expression::check_constant(mpfr_t val, Type* type, | |
1914 | source_location location) | |
1915 | { | |
1916 | if (type == NULL) | |
1917 | return true; | |
1918 | Float_type* ftype = type->float_type(); | |
1919 | if (ftype == NULL || ftype->is_abstract()) | |
1920 | return true; | |
1921 | ||
1922 | // A NaN or Infinity always fits in the range of the type. | |
1923 | if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
1924 | return true; | |
1925 | ||
1926 | mp_exp_t exp = mpfr_get_exp(val); | |
1927 | mp_exp_t max_exp; | |
1928 | switch (ftype->bits()) | |
1929 | { | |
1930 | case 32: | |
1931 | max_exp = 128; | |
1932 | break; | |
1933 | case 64: | |
1934 | max_exp = 1024; | |
1935 | break; | |
1936 | default: | |
1937 | gcc_unreachable(); | |
1938 | } | |
1939 | if (exp > max_exp) | |
1940 | { | |
1941 | error_at(location, "floating point constant overflow"); | |
1942 | return false; | |
1943 | } | |
1944 | return true; | |
1945 | } | |
1946 | ||
1947 | // Check the type of a float value. | |
1948 | ||
1949 | void | |
1950 | Float_expression::do_check_types(Gogo*) | |
1951 | { | |
1952 | if (this->type_ == NULL) | |
1953 | return; | |
1954 | ||
1955 | if (!Float_expression::check_constant(this->val_, this->type_, | |
1956 | this->location())) | |
1957 | this->set_is_error(); | |
1958 | ||
1959 | Integer_type* integer_type = this->type_->integer_type(); | |
1960 | if (integer_type != NULL) | |
1961 | { | |
1962 | if (!mpfr_integer_p(this->val_)) | |
1963 | this->report_error(_("floating point constant truncated to integer")); | |
1964 | else | |
1965 | { | |
1966 | gcc_assert(!integer_type->is_abstract()); | |
1967 | mpz_t ival; | |
1968 | mpz_init(ival); | |
1969 | mpfr_get_z(ival, this->val_, GMP_RNDN); | |
1970 | Integer_expression::check_constant(ival, integer_type, | |
1971 | this->location()); | |
1972 | mpz_clear(ival); | |
1973 | } | |
1974 | } | |
1975 | } | |
1976 | ||
1977 | // Get a tree for a float constant. | |
1978 | ||
1979 | tree | |
1980 | Float_expression::do_get_tree(Translate_context* context) | |
1981 | { | |
1982 | Gogo* gogo = context->gogo(); | |
1983 | tree type; | |
1984 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1985 | type = this->type_->get_tree(gogo); | |
1986 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
1987 | { | |
1988 | // We have an abstract integer type. We just hope for the best. | |
1989 | type = Type::lookup_integer_type("int")->get_tree(gogo); | |
1990 | } | |
1991 | else | |
1992 | { | |
1993 | // If we still have an abstract type here, then this is being | |
1994 | // used in a constant expression which didn't get reduced. We | |
1995 | // just use float64 and hope for the best. | |
1996 | type = Type::lookup_float_type("float64")->get_tree(gogo); | |
1997 | } | |
1998 | return Expression::float_constant_tree(this->val_, type); | |
1999 | } | |
2000 | ||
2001 | // Write a floating point number to export data. | |
2002 | ||
2003 | void | |
2004 | Float_expression::export_float(Export *exp, const mpfr_t val) | |
2005 | { | |
2006 | mp_exp_t exponent; | |
2007 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2008 | if (*s == '-') | |
2009 | exp->write_c_string("-"); | |
2010 | exp->write_c_string("0."); | |
2011 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2012 | mpfr_free_str(s); | |
2013 | char buf[30]; | |
2014 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2015 | exp->write_c_string(buf); | |
2016 | } | |
2017 | ||
2018 | // Export a floating point number in a constant expression. | |
2019 | ||
2020 | void | |
2021 | Float_expression::do_export(Export* exp) const | |
2022 | { | |
2023 | Float_expression::export_float(exp, this->val_); | |
2024 | // A trailing space lets us reliably identify the end of the number. | |
2025 | exp->write_c_string(" "); | |
2026 | } | |
2027 | ||
2028 | // Make a float expression. | |
2029 | ||
2030 | Expression* | |
2031 | Expression::make_float(const mpfr_t* val, Type* type, source_location location) | |
2032 | { | |
2033 | return new Float_expression(val, type, location); | |
2034 | } | |
2035 | ||
2036 | // Complex numbers. | |
2037 | ||
2038 | class Complex_expression : public Expression | |
2039 | { | |
2040 | public: | |
2041 | Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
2042 | source_location location) | |
2043 | : Expression(EXPRESSION_COMPLEX, location), | |
2044 | type_(type) | |
2045 | { | |
2046 | mpfr_init_set(this->real_, *real, GMP_RNDN); | |
2047 | mpfr_init_set(this->imag_, *imag, GMP_RNDN); | |
2048 | } | |
2049 | ||
2050 | // Constrain REAL/IMAG to fit into TYPE. | |
2051 | static void | |
2052 | constrain_complex(mpfr_t real, mpfr_t imag, Type* type); | |
2053 | ||
2054 | // Return whether REAL/IMAG fits in the type. | |
2055 | static bool | |
2056 | check_constant(mpfr_t real, mpfr_t imag, Type*, source_location); | |
2057 | ||
2058 | // Write REAL/IMAG to export data. | |
2059 | static void | |
2060 | export_complex(Export* exp, const mpfr_t real, const mpfr_t val); | |
2061 | ||
2062 | protected: | |
2063 | bool | |
2064 | do_is_constant() const | |
2065 | { return true; } | |
2066 | ||
2067 | bool | |
2068 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const; | |
2069 | ||
2070 | Type* | |
2071 | do_type(); | |
2072 | ||
2073 | void | |
2074 | do_determine_type(const Type_context*); | |
2075 | ||
2076 | void | |
2077 | do_check_types(Gogo*); | |
2078 | ||
2079 | Expression* | |
2080 | do_copy() | |
2081 | { | |
2082 | return Expression::make_complex(&this->real_, &this->imag_, this->type_, | |
2083 | this->location()); | |
2084 | } | |
2085 | ||
2086 | tree | |
2087 | do_get_tree(Translate_context*); | |
2088 | ||
2089 | void | |
2090 | do_export(Export*) const; | |
2091 | ||
2092 | private: | |
2093 | // The real part. | |
2094 | mpfr_t real_; | |
2095 | // The imaginary part; | |
2096 | mpfr_t imag_; | |
2097 | // The type if known. | |
2098 | Type* type_; | |
2099 | }; | |
2100 | ||
2101 | // Constrain REAL/IMAG to fit into TYPE. | |
2102 | ||
2103 | void | |
2104 | Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type) | |
2105 | { | |
2106 | Complex_type* ctype = type->complex_type(); | |
2107 | if (ctype != NULL && !ctype->is_abstract()) | |
2108 | { | |
2109 | tree type_tree = ctype->type_tree(); | |
2110 | ||
2111 | REAL_VALUE_TYPE rvt; | |
2112 | real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN); | |
2113 | real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt); | |
2114 | mpfr_from_real(real, &rvt, GMP_RNDN); | |
2115 | ||
2116 | real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN); | |
2117 | real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt); | |
2118 | mpfr_from_real(imag, &rvt, GMP_RNDN); | |
2119 | } | |
2120 | } | |
2121 | ||
2122 | // Return a complex constant value. | |
2123 | ||
2124 | bool | |
2125 | Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
2126 | Type** ptype) const | |
2127 | { | |
2128 | if (this->type_ != NULL) | |
2129 | *ptype = this->type_; | |
2130 | mpfr_set(real, this->real_, GMP_RNDN); | |
2131 | mpfr_set(imag, this->imag_, GMP_RNDN); | |
2132 | return true; | |
2133 | } | |
2134 | ||
2135 | // Return the current type. If we haven't set the type yet, we return | |
2136 | // an abstract complex type. | |
2137 | ||
2138 | Type* | |
2139 | Complex_expression::do_type() | |
2140 | { | |
2141 | if (this->type_ == NULL) | |
2142 | this->type_ = Type::make_abstract_complex_type(); | |
2143 | return this->type_; | |
2144 | } | |
2145 | ||
2146 | // Set the type of the complex value. Here we may switch from an | |
2147 | // abstract type to a real type. | |
2148 | ||
2149 | void | |
2150 | Complex_expression::do_determine_type(const Type_context* context) | |
2151 | { | |
2152 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2153 | ; | |
2154 | else if (context->type != NULL | |
2155 | && context->type->complex_type() != NULL) | |
2156 | this->type_ = context->type; | |
2157 | else if (!context->may_be_abstract) | |
2158 | this->type_ = Type::lookup_complex_type("complex"); | |
2159 | } | |
2160 | ||
2161 | // Return true if the complex value REAL/IMAG fits in the range of the | |
2162 | // type TYPE. Otherwise give an error and return false. TYPE may be | |
2163 | // NULL. | |
2164 | ||
2165 | bool | |
2166 | Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type, | |
2167 | source_location location) | |
2168 | { | |
2169 | if (type == NULL) | |
2170 | return true; | |
2171 | Complex_type* ctype = type->complex_type(); | |
2172 | if (ctype == NULL || ctype->is_abstract()) | |
2173 | return true; | |
2174 | ||
2175 | mp_exp_t max_exp; | |
2176 | switch (ctype->bits()) | |
2177 | { | |
2178 | case 64: | |
2179 | max_exp = 128; | |
2180 | break; | |
2181 | case 128: | |
2182 | max_exp = 1024; | |
2183 | break; | |
2184 | default: | |
2185 | gcc_unreachable(); | |
2186 | } | |
2187 | ||
2188 | // A NaN or Infinity always fits in the range of the type. | |
2189 | if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real)) | |
2190 | { | |
2191 | if (mpfr_get_exp(real) > max_exp) | |
2192 | { | |
2193 | error_at(location, "complex real part constant overflow"); | |
2194 | return false; | |
2195 | } | |
2196 | } | |
2197 | ||
2198 | if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag)) | |
2199 | { | |
2200 | if (mpfr_get_exp(imag) > max_exp) | |
2201 | { | |
2202 | error_at(location, "complex imaginary part constant overflow"); | |
2203 | return false; | |
2204 | } | |
2205 | } | |
2206 | ||
2207 | return true; | |
2208 | } | |
2209 | ||
2210 | // Check the type of a complex value. | |
2211 | ||
2212 | void | |
2213 | Complex_expression::do_check_types(Gogo*) | |
2214 | { | |
2215 | if (this->type_ == NULL) | |
2216 | return; | |
2217 | ||
2218 | if (!Complex_expression::check_constant(this->real_, this->imag_, | |
2219 | this->type_, this->location())) | |
2220 | this->set_is_error(); | |
2221 | } | |
2222 | ||
2223 | // Get a tree for a complex constant. | |
2224 | ||
2225 | tree | |
2226 | Complex_expression::do_get_tree(Translate_context* context) | |
2227 | { | |
2228 | Gogo* gogo = context->gogo(); | |
2229 | tree type; | |
2230 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2231 | type = this->type_->get_tree(gogo); | |
2232 | else | |
2233 | { | |
2234 | // If we still have an abstract type here, this this is being | |
2235 | // used in a constant expression which didn't get reduced. We | |
2236 | // just use complex128 and hope for the best. | |
2237 | type = Type::lookup_complex_type("complex128")->get_tree(gogo); | |
2238 | } | |
2239 | return Expression::complex_constant_tree(this->real_, this->imag_, type); | |
2240 | } | |
2241 | ||
2242 | // Write REAL/IMAG to export data. | |
2243 | ||
2244 | void | |
2245 | Complex_expression::export_complex(Export* exp, const mpfr_t real, | |
2246 | const mpfr_t imag) | |
2247 | { | |
2248 | if (!mpfr_zero_p(real)) | |
2249 | { | |
2250 | Float_expression::export_float(exp, real); | |
2251 | if (mpfr_sgn(imag) > 0) | |
2252 | exp->write_c_string("+"); | |
2253 | } | |
2254 | Float_expression::export_float(exp, imag); | |
2255 | exp->write_c_string("i"); | |
2256 | } | |
2257 | ||
2258 | // Export a complex number in a constant expression. | |
2259 | ||
2260 | void | |
2261 | Complex_expression::do_export(Export* exp) const | |
2262 | { | |
2263 | Complex_expression::export_complex(exp, this->real_, this->imag_); | |
2264 | // A trailing space lets us reliably identify the end of the number. | |
2265 | exp->write_c_string(" "); | |
2266 | } | |
2267 | ||
2268 | // Make a complex expression. | |
2269 | ||
2270 | Expression* | |
2271 | Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
2272 | source_location location) | |
2273 | { | |
2274 | return new Complex_expression(real, imag, type, location); | |
2275 | } | |
2276 | ||
2277 | // A reference to a const in an expression. | |
2278 | ||
2279 | class Const_expression : public Expression | |
2280 | { | |
2281 | public: | |
2282 | Const_expression(Named_object* constant, source_location location) | |
2283 | : Expression(EXPRESSION_CONST_REFERENCE, location), | |
2284 | constant_(constant), type_(NULL) | |
2285 | { } | |
2286 | ||
2287 | const std::string& | |
2288 | name() const | |
2289 | { return this->constant_->name(); } | |
2290 | ||
2291 | protected: | |
2292 | Expression* | |
2293 | do_lower(Gogo*, Named_object*, int); | |
2294 | ||
2295 | bool | |
2296 | do_is_constant() const | |
2297 | { return true; } | |
2298 | ||
2299 | bool | |
2300 | do_integer_constant_value(bool, mpz_t val, Type**) const; | |
2301 | ||
2302 | bool | |
2303 | do_float_constant_value(mpfr_t val, Type**) const; | |
2304 | ||
2305 | bool | |
2306 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const; | |
2307 | ||
2308 | bool | |
2309 | do_string_constant_value(std::string* val) const | |
2310 | { return this->constant_->const_value()->expr()->string_constant_value(val); } | |
2311 | ||
2312 | Type* | |
2313 | do_type(); | |
2314 | ||
2315 | // The type of a const is set by the declaration, not the use. | |
2316 | void | |
2317 | do_determine_type(const Type_context*); | |
2318 | ||
2319 | void | |
2320 | do_check_types(Gogo*); | |
2321 | ||
2322 | Expression* | |
2323 | do_copy() | |
2324 | { return this; } | |
2325 | ||
2326 | tree | |
2327 | do_get_tree(Translate_context* context); | |
2328 | ||
2329 | // When exporting a reference to a const as part of a const | |
2330 | // expression, we export the value. We ignore the fact that it has | |
2331 | // a name. | |
2332 | void | |
2333 | do_export(Export* exp) const | |
2334 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2335 | ||
2336 | private: | |
2337 | // The constant. | |
2338 | Named_object* constant_; | |
2339 | // The type of this reference. This is used if the constant has an | |
2340 | // abstract type. | |
2341 | Type* type_; | |
2342 | }; | |
2343 | ||
2344 | // Lower a constant expression. This is where we convert the | |
2345 | // predeclared constant iota into an integer value. | |
2346 | ||
2347 | Expression* | |
2348 | Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value) | |
2349 | { | |
2350 | if (this->constant_->const_value()->expr()->classification() | |
2351 | == EXPRESSION_IOTA) | |
2352 | { | |
2353 | if (iota_value == -1) | |
2354 | { | |
2355 | error_at(this->location(), | |
2356 | "iota is only defined in const declarations"); | |
2357 | iota_value = 0; | |
2358 | } | |
2359 | mpz_t val; | |
2360 | mpz_init_set_ui(val, static_cast<unsigned long>(iota_value)); | |
2361 | Expression* ret = Expression::make_integer(&val, NULL, | |
2362 | this->location()); | |
2363 | mpz_clear(val); | |
2364 | return ret; | |
2365 | } | |
2366 | ||
2367 | // Make sure that the constant itself has been lowered. | |
2368 | gogo->lower_constant(this->constant_); | |
2369 | ||
2370 | return this; | |
2371 | } | |
2372 | ||
2373 | // Return an integer constant value. | |
2374 | ||
2375 | bool | |
2376 | Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
2377 | Type** ptype) const | |
2378 | { | |
2379 | Type* ctype; | |
2380 | if (this->type_ != NULL) | |
2381 | ctype = this->type_; | |
2382 | else | |
2383 | ctype = this->constant_->const_value()->type(); | |
2384 | if (ctype != NULL && ctype->integer_type() == NULL) | |
2385 | return false; | |
2386 | ||
2387 | Expression* e = this->constant_->const_value()->expr(); | |
2388 | Type* t; | |
2389 | bool r = e->integer_constant_value(iota_is_constant, val, &t); | |
2390 | ||
2391 | if (r | |
2392 | && ctype != NULL | |
2393 | && !Integer_expression::check_constant(val, ctype, this->location())) | |
2394 | return false; | |
2395 | ||
2396 | *ptype = ctype != NULL ? ctype : t; | |
2397 | return r; | |
2398 | } | |
2399 | ||
2400 | // Return a floating point constant value. | |
2401 | ||
2402 | bool | |
2403 | Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
2404 | { | |
2405 | Type* ctype; | |
2406 | if (this->type_ != NULL) | |
2407 | ctype = this->type_; | |
2408 | else | |
2409 | ctype = this->constant_->const_value()->type(); | |
2410 | if (ctype != NULL && ctype->float_type() == NULL) | |
2411 | return false; | |
2412 | ||
2413 | Type* t; | |
2414 | bool r = this->constant_->const_value()->expr()->float_constant_value(val, | |
2415 | &t); | |
2416 | if (r && ctype != NULL) | |
2417 | { | |
2418 | if (!Float_expression::check_constant(val, ctype, this->location())) | |
2419 | return false; | |
2420 | Float_expression::constrain_float(val, ctype); | |
2421 | } | |
2422 | *ptype = ctype != NULL ? ctype : t; | |
2423 | return r; | |
2424 | } | |
2425 | ||
2426 | // Return a complex constant value. | |
2427 | ||
2428 | bool | |
2429 | Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
2430 | Type **ptype) const | |
2431 | { | |
2432 | Type* ctype; | |
2433 | if (this->type_ != NULL) | |
2434 | ctype = this->type_; | |
2435 | else | |
2436 | ctype = this->constant_->const_value()->type(); | |
2437 | if (ctype != NULL && ctype->complex_type() == NULL) | |
2438 | return false; | |
2439 | ||
2440 | Type *t; | |
2441 | bool r = this->constant_->const_value()->expr()->complex_constant_value(real, | |
2442 | imag, | |
2443 | &t); | |
2444 | if (r && ctype != NULL) | |
2445 | { | |
2446 | if (!Complex_expression::check_constant(real, imag, ctype, | |
2447 | this->location())) | |
2448 | return false; | |
2449 | Complex_expression::constrain_complex(real, imag, ctype); | |
2450 | } | |
2451 | *ptype = ctype != NULL ? ctype : t; | |
2452 | return r; | |
2453 | } | |
2454 | ||
2455 | // Return the type of the const reference. | |
2456 | ||
2457 | Type* | |
2458 | Const_expression::do_type() | |
2459 | { | |
2460 | if (this->type_ != NULL) | |
2461 | return this->type_; | |
2462 | Named_constant* nc = this->constant_->const_value(); | |
2463 | Type* ret = nc->type(); | |
2464 | if (ret != NULL) | |
2465 | return ret; | |
2466 | // During parsing, a named constant may have a NULL type, but we | |
2467 | // must not return a NULL type here. | |
2468 | return nc->expr()->type(); | |
2469 | } | |
2470 | ||
2471 | // Set the type of the const reference. | |
2472 | ||
2473 | void | |
2474 | Const_expression::do_determine_type(const Type_context* context) | |
2475 | { | |
2476 | Type* ctype = this->constant_->const_value()->type(); | |
2477 | Type* cetype = (ctype != NULL | |
2478 | ? ctype | |
2479 | : this->constant_->const_value()->expr()->type()); | |
2480 | if (ctype != NULL && !ctype->is_abstract()) | |
2481 | ; | |
2482 | else if (context->type != NULL | |
2483 | && (context->type->integer_type() != NULL | |
2484 | || context->type->float_type() != NULL | |
2485 | || context->type->complex_type() != NULL) | |
2486 | && (cetype->integer_type() != NULL | |
2487 | || cetype->float_type() != NULL | |
2488 | || cetype->complex_type() != NULL)) | |
2489 | this->type_ = context->type; | |
2490 | else if (context->type != NULL | |
2491 | && context->type->is_string_type() | |
2492 | && cetype->is_string_type()) | |
2493 | this->type_ = context->type; | |
2494 | else if (context->type != NULL | |
2495 | && context->type->is_boolean_type() | |
2496 | && cetype->is_boolean_type()) | |
2497 | this->type_ = context->type; | |
2498 | else if (!context->may_be_abstract) | |
2499 | { | |
2500 | if (cetype->is_abstract()) | |
2501 | cetype = cetype->make_non_abstract_type(); | |
2502 | this->type_ = cetype; | |
2503 | } | |
2504 | } | |
2505 | ||
2506 | // Check types of a const reference. | |
2507 | ||
2508 | void | |
2509 | Const_expression::do_check_types(Gogo*) | |
2510 | { | |
2511 | if (this->type_ == NULL || this->type_->is_abstract()) | |
2512 | return; | |
2513 | ||
2514 | // Check for integer overflow. | |
2515 | if (this->type_->integer_type() != NULL) | |
2516 | { | |
2517 | mpz_t ival; | |
2518 | mpz_init(ival); | |
2519 | Type* dummy; | |
2520 | if (!this->integer_constant_value(true, ival, &dummy)) | |
2521 | { | |
2522 | mpfr_t fval; | |
2523 | mpfr_init(fval); | |
2524 | Expression* cexpr = this->constant_->const_value()->expr(); | |
2525 | if (cexpr->float_constant_value(fval, &dummy)) | |
2526 | { | |
2527 | if (!mpfr_integer_p(fval)) | |
2528 | this->report_error(_("floating point constant " | |
2529 | "truncated to integer")); | |
2530 | else | |
2531 | { | |
2532 | mpfr_get_z(ival, fval, GMP_RNDN); | |
2533 | Integer_expression::check_constant(ival, this->type_, | |
2534 | this->location()); | |
2535 | } | |
2536 | } | |
2537 | mpfr_clear(fval); | |
2538 | } | |
2539 | mpz_clear(ival); | |
2540 | } | |
2541 | } | |
2542 | ||
2543 | // Return a tree for the const reference. | |
2544 | ||
2545 | tree | |
2546 | Const_expression::do_get_tree(Translate_context* context) | |
2547 | { | |
2548 | Gogo* gogo = context->gogo(); | |
2549 | tree type_tree; | |
2550 | if (this->type_ == NULL) | |
2551 | type_tree = NULL_TREE; | |
2552 | else | |
2553 | { | |
2554 | type_tree = this->type_->get_tree(gogo); | |
2555 | if (type_tree == error_mark_node) | |
2556 | return error_mark_node; | |
2557 | } | |
2558 | ||
2559 | // If the type has been set for this expression, but the underlying | |
2560 | // object is an abstract int or float, we try to get the abstract | |
2561 | // value. Otherwise we may lose something in the conversion. | |
2562 | if (this->type_ != NULL | |
2563 | && this->constant_->const_value()->type()->is_abstract()) | |
2564 | { | |
2565 | Expression* expr = this->constant_->const_value()->expr(); | |
2566 | mpz_t ival; | |
2567 | mpz_init(ival); | |
2568 | Type* t; | |
2569 | if (expr->integer_constant_value(true, ival, &t)) | |
2570 | { | |
2571 | tree ret = Expression::integer_constant_tree(ival, type_tree); | |
2572 | mpz_clear(ival); | |
2573 | return ret; | |
2574 | } | |
2575 | mpz_clear(ival); | |
2576 | ||
2577 | mpfr_t fval; | |
2578 | mpfr_init(fval); | |
2579 | if (expr->float_constant_value(fval, &t)) | |
2580 | { | |
2581 | tree ret = Expression::float_constant_tree(fval, type_tree); | |
2582 | mpfr_clear(fval); | |
2583 | return ret; | |
2584 | } | |
2585 | ||
2586 | mpfr_t imag; | |
2587 | mpfr_init(imag); | |
2588 | if (expr->complex_constant_value(fval, imag, &t)) | |
2589 | { | |
2590 | tree ret = Expression::complex_constant_tree(fval, imag, type_tree); | |
2591 | mpfr_clear(fval); | |
2592 | mpfr_clear(imag); | |
2593 | return ret; | |
2594 | } | |
2595 | mpfr_clear(imag); | |
2596 | mpfr_clear(fval); | |
2597 | } | |
2598 | ||
2599 | tree const_tree = this->constant_->get_tree(gogo, context->function()); | |
2600 | if (this->type_ == NULL | |
2601 | || const_tree == error_mark_node | |
2602 | || TREE_TYPE(const_tree) == error_mark_node) | |
2603 | return const_tree; | |
2604 | ||
2605 | tree ret; | |
2606 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree))) | |
2607 | ret = fold_convert(type_tree, const_tree); | |
2608 | else if (TREE_CODE(type_tree) == INTEGER_TYPE) | |
2609 | ret = fold(convert_to_integer(type_tree, const_tree)); | |
2610 | else if (TREE_CODE(type_tree) == REAL_TYPE) | |
2611 | ret = fold(convert_to_real(type_tree, const_tree)); | |
2612 | else if (TREE_CODE(type_tree) == COMPLEX_TYPE) | |
2613 | ret = fold(convert_to_complex(type_tree, const_tree)); | |
2614 | else | |
2615 | gcc_unreachable(); | |
2616 | return ret; | |
2617 | } | |
2618 | ||
2619 | // Make a reference to a constant in an expression. | |
2620 | ||
2621 | Expression* | |
2622 | Expression::make_const_reference(Named_object* constant, | |
2623 | source_location location) | |
2624 | { | |
2625 | return new Const_expression(constant, location); | |
2626 | } | |
2627 | ||
2628 | // The nil value. | |
2629 | ||
2630 | class Nil_expression : public Expression | |
2631 | { | |
2632 | public: | |
2633 | Nil_expression(source_location location) | |
2634 | : Expression(EXPRESSION_NIL, location) | |
2635 | { } | |
2636 | ||
2637 | static Expression* | |
2638 | do_import(Import*); | |
2639 | ||
2640 | protected: | |
2641 | bool | |
2642 | do_is_constant() const | |
2643 | { return true; } | |
2644 | ||
2645 | Type* | |
2646 | do_type() | |
2647 | { return Type::make_nil_type(); } | |
2648 | ||
2649 | void | |
2650 | do_determine_type(const Type_context*) | |
2651 | { } | |
2652 | ||
2653 | Expression* | |
2654 | do_copy() | |
2655 | { return this; } | |
2656 | ||
2657 | tree | |
2658 | do_get_tree(Translate_context*) | |
2659 | { return null_pointer_node; } | |
2660 | ||
2661 | void | |
2662 | do_export(Export* exp) const | |
2663 | { exp->write_c_string("nil"); } | |
2664 | }; | |
2665 | ||
2666 | // Import a nil expression. | |
2667 | ||
2668 | Expression* | |
2669 | Nil_expression::do_import(Import* imp) | |
2670 | { | |
2671 | imp->require_c_string("nil"); | |
2672 | return Expression::make_nil(imp->location()); | |
2673 | } | |
2674 | ||
2675 | // Make a nil expression. | |
2676 | ||
2677 | Expression* | |
2678 | Expression::make_nil(source_location location) | |
2679 | { | |
2680 | return new Nil_expression(location); | |
2681 | } | |
2682 | ||
2683 | // The value of the predeclared constant iota. This is little more | |
2684 | // than a marker. This will be lowered to an integer in | |
2685 | // Const_expression::do_lower, which is where we know the value that | |
2686 | // it should have. | |
2687 | ||
2688 | class Iota_expression : public Parser_expression | |
2689 | { | |
2690 | public: | |
2691 | Iota_expression(source_location location) | |
2692 | : Parser_expression(EXPRESSION_IOTA, location) | |
2693 | { } | |
2694 | ||
2695 | protected: | |
2696 | Expression* | |
2697 | do_lower(Gogo*, Named_object*, int) | |
2698 | { gcc_unreachable(); } | |
2699 | ||
2700 | // There should only ever be one of these. | |
2701 | Expression* | |
2702 | do_copy() | |
2703 | { gcc_unreachable(); } | |
2704 | }; | |
2705 | ||
2706 | // Make an iota expression. This is only called for one case: the | |
2707 | // value of the predeclared constant iota. | |
2708 | ||
2709 | Expression* | |
2710 | Expression::make_iota() | |
2711 | { | |
2712 | static Iota_expression iota_expression(UNKNOWN_LOCATION); | |
2713 | return &iota_expression; | |
2714 | } | |
2715 | ||
2716 | // A type conversion expression. | |
2717 | ||
2718 | class Type_conversion_expression : public Expression | |
2719 | { | |
2720 | public: | |
2721 | Type_conversion_expression(Type* type, Expression* expr, | |
2722 | source_location location) | |
2723 | : Expression(EXPRESSION_CONVERSION, location), | |
2724 | type_(type), expr_(expr), may_convert_function_types_(false) | |
2725 | { } | |
2726 | ||
2727 | // Return the type to which we are converting. | |
2728 | Type* | |
2729 | type() const | |
2730 | { return this->type_; } | |
2731 | ||
2732 | // Return the expression which we are converting. | |
2733 | Expression* | |
2734 | expr() const | |
2735 | { return this->expr_; } | |
2736 | ||
2737 | // Permit converting from one function type to another. This is | |
2738 | // used internally for method expressions. | |
2739 | void | |
2740 | set_may_convert_function_types() | |
2741 | { | |
2742 | this->may_convert_function_types_ = true; | |
2743 | } | |
2744 | ||
2745 | // Import a type conversion expression. | |
2746 | static Expression* | |
2747 | do_import(Import*); | |
2748 | ||
2749 | protected: | |
2750 | int | |
2751 | do_traverse(Traverse* traverse); | |
2752 | ||
2753 | Expression* | |
2754 | do_lower(Gogo*, Named_object*, int); | |
2755 | ||
2756 | bool | |
2757 | do_is_constant() const | |
2758 | { return this->expr_->is_constant(); } | |
2759 | ||
2760 | bool | |
2761 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
2762 | ||
2763 | bool | |
2764 | do_float_constant_value(mpfr_t, Type**) const; | |
2765 | ||
2766 | bool | |
2767 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
2768 | ||
2769 | bool | |
2770 | do_string_constant_value(std::string*) const; | |
2771 | ||
2772 | Type* | |
2773 | do_type() | |
2774 | { return this->type_; } | |
2775 | ||
2776 | void | |
2777 | do_determine_type(const Type_context*) | |
2778 | { | |
2779 | Type_context subcontext(this->type_, false); | |
2780 | this->expr_->determine_type(&subcontext); | |
2781 | } | |
2782 | ||
2783 | void | |
2784 | do_check_types(Gogo*); | |
2785 | ||
2786 | Expression* | |
2787 | do_copy() | |
2788 | { | |
2789 | return new Type_conversion_expression(this->type_, this->expr_->copy(), | |
2790 | this->location()); | |
2791 | } | |
2792 | ||
2793 | tree | |
2794 | do_get_tree(Translate_context* context); | |
2795 | ||
2796 | void | |
2797 | do_export(Export*) const; | |
2798 | ||
2799 | private: | |
2800 | // The type to convert to. | |
2801 | Type* type_; | |
2802 | // The expression to convert. | |
2803 | Expression* expr_; | |
2804 | // True if this is permitted to convert function types. This is | |
2805 | // used internally for method expressions. | |
2806 | bool may_convert_function_types_; | |
2807 | }; | |
2808 | ||
2809 | // Traversal. | |
2810 | ||
2811 | int | |
2812 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
2813 | { | |
2814 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
2815 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
2816 | return TRAVERSE_EXIT; | |
2817 | return TRAVERSE_CONTINUE; | |
2818 | } | |
2819 | ||
2820 | // Convert to a constant at lowering time. | |
2821 | ||
2822 | Expression* | |
2823 | Type_conversion_expression::do_lower(Gogo*, Named_object*, int) | |
2824 | { | |
2825 | Type* type = this->type_; | |
2826 | Expression* val = this->expr_; | |
2827 | source_location location = this->location(); | |
2828 | ||
2829 | if (type->integer_type() != NULL) | |
2830 | { | |
2831 | mpz_t ival; | |
2832 | mpz_init(ival); | |
2833 | Type* dummy; | |
2834 | if (val->integer_constant_value(false, ival, &dummy)) | |
2835 | { | |
2836 | if (!Integer_expression::check_constant(ival, type, location)) | |
2837 | mpz_set_ui(ival, 0); | |
2838 | Expression* ret = Expression::make_integer(&ival, type, location); | |
2839 | mpz_clear(ival); | |
2840 | return ret; | |
2841 | } | |
2842 | ||
2843 | mpfr_t fval; | |
2844 | mpfr_init(fval); | |
2845 | if (val->float_constant_value(fval, &dummy)) | |
2846 | { | |
2847 | if (!mpfr_integer_p(fval)) | |
2848 | { | |
2849 | error_at(location, | |
2850 | "floating point constant truncated to integer"); | |
2851 | return Expression::make_error(location); | |
2852 | } | |
2853 | mpfr_get_z(ival, fval, GMP_RNDN); | |
2854 | if (!Integer_expression::check_constant(ival, type, location)) | |
2855 | mpz_set_ui(ival, 0); | |
2856 | Expression* ret = Expression::make_integer(&ival, type, location); | |
2857 | mpfr_clear(fval); | |
2858 | mpz_clear(ival); | |
2859 | return ret; | |
2860 | } | |
2861 | mpfr_clear(fval); | |
2862 | mpz_clear(ival); | |
2863 | } | |
2864 | ||
2865 | if (type->float_type() != NULL) | |
2866 | { | |
2867 | mpfr_t fval; | |
2868 | mpfr_init(fval); | |
2869 | Type* dummy; | |
2870 | if (val->float_constant_value(fval, &dummy)) | |
2871 | { | |
2872 | if (!Float_expression::check_constant(fval, type, location)) | |
2873 | mpfr_set_ui(fval, 0, GMP_RNDN); | |
2874 | Float_expression::constrain_float(fval, type); | |
2875 | Expression *ret = Expression::make_float(&fval, type, location); | |
2876 | mpfr_clear(fval); | |
2877 | return ret; | |
2878 | } | |
2879 | mpfr_clear(fval); | |
2880 | } | |
2881 | ||
2882 | if (type->complex_type() != NULL) | |
2883 | { | |
2884 | mpfr_t real; | |
2885 | mpfr_t imag; | |
2886 | mpfr_init(real); | |
2887 | mpfr_init(imag); | |
2888 | Type* dummy; | |
2889 | if (val->complex_constant_value(real, imag, &dummy)) | |
2890 | { | |
2891 | if (!Complex_expression::check_constant(real, imag, type, location)) | |
2892 | { | |
2893 | mpfr_set_ui(real, 0, GMP_RNDN); | |
2894 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
2895 | } | |
2896 | Complex_expression::constrain_complex(real, imag, type); | |
2897 | Expression* ret = Expression::make_complex(&real, &imag, type, | |
2898 | location); | |
2899 | mpfr_clear(real); | |
2900 | mpfr_clear(imag); | |
2901 | return ret; | |
2902 | } | |
2903 | mpfr_clear(real); | |
2904 | mpfr_clear(imag); | |
2905 | } | |
2906 | ||
2907 | if (type->is_open_array_type() && type->named_type() == NULL) | |
2908 | { | |
2909 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
2910 | bool is_byte = element_type == Type::lookup_integer_type("uint8"); | |
2911 | bool is_int = element_type == Type::lookup_integer_type("int"); | |
2912 | if (is_byte || is_int) | |
2913 | { | |
2914 | std::string s; | |
2915 | if (val->string_constant_value(&s)) | |
2916 | { | |
2917 | Expression_list* vals = new Expression_list(); | |
2918 | if (is_byte) | |
2919 | { | |
2920 | for (std::string::const_iterator p = s.begin(); | |
2921 | p != s.end(); | |
2922 | p++) | |
2923 | { | |
2924 | mpz_t val; | |
2925 | mpz_init_set_ui(val, static_cast<unsigned char>(*p)); | |
2926 | Expression* v = Expression::make_integer(&val, | |
2927 | element_type, | |
2928 | location); | |
2929 | vals->push_back(v); | |
2930 | mpz_clear(val); | |
2931 | } | |
2932 | } | |
2933 | else | |
2934 | { | |
2935 | const char *p = s.data(); | |
2936 | const char *pend = s.data() + s.length(); | |
2937 | while (p < pend) | |
2938 | { | |
2939 | unsigned int c; | |
2940 | int adv = Lex::fetch_char(p, &c); | |
2941 | if (adv == 0) | |
2942 | { | |
2943 | warning_at(this->location(), 0, | |
2944 | "invalid UTF-8 encoding"); | |
2945 | adv = 1; | |
2946 | } | |
2947 | p += adv; | |
2948 | mpz_t val; | |
2949 | mpz_init_set_ui(val, c); | |
2950 | Expression* v = Expression::make_integer(&val, | |
2951 | element_type, | |
2952 | location); | |
2953 | vals->push_back(v); | |
2954 | mpz_clear(val); | |
2955 | } | |
2956 | } | |
2957 | ||
2958 | return Expression::make_slice_composite_literal(type, vals, | |
2959 | location); | |
2960 | } | |
2961 | } | |
2962 | } | |
2963 | ||
2964 | return this; | |
2965 | } | |
2966 | ||
2967 | // Return the constant integer value if there is one. | |
2968 | ||
2969 | bool | |
2970 | Type_conversion_expression::do_integer_constant_value(bool iota_is_constant, | |
2971 | mpz_t val, | |
2972 | Type** ptype) const | |
2973 | { | |
2974 | if (this->type_->integer_type() == NULL) | |
2975 | return false; | |
2976 | ||
2977 | mpz_t ival; | |
2978 | mpz_init(ival); | |
2979 | Type* dummy; | |
2980 | if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy)) | |
2981 | { | |
2982 | if (!Integer_expression::check_constant(ival, this->type_, | |
2983 | this->location())) | |
2984 | { | |
2985 | mpz_clear(ival); | |
2986 | return false; | |
2987 | } | |
2988 | mpz_set(val, ival); | |
2989 | mpz_clear(ival); | |
2990 | *ptype = this->type_; | |
2991 | return true; | |
2992 | } | |
2993 | mpz_clear(ival); | |
2994 | ||
2995 | mpfr_t fval; | |
2996 | mpfr_init(fval); | |
2997 | if (this->expr_->float_constant_value(fval, &dummy)) | |
2998 | { | |
2999 | mpfr_get_z(val, fval, GMP_RNDN); | |
3000 | mpfr_clear(fval); | |
3001 | if (!Integer_expression::check_constant(val, this->type_, | |
3002 | this->location())) | |
3003 | return false; | |
3004 | *ptype = this->type_; | |
3005 | return true; | |
3006 | } | |
3007 | mpfr_clear(fval); | |
3008 | ||
3009 | return false; | |
3010 | } | |
3011 | ||
3012 | // Return the constant floating point value if there is one. | |
3013 | ||
3014 | bool | |
3015 | Type_conversion_expression::do_float_constant_value(mpfr_t val, | |
3016 | Type** ptype) const | |
3017 | { | |
3018 | if (this->type_->float_type() == NULL) | |
3019 | return false; | |
3020 | ||
3021 | mpfr_t fval; | |
3022 | mpfr_init(fval); | |
3023 | Type* dummy; | |
3024 | if (this->expr_->float_constant_value(fval, &dummy)) | |
3025 | { | |
3026 | if (!Float_expression::check_constant(fval, this->type_, | |
3027 | this->location())) | |
3028 | { | |
3029 | mpfr_clear(fval); | |
3030 | return false; | |
3031 | } | |
3032 | mpfr_set(val, fval, GMP_RNDN); | |
3033 | mpfr_clear(fval); | |
3034 | Float_expression::constrain_float(val, this->type_); | |
3035 | *ptype = this->type_; | |
3036 | return true; | |
3037 | } | |
3038 | mpfr_clear(fval); | |
3039 | ||
3040 | return false; | |
3041 | } | |
3042 | ||
3043 | // Return the constant complex value if there is one. | |
3044 | ||
3045 | bool | |
3046 | Type_conversion_expression::do_complex_constant_value(mpfr_t real, | |
3047 | mpfr_t imag, | |
3048 | Type **ptype) const | |
3049 | { | |
3050 | if (this->type_->complex_type() == NULL) | |
3051 | return false; | |
3052 | ||
3053 | mpfr_t rval; | |
3054 | mpfr_t ival; | |
3055 | mpfr_init(rval); | |
3056 | mpfr_init(ival); | |
3057 | Type* dummy; | |
3058 | if (this->expr_->complex_constant_value(rval, ival, &dummy)) | |
3059 | { | |
3060 | if (!Complex_expression::check_constant(rval, ival, this->type_, | |
3061 | this->location())) | |
3062 | { | |
3063 | mpfr_clear(rval); | |
3064 | mpfr_clear(ival); | |
3065 | return false; | |
3066 | } | |
3067 | mpfr_set(real, rval, GMP_RNDN); | |
3068 | mpfr_set(imag, ival, GMP_RNDN); | |
3069 | mpfr_clear(rval); | |
3070 | mpfr_clear(ival); | |
3071 | Complex_expression::constrain_complex(real, imag, this->type_); | |
3072 | *ptype = this->type_; | |
3073 | return true; | |
3074 | } | |
3075 | mpfr_clear(rval); | |
3076 | mpfr_clear(ival); | |
3077 | ||
3078 | return false; | |
3079 | } | |
3080 | ||
3081 | // Return the constant string value if there is one. | |
3082 | ||
3083 | bool | |
3084 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3085 | { | |
3086 | if (this->type_->is_string_type() | |
3087 | && this->expr_->type()->integer_type() != NULL) | |
3088 | { | |
3089 | mpz_t ival; | |
3090 | mpz_init(ival); | |
3091 | Type* dummy; | |
3092 | if (this->expr_->integer_constant_value(false, ival, &dummy)) | |
3093 | { | |
3094 | unsigned long ulval = mpz_get_ui(ival); | |
3095 | if (mpz_cmp_ui(ival, ulval) == 0) | |
3096 | { | |
3097 | Lex::append_char(ulval, true, val, this->location()); | |
3098 | mpz_clear(ival); | |
3099 | return true; | |
3100 | } | |
3101 | } | |
3102 | mpz_clear(ival); | |
3103 | } | |
3104 | ||
3105 | // FIXME: Could handle conversion from const []int here. | |
3106 | ||
3107 | return false; | |
3108 | } | |
3109 | ||
3110 | // Check that types are convertible. | |
3111 | ||
3112 | void | |
3113 | Type_conversion_expression::do_check_types(Gogo*) | |
3114 | { | |
3115 | Type* type = this->type_; | |
3116 | Type* expr_type = this->expr_->type(); | |
3117 | std::string reason; | |
3118 | ||
3119 | if (this->may_convert_function_types_ | |
3120 | && type->function_type() != NULL | |
3121 | && expr_type->function_type() != NULL) | |
3122 | return; | |
3123 | ||
3124 | if (Type::are_convertible(type, expr_type, &reason)) | |
3125 | return; | |
3126 | ||
3127 | error_at(this->location(), "%s", reason.c_str()); | |
3128 | this->set_is_error(); | |
3129 | } | |
3130 | ||
3131 | // Get a tree for a type conversion. | |
3132 | ||
3133 | tree | |
3134 | Type_conversion_expression::do_get_tree(Translate_context* context) | |
3135 | { | |
3136 | Gogo* gogo = context->gogo(); | |
3137 | tree type_tree = this->type_->get_tree(gogo); | |
3138 | tree expr_tree = this->expr_->get_tree(context); | |
3139 | ||
3140 | if (type_tree == error_mark_node | |
3141 | || expr_tree == error_mark_node | |
3142 | || TREE_TYPE(expr_tree) == error_mark_node) | |
3143 | return error_mark_node; | |
3144 | ||
3145 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree))) | |
3146 | return fold_convert(type_tree, expr_tree); | |
3147 | ||
3148 | Type* type = this->type_; | |
3149 | Type* expr_type = this->expr_->type(); | |
3150 | tree ret; | |
3151 | if (type->interface_type() != NULL || expr_type->interface_type() != NULL) | |
3152 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3153 | expr_tree, this->location()); | |
3154 | else if (type->integer_type() != NULL) | |
3155 | { | |
3156 | if (expr_type->integer_type() != NULL | |
3157 | || expr_type->float_type() != NULL | |
3158 | || expr_type->is_unsafe_pointer_type()) | |
3159 | ret = fold(convert_to_integer(type_tree, expr_tree)); | |
3160 | else | |
3161 | gcc_unreachable(); | |
3162 | } | |
3163 | else if (type->float_type() != NULL) | |
3164 | { | |
3165 | if (expr_type->integer_type() != NULL | |
3166 | || expr_type->float_type() != NULL) | |
3167 | ret = fold(convert_to_real(type_tree, expr_tree)); | |
3168 | else | |
3169 | gcc_unreachable(); | |
3170 | } | |
3171 | else if (type->complex_type() != NULL) | |
3172 | { | |
3173 | if (expr_type->complex_type() != NULL) | |
3174 | ret = fold(convert_to_complex(type_tree, expr_tree)); | |
3175 | else | |
3176 | gcc_unreachable(); | |
3177 | } | |
3178 | else if (type->is_string_type() | |
3179 | && expr_type->integer_type() != NULL) | |
3180 | { | |
3181 | expr_tree = fold_convert(integer_type_node, expr_tree); | |
3182 | if (host_integerp(expr_tree, 0)) | |
3183 | { | |
3184 | HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0); | |
3185 | std::string s; | |
3186 | Lex::append_char(intval, true, &s, this->location()); | |
3187 | Expression* se = Expression::make_string(s, this->location()); | |
3188 | return se->get_tree(context); | |
3189 | } | |
3190 | ||
3191 | static tree int_to_string_fndecl; | |
3192 | ret = Gogo::call_builtin(&int_to_string_fndecl, | |
3193 | this->location(), | |
3194 | "__go_int_to_string", | |
3195 | 1, | |
3196 | type_tree, | |
3197 | integer_type_node, | |
3198 | fold_convert(integer_type_node, expr_tree)); | |
3199 | } | |
3200 | else if (type->is_string_type() | |
3201 | && (expr_type->array_type() != NULL | |
3202 | || (expr_type->points_to() != NULL | |
3203 | && expr_type->points_to()->array_type() != NULL))) | |
3204 | { | |
3205 | Type* t = expr_type; | |
3206 | if (t->points_to() != NULL) | |
3207 | { | |
3208 | t = t->points_to(); | |
3209 | expr_tree = build_fold_indirect_ref(expr_tree); | |
3210 | } | |
3211 | if (!DECL_P(expr_tree)) | |
3212 | expr_tree = save_expr(expr_tree); | |
3213 | Array_type* a = t->array_type(); | |
3214 | Type* e = a->element_type()->forwarded(); | |
3215 | gcc_assert(e->integer_type() != NULL); | |
3216 | tree valptr = fold_convert(const_ptr_type_node, | |
3217 | a->value_pointer_tree(gogo, expr_tree)); | |
3218 | tree len = a->length_tree(gogo, expr_tree); | |
3219 | len = fold_convert_loc(this->location(), size_type_node, len); | |
3220 | if (e->integer_type()->is_unsigned() | |
3221 | && e->integer_type()->bits() == 8) | |
3222 | { | |
3223 | static tree byte_array_to_string_fndecl; | |
3224 | ret = Gogo::call_builtin(&byte_array_to_string_fndecl, | |
3225 | this->location(), | |
3226 | "__go_byte_array_to_string", | |
3227 | 2, | |
3228 | type_tree, | |
3229 | const_ptr_type_node, | |
3230 | valptr, | |
3231 | size_type_node, | |
3232 | len); | |
3233 | } | |
3234 | else | |
3235 | { | |
3236 | gcc_assert(e == Type::lookup_integer_type("int")); | |
3237 | static tree int_array_to_string_fndecl; | |
3238 | ret = Gogo::call_builtin(&int_array_to_string_fndecl, | |
3239 | this->location(), | |
3240 | "__go_int_array_to_string", | |
3241 | 2, | |
3242 | type_tree, | |
3243 | const_ptr_type_node, | |
3244 | valptr, | |
3245 | size_type_node, | |
3246 | len); | |
3247 | } | |
3248 | } | |
3249 | else if (type->is_open_array_type() && expr_type->is_string_type()) | |
3250 | { | |
3251 | Type* e = type->array_type()->element_type()->forwarded(); | |
3252 | gcc_assert(e->integer_type() != NULL); | |
3253 | if (e->integer_type()->is_unsigned() | |
3254 | && e->integer_type()->bits() == 8) | |
3255 | { | |
3256 | static tree string_to_byte_array_fndecl; | |
3257 | ret = Gogo::call_builtin(&string_to_byte_array_fndecl, | |
3258 | this->location(), | |
3259 | "__go_string_to_byte_array", | |
3260 | 1, | |
3261 | type_tree, | |
3262 | TREE_TYPE(expr_tree), | |
3263 | expr_tree); | |
3264 | } | |
3265 | else | |
3266 | { | |
3267 | gcc_assert(e == Type::lookup_integer_type("int")); | |
3268 | static tree string_to_int_array_fndecl; | |
3269 | ret = Gogo::call_builtin(&string_to_int_array_fndecl, | |
3270 | this->location(), | |
3271 | "__go_string_to_int_array", | |
3272 | 1, | |
3273 | type_tree, | |
3274 | TREE_TYPE(expr_tree), | |
3275 | expr_tree); | |
3276 | } | |
3277 | } | |
3278 | else if ((type->is_unsafe_pointer_type() | |
3279 | && expr_type->points_to() != NULL) | |
3280 | || (expr_type->is_unsafe_pointer_type() | |
3281 | && type->points_to() != NULL)) | |
3282 | ret = fold_convert(type_tree, expr_tree); | |
3283 | else if (type->is_unsafe_pointer_type() | |
3284 | && expr_type->integer_type() != NULL) | |
3285 | ret = convert_to_pointer(type_tree, expr_tree); | |
3286 | else if (this->may_convert_function_types_ | |
3287 | && type->function_type() != NULL | |
3288 | && expr_type->function_type() != NULL) | |
3289 | ret = fold_convert_loc(this->location(), type_tree, expr_tree); | |
3290 | else | |
3291 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3292 | expr_tree, this->location()); | |
3293 | ||
3294 | return ret; | |
3295 | } | |
3296 | ||
3297 | // Output a type conversion in a constant expression. | |
3298 | ||
3299 | void | |
3300 | Type_conversion_expression::do_export(Export* exp) const | |
3301 | { | |
3302 | exp->write_c_string("convert("); | |
3303 | exp->write_type(this->type_); | |
3304 | exp->write_c_string(", "); | |
3305 | this->expr_->export_expression(exp); | |
3306 | exp->write_c_string(")"); | |
3307 | } | |
3308 | ||
3309 | // Import a type conversion or a struct construction. | |
3310 | ||
3311 | Expression* | |
3312 | Type_conversion_expression::do_import(Import* imp) | |
3313 | { | |
3314 | imp->require_c_string("convert("); | |
3315 | Type* type = imp->read_type(); | |
3316 | imp->require_c_string(", "); | |
3317 | Expression* val = Expression::import_expression(imp); | |
3318 | imp->require_c_string(")"); | |
3319 | return Expression::make_cast(type, val, imp->location()); | |
3320 | } | |
3321 | ||
3322 | // Make a type cast expression. | |
3323 | ||
3324 | Expression* | |
3325 | Expression::make_cast(Type* type, Expression* val, source_location location) | |
3326 | { | |
3327 | if (type->is_error_type() || val->is_error_expression()) | |
3328 | return Expression::make_error(location); | |
3329 | return new Type_conversion_expression(type, val, location); | |
3330 | } | |
3331 | ||
3332 | // Unary expressions. | |
3333 | ||
3334 | class Unary_expression : public Expression | |
3335 | { | |
3336 | public: | |
3337 | Unary_expression(Operator op, Expression* expr, source_location location) | |
3338 | : Expression(EXPRESSION_UNARY, location), | |
3339 | op_(op), escapes_(true), expr_(expr) | |
3340 | { } | |
3341 | ||
3342 | // Return the operator. | |
3343 | Operator | |
3344 | op() const | |
3345 | { return this->op_; } | |
3346 | ||
3347 | // Return the operand. | |
3348 | Expression* | |
3349 | operand() const | |
3350 | { return this->expr_; } | |
3351 | ||
3352 | // Record that an address expression does not escape. | |
3353 | void | |
3354 | set_does_not_escape() | |
3355 | { | |
3356 | gcc_assert(this->op_ == OPERATOR_AND); | |
3357 | this->escapes_ = false; | |
3358 | } | |
3359 | ||
3360 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3361 | // could be done, false if not. | |
3362 | static bool | |
3363 | eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val, | |
3364 | source_location); | |
3365 | ||
3366 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3367 | // could be done, false if not. | |
3368 | static bool | |
3369 | eval_float(Operator op, mpfr_t uval, mpfr_t val); | |
3370 | ||
3371 | // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return | |
3372 | // true if this could be done, false if not. | |
3373 | static bool | |
3374 | eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real, | |
3375 | mpfr_t imag); | |
3376 | ||
3377 | static Expression* | |
3378 | do_import(Import*); | |
3379 | ||
3380 | protected: | |
3381 | int | |
3382 | do_traverse(Traverse* traverse) | |
3383 | { return Expression::traverse(&this->expr_, traverse); } | |
3384 | ||
3385 | Expression* | |
3386 | do_lower(Gogo*, Named_object*, int); | |
3387 | ||
3388 | bool | |
3389 | do_is_constant() const; | |
3390 | ||
3391 | bool | |
3392 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
3393 | ||
3394 | bool | |
3395 | do_float_constant_value(mpfr_t, Type**) const; | |
3396 | ||
3397 | bool | |
3398 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
3399 | ||
3400 | Type* | |
3401 | do_type(); | |
3402 | ||
3403 | void | |
3404 | do_determine_type(const Type_context*); | |
3405 | ||
3406 | void | |
3407 | do_check_types(Gogo*); | |
3408 | ||
3409 | Expression* | |
3410 | do_copy() | |
3411 | { | |
3412 | return Expression::make_unary(this->op_, this->expr_->copy(), | |
3413 | this->location()); | |
3414 | } | |
3415 | ||
3416 | bool | |
3417 | do_is_addressable() const | |
3418 | { return this->op_ == OPERATOR_MULT; } | |
3419 | ||
3420 | tree | |
3421 | do_get_tree(Translate_context*); | |
3422 | ||
3423 | void | |
3424 | do_export(Export*) const; | |
3425 | ||
3426 | private: | |
3427 | // The unary operator to apply. | |
3428 | Operator op_; | |
3429 | // Normally true. False if this is an address expression which does | |
3430 | // not escape the current function. | |
3431 | bool escapes_; | |
3432 | // The operand. | |
3433 | Expression* expr_; | |
3434 | }; | |
3435 | ||
3436 | // If we are taking the address of a composite literal, and the | |
3437 | // contents are not constant, then we want to make a heap composite | |
3438 | // instead. | |
3439 | ||
3440 | Expression* | |
3441 | Unary_expression::do_lower(Gogo*, Named_object*, int) | |
3442 | { | |
3443 | source_location loc = this->location(); | |
3444 | Operator op = this->op_; | |
3445 | Expression* expr = this->expr_; | |
3446 | ||
3447 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3448 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3449 | ||
3450 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3451 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3452 | // analysis here? This case is found in math/unsafe.go and is | |
3453 | // therefore worth special casing. | |
3454 | if (op == OPERATOR_MULT) | |
3455 | { | |
3456 | Expression* e = expr; | |
3457 | while (e->classification() == EXPRESSION_CONVERSION) | |
3458 | { | |
3459 | Type_conversion_expression* te | |
3460 | = static_cast<Type_conversion_expression*>(e); | |
3461 | e = te->expr(); | |
3462 | } | |
3463 | ||
3464 | if (e->classification() == EXPRESSION_UNARY) | |
3465 | { | |
3466 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3467 | if (ue->op_ == OPERATOR_AND) | |
3468 | { | |
3469 | if (e == expr) | |
3470 | { | |
3471 | // *&x == x. | |
3472 | return ue->expr_; | |
3473 | } | |
3474 | ue->set_does_not_escape(); | |
3475 | } | |
3476 | } | |
3477 | } | |
3478 | ||
3479 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS | |
3480 | || op == OPERATOR_NOT || op == OPERATOR_XOR) | |
3481 | { | |
3482 | Expression* ret = NULL; | |
3483 | ||
3484 | mpz_t eval; | |
3485 | mpz_init(eval); | |
3486 | Type* etype; | |
3487 | if (expr->integer_constant_value(false, eval, &etype)) | |
3488 | { | |
3489 | mpz_t val; | |
3490 | mpz_init(val); | |
3491 | if (Unary_expression::eval_integer(op, etype, eval, val, loc)) | |
3492 | ret = Expression::make_integer(&val, etype, loc); | |
3493 | mpz_clear(val); | |
3494 | } | |
3495 | mpz_clear(eval); | |
3496 | if (ret != NULL) | |
3497 | return ret; | |
3498 | ||
3499 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS) | |
3500 | { | |
3501 | mpfr_t fval; | |
3502 | mpfr_init(fval); | |
3503 | Type* ftype; | |
3504 | if (expr->float_constant_value(fval, &ftype)) | |
3505 | { | |
3506 | mpfr_t val; | |
3507 | mpfr_init(val); | |
3508 | if (Unary_expression::eval_float(op, fval, val)) | |
3509 | ret = Expression::make_float(&val, ftype, loc); | |
3510 | mpfr_clear(val); | |
3511 | } | |
3512 | if (ret != NULL) | |
3513 | { | |
3514 | mpfr_clear(fval); | |
3515 | return ret; | |
3516 | } | |
3517 | ||
3518 | mpfr_t ival; | |
3519 | mpfr_init(ival); | |
3520 | if (expr->complex_constant_value(fval, ival, &ftype)) | |
3521 | { | |
3522 | mpfr_t real; | |
3523 | mpfr_t imag; | |
3524 | mpfr_init(real); | |
3525 | mpfr_init(imag); | |
3526 | if (Unary_expression::eval_complex(op, fval, ival, real, imag)) | |
3527 | ret = Expression::make_complex(&real, &imag, ftype, loc); | |
3528 | mpfr_clear(real); | |
3529 | mpfr_clear(imag); | |
3530 | } | |
3531 | mpfr_clear(ival); | |
3532 | mpfr_clear(fval); | |
3533 | if (ret != NULL) | |
3534 | return ret; | |
3535 | } | |
3536 | } | |
3537 | ||
3538 | return this; | |
3539 | } | |
3540 | ||
3541 | // Return whether a unary expression is a constant. | |
3542 | ||
3543 | bool | |
3544 | Unary_expression::do_is_constant() const | |
3545 | { | |
3546 | if (this->op_ == OPERATOR_MULT) | |
3547 | { | |
3548 | // Indirecting through a pointer is only constant if the object | |
3549 | // to which the expression points is constant, but we currently | |
3550 | // have no way to determine that. | |
3551 | return false; | |
3552 | } | |
3553 | else if (this->op_ == OPERATOR_AND) | |
3554 | { | |
3555 | // Taking the address of a variable is constant if it is a | |
3556 | // global variable, not constant otherwise. In other cases | |
3557 | // taking the address is probably not a constant. | |
3558 | Var_expression* ve = this->expr_->var_expression(); | |
3559 | if (ve != NULL) | |
3560 | { | |
3561 | Named_object* no = ve->named_object(); | |
3562 | return no->is_variable() && no->var_value()->is_global(); | |
3563 | } | |
3564 | return false; | |
3565 | } | |
3566 | else | |
3567 | return this->expr_->is_constant(); | |
3568 | } | |
3569 | ||
3570 | // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of | |
3571 | // UVAL, if known; it may be NULL. Return true if this could be done, | |
3572 | // false if not. | |
3573 | ||
3574 | bool | |
3575 | Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val, | |
3576 | source_location location) | |
3577 | { | |
3578 | switch (op) | |
3579 | { | |
3580 | case OPERATOR_PLUS: | |
3581 | mpz_set(val, uval); | |
3582 | return true; | |
3583 | case OPERATOR_MINUS: | |
3584 | mpz_neg(val, uval); | |
3585 | return Integer_expression::check_constant(val, utype, location); | |
3586 | case OPERATOR_NOT: | |
3587 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); | |
3588 | return true; | |
3589 | case OPERATOR_XOR: | |
3590 | if (utype == NULL | |
3591 | || utype->integer_type() == NULL | |
3592 | || utype->integer_type()->is_abstract()) | |
3593 | mpz_com(val, uval); | |
3594 | else | |
3595 | { | |
3596 | // The number of HOST_WIDE_INTs that it takes to represent | |
3597 | // UVAL. | |
3598 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3599 | + HOST_BITS_PER_WIDE_INT | |
3600 | - 1) | |
3601 | / HOST_BITS_PER_WIDE_INT); | |
3602 | ||
3603 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; | |
3604 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3605 | ||
3606 | size_t ecount; | |
3607 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
3608 | gcc_assert(ecount <= count); | |
3609 | ||
3610 | // Trim down to the number of words required by the type. | |
3611 | size_t obits = utype->integer_type()->bits(); | |
3612 | if (!utype->integer_type()->is_unsigned()) | |
3613 | ++obits; | |
3614 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
3615 | / HOST_BITS_PER_WIDE_INT); | |
3616 | gcc_assert(ocount <= ocount); | |
3617 | ||
3618 | for (size_t i = 0; i < ocount; ++i) | |
3619 | phwi[i] = ~phwi[i]; | |
3620 | ||
3621 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
3622 | if (clearbits != 0) | |
3623 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
3624 | >> clearbits); | |
3625 | ||
3626 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
3627 | ||
3628 | delete[] phwi; | |
3629 | } | |
3630 | return Integer_expression::check_constant(val, utype, location); | |
3631 | case OPERATOR_AND: | |
3632 | case OPERATOR_MULT: | |
3633 | return false; | |
3634 | default: | |
3635 | gcc_unreachable(); | |
3636 | } | |
3637 | } | |
3638 | ||
3639 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3640 | // could be done, false if not. | |
3641 | ||
3642 | bool | |
3643 | Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val) | |
3644 | { | |
3645 | switch (op) | |
3646 | { | |
3647 | case OPERATOR_PLUS: | |
3648 | mpfr_set(val, uval, GMP_RNDN); | |
3649 | return true; | |
3650 | case OPERATOR_MINUS: | |
3651 | mpfr_neg(val, uval, GMP_RNDN); | |
3652 | return true; | |
3653 | case OPERATOR_NOT: | |
3654 | case OPERATOR_XOR: | |
3655 | case OPERATOR_AND: | |
3656 | case OPERATOR_MULT: | |
3657 | return false; | |
3658 | default: | |
3659 | gcc_unreachable(); | |
3660 | } | |
3661 | } | |
3662 | ||
3663 | // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true | |
3664 | // if this could be done, false if not. | |
3665 | ||
3666 | bool | |
3667 | Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival, | |
3668 | mpfr_t real, mpfr_t imag) | |
3669 | { | |
3670 | switch (op) | |
3671 | { | |
3672 | case OPERATOR_PLUS: | |
3673 | mpfr_set(real, rval, GMP_RNDN); | |
3674 | mpfr_set(imag, ival, GMP_RNDN); | |
3675 | return true; | |
3676 | case OPERATOR_MINUS: | |
3677 | mpfr_neg(real, rval, GMP_RNDN); | |
3678 | mpfr_neg(imag, ival, GMP_RNDN); | |
3679 | return true; | |
3680 | case OPERATOR_NOT: | |
3681 | case OPERATOR_XOR: | |
3682 | case OPERATOR_AND: | |
3683 | case OPERATOR_MULT: | |
3684 | return false; | |
3685 | default: | |
3686 | gcc_unreachable(); | |
3687 | } | |
3688 | } | |
3689 | ||
3690 | // Return the integral constant value of a unary expression, if it has one. | |
3691 | ||
3692 | bool | |
3693 | Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
3694 | Type** ptype) const | |
3695 | { | |
3696 | mpz_t uval; | |
3697 | mpz_init(uval); | |
3698 | bool ret; | |
3699 | if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype)) | |
3700 | ret = false; | |
3701 | else | |
3702 | ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val, | |
3703 | this->location()); | |
3704 | mpz_clear(uval); | |
3705 | return ret; | |
3706 | } | |
3707 | ||
3708 | // Return the floating point constant value of a unary expression, if | |
3709 | // it has one. | |
3710 | ||
3711 | bool | |
3712 | Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
3713 | { | |
3714 | mpfr_t uval; | |
3715 | mpfr_init(uval); | |
3716 | bool ret; | |
3717 | if (!this->expr_->float_constant_value(uval, ptype)) | |
3718 | ret = false; | |
3719 | else | |
3720 | ret = Unary_expression::eval_float(this->op_, uval, val); | |
3721 | mpfr_clear(uval); | |
3722 | return ret; | |
3723 | } | |
3724 | ||
3725 | // Return the complex constant value of a unary expression, if it has | |
3726 | // one. | |
3727 | ||
3728 | bool | |
3729 | Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
3730 | Type** ptype) const | |
3731 | { | |
3732 | mpfr_t rval; | |
3733 | mpfr_t ival; | |
3734 | mpfr_init(rval); | |
3735 | mpfr_init(ival); | |
3736 | bool ret; | |
3737 | if (!this->expr_->complex_constant_value(rval, ival, ptype)) | |
3738 | ret = false; | |
3739 | else | |
3740 | ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag); | |
3741 | mpfr_clear(rval); | |
3742 | mpfr_clear(ival); | |
3743 | return ret; | |
3744 | } | |
3745 | ||
3746 | // Return the type of a unary expression. | |
3747 | ||
3748 | Type* | |
3749 | Unary_expression::do_type() | |
3750 | { | |
3751 | switch (this->op_) | |
3752 | { | |
3753 | case OPERATOR_PLUS: | |
3754 | case OPERATOR_MINUS: | |
3755 | case OPERATOR_NOT: | |
3756 | case OPERATOR_XOR: | |
3757 | return this->expr_->type(); | |
3758 | ||
3759 | case OPERATOR_AND: | |
3760 | return Type::make_pointer_type(this->expr_->type()); | |
3761 | ||
3762 | case OPERATOR_MULT: | |
3763 | { | |
3764 | Type* subtype = this->expr_->type(); | |
3765 | Type* points_to = subtype->points_to(); | |
3766 | if (points_to == NULL) | |
3767 | return Type::make_error_type(); | |
3768 | return points_to; | |
3769 | } | |
3770 | ||
3771 | default: | |
3772 | gcc_unreachable(); | |
3773 | } | |
3774 | } | |
3775 | ||
3776 | // Determine abstract types for a unary expression. | |
3777 | ||
3778 | void | |
3779 | Unary_expression::do_determine_type(const Type_context* context) | |
3780 | { | |
3781 | switch (this->op_) | |
3782 | { | |
3783 | case OPERATOR_PLUS: | |
3784 | case OPERATOR_MINUS: | |
3785 | case OPERATOR_NOT: | |
3786 | case OPERATOR_XOR: | |
3787 | this->expr_->determine_type(context); | |
3788 | break; | |
3789 | ||
3790 | case OPERATOR_AND: | |
3791 | // Taking the address of something. | |
3792 | { | |
3793 | Type* subtype = (context->type == NULL | |
3794 | ? NULL | |
3795 | : context->type->points_to()); | |
3796 | Type_context subcontext(subtype, false); | |
3797 | this->expr_->determine_type(&subcontext); | |
3798 | } | |
3799 | break; | |
3800 | ||
3801 | case OPERATOR_MULT: | |
3802 | // Indirecting through a pointer. | |
3803 | { | |
3804 | Type* subtype = (context->type == NULL | |
3805 | ? NULL | |
3806 | : Type::make_pointer_type(context->type)); | |
3807 | Type_context subcontext(subtype, false); | |
3808 | this->expr_->determine_type(&subcontext); | |
3809 | } | |
3810 | break; | |
3811 | ||
3812 | default: | |
3813 | gcc_unreachable(); | |
3814 | } | |
3815 | } | |
3816 | ||
3817 | // Check types for a unary expression. | |
3818 | ||
3819 | void | |
3820 | Unary_expression::do_check_types(Gogo*) | |
3821 | { | |
3822 | switch (this->op_) | |
3823 | { | |
3824 | case OPERATOR_PLUS: | |
3825 | case OPERATOR_MINUS: | |
3826 | { | |
3827 | Type* type = this->expr_->type(); | |
3828 | if (type->integer_type() == NULL | |
3829 | && type->float_type() == NULL | |
3830 | && type->complex_type() == NULL | |
3831 | && !type->is_error_type()) | |
3832 | this->report_error(_("expected numeric type")); | |
3833 | } | |
3834 | break; | |
3835 | ||
3836 | case OPERATOR_NOT: | |
3837 | case OPERATOR_XOR: | |
3838 | { | |
3839 | Type* type = this->expr_->type(); | |
3840 | if (type->integer_type() == NULL | |
3841 | && !type->is_boolean_type() | |
3842 | && !type->is_error_type()) | |
3843 | this->report_error(_("expected integer or boolean type")); | |
3844 | } | |
3845 | break; | |
3846 | ||
3847 | case OPERATOR_AND: | |
3848 | if (!this->expr_->is_addressable()) | |
3849 | this->report_error(_("invalid operand for unary %<&%>")); | |
3850 | else | |
3851 | this->expr_->address_taken(this->escapes_); | |
3852 | break; | |
3853 | ||
3854 | case OPERATOR_MULT: | |
3855 | // Indirecting through a pointer. | |
3856 | { | |
3857 | Type* type = this->expr_->type(); | |
3858 | if (type->points_to() == NULL | |
3859 | && !type->is_error_type()) | |
3860 | this->report_error(_("expected pointer")); | |
3861 | } | |
3862 | break; | |
3863 | ||
3864 | default: | |
3865 | gcc_unreachable(); | |
3866 | } | |
3867 | } | |
3868 | ||
3869 | // Get a tree for a unary expression. | |
3870 | ||
3871 | tree | |
3872 | Unary_expression::do_get_tree(Translate_context* context) | |
3873 | { | |
3874 | tree expr = this->expr_->get_tree(context); | |
3875 | if (expr == error_mark_node) | |
3876 | return error_mark_node; | |
3877 | ||
3878 | source_location loc = this->location(); | |
3879 | switch (this->op_) | |
3880 | { | |
3881 | case OPERATOR_PLUS: | |
3882 | return expr; | |
3883 | ||
3884 | case OPERATOR_MINUS: | |
3885 | { | |
3886 | tree type = TREE_TYPE(expr); | |
3887 | tree compute_type = excess_precision_type(type); | |
3888 | if (compute_type != NULL_TREE) | |
3889 | expr = ::convert(compute_type, expr); | |
3890 | tree ret = fold_build1_loc(loc, NEGATE_EXPR, | |
3891 | (compute_type != NULL_TREE | |
3892 | ? compute_type | |
3893 | : type), | |
3894 | expr); | |
3895 | if (compute_type != NULL_TREE) | |
3896 | ret = ::convert(type, ret); | |
3897 | return ret; | |
3898 | } | |
3899 | ||
3900 | case OPERATOR_NOT: | |
3901 | if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE) | |
3902 | return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr); | |
3903 | else | |
3904 | return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr, | |
3905 | build_int_cst(TREE_TYPE(expr), 0)); | |
3906 | ||
3907 | case OPERATOR_XOR: | |
3908 | return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr); | |
3909 | ||
3910 | case OPERATOR_AND: | |
3911 | // We should not see a non-constant constructor here; cases | |
3912 | // where we would see one should have been moved onto the heap | |
3913 | // at parse time. Taking the address of a nonconstant | |
3914 | // constructor will not do what the programmer expects. | |
3915 | gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr)); | |
3916 | gcc_assert(TREE_CODE(expr) != ADDR_EXPR); | |
3917 | ||
3918 | // Build a decl for a constant constructor. | |
3919 | if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr)) | |
3920 | { | |
3921 | tree decl = build_decl(this->location(), VAR_DECL, | |
3922 | create_tmp_var_name("C"), TREE_TYPE(expr)); | |
3923 | DECL_EXTERNAL(decl) = 0; | |
3924 | TREE_PUBLIC(decl) = 0; | |
3925 | TREE_READONLY(decl) = 1; | |
3926 | TREE_CONSTANT(decl) = 1; | |
3927 | TREE_STATIC(decl) = 1; | |
3928 | TREE_ADDRESSABLE(decl) = 1; | |
3929 | DECL_ARTIFICIAL(decl) = 1; | |
3930 | DECL_INITIAL(decl) = expr; | |
3931 | rest_of_decl_compilation(decl, 1, 0); | |
3932 | expr = decl; | |
3933 | } | |
3934 | ||
3935 | return build_fold_addr_expr_loc(loc, expr); | |
3936 | ||
3937 | case OPERATOR_MULT: | |
3938 | { | |
3939 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr))); | |
3940 | ||
3941 | // If we are dereferencing the pointer to a large struct, we | |
3942 | // need to check for nil. We don't bother to check for small | |
3943 | // structs because we expect the system to crash on a nil | |
3944 | // pointer dereference. | |
3945 | HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr))); | |
3946 | if (s == -1 || s >= 4096) | |
3947 | { | |
3948 | if (!DECL_P(expr)) | |
3949 | expr = save_expr(expr); | |
3950 | tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node, | |
3951 | expr, | |
3952 | fold_convert(TREE_TYPE(expr), | |
3953 | null_pointer_node)); | |
3954 | tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, | |
3955 | loc); | |
3956 | expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr), | |
3957 | build3(COND_EXPR, void_type_node, | |
3958 | compare, crash, NULL_TREE), | |
3959 | expr); | |
3960 | } | |
3961 | ||
3962 | // If the type of EXPR is a recursive pointer type, then we | |
3963 | // need to insert a cast before indirecting. | |
3964 | if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node) | |
3965 | { | |
3966 | Type* pt = this->expr_->type()->points_to(); | |
3967 | tree ind = pt->get_tree(context->gogo()); | |
3968 | expr = fold_convert_loc(loc, build_pointer_type(ind), expr); | |
3969 | } | |
3970 | ||
3971 | return build_fold_indirect_ref_loc(loc, expr); | |
3972 | } | |
3973 | ||
3974 | default: | |
3975 | gcc_unreachable(); | |
3976 | } | |
3977 | } | |
3978 | ||
3979 | // Export a unary expression. | |
3980 | ||
3981 | void | |
3982 | Unary_expression::do_export(Export* exp) const | |
3983 | { | |
3984 | switch (this->op_) | |
3985 | { | |
3986 | case OPERATOR_PLUS: | |
3987 | exp->write_c_string("+ "); | |
3988 | break; | |
3989 | case OPERATOR_MINUS: | |
3990 | exp->write_c_string("- "); | |
3991 | break; | |
3992 | case OPERATOR_NOT: | |
3993 | exp->write_c_string("! "); | |
3994 | break; | |
3995 | case OPERATOR_XOR: | |
3996 | exp->write_c_string("^ "); | |
3997 | break; | |
3998 | case OPERATOR_AND: | |
3999 | case OPERATOR_MULT: | |
4000 | default: | |
4001 | gcc_unreachable(); | |
4002 | } | |
4003 | this->expr_->export_expression(exp); | |
4004 | } | |
4005 | ||
4006 | // Import a unary expression. | |
4007 | ||
4008 | Expression* | |
4009 | Unary_expression::do_import(Import* imp) | |
4010 | { | |
4011 | Operator op; | |
4012 | switch (imp->get_char()) | |
4013 | { | |
4014 | case '+': | |
4015 | op = OPERATOR_PLUS; | |
4016 | break; | |
4017 | case '-': | |
4018 | op = OPERATOR_MINUS; | |
4019 | break; | |
4020 | case '!': | |
4021 | op = OPERATOR_NOT; | |
4022 | break; | |
4023 | case '^': | |
4024 | op = OPERATOR_XOR; | |
4025 | break; | |
4026 | default: | |
4027 | gcc_unreachable(); | |
4028 | } | |
4029 | imp->require_c_string(" "); | |
4030 | Expression* expr = Expression::import_expression(imp); | |
4031 | return Expression::make_unary(op, expr, imp->location()); | |
4032 | } | |
4033 | ||
4034 | // Make a unary expression. | |
4035 | ||
4036 | Expression* | |
4037 | Expression::make_unary(Operator op, Expression* expr, source_location location) | |
4038 | { | |
4039 | return new Unary_expression(op, expr, location); | |
4040 | } | |
4041 | ||
4042 | // If this is an indirection through a pointer, return the expression | |
4043 | // being pointed through. Otherwise return this. | |
4044 | ||
4045 | Expression* | |
4046 | Expression::deref() | |
4047 | { | |
4048 | if (this->classification_ == EXPRESSION_UNARY) | |
4049 | { | |
4050 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4051 | if (ue->op() == OPERATOR_MULT) | |
4052 | return ue->operand(); | |
4053 | } | |
4054 | return this; | |
4055 | } | |
4056 | ||
4057 | // Class Binary_expression. | |
4058 | ||
4059 | // Traversal. | |
4060 | ||
4061 | int | |
4062 | Binary_expression::do_traverse(Traverse* traverse) | |
4063 | { | |
4064 | int t = Expression::traverse(&this->left_, traverse); | |
4065 | if (t == TRAVERSE_EXIT) | |
4066 | return TRAVERSE_EXIT; | |
4067 | return Expression::traverse(&this->right_, traverse); | |
4068 | } | |
4069 | ||
4070 | // Compare integer constants according to OP. | |
4071 | ||
4072 | bool | |
4073 | Binary_expression::compare_integer(Operator op, mpz_t left_val, | |
4074 | mpz_t right_val) | |
4075 | { | |
4076 | int i = mpz_cmp(left_val, right_val); | |
4077 | switch (op) | |
4078 | { | |
4079 | case OPERATOR_EQEQ: | |
4080 | return i == 0; | |
4081 | case OPERATOR_NOTEQ: | |
4082 | return i != 0; | |
4083 | case OPERATOR_LT: | |
4084 | return i < 0; | |
4085 | case OPERATOR_LE: | |
4086 | return i <= 0; | |
4087 | case OPERATOR_GT: | |
4088 | return i > 0; | |
4089 | case OPERATOR_GE: | |
4090 | return i >= 0; | |
4091 | default: | |
4092 | gcc_unreachable(); | |
4093 | } | |
4094 | } | |
4095 | ||
4096 | // Compare floating point constants according to OP. | |
4097 | ||
4098 | bool | |
4099 | Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val, | |
4100 | mpfr_t right_val) | |
4101 | { | |
4102 | int i; | |
4103 | if (type == NULL) | |
4104 | i = mpfr_cmp(left_val, right_val); | |
4105 | else | |
4106 | { | |
4107 | mpfr_t lv; | |
4108 | mpfr_init_set(lv, left_val, GMP_RNDN); | |
4109 | mpfr_t rv; | |
4110 | mpfr_init_set(rv, right_val, GMP_RNDN); | |
4111 | Float_expression::constrain_float(lv, type); | |
4112 | Float_expression::constrain_float(rv, type); | |
4113 | i = mpfr_cmp(lv, rv); | |
4114 | mpfr_clear(lv); | |
4115 | mpfr_clear(rv); | |
4116 | } | |
4117 | switch (op) | |
4118 | { | |
4119 | case OPERATOR_EQEQ: | |
4120 | return i == 0; | |
4121 | case OPERATOR_NOTEQ: | |
4122 | return i != 0; | |
4123 | case OPERATOR_LT: | |
4124 | return i < 0; | |
4125 | case OPERATOR_LE: | |
4126 | return i <= 0; | |
4127 | case OPERATOR_GT: | |
4128 | return i > 0; | |
4129 | case OPERATOR_GE: | |
4130 | return i >= 0; | |
4131 | default: | |
4132 | gcc_unreachable(); | |
4133 | } | |
4134 | } | |
4135 | ||
4136 | // Compare complex constants according to OP. Complex numbers may | |
4137 | // only be compared for equality. | |
4138 | ||
4139 | bool | |
4140 | Binary_expression::compare_complex(Operator op, Type* type, | |
4141 | mpfr_t left_real, mpfr_t left_imag, | |
4142 | mpfr_t right_real, mpfr_t right_imag) | |
4143 | { | |
4144 | bool is_equal; | |
4145 | if (type == NULL) | |
4146 | is_equal = (mpfr_cmp(left_real, right_real) == 0 | |
4147 | && mpfr_cmp(left_imag, right_imag) == 0); | |
4148 | else | |
4149 | { | |
4150 | mpfr_t lr; | |
4151 | mpfr_t li; | |
4152 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4153 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4154 | mpfr_t rr; | |
4155 | mpfr_t ri; | |
4156 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4157 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4158 | Complex_expression::constrain_complex(lr, li, type); | |
4159 | Complex_expression::constrain_complex(rr, ri, type); | |
4160 | is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0; | |
4161 | mpfr_clear(lr); | |
4162 | mpfr_clear(li); | |
4163 | mpfr_clear(rr); | |
4164 | mpfr_clear(ri); | |
4165 | } | |
4166 | switch (op) | |
4167 | { | |
4168 | case OPERATOR_EQEQ: | |
4169 | return is_equal; | |
4170 | case OPERATOR_NOTEQ: | |
4171 | return !is_equal; | |
4172 | default: | |
4173 | gcc_unreachable(); | |
4174 | } | |
4175 | } | |
4176 | ||
4177 | // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. | |
4178 | // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of | |
4179 | // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if | |
4180 | // this could be done, false if not. | |
4181 | ||
4182 | bool | |
4183 | Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val, | |
4184 | Type* right_type, mpz_t right_val, | |
4185 | source_location location, mpz_t val) | |
4186 | { | |
4187 | bool is_shift_op = false; | |
4188 | switch (op) | |
4189 | { | |
4190 | case OPERATOR_OROR: | |
4191 | case OPERATOR_ANDAND: | |
4192 | case OPERATOR_EQEQ: | |
4193 | case OPERATOR_NOTEQ: | |
4194 | case OPERATOR_LT: | |
4195 | case OPERATOR_LE: | |
4196 | case OPERATOR_GT: | |
4197 | case OPERATOR_GE: | |
4198 | // These return boolean values. We should probably handle them | |
4199 | // anyhow in case a type conversion is used on the result. | |
4200 | return false; | |
4201 | case OPERATOR_PLUS: | |
4202 | mpz_add(val, left_val, right_val); | |
4203 | break; | |
4204 | case OPERATOR_MINUS: | |
4205 | mpz_sub(val, left_val, right_val); | |
4206 | break; | |
4207 | case OPERATOR_OR: | |
4208 | mpz_ior(val, left_val, right_val); | |
4209 | break; | |
4210 | case OPERATOR_XOR: | |
4211 | mpz_xor(val, left_val, right_val); | |
4212 | break; | |
4213 | case OPERATOR_MULT: | |
4214 | mpz_mul(val, left_val, right_val); | |
4215 | break; | |
4216 | case OPERATOR_DIV: | |
4217 | if (mpz_sgn(right_val) != 0) | |
4218 | mpz_tdiv_q(val, left_val, right_val); | |
4219 | else | |
4220 | { | |
4221 | error_at(location, "division by zero"); | |
4222 | mpz_set_ui(val, 0); | |
4223 | return true; | |
4224 | } | |
4225 | break; | |
4226 | case OPERATOR_MOD: | |
4227 | if (mpz_sgn(right_val) != 0) | |
4228 | mpz_tdiv_r(val, left_val, right_val); | |
4229 | else | |
4230 | { | |
4231 | error_at(location, "division by zero"); | |
4232 | mpz_set_ui(val, 0); | |
4233 | return true; | |
4234 | } | |
4235 | break; | |
4236 | case OPERATOR_LSHIFT: | |
4237 | { | |
4238 | unsigned long shift = mpz_get_ui(right_val); | |
4239 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4240 | { | |
4241 | error_at(location, "shift count overflow"); | |
4242 | mpz_set_ui(val, 0); | |
4243 | return true; | |
4244 | } | |
4245 | mpz_mul_2exp(val, left_val, shift); | |
4246 | is_shift_op = true; | |
4247 | break; | |
4248 | } | |
4249 | break; | |
4250 | case OPERATOR_RSHIFT: | |
4251 | { | |
4252 | unsigned long shift = mpz_get_ui(right_val); | |
4253 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4254 | { | |
4255 | error_at(location, "shift count overflow"); | |
4256 | mpz_set_ui(val, 0); | |
4257 | return true; | |
4258 | } | |
4259 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4260 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4261 | else | |
4262 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4263 | is_shift_op = true; | |
4264 | break; | |
4265 | } | |
4266 | break; | |
4267 | case OPERATOR_AND: | |
4268 | mpz_and(val, left_val, right_val); | |
4269 | break; | |
4270 | case OPERATOR_BITCLEAR: | |
4271 | { | |
4272 | mpz_t tval; | |
4273 | mpz_init(tval); | |
4274 | mpz_com(tval, right_val); | |
4275 | mpz_and(val, left_val, tval); | |
4276 | mpz_clear(tval); | |
4277 | } | |
4278 | break; | |
4279 | default: | |
4280 | gcc_unreachable(); | |
4281 | } | |
4282 | ||
4283 | Type* type = left_type; | |
4284 | if (!is_shift_op) | |
4285 | { | |
4286 | if (type == NULL) | |
4287 | type = right_type; | |
4288 | else if (type != right_type && right_type != NULL) | |
4289 | { | |
4290 | if (type->is_abstract()) | |
4291 | type = right_type; | |
4292 | else if (!right_type->is_abstract()) | |
4293 | { | |
4294 | // This look like a type error which should be diagnosed | |
4295 | // elsewhere. Don't do anything here, to avoid an | |
4296 | // unhelpful chain of error messages. | |
4297 | return true; | |
4298 | } | |
4299 | } | |
4300 | } | |
4301 | ||
4302 | if (type != NULL && !type->is_abstract()) | |
4303 | { | |
4304 | // We have to check the operands too, as we have implicitly | |
4305 | // coerced them to TYPE. | |
4306 | if ((type != left_type | |
4307 | && !Integer_expression::check_constant(left_val, type, location)) | |
4308 | || (!is_shift_op | |
4309 | && type != right_type | |
4310 | && !Integer_expression::check_constant(right_val, type, | |
4311 | location)) | |
4312 | || !Integer_expression::check_constant(val, type, location)) | |
4313 | mpz_set_ui(val, 0); | |
4314 | } | |
4315 | ||
4316 | return true; | |
4317 | } | |
4318 | ||
4319 | // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. | |
4320 | // Return true if this could be done, false if not. | |
4321 | ||
4322 | bool | |
4323 | Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val, | |
4324 | Type* right_type, mpfr_t right_val, | |
4325 | mpfr_t val, source_location location) | |
4326 | { | |
4327 | switch (op) | |
4328 | { | |
4329 | case OPERATOR_OROR: | |
4330 | case OPERATOR_ANDAND: | |
4331 | case OPERATOR_EQEQ: | |
4332 | case OPERATOR_NOTEQ: | |
4333 | case OPERATOR_LT: | |
4334 | case OPERATOR_LE: | |
4335 | case OPERATOR_GT: | |
4336 | case OPERATOR_GE: | |
4337 | // These return boolean values. We should probably handle them | |
4338 | // anyhow in case a type conversion is used on the result. | |
4339 | return false; | |
4340 | case OPERATOR_PLUS: | |
4341 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4342 | break; | |
4343 | case OPERATOR_MINUS: | |
4344 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4345 | break; | |
4346 | case OPERATOR_OR: | |
4347 | case OPERATOR_XOR: | |
4348 | case OPERATOR_AND: | |
4349 | case OPERATOR_BITCLEAR: | |
4350 | return false; | |
4351 | case OPERATOR_MULT: | |
4352 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4353 | break; | |
4354 | case OPERATOR_DIV: | |
4355 | if (mpfr_zero_p(right_val)) | |
4356 | error_at(location, "division by zero"); | |
4357 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4358 | break; | |
4359 | case OPERATOR_MOD: | |
4360 | return false; | |
4361 | case OPERATOR_LSHIFT: | |
4362 | case OPERATOR_RSHIFT: | |
4363 | return false; | |
4364 | default: | |
4365 | gcc_unreachable(); | |
4366 | } | |
4367 | ||
4368 | Type* type = left_type; | |
4369 | if (type == NULL) | |
4370 | type = right_type; | |
4371 | else if (type != right_type && right_type != NULL) | |
4372 | { | |
4373 | if (type->is_abstract()) | |
4374 | type = right_type; | |
4375 | else if (!right_type->is_abstract()) | |
4376 | { | |
4377 | // This looks like a type error which should be diagnosed | |
4378 | // elsewhere. Don't do anything here, to avoid an unhelpful | |
4379 | // chain of error messages. | |
4380 | return true; | |
4381 | } | |
4382 | } | |
4383 | ||
4384 | if (type != NULL && !type->is_abstract()) | |
4385 | { | |
4386 | if ((type != left_type | |
4387 | && !Float_expression::check_constant(left_val, type, location)) | |
4388 | || (type != right_type | |
4389 | && !Float_expression::check_constant(right_val, type, | |
4390 | location)) | |
4391 | || !Float_expression::check_constant(val, type, location)) | |
4392 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4393 | } | |
4394 | ||
4395 | return true; | |
4396 | } | |
4397 | ||
4398 | // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and | |
4399 | // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this | |
4400 | // could be done, false if not. | |
4401 | ||
4402 | bool | |
4403 | Binary_expression::eval_complex(Operator op, Type* left_type, | |
4404 | mpfr_t left_real, mpfr_t left_imag, | |
4405 | Type *right_type, | |
4406 | mpfr_t right_real, mpfr_t right_imag, | |
4407 | mpfr_t real, mpfr_t imag, | |
4408 | source_location location) | |
4409 | { | |
4410 | switch (op) | |
4411 | { | |
4412 | case OPERATOR_OROR: | |
4413 | case OPERATOR_ANDAND: | |
4414 | case OPERATOR_EQEQ: | |
4415 | case OPERATOR_NOTEQ: | |
4416 | case OPERATOR_LT: | |
4417 | case OPERATOR_LE: | |
4418 | case OPERATOR_GT: | |
4419 | case OPERATOR_GE: | |
4420 | // These return boolean values and must be handled differently. | |
4421 | return false; | |
4422 | case OPERATOR_PLUS: | |
4423 | mpfr_add(real, left_real, right_real, GMP_RNDN); | |
4424 | mpfr_add(imag, left_imag, right_imag, GMP_RNDN); | |
4425 | break; | |
4426 | case OPERATOR_MINUS: | |
4427 | mpfr_sub(real, left_real, right_real, GMP_RNDN); | |
4428 | mpfr_sub(imag, left_imag, right_imag, GMP_RNDN); | |
4429 | break; | |
4430 | case OPERATOR_OR: | |
4431 | case OPERATOR_XOR: | |
4432 | case OPERATOR_AND: | |
4433 | case OPERATOR_BITCLEAR: | |
4434 | return false; | |
4435 | case OPERATOR_MULT: | |
4436 | { | |
4437 | // You might think that multiplying two complex numbers would | |
4438 | // be simple, and you would be right, until you start to think | |
4439 | // about getting the right answer for infinity. If one | |
4440 | // operand here is infinity and the other is anything other | |
4441 | // than zero or NaN, then we are going to wind up subtracting | |
4442 | // two infinity values. That will give us a NaN, but the | |
4443 | // correct answer is infinity. | |
4444 | ||
4445 | mpfr_t lrrr; | |
4446 | mpfr_init(lrrr); | |
4447 | mpfr_mul(lrrr, left_real, right_real, GMP_RNDN); | |
4448 | ||
4449 | mpfr_t lrri; | |
4450 | mpfr_init(lrri); | |
4451 | mpfr_mul(lrri, left_real, right_imag, GMP_RNDN); | |
4452 | ||
4453 | mpfr_t lirr; | |
4454 | mpfr_init(lirr); | |
4455 | mpfr_mul(lirr, left_imag, right_real, GMP_RNDN); | |
4456 | ||
4457 | mpfr_t liri; | |
4458 | mpfr_init(liri); | |
4459 | mpfr_mul(liri, left_imag, right_imag, GMP_RNDN); | |
4460 | ||
4461 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4462 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4463 | ||
4464 | // If we get NaN on both sides, check whether it should really | |
4465 | // be infinity. The rule is that if either side of the | |
4466 | // complex number is infinity, then the whole value is | |
4467 | // infinity, even if the other side is NaN. So the only case | |
4468 | // we have to fix is the one in which both sides are NaN. | |
4469 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4470 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4471 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4472 | { | |
4473 | bool is_infinity = false; | |
4474 | ||
4475 | mpfr_t lr; | |
4476 | mpfr_t li; | |
4477 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4478 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4479 | ||
4480 | mpfr_t rr; | |
4481 | mpfr_t ri; | |
4482 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4483 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4484 | ||
4485 | // If the left side is infinity, then the result is | |
4486 | // infinity. | |
4487 | if (mpfr_inf_p(lr) || mpfr_inf_p(li)) | |
4488 | { | |
4489 | mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN); | |
4490 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4491 | mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN); | |
4492 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4493 | if (mpfr_nan_p(rr)) | |
4494 | { | |
4495 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4496 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4497 | } | |
4498 | if (mpfr_nan_p(ri)) | |
4499 | { | |
4500 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4501 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4502 | } | |
4503 | is_infinity = true; | |
4504 | } | |
4505 | ||
4506 | // If the right side is infinity, then the result is | |
4507 | // infinity. | |
4508 | if (mpfr_inf_p(rr) || mpfr_inf_p(ri)) | |
4509 | { | |
4510 | mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4511 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4512 | mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
4513 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4514 | if (mpfr_nan_p(lr)) | |
4515 | { | |
4516 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4517 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4518 | } | |
4519 | if (mpfr_nan_p(li)) | |
4520 | { | |
4521 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4522 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4523 | } | |
4524 | is_infinity = true; | |
4525 | } | |
4526 | ||
4527 | // If we got an overflow in the intermediate computations, | |
4528 | // then the result is infinity. | |
4529 | if (!is_infinity | |
4530 | && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri) | |
4531 | || mpfr_inf_p(lirr) || mpfr_inf_p(liri))) | |
4532 | { | |
4533 | if (mpfr_nan_p(lr)) | |
4534 | { | |
4535 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4536 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4537 | } | |
4538 | if (mpfr_nan_p(li)) | |
4539 | { | |
4540 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4541 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4542 | } | |
4543 | if (mpfr_nan_p(rr)) | |
4544 | { | |
4545 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4546 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4547 | } | |
4548 | if (mpfr_nan_p(ri)) | |
4549 | { | |
4550 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4551 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4552 | } | |
4553 | is_infinity = true; | |
4554 | } | |
4555 | ||
4556 | if (is_infinity) | |
4557 | { | |
4558 | mpfr_mul(lrrr, lr, rr, GMP_RNDN); | |
4559 | mpfr_mul(lrri, lr, ri, GMP_RNDN); | |
4560 | mpfr_mul(lirr, li, rr, GMP_RNDN); | |
4561 | mpfr_mul(liri, li, ri, GMP_RNDN); | |
4562 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4563 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4564 | mpfr_set_inf(real, mpfr_sgn(real)); | |
4565 | mpfr_set_inf(imag, mpfr_sgn(imag)); | |
4566 | } | |
4567 | ||
4568 | mpfr_clear(lr); | |
4569 | mpfr_clear(li); | |
4570 | mpfr_clear(rr); | |
4571 | mpfr_clear(ri); | |
4572 | } | |
4573 | ||
4574 | mpfr_clear(lrrr); | |
4575 | mpfr_clear(lrri); | |
4576 | mpfr_clear(lirr); | |
4577 | mpfr_clear(liri); | |
4578 | } | |
4579 | break; | |
4580 | case OPERATOR_DIV: | |
4581 | { | |
4582 | // For complex division we want to avoid having an | |
4583 | // intermediate overflow turn the whole result in a NaN. We | |
4584 | // scale the values to try to avoid this. | |
4585 | ||
4586 | if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag)) | |
4587 | error_at(location, "division by zero"); | |
4588 | ||
4589 | mpfr_t rra; | |
4590 | mpfr_t ria; | |
4591 | mpfr_init(rra); | |
4592 | mpfr_init(ria); | |
4593 | mpfr_abs(rra, right_real, GMP_RNDN); | |
4594 | mpfr_abs(ria, right_imag, GMP_RNDN); | |
4595 | mpfr_t t; | |
4596 | mpfr_init(t); | |
4597 | mpfr_max(t, rra, ria, GMP_RNDN); | |
4598 | ||
4599 | mpfr_t rr; | |
4600 | mpfr_t ri; | |
4601 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4602 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4603 | long ilogbw = 0; | |
4604 | if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t)) | |
4605 | { | |
4606 | ilogbw = mpfr_get_exp(t); | |
4607 | mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN); | |
4608 | mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN); | |
4609 | } | |
4610 | ||
4611 | mpfr_t denom; | |
4612 | mpfr_init(denom); | |
4613 | mpfr_mul(denom, rr, rr, GMP_RNDN); | |
4614 | mpfr_mul(t, ri, ri, GMP_RNDN); | |
4615 | mpfr_add(denom, denom, t, GMP_RNDN); | |
4616 | ||
4617 | mpfr_mul(real, left_real, rr, GMP_RNDN); | |
4618 | mpfr_mul(t, left_imag, ri, GMP_RNDN); | |
4619 | mpfr_add(real, real, t, GMP_RNDN); | |
4620 | mpfr_div(real, real, denom, GMP_RNDN); | |
4621 | mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN); | |
4622 | ||
4623 | mpfr_mul(imag, left_imag, rr, GMP_RNDN); | |
4624 | mpfr_mul(t, left_real, ri, GMP_RNDN); | |
4625 | mpfr_sub(imag, imag, t, GMP_RNDN); | |
4626 | mpfr_div(imag, imag, denom, GMP_RNDN); | |
4627 | mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN); | |
4628 | ||
4629 | // If we wind up with NaN on both sides, check whether we | |
4630 | // should really have infinity. The rule is that if either | |
4631 | // side of the complex number is infinity, then the whole | |
4632 | // value is infinity, even if the other side is NaN. So the | |
4633 | // only case we have to fix is the one in which both sides are | |
4634 | // NaN. | |
4635 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4636 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4637 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4638 | { | |
4639 | if (mpfr_zero_p(denom)) | |
4640 | { | |
4641 | mpfr_set_inf(real, mpfr_sgn(rr)); | |
4642 | mpfr_mul(real, real, left_real, GMP_RNDN); | |
4643 | mpfr_set_inf(imag, mpfr_sgn(rr)); | |
4644 | mpfr_mul(imag, imag, left_imag, GMP_RNDN); | |
4645 | } | |
4646 | else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag)) | |
4647 | && mpfr_number_p(rr) && mpfr_number_p(ri)) | |
4648 | { | |
4649 | mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN); | |
4650 | mpfr_copysign(t, t, left_real, GMP_RNDN); | |
4651 | ||
4652 | mpfr_t t2; | |
4653 | mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN); | |
4654 | mpfr_copysign(t2, t2, left_imag, GMP_RNDN); | |
4655 | ||
4656 | mpfr_t t3; | |
4657 | mpfr_init(t3); | |
4658 | mpfr_mul(t3, t, rr, GMP_RNDN); | |
4659 | ||
4660 | mpfr_t t4; | |
4661 | mpfr_init(t4); | |
4662 | mpfr_mul(t4, t2, ri, GMP_RNDN); | |
4663 | ||
4664 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
4665 | mpfr_set_inf(real, mpfr_sgn(t3)); | |
4666 | ||
4667 | mpfr_mul(t3, t2, rr, GMP_RNDN); | |
4668 | mpfr_mul(t4, t, ri, GMP_RNDN); | |
4669 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
4670 | mpfr_set_inf(imag, mpfr_sgn(t3)); | |
4671 | ||
4672 | mpfr_clear(t2); | |
4673 | mpfr_clear(t3); | |
4674 | mpfr_clear(t4); | |
4675 | } | |
4676 | else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag)) | |
4677 | && mpfr_number_p(left_real) && mpfr_number_p(left_imag)) | |
4678 | { | |
4679 | mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4680 | mpfr_copysign(t, t, rr, GMP_RNDN); | |
4681 | ||
4682 | mpfr_t t2; | |
4683 | mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
4684 | mpfr_copysign(t2, t2, ri, GMP_RNDN); | |
4685 | ||
4686 | mpfr_t t3; | |
4687 | mpfr_init(t3); | |
4688 | mpfr_mul(t3, left_real, t, GMP_RNDN); | |
4689 | ||
4690 | mpfr_t t4; | |
4691 | mpfr_init(t4); | |
4692 | mpfr_mul(t4, left_imag, t2, GMP_RNDN); | |
4693 | ||
4694 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
4695 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4696 | mpfr_mul(real, real, t3, GMP_RNDN); | |
4697 | ||
4698 | mpfr_mul(t3, left_imag, t, GMP_RNDN); | |
4699 | mpfr_mul(t4, left_real, t2, GMP_RNDN); | |
4700 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
4701 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4702 | mpfr_mul(imag, imag, t3, GMP_RNDN); | |
4703 | ||
4704 | mpfr_clear(t2); | |
4705 | mpfr_clear(t3); | |
4706 | mpfr_clear(t4); | |
4707 | } | |
4708 | } | |
4709 | ||
4710 | mpfr_clear(denom); | |
4711 | mpfr_clear(rr); | |
4712 | mpfr_clear(ri); | |
4713 | mpfr_clear(t); | |
4714 | mpfr_clear(rra); | |
4715 | mpfr_clear(ria); | |
4716 | } | |
4717 | break; | |
4718 | case OPERATOR_MOD: | |
4719 | return false; | |
4720 | case OPERATOR_LSHIFT: | |
4721 | case OPERATOR_RSHIFT: | |
4722 | return false; | |
4723 | default: | |
4724 | gcc_unreachable(); | |
4725 | } | |
4726 | ||
4727 | Type* type = left_type; | |
4728 | if (type == NULL) | |
4729 | type = right_type; | |
4730 | else if (type != right_type && right_type != NULL) | |
4731 | { | |
4732 | if (type->is_abstract()) | |
4733 | type = right_type; | |
4734 | else if (!right_type->is_abstract()) | |
4735 | { | |
4736 | // This looks like a type error which should be diagnosed | |
4737 | // elsewhere. Don't do anything here, to avoid an unhelpful | |
4738 | // chain of error messages. | |
4739 | return true; | |
4740 | } | |
4741 | } | |
4742 | ||
4743 | if (type != NULL && !type->is_abstract()) | |
4744 | { | |
4745 | if ((type != left_type | |
4746 | && !Complex_expression::check_constant(left_real, left_imag, | |
4747 | type, location)) | |
4748 | || (type != right_type | |
4749 | && !Complex_expression::check_constant(right_real, right_imag, | |
4750 | type, location)) | |
4751 | || !Complex_expression::check_constant(real, imag, type, | |
4752 | location)) | |
4753 | { | |
4754 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4755 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4756 | } | |
4757 | } | |
4758 | ||
4759 | return true; | |
4760 | } | |
4761 | ||
4762 | // Lower a binary expression. We have to evaluate constant | |
4763 | // expressions now, in order to implement Go's unlimited precision | |
4764 | // constants. | |
4765 | ||
4766 | Expression* | |
4767 | Binary_expression::do_lower(Gogo*, Named_object*, int) | |
4768 | { | |
4769 | source_location location = this->location(); | |
4770 | Operator op = this->op_; | |
4771 | Expression* left = this->left_; | |
4772 | Expression* right = this->right_; | |
4773 | ||
4774 | const bool is_comparison = (op == OPERATOR_EQEQ | |
4775 | || op == OPERATOR_NOTEQ | |
4776 | || op == OPERATOR_LT | |
4777 | || op == OPERATOR_LE | |
4778 | || op == OPERATOR_GT | |
4779 | || op == OPERATOR_GE); | |
4780 | ||
4781 | // Integer constant expressions. | |
4782 | { | |
4783 | mpz_t left_val; | |
4784 | mpz_init(left_val); | |
4785 | Type* left_type; | |
4786 | mpz_t right_val; | |
4787 | mpz_init(right_val); | |
4788 | Type* right_type; | |
4789 | if (left->integer_constant_value(false, left_val, &left_type) | |
4790 | && right->integer_constant_value(false, right_val, &right_type)) | |
4791 | { | |
4792 | Expression* ret = NULL; | |
4793 | if (left_type != right_type | |
4794 | && left_type != NULL | |
4795 | && right_type != NULL | |
4796 | && left_type->base() != right_type->base() | |
4797 | && op != OPERATOR_LSHIFT | |
4798 | && op != OPERATOR_RSHIFT) | |
4799 | { | |
4800 | // May be a type error--let it be diagnosed later. | |
4801 | } | |
4802 | else if (is_comparison) | |
4803 | { | |
4804 | bool b = Binary_expression::compare_integer(op, left_val, | |
4805 | right_val); | |
4806 | ret = Expression::make_cast(Type::lookup_bool_type(), | |
4807 | Expression::make_boolean(b, location), | |
4808 | location); | |
4809 | } | |
4810 | else | |
4811 | { | |
4812 | mpz_t val; | |
4813 | mpz_init(val); | |
4814 | ||
4815 | if (Binary_expression::eval_integer(op, left_type, left_val, | |
4816 | right_type, right_val, | |
4817 | location, val)) | |
4818 | { | |
4819 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND); | |
4820 | Type* type; | |
4821 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4822 | type = left_type; | |
4823 | else if (left_type == NULL) | |
4824 | type = right_type; | |
4825 | else if (right_type == NULL) | |
4826 | type = left_type; | |
4827 | else if (!left_type->is_abstract() | |
4828 | && left_type->named_type() != NULL) | |
4829 | type = left_type; | |
4830 | else if (!right_type->is_abstract() | |
4831 | && right_type->named_type() != NULL) | |
4832 | type = right_type; | |
4833 | else if (!left_type->is_abstract()) | |
4834 | type = left_type; | |
4835 | else if (!right_type->is_abstract()) | |
4836 | type = right_type; | |
4837 | else if (left_type->float_type() != NULL) | |
4838 | type = left_type; | |
4839 | else if (right_type->float_type() != NULL) | |
4840 | type = right_type; | |
4841 | else if (left_type->complex_type() != NULL) | |
4842 | type = left_type; | |
4843 | else if (right_type->complex_type() != NULL) | |
4844 | type = right_type; | |
4845 | else | |
4846 | type = left_type; | |
4847 | ret = Expression::make_integer(&val, type, location); | |
4848 | } | |
4849 | ||
4850 | mpz_clear(val); | |
4851 | } | |
4852 | ||
4853 | if (ret != NULL) | |
4854 | { | |
4855 | mpz_clear(right_val); | |
4856 | mpz_clear(left_val); | |
4857 | return ret; | |
4858 | } | |
4859 | } | |
4860 | mpz_clear(right_val); | |
4861 | mpz_clear(left_val); | |
4862 | } | |
4863 | ||
4864 | // Floating point constant expressions. | |
4865 | { | |
4866 | mpfr_t left_val; | |
4867 | mpfr_init(left_val); | |
4868 | Type* left_type; | |
4869 | mpfr_t right_val; | |
4870 | mpfr_init(right_val); | |
4871 | Type* right_type; | |
4872 | if (left->float_constant_value(left_val, &left_type) | |
4873 | && right->float_constant_value(right_val, &right_type)) | |
4874 | { | |
4875 | Expression* ret = NULL; | |
4876 | if (left_type != right_type | |
4877 | && left_type != NULL | |
4878 | && right_type != NULL | |
4879 | && left_type->base() != right_type->base() | |
4880 | && op != OPERATOR_LSHIFT | |
4881 | && op != OPERATOR_RSHIFT) | |
4882 | { | |
4883 | // May be a type error--let it be diagnosed later. | |
4884 | } | |
4885 | else if (is_comparison) | |
4886 | { | |
4887 | bool b = Binary_expression::compare_float(op, | |
4888 | (left_type != NULL | |
4889 | ? left_type | |
4890 | : right_type), | |
4891 | left_val, right_val); | |
4892 | ret = Expression::make_boolean(b, location); | |
4893 | } | |
4894 | else | |
4895 | { | |
4896 | mpfr_t val; | |
4897 | mpfr_init(val); | |
4898 | ||
4899 | if (Binary_expression::eval_float(op, left_type, left_val, | |
4900 | right_type, right_val, val, | |
4901 | location)) | |
4902 | { | |
4903 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND | |
4904 | && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT); | |
4905 | Type* type; | |
4906 | if (left_type == NULL) | |
4907 | type = right_type; | |
4908 | else if (right_type == NULL) | |
4909 | type = left_type; | |
4910 | else if (!left_type->is_abstract() | |
4911 | && left_type->named_type() != NULL) | |
4912 | type = left_type; | |
4913 | else if (!right_type->is_abstract() | |
4914 | && right_type->named_type() != NULL) | |
4915 | type = right_type; | |
4916 | else if (!left_type->is_abstract()) | |
4917 | type = left_type; | |
4918 | else if (!right_type->is_abstract()) | |
4919 | type = right_type; | |
4920 | else if (left_type->float_type() != NULL) | |
4921 | type = left_type; | |
4922 | else if (right_type->float_type() != NULL) | |
4923 | type = right_type; | |
4924 | else | |
4925 | type = left_type; | |
4926 | ret = Expression::make_float(&val, type, location); | |
4927 | } | |
4928 | ||
4929 | mpfr_clear(val); | |
4930 | } | |
4931 | ||
4932 | if (ret != NULL) | |
4933 | { | |
4934 | mpfr_clear(right_val); | |
4935 | mpfr_clear(left_val); | |
4936 | return ret; | |
4937 | } | |
4938 | } | |
4939 | mpfr_clear(right_val); | |
4940 | mpfr_clear(left_val); | |
4941 | } | |
4942 | ||
4943 | // Complex constant expressions. | |
4944 | { | |
4945 | mpfr_t left_real; | |
4946 | mpfr_t left_imag; | |
4947 | mpfr_init(left_real); | |
4948 | mpfr_init(left_imag); | |
4949 | Type* left_type; | |
4950 | ||
4951 | mpfr_t right_real; | |
4952 | mpfr_t right_imag; | |
4953 | mpfr_init(right_real); | |
4954 | mpfr_init(right_imag); | |
4955 | Type* right_type; | |
4956 | ||
4957 | if (left->complex_constant_value(left_real, left_imag, &left_type) | |
4958 | && right->complex_constant_value(right_real, right_imag, &right_type)) | |
4959 | { | |
4960 | Expression* ret = NULL; | |
4961 | if (left_type != right_type | |
4962 | && left_type != NULL | |
4963 | && right_type != NULL | |
4964 | && left_type->base() != right_type->base()) | |
4965 | { | |
4966 | // May be a type error--let it be diagnosed later. | |
4967 | } | |
4968 | else if (is_comparison) | |
4969 | { | |
4970 | bool b = Binary_expression::compare_complex(op, | |
4971 | (left_type != NULL | |
4972 | ? left_type | |
4973 | : right_type), | |
4974 | left_real, | |
4975 | left_imag, | |
4976 | right_real, | |
4977 | right_imag); | |
4978 | ret = Expression::make_boolean(b, location); | |
4979 | } | |
4980 | else | |
4981 | { | |
4982 | mpfr_t real; | |
4983 | mpfr_t imag; | |
4984 | mpfr_init(real); | |
4985 | mpfr_init(imag); | |
4986 | ||
4987 | if (Binary_expression::eval_complex(op, left_type, | |
4988 | left_real, left_imag, | |
4989 | right_type, | |
4990 | right_real, right_imag, | |
4991 | real, imag, | |
4992 | location)) | |
4993 | { | |
4994 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND | |
4995 | && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT); | |
4996 | Type* type; | |
4997 | if (left_type == NULL) | |
4998 | type = right_type; | |
4999 | else if (right_type == NULL) | |
5000 | type = left_type; | |
5001 | else if (!left_type->is_abstract() | |
5002 | && left_type->named_type() != NULL) | |
5003 | type = left_type; | |
5004 | else if (!right_type->is_abstract() | |
5005 | && right_type->named_type() != NULL) | |
5006 | type = right_type; | |
5007 | else if (!left_type->is_abstract()) | |
5008 | type = left_type; | |
5009 | else if (!right_type->is_abstract()) | |
5010 | type = right_type; | |
5011 | else if (left_type->complex_type() != NULL) | |
5012 | type = left_type; | |
5013 | else if (right_type->complex_type() != NULL) | |
5014 | type = right_type; | |
5015 | else | |
5016 | type = left_type; | |
5017 | ret = Expression::make_complex(&real, &imag, type, | |
5018 | location); | |
5019 | } | |
5020 | mpfr_clear(real); | |
5021 | mpfr_clear(imag); | |
5022 | } | |
5023 | ||
5024 | if (ret != NULL) | |
5025 | { | |
5026 | mpfr_clear(left_real); | |
5027 | mpfr_clear(left_imag); | |
5028 | mpfr_clear(right_real); | |
5029 | mpfr_clear(right_imag); | |
5030 | return ret; | |
5031 | } | |
5032 | } | |
5033 | ||
5034 | mpfr_clear(left_real); | |
5035 | mpfr_clear(left_imag); | |
5036 | mpfr_clear(right_real); | |
5037 | mpfr_clear(right_imag); | |
5038 | } | |
5039 | ||
5040 | // String constant expressions. | |
5041 | if (op == OPERATOR_PLUS | |
5042 | && left->type()->is_string_type() | |
5043 | && right->type()->is_string_type()) | |
5044 | { | |
5045 | std::string left_string; | |
5046 | std::string right_string; | |
5047 | if (left->string_constant_value(&left_string) | |
5048 | && right->string_constant_value(&right_string)) | |
5049 | return Expression::make_string(left_string + right_string, location); | |
5050 | } | |
5051 | ||
5052 | return this; | |
5053 | } | |
5054 | ||
5055 | // Return the integer constant value, if it has one. | |
5056 | ||
5057 | bool | |
5058 | Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
5059 | Type** ptype) const | |
5060 | { | |
5061 | mpz_t left_val; | |
5062 | mpz_init(left_val); | |
5063 | Type* left_type; | |
5064 | if (!this->left_->integer_constant_value(iota_is_constant, left_val, | |
5065 | &left_type)) | |
5066 | { | |
5067 | mpz_clear(left_val); | |
5068 | return false; | |
5069 | } | |
5070 | ||
5071 | mpz_t right_val; | |
5072 | mpz_init(right_val); | |
5073 | Type* right_type; | |
5074 | if (!this->right_->integer_constant_value(iota_is_constant, right_val, | |
5075 | &right_type)) | |
5076 | { | |
5077 | mpz_clear(right_val); | |
5078 | mpz_clear(left_val); | |
5079 | return false; | |
5080 | } | |
5081 | ||
5082 | bool ret; | |
5083 | if (left_type != right_type | |
5084 | && left_type != NULL | |
5085 | && right_type != NULL | |
5086 | && left_type->base() != right_type->base() | |
5087 | && this->op_ != OPERATOR_RSHIFT | |
5088 | && this->op_ != OPERATOR_LSHIFT) | |
5089 | ret = false; | |
5090 | else | |
5091 | ret = Binary_expression::eval_integer(this->op_, left_type, left_val, | |
5092 | right_type, right_val, | |
5093 | this->location(), val); | |
5094 | ||
5095 | mpz_clear(right_val); | |
5096 | mpz_clear(left_val); | |
5097 | ||
5098 | if (ret) | |
5099 | *ptype = left_type; | |
5100 | ||
5101 | return ret; | |
5102 | } | |
5103 | ||
5104 | // Return the floating point constant value, if it has one. | |
5105 | ||
5106 | bool | |
5107 | Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
5108 | { | |
5109 | mpfr_t left_val; | |
5110 | mpfr_init(left_val); | |
5111 | Type* left_type; | |
5112 | if (!this->left_->float_constant_value(left_val, &left_type)) | |
5113 | { | |
5114 | mpfr_clear(left_val); | |
5115 | return false; | |
5116 | } | |
5117 | ||
5118 | mpfr_t right_val; | |
5119 | mpfr_init(right_val); | |
5120 | Type* right_type; | |
5121 | if (!this->right_->float_constant_value(right_val, &right_type)) | |
5122 | { | |
5123 | mpfr_clear(right_val); | |
5124 | mpfr_clear(left_val); | |
5125 | return false; | |
5126 | } | |
5127 | ||
5128 | bool ret; | |
5129 | if (left_type != right_type | |
5130 | && left_type != NULL | |
5131 | && right_type != NULL | |
5132 | && left_type->base() != right_type->base()) | |
5133 | ret = false; | |
5134 | else | |
5135 | ret = Binary_expression::eval_float(this->op_, left_type, left_val, | |
5136 | right_type, right_val, | |
5137 | val, this->location()); | |
5138 | ||
5139 | mpfr_clear(left_val); | |
5140 | mpfr_clear(right_val); | |
5141 | ||
5142 | if (ret) | |
5143 | *ptype = left_type; | |
5144 | ||
5145 | return ret; | |
5146 | } | |
5147 | ||
5148 | // Return the complex constant value, if it has one. | |
5149 | ||
5150 | bool | |
5151 | Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
5152 | Type** ptype) const | |
5153 | { | |
5154 | mpfr_t left_real; | |
5155 | mpfr_t left_imag; | |
5156 | mpfr_init(left_real); | |
5157 | mpfr_init(left_imag); | |
5158 | Type* left_type; | |
5159 | if (!this->left_->complex_constant_value(left_real, left_imag, &left_type)) | |
5160 | { | |
5161 | mpfr_clear(left_real); | |
5162 | mpfr_clear(left_imag); | |
5163 | return false; | |
5164 | } | |
5165 | ||
5166 | mpfr_t right_real; | |
5167 | mpfr_t right_imag; | |
5168 | mpfr_init(right_real); | |
5169 | mpfr_init(right_imag); | |
5170 | Type* right_type; | |
5171 | if (!this->right_->complex_constant_value(right_real, right_imag, | |
5172 | &right_type)) | |
5173 | { | |
5174 | mpfr_clear(left_real); | |
5175 | mpfr_clear(left_imag); | |
5176 | mpfr_clear(right_real); | |
5177 | mpfr_clear(right_imag); | |
5178 | return false; | |
5179 | } | |
5180 | ||
5181 | bool ret; | |
5182 | if (left_type != right_type | |
5183 | && left_type != NULL | |
5184 | && right_type != NULL | |
5185 | && left_type->base() != right_type->base()) | |
5186 | ret = false; | |
5187 | else | |
5188 | ret = Binary_expression::eval_complex(this->op_, left_type, | |
5189 | left_real, left_imag, | |
5190 | right_type, | |
5191 | right_real, right_imag, | |
5192 | real, imag, | |
5193 | this->location()); | |
5194 | mpfr_clear(left_real); | |
5195 | mpfr_clear(left_imag); | |
5196 | mpfr_clear(right_real); | |
5197 | mpfr_clear(right_imag); | |
5198 | ||
5199 | if (ret) | |
5200 | *ptype = left_type; | |
5201 | ||
5202 | return ret; | |
5203 | } | |
5204 | ||
5205 | // Note that the value is being discarded. | |
5206 | ||
5207 | void | |
5208 | Binary_expression::do_discarding_value() | |
5209 | { | |
5210 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
5211 | this->right_->discarding_value(); | |
5212 | else | |
5213 | this->warn_about_unused_value(); | |
5214 | } | |
5215 | ||
5216 | // Get type. | |
5217 | ||
5218 | Type* | |
5219 | Binary_expression::do_type() | |
5220 | { | |
5221 | switch (this->op_) | |
5222 | { | |
5223 | case OPERATOR_OROR: | |
5224 | case OPERATOR_ANDAND: | |
5225 | case OPERATOR_EQEQ: | |
5226 | case OPERATOR_NOTEQ: | |
5227 | case OPERATOR_LT: | |
5228 | case OPERATOR_LE: | |
5229 | case OPERATOR_GT: | |
5230 | case OPERATOR_GE: | |
5231 | return Type::lookup_bool_type(); | |
5232 | ||
5233 | case OPERATOR_PLUS: | |
5234 | case OPERATOR_MINUS: | |
5235 | case OPERATOR_OR: | |
5236 | case OPERATOR_XOR: | |
5237 | case OPERATOR_MULT: | |
5238 | case OPERATOR_DIV: | |
5239 | case OPERATOR_MOD: | |
5240 | case OPERATOR_AND: | |
5241 | case OPERATOR_BITCLEAR: | |
5242 | { | |
5243 | Type* left_type = this->left_->type(); | |
5244 | Type* right_type = this->right_->type(); | |
5245 | if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
5246 | return left_type; | |
5247 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
5248 | return right_type; | |
5249 | else if (!left_type->is_abstract()) | |
5250 | return left_type; | |
5251 | else if (!right_type->is_abstract()) | |
5252 | return right_type; | |
5253 | else if (left_type->complex_type() != NULL) | |
5254 | return left_type; | |
5255 | else if (right_type->complex_type() != NULL) | |
5256 | return right_type; | |
5257 | else if (left_type->float_type() != NULL) | |
5258 | return left_type; | |
5259 | else if (right_type->float_type() != NULL) | |
5260 | return right_type; | |
5261 | else | |
5262 | return left_type; | |
5263 | } | |
5264 | ||
5265 | case OPERATOR_LSHIFT: | |
5266 | case OPERATOR_RSHIFT: | |
5267 | return this->left_->type(); | |
5268 | ||
5269 | default: | |
5270 | gcc_unreachable(); | |
5271 | } | |
5272 | } | |
5273 | ||
5274 | // Set type for a binary expression. | |
5275 | ||
5276 | void | |
5277 | Binary_expression::do_determine_type(const Type_context* context) | |
5278 | { | |
5279 | Type* tleft = this->left_->type(); | |
5280 | Type* tright = this->right_->type(); | |
5281 | ||
5282 | // Both sides should have the same type, except for the shift | |
5283 | // operations. For a comparison, we should ignore the incoming | |
5284 | // type. | |
5285 | ||
5286 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5287 | || this->op_ == OPERATOR_RSHIFT); | |
5288 | ||
5289 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5290 | || this->op_ == OPERATOR_NOTEQ | |
5291 | || this->op_ == OPERATOR_LT | |
5292 | || this->op_ == OPERATOR_LE | |
5293 | || this->op_ == OPERATOR_GT | |
5294 | || this->op_ == OPERATOR_GE); | |
5295 | ||
5296 | Type_context subcontext(*context); | |
5297 | ||
5298 | if (is_comparison) | |
5299 | { | |
5300 | // In a comparison, the context does not determine the types of | |
5301 | // the operands. | |
5302 | subcontext.type = NULL; | |
5303 | } | |
5304 | ||
5305 | // Set the context for the left hand operand. | |
5306 | if (is_shift_op) | |
5307 | { | |
5308 | // The right hand operand plays no role in determining the type | |
5309 | // of the left hand operand. A shift of an abstract integer in | |
5310 | // a string context gets special treatment, which may be a | |
5311 | // language bug. | |
5312 | if (subcontext.type != NULL | |
5313 | && subcontext.type->is_string_type() | |
5314 | && tleft->is_abstract()) | |
5315 | error_at(this->location(), "shift of non-integer operand"); | |
5316 | } | |
5317 | else if (!tleft->is_abstract()) | |
5318 | subcontext.type = tleft; | |
5319 | else if (!tright->is_abstract()) | |
5320 | subcontext.type = tright; | |
5321 | else if (subcontext.type == NULL) | |
5322 | { | |
5323 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5324 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5325 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5326 | { | |
5327 | // Both sides have an abstract integer, abstract float, or | |
5328 | // abstract complex type. Just let CONTEXT determine | |
5329 | // whether they may remain abstract or not. | |
5330 | } | |
5331 | else if (tleft->complex_type() != NULL) | |
5332 | subcontext.type = tleft; | |
5333 | else if (tright->complex_type() != NULL) | |
5334 | subcontext.type = tright; | |
5335 | else if (tleft->float_type() != NULL) | |
5336 | subcontext.type = tleft; | |
5337 | else if (tright->float_type() != NULL) | |
5338 | subcontext.type = tright; | |
5339 | else | |
5340 | subcontext.type = tleft; | |
5341 | } | |
5342 | ||
5343 | this->left_->determine_type(&subcontext); | |
5344 | ||
5345 | // The context for the right hand operand is the same as for the | |
5346 | // left hand operand, except for a shift operator. | |
5347 | if (is_shift_op) | |
5348 | { | |
5349 | subcontext.type = Type::lookup_integer_type("uint"); | |
5350 | subcontext.may_be_abstract = false; | |
5351 | } | |
5352 | ||
5353 | this->right_->determine_type(&subcontext); | |
5354 | } | |
5355 | ||
5356 | // Report an error if the binary operator OP does not support TYPE. | |
5357 | // Return whether the operation is OK. This should not be used for | |
5358 | // shift. | |
5359 | ||
5360 | bool | |
5361 | Binary_expression::check_operator_type(Operator op, Type* type, | |
5362 | source_location location) | |
5363 | { | |
5364 | switch (op) | |
5365 | { | |
5366 | case OPERATOR_OROR: | |
5367 | case OPERATOR_ANDAND: | |
5368 | if (!type->is_boolean_type()) | |
5369 | { | |
5370 | error_at(location, "expected boolean type"); | |
5371 | return false; | |
5372 | } | |
5373 | break; | |
5374 | ||
5375 | case OPERATOR_EQEQ: | |
5376 | case OPERATOR_NOTEQ: | |
5377 | if (type->integer_type() == NULL | |
5378 | && type->float_type() == NULL | |
5379 | && type->complex_type() == NULL | |
5380 | && !type->is_string_type() | |
5381 | && type->points_to() == NULL | |
5382 | && !type->is_nil_type() | |
5383 | && !type->is_boolean_type() | |
5384 | && type->interface_type() == NULL | |
5385 | && (type->array_type() == NULL | |
5386 | || type->array_type()->length() != NULL) | |
5387 | && type->map_type() == NULL | |
5388 | && type->channel_type() == NULL | |
5389 | && type->function_type() == NULL) | |
5390 | { | |
5391 | error_at(location, | |
5392 | ("expected integer, floating, complex, string, pointer, " | |
5393 | "boolean, interface, slice, map, channel, " | |
5394 | "or function type")); | |
5395 | return false; | |
5396 | } | |
5397 | break; | |
5398 | ||
5399 | case OPERATOR_LT: | |
5400 | case OPERATOR_LE: | |
5401 | case OPERATOR_GT: | |
5402 | case OPERATOR_GE: | |
5403 | if (type->integer_type() == NULL | |
5404 | && type->float_type() == NULL | |
5405 | && !type->is_string_type()) | |
5406 | { | |
5407 | error_at(location, "expected integer, floating, or string type"); | |
5408 | return false; | |
5409 | } | |
5410 | break; | |
5411 | ||
5412 | case OPERATOR_PLUS: | |
5413 | case OPERATOR_PLUSEQ: | |
5414 | if (type->integer_type() == NULL | |
5415 | && type->float_type() == NULL | |
5416 | && type->complex_type() == NULL | |
5417 | && !type->is_string_type()) | |
5418 | { | |
5419 | error_at(location, | |
5420 | "expected integer, floating, complex, or string type"); | |
5421 | return false; | |
5422 | } | |
5423 | break; | |
5424 | ||
5425 | case OPERATOR_MINUS: | |
5426 | case OPERATOR_MINUSEQ: | |
5427 | case OPERATOR_MULT: | |
5428 | case OPERATOR_MULTEQ: | |
5429 | case OPERATOR_DIV: | |
5430 | case OPERATOR_DIVEQ: | |
5431 | if (type->integer_type() == NULL | |
5432 | && type->float_type() == NULL | |
5433 | && type->complex_type() == NULL) | |
5434 | { | |
5435 | error_at(location, "expected integer, floating, or complex type"); | |
5436 | return false; | |
5437 | } | |
5438 | break; | |
5439 | ||
5440 | case OPERATOR_MOD: | |
5441 | case OPERATOR_MODEQ: | |
5442 | case OPERATOR_OR: | |
5443 | case OPERATOR_OREQ: | |
5444 | case OPERATOR_AND: | |
5445 | case OPERATOR_ANDEQ: | |
5446 | case OPERATOR_XOR: | |
5447 | case OPERATOR_XOREQ: | |
5448 | case OPERATOR_BITCLEAR: | |
5449 | case OPERATOR_BITCLEAREQ: | |
5450 | if (type->integer_type() == NULL) | |
5451 | { | |
5452 | error_at(location, "expected integer type"); | |
5453 | return false; | |
5454 | } | |
5455 | break; | |
5456 | ||
5457 | default: | |
5458 | gcc_unreachable(); | |
5459 | } | |
5460 | ||
5461 | return true; | |
5462 | } | |
5463 | ||
5464 | // Check types. | |
5465 | ||
5466 | void | |
5467 | Binary_expression::do_check_types(Gogo*) | |
5468 | { | |
5469 | Type* left_type = this->left_->type(); | |
5470 | Type* right_type = this->right_->type(); | |
5471 | if (left_type->is_error_type() || right_type->is_error_type()) | |
5472 | return; | |
5473 | ||
5474 | if (this->op_ == OPERATOR_EQEQ | |
5475 | || this->op_ == OPERATOR_NOTEQ | |
5476 | || this->op_ == OPERATOR_LT | |
5477 | || this->op_ == OPERATOR_LE | |
5478 | || this->op_ == OPERATOR_GT | |
5479 | || this->op_ == OPERATOR_GE) | |
5480 | { | |
5481 | if (!Type::are_assignable(left_type, right_type, NULL) | |
5482 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5483 | { | |
5484 | this->report_error(_("incompatible types in binary expression")); | |
5485 | return; | |
5486 | } | |
5487 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
5488 | this->location()) | |
5489 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
5490 | this->location())) | |
5491 | { | |
5492 | this->set_is_error(); | |
5493 | return; | |
5494 | } | |
5495 | } | |
5496 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5497 | { | |
5498 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5499 | { | |
5500 | this->report_error(_("incompatible types in binary expression")); | |
5501 | return; | |
5502 | } | |
5503 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
5504 | this->location())) | |
5505 | { | |
5506 | this->set_is_error(); | |
5507 | return; | |
5508 | } | |
5509 | } | |
5510 | else | |
5511 | { | |
5512 | if (left_type->integer_type() == NULL) | |
5513 | this->report_error(_("shift of non-integer operand")); | |
5514 | ||
5515 | if (!right_type->is_abstract() | |
5516 | && (right_type->integer_type() == NULL | |
5517 | || !right_type->integer_type()->is_unsigned())) | |
5518 | this->report_error(_("shift count not unsigned integer")); | |
5519 | else | |
5520 | { | |
5521 | mpz_t val; | |
5522 | mpz_init(val); | |
5523 | Type* type; | |
5524 | if (this->right_->integer_constant_value(true, val, &type)) | |
5525 | { | |
5526 | if (mpz_sgn(val) < 0) | |
5527 | this->report_error(_("negative shift count")); | |
5528 | } | |
5529 | mpz_clear(val); | |
5530 | } | |
5531 | } | |
5532 | } | |
5533 | ||
5534 | // Get a tree for a binary expression. | |
5535 | ||
5536 | tree | |
5537 | Binary_expression::do_get_tree(Translate_context* context) | |
5538 | { | |
5539 | tree left = this->left_->get_tree(context); | |
5540 | tree right = this->right_->get_tree(context); | |
5541 | ||
5542 | if (left == error_mark_node || right == error_mark_node) | |
5543 | return error_mark_node; | |
5544 | ||
5545 | enum tree_code code; | |
5546 | bool use_left_type = true; | |
5547 | bool is_shift_op = false; | |
5548 | switch (this->op_) | |
5549 | { | |
5550 | case OPERATOR_EQEQ: | |
5551 | case OPERATOR_NOTEQ: | |
5552 | case OPERATOR_LT: | |
5553 | case OPERATOR_LE: | |
5554 | case OPERATOR_GT: | |
5555 | case OPERATOR_GE: | |
5556 | return Expression::comparison_tree(context, this->op_, | |
5557 | this->left_->type(), left, | |
5558 | this->right_->type(), right, | |
5559 | this->location()); | |
5560 | ||
5561 | case OPERATOR_OROR: | |
5562 | code = TRUTH_ORIF_EXPR; | |
5563 | use_left_type = false; | |
5564 | break; | |
5565 | case OPERATOR_ANDAND: | |
5566 | code = TRUTH_ANDIF_EXPR; | |
5567 | use_left_type = false; | |
5568 | break; | |
5569 | case OPERATOR_PLUS: | |
5570 | code = PLUS_EXPR; | |
5571 | break; | |
5572 | case OPERATOR_MINUS: | |
5573 | code = MINUS_EXPR; | |
5574 | break; | |
5575 | case OPERATOR_OR: | |
5576 | code = BIT_IOR_EXPR; | |
5577 | break; | |
5578 | case OPERATOR_XOR: | |
5579 | code = BIT_XOR_EXPR; | |
5580 | break; | |
5581 | case OPERATOR_MULT: | |
5582 | code = MULT_EXPR; | |
5583 | break; | |
5584 | case OPERATOR_DIV: | |
5585 | { | |
5586 | Type *t = this->left_->type(); | |
5587 | if (t->float_type() != NULL || t->complex_type() != NULL) | |
5588 | code = RDIV_EXPR; | |
5589 | else | |
5590 | code = TRUNC_DIV_EXPR; | |
5591 | } | |
5592 | break; | |
5593 | case OPERATOR_MOD: | |
5594 | code = TRUNC_MOD_EXPR; | |
5595 | break; | |
5596 | case OPERATOR_LSHIFT: | |
5597 | code = LSHIFT_EXPR; | |
5598 | is_shift_op = true; | |
5599 | break; | |
5600 | case OPERATOR_RSHIFT: | |
5601 | code = RSHIFT_EXPR; | |
5602 | is_shift_op = true; | |
5603 | break; | |
5604 | case OPERATOR_AND: | |
5605 | code = BIT_AND_EXPR; | |
5606 | break; | |
5607 | case OPERATOR_BITCLEAR: | |
5608 | right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right); | |
5609 | code = BIT_AND_EXPR; | |
5610 | break; | |
5611 | default: | |
5612 | gcc_unreachable(); | |
5613 | } | |
5614 | ||
5615 | tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right); | |
5616 | ||
5617 | if (this->left_->type()->is_string_type()) | |
5618 | { | |
5619 | gcc_assert(this->op_ == OPERATOR_PLUS); | |
5620 | tree string_type = Type::make_string_type()->get_tree(context->gogo()); | |
5621 | static tree string_plus_decl; | |
5622 | return Gogo::call_builtin(&string_plus_decl, | |
5623 | this->location(), | |
5624 | "__go_string_plus", | |
5625 | 2, | |
5626 | string_type, | |
5627 | string_type, | |
5628 | left, | |
5629 | string_type, | |
5630 | right); | |
5631 | } | |
5632 | ||
5633 | tree compute_type = excess_precision_type(type); | |
5634 | if (compute_type != NULL_TREE) | |
5635 | { | |
5636 | left = ::convert(compute_type, left); | |
5637 | right = ::convert(compute_type, right); | |
5638 | } | |
5639 | ||
5640 | tree eval_saved = NULL_TREE; | |
5641 | if (is_shift_op) | |
5642 | { | |
5643 | if (!DECL_P(left)) | |
5644 | left = save_expr(left); | |
5645 | if (!DECL_P(right)) | |
5646 | right = save_expr(right); | |
5647 | // Make sure the values are evaluated. | |
5648 | eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR, | |
5649 | void_type_node, left, right); | |
5650 | } | |
5651 | ||
5652 | tree ret = fold_build2_loc(this->location(), | |
5653 | code, | |
5654 | compute_type != NULL_TREE ? compute_type : type, | |
5655 | left, right); | |
5656 | ||
5657 | if (compute_type != NULL_TREE) | |
5658 | ret = ::convert(type, ret); | |
5659 | ||
5660 | // In Go, a shift larger than the size of the type is well-defined. | |
5661 | // This is not true in GENERIC, so we need to insert a conditional. | |
5662 | if (is_shift_op) | |
5663 | { | |
5664 | gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left))); | |
5665 | gcc_assert(this->left_->type()->integer_type() != NULL); | |
5666 | int bits = TYPE_PRECISION(TREE_TYPE(left)); | |
5667 | ||
5668 | tree compare = fold_build2(LT_EXPR, boolean_type_node, right, | |
5669 | build_int_cst_type(TREE_TYPE(right), bits)); | |
5670 | ||
5671 | tree overflow_result = fold_convert_loc(this->location(), | |
5672 | TREE_TYPE(left), | |
5673 | integer_zero_node); | |
5674 | if (this->op_ == OPERATOR_RSHIFT | |
5675 | && !this->left_->type()->integer_type()->is_unsigned()) | |
5676 | { | |
5677 | tree neg = fold_build2_loc(this->location(), LT_EXPR, | |
5678 | boolean_type_node, left, | |
5679 | fold_convert_loc(this->location(), | |
5680 | TREE_TYPE(left), | |
5681 | integer_zero_node)); | |
5682 | tree neg_one = fold_build2_loc(this->location(), | |
5683 | MINUS_EXPR, TREE_TYPE(left), | |
5684 | fold_convert_loc(this->location(), | |
5685 | TREE_TYPE(left), | |
5686 | integer_zero_node), | |
5687 | fold_convert_loc(this->location(), | |
5688 | TREE_TYPE(left), | |
5689 | integer_one_node)); | |
5690 | overflow_result = fold_build3_loc(this->location(), COND_EXPR, | |
5691 | TREE_TYPE(left), neg, neg_one, | |
5692 | overflow_result); | |
5693 | } | |
5694 | ||
5695 | ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left), | |
5696 | compare, ret, overflow_result); | |
5697 | ||
5698 | ret = fold_build2_loc(this->location(), COMPOUND_EXPR, | |
5699 | TREE_TYPE(ret), eval_saved, ret); | |
5700 | } | |
5701 | ||
5702 | return ret; | |
5703 | } | |
5704 | ||
5705 | // Export a binary expression. | |
5706 | ||
5707 | void | |
5708 | Binary_expression::do_export(Export* exp) const | |
5709 | { | |
5710 | exp->write_c_string("("); | |
5711 | this->left_->export_expression(exp); | |
5712 | switch (this->op_) | |
5713 | { | |
5714 | case OPERATOR_OROR: | |
5715 | exp->write_c_string(" || "); | |
5716 | break; | |
5717 | case OPERATOR_ANDAND: | |
5718 | exp->write_c_string(" && "); | |
5719 | break; | |
5720 | case OPERATOR_EQEQ: | |
5721 | exp->write_c_string(" == "); | |
5722 | break; | |
5723 | case OPERATOR_NOTEQ: | |
5724 | exp->write_c_string(" != "); | |
5725 | break; | |
5726 | case OPERATOR_LT: | |
5727 | exp->write_c_string(" < "); | |
5728 | break; | |
5729 | case OPERATOR_LE: | |
5730 | exp->write_c_string(" <= "); | |
5731 | break; | |
5732 | case OPERATOR_GT: | |
5733 | exp->write_c_string(" > "); | |
5734 | break; | |
5735 | case OPERATOR_GE: | |
5736 | exp->write_c_string(" >= "); | |
5737 | break; | |
5738 | case OPERATOR_PLUS: | |
5739 | exp->write_c_string(" + "); | |
5740 | break; | |
5741 | case OPERATOR_MINUS: | |
5742 | exp->write_c_string(" - "); | |
5743 | break; | |
5744 | case OPERATOR_OR: | |
5745 | exp->write_c_string(" | "); | |
5746 | break; | |
5747 | case OPERATOR_XOR: | |
5748 | exp->write_c_string(" ^ "); | |
5749 | break; | |
5750 | case OPERATOR_MULT: | |
5751 | exp->write_c_string(" * "); | |
5752 | break; | |
5753 | case OPERATOR_DIV: | |
5754 | exp->write_c_string(" / "); | |
5755 | break; | |
5756 | case OPERATOR_MOD: | |
5757 | exp->write_c_string(" % "); | |
5758 | break; | |
5759 | case OPERATOR_LSHIFT: | |
5760 | exp->write_c_string(" << "); | |
5761 | break; | |
5762 | case OPERATOR_RSHIFT: | |
5763 | exp->write_c_string(" >> "); | |
5764 | break; | |
5765 | case OPERATOR_AND: | |
5766 | exp->write_c_string(" & "); | |
5767 | break; | |
5768 | case OPERATOR_BITCLEAR: | |
5769 | exp->write_c_string(" &^ "); | |
5770 | break; | |
5771 | default: | |
5772 | gcc_unreachable(); | |
5773 | } | |
5774 | this->right_->export_expression(exp); | |
5775 | exp->write_c_string(")"); | |
5776 | } | |
5777 | ||
5778 | // Import a binary expression. | |
5779 | ||
5780 | Expression* | |
5781 | Binary_expression::do_import(Import* imp) | |
5782 | { | |
5783 | imp->require_c_string("("); | |
5784 | ||
5785 | Expression* left = Expression::import_expression(imp); | |
5786 | ||
5787 | Operator op; | |
5788 | if (imp->match_c_string(" || ")) | |
5789 | { | |
5790 | op = OPERATOR_OROR; | |
5791 | imp->advance(4); | |
5792 | } | |
5793 | else if (imp->match_c_string(" && ")) | |
5794 | { | |
5795 | op = OPERATOR_ANDAND; | |
5796 | imp->advance(4); | |
5797 | } | |
5798 | else if (imp->match_c_string(" == ")) | |
5799 | { | |
5800 | op = OPERATOR_EQEQ; | |
5801 | imp->advance(4); | |
5802 | } | |
5803 | else if (imp->match_c_string(" != ")) | |
5804 | { | |
5805 | op = OPERATOR_NOTEQ; | |
5806 | imp->advance(4); | |
5807 | } | |
5808 | else if (imp->match_c_string(" < ")) | |
5809 | { | |
5810 | op = OPERATOR_LT; | |
5811 | imp->advance(3); | |
5812 | } | |
5813 | else if (imp->match_c_string(" <= ")) | |
5814 | { | |
5815 | op = OPERATOR_LE; | |
5816 | imp->advance(4); | |
5817 | } | |
5818 | else if (imp->match_c_string(" > ")) | |
5819 | { | |
5820 | op = OPERATOR_GT; | |
5821 | imp->advance(3); | |
5822 | } | |
5823 | else if (imp->match_c_string(" >= ")) | |
5824 | { | |
5825 | op = OPERATOR_GE; | |
5826 | imp->advance(4); | |
5827 | } | |
5828 | else if (imp->match_c_string(" + ")) | |
5829 | { | |
5830 | op = OPERATOR_PLUS; | |
5831 | imp->advance(3); | |
5832 | } | |
5833 | else if (imp->match_c_string(" - ")) | |
5834 | { | |
5835 | op = OPERATOR_MINUS; | |
5836 | imp->advance(3); | |
5837 | } | |
5838 | else if (imp->match_c_string(" | ")) | |
5839 | { | |
5840 | op = OPERATOR_OR; | |
5841 | imp->advance(3); | |
5842 | } | |
5843 | else if (imp->match_c_string(" ^ ")) | |
5844 | { | |
5845 | op = OPERATOR_XOR; | |
5846 | imp->advance(3); | |
5847 | } | |
5848 | else if (imp->match_c_string(" * ")) | |
5849 | { | |
5850 | op = OPERATOR_MULT; | |
5851 | imp->advance(3); | |
5852 | } | |
5853 | else if (imp->match_c_string(" / ")) | |
5854 | { | |
5855 | op = OPERATOR_DIV; | |
5856 | imp->advance(3); | |
5857 | } | |
5858 | else if (imp->match_c_string(" % ")) | |
5859 | { | |
5860 | op = OPERATOR_MOD; | |
5861 | imp->advance(3); | |
5862 | } | |
5863 | else if (imp->match_c_string(" << ")) | |
5864 | { | |
5865 | op = OPERATOR_LSHIFT; | |
5866 | imp->advance(4); | |
5867 | } | |
5868 | else if (imp->match_c_string(" >> ")) | |
5869 | { | |
5870 | op = OPERATOR_RSHIFT; | |
5871 | imp->advance(4); | |
5872 | } | |
5873 | else if (imp->match_c_string(" & ")) | |
5874 | { | |
5875 | op = OPERATOR_AND; | |
5876 | imp->advance(3); | |
5877 | } | |
5878 | else if (imp->match_c_string(" &^ ")) | |
5879 | { | |
5880 | op = OPERATOR_BITCLEAR; | |
5881 | imp->advance(4); | |
5882 | } | |
5883 | else | |
5884 | { | |
5885 | error_at(imp->location(), "unrecognized binary operator"); | |
5886 | return Expression::make_error(imp->location()); | |
5887 | } | |
5888 | ||
5889 | Expression* right = Expression::import_expression(imp); | |
5890 | ||
5891 | imp->require_c_string(")"); | |
5892 | ||
5893 | return Expression::make_binary(op, left, right, imp->location()); | |
5894 | } | |
5895 | ||
5896 | // Make a binary expression. | |
5897 | ||
5898 | Expression* | |
5899 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
5900 | source_location location) | |
5901 | { | |
5902 | return new Binary_expression(op, left, right, location); | |
5903 | } | |
5904 | ||
5905 | // Implement a comparison. | |
5906 | ||
5907 | tree | |
5908 | Expression::comparison_tree(Translate_context* context, Operator op, | |
5909 | Type* left_type, tree left_tree, | |
5910 | Type* right_type, tree right_tree, | |
5911 | source_location location) | |
5912 | { | |
5913 | enum tree_code code; | |
5914 | switch (op) | |
5915 | { | |
5916 | case OPERATOR_EQEQ: | |
5917 | code = EQ_EXPR; | |
5918 | break; | |
5919 | case OPERATOR_NOTEQ: | |
5920 | code = NE_EXPR; | |
5921 | break; | |
5922 | case OPERATOR_LT: | |
5923 | code = LT_EXPR; | |
5924 | break; | |
5925 | case OPERATOR_LE: | |
5926 | code = LE_EXPR; | |
5927 | break; | |
5928 | case OPERATOR_GT: | |
5929 | code = GT_EXPR; | |
5930 | break; | |
5931 | case OPERATOR_GE: | |
5932 | code = GE_EXPR; | |
5933 | break; | |
5934 | default: | |
5935 | gcc_unreachable(); | |
5936 | } | |
5937 | ||
5938 | if (left_type->is_string_type()) | |
5939 | { | |
5940 | gcc_assert(right_type->is_string_type()); | |
5941 | tree string_type = Type::make_string_type()->get_tree(context->gogo()); | |
5942 | static tree string_compare_decl; | |
5943 | left_tree = Gogo::call_builtin(&string_compare_decl, | |
5944 | location, | |
5945 | "__go_strcmp", | |
5946 | 2, | |
5947 | integer_type_node, | |
5948 | string_type, | |
5949 | left_tree, | |
5950 | string_type, | |
5951 | right_tree); | |
5952 | right_tree = build_int_cst_type(integer_type_node, 0); | |
5953 | } | |
5954 | ||
5955 | if ((left_type->interface_type() != NULL | |
5956 | && right_type->interface_type() == NULL | |
5957 | && !right_type->is_nil_type()) | |
5958 | || (left_type->interface_type() == NULL | |
5959 | && !left_type->is_nil_type() | |
5960 | && right_type->interface_type() != NULL)) | |
5961 | { | |
5962 | // Comparing an interface value to a non-interface value. | |
5963 | if (left_type->interface_type() == NULL) | |
5964 | { | |
5965 | std::swap(left_type, right_type); | |
5966 | std::swap(left_tree, right_tree); | |
5967 | } | |
5968 | ||
5969 | // The right operand is not an interface. We need to take its | |
5970 | // address if it is not a pointer. | |
5971 | tree make_tmp; | |
5972 | tree arg; | |
5973 | if (right_type->points_to() != NULL) | |
5974 | { | |
5975 | make_tmp = NULL_TREE; | |
5976 | arg = right_tree; | |
5977 | } | |
5978 | else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree)) | |
5979 | { | |
5980 | make_tmp = NULL_TREE; | |
5981 | arg = build_fold_addr_expr_loc(location, right_tree); | |
5982 | if (DECL_P(right_tree)) | |
5983 | TREE_ADDRESSABLE(right_tree) = 1; | |
5984 | } | |
5985 | else | |
5986 | { | |
5987 | tree tmp = create_tmp_var(TREE_TYPE(right_tree), | |
5988 | get_name(right_tree)); | |
5989 | DECL_IGNORED_P(tmp) = 0; | |
5990 | DECL_INITIAL(tmp) = right_tree; | |
5991 | TREE_ADDRESSABLE(tmp) = 1; | |
5992 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
5993 | SET_EXPR_LOCATION(make_tmp, location); | |
5994 | arg = build_fold_addr_expr_loc(location, tmp); | |
5995 | } | |
5996 | arg = fold_convert_loc(location, ptr_type_node, arg); | |
5997 | ||
5998 | tree descriptor = right_type->type_descriptor_pointer(context->gogo()); | |
5999 | ||
6000 | if (left_type->interface_type()->is_empty()) | |
6001 | { | |
6002 | static tree empty_interface_value_compare_decl; | |
6003 | left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl, | |
6004 | location, | |
6005 | "__go_empty_interface_value_compare", | |
6006 | 3, | |
6007 | integer_type_node, | |
6008 | TREE_TYPE(left_tree), | |
6009 | left_tree, | |
6010 | TREE_TYPE(descriptor), | |
6011 | descriptor, | |
6012 | ptr_type_node, | |
6013 | arg); | |
6014 | // This can panic if the type is not comparable. | |
6015 | TREE_NOTHROW(empty_interface_value_compare_decl) = 0; | |
6016 | } | |
6017 | else | |
6018 | { | |
6019 | static tree interface_value_compare_decl; | |
6020 | left_tree = Gogo::call_builtin(&interface_value_compare_decl, | |
6021 | location, | |
6022 | "__go_interface_value_compare", | |
6023 | 3, | |
6024 | integer_type_node, | |
6025 | TREE_TYPE(left_tree), | |
6026 | left_tree, | |
6027 | TREE_TYPE(descriptor), | |
6028 | descriptor, | |
6029 | ptr_type_node, | |
6030 | arg); | |
6031 | // This can panic if the type is not comparable. | |
6032 | TREE_NOTHROW(interface_value_compare_decl) = 0; | |
6033 | } | |
6034 | right_tree = build_int_cst_type(integer_type_node, 0); | |
6035 | ||
6036 | if (make_tmp != NULL_TREE) | |
6037 | left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp, | |
6038 | left_tree); | |
6039 | } | |
6040 | else if (left_type->interface_type() != NULL | |
6041 | && right_type->interface_type() != NULL) | |
6042 | { | |
6043 | if (left_type->interface_type()->is_empty()) | |
6044 | { | |
6045 | gcc_assert(right_type->interface_type()->is_empty()); | |
6046 | static tree empty_interface_compare_decl; | |
6047 | left_tree = Gogo::call_builtin(&empty_interface_compare_decl, | |
6048 | location, | |
6049 | "__go_empty_interface_compare", | |
6050 | 2, | |
6051 | integer_type_node, | |
6052 | TREE_TYPE(left_tree), | |
6053 | left_tree, | |
6054 | TREE_TYPE(right_tree), | |
6055 | right_tree); | |
6056 | // This can panic if the type is uncomparable. | |
6057 | TREE_NOTHROW(empty_interface_compare_decl) = 0; | |
6058 | } | |
6059 | else | |
6060 | { | |
6061 | gcc_assert(!right_type->interface_type()->is_empty()); | |
6062 | static tree interface_compare_decl; | |
6063 | left_tree = Gogo::call_builtin(&interface_compare_decl, | |
6064 | location, | |
6065 | "__go_interface_compare", | |
6066 | 2, | |
6067 | integer_type_node, | |
6068 | TREE_TYPE(left_tree), | |
6069 | left_tree, | |
6070 | TREE_TYPE(right_tree), | |
6071 | right_tree); | |
6072 | // This can panic if the type is uncomparable. | |
6073 | TREE_NOTHROW(interface_compare_decl) = 0; | |
6074 | } | |
6075 | right_tree = build_int_cst_type(integer_type_node, 0); | |
6076 | } | |
6077 | ||
6078 | if (left_type->is_nil_type() | |
6079 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6080 | { | |
6081 | std::swap(left_type, right_type); | |
6082 | std::swap(left_tree, right_tree); | |
6083 | } | |
6084 | ||
6085 | if (right_type->is_nil_type()) | |
6086 | { | |
6087 | if (left_type->array_type() != NULL | |
6088 | && left_type->array_type()->length() == NULL) | |
6089 | { | |
6090 | Array_type* at = left_type->array_type(); | |
6091 | left_tree = at->value_pointer_tree(context->gogo(), left_tree); | |
6092 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6093 | } | |
6094 | else if (left_type->interface_type() != NULL) | |
6095 | { | |
6096 | // An interface is nil if the first field is nil. | |
6097 | tree left_type_tree = TREE_TYPE(left_tree); | |
6098 | gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE); | |
6099 | tree field = TYPE_FIELDS(left_type_tree); | |
6100 | left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree, | |
6101 | field, NULL_TREE); | |
6102 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6103 | } | |
6104 | else | |
6105 | { | |
6106 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree))); | |
6107 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6108 | } | |
6109 | } | |
6110 | ||
6111 | tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree); | |
6112 | if (CAN_HAVE_LOCATION_P(ret)) | |
6113 | SET_EXPR_LOCATION(ret, location); | |
6114 | return ret; | |
6115 | } | |
6116 | ||
6117 | // Class Bound_method_expression. | |
6118 | ||
6119 | // Traversal. | |
6120 | ||
6121 | int | |
6122 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6123 | { | |
6124 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
6125 | return TRAVERSE_EXIT; | |
6126 | return Expression::traverse(&this->method_, traverse); | |
6127 | } | |
6128 | ||
6129 | // Return the type of a bound method expression. The type of this | |
6130 | // object is really the type of the method with no receiver. We | |
6131 | // should be able to get away with just returning the type of the | |
6132 | // method. | |
6133 | ||
6134 | Type* | |
6135 | Bound_method_expression::do_type() | |
6136 | { | |
6137 | return this->method_->type(); | |
6138 | } | |
6139 | ||
6140 | // Determine the types of a method expression. | |
6141 | ||
6142 | void | |
6143 | Bound_method_expression::do_determine_type(const Type_context*) | |
6144 | { | |
6145 | this->method_->determine_type_no_context(); | |
6146 | Type* mtype = this->method_->type(); | |
6147 | Function_type* fntype = mtype == NULL ? NULL : mtype->function_type(); | |
6148 | if (fntype == NULL || !fntype->is_method()) | |
6149 | this->expr_->determine_type_no_context(); | |
6150 | else | |
6151 | { | |
6152 | Type_context subcontext(fntype->receiver()->type(), false); | |
6153 | this->expr_->determine_type(&subcontext); | |
6154 | } | |
6155 | } | |
6156 | ||
6157 | // Check the types of a method expression. | |
6158 | ||
6159 | void | |
6160 | Bound_method_expression::do_check_types(Gogo*) | |
6161 | { | |
6162 | Type* type = this->method_->type()->deref(); | |
6163 | if (type == NULL | |
6164 | || type->function_type() == NULL | |
6165 | || !type->function_type()->is_method()) | |
6166 | this->report_error(_("object is not a method")); | |
6167 | else | |
6168 | { | |
6169 | Type* rtype = type->function_type()->receiver()->type()->deref(); | |
6170 | Type* etype = (this->expr_type_ != NULL | |
6171 | ? this->expr_type_ | |
6172 | : this->expr_->type()); | |
6173 | etype = etype->deref(); | |
6174 | if (!Type::are_identical(rtype, etype, NULL)) | |
6175 | this->report_error(_("method type does not match object type")); | |
6176 | } | |
6177 | } | |
6178 | ||
6179 | // Get the tree for a method expression. There is no standard tree | |
6180 | // representation for this. The only places it may currently be used | |
6181 | // are in a Call_expression or a Go_statement, which will take it | |
6182 | // apart directly. So this has nothing to do at present. | |
6183 | ||
6184 | tree | |
6185 | Bound_method_expression::do_get_tree(Translate_context*) | |
6186 | { | |
6187 | gcc_unreachable(); | |
6188 | } | |
6189 | ||
6190 | // Make a method expression. | |
6191 | ||
6192 | Bound_method_expression* | |
6193 | Expression::make_bound_method(Expression* expr, Expression* method, | |
6194 | source_location location) | |
6195 | { | |
6196 | return new Bound_method_expression(expr, method, location); | |
6197 | } | |
6198 | ||
6199 | // Class Builtin_call_expression. This is used for a call to a | |
6200 | // builtin function. | |
6201 | ||
6202 | class Builtin_call_expression : public Call_expression | |
6203 | { | |
6204 | public: | |
6205 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
6206 | bool is_varargs, source_location location); | |
6207 | ||
6208 | protected: | |
6209 | // This overrides Call_expression::do_lower. | |
6210 | Expression* | |
6211 | do_lower(Gogo*, Named_object*, int); | |
6212 | ||
6213 | bool | |
6214 | do_is_constant() const; | |
6215 | ||
6216 | bool | |
6217 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
6218 | ||
6219 | bool | |
6220 | do_float_constant_value(mpfr_t, Type**) const; | |
6221 | ||
6222 | bool | |
6223 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
6224 | ||
6225 | Type* | |
6226 | do_type(); | |
6227 | ||
6228 | void | |
6229 | do_determine_type(const Type_context*); | |
6230 | ||
6231 | void | |
6232 | do_check_types(Gogo*); | |
6233 | ||
6234 | Expression* | |
6235 | do_copy() | |
6236 | { | |
6237 | return new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
6238 | this->args()->copy(), | |
6239 | this->is_varargs(), | |
6240 | this->location()); | |
6241 | } | |
6242 | ||
6243 | tree | |
6244 | do_get_tree(Translate_context*); | |
6245 | ||
6246 | void | |
6247 | do_export(Export*) const; | |
6248 | ||
6249 | virtual bool | |
6250 | do_is_recover_call() const; | |
6251 | ||
6252 | virtual void | |
6253 | do_set_recover_arg(Expression*); | |
6254 | ||
6255 | private: | |
6256 | // The builtin functions. | |
6257 | enum Builtin_function_code | |
6258 | { | |
6259 | BUILTIN_INVALID, | |
6260 | ||
6261 | // Predeclared builtin functions. | |
6262 | BUILTIN_APPEND, | |
6263 | BUILTIN_CAP, | |
6264 | BUILTIN_CLOSE, | |
6265 | BUILTIN_CLOSED, | |
6266 | BUILTIN_CMPLX, | |
6267 | BUILTIN_COPY, | |
6268 | BUILTIN_IMAG, | |
6269 | BUILTIN_LEN, | |
6270 | BUILTIN_MAKE, | |
6271 | BUILTIN_NEW, | |
6272 | BUILTIN_PANIC, | |
6273 | BUILTIN_PRINT, | |
6274 | BUILTIN_PRINTLN, | |
6275 | BUILTIN_REAL, | |
6276 | BUILTIN_RECOVER, | |
6277 | ||
6278 | // Builtin functions from the unsafe package. | |
6279 | BUILTIN_ALIGNOF, | |
6280 | BUILTIN_OFFSETOF, | |
6281 | BUILTIN_SIZEOF | |
6282 | }; | |
6283 | ||
6284 | Expression* | |
6285 | one_arg() const; | |
6286 | ||
6287 | bool | |
6288 | check_one_arg(); | |
6289 | ||
6290 | static Type* | |
6291 | real_imag_type(Type*); | |
6292 | ||
6293 | static Type* | |
6294 | cmplx_type(Type*); | |
6295 | ||
6296 | // A pointer back to the general IR structure. This avoids a global | |
6297 | // variable, or passing it around everywhere. | |
6298 | Gogo* gogo_; | |
6299 | // The builtin function being called. | |
6300 | Builtin_function_code code_; | |
6301 | }; | |
6302 | ||
6303 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
6304 | Expression* fn, | |
6305 | Expression_list* args, | |
6306 | bool is_varargs, | |
6307 | source_location location) | |
6308 | : Call_expression(fn, args, is_varargs, location), | |
6309 | gogo_(gogo), code_(BUILTIN_INVALID) | |
6310 | { | |
6311 | Func_expression* fnexp = this->fn()->func_expression(); | |
6312 | gcc_assert(fnexp != NULL); | |
6313 | const std::string& name(fnexp->named_object()->name()); | |
6314 | if (name == "append") | |
6315 | this->code_ = BUILTIN_APPEND; | |
6316 | else if (name == "cap") | |
6317 | this->code_ = BUILTIN_CAP; | |
6318 | else if (name == "close") | |
6319 | this->code_ = BUILTIN_CLOSE; | |
6320 | else if (name == "closed") | |
6321 | this->code_ = BUILTIN_CLOSED; | |
6322 | else if (name == "cmplx") | |
6323 | this->code_ = BUILTIN_CMPLX; | |
6324 | else if (name == "copy") | |
6325 | this->code_ = BUILTIN_COPY; | |
6326 | else if (name == "imag") | |
6327 | this->code_ = BUILTIN_IMAG; | |
6328 | else if (name == "len") | |
6329 | this->code_ = BUILTIN_LEN; | |
6330 | else if (name == "make") | |
6331 | this->code_ = BUILTIN_MAKE; | |
6332 | else if (name == "new") | |
6333 | this->code_ = BUILTIN_NEW; | |
6334 | else if (name == "panic") | |
6335 | this->code_ = BUILTIN_PANIC; | |
6336 | else if (name == "print") | |
6337 | this->code_ = BUILTIN_PRINT; | |
6338 | else if (name == "println") | |
6339 | this->code_ = BUILTIN_PRINTLN; | |
6340 | else if (name == "real") | |
6341 | this->code_ = BUILTIN_REAL; | |
6342 | else if (name == "recover") | |
6343 | this->code_ = BUILTIN_RECOVER; | |
6344 | else if (name == "Alignof") | |
6345 | this->code_ = BUILTIN_ALIGNOF; | |
6346 | else if (name == "Offsetof") | |
6347 | this->code_ = BUILTIN_OFFSETOF; | |
6348 | else if (name == "Sizeof") | |
6349 | this->code_ = BUILTIN_SIZEOF; | |
6350 | else | |
6351 | gcc_unreachable(); | |
6352 | } | |
6353 | ||
6354 | // Return whether this is a call to recover. This is a virtual | |
6355 | // function called from the parent class. | |
6356 | ||
6357 | bool | |
6358 | Builtin_call_expression::do_is_recover_call() const | |
6359 | { | |
6360 | if (this->classification() == EXPRESSION_ERROR) | |
6361 | return false; | |
6362 | return this->code_ == BUILTIN_RECOVER; | |
6363 | } | |
6364 | ||
6365 | // Set the argument for a call to recover. | |
6366 | ||
6367 | void | |
6368 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
6369 | { | |
6370 | const Expression_list* args = this->args(); | |
6371 | gcc_assert(args == NULL || args->empty()); | |
6372 | Expression_list* new_args = new Expression_list(); | |
6373 | new_args->push_back(arg); | |
6374 | this->set_args(new_args); | |
6375 | } | |
6376 | ||
6377 | // A traversal class which looks for a call expression. | |
6378 | ||
6379 | class Find_call_expression : public Traverse | |
6380 | { | |
6381 | public: | |
6382 | Find_call_expression() | |
6383 | : Traverse(traverse_expressions), | |
6384 | found_(false) | |
6385 | { } | |
6386 | ||
6387 | int | |
6388 | expression(Expression**); | |
6389 | ||
6390 | bool | |
6391 | found() | |
6392 | { return this->found_; } | |
6393 | ||
6394 | private: | |
6395 | bool found_; | |
6396 | }; | |
6397 | ||
6398 | int | |
6399 | Find_call_expression::expression(Expression** pexpr) | |
6400 | { | |
6401 | if ((*pexpr)->call_expression() != NULL) | |
6402 | { | |
6403 | this->found_ = true; | |
6404 | return TRAVERSE_EXIT; | |
6405 | } | |
6406 | return TRAVERSE_CONTINUE; | |
6407 | } | |
6408 | ||
6409 | // Lower a builtin call expression. This turns new and make into | |
6410 | // specific expressions. We also convert to a constant if we can. | |
6411 | ||
6412 | Expression* | |
6413 | Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
6414 | { | |
6415 | if (this->code_ == BUILTIN_NEW) | |
6416 | { | |
6417 | const Expression_list* args = this->args(); | |
6418 | if (args == NULL || args->size() < 1) | |
6419 | this->report_error(_("not enough arguments")); | |
6420 | else if (args->size() > 1) | |
6421 | this->report_error(_("too many arguments")); | |
6422 | else | |
6423 | { | |
6424 | Expression* arg = args->front(); | |
6425 | if (!arg->is_type_expression()) | |
6426 | { | |
6427 | error_at(arg->location(), "expected type"); | |
6428 | this->set_is_error(); | |
6429 | } | |
6430 | else | |
6431 | return Expression::make_allocation(arg->type(), this->location()); | |
6432 | } | |
6433 | } | |
6434 | else if (this->code_ == BUILTIN_MAKE) | |
6435 | { | |
6436 | const Expression_list* args = this->args(); | |
6437 | if (args == NULL || args->size() < 1) | |
6438 | this->report_error(_("not enough arguments")); | |
6439 | else | |
6440 | { | |
6441 | Expression* arg = args->front(); | |
6442 | if (!arg->is_type_expression()) | |
6443 | { | |
6444 | error_at(arg->location(), "expected type"); | |
6445 | this->set_is_error(); | |
6446 | } | |
6447 | else | |
6448 | { | |
6449 | Expression_list* newargs; | |
6450 | if (args->size() == 1) | |
6451 | newargs = NULL; | |
6452 | else | |
6453 | { | |
6454 | newargs = new Expression_list(); | |
6455 | Expression_list::const_iterator p = args->begin(); | |
6456 | ++p; | |
6457 | for (; p != args->end(); ++p) | |
6458 | newargs->push_back(*p); | |
6459 | } | |
6460 | return Expression::make_make(arg->type(), newargs, | |
6461 | this->location()); | |
6462 | } | |
6463 | } | |
6464 | } | |
6465 | else if (this->is_constant()) | |
6466 | { | |
6467 | // We can only lower len and cap if there are no function calls | |
6468 | // in the arguments. Otherwise we have to make the call. | |
6469 | if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP) | |
6470 | { | |
6471 | Expression* arg = this->one_arg(); | |
6472 | if (!arg->is_constant()) | |
6473 | { | |
6474 | Find_call_expression find_call; | |
6475 | Expression::traverse(&arg, &find_call); | |
6476 | if (find_call.found()) | |
6477 | return this; | |
6478 | } | |
6479 | } | |
6480 | ||
6481 | mpz_t ival; | |
6482 | mpz_init(ival); | |
6483 | Type* type; | |
6484 | if (this->integer_constant_value(true, ival, &type)) | |
6485 | { | |
6486 | Expression* ret = Expression::make_integer(&ival, type, | |
6487 | this->location()); | |
6488 | mpz_clear(ival); | |
6489 | return ret; | |
6490 | } | |
6491 | mpz_clear(ival); | |
6492 | ||
6493 | mpfr_t rval; | |
6494 | mpfr_init(rval); | |
6495 | if (this->float_constant_value(rval, &type)) | |
6496 | { | |
6497 | Expression* ret = Expression::make_float(&rval, type, | |
6498 | this->location()); | |
6499 | mpfr_clear(rval); | |
6500 | return ret; | |
6501 | } | |
6502 | ||
6503 | mpfr_t imag; | |
6504 | mpfr_init(imag); | |
6505 | if (this->complex_constant_value(rval, imag, &type)) | |
6506 | { | |
6507 | Expression* ret = Expression::make_complex(&rval, &imag, type, | |
6508 | this->location()); | |
6509 | mpfr_clear(rval); | |
6510 | mpfr_clear(imag); | |
6511 | return ret; | |
6512 | } | |
6513 | mpfr_clear(rval); | |
6514 | mpfr_clear(imag); | |
6515 | } | |
6516 | else if (this->code_ == BUILTIN_RECOVER) | |
6517 | { | |
6518 | if (function != NULL) | |
6519 | function->func_value()->set_calls_recover(); | |
6520 | else | |
6521 | { | |
6522 | // Calling recover outside of a function always returns the | |
6523 | // nil empty interface. | |
6524 | Type* eface = Type::make_interface_type(NULL, this->location()); | |
6525 | return Expression::make_cast(eface, | |
6526 | Expression::make_nil(this->location()), | |
6527 | this->location()); | |
6528 | } | |
6529 | } | |
6530 | else if (this->code_ == BUILTIN_APPEND) | |
6531 | { | |
6532 | // Lower the varargs. | |
6533 | const Expression_list* args = this->args(); | |
6534 | if (args == NULL || args->empty()) | |
6535 | return this; | |
6536 | Type* slice_type = args->front()->type(); | |
6537 | if (!slice_type->is_open_array_type()) | |
6538 | { | |
6539 | error_at(args->front()->location(), "argument 1 must be a slice"); | |
6540 | this->set_is_error(); | |
6541 | return this; | |
6542 | } | |
6543 | return this->lower_varargs(gogo, function, slice_type, 2); | |
6544 | } | |
6545 | ||
6546 | return this; | |
6547 | } | |
6548 | ||
6549 | // Return the type of the real or imag functions, given the type of | |
6550 | // the argument. We need to map complex to float, complex64 to | |
6551 | // float32, and complex128 to float64, so it has to be done by name. | |
6552 | // This returns NULL if it can't figure out the type. | |
6553 | ||
6554 | Type* | |
6555 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
6556 | { | |
6557 | if (arg_type == NULL || arg_type->is_abstract()) | |
6558 | return NULL; | |
6559 | Named_type* nt = arg_type->named_type(); | |
6560 | if (nt == NULL) | |
6561 | return NULL; | |
6562 | while (nt->real_type()->named_type() != NULL) | |
6563 | nt = nt->real_type()->named_type(); | |
6564 | if (nt->name() == "complex") | |
6565 | return Type::lookup_float_type("float"); | |
6566 | else if (nt->name() == "complex64") | |
6567 | return Type::lookup_float_type("float32"); | |
6568 | else if (nt->name() == "complex128") | |
6569 | return Type::lookup_float_type("float64"); | |
6570 | else | |
6571 | return NULL; | |
6572 | } | |
6573 | ||
6574 | // Return the type of the cmplx function, given the type of one of the | |
6575 | // argments. Like real_imag_type, we have to map by name. | |
6576 | ||
6577 | Type* | |
6578 | Builtin_call_expression::cmplx_type(Type* arg_type) | |
6579 | { | |
6580 | if (arg_type == NULL || arg_type->is_abstract()) | |
6581 | return NULL; | |
6582 | Named_type* nt = arg_type->named_type(); | |
6583 | if (nt == NULL) | |
6584 | return NULL; | |
6585 | while (nt->real_type()->named_type() != NULL) | |
6586 | nt = nt->real_type()->named_type(); | |
6587 | if (nt->name() == "float") | |
6588 | return Type::lookup_complex_type("complex"); | |
6589 | else if (nt->name() == "float32") | |
6590 | return Type::lookup_complex_type("complex64"); | |
6591 | else if (nt->name() == "float64") | |
6592 | return Type::lookup_complex_type("complex128"); | |
6593 | else | |
6594 | return NULL; | |
6595 | } | |
6596 | ||
6597 | // Return a single argument, or NULL if there isn't one. | |
6598 | ||
6599 | Expression* | |
6600 | Builtin_call_expression::one_arg() const | |
6601 | { | |
6602 | const Expression_list* args = this->args(); | |
6603 | if (args->size() != 1) | |
6604 | return NULL; | |
6605 | return args->front(); | |
6606 | } | |
6607 | ||
6608 | // Return whether this is constant: len of a string, or len or cap of | |
6609 | // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof. | |
6610 | ||
6611 | bool | |
6612 | Builtin_call_expression::do_is_constant() const | |
6613 | { | |
6614 | switch (this->code_) | |
6615 | { | |
6616 | case BUILTIN_LEN: | |
6617 | case BUILTIN_CAP: | |
6618 | { | |
6619 | Expression* arg = this->one_arg(); | |
6620 | if (arg == NULL) | |
6621 | return false; | |
6622 | Type* arg_type = arg->type(); | |
6623 | ||
6624 | if (arg_type->points_to() != NULL | |
6625 | && arg_type->points_to()->array_type() != NULL | |
6626 | && !arg_type->points_to()->is_open_array_type()) | |
6627 | arg_type = arg_type->points_to(); | |
6628 | ||
6629 | if (arg_type->array_type() != NULL | |
6630 | && arg_type->array_type()->length() != NULL) | |
6631 | return arg_type->array_type()->length()->is_constant(); | |
6632 | ||
6633 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
6634 | return arg->is_constant(); | |
6635 | } | |
6636 | break; | |
6637 | ||
6638 | case BUILTIN_SIZEOF: | |
6639 | case BUILTIN_ALIGNOF: | |
6640 | return this->one_arg() != NULL; | |
6641 | ||
6642 | case BUILTIN_OFFSETOF: | |
6643 | { | |
6644 | Expression* arg = this->one_arg(); | |
6645 | if (arg == NULL) | |
6646 | return false; | |
6647 | return arg->field_reference_expression() != NULL; | |
6648 | } | |
6649 | ||
6650 | case BUILTIN_CMPLX: | |
6651 | { | |
6652 | const Expression_list* args = this->args(); | |
6653 | if (args != NULL && args->size() == 2) | |
6654 | return args->front()->is_constant() && args->back()->is_constant(); | |
6655 | } | |
6656 | break; | |
6657 | ||
6658 | case BUILTIN_REAL: | |
6659 | case BUILTIN_IMAG: | |
6660 | { | |
6661 | Expression* arg = this->one_arg(); | |
6662 | return arg != NULL && arg->is_constant(); | |
6663 | } | |
6664 | ||
6665 | default: | |
6666 | break; | |
6667 | } | |
6668 | ||
6669 | return false; | |
6670 | } | |
6671 | ||
6672 | // Return an integer constant value if possible. | |
6673 | ||
6674 | bool | |
6675 | Builtin_call_expression::do_integer_constant_value(bool iota_is_constant, | |
6676 | mpz_t val, | |
6677 | Type** ptype) const | |
6678 | { | |
6679 | if (this->code_ == BUILTIN_LEN | |
6680 | || this->code_ == BUILTIN_CAP) | |
6681 | { | |
6682 | Expression* arg = this->one_arg(); | |
6683 | if (arg == NULL) | |
6684 | return false; | |
6685 | Type* arg_type = arg->type(); | |
6686 | ||
6687 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
6688 | { | |
6689 | std::string sval; | |
6690 | if (arg->string_constant_value(&sval)) | |
6691 | { | |
6692 | mpz_set_ui(val, sval.length()); | |
6693 | *ptype = Type::lookup_integer_type("int"); | |
6694 | return true; | |
6695 | } | |
6696 | } | |
6697 | ||
6698 | if (arg_type->points_to() != NULL | |
6699 | && arg_type->points_to()->array_type() != NULL | |
6700 | && !arg_type->points_to()->is_open_array_type()) | |
6701 | arg_type = arg_type->points_to(); | |
6702 | ||
6703 | if (arg_type->array_type() != NULL | |
6704 | && arg_type->array_type()->length() != NULL) | |
6705 | { | |
6706 | Expression* e = arg_type->array_type()->length(); | |
6707 | if (e->integer_constant_value(iota_is_constant, val, ptype)) | |
6708 | { | |
6709 | *ptype = Type::lookup_integer_type("int"); | |
6710 | return true; | |
6711 | } | |
6712 | } | |
6713 | } | |
6714 | else if (this->code_ == BUILTIN_SIZEOF | |
6715 | || this->code_ == BUILTIN_ALIGNOF) | |
6716 | { | |
6717 | Expression* arg = this->one_arg(); | |
6718 | if (arg == NULL) | |
6719 | return false; | |
6720 | Type* arg_type = arg->type(); | |
6721 | if (arg_type->is_error_type()) | |
6722 | return false; | |
6723 | if (arg_type->is_abstract()) | |
6724 | return false; | |
6725 | tree arg_type_tree = arg_type->get_tree(this->gogo_); | |
6726 | unsigned long val_long; | |
6727 | if (this->code_ == BUILTIN_SIZEOF) | |
6728 | { | |
6729 | tree type_size = TYPE_SIZE_UNIT(arg_type_tree); | |
6730 | gcc_assert(TREE_CODE(type_size) == INTEGER_CST); | |
6731 | if (TREE_INT_CST_HIGH(type_size) != 0) | |
6732 | return false; | |
6733 | unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size); | |
6734 | val_long = static_cast<unsigned long>(val_wide); | |
6735 | if (val_long != val_wide) | |
6736 | return false; | |
6737 | } | |
6738 | else if (this->code_ == BUILTIN_ALIGNOF) | |
6739 | { | |
6740 | val_long = TYPE_ALIGN(arg_type_tree); | |
6741 | if (arg->field_reference_expression() != NULL) | |
6742 | { | |
6743 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
6744 | // the type of f when it is used as a field in a struct. | |
6745 | #ifdef BIGGEST_FIELD_ALIGNMENT | |
6746 | if (val_long > BIGGEST_FIELD_ALIGNMENT) | |
6747 | val_long = BIGGEST_FIELD_ALIGNMENT; | |
6748 | #endif | |
6749 | #ifdef ADJUST_FIELD_ALIGN | |
6750 | // A separate declaration avoids a warning promoted to | |
6751 | // an error if ADJUST_FIELD_ALIGN ignores FIELD. | |
6752 | tree field; | |
6753 | field = build_decl(UNKNOWN_LOCATION, FIELD_DECL, NULL, | |
6754 | arg_type_tree); | |
6755 | val_long = ADJUST_FIELD_ALIGN(field, val_long); | |
6756 | #endif | |
6757 | } | |
6758 | val_long /= BITS_PER_UNIT; | |
6759 | } | |
6760 | else | |
6761 | gcc_unreachable(); | |
6762 | mpz_set_ui(val, val_long); | |
6763 | *ptype = NULL; | |
6764 | return true; | |
6765 | } | |
6766 | else if (this->code_ == BUILTIN_OFFSETOF) | |
6767 | { | |
6768 | Expression* arg = this->one_arg(); | |
6769 | if (arg == NULL) | |
6770 | return false; | |
6771 | Field_reference_expression* farg = arg->field_reference_expression(); | |
6772 | if (farg == NULL) | |
6773 | return false; | |
6774 | Expression* struct_expr = farg->expr(); | |
6775 | Type* st = struct_expr->type(); | |
6776 | if (st->struct_type() == NULL) | |
6777 | return false; | |
6778 | tree struct_tree = st->get_tree(this->gogo_); | |
6779 | gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE); | |
6780 | tree field = TYPE_FIELDS(struct_tree); | |
6781 | for (unsigned int index = farg->field_index(); index > 0; --index) | |
6782 | { | |
6783 | field = DECL_CHAIN(field); | |
6784 | gcc_assert(field != NULL_TREE); | |
6785 | } | |
6786 | HOST_WIDE_INT offset_wide = int_byte_position (field); | |
6787 | if (offset_wide < 0) | |
6788 | return false; | |
6789 | unsigned long offset_long = static_cast<unsigned long>(offset_wide); | |
6790 | if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide)) | |
6791 | return false; | |
6792 | mpz_set_ui(val, offset_long); | |
6793 | return true; | |
6794 | } | |
6795 | return false; | |
6796 | } | |
6797 | ||
6798 | // Return a floating point constant value if possible. | |
6799 | ||
6800 | bool | |
6801 | Builtin_call_expression::do_float_constant_value(mpfr_t val, | |
6802 | Type** ptype) const | |
6803 | { | |
6804 | if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) | |
6805 | { | |
6806 | Expression* arg = this->one_arg(); | |
6807 | if (arg == NULL) | |
6808 | return false; | |
6809 | ||
6810 | mpfr_t real; | |
6811 | mpfr_t imag; | |
6812 | mpfr_init(real); | |
6813 | mpfr_init(imag); | |
6814 | ||
6815 | bool ret = false; | |
6816 | Type* type; | |
6817 | if (arg->complex_constant_value(real, imag, &type)) | |
6818 | { | |
6819 | if (this->code_ == BUILTIN_REAL) | |
6820 | mpfr_set(val, real, GMP_RNDN); | |
6821 | else | |
6822 | mpfr_set(val, imag, GMP_RNDN); | |
6823 | *ptype = Builtin_call_expression::real_imag_type(type); | |
6824 | ret = true; | |
6825 | } | |
6826 | ||
6827 | mpfr_clear(real); | |
6828 | mpfr_clear(imag); | |
6829 | return ret; | |
6830 | } | |
6831 | ||
6832 | return false; | |
6833 | } | |
6834 | ||
6835 | // Return a complex constant value if possible. | |
6836 | ||
6837 | bool | |
6838 | Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
6839 | Type** ptype) const | |
6840 | { | |
6841 | if (this->code_ == BUILTIN_CMPLX) | |
6842 | { | |
6843 | const Expression_list* args = this->args(); | |
6844 | if (args == NULL || args->size() != 2) | |
6845 | return false; | |
6846 | ||
6847 | mpfr_t r; | |
6848 | mpfr_init(r); | |
6849 | Type* rtype; | |
6850 | if (!args->front()->float_constant_value(r, &rtype)) | |
6851 | { | |
6852 | mpfr_clear(r); | |
6853 | return false; | |
6854 | } | |
6855 | ||
6856 | mpfr_t i; | |
6857 | mpfr_init(i); | |
6858 | ||
6859 | bool ret = false; | |
6860 | Type* itype; | |
6861 | if (args->back()->float_constant_value(i, &itype) | |
6862 | && Type::are_identical(rtype, itype, NULL)) | |
6863 | { | |
6864 | mpfr_set(real, r, GMP_RNDN); | |
6865 | mpfr_set(imag, i, GMP_RNDN); | |
6866 | *ptype = Builtin_call_expression::cmplx_type(rtype); | |
6867 | ret = true; | |
6868 | } | |
6869 | ||
6870 | mpfr_clear(r); | |
6871 | mpfr_clear(i); | |
6872 | ||
6873 | return ret; | |
6874 | } | |
6875 | ||
6876 | return false; | |
6877 | } | |
6878 | ||
6879 | // Return the type. | |
6880 | ||
6881 | Type* | |
6882 | Builtin_call_expression::do_type() | |
6883 | { | |
6884 | switch (this->code_) | |
6885 | { | |
6886 | case BUILTIN_INVALID: | |
6887 | default: | |
6888 | gcc_unreachable(); | |
6889 | ||
6890 | case BUILTIN_NEW: | |
6891 | case BUILTIN_MAKE: | |
6892 | { | |
6893 | const Expression_list* args = this->args(); | |
6894 | if (args == NULL || args->empty()) | |
6895 | return Type::make_error_type(); | |
6896 | return Type::make_pointer_type(args->front()->type()); | |
6897 | } | |
6898 | ||
6899 | case BUILTIN_CAP: | |
6900 | case BUILTIN_COPY: | |
6901 | case BUILTIN_LEN: | |
6902 | case BUILTIN_ALIGNOF: | |
6903 | case BUILTIN_OFFSETOF: | |
6904 | case BUILTIN_SIZEOF: | |
6905 | return Type::lookup_integer_type("int"); | |
6906 | ||
6907 | case BUILTIN_CLOSE: | |
6908 | case BUILTIN_PANIC: | |
6909 | case BUILTIN_PRINT: | |
6910 | case BUILTIN_PRINTLN: | |
6911 | return Type::make_void_type(); | |
6912 | ||
6913 | case BUILTIN_CLOSED: | |
6914 | return Type::lookup_bool_type(); | |
6915 | ||
6916 | case BUILTIN_RECOVER: | |
6917 | return Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
6918 | ||
6919 | case BUILTIN_APPEND: | |
6920 | { | |
6921 | const Expression_list* args = this->args(); | |
6922 | if (args == NULL || args->empty()) | |
6923 | return Type::make_error_type(); | |
6924 | return args->front()->type(); | |
6925 | } | |
6926 | ||
6927 | case BUILTIN_REAL: | |
6928 | case BUILTIN_IMAG: | |
6929 | { | |
6930 | Expression* arg = this->one_arg(); | |
6931 | if (arg == NULL) | |
6932 | return Type::make_error_type(); | |
6933 | Type* t = arg->type(); | |
6934 | if (t->is_abstract()) | |
6935 | t = t->make_non_abstract_type(); | |
6936 | t = Builtin_call_expression::real_imag_type(t); | |
6937 | if (t == NULL) | |
6938 | t = Type::make_error_type(); | |
6939 | return t; | |
6940 | } | |
6941 | ||
6942 | case BUILTIN_CMPLX: | |
6943 | { | |
6944 | const Expression_list* args = this->args(); | |
6945 | if (args == NULL || args->size() != 2) | |
6946 | return Type::make_error_type(); | |
6947 | Type* t = args->front()->type(); | |
6948 | if (t->is_abstract()) | |
6949 | { | |
6950 | t = args->back()->type(); | |
6951 | if (t->is_abstract()) | |
6952 | t = t->make_non_abstract_type(); | |
6953 | } | |
6954 | t = Builtin_call_expression::cmplx_type(t); | |
6955 | if (t == NULL) | |
6956 | t = Type::make_error_type(); | |
6957 | return t; | |
6958 | } | |
6959 | } | |
6960 | } | |
6961 | ||
6962 | // Determine the type. | |
6963 | ||
6964 | void | |
6965 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
6966 | { | |
6967 | this->fn()->determine_type_no_context(); | |
6968 | ||
6969 | const Expression_list* args = this->args(); | |
6970 | ||
6971 | bool is_print; | |
6972 | Type* arg_type = NULL; | |
6973 | switch (this->code_) | |
6974 | { | |
6975 | case BUILTIN_PRINT: | |
6976 | case BUILTIN_PRINTLN: | |
6977 | // Do not force a large integer constant to "int". | |
6978 | is_print = true; | |
6979 | break; | |
6980 | ||
6981 | case BUILTIN_REAL: | |
6982 | case BUILTIN_IMAG: | |
6983 | arg_type = Builtin_call_expression::cmplx_type(context->type); | |
6984 | is_print = false; | |
6985 | break; | |
6986 | ||
6987 | case BUILTIN_CMPLX: | |
6988 | { | |
6989 | // For the cmplx function the type of one operand can | |
6990 | // determine the type of the other, as in a binary expression. | |
6991 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
6992 | if (args != NULL && args->size() == 2) | |
6993 | { | |
6994 | Type* t1 = args->front()->type(); | |
6995 | Type* t2 = args->front()->type(); | |
6996 | if (!t1->is_abstract()) | |
6997 | arg_type = t1; | |
6998 | else if (!t2->is_abstract()) | |
6999 | arg_type = t2; | |
7000 | } | |
7001 | is_print = false; | |
7002 | } | |
7003 | break; | |
7004 | ||
7005 | default: | |
7006 | is_print = false; | |
7007 | break; | |
7008 | } | |
7009 | ||
7010 | if (args != NULL) | |
7011 | { | |
7012 | for (Expression_list::const_iterator pa = args->begin(); | |
7013 | pa != args->end(); | |
7014 | ++pa) | |
7015 | { | |
7016 | Type_context subcontext; | |
7017 | subcontext.type = arg_type; | |
7018 | ||
7019 | if (is_print) | |
7020 | { | |
7021 | // We want to print large constants, we so can't just | |
7022 | // use the appropriate nonabstract type. Use uint64 for | |
7023 | // an integer if we know it is nonnegative, otherwise | |
7024 | // use int64 for a integer, otherwise use float64 for a | |
7025 | // float or complex128 for a complex. | |
7026 | Type* want_type = NULL; | |
7027 | Type* atype = (*pa)->type(); | |
7028 | if (atype->is_abstract()) | |
7029 | { | |
7030 | if (atype->integer_type() != NULL) | |
7031 | { | |
7032 | mpz_t val; | |
7033 | mpz_init(val); | |
7034 | Type* dummy; | |
7035 | if (this->integer_constant_value(true, val, &dummy) | |
7036 | && mpz_sgn(val) >= 0) | |
7037 | want_type = Type::lookup_integer_type("uint64"); | |
7038 | else | |
7039 | want_type = Type::lookup_integer_type("int64"); | |
7040 | mpz_clear(val); | |
7041 | } | |
7042 | else if (atype->float_type() != NULL) | |
7043 | want_type = Type::lookup_float_type("float64"); | |
7044 | else if (atype->complex_type() != NULL) | |
7045 | want_type = Type::lookup_complex_type("complex128"); | |
7046 | else if (atype->is_abstract_string_type()) | |
7047 | want_type = Type::lookup_string_type(); | |
7048 | else if (atype->is_abstract_boolean_type()) | |
7049 | want_type = Type::lookup_bool_type(); | |
7050 | else | |
7051 | gcc_unreachable(); | |
7052 | subcontext.type = want_type; | |
7053 | } | |
7054 | } | |
7055 | ||
7056 | (*pa)->determine_type(&subcontext); | |
7057 | } | |
7058 | } | |
7059 | } | |
7060 | ||
7061 | // If there is exactly one argument, return true. Otherwise give an | |
7062 | // error message and return false. | |
7063 | ||
7064 | bool | |
7065 | Builtin_call_expression::check_one_arg() | |
7066 | { | |
7067 | const Expression_list* args = this->args(); | |
7068 | if (args == NULL || args->size() < 1) | |
7069 | { | |
7070 | this->report_error(_("not enough arguments")); | |
7071 | return false; | |
7072 | } | |
7073 | else if (args->size() > 1) | |
7074 | { | |
7075 | this->report_error(_("too many arguments")); | |
7076 | return false; | |
7077 | } | |
7078 | if (args->front()->is_error_expression() | |
7079 | || args->front()->type()->is_error_type()) | |
7080 | { | |
7081 | this->set_is_error(); | |
7082 | return false; | |
7083 | } | |
7084 | return true; | |
7085 | } | |
7086 | ||
7087 | // Check argument types for a builtin function. | |
7088 | ||
7089 | void | |
7090 | Builtin_call_expression::do_check_types(Gogo*) | |
7091 | { | |
7092 | switch (this->code_) | |
7093 | { | |
7094 | case BUILTIN_INVALID: | |
7095 | case BUILTIN_NEW: | |
7096 | case BUILTIN_MAKE: | |
7097 | return; | |
7098 | ||
7099 | case BUILTIN_LEN: | |
7100 | case BUILTIN_CAP: | |
7101 | { | |
7102 | // The single argument may be either a string or an array or a | |
7103 | // map or a channel, or a pointer to a closed array. | |
7104 | if (this->check_one_arg()) | |
7105 | { | |
7106 | Type* arg_type = this->one_arg()->type(); | |
7107 | if (arg_type->points_to() != NULL | |
7108 | && arg_type->points_to()->array_type() != NULL | |
7109 | && !arg_type->points_to()->is_open_array_type()) | |
7110 | arg_type = arg_type->points_to(); | |
7111 | if (this->code_ == BUILTIN_CAP) | |
7112 | { | |
7113 | if (!arg_type->is_error_type() | |
7114 | && arg_type->array_type() == NULL | |
7115 | && arg_type->channel_type() == NULL) | |
7116 | this->report_error(_("argument must be array or slice " | |
7117 | "or channel")); | |
7118 | } | |
7119 | else | |
7120 | { | |
7121 | if (!arg_type->is_error_type() | |
7122 | && !arg_type->is_string_type() | |
7123 | && arg_type->array_type() == NULL | |
7124 | && arg_type->map_type() == NULL | |
7125 | && arg_type->channel_type() == NULL) | |
7126 | this->report_error(_("argument must be string or " | |
7127 | "array or slice or map or channel")); | |
7128 | } | |
7129 | } | |
7130 | } | |
7131 | break; | |
7132 | ||
7133 | case BUILTIN_PRINT: | |
7134 | case BUILTIN_PRINTLN: | |
7135 | { | |
7136 | const Expression_list* args = this->args(); | |
7137 | if (args == NULL) | |
7138 | { | |
7139 | if (this->code_ == BUILTIN_PRINT) | |
7140 | warning_at(this->location(), 0, | |
7141 | "no arguments for builtin function %<%s%>", | |
7142 | (this->code_ == BUILTIN_PRINT | |
7143 | ? "print" | |
7144 | : "println")); | |
7145 | } | |
7146 | else | |
7147 | { | |
7148 | for (Expression_list::const_iterator p = args->begin(); | |
7149 | p != args->end(); | |
7150 | ++p) | |
7151 | { | |
7152 | Type* type = (*p)->type(); | |
7153 | if (type->is_error_type() | |
7154 | || type->is_string_type() | |
7155 | || type->integer_type() != NULL | |
7156 | || type->float_type() != NULL | |
7157 | || type->complex_type() != NULL | |
7158 | || type->is_boolean_type() | |
7159 | || type->points_to() != NULL | |
7160 | || type->interface_type() != NULL | |
7161 | || type->channel_type() != NULL | |
7162 | || type->map_type() != NULL | |
7163 | || type->function_type() != NULL | |
7164 | || type->is_open_array_type()) | |
7165 | ; | |
7166 | else | |
7167 | this->report_error(_("unsupported argument type to " | |
7168 | "builtin function")); | |
7169 | } | |
7170 | } | |
7171 | } | |
7172 | break; | |
7173 | ||
7174 | case BUILTIN_CLOSE: | |
7175 | case BUILTIN_CLOSED: | |
7176 | if (this->check_one_arg()) | |
7177 | { | |
7178 | if (this->one_arg()->type()->channel_type() == NULL) | |
7179 | this->report_error(_("argument must be channel")); | |
7180 | } | |
7181 | break; | |
7182 | ||
7183 | case BUILTIN_PANIC: | |
7184 | case BUILTIN_SIZEOF: | |
7185 | case BUILTIN_ALIGNOF: | |
7186 | this->check_one_arg(); | |
7187 | break; | |
7188 | ||
7189 | case BUILTIN_RECOVER: | |
7190 | if (this->args() != NULL && !this->args()->empty()) | |
7191 | this->report_error(_("too many arguments")); | |
7192 | break; | |
7193 | ||
7194 | case BUILTIN_OFFSETOF: | |
7195 | if (this->check_one_arg()) | |
7196 | { | |
7197 | Expression* arg = this->one_arg(); | |
7198 | if (arg->field_reference_expression() == NULL) | |
7199 | this->report_error(_("argument must be a field reference")); | |
7200 | } | |
7201 | break; | |
7202 | ||
7203 | case BUILTIN_COPY: | |
7204 | { | |
7205 | const Expression_list* args = this->args(); | |
7206 | if (args == NULL || args->size() < 2) | |
7207 | { | |
7208 | this->report_error(_("not enough arguments")); | |
7209 | break; | |
7210 | } | |
7211 | else if (args->size() > 2) | |
7212 | { | |
7213 | this->report_error(_("too many arguments")); | |
7214 | break; | |
7215 | } | |
7216 | Type* arg1_type = args->front()->type(); | |
7217 | Type* arg2_type = args->back()->type(); | |
7218 | if (arg1_type->is_error_type() || arg2_type->is_error_type()) | |
7219 | break; | |
7220 | ||
7221 | Type* e1; | |
7222 | if (arg1_type->is_open_array_type()) | |
7223 | e1 = arg1_type->array_type()->element_type(); | |
7224 | else | |
7225 | { | |
7226 | this->report_error(_("left argument must be a slice")); | |
7227 | break; | |
7228 | } | |
7229 | ||
7230 | Type* e2; | |
7231 | if (arg2_type->is_open_array_type()) | |
7232 | e2 = arg2_type->array_type()->element_type(); | |
7233 | else if (arg2_type->is_string_type()) | |
7234 | e2 = Type::lookup_integer_type("uint8"); | |
7235 | else | |
7236 | { | |
7237 | this->report_error(_("right argument must be a slice or a string")); | |
7238 | break; | |
7239 | } | |
7240 | ||
7241 | if (!Type::are_identical(e1, e2, NULL)) | |
7242 | this->report_error(_("element types must be the same")); | |
7243 | } | |
7244 | break; | |
7245 | ||
7246 | case BUILTIN_APPEND: | |
7247 | { | |
7248 | const Expression_list* args = this->args(); | |
7249 | if (args == NULL || args->empty()) | |
7250 | { | |
7251 | this->report_error(_("not enough arguments")); | |
7252 | break; | |
7253 | } | |
7254 | /* Lowering varargs should have left us with 2 arguments. */ | |
7255 | gcc_assert(args->size() == 2); | |
7256 | std::string reason; | |
7257 | if (!Type::are_assignable(args->front()->type(), args->back()->type(), | |
7258 | &reason)) | |
7259 | { | |
7260 | if (reason.empty()) | |
7261 | this->report_error(_("arguments 1 and 2 have different types")); | |
7262 | else | |
7263 | { | |
7264 | error_at(this->location(), | |
7265 | "arguments 1 and 2 have different types (%s)", | |
7266 | reason.c_str()); | |
7267 | this->set_is_error(); | |
7268 | } | |
7269 | } | |
7270 | break; | |
7271 | } | |
7272 | ||
7273 | case BUILTIN_REAL: | |
7274 | case BUILTIN_IMAG: | |
7275 | if (this->check_one_arg()) | |
7276 | { | |
7277 | if (this->one_arg()->type()->complex_type() == NULL) | |
7278 | this->report_error(_("argument must have complex type")); | |
7279 | } | |
7280 | break; | |
7281 | ||
7282 | case BUILTIN_CMPLX: | |
7283 | { | |
7284 | const Expression_list* args = this->args(); | |
7285 | if (args == NULL || args->size() < 2) | |
7286 | this->report_error(_("not enough arguments")); | |
7287 | else if (args->size() > 2) | |
7288 | this->report_error(_("too many arguments")); | |
7289 | else if (args->front()->is_error_expression() | |
7290 | || args->front()->type()->is_error_type() | |
7291 | || args->back()->is_error_expression() | |
7292 | || args->back()->type()->is_error_type()) | |
7293 | this->set_is_error(); | |
7294 | else if (!Type::are_identical(args->front()->type(), | |
7295 | args->back()->type(), NULL)) | |
7296 | this->report_error(_("cmplx arguments must have identical types")); | |
7297 | else if (args->front()->type()->float_type() == NULL) | |
7298 | this->report_error(_("cmplx arguments must have " | |
7299 | "floating-point type")); | |
7300 | } | |
7301 | break; | |
7302 | ||
7303 | default: | |
7304 | gcc_unreachable(); | |
7305 | } | |
7306 | } | |
7307 | ||
7308 | // Return the tree for a builtin function. | |
7309 | ||
7310 | tree | |
7311 | Builtin_call_expression::do_get_tree(Translate_context* context) | |
7312 | { | |
7313 | Gogo* gogo = context->gogo(); | |
7314 | source_location location = this->location(); | |
7315 | switch (this->code_) | |
7316 | { | |
7317 | case BUILTIN_INVALID: | |
7318 | case BUILTIN_NEW: | |
7319 | case BUILTIN_MAKE: | |
7320 | gcc_unreachable(); | |
7321 | ||
7322 | case BUILTIN_LEN: | |
7323 | case BUILTIN_CAP: | |
7324 | { | |
7325 | const Expression_list* args = this->args(); | |
7326 | gcc_assert(args != NULL && args->size() == 1); | |
7327 | Expression* arg = *args->begin(); | |
7328 | Type* arg_type = arg->type(); | |
7329 | tree arg_tree = arg->get_tree(context); | |
7330 | if (arg_tree == error_mark_node) | |
7331 | return error_mark_node; | |
7332 | ||
7333 | if (arg_type->points_to() != NULL) | |
7334 | { | |
7335 | arg_type = arg_type->points_to(); | |
7336 | gcc_assert(arg_type->array_type() != NULL | |
7337 | && !arg_type->is_open_array_type()); | |
7338 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree))); | |
7339 | arg_tree = build_fold_indirect_ref(arg_tree); | |
7340 | } | |
7341 | ||
7342 | tree val_tree; | |
7343 | if (this->code_ == BUILTIN_LEN) | |
7344 | { | |
7345 | if (arg_type->is_string_type()) | |
7346 | val_tree = String_type::length_tree(gogo, arg_tree); | |
7347 | else if (arg_type->array_type() != NULL) | |
7348 | val_tree = arg_type->array_type()->length_tree(gogo, arg_tree); | |
7349 | else if (arg_type->map_type() != NULL) | |
7350 | { | |
7351 | static tree map_len_fndecl; | |
7352 | val_tree = Gogo::call_builtin(&map_len_fndecl, | |
7353 | location, | |
7354 | "__go_map_len", | |
7355 | 1, | |
7356 | sizetype, | |
7357 | arg_type->get_tree(gogo), | |
7358 | arg_tree); | |
7359 | } | |
7360 | else if (arg_type->channel_type() != NULL) | |
7361 | { | |
7362 | static tree chan_len_fndecl; | |
7363 | val_tree = Gogo::call_builtin(&chan_len_fndecl, | |
7364 | location, | |
7365 | "__go_chan_len", | |
7366 | 1, | |
7367 | sizetype, | |
7368 | arg_type->get_tree(gogo), | |
7369 | arg_tree); | |
7370 | } | |
7371 | else | |
7372 | gcc_unreachable(); | |
7373 | } | |
7374 | else | |
7375 | { | |
7376 | if (arg_type->array_type() != NULL) | |
7377 | val_tree = arg_type->array_type()->capacity_tree(gogo, arg_tree); | |
7378 | else if (arg_type->channel_type() != NULL) | |
7379 | { | |
7380 | static tree chan_cap_fndecl; | |
7381 | val_tree = Gogo::call_builtin(&chan_cap_fndecl, | |
7382 | location, | |
7383 | "__go_chan_cap", | |
7384 | 1, | |
7385 | sizetype, | |
7386 | arg_type->get_tree(gogo), | |
7387 | arg_tree); | |
7388 | } | |
7389 | else | |
7390 | gcc_unreachable(); | |
7391 | } | |
7392 | ||
7393 | tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo); | |
7394 | if (type_tree == TREE_TYPE(val_tree)) | |
7395 | return val_tree; | |
7396 | else | |
7397 | return fold(convert_to_integer(type_tree, val_tree)); | |
7398 | } | |
7399 | ||
7400 | case BUILTIN_PRINT: | |
7401 | case BUILTIN_PRINTLN: | |
7402 | { | |
7403 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
7404 | tree stmt_list = NULL_TREE; | |
7405 | ||
7406 | const Expression_list* call_args = this->args(); | |
7407 | if (call_args != NULL) | |
7408 | { | |
7409 | for (Expression_list::const_iterator p = call_args->begin(); | |
7410 | p != call_args->end(); | |
7411 | ++p) | |
7412 | { | |
7413 | if (is_ln && p != call_args->begin()) | |
7414 | { | |
7415 | static tree print_space_fndecl; | |
7416 | tree call = Gogo::call_builtin(&print_space_fndecl, | |
7417 | location, | |
7418 | "__go_print_space", | |
7419 | 0, | |
7420 | void_type_node); | |
7421 | append_to_statement_list(call, &stmt_list); | |
7422 | } | |
7423 | ||
7424 | Type* type = (*p)->type(); | |
7425 | ||
7426 | tree arg = (*p)->get_tree(context); | |
7427 | if (arg == error_mark_node) | |
7428 | return error_mark_node; | |
7429 | ||
7430 | tree* pfndecl; | |
7431 | const char* fnname; | |
7432 | if (type->is_string_type()) | |
7433 | { | |
7434 | static tree print_string_fndecl; | |
7435 | pfndecl = &print_string_fndecl; | |
7436 | fnname = "__go_print_string"; | |
7437 | } | |
7438 | else if (type->integer_type() != NULL | |
7439 | && type->integer_type()->is_unsigned()) | |
7440 | { | |
7441 | static tree print_uint64_fndecl; | |
7442 | pfndecl = &print_uint64_fndecl; | |
7443 | fnname = "__go_print_uint64"; | |
7444 | Type* itype = Type::lookup_integer_type("uint64"); | |
7445 | arg = fold_convert_loc(location, itype->get_tree(gogo), | |
7446 | arg); | |
7447 | } | |
7448 | else if (type->integer_type() != NULL) | |
7449 | { | |
7450 | static tree print_int64_fndecl; | |
7451 | pfndecl = &print_int64_fndecl; | |
7452 | fnname = "__go_print_int64"; | |
7453 | Type* itype = Type::lookup_integer_type("int64"); | |
7454 | arg = fold_convert_loc(location, itype->get_tree(gogo), | |
7455 | arg); | |
7456 | } | |
7457 | else if (type->float_type() != NULL) | |
7458 | { | |
7459 | static tree print_double_fndecl; | |
7460 | pfndecl = &print_double_fndecl; | |
7461 | fnname = "__go_print_double"; | |
7462 | arg = fold_convert_loc(location, double_type_node, arg); | |
7463 | } | |
7464 | else if (type->complex_type() != NULL) | |
7465 | { | |
7466 | static tree print_complex_fndecl; | |
7467 | pfndecl = &print_complex_fndecl; | |
7468 | fnname = "__go_print_complex"; | |
7469 | arg = fold_convert_loc(location, complex_double_type_node, | |
7470 | arg); | |
7471 | } | |
7472 | else if (type->is_boolean_type()) | |
7473 | { | |
7474 | static tree print_bool_fndecl; | |
7475 | pfndecl = &print_bool_fndecl; | |
7476 | fnname = "__go_print_bool"; | |
7477 | } | |
7478 | else if (type->points_to() != NULL | |
7479 | || type->channel_type() != NULL | |
7480 | || type->map_type() != NULL | |
7481 | || type->function_type() != NULL) | |
7482 | { | |
7483 | static tree print_pointer_fndecl; | |
7484 | pfndecl = &print_pointer_fndecl; | |
7485 | fnname = "__go_print_pointer"; | |
7486 | arg = fold_convert_loc(location, ptr_type_node, arg); | |
7487 | } | |
7488 | else if (type->interface_type() != NULL) | |
7489 | { | |
7490 | if (type->interface_type()->is_empty()) | |
7491 | { | |
7492 | static tree print_empty_interface_fndecl; | |
7493 | pfndecl = &print_empty_interface_fndecl; | |
7494 | fnname = "__go_print_empty_interface"; | |
7495 | } | |
7496 | else | |
7497 | { | |
7498 | static tree print_interface_fndecl; | |
7499 | pfndecl = &print_interface_fndecl; | |
7500 | fnname = "__go_print_interface"; | |
7501 | } | |
7502 | } | |
7503 | else if (type->is_open_array_type()) | |
7504 | { | |
7505 | static tree print_slice_fndecl; | |
7506 | pfndecl = &print_slice_fndecl; | |
7507 | fnname = "__go_print_slice"; | |
7508 | } | |
7509 | else | |
7510 | gcc_unreachable(); | |
7511 | ||
7512 | tree call = Gogo::call_builtin(pfndecl, | |
7513 | location, | |
7514 | fnname, | |
7515 | 1, | |
7516 | void_type_node, | |
7517 | TREE_TYPE(arg), | |
7518 | arg); | |
7519 | append_to_statement_list(call, &stmt_list); | |
7520 | } | |
7521 | } | |
7522 | ||
7523 | if (is_ln) | |
7524 | { | |
7525 | static tree print_nl_fndecl; | |
7526 | tree call = Gogo::call_builtin(&print_nl_fndecl, | |
7527 | location, | |
7528 | "__go_print_nl", | |
7529 | 0, | |
7530 | void_type_node); | |
7531 | append_to_statement_list(call, &stmt_list); | |
7532 | } | |
7533 | ||
7534 | return stmt_list; | |
7535 | } | |
7536 | ||
7537 | case BUILTIN_PANIC: | |
7538 | { | |
7539 | const Expression_list* args = this->args(); | |
7540 | gcc_assert(args != NULL && args->size() == 1); | |
7541 | Expression* arg = args->front(); | |
7542 | tree arg_tree = arg->get_tree(context); | |
7543 | if (arg_tree == error_mark_node) | |
7544 | return error_mark_node; | |
7545 | Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
7546 | arg_tree = Expression::convert_for_assignment(context, empty, | |
7547 | arg->type(), | |
7548 | arg_tree, location); | |
7549 | static tree panic_fndecl; | |
7550 | tree call = Gogo::call_builtin(&panic_fndecl, | |
7551 | location, | |
7552 | "__go_panic", | |
7553 | 1, | |
7554 | void_type_node, | |
7555 | TREE_TYPE(arg_tree), | |
7556 | arg_tree); | |
7557 | // This function will throw an exception. | |
7558 | TREE_NOTHROW(panic_fndecl) = 0; | |
7559 | // This function will not return. | |
7560 | TREE_THIS_VOLATILE(panic_fndecl) = 1; | |
7561 | return call; | |
7562 | } | |
7563 | ||
7564 | case BUILTIN_RECOVER: | |
7565 | { | |
7566 | // The argument is set when building recover thunks. It's a | |
7567 | // boolean value which is true if we can recover a value now. | |
7568 | const Expression_list* args = this->args(); | |
7569 | gcc_assert(args != NULL && args->size() == 1); | |
7570 | Expression* arg = args->front(); | |
7571 | tree arg_tree = arg->get_tree(context); | |
7572 | if (arg_tree == error_mark_node) | |
7573 | return error_mark_node; | |
7574 | ||
7575 | Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
7576 | tree empty_tree = empty->get_tree(context->gogo()); | |
7577 | ||
7578 | Type* nil_type = Type::make_nil_type(); | |
7579 | Expression* nil = Expression::make_nil(location); | |
7580 | tree nil_tree = nil->get_tree(context); | |
7581 | tree empty_nil_tree = Expression::convert_for_assignment(context, | |
7582 | empty, | |
7583 | nil_type, | |
7584 | nil_tree, | |
7585 | location); | |
7586 | ||
7587 | // We need to handle a deferred call to recover specially, | |
7588 | // because it changes whether it can recover a panic or not. | |
7589 | // See test7 in test/recover1.go. | |
7590 | tree call; | |
7591 | if (this->is_deferred()) | |
7592 | { | |
7593 | static tree deferred_recover_fndecl; | |
7594 | call = Gogo::call_builtin(&deferred_recover_fndecl, | |
7595 | location, | |
7596 | "__go_deferred_recover", | |
7597 | 0, | |
7598 | empty_tree); | |
7599 | } | |
7600 | else | |
7601 | { | |
7602 | static tree recover_fndecl; | |
7603 | call = Gogo::call_builtin(&recover_fndecl, | |
7604 | location, | |
7605 | "__go_recover", | |
7606 | 0, | |
7607 | empty_tree); | |
7608 | } | |
7609 | return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree, | |
7610 | call, empty_nil_tree); | |
7611 | } | |
7612 | ||
7613 | case BUILTIN_CLOSE: | |
7614 | case BUILTIN_CLOSED: | |
7615 | { | |
7616 | const Expression_list* args = this->args(); | |
7617 | gcc_assert(args != NULL && args->size() == 1); | |
7618 | Expression* arg = args->front(); | |
7619 | tree arg_tree = arg->get_tree(context); | |
7620 | if (arg_tree == error_mark_node) | |
7621 | return error_mark_node; | |
7622 | if (this->code_ == BUILTIN_CLOSE) | |
7623 | { | |
7624 | static tree close_fndecl; | |
7625 | return Gogo::call_builtin(&close_fndecl, | |
7626 | location, | |
7627 | "__go_builtin_close", | |
7628 | 1, | |
7629 | void_type_node, | |
7630 | TREE_TYPE(arg_tree), | |
7631 | arg_tree); | |
7632 | } | |
7633 | else | |
7634 | { | |
7635 | static tree closed_fndecl; | |
7636 | return Gogo::call_builtin(&closed_fndecl, | |
7637 | location, | |
7638 | "__go_builtin_closed", | |
7639 | 1, | |
7640 | boolean_type_node, | |
7641 | TREE_TYPE(arg_tree), | |
7642 | arg_tree); | |
7643 | } | |
7644 | } | |
7645 | ||
7646 | case BUILTIN_SIZEOF: | |
7647 | case BUILTIN_OFFSETOF: | |
7648 | case BUILTIN_ALIGNOF: | |
7649 | { | |
7650 | mpz_t val; | |
7651 | mpz_init(val); | |
7652 | Type* dummy; | |
7653 | bool b = this->integer_constant_value(true, val, &dummy); | |
7654 | gcc_assert(b); | |
7655 | tree type = Type::lookup_integer_type("int")->get_tree(gogo); | |
7656 | tree ret = Expression::integer_constant_tree(val, type); | |
7657 | mpz_clear(val); | |
7658 | return ret; | |
7659 | } | |
7660 | ||
7661 | case BUILTIN_COPY: | |
7662 | { | |
7663 | const Expression_list* args = this->args(); | |
7664 | gcc_assert(args != NULL && args->size() == 2); | |
7665 | Expression* arg1 = args->front(); | |
7666 | Expression* arg2 = args->back(); | |
7667 | ||
7668 | tree arg1_tree = arg1->get_tree(context); | |
7669 | tree arg2_tree = arg2->get_tree(context); | |
7670 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
7671 | return error_mark_node; | |
7672 | ||
7673 | Type* arg1_type = arg1->type(); | |
7674 | Array_type* at = arg1_type->array_type(); | |
7675 | arg1_tree = save_expr(arg1_tree); | |
7676 | tree arg1_val = at->value_pointer_tree(gogo, arg1_tree); | |
7677 | tree arg1_len = at->length_tree(gogo, arg1_tree); | |
7678 | ||
7679 | Type* arg2_type = arg2->type(); | |
7680 | tree arg2_val; | |
7681 | tree arg2_len; | |
7682 | if (arg2_type->is_open_array_type()) | |
7683 | { | |
7684 | at = arg2_type->array_type(); | |
7685 | arg2_tree = save_expr(arg2_tree); | |
7686 | arg2_val = at->value_pointer_tree(gogo, arg2_tree); | |
7687 | arg2_len = at->length_tree(gogo, arg2_tree); | |
7688 | } | |
7689 | else | |
7690 | { | |
7691 | arg2_tree = save_expr(arg2_tree); | |
7692 | arg2_val = String_type::bytes_tree(gogo, arg2_tree); | |
7693 | arg2_len = String_type::length_tree(gogo, arg2_tree); | |
7694 | } | |
7695 | ||
7696 | arg1_len = save_expr(arg1_len); | |
7697 | arg2_len = save_expr(arg2_len); | |
7698 | tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len), | |
7699 | fold_build2_loc(location, LT_EXPR, | |
7700 | boolean_type_node, | |
7701 | arg1_len, arg2_len), | |
7702 | arg1_len, arg2_len); | |
7703 | len = save_expr(len); | |
7704 | ||
7705 | Type* element_type = at->element_type(); | |
7706 | tree element_type_tree = element_type->get_tree(gogo); | |
7707 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); | |
7708 | tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size), | |
7709 | len); | |
7710 | bytecount = fold_build2_loc(location, MULT_EXPR, | |
7711 | TREE_TYPE(element_size), | |
7712 | bytecount, element_size); | |
7713 | bytecount = fold_convert_loc(location, size_type_node, bytecount); | |
7714 | ||
7715 | tree call = build_call_expr_loc(location, | |
7716 | built_in_decls[BUILT_IN_MEMMOVE], | |
7717 | 3, arg1_val, arg2_val, bytecount); | |
7718 | ||
7719 | return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len), | |
7720 | call, len); | |
7721 | } | |
7722 | ||
7723 | case BUILTIN_APPEND: | |
7724 | { | |
7725 | const Expression_list* args = this->args(); | |
7726 | gcc_assert(args != NULL && args->size() == 2); | |
7727 | Expression* arg1 = args->front(); | |
7728 | Expression* arg2 = args->back(); | |
7729 | ||
7730 | tree arg1_tree = arg1->get_tree(context); | |
7731 | tree arg2_tree = arg2->get_tree(context); | |
7732 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
7733 | return error_mark_node; | |
7734 | ||
7735 | tree descriptor_tree = arg1->type()->type_descriptor_pointer(gogo); | |
7736 | ||
7737 | // We rebuild the decl each time since the slice types may | |
7738 | // change. | |
7739 | tree append_fndecl = NULL_TREE; | |
7740 | return Gogo::call_builtin(&append_fndecl, | |
7741 | location, | |
7742 | "__go_append", | |
7743 | 3, | |
7744 | TREE_TYPE(arg1_tree), | |
7745 | TREE_TYPE(descriptor_tree), | |
7746 | descriptor_tree, | |
7747 | TREE_TYPE(arg1_tree), | |
7748 | arg1_tree, | |
7749 | TREE_TYPE(arg2_tree), | |
7750 | arg2_tree); | |
7751 | } | |
7752 | ||
7753 | case BUILTIN_REAL: | |
7754 | case BUILTIN_IMAG: | |
7755 | { | |
7756 | const Expression_list* args = this->args(); | |
7757 | gcc_assert(args != NULL && args->size() == 1); | |
7758 | Expression* arg = args->front(); | |
7759 | tree arg_tree = arg->get_tree(context); | |
7760 | if (arg_tree == error_mark_node) | |
7761 | return error_mark_node; | |
7762 | gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree))); | |
7763 | if (this->code_ == BUILTIN_REAL) | |
7764 | return fold_build1_loc(location, REALPART_EXPR, | |
7765 | TREE_TYPE(TREE_TYPE(arg_tree)), | |
7766 | arg_tree); | |
7767 | else | |
7768 | return fold_build1_loc(location, IMAGPART_EXPR, | |
7769 | TREE_TYPE(TREE_TYPE(arg_tree)), | |
7770 | arg_tree); | |
7771 | } | |
7772 | ||
7773 | case BUILTIN_CMPLX: | |
7774 | { | |
7775 | const Expression_list* args = this->args(); | |
7776 | gcc_assert(args != NULL && args->size() == 2); | |
7777 | tree r = args->front()->get_tree(context); | |
7778 | tree i = args->back()->get_tree(context); | |
7779 | if (r == error_mark_node || i == error_mark_node) | |
7780 | return error_mark_node; | |
7781 | gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r)) | |
7782 | == TYPE_MAIN_VARIANT(TREE_TYPE(i))); | |
7783 | gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r))); | |
7784 | return fold_build2_loc(location, COMPLEX_EXPR, | |
7785 | build_complex_type(TREE_TYPE(r)), | |
7786 | r, i); | |
7787 | } | |
7788 | ||
7789 | default: | |
7790 | gcc_unreachable(); | |
7791 | } | |
7792 | } | |
7793 | ||
7794 | // We have to support exporting a builtin call expression, because | |
7795 | // code can set a constant to the result of a builtin expression. | |
7796 | ||
7797 | void | |
7798 | Builtin_call_expression::do_export(Export* exp) const | |
7799 | { | |
7800 | bool ok = false; | |
7801 | ||
7802 | mpz_t val; | |
7803 | mpz_init(val); | |
7804 | Type* dummy; | |
7805 | if (this->integer_constant_value(true, val, &dummy)) | |
7806 | { | |
7807 | Integer_expression::export_integer(exp, val); | |
7808 | ok = true; | |
7809 | } | |
7810 | mpz_clear(val); | |
7811 | ||
7812 | if (!ok) | |
7813 | { | |
7814 | mpfr_t fval; | |
7815 | mpfr_init(fval); | |
7816 | if (this->float_constant_value(fval, &dummy)) | |
7817 | { | |
7818 | Float_expression::export_float(exp, fval); | |
7819 | ok = true; | |
7820 | } | |
7821 | mpfr_clear(fval); | |
7822 | } | |
7823 | ||
7824 | if (!ok) | |
7825 | { | |
7826 | mpfr_t real; | |
7827 | mpfr_t imag; | |
7828 | mpfr_init(real); | |
7829 | mpfr_init(imag); | |
7830 | if (this->complex_constant_value(real, imag, &dummy)) | |
7831 | { | |
7832 | Complex_expression::export_complex(exp, real, imag); | |
7833 | ok = true; | |
7834 | } | |
7835 | mpfr_clear(real); | |
7836 | mpfr_clear(imag); | |
7837 | } | |
7838 | ||
7839 | if (!ok) | |
7840 | { | |
7841 | error_at(this->location(), "value is not constant"); | |
7842 | return; | |
7843 | } | |
7844 | ||
7845 | // A trailing space lets us reliably identify the end of the number. | |
7846 | exp->write_c_string(" "); | |
7847 | } | |
7848 | ||
7849 | // Class Call_expression. | |
7850 | ||
7851 | // Traversal. | |
7852 | ||
7853 | int | |
7854 | Call_expression::do_traverse(Traverse* traverse) | |
7855 | { | |
7856 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) | |
7857 | return TRAVERSE_EXIT; | |
7858 | if (this->args_ != NULL) | |
7859 | { | |
7860 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
7861 | return TRAVERSE_EXIT; | |
7862 | } | |
7863 | return TRAVERSE_CONTINUE; | |
7864 | } | |
7865 | ||
7866 | // Lower a call statement. | |
7867 | ||
7868 | Expression* | |
7869 | Call_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
7870 | { | |
7871 | // A type case can look like a function call. | |
7872 | if (this->fn_->is_type_expression() | |
7873 | && this->args_ != NULL | |
7874 | && this->args_->size() == 1) | |
7875 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
7876 | this->location()); | |
7877 | ||
7878 | // Recognize a call to a builtin function. | |
7879 | Func_expression* fne = this->fn_->func_expression(); | |
7880 | if (fne != NULL | |
7881 | && fne->named_object()->is_function_declaration() | |
7882 | && fne->named_object()->func_declaration_value()->type()->is_builtin()) | |
7883 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
7884 | this->is_varargs_, this->location()); | |
7885 | ||
7886 | // Handle an argument which is a call to a function which returns | |
7887 | // multiple results. | |
7888 | if (this->args_ != NULL | |
7889 | && this->args_->size() == 1 | |
7890 | && this->args_->front()->call_expression() != NULL | |
7891 | && this->fn_->type()->function_type() != NULL) | |
7892 | { | |
7893 | Function_type* fntype = this->fn_->type()->function_type(); | |
7894 | size_t rc = this->args_->front()->call_expression()->result_count(); | |
7895 | if (rc > 1 | |
7896 | && fntype->parameters() != NULL | |
7897 | && (fntype->parameters()->size() == rc | |
7898 | || (fntype->is_varargs() | |
7899 | && fntype->parameters()->size() - 1 <= rc))) | |
7900 | { | |
7901 | Call_expression* call = this->args_->front()->call_expression(); | |
7902 | Expression_list* args = new Expression_list; | |
7903 | for (size_t i = 0; i < rc; ++i) | |
7904 | args->push_back(Expression::make_call_result(call, i)); | |
7905 | // We can't return a new call expression here, because this | |
7906 | // one may be referenced by Call_result expressions. FIXME. | |
7907 | delete this->args_; | |
7908 | this->args_ = args; | |
7909 | } | |
7910 | } | |
7911 | ||
7912 | // Handle a call to a varargs function by packaging up the extra | |
7913 | // parameters. | |
7914 | if (this->fn_->type()->function_type() != NULL | |
7915 | && this->fn_->type()->function_type()->is_varargs()) | |
7916 | { | |
7917 | Function_type* fntype = this->fn_->type()->function_type(); | |
7918 | const Typed_identifier_list* parameters = fntype->parameters(); | |
7919 | gcc_assert(parameters != NULL && !parameters->empty()); | |
7920 | Type* varargs_type = parameters->back().type(); | |
7921 | return this->lower_varargs(gogo, function, varargs_type, | |
7922 | parameters->size()); | |
7923 | } | |
7924 | ||
7925 | return this; | |
7926 | } | |
7927 | ||
7928 | // Lower a call to a varargs function. FUNCTION is the function in | |
7929 | // which the call occurs--it's not the function we are calling. | |
7930 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
7931 | // PARAM_COUNT is the number of parameters of the function we are | |
7932 | // calling; the last of these parameters will be the varargs | |
7933 | // parameter. | |
7934 | ||
7935 | Expression* | |
7936 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, | |
7937 | Type* varargs_type, size_t param_count) | |
7938 | { | |
7939 | if (this->varargs_are_lowered_) | |
7940 | return this; | |
7941 | ||
7942 | source_location loc = this->location(); | |
7943 | ||
7944 | gcc_assert(param_count > 0); | |
7945 | gcc_assert(varargs_type->is_open_array_type()); | |
7946 | ||
7947 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
7948 | if (arg_count < param_count - 1) | |
7949 | { | |
7950 | // Not enough arguments; will be caught in check_types. | |
7951 | return this; | |
7952 | } | |
7953 | ||
7954 | Expression_list* old_args = this->args_; | |
7955 | Expression_list* new_args = new Expression_list(); | |
7956 | bool push_empty_arg = false; | |
7957 | if (old_args == NULL || old_args->empty()) | |
7958 | { | |
7959 | gcc_assert(param_count == 1); | |
7960 | push_empty_arg = true; | |
7961 | } | |
7962 | else | |
7963 | { | |
7964 | Expression_list::const_iterator pa; | |
7965 | int i = 1; | |
7966 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
7967 | { | |
7968 | if (static_cast<size_t>(i) == param_count) | |
7969 | break; | |
7970 | new_args->push_back(*pa); | |
7971 | } | |
7972 | ||
7973 | // We have reached the varargs parameter. | |
7974 | ||
7975 | bool issued_error = false; | |
7976 | if (pa == old_args->end()) | |
7977 | push_empty_arg = true; | |
7978 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
7979 | new_args->push_back(*pa); | |
7980 | else if (this->is_varargs_) | |
7981 | { | |
7982 | this->report_error(_("too many arguments")); | |
7983 | return this; | |
7984 | } | |
7985 | else if (pa + 1 == old_args->end() | |
7986 | && this->is_compatible_varargs_argument(function, *pa, | |
7987 | varargs_type, | |
7988 | &issued_error)) | |
7989 | new_args->push_back(*pa); | |
7990 | else | |
7991 | { | |
7992 | Type* element_type = varargs_type->array_type()->element_type(); | |
7993 | Expression_list* vals = new Expression_list; | |
7994 | for (; pa != old_args->end(); ++pa, ++i) | |
7995 | { | |
7996 | // Check types here so that we get a better message. | |
7997 | Type* patype = (*pa)->type(); | |
7998 | source_location paloc = (*pa)->location(); | |
7999 | if (!this->check_argument_type(i, element_type, patype, | |
8000 | paloc, issued_error)) | |
8001 | continue; | |
8002 | vals->push_back(*pa); | |
8003 | } | |
8004 | Expression* val = | |
8005 | Expression::make_slice_composite_literal(varargs_type, vals, loc); | |
8006 | new_args->push_back(val); | |
8007 | } | |
8008 | } | |
8009 | ||
8010 | if (push_empty_arg) | |
8011 | new_args->push_back(Expression::make_nil(loc)); | |
8012 | ||
8013 | // We can't return a new call expression here, because this one may | |
8014 | // be referenced by Call_result expressions. FIXME. | |
8015 | if (old_args != NULL) | |
8016 | delete old_args; | |
8017 | this->args_ = new_args; | |
8018 | this->varargs_are_lowered_ = true; | |
8019 | ||
8020 | // Lower all the new subexpressions. | |
8021 | Expression* ret = this; | |
8022 | gogo->lower_expression(function, &ret); | |
8023 | gcc_assert(ret == this); | |
8024 | return ret; | |
8025 | } | |
8026 | ||
8027 | // Return true if ARG is a varargs argment which should be passed to | |
8028 | // the varargs parameter of type PARAM_TYPE without wrapping. ARG | |
8029 | // will be the last argument passed in the call, and PARAM_TYPE will | |
8030 | // be the type of the last parameter of the varargs function being | |
8031 | // called. | |
8032 | ||
8033 | bool | |
8034 | Call_expression::is_compatible_varargs_argument(Named_object* function, | |
8035 | Expression* arg, | |
8036 | Type* param_type, | |
8037 | bool* issued_error) | |
8038 | { | |
8039 | *issued_error = false; | |
8040 | ||
8041 | Type* var_type = NULL; | |
8042 | ||
8043 | // The simple case is passing the varargs parameter of the caller. | |
8044 | Var_expression* ve = arg->var_expression(); | |
8045 | if (ve != NULL && ve->named_object()->is_variable()) | |
8046 | { | |
8047 | Variable* var = ve->named_object()->var_value(); | |
8048 | if (var->is_varargs_parameter()) | |
8049 | var_type = var->type(); | |
8050 | } | |
8051 | ||
8052 | // The complex case is passing the varargs parameter of some | |
8053 | // enclosing function. This will look like passing down *c.f where | |
8054 | // c is the closure variable and f is a field in the closure. | |
8055 | if (function != NULL | |
8056 | && function->func_value()->needs_closure() | |
8057 | && arg->classification() == EXPRESSION_UNARY) | |
8058 | { | |
8059 | Unary_expression* ue = static_cast<Unary_expression*>(arg); | |
8060 | if (ue->op() == OPERATOR_MULT) | |
8061 | { | |
8062 | Field_reference_expression* fre = | |
8063 | ue->operand()->deref()->field_reference_expression(); | |
8064 | if (fre != NULL) | |
8065 | { | |
8066 | Var_expression* ve = fre->expr()->deref()->var_expression(); | |
8067 | if (ve != NULL) | |
8068 | { | |
8069 | Named_object* no = ve->named_object(); | |
8070 | Function* f = function->func_value(); | |
8071 | if (no == f->closure_var()) | |
8072 | { | |
8073 | // At this point we know that this indeed a | |
8074 | // reference to some enclosing variable. Now we | |
8075 | // need to figure out whether that variable is a | |
8076 | // varargs parameter. | |
8077 | Named_object* enclosing = | |
8078 | f->enclosing_var(fre->field_index()); | |
8079 | Variable* var = enclosing->var_value(); | |
8080 | if (var->is_varargs_parameter()) | |
8081 | var_type = var->type(); | |
8082 | } | |
8083 | } | |
8084 | } | |
8085 | } | |
8086 | } | |
8087 | ||
8088 | if (var_type == NULL) | |
8089 | return false; | |
8090 | ||
8091 | // We only match if the parameter is the same, with an identical | |
8092 | // type. | |
8093 | Array_type* var_at = var_type->array_type(); | |
8094 | gcc_assert(var_at != NULL); | |
8095 | Array_type* param_at = param_type->array_type(); | |
8096 | if (param_at != NULL | |
8097 | && Type::are_identical(var_at->element_type(), | |
8098 | param_at->element_type(), NULL)) | |
8099 | return true; | |
8100 | error_at(arg->location(), "... mismatch: passing ...T as ..."); | |
8101 | *issued_error = true; | |
8102 | return false; | |
8103 | } | |
8104 | ||
8105 | // Get the function type. Returns NULL if we don't know the type. If | |
8106 | // this returns NULL, and if_ERROR is true, issues an error. | |
8107 | ||
8108 | Function_type* | |
8109 | Call_expression::get_function_type() const | |
8110 | { | |
8111 | return this->fn_->type()->function_type(); | |
8112 | } | |
8113 | ||
8114 | // Return the number of values which this call will return. | |
8115 | ||
8116 | size_t | |
8117 | Call_expression::result_count() const | |
8118 | { | |
8119 | const Function_type* fntype = this->get_function_type(); | |
8120 | if (fntype == NULL) | |
8121 | return 0; | |
8122 | if (fntype->results() == NULL) | |
8123 | return 0; | |
8124 | return fntype->results()->size(); | |
8125 | } | |
8126 | ||
8127 | // Return whether this is a call to the predeclared function recover. | |
8128 | ||
8129 | bool | |
8130 | Call_expression::is_recover_call() const | |
8131 | { | |
8132 | return this->do_is_recover_call(); | |
8133 | } | |
8134 | ||
8135 | // Set the argument to the recover function. | |
8136 | ||
8137 | void | |
8138 | Call_expression::set_recover_arg(Expression* arg) | |
8139 | { | |
8140 | this->do_set_recover_arg(arg); | |
8141 | } | |
8142 | ||
8143 | // Virtual functions also implemented by Builtin_call_expression. | |
8144 | ||
8145 | bool | |
8146 | Call_expression::do_is_recover_call() const | |
8147 | { | |
8148 | return false; | |
8149 | } | |
8150 | ||
8151 | void | |
8152 | Call_expression::do_set_recover_arg(Expression*) | |
8153 | { | |
8154 | gcc_unreachable(); | |
8155 | } | |
8156 | ||
8157 | // Get the type. | |
8158 | ||
8159 | Type* | |
8160 | Call_expression::do_type() | |
8161 | { | |
8162 | if (this->type_ != NULL) | |
8163 | return this->type_; | |
8164 | ||
8165 | Type* ret; | |
8166 | Function_type* fntype = this->get_function_type(); | |
8167 | if (fntype == NULL) | |
8168 | return Type::make_error_type(); | |
8169 | ||
8170 | const Typed_identifier_list* results = fntype->results(); | |
8171 | if (results == NULL) | |
8172 | ret = Type::make_void_type(); | |
8173 | else if (results->size() == 1) | |
8174 | ret = results->begin()->type(); | |
8175 | else | |
8176 | ret = Type::make_call_multiple_result_type(this); | |
8177 | ||
8178 | this->type_ = ret; | |
8179 | ||
8180 | return this->type_; | |
8181 | } | |
8182 | ||
8183 | // Determine types for a call expression. We can use the function | |
8184 | // parameter types to set the types of the arguments. | |
8185 | ||
8186 | void | |
8187 | Call_expression::do_determine_type(const Type_context*) | |
8188 | { | |
8189 | this->fn_->determine_type_no_context(); | |
8190 | Function_type* fntype = this->get_function_type(); | |
8191 | const Typed_identifier_list* parameters = NULL; | |
8192 | if (fntype != NULL) | |
8193 | parameters = fntype->parameters(); | |
8194 | if (this->args_ != NULL) | |
8195 | { | |
8196 | Typed_identifier_list::const_iterator pt; | |
8197 | if (parameters != NULL) | |
8198 | pt = parameters->begin(); | |
8199 | for (Expression_list::const_iterator pa = this->args_->begin(); | |
8200 | pa != this->args_->end(); | |
8201 | ++pa) | |
8202 | { | |
8203 | if (parameters != NULL && pt != parameters->end()) | |
8204 | { | |
8205 | Type_context subcontext(pt->type(), false); | |
8206 | (*pa)->determine_type(&subcontext); | |
8207 | ++pt; | |
8208 | } | |
8209 | else | |
8210 | (*pa)->determine_type_no_context(); | |
8211 | } | |
8212 | } | |
8213 | } | |
8214 | ||
8215 | // Check types for parameter I. | |
8216 | ||
8217 | bool | |
8218 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
8219 | const Type* argument_type, | |
8220 | source_location argument_location, | |
8221 | bool issued_error) | |
8222 | { | |
8223 | std::string reason; | |
8224 | if (!Type::are_assignable(parameter_type, argument_type, &reason)) | |
8225 | { | |
8226 | if (!issued_error) | |
8227 | { | |
8228 | if (reason.empty()) | |
8229 | error_at(argument_location, "argument %d has incompatible type", i); | |
8230 | else | |
8231 | error_at(argument_location, | |
8232 | "argument %d has incompatible type (%s)", | |
8233 | i, reason.c_str()); | |
8234 | } | |
8235 | this->set_is_error(); | |
8236 | return false; | |
8237 | } | |
8238 | return true; | |
8239 | } | |
8240 | ||
8241 | // Check types. | |
8242 | ||
8243 | void | |
8244 | Call_expression::do_check_types(Gogo*) | |
8245 | { | |
8246 | Function_type* fntype = this->get_function_type(); | |
8247 | if (fntype == NULL) | |
8248 | { | |
8249 | if (!this->fn_->type()->is_error_type()) | |
8250 | this->report_error(_("expected function")); | |
8251 | return; | |
8252 | } | |
8253 | ||
8254 | if (fntype->is_method()) | |
8255 | { | |
8256 | // We don't support pointers to methods, so the function has to | |
8257 | // be a bound method expression. | |
8258 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
8259 | if (bme == NULL) | |
8260 | { | |
8261 | this->report_error(_("method call without object")); | |
8262 | return; | |
8263 | } | |
8264 | Type* first_arg_type = bme->first_argument()->type(); | |
8265 | if (first_arg_type->points_to() == NULL) | |
8266 | { | |
8267 | // When passing a value, we need to check that we are | |
8268 | // permitted to copy it. | |
8269 | std::string reason; | |
8270 | if (!Type::are_assignable(fntype->receiver()->type(), | |
8271 | first_arg_type, &reason)) | |
8272 | { | |
8273 | if (reason.empty()) | |
8274 | this->report_error(_("incompatible type for receiver")); | |
8275 | else | |
8276 | { | |
8277 | error_at(this->location(), | |
8278 | "incompatible type for receiver (%s)", | |
8279 | reason.c_str()); | |
8280 | this->set_is_error(); | |
8281 | } | |
8282 | } | |
8283 | } | |
8284 | } | |
8285 | ||
8286 | // Note that varargs was handled by the lower_varargs() method, so | |
8287 | // we don't have to worry about it here. | |
8288 | ||
8289 | const Typed_identifier_list* parameters = fntype->parameters(); | |
8290 | if (this->args_ == NULL) | |
8291 | { | |
8292 | if (parameters != NULL && !parameters->empty()) | |
8293 | this->report_error(_("not enough arguments")); | |
8294 | } | |
8295 | else if (parameters == NULL) | |
8296 | this->report_error(_("too many arguments")); | |
8297 | else | |
8298 | { | |
8299 | int i = 0; | |
8300 | Typed_identifier_list::const_iterator pt = parameters->begin(); | |
8301 | for (Expression_list::const_iterator pa = this->args_->begin(); | |
8302 | pa != this->args_->end(); | |
8303 | ++pa, ++pt, ++i) | |
8304 | { | |
8305 | if (pt == parameters->end()) | |
8306 | { | |
8307 | this->report_error(_("too many arguments")); | |
8308 | return; | |
8309 | } | |
8310 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
8311 | (*pa)->location(), false); | |
8312 | } | |
8313 | if (pt != parameters->end()) | |
8314 | this->report_error(_("not enough arguments")); | |
8315 | } | |
8316 | } | |
8317 | ||
8318 | // Return whether we have to use a temporary variable to ensure that | |
8319 | // we evaluate this call expression in order. If the call returns no | |
8320 | // results then it will inevitably be executed last. If the call | |
8321 | // returns more than one result then it will be used with Call_result | |
8322 | // expressions. So we only have to use a temporary variable if the | |
8323 | // call returns exactly one result. | |
8324 | ||
8325 | bool | |
8326 | Call_expression::do_must_eval_in_order() const | |
8327 | { | |
8328 | return this->result_count() == 1; | |
8329 | } | |
8330 | ||
8331 | // Get the function and the first argument to use when calling a bound | |
8332 | // method. | |
8333 | ||
8334 | tree | |
8335 | Call_expression::bound_method_function(Translate_context* context, | |
8336 | Bound_method_expression* bound_method, | |
8337 | tree* first_arg_ptr) | |
8338 | { | |
8339 | Expression* first_argument = bound_method->first_argument(); | |
8340 | tree first_arg = first_argument->get_tree(context); | |
8341 | if (first_arg == error_mark_node) | |
8342 | return error_mark_node; | |
8343 | ||
8344 | // We always pass a pointer to the first argument when calling a | |
8345 | // method. | |
8346 | if (first_argument->type()->points_to() == NULL) | |
8347 | { | |
8348 | tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg)); | |
8349 | if (TREE_ADDRESSABLE(TREE_TYPE(first_arg)) | |
8350 | || DECL_P(first_arg) | |
8351 | || TREE_CODE(first_arg) == INDIRECT_REF | |
8352 | || TREE_CODE(first_arg) == COMPONENT_REF) | |
8353 | { | |
8354 | first_arg = build_fold_addr_expr(first_arg); | |
8355 | if (DECL_P(first_arg)) | |
8356 | TREE_ADDRESSABLE(first_arg) = 1; | |
8357 | } | |
8358 | else | |
8359 | { | |
8360 | tree tmp = create_tmp_var(TREE_TYPE(first_arg), | |
8361 | get_name(first_arg)); | |
8362 | DECL_IGNORED_P(tmp) = 0; | |
8363 | DECL_INITIAL(tmp) = first_arg; | |
8364 | first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type, | |
8365 | build1(DECL_EXPR, void_type_node, tmp), | |
8366 | build_fold_addr_expr(tmp)); | |
8367 | TREE_ADDRESSABLE(tmp) = 1; | |
8368 | } | |
8369 | if (first_arg == error_mark_node) | |
8370 | return error_mark_node; | |
8371 | } | |
8372 | ||
8373 | Type* fatype = bound_method->first_argument_type(); | |
8374 | if (fatype != NULL) | |
8375 | { | |
8376 | if (fatype->points_to() == NULL) | |
8377 | fatype = Type::make_pointer_type(fatype); | |
8378 | first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg); | |
8379 | if (first_arg == error_mark_node | |
8380 | || TREE_TYPE(first_arg) == error_mark_node) | |
8381 | return error_mark_node; | |
8382 | } | |
8383 | ||
8384 | *first_arg_ptr = first_arg; | |
8385 | ||
8386 | return bound_method->method()->get_tree(context); | |
8387 | } | |
8388 | ||
8389 | // Get the function and the first argument to use when calling an | |
8390 | // interface method. | |
8391 | ||
8392 | tree | |
8393 | Call_expression::interface_method_function( | |
8394 | Translate_context* context, | |
8395 | Interface_field_reference_expression* interface_method, | |
8396 | tree* first_arg_ptr) | |
8397 | { | |
8398 | tree expr = interface_method->expr()->get_tree(context); | |
8399 | if (expr == error_mark_node) | |
8400 | return error_mark_node; | |
8401 | expr = save_expr(expr); | |
8402 | tree first_arg = interface_method->get_underlying_object_tree(context, expr); | |
8403 | if (first_arg == error_mark_node) | |
8404 | return error_mark_node; | |
8405 | *first_arg_ptr = first_arg; | |
8406 | return interface_method->get_function_tree(context, expr); | |
8407 | } | |
8408 | ||
8409 | // Build the call expression. | |
8410 | ||
8411 | tree | |
8412 | Call_expression::do_get_tree(Translate_context* context) | |
8413 | { | |
8414 | if (this->tree_ != NULL_TREE) | |
8415 | return this->tree_; | |
8416 | ||
8417 | Function_type* fntype = this->get_function_type(); | |
8418 | if (fntype == NULL) | |
8419 | return error_mark_node; | |
8420 | ||
8421 | if (this->fn_->is_error_expression()) | |
8422 | return error_mark_node; | |
8423 | ||
8424 | Gogo* gogo = context->gogo(); | |
8425 | source_location location = this->location(); | |
8426 | ||
8427 | Func_expression* func = this->fn_->func_expression(); | |
8428 | Bound_method_expression* bound_method = this->fn_->bound_method_expression(); | |
8429 | Interface_field_reference_expression* interface_method = | |
8430 | this->fn_->interface_field_reference_expression(); | |
8431 | const bool has_closure = func != NULL && func->closure() != NULL; | |
8432 | const bool is_method = bound_method != NULL || interface_method != NULL; | |
8433 | gcc_assert(!fntype->is_method() || is_method); | |
8434 | ||
8435 | int nargs; | |
8436 | tree* args; | |
8437 | if (this->args_ == NULL || this->args_->empty()) | |
8438 | { | |
8439 | nargs = is_method ? 1 : 0; | |
8440 | args = nargs == 0 ? NULL : new tree[nargs]; | |
8441 | } | |
8442 | else | |
8443 | { | |
8444 | const Typed_identifier_list* params = fntype->parameters(); | |
8445 | gcc_assert(params != NULL); | |
8446 | ||
8447 | nargs = this->args_->size(); | |
8448 | int i = is_method ? 1 : 0; | |
8449 | nargs += i; | |
8450 | args = new tree[nargs]; | |
8451 | ||
8452 | Typed_identifier_list::const_iterator pp = params->begin(); | |
8453 | Expression_list::const_iterator pe; | |
8454 | for (pe = this->args_->begin(); | |
8455 | pe != this->args_->end(); | |
8456 | ++pe, ++pp, ++i) | |
8457 | { | |
8458 | tree arg_val = (*pe)->get_tree(context); | |
8459 | args[i] = Expression::convert_for_assignment(context, | |
8460 | pp->type(), | |
8461 | (*pe)->type(), | |
8462 | arg_val, | |
8463 | location); | |
8464 | if (args[i] == error_mark_node) | |
8465 | return error_mark_node; | |
8466 | } | |
8467 | gcc_assert(pp == params->end()); | |
8468 | gcc_assert(i == nargs); | |
8469 | } | |
8470 | ||
8471 | tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo))); | |
8472 | if (rettype == error_mark_node) | |
8473 | return error_mark_node; | |
8474 | ||
8475 | tree fn; | |
8476 | if (has_closure) | |
8477 | fn = func->get_tree_without_closure(gogo); | |
8478 | else if (!is_method) | |
8479 | fn = this->fn_->get_tree(context); | |
8480 | else if (bound_method != NULL) | |
8481 | fn = this->bound_method_function(context, bound_method, &args[0]); | |
8482 | else if (interface_method != NULL) | |
8483 | fn = this->interface_method_function(context, interface_method, &args[0]); | |
8484 | else | |
8485 | gcc_unreachable(); | |
8486 | ||
8487 | if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node) | |
8488 | return error_mark_node; | |
8489 | ||
8490 | // This is to support builtin math functions when using 80387 math. | |
8491 | tree fndecl = fn; | |
8492 | if (TREE_CODE(fndecl) == ADDR_EXPR) | |
8493 | fndecl = TREE_OPERAND(fndecl, 0); | |
8494 | tree excess_type = NULL_TREE; | |
8495 | if (DECL_P(fndecl) | |
8496 | && DECL_IS_BUILTIN(fndecl) | |
8497 | && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL | |
8498 | && nargs > 0 | |
8499 | && ((SCALAR_FLOAT_TYPE_P(rettype) | |
8500 | && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0]))) | |
8501 | || (COMPLEX_FLOAT_TYPE_P(rettype) | |
8502 | && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0]))))) | |
8503 | { | |
8504 | excess_type = excess_precision_type(TREE_TYPE(args[0])); | |
8505 | if (excess_type != NULL_TREE) | |
8506 | { | |
8507 | tree excess_fndecl = mathfn_built_in(excess_type, | |
8508 | DECL_FUNCTION_CODE(fndecl)); | |
8509 | if (excess_fndecl == NULL_TREE) | |
8510 | excess_type = NULL_TREE; | |
8511 | else | |
8512 | { | |
8513 | fn = build_fold_addr_expr_loc(location, excess_fndecl); | |
8514 | for (int i = 0; i < nargs; ++i) | |
8515 | args[i] = ::convert(excess_type, args[i]); | |
8516 | } | |
8517 | } | |
8518 | } | |
8519 | ||
8520 | tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype, | |
8521 | fn, nargs, args); | |
8522 | delete[] args; | |
8523 | ||
8524 | SET_EXPR_LOCATION(ret, location); | |
8525 | ||
8526 | if (has_closure) | |
8527 | { | |
8528 | tree closure_tree = func->closure()->get_tree(context); | |
8529 | if (closure_tree != error_mark_node) | |
8530 | CALL_EXPR_STATIC_CHAIN(ret) = closure_tree; | |
8531 | } | |
8532 | ||
8533 | // If this is a recursive function type which returns itself, as in | |
8534 | // type F func() F | |
8535 | // we have used ptr_type_node for the return type. Add a cast here | |
8536 | // to the correct type. | |
8537 | if (TREE_TYPE(ret) == ptr_type_node) | |
8538 | { | |
8539 | tree t = this->type()->get_tree(gogo); | |
8540 | ret = fold_convert_loc(location, t, ret); | |
8541 | } | |
8542 | ||
8543 | if (excess_type != NULL_TREE) | |
8544 | { | |
8545 | // Calling convert here can undo our excess precision change. | |
8546 | // That may or may not be a bug in convert_to_real. | |
8547 | ret = build1(NOP_EXPR, rettype, ret); | |
8548 | } | |
8549 | ||
8550 | // If there is more than one result, we will refer to the call | |
8551 | // multiple times. | |
8552 | if (fntype->results() != NULL && fntype->results()->size() > 1) | |
8553 | ret = save_expr(ret); | |
8554 | ||
8555 | this->tree_ = ret; | |
8556 | ||
8557 | return ret; | |
8558 | } | |
8559 | ||
8560 | // Make a call expression. | |
8561 | ||
8562 | Call_expression* | |
8563 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
8564 | source_location location) | |
8565 | { | |
8566 | return new Call_expression(fn, args, is_varargs, location); | |
8567 | } | |
8568 | ||
8569 | // A single result from a call which returns multiple results. | |
8570 | ||
8571 | class Call_result_expression : public Expression | |
8572 | { | |
8573 | public: | |
8574 | Call_result_expression(Call_expression* call, unsigned int index) | |
8575 | : Expression(EXPRESSION_CALL_RESULT, call->location()), | |
8576 | call_(call), index_(index) | |
8577 | { } | |
8578 | ||
8579 | protected: | |
8580 | int | |
8581 | do_traverse(Traverse*); | |
8582 | ||
8583 | Type* | |
8584 | do_type(); | |
8585 | ||
8586 | void | |
8587 | do_determine_type(const Type_context*); | |
8588 | ||
8589 | void | |
8590 | do_check_types(Gogo*); | |
8591 | ||
8592 | Expression* | |
8593 | do_copy() | |
8594 | { | |
8595 | return new Call_result_expression(this->call_->call_expression(), | |
8596 | this->index_); | |
8597 | } | |
8598 | ||
8599 | bool | |
8600 | do_must_eval_in_order() const | |
8601 | { return true; } | |
8602 | ||
8603 | tree | |
8604 | do_get_tree(Translate_context*); | |
8605 | ||
8606 | private: | |
8607 | // The underlying call expression. | |
8608 | Expression* call_; | |
8609 | // Which result we want. | |
8610 | unsigned int index_; | |
8611 | }; | |
8612 | ||
8613 | // Traverse a call result. | |
8614 | ||
8615 | int | |
8616 | Call_result_expression::do_traverse(Traverse* traverse) | |
8617 | { | |
8618 | if (traverse->remember_expression(this->call_)) | |
8619 | { | |
8620 | // We have already traversed the call expression. | |
8621 | return TRAVERSE_CONTINUE; | |
8622 | } | |
8623 | return Expression::traverse(&this->call_, traverse); | |
8624 | } | |
8625 | ||
8626 | // Get the type. | |
8627 | ||
8628 | Type* | |
8629 | Call_result_expression::do_type() | |
8630 | { | |
8631 | // THIS->CALL_ can be replaced with a temporary reference due to | |
8632 | // Call_expression::do_must_eval_in_order when there is an error. | |
8633 | Call_expression* ce = this->call_->call_expression(); | |
8634 | if (ce == NULL) | |
8635 | return Type::make_error_type(); | |
8636 | Function_type* fntype = ce->get_function_type(); | |
8637 | if (fntype == NULL) | |
8638 | return Type::make_error_type(); | |
8639 | const Typed_identifier_list* results = fntype->results(); | |
8640 | Typed_identifier_list::const_iterator pr = results->begin(); | |
8641 | for (unsigned int i = 0; i < this->index_; ++i) | |
8642 | { | |
8643 | if (pr == results->end()) | |
8644 | return Type::make_error_type(); | |
8645 | ++pr; | |
8646 | } | |
8647 | if (pr == results->end()) | |
8648 | return Type::make_error_type(); | |
8649 | return pr->type(); | |
8650 | } | |
8651 | ||
8652 | // Check the type. This is where we give an error if we're trying to | |
8653 | // extract too many values from a call. | |
8654 | ||
8655 | void | |
8656 | Call_result_expression::do_check_types(Gogo*) | |
8657 | { | |
8658 | bool ok = true; | |
8659 | Call_expression* ce = this->call_->call_expression(); | |
8660 | if (ce != NULL) | |
8661 | ok = this->index_ < ce->result_count(); | |
8662 | else | |
8663 | { | |
8664 | // This can happen when the call returns a single value but we | |
8665 | // are asking for the second result. | |
8666 | if (this->call_->is_error_expression()) | |
8667 | return; | |
8668 | ok = false; | |
8669 | } | |
8670 | if (!ok) | |
8671 | error_at(this->location(), | |
8672 | "number of results does not match number of values"); | |
8673 | } | |
8674 | ||
8675 | // Determine the type. We have nothing to do here, but the 0 result | |
8676 | // needs to pass down to the caller. | |
8677 | ||
8678 | void | |
8679 | Call_result_expression::do_determine_type(const Type_context*) | |
8680 | { | |
8681 | if (this->index_ == 0) | |
8682 | this->call_->determine_type_no_context(); | |
8683 | } | |
8684 | ||
8685 | // Return the tree. | |
8686 | ||
8687 | tree | |
8688 | Call_result_expression::do_get_tree(Translate_context* context) | |
8689 | { | |
8690 | tree call_tree = this->call_->get_tree(context); | |
8691 | if (call_tree == error_mark_node) | |
8692 | return error_mark_node; | |
8693 | gcc_assert(TREE_CODE(TREE_TYPE(call_tree)) == RECORD_TYPE); | |
8694 | tree field = TYPE_FIELDS(TREE_TYPE(call_tree)); | |
8695 | for (unsigned int i = 0; i < this->index_; ++i) | |
8696 | { | |
8697 | gcc_assert(field != NULL_TREE); | |
8698 | field = DECL_CHAIN(field); | |
8699 | } | |
8700 | gcc_assert(field != NULL_TREE); | |
8701 | return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE); | |
8702 | } | |
8703 | ||
8704 | // Make a reference to a single result of a call which returns | |
8705 | // multiple results. | |
8706 | ||
8707 | Expression* | |
8708 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
8709 | { | |
8710 | return new Call_result_expression(call, index); | |
8711 | } | |
8712 | ||
8713 | // Class Index_expression. | |
8714 | ||
8715 | // Traversal. | |
8716 | ||
8717 | int | |
8718 | Index_expression::do_traverse(Traverse* traverse) | |
8719 | { | |
8720 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
8721 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
8722 | || (this->end_ != NULL | |
8723 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)) | |
8724 | return TRAVERSE_EXIT; | |
8725 | return TRAVERSE_CONTINUE; | |
8726 | } | |
8727 | ||
8728 | // Lower an index expression. This converts the generic index | |
8729 | // expression into an array index, a string index, or a map index. | |
8730 | ||
8731 | Expression* | |
8732 | Index_expression::do_lower(Gogo*, Named_object*, int) | |
8733 | { | |
8734 | source_location location = this->location(); | |
8735 | Expression* left = this->left_; | |
8736 | Expression* start = this->start_; | |
8737 | Expression* end = this->end_; | |
8738 | ||
8739 | Type* type = left->type(); | |
8740 | if (type->is_error_type()) | |
8741 | return Expression::make_error(location); | |
8742 | else if (type->array_type() != NULL) | |
8743 | return Expression::make_array_index(left, start, end, location); | |
8744 | else if (type->points_to() != NULL | |
8745 | && type->points_to()->array_type() != NULL | |
8746 | && !type->points_to()->is_open_array_type()) | |
8747 | { | |
8748 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
8749 | location); | |
8750 | return Expression::make_array_index(deref, start, end, location); | |
8751 | } | |
8752 | else if (type->is_string_type()) | |
8753 | return Expression::make_string_index(left, start, end, location); | |
8754 | else if (type->map_type() != NULL) | |
8755 | { | |
8756 | if (end != NULL) | |
8757 | { | |
8758 | error_at(location, "invalid slice of map"); | |
8759 | return Expression::make_error(location); | |
8760 | } | |
8761 | Map_index_expression* ret= Expression::make_map_index(left, start, | |
8762 | location); | |
8763 | if (this->is_lvalue_) | |
8764 | ret->set_is_lvalue(); | |
8765 | return ret; | |
8766 | } | |
8767 | else | |
8768 | { | |
8769 | error_at(location, | |
8770 | "attempt to index object which is not array, string, or map"); | |
8771 | return Expression::make_error(location); | |
8772 | } | |
8773 | } | |
8774 | ||
8775 | // Make an index expression. | |
8776 | ||
8777 | Expression* | |
8778 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
8779 | source_location location) | |
8780 | { | |
8781 | return new Index_expression(left, start, end, location); | |
8782 | } | |
8783 | ||
8784 | // An array index. This is used for both indexing and slicing. | |
8785 | ||
8786 | class Array_index_expression : public Expression | |
8787 | { | |
8788 | public: | |
8789 | Array_index_expression(Expression* array, Expression* start, | |
8790 | Expression* end, source_location location) | |
8791 | : Expression(EXPRESSION_ARRAY_INDEX, location), | |
8792 | array_(array), start_(start), end_(end), type_(NULL) | |
8793 | { } | |
8794 | ||
8795 | protected: | |
8796 | int | |
8797 | do_traverse(Traverse*); | |
8798 | ||
8799 | Type* | |
8800 | do_type(); | |
8801 | ||
8802 | void | |
8803 | do_determine_type(const Type_context*); | |
8804 | ||
8805 | void | |
8806 | do_check_types(Gogo*); | |
8807 | ||
8808 | Expression* | |
8809 | do_copy() | |
8810 | { | |
8811 | return Expression::make_array_index(this->array_->copy(), | |
8812 | this->start_->copy(), | |
8813 | (this->end_ == NULL | |
8814 | ? NULL | |
8815 | : this->end_->copy()), | |
8816 | this->location()); | |
8817 | } | |
8818 | ||
8819 | bool | |
8820 | do_is_addressable() const; | |
8821 | ||
8822 | void | |
8823 | do_address_taken(bool escapes) | |
8824 | { this->array_->address_taken(escapes); } | |
8825 | ||
8826 | tree | |
8827 | do_get_tree(Translate_context*); | |
8828 | ||
8829 | private: | |
8830 | // The array we are getting a value from. | |
8831 | Expression* array_; | |
8832 | // The start or only index. | |
8833 | Expression* start_; | |
8834 | // The end index of a slice. This may be NULL for a simple array | |
8835 | // index, or it may be a nil expression for the length of the array. | |
8836 | Expression* end_; | |
8837 | // The type of the expression. | |
8838 | Type* type_; | |
8839 | }; | |
8840 | ||
8841 | // Array index traversal. | |
8842 | ||
8843 | int | |
8844 | Array_index_expression::do_traverse(Traverse* traverse) | |
8845 | { | |
8846 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
8847 | return TRAVERSE_EXIT; | |
8848 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
8849 | return TRAVERSE_EXIT; | |
8850 | if (this->end_ != NULL) | |
8851 | { | |
8852 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
8853 | return TRAVERSE_EXIT; | |
8854 | } | |
8855 | return TRAVERSE_CONTINUE; | |
8856 | } | |
8857 | ||
8858 | // Return the type of an array index. | |
8859 | ||
8860 | Type* | |
8861 | Array_index_expression::do_type() | |
8862 | { | |
8863 | if (this->type_ == NULL) | |
8864 | { | |
8865 | Array_type* type = this->array_->type()->array_type(); | |
8866 | if (type == NULL) | |
8867 | this->type_ = Type::make_error_type(); | |
8868 | else if (this->end_ == NULL) | |
8869 | this->type_ = type->element_type(); | |
8870 | else if (type->is_open_array_type()) | |
8871 | { | |
8872 | // A slice of a slice has the same type as the original | |
8873 | // slice. | |
8874 | this->type_ = this->array_->type()->deref(); | |
8875 | } | |
8876 | else | |
8877 | { | |
8878 | // A slice of an array is a slice. | |
8879 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
8880 | } | |
8881 | } | |
8882 | return this->type_; | |
8883 | } | |
8884 | ||
8885 | // Set the type of an array index. | |
8886 | ||
8887 | void | |
8888 | Array_index_expression::do_determine_type(const Type_context*) | |
8889 | { | |
8890 | this->array_->determine_type_no_context(); | |
8891 | Type_context subcontext(NULL, true); | |
8892 | this->start_->determine_type(&subcontext); | |
8893 | if (this->end_ != NULL) | |
8894 | this->end_->determine_type(&subcontext); | |
8895 | } | |
8896 | ||
8897 | // Check types of an array index. | |
8898 | ||
8899 | void | |
8900 | Array_index_expression::do_check_types(Gogo*) | |
8901 | { | |
8902 | if (this->start_->type()->integer_type() == NULL) | |
8903 | this->report_error(_("index must be integer")); | |
8904 | if (this->end_ != NULL | |
8905 | && this->end_->type()->integer_type() == NULL | |
8906 | && !this->end_->is_nil_expression()) | |
8907 | this->report_error(_("slice end must be integer")); | |
8908 | ||
8909 | Array_type* array_type = this->array_->type()->array_type(); | |
8910 | gcc_assert(array_type != NULL); | |
8911 | ||
8912 | unsigned int int_bits = | |
8913 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
8914 | ||
8915 | Type* dummy; | |
8916 | mpz_t lval; | |
8917 | mpz_init(lval); | |
8918 | bool lval_valid = (array_type->length() != NULL | |
8919 | && array_type->length()->integer_constant_value(true, | |
8920 | lval, | |
8921 | &dummy)); | |
8922 | mpz_t ival; | |
8923 | mpz_init(ival); | |
8924 | if (this->start_->integer_constant_value(true, ival, &dummy)) | |
8925 | { | |
8926 | if (mpz_sgn(ival) < 0 | |
8927 | || mpz_sizeinbase(ival, 2) >= int_bits | |
8928 | || (lval_valid | |
8929 | && (this->end_ == NULL | |
8930 | ? mpz_cmp(ival, lval) >= 0 | |
8931 | : mpz_cmp(ival, lval) > 0))) | |
8932 | { | |
8933 | error_at(this->start_->location(), "array index out of bounds"); | |
8934 | this->set_is_error(); | |
8935 | } | |
8936 | } | |
8937 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
8938 | { | |
8939 | if (this->end_->integer_constant_value(true, ival, &dummy)) | |
8940 | { | |
8941 | if (mpz_sgn(ival) < 0 | |
8942 | || mpz_sizeinbase(ival, 2) >= int_bits | |
8943 | || (lval_valid && mpz_cmp(ival, lval) > 0)) | |
8944 | { | |
8945 | error_at(this->end_->location(), "array index out of bounds"); | |
8946 | this->set_is_error(); | |
8947 | } | |
8948 | } | |
8949 | } | |
8950 | mpz_clear(ival); | |
8951 | mpz_clear(lval); | |
8952 | ||
8953 | // A slice of an array requires an addressable array. A slice of a | |
8954 | // slice is always possible. | |
8955 | if (this->end_ != NULL | |
8956 | && !array_type->is_open_array_type() | |
8957 | && !this->array_->is_addressable()) | |
8958 | this->report_error(_("array is not addressable")); | |
8959 | } | |
8960 | ||
8961 | // Return whether this expression is addressable. | |
8962 | ||
8963 | bool | |
8964 | Array_index_expression::do_is_addressable() const | |
8965 | { | |
8966 | // A slice expression is not addressable. | |
8967 | if (this->end_ != NULL) | |
8968 | return false; | |
8969 | ||
8970 | // An index into a slice is addressable. | |
8971 | if (this->array_->type()->is_open_array_type()) | |
8972 | return true; | |
8973 | ||
8974 | // An index into an array is addressable if the array is | |
8975 | // addressable. | |
8976 | return this->array_->is_addressable(); | |
8977 | } | |
8978 | ||
8979 | // Get a tree for an array index. | |
8980 | ||
8981 | tree | |
8982 | Array_index_expression::do_get_tree(Translate_context* context) | |
8983 | { | |
8984 | Gogo* gogo = context->gogo(); | |
8985 | source_location loc = this->location(); | |
8986 | ||
8987 | Array_type* array_type = this->array_->type()->array_type(); | |
8988 | gcc_assert(array_type != NULL); | |
8989 | ||
8990 | tree type_tree = array_type->get_tree(gogo); | |
8991 | ||
8992 | tree array_tree = this->array_->get_tree(context); | |
8993 | if (array_tree == error_mark_node) | |
8994 | return error_mark_node; | |
8995 | ||
8996 | if (array_type->length() == NULL && !DECL_P(array_tree)) | |
8997 | array_tree = save_expr(array_tree); | |
8998 | tree length_tree = array_type->length_tree(gogo, array_tree); | |
8999 | length_tree = save_expr(length_tree); | |
9000 | tree length_type = TREE_TYPE(length_tree); | |
9001 | ||
9002 | tree bad_index = boolean_false_node; | |
9003 | ||
9004 | tree start_tree = this->start_->get_tree(context); | |
9005 | if (start_tree == error_mark_node) | |
9006 | return error_mark_node; | |
9007 | if (!DECL_P(start_tree)) | |
9008 | start_tree = save_expr(start_tree); | |
9009 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
9010 | start_tree = convert_to_integer(length_type, start_tree); | |
9011 | ||
9012 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
9013 | loc); | |
9014 | ||
9015 | start_tree = fold_convert_loc(loc, length_type, start_tree); | |
9016 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index, | |
9017 | fold_build2_loc(loc, | |
9018 | (this->end_ == NULL | |
9019 | ? GE_EXPR | |
9020 | : GT_EXPR), | |
9021 | boolean_type_node, start_tree, | |
9022 | length_tree)); | |
9023 | ||
9024 | int code = (array_type->length() != NULL | |
9025 | ? (this->end_ == NULL | |
9026 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
9027 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
9028 | : (this->end_ == NULL | |
9029 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
9030 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
9031 | tree crash = Gogo::runtime_error(code, loc); | |
9032 | ||
9033 | if (this->end_ == NULL) | |
9034 | { | |
9035 | // Simple array indexing. This has to return an l-value, so | |
9036 | // wrap the index check into START_TREE. | |
9037 | start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree), | |
9038 | build3(COND_EXPR, void_type_node, | |
9039 | bad_index, crash, NULL_TREE), | |
9040 | start_tree); | |
9041 | start_tree = fold_convert_loc(loc, sizetype, start_tree); | |
9042 | ||
9043 | if (array_type->length() != NULL) | |
9044 | { | |
9045 | // Fixed array. | |
9046 | return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree, | |
9047 | start_tree, NULL_TREE, NULL_TREE); | |
9048 | } | |
9049 | else | |
9050 | { | |
9051 | // Open array. | |
9052 | tree values = array_type->value_pointer_tree(gogo, array_tree); | |
9053 | tree element_type_tree = array_type->element_type()->get_tree(gogo); | |
9054 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); | |
9055 | tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype, | |
9056 | start_tree, element_size); | |
9057 | tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, | |
9058 | TREE_TYPE(values), values, offset); | |
9059 | return build_fold_indirect_ref(ptr); | |
9060 | } | |
9061 | } | |
9062 | ||
9063 | // Array slice. | |
9064 | ||
9065 | tree capacity_tree = array_type->capacity_tree(gogo, array_tree); | |
9066 | capacity_tree = fold_convert_loc(loc, length_type, capacity_tree); | |
9067 | ||
9068 | tree end_tree; | |
9069 | if (this->end_->is_nil_expression()) | |
9070 | end_tree = length_tree; | |
9071 | else | |
9072 | { | |
9073 | end_tree = this->end_->get_tree(context); | |
9074 | if (end_tree == error_mark_node) | |
9075 | return error_mark_node; | |
9076 | if (!DECL_P(end_tree)) | |
9077 | end_tree = save_expr(end_tree); | |
9078 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
9079 | end_tree = convert_to_integer(length_type, end_tree); | |
9080 | ||
9081 | bad_index = Expression::check_bounds(end_tree, length_type, bad_index, | |
9082 | loc); | |
9083 | ||
9084 | end_tree = fold_convert_loc(loc, length_type, end_tree); | |
9085 | ||
9086 | capacity_tree = save_expr(capacity_tree); | |
9087 | tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9088 | fold_build2_loc(loc, LT_EXPR, | |
9089 | boolean_type_node, | |
9090 | end_tree, start_tree), | |
9091 | fold_build2_loc(loc, GT_EXPR, | |
9092 | boolean_type_node, | |
9093 | end_tree, capacity_tree)); | |
9094 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9095 | bad_index, bad_end); | |
9096 | } | |
9097 | ||
9098 | tree element_type_tree = array_type->element_type()->get_tree(gogo); | |
9099 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); | |
9100 | ||
9101 | tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype, | |
9102 | fold_convert_loc(loc, sizetype, start_tree), | |
9103 | element_size); | |
9104 | ||
9105 | tree value_pointer = array_type->value_pointer_tree(gogo, array_tree); | |
9106 | ||
9107 | value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR, | |
9108 | TREE_TYPE(value_pointer), | |
9109 | value_pointer, offset); | |
9110 | ||
9111 | tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type, | |
9112 | end_tree, start_tree); | |
9113 | ||
9114 | tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type, | |
9115 | capacity_tree, start_tree); | |
9116 | ||
9117 | tree struct_tree = this->type()->get_tree(gogo); | |
9118 | gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE); | |
9119 | ||
9120 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
9121 | ||
9122 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
9123 | tree field = TYPE_FIELDS(struct_tree); | |
9124 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); | |
9125 | elt->index = field; | |
9126 | elt->value = value_pointer; | |
9127 | ||
9128 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
9129 | field = DECL_CHAIN(field); | |
9130 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); | |
9131 | elt->index = field; | |
9132 | elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree); | |
9133 | ||
9134 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
9135 | field = DECL_CHAIN(field); | |
9136 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0); | |
9137 | elt->index = field; | |
9138 | elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree); | |
9139 | ||
9140 | tree constructor = build_constructor(struct_tree, init); | |
9141 | ||
9142 | if (TREE_CONSTANT(value_pointer) | |
9143 | && TREE_CONSTANT(result_length_tree) | |
9144 | && TREE_CONSTANT(result_capacity_tree)) | |
9145 | TREE_CONSTANT(constructor) = 1; | |
9146 | ||
9147 | return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor), | |
9148 | build3(COND_EXPR, void_type_node, | |
9149 | bad_index, crash, NULL_TREE), | |
9150 | constructor); | |
9151 | } | |
9152 | ||
9153 | // Make an array index expression. END may be NULL. | |
9154 | ||
9155 | Expression* | |
9156 | Expression::make_array_index(Expression* array, Expression* start, | |
9157 | Expression* end, source_location location) | |
9158 | { | |
9159 | // Taking a slice of a composite literal requires moving the literal | |
9160 | // onto the heap. | |
9161 | if (end != NULL && array->is_composite_literal()) | |
9162 | { | |
9163 | array = Expression::make_heap_composite(array, location); | |
9164 | array = Expression::make_unary(OPERATOR_MULT, array, location); | |
9165 | } | |
9166 | return new Array_index_expression(array, start, end, location); | |
9167 | } | |
9168 | ||
9169 | // A string index. This is used for both indexing and slicing. | |
9170 | ||
9171 | class String_index_expression : public Expression | |
9172 | { | |
9173 | public: | |
9174 | String_index_expression(Expression* string, Expression* start, | |
9175 | Expression* end, source_location location) | |
9176 | : Expression(EXPRESSION_STRING_INDEX, location), | |
9177 | string_(string), start_(start), end_(end) | |
9178 | { } | |
9179 | ||
9180 | protected: | |
9181 | int | |
9182 | do_traverse(Traverse*); | |
9183 | ||
9184 | Type* | |
9185 | do_type(); | |
9186 | ||
9187 | void | |
9188 | do_determine_type(const Type_context*); | |
9189 | ||
9190 | void | |
9191 | do_check_types(Gogo*); | |
9192 | ||
9193 | Expression* | |
9194 | do_copy() | |
9195 | { | |
9196 | return Expression::make_string_index(this->string_->copy(), | |
9197 | this->start_->copy(), | |
9198 | (this->end_ == NULL | |
9199 | ? NULL | |
9200 | : this->end_->copy()), | |
9201 | this->location()); | |
9202 | } | |
9203 | ||
9204 | tree | |
9205 | do_get_tree(Translate_context*); | |
9206 | ||
9207 | private: | |
9208 | // The string we are getting a value from. | |
9209 | Expression* string_; | |
9210 | // The start or only index. | |
9211 | Expression* start_; | |
9212 | // The end index of a slice. This may be NULL for a single index, | |
9213 | // or it may be a nil expression for the length of the string. | |
9214 | Expression* end_; | |
9215 | }; | |
9216 | ||
9217 | // String index traversal. | |
9218 | ||
9219 | int | |
9220 | String_index_expression::do_traverse(Traverse* traverse) | |
9221 | { | |
9222 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
9223 | return TRAVERSE_EXIT; | |
9224 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
9225 | return TRAVERSE_EXIT; | |
9226 | if (this->end_ != NULL) | |
9227 | { | |
9228 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
9229 | return TRAVERSE_EXIT; | |
9230 | } | |
9231 | return TRAVERSE_CONTINUE; | |
9232 | } | |
9233 | ||
9234 | // Return the type of a string index. | |
9235 | ||
9236 | Type* | |
9237 | String_index_expression::do_type() | |
9238 | { | |
9239 | if (this->end_ == NULL) | |
9240 | return Type::lookup_integer_type("uint8"); | |
9241 | else | |
9242 | return Type::make_string_type(); | |
9243 | } | |
9244 | ||
9245 | // Determine the type of a string index. | |
9246 | ||
9247 | void | |
9248 | String_index_expression::do_determine_type(const Type_context*) | |
9249 | { | |
9250 | this->string_->determine_type_no_context(); | |
9251 | Type_context subcontext(NULL, true); | |
9252 | this->start_->determine_type(&subcontext); | |
9253 | if (this->end_ != NULL) | |
9254 | this->end_->determine_type(&subcontext); | |
9255 | } | |
9256 | ||
9257 | // Check types of a string index. | |
9258 | ||
9259 | void | |
9260 | String_index_expression::do_check_types(Gogo*) | |
9261 | { | |
9262 | if (this->start_->type()->integer_type() == NULL) | |
9263 | this->report_error(_("index must be integer")); | |
9264 | if (this->end_ != NULL | |
9265 | && this->end_->type()->integer_type() == NULL | |
9266 | && !this->end_->is_nil_expression()) | |
9267 | this->report_error(_("slice end must be integer")); | |
9268 | ||
9269 | std::string sval; | |
9270 | bool sval_valid = this->string_->string_constant_value(&sval); | |
9271 | ||
9272 | mpz_t ival; | |
9273 | mpz_init(ival); | |
9274 | Type* dummy; | |
9275 | if (this->start_->integer_constant_value(true, ival, &dummy)) | |
9276 | { | |
9277 | if (mpz_sgn(ival) < 0 | |
9278 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0)) | |
9279 | { | |
9280 | error_at(this->start_->location(), "string index out of bounds"); | |
9281 | this->set_is_error(); | |
9282 | } | |
9283 | } | |
9284 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
9285 | { | |
9286 | if (this->end_->integer_constant_value(true, ival, &dummy)) | |
9287 | { | |
9288 | if (mpz_sgn(ival) < 0 | |
9289 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0)) | |
9290 | { | |
9291 | error_at(this->end_->location(), "string index out of bounds"); | |
9292 | this->set_is_error(); | |
9293 | } | |
9294 | } | |
9295 | } | |
9296 | mpz_clear(ival); | |
9297 | } | |
9298 | ||
9299 | // Get a tree for a string index. | |
9300 | ||
9301 | tree | |
9302 | String_index_expression::do_get_tree(Translate_context* context) | |
9303 | { | |
9304 | source_location loc = this->location(); | |
9305 | ||
9306 | tree string_tree = this->string_->get_tree(context); | |
9307 | if (string_tree == error_mark_node) | |
9308 | return error_mark_node; | |
9309 | ||
9310 | if (this->string_->type()->points_to() != NULL) | |
9311 | string_tree = build_fold_indirect_ref(string_tree); | |
9312 | if (!DECL_P(string_tree)) | |
9313 | string_tree = save_expr(string_tree); | |
9314 | tree string_type = TREE_TYPE(string_tree); | |
9315 | ||
9316 | tree length_tree = String_type::length_tree(context->gogo(), string_tree); | |
9317 | length_tree = save_expr(length_tree); | |
9318 | tree length_type = TREE_TYPE(length_tree); | |
9319 | ||
9320 | tree bad_index = boolean_false_node; | |
9321 | ||
9322 | tree start_tree = this->start_->get_tree(context); | |
9323 | if (start_tree == error_mark_node) | |
9324 | return error_mark_node; | |
9325 | if (!DECL_P(start_tree)) | |
9326 | start_tree = save_expr(start_tree); | |
9327 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
9328 | start_tree = convert_to_integer(length_type, start_tree); | |
9329 | ||
9330 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
9331 | loc); | |
9332 | ||
9333 | start_tree = fold_convert_loc(loc, length_type, start_tree); | |
9334 | ||
9335 | int code = (this->end_ == NULL | |
9336 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
9337 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
9338 | tree crash = Gogo::runtime_error(code, loc); | |
9339 | ||
9340 | if (this->end_ == NULL) | |
9341 | { | |
9342 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9343 | bad_index, | |
9344 | fold_build2_loc(loc, GE_EXPR, | |
9345 | boolean_type_node, | |
9346 | start_tree, length_tree)); | |
9347 | ||
9348 | tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree); | |
9349 | tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree), | |
9350 | bytes_tree, | |
9351 | fold_convert_loc(loc, sizetype, start_tree)); | |
9352 | tree index = build_fold_indirect_ref_loc(loc, ptr); | |
9353 | ||
9354 | return build2(COMPOUND_EXPR, TREE_TYPE(index), | |
9355 | build3(COND_EXPR, void_type_node, | |
9356 | bad_index, crash, NULL_TREE), | |
9357 | index); | |
9358 | } | |
9359 | else | |
9360 | { | |
9361 | tree end_tree; | |
9362 | if (this->end_->is_nil_expression()) | |
9363 | end_tree = build_int_cst(length_type, -1); | |
9364 | else | |
9365 | { | |
9366 | end_tree = this->end_->get_tree(context); | |
9367 | if (end_tree == error_mark_node) | |
9368 | return error_mark_node; | |
9369 | if (!DECL_P(end_tree)) | |
9370 | end_tree = save_expr(end_tree); | |
9371 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
9372 | end_tree = convert_to_integer(length_type, end_tree); | |
9373 | ||
9374 | bad_index = Expression::check_bounds(end_tree, length_type, | |
9375 | bad_index, loc); | |
9376 | ||
9377 | end_tree = fold_convert_loc(loc, length_type, end_tree); | |
9378 | } | |
9379 | ||
9380 | static tree strslice_fndecl; | |
9381 | tree ret = Gogo::call_builtin(&strslice_fndecl, | |
9382 | loc, | |
9383 | "__go_string_slice", | |
9384 | 3, | |
9385 | string_type, | |
9386 | string_type, | |
9387 | string_tree, | |
9388 | length_type, | |
9389 | start_tree, | |
9390 | length_type, | |
9391 | end_tree); | |
9392 | // This will panic if the bounds are out of range for the | |
9393 | // string. | |
9394 | TREE_NOTHROW(strslice_fndecl) = 0; | |
9395 | ||
9396 | if (bad_index == boolean_false_node) | |
9397 | return ret; | |
9398 | else | |
9399 | return build2(COMPOUND_EXPR, TREE_TYPE(ret), | |
9400 | build3(COND_EXPR, void_type_node, | |
9401 | bad_index, crash, NULL_TREE), | |
9402 | ret); | |
9403 | } | |
9404 | } | |
9405 | ||
9406 | // Make a string index expression. END may be NULL. | |
9407 | ||
9408 | Expression* | |
9409 | Expression::make_string_index(Expression* string, Expression* start, | |
9410 | Expression* end, source_location location) | |
9411 | { | |
9412 | return new String_index_expression(string, start, end, location); | |
9413 | } | |
9414 | ||
9415 | // Class Map_index. | |
9416 | ||
9417 | // Get the type of the map. | |
9418 | ||
9419 | Map_type* | |
9420 | Map_index_expression::get_map_type() const | |
9421 | { | |
9422 | Map_type* mt = this->map_->type()->deref()->map_type(); | |
9423 | gcc_assert(mt != NULL); | |
9424 | return mt; | |
9425 | } | |
9426 | ||
9427 | // Map index traversal. | |
9428 | ||
9429 | int | |
9430 | Map_index_expression::do_traverse(Traverse* traverse) | |
9431 | { | |
9432 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
9433 | return TRAVERSE_EXIT; | |
9434 | return Expression::traverse(&this->index_, traverse); | |
9435 | } | |
9436 | ||
9437 | // Return the type of a map index. | |
9438 | ||
9439 | Type* | |
9440 | Map_index_expression::do_type() | |
9441 | { | |
9442 | Type* type = this->get_map_type()->val_type(); | |
9443 | // If this map index is in a tuple assignment, we actually return a | |
9444 | // pointer to the value type. Tuple_map_assignment_statement is | |
9445 | // responsible for handling this correctly. We need to get the type | |
9446 | // right in case this gets assigned to a temporary variable. | |
9447 | if (this->is_in_tuple_assignment_) | |
9448 | type = Type::make_pointer_type(type); | |
9449 | return type; | |
9450 | } | |
9451 | ||
9452 | // Fix the type of a map index. | |
9453 | ||
9454 | void | |
9455 | Map_index_expression::do_determine_type(const Type_context*) | |
9456 | { | |
9457 | this->map_->determine_type_no_context(); | |
9458 | Type_context subcontext(this->get_map_type()->key_type(), false); | |
9459 | this->index_->determine_type(&subcontext); | |
9460 | } | |
9461 | ||
9462 | // Check types of a map index. | |
9463 | ||
9464 | void | |
9465 | Map_index_expression::do_check_types(Gogo*) | |
9466 | { | |
9467 | std::string reason; | |
9468 | if (!Type::are_assignable(this->get_map_type()->key_type(), | |
9469 | this->index_->type(), &reason)) | |
9470 | { | |
9471 | if (reason.empty()) | |
9472 | this->report_error(_("incompatible type for map index")); | |
9473 | else | |
9474 | { | |
9475 | error_at(this->location(), "incompatible type for map index (%s)", | |
9476 | reason.c_str()); | |
9477 | this->set_is_error(); | |
9478 | } | |
9479 | } | |
9480 | } | |
9481 | ||
9482 | // Get a tree for a map index. | |
9483 | ||
9484 | tree | |
9485 | Map_index_expression::do_get_tree(Translate_context* context) | |
9486 | { | |
9487 | Map_type* type = this->get_map_type(); | |
9488 | ||
9489 | tree valptr = this->get_value_pointer(context, this->is_lvalue_); | |
9490 | if (valptr == error_mark_node) | |
9491 | return error_mark_node; | |
9492 | valptr = save_expr(valptr); | |
9493 | ||
9494 | tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr)); | |
9495 | ||
9496 | if (this->is_lvalue_) | |
9497 | return build_fold_indirect_ref(valptr); | |
9498 | else if (this->is_in_tuple_assignment_) | |
9499 | { | |
9500 | // Tuple_map_assignment_statement is responsible for using this | |
9501 | // appropriately. | |
9502 | return valptr; | |
9503 | } | |
9504 | else | |
9505 | { | |
9506 | return fold_build3(COND_EXPR, val_type_tree, | |
9507 | fold_build2(EQ_EXPR, boolean_type_node, valptr, | |
9508 | fold_convert(TREE_TYPE(valptr), | |
9509 | null_pointer_node)), | |
9510 | type->val_type()->get_init_tree(context->gogo(), | |
9511 | false), | |
9512 | build_fold_indirect_ref(valptr)); | |
9513 | } | |
9514 | } | |
9515 | ||
9516 | // Get a tree for the map index. This returns a tree which evaluates | |
9517 | // to a pointer to a value. The pointer will be NULL if the key is | |
9518 | // not in the map. | |
9519 | ||
9520 | tree | |
9521 | Map_index_expression::get_value_pointer(Translate_context* context, | |
9522 | bool insert) | |
9523 | { | |
9524 | Map_type* type = this->get_map_type(); | |
9525 | ||
9526 | tree map_tree = this->map_->get_tree(context); | |
9527 | tree index_tree = this->index_->get_tree(context); | |
9528 | index_tree = Expression::convert_for_assignment(context, type->key_type(), | |
9529 | this->index_->type(), | |
9530 | index_tree, | |
9531 | this->location()); | |
9532 | if (map_tree == error_mark_node || index_tree == error_mark_node) | |
9533 | return error_mark_node; | |
9534 | ||
9535 | if (this->map_->type()->points_to() != NULL) | |
9536 | map_tree = build_fold_indirect_ref(map_tree); | |
9537 | ||
9538 | // We need to pass in a pointer to the key, so stuff it into a | |
9539 | // variable. | |
9540 | tree tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree)); | |
9541 | DECL_IGNORED_P(tmp) = 0; | |
9542 | DECL_INITIAL(tmp) = index_tree; | |
9543 | tree make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
9544 | tree tmpref = fold_convert(const_ptr_type_node, build_fold_addr_expr(tmp)); | |
9545 | TREE_ADDRESSABLE(tmp) = 1; | |
9546 | ||
9547 | static tree map_index_fndecl; | |
9548 | tree call = Gogo::call_builtin(&map_index_fndecl, | |
9549 | this->location(), | |
9550 | "__go_map_index", | |
9551 | 3, | |
9552 | const_ptr_type_node, | |
9553 | TREE_TYPE(map_tree), | |
9554 | map_tree, | |
9555 | const_ptr_type_node, | |
9556 | tmpref, | |
9557 | boolean_type_node, | |
9558 | (insert | |
9559 | ? boolean_true_node | |
9560 | : boolean_false_node)); | |
9561 | // This can panic on a map of interface type if the interface holds | |
9562 | // an uncomparable or unhashable type. | |
9563 | TREE_NOTHROW(map_index_fndecl) = 0; | |
9564 | ||
9565 | tree val_type_tree = type->val_type()->get_tree(context->gogo()); | |
9566 | if (val_type_tree == error_mark_node) | |
9567 | return error_mark_node; | |
9568 | tree ptr_val_type_tree = build_pointer_type(val_type_tree); | |
9569 | ||
9570 | return build2(COMPOUND_EXPR, ptr_val_type_tree, | |
9571 | make_tmp, | |
9572 | fold_convert(ptr_val_type_tree, call)); | |
9573 | } | |
9574 | ||
9575 | // Make a map index expression. | |
9576 | ||
9577 | Map_index_expression* | |
9578 | Expression::make_map_index(Expression* map, Expression* index, | |
9579 | source_location location) | |
9580 | { | |
9581 | return new Map_index_expression(map, index, location); | |
9582 | } | |
9583 | ||
9584 | // Class Field_reference_expression. | |
9585 | ||
9586 | // Return the type of a field reference. | |
9587 | ||
9588 | Type* | |
9589 | Field_reference_expression::do_type() | |
9590 | { | |
9591 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
9592 | gcc_assert(struct_type != NULL); | |
9593 | return struct_type->field(this->field_index_)->type(); | |
9594 | } | |
9595 | ||
9596 | // Check the types for a field reference. | |
9597 | ||
9598 | void | |
9599 | Field_reference_expression::do_check_types(Gogo*) | |
9600 | { | |
9601 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
9602 | gcc_assert(struct_type != NULL); | |
9603 | gcc_assert(struct_type->field(this->field_index_) != NULL); | |
9604 | } | |
9605 | ||
9606 | // Get a tree for a field reference. | |
9607 | ||
9608 | tree | |
9609 | Field_reference_expression::do_get_tree(Translate_context* context) | |
9610 | { | |
9611 | tree struct_tree = this->expr_->get_tree(context); | |
9612 | if (struct_tree == error_mark_node | |
9613 | || TREE_TYPE(struct_tree) == error_mark_node) | |
9614 | return error_mark_node; | |
9615 | gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE); | |
9616 | tree field = TYPE_FIELDS(TREE_TYPE(struct_tree)); | |
9617 | gcc_assert(field != NULL_TREE); | |
9618 | for (unsigned int i = this->field_index_; i > 0; --i) | |
9619 | { | |
9620 | field = DECL_CHAIN(field); | |
9621 | gcc_assert(field != NULL_TREE); | |
9622 | } | |
9623 | return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field, | |
9624 | NULL_TREE); | |
9625 | } | |
9626 | ||
9627 | // Make a reference to a qualified identifier in an expression. | |
9628 | ||
9629 | Field_reference_expression* | |
9630 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
9631 | source_location location) | |
9632 | { | |
9633 | return new Field_reference_expression(expr, field_index, location); | |
9634 | } | |
9635 | ||
9636 | // Class Interface_field_reference_expression. | |
9637 | ||
9638 | // Return a tree for the pointer to the function to call. | |
9639 | ||
9640 | tree | |
9641 | Interface_field_reference_expression::get_function_tree(Translate_context*, | |
9642 | tree expr) | |
9643 | { | |
9644 | if (this->expr_->type()->points_to() != NULL) | |
9645 | expr = build_fold_indirect_ref(expr); | |
9646 | ||
9647 | tree expr_type = TREE_TYPE(expr); | |
9648 | gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE); | |
9649 | ||
9650 | tree field = TYPE_FIELDS(expr_type); | |
9651 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0); | |
9652 | ||
9653 | tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
9654 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(table))); | |
9655 | ||
9656 | table = build_fold_indirect_ref(table); | |
9657 | gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE); | |
9658 | ||
9659 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
9660 | for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table))); | |
9661 | field != NULL_TREE; | |
9662 | field = DECL_CHAIN(field)) | |
9663 | { | |
9664 | if (name == IDENTIFIER_POINTER(DECL_NAME(field))) | |
9665 | break; | |
9666 | } | |
9667 | gcc_assert(field != NULL_TREE); | |
9668 | ||
9669 | return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE); | |
9670 | } | |
9671 | ||
9672 | // Return a tree for the first argument to pass to the interface | |
9673 | // function. | |
9674 | ||
9675 | tree | |
9676 | Interface_field_reference_expression::get_underlying_object_tree( | |
9677 | Translate_context*, | |
9678 | tree expr) | |
9679 | { | |
9680 | if (this->expr_->type()->points_to() != NULL) | |
9681 | expr = build_fold_indirect_ref(expr); | |
9682 | ||
9683 | tree expr_type = TREE_TYPE(expr); | |
9684 | gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE); | |
9685 | ||
9686 | tree field = DECL_CHAIN(TYPE_FIELDS(expr_type)); | |
9687 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); | |
9688 | ||
9689 | return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
9690 | } | |
9691 | ||
9692 | // Traversal. | |
9693 | ||
9694 | int | |
9695 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
9696 | { | |
9697 | return Expression::traverse(&this->expr_, traverse); | |
9698 | } | |
9699 | ||
9700 | // Return the type of an interface field reference. | |
9701 | ||
9702 | Type* | |
9703 | Interface_field_reference_expression::do_type() | |
9704 | { | |
9705 | Type* expr_type = this->expr_->type(); | |
9706 | ||
9707 | Type* points_to = expr_type->points_to(); | |
9708 | if (points_to != NULL) | |
9709 | expr_type = points_to; | |
9710 | ||
9711 | Interface_type* interface_type = expr_type->interface_type(); | |
9712 | if (interface_type == NULL) | |
9713 | return Type::make_error_type(); | |
9714 | ||
9715 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
9716 | if (method == NULL) | |
9717 | return Type::make_error_type(); | |
9718 | ||
9719 | return method->type(); | |
9720 | } | |
9721 | ||
9722 | // Determine types. | |
9723 | ||
9724 | void | |
9725 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
9726 | { | |
9727 | this->expr_->determine_type_no_context(); | |
9728 | } | |
9729 | ||
9730 | // Check the types for an interface field reference. | |
9731 | ||
9732 | void | |
9733 | Interface_field_reference_expression::do_check_types(Gogo*) | |
9734 | { | |
9735 | Type* type = this->expr_->type(); | |
9736 | ||
9737 | Type* points_to = type->points_to(); | |
9738 | if (points_to != NULL) | |
9739 | type = points_to; | |
9740 | ||
9741 | Interface_type* interface_type = type->interface_type(); | |
9742 | if (interface_type == NULL) | |
9743 | this->report_error(_("expected interface or pointer to interface")); | |
9744 | else | |
9745 | { | |
9746 | const Typed_identifier* method = | |
9747 | interface_type->find_method(this->name_); | |
9748 | if (method == NULL) | |
9749 | { | |
9750 | error_at(this->location(), "method %qs not in interface", | |
9751 | Gogo::message_name(this->name_).c_str()); | |
9752 | this->set_is_error(); | |
9753 | } | |
9754 | } | |
9755 | } | |
9756 | ||
9757 | // Get a tree for a reference to a field in an interface. There is no | |
9758 | // standard tree type representation for this: it's a function | |
9759 | // attached to its first argument, like a Bound_method_expression. | |
9760 | // The only places it may currently be used are in a Call_expression | |
9761 | // or a Go_statement, which will take it apart directly. So this has | |
9762 | // nothing to do at present. | |
9763 | ||
9764 | tree | |
9765 | Interface_field_reference_expression::do_get_tree(Translate_context*) | |
9766 | { | |
9767 | gcc_unreachable(); | |
9768 | } | |
9769 | ||
9770 | // Make a reference to a field in an interface. | |
9771 | ||
9772 | Expression* | |
9773 | Expression::make_interface_field_reference(Expression* expr, | |
9774 | const std::string& field, | |
9775 | source_location location) | |
9776 | { | |
9777 | return new Interface_field_reference_expression(expr, field, location); | |
9778 | } | |
9779 | ||
9780 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
9781 | // is lowered after we know the type of the left hand side. | |
9782 | ||
9783 | class Selector_expression : public Parser_expression | |
9784 | { | |
9785 | public: | |
9786 | Selector_expression(Expression* left, const std::string& name, | |
9787 | source_location location) | |
9788 | : Parser_expression(EXPRESSION_SELECTOR, location), | |
9789 | left_(left), name_(name) | |
9790 | { } | |
9791 | ||
9792 | protected: | |
9793 | int | |
9794 | do_traverse(Traverse* traverse) | |
9795 | { return Expression::traverse(&this->left_, traverse); } | |
9796 | ||
9797 | Expression* | |
9798 | do_lower(Gogo*, Named_object*, int); | |
9799 | ||
9800 | Expression* | |
9801 | do_copy() | |
9802 | { | |
9803 | return new Selector_expression(this->left_->copy(), this->name_, | |
9804 | this->location()); | |
9805 | } | |
9806 | ||
9807 | private: | |
9808 | Expression* | |
9809 | lower_method_expression(Gogo*); | |
9810 | ||
9811 | // The expression on the left hand side. | |
9812 | Expression* left_; | |
9813 | // The name on the right hand side. | |
9814 | std::string name_; | |
9815 | }; | |
9816 | ||
9817 | // Lower a selector expression once we know the real type of the left | |
9818 | // hand side. | |
9819 | ||
9820 | Expression* | |
9821 | Selector_expression::do_lower(Gogo* gogo, Named_object*, int) | |
9822 | { | |
9823 | Expression* left = this->left_; | |
9824 | if (left->is_type_expression()) | |
9825 | return this->lower_method_expression(gogo); | |
9826 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
9827 | this->location()); | |
9828 | } | |
9829 | ||
9830 | // Lower a method expression T.M or (*T).M. We turn this into a | |
9831 | // function literal. | |
9832 | ||
9833 | Expression* | |
9834 | Selector_expression::lower_method_expression(Gogo* gogo) | |
9835 | { | |
9836 | source_location location = this->location(); | |
9837 | Type* type = this->left_->type(); | |
9838 | const std::string& name(this->name_); | |
9839 | ||
9840 | bool is_pointer; | |
9841 | if (type->points_to() == NULL) | |
9842 | is_pointer = false; | |
9843 | else | |
9844 | { | |
9845 | is_pointer = true; | |
9846 | type = type->points_to(); | |
9847 | } | |
9848 | Named_type* nt = type->named_type(); | |
9849 | if (nt == NULL) | |
9850 | { | |
9851 | error_at(location, | |
9852 | ("method expression requires named type or " | |
9853 | "pointer to named type")); | |
9854 | return Expression::make_error(location); | |
9855 | } | |
9856 | ||
9857 | bool is_ambiguous; | |
9858 | Method* method = nt->method_function(name, &is_ambiguous); | |
9859 | if (method == NULL) | |
9860 | { | |
9861 | if (!is_ambiguous) | |
9862 | error_at(location, "type %<%s%> has no method %<%s%>", | |
9863 | nt->message_name().c_str(), | |
9864 | Gogo::message_name(name).c_str()); | |
9865 | else | |
9866 | error_at(location, "method %<%s%> is ambiguous in type %<%s%>", | |
9867 | Gogo::message_name(name).c_str(), | |
9868 | nt->message_name().c_str()); | |
9869 | return Expression::make_error(location); | |
9870 | } | |
9871 | ||
9872 | if (!is_pointer && !method->is_value_method()) | |
9873 | { | |
9874 | error_at(location, "method requires pointer (use %<(*%s).%s)%>", | |
9875 | nt->message_name().c_str(), | |
9876 | Gogo::message_name(name).c_str()); | |
9877 | return Expression::make_error(location); | |
9878 | } | |
9879 | ||
9880 | // Build a new function type in which the receiver becomes the first | |
9881 | // argument. | |
9882 | Function_type* method_type = method->type(); | |
9883 | gcc_assert(method_type->is_method()); | |
9884 | ||
9885 | const char* const receiver_name = "$this"; | |
9886 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
9887 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
9888 | location)); | |
9889 | ||
9890 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
9891 | if (method_parameters != NULL) | |
9892 | { | |
9893 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); | |
9894 | p != method_parameters->end(); | |
9895 | ++p) | |
9896 | parameters->push_back(*p); | |
9897 | } | |
9898 | ||
9899 | const Typed_identifier_list* method_results = method_type->results(); | |
9900 | Typed_identifier_list* results; | |
9901 | if (method_results == NULL) | |
9902 | results = NULL; | |
9903 | else | |
9904 | { | |
9905 | results = new Typed_identifier_list(); | |
9906 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
9907 | p != method_results->end(); | |
9908 | ++p) | |
9909 | results->push_back(*p); | |
9910 | } | |
9911 | ||
9912 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
9913 | location); | |
9914 | if (method_type->is_varargs()) | |
9915 | fntype->set_is_varargs(); | |
9916 | ||
9917 | // We generate methods which always takes a pointer to the receiver | |
9918 | // as their first argument. If this is for a pointer type, we can | |
9919 | // simply reuse the existing function. We use an internal hack to | |
9920 | // get the right type. | |
9921 | ||
9922 | if (is_pointer) | |
9923 | { | |
9924 | Named_object* mno = (method->needs_stub_method() | |
9925 | ? method->stub_object() | |
9926 | : method->named_object()); | |
9927 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
9928 | f = Expression::make_cast(fntype, f, location); | |
9929 | Type_conversion_expression* tce = | |
9930 | static_cast<Type_conversion_expression*>(f); | |
9931 | tce->set_may_convert_function_types(); | |
9932 | return f; | |
9933 | } | |
9934 | ||
9935 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
9936 | location); | |
9937 | ||
9938 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
9939 | gcc_assert(vno != NULL); | |
9940 | Expression* ve = Expression::make_var_reference(vno, location); | |
9941 | Expression* bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
9942 | gcc_assert(bm != NULL && !bm->is_error_expression()); | |
9943 | ||
9944 | Expression_list* args; | |
9945 | if (method_parameters == NULL) | |
9946 | args = NULL; | |
9947 | else | |
9948 | { | |
9949 | args = new Expression_list(); | |
9950 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); | |
9951 | p != method_parameters->end(); | |
9952 | ++p) | |
9953 | { | |
9954 | vno = gogo->lookup(p->name(), NULL); | |
9955 | gcc_assert(vno != NULL); | |
9956 | args->push_back(Expression::make_var_reference(vno, location)); | |
9957 | } | |
9958 | } | |
9959 | ||
9960 | Call_expression* call = Expression::make_call(bm, args, | |
9961 | method_type->is_varargs(), | |
9962 | location); | |
9963 | ||
9964 | size_t count = call->result_count(); | |
9965 | Statement* s; | |
9966 | if (count == 0) | |
9967 | s = Statement::make_statement(call); | |
9968 | else | |
9969 | { | |
9970 | Expression_list* retvals = new Expression_list(); | |
9971 | if (count <= 1) | |
9972 | retvals->push_back(call); | |
9973 | else | |
9974 | { | |
9975 | for (size_t i = 0; i < count; ++i) | |
9976 | retvals->push_back(Expression::make_call_result(call, i)); | |
9977 | } | |
9978 | s = Statement::make_return_statement(no->func_value()->type()->results(), | |
9979 | retvals, location); | |
9980 | } | |
9981 | gogo->add_statement(s); | |
9982 | ||
9983 | gogo->finish_function(location); | |
9984 | ||
9985 | return Expression::make_func_reference(no, NULL, location); | |
9986 | } | |
9987 | ||
9988 | // Make a selector expression. | |
9989 | ||
9990 | Expression* | |
9991 | Expression::make_selector(Expression* left, const std::string& name, | |
9992 | source_location location) | |
9993 | { | |
9994 | return new Selector_expression(left, name, location); | |
9995 | } | |
9996 | ||
9997 | // Implement the builtin function new. | |
9998 | ||
9999 | class Allocation_expression : public Expression | |
10000 | { | |
10001 | public: | |
10002 | Allocation_expression(Type* type, source_location location) | |
10003 | : Expression(EXPRESSION_ALLOCATION, location), | |
10004 | type_(type) | |
10005 | { } | |
10006 | ||
10007 | protected: | |
10008 | int | |
10009 | do_traverse(Traverse* traverse) | |
10010 | { return Type::traverse(this->type_, traverse); } | |
10011 | ||
10012 | Type* | |
10013 | do_type() | |
10014 | { return Type::make_pointer_type(this->type_); } | |
10015 | ||
10016 | void | |
10017 | do_determine_type(const Type_context*) | |
10018 | { } | |
10019 | ||
10020 | void | |
10021 | do_check_types(Gogo*); | |
10022 | ||
10023 | Expression* | |
10024 | do_copy() | |
10025 | { return new Allocation_expression(this->type_, this->location()); } | |
10026 | ||
10027 | tree | |
10028 | do_get_tree(Translate_context*); | |
10029 | ||
10030 | private: | |
10031 | // The type we are allocating. | |
10032 | Type* type_; | |
10033 | }; | |
10034 | ||
10035 | // Check the type of an allocation expression. | |
10036 | ||
10037 | void | |
10038 | Allocation_expression::do_check_types(Gogo*) | |
10039 | { | |
10040 | if (this->type_->function_type() != NULL) | |
10041 | this->report_error(_("invalid new of function type")); | |
10042 | } | |
10043 | ||
10044 | // Return a tree for an allocation expression. | |
10045 | ||
10046 | tree | |
10047 | Allocation_expression::do_get_tree(Translate_context* context) | |
10048 | { | |
10049 | tree type_tree = this->type_->get_tree(context->gogo()); | |
10050 | tree size_tree = TYPE_SIZE_UNIT(type_tree); | |
10051 | tree space = context->gogo()->allocate_memory(this->type_, size_tree, | |
10052 | this->location()); | |
10053 | return fold_convert(build_pointer_type(type_tree), space); | |
10054 | } | |
10055 | ||
10056 | // Make an allocation expression. | |
10057 | ||
10058 | Expression* | |
10059 | Expression::make_allocation(Type* type, source_location location) | |
10060 | { | |
10061 | return new Allocation_expression(type, location); | |
10062 | } | |
10063 | ||
10064 | // Implement the builtin function make. | |
10065 | ||
10066 | class Make_expression : public Expression | |
10067 | { | |
10068 | public: | |
10069 | Make_expression(Type* type, Expression_list* args, source_location location) | |
10070 | : Expression(EXPRESSION_MAKE, location), | |
10071 | type_(type), args_(args) | |
10072 | { } | |
10073 | ||
10074 | protected: | |
10075 | int | |
10076 | do_traverse(Traverse* traverse); | |
10077 | ||
10078 | Type* | |
10079 | do_type() | |
10080 | { return this->type_; } | |
10081 | ||
10082 | void | |
10083 | do_determine_type(const Type_context*); | |
10084 | ||
10085 | void | |
10086 | do_check_types(Gogo*); | |
10087 | ||
10088 | Expression* | |
10089 | do_copy() | |
10090 | { | |
10091 | return new Make_expression(this->type_, this->args_->copy(), | |
10092 | this->location()); | |
10093 | } | |
10094 | ||
10095 | tree | |
10096 | do_get_tree(Translate_context*); | |
10097 | ||
10098 | private: | |
10099 | // The type we are making. | |
10100 | Type* type_; | |
10101 | // The arguments to pass to the make routine. | |
10102 | Expression_list* args_; | |
10103 | }; | |
10104 | ||
10105 | // Traversal. | |
10106 | ||
10107 | int | |
10108 | Make_expression::do_traverse(Traverse* traverse) | |
10109 | { | |
10110 | if (this->args_ != NULL | |
10111 | && this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
10112 | return TRAVERSE_EXIT; | |
10113 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10114 | return TRAVERSE_EXIT; | |
10115 | return TRAVERSE_CONTINUE; | |
10116 | } | |
10117 | ||
10118 | // Set types of arguments. | |
10119 | ||
10120 | void | |
10121 | Make_expression::do_determine_type(const Type_context*) | |
10122 | { | |
10123 | if (this->args_ != NULL) | |
10124 | { | |
10125 | Type_context context(Type::lookup_integer_type("int"), false); | |
10126 | for (Expression_list::const_iterator pe = this->args_->begin(); | |
10127 | pe != this->args_->end(); | |
10128 | ++pe) | |
10129 | (*pe)->determine_type(&context); | |
10130 | } | |
10131 | } | |
10132 | ||
10133 | // Check types for a make expression. | |
10134 | ||
10135 | void | |
10136 | Make_expression::do_check_types(Gogo*) | |
10137 | { | |
10138 | if (this->type_->channel_type() == NULL | |
10139 | && this->type_->map_type() == NULL | |
10140 | && (this->type_->array_type() == NULL | |
10141 | || this->type_->array_type()->length() != NULL)) | |
10142 | this->report_error(_("invalid type for make function")); | |
10143 | else if (!this->type_->check_make_expression(this->args_, this->location())) | |
10144 | this->set_is_error(); | |
10145 | } | |
10146 | ||
10147 | // Return a tree for a make expression. | |
10148 | ||
10149 | tree | |
10150 | Make_expression::do_get_tree(Translate_context* context) | |
10151 | { | |
10152 | return this->type_->make_expression_tree(context, this->args_, | |
10153 | this->location()); | |
10154 | } | |
10155 | ||
10156 | // Make a make expression. | |
10157 | ||
10158 | Expression* | |
10159 | Expression::make_make(Type* type, Expression_list* args, | |
10160 | source_location location) | |
10161 | { | |
10162 | return new Make_expression(type, args, location); | |
10163 | } | |
10164 | ||
10165 | // Construct a struct. | |
10166 | ||
10167 | class Struct_construction_expression : public Expression | |
10168 | { | |
10169 | public: | |
10170 | Struct_construction_expression(Type* type, Expression_list* vals, | |
10171 | source_location location) | |
10172 | : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location), | |
10173 | type_(type), vals_(vals) | |
10174 | { } | |
10175 | ||
10176 | // Return whether this is a constant initializer. | |
10177 | bool | |
10178 | is_constant_struct() const; | |
10179 | ||
10180 | protected: | |
10181 | int | |
10182 | do_traverse(Traverse* traverse); | |
10183 | ||
10184 | Type* | |
10185 | do_type() | |
10186 | { return this->type_; } | |
10187 | ||
10188 | void | |
10189 | do_determine_type(const Type_context*); | |
10190 | ||
10191 | void | |
10192 | do_check_types(Gogo*); | |
10193 | ||
10194 | Expression* | |
10195 | do_copy() | |
10196 | { | |
10197 | return new Struct_construction_expression(this->type_, this->vals_->copy(), | |
10198 | this->location()); | |
10199 | } | |
10200 | ||
10201 | bool | |
10202 | do_is_addressable() const | |
10203 | { return true; } | |
10204 | ||
10205 | tree | |
10206 | do_get_tree(Translate_context*); | |
10207 | ||
10208 | void | |
10209 | do_export(Export*) const; | |
10210 | ||
10211 | private: | |
10212 | // The type of the struct to construct. | |
10213 | Type* type_; | |
10214 | // The list of values, in order of the fields in the struct. A NULL | |
10215 | // entry means that the field should be zero-initialized. | |
10216 | Expression_list* vals_; | |
10217 | }; | |
10218 | ||
10219 | // Traversal. | |
10220 | ||
10221 | int | |
10222 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
10223 | { | |
10224 | if (this->vals_ != NULL | |
10225 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
10226 | return TRAVERSE_EXIT; | |
10227 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10228 | return TRAVERSE_EXIT; | |
10229 | return TRAVERSE_CONTINUE; | |
10230 | } | |
10231 | ||
10232 | // Return whether this is a constant initializer. | |
10233 | ||
10234 | bool | |
10235 | Struct_construction_expression::is_constant_struct() const | |
10236 | { | |
10237 | if (this->vals_ == NULL) | |
10238 | return true; | |
10239 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10240 | pv != this->vals_->end(); | |
10241 | ++pv) | |
10242 | { | |
10243 | if (*pv != NULL | |
10244 | && !(*pv)->is_constant() | |
10245 | && (!(*pv)->is_composite_literal() | |
10246 | || (*pv)->is_nonconstant_composite_literal())) | |
10247 | return false; | |
10248 | } | |
10249 | ||
10250 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10251 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10252 | pf != fields->end(); | |
10253 | ++pf) | |
10254 | { | |
10255 | // There are no constant constructors for interfaces. | |
10256 | if (pf->type()->interface_type() != NULL) | |
10257 | return false; | |
10258 | } | |
10259 | ||
10260 | return true; | |
10261 | } | |
10262 | ||
10263 | // Final type determination. | |
10264 | ||
10265 | void | |
10266 | Struct_construction_expression::do_determine_type(const Type_context*) | |
10267 | { | |
10268 | if (this->vals_ == NULL) | |
10269 | return; | |
10270 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10271 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10272 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10273 | pf != fields->end(); | |
10274 | ++pf, ++pv) | |
10275 | { | |
10276 | if (pv == this->vals_->end()) | |
10277 | return; | |
10278 | if (*pv != NULL) | |
10279 | { | |
10280 | Type_context subcontext(pf->type(), false); | |
10281 | (*pv)->determine_type(&subcontext); | |
10282 | } | |
10283 | } | |
10284 | } | |
10285 | ||
10286 | // Check types. | |
10287 | ||
10288 | void | |
10289 | Struct_construction_expression::do_check_types(Gogo*) | |
10290 | { | |
10291 | if (this->vals_ == NULL) | |
10292 | return; | |
10293 | ||
10294 | Struct_type* st = this->type_->struct_type(); | |
10295 | if (this->vals_->size() > st->field_count()) | |
10296 | { | |
10297 | this->report_error(_("too many expressions for struct")); | |
10298 | return; | |
10299 | } | |
10300 | ||
10301 | const Struct_field_list* fields = st->fields(); | |
10302 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10303 | int i = 0; | |
10304 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10305 | pf != fields->end(); | |
10306 | ++pf, ++pv, ++i) | |
10307 | { | |
10308 | if (pv == this->vals_->end()) | |
10309 | { | |
10310 | this->report_error(_("too few expressions for struct")); | |
10311 | break; | |
10312 | } | |
10313 | ||
10314 | if (*pv == NULL) | |
10315 | continue; | |
10316 | ||
10317 | std::string reason; | |
10318 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
10319 | { | |
10320 | if (reason.empty()) | |
10321 | error_at((*pv)->location(), | |
10322 | "incompatible type for field %d in struct construction", | |
10323 | i + 1); | |
10324 | else | |
10325 | error_at((*pv)->location(), | |
10326 | ("incompatible type for field %d in " | |
10327 | "struct construction (%s)"), | |
10328 | i + 1, reason.c_str()); | |
10329 | this->set_is_error(); | |
10330 | } | |
10331 | } | |
10332 | gcc_assert(pv == this->vals_->end()); | |
10333 | } | |
10334 | ||
10335 | // Return a tree for constructing a struct. | |
10336 | ||
10337 | tree | |
10338 | Struct_construction_expression::do_get_tree(Translate_context* context) | |
10339 | { | |
10340 | Gogo* gogo = context->gogo(); | |
10341 | ||
10342 | if (this->vals_ == NULL) | |
10343 | return this->type_->get_init_tree(gogo, false); | |
10344 | ||
10345 | tree type_tree = this->type_->get_tree(gogo); | |
10346 | if (type_tree == error_mark_node) | |
10347 | return error_mark_node; | |
10348 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); | |
10349 | ||
10350 | bool is_constant = true; | |
10351 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10352 | VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc, | |
10353 | fields->size()); | |
10354 | Struct_field_list::const_iterator pf = fields->begin(); | |
10355 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10356 | for (tree field = TYPE_FIELDS(type_tree); | |
10357 | field != NULL_TREE; | |
10358 | field = DECL_CHAIN(field), ++pf) | |
10359 | { | |
10360 | gcc_assert(pf != fields->end()); | |
10361 | ||
10362 | tree val; | |
10363 | if (pv == this->vals_->end()) | |
10364 | val = pf->type()->get_init_tree(gogo, false); | |
10365 | else if (*pv == NULL) | |
10366 | { | |
10367 | val = pf->type()->get_init_tree(gogo, false); | |
10368 | ++pv; | |
10369 | } | |
10370 | else | |
10371 | { | |
10372 | val = Expression::convert_for_assignment(context, pf->type(), | |
10373 | (*pv)->type(), | |
10374 | (*pv)->get_tree(context), | |
10375 | this->location()); | |
10376 | ++pv; | |
10377 | } | |
10378 | ||
10379 | if (val == error_mark_node || TREE_TYPE(val) == error_mark_node) | |
10380 | return error_mark_node; | |
10381 | ||
10382 | constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL); | |
10383 | elt->index = field; | |
10384 | elt->value = val; | |
10385 | if (!TREE_CONSTANT(val)) | |
10386 | is_constant = false; | |
10387 | } | |
10388 | gcc_assert(pf == fields->end()); | |
10389 | ||
10390 | tree ret = build_constructor(type_tree, elts); | |
10391 | if (is_constant) | |
10392 | TREE_CONSTANT(ret) = 1; | |
10393 | return ret; | |
10394 | } | |
10395 | ||
10396 | // Export a struct construction. | |
10397 | ||
10398 | void | |
10399 | Struct_construction_expression::do_export(Export* exp) const | |
10400 | { | |
10401 | exp->write_c_string("convert("); | |
10402 | exp->write_type(this->type_); | |
10403 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10404 | pv != this->vals_->end(); | |
10405 | ++pv) | |
10406 | { | |
10407 | exp->write_c_string(", "); | |
10408 | if (*pv != NULL) | |
10409 | (*pv)->export_expression(exp); | |
10410 | } | |
10411 | exp->write_c_string(")"); | |
10412 | } | |
10413 | ||
10414 | // Make a struct composite literal. This used by the thunk code. | |
10415 | ||
10416 | Expression* | |
10417 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
10418 | source_location location) | |
10419 | { | |
10420 | gcc_assert(type->struct_type() != NULL); | |
10421 | return new Struct_construction_expression(type, vals, location); | |
10422 | } | |
10423 | ||
10424 | // Construct an array. This class is not used directly; instead we | |
10425 | // use the child classes, Fixed_array_construction_expression and | |
10426 | // Open_array_construction_expression. | |
10427 | ||
10428 | class Array_construction_expression : public Expression | |
10429 | { | |
10430 | protected: | |
10431 | Array_construction_expression(Expression_classification classification, | |
10432 | Type* type, Expression_list* vals, | |
10433 | source_location location) | |
10434 | : Expression(classification, location), | |
10435 | type_(type), vals_(vals) | |
10436 | { } | |
10437 | ||
10438 | public: | |
10439 | // Return whether this is a constant initializer. | |
10440 | bool | |
10441 | is_constant_array() const; | |
10442 | ||
10443 | // Return the number of elements. | |
10444 | size_t | |
10445 | element_count() const | |
10446 | { return this->vals_ == NULL ? 0 : this->vals_->size(); } | |
10447 | ||
10448 | protected: | |
10449 | int | |
10450 | do_traverse(Traverse* traverse); | |
10451 | ||
10452 | Type* | |
10453 | do_type() | |
10454 | { return this->type_; } | |
10455 | ||
10456 | void | |
10457 | do_determine_type(const Type_context*); | |
10458 | ||
10459 | void | |
10460 | do_check_types(Gogo*); | |
10461 | ||
10462 | bool | |
10463 | do_is_addressable() const | |
10464 | { return true; } | |
10465 | ||
10466 | void | |
10467 | do_export(Export*) const; | |
10468 | ||
10469 | // The list of values. | |
10470 | Expression_list* | |
10471 | vals() | |
10472 | { return this->vals_; } | |
10473 | ||
10474 | // Get a constructor tree for the array values. | |
10475 | tree | |
10476 | get_constructor_tree(Translate_context* context, tree type_tree); | |
10477 | ||
10478 | private: | |
10479 | // The type of the array to construct. | |
10480 | Type* type_; | |
10481 | // The list of values. | |
10482 | Expression_list* vals_; | |
10483 | }; | |
10484 | ||
10485 | // Traversal. | |
10486 | ||
10487 | int | |
10488 | Array_construction_expression::do_traverse(Traverse* traverse) | |
10489 | { | |
10490 | if (this->vals_ != NULL | |
10491 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
10492 | return TRAVERSE_EXIT; | |
10493 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10494 | return TRAVERSE_EXIT; | |
10495 | return TRAVERSE_CONTINUE; | |
10496 | } | |
10497 | ||
10498 | // Return whether this is a constant initializer. | |
10499 | ||
10500 | bool | |
10501 | Array_construction_expression::is_constant_array() const | |
10502 | { | |
10503 | if (this->vals_ == NULL) | |
10504 | return true; | |
10505 | ||
10506 | // There are no constant constructors for interfaces. | |
10507 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
10508 | return false; | |
10509 | ||
10510 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10511 | pv != this->vals_->end(); | |
10512 | ++pv) | |
10513 | { | |
10514 | if (*pv != NULL | |
10515 | && !(*pv)->is_constant() | |
10516 | && (!(*pv)->is_composite_literal() | |
10517 | || (*pv)->is_nonconstant_composite_literal())) | |
10518 | return false; | |
10519 | } | |
10520 | return true; | |
10521 | } | |
10522 | ||
10523 | // Final type determination. | |
10524 | ||
10525 | void | |
10526 | Array_construction_expression::do_determine_type(const Type_context*) | |
10527 | { | |
10528 | if (this->vals_ == NULL) | |
10529 | return; | |
10530 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
10531 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10532 | pv != this->vals_->end(); | |
10533 | ++pv) | |
10534 | { | |
10535 | if (*pv != NULL) | |
10536 | (*pv)->determine_type(&subcontext); | |
10537 | } | |
10538 | } | |
10539 | ||
10540 | // Check types. | |
10541 | ||
10542 | void | |
10543 | Array_construction_expression::do_check_types(Gogo*) | |
10544 | { | |
10545 | if (this->vals_ == NULL) | |
10546 | return; | |
10547 | ||
10548 | Array_type* at = this->type_->array_type(); | |
10549 | int i = 0; | |
10550 | Type* element_type = at->element_type(); | |
10551 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10552 | pv != this->vals_->end(); | |
10553 | ++pv, ++i) | |
10554 | { | |
10555 | if (*pv != NULL | |
10556 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
10557 | { | |
10558 | error_at((*pv)->location(), | |
10559 | "incompatible type for element %d in composite literal", | |
10560 | i + 1); | |
10561 | this->set_is_error(); | |
10562 | } | |
10563 | } | |
10564 | ||
10565 | Expression* length = at->length(); | |
10566 | if (length != NULL) | |
10567 | { | |
10568 | mpz_t val; | |
10569 | mpz_init(val); | |
10570 | Type* type; | |
10571 | if (at->length()->integer_constant_value(true, val, &type)) | |
10572 | { | |
10573 | if (this->vals_->size() > mpz_get_ui(val)) | |
10574 | this->report_error(_("too many elements in composite literal")); | |
10575 | } | |
10576 | mpz_clear(val); | |
10577 | } | |
10578 | } | |
10579 | ||
10580 | // Get a constructor tree for the array values. | |
10581 | ||
10582 | tree | |
10583 | Array_construction_expression::get_constructor_tree(Translate_context* context, | |
10584 | tree type_tree) | |
10585 | { | |
10586 | VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc, | |
10587 | (this->vals_ == NULL | |
10588 | ? 0 | |
10589 | : this->vals_->size())); | |
10590 | Type* element_type = this->type_->array_type()->element_type(); | |
10591 | bool is_constant = true; | |
10592 | if (this->vals_ != NULL) | |
10593 | { | |
10594 | size_t i = 0; | |
10595 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10596 | pv != this->vals_->end(); | |
10597 | ++pv, ++i) | |
10598 | { | |
10599 | constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL); | |
10600 | elt->index = size_int(i); | |
10601 | if (*pv == NULL) | |
10602 | elt->value = element_type->get_init_tree(context->gogo(), false); | |
10603 | else | |
10604 | { | |
10605 | tree value_tree = (*pv)->get_tree(context); | |
10606 | elt->value = Expression::convert_for_assignment(context, | |
10607 | element_type, | |
10608 | (*pv)->type(), | |
10609 | value_tree, | |
10610 | this->location()); | |
10611 | } | |
10612 | if (elt->value == error_mark_node) | |
10613 | return error_mark_node; | |
10614 | if (!TREE_CONSTANT(elt->value)) | |
10615 | is_constant = false; | |
10616 | } | |
10617 | } | |
10618 | ||
10619 | tree ret = build_constructor(type_tree, values); | |
10620 | if (is_constant) | |
10621 | TREE_CONSTANT(ret) = 1; | |
10622 | return ret; | |
10623 | } | |
10624 | ||
10625 | // Export an array construction. | |
10626 | ||
10627 | void | |
10628 | Array_construction_expression::do_export(Export* exp) const | |
10629 | { | |
10630 | exp->write_c_string("convert("); | |
10631 | exp->write_type(this->type_); | |
10632 | if (this->vals_ != NULL) | |
10633 | { | |
10634 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10635 | pv != this->vals_->end(); | |
10636 | ++pv) | |
10637 | { | |
10638 | exp->write_c_string(", "); | |
10639 | if (*pv != NULL) | |
10640 | (*pv)->export_expression(exp); | |
10641 | } | |
10642 | } | |
10643 | exp->write_c_string(")"); | |
10644 | } | |
10645 | ||
10646 | // Construct a fixed array. | |
10647 | ||
10648 | class Fixed_array_construction_expression : | |
10649 | public Array_construction_expression | |
10650 | { | |
10651 | public: | |
10652 | Fixed_array_construction_expression(Type* type, Expression_list* vals, | |
10653 | source_location location) | |
10654 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, | |
10655 | type, vals, location) | |
10656 | { | |
10657 | gcc_assert(type->array_type() != NULL | |
10658 | && type->array_type()->length() != NULL); | |
10659 | } | |
10660 | ||
10661 | protected: | |
10662 | Expression* | |
10663 | do_copy() | |
10664 | { | |
10665 | return new Fixed_array_construction_expression(this->type(), | |
10666 | (this->vals() == NULL | |
10667 | ? NULL | |
10668 | : this->vals()->copy()), | |
10669 | this->location()); | |
10670 | } | |
10671 | ||
10672 | tree | |
10673 | do_get_tree(Translate_context*); | |
10674 | }; | |
10675 | ||
10676 | // Return a tree for constructing a fixed array. | |
10677 | ||
10678 | tree | |
10679 | Fixed_array_construction_expression::do_get_tree(Translate_context* context) | |
10680 | { | |
10681 | return this->get_constructor_tree(context, | |
10682 | this->type()->get_tree(context->gogo())); | |
10683 | } | |
10684 | ||
10685 | // Construct an open array. | |
10686 | ||
10687 | class Open_array_construction_expression : public Array_construction_expression | |
10688 | { | |
10689 | public: | |
10690 | Open_array_construction_expression(Type* type, Expression_list* vals, | |
10691 | source_location location) | |
10692 | : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION, | |
10693 | type, vals, location) | |
10694 | { | |
10695 | gcc_assert(type->array_type() != NULL | |
10696 | && type->array_type()->length() == NULL); | |
10697 | } | |
10698 | ||
10699 | protected: | |
10700 | // Note that taking the address of an open array literal is invalid. | |
10701 | ||
10702 | Expression* | |
10703 | do_copy() | |
10704 | { | |
10705 | return new Open_array_construction_expression(this->type(), | |
10706 | (this->vals() == NULL | |
10707 | ? NULL | |
10708 | : this->vals()->copy()), | |
10709 | this->location()); | |
10710 | } | |
10711 | ||
10712 | tree | |
10713 | do_get_tree(Translate_context*); | |
10714 | }; | |
10715 | ||
10716 | // Return a tree for constructing an open array. | |
10717 | ||
10718 | tree | |
10719 | Open_array_construction_expression::do_get_tree(Translate_context* context) | |
10720 | { | |
10721 | Type* element_type = this->type()->array_type()->element_type(); | |
10722 | tree element_type_tree = element_type->get_tree(context->gogo()); | |
10723 | tree values; | |
10724 | tree length_tree; | |
10725 | if (this->vals() == NULL || this->vals()->empty()) | |
10726 | { | |
10727 | // We need to create a unique value. | |
10728 | tree max = size_int(0); | |
10729 | tree constructor_type = build_array_type(element_type_tree, | |
10730 | build_index_type(max)); | |
10731 | if (constructor_type == error_mark_node) | |
10732 | return error_mark_node; | |
10733 | VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1); | |
10734 | constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL); | |
10735 | elt->index = size_int(0); | |
10736 | elt->value = element_type->get_init_tree(context->gogo(), false); | |
10737 | values = build_constructor(constructor_type, vec); | |
10738 | if (TREE_CONSTANT(elt->value)) | |
10739 | TREE_CONSTANT(values) = 1; | |
10740 | length_tree = size_int(0); | |
10741 | } | |
10742 | else | |
10743 | { | |
10744 | tree max = size_int(this->vals()->size() - 1); | |
10745 | tree constructor_type = build_array_type(element_type_tree, | |
10746 | build_index_type(max)); | |
10747 | if (constructor_type == error_mark_node) | |
10748 | return error_mark_node; | |
10749 | values = this->get_constructor_tree(context, constructor_type); | |
10750 | length_tree = size_int(this->vals()->size()); | |
10751 | } | |
10752 | ||
10753 | if (values == error_mark_node) | |
10754 | return error_mark_node; | |
10755 | ||
10756 | bool is_constant_initializer = TREE_CONSTANT(values); | |
10757 | bool is_in_function = context->function() != NULL; | |
10758 | ||
10759 | if (is_constant_initializer) | |
10760 | { | |
10761 | tree tmp = build_decl(this->location(), VAR_DECL, | |
10762 | create_tmp_var_name("C"), TREE_TYPE(values)); | |
10763 | DECL_EXTERNAL(tmp) = 0; | |
10764 | TREE_PUBLIC(tmp) = 0; | |
10765 | TREE_STATIC(tmp) = 1; | |
10766 | DECL_ARTIFICIAL(tmp) = 1; | |
10767 | if (is_in_function) | |
10768 | { | |
10769 | // If this is not a function, we will only initialize the | |
10770 | // value once, so we can use this directly rather than | |
10771 | // copying it. In that case we can't make it read-only, | |
10772 | // because the program is permitted to change it. | |
10773 | TREE_READONLY(tmp) = 1; | |
10774 | TREE_CONSTANT(tmp) = 1; | |
10775 | } | |
10776 | DECL_INITIAL(tmp) = values; | |
10777 | rest_of_decl_compilation(tmp, 1, 0); | |
10778 | values = tmp; | |
10779 | } | |
10780 | ||
10781 | tree space; | |
10782 | tree set; | |
10783 | if (!is_in_function && is_constant_initializer) | |
10784 | { | |
10785 | // Outside of a function, we know the initializer will only run | |
10786 | // once. | |
10787 | space = build_fold_addr_expr(values); | |
10788 | set = NULL_TREE; | |
10789 | } | |
10790 | else | |
10791 | { | |
10792 | tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values)); | |
10793 | space = context->gogo()->allocate_memory(element_type, memsize, | |
10794 | this->location()); | |
10795 | space = save_expr(space); | |
10796 | ||
10797 | tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space); | |
10798 | tree ref = build_fold_indirect_ref_loc(this->location(), s); | |
10799 | TREE_THIS_NOTRAP(ref) = 1; | |
10800 | set = build2(MODIFY_EXPR, void_type_node, ref, values); | |
10801 | } | |
10802 | ||
10803 | // Build a constructor for the open array. | |
10804 | ||
10805 | tree type_tree = this->type()->get_tree(context->gogo()); | |
10806 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); | |
10807 | ||
10808 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
10809 | ||
10810 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
10811 | tree field = TYPE_FIELDS(type_tree); | |
10812 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); | |
10813 | elt->index = field; | |
10814 | elt->value = fold_convert(TREE_TYPE(field), space); | |
10815 | ||
10816 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
10817 | field = DECL_CHAIN(field); | |
10818 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); | |
10819 | elt->index = field; | |
10820 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
10821 | ||
10822 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
10823 | field = DECL_CHAIN(field); | |
10824 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0); | |
10825 | elt->index = field; | |
10826 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
10827 | ||
10828 | tree constructor = build_constructor(type_tree, init); | |
10829 | if (!is_in_function && is_constant_initializer) | |
10830 | TREE_CONSTANT(constructor) = 1; | |
10831 | ||
10832 | if (set == NULL_TREE) | |
10833 | return constructor; | |
10834 | else | |
10835 | return build2(COMPOUND_EXPR, type_tree, set, constructor); | |
10836 | } | |
10837 | ||
10838 | // Make a slice composite literal. This is used by the type | |
10839 | // descriptor code. | |
10840 | ||
10841 | Expression* | |
10842 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, | |
10843 | source_location location) | |
10844 | { | |
10845 | gcc_assert(type->is_open_array_type()); | |
10846 | return new Open_array_construction_expression(type, vals, location); | |
10847 | } | |
10848 | ||
10849 | // Construct a map. | |
10850 | ||
10851 | class Map_construction_expression : public Expression | |
10852 | { | |
10853 | public: | |
10854 | Map_construction_expression(Type* type, Expression_list* vals, | |
10855 | source_location location) | |
10856 | : Expression(EXPRESSION_MAP_CONSTRUCTION, location), | |
10857 | type_(type), vals_(vals) | |
10858 | { gcc_assert(vals == NULL || vals->size() % 2 == 0); } | |
10859 | ||
10860 | protected: | |
10861 | int | |
10862 | do_traverse(Traverse* traverse); | |
10863 | ||
10864 | Type* | |
10865 | do_type() | |
10866 | { return this->type_; } | |
10867 | ||
10868 | void | |
10869 | do_determine_type(const Type_context*); | |
10870 | ||
10871 | void | |
10872 | do_check_types(Gogo*); | |
10873 | ||
10874 | Expression* | |
10875 | do_copy() | |
10876 | { | |
10877 | return new Map_construction_expression(this->type_, this->vals_->copy(), | |
10878 | this->location()); | |
10879 | } | |
10880 | ||
10881 | tree | |
10882 | do_get_tree(Translate_context*); | |
10883 | ||
10884 | void | |
10885 | do_export(Export*) const; | |
10886 | ||
10887 | private: | |
10888 | // The type of the map to construct. | |
10889 | Type* type_; | |
10890 | // The list of values. | |
10891 | Expression_list* vals_; | |
10892 | }; | |
10893 | ||
10894 | // Traversal. | |
10895 | ||
10896 | int | |
10897 | Map_construction_expression::do_traverse(Traverse* traverse) | |
10898 | { | |
10899 | if (this->vals_ != NULL | |
10900 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
10901 | return TRAVERSE_EXIT; | |
10902 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10903 | return TRAVERSE_EXIT; | |
10904 | return TRAVERSE_CONTINUE; | |
10905 | } | |
10906 | ||
10907 | // Final type determination. | |
10908 | ||
10909 | void | |
10910 | Map_construction_expression::do_determine_type(const Type_context*) | |
10911 | { | |
10912 | if (this->vals_ == NULL) | |
10913 | return; | |
10914 | ||
10915 | Map_type* mt = this->type_->map_type(); | |
10916 | Type_context key_context(mt->key_type(), false); | |
10917 | Type_context val_context(mt->val_type(), false); | |
10918 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10919 | pv != this->vals_->end(); | |
10920 | ++pv) | |
10921 | { | |
10922 | (*pv)->determine_type(&key_context); | |
10923 | ++pv; | |
10924 | (*pv)->determine_type(&val_context); | |
10925 | } | |
10926 | } | |
10927 | ||
10928 | // Check types. | |
10929 | ||
10930 | void | |
10931 | Map_construction_expression::do_check_types(Gogo*) | |
10932 | { | |
10933 | if (this->vals_ == NULL) | |
10934 | return; | |
10935 | ||
10936 | Map_type* mt = this->type_->map_type(); | |
10937 | int i = 0; | |
10938 | Type* key_type = mt->key_type(); | |
10939 | Type* val_type = mt->val_type(); | |
10940 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10941 | pv != this->vals_->end(); | |
10942 | ++pv, ++i) | |
10943 | { | |
10944 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
10945 | { | |
10946 | error_at((*pv)->location(), | |
10947 | "incompatible type for element %d key in map construction", | |
10948 | i + 1); | |
10949 | this->set_is_error(); | |
10950 | } | |
10951 | ++pv; | |
10952 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
10953 | { | |
10954 | error_at((*pv)->location(), | |
10955 | ("incompatible type for element %d value " | |
10956 | "in map construction"), | |
10957 | i + 1); | |
10958 | this->set_is_error(); | |
10959 | } | |
10960 | } | |
10961 | } | |
10962 | ||
10963 | // Return a tree for constructing a map. | |
10964 | ||
10965 | tree | |
10966 | Map_construction_expression::do_get_tree(Translate_context* context) | |
10967 | { | |
10968 | Gogo* gogo = context->gogo(); | |
10969 | source_location loc = this->location(); | |
10970 | ||
10971 | Map_type* mt = this->type_->map_type(); | |
10972 | ||
10973 | // Build a struct to hold the key and value. | |
10974 | tree struct_type = make_node(RECORD_TYPE); | |
10975 | ||
10976 | Type* key_type = mt->key_type(); | |
10977 | tree id = get_identifier("__key"); | |
10978 | tree key_field = build_decl(loc, FIELD_DECL, id, key_type->get_tree(gogo)); | |
10979 | DECL_CONTEXT(key_field) = struct_type; | |
10980 | TYPE_FIELDS(struct_type) = key_field; | |
10981 | ||
10982 | Type* val_type = mt->val_type(); | |
10983 | id = get_identifier("__val"); | |
10984 | tree val_field = build_decl(loc, FIELD_DECL, id, val_type->get_tree(gogo)); | |
10985 | DECL_CONTEXT(val_field) = struct_type; | |
10986 | DECL_CHAIN(key_field) = val_field; | |
10987 | ||
10988 | layout_type(struct_type); | |
10989 | ||
10990 | bool is_constant = true; | |
10991 | size_t i = 0; | |
10992 | tree valaddr; | |
10993 | tree make_tmp; | |
10994 | ||
10995 | if (this->vals_ == NULL || this->vals_->empty()) | |
10996 | { | |
10997 | valaddr = null_pointer_node; | |
10998 | make_tmp = NULL_TREE; | |
10999 | } | |
11000 | else | |
11001 | { | |
11002 | VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc, | |
11003 | this->vals_->size() / 2); | |
11004 | ||
11005 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11006 | pv != this->vals_->end(); | |
11007 | ++pv, ++i) | |
11008 | { | |
11009 | bool one_is_constant = true; | |
11010 | ||
11011 | VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2); | |
11012 | ||
11013 | constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL); | |
11014 | elt->index = key_field; | |
11015 | tree val_tree = (*pv)->get_tree(context); | |
11016 | elt->value = Expression::convert_for_assignment(context, key_type, | |
11017 | (*pv)->type(), | |
11018 | val_tree, loc); | |
11019 | if (elt->value == error_mark_node) | |
11020 | return error_mark_node; | |
11021 | if (!TREE_CONSTANT(elt->value)) | |
11022 | one_is_constant = false; | |
11023 | ||
11024 | ++pv; | |
11025 | ||
11026 | elt = VEC_quick_push(constructor_elt, one, NULL); | |
11027 | elt->index = val_field; | |
11028 | val_tree = (*pv)->get_tree(context); | |
11029 | elt->value = Expression::convert_for_assignment(context, val_type, | |
11030 | (*pv)->type(), | |
11031 | val_tree, loc); | |
11032 | if (elt->value == error_mark_node) | |
11033 | return error_mark_node; | |
11034 | if (!TREE_CONSTANT(elt->value)) | |
11035 | one_is_constant = false; | |
11036 | ||
11037 | elt = VEC_quick_push(constructor_elt, values, NULL); | |
11038 | elt->index = size_int(i); | |
11039 | elt->value = build_constructor(struct_type, one); | |
11040 | if (one_is_constant) | |
11041 | TREE_CONSTANT(elt->value) = 1; | |
11042 | else | |
11043 | is_constant = false; | |
11044 | } | |
11045 | ||
11046 | tree index_type = build_index_type(size_int(i - 1)); | |
11047 | tree array_type = build_array_type(struct_type, index_type); | |
11048 | tree init = build_constructor(array_type, values); | |
11049 | if (is_constant) | |
11050 | TREE_CONSTANT(init) = 1; | |
11051 | tree tmp; | |
11052 | if (current_function_decl != NULL) | |
11053 | { | |
11054 | tmp = create_tmp_var(array_type, get_name(array_type)); | |
11055 | DECL_INITIAL(tmp) = init; | |
11056 | make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp); | |
11057 | TREE_ADDRESSABLE(tmp) = 1; | |
11058 | } | |
11059 | else | |
11060 | { | |
11061 | tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type); | |
11062 | DECL_EXTERNAL(tmp) = 0; | |
11063 | TREE_PUBLIC(tmp) = 0; | |
11064 | TREE_STATIC(tmp) = 1; | |
11065 | DECL_ARTIFICIAL(tmp) = 1; | |
11066 | if (!TREE_CONSTANT(init)) | |
11067 | make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp, | |
11068 | init); | |
11069 | else | |
11070 | { | |
11071 | TREE_READONLY(tmp) = 1; | |
11072 | TREE_CONSTANT(tmp) = 1; | |
11073 | DECL_INITIAL(tmp) = init; | |
11074 | make_tmp = NULL_TREE; | |
11075 | } | |
11076 | rest_of_decl_compilation(tmp, 1, 0); | |
11077 | } | |
11078 | ||
11079 | valaddr = build_fold_addr_expr(tmp); | |
11080 | } | |
11081 | ||
11082 | tree descriptor = gogo->map_descriptor(mt); | |
11083 | ||
11084 | tree type_tree = this->type_->get_tree(gogo); | |
11085 | ||
11086 | static tree construct_map_fndecl; | |
11087 | tree call = Gogo::call_builtin(&construct_map_fndecl, | |
11088 | loc, | |
11089 | "__go_construct_map", | |
11090 | 6, | |
11091 | type_tree, | |
11092 | TREE_TYPE(descriptor), | |
11093 | descriptor, | |
11094 | sizetype, | |
11095 | size_int(i), | |
11096 | sizetype, | |
11097 | TYPE_SIZE_UNIT(struct_type), | |
11098 | sizetype, | |
11099 | byte_position(val_field), | |
11100 | sizetype, | |
11101 | TYPE_SIZE_UNIT(TREE_TYPE(val_field)), | |
11102 | const_ptr_type_node, | |
11103 | fold_convert(const_ptr_type_node, valaddr)); | |
11104 | ||
11105 | tree ret; | |
11106 | if (make_tmp == NULL) | |
11107 | ret = call; | |
11108 | else | |
11109 | ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call); | |
11110 | return ret; | |
11111 | } | |
11112 | ||
11113 | // Export an array construction. | |
11114 | ||
11115 | void | |
11116 | Map_construction_expression::do_export(Export* exp) const | |
11117 | { | |
11118 | exp->write_c_string("convert("); | |
11119 | exp->write_type(this->type_); | |
11120 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11121 | pv != this->vals_->end(); | |
11122 | ++pv) | |
11123 | { | |
11124 | exp->write_c_string(", "); | |
11125 | (*pv)->export_expression(exp); | |
11126 | } | |
11127 | exp->write_c_string(")"); | |
11128 | } | |
11129 | ||
11130 | // A general composite literal. This is lowered to a type specific | |
11131 | // version. | |
11132 | ||
11133 | class Composite_literal_expression : public Parser_expression | |
11134 | { | |
11135 | public: | |
11136 | Composite_literal_expression(Type* type, int depth, bool has_keys, | |
11137 | Expression_list* vals, source_location location) | |
11138 | : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location), | |
11139 | type_(type), depth_(depth), vals_(vals), has_keys_(has_keys) | |
11140 | { } | |
11141 | ||
11142 | protected: | |
11143 | int | |
11144 | do_traverse(Traverse* traverse); | |
11145 | ||
11146 | Expression* | |
11147 | do_lower(Gogo*, Named_object*, int); | |
11148 | ||
11149 | Expression* | |
11150 | do_copy() | |
11151 | { | |
11152 | return new Composite_literal_expression(this->type_, this->depth_, | |
11153 | this->has_keys_, | |
11154 | (this->vals_ == NULL | |
11155 | ? NULL | |
11156 | : this->vals_->copy()), | |
11157 | this->location()); | |
11158 | } | |
11159 | ||
11160 | private: | |
11161 | Expression* | |
11162 | lower_struct(Type*); | |
11163 | ||
11164 | Expression* | |
11165 | lower_array(Type*); | |
11166 | ||
11167 | Expression* | |
11168 | make_array(Type*, Expression_list*); | |
11169 | ||
11170 | Expression* | |
11171 | lower_map(Type*); | |
11172 | ||
11173 | // The type of the composite literal. | |
11174 | Type* type_; | |
11175 | // The depth within a list of composite literals within a composite | |
11176 | // literal, when the type is omitted. | |
11177 | int depth_; | |
11178 | // The values to put in the composite literal. | |
11179 | Expression_list* vals_; | |
11180 | // If this is true, then VALS_ is a list of pairs: a key and a | |
11181 | // value. In an array initializer, a missing key will be NULL. | |
11182 | bool has_keys_; | |
11183 | }; | |
11184 | ||
11185 | // Traversal. | |
11186 | ||
11187 | int | |
11188 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
11189 | { | |
11190 | if (this->vals_ != NULL | |
11191 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11192 | return TRAVERSE_EXIT; | |
11193 | return Type::traverse(this->type_, traverse); | |
11194 | } | |
11195 | ||
11196 | // Lower a generic composite literal into a specific version based on | |
11197 | // the type. | |
11198 | ||
11199 | Expression* | |
11200 | Composite_literal_expression::do_lower(Gogo*, Named_object*, int) | |
11201 | { | |
11202 | Type* type = this->type_; | |
11203 | ||
11204 | for (int depth = this->depth_; depth > 0; --depth) | |
11205 | { | |
11206 | if (type->array_type() != NULL) | |
11207 | type = type->array_type()->element_type(); | |
11208 | else if (type->map_type() != NULL) | |
11209 | type = type->map_type()->val_type(); | |
11210 | else | |
11211 | { | |
11212 | if (!type->is_error_type()) | |
11213 | error_at(this->location(), | |
11214 | ("may only omit types within composite literals " | |
11215 | "of slice, array, or map type")); | |
11216 | return Expression::make_error(this->location()); | |
11217 | } | |
11218 | } | |
11219 | ||
11220 | if (type->is_error_type()) | |
11221 | return Expression::make_error(this->location()); | |
11222 | else if (type->struct_type() != NULL) | |
11223 | return this->lower_struct(type); | |
11224 | else if (type->array_type() != NULL) | |
11225 | return this->lower_array(type); | |
11226 | else if (type->map_type() != NULL) | |
11227 | return this->lower_map(type); | |
11228 | else | |
11229 | { | |
11230 | error_at(this->location(), | |
11231 | ("expected struct, slice, array, or map type " | |
11232 | "for composite literal")); | |
11233 | return Expression::make_error(this->location()); | |
11234 | } | |
11235 | } | |
11236 | ||
11237 | // Lower a struct composite literal. | |
11238 | ||
11239 | Expression* | |
11240 | Composite_literal_expression::lower_struct(Type* type) | |
11241 | { | |
11242 | source_location location = this->location(); | |
11243 | Struct_type* st = type->struct_type(); | |
11244 | if (this->vals_ == NULL || !this->has_keys_) | |
11245 | return new Struct_construction_expression(type, this->vals_, location); | |
11246 | ||
11247 | size_t field_count = st->field_count(); | |
11248 | std::vector<Expression*> vals(field_count); | |
11249 | Expression_list::const_iterator p = this->vals_->begin(); | |
11250 | while (p != this->vals_->end()) | |
11251 | { | |
11252 | Expression* name_expr = *p; | |
11253 | ||
11254 | ++p; | |
11255 | gcc_assert(p != this->vals_->end()); | |
11256 | Expression* val = *p; | |
11257 | ||
11258 | ++p; | |
11259 | ||
11260 | if (name_expr == NULL) | |
11261 | { | |
11262 | error_at(val->location(), "mixture of field and value initializers"); | |
11263 | return Expression::make_error(location); | |
11264 | } | |
11265 | ||
11266 | bool bad_key = false; | |
11267 | std::string name; | |
11268 | switch (name_expr->classification()) | |
11269 | { | |
11270 | case EXPRESSION_UNKNOWN_REFERENCE: | |
11271 | name = name_expr->unknown_expression()->name(); | |
11272 | break; | |
11273 | ||
11274 | case EXPRESSION_CONST_REFERENCE: | |
11275 | name = static_cast<Const_expression*>(name_expr)->name(); | |
11276 | break; | |
11277 | ||
11278 | case EXPRESSION_TYPE: | |
11279 | { | |
11280 | Type* t = name_expr->type(); | |
11281 | Named_type* nt = t->named_type(); | |
11282 | if (nt == NULL) | |
11283 | bad_key = true; | |
11284 | else | |
11285 | name = nt->name(); | |
11286 | } | |
11287 | break; | |
11288 | ||
11289 | case EXPRESSION_VAR_REFERENCE: | |
11290 | name = name_expr->var_expression()->name(); | |
11291 | break; | |
11292 | ||
11293 | case EXPRESSION_FUNC_REFERENCE: | |
11294 | name = name_expr->func_expression()->name(); | |
11295 | break; | |
11296 | ||
11297 | case EXPRESSION_UNARY: | |
11298 | // If there is a local variable around with the same name as | |
11299 | // the field, and this occurs in the closure, then the | |
11300 | // parser may turn the field reference into an indirection | |
11301 | // through the closure. FIXME: This is a mess. | |
11302 | { | |
11303 | bad_key = true; | |
11304 | Unary_expression* ue = static_cast<Unary_expression*>(name_expr); | |
11305 | if (ue->op() == OPERATOR_MULT) | |
11306 | { | |
11307 | Field_reference_expression* fre = | |
11308 | ue->operand()->field_reference_expression(); | |
11309 | if (fre != NULL) | |
11310 | { | |
11311 | Struct_type* st = | |
11312 | fre->expr()->type()->deref()->struct_type(); | |
11313 | if (st != NULL) | |
11314 | { | |
11315 | const Struct_field* sf = st->field(fre->field_index()); | |
11316 | name = sf->field_name(); | |
11317 | char buf[20]; | |
11318 | snprintf(buf, sizeof buf, "%u", fre->field_index()); | |
11319 | size_t buflen = strlen(buf); | |
11320 | if (name.compare(name.length() - buflen, buflen, buf) | |
11321 | == 0) | |
11322 | { | |
11323 | name = name.substr(0, name.length() - buflen); | |
11324 | bad_key = false; | |
11325 | } | |
11326 | } | |
11327 | } | |
11328 | } | |
11329 | } | |
11330 | break; | |
11331 | ||
11332 | default: | |
11333 | bad_key = true; | |
11334 | break; | |
11335 | } | |
11336 | if (bad_key) | |
11337 | { | |
11338 | error_at(name_expr->location(), "expected struct field name"); | |
11339 | return Expression::make_error(location); | |
11340 | } | |
11341 | ||
11342 | unsigned int index; | |
11343 | const Struct_field* sf = st->find_local_field(name, &index); | |
11344 | if (sf == NULL) | |
11345 | { | |
11346 | error_at(name_expr->location(), "unknown field %qs in %qs", | |
11347 | Gogo::message_name(name).c_str(), | |
11348 | (type->named_type() != NULL | |
11349 | ? type->named_type()->message_name().c_str() | |
11350 | : "unnamed struct")); | |
11351 | return Expression::make_error(location); | |
11352 | } | |
11353 | if (vals[index] != NULL) | |
11354 | { | |
11355 | error_at(name_expr->location(), | |
11356 | "duplicate value for field %qs in %qs", | |
11357 | Gogo::message_name(name).c_str(), | |
11358 | (type->named_type() != NULL | |
11359 | ? type->named_type()->message_name().c_str() | |
11360 | : "unnamed struct")); | |
11361 | return Expression::make_error(location); | |
11362 | } | |
11363 | ||
11364 | vals[index] = val; | |
11365 | } | |
11366 | ||
11367 | Expression_list* list = new Expression_list; | |
11368 | list->reserve(field_count); | |
11369 | for (size_t i = 0; i < field_count; ++i) | |
11370 | list->push_back(vals[i]); | |
11371 | ||
11372 | return new Struct_construction_expression(type, list, location); | |
11373 | } | |
11374 | ||
11375 | // Lower an array composite literal. | |
11376 | ||
11377 | Expression* | |
11378 | Composite_literal_expression::lower_array(Type* type) | |
11379 | { | |
11380 | source_location location = this->location(); | |
11381 | if (this->vals_ == NULL || !this->has_keys_) | |
11382 | return this->make_array(type, this->vals_); | |
11383 | ||
11384 | std::vector<Expression*> vals; | |
11385 | vals.reserve(this->vals_->size()); | |
11386 | unsigned long index = 0; | |
11387 | Expression_list::const_iterator p = this->vals_->begin(); | |
11388 | while (p != this->vals_->end()) | |
11389 | { | |
11390 | Expression* index_expr = *p; | |
11391 | ||
11392 | ++p; | |
11393 | gcc_assert(p != this->vals_->end()); | |
11394 | Expression* val = *p; | |
11395 | ||
11396 | ++p; | |
11397 | ||
11398 | if (index_expr != NULL) | |
11399 | { | |
11400 | mpz_t ival; | |
11401 | mpz_init(ival); | |
11402 | Type* dummy; | |
11403 | if (!index_expr->integer_constant_value(true, ival, &dummy)) | |
11404 | { | |
11405 | mpz_clear(ival); | |
11406 | error_at(index_expr->location(), | |
11407 | "index expression is not integer constant"); | |
11408 | return Expression::make_error(location); | |
11409 | } | |
11410 | if (mpz_sgn(ival) < 0) | |
11411 | { | |
11412 | mpz_clear(ival); | |
11413 | error_at(index_expr->location(), "index expression is negative"); | |
11414 | return Expression::make_error(location); | |
11415 | } | |
11416 | index = mpz_get_ui(ival); | |
11417 | if (mpz_cmp_ui(ival, index) != 0) | |
11418 | { | |
11419 | mpz_clear(ival); | |
11420 | error_at(index_expr->location(), "index value overflow"); | |
11421 | return Expression::make_error(location); | |
11422 | } | |
11423 | mpz_clear(ival); | |
11424 | } | |
11425 | ||
11426 | if (index == vals.size()) | |
11427 | vals.push_back(val); | |
11428 | else | |
11429 | { | |
11430 | if (index > vals.size()) | |
11431 | { | |
11432 | vals.reserve(index + 32); | |
11433 | vals.resize(index + 1, static_cast<Expression*>(NULL)); | |
11434 | } | |
11435 | if (vals[index] != NULL) | |
11436 | { | |
11437 | error_at((index_expr != NULL | |
11438 | ? index_expr->location() | |
11439 | : val->location()), | |
11440 | "duplicate value for index %lu", | |
11441 | index); | |
11442 | return Expression::make_error(location); | |
11443 | } | |
11444 | vals[index] = val; | |
11445 | } | |
11446 | ||
11447 | ++index; | |
11448 | } | |
11449 | ||
11450 | size_t size = vals.size(); | |
11451 | Expression_list* list = new Expression_list; | |
11452 | list->reserve(size); | |
11453 | for (size_t i = 0; i < size; ++i) | |
11454 | list->push_back(vals[i]); | |
11455 | ||
11456 | return this->make_array(type, list); | |
11457 | } | |
11458 | ||
11459 | // Actually build the array composite literal. This handles | |
11460 | // [...]{...}. | |
11461 | ||
11462 | Expression* | |
11463 | Composite_literal_expression::make_array(Type* type, Expression_list* vals) | |
11464 | { | |
11465 | source_location location = this->location(); | |
11466 | Array_type* at = type->array_type(); | |
11467 | if (at->length() != NULL && at->length()->is_nil_expression()) | |
11468 | { | |
11469 | size_t size = vals == NULL ? 0 : vals->size(); | |
11470 | mpz_t vlen; | |
11471 | mpz_init_set_ui(vlen, size); | |
11472 | Expression* elen = Expression::make_integer(&vlen, NULL, location); | |
11473 | mpz_clear(vlen); | |
11474 | at = Type::make_array_type(at->element_type(), elen); | |
11475 | type = at; | |
11476 | } | |
11477 | if (at->length() != NULL) | |
11478 | return new Fixed_array_construction_expression(type, vals, location); | |
11479 | else | |
11480 | return new Open_array_construction_expression(type, vals, location); | |
11481 | } | |
11482 | ||
11483 | // Lower a map composite literal. | |
11484 | ||
11485 | Expression* | |
11486 | Composite_literal_expression::lower_map(Type* type) | |
11487 | { | |
11488 | source_location location = this->location(); | |
11489 | if (this->vals_ != NULL) | |
11490 | { | |
11491 | if (!this->has_keys_) | |
11492 | { | |
11493 | error_at(location, "map composite literal must have keys"); | |
11494 | return Expression::make_error(location); | |
11495 | } | |
11496 | ||
11497 | for (Expression_list::const_iterator p = this->vals_->begin(); | |
11498 | p != this->vals_->end(); | |
11499 | p += 2) | |
11500 | { | |
11501 | if (*p == NULL) | |
11502 | { | |
11503 | ++p; | |
11504 | error_at((*p)->location(), | |
11505 | "map composite literal must have keys for every value"); | |
11506 | return Expression::make_error(location); | |
11507 | } | |
11508 | } | |
11509 | } | |
11510 | ||
11511 | return new Map_construction_expression(type, this->vals_, location); | |
11512 | } | |
11513 | ||
11514 | // Make a composite literal expression. | |
11515 | ||
11516 | Expression* | |
11517 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
11518 | Expression_list* vals, | |
11519 | source_location location) | |
11520 | { | |
11521 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
11522 | location); | |
11523 | } | |
11524 | ||
11525 | // Return whether this expression is a composite literal. | |
11526 | ||
11527 | bool | |
11528 | Expression::is_composite_literal() const | |
11529 | { | |
11530 | switch (this->classification_) | |
11531 | { | |
11532 | case EXPRESSION_COMPOSITE_LITERAL: | |
11533 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
11534 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
11535 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
11536 | case EXPRESSION_MAP_CONSTRUCTION: | |
11537 | return true; | |
11538 | default: | |
11539 | return false; | |
11540 | } | |
11541 | } | |
11542 | ||
11543 | // Return whether this expression is a composite literal which is not | |
11544 | // constant. | |
11545 | ||
11546 | bool | |
11547 | Expression::is_nonconstant_composite_literal() const | |
11548 | { | |
11549 | switch (this->classification_) | |
11550 | { | |
11551 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
11552 | { | |
11553 | const Struct_construction_expression *psce = | |
11554 | static_cast<const Struct_construction_expression*>(this); | |
11555 | return !psce->is_constant_struct(); | |
11556 | } | |
11557 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
11558 | { | |
11559 | const Fixed_array_construction_expression *pace = | |
11560 | static_cast<const Fixed_array_construction_expression*>(this); | |
11561 | return !pace->is_constant_array(); | |
11562 | } | |
11563 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
11564 | { | |
11565 | const Open_array_construction_expression *pace = | |
11566 | static_cast<const Open_array_construction_expression*>(this); | |
11567 | return !pace->is_constant_array(); | |
11568 | } | |
11569 | case EXPRESSION_MAP_CONSTRUCTION: | |
11570 | return true; | |
11571 | default: | |
11572 | return false; | |
11573 | } | |
11574 | } | |
11575 | ||
11576 | // Return true if this is a reference to a local variable. | |
11577 | ||
11578 | bool | |
11579 | Expression::is_local_variable() const | |
11580 | { | |
11581 | const Var_expression* ve = this->var_expression(); | |
11582 | if (ve == NULL) | |
11583 | return false; | |
11584 | const Named_object* no = ve->named_object(); | |
11585 | return (no->is_result_variable() | |
11586 | || (no->is_variable() && !no->var_value()->is_global())); | |
11587 | } | |
11588 | ||
11589 | // Class Type_guard_expression. | |
11590 | ||
11591 | // Traversal. | |
11592 | ||
11593 | int | |
11594 | Type_guard_expression::do_traverse(Traverse* traverse) | |
11595 | { | |
11596 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
11597 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
11598 | return TRAVERSE_EXIT; | |
11599 | return TRAVERSE_CONTINUE; | |
11600 | } | |
11601 | ||
11602 | // Check types of a type guard expression. The expression must have | |
11603 | // an interface type, but the actual type conversion is checked at run | |
11604 | // time. | |
11605 | ||
11606 | void | |
11607 | Type_guard_expression::do_check_types(Gogo*) | |
11608 | { | |
11609 | // 6g permits using a type guard with unsafe.pointer; we are | |
11610 | // compatible. | |
11611 | Type* expr_type = this->expr_->type(); | |
11612 | if (expr_type->is_unsafe_pointer_type()) | |
11613 | { | |
11614 | if (this->type_->points_to() == NULL | |
11615 | && (this->type_->integer_type() == NULL | |
11616 | || (this->type_->forwarded() | |
11617 | != Type::lookup_integer_type("uintptr")))) | |
11618 | this->report_error(_("invalid unsafe.Pointer conversion")); | |
11619 | } | |
11620 | else if (this->type_->is_unsafe_pointer_type()) | |
11621 | { | |
11622 | if (expr_type->points_to() == NULL | |
11623 | && (expr_type->integer_type() == NULL | |
11624 | || (expr_type->forwarded() | |
11625 | != Type::lookup_integer_type("uintptr")))) | |
11626 | this->report_error(_("invalid unsafe.Pointer conversion")); | |
11627 | } | |
11628 | else if (expr_type->interface_type() == NULL) | |
11629 | this->report_error(_("type assertion only valid for interface types")); | |
11630 | else if (this->type_->interface_type() == NULL) | |
11631 | { | |
11632 | std::string reason; | |
11633 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
11634 | &reason)) | |
11635 | { | |
11636 | if (reason.empty()) | |
11637 | this->report_error(_("impossible type assertion: " | |
11638 | "type does not implement interface")); | |
11639 | else | |
11640 | { | |
11641 | error_at(this->location(), | |
11642 | ("impossible type assertion: " | |
11643 | "type does not implement interface (%s)"), | |
11644 | reason.c_str()); | |
11645 | this->set_is_error(); | |
11646 | } | |
11647 | } | |
11648 | } | |
11649 | } | |
11650 | ||
11651 | // Return a tree for a type guard expression. | |
11652 | ||
11653 | tree | |
11654 | Type_guard_expression::do_get_tree(Translate_context* context) | |
11655 | { | |
11656 | Gogo* gogo = context->gogo(); | |
11657 | tree expr_tree = this->expr_->get_tree(context); | |
11658 | if (expr_tree == error_mark_node) | |
11659 | return error_mark_node; | |
11660 | Type* expr_type = this->expr_->type(); | |
11661 | if ((this->type_->is_unsafe_pointer_type() | |
11662 | && (expr_type->points_to() != NULL | |
11663 | || expr_type->integer_type() != NULL)) | |
11664 | || (expr_type->is_unsafe_pointer_type() | |
11665 | && this->type_->points_to() != NULL)) | |
11666 | return convert_to_pointer(this->type_->get_tree(gogo), expr_tree); | |
11667 | else if (expr_type->is_unsafe_pointer_type() | |
11668 | && this->type_->integer_type() != NULL) | |
11669 | return convert_to_integer(this->type_->get_tree(gogo), expr_tree); | |
11670 | else if (this->type_->interface_type() != NULL) | |
11671 | return Expression::convert_interface_to_interface(context, this->type_, | |
11672 | this->expr_->type(), | |
11673 | expr_tree, true, | |
11674 | this->location()); | |
11675 | else | |
11676 | return Expression::convert_for_assignment(context, this->type_, | |
11677 | this->expr_->type(), expr_tree, | |
11678 | this->location()); | |
11679 | } | |
11680 | ||
11681 | // Make a type guard expression. | |
11682 | ||
11683 | Expression* | |
11684 | Expression::make_type_guard(Expression* expr, Type* type, | |
11685 | source_location location) | |
11686 | { | |
11687 | return new Type_guard_expression(expr, type, location); | |
11688 | } | |
11689 | ||
11690 | // Class Heap_composite_expression. | |
11691 | ||
11692 | // When you take the address of a composite literal, it is allocated | |
11693 | // on the heap. This class implements that. | |
11694 | ||
11695 | class Heap_composite_expression : public Expression | |
11696 | { | |
11697 | public: | |
11698 | Heap_composite_expression(Expression* expr, source_location location) | |
11699 | : Expression(EXPRESSION_HEAP_COMPOSITE, location), | |
11700 | expr_(expr) | |
11701 | { } | |
11702 | ||
11703 | protected: | |
11704 | int | |
11705 | do_traverse(Traverse* traverse) | |
11706 | { return Expression::traverse(&this->expr_, traverse); } | |
11707 | ||
11708 | Type* | |
11709 | do_type() | |
11710 | { return Type::make_pointer_type(this->expr_->type()); } | |
11711 | ||
11712 | void | |
11713 | do_determine_type(const Type_context*) | |
11714 | { this->expr_->determine_type_no_context(); } | |
11715 | ||
11716 | Expression* | |
11717 | do_copy() | |
11718 | { | |
11719 | return Expression::make_heap_composite(this->expr_->copy(), | |
11720 | this->location()); | |
11721 | } | |
11722 | ||
11723 | tree | |
11724 | do_get_tree(Translate_context*); | |
11725 | ||
11726 | // We only export global objects, and the parser does not generate | |
11727 | // this in global scope. | |
11728 | void | |
11729 | do_export(Export*) const | |
11730 | { gcc_unreachable(); } | |
11731 | ||
11732 | private: | |
11733 | // The composite literal which is being put on the heap. | |
11734 | Expression* expr_; | |
11735 | }; | |
11736 | ||
11737 | // Return a tree which allocates a composite literal on the heap. | |
11738 | ||
11739 | tree | |
11740 | Heap_composite_expression::do_get_tree(Translate_context* context) | |
11741 | { | |
11742 | tree expr_tree = this->expr_->get_tree(context); | |
11743 | if (expr_tree == error_mark_node) | |
11744 | return error_mark_node; | |
11745 | tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree)); | |
11746 | gcc_assert(TREE_CODE(expr_size) == INTEGER_CST); | |
11747 | tree space = context->gogo()->allocate_memory(this->expr_->type(), | |
11748 | expr_size, this->location()); | |
11749 | space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space); | |
11750 | space = save_expr(space); | |
11751 | tree ref = build_fold_indirect_ref_loc(this->location(), space); | |
11752 | TREE_THIS_NOTRAP(ref) = 1; | |
11753 | tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space), | |
11754 | build2(MODIFY_EXPR, void_type_node, ref, expr_tree), | |
11755 | space); | |
11756 | SET_EXPR_LOCATION(ret, this->location()); | |
11757 | return ret; | |
11758 | } | |
11759 | ||
11760 | // Allocate a composite literal on the heap. | |
11761 | ||
11762 | Expression* | |
11763 | Expression::make_heap_composite(Expression* expr, source_location location) | |
11764 | { | |
11765 | return new Heap_composite_expression(expr, location); | |
11766 | } | |
11767 | ||
11768 | // Class Receive_expression. | |
11769 | ||
11770 | // Return the type of a receive expression. | |
11771 | ||
11772 | Type* | |
11773 | Receive_expression::do_type() | |
11774 | { | |
11775 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
11776 | if (channel_type == NULL) | |
11777 | return Type::make_error_type(); | |
11778 | return channel_type->element_type(); | |
11779 | } | |
11780 | ||
11781 | // Check types for a receive expression. | |
11782 | ||
11783 | void | |
11784 | Receive_expression::do_check_types(Gogo*) | |
11785 | { | |
11786 | Type* type = this->channel_->type(); | |
11787 | if (type->is_error_type()) | |
11788 | { | |
11789 | this->set_is_error(); | |
11790 | return; | |
11791 | } | |
11792 | if (type->channel_type() == NULL) | |
11793 | { | |
11794 | this->report_error(_("expected channel")); | |
11795 | return; | |
11796 | } | |
11797 | if (!type->channel_type()->may_receive()) | |
11798 | { | |
11799 | this->report_error(_("invalid receive on send-only channel")); | |
11800 | return; | |
11801 | } | |
11802 | } | |
11803 | ||
11804 | // Get a tree for a receive expression. | |
11805 | ||
11806 | tree | |
11807 | Receive_expression::do_get_tree(Translate_context* context) | |
11808 | { | |
11809 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
11810 | gcc_assert(channel_type != NULL); | |
11811 | Type* element_type = channel_type->element_type(); | |
11812 | tree element_type_tree = element_type->get_tree(context->gogo()); | |
11813 | ||
11814 | tree channel = this->channel_->get_tree(context); | |
11815 | if (element_type_tree == error_mark_node || channel == error_mark_node) | |
11816 | return error_mark_node; | |
11817 | ||
11818 | return Gogo::receive_from_channel(element_type_tree, channel, | |
11819 | this->for_select_, this->location()); | |
11820 | } | |
11821 | ||
11822 | // Make a receive expression. | |
11823 | ||
11824 | Receive_expression* | |
11825 | Expression::make_receive(Expression* channel, source_location location) | |
11826 | { | |
11827 | return new Receive_expression(channel, location); | |
11828 | } | |
11829 | ||
11830 | // Class Send_expression. | |
11831 | ||
11832 | // Traversal. | |
11833 | ||
11834 | int | |
11835 | Send_expression::do_traverse(Traverse* traverse) | |
11836 | { | |
11837 | if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT) | |
11838 | return TRAVERSE_EXIT; | |
11839 | return Expression::traverse(&this->val_, traverse); | |
11840 | } | |
11841 | ||
11842 | // Get the type. | |
11843 | ||
11844 | Type* | |
11845 | Send_expression::do_type() | |
11846 | { | |
11847 | return Type::lookup_bool_type(); | |
11848 | } | |
11849 | ||
11850 | // Set types. | |
11851 | ||
11852 | void | |
11853 | Send_expression::do_determine_type(const Type_context*) | |
11854 | { | |
11855 | this->channel_->determine_type_no_context(); | |
11856 | ||
11857 | Type* type = this->channel_->type(); | |
11858 | Type_context subcontext; | |
11859 | if (type->channel_type() != NULL) | |
11860 | subcontext.type = type->channel_type()->element_type(); | |
11861 | this->val_->determine_type(&subcontext); | |
11862 | } | |
11863 | ||
11864 | // Check types. | |
11865 | ||
11866 | void | |
11867 | Send_expression::do_check_types(Gogo*) | |
11868 | { | |
11869 | Type* type = this->channel_->type(); | |
11870 | if (type->is_error_type()) | |
11871 | { | |
11872 | this->set_is_error(); | |
11873 | return; | |
11874 | } | |
11875 | Channel_type* channel_type = type->channel_type(); | |
11876 | if (channel_type == NULL) | |
11877 | { | |
11878 | error_at(this->location(), "left operand of %<<-%> must be channel"); | |
11879 | this->set_is_error(); | |
11880 | return; | |
11881 | } | |
11882 | Type* element_type = channel_type->element_type(); | |
11883 | if (element_type != NULL | |
11884 | && !Type::are_assignable(element_type, this->val_->type(), NULL)) | |
11885 | { | |
11886 | this->report_error(_("incompatible types in send")); | |
11887 | return; | |
11888 | } | |
11889 | if (!channel_type->may_send()) | |
11890 | { | |
11891 | this->report_error(_("invalid send on receive-only channel")); | |
11892 | return; | |
11893 | } | |
11894 | } | |
11895 | ||
11896 | // Get a tree for a send expression. | |
11897 | ||
11898 | tree | |
11899 | Send_expression::do_get_tree(Translate_context* context) | |
11900 | { | |
11901 | tree channel = this->channel_->get_tree(context); | |
11902 | tree val = this->val_->get_tree(context); | |
11903 | if (channel == error_mark_node || val == error_mark_node) | |
11904 | return error_mark_node; | |
11905 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
11906 | val = Expression::convert_for_assignment(context, | |
11907 | channel_type->element_type(), | |
11908 | this->val_->type(), | |
11909 | val, | |
11910 | this->location()); | |
11911 | return Gogo::send_on_channel(channel, val, this->is_value_discarded_, | |
11912 | this->for_select_, this->location()); | |
11913 | } | |
11914 | ||
11915 | // Make a send expression | |
11916 | ||
11917 | Send_expression* | |
11918 | Expression::make_send(Expression* channel, Expression* val, | |
11919 | source_location location) | |
11920 | { | |
11921 | return new Send_expression(channel, val, location); | |
11922 | } | |
11923 | ||
11924 | // An expression which evaluates to a pointer to the type descriptor | |
11925 | // of a type. | |
11926 | ||
11927 | class Type_descriptor_expression : public Expression | |
11928 | { | |
11929 | public: | |
11930 | Type_descriptor_expression(Type* type, source_location location) | |
11931 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), | |
11932 | type_(type) | |
11933 | { } | |
11934 | ||
11935 | protected: | |
11936 | Type* | |
11937 | do_type() | |
11938 | { return Type::make_type_descriptor_ptr_type(); } | |
11939 | ||
11940 | void | |
11941 | do_determine_type(const Type_context*) | |
11942 | { } | |
11943 | ||
11944 | Expression* | |
11945 | do_copy() | |
11946 | { return this; } | |
11947 | ||
11948 | tree | |
11949 | do_get_tree(Translate_context* context) | |
11950 | { return this->type_->type_descriptor_pointer(context->gogo()); } | |
11951 | ||
11952 | private: | |
11953 | // The type for which this is the descriptor. | |
11954 | Type* type_; | |
11955 | }; | |
11956 | ||
11957 | // Make a type descriptor expression. | |
11958 | ||
11959 | Expression* | |
11960 | Expression::make_type_descriptor(Type* type, source_location location) | |
11961 | { | |
11962 | return new Type_descriptor_expression(type, location); | |
11963 | } | |
11964 | ||
11965 | // An expression which evaluates to some characteristic of a type. | |
11966 | // This is only used to initialize fields of a type descriptor. Using | |
11967 | // a new expression class is slightly inefficient but gives us a good | |
11968 | // separation between the frontend and the middle-end with regard to | |
11969 | // how types are laid out. | |
11970 | ||
11971 | class Type_info_expression : public Expression | |
11972 | { | |
11973 | public: | |
11974 | Type_info_expression(Type* type, Type_info type_info) | |
11975 | : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION), | |
11976 | type_(type), type_info_(type_info) | |
11977 | { } | |
11978 | ||
11979 | protected: | |
11980 | Type* | |
11981 | do_type(); | |
11982 | ||
11983 | void | |
11984 | do_determine_type(const Type_context*) | |
11985 | { } | |
11986 | ||
11987 | Expression* | |
11988 | do_copy() | |
11989 | { return this; } | |
11990 | ||
11991 | tree | |
11992 | do_get_tree(Translate_context* context); | |
11993 | ||
11994 | private: | |
11995 | // The type for which we are getting information. | |
11996 | Type* type_; | |
11997 | // What information we want. | |
11998 | Type_info type_info_; | |
11999 | }; | |
12000 | ||
12001 | // The type is chosen to match what the type descriptor struct | |
12002 | // expects. | |
12003 | ||
12004 | Type* | |
12005 | Type_info_expression::do_type() | |
12006 | { | |
12007 | switch (this->type_info_) | |
12008 | { | |
12009 | case TYPE_INFO_SIZE: | |
12010 | return Type::lookup_integer_type("uintptr"); | |
12011 | case TYPE_INFO_ALIGNMENT: | |
12012 | case TYPE_INFO_FIELD_ALIGNMENT: | |
12013 | return Type::lookup_integer_type("uint8"); | |
12014 | default: | |
12015 | gcc_unreachable(); | |
12016 | } | |
12017 | } | |
12018 | ||
12019 | // Return type information in GENERIC. | |
12020 | ||
12021 | tree | |
12022 | Type_info_expression::do_get_tree(Translate_context* context) | |
12023 | { | |
12024 | tree type_tree = this->type_->get_tree(context->gogo()); | |
12025 | if (type_tree == error_mark_node) | |
12026 | return error_mark_node; | |
12027 | ||
12028 | tree val_type_tree = this->type()->get_tree(context->gogo()); | |
12029 | gcc_assert(val_type_tree != error_mark_node); | |
12030 | ||
12031 | if (this->type_info_ == TYPE_INFO_SIZE) | |
12032 | return fold_convert_loc(BUILTINS_LOCATION, val_type_tree, | |
12033 | TYPE_SIZE_UNIT(type_tree)); | |
12034 | else | |
12035 | { | |
12036 | unsigned HOST_WIDE_INT val; | |
12037 | if (this->type_info_ == TYPE_INFO_ALIGNMENT) | |
12038 | val = TYPE_ALIGN_UNIT(type_tree); | |
12039 | else | |
12040 | { | |
12041 | gcc_assert(this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT); | |
12042 | val = TYPE_ALIGN(type_tree); | |
12043 | #ifdef BIGGEST_FIELD_ALIGMENT | |
12044 | if (val > BIGGEST_FIELD_ALIGNMENT) | |
12045 | val = BIGGEST_FIELD_ALIGNMENT; | |
12046 | #endif | |
12047 | #ifdef ADJUST_FIELD_ALIGN | |
12048 | { | |
12049 | tree f = build_decl(UNKNOWN_LOCATION, FIELD_DECL, NULL, type_tree); | |
12050 | val = ADJUST_FIELD_ALIGN(f, val); | |
12051 | } | |
12052 | #endif | |
12053 | val /= BITS_PER_UNIT; | |
12054 | } | |
12055 | ||
12056 | return build_int_cstu(val_type_tree, val); | |
12057 | } | |
12058 | } | |
12059 | ||
12060 | // Make a type info expression. | |
12061 | ||
12062 | Expression* | |
12063 | Expression::make_type_info(Type* type, Type_info type_info) | |
12064 | { | |
12065 | return new Type_info_expression(type, type_info); | |
12066 | } | |
12067 | ||
12068 | // An expression which evaluates to the offset of a field within a | |
12069 | // struct. This, like Type_info_expression, q.v., is only used to | |
12070 | // initialize fields of a type descriptor. | |
12071 | ||
12072 | class Struct_field_offset_expression : public Expression | |
12073 | { | |
12074 | public: | |
12075 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
12076 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION), | |
12077 | type_(type), field_(field) | |
12078 | { } | |
12079 | ||
12080 | protected: | |
12081 | Type* | |
12082 | do_type() | |
12083 | { return Type::lookup_integer_type("uintptr"); } | |
12084 | ||
12085 | void | |
12086 | do_determine_type(const Type_context*) | |
12087 | { } | |
12088 | ||
12089 | Expression* | |
12090 | do_copy() | |
12091 | { return this; } | |
12092 | ||
12093 | tree | |
12094 | do_get_tree(Translate_context* context); | |
12095 | ||
12096 | private: | |
12097 | // The type of the struct. | |
12098 | Struct_type* type_; | |
12099 | // The field. | |
12100 | const Struct_field* field_; | |
12101 | }; | |
12102 | ||
12103 | // Return a struct field offset in GENERIC. | |
12104 | ||
12105 | tree | |
12106 | Struct_field_offset_expression::do_get_tree(Translate_context* context) | |
12107 | { | |
12108 | tree type_tree = this->type_->get_tree(context->gogo()); | |
12109 | if (type_tree == error_mark_node) | |
12110 | return error_mark_node; | |
12111 | ||
12112 | tree val_type_tree = this->type()->get_tree(context->gogo()); | |
12113 | gcc_assert(val_type_tree != error_mark_node); | |
12114 | ||
12115 | const Struct_field_list* fields = this->type_->fields(); | |
12116 | tree struct_field_tree = TYPE_FIELDS(type_tree); | |
12117 | Struct_field_list::const_iterator p; | |
12118 | for (p = fields->begin(); | |
12119 | p != fields->end(); | |
12120 | ++p, struct_field_tree = DECL_CHAIN(struct_field_tree)) | |
12121 | { | |
12122 | gcc_assert(struct_field_tree != NULL_TREE); | |
12123 | if (&*p == this->field_) | |
12124 | break; | |
12125 | } | |
12126 | gcc_assert(&*p == this->field_); | |
12127 | ||
12128 | return fold_convert_loc(BUILTINS_LOCATION, val_type_tree, | |
12129 | byte_position(struct_field_tree)); | |
12130 | } | |
12131 | ||
12132 | // Make an expression for a struct field offset. | |
12133 | ||
12134 | Expression* | |
12135 | Expression::make_struct_field_offset(Struct_type* type, | |
12136 | const Struct_field* field) | |
12137 | { | |
12138 | return new Struct_field_offset_expression(type, field); | |
12139 | } | |
12140 | ||
12141 | // An expression which evaluates to the address of an unnamed label. | |
12142 | ||
12143 | class Label_addr_expression : public Expression | |
12144 | { | |
12145 | public: | |
12146 | Label_addr_expression(Label* label, source_location location) | |
12147 | : Expression(EXPRESSION_LABEL_ADDR, location), | |
12148 | label_(label) | |
12149 | { } | |
12150 | ||
12151 | protected: | |
12152 | Type* | |
12153 | do_type() | |
12154 | { return Type::make_pointer_type(Type::make_void_type()); } | |
12155 | ||
12156 | void | |
12157 | do_determine_type(const Type_context*) | |
12158 | { } | |
12159 | ||
12160 | Expression* | |
12161 | do_copy() | |
12162 | { return new Label_addr_expression(this->label_, this->location()); } | |
12163 | ||
12164 | tree | |
12165 | do_get_tree(Translate_context*) | |
12166 | { return this->label_->get_addr(this->location()); } | |
12167 | ||
12168 | private: | |
12169 | // The label whose address we are taking. | |
12170 | Label* label_; | |
12171 | }; | |
12172 | ||
12173 | // Make an expression for the address of an unnamed label. | |
12174 | ||
12175 | Expression* | |
12176 | Expression::make_label_addr(Label* label, source_location location) | |
12177 | { | |
12178 | return new Label_addr_expression(label, location); | |
12179 | } | |
12180 | ||
12181 | // Import an expression. This comes at the end in order to see the | |
12182 | // various class definitions. | |
12183 | ||
12184 | Expression* | |
12185 | Expression::import_expression(Import* imp) | |
12186 | { | |
12187 | int c = imp->peek_char(); | |
12188 | if (imp->match_c_string("- ") | |
12189 | || imp->match_c_string("! ") | |
12190 | || imp->match_c_string("^ ")) | |
12191 | return Unary_expression::do_import(imp); | |
12192 | else if (c == '(') | |
12193 | return Binary_expression::do_import(imp); | |
12194 | else if (imp->match_c_string("true") | |
12195 | || imp->match_c_string("false")) | |
12196 | return Boolean_expression::do_import(imp); | |
12197 | else if (c == '"') | |
12198 | return String_expression::do_import(imp); | |
12199 | else if (c == '-' || (c >= '0' && c <= '9')) | |
12200 | { | |
12201 | // This handles integers, floats and complex constants. | |
12202 | return Integer_expression::do_import(imp); | |
12203 | } | |
12204 | else if (imp->match_c_string("nil")) | |
12205 | return Nil_expression::do_import(imp); | |
12206 | else if (imp->match_c_string("convert")) | |
12207 | return Type_conversion_expression::do_import(imp); | |
12208 | else | |
12209 | { | |
12210 | error_at(imp->location(), "import error: expected expression"); | |
12211 | return Expression::make_error(imp->location()); | |
12212 | } | |
12213 | } | |
12214 | ||
12215 | // Class Expression_list. | |
12216 | ||
12217 | // Traverse the list. | |
12218 | ||
12219 | int | |
12220 | Expression_list::traverse(Traverse* traverse) | |
12221 | { | |
12222 | for (Expression_list::iterator p = this->begin(); | |
12223 | p != this->end(); | |
12224 | ++p) | |
12225 | { | |
12226 | if (*p != NULL) | |
12227 | { | |
12228 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
12229 | return TRAVERSE_EXIT; | |
12230 | } | |
12231 | } | |
12232 | return TRAVERSE_CONTINUE; | |
12233 | } | |
12234 | ||
12235 | // Copy the list. | |
12236 | ||
12237 | Expression_list* | |
12238 | Expression_list::copy() | |
12239 | { | |
12240 | Expression_list* ret = new Expression_list(); | |
12241 | for (Expression_list::iterator p = this->begin(); | |
12242 | p != this->end(); | |
12243 | ++p) | |
12244 | { | |
12245 | if (*p == NULL) | |
12246 | ret->push_back(NULL); | |
12247 | else | |
12248 | ret->push_back((*p)->copy()); | |
12249 | } | |
12250 | return ret; | |
12251 | } | |
12252 | ||
12253 | // Return whether an expression list has an error expression. | |
12254 | ||
12255 | bool | |
12256 | Expression_list::contains_error() const | |
12257 | { | |
12258 | for (Expression_list::const_iterator p = this->begin(); | |
12259 | p != this->end(); | |
12260 | ++p) | |
12261 | if (*p != NULL && (*p)->is_error_expression()) | |
12262 | return true; | |
12263 | return false; | |
12264 | } |