]>
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 | } | |
365 | ||
366 | // Start building a constructor for the value we will return. | |
367 | ||
368 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2); | |
369 | ||
370 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
371 | tree field = TYPE_FIELDS(lhs_type_tree); | |
372 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
373 | (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0); | |
374 | elt->index = field; | |
375 | elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value); | |
376 | ||
377 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
378 | field = DECL_CHAIN(field); | |
379 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); | |
380 | elt->index = field; | |
381 | ||
382 | if (rhs_type->points_to() != NULL) | |
383 | { | |
384 | // We are assigning a pointer to the interface; the interface | |
385 | // holds the pointer itself. | |
386 | elt->value = rhs_tree; | |
387 | return build_constructor(lhs_type_tree, init); | |
388 | } | |
389 | ||
390 | // We are assigning a non-pointer value to the interface; the | |
391 | // interface gets a copy of the value in the heap. | |
392 | ||
393 | tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree)); | |
394 | ||
395 | tree space = gogo->allocate_memory(rhs_type, object_size, location); | |
396 | space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)), | |
397 | space); | |
398 | space = save_expr(space); | |
399 | ||
400 | tree ref = build_fold_indirect_ref_loc(location, space); | |
401 | TREE_THIS_NOTRAP(ref) = 1; | |
402 | tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node, | |
403 | ref, rhs_tree); | |
404 | ||
405 | elt->value = fold_convert_loc(location, TREE_TYPE(field), space); | |
406 | ||
407 | return build2(COMPOUND_EXPR, lhs_type_tree, set, | |
408 | build_constructor(lhs_type_tree, init)); | |
409 | } | |
410 | ||
411 | // Return a tree for the type descriptor of RHS_TREE, which has | |
412 | // interface type RHS_TYPE. If RHS_TREE is nil the result will be | |
413 | // NULL. | |
414 | ||
415 | tree | |
416 | Expression::get_interface_type_descriptor(Translate_context*, | |
417 | Type* rhs_type, tree rhs_tree, | |
418 | source_location location) | |
419 | { | |
420 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
421 | gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); | |
422 | tree rhs_field = TYPE_FIELDS(rhs_type_tree); | |
423 | tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
424 | NULL_TREE); | |
425 | if (rhs_type->interface_type()->is_empty()) | |
426 | { | |
427 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), | |
428 | "__type_descriptor") == 0); | |
429 | return v; | |
430 | } | |
431 | ||
432 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods") | |
433 | == 0); | |
434 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(v))); | |
435 | v = save_expr(v); | |
436 | tree v1 = build_fold_indirect_ref_loc(location, v); | |
437 | gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE); | |
438 | tree f = TYPE_FIELDS(TREE_TYPE(v1)); | |
439 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor") | |
440 | == 0); | |
441 | v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE); | |
442 | ||
443 | tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v, | |
444 | fold_convert_loc(location, TREE_TYPE(v), | |
445 | null_pointer_node)); | |
446 | tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node); | |
447 | return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1), | |
448 | eq, n, v1); | |
449 | } | |
450 | ||
451 | // Return a tree for the conversion of an interface type to an | |
452 | // interface type. | |
453 | ||
454 | tree | |
455 | Expression::convert_interface_to_interface(Translate_context* context, | |
456 | Type *lhs_type, Type *rhs_type, | |
457 | tree rhs_tree, bool for_type_guard, | |
458 | source_location location) | |
459 | { | |
460 | Gogo* gogo = context->gogo(); | |
461 | Interface_type* lhs_interface_type = lhs_type->interface_type(); | |
462 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
463 | ||
464 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
465 | if (lhs_type_tree == error_mark_node) | |
466 | return error_mark_node; | |
467 | ||
468 | // In the general case this requires runtime examination of the type | |
469 | // method table to match it up with the interface methods. | |
470 | ||
471 | // FIXME: If all of the methods in the right hand side interface | |
472 | // also appear in the left hand side interface, then we don't need | |
473 | // to do a runtime check, although we still need to build a new | |
474 | // method table. | |
475 | ||
476 | // Get the type descriptor for the right hand side. This will be | |
477 | // NULL for a nil interface. | |
478 | ||
479 | if (!DECL_P(rhs_tree)) | |
480 | rhs_tree = save_expr(rhs_tree); | |
481 | ||
482 | tree rhs_type_descriptor = | |
483 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
484 | location); | |
485 | ||
486 | // The result is going to be a two element constructor. | |
487 | ||
488 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2); | |
489 | ||
490 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
491 | tree field = TYPE_FIELDS(lhs_type_tree); | |
492 | elt->index = field; | |
493 | ||
494 | if (for_type_guard) | |
495 | { | |
496 | // A type assertion fails when converting a nil interface. | |
497 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo); | |
498 | static tree assert_interface_decl; | |
499 | tree call = Gogo::call_builtin(&assert_interface_decl, | |
500 | location, | |
501 | "__go_assert_interface", | |
502 | 2, | |
503 | ptr_type_node, | |
504 | TREE_TYPE(lhs_type_descriptor), | |
505 | lhs_type_descriptor, | |
506 | TREE_TYPE(rhs_type_descriptor), | |
507 | rhs_type_descriptor); | |
508 | // This will panic if the interface conversion fails. | |
509 | TREE_NOTHROW(assert_interface_decl) = 0; | |
510 | elt->value = fold_convert_loc(location, TREE_TYPE(field), call); | |
511 | } | |
512 | else if (lhs_is_empty) | |
513 | { | |
514 | // A convertion to an empty interface always succeeds, and the | |
515 | // first field is just the type descriptor of the object. | |
516 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
517 | "__type_descriptor") == 0); | |
518 | gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor)); | |
519 | elt->value = rhs_type_descriptor; | |
520 | } | |
521 | else | |
522 | { | |
523 | // A conversion to a non-empty interface may fail, but unlike a | |
524 | // type assertion converting nil will always succeed. | |
525 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") | |
526 | == 0); | |
527 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo); | |
528 | static tree convert_interface_decl; | |
529 | tree call = Gogo::call_builtin(&convert_interface_decl, | |
530 | location, | |
531 | "__go_convert_interface", | |
532 | 2, | |
533 | ptr_type_node, | |
534 | TREE_TYPE(lhs_type_descriptor), | |
535 | lhs_type_descriptor, | |
536 | TREE_TYPE(rhs_type_descriptor), | |
537 | rhs_type_descriptor); | |
538 | // This will panic if the interface conversion fails. | |
539 | TREE_NOTHROW(convert_interface_decl) = 0; | |
540 | elt->value = fold_convert_loc(location, TREE_TYPE(field), call); | |
541 | } | |
542 | ||
543 | // The second field is simply the object pointer. | |
544 | ||
545 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
546 | field = DECL_CHAIN(field); | |
547 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); | |
548 | elt->index = field; | |
549 | ||
550 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
551 | gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); | |
552 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); | |
553 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); | |
554 | elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
555 | NULL_TREE); | |
556 | ||
557 | return build_constructor(lhs_type_tree, init); | |
558 | } | |
559 | ||
560 | // Return a tree for the conversion of an interface type to a | |
561 | // non-interface type. | |
562 | ||
563 | tree | |
564 | Expression::convert_interface_to_type(Translate_context* context, | |
565 | Type *lhs_type, Type* rhs_type, | |
566 | tree rhs_tree, source_location location) | |
567 | { | |
568 | Gogo* gogo = context->gogo(); | |
569 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
570 | ||
571 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
572 | if (lhs_type_tree == error_mark_node) | |
573 | return error_mark_node; | |
574 | ||
575 | // Call a function to check that the type is valid. The function | |
576 | // will panic with an appropriate runtime type error if the type is | |
577 | // not valid. | |
578 | ||
579 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo); | |
580 | ||
581 | if (!DECL_P(rhs_tree)) | |
582 | rhs_tree = save_expr(rhs_tree); | |
583 | ||
584 | tree rhs_type_descriptor = | |
585 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
586 | location); | |
587 | ||
588 | tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo); | |
589 | ||
590 | static tree check_interface_type_decl; | |
591 | tree call = Gogo::call_builtin(&check_interface_type_decl, | |
592 | location, | |
593 | "__go_check_interface_type", | |
594 | 3, | |
595 | void_type_node, | |
596 | TREE_TYPE(lhs_type_descriptor), | |
597 | lhs_type_descriptor, | |
598 | TREE_TYPE(rhs_type_descriptor), | |
599 | rhs_type_descriptor, | |
600 | TREE_TYPE(rhs_inter_descriptor), | |
601 | rhs_inter_descriptor); | |
602 | // This call will panic if the conversion is invalid. | |
603 | TREE_NOTHROW(check_interface_type_decl) = 0; | |
604 | ||
605 | // If the call succeeds, pull out the value. | |
606 | gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); | |
607 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); | |
608 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); | |
609 | tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
610 | NULL_TREE); | |
611 | ||
612 | // If the value is a pointer, then it is the value we want. | |
613 | // Otherwise it points to the value. | |
614 | if (lhs_type->points_to() == NULL) | |
615 | { | |
616 | val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val); | |
617 | val = build_fold_indirect_ref_loc(location, val); | |
618 | } | |
619 | ||
620 | return build2(COMPOUND_EXPR, lhs_type_tree, call, | |
621 | fold_convert_loc(location, lhs_type_tree, val)); | |
622 | } | |
623 | ||
624 | // Convert an expression to a tree. This is implemented by the child | |
625 | // class. Not that it is not in general safe to call this multiple | |
626 | // times for a single expression, but that we don't catch such errors. | |
627 | ||
628 | tree | |
629 | Expression::get_tree(Translate_context* context) | |
630 | { | |
631 | // The child may have marked this expression as having an error. | |
632 | if (this->classification_ == EXPRESSION_ERROR) | |
633 | return error_mark_node; | |
634 | ||
635 | return this->do_get_tree(context); | |
636 | } | |
637 | ||
638 | // Return a tree for VAL in TYPE. | |
639 | ||
640 | tree | |
641 | Expression::integer_constant_tree(mpz_t val, tree type) | |
642 | { | |
643 | if (type == error_mark_node) | |
644 | return error_mark_node; | |
645 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
646 | return double_int_to_tree(type, | |
647 | mpz_get_double_int(type, val, true)); | |
648 | else if (TREE_CODE(type) == REAL_TYPE) | |
649 | { | |
650 | mpfr_t fval; | |
651 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
652 | tree ret = Expression::float_constant_tree(fval, type); | |
653 | mpfr_clear(fval); | |
654 | return ret; | |
655 | } | |
656 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
657 | { | |
658 | mpfr_t fval; | |
659 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
660 | tree real = Expression::float_constant_tree(fval, TREE_TYPE(type)); | |
661 | mpfr_clear(fval); | |
662 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
663 | integer_zero_node); | |
664 | return build_complex(type, real, imag); | |
665 | } | |
666 | else | |
667 | gcc_unreachable(); | |
668 | } | |
669 | ||
670 | // Return a tree for VAL in TYPE. | |
671 | ||
672 | tree | |
673 | Expression::float_constant_tree(mpfr_t val, tree type) | |
674 | { | |
675 | if (type == error_mark_node) | |
676 | return error_mark_node; | |
677 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
678 | { | |
679 | mpz_t ival; | |
680 | mpz_init(ival); | |
681 | mpfr_get_z(ival, val, GMP_RNDN); | |
682 | tree ret = Expression::integer_constant_tree(ival, type); | |
683 | mpz_clear(ival); | |
684 | return ret; | |
685 | } | |
686 | else if (TREE_CODE(type) == REAL_TYPE) | |
687 | { | |
688 | REAL_VALUE_TYPE r1; | |
689 | real_from_mpfr(&r1, val, type, GMP_RNDN); | |
690 | REAL_VALUE_TYPE r2; | |
691 | real_convert(&r2, TYPE_MODE(type), &r1); | |
692 | return build_real(type, r2); | |
693 | } | |
694 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
695 | { | |
696 | REAL_VALUE_TYPE r1; | |
697 | real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN); | |
698 | REAL_VALUE_TYPE r2; | |
699 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
700 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
701 | integer_zero_node); | |
702 | return build_complex(type, build_real(TREE_TYPE(type), r2), imag); | |
703 | } | |
704 | else | |
705 | gcc_unreachable(); | |
706 | } | |
707 | ||
708 | // Return a tree for REAL/IMAG in TYPE. | |
709 | ||
710 | tree | |
711 | Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type) | |
712 | { | |
713 | if (TREE_CODE(type) == COMPLEX_TYPE) | |
714 | { | |
715 | REAL_VALUE_TYPE r1; | |
716 | real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN); | |
717 | REAL_VALUE_TYPE r2; | |
718 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
719 | ||
720 | REAL_VALUE_TYPE r3; | |
721 | real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN); | |
722 | REAL_VALUE_TYPE r4; | |
723 | real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3); | |
724 | ||
725 | return build_complex(type, build_real(TREE_TYPE(type), r2), | |
726 | build_real(TREE_TYPE(type), r4)); | |
727 | } | |
728 | else | |
729 | gcc_unreachable(); | |
730 | } | |
731 | ||
732 | // Return a tree which evaluates to true if VAL, of arbitrary integer | |
733 | // type, is negative or is more than the maximum value of BOUND_TYPE. | |
734 | // If SOFAR is not NULL, it is or'red into the result. The return | |
735 | // value may be NULL if SOFAR is NULL. | |
736 | ||
737 | tree | |
738 | Expression::check_bounds(tree val, tree bound_type, tree sofar, | |
739 | source_location loc) | |
740 | { | |
741 | tree val_type = TREE_TYPE(val); | |
742 | tree ret = NULL_TREE; | |
743 | ||
744 | if (!TYPE_UNSIGNED(val_type)) | |
745 | { | |
746 | ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val, | |
747 | build_int_cst(val_type, 0)); | |
748 | if (ret == boolean_false_node) | |
749 | ret = NULL_TREE; | |
750 | } | |
751 | ||
752 | if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type)) | |
753 | || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type)) | |
754 | { | |
755 | tree max = TYPE_MAX_VALUE(bound_type); | |
756 | tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val, | |
757 | fold_convert_loc(loc, val_type, max)); | |
758 | if (big == boolean_false_node) | |
759 | ; | |
760 | else if (ret == NULL_TREE) | |
761 | ret = big; | |
762 | else | |
763 | ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
764 | ret, big); | |
765 | } | |
766 | ||
767 | if (ret == NULL_TREE) | |
768 | return sofar; | |
769 | else if (sofar == NULL_TREE) | |
770 | return ret; | |
771 | else | |
772 | return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
773 | sofar, ret); | |
774 | } | |
775 | ||
776 | // Error expressions. This are used to avoid cascading errors. | |
777 | ||
778 | class Error_expression : public Expression | |
779 | { | |
780 | public: | |
781 | Error_expression(source_location location) | |
782 | : Expression(EXPRESSION_ERROR, location) | |
783 | { } | |
784 | ||
785 | protected: | |
786 | bool | |
787 | do_is_constant() const | |
788 | { return true; } | |
789 | ||
790 | bool | |
791 | do_integer_constant_value(bool, mpz_t val, Type**) const | |
792 | { | |
793 | mpz_set_ui(val, 0); | |
794 | return true; | |
795 | } | |
796 | ||
797 | bool | |
798 | do_float_constant_value(mpfr_t val, Type**) const | |
799 | { | |
800 | mpfr_set_ui(val, 0, GMP_RNDN); | |
801 | return true; | |
802 | } | |
803 | ||
804 | bool | |
805 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const | |
806 | { | |
807 | mpfr_set_ui(real, 0, GMP_RNDN); | |
808 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
809 | return true; | |
810 | } | |
811 | ||
812 | void | |
813 | do_discarding_value() | |
814 | { } | |
815 | ||
816 | Type* | |
817 | do_type() | |
818 | { return Type::make_error_type(); } | |
819 | ||
820 | void | |
821 | do_determine_type(const Type_context*) | |
822 | { } | |
823 | ||
824 | Expression* | |
825 | do_copy() | |
826 | { return this; } | |
827 | ||
828 | bool | |
829 | do_is_addressable() const | |
830 | { return true; } | |
831 | ||
832 | tree | |
833 | do_get_tree(Translate_context*) | |
834 | { return error_mark_node; } | |
835 | }; | |
836 | ||
837 | Expression* | |
838 | Expression::make_error(source_location location) | |
839 | { | |
840 | return new Error_expression(location); | |
841 | } | |
842 | ||
843 | // An expression which is really a type. This is used during parsing. | |
844 | // It is an error if these survive after lowering. | |
845 | ||
846 | class | |
847 | Type_expression : public Expression | |
848 | { | |
849 | public: | |
850 | Type_expression(Type* type, source_location location) | |
851 | : Expression(EXPRESSION_TYPE, location), | |
852 | type_(type) | |
853 | { } | |
854 | ||
855 | protected: | |
856 | int | |
857 | do_traverse(Traverse* traverse) | |
858 | { return Type::traverse(this->type_, traverse); } | |
859 | ||
860 | Type* | |
861 | do_type() | |
862 | { return this->type_; } | |
863 | ||
864 | void | |
865 | do_determine_type(const Type_context*) | |
866 | { } | |
867 | ||
868 | void | |
869 | do_check_types(Gogo*) | |
870 | { this->report_error(_("invalid use of type")); } | |
871 | ||
872 | Expression* | |
873 | do_copy() | |
874 | { return this; } | |
875 | ||
876 | tree | |
877 | do_get_tree(Translate_context*) | |
878 | { gcc_unreachable(); } | |
879 | ||
880 | private: | |
881 | // The type which we are representing as an expression. | |
882 | Type* type_; | |
883 | }; | |
884 | ||
885 | Expression* | |
886 | Expression::make_type(Type* type, source_location location) | |
887 | { | |
888 | return new Type_expression(type, location); | |
889 | } | |
890 | ||
891 | // Class Var_expression. | |
892 | ||
893 | // Lower a variable expression. Here we just make sure that the | |
894 | // initialization expression of the variable has been lowered. This | |
895 | // ensures that we will be able to determine the type of the variable | |
896 | // if necessary. | |
897 | ||
898 | Expression* | |
899 | Var_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
900 | { | |
901 | if (this->variable_->is_variable()) | |
902 | { | |
903 | Variable* var = this->variable_->var_value(); | |
904 | // This is either a local variable or a global variable. A | |
905 | // reference to a variable which is local to an enclosing | |
906 | // function will be a reference to a field in a closure. | |
907 | if (var->is_global()) | |
908 | function = NULL; | |
909 | var->lower_init_expression(gogo, function); | |
910 | } | |
911 | return this; | |
912 | } | |
913 | ||
914 | // Return the name of the variable. | |
915 | ||
916 | const std::string& | |
917 | Var_expression::name() const | |
918 | { | |
919 | return this->variable_->name(); | |
920 | } | |
921 | ||
922 | // Return the type of a reference to a variable. | |
923 | ||
924 | Type* | |
925 | Var_expression::do_type() | |
926 | { | |
927 | if (this->variable_->is_variable()) | |
928 | return this->variable_->var_value()->type(); | |
929 | else if (this->variable_->is_result_variable()) | |
930 | return this->variable_->result_var_value()->type(); | |
931 | else | |
932 | gcc_unreachable(); | |
933 | } | |
934 | ||
935 | // Something takes the address of this variable. This means that we | |
936 | // may want to move the variable onto the heap. | |
937 | ||
938 | void | |
939 | Var_expression::do_address_taken(bool escapes) | |
940 | { | |
941 | if (!escapes) | |
942 | ; | |
943 | else if (this->variable_->is_variable()) | |
944 | this->variable_->var_value()->set_address_taken(); | |
945 | else if (this->variable_->is_result_variable()) | |
946 | this->variable_->result_var_value()->set_address_taken(); | |
947 | else | |
948 | gcc_unreachable(); | |
949 | } | |
950 | ||
951 | // Get the tree for a reference to a variable. | |
952 | ||
953 | tree | |
954 | Var_expression::do_get_tree(Translate_context* context) | |
955 | { | |
956 | return this->variable_->get_tree(context->gogo(), context->function()); | |
957 | } | |
958 | ||
959 | // Make a reference to a variable in an expression. | |
960 | ||
961 | Expression* | |
962 | Expression::make_var_reference(Named_object* var, source_location location) | |
963 | { | |
964 | if (var->is_sink()) | |
965 | return Expression::make_sink(location); | |
966 | ||
967 | // FIXME: Creating a new object for each reference to a variable is | |
968 | // wasteful. | |
969 | return new Var_expression(var, location); | |
970 | } | |
971 | ||
972 | // Class Temporary_reference_expression. | |
973 | ||
974 | // The type. | |
975 | ||
976 | Type* | |
977 | Temporary_reference_expression::do_type() | |
978 | { | |
979 | return this->statement_->type(); | |
980 | } | |
981 | ||
982 | // Called if something takes the address of this temporary variable. | |
983 | // We never have to move temporary variables to the heap, but we do | |
984 | // need to know that they must live in the stack rather than in a | |
985 | // register. | |
986 | ||
987 | void | |
988 | Temporary_reference_expression::do_address_taken(bool) | |
989 | { | |
990 | this->statement_->set_is_address_taken(); | |
991 | } | |
992 | ||
993 | // Get a tree referring to the variable. | |
994 | ||
995 | tree | |
996 | Temporary_reference_expression::do_get_tree(Translate_context*) | |
997 | { | |
998 | return this->statement_->get_decl(); | |
999 | } | |
1000 | ||
1001 | // Make a reference to a temporary variable. | |
1002 | ||
1003 | Expression* | |
1004 | Expression::make_temporary_reference(Temporary_statement* statement, | |
1005 | source_location location) | |
1006 | { | |
1007 | return new Temporary_reference_expression(statement, location); | |
1008 | } | |
1009 | ||
1010 | // A sink expression--a use of the blank identifier _. | |
1011 | ||
1012 | class Sink_expression : public Expression | |
1013 | { | |
1014 | public: | |
1015 | Sink_expression(source_location location) | |
1016 | : Expression(EXPRESSION_SINK, location), | |
1017 | type_(NULL), var_(NULL_TREE) | |
1018 | { } | |
1019 | ||
1020 | protected: | |
1021 | void | |
1022 | do_discarding_value() | |
1023 | { } | |
1024 | ||
1025 | Type* | |
1026 | do_type(); | |
1027 | ||
1028 | void | |
1029 | do_determine_type(const Type_context*); | |
1030 | ||
1031 | Expression* | |
1032 | do_copy() | |
1033 | { return new Sink_expression(this->location()); } | |
1034 | ||
1035 | tree | |
1036 | do_get_tree(Translate_context*); | |
1037 | ||
1038 | private: | |
1039 | // The type of this sink variable. | |
1040 | Type* type_; | |
1041 | // The temporary variable we generate. | |
1042 | tree var_; | |
1043 | }; | |
1044 | ||
1045 | // Return the type of a sink expression. | |
1046 | ||
1047 | Type* | |
1048 | Sink_expression::do_type() | |
1049 | { | |
1050 | if (this->type_ == NULL) | |
1051 | return Type::make_sink_type(); | |
1052 | return this->type_; | |
1053 | } | |
1054 | ||
1055 | // Determine the type of a sink expression. | |
1056 | ||
1057 | void | |
1058 | Sink_expression::do_determine_type(const Type_context* context) | |
1059 | { | |
1060 | if (context->type != NULL) | |
1061 | this->type_ = context->type; | |
1062 | } | |
1063 | ||
1064 | // Return a temporary variable for a sink expression. This will | |
1065 | // presumably be a write-only variable which the middle-end will drop. | |
1066 | ||
1067 | tree | |
1068 | Sink_expression::do_get_tree(Translate_context* context) | |
1069 | { | |
1070 | if (this->var_ == NULL_TREE) | |
1071 | { | |
1072 | gcc_assert(this->type_ != NULL && !this->type_->is_sink_type()); | |
1073 | this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()), | |
1074 | "blank"); | |
1075 | } | |
1076 | return this->var_; | |
1077 | } | |
1078 | ||
1079 | // Make a sink expression. | |
1080 | ||
1081 | Expression* | |
1082 | Expression::make_sink(source_location location) | |
1083 | { | |
1084 | return new Sink_expression(location); | |
1085 | } | |
1086 | ||
1087 | // Class Func_expression. | |
1088 | ||
1089 | // FIXME: Can a function expression appear in a constant expression? | |
1090 | // The value is unchanging. Initializing a constant to the address of | |
1091 | // a function seems like it could work, though there might be little | |
1092 | // point to it. | |
1093 | ||
1094 | // Return the name of the function. | |
1095 | ||
1096 | const std::string& | |
1097 | Func_expression::name() const | |
1098 | { | |
1099 | return this->function_->name(); | |
1100 | } | |
1101 | ||
1102 | // Traversal. | |
1103 | ||
1104 | int | |
1105 | Func_expression::do_traverse(Traverse* traverse) | |
1106 | { | |
1107 | return (this->closure_ == NULL | |
1108 | ? TRAVERSE_CONTINUE | |
1109 | : Expression::traverse(&this->closure_, traverse)); | |
1110 | } | |
1111 | ||
1112 | // Return the type of a function expression. | |
1113 | ||
1114 | Type* | |
1115 | Func_expression::do_type() | |
1116 | { | |
1117 | if (this->function_->is_function()) | |
1118 | return this->function_->func_value()->type(); | |
1119 | else if (this->function_->is_function_declaration()) | |
1120 | return this->function_->func_declaration_value()->type(); | |
1121 | else | |
1122 | gcc_unreachable(); | |
1123 | } | |
1124 | ||
1125 | // Get the tree for a function expression without evaluating the | |
1126 | // closure. | |
1127 | ||
1128 | tree | |
1129 | Func_expression::get_tree_without_closure(Gogo* gogo) | |
1130 | { | |
1131 | Function_type* fntype; | |
1132 | if (this->function_->is_function()) | |
1133 | fntype = this->function_->func_value()->type(); | |
1134 | else if (this->function_->is_function_declaration()) | |
1135 | fntype = this->function_->func_declaration_value()->type(); | |
1136 | else | |
1137 | gcc_unreachable(); | |
1138 | ||
1139 | // Builtin functions are handled specially by Call_expression. We | |
1140 | // can't take their address. | |
1141 | if (fntype->is_builtin()) | |
1142 | { | |
1143 | error_at(this->location(), "invalid use of special builtin function %qs", | |
1144 | this->function_->name().c_str()); | |
1145 | return error_mark_node; | |
1146 | } | |
1147 | ||
1148 | Named_object* no = this->function_; | |
9d6f3721 | 1149 | |
1150 | tree id = no->get_id(gogo); | |
1151 | if (id == error_mark_node) | |
1152 | return error_mark_node; | |
1153 | ||
e440a328 | 1154 | tree fndecl; |
1155 | if (no->is_function()) | |
1156 | fndecl = no->func_value()->get_or_make_decl(gogo, no, id); | |
1157 | else if (no->is_function_declaration()) | |
1158 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id); | |
1159 | else | |
1160 | gcc_unreachable(); | |
1161 | ||
9d6f3721 | 1162 | if (fndecl == error_mark_node) |
1163 | return error_mark_node; | |
1164 | ||
e440a328 | 1165 | return build_fold_addr_expr_loc(this->location(), fndecl); |
1166 | } | |
1167 | ||
1168 | // Get the tree for a function expression. This is used when we take | |
1169 | // the address of a function rather than simply calling it. If the | |
1170 | // function has a closure, we must use a trampoline. | |
1171 | ||
1172 | tree | |
1173 | Func_expression::do_get_tree(Translate_context* context) | |
1174 | { | |
1175 | Gogo* gogo = context->gogo(); | |
1176 | ||
1177 | tree fnaddr = this->get_tree_without_closure(gogo); | |
1178 | if (fnaddr == error_mark_node) | |
1179 | return error_mark_node; | |
1180 | ||
1181 | gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR | |
1182 | && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL); | |
1183 | TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1; | |
1184 | ||
1185 | // For a normal non-nested function call, that is all we have to do. | |
1186 | if (!this->function_->is_function() | |
1187 | || this->function_->func_value()->enclosing() == NULL) | |
1188 | { | |
1189 | gcc_assert(this->closure_ == NULL); | |
1190 | return fnaddr; | |
1191 | } | |
1192 | ||
1193 | // For a nested function call, we have to always allocate a | |
1194 | // trampoline. If we don't always allocate, then closures will not | |
1195 | // be reliably distinct. | |
1196 | Expression* closure = this->closure_; | |
1197 | tree closure_tree; | |
1198 | if (closure == NULL) | |
1199 | closure_tree = null_pointer_node; | |
1200 | else | |
1201 | { | |
1202 | // Get the value of the closure. This will be a pointer to | |
1203 | // space allocated on the heap. | |
1204 | closure_tree = closure->get_tree(context); | |
1205 | if (closure_tree == error_mark_node) | |
1206 | return error_mark_node; | |
1207 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree))); | |
1208 | } | |
1209 | ||
1210 | // Now we need to build some code on the heap. This code will load | |
1211 | // the static chain pointer with the closure and then jump to the | |
1212 | // body of the function. The normal gcc approach is to build the | |
1213 | // code on the stack. Unfortunately we can not do that, as Go | |
1214 | // permits us to return the function pointer. | |
1215 | ||
1216 | return gogo->make_trampoline(fnaddr, closure_tree, this->location()); | |
1217 | } | |
1218 | ||
1219 | // Make a reference to a function in an expression. | |
1220 | ||
1221 | Expression* | |
1222 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
1223 | source_location location) | |
1224 | { | |
1225 | return new Func_expression(function, closure, location); | |
1226 | } | |
1227 | ||
1228 | // Class Unknown_expression. | |
1229 | ||
1230 | // Return the name of an unknown expression. | |
1231 | ||
1232 | const std::string& | |
1233 | Unknown_expression::name() const | |
1234 | { | |
1235 | return this->named_object_->name(); | |
1236 | } | |
1237 | ||
1238 | // Lower a reference to an unknown name. | |
1239 | ||
1240 | Expression* | |
1241 | Unknown_expression::do_lower(Gogo*, Named_object*, int) | |
1242 | { | |
1243 | source_location location = this->location(); | |
1244 | Named_object* no = this->named_object_; | |
1245 | Named_object* real = no->unknown_value()->real_named_object(); | |
1246 | if (real == NULL) | |
1247 | { | |
1248 | if (this->is_composite_literal_key_) | |
1249 | return this; | |
1250 | error_at(location, "reference to undefined name %qs", | |
1251 | this->named_object_->message_name().c_str()); | |
1252 | return Expression::make_error(location); | |
1253 | } | |
1254 | switch (real->classification()) | |
1255 | { | |
1256 | case Named_object::NAMED_OBJECT_CONST: | |
1257 | return Expression::make_const_reference(real, location); | |
1258 | case Named_object::NAMED_OBJECT_TYPE: | |
1259 | return Expression::make_type(real->type_value(), location); | |
1260 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1261 | if (this->is_composite_literal_key_) | |
1262 | return this; | |
1263 | error_at(location, "reference to undefined type %qs", | |
1264 | real->message_name().c_str()); | |
1265 | return Expression::make_error(location); | |
1266 | case Named_object::NAMED_OBJECT_VAR: | |
1267 | return Expression::make_var_reference(real, location); | |
1268 | case Named_object::NAMED_OBJECT_FUNC: | |
1269 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1270 | return Expression::make_func_reference(real, NULL, location); | |
1271 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1272 | if (this->is_composite_literal_key_) | |
1273 | return this; | |
1274 | error_at(location, "unexpected reference to package"); | |
1275 | return Expression::make_error(location); | |
1276 | default: | |
1277 | gcc_unreachable(); | |
1278 | } | |
1279 | } | |
1280 | ||
1281 | // Make a reference to an unknown name. | |
1282 | ||
1283 | Expression* | |
1284 | Expression::make_unknown_reference(Named_object* no, source_location location) | |
1285 | { | |
1286 | gcc_assert(no->resolve()->is_unknown()); | |
1287 | return new Unknown_expression(no, location); | |
1288 | } | |
1289 | ||
1290 | // A boolean expression. | |
1291 | ||
1292 | class Boolean_expression : public Expression | |
1293 | { | |
1294 | public: | |
1295 | Boolean_expression(bool val, source_location location) | |
1296 | : Expression(EXPRESSION_BOOLEAN, location), | |
1297 | val_(val), type_(NULL) | |
1298 | { } | |
1299 | ||
1300 | static Expression* | |
1301 | do_import(Import*); | |
1302 | ||
1303 | protected: | |
1304 | bool | |
1305 | do_is_constant() const | |
1306 | { return true; } | |
1307 | ||
1308 | Type* | |
1309 | do_type(); | |
1310 | ||
1311 | void | |
1312 | do_determine_type(const Type_context*); | |
1313 | ||
1314 | Expression* | |
1315 | do_copy() | |
1316 | { return this; } | |
1317 | ||
1318 | tree | |
1319 | do_get_tree(Translate_context*) | |
1320 | { return this->val_ ? boolean_true_node : boolean_false_node; } | |
1321 | ||
1322 | void | |
1323 | do_export(Export* exp) const | |
1324 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1325 | ||
1326 | private: | |
1327 | // The constant. | |
1328 | bool val_; | |
1329 | // The type as determined by context. | |
1330 | Type* type_; | |
1331 | }; | |
1332 | ||
1333 | // Get the type. | |
1334 | ||
1335 | Type* | |
1336 | Boolean_expression::do_type() | |
1337 | { | |
1338 | if (this->type_ == NULL) | |
1339 | this->type_ = Type::make_boolean_type(); | |
1340 | return this->type_; | |
1341 | } | |
1342 | ||
1343 | // Set the type from the context. | |
1344 | ||
1345 | void | |
1346 | Boolean_expression::do_determine_type(const Type_context* context) | |
1347 | { | |
1348 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1349 | ; | |
1350 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1351 | this->type_ = context->type; | |
1352 | else if (!context->may_be_abstract) | |
1353 | this->type_ = Type::lookup_bool_type(); | |
1354 | } | |
1355 | ||
1356 | // Import a boolean constant. | |
1357 | ||
1358 | Expression* | |
1359 | Boolean_expression::do_import(Import* imp) | |
1360 | { | |
1361 | if (imp->peek_char() == 't') | |
1362 | { | |
1363 | imp->require_c_string("true"); | |
1364 | return Expression::make_boolean(true, imp->location()); | |
1365 | } | |
1366 | else | |
1367 | { | |
1368 | imp->require_c_string("false"); | |
1369 | return Expression::make_boolean(false, imp->location()); | |
1370 | } | |
1371 | } | |
1372 | ||
1373 | // Make a boolean expression. | |
1374 | ||
1375 | Expression* | |
1376 | Expression::make_boolean(bool val, source_location location) | |
1377 | { | |
1378 | return new Boolean_expression(val, location); | |
1379 | } | |
1380 | ||
1381 | // Class String_expression. | |
1382 | ||
1383 | // Get the type. | |
1384 | ||
1385 | Type* | |
1386 | String_expression::do_type() | |
1387 | { | |
1388 | if (this->type_ == NULL) | |
1389 | this->type_ = Type::make_string_type(); | |
1390 | return this->type_; | |
1391 | } | |
1392 | ||
1393 | // Set the type from the context. | |
1394 | ||
1395 | void | |
1396 | String_expression::do_determine_type(const Type_context* context) | |
1397 | { | |
1398 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1399 | ; | |
1400 | else if (context->type != NULL && context->type->is_string_type()) | |
1401 | this->type_ = context->type; | |
1402 | else if (!context->may_be_abstract) | |
1403 | this->type_ = Type::lookup_string_type(); | |
1404 | } | |
1405 | ||
1406 | // Build a string constant. | |
1407 | ||
1408 | tree | |
1409 | String_expression::do_get_tree(Translate_context* context) | |
1410 | { | |
1411 | return context->gogo()->go_string_constant_tree(this->val_); | |
1412 | } | |
1413 | ||
1414 | // Export a string expression. | |
1415 | ||
1416 | void | |
1417 | String_expression::do_export(Export* exp) const | |
1418 | { | |
1419 | std::string s; | |
1420 | s.reserve(this->val_.length() * 4 + 2); | |
1421 | s += '"'; | |
1422 | for (std::string::const_iterator p = this->val_.begin(); | |
1423 | p != this->val_.end(); | |
1424 | ++p) | |
1425 | { | |
1426 | if (*p == '\\' || *p == '"') | |
1427 | { | |
1428 | s += '\\'; | |
1429 | s += *p; | |
1430 | } | |
1431 | else if (*p >= 0x20 && *p < 0x7f) | |
1432 | s += *p; | |
1433 | else if (*p == '\n') | |
1434 | s += "\\n"; | |
1435 | else if (*p == '\t') | |
1436 | s += "\\t"; | |
1437 | else | |
1438 | { | |
1439 | s += "\\x"; | |
1440 | unsigned char c = *p; | |
1441 | unsigned int dig = c >> 4; | |
1442 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1443 | dig = c & 0xf; | |
1444 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1445 | } | |
1446 | } | |
1447 | s += '"'; | |
1448 | exp->write_string(s); | |
1449 | } | |
1450 | ||
1451 | // Import a string expression. | |
1452 | ||
1453 | Expression* | |
1454 | String_expression::do_import(Import* imp) | |
1455 | { | |
1456 | imp->require_c_string("\""); | |
1457 | std::string val; | |
1458 | while (true) | |
1459 | { | |
1460 | int c = imp->get_char(); | |
1461 | if (c == '"' || c == -1) | |
1462 | break; | |
1463 | if (c != '\\') | |
1464 | val += static_cast<char>(c); | |
1465 | else | |
1466 | { | |
1467 | c = imp->get_char(); | |
1468 | if (c == '\\' || c == '"') | |
1469 | val += static_cast<char>(c); | |
1470 | else if (c == 'n') | |
1471 | val += '\n'; | |
1472 | else if (c == 't') | |
1473 | val += '\t'; | |
1474 | else if (c == 'x') | |
1475 | { | |
1476 | c = imp->get_char(); | |
1477 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1478 | c = imp->get_char(); | |
1479 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1480 | char v = (vh << 4) | vl; | |
1481 | val += v; | |
1482 | } | |
1483 | else | |
1484 | { | |
1485 | error_at(imp->location(), "bad string constant"); | |
1486 | return Expression::make_error(imp->location()); | |
1487 | } | |
1488 | } | |
1489 | } | |
1490 | return Expression::make_string(val, imp->location()); | |
1491 | } | |
1492 | ||
1493 | // Make a string expression. | |
1494 | ||
1495 | Expression* | |
1496 | Expression::make_string(const std::string& val, source_location location) | |
1497 | { | |
1498 | return new String_expression(val, location); | |
1499 | } | |
1500 | ||
1501 | // Make an integer expression. | |
1502 | ||
1503 | class Integer_expression : public Expression | |
1504 | { | |
1505 | public: | |
1506 | Integer_expression(const mpz_t* val, Type* type, source_location location) | |
1507 | : Expression(EXPRESSION_INTEGER, location), | |
1508 | type_(type) | |
1509 | { mpz_init_set(this->val_, *val); } | |
1510 | ||
1511 | static Expression* | |
1512 | do_import(Import*); | |
1513 | ||
1514 | // Return whether VAL fits in the type. | |
1515 | static bool | |
1516 | check_constant(mpz_t val, Type*, source_location); | |
1517 | ||
1518 | // Write VAL to export data. | |
1519 | static void | |
1520 | export_integer(Export* exp, const mpz_t val); | |
1521 | ||
1522 | protected: | |
1523 | bool | |
1524 | do_is_constant() const | |
1525 | { return true; } | |
1526 | ||
1527 | bool | |
1528 | do_integer_constant_value(bool, mpz_t val, Type** ptype) const; | |
1529 | ||
1530 | Type* | |
1531 | do_type(); | |
1532 | ||
1533 | void | |
1534 | do_determine_type(const Type_context* context); | |
1535 | ||
1536 | void | |
1537 | do_check_types(Gogo*); | |
1538 | ||
1539 | tree | |
1540 | do_get_tree(Translate_context*); | |
1541 | ||
1542 | Expression* | |
1543 | do_copy() | |
1544 | { return Expression::make_integer(&this->val_, this->type_, | |
1545 | this->location()); } | |
1546 | ||
1547 | void | |
1548 | do_export(Export*) const; | |
1549 | ||
1550 | private: | |
1551 | // The integer value. | |
1552 | mpz_t val_; | |
1553 | // The type so far. | |
1554 | Type* type_; | |
1555 | }; | |
1556 | ||
1557 | // Return an integer constant value. | |
1558 | ||
1559 | bool | |
1560 | Integer_expression::do_integer_constant_value(bool, mpz_t val, | |
1561 | Type** ptype) const | |
1562 | { | |
1563 | if (this->type_ != NULL) | |
1564 | *ptype = this->type_; | |
1565 | mpz_set(val, this->val_); | |
1566 | return true; | |
1567 | } | |
1568 | ||
1569 | // Return the current type. If we haven't set the type yet, we return | |
1570 | // an abstract integer type. | |
1571 | ||
1572 | Type* | |
1573 | Integer_expression::do_type() | |
1574 | { | |
1575 | if (this->type_ == NULL) | |
1576 | this->type_ = Type::make_abstract_integer_type(); | |
1577 | return this->type_; | |
1578 | } | |
1579 | ||
1580 | // Set the type of the integer value. Here we may switch from an | |
1581 | // abstract type to a real type. | |
1582 | ||
1583 | void | |
1584 | Integer_expression::do_determine_type(const Type_context* context) | |
1585 | { | |
1586 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1587 | ; | |
1588 | else if (context->type != NULL | |
1589 | && (context->type->integer_type() != NULL | |
1590 | || context->type->float_type() != NULL | |
1591 | || context->type->complex_type() != NULL)) | |
1592 | this->type_ = context->type; | |
1593 | else if (!context->may_be_abstract) | |
1594 | this->type_ = Type::lookup_integer_type("int"); | |
1595 | } | |
1596 | ||
1597 | // Return true if the integer VAL fits in the range of the type TYPE. | |
1598 | // Otherwise give an error and return false. TYPE may be NULL. | |
1599 | ||
1600 | bool | |
1601 | Integer_expression::check_constant(mpz_t val, Type* type, | |
1602 | source_location location) | |
1603 | { | |
1604 | if (type == NULL) | |
1605 | return true; | |
1606 | Integer_type* itype = type->integer_type(); | |
1607 | if (itype == NULL || itype->is_abstract()) | |
1608 | return true; | |
1609 | ||
1610 | int bits = mpz_sizeinbase(val, 2); | |
1611 | ||
1612 | if (itype->is_unsigned()) | |
1613 | { | |
1614 | // For an unsigned type we can only accept a nonnegative number, | |
1615 | // and we must be able to represent at least BITS. | |
1616 | if (mpz_sgn(val) >= 0 | |
1617 | && bits <= itype->bits()) | |
1618 | return true; | |
1619 | } | |
1620 | else | |
1621 | { | |
1622 | // For a signed type we need an extra bit to indicate the sign. | |
1623 | // We have to handle the most negative integer specially. | |
1624 | if (bits + 1 <= itype->bits() | |
1625 | || (bits <= itype->bits() | |
1626 | && mpz_sgn(val) < 0 | |
1627 | && (mpz_scan1(val, 0) | |
1628 | == static_cast<unsigned long>(itype->bits() - 1)) | |
1629 | && mpz_scan0(val, itype->bits()) == ULONG_MAX)) | |
1630 | return true; | |
1631 | } | |
1632 | ||
1633 | error_at(location, "integer constant overflow"); | |
1634 | return false; | |
1635 | } | |
1636 | ||
1637 | // Check the type of an integer constant. | |
1638 | ||
1639 | void | |
1640 | Integer_expression::do_check_types(Gogo*) | |
1641 | { | |
1642 | if (this->type_ == NULL) | |
1643 | return; | |
1644 | if (!Integer_expression::check_constant(this->val_, this->type_, | |
1645 | this->location())) | |
1646 | this->set_is_error(); | |
1647 | } | |
1648 | ||
1649 | // Get a tree for an integer constant. | |
1650 | ||
1651 | tree | |
1652 | Integer_expression::do_get_tree(Translate_context* context) | |
1653 | { | |
1654 | Gogo* gogo = context->gogo(); | |
1655 | tree type; | |
1656 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1657 | type = this->type_->get_tree(gogo); | |
1658 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
1659 | { | |
1660 | // We are converting to an abstract floating point type. | |
1661 | type = Type::lookup_float_type("float64")->get_tree(gogo); | |
1662 | } | |
1663 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
1664 | { | |
1665 | // We are converting to an abstract complex type. | |
1666 | type = Type::lookup_complex_type("complex128")->get_tree(gogo); | |
1667 | } | |
1668 | else | |
1669 | { | |
1670 | // If we still have an abstract type here, then this is being | |
1671 | // used in a constant expression which didn't get reduced for | |
1672 | // some reason. Use a type which will fit the value. We use <, | |
1673 | // not <=, because we need an extra bit for the sign bit. | |
1674 | int bits = mpz_sizeinbase(this->val_, 2); | |
1675 | if (bits < INT_TYPE_SIZE) | |
1676 | type = Type::lookup_integer_type("int")->get_tree(gogo); | |
1677 | else if (bits < 64) | |
1678 | type = Type::lookup_integer_type("int64")->get_tree(gogo); | |
1679 | else | |
1680 | type = long_long_integer_type_node; | |
1681 | } | |
1682 | return Expression::integer_constant_tree(this->val_, type); | |
1683 | } | |
1684 | ||
1685 | // Write VAL to export data. | |
1686 | ||
1687 | void | |
1688 | Integer_expression::export_integer(Export* exp, const mpz_t val) | |
1689 | { | |
1690 | char* s = mpz_get_str(NULL, 10, val); | |
1691 | exp->write_c_string(s); | |
1692 | free(s); | |
1693 | } | |
1694 | ||
1695 | // Export an integer in a constant expression. | |
1696 | ||
1697 | void | |
1698 | Integer_expression::do_export(Export* exp) const | |
1699 | { | |
1700 | Integer_expression::export_integer(exp, this->val_); | |
1701 | // A trailing space lets us reliably identify the end of the number. | |
1702 | exp->write_c_string(" "); | |
1703 | } | |
1704 | ||
1705 | // Import an integer, floating point, or complex value. This handles | |
1706 | // all these types because they all start with digits. | |
1707 | ||
1708 | Expression* | |
1709 | Integer_expression::do_import(Import* imp) | |
1710 | { | |
1711 | std::string num = imp->read_identifier(); | |
1712 | imp->require_c_string(" "); | |
1713 | if (!num.empty() && num[num.length() - 1] == 'i') | |
1714 | { | |
1715 | mpfr_t real; | |
1716 | size_t plus_pos = num.find('+', 1); | |
1717 | size_t minus_pos = num.find('-', 1); | |
1718 | size_t pos; | |
1719 | if (plus_pos == std::string::npos) | |
1720 | pos = minus_pos; | |
1721 | else if (minus_pos == std::string::npos) | |
1722 | pos = plus_pos; | |
1723 | else | |
1724 | { | |
1725 | error_at(imp->location(), "bad number in import data: %qs", | |
1726 | num.c_str()); | |
1727 | return Expression::make_error(imp->location()); | |
1728 | } | |
1729 | if (pos == std::string::npos) | |
1730 | mpfr_set_ui(real, 0, GMP_RNDN); | |
1731 | else | |
1732 | { | |
1733 | std::string real_str = num.substr(0, pos); | |
1734 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
1735 | { | |
1736 | error_at(imp->location(), "bad number in import data: %qs", | |
1737 | real_str.c_str()); | |
1738 | return Expression::make_error(imp->location()); | |
1739 | } | |
1740 | } | |
1741 | ||
1742 | std::string imag_str; | |
1743 | if (pos == std::string::npos) | |
1744 | imag_str = num; | |
1745 | else | |
1746 | imag_str = num.substr(pos); | |
1747 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
1748 | mpfr_t imag; | |
1749 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
1750 | { | |
1751 | error_at(imp->location(), "bad number in import data: %qs", | |
1752 | imag_str.c_str()); | |
1753 | return Expression::make_error(imp->location()); | |
1754 | } | |
1755 | Expression* ret = Expression::make_complex(&real, &imag, NULL, | |
1756 | imp->location()); | |
1757 | mpfr_clear(real); | |
1758 | mpfr_clear(imag); | |
1759 | return ret; | |
1760 | } | |
1761 | else if (num.find('.') == std::string::npos | |
1762 | && num.find('E') == std::string::npos) | |
1763 | { | |
1764 | mpz_t val; | |
1765 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
1766 | { | |
1767 | error_at(imp->location(), "bad number in import data: %qs", | |
1768 | num.c_str()); | |
1769 | return Expression::make_error(imp->location()); | |
1770 | } | |
1771 | Expression* ret = Expression::make_integer(&val, NULL, imp->location()); | |
1772 | mpz_clear(val); | |
1773 | return ret; | |
1774 | } | |
1775 | else | |
1776 | { | |
1777 | mpfr_t val; | |
1778 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
1779 | { | |
1780 | error_at(imp->location(), "bad number in import data: %qs", | |
1781 | num.c_str()); | |
1782 | return Expression::make_error(imp->location()); | |
1783 | } | |
1784 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
1785 | mpfr_clear(val); | |
1786 | return ret; | |
1787 | } | |
1788 | } | |
1789 | ||
1790 | // Build a new integer value. | |
1791 | ||
1792 | Expression* | |
1793 | Expression::make_integer(const mpz_t* val, Type* type, | |
1794 | source_location location) | |
1795 | { | |
1796 | return new Integer_expression(val, type, location); | |
1797 | } | |
1798 | ||
1799 | // Floats. | |
1800 | ||
1801 | class Float_expression : public Expression | |
1802 | { | |
1803 | public: | |
1804 | Float_expression(const mpfr_t* val, Type* type, source_location location) | |
1805 | : Expression(EXPRESSION_FLOAT, location), | |
1806 | type_(type) | |
1807 | { | |
1808 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
1809 | } | |
1810 | ||
1811 | // Constrain VAL to fit into TYPE. | |
1812 | static void | |
1813 | constrain_float(mpfr_t val, Type* type); | |
1814 | ||
1815 | // Return whether VAL fits in the type. | |
1816 | static bool | |
1817 | check_constant(mpfr_t val, Type*, source_location); | |
1818 | ||
1819 | // Write VAL to export data. | |
1820 | static void | |
1821 | export_float(Export* exp, const mpfr_t val); | |
1822 | ||
1823 | protected: | |
1824 | bool | |
1825 | do_is_constant() const | |
1826 | { return true; } | |
1827 | ||
1828 | bool | |
1829 | do_float_constant_value(mpfr_t val, Type**) const; | |
1830 | ||
1831 | Type* | |
1832 | do_type(); | |
1833 | ||
1834 | void | |
1835 | do_determine_type(const Type_context*); | |
1836 | ||
1837 | void | |
1838 | do_check_types(Gogo*); | |
1839 | ||
1840 | Expression* | |
1841 | do_copy() | |
1842 | { return Expression::make_float(&this->val_, this->type_, | |
1843 | this->location()); } | |
1844 | ||
1845 | tree | |
1846 | do_get_tree(Translate_context*); | |
1847 | ||
1848 | void | |
1849 | do_export(Export*) const; | |
1850 | ||
1851 | private: | |
1852 | // The floating point value. | |
1853 | mpfr_t val_; | |
1854 | // The type so far. | |
1855 | Type* type_; | |
1856 | }; | |
1857 | ||
1858 | // Constrain VAL to fit into TYPE. | |
1859 | ||
1860 | void | |
1861 | Float_expression::constrain_float(mpfr_t val, Type* type) | |
1862 | { | |
1863 | Float_type* ftype = type->float_type(); | |
1864 | if (ftype != NULL && !ftype->is_abstract()) | |
1865 | { | |
1866 | tree type_tree = ftype->type_tree(); | |
1867 | REAL_VALUE_TYPE rvt; | |
1868 | real_from_mpfr(&rvt, val, type_tree, GMP_RNDN); | |
1869 | real_convert(&rvt, TYPE_MODE(type_tree), &rvt); | |
1870 | mpfr_from_real(val, &rvt, GMP_RNDN); | |
1871 | } | |
1872 | } | |
1873 | ||
1874 | // Return a floating point constant value. | |
1875 | ||
1876 | bool | |
1877 | Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
1878 | { | |
1879 | if (this->type_ != NULL) | |
1880 | *ptype = this->type_; | |
1881 | mpfr_set(val, this->val_, GMP_RNDN); | |
1882 | return true; | |
1883 | } | |
1884 | ||
1885 | // Return the current type. If we haven't set the type yet, we return | |
1886 | // an abstract float type. | |
1887 | ||
1888 | Type* | |
1889 | Float_expression::do_type() | |
1890 | { | |
1891 | if (this->type_ == NULL) | |
1892 | this->type_ = Type::make_abstract_float_type(); | |
1893 | return this->type_; | |
1894 | } | |
1895 | ||
1896 | // Set the type of the float value. Here we may switch from an | |
1897 | // abstract type to a real type. | |
1898 | ||
1899 | void | |
1900 | Float_expression::do_determine_type(const Type_context* context) | |
1901 | { | |
1902 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1903 | ; | |
1904 | else if (context->type != NULL | |
1905 | && (context->type->integer_type() != NULL | |
1906 | || context->type->float_type() != NULL | |
1907 | || context->type->complex_type() != NULL)) | |
1908 | this->type_ = context->type; | |
1909 | else if (!context->may_be_abstract) | |
1910 | this->type_ = Type::lookup_float_type("float"); | |
1911 | } | |
1912 | ||
1913 | // Return true if the floating point value VAL fits in the range of | |
1914 | // the type TYPE. Otherwise give an error and return false. TYPE may | |
1915 | // be NULL. | |
1916 | ||
1917 | bool | |
1918 | Float_expression::check_constant(mpfr_t val, Type* type, | |
1919 | source_location location) | |
1920 | { | |
1921 | if (type == NULL) | |
1922 | return true; | |
1923 | Float_type* ftype = type->float_type(); | |
1924 | if (ftype == NULL || ftype->is_abstract()) | |
1925 | return true; | |
1926 | ||
1927 | // A NaN or Infinity always fits in the range of the type. | |
1928 | if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
1929 | return true; | |
1930 | ||
1931 | mp_exp_t exp = mpfr_get_exp(val); | |
1932 | mp_exp_t max_exp; | |
1933 | switch (ftype->bits()) | |
1934 | { | |
1935 | case 32: | |
1936 | max_exp = 128; | |
1937 | break; | |
1938 | case 64: | |
1939 | max_exp = 1024; | |
1940 | break; | |
1941 | default: | |
1942 | gcc_unreachable(); | |
1943 | } | |
1944 | if (exp > max_exp) | |
1945 | { | |
1946 | error_at(location, "floating point constant overflow"); | |
1947 | return false; | |
1948 | } | |
1949 | return true; | |
1950 | } | |
1951 | ||
1952 | // Check the type of a float value. | |
1953 | ||
1954 | void | |
1955 | Float_expression::do_check_types(Gogo*) | |
1956 | { | |
1957 | if (this->type_ == NULL) | |
1958 | return; | |
1959 | ||
1960 | if (!Float_expression::check_constant(this->val_, this->type_, | |
1961 | this->location())) | |
1962 | this->set_is_error(); | |
1963 | ||
1964 | Integer_type* integer_type = this->type_->integer_type(); | |
1965 | if (integer_type != NULL) | |
1966 | { | |
1967 | if (!mpfr_integer_p(this->val_)) | |
1968 | this->report_error(_("floating point constant truncated to integer")); | |
1969 | else | |
1970 | { | |
1971 | gcc_assert(!integer_type->is_abstract()); | |
1972 | mpz_t ival; | |
1973 | mpz_init(ival); | |
1974 | mpfr_get_z(ival, this->val_, GMP_RNDN); | |
1975 | Integer_expression::check_constant(ival, integer_type, | |
1976 | this->location()); | |
1977 | mpz_clear(ival); | |
1978 | } | |
1979 | } | |
1980 | } | |
1981 | ||
1982 | // Get a tree for a float constant. | |
1983 | ||
1984 | tree | |
1985 | Float_expression::do_get_tree(Translate_context* context) | |
1986 | { | |
1987 | Gogo* gogo = context->gogo(); | |
1988 | tree type; | |
1989 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1990 | type = this->type_->get_tree(gogo); | |
1991 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
1992 | { | |
1993 | // We have an abstract integer type. We just hope for the best. | |
1994 | type = Type::lookup_integer_type("int")->get_tree(gogo); | |
1995 | } | |
1996 | else | |
1997 | { | |
1998 | // If we still have an abstract type here, then this is being | |
1999 | // used in a constant expression which didn't get reduced. We | |
2000 | // just use float64 and hope for the best. | |
2001 | type = Type::lookup_float_type("float64")->get_tree(gogo); | |
2002 | } | |
2003 | return Expression::float_constant_tree(this->val_, type); | |
2004 | } | |
2005 | ||
2006 | // Write a floating point number to export data. | |
2007 | ||
2008 | void | |
2009 | Float_expression::export_float(Export *exp, const mpfr_t val) | |
2010 | { | |
2011 | mp_exp_t exponent; | |
2012 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2013 | if (*s == '-') | |
2014 | exp->write_c_string("-"); | |
2015 | exp->write_c_string("0."); | |
2016 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2017 | mpfr_free_str(s); | |
2018 | char buf[30]; | |
2019 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2020 | exp->write_c_string(buf); | |
2021 | } | |
2022 | ||
2023 | // Export a floating point number in a constant expression. | |
2024 | ||
2025 | void | |
2026 | Float_expression::do_export(Export* exp) const | |
2027 | { | |
2028 | Float_expression::export_float(exp, this->val_); | |
2029 | // A trailing space lets us reliably identify the end of the number. | |
2030 | exp->write_c_string(" "); | |
2031 | } | |
2032 | ||
2033 | // Make a float expression. | |
2034 | ||
2035 | Expression* | |
2036 | Expression::make_float(const mpfr_t* val, Type* type, source_location location) | |
2037 | { | |
2038 | return new Float_expression(val, type, location); | |
2039 | } | |
2040 | ||
2041 | // Complex numbers. | |
2042 | ||
2043 | class Complex_expression : public Expression | |
2044 | { | |
2045 | public: | |
2046 | Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
2047 | source_location location) | |
2048 | : Expression(EXPRESSION_COMPLEX, location), | |
2049 | type_(type) | |
2050 | { | |
2051 | mpfr_init_set(this->real_, *real, GMP_RNDN); | |
2052 | mpfr_init_set(this->imag_, *imag, GMP_RNDN); | |
2053 | } | |
2054 | ||
2055 | // Constrain REAL/IMAG to fit into TYPE. | |
2056 | static void | |
2057 | constrain_complex(mpfr_t real, mpfr_t imag, Type* type); | |
2058 | ||
2059 | // Return whether REAL/IMAG fits in the type. | |
2060 | static bool | |
2061 | check_constant(mpfr_t real, mpfr_t imag, Type*, source_location); | |
2062 | ||
2063 | // Write REAL/IMAG to export data. | |
2064 | static void | |
2065 | export_complex(Export* exp, const mpfr_t real, const mpfr_t val); | |
2066 | ||
2067 | protected: | |
2068 | bool | |
2069 | do_is_constant() const | |
2070 | { return true; } | |
2071 | ||
2072 | bool | |
2073 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const; | |
2074 | ||
2075 | Type* | |
2076 | do_type(); | |
2077 | ||
2078 | void | |
2079 | do_determine_type(const Type_context*); | |
2080 | ||
2081 | void | |
2082 | do_check_types(Gogo*); | |
2083 | ||
2084 | Expression* | |
2085 | do_copy() | |
2086 | { | |
2087 | return Expression::make_complex(&this->real_, &this->imag_, this->type_, | |
2088 | this->location()); | |
2089 | } | |
2090 | ||
2091 | tree | |
2092 | do_get_tree(Translate_context*); | |
2093 | ||
2094 | void | |
2095 | do_export(Export*) const; | |
2096 | ||
2097 | private: | |
2098 | // The real part. | |
2099 | mpfr_t real_; | |
2100 | // The imaginary part; | |
2101 | mpfr_t imag_; | |
2102 | // The type if known. | |
2103 | Type* type_; | |
2104 | }; | |
2105 | ||
2106 | // Constrain REAL/IMAG to fit into TYPE. | |
2107 | ||
2108 | void | |
2109 | Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type) | |
2110 | { | |
2111 | Complex_type* ctype = type->complex_type(); | |
2112 | if (ctype != NULL && !ctype->is_abstract()) | |
2113 | { | |
2114 | tree type_tree = ctype->type_tree(); | |
2115 | ||
2116 | REAL_VALUE_TYPE rvt; | |
2117 | real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN); | |
2118 | real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt); | |
2119 | mpfr_from_real(real, &rvt, GMP_RNDN); | |
2120 | ||
2121 | real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN); | |
2122 | real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt); | |
2123 | mpfr_from_real(imag, &rvt, GMP_RNDN); | |
2124 | } | |
2125 | } | |
2126 | ||
2127 | // Return a complex constant value. | |
2128 | ||
2129 | bool | |
2130 | Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
2131 | Type** ptype) const | |
2132 | { | |
2133 | if (this->type_ != NULL) | |
2134 | *ptype = this->type_; | |
2135 | mpfr_set(real, this->real_, GMP_RNDN); | |
2136 | mpfr_set(imag, this->imag_, GMP_RNDN); | |
2137 | return true; | |
2138 | } | |
2139 | ||
2140 | // Return the current type. If we haven't set the type yet, we return | |
2141 | // an abstract complex type. | |
2142 | ||
2143 | Type* | |
2144 | Complex_expression::do_type() | |
2145 | { | |
2146 | if (this->type_ == NULL) | |
2147 | this->type_ = Type::make_abstract_complex_type(); | |
2148 | return this->type_; | |
2149 | } | |
2150 | ||
2151 | // Set the type of the complex value. Here we may switch from an | |
2152 | // abstract type to a real type. | |
2153 | ||
2154 | void | |
2155 | Complex_expression::do_determine_type(const Type_context* context) | |
2156 | { | |
2157 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2158 | ; | |
2159 | else if (context->type != NULL | |
2160 | && context->type->complex_type() != NULL) | |
2161 | this->type_ = context->type; | |
2162 | else if (!context->may_be_abstract) | |
2163 | this->type_ = Type::lookup_complex_type("complex"); | |
2164 | } | |
2165 | ||
2166 | // Return true if the complex value REAL/IMAG fits in the range of the | |
2167 | // type TYPE. Otherwise give an error and return false. TYPE may be | |
2168 | // NULL. | |
2169 | ||
2170 | bool | |
2171 | Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type, | |
2172 | source_location location) | |
2173 | { | |
2174 | if (type == NULL) | |
2175 | return true; | |
2176 | Complex_type* ctype = type->complex_type(); | |
2177 | if (ctype == NULL || ctype->is_abstract()) | |
2178 | return true; | |
2179 | ||
2180 | mp_exp_t max_exp; | |
2181 | switch (ctype->bits()) | |
2182 | { | |
2183 | case 64: | |
2184 | max_exp = 128; | |
2185 | break; | |
2186 | case 128: | |
2187 | max_exp = 1024; | |
2188 | break; | |
2189 | default: | |
2190 | gcc_unreachable(); | |
2191 | } | |
2192 | ||
2193 | // A NaN or Infinity always fits in the range of the type. | |
2194 | if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real)) | |
2195 | { | |
2196 | if (mpfr_get_exp(real) > max_exp) | |
2197 | { | |
2198 | error_at(location, "complex real part constant overflow"); | |
2199 | return false; | |
2200 | } | |
2201 | } | |
2202 | ||
2203 | if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag)) | |
2204 | { | |
2205 | if (mpfr_get_exp(imag) > max_exp) | |
2206 | { | |
2207 | error_at(location, "complex imaginary part constant overflow"); | |
2208 | return false; | |
2209 | } | |
2210 | } | |
2211 | ||
2212 | return true; | |
2213 | } | |
2214 | ||
2215 | // Check the type of a complex value. | |
2216 | ||
2217 | void | |
2218 | Complex_expression::do_check_types(Gogo*) | |
2219 | { | |
2220 | if (this->type_ == NULL) | |
2221 | return; | |
2222 | ||
2223 | if (!Complex_expression::check_constant(this->real_, this->imag_, | |
2224 | this->type_, this->location())) | |
2225 | this->set_is_error(); | |
2226 | } | |
2227 | ||
2228 | // Get a tree for a complex constant. | |
2229 | ||
2230 | tree | |
2231 | Complex_expression::do_get_tree(Translate_context* context) | |
2232 | { | |
2233 | Gogo* gogo = context->gogo(); | |
2234 | tree type; | |
2235 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2236 | type = this->type_->get_tree(gogo); | |
2237 | else | |
2238 | { | |
2239 | // If we still have an abstract type here, this this is being | |
2240 | // used in a constant expression which didn't get reduced. We | |
2241 | // just use complex128 and hope for the best. | |
2242 | type = Type::lookup_complex_type("complex128")->get_tree(gogo); | |
2243 | } | |
2244 | return Expression::complex_constant_tree(this->real_, this->imag_, type); | |
2245 | } | |
2246 | ||
2247 | // Write REAL/IMAG to export data. | |
2248 | ||
2249 | void | |
2250 | Complex_expression::export_complex(Export* exp, const mpfr_t real, | |
2251 | const mpfr_t imag) | |
2252 | { | |
2253 | if (!mpfr_zero_p(real)) | |
2254 | { | |
2255 | Float_expression::export_float(exp, real); | |
2256 | if (mpfr_sgn(imag) > 0) | |
2257 | exp->write_c_string("+"); | |
2258 | } | |
2259 | Float_expression::export_float(exp, imag); | |
2260 | exp->write_c_string("i"); | |
2261 | } | |
2262 | ||
2263 | // Export a complex number in a constant expression. | |
2264 | ||
2265 | void | |
2266 | Complex_expression::do_export(Export* exp) const | |
2267 | { | |
2268 | Complex_expression::export_complex(exp, this->real_, this->imag_); | |
2269 | // A trailing space lets us reliably identify the end of the number. | |
2270 | exp->write_c_string(" "); | |
2271 | } | |
2272 | ||
2273 | // Make a complex expression. | |
2274 | ||
2275 | Expression* | |
2276 | Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
2277 | source_location location) | |
2278 | { | |
2279 | return new Complex_expression(real, imag, type, location); | |
2280 | } | |
2281 | ||
2282 | // A reference to a const in an expression. | |
2283 | ||
2284 | class Const_expression : public Expression | |
2285 | { | |
2286 | public: | |
2287 | Const_expression(Named_object* constant, source_location location) | |
2288 | : Expression(EXPRESSION_CONST_REFERENCE, location), | |
2289 | constant_(constant), type_(NULL) | |
2290 | { } | |
2291 | ||
2292 | const std::string& | |
2293 | name() const | |
2294 | { return this->constant_->name(); } | |
2295 | ||
2296 | protected: | |
2297 | Expression* | |
2298 | do_lower(Gogo*, Named_object*, int); | |
2299 | ||
2300 | bool | |
2301 | do_is_constant() const | |
2302 | { return true; } | |
2303 | ||
2304 | bool | |
2305 | do_integer_constant_value(bool, mpz_t val, Type**) const; | |
2306 | ||
2307 | bool | |
2308 | do_float_constant_value(mpfr_t val, Type**) const; | |
2309 | ||
2310 | bool | |
2311 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const; | |
2312 | ||
2313 | bool | |
2314 | do_string_constant_value(std::string* val) const | |
2315 | { return this->constant_->const_value()->expr()->string_constant_value(val); } | |
2316 | ||
2317 | Type* | |
2318 | do_type(); | |
2319 | ||
2320 | // The type of a const is set by the declaration, not the use. | |
2321 | void | |
2322 | do_determine_type(const Type_context*); | |
2323 | ||
2324 | void | |
2325 | do_check_types(Gogo*); | |
2326 | ||
2327 | Expression* | |
2328 | do_copy() | |
2329 | { return this; } | |
2330 | ||
2331 | tree | |
2332 | do_get_tree(Translate_context* context); | |
2333 | ||
2334 | // When exporting a reference to a const as part of a const | |
2335 | // expression, we export the value. We ignore the fact that it has | |
2336 | // a name. | |
2337 | void | |
2338 | do_export(Export* exp) const | |
2339 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2340 | ||
2341 | private: | |
2342 | // The constant. | |
2343 | Named_object* constant_; | |
2344 | // The type of this reference. This is used if the constant has an | |
2345 | // abstract type. | |
2346 | Type* type_; | |
2347 | }; | |
2348 | ||
2349 | // Lower a constant expression. This is where we convert the | |
2350 | // predeclared constant iota into an integer value. | |
2351 | ||
2352 | Expression* | |
2353 | Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value) | |
2354 | { | |
2355 | if (this->constant_->const_value()->expr()->classification() | |
2356 | == EXPRESSION_IOTA) | |
2357 | { | |
2358 | if (iota_value == -1) | |
2359 | { | |
2360 | error_at(this->location(), | |
2361 | "iota is only defined in const declarations"); | |
2362 | iota_value = 0; | |
2363 | } | |
2364 | mpz_t val; | |
2365 | mpz_init_set_ui(val, static_cast<unsigned long>(iota_value)); | |
2366 | Expression* ret = Expression::make_integer(&val, NULL, | |
2367 | this->location()); | |
2368 | mpz_clear(val); | |
2369 | return ret; | |
2370 | } | |
2371 | ||
2372 | // Make sure that the constant itself has been lowered. | |
2373 | gogo->lower_constant(this->constant_); | |
2374 | ||
2375 | return this; | |
2376 | } | |
2377 | ||
2378 | // Return an integer constant value. | |
2379 | ||
2380 | bool | |
2381 | Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
2382 | Type** ptype) const | |
2383 | { | |
2384 | Type* ctype; | |
2385 | if (this->type_ != NULL) | |
2386 | ctype = this->type_; | |
2387 | else | |
2388 | ctype = this->constant_->const_value()->type(); | |
2389 | if (ctype != NULL && ctype->integer_type() == NULL) | |
2390 | return false; | |
2391 | ||
2392 | Expression* e = this->constant_->const_value()->expr(); | |
2393 | Type* t; | |
2394 | bool r = e->integer_constant_value(iota_is_constant, val, &t); | |
2395 | ||
2396 | if (r | |
2397 | && ctype != NULL | |
2398 | && !Integer_expression::check_constant(val, ctype, this->location())) | |
2399 | return false; | |
2400 | ||
2401 | *ptype = ctype != NULL ? ctype : t; | |
2402 | return r; | |
2403 | } | |
2404 | ||
2405 | // Return a floating point constant value. | |
2406 | ||
2407 | bool | |
2408 | Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
2409 | { | |
2410 | Type* ctype; | |
2411 | if (this->type_ != NULL) | |
2412 | ctype = this->type_; | |
2413 | else | |
2414 | ctype = this->constant_->const_value()->type(); | |
2415 | if (ctype != NULL && ctype->float_type() == NULL) | |
2416 | return false; | |
2417 | ||
2418 | Type* t; | |
2419 | bool r = this->constant_->const_value()->expr()->float_constant_value(val, | |
2420 | &t); | |
2421 | if (r && ctype != NULL) | |
2422 | { | |
2423 | if (!Float_expression::check_constant(val, ctype, this->location())) | |
2424 | return false; | |
2425 | Float_expression::constrain_float(val, ctype); | |
2426 | } | |
2427 | *ptype = ctype != NULL ? ctype : t; | |
2428 | return r; | |
2429 | } | |
2430 | ||
2431 | // Return a complex constant value. | |
2432 | ||
2433 | bool | |
2434 | Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
2435 | Type **ptype) const | |
2436 | { | |
2437 | Type* ctype; | |
2438 | if (this->type_ != NULL) | |
2439 | ctype = this->type_; | |
2440 | else | |
2441 | ctype = this->constant_->const_value()->type(); | |
2442 | if (ctype != NULL && ctype->complex_type() == NULL) | |
2443 | return false; | |
2444 | ||
2445 | Type *t; | |
2446 | bool r = this->constant_->const_value()->expr()->complex_constant_value(real, | |
2447 | imag, | |
2448 | &t); | |
2449 | if (r && ctype != NULL) | |
2450 | { | |
2451 | if (!Complex_expression::check_constant(real, imag, ctype, | |
2452 | this->location())) | |
2453 | return false; | |
2454 | Complex_expression::constrain_complex(real, imag, ctype); | |
2455 | } | |
2456 | *ptype = ctype != NULL ? ctype : t; | |
2457 | return r; | |
2458 | } | |
2459 | ||
2460 | // Return the type of the const reference. | |
2461 | ||
2462 | Type* | |
2463 | Const_expression::do_type() | |
2464 | { | |
2465 | if (this->type_ != NULL) | |
2466 | return this->type_; | |
2467 | Named_constant* nc = this->constant_->const_value(); | |
2468 | Type* ret = nc->type(); | |
2469 | if (ret != NULL) | |
2470 | return ret; | |
2471 | // During parsing, a named constant may have a NULL type, but we | |
2472 | // must not return a NULL type here. | |
2473 | return nc->expr()->type(); | |
2474 | } | |
2475 | ||
2476 | // Set the type of the const reference. | |
2477 | ||
2478 | void | |
2479 | Const_expression::do_determine_type(const Type_context* context) | |
2480 | { | |
2481 | Type* ctype = this->constant_->const_value()->type(); | |
2482 | Type* cetype = (ctype != NULL | |
2483 | ? ctype | |
2484 | : this->constant_->const_value()->expr()->type()); | |
2485 | if (ctype != NULL && !ctype->is_abstract()) | |
2486 | ; | |
2487 | else if (context->type != NULL | |
2488 | && (context->type->integer_type() != NULL | |
2489 | || context->type->float_type() != NULL | |
2490 | || context->type->complex_type() != NULL) | |
2491 | && (cetype->integer_type() != NULL | |
2492 | || cetype->float_type() != NULL | |
2493 | || cetype->complex_type() != NULL)) | |
2494 | this->type_ = context->type; | |
2495 | else if (context->type != NULL | |
2496 | && context->type->is_string_type() | |
2497 | && cetype->is_string_type()) | |
2498 | this->type_ = context->type; | |
2499 | else if (context->type != NULL | |
2500 | && context->type->is_boolean_type() | |
2501 | && cetype->is_boolean_type()) | |
2502 | this->type_ = context->type; | |
2503 | else if (!context->may_be_abstract) | |
2504 | { | |
2505 | if (cetype->is_abstract()) | |
2506 | cetype = cetype->make_non_abstract_type(); | |
2507 | this->type_ = cetype; | |
2508 | } | |
2509 | } | |
2510 | ||
2511 | // Check types of a const reference. | |
2512 | ||
2513 | void | |
2514 | Const_expression::do_check_types(Gogo*) | |
2515 | { | |
2516 | if (this->type_ == NULL || this->type_->is_abstract()) | |
2517 | return; | |
2518 | ||
2519 | // Check for integer overflow. | |
2520 | if (this->type_->integer_type() != NULL) | |
2521 | { | |
2522 | mpz_t ival; | |
2523 | mpz_init(ival); | |
2524 | Type* dummy; | |
2525 | if (!this->integer_constant_value(true, ival, &dummy)) | |
2526 | { | |
2527 | mpfr_t fval; | |
2528 | mpfr_init(fval); | |
2529 | Expression* cexpr = this->constant_->const_value()->expr(); | |
2530 | if (cexpr->float_constant_value(fval, &dummy)) | |
2531 | { | |
2532 | if (!mpfr_integer_p(fval)) | |
2533 | this->report_error(_("floating point constant " | |
2534 | "truncated to integer")); | |
2535 | else | |
2536 | { | |
2537 | mpfr_get_z(ival, fval, GMP_RNDN); | |
2538 | Integer_expression::check_constant(ival, this->type_, | |
2539 | this->location()); | |
2540 | } | |
2541 | } | |
2542 | mpfr_clear(fval); | |
2543 | } | |
2544 | mpz_clear(ival); | |
2545 | } | |
2546 | } | |
2547 | ||
2548 | // Return a tree for the const reference. | |
2549 | ||
2550 | tree | |
2551 | Const_expression::do_get_tree(Translate_context* context) | |
2552 | { | |
2553 | Gogo* gogo = context->gogo(); | |
2554 | tree type_tree; | |
2555 | if (this->type_ == NULL) | |
2556 | type_tree = NULL_TREE; | |
2557 | else | |
2558 | { | |
2559 | type_tree = this->type_->get_tree(gogo); | |
2560 | if (type_tree == error_mark_node) | |
2561 | return error_mark_node; | |
2562 | } | |
2563 | ||
2564 | // If the type has been set for this expression, but the underlying | |
2565 | // object is an abstract int or float, we try to get the abstract | |
2566 | // value. Otherwise we may lose something in the conversion. | |
2567 | if (this->type_ != NULL | |
2568 | && this->constant_->const_value()->type()->is_abstract()) | |
2569 | { | |
2570 | Expression* expr = this->constant_->const_value()->expr(); | |
2571 | mpz_t ival; | |
2572 | mpz_init(ival); | |
2573 | Type* t; | |
2574 | if (expr->integer_constant_value(true, ival, &t)) | |
2575 | { | |
2576 | tree ret = Expression::integer_constant_tree(ival, type_tree); | |
2577 | mpz_clear(ival); | |
2578 | return ret; | |
2579 | } | |
2580 | mpz_clear(ival); | |
2581 | ||
2582 | mpfr_t fval; | |
2583 | mpfr_init(fval); | |
2584 | if (expr->float_constant_value(fval, &t)) | |
2585 | { | |
2586 | tree ret = Expression::float_constant_tree(fval, type_tree); | |
2587 | mpfr_clear(fval); | |
2588 | return ret; | |
2589 | } | |
2590 | ||
2591 | mpfr_t imag; | |
2592 | mpfr_init(imag); | |
2593 | if (expr->complex_constant_value(fval, imag, &t)) | |
2594 | { | |
2595 | tree ret = Expression::complex_constant_tree(fval, imag, type_tree); | |
2596 | mpfr_clear(fval); | |
2597 | mpfr_clear(imag); | |
2598 | return ret; | |
2599 | } | |
2600 | mpfr_clear(imag); | |
2601 | mpfr_clear(fval); | |
2602 | } | |
2603 | ||
2604 | tree const_tree = this->constant_->get_tree(gogo, context->function()); | |
2605 | if (this->type_ == NULL | |
2606 | || const_tree == error_mark_node | |
2607 | || TREE_TYPE(const_tree) == error_mark_node) | |
2608 | return const_tree; | |
2609 | ||
2610 | tree ret; | |
2611 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree))) | |
2612 | ret = fold_convert(type_tree, const_tree); | |
2613 | else if (TREE_CODE(type_tree) == INTEGER_TYPE) | |
2614 | ret = fold(convert_to_integer(type_tree, const_tree)); | |
2615 | else if (TREE_CODE(type_tree) == REAL_TYPE) | |
2616 | ret = fold(convert_to_real(type_tree, const_tree)); | |
2617 | else if (TREE_CODE(type_tree) == COMPLEX_TYPE) | |
2618 | ret = fold(convert_to_complex(type_tree, const_tree)); | |
2619 | else | |
2620 | gcc_unreachable(); | |
2621 | return ret; | |
2622 | } | |
2623 | ||
2624 | // Make a reference to a constant in an expression. | |
2625 | ||
2626 | Expression* | |
2627 | Expression::make_const_reference(Named_object* constant, | |
2628 | source_location location) | |
2629 | { | |
2630 | return new Const_expression(constant, location); | |
2631 | } | |
2632 | ||
2633 | // The nil value. | |
2634 | ||
2635 | class Nil_expression : public Expression | |
2636 | { | |
2637 | public: | |
2638 | Nil_expression(source_location location) | |
2639 | : Expression(EXPRESSION_NIL, location) | |
2640 | { } | |
2641 | ||
2642 | static Expression* | |
2643 | do_import(Import*); | |
2644 | ||
2645 | protected: | |
2646 | bool | |
2647 | do_is_constant() const | |
2648 | { return true; } | |
2649 | ||
2650 | Type* | |
2651 | do_type() | |
2652 | { return Type::make_nil_type(); } | |
2653 | ||
2654 | void | |
2655 | do_determine_type(const Type_context*) | |
2656 | { } | |
2657 | ||
2658 | Expression* | |
2659 | do_copy() | |
2660 | { return this; } | |
2661 | ||
2662 | tree | |
2663 | do_get_tree(Translate_context*) | |
2664 | { return null_pointer_node; } | |
2665 | ||
2666 | void | |
2667 | do_export(Export* exp) const | |
2668 | { exp->write_c_string("nil"); } | |
2669 | }; | |
2670 | ||
2671 | // Import a nil expression. | |
2672 | ||
2673 | Expression* | |
2674 | Nil_expression::do_import(Import* imp) | |
2675 | { | |
2676 | imp->require_c_string("nil"); | |
2677 | return Expression::make_nil(imp->location()); | |
2678 | } | |
2679 | ||
2680 | // Make a nil expression. | |
2681 | ||
2682 | Expression* | |
2683 | Expression::make_nil(source_location location) | |
2684 | { | |
2685 | return new Nil_expression(location); | |
2686 | } | |
2687 | ||
2688 | // The value of the predeclared constant iota. This is little more | |
2689 | // than a marker. This will be lowered to an integer in | |
2690 | // Const_expression::do_lower, which is where we know the value that | |
2691 | // it should have. | |
2692 | ||
2693 | class Iota_expression : public Parser_expression | |
2694 | { | |
2695 | public: | |
2696 | Iota_expression(source_location location) | |
2697 | : Parser_expression(EXPRESSION_IOTA, location) | |
2698 | { } | |
2699 | ||
2700 | protected: | |
2701 | Expression* | |
2702 | do_lower(Gogo*, Named_object*, int) | |
2703 | { gcc_unreachable(); } | |
2704 | ||
2705 | // There should only ever be one of these. | |
2706 | Expression* | |
2707 | do_copy() | |
2708 | { gcc_unreachable(); } | |
2709 | }; | |
2710 | ||
2711 | // Make an iota expression. This is only called for one case: the | |
2712 | // value of the predeclared constant iota. | |
2713 | ||
2714 | Expression* | |
2715 | Expression::make_iota() | |
2716 | { | |
2717 | static Iota_expression iota_expression(UNKNOWN_LOCATION); | |
2718 | return &iota_expression; | |
2719 | } | |
2720 | ||
2721 | // A type conversion expression. | |
2722 | ||
2723 | class Type_conversion_expression : public Expression | |
2724 | { | |
2725 | public: | |
2726 | Type_conversion_expression(Type* type, Expression* expr, | |
2727 | source_location location) | |
2728 | : Expression(EXPRESSION_CONVERSION, location), | |
2729 | type_(type), expr_(expr), may_convert_function_types_(false) | |
2730 | { } | |
2731 | ||
2732 | // Return the type to which we are converting. | |
2733 | Type* | |
2734 | type() const | |
2735 | { return this->type_; } | |
2736 | ||
2737 | // Return the expression which we are converting. | |
2738 | Expression* | |
2739 | expr() const | |
2740 | { return this->expr_; } | |
2741 | ||
2742 | // Permit converting from one function type to another. This is | |
2743 | // used internally for method expressions. | |
2744 | void | |
2745 | set_may_convert_function_types() | |
2746 | { | |
2747 | this->may_convert_function_types_ = true; | |
2748 | } | |
2749 | ||
2750 | // Import a type conversion expression. | |
2751 | static Expression* | |
2752 | do_import(Import*); | |
2753 | ||
2754 | protected: | |
2755 | int | |
2756 | do_traverse(Traverse* traverse); | |
2757 | ||
2758 | Expression* | |
2759 | do_lower(Gogo*, Named_object*, int); | |
2760 | ||
2761 | bool | |
2762 | do_is_constant() const | |
2763 | { return this->expr_->is_constant(); } | |
2764 | ||
2765 | bool | |
2766 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
2767 | ||
2768 | bool | |
2769 | do_float_constant_value(mpfr_t, Type**) const; | |
2770 | ||
2771 | bool | |
2772 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
2773 | ||
2774 | bool | |
2775 | do_string_constant_value(std::string*) const; | |
2776 | ||
2777 | Type* | |
2778 | do_type() | |
2779 | { return this->type_; } | |
2780 | ||
2781 | void | |
2782 | do_determine_type(const Type_context*) | |
2783 | { | |
2784 | Type_context subcontext(this->type_, false); | |
2785 | this->expr_->determine_type(&subcontext); | |
2786 | } | |
2787 | ||
2788 | void | |
2789 | do_check_types(Gogo*); | |
2790 | ||
2791 | Expression* | |
2792 | do_copy() | |
2793 | { | |
2794 | return new Type_conversion_expression(this->type_, this->expr_->copy(), | |
2795 | this->location()); | |
2796 | } | |
2797 | ||
2798 | tree | |
2799 | do_get_tree(Translate_context* context); | |
2800 | ||
2801 | void | |
2802 | do_export(Export*) const; | |
2803 | ||
2804 | private: | |
2805 | // The type to convert to. | |
2806 | Type* type_; | |
2807 | // The expression to convert. | |
2808 | Expression* expr_; | |
2809 | // True if this is permitted to convert function types. This is | |
2810 | // used internally for method expressions. | |
2811 | bool may_convert_function_types_; | |
2812 | }; | |
2813 | ||
2814 | // Traversal. | |
2815 | ||
2816 | int | |
2817 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
2818 | { | |
2819 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
2820 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
2821 | return TRAVERSE_EXIT; | |
2822 | return TRAVERSE_CONTINUE; | |
2823 | } | |
2824 | ||
2825 | // Convert to a constant at lowering time. | |
2826 | ||
2827 | Expression* | |
2828 | Type_conversion_expression::do_lower(Gogo*, Named_object*, int) | |
2829 | { | |
2830 | Type* type = this->type_; | |
2831 | Expression* val = this->expr_; | |
2832 | source_location location = this->location(); | |
2833 | ||
2834 | if (type->integer_type() != NULL) | |
2835 | { | |
2836 | mpz_t ival; | |
2837 | mpz_init(ival); | |
2838 | Type* dummy; | |
2839 | if (val->integer_constant_value(false, ival, &dummy)) | |
2840 | { | |
2841 | if (!Integer_expression::check_constant(ival, type, location)) | |
2842 | mpz_set_ui(ival, 0); | |
2843 | Expression* ret = Expression::make_integer(&ival, type, location); | |
2844 | mpz_clear(ival); | |
2845 | return ret; | |
2846 | } | |
2847 | ||
2848 | mpfr_t fval; | |
2849 | mpfr_init(fval); | |
2850 | if (val->float_constant_value(fval, &dummy)) | |
2851 | { | |
2852 | if (!mpfr_integer_p(fval)) | |
2853 | { | |
2854 | error_at(location, | |
2855 | "floating point constant truncated to integer"); | |
2856 | return Expression::make_error(location); | |
2857 | } | |
2858 | mpfr_get_z(ival, fval, GMP_RNDN); | |
2859 | if (!Integer_expression::check_constant(ival, type, location)) | |
2860 | mpz_set_ui(ival, 0); | |
2861 | Expression* ret = Expression::make_integer(&ival, type, location); | |
2862 | mpfr_clear(fval); | |
2863 | mpz_clear(ival); | |
2864 | return ret; | |
2865 | } | |
2866 | mpfr_clear(fval); | |
2867 | mpz_clear(ival); | |
2868 | } | |
2869 | ||
2870 | if (type->float_type() != NULL) | |
2871 | { | |
2872 | mpfr_t fval; | |
2873 | mpfr_init(fval); | |
2874 | Type* dummy; | |
2875 | if (val->float_constant_value(fval, &dummy)) | |
2876 | { | |
2877 | if (!Float_expression::check_constant(fval, type, location)) | |
2878 | mpfr_set_ui(fval, 0, GMP_RNDN); | |
2879 | Float_expression::constrain_float(fval, type); | |
2880 | Expression *ret = Expression::make_float(&fval, type, location); | |
2881 | mpfr_clear(fval); | |
2882 | return ret; | |
2883 | } | |
2884 | mpfr_clear(fval); | |
2885 | } | |
2886 | ||
2887 | if (type->complex_type() != NULL) | |
2888 | { | |
2889 | mpfr_t real; | |
2890 | mpfr_t imag; | |
2891 | mpfr_init(real); | |
2892 | mpfr_init(imag); | |
2893 | Type* dummy; | |
2894 | if (val->complex_constant_value(real, imag, &dummy)) | |
2895 | { | |
2896 | if (!Complex_expression::check_constant(real, imag, type, location)) | |
2897 | { | |
2898 | mpfr_set_ui(real, 0, GMP_RNDN); | |
2899 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
2900 | } | |
2901 | Complex_expression::constrain_complex(real, imag, type); | |
2902 | Expression* ret = Expression::make_complex(&real, &imag, type, | |
2903 | location); | |
2904 | mpfr_clear(real); | |
2905 | mpfr_clear(imag); | |
2906 | return ret; | |
2907 | } | |
2908 | mpfr_clear(real); | |
2909 | mpfr_clear(imag); | |
2910 | } | |
2911 | ||
2912 | if (type->is_open_array_type() && type->named_type() == NULL) | |
2913 | { | |
2914 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
2915 | bool is_byte = element_type == Type::lookup_integer_type("uint8"); | |
2916 | bool is_int = element_type == Type::lookup_integer_type("int"); | |
2917 | if (is_byte || is_int) | |
2918 | { | |
2919 | std::string s; | |
2920 | if (val->string_constant_value(&s)) | |
2921 | { | |
2922 | Expression_list* vals = new Expression_list(); | |
2923 | if (is_byte) | |
2924 | { | |
2925 | for (std::string::const_iterator p = s.begin(); | |
2926 | p != s.end(); | |
2927 | p++) | |
2928 | { | |
2929 | mpz_t val; | |
2930 | mpz_init_set_ui(val, static_cast<unsigned char>(*p)); | |
2931 | Expression* v = Expression::make_integer(&val, | |
2932 | element_type, | |
2933 | location); | |
2934 | vals->push_back(v); | |
2935 | mpz_clear(val); | |
2936 | } | |
2937 | } | |
2938 | else | |
2939 | { | |
2940 | const char *p = s.data(); | |
2941 | const char *pend = s.data() + s.length(); | |
2942 | while (p < pend) | |
2943 | { | |
2944 | unsigned int c; | |
2945 | int adv = Lex::fetch_char(p, &c); | |
2946 | if (adv == 0) | |
2947 | { | |
2948 | warning_at(this->location(), 0, | |
2949 | "invalid UTF-8 encoding"); | |
2950 | adv = 1; | |
2951 | } | |
2952 | p += adv; | |
2953 | mpz_t val; | |
2954 | mpz_init_set_ui(val, c); | |
2955 | Expression* v = Expression::make_integer(&val, | |
2956 | element_type, | |
2957 | location); | |
2958 | vals->push_back(v); | |
2959 | mpz_clear(val); | |
2960 | } | |
2961 | } | |
2962 | ||
2963 | return Expression::make_slice_composite_literal(type, vals, | |
2964 | location); | |
2965 | } | |
2966 | } | |
2967 | } | |
2968 | ||
2969 | return this; | |
2970 | } | |
2971 | ||
2972 | // Return the constant integer value if there is one. | |
2973 | ||
2974 | bool | |
2975 | Type_conversion_expression::do_integer_constant_value(bool iota_is_constant, | |
2976 | mpz_t val, | |
2977 | Type** ptype) const | |
2978 | { | |
2979 | if (this->type_->integer_type() == NULL) | |
2980 | return false; | |
2981 | ||
2982 | mpz_t ival; | |
2983 | mpz_init(ival); | |
2984 | Type* dummy; | |
2985 | if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy)) | |
2986 | { | |
2987 | if (!Integer_expression::check_constant(ival, this->type_, | |
2988 | this->location())) | |
2989 | { | |
2990 | mpz_clear(ival); | |
2991 | return false; | |
2992 | } | |
2993 | mpz_set(val, ival); | |
2994 | mpz_clear(ival); | |
2995 | *ptype = this->type_; | |
2996 | return true; | |
2997 | } | |
2998 | mpz_clear(ival); | |
2999 | ||
3000 | mpfr_t fval; | |
3001 | mpfr_init(fval); | |
3002 | if (this->expr_->float_constant_value(fval, &dummy)) | |
3003 | { | |
3004 | mpfr_get_z(val, fval, GMP_RNDN); | |
3005 | mpfr_clear(fval); | |
3006 | if (!Integer_expression::check_constant(val, this->type_, | |
3007 | this->location())) | |
3008 | return false; | |
3009 | *ptype = this->type_; | |
3010 | return true; | |
3011 | } | |
3012 | mpfr_clear(fval); | |
3013 | ||
3014 | return false; | |
3015 | } | |
3016 | ||
3017 | // Return the constant floating point value if there is one. | |
3018 | ||
3019 | bool | |
3020 | Type_conversion_expression::do_float_constant_value(mpfr_t val, | |
3021 | Type** ptype) const | |
3022 | { | |
3023 | if (this->type_->float_type() == NULL) | |
3024 | return false; | |
3025 | ||
3026 | mpfr_t fval; | |
3027 | mpfr_init(fval); | |
3028 | Type* dummy; | |
3029 | if (this->expr_->float_constant_value(fval, &dummy)) | |
3030 | { | |
3031 | if (!Float_expression::check_constant(fval, this->type_, | |
3032 | this->location())) | |
3033 | { | |
3034 | mpfr_clear(fval); | |
3035 | return false; | |
3036 | } | |
3037 | mpfr_set(val, fval, GMP_RNDN); | |
3038 | mpfr_clear(fval); | |
3039 | Float_expression::constrain_float(val, this->type_); | |
3040 | *ptype = this->type_; | |
3041 | return true; | |
3042 | } | |
3043 | mpfr_clear(fval); | |
3044 | ||
3045 | return false; | |
3046 | } | |
3047 | ||
3048 | // Return the constant complex value if there is one. | |
3049 | ||
3050 | bool | |
3051 | Type_conversion_expression::do_complex_constant_value(mpfr_t real, | |
3052 | mpfr_t imag, | |
3053 | Type **ptype) const | |
3054 | { | |
3055 | if (this->type_->complex_type() == NULL) | |
3056 | return false; | |
3057 | ||
3058 | mpfr_t rval; | |
3059 | mpfr_t ival; | |
3060 | mpfr_init(rval); | |
3061 | mpfr_init(ival); | |
3062 | Type* dummy; | |
3063 | if (this->expr_->complex_constant_value(rval, ival, &dummy)) | |
3064 | { | |
3065 | if (!Complex_expression::check_constant(rval, ival, this->type_, | |
3066 | this->location())) | |
3067 | { | |
3068 | mpfr_clear(rval); | |
3069 | mpfr_clear(ival); | |
3070 | return false; | |
3071 | } | |
3072 | mpfr_set(real, rval, GMP_RNDN); | |
3073 | mpfr_set(imag, ival, GMP_RNDN); | |
3074 | mpfr_clear(rval); | |
3075 | mpfr_clear(ival); | |
3076 | Complex_expression::constrain_complex(real, imag, this->type_); | |
3077 | *ptype = this->type_; | |
3078 | return true; | |
3079 | } | |
3080 | mpfr_clear(rval); | |
3081 | mpfr_clear(ival); | |
3082 | ||
3083 | return false; | |
3084 | } | |
3085 | ||
3086 | // Return the constant string value if there is one. | |
3087 | ||
3088 | bool | |
3089 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3090 | { | |
3091 | if (this->type_->is_string_type() | |
3092 | && this->expr_->type()->integer_type() != NULL) | |
3093 | { | |
3094 | mpz_t ival; | |
3095 | mpz_init(ival); | |
3096 | Type* dummy; | |
3097 | if (this->expr_->integer_constant_value(false, ival, &dummy)) | |
3098 | { | |
3099 | unsigned long ulval = mpz_get_ui(ival); | |
3100 | if (mpz_cmp_ui(ival, ulval) == 0) | |
3101 | { | |
3102 | Lex::append_char(ulval, true, val, this->location()); | |
3103 | mpz_clear(ival); | |
3104 | return true; | |
3105 | } | |
3106 | } | |
3107 | mpz_clear(ival); | |
3108 | } | |
3109 | ||
3110 | // FIXME: Could handle conversion from const []int here. | |
3111 | ||
3112 | return false; | |
3113 | } | |
3114 | ||
3115 | // Check that types are convertible. | |
3116 | ||
3117 | void | |
3118 | Type_conversion_expression::do_check_types(Gogo*) | |
3119 | { | |
3120 | Type* type = this->type_; | |
3121 | Type* expr_type = this->expr_->type(); | |
3122 | std::string reason; | |
3123 | ||
3124 | if (this->may_convert_function_types_ | |
3125 | && type->function_type() != NULL | |
3126 | && expr_type->function_type() != NULL) | |
3127 | return; | |
3128 | ||
3129 | if (Type::are_convertible(type, expr_type, &reason)) | |
3130 | return; | |
3131 | ||
3132 | error_at(this->location(), "%s", reason.c_str()); | |
3133 | this->set_is_error(); | |
3134 | } | |
3135 | ||
3136 | // Get a tree for a type conversion. | |
3137 | ||
3138 | tree | |
3139 | Type_conversion_expression::do_get_tree(Translate_context* context) | |
3140 | { | |
3141 | Gogo* gogo = context->gogo(); | |
3142 | tree type_tree = this->type_->get_tree(gogo); | |
3143 | tree expr_tree = this->expr_->get_tree(context); | |
3144 | ||
3145 | if (type_tree == error_mark_node | |
3146 | || expr_tree == error_mark_node | |
3147 | || TREE_TYPE(expr_tree) == error_mark_node) | |
3148 | return error_mark_node; | |
3149 | ||
3150 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree))) | |
3151 | return fold_convert(type_tree, expr_tree); | |
3152 | ||
3153 | Type* type = this->type_; | |
3154 | Type* expr_type = this->expr_->type(); | |
3155 | tree ret; | |
3156 | if (type->interface_type() != NULL || expr_type->interface_type() != NULL) | |
3157 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3158 | expr_tree, this->location()); | |
3159 | else if (type->integer_type() != NULL) | |
3160 | { | |
3161 | if (expr_type->integer_type() != NULL | |
3162 | || expr_type->float_type() != NULL | |
3163 | || expr_type->is_unsafe_pointer_type()) | |
3164 | ret = fold(convert_to_integer(type_tree, expr_tree)); | |
3165 | else | |
3166 | gcc_unreachable(); | |
3167 | } | |
3168 | else if (type->float_type() != NULL) | |
3169 | { | |
3170 | if (expr_type->integer_type() != NULL | |
3171 | || expr_type->float_type() != NULL) | |
3172 | ret = fold(convert_to_real(type_tree, expr_tree)); | |
3173 | else | |
3174 | gcc_unreachable(); | |
3175 | } | |
3176 | else if (type->complex_type() != NULL) | |
3177 | { | |
3178 | if (expr_type->complex_type() != NULL) | |
3179 | ret = fold(convert_to_complex(type_tree, expr_tree)); | |
3180 | else | |
3181 | gcc_unreachable(); | |
3182 | } | |
3183 | else if (type->is_string_type() | |
3184 | && expr_type->integer_type() != NULL) | |
3185 | { | |
3186 | expr_tree = fold_convert(integer_type_node, expr_tree); | |
3187 | if (host_integerp(expr_tree, 0)) | |
3188 | { | |
3189 | HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0); | |
3190 | std::string s; | |
3191 | Lex::append_char(intval, true, &s, this->location()); | |
3192 | Expression* se = Expression::make_string(s, this->location()); | |
3193 | return se->get_tree(context); | |
3194 | } | |
3195 | ||
3196 | static tree int_to_string_fndecl; | |
3197 | ret = Gogo::call_builtin(&int_to_string_fndecl, | |
3198 | this->location(), | |
3199 | "__go_int_to_string", | |
3200 | 1, | |
3201 | type_tree, | |
3202 | integer_type_node, | |
3203 | fold_convert(integer_type_node, expr_tree)); | |
3204 | } | |
3205 | else if (type->is_string_type() | |
3206 | && (expr_type->array_type() != NULL | |
3207 | || (expr_type->points_to() != NULL | |
3208 | && expr_type->points_to()->array_type() != NULL))) | |
3209 | { | |
3210 | Type* t = expr_type; | |
3211 | if (t->points_to() != NULL) | |
3212 | { | |
3213 | t = t->points_to(); | |
3214 | expr_tree = build_fold_indirect_ref(expr_tree); | |
3215 | } | |
3216 | if (!DECL_P(expr_tree)) | |
3217 | expr_tree = save_expr(expr_tree); | |
3218 | Array_type* a = t->array_type(); | |
3219 | Type* e = a->element_type()->forwarded(); | |
3220 | gcc_assert(e->integer_type() != NULL); | |
3221 | tree valptr = fold_convert(const_ptr_type_node, | |
3222 | a->value_pointer_tree(gogo, expr_tree)); | |
3223 | tree len = a->length_tree(gogo, expr_tree); | |
3224 | len = fold_convert_loc(this->location(), size_type_node, len); | |
3225 | if (e->integer_type()->is_unsigned() | |
3226 | && e->integer_type()->bits() == 8) | |
3227 | { | |
3228 | static tree byte_array_to_string_fndecl; | |
3229 | ret = Gogo::call_builtin(&byte_array_to_string_fndecl, | |
3230 | this->location(), | |
3231 | "__go_byte_array_to_string", | |
3232 | 2, | |
3233 | type_tree, | |
3234 | const_ptr_type_node, | |
3235 | valptr, | |
3236 | size_type_node, | |
3237 | len); | |
3238 | } | |
3239 | else | |
3240 | { | |
3241 | gcc_assert(e == Type::lookup_integer_type("int")); | |
3242 | static tree int_array_to_string_fndecl; | |
3243 | ret = Gogo::call_builtin(&int_array_to_string_fndecl, | |
3244 | this->location(), | |
3245 | "__go_int_array_to_string", | |
3246 | 2, | |
3247 | type_tree, | |
3248 | const_ptr_type_node, | |
3249 | valptr, | |
3250 | size_type_node, | |
3251 | len); | |
3252 | } | |
3253 | } | |
3254 | else if (type->is_open_array_type() && expr_type->is_string_type()) | |
3255 | { | |
3256 | Type* e = type->array_type()->element_type()->forwarded(); | |
3257 | gcc_assert(e->integer_type() != NULL); | |
3258 | if (e->integer_type()->is_unsigned() | |
3259 | && e->integer_type()->bits() == 8) | |
3260 | { | |
3261 | static tree string_to_byte_array_fndecl; | |
3262 | ret = Gogo::call_builtin(&string_to_byte_array_fndecl, | |
3263 | this->location(), | |
3264 | "__go_string_to_byte_array", | |
3265 | 1, | |
3266 | type_tree, | |
3267 | TREE_TYPE(expr_tree), | |
3268 | expr_tree); | |
3269 | } | |
3270 | else | |
3271 | { | |
3272 | gcc_assert(e == Type::lookup_integer_type("int")); | |
3273 | static tree string_to_int_array_fndecl; | |
3274 | ret = Gogo::call_builtin(&string_to_int_array_fndecl, | |
3275 | this->location(), | |
3276 | "__go_string_to_int_array", | |
3277 | 1, | |
3278 | type_tree, | |
3279 | TREE_TYPE(expr_tree), | |
3280 | expr_tree); | |
3281 | } | |
3282 | } | |
3283 | else if ((type->is_unsafe_pointer_type() | |
3284 | && expr_type->points_to() != NULL) | |
3285 | || (expr_type->is_unsafe_pointer_type() | |
3286 | && type->points_to() != NULL)) | |
3287 | ret = fold_convert(type_tree, expr_tree); | |
3288 | else if (type->is_unsafe_pointer_type() | |
3289 | && expr_type->integer_type() != NULL) | |
3290 | ret = convert_to_pointer(type_tree, expr_tree); | |
3291 | else if (this->may_convert_function_types_ | |
3292 | && type->function_type() != NULL | |
3293 | && expr_type->function_type() != NULL) | |
3294 | ret = fold_convert_loc(this->location(), type_tree, expr_tree); | |
3295 | else | |
3296 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3297 | expr_tree, this->location()); | |
3298 | ||
3299 | return ret; | |
3300 | } | |
3301 | ||
3302 | // Output a type conversion in a constant expression. | |
3303 | ||
3304 | void | |
3305 | Type_conversion_expression::do_export(Export* exp) const | |
3306 | { | |
3307 | exp->write_c_string("convert("); | |
3308 | exp->write_type(this->type_); | |
3309 | exp->write_c_string(", "); | |
3310 | this->expr_->export_expression(exp); | |
3311 | exp->write_c_string(")"); | |
3312 | } | |
3313 | ||
3314 | // Import a type conversion or a struct construction. | |
3315 | ||
3316 | Expression* | |
3317 | Type_conversion_expression::do_import(Import* imp) | |
3318 | { | |
3319 | imp->require_c_string("convert("); | |
3320 | Type* type = imp->read_type(); | |
3321 | imp->require_c_string(", "); | |
3322 | Expression* val = Expression::import_expression(imp); | |
3323 | imp->require_c_string(")"); | |
3324 | return Expression::make_cast(type, val, imp->location()); | |
3325 | } | |
3326 | ||
3327 | // Make a type cast expression. | |
3328 | ||
3329 | Expression* | |
3330 | Expression::make_cast(Type* type, Expression* val, source_location location) | |
3331 | { | |
3332 | if (type->is_error_type() || val->is_error_expression()) | |
3333 | return Expression::make_error(location); | |
3334 | return new Type_conversion_expression(type, val, location); | |
3335 | } | |
3336 | ||
3337 | // Unary expressions. | |
3338 | ||
3339 | class Unary_expression : public Expression | |
3340 | { | |
3341 | public: | |
3342 | Unary_expression(Operator op, Expression* expr, source_location location) | |
3343 | : Expression(EXPRESSION_UNARY, location), | |
3344 | op_(op), escapes_(true), expr_(expr) | |
3345 | { } | |
3346 | ||
3347 | // Return the operator. | |
3348 | Operator | |
3349 | op() const | |
3350 | { return this->op_; } | |
3351 | ||
3352 | // Return the operand. | |
3353 | Expression* | |
3354 | operand() const | |
3355 | { return this->expr_; } | |
3356 | ||
3357 | // Record that an address expression does not escape. | |
3358 | void | |
3359 | set_does_not_escape() | |
3360 | { | |
3361 | gcc_assert(this->op_ == OPERATOR_AND); | |
3362 | this->escapes_ = false; | |
3363 | } | |
3364 | ||
3365 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3366 | // could be done, false if not. | |
3367 | static bool | |
3368 | eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val, | |
3369 | source_location); | |
3370 | ||
3371 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3372 | // could be done, false if not. | |
3373 | static bool | |
3374 | eval_float(Operator op, mpfr_t uval, mpfr_t val); | |
3375 | ||
3376 | // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return | |
3377 | // true if this could be done, false if not. | |
3378 | static bool | |
3379 | eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real, | |
3380 | mpfr_t imag); | |
3381 | ||
3382 | static Expression* | |
3383 | do_import(Import*); | |
3384 | ||
3385 | protected: | |
3386 | int | |
3387 | do_traverse(Traverse* traverse) | |
3388 | { return Expression::traverse(&this->expr_, traverse); } | |
3389 | ||
3390 | Expression* | |
3391 | do_lower(Gogo*, Named_object*, int); | |
3392 | ||
3393 | bool | |
3394 | do_is_constant() const; | |
3395 | ||
3396 | bool | |
3397 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
3398 | ||
3399 | bool | |
3400 | do_float_constant_value(mpfr_t, Type**) const; | |
3401 | ||
3402 | bool | |
3403 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
3404 | ||
3405 | Type* | |
3406 | do_type(); | |
3407 | ||
3408 | void | |
3409 | do_determine_type(const Type_context*); | |
3410 | ||
3411 | void | |
3412 | do_check_types(Gogo*); | |
3413 | ||
3414 | Expression* | |
3415 | do_copy() | |
3416 | { | |
3417 | return Expression::make_unary(this->op_, this->expr_->copy(), | |
3418 | this->location()); | |
3419 | } | |
3420 | ||
3421 | bool | |
3422 | do_is_addressable() const | |
3423 | { return this->op_ == OPERATOR_MULT; } | |
3424 | ||
3425 | tree | |
3426 | do_get_tree(Translate_context*); | |
3427 | ||
3428 | void | |
3429 | do_export(Export*) const; | |
3430 | ||
3431 | private: | |
3432 | // The unary operator to apply. | |
3433 | Operator op_; | |
3434 | // Normally true. False if this is an address expression which does | |
3435 | // not escape the current function. | |
3436 | bool escapes_; | |
3437 | // The operand. | |
3438 | Expression* expr_; | |
3439 | }; | |
3440 | ||
3441 | // If we are taking the address of a composite literal, and the | |
3442 | // contents are not constant, then we want to make a heap composite | |
3443 | // instead. | |
3444 | ||
3445 | Expression* | |
3446 | Unary_expression::do_lower(Gogo*, Named_object*, int) | |
3447 | { | |
3448 | source_location loc = this->location(); | |
3449 | Operator op = this->op_; | |
3450 | Expression* expr = this->expr_; | |
3451 | ||
3452 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3453 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3454 | ||
3455 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3456 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3457 | // analysis here? This case is found in math/unsafe.go and is | |
3458 | // therefore worth special casing. | |
3459 | if (op == OPERATOR_MULT) | |
3460 | { | |
3461 | Expression* e = expr; | |
3462 | while (e->classification() == EXPRESSION_CONVERSION) | |
3463 | { | |
3464 | Type_conversion_expression* te | |
3465 | = static_cast<Type_conversion_expression*>(e); | |
3466 | e = te->expr(); | |
3467 | } | |
3468 | ||
3469 | if (e->classification() == EXPRESSION_UNARY) | |
3470 | { | |
3471 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3472 | if (ue->op_ == OPERATOR_AND) | |
3473 | { | |
3474 | if (e == expr) | |
3475 | { | |
3476 | // *&x == x. | |
3477 | return ue->expr_; | |
3478 | } | |
3479 | ue->set_does_not_escape(); | |
3480 | } | |
3481 | } | |
3482 | } | |
3483 | ||
3484 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS | |
3485 | || op == OPERATOR_NOT || op == OPERATOR_XOR) | |
3486 | { | |
3487 | Expression* ret = NULL; | |
3488 | ||
3489 | mpz_t eval; | |
3490 | mpz_init(eval); | |
3491 | Type* etype; | |
3492 | if (expr->integer_constant_value(false, eval, &etype)) | |
3493 | { | |
3494 | mpz_t val; | |
3495 | mpz_init(val); | |
3496 | if (Unary_expression::eval_integer(op, etype, eval, val, loc)) | |
3497 | ret = Expression::make_integer(&val, etype, loc); | |
3498 | mpz_clear(val); | |
3499 | } | |
3500 | mpz_clear(eval); | |
3501 | if (ret != NULL) | |
3502 | return ret; | |
3503 | ||
3504 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS) | |
3505 | { | |
3506 | mpfr_t fval; | |
3507 | mpfr_init(fval); | |
3508 | Type* ftype; | |
3509 | if (expr->float_constant_value(fval, &ftype)) | |
3510 | { | |
3511 | mpfr_t val; | |
3512 | mpfr_init(val); | |
3513 | if (Unary_expression::eval_float(op, fval, val)) | |
3514 | ret = Expression::make_float(&val, ftype, loc); | |
3515 | mpfr_clear(val); | |
3516 | } | |
3517 | if (ret != NULL) | |
3518 | { | |
3519 | mpfr_clear(fval); | |
3520 | return ret; | |
3521 | } | |
3522 | ||
3523 | mpfr_t ival; | |
3524 | mpfr_init(ival); | |
3525 | if (expr->complex_constant_value(fval, ival, &ftype)) | |
3526 | { | |
3527 | mpfr_t real; | |
3528 | mpfr_t imag; | |
3529 | mpfr_init(real); | |
3530 | mpfr_init(imag); | |
3531 | if (Unary_expression::eval_complex(op, fval, ival, real, imag)) | |
3532 | ret = Expression::make_complex(&real, &imag, ftype, loc); | |
3533 | mpfr_clear(real); | |
3534 | mpfr_clear(imag); | |
3535 | } | |
3536 | mpfr_clear(ival); | |
3537 | mpfr_clear(fval); | |
3538 | if (ret != NULL) | |
3539 | return ret; | |
3540 | } | |
3541 | } | |
3542 | ||
3543 | return this; | |
3544 | } | |
3545 | ||
3546 | // Return whether a unary expression is a constant. | |
3547 | ||
3548 | bool | |
3549 | Unary_expression::do_is_constant() const | |
3550 | { | |
3551 | if (this->op_ == OPERATOR_MULT) | |
3552 | { | |
3553 | // Indirecting through a pointer is only constant if the object | |
3554 | // to which the expression points is constant, but we currently | |
3555 | // have no way to determine that. | |
3556 | return false; | |
3557 | } | |
3558 | else if (this->op_ == OPERATOR_AND) | |
3559 | { | |
3560 | // Taking the address of a variable is constant if it is a | |
3561 | // global variable, not constant otherwise. In other cases | |
3562 | // taking the address is probably not a constant. | |
3563 | Var_expression* ve = this->expr_->var_expression(); | |
3564 | if (ve != NULL) | |
3565 | { | |
3566 | Named_object* no = ve->named_object(); | |
3567 | return no->is_variable() && no->var_value()->is_global(); | |
3568 | } | |
3569 | return false; | |
3570 | } | |
3571 | else | |
3572 | return this->expr_->is_constant(); | |
3573 | } | |
3574 | ||
3575 | // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of | |
3576 | // UVAL, if known; it may be NULL. Return true if this could be done, | |
3577 | // false if not. | |
3578 | ||
3579 | bool | |
3580 | Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val, | |
3581 | source_location location) | |
3582 | { | |
3583 | switch (op) | |
3584 | { | |
3585 | case OPERATOR_PLUS: | |
3586 | mpz_set(val, uval); | |
3587 | return true; | |
3588 | case OPERATOR_MINUS: | |
3589 | mpz_neg(val, uval); | |
3590 | return Integer_expression::check_constant(val, utype, location); | |
3591 | case OPERATOR_NOT: | |
3592 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); | |
3593 | return true; | |
3594 | case OPERATOR_XOR: | |
3595 | if (utype == NULL | |
3596 | || utype->integer_type() == NULL | |
3597 | || utype->integer_type()->is_abstract()) | |
3598 | mpz_com(val, uval); | |
3599 | else | |
3600 | { | |
3601 | // The number of HOST_WIDE_INTs that it takes to represent | |
3602 | // UVAL. | |
3603 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3604 | + HOST_BITS_PER_WIDE_INT | |
3605 | - 1) | |
3606 | / HOST_BITS_PER_WIDE_INT); | |
3607 | ||
3608 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; | |
3609 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3610 | ||
3611 | size_t ecount; | |
3612 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
3613 | gcc_assert(ecount <= count); | |
3614 | ||
3615 | // Trim down to the number of words required by the type. | |
3616 | size_t obits = utype->integer_type()->bits(); | |
3617 | if (!utype->integer_type()->is_unsigned()) | |
3618 | ++obits; | |
3619 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
3620 | / HOST_BITS_PER_WIDE_INT); | |
3621 | gcc_assert(ocount <= ocount); | |
3622 | ||
3623 | for (size_t i = 0; i < ocount; ++i) | |
3624 | phwi[i] = ~phwi[i]; | |
3625 | ||
3626 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
3627 | if (clearbits != 0) | |
3628 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
3629 | >> clearbits); | |
3630 | ||
3631 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
3632 | ||
3633 | delete[] phwi; | |
3634 | } | |
3635 | return Integer_expression::check_constant(val, utype, location); | |
3636 | case OPERATOR_AND: | |
3637 | case OPERATOR_MULT: | |
3638 | return false; | |
3639 | default: | |
3640 | gcc_unreachable(); | |
3641 | } | |
3642 | } | |
3643 | ||
3644 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3645 | // could be done, false if not. | |
3646 | ||
3647 | bool | |
3648 | Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val) | |
3649 | { | |
3650 | switch (op) | |
3651 | { | |
3652 | case OPERATOR_PLUS: | |
3653 | mpfr_set(val, uval, GMP_RNDN); | |
3654 | return true; | |
3655 | case OPERATOR_MINUS: | |
3656 | mpfr_neg(val, uval, GMP_RNDN); | |
3657 | return true; | |
3658 | case OPERATOR_NOT: | |
3659 | case OPERATOR_XOR: | |
3660 | case OPERATOR_AND: | |
3661 | case OPERATOR_MULT: | |
3662 | return false; | |
3663 | default: | |
3664 | gcc_unreachable(); | |
3665 | } | |
3666 | } | |
3667 | ||
3668 | // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true | |
3669 | // if this could be done, false if not. | |
3670 | ||
3671 | bool | |
3672 | Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival, | |
3673 | mpfr_t real, mpfr_t imag) | |
3674 | { | |
3675 | switch (op) | |
3676 | { | |
3677 | case OPERATOR_PLUS: | |
3678 | mpfr_set(real, rval, GMP_RNDN); | |
3679 | mpfr_set(imag, ival, GMP_RNDN); | |
3680 | return true; | |
3681 | case OPERATOR_MINUS: | |
3682 | mpfr_neg(real, rval, GMP_RNDN); | |
3683 | mpfr_neg(imag, ival, GMP_RNDN); | |
3684 | return true; | |
3685 | case OPERATOR_NOT: | |
3686 | case OPERATOR_XOR: | |
3687 | case OPERATOR_AND: | |
3688 | case OPERATOR_MULT: | |
3689 | return false; | |
3690 | default: | |
3691 | gcc_unreachable(); | |
3692 | } | |
3693 | } | |
3694 | ||
3695 | // Return the integral constant value of a unary expression, if it has one. | |
3696 | ||
3697 | bool | |
3698 | Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
3699 | Type** ptype) const | |
3700 | { | |
3701 | mpz_t uval; | |
3702 | mpz_init(uval); | |
3703 | bool ret; | |
3704 | if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype)) | |
3705 | ret = false; | |
3706 | else | |
3707 | ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val, | |
3708 | this->location()); | |
3709 | mpz_clear(uval); | |
3710 | return ret; | |
3711 | } | |
3712 | ||
3713 | // Return the floating point constant value of a unary expression, if | |
3714 | // it has one. | |
3715 | ||
3716 | bool | |
3717 | Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
3718 | { | |
3719 | mpfr_t uval; | |
3720 | mpfr_init(uval); | |
3721 | bool ret; | |
3722 | if (!this->expr_->float_constant_value(uval, ptype)) | |
3723 | ret = false; | |
3724 | else | |
3725 | ret = Unary_expression::eval_float(this->op_, uval, val); | |
3726 | mpfr_clear(uval); | |
3727 | return ret; | |
3728 | } | |
3729 | ||
3730 | // Return the complex constant value of a unary expression, if it has | |
3731 | // one. | |
3732 | ||
3733 | bool | |
3734 | Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
3735 | Type** ptype) const | |
3736 | { | |
3737 | mpfr_t rval; | |
3738 | mpfr_t ival; | |
3739 | mpfr_init(rval); | |
3740 | mpfr_init(ival); | |
3741 | bool ret; | |
3742 | if (!this->expr_->complex_constant_value(rval, ival, ptype)) | |
3743 | ret = false; | |
3744 | else | |
3745 | ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag); | |
3746 | mpfr_clear(rval); | |
3747 | mpfr_clear(ival); | |
3748 | return ret; | |
3749 | } | |
3750 | ||
3751 | // Return the type of a unary expression. | |
3752 | ||
3753 | Type* | |
3754 | Unary_expression::do_type() | |
3755 | { | |
3756 | switch (this->op_) | |
3757 | { | |
3758 | case OPERATOR_PLUS: | |
3759 | case OPERATOR_MINUS: | |
3760 | case OPERATOR_NOT: | |
3761 | case OPERATOR_XOR: | |
3762 | return this->expr_->type(); | |
3763 | ||
3764 | case OPERATOR_AND: | |
3765 | return Type::make_pointer_type(this->expr_->type()); | |
3766 | ||
3767 | case OPERATOR_MULT: | |
3768 | { | |
3769 | Type* subtype = this->expr_->type(); | |
3770 | Type* points_to = subtype->points_to(); | |
3771 | if (points_to == NULL) | |
3772 | return Type::make_error_type(); | |
3773 | return points_to; | |
3774 | } | |
3775 | ||
3776 | default: | |
3777 | gcc_unreachable(); | |
3778 | } | |
3779 | } | |
3780 | ||
3781 | // Determine abstract types for a unary expression. | |
3782 | ||
3783 | void | |
3784 | Unary_expression::do_determine_type(const Type_context* context) | |
3785 | { | |
3786 | switch (this->op_) | |
3787 | { | |
3788 | case OPERATOR_PLUS: | |
3789 | case OPERATOR_MINUS: | |
3790 | case OPERATOR_NOT: | |
3791 | case OPERATOR_XOR: | |
3792 | this->expr_->determine_type(context); | |
3793 | break; | |
3794 | ||
3795 | case OPERATOR_AND: | |
3796 | // Taking the address of something. | |
3797 | { | |
3798 | Type* subtype = (context->type == NULL | |
3799 | ? NULL | |
3800 | : context->type->points_to()); | |
3801 | Type_context subcontext(subtype, false); | |
3802 | this->expr_->determine_type(&subcontext); | |
3803 | } | |
3804 | break; | |
3805 | ||
3806 | case OPERATOR_MULT: | |
3807 | // Indirecting through a pointer. | |
3808 | { | |
3809 | Type* subtype = (context->type == NULL | |
3810 | ? NULL | |
3811 | : Type::make_pointer_type(context->type)); | |
3812 | Type_context subcontext(subtype, false); | |
3813 | this->expr_->determine_type(&subcontext); | |
3814 | } | |
3815 | break; | |
3816 | ||
3817 | default: | |
3818 | gcc_unreachable(); | |
3819 | } | |
3820 | } | |
3821 | ||
3822 | // Check types for a unary expression. | |
3823 | ||
3824 | void | |
3825 | Unary_expression::do_check_types(Gogo*) | |
3826 | { | |
9fe897ef | 3827 | Type* type = this->expr_->type(); |
3828 | if (type->is_error_type()) | |
3829 | { | |
3830 | this->set_is_error(); | |
3831 | return; | |
3832 | } | |
3833 | ||
e440a328 | 3834 | switch (this->op_) |
3835 | { | |
3836 | case OPERATOR_PLUS: | |
3837 | case OPERATOR_MINUS: | |
9fe897ef | 3838 | if (type->integer_type() == NULL |
3839 | && type->float_type() == NULL | |
3840 | && type->complex_type() == NULL) | |
3841 | this->report_error(_("expected numeric type")); | |
e440a328 | 3842 | break; |
3843 | ||
3844 | case OPERATOR_NOT: | |
3845 | case OPERATOR_XOR: | |
9fe897ef | 3846 | if (type->integer_type() == NULL |
3847 | && !type->is_boolean_type()) | |
3848 | this->report_error(_("expected integer or boolean type")); | |
e440a328 | 3849 | break; |
3850 | ||
3851 | case OPERATOR_AND: | |
3852 | if (!this->expr_->is_addressable()) | |
3853 | this->report_error(_("invalid operand for unary %<&%>")); | |
3854 | else | |
3855 | this->expr_->address_taken(this->escapes_); | |
3856 | break; | |
3857 | ||
3858 | case OPERATOR_MULT: | |
3859 | // Indirecting through a pointer. | |
9fe897ef | 3860 | if (type->points_to() == NULL) |
3861 | this->report_error(_("expected pointer")); | |
e440a328 | 3862 | break; |
3863 | ||
3864 | default: | |
3865 | gcc_unreachable(); | |
3866 | } | |
3867 | } | |
3868 | ||
3869 | // Get a tree for a unary expression. | |
3870 | ||
3871 | tree | |
3872 | Unary_expression::do_get_tree(Translate_context* context) | |
3873 | { | |
3874 | tree expr = this->expr_->get_tree(context); | |
3875 | if (expr == error_mark_node) | |
3876 | return error_mark_node; | |
3877 | ||
3878 | source_location loc = this->location(); | |
3879 | switch (this->op_) | |
3880 | { | |
3881 | case OPERATOR_PLUS: | |
3882 | return expr; | |
3883 | ||
3884 | case OPERATOR_MINUS: | |
3885 | { | |
3886 | tree type = TREE_TYPE(expr); | |
3887 | tree compute_type = excess_precision_type(type); | |
3888 | if (compute_type != NULL_TREE) | |
3889 | expr = ::convert(compute_type, expr); | |
3890 | tree ret = fold_build1_loc(loc, NEGATE_EXPR, | |
3891 | (compute_type != NULL_TREE | |
3892 | ? compute_type | |
3893 | : type), | |
3894 | expr); | |
3895 | if (compute_type != NULL_TREE) | |
3896 | ret = ::convert(type, ret); | |
3897 | return ret; | |
3898 | } | |
3899 | ||
3900 | case OPERATOR_NOT: | |
3901 | if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE) | |
3902 | return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr); | |
3903 | else | |
3904 | return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr, | |
3905 | build_int_cst(TREE_TYPE(expr), 0)); | |
3906 | ||
3907 | case OPERATOR_XOR: | |
3908 | return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr); | |
3909 | ||
3910 | case OPERATOR_AND: | |
3911 | // We should not see a non-constant constructor here; cases | |
3912 | // where we would see one should have been moved onto the heap | |
3913 | // at parse time. Taking the address of a nonconstant | |
3914 | // constructor will not do what the programmer expects. | |
3915 | gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr)); | |
3916 | gcc_assert(TREE_CODE(expr) != ADDR_EXPR); | |
3917 | ||
3918 | // Build a decl for a constant constructor. | |
3919 | if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr)) | |
3920 | { | |
3921 | tree decl = build_decl(this->location(), VAR_DECL, | |
3922 | create_tmp_var_name("C"), TREE_TYPE(expr)); | |
3923 | DECL_EXTERNAL(decl) = 0; | |
3924 | TREE_PUBLIC(decl) = 0; | |
3925 | TREE_READONLY(decl) = 1; | |
3926 | TREE_CONSTANT(decl) = 1; | |
3927 | TREE_STATIC(decl) = 1; | |
3928 | TREE_ADDRESSABLE(decl) = 1; | |
3929 | DECL_ARTIFICIAL(decl) = 1; | |
3930 | DECL_INITIAL(decl) = expr; | |
3931 | rest_of_decl_compilation(decl, 1, 0); | |
3932 | expr = decl; | |
3933 | } | |
3934 | ||
3935 | return build_fold_addr_expr_loc(loc, expr); | |
3936 | ||
3937 | case OPERATOR_MULT: | |
3938 | { | |
3939 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr))); | |
3940 | ||
3941 | // If we are dereferencing the pointer to a large struct, we | |
3942 | // need to check for nil. We don't bother to check for small | |
3943 | // structs because we expect the system to crash on a nil | |
3944 | // pointer dereference. | |
3945 | HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr))); | |
3946 | if (s == -1 || s >= 4096) | |
3947 | { | |
3948 | if (!DECL_P(expr)) | |
3949 | expr = save_expr(expr); | |
3950 | tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node, | |
3951 | expr, | |
3952 | fold_convert(TREE_TYPE(expr), | |
3953 | null_pointer_node)); | |
3954 | tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, | |
3955 | loc); | |
3956 | expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr), | |
3957 | build3(COND_EXPR, void_type_node, | |
3958 | compare, crash, NULL_TREE), | |
3959 | expr); | |
3960 | } | |
3961 | ||
3962 | // If the type of EXPR is a recursive pointer type, then we | |
3963 | // need to insert a cast before indirecting. | |
3964 | if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node) | |
3965 | { | |
3966 | Type* pt = this->expr_->type()->points_to(); | |
3967 | tree ind = pt->get_tree(context->gogo()); | |
3968 | expr = fold_convert_loc(loc, build_pointer_type(ind), expr); | |
3969 | } | |
3970 | ||
3971 | return build_fold_indirect_ref_loc(loc, expr); | |
3972 | } | |
3973 | ||
3974 | default: | |
3975 | gcc_unreachable(); | |
3976 | } | |
3977 | } | |
3978 | ||
3979 | // Export a unary expression. | |
3980 | ||
3981 | void | |
3982 | Unary_expression::do_export(Export* exp) const | |
3983 | { | |
3984 | switch (this->op_) | |
3985 | { | |
3986 | case OPERATOR_PLUS: | |
3987 | exp->write_c_string("+ "); | |
3988 | break; | |
3989 | case OPERATOR_MINUS: | |
3990 | exp->write_c_string("- "); | |
3991 | break; | |
3992 | case OPERATOR_NOT: | |
3993 | exp->write_c_string("! "); | |
3994 | break; | |
3995 | case OPERATOR_XOR: | |
3996 | exp->write_c_string("^ "); | |
3997 | break; | |
3998 | case OPERATOR_AND: | |
3999 | case OPERATOR_MULT: | |
4000 | default: | |
4001 | gcc_unreachable(); | |
4002 | } | |
4003 | this->expr_->export_expression(exp); | |
4004 | } | |
4005 | ||
4006 | // Import a unary expression. | |
4007 | ||
4008 | Expression* | |
4009 | Unary_expression::do_import(Import* imp) | |
4010 | { | |
4011 | Operator op; | |
4012 | switch (imp->get_char()) | |
4013 | { | |
4014 | case '+': | |
4015 | op = OPERATOR_PLUS; | |
4016 | break; | |
4017 | case '-': | |
4018 | op = OPERATOR_MINUS; | |
4019 | break; | |
4020 | case '!': | |
4021 | op = OPERATOR_NOT; | |
4022 | break; | |
4023 | case '^': | |
4024 | op = OPERATOR_XOR; | |
4025 | break; | |
4026 | default: | |
4027 | gcc_unreachable(); | |
4028 | } | |
4029 | imp->require_c_string(" "); | |
4030 | Expression* expr = Expression::import_expression(imp); | |
4031 | return Expression::make_unary(op, expr, imp->location()); | |
4032 | } | |
4033 | ||
4034 | // Make a unary expression. | |
4035 | ||
4036 | Expression* | |
4037 | Expression::make_unary(Operator op, Expression* expr, source_location location) | |
4038 | { | |
4039 | return new Unary_expression(op, expr, location); | |
4040 | } | |
4041 | ||
4042 | // If this is an indirection through a pointer, return the expression | |
4043 | // being pointed through. Otherwise return this. | |
4044 | ||
4045 | Expression* | |
4046 | Expression::deref() | |
4047 | { | |
4048 | if (this->classification_ == EXPRESSION_UNARY) | |
4049 | { | |
4050 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4051 | if (ue->op() == OPERATOR_MULT) | |
4052 | return ue->operand(); | |
4053 | } | |
4054 | return this; | |
4055 | } | |
4056 | ||
4057 | // Class Binary_expression. | |
4058 | ||
4059 | // Traversal. | |
4060 | ||
4061 | int | |
4062 | Binary_expression::do_traverse(Traverse* traverse) | |
4063 | { | |
4064 | int t = Expression::traverse(&this->left_, traverse); | |
4065 | if (t == TRAVERSE_EXIT) | |
4066 | return TRAVERSE_EXIT; | |
4067 | return Expression::traverse(&this->right_, traverse); | |
4068 | } | |
4069 | ||
4070 | // Compare integer constants according to OP. | |
4071 | ||
4072 | bool | |
4073 | Binary_expression::compare_integer(Operator op, mpz_t left_val, | |
4074 | mpz_t right_val) | |
4075 | { | |
4076 | int i = mpz_cmp(left_val, right_val); | |
4077 | switch (op) | |
4078 | { | |
4079 | case OPERATOR_EQEQ: | |
4080 | return i == 0; | |
4081 | case OPERATOR_NOTEQ: | |
4082 | return i != 0; | |
4083 | case OPERATOR_LT: | |
4084 | return i < 0; | |
4085 | case OPERATOR_LE: | |
4086 | return i <= 0; | |
4087 | case OPERATOR_GT: | |
4088 | return i > 0; | |
4089 | case OPERATOR_GE: | |
4090 | return i >= 0; | |
4091 | default: | |
4092 | gcc_unreachable(); | |
4093 | } | |
4094 | } | |
4095 | ||
4096 | // Compare floating point constants according to OP. | |
4097 | ||
4098 | bool | |
4099 | Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val, | |
4100 | mpfr_t right_val) | |
4101 | { | |
4102 | int i; | |
4103 | if (type == NULL) | |
4104 | i = mpfr_cmp(left_val, right_val); | |
4105 | else | |
4106 | { | |
4107 | mpfr_t lv; | |
4108 | mpfr_init_set(lv, left_val, GMP_RNDN); | |
4109 | mpfr_t rv; | |
4110 | mpfr_init_set(rv, right_val, GMP_RNDN); | |
4111 | Float_expression::constrain_float(lv, type); | |
4112 | Float_expression::constrain_float(rv, type); | |
4113 | i = mpfr_cmp(lv, rv); | |
4114 | mpfr_clear(lv); | |
4115 | mpfr_clear(rv); | |
4116 | } | |
4117 | switch (op) | |
4118 | { | |
4119 | case OPERATOR_EQEQ: | |
4120 | return i == 0; | |
4121 | case OPERATOR_NOTEQ: | |
4122 | return i != 0; | |
4123 | case OPERATOR_LT: | |
4124 | return i < 0; | |
4125 | case OPERATOR_LE: | |
4126 | return i <= 0; | |
4127 | case OPERATOR_GT: | |
4128 | return i > 0; | |
4129 | case OPERATOR_GE: | |
4130 | return i >= 0; | |
4131 | default: | |
4132 | gcc_unreachable(); | |
4133 | } | |
4134 | } | |
4135 | ||
4136 | // Compare complex constants according to OP. Complex numbers may | |
4137 | // only be compared for equality. | |
4138 | ||
4139 | bool | |
4140 | Binary_expression::compare_complex(Operator op, Type* type, | |
4141 | mpfr_t left_real, mpfr_t left_imag, | |
4142 | mpfr_t right_real, mpfr_t right_imag) | |
4143 | { | |
4144 | bool is_equal; | |
4145 | if (type == NULL) | |
4146 | is_equal = (mpfr_cmp(left_real, right_real) == 0 | |
4147 | && mpfr_cmp(left_imag, right_imag) == 0); | |
4148 | else | |
4149 | { | |
4150 | mpfr_t lr; | |
4151 | mpfr_t li; | |
4152 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4153 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4154 | mpfr_t rr; | |
4155 | mpfr_t ri; | |
4156 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4157 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4158 | Complex_expression::constrain_complex(lr, li, type); | |
4159 | Complex_expression::constrain_complex(rr, ri, type); | |
4160 | is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0; | |
4161 | mpfr_clear(lr); | |
4162 | mpfr_clear(li); | |
4163 | mpfr_clear(rr); | |
4164 | mpfr_clear(ri); | |
4165 | } | |
4166 | switch (op) | |
4167 | { | |
4168 | case OPERATOR_EQEQ: | |
4169 | return is_equal; | |
4170 | case OPERATOR_NOTEQ: | |
4171 | return !is_equal; | |
4172 | default: | |
4173 | gcc_unreachable(); | |
4174 | } | |
4175 | } | |
4176 | ||
4177 | // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. | |
4178 | // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of | |
4179 | // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if | |
4180 | // this could be done, false if not. | |
4181 | ||
4182 | bool | |
4183 | Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val, | |
4184 | Type* right_type, mpz_t right_val, | |
4185 | source_location location, mpz_t val) | |
4186 | { | |
4187 | bool is_shift_op = false; | |
4188 | switch (op) | |
4189 | { | |
4190 | case OPERATOR_OROR: | |
4191 | case OPERATOR_ANDAND: | |
4192 | case OPERATOR_EQEQ: | |
4193 | case OPERATOR_NOTEQ: | |
4194 | case OPERATOR_LT: | |
4195 | case OPERATOR_LE: | |
4196 | case OPERATOR_GT: | |
4197 | case OPERATOR_GE: | |
4198 | // These return boolean values. We should probably handle them | |
4199 | // anyhow in case a type conversion is used on the result. | |
4200 | return false; | |
4201 | case OPERATOR_PLUS: | |
4202 | mpz_add(val, left_val, right_val); | |
4203 | break; | |
4204 | case OPERATOR_MINUS: | |
4205 | mpz_sub(val, left_val, right_val); | |
4206 | break; | |
4207 | case OPERATOR_OR: | |
4208 | mpz_ior(val, left_val, right_val); | |
4209 | break; | |
4210 | case OPERATOR_XOR: | |
4211 | mpz_xor(val, left_val, right_val); | |
4212 | break; | |
4213 | case OPERATOR_MULT: | |
4214 | mpz_mul(val, left_val, right_val); | |
4215 | break; | |
4216 | case OPERATOR_DIV: | |
4217 | if (mpz_sgn(right_val) != 0) | |
4218 | mpz_tdiv_q(val, left_val, right_val); | |
4219 | else | |
4220 | { | |
4221 | error_at(location, "division by zero"); | |
4222 | mpz_set_ui(val, 0); | |
4223 | return true; | |
4224 | } | |
4225 | break; | |
4226 | case OPERATOR_MOD: | |
4227 | if (mpz_sgn(right_val) != 0) | |
4228 | mpz_tdiv_r(val, left_val, right_val); | |
4229 | else | |
4230 | { | |
4231 | error_at(location, "division by zero"); | |
4232 | mpz_set_ui(val, 0); | |
4233 | return true; | |
4234 | } | |
4235 | break; | |
4236 | case OPERATOR_LSHIFT: | |
4237 | { | |
4238 | unsigned long shift = mpz_get_ui(right_val); | |
4239 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4240 | { | |
4241 | error_at(location, "shift count overflow"); | |
4242 | mpz_set_ui(val, 0); | |
4243 | return true; | |
4244 | } | |
4245 | mpz_mul_2exp(val, left_val, shift); | |
4246 | is_shift_op = true; | |
4247 | break; | |
4248 | } | |
4249 | break; | |
4250 | case OPERATOR_RSHIFT: | |
4251 | { | |
4252 | unsigned long shift = mpz_get_ui(right_val); | |
4253 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4254 | { | |
4255 | error_at(location, "shift count overflow"); | |
4256 | mpz_set_ui(val, 0); | |
4257 | return true; | |
4258 | } | |
4259 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4260 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4261 | else | |
4262 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4263 | is_shift_op = true; | |
4264 | break; | |
4265 | } | |
4266 | break; | |
4267 | case OPERATOR_AND: | |
4268 | mpz_and(val, left_val, right_val); | |
4269 | break; | |
4270 | case OPERATOR_BITCLEAR: | |
4271 | { | |
4272 | mpz_t tval; | |
4273 | mpz_init(tval); | |
4274 | mpz_com(tval, right_val); | |
4275 | mpz_and(val, left_val, tval); | |
4276 | mpz_clear(tval); | |
4277 | } | |
4278 | break; | |
4279 | default: | |
4280 | gcc_unreachable(); | |
4281 | } | |
4282 | ||
4283 | Type* type = left_type; | |
4284 | if (!is_shift_op) | |
4285 | { | |
4286 | if (type == NULL) | |
4287 | type = right_type; | |
4288 | else if (type != right_type && right_type != NULL) | |
4289 | { | |
4290 | if (type->is_abstract()) | |
4291 | type = right_type; | |
4292 | else if (!right_type->is_abstract()) | |
4293 | { | |
4294 | // This look like a type error which should be diagnosed | |
4295 | // elsewhere. Don't do anything here, to avoid an | |
4296 | // unhelpful chain of error messages. | |
4297 | return true; | |
4298 | } | |
4299 | } | |
4300 | } | |
4301 | ||
4302 | if (type != NULL && !type->is_abstract()) | |
4303 | { | |
4304 | // We have to check the operands too, as we have implicitly | |
4305 | // coerced them to TYPE. | |
4306 | if ((type != left_type | |
4307 | && !Integer_expression::check_constant(left_val, type, location)) | |
4308 | || (!is_shift_op | |
4309 | && type != right_type | |
4310 | && !Integer_expression::check_constant(right_val, type, | |
4311 | location)) | |
4312 | || !Integer_expression::check_constant(val, type, location)) | |
4313 | mpz_set_ui(val, 0); | |
4314 | } | |
4315 | ||
4316 | return true; | |
4317 | } | |
4318 | ||
4319 | // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. | |
4320 | // Return true if this could be done, false if not. | |
4321 | ||
4322 | bool | |
4323 | Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val, | |
4324 | Type* right_type, mpfr_t right_val, | |
4325 | mpfr_t val, source_location location) | |
4326 | { | |
4327 | switch (op) | |
4328 | { | |
4329 | case OPERATOR_OROR: | |
4330 | case OPERATOR_ANDAND: | |
4331 | case OPERATOR_EQEQ: | |
4332 | case OPERATOR_NOTEQ: | |
4333 | case OPERATOR_LT: | |
4334 | case OPERATOR_LE: | |
4335 | case OPERATOR_GT: | |
4336 | case OPERATOR_GE: | |
4337 | // These return boolean values. We should probably handle them | |
4338 | // anyhow in case a type conversion is used on the result. | |
4339 | return false; | |
4340 | case OPERATOR_PLUS: | |
4341 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4342 | break; | |
4343 | case OPERATOR_MINUS: | |
4344 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4345 | break; | |
4346 | case OPERATOR_OR: | |
4347 | case OPERATOR_XOR: | |
4348 | case OPERATOR_AND: | |
4349 | case OPERATOR_BITCLEAR: | |
4350 | return false; | |
4351 | case OPERATOR_MULT: | |
4352 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4353 | break; | |
4354 | case OPERATOR_DIV: | |
4355 | if (mpfr_zero_p(right_val)) | |
4356 | error_at(location, "division by zero"); | |
4357 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4358 | break; | |
4359 | case OPERATOR_MOD: | |
4360 | return false; | |
4361 | case OPERATOR_LSHIFT: | |
4362 | case OPERATOR_RSHIFT: | |
4363 | return false; | |
4364 | default: | |
4365 | gcc_unreachable(); | |
4366 | } | |
4367 | ||
4368 | Type* type = left_type; | |
4369 | if (type == NULL) | |
4370 | type = right_type; | |
4371 | else if (type != right_type && right_type != NULL) | |
4372 | { | |
4373 | if (type->is_abstract()) | |
4374 | type = right_type; | |
4375 | else if (!right_type->is_abstract()) | |
4376 | { | |
4377 | // This looks like a type error which should be diagnosed | |
4378 | // elsewhere. Don't do anything here, to avoid an unhelpful | |
4379 | // chain of error messages. | |
4380 | return true; | |
4381 | } | |
4382 | } | |
4383 | ||
4384 | if (type != NULL && !type->is_abstract()) | |
4385 | { | |
4386 | if ((type != left_type | |
4387 | && !Float_expression::check_constant(left_val, type, location)) | |
4388 | || (type != right_type | |
4389 | && !Float_expression::check_constant(right_val, type, | |
4390 | location)) | |
4391 | || !Float_expression::check_constant(val, type, location)) | |
4392 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4393 | } | |
4394 | ||
4395 | return true; | |
4396 | } | |
4397 | ||
4398 | // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and | |
4399 | // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this | |
4400 | // could be done, false if not. | |
4401 | ||
4402 | bool | |
4403 | Binary_expression::eval_complex(Operator op, Type* left_type, | |
4404 | mpfr_t left_real, mpfr_t left_imag, | |
4405 | Type *right_type, | |
4406 | mpfr_t right_real, mpfr_t right_imag, | |
4407 | mpfr_t real, mpfr_t imag, | |
4408 | source_location location) | |
4409 | { | |
4410 | switch (op) | |
4411 | { | |
4412 | case OPERATOR_OROR: | |
4413 | case OPERATOR_ANDAND: | |
4414 | case OPERATOR_EQEQ: | |
4415 | case OPERATOR_NOTEQ: | |
4416 | case OPERATOR_LT: | |
4417 | case OPERATOR_LE: | |
4418 | case OPERATOR_GT: | |
4419 | case OPERATOR_GE: | |
4420 | // These return boolean values and must be handled differently. | |
4421 | return false; | |
4422 | case OPERATOR_PLUS: | |
4423 | mpfr_add(real, left_real, right_real, GMP_RNDN); | |
4424 | mpfr_add(imag, left_imag, right_imag, GMP_RNDN); | |
4425 | break; | |
4426 | case OPERATOR_MINUS: | |
4427 | mpfr_sub(real, left_real, right_real, GMP_RNDN); | |
4428 | mpfr_sub(imag, left_imag, right_imag, GMP_RNDN); | |
4429 | break; | |
4430 | case OPERATOR_OR: | |
4431 | case OPERATOR_XOR: | |
4432 | case OPERATOR_AND: | |
4433 | case OPERATOR_BITCLEAR: | |
4434 | return false; | |
4435 | case OPERATOR_MULT: | |
4436 | { | |
4437 | // You might think that multiplying two complex numbers would | |
4438 | // be simple, and you would be right, until you start to think | |
4439 | // about getting the right answer for infinity. If one | |
4440 | // operand here is infinity and the other is anything other | |
4441 | // than zero or NaN, then we are going to wind up subtracting | |
4442 | // two infinity values. That will give us a NaN, but the | |
4443 | // correct answer is infinity. | |
4444 | ||
4445 | mpfr_t lrrr; | |
4446 | mpfr_init(lrrr); | |
4447 | mpfr_mul(lrrr, left_real, right_real, GMP_RNDN); | |
4448 | ||
4449 | mpfr_t lrri; | |
4450 | mpfr_init(lrri); | |
4451 | mpfr_mul(lrri, left_real, right_imag, GMP_RNDN); | |
4452 | ||
4453 | mpfr_t lirr; | |
4454 | mpfr_init(lirr); | |
4455 | mpfr_mul(lirr, left_imag, right_real, GMP_RNDN); | |
4456 | ||
4457 | mpfr_t liri; | |
4458 | mpfr_init(liri); | |
4459 | mpfr_mul(liri, left_imag, right_imag, GMP_RNDN); | |
4460 | ||
4461 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4462 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4463 | ||
4464 | // If we get NaN on both sides, check whether it should really | |
4465 | // be infinity. The rule is that if either side of the | |
4466 | // complex number is infinity, then the whole value is | |
4467 | // infinity, even if the other side is NaN. So the only case | |
4468 | // we have to fix is the one in which both sides are NaN. | |
4469 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4470 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4471 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4472 | { | |
4473 | bool is_infinity = false; | |
4474 | ||
4475 | mpfr_t lr; | |
4476 | mpfr_t li; | |
4477 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4478 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4479 | ||
4480 | mpfr_t rr; | |
4481 | mpfr_t ri; | |
4482 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4483 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4484 | ||
4485 | // If the left side is infinity, then the result is | |
4486 | // infinity. | |
4487 | if (mpfr_inf_p(lr) || mpfr_inf_p(li)) | |
4488 | { | |
4489 | mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN); | |
4490 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4491 | mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN); | |
4492 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4493 | if (mpfr_nan_p(rr)) | |
4494 | { | |
4495 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4496 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4497 | } | |
4498 | if (mpfr_nan_p(ri)) | |
4499 | { | |
4500 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4501 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4502 | } | |
4503 | is_infinity = true; | |
4504 | } | |
4505 | ||
4506 | // If the right side is infinity, then the result is | |
4507 | // infinity. | |
4508 | if (mpfr_inf_p(rr) || mpfr_inf_p(ri)) | |
4509 | { | |
4510 | mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4511 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4512 | mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
4513 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4514 | if (mpfr_nan_p(lr)) | |
4515 | { | |
4516 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4517 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4518 | } | |
4519 | if (mpfr_nan_p(li)) | |
4520 | { | |
4521 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4522 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4523 | } | |
4524 | is_infinity = true; | |
4525 | } | |
4526 | ||
4527 | // If we got an overflow in the intermediate computations, | |
4528 | // then the result is infinity. | |
4529 | if (!is_infinity | |
4530 | && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri) | |
4531 | || mpfr_inf_p(lirr) || mpfr_inf_p(liri))) | |
4532 | { | |
4533 | if (mpfr_nan_p(lr)) | |
4534 | { | |
4535 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4536 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4537 | } | |
4538 | if (mpfr_nan_p(li)) | |
4539 | { | |
4540 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4541 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4542 | } | |
4543 | if (mpfr_nan_p(rr)) | |
4544 | { | |
4545 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4546 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4547 | } | |
4548 | if (mpfr_nan_p(ri)) | |
4549 | { | |
4550 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4551 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4552 | } | |
4553 | is_infinity = true; | |
4554 | } | |
4555 | ||
4556 | if (is_infinity) | |
4557 | { | |
4558 | mpfr_mul(lrrr, lr, rr, GMP_RNDN); | |
4559 | mpfr_mul(lrri, lr, ri, GMP_RNDN); | |
4560 | mpfr_mul(lirr, li, rr, GMP_RNDN); | |
4561 | mpfr_mul(liri, li, ri, GMP_RNDN); | |
4562 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4563 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4564 | mpfr_set_inf(real, mpfr_sgn(real)); | |
4565 | mpfr_set_inf(imag, mpfr_sgn(imag)); | |
4566 | } | |
4567 | ||
4568 | mpfr_clear(lr); | |
4569 | mpfr_clear(li); | |
4570 | mpfr_clear(rr); | |
4571 | mpfr_clear(ri); | |
4572 | } | |
4573 | ||
4574 | mpfr_clear(lrrr); | |
4575 | mpfr_clear(lrri); | |
4576 | mpfr_clear(lirr); | |
4577 | mpfr_clear(liri); | |
4578 | } | |
4579 | break; | |
4580 | case OPERATOR_DIV: | |
4581 | { | |
4582 | // For complex division we want to avoid having an | |
4583 | // intermediate overflow turn the whole result in a NaN. We | |
4584 | // scale the values to try to avoid this. | |
4585 | ||
4586 | if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag)) | |
4587 | error_at(location, "division by zero"); | |
4588 | ||
4589 | mpfr_t rra; | |
4590 | mpfr_t ria; | |
4591 | mpfr_init(rra); | |
4592 | mpfr_init(ria); | |
4593 | mpfr_abs(rra, right_real, GMP_RNDN); | |
4594 | mpfr_abs(ria, right_imag, GMP_RNDN); | |
4595 | mpfr_t t; | |
4596 | mpfr_init(t); | |
4597 | mpfr_max(t, rra, ria, GMP_RNDN); | |
4598 | ||
4599 | mpfr_t rr; | |
4600 | mpfr_t ri; | |
4601 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4602 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4603 | long ilogbw = 0; | |
4604 | if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t)) | |
4605 | { | |
4606 | ilogbw = mpfr_get_exp(t); | |
4607 | mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN); | |
4608 | mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN); | |
4609 | } | |
4610 | ||
4611 | mpfr_t denom; | |
4612 | mpfr_init(denom); | |
4613 | mpfr_mul(denom, rr, rr, GMP_RNDN); | |
4614 | mpfr_mul(t, ri, ri, GMP_RNDN); | |
4615 | mpfr_add(denom, denom, t, GMP_RNDN); | |
4616 | ||
4617 | mpfr_mul(real, left_real, rr, GMP_RNDN); | |
4618 | mpfr_mul(t, left_imag, ri, GMP_RNDN); | |
4619 | mpfr_add(real, real, t, GMP_RNDN); | |
4620 | mpfr_div(real, real, denom, GMP_RNDN); | |
4621 | mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN); | |
4622 | ||
4623 | mpfr_mul(imag, left_imag, rr, GMP_RNDN); | |
4624 | mpfr_mul(t, left_real, ri, GMP_RNDN); | |
4625 | mpfr_sub(imag, imag, t, GMP_RNDN); | |
4626 | mpfr_div(imag, imag, denom, GMP_RNDN); | |
4627 | mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN); | |
4628 | ||
4629 | // If we wind up with NaN on both sides, check whether we | |
4630 | // should really have infinity. The rule is that if either | |
4631 | // side of the complex number is infinity, then the whole | |
4632 | // value is infinity, even if the other side is NaN. So the | |
4633 | // only case we have to fix is the one in which both sides are | |
4634 | // NaN. | |
4635 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4636 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4637 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4638 | { | |
4639 | if (mpfr_zero_p(denom)) | |
4640 | { | |
4641 | mpfr_set_inf(real, mpfr_sgn(rr)); | |
4642 | mpfr_mul(real, real, left_real, GMP_RNDN); | |
4643 | mpfr_set_inf(imag, mpfr_sgn(rr)); | |
4644 | mpfr_mul(imag, imag, left_imag, GMP_RNDN); | |
4645 | } | |
4646 | else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag)) | |
4647 | && mpfr_number_p(rr) && mpfr_number_p(ri)) | |
4648 | { | |
4649 | mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN); | |
4650 | mpfr_copysign(t, t, left_real, GMP_RNDN); | |
4651 | ||
4652 | mpfr_t t2; | |
4653 | mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN); | |
4654 | mpfr_copysign(t2, t2, left_imag, GMP_RNDN); | |
4655 | ||
4656 | mpfr_t t3; | |
4657 | mpfr_init(t3); | |
4658 | mpfr_mul(t3, t, rr, GMP_RNDN); | |
4659 | ||
4660 | mpfr_t t4; | |
4661 | mpfr_init(t4); | |
4662 | mpfr_mul(t4, t2, ri, GMP_RNDN); | |
4663 | ||
4664 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
4665 | mpfr_set_inf(real, mpfr_sgn(t3)); | |
4666 | ||
4667 | mpfr_mul(t3, t2, rr, GMP_RNDN); | |
4668 | mpfr_mul(t4, t, ri, GMP_RNDN); | |
4669 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
4670 | mpfr_set_inf(imag, mpfr_sgn(t3)); | |
4671 | ||
4672 | mpfr_clear(t2); | |
4673 | mpfr_clear(t3); | |
4674 | mpfr_clear(t4); | |
4675 | } | |
4676 | else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag)) | |
4677 | && mpfr_number_p(left_real) && mpfr_number_p(left_imag)) | |
4678 | { | |
4679 | mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4680 | mpfr_copysign(t, t, rr, GMP_RNDN); | |
4681 | ||
4682 | mpfr_t t2; | |
4683 | mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
4684 | mpfr_copysign(t2, t2, ri, GMP_RNDN); | |
4685 | ||
4686 | mpfr_t t3; | |
4687 | mpfr_init(t3); | |
4688 | mpfr_mul(t3, left_real, t, GMP_RNDN); | |
4689 | ||
4690 | mpfr_t t4; | |
4691 | mpfr_init(t4); | |
4692 | mpfr_mul(t4, left_imag, t2, GMP_RNDN); | |
4693 | ||
4694 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
4695 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4696 | mpfr_mul(real, real, t3, GMP_RNDN); | |
4697 | ||
4698 | mpfr_mul(t3, left_imag, t, GMP_RNDN); | |
4699 | mpfr_mul(t4, left_real, t2, GMP_RNDN); | |
4700 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
4701 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4702 | mpfr_mul(imag, imag, t3, GMP_RNDN); | |
4703 | ||
4704 | mpfr_clear(t2); | |
4705 | mpfr_clear(t3); | |
4706 | mpfr_clear(t4); | |
4707 | } | |
4708 | } | |
4709 | ||
4710 | mpfr_clear(denom); | |
4711 | mpfr_clear(rr); | |
4712 | mpfr_clear(ri); | |
4713 | mpfr_clear(t); | |
4714 | mpfr_clear(rra); | |
4715 | mpfr_clear(ria); | |
4716 | } | |
4717 | break; | |
4718 | case OPERATOR_MOD: | |
4719 | return false; | |
4720 | case OPERATOR_LSHIFT: | |
4721 | case OPERATOR_RSHIFT: | |
4722 | return false; | |
4723 | default: | |
4724 | gcc_unreachable(); | |
4725 | } | |
4726 | ||
4727 | Type* type = left_type; | |
4728 | if (type == NULL) | |
4729 | type = right_type; | |
4730 | else if (type != right_type && right_type != NULL) | |
4731 | { | |
4732 | if (type->is_abstract()) | |
4733 | type = right_type; | |
4734 | else if (!right_type->is_abstract()) | |
4735 | { | |
4736 | // This looks like a type error which should be diagnosed | |
4737 | // elsewhere. Don't do anything here, to avoid an unhelpful | |
4738 | // chain of error messages. | |
4739 | return true; | |
4740 | } | |
4741 | } | |
4742 | ||
4743 | if (type != NULL && !type->is_abstract()) | |
4744 | { | |
4745 | if ((type != left_type | |
4746 | && !Complex_expression::check_constant(left_real, left_imag, | |
4747 | type, location)) | |
4748 | || (type != right_type | |
4749 | && !Complex_expression::check_constant(right_real, right_imag, | |
4750 | type, location)) | |
4751 | || !Complex_expression::check_constant(real, imag, type, | |
4752 | location)) | |
4753 | { | |
4754 | mpfr_set_ui(real, 0, GMP_RNDN); | |
4755 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
4756 | } | |
4757 | } | |
4758 | ||
4759 | return true; | |
4760 | } | |
4761 | ||
4762 | // Lower a binary expression. We have to evaluate constant | |
4763 | // expressions now, in order to implement Go's unlimited precision | |
4764 | // constants. | |
4765 | ||
4766 | Expression* | |
4767 | Binary_expression::do_lower(Gogo*, Named_object*, int) | |
4768 | { | |
4769 | source_location location = this->location(); | |
4770 | Operator op = this->op_; | |
4771 | Expression* left = this->left_; | |
4772 | Expression* right = this->right_; | |
4773 | ||
4774 | const bool is_comparison = (op == OPERATOR_EQEQ | |
4775 | || op == OPERATOR_NOTEQ | |
4776 | || op == OPERATOR_LT | |
4777 | || op == OPERATOR_LE | |
4778 | || op == OPERATOR_GT | |
4779 | || op == OPERATOR_GE); | |
4780 | ||
4781 | // Integer constant expressions. | |
4782 | { | |
4783 | mpz_t left_val; | |
4784 | mpz_init(left_val); | |
4785 | Type* left_type; | |
4786 | mpz_t right_val; | |
4787 | mpz_init(right_val); | |
4788 | Type* right_type; | |
4789 | if (left->integer_constant_value(false, left_val, &left_type) | |
4790 | && right->integer_constant_value(false, right_val, &right_type)) | |
4791 | { | |
4792 | Expression* ret = NULL; | |
4793 | if (left_type != right_type | |
4794 | && left_type != NULL | |
4795 | && right_type != NULL | |
4796 | && left_type->base() != right_type->base() | |
4797 | && op != OPERATOR_LSHIFT | |
4798 | && op != OPERATOR_RSHIFT) | |
4799 | { | |
4800 | // May be a type error--let it be diagnosed later. | |
4801 | } | |
4802 | else if (is_comparison) | |
4803 | { | |
4804 | bool b = Binary_expression::compare_integer(op, left_val, | |
4805 | right_val); | |
4806 | ret = Expression::make_cast(Type::lookup_bool_type(), | |
4807 | Expression::make_boolean(b, location), | |
4808 | location); | |
4809 | } | |
4810 | else | |
4811 | { | |
4812 | mpz_t val; | |
4813 | mpz_init(val); | |
4814 | ||
4815 | if (Binary_expression::eval_integer(op, left_type, left_val, | |
4816 | right_type, right_val, | |
4817 | location, val)) | |
4818 | { | |
4819 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND); | |
4820 | Type* type; | |
4821 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
4822 | type = left_type; | |
4823 | else if (left_type == NULL) | |
4824 | type = right_type; | |
4825 | else if (right_type == NULL) | |
4826 | type = left_type; | |
4827 | else if (!left_type->is_abstract() | |
4828 | && left_type->named_type() != NULL) | |
4829 | type = left_type; | |
4830 | else if (!right_type->is_abstract() | |
4831 | && right_type->named_type() != NULL) | |
4832 | type = right_type; | |
4833 | else if (!left_type->is_abstract()) | |
4834 | type = left_type; | |
4835 | else if (!right_type->is_abstract()) | |
4836 | type = right_type; | |
4837 | else if (left_type->float_type() != NULL) | |
4838 | type = left_type; | |
4839 | else if (right_type->float_type() != NULL) | |
4840 | type = right_type; | |
4841 | else if (left_type->complex_type() != NULL) | |
4842 | type = left_type; | |
4843 | else if (right_type->complex_type() != NULL) | |
4844 | type = right_type; | |
4845 | else | |
4846 | type = left_type; | |
4847 | ret = Expression::make_integer(&val, type, location); | |
4848 | } | |
4849 | ||
4850 | mpz_clear(val); | |
4851 | } | |
4852 | ||
4853 | if (ret != NULL) | |
4854 | { | |
4855 | mpz_clear(right_val); | |
4856 | mpz_clear(left_val); | |
4857 | return ret; | |
4858 | } | |
4859 | } | |
4860 | mpz_clear(right_val); | |
4861 | mpz_clear(left_val); | |
4862 | } | |
4863 | ||
4864 | // Floating point constant expressions. | |
4865 | { | |
4866 | mpfr_t left_val; | |
4867 | mpfr_init(left_val); | |
4868 | Type* left_type; | |
4869 | mpfr_t right_val; | |
4870 | mpfr_init(right_val); | |
4871 | Type* right_type; | |
4872 | if (left->float_constant_value(left_val, &left_type) | |
4873 | && right->float_constant_value(right_val, &right_type)) | |
4874 | { | |
4875 | Expression* ret = NULL; | |
4876 | if (left_type != right_type | |
4877 | && left_type != NULL | |
4878 | && right_type != NULL | |
4879 | && left_type->base() != right_type->base() | |
4880 | && op != OPERATOR_LSHIFT | |
4881 | && op != OPERATOR_RSHIFT) | |
4882 | { | |
4883 | // May be a type error--let it be diagnosed later. | |
4884 | } | |
4885 | else if (is_comparison) | |
4886 | { | |
4887 | bool b = Binary_expression::compare_float(op, | |
4888 | (left_type != NULL | |
4889 | ? left_type | |
4890 | : right_type), | |
4891 | left_val, right_val); | |
4892 | ret = Expression::make_boolean(b, location); | |
4893 | } | |
4894 | else | |
4895 | { | |
4896 | mpfr_t val; | |
4897 | mpfr_init(val); | |
4898 | ||
4899 | if (Binary_expression::eval_float(op, left_type, left_val, | |
4900 | right_type, right_val, val, | |
4901 | location)) | |
4902 | { | |
4903 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND | |
4904 | && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT); | |
4905 | Type* type; | |
4906 | if (left_type == NULL) | |
4907 | type = right_type; | |
4908 | else if (right_type == NULL) | |
4909 | type = left_type; | |
4910 | else if (!left_type->is_abstract() | |
4911 | && left_type->named_type() != NULL) | |
4912 | type = left_type; | |
4913 | else if (!right_type->is_abstract() | |
4914 | && right_type->named_type() != NULL) | |
4915 | type = right_type; | |
4916 | else if (!left_type->is_abstract()) | |
4917 | type = left_type; | |
4918 | else if (!right_type->is_abstract()) | |
4919 | type = right_type; | |
4920 | else if (left_type->float_type() != NULL) | |
4921 | type = left_type; | |
4922 | else if (right_type->float_type() != NULL) | |
4923 | type = right_type; | |
4924 | else | |
4925 | type = left_type; | |
4926 | ret = Expression::make_float(&val, type, location); | |
4927 | } | |
4928 | ||
4929 | mpfr_clear(val); | |
4930 | } | |
4931 | ||
4932 | if (ret != NULL) | |
4933 | { | |
4934 | mpfr_clear(right_val); | |
4935 | mpfr_clear(left_val); | |
4936 | return ret; | |
4937 | } | |
4938 | } | |
4939 | mpfr_clear(right_val); | |
4940 | mpfr_clear(left_val); | |
4941 | } | |
4942 | ||
4943 | // Complex constant expressions. | |
4944 | { | |
4945 | mpfr_t left_real; | |
4946 | mpfr_t left_imag; | |
4947 | mpfr_init(left_real); | |
4948 | mpfr_init(left_imag); | |
4949 | Type* left_type; | |
4950 | ||
4951 | mpfr_t right_real; | |
4952 | mpfr_t right_imag; | |
4953 | mpfr_init(right_real); | |
4954 | mpfr_init(right_imag); | |
4955 | Type* right_type; | |
4956 | ||
4957 | if (left->complex_constant_value(left_real, left_imag, &left_type) | |
4958 | && right->complex_constant_value(right_real, right_imag, &right_type)) | |
4959 | { | |
4960 | Expression* ret = NULL; | |
4961 | if (left_type != right_type | |
4962 | && left_type != NULL | |
4963 | && right_type != NULL | |
4964 | && left_type->base() != right_type->base()) | |
4965 | { | |
4966 | // May be a type error--let it be diagnosed later. | |
4967 | } | |
4968 | else if (is_comparison) | |
4969 | { | |
4970 | bool b = Binary_expression::compare_complex(op, | |
4971 | (left_type != NULL | |
4972 | ? left_type | |
4973 | : right_type), | |
4974 | left_real, | |
4975 | left_imag, | |
4976 | right_real, | |
4977 | right_imag); | |
4978 | ret = Expression::make_boolean(b, location); | |
4979 | } | |
4980 | else | |
4981 | { | |
4982 | mpfr_t real; | |
4983 | mpfr_t imag; | |
4984 | mpfr_init(real); | |
4985 | mpfr_init(imag); | |
4986 | ||
4987 | if (Binary_expression::eval_complex(op, left_type, | |
4988 | left_real, left_imag, | |
4989 | right_type, | |
4990 | right_real, right_imag, | |
4991 | real, imag, | |
4992 | location)) | |
4993 | { | |
4994 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND | |
4995 | && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT); | |
4996 | Type* type; | |
4997 | if (left_type == NULL) | |
4998 | type = right_type; | |
4999 | else if (right_type == NULL) | |
5000 | type = left_type; | |
5001 | else if (!left_type->is_abstract() | |
5002 | && left_type->named_type() != NULL) | |
5003 | type = left_type; | |
5004 | else if (!right_type->is_abstract() | |
5005 | && right_type->named_type() != NULL) | |
5006 | type = right_type; | |
5007 | else if (!left_type->is_abstract()) | |
5008 | type = left_type; | |
5009 | else if (!right_type->is_abstract()) | |
5010 | type = right_type; | |
5011 | else if (left_type->complex_type() != NULL) | |
5012 | type = left_type; | |
5013 | else if (right_type->complex_type() != NULL) | |
5014 | type = right_type; | |
5015 | else | |
5016 | type = left_type; | |
5017 | ret = Expression::make_complex(&real, &imag, type, | |
5018 | location); | |
5019 | } | |
5020 | mpfr_clear(real); | |
5021 | mpfr_clear(imag); | |
5022 | } | |
5023 | ||
5024 | if (ret != NULL) | |
5025 | { | |
5026 | mpfr_clear(left_real); | |
5027 | mpfr_clear(left_imag); | |
5028 | mpfr_clear(right_real); | |
5029 | mpfr_clear(right_imag); | |
5030 | return ret; | |
5031 | } | |
5032 | } | |
5033 | ||
5034 | mpfr_clear(left_real); | |
5035 | mpfr_clear(left_imag); | |
5036 | mpfr_clear(right_real); | |
5037 | mpfr_clear(right_imag); | |
5038 | } | |
5039 | ||
5040 | // String constant expressions. | |
5041 | if (op == OPERATOR_PLUS | |
5042 | && left->type()->is_string_type() | |
5043 | && right->type()->is_string_type()) | |
5044 | { | |
5045 | std::string left_string; | |
5046 | std::string right_string; | |
5047 | if (left->string_constant_value(&left_string) | |
5048 | && right->string_constant_value(&right_string)) | |
5049 | return Expression::make_string(left_string + right_string, location); | |
5050 | } | |
5051 | ||
5052 | return this; | |
5053 | } | |
5054 | ||
5055 | // Return the integer constant value, if it has one. | |
5056 | ||
5057 | bool | |
5058 | Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
5059 | Type** ptype) const | |
5060 | { | |
5061 | mpz_t left_val; | |
5062 | mpz_init(left_val); | |
5063 | Type* left_type; | |
5064 | if (!this->left_->integer_constant_value(iota_is_constant, left_val, | |
5065 | &left_type)) | |
5066 | { | |
5067 | mpz_clear(left_val); | |
5068 | return false; | |
5069 | } | |
5070 | ||
5071 | mpz_t right_val; | |
5072 | mpz_init(right_val); | |
5073 | Type* right_type; | |
5074 | if (!this->right_->integer_constant_value(iota_is_constant, right_val, | |
5075 | &right_type)) | |
5076 | { | |
5077 | mpz_clear(right_val); | |
5078 | mpz_clear(left_val); | |
5079 | return false; | |
5080 | } | |
5081 | ||
5082 | bool ret; | |
5083 | if (left_type != right_type | |
5084 | && left_type != NULL | |
5085 | && right_type != NULL | |
5086 | && left_type->base() != right_type->base() | |
5087 | && this->op_ != OPERATOR_RSHIFT | |
5088 | && this->op_ != OPERATOR_LSHIFT) | |
5089 | ret = false; | |
5090 | else | |
5091 | ret = Binary_expression::eval_integer(this->op_, left_type, left_val, | |
5092 | right_type, right_val, | |
5093 | this->location(), val); | |
5094 | ||
5095 | mpz_clear(right_val); | |
5096 | mpz_clear(left_val); | |
5097 | ||
5098 | if (ret) | |
5099 | *ptype = left_type; | |
5100 | ||
5101 | return ret; | |
5102 | } | |
5103 | ||
5104 | // Return the floating point constant value, if it has one. | |
5105 | ||
5106 | bool | |
5107 | Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
5108 | { | |
5109 | mpfr_t left_val; | |
5110 | mpfr_init(left_val); | |
5111 | Type* left_type; | |
5112 | if (!this->left_->float_constant_value(left_val, &left_type)) | |
5113 | { | |
5114 | mpfr_clear(left_val); | |
5115 | return false; | |
5116 | } | |
5117 | ||
5118 | mpfr_t right_val; | |
5119 | mpfr_init(right_val); | |
5120 | Type* right_type; | |
5121 | if (!this->right_->float_constant_value(right_val, &right_type)) | |
5122 | { | |
5123 | mpfr_clear(right_val); | |
5124 | mpfr_clear(left_val); | |
5125 | return false; | |
5126 | } | |
5127 | ||
5128 | bool ret; | |
5129 | if (left_type != right_type | |
5130 | && left_type != NULL | |
5131 | && right_type != NULL | |
5132 | && left_type->base() != right_type->base()) | |
5133 | ret = false; | |
5134 | else | |
5135 | ret = Binary_expression::eval_float(this->op_, left_type, left_val, | |
5136 | right_type, right_val, | |
5137 | val, this->location()); | |
5138 | ||
5139 | mpfr_clear(left_val); | |
5140 | mpfr_clear(right_val); | |
5141 | ||
5142 | if (ret) | |
5143 | *ptype = left_type; | |
5144 | ||
5145 | return ret; | |
5146 | } | |
5147 | ||
5148 | // Return the complex constant value, if it has one. | |
5149 | ||
5150 | bool | |
5151 | Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
5152 | Type** ptype) const | |
5153 | { | |
5154 | mpfr_t left_real; | |
5155 | mpfr_t left_imag; | |
5156 | mpfr_init(left_real); | |
5157 | mpfr_init(left_imag); | |
5158 | Type* left_type; | |
5159 | if (!this->left_->complex_constant_value(left_real, left_imag, &left_type)) | |
5160 | { | |
5161 | mpfr_clear(left_real); | |
5162 | mpfr_clear(left_imag); | |
5163 | return false; | |
5164 | } | |
5165 | ||
5166 | mpfr_t right_real; | |
5167 | mpfr_t right_imag; | |
5168 | mpfr_init(right_real); | |
5169 | mpfr_init(right_imag); | |
5170 | Type* right_type; | |
5171 | if (!this->right_->complex_constant_value(right_real, right_imag, | |
5172 | &right_type)) | |
5173 | { | |
5174 | mpfr_clear(left_real); | |
5175 | mpfr_clear(left_imag); | |
5176 | mpfr_clear(right_real); | |
5177 | mpfr_clear(right_imag); | |
5178 | return false; | |
5179 | } | |
5180 | ||
5181 | bool ret; | |
5182 | if (left_type != right_type | |
5183 | && left_type != NULL | |
5184 | && right_type != NULL | |
5185 | && left_type->base() != right_type->base()) | |
5186 | ret = false; | |
5187 | else | |
5188 | ret = Binary_expression::eval_complex(this->op_, left_type, | |
5189 | left_real, left_imag, | |
5190 | right_type, | |
5191 | right_real, right_imag, | |
5192 | real, imag, | |
5193 | this->location()); | |
5194 | mpfr_clear(left_real); | |
5195 | mpfr_clear(left_imag); | |
5196 | mpfr_clear(right_real); | |
5197 | mpfr_clear(right_imag); | |
5198 | ||
5199 | if (ret) | |
5200 | *ptype = left_type; | |
5201 | ||
5202 | return ret; | |
5203 | } | |
5204 | ||
5205 | // Note that the value is being discarded. | |
5206 | ||
5207 | void | |
5208 | Binary_expression::do_discarding_value() | |
5209 | { | |
5210 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
5211 | this->right_->discarding_value(); | |
5212 | else | |
5213 | this->warn_about_unused_value(); | |
5214 | } | |
5215 | ||
5216 | // Get type. | |
5217 | ||
5218 | Type* | |
5219 | Binary_expression::do_type() | |
5220 | { | |
5221 | switch (this->op_) | |
5222 | { | |
5223 | case OPERATOR_OROR: | |
5224 | case OPERATOR_ANDAND: | |
5225 | case OPERATOR_EQEQ: | |
5226 | case OPERATOR_NOTEQ: | |
5227 | case OPERATOR_LT: | |
5228 | case OPERATOR_LE: | |
5229 | case OPERATOR_GT: | |
5230 | case OPERATOR_GE: | |
5231 | return Type::lookup_bool_type(); | |
5232 | ||
5233 | case OPERATOR_PLUS: | |
5234 | case OPERATOR_MINUS: | |
5235 | case OPERATOR_OR: | |
5236 | case OPERATOR_XOR: | |
5237 | case OPERATOR_MULT: | |
5238 | case OPERATOR_DIV: | |
5239 | case OPERATOR_MOD: | |
5240 | case OPERATOR_AND: | |
5241 | case OPERATOR_BITCLEAR: | |
5242 | { | |
5243 | Type* left_type = this->left_->type(); | |
5244 | Type* right_type = this->right_->type(); | |
5245 | if (!left_type->is_abstract() && left_type->named_type() != NULL) | |
5246 | return left_type; | |
5247 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
5248 | return right_type; | |
5249 | else if (!left_type->is_abstract()) | |
5250 | return left_type; | |
5251 | else if (!right_type->is_abstract()) | |
5252 | return right_type; | |
5253 | else if (left_type->complex_type() != NULL) | |
5254 | return left_type; | |
5255 | else if (right_type->complex_type() != NULL) | |
5256 | return right_type; | |
5257 | else if (left_type->float_type() != NULL) | |
5258 | return left_type; | |
5259 | else if (right_type->float_type() != NULL) | |
5260 | return right_type; | |
5261 | else | |
5262 | return left_type; | |
5263 | } | |
5264 | ||
5265 | case OPERATOR_LSHIFT: | |
5266 | case OPERATOR_RSHIFT: | |
5267 | return this->left_->type(); | |
5268 | ||
5269 | default: | |
5270 | gcc_unreachable(); | |
5271 | } | |
5272 | } | |
5273 | ||
5274 | // Set type for a binary expression. | |
5275 | ||
5276 | void | |
5277 | Binary_expression::do_determine_type(const Type_context* context) | |
5278 | { | |
5279 | Type* tleft = this->left_->type(); | |
5280 | Type* tright = this->right_->type(); | |
5281 | ||
5282 | // Both sides should have the same type, except for the shift | |
5283 | // operations. For a comparison, we should ignore the incoming | |
5284 | // type. | |
5285 | ||
5286 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5287 | || this->op_ == OPERATOR_RSHIFT); | |
5288 | ||
5289 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5290 | || this->op_ == OPERATOR_NOTEQ | |
5291 | || this->op_ == OPERATOR_LT | |
5292 | || this->op_ == OPERATOR_LE | |
5293 | || this->op_ == OPERATOR_GT | |
5294 | || this->op_ == OPERATOR_GE); | |
5295 | ||
5296 | Type_context subcontext(*context); | |
5297 | ||
5298 | if (is_comparison) | |
5299 | { | |
5300 | // In a comparison, the context does not determine the types of | |
5301 | // the operands. | |
5302 | subcontext.type = NULL; | |
5303 | } | |
5304 | ||
5305 | // Set the context for the left hand operand. | |
5306 | if (is_shift_op) | |
5307 | { | |
5308 | // The right hand operand plays no role in determining the type | |
5309 | // of the left hand operand. A shift of an abstract integer in | |
5310 | // a string context gets special treatment, which may be a | |
5311 | // language bug. | |
5312 | if (subcontext.type != NULL | |
5313 | && subcontext.type->is_string_type() | |
5314 | && tleft->is_abstract()) | |
5315 | error_at(this->location(), "shift of non-integer operand"); | |
5316 | } | |
5317 | else if (!tleft->is_abstract()) | |
5318 | subcontext.type = tleft; | |
5319 | else if (!tright->is_abstract()) | |
5320 | subcontext.type = tright; | |
5321 | else if (subcontext.type == NULL) | |
5322 | { | |
5323 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5324 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5325 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5326 | { | |
5327 | // Both sides have an abstract integer, abstract float, or | |
5328 | // abstract complex type. Just let CONTEXT determine | |
5329 | // whether they may remain abstract or not. | |
5330 | } | |
5331 | else if (tleft->complex_type() != NULL) | |
5332 | subcontext.type = tleft; | |
5333 | else if (tright->complex_type() != NULL) | |
5334 | subcontext.type = tright; | |
5335 | else if (tleft->float_type() != NULL) | |
5336 | subcontext.type = tleft; | |
5337 | else if (tright->float_type() != NULL) | |
5338 | subcontext.type = tright; | |
5339 | else | |
5340 | subcontext.type = tleft; | |
5341 | } | |
5342 | ||
5343 | this->left_->determine_type(&subcontext); | |
5344 | ||
5345 | // The context for the right hand operand is the same as for the | |
5346 | // left hand operand, except for a shift operator. | |
5347 | if (is_shift_op) | |
5348 | { | |
5349 | subcontext.type = Type::lookup_integer_type("uint"); | |
5350 | subcontext.may_be_abstract = false; | |
5351 | } | |
5352 | ||
5353 | this->right_->determine_type(&subcontext); | |
5354 | } | |
5355 | ||
5356 | // Report an error if the binary operator OP does not support TYPE. | |
5357 | // Return whether the operation is OK. This should not be used for | |
5358 | // shift. | |
5359 | ||
5360 | bool | |
5361 | Binary_expression::check_operator_type(Operator op, Type* type, | |
5362 | source_location location) | |
5363 | { | |
5364 | switch (op) | |
5365 | { | |
5366 | case OPERATOR_OROR: | |
5367 | case OPERATOR_ANDAND: | |
5368 | if (!type->is_boolean_type()) | |
5369 | { | |
5370 | error_at(location, "expected boolean type"); | |
5371 | return false; | |
5372 | } | |
5373 | break; | |
5374 | ||
5375 | case OPERATOR_EQEQ: | |
5376 | case OPERATOR_NOTEQ: | |
5377 | if (type->integer_type() == NULL | |
5378 | && type->float_type() == NULL | |
5379 | && type->complex_type() == NULL | |
5380 | && !type->is_string_type() | |
5381 | && type->points_to() == NULL | |
5382 | && !type->is_nil_type() | |
5383 | && !type->is_boolean_type() | |
5384 | && type->interface_type() == NULL | |
5385 | && (type->array_type() == NULL | |
5386 | || type->array_type()->length() != NULL) | |
5387 | && type->map_type() == NULL | |
5388 | && type->channel_type() == NULL | |
5389 | && type->function_type() == NULL) | |
5390 | { | |
5391 | error_at(location, | |
5392 | ("expected integer, floating, complex, string, pointer, " | |
5393 | "boolean, interface, slice, map, channel, " | |
5394 | "or function type")); | |
5395 | return false; | |
5396 | } | |
5397 | break; | |
5398 | ||
5399 | case OPERATOR_LT: | |
5400 | case OPERATOR_LE: | |
5401 | case OPERATOR_GT: | |
5402 | case OPERATOR_GE: | |
5403 | if (type->integer_type() == NULL | |
5404 | && type->float_type() == NULL | |
5405 | && !type->is_string_type()) | |
5406 | { | |
5407 | error_at(location, "expected integer, floating, or string type"); | |
5408 | return false; | |
5409 | } | |
5410 | break; | |
5411 | ||
5412 | case OPERATOR_PLUS: | |
5413 | case OPERATOR_PLUSEQ: | |
5414 | if (type->integer_type() == NULL | |
5415 | && type->float_type() == NULL | |
5416 | && type->complex_type() == NULL | |
5417 | && !type->is_string_type()) | |
5418 | { | |
5419 | error_at(location, | |
5420 | "expected integer, floating, complex, or string type"); | |
5421 | return false; | |
5422 | } | |
5423 | break; | |
5424 | ||
5425 | case OPERATOR_MINUS: | |
5426 | case OPERATOR_MINUSEQ: | |
5427 | case OPERATOR_MULT: | |
5428 | case OPERATOR_MULTEQ: | |
5429 | case OPERATOR_DIV: | |
5430 | case OPERATOR_DIVEQ: | |
5431 | if (type->integer_type() == NULL | |
5432 | && type->float_type() == NULL | |
5433 | && type->complex_type() == NULL) | |
5434 | { | |
5435 | error_at(location, "expected integer, floating, or complex type"); | |
5436 | return false; | |
5437 | } | |
5438 | break; | |
5439 | ||
5440 | case OPERATOR_MOD: | |
5441 | case OPERATOR_MODEQ: | |
5442 | case OPERATOR_OR: | |
5443 | case OPERATOR_OREQ: | |
5444 | case OPERATOR_AND: | |
5445 | case OPERATOR_ANDEQ: | |
5446 | case OPERATOR_XOR: | |
5447 | case OPERATOR_XOREQ: | |
5448 | case OPERATOR_BITCLEAR: | |
5449 | case OPERATOR_BITCLEAREQ: | |
5450 | if (type->integer_type() == NULL) | |
5451 | { | |
5452 | error_at(location, "expected integer type"); | |
5453 | return false; | |
5454 | } | |
5455 | break; | |
5456 | ||
5457 | default: | |
5458 | gcc_unreachable(); | |
5459 | } | |
5460 | ||
5461 | return true; | |
5462 | } | |
5463 | ||
5464 | // Check types. | |
5465 | ||
5466 | void | |
5467 | Binary_expression::do_check_types(Gogo*) | |
5468 | { | |
5469 | Type* left_type = this->left_->type(); | |
5470 | Type* right_type = this->right_->type(); | |
5471 | if (left_type->is_error_type() || right_type->is_error_type()) | |
9fe897ef | 5472 | { |
5473 | this->set_is_error(); | |
5474 | return; | |
5475 | } | |
e440a328 | 5476 | |
5477 | if (this->op_ == OPERATOR_EQEQ | |
5478 | || this->op_ == OPERATOR_NOTEQ | |
5479 | || this->op_ == OPERATOR_LT | |
5480 | || this->op_ == OPERATOR_LE | |
5481 | || this->op_ == OPERATOR_GT | |
5482 | || this->op_ == OPERATOR_GE) | |
5483 | { | |
5484 | if (!Type::are_assignable(left_type, right_type, NULL) | |
5485 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5486 | { | |
5487 | this->report_error(_("incompatible types in binary expression")); | |
5488 | return; | |
5489 | } | |
5490 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
5491 | this->location()) | |
5492 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
5493 | this->location())) | |
5494 | { | |
5495 | this->set_is_error(); | |
5496 | return; | |
5497 | } | |
5498 | } | |
5499 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5500 | { | |
5501 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5502 | { | |
5503 | this->report_error(_("incompatible types in binary expression")); | |
5504 | return; | |
5505 | } | |
5506 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
5507 | this->location())) | |
5508 | { | |
5509 | this->set_is_error(); | |
5510 | return; | |
5511 | } | |
5512 | } | |
5513 | else | |
5514 | { | |
5515 | if (left_type->integer_type() == NULL) | |
5516 | this->report_error(_("shift of non-integer operand")); | |
5517 | ||
5518 | if (!right_type->is_abstract() | |
5519 | && (right_type->integer_type() == NULL | |
5520 | || !right_type->integer_type()->is_unsigned())) | |
5521 | this->report_error(_("shift count not unsigned integer")); | |
5522 | else | |
5523 | { | |
5524 | mpz_t val; | |
5525 | mpz_init(val); | |
5526 | Type* type; | |
5527 | if (this->right_->integer_constant_value(true, val, &type)) | |
5528 | { | |
5529 | if (mpz_sgn(val) < 0) | |
5530 | this->report_error(_("negative shift count")); | |
5531 | } | |
5532 | mpz_clear(val); | |
5533 | } | |
5534 | } | |
5535 | } | |
5536 | ||
5537 | // Get a tree for a binary expression. | |
5538 | ||
5539 | tree | |
5540 | Binary_expression::do_get_tree(Translate_context* context) | |
5541 | { | |
5542 | tree left = this->left_->get_tree(context); | |
5543 | tree right = this->right_->get_tree(context); | |
5544 | ||
5545 | if (left == error_mark_node || right == error_mark_node) | |
5546 | return error_mark_node; | |
5547 | ||
5548 | enum tree_code code; | |
5549 | bool use_left_type = true; | |
5550 | bool is_shift_op = false; | |
5551 | switch (this->op_) | |
5552 | { | |
5553 | case OPERATOR_EQEQ: | |
5554 | case OPERATOR_NOTEQ: | |
5555 | case OPERATOR_LT: | |
5556 | case OPERATOR_LE: | |
5557 | case OPERATOR_GT: | |
5558 | case OPERATOR_GE: | |
5559 | return Expression::comparison_tree(context, this->op_, | |
5560 | this->left_->type(), left, | |
5561 | this->right_->type(), right, | |
5562 | this->location()); | |
5563 | ||
5564 | case OPERATOR_OROR: | |
5565 | code = TRUTH_ORIF_EXPR; | |
5566 | use_left_type = false; | |
5567 | break; | |
5568 | case OPERATOR_ANDAND: | |
5569 | code = TRUTH_ANDIF_EXPR; | |
5570 | use_left_type = false; | |
5571 | break; | |
5572 | case OPERATOR_PLUS: | |
5573 | code = PLUS_EXPR; | |
5574 | break; | |
5575 | case OPERATOR_MINUS: | |
5576 | code = MINUS_EXPR; | |
5577 | break; | |
5578 | case OPERATOR_OR: | |
5579 | code = BIT_IOR_EXPR; | |
5580 | break; | |
5581 | case OPERATOR_XOR: | |
5582 | code = BIT_XOR_EXPR; | |
5583 | break; | |
5584 | case OPERATOR_MULT: | |
5585 | code = MULT_EXPR; | |
5586 | break; | |
5587 | case OPERATOR_DIV: | |
5588 | { | |
5589 | Type *t = this->left_->type(); | |
5590 | if (t->float_type() != NULL || t->complex_type() != NULL) | |
5591 | code = RDIV_EXPR; | |
5592 | else | |
5593 | code = TRUNC_DIV_EXPR; | |
5594 | } | |
5595 | break; | |
5596 | case OPERATOR_MOD: | |
5597 | code = TRUNC_MOD_EXPR; | |
5598 | break; | |
5599 | case OPERATOR_LSHIFT: | |
5600 | code = LSHIFT_EXPR; | |
5601 | is_shift_op = true; | |
5602 | break; | |
5603 | case OPERATOR_RSHIFT: | |
5604 | code = RSHIFT_EXPR; | |
5605 | is_shift_op = true; | |
5606 | break; | |
5607 | case OPERATOR_AND: | |
5608 | code = BIT_AND_EXPR; | |
5609 | break; | |
5610 | case OPERATOR_BITCLEAR: | |
5611 | right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right); | |
5612 | code = BIT_AND_EXPR; | |
5613 | break; | |
5614 | default: | |
5615 | gcc_unreachable(); | |
5616 | } | |
5617 | ||
5618 | tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right); | |
5619 | ||
5620 | if (this->left_->type()->is_string_type()) | |
5621 | { | |
5622 | gcc_assert(this->op_ == OPERATOR_PLUS); | |
5623 | tree string_type = Type::make_string_type()->get_tree(context->gogo()); | |
5624 | static tree string_plus_decl; | |
5625 | return Gogo::call_builtin(&string_plus_decl, | |
5626 | this->location(), | |
5627 | "__go_string_plus", | |
5628 | 2, | |
5629 | string_type, | |
5630 | string_type, | |
5631 | left, | |
5632 | string_type, | |
5633 | right); | |
5634 | } | |
5635 | ||
5636 | tree compute_type = excess_precision_type(type); | |
5637 | if (compute_type != NULL_TREE) | |
5638 | { | |
5639 | left = ::convert(compute_type, left); | |
5640 | right = ::convert(compute_type, right); | |
5641 | } | |
5642 | ||
5643 | tree eval_saved = NULL_TREE; | |
5644 | if (is_shift_op) | |
5645 | { | |
5646 | if (!DECL_P(left)) | |
5647 | left = save_expr(left); | |
5648 | if (!DECL_P(right)) | |
5649 | right = save_expr(right); | |
5650 | // Make sure the values are evaluated. | |
5651 | eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR, | |
5652 | void_type_node, left, right); | |
5653 | } | |
5654 | ||
5655 | tree ret = fold_build2_loc(this->location(), | |
5656 | code, | |
5657 | compute_type != NULL_TREE ? compute_type : type, | |
5658 | left, right); | |
5659 | ||
5660 | if (compute_type != NULL_TREE) | |
5661 | ret = ::convert(type, ret); | |
5662 | ||
5663 | // In Go, a shift larger than the size of the type is well-defined. | |
5664 | // This is not true in GENERIC, so we need to insert a conditional. | |
5665 | if (is_shift_op) | |
5666 | { | |
5667 | gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left))); | |
5668 | gcc_assert(this->left_->type()->integer_type() != NULL); | |
5669 | int bits = TYPE_PRECISION(TREE_TYPE(left)); | |
5670 | ||
5671 | tree compare = fold_build2(LT_EXPR, boolean_type_node, right, | |
5672 | build_int_cst_type(TREE_TYPE(right), bits)); | |
5673 | ||
5674 | tree overflow_result = fold_convert_loc(this->location(), | |
5675 | TREE_TYPE(left), | |
5676 | integer_zero_node); | |
5677 | if (this->op_ == OPERATOR_RSHIFT | |
5678 | && !this->left_->type()->integer_type()->is_unsigned()) | |
5679 | { | |
5680 | tree neg = fold_build2_loc(this->location(), LT_EXPR, | |
5681 | boolean_type_node, left, | |
5682 | fold_convert_loc(this->location(), | |
5683 | TREE_TYPE(left), | |
5684 | integer_zero_node)); | |
5685 | tree neg_one = fold_build2_loc(this->location(), | |
5686 | MINUS_EXPR, TREE_TYPE(left), | |
5687 | fold_convert_loc(this->location(), | |
5688 | TREE_TYPE(left), | |
5689 | integer_zero_node), | |
5690 | fold_convert_loc(this->location(), | |
5691 | TREE_TYPE(left), | |
5692 | integer_one_node)); | |
5693 | overflow_result = fold_build3_loc(this->location(), COND_EXPR, | |
5694 | TREE_TYPE(left), neg, neg_one, | |
5695 | overflow_result); | |
5696 | } | |
5697 | ||
5698 | ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left), | |
5699 | compare, ret, overflow_result); | |
5700 | ||
5701 | ret = fold_build2_loc(this->location(), COMPOUND_EXPR, | |
5702 | TREE_TYPE(ret), eval_saved, ret); | |
5703 | } | |
5704 | ||
5705 | return ret; | |
5706 | } | |
5707 | ||
5708 | // Export a binary expression. | |
5709 | ||
5710 | void | |
5711 | Binary_expression::do_export(Export* exp) const | |
5712 | { | |
5713 | exp->write_c_string("("); | |
5714 | this->left_->export_expression(exp); | |
5715 | switch (this->op_) | |
5716 | { | |
5717 | case OPERATOR_OROR: | |
5718 | exp->write_c_string(" || "); | |
5719 | break; | |
5720 | case OPERATOR_ANDAND: | |
5721 | exp->write_c_string(" && "); | |
5722 | break; | |
5723 | case OPERATOR_EQEQ: | |
5724 | exp->write_c_string(" == "); | |
5725 | break; | |
5726 | case OPERATOR_NOTEQ: | |
5727 | exp->write_c_string(" != "); | |
5728 | break; | |
5729 | case OPERATOR_LT: | |
5730 | exp->write_c_string(" < "); | |
5731 | break; | |
5732 | case OPERATOR_LE: | |
5733 | exp->write_c_string(" <= "); | |
5734 | break; | |
5735 | case OPERATOR_GT: | |
5736 | exp->write_c_string(" > "); | |
5737 | break; | |
5738 | case OPERATOR_GE: | |
5739 | exp->write_c_string(" >= "); | |
5740 | break; | |
5741 | case OPERATOR_PLUS: | |
5742 | exp->write_c_string(" + "); | |
5743 | break; | |
5744 | case OPERATOR_MINUS: | |
5745 | exp->write_c_string(" - "); | |
5746 | break; | |
5747 | case OPERATOR_OR: | |
5748 | exp->write_c_string(" | "); | |
5749 | break; | |
5750 | case OPERATOR_XOR: | |
5751 | exp->write_c_string(" ^ "); | |
5752 | break; | |
5753 | case OPERATOR_MULT: | |
5754 | exp->write_c_string(" * "); | |
5755 | break; | |
5756 | case OPERATOR_DIV: | |
5757 | exp->write_c_string(" / "); | |
5758 | break; | |
5759 | case OPERATOR_MOD: | |
5760 | exp->write_c_string(" % "); | |
5761 | break; | |
5762 | case OPERATOR_LSHIFT: | |
5763 | exp->write_c_string(" << "); | |
5764 | break; | |
5765 | case OPERATOR_RSHIFT: | |
5766 | exp->write_c_string(" >> "); | |
5767 | break; | |
5768 | case OPERATOR_AND: | |
5769 | exp->write_c_string(" & "); | |
5770 | break; | |
5771 | case OPERATOR_BITCLEAR: | |
5772 | exp->write_c_string(" &^ "); | |
5773 | break; | |
5774 | default: | |
5775 | gcc_unreachable(); | |
5776 | } | |
5777 | this->right_->export_expression(exp); | |
5778 | exp->write_c_string(")"); | |
5779 | } | |
5780 | ||
5781 | // Import a binary expression. | |
5782 | ||
5783 | Expression* | |
5784 | Binary_expression::do_import(Import* imp) | |
5785 | { | |
5786 | imp->require_c_string("("); | |
5787 | ||
5788 | Expression* left = Expression::import_expression(imp); | |
5789 | ||
5790 | Operator op; | |
5791 | if (imp->match_c_string(" || ")) | |
5792 | { | |
5793 | op = OPERATOR_OROR; | |
5794 | imp->advance(4); | |
5795 | } | |
5796 | else if (imp->match_c_string(" && ")) | |
5797 | { | |
5798 | op = OPERATOR_ANDAND; | |
5799 | imp->advance(4); | |
5800 | } | |
5801 | else if (imp->match_c_string(" == ")) | |
5802 | { | |
5803 | op = OPERATOR_EQEQ; | |
5804 | imp->advance(4); | |
5805 | } | |
5806 | else if (imp->match_c_string(" != ")) | |
5807 | { | |
5808 | op = OPERATOR_NOTEQ; | |
5809 | imp->advance(4); | |
5810 | } | |
5811 | else if (imp->match_c_string(" < ")) | |
5812 | { | |
5813 | op = OPERATOR_LT; | |
5814 | imp->advance(3); | |
5815 | } | |
5816 | else if (imp->match_c_string(" <= ")) | |
5817 | { | |
5818 | op = OPERATOR_LE; | |
5819 | imp->advance(4); | |
5820 | } | |
5821 | else if (imp->match_c_string(" > ")) | |
5822 | { | |
5823 | op = OPERATOR_GT; | |
5824 | imp->advance(3); | |
5825 | } | |
5826 | else if (imp->match_c_string(" >= ")) | |
5827 | { | |
5828 | op = OPERATOR_GE; | |
5829 | imp->advance(4); | |
5830 | } | |
5831 | else if (imp->match_c_string(" + ")) | |
5832 | { | |
5833 | op = OPERATOR_PLUS; | |
5834 | imp->advance(3); | |
5835 | } | |
5836 | else if (imp->match_c_string(" - ")) | |
5837 | { | |
5838 | op = OPERATOR_MINUS; | |
5839 | imp->advance(3); | |
5840 | } | |
5841 | else if (imp->match_c_string(" | ")) | |
5842 | { | |
5843 | op = OPERATOR_OR; | |
5844 | imp->advance(3); | |
5845 | } | |
5846 | else if (imp->match_c_string(" ^ ")) | |
5847 | { | |
5848 | op = OPERATOR_XOR; | |
5849 | imp->advance(3); | |
5850 | } | |
5851 | else if (imp->match_c_string(" * ")) | |
5852 | { | |
5853 | op = OPERATOR_MULT; | |
5854 | imp->advance(3); | |
5855 | } | |
5856 | else if (imp->match_c_string(" / ")) | |
5857 | { | |
5858 | op = OPERATOR_DIV; | |
5859 | imp->advance(3); | |
5860 | } | |
5861 | else if (imp->match_c_string(" % ")) | |
5862 | { | |
5863 | op = OPERATOR_MOD; | |
5864 | imp->advance(3); | |
5865 | } | |
5866 | else if (imp->match_c_string(" << ")) | |
5867 | { | |
5868 | op = OPERATOR_LSHIFT; | |
5869 | imp->advance(4); | |
5870 | } | |
5871 | else if (imp->match_c_string(" >> ")) | |
5872 | { | |
5873 | op = OPERATOR_RSHIFT; | |
5874 | imp->advance(4); | |
5875 | } | |
5876 | else if (imp->match_c_string(" & ")) | |
5877 | { | |
5878 | op = OPERATOR_AND; | |
5879 | imp->advance(3); | |
5880 | } | |
5881 | else if (imp->match_c_string(" &^ ")) | |
5882 | { | |
5883 | op = OPERATOR_BITCLEAR; | |
5884 | imp->advance(4); | |
5885 | } | |
5886 | else | |
5887 | { | |
5888 | error_at(imp->location(), "unrecognized binary operator"); | |
5889 | return Expression::make_error(imp->location()); | |
5890 | } | |
5891 | ||
5892 | Expression* right = Expression::import_expression(imp); | |
5893 | ||
5894 | imp->require_c_string(")"); | |
5895 | ||
5896 | return Expression::make_binary(op, left, right, imp->location()); | |
5897 | } | |
5898 | ||
5899 | // Make a binary expression. | |
5900 | ||
5901 | Expression* | |
5902 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
5903 | source_location location) | |
5904 | { | |
5905 | return new Binary_expression(op, left, right, location); | |
5906 | } | |
5907 | ||
5908 | // Implement a comparison. | |
5909 | ||
5910 | tree | |
5911 | Expression::comparison_tree(Translate_context* context, Operator op, | |
5912 | Type* left_type, tree left_tree, | |
5913 | Type* right_type, tree right_tree, | |
5914 | source_location location) | |
5915 | { | |
5916 | enum tree_code code; | |
5917 | switch (op) | |
5918 | { | |
5919 | case OPERATOR_EQEQ: | |
5920 | code = EQ_EXPR; | |
5921 | break; | |
5922 | case OPERATOR_NOTEQ: | |
5923 | code = NE_EXPR; | |
5924 | break; | |
5925 | case OPERATOR_LT: | |
5926 | code = LT_EXPR; | |
5927 | break; | |
5928 | case OPERATOR_LE: | |
5929 | code = LE_EXPR; | |
5930 | break; | |
5931 | case OPERATOR_GT: | |
5932 | code = GT_EXPR; | |
5933 | break; | |
5934 | case OPERATOR_GE: | |
5935 | code = GE_EXPR; | |
5936 | break; | |
5937 | default: | |
5938 | gcc_unreachable(); | |
5939 | } | |
5940 | ||
15c67ee2 | 5941 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 5942 | { |
e440a328 | 5943 | tree string_type = Type::make_string_type()->get_tree(context->gogo()); |
5944 | static tree string_compare_decl; | |
5945 | left_tree = Gogo::call_builtin(&string_compare_decl, | |
5946 | location, | |
5947 | "__go_strcmp", | |
5948 | 2, | |
5949 | integer_type_node, | |
5950 | string_type, | |
5951 | left_tree, | |
5952 | string_type, | |
5953 | right_tree); | |
5954 | right_tree = build_int_cst_type(integer_type_node, 0); | |
5955 | } | |
15c67ee2 | 5956 | else if ((left_type->interface_type() != NULL |
5957 | && right_type->interface_type() == NULL | |
5958 | && !right_type->is_nil_type()) | |
5959 | || (left_type->interface_type() == NULL | |
5960 | && !left_type->is_nil_type() | |
5961 | && right_type->interface_type() != NULL)) | |
e440a328 | 5962 | { |
5963 | // Comparing an interface value to a non-interface value. | |
5964 | if (left_type->interface_type() == NULL) | |
5965 | { | |
5966 | std::swap(left_type, right_type); | |
5967 | std::swap(left_tree, right_tree); | |
5968 | } | |
5969 | ||
5970 | // The right operand is not an interface. We need to take its | |
5971 | // address if it is not a pointer. | |
5972 | tree make_tmp; | |
5973 | tree arg; | |
5974 | if (right_type->points_to() != NULL) | |
5975 | { | |
5976 | make_tmp = NULL_TREE; | |
5977 | arg = right_tree; | |
5978 | } | |
5979 | else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree)) | |
5980 | { | |
5981 | make_tmp = NULL_TREE; | |
5982 | arg = build_fold_addr_expr_loc(location, right_tree); | |
5983 | if (DECL_P(right_tree)) | |
5984 | TREE_ADDRESSABLE(right_tree) = 1; | |
5985 | } | |
5986 | else | |
5987 | { | |
5988 | tree tmp = create_tmp_var(TREE_TYPE(right_tree), | |
5989 | get_name(right_tree)); | |
5990 | DECL_IGNORED_P(tmp) = 0; | |
5991 | DECL_INITIAL(tmp) = right_tree; | |
5992 | TREE_ADDRESSABLE(tmp) = 1; | |
5993 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
5994 | SET_EXPR_LOCATION(make_tmp, location); | |
5995 | arg = build_fold_addr_expr_loc(location, tmp); | |
5996 | } | |
5997 | arg = fold_convert_loc(location, ptr_type_node, arg); | |
5998 | ||
5999 | tree descriptor = right_type->type_descriptor_pointer(context->gogo()); | |
6000 | ||
6001 | if (left_type->interface_type()->is_empty()) | |
6002 | { | |
6003 | static tree empty_interface_value_compare_decl; | |
6004 | left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl, | |
6005 | location, | |
6006 | "__go_empty_interface_value_compare", | |
6007 | 3, | |
6008 | integer_type_node, | |
6009 | TREE_TYPE(left_tree), | |
6010 | left_tree, | |
6011 | TREE_TYPE(descriptor), | |
6012 | descriptor, | |
6013 | ptr_type_node, | |
6014 | arg); | |
6015 | // This can panic if the type is not comparable. | |
6016 | TREE_NOTHROW(empty_interface_value_compare_decl) = 0; | |
6017 | } | |
6018 | else | |
6019 | { | |
6020 | static tree interface_value_compare_decl; | |
6021 | left_tree = Gogo::call_builtin(&interface_value_compare_decl, | |
6022 | location, | |
6023 | "__go_interface_value_compare", | |
6024 | 3, | |
6025 | integer_type_node, | |
6026 | TREE_TYPE(left_tree), | |
6027 | left_tree, | |
6028 | TREE_TYPE(descriptor), | |
6029 | descriptor, | |
6030 | ptr_type_node, | |
6031 | arg); | |
6032 | // This can panic if the type is not comparable. | |
6033 | TREE_NOTHROW(interface_value_compare_decl) = 0; | |
6034 | } | |
6035 | right_tree = build_int_cst_type(integer_type_node, 0); | |
6036 | ||
6037 | if (make_tmp != NULL_TREE) | |
6038 | left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp, | |
6039 | left_tree); | |
6040 | } | |
6041 | else if (left_type->interface_type() != NULL | |
6042 | && right_type->interface_type() != NULL) | |
6043 | { | |
6044 | if (left_type->interface_type()->is_empty()) | |
6045 | { | |
6046 | gcc_assert(right_type->interface_type()->is_empty()); | |
6047 | static tree empty_interface_compare_decl; | |
6048 | left_tree = Gogo::call_builtin(&empty_interface_compare_decl, | |
6049 | location, | |
6050 | "__go_empty_interface_compare", | |
6051 | 2, | |
6052 | integer_type_node, | |
6053 | TREE_TYPE(left_tree), | |
6054 | left_tree, | |
6055 | TREE_TYPE(right_tree), | |
6056 | right_tree); | |
6057 | // This can panic if the type is uncomparable. | |
6058 | TREE_NOTHROW(empty_interface_compare_decl) = 0; | |
6059 | } | |
6060 | else | |
6061 | { | |
6062 | gcc_assert(!right_type->interface_type()->is_empty()); | |
6063 | static tree interface_compare_decl; | |
6064 | left_tree = Gogo::call_builtin(&interface_compare_decl, | |
6065 | location, | |
6066 | "__go_interface_compare", | |
6067 | 2, | |
6068 | integer_type_node, | |
6069 | TREE_TYPE(left_tree), | |
6070 | left_tree, | |
6071 | TREE_TYPE(right_tree), | |
6072 | right_tree); | |
6073 | // This can panic if the type is uncomparable. | |
6074 | TREE_NOTHROW(interface_compare_decl) = 0; | |
6075 | } | |
6076 | right_tree = build_int_cst_type(integer_type_node, 0); | |
6077 | } | |
6078 | ||
6079 | if (left_type->is_nil_type() | |
6080 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6081 | { | |
6082 | std::swap(left_type, right_type); | |
6083 | std::swap(left_tree, right_tree); | |
6084 | } | |
6085 | ||
6086 | if (right_type->is_nil_type()) | |
6087 | { | |
6088 | if (left_type->array_type() != NULL | |
6089 | && left_type->array_type()->length() == NULL) | |
6090 | { | |
6091 | Array_type* at = left_type->array_type(); | |
6092 | left_tree = at->value_pointer_tree(context->gogo(), left_tree); | |
6093 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6094 | } | |
6095 | else if (left_type->interface_type() != NULL) | |
6096 | { | |
6097 | // An interface is nil if the first field is nil. | |
6098 | tree left_type_tree = TREE_TYPE(left_tree); | |
6099 | gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE); | |
6100 | tree field = TYPE_FIELDS(left_type_tree); | |
6101 | left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree, | |
6102 | field, NULL_TREE); | |
6103 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6104 | } | |
6105 | else | |
6106 | { | |
6107 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree))); | |
6108 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6109 | } | |
6110 | } | |
6111 | ||
d8ccb1e3 | 6112 | if (left_tree == error_mark_node || right_tree == error_mark_node) |
6113 | return error_mark_node; | |
6114 | ||
e440a328 | 6115 | tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree); |
6116 | if (CAN_HAVE_LOCATION_P(ret)) | |
6117 | SET_EXPR_LOCATION(ret, location); | |
6118 | return ret; | |
6119 | } | |
6120 | ||
6121 | // Class Bound_method_expression. | |
6122 | ||
6123 | // Traversal. | |
6124 | ||
6125 | int | |
6126 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6127 | { | |
6128 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
6129 | return TRAVERSE_EXIT; | |
6130 | return Expression::traverse(&this->method_, traverse); | |
6131 | } | |
6132 | ||
6133 | // Return the type of a bound method expression. The type of this | |
6134 | // object is really the type of the method with no receiver. We | |
6135 | // should be able to get away with just returning the type of the | |
6136 | // method. | |
6137 | ||
6138 | Type* | |
6139 | Bound_method_expression::do_type() | |
6140 | { | |
6141 | return this->method_->type(); | |
6142 | } | |
6143 | ||
6144 | // Determine the types of a method expression. | |
6145 | ||
6146 | void | |
6147 | Bound_method_expression::do_determine_type(const Type_context*) | |
6148 | { | |
6149 | this->method_->determine_type_no_context(); | |
6150 | Type* mtype = this->method_->type(); | |
6151 | Function_type* fntype = mtype == NULL ? NULL : mtype->function_type(); | |
6152 | if (fntype == NULL || !fntype->is_method()) | |
6153 | this->expr_->determine_type_no_context(); | |
6154 | else | |
6155 | { | |
6156 | Type_context subcontext(fntype->receiver()->type(), false); | |
6157 | this->expr_->determine_type(&subcontext); | |
6158 | } | |
6159 | } | |
6160 | ||
6161 | // Check the types of a method expression. | |
6162 | ||
6163 | void | |
6164 | Bound_method_expression::do_check_types(Gogo*) | |
6165 | { | |
6166 | Type* type = this->method_->type()->deref(); | |
6167 | if (type == NULL | |
6168 | || type->function_type() == NULL | |
6169 | || !type->function_type()->is_method()) | |
6170 | this->report_error(_("object is not a method")); | |
6171 | else | |
6172 | { | |
6173 | Type* rtype = type->function_type()->receiver()->type()->deref(); | |
6174 | Type* etype = (this->expr_type_ != NULL | |
6175 | ? this->expr_type_ | |
6176 | : this->expr_->type()); | |
6177 | etype = etype->deref(); | |
07ba8be5 | 6178 | if (!Type::are_identical(rtype, etype, true, NULL)) |
e440a328 | 6179 | this->report_error(_("method type does not match object type")); |
6180 | } | |
6181 | } | |
6182 | ||
6183 | // Get the tree for a method expression. There is no standard tree | |
6184 | // representation for this. The only places it may currently be used | |
6185 | // are in a Call_expression or a Go_statement, which will take it | |
6186 | // apart directly. So this has nothing to do at present. | |
6187 | ||
6188 | tree | |
6189 | Bound_method_expression::do_get_tree(Translate_context*) | |
6190 | { | |
6191 | gcc_unreachable(); | |
6192 | } | |
6193 | ||
6194 | // Make a method expression. | |
6195 | ||
6196 | Bound_method_expression* | |
6197 | Expression::make_bound_method(Expression* expr, Expression* method, | |
6198 | source_location location) | |
6199 | { | |
6200 | return new Bound_method_expression(expr, method, location); | |
6201 | } | |
6202 | ||
6203 | // Class Builtin_call_expression. This is used for a call to a | |
6204 | // builtin function. | |
6205 | ||
6206 | class Builtin_call_expression : public Call_expression | |
6207 | { | |
6208 | public: | |
6209 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
6210 | bool is_varargs, source_location location); | |
6211 | ||
6212 | protected: | |
6213 | // This overrides Call_expression::do_lower. | |
6214 | Expression* | |
6215 | do_lower(Gogo*, Named_object*, int); | |
6216 | ||
6217 | bool | |
6218 | do_is_constant() const; | |
6219 | ||
6220 | bool | |
6221 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
6222 | ||
6223 | bool | |
6224 | do_float_constant_value(mpfr_t, Type**) const; | |
6225 | ||
6226 | bool | |
6227 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
6228 | ||
6229 | Type* | |
6230 | do_type(); | |
6231 | ||
6232 | void | |
6233 | do_determine_type(const Type_context*); | |
6234 | ||
6235 | void | |
6236 | do_check_types(Gogo*); | |
6237 | ||
6238 | Expression* | |
6239 | do_copy() | |
6240 | { | |
6241 | return new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
6242 | this->args()->copy(), | |
6243 | this->is_varargs(), | |
6244 | this->location()); | |
6245 | } | |
6246 | ||
6247 | tree | |
6248 | do_get_tree(Translate_context*); | |
6249 | ||
6250 | void | |
6251 | do_export(Export*) const; | |
6252 | ||
6253 | virtual bool | |
6254 | do_is_recover_call() const; | |
6255 | ||
6256 | virtual void | |
6257 | do_set_recover_arg(Expression*); | |
6258 | ||
6259 | private: | |
6260 | // The builtin functions. | |
6261 | enum Builtin_function_code | |
6262 | { | |
6263 | BUILTIN_INVALID, | |
6264 | ||
6265 | // Predeclared builtin functions. | |
6266 | BUILTIN_APPEND, | |
6267 | BUILTIN_CAP, | |
6268 | BUILTIN_CLOSE, | |
6269 | BUILTIN_CLOSED, | |
6270 | BUILTIN_CMPLX, | |
6271 | BUILTIN_COPY, | |
6272 | BUILTIN_IMAG, | |
6273 | BUILTIN_LEN, | |
6274 | BUILTIN_MAKE, | |
6275 | BUILTIN_NEW, | |
6276 | BUILTIN_PANIC, | |
6277 | BUILTIN_PRINT, | |
6278 | BUILTIN_PRINTLN, | |
6279 | BUILTIN_REAL, | |
6280 | BUILTIN_RECOVER, | |
6281 | ||
6282 | // Builtin functions from the unsafe package. | |
6283 | BUILTIN_ALIGNOF, | |
6284 | BUILTIN_OFFSETOF, | |
6285 | BUILTIN_SIZEOF | |
6286 | }; | |
6287 | ||
6288 | Expression* | |
6289 | one_arg() const; | |
6290 | ||
6291 | bool | |
6292 | check_one_arg(); | |
6293 | ||
6294 | static Type* | |
6295 | real_imag_type(Type*); | |
6296 | ||
6297 | static Type* | |
6298 | cmplx_type(Type*); | |
6299 | ||
6300 | // A pointer back to the general IR structure. This avoids a global | |
6301 | // variable, or passing it around everywhere. | |
6302 | Gogo* gogo_; | |
6303 | // The builtin function being called. | |
6304 | Builtin_function_code code_; | |
6305 | }; | |
6306 | ||
6307 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
6308 | Expression* fn, | |
6309 | Expression_list* args, | |
6310 | bool is_varargs, | |
6311 | source_location location) | |
6312 | : Call_expression(fn, args, is_varargs, location), | |
6313 | gogo_(gogo), code_(BUILTIN_INVALID) | |
6314 | { | |
6315 | Func_expression* fnexp = this->fn()->func_expression(); | |
6316 | gcc_assert(fnexp != NULL); | |
6317 | const std::string& name(fnexp->named_object()->name()); | |
6318 | if (name == "append") | |
6319 | this->code_ = BUILTIN_APPEND; | |
6320 | else if (name == "cap") | |
6321 | this->code_ = BUILTIN_CAP; | |
6322 | else if (name == "close") | |
6323 | this->code_ = BUILTIN_CLOSE; | |
6324 | else if (name == "closed") | |
6325 | this->code_ = BUILTIN_CLOSED; | |
6326 | else if (name == "cmplx") | |
6327 | this->code_ = BUILTIN_CMPLX; | |
6328 | else if (name == "copy") | |
6329 | this->code_ = BUILTIN_COPY; | |
6330 | else if (name == "imag") | |
6331 | this->code_ = BUILTIN_IMAG; | |
6332 | else if (name == "len") | |
6333 | this->code_ = BUILTIN_LEN; | |
6334 | else if (name == "make") | |
6335 | this->code_ = BUILTIN_MAKE; | |
6336 | else if (name == "new") | |
6337 | this->code_ = BUILTIN_NEW; | |
6338 | else if (name == "panic") | |
6339 | this->code_ = BUILTIN_PANIC; | |
6340 | else if (name == "print") | |
6341 | this->code_ = BUILTIN_PRINT; | |
6342 | else if (name == "println") | |
6343 | this->code_ = BUILTIN_PRINTLN; | |
6344 | else if (name == "real") | |
6345 | this->code_ = BUILTIN_REAL; | |
6346 | else if (name == "recover") | |
6347 | this->code_ = BUILTIN_RECOVER; | |
6348 | else if (name == "Alignof") | |
6349 | this->code_ = BUILTIN_ALIGNOF; | |
6350 | else if (name == "Offsetof") | |
6351 | this->code_ = BUILTIN_OFFSETOF; | |
6352 | else if (name == "Sizeof") | |
6353 | this->code_ = BUILTIN_SIZEOF; | |
6354 | else | |
6355 | gcc_unreachable(); | |
6356 | } | |
6357 | ||
6358 | // Return whether this is a call to recover. This is a virtual | |
6359 | // function called from the parent class. | |
6360 | ||
6361 | bool | |
6362 | Builtin_call_expression::do_is_recover_call() const | |
6363 | { | |
6364 | if (this->classification() == EXPRESSION_ERROR) | |
6365 | return false; | |
6366 | return this->code_ == BUILTIN_RECOVER; | |
6367 | } | |
6368 | ||
6369 | // Set the argument for a call to recover. | |
6370 | ||
6371 | void | |
6372 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
6373 | { | |
6374 | const Expression_list* args = this->args(); | |
6375 | gcc_assert(args == NULL || args->empty()); | |
6376 | Expression_list* new_args = new Expression_list(); | |
6377 | new_args->push_back(arg); | |
6378 | this->set_args(new_args); | |
6379 | } | |
6380 | ||
6381 | // A traversal class which looks for a call expression. | |
6382 | ||
6383 | class Find_call_expression : public Traverse | |
6384 | { | |
6385 | public: | |
6386 | Find_call_expression() | |
6387 | : Traverse(traverse_expressions), | |
6388 | found_(false) | |
6389 | { } | |
6390 | ||
6391 | int | |
6392 | expression(Expression**); | |
6393 | ||
6394 | bool | |
6395 | found() | |
6396 | { return this->found_; } | |
6397 | ||
6398 | private: | |
6399 | bool found_; | |
6400 | }; | |
6401 | ||
6402 | int | |
6403 | Find_call_expression::expression(Expression** pexpr) | |
6404 | { | |
6405 | if ((*pexpr)->call_expression() != NULL) | |
6406 | { | |
6407 | this->found_ = true; | |
6408 | return TRAVERSE_EXIT; | |
6409 | } | |
6410 | return TRAVERSE_CONTINUE; | |
6411 | } | |
6412 | ||
6413 | // Lower a builtin call expression. This turns new and make into | |
6414 | // specific expressions. We also convert to a constant if we can. | |
6415 | ||
6416 | Expression* | |
6417 | Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
6418 | { | |
6419 | if (this->code_ == BUILTIN_NEW) | |
6420 | { | |
6421 | const Expression_list* args = this->args(); | |
6422 | if (args == NULL || args->size() < 1) | |
6423 | this->report_error(_("not enough arguments")); | |
6424 | else if (args->size() > 1) | |
6425 | this->report_error(_("too many arguments")); | |
6426 | else | |
6427 | { | |
6428 | Expression* arg = args->front(); | |
6429 | if (!arg->is_type_expression()) | |
6430 | { | |
6431 | error_at(arg->location(), "expected type"); | |
6432 | this->set_is_error(); | |
6433 | } | |
6434 | else | |
6435 | return Expression::make_allocation(arg->type(), this->location()); | |
6436 | } | |
6437 | } | |
6438 | else if (this->code_ == BUILTIN_MAKE) | |
6439 | { | |
6440 | const Expression_list* args = this->args(); | |
6441 | if (args == NULL || args->size() < 1) | |
6442 | this->report_error(_("not enough arguments")); | |
6443 | else | |
6444 | { | |
6445 | Expression* arg = args->front(); | |
6446 | if (!arg->is_type_expression()) | |
6447 | { | |
6448 | error_at(arg->location(), "expected type"); | |
6449 | this->set_is_error(); | |
6450 | } | |
6451 | else | |
6452 | { | |
6453 | Expression_list* newargs; | |
6454 | if (args->size() == 1) | |
6455 | newargs = NULL; | |
6456 | else | |
6457 | { | |
6458 | newargs = new Expression_list(); | |
6459 | Expression_list::const_iterator p = args->begin(); | |
6460 | ++p; | |
6461 | for (; p != args->end(); ++p) | |
6462 | newargs->push_back(*p); | |
6463 | } | |
6464 | return Expression::make_make(arg->type(), newargs, | |
6465 | this->location()); | |
6466 | } | |
6467 | } | |
6468 | } | |
6469 | else if (this->is_constant()) | |
6470 | { | |
6471 | // We can only lower len and cap if there are no function calls | |
6472 | // in the arguments. Otherwise we have to make the call. | |
6473 | if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP) | |
6474 | { | |
6475 | Expression* arg = this->one_arg(); | |
6476 | if (!arg->is_constant()) | |
6477 | { | |
6478 | Find_call_expression find_call; | |
6479 | Expression::traverse(&arg, &find_call); | |
6480 | if (find_call.found()) | |
6481 | return this; | |
6482 | } | |
6483 | } | |
6484 | ||
6485 | mpz_t ival; | |
6486 | mpz_init(ival); | |
6487 | Type* type; | |
6488 | if (this->integer_constant_value(true, ival, &type)) | |
6489 | { | |
6490 | Expression* ret = Expression::make_integer(&ival, type, | |
6491 | this->location()); | |
6492 | mpz_clear(ival); | |
6493 | return ret; | |
6494 | } | |
6495 | mpz_clear(ival); | |
6496 | ||
6497 | mpfr_t rval; | |
6498 | mpfr_init(rval); | |
6499 | if (this->float_constant_value(rval, &type)) | |
6500 | { | |
6501 | Expression* ret = Expression::make_float(&rval, type, | |
6502 | this->location()); | |
6503 | mpfr_clear(rval); | |
6504 | return ret; | |
6505 | } | |
6506 | ||
6507 | mpfr_t imag; | |
6508 | mpfr_init(imag); | |
6509 | if (this->complex_constant_value(rval, imag, &type)) | |
6510 | { | |
6511 | Expression* ret = Expression::make_complex(&rval, &imag, type, | |
6512 | this->location()); | |
6513 | mpfr_clear(rval); | |
6514 | mpfr_clear(imag); | |
6515 | return ret; | |
6516 | } | |
6517 | mpfr_clear(rval); | |
6518 | mpfr_clear(imag); | |
6519 | } | |
6520 | else if (this->code_ == BUILTIN_RECOVER) | |
6521 | { | |
6522 | if (function != NULL) | |
6523 | function->func_value()->set_calls_recover(); | |
6524 | else | |
6525 | { | |
6526 | // Calling recover outside of a function always returns the | |
6527 | // nil empty interface. | |
6528 | Type* eface = Type::make_interface_type(NULL, this->location()); | |
6529 | return Expression::make_cast(eface, | |
6530 | Expression::make_nil(this->location()), | |
6531 | this->location()); | |
6532 | } | |
6533 | } | |
6534 | else if (this->code_ == BUILTIN_APPEND) | |
6535 | { | |
6536 | // Lower the varargs. | |
6537 | const Expression_list* args = this->args(); | |
6538 | if (args == NULL || args->empty()) | |
6539 | return this; | |
6540 | Type* slice_type = args->front()->type(); | |
6541 | if (!slice_type->is_open_array_type()) | |
6542 | { | |
6543 | error_at(args->front()->location(), "argument 1 must be a slice"); | |
6544 | this->set_is_error(); | |
6545 | return this; | |
6546 | } | |
6547 | return this->lower_varargs(gogo, function, slice_type, 2); | |
6548 | } | |
6549 | ||
6550 | return this; | |
6551 | } | |
6552 | ||
6553 | // Return the type of the real or imag functions, given the type of | |
6554 | // the argument. We need to map complex to float, complex64 to | |
6555 | // float32, and complex128 to float64, so it has to be done by name. | |
6556 | // This returns NULL if it can't figure out the type. | |
6557 | ||
6558 | Type* | |
6559 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
6560 | { | |
6561 | if (arg_type == NULL || arg_type->is_abstract()) | |
6562 | return NULL; | |
6563 | Named_type* nt = arg_type->named_type(); | |
6564 | if (nt == NULL) | |
6565 | return NULL; | |
6566 | while (nt->real_type()->named_type() != NULL) | |
6567 | nt = nt->real_type()->named_type(); | |
6568 | if (nt->name() == "complex") | |
6569 | return Type::lookup_float_type("float"); | |
6570 | else if (nt->name() == "complex64") | |
6571 | return Type::lookup_float_type("float32"); | |
6572 | else if (nt->name() == "complex128") | |
6573 | return Type::lookup_float_type("float64"); | |
6574 | else | |
6575 | return NULL; | |
6576 | } | |
6577 | ||
6578 | // Return the type of the cmplx function, given the type of one of the | |
6579 | // argments. Like real_imag_type, we have to map by name. | |
6580 | ||
6581 | Type* | |
6582 | Builtin_call_expression::cmplx_type(Type* arg_type) | |
6583 | { | |
6584 | if (arg_type == NULL || arg_type->is_abstract()) | |
6585 | return NULL; | |
6586 | Named_type* nt = arg_type->named_type(); | |
6587 | if (nt == NULL) | |
6588 | return NULL; | |
6589 | while (nt->real_type()->named_type() != NULL) | |
6590 | nt = nt->real_type()->named_type(); | |
6591 | if (nt->name() == "float") | |
6592 | return Type::lookup_complex_type("complex"); | |
6593 | else if (nt->name() == "float32") | |
6594 | return Type::lookup_complex_type("complex64"); | |
6595 | else if (nt->name() == "float64") | |
6596 | return Type::lookup_complex_type("complex128"); | |
6597 | else | |
6598 | return NULL; | |
6599 | } | |
6600 | ||
6601 | // Return a single argument, or NULL if there isn't one. | |
6602 | ||
6603 | Expression* | |
6604 | Builtin_call_expression::one_arg() const | |
6605 | { | |
6606 | const Expression_list* args = this->args(); | |
6607 | if (args->size() != 1) | |
6608 | return NULL; | |
6609 | return args->front(); | |
6610 | } | |
6611 | ||
6612 | // Return whether this is constant: len of a string, or len or cap of | |
6613 | // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof. | |
6614 | ||
6615 | bool | |
6616 | Builtin_call_expression::do_is_constant() const | |
6617 | { | |
6618 | switch (this->code_) | |
6619 | { | |
6620 | case BUILTIN_LEN: | |
6621 | case BUILTIN_CAP: | |
6622 | { | |
6623 | Expression* arg = this->one_arg(); | |
6624 | if (arg == NULL) | |
6625 | return false; | |
6626 | Type* arg_type = arg->type(); | |
6627 | ||
6628 | if (arg_type->points_to() != NULL | |
6629 | && arg_type->points_to()->array_type() != NULL | |
6630 | && !arg_type->points_to()->is_open_array_type()) | |
6631 | arg_type = arg_type->points_to(); | |
6632 | ||
6633 | if (arg_type->array_type() != NULL | |
6634 | && arg_type->array_type()->length() != NULL) | |
6635 | return arg_type->array_type()->length()->is_constant(); | |
6636 | ||
6637 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
6638 | return arg->is_constant(); | |
6639 | } | |
6640 | break; | |
6641 | ||
6642 | case BUILTIN_SIZEOF: | |
6643 | case BUILTIN_ALIGNOF: | |
6644 | return this->one_arg() != NULL; | |
6645 | ||
6646 | case BUILTIN_OFFSETOF: | |
6647 | { | |
6648 | Expression* arg = this->one_arg(); | |
6649 | if (arg == NULL) | |
6650 | return false; | |
6651 | return arg->field_reference_expression() != NULL; | |
6652 | } | |
6653 | ||
6654 | case BUILTIN_CMPLX: | |
6655 | { | |
6656 | const Expression_list* args = this->args(); | |
6657 | if (args != NULL && args->size() == 2) | |
6658 | return args->front()->is_constant() && args->back()->is_constant(); | |
6659 | } | |
6660 | break; | |
6661 | ||
6662 | case BUILTIN_REAL: | |
6663 | case BUILTIN_IMAG: | |
6664 | { | |
6665 | Expression* arg = this->one_arg(); | |
6666 | return arg != NULL && arg->is_constant(); | |
6667 | } | |
6668 | ||
6669 | default: | |
6670 | break; | |
6671 | } | |
6672 | ||
6673 | return false; | |
6674 | } | |
6675 | ||
6676 | // Return an integer constant value if possible. | |
6677 | ||
6678 | bool | |
6679 | Builtin_call_expression::do_integer_constant_value(bool iota_is_constant, | |
6680 | mpz_t val, | |
6681 | Type** ptype) const | |
6682 | { | |
6683 | if (this->code_ == BUILTIN_LEN | |
6684 | || this->code_ == BUILTIN_CAP) | |
6685 | { | |
6686 | Expression* arg = this->one_arg(); | |
6687 | if (arg == NULL) | |
6688 | return false; | |
6689 | Type* arg_type = arg->type(); | |
6690 | ||
6691 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
6692 | { | |
6693 | std::string sval; | |
6694 | if (arg->string_constant_value(&sval)) | |
6695 | { | |
6696 | mpz_set_ui(val, sval.length()); | |
6697 | *ptype = Type::lookup_integer_type("int"); | |
6698 | return true; | |
6699 | } | |
6700 | } | |
6701 | ||
6702 | if (arg_type->points_to() != NULL | |
6703 | && arg_type->points_to()->array_type() != NULL | |
6704 | && !arg_type->points_to()->is_open_array_type()) | |
6705 | arg_type = arg_type->points_to(); | |
6706 | ||
6707 | if (arg_type->array_type() != NULL | |
6708 | && arg_type->array_type()->length() != NULL) | |
6709 | { | |
6710 | Expression* e = arg_type->array_type()->length(); | |
6711 | if (e->integer_constant_value(iota_is_constant, val, ptype)) | |
6712 | { | |
6713 | *ptype = Type::lookup_integer_type("int"); | |
6714 | return true; | |
6715 | } | |
6716 | } | |
6717 | } | |
6718 | else if (this->code_ == BUILTIN_SIZEOF | |
6719 | || this->code_ == BUILTIN_ALIGNOF) | |
6720 | { | |
6721 | Expression* arg = this->one_arg(); | |
6722 | if (arg == NULL) | |
6723 | return false; | |
6724 | Type* arg_type = arg->type(); | |
6725 | if (arg_type->is_error_type()) | |
6726 | return false; | |
6727 | if (arg_type->is_abstract()) | |
6728 | return false; | |
6729 | tree arg_type_tree = arg_type->get_tree(this->gogo_); | |
6730 | unsigned long val_long; | |
6731 | if (this->code_ == BUILTIN_SIZEOF) | |
6732 | { | |
6733 | tree type_size = TYPE_SIZE_UNIT(arg_type_tree); | |
6734 | gcc_assert(TREE_CODE(type_size) == INTEGER_CST); | |
6735 | if (TREE_INT_CST_HIGH(type_size) != 0) | |
6736 | return false; | |
6737 | unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size); | |
6738 | val_long = static_cast<unsigned long>(val_wide); | |
6739 | if (val_long != val_wide) | |
6740 | return false; | |
6741 | } | |
6742 | else if (this->code_ == BUILTIN_ALIGNOF) | |
6743 | { | |
637bd3af | 6744 | if (arg->field_reference_expression() == NULL) |
6745 | val_long = go_type_alignment(arg_type_tree); | |
6746 | else | |
e440a328 | 6747 | { |
6748 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
6749 | // the type of f when it is used as a field in a struct. | |
637bd3af | 6750 | val_long = go_field_alignment(arg_type_tree); |
e440a328 | 6751 | } |
e440a328 | 6752 | } |
6753 | else | |
6754 | gcc_unreachable(); | |
6755 | mpz_set_ui(val, val_long); | |
6756 | *ptype = NULL; | |
6757 | return true; | |
6758 | } | |
6759 | else if (this->code_ == BUILTIN_OFFSETOF) | |
6760 | { | |
6761 | Expression* arg = this->one_arg(); | |
6762 | if (arg == NULL) | |
6763 | return false; | |
6764 | Field_reference_expression* farg = arg->field_reference_expression(); | |
6765 | if (farg == NULL) | |
6766 | return false; | |
6767 | Expression* struct_expr = farg->expr(); | |
6768 | Type* st = struct_expr->type(); | |
6769 | if (st->struct_type() == NULL) | |
6770 | return false; | |
6771 | tree struct_tree = st->get_tree(this->gogo_); | |
6772 | gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE); | |
6773 | tree field = TYPE_FIELDS(struct_tree); | |
6774 | for (unsigned int index = farg->field_index(); index > 0; --index) | |
6775 | { | |
6776 | field = DECL_CHAIN(field); | |
6777 | gcc_assert(field != NULL_TREE); | |
6778 | } | |
6779 | HOST_WIDE_INT offset_wide = int_byte_position (field); | |
6780 | if (offset_wide < 0) | |
6781 | return false; | |
6782 | unsigned long offset_long = static_cast<unsigned long>(offset_wide); | |
6783 | if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide)) | |
6784 | return false; | |
6785 | mpz_set_ui(val, offset_long); | |
6786 | return true; | |
6787 | } | |
6788 | return false; | |
6789 | } | |
6790 | ||
6791 | // Return a floating point constant value if possible. | |
6792 | ||
6793 | bool | |
6794 | Builtin_call_expression::do_float_constant_value(mpfr_t val, | |
6795 | Type** ptype) const | |
6796 | { | |
6797 | if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) | |
6798 | { | |
6799 | Expression* arg = this->one_arg(); | |
6800 | if (arg == NULL) | |
6801 | return false; | |
6802 | ||
6803 | mpfr_t real; | |
6804 | mpfr_t imag; | |
6805 | mpfr_init(real); | |
6806 | mpfr_init(imag); | |
6807 | ||
6808 | bool ret = false; | |
6809 | Type* type; | |
6810 | if (arg->complex_constant_value(real, imag, &type)) | |
6811 | { | |
6812 | if (this->code_ == BUILTIN_REAL) | |
6813 | mpfr_set(val, real, GMP_RNDN); | |
6814 | else | |
6815 | mpfr_set(val, imag, GMP_RNDN); | |
6816 | *ptype = Builtin_call_expression::real_imag_type(type); | |
6817 | ret = true; | |
6818 | } | |
6819 | ||
6820 | mpfr_clear(real); | |
6821 | mpfr_clear(imag); | |
6822 | return ret; | |
6823 | } | |
6824 | ||
6825 | return false; | |
6826 | } | |
6827 | ||
6828 | // Return a complex constant value if possible. | |
6829 | ||
6830 | bool | |
6831 | Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
6832 | Type** ptype) const | |
6833 | { | |
6834 | if (this->code_ == BUILTIN_CMPLX) | |
6835 | { | |
6836 | const Expression_list* args = this->args(); | |
6837 | if (args == NULL || args->size() != 2) | |
6838 | return false; | |
6839 | ||
6840 | mpfr_t r; | |
6841 | mpfr_init(r); | |
6842 | Type* rtype; | |
6843 | if (!args->front()->float_constant_value(r, &rtype)) | |
6844 | { | |
6845 | mpfr_clear(r); | |
6846 | return false; | |
6847 | } | |
6848 | ||
6849 | mpfr_t i; | |
6850 | mpfr_init(i); | |
6851 | ||
6852 | bool ret = false; | |
6853 | Type* itype; | |
6854 | if (args->back()->float_constant_value(i, &itype) | |
07ba8be5 | 6855 | && Type::are_identical(rtype, itype, false, NULL)) |
e440a328 | 6856 | { |
6857 | mpfr_set(real, r, GMP_RNDN); | |
6858 | mpfr_set(imag, i, GMP_RNDN); | |
6859 | *ptype = Builtin_call_expression::cmplx_type(rtype); | |
6860 | ret = true; | |
6861 | } | |
6862 | ||
6863 | mpfr_clear(r); | |
6864 | mpfr_clear(i); | |
6865 | ||
6866 | return ret; | |
6867 | } | |
6868 | ||
6869 | return false; | |
6870 | } | |
6871 | ||
6872 | // Return the type. | |
6873 | ||
6874 | Type* | |
6875 | Builtin_call_expression::do_type() | |
6876 | { | |
6877 | switch (this->code_) | |
6878 | { | |
6879 | case BUILTIN_INVALID: | |
6880 | default: | |
6881 | gcc_unreachable(); | |
6882 | ||
6883 | case BUILTIN_NEW: | |
6884 | case BUILTIN_MAKE: | |
6885 | { | |
6886 | const Expression_list* args = this->args(); | |
6887 | if (args == NULL || args->empty()) | |
6888 | return Type::make_error_type(); | |
6889 | return Type::make_pointer_type(args->front()->type()); | |
6890 | } | |
6891 | ||
6892 | case BUILTIN_CAP: | |
6893 | case BUILTIN_COPY: | |
6894 | case BUILTIN_LEN: | |
6895 | case BUILTIN_ALIGNOF: | |
6896 | case BUILTIN_OFFSETOF: | |
6897 | case BUILTIN_SIZEOF: | |
6898 | return Type::lookup_integer_type("int"); | |
6899 | ||
6900 | case BUILTIN_CLOSE: | |
6901 | case BUILTIN_PANIC: | |
6902 | case BUILTIN_PRINT: | |
6903 | case BUILTIN_PRINTLN: | |
6904 | return Type::make_void_type(); | |
6905 | ||
6906 | case BUILTIN_CLOSED: | |
6907 | return Type::lookup_bool_type(); | |
6908 | ||
6909 | case BUILTIN_RECOVER: | |
6910 | return Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
6911 | ||
6912 | case BUILTIN_APPEND: | |
6913 | { | |
6914 | const Expression_list* args = this->args(); | |
6915 | if (args == NULL || args->empty()) | |
6916 | return Type::make_error_type(); | |
6917 | return args->front()->type(); | |
6918 | } | |
6919 | ||
6920 | case BUILTIN_REAL: | |
6921 | case BUILTIN_IMAG: | |
6922 | { | |
6923 | Expression* arg = this->one_arg(); | |
6924 | if (arg == NULL) | |
6925 | return Type::make_error_type(); | |
6926 | Type* t = arg->type(); | |
6927 | if (t->is_abstract()) | |
6928 | t = t->make_non_abstract_type(); | |
6929 | t = Builtin_call_expression::real_imag_type(t); | |
6930 | if (t == NULL) | |
6931 | t = Type::make_error_type(); | |
6932 | return t; | |
6933 | } | |
6934 | ||
6935 | case BUILTIN_CMPLX: | |
6936 | { | |
6937 | const Expression_list* args = this->args(); | |
6938 | if (args == NULL || args->size() != 2) | |
6939 | return Type::make_error_type(); | |
6940 | Type* t = args->front()->type(); | |
6941 | if (t->is_abstract()) | |
6942 | { | |
6943 | t = args->back()->type(); | |
6944 | if (t->is_abstract()) | |
6945 | t = t->make_non_abstract_type(); | |
6946 | } | |
6947 | t = Builtin_call_expression::cmplx_type(t); | |
6948 | if (t == NULL) | |
6949 | t = Type::make_error_type(); | |
6950 | return t; | |
6951 | } | |
6952 | } | |
6953 | } | |
6954 | ||
6955 | // Determine the type. | |
6956 | ||
6957 | void | |
6958 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
6959 | { | |
6960 | this->fn()->determine_type_no_context(); | |
6961 | ||
6962 | const Expression_list* args = this->args(); | |
6963 | ||
6964 | bool is_print; | |
6965 | Type* arg_type = NULL; | |
6966 | switch (this->code_) | |
6967 | { | |
6968 | case BUILTIN_PRINT: | |
6969 | case BUILTIN_PRINTLN: | |
6970 | // Do not force a large integer constant to "int". | |
6971 | is_print = true; | |
6972 | break; | |
6973 | ||
6974 | case BUILTIN_REAL: | |
6975 | case BUILTIN_IMAG: | |
6976 | arg_type = Builtin_call_expression::cmplx_type(context->type); | |
6977 | is_print = false; | |
6978 | break; | |
6979 | ||
6980 | case BUILTIN_CMPLX: | |
6981 | { | |
6982 | // For the cmplx function the type of one operand can | |
6983 | // determine the type of the other, as in a binary expression. | |
6984 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
6985 | if (args != NULL && args->size() == 2) | |
6986 | { | |
6987 | Type* t1 = args->front()->type(); | |
6988 | Type* t2 = args->front()->type(); | |
6989 | if (!t1->is_abstract()) | |
6990 | arg_type = t1; | |
6991 | else if (!t2->is_abstract()) | |
6992 | arg_type = t2; | |
6993 | } | |
6994 | is_print = false; | |
6995 | } | |
6996 | break; | |
6997 | ||
6998 | default: | |
6999 | is_print = false; | |
7000 | break; | |
7001 | } | |
7002 | ||
7003 | if (args != NULL) | |
7004 | { | |
7005 | for (Expression_list::const_iterator pa = args->begin(); | |
7006 | pa != args->end(); | |
7007 | ++pa) | |
7008 | { | |
7009 | Type_context subcontext; | |
7010 | subcontext.type = arg_type; | |
7011 | ||
7012 | if (is_print) | |
7013 | { | |
7014 | // We want to print large constants, we so can't just | |
7015 | // use the appropriate nonabstract type. Use uint64 for | |
7016 | // an integer if we know it is nonnegative, otherwise | |
7017 | // use int64 for a integer, otherwise use float64 for a | |
7018 | // float or complex128 for a complex. | |
7019 | Type* want_type = NULL; | |
7020 | Type* atype = (*pa)->type(); | |
7021 | if (atype->is_abstract()) | |
7022 | { | |
7023 | if (atype->integer_type() != NULL) | |
7024 | { | |
7025 | mpz_t val; | |
7026 | mpz_init(val); | |
7027 | Type* dummy; | |
7028 | if (this->integer_constant_value(true, val, &dummy) | |
7029 | && mpz_sgn(val) >= 0) | |
7030 | want_type = Type::lookup_integer_type("uint64"); | |
7031 | else | |
7032 | want_type = Type::lookup_integer_type("int64"); | |
7033 | mpz_clear(val); | |
7034 | } | |
7035 | else if (atype->float_type() != NULL) | |
7036 | want_type = Type::lookup_float_type("float64"); | |
7037 | else if (atype->complex_type() != NULL) | |
7038 | want_type = Type::lookup_complex_type("complex128"); | |
7039 | else if (atype->is_abstract_string_type()) | |
7040 | want_type = Type::lookup_string_type(); | |
7041 | else if (atype->is_abstract_boolean_type()) | |
7042 | want_type = Type::lookup_bool_type(); | |
7043 | else | |
7044 | gcc_unreachable(); | |
7045 | subcontext.type = want_type; | |
7046 | } | |
7047 | } | |
7048 | ||
7049 | (*pa)->determine_type(&subcontext); | |
7050 | } | |
7051 | } | |
7052 | } | |
7053 | ||
7054 | // If there is exactly one argument, return true. Otherwise give an | |
7055 | // error message and return false. | |
7056 | ||
7057 | bool | |
7058 | Builtin_call_expression::check_one_arg() | |
7059 | { | |
7060 | const Expression_list* args = this->args(); | |
7061 | if (args == NULL || args->size() < 1) | |
7062 | { | |
7063 | this->report_error(_("not enough arguments")); | |
7064 | return false; | |
7065 | } | |
7066 | else if (args->size() > 1) | |
7067 | { | |
7068 | this->report_error(_("too many arguments")); | |
7069 | return false; | |
7070 | } | |
7071 | if (args->front()->is_error_expression() | |
7072 | || args->front()->type()->is_error_type()) | |
7073 | { | |
7074 | this->set_is_error(); | |
7075 | return false; | |
7076 | } | |
7077 | return true; | |
7078 | } | |
7079 | ||
7080 | // Check argument types for a builtin function. | |
7081 | ||
7082 | void | |
7083 | Builtin_call_expression::do_check_types(Gogo*) | |
7084 | { | |
7085 | switch (this->code_) | |
7086 | { | |
7087 | case BUILTIN_INVALID: | |
7088 | case BUILTIN_NEW: | |
7089 | case BUILTIN_MAKE: | |
7090 | return; | |
7091 | ||
7092 | case BUILTIN_LEN: | |
7093 | case BUILTIN_CAP: | |
7094 | { | |
7095 | // The single argument may be either a string or an array or a | |
7096 | // map or a channel, or a pointer to a closed array. | |
7097 | if (this->check_one_arg()) | |
7098 | { | |
7099 | Type* arg_type = this->one_arg()->type(); | |
7100 | if (arg_type->points_to() != NULL | |
7101 | && arg_type->points_to()->array_type() != NULL | |
7102 | && !arg_type->points_to()->is_open_array_type()) | |
7103 | arg_type = arg_type->points_to(); | |
7104 | if (this->code_ == BUILTIN_CAP) | |
7105 | { | |
7106 | if (!arg_type->is_error_type() | |
7107 | && arg_type->array_type() == NULL | |
7108 | && arg_type->channel_type() == NULL) | |
7109 | this->report_error(_("argument must be array or slice " | |
7110 | "or channel")); | |
7111 | } | |
7112 | else | |
7113 | { | |
7114 | if (!arg_type->is_error_type() | |
7115 | && !arg_type->is_string_type() | |
7116 | && arg_type->array_type() == NULL | |
7117 | && arg_type->map_type() == NULL | |
7118 | && arg_type->channel_type() == NULL) | |
7119 | this->report_error(_("argument must be string or " | |
7120 | "array or slice or map or channel")); | |
7121 | } | |
7122 | } | |
7123 | } | |
7124 | break; | |
7125 | ||
7126 | case BUILTIN_PRINT: | |
7127 | case BUILTIN_PRINTLN: | |
7128 | { | |
7129 | const Expression_list* args = this->args(); | |
7130 | if (args == NULL) | |
7131 | { | |
7132 | if (this->code_ == BUILTIN_PRINT) | |
7133 | warning_at(this->location(), 0, | |
7134 | "no arguments for builtin function %<%s%>", | |
7135 | (this->code_ == BUILTIN_PRINT | |
7136 | ? "print" | |
7137 | : "println")); | |
7138 | } | |
7139 | else | |
7140 | { | |
7141 | for (Expression_list::const_iterator p = args->begin(); | |
7142 | p != args->end(); | |
7143 | ++p) | |
7144 | { | |
7145 | Type* type = (*p)->type(); | |
7146 | if (type->is_error_type() | |
7147 | || type->is_string_type() | |
7148 | || type->integer_type() != NULL | |
7149 | || type->float_type() != NULL | |
7150 | || type->complex_type() != NULL | |
7151 | || type->is_boolean_type() | |
7152 | || type->points_to() != NULL | |
7153 | || type->interface_type() != NULL | |
7154 | || type->channel_type() != NULL | |
7155 | || type->map_type() != NULL | |
7156 | || type->function_type() != NULL | |
7157 | || type->is_open_array_type()) | |
7158 | ; | |
7159 | else | |
7160 | this->report_error(_("unsupported argument type to " | |
7161 | "builtin function")); | |
7162 | } | |
7163 | } | |
7164 | } | |
7165 | break; | |
7166 | ||
7167 | case BUILTIN_CLOSE: | |
7168 | case BUILTIN_CLOSED: | |
7169 | if (this->check_one_arg()) | |
7170 | { | |
7171 | if (this->one_arg()->type()->channel_type() == NULL) | |
7172 | this->report_error(_("argument must be channel")); | |
7173 | } | |
7174 | break; | |
7175 | ||
7176 | case BUILTIN_PANIC: | |
7177 | case BUILTIN_SIZEOF: | |
7178 | case BUILTIN_ALIGNOF: | |
7179 | this->check_one_arg(); | |
7180 | break; | |
7181 | ||
7182 | case BUILTIN_RECOVER: | |
7183 | if (this->args() != NULL && !this->args()->empty()) | |
7184 | this->report_error(_("too many arguments")); | |
7185 | break; | |
7186 | ||
7187 | case BUILTIN_OFFSETOF: | |
7188 | if (this->check_one_arg()) | |
7189 | { | |
7190 | Expression* arg = this->one_arg(); | |
7191 | if (arg->field_reference_expression() == NULL) | |
7192 | this->report_error(_("argument must be a field reference")); | |
7193 | } | |
7194 | break; | |
7195 | ||
7196 | case BUILTIN_COPY: | |
7197 | { | |
7198 | const Expression_list* args = this->args(); | |
7199 | if (args == NULL || args->size() < 2) | |
7200 | { | |
7201 | this->report_error(_("not enough arguments")); | |
7202 | break; | |
7203 | } | |
7204 | else if (args->size() > 2) | |
7205 | { | |
7206 | this->report_error(_("too many arguments")); | |
7207 | break; | |
7208 | } | |
7209 | Type* arg1_type = args->front()->type(); | |
7210 | Type* arg2_type = args->back()->type(); | |
7211 | if (arg1_type->is_error_type() || arg2_type->is_error_type()) | |
7212 | break; | |
7213 | ||
7214 | Type* e1; | |
7215 | if (arg1_type->is_open_array_type()) | |
7216 | e1 = arg1_type->array_type()->element_type(); | |
7217 | else | |
7218 | { | |
7219 | this->report_error(_("left argument must be a slice")); | |
7220 | break; | |
7221 | } | |
7222 | ||
7223 | Type* e2; | |
7224 | if (arg2_type->is_open_array_type()) | |
7225 | e2 = arg2_type->array_type()->element_type(); | |
7226 | else if (arg2_type->is_string_type()) | |
7227 | e2 = Type::lookup_integer_type("uint8"); | |
7228 | else | |
7229 | { | |
7230 | this->report_error(_("right argument must be a slice or a string")); | |
7231 | break; | |
7232 | } | |
7233 | ||
07ba8be5 | 7234 | if (!Type::are_identical(e1, e2, true, NULL)) |
e440a328 | 7235 | this->report_error(_("element types must be the same")); |
7236 | } | |
7237 | break; | |
7238 | ||
7239 | case BUILTIN_APPEND: | |
7240 | { | |
7241 | const Expression_list* args = this->args(); | |
7242 | if (args == NULL || args->empty()) | |
7243 | { | |
7244 | this->report_error(_("not enough arguments")); | |
7245 | break; | |
7246 | } | |
7247 | /* Lowering varargs should have left us with 2 arguments. */ | |
7248 | gcc_assert(args->size() == 2); | |
7249 | std::string reason; | |
7250 | if (!Type::are_assignable(args->front()->type(), args->back()->type(), | |
7251 | &reason)) | |
7252 | { | |
7253 | if (reason.empty()) | |
7254 | this->report_error(_("arguments 1 and 2 have different types")); | |
7255 | else | |
7256 | { | |
7257 | error_at(this->location(), | |
7258 | "arguments 1 and 2 have different types (%s)", | |
7259 | reason.c_str()); | |
7260 | this->set_is_error(); | |
7261 | } | |
7262 | } | |
7263 | break; | |
7264 | } | |
7265 | ||
7266 | case BUILTIN_REAL: | |
7267 | case BUILTIN_IMAG: | |
7268 | if (this->check_one_arg()) | |
7269 | { | |
7270 | if (this->one_arg()->type()->complex_type() == NULL) | |
7271 | this->report_error(_("argument must have complex type")); | |
7272 | } | |
7273 | break; | |
7274 | ||
7275 | case BUILTIN_CMPLX: | |
7276 | { | |
7277 | const Expression_list* args = this->args(); | |
7278 | if (args == NULL || args->size() < 2) | |
7279 | this->report_error(_("not enough arguments")); | |
7280 | else if (args->size() > 2) | |
7281 | this->report_error(_("too many arguments")); | |
7282 | else if (args->front()->is_error_expression() | |
7283 | || args->front()->type()->is_error_type() | |
7284 | || args->back()->is_error_expression() | |
7285 | || args->back()->type()->is_error_type()) | |
7286 | this->set_is_error(); | |
7287 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 7288 | args->back()->type(), true, NULL)) |
e440a328 | 7289 | this->report_error(_("cmplx arguments must have identical types")); |
7290 | else if (args->front()->type()->float_type() == NULL) | |
7291 | this->report_error(_("cmplx arguments must have " | |
7292 | "floating-point type")); | |
7293 | } | |
7294 | break; | |
7295 | ||
7296 | default: | |
7297 | gcc_unreachable(); | |
7298 | } | |
7299 | } | |
7300 | ||
7301 | // Return the tree for a builtin function. | |
7302 | ||
7303 | tree | |
7304 | Builtin_call_expression::do_get_tree(Translate_context* context) | |
7305 | { | |
7306 | Gogo* gogo = context->gogo(); | |
7307 | source_location location = this->location(); | |
7308 | switch (this->code_) | |
7309 | { | |
7310 | case BUILTIN_INVALID: | |
7311 | case BUILTIN_NEW: | |
7312 | case BUILTIN_MAKE: | |
7313 | gcc_unreachable(); | |
7314 | ||
7315 | case BUILTIN_LEN: | |
7316 | case BUILTIN_CAP: | |
7317 | { | |
7318 | const Expression_list* args = this->args(); | |
7319 | gcc_assert(args != NULL && args->size() == 1); | |
7320 | Expression* arg = *args->begin(); | |
7321 | Type* arg_type = arg->type(); | |
7322 | tree arg_tree = arg->get_tree(context); | |
7323 | if (arg_tree == error_mark_node) | |
7324 | return error_mark_node; | |
7325 | ||
7326 | if (arg_type->points_to() != NULL) | |
7327 | { | |
7328 | arg_type = arg_type->points_to(); | |
7329 | gcc_assert(arg_type->array_type() != NULL | |
7330 | && !arg_type->is_open_array_type()); | |
7331 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree))); | |
7332 | arg_tree = build_fold_indirect_ref(arg_tree); | |
7333 | } | |
7334 | ||
7335 | tree val_tree; | |
7336 | if (this->code_ == BUILTIN_LEN) | |
7337 | { | |
7338 | if (arg_type->is_string_type()) | |
7339 | val_tree = String_type::length_tree(gogo, arg_tree); | |
7340 | else if (arg_type->array_type() != NULL) | |
7341 | val_tree = arg_type->array_type()->length_tree(gogo, arg_tree); | |
7342 | else if (arg_type->map_type() != NULL) | |
7343 | { | |
7344 | static tree map_len_fndecl; | |
7345 | val_tree = Gogo::call_builtin(&map_len_fndecl, | |
7346 | location, | |
7347 | "__go_map_len", | |
7348 | 1, | |
7349 | sizetype, | |
7350 | arg_type->get_tree(gogo), | |
7351 | arg_tree); | |
7352 | } | |
7353 | else if (arg_type->channel_type() != NULL) | |
7354 | { | |
7355 | static tree chan_len_fndecl; | |
7356 | val_tree = Gogo::call_builtin(&chan_len_fndecl, | |
7357 | location, | |
7358 | "__go_chan_len", | |
7359 | 1, | |
7360 | sizetype, | |
7361 | arg_type->get_tree(gogo), | |
7362 | arg_tree); | |
7363 | } | |
7364 | else | |
7365 | gcc_unreachable(); | |
7366 | } | |
7367 | else | |
7368 | { | |
7369 | if (arg_type->array_type() != NULL) | |
7370 | val_tree = arg_type->array_type()->capacity_tree(gogo, arg_tree); | |
7371 | else if (arg_type->channel_type() != NULL) | |
7372 | { | |
7373 | static tree chan_cap_fndecl; | |
7374 | val_tree = Gogo::call_builtin(&chan_cap_fndecl, | |
7375 | location, | |
7376 | "__go_chan_cap", | |
7377 | 1, | |
7378 | sizetype, | |
7379 | arg_type->get_tree(gogo), | |
7380 | arg_tree); | |
7381 | } | |
7382 | else | |
7383 | gcc_unreachable(); | |
7384 | } | |
7385 | ||
d8ccb1e3 | 7386 | if (val_tree == error_mark_node) |
7387 | return error_mark_node; | |
7388 | ||
e440a328 | 7389 | tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo); |
7390 | if (type_tree == TREE_TYPE(val_tree)) | |
7391 | return val_tree; | |
7392 | else | |
7393 | return fold(convert_to_integer(type_tree, val_tree)); | |
7394 | } | |
7395 | ||
7396 | case BUILTIN_PRINT: | |
7397 | case BUILTIN_PRINTLN: | |
7398 | { | |
7399 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
7400 | tree stmt_list = NULL_TREE; | |
7401 | ||
7402 | const Expression_list* call_args = this->args(); | |
7403 | if (call_args != NULL) | |
7404 | { | |
7405 | for (Expression_list::const_iterator p = call_args->begin(); | |
7406 | p != call_args->end(); | |
7407 | ++p) | |
7408 | { | |
7409 | if (is_ln && p != call_args->begin()) | |
7410 | { | |
7411 | static tree print_space_fndecl; | |
7412 | tree call = Gogo::call_builtin(&print_space_fndecl, | |
7413 | location, | |
7414 | "__go_print_space", | |
7415 | 0, | |
7416 | void_type_node); | |
7417 | append_to_statement_list(call, &stmt_list); | |
7418 | } | |
7419 | ||
7420 | Type* type = (*p)->type(); | |
7421 | ||
7422 | tree arg = (*p)->get_tree(context); | |
7423 | if (arg == error_mark_node) | |
7424 | return error_mark_node; | |
7425 | ||
7426 | tree* pfndecl; | |
7427 | const char* fnname; | |
7428 | if (type->is_string_type()) | |
7429 | { | |
7430 | static tree print_string_fndecl; | |
7431 | pfndecl = &print_string_fndecl; | |
7432 | fnname = "__go_print_string"; | |
7433 | } | |
7434 | else if (type->integer_type() != NULL | |
7435 | && type->integer_type()->is_unsigned()) | |
7436 | { | |
7437 | static tree print_uint64_fndecl; | |
7438 | pfndecl = &print_uint64_fndecl; | |
7439 | fnname = "__go_print_uint64"; | |
7440 | Type* itype = Type::lookup_integer_type("uint64"); | |
7441 | arg = fold_convert_loc(location, itype->get_tree(gogo), | |
7442 | arg); | |
7443 | } | |
7444 | else if (type->integer_type() != NULL) | |
7445 | { | |
7446 | static tree print_int64_fndecl; | |
7447 | pfndecl = &print_int64_fndecl; | |
7448 | fnname = "__go_print_int64"; | |
7449 | Type* itype = Type::lookup_integer_type("int64"); | |
7450 | arg = fold_convert_loc(location, itype->get_tree(gogo), | |
7451 | arg); | |
7452 | } | |
7453 | else if (type->float_type() != NULL) | |
7454 | { | |
7455 | static tree print_double_fndecl; | |
7456 | pfndecl = &print_double_fndecl; | |
7457 | fnname = "__go_print_double"; | |
7458 | arg = fold_convert_loc(location, double_type_node, arg); | |
7459 | } | |
7460 | else if (type->complex_type() != NULL) | |
7461 | { | |
7462 | static tree print_complex_fndecl; | |
7463 | pfndecl = &print_complex_fndecl; | |
7464 | fnname = "__go_print_complex"; | |
7465 | arg = fold_convert_loc(location, complex_double_type_node, | |
7466 | arg); | |
7467 | } | |
7468 | else if (type->is_boolean_type()) | |
7469 | { | |
7470 | static tree print_bool_fndecl; | |
7471 | pfndecl = &print_bool_fndecl; | |
7472 | fnname = "__go_print_bool"; | |
7473 | } | |
7474 | else if (type->points_to() != NULL | |
7475 | || type->channel_type() != NULL | |
7476 | || type->map_type() != NULL | |
7477 | || type->function_type() != NULL) | |
7478 | { | |
7479 | static tree print_pointer_fndecl; | |
7480 | pfndecl = &print_pointer_fndecl; | |
7481 | fnname = "__go_print_pointer"; | |
7482 | arg = fold_convert_loc(location, ptr_type_node, arg); | |
7483 | } | |
7484 | else if (type->interface_type() != NULL) | |
7485 | { | |
7486 | if (type->interface_type()->is_empty()) | |
7487 | { | |
7488 | static tree print_empty_interface_fndecl; | |
7489 | pfndecl = &print_empty_interface_fndecl; | |
7490 | fnname = "__go_print_empty_interface"; | |
7491 | } | |
7492 | else | |
7493 | { | |
7494 | static tree print_interface_fndecl; | |
7495 | pfndecl = &print_interface_fndecl; | |
7496 | fnname = "__go_print_interface"; | |
7497 | } | |
7498 | } | |
7499 | else if (type->is_open_array_type()) | |
7500 | { | |
7501 | static tree print_slice_fndecl; | |
7502 | pfndecl = &print_slice_fndecl; | |
7503 | fnname = "__go_print_slice"; | |
7504 | } | |
7505 | else | |
7506 | gcc_unreachable(); | |
7507 | ||
7508 | tree call = Gogo::call_builtin(pfndecl, | |
7509 | location, | |
7510 | fnname, | |
7511 | 1, | |
7512 | void_type_node, | |
7513 | TREE_TYPE(arg), | |
7514 | arg); | |
d8ccb1e3 | 7515 | if (call != error_mark_node) |
7516 | append_to_statement_list(call, &stmt_list); | |
e440a328 | 7517 | } |
7518 | } | |
7519 | ||
7520 | if (is_ln) | |
7521 | { | |
7522 | static tree print_nl_fndecl; | |
7523 | tree call = Gogo::call_builtin(&print_nl_fndecl, | |
7524 | location, | |
7525 | "__go_print_nl", | |
7526 | 0, | |
7527 | void_type_node); | |
7528 | append_to_statement_list(call, &stmt_list); | |
7529 | } | |
7530 | ||
7531 | return stmt_list; | |
7532 | } | |
7533 | ||
7534 | case BUILTIN_PANIC: | |
7535 | { | |
7536 | const Expression_list* args = this->args(); | |
7537 | gcc_assert(args != NULL && args->size() == 1); | |
7538 | Expression* arg = args->front(); | |
7539 | tree arg_tree = arg->get_tree(context); | |
7540 | if (arg_tree == error_mark_node) | |
7541 | return error_mark_node; | |
7542 | Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
7543 | arg_tree = Expression::convert_for_assignment(context, empty, | |
7544 | arg->type(), | |
7545 | arg_tree, location); | |
7546 | static tree panic_fndecl; | |
7547 | tree call = Gogo::call_builtin(&panic_fndecl, | |
7548 | location, | |
7549 | "__go_panic", | |
7550 | 1, | |
7551 | void_type_node, | |
7552 | TREE_TYPE(arg_tree), | |
7553 | arg_tree); | |
7554 | // This function will throw an exception. | |
7555 | TREE_NOTHROW(panic_fndecl) = 0; | |
7556 | // This function will not return. | |
7557 | TREE_THIS_VOLATILE(panic_fndecl) = 1; | |
7558 | return call; | |
7559 | } | |
7560 | ||
7561 | case BUILTIN_RECOVER: | |
7562 | { | |
7563 | // The argument is set when building recover thunks. It's a | |
7564 | // boolean value which is true if we can recover a value now. | |
7565 | const Expression_list* args = this->args(); | |
7566 | gcc_assert(args != NULL && args->size() == 1); | |
7567 | Expression* arg = args->front(); | |
7568 | tree arg_tree = arg->get_tree(context); | |
7569 | if (arg_tree == error_mark_node) | |
7570 | return error_mark_node; | |
7571 | ||
7572 | Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
7573 | tree empty_tree = empty->get_tree(context->gogo()); | |
7574 | ||
7575 | Type* nil_type = Type::make_nil_type(); | |
7576 | Expression* nil = Expression::make_nil(location); | |
7577 | tree nil_tree = nil->get_tree(context); | |
7578 | tree empty_nil_tree = Expression::convert_for_assignment(context, | |
7579 | empty, | |
7580 | nil_type, | |
7581 | nil_tree, | |
7582 | location); | |
7583 | ||
7584 | // We need to handle a deferred call to recover specially, | |
7585 | // because it changes whether it can recover a panic or not. | |
7586 | // See test7 in test/recover1.go. | |
7587 | tree call; | |
7588 | if (this->is_deferred()) | |
7589 | { | |
7590 | static tree deferred_recover_fndecl; | |
7591 | call = Gogo::call_builtin(&deferred_recover_fndecl, | |
7592 | location, | |
7593 | "__go_deferred_recover", | |
7594 | 0, | |
7595 | empty_tree); | |
7596 | } | |
7597 | else | |
7598 | { | |
7599 | static tree recover_fndecl; | |
7600 | call = Gogo::call_builtin(&recover_fndecl, | |
7601 | location, | |
7602 | "__go_recover", | |
7603 | 0, | |
7604 | empty_tree); | |
7605 | } | |
7606 | return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree, | |
7607 | call, empty_nil_tree); | |
7608 | } | |
7609 | ||
7610 | case BUILTIN_CLOSE: | |
7611 | case BUILTIN_CLOSED: | |
7612 | { | |
7613 | const Expression_list* args = this->args(); | |
7614 | gcc_assert(args != NULL && args->size() == 1); | |
7615 | Expression* arg = args->front(); | |
7616 | tree arg_tree = arg->get_tree(context); | |
7617 | if (arg_tree == error_mark_node) | |
7618 | return error_mark_node; | |
7619 | if (this->code_ == BUILTIN_CLOSE) | |
7620 | { | |
7621 | static tree close_fndecl; | |
7622 | return Gogo::call_builtin(&close_fndecl, | |
7623 | location, | |
7624 | "__go_builtin_close", | |
7625 | 1, | |
7626 | void_type_node, | |
7627 | TREE_TYPE(arg_tree), | |
7628 | arg_tree); | |
7629 | } | |
7630 | else | |
7631 | { | |
7632 | static tree closed_fndecl; | |
7633 | return Gogo::call_builtin(&closed_fndecl, | |
7634 | location, | |
7635 | "__go_builtin_closed", | |
7636 | 1, | |
7637 | boolean_type_node, | |
7638 | TREE_TYPE(arg_tree), | |
7639 | arg_tree); | |
7640 | } | |
7641 | } | |
7642 | ||
7643 | case BUILTIN_SIZEOF: | |
7644 | case BUILTIN_OFFSETOF: | |
7645 | case BUILTIN_ALIGNOF: | |
7646 | { | |
7647 | mpz_t val; | |
7648 | mpz_init(val); | |
7649 | Type* dummy; | |
7650 | bool b = this->integer_constant_value(true, val, &dummy); | |
7651 | gcc_assert(b); | |
7652 | tree type = Type::lookup_integer_type("int")->get_tree(gogo); | |
7653 | tree ret = Expression::integer_constant_tree(val, type); | |
7654 | mpz_clear(val); | |
7655 | return ret; | |
7656 | } | |
7657 | ||
7658 | case BUILTIN_COPY: | |
7659 | { | |
7660 | const Expression_list* args = this->args(); | |
7661 | gcc_assert(args != NULL && args->size() == 2); | |
7662 | Expression* arg1 = args->front(); | |
7663 | Expression* arg2 = args->back(); | |
7664 | ||
7665 | tree arg1_tree = arg1->get_tree(context); | |
7666 | tree arg2_tree = arg2->get_tree(context); | |
7667 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
7668 | return error_mark_node; | |
7669 | ||
7670 | Type* arg1_type = arg1->type(); | |
7671 | Array_type* at = arg1_type->array_type(); | |
7672 | arg1_tree = save_expr(arg1_tree); | |
7673 | tree arg1_val = at->value_pointer_tree(gogo, arg1_tree); | |
7674 | tree arg1_len = at->length_tree(gogo, arg1_tree); | |
d8ccb1e3 | 7675 | if (arg1_val == error_mark_node || arg1_len == error_mark_node) |
7676 | return error_mark_node; | |
e440a328 | 7677 | |
7678 | Type* arg2_type = arg2->type(); | |
7679 | tree arg2_val; | |
7680 | tree arg2_len; | |
7681 | if (arg2_type->is_open_array_type()) | |
7682 | { | |
7683 | at = arg2_type->array_type(); | |
7684 | arg2_tree = save_expr(arg2_tree); | |
7685 | arg2_val = at->value_pointer_tree(gogo, arg2_tree); | |
7686 | arg2_len = at->length_tree(gogo, arg2_tree); | |
7687 | } | |
7688 | else | |
7689 | { | |
7690 | arg2_tree = save_expr(arg2_tree); | |
7691 | arg2_val = String_type::bytes_tree(gogo, arg2_tree); | |
7692 | arg2_len = String_type::length_tree(gogo, arg2_tree); | |
7693 | } | |
d8ccb1e3 | 7694 | if (arg2_val == error_mark_node || arg2_len == error_mark_node) |
7695 | return error_mark_node; | |
e440a328 | 7696 | |
7697 | arg1_len = save_expr(arg1_len); | |
7698 | arg2_len = save_expr(arg2_len); | |
7699 | tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len), | |
7700 | fold_build2_loc(location, LT_EXPR, | |
7701 | boolean_type_node, | |
7702 | arg1_len, arg2_len), | |
7703 | arg1_len, arg2_len); | |
7704 | len = save_expr(len); | |
7705 | ||
7706 | Type* element_type = at->element_type(); | |
7707 | tree element_type_tree = element_type->get_tree(gogo); | |
d8ccb1e3 | 7708 | if (element_type_tree == error_mark_node) |
7709 | return error_mark_node; | |
e440a328 | 7710 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
7711 | tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size), | |
7712 | len); | |
7713 | bytecount = fold_build2_loc(location, MULT_EXPR, | |
7714 | TREE_TYPE(element_size), | |
7715 | bytecount, element_size); | |
7716 | bytecount = fold_convert_loc(location, size_type_node, bytecount); | |
7717 | ||
7718 | tree call = build_call_expr_loc(location, | |
7719 | built_in_decls[BUILT_IN_MEMMOVE], | |
7720 | 3, arg1_val, arg2_val, bytecount); | |
7721 | ||
7722 | return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len), | |
7723 | call, len); | |
7724 | } | |
7725 | ||
7726 | case BUILTIN_APPEND: | |
7727 | { | |
7728 | const Expression_list* args = this->args(); | |
7729 | gcc_assert(args != NULL && args->size() == 2); | |
7730 | Expression* arg1 = args->front(); | |
7731 | Expression* arg2 = args->back(); | |
7732 | ||
7733 | tree arg1_tree = arg1->get_tree(context); | |
7734 | tree arg2_tree = arg2->get_tree(context); | |
7735 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
7736 | return error_mark_node; | |
7737 | ||
7738 | tree descriptor_tree = arg1->type()->type_descriptor_pointer(gogo); | |
7739 | ||
7740 | // We rebuild the decl each time since the slice types may | |
7741 | // change. | |
7742 | tree append_fndecl = NULL_TREE; | |
7743 | return Gogo::call_builtin(&append_fndecl, | |
7744 | location, | |
7745 | "__go_append", | |
7746 | 3, | |
7747 | TREE_TYPE(arg1_tree), | |
7748 | TREE_TYPE(descriptor_tree), | |
7749 | descriptor_tree, | |
7750 | TREE_TYPE(arg1_tree), | |
7751 | arg1_tree, | |
7752 | TREE_TYPE(arg2_tree), | |
7753 | arg2_tree); | |
7754 | } | |
7755 | ||
7756 | case BUILTIN_REAL: | |
7757 | case BUILTIN_IMAG: | |
7758 | { | |
7759 | const Expression_list* args = this->args(); | |
7760 | gcc_assert(args != NULL && args->size() == 1); | |
7761 | Expression* arg = args->front(); | |
7762 | tree arg_tree = arg->get_tree(context); | |
7763 | if (arg_tree == error_mark_node) | |
7764 | return error_mark_node; | |
7765 | gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree))); | |
7766 | if (this->code_ == BUILTIN_REAL) | |
7767 | return fold_build1_loc(location, REALPART_EXPR, | |
7768 | TREE_TYPE(TREE_TYPE(arg_tree)), | |
7769 | arg_tree); | |
7770 | else | |
7771 | return fold_build1_loc(location, IMAGPART_EXPR, | |
7772 | TREE_TYPE(TREE_TYPE(arg_tree)), | |
7773 | arg_tree); | |
7774 | } | |
7775 | ||
7776 | case BUILTIN_CMPLX: | |
7777 | { | |
7778 | const Expression_list* args = this->args(); | |
7779 | gcc_assert(args != NULL && args->size() == 2); | |
7780 | tree r = args->front()->get_tree(context); | |
7781 | tree i = args->back()->get_tree(context); | |
7782 | if (r == error_mark_node || i == error_mark_node) | |
7783 | return error_mark_node; | |
7784 | gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r)) | |
7785 | == TYPE_MAIN_VARIANT(TREE_TYPE(i))); | |
7786 | gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r))); | |
7787 | return fold_build2_loc(location, COMPLEX_EXPR, | |
7788 | build_complex_type(TREE_TYPE(r)), | |
7789 | r, i); | |
7790 | } | |
7791 | ||
7792 | default: | |
7793 | gcc_unreachable(); | |
7794 | } | |
7795 | } | |
7796 | ||
7797 | // We have to support exporting a builtin call expression, because | |
7798 | // code can set a constant to the result of a builtin expression. | |
7799 | ||
7800 | void | |
7801 | Builtin_call_expression::do_export(Export* exp) const | |
7802 | { | |
7803 | bool ok = false; | |
7804 | ||
7805 | mpz_t val; | |
7806 | mpz_init(val); | |
7807 | Type* dummy; | |
7808 | if (this->integer_constant_value(true, val, &dummy)) | |
7809 | { | |
7810 | Integer_expression::export_integer(exp, val); | |
7811 | ok = true; | |
7812 | } | |
7813 | mpz_clear(val); | |
7814 | ||
7815 | if (!ok) | |
7816 | { | |
7817 | mpfr_t fval; | |
7818 | mpfr_init(fval); | |
7819 | if (this->float_constant_value(fval, &dummy)) | |
7820 | { | |
7821 | Float_expression::export_float(exp, fval); | |
7822 | ok = true; | |
7823 | } | |
7824 | mpfr_clear(fval); | |
7825 | } | |
7826 | ||
7827 | if (!ok) | |
7828 | { | |
7829 | mpfr_t real; | |
7830 | mpfr_t imag; | |
7831 | mpfr_init(real); | |
7832 | mpfr_init(imag); | |
7833 | if (this->complex_constant_value(real, imag, &dummy)) | |
7834 | { | |
7835 | Complex_expression::export_complex(exp, real, imag); | |
7836 | ok = true; | |
7837 | } | |
7838 | mpfr_clear(real); | |
7839 | mpfr_clear(imag); | |
7840 | } | |
7841 | ||
7842 | if (!ok) | |
7843 | { | |
7844 | error_at(this->location(), "value is not constant"); | |
7845 | return; | |
7846 | } | |
7847 | ||
7848 | // A trailing space lets us reliably identify the end of the number. | |
7849 | exp->write_c_string(" "); | |
7850 | } | |
7851 | ||
7852 | // Class Call_expression. | |
7853 | ||
7854 | // Traversal. | |
7855 | ||
7856 | int | |
7857 | Call_expression::do_traverse(Traverse* traverse) | |
7858 | { | |
7859 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) | |
7860 | return TRAVERSE_EXIT; | |
7861 | if (this->args_ != NULL) | |
7862 | { | |
7863 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
7864 | return TRAVERSE_EXIT; | |
7865 | } | |
7866 | return TRAVERSE_CONTINUE; | |
7867 | } | |
7868 | ||
7869 | // Lower a call statement. | |
7870 | ||
7871 | Expression* | |
7872 | Call_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
7873 | { | |
7874 | // A type case can look like a function call. | |
7875 | if (this->fn_->is_type_expression() | |
7876 | && this->args_ != NULL | |
7877 | && this->args_->size() == 1) | |
7878 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
7879 | this->location()); | |
7880 | ||
7881 | // Recognize a call to a builtin function. | |
7882 | Func_expression* fne = this->fn_->func_expression(); | |
7883 | if (fne != NULL | |
7884 | && fne->named_object()->is_function_declaration() | |
7885 | && fne->named_object()->func_declaration_value()->type()->is_builtin()) | |
7886 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
7887 | this->is_varargs_, this->location()); | |
7888 | ||
7889 | // Handle an argument which is a call to a function which returns | |
7890 | // multiple results. | |
7891 | if (this->args_ != NULL | |
7892 | && this->args_->size() == 1 | |
7893 | && this->args_->front()->call_expression() != NULL | |
7894 | && this->fn_->type()->function_type() != NULL) | |
7895 | { | |
7896 | Function_type* fntype = this->fn_->type()->function_type(); | |
7897 | size_t rc = this->args_->front()->call_expression()->result_count(); | |
7898 | if (rc > 1 | |
7899 | && fntype->parameters() != NULL | |
7900 | && (fntype->parameters()->size() == rc | |
7901 | || (fntype->is_varargs() | |
7902 | && fntype->parameters()->size() - 1 <= rc))) | |
7903 | { | |
7904 | Call_expression* call = this->args_->front()->call_expression(); | |
7905 | Expression_list* args = new Expression_list; | |
7906 | for (size_t i = 0; i < rc; ++i) | |
7907 | args->push_back(Expression::make_call_result(call, i)); | |
7908 | // We can't return a new call expression here, because this | |
7909 | // one may be referenced by Call_result expressions. FIXME. | |
7910 | delete this->args_; | |
7911 | this->args_ = args; | |
7912 | } | |
7913 | } | |
7914 | ||
7915 | // Handle a call to a varargs function by packaging up the extra | |
7916 | // parameters. | |
7917 | if (this->fn_->type()->function_type() != NULL | |
7918 | && this->fn_->type()->function_type()->is_varargs()) | |
7919 | { | |
7920 | Function_type* fntype = this->fn_->type()->function_type(); | |
7921 | const Typed_identifier_list* parameters = fntype->parameters(); | |
7922 | gcc_assert(parameters != NULL && !parameters->empty()); | |
7923 | Type* varargs_type = parameters->back().type(); | |
7924 | return this->lower_varargs(gogo, function, varargs_type, | |
7925 | parameters->size()); | |
7926 | } | |
7927 | ||
7928 | return this; | |
7929 | } | |
7930 | ||
7931 | // Lower a call to a varargs function. FUNCTION is the function in | |
7932 | // which the call occurs--it's not the function we are calling. | |
7933 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
7934 | // PARAM_COUNT is the number of parameters of the function we are | |
7935 | // calling; the last of these parameters will be the varargs | |
7936 | // parameter. | |
7937 | ||
7938 | Expression* | |
7939 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, | |
7940 | Type* varargs_type, size_t param_count) | |
7941 | { | |
7942 | if (this->varargs_are_lowered_) | |
7943 | return this; | |
7944 | ||
7945 | source_location loc = this->location(); | |
7946 | ||
7947 | gcc_assert(param_count > 0); | |
7948 | gcc_assert(varargs_type->is_open_array_type()); | |
7949 | ||
7950 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
7951 | if (arg_count < param_count - 1) | |
7952 | { | |
7953 | // Not enough arguments; will be caught in check_types. | |
7954 | return this; | |
7955 | } | |
7956 | ||
7957 | Expression_list* old_args = this->args_; | |
7958 | Expression_list* new_args = new Expression_list(); | |
7959 | bool push_empty_arg = false; | |
7960 | if (old_args == NULL || old_args->empty()) | |
7961 | { | |
7962 | gcc_assert(param_count == 1); | |
7963 | push_empty_arg = true; | |
7964 | } | |
7965 | else | |
7966 | { | |
7967 | Expression_list::const_iterator pa; | |
7968 | int i = 1; | |
7969 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
7970 | { | |
7971 | if (static_cast<size_t>(i) == param_count) | |
7972 | break; | |
7973 | new_args->push_back(*pa); | |
7974 | } | |
7975 | ||
7976 | // We have reached the varargs parameter. | |
7977 | ||
7978 | bool issued_error = false; | |
7979 | if (pa == old_args->end()) | |
7980 | push_empty_arg = true; | |
7981 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
7982 | new_args->push_back(*pa); | |
7983 | else if (this->is_varargs_) | |
7984 | { | |
7985 | this->report_error(_("too many arguments")); | |
7986 | return this; | |
7987 | } | |
7988 | else if (pa + 1 == old_args->end() | |
7989 | && this->is_compatible_varargs_argument(function, *pa, | |
7990 | varargs_type, | |
7991 | &issued_error)) | |
7992 | new_args->push_back(*pa); | |
7993 | else | |
7994 | { | |
7995 | Type* element_type = varargs_type->array_type()->element_type(); | |
7996 | Expression_list* vals = new Expression_list; | |
7997 | for (; pa != old_args->end(); ++pa, ++i) | |
7998 | { | |
7999 | // Check types here so that we get a better message. | |
8000 | Type* patype = (*pa)->type(); | |
8001 | source_location paloc = (*pa)->location(); | |
8002 | if (!this->check_argument_type(i, element_type, patype, | |
8003 | paloc, issued_error)) | |
8004 | continue; | |
8005 | vals->push_back(*pa); | |
8006 | } | |
8007 | Expression* val = | |
8008 | Expression::make_slice_composite_literal(varargs_type, vals, loc); | |
8009 | new_args->push_back(val); | |
8010 | } | |
8011 | } | |
8012 | ||
8013 | if (push_empty_arg) | |
8014 | new_args->push_back(Expression::make_nil(loc)); | |
8015 | ||
8016 | // We can't return a new call expression here, because this one may | |
8017 | // be referenced by Call_result expressions. FIXME. | |
8018 | if (old_args != NULL) | |
8019 | delete old_args; | |
8020 | this->args_ = new_args; | |
8021 | this->varargs_are_lowered_ = true; | |
8022 | ||
8023 | // Lower all the new subexpressions. | |
8024 | Expression* ret = this; | |
8025 | gogo->lower_expression(function, &ret); | |
8026 | gcc_assert(ret == this); | |
8027 | return ret; | |
8028 | } | |
8029 | ||
8030 | // Return true if ARG is a varargs argment which should be passed to | |
8031 | // the varargs parameter of type PARAM_TYPE without wrapping. ARG | |
8032 | // will be the last argument passed in the call, and PARAM_TYPE will | |
8033 | // be the type of the last parameter of the varargs function being | |
8034 | // called. | |
8035 | ||
8036 | bool | |
8037 | Call_expression::is_compatible_varargs_argument(Named_object* function, | |
8038 | Expression* arg, | |
8039 | Type* param_type, | |
8040 | bool* issued_error) | |
8041 | { | |
8042 | *issued_error = false; | |
8043 | ||
8044 | Type* var_type = NULL; | |
8045 | ||
8046 | // The simple case is passing the varargs parameter of the caller. | |
8047 | Var_expression* ve = arg->var_expression(); | |
8048 | if (ve != NULL && ve->named_object()->is_variable()) | |
8049 | { | |
8050 | Variable* var = ve->named_object()->var_value(); | |
8051 | if (var->is_varargs_parameter()) | |
8052 | var_type = var->type(); | |
8053 | } | |
8054 | ||
8055 | // The complex case is passing the varargs parameter of some | |
8056 | // enclosing function. This will look like passing down *c.f where | |
8057 | // c is the closure variable and f is a field in the closure. | |
8058 | if (function != NULL | |
8059 | && function->func_value()->needs_closure() | |
8060 | && arg->classification() == EXPRESSION_UNARY) | |
8061 | { | |
8062 | Unary_expression* ue = static_cast<Unary_expression*>(arg); | |
8063 | if (ue->op() == OPERATOR_MULT) | |
8064 | { | |
8065 | Field_reference_expression* fre = | |
8066 | ue->operand()->deref()->field_reference_expression(); | |
8067 | if (fre != NULL) | |
8068 | { | |
8069 | Var_expression* ve = fre->expr()->deref()->var_expression(); | |
8070 | if (ve != NULL) | |
8071 | { | |
8072 | Named_object* no = ve->named_object(); | |
8073 | Function* f = function->func_value(); | |
8074 | if (no == f->closure_var()) | |
8075 | { | |
8076 | // At this point we know that this indeed a | |
8077 | // reference to some enclosing variable. Now we | |
8078 | // need to figure out whether that variable is a | |
8079 | // varargs parameter. | |
8080 | Named_object* enclosing = | |
8081 | f->enclosing_var(fre->field_index()); | |
8082 | Variable* var = enclosing->var_value(); | |
8083 | if (var->is_varargs_parameter()) | |
8084 | var_type = var->type(); | |
8085 | } | |
8086 | } | |
8087 | } | |
8088 | } | |
8089 | } | |
8090 | ||
8091 | if (var_type == NULL) | |
8092 | return false; | |
8093 | ||
8094 | // We only match if the parameter is the same, with an identical | |
8095 | // type. | |
8096 | Array_type* var_at = var_type->array_type(); | |
8097 | gcc_assert(var_at != NULL); | |
8098 | Array_type* param_at = param_type->array_type(); | |
8099 | if (param_at != NULL | |
8100 | && Type::are_identical(var_at->element_type(), | |
07ba8be5 | 8101 | param_at->element_type(), true, NULL)) |
e440a328 | 8102 | return true; |
8103 | error_at(arg->location(), "... mismatch: passing ...T as ..."); | |
8104 | *issued_error = true; | |
8105 | return false; | |
8106 | } | |
8107 | ||
8108 | // Get the function type. Returns NULL if we don't know the type. If | |
8109 | // this returns NULL, and if_ERROR is true, issues an error. | |
8110 | ||
8111 | Function_type* | |
8112 | Call_expression::get_function_type() const | |
8113 | { | |
8114 | return this->fn_->type()->function_type(); | |
8115 | } | |
8116 | ||
8117 | // Return the number of values which this call will return. | |
8118 | ||
8119 | size_t | |
8120 | Call_expression::result_count() const | |
8121 | { | |
8122 | const Function_type* fntype = this->get_function_type(); | |
8123 | if (fntype == NULL) | |
8124 | return 0; | |
8125 | if (fntype->results() == NULL) | |
8126 | return 0; | |
8127 | return fntype->results()->size(); | |
8128 | } | |
8129 | ||
8130 | // Return whether this is a call to the predeclared function recover. | |
8131 | ||
8132 | bool | |
8133 | Call_expression::is_recover_call() const | |
8134 | { | |
8135 | return this->do_is_recover_call(); | |
8136 | } | |
8137 | ||
8138 | // Set the argument to the recover function. | |
8139 | ||
8140 | void | |
8141 | Call_expression::set_recover_arg(Expression* arg) | |
8142 | { | |
8143 | this->do_set_recover_arg(arg); | |
8144 | } | |
8145 | ||
8146 | // Virtual functions also implemented by Builtin_call_expression. | |
8147 | ||
8148 | bool | |
8149 | Call_expression::do_is_recover_call() const | |
8150 | { | |
8151 | return false; | |
8152 | } | |
8153 | ||
8154 | void | |
8155 | Call_expression::do_set_recover_arg(Expression*) | |
8156 | { | |
8157 | gcc_unreachable(); | |
8158 | } | |
8159 | ||
8160 | // Get the type. | |
8161 | ||
8162 | Type* | |
8163 | Call_expression::do_type() | |
8164 | { | |
8165 | if (this->type_ != NULL) | |
8166 | return this->type_; | |
8167 | ||
8168 | Type* ret; | |
8169 | Function_type* fntype = this->get_function_type(); | |
8170 | if (fntype == NULL) | |
8171 | return Type::make_error_type(); | |
8172 | ||
8173 | const Typed_identifier_list* results = fntype->results(); | |
8174 | if (results == NULL) | |
8175 | ret = Type::make_void_type(); | |
8176 | else if (results->size() == 1) | |
8177 | ret = results->begin()->type(); | |
8178 | else | |
8179 | ret = Type::make_call_multiple_result_type(this); | |
8180 | ||
8181 | this->type_ = ret; | |
8182 | ||
8183 | return this->type_; | |
8184 | } | |
8185 | ||
8186 | // Determine types for a call expression. We can use the function | |
8187 | // parameter types to set the types of the arguments. | |
8188 | ||
8189 | void | |
8190 | Call_expression::do_determine_type(const Type_context*) | |
8191 | { | |
8192 | this->fn_->determine_type_no_context(); | |
8193 | Function_type* fntype = this->get_function_type(); | |
8194 | const Typed_identifier_list* parameters = NULL; | |
8195 | if (fntype != NULL) | |
8196 | parameters = fntype->parameters(); | |
8197 | if (this->args_ != NULL) | |
8198 | { | |
8199 | Typed_identifier_list::const_iterator pt; | |
8200 | if (parameters != NULL) | |
8201 | pt = parameters->begin(); | |
8202 | for (Expression_list::const_iterator pa = this->args_->begin(); | |
8203 | pa != this->args_->end(); | |
8204 | ++pa) | |
8205 | { | |
8206 | if (parameters != NULL && pt != parameters->end()) | |
8207 | { | |
8208 | Type_context subcontext(pt->type(), false); | |
8209 | (*pa)->determine_type(&subcontext); | |
8210 | ++pt; | |
8211 | } | |
8212 | else | |
8213 | (*pa)->determine_type_no_context(); | |
8214 | } | |
8215 | } | |
8216 | } | |
8217 | ||
8218 | // Check types for parameter I. | |
8219 | ||
8220 | bool | |
8221 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
8222 | const Type* argument_type, | |
8223 | source_location argument_location, | |
8224 | bool issued_error) | |
8225 | { | |
8226 | std::string reason; | |
8227 | if (!Type::are_assignable(parameter_type, argument_type, &reason)) | |
8228 | { | |
8229 | if (!issued_error) | |
8230 | { | |
8231 | if (reason.empty()) | |
8232 | error_at(argument_location, "argument %d has incompatible type", i); | |
8233 | else | |
8234 | error_at(argument_location, | |
8235 | "argument %d has incompatible type (%s)", | |
8236 | i, reason.c_str()); | |
8237 | } | |
8238 | this->set_is_error(); | |
8239 | return false; | |
8240 | } | |
8241 | return true; | |
8242 | } | |
8243 | ||
8244 | // Check types. | |
8245 | ||
8246 | void | |
8247 | Call_expression::do_check_types(Gogo*) | |
8248 | { | |
8249 | Function_type* fntype = this->get_function_type(); | |
8250 | if (fntype == NULL) | |
8251 | { | |
8252 | if (!this->fn_->type()->is_error_type()) | |
8253 | this->report_error(_("expected function")); | |
8254 | return; | |
8255 | } | |
8256 | ||
8257 | if (fntype->is_method()) | |
8258 | { | |
8259 | // We don't support pointers to methods, so the function has to | |
8260 | // be a bound method expression. | |
8261 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
8262 | if (bme == NULL) | |
8263 | { | |
8264 | this->report_error(_("method call without object")); | |
8265 | return; | |
8266 | } | |
8267 | Type* first_arg_type = bme->first_argument()->type(); | |
8268 | if (first_arg_type->points_to() == NULL) | |
8269 | { | |
8270 | // When passing a value, we need to check that we are | |
8271 | // permitted to copy it. | |
8272 | std::string reason; | |
8273 | if (!Type::are_assignable(fntype->receiver()->type(), | |
8274 | first_arg_type, &reason)) | |
8275 | { | |
8276 | if (reason.empty()) | |
8277 | this->report_error(_("incompatible type for receiver")); | |
8278 | else | |
8279 | { | |
8280 | error_at(this->location(), | |
8281 | "incompatible type for receiver (%s)", | |
8282 | reason.c_str()); | |
8283 | this->set_is_error(); | |
8284 | } | |
8285 | } | |
8286 | } | |
8287 | } | |
8288 | ||
8289 | // Note that varargs was handled by the lower_varargs() method, so | |
8290 | // we don't have to worry about it here. | |
8291 | ||
8292 | const Typed_identifier_list* parameters = fntype->parameters(); | |
8293 | if (this->args_ == NULL) | |
8294 | { | |
8295 | if (parameters != NULL && !parameters->empty()) | |
8296 | this->report_error(_("not enough arguments")); | |
8297 | } | |
8298 | else if (parameters == NULL) | |
8299 | this->report_error(_("too many arguments")); | |
8300 | else | |
8301 | { | |
8302 | int i = 0; | |
8303 | Typed_identifier_list::const_iterator pt = parameters->begin(); | |
8304 | for (Expression_list::const_iterator pa = this->args_->begin(); | |
8305 | pa != this->args_->end(); | |
8306 | ++pa, ++pt, ++i) | |
8307 | { | |
8308 | if (pt == parameters->end()) | |
8309 | { | |
8310 | this->report_error(_("too many arguments")); | |
8311 | return; | |
8312 | } | |
8313 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
8314 | (*pa)->location(), false); | |
8315 | } | |
8316 | if (pt != parameters->end()) | |
8317 | this->report_error(_("not enough arguments")); | |
8318 | } | |
8319 | } | |
8320 | ||
8321 | // Return whether we have to use a temporary variable to ensure that | |
8322 | // we evaluate this call expression in order. If the call returns no | |
8323 | // results then it will inevitably be executed last. If the call | |
8324 | // returns more than one result then it will be used with Call_result | |
8325 | // expressions. So we only have to use a temporary variable if the | |
8326 | // call returns exactly one result. | |
8327 | ||
8328 | bool | |
8329 | Call_expression::do_must_eval_in_order() const | |
8330 | { | |
8331 | return this->result_count() == 1; | |
8332 | } | |
8333 | ||
8334 | // Get the function and the first argument to use when calling a bound | |
8335 | // method. | |
8336 | ||
8337 | tree | |
8338 | Call_expression::bound_method_function(Translate_context* context, | |
8339 | Bound_method_expression* bound_method, | |
8340 | tree* first_arg_ptr) | |
8341 | { | |
8342 | Expression* first_argument = bound_method->first_argument(); | |
8343 | tree first_arg = first_argument->get_tree(context); | |
8344 | if (first_arg == error_mark_node) | |
8345 | return error_mark_node; | |
8346 | ||
8347 | // We always pass a pointer to the first argument when calling a | |
8348 | // method. | |
8349 | if (first_argument->type()->points_to() == NULL) | |
8350 | { | |
8351 | tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg)); | |
8352 | if (TREE_ADDRESSABLE(TREE_TYPE(first_arg)) | |
8353 | || DECL_P(first_arg) | |
8354 | || TREE_CODE(first_arg) == INDIRECT_REF | |
8355 | || TREE_CODE(first_arg) == COMPONENT_REF) | |
8356 | { | |
8357 | first_arg = build_fold_addr_expr(first_arg); | |
8358 | if (DECL_P(first_arg)) | |
8359 | TREE_ADDRESSABLE(first_arg) = 1; | |
8360 | } | |
8361 | else | |
8362 | { | |
8363 | tree tmp = create_tmp_var(TREE_TYPE(first_arg), | |
8364 | get_name(first_arg)); | |
8365 | DECL_IGNORED_P(tmp) = 0; | |
8366 | DECL_INITIAL(tmp) = first_arg; | |
8367 | first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type, | |
8368 | build1(DECL_EXPR, void_type_node, tmp), | |
8369 | build_fold_addr_expr(tmp)); | |
8370 | TREE_ADDRESSABLE(tmp) = 1; | |
8371 | } | |
8372 | if (first_arg == error_mark_node) | |
8373 | return error_mark_node; | |
8374 | } | |
8375 | ||
8376 | Type* fatype = bound_method->first_argument_type(); | |
8377 | if (fatype != NULL) | |
8378 | { | |
8379 | if (fatype->points_to() == NULL) | |
8380 | fatype = Type::make_pointer_type(fatype); | |
8381 | first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg); | |
8382 | if (first_arg == error_mark_node | |
8383 | || TREE_TYPE(first_arg) == error_mark_node) | |
8384 | return error_mark_node; | |
8385 | } | |
8386 | ||
8387 | *first_arg_ptr = first_arg; | |
8388 | ||
8389 | return bound_method->method()->get_tree(context); | |
8390 | } | |
8391 | ||
8392 | // Get the function and the first argument to use when calling an | |
8393 | // interface method. | |
8394 | ||
8395 | tree | |
8396 | Call_expression::interface_method_function( | |
8397 | Translate_context* context, | |
8398 | Interface_field_reference_expression* interface_method, | |
8399 | tree* first_arg_ptr) | |
8400 | { | |
8401 | tree expr = interface_method->expr()->get_tree(context); | |
8402 | if (expr == error_mark_node) | |
8403 | return error_mark_node; | |
8404 | expr = save_expr(expr); | |
8405 | tree first_arg = interface_method->get_underlying_object_tree(context, expr); | |
8406 | if (first_arg == error_mark_node) | |
8407 | return error_mark_node; | |
8408 | *first_arg_ptr = first_arg; | |
8409 | return interface_method->get_function_tree(context, expr); | |
8410 | } | |
8411 | ||
8412 | // Build the call expression. | |
8413 | ||
8414 | tree | |
8415 | Call_expression::do_get_tree(Translate_context* context) | |
8416 | { | |
8417 | if (this->tree_ != NULL_TREE) | |
8418 | return this->tree_; | |
8419 | ||
8420 | Function_type* fntype = this->get_function_type(); | |
8421 | if (fntype == NULL) | |
8422 | return error_mark_node; | |
8423 | ||
8424 | if (this->fn_->is_error_expression()) | |
8425 | return error_mark_node; | |
8426 | ||
8427 | Gogo* gogo = context->gogo(); | |
8428 | source_location location = this->location(); | |
8429 | ||
8430 | Func_expression* func = this->fn_->func_expression(); | |
8431 | Bound_method_expression* bound_method = this->fn_->bound_method_expression(); | |
8432 | Interface_field_reference_expression* interface_method = | |
8433 | this->fn_->interface_field_reference_expression(); | |
8434 | const bool has_closure = func != NULL && func->closure() != NULL; | |
8435 | const bool is_method = bound_method != NULL || interface_method != NULL; | |
8436 | gcc_assert(!fntype->is_method() || is_method); | |
8437 | ||
8438 | int nargs; | |
8439 | tree* args; | |
8440 | if (this->args_ == NULL || this->args_->empty()) | |
8441 | { | |
8442 | nargs = is_method ? 1 : 0; | |
8443 | args = nargs == 0 ? NULL : new tree[nargs]; | |
8444 | } | |
8445 | else | |
8446 | { | |
8447 | const Typed_identifier_list* params = fntype->parameters(); | |
8448 | gcc_assert(params != NULL); | |
8449 | ||
8450 | nargs = this->args_->size(); | |
8451 | int i = is_method ? 1 : 0; | |
8452 | nargs += i; | |
8453 | args = new tree[nargs]; | |
8454 | ||
8455 | Typed_identifier_list::const_iterator pp = params->begin(); | |
8456 | Expression_list::const_iterator pe; | |
8457 | for (pe = this->args_->begin(); | |
8458 | pe != this->args_->end(); | |
8459 | ++pe, ++pp, ++i) | |
8460 | { | |
8461 | tree arg_val = (*pe)->get_tree(context); | |
8462 | args[i] = Expression::convert_for_assignment(context, | |
8463 | pp->type(), | |
8464 | (*pe)->type(), | |
8465 | arg_val, | |
8466 | location); | |
8467 | if (args[i] == error_mark_node) | |
8468 | return error_mark_node; | |
8469 | } | |
8470 | gcc_assert(pp == params->end()); | |
8471 | gcc_assert(i == nargs); | |
8472 | } | |
8473 | ||
8474 | tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo))); | |
8475 | if (rettype == error_mark_node) | |
8476 | return error_mark_node; | |
8477 | ||
8478 | tree fn; | |
8479 | if (has_closure) | |
8480 | fn = func->get_tree_without_closure(gogo); | |
8481 | else if (!is_method) | |
8482 | fn = this->fn_->get_tree(context); | |
8483 | else if (bound_method != NULL) | |
8484 | fn = this->bound_method_function(context, bound_method, &args[0]); | |
8485 | else if (interface_method != NULL) | |
8486 | fn = this->interface_method_function(context, interface_method, &args[0]); | |
8487 | else | |
8488 | gcc_unreachable(); | |
8489 | ||
8490 | if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node) | |
8491 | return error_mark_node; | |
8492 | ||
8493 | // This is to support builtin math functions when using 80387 math. | |
8494 | tree fndecl = fn; | |
8495 | if (TREE_CODE(fndecl) == ADDR_EXPR) | |
8496 | fndecl = TREE_OPERAND(fndecl, 0); | |
8497 | tree excess_type = NULL_TREE; | |
8498 | if (DECL_P(fndecl) | |
8499 | && DECL_IS_BUILTIN(fndecl) | |
8500 | && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL | |
8501 | && nargs > 0 | |
8502 | && ((SCALAR_FLOAT_TYPE_P(rettype) | |
8503 | && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0]))) | |
8504 | || (COMPLEX_FLOAT_TYPE_P(rettype) | |
8505 | && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0]))))) | |
8506 | { | |
8507 | excess_type = excess_precision_type(TREE_TYPE(args[0])); | |
8508 | if (excess_type != NULL_TREE) | |
8509 | { | |
8510 | tree excess_fndecl = mathfn_built_in(excess_type, | |
8511 | DECL_FUNCTION_CODE(fndecl)); | |
8512 | if (excess_fndecl == NULL_TREE) | |
8513 | excess_type = NULL_TREE; | |
8514 | else | |
8515 | { | |
8516 | fn = build_fold_addr_expr_loc(location, excess_fndecl); | |
8517 | for (int i = 0; i < nargs; ++i) | |
8518 | args[i] = ::convert(excess_type, args[i]); | |
8519 | } | |
8520 | } | |
8521 | } | |
8522 | ||
8523 | tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype, | |
8524 | fn, nargs, args); | |
8525 | delete[] args; | |
8526 | ||
8527 | SET_EXPR_LOCATION(ret, location); | |
8528 | ||
8529 | if (has_closure) | |
8530 | { | |
8531 | tree closure_tree = func->closure()->get_tree(context); | |
8532 | if (closure_tree != error_mark_node) | |
8533 | CALL_EXPR_STATIC_CHAIN(ret) = closure_tree; | |
8534 | } | |
8535 | ||
8536 | // If this is a recursive function type which returns itself, as in | |
8537 | // type F func() F | |
8538 | // we have used ptr_type_node for the return type. Add a cast here | |
8539 | // to the correct type. | |
8540 | if (TREE_TYPE(ret) == ptr_type_node) | |
8541 | { | |
8542 | tree t = this->type()->get_tree(gogo); | |
8543 | ret = fold_convert_loc(location, t, ret); | |
8544 | } | |
8545 | ||
8546 | if (excess_type != NULL_TREE) | |
8547 | { | |
8548 | // Calling convert here can undo our excess precision change. | |
8549 | // That may or may not be a bug in convert_to_real. | |
8550 | ret = build1(NOP_EXPR, rettype, ret); | |
8551 | } | |
8552 | ||
8553 | // If there is more than one result, we will refer to the call | |
8554 | // multiple times. | |
8555 | if (fntype->results() != NULL && fntype->results()->size() > 1) | |
8556 | ret = save_expr(ret); | |
8557 | ||
8558 | this->tree_ = ret; | |
8559 | ||
8560 | return ret; | |
8561 | } | |
8562 | ||
8563 | // Make a call expression. | |
8564 | ||
8565 | Call_expression* | |
8566 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
8567 | source_location location) | |
8568 | { | |
8569 | return new Call_expression(fn, args, is_varargs, location); | |
8570 | } | |
8571 | ||
8572 | // A single result from a call which returns multiple results. | |
8573 | ||
8574 | class Call_result_expression : public Expression | |
8575 | { | |
8576 | public: | |
8577 | Call_result_expression(Call_expression* call, unsigned int index) | |
8578 | : Expression(EXPRESSION_CALL_RESULT, call->location()), | |
8579 | call_(call), index_(index) | |
8580 | { } | |
8581 | ||
8582 | protected: | |
8583 | int | |
8584 | do_traverse(Traverse*); | |
8585 | ||
8586 | Type* | |
8587 | do_type(); | |
8588 | ||
8589 | void | |
8590 | do_determine_type(const Type_context*); | |
8591 | ||
8592 | void | |
8593 | do_check_types(Gogo*); | |
8594 | ||
8595 | Expression* | |
8596 | do_copy() | |
8597 | { | |
8598 | return new Call_result_expression(this->call_->call_expression(), | |
8599 | this->index_); | |
8600 | } | |
8601 | ||
8602 | bool | |
8603 | do_must_eval_in_order() const | |
8604 | { return true; } | |
8605 | ||
8606 | tree | |
8607 | do_get_tree(Translate_context*); | |
8608 | ||
8609 | private: | |
8610 | // The underlying call expression. | |
8611 | Expression* call_; | |
8612 | // Which result we want. | |
8613 | unsigned int index_; | |
8614 | }; | |
8615 | ||
8616 | // Traverse a call result. | |
8617 | ||
8618 | int | |
8619 | Call_result_expression::do_traverse(Traverse* traverse) | |
8620 | { | |
8621 | if (traverse->remember_expression(this->call_)) | |
8622 | { | |
8623 | // We have already traversed the call expression. | |
8624 | return TRAVERSE_CONTINUE; | |
8625 | } | |
8626 | return Expression::traverse(&this->call_, traverse); | |
8627 | } | |
8628 | ||
8629 | // Get the type. | |
8630 | ||
8631 | Type* | |
8632 | Call_result_expression::do_type() | |
8633 | { | |
8634 | // THIS->CALL_ can be replaced with a temporary reference due to | |
8635 | // Call_expression::do_must_eval_in_order when there is an error. | |
8636 | Call_expression* ce = this->call_->call_expression(); | |
8637 | if (ce == NULL) | |
8638 | return Type::make_error_type(); | |
8639 | Function_type* fntype = ce->get_function_type(); | |
8640 | if (fntype == NULL) | |
8641 | return Type::make_error_type(); | |
8642 | const Typed_identifier_list* results = fntype->results(); | |
8643 | Typed_identifier_list::const_iterator pr = results->begin(); | |
8644 | for (unsigned int i = 0; i < this->index_; ++i) | |
8645 | { | |
8646 | if (pr == results->end()) | |
8647 | return Type::make_error_type(); | |
8648 | ++pr; | |
8649 | } | |
8650 | if (pr == results->end()) | |
8651 | return Type::make_error_type(); | |
8652 | return pr->type(); | |
8653 | } | |
8654 | ||
8655 | // Check the type. This is where we give an error if we're trying to | |
8656 | // extract too many values from a call. | |
8657 | ||
8658 | void | |
8659 | Call_result_expression::do_check_types(Gogo*) | |
8660 | { | |
8661 | bool ok = true; | |
8662 | Call_expression* ce = this->call_->call_expression(); | |
8663 | if (ce != NULL) | |
8664 | ok = this->index_ < ce->result_count(); | |
8665 | else | |
8666 | { | |
8667 | // This can happen when the call returns a single value but we | |
8668 | // are asking for the second result. | |
8669 | if (this->call_->is_error_expression()) | |
8670 | return; | |
8671 | ok = false; | |
8672 | } | |
8673 | if (!ok) | |
567b7660 | 8674 | this->report_error(_("number of results does not match number of values")); |
e440a328 | 8675 | } |
8676 | ||
8677 | // Determine the type. We have nothing to do here, but the 0 result | |
8678 | // needs to pass down to the caller. | |
8679 | ||
8680 | void | |
8681 | Call_result_expression::do_determine_type(const Type_context*) | |
8682 | { | |
8683 | if (this->index_ == 0) | |
8684 | this->call_->determine_type_no_context(); | |
8685 | } | |
8686 | ||
8687 | // Return the tree. | |
8688 | ||
8689 | tree | |
8690 | Call_result_expression::do_get_tree(Translate_context* context) | |
8691 | { | |
8692 | tree call_tree = this->call_->get_tree(context); | |
8693 | if (call_tree == error_mark_node) | |
8694 | return error_mark_node; | |
8695 | gcc_assert(TREE_CODE(TREE_TYPE(call_tree)) == RECORD_TYPE); | |
8696 | tree field = TYPE_FIELDS(TREE_TYPE(call_tree)); | |
8697 | for (unsigned int i = 0; i < this->index_; ++i) | |
8698 | { | |
8699 | gcc_assert(field != NULL_TREE); | |
8700 | field = DECL_CHAIN(field); | |
8701 | } | |
8702 | gcc_assert(field != NULL_TREE); | |
8703 | return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE); | |
8704 | } | |
8705 | ||
8706 | // Make a reference to a single result of a call which returns | |
8707 | // multiple results. | |
8708 | ||
8709 | Expression* | |
8710 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
8711 | { | |
8712 | return new Call_result_expression(call, index); | |
8713 | } | |
8714 | ||
8715 | // Class Index_expression. | |
8716 | ||
8717 | // Traversal. | |
8718 | ||
8719 | int | |
8720 | Index_expression::do_traverse(Traverse* traverse) | |
8721 | { | |
8722 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
8723 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
8724 | || (this->end_ != NULL | |
8725 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)) | |
8726 | return TRAVERSE_EXIT; | |
8727 | return TRAVERSE_CONTINUE; | |
8728 | } | |
8729 | ||
8730 | // Lower an index expression. This converts the generic index | |
8731 | // expression into an array index, a string index, or a map index. | |
8732 | ||
8733 | Expression* | |
8734 | Index_expression::do_lower(Gogo*, Named_object*, int) | |
8735 | { | |
8736 | source_location location = this->location(); | |
8737 | Expression* left = this->left_; | |
8738 | Expression* start = this->start_; | |
8739 | Expression* end = this->end_; | |
8740 | ||
8741 | Type* type = left->type(); | |
8742 | if (type->is_error_type()) | |
8743 | return Expression::make_error(location); | |
8744 | else if (type->array_type() != NULL) | |
8745 | return Expression::make_array_index(left, start, end, location); | |
8746 | else if (type->points_to() != NULL | |
8747 | && type->points_to()->array_type() != NULL | |
8748 | && !type->points_to()->is_open_array_type()) | |
8749 | { | |
8750 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
8751 | location); | |
8752 | return Expression::make_array_index(deref, start, end, location); | |
8753 | } | |
8754 | else if (type->is_string_type()) | |
8755 | return Expression::make_string_index(left, start, end, location); | |
8756 | else if (type->map_type() != NULL) | |
8757 | { | |
8758 | if (end != NULL) | |
8759 | { | |
8760 | error_at(location, "invalid slice of map"); | |
8761 | return Expression::make_error(location); | |
8762 | } | |
8763 | Map_index_expression* ret= Expression::make_map_index(left, start, | |
8764 | location); | |
8765 | if (this->is_lvalue_) | |
8766 | ret->set_is_lvalue(); | |
8767 | return ret; | |
8768 | } | |
8769 | else | |
8770 | { | |
8771 | error_at(location, | |
8772 | "attempt to index object which is not array, string, or map"); | |
8773 | return Expression::make_error(location); | |
8774 | } | |
8775 | } | |
8776 | ||
8777 | // Make an index expression. | |
8778 | ||
8779 | Expression* | |
8780 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
8781 | source_location location) | |
8782 | { | |
8783 | return new Index_expression(left, start, end, location); | |
8784 | } | |
8785 | ||
8786 | // An array index. This is used for both indexing and slicing. | |
8787 | ||
8788 | class Array_index_expression : public Expression | |
8789 | { | |
8790 | public: | |
8791 | Array_index_expression(Expression* array, Expression* start, | |
8792 | Expression* end, source_location location) | |
8793 | : Expression(EXPRESSION_ARRAY_INDEX, location), | |
8794 | array_(array), start_(start), end_(end), type_(NULL) | |
8795 | { } | |
8796 | ||
8797 | protected: | |
8798 | int | |
8799 | do_traverse(Traverse*); | |
8800 | ||
8801 | Type* | |
8802 | do_type(); | |
8803 | ||
8804 | void | |
8805 | do_determine_type(const Type_context*); | |
8806 | ||
8807 | void | |
8808 | do_check_types(Gogo*); | |
8809 | ||
8810 | Expression* | |
8811 | do_copy() | |
8812 | { | |
8813 | return Expression::make_array_index(this->array_->copy(), | |
8814 | this->start_->copy(), | |
8815 | (this->end_ == NULL | |
8816 | ? NULL | |
8817 | : this->end_->copy()), | |
8818 | this->location()); | |
8819 | } | |
8820 | ||
8821 | bool | |
8822 | do_is_addressable() const; | |
8823 | ||
8824 | void | |
8825 | do_address_taken(bool escapes) | |
8826 | { this->array_->address_taken(escapes); } | |
8827 | ||
8828 | tree | |
8829 | do_get_tree(Translate_context*); | |
8830 | ||
8831 | private: | |
8832 | // The array we are getting a value from. | |
8833 | Expression* array_; | |
8834 | // The start or only index. | |
8835 | Expression* start_; | |
8836 | // The end index of a slice. This may be NULL for a simple array | |
8837 | // index, or it may be a nil expression for the length of the array. | |
8838 | Expression* end_; | |
8839 | // The type of the expression. | |
8840 | Type* type_; | |
8841 | }; | |
8842 | ||
8843 | // Array index traversal. | |
8844 | ||
8845 | int | |
8846 | Array_index_expression::do_traverse(Traverse* traverse) | |
8847 | { | |
8848 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
8849 | return TRAVERSE_EXIT; | |
8850 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
8851 | return TRAVERSE_EXIT; | |
8852 | if (this->end_ != NULL) | |
8853 | { | |
8854 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
8855 | return TRAVERSE_EXIT; | |
8856 | } | |
8857 | return TRAVERSE_CONTINUE; | |
8858 | } | |
8859 | ||
8860 | // Return the type of an array index. | |
8861 | ||
8862 | Type* | |
8863 | Array_index_expression::do_type() | |
8864 | { | |
8865 | if (this->type_ == NULL) | |
8866 | { | |
8867 | Array_type* type = this->array_->type()->array_type(); | |
8868 | if (type == NULL) | |
8869 | this->type_ = Type::make_error_type(); | |
8870 | else if (this->end_ == NULL) | |
8871 | this->type_ = type->element_type(); | |
8872 | else if (type->is_open_array_type()) | |
8873 | { | |
8874 | // A slice of a slice has the same type as the original | |
8875 | // slice. | |
8876 | this->type_ = this->array_->type()->deref(); | |
8877 | } | |
8878 | else | |
8879 | { | |
8880 | // A slice of an array is a slice. | |
8881 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
8882 | } | |
8883 | } | |
8884 | return this->type_; | |
8885 | } | |
8886 | ||
8887 | // Set the type of an array index. | |
8888 | ||
8889 | void | |
8890 | Array_index_expression::do_determine_type(const Type_context*) | |
8891 | { | |
8892 | this->array_->determine_type_no_context(); | |
8893 | Type_context subcontext(NULL, true); | |
8894 | this->start_->determine_type(&subcontext); | |
8895 | if (this->end_ != NULL) | |
8896 | this->end_->determine_type(&subcontext); | |
8897 | } | |
8898 | ||
8899 | // Check types of an array index. | |
8900 | ||
8901 | void | |
8902 | Array_index_expression::do_check_types(Gogo*) | |
8903 | { | |
8904 | if (this->start_->type()->integer_type() == NULL) | |
8905 | this->report_error(_("index must be integer")); | |
8906 | if (this->end_ != NULL | |
8907 | && this->end_->type()->integer_type() == NULL | |
8908 | && !this->end_->is_nil_expression()) | |
8909 | this->report_error(_("slice end must be integer")); | |
8910 | ||
8911 | Array_type* array_type = this->array_->type()->array_type(); | |
8912 | gcc_assert(array_type != NULL); | |
8913 | ||
8914 | unsigned int int_bits = | |
8915 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
8916 | ||
8917 | Type* dummy; | |
8918 | mpz_t lval; | |
8919 | mpz_init(lval); | |
8920 | bool lval_valid = (array_type->length() != NULL | |
8921 | && array_type->length()->integer_constant_value(true, | |
8922 | lval, | |
8923 | &dummy)); | |
8924 | mpz_t ival; | |
8925 | mpz_init(ival); | |
8926 | if (this->start_->integer_constant_value(true, ival, &dummy)) | |
8927 | { | |
8928 | if (mpz_sgn(ival) < 0 | |
8929 | || mpz_sizeinbase(ival, 2) >= int_bits | |
8930 | || (lval_valid | |
8931 | && (this->end_ == NULL | |
8932 | ? mpz_cmp(ival, lval) >= 0 | |
8933 | : mpz_cmp(ival, lval) > 0))) | |
8934 | { | |
8935 | error_at(this->start_->location(), "array index out of bounds"); | |
8936 | this->set_is_error(); | |
8937 | } | |
8938 | } | |
8939 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
8940 | { | |
8941 | if (this->end_->integer_constant_value(true, ival, &dummy)) | |
8942 | { | |
8943 | if (mpz_sgn(ival) < 0 | |
8944 | || mpz_sizeinbase(ival, 2) >= int_bits | |
8945 | || (lval_valid && mpz_cmp(ival, lval) > 0)) | |
8946 | { | |
8947 | error_at(this->end_->location(), "array index out of bounds"); | |
8948 | this->set_is_error(); | |
8949 | } | |
8950 | } | |
8951 | } | |
8952 | mpz_clear(ival); | |
8953 | mpz_clear(lval); | |
8954 | ||
8955 | // A slice of an array requires an addressable array. A slice of a | |
8956 | // slice is always possible. | |
8957 | if (this->end_ != NULL | |
8958 | && !array_type->is_open_array_type() | |
8959 | && !this->array_->is_addressable()) | |
8960 | this->report_error(_("array is not addressable")); | |
8961 | } | |
8962 | ||
8963 | // Return whether this expression is addressable. | |
8964 | ||
8965 | bool | |
8966 | Array_index_expression::do_is_addressable() const | |
8967 | { | |
8968 | // A slice expression is not addressable. | |
8969 | if (this->end_ != NULL) | |
8970 | return false; | |
8971 | ||
8972 | // An index into a slice is addressable. | |
8973 | if (this->array_->type()->is_open_array_type()) | |
8974 | return true; | |
8975 | ||
8976 | // An index into an array is addressable if the array is | |
8977 | // addressable. | |
8978 | return this->array_->is_addressable(); | |
8979 | } | |
8980 | ||
8981 | // Get a tree for an array index. | |
8982 | ||
8983 | tree | |
8984 | Array_index_expression::do_get_tree(Translate_context* context) | |
8985 | { | |
8986 | Gogo* gogo = context->gogo(); | |
8987 | source_location loc = this->location(); | |
8988 | ||
8989 | Array_type* array_type = this->array_->type()->array_type(); | |
8990 | gcc_assert(array_type != NULL); | |
8991 | ||
8992 | tree type_tree = array_type->get_tree(gogo); | |
c65212a0 | 8993 | if (type_tree == error_mark_node) |
8994 | return error_mark_node; | |
e440a328 | 8995 | |
8996 | tree array_tree = this->array_->get_tree(context); | |
8997 | if (array_tree == error_mark_node) | |
8998 | return error_mark_node; | |
8999 | ||
9000 | if (array_type->length() == NULL && !DECL_P(array_tree)) | |
9001 | array_tree = save_expr(array_tree); | |
9002 | tree length_tree = array_type->length_tree(gogo, array_tree); | |
c65212a0 | 9003 | if (length_tree == error_mark_node) |
9004 | return error_mark_node; | |
e440a328 | 9005 | length_tree = save_expr(length_tree); |
9006 | tree length_type = TREE_TYPE(length_tree); | |
9007 | ||
9008 | tree bad_index = boolean_false_node; | |
9009 | ||
9010 | tree start_tree = this->start_->get_tree(context); | |
9011 | if (start_tree == error_mark_node) | |
9012 | return error_mark_node; | |
9013 | if (!DECL_P(start_tree)) | |
9014 | start_tree = save_expr(start_tree); | |
9015 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
9016 | start_tree = convert_to_integer(length_type, start_tree); | |
9017 | ||
9018 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
9019 | loc); | |
9020 | ||
9021 | start_tree = fold_convert_loc(loc, length_type, start_tree); | |
9022 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index, | |
9023 | fold_build2_loc(loc, | |
9024 | (this->end_ == NULL | |
9025 | ? GE_EXPR | |
9026 | : GT_EXPR), | |
9027 | boolean_type_node, start_tree, | |
9028 | length_tree)); | |
9029 | ||
9030 | int code = (array_type->length() != NULL | |
9031 | ? (this->end_ == NULL | |
9032 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
9033 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
9034 | : (this->end_ == NULL | |
9035 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
9036 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
9037 | tree crash = Gogo::runtime_error(code, loc); | |
9038 | ||
9039 | if (this->end_ == NULL) | |
9040 | { | |
9041 | // Simple array indexing. This has to return an l-value, so | |
9042 | // wrap the index check into START_TREE. | |
9043 | start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree), | |
9044 | build3(COND_EXPR, void_type_node, | |
9045 | bad_index, crash, NULL_TREE), | |
9046 | start_tree); | |
9047 | start_tree = fold_convert_loc(loc, sizetype, start_tree); | |
9048 | ||
9049 | if (array_type->length() != NULL) | |
9050 | { | |
9051 | // Fixed array. | |
9052 | return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree, | |
9053 | start_tree, NULL_TREE, NULL_TREE); | |
9054 | } | |
9055 | else | |
9056 | { | |
9057 | // Open array. | |
9058 | tree values = array_type->value_pointer_tree(gogo, array_tree); | |
9059 | tree element_type_tree = array_type->element_type()->get_tree(gogo); | |
c65212a0 | 9060 | if (element_type_tree == error_mark_node) |
9061 | return error_mark_node; | |
e440a328 | 9062 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
9063 | tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype, | |
9064 | start_tree, element_size); | |
9065 | tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, | |
9066 | TREE_TYPE(values), values, offset); | |
9067 | return build_fold_indirect_ref(ptr); | |
9068 | } | |
9069 | } | |
9070 | ||
9071 | // Array slice. | |
9072 | ||
9073 | tree capacity_tree = array_type->capacity_tree(gogo, array_tree); | |
c65212a0 | 9074 | if (capacity_tree == error_mark_node) |
9075 | return error_mark_node; | |
e440a328 | 9076 | capacity_tree = fold_convert_loc(loc, length_type, capacity_tree); |
9077 | ||
9078 | tree end_tree; | |
9079 | if (this->end_->is_nil_expression()) | |
9080 | end_tree = length_tree; | |
9081 | else | |
9082 | { | |
9083 | end_tree = this->end_->get_tree(context); | |
9084 | if (end_tree == error_mark_node) | |
9085 | return error_mark_node; | |
9086 | if (!DECL_P(end_tree)) | |
9087 | end_tree = save_expr(end_tree); | |
9088 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
9089 | end_tree = convert_to_integer(length_type, end_tree); | |
9090 | ||
9091 | bad_index = Expression::check_bounds(end_tree, length_type, bad_index, | |
9092 | loc); | |
9093 | ||
9094 | end_tree = fold_convert_loc(loc, length_type, end_tree); | |
9095 | ||
9096 | capacity_tree = save_expr(capacity_tree); | |
9097 | tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9098 | fold_build2_loc(loc, LT_EXPR, | |
9099 | boolean_type_node, | |
9100 | end_tree, start_tree), | |
9101 | fold_build2_loc(loc, GT_EXPR, | |
9102 | boolean_type_node, | |
9103 | end_tree, capacity_tree)); | |
9104 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9105 | bad_index, bad_end); | |
9106 | } | |
9107 | ||
9108 | tree element_type_tree = array_type->element_type()->get_tree(gogo); | |
c65212a0 | 9109 | if (element_type_tree == error_mark_node) |
9110 | return error_mark_node; | |
e440a328 | 9111 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
9112 | ||
9113 | tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype, | |
9114 | fold_convert_loc(loc, sizetype, start_tree), | |
9115 | element_size); | |
9116 | ||
9117 | tree value_pointer = array_type->value_pointer_tree(gogo, array_tree); | |
c65212a0 | 9118 | if (value_pointer == error_mark_node) |
9119 | return error_mark_node; | |
e440a328 | 9120 | |
9121 | value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR, | |
9122 | TREE_TYPE(value_pointer), | |
9123 | value_pointer, offset); | |
9124 | ||
9125 | tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type, | |
9126 | end_tree, start_tree); | |
9127 | ||
9128 | tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type, | |
9129 | capacity_tree, start_tree); | |
9130 | ||
9131 | tree struct_tree = this->type()->get_tree(gogo); | |
9132 | gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE); | |
9133 | ||
9134 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
9135 | ||
9136 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
9137 | tree field = TYPE_FIELDS(struct_tree); | |
9138 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); | |
9139 | elt->index = field; | |
9140 | elt->value = value_pointer; | |
9141 | ||
9142 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
9143 | field = DECL_CHAIN(field); | |
9144 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); | |
9145 | elt->index = field; | |
9146 | elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree); | |
9147 | ||
9148 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
9149 | field = DECL_CHAIN(field); | |
9150 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0); | |
9151 | elt->index = field; | |
9152 | elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree); | |
9153 | ||
9154 | tree constructor = build_constructor(struct_tree, init); | |
9155 | ||
9156 | if (TREE_CONSTANT(value_pointer) | |
9157 | && TREE_CONSTANT(result_length_tree) | |
9158 | && TREE_CONSTANT(result_capacity_tree)) | |
9159 | TREE_CONSTANT(constructor) = 1; | |
9160 | ||
9161 | return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor), | |
9162 | build3(COND_EXPR, void_type_node, | |
9163 | bad_index, crash, NULL_TREE), | |
9164 | constructor); | |
9165 | } | |
9166 | ||
9167 | // Make an array index expression. END may be NULL. | |
9168 | ||
9169 | Expression* | |
9170 | Expression::make_array_index(Expression* array, Expression* start, | |
9171 | Expression* end, source_location location) | |
9172 | { | |
9173 | // Taking a slice of a composite literal requires moving the literal | |
9174 | // onto the heap. | |
9175 | if (end != NULL && array->is_composite_literal()) | |
9176 | { | |
9177 | array = Expression::make_heap_composite(array, location); | |
9178 | array = Expression::make_unary(OPERATOR_MULT, array, location); | |
9179 | } | |
9180 | return new Array_index_expression(array, start, end, location); | |
9181 | } | |
9182 | ||
9183 | // A string index. This is used for both indexing and slicing. | |
9184 | ||
9185 | class String_index_expression : public Expression | |
9186 | { | |
9187 | public: | |
9188 | String_index_expression(Expression* string, Expression* start, | |
9189 | Expression* end, source_location location) | |
9190 | : Expression(EXPRESSION_STRING_INDEX, location), | |
9191 | string_(string), start_(start), end_(end) | |
9192 | { } | |
9193 | ||
9194 | protected: | |
9195 | int | |
9196 | do_traverse(Traverse*); | |
9197 | ||
9198 | Type* | |
9199 | do_type(); | |
9200 | ||
9201 | void | |
9202 | do_determine_type(const Type_context*); | |
9203 | ||
9204 | void | |
9205 | do_check_types(Gogo*); | |
9206 | ||
9207 | Expression* | |
9208 | do_copy() | |
9209 | { | |
9210 | return Expression::make_string_index(this->string_->copy(), | |
9211 | this->start_->copy(), | |
9212 | (this->end_ == NULL | |
9213 | ? NULL | |
9214 | : this->end_->copy()), | |
9215 | this->location()); | |
9216 | } | |
9217 | ||
9218 | tree | |
9219 | do_get_tree(Translate_context*); | |
9220 | ||
9221 | private: | |
9222 | // The string we are getting a value from. | |
9223 | Expression* string_; | |
9224 | // The start or only index. | |
9225 | Expression* start_; | |
9226 | // The end index of a slice. This may be NULL for a single index, | |
9227 | // or it may be a nil expression for the length of the string. | |
9228 | Expression* end_; | |
9229 | }; | |
9230 | ||
9231 | // String index traversal. | |
9232 | ||
9233 | int | |
9234 | String_index_expression::do_traverse(Traverse* traverse) | |
9235 | { | |
9236 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
9237 | return TRAVERSE_EXIT; | |
9238 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
9239 | return TRAVERSE_EXIT; | |
9240 | if (this->end_ != NULL) | |
9241 | { | |
9242 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
9243 | return TRAVERSE_EXIT; | |
9244 | } | |
9245 | return TRAVERSE_CONTINUE; | |
9246 | } | |
9247 | ||
9248 | // Return the type of a string index. | |
9249 | ||
9250 | Type* | |
9251 | String_index_expression::do_type() | |
9252 | { | |
9253 | if (this->end_ == NULL) | |
9254 | return Type::lookup_integer_type("uint8"); | |
9255 | else | |
9256 | return Type::make_string_type(); | |
9257 | } | |
9258 | ||
9259 | // Determine the type of a string index. | |
9260 | ||
9261 | void | |
9262 | String_index_expression::do_determine_type(const Type_context*) | |
9263 | { | |
9264 | this->string_->determine_type_no_context(); | |
9265 | Type_context subcontext(NULL, true); | |
9266 | this->start_->determine_type(&subcontext); | |
9267 | if (this->end_ != NULL) | |
9268 | this->end_->determine_type(&subcontext); | |
9269 | } | |
9270 | ||
9271 | // Check types of a string index. | |
9272 | ||
9273 | void | |
9274 | String_index_expression::do_check_types(Gogo*) | |
9275 | { | |
9276 | if (this->start_->type()->integer_type() == NULL) | |
9277 | this->report_error(_("index must be integer")); | |
9278 | if (this->end_ != NULL | |
9279 | && this->end_->type()->integer_type() == NULL | |
9280 | && !this->end_->is_nil_expression()) | |
9281 | this->report_error(_("slice end must be integer")); | |
9282 | ||
9283 | std::string sval; | |
9284 | bool sval_valid = this->string_->string_constant_value(&sval); | |
9285 | ||
9286 | mpz_t ival; | |
9287 | mpz_init(ival); | |
9288 | Type* dummy; | |
9289 | if (this->start_->integer_constant_value(true, ival, &dummy)) | |
9290 | { | |
9291 | if (mpz_sgn(ival) < 0 | |
9292 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0)) | |
9293 | { | |
9294 | error_at(this->start_->location(), "string index out of bounds"); | |
9295 | this->set_is_error(); | |
9296 | } | |
9297 | } | |
9298 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
9299 | { | |
9300 | if (this->end_->integer_constant_value(true, ival, &dummy)) | |
9301 | { | |
9302 | if (mpz_sgn(ival) < 0 | |
9303 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0)) | |
9304 | { | |
9305 | error_at(this->end_->location(), "string index out of bounds"); | |
9306 | this->set_is_error(); | |
9307 | } | |
9308 | } | |
9309 | } | |
9310 | mpz_clear(ival); | |
9311 | } | |
9312 | ||
9313 | // Get a tree for a string index. | |
9314 | ||
9315 | tree | |
9316 | String_index_expression::do_get_tree(Translate_context* context) | |
9317 | { | |
9318 | source_location loc = this->location(); | |
9319 | ||
9320 | tree string_tree = this->string_->get_tree(context); | |
9321 | if (string_tree == error_mark_node) | |
9322 | return error_mark_node; | |
9323 | ||
9324 | if (this->string_->type()->points_to() != NULL) | |
9325 | string_tree = build_fold_indirect_ref(string_tree); | |
9326 | if (!DECL_P(string_tree)) | |
9327 | string_tree = save_expr(string_tree); | |
9328 | tree string_type = TREE_TYPE(string_tree); | |
9329 | ||
9330 | tree length_tree = String_type::length_tree(context->gogo(), string_tree); | |
9331 | length_tree = save_expr(length_tree); | |
9332 | tree length_type = TREE_TYPE(length_tree); | |
9333 | ||
9334 | tree bad_index = boolean_false_node; | |
9335 | ||
9336 | tree start_tree = this->start_->get_tree(context); | |
9337 | if (start_tree == error_mark_node) | |
9338 | return error_mark_node; | |
9339 | if (!DECL_P(start_tree)) | |
9340 | start_tree = save_expr(start_tree); | |
9341 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
9342 | start_tree = convert_to_integer(length_type, start_tree); | |
9343 | ||
9344 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
9345 | loc); | |
9346 | ||
9347 | start_tree = fold_convert_loc(loc, length_type, start_tree); | |
9348 | ||
9349 | int code = (this->end_ == NULL | |
9350 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
9351 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
9352 | tree crash = Gogo::runtime_error(code, loc); | |
9353 | ||
9354 | if (this->end_ == NULL) | |
9355 | { | |
9356 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9357 | bad_index, | |
9358 | fold_build2_loc(loc, GE_EXPR, | |
9359 | boolean_type_node, | |
9360 | start_tree, length_tree)); | |
9361 | ||
9362 | tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree); | |
9363 | tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree), | |
9364 | bytes_tree, | |
9365 | fold_convert_loc(loc, sizetype, start_tree)); | |
9366 | tree index = build_fold_indirect_ref_loc(loc, ptr); | |
9367 | ||
9368 | return build2(COMPOUND_EXPR, TREE_TYPE(index), | |
9369 | build3(COND_EXPR, void_type_node, | |
9370 | bad_index, crash, NULL_TREE), | |
9371 | index); | |
9372 | } | |
9373 | else | |
9374 | { | |
9375 | tree end_tree; | |
9376 | if (this->end_->is_nil_expression()) | |
9377 | end_tree = build_int_cst(length_type, -1); | |
9378 | else | |
9379 | { | |
9380 | end_tree = this->end_->get_tree(context); | |
9381 | if (end_tree == error_mark_node) | |
9382 | return error_mark_node; | |
9383 | if (!DECL_P(end_tree)) | |
9384 | end_tree = save_expr(end_tree); | |
9385 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
9386 | end_tree = convert_to_integer(length_type, end_tree); | |
9387 | ||
9388 | bad_index = Expression::check_bounds(end_tree, length_type, | |
9389 | bad_index, loc); | |
9390 | ||
9391 | end_tree = fold_convert_loc(loc, length_type, end_tree); | |
9392 | } | |
9393 | ||
9394 | static tree strslice_fndecl; | |
9395 | tree ret = Gogo::call_builtin(&strslice_fndecl, | |
9396 | loc, | |
9397 | "__go_string_slice", | |
9398 | 3, | |
9399 | string_type, | |
9400 | string_type, | |
9401 | string_tree, | |
9402 | length_type, | |
9403 | start_tree, | |
9404 | length_type, | |
9405 | end_tree); | |
9406 | // This will panic if the bounds are out of range for the | |
9407 | // string. | |
9408 | TREE_NOTHROW(strslice_fndecl) = 0; | |
9409 | ||
9410 | if (bad_index == boolean_false_node) | |
9411 | return ret; | |
9412 | else | |
9413 | return build2(COMPOUND_EXPR, TREE_TYPE(ret), | |
9414 | build3(COND_EXPR, void_type_node, | |
9415 | bad_index, crash, NULL_TREE), | |
9416 | ret); | |
9417 | } | |
9418 | } | |
9419 | ||
9420 | // Make a string index expression. END may be NULL. | |
9421 | ||
9422 | Expression* | |
9423 | Expression::make_string_index(Expression* string, Expression* start, | |
9424 | Expression* end, source_location location) | |
9425 | { | |
9426 | return new String_index_expression(string, start, end, location); | |
9427 | } | |
9428 | ||
9429 | // Class Map_index. | |
9430 | ||
9431 | // Get the type of the map. | |
9432 | ||
9433 | Map_type* | |
9434 | Map_index_expression::get_map_type() const | |
9435 | { | |
9436 | Map_type* mt = this->map_->type()->deref()->map_type(); | |
9437 | gcc_assert(mt != NULL); | |
9438 | return mt; | |
9439 | } | |
9440 | ||
9441 | // Map index traversal. | |
9442 | ||
9443 | int | |
9444 | Map_index_expression::do_traverse(Traverse* traverse) | |
9445 | { | |
9446 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
9447 | return TRAVERSE_EXIT; | |
9448 | return Expression::traverse(&this->index_, traverse); | |
9449 | } | |
9450 | ||
9451 | // Return the type of a map index. | |
9452 | ||
9453 | Type* | |
9454 | Map_index_expression::do_type() | |
9455 | { | |
9456 | Type* type = this->get_map_type()->val_type(); | |
9457 | // If this map index is in a tuple assignment, we actually return a | |
9458 | // pointer to the value type. Tuple_map_assignment_statement is | |
9459 | // responsible for handling this correctly. We need to get the type | |
9460 | // right in case this gets assigned to a temporary variable. | |
9461 | if (this->is_in_tuple_assignment_) | |
9462 | type = Type::make_pointer_type(type); | |
9463 | return type; | |
9464 | } | |
9465 | ||
9466 | // Fix the type of a map index. | |
9467 | ||
9468 | void | |
9469 | Map_index_expression::do_determine_type(const Type_context*) | |
9470 | { | |
9471 | this->map_->determine_type_no_context(); | |
9472 | Type_context subcontext(this->get_map_type()->key_type(), false); | |
9473 | this->index_->determine_type(&subcontext); | |
9474 | } | |
9475 | ||
9476 | // Check types of a map index. | |
9477 | ||
9478 | void | |
9479 | Map_index_expression::do_check_types(Gogo*) | |
9480 | { | |
9481 | std::string reason; | |
9482 | if (!Type::are_assignable(this->get_map_type()->key_type(), | |
9483 | this->index_->type(), &reason)) | |
9484 | { | |
9485 | if (reason.empty()) | |
9486 | this->report_error(_("incompatible type for map index")); | |
9487 | else | |
9488 | { | |
9489 | error_at(this->location(), "incompatible type for map index (%s)", | |
9490 | reason.c_str()); | |
9491 | this->set_is_error(); | |
9492 | } | |
9493 | } | |
9494 | } | |
9495 | ||
9496 | // Get a tree for a map index. | |
9497 | ||
9498 | tree | |
9499 | Map_index_expression::do_get_tree(Translate_context* context) | |
9500 | { | |
9501 | Map_type* type = this->get_map_type(); | |
9502 | ||
9503 | tree valptr = this->get_value_pointer(context, this->is_lvalue_); | |
9504 | if (valptr == error_mark_node) | |
9505 | return error_mark_node; | |
9506 | valptr = save_expr(valptr); | |
9507 | ||
9508 | tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr)); | |
9509 | ||
9510 | if (this->is_lvalue_) | |
9511 | return build_fold_indirect_ref(valptr); | |
9512 | else if (this->is_in_tuple_assignment_) | |
9513 | { | |
9514 | // Tuple_map_assignment_statement is responsible for using this | |
9515 | // appropriately. | |
9516 | return valptr; | |
9517 | } | |
9518 | else | |
9519 | { | |
9520 | return fold_build3(COND_EXPR, val_type_tree, | |
9521 | fold_build2(EQ_EXPR, boolean_type_node, valptr, | |
9522 | fold_convert(TREE_TYPE(valptr), | |
9523 | null_pointer_node)), | |
9524 | type->val_type()->get_init_tree(context->gogo(), | |
9525 | false), | |
9526 | build_fold_indirect_ref(valptr)); | |
9527 | } | |
9528 | } | |
9529 | ||
9530 | // Get a tree for the map index. This returns a tree which evaluates | |
9531 | // to a pointer to a value. The pointer will be NULL if the key is | |
9532 | // not in the map. | |
9533 | ||
9534 | tree | |
9535 | Map_index_expression::get_value_pointer(Translate_context* context, | |
9536 | bool insert) | |
9537 | { | |
9538 | Map_type* type = this->get_map_type(); | |
9539 | ||
9540 | tree map_tree = this->map_->get_tree(context); | |
9541 | tree index_tree = this->index_->get_tree(context); | |
9542 | index_tree = Expression::convert_for_assignment(context, type->key_type(), | |
9543 | this->index_->type(), | |
9544 | index_tree, | |
9545 | this->location()); | |
9546 | if (map_tree == error_mark_node || index_tree == error_mark_node) | |
9547 | return error_mark_node; | |
9548 | ||
9549 | if (this->map_->type()->points_to() != NULL) | |
9550 | map_tree = build_fold_indirect_ref(map_tree); | |
9551 | ||
9552 | // We need to pass in a pointer to the key, so stuff it into a | |
9553 | // variable. | |
9554 | tree tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree)); | |
9555 | DECL_IGNORED_P(tmp) = 0; | |
9556 | DECL_INITIAL(tmp) = index_tree; | |
9557 | tree make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
9558 | tree tmpref = fold_convert(const_ptr_type_node, build_fold_addr_expr(tmp)); | |
9559 | TREE_ADDRESSABLE(tmp) = 1; | |
9560 | ||
9561 | static tree map_index_fndecl; | |
9562 | tree call = Gogo::call_builtin(&map_index_fndecl, | |
9563 | this->location(), | |
9564 | "__go_map_index", | |
9565 | 3, | |
9566 | const_ptr_type_node, | |
9567 | TREE_TYPE(map_tree), | |
9568 | map_tree, | |
9569 | const_ptr_type_node, | |
9570 | tmpref, | |
9571 | boolean_type_node, | |
9572 | (insert | |
9573 | ? boolean_true_node | |
9574 | : boolean_false_node)); | |
9575 | // This can panic on a map of interface type if the interface holds | |
9576 | // an uncomparable or unhashable type. | |
9577 | TREE_NOTHROW(map_index_fndecl) = 0; | |
9578 | ||
9579 | tree val_type_tree = type->val_type()->get_tree(context->gogo()); | |
9580 | if (val_type_tree == error_mark_node) | |
9581 | return error_mark_node; | |
9582 | tree ptr_val_type_tree = build_pointer_type(val_type_tree); | |
9583 | ||
9584 | return build2(COMPOUND_EXPR, ptr_val_type_tree, | |
9585 | make_tmp, | |
9586 | fold_convert(ptr_val_type_tree, call)); | |
9587 | } | |
9588 | ||
9589 | // Make a map index expression. | |
9590 | ||
9591 | Map_index_expression* | |
9592 | Expression::make_map_index(Expression* map, Expression* index, | |
9593 | source_location location) | |
9594 | { | |
9595 | return new Map_index_expression(map, index, location); | |
9596 | } | |
9597 | ||
9598 | // Class Field_reference_expression. | |
9599 | ||
9600 | // Return the type of a field reference. | |
9601 | ||
9602 | Type* | |
9603 | Field_reference_expression::do_type() | |
9604 | { | |
9605 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
9606 | gcc_assert(struct_type != NULL); | |
9607 | return struct_type->field(this->field_index_)->type(); | |
9608 | } | |
9609 | ||
9610 | // Check the types for a field reference. | |
9611 | ||
9612 | void | |
9613 | Field_reference_expression::do_check_types(Gogo*) | |
9614 | { | |
9615 | Struct_type* struct_type = this->expr_->type()->struct_type(); | |
9616 | gcc_assert(struct_type != NULL); | |
9617 | gcc_assert(struct_type->field(this->field_index_) != NULL); | |
9618 | } | |
9619 | ||
9620 | // Get a tree for a field reference. | |
9621 | ||
9622 | tree | |
9623 | Field_reference_expression::do_get_tree(Translate_context* context) | |
9624 | { | |
9625 | tree struct_tree = this->expr_->get_tree(context); | |
9626 | if (struct_tree == error_mark_node | |
9627 | || TREE_TYPE(struct_tree) == error_mark_node) | |
9628 | return error_mark_node; | |
9629 | gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE); | |
9630 | tree field = TYPE_FIELDS(TREE_TYPE(struct_tree)); | |
9631 | gcc_assert(field != NULL_TREE); | |
9632 | for (unsigned int i = this->field_index_; i > 0; --i) | |
9633 | { | |
9634 | field = DECL_CHAIN(field); | |
9635 | gcc_assert(field != NULL_TREE); | |
9636 | } | |
9637 | return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field, | |
9638 | NULL_TREE); | |
9639 | } | |
9640 | ||
9641 | // Make a reference to a qualified identifier in an expression. | |
9642 | ||
9643 | Field_reference_expression* | |
9644 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
9645 | source_location location) | |
9646 | { | |
9647 | return new Field_reference_expression(expr, field_index, location); | |
9648 | } | |
9649 | ||
9650 | // Class Interface_field_reference_expression. | |
9651 | ||
9652 | // Return a tree for the pointer to the function to call. | |
9653 | ||
9654 | tree | |
9655 | Interface_field_reference_expression::get_function_tree(Translate_context*, | |
9656 | tree expr) | |
9657 | { | |
9658 | if (this->expr_->type()->points_to() != NULL) | |
9659 | expr = build_fold_indirect_ref(expr); | |
9660 | ||
9661 | tree expr_type = TREE_TYPE(expr); | |
9662 | gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE); | |
9663 | ||
9664 | tree field = TYPE_FIELDS(expr_type); | |
9665 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0); | |
9666 | ||
9667 | tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
9668 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(table))); | |
9669 | ||
9670 | table = build_fold_indirect_ref(table); | |
9671 | gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE); | |
9672 | ||
9673 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
9674 | for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table))); | |
9675 | field != NULL_TREE; | |
9676 | field = DECL_CHAIN(field)) | |
9677 | { | |
9678 | if (name == IDENTIFIER_POINTER(DECL_NAME(field))) | |
9679 | break; | |
9680 | } | |
9681 | gcc_assert(field != NULL_TREE); | |
9682 | ||
9683 | return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE); | |
9684 | } | |
9685 | ||
9686 | // Return a tree for the first argument to pass to the interface | |
9687 | // function. | |
9688 | ||
9689 | tree | |
9690 | Interface_field_reference_expression::get_underlying_object_tree( | |
9691 | Translate_context*, | |
9692 | tree expr) | |
9693 | { | |
9694 | if (this->expr_->type()->points_to() != NULL) | |
9695 | expr = build_fold_indirect_ref(expr); | |
9696 | ||
9697 | tree expr_type = TREE_TYPE(expr); | |
9698 | gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE); | |
9699 | ||
9700 | tree field = DECL_CHAIN(TYPE_FIELDS(expr_type)); | |
9701 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); | |
9702 | ||
9703 | return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
9704 | } | |
9705 | ||
9706 | // Traversal. | |
9707 | ||
9708 | int | |
9709 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
9710 | { | |
9711 | return Expression::traverse(&this->expr_, traverse); | |
9712 | } | |
9713 | ||
9714 | // Return the type of an interface field reference. | |
9715 | ||
9716 | Type* | |
9717 | Interface_field_reference_expression::do_type() | |
9718 | { | |
9719 | Type* expr_type = this->expr_->type(); | |
9720 | ||
9721 | Type* points_to = expr_type->points_to(); | |
9722 | if (points_to != NULL) | |
9723 | expr_type = points_to; | |
9724 | ||
9725 | Interface_type* interface_type = expr_type->interface_type(); | |
9726 | if (interface_type == NULL) | |
9727 | return Type::make_error_type(); | |
9728 | ||
9729 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
9730 | if (method == NULL) | |
9731 | return Type::make_error_type(); | |
9732 | ||
9733 | return method->type(); | |
9734 | } | |
9735 | ||
9736 | // Determine types. | |
9737 | ||
9738 | void | |
9739 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
9740 | { | |
9741 | this->expr_->determine_type_no_context(); | |
9742 | } | |
9743 | ||
9744 | // Check the types for an interface field reference. | |
9745 | ||
9746 | void | |
9747 | Interface_field_reference_expression::do_check_types(Gogo*) | |
9748 | { | |
9749 | Type* type = this->expr_->type(); | |
9750 | ||
9751 | Type* points_to = type->points_to(); | |
9752 | if (points_to != NULL) | |
9753 | type = points_to; | |
9754 | ||
9755 | Interface_type* interface_type = type->interface_type(); | |
9756 | if (interface_type == NULL) | |
9757 | this->report_error(_("expected interface or pointer to interface")); | |
9758 | else | |
9759 | { | |
9760 | const Typed_identifier* method = | |
9761 | interface_type->find_method(this->name_); | |
9762 | if (method == NULL) | |
9763 | { | |
9764 | error_at(this->location(), "method %qs not in interface", | |
9765 | Gogo::message_name(this->name_).c_str()); | |
9766 | this->set_is_error(); | |
9767 | } | |
9768 | } | |
9769 | } | |
9770 | ||
9771 | // Get a tree for a reference to a field in an interface. There is no | |
9772 | // standard tree type representation for this: it's a function | |
9773 | // attached to its first argument, like a Bound_method_expression. | |
9774 | // The only places it may currently be used are in a Call_expression | |
9775 | // or a Go_statement, which will take it apart directly. So this has | |
9776 | // nothing to do at present. | |
9777 | ||
9778 | tree | |
9779 | Interface_field_reference_expression::do_get_tree(Translate_context*) | |
9780 | { | |
9781 | gcc_unreachable(); | |
9782 | } | |
9783 | ||
9784 | // Make a reference to a field in an interface. | |
9785 | ||
9786 | Expression* | |
9787 | Expression::make_interface_field_reference(Expression* expr, | |
9788 | const std::string& field, | |
9789 | source_location location) | |
9790 | { | |
9791 | return new Interface_field_reference_expression(expr, field, location); | |
9792 | } | |
9793 | ||
9794 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
9795 | // is lowered after we know the type of the left hand side. | |
9796 | ||
9797 | class Selector_expression : public Parser_expression | |
9798 | { | |
9799 | public: | |
9800 | Selector_expression(Expression* left, const std::string& name, | |
9801 | source_location location) | |
9802 | : Parser_expression(EXPRESSION_SELECTOR, location), | |
9803 | left_(left), name_(name) | |
9804 | { } | |
9805 | ||
9806 | protected: | |
9807 | int | |
9808 | do_traverse(Traverse* traverse) | |
9809 | { return Expression::traverse(&this->left_, traverse); } | |
9810 | ||
9811 | Expression* | |
9812 | do_lower(Gogo*, Named_object*, int); | |
9813 | ||
9814 | Expression* | |
9815 | do_copy() | |
9816 | { | |
9817 | return new Selector_expression(this->left_->copy(), this->name_, | |
9818 | this->location()); | |
9819 | } | |
9820 | ||
9821 | private: | |
9822 | Expression* | |
9823 | lower_method_expression(Gogo*); | |
9824 | ||
9825 | // The expression on the left hand side. | |
9826 | Expression* left_; | |
9827 | // The name on the right hand side. | |
9828 | std::string name_; | |
9829 | }; | |
9830 | ||
9831 | // Lower a selector expression once we know the real type of the left | |
9832 | // hand side. | |
9833 | ||
9834 | Expression* | |
9835 | Selector_expression::do_lower(Gogo* gogo, Named_object*, int) | |
9836 | { | |
9837 | Expression* left = this->left_; | |
9838 | if (left->is_type_expression()) | |
9839 | return this->lower_method_expression(gogo); | |
9840 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
9841 | this->location()); | |
9842 | } | |
9843 | ||
9844 | // Lower a method expression T.M or (*T).M. We turn this into a | |
9845 | // function literal. | |
9846 | ||
9847 | Expression* | |
9848 | Selector_expression::lower_method_expression(Gogo* gogo) | |
9849 | { | |
9850 | source_location location = this->location(); | |
9851 | Type* type = this->left_->type(); | |
9852 | const std::string& name(this->name_); | |
9853 | ||
9854 | bool is_pointer; | |
9855 | if (type->points_to() == NULL) | |
9856 | is_pointer = false; | |
9857 | else | |
9858 | { | |
9859 | is_pointer = true; | |
9860 | type = type->points_to(); | |
9861 | } | |
9862 | Named_type* nt = type->named_type(); | |
9863 | if (nt == NULL) | |
9864 | { | |
9865 | error_at(location, | |
9866 | ("method expression requires named type or " | |
9867 | "pointer to named type")); | |
9868 | return Expression::make_error(location); | |
9869 | } | |
9870 | ||
9871 | bool is_ambiguous; | |
9872 | Method* method = nt->method_function(name, &is_ambiguous); | |
9873 | if (method == NULL) | |
9874 | { | |
9875 | if (!is_ambiguous) | |
9876 | error_at(location, "type %<%s%> has no method %<%s%>", | |
9877 | nt->message_name().c_str(), | |
9878 | Gogo::message_name(name).c_str()); | |
9879 | else | |
9880 | error_at(location, "method %<%s%> is ambiguous in type %<%s%>", | |
9881 | Gogo::message_name(name).c_str(), | |
9882 | nt->message_name().c_str()); | |
9883 | return Expression::make_error(location); | |
9884 | } | |
9885 | ||
9886 | if (!is_pointer && !method->is_value_method()) | |
9887 | { | |
9888 | error_at(location, "method requires pointer (use %<(*%s).%s)%>", | |
9889 | nt->message_name().c_str(), | |
9890 | Gogo::message_name(name).c_str()); | |
9891 | return Expression::make_error(location); | |
9892 | } | |
9893 | ||
9894 | // Build a new function type in which the receiver becomes the first | |
9895 | // argument. | |
9896 | Function_type* method_type = method->type(); | |
9897 | gcc_assert(method_type->is_method()); | |
9898 | ||
9899 | const char* const receiver_name = "$this"; | |
9900 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
9901 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
9902 | location)); | |
9903 | ||
9904 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
9905 | if (method_parameters != NULL) | |
9906 | { | |
9907 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); | |
9908 | p != method_parameters->end(); | |
9909 | ++p) | |
9910 | parameters->push_back(*p); | |
9911 | } | |
9912 | ||
9913 | const Typed_identifier_list* method_results = method_type->results(); | |
9914 | Typed_identifier_list* results; | |
9915 | if (method_results == NULL) | |
9916 | results = NULL; | |
9917 | else | |
9918 | { | |
9919 | results = new Typed_identifier_list(); | |
9920 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
9921 | p != method_results->end(); | |
9922 | ++p) | |
9923 | results->push_back(*p); | |
9924 | } | |
9925 | ||
9926 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
9927 | location); | |
9928 | if (method_type->is_varargs()) | |
9929 | fntype->set_is_varargs(); | |
9930 | ||
9931 | // We generate methods which always takes a pointer to the receiver | |
9932 | // as their first argument. If this is for a pointer type, we can | |
9933 | // simply reuse the existing function. We use an internal hack to | |
9934 | // get the right type. | |
9935 | ||
9936 | if (is_pointer) | |
9937 | { | |
9938 | Named_object* mno = (method->needs_stub_method() | |
9939 | ? method->stub_object() | |
9940 | : method->named_object()); | |
9941 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
9942 | f = Expression::make_cast(fntype, f, location); | |
9943 | Type_conversion_expression* tce = | |
9944 | static_cast<Type_conversion_expression*>(f); | |
9945 | tce->set_may_convert_function_types(); | |
9946 | return f; | |
9947 | } | |
9948 | ||
9949 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
9950 | location); | |
9951 | ||
9952 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
9953 | gcc_assert(vno != NULL); | |
9954 | Expression* ve = Expression::make_var_reference(vno, location); | |
9955 | Expression* bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
9956 | gcc_assert(bm != NULL && !bm->is_error_expression()); | |
9957 | ||
9958 | Expression_list* args; | |
9959 | if (method_parameters == NULL) | |
9960 | args = NULL; | |
9961 | else | |
9962 | { | |
9963 | args = new Expression_list(); | |
9964 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); | |
9965 | p != method_parameters->end(); | |
9966 | ++p) | |
9967 | { | |
9968 | vno = gogo->lookup(p->name(), NULL); | |
9969 | gcc_assert(vno != NULL); | |
9970 | args->push_back(Expression::make_var_reference(vno, location)); | |
9971 | } | |
9972 | } | |
9973 | ||
9974 | Call_expression* call = Expression::make_call(bm, args, | |
9975 | method_type->is_varargs(), | |
9976 | location); | |
9977 | ||
9978 | size_t count = call->result_count(); | |
9979 | Statement* s; | |
9980 | if (count == 0) | |
9981 | s = Statement::make_statement(call); | |
9982 | else | |
9983 | { | |
9984 | Expression_list* retvals = new Expression_list(); | |
9985 | if (count <= 1) | |
9986 | retvals->push_back(call); | |
9987 | else | |
9988 | { | |
9989 | for (size_t i = 0; i < count; ++i) | |
9990 | retvals->push_back(Expression::make_call_result(call, i)); | |
9991 | } | |
9992 | s = Statement::make_return_statement(no->func_value()->type()->results(), | |
9993 | retvals, location); | |
9994 | } | |
9995 | gogo->add_statement(s); | |
9996 | ||
9997 | gogo->finish_function(location); | |
9998 | ||
9999 | return Expression::make_func_reference(no, NULL, location); | |
10000 | } | |
10001 | ||
10002 | // Make a selector expression. | |
10003 | ||
10004 | Expression* | |
10005 | Expression::make_selector(Expression* left, const std::string& name, | |
10006 | source_location location) | |
10007 | { | |
10008 | return new Selector_expression(left, name, location); | |
10009 | } | |
10010 | ||
10011 | // Implement the builtin function new. | |
10012 | ||
10013 | class Allocation_expression : public Expression | |
10014 | { | |
10015 | public: | |
10016 | Allocation_expression(Type* type, source_location location) | |
10017 | : Expression(EXPRESSION_ALLOCATION, location), | |
10018 | type_(type) | |
10019 | { } | |
10020 | ||
10021 | protected: | |
10022 | int | |
10023 | do_traverse(Traverse* traverse) | |
10024 | { return Type::traverse(this->type_, traverse); } | |
10025 | ||
10026 | Type* | |
10027 | do_type() | |
10028 | { return Type::make_pointer_type(this->type_); } | |
10029 | ||
10030 | void | |
10031 | do_determine_type(const Type_context*) | |
10032 | { } | |
10033 | ||
10034 | void | |
10035 | do_check_types(Gogo*); | |
10036 | ||
10037 | Expression* | |
10038 | do_copy() | |
10039 | { return new Allocation_expression(this->type_, this->location()); } | |
10040 | ||
10041 | tree | |
10042 | do_get_tree(Translate_context*); | |
10043 | ||
10044 | private: | |
10045 | // The type we are allocating. | |
10046 | Type* type_; | |
10047 | }; | |
10048 | ||
10049 | // Check the type of an allocation expression. | |
10050 | ||
10051 | void | |
10052 | Allocation_expression::do_check_types(Gogo*) | |
10053 | { | |
10054 | if (this->type_->function_type() != NULL) | |
10055 | this->report_error(_("invalid new of function type")); | |
10056 | } | |
10057 | ||
10058 | // Return a tree for an allocation expression. | |
10059 | ||
10060 | tree | |
10061 | Allocation_expression::do_get_tree(Translate_context* context) | |
10062 | { | |
10063 | tree type_tree = this->type_->get_tree(context->gogo()); | |
10064 | tree size_tree = TYPE_SIZE_UNIT(type_tree); | |
10065 | tree space = context->gogo()->allocate_memory(this->type_, size_tree, | |
10066 | this->location()); | |
10067 | return fold_convert(build_pointer_type(type_tree), space); | |
10068 | } | |
10069 | ||
10070 | // Make an allocation expression. | |
10071 | ||
10072 | Expression* | |
10073 | Expression::make_allocation(Type* type, source_location location) | |
10074 | { | |
10075 | return new Allocation_expression(type, location); | |
10076 | } | |
10077 | ||
10078 | // Implement the builtin function make. | |
10079 | ||
10080 | class Make_expression : public Expression | |
10081 | { | |
10082 | public: | |
10083 | Make_expression(Type* type, Expression_list* args, source_location location) | |
10084 | : Expression(EXPRESSION_MAKE, location), | |
10085 | type_(type), args_(args) | |
10086 | { } | |
10087 | ||
10088 | protected: | |
10089 | int | |
10090 | do_traverse(Traverse* traverse); | |
10091 | ||
10092 | Type* | |
10093 | do_type() | |
10094 | { return this->type_; } | |
10095 | ||
10096 | void | |
10097 | do_determine_type(const Type_context*); | |
10098 | ||
10099 | void | |
10100 | do_check_types(Gogo*); | |
10101 | ||
10102 | Expression* | |
10103 | do_copy() | |
10104 | { | |
10105 | return new Make_expression(this->type_, this->args_->copy(), | |
10106 | this->location()); | |
10107 | } | |
10108 | ||
10109 | tree | |
10110 | do_get_tree(Translate_context*); | |
10111 | ||
10112 | private: | |
10113 | // The type we are making. | |
10114 | Type* type_; | |
10115 | // The arguments to pass to the make routine. | |
10116 | Expression_list* args_; | |
10117 | }; | |
10118 | ||
10119 | // Traversal. | |
10120 | ||
10121 | int | |
10122 | Make_expression::do_traverse(Traverse* traverse) | |
10123 | { | |
10124 | if (this->args_ != NULL | |
10125 | && this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
10126 | return TRAVERSE_EXIT; | |
10127 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10128 | return TRAVERSE_EXIT; | |
10129 | return TRAVERSE_CONTINUE; | |
10130 | } | |
10131 | ||
10132 | // Set types of arguments. | |
10133 | ||
10134 | void | |
10135 | Make_expression::do_determine_type(const Type_context*) | |
10136 | { | |
10137 | if (this->args_ != NULL) | |
10138 | { | |
10139 | Type_context context(Type::lookup_integer_type("int"), false); | |
10140 | for (Expression_list::const_iterator pe = this->args_->begin(); | |
10141 | pe != this->args_->end(); | |
10142 | ++pe) | |
10143 | (*pe)->determine_type(&context); | |
10144 | } | |
10145 | } | |
10146 | ||
10147 | // Check types for a make expression. | |
10148 | ||
10149 | void | |
10150 | Make_expression::do_check_types(Gogo*) | |
10151 | { | |
10152 | if (this->type_->channel_type() == NULL | |
10153 | && this->type_->map_type() == NULL | |
10154 | && (this->type_->array_type() == NULL | |
10155 | || this->type_->array_type()->length() != NULL)) | |
10156 | this->report_error(_("invalid type for make function")); | |
10157 | else if (!this->type_->check_make_expression(this->args_, this->location())) | |
10158 | this->set_is_error(); | |
10159 | } | |
10160 | ||
10161 | // Return a tree for a make expression. | |
10162 | ||
10163 | tree | |
10164 | Make_expression::do_get_tree(Translate_context* context) | |
10165 | { | |
10166 | return this->type_->make_expression_tree(context, this->args_, | |
10167 | this->location()); | |
10168 | } | |
10169 | ||
10170 | // Make a make expression. | |
10171 | ||
10172 | Expression* | |
10173 | Expression::make_make(Type* type, Expression_list* args, | |
10174 | source_location location) | |
10175 | { | |
10176 | return new Make_expression(type, args, location); | |
10177 | } | |
10178 | ||
10179 | // Construct a struct. | |
10180 | ||
10181 | class Struct_construction_expression : public Expression | |
10182 | { | |
10183 | public: | |
10184 | Struct_construction_expression(Type* type, Expression_list* vals, | |
10185 | source_location location) | |
10186 | : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location), | |
10187 | type_(type), vals_(vals) | |
10188 | { } | |
10189 | ||
10190 | // Return whether this is a constant initializer. | |
10191 | bool | |
10192 | is_constant_struct() const; | |
10193 | ||
10194 | protected: | |
10195 | int | |
10196 | do_traverse(Traverse* traverse); | |
10197 | ||
10198 | Type* | |
10199 | do_type() | |
10200 | { return this->type_; } | |
10201 | ||
10202 | void | |
10203 | do_determine_type(const Type_context*); | |
10204 | ||
10205 | void | |
10206 | do_check_types(Gogo*); | |
10207 | ||
10208 | Expression* | |
10209 | do_copy() | |
10210 | { | |
10211 | return new Struct_construction_expression(this->type_, this->vals_->copy(), | |
10212 | this->location()); | |
10213 | } | |
10214 | ||
10215 | bool | |
10216 | do_is_addressable() const | |
10217 | { return true; } | |
10218 | ||
10219 | tree | |
10220 | do_get_tree(Translate_context*); | |
10221 | ||
10222 | void | |
10223 | do_export(Export*) const; | |
10224 | ||
10225 | private: | |
10226 | // The type of the struct to construct. | |
10227 | Type* type_; | |
10228 | // The list of values, in order of the fields in the struct. A NULL | |
10229 | // entry means that the field should be zero-initialized. | |
10230 | Expression_list* vals_; | |
10231 | }; | |
10232 | ||
10233 | // Traversal. | |
10234 | ||
10235 | int | |
10236 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
10237 | { | |
10238 | if (this->vals_ != NULL | |
10239 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
10240 | return TRAVERSE_EXIT; | |
10241 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10242 | return TRAVERSE_EXIT; | |
10243 | return TRAVERSE_CONTINUE; | |
10244 | } | |
10245 | ||
10246 | // Return whether this is a constant initializer. | |
10247 | ||
10248 | bool | |
10249 | Struct_construction_expression::is_constant_struct() const | |
10250 | { | |
10251 | if (this->vals_ == NULL) | |
10252 | return true; | |
10253 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10254 | pv != this->vals_->end(); | |
10255 | ++pv) | |
10256 | { | |
10257 | if (*pv != NULL | |
10258 | && !(*pv)->is_constant() | |
10259 | && (!(*pv)->is_composite_literal() | |
10260 | || (*pv)->is_nonconstant_composite_literal())) | |
10261 | return false; | |
10262 | } | |
10263 | ||
10264 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10265 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10266 | pf != fields->end(); | |
10267 | ++pf) | |
10268 | { | |
10269 | // There are no constant constructors for interfaces. | |
10270 | if (pf->type()->interface_type() != NULL) | |
10271 | return false; | |
10272 | } | |
10273 | ||
10274 | return true; | |
10275 | } | |
10276 | ||
10277 | // Final type determination. | |
10278 | ||
10279 | void | |
10280 | Struct_construction_expression::do_determine_type(const Type_context*) | |
10281 | { | |
10282 | if (this->vals_ == NULL) | |
10283 | return; | |
10284 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10285 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10286 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10287 | pf != fields->end(); | |
10288 | ++pf, ++pv) | |
10289 | { | |
10290 | if (pv == this->vals_->end()) | |
10291 | return; | |
10292 | if (*pv != NULL) | |
10293 | { | |
10294 | Type_context subcontext(pf->type(), false); | |
10295 | (*pv)->determine_type(&subcontext); | |
10296 | } | |
10297 | } | |
10298 | } | |
10299 | ||
10300 | // Check types. | |
10301 | ||
10302 | void | |
10303 | Struct_construction_expression::do_check_types(Gogo*) | |
10304 | { | |
10305 | if (this->vals_ == NULL) | |
10306 | return; | |
10307 | ||
10308 | Struct_type* st = this->type_->struct_type(); | |
10309 | if (this->vals_->size() > st->field_count()) | |
10310 | { | |
10311 | this->report_error(_("too many expressions for struct")); | |
10312 | return; | |
10313 | } | |
10314 | ||
10315 | const Struct_field_list* fields = st->fields(); | |
10316 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10317 | int i = 0; | |
10318 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10319 | pf != fields->end(); | |
10320 | ++pf, ++pv, ++i) | |
10321 | { | |
10322 | if (pv == this->vals_->end()) | |
10323 | { | |
10324 | this->report_error(_("too few expressions for struct")); | |
10325 | break; | |
10326 | } | |
10327 | ||
10328 | if (*pv == NULL) | |
10329 | continue; | |
10330 | ||
10331 | std::string reason; | |
10332 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
10333 | { | |
10334 | if (reason.empty()) | |
10335 | error_at((*pv)->location(), | |
10336 | "incompatible type for field %d in struct construction", | |
10337 | i + 1); | |
10338 | else | |
10339 | error_at((*pv)->location(), | |
10340 | ("incompatible type for field %d in " | |
10341 | "struct construction (%s)"), | |
10342 | i + 1, reason.c_str()); | |
10343 | this->set_is_error(); | |
10344 | } | |
10345 | } | |
10346 | gcc_assert(pv == this->vals_->end()); | |
10347 | } | |
10348 | ||
10349 | // Return a tree for constructing a struct. | |
10350 | ||
10351 | tree | |
10352 | Struct_construction_expression::do_get_tree(Translate_context* context) | |
10353 | { | |
10354 | Gogo* gogo = context->gogo(); | |
10355 | ||
10356 | if (this->vals_ == NULL) | |
10357 | return this->type_->get_init_tree(gogo, false); | |
10358 | ||
10359 | tree type_tree = this->type_->get_tree(gogo); | |
10360 | if (type_tree == error_mark_node) | |
10361 | return error_mark_node; | |
10362 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); | |
10363 | ||
10364 | bool is_constant = true; | |
10365 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10366 | VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc, | |
10367 | fields->size()); | |
10368 | Struct_field_list::const_iterator pf = fields->begin(); | |
10369 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10370 | for (tree field = TYPE_FIELDS(type_tree); | |
10371 | field != NULL_TREE; | |
10372 | field = DECL_CHAIN(field), ++pf) | |
10373 | { | |
10374 | gcc_assert(pf != fields->end()); | |
10375 | ||
10376 | tree val; | |
10377 | if (pv == this->vals_->end()) | |
10378 | val = pf->type()->get_init_tree(gogo, false); | |
10379 | else if (*pv == NULL) | |
10380 | { | |
10381 | val = pf->type()->get_init_tree(gogo, false); | |
10382 | ++pv; | |
10383 | } | |
10384 | else | |
10385 | { | |
10386 | val = Expression::convert_for_assignment(context, pf->type(), | |
10387 | (*pv)->type(), | |
10388 | (*pv)->get_tree(context), | |
10389 | this->location()); | |
10390 | ++pv; | |
10391 | } | |
10392 | ||
10393 | if (val == error_mark_node || TREE_TYPE(val) == error_mark_node) | |
10394 | return error_mark_node; | |
10395 | ||
10396 | constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL); | |
10397 | elt->index = field; | |
10398 | elt->value = val; | |
10399 | if (!TREE_CONSTANT(val)) | |
10400 | is_constant = false; | |
10401 | } | |
10402 | gcc_assert(pf == fields->end()); | |
10403 | ||
10404 | tree ret = build_constructor(type_tree, elts); | |
10405 | if (is_constant) | |
10406 | TREE_CONSTANT(ret) = 1; | |
10407 | return ret; | |
10408 | } | |
10409 | ||
10410 | // Export a struct construction. | |
10411 | ||
10412 | void | |
10413 | Struct_construction_expression::do_export(Export* exp) const | |
10414 | { | |
10415 | exp->write_c_string("convert("); | |
10416 | exp->write_type(this->type_); | |
10417 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10418 | pv != this->vals_->end(); | |
10419 | ++pv) | |
10420 | { | |
10421 | exp->write_c_string(", "); | |
10422 | if (*pv != NULL) | |
10423 | (*pv)->export_expression(exp); | |
10424 | } | |
10425 | exp->write_c_string(")"); | |
10426 | } | |
10427 | ||
10428 | // Make a struct composite literal. This used by the thunk code. | |
10429 | ||
10430 | Expression* | |
10431 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
10432 | source_location location) | |
10433 | { | |
10434 | gcc_assert(type->struct_type() != NULL); | |
10435 | return new Struct_construction_expression(type, vals, location); | |
10436 | } | |
10437 | ||
10438 | // Construct an array. This class is not used directly; instead we | |
10439 | // use the child classes, Fixed_array_construction_expression and | |
10440 | // Open_array_construction_expression. | |
10441 | ||
10442 | class Array_construction_expression : public Expression | |
10443 | { | |
10444 | protected: | |
10445 | Array_construction_expression(Expression_classification classification, | |
10446 | Type* type, Expression_list* vals, | |
10447 | source_location location) | |
10448 | : Expression(classification, location), | |
10449 | type_(type), vals_(vals) | |
10450 | { } | |
10451 | ||
10452 | public: | |
10453 | // Return whether this is a constant initializer. | |
10454 | bool | |
10455 | is_constant_array() const; | |
10456 | ||
10457 | // Return the number of elements. | |
10458 | size_t | |
10459 | element_count() const | |
10460 | { return this->vals_ == NULL ? 0 : this->vals_->size(); } | |
10461 | ||
10462 | protected: | |
10463 | int | |
10464 | do_traverse(Traverse* traverse); | |
10465 | ||
10466 | Type* | |
10467 | do_type() | |
10468 | { return this->type_; } | |
10469 | ||
10470 | void | |
10471 | do_determine_type(const Type_context*); | |
10472 | ||
10473 | void | |
10474 | do_check_types(Gogo*); | |
10475 | ||
10476 | bool | |
10477 | do_is_addressable() const | |
10478 | { return true; } | |
10479 | ||
10480 | void | |
10481 | do_export(Export*) const; | |
10482 | ||
10483 | // The list of values. | |
10484 | Expression_list* | |
10485 | vals() | |
10486 | { return this->vals_; } | |
10487 | ||
10488 | // Get a constructor tree for the array values. | |
10489 | tree | |
10490 | get_constructor_tree(Translate_context* context, tree type_tree); | |
10491 | ||
10492 | private: | |
10493 | // The type of the array to construct. | |
10494 | Type* type_; | |
10495 | // The list of values. | |
10496 | Expression_list* vals_; | |
10497 | }; | |
10498 | ||
10499 | // Traversal. | |
10500 | ||
10501 | int | |
10502 | Array_construction_expression::do_traverse(Traverse* traverse) | |
10503 | { | |
10504 | if (this->vals_ != NULL | |
10505 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
10506 | return TRAVERSE_EXIT; | |
10507 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10508 | return TRAVERSE_EXIT; | |
10509 | return TRAVERSE_CONTINUE; | |
10510 | } | |
10511 | ||
10512 | // Return whether this is a constant initializer. | |
10513 | ||
10514 | bool | |
10515 | Array_construction_expression::is_constant_array() const | |
10516 | { | |
10517 | if (this->vals_ == NULL) | |
10518 | return true; | |
10519 | ||
10520 | // There are no constant constructors for interfaces. | |
10521 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
10522 | return false; | |
10523 | ||
10524 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10525 | pv != this->vals_->end(); | |
10526 | ++pv) | |
10527 | { | |
10528 | if (*pv != NULL | |
10529 | && !(*pv)->is_constant() | |
10530 | && (!(*pv)->is_composite_literal() | |
10531 | || (*pv)->is_nonconstant_composite_literal())) | |
10532 | return false; | |
10533 | } | |
10534 | return true; | |
10535 | } | |
10536 | ||
10537 | // Final type determination. | |
10538 | ||
10539 | void | |
10540 | Array_construction_expression::do_determine_type(const Type_context*) | |
10541 | { | |
10542 | if (this->vals_ == NULL) | |
10543 | return; | |
10544 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
10545 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10546 | pv != this->vals_->end(); | |
10547 | ++pv) | |
10548 | { | |
10549 | if (*pv != NULL) | |
10550 | (*pv)->determine_type(&subcontext); | |
10551 | } | |
10552 | } | |
10553 | ||
10554 | // Check types. | |
10555 | ||
10556 | void | |
10557 | Array_construction_expression::do_check_types(Gogo*) | |
10558 | { | |
10559 | if (this->vals_ == NULL) | |
10560 | return; | |
10561 | ||
10562 | Array_type* at = this->type_->array_type(); | |
10563 | int i = 0; | |
10564 | Type* element_type = at->element_type(); | |
10565 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10566 | pv != this->vals_->end(); | |
10567 | ++pv, ++i) | |
10568 | { | |
10569 | if (*pv != NULL | |
10570 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
10571 | { | |
10572 | error_at((*pv)->location(), | |
10573 | "incompatible type for element %d in composite literal", | |
10574 | i + 1); | |
10575 | this->set_is_error(); | |
10576 | } | |
10577 | } | |
10578 | ||
10579 | Expression* length = at->length(); | |
10580 | if (length != NULL) | |
10581 | { | |
10582 | mpz_t val; | |
10583 | mpz_init(val); | |
10584 | Type* type; | |
10585 | if (at->length()->integer_constant_value(true, val, &type)) | |
10586 | { | |
10587 | if (this->vals_->size() > mpz_get_ui(val)) | |
10588 | this->report_error(_("too many elements in composite literal")); | |
10589 | } | |
10590 | mpz_clear(val); | |
10591 | } | |
10592 | } | |
10593 | ||
10594 | // Get a constructor tree for the array values. | |
10595 | ||
10596 | tree | |
10597 | Array_construction_expression::get_constructor_tree(Translate_context* context, | |
10598 | tree type_tree) | |
10599 | { | |
10600 | VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc, | |
10601 | (this->vals_ == NULL | |
10602 | ? 0 | |
10603 | : this->vals_->size())); | |
10604 | Type* element_type = this->type_->array_type()->element_type(); | |
10605 | bool is_constant = true; | |
10606 | if (this->vals_ != NULL) | |
10607 | { | |
10608 | size_t i = 0; | |
10609 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10610 | pv != this->vals_->end(); | |
10611 | ++pv, ++i) | |
10612 | { | |
10613 | constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL); | |
10614 | elt->index = size_int(i); | |
10615 | if (*pv == NULL) | |
10616 | elt->value = element_type->get_init_tree(context->gogo(), false); | |
10617 | else | |
10618 | { | |
10619 | tree value_tree = (*pv)->get_tree(context); | |
10620 | elt->value = Expression::convert_for_assignment(context, | |
10621 | element_type, | |
10622 | (*pv)->type(), | |
10623 | value_tree, | |
10624 | this->location()); | |
10625 | } | |
10626 | if (elt->value == error_mark_node) | |
10627 | return error_mark_node; | |
10628 | if (!TREE_CONSTANT(elt->value)) | |
10629 | is_constant = false; | |
10630 | } | |
10631 | } | |
10632 | ||
10633 | tree ret = build_constructor(type_tree, values); | |
10634 | if (is_constant) | |
10635 | TREE_CONSTANT(ret) = 1; | |
10636 | return ret; | |
10637 | } | |
10638 | ||
10639 | // Export an array construction. | |
10640 | ||
10641 | void | |
10642 | Array_construction_expression::do_export(Export* exp) const | |
10643 | { | |
10644 | exp->write_c_string("convert("); | |
10645 | exp->write_type(this->type_); | |
10646 | if (this->vals_ != NULL) | |
10647 | { | |
10648 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10649 | pv != this->vals_->end(); | |
10650 | ++pv) | |
10651 | { | |
10652 | exp->write_c_string(", "); | |
10653 | if (*pv != NULL) | |
10654 | (*pv)->export_expression(exp); | |
10655 | } | |
10656 | } | |
10657 | exp->write_c_string(")"); | |
10658 | } | |
10659 | ||
10660 | // Construct a fixed array. | |
10661 | ||
10662 | class Fixed_array_construction_expression : | |
10663 | public Array_construction_expression | |
10664 | { | |
10665 | public: | |
10666 | Fixed_array_construction_expression(Type* type, Expression_list* vals, | |
10667 | source_location location) | |
10668 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, | |
10669 | type, vals, location) | |
10670 | { | |
10671 | gcc_assert(type->array_type() != NULL | |
10672 | && type->array_type()->length() != NULL); | |
10673 | } | |
10674 | ||
10675 | protected: | |
10676 | Expression* | |
10677 | do_copy() | |
10678 | { | |
10679 | return new Fixed_array_construction_expression(this->type(), | |
10680 | (this->vals() == NULL | |
10681 | ? NULL | |
10682 | : this->vals()->copy()), | |
10683 | this->location()); | |
10684 | } | |
10685 | ||
10686 | tree | |
10687 | do_get_tree(Translate_context*); | |
10688 | }; | |
10689 | ||
10690 | // Return a tree for constructing a fixed array. | |
10691 | ||
10692 | tree | |
10693 | Fixed_array_construction_expression::do_get_tree(Translate_context* context) | |
10694 | { | |
10695 | return this->get_constructor_tree(context, | |
10696 | this->type()->get_tree(context->gogo())); | |
10697 | } | |
10698 | ||
10699 | // Construct an open array. | |
10700 | ||
10701 | class Open_array_construction_expression : public Array_construction_expression | |
10702 | { | |
10703 | public: | |
10704 | Open_array_construction_expression(Type* type, Expression_list* vals, | |
10705 | source_location location) | |
10706 | : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION, | |
10707 | type, vals, location) | |
10708 | { | |
10709 | gcc_assert(type->array_type() != NULL | |
10710 | && type->array_type()->length() == NULL); | |
10711 | } | |
10712 | ||
10713 | protected: | |
10714 | // Note that taking the address of an open array literal is invalid. | |
10715 | ||
10716 | Expression* | |
10717 | do_copy() | |
10718 | { | |
10719 | return new Open_array_construction_expression(this->type(), | |
10720 | (this->vals() == NULL | |
10721 | ? NULL | |
10722 | : this->vals()->copy()), | |
10723 | this->location()); | |
10724 | } | |
10725 | ||
10726 | tree | |
10727 | do_get_tree(Translate_context*); | |
10728 | }; | |
10729 | ||
10730 | // Return a tree for constructing an open array. | |
10731 | ||
10732 | tree | |
10733 | Open_array_construction_expression::do_get_tree(Translate_context* context) | |
10734 | { | |
10735 | Type* element_type = this->type()->array_type()->element_type(); | |
10736 | tree element_type_tree = element_type->get_tree(context->gogo()); | |
3d60812e | 10737 | if (element_type_tree == error_mark_node) |
10738 | return error_mark_node; | |
10739 | ||
e440a328 | 10740 | tree values; |
10741 | tree length_tree; | |
10742 | if (this->vals() == NULL || this->vals()->empty()) | |
10743 | { | |
10744 | // We need to create a unique value. | |
10745 | tree max = size_int(0); | |
10746 | tree constructor_type = build_array_type(element_type_tree, | |
10747 | build_index_type(max)); | |
10748 | if (constructor_type == error_mark_node) | |
10749 | return error_mark_node; | |
10750 | VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1); | |
10751 | constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL); | |
10752 | elt->index = size_int(0); | |
10753 | elt->value = element_type->get_init_tree(context->gogo(), false); | |
10754 | values = build_constructor(constructor_type, vec); | |
10755 | if (TREE_CONSTANT(elt->value)) | |
10756 | TREE_CONSTANT(values) = 1; | |
10757 | length_tree = size_int(0); | |
10758 | } | |
10759 | else | |
10760 | { | |
10761 | tree max = size_int(this->vals()->size() - 1); | |
10762 | tree constructor_type = build_array_type(element_type_tree, | |
10763 | build_index_type(max)); | |
10764 | if (constructor_type == error_mark_node) | |
10765 | return error_mark_node; | |
10766 | values = this->get_constructor_tree(context, constructor_type); | |
10767 | length_tree = size_int(this->vals()->size()); | |
10768 | } | |
10769 | ||
10770 | if (values == error_mark_node) | |
10771 | return error_mark_node; | |
10772 | ||
10773 | bool is_constant_initializer = TREE_CONSTANT(values); | |
10774 | bool is_in_function = context->function() != NULL; | |
10775 | ||
10776 | if (is_constant_initializer) | |
10777 | { | |
10778 | tree tmp = build_decl(this->location(), VAR_DECL, | |
10779 | create_tmp_var_name("C"), TREE_TYPE(values)); | |
10780 | DECL_EXTERNAL(tmp) = 0; | |
10781 | TREE_PUBLIC(tmp) = 0; | |
10782 | TREE_STATIC(tmp) = 1; | |
10783 | DECL_ARTIFICIAL(tmp) = 1; | |
10784 | if (is_in_function) | |
10785 | { | |
10786 | // If this is not a function, we will only initialize the | |
10787 | // value once, so we can use this directly rather than | |
10788 | // copying it. In that case we can't make it read-only, | |
10789 | // because the program is permitted to change it. | |
10790 | TREE_READONLY(tmp) = 1; | |
10791 | TREE_CONSTANT(tmp) = 1; | |
10792 | } | |
10793 | DECL_INITIAL(tmp) = values; | |
10794 | rest_of_decl_compilation(tmp, 1, 0); | |
10795 | values = tmp; | |
10796 | } | |
10797 | ||
10798 | tree space; | |
10799 | tree set; | |
10800 | if (!is_in_function && is_constant_initializer) | |
10801 | { | |
10802 | // Outside of a function, we know the initializer will only run | |
10803 | // once. | |
10804 | space = build_fold_addr_expr(values); | |
10805 | set = NULL_TREE; | |
10806 | } | |
10807 | else | |
10808 | { | |
10809 | tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values)); | |
10810 | space = context->gogo()->allocate_memory(element_type, memsize, | |
10811 | this->location()); | |
10812 | space = save_expr(space); | |
10813 | ||
10814 | tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space); | |
10815 | tree ref = build_fold_indirect_ref_loc(this->location(), s); | |
10816 | TREE_THIS_NOTRAP(ref) = 1; | |
10817 | set = build2(MODIFY_EXPR, void_type_node, ref, values); | |
10818 | } | |
10819 | ||
10820 | // Build a constructor for the open array. | |
10821 | ||
10822 | tree type_tree = this->type()->get_tree(context->gogo()); | |
3d60812e | 10823 | if (type_tree == error_mark_node) |
10824 | return error_mark_node; | |
e440a328 | 10825 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); |
10826 | ||
10827 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
10828 | ||
10829 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
10830 | tree field = TYPE_FIELDS(type_tree); | |
10831 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); | |
10832 | elt->index = field; | |
10833 | elt->value = fold_convert(TREE_TYPE(field), space); | |
10834 | ||
10835 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
10836 | field = DECL_CHAIN(field); | |
10837 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); | |
10838 | elt->index = field; | |
10839 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
10840 | ||
10841 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
10842 | field = DECL_CHAIN(field); | |
10843 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0); | |
10844 | elt->index = field; | |
10845 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
10846 | ||
10847 | tree constructor = build_constructor(type_tree, init); | |
3d60812e | 10848 | if (constructor == error_mark_node) |
10849 | return error_mark_node; | |
e440a328 | 10850 | if (!is_in_function && is_constant_initializer) |
10851 | TREE_CONSTANT(constructor) = 1; | |
10852 | ||
10853 | if (set == NULL_TREE) | |
10854 | return constructor; | |
10855 | else | |
10856 | return build2(COMPOUND_EXPR, type_tree, set, constructor); | |
10857 | } | |
10858 | ||
10859 | // Make a slice composite literal. This is used by the type | |
10860 | // descriptor code. | |
10861 | ||
10862 | Expression* | |
10863 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, | |
10864 | source_location location) | |
10865 | { | |
10866 | gcc_assert(type->is_open_array_type()); | |
10867 | return new Open_array_construction_expression(type, vals, location); | |
10868 | } | |
10869 | ||
10870 | // Construct a map. | |
10871 | ||
10872 | class Map_construction_expression : public Expression | |
10873 | { | |
10874 | public: | |
10875 | Map_construction_expression(Type* type, Expression_list* vals, | |
10876 | source_location location) | |
10877 | : Expression(EXPRESSION_MAP_CONSTRUCTION, location), | |
10878 | type_(type), vals_(vals) | |
10879 | { gcc_assert(vals == NULL || vals->size() % 2 == 0); } | |
10880 | ||
10881 | protected: | |
10882 | int | |
10883 | do_traverse(Traverse* traverse); | |
10884 | ||
10885 | Type* | |
10886 | do_type() | |
10887 | { return this->type_; } | |
10888 | ||
10889 | void | |
10890 | do_determine_type(const Type_context*); | |
10891 | ||
10892 | void | |
10893 | do_check_types(Gogo*); | |
10894 | ||
10895 | Expression* | |
10896 | do_copy() | |
10897 | { | |
10898 | return new Map_construction_expression(this->type_, this->vals_->copy(), | |
10899 | this->location()); | |
10900 | } | |
10901 | ||
10902 | tree | |
10903 | do_get_tree(Translate_context*); | |
10904 | ||
10905 | void | |
10906 | do_export(Export*) const; | |
10907 | ||
10908 | private: | |
10909 | // The type of the map to construct. | |
10910 | Type* type_; | |
10911 | // The list of values. | |
10912 | Expression_list* vals_; | |
10913 | }; | |
10914 | ||
10915 | // Traversal. | |
10916 | ||
10917 | int | |
10918 | Map_construction_expression::do_traverse(Traverse* traverse) | |
10919 | { | |
10920 | if (this->vals_ != NULL | |
10921 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
10922 | return TRAVERSE_EXIT; | |
10923 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10924 | return TRAVERSE_EXIT; | |
10925 | return TRAVERSE_CONTINUE; | |
10926 | } | |
10927 | ||
10928 | // Final type determination. | |
10929 | ||
10930 | void | |
10931 | Map_construction_expression::do_determine_type(const Type_context*) | |
10932 | { | |
10933 | if (this->vals_ == NULL) | |
10934 | return; | |
10935 | ||
10936 | Map_type* mt = this->type_->map_type(); | |
10937 | Type_context key_context(mt->key_type(), false); | |
10938 | Type_context val_context(mt->val_type(), false); | |
10939 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10940 | pv != this->vals_->end(); | |
10941 | ++pv) | |
10942 | { | |
10943 | (*pv)->determine_type(&key_context); | |
10944 | ++pv; | |
10945 | (*pv)->determine_type(&val_context); | |
10946 | } | |
10947 | } | |
10948 | ||
10949 | // Check types. | |
10950 | ||
10951 | void | |
10952 | Map_construction_expression::do_check_types(Gogo*) | |
10953 | { | |
10954 | if (this->vals_ == NULL) | |
10955 | return; | |
10956 | ||
10957 | Map_type* mt = this->type_->map_type(); | |
10958 | int i = 0; | |
10959 | Type* key_type = mt->key_type(); | |
10960 | Type* val_type = mt->val_type(); | |
10961 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10962 | pv != this->vals_->end(); | |
10963 | ++pv, ++i) | |
10964 | { | |
10965 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
10966 | { | |
10967 | error_at((*pv)->location(), | |
10968 | "incompatible type for element %d key in map construction", | |
10969 | i + 1); | |
10970 | this->set_is_error(); | |
10971 | } | |
10972 | ++pv; | |
10973 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
10974 | { | |
10975 | error_at((*pv)->location(), | |
10976 | ("incompatible type for element %d value " | |
10977 | "in map construction"), | |
10978 | i + 1); | |
10979 | this->set_is_error(); | |
10980 | } | |
10981 | } | |
10982 | } | |
10983 | ||
10984 | // Return a tree for constructing a map. | |
10985 | ||
10986 | tree | |
10987 | Map_construction_expression::do_get_tree(Translate_context* context) | |
10988 | { | |
10989 | Gogo* gogo = context->gogo(); | |
10990 | source_location loc = this->location(); | |
10991 | ||
10992 | Map_type* mt = this->type_->map_type(); | |
10993 | ||
10994 | // Build a struct to hold the key and value. | |
10995 | tree struct_type = make_node(RECORD_TYPE); | |
10996 | ||
10997 | Type* key_type = mt->key_type(); | |
10998 | tree id = get_identifier("__key"); | |
10999 | tree key_field = build_decl(loc, FIELD_DECL, id, key_type->get_tree(gogo)); | |
11000 | DECL_CONTEXT(key_field) = struct_type; | |
11001 | TYPE_FIELDS(struct_type) = key_field; | |
11002 | ||
11003 | Type* val_type = mt->val_type(); | |
11004 | id = get_identifier("__val"); | |
11005 | tree val_field = build_decl(loc, FIELD_DECL, id, val_type->get_tree(gogo)); | |
11006 | DECL_CONTEXT(val_field) = struct_type; | |
11007 | DECL_CHAIN(key_field) = val_field; | |
11008 | ||
11009 | layout_type(struct_type); | |
11010 | ||
11011 | bool is_constant = true; | |
11012 | size_t i = 0; | |
11013 | tree valaddr; | |
11014 | tree make_tmp; | |
11015 | ||
11016 | if (this->vals_ == NULL || this->vals_->empty()) | |
11017 | { | |
11018 | valaddr = null_pointer_node; | |
11019 | make_tmp = NULL_TREE; | |
11020 | } | |
11021 | else | |
11022 | { | |
11023 | VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc, | |
11024 | this->vals_->size() / 2); | |
11025 | ||
11026 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11027 | pv != this->vals_->end(); | |
11028 | ++pv, ++i) | |
11029 | { | |
11030 | bool one_is_constant = true; | |
11031 | ||
11032 | VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2); | |
11033 | ||
11034 | constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL); | |
11035 | elt->index = key_field; | |
11036 | tree val_tree = (*pv)->get_tree(context); | |
11037 | elt->value = Expression::convert_for_assignment(context, key_type, | |
11038 | (*pv)->type(), | |
11039 | val_tree, loc); | |
11040 | if (elt->value == error_mark_node) | |
11041 | return error_mark_node; | |
11042 | if (!TREE_CONSTANT(elt->value)) | |
11043 | one_is_constant = false; | |
11044 | ||
11045 | ++pv; | |
11046 | ||
11047 | elt = VEC_quick_push(constructor_elt, one, NULL); | |
11048 | elt->index = val_field; | |
11049 | val_tree = (*pv)->get_tree(context); | |
11050 | elt->value = Expression::convert_for_assignment(context, val_type, | |
11051 | (*pv)->type(), | |
11052 | val_tree, loc); | |
11053 | if (elt->value == error_mark_node) | |
11054 | return error_mark_node; | |
11055 | if (!TREE_CONSTANT(elt->value)) | |
11056 | one_is_constant = false; | |
11057 | ||
11058 | elt = VEC_quick_push(constructor_elt, values, NULL); | |
11059 | elt->index = size_int(i); | |
11060 | elt->value = build_constructor(struct_type, one); | |
11061 | if (one_is_constant) | |
11062 | TREE_CONSTANT(elt->value) = 1; | |
11063 | else | |
11064 | is_constant = false; | |
11065 | } | |
11066 | ||
11067 | tree index_type = build_index_type(size_int(i - 1)); | |
11068 | tree array_type = build_array_type(struct_type, index_type); | |
11069 | tree init = build_constructor(array_type, values); | |
11070 | if (is_constant) | |
11071 | TREE_CONSTANT(init) = 1; | |
11072 | tree tmp; | |
11073 | if (current_function_decl != NULL) | |
11074 | { | |
11075 | tmp = create_tmp_var(array_type, get_name(array_type)); | |
11076 | DECL_INITIAL(tmp) = init; | |
11077 | make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp); | |
11078 | TREE_ADDRESSABLE(tmp) = 1; | |
11079 | } | |
11080 | else | |
11081 | { | |
11082 | tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type); | |
11083 | DECL_EXTERNAL(tmp) = 0; | |
11084 | TREE_PUBLIC(tmp) = 0; | |
11085 | TREE_STATIC(tmp) = 1; | |
11086 | DECL_ARTIFICIAL(tmp) = 1; | |
11087 | if (!TREE_CONSTANT(init)) | |
11088 | make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp, | |
11089 | init); | |
11090 | else | |
11091 | { | |
11092 | TREE_READONLY(tmp) = 1; | |
11093 | TREE_CONSTANT(tmp) = 1; | |
11094 | DECL_INITIAL(tmp) = init; | |
11095 | make_tmp = NULL_TREE; | |
11096 | } | |
11097 | rest_of_decl_compilation(tmp, 1, 0); | |
11098 | } | |
11099 | ||
11100 | valaddr = build_fold_addr_expr(tmp); | |
11101 | } | |
11102 | ||
11103 | tree descriptor = gogo->map_descriptor(mt); | |
11104 | ||
11105 | tree type_tree = this->type_->get_tree(gogo); | |
11106 | ||
11107 | static tree construct_map_fndecl; | |
11108 | tree call = Gogo::call_builtin(&construct_map_fndecl, | |
11109 | loc, | |
11110 | "__go_construct_map", | |
11111 | 6, | |
11112 | type_tree, | |
11113 | TREE_TYPE(descriptor), | |
11114 | descriptor, | |
11115 | sizetype, | |
11116 | size_int(i), | |
11117 | sizetype, | |
11118 | TYPE_SIZE_UNIT(struct_type), | |
11119 | sizetype, | |
11120 | byte_position(val_field), | |
11121 | sizetype, | |
11122 | TYPE_SIZE_UNIT(TREE_TYPE(val_field)), | |
11123 | const_ptr_type_node, | |
11124 | fold_convert(const_ptr_type_node, valaddr)); | |
11125 | ||
11126 | tree ret; | |
11127 | if (make_tmp == NULL) | |
11128 | ret = call; | |
11129 | else | |
11130 | ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call); | |
11131 | return ret; | |
11132 | } | |
11133 | ||
11134 | // Export an array construction. | |
11135 | ||
11136 | void | |
11137 | Map_construction_expression::do_export(Export* exp) const | |
11138 | { | |
11139 | exp->write_c_string("convert("); | |
11140 | exp->write_type(this->type_); | |
11141 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11142 | pv != this->vals_->end(); | |
11143 | ++pv) | |
11144 | { | |
11145 | exp->write_c_string(", "); | |
11146 | (*pv)->export_expression(exp); | |
11147 | } | |
11148 | exp->write_c_string(")"); | |
11149 | } | |
11150 | ||
11151 | // A general composite literal. This is lowered to a type specific | |
11152 | // version. | |
11153 | ||
11154 | class Composite_literal_expression : public Parser_expression | |
11155 | { | |
11156 | public: | |
11157 | Composite_literal_expression(Type* type, int depth, bool has_keys, | |
11158 | Expression_list* vals, source_location location) | |
11159 | : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location), | |
11160 | type_(type), depth_(depth), vals_(vals), has_keys_(has_keys) | |
11161 | { } | |
11162 | ||
11163 | protected: | |
11164 | int | |
11165 | do_traverse(Traverse* traverse); | |
11166 | ||
11167 | Expression* | |
11168 | do_lower(Gogo*, Named_object*, int); | |
11169 | ||
11170 | Expression* | |
11171 | do_copy() | |
11172 | { | |
11173 | return new Composite_literal_expression(this->type_, this->depth_, | |
11174 | this->has_keys_, | |
11175 | (this->vals_ == NULL | |
11176 | ? NULL | |
11177 | : this->vals_->copy()), | |
11178 | this->location()); | |
11179 | } | |
11180 | ||
11181 | private: | |
11182 | Expression* | |
11183 | lower_struct(Type*); | |
11184 | ||
11185 | Expression* | |
11186 | lower_array(Type*); | |
11187 | ||
11188 | Expression* | |
11189 | make_array(Type*, Expression_list*); | |
11190 | ||
11191 | Expression* | |
a287720d | 11192 | lower_map(Gogo*, Named_object*, Type*); |
e440a328 | 11193 | |
11194 | // The type of the composite literal. | |
11195 | Type* type_; | |
11196 | // The depth within a list of composite literals within a composite | |
11197 | // literal, when the type is omitted. | |
11198 | int depth_; | |
11199 | // The values to put in the composite literal. | |
11200 | Expression_list* vals_; | |
11201 | // If this is true, then VALS_ is a list of pairs: a key and a | |
11202 | // value. In an array initializer, a missing key will be NULL. | |
11203 | bool has_keys_; | |
11204 | }; | |
11205 | ||
11206 | // Traversal. | |
11207 | ||
11208 | int | |
11209 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
11210 | { | |
11211 | if (this->vals_ != NULL | |
11212 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11213 | return TRAVERSE_EXIT; | |
11214 | return Type::traverse(this->type_, traverse); | |
11215 | } | |
11216 | ||
11217 | // Lower a generic composite literal into a specific version based on | |
11218 | // the type. | |
11219 | ||
11220 | Expression* | |
a287720d | 11221 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int) |
e440a328 | 11222 | { |
11223 | Type* type = this->type_; | |
11224 | ||
11225 | for (int depth = this->depth_; depth > 0; --depth) | |
11226 | { | |
11227 | if (type->array_type() != NULL) | |
11228 | type = type->array_type()->element_type(); | |
11229 | else if (type->map_type() != NULL) | |
11230 | type = type->map_type()->val_type(); | |
11231 | else | |
11232 | { | |
11233 | if (!type->is_error_type()) | |
11234 | error_at(this->location(), | |
11235 | ("may only omit types within composite literals " | |
11236 | "of slice, array, or map type")); | |
11237 | return Expression::make_error(this->location()); | |
11238 | } | |
11239 | } | |
11240 | ||
11241 | if (type->is_error_type()) | |
11242 | return Expression::make_error(this->location()); | |
11243 | else if (type->struct_type() != NULL) | |
11244 | return this->lower_struct(type); | |
11245 | else if (type->array_type() != NULL) | |
11246 | return this->lower_array(type); | |
11247 | else if (type->map_type() != NULL) | |
a287720d | 11248 | return this->lower_map(gogo, function, type); |
e440a328 | 11249 | else |
11250 | { | |
11251 | error_at(this->location(), | |
11252 | ("expected struct, slice, array, or map type " | |
11253 | "for composite literal")); | |
11254 | return Expression::make_error(this->location()); | |
11255 | } | |
11256 | } | |
11257 | ||
11258 | // Lower a struct composite literal. | |
11259 | ||
11260 | Expression* | |
11261 | Composite_literal_expression::lower_struct(Type* type) | |
11262 | { | |
11263 | source_location location = this->location(); | |
11264 | Struct_type* st = type->struct_type(); | |
11265 | if (this->vals_ == NULL || !this->has_keys_) | |
11266 | return new Struct_construction_expression(type, this->vals_, location); | |
11267 | ||
11268 | size_t field_count = st->field_count(); | |
11269 | std::vector<Expression*> vals(field_count); | |
11270 | Expression_list::const_iterator p = this->vals_->begin(); | |
11271 | while (p != this->vals_->end()) | |
11272 | { | |
11273 | Expression* name_expr = *p; | |
11274 | ||
11275 | ++p; | |
11276 | gcc_assert(p != this->vals_->end()); | |
11277 | Expression* val = *p; | |
11278 | ||
11279 | ++p; | |
11280 | ||
11281 | if (name_expr == NULL) | |
11282 | { | |
11283 | error_at(val->location(), "mixture of field and value initializers"); | |
11284 | return Expression::make_error(location); | |
11285 | } | |
11286 | ||
11287 | bool bad_key = false; | |
11288 | std::string name; | |
11289 | switch (name_expr->classification()) | |
11290 | { | |
11291 | case EXPRESSION_UNKNOWN_REFERENCE: | |
11292 | name = name_expr->unknown_expression()->name(); | |
11293 | break; | |
11294 | ||
11295 | case EXPRESSION_CONST_REFERENCE: | |
11296 | name = static_cast<Const_expression*>(name_expr)->name(); | |
11297 | break; | |
11298 | ||
11299 | case EXPRESSION_TYPE: | |
11300 | { | |
11301 | Type* t = name_expr->type(); | |
11302 | Named_type* nt = t->named_type(); | |
11303 | if (nt == NULL) | |
11304 | bad_key = true; | |
11305 | else | |
11306 | name = nt->name(); | |
11307 | } | |
11308 | break; | |
11309 | ||
11310 | case EXPRESSION_VAR_REFERENCE: | |
11311 | name = name_expr->var_expression()->name(); | |
11312 | break; | |
11313 | ||
11314 | case EXPRESSION_FUNC_REFERENCE: | |
11315 | name = name_expr->func_expression()->name(); | |
11316 | break; | |
11317 | ||
11318 | case EXPRESSION_UNARY: | |
11319 | // If there is a local variable around with the same name as | |
11320 | // the field, and this occurs in the closure, then the | |
11321 | // parser may turn the field reference into an indirection | |
11322 | // through the closure. FIXME: This is a mess. | |
11323 | { | |
11324 | bad_key = true; | |
11325 | Unary_expression* ue = static_cast<Unary_expression*>(name_expr); | |
11326 | if (ue->op() == OPERATOR_MULT) | |
11327 | { | |
11328 | Field_reference_expression* fre = | |
11329 | ue->operand()->field_reference_expression(); | |
11330 | if (fre != NULL) | |
11331 | { | |
11332 | Struct_type* st = | |
11333 | fre->expr()->type()->deref()->struct_type(); | |
11334 | if (st != NULL) | |
11335 | { | |
11336 | const Struct_field* sf = st->field(fre->field_index()); | |
11337 | name = sf->field_name(); | |
11338 | char buf[20]; | |
11339 | snprintf(buf, sizeof buf, "%u", fre->field_index()); | |
11340 | size_t buflen = strlen(buf); | |
11341 | if (name.compare(name.length() - buflen, buflen, buf) | |
11342 | == 0) | |
11343 | { | |
11344 | name = name.substr(0, name.length() - buflen); | |
11345 | bad_key = false; | |
11346 | } | |
11347 | } | |
11348 | } | |
11349 | } | |
11350 | } | |
11351 | break; | |
11352 | ||
11353 | default: | |
11354 | bad_key = true; | |
11355 | break; | |
11356 | } | |
11357 | if (bad_key) | |
11358 | { | |
11359 | error_at(name_expr->location(), "expected struct field name"); | |
11360 | return Expression::make_error(location); | |
11361 | } | |
11362 | ||
11363 | unsigned int index; | |
11364 | const Struct_field* sf = st->find_local_field(name, &index); | |
11365 | if (sf == NULL) | |
11366 | { | |
11367 | error_at(name_expr->location(), "unknown field %qs in %qs", | |
11368 | Gogo::message_name(name).c_str(), | |
11369 | (type->named_type() != NULL | |
11370 | ? type->named_type()->message_name().c_str() | |
11371 | : "unnamed struct")); | |
11372 | return Expression::make_error(location); | |
11373 | } | |
11374 | if (vals[index] != NULL) | |
11375 | { | |
11376 | error_at(name_expr->location(), | |
11377 | "duplicate value for field %qs in %qs", | |
11378 | Gogo::message_name(name).c_str(), | |
11379 | (type->named_type() != NULL | |
11380 | ? type->named_type()->message_name().c_str() | |
11381 | : "unnamed struct")); | |
11382 | return Expression::make_error(location); | |
11383 | } | |
11384 | ||
11385 | vals[index] = val; | |
11386 | } | |
11387 | ||
11388 | Expression_list* list = new Expression_list; | |
11389 | list->reserve(field_count); | |
11390 | for (size_t i = 0; i < field_count; ++i) | |
11391 | list->push_back(vals[i]); | |
11392 | ||
11393 | return new Struct_construction_expression(type, list, location); | |
11394 | } | |
11395 | ||
11396 | // Lower an array composite literal. | |
11397 | ||
11398 | Expression* | |
11399 | Composite_literal_expression::lower_array(Type* type) | |
11400 | { | |
11401 | source_location location = this->location(); | |
11402 | if (this->vals_ == NULL || !this->has_keys_) | |
11403 | return this->make_array(type, this->vals_); | |
11404 | ||
11405 | std::vector<Expression*> vals; | |
11406 | vals.reserve(this->vals_->size()); | |
11407 | unsigned long index = 0; | |
11408 | Expression_list::const_iterator p = this->vals_->begin(); | |
11409 | while (p != this->vals_->end()) | |
11410 | { | |
11411 | Expression* index_expr = *p; | |
11412 | ||
11413 | ++p; | |
11414 | gcc_assert(p != this->vals_->end()); | |
11415 | Expression* val = *p; | |
11416 | ||
11417 | ++p; | |
11418 | ||
11419 | if (index_expr != NULL) | |
11420 | { | |
11421 | mpz_t ival; | |
11422 | mpz_init(ival); | |
11423 | Type* dummy; | |
11424 | if (!index_expr->integer_constant_value(true, ival, &dummy)) | |
11425 | { | |
11426 | mpz_clear(ival); | |
11427 | error_at(index_expr->location(), | |
11428 | "index expression is not integer constant"); | |
11429 | return Expression::make_error(location); | |
11430 | } | |
11431 | if (mpz_sgn(ival) < 0) | |
11432 | { | |
11433 | mpz_clear(ival); | |
11434 | error_at(index_expr->location(), "index expression is negative"); | |
11435 | return Expression::make_error(location); | |
11436 | } | |
11437 | index = mpz_get_ui(ival); | |
11438 | if (mpz_cmp_ui(ival, index) != 0) | |
11439 | { | |
11440 | mpz_clear(ival); | |
11441 | error_at(index_expr->location(), "index value overflow"); | |
11442 | return Expression::make_error(location); | |
11443 | } | |
11444 | mpz_clear(ival); | |
11445 | } | |
11446 | ||
11447 | if (index == vals.size()) | |
11448 | vals.push_back(val); | |
11449 | else | |
11450 | { | |
11451 | if (index > vals.size()) | |
11452 | { | |
11453 | vals.reserve(index + 32); | |
11454 | vals.resize(index + 1, static_cast<Expression*>(NULL)); | |
11455 | } | |
11456 | if (vals[index] != NULL) | |
11457 | { | |
11458 | error_at((index_expr != NULL | |
11459 | ? index_expr->location() | |
11460 | : val->location()), | |
11461 | "duplicate value for index %lu", | |
11462 | index); | |
11463 | return Expression::make_error(location); | |
11464 | } | |
11465 | vals[index] = val; | |
11466 | } | |
11467 | ||
11468 | ++index; | |
11469 | } | |
11470 | ||
11471 | size_t size = vals.size(); | |
11472 | Expression_list* list = new Expression_list; | |
11473 | list->reserve(size); | |
11474 | for (size_t i = 0; i < size; ++i) | |
11475 | list->push_back(vals[i]); | |
11476 | ||
11477 | return this->make_array(type, list); | |
11478 | } | |
11479 | ||
11480 | // Actually build the array composite literal. This handles | |
11481 | // [...]{...}. | |
11482 | ||
11483 | Expression* | |
11484 | Composite_literal_expression::make_array(Type* type, Expression_list* vals) | |
11485 | { | |
11486 | source_location location = this->location(); | |
11487 | Array_type* at = type->array_type(); | |
11488 | if (at->length() != NULL && at->length()->is_nil_expression()) | |
11489 | { | |
11490 | size_t size = vals == NULL ? 0 : vals->size(); | |
11491 | mpz_t vlen; | |
11492 | mpz_init_set_ui(vlen, size); | |
11493 | Expression* elen = Expression::make_integer(&vlen, NULL, location); | |
11494 | mpz_clear(vlen); | |
11495 | at = Type::make_array_type(at->element_type(), elen); | |
11496 | type = at; | |
11497 | } | |
11498 | if (at->length() != NULL) | |
11499 | return new Fixed_array_construction_expression(type, vals, location); | |
11500 | else | |
11501 | return new Open_array_construction_expression(type, vals, location); | |
11502 | } | |
11503 | ||
11504 | // Lower a map composite literal. | |
11505 | ||
11506 | Expression* | |
a287720d | 11507 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
11508 | Type* type) | |
e440a328 | 11509 | { |
11510 | source_location location = this->location(); | |
11511 | if (this->vals_ != NULL) | |
11512 | { | |
11513 | if (!this->has_keys_) | |
11514 | { | |
11515 | error_at(location, "map composite literal must have keys"); | |
11516 | return Expression::make_error(location); | |
11517 | } | |
11518 | ||
a287720d | 11519 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 11520 | p != this->vals_->end(); |
11521 | p += 2) | |
11522 | { | |
11523 | if (*p == NULL) | |
11524 | { | |
11525 | ++p; | |
11526 | error_at((*p)->location(), | |
11527 | "map composite literal must have keys for every value"); | |
11528 | return Expression::make_error(location); | |
11529 | } | |
a287720d | 11530 | // Make sure we have lowered the key; it may not have been |
11531 | // lowered in order to handle keys for struct composite | |
11532 | // literals. Lower it now to get the right error message. | |
11533 | if ((*p)->unknown_expression() != NULL) | |
11534 | { | |
11535 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
11536 | gogo->lower_expression(function, &*p); | |
11537 | gcc_assert((*p)->is_error_expression()); | |
11538 | return Expression::make_error(location); | |
11539 | } | |
e440a328 | 11540 | } |
11541 | } | |
11542 | ||
11543 | return new Map_construction_expression(type, this->vals_, location); | |
11544 | } | |
11545 | ||
11546 | // Make a composite literal expression. | |
11547 | ||
11548 | Expression* | |
11549 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
11550 | Expression_list* vals, | |
11551 | source_location location) | |
11552 | { | |
11553 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
11554 | location); | |
11555 | } | |
11556 | ||
11557 | // Return whether this expression is a composite literal. | |
11558 | ||
11559 | bool | |
11560 | Expression::is_composite_literal() const | |
11561 | { | |
11562 | switch (this->classification_) | |
11563 | { | |
11564 | case EXPRESSION_COMPOSITE_LITERAL: | |
11565 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
11566 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
11567 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
11568 | case EXPRESSION_MAP_CONSTRUCTION: | |
11569 | return true; | |
11570 | default: | |
11571 | return false; | |
11572 | } | |
11573 | } | |
11574 | ||
11575 | // Return whether this expression is a composite literal which is not | |
11576 | // constant. | |
11577 | ||
11578 | bool | |
11579 | Expression::is_nonconstant_composite_literal() const | |
11580 | { | |
11581 | switch (this->classification_) | |
11582 | { | |
11583 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
11584 | { | |
11585 | const Struct_construction_expression *psce = | |
11586 | static_cast<const Struct_construction_expression*>(this); | |
11587 | return !psce->is_constant_struct(); | |
11588 | } | |
11589 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
11590 | { | |
11591 | const Fixed_array_construction_expression *pace = | |
11592 | static_cast<const Fixed_array_construction_expression*>(this); | |
11593 | return !pace->is_constant_array(); | |
11594 | } | |
11595 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
11596 | { | |
11597 | const Open_array_construction_expression *pace = | |
11598 | static_cast<const Open_array_construction_expression*>(this); | |
11599 | return !pace->is_constant_array(); | |
11600 | } | |
11601 | case EXPRESSION_MAP_CONSTRUCTION: | |
11602 | return true; | |
11603 | default: | |
11604 | return false; | |
11605 | } | |
11606 | } | |
11607 | ||
11608 | // Return true if this is a reference to a local variable. | |
11609 | ||
11610 | bool | |
11611 | Expression::is_local_variable() const | |
11612 | { | |
11613 | const Var_expression* ve = this->var_expression(); | |
11614 | if (ve == NULL) | |
11615 | return false; | |
11616 | const Named_object* no = ve->named_object(); | |
11617 | return (no->is_result_variable() | |
11618 | || (no->is_variable() && !no->var_value()->is_global())); | |
11619 | } | |
11620 | ||
11621 | // Class Type_guard_expression. | |
11622 | ||
11623 | // Traversal. | |
11624 | ||
11625 | int | |
11626 | Type_guard_expression::do_traverse(Traverse* traverse) | |
11627 | { | |
11628 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
11629 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
11630 | return TRAVERSE_EXIT; | |
11631 | return TRAVERSE_CONTINUE; | |
11632 | } | |
11633 | ||
11634 | // Check types of a type guard expression. The expression must have | |
11635 | // an interface type, but the actual type conversion is checked at run | |
11636 | // time. | |
11637 | ||
11638 | void | |
11639 | Type_guard_expression::do_check_types(Gogo*) | |
11640 | { | |
11641 | // 6g permits using a type guard with unsafe.pointer; we are | |
11642 | // compatible. | |
11643 | Type* expr_type = this->expr_->type(); | |
11644 | if (expr_type->is_unsafe_pointer_type()) | |
11645 | { | |
11646 | if (this->type_->points_to() == NULL | |
11647 | && (this->type_->integer_type() == NULL | |
11648 | || (this->type_->forwarded() | |
11649 | != Type::lookup_integer_type("uintptr")))) | |
11650 | this->report_error(_("invalid unsafe.Pointer conversion")); | |
11651 | } | |
11652 | else if (this->type_->is_unsafe_pointer_type()) | |
11653 | { | |
11654 | if (expr_type->points_to() == NULL | |
11655 | && (expr_type->integer_type() == NULL | |
11656 | || (expr_type->forwarded() | |
11657 | != Type::lookup_integer_type("uintptr")))) | |
11658 | this->report_error(_("invalid unsafe.Pointer conversion")); | |
11659 | } | |
11660 | else if (expr_type->interface_type() == NULL) | |
f725ade8 | 11661 | { |
11662 | if (!expr_type->is_error_type() && !this->type_->is_error_type()) | |
11663 | this->report_error(_("type assertion only valid for interface types")); | |
11664 | this->set_is_error(); | |
11665 | } | |
e440a328 | 11666 | else if (this->type_->interface_type() == NULL) |
11667 | { | |
11668 | std::string reason; | |
11669 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
11670 | &reason)) | |
11671 | { | |
f725ade8 | 11672 | if (!this->type_->is_error_type()) |
e440a328 | 11673 | { |
f725ade8 | 11674 | if (reason.empty()) |
11675 | this->report_error(_("impossible type assertion: " | |
11676 | "type does not implement interface")); | |
11677 | else | |
11678 | error_at(this->location(), | |
11679 | ("impossible type assertion: " | |
11680 | "type does not implement interface (%s)"), | |
11681 | reason.c_str()); | |
e440a328 | 11682 | } |
f725ade8 | 11683 | this->set_is_error(); |
e440a328 | 11684 | } |
11685 | } | |
11686 | } | |
11687 | ||
11688 | // Return a tree for a type guard expression. | |
11689 | ||
11690 | tree | |
11691 | Type_guard_expression::do_get_tree(Translate_context* context) | |
11692 | { | |
11693 | Gogo* gogo = context->gogo(); | |
11694 | tree expr_tree = this->expr_->get_tree(context); | |
11695 | if (expr_tree == error_mark_node) | |
11696 | return error_mark_node; | |
11697 | Type* expr_type = this->expr_->type(); | |
11698 | if ((this->type_->is_unsafe_pointer_type() | |
11699 | && (expr_type->points_to() != NULL | |
11700 | || expr_type->integer_type() != NULL)) | |
11701 | || (expr_type->is_unsafe_pointer_type() | |
11702 | && this->type_->points_to() != NULL)) | |
11703 | return convert_to_pointer(this->type_->get_tree(gogo), expr_tree); | |
11704 | else if (expr_type->is_unsafe_pointer_type() | |
11705 | && this->type_->integer_type() != NULL) | |
11706 | return convert_to_integer(this->type_->get_tree(gogo), expr_tree); | |
11707 | else if (this->type_->interface_type() != NULL) | |
11708 | return Expression::convert_interface_to_interface(context, this->type_, | |
11709 | this->expr_->type(), | |
11710 | expr_tree, true, | |
11711 | this->location()); | |
11712 | else | |
11713 | return Expression::convert_for_assignment(context, this->type_, | |
11714 | this->expr_->type(), expr_tree, | |
11715 | this->location()); | |
11716 | } | |
11717 | ||
11718 | // Make a type guard expression. | |
11719 | ||
11720 | Expression* | |
11721 | Expression::make_type_guard(Expression* expr, Type* type, | |
11722 | source_location location) | |
11723 | { | |
11724 | return new Type_guard_expression(expr, type, location); | |
11725 | } | |
11726 | ||
11727 | // Class Heap_composite_expression. | |
11728 | ||
11729 | // When you take the address of a composite literal, it is allocated | |
11730 | // on the heap. This class implements that. | |
11731 | ||
11732 | class Heap_composite_expression : public Expression | |
11733 | { | |
11734 | public: | |
11735 | Heap_composite_expression(Expression* expr, source_location location) | |
11736 | : Expression(EXPRESSION_HEAP_COMPOSITE, location), | |
11737 | expr_(expr) | |
11738 | { } | |
11739 | ||
11740 | protected: | |
11741 | int | |
11742 | do_traverse(Traverse* traverse) | |
11743 | { return Expression::traverse(&this->expr_, traverse); } | |
11744 | ||
11745 | Type* | |
11746 | do_type() | |
11747 | { return Type::make_pointer_type(this->expr_->type()); } | |
11748 | ||
11749 | void | |
11750 | do_determine_type(const Type_context*) | |
11751 | { this->expr_->determine_type_no_context(); } | |
11752 | ||
11753 | Expression* | |
11754 | do_copy() | |
11755 | { | |
11756 | return Expression::make_heap_composite(this->expr_->copy(), | |
11757 | this->location()); | |
11758 | } | |
11759 | ||
11760 | tree | |
11761 | do_get_tree(Translate_context*); | |
11762 | ||
11763 | // We only export global objects, and the parser does not generate | |
11764 | // this in global scope. | |
11765 | void | |
11766 | do_export(Export*) const | |
11767 | { gcc_unreachable(); } | |
11768 | ||
11769 | private: | |
11770 | // The composite literal which is being put on the heap. | |
11771 | Expression* expr_; | |
11772 | }; | |
11773 | ||
11774 | // Return a tree which allocates a composite literal on the heap. | |
11775 | ||
11776 | tree | |
11777 | Heap_composite_expression::do_get_tree(Translate_context* context) | |
11778 | { | |
11779 | tree expr_tree = this->expr_->get_tree(context); | |
11780 | if (expr_tree == error_mark_node) | |
11781 | return error_mark_node; | |
11782 | tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree)); | |
11783 | gcc_assert(TREE_CODE(expr_size) == INTEGER_CST); | |
11784 | tree space = context->gogo()->allocate_memory(this->expr_->type(), | |
11785 | expr_size, this->location()); | |
11786 | space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space); | |
11787 | space = save_expr(space); | |
11788 | tree ref = build_fold_indirect_ref_loc(this->location(), space); | |
11789 | TREE_THIS_NOTRAP(ref) = 1; | |
11790 | tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space), | |
11791 | build2(MODIFY_EXPR, void_type_node, ref, expr_tree), | |
11792 | space); | |
11793 | SET_EXPR_LOCATION(ret, this->location()); | |
11794 | return ret; | |
11795 | } | |
11796 | ||
11797 | // Allocate a composite literal on the heap. | |
11798 | ||
11799 | Expression* | |
11800 | Expression::make_heap_composite(Expression* expr, source_location location) | |
11801 | { | |
11802 | return new Heap_composite_expression(expr, location); | |
11803 | } | |
11804 | ||
11805 | // Class Receive_expression. | |
11806 | ||
11807 | // Return the type of a receive expression. | |
11808 | ||
11809 | Type* | |
11810 | Receive_expression::do_type() | |
11811 | { | |
11812 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
11813 | if (channel_type == NULL) | |
11814 | return Type::make_error_type(); | |
11815 | return channel_type->element_type(); | |
11816 | } | |
11817 | ||
11818 | // Check types for a receive expression. | |
11819 | ||
11820 | void | |
11821 | Receive_expression::do_check_types(Gogo*) | |
11822 | { | |
11823 | Type* type = this->channel_->type(); | |
11824 | if (type->is_error_type()) | |
11825 | { | |
11826 | this->set_is_error(); | |
11827 | return; | |
11828 | } | |
11829 | if (type->channel_type() == NULL) | |
11830 | { | |
11831 | this->report_error(_("expected channel")); | |
11832 | return; | |
11833 | } | |
11834 | if (!type->channel_type()->may_receive()) | |
11835 | { | |
11836 | this->report_error(_("invalid receive on send-only channel")); | |
11837 | return; | |
11838 | } | |
11839 | } | |
11840 | ||
11841 | // Get a tree for a receive expression. | |
11842 | ||
11843 | tree | |
11844 | Receive_expression::do_get_tree(Translate_context* context) | |
11845 | { | |
11846 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
11847 | gcc_assert(channel_type != NULL); | |
11848 | Type* element_type = channel_type->element_type(); | |
11849 | tree element_type_tree = element_type->get_tree(context->gogo()); | |
11850 | ||
11851 | tree channel = this->channel_->get_tree(context); | |
11852 | if (element_type_tree == error_mark_node || channel == error_mark_node) | |
11853 | return error_mark_node; | |
11854 | ||
11855 | return Gogo::receive_from_channel(element_type_tree, channel, | |
11856 | this->for_select_, this->location()); | |
11857 | } | |
11858 | ||
11859 | // Make a receive expression. | |
11860 | ||
11861 | Receive_expression* | |
11862 | Expression::make_receive(Expression* channel, source_location location) | |
11863 | { | |
11864 | return new Receive_expression(channel, location); | |
11865 | } | |
11866 | ||
11867 | // Class Send_expression. | |
11868 | ||
11869 | // Traversal. | |
11870 | ||
11871 | int | |
11872 | Send_expression::do_traverse(Traverse* traverse) | |
11873 | { | |
11874 | if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT) | |
11875 | return TRAVERSE_EXIT; | |
11876 | return Expression::traverse(&this->val_, traverse); | |
11877 | } | |
11878 | ||
11879 | // Get the type. | |
11880 | ||
11881 | Type* | |
11882 | Send_expression::do_type() | |
11883 | { | |
11884 | return Type::lookup_bool_type(); | |
11885 | } | |
11886 | ||
11887 | // Set types. | |
11888 | ||
11889 | void | |
11890 | Send_expression::do_determine_type(const Type_context*) | |
11891 | { | |
11892 | this->channel_->determine_type_no_context(); | |
11893 | ||
11894 | Type* type = this->channel_->type(); | |
11895 | Type_context subcontext; | |
11896 | if (type->channel_type() != NULL) | |
11897 | subcontext.type = type->channel_type()->element_type(); | |
11898 | this->val_->determine_type(&subcontext); | |
11899 | } | |
11900 | ||
11901 | // Check types. | |
11902 | ||
11903 | void | |
11904 | Send_expression::do_check_types(Gogo*) | |
11905 | { | |
11906 | Type* type = this->channel_->type(); | |
11907 | if (type->is_error_type()) | |
11908 | { | |
11909 | this->set_is_error(); | |
11910 | return; | |
11911 | } | |
11912 | Channel_type* channel_type = type->channel_type(); | |
11913 | if (channel_type == NULL) | |
11914 | { | |
11915 | error_at(this->location(), "left operand of %<<-%> must be channel"); | |
11916 | this->set_is_error(); | |
11917 | return; | |
11918 | } | |
11919 | Type* element_type = channel_type->element_type(); | |
11920 | if (element_type != NULL | |
11921 | && !Type::are_assignable(element_type, this->val_->type(), NULL)) | |
11922 | { | |
11923 | this->report_error(_("incompatible types in send")); | |
11924 | return; | |
11925 | } | |
11926 | if (!channel_type->may_send()) | |
11927 | { | |
11928 | this->report_error(_("invalid send on receive-only channel")); | |
11929 | return; | |
11930 | } | |
11931 | } | |
11932 | ||
11933 | // Get a tree for a send expression. | |
11934 | ||
11935 | tree | |
11936 | Send_expression::do_get_tree(Translate_context* context) | |
11937 | { | |
11938 | tree channel = this->channel_->get_tree(context); | |
11939 | tree val = this->val_->get_tree(context); | |
11940 | if (channel == error_mark_node || val == error_mark_node) | |
11941 | return error_mark_node; | |
11942 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
11943 | val = Expression::convert_for_assignment(context, | |
11944 | channel_type->element_type(), | |
11945 | this->val_->type(), | |
11946 | val, | |
11947 | this->location()); | |
11948 | return Gogo::send_on_channel(channel, val, this->is_value_discarded_, | |
11949 | this->for_select_, this->location()); | |
11950 | } | |
11951 | ||
11952 | // Make a send expression | |
11953 | ||
11954 | Send_expression* | |
11955 | Expression::make_send(Expression* channel, Expression* val, | |
11956 | source_location location) | |
11957 | { | |
11958 | return new Send_expression(channel, val, location); | |
11959 | } | |
11960 | ||
11961 | // An expression which evaluates to a pointer to the type descriptor | |
11962 | // of a type. | |
11963 | ||
11964 | class Type_descriptor_expression : public Expression | |
11965 | { | |
11966 | public: | |
11967 | Type_descriptor_expression(Type* type, source_location location) | |
11968 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), | |
11969 | type_(type) | |
11970 | { } | |
11971 | ||
11972 | protected: | |
11973 | Type* | |
11974 | do_type() | |
11975 | { return Type::make_type_descriptor_ptr_type(); } | |
11976 | ||
11977 | void | |
11978 | do_determine_type(const Type_context*) | |
11979 | { } | |
11980 | ||
11981 | Expression* | |
11982 | do_copy() | |
11983 | { return this; } | |
11984 | ||
11985 | tree | |
11986 | do_get_tree(Translate_context* context) | |
11987 | { return this->type_->type_descriptor_pointer(context->gogo()); } | |
11988 | ||
11989 | private: | |
11990 | // The type for which this is the descriptor. | |
11991 | Type* type_; | |
11992 | }; | |
11993 | ||
11994 | // Make a type descriptor expression. | |
11995 | ||
11996 | Expression* | |
11997 | Expression::make_type_descriptor(Type* type, source_location location) | |
11998 | { | |
11999 | return new Type_descriptor_expression(type, location); | |
12000 | } | |
12001 | ||
12002 | // An expression which evaluates to some characteristic of a type. | |
12003 | // This is only used to initialize fields of a type descriptor. Using | |
12004 | // a new expression class is slightly inefficient but gives us a good | |
12005 | // separation between the frontend and the middle-end with regard to | |
12006 | // how types are laid out. | |
12007 | ||
12008 | class Type_info_expression : public Expression | |
12009 | { | |
12010 | public: | |
12011 | Type_info_expression(Type* type, Type_info type_info) | |
12012 | : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION), | |
12013 | type_(type), type_info_(type_info) | |
12014 | { } | |
12015 | ||
12016 | protected: | |
12017 | Type* | |
12018 | do_type(); | |
12019 | ||
12020 | void | |
12021 | do_determine_type(const Type_context*) | |
12022 | { } | |
12023 | ||
12024 | Expression* | |
12025 | do_copy() | |
12026 | { return this; } | |
12027 | ||
12028 | tree | |
12029 | do_get_tree(Translate_context* context); | |
12030 | ||
12031 | private: | |
12032 | // The type for which we are getting information. | |
12033 | Type* type_; | |
12034 | // What information we want. | |
12035 | Type_info type_info_; | |
12036 | }; | |
12037 | ||
12038 | // The type is chosen to match what the type descriptor struct | |
12039 | // expects. | |
12040 | ||
12041 | Type* | |
12042 | Type_info_expression::do_type() | |
12043 | { | |
12044 | switch (this->type_info_) | |
12045 | { | |
12046 | case TYPE_INFO_SIZE: | |
12047 | return Type::lookup_integer_type("uintptr"); | |
12048 | case TYPE_INFO_ALIGNMENT: | |
12049 | case TYPE_INFO_FIELD_ALIGNMENT: | |
12050 | return Type::lookup_integer_type("uint8"); | |
12051 | default: | |
12052 | gcc_unreachable(); | |
12053 | } | |
12054 | } | |
12055 | ||
12056 | // Return type information in GENERIC. | |
12057 | ||
12058 | tree | |
12059 | Type_info_expression::do_get_tree(Translate_context* context) | |
12060 | { | |
12061 | tree type_tree = this->type_->get_tree(context->gogo()); | |
12062 | if (type_tree == error_mark_node) | |
12063 | return error_mark_node; | |
12064 | ||
12065 | tree val_type_tree = this->type()->get_tree(context->gogo()); | |
12066 | gcc_assert(val_type_tree != error_mark_node); | |
12067 | ||
12068 | if (this->type_info_ == TYPE_INFO_SIZE) | |
12069 | return fold_convert_loc(BUILTINS_LOCATION, val_type_tree, | |
12070 | TYPE_SIZE_UNIT(type_tree)); | |
12071 | else | |
12072 | { | |
637bd3af | 12073 | unsigned int val; |
e440a328 | 12074 | if (this->type_info_ == TYPE_INFO_ALIGNMENT) |
637bd3af | 12075 | val = go_type_alignment(type_tree); |
e440a328 | 12076 | else |
637bd3af | 12077 | val = go_field_alignment(type_tree); |
e440a328 | 12078 | return build_int_cstu(val_type_tree, val); |
12079 | } | |
12080 | } | |
12081 | ||
12082 | // Make a type info expression. | |
12083 | ||
12084 | Expression* | |
12085 | Expression::make_type_info(Type* type, Type_info type_info) | |
12086 | { | |
12087 | return new Type_info_expression(type, type_info); | |
12088 | } | |
12089 | ||
12090 | // An expression which evaluates to the offset of a field within a | |
12091 | // struct. This, like Type_info_expression, q.v., is only used to | |
12092 | // initialize fields of a type descriptor. | |
12093 | ||
12094 | class Struct_field_offset_expression : public Expression | |
12095 | { | |
12096 | public: | |
12097 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
12098 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION), | |
12099 | type_(type), field_(field) | |
12100 | { } | |
12101 | ||
12102 | protected: | |
12103 | Type* | |
12104 | do_type() | |
12105 | { return Type::lookup_integer_type("uintptr"); } | |
12106 | ||
12107 | void | |
12108 | do_determine_type(const Type_context*) | |
12109 | { } | |
12110 | ||
12111 | Expression* | |
12112 | do_copy() | |
12113 | { return this; } | |
12114 | ||
12115 | tree | |
12116 | do_get_tree(Translate_context* context); | |
12117 | ||
12118 | private: | |
12119 | // The type of the struct. | |
12120 | Struct_type* type_; | |
12121 | // The field. | |
12122 | const Struct_field* field_; | |
12123 | }; | |
12124 | ||
12125 | // Return a struct field offset in GENERIC. | |
12126 | ||
12127 | tree | |
12128 | Struct_field_offset_expression::do_get_tree(Translate_context* context) | |
12129 | { | |
12130 | tree type_tree = this->type_->get_tree(context->gogo()); | |
12131 | if (type_tree == error_mark_node) | |
12132 | return error_mark_node; | |
12133 | ||
12134 | tree val_type_tree = this->type()->get_tree(context->gogo()); | |
12135 | gcc_assert(val_type_tree != error_mark_node); | |
12136 | ||
12137 | const Struct_field_list* fields = this->type_->fields(); | |
12138 | tree struct_field_tree = TYPE_FIELDS(type_tree); | |
12139 | Struct_field_list::const_iterator p; | |
12140 | for (p = fields->begin(); | |
12141 | p != fields->end(); | |
12142 | ++p, struct_field_tree = DECL_CHAIN(struct_field_tree)) | |
12143 | { | |
12144 | gcc_assert(struct_field_tree != NULL_TREE); | |
12145 | if (&*p == this->field_) | |
12146 | break; | |
12147 | } | |
12148 | gcc_assert(&*p == this->field_); | |
12149 | ||
12150 | return fold_convert_loc(BUILTINS_LOCATION, val_type_tree, | |
12151 | byte_position(struct_field_tree)); | |
12152 | } | |
12153 | ||
12154 | // Make an expression for a struct field offset. | |
12155 | ||
12156 | Expression* | |
12157 | Expression::make_struct_field_offset(Struct_type* type, | |
12158 | const Struct_field* field) | |
12159 | { | |
12160 | return new Struct_field_offset_expression(type, field); | |
12161 | } | |
12162 | ||
12163 | // An expression which evaluates to the address of an unnamed label. | |
12164 | ||
12165 | class Label_addr_expression : public Expression | |
12166 | { | |
12167 | public: | |
12168 | Label_addr_expression(Label* label, source_location location) | |
12169 | : Expression(EXPRESSION_LABEL_ADDR, location), | |
12170 | label_(label) | |
12171 | { } | |
12172 | ||
12173 | protected: | |
12174 | Type* | |
12175 | do_type() | |
12176 | { return Type::make_pointer_type(Type::make_void_type()); } | |
12177 | ||
12178 | void | |
12179 | do_determine_type(const Type_context*) | |
12180 | { } | |
12181 | ||
12182 | Expression* | |
12183 | do_copy() | |
12184 | { return new Label_addr_expression(this->label_, this->location()); } | |
12185 | ||
12186 | tree | |
12187 | do_get_tree(Translate_context*) | |
12188 | { return this->label_->get_addr(this->location()); } | |
12189 | ||
12190 | private: | |
12191 | // The label whose address we are taking. | |
12192 | Label* label_; | |
12193 | }; | |
12194 | ||
12195 | // Make an expression for the address of an unnamed label. | |
12196 | ||
12197 | Expression* | |
12198 | Expression::make_label_addr(Label* label, source_location location) | |
12199 | { | |
12200 | return new Label_addr_expression(label, location); | |
12201 | } | |
12202 | ||
12203 | // Import an expression. This comes at the end in order to see the | |
12204 | // various class definitions. | |
12205 | ||
12206 | Expression* | |
12207 | Expression::import_expression(Import* imp) | |
12208 | { | |
12209 | int c = imp->peek_char(); | |
12210 | if (imp->match_c_string("- ") | |
12211 | || imp->match_c_string("! ") | |
12212 | || imp->match_c_string("^ ")) | |
12213 | return Unary_expression::do_import(imp); | |
12214 | else if (c == '(') | |
12215 | return Binary_expression::do_import(imp); | |
12216 | else if (imp->match_c_string("true") | |
12217 | || imp->match_c_string("false")) | |
12218 | return Boolean_expression::do_import(imp); | |
12219 | else if (c == '"') | |
12220 | return String_expression::do_import(imp); | |
12221 | else if (c == '-' || (c >= '0' && c <= '9')) | |
12222 | { | |
12223 | // This handles integers, floats and complex constants. | |
12224 | return Integer_expression::do_import(imp); | |
12225 | } | |
12226 | else if (imp->match_c_string("nil")) | |
12227 | return Nil_expression::do_import(imp); | |
12228 | else if (imp->match_c_string("convert")) | |
12229 | return Type_conversion_expression::do_import(imp); | |
12230 | else | |
12231 | { | |
12232 | error_at(imp->location(), "import error: expected expression"); | |
12233 | return Expression::make_error(imp->location()); | |
12234 | } | |
12235 | } | |
12236 | ||
12237 | // Class Expression_list. | |
12238 | ||
12239 | // Traverse the list. | |
12240 | ||
12241 | int | |
12242 | Expression_list::traverse(Traverse* traverse) | |
12243 | { | |
12244 | for (Expression_list::iterator p = this->begin(); | |
12245 | p != this->end(); | |
12246 | ++p) | |
12247 | { | |
12248 | if (*p != NULL) | |
12249 | { | |
12250 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
12251 | return TRAVERSE_EXIT; | |
12252 | } | |
12253 | } | |
12254 | return TRAVERSE_CONTINUE; | |
12255 | } | |
12256 | ||
12257 | // Copy the list. | |
12258 | ||
12259 | Expression_list* | |
12260 | Expression_list::copy() | |
12261 | { | |
12262 | Expression_list* ret = new Expression_list(); | |
12263 | for (Expression_list::iterator p = this->begin(); | |
12264 | p != this->end(); | |
12265 | ++p) | |
12266 | { | |
12267 | if (*p == NULL) | |
12268 | ret->push_back(NULL); | |
12269 | else | |
12270 | ret->push_back((*p)->copy()); | |
12271 | } | |
12272 | return ret; | |
12273 | } | |
12274 | ||
12275 | // Return whether an expression list has an error expression. | |
12276 | ||
12277 | bool | |
12278 | Expression_list::contains_error() const | |
12279 | { | |
12280 | for (Expression_list::const_iterator p = this->begin(); | |
12281 | p != this->end(); | |
12282 | ++p) | |
12283 | if (*p != NULL && (*p)->is_error_expression()) | |
12284 | return true; | |
12285 | return false; | |
12286 | } |