]>
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" | |
e440a328 | 24 | |
25 | #ifndef ENABLE_BUILD_WITH_CXX | |
26 | } | |
27 | #endif | |
28 | ||
29 | #include "go-c.h" | |
30 | #include "gogo.h" | |
31 | #include "types.h" | |
32 | #include "export.h" | |
33 | #include "import.h" | |
34 | #include "statements.h" | |
35 | #include "lex.h" | |
36 | #include "expressions.h" | |
37 | ||
38 | // Class Expression. | |
39 | ||
40 | Expression::Expression(Expression_classification classification, | |
41 | source_location location) | |
42 | : classification_(classification), location_(location) | |
43 | { | |
44 | } | |
45 | ||
46 | Expression::~Expression() | |
47 | { | |
48 | } | |
49 | ||
50 | // If this expression has a constant integer value, return it. | |
51 | ||
52 | bool | |
53 | Expression::integer_constant_value(bool iota_is_constant, mpz_t val, | |
54 | Type** ptype) const | |
55 | { | |
56 | *ptype = NULL; | |
57 | return this->do_integer_constant_value(iota_is_constant, val, ptype); | |
58 | } | |
59 | ||
60 | // If this expression has a constant floating point value, return it. | |
61 | ||
62 | bool | |
63 | Expression::float_constant_value(mpfr_t val, Type** ptype) const | |
64 | { | |
65 | *ptype = NULL; | |
66 | if (this->do_float_constant_value(val, ptype)) | |
67 | return true; | |
68 | mpz_t ival; | |
69 | mpz_init(ival); | |
70 | Type* t; | |
71 | bool ret; | |
72 | if (!this->do_integer_constant_value(false, ival, &t)) | |
73 | ret = false; | |
74 | else | |
75 | { | |
76 | mpfr_set_z(val, ival, GMP_RNDN); | |
77 | ret = true; | |
78 | } | |
79 | mpz_clear(ival); | |
80 | return ret; | |
81 | } | |
82 | ||
83 | // If this expression has a constant complex value, return it. | |
84 | ||
85 | bool | |
86 | Expression::complex_constant_value(mpfr_t real, mpfr_t imag, | |
87 | Type** ptype) const | |
88 | { | |
89 | *ptype = NULL; | |
90 | if (this->do_complex_constant_value(real, imag, ptype)) | |
91 | return true; | |
92 | Type *t; | |
93 | if (this->float_constant_value(real, &t)) | |
94 | { | |
95 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
96 | return true; | |
97 | } | |
98 | return false; | |
99 | } | |
100 | ||
101 | // Traverse the expressions. | |
102 | ||
103 | int | |
104 | Expression::traverse(Expression** pexpr, Traverse* traverse) | |
105 | { | |
106 | Expression* expr = *pexpr; | |
107 | if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0) | |
108 | { | |
109 | int t = traverse->expression(pexpr); | |
110 | if (t == TRAVERSE_EXIT) | |
111 | return TRAVERSE_EXIT; | |
112 | else if (t == TRAVERSE_SKIP_COMPONENTS) | |
113 | return TRAVERSE_CONTINUE; | |
114 | } | |
115 | return expr->do_traverse(traverse); | |
116 | } | |
117 | ||
118 | // Traverse subexpressions of this expression. | |
119 | ||
120 | int | |
121 | Expression::traverse_subexpressions(Traverse* traverse) | |
122 | { | |
123 | return this->do_traverse(traverse); | |
124 | } | |
125 | ||
126 | // Default implementation for do_traverse for child classes. | |
127 | ||
128 | int | |
129 | Expression::do_traverse(Traverse*) | |
130 | { | |
131 | return TRAVERSE_CONTINUE; | |
132 | } | |
133 | ||
134 | // This virtual function is called by the parser if the value of this | |
135 | // expression is being discarded. By default, we warn. Expressions | |
136 | // with side effects override. | |
137 | ||
138 | void | |
139 | Expression::do_discarding_value() | |
140 | { | |
141 | this->warn_about_unused_value(); | |
142 | } | |
143 | ||
144 | // This virtual function is called to export expressions. This will | |
145 | // only be used by expressions which may be constant. | |
146 | ||
147 | void | |
148 | Expression::do_export(Export*) const | |
149 | { | |
150 | gcc_unreachable(); | |
151 | } | |
152 | ||
153 | // Warn that the value of the expression is not used. | |
154 | ||
155 | void | |
156 | Expression::warn_about_unused_value() | |
157 | { | |
158 | warning_at(this->location(), OPT_Wunused_value, "value computed is not used"); | |
159 | } | |
160 | ||
161 | // Note that this expression is an error. This is called by children | |
162 | // when they discover an error. | |
163 | ||
164 | void | |
165 | Expression::set_is_error() | |
166 | { | |
167 | this->classification_ = EXPRESSION_ERROR; | |
168 | } | |
169 | ||
170 | // For children to call to report an error conveniently. | |
171 | ||
172 | void | |
173 | Expression::report_error(const char* msg) | |
174 | { | |
175 | error_at(this->location_, "%s", msg); | |
176 | this->set_is_error(); | |
177 | } | |
178 | ||
179 | // Set types of variables and constants. This is implemented by the | |
180 | // child class. | |
181 | ||
182 | void | |
183 | Expression::determine_type(const Type_context* context) | |
184 | { | |
185 | this->do_determine_type(context); | |
186 | } | |
187 | ||
188 | // Set types when there is no context. | |
189 | ||
190 | void | |
191 | Expression::determine_type_no_context() | |
192 | { | |
193 | Type_context context; | |
194 | this->do_determine_type(&context); | |
195 | } | |
196 | ||
197 | // Return a tree handling any conversions which must be done during | |
198 | // assignment. | |
199 | ||
200 | tree | |
201 | Expression::convert_for_assignment(Translate_context* context, Type* lhs_type, | |
202 | Type* rhs_type, tree rhs_tree, | |
203 | source_location location) | |
204 | { | |
205 | if (lhs_type == rhs_type) | |
206 | return rhs_tree; | |
207 | ||
208 | if (lhs_type->is_error_type() || rhs_type->is_error_type()) | |
209 | return error_mark_node; | |
210 | ||
211 | if (lhs_type->is_undefined() || rhs_type->is_undefined()) | |
212 | { | |
213 | // Make sure we report the error. | |
214 | lhs_type->base(); | |
215 | rhs_type->base(); | |
216 | return error_mark_node; | |
217 | } | |
218 | ||
219 | if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node) | |
220 | return error_mark_node; | |
221 | ||
222 | Gogo* gogo = context->gogo(); | |
223 | ||
224 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
225 | if (lhs_type_tree == error_mark_node) | |
226 | return error_mark_node; | |
227 | ||
228 | if (lhs_type->interface_type() != NULL) | |
229 | { | |
230 | if (rhs_type->interface_type() == NULL) | |
231 | return Expression::convert_type_to_interface(context, lhs_type, | |
232 | rhs_type, rhs_tree, | |
233 | location); | |
234 | else | |
235 | return Expression::convert_interface_to_interface(context, lhs_type, | |
236 | rhs_type, rhs_tree, | |
237 | false, location); | |
238 | } | |
239 | else if (rhs_type->interface_type() != NULL) | |
240 | return Expression::convert_interface_to_type(context, lhs_type, rhs_type, | |
241 | rhs_tree, location); | |
242 | else if (lhs_type->is_open_array_type() | |
243 | && rhs_type->is_nil_type()) | |
244 | { | |
245 | // Assigning nil to an open array. | |
246 | gcc_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE); | |
247 | ||
248 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
249 | ||
250 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
251 | tree field = TYPE_FIELDS(lhs_type_tree); | |
252 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
253 | "__values") == 0); | |
254 | elt->index = field; | |
255 | elt->value = fold_convert(TREE_TYPE(field), null_pointer_node); | |
256 | ||
257 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
258 | field = DECL_CHAIN(field); | |
259 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
260 | "__count") == 0); | |
261 | elt->index = field; | |
262 | elt->value = fold_convert(TREE_TYPE(field), integer_zero_node); | |
263 | ||
264 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
265 | field = DECL_CHAIN(field); | |
266 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
267 | "__capacity") == 0); | |
268 | elt->index = field; | |
269 | elt->value = fold_convert(TREE_TYPE(field), integer_zero_node); | |
270 | ||
271 | tree val = build_constructor(lhs_type_tree, init); | |
272 | TREE_CONSTANT(val) = 1; | |
273 | ||
274 | return val; | |
275 | } | |
276 | else if (rhs_type->is_nil_type()) | |
277 | { | |
278 | // The left hand side should be a pointer type at the tree | |
279 | // level. | |
280 | gcc_assert(POINTER_TYPE_P(lhs_type_tree)); | |
281 | return fold_convert(lhs_type_tree, null_pointer_node); | |
282 | } | |
283 | else if (lhs_type_tree == TREE_TYPE(rhs_tree)) | |
284 | { | |
285 | // No conversion is needed. | |
286 | return rhs_tree; | |
287 | } | |
288 | else if (POINTER_TYPE_P(lhs_type_tree) | |
289 | || INTEGRAL_TYPE_P(lhs_type_tree) | |
290 | || SCALAR_FLOAT_TYPE_P(lhs_type_tree) | |
291 | || COMPLEX_FLOAT_TYPE_P(lhs_type_tree)) | |
292 | return fold_convert_loc(location, lhs_type_tree, rhs_tree); | |
293 | else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE | |
294 | && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE) | |
295 | { | |
296 | // This conversion must be permitted by Go, or we wouldn't have | |
297 | // gotten here. | |
298 | gcc_assert(int_size_in_bytes(lhs_type_tree) | |
299 | == int_size_in_bytes(TREE_TYPE(rhs_tree))); | |
300 | return fold_build1_loc(location, VIEW_CONVERT_EXPR, lhs_type_tree, | |
301 | rhs_tree); | |
302 | } | |
303 | else | |
304 | { | |
305 | gcc_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree))); | |
306 | return rhs_tree; | |
307 | } | |
308 | } | |
309 | ||
310 | // Return a tree for a conversion from a non-interface type to an | |
311 | // interface type. | |
312 | ||
313 | tree | |
314 | Expression::convert_type_to_interface(Translate_context* context, | |
315 | Type* lhs_type, Type* rhs_type, | |
316 | tree rhs_tree, source_location location) | |
317 | { | |
318 | Gogo* gogo = context->gogo(); | |
319 | Interface_type* lhs_interface_type = lhs_type->interface_type(); | |
320 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
321 | ||
322 | // Since RHS_TYPE is a static type, we can create the interface | |
323 | // method table at compile time. | |
324 | ||
325 | // When setting an interface to nil, we just set both fields to | |
326 | // NULL. | |
327 | if (rhs_type->is_nil_type()) | |
328 | return lhs_type->get_init_tree(gogo, false); | |
329 | ||
330 | // This should have been checked already. | |
331 | gcc_assert(lhs_interface_type->implements_interface(rhs_type, NULL)); | |
332 | ||
333 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
334 | if (lhs_type_tree == error_mark_node) | |
335 | return error_mark_node; | |
336 | ||
337 | // An interface is a tuple. If LHS_TYPE is an empty interface type, | |
338 | // then the first field is the type descriptor for RHS_TYPE. | |
339 | // Otherwise it is the interface method table for RHS_TYPE. | |
340 | tree first_field_value; | |
341 | if (lhs_is_empty) | |
342 | first_field_value = rhs_type->type_descriptor_pointer(gogo); | |
343 | else | |
344 | { | |
345 | // Build the interface method table for this interface and this | |
346 | // object type: a list of function pointers for each interface | |
347 | // method. | |
348 | Named_type* rhs_named_type = rhs_type->named_type(); | |
349 | bool is_pointer = false; | |
350 | if (rhs_named_type == NULL) | |
351 | { | |
352 | rhs_named_type = rhs_type->deref()->named_type(); | |
353 | is_pointer = true; | |
354 | } | |
355 | tree method_table; | |
356 | if (rhs_named_type == NULL) | |
357 | method_table = null_pointer_node; | |
358 | else | |
359 | method_table = | |
360 | rhs_named_type->interface_method_table(gogo, lhs_interface_type, | |
361 | is_pointer); | |
362 | first_field_value = fold_convert_loc(location, const_ptr_type_node, | |
363 | method_table); | |
364 | } | |
84b7d3c6 | 365 | if (first_field_value == error_mark_node) |
366 | return error_mark_node; | |
e440a328 | 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); | |
5fb82b5e | 510 | if (call == error_mark_node) |
511 | return error_mark_node; | |
e440a328 | 512 | // This will panic if the interface conversion fails. |
513 | TREE_NOTHROW(assert_interface_decl) = 0; | |
514 | elt->value = fold_convert_loc(location, TREE_TYPE(field), call); | |
515 | } | |
516 | else if (lhs_is_empty) | |
517 | { | |
518 | // A convertion to an empty interface always succeeds, and the | |
519 | // first field is just the type descriptor of the object. | |
520 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
521 | "__type_descriptor") == 0); | |
522 | gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor)); | |
523 | elt->value = rhs_type_descriptor; | |
524 | } | |
525 | else | |
526 | { | |
527 | // A conversion to a non-empty interface may fail, but unlike a | |
528 | // type assertion converting nil will always succeed. | |
529 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") | |
530 | == 0); | |
531 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo); | |
532 | static tree convert_interface_decl; | |
533 | tree call = Gogo::call_builtin(&convert_interface_decl, | |
534 | location, | |
535 | "__go_convert_interface", | |
536 | 2, | |
537 | ptr_type_node, | |
538 | TREE_TYPE(lhs_type_descriptor), | |
539 | lhs_type_descriptor, | |
540 | TREE_TYPE(rhs_type_descriptor), | |
541 | rhs_type_descriptor); | |
5fb82b5e | 542 | if (call == error_mark_node) |
543 | return error_mark_node; | |
e440a328 | 544 | // This will panic if the interface conversion fails. |
545 | TREE_NOTHROW(convert_interface_decl) = 0; | |
546 | elt->value = fold_convert_loc(location, TREE_TYPE(field), call); | |
547 | } | |
548 | ||
549 | // The second field is simply the object pointer. | |
550 | ||
551 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
552 | field = DECL_CHAIN(field); | |
553 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); | |
554 | elt->index = field; | |
555 | ||
556 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
557 | gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); | |
558 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); | |
559 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); | |
560 | elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
561 | NULL_TREE); | |
562 | ||
563 | return build_constructor(lhs_type_tree, init); | |
564 | } | |
565 | ||
566 | // Return a tree for the conversion of an interface type to a | |
567 | // non-interface type. | |
568 | ||
569 | tree | |
570 | Expression::convert_interface_to_type(Translate_context* context, | |
571 | Type *lhs_type, Type* rhs_type, | |
572 | tree rhs_tree, source_location location) | |
573 | { | |
574 | Gogo* gogo = context->gogo(); | |
575 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
576 | ||
577 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
578 | if (lhs_type_tree == error_mark_node) | |
579 | return error_mark_node; | |
580 | ||
581 | // Call a function to check that the type is valid. The function | |
582 | // will panic with an appropriate runtime type error if the type is | |
583 | // not valid. | |
584 | ||
585 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo); | |
586 | ||
587 | if (!DECL_P(rhs_tree)) | |
588 | rhs_tree = save_expr(rhs_tree); | |
589 | ||
590 | tree rhs_type_descriptor = | |
591 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
592 | location); | |
593 | ||
594 | tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo); | |
595 | ||
596 | static tree check_interface_type_decl; | |
597 | tree call = Gogo::call_builtin(&check_interface_type_decl, | |
598 | location, | |
599 | "__go_check_interface_type", | |
600 | 3, | |
601 | void_type_node, | |
602 | TREE_TYPE(lhs_type_descriptor), | |
603 | lhs_type_descriptor, | |
604 | TREE_TYPE(rhs_type_descriptor), | |
605 | rhs_type_descriptor, | |
606 | TREE_TYPE(rhs_inter_descriptor), | |
607 | rhs_inter_descriptor); | |
5fb82b5e | 608 | if (call == error_mark_node) |
609 | return error_mark_node; | |
e440a328 | 610 | // This call will panic if the conversion is invalid. |
611 | TREE_NOTHROW(check_interface_type_decl) = 0; | |
612 | ||
613 | // If the call succeeds, pull out the value. | |
614 | gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); | |
615 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); | |
616 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); | |
617 | tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
618 | NULL_TREE); | |
619 | ||
620 | // If the value is a pointer, then it is the value we want. | |
621 | // Otherwise it points to the value. | |
622 | if (lhs_type->points_to() == NULL) | |
623 | { | |
624 | val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val); | |
625 | val = build_fold_indirect_ref_loc(location, val); | |
626 | } | |
627 | ||
628 | return build2(COMPOUND_EXPR, lhs_type_tree, call, | |
629 | fold_convert_loc(location, lhs_type_tree, val)); | |
630 | } | |
631 | ||
632 | // Convert an expression to a tree. This is implemented by the child | |
633 | // class. Not that it is not in general safe to call this multiple | |
634 | // times for a single expression, but that we don't catch such errors. | |
635 | ||
636 | tree | |
637 | Expression::get_tree(Translate_context* context) | |
638 | { | |
639 | // The child may have marked this expression as having an error. | |
640 | if (this->classification_ == EXPRESSION_ERROR) | |
641 | return error_mark_node; | |
642 | ||
643 | return this->do_get_tree(context); | |
644 | } | |
645 | ||
646 | // Return a tree for VAL in TYPE. | |
647 | ||
648 | tree | |
649 | Expression::integer_constant_tree(mpz_t val, tree type) | |
650 | { | |
651 | if (type == error_mark_node) | |
652 | return error_mark_node; | |
653 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
654 | return double_int_to_tree(type, | |
655 | mpz_get_double_int(type, val, true)); | |
656 | else if (TREE_CODE(type) == REAL_TYPE) | |
657 | { | |
658 | mpfr_t fval; | |
659 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
660 | tree ret = Expression::float_constant_tree(fval, type); | |
661 | mpfr_clear(fval); | |
662 | return ret; | |
663 | } | |
664 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
665 | { | |
666 | mpfr_t fval; | |
667 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
668 | tree real = Expression::float_constant_tree(fval, TREE_TYPE(type)); | |
669 | mpfr_clear(fval); | |
670 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
671 | integer_zero_node); | |
672 | return build_complex(type, real, imag); | |
673 | } | |
674 | else | |
675 | gcc_unreachable(); | |
676 | } | |
677 | ||
678 | // Return a tree for VAL in TYPE. | |
679 | ||
680 | tree | |
681 | Expression::float_constant_tree(mpfr_t val, tree type) | |
682 | { | |
683 | if (type == error_mark_node) | |
684 | return error_mark_node; | |
685 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
686 | { | |
687 | mpz_t ival; | |
688 | mpz_init(ival); | |
689 | mpfr_get_z(ival, val, GMP_RNDN); | |
690 | tree ret = Expression::integer_constant_tree(ival, type); | |
691 | mpz_clear(ival); | |
692 | return ret; | |
693 | } | |
694 | else if (TREE_CODE(type) == REAL_TYPE) | |
695 | { | |
696 | REAL_VALUE_TYPE r1; | |
697 | real_from_mpfr(&r1, val, type, GMP_RNDN); | |
698 | REAL_VALUE_TYPE r2; | |
699 | real_convert(&r2, TYPE_MODE(type), &r1); | |
700 | return build_real(type, r2); | |
701 | } | |
702 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
703 | { | |
704 | REAL_VALUE_TYPE r1; | |
705 | real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN); | |
706 | REAL_VALUE_TYPE r2; | |
707 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
708 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
709 | integer_zero_node); | |
710 | return build_complex(type, build_real(TREE_TYPE(type), r2), imag); | |
711 | } | |
712 | else | |
713 | gcc_unreachable(); | |
714 | } | |
715 | ||
716 | // Return a tree for REAL/IMAG in TYPE. | |
717 | ||
718 | tree | |
719 | Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type) | |
720 | { | |
f690b0bb | 721 | if (type == error_mark_node) |
722 | return error_mark_node; | |
723 | else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE) | |
724 | return Expression::float_constant_tree(real, type); | |
725 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
e440a328 | 726 | { |
727 | REAL_VALUE_TYPE r1; | |
728 | real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN); | |
729 | REAL_VALUE_TYPE r2; | |
730 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
731 | ||
732 | REAL_VALUE_TYPE r3; | |
733 | real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN); | |
734 | REAL_VALUE_TYPE r4; | |
735 | real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3); | |
736 | ||
737 | return build_complex(type, build_real(TREE_TYPE(type), r2), | |
738 | build_real(TREE_TYPE(type), r4)); | |
739 | } | |
740 | else | |
741 | gcc_unreachable(); | |
742 | } | |
743 | ||
744 | // Return a tree which evaluates to true if VAL, of arbitrary integer | |
745 | // type, is negative or is more than the maximum value of BOUND_TYPE. | |
746 | // If SOFAR is not NULL, it is or'red into the result. The return | |
747 | // value may be NULL if SOFAR is NULL. | |
748 | ||
749 | tree | |
750 | Expression::check_bounds(tree val, tree bound_type, tree sofar, | |
751 | source_location loc) | |
752 | { | |
753 | tree val_type = TREE_TYPE(val); | |
754 | tree ret = NULL_TREE; | |
755 | ||
756 | if (!TYPE_UNSIGNED(val_type)) | |
757 | { | |
758 | ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val, | |
759 | build_int_cst(val_type, 0)); | |
760 | if (ret == boolean_false_node) | |
761 | ret = NULL_TREE; | |
762 | } | |
763 | ||
764 | if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type)) | |
765 | || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type)) | |
766 | { | |
767 | tree max = TYPE_MAX_VALUE(bound_type); | |
768 | tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val, | |
769 | fold_convert_loc(loc, val_type, max)); | |
770 | if (big == boolean_false_node) | |
771 | ; | |
772 | else if (ret == NULL_TREE) | |
773 | ret = big; | |
774 | else | |
775 | ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
776 | ret, big); | |
777 | } | |
778 | ||
779 | if (ret == NULL_TREE) | |
780 | return sofar; | |
781 | else if (sofar == NULL_TREE) | |
782 | return ret; | |
783 | else | |
784 | return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
785 | sofar, ret); | |
786 | } | |
787 | ||
788 | // Error expressions. This are used to avoid cascading errors. | |
789 | ||
790 | class Error_expression : public Expression | |
791 | { | |
792 | public: | |
793 | Error_expression(source_location location) | |
794 | : Expression(EXPRESSION_ERROR, location) | |
795 | { } | |
796 | ||
797 | protected: | |
798 | bool | |
799 | do_is_constant() const | |
800 | { return true; } | |
801 | ||
802 | bool | |
803 | do_integer_constant_value(bool, mpz_t val, Type**) const | |
804 | { | |
805 | mpz_set_ui(val, 0); | |
806 | return true; | |
807 | } | |
808 | ||
809 | bool | |
810 | do_float_constant_value(mpfr_t val, Type**) const | |
811 | { | |
812 | mpfr_set_ui(val, 0, GMP_RNDN); | |
813 | return true; | |
814 | } | |
815 | ||
816 | bool | |
817 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const | |
818 | { | |
819 | mpfr_set_ui(real, 0, GMP_RNDN); | |
820 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
821 | return true; | |
822 | } | |
823 | ||
824 | void | |
825 | do_discarding_value() | |
826 | { } | |
827 | ||
828 | Type* | |
829 | do_type() | |
830 | { return Type::make_error_type(); } | |
831 | ||
832 | void | |
833 | do_determine_type(const Type_context*) | |
834 | { } | |
835 | ||
836 | Expression* | |
837 | do_copy() | |
838 | { return this; } | |
839 | ||
840 | bool | |
841 | do_is_addressable() const | |
842 | { return true; } | |
843 | ||
844 | tree | |
845 | do_get_tree(Translate_context*) | |
846 | { return error_mark_node; } | |
847 | }; | |
848 | ||
849 | Expression* | |
850 | Expression::make_error(source_location location) | |
851 | { | |
852 | return new Error_expression(location); | |
853 | } | |
854 | ||
855 | // An expression which is really a type. This is used during parsing. | |
856 | // It is an error if these survive after lowering. | |
857 | ||
858 | class | |
859 | Type_expression : public Expression | |
860 | { | |
861 | public: | |
862 | Type_expression(Type* type, source_location location) | |
863 | : Expression(EXPRESSION_TYPE, location), | |
864 | type_(type) | |
865 | { } | |
866 | ||
867 | protected: | |
868 | int | |
869 | do_traverse(Traverse* traverse) | |
870 | { return Type::traverse(this->type_, traverse); } | |
871 | ||
872 | Type* | |
873 | do_type() | |
874 | { return this->type_; } | |
875 | ||
876 | void | |
877 | do_determine_type(const Type_context*) | |
878 | { } | |
879 | ||
880 | void | |
881 | do_check_types(Gogo*) | |
882 | { this->report_error(_("invalid use of type")); } | |
883 | ||
884 | Expression* | |
885 | do_copy() | |
886 | { return this; } | |
887 | ||
888 | tree | |
889 | do_get_tree(Translate_context*) | |
890 | { gcc_unreachable(); } | |
891 | ||
892 | private: | |
893 | // The type which we are representing as an expression. | |
894 | Type* type_; | |
895 | }; | |
896 | ||
897 | Expression* | |
898 | Expression::make_type(Type* type, source_location location) | |
899 | { | |
900 | return new Type_expression(type, location); | |
901 | } | |
902 | ||
e03bdf36 | 903 | // Class Parser_expression. |
904 | ||
905 | Type* | |
906 | Parser_expression::do_type() | |
907 | { | |
908 | // We should never really ask for the type of a Parser_expression. | |
909 | // However, it can happen, at least when we have an invalid const | |
910 | // whose initializer refers to the const itself. In that case we | |
911 | // may ask for the type when lowering the const itself. | |
912 | gcc_assert(saw_errors()); | |
913 | return Type::make_error_type(); | |
914 | } | |
915 | ||
e440a328 | 916 | // Class Var_expression. |
917 | ||
918 | // Lower a variable expression. Here we just make sure that the | |
919 | // initialization expression of the variable has been lowered. This | |
920 | // ensures that we will be able to determine the type of the variable | |
921 | // if necessary. | |
922 | ||
923 | Expression* | |
924 | Var_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
925 | { | |
926 | if (this->variable_->is_variable()) | |
927 | { | |
928 | Variable* var = this->variable_->var_value(); | |
929 | // This is either a local variable or a global variable. A | |
930 | // reference to a variable which is local to an enclosing | |
931 | // function will be a reference to a field in a closure. | |
932 | if (var->is_global()) | |
933 | function = NULL; | |
934 | var->lower_init_expression(gogo, function); | |
935 | } | |
936 | return this; | |
937 | } | |
938 | ||
939 | // Return the name of the variable. | |
940 | ||
941 | const std::string& | |
942 | Var_expression::name() const | |
943 | { | |
944 | return this->variable_->name(); | |
945 | } | |
946 | ||
947 | // Return the type of a reference to a variable. | |
948 | ||
949 | Type* | |
950 | Var_expression::do_type() | |
951 | { | |
952 | if (this->variable_->is_variable()) | |
953 | return this->variable_->var_value()->type(); | |
954 | else if (this->variable_->is_result_variable()) | |
955 | return this->variable_->result_var_value()->type(); | |
956 | else | |
957 | gcc_unreachable(); | |
958 | } | |
959 | ||
960 | // Something takes the address of this variable. This means that we | |
961 | // may want to move the variable onto the heap. | |
962 | ||
963 | void | |
964 | Var_expression::do_address_taken(bool escapes) | |
965 | { | |
966 | if (!escapes) | |
967 | ; | |
968 | else if (this->variable_->is_variable()) | |
969 | this->variable_->var_value()->set_address_taken(); | |
970 | else if (this->variable_->is_result_variable()) | |
971 | this->variable_->result_var_value()->set_address_taken(); | |
972 | else | |
973 | gcc_unreachable(); | |
974 | } | |
975 | ||
976 | // Get the tree for a reference to a variable. | |
977 | ||
978 | tree | |
979 | Var_expression::do_get_tree(Translate_context* context) | |
980 | { | |
981 | return this->variable_->get_tree(context->gogo(), context->function()); | |
982 | } | |
983 | ||
984 | // Make a reference to a variable in an expression. | |
985 | ||
986 | Expression* | |
987 | Expression::make_var_reference(Named_object* var, source_location location) | |
988 | { | |
989 | if (var->is_sink()) | |
990 | return Expression::make_sink(location); | |
991 | ||
992 | // FIXME: Creating a new object for each reference to a variable is | |
993 | // wasteful. | |
994 | return new Var_expression(var, location); | |
995 | } | |
996 | ||
997 | // Class Temporary_reference_expression. | |
998 | ||
999 | // The type. | |
1000 | ||
1001 | Type* | |
1002 | Temporary_reference_expression::do_type() | |
1003 | { | |
1004 | return this->statement_->type(); | |
1005 | } | |
1006 | ||
1007 | // Called if something takes the address of this temporary variable. | |
1008 | // We never have to move temporary variables to the heap, but we do | |
1009 | // need to know that they must live in the stack rather than in a | |
1010 | // register. | |
1011 | ||
1012 | void | |
1013 | Temporary_reference_expression::do_address_taken(bool) | |
1014 | { | |
1015 | this->statement_->set_is_address_taken(); | |
1016 | } | |
1017 | ||
1018 | // Get a tree referring to the variable. | |
1019 | ||
1020 | tree | |
1021 | Temporary_reference_expression::do_get_tree(Translate_context*) | |
1022 | { | |
1023 | return this->statement_->get_decl(); | |
1024 | } | |
1025 | ||
1026 | // Make a reference to a temporary variable. | |
1027 | ||
1028 | Expression* | |
1029 | Expression::make_temporary_reference(Temporary_statement* statement, | |
1030 | source_location location) | |
1031 | { | |
1032 | return new Temporary_reference_expression(statement, location); | |
1033 | } | |
1034 | ||
1035 | // A sink expression--a use of the blank identifier _. | |
1036 | ||
1037 | class Sink_expression : public Expression | |
1038 | { | |
1039 | public: | |
1040 | Sink_expression(source_location location) | |
1041 | : Expression(EXPRESSION_SINK, location), | |
1042 | type_(NULL), var_(NULL_TREE) | |
1043 | { } | |
1044 | ||
1045 | protected: | |
1046 | void | |
1047 | do_discarding_value() | |
1048 | { } | |
1049 | ||
1050 | Type* | |
1051 | do_type(); | |
1052 | ||
1053 | void | |
1054 | do_determine_type(const Type_context*); | |
1055 | ||
1056 | Expression* | |
1057 | do_copy() | |
1058 | { return new Sink_expression(this->location()); } | |
1059 | ||
1060 | tree | |
1061 | do_get_tree(Translate_context*); | |
1062 | ||
1063 | private: | |
1064 | // The type of this sink variable. | |
1065 | Type* type_; | |
1066 | // The temporary variable we generate. | |
1067 | tree var_; | |
1068 | }; | |
1069 | ||
1070 | // Return the type of a sink expression. | |
1071 | ||
1072 | Type* | |
1073 | Sink_expression::do_type() | |
1074 | { | |
1075 | if (this->type_ == NULL) | |
1076 | return Type::make_sink_type(); | |
1077 | return this->type_; | |
1078 | } | |
1079 | ||
1080 | // Determine the type of a sink expression. | |
1081 | ||
1082 | void | |
1083 | Sink_expression::do_determine_type(const Type_context* context) | |
1084 | { | |
1085 | if (context->type != NULL) | |
1086 | this->type_ = context->type; | |
1087 | } | |
1088 | ||
1089 | // Return a temporary variable for a sink expression. This will | |
1090 | // presumably be a write-only variable which the middle-end will drop. | |
1091 | ||
1092 | tree | |
1093 | Sink_expression::do_get_tree(Translate_context* context) | |
1094 | { | |
1095 | if (this->var_ == NULL_TREE) | |
1096 | { | |
1097 | gcc_assert(this->type_ != NULL && !this->type_->is_sink_type()); | |
1098 | this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()), | |
1099 | "blank"); | |
1100 | } | |
1101 | return this->var_; | |
1102 | } | |
1103 | ||
1104 | // Make a sink expression. | |
1105 | ||
1106 | Expression* | |
1107 | Expression::make_sink(source_location location) | |
1108 | { | |
1109 | return new Sink_expression(location); | |
1110 | } | |
1111 | ||
1112 | // Class Func_expression. | |
1113 | ||
1114 | // FIXME: Can a function expression appear in a constant expression? | |
1115 | // The value is unchanging. Initializing a constant to the address of | |
1116 | // a function seems like it could work, though there might be little | |
1117 | // point to it. | |
1118 | ||
1119 | // Return the name of the function. | |
1120 | ||
1121 | const std::string& | |
1122 | Func_expression::name() const | |
1123 | { | |
1124 | return this->function_->name(); | |
1125 | } | |
1126 | ||
1127 | // Traversal. | |
1128 | ||
1129 | int | |
1130 | Func_expression::do_traverse(Traverse* traverse) | |
1131 | { | |
1132 | return (this->closure_ == NULL | |
1133 | ? TRAVERSE_CONTINUE | |
1134 | : Expression::traverse(&this->closure_, traverse)); | |
1135 | } | |
1136 | ||
1137 | // Return the type of a function expression. | |
1138 | ||
1139 | Type* | |
1140 | Func_expression::do_type() | |
1141 | { | |
1142 | if (this->function_->is_function()) | |
1143 | return this->function_->func_value()->type(); | |
1144 | else if (this->function_->is_function_declaration()) | |
1145 | return this->function_->func_declaration_value()->type(); | |
1146 | else | |
1147 | gcc_unreachable(); | |
1148 | } | |
1149 | ||
1150 | // Get the tree for a function expression without evaluating the | |
1151 | // closure. | |
1152 | ||
1153 | tree | |
1154 | Func_expression::get_tree_without_closure(Gogo* gogo) | |
1155 | { | |
1156 | Function_type* fntype; | |
1157 | if (this->function_->is_function()) | |
1158 | fntype = this->function_->func_value()->type(); | |
1159 | else if (this->function_->is_function_declaration()) | |
1160 | fntype = this->function_->func_declaration_value()->type(); | |
1161 | else | |
1162 | gcc_unreachable(); | |
1163 | ||
1164 | // Builtin functions are handled specially by Call_expression. We | |
1165 | // can't take their address. | |
1166 | if (fntype->is_builtin()) | |
1167 | { | |
1168 | error_at(this->location(), "invalid use of special builtin function %qs", | |
1169 | this->function_->name().c_str()); | |
1170 | return error_mark_node; | |
1171 | } | |
1172 | ||
1173 | Named_object* no = this->function_; | |
9d6f3721 | 1174 | |
1175 | tree id = no->get_id(gogo); | |
1176 | if (id == error_mark_node) | |
1177 | return error_mark_node; | |
1178 | ||
e440a328 | 1179 | tree fndecl; |
1180 | if (no->is_function()) | |
1181 | fndecl = no->func_value()->get_or_make_decl(gogo, no, id); | |
1182 | else if (no->is_function_declaration()) | |
1183 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id); | |
1184 | else | |
1185 | gcc_unreachable(); | |
1186 | ||
9d6f3721 | 1187 | if (fndecl == error_mark_node) |
1188 | return error_mark_node; | |
1189 | ||
e440a328 | 1190 | return build_fold_addr_expr_loc(this->location(), fndecl); |
1191 | } | |
1192 | ||
1193 | // Get the tree for a function expression. This is used when we take | |
1194 | // the address of a function rather than simply calling it. If the | |
1195 | // function has a closure, we must use a trampoline. | |
1196 | ||
1197 | tree | |
1198 | Func_expression::do_get_tree(Translate_context* context) | |
1199 | { | |
1200 | Gogo* gogo = context->gogo(); | |
1201 | ||
1202 | tree fnaddr = this->get_tree_without_closure(gogo); | |
1203 | if (fnaddr == error_mark_node) | |
1204 | return error_mark_node; | |
1205 | ||
1206 | gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR | |
1207 | && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL); | |
1208 | TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1; | |
1209 | ||
1210 | // For a normal non-nested function call, that is all we have to do. | |
1211 | if (!this->function_->is_function() | |
1212 | || this->function_->func_value()->enclosing() == NULL) | |
1213 | { | |
1214 | gcc_assert(this->closure_ == NULL); | |
1215 | return fnaddr; | |
1216 | } | |
1217 | ||
1218 | // For a nested function call, we have to always allocate a | |
1219 | // trampoline. If we don't always allocate, then closures will not | |
1220 | // be reliably distinct. | |
1221 | Expression* closure = this->closure_; | |
1222 | tree closure_tree; | |
1223 | if (closure == NULL) | |
1224 | closure_tree = null_pointer_node; | |
1225 | else | |
1226 | { | |
1227 | // Get the value of the closure. This will be a pointer to | |
1228 | // space allocated on the heap. | |
1229 | closure_tree = closure->get_tree(context); | |
1230 | if (closure_tree == error_mark_node) | |
1231 | return error_mark_node; | |
1232 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree))); | |
1233 | } | |
1234 | ||
1235 | // Now we need to build some code on the heap. This code will load | |
1236 | // the static chain pointer with the closure and then jump to the | |
1237 | // body of the function. The normal gcc approach is to build the | |
1238 | // code on the stack. Unfortunately we can not do that, as Go | |
1239 | // permits us to return the function pointer. | |
1240 | ||
1241 | return gogo->make_trampoline(fnaddr, closure_tree, this->location()); | |
1242 | } | |
1243 | ||
1244 | // Make a reference to a function in an expression. | |
1245 | ||
1246 | Expression* | |
1247 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
1248 | source_location location) | |
1249 | { | |
1250 | return new Func_expression(function, closure, location); | |
1251 | } | |
1252 | ||
1253 | // Class Unknown_expression. | |
1254 | ||
1255 | // Return the name of an unknown expression. | |
1256 | ||
1257 | const std::string& | |
1258 | Unknown_expression::name() const | |
1259 | { | |
1260 | return this->named_object_->name(); | |
1261 | } | |
1262 | ||
1263 | // Lower a reference to an unknown name. | |
1264 | ||
1265 | Expression* | |
1266 | Unknown_expression::do_lower(Gogo*, Named_object*, int) | |
1267 | { | |
1268 | source_location location = this->location(); | |
1269 | Named_object* no = this->named_object_; | |
deded542 | 1270 | Named_object* real; |
1271 | if (!no->is_unknown()) | |
1272 | real = no; | |
1273 | else | |
e440a328 | 1274 | { |
deded542 | 1275 | real = no->unknown_value()->real_named_object(); |
1276 | if (real == NULL) | |
1277 | { | |
1278 | if (this->is_composite_literal_key_) | |
1279 | return this; | |
1280 | error_at(location, "reference to undefined name %qs", | |
1281 | this->named_object_->message_name().c_str()); | |
1282 | return Expression::make_error(location); | |
1283 | } | |
e440a328 | 1284 | } |
1285 | switch (real->classification()) | |
1286 | { | |
1287 | case Named_object::NAMED_OBJECT_CONST: | |
1288 | return Expression::make_const_reference(real, location); | |
1289 | case Named_object::NAMED_OBJECT_TYPE: | |
1290 | return Expression::make_type(real->type_value(), location); | |
1291 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1292 | if (this->is_composite_literal_key_) | |
1293 | return this; | |
1294 | error_at(location, "reference to undefined type %qs", | |
1295 | real->message_name().c_str()); | |
1296 | return Expression::make_error(location); | |
1297 | case Named_object::NAMED_OBJECT_VAR: | |
1298 | return Expression::make_var_reference(real, location); | |
1299 | case Named_object::NAMED_OBJECT_FUNC: | |
1300 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1301 | return Expression::make_func_reference(real, NULL, location); | |
1302 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1303 | if (this->is_composite_literal_key_) | |
1304 | return this; | |
1305 | error_at(location, "unexpected reference to package"); | |
1306 | return Expression::make_error(location); | |
1307 | default: | |
1308 | gcc_unreachable(); | |
1309 | } | |
1310 | } | |
1311 | ||
1312 | // Make a reference to an unknown name. | |
1313 | ||
1314 | Expression* | |
1315 | Expression::make_unknown_reference(Named_object* no, source_location location) | |
1316 | { | |
1317 | gcc_assert(no->resolve()->is_unknown()); | |
1318 | return new Unknown_expression(no, location); | |
1319 | } | |
1320 | ||
1321 | // A boolean expression. | |
1322 | ||
1323 | class Boolean_expression : public Expression | |
1324 | { | |
1325 | public: | |
1326 | Boolean_expression(bool val, source_location location) | |
1327 | : Expression(EXPRESSION_BOOLEAN, location), | |
1328 | val_(val), type_(NULL) | |
1329 | { } | |
1330 | ||
1331 | static Expression* | |
1332 | do_import(Import*); | |
1333 | ||
1334 | protected: | |
1335 | bool | |
1336 | do_is_constant() const | |
1337 | { return true; } | |
1338 | ||
1339 | Type* | |
1340 | do_type(); | |
1341 | ||
1342 | void | |
1343 | do_determine_type(const Type_context*); | |
1344 | ||
1345 | Expression* | |
1346 | do_copy() | |
1347 | { return this; } | |
1348 | ||
1349 | tree | |
1350 | do_get_tree(Translate_context*) | |
1351 | { return this->val_ ? boolean_true_node : boolean_false_node; } | |
1352 | ||
1353 | void | |
1354 | do_export(Export* exp) const | |
1355 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1356 | ||
1357 | private: | |
1358 | // The constant. | |
1359 | bool val_; | |
1360 | // The type as determined by context. | |
1361 | Type* type_; | |
1362 | }; | |
1363 | ||
1364 | // Get the type. | |
1365 | ||
1366 | Type* | |
1367 | Boolean_expression::do_type() | |
1368 | { | |
1369 | if (this->type_ == NULL) | |
1370 | this->type_ = Type::make_boolean_type(); | |
1371 | return this->type_; | |
1372 | } | |
1373 | ||
1374 | // Set the type from the context. | |
1375 | ||
1376 | void | |
1377 | Boolean_expression::do_determine_type(const Type_context* context) | |
1378 | { | |
1379 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1380 | ; | |
1381 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1382 | this->type_ = context->type; | |
1383 | else if (!context->may_be_abstract) | |
1384 | this->type_ = Type::lookup_bool_type(); | |
1385 | } | |
1386 | ||
1387 | // Import a boolean constant. | |
1388 | ||
1389 | Expression* | |
1390 | Boolean_expression::do_import(Import* imp) | |
1391 | { | |
1392 | if (imp->peek_char() == 't') | |
1393 | { | |
1394 | imp->require_c_string("true"); | |
1395 | return Expression::make_boolean(true, imp->location()); | |
1396 | } | |
1397 | else | |
1398 | { | |
1399 | imp->require_c_string("false"); | |
1400 | return Expression::make_boolean(false, imp->location()); | |
1401 | } | |
1402 | } | |
1403 | ||
1404 | // Make a boolean expression. | |
1405 | ||
1406 | Expression* | |
1407 | Expression::make_boolean(bool val, source_location location) | |
1408 | { | |
1409 | return new Boolean_expression(val, location); | |
1410 | } | |
1411 | ||
1412 | // Class String_expression. | |
1413 | ||
1414 | // Get the type. | |
1415 | ||
1416 | Type* | |
1417 | String_expression::do_type() | |
1418 | { | |
1419 | if (this->type_ == NULL) | |
1420 | this->type_ = Type::make_string_type(); | |
1421 | return this->type_; | |
1422 | } | |
1423 | ||
1424 | // Set the type from the context. | |
1425 | ||
1426 | void | |
1427 | String_expression::do_determine_type(const Type_context* context) | |
1428 | { | |
1429 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1430 | ; | |
1431 | else if (context->type != NULL && context->type->is_string_type()) | |
1432 | this->type_ = context->type; | |
1433 | else if (!context->may_be_abstract) | |
1434 | this->type_ = Type::lookup_string_type(); | |
1435 | } | |
1436 | ||
1437 | // Build a string constant. | |
1438 | ||
1439 | tree | |
1440 | String_expression::do_get_tree(Translate_context* context) | |
1441 | { | |
1442 | return context->gogo()->go_string_constant_tree(this->val_); | |
1443 | } | |
1444 | ||
1445 | // Export a string expression. | |
1446 | ||
1447 | void | |
1448 | String_expression::do_export(Export* exp) const | |
1449 | { | |
1450 | std::string s; | |
1451 | s.reserve(this->val_.length() * 4 + 2); | |
1452 | s += '"'; | |
1453 | for (std::string::const_iterator p = this->val_.begin(); | |
1454 | p != this->val_.end(); | |
1455 | ++p) | |
1456 | { | |
1457 | if (*p == '\\' || *p == '"') | |
1458 | { | |
1459 | s += '\\'; | |
1460 | s += *p; | |
1461 | } | |
1462 | else if (*p >= 0x20 && *p < 0x7f) | |
1463 | s += *p; | |
1464 | else if (*p == '\n') | |
1465 | s += "\\n"; | |
1466 | else if (*p == '\t') | |
1467 | s += "\\t"; | |
1468 | else | |
1469 | { | |
1470 | s += "\\x"; | |
1471 | unsigned char c = *p; | |
1472 | unsigned int dig = c >> 4; | |
1473 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1474 | dig = c & 0xf; | |
1475 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1476 | } | |
1477 | } | |
1478 | s += '"'; | |
1479 | exp->write_string(s); | |
1480 | } | |
1481 | ||
1482 | // Import a string expression. | |
1483 | ||
1484 | Expression* | |
1485 | String_expression::do_import(Import* imp) | |
1486 | { | |
1487 | imp->require_c_string("\""); | |
1488 | std::string val; | |
1489 | while (true) | |
1490 | { | |
1491 | int c = imp->get_char(); | |
1492 | if (c == '"' || c == -1) | |
1493 | break; | |
1494 | if (c != '\\') | |
1495 | val += static_cast<char>(c); | |
1496 | else | |
1497 | { | |
1498 | c = imp->get_char(); | |
1499 | if (c == '\\' || c == '"') | |
1500 | val += static_cast<char>(c); | |
1501 | else if (c == 'n') | |
1502 | val += '\n'; | |
1503 | else if (c == 't') | |
1504 | val += '\t'; | |
1505 | else if (c == 'x') | |
1506 | { | |
1507 | c = imp->get_char(); | |
1508 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1509 | c = imp->get_char(); | |
1510 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1511 | char v = (vh << 4) | vl; | |
1512 | val += v; | |
1513 | } | |
1514 | else | |
1515 | { | |
1516 | error_at(imp->location(), "bad string constant"); | |
1517 | return Expression::make_error(imp->location()); | |
1518 | } | |
1519 | } | |
1520 | } | |
1521 | return Expression::make_string(val, imp->location()); | |
1522 | } | |
1523 | ||
1524 | // Make a string expression. | |
1525 | ||
1526 | Expression* | |
1527 | Expression::make_string(const std::string& val, source_location location) | |
1528 | { | |
1529 | return new String_expression(val, location); | |
1530 | } | |
1531 | ||
1532 | // Make an integer expression. | |
1533 | ||
1534 | class Integer_expression : public Expression | |
1535 | { | |
1536 | public: | |
1537 | Integer_expression(const mpz_t* val, Type* type, source_location location) | |
1538 | : Expression(EXPRESSION_INTEGER, location), | |
1539 | type_(type) | |
1540 | { mpz_init_set(this->val_, *val); } | |
1541 | ||
1542 | static Expression* | |
1543 | do_import(Import*); | |
1544 | ||
1545 | // Return whether VAL fits in the type. | |
1546 | static bool | |
1547 | check_constant(mpz_t val, Type*, source_location); | |
1548 | ||
1549 | // Write VAL to export data. | |
1550 | static void | |
1551 | export_integer(Export* exp, const mpz_t val); | |
1552 | ||
1553 | protected: | |
1554 | bool | |
1555 | do_is_constant() const | |
1556 | { return true; } | |
1557 | ||
1558 | bool | |
1559 | do_integer_constant_value(bool, mpz_t val, Type** ptype) const; | |
1560 | ||
1561 | Type* | |
1562 | do_type(); | |
1563 | ||
1564 | void | |
1565 | do_determine_type(const Type_context* context); | |
1566 | ||
1567 | void | |
1568 | do_check_types(Gogo*); | |
1569 | ||
1570 | tree | |
1571 | do_get_tree(Translate_context*); | |
1572 | ||
1573 | Expression* | |
1574 | do_copy() | |
1575 | { return Expression::make_integer(&this->val_, this->type_, | |
1576 | this->location()); } | |
1577 | ||
1578 | void | |
1579 | do_export(Export*) const; | |
1580 | ||
1581 | private: | |
1582 | // The integer value. | |
1583 | mpz_t val_; | |
1584 | // The type so far. | |
1585 | Type* type_; | |
1586 | }; | |
1587 | ||
1588 | // Return an integer constant value. | |
1589 | ||
1590 | bool | |
1591 | Integer_expression::do_integer_constant_value(bool, mpz_t val, | |
1592 | Type** ptype) const | |
1593 | { | |
1594 | if (this->type_ != NULL) | |
1595 | *ptype = this->type_; | |
1596 | mpz_set(val, this->val_); | |
1597 | return true; | |
1598 | } | |
1599 | ||
1600 | // Return the current type. If we haven't set the type yet, we return | |
1601 | // an abstract integer type. | |
1602 | ||
1603 | Type* | |
1604 | Integer_expression::do_type() | |
1605 | { | |
1606 | if (this->type_ == NULL) | |
1607 | this->type_ = Type::make_abstract_integer_type(); | |
1608 | return this->type_; | |
1609 | } | |
1610 | ||
1611 | // Set the type of the integer value. Here we may switch from an | |
1612 | // abstract type to a real type. | |
1613 | ||
1614 | void | |
1615 | Integer_expression::do_determine_type(const Type_context* context) | |
1616 | { | |
1617 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1618 | ; | |
1619 | else if (context->type != NULL | |
1620 | && (context->type->integer_type() != NULL | |
1621 | || context->type->float_type() != NULL | |
1622 | || context->type->complex_type() != NULL)) | |
1623 | this->type_ = context->type; | |
1624 | else if (!context->may_be_abstract) | |
1625 | this->type_ = Type::lookup_integer_type("int"); | |
1626 | } | |
1627 | ||
1628 | // Return true if the integer VAL fits in the range of the type TYPE. | |
1629 | // Otherwise give an error and return false. TYPE may be NULL. | |
1630 | ||
1631 | bool | |
1632 | Integer_expression::check_constant(mpz_t val, Type* type, | |
1633 | source_location location) | |
1634 | { | |
1635 | if (type == NULL) | |
1636 | return true; | |
1637 | Integer_type* itype = type->integer_type(); | |
1638 | if (itype == NULL || itype->is_abstract()) | |
1639 | return true; | |
1640 | ||
1641 | int bits = mpz_sizeinbase(val, 2); | |
1642 | ||
1643 | if (itype->is_unsigned()) | |
1644 | { | |
1645 | // For an unsigned type we can only accept a nonnegative number, | |
1646 | // and we must be able to represent at least BITS. | |
1647 | if (mpz_sgn(val) >= 0 | |
1648 | && bits <= itype->bits()) | |
1649 | return true; | |
1650 | } | |
1651 | else | |
1652 | { | |
1653 | // For a signed type we need an extra bit to indicate the sign. | |
1654 | // We have to handle the most negative integer specially. | |
1655 | if (bits + 1 <= itype->bits() | |
1656 | || (bits <= itype->bits() | |
1657 | && mpz_sgn(val) < 0 | |
1658 | && (mpz_scan1(val, 0) | |
1659 | == static_cast<unsigned long>(itype->bits() - 1)) | |
1660 | && mpz_scan0(val, itype->bits()) == ULONG_MAX)) | |
1661 | return true; | |
1662 | } | |
1663 | ||
1664 | error_at(location, "integer constant overflow"); | |
1665 | return false; | |
1666 | } | |
1667 | ||
1668 | // Check the type of an integer constant. | |
1669 | ||
1670 | void | |
1671 | Integer_expression::do_check_types(Gogo*) | |
1672 | { | |
1673 | if (this->type_ == NULL) | |
1674 | return; | |
1675 | if (!Integer_expression::check_constant(this->val_, this->type_, | |
1676 | this->location())) | |
1677 | this->set_is_error(); | |
1678 | } | |
1679 | ||
1680 | // Get a tree for an integer constant. | |
1681 | ||
1682 | tree | |
1683 | Integer_expression::do_get_tree(Translate_context* context) | |
1684 | { | |
1685 | Gogo* gogo = context->gogo(); | |
1686 | tree type; | |
1687 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1688 | type = this->type_->get_tree(gogo); | |
1689 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
1690 | { | |
1691 | // We are converting to an abstract floating point type. | |
1692 | type = Type::lookup_float_type("float64")->get_tree(gogo); | |
1693 | } | |
1694 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
1695 | { | |
1696 | // We are converting to an abstract complex type. | |
1697 | type = Type::lookup_complex_type("complex128")->get_tree(gogo); | |
1698 | } | |
1699 | else | |
1700 | { | |
1701 | // If we still have an abstract type here, then this is being | |
1702 | // used in a constant expression which didn't get reduced for | |
1703 | // some reason. Use a type which will fit the value. We use <, | |
1704 | // not <=, because we need an extra bit for the sign bit. | |
1705 | int bits = mpz_sizeinbase(this->val_, 2); | |
1706 | if (bits < INT_TYPE_SIZE) | |
1707 | type = Type::lookup_integer_type("int")->get_tree(gogo); | |
1708 | else if (bits < 64) | |
1709 | type = Type::lookup_integer_type("int64")->get_tree(gogo); | |
1710 | else | |
1711 | type = long_long_integer_type_node; | |
1712 | } | |
1713 | return Expression::integer_constant_tree(this->val_, type); | |
1714 | } | |
1715 | ||
1716 | // Write VAL to export data. | |
1717 | ||
1718 | void | |
1719 | Integer_expression::export_integer(Export* exp, const mpz_t val) | |
1720 | { | |
1721 | char* s = mpz_get_str(NULL, 10, val); | |
1722 | exp->write_c_string(s); | |
1723 | free(s); | |
1724 | } | |
1725 | ||
1726 | // Export an integer in a constant expression. | |
1727 | ||
1728 | void | |
1729 | Integer_expression::do_export(Export* exp) const | |
1730 | { | |
1731 | Integer_expression::export_integer(exp, this->val_); | |
1732 | // A trailing space lets us reliably identify the end of the number. | |
1733 | exp->write_c_string(" "); | |
1734 | } | |
1735 | ||
1736 | // Import an integer, floating point, or complex value. This handles | |
1737 | // all these types because they all start with digits. | |
1738 | ||
1739 | Expression* | |
1740 | Integer_expression::do_import(Import* imp) | |
1741 | { | |
1742 | std::string num = imp->read_identifier(); | |
1743 | imp->require_c_string(" "); | |
1744 | if (!num.empty() && num[num.length() - 1] == 'i') | |
1745 | { | |
1746 | mpfr_t real; | |
1747 | size_t plus_pos = num.find('+', 1); | |
1748 | size_t minus_pos = num.find('-', 1); | |
1749 | size_t pos; | |
1750 | if (plus_pos == std::string::npos) | |
1751 | pos = minus_pos; | |
1752 | else if (minus_pos == std::string::npos) | |
1753 | pos = plus_pos; | |
1754 | else | |
1755 | { | |
1756 | error_at(imp->location(), "bad number in import data: %qs", | |
1757 | num.c_str()); | |
1758 | return Expression::make_error(imp->location()); | |
1759 | } | |
1760 | if (pos == std::string::npos) | |
1761 | mpfr_set_ui(real, 0, GMP_RNDN); | |
1762 | else | |
1763 | { | |
1764 | std::string real_str = num.substr(0, pos); | |
1765 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
1766 | { | |
1767 | error_at(imp->location(), "bad number in import data: %qs", | |
1768 | real_str.c_str()); | |
1769 | return Expression::make_error(imp->location()); | |
1770 | } | |
1771 | } | |
1772 | ||
1773 | std::string imag_str; | |
1774 | if (pos == std::string::npos) | |
1775 | imag_str = num; | |
1776 | else | |
1777 | imag_str = num.substr(pos); | |
1778 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
1779 | mpfr_t imag; | |
1780 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
1781 | { | |
1782 | error_at(imp->location(), "bad number in import data: %qs", | |
1783 | imag_str.c_str()); | |
1784 | return Expression::make_error(imp->location()); | |
1785 | } | |
1786 | Expression* ret = Expression::make_complex(&real, &imag, NULL, | |
1787 | imp->location()); | |
1788 | mpfr_clear(real); | |
1789 | mpfr_clear(imag); | |
1790 | return ret; | |
1791 | } | |
1792 | else if (num.find('.') == std::string::npos | |
1793 | && num.find('E') == std::string::npos) | |
1794 | { | |
1795 | mpz_t val; | |
1796 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
1797 | { | |
1798 | error_at(imp->location(), "bad number in import data: %qs", | |
1799 | num.c_str()); | |
1800 | return Expression::make_error(imp->location()); | |
1801 | } | |
1802 | Expression* ret = Expression::make_integer(&val, NULL, imp->location()); | |
1803 | mpz_clear(val); | |
1804 | return ret; | |
1805 | } | |
1806 | else | |
1807 | { | |
1808 | mpfr_t val; | |
1809 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
1810 | { | |
1811 | error_at(imp->location(), "bad number in import data: %qs", | |
1812 | num.c_str()); | |
1813 | return Expression::make_error(imp->location()); | |
1814 | } | |
1815 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
1816 | mpfr_clear(val); | |
1817 | return ret; | |
1818 | } | |
1819 | } | |
1820 | ||
1821 | // Build a new integer value. | |
1822 | ||
1823 | Expression* | |
1824 | Expression::make_integer(const mpz_t* val, Type* type, | |
1825 | source_location location) | |
1826 | { | |
1827 | return new Integer_expression(val, type, location); | |
1828 | } | |
1829 | ||
1830 | // Floats. | |
1831 | ||
1832 | class Float_expression : public Expression | |
1833 | { | |
1834 | public: | |
1835 | Float_expression(const mpfr_t* val, Type* type, source_location location) | |
1836 | : Expression(EXPRESSION_FLOAT, location), | |
1837 | type_(type) | |
1838 | { | |
1839 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
1840 | } | |
1841 | ||
1842 | // Constrain VAL to fit into TYPE. | |
1843 | static void | |
1844 | constrain_float(mpfr_t val, Type* type); | |
1845 | ||
1846 | // Return whether VAL fits in the type. | |
1847 | static bool | |
1848 | check_constant(mpfr_t val, Type*, source_location); | |
1849 | ||
1850 | // Write VAL to export data. | |
1851 | static void | |
1852 | export_float(Export* exp, const mpfr_t val); | |
1853 | ||
1854 | protected: | |
1855 | bool | |
1856 | do_is_constant() const | |
1857 | { return true; } | |
1858 | ||
1859 | bool | |
1860 | do_float_constant_value(mpfr_t val, Type**) const; | |
1861 | ||
1862 | Type* | |
1863 | do_type(); | |
1864 | ||
1865 | void | |
1866 | do_determine_type(const Type_context*); | |
1867 | ||
1868 | void | |
1869 | do_check_types(Gogo*); | |
1870 | ||
1871 | Expression* | |
1872 | do_copy() | |
1873 | { return Expression::make_float(&this->val_, this->type_, | |
1874 | this->location()); } | |
1875 | ||
1876 | tree | |
1877 | do_get_tree(Translate_context*); | |
1878 | ||
1879 | void | |
1880 | do_export(Export*) const; | |
1881 | ||
1882 | private: | |
1883 | // The floating point value. | |
1884 | mpfr_t val_; | |
1885 | // The type so far. | |
1886 | Type* type_; | |
1887 | }; | |
1888 | ||
1889 | // Constrain VAL to fit into TYPE. | |
1890 | ||
1891 | void | |
1892 | Float_expression::constrain_float(mpfr_t val, Type* type) | |
1893 | { | |
1894 | Float_type* ftype = type->float_type(); | |
1895 | if (ftype != NULL && !ftype->is_abstract()) | |
1896 | { | |
1897 | tree type_tree = ftype->type_tree(); | |
1898 | REAL_VALUE_TYPE rvt; | |
1899 | real_from_mpfr(&rvt, val, type_tree, GMP_RNDN); | |
1900 | real_convert(&rvt, TYPE_MODE(type_tree), &rvt); | |
1901 | mpfr_from_real(val, &rvt, GMP_RNDN); | |
1902 | } | |
1903 | } | |
1904 | ||
1905 | // Return a floating point constant value. | |
1906 | ||
1907 | bool | |
1908 | Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
1909 | { | |
1910 | if (this->type_ != NULL) | |
1911 | *ptype = this->type_; | |
1912 | mpfr_set(val, this->val_, GMP_RNDN); | |
1913 | return true; | |
1914 | } | |
1915 | ||
1916 | // Return the current type. If we haven't set the type yet, we return | |
1917 | // an abstract float type. | |
1918 | ||
1919 | Type* | |
1920 | Float_expression::do_type() | |
1921 | { | |
1922 | if (this->type_ == NULL) | |
1923 | this->type_ = Type::make_abstract_float_type(); | |
1924 | return this->type_; | |
1925 | } | |
1926 | ||
1927 | // Set the type of the float value. Here we may switch from an | |
1928 | // abstract type to a real type. | |
1929 | ||
1930 | void | |
1931 | Float_expression::do_determine_type(const Type_context* context) | |
1932 | { | |
1933 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1934 | ; | |
1935 | else if (context->type != NULL | |
1936 | && (context->type->integer_type() != NULL | |
1937 | || context->type->float_type() != NULL | |
1938 | || context->type->complex_type() != NULL)) | |
1939 | this->type_ = context->type; | |
1940 | else if (!context->may_be_abstract) | |
48080209 | 1941 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 1942 | } |
1943 | ||
1944 | // Return true if the floating point value VAL fits in the range of | |
1945 | // the type TYPE. Otherwise give an error and return false. TYPE may | |
1946 | // be NULL. | |
1947 | ||
1948 | bool | |
1949 | Float_expression::check_constant(mpfr_t val, Type* type, | |
1950 | source_location location) | |
1951 | { | |
1952 | if (type == NULL) | |
1953 | return true; | |
1954 | Float_type* ftype = type->float_type(); | |
1955 | if (ftype == NULL || ftype->is_abstract()) | |
1956 | return true; | |
1957 | ||
1958 | // A NaN or Infinity always fits in the range of the type. | |
1959 | if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
1960 | return true; | |
1961 | ||
1962 | mp_exp_t exp = mpfr_get_exp(val); | |
1963 | mp_exp_t max_exp; | |
1964 | switch (ftype->bits()) | |
1965 | { | |
1966 | case 32: | |
1967 | max_exp = 128; | |
1968 | break; | |
1969 | case 64: | |
1970 | max_exp = 1024; | |
1971 | break; | |
1972 | default: | |
1973 | gcc_unreachable(); | |
1974 | } | |
1975 | if (exp > max_exp) | |
1976 | { | |
1977 | error_at(location, "floating point constant overflow"); | |
1978 | return false; | |
1979 | } | |
1980 | return true; | |
1981 | } | |
1982 | ||
1983 | // Check the type of a float value. | |
1984 | ||
1985 | void | |
1986 | Float_expression::do_check_types(Gogo*) | |
1987 | { | |
1988 | if (this->type_ == NULL) | |
1989 | return; | |
1990 | ||
1991 | if (!Float_expression::check_constant(this->val_, this->type_, | |
1992 | this->location())) | |
1993 | this->set_is_error(); | |
1994 | ||
1995 | Integer_type* integer_type = this->type_->integer_type(); | |
1996 | if (integer_type != NULL) | |
1997 | { | |
1998 | if (!mpfr_integer_p(this->val_)) | |
1999 | this->report_error(_("floating point constant truncated to integer")); | |
2000 | else | |
2001 | { | |
2002 | gcc_assert(!integer_type->is_abstract()); | |
2003 | mpz_t ival; | |
2004 | mpz_init(ival); | |
2005 | mpfr_get_z(ival, this->val_, GMP_RNDN); | |
2006 | Integer_expression::check_constant(ival, integer_type, | |
2007 | this->location()); | |
2008 | mpz_clear(ival); | |
2009 | } | |
2010 | } | |
2011 | } | |
2012 | ||
2013 | // Get a tree for a float constant. | |
2014 | ||
2015 | tree | |
2016 | Float_expression::do_get_tree(Translate_context* context) | |
2017 | { | |
2018 | Gogo* gogo = context->gogo(); | |
2019 | tree type; | |
2020 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2021 | type = this->type_->get_tree(gogo); | |
2022 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
2023 | { | |
2024 | // We have an abstract integer type. We just hope for the best. | |
2025 | type = Type::lookup_integer_type("int")->get_tree(gogo); | |
2026 | } | |
2027 | else | |
2028 | { | |
2029 | // If we still have an abstract type here, then this is being | |
2030 | // used in a constant expression which didn't get reduced. We | |
2031 | // just use float64 and hope for the best. | |
2032 | type = Type::lookup_float_type("float64")->get_tree(gogo); | |
2033 | } | |
2034 | return Expression::float_constant_tree(this->val_, type); | |
2035 | } | |
2036 | ||
2037 | // Write a floating point number to export data. | |
2038 | ||
2039 | void | |
2040 | Float_expression::export_float(Export *exp, const mpfr_t val) | |
2041 | { | |
2042 | mp_exp_t exponent; | |
2043 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2044 | if (*s == '-') | |
2045 | exp->write_c_string("-"); | |
2046 | exp->write_c_string("0."); | |
2047 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2048 | mpfr_free_str(s); | |
2049 | char buf[30]; | |
2050 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2051 | exp->write_c_string(buf); | |
2052 | } | |
2053 | ||
2054 | // Export a floating point number in a constant expression. | |
2055 | ||
2056 | void | |
2057 | Float_expression::do_export(Export* exp) const | |
2058 | { | |
2059 | Float_expression::export_float(exp, this->val_); | |
2060 | // A trailing space lets us reliably identify the end of the number. | |
2061 | exp->write_c_string(" "); | |
2062 | } | |
2063 | ||
2064 | // Make a float expression. | |
2065 | ||
2066 | Expression* | |
2067 | Expression::make_float(const mpfr_t* val, Type* type, source_location location) | |
2068 | { | |
2069 | return new Float_expression(val, type, location); | |
2070 | } | |
2071 | ||
2072 | // Complex numbers. | |
2073 | ||
2074 | class Complex_expression : public Expression | |
2075 | { | |
2076 | public: | |
2077 | Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
2078 | source_location location) | |
2079 | : Expression(EXPRESSION_COMPLEX, location), | |
2080 | type_(type) | |
2081 | { | |
2082 | mpfr_init_set(this->real_, *real, GMP_RNDN); | |
2083 | mpfr_init_set(this->imag_, *imag, GMP_RNDN); | |
2084 | } | |
2085 | ||
2086 | // Constrain REAL/IMAG to fit into TYPE. | |
2087 | static void | |
2088 | constrain_complex(mpfr_t real, mpfr_t imag, Type* type); | |
2089 | ||
2090 | // Return whether REAL/IMAG fits in the type. | |
2091 | static bool | |
2092 | check_constant(mpfr_t real, mpfr_t imag, Type*, source_location); | |
2093 | ||
2094 | // Write REAL/IMAG to export data. | |
2095 | static void | |
2096 | export_complex(Export* exp, const mpfr_t real, const mpfr_t val); | |
2097 | ||
2098 | protected: | |
2099 | bool | |
2100 | do_is_constant() const | |
2101 | { return true; } | |
2102 | ||
2103 | bool | |
2104 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const; | |
2105 | ||
2106 | Type* | |
2107 | do_type(); | |
2108 | ||
2109 | void | |
2110 | do_determine_type(const Type_context*); | |
2111 | ||
2112 | void | |
2113 | do_check_types(Gogo*); | |
2114 | ||
2115 | Expression* | |
2116 | do_copy() | |
2117 | { | |
2118 | return Expression::make_complex(&this->real_, &this->imag_, this->type_, | |
2119 | this->location()); | |
2120 | } | |
2121 | ||
2122 | tree | |
2123 | do_get_tree(Translate_context*); | |
2124 | ||
2125 | void | |
2126 | do_export(Export*) const; | |
2127 | ||
2128 | private: | |
2129 | // The real part. | |
2130 | mpfr_t real_; | |
2131 | // The imaginary part; | |
2132 | mpfr_t imag_; | |
2133 | // The type if known. | |
2134 | Type* type_; | |
2135 | }; | |
2136 | ||
2137 | // Constrain REAL/IMAG to fit into TYPE. | |
2138 | ||
2139 | void | |
2140 | Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type) | |
2141 | { | |
2142 | Complex_type* ctype = type->complex_type(); | |
2143 | if (ctype != NULL && !ctype->is_abstract()) | |
2144 | { | |
2145 | tree type_tree = ctype->type_tree(); | |
2146 | ||
2147 | REAL_VALUE_TYPE rvt; | |
2148 | real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN); | |
2149 | real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt); | |
2150 | mpfr_from_real(real, &rvt, GMP_RNDN); | |
2151 | ||
2152 | real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN); | |
2153 | real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt); | |
2154 | mpfr_from_real(imag, &rvt, GMP_RNDN); | |
2155 | } | |
2156 | } | |
2157 | ||
2158 | // Return a complex constant value. | |
2159 | ||
2160 | bool | |
2161 | Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
2162 | Type** ptype) const | |
2163 | { | |
2164 | if (this->type_ != NULL) | |
2165 | *ptype = this->type_; | |
2166 | mpfr_set(real, this->real_, GMP_RNDN); | |
2167 | mpfr_set(imag, this->imag_, GMP_RNDN); | |
2168 | return true; | |
2169 | } | |
2170 | ||
2171 | // Return the current type. If we haven't set the type yet, we return | |
2172 | // an abstract complex type. | |
2173 | ||
2174 | Type* | |
2175 | Complex_expression::do_type() | |
2176 | { | |
2177 | if (this->type_ == NULL) | |
2178 | this->type_ = Type::make_abstract_complex_type(); | |
2179 | return this->type_; | |
2180 | } | |
2181 | ||
2182 | // Set the type of the complex value. Here we may switch from an | |
2183 | // abstract type to a real type. | |
2184 | ||
2185 | void | |
2186 | Complex_expression::do_determine_type(const Type_context* context) | |
2187 | { | |
2188 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2189 | ; | |
2190 | else if (context->type != NULL | |
2191 | && context->type->complex_type() != NULL) | |
2192 | this->type_ = context->type; | |
2193 | else if (!context->may_be_abstract) | |
48080209 | 2194 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2195 | } |
2196 | ||
2197 | // Return true if the complex value REAL/IMAG fits in the range of the | |
2198 | // type TYPE. Otherwise give an error and return false. TYPE may be | |
2199 | // NULL. | |
2200 | ||
2201 | bool | |
2202 | Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type, | |
2203 | source_location location) | |
2204 | { | |
2205 | if (type == NULL) | |
2206 | return true; | |
2207 | Complex_type* ctype = type->complex_type(); | |
2208 | if (ctype == NULL || ctype->is_abstract()) | |
2209 | return true; | |
2210 | ||
2211 | mp_exp_t max_exp; | |
2212 | switch (ctype->bits()) | |
2213 | { | |
2214 | case 64: | |
2215 | max_exp = 128; | |
2216 | break; | |
2217 | case 128: | |
2218 | max_exp = 1024; | |
2219 | break; | |
2220 | default: | |
2221 | gcc_unreachable(); | |
2222 | } | |
2223 | ||
2224 | // A NaN or Infinity always fits in the range of the type. | |
2225 | if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real)) | |
2226 | { | |
2227 | if (mpfr_get_exp(real) > max_exp) | |
2228 | { | |
2229 | error_at(location, "complex real part constant overflow"); | |
2230 | return false; | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag)) | |
2235 | { | |
2236 | if (mpfr_get_exp(imag) > max_exp) | |
2237 | { | |
2238 | error_at(location, "complex imaginary part constant overflow"); | |
2239 | return false; | |
2240 | } | |
2241 | } | |
2242 | ||
2243 | return true; | |
2244 | } | |
2245 | ||
2246 | // Check the type of a complex value. | |
2247 | ||
2248 | void | |
2249 | Complex_expression::do_check_types(Gogo*) | |
2250 | { | |
2251 | if (this->type_ == NULL) | |
2252 | return; | |
2253 | ||
2254 | if (!Complex_expression::check_constant(this->real_, this->imag_, | |
2255 | this->type_, this->location())) | |
2256 | this->set_is_error(); | |
2257 | } | |
2258 | ||
2259 | // Get a tree for a complex constant. | |
2260 | ||
2261 | tree | |
2262 | Complex_expression::do_get_tree(Translate_context* context) | |
2263 | { | |
2264 | Gogo* gogo = context->gogo(); | |
2265 | tree type; | |
2266 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2267 | type = this->type_->get_tree(gogo); | |
2268 | else | |
2269 | { | |
2270 | // If we still have an abstract type here, this this is being | |
2271 | // used in a constant expression which didn't get reduced. We | |
2272 | // just use complex128 and hope for the best. | |
2273 | type = Type::lookup_complex_type("complex128")->get_tree(gogo); | |
2274 | } | |
2275 | return Expression::complex_constant_tree(this->real_, this->imag_, type); | |
2276 | } | |
2277 | ||
2278 | // Write REAL/IMAG to export data. | |
2279 | ||
2280 | void | |
2281 | Complex_expression::export_complex(Export* exp, const mpfr_t real, | |
2282 | const mpfr_t imag) | |
2283 | { | |
2284 | if (!mpfr_zero_p(real)) | |
2285 | { | |
2286 | Float_expression::export_float(exp, real); | |
2287 | if (mpfr_sgn(imag) > 0) | |
2288 | exp->write_c_string("+"); | |
2289 | } | |
2290 | Float_expression::export_float(exp, imag); | |
2291 | exp->write_c_string("i"); | |
2292 | } | |
2293 | ||
2294 | // Export a complex number in a constant expression. | |
2295 | ||
2296 | void | |
2297 | Complex_expression::do_export(Export* exp) const | |
2298 | { | |
2299 | Complex_expression::export_complex(exp, this->real_, this->imag_); | |
2300 | // A trailing space lets us reliably identify the end of the number. | |
2301 | exp->write_c_string(" "); | |
2302 | } | |
2303 | ||
2304 | // Make a complex expression. | |
2305 | ||
2306 | Expression* | |
2307 | Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
2308 | source_location location) | |
2309 | { | |
2310 | return new Complex_expression(real, imag, type, location); | |
2311 | } | |
2312 | ||
d5b605df | 2313 | // Find a named object in an expression. |
2314 | ||
2315 | class Find_named_object : public Traverse | |
2316 | { | |
2317 | public: | |
2318 | Find_named_object(Named_object* no) | |
2319 | : Traverse(traverse_expressions), | |
2320 | no_(no), found_(false) | |
2321 | { } | |
2322 | ||
2323 | // Whether we found the object. | |
2324 | bool | |
2325 | found() const | |
2326 | { return this->found_; } | |
2327 | ||
2328 | protected: | |
2329 | int | |
2330 | expression(Expression**); | |
2331 | ||
2332 | private: | |
2333 | // The object we are looking for. | |
2334 | Named_object* no_; | |
2335 | // Whether we found it. | |
2336 | bool found_; | |
2337 | }; | |
2338 | ||
e440a328 | 2339 | // A reference to a const in an expression. |
2340 | ||
2341 | class Const_expression : public Expression | |
2342 | { | |
2343 | public: | |
2344 | Const_expression(Named_object* constant, source_location location) | |
2345 | : Expression(EXPRESSION_CONST_REFERENCE, location), | |
13e818f5 | 2346 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2347 | { } |
2348 | ||
d5b605df | 2349 | Named_object* |
2350 | named_object() | |
2351 | { return this->constant_; } | |
2352 | ||
e440a328 | 2353 | const std::string& |
2354 | name() const | |
2355 | { return this->constant_->name(); } | |
2356 | ||
a7f064d5 | 2357 | // Check that the initializer does not refer to the constant itself. |
2358 | void | |
2359 | check_for_init_loop(); | |
2360 | ||
e440a328 | 2361 | protected: |
ba4aedd4 | 2362 | int |
2363 | do_traverse(Traverse*); | |
2364 | ||
e440a328 | 2365 | Expression* |
2366 | do_lower(Gogo*, Named_object*, int); | |
2367 | ||
2368 | bool | |
2369 | do_is_constant() const | |
2370 | { return true; } | |
2371 | ||
2372 | bool | |
2373 | do_integer_constant_value(bool, mpz_t val, Type**) const; | |
2374 | ||
2375 | bool | |
2376 | do_float_constant_value(mpfr_t val, Type**) const; | |
2377 | ||
2378 | bool | |
2379 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const; | |
2380 | ||
2381 | bool | |
2382 | do_string_constant_value(std::string* val) const | |
2383 | { return this->constant_->const_value()->expr()->string_constant_value(val); } | |
2384 | ||
2385 | Type* | |
2386 | do_type(); | |
2387 | ||
2388 | // The type of a const is set by the declaration, not the use. | |
2389 | void | |
2390 | do_determine_type(const Type_context*); | |
2391 | ||
2392 | void | |
2393 | do_check_types(Gogo*); | |
2394 | ||
2395 | Expression* | |
2396 | do_copy() | |
2397 | { return this; } | |
2398 | ||
2399 | tree | |
2400 | do_get_tree(Translate_context* context); | |
2401 | ||
2402 | // When exporting a reference to a const as part of a const | |
2403 | // expression, we export the value. We ignore the fact that it has | |
2404 | // a name. | |
2405 | void | |
2406 | do_export(Export* exp) const | |
2407 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2408 | ||
2409 | private: | |
2410 | // The constant. | |
2411 | Named_object* constant_; | |
2412 | // The type of this reference. This is used if the constant has an | |
2413 | // abstract type. | |
2414 | Type* type_; | |
13e818f5 | 2415 | // Used to prevent infinite recursion when a constant incorrectly |
2416 | // refers to itself. | |
2417 | mutable bool seen_; | |
e440a328 | 2418 | }; |
2419 | ||
ba4aedd4 | 2420 | // Traversal. |
2421 | ||
2422 | int | |
2423 | Const_expression::do_traverse(Traverse* traverse) | |
2424 | { | |
2425 | if (this->type_ != NULL) | |
2426 | return Type::traverse(this->type_, traverse); | |
2427 | return TRAVERSE_CONTINUE; | |
2428 | } | |
2429 | ||
e440a328 | 2430 | // Lower a constant expression. This is where we convert the |
2431 | // predeclared constant iota into an integer value. | |
2432 | ||
2433 | Expression* | |
2434 | Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value) | |
2435 | { | |
2436 | if (this->constant_->const_value()->expr()->classification() | |
2437 | == EXPRESSION_IOTA) | |
2438 | { | |
2439 | if (iota_value == -1) | |
2440 | { | |
2441 | error_at(this->location(), | |
2442 | "iota is only defined in const declarations"); | |
2443 | iota_value = 0; | |
2444 | } | |
2445 | mpz_t val; | |
2446 | mpz_init_set_ui(val, static_cast<unsigned long>(iota_value)); | |
2447 | Expression* ret = Expression::make_integer(&val, NULL, | |
2448 | this->location()); | |
2449 | mpz_clear(val); | |
2450 | return ret; | |
2451 | } | |
2452 | ||
2453 | // Make sure that the constant itself has been lowered. | |
2454 | gogo->lower_constant(this->constant_); | |
2455 | ||
2456 | return this; | |
2457 | } | |
2458 | ||
2459 | // Return an integer constant value. | |
2460 | ||
2461 | bool | |
2462 | Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
2463 | Type** ptype) const | |
2464 | { | |
13e818f5 | 2465 | if (this->seen_) |
2466 | return false; | |
2467 | ||
e440a328 | 2468 | Type* ctype; |
2469 | if (this->type_ != NULL) | |
2470 | ctype = this->type_; | |
2471 | else | |
2472 | ctype = this->constant_->const_value()->type(); | |
2473 | if (ctype != NULL && ctype->integer_type() == NULL) | |
2474 | return false; | |
2475 | ||
2476 | Expression* e = this->constant_->const_value()->expr(); | |
13e818f5 | 2477 | |
2478 | this->seen_ = true; | |
2479 | ||
e440a328 | 2480 | Type* t; |
2481 | bool r = e->integer_constant_value(iota_is_constant, val, &t); | |
2482 | ||
13e818f5 | 2483 | this->seen_ = false; |
2484 | ||
e440a328 | 2485 | if (r |
2486 | && ctype != NULL | |
2487 | && !Integer_expression::check_constant(val, ctype, this->location())) | |
2488 | return false; | |
2489 | ||
2490 | *ptype = ctype != NULL ? ctype : t; | |
2491 | return r; | |
2492 | } | |
2493 | ||
2494 | // Return a floating point constant value. | |
2495 | ||
2496 | bool | |
2497 | Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
2498 | { | |
13e818f5 | 2499 | if (this->seen_) |
2500 | return false; | |
2501 | ||
e440a328 | 2502 | Type* ctype; |
2503 | if (this->type_ != NULL) | |
2504 | ctype = this->type_; | |
2505 | else | |
2506 | ctype = this->constant_->const_value()->type(); | |
2507 | if (ctype != NULL && ctype->float_type() == NULL) | |
2508 | return false; | |
2509 | ||
13e818f5 | 2510 | this->seen_ = true; |
2511 | ||
e440a328 | 2512 | Type* t; |
2513 | bool r = this->constant_->const_value()->expr()->float_constant_value(val, | |
2514 | &t); | |
13e818f5 | 2515 | |
2516 | this->seen_ = false; | |
2517 | ||
e440a328 | 2518 | if (r && ctype != NULL) |
2519 | { | |
2520 | if (!Float_expression::check_constant(val, ctype, this->location())) | |
2521 | return false; | |
2522 | Float_expression::constrain_float(val, ctype); | |
2523 | } | |
2524 | *ptype = ctype != NULL ? ctype : t; | |
2525 | return r; | |
2526 | } | |
2527 | ||
2528 | // Return a complex constant value. | |
2529 | ||
2530 | bool | |
2531 | Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
2532 | Type **ptype) const | |
2533 | { | |
13e818f5 | 2534 | if (this->seen_) |
2535 | return false; | |
2536 | ||
e440a328 | 2537 | Type* ctype; |
2538 | if (this->type_ != NULL) | |
2539 | ctype = this->type_; | |
2540 | else | |
2541 | ctype = this->constant_->const_value()->type(); | |
2542 | if (ctype != NULL && ctype->complex_type() == NULL) | |
2543 | return false; | |
2544 | ||
13e818f5 | 2545 | this->seen_ = true; |
2546 | ||
e440a328 | 2547 | Type *t; |
2548 | bool r = this->constant_->const_value()->expr()->complex_constant_value(real, | |
2549 | imag, | |
2550 | &t); | |
13e818f5 | 2551 | |
2552 | this->seen_ = false; | |
2553 | ||
e440a328 | 2554 | if (r && ctype != NULL) |
2555 | { | |
2556 | if (!Complex_expression::check_constant(real, imag, ctype, | |
2557 | this->location())) | |
2558 | return false; | |
2559 | Complex_expression::constrain_complex(real, imag, ctype); | |
2560 | } | |
2561 | *ptype = ctype != NULL ? ctype : t; | |
2562 | return r; | |
2563 | } | |
2564 | ||
2565 | // Return the type of the const reference. | |
2566 | ||
2567 | Type* | |
2568 | Const_expression::do_type() | |
2569 | { | |
2570 | if (this->type_ != NULL) | |
2571 | return this->type_; | |
13e818f5 | 2572 | |
2f78f012 | 2573 | Named_constant* nc = this->constant_->const_value(); |
2574 | ||
2575 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2576 | { |
2577 | this->report_error(_("constant refers to itself")); | |
2578 | this->type_ = Type::make_error_type(); | |
2579 | return this->type_; | |
2580 | } | |
2581 | ||
2582 | this->seen_ = true; | |
2583 | ||
e440a328 | 2584 | Type* ret = nc->type(); |
13e818f5 | 2585 | |
e440a328 | 2586 | if (ret != NULL) |
13e818f5 | 2587 | { |
2588 | this->seen_ = false; | |
2589 | return ret; | |
2590 | } | |
2591 | ||
e440a328 | 2592 | // During parsing, a named constant may have a NULL type, but we |
2593 | // must not return a NULL type here. | |
13e818f5 | 2594 | ret = nc->expr()->type(); |
2595 | ||
2596 | this->seen_ = false; | |
2597 | ||
2598 | return ret; | |
e440a328 | 2599 | } |
2600 | ||
2601 | // Set the type of the const reference. | |
2602 | ||
2603 | void | |
2604 | Const_expression::do_determine_type(const Type_context* context) | |
2605 | { | |
2606 | Type* ctype = this->constant_->const_value()->type(); | |
2607 | Type* cetype = (ctype != NULL | |
2608 | ? ctype | |
2609 | : this->constant_->const_value()->expr()->type()); | |
2610 | if (ctype != NULL && !ctype->is_abstract()) | |
2611 | ; | |
2612 | else if (context->type != NULL | |
2613 | && (context->type->integer_type() != NULL | |
2614 | || context->type->float_type() != NULL | |
2615 | || context->type->complex_type() != NULL) | |
2616 | && (cetype->integer_type() != NULL | |
2617 | || cetype->float_type() != NULL | |
2618 | || cetype->complex_type() != NULL)) | |
2619 | this->type_ = context->type; | |
2620 | else if (context->type != NULL | |
2621 | && context->type->is_string_type() | |
2622 | && cetype->is_string_type()) | |
2623 | this->type_ = context->type; | |
2624 | else if (context->type != NULL | |
2625 | && context->type->is_boolean_type() | |
2626 | && cetype->is_boolean_type()) | |
2627 | this->type_ = context->type; | |
2628 | else if (!context->may_be_abstract) | |
2629 | { | |
2630 | if (cetype->is_abstract()) | |
2631 | cetype = cetype->make_non_abstract_type(); | |
2632 | this->type_ = cetype; | |
2633 | } | |
2634 | } | |
2635 | ||
a7f064d5 | 2636 | // Check for a loop in which the initializer of a constant refers to |
2637 | // the constant itself. | |
e440a328 | 2638 | |
2639 | void | |
a7f064d5 | 2640 | Const_expression::check_for_init_loop() |
e440a328 | 2641 | { |
d5b605df | 2642 | if (this->type_ != NULL && this->type_->is_error_type()) |
2643 | return; | |
2644 | ||
a7f064d5 | 2645 | if (this->seen_) |
2646 | { | |
2647 | this->report_error(_("constant refers to itself")); | |
2648 | this->type_ = Type::make_error_type(); | |
2649 | return; | |
2650 | } | |
2651 | ||
d5b605df | 2652 | Expression* init = this->constant_->const_value()->expr(); |
2653 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2654 | |
2655 | this->seen_ = true; | |
d5b605df | 2656 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2657 | this->seen_ = false; |
2658 | ||
d5b605df | 2659 | if (find_named_object.found()) |
2660 | { | |
a7f064d5 | 2661 | if (this->type_ == NULL || !this->type_->is_error_type()) |
2662 | { | |
2663 | this->report_error(_("constant refers to itself")); | |
2664 | this->type_ = Type::make_error_type(); | |
2665 | } | |
d5b605df | 2666 | return; |
2667 | } | |
a7f064d5 | 2668 | } |
2669 | ||
2670 | // Check types of a const reference. | |
2671 | ||
2672 | void | |
2673 | Const_expression::do_check_types(Gogo*) | |
2674 | { | |
2675 | if (this->type_ != NULL && this->type_->is_error_type()) | |
2676 | return; | |
2677 | ||
2678 | this->check_for_init_loop(); | |
d5b605df | 2679 | |
e440a328 | 2680 | if (this->type_ == NULL || this->type_->is_abstract()) |
2681 | return; | |
2682 | ||
2683 | // Check for integer overflow. | |
2684 | if (this->type_->integer_type() != NULL) | |
2685 | { | |
2686 | mpz_t ival; | |
2687 | mpz_init(ival); | |
2688 | Type* dummy; | |
2689 | if (!this->integer_constant_value(true, ival, &dummy)) | |
2690 | { | |
2691 | mpfr_t fval; | |
2692 | mpfr_init(fval); | |
2693 | Expression* cexpr = this->constant_->const_value()->expr(); | |
2694 | if (cexpr->float_constant_value(fval, &dummy)) | |
2695 | { | |
2696 | if (!mpfr_integer_p(fval)) | |
2697 | this->report_error(_("floating point constant " | |
2698 | "truncated to integer")); | |
2699 | else | |
2700 | { | |
2701 | mpfr_get_z(ival, fval, GMP_RNDN); | |
2702 | Integer_expression::check_constant(ival, this->type_, | |
2703 | this->location()); | |
2704 | } | |
2705 | } | |
2706 | mpfr_clear(fval); | |
2707 | } | |
2708 | mpz_clear(ival); | |
2709 | } | |
2710 | } | |
2711 | ||
2712 | // Return a tree for the const reference. | |
2713 | ||
2714 | tree | |
2715 | Const_expression::do_get_tree(Translate_context* context) | |
2716 | { | |
2717 | Gogo* gogo = context->gogo(); | |
2718 | tree type_tree; | |
2719 | if (this->type_ == NULL) | |
2720 | type_tree = NULL_TREE; | |
2721 | else | |
2722 | { | |
2723 | type_tree = this->type_->get_tree(gogo); | |
2724 | if (type_tree == error_mark_node) | |
2725 | return error_mark_node; | |
2726 | } | |
2727 | ||
2728 | // If the type has been set for this expression, but the underlying | |
2729 | // object is an abstract int or float, we try to get the abstract | |
2730 | // value. Otherwise we may lose something in the conversion. | |
2731 | if (this->type_ != NULL | |
a68492b4 | 2732 | && (this->constant_->const_value()->type() == NULL |
2733 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2734 | { |
2735 | Expression* expr = this->constant_->const_value()->expr(); | |
2736 | mpz_t ival; | |
2737 | mpz_init(ival); | |
2738 | Type* t; | |
2739 | if (expr->integer_constant_value(true, ival, &t)) | |
2740 | { | |
2741 | tree ret = Expression::integer_constant_tree(ival, type_tree); | |
2742 | mpz_clear(ival); | |
2743 | return ret; | |
2744 | } | |
2745 | mpz_clear(ival); | |
2746 | ||
2747 | mpfr_t fval; | |
2748 | mpfr_init(fval); | |
2749 | if (expr->float_constant_value(fval, &t)) | |
2750 | { | |
2751 | tree ret = Expression::float_constant_tree(fval, type_tree); | |
2752 | mpfr_clear(fval); | |
2753 | return ret; | |
2754 | } | |
2755 | ||
2756 | mpfr_t imag; | |
2757 | mpfr_init(imag); | |
2758 | if (expr->complex_constant_value(fval, imag, &t)) | |
2759 | { | |
2760 | tree ret = Expression::complex_constant_tree(fval, imag, type_tree); | |
2761 | mpfr_clear(fval); | |
2762 | mpfr_clear(imag); | |
2763 | return ret; | |
2764 | } | |
2765 | mpfr_clear(imag); | |
2766 | mpfr_clear(fval); | |
2767 | } | |
2768 | ||
2769 | tree const_tree = this->constant_->get_tree(gogo, context->function()); | |
2770 | if (this->type_ == NULL | |
2771 | || const_tree == error_mark_node | |
2772 | || TREE_TYPE(const_tree) == error_mark_node) | |
2773 | return const_tree; | |
2774 | ||
2775 | tree ret; | |
2776 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree))) | |
2777 | ret = fold_convert(type_tree, const_tree); | |
2778 | else if (TREE_CODE(type_tree) == INTEGER_TYPE) | |
2779 | ret = fold(convert_to_integer(type_tree, const_tree)); | |
2780 | else if (TREE_CODE(type_tree) == REAL_TYPE) | |
2781 | ret = fold(convert_to_real(type_tree, const_tree)); | |
2782 | else if (TREE_CODE(type_tree) == COMPLEX_TYPE) | |
2783 | ret = fold(convert_to_complex(type_tree, const_tree)); | |
2784 | else | |
2785 | gcc_unreachable(); | |
2786 | return ret; | |
2787 | } | |
2788 | ||
2789 | // Make a reference to a constant in an expression. | |
2790 | ||
2791 | Expression* | |
2792 | Expression::make_const_reference(Named_object* constant, | |
2793 | source_location location) | |
2794 | { | |
2795 | return new Const_expression(constant, location); | |
2796 | } | |
2797 | ||
d5b605df | 2798 | // Find a named object in an expression. |
2799 | ||
2800 | int | |
2801 | Find_named_object::expression(Expression** pexpr) | |
2802 | { | |
2803 | switch ((*pexpr)->classification()) | |
2804 | { | |
2805 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 2806 | { |
2807 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
2808 | if (ce->named_object() == this->no_) | |
2809 | break; | |
2810 | ||
2811 | // We need to check a constant initializer explicitly, as | |
2812 | // loops here will not be caught by the loop checking for | |
2813 | // variable initializers. | |
2814 | ce->check_for_init_loop(); | |
2815 | ||
2816 | return TRAVERSE_CONTINUE; | |
2817 | } | |
2818 | ||
d5b605df | 2819 | case Expression::EXPRESSION_VAR_REFERENCE: |
2820 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
2821 | break; | |
2822 | return TRAVERSE_CONTINUE; | |
2823 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
2824 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
2825 | break; | |
2826 | return TRAVERSE_CONTINUE; | |
2827 | default: | |
2828 | return TRAVERSE_CONTINUE; | |
2829 | } | |
2830 | this->found_ = true; | |
2831 | return TRAVERSE_EXIT; | |
2832 | } | |
2833 | ||
e440a328 | 2834 | // The nil value. |
2835 | ||
2836 | class Nil_expression : public Expression | |
2837 | { | |
2838 | public: | |
2839 | Nil_expression(source_location location) | |
2840 | : Expression(EXPRESSION_NIL, location) | |
2841 | { } | |
2842 | ||
2843 | static Expression* | |
2844 | do_import(Import*); | |
2845 | ||
2846 | protected: | |
2847 | bool | |
2848 | do_is_constant() const | |
2849 | { return true; } | |
2850 | ||
2851 | Type* | |
2852 | do_type() | |
2853 | { return Type::make_nil_type(); } | |
2854 | ||
2855 | void | |
2856 | do_determine_type(const Type_context*) | |
2857 | { } | |
2858 | ||
2859 | Expression* | |
2860 | do_copy() | |
2861 | { return this; } | |
2862 | ||
2863 | tree | |
2864 | do_get_tree(Translate_context*) | |
2865 | { return null_pointer_node; } | |
2866 | ||
2867 | void | |
2868 | do_export(Export* exp) const | |
2869 | { exp->write_c_string("nil"); } | |
2870 | }; | |
2871 | ||
2872 | // Import a nil expression. | |
2873 | ||
2874 | Expression* | |
2875 | Nil_expression::do_import(Import* imp) | |
2876 | { | |
2877 | imp->require_c_string("nil"); | |
2878 | return Expression::make_nil(imp->location()); | |
2879 | } | |
2880 | ||
2881 | // Make a nil expression. | |
2882 | ||
2883 | Expression* | |
2884 | Expression::make_nil(source_location location) | |
2885 | { | |
2886 | return new Nil_expression(location); | |
2887 | } | |
2888 | ||
2889 | // The value of the predeclared constant iota. This is little more | |
2890 | // than a marker. This will be lowered to an integer in | |
2891 | // Const_expression::do_lower, which is where we know the value that | |
2892 | // it should have. | |
2893 | ||
2894 | class Iota_expression : public Parser_expression | |
2895 | { | |
2896 | public: | |
2897 | Iota_expression(source_location location) | |
2898 | : Parser_expression(EXPRESSION_IOTA, location) | |
2899 | { } | |
2900 | ||
2901 | protected: | |
2902 | Expression* | |
2903 | do_lower(Gogo*, Named_object*, int) | |
2904 | { gcc_unreachable(); } | |
2905 | ||
2906 | // There should only ever be one of these. | |
2907 | Expression* | |
2908 | do_copy() | |
2909 | { gcc_unreachable(); } | |
2910 | }; | |
2911 | ||
2912 | // Make an iota expression. This is only called for one case: the | |
2913 | // value of the predeclared constant iota. | |
2914 | ||
2915 | Expression* | |
2916 | Expression::make_iota() | |
2917 | { | |
2918 | static Iota_expression iota_expression(UNKNOWN_LOCATION); | |
2919 | return &iota_expression; | |
2920 | } | |
2921 | ||
2922 | // A type conversion expression. | |
2923 | ||
2924 | class Type_conversion_expression : public Expression | |
2925 | { | |
2926 | public: | |
2927 | Type_conversion_expression(Type* type, Expression* expr, | |
2928 | source_location location) | |
2929 | : Expression(EXPRESSION_CONVERSION, location), | |
2930 | type_(type), expr_(expr), may_convert_function_types_(false) | |
2931 | { } | |
2932 | ||
2933 | // Return the type to which we are converting. | |
2934 | Type* | |
2935 | type() const | |
2936 | { return this->type_; } | |
2937 | ||
2938 | // Return the expression which we are converting. | |
2939 | Expression* | |
2940 | expr() const | |
2941 | { return this->expr_; } | |
2942 | ||
2943 | // Permit converting from one function type to another. This is | |
2944 | // used internally for method expressions. | |
2945 | void | |
2946 | set_may_convert_function_types() | |
2947 | { | |
2948 | this->may_convert_function_types_ = true; | |
2949 | } | |
2950 | ||
2951 | // Import a type conversion expression. | |
2952 | static Expression* | |
2953 | do_import(Import*); | |
2954 | ||
2955 | protected: | |
2956 | int | |
2957 | do_traverse(Traverse* traverse); | |
2958 | ||
2959 | Expression* | |
2960 | do_lower(Gogo*, Named_object*, int); | |
2961 | ||
2962 | bool | |
2963 | do_is_constant() const | |
2964 | { return this->expr_->is_constant(); } | |
2965 | ||
2966 | bool | |
2967 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
2968 | ||
2969 | bool | |
2970 | do_float_constant_value(mpfr_t, Type**) const; | |
2971 | ||
2972 | bool | |
2973 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
2974 | ||
2975 | bool | |
2976 | do_string_constant_value(std::string*) const; | |
2977 | ||
2978 | Type* | |
2979 | do_type() | |
2980 | { return this->type_; } | |
2981 | ||
2982 | void | |
2983 | do_determine_type(const Type_context*) | |
2984 | { | |
2985 | Type_context subcontext(this->type_, false); | |
2986 | this->expr_->determine_type(&subcontext); | |
2987 | } | |
2988 | ||
2989 | void | |
2990 | do_check_types(Gogo*); | |
2991 | ||
2992 | Expression* | |
2993 | do_copy() | |
2994 | { | |
2995 | return new Type_conversion_expression(this->type_, this->expr_->copy(), | |
2996 | this->location()); | |
2997 | } | |
2998 | ||
2999 | tree | |
3000 | do_get_tree(Translate_context* context); | |
3001 | ||
3002 | void | |
3003 | do_export(Export*) const; | |
3004 | ||
3005 | private: | |
3006 | // The type to convert to. | |
3007 | Type* type_; | |
3008 | // The expression to convert. | |
3009 | Expression* expr_; | |
3010 | // True if this is permitted to convert function types. This is | |
3011 | // used internally for method expressions. | |
3012 | bool may_convert_function_types_; | |
3013 | }; | |
3014 | ||
3015 | // Traversal. | |
3016 | ||
3017 | int | |
3018 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
3019 | { | |
3020 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3021 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3022 | return TRAVERSE_EXIT; | |
3023 | return TRAVERSE_CONTINUE; | |
3024 | } | |
3025 | ||
3026 | // Convert to a constant at lowering time. | |
3027 | ||
3028 | Expression* | |
3029 | Type_conversion_expression::do_lower(Gogo*, Named_object*, int) | |
3030 | { | |
3031 | Type* type = this->type_; | |
3032 | Expression* val = this->expr_; | |
3033 | source_location location = this->location(); | |
3034 | ||
3035 | if (type->integer_type() != NULL) | |
3036 | { | |
3037 | mpz_t ival; | |
3038 | mpz_init(ival); | |
3039 | Type* dummy; | |
3040 | if (val->integer_constant_value(false, ival, &dummy)) | |
3041 | { | |
3042 | if (!Integer_expression::check_constant(ival, type, location)) | |
3043 | mpz_set_ui(ival, 0); | |
3044 | Expression* ret = Expression::make_integer(&ival, type, location); | |
3045 | mpz_clear(ival); | |
3046 | return ret; | |
3047 | } | |
3048 | ||
3049 | mpfr_t fval; | |
3050 | mpfr_init(fval); | |
3051 | if (val->float_constant_value(fval, &dummy)) | |
3052 | { | |
3053 | if (!mpfr_integer_p(fval)) | |
3054 | { | |
3055 | error_at(location, | |
3056 | "floating point constant truncated to integer"); | |
3057 | return Expression::make_error(location); | |
3058 | } | |
3059 | mpfr_get_z(ival, fval, GMP_RNDN); | |
3060 | if (!Integer_expression::check_constant(ival, type, location)) | |
3061 | mpz_set_ui(ival, 0); | |
3062 | Expression* ret = Expression::make_integer(&ival, type, location); | |
3063 | mpfr_clear(fval); | |
3064 | mpz_clear(ival); | |
3065 | return ret; | |
3066 | } | |
3067 | mpfr_clear(fval); | |
3068 | mpz_clear(ival); | |
3069 | } | |
3070 | ||
3071 | if (type->float_type() != NULL) | |
3072 | { | |
3073 | mpfr_t fval; | |
3074 | mpfr_init(fval); | |
3075 | Type* dummy; | |
3076 | if (val->float_constant_value(fval, &dummy)) | |
3077 | { | |
3078 | if (!Float_expression::check_constant(fval, type, location)) | |
3079 | mpfr_set_ui(fval, 0, GMP_RNDN); | |
3080 | Float_expression::constrain_float(fval, type); | |
3081 | Expression *ret = Expression::make_float(&fval, type, location); | |
3082 | mpfr_clear(fval); | |
3083 | return ret; | |
3084 | } | |
3085 | mpfr_clear(fval); | |
3086 | } | |
3087 | ||
3088 | if (type->complex_type() != NULL) | |
3089 | { | |
3090 | mpfr_t real; | |
3091 | mpfr_t imag; | |
3092 | mpfr_init(real); | |
3093 | mpfr_init(imag); | |
3094 | Type* dummy; | |
3095 | if (val->complex_constant_value(real, imag, &dummy)) | |
3096 | { | |
3097 | if (!Complex_expression::check_constant(real, imag, type, location)) | |
3098 | { | |
3099 | mpfr_set_ui(real, 0, GMP_RNDN); | |
3100 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
3101 | } | |
3102 | Complex_expression::constrain_complex(real, imag, type); | |
3103 | Expression* ret = Expression::make_complex(&real, &imag, type, | |
3104 | location); | |
3105 | mpfr_clear(real); | |
3106 | mpfr_clear(imag); | |
3107 | return ret; | |
3108 | } | |
3109 | mpfr_clear(real); | |
3110 | mpfr_clear(imag); | |
3111 | } | |
3112 | ||
3113 | if (type->is_open_array_type() && type->named_type() == NULL) | |
3114 | { | |
3115 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
3116 | bool is_byte = element_type == Type::lookup_integer_type("uint8"); | |
3117 | bool is_int = element_type == Type::lookup_integer_type("int"); | |
3118 | if (is_byte || is_int) | |
3119 | { | |
3120 | std::string s; | |
3121 | if (val->string_constant_value(&s)) | |
3122 | { | |
3123 | Expression_list* vals = new Expression_list(); | |
3124 | if (is_byte) | |
3125 | { | |
3126 | for (std::string::const_iterator p = s.begin(); | |
3127 | p != s.end(); | |
3128 | p++) | |
3129 | { | |
3130 | mpz_t val; | |
3131 | mpz_init_set_ui(val, static_cast<unsigned char>(*p)); | |
3132 | Expression* v = Expression::make_integer(&val, | |
3133 | element_type, | |
3134 | location); | |
3135 | vals->push_back(v); | |
3136 | mpz_clear(val); | |
3137 | } | |
3138 | } | |
3139 | else | |
3140 | { | |
3141 | const char *p = s.data(); | |
3142 | const char *pend = s.data() + s.length(); | |
3143 | while (p < pend) | |
3144 | { | |
3145 | unsigned int c; | |
3146 | int adv = Lex::fetch_char(p, &c); | |
3147 | if (adv == 0) | |
3148 | { | |
3149 | warning_at(this->location(), 0, | |
3150 | "invalid UTF-8 encoding"); | |
3151 | adv = 1; | |
3152 | } | |
3153 | p += adv; | |
3154 | mpz_t val; | |
3155 | mpz_init_set_ui(val, c); | |
3156 | Expression* v = Expression::make_integer(&val, | |
3157 | element_type, | |
3158 | location); | |
3159 | vals->push_back(v); | |
3160 | mpz_clear(val); | |
3161 | } | |
3162 | } | |
3163 | ||
3164 | return Expression::make_slice_composite_literal(type, vals, | |
3165 | location); | |
3166 | } | |
3167 | } | |
3168 | } | |
3169 | ||
3170 | return this; | |
3171 | } | |
3172 | ||
3173 | // Return the constant integer value if there is one. | |
3174 | ||
3175 | bool | |
3176 | Type_conversion_expression::do_integer_constant_value(bool iota_is_constant, | |
3177 | mpz_t val, | |
3178 | Type** ptype) const | |
3179 | { | |
3180 | if (this->type_->integer_type() == NULL) | |
3181 | return false; | |
3182 | ||
3183 | mpz_t ival; | |
3184 | mpz_init(ival); | |
3185 | Type* dummy; | |
3186 | if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy)) | |
3187 | { | |
3188 | if (!Integer_expression::check_constant(ival, this->type_, | |
3189 | this->location())) | |
3190 | { | |
3191 | mpz_clear(ival); | |
3192 | return false; | |
3193 | } | |
3194 | mpz_set(val, ival); | |
3195 | mpz_clear(ival); | |
3196 | *ptype = this->type_; | |
3197 | return true; | |
3198 | } | |
3199 | mpz_clear(ival); | |
3200 | ||
3201 | mpfr_t fval; | |
3202 | mpfr_init(fval); | |
3203 | if (this->expr_->float_constant_value(fval, &dummy)) | |
3204 | { | |
3205 | mpfr_get_z(val, fval, GMP_RNDN); | |
3206 | mpfr_clear(fval); | |
3207 | if (!Integer_expression::check_constant(val, this->type_, | |
3208 | this->location())) | |
3209 | return false; | |
3210 | *ptype = this->type_; | |
3211 | return true; | |
3212 | } | |
3213 | mpfr_clear(fval); | |
3214 | ||
3215 | return false; | |
3216 | } | |
3217 | ||
3218 | // Return the constant floating point value if there is one. | |
3219 | ||
3220 | bool | |
3221 | Type_conversion_expression::do_float_constant_value(mpfr_t val, | |
3222 | Type** ptype) const | |
3223 | { | |
3224 | if (this->type_->float_type() == NULL) | |
3225 | return false; | |
3226 | ||
3227 | mpfr_t fval; | |
3228 | mpfr_init(fval); | |
3229 | Type* dummy; | |
3230 | if (this->expr_->float_constant_value(fval, &dummy)) | |
3231 | { | |
3232 | if (!Float_expression::check_constant(fval, this->type_, | |
3233 | this->location())) | |
3234 | { | |
3235 | mpfr_clear(fval); | |
3236 | return false; | |
3237 | } | |
3238 | mpfr_set(val, fval, GMP_RNDN); | |
3239 | mpfr_clear(fval); | |
3240 | Float_expression::constrain_float(val, this->type_); | |
3241 | *ptype = this->type_; | |
3242 | return true; | |
3243 | } | |
3244 | mpfr_clear(fval); | |
3245 | ||
3246 | return false; | |
3247 | } | |
3248 | ||
3249 | // Return the constant complex value if there is one. | |
3250 | ||
3251 | bool | |
3252 | Type_conversion_expression::do_complex_constant_value(mpfr_t real, | |
3253 | mpfr_t imag, | |
3254 | Type **ptype) const | |
3255 | { | |
3256 | if (this->type_->complex_type() == NULL) | |
3257 | return false; | |
3258 | ||
3259 | mpfr_t rval; | |
3260 | mpfr_t ival; | |
3261 | mpfr_init(rval); | |
3262 | mpfr_init(ival); | |
3263 | Type* dummy; | |
3264 | if (this->expr_->complex_constant_value(rval, ival, &dummy)) | |
3265 | { | |
3266 | if (!Complex_expression::check_constant(rval, ival, this->type_, | |
3267 | this->location())) | |
3268 | { | |
3269 | mpfr_clear(rval); | |
3270 | mpfr_clear(ival); | |
3271 | return false; | |
3272 | } | |
3273 | mpfr_set(real, rval, GMP_RNDN); | |
3274 | mpfr_set(imag, ival, GMP_RNDN); | |
3275 | mpfr_clear(rval); | |
3276 | mpfr_clear(ival); | |
3277 | Complex_expression::constrain_complex(real, imag, this->type_); | |
3278 | *ptype = this->type_; | |
3279 | return true; | |
3280 | } | |
3281 | mpfr_clear(rval); | |
3282 | mpfr_clear(ival); | |
3283 | ||
3284 | return false; | |
3285 | } | |
3286 | ||
3287 | // Return the constant string value if there is one. | |
3288 | ||
3289 | bool | |
3290 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3291 | { | |
3292 | if (this->type_->is_string_type() | |
3293 | && this->expr_->type()->integer_type() != NULL) | |
3294 | { | |
3295 | mpz_t ival; | |
3296 | mpz_init(ival); | |
3297 | Type* dummy; | |
3298 | if (this->expr_->integer_constant_value(false, ival, &dummy)) | |
3299 | { | |
3300 | unsigned long ulval = mpz_get_ui(ival); | |
3301 | if (mpz_cmp_ui(ival, ulval) == 0) | |
3302 | { | |
3303 | Lex::append_char(ulval, true, val, this->location()); | |
3304 | mpz_clear(ival); | |
3305 | return true; | |
3306 | } | |
3307 | } | |
3308 | mpz_clear(ival); | |
3309 | } | |
3310 | ||
3311 | // FIXME: Could handle conversion from const []int here. | |
3312 | ||
3313 | return false; | |
3314 | } | |
3315 | ||
3316 | // Check that types are convertible. | |
3317 | ||
3318 | void | |
3319 | Type_conversion_expression::do_check_types(Gogo*) | |
3320 | { | |
3321 | Type* type = this->type_; | |
3322 | Type* expr_type = this->expr_->type(); | |
3323 | std::string reason; | |
3324 | ||
842f6425 | 3325 | if (type->is_error_type() |
3326 | || type->is_undefined() | |
3327 | || expr_type->is_error_type() | |
3328 | || expr_type->is_undefined()) | |
3329 | { | |
3330 | // Make sure we emit an error for an undefined type. | |
3331 | type->base(); | |
3332 | expr_type->base(); | |
3333 | this->set_is_error(); | |
3334 | return; | |
3335 | } | |
3336 | ||
e440a328 | 3337 | if (this->may_convert_function_types_ |
3338 | && type->function_type() != NULL | |
3339 | && expr_type->function_type() != NULL) | |
3340 | return; | |
3341 | ||
3342 | if (Type::are_convertible(type, expr_type, &reason)) | |
3343 | return; | |
3344 | ||
3345 | error_at(this->location(), "%s", reason.c_str()); | |
3346 | this->set_is_error(); | |
3347 | } | |
3348 | ||
3349 | // Get a tree for a type conversion. | |
3350 | ||
3351 | tree | |
3352 | Type_conversion_expression::do_get_tree(Translate_context* context) | |
3353 | { | |
3354 | Gogo* gogo = context->gogo(); | |
3355 | tree type_tree = this->type_->get_tree(gogo); | |
3356 | tree expr_tree = this->expr_->get_tree(context); | |
3357 | ||
3358 | if (type_tree == error_mark_node | |
3359 | || expr_tree == error_mark_node | |
3360 | || TREE_TYPE(expr_tree) == error_mark_node) | |
3361 | return error_mark_node; | |
3362 | ||
3363 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree))) | |
3364 | return fold_convert(type_tree, expr_tree); | |
3365 | ||
3366 | Type* type = this->type_; | |
3367 | Type* expr_type = this->expr_->type(); | |
3368 | tree ret; | |
3369 | if (type->interface_type() != NULL || expr_type->interface_type() != NULL) | |
3370 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3371 | expr_tree, this->location()); | |
3372 | else if (type->integer_type() != NULL) | |
3373 | { | |
3374 | if (expr_type->integer_type() != NULL | |
3375 | || expr_type->float_type() != NULL | |
3376 | || expr_type->is_unsafe_pointer_type()) | |
3377 | ret = fold(convert_to_integer(type_tree, expr_tree)); | |
3378 | else | |
3379 | gcc_unreachable(); | |
3380 | } | |
3381 | else if (type->float_type() != NULL) | |
3382 | { | |
3383 | if (expr_type->integer_type() != NULL | |
3384 | || expr_type->float_type() != NULL) | |
3385 | ret = fold(convert_to_real(type_tree, expr_tree)); | |
3386 | else | |
3387 | gcc_unreachable(); | |
3388 | } | |
3389 | else if (type->complex_type() != NULL) | |
3390 | { | |
3391 | if (expr_type->complex_type() != NULL) | |
3392 | ret = fold(convert_to_complex(type_tree, expr_tree)); | |
3393 | else | |
3394 | gcc_unreachable(); | |
3395 | } | |
3396 | else if (type->is_string_type() | |
3397 | && expr_type->integer_type() != NULL) | |
3398 | { | |
3399 | expr_tree = fold_convert(integer_type_node, expr_tree); | |
3400 | if (host_integerp(expr_tree, 0)) | |
3401 | { | |
3402 | HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0); | |
3403 | std::string s; | |
3404 | Lex::append_char(intval, true, &s, this->location()); | |
3405 | Expression* se = Expression::make_string(s, this->location()); | |
3406 | return se->get_tree(context); | |
3407 | } | |
3408 | ||
3409 | static tree int_to_string_fndecl; | |
3410 | ret = Gogo::call_builtin(&int_to_string_fndecl, | |
3411 | this->location(), | |
3412 | "__go_int_to_string", | |
3413 | 1, | |
3414 | type_tree, | |
3415 | integer_type_node, | |
3416 | fold_convert(integer_type_node, expr_tree)); | |
3417 | } | |
3418 | else if (type->is_string_type() | |
3419 | && (expr_type->array_type() != NULL | |
3420 | || (expr_type->points_to() != NULL | |
3421 | && expr_type->points_to()->array_type() != NULL))) | |
3422 | { | |
3423 | Type* t = expr_type; | |
3424 | if (t->points_to() != NULL) | |
3425 | { | |
3426 | t = t->points_to(); | |
3427 | expr_tree = build_fold_indirect_ref(expr_tree); | |
3428 | } | |
3429 | if (!DECL_P(expr_tree)) | |
3430 | expr_tree = save_expr(expr_tree); | |
3431 | Array_type* a = t->array_type(); | |
3432 | Type* e = a->element_type()->forwarded(); | |
3433 | gcc_assert(e->integer_type() != NULL); | |
3434 | tree valptr = fold_convert(const_ptr_type_node, | |
3435 | a->value_pointer_tree(gogo, expr_tree)); | |
3436 | tree len = a->length_tree(gogo, expr_tree); | |
3437 | len = fold_convert_loc(this->location(), size_type_node, len); | |
3438 | if (e->integer_type()->is_unsigned() | |
3439 | && e->integer_type()->bits() == 8) | |
3440 | { | |
3441 | static tree byte_array_to_string_fndecl; | |
3442 | ret = Gogo::call_builtin(&byte_array_to_string_fndecl, | |
3443 | this->location(), | |
3444 | "__go_byte_array_to_string", | |
3445 | 2, | |
3446 | type_tree, | |
3447 | const_ptr_type_node, | |
3448 | valptr, | |
3449 | size_type_node, | |
3450 | len); | |
3451 | } | |
3452 | else | |
3453 | { | |
3454 | gcc_assert(e == Type::lookup_integer_type("int")); | |
3455 | static tree int_array_to_string_fndecl; | |
3456 | ret = Gogo::call_builtin(&int_array_to_string_fndecl, | |
3457 | this->location(), | |
3458 | "__go_int_array_to_string", | |
3459 | 2, | |
3460 | type_tree, | |
3461 | const_ptr_type_node, | |
3462 | valptr, | |
3463 | size_type_node, | |
3464 | len); | |
3465 | } | |
3466 | } | |
3467 | else if (type->is_open_array_type() && expr_type->is_string_type()) | |
3468 | { | |
3469 | Type* e = type->array_type()->element_type()->forwarded(); | |
3470 | gcc_assert(e->integer_type() != NULL); | |
3471 | if (e->integer_type()->is_unsigned() | |
3472 | && e->integer_type()->bits() == 8) | |
3473 | { | |
3474 | static tree string_to_byte_array_fndecl; | |
3475 | ret = Gogo::call_builtin(&string_to_byte_array_fndecl, | |
3476 | this->location(), | |
3477 | "__go_string_to_byte_array", | |
3478 | 1, | |
3479 | type_tree, | |
3480 | TREE_TYPE(expr_tree), | |
3481 | expr_tree); | |
3482 | } | |
3483 | else | |
3484 | { | |
3485 | gcc_assert(e == Type::lookup_integer_type("int")); | |
3486 | static tree string_to_int_array_fndecl; | |
3487 | ret = Gogo::call_builtin(&string_to_int_array_fndecl, | |
3488 | this->location(), | |
3489 | "__go_string_to_int_array", | |
3490 | 1, | |
3491 | type_tree, | |
3492 | TREE_TYPE(expr_tree), | |
3493 | expr_tree); | |
3494 | } | |
3495 | } | |
3496 | else if ((type->is_unsafe_pointer_type() | |
3497 | && expr_type->points_to() != NULL) | |
3498 | || (expr_type->is_unsafe_pointer_type() | |
3499 | && type->points_to() != NULL)) | |
3500 | ret = fold_convert(type_tree, expr_tree); | |
3501 | else if (type->is_unsafe_pointer_type() | |
3502 | && expr_type->integer_type() != NULL) | |
3503 | ret = convert_to_pointer(type_tree, expr_tree); | |
3504 | else if (this->may_convert_function_types_ | |
3505 | && type->function_type() != NULL | |
3506 | && expr_type->function_type() != NULL) | |
3507 | ret = fold_convert_loc(this->location(), type_tree, expr_tree); | |
3508 | else | |
3509 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3510 | expr_tree, this->location()); | |
3511 | ||
3512 | return ret; | |
3513 | } | |
3514 | ||
3515 | // Output a type conversion in a constant expression. | |
3516 | ||
3517 | void | |
3518 | Type_conversion_expression::do_export(Export* exp) const | |
3519 | { | |
3520 | exp->write_c_string("convert("); | |
3521 | exp->write_type(this->type_); | |
3522 | exp->write_c_string(", "); | |
3523 | this->expr_->export_expression(exp); | |
3524 | exp->write_c_string(")"); | |
3525 | } | |
3526 | ||
3527 | // Import a type conversion or a struct construction. | |
3528 | ||
3529 | Expression* | |
3530 | Type_conversion_expression::do_import(Import* imp) | |
3531 | { | |
3532 | imp->require_c_string("convert("); | |
3533 | Type* type = imp->read_type(); | |
3534 | imp->require_c_string(", "); | |
3535 | Expression* val = Expression::import_expression(imp); | |
3536 | imp->require_c_string(")"); | |
3537 | return Expression::make_cast(type, val, imp->location()); | |
3538 | } | |
3539 | ||
3540 | // Make a type cast expression. | |
3541 | ||
3542 | Expression* | |
3543 | Expression::make_cast(Type* type, Expression* val, source_location location) | |
3544 | { | |
3545 | if (type->is_error_type() || val->is_error_expression()) | |
3546 | return Expression::make_error(location); | |
3547 | return new Type_conversion_expression(type, val, location); | |
3548 | } | |
3549 | ||
3550 | // Unary expressions. | |
3551 | ||
3552 | class Unary_expression : public Expression | |
3553 | { | |
3554 | public: | |
3555 | Unary_expression(Operator op, Expression* expr, source_location location) | |
3556 | : Expression(EXPRESSION_UNARY, location), | |
3557 | op_(op), escapes_(true), expr_(expr) | |
3558 | { } | |
3559 | ||
3560 | // Return the operator. | |
3561 | Operator | |
3562 | op() const | |
3563 | { return this->op_; } | |
3564 | ||
3565 | // Return the operand. | |
3566 | Expression* | |
3567 | operand() const | |
3568 | { return this->expr_; } | |
3569 | ||
3570 | // Record that an address expression does not escape. | |
3571 | void | |
3572 | set_does_not_escape() | |
3573 | { | |
3574 | gcc_assert(this->op_ == OPERATOR_AND); | |
3575 | this->escapes_ = false; | |
3576 | } | |
3577 | ||
3578 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3579 | // could be done, false if not. | |
3580 | static bool | |
3581 | eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val, | |
3582 | source_location); | |
3583 | ||
3584 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3585 | // could be done, false if not. | |
3586 | static bool | |
3587 | eval_float(Operator op, mpfr_t uval, mpfr_t val); | |
3588 | ||
3589 | // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return | |
3590 | // true if this could be done, false if not. | |
3591 | static bool | |
3592 | eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real, | |
3593 | mpfr_t imag); | |
3594 | ||
3595 | static Expression* | |
3596 | do_import(Import*); | |
3597 | ||
3598 | protected: | |
3599 | int | |
3600 | do_traverse(Traverse* traverse) | |
3601 | { return Expression::traverse(&this->expr_, traverse); } | |
3602 | ||
3603 | Expression* | |
3604 | do_lower(Gogo*, Named_object*, int); | |
3605 | ||
3606 | bool | |
3607 | do_is_constant() const; | |
3608 | ||
3609 | bool | |
3610 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
3611 | ||
3612 | bool | |
3613 | do_float_constant_value(mpfr_t, Type**) const; | |
3614 | ||
3615 | bool | |
3616 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
3617 | ||
3618 | Type* | |
3619 | do_type(); | |
3620 | ||
3621 | void | |
3622 | do_determine_type(const Type_context*); | |
3623 | ||
3624 | void | |
3625 | do_check_types(Gogo*); | |
3626 | ||
3627 | Expression* | |
3628 | do_copy() | |
3629 | { | |
3630 | return Expression::make_unary(this->op_, this->expr_->copy(), | |
3631 | this->location()); | |
3632 | } | |
3633 | ||
3634 | bool | |
3635 | do_is_addressable() const | |
3636 | { return this->op_ == OPERATOR_MULT; } | |
3637 | ||
3638 | tree | |
3639 | do_get_tree(Translate_context*); | |
3640 | ||
3641 | void | |
3642 | do_export(Export*) const; | |
3643 | ||
3644 | private: | |
3645 | // The unary operator to apply. | |
3646 | Operator op_; | |
3647 | // Normally true. False if this is an address expression which does | |
3648 | // not escape the current function. | |
3649 | bool escapes_; | |
3650 | // The operand. | |
3651 | Expression* expr_; | |
3652 | }; | |
3653 | ||
3654 | // If we are taking the address of a composite literal, and the | |
3655 | // contents are not constant, then we want to make a heap composite | |
3656 | // instead. | |
3657 | ||
3658 | Expression* | |
3659 | Unary_expression::do_lower(Gogo*, Named_object*, int) | |
3660 | { | |
3661 | source_location loc = this->location(); | |
3662 | Operator op = this->op_; | |
3663 | Expression* expr = this->expr_; | |
3664 | ||
3665 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3666 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3667 | ||
3668 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3669 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3670 | // analysis here? This case is found in math/unsafe.go and is | |
3671 | // therefore worth special casing. | |
3672 | if (op == OPERATOR_MULT) | |
3673 | { | |
3674 | Expression* e = expr; | |
3675 | while (e->classification() == EXPRESSION_CONVERSION) | |
3676 | { | |
3677 | Type_conversion_expression* te | |
3678 | = static_cast<Type_conversion_expression*>(e); | |
3679 | e = te->expr(); | |
3680 | } | |
3681 | ||
3682 | if (e->classification() == EXPRESSION_UNARY) | |
3683 | { | |
3684 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3685 | if (ue->op_ == OPERATOR_AND) | |
3686 | { | |
3687 | if (e == expr) | |
3688 | { | |
3689 | // *&x == x. | |
3690 | return ue->expr_; | |
3691 | } | |
3692 | ue->set_does_not_escape(); | |
3693 | } | |
3694 | } | |
3695 | } | |
3696 | ||
3697 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS | |
3698 | || op == OPERATOR_NOT || op == OPERATOR_XOR) | |
3699 | { | |
3700 | Expression* ret = NULL; | |
3701 | ||
3702 | mpz_t eval; | |
3703 | mpz_init(eval); | |
3704 | Type* etype; | |
3705 | if (expr->integer_constant_value(false, eval, &etype)) | |
3706 | { | |
3707 | mpz_t val; | |
3708 | mpz_init(val); | |
3709 | if (Unary_expression::eval_integer(op, etype, eval, val, loc)) | |
3710 | ret = Expression::make_integer(&val, etype, loc); | |
3711 | mpz_clear(val); | |
3712 | } | |
3713 | mpz_clear(eval); | |
3714 | if (ret != NULL) | |
3715 | return ret; | |
3716 | ||
3717 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS) | |
3718 | { | |
3719 | mpfr_t fval; | |
3720 | mpfr_init(fval); | |
3721 | Type* ftype; | |
3722 | if (expr->float_constant_value(fval, &ftype)) | |
3723 | { | |
3724 | mpfr_t val; | |
3725 | mpfr_init(val); | |
3726 | if (Unary_expression::eval_float(op, fval, val)) | |
3727 | ret = Expression::make_float(&val, ftype, loc); | |
3728 | mpfr_clear(val); | |
3729 | } | |
3730 | if (ret != NULL) | |
3731 | { | |
3732 | mpfr_clear(fval); | |
3733 | return ret; | |
3734 | } | |
3735 | ||
3736 | mpfr_t ival; | |
3737 | mpfr_init(ival); | |
3738 | if (expr->complex_constant_value(fval, ival, &ftype)) | |
3739 | { | |
3740 | mpfr_t real; | |
3741 | mpfr_t imag; | |
3742 | mpfr_init(real); | |
3743 | mpfr_init(imag); | |
3744 | if (Unary_expression::eval_complex(op, fval, ival, real, imag)) | |
3745 | ret = Expression::make_complex(&real, &imag, ftype, loc); | |
3746 | mpfr_clear(real); | |
3747 | mpfr_clear(imag); | |
3748 | } | |
3749 | mpfr_clear(ival); | |
3750 | mpfr_clear(fval); | |
3751 | if (ret != NULL) | |
3752 | return ret; | |
3753 | } | |
3754 | } | |
3755 | ||
3756 | return this; | |
3757 | } | |
3758 | ||
3759 | // Return whether a unary expression is a constant. | |
3760 | ||
3761 | bool | |
3762 | Unary_expression::do_is_constant() const | |
3763 | { | |
3764 | if (this->op_ == OPERATOR_MULT) | |
3765 | { | |
3766 | // Indirecting through a pointer is only constant if the object | |
3767 | // to which the expression points is constant, but we currently | |
3768 | // have no way to determine that. | |
3769 | return false; | |
3770 | } | |
3771 | else if (this->op_ == OPERATOR_AND) | |
3772 | { | |
3773 | // Taking the address of a variable is constant if it is a | |
3774 | // global variable, not constant otherwise. In other cases | |
3775 | // taking the address is probably not a constant. | |
3776 | Var_expression* ve = this->expr_->var_expression(); | |
3777 | if (ve != NULL) | |
3778 | { | |
3779 | Named_object* no = ve->named_object(); | |
3780 | return no->is_variable() && no->var_value()->is_global(); | |
3781 | } | |
3782 | return false; | |
3783 | } | |
3784 | else | |
3785 | return this->expr_->is_constant(); | |
3786 | } | |
3787 | ||
3788 | // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of | |
3789 | // UVAL, if known; it may be NULL. Return true if this could be done, | |
3790 | // false if not. | |
3791 | ||
3792 | bool | |
3793 | Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val, | |
3794 | source_location location) | |
3795 | { | |
3796 | switch (op) | |
3797 | { | |
3798 | case OPERATOR_PLUS: | |
3799 | mpz_set(val, uval); | |
3800 | return true; | |
3801 | case OPERATOR_MINUS: | |
3802 | mpz_neg(val, uval); | |
3803 | return Integer_expression::check_constant(val, utype, location); | |
3804 | case OPERATOR_NOT: | |
3805 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); | |
3806 | return true; | |
3807 | case OPERATOR_XOR: | |
3808 | if (utype == NULL | |
3809 | || utype->integer_type() == NULL | |
3810 | || utype->integer_type()->is_abstract()) | |
3811 | mpz_com(val, uval); | |
3812 | else | |
3813 | { | |
3814 | // The number of HOST_WIDE_INTs that it takes to represent | |
3815 | // UVAL. | |
3816 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3817 | + HOST_BITS_PER_WIDE_INT | |
3818 | - 1) | |
3819 | / HOST_BITS_PER_WIDE_INT); | |
3820 | ||
3821 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; | |
3822 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3823 | ||
3824 | size_t ecount; | |
3825 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
3826 | gcc_assert(ecount <= count); | |
3827 | ||
3828 | // Trim down to the number of words required by the type. | |
3829 | size_t obits = utype->integer_type()->bits(); | |
3830 | if (!utype->integer_type()->is_unsigned()) | |
3831 | ++obits; | |
3832 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
3833 | / HOST_BITS_PER_WIDE_INT); | |
3834 | gcc_assert(ocount <= ocount); | |
3835 | ||
3836 | for (size_t i = 0; i < ocount; ++i) | |
3837 | phwi[i] = ~phwi[i]; | |
3838 | ||
3839 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
3840 | if (clearbits != 0) | |
3841 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
3842 | >> clearbits); | |
3843 | ||
3844 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
3845 | ||
3846 | delete[] phwi; | |
3847 | } | |
3848 | return Integer_expression::check_constant(val, utype, location); | |
3849 | case OPERATOR_AND: | |
3850 | case OPERATOR_MULT: | |
3851 | return false; | |
3852 | default: | |
3853 | gcc_unreachable(); | |
3854 | } | |
3855 | } | |
3856 | ||
3857 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3858 | // could be done, false if not. | |
3859 | ||
3860 | bool | |
3861 | Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val) | |
3862 | { | |
3863 | switch (op) | |
3864 | { | |
3865 | case OPERATOR_PLUS: | |
3866 | mpfr_set(val, uval, GMP_RNDN); | |
3867 | return true; | |
3868 | case OPERATOR_MINUS: | |
3869 | mpfr_neg(val, uval, GMP_RNDN); | |
3870 | return true; | |
3871 | case OPERATOR_NOT: | |
3872 | case OPERATOR_XOR: | |
3873 | case OPERATOR_AND: | |
3874 | case OPERATOR_MULT: | |
3875 | return false; | |
3876 | default: | |
3877 | gcc_unreachable(); | |
3878 | } | |
3879 | } | |
3880 | ||
3881 | // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true | |
3882 | // if this could be done, false if not. | |
3883 | ||
3884 | bool | |
3885 | Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival, | |
3886 | mpfr_t real, mpfr_t imag) | |
3887 | { | |
3888 | switch (op) | |
3889 | { | |
3890 | case OPERATOR_PLUS: | |
3891 | mpfr_set(real, rval, GMP_RNDN); | |
3892 | mpfr_set(imag, ival, GMP_RNDN); | |
3893 | return true; | |
3894 | case OPERATOR_MINUS: | |
3895 | mpfr_neg(real, rval, GMP_RNDN); | |
3896 | mpfr_neg(imag, ival, GMP_RNDN); | |
3897 | return true; | |
3898 | case OPERATOR_NOT: | |
3899 | case OPERATOR_XOR: | |
3900 | case OPERATOR_AND: | |
3901 | case OPERATOR_MULT: | |
3902 | return false; | |
3903 | default: | |
3904 | gcc_unreachable(); | |
3905 | } | |
3906 | } | |
3907 | ||
3908 | // Return the integral constant value of a unary expression, if it has one. | |
3909 | ||
3910 | bool | |
3911 | Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
3912 | Type** ptype) const | |
3913 | { | |
3914 | mpz_t uval; | |
3915 | mpz_init(uval); | |
3916 | bool ret; | |
3917 | if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype)) | |
3918 | ret = false; | |
3919 | else | |
3920 | ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val, | |
3921 | this->location()); | |
3922 | mpz_clear(uval); | |
3923 | return ret; | |
3924 | } | |
3925 | ||
3926 | // Return the floating point constant value of a unary expression, if | |
3927 | // it has one. | |
3928 | ||
3929 | bool | |
3930 | Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
3931 | { | |
3932 | mpfr_t uval; | |
3933 | mpfr_init(uval); | |
3934 | bool ret; | |
3935 | if (!this->expr_->float_constant_value(uval, ptype)) | |
3936 | ret = false; | |
3937 | else | |
3938 | ret = Unary_expression::eval_float(this->op_, uval, val); | |
3939 | mpfr_clear(uval); | |
3940 | return ret; | |
3941 | } | |
3942 | ||
3943 | // Return the complex constant value of a unary expression, if it has | |
3944 | // one. | |
3945 | ||
3946 | bool | |
3947 | Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
3948 | Type** ptype) const | |
3949 | { | |
3950 | mpfr_t rval; | |
3951 | mpfr_t ival; | |
3952 | mpfr_init(rval); | |
3953 | mpfr_init(ival); | |
3954 | bool ret; | |
3955 | if (!this->expr_->complex_constant_value(rval, ival, ptype)) | |
3956 | ret = false; | |
3957 | else | |
3958 | ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag); | |
3959 | mpfr_clear(rval); | |
3960 | mpfr_clear(ival); | |
3961 | return ret; | |
3962 | } | |
3963 | ||
3964 | // Return the type of a unary expression. | |
3965 | ||
3966 | Type* | |
3967 | Unary_expression::do_type() | |
3968 | { | |
3969 | switch (this->op_) | |
3970 | { | |
3971 | case OPERATOR_PLUS: | |
3972 | case OPERATOR_MINUS: | |
3973 | case OPERATOR_NOT: | |
3974 | case OPERATOR_XOR: | |
3975 | return this->expr_->type(); | |
3976 | ||
3977 | case OPERATOR_AND: | |
3978 | return Type::make_pointer_type(this->expr_->type()); | |
3979 | ||
3980 | case OPERATOR_MULT: | |
3981 | { | |
3982 | Type* subtype = this->expr_->type(); | |
3983 | Type* points_to = subtype->points_to(); | |
3984 | if (points_to == NULL) | |
3985 | return Type::make_error_type(); | |
3986 | return points_to; | |
3987 | } | |
3988 | ||
3989 | default: | |
3990 | gcc_unreachable(); | |
3991 | } | |
3992 | } | |
3993 | ||
3994 | // Determine abstract types for a unary expression. | |
3995 | ||
3996 | void | |
3997 | Unary_expression::do_determine_type(const Type_context* context) | |
3998 | { | |
3999 | switch (this->op_) | |
4000 | { | |
4001 | case OPERATOR_PLUS: | |
4002 | case OPERATOR_MINUS: | |
4003 | case OPERATOR_NOT: | |
4004 | case OPERATOR_XOR: | |
4005 | this->expr_->determine_type(context); | |
4006 | break; | |
4007 | ||
4008 | case OPERATOR_AND: | |
4009 | // Taking the address of something. | |
4010 | { | |
4011 | Type* subtype = (context->type == NULL | |
4012 | ? NULL | |
4013 | : context->type->points_to()); | |
4014 | Type_context subcontext(subtype, false); | |
4015 | this->expr_->determine_type(&subcontext); | |
4016 | } | |
4017 | break; | |
4018 | ||
4019 | case OPERATOR_MULT: | |
4020 | // Indirecting through a pointer. | |
4021 | { | |
4022 | Type* subtype = (context->type == NULL | |
4023 | ? NULL | |
4024 | : Type::make_pointer_type(context->type)); | |
4025 | Type_context subcontext(subtype, false); | |
4026 | this->expr_->determine_type(&subcontext); | |
4027 | } | |
4028 | break; | |
4029 | ||
4030 | default: | |
4031 | gcc_unreachable(); | |
4032 | } | |
4033 | } | |
4034 | ||
4035 | // Check types for a unary expression. | |
4036 | ||
4037 | void | |
4038 | Unary_expression::do_check_types(Gogo*) | |
4039 | { | |
9fe897ef | 4040 | Type* type = this->expr_->type(); |
4041 | if (type->is_error_type()) | |
4042 | { | |
4043 | this->set_is_error(); | |
4044 | return; | |
4045 | } | |
4046 | ||
e440a328 | 4047 | switch (this->op_) |
4048 | { | |
4049 | case OPERATOR_PLUS: | |
4050 | case OPERATOR_MINUS: | |
9fe897ef | 4051 | if (type->integer_type() == NULL |
4052 | && type->float_type() == NULL | |
4053 | && type->complex_type() == NULL) | |
4054 | this->report_error(_("expected numeric type")); | |
e440a328 | 4055 | break; |
4056 | ||
4057 | case OPERATOR_NOT: | |
4058 | case OPERATOR_XOR: | |
9fe897ef | 4059 | if (type->integer_type() == NULL |
4060 | && !type->is_boolean_type()) | |
4061 | this->report_error(_("expected integer or boolean type")); | |
e440a328 | 4062 | break; |
4063 | ||
4064 | case OPERATOR_AND: | |
4065 | if (!this->expr_->is_addressable()) | |
4066 | this->report_error(_("invalid operand for unary %<&%>")); | |
4067 | else | |
4068 | this->expr_->address_taken(this->escapes_); | |
4069 | break; | |
4070 | ||
4071 | case OPERATOR_MULT: | |
4072 | // Indirecting through a pointer. | |
9fe897ef | 4073 | if (type->points_to() == NULL) |
4074 | this->report_error(_("expected pointer")); | |
e440a328 | 4075 | break; |
4076 | ||
4077 | default: | |
4078 | gcc_unreachable(); | |
4079 | } | |
4080 | } | |
4081 | ||
4082 | // Get a tree for a unary expression. | |
4083 | ||
4084 | tree | |
4085 | Unary_expression::do_get_tree(Translate_context* context) | |
4086 | { | |
4087 | tree expr = this->expr_->get_tree(context); | |
4088 | if (expr == error_mark_node) | |
4089 | return error_mark_node; | |
4090 | ||
4091 | source_location loc = this->location(); | |
4092 | switch (this->op_) | |
4093 | { | |
4094 | case OPERATOR_PLUS: | |
4095 | return expr; | |
4096 | ||
4097 | case OPERATOR_MINUS: | |
4098 | { | |
4099 | tree type = TREE_TYPE(expr); | |
4100 | tree compute_type = excess_precision_type(type); | |
4101 | if (compute_type != NULL_TREE) | |
4102 | expr = ::convert(compute_type, expr); | |
4103 | tree ret = fold_build1_loc(loc, NEGATE_EXPR, | |
4104 | (compute_type != NULL_TREE | |
4105 | ? compute_type | |
4106 | : type), | |
4107 | expr); | |
4108 | if (compute_type != NULL_TREE) | |
4109 | ret = ::convert(type, ret); | |
4110 | return ret; | |
4111 | } | |
4112 | ||
4113 | case OPERATOR_NOT: | |
4114 | if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE) | |
4115 | return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr); | |
4116 | else | |
4117 | return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr, | |
4118 | build_int_cst(TREE_TYPE(expr), 0)); | |
4119 | ||
4120 | case OPERATOR_XOR: | |
4121 | return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr); | |
4122 | ||
4123 | case OPERATOR_AND: | |
4124 | // We should not see a non-constant constructor here; cases | |
4125 | // where we would see one should have been moved onto the heap | |
4126 | // at parse time. Taking the address of a nonconstant | |
4127 | // constructor will not do what the programmer expects. | |
4128 | gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr)); | |
4129 | gcc_assert(TREE_CODE(expr) != ADDR_EXPR); | |
4130 | ||
4131 | // Build a decl for a constant constructor. | |
4132 | if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr)) | |
4133 | { | |
4134 | tree decl = build_decl(this->location(), VAR_DECL, | |
4135 | create_tmp_var_name("C"), TREE_TYPE(expr)); | |
4136 | DECL_EXTERNAL(decl) = 0; | |
4137 | TREE_PUBLIC(decl) = 0; | |
4138 | TREE_READONLY(decl) = 1; | |
4139 | TREE_CONSTANT(decl) = 1; | |
4140 | TREE_STATIC(decl) = 1; | |
4141 | TREE_ADDRESSABLE(decl) = 1; | |
4142 | DECL_ARTIFICIAL(decl) = 1; | |
4143 | DECL_INITIAL(decl) = expr; | |
4144 | rest_of_decl_compilation(decl, 1, 0); | |
4145 | expr = decl; | |
4146 | } | |
4147 | ||
4148 | return build_fold_addr_expr_loc(loc, expr); | |
4149 | ||
4150 | case OPERATOR_MULT: | |
4151 | { | |
4152 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr))); | |
4153 | ||
4154 | // If we are dereferencing the pointer to a large struct, we | |
4155 | // need to check for nil. We don't bother to check for small | |
4156 | // structs because we expect the system to crash on a nil | |
4157 | // pointer dereference. | |
4158 | HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr))); | |
4159 | if (s == -1 || s >= 4096) | |
4160 | { | |
4161 | if (!DECL_P(expr)) | |
4162 | expr = save_expr(expr); | |
4163 | tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node, | |
4164 | expr, | |
4165 | fold_convert(TREE_TYPE(expr), | |
4166 | null_pointer_node)); | |
4167 | tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, | |
4168 | loc); | |
4169 | expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr), | |
4170 | build3(COND_EXPR, void_type_node, | |
4171 | compare, crash, NULL_TREE), | |
4172 | expr); | |
4173 | } | |
4174 | ||
4175 | // If the type of EXPR is a recursive pointer type, then we | |
4176 | // need to insert a cast before indirecting. | |
4177 | if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node) | |
4178 | { | |
4179 | Type* pt = this->expr_->type()->points_to(); | |
4180 | tree ind = pt->get_tree(context->gogo()); | |
4181 | expr = fold_convert_loc(loc, build_pointer_type(ind), expr); | |
4182 | } | |
4183 | ||
4184 | return build_fold_indirect_ref_loc(loc, expr); | |
4185 | } | |
4186 | ||
4187 | default: | |
4188 | gcc_unreachable(); | |
4189 | } | |
4190 | } | |
4191 | ||
4192 | // Export a unary expression. | |
4193 | ||
4194 | void | |
4195 | Unary_expression::do_export(Export* exp) const | |
4196 | { | |
4197 | switch (this->op_) | |
4198 | { | |
4199 | case OPERATOR_PLUS: | |
4200 | exp->write_c_string("+ "); | |
4201 | break; | |
4202 | case OPERATOR_MINUS: | |
4203 | exp->write_c_string("- "); | |
4204 | break; | |
4205 | case OPERATOR_NOT: | |
4206 | exp->write_c_string("! "); | |
4207 | break; | |
4208 | case OPERATOR_XOR: | |
4209 | exp->write_c_string("^ "); | |
4210 | break; | |
4211 | case OPERATOR_AND: | |
4212 | case OPERATOR_MULT: | |
4213 | default: | |
4214 | gcc_unreachable(); | |
4215 | } | |
4216 | this->expr_->export_expression(exp); | |
4217 | } | |
4218 | ||
4219 | // Import a unary expression. | |
4220 | ||
4221 | Expression* | |
4222 | Unary_expression::do_import(Import* imp) | |
4223 | { | |
4224 | Operator op; | |
4225 | switch (imp->get_char()) | |
4226 | { | |
4227 | case '+': | |
4228 | op = OPERATOR_PLUS; | |
4229 | break; | |
4230 | case '-': | |
4231 | op = OPERATOR_MINUS; | |
4232 | break; | |
4233 | case '!': | |
4234 | op = OPERATOR_NOT; | |
4235 | break; | |
4236 | case '^': | |
4237 | op = OPERATOR_XOR; | |
4238 | break; | |
4239 | default: | |
4240 | gcc_unreachable(); | |
4241 | } | |
4242 | imp->require_c_string(" "); | |
4243 | Expression* expr = Expression::import_expression(imp); | |
4244 | return Expression::make_unary(op, expr, imp->location()); | |
4245 | } | |
4246 | ||
4247 | // Make a unary expression. | |
4248 | ||
4249 | Expression* | |
4250 | Expression::make_unary(Operator op, Expression* expr, source_location location) | |
4251 | { | |
4252 | return new Unary_expression(op, expr, location); | |
4253 | } | |
4254 | ||
4255 | // If this is an indirection through a pointer, return the expression | |
4256 | // being pointed through. Otherwise return this. | |
4257 | ||
4258 | Expression* | |
4259 | Expression::deref() | |
4260 | { | |
4261 | if (this->classification_ == EXPRESSION_UNARY) | |
4262 | { | |
4263 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4264 | if (ue->op() == OPERATOR_MULT) | |
4265 | return ue->operand(); | |
4266 | } | |
4267 | return this; | |
4268 | } | |
4269 | ||
4270 | // Class Binary_expression. | |
4271 | ||
4272 | // Traversal. | |
4273 | ||
4274 | int | |
4275 | Binary_expression::do_traverse(Traverse* traverse) | |
4276 | { | |
4277 | int t = Expression::traverse(&this->left_, traverse); | |
4278 | if (t == TRAVERSE_EXIT) | |
4279 | return TRAVERSE_EXIT; | |
4280 | return Expression::traverse(&this->right_, traverse); | |
4281 | } | |
4282 | ||
4283 | // Compare integer constants according to OP. | |
4284 | ||
4285 | bool | |
4286 | Binary_expression::compare_integer(Operator op, mpz_t left_val, | |
4287 | mpz_t right_val) | |
4288 | { | |
4289 | int i = mpz_cmp(left_val, right_val); | |
4290 | switch (op) | |
4291 | { | |
4292 | case OPERATOR_EQEQ: | |
4293 | return i == 0; | |
4294 | case OPERATOR_NOTEQ: | |
4295 | return i != 0; | |
4296 | case OPERATOR_LT: | |
4297 | return i < 0; | |
4298 | case OPERATOR_LE: | |
4299 | return i <= 0; | |
4300 | case OPERATOR_GT: | |
4301 | return i > 0; | |
4302 | case OPERATOR_GE: | |
4303 | return i >= 0; | |
4304 | default: | |
4305 | gcc_unreachable(); | |
4306 | } | |
4307 | } | |
4308 | ||
4309 | // Compare floating point constants according to OP. | |
4310 | ||
4311 | bool | |
4312 | Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val, | |
4313 | mpfr_t right_val) | |
4314 | { | |
4315 | int i; | |
4316 | if (type == NULL) | |
4317 | i = mpfr_cmp(left_val, right_val); | |
4318 | else | |
4319 | { | |
4320 | mpfr_t lv; | |
4321 | mpfr_init_set(lv, left_val, GMP_RNDN); | |
4322 | mpfr_t rv; | |
4323 | mpfr_init_set(rv, right_val, GMP_RNDN); | |
4324 | Float_expression::constrain_float(lv, type); | |
4325 | Float_expression::constrain_float(rv, type); | |
4326 | i = mpfr_cmp(lv, rv); | |
4327 | mpfr_clear(lv); | |
4328 | mpfr_clear(rv); | |
4329 | } | |
4330 | switch (op) | |
4331 | { | |
4332 | case OPERATOR_EQEQ: | |
4333 | return i == 0; | |
4334 | case OPERATOR_NOTEQ: | |
4335 | return i != 0; | |
4336 | case OPERATOR_LT: | |
4337 | return i < 0; | |
4338 | case OPERATOR_LE: | |
4339 | return i <= 0; | |
4340 | case OPERATOR_GT: | |
4341 | return i > 0; | |
4342 | case OPERATOR_GE: | |
4343 | return i >= 0; | |
4344 | default: | |
4345 | gcc_unreachable(); | |
4346 | } | |
4347 | } | |
4348 | ||
4349 | // Compare complex constants according to OP. Complex numbers may | |
4350 | // only be compared for equality. | |
4351 | ||
4352 | bool | |
4353 | Binary_expression::compare_complex(Operator op, Type* type, | |
4354 | mpfr_t left_real, mpfr_t left_imag, | |
4355 | mpfr_t right_real, mpfr_t right_imag) | |
4356 | { | |
4357 | bool is_equal; | |
4358 | if (type == NULL) | |
4359 | is_equal = (mpfr_cmp(left_real, right_real) == 0 | |
4360 | && mpfr_cmp(left_imag, right_imag) == 0); | |
4361 | else | |
4362 | { | |
4363 | mpfr_t lr; | |
4364 | mpfr_t li; | |
4365 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4366 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4367 | mpfr_t rr; | |
4368 | mpfr_t ri; | |
4369 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4370 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4371 | Complex_expression::constrain_complex(lr, li, type); | |
4372 | Complex_expression::constrain_complex(rr, ri, type); | |
4373 | is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0; | |
4374 | mpfr_clear(lr); | |
4375 | mpfr_clear(li); | |
4376 | mpfr_clear(rr); | |
4377 | mpfr_clear(ri); | |
4378 | } | |
4379 | switch (op) | |
4380 | { | |
4381 | case OPERATOR_EQEQ: | |
4382 | return is_equal; | |
4383 | case OPERATOR_NOTEQ: | |
4384 | return !is_equal; | |
4385 | default: | |
4386 | gcc_unreachable(); | |
4387 | } | |
4388 | } | |
4389 | ||
4390 | // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. | |
4391 | // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of | |
4392 | // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if | |
4393 | // this could be done, false if not. | |
4394 | ||
4395 | bool | |
4396 | Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val, | |
4397 | Type* right_type, mpz_t right_val, | |
4398 | source_location location, mpz_t val) | |
4399 | { | |
4400 | bool is_shift_op = false; | |
4401 | switch (op) | |
4402 | { | |
4403 | case OPERATOR_OROR: | |
4404 | case OPERATOR_ANDAND: | |
4405 | case OPERATOR_EQEQ: | |
4406 | case OPERATOR_NOTEQ: | |
4407 | case OPERATOR_LT: | |
4408 | case OPERATOR_LE: | |
4409 | case OPERATOR_GT: | |
4410 | case OPERATOR_GE: | |
4411 | // These return boolean values. We should probably handle them | |
4412 | // anyhow in case a type conversion is used on the result. | |
4413 | return false; | |
4414 | case OPERATOR_PLUS: | |
4415 | mpz_add(val, left_val, right_val); | |
4416 | break; | |
4417 | case OPERATOR_MINUS: | |
4418 | mpz_sub(val, left_val, right_val); | |
4419 | break; | |
4420 | case OPERATOR_OR: | |
4421 | mpz_ior(val, left_val, right_val); | |
4422 | break; | |
4423 | case OPERATOR_XOR: | |
4424 | mpz_xor(val, left_val, right_val); | |
4425 | break; | |
4426 | case OPERATOR_MULT: | |
4427 | mpz_mul(val, left_val, right_val); | |
4428 | break; | |
4429 | case OPERATOR_DIV: | |
4430 | if (mpz_sgn(right_val) != 0) | |
4431 | mpz_tdiv_q(val, left_val, right_val); | |
4432 | else | |
4433 | { | |
4434 | error_at(location, "division by zero"); | |
4435 | mpz_set_ui(val, 0); | |
4436 | return true; | |
4437 | } | |
4438 | break; | |
4439 | case OPERATOR_MOD: | |
4440 | if (mpz_sgn(right_val) != 0) | |
4441 | mpz_tdiv_r(val, left_val, right_val); | |
4442 | else | |
4443 | { | |
4444 | error_at(location, "division by zero"); | |
4445 | mpz_set_ui(val, 0); | |
4446 | return true; | |
4447 | } | |
4448 | break; | |
4449 | case OPERATOR_LSHIFT: | |
4450 | { | |
4451 | unsigned long shift = mpz_get_ui(right_val); | |
a28c1598 | 4452 | if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000) |
e440a328 | 4453 | { |
4454 | error_at(location, "shift count overflow"); | |
4455 | mpz_set_ui(val, 0); | |
4456 | return true; | |
4457 | } | |
4458 | mpz_mul_2exp(val, left_val, shift); | |
4459 | is_shift_op = true; | |
4460 | break; | |
4461 | } | |
4462 | break; | |
4463 | case OPERATOR_RSHIFT: | |
4464 | { | |
4465 | unsigned long shift = mpz_get_ui(right_val); | |
4466 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4467 | { | |
4468 | error_at(location, "shift count overflow"); | |
4469 | mpz_set_ui(val, 0); | |
4470 | return true; | |
4471 | } | |
4472 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4473 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4474 | else | |
4475 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4476 | is_shift_op = true; | |
4477 | break; | |
4478 | } | |
4479 | break; | |
4480 | case OPERATOR_AND: | |
4481 | mpz_and(val, left_val, right_val); | |
4482 | break; | |
4483 | case OPERATOR_BITCLEAR: | |
4484 | { | |
4485 | mpz_t tval; | |
4486 | mpz_init(tval); | |
4487 | mpz_com(tval, right_val); | |
4488 | mpz_and(val, left_val, tval); | |
4489 | mpz_clear(tval); | |
4490 | } | |
4491 | break; | |
4492 | default: | |
4493 | gcc_unreachable(); | |
4494 | } | |
4495 | ||
4496 | Type* type = left_type; | |
4497 | if (!is_shift_op) | |
4498 | { | |
4499 | if (type == NULL) | |
4500 | type = right_type; | |
4501 | else if (type != right_type && right_type != NULL) | |
4502 | { | |
4503 | if (type->is_abstract()) | |
4504 | type = right_type; | |
4505 | else if (!right_type->is_abstract()) | |
4506 | { | |
4507 | // This look like a type error which should be diagnosed | |
4508 | // elsewhere. Don't do anything here, to avoid an | |
4509 | // unhelpful chain of error messages. | |
4510 | return true; | |
4511 | } | |
4512 | } | |
4513 | } | |
4514 | ||
4515 | if (type != NULL && !type->is_abstract()) | |
4516 | { | |
4517 | // We have to check the operands too, as we have implicitly | |
4518 | // coerced them to TYPE. | |
4519 | if ((type != left_type | |
4520 | && !Integer_expression::check_constant(left_val, type, location)) | |
4521 | || (!is_shift_op | |
4522 | && type != right_type | |
4523 | && !Integer_expression::check_constant(right_val, type, | |
4524 | location)) | |
4525 | || !Integer_expression::check_constant(val, type, location)) | |
4526 | mpz_set_ui(val, 0); | |
4527 | } | |
4528 | ||
4529 | return true; | |
4530 | } | |
4531 | ||
4532 | // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. | |
4533 | // Return true if this could be done, false if not. | |
4534 | ||
4535 | bool | |
4536 | Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val, | |
4537 | Type* right_type, mpfr_t right_val, | |
4538 | mpfr_t val, source_location location) | |
4539 | { | |
4540 | switch (op) | |
4541 | { | |
4542 | case OPERATOR_OROR: | |
4543 | case OPERATOR_ANDAND: | |
4544 | case OPERATOR_EQEQ: | |
4545 | case OPERATOR_NOTEQ: | |
4546 | case OPERATOR_LT: | |
4547 | case OPERATOR_LE: | |
4548 | case OPERATOR_GT: | |
4549 | case OPERATOR_GE: | |
4550 | // These return boolean values. We should probably handle them | |
4551 | // anyhow in case a type conversion is used on the result. | |
4552 | return false; | |
4553 | case OPERATOR_PLUS: | |
4554 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4555 | break; | |
4556 | case OPERATOR_MINUS: | |
4557 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4558 | break; | |
4559 | case OPERATOR_OR: | |
4560 | case OPERATOR_XOR: | |
4561 | case OPERATOR_AND: | |
4562 | case OPERATOR_BITCLEAR: | |
4563 | return false; | |
4564 | case OPERATOR_MULT: | |
4565 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4566 | break; | |
4567 | case OPERATOR_DIV: | |
4568 | if (mpfr_zero_p(right_val)) | |
4569 | error_at(location, "division by zero"); | |
4570 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4571 | break; | |
4572 | case OPERATOR_MOD: | |
4573 | return false; | |
4574 | case OPERATOR_LSHIFT: | |
4575 | case OPERATOR_RSHIFT: | |
4576 | return false; | |
4577 | default: | |
4578 | gcc_unreachable(); | |
4579 | } | |
4580 | ||
4581 | Type* type = left_type; | |
4582 | if (type == NULL) | |
4583 | type = right_type; | |
4584 | else if (type != right_type && right_type != NULL) | |
4585 | { | |
4586 | if (type->is_abstract()) | |
4587 | type = right_type; | |
4588 | else if (!right_type->is_abstract()) | |
4589 | { | |
4590 | // This looks like a type error which should be diagnosed | |
4591 | // elsewhere. Don't do anything here, to avoid an unhelpful | |
4592 | // chain of error messages. | |
4593 | return true; | |
4594 | } | |
4595 | } | |
4596 | ||
4597 | if (type != NULL && !type->is_abstract()) | |
4598 | { | |
4599 | if ((type != left_type | |
4600 | && !Float_expression::check_constant(left_val, type, location)) | |
4601 | || (type != right_type | |
4602 | && !Float_expression::check_constant(right_val, type, | |
4603 | location)) | |
4604 | || !Float_expression::check_constant(val, type, location)) | |
4605 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4606 | } | |
4607 | ||
4608 | return true; | |
4609 | } | |
4610 | ||
4611 | // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and | |
4612 | // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this | |
4613 | // could be done, false if not. | |
4614 | ||
4615 | bool | |
4616 | Binary_expression::eval_complex(Operator op, Type* left_type, | |
4617 | mpfr_t left_real, mpfr_t left_imag, | |
4618 | Type *right_type, | |
4619 | mpfr_t right_real, mpfr_t right_imag, | |
4620 | mpfr_t real, mpfr_t imag, | |
4621 | source_location location) | |
4622 | { | |
4623 | switch (op) | |
4624 | { | |
4625 | case OPERATOR_OROR: | |
4626 | case OPERATOR_ANDAND: | |
4627 | case OPERATOR_EQEQ: | |
4628 | case OPERATOR_NOTEQ: | |
4629 | case OPERATOR_LT: | |
4630 | case OPERATOR_LE: | |
4631 | case OPERATOR_GT: | |
4632 | case OPERATOR_GE: | |
4633 | // These return boolean values and must be handled differently. | |
4634 | return false; | |
4635 | case OPERATOR_PLUS: | |
4636 | mpfr_add(real, left_real, right_real, GMP_RNDN); | |
4637 | mpfr_add(imag, left_imag, right_imag, GMP_RNDN); | |
4638 | break; | |
4639 | case OPERATOR_MINUS: | |
4640 | mpfr_sub(real, left_real, right_real, GMP_RNDN); | |
4641 | mpfr_sub(imag, left_imag, right_imag, GMP_RNDN); | |
4642 | break; | |
4643 | case OPERATOR_OR: | |
4644 | case OPERATOR_XOR: | |
4645 | case OPERATOR_AND: | |
4646 | case OPERATOR_BITCLEAR: | |
4647 | return false; | |
4648 | case OPERATOR_MULT: | |
4649 | { | |
4650 | // You might think that multiplying two complex numbers would | |
4651 | // be simple, and you would be right, until you start to think | |
4652 | // about getting the right answer for infinity. If one | |
4653 | // operand here is infinity and the other is anything other | |
4654 | // than zero or NaN, then we are going to wind up subtracting | |
4655 | // two infinity values. That will give us a NaN, but the | |
4656 | // correct answer is infinity. | |
4657 | ||
4658 | mpfr_t lrrr; | |
4659 | mpfr_init(lrrr); | |
4660 | mpfr_mul(lrrr, left_real, right_real, GMP_RNDN); | |
4661 | ||
4662 | mpfr_t lrri; | |
4663 | mpfr_init(lrri); | |
4664 | mpfr_mul(lrri, left_real, right_imag, GMP_RNDN); | |
4665 | ||
4666 | mpfr_t lirr; | |
4667 | mpfr_init(lirr); | |
4668 | mpfr_mul(lirr, left_imag, right_real, GMP_RNDN); | |
4669 | ||
4670 | mpfr_t liri; | |
4671 | mpfr_init(liri); | |
4672 | mpfr_mul(liri, left_imag, right_imag, GMP_RNDN); | |
4673 | ||
4674 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4675 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4676 | ||
4677 | // If we get NaN on both sides, check whether it should really | |
4678 | // be infinity. The rule is that if either side of the | |
4679 | // complex number is infinity, then the whole value is | |
4680 | // infinity, even if the other side is NaN. So the only case | |
4681 | // we have to fix is the one in which both sides are NaN. | |
4682 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4683 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4684 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4685 | { | |
4686 | bool is_infinity = false; | |
4687 | ||
4688 | mpfr_t lr; | |
4689 | mpfr_t li; | |
4690 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4691 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4692 | ||
4693 | mpfr_t rr; | |
4694 | mpfr_t ri; | |
4695 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4696 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4697 | ||
4698 | // If the left side is infinity, then the result is | |
4699 | // infinity. | |
4700 | if (mpfr_inf_p(lr) || mpfr_inf_p(li)) | |
4701 | { | |
4702 | mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN); | |
4703 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4704 | mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN); | |
4705 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4706 | if (mpfr_nan_p(rr)) | |
4707 | { | |
4708 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4709 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4710 | } | |
4711 | if (mpfr_nan_p(ri)) | |
4712 | { | |
4713 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4714 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4715 | } | |
4716 | is_infinity = true; | |
4717 | } | |
4718 | ||
4719 | // If the right side is infinity, then the result is | |
4720 | // infinity. | |
4721 | if (mpfr_inf_p(rr) || mpfr_inf_p(ri)) | |
4722 | { | |
4723 | mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4724 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4725 | mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
4726 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4727 | if (mpfr_nan_p(lr)) | |
4728 | { | |
4729 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4730 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4731 | } | |
4732 | if (mpfr_nan_p(li)) | |
4733 | { | |
4734 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4735 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4736 | } | |
4737 | is_infinity = true; | |
4738 | } | |
4739 | ||
4740 | // If we got an overflow in the intermediate computations, | |
4741 | // then the result is infinity. | |
4742 | if (!is_infinity | |
4743 | && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri) | |
4744 | || mpfr_inf_p(lirr) || mpfr_inf_p(liri))) | |
4745 | { | |
4746 | if (mpfr_nan_p(lr)) | |
4747 | { | |
4748 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4749 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4750 | } | |
4751 | if (mpfr_nan_p(li)) | |
4752 | { | |
4753 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4754 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4755 | } | |
4756 | if (mpfr_nan_p(rr)) | |
4757 | { | |
4758 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4759 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4760 | } | |
4761 | if (mpfr_nan_p(ri)) | |
4762 | { | |
4763 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4764 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4765 | } | |
4766 | is_infinity = true; | |
4767 | } | |
4768 | ||
4769 | if (is_infinity) | |
4770 | { | |
4771 | mpfr_mul(lrrr, lr, rr, GMP_RNDN); | |
4772 | mpfr_mul(lrri, lr, ri, GMP_RNDN); | |
4773 | mpfr_mul(lirr, li, rr, GMP_RNDN); | |
4774 | mpfr_mul(liri, li, ri, GMP_RNDN); | |
4775 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4776 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4777 | mpfr_set_inf(real, mpfr_sgn(real)); | |
4778 | mpfr_set_inf(imag, mpfr_sgn(imag)); | |
4779 | } | |
4780 | ||
4781 | mpfr_clear(lr); | |
4782 | mpfr_clear(li); | |
4783 | mpfr_clear(rr); | |
4784 | mpfr_clear(ri); | |
4785 | } | |
4786 | ||
4787 | mpfr_clear(lrrr); | |
4788 | mpfr_clear(lrri); | |
4789 | mpfr_clear(lirr); | |
4790 | mpfr_clear(liri); | |
4791 | } | |
4792 | break; | |
4793 | case OPERATOR_DIV: | |
4794 | { | |
4795 | // For complex division we want to avoid having an | |
4796 | // intermediate overflow turn the whole result in a NaN. We | |
4797 | // scale the values to try to avoid this. | |
4798 | ||
4799 | if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag)) | |
4800 | error_at(location, "division by zero"); | |
4801 | ||
4802 | mpfr_t rra; | |
4803 | mpfr_t ria; | |
4804 | mpfr_init(rra); | |
4805 | mpfr_init(ria); | |
4806 | mpfr_abs(rra, right_real, GMP_RNDN); | |
4807 | mpfr_abs(ria, right_imag, GMP_RNDN); | |
4808 | mpfr_t t; | |
4809 | mpfr_init(t); | |
4810 | mpfr_max(t, rra, ria, GMP_RNDN); | |
4811 | ||
4812 | mpfr_t rr; | |
4813 | mpfr_t ri; | |
4814 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4815 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4816 | long ilogbw = 0; | |
4817 | if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t)) | |
4818 | { | |
4819 | ilogbw = mpfr_get_exp(t); | |
4820 | mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN); | |
4821 | mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN); | |
4822 | } | |
4823 | ||
4824 | mpfr_t denom; | |
4825 | mpfr_init(denom); | |
4826 | mpfr_mul(denom, rr, rr, GMP_RNDN); | |
4827 | mpfr_mul(t, ri, ri, GMP_RNDN); | |
4828 | mpfr_add(denom, denom, t, GMP_RNDN); | |
4829 | ||
4830 | mpfr_mul(real, left_real, rr, GMP_RNDN); | |
4831 | mpfr_mul(t, left_imag, ri, GMP_RNDN); | |
4832 | mpfr_add(real, real, t, GMP_RNDN); | |
4833 | mpfr_div(real, real, denom, GMP_RNDN); | |
4834 | mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN); | |
4835 | ||
4836 | mpfr_mul(imag, left_imag, rr, GMP_RNDN); | |
4837 | mpfr_mul(t, left_real, ri, GMP_RNDN); | |
4838 | mpfr_sub(imag, imag, t, GMP_RNDN); | |
4839 | mpfr_div(imag, imag, denom, GMP_RNDN); | |
4840 | mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN); | |
4841 | ||
4842 | // If we wind up with NaN on both sides, check whether we | |
4843 | // should really have infinity. The rule is that if either | |
4844 | // side of the complex number is infinity, then the whole | |
4845 | // value is infinity, even if the other side is NaN. So the | |
4846 | // only case we have to fix is the one in which both sides are | |
4847 | // NaN. | |
4848 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4849 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4850 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4851 | { | |
4852 | if (mpfr_zero_p(denom)) | |
4853 | { | |
4854 | mpfr_set_inf(real, mpfr_sgn(rr)); | |
4855 | mpfr_mul(real, real, left_real, GMP_RNDN); | |
4856 | mpfr_set_inf(imag, mpfr_sgn(rr)); | |
4857 | mpfr_mul(imag, imag, left_imag, GMP_RNDN); | |
4858 | } | |
4859 | else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag)) | |
4860 | && mpfr_number_p(rr) && mpfr_number_p(ri)) | |
4861 | { | |
4862 | mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN); | |
4863 | mpfr_copysign(t, t, left_real, GMP_RNDN); | |
4864 | ||
4865 | mpfr_t t2; | |
4866 | mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN); | |
4867 | mpfr_copysign(t2, t2, left_imag, GMP_RNDN); | |
4868 | ||
4869 | mpfr_t t3; | |
4870 | mpfr_init(t3); | |
4871 | mpfr_mul(t3, t, rr, GMP_RNDN); | |
4872 | ||
4873 | mpfr_t t4; | |
4874 | mpfr_init(t4); | |
4875 | mpfr_mul(t4, t2, ri, GMP_RNDN); | |
4876 | ||
4877 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
4878 | mpfr_set_inf(real, mpfr_sgn(t3)); | |
4879 | ||
4880 | mpfr_mul(t3, t2, rr, GMP_RNDN); | |
4881 | mpfr_mul(t4, t, ri, GMP_RNDN); | |
4882 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
4883 | mpfr_set_inf(imag, mpfr_sgn(t3)); | |
4884 | ||
4885 | mpfr_clear(t2); | |
4886 | mpfr_clear(t3); | |
4887 | mpfr_clear(t4); | |
4888 | } | |
4889 | else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag)) | |
4890 | && mpfr_number_p(left_real) && mpfr_number_p(left_imag)) | |
4891 | { | |
4892 | mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4893 | mpfr_copysign(t, t, rr, GMP_RNDN); | |
4894 | ||
4895 | mpfr_t t2; | |
4896 | mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
4897 | mpfr_copysign(t2, t2, ri, GMP_RNDN); | |
4898 | ||
4899 | mpfr_t t3; | |
4900 | mpfr_init(t3); | |
4901 | mpfr_mul(t3, left_real, t, GMP_RNDN); | |
4902 | ||
4903 | mpfr_t t4; | |
4904 | mpfr_init(t4); | |
4905 | mpfr_mul(t4, left_imag, t2, GMP_RNDN); | |
4906 | ||
4907 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
4908 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4909 | mpfr_mul(real, real, t3, GMP_RNDN); | |
4910 | ||
4911 | mpfr_mul(t3, left_imag, t, GMP_RNDN); | |
4912 | mpfr_mul(t4, left_real, t2, GMP_RNDN); | |
4913 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
4914 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4915 | mpfr_mul(imag, imag, t3, GMP_RNDN); | |
4916 | ||
4917 | mpfr_clear(t2); | |
4918 | mpfr_clear(t3); | |
4919 | mpfr_clear(t4); | |
4920 | } | |
4921 | } | |
4922 | ||
4923 | mpfr_clear(denom); | |
4924 | mpfr_clear(rr); | |
4925 | mpfr_clear(ri); | |
4926 | mpfr_clear(t); | |
4927 | mpfr_clear(rra); | |
4928 | mpfr_clear(ria); | |
4929 | } | |
4930 | break; | |
4931 | case OPERATOR_MOD: | |
4932 | return false; | |
4933 | case OPERATOR_LSHIFT: | |
4934 | case OPERATOR_RSHIFT: | |
4935 | return false; | |
4936 | default: | |
4937 | gcc_unreachable(); | |
4938 | } | |
4939 | ||
4940 | Type* type = left_type; | |
4941 | if (type == NULL) | |
4942 | type = right_type; | |
4943 | else if (type != right_type && right_type != NULL) | |
4944 | { | |
4945 | if (type->is_abstract()) | |
4946 | type = right_type; | |
4947 | else if (!right_type->is_abstract()) | |
4948 | { | |
4949 | // This looks like a type error which should be diagnosed | |
4950 | // elsewhere. Don't do anything here, to avoid an unhelpful | |
4951 | // chain of error messages. | |
4952 | return true; | |
4953 | } | |
4954 | } | |
4955 | ||
4956 | if (type != NULL && !type->is_abstract()) | |
4957 | { | |
4958 | if ((type != left_type | |
4959 | && !Complex_expression::check_constant(left_real, left_imag, | |
4960 | type, location)) | |
4961 | || (type != right_type | |
4962 | && !Complex_expression::check_constant(right_real, right_imag, | |
4963 | type, location)) | |
4964 | || !Complex_expression::check_constant(real, imag, type, | |
4965 | location)) | |
4966 | { | |
4967 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4968 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4969 | } | |
4970 | } | |
4971 | ||
4972 | return true; | |
4973 | } | |
4974 | ||
4975 | // Lower a binary expression. We have to evaluate constant | |
4976 | // expressions now, in order to implement Go's unlimited precision | |
4977 | // constants. | |
4978 | ||
4979 | Expression* | |
4980 | Binary_expression::do_lower(Gogo*, Named_object*, int) | |
4981 | { | |
4982 | source_location location = this->location(); | |
4983 | Operator op = this->op_; | |
4984 | Expression* left = this->left_; | |
4985 | Expression* right = this->right_; | |
4986 | ||
4987 | const bool is_comparison = (op == OPERATOR_EQEQ | |
4988 | || op == OPERATOR_NOTEQ | |
4989 | || op == OPERATOR_LT | |
4990 | || op == OPERATOR_LE | |
4991 | || op == OPERATOR_GT | |
4992 | || op == OPERATOR_GE); | |
4993 | ||
4994 | // Integer constant expressions. | |
4995 | { | |
4996 | mpz_t left_val; | |
4997 | mpz_init(left_val); | |
4998 | Type* left_type; | |
4999 | mpz_t right_val; | |
5000 | mpz_init(right_val); | |
5001 | Type* right_type; | |
5002 | if (left->integer_constant_value(false, left_val, &left_type) | |
5003 | && right->integer_constant_value(false, right_val, &right_type)) | |
5004 | { | |
5005 | Expression* ret = NULL; | |
5006 | if (left_type != right_type | |
5007 | && left_type != NULL | |
5008 | && right_type != NULL | |
5009 | && left_type->base() != right_type->base() | |
5010 | && op != OPERATOR_LSHIFT | |
5011 | && op != OPERATOR_RSHIFT) | |
5012 | { | |
5013 | // May be a type error--let it be diagnosed later. | |
5014 | } | |
5015 | else if (is_comparison) | |
5016 | { | |
5017 | bool b = Binary_expression::compare_integer(op, left_val, | |
5018 | right_val); | |
5019 | ret = Expression::make_cast(Type::lookup_bool_type(), | |
5020 | Expression::make_boolean(b, location), | |
5021 | location); | |
5022 | } | |
5023 | else | |
5024 | { | |
5025 | mpz_t val; | |
5026 | mpz_init(val); | |
5027 | ||
5028 | if (Binary_expression::eval_integer(op, left_type, left_val, | |
5029 | right_type, right_val, | |
5030 | location, val)) | |
5031 | { | |
5032 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND); | |
5033 | Type* type; | |
5034 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
5035 | type = left_type; | |
5036 | else if (left_type == NULL) | |
5037 | type = right_type; | |
5038 | else if (right_type == NULL) | |
5039 | type = left_type; | |
5040 | else if (!left_type->is_abstract() | |
5041 | && left_type->named_type() != NULL) | |
5042 | type = left_type; | |
5043 | else if (!right_type->is_abstract() | |
5044 | && right_type->named_type() != NULL) | |
5045 | type = right_type; | |
5046 | else if (!left_type->is_abstract()) | |
5047 | type = left_type; | |
5048 | else if (!right_type->is_abstract()) | |
5049 | type = right_type; | |
5050 | else if (left_type->float_type() != NULL) | |
5051 | type = left_type; | |
5052 | else if (right_type->float_type() != NULL) | |
5053 | type = right_type; | |
5054 | else if (left_type->complex_type() != NULL) | |
5055 | type = left_type; | |
5056 | else if (right_type->complex_type() != NULL) | |
5057 | type = right_type; | |
5058 | else | |
5059 | type = left_type; | |
5060 | ret = Expression::make_integer(&val, type, location); | |
5061 | } | |
5062 | ||
5063 | mpz_clear(val); | |
5064 | } | |
5065 | ||
5066 | if (ret != NULL) | |
5067 | { | |
5068 | mpz_clear(right_val); | |
5069 | mpz_clear(left_val); | |
5070 | return ret; | |
5071 | } | |
5072 | } | |
5073 | mpz_clear(right_val); | |
5074 | mpz_clear(left_val); | |
5075 | } | |
5076 | ||
5077 | // Floating point constant expressions. | |
5078 | { | |
5079 | mpfr_t left_val; | |
5080 | mpfr_init(left_val); | |
5081 | Type* left_type; | |
5082 | mpfr_t right_val; | |
5083 | mpfr_init(right_val); | |
5084 | Type* right_type; | |
5085 | if (left->float_constant_value(left_val, &left_type) | |
5086 | && right->float_constant_value(right_val, &right_type)) | |
5087 | { | |
5088 | Expression* ret = NULL; | |
5089 | if (left_type != right_type | |
5090 | && left_type != NULL | |
5091 | && right_type != NULL | |
5092 | && left_type->base() != right_type->base() | |
5093 | && op != OPERATOR_LSHIFT | |
5094 | && op != OPERATOR_RSHIFT) | |
5095 | { | |
5096 | // May be a type error--let it be diagnosed later. | |
5097 | } | |
5098 | else if (is_comparison) | |
5099 | { | |
5100 | bool b = Binary_expression::compare_float(op, | |
5101 | (left_type != NULL | |
5102 | ? left_type | |
5103 | : right_type), | |
5104 | left_val, right_val); | |
5105 | ret = Expression::make_boolean(b, location); | |
5106 | } | |
5107 | else | |
5108 | { | |
5109 | mpfr_t val; | |
5110 | mpfr_init(val); | |
5111 | ||
5112 | if (Binary_expression::eval_float(op, left_type, left_val, | |
5113 | right_type, right_val, val, | |
5114 | location)) | |
5115 | { | |
5116 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND | |
5117 | && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT); | |
5118 | Type* type; | |
5119 | if (left_type == NULL) | |
5120 | type = right_type; | |
5121 | else if (right_type == NULL) | |
5122 | type = left_type; | |
5123 | else if (!left_type->is_abstract() | |
5124 | && left_type->named_type() != NULL) | |
5125 | type = left_type; | |
5126 | else if (!right_type->is_abstract() | |
5127 | && right_type->named_type() != NULL) | |
5128 | type = right_type; | |
5129 | else if (!left_type->is_abstract()) | |
5130 | type = left_type; | |
5131 | else if (!right_type->is_abstract()) | |
5132 | type = right_type; | |
5133 | else if (left_type->float_type() != NULL) | |
5134 | type = left_type; | |
5135 | else if (right_type->float_type() != NULL) | |
5136 | type = right_type; | |
5137 | else | |
5138 | type = left_type; | |
5139 | ret = Expression::make_float(&val, type, location); | |
5140 | } | |
5141 | ||
5142 | mpfr_clear(val); | |
5143 | } | |
5144 | ||
5145 | if (ret != NULL) | |
5146 | { | |
5147 | mpfr_clear(right_val); | |
5148 | mpfr_clear(left_val); | |
5149 | return ret; | |
5150 | } | |
5151 | } | |
5152 | mpfr_clear(right_val); | |
5153 | mpfr_clear(left_val); | |
5154 | } | |
5155 | ||
5156 | // Complex constant expressions. | |
5157 | { | |
5158 | mpfr_t left_real; | |
5159 | mpfr_t left_imag; | |
5160 | mpfr_init(left_real); | |
5161 | mpfr_init(left_imag); | |
5162 | Type* left_type; | |
5163 | ||
5164 | mpfr_t right_real; | |
5165 | mpfr_t right_imag; | |
5166 | mpfr_init(right_real); | |
5167 | mpfr_init(right_imag); | |
5168 | Type* right_type; | |
5169 | ||
5170 | if (left->complex_constant_value(left_real, left_imag, &left_type) | |
5171 | && right->complex_constant_value(right_real, right_imag, &right_type)) | |
5172 | { | |
5173 | Expression* ret = NULL; | |
5174 | if (left_type != right_type | |
5175 | && left_type != NULL | |
5176 | && right_type != NULL | |
5177 | && left_type->base() != right_type->base()) | |
5178 | { | |
5179 | // May be a type error--let it be diagnosed later. | |
5180 | } | |
3b59603e | 5181 | else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
e440a328 | 5182 | { |
5183 | bool b = Binary_expression::compare_complex(op, | |
5184 | (left_type != NULL | |
5185 | ? left_type | |
5186 | : right_type), | |
5187 | left_real, | |
5188 | left_imag, | |
5189 | right_real, | |
5190 | right_imag); | |
5191 | ret = Expression::make_boolean(b, location); | |
5192 | } | |
5193 | else | |
5194 | { | |
5195 | mpfr_t real; | |
5196 | mpfr_t imag; | |
5197 | mpfr_init(real); | |
5198 | mpfr_init(imag); | |
5199 | ||
5200 | if (Binary_expression::eval_complex(op, left_type, | |
5201 | left_real, left_imag, | |
5202 | right_type, | |
5203 | right_real, right_imag, | |
5204 | real, imag, | |
5205 | location)) | |
5206 | { | |
5207 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND | |
5208 | && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT); | |
5209 | Type* type; | |
5210 | if (left_type == NULL) | |
5211 | type = right_type; | |
5212 | else if (right_type == NULL) | |
5213 | type = left_type; | |
5214 | else if (!left_type->is_abstract() | |
5215 | && left_type->named_type() != NULL) | |
5216 | type = left_type; | |
5217 | else if (!right_type->is_abstract() | |
5218 | && right_type->named_type() != NULL) | |
5219 | type = right_type; | |
5220 | else if (!left_type->is_abstract()) | |
5221 | type = left_type; | |
5222 | else if (!right_type->is_abstract()) | |
5223 | type = right_type; | |
5224 | else if (left_type->complex_type() != NULL) | |
5225 | type = left_type; | |
5226 | else if (right_type->complex_type() != NULL) | |
5227 | type = right_type; | |
5228 | else | |
5229 | type = left_type; | |
5230 | ret = Expression::make_complex(&real, &imag, type, | |
5231 | location); | |
5232 | } | |
5233 | mpfr_clear(real); | |
5234 | mpfr_clear(imag); | |
5235 | } | |
5236 | ||
5237 | if (ret != NULL) | |
5238 | { | |
5239 | mpfr_clear(left_real); | |
5240 | mpfr_clear(left_imag); | |
5241 | mpfr_clear(right_real); | |
5242 | mpfr_clear(right_imag); | |
5243 | return ret; | |
5244 | } | |
5245 | } | |
5246 | ||
5247 | mpfr_clear(left_real); | |
5248 | mpfr_clear(left_imag); | |
5249 | mpfr_clear(right_real); | |
5250 | mpfr_clear(right_imag); | |
5251 | } | |
5252 | ||
5253 | // String constant expressions. | |
5254 | if (op == OPERATOR_PLUS | |
5255 | && left->type()->is_string_type() | |
5256 | && right->type()->is_string_type()) | |
5257 | { | |
5258 | std::string left_string; | |
5259 | std::string right_string; | |
5260 | if (left->string_constant_value(&left_string) | |
5261 | && right->string_constant_value(&right_string)) | |
5262 | return Expression::make_string(left_string + right_string, location); | |
5263 | } | |
5264 | ||
5265 | return this; | |
5266 | } | |
5267 | ||
5268 | // Return the integer constant value, if it has one. | |
5269 | ||
5270 | bool | |
5271 | Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
5272 | Type** ptype) const | |
5273 | { | |
5274 | mpz_t left_val; | |
5275 | mpz_init(left_val); | |
5276 | Type* left_type; | |
5277 | if (!this->left_->integer_constant_value(iota_is_constant, left_val, | |
5278 | &left_type)) | |
5279 | { | |
5280 | mpz_clear(left_val); | |
5281 | return false; | |
5282 | } | |
5283 | ||
5284 | mpz_t right_val; | |
5285 | mpz_init(right_val); | |
5286 | Type* right_type; | |
5287 | if (!this->right_->integer_constant_value(iota_is_constant, right_val, | |
5288 | &right_type)) | |
5289 | { | |
5290 | mpz_clear(right_val); | |
5291 | mpz_clear(left_val); | |
5292 | return false; | |
5293 | } | |
5294 | ||
5295 | bool ret; | |
5296 | if (left_type != right_type | |
5297 | && left_type != NULL | |
5298 | && right_type != NULL | |
5299 | && left_type->base() != right_type->base() | |
5300 | && this->op_ != OPERATOR_RSHIFT | |
5301 | && this->op_ != OPERATOR_LSHIFT) | |
5302 | ret = false; | |
5303 | else | |
5304 | ret = Binary_expression::eval_integer(this->op_, left_type, left_val, | |
5305 | right_type, right_val, | |
5306 | this->location(), val); | |
5307 | ||
5308 | mpz_clear(right_val); | |
5309 | mpz_clear(left_val); | |
5310 | ||
5311 | if (ret) | |
5312 | *ptype = left_type; | |
5313 | ||
5314 | return ret; | |
5315 | } | |
5316 | ||
5317 | // Return the floating point constant value, if it has one. | |
5318 | ||
5319 | bool | |
5320 | Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
5321 | { | |
5322 | mpfr_t left_val; | |
5323 | mpfr_init(left_val); | |
5324 | Type* left_type; | |
5325 | if (!this->left_->float_constant_value(left_val, &left_type)) | |
5326 | { | |
5327 | mpfr_clear(left_val); | |
5328 | return false; | |
5329 | } | |
5330 | ||
5331 | mpfr_t right_val; | |
5332 | mpfr_init(right_val); | |
5333 | Type* right_type; | |
5334 | if (!this->right_->float_constant_value(right_val, &right_type)) | |
5335 | { | |
5336 | mpfr_clear(right_val); | |
5337 | mpfr_clear(left_val); | |
5338 | return false; | |
5339 | } | |
5340 | ||
5341 | bool ret; | |
5342 | if (left_type != right_type | |
5343 | && left_type != NULL | |
5344 | && right_type != NULL | |
5345 | && left_type->base() != right_type->base()) | |
5346 | ret = false; | |
5347 | else | |
5348 | ret = Binary_expression::eval_float(this->op_, left_type, left_val, | |
5349 | right_type, right_val, | |
5350 | val, this->location()); | |
5351 | ||
5352 | mpfr_clear(left_val); | |
5353 | mpfr_clear(right_val); | |
5354 | ||
5355 | if (ret) | |
5356 | *ptype = left_type; | |
5357 | ||
5358 | return ret; | |
5359 | } | |
5360 | ||
5361 | // Return the complex constant value, if it has one. | |
5362 | ||
5363 | bool | |
5364 | Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
5365 | Type** ptype) const | |
5366 | { | |
5367 | mpfr_t left_real; | |
5368 | mpfr_t left_imag; | |
5369 | mpfr_init(left_real); | |
5370 | mpfr_init(left_imag); | |
5371 | Type* left_type; | |
5372 | if (!this->left_->complex_constant_value(left_real, left_imag, &left_type)) | |
5373 | { | |
5374 | mpfr_clear(left_real); | |
5375 | mpfr_clear(left_imag); | |
5376 | return false; | |
5377 | } | |
5378 | ||
5379 | mpfr_t right_real; | |
5380 | mpfr_t right_imag; | |
5381 | mpfr_init(right_real); | |
5382 | mpfr_init(right_imag); | |
5383 | Type* right_type; | |
5384 | if (!this->right_->complex_constant_value(right_real, right_imag, | |
5385 | &right_type)) | |
5386 | { | |
5387 | mpfr_clear(left_real); | |
5388 | mpfr_clear(left_imag); | |
5389 | mpfr_clear(right_real); | |
5390 | mpfr_clear(right_imag); | |
5391 | return false; | |
5392 | } | |
5393 | ||
5394 | bool ret; | |
5395 | if (left_type != right_type | |
5396 | && left_type != NULL | |
5397 | && right_type != NULL | |
5398 | && left_type->base() != right_type->base()) | |
5399 | ret = false; | |
5400 | else | |
5401 | ret = Binary_expression::eval_complex(this->op_, left_type, | |
5402 | left_real, left_imag, | |
5403 | right_type, | |
5404 | right_real, right_imag, | |
5405 | real, imag, | |
5406 | this->location()); | |
5407 | mpfr_clear(left_real); | |
5408 | mpfr_clear(left_imag); | |
5409 | mpfr_clear(right_real); | |
5410 | mpfr_clear(right_imag); | |
5411 | ||
5412 | if (ret) | |
5413 | *ptype = left_type; | |
5414 | ||
5415 | return ret; | |
5416 | } | |
5417 | ||
5418 | // Note that the value is being discarded. | |
5419 | ||
5420 | void | |
5421 | Binary_expression::do_discarding_value() | |
5422 | { | |
5423 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
5424 | this->right_->discarding_value(); | |
5425 | else | |
5426 | this->warn_about_unused_value(); | |
5427 | } | |
5428 | ||
5429 | // Get type. | |
5430 | ||
5431 | Type* | |
5432 | Binary_expression::do_type() | |
5433 | { | |
5f5fea79 | 5434 | if (this->classification() == EXPRESSION_ERROR) |
5435 | return Type::make_error_type(); | |
5436 | ||
e440a328 | 5437 | switch (this->op_) |
5438 | { | |
5439 | case OPERATOR_OROR: | |
5440 | case OPERATOR_ANDAND: | |
5441 | case OPERATOR_EQEQ: | |
5442 | case OPERATOR_NOTEQ: | |
5443 | case OPERATOR_LT: | |
5444 | case OPERATOR_LE: | |
5445 | case OPERATOR_GT: | |
5446 | case OPERATOR_GE: | |
5447 | return Type::lookup_bool_type(); | |
5448 | ||
5449 | case OPERATOR_PLUS: | |
5450 | case OPERATOR_MINUS: | |
5451 | case OPERATOR_OR: | |
5452 | case OPERATOR_XOR: | |
5453 | case OPERATOR_MULT: | |
5454 | case OPERATOR_DIV: | |
5455 | case OPERATOR_MOD: | |
5456 | case OPERATOR_AND: | |
5457 | case OPERATOR_BITCLEAR: | |
5458 | { | |
5459 | Type* left_type = this->left_->type(); | |
5460 | Type* right_type = this->right_->type(); | |
a5fe8571 | 5461 | if (left_type->is_error_type()) |
5462 | return left_type; | |
5463 | else if (right_type->is_error_type()) | |
5464 | return right_type; | |
5f5fea79 | 5465 | else if (!Type::are_compatible_for_binop(left_type, right_type)) |
5466 | { | |
5467 | this->report_error(_("incompatible types in binary expression")); | |
5468 | return Type::make_error_type(); | |
5469 | } | |
a5fe8571 | 5470 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) |
e440a328 | 5471 | return left_type; |
5472 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
5473 | return right_type; | |
5474 | else if (!left_type->is_abstract()) | |
5475 | return left_type; | |
5476 | else if (!right_type->is_abstract()) | |
5477 | return right_type; | |
5478 | else if (left_type->complex_type() != NULL) | |
5479 | return left_type; | |
5480 | else if (right_type->complex_type() != NULL) | |
5481 | return right_type; | |
5482 | else if (left_type->float_type() != NULL) | |
5483 | return left_type; | |
5484 | else if (right_type->float_type() != NULL) | |
5485 | return right_type; | |
5486 | else | |
5487 | return left_type; | |
5488 | } | |
5489 | ||
5490 | case OPERATOR_LSHIFT: | |
5491 | case OPERATOR_RSHIFT: | |
5492 | return this->left_->type(); | |
5493 | ||
5494 | default: | |
5495 | gcc_unreachable(); | |
5496 | } | |
5497 | } | |
5498 | ||
5499 | // Set type for a binary expression. | |
5500 | ||
5501 | void | |
5502 | Binary_expression::do_determine_type(const Type_context* context) | |
5503 | { | |
5504 | Type* tleft = this->left_->type(); | |
5505 | Type* tright = this->right_->type(); | |
5506 | ||
5507 | // Both sides should have the same type, except for the shift | |
5508 | // operations. For a comparison, we should ignore the incoming | |
5509 | // type. | |
5510 | ||
5511 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5512 | || this->op_ == OPERATOR_RSHIFT); | |
5513 | ||
5514 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5515 | || this->op_ == OPERATOR_NOTEQ | |
5516 | || this->op_ == OPERATOR_LT | |
5517 | || this->op_ == OPERATOR_LE | |
5518 | || this->op_ == OPERATOR_GT | |
5519 | || this->op_ == OPERATOR_GE); | |
5520 | ||
5521 | Type_context subcontext(*context); | |
5522 | ||
5523 | if (is_comparison) | |
5524 | { | |
5525 | // In a comparison, the context does not determine the types of | |
5526 | // the operands. | |
5527 | subcontext.type = NULL; | |
5528 | } | |
5529 | ||
5530 | // Set the context for the left hand operand. | |
5531 | if (is_shift_op) | |
5532 | { | |
5533 | // The right hand operand plays no role in determining the type | |
5534 | // of the left hand operand. A shift of an abstract integer in | |
5535 | // a string context gets special treatment, which may be a | |
5536 | // language bug. | |
5537 | if (subcontext.type != NULL | |
5538 | && subcontext.type->is_string_type() | |
5539 | && tleft->is_abstract()) | |
5540 | error_at(this->location(), "shift of non-integer operand"); | |
5541 | } | |
5542 | else if (!tleft->is_abstract()) | |
5543 | subcontext.type = tleft; | |
5544 | else if (!tright->is_abstract()) | |
5545 | subcontext.type = tright; | |
5546 | else if (subcontext.type == NULL) | |
5547 | { | |
5548 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5549 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5550 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5551 | { | |
5552 | // Both sides have an abstract integer, abstract float, or | |
5553 | // abstract complex type. Just let CONTEXT determine | |
5554 | // whether they may remain abstract or not. | |
5555 | } | |
5556 | else if (tleft->complex_type() != NULL) | |
5557 | subcontext.type = tleft; | |
5558 | else if (tright->complex_type() != NULL) | |
5559 | subcontext.type = tright; | |
5560 | else if (tleft->float_type() != NULL) | |
5561 | subcontext.type = tleft; | |
5562 | else if (tright->float_type() != NULL) | |
5563 | subcontext.type = tright; | |
5564 | else | |
5565 | subcontext.type = tleft; | |
f58a23ae | 5566 | |
5567 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5568 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5569 | } |
5570 | ||
5571 | this->left_->determine_type(&subcontext); | |
5572 | ||
5573 | // The context for the right hand operand is the same as for the | |
5574 | // left hand operand, except for a shift operator. | |
5575 | if (is_shift_op) | |
5576 | { | |
5577 | subcontext.type = Type::lookup_integer_type("uint"); | |
5578 | subcontext.may_be_abstract = false; | |
5579 | } | |
5580 | ||
5581 | this->right_->determine_type(&subcontext); | |
5582 | } | |
5583 | ||
5584 | // Report an error if the binary operator OP does not support TYPE. | |
5585 | // Return whether the operation is OK. This should not be used for | |
5586 | // shift. | |
5587 | ||
5588 | bool | |
5589 | Binary_expression::check_operator_type(Operator op, Type* type, | |
5590 | source_location location) | |
5591 | { | |
5592 | switch (op) | |
5593 | { | |
5594 | case OPERATOR_OROR: | |
5595 | case OPERATOR_ANDAND: | |
5596 | if (!type->is_boolean_type()) | |
5597 | { | |
5598 | error_at(location, "expected boolean type"); | |
5599 | return false; | |
5600 | } | |
5601 | break; | |
5602 | ||
5603 | case OPERATOR_EQEQ: | |
5604 | case OPERATOR_NOTEQ: | |
5605 | if (type->integer_type() == NULL | |
5606 | && type->float_type() == NULL | |
5607 | && type->complex_type() == NULL | |
5608 | && !type->is_string_type() | |
5609 | && type->points_to() == NULL | |
5610 | && !type->is_nil_type() | |
5611 | && !type->is_boolean_type() | |
5612 | && type->interface_type() == NULL | |
5613 | && (type->array_type() == NULL | |
5614 | || type->array_type()->length() != NULL) | |
5615 | && type->map_type() == NULL | |
5616 | && type->channel_type() == NULL | |
5617 | && type->function_type() == NULL) | |
5618 | { | |
5619 | error_at(location, | |
5620 | ("expected integer, floating, complex, string, pointer, " | |
5621 | "boolean, interface, slice, map, channel, " | |
5622 | "or function type")); | |
5623 | return false; | |
5624 | } | |
5625 | break; | |
5626 | ||
5627 | case OPERATOR_LT: | |
5628 | case OPERATOR_LE: | |
5629 | case OPERATOR_GT: | |
5630 | case OPERATOR_GE: | |
5631 | if (type->integer_type() == NULL | |
5632 | && type->float_type() == NULL | |
5633 | && !type->is_string_type()) | |
5634 | { | |
5635 | error_at(location, "expected integer, floating, or string type"); | |
5636 | return false; | |
5637 | } | |
5638 | break; | |
5639 | ||
5640 | case OPERATOR_PLUS: | |
5641 | case OPERATOR_PLUSEQ: | |
5642 | if (type->integer_type() == NULL | |
5643 | && type->float_type() == NULL | |
5644 | && type->complex_type() == NULL | |
5645 | && !type->is_string_type()) | |
5646 | { | |
5647 | error_at(location, | |
5648 | "expected integer, floating, complex, or string type"); | |
5649 | return false; | |
5650 | } | |
5651 | break; | |
5652 | ||
5653 | case OPERATOR_MINUS: | |
5654 | case OPERATOR_MINUSEQ: | |
5655 | case OPERATOR_MULT: | |
5656 | case OPERATOR_MULTEQ: | |
5657 | case OPERATOR_DIV: | |
5658 | case OPERATOR_DIVEQ: | |
5659 | if (type->integer_type() == NULL | |
5660 | && type->float_type() == NULL | |
5661 | && type->complex_type() == NULL) | |
5662 | { | |
5663 | error_at(location, "expected integer, floating, or complex type"); | |
5664 | return false; | |
5665 | } | |
5666 | break; | |
5667 | ||
5668 | case OPERATOR_MOD: | |
5669 | case OPERATOR_MODEQ: | |
5670 | case OPERATOR_OR: | |
5671 | case OPERATOR_OREQ: | |
5672 | case OPERATOR_AND: | |
5673 | case OPERATOR_ANDEQ: | |
5674 | case OPERATOR_XOR: | |
5675 | case OPERATOR_XOREQ: | |
5676 | case OPERATOR_BITCLEAR: | |
5677 | case OPERATOR_BITCLEAREQ: | |
5678 | if (type->integer_type() == NULL) | |
5679 | { | |
5680 | error_at(location, "expected integer type"); | |
5681 | return false; | |
5682 | } | |
5683 | break; | |
5684 | ||
5685 | default: | |
5686 | gcc_unreachable(); | |
5687 | } | |
5688 | ||
5689 | return true; | |
5690 | } | |
5691 | ||
5692 | // Check types. | |
5693 | ||
5694 | void | |
5695 | Binary_expression::do_check_types(Gogo*) | |
5696 | { | |
5f5fea79 | 5697 | if (this->classification() == EXPRESSION_ERROR) |
5698 | return; | |
5699 | ||
e440a328 | 5700 | Type* left_type = this->left_->type(); |
5701 | Type* right_type = this->right_->type(); | |
5702 | if (left_type->is_error_type() || right_type->is_error_type()) | |
9fe897ef | 5703 | { |
5704 | this->set_is_error(); | |
5705 | return; | |
5706 | } | |
e440a328 | 5707 | |
5708 | if (this->op_ == OPERATOR_EQEQ | |
5709 | || this->op_ == OPERATOR_NOTEQ | |
5710 | || this->op_ == OPERATOR_LT | |
5711 | || this->op_ == OPERATOR_LE | |
5712 | || this->op_ == OPERATOR_GT | |
5713 | || this->op_ == OPERATOR_GE) | |
5714 | { | |
5715 | if (!Type::are_assignable(left_type, right_type, NULL) | |
5716 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5717 | { | |
5718 | this->report_error(_("incompatible types in binary expression")); | |
5719 | return; | |
5720 | } | |
5721 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
5722 | this->location()) | |
5723 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
5724 | this->location())) | |
5725 | { | |
5726 | this->set_is_error(); | |
5727 | return; | |
5728 | } | |
5729 | } | |
5730 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5731 | { | |
5732 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5733 | { | |
5734 | this->report_error(_("incompatible types in binary expression")); | |
5735 | return; | |
5736 | } | |
5737 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
5738 | this->location())) | |
5739 | { | |
5740 | this->set_is_error(); | |
5741 | return; | |
5742 | } | |
5743 | } | |
5744 | else | |
5745 | { | |
5746 | if (left_type->integer_type() == NULL) | |
5747 | this->report_error(_("shift of non-integer operand")); | |
5748 | ||
5749 | if (!right_type->is_abstract() | |
5750 | && (right_type->integer_type() == NULL | |
5751 | || !right_type->integer_type()->is_unsigned())) | |
5752 | this->report_error(_("shift count not unsigned integer")); | |
5753 | else | |
5754 | { | |
5755 | mpz_t val; | |
5756 | mpz_init(val); | |
5757 | Type* type; | |
5758 | if (this->right_->integer_constant_value(true, val, &type)) | |
5759 | { | |
5760 | if (mpz_sgn(val) < 0) | |
5761 | this->report_error(_("negative shift count")); | |
5762 | } | |
5763 | mpz_clear(val); | |
5764 | } | |
5765 | } | |
5766 | } | |
5767 | ||
5768 | // Get a tree for a binary expression. | |
5769 | ||
5770 | tree | |
5771 | Binary_expression::do_get_tree(Translate_context* context) | |
5772 | { | |
5773 | tree left = this->left_->get_tree(context); | |
5774 | tree right = this->right_->get_tree(context); | |
5775 | ||
5776 | if (left == error_mark_node || right == error_mark_node) | |
5777 | return error_mark_node; | |
5778 | ||
5779 | enum tree_code code; | |
5780 | bool use_left_type = true; | |
5781 | bool is_shift_op = false; | |
5782 | switch (this->op_) | |
5783 | { | |
5784 | case OPERATOR_EQEQ: | |
5785 | case OPERATOR_NOTEQ: | |
5786 | case OPERATOR_LT: | |
5787 | case OPERATOR_LE: | |
5788 | case OPERATOR_GT: | |
5789 | case OPERATOR_GE: | |
5790 | return Expression::comparison_tree(context, this->op_, | |
5791 | this->left_->type(), left, | |
5792 | this->right_->type(), right, | |
5793 | this->location()); | |
5794 | ||
5795 | case OPERATOR_OROR: | |
5796 | code = TRUTH_ORIF_EXPR; | |
5797 | use_left_type = false; | |
5798 | break; | |
5799 | case OPERATOR_ANDAND: | |
5800 | code = TRUTH_ANDIF_EXPR; | |
5801 | use_left_type = false; | |
5802 | break; | |
5803 | case OPERATOR_PLUS: | |
5804 | code = PLUS_EXPR; | |
5805 | break; | |
5806 | case OPERATOR_MINUS: | |
5807 | code = MINUS_EXPR; | |
5808 | break; | |
5809 | case OPERATOR_OR: | |
5810 | code = BIT_IOR_EXPR; | |
5811 | break; | |
5812 | case OPERATOR_XOR: | |
5813 | code = BIT_XOR_EXPR; | |
5814 | break; | |
5815 | case OPERATOR_MULT: | |
5816 | code = MULT_EXPR; | |
5817 | break; | |
5818 | case OPERATOR_DIV: | |
5819 | { | |
5820 | Type *t = this->left_->type(); | |
5821 | if (t->float_type() != NULL || t->complex_type() != NULL) | |
5822 | code = RDIV_EXPR; | |
5823 | else | |
5824 | code = TRUNC_DIV_EXPR; | |
5825 | } | |
5826 | break; | |
5827 | case OPERATOR_MOD: | |
5828 | code = TRUNC_MOD_EXPR; | |
5829 | break; | |
5830 | case OPERATOR_LSHIFT: | |
5831 | code = LSHIFT_EXPR; | |
5832 | is_shift_op = true; | |
5833 | break; | |
5834 | case OPERATOR_RSHIFT: | |
5835 | code = RSHIFT_EXPR; | |
5836 | is_shift_op = true; | |
5837 | break; | |
5838 | case OPERATOR_AND: | |
5839 | code = BIT_AND_EXPR; | |
5840 | break; | |
5841 | case OPERATOR_BITCLEAR: | |
5842 | right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right); | |
5843 | code = BIT_AND_EXPR; | |
5844 | break; | |
5845 | default: | |
5846 | gcc_unreachable(); | |
5847 | } | |
5848 | ||
5849 | tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right); | |
5850 | ||
5851 | if (this->left_->type()->is_string_type()) | |
5852 | { | |
5853 | gcc_assert(this->op_ == OPERATOR_PLUS); | |
5854 | tree string_type = Type::make_string_type()->get_tree(context->gogo()); | |
5855 | static tree string_plus_decl; | |
5856 | return Gogo::call_builtin(&string_plus_decl, | |
5857 | this->location(), | |
5858 | "__go_string_plus", | |
5859 | 2, | |
5860 | string_type, | |
5861 | string_type, | |
5862 | left, | |
5863 | string_type, | |
5864 | right); | |
5865 | } | |
5866 | ||
5867 | tree compute_type = excess_precision_type(type); | |
5868 | if (compute_type != NULL_TREE) | |
5869 | { | |
5870 | left = ::convert(compute_type, left); | |
5871 | right = ::convert(compute_type, right); | |
5872 | } | |
5873 | ||
5874 | tree eval_saved = NULL_TREE; | |
5875 | if (is_shift_op) | |
5876 | { | |
e440a328 | 5877 | // Make sure the values are evaluated. |
a7a70f31 | 5878 | if (!DECL_P(left) && TREE_SIDE_EFFECTS(left)) |
5879 | { | |
5880 | left = save_expr(left); | |
5881 | eval_saved = left; | |
5882 | } | |
5883 | if (!DECL_P(right) && TREE_SIDE_EFFECTS(right)) | |
5884 | { | |
5885 | right = save_expr(right); | |
5886 | if (eval_saved == NULL_TREE) | |
5887 | eval_saved = right; | |
5888 | else | |
5889 | eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR, | |
5890 | void_type_node, eval_saved, right); | |
5891 | } | |
e440a328 | 5892 | } |
5893 | ||
5894 | tree ret = fold_build2_loc(this->location(), | |
5895 | code, | |
5896 | compute_type != NULL_TREE ? compute_type : type, | |
5897 | left, right); | |
5898 | ||
5899 | if (compute_type != NULL_TREE) | |
5900 | ret = ::convert(type, ret); | |
5901 | ||
5902 | // In Go, a shift larger than the size of the type is well-defined. | |
5903 | // This is not true in GENERIC, so we need to insert a conditional. | |
5904 | if (is_shift_op) | |
5905 | { | |
5906 | gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left))); | |
5907 | gcc_assert(this->left_->type()->integer_type() != NULL); | |
5908 | int bits = TYPE_PRECISION(TREE_TYPE(left)); | |
5909 | ||
5910 | tree compare = fold_build2(LT_EXPR, boolean_type_node, right, | |
5911 | build_int_cst_type(TREE_TYPE(right), bits)); | |
5912 | ||
5913 | tree overflow_result = fold_convert_loc(this->location(), | |
5914 | TREE_TYPE(left), | |
5915 | integer_zero_node); | |
5916 | if (this->op_ == OPERATOR_RSHIFT | |
5917 | && !this->left_->type()->integer_type()->is_unsigned()) | |
5918 | { | |
5919 | tree neg = fold_build2_loc(this->location(), LT_EXPR, | |
5920 | boolean_type_node, left, | |
5921 | fold_convert_loc(this->location(), | |
5922 | TREE_TYPE(left), | |
5923 | integer_zero_node)); | |
5924 | tree neg_one = fold_build2_loc(this->location(), | |
5925 | MINUS_EXPR, TREE_TYPE(left), | |
5926 | fold_convert_loc(this->location(), | |
5927 | TREE_TYPE(left), | |
5928 | integer_zero_node), | |
5929 | fold_convert_loc(this->location(), | |
5930 | TREE_TYPE(left), | |
5931 | integer_one_node)); | |
5932 | overflow_result = fold_build3_loc(this->location(), COND_EXPR, | |
5933 | TREE_TYPE(left), neg, neg_one, | |
5934 | overflow_result); | |
5935 | } | |
5936 | ||
5937 | ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left), | |
5938 | compare, ret, overflow_result); | |
5939 | ||
a7a70f31 | 5940 | if (eval_saved != NULL_TREE) |
5941 | ret = fold_build2_loc(this->location(), COMPOUND_EXPR, | |
5942 | TREE_TYPE(ret), eval_saved, ret); | |
e440a328 | 5943 | } |
5944 | ||
5945 | return ret; | |
5946 | } | |
5947 | ||
5948 | // Export a binary expression. | |
5949 | ||
5950 | void | |
5951 | Binary_expression::do_export(Export* exp) const | |
5952 | { | |
5953 | exp->write_c_string("("); | |
5954 | this->left_->export_expression(exp); | |
5955 | switch (this->op_) | |
5956 | { | |
5957 | case OPERATOR_OROR: | |
5958 | exp->write_c_string(" || "); | |
5959 | break; | |
5960 | case OPERATOR_ANDAND: | |
5961 | exp->write_c_string(" && "); | |
5962 | break; | |
5963 | case OPERATOR_EQEQ: | |
5964 | exp->write_c_string(" == "); | |
5965 | break; | |
5966 | case OPERATOR_NOTEQ: | |
5967 | exp->write_c_string(" != "); | |
5968 | break; | |
5969 | case OPERATOR_LT: | |
5970 | exp->write_c_string(" < "); | |
5971 | break; | |
5972 | case OPERATOR_LE: | |
5973 | exp->write_c_string(" <= "); | |
5974 | break; | |
5975 | case OPERATOR_GT: | |
5976 | exp->write_c_string(" > "); | |
5977 | break; | |
5978 | case OPERATOR_GE: | |
5979 | exp->write_c_string(" >= "); | |
5980 | break; | |
5981 | case OPERATOR_PLUS: | |
5982 | exp->write_c_string(" + "); | |
5983 | break; | |
5984 | case OPERATOR_MINUS: | |
5985 | exp->write_c_string(" - "); | |
5986 | break; | |
5987 | case OPERATOR_OR: | |
5988 | exp->write_c_string(" | "); | |
5989 | break; | |
5990 | case OPERATOR_XOR: | |
5991 | exp->write_c_string(" ^ "); | |
5992 | break; | |
5993 | case OPERATOR_MULT: | |
5994 | exp->write_c_string(" * "); | |
5995 | break; | |
5996 | case OPERATOR_DIV: | |
5997 | exp->write_c_string(" / "); | |
5998 | break; | |
5999 | case OPERATOR_MOD: | |
6000 | exp->write_c_string(" % "); | |
6001 | break; | |
6002 | case OPERATOR_LSHIFT: | |
6003 | exp->write_c_string(" << "); | |
6004 | break; | |
6005 | case OPERATOR_RSHIFT: | |
6006 | exp->write_c_string(" >> "); | |
6007 | break; | |
6008 | case OPERATOR_AND: | |
6009 | exp->write_c_string(" & "); | |
6010 | break; | |
6011 | case OPERATOR_BITCLEAR: | |
6012 | exp->write_c_string(" &^ "); | |
6013 | break; | |
6014 | default: | |
6015 | gcc_unreachable(); | |
6016 | } | |
6017 | this->right_->export_expression(exp); | |
6018 | exp->write_c_string(")"); | |
6019 | } | |
6020 | ||
6021 | // Import a binary expression. | |
6022 | ||
6023 | Expression* | |
6024 | Binary_expression::do_import(Import* imp) | |
6025 | { | |
6026 | imp->require_c_string("("); | |
6027 | ||
6028 | Expression* left = Expression::import_expression(imp); | |
6029 | ||
6030 | Operator op; | |
6031 | if (imp->match_c_string(" || ")) | |
6032 | { | |
6033 | op = OPERATOR_OROR; | |
6034 | imp->advance(4); | |
6035 | } | |
6036 | else if (imp->match_c_string(" && ")) | |
6037 | { | |
6038 | op = OPERATOR_ANDAND; | |
6039 | imp->advance(4); | |
6040 | } | |
6041 | else if (imp->match_c_string(" == ")) | |
6042 | { | |
6043 | op = OPERATOR_EQEQ; | |
6044 | imp->advance(4); | |
6045 | } | |
6046 | else if (imp->match_c_string(" != ")) | |
6047 | { | |
6048 | op = OPERATOR_NOTEQ; | |
6049 | imp->advance(4); | |
6050 | } | |
6051 | else if (imp->match_c_string(" < ")) | |
6052 | { | |
6053 | op = OPERATOR_LT; | |
6054 | imp->advance(3); | |
6055 | } | |
6056 | else if (imp->match_c_string(" <= ")) | |
6057 | { | |
6058 | op = OPERATOR_LE; | |
6059 | imp->advance(4); | |
6060 | } | |
6061 | else if (imp->match_c_string(" > ")) | |
6062 | { | |
6063 | op = OPERATOR_GT; | |
6064 | imp->advance(3); | |
6065 | } | |
6066 | else if (imp->match_c_string(" >= ")) | |
6067 | { | |
6068 | op = OPERATOR_GE; | |
6069 | imp->advance(4); | |
6070 | } | |
6071 | else if (imp->match_c_string(" + ")) | |
6072 | { | |
6073 | op = OPERATOR_PLUS; | |
6074 | imp->advance(3); | |
6075 | } | |
6076 | else if (imp->match_c_string(" - ")) | |
6077 | { | |
6078 | op = OPERATOR_MINUS; | |
6079 | imp->advance(3); | |
6080 | } | |
6081 | else if (imp->match_c_string(" | ")) | |
6082 | { | |
6083 | op = OPERATOR_OR; | |
6084 | imp->advance(3); | |
6085 | } | |
6086 | else if (imp->match_c_string(" ^ ")) | |
6087 | { | |
6088 | op = OPERATOR_XOR; | |
6089 | imp->advance(3); | |
6090 | } | |
6091 | else if (imp->match_c_string(" * ")) | |
6092 | { | |
6093 | op = OPERATOR_MULT; | |
6094 | imp->advance(3); | |
6095 | } | |
6096 | else if (imp->match_c_string(" / ")) | |
6097 | { | |
6098 | op = OPERATOR_DIV; | |
6099 | imp->advance(3); | |
6100 | } | |
6101 | else if (imp->match_c_string(" % ")) | |
6102 | { | |
6103 | op = OPERATOR_MOD; | |
6104 | imp->advance(3); | |
6105 | } | |
6106 | else if (imp->match_c_string(" << ")) | |
6107 | { | |
6108 | op = OPERATOR_LSHIFT; | |
6109 | imp->advance(4); | |
6110 | } | |
6111 | else if (imp->match_c_string(" >> ")) | |
6112 | { | |
6113 | op = OPERATOR_RSHIFT; | |
6114 | imp->advance(4); | |
6115 | } | |
6116 | else if (imp->match_c_string(" & ")) | |
6117 | { | |
6118 | op = OPERATOR_AND; | |
6119 | imp->advance(3); | |
6120 | } | |
6121 | else if (imp->match_c_string(" &^ ")) | |
6122 | { | |
6123 | op = OPERATOR_BITCLEAR; | |
6124 | imp->advance(4); | |
6125 | } | |
6126 | else | |
6127 | { | |
6128 | error_at(imp->location(), "unrecognized binary operator"); | |
6129 | return Expression::make_error(imp->location()); | |
6130 | } | |
6131 | ||
6132 | Expression* right = Expression::import_expression(imp); | |
6133 | ||
6134 | imp->require_c_string(")"); | |
6135 | ||
6136 | return Expression::make_binary(op, left, right, imp->location()); | |
6137 | } | |
6138 | ||
6139 | // Make a binary expression. | |
6140 | ||
6141 | Expression* | |
6142 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
6143 | source_location location) | |
6144 | { | |
6145 | return new Binary_expression(op, left, right, location); | |
6146 | } | |
6147 | ||
6148 | // Implement a comparison. | |
6149 | ||
6150 | tree | |
6151 | Expression::comparison_tree(Translate_context* context, Operator op, | |
6152 | Type* left_type, tree left_tree, | |
6153 | Type* right_type, tree right_tree, | |
6154 | source_location location) | |
6155 | { | |
6156 | enum tree_code code; | |
6157 | switch (op) | |
6158 | { | |
6159 | case OPERATOR_EQEQ: | |
6160 | code = EQ_EXPR; | |
6161 | break; | |
6162 | case OPERATOR_NOTEQ: | |
6163 | code = NE_EXPR; | |
6164 | break; | |
6165 | case OPERATOR_LT: | |
6166 | code = LT_EXPR; | |
6167 | break; | |
6168 | case OPERATOR_LE: | |
6169 | code = LE_EXPR; | |
6170 | break; | |
6171 | case OPERATOR_GT: | |
6172 | code = GT_EXPR; | |
6173 | break; | |
6174 | case OPERATOR_GE: | |
6175 | code = GE_EXPR; | |
6176 | break; | |
6177 | default: | |
6178 | gcc_unreachable(); | |
6179 | } | |
6180 | ||
15c67ee2 | 6181 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6182 | { |
e440a328 | 6183 | tree string_type = Type::make_string_type()->get_tree(context->gogo()); |
6184 | static tree string_compare_decl; | |
6185 | left_tree = Gogo::call_builtin(&string_compare_decl, | |
6186 | location, | |
6187 | "__go_strcmp", | |
6188 | 2, | |
6189 | integer_type_node, | |
6190 | string_type, | |
6191 | left_tree, | |
6192 | string_type, | |
6193 | right_tree); | |
6194 | right_tree = build_int_cst_type(integer_type_node, 0); | |
6195 | } | |
15c67ee2 | 6196 | else if ((left_type->interface_type() != NULL |
6197 | && right_type->interface_type() == NULL | |
6198 | && !right_type->is_nil_type()) | |
6199 | || (left_type->interface_type() == NULL | |
6200 | && !left_type->is_nil_type() | |
6201 | && right_type->interface_type() != NULL)) | |
e440a328 | 6202 | { |
6203 | // Comparing an interface value to a non-interface value. | |
6204 | if (left_type->interface_type() == NULL) | |
6205 | { | |
6206 | std::swap(left_type, right_type); | |
6207 | std::swap(left_tree, right_tree); | |
6208 | } | |
6209 | ||
6210 | // The right operand is not an interface. We need to take its | |
6211 | // address if it is not a pointer. | |
6212 | tree make_tmp; | |
6213 | tree arg; | |
6214 | if (right_type->points_to() != NULL) | |
6215 | { | |
6216 | make_tmp = NULL_TREE; | |
6217 | arg = right_tree; | |
6218 | } | |
6219 | else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree)) | |
6220 | { | |
6221 | make_tmp = NULL_TREE; | |
6222 | arg = build_fold_addr_expr_loc(location, right_tree); | |
6223 | if (DECL_P(right_tree)) | |
6224 | TREE_ADDRESSABLE(right_tree) = 1; | |
6225 | } | |
6226 | else | |
6227 | { | |
6228 | tree tmp = create_tmp_var(TREE_TYPE(right_tree), | |
6229 | get_name(right_tree)); | |
6230 | DECL_IGNORED_P(tmp) = 0; | |
6231 | DECL_INITIAL(tmp) = right_tree; | |
6232 | TREE_ADDRESSABLE(tmp) = 1; | |
6233 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
6234 | SET_EXPR_LOCATION(make_tmp, location); | |
6235 | arg = build_fold_addr_expr_loc(location, tmp); | |
6236 | } | |
6237 | arg = fold_convert_loc(location, ptr_type_node, arg); | |
6238 | ||
6239 | tree descriptor = right_type->type_descriptor_pointer(context->gogo()); | |
6240 | ||
6241 | if (left_type->interface_type()->is_empty()) | |
6242 | { | |
6243 | static tree empty_interface_value_compare_decl; | |
6244 | left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl, | |
6245 | location, | |
6246 | "__go_empty_interface_value_compare", | |
6247 | 3, | |
6248 | integer_type_node, | |
6249 | TREE_TYPE(left_tree), | |
6250 | left_tree, | |
6251 | TREE_TYPE(descriptor), | |
6252 | descriptor, | |
6253 | ptr_type_node, | |
6254 | arg); | |
5fb82b5e | 6255 | if (left_tree == error_mark_node) |
6256 | return error_mark_node; | |
e440a328 | 6257 | // This can panic if the type is not comparable. |
6258 | TREE_NOTHROW(empty_interface_value_compare_decl) = 0; | |
6259 | } | |
6260 | else | |
6261 | { | |
6262 | static tree interface_value_compare_decl; | |
6263 | left_tree = Gogo::call_builtin(&interface_value_compare_decl, | |
6264 | location, | |
6265 | "__go_interface_value_compare", | |
6266 | 3, | |
6267 | integer_type_node, | |
6268 | TREE_TYPE(left_tree), | |
6269 | left_tree, | |
6270 | TREE_TYPE(descriptor), | |
6271 | descriptor, | |
6272 | ptr_type_node, | |
6273 | arg); | |
5fb82b5e | 6274 | if (left_tree == error_mark_node) |
6275 | return error_mark_node; | |
e440a328 | 6276 | // This can panic if the type is not comparable. |
6277 | TREE_NOTHROW(interface_value_compare_decl) = 0; | |
6278 | } | |
6279 | right_tree = build_int_cst_type(integer_type_node, 0); | |
6280 | ||
6281 | if (make_tmp != NULL_TREE) | |
6282 | left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp, | |
6283 | left_tree); | |
6284 | } | |
6285 | else if (left_type->interface_type() != NULL | |
6286 | && right_type->interface_type() != NULL) | |
6287 | { | |
739bad04 | 6288 | if (left_type->interface_type()->is_empty() |
6289 | && right_type->interface_type()->is_empty()) | |
e440a328 | 6290 | { |
e440a328 | 6291 | static tree empty_interface_compare_decl; |
6292 | left_tree = Gogo::call_builtin(&empty_interface_compare_decl, | |
6293 | location, | |
6294 | "__go_empty_interface_compare", | |
6295 | 2, | |
6296 | integer_type_node, | |
6297 | TREE_TYPE(left_tree), | |
6298 | left_tree, | |
6299 | TREE_TYPE(right_tree), | |
6300 | right_tree); | |
5fb82b5e | 6301 | if (left_tree == error_mark_node) |
6302 | return error_mark_node; | |
e440a328 | 6303 | // This can panic if the type is uncomparable. |
6304 | TREE_NOTHROW(empty_interface_compare_decl) = 0; | |
6305 | } | |
739bad04 | 6306 | else if (!left_type->interface_type()->is_empty() |
6307 | && !right_type->interface_type()->is_empty()) | |
e440a328 | 6308 | { |
e440a328 | 6309 | static tree interface_compare_decl; |
6310 | left_tree = Gogo::call_builtin(&interface_compare_decl, | |
6311 | location, | |
6312 | "__go_interface_compare", | |
6313 | 2, | |
6314 | integer_type_node, | |
6315 | TREE_TYPE(left_tree), | |
6316 | left_tree, | |
6317 | TREE_TYPE(right_tree), | |
6318 | right_tree); | |
5fb82b5e | 6319 | if (left_tree == error_mark_node) |
6320 | return error_mark_node; | |
e440a328 | 6321 | // This can panic if the type is uncomparable. |
6322 | TREE_NOTHROW(interface_compare_decl) = 0; | |
6323 | } | |
739bad04 | 6324 | else |
6325 | { | |
6326 | if (left_type->interface_type()->is_empty()) | |
6327 | { | |
6328 | gcc_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); | |
6329 | std::swap(left_type, right_type); | |
6330 | std::swap(left_tree, right_tree); | |
6331 | } | |
6332 | gcc_assert(!left_type->interface_type()->is_empty()); | |
6333 | gcc_assert(right_type->interface_type()->is_empty()); | |
6334 | static tree interface_empty_compare_decl; | |
6335 | left_tree = Gogo::call_builtin(&interface_empty_compare_decl, | |
6336 | location, | |
6337 | "__go_interface_empty_compare", | |
6338 | 2, | |
6339 | integer_type_node, | |
6340 | TREE_TYPE(left_tree), | |
6341 | left_tree, | |
6342 | TREE_TYPE(right_tree), | |
6343 | right_tree); | |
6344 | if (left_tree == error_mark_node) | |
6345 | return error_mark_node; | |
6346 | // This can panic if the type is uncomparable. | |
6347 | TREE_NOTHROW(interface_empty_compare_decl) = 0; | |
6348 | } | |
6349 | ||
e440a328 | 6350 | right_tree = build_int_cst_type(integer_type_node, 0); |
6351 | } | |
6352 | ||
6353 | if (left_type->is_nil_type() | |
6354 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6355 | { | |
6356 | std::swap(left_type, right_type); | |
6357 | std::swap(left_tree, right_tree); | |
6358 | } | |
6359 | ||
6360 | if (right_type->is_nil_type()) | |
6361 | { | |
6362 | if (left_type->array_type() != NULL | |
6363 | && left_type->array_type()->length() == NULL) | |
6364 | { | |
6365 | Array_type* at = left_type->array_type(); | |
6366 | left_tree = at->value_pointer_tree(context->gogo(), left_tree); | |
6367 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6368 | } | |
6369 | else if (left_type->interface_type() != NULL) | |
6370 | { | |
6371 | // An interface is nil if the first field is nil. | |
6372 | tree left_type_tree = TREE_TYPE(left_tree); | |
6373 | gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE); | |
6374 | tree field = TYPE_FIELDS(left_type_tree); | |
6375 | left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree, | |
6376 | field, NULL_TREE); | |
6377 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6378 | } | |
6379 | else | |
6380 | { | |
6381 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree))); | |
6382 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6383 | } | |
6384 | } | |
6385 | ||
d8ccb1e3 | 6386 | if (left_tree == error_mark_node || right_tree == error_mark_node) |
6387 | return error_mark_node; | |
6388 | ||
e440a328 | 6389 | tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree); |
6390 | if (CAN_HAVE_LOCATION_P(ret)) | |
6391 | SET_EXPR_LOCATION(ret, location); | |
6392 | return ret; | |
6393 | } | |
6394 | ||
6395 | // Class Bound_method_expression. | |
6396 | ||
6397 | // Traversal. | |
6398 | ||
6399 | int | |
6400 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6401 | { | |
6402 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
6403 | return TRAVERSE_EXIT; | |
6404 | return Expression::traverse(&this->method_, traverse); | |
6405 | } | |
6406 | ||
6407 | // Return the type of a bound method expression. The type of this | |
6408 | // object is really the type of the method with no receiver. We | |
6409 | // should be able to get away with just returning the type of the | |
6410 | // method. | |
6411 | ||
6412 | Type* | |
6413 | Bound_method_expression::do_type() | |
6414 | { | |
6415 | return this->method_->type(); | |
6416 | } | |
6417 | ||
6418 | // Determine the types of a method expression. | |
6419 | ||
6420 | void | |
6421 | Bound_method_expression::do_determine_type(const Type_context*) | |
6422 | { | |
6423 | this->method_->determine_type_no_context(); | |
6424 | Type* mtype = this->method_->type(); | |
6425 | Function_type* fntype = mtype == NULL ? NULL : mtype->function_type(); | |
6426 | if (fntype == NULL || !fntype->is_method()) | |
6427 | this->expr_->determine_type_no_context(); | |
6428 | else | |
6429 | { | |
6430 | Type_context subcontext(fntype->receiver()->type(), false); | |
6431 | this->expr_->determine_type(&subcontext); | |
6432 | } | |
6433 | } | |
6434 | ||
6435 | // Check the types of a method expression. | |
6436 | ||
6437 | void | |
6438 | Bound_method_expression::do_check_types(Gogo*) | |
6439 | { | |
6440 | Type* type = this->method_->type()->deref(); | |
6441 | if (type == NULL | |
6442 | || type->function_type() == NULL | |
6443 | || !type->function_type()->is_method()) | |
6444 | this->report_error(_("object is not a method")); | |
6445 | else | |
6446 | { | |
6447 | Type* rtype = type->function_type()->receiver()->type()->deref(); | |
6448 | Type* etype = (this->expr_type_ != NULL | |
6449 | ? this->expr_type_ | |
6450 | : this->expr_->type()); | |
6451 | etype = etype->deref(); | |
07ba8be5 | 6452 | if (!Type::are_identical(rtype, etype, true, NULL)) |
e440a328 | 6453 | this->report_error(_("method type does not match object type")); |
6454 | } | |
6455 | } | |
6456 | ||
6457 | // Get the tree for a method expression. There is no standard tree | |
6458 | // representation for this. The only places it may currently be used | |
6459 | // are in a Call_expression or a Go_statement, which will take it | |
6460 | // apart directly. So this has nothing to do at present. | |
6461 | ||
6462 | tree | |
6463 | Bound_method_expression::do_get_tree(Translate_context*) | |
6464 | { | |
d40405e2 | 6465 | error_at(this->location(), "reference to method other than calling it"); |
6466 | return error_mark_node; | |
e440a328 | 6467 | } |
6468 | ||
6469 | // Make a method expression. | |
6470 | ||
6471 | Bound_method_expression* | |
6472 | Expression::make_bound_method(Expression* expr, Expression* method, | |
6473 | source_location location) | |
6474 | { | |
6475 | return new Bound_method_expression(expr, method, location); | |
6476 | } | |
6477 | ||
6478 | // Class Builtin_call_expression. This is used for a call to a | |
6479 | // builtin function. | |
6480 | ||
6481 | class Builtin_call_expression : public Call_expression | |
6482 | { | |
6483 | public: | |
6484 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
6485 | bool is_varargs, source_location location); | |
6486 | ||
6487 | protected: | |
6488 | // This overrides Call_expression::do_lower. | |
6489 | Expression* | |
6490 | do_lower(Gogo*, Named_object*, int); | |
6491 | ||
6492 | bool | |
6493 | do_is_constant() const; | |
6494 | ||
6495 | bool | |
6496 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
6497 | ||
6498 | bool | |
6499 | do_float_constant_value(mpfr_t, Type**) const; | |
6500 | ||
6501 | bool | |
6502 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
6503 | ||
6504 | Type* | |
6505 | do_type(); | |
6506 | ||
6507 | void | |
6508 | do_determine_type(const Type_context*); | |
6509 | ||
6510 | void | |
6511 | do_check_types(Gogo*); | |
6512 | ||
6513 | Expression* | |
6514 | do_copy() | |
6515 | { | |
6516 | return new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
6517 | this->args()->copy(), | |
6518 | this->is_varargs(), | |
6519 | this->location()); | |
6520 | } | |
6521 | ||
6522 | tree | |
6523 | do_get_tree(Translate_context*); | |
6524 | ||
6525 | void | |
6526 | do_export(Export*) const; | |
6527 | ||
6528 | virtual bool | |
6529 | do_is_recover_call() const; | |
6530 | ||
6531 | virtual void | |
6532 | do_set_recover_arg(Expression*); | |
6533 | ||
6534 | private: | |
6535 | // The builtin functions. | |
6536 | enum Builtin_function_code | |
6537 | { | |
6538 | BUILTIN_INVALID, | |
6539 | ||
6540 | // Predeclared builtin functions. | |
6541 | BUILTIN_APPEND, | |
6542 | BUILTIN_CAP, | |
6543 | BUILTIN_CLOSE, | |
6544 | BUILTIN_CLOSED, | |
48080209 | 6545 | BUILTIN_COMPLEX, |
e440a328 | 6546 | BUILTIN_COPY, |
6547 | BUILTIN_IMAG, | |
6548 | BUILTIN_LEN, | |
6549 | BUILTIN_MAKE, | |
6550 | BUILTIN_NEW, | |
6551 | BUILTIN_PANIC, | |
6552 | BUILTIN_PRINT, | |
6553 | BUILTIN_PRINTLN, | |
6554 | BUILTIN_REAL, | |
6555 | BUILTIN_RECOVER, | |
6556 | ||
6557 | // Builtin functions from the unsafe package. | |
6558 | BUILTIN_ALIGNOF, | |
6559 | BUILTIN_OFFSETOF, | |
6560 | BUILTIN_SIZEOF | |
6561 | }; | |
6562 | ||
6563 | Expression* | |
6564 | one_arg() const; | |
6565 | ||
6566 | bool | |
6567 | check_one_arg(); | |
6568 | ||
6569 | static Type* | |
6570 | real_imag_type(Type*); | |
6571 | ||
6572 | static Type* | |
48080209 | 6573 | complex_type(Type*); |
e440a328 | 6574 | |
6575 | // A pointer back to the general IR structure. This avoids a global | |
6576 | // variable, or passing it around everywhere. | |
6577 | Gogo* gogo_; | |
6578 | // The builtin function being called. | |
6579 | Builtin_function_code code_; | |
0f914071 | 6580 | // Used to stop endless loops when the length of an array uses len |
6581 | // or cap of the array itself. | |
6582 | mutable bool seen_; | |
e440a328 | 6583 | }; |
6584 | ||
6585 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
6586 | Expression* fn, | |
6587 | Expression_list* args, | |
6588 | bool is_varargs, | |
6589 | source_location location) | |
6590 | : Call_expression(fn, args, is_varargs, location), | |
0f914071 | 6591 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false) |
e440a328 | 6592 | { |
6593 | Func_expression* fnexp = this->fn()->func_expression(); | |
6594 | gcc_assert(fnexp != NULL); | |
6595 | const std::string& name(fnexp->named_object()->name()); | |
6596 | if (name == "append") | |
6597 | this->code_ = BUILTIN_APPEND; | |
6598 | else if (name == "cap") | |
6599 | this->code_ = BUILTIN_CAP; | |
6600 | else if (name == "close") | |
6601 | this->code_ = BUILTIN_CLOSE; | |
6602 | else if (name == "closed") | |
6603 | this->code_ = BUILTIN_CLOSED; | |
48080209 | 6604 | else if (name == "complex") |
6605 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 6606 | else if (name == "copy") |
6607 | this->code_ = BUILTIN_COPY; | |
6608 | else if (name == "imag") | |
6609 | this->code_ = BUILTIN_IMAG; | |
6610 | else if (name == "len") | |
6611 | this->code_ = BUILTIN_LEN; | |
6612 | else if (name == "make") | |
6613 | this->code_ = BUILTIN_MAKE; | |
6614 | else if (name == "new") | |
6615 | this->code_ = BUILTIN_NEW; | |
6616 | else if (name == "panic") | |
6617 | this->code_ = BUILTIN_PANIC; | |
6618 | else if (name == "print") | |
6619 | this->code_ = BUILTIN_PRINT; | |
6620 | else if (name == "println") | |
6621 | this->code_ = BUILTIN_PRINTLN; | |
6622 | else if (name == "real") | |
6623 | this->code_ = BUILTIN_REAL; | |
6624 | else if (name == "recover") | |
6625 | this->code_ = BUILTIN_RECOVER; | |
6626 | else if (name == "Alignof") | |
6627 | this->code_ = BUILTIN_ALIGNOF; | |
6628 | else if (name == "Offsetof") | |
6629 | this->code_ = BUILTIN_OFFSETOF; | |
6630 | else if (name == "Sizeof") | |
6631 | this->code_ = BUILTIN_SIZEOF; | |
6632 | else | |
6633 | gcc_unreachable(); | |
6634 | } | |
6635 | ||
6636 | // Return whether this is a call to recover. This is a virtual | |
6637 | // function called from the parent class. | |
6638 | ||
6639 | bool | |
6640 | Builtin_call_expression::do_is_recover_call() const | |
6641 | { | |
6642 | if (this->classification() == EXPRESSION_ERROR) | |
6643 | return false; | |
6644 | return this->code_ == BUILTIN_RECOVER; | |
6645 | } | |
6646 | ||
6647 | // Set the argument for a call to recover. | |
6648 | ||
6649 | void | |
6650 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
6651 | { | |
6652 | const Expression_list* args = this->args(); | |
6653 | gcc_assert(args == NULL || args->empty()); | |
6654 | Expression_list* new_args = new Expression_list(); | |
6655 | new_args->push_back(arg); | |
6656 | this->set_args(new_args); | |
6657 | } | |
6658 | ||
6659 | // A traversal class which looks for a call expression. | |
6660 | ||
6661 | class Find_call_expression : public Traverse | |
6662 | { | |
6663 | public: | |
6664 | Find_call_expression() | |
6665 | : Traverse(traverse_expressions), | |
6666 | found_(false) | |
6667 | { } | |
6668 | ||
6669 | int | |
6670 | expression(Expression**); | |
6671 | ||
6672 | bool | |
6673 | found() | |
6674 | { return this->found_; } | |
6675 | ||
6676 | private: | |
6677 | bool found_; | |
6678 | }; | |
6679 | ||
6680 | int | |
6681 | Find_call_expression::expression(Expression** pexpr) | |
6682 | { | |
6683 | if ((*pexpr)->call_expression() != NULL) | |
6684 | { | |
6685 | this->found_ = true; | |
6686 | return TRAVERSE_EXIT; | |
6687 | } | |
6688 | return TRAVERSE_CONTINUE; | |
6689 | } | |
6690 | ||
6691 | // Lower a builtin call expression. This turns new and make into | |
6692 | // specific expressions. We also convert to a constant if we can. | |
6693 | ||
6694 | Expression* | |
6695 | Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
6696 | { | |
6697 | if (this->code_ == BUILTIN_NEW) | |
6698 | { | |
6699 | const Expression_list* args = this->args(); | |
6700 | if (args == NULL || args->size() < 1) | |
6701 | this->report_error(_("not enough arguments")); | |
6702 | else if (args->size() > 1) | |
6703 | this->report_error(_("too many arguments")); | |
6704 | else | |
6705 | { | |
6706 | Expression* arg = args->front(); | |
6707 | if (!arg->is_type_expression()) | |
6708 | { | |
6709 | error_at(arg->location(), "expected type"); | |
6710 | this->set_is_error(); | |
6711 | } | |
6712 | else | |
6713 | return Expression::make_allocation(arg->type(), this->location()); | |
6714 | } | |
6715 | } | |
6716 | else if (this->code_ == BUILTIN_MAKE) | |
6717 | { | |
6718 | const Expression_list* args = this->args(); | |
6719 | if (args == NULL || args->size() < 1) | |
6720 | this->report_error(_("not enough arguments")); | |
6721 | else | |
6722 | { | |
6723 | Expression* arg = args->front(); | |
6724 | if (!arg->is_type_expression()) | |
6725 | { | |
6726 | error_at(arg->location(), "expected type"); | |
6727 | this->set_is_error(); | |
6728 | } | |
6729 | else | |
6730 | { | |
6731 | Expression_list* newargs; | |
6732 | if (args->size() == 1) | |
6733 | newargs = NULL; | |
6734 | else | |
6735 | { | |
6736 | newargs = new Expression_list(); | |
6737 | Expression_list::const_iterator p = args->begin(); | |
6738 | ++p; | |
6739 | for (; p != args->end(); ++p) | |
6740 | newargs->push_back(*p); | |
6741 | } | |
6742 | return Expression::make_make(arg->type(), newargs, | |
6743 | this->location()); | |
6744 | } | |
6745 | } | |
6746 | } | |
6747 | else if (this->is_constant()) | |
6748 | { | |
6749 | // We can only lower len and cap if there are no function calls | |
6750 | // in the arguments. Otherwise we have to make the call. | |
6751 | if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP) | |
6752 | { | |
6753 | Expression* arg = this->one_arg(); | |
6754 | if (!arg->is_constant()) | |
6755 | { | |
6756 | Find_call_expression find_call; | |
6757 | Expression::traverse(&arg, &find_call); | |
6758 | if (find_call.found()) | |
6759 | return this; | |
6760 | } | |
6761 | } | |
6762 | ||
6763 | mpz_t ival; | |
6764 | mpz_init(ival); | |
6765 | Type* type; | |
6766 | if (this->integer_constant_value(true, ival, &type)) | |
6767 | { | |
6768 | Expression* ret = Expression::make_integer(&ival, type, | |
6769 | this->location()); | |
6770 | mpz_clear(ival); | |
6771 | return ret; | |
6772 | } | |
6773 | mpz_clear(ival); | |
6774 | ||
6775 | mpfr_t rval; | |
6776 | mpfr_init(rval); | |
6777 | if (this->float_constant_value(rval, &type)) | |
6778 | { | |
6779 | Expression* ret = Expression::make_float(&rval, type, | |
6780 | this->location()); | |
6781 | mpfr_clear(rval); | |
6782 | return ret; | |
6783 | } | |
6784 | ||
6785 | mpfr_t imag; | |
6786 | mpfr_init(imag); | |
6787 | if (this->complex_constant_value(rval, imag, &type)) | |
6788 | { | |
6789 | Expression* ret = Expression::make_complex(&rval, &imag, type, | |
6790 | this->location()); | |
6791 | mpfr_clear(rval); | |
6792 | mpfr_clear(imag); | |
6793 | return ret; | |
6794 | } | |
6795 | mpfr_clear(rval); | |
6796 | mpfr_clear(imag); | |
6797 | } | |
6798 | else if (this->code_ == BUILTIN_RECOVER) | |
6799 | { | |
6800 | if (function != NULL) | |
6801 | function->func_value()->set_calls_recover(); | |
6802 | else | |
6803 | { | |
6804 | // Calling recover outside of a function always returns the | |
6805 | // nil empty interface. | |
6806 | Type* eface = Type::make_interface_type(NULL, this->location()); | |
6807 | return Expression::make_cast(eface, | |
6808 | Expression::make_nil(this->location()), | |
6809 | this->location()); | |
6810 | } | |
6811 | } | |
6812 | else if (this->code_ == BUILTIN_APPEND) | |
6813 | { | |
6814 | // Lower the varargs. | |
6815 | const Expression_list* args = this->args(); | |
6816 | if (args == NULL || args->empty()) | |
6817 | return this; | |
6818 | Type* slice_type = args->front()->type(); | |
6819 | if (!slice_type->is_open_array_type()) | |
6820 | { | |
6821 | error_at(args->front()->location(), "argument 1 must be a slice"); | |
6822 | this->set_is_error(); | |
6823 | return this; | |
6824 | } | |
6825 | return this->lower_varargs(gogo, function, slice_type, 2); | |
6826 | } | |
6827 | ||
6828 | return this; | |
6829 | } | |
6830 | ||
6831 | // Return the type of the real or imag functions, given the type of | |
6832 | // the argument. We need to map complex to float, complex64 to | |
6833 | // float32, and complex128 to float64, so it has to be done by name. | |
6834 | // This returns NULL if it can't figure out the type. | |
6835 | ||
6836 | Type* | |
6837 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
6838 | { | |
6839 | if (arg_type == NULL || arg_type->is_abstract()) | |
6840 | return NULL; | |
6841 | Named_type* nt = arg_type->named_type(); | |
6842 | if (nt == NULL) | |
6843 | return NULL; | |
6844 | while (nt->real_type()->named_type() != NULL) | |
6845 | nt = nt->real_type()->named_type(); | |
48080209 | 6846 | if (nt->name() == "complex64") |
e440a328 | 6847 | return Type::lookup_float_type("float32"); |
6848 | else if (nt->name() == "complex128") | |
6849 | return Type::lookup_float_type("float64"); | |
6850 | else | |
6851 | return NULL; | |
6852 | } | |
6853 | ||
48080209 | 6854 | // Return the type of the complex function, given the type of one of the |
e440a328 | 6855 | // argments. Like real_imag_type, we have to map by name. |
6856 | ||
6857 | Type* | |
48080209 | 6858 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 6859 | { |
6860 | if (arg_type == NULL || arg_type->is_abstract()) | |
6861 | return NULL; | |
6862 | Named_type* nt = arg_type->named_type(); | |
6863 | if (nt == NULL) | |
6864 | return NULL; | |
6865 | while (nt->real_type()->named_type() != NULL) | |
6866 | nt = nt->real_type()->named_type(); | |
48080209 | 6867 | if (nt->name() == "float32") |
e440a328 | 6868 | return Type::lookup_complex_type("complex64"); |
6869 | else if (nt->name() == "float64") | |
6870 | return Type::lookup_complex_type("complex128"); | |
6871 | else | |
6872 | return NULL; | |
6873 | } | |
6874 | ||
6875 | // Return a single argument, or NULL if there isn't one. | |
6876 | ||
6877 | Expression* | |
6878 | Builtin_call_expression::one_arg() const | |
6879 | { | |
6880 | const Expression_list* args = this->args(); | |
6881 | if (args->size() != 1) | |
6882 | return NULL; | |
6883 | return args->front(); | |
6884 | } | |
6885 | ||
6886 | // Return whether this is constant: len of a string, or len or cap of | |
6887 | // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof. | |
6888 | ||
6889 | bool | |
6890 | Builtin_call_expression::do_is_constant() const | |
6891 | { | |
6892 | switch (this->code_) | |
6893 | { | |
6894 | case BUILTIN_LEN: | |
6895 | case BUILTIN_CAP: | |
6896 | { | |
0f914071 | 6897 | if (this->seen_) |
6898 | return false; | |
6899 | ||
e440a328 | 6900 | Expression* arg = this->one_arg(); |
6901 | if (arg == NULL) | |
6902 | return false; | |
6903 | Type* arg_type = arg->type(); | |
6904 | ||
6905 | if (arg_type->points_to() != NULL | |
6906 | && arg_type->points_to()->array_type() != NULL | |
6907 | && !arg_type->points_to()->is_open_array_type()) | |
6908 | arg_type = arg_type->points_to(); | |
6909 | ||
6910 | if (arg_type->array_type() != NULL | |
6911 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 6912 | return true; |
e440a328 | 6913 | |
6914 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 6915 | { |
6916 | this->seen_ = true; | |
6917 | bool ret = arg->is_constant(); | |
6918 | this->seen_ = false; | |
6919 | return ret; | |
6920 | } | |
e440a328 | 6921 | } |
6922 | break; | |
6923 | ||
6924 | case BUILTIN_SIZEOF: | |
6925 | case BUILTIN_ALIGNOF: | |
6926 | return this->one_arg() != NULL; | |
6927 | ||
6928 | case BUILTIN_OFFSETOF: | |
6929 | { | |
6930 | Expression* arg = this->one_arg(); | |
6931 | if (arg == NULL) | |
6932 | return false; | |
6933 | return arg->field_reference_expression() != NULL; | |
6934 | } | |
6935 | ||
48080209 | 6936 | case BUILTIN_COMPLEX: |
e440a328 | 6937 | { |
6938 | const Expression_list* args = this->args(); | |
6939 | if (args != NULL && args->size() == 2) | |
6940 | return args->front()->is_constant() && args->back()->is_constant(); | |
6941 | } | |
6942 | break; | |
6943 | ||
6944 | case BUILTIN_REAL: | |
6945 | case BUILTIN_IMAG: | |
6946 | { | |
6947 | Expression* arg = this->one_arg(); | |
6948 | return arg != NULL && arg->is_constant(); | |
6949 | } | |
6950 | ||
6951 | default: | |
6952 | break; | |
6953 | } | |
6954 | ||
6955 | return false; | |
6956 | } | |
6957 | ||
6958 | // Return an integer constant value if possible. | |
6959 | ||
6960 | bool | |
6961 | Builtin_call_expression::do_integer_constant_value(bool iota_is_constant, | |
6962 | mpz_t val, | |
6963 | Type** ptype) const | |
6964 | { | |
6965 | if (this->code_ == BUILTIN_LEN | |
6966 | || this->code_ == BUILTIN_CAP) | |
6967 | { | |
6968 | Expression* arg = this->one_arg(); | |
6969 | if (arg == NULL) | |
6970 | return false; | |
6971 | Type* arg_type = arg->type(); | |
6972 | ||
6973 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
6974 | { | |
6975 | std::string sval; | |
6976 | if (arg->string_constant_value(&sval)) | |
6977 | { | |
6978 | mpz_set_ui(val, sval.length()); | |
6979 | *ptype = Type::lookup_integer_type("int"); | |
6980 | return true; | |
6981 | } | |
6982 | } | |
6983 | ||
6984 | if (arg_type->points_to() != NULL | |
6985 | && arg_type->points_to()->array_type() != NULL | |
6986 | && !arg_type->points_to()->is_open_array_type()) | |
6987 | arg_type = arg_type->points_to(); | |
6988 | ||
6989 | if (arg_type->array_type() != NULL | |
6990 | && arg_type->array_type()->length() != NULL) | |
6991 | { | |
0f914071 | 6992 | if (this->seen_) |
6993 | return false; | |
e440a328 | 6994 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 6995 | this->seen_ = true; |
6996 | bool r = e->integer_constant_value(iota_is_constant, val, ptype); | |
6997 | this->seen_ = false; | |
6998 | if (r) | |
e440a328 | 6999 | { |
7000 | *ptype = Type::lookup_integer_type("int"); | |
7001 | return true; | |
7002 | } | |
7003 | } | |
7004 | } | |
7005 | else if (this->code_ == BUILTIN_SIZEOF | |
7006 | || this->code_ == BUILTIN_ALIGNOF) | |
7007 | { | |
7008 | Expression* arg = this->one_arg(); | |
7009 | if (arg == NULL) | |
7010 | return false; | |
7011 | Type* arg_type = arg->type(); | |
ef3f552a | 7012 | if (arg_type->is_error_type() || arg_type->is_undefined()) |
e440a328 | 7013 | return false; |
7014 | if (arg_type->is_abstract()) | |
7015 | return false; | |
9aa9e2df | 7016 | if (arg_type->named_type() != NULL) |
7017 | arg_type->named_type()->convert(this->gogo_); | |
e440a328 | 7018 | tree arg_type_tree = arg_type->get_tree(this->gogo_); |
f690b0bb | 7019 | if (arg_type_tree == error_mark_node) |
7020 | return false; | |
e440a328 | 7021 | unsigned long val_long; |
7022 | if (this->code_ == BUILTIN_SIZEOF) | |
7023 | { | |
7024 | tree type_size = TYPE_SIZE_UNIT(arg_type_tree); | |
7025 | gcc_assert(TREE_CODE(type_size) == INTEGER_CST); | |
7026 | if (TREE_INT_CST_HIGH(type_size) != 0) | |
7027 | return false; | |
7028 | unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size); | |
7029 | val_long = static_cast<unsigned long>(val_wide); | |
7030 | if (val_long != val_wide) | |
7031 | return false; | |
7032 | } | |
7033 | else if (this->code_ == BUILTIN_ALIGNOF) | |
7034 | { | |
637bd3af | 7035 | if (arg->field_reference_expression() == NULL) |
7036 | val_long = go_type_alignment(arg_type_tree); | |
7037 | else | |
e440a328 | 7038 | { |
7039 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
7040 | // the type of f when it is used as a field in a struct. | |
637bd3af | 7041 | val_long = go_field_alignment(arg_type_tree); |
e440a328 | 7042 | } |
e440a328 | 7043 | } |
7044 | else | |
7045 | gcc_unreachable(); | |
7046 | mpz_set_ui(val, val_long); | |
7047 | *ptype = NULL; | |
7048 | return true; | |
7049 | } | |
7050 | else if (this->code_ == BUILTIN_OFFSETOF) | |
7051 | { | |
7052 | Expression* arg = this->one_arg(); | |
7053 | if (arg == NULL) | |
7054 | return false; | |
7055 | Field_reference_expression* farg = arg->field_reference_expression(); | |
7056 | if (farg == NULL) | |
7057 | return false; | |
7058 | Expression* struct_expr = farg->expr(); | |
7059 | Type* st = struct_expr->type(); | |
7060 | if (st->struct_type() == NULL) | |
7061 | return false; | |
9aa9e2df | 7062 | if (st->named_type() != NULL) |
7063 | st->named_type()->convert(this->gogo_); | |
e440a328 | 7064 | tree struct_tree = st->get_tree(this->gogo_); |
7065 | gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE); | |
7066 | tree field = TYPE_FIELDS(struct_tree); | |
7067 | for (unsigned int index = farg->field_index(); index > 0; --index) | |
7068 | { | |
7069 | field = DECL_CHAIN(field); | |
7070 | gcc_assert(field != NULL_TREE); | |
7071 | } | |
7072 | HOST_WIDE_INT offset_wide = int_byte_position (field); | |
7073 | if (offset_wide < 0) | |
7074 | return false; | |
7075 | unsigned long offset_long = static_cast<unsigned long>(offset_wide); | |
7076 | if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide)) | |
7077 | return false; | |
7078 | mpz_set_ui(val, offset_long); | |
7079 | return true; | |
7080 | } | |
7081 | return false; | |
7082 | } | |
7083 | ||
7084 | // Return a floating point constant value if possible. | |
7085 | ||
7086 | bool | |
7087 | Builtin_call_expression::do_float_constant_value(mpfr_t val, | |
7088 | Type** ptype) const | |
7089 | { | |
7090 | if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) | |
7091 | { | |
7092 | Expression* arg = this->one_arg(); | |
7093 | if (arg == NULL) | |
7094 | return false; | |
7095 | ||
7096 | mpfr_t real; | |
7097 | mpfr_t imag; | |
7098 | mpfr_init(real); | |
7099 | mpfr_init(imag); | |
7100 | ||
7101 | bool ret = false; | |
7102 | Type* type; | |
7103 | if (arg->complex_constant_value(real, imag, &type)) | |
7104 | { | |
7105 | if (this->code_ == BUILTIN_REAL) | |
7106 | mpfr_set(val, real, GMP_RNDN); | |
7107 | else | |
7108 | mpfr_set(val, imag, GMP_RNDN); | |
7109 | *ptype = Builtin_call_expression::real_imag_type(type); | |
7110 | ret = true; | |
7111 | } | |
7112 | ||
7113 | mpfr_clear(real); | |
7114 | mpfr_clear(imag); | |
7115 | return ret; | |
7116 | } | |
7117 | ||
7118 | return false; | |
7119 | } | |
7120 | ||
7121 | // Return a complex constant value if possible. | |
7122 | ||
7123 | bool | |
7124 | Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
7125 | Type** ptype) const | |
7126 | { | |
48080209 | 7127 | if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 7128 | { |
7129 | const Expression_list* args = this->args(); | |
7130 | if (args == NULL || args->size() != 2) | |
7131 | return false; | |
7132 | ||
7133 | mpfr_t r; | |
7134 | mpfr_init(r); | |
7135 | Type* rtype; | |
7136 | if (!args->front()->float_constant_value(r, &rtype)) | |
7137 | { | |
7138 | mpfr_clear(r); | |
7139 | return false; | |
7140 | } | |
7141 | ||
7142 | mpfr_t i; | |
7143 | mpfr_init(i); | |
7144 | ||
7145 | bool ret = false; | |
7146 | Type* itype; | |
7147 | if (args->back()->float_constant_value(i, &itype) | |
07ba8be5 | 7148 | && Type::are_identical(rtype, itype, false, NULL)) |
e440a328 | 7149 | { |
7150 | mpfr_set(real, r, GMP_RNDN); | |
7151 | mpfr_set(imag, i, GMP_RNDN); | |
48080209 | 7152 | *ptype = Builtin_call_expression::complex_type(rtype); |
e440a328 | 7153 | ret = true; |
7154 | } | |
7155 | ||
7156 | mpfr_clear(r); | |
7157 | mpfr_clear(i); | |
7158 | ||
7159 | return ret; | |
7160 | } | |
7161 | ||
7162 | return false; | |
7163 | } | |
7164 | ||
7165 | // Return the type. | |
7166 | ||
7167 | Type* | |
7168 | Builtin_call_expression::do_type() | |
7169 | { | |
7170 | switch (this->code_) | |
7171 | { | |
7172 | case BUILTIN_INVALID: | |
7173 | default: | |
7174 | gcc_unreachable(); | |
7175 | ||
7176 | case BUILTIN_NEW: | |
7177 | case BUILTIN_MAKE: | |
7178 | { | |
7179 | const Expression_list* args = this->args(); | |
7180 | if (args == NULL || args->empty()) | |
7181 | return Type::make_error_type(); | |
7182 | return Type::make_pointer_type(args->front()->type()); | |
7183 | } | |
7184 | ||
7185 | case BUILTIN_CAP: | |
7186 | case BUILTIN_COPY: | |
7187 | case BUILTIN_LEN: | |
7188 | case BUILTIN_ALIGNOF: | |
7189 | case BUILTIN_OFFSETOF: | |
7190 | case BUILTIN_SIZEOF: | |
7191 | return Type::lookup_integer_type("int"); | |
7192 | ||
7193 | case BUILTIN_CLOSE: | |
7194 | case BUILTIN_PANIC: | |
7195 | case BUILTIN_PRINT: | |
7196 | case BUILTIN_PRINTLN: | |
7197 | return Type::make_void_type(); | |
7198 | ||
7199 | case BUILTIN_CLOSED: | |
7200 | return Type::lookup_bool_type(); | |
7201 | ||
7202 | case BUILTIN_RECOVER: | |
7203 | return Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
7204 | ||
7205 | case BUILTIN_APPEND: | |
7206 | { | |
7207 | const Expression_list* args = this->args(); | |
7208 | if (args == NULL || args->empty()) | |
7209 | return Type::make_error_type(); | |
7210 | return args->front()->type(); | |
7211 | } | |
7212 | ||
7213 | case BUILTIN_REAL: | |
7214 | case BUILTIN_IMAG: | |
7215 | { | |
7216 | Expression* arg = this->one_arg(); | |
7217 | if (arg == NULL) | |
7218 | return Type::make_error_type(); | |
7219 | Type* t = arg->type(); | |
7220 | if (t->is_abstract()) | |
7221 | t = t->make_non_abstract_type(); | |
7222 | t = Builtin_call_expression::real_imag_type(t); | |
7223 | if (t == NULL) | |
7224 | t = Type::make_error_type(); | |
7225 | return t; | |
7226 | } | |
7227 | ||
48080209 | 7228 | case BUILTIN_COMPLEX: |
e440a328 | 7229 | { |
7230 | const Expression_list* args = this->args(); | |
7231 | if (args == NULL || args->size() != 2) | |
7232 | return Type::make_error_type(); | |
7233 | Type* t = args->front()->type(); | |
7234 | if (t->is_abstract()) | |
7235 | { | |
7236 | t = args->back()->type(); | |
7237 | if (t->is_abstract()) | |
7238 | t = t->make_non_abstract_type(); | |
7239 | } | |
48080209 | 7240 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 7241 | if (t == NULL) |
7242 | t = Type::make_error_type(); | |
7243 | return t; | |
7244 | } | |
7245 | } | |
7246 | } | |
7247 | ||
7248 | // Determine the type. | |
7249 | ||
7250 | void | |
7251 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
7252 | { | |
7253 | this->fn()->determine_type_no_context(); | |
7254 | ||
7255 | const Expression_list* args = this->args(); | |
7256 | ||
7257 | bool is_print; | |
7258 | Type* arg_type = NULL; | |
7259 | switch (this->code_) | |
7260 | { | |
7261 | case BUILTIN_PRINT: | |
7262 | case BUILTIN_PRINTLN: | |
7263 | // Do not force a large integer constant to "int". | |
7264 | is_print = true; | |
7265 | break; | |
7266 | ||
7267 | case BUILTIN_REAL: | |
7268 | case BUILTIN_IMAG: | |
48080209 | 7269 | arg_type = Builtin_call_expression::complex_type(context->type); |
e440a328 | 7270 | is_print = false; |
7271 | break; | |
7272 | ||
48080209 | 7273 | case BUILTIN_COMPLEX: |
e440a328 | 7274 | { |
48080209 | 7275 | // For the complex function the type of one operand can |
e440a328 | 7276 | // determine the type of the other, as in a binary expression. |
7277 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
7278 | if (args != NULL && args->size() == 2) | |
7279 | { | |
7280 | Type* t1 = args->front()->type(); | |
7281 | Type* t2 = args->front()->type(); | |
7282 | if (!t1->is_abstract()) | |
7283 | arg_type = t1; | |
7284 | else if (!t2->is_abstract()) | |
7285 | arg_type = t2; | |
7286 | } | |
7287 | is_print = false; | |
7288 | } | |
7289 | break; | |
7290 | ||
7291 | default: | |
7292 | is_print = false; | |
7293 | break; | |
7294 | } | |
7295 | ||
7296 | if (args != NULL) | |
7297 | { | |
7298 | for (Expression_list::const_iterator pa = args->begin(); | |
7299 | pa != args->end(); | |
7300 | ++pa) | |
7301 | { | |
7302 | Type_context subcontext; | |
7303 | subcontext.type = arg_type; | |
7304 | ||
7305 | if (is_print) | |
7306 | { | |
7307 | // We want to print large constants, we so can't just | |
7308 | // use the appropriate nonabstract type. Use uint64 for | |
7309 | // an integer if we know it is nonnegative, otherwise | |
7310 | // use int64 for a integer, otherwise use float64 for a | |
7311 | // float or complex128 for a complex. | |
7312 | Type* want_type = NULL; | |
7313 | Type* atype = (*pa)->type(); | |
7314 | if (atype->is_abstract()) | |
7315 | { | |
7316 | if (atype->integer_type() != NULL) | |
7317 | { | |
7318 | mpz_t val; | |
7319 | mpz_init(val); | |
7320 | Type* dummy; | |
7321 | if (this->integer_constant_value(true, val, &dummy) | |
7322 | && mpz_sgn(val) >= 0) | |
7323 | want_type = Type::lookup_integer_type("uint64"); | |
7324 | else | |
7325 | want_type = Type::lookup_integer_type("int64"); | |
7326 | mpz_clear(val); | |
7327 | } | |
7328 | else if (atype->float_type() != NULL) | |
7329 | want_type = Type::lookup_float_type("float64"); | |
7330 | else if (atype->complex_type() != NULL) | |
7331 | want_type = Type::lookup_complex_type("complex128"); | |
7332 | else if (atype->is_abstract_string_type()) | |
7333 | want_type = Type::lookup_string_type(); | |
7334 | else if (atype->is_abstract_boolean_type()) | |
7335 | want_type = Type::lookup_bool_type(); | |
7336 | else | |
7337 | gcc_unreachable(); | |
7338 | subcontext.type = want_type; | |
7339 | } | |
7340 | } | |
7341 | ||
7342 | (*pa)->determine_type(&subcontext); | |
7343 | } | |
7344 | } | |
7345 | } | |
7346 | ||
7347 | // If there is exactly one argument, return true. Otherwise give an | |
7348 | // error message and return false. | |
7349 | ||
7350 | bool | |
7351 | Builtin_call_expression::check_one_arg() | |
7352 | { | |
7353 | const Expression_list* args = this->args(); | |
7354 | if (args == NULL || args->size() < 1) | |
7355 | { | |
7356 | this->report_error(_("not enough arguments")); | |
7357 | return false; | |
7358 | } | |
7359 | else if (args->size() > 1) | |
7360 | { | |
7361 | this->report_error(_("too many arguments")); | |
7362 | return false; | |
7363 | } | |
7364 | if (args->front()->is_error_expression() | |
4c0f874c | 7365 | || args->front()->type()->is_error_type() |
7366 | || args->front()->type()->is_undefined()) | |
e440a328 | 7367 | { |
7368 | this->set_is_error(); | |
7369 | return false; | |
7370 | } | |
7371 | return true; | |
7372 | } | |
7373 | ||
7374 | // Check argument types for a builtin function. | |
7375 | ||
7376 | void | |
7377 | Builtin_call_expression::do_check_types(Gogo*) | |
7378 | { | |
7379 | switch (this->code_) | |
7380 | { | |
7381 | case BUILTIN_INVALID: | |
7382 | case BUILTIN_NEW: | |
7383 | case BUILTIN_MAKE: | |
7384 | return; | |
7385 | ||
7386 | case BUILTIN_LEN: | |
7387 | case BUILTIN_CAP: | |
7388 | { | |
7389 | // The single argument may be either a string or an array or a | |
7390 | // map or a channel, or a pointer to a closed array. | |
7391 | if (this->check_one_arg()) | |
7392 | { | |
7393 | Type* arg_type = this->one_arg()->type(); | |
7394 | if (arg_type->points_to() != NULL | |
7395 | && arg_type->points_to()->array_type() != NULL | |
7396 | && !arg_type->points_to()->is_open_array_type()) | |
7397 | arg_type = arg_type->points_to(); | |
7398 | if (this->code_ == BUILTIN_CAP) | |
7399 | { | |
7400 | if (!arg_type->is_error_type() | |
7401 | && arg_type->array_type() == NULL | |
7402 | && arg_type->channel_type() == NULL) | |
7403 | this->report_error(_("argument must be array or slice " | |
7404 | "or channel")); | |
7405 | } | |
7406 | else | |
7407 | { | |
7408 | if (!arg_type->is_error_type() | |
7409 | && !arg_type->is_string_type() | |
7410 | && arg_type->array_type() == NULL | |
7411 | && arg_type->map_type() == NULL | |
7412 | && arg_type->channel_type() == NULL) | |
7413 | this->report_error(_("argument must be string or " | |
7414 | "array or slice or map or channel")); | |
7415 | } | |
7416 | } | |
7417 | } | |
7418 | break; | |
7419 | ||
7420 | case BUILTIN_PRINT: | |
7421 | case BUILTIN_PRINTLN: | |
7422 | { | |
7423 | const Expression_list* args = this->args(); | |
7424 | if (args == NULL) | |
7425 | { | |
7426 | if (this->code_ == BUILTIN_PRINT) | |
7427 | warning_at(this->location(), 0, | |
7428 | "no arguments for builtin function %<%s%>", | |
7429 | (this->code_ == BUILTIN_PRINT | |
7430 | ? "print" | |
7431 | : "println")); | |
7432 | } | |
7433 | else | |
7434 | { | |
7435 | for (Expression_list::const_iterator p = args->begin(); | |
7436 | p != args->end(); | |
7437 | ++p) | |
7438 | { | |
7439 | Type* type = (*p)->type(); | |
7440 | if (type->is_error_type() | |
7441 | || type->is_string_type() | |
7442 | || type->integer_type() != NULL | |
7443 | || type->float_type() != NULL | |
7444 | || type->complex_type() != NULL | |
7445 | || type->is_boolean_type() | |
7446 | || type->points_to() != NULL | |
7447 | || type->interface_type() != NULL | |
7448 | || type->channel_type() != NULL | |
7449 | || type->map_type() != NULL | |
7450 | || type->function_type() != NULL | |
7451 | || type->is_open_array_type()) | |
7452 | ; | |
7453 | else | |
7454 | this->report_error(_("unsupported argument type to " | |
7455 | "builtin function")); | |
7456 | } | |
7457 | } | |
7458 | } | |
7459 | break; | |
7460 | ||
7461 | case BUILTIN_CLOSE: | |
7462 | case BUILTIN_CLOSED: | |
7463 | if (this->check_one_arg()) | |
7464 | { | |
7465 | if (this->one_arg()->type()->channel_type() == NULL) | |
7466 | this->report_error(_("argument must be channel")); | |
7467 | } | |
7468 | break; | |
7469 | ||
7470 | case BUILTIN_PANIC: | |
7471 | case BUILTIN_SIZEOF: | |
7472 | case BUILTIN_ALIGNOF: | |
7473 | this->check_one_arg(); | |
7474 | break; | |
7475 | ||
7476 | case BUILTIN_RECOVER: | |
7477 | if (this->args() != NULL && !this->args()->empty()) | |
7478 | this->report_error(_("too many arguments")); | |
7479 | break; | |
7480 | ||
7481 | case BUILTIN_OFFSETOF: | |
7482 | if (this->check_one_arg()) | |
7483 | { | |
7484 | Expression* arg = this->one_arg(); | |
7485 | if (arg->field_reference_expression() == NULL) | |
7486 | this->report_error(_("argument must be a field reference")); | |
7487 | } | |
7488 | break; | |
7489 | ||
7490 | case BUILTIN_COPY: | |
7491 | { | |
7492 | const Expression_list* args = this->args(); | |
7493 | if (args == NULL || args->size() < 2) | |
7494 | { | |
7495 | this->report_error(_("not enough arguments")); | |
7496 | break; | |
7497 | } | |
7498 | else if (args->size() > 2) | |
7499 | { | |
7500 | this->report_error(_("too many arguments")); | |
7501 | break; | |
7502 | } | |
7503 | Type* arg1_type = args->front()->type(); | |
7504 | Type* arg2_type = args->back()->type(); | |
7505 | if (arg1_type->is_error_type() || arg2_type->is_error_type()) | |
7506 | break; | |
7507 | ||
7508 | Type* e1; | |
7509 | if (arg1_type->is_open_array_type()) | |
7510 | e1 = arg1_type->array_type()->element_type(); | |
7511 | else | |
7512 | { | |
7513 | this->report_error(_("left argument must be a slice")); | |
7514 | break; | |
7515 | } | |
7516 | ||
7517 | Type* e2; | |
7518 | if (arg2_type->is_open_array_type()) | |
7519 | e2 = arg2_type->array_type()->element_type(); | |
7520 | else if (arg2_type->is_string_type()) | |
7521 | e2 = Type::lookup_integer_type("uint8"); | |
7522 | else | |
7523 | { | |
7524 | this->report_error(_("right argument must be a slice or a string")); | |
7525 | break; | |
7526 | } | |
7527 | ||
07ba8be5 | 7528 | if (!Type::are_identical(e1, e2, true, NULL)) |
e440a328 | 7529 | this->report_error(_("element types must be the same")); |
7530 | } | |
7531 | break; | |
7532 | ||
7533 | case BUILTIN_APPEND: | |
7534 | { | |
7535 | const Expression_list* args = this->args(); | |
b0d311a1 | 7536 | if (args == NULL || args->size() < 2) |
e440a328 | 7537 | { |
7538 | this->report_error(_("not enough arguments")); | |
7539 | break; | |
7540 | } | |
0b7755ec | 7541 | if (args->size() > 2) |
7542 | { | |
7543 | this->report_error(_("too many arguments")); | |
7544 | break; | |
7545 | } | |
e440a328 | 7546 | std::string reason; |
7547 | if (!Type::are_assignable(args->front()->type(), args->back()->type(), | |
7548 | &reason)) | |
7549 | { | |
7550 | if (reason.empty()) | |
7551 | this->report_error(_("arguments 1 and 2 have different types")); | |
7552 | else | |
7553 | { | |
7554 | error_at(this->location(), | |
7555 | "arguments 1 and 2 have different types (%s)", | |
7556 | reason.c_str()); | |
7557 | this->set_is_error(); | |
7558 | } | |
7559 | } | |
7560 | break; | |
7561 | } | |
7562 | ||
7563 | case BUILTIN_REAL: | |
7564 | case BUILTIN_IMAG: | |
7565 | if (this->check_one_arg()) | |
7566 | { | |
7567 | if (this->one_arg()->type()->complex_type() == NULL) | |
7568 | this->report_error(_("argument must have complex type")); | |
7569 | } | |
7570 | break; | |
7571 | ||
48080209 | 7572 | case BUILTIN_COMPLEX: |
e440a328 | 7573 | { |
7574 | const Expression_list* args = this->args(); | |
7575 | if (args == NULL || args->size() < 2) | |
7576 | this->report_error(_("not enough arguments")); | |
7577 | else if (args->size() > 2) | |
7578 | this->report_error(_("too many arguments")); | |
7579 | else if (args->front()->is_error_expression() | |
7580 | || args->front()->type()->is_error_type() | |
7581 | || args->back()->is_error_expression() | |
7582 | || args->back()->type()->is_error_type()) | |
7583 | this->set_is_error(); | |
7584 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 7585 | args->back()->type(), true, NULL)) |
48080209 | 7586 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 7587 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 7588 | this->report_error(_("complex arguments must have " |
e440a328 | 7589 | "floating-point type")); |
7590 | } | |
7591 | break; | |
7592 | ||
7593 | default: | |
7594 | gcc_unreachable(); | |
7595 | } | |
7596 | } | |
7597 | ||
7598 | // Return the tree for a builtin function. | |
7599 | ||
7600 | tree | |
7601 | Builtin_call_expression::do_get_tree(Translate_context* context) | |
7602 | { | |
7603 | Gogo* gogo = context->gogo(); | |
7604 | source_location location = this->location(); | |
7605 | switch (this->code_) | |
7606 | { | |
7607 | case BUILTIN_INVALID: | |
7608 | case BUILTIN_NEW: | |
7609 | case BUILTIN_MAKE: | |
7610 | gcc_unreachable(); | |
7611 | ||
7612 | case BUILTIN_LEN: | |
7613 | case BUILTIN_CAP: | |
7614 | { | |
7615 | const Expression_list* args = this->args(); | |
7616 | gcc_assert(args != NULL && args->size() == 1); | |
7617 | Expression* arg = *args->begin(); | |
7618 | Type* arg_type = arg->type(); | |
0f914071 | 7619 | |
7620 | if (this->seen_) | |
7621 | { | |
7622 | gcc_assert(saw_errors()); | |
7623 | return error_mark_node; | |
7624 | } | |
7625 | this->seen_ = true; | |
7626 | ||
e440a328 | 7627 | tree arg_tree = arg->get_tree(context); |
0f914071 | 7628 | |
7629 | this->seen_ = false; | |
7630 | ||
e440a328 | 7631 | if (arg_tree == error_mark_node) |
7632 | return error_mark_node; | |
7633 | ||
7634 | if (arg_type->points_to() != NULL) | |
7635 | { | |
7636 | arg_type = arg_type->points_to(); | |
7637 | gcc_assert(arg_type->array_type() != NULL | |
7638 | && !arg_type->is_open_array_type()); | |
7639 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree))); | |
7640 | arg_tree = build_fold_indirect_ref(arg_tree); | |
7641 | } | |
7642 | ||
7643 | tree val_tree; | |
7644 | if (this->code_ == BUILTIN_LEN) | |
7645 | { | |
7646 | if (arg_type->is_string_type()) | |
7647 | val_tree = String_type::length_tree(gogo, arg_tree); | |
7648 | else if (arg_type->array_type() != NULL) | |
0f914071 | 7649 | { |
7650 | if (this->seen_) | |
7651 | { | |
7652 | gcc_assert(saw_errors()); | |
7653 | return error_mark_node; | |
7654 | } | |
7655 | this->seen_ = true; | |
7656 | val_tree = arg_type->array_type()->length_tree(gogo, arg_tree); | |
7657 | this->seen_ = false; | |
7658 | } | |
e440a328 | 7659 | else if (arg_type->map_type() != NULL) |
7660 | { | |
7661 | static tree map_len_fndecl; | |
7662 | val_tree = Gogo::call_builtin(&map_len_fndecl, | |
7663 | location, | |
7664 | "__go_map_len", | |
7665 | 1, | |
7666 | sizetype, | |
7667 | arg_type->get_tree(gogo), | |
7668 | arg_tree); | |
7669 | } | |
7670 | else if (arg_type->channel_type() != NULL) | |
7671 | { | |
7672 | static tree chan_len_fndecl; | |
7673 | val_tree = Gogo::call_builtin(&chan_len_fndecl, | |
7674 | location, | |
7675 | "__go_chan_len", | |
7676 | 1, | |
7677 | sizetype, | |
7678 | arg_type->get_tree(gogo), | |
7679 | arg_tree); | |
7680 | } | |
7681 | else | |
7682 | gcc_unreachable(); | |
7683 | } | |
7684 | else | |
7685 | { | |
7686 | if (arg_type->array_type() != NULL) | |
0f914071 | 7687 | { |
7688 | if (this->seen_) | |
7689 | { | |
7690 | gcc_assert(saw_errors()); | |
7691 | return error_mark_node; | |
7692 | } | |
7693 | this->seen_ = true; | |
7694 | val_tree = arg_type->array_type()->capacity_tree(gogo, | |
7695 | arg_tree); | |
7696 | this->seen_ = false; | |
7697 | } | |
e440a328 | 7698 | else if (arg_type->channel_type() != NULL) |
7699 | { | |
7700 | static tree chan_cap_fndecl; | |
7701 | val_tree = Gogo::call_builtin(&chan_cap_fndecl, | |
7702 | location, | |
7703 | "__go_chan_cap", | |
7704 | 1, | |
7705 | sizetype, | |
7706 | arg_type->get_tree(gogo), | |
7707 | arg_tree); | |
7708 | } | |
7709 | else | |
7710 | gcc_unreachable(); | |
7711 | } | |
7712 | ||
d8ccb1e3 | 7713 | if (val_tree == error_mark_node) |
7714 | return error_mark_node; | |
7715 | ||
e440a328 | 7716 | tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo); |
7717 | if (type_tree == TREE_TYPE(val_tree)) | |
7718 | return val_tree; | |
7719 | else | |
7720 | return fold(convert_to_integer(type_tree, val_tree)); | |
7721 | } | |
7722 | ||
7723 | case BUILTIN_PRINT: | |
7724 | case BUILTIN_PRINTLN: | |
7725 | { | |
7726 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
7727 | tree stmt_list = NULL_TREE; | |
7728 | ||
7729 | const Expression_list* call_args = this->args(); | |
7730 | if (call_args != NULL) | |
7731 | { | |
7732 | for (Expression_list::const_iterator p = call_args->begin(); | |
7733 | p != call_args->end(); | |
7734 | ++p) | |
7735 | { | |
7736 | if (is_ln && p != call_args->begin()) | |
7737 | { | |
7738 | static tree print_space_fndecl; | |
7739 | tree call = Gogo::call_builtin(&print_space_fndecl, | |
7740 | location, | |
7741 | "__go_print_space", | |
7742 | 0, | |
7743 | void_type_node); | |
5fb82b5e | 7744 | if (call == error_mark_node) |
7745 | return error_mark_node; | |
e440a328 | 7746 | append_to_statement_list(call, &stmt_list); |
7747 | } | |
7748 | ||
7749 | Type* type = (*p)->type(); | |
7750 | ||
7751 | tree arg = (*p)->get_tree(context); | |
7752 | if (arg == error_mark_node) | |
7753 | return error_mark_node; | |
7754 | ||
7755 | tree* pfndecl; | |
7756 | const char* fnname; | |
7757 | if (type->is_string_type()) | |
7758 | { | |
7759 | static tree print_string_fndecl; | |
7760 | pfndecl = &print_string_fndecl; | |
7761 | fnname = "__go_print_string"; | |
7762 | } | |
7763 | else if (type->integer_type() != NULL | |
7764 | && type->integer_type()->is_unsigned()) | |
7765 | { | |
7766 | static tree print_uint64_fndecl; | |
7767 | pfndecl = &print_uint64_fndecl; | |
7768 | fnname = "__go_print_uint64"; | |
7769 | Type* itype = Type::lookup_integer_type("uint64"); | |
7770 | arg = fold_convert_loc(location, itype->get_tree(gogo), | |
7771 | arg); | |
7772 | } | |
7773 | else if (type->integer_type() != NULL) | |
7774 | { | |
7775 | static tree print_int64_fndecl; | |
7776 | pfndecl = &print_int64_fndecl; | |
7777 | fnname = "__go_print_int64"; | |
7778 | Type* itype = Type::lookup_integer_type("int64"); | |
7779 | arg = fold_convert_loc(location, itype->get_tree(gogo), | |
7780 | arg); | |
7781 | } | |
7782 | else if (type->float_type() != NULL) | |
7783 | { | |
7784 | static tree print_double_fndecl; | |
7785 | pfndecl = &print_double_fndecl; | |
7786 | fnname = "__go_print_double"; | |
7787 | arg = fold_convert_loc(location, double_type_node, arg); | |
7788 | } | |
7789 | else if (type->complex_type() != NULL) | |
7790 | { | |
7791 | static tree print_complex_fndecl; | |
7792 | pfndecl = &print_complex_fndecl; | |
7793 | fnname = "__go_print_complex"; | |
7794 | arg = fold_convert_loc(location, complex_double_type_node, | |
7795 | arg); | |
7796 | } | |
7797 | else if (type->is_boolean_type()) | |
7798 | { | |
7799 | static tree print_bool_fndecl; | |
7800 | pfndecl = &print_bool_fndecl; | |
7801 | fnname = "__go_print_bool"; | |
7802 | } | |
7803 | else if (type->points_to() != NULL | |
7804 | || type->channel_type() != NULL | |
7805 | || type->map_type() != NULL | |
7806 | || type->function_type() != NULL) | |
7807 | { | |
7808 | static tree print_pointer_fndecl; | |
7809 | pfndecl = &print_pointer_fndecl; | |
7810 | fnname = "__go_print_pointer"; | |
7811 | arg = fold_convert_loc(location, ptr_type_node, arg); | |
7812 | } | |
7813 | else if (type->interface_type() != NULL) | |
7814 | { | |
7815 | if (type->interface_type()->is_empty()) | |
7816 | { | |
7817 | static tree print_empty_interface_fndecl; | |
7818 | pfndecl = &print_empty_interface_fndecl; | |
7819 | fnname = "__go_print_empty_interface"; | |
7820 | } | |
7821 | else | |
7822 | { | |
7823 | static tree print_interface_fndecl; | |
7824 | pfndecl = &print_interface_fndecl; | |
7825 | fnname = "__go_print_interface"; | |
7826 | } | |
7827 | } | |
7828 | else if (type->is_open_array_type()) | |
7829 | { | |
7830 | static tree print_slice_fndecl; | |
7831 | pfndecl = &print_slice_fndecl; | |
7832 | fnname = "__go_print_slice"; | |
7833 | } | |
7834 | else | |
7835 | gcc_unreachable(); | |
7836 | ||
7837 | tree call = Gogo::call_builtin(pfndecl, | |
7838 | location, | |
7839 | fnname, | |
7840 | 1, | |
7841 | void_type_node, | |
7842 | TREE_TYPE(arg), | |
7843 | arg); | |
5fb82b5e | 7844 | if (call == error_mark_node) |
7845 | return error_mark_node; | |
7846 | append_to_statement_list(call, &stmt_list); | |
e440a328 | 7847 | } |
7848 | } | |
7849 | ||
7850 | if (is_ln) | |
7851 | { | |
7852 | static tree print_nl_fndecl; | |
7853 | tree call = Gogo::call_builtin(&print_nl_fndecl, | |
7854 | location, | |
7855 | "__go_print_nl", | |
7856 | 0, | |
7857 | void_type_node); | |
5fb82b5e | 7858 | if (call == error_mark_node) |
7859 | return error_mark_node; | |
e440a328 | 7860 | append_to_statement_list(call, &stmt_list); |
7861 | } | |
7862 | ||
7863 | return stmt_list; | |
7864 | } | |
7865 | ||
7866 | case BUILTIN_PANIC: | |
7867 | { | |
7868 | const Expression_list* args = this->args(); | |
7869 | gcc_assert(args != NULL && args->size() == 1); | |
7870 | Expression* arg = args->front(); | |
7871 | tree arg_tree = arg->get_tree(context); | |
7872 | if (arg_tree == error_mark_node) | |
7873 | return error_mark_node; | |
7874 | Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
7875 | arg_tree = Expression::convert_for_assignment(context, empty, | |
7876 | arg->type(), | |
7877 | arg_tree, location); | |
7878 | static tree panic_fndecl; | |
7879 | tree call = Gogo::call_builtin(&panic_fndecl, | |
7880 | location, | |
7881 | "__go_panic", | |
7882 | 1, | |
7883 | void_type_node, | |
7884 | TREE_TYPE(arg_tree), | |
7885 | arg_tree); | |
5fb82b5e | 7886 | if (call == error_mark_node) |
7887 | return error_mark_node; | |
e440a328 | 7888 | // This function will throw an exception. |
7889 | TREE_NOTHROW(panic_fndecl) = 0; | |
7890 | // This function will not return. | |
7891 | TREE_THIS_VOLATILE(panic_fndecl) = 1; | |
7892 | return call; | |
7893 | } | |
7894 | ||
7895 | case BUILTIN_RECOVER: | |
7896 | { | |
7897 | // The argument is set when building recover thunks. It's a | |
7898 | // boolean value which is true if we can recover a value now. | |
7899 | const Expression_list* args = this->args(); | |
7900 | gcc_assert(args != NULL && args->size() == 1); | |
7901 | Expression* arg = args->front(); | |
7902 | tree arg_tree = arg->get_tree(context); | |
7903 | if (arg_tree == error_mark_node) | |
7904 | return error_mark_node; | |
7905 | ||
7906 | Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
7907 | tree empty_tree = empty->get_tree(context->gogo()); | |
7908 | ||
7909 | Type* nil_type = Type::make_nil_type(); | |
7910 | Expression* nil = Expression::make_nil(location); | |
7911 | tree nil_tree = nil->get_tree(context); | |
7912 | tree empty_nil_tree = Expression::convert_for_assignment(context, | |
7913 | empty, | |
7914 | nil_type, | |
7915 | nil_tree, | |
7916 | location); | |
7917 | ||
7918 | // We need to handle a deferred call to recover specially, | |
7919 | // because it changes whether it can recover a panic or not. | |
7920 | // See test7 in test/recover1.go. | |
7921 | tree call; | |
7922 | if (this->is_deferred()) | |
7923 | { | |
7924 | static tree deferred_recover_fndecl; | |
7925 | call = Gogo::call_builtin(&deferred_recover_fndecl, | |
7926 | location, | |
7927 | "__go_deferred_recover", | |
7928 | 0, | |
7929 | empty_tree); | |
7930 | } | |
7931 | else | |
7932 | { | |
7933 | static tree recover_fndecl; | |
7934 | call = Gogo::call_builtin(&recover_fndecl, | |
7935 | location, | |
7936 | "__go_recover", | |
7937 | 0, | |
7938 | empty_tree); | |
7939 | } | |
5fb82b5e | 7940 | if (call == error_mark_node) |
7941 | return error_mark_node; | |
e440a328 | 7942 | return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree, |
7943 | call, empty_nil_tree); | |
7944 | } | |
7945 | ||
7946 | case BUILTIN_CLOSE: | |
7947 | case BUILTIN_CLOSED: | |
7948 | { | |
7949 | const Expression_list* args = this->args(); | |
7950 | gcc_assert(args != NULL && args->size() == 1); | |
7951 | Expression* arg = args->front(); | |
7952 | tree arg_tree = arg->get_tree(context); | |
7953 | if (arg_tree == error_mark_node) | |
7954 | return error_mark_node; | |
7955 | if (this->code_ == BUILTIN_CLOSE) | |
7956 | { | |
7957 | static tree close_fndecl; | |
7958 | return Gogo::call_builtin(&close_fndecl, | |
7959 | location, | |
7960 | "__go_builtin_close", | |
7961 | 1, | |
7962 | void_type_node, | |
7963 | TREE_TYPE(arg_tree), | |
7964 | arg_tree); | |
7965 | } | |
7966 | else | |
7967 | { | |
7968 | static tree closed_fndecl; | |
7969 | return Gogo::call_builtin(&closed_fndecl, | |
7970 | location, | |
7971 | "__go_builtin_closed", | |
7972 | 1, | |
7973 | boolean_type_node, | |
7974 | TREE_TYPE(arg_tree), | |
7975 | arg_tree); | |
7976 | } | |
7977 | } | |
7978 | ||
7979 | case BUILTIN_SIZEOF: | |
7980 | case BUILTIN_OFFSETOF: | |
7981 | case BUILTIN_ALIGNOF: | |
7982 | { | |
7983 | mpz_t val; | |
7984 | mpz_init(val); | |
7985 | Type* dummy; | |
7986 | bool b = this->integer_constant_value(true, val, &dummy); | |
7f1d9abd | 7987 | if (!b) |
7988 | { | |
7989 | gcc_assert(saw_errors()); | |
7990 | return error_mark_node; | |
7991 | } | |
e440a328 | 7992 | tree type = Type::lookup_integer_type("int")->get_tree(gogo); |
7993 | tree ret = Expression::integer_constant_tree(val, type); | |
7994 | mpz_clear(val); | |
7995 | return ret; | |
7996 | } | |
7997 | ||
7998 | case BUILTIN_COPY: | |
7999 | { | |
8000 | const Expression_list* args = this->args(); | |
8001 | gcc_assert(args != NULL && args->size() == 2); | |
8002 | Expression* arg1 = args->front(); | |
8003 | Expression* arg2 = args->back(); | |
8004 | ||
8005 | tree arg1_tree = arg1->get_tree(context); | |
8006 | tree arg2_tree = arg2->get_tree(context); | |
8007 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
8008 | return error_mark_node; | |
8009 | ||
8010 | Type* arg1_type = arg1->type(); | |
8011 | Array_type* at = arg1_type->array_type(); | |
8012 | arg1_tree = save_expr(arg1_tree); | |
8013 | tree arg1_val = at->value_pointer_tree(gogo, arg1_tree); | |
8014 | tree arg1_len = at->length_tree(gogo, arg1_tree); | |
d8ccb1e3 | 8015 | if (arg1_val == error_mark_node || arg1_len == error_mark_node) |
8016 | return error_mark_node; | |
e440a328 | 8017 | |
8018 | Type* arg2_type = arg2->type(); | |
8019 | tree arg2_val; | |
8020 | tree arg2_len; | |
8021 | if (arg2_type->is_open_array_type()) | |
8022 | { | |
8023 | at = arg2_type->array_type(); | |
8024 | arg2_tree = save_expr(arg2_tree); | |
8025 | arg2_val = at->value_pointer_tree(gogo, arg2_tree); | |
8026 | arg2_len = at->length_tree(gogo, arg2_tree); | |
8027 | } | |
8028 | else | |
8029 | { | |
8030 | arg2_tree = save_expr(arg2_tree); | |
8031 | arg2_val = String_type::bytes_tree(gogo, arg2_tree); | |
8032 | arg2_len = String_type::length_tree(gogo, arg2_tree); | |
8033 | } | |
d8ccb1e3 | 8034 | if (arg2_val == error_mark_node || arg2_len == error_mark_node) |
8035 | return error_mark_node; | |
e440a328 | 8036 | |
8037 | arg1_len = save_expr(arg1_len); | |
8038 | arg2_len = save_expr(arg2_len); | |
8039 | tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len), | |
8040 | fold_build2_loc(location, LT_EXPR, | |
8041 | boolean_type_node, | |
8042 | arg1_len, arg2_len), | |
8043 | arg1_len, arg2_len); | |
8044 | len = save_expr(len); | |
8045 | ||
8046 | Type* element_type = at->element_type(); | |
8047 | tree element_type_tree = element_type->get_tree(gogo); | |
d8ccb1e3 | 8048 | if (element_type_tree == error_mark_node) |
8049 | return error_mark_node; | |
e440a328 | 8050 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
8051 | tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size), | |
8052 | len); | |
8053 | bytecount = fold_build2_loc(location, MULT_EXPR, | |
8054 | TREE_TYPE(element_size), | |
8055 | bytecount, element_size); | |
8056 | bytecount = fold_convert_loc(location, size_type_node, bytecount); | |
8057 | ||
3991cb03 | 8058 | arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val); |
8059 | arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val); | |
8060 | ||
8061 | static tree copy_fndecl; | |
8062 | tree call = Gogo::call_builtin(©_fndecl, | |
8063 | location, | |
8064 | "__go_copy", | |
8065 | 3, | |
8066 | void_type_node, | |
8067 | ptr_type_node, | |
8068 | arg1_val, | |
8069 | ptr_type_node, | |
8070 | arg2_val, | |
8071 | size_type_node, | |
8072 | bytecount); | |
8073 | if (call == error_mark_node) | |
8074 | return error_mark_node; | |
e440a328 | 8075 | |
8076 | return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len), | |
8077 | call, len); | |
8078 | } | |
8079 | ||
8080 | case BUILTIN_APPEND: | |
8081 | { | |
8082 | const Expression_list* args = this->args(); | |
8083 | gcc_assert(args != NULL && args->size() == 2); | |
8084 | Expression* arg1 = args->front(); | |
8085 | Expression* arg2 = args->back(); | |
8086 | ||
8087 | tree arg1_tree = arg1->get_tree(context); | |
8088 | tree arg2_tree = arg2->get_tree(context); | |
8089 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
8090 | return error_mark_node; | |
8091 | ||
9d44fbe3 | 8092 | Array_type* at = arg1->type()->array_type(); |
8093 | Type* element_type = at->element_type(); | |
8094 | ||
ed64c8e5 | 8095 | arg2_tree = Expression::convert_for_assignment(context, at, |
8096 | arg2->type(), | |
8097 | arg2_tree, | |
8098 | location); | |
8099 | if (arg2_tree == error_mark_node) | |
8100 | return error_mark_node; | |
8101 | ||
3991cb03 | 8102 | arg2_tree = save_expr(arg2_tree); |
ed64c8e5 | 8103 | tree arg2_val = at->value_pointer_tree(gogo, arg2_tree); |
8104 | tree arg2_len = at->length_tree(gogo, arg2_tree); | |
3991cb03 | 8105 | if (arg2_val == error_mark_node || arg2_len == error_mark_node) |
8106 | return error_mark_node; | |
8107 | arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val); | |
8108 | arg2_len = fold_convert_loc(location, size_type_node, arg2_len); | |
8109 | ||
8110 | tree element_type_tree = element_type->get_tree(gogo); | |
8111 | if (element_type_tree == error_mark_node) | |
8112 | return error_mark_node; | |
8113 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); | |
8114 | element_size = fold_convert_loc(location, size_type_node, | |
8115 | element_size); | |
e440a328 | 8116 | |
8117 | // We rebuild the decl each time since the slice types may | |
8118 | // change. | |
8119 | tree append_fndecl = NULL_TREE; | |
8120 | return Gogo::call_builtin(&append_fndecl, | |
8121 | location, | |
8122 | "__go_append", | |
3991cb03 | 8123 | 4, |
e440a328 | 8124 | TREE_TYPE(arg1_tree), |
e440a328 | 8125 | TREE_TYPE(arg1_tree), |
8126 | arg1_tree, | |
3991cb03 | 8127 | ptr_type_node, |
8128 | arg2_val, | |
8129 | size_type_node, | |
8130 | arg2_len, | |
8131 | size_type_node, | |
8132 | element_size); | |
e440a328 | 8133 | } |
8134 | ||
8135 | case BUILTIN_REAL: | |
8136 | case BUILTIN_IMAG: | |
8137 | { | |
8138 | const Expression_list* args = this->args(); | |
8139 | gcc_assert(args != NULL && args->size() == 1); | |
8140 | Expression* arg = args->front(); | |
8141 | tree arg_tree = arg->get_tree(context); | |
8142 | if (arg_tree == error_mark_node) | |
8143 | return error_mark_node; | |
8144 | gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree))); | |
8145 | if (this->code_ == BUILTIN_REAL) | |
8146 | return fold_build1_loc(location, REALPART_EXPR, | |
8147 | TREE_TYPE(TREE_TYPE(arg_tree)), | |
8148 | arg_tree); | |
8149 | else | |
8150 | return fold_build1_loc(location, IMAGPART_EXPR, | |
8151 | TREE_TYPE(TREE_TYPE(arg_tree)), | |
8152 | arg_tree); | |
8153 | } | |
8154 | ||
48080209 | 8155 | case BUILTIN_COMPLEX: |
e440a328 | 8156 | { |
8157 | const Expression_list* args = this->args(); | |
8158 | gcc_assert(args != NULL && args->size() == 2); | |
8159 | tree r = args->front()->get_tree(context); | |
8160 | tree i = args->back()->get_tree(context); | |
8161 | if (r == error_mark_node || i == error_mark_node) | |
8162 | return error_mark_node; | |
8163 | gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r)) | |
8164 | == TYPE_MAIN_VARIANT(TREE_TYPE(i))); | |
8165 | gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r))); | |
8166 | return fold_build2_loc(location, COMPLEX_EXPR, | |
8167 | build_complex_type(TREE_TYPE(r)), | |
8168 | r, i); | |
8169 | } | |
8170 | ||
8171 | default: | |
8172 | gcc_unreachable(); | |
8173 | } | |
8174 | } | |
8175 | ||
8176 | // We have to support exporting a builtin call expression, because | |
8177 | // code can set a constant to the result of a builtin expression. | |
8178 | ||
8179 | void | |
8180 | Builtin_call_expression::do_export(Export* exp) const | |
8181 | { | |
8182 | bool ok = false; | |
8183 | ||
8184 | mpz_t val; | |
8185 | mpz_init(val); | |
8186 | Type* dummy; | |
8187 | if (this->integer_constant_value(true, val, &dummy)) | |
8188 | { | |
8189 | Integer_expression::export_integer(exp, val); | |
8190 | ok = true; | |
8191 | } | |
8192 | mpz_clear(val); | |
8193 | ||
8194 | if (!ok) | |
8195 | { | |
8196 | mpfr_t fval; | |
8197 | mpfr_init(fval); | |
8198 | if (this->float_constant_value(fval, &dummy)) | |
8199 | { | |
8200 | Float_expression::export_float(exp, fval); | |
8201 | ok = true; | |
8202 | } | |
8203 | mpfr_clear(fval); | |
8204 | } | |
8205 | ||
8206 | if (!ok) | |
8207 | { | |
8208 | mpfr_t real; | |
8209 | mpfr_t imag; | |
8210 | mpfr_init(real); | |
8211 | mpfr_init(imag); | |
8212 | if (this->complex_constant_value(real, imag, &dummy)) | |
8213 | { | |
8214 | Complex_expression::export_complex(exp, real, imag); | |
8215 | ok = true; | |
8216 | } | |
8217 | mpfr_clear(real); | |
8218 | mpfr_clear(imag); | |
8219 | } | |
8220 | ||
8221 | if (!ok) | |
8222 | { | |
8223 | error_at(this->location(), "value is not constant"); | |
8224 | return; | |
8225 | } | |
8226 | ||
8227 | // A trailing space lets us reliably identify the end of the number. | |
8228 | exp->write_c_string(" "); | |
8229 | } | |
8230 | ||
8231 | // Class Call_expression. | |
8232 | ||
8233 | // Traversal. | |
8234 | ||
8235 | int | |
8236 | Call_expression::do_traverse(Traverse* traverse) | |
8237 | { | |
8238 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) | |
8239 | return TRAVERSE_EXIT; | |
8240 | if (this->args_ != NULL) | |
8241 | { | |
8242 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
8243 | return TRAVERSE_EXIT; | |
8244 | } | |
8245 | return TRAVERSE_CONTINUE; | |
8246 | } | |
8247 | ||
8248 | // Lower a call statement. | |
8249 | ||
8250 | Expression* | |
8251 | Call_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
8252 | { | |
8253 | // A type case can look like a function call. | |
8254 | if (this->fn_->is_type_expression() | |
8255 | && this->args_ != NULL | |
8256 | && this->args_->size() == 1) | |
8257 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
8258 | this->location()); | |
8259 | ||
8260 | // Recognize a call to a builtin function. | |
8261 | Func_expression* fne = this->fn_->func_expression(); | |
8262 | if (fne != NULL | |
8263 | && fne->named_object()->is_function_declaration() | |
8264 | && fne->named_object()->func_declaration_value()->type()->is_builtin()) | |
8265 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
8266 | this->is_varargs_, this->location()); | |
8267 | ||
8268 | // Handle an argument which is a call to a function which returns | |
8269 | // multiple results. | |
8270 | if (this->args_ != NULL | |
8271 | && this->args_->size() == 1 | |
8272 | && this->args_->front()->call_expression() != NULL | |
8273 | && this->fn_->type()->function_type() != NULL) | |
8274 | { | |
8275 | Function_type* fntype = this->fn_->type()->function_type(); | |
8276 | size_t rc = this->args_->front()->call_expression()->result_count(); | |
8277 | if (rc > 1 | |
8278 | && fntype->parameters() != NULL | |
8279 | && (fntype->parameters()->size() == rc | |
8280 | || (fntype->is_varargs() | |
8281 | && fntype->parameters()->size() - 1 <= rc))) | |
8282 | { | |
8283 | Call_expression* call = this->args_->front()->call_expression(); | |
8284 | Expression_list* args = new Expression_list; | |
8285 | for (size_t i = 0; i < rc; ++i) | |
8286 | args->push_back(Expression::make_call_result(call, i)); | |
8287 | // We can't return a new call expression here, because this | |
42535814 | 8288 | // one may be referenced by Call_result expressions. We |
8289 | // also can't delete the old arguments, because we may still | |
8290 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 8291 | this->args_ = args; |
8292 | } | |
8293 | } | |
8294 | ||
8295 | // Handle a call to a varargs function by packaging up the extra | |
8296 | // parameters. | |
8297 | if (this->fn_->type()->function_type() != NULL | |
8298 | && this->fn_->type()->function_type()->is_varargs()) | |
8299 | { | |
8300 | Function_type* fntype = this->fn_->type()->function_type(); | |
8301 | const Typed_identifier_list* parameters = fntype->parameters(); | |
8302 | gcc_assert(parameters != NULL && !parameters->empty()); | |
8303 | Type* varargs_type = parameters->back().type(); | |
8304 | return this->lower_varargs(gogo, function, varargs_type, | |
8305 | parameters->size()); | |
8306 | } | |
8307 | ||
8308 | return this; | |
8309 | } | |
8310 | ||
8311 | // Lower a call to a varargs function. FUNCTION is the function in | |
8312 | // which the call occurs--it's not the function we are calling. | |
8313 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
8314 | // PARAM_COUNT is the number of parameters of the function we are | |
8315 | // calling; the last of these parameters will be the varargs | |
8316 | // parameter. | |
8317 | ||
8318 | Expression* | |
8319 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, | |
8320 | Type* varargs_type, size_t param_count) | |
8321 | { | |
8322 | if (this->varargs_are_lowered_) | |
8323 | return this; | |
8324 | ||
8325 | source_location loc = this->location(); | |
8326 | ||
8327 | gcc_assert(param_count > 0); | |
8328 | gcc_assert(varargs_type->is_open_array_type()); | |
8329 | ||
8330 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
8331 | if (arg_count < param_count - 1) | |
8332 | { | |
8333 | // Not enough arguments; will be caught in check_types. | |
8334 | return this; | |
8335 | } | |
8336 | ||
8337 | Expression_list* old_args = this->args_; | |
8338 | Expression_list* new_args = new Expression_list(); | |
8339 | bool push_empty_arg = false; | |
8340 | if (old_args == NULL || old_args->empty()) | |
8341 | { | |
8342 | gcc_assert(param_count == 1); | |
8343 | push_empty_arg = true; | |
8344 | } | |
8345 | else | |
8346 | { | |
8347 | Expression_list::const_iterator pa; | |
8348 | int i = 1; | |
8349 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
8350 | { | |
8351 | if (static_cast<size_t>(i) == param_count) | |
8352 | break; | |
8353 | new_args->push_back(*pa); | |
8354 | } | |
8355 | ||
8356 | // We have reached the varargs parameter. | |
8357 | ||
8358 | bool issued_error = false; | |
8359 | if (pa == old_args->end()) | |
8360 | push_empty_arg = true; | |
8361 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
8362 | new_args->push_back(*pa); | |
8363 | else if (this->is_varargs_) | |
8364 | { | |
8365 | this->report_error(_("too many arguments")); | |
8366 | return this; | |
8367 | } | |
e440a328 | 8368 | else |
8369 | { | |
8370 | Type* element_type = varargs_type->array_type()->element_type(); | |
8371 | Expression_list* vals = new Expression_list; | |
8372 | for (; pa != old_args->end(); ++pa, ++i) | |
8373 | { | |
8374 | // Check types here so that we get a better message. | |
8375 | Type* patype = (*pa)->type(); | |
8376 | source_location paloc = (*pa)->location(); | |
8377 | if (!this->check_argument_type(i, element_type, patype, | |
8378 | paloc, issued_error)) | |
8379 | continue; | |
8380 | vals->push_back(*pa); | |
8381 | } | |
8382 | Expression* val = | |
8383 | Expression::make_slice_composite_literal(varargs_type, vals, loc); | |
8384 | new_args->push_back(val); | |
8385 | } | |
8386 | } | |
8387 | ||
8388 | if (push_empty_arg) | |
8389 | new_args->push_back(Expression::make_nil(loc)); | |
8390 | ||
8391 | // We can't return a new call expression here, because this one may | |
8392 | // be referenced by Call_result expressions. FIXME. | |
8393 | if (old_args != NULL) | |
8394 | delete old_args; | |
8395 | this->args_ = new_args; | |
8396 | this->varargs_are_lowered_ = true; | |
8397 | ||
8398 | // Lower all the new subexpressions. | |
8399 | Expression* ret = this; | |
8400 | gogo->lower_expression(function, &ret); | |
8401 | gcc_assert(ret == this); | |
8402 | return ret; | |
8403 | } | |
8404 | ||
e440a328 | 8405 | // Get the function type. Returns NULL if we don't know the type. If |
8406 | // this returns NULL, and if_ERROR is true, issues an error. | |
8407 | ||
8408 | Function_type* | |
8409 | Call_expression::get_function_type() const | |
8410 | { | |
8411 | return this->fn_->type()->function_type(); | |
8412 | } | |
8413 | ||
8414 | // Return the number of values which this call will return. | |
8415 | ||
8416 | size_t | |
8417 | Call_expression::result_count() const | |
8418 | { | |
8419 | const Function_type* fntype = this->get_function_type(); | |
8420 | if (fntype == NULL) | |
8421 | return 0; | |
8422 | if (fntype->results() == NULL) | |
8423 | return 0; | |
8424 | return fntype->results()->size(); | |
8425 | } | |
8426 | ||
8427 | // Return whether this is a call to the predeclared function recover. | |
8428 | ||
8429 | bool | |
8430 | Call_expression::is_recover_call() const | |
8431 | { | |
8432 | return this->do_is_recover_call(); | |
8433 | } | |
8434 | ||
8435 | // Set the argument to the recover function. | |
8436 | ||
8437 | void | |
8438 | Call_expression::set_recover_arg(Expression* arg) | |
8439 | { | |
8440 | this->do_set_recover_arg(arg); | |
8441 | } | |
8442 | ||
8443 | // Virtual functions also implemented by Builtin_call_expression. | |
8444 | ||
8445 | bool | |
8446 | Call_expression::do_is_recover_call() const | |
8447 | { | |
8448 | return false; | |
8449 | } | |
8450 | ||
8451 | void | |
8452 | Call_expression::do_set_recover_arg(Expression*) | |
8453 | { | |
8454 | gcc_unreachable(); | |
8455 | } | |
8456 | ||
8457 | // Get the type. | |
8458 | ||
8459 | Type* | |
8460 | Call_expression::do_type() | |
8461 | { | |
8462 | if (this->type_ != NULL) | |
8463 | return this->type_; | |
8464 | ||
8465 | Type* ret; | |
8466 | Function_type* fntype = this->get_function_type(); | |
8467 | if (fntype == NULL) | |
8468 | return Type::make_error_type(); | |
8469 | ||
8470 | const Typed_identifier_list* results = fntype->results(); | |
8471 | if (results == NULL) | |
8472 | ret = Type::make_void_type(); | |
8473 | else if (results->size() == 1) | |
8474 | ret = results->begin()->type(); | |
8475 | else | |
8476 | ret = Type::make_call_multiple_result_type(this); | |
8477 | ||
8478 | this->type_ = ret; | |
8479 | ||
8480 | return this->type_; | |
8481 | } | |
8482 | ||
8483 | // Determine types for a call expression. We can use the function | |
8484 | // parameter types to set the types of the arguments. | |
8485 | ||
8486 | void | |
8487 | Call_expression::do_determine_type(const Type_context*) | |
8488 | { | |
8489 | this->fn_->determine_type_no_context(); | |
8490 | Function_type* fntype = this->get_function_type(); | |
8491 | const Typed_identifier_list* parameters = NULL; | |
8492 | if (fntype != NULL) | |
8493 | parameters = fntype->parameters(); | |
8494 | if (this->args_ != NULL) | |
8495 | { | |
8496 | Typed_identifier_list::const_iterator pt; | |
8497 | if (parameters != NULL) | |
8498 | pt = parameters->begin(); | |
8499 | for (Expression_list::const_iterator pa = this->args_->begin(); | |
8500 | pa != this->args_->end(); | |
8501 | ++pa) | |
8502 | { | |
8503 | if (parameters != NULL && pt != parameters->end()) | |
8504 | { | |
8505 | Type_context subcontext(pt->type(), false); | |
8506 | (*pa)->determine_type(&subcontext); | |
8507 | ++pt; | |
8508 | } | |
8509 | else | |
8510 | (*pa)->determine_type_no_context(); | |
8511 | } | |
8512 | } | |
8513 | } | |
8514 | ||
8515 | // Check types for parameter I. | |
8516 | ||
8517 | bool | |
8518 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
8519 | const Type* argument_type, | |
8520 | source_location argument_location, | |
8521 | bool issued_error) | |
8522 | { | |
8523 | std::string reason; | |
8524 | if (!Type::are_assignable(parameter_type, argument_type, &reason)) | |
8525 | { | |
8526 | if (!issued_error) | |
8527 | { | |
8528 | if (reason.empty()) | |
8529 | error_at(argument_location, "argument %d has incompatible type", i); | |
8530 | else | |
8531 | error_at(argument_location, | |
8532 | "argument %d has incompatible type (%s)", | |
8533 | i, reason.c_str()); | |
8534 | } | |
8535 | this->set_is_error(); | |
8536 | return false; | |
8537 | } | |
8538 | return true; | |
8539 | } | |
8540 | ||
8541 | // Check types. | |
8542 | ||
8543 | void | |
8544 | Call_expression::do_check_types(Gogo*) | |
8545 | { | |
8546 | Function_type* fntype = this->get_function_type(); | |
8547 | if (fntype == NULL) | |
8548 | { | |
8549 | if (!this->fn_->type()->is_error_type()) | |
8550 | this->report_error(_("expected function")); | |
8551 | return; | |
8552 | } | |
8553 | ||
8554 | if (fntype->is_method()) | |
8555 | { | |
8556 | // We don't support pointers to methods, so the function has to | |
8557 | // be a bound method expression. | |
8558 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
8559 | if (bme == NULL) | |
8560 | { | |
8561 | this->report_error(_("method call without object")); | |
8562 | return; | |
8563 | } | |
8564 | Type* first_arg_type = bme->first_argument()->type(); | |
8565 | if (first_arg_type->points_to() == NULL) | |
8566 | { | |
8567 | // When passing a value, we need to check that we are | |
8568 | // permitted to copy it. | |
8569 | std::string reason; | |
8570 | if (!Type::are_assignable(fntype->receiver()->type(), | |
8571 | first_arg_type, &reason)) | |
8572 | { | |
8573 | if (reason.empty()) | |
8574 | this->report_error(_("incompatible type for receiver")); | |
8575 | else | |
8576 | { | |
8577 | error_at(this->location(), | |
8578 | "incompatible type for receiver (%s)", | |
8579 | reason.c_str()); | |
8580 | this->set_is_error(); | |
8581 | } | |
8582 | } | |
8583 | } | |
8584 | } | |
8585 | ||
8586 | // Note that varargs was handled by the lower_varargs() method, so | |
8587 | // we don't have to worry about it here. | |
8588 | ||
8589 | const Typed_identifier_list* parameters = fntype->parameters(); | |
8590 | if (this->args_ == NULL) | |
8591 | { | |
8592 | if (parameters != NULL && !parameters->empty()) | |
8593 | this->report_error(_("not enough arguments")); | |
8594 | } | |
8595 | else if (parameters == NULL) | |
8596 | this->report_error(_("too many arguments")); | |
8597 | else | |
8598 | { | |
8599 | int i = 0; | |
8600 | Typed_identifier_list::const_iterator pt = parameters->begin(); | |
8601 | for (Expression_list::const_iterator pa = this->args_->begin(); | |
8602 | pa != this->args_->end(); | |
8603 | ++pa, ++pt, ++i) | |
8604 | { | |
8605 | if (pt == parameters->end()) | |
8606 | { | |
8607 | this->report_error(_("too many arguments")); | |
8608 | return; | |
8609 | } | |
8610 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
8611 | (*pa)->location(), false); | |
8612 | } | |
8613 | if (pt != parameters->end()) | |
8614 | this->report_error(_("not enough arguments")); | |
8615 | } | |
8616 | } | |
8617 | ||
8618 | // Return whether we have to use a temporary variable to ensure that | |
8619 | // we evaluate this call expression in order. If the call returns no | |
8620 | // results then it will inevitably be executed last. If the call | |
8621 | // returns more than one result then it will be used with Call_result | |
8622 | // expressions. So we only have to use a temporary variable if the | |
8623 | // call returns exactly one result. | |
8624 | ||
8625 | bool | |
8626 | Call_expression::do_must_eval_in_order() const | |
8627 | { | |
8628 | return this->result_count() == 1; | |
8629 | } | |
8630 | ||
8631 | // Get the function and the first argument to use when calling a bound | |
8632 | // method. | |
8633 | ||
8634 | tree | |
8635 | Call_expression::bound_method_function(Translate_context* context, | |
8636 | Bound_method_expression* bound_method, | |
8637 | tree* first_arg_ptr) | |
8638 | { | |
8639 | Expression* first_argument = bound_method->first_argument(); | |
8640 | tree first_arg = first_argument->get_tree(context); | |
8641 | if (first_arg == error_mark_node) | |
8642 | return error_mark_node; | |
8643 | ||
8644 | // We always pass a pointer to the first argument when calling a | |
8645 | // method. | |
8646 | if (first_argument->type()->points_to() == NULL) | |
8647 | { | |
8648 | tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg)); | |
8649 | if (TREE_ADDRESSABLE(TREE_TYPE(first_arg)) | |
8650 | || DECL_P(first_arg) | |
8651 | || TREE_CODE(first_arg) == INDIRECT_REF | |
8652 | || TREE_CODE(first_arg) == COMPONENT_REF) | |
8653 | { | |
8654 | first_arg = build_fold_addr_expr(first_arg); | |
8655 | if (DECL_P(first_arg)) | |
8656 | TREE_ADDRESSABLE(first_arg) = 1; | |
8657 | } | |
8658 | else | |
8659 | { | |
8660 | tree tmp = create_tmp_var(TREE_TYPE(first_arg), | |
8661 | get_name(first_arg)); | |
8662 | DECL_IGNORED_P(tmp) = 0; | |
8663 | DECL_INITIAL(tmp) = first_arg; | |
8664 | first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type, | |
8665 | build1(DECL_EXPR, void_type_node, tmp), | |
8666 | build_fold_addr_expr(tmp)); | |
8667 | TREE_ADDRESSABLE(tmp) = 1; | |
8668 | } | |
8669 | if (first_arg == error_mark_node) | |
8670 | return error_mark_node; | |
8671 | } | |
8672 | ||
8673 | Type* fatype = bound_method->first_argument_type(); | |
8674 | if (fatype != NULL) | |
8675 | { | |
8676 | if (fatype->points_to() == NULL) | |
8677 | fatype = Type::make_pointer_type(fatype); | |
8678 | first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg); | |
8679 | if (first_arg == error_mark_node | |
8680 | || TREE_TYPE(first_arg) == error_mark_node) | |
8681 | return error_mark_node; | |
8682 | } | |
8683 | ||
8684 | *first_arg_ptr = first_arg; | |
8685 | ||
8686 | return bound_method->method()->get_tree(context); | |
8687 | } | |
8688 | ||
8689 | // Get the function and the first argument to use when calling an | |
8690 | // interface method. | |
8691 | ||
8692 | tree | |
8693 | Call_expression::interface_method_function( | |
8694 | Translate_context* context, | |
8695 | Interface_field_reference_expression* interface_method, | |
8696 | tree* first_arg_ptr) | |
8697 | { | |
8698 | tree expr = interface_method->expr()->get_tree(context); | |
8699 | if (expr == error_mark_node) | |
8700 | return error_mark_node; | |
8701 | expr = save_expr(expr); | |
8702 | tree first_arg = interface_method->get_underlying_object_tree(context, expr); | |
8703 | if (first_arg == error_mark_node) | |
8704 | return error_mark_node; | |
8705 | *first_arg_ptr = first_arg; | |
8706 | return interface_method->get_function_tree(context, expr); | |
8707 | } | |
8708 | ||
8709 | // Build the call expression. | |
8710 | ||
8711 | tree | |
8712 | Call_expression::do_get_tree(Translate_context* context) | |
8713 | { | |
8714 | if (this->tree_ != NULL_TREE) | |
8715 | return this->tree_; | |
8716 | ||
8717 | Function_type* fntype = this->get_function_type(); | |
8718 | if (fntype == NULL) | |
8719 | return error_mark_node; | |
8720 | ||
8721 | if (this->fn_->is_error_expression()) | |
8722 | return error_mark_node; | |
8723 | ||
8724 | Gogo* gogo = context->gogo(); | |
8725 | source_location location = this->location(); | |
8726 | ||
8727 | Func_expression* func = this->fn_->func_expression(); | |
8728 | Bound_method_expression* bound_method = this->fn_->bound_method_expression(); | |
8729 | Interface_field_reference_expression* interface_method = | |
8730 | this->fn_->interface_field_reference_expression(); | |
8731 | const bool has_closure = func != NULL && func->closure() != NULL; | |
8732 | const bool is_method = bound_method != NULL || interface_method != NULL; | |
8733 | gcc_assert(!fntype->is_method() || is_method); | |
8734 | ||
8735 | int nargs; | |
8736 | tree* args; | |
8737 | if (this->args_ == NULL || this->args_->empty()) | |
8738 | { | |
8739 | nargs = is_method ? 1 : 0; | |
8740 | args = nargs == 0 ? NULL : new tree[nargs]; | |
8741 | } | |
8742 | else | |
8743 | { | |
8744 | const Typed_identifier_list* params = fntype->parameters(); | |
8745 | gcc_assert(params != NULL); | |
8746 | ||
8747 | nargs = this->args_->size(); | |
8748 | int i = is_method ? 1 : 0; | |
8749 | nargs += i; | |
8750 | args = new tree[nargs]; | |
8751 | ||
8752 | Typed_identifier_list::const_iterator pp = params->begin(); | |
8753 | Expression_list::const_iterator pe; | |
8754 | for (pe = this->args_->begin(); | |
8755 | pe != this->args_->end(); | |
8756 | ++pe, ++pp, ++i) | |
8757 | { | |
a805a149 | 8758 | gcc_assert(pp != params->end()); |
e440a328 | 8759 | tree arg_val = (*pe)->get_tree(context); |
8760 | args[i] = Expression::convert_for_assignment(context, | |
8761 | pp->type(), | |
8762 | (*pe)->type(), | |
8763 | arg_val, | |
8764 | location); | |
8765 | if (args[i] == error_mark_node) | |
cf609de4 | 8766 | { |
8767 | delete[] args; | |
8768 | return error_mark_node; | |
8769 | } | |
e440a328 | 8770 | } |
8771 | gcc_assert(pp == params->end()); | |
8772 | gcc_assert(i == nargs); | |
8773 | } | |
8774 | ||
8775 | tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo))); | |
8776 | if (rettype == error_mark_node) | |
cf609de4 | 8777 | { |
8778 | delete[] args; | |
8779 | return error_mark_node; | |
8780 | } | |
e440a328 | 8781 | |
8782 | tree fn; | |
8783 | if (has_closure) | |
8784 | fn = func->get_tree_without_closure(gogo); | |
8785 | else if (!is_method) | |
8786 | fn = this->fn_->get_tree(context); | |
8787 | else if (bound_method != NULL) | |
8788 | fn = this->bound_method_function(context, bound_method, &args[0]); | |
8789 | else if (interface_method != NULL) | |
8790 | fn = this->interface_method_function(context, interface_method, &args[0]); | |
8791 | else | |
8792 | gcc_unreachable(); | |
8793 | ||
8794 | if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node) | |
cf609de4 | 8795 | { |
8796 | delete[] args; | |
8797 | return error_mark_node; | |
8798 | } | |
e440a328 | 8799 | |
e440a328 | 8800 | tree fndecl = fn; |
8801 | if (TREE_CODE(fndecl) == ADDR_EXPR) | |
8802 | fndecl = TREE_OPERAND(fndecl, 0); | |
9aa9e2df | 8803 | |
8804 | // Add a type cast in case the type of the function is a recursive | |
8805 | // type which refers to itself. | |
8806 | if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl)) | |
8807 | { | |
8808 | tree fnt = fntype->get_tree(gogo); | |
8809 | if (fnt == error_mark_node) | |
8810 | return error_mark_node; | |
8811 | fn = fold_convert_loc(location, fnt, fn); | |
8812 | } | |
8813 | ||
8814 | // This is to support builtin math functions when using 80387 math. | |
e440a328 | 8815 | tree excess_type = NULL_TREE; |
8816 | if (DECL_P(fndecl) | |
8817 | && DECL_IS_BUILTIN(fndecl) | |
8818 | && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL | |
8819 | && nargs > 0 | |
8820 | && ((SCALAR_FLOAT_TYPE_P(rettype) | |
8821 | && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0]))) | |
8822 | || (COMPLEX_FLOAT_TYPE_P(rettype) | |
8823 | && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0]))))) | |
8824 | { | |
8825 | excess_type = excess_precision_type(TREE_TYPE(args[0])); | |
8826 | if (excess_type != NULL_TREE) | |
8827 | { | |
8828 | tree excess_fndecl = mathfn_built_in(excess_type, | |
8829 | DECL_FUNCTION_CODE(fndecl)); | |
8830 | if (excess_fndecl == NULL_TREE) | |
8831 | excess_type = NULL_TREE; | |
8832 | else | |
8833 | { | |
8834 | fn = build_fold_addr_expr_loc(location, excess_fndecl); | |
8835 | for (int i = 0; i < nargs; ++i) | |
8836 | args[i] = ::convert(excess_type, args[i]); | |
8837 | } | |
8838 | } | |
8839 | } | |
8840 | ||
8841 | tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype, | |
8842 | fn, nargs, args); | |
8843 | delete[] args; | |
8844 | ||
8845 | SET_EXPR_LOCATION(ret, location); | |
8846 | ||
8847 | if (has_closure) | |
8848 | { | |
8849 | tree closure_tree = func->closure()->get_tree(context); | |
8850 | if (closure_tree != error_mark_node) | |
8851 | CALL_EXPR_STATIC_CHAIN(ret) = closure_tree; | |
8852 | } | |
8853 | ||
8854 | // If this is a recursive function type which returns itself, as in | |
8855 | // type F func() F | |
8856 | // we have used ptr_type_node for the return type. Add a cast here | |
8857 | // to the correct type. | |
8858 | if (TREE_TYPE(ret) == ptr_type_node) | |
8859 | { | |
9aa9e2df | 8860 | tree t = this->type()->base()->get_tree(gogo); |
e440a328 | 8861 | ret = fold_convert_loc(location, t, ret); |
8862 | } | |
8863 | ||
8864 | if (excess_type != NULL_TREE) | |
8865 | { | |
8866 | // Calling convert here can undo our excess precision change. | |
8867 | // That may or may not be a bug in convert_to_real. | |
8868 | ret = build1(NOP_EXPR, rettype, ret); | |
8869 | } | |
8870 | ||
8871 | // If there is more than one result, we will refer to the call | |
8872 | // multiple times. | |
8873 | if (fntype->results() != NULL && fntype->results()->size() > 1) | |
8874 | ret = save_expr(ret); | |
8875 | ||
8876 | this->tree_ = ret; | |
8877 | ||
8878 | return ret; | |
8879 | } | |
8880 | ||
8881 | // Make a call expression. | |
8882 | ||
8883 | Call_expression* | |
8884 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
8885 | source_location location) | |
8886 | { | |
8887 | return new Call_expression(fn, args, is_varargs, location); | |
8888 | } | |
8889 | ||
8890 | // A single result from a call which returns multiple results. | |
8891 | ||
8892 | class Call_result_expression : public Expression | |
8893 | { | |
8894 | public: | |
8895 | Call_result_expression(Call_expression* call, unsigned int index) | |
8896 | : Expression(EXPRESSION_CALL_RESULT, call->location()), | |
8897 | call_(call), index_(index) | |
8898 | { } | |
8899 | ||
8900 | protected: | |
8901 | int | |
8902 | do_traverse(Traverse*); | |
8903 | ||
8904 | Type* | |
8905 | do_type(); | |
8906 | ||
8907 | void | |
8908 | do_determine_type(const Type_context*); | |
8909 | ||
8910 | void | |
8911 | do_check_types(Gogo*); | |
8912 | ||
8913 | Expression* | |
8914 | do_copy() | |
8915 | { | |
8916 | return new Call_result_expression(this->call_->call_expression(), | |
8917 | this->index_); | |
8918 | } | |
8919 | ||
8920 | bool | |
8921 | do_must_eval_in_order() const | |
8922 | { return true; } | |
8923 | ||
8924 | tree | |
8925 | do_get_tree(Translate_context*); | |
8926 | ||
8927 | private: | |
8928 | // The underlying call expression. | |
8929 | Expression* call_; | |
8930 | // Which result we want. | |
8931 | unsigned int index_; | |
8932 | }; | |
8933 | ||
8934 | // Traverse a call result. | |
8935 | ||
8936 | int | |
8937 | Call_result_expression::do_traverse(Traverse* traverse) | |
8938 | { | |
8939 | if (traverse->remember_expression(this->call_)) | |
8940 | { | |
8941 | // We have already traversed the call expression. | |
8942 | return TRAVERSE_CONTINUE; | |
8943 | } | |
8944 | return Expression::traverse(&this->call_, traverse); | |
8945 | } | |
8946 | ||
8947 | // Get the type. | |
8948 | ||
8949 | Type* | |
8950 | Call_result_expression::do_type() | |
8951 | { | |
425dd051 | 8952 | if (this->classification() == EXPRESSION_ERROR) |
8953 | return Type::make_error_type(); | |
8954 | ||
e440a328 | 8955 | // THIS->CALL_ can be replaced with a temporary reference due to |
8956 | // Call_expression::do_must_eval_in_order when there is an error. | |
8957 | Call_expression* ce = this->call_->call_expression(); | |
8958 | if (ce == NULL) | |
5e85f268 | 8959 | { |
8960 | this->set_is_error(); | |
8961 | return Type::make_error_type(); | |
8962 | } | |
e440a328 | 8963 | Function_type* fntype = ce->get_function_type(); |
8964 | if (fntype == NULL) | |
5e85f268 | 8965 | { |
8966 | this->set_is_error(); | |
8967 | return Type::make_error_type(); | |
8968 | } | |
e440a328 | 8969 | const Typed_identifier_list* results = fntype->results(); |
7b8d861f | 8970 | if (results == NULL) |
8971 | { | |
8972 | this->report_error(_("number of results does not match " | |
8973 | "number of values")); | |
8974 | return Type::make_error_type(); | |
8975 | } | |
e440a328 | 8976 | Typed_identifier_list::const_iterator pr = results->begin(); |
8977 | for (unsigned int i = 0; i < this->index_; ++i) | |
8978 | { | |
8979 | if (pr == results->end()) | |
425dd051 | 8980 | break; |
e440a328 | 8981 | ++pr; |
8982 | } | |
8983 | if (pr == results->end()) | |
425dd051 | 8984 | { |
8985 | this->report_error(_("number of results does not match " | |
8986 | "number of values")); | |
8987 | return Type::make_error_type(); | |
8988 | } | |
e440a328 | 8989 | return pr->type(); |
8990 | } | |
8991 | ||
425dd051 | 8992 | // Check the type. Just make sure that we trigger the warning in |
8993 | // do_type. | |
e440a328 | 8994 | |
8995 | void | |
8996 | Call_result_expression::do_check_types(Gogo*) | |
8997 | { | |
425dd051 | 8998 | this->type(); |
e440a328 | 8999 | } |
9000 | ||
9001 | // Determine the type. We have nothing to do here, but the 0 result | |
9002 | // needs to pass down to the caller. | |
9003 | ||
9004 | void | |
9005 | Call_result_expression::do_determine_type(const Type_context*) | |
9006 | { | |
9007 | if (this->index_ == 0) | |
9008 | this->call_->determine_type_no_context(); | |
9009 | } | |
9010 | ||
9011 | // Return the tree. | |
9012 | ||
9013 | tree | |
9014 | Call_result_expression::do_get_tree(Translate_context* context) | |
9015 | { | |
9016 | tree call_tree = this->call_->get_tree(context); | |
9017 | if (call_tree == error_mark_node) | |
9018 | return error_mark_node; | |
5e85f268 | 9019 | if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE) |
9020 | { | |
9021 | gcc_assert(saw_errors()); | |
9022 | return error_mark_node; | |
9023 | } | |
e440a328 | 9024 | tree field = TYPE_FIELDS(TREE_TYPE(call_tree)); |
9025 | for (unsigned int i = 0; i < this->index_; ++i) | |
9026 | { | |
9027 | gcc_assert(field != NULL_TREE); | |
9028 | field = DECL_CHAIN(field); | |
9029 | } | |
9030 | gcc_assert(field != NULL_TREE); | |
9031 | return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE); | |
9032 | } | |
9033 | ||
9034 | // Make a reference to a single result of a call which returns | |
9035 | // multiple results. | |
9036 | ||
9037 | Expression* | |
9038 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
9039 | { | |
9040 | return new Call_result_expression(call, index); | |
9041 | } | |
9042 | ||
9043 | // Class Index_expression. | |
9044 | ||
9045 | // Traversal. | |
9046 | ||
9047 | int | |
9048 | Index_expression::do_traverse(Traverse* traverse) | |
9049 | { | |
9050 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
9051 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
9052 | || (this->end_ != NULL | |
9053 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)) | |
9054 | return TRAVERSE_EXIT; | |
9055 | return TRAVERSE_CONTINUE; | |
9056 | } | |
9057 | ||
9058 | // Lower an index expression. This converts the generic index | |
9059 | // expression into an array index, a string index, or a map index. | |
9060 | ||
9061 | Expression* | |
9062 | Index_expression::do_lower(Gogo*, Named_object*, int) | |
9063 | { | |
9064 | source_location location = this->location(); | |
9065 | Expression* left = this->left_; | |
9066 | Expression* start = this->start_; | |
9067 | Expression* end = this->end_; | |
9068 | ||
9069 | Type* type = left->type(); | |
9070 | if (type->is_error_type()) | |
9071 | return Expression::make_error(location); | |
b0cf7ddd | 9072 | else if (left->is_type_expression()) |
9073 | { | |
9074 | error_at(location, "attempt to index type expression"); | |
9075 | return Expression::make_error(location); | |
9076 | } | |
e440a328 | 9077 | else if (type->array_type() != NULL) |
9078 | return Expression::make_array_index(left, start, end, location); | |
9079 | else if (type->points_to() != NULL | |
9080 | && type->points_to()->array_type() != NULL | |
9081 | && !type->points_to()->is_open_array_type()) | |
9082 | { | |
9083 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
9084 | location); | |
9085 | return Expression::make_array_index(deref, start, end, location); | |
9086 | } | |
9087 | else if (type->is_string_type()) | |
9088 | return Expression::make_string_index(left, start, end, location); | |
9089 | else if (type->map_type() != NULL) | |
9090 | { | |
9091 | if (end != NULL) | |
9092 | { | |
9093 | error_at(location, "invalid slice of map"); | |
9094 | return Expression::make_error(location); | |
9095 | } | |
9096 | Map_index_expression* ret= Expression::make_map_index(left, start, | |
9097 | location); | |
9098 | if (this->is_lvalue_) | |
9099 | ret->set_is_lvalue(); | |
9100 | return ret; | |
9101 | } | |
9102 | else | |
9103 | { | |
9104 | error_at(location, | |
9105 | "attempt to index object which is not array, string, or map"); | |
9106 | return Expression::make_error(location); | |
9107 | } | |
9108 | } | |
9109 | ||
9110 | // Make an index expression. | |
9111 | ||
9112 | Expression* | |
9113 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
9114 | source_location location) | |
9115 | { | |
9116 | return new Index_expression(left, start, end, location); | |
9117 | } | |
9118 | ||
9119 | // An array index. This is used for both indexing and slicing. | |
9120 | ||
9121 | class Array_index_expression : public Expression | |
9122 | { | |
9123 | public: | |
9124 | Array_index_expression(Expression* array, Expression* start, | |
9125 | Expression* end, source_location location) | |
9126 | : Expression(EXPRESSION_ARRAY_INDEX, location), | |
9127 | array_(array), start_(start), end_(end), type_(NULL) | |
9128 | { } | |
9129 | ||
9130 | protected: | |
9131 | int | |
9132 | do_traverse(Traverse*); | |
9133 | ||
9134 | Type* | |
9135 | do_type(); | |
9136 | ||
9137 | void | |
9138 | do_determine_type(const Type_context*); | |
9139 | ||
9140 | void | |
9141 | do_check_types(Gogo*); | |
9142 | ||
9143 | Expression* | |
9144 | do_copy() | |
9145 | { | |
9146 | return Expression::make_array_index(this->array_->copy(), | |
9147 | this->start_->copy(), | |
9148 | (this->end_ == NULL | |
9149 | ? NULL | |
9150 | : this->end_->copy()), | |
9151 | this->location()); | |
9152 | } | |
9153 | ||
9154 | bool | |
9155 | do_is_addressable() const; | |
9156 | ||
9157 | void | |
9158 | do_address_taken(bool escapes) | |
9159 | { this->array_->address_taken(escapes); } | |
9160 | ||
9161 | tree | |
9162 | do_get_tree(Translate_context*); | |
9163 | ||
9164 | private: | |
9165 | // The array we are getting a value from. | |
9166 | Expression* array_; | |
9167 | // The start or only index. | |
9168 | Expression* start_; | |
9169 | // The end index of a slice. This may be NULL for a simple array | |
9170 | // index, or it may be a nil expression for the length of the array. | |
9171 | Expression* end_; | |
9172 | // The type of the expression. | |
9173 | Type* type_; | |
9174 | }; | |
9175 | ||
9176 | // Array index traversal. | |
9177 | ||
9178 | int | |
9179 | Array_index_expression::do_traverse(Traverse* traverse) | |
9180 | { | |
9181 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
9182 | return TRAVERSE_EXIT; | |
9183 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
9184 | return TRAVERSE_EXIT; | |
9185 | if (this->end_ != NULL) | |
9186 | { | |
9187 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
9188 | return TRAVERSE_EXIT; | |
9189 | } | |
9190 | return TRAVERSE_CONTINUE; | |
9191 | } | |
9192 | ||
9193 | // Return the type of an array index. | |
9194 | ||
9195 | Type* | |
9196 | Array_index_expression::do_type() | |
9197 | { | |
9198 | if (this->type_ == NULL) | |
9199 | { | |
9200 | Array_type* type = this->array_->type()->array_type(); | |
9201 | if (type == NULL) | |
9202 | this->type_ = Type::make_error_type(); | |
9203 | else if (this->end_ == NULL) | |
9204 | this->type_ = type->element_type(); | |
9205 | else if (type->is_open_array_type()) | |
9206 | { | |
9207 | // A slice of a slice has the same type as the original | |
9208 | // slice. | |
9209 | this->type_ = this->array_->type()->deref(); | |
9210 | } | |
9211 | else | |
9212 | { | |
9213 | // A slice of an array is a slice. | |
9214 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
9215 | } | |
9216 | } | |
9217 | return this->type_; | |
9218 | } | |
9219 | ||
9220 | // Set the type of an array index. | |
9221 | ||
9222 | void | |
9223 | Array_index_expression::do_determine_type(const Type_context*) | |
9224 | { | |
9225 | this->array_->determine_type_no_context(); | |
7917ad68 | 9226 | this->start_->determine_type_no_context(); |
e440a328 | 9227 | if (this->end_ != NULL) |
7917ad68 | 9228 | this->end_->determine_type_no_context(); |
e440a328 | 9229 | } |
9230 | ||
9231 | // Check types of an array index. | |
9232 | ||
9233 | void | |
9234 | Array_index_expression::do_check_types(Gogo*) | |
9235 | { | |
9236 | if (this->start_->type()->integer_type() == NULL) | |
9237 | this->report_error(_("index must be integer")); | |
9238 | if (this->end_ != NULL | |
9239 | && this->end_->type()->integer_type() == NULL | |
9240 | && !this->end_->is_nil_expression()) | |
9241 | this->report_error(_("slice end must be integer")); | |
9242 | ||
9243 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 9244 | if (array_type == NULL) |
9245 | { | |
9246 | gcc_assert(this->array_->type()->is_error_type()); | |
9247 | return; | |
9248 | } | |
e440a328 | 9249 | |
9250 | unsigned int int_bits = | |
9251 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
9252 | ||
9253 | Type* dummy; | |
9254 | mpz_t lval; | |
9255 | mpz_init(lval); | |
9256 | bool lval_valid = (array_type->length() != NULL | |
9257 | && array_type->length()->integer_constant_value(true, | |
9258 | lval, | |
9259 | &dummy)); | |
9260 | mpz_t ival; | |
9261 | mpz_init(ival); | |
9262 | if (this->start_->integer_constant_value(true, ival, &dummy)) | |
9263 | { | |
9264 | if (mpz_sgn(ival) < 0 | |
9265 | || mpz_sizeinbase(ival, 2) >= int_bits | |
9266 | || (lval_valid | |
9267 | && (this->end_ == NULL | |
9268 | ? mpz_cmp(ival, lval) >= 0 | |
9269 | : mpz_cmp(ival, lval) > 0))) | |
9270 | { | |
9271 | error_at(this->start_->location(), "array index out of bounds"); | |
9272 | this->set_is_error(); | |
9273 | } | |
9274 | } | |
9275 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
9276 | { | |
9277 | if (this->end_->integer_constant_value(true, ival, &dummy)) | |
9278 | { | |
9279 | if (mpz_sgn(ival) < 0 | |
9280 | || mpz_sizeinbase(ival, 2) >= int_bits | |
9281 | || (lval_valid && mpz_cmp(ival, lval) > 0)) | |
9282 | { | |
9283 | error_at(this->end_->location(), "array index out of bounds"); | |
9284 | this->set_is_error(); | |
9285 | } | |
9286 | } | |
9287 | } | |
9288 | mpz_clear(ival); | |
9289 | mpz_clear(lval); | |
9290 | ||
9291 | // A slice of an array requires an addressable array. A slice of a | |
9292 | // slice is always possible. | |
9293 | if (this->end_ != NULL | |
9294 | && !array_type->is_open_array_type() | |
9295 | && !this->array_->is_addressable()) | |
9296 | this->report_error(_("array is not addressable")); | |
9297 | } | |
9298 | ||
9299 | // Return whether this expression is addressable. | |
9300 | ||
9301 | bool | |
9302 | Array_index_expression::do_is_addressable() const | |
9303 | { | |
9304 | // A slice expression is not addressable. | |
9305 | if (this->end_ != NULL) | |
9306 | return false; | |
9307 | ||
9308 | // An index into a slice is addressable. | |
9309 | if (this->array_->type()->is_open_array_type()) | |
9310 | return true; | |
9311 | ||
9312 | // An index into an array is addressable if the array is | |
9313 | // addressable. | |
9314 | return this->array_->is_addressable(); | |
9315 | } | |
9316 | ||
9317 | // Get a tree for an array index. | |
9318 | ||
9319 | tree | |
9320 | Array_index_expression::do_get_tree(Translate_context* context) | |
9321 | { | |
9322 | Gogo* gogo = context->gogo(); | |
9323 | source_location loc = this->location(); | |
9324 | ||
9325 | Array_type* array_type = this->array_->type()->array_type(); | |
d8cd8e2d | 9326 | if (array_type == NULL) |
9327 | { | |
9328 | gcc_assert(this->array_->type()->is_error_type()); | |
9329 | return error_mark_node; | |
9330 | } | |
e440a328 | 9331 | |
9332 | tree type_tree = array_type->get_tree(gogo); | |
c65212a0 | 9333 | if (type_tree == error_mark_node) |
9334 | return error_mark_node; | |
e440a328 | 9335 | |
9336 | tree array_tree = this->array_->get_tree(context); | |
9337 | if (array_tree == error_mark_node) | |
9338 | return error_mark_node; | |
9339 | ||
9340 | if (array_type->length() == NULL && !DECL_P(array_tree)) | |
9341 | array_tree = save_expr(array_tree); | |
9342 | tree length_tree = array_type->length_tree(gogo, array_tree); | |
c65212a0 | 9343 | if (length_tree == error_mark_node) |
9344 | return error_mark_node; | |
e440a328 | 9345 | length_tree = save_expr(length_tree); |
9346 | tree length_type = TREE_TYPE(length_tree); | |
9347 | ||
9348 | tree bad_index = boolean_false_node; | |
9349 | ||
9350 | tree start_tree = this->start_->get_tree(context); | |
9351 | if (start_tree == error_mark_node) | |
9352 | return error_mark_node; | |
9353 | if (!DECL_P(start_tree)) | |
9354 | start_tree = save_expr(start_tree); | |
9355 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
9356 | start_tree = convert_to_integer(length_type, start_tree); | |
9357 | ||
9358 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
9359 | loc); | |
9360 | ||
9361 | start_tree = fold_convert_loc(loc, length_type, start_tree); | |
9362 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index, | |
9363 | fold_build2_loc(loc, | |
9364 | (this->end_ == NULL | |
9365 | ? GE_EXPR | |
9366 | : GT_EXPR), | |
9367 | boolean_type_node, start_tree, | |
9368 | length_tree)); | |
9369 | ||
9370 | int code = (array_type->length() != NULL | |
9371 | ? (this->end_ == NULL | |
9372 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
9373 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
9374 | : (this->end_ == NULL | |
9375 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
9376 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
9377 | tree crash = Gogo::runtime_error(code, loc); | |
9378 | ||
9379 | if (this->end_ == NULL) | |
9380 | { | |
9381 | // Simple array indexing. This has to return an l-value, so | |
9382 | // wrap the index check into START_TREE. | |
9383 | start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree), | |
9384 | build3(COND_EXPR, void_type_node, | |
9385 | bad_index, crash, NULL_TREE), | |
9386 | start_tree); | |
9387 | start_tree = fold_convert_loc(loc, sizetype, start_tree); | |
9388 | ||
9389 | if (array_type->length() != NULL) | |
9390 | { | |
9391 | // Fixed array. | |
9392 | return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree, | |
9393 | start_tree, NULL_TREE, NULL_TREE); | |
9394 | } | |
9395 | else | |
9396 | { | |
9397 | // Open array. | |
9398 | tree values = array_type->value_pointer_tree(gogo, array_tree); | |
9399 | tree element_type_tree = array_type->element_type()->get_tree(gogo); | |
c65212a0 | 9400 | if (element_type_tree == error_mark_node) |
9401 | return error_mark_node; | |
e440a328 | 9402 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
9403 | tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype, | |
9404 | start_tree, element_size); | |
9405 | tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, | |
9406 | TREE_TYPE(values), values, offset); | |
9407 | return build_fold_indirect_ref(ptr); | |
9408 | } | |
9409 | } | |
9410 | ||
9411 | // Array slice. | |
9412 | ||
9413 | tree capacity_tree = array_type->capacity_tree(gogo, array_tree); | |
c65212a0 | 9414 | if (capacity_tree == error_mark_node) |
9415 | return error_mark_node; | |
e440a328 | 9416 | capacity_tree = fold_convert_loc(loc, length_type, capacity_tree); |
9417 | ||
9418 | tree end_tree; | |
9419 | if (this->end_->is_nil_expression()) | |
9420 | end_tree = length_tree; | |
9421 | else | |
9422 | { | |
9423 | end_tree = this->end_->get_tree(context); | |
9424 | if (end_tree == error_mark_node) | |
9425 | return error_mark_node; | |
9426 | if (!DECL_P(end_tree)) | |
9427 | end_tree = save_expr(end_tree); | |
9428 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
9429 | end_tree = convert_to_integer(length_type, end_tree); | |
9430 | ||
9431 | bad_index = Expression::check_bounds(end_tree, length_type, bad_index, | |
9432 | loc); | |
9433 | ||
9434 | end_tree = fold_convert_loc(loc, length_type, end_tree); | |
9435 | ||
9436 | capacity_tree = save_expr(capacity_tree); | |
9437 | tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9438 | fold_build2_loc(loc, LT_EXPR, | |
9439 | boolean_type_node, | |
9440 | end_tree, start_tree), | |
9441 | fold_build2_loc(loc, GT_EXPR, | |
9442 | boolean_type_node, | |
9443 | end_tree, capacity_tree)); | |
9444 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9445 | bad_index, bad_end); | |
9446 | } | |
9447 | ||
9448 | tree element_type_tree = array_type->element_type()->get_tree(gogo); | |
c65212a0 | 9449 | if (element_type_tree == error_mark_node) |
9450 | return error_mark_node; | |
e440a328 | 9451 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
9452 | ||
9453 | tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype, | |
9454 | fold_convert_loc(loc, sizetype, start_tree), | |
9455 | element_size); | |
9456 | ||
9457 | tree value_pointer = array_type->value_pointer_tree(gogo, array_tree); | |
c65212a0 | 9458 | if (value_pointer == error_mark_node) |
9459 | return error_mark_node; | |
e440a328 | 9460 | |
9461 | value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR, | |
9462 | TREE_TYPE(value_pointer), | |
9463 | value_pointer, offset); | |
9464 | ||
9465 | tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type, | |
9466 | end_tree, start_tree); | |
9467 | ||
9468 | tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type, | |
9469 | capacity_tree, start_tree); | |
9470 | ||
9471 | tree struct_tree = this->type()->get_tree(gogo); | |
9472 | gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE); | |
9473 | ||
9474 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
9475 | ||
9476 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
9477 | tree field = TYPE_FIELDS(struct_tree); | |
9478 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); | |
9479 | elt->index = field; | |
9480 | elt->value = value_pointer; | |
9481 | ||
9482 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
9483 | field = DECL_CHAIN(field); | |
9484 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); | |
9485 | elt->index = field; | |
9486 | elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree); | |
9487 | ||
9488 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
9489 | field = DECL_CHAIN(field); | |
9490 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0); | |
9491 | elt->index = field; | |
9492 | elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree); | |
9493 | ||
9494 | tree constructor = build_constructor(struct_tree, init); | |
9495 | ||
9496 | if (TREE_CONSTANT(value_pointer) | |
9497 | && TREE_CONSTANT(result_length_tree) | |
9498 | && TREE_CONSTANT(result_capacity_tree)) | |
9499 | TREE_CONSTANT(constructor) = 1; | |
9500 | ||
9501 | return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor), | |
9502 | build3(COND_EXPR, void_type_node, | |
9503 | bad_index, crash, NULL_TREE), | |
9504 | constructor); | |
9505 | } | |
9506 | ||
9507 | // Make an array index expression. END may be NULL. | |
9508 | ||
9509 | Expression* | |
9510 | Expression::make_array_index(Expression* array, Expression* start, | |
9511 | Expression* end, source_location location) | |
9512 | { | |
9513 | // Taking a slice of a composite literal requires moving the literal | |
9514 | // onto the heap. | |
9515 | if (end != NULL && array->is_composite_literal()) | |
9516 | { | |
9517 | array = Expression::make_heap_composite(array, location); | |
9518 | array = Expression::make_unary(OPERATOR_MULT, array, location); | |
9519 | } | |
9520 | return new Array_index_expression(array, start, end, location); | |
9521 | } | |
9522 | ||
9523 | // A string index. This is used for both indexing and slicing. | |
9524 | ||
9525 | class String_index_expression : public Expression | |
9526 | { | |
9527 | public: | |
9528 | String_index_expression(Expression* string, Expression* start, | |
9529 | Expression* end, source_location location) | |
9530 | : Expression(EXPRESSION_STRING_INDEX, location), | |
9531 | string_(string), start_(start), end_(end) | |
9532 | { } | |
9533 | ||
9534 | protected: | |
9535 | int | |
9536 | do_traverse(Traverse*); | |
9537 | ||
9538 | Type* | |
9539 | do_type(); | |
9540 | ||
9541 | void | |
9542 | do_determine_type(const Type_context*); | |
9543 | ||
9544 | void | |
9545 | do_check_types(Gogo*); | |
9546 | ||
9547 | Expression* | |
9548 | do_copy() | |
9549 | { | |
9550 | return Expression::make_string_index(this->string_->copy(), | |
9551 | this->start_->copy(), | |
9552 | (this->end_ == NULL | |
9553 | ? NULL | |
9554 | : this->end_->copy()), | |
9555 | this->location()); | |
9556 | } | |
9557 | ||
9558 | tree | |
9559 | do_get_tree(Translate_context*); | |
9560 | ||
9561 | private: | |
9562 | // The string we are getting a value from. | |
9563 | Expression* string_; | |
9564 | // The start or only index. | |
9565 | Expression* start_; | |
9566 | // The end index of a slice. This may be NULL for a single index, | |
9567 | // or it may be a nil expression for the length of the string. | |
9568 | Expression* end_; | |
9569 | }; | |
9570 | ||
9571 | // String index traversal. | |
9572 | ||
9573 | int | |
9574 | String_index_expression::do_traverse(Traverse* traverse) | |
9575 | { | |
9576 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
9577 | return TRAVERSE_EXIT; | |
9578 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
9579 | return TRAVERSE_EXIT; | |
9580 | if (this->end_ != NULL) | |
9581 | { | |
9582 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
9583 | return TRAVERSE_EXIT; | |
9584 | } | |
9585 | return TRAVERSE_CONTINUE; | |
9586 | } | |
9587 | ||
9588 | // Return the type of a string index. | |
9589 | ||
9590 | Type* | |
9591 | String_index_expression::do_type() | |
9592 | { | |
9593 | if (this->end_ == NULL) | |
9594 | return Type::lookup_integer_type("uint8"); | |
9595 | else | |
7672d35f | 9596 | return this->string_->type(); |
e440a328 | 9597 | } |
9598 | ||
9599 | // Determine the type of a string index. | |
9600 | ||
9601 | void | |
9602 | String_index_expression::do_determine_type(const Type_context*) | |
9603 | { | |
9604 | this->string_->determine_type_no_context(); | |
93000773 | 9605 | this->start_->determine_type_no_context(); |
e440a328 | 9606 | if (this->end_ != NULL) |
93000773 | 9607 | this->end_->determine_type_no_context(); |
e440a328 | 9608 | } |
9609 | ||
9610 | // Check types of a string index. | |
9611 | ||
9612 | void | |
9613 | String_index_expression::do_check_types(Gogo*) | |
9614 | { | |
9615 | if (this->start_->type()->integer_type() == NULL) | |
9616 | this->report_error(_("index must be integer")); | |
9617 | if (this->end_ != NULL | |
9618 | && this->end_->type()->integer_type() == NULL | |
9619 | && !this->end_->is_nil_expression()) | |
9620 | this->report_error(_("slice end must be integer")); | |
9621 | ||
9622 | std::string sval; | |
9623 | bool sval_valid = this->string_->string_constant_value(&sval); | |
9624 | ||
9625 | mpz_t ival; | |
9626 | mpz_init(ival); | |
9627 | Type* dummy; | |
9628 | if (this->start_->integer_constant_value(true, ival, &dummy)) | |
9629 | { | |
9630 | if (mpz_sgn(ival) < 0 | |
9631 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0)) | |
9632 | { | |
9633 | error_at(this->start_->location(), "string index out of bounds"); | |
9634 | this->set_is_error(); | |
9635 | } | |
9636 | } | |
9637 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
9638 | { | |
9639 | if (this->end_->integer_constant_value(true, ival, &dummy)) | |
9640 | { | |
9641 | if (mpz_sgn(ival) < 0 | |
9642 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0)) | |
9643 | { | |
9644 | error_at(this->end_->location(), "string index out of bounds"); | |
9645 | this->set_is_error(); | |
9646 | } | |
9647 | } | |
9648 | } | |
9649 | mpz_clear(ival); | |
9650 | } | |
9651 | ||
9652 | // Get a tree for a string index. | |
9653 | ||
9654 | tree | |
9655 | String_index_expression::do_get_tree(Translate_context* context) | |
9656 | { | |
9657 | source_location loc = this->location(); | |
9658 | ||
9659 | tree string_tree = this->string_->get_tree(context); | |
9660 | if (string_tree == error_mark_node) | |
9661 | return error_mark_node; | |
9662 | ||
9663 | if (this->string_->type()->points_to() != NULL) | |
9664 | string_tree = build_fold_indirect_ref(string_tree); | |
9665 | if (!DECL_P(string_tree)) | |
9666 | string_tree = save_expr(string_tree); | |
9667 | tree string_type = TREE_TYPE(string_tree); | |
9668 | ||
9669 | tree length_tree = String_type::length_tree(context->gogo(), string_tree); | |
9670 | length_tree = save_expr(length_tree); | |
9671 | tree length_type = TREE_TYPE(length_tree); | |
9672 | ||
9673 | tree bad_index = boolean_false_node; | |
9674 | ||
9675 | tree start_tree = this->start_->get_tree(context); | |
9676 | if (start_tree == error_mark_node) | |
9677 | return error_mark_node; | |
9678 | if (!DECL_P(start_tree)) | |
9679 | start_tree = save_expr(start_tree); | |
9680 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
9681 | start_tree = convert_to_integer(length_type, start_tree); | |
9682 | ||
9683 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
9684 | loc); | |
9685 | ||
9686 | start_tree = fold_convert_loc(loc, length_type, start_tree); | |
9687 | ||
9688 | int code = (this->end_ == NULL | |
9689 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
9690 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
9691 | tree crash = Gogo::runtime_error(code, loc); | |
9692 | ||
9693 | if (this->end_ == NULL) | |
9694 | { | |
9695 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9696 | bad_index, | |
9697 | fold_build2_loc(loc, GE_EXPR, | |
9698 | boolean_type_node, | |
9699 | start_tree, length_tree)); | |
9700 | ||
9701 | tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree); | |
9702 | tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree), | |
9703 | bytes_tree, | |
9704 | fold_convert_loc(loc, sizetype, start_tree)); | |
9705 | tree index = build_fold_indirect_ref_loc(loc, ptr); | |
9706 | ||
9707 | return build2(COMPOUND_EXPR, TREE_TYPE(index), | |
9708 | build3(COND_EXPR, void_type_node, | |
9709 | bad_index, crash, NULL_TREE), | |
9710 | index); | |
9711 | } | |
9712 | else | |
9713 | { | |
9714 | tree end_tree; | |
9715 | if (this->end_->is_nil_expression()) | |
9716 | end_tree = build_int_cst(length_type, -1); | |
9717 | else | |
9718 | { | |
9719 | end_tree = this->end_->get_tree(context); | |
9720 | if (end_tree == error_mark_node) | |
9721 | return error_mark_node; | |
9722 | if (!DECL_P(end_tree)) | |
9723 | end_tree = save_expr(end_tree); | |
9724 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
9725 | end_tree = convert_to_integer(length_type, end_tree); | |
9726 | ||
9727 | bad_index = Expression::check_bounds(end_tree, length_type, | |
9728 | bad_index, loc); | |
9729 | ||
9730 | end_tree = fold_convert_loc(loc, length_type, end_tree); | |
9731 | } | |
9732 | ||
9733 | static tree strslice_fndecl; | |
9734 | tree ret = Gogo::call_builtin(&strslice_fndecl, | |
9735 | loc, | |
9736 | "__go_string_slice", | |
9737 | 3, | |
9738 | string_type, | |
9739 | string_type, | |
9740 | string_tree, | |
9741 | length_type, | |
9742 | start_tree, | |
9743 | length_type, | |
9744 | end_tree); | |
5fb82b5e | 9745 | if (ret == error_mark_node) |
9746 | return error_mark_node; | |
e440a328 | 9747 | // This will panic if the bounds are out of range for the |
9748 | // string. | |
9749 | TREE_NOTHROW(strslice_fndecl) = 0; | |
9750 | ||
9751 | if (bad_index == boolean_false_node) | |
9752 | return ret; | |
9753 | else | |
9754 | return build2(COMPOUND_EXPR, TREE_TYPE(ret), | |
9755 | build3(COND_EXPR, void_type_node, | |
9756 | bad_index, crash, NULL_TREE), | |
9757 | ret); | |
9758 | } | |
9759 | } | |
9760 | ||
9761 | // Make a string index expression. END may be NULL. | |
9762 | ||
9763 | Expression* | |
9764 | Expression::make_string_index(Expression* string, Expression* start, | |
9765 | Expression* end, source_location location) | |
9766 | { | |
9767 | return new String_index_expression(string, start, end, location); | |
9768 | } | |
9769 | ||
9770 | // Class Map_index. | |
9771 | ||
9772 | // Get the type of the map. | |
9773 | ||
9774 | Map_type* | |
9775 | Map_index_expression::get_map_type() const | |
9776 | { | |
9777 | Map_type* mt = this->map_->type()->deref()->map_type(); | |
c7524fae | 9778 | if (mt == NULL) |
9779 | gcc_assert(saw_errors()); | |
e440a328 | 9780 | return mt; |
9781 | } | |
9782 | ||
9783 | // Map index traversal. | |
9784 | ||
9785 | int | |
9786 | Map_index_expression::do_traverse(Traverse* traverse) | |
9787 | { | |
9788 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
9789 | return TRAVERSE_EXIT; | |
9790 | return Expression::traverse(&this->index_, traverse); | |
9791 | } | |
9792 | ||
9793 | // Return the type of a map index. | |
9794 | ||
9795 | Type* | |
9796 | Map_index_expression::do_type() | |
9797 | { | |
c7524fae | 9798 | Map_type* mt = this->get_map_type(); |
9799 | if (mt == NULL) | |
9800 | return Type::make_error_type(); | |
9801 | Type* type = mt->val_type(); | |
e440a328 | 9802 | // If this map index is in a tuple assignment, we actually return a |
9803 | // pointer to the value type. Tuple_map_assignment_statement is | |
9804 | // responsible for handling this correctly. We need to get the type | |
9805 | // right in case this gets assigned to a temporary variable. | |
9806 | if (this->is_in_tuple_assignment_) | |
9807 | type = Type::make_pointer_type(type); | |
9808 | return type; | |
9809 | } | |
9810 | ||
9811 | // Fix the type of a map index. | |
9812 | ||
9813 | void | |
9814 | Map_index_expression::do_determine_type(const Type_context*) | |
9815 | { | |
9816 | this->map_->determine_type_no_context(); | |
c7524fae | 9817 | Map_type* mt = this->get_map_type(); |
9818 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
9819 | Type_context subcontext(key_type, false); | |
e440a328 | 9820 | this->index_->determine_type(&subcontext); |
9821 | } | |
9822 | ||
9823 | // Check types of a map index. | |
9824 | ||
9825 | void | |
9826 | Map_index_expression::do_check_types(Gogo*) | |
9827 | { | |
9828 | std::string reason; | |
c7524fae | 9829 | Map_type* mt = this->get_map_type(); |
9830 | if (mt == NULL) | |
9831 | return; | |
9832 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 9833 | { |
9834 | if (reason.empty()) | |
9835 | this->report_error(_("incompatible type for map index")); | |
9836 | else | |
9837 | { | |
9838 | error_at(this->location(), "incompatible type for map index (%s)", | |
9839 | reason.c_str()); | |
9840 | this->set_is_error(); | |
9841 | } | |
9842 | } | |
9843 | } | |
9844 | ||
9845 | // Get a tree for a map index. | |
9846 | ||
9847 | tree | |
9848 | Map_index_expression::do_get_tree(Translate_context* context) | |
9849 | { | |
9850 | Map_type* type = this->get_map_type(); | |
c7524fae | 9851 | if (type == NULL) |
9852 | return error_mark_node; | |
e440a328 | 9853 | |
9854 | tree valptr = this->get_value_pointer(context, this->is_lvalue_); | |
9855 | if (valptr == error_mark_node) | |
9856 | return error_mark_node; | |
9857 | valptr = save_expr(valptr); | |
9858 | ||
9859 | tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr)); | |
9860 | ||
9861 | if (this->is_lvalue_) | |
9862 | return build_fold_indirect_ref(valptr); | |
9863 | else if (this->is_in_tuple_assignment_) | |
9864 | { | |
9865 | // Tuple_map_assignment_statement is responsible for using this | |
9866 | // appropriately. | |
9867 | return valptr; | |
9868 | } | |
9869 | else | |
9870 | { | |
9871 | return fold_build3(COND_EXPR, val_type_tree, | |
9872 | fold_build2(EQ_EXPR, boolean_type_node, valptr, | |
9873 | fold_convert(TREE_TYPE(valptr), | |
9874 | null_pointer_node)), | |
9875 | type->val_type()->get_init_tree(context->gogo(), | |
9876 | false), | |
9877 | build_fold_indirect_ref(valptr)); | |
9878 | } | |
9879 | } | |
9880 | ||
9881 | // Get a tree for the map index. This returns a tree which evaluates | |
9882 | // to a pointer to a value. The pointer will be NULL if the key is | |
9883 | // not in the map. | |
9884 | ||
9885 | tree | |
9886 | Map_index_expression::get_value_pointer(Translate_context* context, | |
9887 | bool insert) | |
9888 | { | |
9889 | Map_type* type = this->get_map_type(); | |
c7524fae | 9890 | if (type == NULL) |
9891 | return error_mark_node; | |
e440a328 | 9892 | |
9893 | tree map_tree = this->map_->get_tree(context); | |
9894 | tree index_tree = this->index_->get_tree(context); | |
9895 | index_tree = Expression::convert_for_assignment(context, type->key_type(), | |
9896 | this->index_->type(), | |
9897 | index_tree, | |
9898 | this->location()); | |
9899 | if (map_tree == error_mark_node || index_tree == error_mark_node) | |
9900 | return error_mark_node; | |
9901 | ||
9902 | if (this->map_->type()->points_to() != NULL) | |
9903 | map_tree = build_fold_indirect_ref(map_tree); | |
9904 | ||
9905 | // We need to pass in a pointer to the key, so stuff it into a | |
9906 | // variable. | |
746d2e73 | 9907 | tree tmp; |
9908 | tree make_tmp; | |
9909 | if (current_function_decl != NULL) | |
9910 | { | |
9911 | tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree)); | |
9912 | DECL_IGNORED_P(tmp) = 0; | |
9913 | DECL_INITIAL(tmp) = index_tree; | |
9914 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
9915 | TREE_ADDRESSABLE(tmp) = 1; | |
9916 | } | |
9917 | else | |
9918 | { | |
9919 | tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"), | |
9920 | TREE_TYPE(index_tree)); | |
9921 | DECL_EXTERNAL(tmp) = 0; | |
9922 | TREE_PUBLIC(tmp) = 0; | |
9923 | TREE_STATIC(tmp) = 1; | |
9924 | DECL_ARTIFICIAL(tmp) = 1; | |
9925 | if (!TREE_CONSTANT(index_tree)) | |
9926 | make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node, | |
9927 | tmp, index_tree); | |
9928 | else | |
9929 | { | |
9930 | TREE_READONLY(tmp) = 1; | |
9931 | TREE_CONSTANT(tmp) = 1; | |
9932 | DECL_INITIAL(tmp) = index_tree; | |
9933 | make_tmp = NULL_TREE; | |
9934 | } | |
9935 | rest_of_decl_compilation(tmp, 1, 0); | |
9936 | } | |
9937 | tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node, | |
9938 | build_fold_addr_expr_loc(this->location(), | |
9939 | tmp)); | |
e440a328 | 9940 | |
9941 | static tree map_index_fndecl; | |
9942 | tree call = Gogo::call_builtin(&map_index_fndecl, | |
9943 | this->location(), | |
9944 | "__go_map_index", | |
9945 | 3, | |
9946 | const_ptr_type_node, | |
9947 | TREE_TYPE(map_tree), | |
9948 | map_tree, | |
9949 | const_ptr_type_node, | |
9950 | tmpref, | |
9951 | boolean_type_node, | |
9952 | (insert | |
9953 | ? boolean_true_node | |
9954 | : boolean_false_node)); | |
5fb82b5e | 9955 | if (call == error_mark_node) |
9956 | return error_mark_node; | |
e440a328 | 9957 | // This can panic on a map of interface type if the interface holds |
9958 | // an uncomparable or unhashable type. | |
9959 | TREE_NOTHROW(map_index_fndecl) = 0; | |
9960 | ||
9961 | tree val_type_tree = type->val_type()->get_tree(context->gogo()); | |
9962 | if (val_type_tree == error_mark_node) | |
9963 | return error_mark_node; | |
9964 | tree ptr_val_type_tree = build_pointer_type(val_type_tree); | |
9965 | ||
746d2e73 | 9966 | tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call); |
9967 | if (make_tmp != NULL_TREE) | |
9968 | ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret); | |
9969 | return ret; | |
e440a328 | 9970 | } |
9971 | ||
9972 | // Make a map index expression. | |
9973 | ||
9974 | Map_index_expression* | |
9975 | Expression::make_map_index(Expression* map, Expression* index, | |
9976 | source_location location) | |
9977 | { | |
9978 | return new Map_index_expression(map, index, location); | |
9979 | } | |
9980 | ||
9981 | // Class Field_reference_expression. | |
9982 | ||
9983 | // Return the type of a field reference. | |
9984 | ||
9985 | Type* | |
9986 | Field_reference_expression::do_type() | |
9987 | { | |
b0e628fb | 9988 | Type* type = this->expr_->type(); |
9989 | if (type->is_error_type()) | |
9990 | return type; | |
9991 | Struct_type* struct_type = type->struct_type(); | |
e440a328 | 9992 | gcc_assert(struct_type != NULL); |
9993 | return struct_type->field(this->field_index_)->type(); | |
9994 | } | |
9995 | ||
9996 | // Check the types for a field reference. | |
9997 | ||
9998 | void | |
9999 | Field_reference_expression::do_check_types(Gogo*) | |
10000 | { | |
b0e628fb | 10001 | Type* type = this->expr_->type(); |
10002 | if (type->is_error_type()) | |
10003 | return; | |
10004 | Struct_type* struct_type = type->struct_type(); | |
e440a328 | 10005 | gcc_assert(struct_type != NULL); |
10006 | gcc_assert(struct_type->field(this->field_index_) != NULL); | |
10007 | } | |
10008 | ||
10009 | // Get a tree for a field reference. | |
10010 | ||
10011 | tree | |
10012 | Field_reference_expression::do_get_tree(Translate_context* context) | |
10013 | { | |
10014 | tree struct_tree = this->expr_->get_tree(context); | |
10015 | if (struct_tree == error_mark_node | |
10016 | || TREE_TYPE(struct_tree) == error_mark_node) | |
10017 | return error_mark_node; | |
10018 | gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE); | |
10019 | tree field = TYPE_FIELDS(TREE_TYPE(struct_tree)); | |
b1d655d5 | 10020 | if (field == NULL_TREE) |
10021 | { | |
10022 | // This can happen for a type which refers to itself indirectly | |
10023 | // and then turns out to be erroneous. | |
10024 | gcc_assert(saw_errors()); | |
10025 | return error_mark_node; | |
10026 | } | |
e440a328 | 10027 | for (unsigned int i = this->field_index_; i > 0; --i) |
10028 | { | |
10029 | field = DECL_CHAIN(field); | |
10030 | gcc_assert(field != NULL_TREE); | |
10031 | } | |
c35179ff | 10032 | if (TREE_TYPE(field) == error_mark_node) |
10033 | return error_mark_node; | |
e440a328 | 10034 | return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field, |
10035 | NULL_TREE); | |
10036 | } | |
10037 | ||
10038 | // Make a reference to a qualified identifier in an expression. | |
10039 | ||
10040 | Field_reference_expression* | |
10041 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
10042 | source_location location) | |
10043 | { | |
10044 | return new Field_reference_expression(expr, field_index, location); | |
10045 | } | |
10046 | ||
10047 | // Class Interface_field_reference_expression. | |
10048 | ||
10049 | // Return a tree for the pointer to the function to call. | |
10050 | ||
10051 | tree | |
10052 | Interface_field_reference_expression::get_function_tree(Translate_context*, | |
10053 | tree expr) | |
10054 | { | |
10055 | if (this->expr_->type()->points_to() != NULL) | |
10056 | expr = build_fold_indirect_ref(expr); | |
10057 | ||
10058 | tree expr_type = TREE_TYPE(expr); | |
10059 | gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE); | |
10060 | ||
10061 | tree field = TYPE_FIELDS(expr_type); | |
10062 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0); | |
10063 | ||
10064 | tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
10065 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(table))); | |
10066 | ||
10067 | table = build_fold_indirect_ref(table); | |
10068 | gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE); | |
10069 | ||
10070 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
10071 | for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table))); | |
10072 | field != NULL_TREE; | |
10073 | field = DECL_CHAIN(field)) | |
10074 | { | |
10075 | if (name == IDENTIFIER_POINTER(DECL_NAME(field))) | |
10076 | break; | |
10077 | } | |
10078 | gcc_assert(field != NULL_TREE); | |
10079 | ||
10080 | return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE); | |
10081 | } | |
10082 | ||
10083 | // Return a tree for the first argument to pass to the interface | |
10084 | // function. | |
10085 | ||
10086 | tree | |
10087 | Interface_field_reference_expression::get_underlying_object_tree( | |
10088 | Translate_context*, | |
10089 | tree expr) | |
10090 | { | |
10091 | if (this->expr_->type()->points_to() != NULL) | |
10092 | expr = build_fold_indirect_ref(expr); | |
10093 | ||
10094 | tree expr_type = TREE_TYPE(expr); | |
10095 | gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE); | |
10096 | ||
10097 | tree field = DECL_CHAIN(TYPE_FIELDS(expr_type)); | |
10098 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); | |
10099 | ||
10100 | return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
10101 | } | |
10102 | ||
10103 | // Traversal. | |
10104 | ||
10105 | int | |
10106 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
10107 | { | |
10108 | return Expression::traverse(&this->expr_, traverse); | |
10109 | } | |
10110 | ||
10111 | // Return the type of an interface field reference. | |
10112 | ||
10113 | Type* | |
10114 | Interface_field_reference_expression::do_type() | |
10115 | { | |
10116 | Type* expr_type = this->expr_->type(); | |
10117 | ||
10118 | Type* points_to = expr_type->points_to(); | |
10119 | if (points_to != NULL) | |
10120 | expr_type = points_to; | |
10121 | ||
10122 | Interface_type* interface_type = expr_type->interface_type(); | |
10123 | if (interface_type == NULL) | |
10124 | return Type::make_error_type(); | |
10125 | ||
10126 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
10127 | if (method == NULL) | |
10128 | return Type::make_error_type(); | |
10129 | ||
10130 | return method->type(); | |
10131 | } | |
10132 | ||
10133 | // Determine types. | |
10134 | ||
10135 | void | |
10136 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
10137 | { | |
10138 | this->expr_->determine_type_no_context(); | |
10139 | } | |
10140 | ||
10141 | // Check the types for an interface field reference. | |
10142 | ||
10143 | void | |
10144 | Interface_field_reference_expression::do_check_types(Gogo*) | |
10145 | { | |
10146 | Type* type = this->expr_->type(); | |
10147 | ||
10148 | Type* points_to = type->points_to(); | |
10149 | if (points_to != NULL) | |
10150 | type = points_to; | |
10151 | ||
10152 | Interface_type* interface_type = type->interface_type(); | |
10153 | if (interface_type == NULL) | |
10154 | this->report_error(_("expected interface or pointer to interface")); | |
10155 | else | |
10156 | { | |
10157 | const Typed_identifier* method = | |
10158 | interface_type->find_method(this->name_); | |
10159 | if (method == NULL) | |
10160 | { | |
10161 | error_at(this->location(), "method %qs not in interface", | |
10162 | Gogo::message_name(this->name_).c_str()); | |
10163 | this->set_is_error(); | |
10164 | } | |
10165 | } | |
10166 | } | |
10167 | ||
10168 | // Get a tree for a reference to a field in an interface. There is no | |
10169 | // standard tree type representation for this: it's a function | |
10170 | // attached to its first argument, like a Bound_method_expression. | |
10171 | // The only places it may currently be used are in a Call_expression | |
10172 | // or a Go_statement, which will take it apart directly. So this has | |
10173 | // nothing to do at present. | |
10174 | ||
10175 | tree | |
10176 | Interface_field_reference_expression::do_get_tree(Translate_context*) | |
10177 | { | |
10178 | gcc_unreachable(); | |
10179 | } | |
10180 | ||
10181 | // Make a reference to a field in an interface. | |
10182 | ||
10183 | Expression* | |
10184 | Expression::make_interface_field_reference(Expression* expr, | |
10185 | const std::string& field, | |
10186 | source_location location) | |
10187 | { | |
10188 | return new Interface_field_reference_expression(expr, field, location); | |
10189 | } | |
10190 | ||
10191 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
10192 | // is lowered after we know the type of the left hand side. | |
10193 | ||
10194 | class Selector_expression : public Parser_expression | |
10195 | { | |
10196 | public: | |
10197 | Selector_expression(Expression* left, const std::string& name, | |
10198 | source_location location) | |
10199 | : Parser_expression(EXPRESSION_SELECTOR, location), | |
10200 | left_(left), name_(name) | |
10201 | { } | |
10202 | ||
10203 | protected: | |
10204 | int | |
10205 | do_traverse(Traverse* traverse) | |
10206 | { return Expression::traverse(&this->left_, traverse); } | |
10207 | ||
10208 | Expression* | |
10209 | do_lower(Gogo*, Named_object*, int); | |
10210 | ||
10211 | Expression* | |
10212 | do_copy() | |
10213 | { | |
10214 | return new Selector_expression(this->left_->copy(), this->name_, | |
10215 | this->location()); | |
10216 | } | |
10217 | ||
10218 | private: | |
10219 | Expression* | |
10220 | lower_method_expression(Gogo*); | |
10221 | ||
10222 | // The expression on the left hand side. | |
10223 | Expression* left_; | |
10224 | // The name on the right hand side. | |
10225 | std::string name_; | |
10226 | }; | |
10227 | ||
10228 | // Lower a selector expression once we know the real type of the left | |
10229 | // hand side. | |
10230 | ||
10231 | Expression* | |
10232 | Selector_expression::do_lower(Gogo* gogo, Named_object*, int) | |
10233 | { | |
10234 | Expression* left = this->left_; | |
10235 | if (left->is_type_expression()) | |
10236 | return this->lower_method_expression(gogo); | |
10237 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
10238 | this->location()); | |
10239 | } | |
10240 | ||
10241 | // Lower a method expression T.M or (*T).M. We turn this into a | |
10242 | // function literal. | |
10243 | ||
10244 | Expression* | |
10245 | Selector_expression::lower_method_expression(Gogo* gogo) | |
10246 | { | |
10247 | source_location location = this->location(); | |
10248 | Type* type = this->left_->type(); | |
10249 | const std::string& name(this->name_); | |
10250 | ||
10251 | bool is_pointer; | |
10252 | if (type->points_to() == NULL) | |
10253 | is_pointer = false; | |
10254 | else | |
10255 | { | |
10256 | is_pointer = true; | |
10257 | type = type->points_to(); | |
10258 | } | |
10259 | Named_type* nt = type->named_type(); | |
10260 | if (nt == NULL) | |
10261 | { | |
10262 | error_at(location, | |
10263 | ("method expression requires named type or " | |
10264 | "pointer to named type")); | |
10265 | return Expression::make_error(location); | |
10266 | } | |
10267 | ||
10268 | bool is_ambiguous; | |
10269 | Method* method = nt->method_function(name, &is_ambiguous); | |
10270 | if (method == NULL) | |
10271 | { | |
10272 | if (!is_ambiguous) | |
10273 | error_at(location, "type %<%s%> has no method %<%s%>", | |
10274 | nt->message_name().c_str(), | |
10275 | Gogo::message_name(name).c_str()); | |
10276 | else | |
10277 | error_at(location, "method %<%s%> is ambiguous in type %<%s%>", | |
10278 | Gogo::message_name(name).c_str(), | |
10279 | nt->message_name().c_str()); | |
10280 | return Expression::make_error(location); | |
10281 | } | |
10282 | ||
10283 | if (!is_pointer && !method->is_value_method()) | |
10284 | { | |
10285 | error_at(location, "method requires pointer (use %<(*%s).%s)%>", | |
10286 | nt->message_name().c_str(), | |
10287 | Gogo::message_name(name).c_str()); | |
10288 | return Expression::make_error(location); | |
10289 | } | |
10290 | ||
10291 | // Build a new function type in which the receiver becomes the first | |
10292 | // argument. | |
10293 | Function_type* method_type = method->type(); | |
10294 | gcc_assert(method_type->is_method()); | |
10295 | ||
10296 | const char* const receiver_name = "$this"; | |
10297 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
10298 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
10299 | location)); | |
10300 | ||
10301 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
10302 | if (method_parameters != NULL) | |
10303 | { | |
10304 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); | |
10305 | p != method_parameters->end(); | |
10306 | ++p) | |
10307 | parameters->push_back(*p); | |
10308 | } | |
10309 | ||
10310 | const Typed_identifier_list* method_results = method_type->results(); | |
10311 | Typed_identifier_list* results; | |
10312 | if (method_results == NULL) | |
10313 | results = NULL; | |
10314 | else | |
10315 | { | |
10316 | results = new Typed_identifier_list(); | |
10317 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
10318 | p != method_results->end(); | |
10319 | ++p) | |
10320 | results->push_back(*p); | |
10321 | } | |
10322 | ||
10323 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
10324 | location); | |
10325 | if (method_type->is_varargs()) | |
10326 | fntype->set_is_varargs(); | |
10327 | ||
10328 | // We generate methods which always takes a pointer to the receiver | |
10329 | // as their first argument. If this is for a pointer type, we can | |
10330 | // simply reuse the existing function. We use an internal hack to | |
10331 | // get the right type. | |
10332 | ||
10333 | if (is_pointer) | |
10334 | { | |
10335 | Named_object* mno = (method->needs_stub_method() | |
10336 | ? method->stub_object() | |
10337 | : method->named_object()); | |
10338 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
10339 | f = Expression::make_cast(fntype, f, location); | |
10340 | Type_conversion_expression* tce = | |
10341 | static_cast<Type_conversion_expression*>(f); | |
10342 | tce->set_may_convert_function_types(); | |
10343 | return f; | |
10344 | } | |
10345 | ||
10346 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
10347 | location); | |
10348 | ||
10349 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
10350 | gcc_assert(vno != NULL); | |
10351 | Expression* ve = Expression::make_var_reference(vno, location); | |
10352 | Expression* bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
f690b0bb | 10353 | |
10354 | // Even though we found the method above, if it has an error type we | |
10355 | // may see an error here. | |
10356 | if (bm->is_error_expression()) | |
463fe805 | 10357 | { |
10358 | gogo->finish_function(location); | |
10359 | return bm; | |
10360 | } | |
e440a328 | 10361 | |
10362 | Expression_list* args; | |
10363 | if (method_parameters == NULL) | |
10364 | args = NULL; | |
10365 | else | |
10366 | { | |
10367 | args = new Expression_list(); | |
10368 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); | |
10369 | p != method_parameters->end(); | |
10370 | ++p) | |
10371 | { | |
10372 | vno = gogo->lookup(p->name(), NULL); | |
10373 | gcc_assert(vno != NULL); | |
10374 | args->push_back(Expression::make_var_reference(vno, location)); | |
10375 | } | |
10376 | } | |
10377 | ||
10378 | Call_expression* call = Expression::make_call(bm, args, | |
10379 | method_type->is_varargs(), | |
10380 | location); | |
10381 | ||
10382 | size_t count = call->result_count(); | |
10383 | Statement* s; | |
10384 | if (count == 0) | |
10385 | s = Statement::make_statement(call); | |
10386 | else | |
10387 | { | |
10388 | Expression_list* retvals = new Expression_list(); | |
10389 | if (count <= 1) | |
10390 | retvals->push_back(call); | |
10391 | else | |
10392 | { | |
10393 | for (size_t i = 0; i < count; ++i) | |
10394 | retvals->push_back(Expression::make_call_result(call, i)); | |
10395 | } | |
10396 | s = Statement::make_return_statement(no->func_value()->type()->results(), | |
10397 | retvals, location); | |
10398 | } | |
10399 | gogo->add_statement(s); | |
10400 | ||
10401 | gogo->finish_function(location); | |
10402 | ||
10403 | return Expression::make_func_reference(no, NULL, location); | |
10404 | } | |
10405 | ||
10406 | // Make a selector expression. | |
10407 | ||
10408 | Expression* | |
10409 | Expression::make_selector(Expression* left, const std::string& name, | |
10410 | source_location location) | |
10411 | { | |
10412 | return new Selector_expression(left, name, location); | |
10413 | } | |
10414 | ||
10415 | // Implement the builtin function new. | |
10416 | ||
10417 | class Allocation_expression : public Expression | |
10418 | { | |
10419 | public: | |
10420 | Allocation_expression(Type* type, source_location location) | |
10421 | : Expression(EXPRESSION_ALLOCATION, location), | |
10422 | type_(type) | |
10423 | { } | |
10424 | ||
10425 | protected: | |
10426 | int | |
10427 | do_traverse(Traverse* traverse) | |
10428 | { return Type::traverse(this->type_, traverse); } | |
10429 | ||
10430 | Type* | |
10431 | do_type() | |
10432 | { return Type::make_pointer_type(this->type_); } | |
10433 | ||
10434 | void | |
10435 | do_determine_type(const Type_context*) | |
10436 | { } | |
10437 | ||
10438 | void | |
10439 | do_check_types(Gogo*); | |
10440 | ||
10441 | Expression* | |
10442 | do_copy() | |
10443 | { return new Allocation_expression(this->type_, this->location()); } | |
10444 | ||
10445 | tree | |
10446 | do_get_tree(Translate_context*); | |
10447 | ||
10448 | private: | |
10449 | // The type we are allocating. | |
10450 | Type* type_; | |
10451 | }; | |
10452 | ||
10453 | // Check the type of an allocation expression. | |
10454 | ||
10455 | void | |
10456 | Allocation_expression::do_check_types(Gogo*) | |
10457 | { | |
10458 | if (this->type_->function_type() != NULL) | |
10459 | this->report_error(_("invalid new of function type")); | |
10460 | } | |
10461 | ||
10462 | // Return a tree for an allocation expression. | |
10463 | ||
10464 | tree | |
10465 | Allocation_expression::do_get_tree(Translate_context* context) | |
10466 | { | |
10467 | tree type_tree = this->type_->get_tree(context->gogo()); | |
19824ddb | 10468 | if (type_tree == error_mark_node) |
10469 | return error_mark_node; | |
e440a328 | 10470 | tree size_tree = TYPE_SIZE_UNIT(type_tree); |
10471 | tree space = context->gogo()->allocate_memory(this->type_, size_tree, | |
10472 | this->location()); | |
19824ddb | 10473 | if (space == error_mark_node) |
10474 | return error_mark_node; | |
e440a328 | 10475 | return fold_convert(build_pointer_type(type_tree), space); |
10476 | } | |
10477 | ||
10478 | // Make an allocation expression. | |
10479 | ||
10480 | Expression* | |
10481 | Expression::make_allocation(Type* type, source_location location) | |
10482 | { | |
10483 | return new Allocation_expression(type, location); | |
10484 | } | |
10485 | ||
10486 | // Implement the builtin function make. | |
10487 | ||
10488 | class Make_expression : public Expression | |
10489 | { | |
10490 | public: | |
10491 | Make_expression(Type* type, Expression_list* args, source_location location) | |
10492 | : Expression(EXPRESSION_MAKE, location), | |
10493 | type_(type), args_(args) | |
10494 | { } | |
10495 | ||
10496 | protected: | |
10497 | int | |
10498 | do_traverse(Traverse* traverse); | |
10499 | ||
10500 | Type* | |
10501 | do_type() | |
10502 | { return this->type_; } | |
10503 | ||
10504 | void | |
10505 | do_determine_type(const Type_context*); | |
10506 | ||
10507 | void | |
10508 | do_check_types(Gogo*); | |
10509 | ||
10510 | Expression* | |
10511 | do_copy() | |
10512 | { | |
10513 | return new Make_expression(this->type_, this->args_->copy(), | |
10514 | this->location()); | |
10515 | } | |
10516 | ||
10517 | tree | |
10518 | do_get_tree(Translate_context*); | |
10519 | ||
10520 | private: | |
10521 | // The type we are making. | |
10522 | Type* type_; | |
10523 | // The arguments to pass to the make routine. | |
10524 | Expression_list* args_; | |
10525 | }; | |
10526 | ||
10527 | // Traversal. | |
10528 | ||
10529 | int | |
10530 | Make_expression::do_traverse(Traverse* traverse) | |
10531 | { | |
10532 | if (this->args_ != NULL | |
10533 | && this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
10534 | return TRAVERSE_EXIT; | |
10535 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10536 | return TRAVERSE_EXIT; | |
10537 | return TRAVERSE_CONTINUE; | |
10538 | } | |
10539 | ||
10540 | // Set types of arguments. | |
10541 | ||
10542 | void | |
10543 | Make_expression::do_determine_type(const Type_context*) | |
10544 | { | |
10545 | if (this->args_ != NULL) | |
10546 | { | |
10547 | Type_context context(Type::lookup_integer_type("int"), false); | |
10548 | for (Expression_list::const_iterator pe = this->args_->begin(); | |
10549 | pe != this->args_->end(); | |
10550 | ++pe) | |
10551 | (*pe)->determine_type(&context); | |
10552 | } | |
10553 | } | |
10554 | ||
10555 | // Check types for a make expression. | |
10556 | ||
10557 | void | |
10558 | Make_expression::do_check_types(Gogo*) | |
10559 | { | |
10560 | if (this->type_->channel_type() == NULL | |
10561 | && this->type_->map_type() == NULL | |
10562 | && (this->type_->array_type() == NULL | |
10563 | || this->type_->array_type()->length() != NULL)) | |
10564 | this->report_error(_("invalid type for make function")); | |
10565 | else if (!this->type_->check_make_expression(this->args_, this->location())) | |
10566 | this->set_is_error(); | |
10567 | } | |
10568 | ||
10569 | // Return a tree for a make expression. | |
10570 | ||
10571 | tree | |
10572 | Make_expression::do_get_tree(Translate_context* context) | |
10573 | { | |
10574 | return this->type_->make_expression_tree(context, this->args_, | |
10575 | this->location()); | |
10576 | } | |
10577 | ||
10578 | // Make a make expression. | |
10579 | ||
10580 | Expression* | |
10581 | Expression::make_make(Type* type, Expression_list* args, | |
10582 | source_location location) | |
10583 | { | |
10584 | return new Make_expression(type, args, location); | |
10585 | } | |
10586 | ||
10587 | // Construct a struct. | |
10588 | ||
10589 | class Struct_construction_expression : public Expression | |
10590 | { | |
10591 | public: | |
10592 | Struct_construction_expression(Type* type, Expression_list* vals, | |
10593 | source_location location) | |
10594 | : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location), | |
10595 | type_(type), vals_(vals) | |
10596 | { } | |
10597 | ||
10598 | // Return whether this is a constant initializer. | |
10599 | bool | |
10600 | is_constant_struct() const; | |
10601 | ||
10602 | protected: | |
10603 | int | |
10604 | do_traverse(Traverse* traverse); | |
10605 | ||
10606 | Type* | |
10607 | do_type() | |
10608 | { return this->type_; } | |
10609 | ||
10610 | void | |
10611 | do_determine_type(const Type_context*); | |
10612 | ||
10613 | void | |
10614 | do_check_types(Gogo*); | |
10615 | ||
10616 | Expression* | |
10617 | do_copy() | |
10618 | { | |
10619 | return new Struct_construction_expression(this->type_, this->vals_->copy(), | |
10620 | this->location()); | |
10621 | } | |
10622 | ||
10623 | bool | |
10624 | do_is_addressable() const | |
10625 | { return true; } | |
10626 | ||
10627 | tree | |
10628 | do_get_tree(Translate_context*); | |
10629 | ||
10630 | void | |
10631 | do_export(Export*) const; | |
10632 | ||
10633 | private: | |
10634 | // The type of the struct to construct. | |
10635 | Type* type_; | |
10636 | // The list of values, in order of the fields in the struct. A NULL | |
10637 | // entry means that the field should be zero-initialized. | |
10638 | Expression_list* vals_; | |
10639 | }; | |
10640 | ||
10641 | // Traversal. | |
10642 | ||
10643 | int | |
10644 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
10645 | { | |
10646 | if (this->vals_ != NULL | |
10647 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
10648 | return TRAVERSE_EXIT; | |
10649 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10650 | return TRAVERSE_EXIT; | |
10651 | return TRAVERSE_CONTINUE; | |
10652 | } | |
10653 | ||
10654 | // Return whether this is a constant initializer. | |
10655 | ||
10656 | bool | |
10657 | Struct_construction_expression::is_constant_struct() const | |
10658 | { | |
10659 | if (this->vals_ == NULL) | |
10660 | return true; | |
10661 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10662 | pv != this->vals_->end(); | |
10663 | ++pv) | |
10664 | { | |
10665 | if (*pv != NULL | |
10666 | && !(*pv)->is_constant() | |
10667 | && (!(*pv)->is_composite_literal() | |
10668 | || (*pv)->is_nonconstant_composite_literal())) | |
10669 | return false; | |
10670 | } | |
10671 | ||
10672 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10673 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10674 | pf != fields->end(); | |
10675 | ++pf) | |
10676 | { | |
10677 | // There are no constant constructors for interfaces. | |
10678 | if (pf->type()->interface_type() != NULL) | |
10679 | return false; | |
10680 | } | |
10681 | ||
10682 | return true; | |
10683 | } | |
10684 | ||
10685 | // Final type determination. | |
10686 | ||
10687 | void | |
10688 | Struct_construction_expression::do_determine_type(const Type_context*) | |
10689 | { | |
10690 | if (this->vals_ == NULL) | |
10691 | return; | |
10692 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10693 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10694 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10695 | pf != fields->end(); | |
10696 | ++pf, ++pv) | |
10697 | { | |
10698 | if (pv == this->vals_->end()) | |
10699 | return; | |
10700 | if (*pv != NULL) | |
10701 | { | |
10702 | Type_context subcontext(pf->type(), false); | |
10703 | (*pv)->determine_type(&subcontext); | |
10704 | } | |
10705 | } | |
a6cb4c0e | 10706 | // Extra values are an error we will report elsewhere; we still want |
10707 | // to determine the type to avoid knockon errors. | |
10708 | for (; pv != this->vals_->end(); ++pv) | |
10709 | (*pv)->determine_type_no_context(); | |
e440a328 | 10710 | } |
10711 | ||
10712 | // Check types. | |
10713 | ||
10714 | void | |
10715 | Struct_construction_expression::do_check_types(Gogo*) | |
10716 | { | |
10717 | if (this->vals_ == NULL) | |
10718 | return; | |
10719 | ||
10720 | Struct_type* st = this->type_->struct_type(); | |
10721 | if (this->vals_->size() > st->field_count()) | |
10722 | { | |
10723 | this->report_error(_("too many expressions for struct")); | |
10724 | return; | |
10725 | } | |
10726 | ||
10727 | const Struct_field_list* fields = st->fields(); | |
10728 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10729 | int i = 0; | |
10730 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10731 | pf != fields->end(); | |
10732 | ++pf, ++pv, ++i) | |
10733 | { | |
10734 | if (pv == this->vals_->end()) | |
10735 | { | |
10736 | this->report_error(_("too few expressions for struct")); | |
10737 | break; | |
10738 | } | |
10739 | ||
10740 | if (*pv == NULL) | |
10741 | continue; | |
10742 | ||
10743 | std::string reason; | |
10744 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
10745 | { | |
10746 | if (reason.empty()) | |
10747 | error_at((*pv)->location(), | |
10748 | "incompatible type for field %d in struct construction", | |
10749 | i + 1); | |
10750 | else | |
10751 | error_at((*pv)->location(), | |
10752 | ("incompatible type for field %d in " | |
10753 | "struct construction (%s)"), | |
10754 | i + 1, reason.c_str()); | |
10755 | this->set_is_error(); | |
10756 | } | |
10757 | } | |
10758 | gcc_assert(pv == this->vals_->end()); | |
10759 | } | |
10760 | ||
10761 | // Return a tree for constructing a struct. | |
10762 | ||
10763 | tree | |
10764 | Struct_construction_expression::do_get_tree(Translate_context* context) | |
10765 | { | |
10766 | Gogo* gogo = context->gogo(); | |
10767 | ||
10768 | if (this->vals_ == NULL) | |
10769 | return this->type_->get_init_tree(gogo, false); | |
10770 | ||
10771 | tree type_tree = this->type_->get_tree(gogo); | |
10772 | if (type_tree == error_mark_node) | |
10773 | return error_mark_node; | |
10774 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); | |
10775 | ||
10776 | bool is_constant = true; | |
10777 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10778 | VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc, | |
10779 | fields->size()); | |
10780 | Struct_field_list::const_iterator pf = fields->begin(); | |
10781 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10782 | for (tree field = TYPE_FIELDS(type_tree); | |
10783 | field != NULL_TREE; | |
10784 | field = DECL_CHAIN(field), ++pf) | |
10785 | { | |
10786 | gcc_assert(pf != fields->end()); | |
10787 | ||
10788 | tree val; | |
10789 | if (pv == this->vals_->end()) | |
10790 | val = pf->type()->get_init_tree(gogo, false); | |
10791 | else if (*pv == NULL) | |
10792 | { | |
10793 | val = pf->type()->get_init_tree(gogo, false); | |
10794 | ++pv; | |
10795 | } | |
10796 | else | |
10797 | { | |
10798 | val = Expression::convert_for_assignment(context, pf->type(), | |
10799 | (*pv)->type(), | |
10800 | (*pv)->get_tree(context), | |
10801 | this->location()); | |
10802 | ++pv; | |
10803 | } | |
10804 | ||
10805 | if (val == error_mark_node || TREE_TYPE(val) == error_mark_node) | |
10806 | return error_mark_node; | |
10807 | ||
10808 | constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL); | |
10809 | elt->index = field; | |
10810 | elt->value = val; | |
10811 | if (!TREE_CONSTANT(val)) | |
10812 | is_constant = false; | |
10813 | } | |
10814 | gcc_assert(pf == fields->end()); | |
10815 | ||
10816 | tree ret = build_constructor(type_tree, elts); | |
10817 | if (is_constant) | |
10818 | TREE_CONSTANT(ret) = 1; | |
10819 | return ret; | |
10820 | } | |
10821 | ||
10822 | // Export a struct construction. | |
10823 | ||
10824 | void | |
10825 | Struct_construction_expression::do_export(Export* exp) const | |
10826 | { | |
10827 | exp->write_c_string("convert("); | |
10828 | exp->write_type(this->type_); | |
10829 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10830 | pv != this->vals_->end(); | |
10831 | ++pv) | |
10832 | { | |
10833 | exp->write_c_string(", "); | |
10834 | if (*pv != NULL) | |
10835 | (*pv)->export_expression(exp); | |
10836 | } | |
10837 | exp->write_c_string(")"); | |
10838 | } | |
10839 | ||
10840 | // Make a struct composite literal. This used by the thunk code. | |
10841 | ||
10842 | Expression* | |
10843 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
10844 | source_location location) | |
10845 | { | |
10846 | gcc_assert(type->struct_type() != NULL); | |
10847 | return new Struct_construction_expression(type, vals, location); | |
10848 | } | |
10849 | ||
10850 | // Construct an array. This class is not used directly; instead we | |
10851 | // use the child classes, Fixed_array_construction_expression and | |
10852 | // Open_array_construction_expression. | |
10853 | ||
10854 | class Array_construction_expression : public Expression | |
10855 | { | |
10856 | protected: | |
10857 | Array_construction_expression(Expression_classification classification, | |
10858 | Type* type, Expression_list* vals, | |
10859 | source_location location) | |
10860 | : Expression(classification, location), | |
10861 | type_(type), vals_(vals) | |
10862 | { } | |
10863 | ||
10864 | public: | |
10865 | // Return whether this is a constant initializer. | |
10866 | bool | |
10867 | is_constant_array() const; | |
10868 | ||
10869 | // Return the number of elements. | |
10870 | size_t | |
10871 | element_count() const | |
10872 | { return this->vals_ == NULL ? 0 : this->vals_->size(); } | |
10873 | ||
10874 | protected: | |
10875 | int | |
10876 | do_traverse(Traverse* traverse); | |
10877 | ||
10878 | Type* | |
10879 | do_type() | |
10880 | { return this->type_; } | |
10881 | ||
10882 | void | |
10883 | do_determine_type(const Type_context*); | |
10884 | ||
10885 | void | |
10886 | do_check_types(Gogo*); | |
10887 | ||
10888 | bool | |
10889 | do_is_addressable() const | |
10890 | { return true; } | |
10891 | ||
10892 | void | |
10893 | do_export(Export*) const; | |
10894 | ||
10895 | // The list of values. | |
10896 | Expression_list* | |
10897 | vals() | |
10898 | { return this->vals_; } | |
10899 | ||
10900 | // Get a constructor tree for the array values. | |
10901 | tree | |
10902 | get_constructor_tree(Translate_context* context, tree type_tree); | |
10903 | ||
10904 | private: | |
10905 | // The type of the array to construct. | |
10906 | Type* type_; | |
10907 | // The list of values. | |
10908 | Expression_list* vals_; | |
10909 | }; | |
10910 | ||
10911 | // Traversal. | |
10912 | ||
10913 | int | |
10914 | Array_construction_expression::do_traverse(Traverse* traverse) | |
10915 | { | |
10916 | if (this->vals_ != NULL | |
10917 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
10918 | return TRAVERSE_EXIT; | |
10919 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10920 | return TRAVERSE_EXIT; | |
10921 | return TRAVERSE_CONTINUE; | |
10922 | } | |
10923 | ||
10924 | // Return whether this is a constant initializer. | |
10925 | ||
10926 | bool | |
10927 | Array_construction_expression::is_constant_array() const | |
10928 | { | |
10929 | if (this->vals_ == NULL) | |
10930 | return true; | |
10931 | ||
10932 | // There are no constant constructors for interfaces. | |
10933 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
10934 | return false; | |
10935 | ||
10936 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10937 | pv != this->vals_->end(); | |
10938 | ++pv) | |
10939 | { | |
10940 | if (*pv != NULL | |
10941 | && !(*pv)->is_constant() | |
10942 | && (!(*pv)->is_composite_literal() | |
10943 | || (*pv)->is_nonconstant_composite_literal())) | |
10944 | return false; | |
10945 | } | |
10946 | return true; | |
10947 | } | |
10948 | ||
10949 | // Final type determination. | |
10950 | ||
10951 | void | |
10952 | Array_construction_expression::do_determine_type(const Type_context*) | |
10953 | { | |
10954 | if (this->vals_ == NULL) | |
10955 | return; | |
10956 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
10957 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10958 | pv != this->vals_->end(); | |
10959 | ++pv) | |
10960 | { | |
10961 | if (*pv != NULL) | |
10962 | (*pv)->determine_type(&subcontext); | |
10963 | } | |
10964 | } | |
10965 | ||
10966 | // Check types. | |
10967 | ||
10968 | void | |
10969 | Array_construction_expression::do_check_types(Gogo*) | |
10970 | { | |
10971 | if (this->vals_ == NULL) | |
10972 | return; | |
10973 | ||
10974 | Array_type* at = this->type_->array_type(); | |
10975 | int i = 0; | |
10976 | Type* element_type = at->element_type(); | |
10977 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10978 | pv != this->vals_->end(); | |
10979 | ++pv, ++i) | |
10980 | { | |
10981 | if (*pv != NULL | |
10982 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
10983 | { | |
10984 | error_at((*pv)->location(), | |
10985 | "incompatible type for element %d in composite literal", | |
10986 | i + 1); | |
10987 | this->set_is_error(); | |
10988 | } | |
10989 | } | |
10990 | ||
10991 | Expression* length = at->length(); | |
10992 | if (length != NULL) | |
10993 | { | |
10994 | mpz_t val; | |
10995 | mpz_init(val); | |
10996 | Type* type; | |
10997 | if (at->length()->integer_constant_value(true, val, &type)) | |
10998 | { | |
10999 | if (this->vals_->size() > mpz_get_ui(val)) | |
11000 | this->report_error(_("too many elements in composite literal")); | |
11001 | } | |
11002 | mpz_clear(val); | |
11003 | } | |
11004 | } | |
11005 | ||
11006 | // Get a constructor tree for the array values. | |
11007 | ||
11008 | tree | |
11009 | Array_construction_expression::get_constructor_tree(Translate_context* context, | |
11010 | tree type_tree) | |
11011 | { | |
11012 | VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc, | |
11013 | (this->vals_ == NULL | |
11014 | ? 0 | |
11015 | : this->vals_->size())); | |
11016 | Type* element_type = this->type_->array_type()->element_type(); | |
11017 | bool is_constant = true; | |
11018 | if (this->vals_ != NULL) | |
11019 | { | |
11020 | size_t i = 0; | |
11021 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11022 | pv != this->vals_->end(); | |
11023 | ++pv, ++i) | |
11024 | { | |
11025 | constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL); | |
11026 | elt->index = size_int(i); | |
11027 | if (*pv == NULL) | |
11028 | elt->value = element_type->get_init_tree(context->gogo(), false); | |
11029 | else | |
11030 | { | |
11031 | tree value_tree = (*pv)->get_tree(context); | |
11032 | elt->value = Expression::convert_for_assignment(context, | |
11033 | element_type, | |
11034 | (*pv)->type(), | |
11035 | value_tree, | |
11036 | this->location()); | |
11037 | } | |
11038 | if (elt->value == error_mark_node) | |
11039 | return error_mark_node; | |
11040 | if (!TREE_CONSTANT(elt->value)) | |
11041 | is_constant = false; | |
11042 | } | |
11043 | } | |
11044 | ||
11045 | tree ret = build_constructor(type_tree, values); | |
11046 | if (is_constant) | |
11047 | TREE_CONSTANT(ret) = 1; | |
11048 | return ret; | |
11049 | } | |
11050 | ||
11051 | // Export an array construction. | |
11052 | ||
11053 | void | |
11054 | Array_construction_expression::do_export(Export* exp) const | |
11055 | { | |
11056 | exp->write_c_string("convert("); | |
11057 | exp->write_type(this->type_); | |
11058 | if (this->vals_ != NULL) | |
11059 | { | |
11060 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11061 | pv != this->vals_->end(); | |
11062 | ++pv) | |
11063 | { | |
11064 | exp->write_c_string(", "); | |
11065 | if (*pv != NULL) | |
11066 | (*pv)->export_expression(exp); | |
11067 | } | |
11068 | } | |
11069 | exp->write_c_string(")"); | |
11070 | } | |
11071 | ||
11072 | // Construct a fixed array. | |
11073 | ||
11074 | class Fixed_array_construction_expression : | |
11075 | public Array_construction_expression | |
11076 | { | |
11077 | public: | |
11078 | Fixed_array_construction_expression(Type* type, Expression_list* vals, | |
11079 | source_location location) | |
11080 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, | |
11081 | type, vals, location) | |
11082 | { | |
11083 | gcc_assert(type->array_type() != NULL | |
11084 | && type->array_type()->length() != NULL); | |
11085 | } | |
11086 | ||
11087 | protected: | |
11088 | Expression* | |
11089 | do_copy() | |
11090 | { | |
11091 | return new Fixed_array_construction_expression(this->type(), | |
11092 | (this->vals() == NULL | |
11093 | ? NULL | |
11094 | : this->vals()->copy()), | |
11095 | this->location()); | |
11096 | } | |
11097 | ||
11098 | tree | |
11099 | do_get_tree(Translate_context*); | |
11100 | }; | |
11101 | ||
11102 | // Return a tree for constructing a fixed array. | |
11103 | ||
11104 | tree | |
11105 | Fixed_array_construction_expression::do_get_tree(Translate_context* context) | |
11106 | { | |
11107 | return this->get_constructor_tree(context, | |
11108 | this->type()->get_tree(context->gogo())); | |
11109 | } | |
11110 | ||
11111 | // Construct an open array. | |
11112 | ||
11113 | class Open_array_construction_expression : public Array_construction_expression | |
11114 | { | |
11115 | public: | |
11116 | Open_array_construction_expression(Type* type, Expression_list* vals, | |
11117 | source_location location) | |
11118 | : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION, | |
11119 | type, vals, location) | |
11120 | { | |
11121 | gcc_assert(type->array_type() != NULL | |
11122 | && type->array_type()->length() == NULL); | |
11123 | } | |
11124 | ||
11125 | protected: | |
11126 | // Note that taking the address of an open array literal is invalid. | |
11127 | ||
11128 | Expression* | |
11129 | do_copy() | |
11130 | { | |
11131 | return new Open_array_construction_expression(this->type(), | |
11132 | (this->vals() == NULL | |
11133 | ? NULL | |
11134 | : this->vals()->copy()), | |
11135 | this->location()); | |
11136 | } | |
11137 | ||
11138 | tree | |
11139 | do_get_tree(Translate_context*); | |
11140 | }; | |
11141 | ||
11142 | // Return a tree for constructing an open array. | |
11143 | ||
11144 | tree | |
11145 | Open_array_construction_expression::do_get_tree(Translate_context* context) | |
11146 | { | |
f9c68f17 | 11147 | Array_type* array_type = this->type()->array_type(); |
11148 | if (array_type == NULL) | |
11149 | { | |
11150 | gcc_assert(this->type()->is_error_type()); | |
11151 | return error_mark_node; | |
11152 | } | |
11153 | ||
11154 | Type* element_type = array_type->element_type(); | |
e440a328 | 11155 | tree element_type_tree = element_type->get_tree(context->gogo()); |
3d60812e | 11156 | if (element_type_tree == error_mark_node) |
11157 | return error_mark_node; | |
11158 | ||
e440a328 | 11159 | tree values; |
11160 | tree length_tree; | |
11161 | if (this->vals() == NULL || this->vals()->empty()) | |
11162 | { | |
11163 | // We need to create a unique value. | |
11164 | tree max = size_int(0); | |
11165 | tree constructor_type = build_array_type(element_type_tree, | |
11166 | build_index_type(max)); | |
11167 | if (constructor_type == error_mark_node) | |
11168 | return error_mark_node; | |
11169 | VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1); | |
11170 | constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL); | |
11171 | elt->index = size_int(0); | |
11172 | elt->value = element_type->get_init_tree(context->gogo(), false); | |
11173 | values = build_constructor(constructor_type, vec); | |
11174 | if (TREE_CONSTANT(elt->value)) | |
11175 | TREE_CONSTANT(values) = 1; | |
11176 | length_tree = size_int(0); | |
11177 | } | |
11178 | else | |
11179 | { | |
11180 | tree max = size_int(this->vals()->size() - 1); | |
11181 | tree constructor_type = build_array_type(element_type_tree, | |
11182 | build_index_type(max)); | |
11183 | if (constructor_type == error_mark_node) | |
11184 | return error_mark_node; | |
11185 | values = this->get_constructor_tree(context, constructor_type); | |
11186 | length_tree = size_int(this->vals()->size()); | |
11187 | } | |
11188 | ||
11189 | if (values == error_mark_node) | |
11190 | return error_mark_node; | |
11191 | ||
11192 | bool is_constant_initializer = TREE_CONSTANT(values); | |
d8829beb | 11193 | |
11194 | // We have to copy the initial values into heap memory if we are in | |
11195 | // a function or if the values are not constants. We also have to | |
11196 | // copy them if they may contain pointers in a non-constant context, | |
11197 | // as otherwise the garbage collector won't see them. | |
11198 | bool copy_to_heap = (context->function() != NULL | |
11199 | || !is_constant_initializer | |
11200 | || (element_type->has_pointer() | |
11201 | && !context->is_const())); | |
e440a328 | 11202 | |
11203 | if (is_constant_initializer) | |
11204 | { | |
11205 | tree tmp = build_decl(this->location(), VAR_DECL, | |
11206 | create_tmp_var_name("C"), TREE_TYPE(values)); | |
11207 | DECL_EXTERNAL(tmp) = 0; | |
11208 | TREE_PUBLIC(tmp) = 0; | |
11209 | TREE_STATIC(tmp) = 1; | |
11210 | DECL_ARTIFICIAL(tmp) = 1; | |
d8829beb | 11211 | if (copy_to_heap) |
e440a328 | 11212 | { |
d8829beb | 11213 | // If we are not copying the value to the heap, we will only |
11214 | // initialize the value once, so we can use this directly | |
11215 | // rather than copying it. In that case we can't make it | |
11216 | // read-only, because the program is permitted to change it. | |
e440a328 | 11217 | TREE_READONLY(tmp) = 1; |
11218 | TREE_CONSTANT(tmp) = 1; | |
11219 | } | |
11220 | DECL_INITIAL(tmp) = values; | |
11221 | rest_of_decl_compilation(tmp, 1, 0); | |
11222 | values = tmp; | |
11223 | } | |
11224 | ||
11225 | tree space; | |
11226 | tree set; | |
d8829beb | 11227 | if (!copy_to_heap) |
e440a328 | 11228 | { |
d8829beb | 11229 | // the initializer will only run once. |
e440a328 | 11230 | space = build_fold_addr_expr(values); |
11231 | set = NULL_TREE; | |
11232 | } | |
11233 | else | |
11234 | { | |
11235 | tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values)); | |
11236 | space = context->gogo()->allocate_memory(element_type, memsize, | |
11237 | this->location()); | |
11238 | space = save_expr(space); | |
11239 | ||
11240 | tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space); | |
11241 | tree ref = build_fold_indirect_ref_loc(this->location(), s); | |
11242 | TREE_THIS_NOTRAP(ref) = 1; | |
11243 | set = build2(MODIFY_EXPR, void_type_node, ref, values); | |
11244 | } | |
11245 | ||
11246 | // Build a constructor for the open array. | |
11247 | ||
11248 | tree type_tree = this->type()->get_tree(context->gogo()); | |
3d60812e | 11249 | if (type_tree == error_mark_node) |
11250 | return error_mark_node; | |
e440a328 | 11251 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); |
11252 | ||
11253 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
11254 | ||
11255 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
11256 | tree field = TYPE_FIELDS(type_tree); | |
11257 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); | |
11258 | elt->index = field; | |
11259 | elt->value = fold_convert(TREE_TYPE(field), space); | |
11260 | ||
11261 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
11262 | field = DECL_CHAIN(field); | |
11263 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); | |
11264 | elt->index = field; | |
11265 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
11266 | ||
11267 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
11268 | field = DECL_CHAIN(field); | |
11269 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0); | |
11270 | elt->index = field; | |
11271 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
11272 | ||
11273 | tree constructor = build_constructor(type_tree, init); | |
3d60812e | 11274 | if (constructor == error_mark_node) |
11275 | return error_mark_node; | |
d8829beb | 11276 | if (!copy_to_heap) |
e440a328 | 11277 | TREE_CONSTANT(constructor) = 1; |
11278 | ||
11279 | if (set == NULL_TREE) | |
11280 | return constructor; | |
11281 | else | |
11282 | return build2(COMPOUND_EXPR, type_tree, set, constructor); | |
11283 | } | |
11284 | ||
11285 | // Make a slice composite literal. This is used by the type | |
11286 | // descriptor code. | |
11287 | ||
11288 | Expression* | |
11289 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, | |
11290 | source_location location) | |
11291 | { | |
11292 | gcc_assert(type->is_open_array_type()); | |
11293 | return new Open_array_construction_expression(type, vals, location); | |
11294 | } | |
11295 | ||
11296 | // Construct a map. | |
11297 | ||
11298 | class Map_construction_expression : public Expression | |
11299 | { | |
11300 | public: | |
11301 | Map_construction_expression(Type* type, Expression_list* vals, | |
11302 | source_location location) | |
11303 | : Expression(EXPRESSION_MAP_CONSTRUCTION, location), | |
11304 | type_(type), vals_(vals) | |
11305 | { gcc_assert(vals == NULL || vals->size() % 2 == 0); } | |
11306 | ||
11307 | protected: | |
11308 | int | |
11309 | do_traverse(Traverse* traverse); | |
11310 | ||
11311 | Type* | |
11312 | do_type() | |
11313 | { return this->type_; } | |
11314 | ||
11315 | void | |
11316 | do_determine_type(const Type_context*); | |
11317 | ||
11318 | void | |
11319 | do_check_types(Gogo*); | |
11320 | ||
11321 | Expression* | |
11322 | do_copy() | |
11323 | { | |
11324 | return new Map_construction_expression(this->type_, this->vals_->copy(), | |
11325 | this->location()); | |
11326 | } | |
11327 | ||
11328 | tree | |
11329 | do_get_tree(Translate_context*); | |
11330 | ||
11331 | void | |
11332 | do_export(Export*) const; | |
11333 | ||
11334 | private: | |
11335 | // The type of the map to construct. | |
11336 | Type* type_; | |
11337 | // The list of values. | |
11338 | Expression_list* vals_; | |
11339 | }; | |
11340 | ||
11341 | // Traversal. | |
11342 | ||
11343 | int | |
11344 | Map_construction_expression::do_traverse(Traverse* traverse) | |
11345 | { | |
11346 | if (this->vals_ != NULL | |
11347 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11348 | return TRAVERSE_EXIT; | |
11349 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
11350 | return TRAVERSE_EXIT; | |
11351 | return TRAVERSE_CONTINUE; | |
11352 | } | |
11353 | ||
11354 | // Final type determination. | |
11355 | ||
11356 | void | |
11357 | Map_construction_expression::do_determine_type(const Type_context*) | |
11358 | { | |
11359 | if (this->vals_ == NULL) | |
11360 | return; | |
11361 | ||
11362 | Map_type* mt = this->type_->map_type(); | |
11363 | Type_context key_context(mt->key_type(), false); | |
11364 | Type_context val_context(mt->val_type(), false); | |
11365 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11366 | pv != this->vals_->end(); | |
11367 | ++pv) | |
11368 | { | |
11369 | (*pv)->determine_type(&key_context); | |
11370 | ++pv; | |
11371 | (*pv)->determine_type(&val_context); | |
11372 | } | |
11373 | } | |
11374 | ||
11375 | // Check types. | |
11376 | ||
11377 | void | |
11378 | Map_construction_expression::do_check_types(Gogo*) | |
11379 | { | |
11380 | if (this->vals_ == NULL) | |
11381 | return; | |
11382 | ||
11383 | Map_type* mt = this->type_->map_type(); | |
11384 | int i = 0; | |
11385 | Type* key_type = mt->key_type(); | |
11386 | Type* val_type = mt->val_type(); | |
11387 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11388 | pv != this->vals_->end(); | |
11389 | ++pv, ++i) | |
11390 | { | |
11391 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
11392 | { | |
11393 | error_at((*pv)->location(), | |
11394 | "incompatible type for element %d key in map construction", | |
11395 | i + 1); | |
11396 | this->set_is_error(); | |
11397 | } | |
11398 | ++pv; | |
11399 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
11400 | { | |
11401 | error_at((*pv)->location(), | |
11402 | ("incompatible type for element %d value " | |
11403 | "in map construction"), | |
11404 | i + 1); | |
11405 | this->set_is_error(); | |
11406 | } | |
11407 | } | |
11408 | } | |
11409 | ||
11410 | // Return a tree for constructing a map. | |
11411 | ||
11412 | tree | |
11413 | Map_construction_expression::do_get_tree(Translate_context* context) | |
11414 | { | |
11415 | Gogo* gogo = context->gogo(); | |
11416 | source_location loc = this->location(); | |
11417 | ||
11418 | Map_type* mt = this->type_->map_type(); | |
11419 | ||
11420 | // Build a struct to hold the key and value. | |
11421 | tree struct_type = make_node(RECORD_TYPE); | |
11422 | ||
11423 | Type* key_type = mt->key_type(); | |
11424 | tree id = get_identifier("__key"); | |
5845bde6 | 11425 | tree key_type_tree = key_type->get_tree(gogo); |
11426 | if (key_type_tree == error_mark_node) | |
11427 | return error_mark_node; | |
11428 | tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree); | |
e440a328 | 11429 | DECL_CONTEXT(key_field) = struct_type; |
11430 | TYPE_FIELDS(struct_type) = key_field; | |
11431 | ||
11432 | Type* val_type = mt->val_type(); | |
11433 | id = get_identifier("__val"); | |
5845bde6 | 11434 | tree val_type_tree = val_type->get_tree(gogo); |
11435 | if (val_type_tree == error_mark_node) | |
11436 | return error_mark_node; | |
11437 | tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree); | |
e440a328 | 11438 | DECL_CONTEXT(val_field) = struct_type; |
11439 | DECL_CHAIN(key_field) = val_field; | |
11440 | ||
11441 | layout_type(struct_type); | |
11442 | ||
11443 | bool is_constant = true; | |
11444 | size_t i = 0; | |
11445 | tree valaddr; | |
11446 | tree make_tmp; | |
11447 | ||
11448 | if (this->vals_ == NULL || this->vals_->empty()) | |
11449 | { | |
11450 | valaddr = null_pointer_node; | |
11451 | make_tmp = NULL_TREE; | |
11452 | } | |
11453 | else | |
11454 | { | |
11455 | VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc, | |
11456 | this->vals_->size() / 2); | |
11457 | ||
11458 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11459 | pv != this->vals_->end(); | |
11460 | ++pv, ++i) | |
11461 | { | |
11462 | bool one_is_constant = true; | |
11463 | ||
11464 | VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2); | |
11465 | ||
11466 | constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL); | |
11467 | elt->index = key_field; | |
11468 | tree val_tree = (*pv)->get_tree(context); | |
11469 | elt->value = Expression::convert_for_assignment(context, key_type, | |
11470 | (*pv)->type(), | |
11471 | val_tree, loc); | |
11472 | if (elt->value == error_mark_node) | |
11473 | return error_mark_node; | |
11474 | if (!TREE_CONSTANT(elt->value)) | |
11475 | one_is_constant = false; | |
11476 | ||
11477 | ++pv; | |
11478 | ||
11479 | elt = VEC_quick_push(constructor_elt, one, NULL); | |
11480 | elt->index = val_field; | |
11481 | val_tree = (*pv)->get_tree(context); | |
11482 | elt->value = Expression::convert_for_assignment(context, val_type, | |
11483 | (*pv)->type(), | |
11484 | val_tree, loc); | |
11485 | if (elt->value == error_mark_node) | |
11486 | return error_mark_node; | |
11487 | if (!TREE_CONSTANT(elt->value)) | |
11488 | one_is_constant = false; | |
11489 | ||
11490 | elt = VEC_quick_push(constructor_elt, values, NULL); | |
11491 | elt->index = size_int(i); | |
11492 | elt->value = build_constructor(struct_type, one); | |
11493 | if (one_is_constant) | |
11494 | TREE_CONSTANT(elt->value) = 1; | |
11495 | else | |
11496 | is_constant = false; | |
11497 | } | |
11498 | ||
11499 | tree index_type = build_index_type(size_int(i - 1)); | |
11500 | tree array_type = build_array_type(struct_type, index_type); | |
11501 | tree init = build_constructor(array_type, values); | |
11502 | if (is_constant) | |
11503 | TREE_CONSTANT(init) = 1; | |
11504 | tree tmp; | |
11505 | if (current_function_decl != NULL) | |
11506 | { | |
11507 | tmp = create_tmp_var(array_type, get_name(array_type)); | |
11508 | DECL_INITIAL(tmp) = init; | |
11509 | make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp); | |
11510 | TREE_ADDRESSABLE(tmp) = 1; | |
11511 | } | |
11512 | else | |
11513 | { | |
11514 | tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type); | |
11515 | DECL_EXTERNAL(tmp) = 0; | |
11516 | TREE_PUBLIC(tmp) = 0; | |
11517 | TREE_STATIC(tmp) = 1; | |
11518 | DECL_ARTIFICIAL(tmp) = 1; | |
11519 | if (!TREE_CONSTANT(init)) | |
11520 | make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp, | |
11521 | init); | |
11522 | else | |
11523 | { | |
11524 | TREE_READONLY(tmp) = 1; | |
11525 | TREE_CONSTANT(tmp) = 1; | |
11526 | DECL_INITIAL(tmp) = init; | |
11527 | make_tmp = NULL_TREE; | |
11528 | } | |
11529 | rest_of_decl_compilation(tmp, 1, 0); | |
11530 | } | |
11531 | ||
11532 | valaddr = build_fold_addr_expr(tmp); | |
11533 | } | |
11534 | ||
11535 | tree descriptor = gogo->map_descriptor(mt); | |
11536 | ||
11537 | tree type_tree = this->type_->get_tree(gogo); | |
5845bde6 | 11538 | if (type_tree == error_mark_node) |
11539 | return error_mark_node; | |
e440a328 | 11540 | |
11541 | static tree construct_map_fndecl; | |
11542 | tree call = Gogo::call_builtin(&construct_map_fndecl, | |
11543 | loc, | |
11544 | "__go_construct_map", | |
11545 | 6, | |
11546 | type_tree, | |
11547 | TREE_TYPE(descriptor), | |
11548 | descriptor, | |
11549 | sizetype, | |
11550 | size_int(i), | |
11551 | sizetype, | |
11552 | TYPE_SIZE_UNIT(struct_type), | |
11553 | sizetype, | |
11554 | byte_position(val_field), | |
11555 | sizetype, | |
11556 | TYPE_SIZE_UNIT(TREE_TYPE(val_field)), | |
11557 | const_ptr_type_node, | |
11558 | fold_convert(const_ptr_type_node, valaddr)); | |
5fb82b5e | 11559 | if (call == error_mark_node) |
11560 | return error_mark_node; | |
e440a328 | 11561 | |
11562 | tree ret; | |
11563 | if (make_tmp == NULL) | |
11564 | ret = call; | |
11565 | else | |
11566 | ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call); | |
11567 | return ret; | |
11568 | } | |
11569 | ||
11570 | // Export an array construction. | |
11571 | ||
11572 | void | |
11573 | Map_construction_expression::do_export(Export* exp) const | |
11574 | { | |
11575 | exp->write_c_string("convert("); | |
11576 | exp->write_type(this->type_); | |
11577 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11578 | pv != this->vals_->end(); | |
11579 | ++pv) | |
11580 | { | |
11581 | exp->write_c_string(", "); | |
11582 | (*pv)->export_expression(exp); | |
11583 | } | |
11584 | exp->write_c_string(")"); | |
11585 | } | |
11586 | ||
11587 | // A general composite literal. This is lowered to a type specific | |
11588 | // version. | |
11589 | ||
11590 | class Composite_literal_expression : public Parser_expression | |
11591 | { | |
11592 | public: | |
11593 | Composite_literal_expression(Type* type, int depth, bool has_keys, | |
11594 | Expression_list* vals, source_location location) | |
11595 | : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location), | |
11596 | type_(type), depth_(depth), vals_(vals), has_keys_(has_keys) | |
11597 | { } | |
11598 | ||
11599 | protected: | |
11600 | int | |
11601 | do_traverse(Traverse* traverse); | |
11602 | ||
11603 | Expression* | |
11604 | do_lower(Gogo*, Named_object*, int); | |
11605 | ||
11606 | Expression* | |
11607 | do_copy() | |
11608 | { | |
11609 | return new Composite_literal_expression(this->type_, this->depth_, | |
11610 | this->has_keys_, | |
11611 | (this->vals_ == NULL | |
11612 | ? NULL | |
11613 | : this->vals_->copy()), | |
11614 | this->location()); | |
11615 | } | |
11616 | ||
11617 | private: | |
11618 | Expression* | |
11619 | lower_struct(Type*); | |
11620 | ||
11621 | Expression* | |
11622 | lower_array(Type*); | |
11623 | ||
11624 | Expression* | |
11625 | make_array(Type*, Expression_list*); | |
11626 | ||
11627 | Expression* | |
a287720d | 11628 | lower_map(Gogo*, Named_object*, Type*); |
e440a328 | 11629 | |
11630 | // The type of the composite literal. | |
11631 | Type* type_; | |
11632 | // The depth within a list of composite literals within a composite | |
11633 | // literal, when the type is omitted. | |
11634 | int depth_; | |
11635 | // The values to put in the composite literal. | |
11636 | Expression_list* vals_; | |
11637 | // If this is true, then VALS_ is a list of pairs: a key and a | |
11638 | // value. In an array initializer, a missing key will be NULL. | |
11639 | bool has_keys_; | |
11640 | }; | |
11641 | ||
11642 | // Traversal. | |
11643 | ||
11644 | int | |
11645 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
11646 | { | |
11647 | if (this->vals_ != NULL | |
11648 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11649 | return TRAVERSE_EXIT; | |
11650 | return Type::traverse(this->type_, traverse); | |
11651 | } | |
11652 | ||
11653 | // Lower a generic composite literal into a specific version based on | |
11654 | // the type. | |
11655 | ||
11656 | Expression* | |
a287720d | 11657 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int) |
e440a328 | 11658 | { |
11659 | Type* type = this->type_; | |
11660 | ||
11661 | for (int depth = this->depth_; depth > 0; --depth) | |
11662 | { | |
11663 | if (type->array_type() != NULL) | |
11664 | type = type->array_type()->element_type(); | |
11665 | else if (type->map_type() != NULL) | |
11666 | type = type->map_type()->val_type(); | |
11667 | else | |
11668 | { | |
11669 | if (!type->is_error_type()) | |
11670 | error_at(this->location(), | |
11671 | ("may only omit types within composite literals " | |
11672 | "of slice, array, or map type")); | |
11673 | return Expression::make_error(this->location()); | |
11674 | } | |
11675 | } | |
11676 | ||
11677 | if (type->is_error_type()) | |
11678 | return Expression::make_error(this->location()); | |
11679 | else if (type->struct_type() != NULL) | |
11680 | return this->lower_struct(type); | |
11681 | else if (type->array_type() != NULL) | |
11682 | return this->lower_array(type); | |
11683 | else if (type->map_type() != NULL) | |
a287720d | 11684 | return this->lower_map(gogo, function, type); |
e440a328 | 11685 | else |
11686 | { | |
11687 | error_at(this->location(), | |
11688 | ("expected struct, slice, array, or map type " | |
11689 | "for composite literal")); | |
11690 | return Expression::make_error(this->location()); | |
11691 | } | |
11692 | } | |
11693 | ||
11694 | // Lower a struct composite literal. | |
11695 | ||
11696 | Expression* | |
11697 | Composite_literal_expression::lower_struct(Type* type) | |
11698 | { | |
11699 | source_location location = this->location(); | |
11700 | Struct_type* st = type->struct_type(); | |
11701 | if (this->vals_ == NULL || !this->has_keys_) | |
11702 | return new Struct_construction_expression(type, this->vals_, location); | |
11703 | ||
11704 | size_t field_count = st->field_count(); | |
11705 | std::vector<Expression*> vals(field_count); | |
11706 | Expression_list::const_iterator p = this->vals_->begin(); | |
11707 | while (p != this->vals_->end()) | |
11708 | { | |
11709 | Expression* name_expr = *p; | |
11710 | ||
11711 | ++p; | |
11712 | gcc_assert(p != this->vals_->end()); | |
11713 | Expression* val = *p; | |
11714 | ||
11715 | ++p; | |
11716 | ||
11717 | if (name_expr == NULL) | |
11718 | { | |
11719 | error_at(val->location(), "mixture of field and value initializers"); | |
11720 | return Expression::make_error(location); | |
11721 | } | |
11722 | ||
11723 | bool bad_key = false; | |
11724 | std::string name; | |
11725 | switch (name_expr->classification()) | |
11726 | { | |
11727 | case EXPRESSION_UNKNOWN_REFERENCE: | |
11728 | name = name_expr->unknown_expression()->name(); | |
11729 | break; | |
11730 | ||
11731 | case EXPRESSION_CONST_REFERENCE: | |
11732 | name = static_cast<Const_expression*>(name_expr)->name(); | |
11733 | break; | |
11734 | ||
11735 | case EXPRESSION_TYPE: | |
11736 | { | |
11737 | Type* t = name_expr->type(); | |
11738 | Named_type* nt = t->named_type(); | |
11739 | if (nt == NULL) | |
11740 | bad_key = true; | |
11741 | else | |
11742 | name = nt->name(); | |
11743 | } | |
11744 | break; | |
11745 | ||
11746 | case EXPRESSION_VAR_REFERENCE: | |
11747 | name = name_expr->var_expression()->name(); | |
11748 | break; | |
11749 | ||
11750 | case EXPRESSION_FUNC_REFERENCE: | |
11751 | name = name_expr->func_expression()->name(); | |
11752 | break; | |
11753 | ||
11754 | case EXPRESSION_UNARY: | |
11755 | // If there is a local variable around with the same name as | |
11756 | // the field, and this occurs in the closure, then the | |
11757 | // parser may turn the field reference into an indirection | |
11758 | // through the closure. FIXME: This is a mess. | |
11759 | { | |
11760 | bad_key = true; | |
11761 | Unary_expression* ue = static_cast<Unary_expression*>(name_expr); | |
11762 | if (ue->op() == OPERATOR_MULT) | |
11763 | { | |
11764 | Field_reference_expression* fre = | |
11765 | ue->operand()->field_reference_expression(); | |
11766 | if (fre != NULL) | |
11767 | { | |
11768 | Struct_type* st = | |
11769 | fre->expr()->type()->deref()->struct_type(); | |
11770 | if (st != NULL) | |
11771 | { | |
11772 | const Struct_field* sf = st->field(fre->field_index()); | |
11773 | name = sf->field_name(); | |
11774 | char buf[20]; | |
11775 | snprintf(buf, sizeof buf, "%u", fre->field_index()); | |
11776 | size_t buflen = strlen(buf); | |
11777 | if (name.compare(name.length() - buflen, buflen, buf) | |
11778 | == 0) | |
11779 | { | |
11780 | name = name.substr(0, name.length() - buflen); | |
11781 | bad_key = false; | |
11782 | } | |
11783 | } | |
11784 | } | |
11785 | } | |
11786 | } | |
11787 | break; | |
11788 | ||
11789 | default: | |
11790 | bad_key = true; | |
11791 | break; | |
11792 | } | |
11793 | if (bad_key) | |
11794 | { | |
11795 | error_at(name_expr->location(), "expected struct field name"); | |
11796 | return Expression::make_error(location); | |
11797 | } | |
11798 | ||
11799 | unsigned int index; | |
11800 | const Struct_field* sf = st->find_local_field(name, &index); | |
11801 | if (sf == NULL) | |
11802 | { | |
11803 | error_at(name_expr->location(), "unknown field %qs in %qs", | |
11804 | Gogo::message_name(name).c_str(), | |
11805 | (type->named_type() != NULL | |
11806 | ? type->named_type()->message_name().c_str() | |
11807 | : "unnamed struct")); | |
11808 | return Expression::make_error(location); | |
11809 | } | |
11810 | if (vals[index] != NULL) | |
11811 | { | |
11812 | error_at(name_expr->location(), | |
11813 | "duplicate value for field %qs in %qs", | |
11814 | Gogo::message_name(name).c_str(), | |
11815 | (type->named_type() != NULL | |
11816 | ? type->named_type()->message_name().c_str() | |
11817 | : "unnamed struct")); | |
11818 | return Expression::make_error(location); | |
11819 | } | |
11820 | ||
11821 | vals[index] = val; | |
11822 | } | |
11823 | ||
11824 | Expression_list* list = new Expression_list; | |
11825 | list->reserve(field_count); | |
11826 | for (size_t i = 0; i < field_count; ++i) | |
11827 | list->push_back(vals[i]); | |
11828 | ||
11829 | return new Struct_construction_expression(type, list, location); | |
11830 | } | |
11831 | ||
11832 | // Lower an array composite literal. | |
11833 | ||
11834 | Expression* | |
11835 | Composite_literal_expression::lower_array(Type* type) | |
11836 | { | |
11837 | source_location location = this->location(); | |
11838 | if (this->vals_ == NULL || !this->has_keys_) | |
11839 | return this->make_array(type, this->vals_); | |
11840 | ||
11841 | std::vector<Expression*> vals; | |
11842 | vals.reserve(this->vals_->size()); | |
11843 | unsigned long index = 0; | |
11844 | Expression_list::const_iterator p = this->vals_->begin(); | |
11845 | while (p != this->vals_->end()) | |
11846 | { | |
11847 | Expression* index_expr = *p; | |
11848 | ||
11849 | ++p; | |
11850 | gcc_assert(p != this->vals_->end()); | |
11851 | Expression* val = *p; | |
11852 | ||
11853 | ++p; | |
11854 | ||
11855 | if (index_expr != NULL) | |
11856 | { | |
11857 | mpz_t ival; | |
11858 | mpz_init(ival); | |
11859 | Type* dummy; | |
11860 | if (!index_expr->integer_constant_value(true, ival, &dummy)) | |
11861 | { | |
11862 | mpz_clear(ival); | |
11863 | error_at(index_expr->location(), | |
11864 | "index expression is not integer constant"); | |
11865 | return Expression::make_error(location); | |
11866 | } | |
11867 | if (mpz_sgn(ival) < 0) | |
11868 | { | |
11869 | mpz_clear(ival); | |
11870 | error_at(index_expr->location(), "index expression is negative"); | |
11871 | return Expression::make_error(location); | |
11872 | } | |
11873 | index = mpz_get_ui(ival); | |
11874 | if (mpz_cmp_ui(ival, index) != 0) | |
11875 | { | |
11876 | mpz_clear(ival); | |
11877 | error_at(index_expr->location(), "index value overflow"); | |
11878 | return Expression::make_error(location); | |
11879 | } | |
11880 | mpz_clear(ival); | |
11881 | } | |
11882 | ||
11883 | if (index == vals.size()) | |
11884 | vals.push_back(val); | |
11885 | else | |
11886 | { | |
11887 | if (index > vals.size()) | |
11888 | { | |
11889 | vals.reserve(index + 32); | |
11890 | vals.resize(index + 1, static_cast<Expression*>(NULL)); | |
11891 | } | |
11892 | if (vals[index] != NULL) | |
11893 | { | |
11894 | error_at((index_expr != NULL | |
11895 | ? index_expr->location() | |
11896 | : val->location()), | |
11897 | "duplicate value for index %lu", | |
11898 | index); | |
11899 | return Expression::make_error(location); | |
11900 | } | |
11901 | vals[index] = val; | |
11902 | } | |
11903 | ||
11904 | ++index; | |
11905 | } | |
11906 | ||
11907 | size_t size = vals.size(); | |
11908 | Expression_list* list = new Expression_list; | |
11909 | list->reserve(size); | |
11910 | for (size_t i = 0; i < size; ++i) | |
11911 | list->push_back(vals[i]); | |
11912 | ||
11913 | return this->make_array(type, list); | |
11914 | } | |
11915 | ||
11916 | // Actually build the array composite literal. This handles | |
11917 | // [...]{...}. | |
11918 | ||
11919 | Expression* | |
11920 | Composite_literal_expression::make_array(Type* type, Expression_list* vals) | |
11921 | { | |
11922 | source_location location = this->location(); | |
11923 | Array_type* at = type->array_type(); | |
11924 | if (at->length() != NULL && at->length()->is_nil_expression()) | |
11925 | { | |
11926 | size_t size = vals == NULL ? 0 : vals->size(); | |
11927 | mpz_t vlen; | |
11928 | mpz_init_set_ui(vlen, size); | |
11929 | Expression* elen = Expression::make_integer(&vlen, NULL, location); | |
11930 | mpz_clear(vlen); | |
11931 | at = Type::make_array_type(at->element_type(), elen); | |
11932 | type = at; | |
11933 | } | |
11934 | if (at->length() != NULL) | |
11935 | return new Fixed_array_construction_expression(type, vals, location); | |
11936 | else | |
11937 | return new Open_array_construction_expression(type, vals, location); | |
11938 | } | |
11939 | ||
11940 | // Lower a map composite literal. | |
11941 | ||
11942 | Expression* | |
a287720d | 11943 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
11944 | Type* type) | |
e440a328 | 11945 | { |
11946 | source_location location = this->location(); | |
11947 | if (this->vals_ != NULL) | |
11948 | { | |
11949 | if (!this->has_keys_) | |
11950 | { | |
11951 | error_at(location, "map composite literal must have keys"); | |
11952 | return Expression::make_error(location); | |
11953 | } | |
11954 | ||
a287720d | 11955 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 11956 | p != this->vals_->end(); |
11957 | p += 2) | |
11958 | { | |
11959 | if (*p == NULL) | |
11960 | { | |
11961 | ++p; | |
11962 | error_at((*p)->location(), | |
11963 | "map composite literal must have keys for every value"); | |
11964 | return Expression::make_error(location); | |
11965 | } | |
a287720d | 11966 | // Make sure we have lowered the key; it may not have been |
11967 | // lowered in order to handle keys for struct composite | |
11968 | // literals. Lower it now to get the right error message. | |
11969 | if ((*p)->unknown_expression() != NULL) | |
11970 | { | |
11971 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
11972 | gogo->lower_expression(function, &*p); | |
11973 | gcc_assert((*p)->is_error_expression()); | |
11974 | return Expression::make_error(location); | |
11975 | } | |
e440a328 | 11976 | } |
11977 | } | |
11978 | ||
11979 | return new Map_construction_expression(type, this->vals_, location); | |
11980 | } | |
11981 | ||
11982 | // Make a composite literal expression. | |
11983 | ||
11984 | Expression* | |
11985 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
11986 | Expression_list* vals, | |
11987 | source_location location) | |
11988 | { | |
11989 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
11990 | location); | |
11991 | } | |
11992 | ||
11993 | // Return whether this expression is a composite literal. | |
11994 | ||
11995 | bool | |
11996 | Expression::is_composite_literal() const | |
11997 | { | |
11998 | switch (this->classification_) | |
11999 | { | |
12000 | case EXPRESSION_COMPOSITE_LITERAL: | |
12001 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
12002 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
12003 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
12004 | case EXPRESSION_MAP_CONSTRUCTION: | |
12005 | return true; | |
12006 | default: | |
12007 | return false; | |
12008 | } | |
12009 | } | |
12010 | ||
12011 | // Return whether this expression is a composite literal which is not | |
12012 | // constant. | |
12013 | ||
12014 | bool | |
12015 | Expression::is_nonconstant_composite_literal() const | |
12016 | { | |
12017 | switch (this->classification_) | |
12018 | { | |
12019 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
12020 | { | |
12021 | const Struct_construction_expression *psce = | |
12022 | static_cast<const Struct_construction_expression*>(this); | |
12023 | return !psce->is_constant_struct(); | |
12024 | } | |
12025 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
12026 | { | |
12027 | const Fixed_array_construction_expression *pace = | |
12028 | static_cast<const Fixed_array_construction_expression*>(this); | |
12029 | return !pace->is_constant_array(); | |
12030 | } | |
12031 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
12032 | { | |
12033 | const Open_array_construction_expression *pace = | |
12034 | static_cast<const Open_array_construction_expression*>(this); | |
12035 | return !pace->is_constant_array(); | |
12036 | } | |
12037 | case EXPRESSION_MAP_CONSTRUCTION: | |
12038 | return true; | |
12039 | default: | |
12040 | return false; | |
12041 | } | |
12042 | } | |
12043 | ||
12044 | // Return true if this is a reference to a local variable. | |
12045 | ||
12046 | bool | |
12047 | Expression::is_local_variable() const | |
12048 | { | |
12049 | const Var_expression* ve = this->var_expression(); | |
12050 | if (ve == NULL) | |
12051 | return false; | |
12052 | const Named_object* no = ve->named_object(); | |
12053 | return (no->is_result_variable() | |
12054 | || (no->is_variable() && !no->var_value()->is_global())); | |
12055 | } | |
12056 | ||
12057 | // Class Type_guard_expression. | |
12058 | ||
12059 | // Traversal. | |
12060 | ||
12061 | int | |
12062 | Type_guard_expression::do_traverse(Traverse* traverse) | |
12063 | { | |
12064 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
12065 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12066 | return TRAVERSE_EXIT; | |
12067 | return TRAVERSE_CONTINUE; | |
12068 | } | |
12069 | ||
12070 | // Check types of a type guard expression. The expression must have | |
12071 | // an interface type, but the actual type conversion is checked at run | |
12072 | // time. | |
12073 | ||
12074 | void | |
12075 | Type_guard_expression::do_check_types(Gogo*) | |
12076 | { | |
12077 | // 6g permits using a type guard with unsafe.pointer; we are | |
12078 | // compatible. | |
12079 | Type* expr_type = this->expr_->type(); | |
12080 | if (expr_type->is_unsafe_pointer_type()) | |
12081 | { | |
12082 | if (this->type_->points_to() == NULL | |
12083 | && (this->type_->integer_type() == NULL | |
12084 | || (this->type_->forwarded() | |
12085 | != Type::lookup_integer_type("uintptr")))) | |
12086 | this->report_error(_("invalid unsafe.Pointer conversion")); | |
12087 | } | |
12088 | else if (this->type_->is_unsafe_pointer_type()) | |
12089 | { | |
12090 | if (expr_type->points_to() == NULL | |
12091 | && (expr_type->integer_type() == NULL | |
12092 | || (expr_type->forwarded() | |
12093 | != Type::lookup_integer_type("uintptr")))) | |
12094 | this->report_error(_("invalid unsafe.Pointer conversion")); | |
12095 | } | |
12096 | else if (expr_type->interface_type() == NULL) | |
f725ade8 | 12097 | { |
12098 | if (!expr_type->is_error_type() && !this->type_->is_error_type()) | |
12099 | this->report_error(_("type assertion only valid for interface types")); | |
12100 | this->set_is_error(); | |
12101 | } | |
e440a328 | 12102 | else if (this->type_->interface_type() == NULL) |
12103 | { | |
12104 | std::string reason; | |
12105 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
12106 | &reason)) | |
12107 | { | |
f725ade8 | 12108 | if (!this->type_->is_error_type()) |
e440a328 | 12109 | { |
f725ade8 | 12110 | if (reason.empty()) |
12111 | this->report_error(_("impossible type assertion: " | |
12112 | "type does not implement interface")); | |
12113 | else | |
12114 | error_at(this->location(), | |
12115 | ("impossible type assertion: " | |
12116 | "type does not implement interface (%s)"), | |
12117 | reason.c_str()); | |
e440a328 | 12118 | } |
f725ade8 | 12119 | this->set_is_error(); |
e440a328 | 12120 | } |
12121 | } | |
12122 | } | |
12123 | ||
12124 | // Return a tree for a type guard expression. | |
12125 | ||
12126 | tree | |
12127 | Type_guard_expression::do_get_tree(Translate_context* context) | |
12128 | { | |
12129 | Gogo* gogo = context->gogo(); | |
12130 | tree expr_tree = this->expr_->get_tree(context); | |
12131 | if (expr_tree == error_mark_node) | |
12132 | return error_mark_node; | |
12133 | Type* expr_type = this->expr_->type(); | |
12134 | if ((this->type_->is_unsafe_pointer_type() | |
12135 | && (expr_type->points_to() != NULL | |
12136 | || expr_type->integer_type() != NULL)) | |
12137 | || (expr_type->is_unsafe_pointer_type() | |
12138 | && this->type_->points_to() != NULL)) | |
12139 | return convert_to_pointer(this->type_->get_tree(gogo), expr_tree); | |
12140 | else if (expr_type->is_unsafe_pointer_type() | |
12141 | && this->type_->integer_type() != NULL) | |
12142 | return convert_to_integer(this->type_->get_tree(gogo), expr_tree); | |
12143 | else if (this->type_->interface_type() != NULL) | |
12144 | return Expression::convert_interface_to_interface(context, this->type_, | |
12145 | this->expr_->type(), | |
12146 | expr_tree, true, | |
12147 | this->location()); | |
12148 | else | |
12149 | return Expression::convert_for_assignment(context, this->type_, | |
12150 | this->expr_->type(), expr_tree, | |
12151 | this->location()); | |
12152 | } | |
12153 | ||
12154 | // Make a type guard expression. | |
12155 | ||
12156 | Expression* | |
12157 | Expression::make_type_guard(Expression* expr, Type* type, | |
12158 | source_location location) | |
12159 | { | |
12160 | return new Type_guard_expression(expr, type, location); | |
12161 | } | |
12162 | ||
12163 | // Class Heap_composite_expression. | |
12164 | ||
12165 | // When you take the address of a composite literal, it is allocated | |
12166 | // on the heap. This class implements that. | |
12167 | ||
12168 | class Heap_composite_expression : public Expression | |
12169 | { | |
12170 | public: | |
12171 | Heap_composite_expression(Expression* expr, source_location location) | |
12172 | : Expression(EXPRESSION_HEAP_COMPOSITE, location), | |
12173 | expr_(expr) | |
12174 | { } | |
12175 | ||
12176 | protected: | |
12177 | int | |
12178 | do_traverse(Traverse* traverse) | |
12179 | { return Expression::traverse(&this->expr_, traverse); } | |
12180 | ||
12181 | Type* | |
12182 | do_type() | |
12183 | { return Type::make_pointer_type(this->expr_->type()); } | |
12184 | ||
12185 | void | |
12186 | do_determine_type(const Type_context*) | |
12187 | { this->expr_->determine_type_no_context(); } | |
12188 | ||
12189 | Expression* | |
12190 | do_copy() | |
12191 | { | |
12192 | return Expression::make_heap_composite(this->expr_->copy(), | |
12193 | this->location()); | |
12194 | } | |
12195 | ||
12196 | tree | |
12197 | do_get_tree(Translate_context*); | |
12198 | ||
12199 | // We only export global objects, and the parser does not generate | |
12200 | // this in global scope. | |
12201 | void | |
12202 | do_export(Export*) const | |
12203 | { gcc_unreachable(); } | |
12204 | ||
12205 | private: | |
12206 | // The composite literal which is being put on the heap. | |
12207 | Expression* expr_; | |
12208 | }; | |
12209 | ||
12210 | // Return a tree which allocates a composite literal on the heap. | |
12211 | ||
12212 | tree | |
12213 | Heap_composite_expression::do_get_tree(Translate_context* context) | |
12214 | { | |
12215 | tree expr_tree = this->expr_->get_tree(context); | |
12216 | if (expr_tree == error_mark_node) | |
12217 | return error_mark_node; | |
12218 | tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree)); | |
12219 | gcc_assert(TREE_CODE(expr_size) == INTEGER_CST); | |
12220 | tree space = context->gogo()->allocate_memory(this->expr_->type(), | |
12221 | expr_size, this->location()); | |
12222 | space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space); | |
12223 | space = save_expr(space); | |
12224 | tree ref = build_fold_indirect_ref_loc(this->location(), space); | |
12225 | TREE_THIS_NOTRAP(ref) = 1; | |
12226 | tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space), | |
12227 | build2(MODIFY_EXPR, void_type_node, ref, expr_tree), | |
12228 | space); | |
12229 | SET_EXPR_LOCATION(ret, this->location()); | |
12230 | return ret; | |
12231 | } | |
12232 | ||
12233 | // Allocate a composite literal on the heap. | |
12234 | ||
12235 | Expression* | |
12236 | Expression::make_heap_composite(Expression* expr, source_location location) | |
12237 | { | |
12238 | return new Heap_composite_expression(expr, location); | |
12239 | } | |
12240 | ||
12241 | // Class Receive_expression. | |
12242 | ||
12243 | // Return the type of a receive expression. | |
12244 | ||
12245 | Type* | |
12246 | Receive_expression::do_type() | |
12247 | { | |
12248 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
12249 | if (channel_type == NULL) | |
12250 | return Type::make_error_type(); | |
12251 | return channel_type->element_type(); | |
12252 | } | |
12253 | ||
12254 | // Check types for a receive expression. | |
12255 | ||
12256 | void | |
12257 | Receive_expression::do_check_types(Gogo*) | |
12258 | { | |
12259 | Type* type = this->channel_->type(); | |
12260 | if (type->is_error_type()) | |
12261 | { | |
12262 | this->set_is_error(); | |
12263 | return; | |
12264 | } | |
12265 | if (type->channel_type() == NULL) | |
12266 | { | |
12267 | this->report_error(_("expected channel")); | |
12268 | return; | |
12269 | } | |
12270 | if (!type->channel_type()->may_receive()) | |
12271 | { | |
12272 | this->report_error(_("invalid receive on send-only channel")); | |
12273 | return; | |
12274 | } | |
12275 | } | |
12276 | ||
12277 | // Get a tree for a receive expression. | |
12278 | ||
12279 | tree | |
12280 | Receive_expression::do_get_tree(Translate_context* context) | |
12281 | { | |
12282 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
5b8368f4 | 12283 | if (channel_type == NULL) |
12284 | { | |
12285 | gcc_assert(this->channel_->type()->is_error_type()); | |
12286 | return error_mark_node; | |
12287 | } | |
e440a328 | 12288 | Type* element_type = channel_type->element_type(); |
12289 | tree element_type_tree = element_type->get_tree(context->gogo()); | |
12290 | ||
12291 | tree channel = this->channel_->get_tree(context); | |
12292 | if (element_type_tree == error_mark_node || channel == error_mark_node) | |
12293 | return error_mark_node; | |
12294 | ||
12295 | return Gogo::receive_from_channel(element_type_tree, channel, | |
12296 | this->for_select_, this->location()); | |
12297 | } | |
12298 | ||
12299 | // Make a receive expression. | |
12300 | ||
12301 | Receive_expression* | |
12302 | Expression::make_receive(Expression* channel, source_location location) | |
12303 | { | |
12304 | return new Receive_expression(channel, location); | |
12305 | } | |
12306 | ||
12307 | // Class Send_expression. | |
12308 | ||
12309 | // Traversal. | |
12310 | ||
12311 | int | |
12312 | Send_expression::do_traverse(Traverse* traverse) | |
12313 | { | |
12314 | if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT) | |
12315 | return TRAVERSE_EXIT; | |
12316 | return Expression::traverse(&this->val_, traverse); | |
12317 | } | |
12318 | ||
12319 | // Get the type. | |
12320 | ||
12321 | Type* | |
12322 | Send_expression::do_type() | |
12323 | { | |
ee041e33 | 12324 | if (this->is_value_discarded_) |
12325 | return Type::make_void_type(); | |
12326 | else | |
12327 | return Type::lookup_bool_type(); | |
e440a328 | 12328 | } |
12329 | ||
12330 | // Set types. | |
12331 | ||
12332 | void | |
12333 | Send_expression::do_determine_type(const Type_context*) | |
12334 | { | |
12335 | this->channel_->determine_type_no_context(); | |
12336 | ||
12337 | Type* type = this->channel_->type(); | |
12338 | Type_context subcontext; | |
12339 | if (type->channel_type() != NULL) | |
12340 | subcontext.type = type->channel_type()->element_type(); | |
12341 | this->val_->determine_type(&subcontext); | |
12342 | } | |
12343 | ||
12344 | // Check types. | |
12345 | ||
12346 | void | |
12347 | Send_expression::do_check_types(Gogo*) | |
12348 | { | |
12349 | Type* type = this->channel_->type(); | |
12350 | if (type->is_error_type()) | |
12351 | { | |
12352 | this->set_is_error(); | |
12353 | return; | |
12354 | } | |
12355 | Channel_type* channel_type = type->channel_type(); | |
12356 | if (channel_type == NULL) | |
12357 | { | |
12358 | error_at(this->location(), "left operand of %<<-%> must be channel"); | |
12359 | this->set_is_error(); | |
12360 | return; | |
12361 | } | |
12362 | Type* element_type = channel_type->element_type(); | |
12363 | if (element_type != NULL | |
12364 | && !Type::are_assignable(element_type, this->val_->type(), NULL)) | |
12365 | { | |
12366 | this->report_error(_("incompatible types in send")); | |
12367 | return; | |
12368 | } | |
12369 | if (!channel_type->may_send()) | |
12370 | { | |
12371 | this->report_error(_("invalid send on receive-only channel")); | |
12372 | return; | |
12373 | } | |
12374 | } | |
12375 | ||
12376 | // Get a tree for a send expression. | |
12377 | ||
12378 | tree | |
12379 | Send_expression::do_get_tree(Translate_context* context) | |
12380 | { | |
12381 | tree channel = this->channel_->get_tree(context); | |
12382 | tree val = this->val_->get_tree(context); | |
12383 | if (channel == error_mark_node || val == error_mark_node) | |
12384 | return error_mark_node; | |
12385 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
12386 | val = Expression::convert_for_assignment(context, | |
12387 | channel_type->element_type(), | |
12388 | this->val_->type(), | |
12389 | val, | |
12390 | this->location()); | |
12391 | return Gogo::send_on_channel(channel, val, this->is_value_discarded_, | |
12392 | this->for_select_, this->location()); | |
12393 | } | |
12394 | ||
12395 | // Make a send expression | |
12396 | ||
12397 | Send_expression* | |
12398 | Expression::make_send(Expression* channel, Expression* val, | |
12399 | source_location location) | |
12400 | { | |
12401 | return new Send_expression(channel, val, location); | |
12402 | } | |
12403 | ||
12404 | // An expression which evaluates to a pointer to the type descriptor | |
12405 | // of a type. | |
12406 | ||
12407 | class Type_descriptor_expression : public Expression | |
12408 | { | |
12409 | public: | |
12410 | Type_descriptor_expression(Type* type, source_location location) | |
12411 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), | |
12412 | type_(type) | |
12413 | { } | |
12414 | ||
12415 | protected: | |
12416 | Type* | |
12417 | do_type() | |
12418 | { return Type::make_type_descriptor_ptr_type(); } | |
12419 | ||
12420 | void | |
12421 | do_determine_type(const Type_context*) | |
12422 | { } | |
12423 | ||
12424 | Expression* | |
12425 | do_copy() | |
12426 | { return this; } | |
12427 | ||
12428 | tree | |
12429 | do_get_tree(Translate_context* context) | |
12430 | { return this->type_->type_descriptor_pointer(context->gogo()); } | |
12431 | ||
12432 | private: | |
12433 | // The type for which this is the descriptor. | |
12434 | Type* type_; | |
12435 | }; | |
12436 | ||
12437 | // Make a type descriptor expression. | |
12438 | ||
12439 | Expression* | |
12440 | Expression::make_type_descriptor(Type* type, source_location location) | |
12441 | { | |
12442 | return new Type_descriptor_expression(type, location); | |
12443 | } | |
12444 | ||
12445 | // An expression which evaluates to some characteristic of a type. | |
12446 | // This is only used to initialize fields of a type descriptor. Using | |
12447 | // a new expression class is slightly inefficient but gives us a good | |
12448 | // separation between the frontend and the middle-end with regard to | |
12449 | // how types are laid out. | |
12450 | ||
12451 | class Type_info_expression : public Expression | |
12452 | { | |
12453 | public: | |
12454 | Type_info_expression(Type* type, Type_info type_info) | |
12455 | : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION), | |
12456 | type_(type), type_info_(type_info) | |
12457 | { } | |
12458 | ||
12459 | protected: | |
12460 | Type* | |
12461 | do_type(); | |
12462 | ||
12463 | void | |
12464 | do_determine_type(const Type_context*) | |
12465 | { } | |
12466 | ||
12467 | Expression* | |
12468 | do_copy() | |
12469 | { return this; } | |
12470 | ||
12471 | tree | |
12472 | do_get_tree(Translate_context* context); | |
12473 | ||
12474 | private: | |
12475 | // The type for which we are getting information. | |
12476 | Type* type_; | |
12477 | // What information we want. | |
12478 | Type_info type_info_; | |
12479 | }; | |
12480 | ||
12481 | // The type is chosen to match what the type descriptor struct | |
12482 | // expects. | |
12483 | ||
12484 | Type* | |
12485 | Type_info_expression::do_type() | |
12486 | { | |
12487 | switch (this->type_info_) | |
12488 | { | |
12489 | case TYPE_INFO_SIZE: | |
12490 | return Type::lookup_integer_type("uintptr"); | |
12491 | case TYPE_INFO_ALIGNMENT: | |
12492 | case TYPE_INFO_FIELD_ALIGNMENT: | |
12493 | return Type::lookup_integer_type("uint8"); | |
12494 | default: | |
12495 | gcc_unreachable(); | |
12496 | } | |
12497 | } | |
12498 | ||
12499 | // Return type information in GENERIC. | |
12500 | ||
12501 | tree | |
12502 | Type_info_expression::do_get_tree(Translate_context* context) | |
12503 | { | |
12504 | tree type_tree = this->type_->get_tree(context->gogo()); | |
12505 | if (type_tree == error_mark_node) | |
12506 | return error_mark_node; | |
12507 | ||
12508 | tree val_type_tree = this->type()->get_tree(context->gogo()); | |
12509 | gcc_assert(val_type_tree != error_mark_node); | |
12510 | ||
12511 | if (this->type_info_ == TYPE_INFO_SIZE) | |
12512 | return fold_convert_loc(BUILTINS_LOCATION, val_type_tree, | |
12513 | TYPE_SIZE_UNIT(type_tree)); | |
12514 | else | |
12515 | { | |
637bd3af | 12516 | unsigned int val; |
e440a328 | 12517 | if (this->type_info_ == TYPE_INFO_ALIGNMENT) |
637bd3af | 12518 | val = go_type_alignment(type_tree); |
e440a328 | 12519 | else |
637bd3af | 12520 | val = go_field_alignment(type_tree); |
e440a328 | 12521 | return build_int_cstu(val_type_tree, val); |
12522 | } | |
12523 | } | |
12524 | ||
12525 | // Make a type info expression. | |
12526 | ||
12527 | Expression* | |
12528 | Expression::make_type_info(Type* type, Type_info type_info) | |
12529 | { | |
12530 | return new Type_info_expression(type, type_info); | |
12531 | } | |
12532 | ||
12533 | // An expression which evaluates to the offset of a field within a | |
12534 | // struct. This, like Type_info_expression, q.v., is only used to | |
12535 | // initialize fields of a type descriptor. | |
12536 | ||
12537 | class Struct_field_offset_expression : public Expression | |
12538 | { | |
12539 | public: | |
12540 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
12541 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION), | |
12542 | type_(type), field_(field) | |
12543 | { } | |
12544 | ||
12545 | protected: | |
12546 | Type* | |
12547 | do_type() | |
12548 | { return Type::lookup_integer_type("uintptr"); } | |
12549 | ||
12550 | void | |
12551 | do_determine_type(const Type_context*) | |
12552 | { } | |
12553 | ||
12554 | Expression* | |
12555 | do_copy() | |
12556 | { return this; } | |
12557 | ||
12558 | tree | |
12559 | do_get_tree(Translate_context* context); | |
12560 | ||
12561 | private: | |
12562 | // The type of the struct. | |
12563 | Struct_type* type_; | |
12564 | // The field. | |
12565 | const Struct_field* field_; | |
12566 | }; | |
12567 | ||
12568 | // Return a struct field offset in GENERIC. | |
12569 | ||
12570 | tree | |
12571 | Struct_field_offset_expression::do_get_tree(Translate_context* context) | |
12572 | { | |
12573 | tree type_tree = this->type_->get_tree(context->gogo()); | |
12574 | if (type_tree == error_mark_node) | |
12575 | return error_mark_node; | |
12576 | ||
12577 | tree val_type_tree = this->type()->get_tree(context->gogo()); | |
12578 | gcc_assert(val_type_tree != error_mark_node); | |
12579 | ||
12580 | const Struct_field_list* fields = this->type_->fields(); | |
12581 | tree struct_field_tree = TYPE_FIELDS(type_tree); | |
12582 | Struct_field_list::const_iterator p; | |
12583 | for (p = fields->begin(); | |
12584 | p != fields->end(); | |
12585 | ++p, struct_field_tree = DECL_CHAIN(struct_field_tree)) | |
12586 | { | |
12587 | gcc_assert(struct_field_tree != NULL_TREE); | |
12588 | if (&*p == this->field_) | |
12589 | break; | |
12590 | } | |
12591 | gcc_assert(&*p == this->field_); | |
12592 | ||
12593 | return fold_convert_loc(BUILTINS_LOCATION, val_type_tree, | |
12594 | byte_position(struct_field_tree)); | |
12595 | } | |
12596 | ||
12597 | // Make an expression for a struct field offset. | |
12598 | ||
12599 | Expression* | |
12600 | Expression::make_struct_field_offset(Struct_type* type, | |
12601 | const Struct_field* field) | |
12602 | { | |
12603 | return new Struct_field_offset_expression(type, field); | |
12604 | } | |
12605 | ||
12606 | // An expression which evaluates to the address of an unnamed label. | |
12607 | ||
12608 | class Label_addr_expression : public Expression | |
12609 | { | |
12610 | public: | |
12611 | Label_addr_expression(Label* label, source_location location) | |
12612 | : Expression(EXPRESSION_LABEL_ADDR, location), | |
12613 | label_(label) | |
12614 | { } | |
12615 | ||
12616 | protected: | |
12617 | Type* | |
12618 | do_type() | |
12619 | { return Type::make_pointer_type(Type::make_void_type()); } | |
12620 | ||
12621 | void | |
12622 | do_determine_type(const Type_context*) | |
12623 | { } | |
12624 | ||
12625 | Expression* | |
12626 | do_copy() | |
12627 | { return new Label_addr_expression(this->label_, this->location()); } | |
12628 | ||
12629 | tree | |
12630 | do_get_tree(Translate_context*) | |
12631 | { return this->label_->get_addr(this->location()); } | |
12632 | ||
12633 | private: | |
12634 | // The label whose address we are taking. | |
12635 | Label* label_; | |
12636 | }; | |
12637 | ||
12638 | // Make an expression for the address of an unnamed label. | |
12639 | ||
12640 | Expression* | |
12641 | Expression::make_label_addr(Label* label, source_location location) | |
12642 | { | |
12643 | return new Label_addr_expression(label, location); | |
12644 | } | |
12645 | ||
12646 | // Import an expression. This comes at the end in order to see the | |
12647 | // various class definitions. | |
12648 | ||
12649 | Expression* | |
12650 | Expression::import_expression(Import* imp) | |
12651 | { | |
12652 | int c = imp->peek_char(); | |
12653 | if (imp->match_c_string("- ") | |
12654 | || imp->match_c_string("! ") | |
12655 | || imp->match_c_string("^ ")) | |
12656 | return Unary_expression::do_import(imp); | |
12657 | else if (c == '(') | |
12658 | return Binary_expression::do_import(imp); | |
12659 | else if (imp->match_c_string("true") | |
12660 | || imp->match_c_string("false")) | |
12661 | return Boolean_expression::do_import(imp); | |
12662 | else if (c == '"') | |
12663 | return String_expression::do_import(imp); | |
12664 | else if (c == '-' || (c >= '0' && c <= '9')) | |
12665 | { | |
12666 | // This handles integers, floats and complex constants. | |
12667 | return Integer_expression::do_import(imp); | |
12668 | } | |
12669 | else if (imp->match_c_string("nil")) | |
12670 | return Nil_expression::do_import(imp); | |
12671 | else if (imp->match_c_string("convert")) | |
12672 | return Type_conversion_expression::do_import(imp); | |
12673 | else | |
12674 | { | |
12675 | error_at(imp->location(), "import error: expected expression"); | |
12676 | return Expression::make_error(imp->location()); | |
12677 | } | |
12678 | } | |
12679 | ||
12680 | // Class Expression_list. | |
12681 | ||
12682 | // Traverse the list. | |
12683 | ||
12684 | int | |
12685 | Expression_list::traverse(Traverse* traverse) | |
12686 | { | |
12687 | for (Expression_list::iterator p = this->begin(); | |
12688 | p != this->end(); | |
12689 | ++p) | |
12690 | { | |
12691 | if (*p != NULL) | |
12692 | { | |
12693 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
12694 | return TRAVERSE_EXIT; | |
12695 | } | |
12696 | } | |
12697 | return TRAVERSE_CONTINUE; | |
12698 | } | |
12699 | ||
12700 | // Copy the list. | |
12701 | ||
12702 | Expression_list* | |
12703 | Expression_list::copy() | |
12704 | { | |
12705 | Expression_list* ret = new Expression_list(); | |
12706 | for (Expression_list::iterator p = this->begin(); | |
12707 | p != this->end(); | |
12708 | ++p) | |
12709 | { | |
12710 | if (*p == NULL) | |
12711 | ret->push_back(NULL); | |
12712 | else | |
12713 | ret->push_back((*p)->copy()); | |
12714 | } | |
12715 | return ret; | |
12716 | } | |
12717 | ||
12718 | // Return whether an expression list has an error expression. | |
12719 | ||
12720 | bool | |
12721 | Expression_list::contains_error() const | |
12722 | { | |
12723 | for (Expression_list::const_iterator p = this->begin(); | |
12724 | p != this->end(); | |
12725 | ++p) | |
12726 | if (*p != NULL && (*p)->is_error_expression()) | |
12727 | return true; | |
12728 | return false; | |
12729 | } |