]>
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" | |
6e193e6f | 36 | #include "backend.h" |
e440a328 | 37 | #include "expressions.h" |
38 | ||
39 | // Class Expression. | |
40 | ||
41 | Expression::Expression(Expression_classification classification, | |
42 | source_location location) | |
43 | : classification_(classification), location_(location) | |
44 | { | |
45 | } | |
46 | ||
47 | Expression::~Expression() | |
48 | { | |
49 | } | |
50 | ||
51 | // If this expression has a constant integer value, return it. | |
52 | ||
53 | bool | |
54 | Expression::integer_constant_value(bool iota_is_constant, mpz_t val, | |
55 | Type** ptype) const | |
56 | { | |
57 | *ptype = NULL; | |
58 | return this->do_integer_constant_value(iota_is_constant, val, ptype); | |
59 | } | |
60 | ||
61 | // If this expression has a constant floating point value, return it. | |
62 | ||
63 | bool | |
64 | Expression::float_constant_value(mpfr_t val, Type** ptype) const | |
65 | { | |
66 | *ptype = NULL; | |
67 | if (this->do_float_constant_value(val, ptype)) | |
68 | return true; | |
69 | mpz_t ival; | |
70 | mpz_init(ival); | |
71 | Type* t; | |
72 | bool ret; | |
73 | if (!this->do_integer_constant_value(false, ival, &t)) | |
74 | ret = false; | |
75 | else | |
76 | { | |
77 | mpfr_set_z(val, ival, GMP_RNDN); | |
78 | ret = true; | |
79 | } | |
80 | mpz_clear(ival); | |
81 | return ret; | |
82 | } | |
83 | ||
84 | // If this expression has a constant complex value, return it. | |
85 | ||
86 | bool | |
87 | Expression::complex_constant_value(mpfr_t real, mpfr_t imag, | |
88 | Type** ptype) const | |
89 | { | |
90 | *ptype = NULL; | |
91 | if (this->do_complex_constant_value(real, imag, ptype)) | |
92 | return true; | |
93 | Type *t; | |
94 | if (this->float_constant_value(real, &t)) | |
95 | { | |
96 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
97 | return true; | |
98 | } | |
99 | return false; | |
100 | } | |
101 | ||
102 | // Traverse the expressions. | |
103 | ||
104 | int | |
105 | Expression::traverse(Expression** pexpr, Traverse* traverse) | |
106 | { | |
107 | Expression* expr = *pexpr; | |
108 | if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0) | |
109 | { | |
110 | int t = traverse->expression(pexpr); | |
111 | if (t == TRAVERSE_EXIT) | |
112 | return TRAVERSE_EXIT; | |
113 | else if (t == TRAVERSE_SKIP_COMPONENTS) | |
114 | return TRAVERSE_CONTINUE; | |
115 | } | |
116 | return expr->do_traverse(traverse); | |
117 | } | |
118 | ||
119 | // Traverse subexpressions of this expression. | |
120 | ||
121 | int | |
122 | Expression::traverse_subexpressions(Traverse* traverse) | |
123 | { | |
124 | return this->do_traverse(traverse); | |
125 | } | |
126 | ||
127 | // Default implementation for do_traverse for child classes. | |
128 | ||
129 | int | |
130 | Expression::do_traverse(Traverse*) | |
131 | { | |
132 | return TRAVERSE_CONTINUE; | |
133 | } | |
134 | ||
135 | // This virtual function is called by the parser if the value of this | |
136 | // expression is being discarded. By default, we warn. Expressions | |
137 | // with side effects override. | |
138 | ||
139 | void | |
140 | Expression::do_discarding_value() | |
141 | { | |
142 | this->warn_about_unused_value(); | |
143 | } | |
144 | ||
145 | // This virtual function is called to export expressions. This will | |
146 | // only be used by expressions which may be constant. | |
147 | ||
148 | void | |
149 | Expression::do_export(Export*) const | |
150 | { | |
151 | gcc_unreachable(); | |
152 | } | |
153 | ||
154 | // Warn that the value of the expression is not used. | |
155 | ||
156 | void | |
157 | Expression::warn_about_unused_value() | |
158 | { | |
159 | warning_at(this->location(), OPT_Wunused_value, "value computed is not used"); | |
160 | } | |
161 | ||
162 | // Note that this expression is an error. This is called by children | |
163 | // when they discover an error. | |
164 | ||
165 | void | |
166 | Expression::set_is_error() | |
167 | { | |
168 | this->classification_ = EXPRESSION_ERROR; | |
169 | } | |
170 | ||
171 | // For children to call to report an error conveniently. | |
172 | ||
173 | void | |
174 | Expression::report_error(const char* msg) | |
175 | { | |
176 | error_at(this->location_, "%s", msg); | |
177 | this->set_is_error(); | |
178 | } | |
179 | ||
180 | // Set types of variables and constants. This is implemented by the | |
181 | // child class. | |
182 | ||
183 | void | |
184 | Expression::determine_type(const Type_context* context) | |
185 | { | |
186 | this->do_determine_type(context); | |
187 | } | |
188 | ||
189 | // Set types when there is no context. | |
190 | ||
191 | void | |
192 | Expression::determine_type_no_context() | |
193 | { | |
194 | Type_context context; | |
195 | this->do_determine_type(&context); | |
196 | } | |
197 | ||
198 | // Return a tree handling any conversions which must be done during | |
199 | // assignment. | |
200 | ||
201 | tree | |
202 | Expression::convert_for_assignment(Translate_context* context, Type* lhs_type, | |
203 | Type* rhs_type, tree rhs_tree, | |
204 | source_location location) | |
205 | { | |
206 | if (lhs_type == rhs_type) | |
207 | return rhs_tree; | |
208 | ||
5c13bd80 | 209 | if (lhs_type->is_error() || rhs_type->is_error()) |
e440a328 | 210 | return error_mark_node; |
211 | ||
e440a328 | 212 | if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node) |
213 | return error_mark_node; | |
214 | ||
215 | Gogo* gogo = context->gogo(); | |
216 | ||
217 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
218 | if (lhs_type_tree == error_mark_node) | |
219 | return error_mark_node; | |
220 | ||
221 | if (lhs_type->interface_type() != NULL) | |
222 | { | |
223 | if (rhs_type->interface_type() == NULL) | |
224 | return Expression::convert_type_to_interface(context, lhs_type, | |
225 | rhs_type, rhs_tree, | |
226 | location); | |
227 | else | |
228 | return Expression::convert_interface_to_interface(context, lhs_type, | |
229 | rhs_type, rhs_tree, | |
230 | false, location); | |
231 | } | |
232 | else if (rhs_type->interface_type() != NULL) | |
233 | return Expression::convert_interface_to_type(context, lhs_type, rhs_type, | |
234 | rhs_tree, location); | |
235 | else if (lhs_type->is_open_array_type() | |
236 | && rhs_type->is_nil_type()) | |
237 | { | |
238 | // Assigning nil to an open array. | |
239 | gcc_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE); | |
240 | ||
241 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
242 | ||
243 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
244 | tree field = TYPE_FIELDS(lhs_type_tree); | |
245 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
246 | "__values") == 0); | |
247 | elt->index = field; | |
248 | elt->value = fold_convert(TREE_TYPE(field), null_pointer_node); | |
249 | ||
250 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
251 | field = DECL_CHAIN(field); | |
252 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
253 | "__count") == 0); | |
254 | elt->index = field; | |
255 | elt->value = fold_convert(TREE_TYPE(field), integer_zero_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 | "__capacity") == 0); | |
261 | elt->index = field; | |
262 | elt->value = fold_convert(TREE_TYPE(field), integer_zero_node); | |
263 | ||
264 | tree val = build_constructor(lhs_type_tree, init); | |
265 | TREE_CONSTANT(val) = 1; | |
266 | ||
267 | return val; | |
268 | } | |
269 | else if (rhs_type->is_nil_type()) | |
270 | { | |
271 | // The left hand side should be a pointer type at the tree | |
272 | // level. | |
273 | gcc_assert(POINTER_TYPE_P(lhs_type_tree)); | |
274 | return fold_convert(lhs_type_tree, null_pointer_node); | |
275 | } | |
276 | else if (lhs_type_tree == TREE_TYPE(rhs_tree)) | |
277 | { | |
278 | // No conversion is needed. | |
279 | return rhs_tree; | |
280 | } | |
281 | else if (POINTER_TYPE_P(lhs_type_tree) | |
282 | || INTEGRAL_TYPE_P(lhs_type_tree) | |
283 | || SCALAR_FLOAT_TYPE_P(lhs_type_tree) | |
284 | || COMPLEX_FLOAT_TYPE_P(lhs_type_tree)) | |
285 | return fold_convert_loc(location, lhs_type_tree, rhs_tree); | |
286 | else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE | |
287 | && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE) | |
288 | { | |
289 | // This conversion must be permitted by Go, or we wouldn't have | |
290 | // gotten here. | |
291 | gcc_assert(int_size_in_bytes(lhs_type_tree) | |
292 | == int_size_in_bytes(TREE_TYPE(rhs_tree))); | |
293 | return fold_build1_loc(location, VIEW_CONVERT_EXPR, lhs_type_tree, | |
294 | rhs_tree); | |
295 | } | |
296 | else | |
297 | { | |
298 | gcc_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree))); | |
299 | return rhs_tree; | |
300 | } | |
301 | } | |
302 | ||
303 | // Return a tree for a conversion from a non-interface type to an | |
304 | // interface type. | |
305 | ||
306 | tree | |
307 | Expression::convert_type_to_interface(Translate_context* context, | |
308 | Type* lhs_type, Type* rhs_type, | |
309 | tree rhs_tree, source_location location) | |
310 | { | |
311 | Gogo* gogo = context->gogo(); | |
312 | Interface_type* lhs_interface_type = lhs_type->interface_type(); | |
313 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
314 | ||
315 | // Since RHS_TYPE is a static type, we can create the interface | |
316 | // method table at compile time. | |
317 | ||
318 | // When setting an interface to nil, we just set both fields to | |
319 | // NULL. | |
320 | if (rhs_type->is_nil_type()) | |
321 | return lhs_type->get_init_tree(gogo, false); | |
322 | ||
323 | // This should have been checked already. | |
324 | gcc_assert(lhs_interface_type->implements_interface(rhs_type, NULL)); | |
325 | ||
326 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
327 | if (lhs_type_tree == error_mark_node) | |
328 | return error_mark_node; | |
329 | ||
330 | // An interface is a tuple. If LHS_TYPE is an empty interface type, | |
331 | // then the first field is the type descriptor for RHS_TYPE. | |
332 | // Otherwise it is the interface method table for RHS_TYPE. | |
333 | tree first_field_value; | |
334 | if (lhs_is_empty) | |
335 | first_field_value = rhs_type->type_descriptor_pointer(gogo); | |
336 | else | |
337 | { | |
338 | // Build the interface method table for this interface and this | |
339 | // object type: a list of function pointers for each interface | |
340 | // method. | |
341 | Named_type* rhs_named_type = rhs_type->named_type(); | |
342 | bool is_pointer = false; | |
343 | if (rhs_named_type == NULL) | |
344 | { | |
345 | rhs_named_type = rhs_type->deref()->named_type(); | |
346 | is_pointer = true; | |
347 | } | |
348 | tree method_table; | |
349 | if (rhs_named_type == NULL) | |
350 | method_table = null_pointer_node; | |
351 | else | |
352 | method_table = | |
353 | rhs_named_type->interface_method_table(gogo, lhs_interface_type, | |
354 | is_pointer); | |
355 | first_field_value = fold_convert_loc(location, const_ptr_type_node, | |
356 | method_table); | |
357 | } | |
84b7d3c6 | 358 | if (first_field_value == error_mark_node) |
359 | return error_mark_node; | |
e440a328 | 360 | |
361 | // Start building a constructor for the value we will return. | |
362 | ||
363 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2); | |
364 | ||
365 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
366 | tree field = TYPE_FIELDS(lhs_type_tree); | |
367 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
368 | (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0); | |
369 | elt->index = field; | |
370 | elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value); | |
371 | ||
372 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
373 | field = DECL_CHAIN(field); | |
374 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); | |
375 | elt->index = field; | |
376 | ||
377 | if (rhs_type->points_to() != NULL) | |
378 | { | |
379 | // We are assigning a pointer to the interface; the interface | |
380 | // holds the pointer itself. | |
381 | elt->value = rhs_tree; | |
382 | return build_constructor(lhs_type_tree, init); | |
383 | } | |
384 | ||
385 | // We are assigning a non-pointer value to the interface; the | |
386 | // interface gets a copy of the value in the heap. | |
387 | ||
388 | tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree)); | |
389 | ||
390 | tree space = gogo->allocate_memory(rhs_type, object_size, location); | |
391 | space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)), | |
392 | space); | |
393 | space = save_expr(space); | |
394 | ||
395 | tree ref = build_fold_indirect_ref_loc(location, space); | |
396 | TREE_THIS_NOTRAP(ref) = 1; | |
397 | tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node, | |
398 | ref, rhs_tree); | |
399 | ||
400 | elt->value = fold_convert_loc(location, TREE_TYPE(field), space); | |
401 | ||
402 | return build2(COMPOUND_EXPR, lhs_type_tree, set, | |
403 | build_constructor(lhs_type_tree, init)); | |
404 | } | |
405 | ||
406 | // Return a tree for the type descriptor of RHS_TREE, which has | |
407 | // interface type RHS_TYPE. If RHS_TREE is nil the result will be | |
408 | // NULL. | |
409 | ||
410 | tree | |
411 | Expression::get_interface_type_descriptor(Translate_context*, | |
412 | Type* rhs_type, tree rhs_tree, | |
413 | source_location location) | |
414 | { | |
415 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
416 | gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); | |
417 | tree rhs_field = TYPE_FIELDS(rhs_type_tree); | |
418 | tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
419 | NULL_TREE); | |
420 | if (rhs_type->interface_type()->is_empty()) | |
421 | { | |
422 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), | |
423 | "__type_descriptor") == 0); | |
424 | return v; | |
425 | } | |
426 | ||
427 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods") | |
428 | == 0); | |
429 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(v))); | |
430 | v = save_expr(v); | |
431 | tree v1 = build_fold_indirect_ref_loc(location, v); | |
432 | gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE); | |
433 | tree f = TYPE_FIELDS(TREE_TYPE(v1)); | |
434 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor") | |
435 | == 0); | |
436 | v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE); | |
437 | ||
438 | tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v, | |
439 | fold_convert_loc(location, TREE_TYPE(v), | |
440 | null_pointer_node)); | |
441 | tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node); | |
442 | return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1), | |
443 | eq, n, v1); | |
444 | } | |
445 | ||
446 | // Return a tree for the conversion of an interface type to an | |
447 | // interface type. | |
448 | ||
449 | tree | |
450 | Expression::convert_interface_to_interface(Translate_context* context, | |
451 | Type *lhs_type, Type *rhs_type, | |
452 | tree rhs_tree, bool for_type_guard, | |
453 | source_location location) | |
454 | { | |
455 | Gogo* gogo = context->gogo(); | |
456 | Interface_type* lhs_interface_type = lhs_type->interface_type(); | |
457 | bool lhs_is_empty = lhs_interface_type->is_empty(); | |
458 | ||
459 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
460 | if (lhs_type_tree == error_mark_node) | |
461 | return error_mark_node; | |
462 | ||
463 | // In the general case this requires runtime examination of the type | |
464 | // method table to match it up with the interface methods. | |
465 | ||
466 | // FIXME: If all of the methods in the right hand side interface | |
467 | // also appear in the left hand side interface, then we don't need | |
468 | // to do a runtime check, although we still need to build a new | |
469 | // method table. | |
470 | ||
471 | // Get the type descriptor for the right hand side. This will be | |
472 | // NULL for a nil interface. | |
473 | ||
474 | if (!DECL_P(rhs_tree)) | |
475 | rhs_tree = save_expr(rhs_tree); | |
476 | ||
477 | tree rhs_type_descriptor = | |
478 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
479 | location); | |
480 | ||
481 | // The result is going to be a two element constructor. | |
482 | ||
483 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2); | |
484 | ||
485 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
486 | tree field = TYPE_FIELDS(lhs_type_tree); | |
487 | elt->index = field; | |
488 | ||
489 | if (for_type_guard) | |
490 | { | |
491 | // A type assertion fails when converting a nil interface. | |
492 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo); | |
493 | static tree assert_interface_decl; | |
494 | tree call = Gogo::call_builtin(&assert_interface_decl, | |
495 | location, | |
496 | "__go_assert_interface", | |
497 | 2, | |
498 | ptr_type_node, | |
499 | TREE_TYPE(lhs_type_descriptor), | |
500 | lhs_type_descriptor, | |
501 | TREE_TYPE(rhs_type_descriptor), | |
502 | rhs_type_descriptor); | |
5fb82b5e | 503 | if (call == error_mark_node) |
504 | return error_mark_node; | |
e440a328 | 505 | // This will panic if the interface conversion fails. |
506 | TREE_NOTHROW(assert_interface_decl) = 0; | |
507 | elt->value = fold_convert_loc(location, TREE_TYPE(field), call); | |
508 | } | |
509 | else if (lhs_is_empty) | |
510 | { | |
511 | // A convertion to an empty interface always succeeds, and the | |
512 | // first field is just the type descriptor of the object. | |
513 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), | |
514 | "__type_descriptor") == 0); | |
515 | gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor)); | |
516 | elt->value = rhs_type_descriptor; | |
517 | } | |
518 | else | |
519 | { | |
520 | // A conversion to a non-empty interface may fail, but unlike a | |
521 | // type assertion converting nil will always succeed. | |
522 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") | |
523 | == 0); | |
524 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo); | |
525 | static tree convert_interface_decl; | |
526 | tree call = Gogo::call_builtin(&convert_interface_decl, | |
527 | location, | |
528 | "__go_convert_interface", | |
529 | 2, | |
530 | ptr_type_node, | |
531 | TREE_TYPE(lhs_type_descriptor), | |
532 | lhs_type_descriptor, | |
533 | TREE_TYPE(rhs_type_descriptor), | |
534 | rhs_type_descriptor); | |
5fb82b5e | 535 | if (call == error_mark_node) |
536 | return error_mark_node; | |
e440a328 | 537 | // This will panic if the interface conversion fails. |
538 | TREE_NOTHROW(convert_interface_decl) = 0; | |
539 | elt->value = fold_convert_loc(location, TREE_TYPE(field), call); | |
540 | } | |
541 | ||
542 | // The second field is simply the object pointer. | |
543 | ||
544 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
545 | field = DECL_CHAIN(field); | |
546 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); | |
547 | elt->index = field; | |
548 | ||
549 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
550 | gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); | |
551 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); | |
552 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); | |
553 | elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
554 | NULL_TREE); | |
555 | ||
556 | return build_constructor(lhs_type_tree, init); | |
557 | } | |
558 | ||
559 | // Return a tree for the conversion of an interface type to a | |
560 | // non-interface type. | |
561 | ||
562 | tree | |
563 | Expression::convert_interface_to_type(Translate_context* context, | |
564 | Type *lhs_type, Type* rhs_type, | |
565 | tree rhs_tree, source_location location) | |
566 | { | |
567 | Gogo* gogo = context->gogo(); | |
568 | tree rhs_type_tree = TREE_TYPE(rhs_tree); | |
569 | ||
570 | tree lhs_type_tree = lhs_type->get_tree(gogo); | |
571 | if (lhs_type_tree == error_mark_node) | |
572 | return error_mark_node; | |
573 | ||
574 | // Call a function to check that the type is valid. The function | |
575 | // will panic with an appropriate runtime type error if the type is | |
576 | // not valid. | |
577 | ||
578 | tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo); | |
579 | ||
580 | if (!DECL_P(rhs_tree)) | |
581 | rhs_tree = save_expr(rhs_tree); | |
582 | ||
583 | tree rhs_type_descriptor = | |
584 | Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree, | |
585 | location); | |
586 | ||
587 | tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo); | |
588 | ||
589 | static tree check_interface_type_decl; | |
590 | tree call = Gogo::call_builtin(&check_interface_type_decl, | |
591 | location, | |
592 | "__go_check_interface_type", | |
593 | 3, | |
594 | void_type_node, | |
595 | TREE_TYPE(lhs_type_descriptor), | |
596 | lhs_type_descriptor, | |
597 | TREE_TYPE(rhs_type_descriptor), | |
598 | rhs_type_descriptor, | |
599 | TREE_TYPE(rhs_inter_descriptor), | |
600 | rhs_inter_descriptor); | |
5fb82b5e | 601 | if (call == error_mark_node) |
602 | return error_mark_node; | |
e440a328 | 603 | // This call will panic if the conversion is invalid. |
604 | TREE_NOTHROW(check_interface_type_decl) = 0; | |
605 | ||
606 | // If the call succeeds, pull out the value. | |
607 | gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE); | |
608 | tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree)); | |
609 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0); | |
610 | tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field, | |
611 | NULL_TREE); | |
612 | ||
613 | // If the value is a pointer, then it is the value we want. | |
614 | // Otherwise it points to the value. | |
615 | if (lhs_type->points_to() == NULL) | |
616 | { | |
617 | val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val); | |
618 | val = build_fold_indirect_ref_loc(location, val); | |
619 | } | |
620 | ||
621 | return build2(COMPOUND_EXPR, lhs_type_tree, call, | |
622 | fold_convert_loc(location, lhs_type_tree, val)); | |
623 | } | |
624 | ||
625 | // Convert an expression to a tree. This is implemented by the child | |
626 | // class. Not that it is not in general safe to call this multiple | |
627 | // times for a single expression, but that we don't catch such errors. | |
628 | ||
629 | tree | |
630 | Expression::get_tree(Translate_context* context) | |
631 | { | |
632 | // The child may have marked this expression as having an error. | |
633 | if (this->classification_ == EXPRESSION_ERROR) | |
634 | return error_mark_node; | |
635 | ||
636 | return this->do_get_tree(context); | |
637 | } | |
638 | ||
639 | // Return a tree for VAL in TYPE. | |
640 | ||
641 | tree | |
642 | Expression::integer_constant_tree(mpz_t val, tree type) | |
643 | { | |
644 | if (type == error_mark_node) | |
645 | return error_mark_node; | |
646 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
647 | return double_int_to_tree(type, | |
648 | mpz_get_double_int(type, val, true)); | |
649 | else if (TREE_CODE(type) == REAL_TYPE) | |
650 | { | |
651 | mpfr_t fval; | |
652 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
653 | tree ret = Expression::float_constant_tree(fval, type); | |
654 | mpfr_clear(fval); | |
655 | return ret; | |
656 | } | |
657 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
658 | { | |
659 | mpfr_t fval; | |
660 | mpfr_init_set_z(fval, val, GMP_RNDN); | |
661 | tree real = Expression::float_constant_tree(fval, TREE_TYPE(type)); | |
662 | mpfr_clear(fval); | |
663 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
664 | integer_zero_node); | |
665 | return build_complex(type, real, imag); | |
666 | } | |
667 | else | |
668 | gcc_unreachable(); | |
669 | } | |
670 | ||
671 | // Return a tree for VAL in TYPE. | |
672 | ||
673 | tree | |
674 | Expression::float_constant_tree(mpfr_t val, tree type) | |
675 | { | |
676 | if (type == error_mark_node) | |
677 | return error_mark_node; | |
678 | else if (TREE_CODE(type) == INTEGER_TYPE) | |
679 | { | |
680 | mpz_t ival; | |
681 | mpz_init(ival); | |
682 | mpfr_get_z(ival, val, GMP_RNDN); | |
683 | tree ret = Expression::integer_constant_tree(ival, type); | |
684 | mpz_clear(ival); | |
685 | return ret; | |
686 | } | |
687 | else if (TREE_CODE(type) == REAL_TYPE) | |
688 | { | |
689 | REAL_VALUE_TYPE r1; | |
690 | real_from_mpfr(&r1, val, type, GMP_RNDN); | |
691 | REAL_VALUE_TYPE r2; | |
692 | real_convert(&r2, TYPE_MODE(type), &r1); | |
693 | return build_real(type, r2); | |
694 | } | |
695 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
696 | { | |
697 | REAL_VALUE_TYPE r1; | |
698 | real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN); | |
699 | REAL_VALUE_TYPE r2; | |
700 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
701 | tree imag = build_real_from_int_cst(TREE_TYPE(type), | |
702 | integer_zero_node); | |
703 | return build_complex(type, build_real(TREE_TYPE(type), r2), imag); | |
704 | } | |
705 | else | |
706 | gcc_unreachable(); | |
707 | } | |
708 | ||
709 | // Return a tree for REAL/IMAG in TYPE. | |
710 | ||
711 | tree | |
712 | Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type) | |
713 | { | |
f690b0bb | 714 | if (type == error_mark_node) |
715 | return error_mark_node; | |
716 | else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE) | |
717 | return Expression::float_constant_tree(real, type); | |
718 | else if (TREE_CODE(type) == COMPLEX_TYPE) | |
e440a328 | 719 | { |
720 | REAL_VALUE_TYPE r1; | |
721 | real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN); | |
722 | REAL_VALUE_TYPE r2; | |
723 | real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1); | |
724 | ||
725 | REAL_VALUE_TYPE r3; | |
726 | real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN); | |
727 | REAL_VALUE_TYPE r4; | |
728 | real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3); | |
729 | ||
730 | return build_complex(type, build_real(TREE_TYPE(type), r2), | |
731 | build_real(TREE_TYPE(type), r4)); | |
732 | } | |
733 | else | |
734 | gcc_unreachable(); | |
735 | } | |
736 | ||
737 | // Return a tree which evaluates to true if VAL, of arbitrary integer | |
738 | // type, is negative or is more than the maximum value of BOUND_TYPE. | |
739 | // If SOFAR is not NULL, it is or'red into the result. The return | |
740 | // value may be NULL if SOFAR is NULL. | |
741 | ||
742 | tree | |
743 | Expression::check_bounds(tree val, tree bound_type, tree sofar, | |
744 | source_location loc) | |
745 | { | |
746 | tree val_type = TREE_TYPE(val); | |
747 | tree ret = NULL_TREE; | |
748 | ||
749 | if (!TYPE_UNSIGNED(val_type)) | |
750 | { | |
751 | ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val, | |
752 | build_int_cst(val_type, 0)); | |
753 | if (ret == boolean_false_node) | |
754 | ret = NULL_TREE; | |
755 | } | |
756 | ||
757 | if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type)) | |
758 | || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type)) | |
759 | { | |
760 | tree max = TYPE_MAX_VALUE(bound_type); | |
761 | tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val, | |
762 | fold_convert_loc(loc, val_type, max)); | |
763 | if (big == boolean_false_node) | |
764 | ; | |
765 | else if (ret == NULL_TREE) | |
766 | ret = big; | |
767 | else | |
768 | ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
769 | ret, big); | |
770 | } | |
771 | ||
772 | if (ret == NULL_TREE) | |
773 | return sofar; | |
774 | else if (sofar == NULL_TREE) | |
775 | return ret; | |
776 | else | |
777 | return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
778 | sofar, ret); | |
779 | } | |
780 | ||
781 | // Error expressions. This are used to avoid cascading errors. | |
782 | ||
783 | class Error_expression : public Expression | |
784 | { | |
785 | public: | |
786 | Error_expression(source_location location) | |
787 | : Expression(EXPRESSION_ERROR, location) | |
788 | { } | |
789 | ||
790 | protected: | |
791 | bool | |
792 | do_is_constant() const | |
793 | { return true; } | |
794 | ||
795 | bool | |
796 | do_integer_constant_value(bool, mpz_t val, Type**) const | |
797 | { | |
798 | mpz_set_ui(val, 0); | |
799 | return true; | |
800 | } | |
801 | ||
802 | bool | |
803 | do_float_constant_value(mpfr_t val, Type**) const | |
804 | { | |
805 | mpfr_set_ui(val, 0, GMP_RNDN); | |
806 | return true; | |
807 | } | |
808 | ||
809 | bool | |
810 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const | |
811 | { | |
812 | mpfr_set_ui(real, 0, GMP_RNDN); | |
813 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
814 | return true; | |
815 | } | |
816 | ||
817 | void | |
818 | do_discarding_value() | |
819 | { } | |
820 | ||
821 | Type* | |
822 | do_type() | |
823 | { return Type::make_error_type(); } | |
824 | ||
825 | void | |
826 | do_determine_type(const Type_context*) | |
827 | { } | |
828 | ||
829 | Expression* | |
830 | do_copy() | |
831 | { return this; } | |
832 | ||
833 | bool | |
834 | do_is_addressable() const | |
835 | { return true; } | |
836 | ||
837 | tree | |
838 | do_get_tree(Translate_context*) | |
839 | { return error_mark_node; } | |
840 | }; | |
841 | ||
842 | Expression* | |
843 | Expression::make_error(source_location location) | |
844 | { | |
845 | return new Error_expression(location); | |
846 | } | |
847 | ||
848 | // An expression which is really a type. This is used during parsing. | |
849 | // It is an error if these survive after lowering. | |
850 | ||
851 | class | |
852 | Type_expression : public Expression | |
853 | { | |
854 | public: | |
855 | Type_expression(Type* type, source_location location) | |
856 | : Expression(EXPRESSION_TYPE, location), | |
857 | type_(type) | |
858 | { } | |
859 | ||
860 | protected: | |
861 | int | |
862 | do_traverse(Traverse* traverse) | |
863 | { return Type::traverse(this->type_, traverse); } | |
864 | ||
865 | Type* | |
866 | do_type() | |
867 | { return this->type_; } | |
868 | ||
869 | void | |
870 | do_determine_type(const Type_context*) | |
871 | { } | |
872 | ||
873 | void | |
874 | do_check_types(Gogo*) | |
875 | { this->report_error(_("invalid use of type")); } | |
876 | ||
877 | Expression* | |
878 | do_copy() | |
879 | { return this; } | |
880 | ||
881 | tree | |
882 | do_get_tree(Translate_context*) | |
883 | { gcc_unreachable(); } | |
884 | ||
885 | private: | |
886 | // The type which we are representing as an expression. | |
887 | Type* type_; | |
888 | }; | |
889 | ||
890 | Expression* | |
891 | Expression::make_type(Type* type, source_location location) | |
892 | { | |
893 | return new Type_expression(type, location); | |
894 | } | |
895 | ||
e03bdf36 | 896 | // Class Parser_expression. |
897 | ||
898 | Type* | |
899 | Parser_expression::do_type() | |
900 | { | |
901 | // We should never really ask for the type of a Parser_expression. | |
902 | // However, it can happen, at least when we have an invalid const | |
903 | // whose initializer refers to the const itself. In that case we | |
904 | // may ask for the type when lowering the const itself. | |
905 | gcc_assert(saw_errors()); | |
906 | return Type::make_error_type(); | |
907 | } | |
908 | ||
e440a328 | 909 | // Class Var_expression. |
910 | ||
911 | // Lower a variable expression. Here we just make sure that the | |
912 | // initialization expression of the variable has been lowered. This | |
913 | // ensures that we will be able to determine the type of the variable | |
914 | // if necessary. | |
915 | ||
916 | Expression* | |
917 | Var_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
918 | { | |
919 | if (this->variable_->is_variable()) | |
920 | { | |
921 | Variable* var = this->variable_->var_value(); | |
922 | // This is either a local variable or a global variable. A | |
923 | // reference to a variable which is local to an enclosing | |
924 | // function will be a reference to a field in a closure. | |
925 | if (var->is_global()) | |
926 | function = NULL; | |
927 | var->lower_init_expression(gogo, function); | |
928 | } | |
929 | return this; | |
930 | } | |
931 | ||
e440a328 | 932 | // Return the type of a reference to a variable. |
933 | ||
934 | Type* | |
935 | Var_expression::do_type() | |
936 | { | |
937 | if (this->variable_->is_variable()) | |
938 | return this->variable_->var_value()->type(); | |
939 | else if (this->variable_->is_result_variable()) | |
940 | return this->variable_->result_var_value()->type(); | |
941 | else | |
942 | gcc_unreachable(); | |
943 | } | |
944 | ||
0ab09e06 | 945 | // Determine the type of a reference to a variable. |
946 | ||
947 | void | |
948 | Var_expression::do_determine_type(const Type_context*) | |
949 | { | |
950 | if (this->variable_->is_variable()) | |
951 | this->variable_->var_value()->determine_type(); | |
952 | } | |
953 | ||
e440a328 | 954 | // Something takes the address of this variable. This means that we |
955 | // may want to move the variable onto the heap. | |
956 | ||
957 | void | |
958 | Var_expression::do_address_taken(bool escapes) | |
959 | { | |
960 | if (!escapes) | |
961 | ; | |
962 | else if (this->variable_->is_variable()) | |
963 | this->variable_->var_value()->set_address_taken(); | |
964 | else if (this->variable_->is_result_variable()) | |
965 | this->variable_->result_var_value()->set_address_taken(); | |
966 | else | |
967 | gcc_unreachable(); | |
968 | } | |
969 | ||
970 | // Get the tree for a reference to a variable. | |
971 | ||
972 | tree | |
973 | Var_expression::do_get_tree(Translate_context* context) | |
974 | { | |
975 | return this->variable_->get_tree(context->gogo(), context->function()); | |
976 | } | |
977 | ||
978 | // Make a reference to a variable in an expression. | |
979 | ||
980 | Expression* | |
981 | Expression::make_var_reference(Named_object* var, source_location location) | |
982 | { | |
983 | if (var->is_sink()) | |
984 | return Expression::make_sink(location); | |
985 | ||
986 | // FIXME: Creating a new object for each reference to a variable is | |
987 | // wasteful. | |
988 | return new Var_expression(var, location); | |
989 | } | |
990 | ||
991 | // Class Temporary_reference_expression. | |
992 | ||
993 | // The type. | |
994 | ||
995 | Type* | |
996 | Temporary_reference_expression::do_type() | |
997 | { | |
998 | return this->statement_->type(); | |
999 | } | |
1000 | ||
1001 | // Called if something takes the address of this temporary variable. | |
1002 | // We never have to move temporary variables to the heap, but we do | |
1003 | // need to know that they must live in the stack rather than in a | |
1004 | // register. | |
1005 | ||
1006 | void | |
1007 | Temporary_reference_expression::do_address_taken(bool) | |
1008 | { | |
1009 | this->statement_->set_is_address_taken(); | |
1010 | } | |
1011 | ||
1012 | // Get a tree referring to the variable. | |
1013 | ||
1014 | tree | |
1015 | Temporary_reference_expression::do_get_tree(Translate_context*) | |
1016 | { | |
1017 | return this->statement_->get_decl(); | |
1018 | } | |
1019 | ||
1020 | // Make a reference to a temporary variable. | |
1021 | ||
1022 | Expression* | |
1023 | Expression::make_temporary_reference(Temporary_statement* statement, | |
1024 | source_location location) | |
1025 | { | |
1026 | return new Temporary_reference_expression(statement, location); | |
1027 | } | |
1028 | ||
1029 | // A sink expression--a use of the blank identifier _. | |
1030 | ||
1031 | class Sink_expression : public Expression | |
1032 | { | |
1033 | public: | |
1034 | Sink_expression(source_location location) | |
1035 | : Expression(EXPRESSION_SINK, location), | |
1036 | type_(NULL), var_(NULL_TREE) | |
1037 | { } | |
1038 | ||
1039 | protected: | |
1040 | void | |
1041 | do_discarding_value() | |
1042 | { } | |
1043 | ||
1044 | Type* | |
1045 | do_type(); | |
1046 | ||
1047 | void | |
1048 | do_determine_type(const Type_context*); | |
1049 | ||
1050 | Expression* | |
1051 | do_copy() | |
1052 | { return new Sink_expression(this->location()); } | |
1053 | ||
1054 | tree | |
1055 | do_get_tree(Translate_context*); | |
1056 | ||
1057 | private: | |
1058 | // The type of this sink variable. | |
1059 | Type* type_; | |
1060 | // The temporary variable we generate. | |
1061 | tree var_; | |
1062 | }; | |
1063 | ||
1064 | // Return the type of a sink expression. | |
1065 | ||
1066 | Type* | |
1067 | Sink_expression::do_type() | |
1068 | { | |
1069 | if (this->type_ == NULL) | |
1070 | return Type::make_sink_type(); | |
1071 | return this->type_; | |
1072 | } | |
1073 | ||
1074 | // Determine the type of a sink expression. | |
1075 | ||
1076 | void | |
1077 | Sink_expression::do_determine_type(const Type_context* context) | |
1078 | { | |
1079 | if (context->type != NULL) | |
1080 | this->type_ = context->type; | |
1081 | } | |
1082 | ||
1083 | // Return a temporary variable for a sink expression. This will | |
1084 | // presumably be a write-only variable which the middle-end will drop. | |
1085 | ||
1086 | tree | |
1087 | Sink_expression::do_get_tree(Translate_context* context) | |
1088 | { | |
1089 | if (this->var_ == NULL_TREE) | |
1090 | { | |
1091 | gcc_assert(this->type_ != NULL && !this->type_->is_sink_type()); | |
1092 | this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()), | |
1093 | "blank"); | |
1094 | } | |
1095 | return this->var_; | |
1096 | } | |
1097 | ||
1098 | // Make a sink expression. | |
1099 | ||
1100 | Expression* | |
1101 | Expression::make_sink(source_location location) | |
1102 | { | |
1103 | return new Sink_expression(location); | |
1104 | } | |
1105 | ||
1106 | // Class Func_expression. | |
1107 | ||
1108 | // FIXME: Can a function expression appear in a constant expression? | |
1109 | // The value is unchanging. Initializing a constant to the address of | |
1110 | // a function seems like it could work, though there might be little | |
1111 | // point to it. | |
1112 | ||
e440a328 | 1113 | // Traversal. |
1114 | ||
1115 | int | |
1116 | Func_expression::do_traverse(Traverse* traverse) | |
1117 | { | |
1118 | return (this->closure_ == NULL | |
1119 | ? TRAVERSE_CONTINUE | |
1120 | : Expression::traverse(&this->closure_, traverse)); | |
1121 | } | |
1122 | ||
1123 | // Return the type of a function expression. | |
1124 | ||
1125 | Type* | |
1126 | Func_expression::do_type() | |
1127 | { | |
1128 | if (this->function_->is_function()) | |
1129 | return this->function_->func_value()->type(); | |
1130 | else if (this->function_->is_function_declaration()) | |
1131 | return this->function_->func_declaration_value()->type(); | |
1132 | else | |
1133 | gcc_unreachable(); | |
1134 | } | |
1135 | ||
1136 | // Get the tree for a function expression without evaluating the | |
1137 | // closure. | |
1138 | ||
1139 | tree | |
1140 | Func_expression::get_tree_without_closure(Gogo* gogo) | |
1141 | { | |
1142 | Function_type* fntype; | |
1143 | if (this->function_->is_function()) | |
1144 | fntype = this->function_->func_value()->type(); | |
1145 | else if (this->function_->is_function_declaration()) | |
1146 | fntype = this->function_->func_declaration_value()->type(); | |
1147 | else | |
1148 | gcc_unreachable(); | |
1149 | ||
1150 | // Builtin functions are handled specially by Call_expression. We | |
1151 | // can't take their address. | |
1152 | if (fntype->is_builtin()) | |
1153 | { | |
1154 | error_at(this->location(), "invalid use of special builtin function %qs", | |
1155 | this->function_->name().c_str()); | |
1156 | return error_mark_node; | |
1157 | } | |
1158 | ||
1159 | Named_object* no = this->function_; | |
9d6f3721 | 1160 | |
1161 | tree id = no->get_id(gogo); | |
1162 | if (id == error_mark_node) | |
1163 | return error_mark_node; | |
1164 | ||
e440a328 | 1165 | tree fndecl; |
1166 | if (no->is_function()) | |
1167 | fndecl = no->func_value()->get_or_make_decl(gogo, no, id); | |
1168 | else if (no->is_function_declaration()) | |
1169 | fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id); | |
1170 | else | |
1171 | gcc_unreachable(); | |
1172 | ||
9d6f3721 | 1173 | if (fndecl == error_mark_node) |
1174 | return error_mark_node; | |
1175 | ||
e440a328 | 1176 | return build_fold_addr_expr_loc(this->location(), fndecl); |
1177 | } | |
1178 | ||
1179 | // Get the tree for a function expression. This is used when we take | |
1180 | // the address of a function rather than simply calling it. If the | |
1181 | // function has a closure, we must use a trampoline. | |
1182 | ||
1183 | tree | |
1184 | Func_expression::do_get_tree(Translate_context* context) | |
1185 | { | |
1186 | Gogo* gogo = context->gogo(); | |
1187 | ||
1188 | tree fnaddr = this->get_tree_without_closure(gogo); | |
1189 | if (fnaddr == error_mark_node) | |
1190 | return error_mark_node; | |
1191 | ||
1192 | gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR | |
1193 | && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL); | |
1194 | TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1; | |
1195 | ||
1196 | // For a normal non-nested function call, that is all we have to do. | |
1197 | if (!this->function_->is_function() | |
1198 | || this->function_->func_value()->enclosing() == NULL) | |
1199 | { | |
1200 | gcc_assert(this->closure_ == NULL); | |
1201 | return fnaddr; | |
1202 | } | |
1203 | ||
1204 | // For a nested function call, we have to always allocate a | |
1205 | // trampoline. If we don't always allocate, then closures will not | |
1206 | // be reliably distinct. | |
1207 | Expression* closure = this->closure_; | |
1208 | tree closure_tree; | |
1209 | if (closure == NULL) | |
1210 | closure_tree = null_pointer_node; | |
1211 | else | |
1212 | { | |
1213 | // Get the value of the closure. This will be a pointer to | |
1214 | // space allocated on the heap. | |
1215 | closure_tree = closure->get_tree(context); | |
1216 | if (closure_tree == error_mark_node) | |
1217 | return error_mark_node; | |
1218 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree))); | |
1219 | } | |
1220 | ||
1221 | // Now we need to build some code on the heap. This code will load | |
1222 | // the static chain pointer with the closure and then jump to the | |
1223 | // body of the function. The normal gcc approach is to build the | |
1224 | // code on the stack. Unfortunately we can not do that, as Go | |
1225 | // permits us to return the function pointer. | |
1226 | ||
1227 | return gogo->make_trampoline(fnaddr, closure_tree, this->location()); | |
1228 | } | |
1229 | ||
1230 | // Make a reference to a function in an expression. | |
1231 | ||
1232 | Expression* | |
1233 | Expression::make_func_reference(Named_object* function, Expression* closure, | |
1234 | source_location location) | |
1235 | { | |
1236 | return new Func_expression(function, closure, location); | |
1237 | } | |
1238 | ||
1239 | // Class Unknown_expression. | |
1240 | ||
1241 | // Return the name of an unknown expression. | |
1242 | ||
1243 | const std::string& | |
1244 | Unknown_expression::name() const | |
1245 | { | |
1246 | return this->named_object_->name(); | |
1247 | } | |
1248 | ||
1249 | // Lower a reference to an unknown name. | |
1250 | ||
1251 | Expression* | |
1252 | Unknown_expression::do_lower(Gogo*, Named_object*, int) | |
1253 | { | |
1254 | source_location location = this->location(); | |
1255 | Named_object* no = this->named_object_; | |
deded542 | 1256 | Named_object* real; |
1257 | if (!no->is_unknown()) | |
1258 | real = no; | |
1259 | else | |
e440a328 | 1260 | { |
deded542 | 1261 | real = no->unknown_value()->real_named_object(); |
1262 | if (real == NULL) | |
1263 | { | |
1264 | if (this->is_composite_literal_key_) | |
1265 | return this; | |
1266 | error_at(location, "reference to undefined name %qs", | |
1267 | this->named_object_->message_name().c_str()); | |
1268 | return Expression::make_error(location); | |
1269 | } | |
e440a328 | 1270 | } |
1271 | switch (real->classification()) | |
1272 | { | |
1273 | case Named_object::NAMED_OBJECT_CONST: | |
1274 | return Expression::make_const_reference(real, location); | |
1275 | case Named_object::NAMED_OBJECT_TYPE: | |
1276 | return Expression::make_type(real->type_value(), location); | |
1277 | case Named_object::NAMED_OBJECT_TYPE_DECLARATION: | |
1278 | if (this->is_composite_literal_key_) | |
1279 | return this; | |
1280 | error_at(location, "reference to undefined type %qs", | |
1281 | real->message_name().c_str()); | |
1282 | return Expression::make_error(location); | |
1283 | case Named_object::NAMED_OBJECT_VAR: | |
1284 | return Expression::make_var_reference(real, location); | |
1285 | case Named_object::NAMED_OBJECT_FUNC: | |
1286 | case Named_object::NAMED_OBJECT_FUNC_DECLARATION: | |
1287 | return Expression::make_func_reference(real, NULL, location); | |
1288 | case Named_object::NAMED_OBJECT_PACKAGE: | |
1289 | if (this->is_composite_literal_key_) | |
1290 | return this; | |
1291 | error_at(location, "unexpected reference to package"); | |
1292 | return Expression::make_error(location); | |
1293 | default: | |
1294 | gcc_unreachable(); | |
1295 | } | |
1296 | } | |
1297 | ||
1298 | // Make a reference to an unknown name. | |
1299 | ||
1300 | Expression* | |
1301 | Expression::make_unknown_reference(Named_object* no, source_location location) | |
1302 | { | |
1303 | gcc_assert(no->resolve()->is_unknown()); | |
1304 | return new Unknown_expression(no, location); | |
1305 | } | |
1306 | ||
1307 | // A boolean expression. | |
1308 | ||
1309 | class Boolean_expression : public Expression | |
1310 | { | |
1311 | public: | |
1312 | Boolean_expression(bool val, source_location location) | |
1313 | : Expression(EXPRESSION_BOOLEAN, location), | |
1314 | val_(val), type_(NULL) | |
1315 | { } | |
1316 | ||
1317 | static Expression* | |
1318 | do_import(Import*); | |
1319 | ||
1320 | protected: | |
1321 | bool | |
1322 | do_is_constant() const | |
1323 | { return true; } | |
1324 | ||
1325 | Type* | |
1326 | do_type(); | |
1327 | ||
1328 | void | |
1329 | do_determine_type(const Type_context*); | |
1330 | ||
1331 | Expression* | |
1332 | do_copy() | |
1333 | { return this; } | |
1334 | ||
1335 | tree | |
1336 | do_get_tree(Translate_context*) | |
1337 | { return this->val_ ? boolean_true_node : boolean_false_node; } | |
1338 | ||
1339 | void | |
1340 | do_export(Export* exp) const | |
1341 | { exp->write_c_string(this->val_ ? "true" : "false"); } | |
1342 | ||
1343 | private: | |
1344 | // The constant. | |
1345 | bool val_; | |
1346 | // The type as determined by context. | |
1347 | Type* type_; | |
1348 | }; | |
1349 | ||
1350 | // Get the type. | |
1351 | ||
1352 | Type* | |
1353 | Boolean_expression::do_type() | |
1354 | { | |
1355 | if (this->type_ == NULL) | |
1356 | this->type_ = Type::make_boolean_type(); | |
1357 | return this->type_; | |
1358 | } | |
1359 | ||
1360 | // Set the type from the context. | |
1361 | ||
1362 | void | |
1363 | Boolean_expression::do_determine_type(const Type_context* context) | |
1364 | { | |
1365 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1366 | ; | |
1367 | else if (context->type != NULL && context->type->is_boolean_type()) | |
1368 | this->type_ = context->type; | |
1369 | else if (!context->may_be_abstract) | |
1370 | this->type_ = Type::lookup_bool_type(); | |
1371 | } | |
1372 | ||
1373 | // Import a boolean constant. | |
1374 | ||
1375 | Expression* | |
1376 | Boolean_expression::do_import(Import* imp) | |
1377 | { | |
1378 | if (imp->peek_char() == 't') | |
1379 | { | |
1380 | imp->require_c_string("true"); | |
1381 | return Expression::make_boolean(true, imp->location()); | |
1382 | } | |
1383 | else | |
1384 | { | |
1385 | imp->require_c_string("false"); | |
1386 | return Expression::make_boolean(false, imp->location()); | |
1387 | } | |
1388 | } | |
1389 | ||
1390 | // Make a boolean expression. | |
1391 | ||
1392 | Expression* | |
1393 | Expression::make_boolean(bool val, source_location location) | |
1394 | { | |
1395 | return new Boolean_expression(val, location); | |
1396 | } | |
1397 | ||
1398 | // Class String_expression. | |
1399 | ||
1400 | // Get the type. | |
1401 | ||
1402 | Type* | |
1403 | String_expression::do_type() | |
1404 | { | |
1405 | if (this->type_ == NULL) | |
1406 | this->type_ = Type::make_string_type(); | |
1407 | return this->type_; | |
1408 | } | |
1409 | ||
1410 | // Set the type from the context. | |
1411 | ||
1412 | void | |
1413 | String_expression::do_determine_type(const Type_context* context) | |
1414 | { | |
1415 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1416 | ; | |
1417 | else if (context->type != NULL && context->type->is_string_type()) | |
1418 | this->type_ = context->type; | |
1419 | else if (!context->may_be_abstract) | |
1420 | this->type_ = Type::lookup_string_type(); | |
1421 | } | |
1422 | ||
1423 | // Build a string constant. | |
1424 | ||
1425 | tree | |
1426 | String_expression::do_get_tree(Translate_context* context) | |
1427 | { | |
1428 | return context->gogo()->go_string_constant_tree(this->val_); | |
1429 | } | |
1430 | ||
1431 | // Export a string expression. | |
1432 | ||
1433 | void | |
1434 | String_expression::do_export(Export* exp) const | |
1435 | { | |
1436 | std::string s; | |
1437 | s.reserve(this->val_.length() * 4 + 2); | |
1438 | s += '"'; | |
1439 | for (std::string::const_iterator p = this->val_.begin(); | |
1440 | p != this->val_.end(); | |
1441 | ++p) | |
1442 | { | |
1443 | if (*p == '\\' || *p == '"') | |
1444 | { | |
1445 | s += '\\'; | |
1446 | s += *p; | |
1447 | } | |
1448 | else if (*p >= 0x20 && *p < 0x7f) | |
1449 | s += *p; | |
1450 | else if (*p == '\n') | |
1451 | s += "\\n"; | |
1452 | else if (*p == '\t') | |
1453 | s += "\\t"; | |
1454 | else | |
1455 | { | |
1456 | s += "\\x"; | |
1457 | unsigned char c = *p; | |
1458 | unsigned int dig = c >> 4; | |
1459 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1460 | dig = c & 0xf; | |
1461 | s += dig < 10 ? '0' + dig : 'A' + dig - 10; | |
1462 | } | |
1463 | } | |
1464 | s += '"'; | |
1465 | exp->write_string(s); | |
1466 | } | |
1467 | ||
1468 | // Import a string expression. | |
1469 | ||
1470 | Expression* | |
1471 | String_expression::do_import(Import* imp) | |
1472 | { | |
1473 | imp->require_c_string("\""); | |
1474 | std::string val; | |
1475 | while (true) | |
1476 | { | |
1477 | int c = imp->get_char(); | |
1478 | if (c == '"' || c == -1) | |
1479 | break; | |
1480 | if (c != '\\') | |
1481 | val += static_cast<char>(c); | |
1482 | else | |
1483 | { | |
1484 | c = imp->get_char(); | |
1485 | if (c == '\\' || c == '"') | |
1486 | val += static_cast<char>(c); | |
1487 | else if (c == 'n') | |
1488 | val += '\n'; | |
1489 | else if (c == 't') | |
1490 | val += '\t'; | |
1491 | else if (c == 'x') | |
1492 | { | |
1493 | c = imp->get_char(); | |
1494 | unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1495 | c = imp->get_char(); | |
1496 | unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10; | |
1497 | char v = (vh << 4) | vl; | |
1498 | val += v; | |
1499 | } | |
1500 | else | |
1501 | { | |
1502 | error_at(imp->location(), "bad string constant"); | |
1503 | return Expression::make_error(imp->location()); | |
1504 | } | |
1505 | } | |
1506 | } | |
1507 | return Expression::make_string(val, imp->location()); | |
1508 | } | |
1509 | ||
1510 | // Make a string expression. | |
1511 | ||
1512 | Expression* | |
1513 | Expression::make_string(const std::string& val, source_location location) | |
1514 | { | |
1515 | return new String_expression(val, location); | |
1516 | } | |
1517 | ||
1518 | // Make an integer expression. | |
1519 | ||
1520 | class Integer_expression : public Expression | |
1521 | { | |
1522 | public: | |
1523 | Integer_expression(const mpz_t* val, Type* type, source_location location) | |
1524 | : Expression(EXPRESSION_INTEGER, location), | |
1525 | type_(type) | |
1526 | { mpz_init_set(this->val_, *val); } | |
1527 | ||
1528 | static Expression* | |
1529 | do_import(Import*); | |
1530 | ||
1531 | // Return whether VAL fits in the type. | |
1532 | static bool | |
1533 | check_constant(mpz_t val, Type*, source_location); | |
1534 | ||
1535 | // Write VAL to export data. | |
1536 | static void | |
1537 | export_integer(Export* exp, const mpz_t val); | |
1538 | ||
1539 | protected: | |
1540 | bool | |
1541 | do_is_constant() const | |
1542 | { return true; } | |
1543 | ||
1544 | bool | |
1545 | do_integer_constant_value(bool, mpz_t val, Type** ptype) const; | |
1546 | ||
1547 | Type* | |
1548 | do_type(); | |
1549 | ||
1550 | void | |
1551 | do_determine_type(const Type_context* context); | |
1552 | ||
1553 | void | |
1554 | do_check_types(Gogo*); | |
1555 | ||
1556 | tree | |
1557 | do_get_tree(Translate_context*); | |
1558 | ||
1559 | Expression* | |
1560 | do_copy() | |
1561 | { return Expression::make_integer(&this->val_, this->type_, | |
1562 | this->location()); } | |
1563 | ||
1564 | void | |
1565 | do_export(Export*) const; | |
1566 | ||
1567 | private: | |
1568 | // The integer value. | |
1569 | mpz_t val_; | |
1570 | // The type so far. | |
1571 | Type* type_; | |
1572 | }; | |
1573 | ||
1574 | // Return an integer constant value. | |
1575 | ||
1576 | bool | |
1577 | Integer_expression::do_integer_constant_value(bool, mpz_t val, | |
1578 | Type** ptype) const | |
1579 | { | |
1580 | if (this->type_ != NULL) | |
1581 | *ptype = this->type_; | |
1582 | mpz_set(val, this->val_); | |
1583 | return true; | |
1584 | } | |
1585 | ||
1586 | // Return the current type. If we haven't set the type yet, we return | |
1587 | // an abstract integer type. | |
1588 | ||
1589 | Type* | |
1590 | Integer_expression::do_type() | |
1591 | { | |
1592 | if (this->type_ == NULL) | |
1593 | this->type_ = Type::make_abstract_integer_type(); | |
1594 | return this->type_; | |
1595 | } | |
1596 | ||
1597 | // Set the type of the integer value. Here we may switch from an | |
1598 | // abstract type to a real type. | |
1599 | ||
1600 | void | |
1601 | Integer_expression::do_determine_type(const Type_context* context) | |
1602 | { | |
1603 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1604 | ; | |
1605 | else if (context->type != NULL | |
1606 | && (context->type->integer_type() != NULL | |
1607 | || context->type->float_type() != NULL | |
1608 | || context->type->complex_type() != NULL)) | |
1609 | this->type_ = context->type; | |
1610 | else if (!context->may_be_abstract) | |
1611 | this->type_ = Type::lookup_integer_type("int"); | |
1612 | } | |
1613 | ||
1614 | // Return true if the integer VAL fits in the range of the type TYPE. | |
1615 | // Otherwise give an error and return false. TYPE may be NULL. | |
1616 | ||
1617 | bool | |
1618 | Integer_expression::check_constant(mpz_t val, Type* type, | |
1619 | source_location location) | |
1620 | { | |
1621 | if (type == NULL) | |
1622 | return true; | |
1623 | Integer_type* itype = type->integer_type(); | |
1624 | if (itype == NULL || itype->is_abstract()) | |
1625 | return true; | |
1626 | ||
1627 | int bits = mpz_sizeinbase(val, 2); | |
1628 | ||
1629 | if (itype->is_unsigned()) | |
1630 | { | |
1631 | // For an unsigned type we can only accept a nonnegative number, | |
1632 | // and we must be able to represent at least BITS. | |
1633 | if (mpz_sgn(val) >= 0 | |
1634 | && bits <= itype->bits()) | |
1635 | return true; | |
1636 | } | |
1637 | else | |
1638 | { | |
1639 | // For a signed type we need an extra bit to indicate the sign. | |
1640 | // We have to handle the most negative integer specially. | |
1641 | if (bits + 1 <= itype->bits() | |
1642 | || (bits <= itype->bits() | |
1643 | && mpz_sgn(val) < 0 | |
1644 | && (mpz_scan1(val, 0) | |
1645 | == static_cast<unsigned long>(itype->bits() - 1)) | |
1646 | && mpz_scan0(val, itype->bits()) == ULONG_MAX)) | |
1647 | return true; | |
1648 | } | |
1649 | ||
1650 | error_at(location, "integer constant overflow"); | |
1651 | return false; | |
1652 | } | |
1653 | ||
1654 | // Check the type of an integer constant. | |
1655 | ||
1656 | void | |
1657 | Integer_expression::do_check_types(Gogo*) | |
1658 | { | |
1659 | if (this->type_ == NULL) | |
1660 | return; | |
1661 | if (!Integer_expression::check_constant(this->val_, this->type_, | |
1662 | this->location())) | |
1663 | this->set_is_error(); | |
1664 | } | |
1665 | ||
1666 | // Get a tree for an integer constant. | |
1667 | ||
1668 | tree | |
1669 | Integer_expression::do_get_tree(Translate_context* context) | |
1670 | { | |
1671 | Gogo* gogo = context->gogo(); | |
1672 | tree type; | |
1673 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1674 | type = this->type_->get_tree(gogo); | |
1675 | else if (this->type_ != NULL && this->type_->float_type() != NULL) | |
1676 | { | |
1677 | // We are converting to an abstract floating point type. | |
1678 | type = Type::lookup_float_type("float64")->get_tree(gogo); | |
1679 | } | |
1680 | else if (this->type_ != NULL && this->type_->complex_type() != NULL) | |
1681 | { | |
1682 | // We are converting to an abstract complex type. | |
1683 | type = Type::lookup_complex_type("complex128")->get_tree(gogo); | |
1684 | } | |
1685 | else | |
1686 | { | |
1687 | // If we still have an abstract type here, then this is being | |
1688 | // used in a constant expression which didn't get reduced for | |
1689 | // some reason. Use a type which will fit the value. We use <, | |
1690 | // not <=, because we need an extra bit for the sign bit. | |
1691 | int bits = mpz_sizeinbase(this->val_, 2); | |
1692 | if (bits < INT_TYPE_SIZE) | |
1693 | type = Type::lookup_integer_type("int")->get_tree(gogo); | |
1694 | else if (bits < 64) | |
1695 | type = Type::lookup_integer_type("int64")->get_tree(gogo); | |
1696 | else | |
1697 | type = long_long_integer_type_node; | |
1698 | } | |
1699 | return Expression::integer_constant_tree(this->val_, type); | |
1700 | } | |
1701 | ||
1702 | // Write VAL to export data. | |
1703 | ||
1704 | void | |
1705 | Integer_expression::export_integer(Export* exp, const mpz_t val) | |
1706 | { | |
1707 | char* s = mpz_get_str(NULL, 10, val); | |
1708 | exp->write_c_string(s); | |
1709 | free(s); | |
1710 | } | |
1711 | ||
1712 | // Export an integer in a constant expression. | |
1713 | ||
1714 | void | |
1715 | Integer_expression::do_export(Export* exp) const | |
1716 | { | |
1717 | Integer_expression::export_integer(exp, this->val_); | |
1718 | // A trailing space lets us reliably identify the end of the number. | |
1719 | exp->write_c_string(" "); | |
1720 | } | |
1721 | ||
1722 | // Import an integer, floating point, or complex value. This handles | |
1723 | // all these types because they all start with digits. | |
1724 | ||
1725 | Expression* | |
1726 | Integer_expression::do_import(Import* imp) | |
1727 | { | |
1728 | std::string num = imp->read_identifier(); | |
1729 | imp->require_c_string(" "); | |
1730 | if (!num.empty() && num[num.length() - 1] == 'i') | |
1731 | { | |
1732 | mpfr_t real; | |
1733 | size_t plus_pos = num.find('+', 1); | |
1734 | size_t minus_pos = num.find('-', 1); | |
1735 | size_t pos; | |
1736 | if (plus_pos == std::string::npos) | |
1737 | pos = minus_pos; | |
1738 | else if (minus_pos == std::string::npos) | |
1739 | pos = plus_pos; | |
1740 | else | |
1741 | { | |
1742 | error_at(imp->location(), "bad number in import data: %qs", | |
1743 | num.c_str()); | |
1744 | return Expression::make_error(imp->location()); | |
1745 | } | |
1746 | if (pos == std::string::npos) | |
1747 | mpfr_set_ui(real, 0, GMP_RNDN); | |
1748 | else | |
1749 | { | |
1750 | std::string real_str = num.substr(0, pos); | |
1751 | if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0) | |
1752 | { | |
1753 | error_at(imp->location(), "bad number in import data: %qs", | |
1754 | real_str.c_str()); | |
1755 | return Expression::make_error(imp->location()); | |
1756 | } | |
1757 | } | |
1758 | ||
1759 | std::string imag_str; | |
1760 | if (pos == std::string::npos) | |
1761 | imag_str = num; | |
1762 | else | |
1763 | imag_str = num.substr(pos); | |
1764 | imag_str = imag_str.substr(0, imag_str.size() - 1); | |
1765 | mpfr_t imag; | |
1766 | if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0) | |
1767 | { | |
1768 | error_at(imp->location(), "bad number in import data: %qs", | |
1769 | imag_str.c_str()); | |
1770 | return Expression::make_error(imp->location()); | |
1771 | } | |
1772 | Expression* ret = Expression::make_complex(&real, &imag, NULL, | |
1773 | imp->location()); | |
1774 | mpfr_clear(real); | |
1775 | mpfr_clear(imag); | |
1776 | return ret; | |
1777 | } | |
1778 | else if (num.find('.') == std::string::npos | |
1779 | && num.find('E') == std::string::npos) | |
1780 | { | |
1781 | mpz_t val; | |
1782 | if (mpz_init_set_str(val, num.c_str(), 10) != 0) | |
1783 | { | |
1784 | error_at(imp->location(), "bad number in import data: %qs", | |
1785 | num.c_str()); | |
1786 | return Expression::make_error(imp->location()); | |
1787 | } | |
1788 | Expression* ret = Expression::make_integer(&val, NULL, imp->location()); | |
1789 | mpz_clear(val); | |
1790 | return ret; | |
1791 | } | |
1792 | else | |
1793 | { | |
1794 | mpfr_t val; | |
1795 | if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0) | |
1796 | { | |
1797 | error_at(imp->location(), "bad number in import data: %qs", | |
1798 | num.c_str()); | |
1799 | return Expression::make_error(imp->location()); | |
1800 | } | |
1801 | Expression* ret = Expression::make_float(&val, NULL, imp->location()); | |
1802 | mpfr_clear(val); | |
1803 | return ret; | |
1804 | } | |
1805 | } | |
1806 | ||
1807 | // Build a new integer value. | |
1808 | ||
1809 | Expression* | |
1810 | Expression::make_integer(const mpz_t* val, Type* type, | |
1811 | source_location location) | |
1812 | { | |
1813 | return new Integer_expression(val, type, location); | |
1814 | } | |
1815 | ||
1816 | // Floats. | |
1817 | ||
1818 | class Float_expression : public Expression | |
1819 | { | |
1820 | public: | |
1821 | Float_expression(const mpfr_t* val, Type* type, source_location location) | |
1822 | : Expression(EXPRESSION_FLOAT, location), | |
1823 | type_(type) | |
1824 | { | |
1825 | mpfr_init_set(this->val_, *val, GMP_RNDN); | |
1826 | } | |
1827 | ||
1828 | // Constrain VAL to fit into TYPE. | |
1829 | static void | |
1830 | constrain_float(mpfr_t val, Type* type); | |
1831 | ||
1832 | // Return whether VAL fits in the type. | |
1833 | static bool | |
1834 | check_constant(mpfr_t val, Type*, source_location); | |
1835 | ||
1836 | // Write VAL to export data. | |
1837 | static void | |
1838 | export_float(Export* exp, const mpfr_t val); | |
1839 | ||
1840 | protected: | |
1841 | bool | |
1842 | do_is_constant() const | |
1843 | { return true; } | |
1844 | ||
1845 | bool | |
1846 | do_float_constant_value(mpfr_t val, Type**) const; | |
1847 | ||
1848 | Type* | |
1849 | do_type(); | |
1850 | ||
1851 | void | |
1852 | do_determine_type(const Type_context*); | |
1853 | ||
1854 | void | |
1855 | do_check_types(Gogo*); | |
1856 | ||
1857 | Expression* | |
1858 | do_copy() | |
1859 | { return Expression::make_float(&this->val_, this->type_, | |
1860 | this->location()); } | |
1861 | ||
1862 | tree | |
1863 | do_get_tree(Translate_context*); | |
1864 | ||
1865 | void | |
1866 | do_export(Export*) const; | |
1867 | ||
1868 | private: | |
1869 | // The floating point value. | |
1870 | mpfr_t val_; | |
1871 | // The type so far. | |
1872 | Type* type_; | |
1873 | }; | |
1874 | ||
1875 | // Constrain VAL to fit into TYPE. | |
1876 | ||
1877 | void | |
1878 | Float_expression::constrain_float(mpfr_t val, Type* type) | |
1879 | { | |
1880 | Float_type* ftype = type->float_type(); | |
1881 | if (ftype != NULL && !ftype->is_abstract()) | |
1882 | { | |
1883 | tree type_tree = ftype->type_tree(); | |
1884 | REAL_VALUE_TYPE rvt; | |
1885 | real_from_mpfr(&rvt, val, type_tree, GMP_RNDN); | |
1886 | real_convert(&rvt, TYPE_MODE(type_tree), &rvt); | |
1887 | mpfr_from_real(val, &rvt, GMP_RNDN); | |
1888 | } | |
1889 | } | |
1890 | ||
1891 | // Return a floating point constant value. | |
1892 | ||
1893 | bool | |
1894 | Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
1895 | { | |
1896 | if (this->type_ != NULL) | |
1897 | *ptype = this->type_; | |
1898 | mpfr_set(val, this->val_, GMP_RNDN); | |
1899 | return true; | |
1900 | } | |
1901 | ||
1902 | // Return the current type. If we haven't set the type yet, we return | |
1903 | // an abstract float type. | |
1904 | ||
1905 | Type* | |
1906 | Float_expression::do_type() | |
1907 | { | |
1908 | if (this->type_ == NULL) | |
1909 | this->type_ = Type::make_abstract_float_type(); | |
1910 | return this->type_; | |
1911 | } | |
1912 | ||
1913 | // Set the type of the float value. Here we may switch from an | |
1914 | // abstract type to a real type. | |
1915 | ||
1916 | void | |
1917 | Float_expression::do_determine_type(const Type_context* context) | |
1918 | { | |
1919 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
1920 | ; | |
1921 | else if (context->type != NULL | |
1922 | && (context->type->integer_type() != NULL | |
1923 | || context->type->float_type() != NULL | |
1924 | || context->type->complex_type() != NULL)) | |
1925 | this->type_ = context->type; | |
1926 | else if (!context->may_be_abstract) | |
48080209 | 1927 | this->type_ = Type::lookup_float_type("float64"); |
e440a328 | 1928 | } |
1929 | ||
1930 | // Return true if the floating point value VAL fits in the range of | |
1931 | // the type TYPE. Otherwise give an error and return false. TYPE may | |
1932 | // be NULL. | |
1933 | ||
1934 | bool | |
1935 | Float_expression::check_constant(mpfr_t val, Type* type, | |
1936 | source_location location) | |
1937 | { | |
1938 | if (type == NULL) | |
1939 | return true; | |
1940 | Float_type* ftype = type->float_type(); | |
1941 | if (ftype == NULL || ftype->is_abstract()) | |
1942 | return true; | |
1943 | ||
1944 | // A NaN or Infinity always fits in the range of the type. | |
1945 | if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val)) | |
1946 | return true; | |
1947 | ||
1948 | mp_exp_t exp = mpfr_get_exp(val); | |
1949 | mp_exp_t max_exp; | |
1950 | switch (ftype->bits()) | |
1951 | { | |
1952 | case 32: | |
1953 | max_exp = 128; | |
1954 | break; | |
1955 | case 64: | |
1956 | max_exp = 1024; | |
1957 | break; | |
1958 | default: | |
1959 | gcc_unreachable(); | |
1960 | } | |
1961 | if (exp > max_exp) | |
1962 | { | |
1963 | error_at(location, "floating point constant overflow"); | |
1964 | return false; | |
1965 | } | |
1966 | return true; | |
1967 | } | |
1968 | ||
1969 | // Check the type of a float value. | |
1970 | ||
1971 | void | |
1972 | Float_expression::do_check_types(Gogo*) | |
1973 | { | |
1974 | if (this->type_ == NULL) | |
1975 | return; | |
1976 | ||
1977 | if (!Float_expression::check_constant(this->val_, this->type_, | |
1978 | this->location())) | |
1979 | this->set_is_error(); | |
1980 | ||
1981 | Integer_type* integer_type = this->type_->integer_type(); | |
1982 | if (integer_type != NULL) | |
1983 | { | |
1984 | if (!mpfr_integer_p(this->val_)) | |
1985 | this->report_error(_("floating point constant truncated to integer")); | |
1986 | else | |
1987 | { | |
1988 | gcc_assert(!integer_type->is_abstract()); | |
1989 | mpz_t ival; | |
1990 | mpz_init(ival); | |
1991 | mpfr_get_z(ival, this->val_, GMP_RNDN); | |
1992 | Integer_expression::check_constant(ival, integer_type, | |
1993 | this->location()); | |
1994 | mpz_clear(ival); | |
1995 | } | |
1996 | } | |
1997 | } | |
1998 | ||
1999 | // Get a tree for a float constant. | |
2000 | ||
2001 | tree | |
2002 | Float_expression::do_get_tree(Translate_context* context) | |
2003 | { | |
2004 | Gogo* gogo = context->gogo(); | |
2005 | tree type; | |
2006 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2007 | type = this->type_->get_tree(gogo); | |
2008 | else if (this->type_ != NULL && this->type_->integer_type() != NULL) | |
2009 | { | |
2010 | // We have an abstract integer type. We just hope for the best. | |
2011 | type = Type::lookup_integer_type("int")->get_tree(gogo); | |
2012 | } | |
2013 | else | |
2014 | { | |
2015 | // If we still have an abstract type here, then this is being | |
2016 | // used in a constant expression which didn't get reduced. We | |
2017 | // just use float64 and hope for the best. | |
2018 | type = Type::lookup_float_type("float64")->get_tree(gogo); | |
2019 | } | |
2020 | return Expression::float_constant_tree(this->val_, type); | |
2021 | } | |
2022 | ||
2023 | // Write a floating point number to export data. | |
2024 | ||
2025 | void | |
2026 | Float_expression::export_float(Export *exp, const mpfr_t val) | |
2027 | { | |
2028 | mp_exp_t exponent; | |
2029 | char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN); | |
2030 | if (*s == '-') | |
2031 | exp->write_c_string("-"); | |
2032 | exp->write_c_string("0."); | |
2033 | exp->write_c_string(*s == '-' ? s + 1 : s); | |
2034 | mpfr_free_str(s); | |
2035 | char buf[30]; | |
2036 | snprintf(buf, sizeof buf, "E%ld", exponent); | |
2037 | exp->write_c_string(buf); | |
2038 | } | |
2039 | ||
2040 | // Export a floating point number in a constant expression. | |
2041 | ||
2042 | void | |
2043 | Float_expression::do_export(Export* exp) const | |
2044 | { | |
2045 | Float_expression::export_float(exp, this->val_); | |
2046 | // A trailing space lets us reliably identify the end of the number. | |
2047 | exp->write_c_string(" "); | |
2048 | } | |
2049 | ||
2050 | // Make a float expression. | |
2051 | ||
2052 | Expression* | |
2053 | Expression::make_float(const mpfr_t* val, Type* type, source_location location) | |
2054 | { | |
2055 | return new Float_expression(val, type, location); | |
2056 | } | |
2057 | ||
2058 | // Complex numbers. | |
2059 | ||
2060 | class Complex_expression : public Expression | |
2061 | { | |
2062 | public: | |
2063 | Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
2064 | source_location location) | |
2065 | : Expression(EXPRESSION_COMPLEX, location), | |
2066 | type_(type) | |
2067 | { | |
2068 | mpfr_init_set(this->real_, *real, GMP_RNDN); | |
2069 | mpfr_init_set(this->imag_, *imag, GMP_RNDN); | |
2070 | } | |
2071 | ||
2072 | // Constrain REAL/IMAG to fit into TYPE. | |
2073 | static void | |
2074 | constrain_complex(mpfr_t real, mpfr_t imag, Type* type); | |
2075 | ||
2076 | // Return whether REAL/IMAG fits in the type. | |
2077 | static bool | |
2078 | check_constant(mpfr_t real, mpfr_t imag, Type*, source_location); | |
2079 | ||
2080 | // Write REAL/IMAG to export data. | |
2081 | static void | |
2082 | export_complex(Export* exp, const mpfr_t real, const mpfr_t val); | |
2083 | ||
2084 | protected: | |
2085 | bool | |
2086 | do_is_constant() const | |
2087 | { return true; } | |
2088 | ||
2089 | bool | |
2090 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const; | |
2091 | ||
2092 | Type* | |
2093 | do_type(); | |
2094 | ||
2095 | void | |
2096 | do_determine_type(const Type_context*); | |
2097 | ||
2098 | void | |
2099 | do_check_types(Gogo*); | |
2100 | ||
2101 | Expression* | |
2102 | do_copy() | |
2103 | { | |
2104 | return Expression::make_complex(&this->real_, &this->imag_, this->type_, | |
2105 | this->location()); | |
2106 | } | |
2107 | ||
2108 | tree | |
2109 | do_get_tree(Translate_context*); | |
2110 | ||
2111 | void | |
2112 | do_export(Export*) const; | |
2113 | ||
2114 | private: | |
2115 | // The real part. | |
2116 | mpfr_t real_; | |
2117 | // The imaginary part; | |
2118 | mpfr_t imag_; | |
2119 | // The type if known. | |
2120 | Type* type_; | |
2121 | }; | |
2122 | ||
2123 | // Constrain REAL/IMAG to fit into TYPE. | |
2124 | ||
2125 | void | |
2126 | Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type) | |
2127 | { | |
2128 | Complex_type* ctype = type->complex_type(); | |
2129 | if (ctype != NULL && !ctype->is_abstract()) | |
2130 | { | |
2131 | tree type_tree = ctype->type_tree(); | |
2132 | ||
2133 | REAL_VALUE_TYPE rvt; | |
2134 | real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN); | |
2135 | real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt); | |
2136 | mpfr_from_real(real, &rvt, GMP_RNDN); | |
2137 | ||
2138 | real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN); | |
2139 | real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt); | |
2140 | mpfr_from_real(imag, &rvt, GMP_RNDN); | |
2141 | } | |
2142 | } | |
2143 | ||
2144 | // Return a complex constant value. | |
2145 | ||
2146 | bool | |
2147 | Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
2148 | Type** ptype) const | |
2149 | { | |
2150 | if (this->type_ != NULL) | |
2151 | *ptype = this->type_; | |
2152 | mpfr_set(real, this->real_, GMP_RNDN); | |
2153 | mpfr_set(imag, this->imag_, GMP_RNDN); | |
2154 | return true; | |
2155 | } | |
2156 | ||
2157 | // Return the current type. If we haven't set the type yet, we return | |
2158 | // an abstract complex type. | |
2159 | ||
2160 | Type* | |
2161 | Complex_expression::do_type() | |
2162 | { | |
2163 | if (this->type_ == NULL) | |
2164 | this->type_ = Type::make_abstract_complex_type(); | |
2165 | return this->type_; | |
2166 | } | |
2167 | ||
2168 | // Set the type of the complex value. Here we may switch from an | |
2169 | // abstract type to a real type. | |
2170 | ||
2171 | void | |
2172 | Complex_expression::do_determine_type(const Type_context* context) | |
2173 | { | |
2174 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2175 | ; | |
2176 | else if (context->type != NULL | |
2177 | && context->type->complex_type() != NULL) | |
2178 | this->type_ = context->type; | |
2179 | else if (!context->may_be_abstract) | |
48080209 | 2180 | this->type_ = Type::lookup_complex_type("complex128"); |
e440a328 | 2181 | } |
2182 | ||
2183 | // Return true if the complex value REAL/IMAG fits in the range of the | |
2184 | // type TYPE. Otherwise give an error and return false. TYPE may be | |
2185 | // NULL. | |
2186 | ||
2187 | bool | |
2188 | Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type, | |
2189 | source_location location) | |
2190 | { | |
2191 | if (type == NULL) | |
2192 | return true; | |
2193 | Complex_type* ctype = type->complex_type(); | |
2194 | if (ctype == NULL || ctype->is_abstract()) | |
2195 | return true; | |
2196 | ||
2197 | mp_exp_t max_exp; | |
2198 | switch (ctype->bits()) | |
2199 | { | |
2200 | case 64: | |
2201 | max_exp = 128; | |
2202 | break; | |
2203 | case 128: | |
2204 | max_exp = 1024; | |
2205 | break; | |
2206 | default: | |
2207 | gcc_unreachable(); | |
2208 | } | |
2209 | ||
2210 | // A NaN or Infinity always fits in the range of the type. | |
2211 | if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real)) | |
2212 | { | |
2213 | if (mpfr_get_exp(real) > max_exp) | |
2214 | { | |
2215 | error_at(location, "complex real part constant overflow"); | |
2216 | return false; | |
2217 | } | |
2218 | } | |
2219 | ||
2220 | if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag)) | |
2221 | { | |
2222 | if (mpfr_get_exp(imag) > max_exp) | |
2223 | { | |
2224 | error_at(location, "complex imaginary part constant overflow"); | |
2225 | return false; | |
2226 | } | |
2227 | } | |
2228 | ||
2229 | return true; | |
2230 | } | |
2231 | ||
2232 | // Check the type of a complex value. | |
2233 | ||
2234 | void | |
2235 | Complex_expression::do_check_types(Gogo*) | |
2236 | { | |
2237 | if (this->type_ == NULL) | |
2238 | return; | |
2239 | ||
2240 | if (!Complex_expression::check_constant(this->real_, this->imag_, | |
2241 | this->type_, this->location())) | |
2242 | this->set_is_error(); | |
2243 | } | |
2244 | ||
2245 | // Get a tree for a complex constant. | |
2246 | ||
2247 | tree | |
2248 | Complex_expression::do_get_tree(Translate_context* context) | |
2249 | { | |
2250 | Gogo* gogo = context->gogo(); | |
2251 | tree type; | |
2252 | if (this->type_ != NULL && !this->type_->is_abstract()) | |
2253 | type = this->type_->get_tree(gogo); | |
2254 | else | |
2255 | { | |
2256 | // If we still have an abstract type here, this this is being | |
2257 | // used in a constant expression which didn't get reduced. We | |
2258 | // just use complex128 and hope for the best. | |
2259 | type = Type::lookup_complex_type("complex128")->get_tree(gogo); | |
2260 | } | |
2261 | return Expression::complex_constant_tree(this->real_, this->imag_, type); | |
2262 | } | |
2263 | ||
2264 | // Write REAL/IMAG to export data. | |
2265 | ||
2266 | void | |
2267 | Complex_expression::export_complex(Export* exp, const mpfr_t real, | |
2268 | const mpfr_t imag) | |
2269 | { | |
2270 | if (!mpfr_zero_p(real)) | |
2271 | { | |
2272 | Float_expression::export_float(exp, real); | |
2273 | if (mpfr_sgn(imag) > 0) | |
2274 | exp->write_c_string("+"); | |
2275 | } | |
2276 | Float_expression::export_float(exp, imag); | |
2277 | exp->write_c_string("i"); | |
2278 | } | |
2279 | ||
2280 | // Export a complex number in a constant expression. | |
2281 | ||
2282 | void | |
2283 | Complex_expression::do_export(Export* exp) const | |
2284 | { | |
2285 | Complex_expression::export_complex(exp, this->real_, this->imag_); | |
2286 | // A trailing space lets us reliably identify the end of the number. | |
2287 | exp->write_c_string(" "); | |
2288 | } | |
2289 | ||
2290 | // Make a complex expression. | |
2291 | ||
2292 | Expression* | |
2293 | Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type, | |
2294 | source_location location) | |
2295 | { | |
2296 | return new Complex_expression(real, imag, type, location); | |
2297 | } | |
2298 | ||
d5b605df | 2299 | // Find a named object in an expression. |
2300 | ||
2301 | class Find_named_object : public Traverse | |
2302 | { | |
2303 | public: | |
2304 | Find_named_object(Named_object* no) | |
2305 | : Traverse(traverse_expressions), | |
2306 | no_(no), found_(false) | |
2307 | { } | |
2308 | ||
2309 | // Whether we found the object. | |
2310 | bool | |
2311 | found() const | |
2312 | { return this->found_; } | |
2313 | ||
2314 | protected: | |
2315 | int | |
2316 | expression(Expression**); | |
2317 | ||
2318 | private: | |
2319 | // The object we are looking for. | |
2320 | Named_object* no_; | |
2321 | // Whether we found it. | |
2322 | bool found_; | |
2323 | }; | |
2324 | ||
e440a328 | 2325 | // A reference to a const in an expression. |
2326 | ||
2327 | class Const_expression : public Expression | |
2328 | { | |
2329 | public: | |
2330 | Const_expression(Named_object* constant, source_location location) | |
2331 | : Expression(EXPRESSION_CONST_REFERENCE, location), | |
13e818f5 | 2332 | constant_(constant), type_(NULL), seen_(false) |
e440a328 | 2333 | { } |
2334 | ||
d5b605df | 2335 | Named_object* |
2336 | named_object() | |
2337 | { return this->constant_; } | |
2338 | ||
a7f064d5 | 2339 | // Check that the initializer does not refer to the constant itself. |
2340 | void | |
2341 | check_for_init_loop(); | |
2342 | ||
e440a328 | 2343 | protected: |
ba4aedd4 | 2344 | int |
2345 | do_traverse(Traverse*); | |
2346 | ||
e440a328 | 2347 | Expression* |
2348 | do_lower(Gogo*, Named_object*, int); | |
2349 | ||
2350 | bool | |
2351 | do_is_constant() const | |
2352 | { return true; } | |
2353 | ||
2354 | bool | |
2355 | do_integer_constant_value(bool, mpz_t val, Type**) const; | |
2356 | ||
2357 | bool | |
2358 | do_float_constant_value(mpfr_t val, Type**) const; | |
2359 | ||
2360 | bool | |
2361 | do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const; | |
2362 | ||
2363 | bool | |
2364 | do_string_constant_value(std::string* val) const | |
2365 | { return this->constant_->const_value()->expr()->string_constant_value(val); } | |
2366 | ||
2367 | Type* | |
2368 | do_type(); | |
2369 | ||
2370 | // The type of a const is set by the declaration, not the use. | |
2371 | void | |
2372 | do_determine_type(const Type_context*); | |
2373 | ||
2374 | void | |
2375 | do_check_types(Gogo*); | |
2376 | ||
2377 | Expression* | |
2378 | do_copy() | |
2379 | { return this; } | |
2380 | ||
2381 | tree | |
2382 | do_get_tree(Translate_context* context); | |
2383 | ||
2384 | // When exporting a reference to a const as part of a const | |
2385 | // expression, we export the value. We ignore the fact that it has | |
2386 | // a name. | |
2387 | void | |
2388 | do_export(Export* exp) const | |
2389 | { this->constant_->const_value()->expr()->export_expression(exp); } | |
2390 | ||
2391 | private: | |
2392 | // The constant. | |
2393 | Named_object* constant_; | |
2394 | // The type of this reference. This is used if the constant has an | |
2395 | // abstract type. | |
2396 | Type* type_; | |
13e818f5 | 2397 | // Used to prevent infinite recursion when a constant incorrectly |
2398 | // refers to itself. | |
2399 | mutable bool seen_; | |
e440a328 | 2400 | }; |
2401 | ||
ba4aedd4 | 2402 | // Traversal. |
2403 | ||
2404 | int | |
2405 | Const_expression::do_traverse(Traverse* traverse) | |
2406 | { | |
2407 | if (this->type_ != NULL) | |
2408 | return Type::traverse(this->type_, traverse); | |
2409 | return TRAVERSE_CONTINUE; | |
2410 | } | |
2411 | ||
e440a328 | 2412 | // Lower a constant expression. This is where we convert the |
2413 | // predeclared constant iota into an integer value. | |
2414 | ||
2415 | Expression* | |
2416 | Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value) | |
2417 | { | |
2418 | if (this->constant_->const_value()->expr()->classification() | |
2419 | == EXPRESSION_IOTA) | |
2420 | { | |
2421 | if (iota_value == -1) | |
2422 | { | |
2423 | error_at(this->location(), | |
2424 | "iota is only defined in const declarations"); | |
2425 | iota_value = 0; | |
2426 | } | |
2427 | mpz_t val; | |
2428 | mpz_init_set_ui(val, static_cast<unsigned long>(iota_value)); | |
2429 | Expression* ret = Expression::make_integer(&val, NULL, | |
2430 | this->location()); | |
2431 | mpz_clear(val); | |
2432 | return ret; | |
2433 | } | |
2434 | ||
2435 | // Make sure that the constant itself has been lowered. | |
2436 | gogo->lower_constant(this->constant_); | |
2437 | ||
2438 | return this; | |
2439 | } | |
2440 | ||
2441 | // Return an integer constant value. | |
2442 | ||
2443 | bool | |
2444 | Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
2445 | Type** ptype) const | |
2446 | { | |
13e818f5 | 2447 | if (this->seen_) |
2448 | return false; | |
2449 | ||
e440a328 | 2450 | Type* ctype; |
2451 | if (this->type_ != NULL) | |
2452 | ctype = this->type_; | |
2453 | else | |
2454 | ctype = this->constant_->const_value()->type(); | |
2455 | if (ctype != NULL && ctype->integer_type() == NULL) | |
2456 | return false; | |
2457 | ||
2458 | Expression* e = this->constant_->const_value()->expr(); | |
13e818f5 | 2459 | |
2460 | this->seen_ = true; | |
2461 | ||
e440a328 | 2462 | Type* t; |
2463 | bool r = e->integer_constant_value(iota_is_constant, val, &t); | |
2464 | ||
13e818f5 | 2465 | this->seen_ = false; |
2466 | ||
e440a328 | 2467 | if (r |
2468 | && ctype != NULL | |
2469 | && !Integer_expression::check_constant(val, ctype, this->location())) | |
2470 | return false; | |
2471 | ||
2472 | *ptype = ctype != NULL ? ctype : t; | |
2473 | return r; | |
2474 | } | |
2475 | ||
2476 | // Return a floating point constant value. | |
2477 | ||
2478 | bool | |
2479 | Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
2480 | { | |
13e818f5 | 2481 | if (this->seen_) |
2482 | return false; | |
2483 | ||
e440a328 | 2484 | Type* ctype; |
2485 | if (this->type_ != NULL) | |
2486 | ctype = this->type_; | |
2487 | else | |
2488 | ctype = this->constant_->const_value()->type(); | |
2489 | if (ctype != NULL && ctype->float_type() == NULL) | |
2490 | return false; | |
2491 | ||
13e818f5 | 2492 | this->seen_ = true; |
2493 | ||
e440a328 | 2494 | Type* t; |
2495 | bool r = this->constant_->const_value()->expr()->float_constant_value(val, | |
2496 | &t); | |
13e818f5 | 2497 | |
2498 | this->seen_ = false; | |
2499 | ||
e440a328 | 2500 | if (r && ctype != NULL) |
2501 | { | |
2502 | if (!Float_expression::check_constant(val, ctype, this->location())) | |
2503 | return false; | |
2504 | Float_expression::constrain_float(val, ctype); | |
2505 | } | |
2506 | *ptype = ctype != NULL ? ctype : t; | |
2507 | return r; | |
2508 | } | |
2509 | ||
2510 | // Return a complex constant value. | |
2511 | ||
2512 | bool | |
2513 | Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
2514 | Type **ptype) const | |
2515 | { | |
13e818f5 | 2516 | if (this->seen_) |
2517 | return false; | |
2518 | ||
e440a328 | 2519 | Type* ctype; |
2520 | if (this->type_ != NULL) | |
2521 | ctype = this->type_; | |
2522 | else | |
2523 | ctype = this->constant_->const_value()->type(); | |
2524 | if (ctype != NULL && ctype->complex_type() == NULL) | |
2525 | return false; | |
2526 | ||
13e818f5 | 2527 | this->seen_ = true; |
2528 | ||
e440a328 | 2529 | Type *t; |
2530 | bool r = this->constant_->const_value()->expr()->complex_constant_value(real, | |
2531 | imag, | |
2532 | &t); | |
13e818f5 | 2533 | |
2534 | this->seen_ = false; | |
2535 | ||
e440a328 | 2536 | if (r && ctype != NULL) |
2537 | { | |
2538 | if (!Complex_expression::check_constant(real, imag, ctype, | |
2539 | this->location())) | |
2540 | return false; | |
2541 | Complex_expression::constrain_complex(real, imag, ctype); | |
2542 | } | |
2543 | *ptype = ctype != NULL ? ctype : t; | |
2544 | return r; | |
2545 | } | |
2546 | ||
2547 | // Return the type of the const reference. | |
2548 | ||
2549 | Type* | |
2550 | Const_expression::do_type() | |
2551 | { | |
2552 | if (this->type_ != NULL) | |
2553 | return this->type_; | |
13e818f5 | 2554 | |
2f78f012 | 2555 | Named_constant* nc = this->constant_->const_value(); |
2556 | ||
2557 | if (this->seen_ || nc->lowering()) | |
13e818f5 | 2558 | { |
2559 | this->report_error(_("constant refers to itself")); | |
2560 | this->type_ = Type::make_error_type(); | |
2561 | return this->type_; | |
2562 | } | |
2563 | ||
2564 | this->seen_ = true; | |
2565 | ||
e440a328 | 2566 | Type* ret = nc->type(); |
13e818f5 | 2567 | |
e440a328 | 2568 | if (ret != NULL) |
13e818f5 | 2569 | { |
2570 | this->seen_ = false; | |
2571 | return ret; | |
2572 | } | |
2573 | ||
e440a328 | 2574 | // During parsing, a named constant may have a NULL type, but we |
2575 | // must not return a NULL type here. | |
13e818f5 | 2576 | ret = nc->expr()->type(); |
2577 | ||
2578 | this->seen_ = false; | |
2579 | ||
2580 | return ret; | |
e440a328 | 2581 | } |
2582 | ||
2583 | // Set the type of the const reference. | |
2584 | ||
2585 | void | |
2586 | Const_expression::do_determine_type(const Type_context* context) | |
2587 | { | |
2588 | Type* ctype = this->constant_->const_value()->type(); | |
2589 | Type* cetype = (ctype != NULL | |
2590 | ? ctype | |
2591 | : this->constant_->const_value()->expr()->type()); | |
2592 | if (ctype != NULL && !ctype->is_abstract()) | |
2593 | ; | |
2594 | else if (context->type != NULL | |
2595 | && (context->type->integer_type() != NULL | |
2596 | || context->type->float_type() != NULL | |
2597 | || context->type->complex_type() != NULL) | |
2598 | && (cetype->integer_type() != NULL | |
2599 | || cetype->float_type() != NULL | |
2600 | || cetype->complex_type() != NULL)) | |
2601 | this->type_ = context->type; | |
2602 | else if (context->type != NULL | |
2603 | && context->type->is_string_type() | |
2604 | && cetype->is_string_type()) | |
2605 | this->type_ = context->type; | |
2606 | else if (context->type != NULL | |
2607 | && context->type->is_boolean_type() | |
2608 | && cetype->is_boolean_type()) | |
2609 | this->type_ = context->type; | |
2610 | else if (!context->may_be_abstract) | |
2611 | { | |
2612 | if (cetype->is_abstract()) | |
2613 | cetype = cetype->make_non_abstract_type(); | |
2614 | this->type_ = cetype; | |
2615 | } | |
2616 | } | |
2617 | ||
a7f064d5 | 2618 | // Check for a loop in which the initializer of a constant refers to |
2619 | // the constant itself. | |
e440a328 | 2620 | |
2621 | void | |
a7f064d5 | 2622 | Const_expression::check_for_init_loop() |
e440a328 | 2623 | { |
5c13bd80 | 2624 | if (this->type_ != NULL && this->type_->is_error()) |
d5b605df | 2625 | return; |
2626 | ||
a7f064d5 | 2627 | if (this->seen_) |
2628 | { | |
2629 | this->report_error(_("constant refers to itself")); | |
2630 | this->type_ = Type::make_error_type(); | |
2631 | return; | |
2632 | } | |
2633 | ||
d5b605df | 2634 | Expression* init = this->constant_->const_value()->expr(); |
2635 | Find_named_object find_named_object(this->constant_); | |
a7f064d5 | 2636 | |
2637 | this->seen_ = true; | |
d5b605df | 2638 | Expression::traverse(&init, &find_named_object); |
a7f064d5 | 2639 | this->seen_ = false; |
2640 | ||
d5b605df | 2641 | if (find_named_object.found()) |
2642 | { | |
5c13bd80 | 2643 | if (this->type_ == NULL || !this->type_->is_error()) |
a7f064d5 | 2644 | { |
2645 | this->report_error(_("constant refers to itself")); | |
2646 | this->type_ = Type::make_error_type(); | |
2647 | } | |
d5b605df | 2648 | return; |
2649 | } | |
a7f064d5 | 2650 | } |
2651 | ||
2652 | // Check types of a const reference. | |
2653 | ||
2654 | void | |
2655 | Const_expression::do_check_types(Gogo*) | |
2656 | { | |
5c13bd80 | 2657 | if (this->type_ != NULL && this->type_->is_error()) |
a7f064d5 | 2658 | return; |
2659 | ||
2660 | this->check_for_init_loop(); | |
d5b605df | 2661 | |
e440a328 | 2662 | if (this->type_ == NULL || this->type_->is_abstract()) |
2663 | return; | |
2664 | ||
2665 | // Check for integer overflow. | |
2666 | if (this->type_->integer_type() != NULL) | |
2667 | { | |
2668 | mpz_t ival; | |
2669 | mpz_init(ival); | |
2670 | Type* dummy; | |
2671 | if (!this->integer_constant_value(true, ival, &dummy)) | |
2672 | { | |
2673 | mpfr_t fval; | |
2674 | mpfr_init(fval); | |
2675 | Expression* cexpr = this->constant_->const_value()->expr(); | |
2676 | if (cexpr->float_constant_value(fval, &dummy)) | |
2677 | { | |
2678 | if (!mpfr_integer_p(fval)) | |
2679 | this->report_error(_("floating point constant " | |
2680 | "truncated to integer")); | |
2681 | else | |
2682 | { | |
2683 | mpfr_get_z(ival, fval, GMP_RNDN); | |
2684 | Integer_expression::check_constant(ival, this->type_, | |
2685 | this->location()); | |
2686 | } | |
2687 | } | |
2688 | mpfr_clear(fval); | |
2689 | } | |
2690 | mpz_clear(ival); | |
2691 | } | |
2692 | } | |
2693 | ||
2694 | // Return a tree for the const reference. | |
2695 | ||
2696 | tree | |
2697 | Const_expression::do_get_tree(Translate_context* context) | |
2698 | { | |
2699 | Gogo* gogo = context->gogo(); | |
2700 | tree type_tree; | |
2701 | if (this->type_ == NULL) | |
2702 | type_tree = NULL_TREE; | |
2703 | else | |
2704 | { | |
2705 | type_tree = this->type_->get_tree(gogo); | |
2706 | if (type_tree == error_mark_node) | |
2707 | return error_mark_node; | |
2708 | } | |
2709 | ||
2710 | // If the type has been set for this expression, but the underlying | |
2711 | // object is an abstract int or float, we try to get the abstract | |
2712 | // value. Otherwise we may lose something in the conversion. | |
2713 | if (this->type_ != NULL | |
a68492b4 | 2714 | && (this->constant_->const_value()->type() == NULL |
2715 | || this->constant_->const_value()->type()->is_abstract())) | |
e440a328 | 2716 | { |
2717 | Expression* expr = this->constant_->const_value()->expr(); | |
2718 | mpz_t ival; | |
2719 | mpz_init(ival); | |
2720 | Type* t; | |
2721 | if (expr->integer_constant_value(true, ival, &t)) | |
2722 | { | |
2723 | tree ret = Expression::integer_constant_tree(ival, type_tree); | |
2724 | mpz_clear(ival); | |
2725 | return ret; | |
2726 | } | |
2727 | mpz_clear(ival); | |
2728 | ||
2729 | mpfr_t fval; | |
2730 | mpfr_init(fval); | |
2731 | if (expr->float_constant_value(fval, &t)) | |
2732 | { | |
2733 | tree ret = Expression::float_constant_tree(fval, type_tree); | |
2734 | mpfr_clear(fval); | |
2735 | return ret; | |
2736 | } | |
2737 | ||
2738 | mpfr_t imag; | |
2739 | mpfr_init(imag); | |
2740 | if (expr->complex_constant_value(fval, imag, &t)) | |
2741 | { | |
2742 | tree ret = Expression::complex_constant_tree(fval, imag, type_tree); | |
2743 | mpfr_clear(fval); | |
2744 | mpfr_clear(imag); | |
2745 | return ret; | |
2746 | } | |
2747 | mpfr_clear(imag); | |
2748 | mpfr_clear(fval); | |
2749 | } | |
2750 | ||
2751 | tree const_tree = this->constant_->get_tree(gogo, context->function()); | |
2752 | if (this->type_ == NULL | |
2753 | || const_tree == error_mark_node | |
2754 | || TREE_TYPE(const_tree) == error_mark_node) | |
2755 | return const_tree; | |
2756 | ||
2757 | tree ret; | |
2758 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree))) | |
2759 | ret = fold_convert(type_tree, const_tree); | |
2760 | else if (TREE_CODE(type_tree) == INTEGER_TYPE) | |
2761 | ret = fold(convert_to_integer(type_tree, const_tree)); | |
2762 | else if (TREE_CODE(type_tree) == REAL_TYPE) | |
2763 | ret = fold(convert_to_real(type_tree, const_tree)); | |
2764 | else if (TREE_CODE(type_tree) == COMPLEX_TYPE) | |
2765 | ret = fold(convert_to_complex(type_tree, const_tree)); | |
2766 | else | |
2767 | gcc_unreachable(); | |
2768 | return ret; | |
2769 | } | |
2770 | ||
2771 | // Make a reference to a constant in an expression. | |
2772 | ||
2773 | Expression* | |
2774 | Expression::make_const_reference(Named_object* constant, | |
2775 | source_location location) | |
2776 | { | |
2777 | return new Const_expression(constant, location); | |
2778 | } | |
2779 | ||
d5b605df | 2780 | // Find a named object in an expression. |
2781 | ||
2782 | int | |
2783 | Find_named_object::expression(Expression** pexpr) | |
2784 | { | |
2785 | switch ((*pexpr)->classification()) | |
2786 | { | |
2787 | case Expression::EXPRESSION_CONST_REFERENCE: | |
a7f064d5 | 2788 | { |
2789 | Const_expression* ce = static_cast<Const_expression*>(*pexpr); | |
2790 | if (ce->named_object() == this->no_) | |
2791 | break; | |
2792 | ||
2793 | // We need to check a constant initializer explicitly, as | |
2794 | // loops here will not be caught by the loop checking for | |
2795 | // variable initializers. | |
2796 | ce->check_for_init_loop(); | |
2797 | ||
2798 | return TRAVERSE_CONTINUE; | |
2799 | } | |
2800 | ||
d5b605df | 2801 | case Expression::EXPRESSION_VAR_REFERENCE: |
2802 | if ((*pexpr)->var_expression()->named_object() == this->no_) | |
2803 | break; | |
2804 | return TRAVERSE_CONTINUE; | |
2805 | case Expression::EXPRESSION_FUNC_REFERENCE: | |
2806 | if ((*pexpr)->func_expression()->named_object() == this->no_) | |
2807 | break; | |
2808 | return TRAVERSE_CONTINUE; | |
2809 | default: | |
2810 | return TRAVERSE_CONTINUE; | |
2811 | } | |
2812 | this->found_ = true; | |
2813 | return TRAVERSE_EXIT; | |
2814 | } | |
2815 | ||
e440a328 | 2816 | // The nil value. |
2817 | ||
2818 | class Nil_expression : public Expression | |
2819 | { | |
2820 | public: | |
2821 | Nil_expression(source_location location) | |
2822 | : Expression(EXPRESSION_NIL, location) | |
2823 | { } | |
2824 | ||
2825 | static Expression* | |
2826 | do_import(Import*); | |
2827 | ||
2828 | protected: | |
2829 | bool | |
2830 | do_is_constant() const | |
2831 | { return true; } | |
2832 | ||
2833 | Type* | |
2834 | do_type() | |
2835 | { return Type::make_nil_type(); } | |
2836 | ||
2837 | void | |
2838 | do_determine_type(const Type_context*) | |
2839 | { } | |
2840 | ||
2841 | Expression* | |
2842 | do_copy() | |
2843 | { return this; } | |
2844 | ||
2845 | tree | |
2846 | do_get_tree(Translate_context*) | |
2847 | { return null_pointer_node; } | |
2848 | ||
2849 | void | |
2850 | do_export(Export* exp) const | |
2851 | { exp->write_c_string("nil"); } | |
2852 | }; | |
2853 | ||
2854 | // Import a nil expression. | |
2855 | ||
2856 | Expression* | |
2857 | Nil_expression::do_import(Import* imp) | |
2858 | { | |
2859 | imp->require_c_string("nil"); | |
2860 | return Expression::make_nil(imp->location()); | |
2861 | } | |
2862 | ||
2863 | // Make a nil expression. | |
2864 | ||
2865 | Expression* | |
2866 | Expression::make_nil(source_location location) | |
2867 | { | |
2868 | return new Nil_expression(location); | |
2869 | } | |
2870 | ||
2871 | // The value of the predeclared constant iota. This is little more | |
2872 | // than a marker. This will be lowered to an integer in | |
2873 | // Const_expression::do_lower, which is where we know the value that | |
2874 | // it should have. | |
2875 | ||
2876 | class Iota_expression : public Parser_expression | |
2877 | { | |
2878 | public: | |
2879 | Iota_expression(source_location location) | |
2880 | : Parser_expression(EXPRESSION_IOTA, location) | |
2881 | { } | |
2882 | ||
2883 | protected: | |
2884 | Expression* | |
2885 | do_lower(Gogo*, Named_object*, int) | |
2886 | { gcc_unreachable(); } | |
2887 | ||
2888 | // There should only ever be one of these. | |
2889 | Expression* | |
2890 | do_copy() | |
2891 | { gcc_unreachable(); } | |
2892 | }; | |
2893 | ||
2894 | // Make an iota expression. This is only called for one case: the | |
2895 | // value of the predeclared constant iota. | |
2896 | ||
2897 | Expression* | |
2898 | Expression::make_iota() | |
2899 | { | |
2900 | static Iota_expression iota_expression(UNKNOWN_LOCATION); | |
2901 | return &iota_expression; | |
2902 | } | |
2903 | ||
2904 | // A type conversion expression. | |
2905 | ||
2906 | class Type_conversion_expression : public Expression | |
2907 | { | |
2908 | public: | |
2909 | Type_conversion_expression(Type* type, Expression* expr, | |
2910 | source_location location) | |
2911 | : Expression(EXPRESSION_CONVERSION, location), | |
2912 | type_(type), expr_(expr), may_convert_function_types_(false) | |
2913 | { } | |
2914 | ||
2915 | // Return the type to which we are converting. | |
2916 | Type* | |
2917 | type() const | |
2918 | { return this->type_; } | |
2919 | ||
2920 | // Return the expression which we are converting. | |
2921 | Expression* | |
2922 | expr() const | |
2923 | { return this->expr_; } | |
2924 | ||
2925 | // Permit converting from one function type to another. This is | |
2926 | // used internally for method expressions. | |
2927 | void | |
2928 | set_may_convert_function_types() | |
2929 | { | |
2930 | this->may_convert_function_types_ = true; | |
2931 | } | |
2932 | ||
2933 | // Import a type conversion expression. | |
2934 | static Expression* | |
2935 | do_import(Import*); | |
2936 | ||
2937 | protected: | |
2938 | int | |
2939 | do_traverse(Traverse* traverse); | |
2940 | ||
2941 | Expression* | |
2942 | do_lower(Gogo*, Named_object*, int); | |
2943 | ||
2944 | bool | |
2945 | do_is_constant() const | |
2946 | { return this->expr_->is_constant(); } | |
2947 | ||
2948 | bool | |
2949 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
2950 | ||
2951 | bool | |
2952 | do_float_constant_value(mpfr_t, Type**) const; | |
2953 | ||
2954 | bool | |
2955 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
2956 | ||
2957 | bool | |
2958 | do_string_constant_value(std::string*) const; | |
2959 | ||
2960 | Type* | |
2961 | do_type() | |
2962 | { return this->type_; } | |
2963 | ||
2964 | void | |
2965 | do_determine_type(const Type_context*) | |
2966 | { | |
2967 | Type_context subcontext(this->type_, false); | |
2968 | this->expr_->determine_type(&subcontext); | |
2969 | } | |
2970 | ||
2971 | void | |
2972 | do_check_types(Gogo*); | |
2973 | ||
2974 | Expression* | |
2975 | do_copy() | |
2976 | { | |
2977 | return new Type_conversion_expression(this->type_, this->expr_->copy(), | |
2978 | this->location()); | |
2979 | } | |
2980 | ||
2981 | tree | |
2982 | do_get_tree(Translate_context* context); | |
2983 | ||
2984 | void | |
2985 | do_export(Export*) const; | |
2986 | ||
2987 | private: | |
2988 | // The type to convert to. | |
2989 | Type* type_; | |
2990 | // The expression to convert. | |
2991 | Expression* expr_; | |
2992 | // True if this is permitted to convert function types. This is | |
2993 | // used internally for method expressions. | |
2994 | bool may_convert_function_types_; | |
2995 | }; | |
2996 | ||
2997 | // Traversal. | |
2998 | ||
2999 | int | |
3000 | Type_conversion_expression::do_traverse(Traverse* traverse) | |
3001 | { | |
3002 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3003 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3004 | return TRAVERSE_EXIT; | |
3005 | return TRAVERSE_CONTINUE; | |
3006 | } | |
3007 | ||
3008 | // Convert to a constant at lowering time. | |
3009 | ||
3010 | Expression* | |
3011 | Type_conversion_expression::do_lower(Gogo*, Named_object*, int) | |
3012 | { | |
3013 | Type* type = this->type_; | |
3014 | Expression* val = this->expr_; | |
3015 | source_location location = this->location(); | |
3016 | ||
3017 | if (type->integer_type() != NULL) | |
3018 | { | |
3019 | mpz_t ival; | |
3020 | mpz_init(ival); | |
3021 | Type* dummy; | |
3022 | if (val->integer_constant_value(false, ival, &dummy)) | |
3023 | { | |
3024 | if (!Integer_expression::check_constant(ival, type, location)) | |
3025 | mpz_set_ui(ival, 0); | |
3026 | Expression* ret = Expression::make_integer(&ival, type, location); | |
3027 | mpz_clear(ival); | |
3028 | return ret; | |
3029 | } | |
3030 | ||
3031 | mpfr_t fval; | |
3032 | mpfr_init(fval); | |
3033 | if (val->float_constant_value(fval, &dummy)) | |
3034 | { | |
3035 | if (!mpfr_integer_p(fval)) | |
3036 | { | |
3037 | error_at(location, | |
3038 | "floating point constant truncated to integer"); | |
3039 | return Expression::make_error(location); | |
3040 | } | |
3041 | mpfr_get_z(ival, fval, GMP_RNDN); | |
3042 | if (!Integer_expression::check_constant(ival, type, location)) | |
3043 | mpz_set_ui(ival, 0); | |
3044 | Expression* ret = Expression::make_integer(&ival, type, location); | |
3045 | mpfr_clear(fval); | |
3046 | mpz_clear(ival); | |
3047 | return ret; | |
3048 | } | |
3049 | mpfr_clear(fval); | |
3050 | mpz_clear(ival); | |
3051 | } | |
3052 | ||
3053 | if (type->float_type() != NULL) | |
3054 | { | |
3055 | mpfr_t fval; | |
3056 | mpfr_init(fval); | |
3057 | Type* dummy; | |
3058 | if (val->float_constant_value(fval, &dummy)) | |
3059 | { | |
3060 | if (!Float_expression::check_constant(fval, type, location)) | |
3061 | mpfr_set_ui(fval, 0, GMP_RNDN); | |
3062 | Float_expression::constrain_float(fval, type); | |
3063 | Expression *ret = Expression::make_float(&fval, type, location); | |
3064 | mpfr_clear(fval); | |
3065 | return ret; | |
3066 | } | |
3067 | mpfr_clear(fval); | |
3068 | } | |
3069 | ||
3070 | if (type->complex_type() != NULL) | |
3071 | { | |
3072 | mpfr_t real; | |
3073 | mpfr_t imag; | |
3074 | mpfr_init(real); | |
3075 | mpfr_init(imag); | |
3076 | Type* dummy; | |
3077 | if (val->complex_constant_value(real, imag, &dummy)) | |
3078 | { | |
3079 | if (!Complex_expression::check_constant(real, imag, type, location)) | |
3080 | { | |
3081 | mpfr_set_ui(real, 0, GMP_RNDN); | |
3082 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
3083 | } | |
3084 | Complex_expression::constrain_complex(real, imag, type); | |
3085 | Expression* ret = Expression::make_complex(&real, &imag, type, | |
3086 | location); | |
3087 | mpfr_clear(real); | |
3088 | mpfr_clear(imag); | |
3089 | return ret; | |
3090 | } | |
3091 | mpfr_clear(real); | |
3092 | mpfr_clear(imag); | |
3093 | } | |
3094 | ||
3095 | if (type->is_open_array_type() && type->named_type() == NULL) | |
3096 | { | |
3097 | Type* element_type = type->array_type()->element_type()->forwarded(); | |
3098 | bool is_byte = element_type == Type::lookup_integer_type("uint8"); | |
3099 | bool is_int = element_type == Type::lookup_integer_type("int"); | |
3100 | if (is_byte || is_int) | |
3101 | { | |
3102 | std::string s; | |
3103 | if (val->string_constant_value(&s)) | |
3104 | { | |
3105 | Expression_list* vals = new Expression_list(); | |
3106 | if (is_byte) | |
3107 | { | |
3108 | for (std::string::const_iterator p = s.begin(); | |
3109 | p != s.end(); | |
3110 | p++) | |
3111 | { | |
3112 | mpz_t val; | |
3113 | mpz_init_set_ui(val, static_cast<unsigned char>(*p)); | |
3114 | Expression* v = Expression::make_integer(&val, | |
3115 | element_type, | |
3116 | location); | |
3117 | vals->push_back(v); | |
3118 | mpz_clear(val); | |
3119 | } | |
3120 | } | |
3121 | else | |
3122 | { | |
3123 | const char *p = s.data(); | |
3124 | const char *pend = s.data() + s.length(); | |
3125 | while (p < pend) | |
3126 | { | |
3127 | unsigned int c; | |
3128 | int adv = Lex::fetch_char(p, &c); | |
3129 | if (adv == 0) | |
3130 | { | |
3131 | warning_at(this->location(), 0, | |
3132 | "invalid UTF-8 encoding"); | |
3133 | adv = 1; | |
3134 | } | |
3135 | p += adv; | |
3136 | mpz_t val; | |
3137 | mpz_init_set_ui(val, c); | |
3138 | Expression* v = Expression::make_integer(&val, | |
3139 | element_type, | |
3140 | location); | |
3141 | vals->push_back(v); | |
3142 | mpz_clear(val); | |
3143 | } | |
3144 | } | |
3145 | ||
3146 | return Expression::make_slice_composite_literal(type, vals, | |
3147 | location); | |
3148 | } | |
3149 | } | |
3150 | } | |
3151 | ||
3152 | return this; | |
3153 | } | |
3154 | ||
3155 | // Return the constant integer value if there is one. | |
3156 | ||
3157 | bool | |
3158 | Type_conversion_expression::do_integer_constant_value(bool iota_is_constant, | |
3159 | mpz_t val, | |
3160 | Type** ptype) const | |
3161 | { | |
3162 | if (this->type_->integer_type() == NULL) | |
3163 | return false; | |
3164 | ||
3165 | mpz_t ival; | |
3166 | mpz_init(ival); | |
3167 | Type* dummy; | |
3168 | if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy)) | |
3169 | { | |
3170 | if (!Integer_expression::check_constant(ival, this->type_, | |
3171 | this->location())) | |
3172 | { | |
3173 | mpz_clear(ival); | |
3174 | return false; | |
3175 | } | |
3176 | mpz_set(val, ival); | |
3177 | mpz_clear(ival); | |
3178 | *ptype = this->type_; | |
3179 | return true; | |
3180 | } | |
3181 | mpz_clear(ival); | |
3182 | ||
3183 | mpfr_t fval; | |
3184 | mpfr_init(fval); | |
3185 | if (this->expr_->float_constant_value(fval, &dummy)) | |
3186 | { | |
3187 | mpfr_get_z(val, fval, GMP_RNDN); | |
3188 | mpfr_clear(fval); | |
3189 | if (!Integer_expression::check_constant(val, this->type_, | |
3190 | this->location())) | |
3191 | return false; | |
3192 | *ptype = this->type_; | |
3193 | return true; | |
3194 | } | |
3195 | mpfr_clear(fval); | |
3196 | ||
3197 | return false; | |
3198 | } | |
3199 | ||
3200 | // Return the constant floating point value if there is one. | |
3201 | ||
3202 | bool | |
3203 | Type_conversion_expression::do_float_constant_value(mpfr_t val, | |
3204 | Type** ptype) const | |
3205 | { | |
3206 | if (this->type_->float_type() == NULL) | |
3207 | return false; | |
3208 | ||
3209 | mpfr_t fval; | |
3210 | mpfr_init(fval); | |
3211 | Type* dummy; | |
3212 | if (this->expr_->float_constant_value(fval, &dummy)) | |
3213 | { | |
3214 | if (!Float_expression::check_constant(fval, this->type_, | |
3215 | this->location())) | |
3216 | { | |
3217 | mpfr_clear(fval); | |
3218 | return false; | |
3219 | } | |
3220 | mpfr_set(val, fval, GMP_RNDN); | |
3221 | mpfr_clear(fval); | |
3222 | Float_expression::constrain_float(val, this->type_); | |
3223 | *ptype = this->type_; | |
3224 | return true; | |
3225 | } | |
3226 | mpfr_clear(fval); | |
3227 | ||
3228 | return false; | |
3229 | } | |
3230 | ||
3231 | // Return the constant complex value if there is one. | |
3232 | ||
3233 | bool | |
3234 | Type_conversion_expression::do_complex_constant_value(mpfr_t real, | |
3235 | mpfr_t imag, | |
3236 | Type **ptype) const | |
3237 | { | |
3238 | if (this->type_->complex_type() == NULL) | |
3239 | return false; | |
3240 | ||
3241 | mpfr_t rval; | |
3242 | mpfr_t ival; | |
3243 | mpfr_init(rval); | |
3244 | mpfr_init(ival); | |
3245 | Type* dummy; | |
3246 | if (this->expr_->complex_constant_value(rval, ival, &dummy)) | |
3247 | { | |
3248 | if (!Complex_expression::check_constant(rval, ival, this->type_, | |
3249 | this->location())) | |
3250 | { | |
3251 | mpfr_clear(rval); | |
3252 | mpfr_clear(ival); | |
3253 | return false; | |
3254 | } | |
3255 | mpfr_set(real, rval, GMP_RNDN); | |
3256 | mpfr_set(imag, ival, GMP_RNDN); | |
3257 | mpfr_clear(rval); | |
3258 | mpfr_clear(ival); | |
3259 | Complex_expression::constrain_complex(real, imag, this->type_); | |
3260 | *ptype = this->type_; | |
3261 | return true; | |
3262 | } | |
3263 | mpfr_clear(rval); | |
3264 | mpfr_clear(ival); | |
3265 | ||
3266 | return false; | |
3267 | } | |
3268 | ||
3269 | // Return the constant string value if there is one. | |
3270 | ||
3271 | bool | |
3272 | Type_conversion_expression::do_string_constant_value(std::string* val) const | |
3273 | { | |
3274 | if (this->type_->is_string_type() | |
3275 | && this->expr_->type()->integer_type() != NULL) | |
3276 | { | |
3277 | mpz_t ival; | |
3278 | mpz_init(ival); | |
3279 | Type* dummy; | |
3280 | if (this->expr_->integer_constant_value(false, ival, &dummy)) | |
3281 | { | |
3282 | unsigned long ulval = mpz_get_ui(ival); | |
3283 | if (mpz_cmp_ui(ival, ulval) == 0) | |
3284 | { | |
3285 | Lex::append_char(ulval, true, val, this->location()); | |
3286 | mpz_clear(ival); | |
3287 | return true; | |
3288 | } | |
3289 | } | |
3290 | mpz_clear(ival); | |
3291 | } | |
3292 | ||
3293 | // FIXME: Could handle conversion from const []int here. | |
3294 | ||
3295 | return false; | |
3296 | } | |
3297 | ||
3298 | // Check that types are convertible. | |
3299 | ||
3300 | void | |
3301 | Type_conversion_expression::do_check_types(Gogo*) | |
3302 | { | |
3303 | Type* type = this->type_; | |
3304 | Type* expr_type = this->expr_->type(); | |
3305 | std::string reason; | |
3306 | ||
5c13bd80 | 3307 | if (type->is_error() || expr_type->is_error()) |
842f6425 | 3308 | { |
842f6425 | 3309 | this->set_is_error(); |
3310 | return; | |
3311 | } | |
3312 | ||
e440a328 | 3313 | if (this->may_convert_function_types_ |
3314 | && type->function_type() != NULL | |
3315 | && expr_type->function_type() != NULL) | |
3316 | return; | |
3317 | ||
3318 | if (Type::are_convertible(type, expr_type, &reason)) | |
3319 | return; | |
3320 | ||
3321 | error_at(this->location(), "%s", reason.c_str()); | |
3322 | this->set_is_error(); | |
3323 | } | |
3324 | ||
3325 | // Get a tree for a type conversion. | |
3326 | ||
3327 | tree | |
3328 | Type_conversion_expression::do_get_tree(Translate_context* context) | |
3329 | { | |
3330 | Gogo* gogo = context->gogo(); | |
3331 | tree type_tree = this->type_->get_tree(gogo); | |
3332 | tree expr_tree = this->expr_->get_tree(context); | |
3333 | ||
3334 | if (type_tree == error_mark_node | |
3335 | || expr_tree == error_mark_node | |
3336 | || TREE_TYPE(expr_tree) == error_mark_node) | |
3337 | return error_mark_node; | |
3338 | ||
3339 | if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree))) | |
3340 | return fold_convert(type_tree, expr_tree); | |
3341 | ||
3342 | Type* type = this->type_; | |
3343 | Type* expr_type = this->expr_->type(); | |
3344 | tree ret; | |
3345 | if (type->interface_type() != NULL || expr_type->interface_type() != NULL) | |
3346 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3347 | expr_tree, this->location()); | |
3348 | else if (type->integer_type() != NULL) | |
3349 | { | |
3350 | if (expr_type->integer_type() != NULL | |
3351 | || expr_type->float_type() != NULL | |
3352 | || expr_type->is_unsafe_pointer_type()) | |
3353 | ret = fold(convert_to_integer(type_tree, expr_tree)); | |
3354 | else | |
3355 | gcc_unreachable(); | |
3356 | } | |
3357 | else if (type->float_type() != NULL) | |
3358 | { | |
3359 | if (expr_type->integer_type() != NULL | |
3360 | || expr_type->float_type() != NULL) | |
3361 | ret = fold(convert_to_real(type_tree, expr_tree)); | |
3362 | else | |
3363 | gcc_unreachable(); | |
3364 | } | |
3365 | else if (type->complex_type() != NULL) | |
3366 | { | |
3367 | if (expr_type->complex_type() != NULL) | |
3368 | ret = fold(convert_to_complex(type_tree, expr_tree)); | |
3369 | else | |
3370 | gcc_unreachable(); | |
3371 | } | |
3372 | else if (type->is_string_type() | |
3373 | && expr_type->integer_type() != NULL) | |
3374 | { | |
3375 | expr_tree = fold_convert(integer_type_node, expr_tree); | |
3376 | if (host_integerp(expr_tree, 0)) | |
3377 | { | |
3378 | HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0); | |
3379 | std::string s; | |
3380 | Lex::append_char(intval, true, &s, this->location()); | |
3381 | Expression* se = Expression::make_string(s, this->location()); | |
3382 | return se->get_tree(context); | |
3383 | } | |
3384 | ||
3385 | static tree int_to_string_fndecl; | |
3386 | ret = Gogo::call_builtin(&int_to_string_fndecl, | |
3387 | this->location(), | |
3388 | "__go_int_to_string", | |
3389 | 1, | |
3390 | type_tree, | |
3391 | integer_type_node, | |
3392 | fold_convert(integer_type_node, expr_tree)); | |
3393 | } | |
3394 | else if (type->is_string_type() | |
3395 | && (expr_type->array_type() != NULL | |
3396 | || (expr_type->points_to() != NULL | |
3397 | && expr_type->points_to()->array_type() != NULL))) | |
3398 | { | |
3399 | Type* t = expr_type; | |
3400 | if (t->points_to() != NULL) | |
3401 | { | |
3402 | t = t->points_to(); | |
3403 | expr_tree = build_fold_indirect_ref(expr_tree); | |
3404 | } | |
3405 | if (!DECL_P(expr_tree)) | |
3406 | expr_tree = save_expr(expr_tree); | |
3407 | Array_type* a = t->array_type(); | |
3408 | Type* e = a->element_type()->forwarded(); | |
3409 | gcc_assert(e->integer_type() != NULL); | |
3410 | tree valptr = fold_convert(const_ptr_type_node, | |
3411 | a->value_pointer_tree(gogo, expr_tree)); | |
3412 | tree len = a->length_tree(gogo, expr_tree); | |
9581e91d | 3413 | len = fold_convert_loc(this->location(), integer_type_node, len); |
e440a328 | 3414 | if (e->integer_type()->is_unsigned() |
3415 | && e->integer_type()->bits() == 8) | |
3416 | { | |
3417 | static tree byte_array_to_string_fndecl; | |
3418 | ret = Gogo::call_builtin(&byte_array_to_string_fndecl, | |
3419 | this->location(), | |
3420 | "__go_byte_array_to_string", | |
3421 | 2, | |
3422 | type_tree, | |
3423 | const_ptr_type_node, | |
3424 | valptr, | |
9581e91d | 3425 | integer_type_node, |
e440a328 | 3426 | len); |
3427 | } | |
3428 | else | |
3429 | { | |
3430 | gcc_assert(e == Type::lookup_integer_type("int")); | |
3431 | static tree int_array_to_string_fndecl; | |
3432 | ret = Gogo::call_builtin(&int_array_to_string_fndecl, | |
3433 | this->location(), | |
3434 | "__go_int_array_to_string", | |
3435 | 2, | |
3436 | type_tree, | |
3437 | const_ptr_type_node, | |
3438 | valptr, | |
9581e91d | 3439 | integer_type_node, |
e440a328 | 3440 | len); |
3441 | } | |
3442 | } | |
3443 | else if (type->is_open_array_type() && expr_type->is_string_type()) | |
3444 | { | |
3445 | Type* e = type->array_type()->element_type()->forwarded(); | |
3446 | gcc_assert(e->integer_type() != NULL); | |
3447 | if (e->integer_type()->is_unsigned() | |
3448 | && e->integer_type()->bits() == 8) | |
3449 | { | |
3450 | static tree string_to_byte_array_fndecl; | |
3451 | ret = Gogo::call_builtin(&string_to_byte_array_fndecl, | |
3452 | this->location(), | |
3453 | "__go_string_to_byte_array", | |
3454 | 1, | |
3455 | type_tree, | |
3456 | TREE_TYPE(expr_tree), | |
3457 | expr_tree); | |
3458 | } | |
3459 | else | |
3460 | { | |
3461 | gcc_assert(e == Type::lookup_integer_type("int")); | |
3462 | static tree string_to_int_array_fndecl; | |
3463 | ret = Gogo::call_builtin(&string_to_int_array_fndecl, | |
3464 | this->location(), | |
3465 | "__go_string_to_int_array", | |
3466 | 1, | |
3467 | type_tree, | |
3468 | TREE_TYPE(expr_tree), | |
3469 | expr_tree); | |
3470 | } | |
3471 | } | |
3472 | else if ((type->is_unsafe_pointer_type() | |
3473 | && expr_type->points_to() != NULL) | |
3474 | || (expr_type->is_unsafe_pointer_type() | |
3475 | && type->points_to() != NULL)) | |
3476 | ret = fold_convert(type_tree, expr_tree); | |
3477 | else if (type->is_unsafe_pointer_type() | |
3478 | && expr_type->integer_type() != NULL) | |
3479 | ret = convert_to_pointer(type_tree, expr_tree); | |
3480 | else if (this->may_convert_function_types_ | |
3481 | && type->function_type() != NULL | |
3482 | && expr_type->function_type() != NULL) | |
3483 | ret = fold_convert_loc(this->location(), type_tree, expr_tree); | |
3484 | else | |
3485 | ret = Expression::convert_for_assignment(context, type, expr_type, | |
3486 | expr_tree, this->location()); | |
3487 | ||
3488 | return ret; | |
3489 | } | |
3490 | ||
3491 | // Output a type conversion in a constant expression. | |
3492 | ||
3493 | void | |
3494 | Type_conversion_expression::do_export(Export* exp) const | |
3495 | { | |
3496 | exp->write_c_string("convert("); | |
3497 | exp->write_type(this->type_); | |
3498 | exp->write_c_string(", "); | |
3499 | this->expr_->export_expression(exp); | |
3500 | exp->write_c_string(")"); | |
3501 | } | |
3502 | ||
3503 | // Import a type conversion or a struct construction. | |
3504 | ||
3505 | Expression* | |
3506 | Type_conversion_expression::do_import(Import* imp) | |
3507 | { | |
3508 | imp->require_c_string("convert("); | |
3509 | Type* type = imp->read_type(); | |
3510 | imp->require_c_string(", "); | |
3511 | Expression* val = Expression::import_expression(imp); | |
3512 | imp->require_c_string(")"); | |
3513 | return Expression::make_cast(type, val, imp->location()); | |
3514 | } | |
3515 | ||
3516 | // Make a type cast expression. | |
3517 | ||
3518 | Expression* | |
3519 | Expression::make_cast(Type* type, Expression* val, source_location location) | |
3520 | { | |
3521 | if (type->is_error_type() || val->is_error_expression()) | |
3522 | return Expression::make_error(location); | |
3523 | return new Type_conversion_expression(type, val, location); | |
3524 | } | |
3525 | ||
9581e91d | 3526 | // An unsafe type conversion, used to pass values to builtin functions. |
3527 | ||
3528 | class Unsafe_type_conversion_expression : public Expression | |
3529 | { | |
3530 | public: | |
3531 | Unsafe_type_conversion_expression(Type* type, Expression* expr, | |
3532 | source_location location) | |
3533 | : Expression(EXPRESSION_UNSAFE_CONVERSION, location), | |
3534 | type_(type), expr_(expr) | |
3535 | { } | |
3536 | ||
3537 | protected: | |
3538 | int | |
3539 | do_traverse(Traverse* traverse); | |
3540 | ||
3541 | Type* | |
3542 | do_type() | |
3543 | { return this->type_; } | |
3544 | ||
3545 | void | |
3546 | do_determine_type(const Type_context*) | |
3547 | { } | |
3548 | ||
3549 | Expression* | |
3550 | do_copy() | |
3551 | { | |
3552 | return new Unsafe_type_conversion_expression(this->type_, | |
3553 | this->expr_->copy(), | |
3554 | this->location()); | |
3555 | } | |
3556 | ||
3557 | tree | |
3558 | do_get_tree(Translate_context*); | |
3559 | ||
3560 | private: | |
3561 | // The type to convert to. | |
3562 | Type* type_; | |
3563 | // The expression to convert. | |
3564 | Expression* expr_; | |
3565 | }; | |
3566 | ||
3567 | // Traversal. | |
3568 | ||
3569 | int | |
3570 | Unsafe_type_conversion_expression::do_traverse(Traverse* traverse) | |
3571 | { | |
3572 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
3573 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
3574 | return TRAVERSE_EXIT; | |
3575 | return TRAVERSE_CONTINUE; | |
3576 | } | |
3577 | ||
3578 | // Convert to backend representation. | |
3579 | ||
3580 | tree | |
3581 | Unsafe_type_conversion_expression::do_get_tree(Translate_context* context) | |
3582 | { | |
3583 | // We are only called for a limited number of cases. | |
3584 | ||
3585 | Type* t = this->type_; | |
3586 | Type* et = this->expr_->type(); | |
3587 | ||
3588 | tree type_tree = this->type_->get_tree(context->gogo()); | |
3589 | tree expr_tree = this->expr_->get_tree(context); | |
3590 | if (type_tree == error_mark_node || expr_tree == error_mark_node) | |
3591 | return error_mark_node; | |
3592 | ||
3593 | source_location loc = this->location(); | |
3594 | ||
3595 | bool use_view_convert = false; | |
3596 | if (t->is_open_array_type()) | |
3597 | { | |
3598 | gcc_assert(et->is_open_array_type()); | |
3599 | use_view_convert = true; | |
3600 | } | |
3601 | else if (t->map_type() != NULL) | |
3602 | gcc_assert(et->map_type() != NULL); | |
3603 | else if (t->channel_type() != NULL) | |
3604 | gcc_assert(et->channel_type() != NULL); | |
3605 | else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL) | |
de0e0814 | 3606 | gcc_assert((et->points_to() != NULL |
3607 | && et->points_to()->channel_type() != NULL) | |
3608 | || et->is_nil_type()); | |
9581e91d | 3609 | else if (t->is_unsafe_pointer_type()) |
de0e0814 | 3610 | gcc_assert(et->points_to() != NULL || et->is_nil_type()); |
9581e91d | 3611 | else if (et->is_unsafe_pointer_type()) |
3612 | gcc_assert(t->points_to() != NULL); | |
3613 | else if (t->interface_type() != NULL && !t->interface_type()->is_empty()) | |
3614 | { | |
3615 | gcc_assert(et->interface_type() != NULL | |
3616 | && !et->interface_type()->is_empty()); | |
3617 | use_view_convert = true; | |
3618 | } | |
3619 | else if (t->interface_type() != NULL && t->interface_type()->is_empty()) | |
3620 | { | |
3621 | gcc_assert(et->interface_type() != NULL | |
3622 | && et->interface_type()->is_empty()); | |
3623 | use_view_convert = true; | |
3624 | } | |
588e3cf9 | 3625 | else if (t->integer_type() != NULL) |
3626 | { | |
3627 | gcc_assert(et->is_boolean_type() | |
3628 | || et->integer_type() != NULL | |
3629 | || et->function_type() != NULL | |
3630 | || et->points_to() != NULL | |
3631 | || et->map_type() != NULL | |
3632 | || et->channel_type() != NULL); | |
3633 | return convert_to_integer(type_tree, expr_tree); | |
3634 | } | |
9581e91d | 3635 | else |
3636 | gcc_unreachable(); | |
3637 | ||
3638 | if (use_view_convert) | |
3639 | return fold_build1_loc(loc, VIEW_CONVERT_EXPR, type_tree, expr_tree); | |
3640 | else | |
3641 | return fold_convert_loc(loc, type_tree, expr_tree); | |
3642 | } | |
3643 | ||
3644 | // Make an unsafe type conversion expression. | |
3645 | ||
3646 | Expression* | |
3647 | Expression::make_unsafe_cast(Type* type, Expression* expr, | |
3648 | source_location location) | |
3649 | { | |
3650 | return new Unsafe_type_conversion_expression(type, expr, location); | |
3651 | } | |
3652 | ||
e440a328 | 3653 | // Unary expressions. |
3654 | ||
3655 | class Unary_expression : public Expression | |
3656 | { | |
3657 | public: | |
3658 | Unary_expression(Operator op, Expression* expr, source_location location) | |
3659 | : Expression(EXPRESSION_UNARY, location), | |
3660 | op_(op), escapes_(true), expr_(expr) | |
3661 | { } | |
3662 | ||
3663 | // Return the operator. | |
3664 | Operator | |
3665 | op() const | |
3666 | { return this->op_; } | |
3667 | ||
3668 | // Return the operand. | |
3669 | Expression* | |
3670 | operand() const | |
3671 | { return this->expr_; } | |
3672 | ||
3673 | // Record that an address expression does not escape. | |
3674 | void | |
3675 | set_does_not_escape() | |
3676 | { | |
3677 | gcc_assert(this->op_ == OPERATOR_AND); | |
3678 | this->escapes_ = false; | |
3679 | } | |
3680 | ||
3681 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3682 | // could be done, false if not. | |
3683 | static bool | |
3684 | eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val, | |
3685 | source_location); | |
3686 | ||
3687 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3688 | // could be done, false if not. | |
3689 | static bool | |
3690 | eval_float(Operator op, mpfr_t uval, mpfr_t val); | |
3691 | ||
3692 | // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return | |
3693 | // true if this could be done, false if not. | |
3694 | static bool | |
3695 | eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real, | |
3696 | mpfr_t imag); | |
3697 | ||
3698 | static Expression* | |
3699 | do_import(Import*); | |
3700 | ||
3701 | protected: | |
3702 | int | |
3703 | do_traverse(Traverse* traverse) | |
3704 | { return Expression::traverse(&this->expr_, traverse); } | |
3705 | ||
3706 | Expression* | |
3707 | do_lower(Gogo*, Named_object*, int); | |
3708 | ||
3709 | bool | |
3710 | do_is_constant() const; | |
3711 | ||
3712 | bool | |
3713 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
3714 | ||
3715 | bool | |
3716 | do_float_constant_value(mpfr_t, Type**) const; | |
3717 | ||
3718 | bool | |
3719 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
3720 | ||
3721 | Type* | |
3722 | do_type(); | |
3723 | ||
3724 | void | |
3725 | do_determine_type(const Type_context*); | |
3726 | ||
3727 | void | |
3728 | do_check_types(Gogo*); | |
3729 | ||
3730 | Expression* | |
3731 | do_copy() | |
3732 | { | |
3733 | return Expression::make_unary(this->op_, this->expr_->copy(), | |
3734 | this->location()); | |
3735 | } | |
3736 | ||
3737 | bool | |
3738 | do_is_addressable() const | |
3739 | { return this->op_ == OPERATOR_MULT; } | |
3740 | ||
3741 | tree | |
3742 | do_get_tree(Translate_context*); | |
3743 | ||
3744 | void | |
3745 | do_export(Export*) const; | |
3746 | ||
3747 | private: | |
3748 | // The unary operator to apply. | |
3749 | Operator op_; | |
3750 | // Normally true. False if this is an address expression which does | |
3751 | // not escape the current function. | |
3752 | bool escapes_; | |
3753 | // The operand. | |
3754 | Expression* expr_; | |
3755 | }; | |
3756 | ||
3757 | // If we are taking the address of a composite literal, and the | |
3758 | // contents are not constant, then we want to make a heap composite | |
3759 | // instead. | |
3760 | ||
3761 | Expression* | |
3762 | Unary_expression::do_lower(Gogo*, Named_object*, int) | |
3763 | { | |
3764 | source_location loc = this->location(); | |
3765 | Operator op = this->op_; | |
3766 | Expression* expr = this->expr_; | |
3767 | ||
3768 | if (op == OPERATOR_MULT && expr->is_type_expression()) | |
3769 | return Expression::make_type(Type::make_pointer_type(expr->type()), loc); | |
3770 | ||
3771 | // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require | |
3772 | // moving x to the heap. FIXME: Is it worth doing a real escape | |
3773 | // analysis here? This case is found in math/unsafe.go and is | |
3774 | // therefore worth special casing. | |
3775 | if (op == OPERATOR_MULT) | |
3776 | { | |
3777 | Expression* e = expr; | |
3778 | while (e->classification() == EXPRESSION_CONVERSION) | |
3779 | { | |
3780 | Type_conversion_expression* te | |
3781 | = static_cast<Type_conversion_expression*>(e); | |
3782 | e = te->expr(); | |
3783 | } | |
3784 | ||
3785 | if (e->classification() == EXPRESSION_UNARY) | |
3786 | { | |
3787 | Unary_expression* ue = static_cast<Unary_expression*>(e); | |
3788 | if (ue->op_ == OPERATOR_AND) | |
3789 | { | |
3790 | if (e == expr) | |
3791 | { | |
3792 | // *&x == x. | |
3793 | return ue->expr_; | |
3794 | } | |
3795 | ue->set_does_not_escape(); | |
3796 | } | |
3797 | } | |
3798 | } | |
3799 | ||
55661ce9 | 3800 | // Catching an invalid indirection of unsafe.Pointer here avoid |
3801 | // having to deal with TYPE_VOID in other places. | |
3802 | if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type()) | |
3803 | { | |
3804 | error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>"); | |
3805 | return Expression::make_error(this->location()); | |
3806 | } | |
3807 | ||
e440a328 | 3808 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS |
3809 | || op == OPERATOR_NOT || op == OPERATOR_XOR) | |
3810 | { | |
3811 | Expression* ret = NULL; | |
3812 | ||
3813 | mpz_t eval; | |
3814 | mpz_init(eval); | |
3815 | Type* etype; | |
3816 | if (expr->integer_constant_value(false, eval, &etype)) | |
3817 | { | |
3818 | mpz_t val; | |
3819 | mpz_init(val); | |
3820 | if (Unary_expression::eval_integer(op, etype, eval, val, loc)) | |
3821 | ret = Expression::make_integer(&val, etype, loc); | |
3822 | mpz_clear(val); | |
3823 | } | |
3824 | mpz_clear(eval); | |
3825 | if (ret != NULL) | |
3826 | return ret; | |
3827 | ||
3828 | if (op == OPERATOR_PLUS || op == OPERATOR_MINUS) | |
3829 | { | |
3830 | mpfr_t fval; | |
3831 | mpfr_init(fval); | |
3832 | Type* ftype; | |
3833 | if (expr->float_constant_value(fval, &ftype)) | |
3834 | { | |
3835 | mpfr_t val; | |
3836 | mpfr_init(val); | |
3837 | if (Unary_expression::eval_float(op, fval, val)) | |
3838 | ret = Expression::make_float(&val, ftype, loc); | |
3839 | mpfr_clear(val); | |
3840 | } | |
3841 | if (ret != NULL) | |
3842 | { | |
3843 | mpfr_clear(fval); | |
3844 | return ret; | |
3845 | } | |
3846 | ||
3847 | mpfr_t ival; | |
3848 | mpfr_init(ival); | |
3849 | if (expr->complex_constant_value(fval, ival, &ftype)) | |
3850 | { | |
3851 | mpfr_t real; | |
3852 | mpfr_t imag; | |
3853 | mpfr_init(real); | |
3854 | mpfr_init(imag); | |
3855 | if (Unary_expression::eval_complex(op, fval, ival, real, imag)) | |
3856 | ret = Expression::make_complex(&real, &imag, ftype, loc); | |
3857 | mpfr_clear(real); | |
3858 | mpfr_clear(imag); | |
3859 | } | |
3860 | mpfr_clear(ival); | |
3861 | mpfr_clear(fval); | |
3862 | if (ret != NULL) | |
3863 | return ret; | |
3864 | } | |
3865 | } | |
3866 | ||
3867 | return this; | |
3868 | } | |
3869 | ||
3870 | // Return whether a unary expression is a constant. | |
3871 | ||
3872 | bool | |
3873 | Unary_expression::do_is_constant() const | |
3874 | { | |
3875 | if (this->op_ == OPERATOR_MULT) | |
3876 | { | |
3877 | // Indirecting through a pointer is only constant if the object | |
3878 | // to which the expression points is constant, but we currently | |
3879 | // have no way to determine that. | |
3880 | return false; | |
3881 | } | |
3882 | else if (this->op_ == OPERATOR_AND) | |
3883 | { | |
3884 | // Taking the address of a variable is constant if it is a | |
3885 | // global variable, not constant otherwise. In other cases | |
3886 | // taking the address is probably not a constant. | |
3887 | Var_expression* ve = this->expr_->var_expression(); | |
3888 | if (ve != NULL) | |
3889 | { | |
3890 | Named_object* no = ve->named_object(); | |
3891 | return no->is_variable() && no->var_value()->is_global(); | |
3892 | } | |
3893 | return false; | |
3894 | } | |
3895 | else | |
3896 | return this->expr_->is_constant(); | |
3897 | } | |
3898 | ||
3899 | // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of | |
3900 | // UVAL, if known; it may be NULL. Return true if this could be done, | |
3901 | // false if not. | |
3902 | ||
3903 | bool | |
3904 | Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val, | |
3905 | source_location location) | |
3906 | { | |
3907 | switch (op) | |
3908 | { | |
3909 | case OPERATOR_PLUS: | |
3910 | mpz_set(val, uval); | |
3911 | return true; | |
3912 | case OPERATOR_MINUS: | |
3913 | mpz_neg(val, uval); | |
3914 | return Integer_expression::check_constant(val, utype, location); | |
3915 | case OPERATOR_NOT: | |
3916 | mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0); | |
3917 | return true; | |
3918 | case OPERATOR_XOR: | |
3919 | if (utype == NULL | |
3920 | || utype->integer_type() == NULL | |
3921 | || utype->integer_type()->is_abstract()) | |
3922 | mpz_com(val, uval); | |
3923 | else | |
3924 | { | |
3925 | // The number of HOST_WIDE_INTs that it takes to represent | |
3926 | // UVAL. | |
3927 | size_t count = ((mpz_sizeinbase(uval, 2) | |
3928 | + HOST_BITS_PER_WIDE_INT | |
3929 | - 1) | |
3930 | / HOST_BITS_PER_WIDE_INT); | |
3931 | ||
3932 | unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count]; | |
3933 | memset(phwi, 0, count * sizeof(HOST_WIDE_INT)); | |
3934 | ||
3935 | size_t ecount; | |
3936 | mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval); | |
3937 | gcc_assert(ecount <= count); | |
3938 | ||
3939 | // Trim down to the number of words required by the type. | |
3940 | size_t obits = utype->integer_type()->bits(); | |
3941 | if (!utype->integer_type()->is_unsigned()) | |
3942 | ++obits; | |
3943 | size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1) | |
3944 | / HOST_BITS_PER_WIDE_INT); | |
714722b6 | 3945 | gcc_assert(ocount <= count); |
e440a328 | 3946 | |
3947 | for (size_t i = 0; i < ocount; ++i) | |
3948 | phwi[i] = ~phwi[i]; | |
3949 | ||
3950 | size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits; | |
3951 | if (clearbits != 0) | |
3952 | phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1) | |
3953 | >> clearbits); | |
3954 | ||
3955 | mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi); | |
3956 | ||
3957 | delete[] phwi; | |
3958 | } | |
3959 | return Integer_expression::check_constant(val, utype, location); | |
3960 | case OPERATOR_AND: | |
3961 | case OPERATOR_MULT: | |
3962 | return false; | |
3963 | default: | |
3964 | gcc_unreachable(); | |
3965 | } | |
3966 | } | |
3967 | ||
3968 | // Apply unary opcode OP to UVAL, setting VAL. Return true if this | |
3969 | // could be done, false if not. | |
3970 | ||
3971 | bool | |
3972 | Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val) | |
3973 | { | |
3974 | switch (op) | |
3975 | { | |
3976 | case OPERATOR_PLUS: | |
3977 | mpfr_set(val, uval, GMP_RNDN); | |
3978 | return true; | |
3979 | case OPERATOR_MINUS: | |
3980 | mpfr_neg(val, uval, GMP_RNDN); | |
3981 | return true; | |
3982 | case OPERATOR_NOT: | |
3983 | case OPERATOR_XOR: | |
3984 | case OPERATOR_AND: | |
3985 | case OPERATOR_MULT: | |
3986 | return false; | |
3987 | default: | |
3988 | gcc_unreachable(); | |
3989 | } | |
3990 | } | |
3991 | ||
3992 | // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true | |
3993 | // if this could be done, false if not. | |
3994 | ||
3995 | bool | |
3996 | Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival, | |
3997 | mpfr_t real, mpfr_t imag) | |
3998 | { | |
3999 | switch (op) | |
4000 | { | |
4001 | case OPERATOR_PLUS: | |
4002 | mpfr_set(real, rval, GMP_RNDN); | |
4003 | mpfr_set(imag, ival, GMP_RNDN); | |
4004 | return true; | |
4005 | case OPERATOR_MINUS: | |
4006 | mpfr_neg(real, rval, GMP_RNDN); | |
4007 | mpfr_neg(imag, ival, GMP_RNDN); | |
4008 | return true; | |
4009 | case OPERATOR_NOT: | |
4010 | case OPERATOR_XOR: | |
4011 | case OPERATOR_AND: | |
4012 | case OPERATOR_MULT: | |
4013 | return false; | |
4014 | default: | |
4015 | gcc_unreachable(); | |
4016 | } | |
4017 | } | |
4018 | ||
4019 | // Return the integral constant value of a unary expression, if it has one. | |
4020 | ||
4021 | bool | |
4022 | Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
4023 | Type** ptype) const | |
4024 | { | |
4025 | mpz_t uval; | |
4026 | mpz_init(uval); | |
4027 | bool ret; | |
4028 | if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype)) | |
4029 | ret = false; | |
4030 | else | |
4031 | ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val, | |
4032 | this->location()); | |
4033 | mpz_clear(uval); | |
4034 | return ret; | |
4035 | } | |
4036 | ||
4037 | // Return the floating point constant value of a unary expression, if | |
4038 | // it has one. | |
4039 | ||
4040 | bool | |
4041 | Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
4042 | { | |
4043 | mpfr_t uval; | |
4044 | mpfr_init(uval); | |
4045 | bool ret; | |
4046 | if (!this->expr_->float_constant_value(uval, ptype)) | |
4047 | ret = false; | |
4048 | else | |
4049 | ret = Unary_expression::eval_float(this->op_, uval, val); | |
4050 | mpfr_clear(uval); | |
4051 | return ret; | |
4052 | } | |
4053 | ||
4054 | // Return the complex constant value of a unary expression, if it has | |
4055 | // one. | |
4056 | ||
4057 | bool | |
4058 | Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
4059 | Type** ptype) const | |
4060 | { | |
4061 | mpfr_t rval; | |
4062 | mpfr_t ival; | |
4063 | mpfr_init(rval); | |
4064 | mpfr_init(ival); | |
4065 | bool ret; | |
4066 | if (!this->expr_->complex_constant_value(rval, ival, ptype)) | |
4067 | ret = false; | |
4068 | else | |
4069 | ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag); | |
4070 | mpfr_clear(rval); | |
4071 | mpfr_clear(ival); | |
4072 | return ret; | |
4073 | } | |
4074 | ||
4075 | // Return the type of a unary expression. | |
4076 | ||
4077 | Type* | |
4078 | Unary_expression::do_type() | |
4079 | { | |
4080 | switch (this->op_) | |
4081 | { | |
4082 | case OPERATOR_PLUS: | |
4083 | case OPERATOR_MINUS: | |
4084 | case OPERATOR_NOT: | |
4085 | case OPERATOR_XOR: | |
4086 | return this->expr_->type(); | |
4087 | ||
4088 | case OPERATOR_AND: | |
4089 | return Type::make_pointer_type(this->expr_->type()); | |
4090 | ||
4091 | case OPERATOR_MULT: | |
4092 | { | |
4093 | Type* subtype = this->expr_->type(); | |
4094 | Type* points_to = subtype->points_to(); | |
4095 | if (points_to == NULL) | |
4096 | return Type::make_error_type(); | |
4097 | return points_to; | |
4098 | } | |
4099 | ||
4100 | default: | |
4101 | gcc_unreachable(); | |
4102 | } | |
4103 | } | |
4104 | ||
4105 | // Determine abstract types for a unary expression. | |
4106 | ||
4107 | void | |
4108 | Unary_expression::do_determine_type(const Type_context* context) | |
4109 | { | |
4110 | switch (this->op_) | |
4111 | { | |
4112 | case OPERATOR_PLUS: | |
4113 | case OPERATOR_MINUS: | |
4114 | case OPERATOR_NOT: | |
4115 | case OPERATOR_XOR: | |
4116 | this->expr_->determine_type(context); | |
4117 | break; | |
4118 | ||
4119 | case OPERATOR_AND: | |
4120 | // Taking the address of something. | |
4121 | { | |
4122 | Type* subtype = (context->type == NULL | |
4123 | ? NULL | |
4124 | : context->type->points_to()); | |
4125 | Type_context subcontext(subtype, false); | |
4126 | this->expr_->determine_type(&subcontext); | |
4127 | } | |
4128 | break; | |
4129 | ||
4130 | case OPERATOR_MULT: | |
4131 | // Indirecting through a pointer. | |
4132 | { | |
4133 | Type* subtype = (context->type == NULL | |
4134 | ? NULL | |
4135 | : Type::make_pointer_type(context->type)); | |
4136 | Type_context subcontext(subtype, false); | |
4137 | this->expr_->determine_type(&subcontext); | |
4138 | } | |
4139 | break; | |
4140 | ||
4141 | default: | |
4142 | gcc_unreachable(); | |
4143 | } | |
4144 | } | |
4145 | ||
4146 | // Check types for a unary expression. | |
4147 | ||
4148 | void | |
4149 | Unary_expression::do_check_types(Gogo*) | |
4150 | { | |
9fe897ef | 4151 | Type* type = this->expr_->type(); |
5c13bd80 | 4152 | if (type->is_error()) |
9fe897ef | 4153 | { |
4154 | this->set_is_error(); | |
4155 | return; | |
4156 | } | |
4157 | ||
e440a328 | 4158 | switch (this->op_) |
4159 | { | |
4160 | case OPERATOR_PLUS: | |
4161 | case OPERATOR_MINUS: | |
9fe897ef | 4162 | if (type->integer_type() == NULL |
4163 | && type->float_type() == NULL | |
4164 | && type->complex_type() == NULL) | |
4165 | this->report_error(_("expected numeric type")); | |
e440a328 | 4166 | break; |
4167 | ||
4168 | case OPERATOR_NOT: | |
4169 | case OPERATOR_XOR: | |
9fe897ef | 4170 | if (type->integer_type() == NULL |
4171 | && !type->is_boolean_type()) | |
4172 | this->report_error(_("expected integer or boolean type")); | |
e440a328 | 4173 | break; |
4174 | ||
4175 | case OPERATOR_AND: | |
4176 | if (!this->expr_->is_addressable()) | |
4177 | this->report_error(_("invalid operand for unary %<&%>")); | |
4178 | else | |
4179 | this->expr_->address_taken(this->escapes_); | |
4180 | break; | |
4181 | ||
4182 | case OPERATOR_MULT: | |
4183 | // Indirecting through a pointer. | |
9fe897ef | 4184 | if (type->points_to() == NULL) |
4185 | this->report_error(_("expected pointer")); | |
e440a328 | 4186 | break; |
4187 | ||
4188 | default: | |
4189 | gcc_unreachable(); | |
4190 | } | |
4191 | } | |
4192 | ||
4193 | // Get a tree for a unary expression. | |
4194 | ||
4195 | tree | |
4196 | Unary_expression::do_get_tree(Translate_context* context) | |
4197 | { | |
4198 | tree expr = this->expr_->get_tree(context); | |
4199 | if (expr == error_mark_node) | |
4200 | return error_mark_node; | |
4201 | ||
4202 | source_location loc = this->location(); | |
4203 | switch (this->op_) | |
4204 | { | |
4205 | case OPERATOR_PLUS: | |
4206 | return expr; | |
4207 | ||
4208 | case OPERATOR_MINUS: | |
4209 | { | |
4210 | tree type = TREE_TYPE(expr); | |
4211 | tree compute_type = excess_precision_type(type); | |
4212 | if (compute_type != NULL_TREE) | |
4213 | expr = ::convert(compute_type, expr); | |
4214 | tree ret = fold_build1_loc(loc, NEGATE_EXPR, | |
4215 | (compute_type != NULL_TREE | |
4216 | ? compute_type | |
4217 | : type), | |
4218 | expr); | |
4219 | if (compute_type != NULL_TREE) | |
4220 | ret = ::convert(type, ret); | |
4221 | return ret; | |
4222 | } | |
4223 | ||
4224 | case OPERATOR_NOT: | |
4225 | if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE) | |
4226 | return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr); | |
4227 | else | |
4228 | return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr, | |
4229 | build_int_cst(TREE_TYPE(expr), 0)); | |
4230 | ||
4231 | case OPERATOR_XOR: | |
4232 | return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr); | |
4233 | ||
4234 | case OPERATOR_AND: | |
4235 | // We should not see a non-constant constructor here; cases | |
4236 | // where we would see one should have been moved onto the heap | |
4237 | // at parse time. Taking the address of a nonconstant | |
4238 | // constructor will not do what the programmer expects. | |
4239 | gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr)); | |
4240 | gcc_assert(TREE_CODE(expr) != ADDR_EXPR); | |
4241 | ||
4242 | // Build a decl for a constant constructor. | |
4243 | if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr)) | |
4244 | { | |
4245 | tree decl = build_decl(this->location(), VAR_DECL, | |
4246 | create_tmp_var_name("C"), TREE_TYPE(expr)); | |
4247 | DECL_EXTERNAL(decl) = 0; | |
4248 | TREE_PUBLIC(decl) = 0; | |
4249 | TREE_READONLY(decl) = 1; | |
4250 | TREE_CONSTANT(decl) = 1; | |
4251 | TREE_STATIC(decl) = 1; | |
4252 | TREE_ADDRESSABLE(decl) = 1; | |
4253 | DECL_ARTIFICIAL(decl) = 1; | |
4254 | DECL_INITIAL(decl) = expr; | |
4255 | rest_of_decl_compilation(decl, 1, 0); | |
4256 | expr = decl; | |
4257 | } | |
4258 | ||
4259 | return build_fold_addr_expr_loc(loc, expr); | |
4260 | ||
4261 | case OPERATOR_MULT: | |
4262 | { | |
4263 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr))); | |
4264 | ||
4265 | // If we are dereferencing the pointer to a large struct, we | |
4266 | // need to check for nil. We don't bother to check for small | |
4267 | // structs because we expect the system to crash on a nil | |
4268 | // pointer dereference. | |
4269 | HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr))); | |
4270 | if (s == -1 || s >= 4096) | |
4271 | { | |
4272 | if (!DECL_P(expr)) | |
4273 | expr = save_expr(expr); | |
4274 | tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node, | |
4275 | expr, | |
4276 | fold_convert(TREE_TYPE(expr), | |
4277 | null_pointer_node)); | |
4278 | tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, | |
4279 | loc); | |
4280 | expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr), | |
4281 | build3(COND_EXPR, void_type_node, | |
4282 | compare, crash, NULL_TREE), | |
4283 | expr); | |
4284 | } | |
4285 | ||
4286 | // If the type of EXPR is a recursive pointer type, then we | |
4287 | // need to insert a cast before indirecting. | |
4288 | if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node) | |
4289 | { | |
4290 | Type* pt = this->expr_->type()->points_to(); | |
4291 | tree ind = pt->get_tree(context->gogo()); | |
4292 | expr = fold_convert_loc(loc, build_pointer_type(ind), expr); | |
4293 | } | |
4294 | ||
4295 | return build_fold_indirect_ref_loc(loc, expr); | |
4296 | } | |
4297 | ||
4298 | default: | |
4299 | gcc_unreachable(); | |
4300 | } | |
4301 | } | |
4302 | ||
4303 | // Export a unary expression. | |
4304 | ||
4305 | void | |
4306 | Unary_expression::do_export(Export* exp) const | |
4307 | { | |
4308 | switch (this->op_) | |
4309 | { | |
4310 | case OPERATOR_PLUS: | |
4311 | exp->write_c_string("+ "); | |
4312 | break; | |
4313 | case OPERATOR_MINUS: | |
4314 | exp->write_c_string("- "); | |
4315 | break; | |
4316 | case OPERATOR_NOT: | |
4317 | exp->write_c_string("! "); | |
4318 | break; | |
4319 | case OPERATOR_XOR: | |
4320 | exp->write_c_string("^ "); | |
4321 | break; | |
4322 | case OPERATOR_AND: | |
4323 | case OPERATOR_MULT: | |
4324 | default: | |
4325 | gcc_unreachable(); | |
4326 | } | |
4327 | this->expr_->export_expression(exp); | |
4328 | } | |
4329 | ||
4330 | // Import a unary expression. | |
4331 | ||
4332 | Expression* | |
4333 | Unary_expression::do_import(Import* imp) | |
4334 | { | |
4335 | Operator op; | |
4336 | switch (imp->get_char()) | |
4337 | { | |
4338 | case '+': | |
4339 | op = OPERATOR_PLUS; | |
4340 | break; | |
4341 | case '-': | |
4342 | op = OPERATOR_MINUS; | |
4343 | break; | |
4344 | case '!': | |
4345 | op = OPERATOR_NOT; | |
4346 | break; | |
4347 | case '^': | |
4348 | op = OPERATOR_XOR; | |
4349 | break; | |
4350 | default: | |
4351 | gcc_unreachable(); | |
4352 | } | |
4353 | imp->require_c_string(" "); | |
4354 | Expression* expr = Expression::import_expression(imp); | |
4355 | return Expression::make_unary(op, expr, imp->location()); | |
4356 | } | |
4357 | ||
4358 | // Make a unary expression. | |
4359 | ||
4360 | Expression* | |
4361 | Expression::make_unary(Operator op, Expression* expr, source_location location) | |
4362 | { | |
4363 | return new Unary_expression(op, expr, location); | |
4364 | } | |
4365 | ||
4366 | // If this is an indirection through a pointer, return the expression | |
4367 | // being pointed through. Otherwise return this. | |
4368 | ||
4369 | Expression* | |
4370 | Expression::deref() | |
4371 | { | |
4372 | if (this->classification_ == EXPRESSION_UNARY) | |
4373 | { | |
4374 | Unary_expression* ue = static_cast<Unary_expression*>(this); | |
4375 | if (ue->op() == OPERATOR_MULT) | |
4376 | return ue->operand(); | |
4377 | } | |
4378 | return this; | |
4379 | } | |
4380 | ||
4381 | // Class Binary_expression. | |
4382 | ||
4383 | // Traversal. | |
4384 | ||
4385 | int | |
4386 | Binary_expression::do_traverse(Traverse* traverse) | |
4387 | { | |
4388 | int t = Expression::traverse(&this->left_, traverse); | |
4389 | if (t == TRAVERSE_EXIT) | |
4390 | return TRAVERSE_EXIT; | |
4391 | return Expression::traverse(&this->right_, traverse); | |
4392 | } | |
4393 | ||
4394 | // Compare integer constants according to OP. | |
4395 | ||
4396 | bool | |
4397 | Binary_expression::compare_integer(Operator op, mpz_t left_val, | |
4398 | mpz_t right_val) | |
4399 | { | |
4400 | int i = mpz_cmp(left_val, right_val); | |
4401 | switch (op) | |
4402 | { | |
4403 | case OPERATOR_EQEQ: | |
4404 | return i == 0; | |
4405 | case OPERATOR_NOTEQ: | |
4406 | return i != 0; | |
4407 | case OPERATOR_LT: | |
4408 | return i < 0; | |
4409 | case OPERATOR_LE: | |
4410 | return i <= 0; | |
4411 | case OPERATOR_GT: | |
4412 | return i > 0; | |
4413 | case OPERATOR_GE: | |
4414 | return i >= 0; | |
4415 | default: | |
4416 | gcc_unreachable(); | |
4417 | } | |
4418 | } | |
4419 | ||
4420 | // Compare floating point constants according to OP. | |
4421 | ||
4422 | bool | |
4423 | Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val, | |
4424 | mpfr_t right_val) | |
4425 | { | |
4426 | int i; | |
4427 | if (type == NULL) | |
4428 | i = mpfr_cmp(left_val, right_val); | |
4429 | else | |
4430 | { | |
4431 | mpfr_t lv; | |
4432 | mpfr_init_set(lv, left_val, GMP_RNDN); | |
4433 | mpfr_t rv; | |
4434 | mpfr_init_set(rv, right_val, GMP_RNDN); | |
4435 | Float_expression::constrain_float(lv, type); | |
4436 | Float_expression::constrain_float(rv, type); | |
4437 | i = mpfr_cmp(lv, rv); | |
4438 | mpfr_clear(lv); | |
4439 | mpfr_clear(rv); | |
4440 | } | |
4441 | switch (op) | |
4442 | { | |
4443 | case OPERATOR_EQEQ: | |
4444 | return i == 0; | |
4445 | case OPERATOR_NOTEQ: | |
4446 | return i != 0; | |
4447 | case OPERATOR_LT: | |
4448 | return i < 0; | |
4449 | case OPERATOR_LE: | |
4450 | return i <= 0; | |
4451 | case OPERATOR_GT: | |
4452 | return i > 0; | |
4453 | case OPERATOR_GE: | |
4454 | return i >= 0; | |
4455 | default: | |
4456 | gcc_unreachable(); | |
4457 | } | |
4458 | } | |
4459 | ||
4460 | // Compare complex constants according to OP. Complex numbers may | |
4461 | // only be compared for equality. | |
4462 | ||
4463 | bool | |
4464 | Binary_expression::compare_complex(Operator op, Type* type, | |
4465 | mpfr_t left_real, mpfr_t left_imag, | |
4466 | mpfr_t right_real, mpfr_t right_imag) | |
4467 | { | |
4468 | bool is_equal; | |
4469 | if (type == NULL) | |
4470 | is_equal = (mpfr_cmp(left_real, right_real) == 0 | |
4471 | && mpfr_cmp(left_imag, right_imag) == 0); | |
4472 | else | |
4473 | { | |
4474 | mpfr_t lr; | |
4475 | mpfr_t li; | |
4476 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4477 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4478 | mpfr_t rr; | |
4479 | mpfr_t ri; | |
4480 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4481 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4482 | Complex_expression::constrain_complex(lr, li, type); | |
4483 | Complex_expression::constrain_complex(rr, ri, type); | |
4484 | is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0; | |
4485 | mpfr_clear(lr); | |
4486 | mpfr_clear(li); | |
4487 | mpfr_clear(rr); | |
4488 | mpfr_clear(ri); | |
4489 | } | |
4490 | switch (op) | |
4491 | { | |
4492 | case OPERATOR_EQEQ: | |
4493 | return is_equal; | |
4494 | case OPERATOR_NOTEQ: | |
4495 | return !is_equal; | |
4496 | default: | |
4497 | gcc_unreachable(); | |
4498 | } | |
4499 | } | |
4500 | ||
4501 | // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. | |
4502 | // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of | |
4503 | // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if | |
4504 | // this could be done, false if not. | |
4505 | ||
4506 | bool | |
4507 | Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val, | |
4508 | Type* right_type, mpz_t right_val, | |
4509 | source_location location, mpz_t val) | |
4510 | { | |
4511 | bool is_shift_op = false; | |
4512 | switch (op) | |
4513 | { | |
4514 | case OPERATOR_OROR: | |
4515 | case OPERATOR_ANDAND: | |
4516 | case OPERATOR_EQEQ: | |
4517 | case OPERATOR_NOTEQ: | |
4518 | case OPERATOR_LT: | |
4519 | case OPERATOR_LE: | |
4520 | case OPERATOR_GT: | |
4521 | case OPERATOR_GE: | |
4522 | // These return boolean values. We should probably handle them | |
4523 | // anyhow in case a type conversion is used on the result. | |
4524 | return false; | |
4525 | case OPERATOR_PLUS: | |
4526 | mpz_add(val, left_val, right_val); | |
4527 | break; | |
4528 | case OPERATOR_MINUS: | |
4529 | mpz_sub(val, left_val, right_val); | |
4530 | break; | |
4531 | case OPERATOR_OR: | |
4532 | mpz_ior(val, left_val, right_val); | |
4533 | break; | |
4534 | case OPERATOR_XOR: | |
4535 | mpz_xor(val, left_val, right_val); | |
4536 | break; | |
4537 | case OPERATOR_MULT: | |
4538 | mpz_mul(val, left_val, right_val); | |
4539 | break; | |
4540 | case OPERATOR_DIV: | |
4541 | if (mpz_sgn(right_val) != 0) | |
4542 | mpz_tdiv_q(val, left_val, right_val); | |
4543 | else | |
4544 | { | |
4545 | error_at(location, "division by zero"); | |
4546 | mpz_set_ui(val, 0); | |
4547 | return true; | |
4548 | } | |
4549 | break; | |
4550 | case OPERATOR_MOD: | |
4551 | if (mpz_sgn(right_val) != 0) | |
4552 | mpz_tdiv_r(val, left_val, right_val); | |
4553 | else | |
4554 | { | |
4555 | error_at(location, "division by zero"); | |
4556 | mpz_set_ui(val, 0); | |
4557 | return true; | |
4558 | } | |
4559 | break; | |
4560 | case OPERATOR_LSHIFT: | |
4561 | { | |
4562 | unsigned long shift = mpz_get_ui(right_val); | |
a28c1598 | 4563 | if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000) |
e440a328 | 4564 | { |
4565 | error_at(location, "shift count overflow"); | |
4566 | mpz_set_ui(val, 0); | |
4567 | return true; | |
4568 | } | |
4569 | mpz_mul_2exp(val, left_val, shift); | |
4570 | is_shift_op = true; | |
4571 | break; | |
4572 | } | |
4573 | break; | |
4574 | case OPERATOR_RSHIFT: | |
4575 | { | |
4576 | unsigned long shift = mpz_get_ui(right_val); | |
4577 | if (mpz_cmp_ui(right_val, shift) != 0) | |
4578 | { | |
4579 | error_at(location, "shift count overflow"); | |
4580 | mpz_set_ui(val, 0); | |
4581 | return true; | |
4582 | } | |
4583 | if (mpz_cmp_ui(left_val, 0) >= 0) | |
4584 | mpz_tdiv_q_2exp(val, left_val, shift); | |
4585 | else | |
4586 | mpz_fdiv_q_2exp(val, left_val, shift); | |
4587 | is_shift_op = true; | |
4588 | break; | |
4589 | } | |
4590 | break; | |
4591 | case OPERATOR_AND: | |
4592 | mpz_and(val, left_val, right_val); | |
4593 | break; | |
4594 | case OPERATOR_BITCLEAR: | |
4595 | { | |
4596 | mpz_t tval; | |
4597 | mpz_init(tval); | |
4598 | mpz_com(tval, right_val); | |
4599 | mpz_and(val, left_val, tval); | |
4600 | mpz_clear(tval); | |
4601 | } | |
4602 | break; | |
4603 | default: | |
4604 | gcc_unreachable(); | |
4605 | } | |
4606 | ||
4607 | Type* type = left_type; | |
4608 | if (!is_shift_op) | |
4609 | { | |
4610 | if (type == NULL) | |
4611 | type = right_type; | |
4612 | else if (type != right_type && right_type != NULL) | |
4613 | { | |
4614 | if (type->is_abstract()) | |
4615 | type = right_type; | |
4616 | else if (!right_type->is_abstract()) | |
4617 | { | |
4618 | // This look like a type error which should be diagnosed | |
4619 | // elsewhere. Don't do anything here, to avoid an | |
4620 | // unhelpful chain of error messages. | |
4621 | return true; | |
4622 | } | |
4623 | } | |
4624 | } | |
4625 | ||
4626 | if (type != NULL && !type->is_abstract()) | |
4627 | { | |
4628 | // We have to check the operands too, as we have implicitly | |
4629 | // coerced them to TYPE. | |
4630 | if ((type != left_type | |
4631 | && !Integer_expression::check_constant(left_val, type, location)) | |
4632 | || (!is_shift_op | |
4633 | && type != right_type | |
4634 | && !Integer_expression::check_constant(right_val, type, | |
4635 | location)) | |
4636 | || !Integer_expression::check_constant(val, type, location)) | |
4637 | mpz_set_ui(val, 0); | |
4638 | } | |
4639 | ||
4640 | return true; | |
4641 | } | |
4642 | ||
4643 | // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL. | |
4644 | // Return true if this could be done, false if not. | |
4645 | ||
4646 | bool | |
4647 | Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val, | |
4648 | Type* right_type, mpfr_t right_val, | |
4649 | mpfr_t val, source_location location) | |
4650 | { | |
4651 | switch (op) | |
4652 | { | |
4653 | case OPERATOR_OROR: | |
4654 | case OPERATOR_ANDAND: | |
4655 | case OPERATOR_EQEQ: | |
4656 | case OPERATOR_NOTEQ: | |
4657 | case OPERATOR_LT: | |
4658 | case OPERATOR_LE: | |
4659 | case OPERATOR_GT: | |
4660 | case OPERATOR_GE: | |
4661 | // These return boolean values. We should probably handle them | |
4662 | // anyhow in case a type conversion is used on the result. | |
4663 | return false; | |
4664 | case OPERATOR_PLUS: | |
4665 | mpfr_add(val, left_val, right_val, GMP_RNDN); | |
4666 | break; | |
4667 | case OPERATOR_MINUS: | |
4668 | mpfr_sub(val, left_val, right_val, GMP_RNDN); | |
4669 | break; | |
4670 | case OPERATOR_OR: | |
4671 | case OPERATOR_XOR: | |
4672 | case OPERATOR_AND: | |
4673 | case OPERATOR_BITCLEAR: | |
4674 | return false; | |
4675 | case OPERATOR_MULT: | |
4676 | mpfr_mul(val, left_val, right_val, GMP_RNDN); | |
4677 | break; | |
4678 | case OPERATOR_DIV: | |
4679 | if (mpfr_zero_p(right_val)) | |
4680 | error_at(location, "division by zero"); | |
4681 | mpfr_div(val, left_val, right_val, GMP_RNDN); | |
4682 | break; | |
4683 | case OPERATOR_MOD: | |
4684 | return false; | |
4685 | case OPERATOR_LSHIFT: | |
4686 | case OPERATOR_RSHIFT: | |
4687 | return false; | |
4688 | default: | |
4689 | gcc_unreachable(); | |
4690 | } | |
4691 | ||
4692 | Type* type = left_type; | |
4693 | if (type == NULL) | |
4694 | type = right_type; | |
4695 | else if (type != right_type && right_type != NULL) | |
4696 | { | |
4697 | if (type->is_abstract()) | |
4698 | type = right_type; | |
4699 | else if (!right_type->is_abstract()) | |
4700 | { | |
4701 | // This looks like a type error which should be diagnosed | |
4702 | // elsewhere. Don't do anything here, to avoid an unhelpful | |
4703 | // chain of error messages. | |
4704 | return true; | |
4705 | } | |
4706 | } | |
4707 | ||
4708 | if (type != NULL && !type->is_abstract()) | |
4709 | { | |
4710 | if ((type != left_type | |
4711 | && !Float_expression::check_constant(left_val, type, location)) | |
4712 | || (type != right_type | |
4713 | && !Float_expression::check_constant(right_val, type, | |
4714 | location)) | |
4715 | || !Float_expression::check_constant(val, type, location)) | |
4716 | mpfr_set_ui(val, 0, GMP_RNDN); | |
4717 | } | |
4718 | ||
4719 | return true; | |
4720 | } | |
4721 | ||
4722 | // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and | |
4723 | // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this | |
4724 | // could be done, false if not. | |
4725 | ||
4726 | bool | |
4727 | Binary_expression::eval_complex(Operator op, Type* left_type, | |
4728 | mpfr_t left_real, mpfr_t left_imag, | |
4729 | Type *right_type, | |
4730 | mpfr_t right_real, mpfr_t right_imag, | |
4731 | mpfr_t real, mpfr_t imag, | |
4732 | source_location location) | |
4733 | { | |
4734 | switch (op) | |
4735 | { | |
4736 | case OPERATOR_OROR: | |
4737 | case OPERATOR_ANDAND: | |
4738 | case OPERATOR_EQEQ: | |
4739 | case OPERATOR_NOTEQ: | |
4740 | case OPERATOR_LT: | |
4741 | case OPERATOR_LE: | |
4742 | case OPERATOR_GT: | |
4743 | case OPERATOR_GE: | |
4744 | // These return boolean values and must be handled differently. | |
4745 | return false; | |
4746 | case OPERATOR_PLUS: | |
4747 | mpfr_add(real, left_real, right_real, GMP_RNDN); | |
4748 | mpfr_add(imag, left_imag, right_imag, GMP_RNDN); | |
4749 | break; | |
4750 | case OPERATOR_MINUS: | |
4751 | mpfr_sub(real, left_real, right_real, GMP_RNDN); | |
4752 | mpfr_sub(imag, left_imag, right_imag, GMP_RNDN); | |
4753 | break; | |
4754 | case OPERATOR_OR: | |
4755 | case OPERATOR_XOR: | |
4756 | case OPERATOR_AND: | |
4757 | case OPERATOR_BITCLEAR: | |
4758 | return false; | |
4759 | case OPERATOR_MULT: | |
4760 | { | |
4761 | // You might think that multiplying two complex numbers would | |
4762 | // be simple, and you would be right, until you start to think | |
4763 | // about getting the right answer for infinity. If one | |
4764 | // operand here is infinity and the other is anything other | |
4765 | // than zero or NaN, then we are going to wind up subtracting | |
4766 | // two infinity values. That will give us a NaN, but the | |
4767 | // correct answer is infinity. | |
4768 | ||
4769 | mpfr_t lrrr; | |
4770 | mpfr_init(lrrr); | |
4771 | mpfr_mul(lrrr, left_real, right_real, GMP_RNDN); | |
4772 | ||
4773 | mpfr_t lrri; | |
4774 | mpfr_init(lrri); | |
4775 | mpfr_mul(lrri, left_real, right_imag, GMP_RNDN); | |
4776 | ||
4777 | mpfr_t lirr; | |
4778 | mpfr_init(lirr); | |
4779 | mpfr_mul(lirr, left_imag, right_real, GMP_RNDN); | |
4780 | ||
4781 | mpfr_t liri; | |
4782 | mpfr_init(liri); | |
4783 | mpfr_mul(liri, left_imag, right_imag, GMP_RNDN); | |
4784 | ||
4785 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4786 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4787 | ||
4788 | // If we get NaN on both sides, check whether it should really | |
4789 | // be infinity. The rule is that if either side of the | |
4790 | // complex number is infinity, then the whole value is | |
4791 | // infinity, even if the other side is NaN. So the only case | |
4792 | // we have to fix is the one in which both sides are NaN. | |
4793 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4794 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4795 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4796 | { | |
4797 | bool is_infinity = false; | |
4798 | ||
4799 | mpfr_t lr; | |
4800 | mpfr_t li; | |
4801 | mpfr_init_set(lr, left_real, GMP_RNDN); | |
4802 | mpfr_init_set(li, left_imag, GMP_RNDN); | |
4803 | ||
4804 | mpfr_t rr; | |
4805 | mpfr_t ri; | |
4806 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4807 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4808 | ||
4809 | // If the left side is infinity, then the result is | |
4810 | // infinity. | |
4811 | if (mpfr_inf_p(lr) || mpfr_inf_p(li)) | |
4812 | { | |
4813 | mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN); | |
4814 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4815 | mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN); | |
4816 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4817 | if (mpfr_nan_p(rr)) | |
4818 | { | |
4819 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4820 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4821 | } | |
4822 | if (mpfr_nan_p(ri)) | |
4823 | { | |
4824 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4825 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4826 | } | |
4827 | is_infinity = true; | |
4828 | } | |
4829 | ||
4830 | // If the right side is infinity, then the result is | |
4831 | // infinity. | |
4832 | if (mpfr_inf_p(rr) || mpfr_inf_p(ri)) | |
4833 | { | |
4834 | mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
4835 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4836 | mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
4837 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4838 | if (mpfr_nan_p(lr)) | |
4839 | { | |
4840 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4841 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4842 | } | |
4843 | if (mpfr_nan_p(li)) | |
4844 | { | |
4845 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4846 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4847 | } | |
4848 | is_infinity = true; | |
4849 | } | |
4850 | ||
4851 | // If we got an overflow in the intermediate computations, | |
4852 | // then the result is infinity. | |
4853 | if (!is_infinity | |
4854 | && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri) | |
4855 | || mpfr_inf_p(lirr) || mpfr_inf_p(liri))) | |
4856 | { | |
4857 | if (mpfr_nan_p(lr)) | |
4858 | { | |
4859 | mpfr_set_ui(lr, 0, GMP_RNDN); | |
4860 | mpfr_copysign(lr, lr, left_real, GMP_RNDN); | |
4861 | } | |
4862 | if (mpfr_nan_p(li)) | |
4863 | { | |
4864 | mpfr_set_ui(li, 0, GMP_RNDN); | |
4865 | mpfr_copysign(li, li, left_imag, GMP_RNDN); | |
4866 | } | |
4867 | if (mpfr_nan_p(rr)) | |
4868 | { | |
4869 | mpfr_set_ui(rr, 0, GMP_RNDN); | |
4870 | mpfr_copysign(rr, rr, right_real, GMP_RNDN); | |
4871 | } | |
4872 | if (mpfr_nan_p(ri)) | |
4873 | { | |
4874 | mpfr_set_ui(ri, 0, GMP_RNDN); | |
4875 | mpfr_copysign(ri, ri, right_imag, GMP_RNDN); | |
4876 | } | |
4877 | is_infinity = true; | |
4878 | } | |
4879 | ||
4880 | if (is_infinity) | |
4881 | { | |
4882 | mpfr_mul(lrrr, lr, rr, GMP_RNDN); | |
4883 | mpfr_mul(lrri, lr, ri, GMP_RNDN); | |
4884 | mpfr_mul(lirr, li, rr, GMP_RNDN); | |
4885 | mpfr_mul(liri, li, ri, GMP_RNDN); | |
4886 | mpfr_sub(real, lrrr, liri, GMP_RNDN); | |
4887 | mpfr_add(imag, lrri, lirr, GMP_RNDN); | |
4888 | mpfr_set_inf(real, mpfr_sgn(real)); | |
4889 | mpfr_set_inf(imag, mpfr_sgn(imag)); | |
4890 | } | |
4891 | ||
4892 | mpfr_clear(lr); | |
4893 | mpfr_clear(li); | |
4894 | mpfr_clear(rr); | |
4895 | mpfr_clear(ri); | |
4896 | } | |
4897 | ||
4898 | mpfr_clear(lrrr); | |
4899 | mpfr_clear(lrri); | |
4900 | mpfr_clear(lirr); | |
4901 | mpfr_clear(liri); | |
4902 | } | |
4903 | break; | |
4904 | case OPERATOR_DIV: | |
4905 | { | |
4906 | // For complex division we want to avoid having an | |
4907 | // intermediate overflow turn the whole result in a NaN. We | |
4908 | // scale the values to try to avoid this. | |
4909 | ||
4910 | if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag)) | |
4911 | error_at(location, "division by zero"); | |
4912 | ||
4913 | mpfr_t rra; | |
4914 | mpfr_t ria; | |
4915 | mpfr_init(rra); | |
4916 | mpfr_init(ria); | |
4917 | mpfr_abs(rra, right_real, GMP_RNDN); | |
4918 | mpfr_abs(ria, right_imag, GMP_RNDN); | |
4919 | mpfr_t t; | |
4920 | mpfr_init(t); | |
4921 | mpfr_max(t, rra, ria, GMP_RNDN); | |
4922 | ||
4923 | mpfr_t rr; | |
4924 | mpfr_t ri; | |
4925 | mpfr_init_set(rr, right_real, GMP_RNDN); | |
4926 | mpfr_init_set(ri, right_imag, GMP_RNDN); | |
4927 | long ilogbw = 0; | |
4928 | if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t)) | |
4929 | { | |
4930 | ilogbw = mpfr_get_exp(t); | |
4931 | mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN); | |
4932 | mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN); | |
4933 | } | |
4934 | ||
4935 | mpfr_t denom; | |
4936 | mpfr_init(denom); | |
4937 | mpfr_mul(denom, rr, rr, GMP_RNDN); | |
4938 | mpfr_mul(t, ri, ri, GMP_RNDN); | |
4939 | mpfr_add(denom, denom, t, GMP_RNDN); | |
4940 | ||
4941 | mpfr_mul(real, left_real, rr, GMP_RNDN); | |
4942 | mpfr_mul(t, left_imag, ri, GMP_RNDN); | |
4943 | mpfr_add(real, real, t, GMP_RNDN); | |
4944 | mpfr_div(real, real, denom, GMP_RNDN); | |
4945 | mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN); | |
4946 | ||
4947 | mpfr_mul(imag, left_imag, rr, GMP_RNDN); | |
4948 | mpfr_mul(t, left_real, ri, GMP_RNDN); | |
4949 | mpfr_sub(imag, imag, t, GMP_RNDN); | |
4950 | mpfr_div(imag, imag, denom, GMP_RNDN); | |
4951 | mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN); | |
4952 | ||
4953 | // If we wind up with NaN on both sides, check whether we | |
4954 | // should really have infinity. The rule is that if either | |
4955 | // side of the complex number is infinity, then the whole | |
4956 | // value is infinity, even if the other side is NaN. So the | |
4957 | // only case we have to fix is the one in which both sides are | |
4958 | // NaN. | |
4959 | if (mpfr_nan_p(real) && mpfr_nan_p(imag) | |
4960 | && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag)) | |
4961 | && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag))) | |
4962 | { | |
4963 | if (mpfr_zero_p(denom)) | |
4964 | { | |
4965 | mpfr_set_inf(real, mpfr_sgn(rr)); | |
4966 | mpfr_mul(real, real, left_real, GMP_RNDN); | |
4967 | mpfr_set_inf(imag, mpfr_sgn(rr)); | |
4968 | mpfr_mul(imag, imag, left_imag, GMP_RNDN); | |
4969 | } | |
4970 | else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag)) | |
4971 | && mpfr_number_p(rr) && mpfr_number_p(ri)) | |
4972 | { | |
4973 | mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN); | |
4974 | mpfr_copysign(t, t, left_real, GMP_RNDN); | |
4975 | ||
4976 | mpfr_t t2; | |
4977 | mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN); | |
4978 | mpfr_copysign(t2, t2, left_imag, GMP_RNDN); | |
4979 | ||
4980 | mpfr_t t3; | |
4981 | mpfr_init(t3); | |
4982 | mpfr_mul(t3, t, rr, GMP_RNDN); | |
4983 | ||
4984 | mpfr_t t4; | |
4985 | mpfr_init(t4); | |
4986 | mpfr_mul(t4, t2, ri, GMP_RNDN); | |
4987 | ||
4988 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
4989 | mpfr_set_inf(real, mpfr_sgn(t3)); | |
4990 | ||
4991 | mpfr_mul(t3, t2, rr, GMP_RNDN); | |
4992 | mpfr_mul(t4, t, ri, GMP_RNDN); | |
4993 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
4994 | mpfr_set_inf(imag, mpfr_sgn(t3)); | |
4995 | ||
4996 | mpfr_clear(t2); | |
4997 | mpfr_clear(t3); | |
4998 | mpfr_clear(t4); | |
4999 | } | |
5000 | else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag)) | |
5001 | && mpfr_number_p(left_real) && mpfr_number_p(left_imag)) | |
5002 | { | |
5003 | mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN); | |
5004 | mpfr_copysign(t, t, rr, GMP_RNDN); | |
5005 | ||
5006 | mpfr_t t2; | |
5007 | mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN); | |
5008 | mpfr_copysign(t2, t2, ri, GMP_RNDN); | |
5009 | ||
5010 | mpfr_t t3; | |
5011 | mpfr_init(t3); | |
5012 | mpfr_mul(t3, left_real, t, GMP_RNDN); | |
5013 | ||
5014 | mpfr_t t4; | |
5015 | mpfr_init(t4); | |
5016 | mpfr_mul(t4, left_imag, t2, GMP_RNDN); | |
5017 | ||
5018 | mpfr_add(t3, t3, t4, GMP_RNDN); | |
5019 | mpfr_set_ui(real, 0, GMP_RNDN); | |
5020 | mpfr_mul(real, real, t3, GMP_RNDN); | |
5021 | ||
5022 | mpfr_mul(t3, left_imag, t, GMP_RNDN); | |
5023 | mpfr_mul(t4, left_real, t2, GMP_RNDN); | |
5024 | mpfr_sub(t3, t3, t4, GMP_RNDN); | |
5025 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
5026 | mpfr_mul(imag, imag, t3, GMP_RNDN); | |
5027 | ||
5028 | mpfr_clear(t2); | |
5029 | mpfr_clear(t3); | |
5030 | mpfr_clear(t4); | |
5031 | } | |
5032 | } | |
5033 | ||
5034 | mpfr_clear(denom); | |
5035 | mpfr_clear(rr); | |
5036 | mpfr_clear(ri); | |
5037 | mpfr_clear(t); | |
5038 | mpfr_clear(rra); | |
5039 | mpfr_clear(ria); | |
5040 | } | |
5041 | break; | |
5042 | case OPERATOR_MOD: | |
5043 | return false; | |
5044 | case OPERATOR_LSHIFT: | |
5045 | case OPERATOR_RSHIFT: | |
5046 | return false; | |
5047 | default: | |
5048 | gcc_unreachable(); | |
5049 | } | |
5050 | ||
5051 | Type* type = left_type; | |
5052 | if (type == NULL) | |
5053 | type = right_type; | |
5054 | else if (type != right_type && right_type != NULL) | |
5055 | { | |
5056 | if (type->is_abstract()) | |
5057 | type = right_type; | |
5058 | else if (!right_type->is_abstract()) | |
5059 | { | |
5060 | // This looks like a type error which should be diagnosed | |
5061 | // elsewhere. Don't do anything here, to avoid an unhelpful | |
5062 | // chain of error messages. | |
5063 | return true; | |
5064 | } | |
5065 | } | |
5066 | ||
5067 | if (type != NULL && !type->is_abstract()) | |
5068 | { | |
5069 | if ((type != left_type | |
5070 | && !Complex_expression::check_constant(left_real, left_imag, | |
5071 | type, location)) | |
5072 | || (type != right_type | |
5073 | && !Complex_expression::check_constant(right_real, right_imag, | |
5074 | type, location)) | |
5075 | || !Complex_expression::check_constant(real, imag, type, | |
5076 | location)) | |
5077 | { | |
5078 | mpfr_set_ui(real, 0, GMP_RNDN); | |
5079 | mpfr_set_ui(imag, 0, GMP_RNDN); | |
5080 | } | |
5081 | } | |
5082 | ||
5083 | return true; | |
5084 | } | |
5085 | ||
5086 | // Lower a binary expression. We have to evaluate constant | |
5087 | // expressions now, in order to implement Go's unlimited precision | |
5088 | // constants. | |
5089 | ||
5090 | Expression* | |
5091 | Binary_expression::do_lower(Gogo*, Named_object*, int) | |
5092 | { | |
5093 | source_location location = this->location(); | |
5094 | Operator op = this->op_; | |
5095 | Expression* left = this->left_; | |
5096 | Expression* right = this->right_; | |
5097 | ||
5098 | const bool is_comparison = (op == OPERATOR_EQEQ | |
5099 | || op == OPERATOR_NOTEQ | |
5100 | || op == OPERATOR_LT | |
5101 | || op == OPERATOR_LE | |
5102 | || op == OPERATOR_GT | |
5103 | || op == OPERATOR_GE); | |
5104 | ||
5105 | // Integer constant expressions. | |
5106 | { | |
5107 | mpz_t left_val; | |
5108 | mpz_init(left_val); | |
5109 | Type* left_type; | |
5110 | mpz_t right_val; | |
5111 | mpz_init(right_val); | |
5112 | Type* right_type; | |
5113 | if (left->integer_constant_value(false, left_val, &left_type) | |
5114 | && right->integer_constant_value(false, right_val, &right_type)) | |
5115 | { | |
5116 | Expression* ret = NULL; | |
5117 | if (left_type != right_type | |
5118 | && left_type != NULL | |
5119 | && right_type != NULL | |
5120 | && left_type->base() != right_type->base() | |
5121 | && op != OPERATOR_LSHIFT | |
5122 | && op != OPERATOR_RSHIFT) | |
5123 | { | |
5124 | // May be a type error--let it be diagnosed later. | |
5125 | } | |
5126 | else if (is_comparison) | |
5127 | { | |
5128 | bool b = Binary_expression::compare_integer(op, left_val, | |
5129 | right_val); | |
5130 | ret = Expression::make_cast(Type::lookup_bool_type(), | |
5131 | Expression::make_boolean(b, location), | |
5132 | location); | |
5133 | } | |
5134 | else | |
5135 | { | |
5136 | mpz_t val; | |
5137 | mpz_init(val); | |
5138 | ||
5139 | if (Binary_expression::eval_integer(op, left_type, left_val, | |
5140 | right_type, right_val, | |
5141 | location, val)) | |
5142 | { | |
5143 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND); | |
5144 | Type* type; | |
5145 | if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT) | |
5146 | type = left_type; | |
5147 | else if (left_type == NULL) | |
5148 | type = right_type; | |
5149 | else if (right_type == NULL) | |
5150 | type = left_type; | |
5151 | else if (!left_type->is_abstract() | |
5152 | && left_type->named_type() != NULL) | |
5153 | type = left_type; | |
5154 | else if (!right_type->is_abstract() | |
5155 | && right_type->named_type() != NULL) | |
5156 | type = right_type; | |
5157 | else if (!left_type->is_abstract()) | |
5158 | type = left_type; | |
5159 | else if (!right_type->is_abstract()) | |
5160 | type = right_type; | |
5161 | else if (left_type->float_type() != NULL) | |
5162 | type = left_type; | |
5163 | else if (right_type->float_type() != NULL) | |
5164 | type = right_type; | |
5165 | else if (left_type->complex_type() != NULL) | |
5166 | type = left_type; | |
5167 | else if (right_type->complex_type() != NULL) | |
5168 | type = right_type; | |
5169 | else | |
5170 | type = left_type; | |
5171 | ret = Expression::make_integer(&val, type, location); | |
5172 | } | |
5173 | ||
5174 | mpz_clear(val); | |
5175 | } | |
5176 | ||
5177 | if (ret != NULL) | |
5178 | { | |
5179 | mpz_clear(right_val); | |
5180 | mpz_clear(left_val); | |
5181 | return ret; | |
5182 | } | |
5183 | } | |
5184 | mpz_clear(right_val); | |
5185 | mpz_clear(left_val); | |
5186 | } | |
5187 | ||
5188 | // Floating point constant expressions. | |
5189 | { | |
5190 | mpfr_t left_val; | |
5191 | mpfr_init(left_val); | |
5192 | Type* left_type; | |
5193 | mpfr_t right_val; | |
5194 | mpfr_init(right_val); | |
5195 | Type* right_type; | |
5196 | if (left->float_constant_value(left_val, &left_type) | |
5197 | && right->float_constant_value(right_val, &right_type)) | |
5198 | { | |
5199 | Expression* ret = NULL; | |
5200 | if (left_type != right_type | |
5201 | && left_type != NULL | |
5202 | && right_type != NULL | |
5203 | && left_type->base() != right_type->base() | |
5204 | && op != OPERATOR_LSHIFT | |
5205 | && op != OPERATOR_RSHIFT) | |
5206 | { | |
5207 | // May be a type error--let it be diagnosed later. | |
5208 | } | |
5209 | else if (is_comparison) | |
5210 | { | |
5211 | bool b = Binary_expression::compare_float(op, | |
5212 | (left_type != NULL | |
5213 | ? left_type | |
5214 | : right_type), | |
5215 | left_val, right_val); | |
5216 | ret = Expression::make_boolean(b, location); | |
5217 | } | |
5218 | else | |
5219 | { | |
5220 | mpfr_t val; | |
5221 | mpfr_init(val); | |
5222 | ||
5223 | if (Binary_expression::eval_float(op, left_type, left_val, | |
5224 | right_type, right_val, val, | |
5225 | location)) | |
5226 | { | |
5227 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND | |
5228 | && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT); | |
5229 | Type* type; | |
5230 | if (left_type == NULL) | |
5231 | type = right_type; | |
5232 | else if (right_type == NULL) | |
5233 | type = left_type; | |
5234 | else if (!left_type->is_abstract() | |
5235 | && left_type->named_type() != NULL) | |
5236 | type = left_type; | |
5237 | else if (!right_type->is_abstract() | |
5238 | && right_type->named_type() != NULL) | |
5239 | type = right_type; | |
5240 | else if (!left_type->is_abstract()) | |
5241 | type = left_type; | |
5242 | else if (!right_type->is_abstract()) | |
5243 | type = right_type; | |
5244 | else if (left_type->float_type() != NULL) | |
5245 | type = left_type; | |
5246 | else if (right_type->float_type() != NULL) | |
5247 | type = right_type; | |
5248 | else | |
5249 | type = left_type; | |
5250 | ret = Expression::make_float(&val, type, location); | |
5251 | } | |
5252 | ||
5253 | mpfr_clear(val); | |
5254 | } | |
5255 | ||
5256 | if (ret != NULL) | |
5257 | { | |
5258 | mpfr_clear(right_val); | |
5259 | mpfr_clear(left_val); | |
5260 | return ret; | |
5261 | } | |
5262 | } | |
5263 | mpfr_clear(right_val); | |
5264 | mpfr_clear(left_val); | |
5265 | } | |
5266 | ||
5267 | // Complex constant expressions. | |
5268 | { | |
5269 | mpfr_t left_real; | |
5270 | mpfr_t left_imag; | |
5271 | mpfr_init(left_real); | |
5272 | mpfr_init(left_imag); | |
5273 | Type* left_type; | |
5274 | ||
5275 | mpfr_t right_real; | |
5276 | mpfr_t right_imag; | |
5277 | mpfr_init(right_real); | |
5278 | mpfr_init(right_imag); | |
5279 | Type* right_type; | |
5280 | ||
5281 | if (left->complex_constant_value(left_real, left_imag, &left_type) | |
5282 | && right->complex_constant_value(right_real, right_imag, &right_type)) | |
5283 | { | |
5284 | Expression* ret = NULL; | |
5285 | if (left_type != right_type | |
5286 | && left_type != NULL | |
5287 | && right_type != NULL | |
5288 | && left_type->base() != right_type->base()) | |
5289 | { | |
5290 | // May be a type error--let it be diagnosed later. | |
5291 | } | |
3b59603e | 5292 | else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ) |
e440a328 | 5293 | { |
5294 | bool b = Binary_expression::compare_complex(op, | |
5295 | (left_type != NULL | |
5296 | ? left_type | |
5297 | : right_type), | |
5298 | left_real, | |
5299 | left_imag, | |
5300 | right_real, | |
5301 | right_imag); | |
5302 | ret = Expression::make_boolean(b, location); | |
5303 | } | |
5304 | else | |
5305 | { | |
5306 | mpfr_t real; | |
5307 | mpfr_t imag; | |
5308 | mpfr_init(real); | |
5309 | mpfr_init(imag); | |
5310 | ||
5311 | if (Binary_expression::eval_complex(op, left_type, | |
5312 | left_real, left_imag, | |
5313 | right_type, | |
5314 | right_real, right_imag, | |
5315 | real, imag, | |
5316 | location)) | |
5317 | { | |
5318 | gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND | |
5319 | && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT); | |
5320 | Type* type; | |
5321 | if (left_type == NULL) | |
5322 | type = right_type; | |
5323 | else if (right_type == NULL) | |
5324 | type = left_type; | |
5325 | else if (!left_type->is_abstract() | |
5326 | && left_type->named_type() != NULL) | |
5327 | type = left_type; | |
5328 | else if (!right_type->is_abstract() | |
5329 | && right_type->named_type() != NULL) | |
5330 | type = right_type; | |
5331 | else if (!left_type->is_abstract()) | |
5332 | type = left_type; | |
5333 | else if (!right_type->is_abstract()) | |
5334 | type = right_type; | |
5335 | else if (left_type->complex_type() != NULL) | |
5336 | type = left_type; | |
5337 | else if (right_type->complex_type() != NULL) | |
5338 | type = right_type; | |
5339 | else | |
5340 | type = left_type; | |
5341 | ret = Expression::make_complex(&real, &imag, type, | |
5342 | location); | |
5343 | } | |
5344 | mpfr_clear(real); | |
5345 | mpfr_clear(imag); | |
5346 | } | |
5347 | ||
5348 | if (ret != NULL) | |
5349 | { | |
5350 | mpfr_clear(left_real); | |
5351 | mpfr_clear(left_imag); | |
5352 | mpfr_clear(right_real); | |
5353 | mpfr_clear(right_imag); | |
5354 | return ret; | |
5355 | } | |
5356 | } | |
5357 | ||
5358 | mpfr_clear(left_real); | |
5359 | mpfr_clear(left_imag); | |
5360 | mpfr_clear(right_real); | |
5361 | mpfr_clear(right_imag); | |
5362 | } | |
5363 | ||
5364 | // String constant expressions. | |
5365 | if (op == OPERATOR_PLUS | |
5366 | && left->type()->is_string_type() | |
5367 | && right->type()->is_string_type()) | |
5368 | { | |
5369 | std::string left_string; | |
5370 | std::string right_string; | |
5371 | if (left->string_constant_value(&left_string) | |
5372 | && right->string_constant_value(&right_string)) | |
5373 | return Expression::make_string(left_string + right_string, location); | |
5374 | } | |
5375 | ||
5376 | return this; | |
5377 | } | |
5378 | ||
5379 | // Return the integer constant value, if it has one. | |
5380 | ||
5381 | bool | |
5382 | Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val, | |
5383 | Type** ptype) const | |
5384 | { | |
5385 | mpz_t left_val; | |
5386 | mpz_init(left_val); | |
5387 | Type* left_type; | |
5388 | if (!this->left_->integer_constant_value(iota_is_constant, left_val, | |
5389 | &left_type)) | |
5390 | { | |
5391 | mpz_clear(left_val); | |
5392 | return false; | |
5393 | } | |
5394 | ||
5395 | mpz_t right_val; | |
5396 | mpz_init(right_val); | |
5397 | Type* right_type; | |
5398 | if (!this->right_->integer_constant_value(iota_is_constant, right_val, | |
5399 | &right_type)) | |
5400 | { | |
5401 | mpz_clear(right_val); | |
5402 | mpz_clear(left_val); | |
5403 | return false; | |
5404 | } | |
5405 | ||
5406 | bool ret; | |
5407 | if (left_type != right_type | |
5408 | && left_type != NULL | |
5409 | && right_type != NULL | |
5410 | && left_type->base() != right_type->base() | |
5411 | && this->op_ != OPERATOR_RSHIFT | |
5412 | && this->op_ != OPERATOR_LSHIFT) | |
5413 | ret = false; | |
5414 | else | |
5415 | ret = Binary_expression::eval_integer(this->op_, left_type, left_val, | |
5416 | right_type, right_val, | |
5417 | this->location(), val); | |
5418 | ||
5419 | mpz_clear(right_val); | |
5420 | mpz_clear(left_val); | |
5421 | ||
5422 | if (ret) | |
5423 | *ptype = left_type; | |
5424 | ||
5425 | return ret; | |
5426 | } | |
5427 | ||
5428 | // Return the floating point constant value, if it has one. | |
5429 | ||
5430 | bool | |
5431 | Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const | |
5432 | { | |
5433 | mpfr_t left_val; | |
5434 | mpfr_init(left_val); | |
5435 | Type* left_type; | |
5436 | if (!this->left_->float_constant_value(left_val, &left_type)) | |
5437 | { | |
5438 | mpfr_clear(left_val); | |
5439 | return false; | |
5440 | } | |
5441 | ||
5442 | mpfr_t right_val; | |
5443 | mpfr_init(right_val); | |
5444 | Type* right_type; | |
5445 | if (!this->right_->float_constant_value(right_val, &right_type)) | |
5446 | { | |
5447 | mpfr_clear(right_val); | |
5448 | mpfr_clear(left_val); | |
5449 | return false; | |
5450 | } | |
5451 | ||
5452 | bool ret; | |
5453 | if (left_type != right_type | |
5454 | && left_type != NULL | |
5455 | && right_type != NULL | |
5456 | && left_type->base() != right_type->base()) | |
5457 | ret = false; | |
5458 | else | |
5459 | ret = Binary_expression::eval_float(this->op_, left_type, left_val, | |
5460 | right_type, right_val, | |
5461 | val, this->location()); | |
5462 | ||
5463 | mpfr_clear(left_val); | |
5464 | mpfr_clear(right_val); | |
5465 | ||
5466 | if (ret) | |
5467 | *ptype = left_type; | |
5468 | ||
5469 | return ret; | |
5470 | } | |
5471 | ||
5472 | // Return the complex constant value, if it has one. | |
5473 | ||
5474 | bool | |
5475 | Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
5476 | Type** ptype) const | |
5477 | { | |
5478 | mpfr_t left_real; | |
5479 | mpfr_t left_imag; | |
5480 | mpfr_init(left_real); | |
5481 | mpfr_init(left_imag); | |
5482 | Type* left_type; | |
5483 | if (!this->left_->complex_constant_value(left_real, left_imag, &left_type)) | |
5484 | { | |
5485 | mpfr_clear(left_real); | |
5486 | mpfr_clear(left_imag); | |
5487 | return false; | |
5488 | } | |
5489 | ||
5490 | mpfr_t right_real; | |
5491 | mpfr_t right_imag; | |
5492 | mpfr_init(right_real); | |
5493 | mpfr_init(right_imag); | |
5494 | Type* right_type; | |
5495 | if (!this->right_->complex_constant_value(right_real, right_imag, | |
5496 | &right_type)) | |
5497 | { | |
5498 | mpfr_clear(left_real); | |
5499 | mpfr_clear(left_imag); | |
5500 | mpfr_clear(right_real); | |
5501 | mpfr_clear(right_imag); | |
5502 | return false; | |
5503 | } | |
5504 | ||
5505 | bool ret; | |
5506 | if (left_type != right_type | |
5507 | && left_type != NULL | |
5508 | && right_type != NULL | |
5509 | && left_type->base() != right_type->base()) | |
5510 | ret = false; | |
5511 | else | |
5512 | ret = Binary_expression::eval_complex(this->op_, left_type, | |
5513 | left_real, left_imag, | |
5514 | right_type, | |
5515 | right_real, right_imag, | |
5516 | real, imag, | |
5517 | this->location()); | |
5518 | mpfr_clear(left_real); | |
5519 | mpfr_clear(left_imag); | |
5520 | mpfr_clear(right_real); | |
5521 | mpfr_clear(right_imag); | |
5522 | ||
5523 | if (ret) | |
5524 | *ptype = left_type; | |
5525 | ||
5526 | return ret; | |
5527 | } | |
5528 | ||
5529 | // Note that the value is being discarded. | |
5530 | ||
5531 | void | |
5532 | Binary_expression::do_discarding_value() | |
5533 | { | |
5534 | if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND) | |
5535 | this->right_->discarding_value(); | |
5536 | else | |
5537 | this->warn_about_unused_value(); | |
5538 | } | |
5539 | ||
5540 | // Get type. | |
5541 | ||
5542 | Type* | |
5543 | Binary_expression::do_type() | |
5544 | { | |
5f5fea79 | 5545 | if (this->classification() == EXPRESSION_ERROR) |
5546 | return Type::make_error_type(); | |
5547 | ||
e440a328 | 5548 | switch (this->op_) |
5549 | { | |
5550 | case OPERATOR_OROR: | |
5551 | case OPERATOR_ANDAND: | |
5552 | case OPERATOR_EQEQ: | |
5553 | case OPERATOR_NOTEQ: | |
5554 | case OPERATOR_LT: | |
5555 | case OPERATOR_LE: | |
5556 | case OPERATOR_GT: | |
5557 | case OPERATOR_GE: | |
5558 | return Type::lookup_bool_type(); | |
5559 | ||
5560 | case OPERATOR_PLUS: | |
5561 | case OPERATOR_MINUS: | |
5562 | case OPERATOR_OR: | |
5563 | case OPERATOR_XOR: | |
5564 | case OPERATOR_MULT: | |
5565 | case OPERATOR_DIV: | |
5566 | case OPERATOR_MOD: | |
5567 | case OPERATOR_AND: | |
5568 | case OPERATOR_BITCLEAR: | |
5569 | { | |
5570 | Type* left_type = this->left_->type(); | |
5571 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5572 | if (left_type->is_error()) |
a5fe8571 | 5573 | return left_type; |
5c13bd80 | 5574 | else if (right_type->is_error()) |
a5fe8571 | 5575 | return right_type; |
5f5fea79 | 5576 | else if (!Type::are_compatible_for_binop(left_type, right_type)) |
5577 | { | |
5578 | this->report_error(_("incompatible types in binary expression")); | |
5579 | return Type::make_error_type(); | |
5580 | } | |
a5fe8571 | 5581 | else if (!left_type->is_abstract() && left_type->named_type() != NULL) |
e440a328 | 5582 | return left_type; |
5583 | else if (!right_type->is_abstract() && right_type->named_type() != NULL) | |
5584 | return right_type; | |
5585 | else if (!left_type->is_abstract()) | |
5586 | return left_type; | |
5587 | else if (!right_type->is_abstract()) | |
5588 | return right_type; | |
5589 | else if (left_type->complex_type() != NULL) | |
5590 | return left_type; | |
5591 | else if (right_type->complex_type() != NULL) | |
5592 | return right_type; | |
5593 | else if (left_type->float_type() != NULL) | |
5594 | return left_type; | |
5595 | else if (right_type->float_type() != NULL) | |
5596 | return right_type; | |
5597 | else | |
5598 | return left_type; | |
5599 | } | |
5600 | ||
5601 | case OPERATOR_LSHIFT: | |
5602 | case OPERATOR_RSHIFT: | |
5603 | return this->left_->type(); | |
5604 | ||
5605 | default: | |
5606 | gcc_unreachable(); | |
5607 | } | |
5608 | } | |
5609 | ||
5610 | // Set type for a binary expression. | |
5611 | ||
5612 | void | |
5613 | Binary_expression::do_determine_type(const Type_context* context) | |
5614 | { | |
5615 | Type* tleft = this->left_->type(); | |
5616 | Type* tright = this->right_->type(); | |
5617 | ||
5618 | // Both sides should have the same type, except for the shift | |
5619 | // operations. For a comparison, we should ignore the incoming | |
5620 | // type. | |
5621 | ||
5622 | bool is_shift_op = (this->op_ == OPERATOR_LSHIFT | |
5623 | || this->op_ == OPERATOR_RSHIFT); | |
5624 | ||
5625 | bool is_comparison = (this->op_ == OPERATOR_EQEQ | |
5626 | || this->op_ == OPERATOR_NOTEQ | |
5627 | || this->op_ == OPERATOR_LT | |
5628 | || this->op_ == OPERATOR_LE | |
5629 | || this->op_ == OPERATOR_GT | |
5630 | || this->op_ == OPERATOR_GE); | |
5631 | ||
5632 | Type_context subcontext(*context); | |
5633 | ||
5634 | if (is_comparison) | |
5635 | { | |
5636 | // In a comparison, the context does not determine the types of | |
5637 | // the operands. | |
5638 | subcontext.type = NULL; | |
5639 | } | |
5640 | ||
5641 | // Set the context for the left hand operand. | |
5642 | if (is_shift_op) | |
5643 | { | |
5644 | // The right hand operand plays no role in determining the type | |
5645 | // of the left hand operand. A shift of an abstract integer in | |
5646 | // a string context gets special treatment, which may be a | |
5647 | // language bug. | |
5648 | if (subcontext.type != NULL | |
5649 | && subcontext.type->is_string_type() | |
5650 | && tleft->is_abstract()) | |
5651 | error_at(this->location(), "shift of non-integer operand"); | |
5652 | } | |
5653 | else if (!tleft->is_abstract()) | |
5654 | subcontext.type = tleft; | |
5655 | else if (!tright->is_abstract()) | |
5656 | subcontext.type = tright; | |
5657 | else if (subcontext.type == NULL) | |
5658 | { | |
5659 | if ((tleft->integer_type() != NULL && tright->integer_type() != NULL) | |
5660 | || (tleft->float_type() != NULL && tright->float_type() != NULL) | |
5661 | || (tleft->complex_type() != NULL && tright->complex_type() != NULL)) | |
5662 | { | |
5663 | // Both sides have an abstract integer, abstract float, or | |
5664 | // abstract complex type. Just let CONTEXT determine | |
5665 | // whether they may remain abstract or not. | |
5666 | } | |
5667 | else if (tleft->complex_type() != NULL) | |
5668 | subcontext.type = tleft; | |
5669 | else if (tright->complex_type() != NULL) | |
5670 | subcontext.type = tright; | |
5671 | else if (tleft->float_type() != NULL) | |
5672 | subcontext.type = tleft; | |
5673 | else if (tright->float_type() != NULL) | |
5674 | subcontext.type = tright; | |
5675 | else | |
5676 | subcontext.type = tleft; | |
f58a23ae | 5677 | |
5678 | if (subcontext.type != NULL && !context->may_be_abstract) | |
5679 | subcontext.type = subcontext.type->make_non_abstract_type(); | |
e440a328 | 5680 | } |
5681 | ||
5682 | this->left_->determine_type(&subcontext); | |
5683 | ||
5684 | // The context for the right hand operand is the same as for the | |
5685 | // left hand operand, except for a shift operator. | |
5686 | if (is_shift_op) | |
5687 | { | |
5688 | subcontext.type = Type::lookup_integer_type("uint"); | |
5689 | subcontext.may_be_abstract = false; | |
5690 | } | |
5691 | ||
5692 | this->right_->determine_type(&subcontext); | |
5693 | } | |
5694 | ||
5695 | // Report an error if the binary operator OP does not support TYPE. | |
5696 | // Return whether the operation is OK. This should not be used for | |
5697 | // shift. | |
5698 | ||
5699 | bool | |
5700 | Binary_expression::check_operator_type(Operator op, Type* type, | |
5701 | source_location location) | |
5702 | { | |
5703 | switch (op) | |
5704 | { | |
5705 | case OPERATOR_OROR: | |
5706 | case OPERATOR_ANDAND: | |
5707 | if (!type->is_boolean_type()) | |
5708 | { | |
5709 | error_at(location, "expected boolean type"); | |
5710 | return false; | |
5711 | } | |
5712 | break; | |
5713 | ||
5714 | case OPERATOR_EQEQ: | |
5715 | case OPERATOR_NOTEQ: | |
5716 | if (type->integer_type() == NULL | |
5717 | && type->float_type() == NULL | |
5718 | && type->complex_type() == NULL | |
5719 | && !type->is_string_type() | |
5720 | && type->points_to() == NULL | |
5721 | && !type->is_nil_type() | |
5722 | && !type->is_boolean_type() | |
5723 | && type->interface_type() == NULL | |
5724 | && (type->array_type() == NULL | |
5725 | || type->array_type()->length() != NULL) | |
5726 | && type->map_type() == NULL | |
5727 | && type->channel_type() == NULL | |
5728 | && type->function_type() == NULL) | |
5729 | { | |
5730 | error_at(location, | |
5731 | ("expected integer, floating, complex, string, pointer, " | |
5732 | "boolean, interface, slice, map, channel, " | |
5733 | "or function type")); | |
5734 | return false; | |
5735 | } | |
5736 | break; | |
5737 | ||
5738 | case OPERATOR_LT: | |
5739 | case OPERATOR_LE: | |
5740 | case OPERATOR_GT: | |
5741 | case OPERATOR_GE: | |
5742 | if (type->integer_type() == NULL | |
5743 | && type->float_type() == NULL | |
5744 | && !type->is_string_type()) | |
5745 | { | |
5746 | error_at(location, "expected integer, floating, or string type"); | |
5747 | return false; | |
5748 | } | |
5749 | break; | |
5750 | ||
5751 | case OPERATOR_PLUS: | |
5752 | case OPERATOR_PLUSEQ: | |
5753 | if (type->integer_type() == NULL | |
5754 | && type->float_type() == NULL | |
5755 | && type->complex_type() == NULL | |
5756 | && !type->is_string_type()) | |
5757 | { | |
5758 | error_at(location, | |
5759 | "expected integer, floating, complex, or string type"); | |
5760 | return false; | |
5761 | } | |
5762 | break; | |
5763 | ||
5764 | case OPERATOR_MINUS: | |
5765 | case OPERATOR_MINUSEQ: | |
5766 | case OPERATOR_MULT: | |
5767 | case OPERATOR_MULTEQ: | |
5768 | case OPERATOR_DIV: | |
5769 | case OPERATOR_DIVEQ: | |
5770 | if (type->integer_type() == NULL | |
5771 | && type->float_type() == NULL | |
5772 | && type->complex_type() == NULL) | |
5773 | { | |
5774 | error_at(location, "expected integer, floating, or complex type"); | |
5775 | return false; | |
5776 | } | |
5777 | break; | |
5778 | ||
5779 | case OPERATOR_MOD: | |
5780 | case OPERATOR_MODEQ: | |
5781 | case OPERATOR_OR: | |
5782 | case OPERATOR_OREQ: | |
5783 | case OPERATOR_AND: | |
5784 | case OPERATOR_ANDEQ: | |
5785 | case OPERATOR_XOR: | |
5786 | case OPERATOR_XOREQ: | |
5787 | case OPERATOR_BITCLEAR: | |
5788 | case OPERATOR_BITCLEAREQ: | |
5789 | if (type->integer_type() == NULL) | |
5790 | { | |
5791 | error_at(location, "expected integer type"); | |
5792 | return false; | |
5793 | } | |
5794 | break; | |
5795 | ||
5796 | default: | |
5797 | gcc_unreachable(); | |
5798 | } | |
5799 | ||
5800 | return true; | |
5801 | } | |
5802 | ||
5803 | // Check types. | |
5804 | ||
5805 | void | |
5806 | Binary_expression::do_check_types(Gogo*) | |
5807 | { | |
5f5fea79 | 5808 | if (this->classification() == EXPRESSION_ERROR) |
5809 | return; | |
5810 | ||
e440a328 | 5811 | Type* left_type = this->left_->type(); |
5812 | Type* right_type = this->right_->type(); | |
5c13bd80 | 5813 | if (left_type->is_error() || right_type->is_error()) |
9fe897ef | 5814 | { |
5815 | this->set_is_error(); | |
5816 | return; | |
5817 | } | |
e440a328 | 5818 | |
5819 | if (this->op_ == OPERATOR_EQEQ | |
5820 | || this->op_ == OPERATOR_NOTEQ | |
5821 | || this->op_ == OPERATOR_LT | |
5822 | || this->op_ == OPERATOR_LE | |
5823 | || this->op_ == OPERATOR_GT | |
5824 | || this->op_ == OPERATOR_GE) | |
5825 | { | |
5826 | if (!Type::are_assignable(left_type, right_type, NULL) | |
5827 | && !Type::are_assignable(right_type, left_type, NULL)) | |
5828 | { | |
5829 | this->report_error(_("incompatible types in binary expression")); | |
5830 | return; | |
5831 | } | |
5832 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
5833 | this->location()) | |
5834 | || !Binary_expression::check_operator_type(this->op_, right_type, | |
5835 | this->location())) | |
5836 | { | |
5837 | this->set_is_error(); | |
5838 | return; | |
5839 | } | |
5840 | } | |
5841 | else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT) | |
5842 | { | |
5843 | if (!Type::are_compatible_for_binop(left_type, right_type)) | |
5844 | { | |
5845 | this->report_error(_("incompatible types in binary expression")); | |
5846 | return; | |
5847 | } | |
5848 | if (!Binary_expression::check_operator_type(this->op_, left_type, | |
5849 | this->location())) | |
5850 | { | |
5851 | this->set_is_error(); | |
5852 | return; | |
5853 | } | |
5854 | } | |
5855 | else | |
5856 | { | |
5857 | if (left_type->integer_type() == NULL) | |
5858 | this->report_error(_("shift of non-integer operand")); | |
5859 | ||
5860 | if (!right_type->is_abstract() | |
5861 | && (right_type->integer_type() == NULL | |
5862 | || !right_type->integer_type()->is_unsigned())) | |
5863 | this->report_error(_("shift count not unsigned integer")); | |
5864 | else | |
5865 | { | |
5866 | mpz_t val; | |
5867 | mpz_init(val); | |
5868 | Type* type; | |
5869 | if (this->right_->integer_constant_value(true, val, &type)) | |
5870 | { | |
5871 | if (mpz_sgn(val) < 0) | |
a4eba91b | 5872 | { |
5873 | this->report_error(_("negative shift count")); | |
5874 | mpz_set_ui(val, 0); | |
5875 | source_location rloc = this->right_->location(); | |
5876 | this->right_ = Expression::make_integer(&val, right_type, | |
5877 | rloc); | |
5878 | } | |
e440a328 | 5879 | } |
5880 | mpz_clear(val); | |
5881 | } | |
5882 | } | |
5883 | } | |
5884 | ||
5885 | // Get a tree for a binary expression. | |
5886 | ||
5887 | tree | |
5888 | Binary_expression::do_get_tree(Translate_context* context) | |
5889 | { | |
5890 | tree left = this->left_->get_tree(context); | |
5891 | tree right = this->right_->get_tree(context); | |
5892 | ||
5893 | if (left == error_mark_node || right == error_mark_node) | |
5894 | return error_mark_node; | |
5895 | ||
5896 | enum tree_code code; | |
5897 | bool use_left_type = true; | |
5898 | bool is_shift_op = false; | |
5899 | switch (this->op_) | |
5900 | { | |
5901 | case OPERATOR_EQEQ: | |
5902 | case OPERATOR_NOTEQ: | |
5903 | case OPERATOR_LT: | |
5904 | case OPERATOR_LE: | |
5905 | case OPERATOR_GT: | |
5906 | case OPERATOR_GE: | |
5907 | return Expression::comparison_tree(context, this->op_, | |
5908 | this->left_->type(), left, | |
5909 | this->right_->type(), right, | |
5910 | this->location()); | |
5911 | ||
5912 | case OPERATOR_OROR: | |
5913 | code = TRUTH_ORIF_EXPR; | |
5914 | use_left_type = false; | |
5915 | break; | |
5916 | case OPERATOR_ANDAND: | |
5917 | code = TRUTH_ANDIF_EXPR; | |
5918 | use_left_type = false; | |
5919 | break; | |
5920 | case OPERATOR_PLUS: | |
5921 | code = PLUS_EXPR; | |
5922 | break; | |
5923 | case OPERATOR_MINUS: | |
5924 | code = MINUS_EXPR; | |
5925 | break; | |
5926 | case OPERATOR_OR: | |
5927 | code = BIT_IOR_EXPR; | |
5928 | break; | |
5929 | case OPERATOR_XOR: | |
5930 | code = BIT_XOR_EXPR; | |
5931 | break; | |
5932 | case OPERATOR_MULT: | |
5933 | code = MULT_EXPR; | |
5934 | break; | |
5935 | case OPERATOR_DIV: | |
5936 | { | |
5937 | Type *t = this->left_->type(); | |
5938 | if (t->float_type() != NULL || t->complex_type() != NULL) | |
5939 | code = RDIV_EXPR; | |
5940 | else | |
5941 | code = TRUNC_DIV_EXPR; | |
5942 | } | |
5943 | break; | |
5944 | case OPERATOR_MOD: | |
5945 | code = TRUNC_MOD_EXPR; | |
5946 | break; | |
5947 | case OPERATOR_LSHIFT: | |
5948 | code = LSHIFT_EXPR; | |
5949 | is_shift_op = true; | |
5950 | break; | |
5951 | case OPERATOR_RSHIFT: | |
5952 | code = RSHIFT_EXPR; | |
5953 | is_shift_op = true; | |
5954 | break; | |
5955 | case OPERATOR_AND: | |
5956 | code = BIT_AND_EXPR; | |
5957 | break; | |
5958 | case OPERATOR_BITCLEAR: | |
5959 | right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right); | |
5960 | code = BIT_AND_EXPR; | |
5961 | break; | |
5962 | default: | |
5963 | gcc_unreachable(); | |
5964 | } | |
5965 | ||
5966 | tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right); | |
5967 | ||
5968 | if (this->left_->type()->is_string_type()) | |
5969 | { | |
5970 | gcc_assert(this->op_ == OPERATOR_PLUS); | |
5971 | tree string_type = Type::make_string_type()->get_tree(context->gogo()); | |
5972 | static tree string_plus_decl; | |
5973 | return Gogo::call_builtin(&string_plus_decl, | |
5974 | this->location(), | |
5975 | "__go_string_plus", | |
5976 | 2, | |
5977 | string_type, | |
5978 | string_type, | |
5979 | left, | |
5980 | string_type, | |
5981 | right); | |
5982 | } | |
5983 | ||
5984 | tree compute_type = excess_precision_type(type); | |
5985 | if (compute_type != NULL_TREE) | |
5986 | { | |
5987 | left = ::convert(compute_type, left); | |
5988 | right = ::convert(compute_type, right); | |
5989 | } | |
5990 | ||
5991 | tree eval_saved = NULL_TREE; | |
5992 | if (is_shift_op) | |
5993 | { | |
e440a328 | 5994 | // Make sure the values are evaluated. |
a7a70f31 | 5995 | if (!DECL_P(left) && TREE_SIDE_EFFECTS(left)) |
5996 | { | |
5997 | left = save_expr(left); | |
5998 | eval_saved = left; | |
5999 | } | |
6000 | if (!DECL_P(right) && TREE_SIDE_EFFECTS(right)) | |
6001 | { | |
6002 | right = save_expr(right); | |
6003 | if (eval_saved == NULL_TREE) | |
6004 | eval_saved = right; | |
6005 | else | |
6006 | eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR, | |
6007 | void_type_node, eval_saved, right); | |
6008 | } | |
e440a328 | 6009 | } |
6010 | ||
6011 | tree ret = fold_build2_loc(this->location(), | |
6012 | code, | |
6013 | compute_type != NULL_TREE ? compute_type : type, | |
6014 | left, right); | |
6015 | ||
6016 | if (compute_type != NULL_TREE) | |
6017 | ret = ::convert(type, ret); | |
6018 | ||
6019 | // In Go, a shift larger than the size of the type is well-defined. | |
6020 | // This is not true in GENERIC, so we need to insert a conditional. | |
6021 | if (is_shift_op) | |
6022 | { | |
6023 | gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left))); | |
6024 | gcc_assert(this->left_->type()->integer_type() != NULL); | |
6025 | int bits = TYPE_PRECISION(TREE_TYPE(left)); | |
6026 | ||
6027 | tree compare = fold_build2(LT_EXPR, boolean_type_node, right, | |
6028 | build_int_cst_type(TREE_TYPE(right), bits)); | |
6029 | ||
6030 | tree overflow_result = fold_convert_loc(this->location(), | |
6031 | TREE_TYPE(left), | |
6032 | integer_zero_node); | |
6033 | if (this->op_ == OPERATOR_RSHIFT | |
6034 | && !this->left_->type()->integer_type()->is_unsigned()) | |
6035 | { | |
6036 | tree neg = fold_build2_loc(this->location(), LT_EXPR, | |
6037 | boolean_type_node, left, | |
6038 | fold_convert_loc(this->location(), | |
6039 | TREE_TYPE(left), | |
6040 | integer_zero_node)); | |
6041 | tree neg_one = fold_build2_loc(this->location(), | |
6042 | MINUS_EXPR, TREE_TYPE(left), | |
6043 | fold_convert_loc(this->location(), | |
6044 | TREE_TYPE(left), | |
6045 | integer_zero_node), | |
6046 | fold_convert_loc(this->location(), | |
6047 | TREE_TYPE(left), | |
6048 | integer_one_node)); | |
6049 | overflow_result = fold_build3_loc(this->location(), COND_EXPR, | |
6050 | TREE_TYPE(left), neg, neg_one, | |
6051 | overflow_result); | |
6052 | } | |
6053 | ||
6054 | ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left), | |
6055 | compare, ret, overflow_result); | |
6056 | ||
a7a70f31 | 6057 | if (eval_saved != NULL_TREE) |
6058 | ret = fold_build2_loc(this->location(), COMPOUND_EXPR, | |
6059 | TREE_TYPE(ret), eval_saved, ret); | |
e440a328 | 6060 | } |
6061 | ||
6062 | return ret; | |
6063 | } | |
6064 | ||
6065 | // Export a binary expression. | |
6066 | ||
6067 | void | |
6068 | Binary_expression::do_export(Export* exp) const | |
6069 | { | |
6070 | exp->write_c_string("("); | |
6071 | this->left_->export_expression(exp); | |
6072 | switch (this->op_) | |
6073 | { | |
6074 | case OPERATOR_OROR: | |
6075 | exp->write_c_string(" || "); | |
6076 | break; | |
6077 | case OPERATOR_ANDAND: | |
6078 | exp->write_c_string(" && "); | |
6079 | break; | |
6080 | case OPERATOR_EQEQ: | |
6081 | exp->write_c_string(" == "); | |
6082 | break; | |
6083 | case OPERATOR_NOTEQ: | |
6084 | exp->write_c_string(" != "); | |
6085 | break; | |
6086 | case OPERATOR_LT: | |
6087 | exp->write_c_string(" < "); | |
6088 | break; | |
6089 | case OPERATOR_LE: | |
6090 | exp->write_c_string(" <= "); | |
6091 | break; | |
6092 | case OPERATOR_GT: | |
6093 | exp->write_c_string(" > "); | |
6094 | break; | |
6095 | case OPERATOR_GE: | |
6096 | exp->write_c_string(" >= "); | |
6097 | break; | |
6098 | case OPERATOR_PLUS: | |
6099 | exp->write_c_string(" + "); | |
6100 | break; | |
6101 | case OPERATOR_MINUS: | |
6102 | exp->write_c_string(" - "); | |
6103 | break; | |
6104 | case OPERATOR_OR: | |
6105 | exp->write_c_string(" | "); | |
6106 | break; | |
6107 | case OPERATOR_XOR: | |
6108 | exp->write_c_string(" ^ "); | |
6109 | break; | |
6110 | case OPERATOR_MULT: | |
6111 | exp->write_c_string(" * "); | |
6112 | break; | |
6113 | case OPERATOR_DIV: | |
6114 | exp->write_c_string(" / "); | |
6115 | break; | |
6116 | case OPERATOR_MOD: | |
6117 | exp->write_c_string(" % "); | |
6118 | break; | |
6119 | case OPERATOR_LSHIFT: | |
6120 | exp->write_c_string(" << "); | |
6121 | break; | |
6122 | case OPERATOR_RSHIFT: | |
6123 | exp->write_c_string(" >> "); | |
6124 | break; | |
6125 | case OPERATOR_AND: | |
6126 | exp->write_c_string(" & "); | |
6127 | break; | |
6128 | case OPERATOR_BITCLEAR: | |
6129 | exp->write_c_string(" &^ "); | |
6130 | break; | |
6131 | default: | |
6132 | gcc_unreachable(); | |
6133 | } | |
6134 | this->right_->export_expression(exp); | |
6135 | exp->write_c_string(")"); | |
6136 | } | |
6137 | ||
6138 | // Import a binary expression. | |
6139 | ||
6140 | Expression* | |
6141 | Binary_expression::do_import(Import* imp) | |
6142 | { | |
6143 | imp->require_c_string("("); | |
6144 | ||
6145 | Expression* left = Expression::import_expression(imp); | |
6146 | ||
6147 | Operator op; | |
6148 | if (imp->match_c_string(" || ")) | |
6149 | { | |
6150 | op = OPERATOR_OROR; | |
6151 | imp->advance(4); | |
6152 | } | |
6153 | else if (imp->match_c_string(" && ")) | |
6154 | { | |
6155 | op = OPERATOR_ANDAND; | |
6156 | imp->advance(4); | |
6157 | } | |
6158 | else if (imp->match_c_string(" == ")) | |
6159 | { | |
6160 | op = OPERATOR_EQEQ; | |
6161 | imp->advance(4); | |
6162 | } | |
6163 | else if (imp->match_c_string(" != ")) | |
6164 | { | |
6165 | op = OPERATOR_NOTEQ; | |
6166 | imp->advance(4); | |
6167 | } | |
6168 | else if (imp->match_c_string(" < ")) | |
6169 | { | |
6170 | op = OPERATOR_LT; | |
6171 | imp->advance(3); | |
6172 | } | |
6173 | else if (imp->match_c_string(" <= ")) | |
6174 | { | |
6175 | op = OPERATOR_LE; | |
6176 | imp->advance(4); | |
6177 | } | |
6178 | else if (imp->match_c_string(" > ")) | |
6179 | { | |
6180 | op = OPERATOR_GT; | |
6181 | imp->advance(3); | |
6182 | } | |
6183 | else if (imp->match_c_string(" >= ")) | |
6184 | { | |
6185 | op = OPERATOR_GE; | |
6186 | imp->advance(4); | |
6187 | } | |
6188 | else if (imp->match_c_string(" + ")) | |
6189 | { | |
6190 | op = OPERATOR_PLUS; | |
6191 | imp->advance(3); | |
6192 | } | |
6193 | else if (imp->match_c_string(" - ")) | |
6194 | { | |
6195 | op = OPERATOR_MINUS; | |
6196 | imp->advance(3); | |
6197 | } | |
6198 | else if (imp->match_c_string(" | ")) | |
6199 | { | |
6200 | op = OPERATOR_OR; | |
6201 | imp->advance(3); | |
6202 | } | |
6203 | else if (imp->match_c_string(" ^ ")) | |
6204 | { | |
6205 | op = OPERATOR_XOR; | |
6206 | imp->advance(3); | |
6207 | } | |
6208 | else if (imp->match_c_string(" * ")) | |
6209 | { | |
6210 | op = OPERATOR_MULT; | |
6211 | imp->advance(3); | |
6212 | } | |
6213 | else if (imp->match_c_string(" / ")) | |
6214 | { | |
6215 | op = OPERATOR_DIV; | |
6216 | imp->advance(3); | |
6217 | } | |
6218 | else if (imp->match_c_string(" % ")) | |
6219 | { | |
6220 | op = OPERATOR_MOD; | |
6221 | imp->advance(3); | |
6222 | } | |
6223 | else if (imp->match_c_string(" << ")) | |
6224 | { | |
6225 | op = OPERATOR_LSHIFT; | |
6226 | imp->advance(4); | |
6227 | } | |
6228 | else if (imp->match_c_string(" >> ")) | |
6229 | { | |
6230 | op = OPERATOR_RSHIFT; | |
6231 | imp->advance(4); | |
6232 | } | |
6233 | else if (imp->match_c_string(" & ")) | |
6234 | { | |
6235 | op = OPERATOR_AND; | |
6236 | imp->advance(3); | |
6237 | } | |
6238 | else if (imp->match_c_string(" &^ ")) | |
6239 | { | |
6240 | op = OPERATOR_BITCLEAR; | |
6241 | imp->advance(4); | |
6242 | } | |
6243 | else | |
6244 | { | |
6245 | error_at(imp->location(), "unrecognized binary operator"); | |
6246 | return Expression::make_error(imp->location()); | |
6247 | } | |
6248 | ||
6249 | Expression* right = Expression::import_expression(imp); | |
6250 | ||
6251 | imp->require_c_string(")"); | |
6252 | ||
6253 | return Expression::make_binary(op, left, right, imp->location()); | |
6254 | } | |
6255 | ||
6256 | // Make a binary expression. | |
6257 | ||
6258 | Expression* | |
6259 | Expression::make_binary(Operator op, Expression* left, Expression* right, | |
6260 | source_location location) | |
6261 | { | |
6262 | return new Binary_expression(op, left, right, location); | |
6263 | } | |
6264 | ||
6265 | // Implement a comparison. | |
6266 | ||
6267 | tree | |
6268 | Expression::comparison_tree(Translate_context* context, Operator op, | |
6269 | Type* left_type, tree left_tree, | |
6270 | Type* right_type, tree right_tree, | |
6271 | source_location location) | |
6272 | { | |
6273 | enum tree_code code; | |
6274 | switch (op) | |
6275 | { | |
6276 | case OPERATOR_EQEQ: | |
6277 | code = EQ_EXPR; | |
6278 | break; | |
6279 | case OPERATOR_NOTEQ: | |
6280 | code = NE_EXPR; | |
6281 | break; | |
6282 | case OPERATOR_LT: | |
6283 | code = LT_EXPR; | |
6284 | break; | |
6285 | case OPERATOR_LE: | |
6286 | code = LE_EXPR; | |
6287 | break; | |
6288 | case OPERATOR_GT: | |
6289 | code = GT_EXPR; | |
6290 | break; | |
6291 | case OPERATOR_GE: | |
6292 | code = GE_EXPR; | |
6293 | break; | |
6294 | default: | |
6295 | gcc_unreachable(); | |
6296 | } | |
6297 | ||
15c67ee2 | 6298 | if (left_type->is_string_type() && right_type->is_string_type()) |
e440a328 | 6299 | { |
e440a328 | 6300 | tree string_type = Type::make_string_type()->get_tree(context->gogo()); |
6301 | static tree string_compare_decl; | |
6302 | left_tree = Gogo::call_builtin(&string_compare_decl, | |
6303 | location, | |
6304 | "__go_strcmp", | |
6305 | 2, | |
6306 | integer_type_node, | |
6307 | string_type, | |
6308 | left_tree, | |
6309 | string_type, | |
6310 | right_tree); | |
6311 | right_tree = build_int_cst_type(integer_type_node, 0); | |
6312 | } | |
15c67ee2 | 6313 | else if ((left_type->interface_type() != NULL |
6314 | && right_type->interface_type() == NULL | |
6315 | && !right_type->is_nil_type()) | |
6316 | || (left_type->interface_type() == NULL | |
6317 | && !left_type->is_nil_type() | |
6318 | && right_type->interface_type() != NULL)) | |
e440a328 | 6319 | { |
6320 | // Comparing an interface value to a non-interface value. | |
6321 | if (left_type->interface_type() == NULL) | |
6322 | { | |
6323 | std::swap(left_type, right_type); | |
6324 | std::swap(left_tree, right_tree); | |
6325 | } | |
6326 | ||
6327 | // The right operand is not an interface. We need to take its | |
6328 | // address if it is not a pointer. | |
6329 | tree make_tmp; | |
6330 | tree arg; | |
6331 | if (right_type->points_to() != NULL) | |
6332 | { | |
6333 | make_tmp = NULL_TREE; | |
6334 | arg = right_tree; | |
6335 | } | |
6336 | else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree)) | |
6337 | { | |
6338 | make_tmp = NULL_TREE; | |
6339 | arg = build_fold_addr_expr_loc(location, right_tree); | |
6340 | if (DECL_P(right_tree)) | |
6341 | TREE_ADDRESSABLE(right_tree) = 1; | |
6342 | } | |
6343 | else | |
6344 | { | |
6345 | tree tmp = create_tmp_var(TREE_TYPE(right_tree), | |
6346 | get_name(right_tree)); | |
6347 | DECL_IGNORED_P(tmp) = 0; | |
6348 | DECL_INITIAL(tmp) = right_tree; | |
6349 | TREE_ADDRESSABLE(tmp) = 1; | |
6350 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
6351 | SET_EXPR_LOCATION(make_tmp, location); | |
6352 | arg = build_fold_addr_expr_loc(location, tmp); | |
6353 | } | |
6354 | arg = fold_convert_loc(location, ptr_type_node, arg); | |
6355 | ||
6356 | tree descriptor = right_type->type_descriptor_pointer(context->gogo()); | |
6357 | ||
6358 | if (left_type->interface_type()->is_empty()) | |
6359 | { | |
6360 | static tree empty_interface_value_compare_decl; | |
6361 | left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl, | |
6362 | location, | |
6363 | "__go_empty_interface_value_compare", | |
6364 | 3, | |
6365 | integer_type_node, | |
6366 | TREE_TYPE(left_tree), | |
6367 | left_tree, | |
6368 | TREE_TYPE(descriptor), | |
6369 | descriptor, | |
6370 | ptr_type_node, | |
6371 | arg); | |
5fb82b5e | 6372 | if (left_tree == error_mark_node) |
6373 | return error_mark_node; | |
e440a328 | 6374 | // This can panic if the type is not comparable. |
6375 | TREE_NOTHROW(empty_interface_value_compare_decl) = 0; | |
6376 | } | |
6377 | else | |
6378 | { | |
6379 | static tree interface_value_compare_decl; | |
6380 | left_tree = Gogo::call_builtin(&interface_value_compare_decl, | |
6381 | location, | |
6382 | "__go_interface_value_compare", | |
6383 | 3, | |
6384 | integer_type_node, | |
6385 | TREE_TYPE(left_tree), | |
6386 | left_tree, | |
6387 | TREE_TYPE(descriptor), | |
6388 | descriptor, | |
6389 | ptr_type_node, | |
6390 | arg); | |
5fb82b5e | 6391 | if (left_tree == error_mark_node) |
6392 | return error_mark_node; | |
e440a328 | 6393 | // This can panic if the type is not comparable. |
6394 | TREE_NOTHROW(interface_value_compare_decl) = 0; | |
6395 | } | |
6396 | right_tree = build_int_cst_type(integer_type_node, 0); | |
6397 | ||
6398 | if (make_tmp != NULL_TREE) | |
6399 | left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp, | |
6400 | left_tree); | |
6401 | } | |
6402 | else if (left_type->interface_type() != NULL | |
6403 | && right_type->interface_type() != NULL) | |
6404 | { | |
739bad04 | 6405 | if (left_type->interface_type()->is_empty() |
6406 | && right_type->interface_type()->is_empty()) | |
e440a328 | 6407 | { |
e440a328 | 6408 | static tree empty_interface_compare_decl; |
6409 | left_tree = Gogo::call_builtin(&empty_interface_compare_decl, | |
6410 | location, | |
6411 | "__go_empty_interface_compare", | |
6412 | 2, | |
6413 | integer_type_node, | |
6414 | TREE_TYPE(left_tree), | |
6415 | left_tree, | |
6416 | TREE_TYPE(right_tree), | |
6417 | right_tree); | |
5fb82b5e | 6418 | if (left_tree == error_mark_node) |
6419 | return error_mark_node; | |
e440a328 | 6420 | // This can panic if the type is uncomparable. |
6421 | TREE_NOTHROW(empty_interface_compare_decl) = 0; | |
6422 | } | |
739bad04 | 6423 | else if (!left_type->interface_type()->is_empty() |
6424 | && !right_type->interface_type()->is_empty()) | |
e440a328 | 6425 | { |
e440a328 | 6426 | static tree interface_compare_decl; |
6427 | left_tree = Gogo::call_builtin(&interface_compare_decl, | |
6428 | location, | |
6429 | "__go_interface_compare", | |
6430 | 2, | |
6431 | integer_type_node, | |
6432 | TREE_TYPE(left_tree), | |
6433 | left_tree, | |
6434 | TREE_TYPE(right_tree), | |
6435 | right_tree); | |
5fb82b5e | 6436 | if (left_tree == error_mark_node) |
6437 | return error_mark_node; | |
e440a328 | 6438 | // This can panic if the type is uncomparable. |
6439 | TREE_NOTHROW(interface_compare_decl) = 0; | |
6440 | } | |
739bad04 | 6441 | else |
6442 | { | |
6443 | if (left_type->interface_type()->is_empty()) | |
6444 | { | |
6445 | gcc_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ); | |
6446 | std::swap(left_type, right_type); | |
6447 | std::swap(left_tree, right_tree); | |
6448 | } | |
6449 | gcc_assert(!left_type->interface_type()->is_empty()); | |
6450 | gcc_assert(right_type->interface_type()->is_empty()); | |
6451 | static tree interface_empty_compare_decl; | |
6452 | left_tree = Gogo::call_builtin(&interface_empty_compare_decl, | |
6453 | location, | |
6454 | "__go_interface_empty_compare", | |
6455 | 2, | |
6456 | integer_type_node, | |
6457 | TREE_TYPE(left_tree), | |
6458 | left_tree, | |
6459 | TREE_TYPE(right_tree), | |
6460 | right_tree); | |
6461 | if (left_tree == error_mark_node) | |
6462 | return error_mark_node; | |
6463 | // This can panic if the type is uncomparable. | |
6464 | TREE_NOTHROW(interface_empty_compare_decl) = 0; | |
6465 | } | |
6466 | ||
e440a328 | 6467 | right_tree = build_int_cst_type(integer_type_node, 0); |
6468 | } | |
6469 | ||
6470 | if (left_type->is_nil_type() | |
6471 | && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)) | |
6472 | { | |
6473 | std::swap(left_type, right_type); | |
6474 | std::swap(left_tree, right_tree); | |
6475 | } | |
6476 | ||
6477 | if (right_type->is_nil_type()) | |
6478 | { | |
6479 | if (left_type->array_type() != NULL | |
6480 | && left_type->array_type()->length() == NULL) | |
6481 | { | |
6482 | Array_type* at = left_type->array_type(); | |
6483 | left_tree = at->value_pointer_tree(context->gogo(), left_tree); | |
6484 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6485 | } | |
6486 | else if (left_type->interface_type() != NULL) | |
6487 | { | |
6488 | // An interface is nil if the first field is nil. | |
6489 | tree left_type_tree = TREE_TYPE(left_tree); | |
6490 | gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE); | |
6491 | tree field = TYPE_FIELDS(left_type_tree); | |
6492 | left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree, | |
6493 | field, NULL_TREE); | |
6494 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6495 | } | |
6496 | else | |
6497 | { | |
6498 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree))); | |
6499 | right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node); | |
6500 | } | |
6501 | } | |
6502 | ||
d8ccb1e3 | 6503 | if (left_tree == error_mark_node || right_tree == error_mark_node) |
6504 | return error_mark_node; | |
6505 | ||
e440a328 | 6506 | tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree); |
6507 | if (CAN_HAVE_LOCATION_P(ret)) | |
6508 | SET_EXPR_LOCATION(ret, location); | |
6509 | return ret; | |
6510 | } | |
6511 | ||
6512 | // Class Bound_method_expression. | |
6513 | ||
6514 | // Traversal. | |
6515 | ||
6516 | int | |
6517 | Bound_method_expression::do_traverse(Traverse* traverse) | |
6518 | { | |
6519 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT) | |
6520 | return TRAVERSE_EXIT; | |
6521 | return Expression::traverse(&this->method_, traverse); | |
6522 | } | |
6523 | ||
6524 | // Return the type of a bound method expression. The type of this | |
6525 | // object is really the type of the method with no receiver. We | |
6526 | // should be able to get away with just returning the type of the | |
6527 | // method. | |
6528 | ||
6529 | Type* | |
6530 | Bound_method_expression::do_type() | |
6531 | { | |
6532 | return this->method_->type(); | |
6533 | } | |
6534 | ||
6535 | // Determine the types of a method expression. | |
6536 | ||
6537 | void | |
6538 | Bound_method_expression::do_determine_type(const Type_context*) | |
6539 | { | |
6540 | this->method_->determine_type_no_context(); | |
6541 | Type* mtype = this->method_->type(); | |
6542 | Function_type* fntype = mtype == NULL ? NULL : mtype->function_type(); | |
6543 | if (fntype == NULL || !fntype->is_method()) | |
6544 | this->expr_->determine_type_no_context(); | |
6545 | else | |
6546 | { | |
6547 | Type_context subcontext(fntype->receiver()->type(), false); | |
6548 | this->expr_->determine_type(&subcontext); | |
6549 | } | |
6550 | } | |
6551 | ||
6552 | // Check the types of a method expression. | |
6553 | ||
6554 | void | |
6555 | Bound_method_expression::do_check_types(Gogo*) | |
6556 | { | |
6557 | Type* type = this->method_->type()->deref(); | |
6558 | if (type == NULL | |
6559 | || type->function_type() == NULL | |
6560 | || !type->function_type()->is_method()) | |
6561 | this->report_error(_("object is not a method")); | |
6562 | else | |
6563 | { | |
6564 | Type* rtype = type->function_type()->receiver()->type()->deref(); | |
6565 | Type* etype = (this->expr_type_ != NULL | |
6566 | ? this->expr_type_ | |
6567 | : this->expr_->type()); | |
6568 | etype = etype->deref(); | |
07ba8be5 | 6569 | if (!Type::are_identical(rtype, etype, true, NULL)) |
e440a328 | 6570 | this->report_error(_("method type does not match object type")); |
6571 | } | |
6572 | } | |
6573 | ||
6574 | // Get the tree for a method expression. There is no standard tree | |
6575 | // representation for this. The only places it may currently be used | |
6576 | // are in a Call_expression or a Go_statement, which will take it | |
6577 | // apart directly. So this has nothing to do at present. | |
6578 | ||
6579 | tree | |
6580 | Bound_method_expression::do_get_tree(Translate_context*) | |
6581 | { | |
d40405e2 | 6582 | error_at(this->location(), "reference to method other than calling it"); |
6583 | return error_mark_node; | |
e440a328 | 6584 | } |
6585 | ||
6586 | // Make a method expression. | |
6587 | ||
6588 | Bound_method_expression* | |
6589 | Expression::make_bound_method(Expression* expr, Expression* method, | |
6590 | source_location location) | |
6591 | { | |
6592 | return new Bound_method_expression(expr, method, location); | |
6593 | } | |
6594 | ||
6595 | // Class Builtin_call_expression. This is used for a call to a | |
6596 | // builtin function. | |
6597 | ||
6598 | class Builtin_call_expression : public Call_expression | |
6599 | { | |
6600 | public: | |
6601 | Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args, | |
6602 | bool is_varargs, source_location location); | |
6603 | ||
6604 | protected: | |
6605 | // This overrides Call_expression::do_lower. | |
6606 | Expression* | |
6607 | do_lower(Gogo*, Named_object*, int); | |
6608 | ||
6609 | bool | |
6610 | do_is_constant() const; | |
6611 | ||
6612 | bool | |
6613 | do_integer_constant_value(bool, mpz_t, Type**) const; | |
6614 | ||
6615 | bool | |
6616 | do_float_constant_value(mpfr_t, Type**) const; | |
6617 | ||
6618 | bool | |
6619 | do_complex_constant_value(mpfr_t, mpfr_t, Type**) const; | |
6620 | ||
6621 | Type* | |
6622 | do_type(); | |
6623 | ||
6624 | void | |
6625 | do_determine_type(const Type_context*); | |
6626 | ||
6627 | void | |
6628 | do_check_types(Gogo*); | |
6629 | ||
6630 | Expression* | |
6631 | do_copy() | |
6632 | { | |
6633 | return new Builtin_call_expression(this->gogo_, this->fn()->copy(), | |
6634 | this->args()->copy(), | |
6635 | this->is_varargs(), | |
6636 | this->location()); | |
6637 | } | |
6638 | ||
6639 | tree | |
6640 | do_get_tree(Translate_context*); | |
6641 | ||
6642 | void | |
6643 | do_export(Export*) const; | |
6644 | ||
6645 | virtual bool | |
6646 | do_is_recover_call() const; | |
6647 | ||
6648 | virtual void | |
6649 | do_set_recover_arg(Expression*); | |
6650 | ||
6651 | private: | |
6652 | // The builtin functions. | |
6653 | enum Builtin_function_code | |
6654 | { | |
6655 | BUILTIN_INVALID, | |
6656 | ||
6657 | // Predeclared builtin functions. | |
6658 | BUILTIN_APPEND, | |
6659 | BUILTIN_CAP, | |
6660 | BUILTIN_CLOSE, | |
48080209 | 6661 | BUILTIN_COMPLEX, |
e440a328 | 6662 | BUILTIN_COPY, |
6663 | BUILTIN_IMAG, | |
6664 | BUILTIN_LEN, | |
6665 | BUILTIN_MAKE, | |
6666 | BUILTIN_NEW, | |
6667 | BUILTIN_PANIC, | |
6668 | BUILTIN_PRINT, | |
6669 | BUILTIN_PRINTLN, | |
6670 | BUILTIN_REAL, | |
6671 | BUILTIN_RECOVER, | |
6672 | ||
6673 | // Builtin functions from the unsafe package. | |
6674 | BUILTIN_ALIGNOF, | |
6675 | BUILTIN_OFFSETOF, | |
6676 | BUILTIN_SIZEOF | |
6677 | }; | |
6678 | ||
6679 | Expression* | |
6680 | one_arg() const; | |
6681 | ||
6682 | bool | |
6683 | check_one_arg(); | |
6684 | ||
6685 | static Type* | |
6686 | real_imag_type(Type*); | |
6687 | ||
6688 | static Type* | |
48080209 | 6689 | complex_type(Type*); |
e440a328 | 6690 | |
6691 | // A pointer back to the general IR structure. This avoids a global | |
6692 | // variable, or passing it around everywhere. | |
6693 | Gogo* gogo_; | |
6694 | // The builtin function being called. | |
6695 | Builtin_function_code code_; | |
0f914071 | 6696 | // Used to stop endless loops when the length of an array uses len |
6697 | // or cap of the array itself. | |
6698 | mutable bool seen_; | |
e440a328 | 6699 | }; |
6700 | ||
6701 | Builtin_call_expression::Builtin_call_expression(Gogo* gogo, | |
6702 | Expression* fn, | |
6703 | Expression_list* args, | |
6704 | bool is_varargs, | |
6705 | source_location location) | |
6706 | : Call_expression(fn, args, is_varargs, location), | |
0f914071 | 6707 | gogo_(gogo), code_(BUILTIN_INVALID), seen_(false) |
e440a328 | 6708 | { |
6709 | Func_expression* fnexp = this->fn()->func_expression(); | |
6710 | gcc_assert(fnexp != NULL); | |
6711 | const std::string& name(fnexp->named_object()->name()); | |
6712 | if (name == "append") | |
6713 | this->code_ = BUILTIN_APPEND; | |
6714 | else if (name == "cap") | |
6715 | this->code_ = BUILTIN_CAP; | |
6716 | else if (name == "close") | |
6717 | this->code_ = BUILTIN_CLOSE; | |
48080209 | 6718 | else if (name == "complex") |
6719 | this->code_ = BUILTIN_COMPLEX; | |
e440a328 | 6720 | else if (name == "copy") |
6721 | this->code_ = BUILTIN_COPY; | |
6722 | else if (name == "imag") | |
6723 | this->code_ = BUILTIN_IMAG; | |
6724 | else if (name == "len") | |
6725 | this->code_ = BUILTIN_LEN; | |
6726 | else if (name == "make") | |
6727 | this->code_ = BUILTIN_MAKE; | |
6728 | else if (name == "new") | |
6729 | this->code_ = BUILTIN_NEW; | |
6730 | else if (name == "panic") | |
6731 | this->code_ = BUILTIN_PANIC; | |
6732 | else if (name == "print") | |
6733 | this->code_ = BUILTIN_PRINT; | |
6734 | else if (name == "println") | |
6735 | this->code_ = BUILTIN_PRINTLN; | |
6736 | else if (name == "real") | |
6737 | this->code_ = BUILTIN_REAL; | |
6738 | else if (name == "recover") | |
6739 | this->code_ = BUILTIN_RECOVER; | |
6740 | else if (name == "Alignof") | |
6741 | this->code_ = BUILTIN_ALIGNOF; | |
6742 | else if (name == "Offsetof") | |
6743 | this->code_ = BUILTIN_OFFSETOF; | |
6744 | else if (name == "Sizeof") | |
6745 | this->code_ = BUILTIN_SIZEOF; | |
6746 | else | |
6747 | gcc_unreachable(); | |
6748 | } | |
6749 | ||
6750 | // Return whether this is a call to recover. This is a virtual | |
6751 | // function called from the parent class. | |
6752 | ||
6753 | bool | |
6754 | Builtin_call_expression::do_is_recover_call() const | |
6755 | { | |
6756 | if (this->classification() == EXPRESSION_ERROR) | |
6757 | return false; | |
6758 | return this->code_ == BUILTIN_RECOVER; | |
6759 | } | |
6760 | ||
6761 | // Set the argument for a call to recover. | |
6762 | ||
6763 | void | |
6764 | Builtin_call_expression::do_set_recover_arg(Expression* arg) | |
6765 | { | |
6766 | const Expression_list* args = this->args(); | |
6767 | gcc_assert(args == NULL || args->empty()); | |
6768 | Expression_list* new_args = new Expression_list(); | |
6769 | new_args->push_back(arg); | |
6770 | this->set_args(new_args); | |
6771 | } | |
6772 | ||
6773 | // A traversal class which looks for a call expression. | |
6774 | ||
6775 | class Find_call_expression : public Traverse | |
6776 | { | |
6777 | public: | |
6778 | Find_call_expression() | |
6779 | : Traverse(traverse_expressions), | |
6780 | found_(false) | |
6781 | { } | |
6782 | ||
6783 | int | |
6784 | expression(Expression**); | |
6785 | ||
6786 | bool | |
6787 | found() | |
6788 | { return this->found_; } | |
6789 | ||
6790 | private: | |
6791 | bool found_; | |
6792 | }; | |
6793 | ||
6794 | int | |
6795 | Find_call_expression::expression(Expression** pexpr) | |
6796 | { | |
6797 | if ((*pexpr)->call_expression() != NULL) | |
6798 | { | |
6799 | this->found_ = true; | |
6800 | return TRAVERSE_EXIT; | |
6801 | } | |
6802 | return TRAVERSE_CONTINUE; | |
6803 | } | |
6804 | ||
6805 | // Lower a builtin call expression. This turns new and make into | |
6806 | // specific expressions. We also convert to a constant if we can. | |
6807 | ||
6808 | Expression* | |
6809 | Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
6810 | { | |
a8725655 | 6811 | if (this->is_varargs() && this->code_ != BUILTIN_APPEND) |
6812 | { | |
6813 | this->report_error(_("invalid use of %<...%> with builtin function")); | |
6814 | return Expression::make_error(this->location()); | |
6815 | } | |
6816 | ||
e440a328 | 6817 | if (this->code_ == BUILTIN_NEW) |
6818 | { | |
6819 | const Expression_list* args = this->args(); | |
6820 | if (args == NULL || args->size() < 1) | |
6821 | this->report_error(_("not enough arguments")); | |
6822 | else if (args->size() > 1) | |
6823 | this->report_error(_("too many arguments")); | |
6824 | else | |
6825 | { | |
6826 | Expression* arg = args->front(); | |
6827 | if (!arg->is_type_expression()) | |
6828 | { | |
6829 | error_at(arg->location(), "expected type"); | |
6830 | this->set_is_error(); | |
6831 | } | |
6832 | else | |
6833 | return Expression::make_allocation(arg->type(), this->location()); | |
6834 | } | |
6835 | } | |
6836 | else if (this->code_ == BUILTIN_MAKE) | |
6837 | { | |
6838 | const Expression_list* args = this->args(); | |
6839 | if (args == NULL || args->size() < 1) | |
6840 | this->report_error(_("not enough arguments")); | |
6841 | else | |
6842 | { | |
6843 | Expression* arg = args->front(); | |
6844 | if (!arg->is_type_expression()) | |
6845 | { | |
6846 | error_at(arg->location(), "expected type"); | |
6847 | this->set_is_error(); | |
6848 | } | |
6849 | else | |
6850 | { | |
6851 | Expression_list* newargs; | |
6852 | if (args->size() == 1) | |
6853 | newargs = NULL; | |
6854 | else | |
6855 | { | |
6856 | newargs = new Expression_list(); | |
6857 | Expression_list::const_iterator p = args->begin(); | |
6858 | ++p; | |
6859 | for (; p != args->end(); ++p) | |
6860 | newargs->push_back(*p); | |
6861 | } | |
6862 | return Expression::make_make(arg->type(), newargs, | |
6863 | this->location()); | |
6864 | } | |
6865 | } | |
6866 | } | |
6867 | else if (this->is_constant()) | |
6868 | { | |
6869 | // We can only lower len and cap if there are no function calls | |
6870 | // in the arguments. Otherwise we have to make the call. | |
6871 | if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP) | |
6872 | { | |
6873 | Expression* arg = this->one_arg(); | |
6874 | if (!arg->is_constant()) | |
6875 | { | |
6876 | Find_call_expression find_call; | |
6877 | Expression::traverse(&arg, &find_call); | |
6878 | if (find_call.found()) | |
6879 | return this; | |
6880 | } | |
6881 | } | |
6882 | ||
6883 | mpz_t ival; | |
6884 | mpz_init(ival); | |
6885 | Type* type; | |
6886 | if (this->integer_constant_value(true, ival, &type)) | |
6887 | { | |
6888 | Expression* ret = Expression::make_integer(&ival, type, | |
6889 | this->location()); | |
6890 | mpz_clear(ival); | |
6891 | return ret; | |
6892 | } | |
6893 | mpz_clear(ival); | |
6894 | ||
6895 | mpfr_t rval; | |
6896 | mpfr_init(rval); | |
6897 | if (this->float_constant_value(rval, &type)) | |
6898 | { | |
6899 | Expression* ret = Expression::make_float(&rval, type, | |
6900 | this->location()); | |
6901 | mpfr_clear(rval); | |
6902 | return ret; | |
6903 | } | |
6904 | ||
6905 | mpfr_t imag; | |
6906 | mpfr_init(imag); | |
6907 | if (this->complex_constant_value(rval, imag, &type)) | |
6908 | { | |
6909 | Expression* ret = Expression::make_complex(&rval, &imag, type, | |
6910 | this->location()); | |
6911 | mpfr_clear(rval); | |
6912 | mpfr_clear(imag); | |
6913 | return ret; | |
6914 | } | |
6915 | mpfr_clear(rval); | |
6916 | mpfr_clear(imag); | |
6917 | } | |
6918 | else if (this->code_ == BUILTIN_RECOVER) | |
6919 | { | |
6920 | if (function != NULL) | |
6921 | function->func_value()->set_calls_recover(); | |
6922 | else | |
6923 | { | |
6924 | // Calling recover outside of a function always returns the | |
6925 | // nil empty interface. | |
6926 | Type* eface = Type::make_interface_type(NULL, this->location()); | |
6927 | return Expression::make_cast(eface, | |
6928 | Expression::make_nil(this->location()), | |
6929 | this->location()); | |
6930 | } | |
6931 | } | |
6932 | else if (this->code_ == BUILTIN_APPEND) | |
6933 | { | |
6934 | // Lower the varargs. | |
6935 | const Expression_list* args = this->args(); | |
6936 | if (args == NULL || args->empty()) | |
6937 | return this; | |
6938 | Type* slice_type = args->front()->type(); | |
6939 | if (!slice_type->is_open_array_type()) | |
6940 | { | |
6941 | error_at(args->front()->location(), "argument 1 must be a slice"); | |
6942 | this->set_is_error(); | |
6943 | return this; | |
6944 | } | |
6945 | return this->lower_varargs(gogo, function, slice_type, 2); | |
6946 | } | |
6947 | ||
6948 | return this; | |
6949 | } | |
6950 | ||
6951 | // Return the type of the real or imag functions, given the type of | |
6952 | // the argument. We need to map complex to float, complex64 to | |
6953 | // float32, and complex128 to float64, so it has to be done by name. | |
6954 | // This returns NULL if it can't figure out the type. | |
6955 | ||
6956 | Type* | |
6957 | Builtin_call_expression::real_imag_type(Type* arg_type) | |
6958 | { | |
6959 | if (arg_type == NULL || arg_type->is_abstract()) | |
6960 | return NULL; | |
6961 | Named_type* nt = arg_type->named_type(); | |
6962 | if (nt == NULL) | |
6963 | return NULL; | |
6964 | while (nt->real_type()->named_type() != NULL) | |
6965 | nt = nt->real_type()->named_type(); | |
48080209 | 6966 | if (nt->name() == "complex64") |
e440a328 | 6967 | return Type::lookup_float_type("float32"); |
6968 | else if (nt->name() == "complex128") | |
6969 | return Type::lookup_float_type("float64"); | |
6970 | else | |
6971 | return NULL; | |
6972 | } | |
6973 | ||
48080209 | 6974 | // Return the type of the complex function, given the type of one of the |
e440a328 | 6975 | // argments. Like real_imag_type, we have to map by name. |
6976 | ||
6977 | Type* | |
48080209 | 6978 | Builtin_call_expression::complex_type(Type* arg_type) |
e440a328 | 6979 | { |
6980 | if (arg_type == NULL || arg_type->is_abstract()) | |
6981 | return NULL; | |
6982 | Named_type* nt = arg_type->named_type(); | |
6983 | if (nt == NULL) | |
6984 | return NULL; | |
6985 | while (nt->real_type()->named_type() != NULL) | |
6986 | nt = nt->real_type()->named_type(); | |
48080209 | 6987 | if (nt->name() == "float32") |
e440a328 | 6988 | return Type::lookup_complex_type("complex64"); |
6989 | else if (nt->name() == "float64") | |
6990 | return Type::lookup_complex_type("complex128"); | |
6991 | else | |
6992 | return NULL; | |
6993 | } | |
6994 | ||
6995 | // Return a single argument, or NULL if there isn't one. | |
6996 | ||
6997 | Expression* | |
6998 | Builtin_call_expression::one_arg() const | |
6999 | { | |
7000 | const Expression_list* args = this->args(); | |
7001 | if (args->size() != 1) | |
7002 | return NULL; | |
7003 | return args->front(); | |
7004 | } | |
7005 | ||
7006 | // Return whether this is constant: len of a string, or len or cap of | |
7007 | // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof. | |
7008 | ||
7009 | bool | |
7010 | Builtin_call_expression::do_is_constant() const | |
7011 | { | |
7012 | switch (this->code_) | |
7013 | { | |
7014 | case BUILTIN_LEN: | |
7015 | case BUILTIN_CAP: | |
7016 | { | |
0f914071 | 7017 | if (this->seen_) |
7018 | return false; | |
7019 | ||
e440a328 | 7020 | Expression* arg = this->one_arg(); |
7021 | if (arg == NULL) | |
7022 | return false; | |
7023 | Type* arg_type = arg->type(); | |
7024 | ||
7025 | if (arg_type->points_to() != NULL | |
7026 | && arg_type->points_to()->array_type() != NULL | |
7027 | && !arg_type->points_to()->is_open_array_type()) | |
7028 | arg_type = arg_type->points_to(); | |
7029 | ||
7030 | if (arg_type->array_type() != NULL | |
7031 | && arg_type->array_type()->length() != NULL) | |
0f914071 | 7032 | return true; |
e440a328 | 7033 | |
7034 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
0f914071 | 7035 | { |
7036 | this->seen_ = true; | |
7037 | bool ret = arg->is_constant(); | |
7038 | this->seen_ = false; | |
7039 | return ret; | |
7040 | } | |
e440a328 | 7041 | } |
7042 | break; | |
7043 | ||
7044 | case BUILTIN_SIZEOF: | |
7045 | case BUILTIN_ALIGNOF: | |
7046 | return this->one_arg() != NULL; | |
7047 | ||
7048 | case BUILTIN_OFFSETOF: | |
7049 | { | |
7050 | Expression* arg = this->one_arg(); | |
7051 | if (arg == NULL) | |
7052 | return false; | |
7053 | return arg->field_reference_expression() != NULL; | |
7054 | } | |
7055 | ||
48080209 | 7056 | case BUILTIN_COMPLEX: |
e440a328 | 7057 | { |
7058 | const Expression_list* args = this->args(); | |
7059 | if (args != NULL && args->size() == 2) | |
7060 | return args->front()->is_constant() && args->back()->is_constant(); | |
7061 | } | |
7062 | break; | |
7063 | ||
7064 | case BUILTIN_REAL: | |
7065 | case BUILTIN_IMAG: | |
7066 | { | |
7067 | Expression* arg = this->one_arg(); | |
7068 | return arg != NULL && arg->is_constant(); | |
7069 | } | |
7070 | ||
7071 | default: | |
7072 | break; | |
7073 | } | |
7074 | ||
7075 | return false; | |
7076 | } | |
7077 | ||
7078 | // Return an integer constant value if possible. | |
7079 | ||
7080 | bool | |
7081 | Builtin_call_expression::do_integer_constant_value(bool iota_is_constant, | |
7082 | mpz_t val, | |
7083 | Type** ptype) const | |
7084 | { | |
7085 | if (this->code_ == BUILTIN_LEN | |
7086 | || this->code_ == BUILTIN_CAP) | |
7087 | { | |
7088 | Expression* arg = this->one_arg(); | |
7089 | if (arg == NULL) | |
7090 | return false; | |
7091 | Type* arg_type = arg->type(); | |
7092 | ||
7093 | if (this->code_ == BUILTIN_LEN && arg_type->is_string_type()) | |
7094 | { | |
7095 | std::string sval; | |
7096 | if (arg->string_constant_value(&sval)) | |
7097 | { | |
7098 | mpz_set_ui(val, sval.length()); | |
7099 | *ptype = Type::lookup_integer_type("int"); | |
7100 | return true; | |
7101 | } | |
7102 | } | |
7103 | ||
7104 | if (arg_type->points_to() != NULL | |
7105 | && arg_type->points_to()->array_type() != NULL | |
7106 | && !arg_type->points_to()->is_open_array_type()) | |
7107 | arg_type = arg_type->points_to(); | |
7108 | ||
7109 | if (arg_type->array_type() != NULL | |
7110 | && arg_type->array_type()->length() != NULL) | |
7111 | { | |
0f914071 | 7112 | if (this->seen_) |
7113 | return false; | |
e440a328 | 7114 | Expression* e = arg_type->array_type()->length(); |
0f914071 | 7115 | this->seen_ = true; |
7116 | bool r = e->integer_constant_value(iota_is_constant, val, ptype); | |
7117 | this->seen_ = false; | |
7118 | if (r) | |
e440a328 | 7119 | { |
7120 | *ptype = Type::lookup_integer_type("int"); | |
7121 | return true; | |
7122 | } | |
7123 | } | |
7124 | } | |
7125 | else if (this->code_ == BUILTIN_SIZEOF | |
7126 | || this->code_ == BUILTIN_ALIGNOF) | |
7127 | { | |
7128 | Expression* arg = this->one_arg(); | |
7129 | if (arg == NULL) | |
7130 | return false; | |
7131 | Type* arg_type = arg->type(); | |
5c13bd80 | 7132 | if (arg_type->is_error()) |
e440a328 | 7133 | return false; |
7134 | if (arg_type->is_abstract()) | |
7135 | return false; | |
9aa9e2df | 7136 | if (arg_type->named_type() != NULL) |
7137 | arg_type->named_type()->convert(this->gogo_); | |
e440a328 | 7138 | tree arg_type_tree = arg_type->get_tree(this->gogo_); |
f690b0bb | 7139 | if (arg_type_tree == error_mark_node) |
7140 | return false; | |
e440a328 | 7141 | unsigned long val_long; |
7142 | if (this->code_ == BUILTIN_SIZEOF) | |
7143 | { | |
7144 | tree type_size = TYPE_SIZE_UNIT(arg_type_tree); | |
7145 | gcc_assert(TREE_CODE(type_size) == INTEGER_CST); | |
7146 | if (TREE_INT_CST_HIGH(type_size) != 0) | |
7147 | return false; | |
7148 | unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size); | |
7149 | val_long = static_cast<unsigned long>(val_wide); | |
7150 | if (val_long != val_wide) | |
7151 | return false; | |
7152 | } | |
7153 | else if (this->code_ == BUILTIN_ALIGNOF) | |
7154 | { | |
637bd3af | 7155 | if (arg->field_reference_expression() == NULL) |
7156 | val_long = go_type_alignment(arg_type_tree); | |
7157 | else | |
e440a328 | 7158 | { |
7159 | // Calling unsafe.Alignof(s.f) returns the alignment of | |
7160 | // the type of f when it is used as a field in a struct. | |
637bd3af | 7161 | val_long = go_field_alignment(arg_type_tree); |
e440a328 | 7162 | } |
e440a328 | 7163 | } |
7164 | else | |
7165 | gcc_unreachable(); | |
7166 | mpz_set_ui(val, val_long); | |
7167 | *ptype = NULL; | |
7168 | return true; | |
7169 | } | |
7170 | else if (this->code_ == BUILTIN_OFFSETOF) | |
7171 | { | |
7172 | Expression* arg = this->one_arg(); | |
7173 | if (arg == NULL) | |
7174 | return false; | |
7175 | Field_reference_expression* farg = arg->field_reference_expression(); | |
7176 | if (farg == NULL) | |
7177 | return false; | |
7178 | Expression* struct_expr = farg->expr(); | |
7179 | Type* st = struct_expr->type(); | |
7180 | if (st->struct_type() == NULL) | |
7181 | return false; | |
9aa9e2df | 7182 | if (st->named_type() != NULL) |
7183 | st->named_type()->convert(this->gogo_); | |
e440a328 | 7184 | tree struct_tree = st->get_tree(this->gogo_); |
7185 | gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE); | |
7186 | tree field = TYPE_FIELDS(struct_tree); | |
7187 | for (unsigned int index = farg->field_index(); index > 0; --index) | |
7188 | { | |
7189 | field = DECL_CHAIN(field); | |
7190 | gcc_assert(field != NULL_TREE); | |
7191 | } | |
7192 | HOST_WIDE_INT offset_wide = int_byte_position (field); | |
7193 | if (offset_wide < 0) | |
7194 | return false; | |
7195 | unsigned long offset_long = static_cast<unsigned long>(offset_wide); | |
7196 | if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide)) | |
7197 | return false; | |
7198 | mpz_set_ui(val, offset_long); | |
7199 | return true; | |
7200 | } | |
7201 | return false; | |
7202 | } | |
7203 | ||
7204 | // Return a floating point constant value if possible. | |
7205 | ||
7206 | bool | |
7207 | Builtin_call_expression::do_float_constant_value(mpfr_t val, | |
7208 | Type** ptype) const | |
7209 | { | |
7210 | if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG) | |
7211 | { | |
7212 | Expression* arg = this->one_arg(); | |
7213 | if (arg == NULL) | |
7214 | return false; | |
7215 | ||
7216 | mpfr_t real; | |
7217 | mpfr_t imag; | |
7218 | mpfr_init(real); | |
7219 | mpfr_init(imag); | |
7220 | ||
7221 | bool ret = false; | |
7222 | Type* type; | |
7223 | if (arg->complex_constant_value(real, imag, &type)) | |
7224 | { | |
7225 | if (this->code_ == BUILTIN_REAL) | |
7226 | mpfr_set(val, real, GMP_RNDN); | |
7227 | else | |
7228 | mpfr_set(val, imag, GMP_RNDN); | |
7229 | *ptype = Builtin_call_expression::real_imag_type(type); | |
7230 | ret = true; | |
7231 | } | |
7232 | ||
7233 | mpfr_clear(real); | |
7234 | mpfr_clear(imag); | |
7235 | return ret; | |
7236 | } | |
7237 | ||
7238 | return false; | |
7239 | } | |
7240 | ||
7241 | // Return a complex constant value if possible. | |
7242 | ||
7243 | bool | |
7244 | Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag, | |
7245 | Type** ptype) const | |
7246 | { | |
48080209 | 7247 | if (this->code_ == BUILTIN_COMPLEX) |
e440a328 | 7248 | { |
7249 | const Expression_list* args = this->args(); | |
7250 | if (args == NULL || args->size() != 2) | |
7251 | return false; | |
7252 | ||
7253 | mpfr_t r; | |
7254 | mpfr_init(r); | |
7255 | Type* rtype; | |
7256 | if (!args->front()->float_constant_value(r, &rtype)) | |
7257 | { | |
7258 | mpfr_clear(r); | |
7259 | return false; | |
7260 | } | |
7261 | ||
7262 | mpfr_t i; | |
7263 | mpfr_init(i); | |
7264 | ||
7265 | bool ret = false; | |
7266 | Type* itype; | |
7267 | if (args->back()->float_constant_value(i, &itype) | |
07ba8be5 | 7268 | && Type::are_identical(rtype, itype, false, NULL)) |
e440a328 | 7269 | { |
7270 | mpfr_set(real, r, GMP_RNDN); | |
7271 | mpfr_set(imag, i, GMP_RNDN); | |
48080209 | 7272 | *ptype = Builtin_call_expression::complex_type(rtype); |
e440a328 | 7273 | ret = true; |
7274 | } | |
7275 | ||
7276 | mpfr_clear(r); | |
7277 | mpfr_clear(i); | |
7278 | ||
7279 | return ret; | |
7280 | } | |
7281 | ||
7282 | return false; | |
7283 | } | |
7284 | ||
7285 | // Return the type. | |
7286 | ||
7287 | Type* | |
7288 | Builtin_call_expression::do_type() | |
7289 | { | |
7290 | switch (this->code_) | |
7291 | { | |
7292 | case BUILTIN_INVALID: | |
7293 | default: | |
7294 | gcc_unreachable(); | |
7295 | ||
7296 | case BUILTIN_NEW: | |
7297 | case BUILTIN_MAKE: | |
7298 | { | |
7299 | const Expression_list* args = this->args(); | |
7300 | if (args == NULL || args->empty()) | |
7301 | return Type::make_error_type(); | |
7302 | return Type::make_pointer_type(args->front()->type()); | |
7303 | } | |
7304 | ||
7305 | case BUILTIN_CAP: | |
7306 | case BUILTIN_COPY: | |
7307 | case BUILTIN_LEN: | |
7308 | case BUILTIN_ALIGNOF: | |
7309 | case BUILTIN_OFFSETOF: | |
7310 | case BUILTIN_SIZEOF: | |
7311 | return Type::lookup_integer_type("int"); | |
7312 | ||
7313 | case BUILTIN_CLOSE: | |
7314 | case BUILTIN_PANIC: | |
7315 | case BUILTIN_PRINT: | |
7316 | case BUILTIN_PRINTLN: | |
7317 | return Type::make_void_type(); | |
7318 | ||
e440a328 | 7319 | case BUILTIN_RECOVER: |
7320 | return Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
7321 | ||
7322 | case BUILTIN_APPEND: | |
7323 | { | |
7324 | const Expression_list* args = this->args(); | |
7325 | if (args == NULL || args->empty()) | |
7326 | return Type::make_error_type(); | |
7327 | return args->front()->type(); | |
7328 | } | |
7329 | ||
7330 | case BUILTIN_REAL: | |
7331 | case BUILTIN_IMAG: | |
7332 | { | |
7333 | Expression* arg = this->one_arg(); | |
7334 | if (arg == NULL) | |
7335 | return Type::make_error_type(); | |
7336 | Type* t = arg->type(); | |
7337 | if (t->is_abstract()) | |
7338 | t = t->make_non_abstract_type(); | |
7339 | t = Builtin_call_expression::real_imag_type(t); | |
7340 | if (t == NULL) | |
7341 | t = Type::make_error_type(); | |
7342 | return t; | |
7343 | } | |
7344 | ||
48080209 | 7345 | case BUILTIN_COMPLEX: |
e440a328 | 7346 | { |
7347 | const Expression_list* args = this->args(); | |
7348 | if (args == NULL || args->size() != 2) | |
7349 | return Type::make_error_type(); | |
7350 | Type* t = args->front()->type(); | |
7351 | if (t->is_abstract()) | |
7352 | { | |
7353 | t = args->back()->type(); | |
7354 | if (t->is_abstract()) | |
7355 | t = t->make_non_abstract_type(); | |
7356 | } | |
48080209 | 7357 | t = Builtin_call_expression::complex_type(t); |
e440a328 | 7358 | if (t == NULL) |
7359 | t = Type::make_error_type(); | |
7360 | return t; | |
7361 | } | |
7362 | } | |
7363 | } | |
7364 | ||
7365 | // Determine the type. | |
7366 | ||
7367 | void | |
7368 | Builtin_call_expression::do_determine_type(const Type_context* context) | |
7369 | { | |
fb94b0ca | 7370 | if (!this->determining_types()) |
7371 | return; | |
7372 | ||
e440a328 | 7373 | this->fn()->determine_type_no_context(); |
7374 | ||
7375 | const Expression_list* args = this->args(); | |
7376 | ||
7377 | bool is_print; | |
7378 | Type* arg_type = NULL; | |
7379 | switch (this->code_) | |
7380 | { | |
7381 | case BUILTIN_PRINT: | |
7382 | case BUILTIN_PRINTLN: | |
7383 | // Do not force a large integer constant to "int". | |
7384 | is_print = true; | |
7385 | break; | |
7386 | ||
7387 | case BUILTIN_REAL: | |
7388 | case BUILTIN_IMAG: | |
48080209 | 7389 | arg_type = Builtin_call_expression::complex_type(context->type); |
e440a328 | 7390 | is_print = false; |
7391 | break; | |
7392 | ||
48080209 | 7393 | case BUILTIN_COMPLEX: |
e440a328 | 7394 | { |
48080209 | 7395 | // For the complex function the type of one operand can |
e440a328 | 7396 | // determine the type of the other, as in a binary expression. |
7397 | arg_type = Builtin_call_expression::real_imag_type(context->type); | |
7398 | if (args != NULL && args->size() == 2) | |
7399 | { | |
7400 | Type* t1 = args->front()->type(); | |
7401 | Type* t2 = args->front()->type(); | |
7402 | if (!t1->is_abstract()) | |
7403 | arg_type = t1; | |
7404 | else if (!t2->is_abstract()) | |
7405 | arg_type = t2; | |
7406 | } | |
7407 | is_print = false; | |
7408 | } | |
7409 | break; | |
7410 | ||
7411 | default: | |
7412 | is_print = false; | |
7413 | break; | |
7414 | } | |
7415 | ||
7416 | if (args != NULL) | |
7417 | { | |
7418 | for (Expression_list::const_iterator pa = args->begin(); | |
7419 | pa != args->end(); | |
7420 | ++pa) | |
7421 | { | |
7422 | Type_context subcontext; | |
7423 | subcontext.type = arg_type; | |
7424 | ||
7425 | if (is_print) | |
7426 | { | |
7427 | // We want to print large constants, we so can't just | |
7428 | // use the appropriate nonabstract type. Use uint64 for | |
7429 | // an integer if we know it is nonnegative, otherwise | |
7430 | // use int64 for a integer, otherwise use float64 for a | |
7431 | // float or complex128 for a complex. | |
7432 | Type* want_type = NULL; | |
7433 | Type* atype = (*pa)->type(); | |
7434 | if (atype->is_abstract()) | |
7435 | { | |
7436 | if (atype->integer_type() != NULL) | |
7437 | { | |
7438 | mpz_t val; | |
7439 | mpz_init(val); | |
7440 | Type* dummy; | |
7441 | if (this->integer_constant_value(true, val, &dummy) | |
7442 | && mpz_sgn(val) >= 0) | |
7443 | want_type = Type::lookup_integer_type("uint64"); | |
7444 | else | |
7445 | want_type = Type::lookup_integer_type("int64"); | |
7446 | mpz_clear(val); | |
7447 | } | |
7448 | else if (atype->float_type() != NULL) | |
7449 | want_type = Type::lookup_float_type("float64"); | |
7450 | else if (atype->complex_type() != NULL) | |
7451 | want_type = Type::lookup_complex_type("complex128"); | |
7452 | else if (atype->is_abstract_string_type()) | |
7453 | want_type = Type::lookup_string_type(); | |
7454 | else if (atype->is_abstract_boolean_type()) | |
7455 | want_type = Type::lookup_bool_type(); | |
7456 | else | |
7457 | gcc_unreachable(); | |
7458 | subcontext.type = want_type; | |
7459 | } | |
7460 | } | |
7461 | ||
7462 | (*pa)->determine_type(&subcontext); | |
7463 | } | |
7464 | } | |
7465 | } | |
7466 | ||
7467 | // If there is exactly one argument, return true. Otherwise give an | |
7468 | // error message and return false. | |
7469 | ||
7470 | bool | |
7471 | Builtin_call_expression::check_one_arg() | |
7472 | { | |
7473 | const Expression_list* args = this->args(); | |
7474 | if (args == NULL || args->size() < 1) | |
7475 | { | |
7476 | this->report_error(_("not enough arguments")); | |
7477 | return false; | |
7478 | } | |
7479 | else if (args->size() > 1) | |
7480 | { | |
7481 | this->report_error(_("too many arguments")); | |
7482 | return false; | |
7483 | } | |
7484 | if (args->front()->is_error_expression() | |
5c13bd80 | 7485 | || args->front()->type()->is_error()) |
e440a328 | 7486 | { |
7487 | this->set_is_error(); | |
7488 | return false; | |
7489 | } | |
7490 | return true; | |
7491 | } | |
7492 | ||
7493 | // Check argument types for a builtin function. | |
7494 | ||
7495 | void | |
7496 | Builtin_call_expression::do_check_types(Gogo*) | |
7497 | { | |
7498 | switch (this->code_) | |
7499 | { | |
7500 | case BUILTIN_INVALID: | |
7501 | case BUILTIN_NEW: | |
7502 | case BUILTIN_MAKE: | |
7503 | return; | |
7504 | ||
7505 | case BUILTIN_LEN: | |
7506 | case BUILTIN_CAP: | |
7507 | { | |
7508 | // The single argument may be either a string or an array or a | |
7509 | // map or a channel, or a pointer to a closed array. | |
7510 | if (this->check_one_arg()) | |
7511 | { | |
7512 | Type* arg_type = this->one_arg()->type(); | |
7513 | if (arg_type->points_to() != NULL | |
7514 | && arg_type->points_to()->array_type() != NULL | |
7515 | && !arg_type->points_to()->is_open_array_type()) | |
7516 | arg_type = arg_type->points_to(); | |
7517 | if (this->code_ == BUILTIN_CAP) | |
7518 | { | |
5c13bd80 | 7519 | if (!arg_type->is_error() |
e440a328 | 7520 | && arg_type->array_type() == NULL |
7521 | && arg_type->channel_type() == NULL) | |
7522 | this->report_error(_("argument must be array or slice " | |
7523 | "or channel")); | |
7524 | } | |
7525 | else | |
7526 | { | |
5c13bd80 | 7527 | if (!arg_type->is_error() |
e440a328 | 7528 | && !arg_type->is_string_type() |
7529 | && arg_type->array_type() == NULL | |
7530 | && arg_type->map_type() == NULL | |
7531 | && arg_type->channel_type() == NULL) | |
7532 | this->report_error(_("argument must be string or " | |
7533 | "array or slice or map or channel")); | |
7534 | } | |
7535 | } | |
7536 | } | |
7537 | break; | |
7538 | ||
7539 | case BUILTIN_PRINT: | |
7540 | case BUILTIN_PRINTLN: | |
7541 | { | |
7542 | const Expression_list* args = this->args(); | |
7543 | if (args == NULL) | |
7544 | { | |
7545 | if (this->code_ == BUILTIN_PRINT) | |
7546 | warning_at(this->location(), 0, | |
7547 | "no arguments for builtin function %<%s%>", | |
7548 | (this->code_ == BUILTIN_PRINT | |
7549 | ? "print" | |
7550 | : "println")); | |
7551 | } | |
7552 | else | |
7553 | { | |
7554 | for (Expression_list::const_iterator p = args->begin(); | |
7555 | p != args->end(); | |
7556 | ++p) | |
7557 | { | |
7558 | Type* type = (*p)->type(); | |
5c13bd80 | 7559 | if (type->is_error() |
e440a328 | 7560 | || type->is_string_type() |
7561 | || type->integer_type() != NULL | |
7562 | || type->float_type() != NULL | |
7563 | || type->complex_type() != NULL | |
7564 | || type->is_boolean_type() | |
7565 | || type->points_to() != NULL | |
7566 | || type->interface_type() != NULL | |
7567 | || type->channel_type() != NULL | |
7568 | || type->map_type() != NULL | |
7569 | || type->function_type() != NULL | |
7570 | || type->is_open_array_type()) | |
7571 | ; | |
7572 | else | |
7573 | this->report_error(_("unsupported argument type to " | |
7574 | "builtin function")); | |
7575 | } | |
7576 | } | |
7577 | } | |
7578 | break; | |
7579 | ||
7580 | case BUILTIN_CLOSE: | |
e440a328 | 7581 | if (this->check_one_arg()) |
7582 | { | |
7583 | if (this->one_arg()->type()->channel_type() == NULL) | |
7584 | this->report_error(_("argument must be channel")); | |
7585 | } | |
7586 | break; | |
7587 | ||
7588 | case BUILTIN_PANIC: | |
7589 | case BUILTIN_SIZEOF: | |
7590 | case BUILTIN_ALIGNOF: | |
7591 | this->check_one_arg(); | |
7592 | break; | |
7593 | ||
7594 | case BUILTIN_RECOVER: | |
7595 | if (this->args() != NULL && !this->args()->empty()) | |
7596 | this->report_error(_("too many arguments")); | |
7597 | break; | |
7598 | ||
7599 | case BUILTIN_OFFSETOF: | |
7600 | if (this->check_one_arg()) | |
7601 | { | |
7602 | Expression* arg = this->one_arg(); | |
7603 | if (arg->field_reference_expression() == NULL) | |
7604 | this->report_error(_("argument must be a field reference")); | |
7605 | } | |
7606 | break; | |
7607 | ||
7608 | case BUILTIN_COPY: | |
7609 | { | |
7610 | const Expression_list* args = this->args(); | |
7611 | if (args == NULL || args->size() < 2) | |
7612 | { | |
7613 | this->report_error(_("not enough arguments")); | |
7614 | break; | |
7615 | } | |
7616 | else if (args->size() > 2) | |
7617 | { | |
7618 | this->report_error(_("too many arguments")); | |
7619 | break; | |
7620 | } | |
7621 | Type* arg1_type = args->front()->type(); | |
7622 | Type* arg2_type = args->back()->type(); | |
5c13bd80 | 7623 | if (arg1_type->is_error() || arg2_type->is_error()) |
e440a328 | 7624 | break; |
7625 | ||
7626 | Type* e1; | |
7627 | if (arg1_type->is_open_array_type()) | |
7628 | e1 = arg1_type->array_type()->element_type(); | |
7629 | else | |
7630 | { | |
7631 | this->report_error(_("left argument must be a slice")); | |
7632 | break; | |
7633 | } | |
7634 | ||
7635 | Type* e2; | |
7636 | if (arg2_type->is_open_array_type()) | |
7637 | e2 = arg2_type->array_type()->element_type(); | |
7638 | else if (arg2_type->is_string_type()) | |
7639 | e2 = Type::lookup_integer_type("uint8"); | |
7640 | else | |
7641 | { | |
7642 | this->report_error(_("right argument must be a slice or a string")); | |
7643 | break; | |
7644 | } | |
7645 | ||
07ba8be5 | 7646 | if (!Type::are_identical(e1, e2, true, NULL)) |
e440a328 | 7647 | this->report_error(_("element types must be the same")); |
7648 | } | |
7649 | break; | |
7650 | ||
7651 | case BUILTIN_APPEND: | |
7652 | { | |
7653 | const Expression_list* args = this->args(); | |
b0d311a1 | 7654 | if (args == NULL || args->size() < 2) |
e440a328 | 7655 | { |
7656 | this->report_error(_("not enough arguments")); | |
7657 | break; | |
7658 | } | |
0b7755ec | 7659 | if (args->size() > 2) |
7660 | { | |
7661 | this->report_error(_("too many arguments")); | |
7662 | break; | |
7663 | } | |
e440a328 | 7664 | std::string reason; |
7665 | if (!Type::are_assignable(args->front()->type(), args->back()->type(), | |
7666 | &reason)) | |
7667 | { | |
7668 | if (reason.empty()) | |
7669 | this->report_error(_("arguments 1 and 2 have different types")); | |
7670 | else | |
7671 | { | |
7672 | error_at(this->location(), | |
7673 | "arguments 1 and 2 have different types (%s)", | |
7674 | reason.c_str()); | |
7675 | this->set_is_error(); | |
7676 | } | |
7677 | } | |
7678 | break; | |
7679 | } | |
7680 | ||
7681 | case BUILTIN_REAL: | |
7682 | case BUILTIN_IMAG: | |
7683 | if (this->check_one_arg()) | |
7684 | { | |
7685 | if (this->one_arg()->type()->complex_type() == NULL) | |
7686 | this->report_error(_("argument must have complex type")); | |
7687 | } | |
7688 | break; | |
7689 | ||
48080209 | 7690 | case BUILTIN_COMPLEX: |
e440a328 | 7691 | { |
7692 | const Expression_list* args = this->args(); | |
7693 | if (args == NULL || args->size() < 2) | |
7694 | this->report_error(_("not enough arguments")); | |
7695 | else if (args->size() > 2) | |
7696 | this->report_error(_("too many arguments")); | |
7697 | else if (args->front()->is_error_expression() | |
5c13bd80 | 7698 | || args->front()->type()->is_error() |
e440a328 | 7699 | || args->back()->is_error_expression() |
5c13bd80 | 7700 | || args->back()->type()->is_error()) |
e440a328 | 7701 | this->set_is_error(); |
7702 | else if (!Type::are_identical(args->front()->type(), | |
07ba8be5 | 7703 | args->back()->type(), true, NULL)) |
48080209 | 7704 | this->report_error(_("complex arguments must have identical types")); |
e440a328 | 7705 | else if (args->front()->type()->float_type() == NULL) |
48080209 | 7706 | this->report_error(_("complex arguments must have " |
e440a328 | 7707 | "floating-point type")); |
7708 | } | |
7709 | break; | |
7710 | ||
7711 | default: | |
7712 | gcc_unreachable(); | |
7713 | } | |
7714 | } | |
7715 | ||
7716 | // Return the tree for a builtin function. | |
7717 | ||
7718 | tree | |
7719 | Builtin_call_expression::do_get_tree(Translate_context* context) | |
7720 | { | |
7721 | Gogo* gogo = context->gogo(); | |
7722 | source_location location = this->location(); | |
7723 | switch (this->code_) | |
7724 | { | |
7725 | case BUILTIN_INVALID: | |
7726 | case BUILTIN_NEW: | |
7727 | case BUILTIN_MAKE: | |
7728 | gcc_unreachable(); | |
7729 | ||
7730 | case BUILTIN_LEN: | |
7731 | case BUILTIN_CAP: | |
7732 | { | |
7733 | const Expression_list* args = this->args(); | |
7734 | gcc_assert(args != NULL && args->size() == 1); | |
7735 | Expression* arg = *args->begin(); | |
7736 | Type* arg_type = arg->type(); | |
0f914071 | 7737 | |
7738 | if (this->seen_) | |
7739 | { | |
7740 | gcc_assert(saw_errors()); | |
7741 | return error_mark_node; | |
7742 | } | |
7743 | this->seen_ = true; | |
7744 | ||
e440a328 | 7745 | tree arg_tree = arg->get_tree(context); |
0f914071 | 7746 | |
7747 | this->seen_ = false; | |
7748 | ||
e440a328 | 7749 | if (arg_tree == error_mark_node) |
7750 | return error_mark_node; | |
7751 | ||
7752 | if (arg_type->points_to() != NULL) | |
7753 | { | |
7754 | arg_type = arg_type->points_to(); | |
7755 | gcc_assert(arg_type->array_type() != NULL | |
7756 | && !arg_type->is_open_array_type()); | |
7757 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree))); | |
7758 | arg_tree = build_fold_indirect_ref(arg_tree); | |
7759 | } | |
7760 | ||
7761 | tree val_tree; | |
7762 | if (this->code_ == BUILTIN_LEN) | |
7763 | { | |
7764 | if (arg_type->is_string_type()) | |
7765 | val_tree = String_type::length_tree(gogo, arg_tree); | |
7766 | else if (arg_type->array_type() != NULL) | |
0f914071 | 7767 | { |
7768 | if (this->seen_) | |
7769 | { | |
7770 | gcc_assert(saw_errors()); | |
7771 | return error_mark_node; | |
7772 | } | |
7773 | this->seen_ = true; | |
7774 | val_tree = arg_type->array_type()->length_tree(gogo, arg_tree); | |
7775 | this->seen_ = false; | |
7776 | } | |
e440a328 | 7777 | else if (arg_type->map_type() != NULL) |
7778 | { | |
7779 | static tree map_len_fndecl; | |
7780 | val_tree = Gogo::call_builtin(&map_len_fndecl, | |
7781 | location, | |
7782 | "__go_map_len", | |
7783 | 1, | |
9581e91d | 7784 | integer_type_node, |
e440a328 | 7785 | arg_type->get_tree(gogo), |
7786 | arg_tree); | |
7787 | } | |
7788 | else if (arg_type->channel_type() != NULL) | |
7789 | { | |
7790 | static tree chan_len_fndecl; | |
7791 | val_tree = Gogo::call_builtin(&chan_len_fndecl, | |
7792 | location, | |
7793 | "__go_chan_len", | |
7794 | 1, | |
9581e91d | 7795 | integer_type_node, |
e440a328 | 7796 | arg_type->get_tree(gogo), |
7797 | arg_tree); | |
7798 | } | |
7799 | else | |
7800 | gcc_unreachable(); | |
7801 | } | |
7802 | else | |
7803 | { | |
7804 | if (arg_type->array_type() != NULL) | |
0f914071 | 7805 | { |
7806 | if (this->seen_) | |
7807 | { | |
7808 | gcc_assert(saw_errors()); | |
7809 | return error_mark_node; | |
7810 | } | |
7811 | this->seen_ = true; | |
7812 | val_tree = arg_type->array_type()->capacity_tree(gogo, | |
7813 | arg_tree); | |
7814 | this->seen_ = false; | |
7815 | } | |
e440a328 | 7816 | else if (arg_type->channel_type() != NULL) |
7817 | { | |
7818 | static tree chan_cap_fndecl; | |
7819 | val_tree = Gogo::call_builtin(&chan_cap_fndecl, | |
7820 | location, | |
7821 | "__go_chan_cap", | |
7822 | 1, | |
9581e91d | 7823 | integer_type_node, |
e440a328 | 7824 | arg_type->get_tree(gogo), |
7825 | arg_tree); | |
7826 | } | |
7827 | else | |
7828 | gcc_unreachable(); | |
7829 | } | |
7830 | ||
d8ccb1e3 | 7831 | if (val_tree == error_mark_node) |
7832 | return error_mark_node; | |
7833 | ||
e440a328 | 7834 | tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo); |
7835 | if (type_tree == TREE_TYPE(val_tree)) | |
7836 | return val_tree; | |
7837 | else | |
7838 | return fold(convert_to_integer(type_tree, val_tree)); | |
7839 | } | |
7840 | ||
7841 | case BUILTIN_PRINT: | |
7842 | case BUILTIN_PRINTLN: | |
7843 | { | |
7844 | const bool is_ln = this->code_ == BUILTIN_PRINTLN; | |
7845 | tree stmt_list = NULL_TREE; | |
7846 | ||
7847 | const Expression_list* call_args = this->args(); | |
7848 | if (call_args != NULL) | |
7849 | { | |
7850 | for (Expression_list::const_iterator p = call_args->begin(); | |
7851 | p != call_args->end(); | |
7852 | ++p) | |
7853 | { | |
7854 | if (is_ln && p != call_args->begin()) | |
7855 | { | |
7856 | static tree print_space_fndecl; | |
7857 | tree call = Gogo::call_builtin(&print_space_fndecl, | |
7858 | location, | |
7859 | "__go_print_space", | |
7860 | 0, | |
7861 | void_type_node); | |
5fb82b5e | 7862 | if (call == error_mark_node) |
7863 | return error_mark_node; | |
e440a328 | 7864 | append_to_statement_list(call, &stmt_list); |
7865 | } | |
7866 | ||
7867 | Type* type = (*p)->type(); | |
7868 | ||
7869 | tree arg = (*p)->get_tree(context); | |
7870 | if (arg == error_mark_node) | |
7871 | return error_mark_node; | |
7872 | ||
7873 | tree* pfndecl; | |
7874 | const char* fnname; | |
7875 | if (type->is_string_type()) | |
7876 | { | |
7877 | static tree print_string_fndecl; | |
7878 | pfndecl = &print_string_fndecl; | |
7879 | fnname = "__go_print_string"; | |
7880 | } | |
7881 | else if (type->integer_type() != NULL | |
7882 | && type->integer_type()->is_unsigned()) | |
7883 | { | |
7884 | static tree print_uint64_fndecl; | |
7885 | pfndecl = &print_uint64_fndecl; | |
7886 | fnname = "__go_print_uint64"; | |
7887 | Type* itype = Type::lookup_integer_type("uint64"); | |
7888 | arg = fold_convert_loc(location, itype->get_tree(gogo), | |
7889 | arg); | |
7890 | } | |
7891 | else if (type->integer_type() != NULL) | |
7892 | { | |
7893 | static tree print_int64_fndecl; | |
7894 | pfndecl = &print_int64_fndecl; | |
7895 | fnname = "__go_print_int64"; | |
7896 | Type* itype = Type::lookup_integer_type("int64"); | |
7897 | arg = fold_convert_loc(location, itype->get_tree(gogo), | |
7898 | arg); | |
7899 | } | |
7900 | else if (type->float_type() != NULL) | |
7901 | { | |
7902 | static tree print_double_fndecl; | |
7903 | pfndecl = &print_double_fndecl; | |
7904 | fnname = "__go_print_double"; | |
7905 | arg = fold_convert_loc(location, double_type_node, arg); | |
7906 | } | |
7907 | else if (type->complex_type() != NULL) | |
7908 | { | |
7909 | static tree print_complex_fndecl; | |
7910 | pfndecl = &print_complex_fndecl; | |
7911 | fnname = "__go_print_complex"; | |
7912 | arg = fold_convert_loc(location, complex_double_type_node, | |
7913 | arg); | |
7914 | } | |
7915 | else if (type->is_boolean_type()) | |
7916 | { | |
7917 | static tree print_bool_fndecl; | |
7918 | pfndecl = &print_bool_fndecl; | |
7919 | fnname = "__go_print_bool"; | |
7920 | } | |
7921 | else if (type->points_to() != NULL | |
7922 | || type->channel_type() != NULL | |
7923 | || type->map_type() != NULL | |
7924 | || type->function_type() != NULL) | |
7925 | { | |
7926 | static tree print_pointer_fndecl; | |
7927 | pfndecl = &print_pointer_fndecl; | |
7928 | fnname = "__go_print_pointer"; | |
7929 | arg = fold_convert_loc(location, ptr_type_node, arg); | |
7930 | } | |
7931 | else if (type->interface_type() != NULL) | |
7932 | { | |
7933 | if (type->interface_type()->is_empty()) | |
7934 | { | |
7935 | static tree print_empty_interface_fndecl; | |
7936 | pfndecl = &print_empty_interface_fndecl; | |
7937 | fnname = "__go_print_empty_interface"; | |
7938 | } | |
7939 | else | |
7940 | { | |
7941 | static tree print_interface_fndecl; | |
7942 | pfndecl = &print_interface_fndecl; | |
7943 | fnname = "__go_print_interface"; | |
7944 | } | |
7945 | } | |
7946 | else if (type->is_open_array_type()) | |
7947 | { | |
7948 | static tree print_slice_fndecl; | |
7949 | pfndecl = &print_slice_fndecl; | |
7950 | fnname = "__go_print_slice"; | |
7951 | } | |
7952 | else | |
7953 | gcc_unreachable(); | |
7954 | ||
7955 | tree call = Gogo::call_builtin(pfndecl, | |
7956 | location, | |
7957 | fnname, | |
7958 | 1, | |
7959 | void_type_node, | |
7960 | TREE_TYPE(arg), | |
7961 | arg); | |
5fb82b5e | 7962 | if (call == error_mark_node) |
7963 | return error_mark_node; | |
7964 | append_to_statement_list(call, &stmt_list); | |
e440a328 | 7965 | } |
7966 | } | |
7967 | ||
7968 | if (is_ln) | |
7969 | { | |
7970 | static tree print_nl_fndecl; | |
7971 | tree call = Gogo::call_builtin(&print_nl_fndecl, | |
7972 | location, | |
7973 | "__go_print_nl", | |
7974 | 0, | |
7975 | void_type_node); | |
5fb82b5e | 7976 | if (call == error_mark_node) |
7977 | return error_mark_node; | |
e440a328 | 7978 | append_to_statement_list(call, &stmt_list); |
7979 | } | |
7980 | ||
7981 | return stmt_list; | |
7982 | } | |
7983 | ||
7984 | case BUILTIN_PANIC: | |
7985 | { | |
7986 | const Expression_list* args = this->args(); | |
7987 | gcc_assert(args != NULL && args->size() == 1); | |
7988 | Expression* arg = args->front(); | |
7989 | tree arg_tree = arg->get_tree(context); | |
7990 | if (arg_tree == error_mark_node) | |
7991 | return error_mark_node; | |
7992 | Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
7993 | arg_tree = Expression::convert_for_assignment(context, empty, | |
7994 | arg->type(), | |
7995 | arg_tree, location); | |
7996 | static tree panic_fndecl; | |
7997 | tree call = Gogo::call_builtin(&panic_fndecl, | |
7998 | location, | |
7999 | "__go_panic", | |
8000 | 1, | |
8001 | void_type_node, | |
8002 | TREE_TYPE(arg_tree), | |
8003 | arg_tree); | |
5fb82b5e | 8004 | if (call == error_mark_node) |
8005 | return error_mark_node; | |
e440a328 | 8006 | // This function will throw an exception. |
8007 | TREE_NOTHROW(panic_fndecl) = 0; | |
8008 | // This function will not return. | |
8009 | TREE_THIS_VOLATILE(panic_fndecl) = 1; | |
8010 | return call; | |
8011 | } | |
8012 | ||
8013 | case BUILTIN_RECOVER: | |
8014 | { | |
8015 | // The argument is set when building recover thunks. It's a | |
8016 | // boolean value which is true if we can recover a value now. | |
8017 | const Expression_list* args = this->args(); | |
8018 | gcc_assert(args != NULL && args->size() == 1); | |
8019 | Expression* arg = args->front(); | |
8020 | tree arg_tree = arg->get_tree(context); | |
8021 | if (arg_tree == error_mark_node) | |
8022 | return error_mark_node; | |
8023 | ||
8024 | Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION); | |
8025 | tree empty_tree = empty->get_tree(context->gogo()); | |
8026 | ||
8027 | Type* nil_type = Type::make_nil_type(); | |
8028 | Expression* nil = Expression::make_nil(location); | |
8029 | tree nil_tree = nil->get_tree(context); | |
8030 | tree empty_nil_tree = Expression::convert_for_assignment(context, | |
8031 | empty, | |
8032 | nil_type, | |
8033 | nil_tree, | |
8034 | location); | |
8035 | ||
8036 | // We need to handle a deferred call to recover specially, | |
8037 | // because it changes whether it can recover a panic or not. | |
8038 | // See test7 in test/recover1.go. | |
8039 | tree call; | |
8040 | if (this->is_deferred()) | |
8041 | { | |
8042 | static tree deferred_recover_fndecl; | |
8043 | call = Gogo::call_builtin(&deferred_recover_fndecl, | |
8044 | location, | |
8045 | "__go_deferred_recover", | |
8046 | 0, | |
8047 | empty_tree); | |
8048 | } | |
8049 | else | |
8050 | { | |
8051 | static tree recover_fndecl; | |
8052 | call = Gogo::call_builtin(&recover_fndecl, | |
8053 | location, | |
8054 | "__go_recover", | |
8055 | 0, | |
8056 | empty_tree); | |
8057 | } | |
5fb82b5e | 8058 | if (call == error_mark_node) |
8059 | return error_mark_node; | |
e440a328 | 8060 | return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree, |
8061 | call, empty_nil_tree); | |
8062 | } | |
8063 | ||
8064 | case BUILTIN_CLOSE: | |
e440a328 | 8065 | { |
8066 | const Expression_list* args = this->args(); | |
8067 | gcc_assert(args != NULL && args->size() == 1); | |
8068 | Expression* arg = args->front(); | |
8069 | tree arg_tree = arg->get_tree(context); | |
8070 | if (arg_tree == error_mark_node) | |
8071 | return error_mark_node; | |
0dc2f918 | 8072 | static tree close_fndecl; |
8073 | return Gogo::call_builtin(&close_fndecl, | |
8074 | location, | |
8075 | "__go_builtin_close", | |
8076 | 1, | |
8077 | void_type_node, | |
8078 | TREE_TYPE(arg_tree), | |
8079 | arg_tree); | |
e440a328 | 8080 | } |
8081 | ||
8082 | case BUILTIN_SIZEOF: | |
8083 | case BUILTIN_OFFSETOF: | |
8084 | case BUILTIN_ALIGNOF: | |
8085 | { | |
8086 | mpz_t val; | |
8087 | mpz_init(val); | |
8088 | Type* dummy; | |
8089 | bool b = this->integer_constant_value(true, val, &dummy); | |
7f1d9abd | 8090 | if (!b) |
8091 | { | |
8092 | gcc_assert(saw_errors()); | |
8093 | return error_mark_node; | |
8094 | } | |
e440a328 | 8095 | tree type = Type::lookup_integer_type("int")->get_tree(gogo); |
8096 | tree ret = Expression::integer_constant_tree(val, type); | |
8097 | mpz_clear(val); | |
8098 | return ret; | |
8099 | } | |
8100 | ||
8101 | case BUILTIN_COPY: | |
8102 | { | |
8103 | const Expression_list* args = this->args(); | |
8104 | gcc_assert(args != NULL && args->size() == 2); | |
8105 | Expression* arg1 = args->front(); | |
8106 | Expression* arg2 = args->back(); | |
8107 | ||
8108 | tree arg1_tree = arg1->get_tree(context); | |
8109 | tree arg2_tree = arg2->get_tree(context); | |
8110 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
8111 | return error_mark_node; | |
8112 | ||
8113 | Type* arg1_type = arg1->type(); | |
8114 | Array_type* at = arg1_type->array_type(); | |
8115 | arg1_tree = save_expr(arg1_tree); | |
8116 | tree arg1_val = at->value_pointer_tree(gogo, arg1_tree); | |
8117 | tree arg1_len = at->length_tree(gogo, arg1_tree); | |
d8ccb1e3 | 8118 | if (arg1_val == error_mark_node || arg1_len == error_mark_node) |
8119 | return error_mark_node; | |
e440a328 | 8120 | |
8121 | Type* arg2_type = arg2->type(); | |
8122 | tree arg2_val; | |
8123 | tree arg2_len; | |
8124 | if (arg2_type->is_open_array_type()) | |
8125 | { | |
8126 | at = arg2_type->array_type(); | |
8127 | arg2_tree = save_expr(arg2_tree); | |
8128 | arg2_val = at->value_pointer_tree(gogo, arg2_tree); | |
8129 | arg2_len = at->length_tree(gogo, arg2_tree); | |
8130 | } | |
8131 | else | |
8132 | { | |
8133 | arg2_tree = save_expr(arg2_tree); | |
8134 | arg2_val = String_type::bytes_tree(gogo, arg2_tree); | |
8135 | arg2_len = String_type::length_tree(gogo, arg2_tree); | |
8136 | } | |
d8ccb1e3 | 8137 | if (arg2_val == error_mark_node || arg2_len == error_mark_node) |
8138 | return error_mark_node; | |
e440a328 | 8139 | |
8140 | arg1_len = save_expr(arg1_len); | |
8141 | arg2_len = save_expr(arg2_len); | |
8142 | tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len), | |
8143 | fold_build2_loc(location, LT_EXPR, | |
8144 | boolean_type_node, | |
8145 | arg1_len, arg2_len), | |
8146 | arg1_len, arg2_len); | |
8147 | len = save_expr(len); | |
8148 | ||
8149 | Type* element_type = at->element_type(); | |
8150 | tree element_type_tree = element_type->get_tree(gogo); | |
d8ccb1e3 | 8151 | if (element_type_tree == error_mark_node) |
8152 | return error_mark_node; | |
e440a328 | 8153 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
8154 | tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size), | |
8155 | len); | |
8156 | bytecount = fold_build2_loc(location, MULT_EXPR, | |
8157 | TREE_TYPE(element_size), | |
8158 | bytecount, element_size); | |
8159 | bytecount = fold_convert_loc(location, size_type_node, bytecount); | |
8160 | ||
3991cb03 | 8161 | arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val); |
8162 | arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val); | |
8163 | ||
8164 | static tree copy_fndecl; | |
8165 | tree call = Gogo::call_builtin(©_fndecl, | |
8166 | location, | |
8167 | "__go_copy", | |
8168 | 3, | |
8169 | void_type_node, | |
8170 | ptr_type_node, | |
8171 | arg1_val, | |
8172 | ptr_type_node, | |
8173 | arg2_val, | |
8174 | size_type_node, | |
8175 | bytecount); | |
8176 | if (call == error_mark_node) | |
8177 | return error_mark_node; | |
e440a328 | 8178 | |
8179 | return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len), | |
8180 | call, len); | |
8181 | } | |
8182 | ||
8183 | case BUILTIN_APPEND: | |
8184 | { | |
8185 | const Expression_list* args = this->args(); | |
8186 | gcc_assert(args != NULL && args->size() == 2); | |
8187 | Expression* arg1 = args->front(); | |
8188 | Expression* arg2 = args->back(); | |
8189 | ||
8190 | tree arg1_tree = arg1->get_tree(context); | |
8191 | tree arg2_tree = arg2->get_tree(context); | |
8192 | if (arg1_tree == error_mark_node || arg2_tree == error_mark_node) | |
8193 | return error_mark_node; | |
8194 | ||
9d44fbe3 | 8195 | Array_type* at = arg1->type()->array_type(); |
8196 | Type* element_type = at->element_type(); | |
8197 | ||
ed64c8e5 | 8198 | arg2_tree = Expression::convert_for_assignment(context, at, |
8199 | arg2->type(), | |
8200 | arg2_tree, | |
8201 | location); | |
8202 | if (arg2_tree == error_mark_node) | |
8203 | return error_mark_node; | |
8204 | ||
3991cb03 | 8205 | arg2_tree = save_expr(arg2_tree); |
ed64c8e5 | 8206 | tree arg2_val = at->value_pointer_tree(gogo, arg2_tree); |
8207 | tree arg2_len = at->length_tree(gogo, arg2_tree); | |
3991cb03 | 8208 | if (arg2_val == error_mark_node || arg2_len == error_mark_node) |
8209 | return error_mark_node; | |
8210 | arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val); | |
8211 | arg2_len = fold_convert_loc(location, size_type_node, arg2_len); | |
8212 | ||
8213 | tree element_type_tree = element_type->get_tree(gogo); | |
8214 | if (element_type_tree == error_mark_node) | |
8215 | return error_mark_node; | |
8216 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); | |
8217 | element_size = fold_convert_loc(location, size_type_node, | |
8218 | element_size); | |
e440a328 | 8219 | |
8220 | // We rebuild the decl each time since the slice types may | |
8221 | // change. | |
8222 | tree append_fndecl = NULL_TREE; | |
8223 | return Gogo::call_builtin(&append_fndecl, | |
8224 | location, | |
8225 | "__go_append", | |
3991cb03 | 8226 | 4, |
e440a328 | 8227 | TREE_TYPE(arg1_tree), |
e440a328 | 8228 | TREE_TYPE(arg1_tree), |
8229 | arg1_tree, | |
3991cb03 | 8230 | ptr_type_node, |
8231 | arg2_val, | |
8232 | size_type_node, | |
8233 | arg2_len, | |
8234 | size_type_node, | |
8235 | element_size); | |
e440a328 | 8236 | } |
8237 | ||
8238 | case BUILTIN_REAL: | |
8239 | case BUILTIN_IMAG: | |
8240 | { | |
8241 | const Expression_list* args = this->args(); | |
8242 | gcc_assert(args != NULL && args->size() == 1); | |
8243 | Expression* arg = args->front(); | |
8244 | tree arg_tree = arg->get_tree(context); | |
8245 | if (arg_tree == error_mark_node) | |
8246 | return error_mark_node; | |
8247 | gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree))); | |
8248 | if (this->code_ == BUILTIN_REAL) | |
8249 | return fold_build1_loc(location, REALPART_EXPR, | |
8250 | TREE_TYPE(TREE_TYPE(arg_tree)), | |
8251 | arg_tree); | |
8252 | else | |
8253 | return fold_build1_loc(location, IMAGPART_EXPR, | |
8254 | TREE_TYPE(TREE_TYPE(arg_tree)), | |
8255 | arg_tree); | |
8256 | } | |
8257 | ||
48080209 | 8258 | case BUILTIN_COMPLEX: |
e440a328 | 8259 | { |
8260 | const Expression_list* args = this->args(); | |
8261 | gcc_assert(args != NULL && args->size() == 2); | |
8262 | tree r = args->front()->get_tree(context); | |
8263 | tree i = args->back()->get_tree(context); | |
8264 | if (r == error_mark_node || i == error_mark_node) | |
8265 | return error_mark_node; | |
8266 | gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r)) | |
8267 | == TYPE_MAIN_VARIANT(TREE_TYPE(i))); | |
8268 | gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r))); | |
8269 | return fold_build2_loc(location, COMPLEX_EXPR, | |
8270 | build_complex_type(TREE_TYPE(r)), | |
8271 | r, i); | |
8272 | } | |
8273 | ||
8274 | default: | |
8275 | gcc_unreachable(); | |
8276 | } | |
8277 | } | |
8278 | ||
8279 | // We have to support exporting a builtin call expression, because | |
8280 | // code can set a constant to the result of a builtin expression. | |
8281 | ||
8282 | void | |
8283 | Builtin_call_expression::do_export(Export* exp) const | |
8284 | { | |
8285 | bool ok = false; | |
8286 | ||
8287 | mpz_t val; | |
8288 | mpz_init(val); | |
8289 | Type* dummy; | |
8290 | if (this->integer_constant_value(true, val, &dummy)) | |
8291 | { | |
8292 | Integer_expression::export_integer(exp, val); | |
8293 | ok = true; | |
8294 | } | |
8295 | mpz_clear(val); | |
8296 | ||
8297 | if (!ok) | |
8298 | { | |
8299 | mpfr_t fval; | |
8300 | mpfr_init(fval); | |
8301 | if (this->float_constant_value(fval, &dummy)) | |
8302 | { | |
8303 | Float_expression::export_float(exp, fval); | |
8304 | ok = true; | |
8305 | } | |
8306 | mpfr_clear(fval); | |
8307 | } | |
8308 | ||
8309 | if (!ok) | |
8310 | { | |
8311 | mpfr_t real; | |
8312 | mpfr_t imag; | |
8313 | mpfr_init(real); | |
8314 | mpfr_init(imag); | |
8315 | if (this->complex_constant_value(real, imag, &dummy)) | |
8316 | { | |
8317 | Complex_expression::export_complex(exp, real, imag); | |
8318 | ok = true; | |
8319 | } | |
8320 | mpfr_clear(real); | |
8321 | mpfr_clear(imag); | |
8322 | } | |
8323 | ||
8324 | if (!ok) | |
8325 | { | |
8326 | error_at(this->location(), "value is not constant"); | |
8327 | return; | |
8328 | } | |
8329 | ||
8330 | // A trailing space lets us reliably identify the end of the number. | |
8331 | exp->write_c_string(" "); | |
8332 | } | |
8333 | ||
8334 | // Class Call_expression. | |
8335 | ||
8336 | // Traversal. | |
8337 | ||
8338 | int | |
8339 | Call_expression::do_traverse(Traverse* traverse) | |
8340 | { | |
8341 | if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT) | |
8342 | return TRAVERSE_EXIT; | |
8343 | if (this->args_ != NULL) | |
8344 | { | |
8345 | if (this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
8346 | return TRAVERSE_EXIT; | |
8347 | } | |
8348 | return TRAVERSE_CONTINUE; | |
8349 | } | |
8350 | ||
8351 | // Lower a call statement. | |
8352 | ||
8353 | Expression* | |
8354 | Call_expression::do_lower(Gogo* gogo, Named_object* function, int) | |
8355 | { | |
8356 | // A type case can look like a function call. | |
8357 | if (this->fn_->is_type_expression() | |
8358 | && this->args_ != NULL | |
8359 | && this->args_->size() == 1) | |
8360 | return Expression::make_cast(this->fn_->type(), this->args_->front(), | |
8361 | this->location()); | |
8362 | ||
8363 | // Recognize a call to a builtin function. | |
8364 | Func_expression* fne = this->fn_->func_expression(); | |
8365 | if (fne != NULL | |
8366 | && fne->named_object()->is_function_declaration() | |
8367 | && fne->named_object()->func_declaration_value()->type()->is_builtin()) | |
8368 | return new Builtin_call_expression(gogo, this->fn_, this->args_, | |
8369 | this->is_varargs_, this->location()); | |
8370 | ||
8371 | // Handle an argument which is a call to a function which returns | |
8372 | // multiple results. | |
8373 | if (this->args_ != NULL | |
8374 | && this->args_->size() == 1 | |
8375 | && this->args_->front()->call_expression() != NULL | |
8376 | && this->fn_->type()->function_type() != NULL) | |
8377 | { | |
8378 | Function_type* fntype = this->fn_->type()->function_type(); | |
8379 | size_t rc = this->args_->front()->call_expression()->result_count(); | |
8380 | if (rc > 1 | |
8381 | && fntype->parameters() != NULL | |
8382 | && (fntype->parameters()->size() == rc | |
8383 | || (fntype->is_varargs() | |
8384 | && fntype->parameters()->size() - 1 <= rc))) | |
8385 | { | |
8386 | Call_expression* call = this->args_->front()->call_expression(); | |
8387 | Expression_list* args = new Expression_list; | |
8388 | for (size_t i = 0; i < rc; ++i) | |
8389 | args->push_back(Expression::make_call_result(call, i)); | |
8390 | // We can't return a new call expression here, because this | |
42535814 | 8391 | // one may be referenced by Call_result expressions. We |
8392 | // also can't delete the old arguments, because we may still | |
8393 | // traverse them somewhere up the call stack. FIXME. | |
e440a328 | 8394 | this->args_ = args; |
8395 | } | |
8396 | } | |
8397 | ||
8398 | // Handle a call to a varargs function by packaging up the extra | |
8399 | // parameters. | |
8400 | if (this->fn_->type()->function_type() != NULL | |
8401 | && this->fn_->type()->function_type()->is_varargs()) | |
8402 | { | |
8403 | Function_type* fntype = this->fn_->type()->function_type(); | |
8404 | const Typed_identifier_list* parameters = fntype->parameters(); | |
8405 | gcc_assert(parameters != NULL && !parameters->empty()); | |
8406 | Type* varargs_type = parameters->back().type(); | |
8407 | return this->lower_varargs(gogo, function, varargs_type, | |
8408 | parameters->size()); | |
8409 | } | |
8410 | ||
8411 | return this; | |
8412 | } | |
8413 | ||
8414 | // Lower a call to a varargs function. FUNCTION is the function in | |
8415 | // which the call occurs--it's not the function we are calling. | |
8416 | // VARARGS_TYPE is the type of the varargs parameter, a slice type. | |
8417 | // PARAM_COUNT is the number of parameters of the function we are | |
8418 | // calling; the last of these parameters will be the varargs | |
8419 | // parameter. | |
8420 | ||
8421 | Expression* | |
8422 | Call_expression::lower_varargs(Gogo* gogo, Named_object* function, | |
8423 | Type* varargs_type, size_t param_count) | |
8424 | { | |
8425 | if (this->varargs_are_lowered_) | |
8426 | return this; | |
8427 | ||
8428 | source_location loc = this->location(); | |
8429 | ||
8430 | gcc_assert(param_count > 0); | |
8431 | gcc_assert(varargs_type->is_open_array_type()); | |
8432 | ||
8433 | size_t arg_count = this->args_ == NULL ? 0 : this->args_->size(); | |
8434 | if (arg_count < param_count - 1) | |
8435 | { | |
8436 | // Not enough arguments; will be caught in check_types. | |
8437 | return this; | |
8438 | } | |
8439 | ||
8440 | Expression_list* old_args = this->args_; | |
8441 | Expression_list* new_args = new Expression_list(); | |
8442 | bool push_empty_arg = false; | |
8443 | if (old_args == NULL || old_args->empty()) | |
8444 | { | |
8445 | gcc_assert(param_count == 1); | |
8446 | push_empty_arg = true; | |
8447 | } | |
8448 | else | |
8449 | { | |
8450 | Expression_list::const_iterator pa; | |
8451 | int i = 1; | |
8452 | for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i) | |
8453 | { | |
8454 | if (static_cast<size_t>(i) == param_count) | |
8455 | break; | |
8456 | new_args->push_back(*pa); | |
8457 | } | |
8458 | ||
8459 | // We have reached the varargs parameter. | |
8460 | ||
8461 | bool issued_error = false; | |
8462 | if (pa == old_args->end()) | |
8463 | push_empty_arg = true; | |
8464 | else if (pa + 1 == old_args->end() && this->is_varargs_) | |
8465 | new_args->push_back(*pa); | |
8466 | else if (this->is_varargs_) | |
8467 | { | |
8468 | this->report_error(_("too many arguments")); | |
8469 | return this; | |
8470 | } | |
e440a328 | 8471 | else |
8472 | { | |
8473 | Type* element_type = varargs_type->array_type()->element_type(); | |
8474 | Expression_list* vals = new Expression_list; | |
8475 | for (; pa != old_args->end(); ++pa, ++i) | |
8476 | { | |
8477 | // Check types here so that we get a better message. | |
8478 | Type* patype = (*pa)->type(); | |
8479 | source_location paloc = (*pa)->location(); | |
8480 | if (!this->check_argument_type(i, element_type, patype, | |
8481 | paloc, issued_error)) | |
8482 | continue; | |
8483 | vals->push_back(*pa); | |
8484 | } | |
8485 | Expression* val = | |
8486 | Expression::make_slice_composite_literal(varargs_type, vals, loc); | |
8487 | new_args->push_back(val); | |
8488 | } | |
8489 | } | |
8490 | ||
8491 | if (push_empty_arg) | |
8492 | new_args->push_back(Expression::make_nil(loc)); | |
8493 | ||
8494 | // We can't return a new call expression here, because this one may | |
8495 | // be referenced by Call_result expressions. FIXME. | |
8496 | if (old_args != NULL) | |
8497 | delete old_args; | |
8498 | this->args_ = new_args; | |
8499 | this->varargs_are_lowered_ = true; | |
8500 | ||
8501 | // Lower all the new subexpressions. | |
8502 | Expression* ret = this; | |
8503 | gogo->lower_expression(function, &ret); | |
8504 | gcc_assert(ret == this); | |
8505 | return ret; | |
8506 | } | |
8507 | ||
e440a328 | 8508 | // Get the function type. Returns NULL if we don't know the type. If |
8509 | // this returns NULL, and if_ERROR is true, issues an error. | |
8510 | ||
8511 | Function_type* | |
8512 | Call_expression::get_function_type() const | |
8513 | { | |
8514 | return this->fn_->type()->function_type(); | |
8515 | } | |
8516 | ||
8517 | // Return the number of values which this call will return. | |
8518 | ||
8519 | size_t | |
8520 | Call_expression::result_count() const | |
8521 | { | |
8522 | const Function_type* fntype = this->get_function_type(); | |
8523 | if (fntype == NULL) | |
8524 | return 0; | |
8525 | if (fntype->results() == NULL) | |
8526 | return 0; | |
8527 | return fntype->results()->size(); | |
8528 | } | |
8529 | ||
8530 | // Return whether this is a call to the predeclared function recover. | |
8531 | ||
8532 | bool | |
8533 | Call_expression::is_recover_call() const | |
8534 | { | |
8535 | return this->do_is_recover_call(); | |
8536 | } | |
8537 | ||
8538 | // Set the argument to the recover function. | |
8539 | ||
8540 | void | |
8541 | Call_expression::set_recover_arg(Expression* arg) | |
8542 | { | |
8543 | this->do_set_recover_arg(arg); | |
8544 | } | |
8545 | ||
8546 | // Virtual functions also implemented by Builtin_call_expression. | |
8547 | ||
8548 | bool | |
8549 | Call_expression::do_is_recover_call() const | |
8550 | { | |
8551 | return false; | |
8552 | } | |
8553 | ||
8554 | void | |
8555 | Call_expression::do_set_recover_arg(Expression*) | |
8556 | { | |
8557 | gcc_unreachable(); | |
8558 | } | |
8559 | ||
8560 | // Get the type. | |
8561 | ||
8562 | Type* | |
8563 | Call_expression::do_type() | |
8564 | { | |
8565 | if (this->type_ != NULL) | |
8566 | return this->type_; | |
8567 | ||
8568 | Type* ret; | |
8569 | Function_type* fntype = this->get_function_type(); | |
8570 | if (fntype == NULL) | |
8571 | return Type::make_error_type(); | |
8572 | ||
8573 | const Typed_identifier_list* results = fntype->results(); | |
8574 | if (results == NULL) | |
8575 | ret = Type::make_void_type(); | |
8576 | else if (results->size() == 1) | |
8577 | ret = results->begin()->type(); | |
8578 | else | |
8579 | ret = Type::make_call_multiple_result_type(this); | |
8580 | ||
8581 | this->type_ = ret; | |
8582 | ||
8583 | return this->type_; | |
8584 | } | |
8585 | ||
8586 | // Determine types for a call expression. We can use the function | |
8587 | // parameter types to set the types of the arguments. | |
8588 | ||
8589 | void | |
8590 | Call_expression::do_determine_type(const Type_context*) | |
8591 | { | |
fb94b0ca | 8592 | if (!this->determining_types()) |
8593 | return; | |
8594 | ||
e440a328 | 8595 | this->fn_->determine_type_no_context(); |
8596 | Function_type* fntype = this->get_function_type(); | |
8597 | const Typed_identifier_list* parameters = NULL; | |
8598 | if (fntype != NULL) | |
8599 | parameters = fntype->parameters(); | |
8600 | if (this->args_ != NULL) | |
8601 | { | |
8602 | Typed_identifier_list::const_iterator pt; | |
8603 | if (parameters != NULL) | |
8604 | pt = parameters->begin(); | |
8605 | for (Expression_list::const_iterator pa = this->args_->begin(); | |
8606 | pa != this->args_->end(); | |
8607 | ++pa) | |
8608 | { | |
8609 | if (parameters != NULL && pt != parameters->end()) | |
8610 | { | |
8611 | Type_context subcontext(pt->type(), false); | |
8612 | (*pa)->determine_type(&subcontext); | |
8613 | ++pt; | |
8614 | } | |
8615 | else | |
8616 | (*pa)->determine_type_no_context(); | |
8617 | } | |
8618 | } | |
8619 | } | |
8620 | ||
fb94b0ca | 8621 | // Called when determining types for a Call_expression. Return true |
8622 | // if we should go ahead, false if they have already been determined. | |
8623 | ||
8624 | bool | |
8625 | Call_expression::determining_types() | |
8626 | { | |
8627 | if (this->types_are_determined_) | |
8628 | return false; | |
8629 | else | |
8630 | { | |
8631 | this->types_are_determined_ = true; | |
8632 | return true; | |
8633 | } | |
8634 | } | |
8635 | ||
e440a328 | 8636 | // Check types for parameter I. |
8637 | ||
8638 | bool | |
8639 | Call_expression::check_argument_type(int i, const Type* parameter_type, | |
8640 | const Type* argument_type, | |
8641 | source_location argument_location, | |
8642 | bool issued_error) | |
8643 | { | |
8644 | std::string reason; | |
8645 | if (!Type::are_assignable(parameter_type, argument_type, &reason)) | |
8646 | { | |
8647 | if (!issued_error) | |
8648 | { | |
8649 | if (reason.empty()) | |
8650 | error_at(argument_location, "argument %d has incompatible type", i); | |
8651 | else | |
8652 | error_at(argument_location, | |
8653 | "argument %d has incompatible type (%s)", | |
8654 | i, reason.c_str()); | |
8655 | } | |
8656 | this->set_is_error(); | |
8657 | return false; | |
8658 | } | |
8659 | return true; | |
8660 | } | |
8661 | ||
8662 | // Check types. | |
8663 | ||
8664 | void | |
8665 | Call_expression::do_check_types(Gogo*) | |
8666 | { | |
8667 | Function_type* fntype = this->get_function_type(); | |
8668 | if (fntype == NULL) | |
8669 | { | |
5c13bd80 | 8670 | if (!this->fn_->type()->is_error()) |
e440a328 | 8671 | this->report_error(_("expected function")); |
8672 | return; | |
8673 | } | |
8674 | ||
8675 | if (fntype->is_method()) | |
8676 | { | |
8677 | // We don't support pointers to methods, so the function has to | |
8678 | // be a bound method expression. | |
8679 | Bound_method_expression* bme = this->fn_->bound_method_expression(); | |
8680 | if (bme == NULL) | |
8681 | { | |
8682 | this->report_error(_("method call without object")); | |
8683 | return; | |
8684 | } | |
8685 | Type* first_arg_type = bme->first_argument()->type(); | |
8686 | if (first_arg_type->points_to() == NULL) | |
8687 | { | |
8688 | // When passing a value, we need to check that we are | |
d2b330e0 | 8689 | // permitted to copy it. The language permits copying |
8690 | // hidden fields for a method receiver. | |
e440a328 | 8691 | std::string reason; |
d2b330e0 | 8692 | if (!Type::are_assignable_hidden_ok(fntype->receiver()->type(), |
8693 | first_arg_type, &reason)) | |
e440a328 | 8694 | { |
8695 | if (reason.empty()) | |
8696 | this->report_error(_("incompatible type for receiver")); | |
8697 | else | |
8698 | { | |
8699 | error_at(this->location(), | |
8700 | "incompatible type for receiver (%s)", | |
8701 | reason.c_str()); | |
8702 | this->set_is_error(); | |
8703 | } | |
8704 | } | |
8705 | } | |
8706 | } | |
8707 | ||
8708 | // Note that varargs was handled by the lower_varargs() method, so | |
8709 | // we don't have to worry about it here. | |
8710 | ||
8711 | const Typed_identifier_list* parameters = fntype->parameters(); | |
8712 | if (this->args_ == NULL) | |
8713 | { | |
8714 | if (parameters != NULL && !parameters->empty()) | |
8715 | this->report_error(_("not enough arguments")); | |
8716 | } | |
8717 | else if (parameters == NULL) | |
8718 | this->report_error(_("too many arguments")); | |
8719 | else | |
8720 | { | |
8721 | int i = 0; | |
8722 | Typed_identifier_list::const_iterator pt = parameters->begin(); | |
8723 | for (Expression_list::const_iterator pa = this->args_->begin(); | |
8724 | pa != this->args_->end(); | |
8725 | ++pa, ++pt, ++i) | |
8726 | { | |
8727 | if (pt == parameters->end()) | |
8728 | { | |
8729 | this->report_error(_("too many arguments")); | |
8730 | return; | |
8731 | } | |
8732 | this->check_argument_type(i + 1, pt->type(), (*pa)->type(), | |
8733 | (*pa)->location(), false); | |
8734 | } | |
8735 | if (pt != parameters->end()) | |
8736 | this->report_error(_("not enough arguments")); | |
8737 | } | |
8738 | } | |
8739 | ||
8740 | // Return whether we have to use a temporary variable to ensure that | |
8741 | // we evaluate this call expression in order. If the call returns no | |
8742 | // results then it will inevitably be executed last. If the call | |
8743 | // returns more than one result then it will be used with Call_result | |
8744 | // expressions. So we only have to use a temporary variable if the | |
8745 | // call returns exactly one result. | |
8746 | ||
8747 | bool | |
8748 | Call_expression::do_must_eval_in_order() const | |
8749 | { | |
8750 | return this->result_count() == 1; | |
8751 | } | |
8752 | ||
8753 | // Get the function and the first argument to use when calling a bound | |
8754 | // method. | |
8755 | ||
8756 | tree | |
8757 | Call_expression::bound_method_function(Translate_context* context, | |
8758 | Bound_method_expression* bound_method, | |
8759 | tree* first_arg_ptr) | |
8760 | { | |
8761 | Expression* first_argument = bound_method->first_argument(); | |
8762 | tree first_arg = first_argument->get_tree(context); | |
8763 | if (first_arg == error_mark_node) | |
8764 | return error_mark_node; | |
8765 | ||
8766 | // We always pass a pointer to the first argument when calling a | |
8767 | // method. | |
8768 | if (first_argument->type()->points_to() == NULL) | |
8769 | { | |
8770 | tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg)); | |
8771 | if (TREE_ADDRESSABLE(TREE_TYPE(first_arg)) | |
8772 | || DECL_P(first_arg) | |
8773 | || TREE_CODE(first_arg) == INDIRECT_REF | |
8774 | || TREE_CODE(first_arg) == COMPONENT_REF) | |
8775 | { | |
8776 | first_arg = build_fold_addr_expr(first_arg); | |
8777 | if (DECL_P(first_arg)) | |
8778 | TREE_ADDRESSABLE(first_arg) = 1; | |
8779 | } | |
8780 | else | |
8781 | { | |
8782 | tree tmp = create_tmp_var(TREE_TYPE(first_arg), | |
8783 | get_name(first_arg)); | |
8784 | DECL_IGNORED_P(tmp) = 0; | |
8785 | DECL_INITIAL(tmp) = first_arg; | |
8786 | first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type, | |
8787 | build1(DECL_EXPR, void_type_node, tmp), | |
8788 | build_fold_addr_expr(tmp)); | |
8789 | TREE_ADDRESSABLE(tmp) = 1; | |
8790 | } | |
8791 | if (first_arg == error_mark_node) | |
8792 | return error_mark_node; | |
8793 | } | |
8794 | ||
8795 | Type* fatype = bound_method->first_argument_type(); | |
8796 | if (fatype != NULL) | |
8797 | { | |
8798 | if (fatype->points_to() == NULL) | |
8799 | fatype = Type::make_pointer_type(fatype); | |
8800 | first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg); | |
8801 | if (first_arg == error_mark_node | |
8802 | || TREE_TYPE(first_arg) == error_mark_node) | |
8803 | return error_mark_node; | |
8804 | } | |
8805 | ||
8806 | *first_arg_ptr = first_arg; | |
8807 | ||
8808 | return bound_method->method()->get_tree(context); | |
8809 | } | |
8810 | ||
8811 | // Get the function and the first argument to use when calling an | |
8812 | // interface method. | |
8813 | ||
8814 | tree | |
8815 | Call_expression::interface_method_function( | |
8816 | Translate_context* context, | |
8817 | Interface_field_reference_expression* interface_method, | |
8818 | tree* first_arg_ptr) | |
8819 | { | |
8820 | tree expr = interface_method->expr()->get_tree(context); | |
8821 | if (expr == error_mark_node) | |
8822 | return error_mark_node; | |
8823 | expr = save_expr(expr); | |
8824 | tree first_arg = interface_method->get_underlying_object_tree(context, expr); | |
8825 | if (first_arg == error_mark_node) | |
8826 | return error_mark_node; | |
8827 | *first_arg_ptr = first_arg; | |
8828 | return interface_method->get_function_tree(context, expr); | |
8829 | } | |
8830 | ||
8831 | // Build the call expression. | |
8832 | ||
8833 | tree | |
8834 | Call_expression::do_get_tree(Translate_context* context) | |
8835 | { | |
8836 | if (this->tree_ != NULL_TREE) | |
8837 | return this->tree_; | |
8838 | ||
8839 | Function_type* fntype = this->get_function_type(); | |
8840 | if (fntype == NULL) | |
8841 | return error_mark_node; | |
8842 | ||
8843 | if (this->fn_->is_error_expression()) | |
8844 | return error_mark_node; | |
8845 | ||
8846 | Gogo* gogo = context->gogo(); | |
8847 | source_location location = this->location(); | |
8848 | ||
8849 | Func_expression* func = this->fn_->func_expression(); | |
8850 | Bound_method_expression* bound_method = this->fn_->bound_method_expression(); | |
8851 | Interface_field_reference_expression* interface_method = | |
8852 | this->fn_->interface_field_reference_expression(); | |
8853 | const bool has_closure = func != NULL && func->closure() != NULL; | |
8854 | const bool is_method = bound_method != NULL || interface_method != NULL; | |
8855 | gcc_assert(!fntype->is_method() || is_method); | |
8856 | ||
8857 | int nargs; | |
8858 | tree* args; | |
8859 | if (this->args_ == NULL || this->args_->empty()) | |
8860 | { | |
8861 | nargs = is_method ? 1 : 0; | |
8862 | args = nargs == 0 ? NULL : new tree[nargs]; | |
8863 | } | |
8864 | else | |
8865 | { | |
8866 | const Typed_identifier_list* params = fntype->parameters(); | |
8867 | gcc_assert(params != NULL); | |
8868 | ||
8869 | nargs = this->args_->size(); | |
8870 | int i = is_method ? 1 : 0; | |
8871 | nargs += i; | |
8872 | args = new tree[nargs]; | |
8873 | ||
8874 | Typed_identifier_list::const_iterator pp = params->begin(); | |
8875 | Expression_list::const_iterator pe; | |
8876 | for (pe = this->args_->begin(); | |
8877 | pe != this->args_->end(); | |
8878 | ++pe, ++pp, ++i) | |
8879 | { | |
a805a149 | 8880 | gcc_assert(pp != params->end()); |
e440a328 | 8881 | tree arg_val = (*pe)->get_tree(context); |
8882 | args[i] = Expression::convert_for_assignment(context, | |
8883 | pp->type(), | |
8884 | (*pe)->type(), | |
8885 | arg_val, | |
8886 | location); | |
8887 | if (args[i] == error_mark_node) | |
cf609de4 | 8888 | { |
8889 | delete[] args; | |
8890 | return error_mark_node; | |
8891 | } | |
e440a328 | 8892 | } |
8893 | gcc_assert(pp == params->end()); | |
8894 | gcc_assert(i == nargs); | |
8895 | } | |
8896 | ||
8897 | tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo))); | |
8898 | if (rettype == error_mark_node) | |
cf609de4 | 8899 | { |
8900 | delete[] args; | |
8901 | return error_mark_node; | |
8902 | } | |
e440a328 | 8903 | |
8904 | tree fn; | |
8905 | if (has_closure) | |
8906 | fn = func->get_tree_without_closure(gogo); | |
8907 | else if (!is_method) | |
8908 | fn = this->fn_->get_tree(context); | |
8909 | else if (bound_method != NULL) | |
8910 | fn = this->bound_method_function(context, bound_method, &args[0]); | |
8911 | else if (interface_method != NULL) | |
8912 | fn = this->interface_method_function(context, interface_method, &args[0]); | |
8913 | else | |
8914 | gcc_unreachable(); | |
8915 | ||
8916 | if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node) | |
cf609de4 | 8917 | { |
8918 | delete[] args; | |
8919 | return error_mark_node; | |
8920 | } | |
e440a328 | 8921 | |
e440a328 | 8922 | tree fndecl = fn; |
8923 | if (TREE_CODE(fndecl) == ADDR_EXPR) | |
8924 | fndecl = TREE_OPERAND(fndecl, 0); | |
9aa9e2df | 8925 | |
8926 | // Add a type cast in case the type of the function is a recursive | |
8927 | // type which refers to itself. | |
8928 | if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl)) | |
8929 | { | |
8930 | tree fnt = fntype->get_tree(gogo); | |
8931 | if (fnt == error_mark_node) | |
8932 | return error_mark_node; | |
8933 | fn = fold_convert_loc(location, fnt, fn); | |
8934 | } | |
8935 | ||
8936 | // This is to support builtin math functions when using 80387 math. | |
e440a328 | 8937 | tree excess_type = NULL_TREE; |
8938 | if (DECL_P(fndecl) | |
8939 | && DECL_IS_BUILTIN(fndecl) | |
8940 | && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL | |
8941 | && nargs > 0 | |
8942 | && ((SCALAR_FLOAT_TYPE_P(rettype) | |
8943 | && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0]))) | |
8944 | || (COMPLEX_FLOAT_TYPE_P(rettype) | |
8945 | && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0]))))) | |
8946 | { | |
8947 | excess_type = excess_precision_type(TREE_TYPE(args[0])); | |
8948 | if (excess_type != NULL_TREE) | |
8949 | { | |
8950 | tree excess_fndecl = mathfn_built_in(excess_type, | |
8951 | DECL_FUNCTION_CODE(fndecl)); | |
8952 | if (excess_fndecl == NULL_TREE) | |
8953 | excess_type = NULL_TREE; | |
8954 | else | |
8955 | { | |
8956 | fn = build_fold_addr_expr_loc(location, excess_fndecl); | |
8957 | for (int i = 0; i < nargs; ++i) | |
8958 | args[i] = ::convert(excess_type, args[i]); | |
8959 | } | |
8960 | } | |
8961 | } | |
8962 | ||
8963 | tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype, | |
8964 | fn, nargs, args); | |
8965 | delete[] args; | |
8966 | ||
8967 | SET_EXPR_LOCATION(ret, location); | |
8968 | ||
8969 | if (has_closure) | |
8970 | { | |
8971 | tree closure_tree = func->closure()->get_tree(context); | |
8972 | if (closure_tree != error_mark_node) | |
8973 | CALL_EXPR_STATIC_CHAIN(ret) = closure_tree; | |
8974 | } | |
8975 | ||
8976 | // If this is a recursive function type which returns itself, as in | |
8977 | // type F func() F | |
8978 | // we have used ptr_type_node for the return type. Add a cast here | |
8979 | // to the correct type. | |
8980 | if (TREE_TYPE(ret) == ptr_type_node) | |
8981 | { | |
9aa9e2df | 8982 | tree t = this->type()->base()->get_tree(gogo); |
e440a328 | 8983 | ret = fold_convert_loc(location, t, ret); |
8984 | } | |
8985 | ||
8986 | if (excess_type != NULL_TREE) | |
8987 | { | |
8988 | // Calling convert here can undo our excess precision change. | |
8989 | // That may or may not be a bug in convert_to_real. | |
8990 | ret = build1(NOP_EXPR, rettype, ret); | |
8991 | } | |
8992 | ||
8993 | // If there is more than one result, we will refer to the call | |
8994 | // multiple times. | |
8995 | if (fntype->results() != NULL && fntype->results()->size() > 1) | |
8996 | ret = save_expr(ret); | |
8997 | ||
8998 | this->tree_ = ret; | |
8999 | ||
9000 | return ret; | |
9001 | } | |
9002 | ||
9003 | // Make a call expression. | |
9004 | ||
9005 | Call_expression* | |
9006 | Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs, | |
9007 | source_location location) | |
9008 | { | |
9009 | return new Call_expression(fn, args, is_varargs, location); | |
9010 | } | |
9011 | ||
9012 | // A single result from a call which returns multiple results. | |
9013 | ||
9014 | class Call_result_expression : public Expression | |
9015 | { | |
9016 | public: | |
9017 | Call_result_expression(Call_expression* call, unsigned int index) | |
9018 | : Expression(EXPRESSION_CALL_RESULT, call->location()), | |
9019 | call_(call), index_(index) | |
9020 | { } | |
9021 | ||
9022 | protected: | |
9023 | int | |
9024 | do_traverse(Traverse*); | |
9025 | ||
9026 | Type* | |
9027 | do_type(); | |
9028 | ||
9029 | void | |
9030 | do_determine_type(const Type_context*); | |
9031 | ||
9032 | void | |
9033 | do_check_types(Gogo*); | |
9034 | ||
9035 | Expression* | |
9036 | do_copy() | |
9037 | { | |
9038 | return new Call_result_expression(this->call_->call_expression(), | |
9039 | this->index_); | |
9040 | } | |
9041 | ||
9042 | bool | |
9043 | do_must_eval_in_order() const | |
9044 | { return true; } | |
9045 | ||
9046 | tree | |
9047 | do_get_tree(Translate_context*); | |
9048 | ||
9049 | private: | |
9050 | // The underlying call expression. | |
9051 | Expression* call_; | |
9052 | // Which result we want. | |
9053 | unsigned int index_; | |
9054 | }; | |
9055 | ||
9056 | // Traverse a call result. | |
9057 | ||
9058 | int | |
9059 | Call_result_expression::do_traverse(Traverse* traverse) | |
9060 | { | |
9061 | if (traverse->remember_expression(this->call_)) | |
9062 | { | |
9063 | // We have already traversed the call expression. | |
9064 | return TRAVERSE_CONTINUE; | |
9065 | } | |
9066 | return Expression::traverse(&this->call_, traverse); | |
9067 | } | |
9068 | ||
9069 | // Get the type. | |
9070 | ||
9071 | Type* | |
9072 | Call_result_expression::do_type() | |
9073 | { | |
425dd051 | 9074 | if (this->classification() == EXPRESSION_ERROR) |
9075 | return Type::make_error_type(); | |
9076 | ||
e440a328 | 9077 | // THIS->CALL_ can be replaced with a temporary reference due to |
9078 | // Call_expression::do_must_eval_in_order when there is an error. | |
9079 | Call_expression* ce = this->call_->call_expression(); | |
9080 | if (ce == NULL) | |
5e85f268 | 9081 | { |
9082 | this->set_is_error(); | |
9083 | return Type::make_error_type(); | |
9084 | } | |
e440a328 | 9085 | Function_type* fntype = ce->get_function_type(); |
9086 | if (fntype == NULL) | |
5e85f268 | 9087 | { |
9088 | this->set_is_error(); | |
9089 | return Type::make_error_type(); | |
9090 | } | |
e440a328 | 9091 | const Typed_identifier_list* results = fntype->results(); |
7b8d861f | 9092 | if (results == NULL) |
9093 | { | |
9094 | this->report_error(_("number of results does not match " | |
9095 | "number of values")); | |
9096 | return Type::make_error_type(); | |
9097 | } | |
e440a328 | 9098 | Typed_identifier_list::const_iterator pr = results->begin(); |
9099 | for (unsigned int i = 0; i < this->index_; ++i) | |
9100 | { | |
9101 | if (pr == results->end()) | |
425dd051 | 9102 | break; |
e440a328 | 9103 | ++pr; |
9104 | } | |
9105 | if (pr == results->end()) | |
425dd051 | 9106 | { |
9107 | this->report_error(_("number of results does not match " | |
9108 | "number of values")); | |
9109 | return Type::make_error_type(); | |
9110 | } | |
e440a328 | 9111 | return pr->type(); |
9112 | } | |
9113 | ||
425dd051 | 9114 | // Check the type. Just make sure that we trigger the warning in |
9115 | // do_type. | |
e440a328 | 9116 | |
9117 | void | |
9118 | Call_result_expression::do_check_types(Gogo*) | |
9119 | { | |
425dd051 | 9120 | this->type(); |
e440a328 | 9121 | } |
9122 | ||
9123 | // Determine the type. We have nothing to do here, but the 0 result | |
9124 | // needs to pass down to the caller. | |
9125 | ||
9126 | void | |
9127 | Call_result_expression::do_determine_type(const Type_context*) | |
9128 | { | |
fb94b0ca | 9129 | this->call_->determine_type_no_context(); |
e440a328 | 9130 | } |
9131 | ||
9132 | // Return the tree. | |
9133 | ||
9134 | tree | |
9135 | Call_result_expression::do_get_tree(Translate_context* context) | |
9136 | { | |
9137 | tree call_tree = this->call_->get_tree(context); | |
9138 | if (call_tree == error_mark_node) | |
9139 | return error_mark_node; | |
5e85f268 | 9140 | if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE) |
9141 | { | |
9142 | gcc_assert(saw_errors()); | |
9143 | return error_mark_node; | |
9144 | } | |
e440a328 | 9145 | tree field = TYPE_FIELDS(TREE_TYPE(call_tree)); |
9146 | for (unsigned int i = 0; i < this->index_; ++i) | |
9147 | { | |
9148 | gcc_assert(field != NULL_TREE); | |
9149 | field = DECL_CHAIN(field); | |
9150 | } | |
9151 | gcc_assert(field != NULL_TREE); | |
9152 | return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE); | |
9153 | } | |
9154 | ||
9155 | // Make a reference to a single result of a call which returns | |
9156 | // multiple results. | |
9157 | ||
9158 | Expression* | |
9159 | Expression::make_call_result(Call_expression* call, unsigned int index) | |
9160 | { | |
9161 | return new Call_result_expression(call, index); | |
9162 | } | |
9163 | ||
9164 | // Class Index_expression. | |
9165 | ||
9166 | // Traversal. | |
9167 | ||
9168 | int | |
9169 | Index_expression::do_traverse(Traverse* traverse) | |
9170 | { | |
9171 | if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT | |
9172 | || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT | |
9173 | || (this->end_ != NULL | |
9174 | && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)) | |
9175 | return TRAVERSE_EXIT; | |
9176 | return TRAVERSE_CONTINUE; | |
9177 | } | |
9178 | ||
9179 | // Lower an index expression. This converts the generic index | |
9180 | // expression into an array index, a string index, or a map index. | |
9181 | ||
9182 | Expression* | |
9183 | Index_expression::do_lower(Gogo*, Named_object*, int) | |
9184 | { | |
9185 | source_location location = this->location(); | |
9186 | Expression* left = this->left_; | |
9187 | Expression* start = this->start_; | |
9188 | Expression* end = this->end_; | |
9189 | ||
9190 | Type* type = left->type(); | |
5c13bd80 | 9191 | if (type->is_error()) |
e440a328 | 9192 | return Expression::make_error(location); |
b0cf7ddd | 9193 | else if (left->is_type_expression()) |
9194 | { | |
9195 | error_at(location, "attempt to index type expression"); | |
9196 | return Expression::make_error(location); | |
9197 | } | |
e440a328 | 9198 | else if (type->array_type() != NULL) |
9199 | return Expression::make_array_index(left, start, end, location); | |
9200 | else if (type->points_to() != NULL | |
9201 | && type->points_to()->array_type() != NULL | |
9202 | && !type->points_to()->is_open_array_type()) | |
9203 | { | |
9204 | Expression* deref = Expression::make_unary(OPERATOR_MULT, left, | |
9205 | location); | |
9206 | return Expression::make_array_index(deref, start, end, location); | |
9207 | } | |
9208 | else if (type->is_string_type()) | |
9209 | return Expression::make_string_index(left, start, end, location); | |
9210 | else if (type->map_type() != NULL) | |
9211 | { | |
9212 | if (end != NULL) | |
9213 | { | |
9214 | error_at(location, "invalid slice of map"); | |
9215 | return Expression::make_error(location); | |
9216 | } | |
9217 | Map_index_expression* ret= Expression::make_map_index(left, start, | |
9218 | location); | |
9219 | if (this->is_lvalue_) | |
9220 | ret->set_is_lvalue(); | |
9221 | return ret; | |
9222 | } | |
9223 | else | |
9224 | { | |
9225 | error_at(location, | |
9226 | "attempt to index object which is not array, string, or map"); | |
9227 | return Expression::make_error(location); | |
9228 | } | |
9229 | } | |
9230 | ||
9231 | // Make an index expression. | |
9232 | ||
9233 | Expression* | |
9234 | Expression::make_index(Expression* left, Expression* start, Expression* end, | |
9235 | source_location location) | |
9236 | { | |
9237 | return new Index_expression(left, start, end, location); | |
9238 | } | |
9239 | ||
9240 | // An array index. This is used for both indexing and slicing. | |
9241 | ||
9242 | class Array_index_expression : public Expression | |
9243 | { | |
9244 | public: | |
9245 | Array_index_expression(Expression* array, Expression* start, | |
9246 | Expression* end, source_location location) | |
9247 | : Expression(EXPRESSION_ARRAY_INDEX, location), | |
9248 | array_(array), start_(start), end_(end), type_(NULL) | |
9249 | { } | |
9250 | ||
9251 | protected: | |
9252 | int | |
9253 | do_traverse(Traverse*); | |
9254 | ||
9255 | Type* | |
9256 | do_type(); | |
9257 | ||
9258 | void | |
9259 | do_determine_type(const Type_context*); | |
9260 | ||
9261 | void | |
9262 | do_check_types(Gogo*); | |
9263 | ||
9264 | Expression* | |
9265 | do_copy() | |
9266 | { | |
9267 | return Expression::make_array_index(this->array_->copy(), | |
9268 | this->start_->copy(), | |
9269 | (this->end_ == NULL | |
9270 | ? NULL | |
9271 | : this->end_->copy()), | |
9272 | this->location()); | |
9273 | } | |
9274 | ||
9275 | bool | |
9276 | do_is_addressable() const; | |
9277 | ||
9278 | void | |
9279 | do_address_taken(bool escapes) | |
9280 | { this->array_->address_taken(escapes); } | |
9281 | ||
9282 | tree | |
9283 | do_get_tree(Translate_context*); | |
9284 | ||
9285 | private: | |
9286 | // The array we are getting a value from. | |
9287 | Expression* array_; | |
9288 | // The start or only index. | |
9289 | Expression* start_; | |
9290 | // The end index of a slice. This may be NULL for a simple array | |
9291 | // index, or it may be a nil expression for the length of the array. | |
9292 | Expression* end_; | |
9293 | // The type of the expression. | |
9294 | Type* type_; | |
9295 | }; | |
9296 | ||
9297 | // Array index traversal. | |
9298 | ||
9299 | int | |
9300 | Array_index_expression::do_traverse(Traverse* traverse) | |
9301 | { | |
9302 | if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT) | |
9303 | return TRAVERSE_EXIT; | |
9304 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
9305 | return TRAVERSE_EXIT; | |
9306 | if (this->end_ != NULL) | |
9307 | { | |
9308 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
9309 | return TRAVERSE_EXIT; | |
9310 | } | |
9311 | return TRAVERSE_CONTINUE; | |
9312 | } | |
9313 | ||
9314 | // Return the type of an array index. | |
9315 | ||
9316 | Type* | |
9317 | Array_index_expression::do_type() | |
9318 | { | |
9319 | if (this->type_ == NULL) | |
9320 | { | |
9321 | Array_type* type = this->array_->type()->array_type(); | |
9322 | if (type == NULL) | |
9323 | this->type_ = Type::make_error_type(); | |
9324 | else if (this->end_ == NULL) | |
9325 | this->type_ = type->element_type(); | |
9326 | else if (type->is_open_array_type()) | |
9327 | { | |
9328 | // A slice of a slice has the same type as the original | |
9329 | // slice. | |
9330 | this->type_ = this->array_->type()->deref(); | |
9331 | } | |
9332 | else | |
9333 | { | |
9334 | // A slice of an array is a slice. | |
9335 | this->type_ = Type::make_array_type(type->element_type(), NULL); | |
9336 | } | |
9337 | } | |
9338 | return this->type_; | |
9339 | } | |
9340 | ||
9341 | // Set the type of an array index. | |
9342 | ||
9343 | void | |
9344 | Array_index_expression::do_determine_type(const Type_context*) | |
9345 | { | |
9346 | this->array_->determine_type_no_context(); | |
7917ad68 | 9347 | this->start_->determine_type_no_context(); |
e440a328 | 9348 | if (this->end_ != NULL) |
7917ad68 | 9349 | this->end_->determine_type_no_context(); |
e440a328 | 9350 | } |
9351 | ||
9352 | // Check types of an array index. | |
9353 | ||
9354 | void | |
9355 | Array_index_expression::do_check_types(Gogo*) | |
9356 | { | |
9357 | if (this->start_->type()->integer_type() == NULL) | |
9358 | this->report_error(_("index must be integer")); | |
9359 | if (this->end_ != NULL | |
9360 | && this->end_->type()->integer_type() == NULL | |
9361 | && !this->end_->is_nil_expression()) | |
9362 | this->report_error(_("slice end must be integer")); | |
9363 | ||
9364 | Array_type* array_type = this->array_->type()->array_type(); | |
f9c68f17 | 9365 | if (array_type == NULL) |
9366 | { | |
5c13bd80 | 9367 | gcc_assert(this->array_->type()->is_error()); |
f9c68f17 | 9368 | return; |
9369 | } | |
e440a328 | 9370 | |
9371 | unsigned int int_bits = | |
9372 | Type::lookup_integer_type("int")->integer_type()->bits(); | |
9373 | ||
9374 | Type* dummy; | |
9375 | mpz_t lval; | |
9376 | mpz_init(lval); | |
9377 | bool lval_valid = (array_type->length() != NULL | |
9378 | && array_type->length()->integer_constant_value(true, | |
9379 | lval, | |
9380 | &dummy)); | |
9381 | mpz_t ival; | |
9382 | mpz_init(ival); | |
9383 | if (this->start_->integer_constant_value(true, ival, &dummy)) | |
9384 | { | |
9385 | if (mpz_sgn(ival) < 0 | |
9386 | || mpz_sizeinbase(ival, 2) >= int_bits | |
9387 | || (lval_valid | |
9388 | && (this->end_ == NULL | |
9389 | ? mpz_cmp(ival, lval) >= 0 | |
9390 | : mpz_cmp(ival, lval) > 0))) | |
9391 | { | |
9392 | error_at(this->start_->location(), "array index out of bounds"); | |
9393 | this->set_is_error(); | |
9394 | } | |
9395 | } | |
9396 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
9397 | { | |
9398 | if (this->end_->integer_constant_value(true, ival, &dummy)) | |
9399 | { | |
9400 | if (mpz_sgn(ival) < 0 | |
9401 | || mpz_sizeinbase(ival, 2) >= int_bits | |
9402 | || (lval_valid && mpz_cmp(ival, lval) > 0)) | |
9403 | { | |
9404 | error_at(this->end_->location(), "array index out of bounds"); | |
9405 | this->set_is_error(); | |
9406 | } | |
9407 | } | |
9408 | } | |
9409 | mpz_clear(ival); | |
9410 | mpz_clear(lval); | |
9411 | ||
9412 | // A slice of an array requires an addressable array. A slice of a | |
9413 | // slice is always possible. | |
88ec30c8 | 9414 | if (this->end_ != NULL && !array_type->is_open_array_type()) |
9415 | { | |
9416 | if (!this->array_->is_addressable()) | |
9417 | this->report_error(_("array is not addressable")); | |
9418 | else | |
9419 | this->array_->address_taken(true); | |
9420 | } | |
e440a328 | 9421 | } |
9422 | ||
9423 | // Return whether this expression is addressable. | |
9424 | ||
9425 | bool | |
9426 | Array_index_expression::do_is_addressable() const | |
9427 | { | |
9428 | // A slice expression is not addressable. | |
9429 | if (this->end_ != NULL) | |
9430 | return false; | |
9431 | ||
9432 | // An index into a slice is addressable. | |
9433 | if (this->array_->type()->is_open_array_type()) | |
9434 | return true; | |
9435 | ||
9436 | // An index into an array is addressable if the array is | |
9437 | // addressable. | |
9438 | return this->array_->is_addressable(); | |
9439 | } | |
9440 | ||
9441 | // Get a tree for an array index. | |
9442 | ||
9443 | tree | |
9444 | Array_index_expression::do_get_tree(Translate_context* context) | |
9445 | { | |
9446 | Gogo* gogo = context->gogo(); | |
9447 | source_location loc = this->location(); | |
9448 | ||
9449 | Array_type* array_type = this->array_->type()->array_type(); | |
d8cd8e2d | 9450 | if (array_type == NULL) |
9451 | { | |
5c13bd80 | 9452 | gcc_assert(this->array_->type()->is_error()); |
d8cd8e2d | 9453 | return error_mark_node; |
9454 | } | |
e440a328 | 9455 | |
9456 | tree type_tree = array_type->get_tree(gogo); | |
c65212a0 | 9457 | if (type_tree == error_mark_node) |
9458 | return error_mark_node; | |
e440a328 | 9459 | |
9460 | tree array_tree = this->array_->get_tree(context); | |
9461 | if (array_tree == error_mark_node) | |
9462 | return error_mark_node; | |
9463 | ||
9464 | if (array_type->length() == NULL && !DECL_P(array_tree)) | |
9465 | array_tree = save_expr(array_tree); | |
9466 | tree length_tree = array_type->length_tree(gogo, array_tree); | |
c65212a0 | 9467 | if (length_tree == error_mark_node) |
9468 | return error_mark_node; | |
e440a328 | 9469 | length_tree = save_expr(length_tree); |
9470 | tree length_type = TREE_TYPE(length_tree); | |
9471 | ||
9472 | tree bad_index = boolean_false_node; | |
9473 | ||
9474 | tree start_tree = this->start_->get_tree(context); | |
9475 | if (start_tree == error_mark_node) | |
9476 | return error_mark_node; | |
9477 | if (!DECL_P(start_tree)) | |
9478 | start_tree = save_expr(start_tree); | |
9479 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
9480 | start_tree = convert_to_integer(length_type, start_tree); | |
9481 | ||
9482 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
9483 | loc); | |
9484 | ||
9485 | start_tree = fold_convert_loc(loc, length_type, start_tree); | |
9486 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index, | |
9487 | fold_build2_loc(loc, | |
9488 | (this->end_ == NULL | |
9489 | ? GE_EXPR | |
9490 | : GT_EXPR), | |
9491 | boolean_type_node, start_tree, | |
9492 | length_tree)); | |
9493 | ||
9494 | int code = (array_type->length() != NULL | |
9495 | ? (this->end_ == NULL | |
9496 | ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS | |
9497 | : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS) | |
9498 | : (this->end_ == NULL | |
9499 | ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS | |
9500 | : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS)); | |
9501 | tree crash = Gogo::runtime_error(code, loc); | |
9502 | ||
9503 | if (this->end_ == NULL) | |
9504 | { | |
9505 | // Simple array indexing. This has to return an l-value, so | |
9506 | // wrap the index check into START_TREE. | |
9507 | start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree), | |
9508 | build3(COND_EXPR, void_type_node, | |
9509 | bad_index, crash, NULL_TREE), | |
9510 | start_tree); | |
9511 | start_tree = fold_convert_loc(loc, sizetype, start_tree); | |
9512 | ||
9513 | if (array_type->length() != NULL) | |
9514 | { | |
9515 | // Fixed array. | |
9516 | return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree, | |
9517 | start_tree, NULL_TREE, NULL_TREE); | |
9518 | } | |
9519 | else | |
9520 | { | |
9521 | // Open array. | |
9522 | tree values = array_type->value_pointer_tree(gogo, array_tree); | |
9523 | tree element_type_tree = array_type->element_type()->get_tree(gogo); | |
c65212a0 | 9524 | if (element_type_tree == error_mark_node) |
9525 | return error_mark_node; | |
e440a328 | 9526 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
9527 | tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype, | |
9528 | start_tree, element_size); | |
9529 | tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, | |
9530 | TREE_TYPE(values), values, offset); | |
9531 | return build_fold_indirect_ref(ptr); | |
9532 | } | |
9533 | } | |
9534 | ||
9535 | // Array slice. | |
9536 | ||
9537 | tree capacity_tree = array_type->capacity_tree(gogo, array_tree); | |
c65212a0 | 9538 | if (capacity_tree == error_mark_node) |
9539 | return error_mark_node; | |
e440a328 | 9540 | capacity_tree = fold_convert_loc(loc, length_type, capacity_tree); |
9541 | ||
9542 | tree end_tree; | |
9543 | if (this->end_->is_nil_expression()) | |
9544 | end_tree = length_tree; | |
9545 | else | |
9546 | { | |
9547 | end_tree = this->end_->get_tree(context); | |
9548 | if (end_tree == error_mark_node) | |
9549 | return error_mark_node; | |
9550 | if (!DECL_P(end_tree)) | |
9551 | end_tree = save_expr(end_tree); | |
9552 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
9553 | end_tree = convert_to_integer(length_type, end_tree); | |
9554 | ||
9555 | bad_index = Expression::check_bounds(end_tree, length_type, bad_index, | |
9556 | loc); | |
9557 | ||
9558 | end_tree = fold_convert_loc(loc, length_type, end_tree); | |
9559 | ||
9560 | capacity_tree = save_expr(capacity_tree); | |
9561 | tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9562 | fold_build2_loc(loc, LT_EXPR, | |
9563 | boolean_type_node, | |
9564 | end_tree, start_tree), | |
9565 | fold_build2_loc(loc, GT_EXPR, | |
9566 | boolean_type_node, | |
9567 | end_tree, capacity_tree)); | |
9568 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9569 | bad_index, bad_end); | |
9570 | } | |
9571 | ||
9572 | tree element_type_tree = array_type->element_type()->get_tree(gogo); | |
c65212a0 | 9573 | if (element_type_tree == error_mark_node) |
9574 | return error_mark_node; | |
e440a328 | 9575 | tree element_size = TYPE_SIZE_UNIT(element_type_tree); |
9576 | ||
9577 | tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype, | |
9578 | fold_convert_loc(loc, sizetype, start_tree), | |
9579 | element_size); | |
9580 | ||
9581 | tree value_pointer = array_type->value_pointer_tree(gogo, array_tree); | |
c65212a0 | 9582 | if (value_pointer == error_mark_node) |
9583 | return error_mark_node; | |
e440a328 | 9584 | |
9585 | value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR, | |
9586 | TREE_TYPE(value_pointer), | |
9587 | value_pointer, offset); | |
9588 | ||
9589 | tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type, | |
9590 | end_tree, start_tree); | |
9591 | ||
9592 | tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type, | |
9593 | capacity_tree, start_tree); | |
9594 | ||
9595 | tree struct_tree = this->type()->get_tree(gogo); | |
9596 | gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE); | |
9597 | ||
9598 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
9599 | ||
9600 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
9601 | tree field = TYPE_FIELDS(struct_tree); | |
9602 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); | |
9603 | elt->index = field; | |
9604 | elt->value = value_pointer; | |
9605 | ||
9606 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
9607 | field = DECL_CHAIN(field); | |
9608 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); | |
9609 | elt->index = field; | |
9610 | elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree); | |
9611 | ||
9612 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
9613 | field = DECL_CHAIN(field); | |
9614 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0); | |
9615 | elt->index = field; | |
9616 | elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree); | |
9617 | ||
9618 | tree constructor = build_constructor(struct_tree, init); | |
9619 | ||
9620 | if (TREE_CONSTANT(value_pointer) | |
9621 | && TREE_CONSTANT(result_length_tree) | |
9622 | && TREE_CONSTANT(result_capacity_tree)) | |
9623 | TREE_CONSTANT(constructor) = 1; | |
9624 | ||
9625 | return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor), | |
9626 | build3(COND_EXPR, void_type_node, | |
9627 | bad_index, crash, NULL_TREE), | |
9628 | constructor); | |
9629 | } | |
9630 | ||
9631 | // Make an array index expression. END may be NULL. | |
9632 | ||
9633 | Expression* | |
9634 | Expression::make_array_index(Expression* array, Expression* start, | |
9635 | Expression* end, source_location location) | |
9636 | { | |
9637 | // Taking a slice of a composite literal requires moving the literal | |
9638 | // onto the heap. | |
9639 | if (end != NULL && array->is_composite_literal()) | |
9640 | { | |
9641 | array = Expression::make_heap_composite(array, location); | |
9642 | array = Expression::make_unary(OPERATOR_MULT, array, location); | |
9643 | } | |
9644 | return new Array_index_expression(array, start, end, location); | |
9645 | } | |
9646 | ||
9647 | // A string index. This is used for both indexing and slicing. | |
9648 | ||
9649 | class String_index_expression : public Expression | |
9650 | { | |
9651 | public: | |
9652 | String_index_expression(Expression* string, Expression* start, | |
9653 | Expression* end, source_location location) | |
9654 | : Expression(EXPRESSION_STRING_INDEX, location), | |
9655 | string_(string), start_(start), end_(end) | |
9656 | { } | |
9657 | ||
9658 | protected: | |
9659 | int | |
9660 | do_traverse(Traverse*); | |
9661 | ||
9662 | Type* | |
9663 | do_type(); | |
9664 | ||
9665 | void | |
9666 | do_determine_type(const Type_context*); | |
9667 | ||
9668 | void | |
9669 | do_check_types(Gogo*); | |
9670 | ||
9671 | Expression* | |
9672 | do_copy() | |
9673 | { | |
9674 | return Expression::make_string_index(this->string_->copy(), | |
9675 | this->start_->copy(), | |
9676 | (this->end_ == NULL | |
9677 | ? NULL | |
9678 | : this->end_->copy()), | |
9679 | this->location()); | |
9680 | } | |
9681 | ||
9682 | tree | |
9683 | do_get_tree(Translate_context*); | |
9684 | ||
9685 | private: | |
9686 | // The string we are getting a value from. | |
9687 | Expression* string_; | |
9688 | // The start or only index. | |
9689 | Expression* start_; | |
9690 | // The end index of a slice. This may be NULL for a single index, | |
9691 | // or it may be a nil expression for the length of the string. | |
9692 | Expression* end_; | |
9693 | }; | |
9694 | ||
9695 | // String index traversal. | |
9696 | ||
9697 | int | |
9698 | String_index_expression::do_traverse(Traverse* traverse) | |
9699 | { | |
9700 | if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT) | |
9701 | return TRAVERSE_EXIT; | |
9702 | if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT) | |
9703 | return TRAVERSE_EXIT; | |
9704 | if (this->end_ != NULL) | |
9705 | { | |
9706 | if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT) | |
9707 | return TRAVERSE_EXIT; | |
9708 | } | |
9709 | return TRAVERSE_CONTINUE; | |
9710 | } | |
9711 | ||
9712 | // Return the type of a string index. | |
9713 | ||
9714 | Type* | |
9715 | String_index_expression::do_type() | |
9716 | { | |
9717 | if (this->end_ == NULL) | |
9718 | return Type::lookup_integer_type("uint8"); | |
9719 | else | |
7672d35f | 9720 | return this->string_->type(); |
e440a328 | 9721 | } |
9722 | ||
9723 | // Determine the type of a string index. | |
9724 | ||
9725 | void | |
9726 | String_index_expression::do_determine_type(const Type_context*) | |
9727 | { | |
9728 | this->string_->determine_type_no_context(); | |
93000773 | 9729 | this->start_->determine_type_no_context(); |
e440a328 | 9730 | if (this->end_ != NULL) |
93000773 | 9731 | this->end_->determine_type_no_context(); |
e440a328 | 9732 | } |
9733 | ||
9734 | // Check types of a string index. | |
9735 | ||
9736 | void | |
9737 | String_index_expression::do_check_types(Gogo*) | |
9738 | { | |
9739 | if (this->start_->type()->integer_type() == NULL) | |
9740 | this->report_error(_("index must be integer")); | |
9741 | if (this->end_ != NULL | |
9742 | && this->end_->type()->integer_type() == NULL | |
9743 | && !this->end_->is_nil_expression()) | |
9744 | this->report_error(_("slice end must be integer")); | |
9745 | ||
9746 | std::string sval; | |
9747 | bool sval_valid = this->string_->string_constant_value(&sval); | |
9748 | ||
9749 | mpz_t ival; | |
9750 | mpz_init(ival); | |
9751 | Type* dummy; | |
9752 | if (this->start_->integer_constant_value(true, ival, &dummy)) | |
9753 | { | |
9754 | if (mpz_sgn(ival) < 0 | |
9755 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0)) | |
9756 | { | |
9757 | error_at(this->start_->location(), "string index out of bounds"); | |
9758 | this->set_is_error(); | |
9759 | } | |
9760 | } | |
9761 | if (this->end_ != NULL && !this->end_->is_nil_expression()) | |
9762 | { | |
9763 | if (this->end_->integer_constant_value(true, ival, &dummy)) | |
9764 | { | |
9765 | if (mpz_sgn(ival) < 0 | |
9766 | || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0)) | |
9767 | { | |
9768 | error_at(this->end_->location(), "string index out of bounds"); | |
9769 | this->set_is_error(); | |
9770 | } | |
9771 | } | |
9772 | } | |
9773 | mpz_clear(ival); | |
9774 | } | |
9775 | ||
9776 | // Get a tree for a string index. | |
9777 | ||
9778 | tree | |
9779 | String_index_expression::do_get_tree(Translate_context* context) | |
9780 | { | |
9781 | source_location loc = this->location(); | |
9782 | ||
9783 | tree string_tree = this->string_->get_tree(context); | |
9784 | if (string_tree == error_mark_node) | |
9785 | return error_mark_node; | |
9786 | ||
9787 | if (this->string_->type()->points_to() != NULL) | |
9788 | string_tree = build_fold_indirect_ref(string_tree); | |
9789 | if (!DECL_P(string_tree)) | |
9790 | string_tree = save_expr(string_tree); | |
9791 | tree string_type = TREE_TYPE(string_tree); | |
9792 | ||
9793 | tree length_tree = String_type::length_tree(context->gogo(), string_tree); | |
9794 | length_tree = save_expr(length_tree); | |
9795 | tree length_type = TREE_TYPE(length_tree); | |
9796 | ||
9797 | tree bad_index = boolean_false_node; | |
9798 | ||
9799 | tree start_tree = this->start_->get_tree(context); | |
9800 | if (start_tree == error_mark_node) | |
9801 | return error_mark_node; | |
9802 | if (!DECL_P(start_tree)) | |
9803 | start_tree = save_expr(start_tree); | |
9804 | if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree))) | |
9805 | start_tree = convert_to_integer(length_type, start_tree); | |
9806 | ||
9807 | bad_index = Expression::check_bounds(start_tree, length_type, bad_index, | |
9808 | loc); | |
9809 | ||
9810 | start_tree = fold_convert_loc(loc, length_type, start_tree); | |
9811 | ||
9812 | int code = (this->end_ == NULL | |
9813 | ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS | |
9814 | : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS); | |
9815 | tree crash = Gogo::runtime_error(code, loc); | |
9816 | ||
9817 | if (this->end_ == NULL) | |
9818 | { | |
9819 | bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, | |
9820 | bad_index, | |
9821 | fold_build2_loc(loc, GE_EXPR, | |
9822 | boolean_type_node, | |
9823 | start_tree, length_tree)); | |
9824 | ||
9825 | tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree); | |
9826 | tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree), | |
9827 | bytes_tree, | |
9828 | fold_convert_loc(loc, sizetype, start_tree)); | |
9829 | tree index = build_fold_indirect_ref_loc(loc, ptr); | |
9830 | ||
9831 | return build2(COMPOUND_EXPR, TREE_TYPE(index), | |
9832 | build3(COND_EXPR, void_type_node, | |
9833 | bad_index, crash, NULL_TREE), | |
9834 | index); | |
9835 | } | |
9836 | else | |
9837 | { | |
9838 | tree end_tree; | |
9839 | if (this->end_->is_nil_expression()) | |
9840 | end_tree = build_int_cst(length_type, -1); | |
9841 | else | |
9842 | { | |
9843 | end_tree = this->end_->get_tree(context); | |
9844 | if (end_tree == error_mark_node) | |
9845 | return error_mark_node; | |
9846 | if (!DECL_P(end_tree)) | |
9847 | end_tree = save_expr(end_tree); | |
9848 | if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree))) | |
9849 | end_tree = convert_to_integer(length_type, end_tree); | |
9850 | ||
9851 | bad_index = Expression::check_bounds(end_tree, length_type, | |
9852 | bad_index, loc); | |
9853 | ||
9854 | end_tree = fold_convert_loc(loc, length_type, end_tree); | |
9855 | } | |
9856 | ||
9857 | static tree strslice_fndecl; | |
9858 | tree ret = Gogo::call_builtin(&strslice_fndecl, | |
9859 | loc, | |
9860 | "__go_string_slice", | |
9861 | 3, | |
9862 | string_type, | |
9863 | string_type, | |
9864 | string_tree, | |
9865 | length_type, | |
9866 | start_tree, | |
9867 | length_type, | |
9868 | end_tree); | |
5fb82b5e | 9869 | if (ret == error_mark_node) |
9870 | return error_mark_node; | |
e440a328 | 9871 | // This will panic if the bounds are out of range for the |
9872 | // string. | |
9873 | TREE_NOTHROW(strslice_fndecl) = 0; | |
9874 | ||
9875 | if (bad_index == boolean_false_node) | |
9876 | return ret; | |
9877 | else | |
9878 | return build2(COMPOUND_EXPR, TREE_TYPE(ret), | |
9879 | build3(COND_EXPR, void_type_node, | |
9880 | bad_index, crash, NULL_TREE), | |
9881 | ret); | |
9882 | } | |
9883 | } | |
9884 | ||
9885 | // Make a string index expression. END may be NULL. | |
9886 | ||
9887 | Expression* | |
9888 | Expression::make_string_index(Expression* string, Expression* start, | |
9889 | Expression* end, source_location location) | |
9890 | { | |
9891 | return new String_index_expression(string, start, end, location); | |
9892 | } | |
9893 | ||
9894 | // Class Map_index. | |
9895 | ||
9896 | // Get the type of the map. | |
9897 | ||
9898 | Map_type* | |
9899 | Map_index_expression::get_map_type() const | |
9900 | { | |
9901 | Map_type* mt = this->map_->type()->deref()->map_type(); | |
c7524fae | 9902 | if (mt == NULL) |
9903 | gcc_assert(saw_errors()); | |
e440a328 | 9904 | return mt; |
9905 | } | |
9906 | ||
9907 | // Map index traversal. | |
9908 | ||
9909 | int | |
9910 | Map_index_expression::do_traverse(Traverse* traverse) | |
9911 | { | |
9912 | if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT) | |
9913 | return TRAVERSE_EXIT; | |
9914 | return Expression::traverse(&this->index_, traverse); | |
9915 | } | |
9916 | ||
9917 | // Return the type of a map index. | |
9918 | ||
9919 | Type* | |
9920 | Map_index_expression::do_type() | |
9921 | { | |
c7524fae | 9922 | Map_type* mt = this->get_map_type(); |
9923 | if (mt == NULL) | |
9924 | return Type::make_error_type(); | |
9925 | Type* type = mt->val_type(); | |
e440a328 | 9926 | // If this map index is in a tuple assignment, we actually return a |
9927 | // pointer to the value type. Tuple_map_assignment_statement is | |
9928 | // responsible for handling this correctly. We need to get the type | |
9929 | // right in case this gets assigned to a temporary variable. | |
9930 | if (this->is_in_tuple_assignment_) | |
9931 | type = Type::make_pointer_type(type); | |
9932 | return type; | |
9933 | } | |
9934 | ||
9935 | // Fix the type of a map index. | |
9936 | ||
9937 | void | |
9938 | Map_index_expression::do_determine_type(const Type_context*) | |
9939 | { | |
9940 | this->map_->determine_type_no_context(); | |
c7524fae | 9941 | Map_type* mt = this->get_map_type(); |
9942 | Type* key_type = mt == NULL ? NULL : mt->key_type(); | |
9943 | Type_context subcontext(key_type, false); | |
e440a328 | 9944 | this->index_->determine_type(&subcontext); |
9945 | } | |
9946 | ||
9947 | // Check types of a map index. | |
9948 | ||
9949 | void | |
9950 | Map_index_expression::do_check_types(Gogo*) | |
9951 | { | |
9952 | std::string reason; | |
c7524fae | 9953 | Map_type* mt = this->get_map_type(); |
9954 | if (mt == NULL) | |
9955 | return; | |
9956 | if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) | |
e440a328 | 9957 | { |
9958 | if (reason.empty()) | |
9959 | this->report_error(_("incompatible type for map index")); | |
9960 | else | |
9961 | { | |
9962 | error_at(this->location(), "incompatible type for map index (%s)", | |
9963 | reason.c_str()); | |
9964 | this->set_is_error(); | |
9965 | } | |
9966 | } | |
9967 | } | |
9968 | ||
9969 | // Get a tree for a map index. | |
9970 | ||
9971 | tree | |
9972 | Map_index_expression::do_get_tree(Translate_context* context) | |
9973 | { | |
9974 | Map_type* type = this->get_map_type(); | |
c7524fae | 9975 | if (type == NULL) |
9976 | return error_mark_node; | |
e440a328 | 9977 | |
9978 | tree valptr = this->get_value_pointer(context, this->is_lvalue_); | |
9979 | if (valptr == error_mark_node) | |
9980 | return error_mark_node; | |
9981 | valptr = save_expr(valptr); | |
9982 | ||
9983 | tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr)); | |
9984 | ||
9985 | if (this->is_lvalue_) | |
9986 | return build_fold_indirect_ref(valptr); | |
9987 | else if (this->is_in_tuple_assignment_) | |
9988 | { | |
9989 | // Tuple_map_assignment_statement is responsible for using this | |
9990 | // appropriately. | |
9991 | return valptr; | |
9992 | } | |
9993 | else | |
9994 | { | |
9995 | return fold_build3(COND_EXPR, val_type_tree, | |
9996 | fold_build2(EQ_EXPR, boolean_type_node, valptr, | |
9997 | fold_convert(TREE_TYPE(valptr), | |
9998 | null_pointer_node)), | |
9999 | type->val_type()->get_init_tree(context->gogo(), | |
10000 | false), | |
10001 | build_fold_indirect_ref(valptr)); | |
10002 | } | |
10003 | } | |
10004 | ||
10005 | // Get a tree for the map index. This returns a tree which evaluates | |
10006 | // to a pointer to a value. The pointer will be NULL if the key is | |
10007 | // not in the map. | |
10008 | ||
10009 | tree | |
10010 | Map_index_expression::get_value_pointer(Translate_context* context, | |
10011 | bool insert) | |
10012 | { | |
10013 | Map_type* type = this->get_map_type(); | |
c7524fae | 10014 | if (type == NULL) |
10015 | return error_mark_node; | |
e440a328 | 10016 | |
10017 | tree map_tree = this->map_->get_tree(context); | |
10018 | tree index_tree = this->index_->get_tree(context); | |
10019 | index_tree = Expression::convert_for_assignment(context, type->key_type(), | |
10020 | this->index_->type(), | |
10021 | index_tree, | |
10022 | this->location()); | |
10023 | if (map_tree == error_mark_node || index_tree == error_mark_node) | |
10024 | return error_mark_node; | |
10025 | ||
10026 | if (this->map_->type()->points_to() != NULL) | |
10027 | map_tree = build_fold_indirect_ref(map_tree); | |
10028 | ||
10029 | // We need to pass in a pointer to the key, so stuff it into a | |
10030 | // variable. | |
746d2e73 | 10031 | tree tmp; |
10032 | tree make_tmp; | |
10033 | if (current_function_decl != NULL) | |
10034 | { | |
10035 | tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree)); | |
10036 | DECL_IGNORED_P(tmp) = 0; | |
10037 | DECL_INITIAL(tmp) = index_tree; | |
10038 | make_tmp = build1(DECL_EXPR, void_type_node, tmp); | |
10039 | TREE_ADDRESSABLE(tmp) = 1; | |
10040 | } | |
10041 | else | |
10042 | { | |
10043 | tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"), | |
10044 | TREE_TYPE(index_tree)); | |
10045 | DECL_EXTERNAL(tmp) = 0; | |
10046 | TREE_PUBLIC(tmp) = 0; | |
10047 | TREE_STATIC(tmp) = 1; | |
10048 | DECL_ARTIFICIAL(tmp) = 1; | |
10049 | if (!TREE_CONSTANT(index_tree)) | |
10050 | make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node, | |
10051 | tmp, index_tree); | |
10052 | else | |
10053 | { | |
10054 | TREE_READONLY(tmp) = 1; | |
10055 | TREE_CONSTANT(tmp) = 1; | |
10056 | DECL_INITIAL(tmp) = index_tree; | |
10057 | make_tmp = NULL_TREE; | |
10058 | } | |
10059 | rest_of_decl_compilation(tmp, 1, 0); | |
10060 | } | |
10061 | tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node, | |
10062 | build_fold_addr_expr_loc(this->location(), | |
10063 | tmp)); | |
e440a328 | 10064 | |
10065 | static tree map_index_fndecl; | |
10066 | tree call = Gogo::call_builtin(&map_index_fndecl, | |
10067 | this->location(), | |
10068 | "__go_map_index", | |
10069 | 3, | |
10070 | const_ptr_type_node, | |
10071 | TREE_TYPE(map_tree), | |
10072 | map_tree, | |
10073 | const_ptr_type_node, | |
10074 | tmpref, | |
10075 | boolean_type_node, | |
10076 | (insert | |
10077 | ? boolean_true_node | |
10078 | : boolean_false_node)); | |
5fb82b5e | 10079 | if (call == error_mark_node) |
10080 | return error_mark_node; | |
e440a328 | 10081 | // This can panic on a map of interface type if the interface holds |
10082 | // an uncomparable or unhashable type. | |
10083 | TREE_NOTHROW(map_index_fndecl) = 0; | |
10084 | ||
10085 | tree val_type_tree = type->val_type()->get_tree(context->gogo()); | |
10086 | if (val_type_tree == error_mark_node) | |
10087 | return error_mark_node; | |
10088 | tree ptr_val_type_tree = build_pointer_type(val_type_tree); | |
10089 | ||
746d2e73 | 10090 | tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call); |
10091 | if (make_tmp != NULL_TREE) | |
10092 | ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret); | |
10093 | return ret; | |
e440a328 | 10094 | } |
10095 | ||
10096 | // Make a map index expression. | |
10097 | ||
10098 | Map_index_expression* | |
10099 | Expression::make_map_index(Expression* map, Expression* index, | |
10100 | source_location location) | |
10101 | { | |
10102 | return new Map_index_expression(map, index, location); | |
10103 | } | |
10104 | ||
10105 | // Class Field_reference_expression. | |
10106 | ||
10107 | // Return the type of a field reference. | |
10108 | ||
10109 | Type* | |
10110 | Field_reference_expression::do_type() | |
10111 | { | |
b0e628fb | 10112 | Type* type = this->expr_->type(); |
5c13bd80 | 10113 | if (type->is_error()) |
b0e628fb | 10114 | return type; |
10115 | Struct_type* struct_type = type->struct_type(); | |
e440a328 | 10116 | gcc_assert(struct_type != NULL); |
10117 | return struct_type->field(this->field_index_)->type(); | |
10118 | } | |
10119 | ||
10120 | // Check the types for a field reference. | |
10121 | ||
10122 | void | |
10123 | Field_reference_expression::do_check_types(Gogo*) | |
10124 | { | |
b0e628fb | 10125 | Type* type = this->expr_->type(); |
5c13bd80 | 10126 | if (type->is_error()) |
b0e628fb | 10127 | return; |
10128 | Struct_type* struct_type = type->struct_type(); | |
e440a328 | 10129 | gcc_assert(struct_type != NULL); |
10130 | gcc_assert(struct_type->field(this->field_index_) != NULL); | |
10131 | } | |
10132 | ||
10133 | // Get a tree for a field reference. | |
10134 | ||
10135 | tree | |
10136 | Field_reference_expression::do_get_tree(Translate_context* context) | |
10137 | { | |
10138 | tree struct_tree = this->expr_->get_tree(context); | |
10139 | if (struct_tree == error_mark_node | |
10140 | || TREE_TYPE(struct_tree) == error_mark_node) | |
10141 | return error_mark_node; | |
10142 | gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE); | |
10143 | tree field = TYPE_FIELDS(TREE_TYPE(struct_tree)); | |
b1d655d5 | 10144 | if (field == NULL_TREE) |
10145 | { | |
10146 | // This can happen for a type which refers to itself indirectly | |
10147 | // and then turns out to be erroneous. | |
10148 | gcc_assert(saw_errors()); | |
10149 | return error_mark_node; | |
10150 | } | |
e440a328 | 10151 | for (unsigned int i = this->field_index_; i > 0; --i) |
10152 | { | |
10153 | field = DECL_CHAIN(field); | |
10154 | gcc_assert(field != NULL_TREE); | |
10155 | } | |
c35179ff | 10156 | if (TREE_TYPE(field) == error_mark_node) |
10157 | return error_mark_node; | |
e440a328 | 10158 | return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field, |
10159 | NULL_TREE); | |
10160 | } | |
10161 | ||
10162 | // Make a reference to a qualified identifier in an expression. | |
10163 | ||
10164 | Field_reference_expression* | |
10165 | Expression::make_field_reference(Expression* expr, unsigned int field_index, | |
10166 | source_location location) | |
10167 | { | |
10168 | return new Field_reference_expression(expr, field_index, location); | |
10169 | } | |
10170 | ||
10171 | // Class Interface_field_reference_expression. | |
10172 | ||
10173 | // Return a tree for the pointer to the function to call. | |
10174 | ||
10175 | tree | |
10176 | Interface_field_reference_expression::get_function_tree(Translate_context*, | |
10177 | tree expr) | |
10178 | { | |
10179 | if (this->expr_->type()->points_to() != NULL) | |
10180 | expr = build_fold_indirect_ref(expr); | |
10181 | ||
10182 | tree expr_type = TREE_TYPE(expr); | |
10183 | gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE); | |
10184 | ||
10185 | tree field = TYPE_FIELDS(expr_type); | |
10186 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0); | |
10187 | ||
10188 | tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
10189 | gcc_assert(POINTER_TYPE_P(TREE_TYPE(table))); | |
10190 | ||
10191 | table = build_fold_indirect_ref(table); | |
10192 | gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE); | |
10193 | ||
10194 | std::string name = Gogo::unpack_hidden_name(this->name_); | |
10195 | for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table))); | |
10196 | field != NULL_TREE; | |
10197 | field = DECL_CHAIN(field)) | |
10198 | { | |
10199 | if (name == IDENTIFIER_POINTER(DECL_NAME(field))) | |
10200 | break; | |
10201 | } | |
10202 | gcc_assert(field != NULL_TREE); | |
10203 | ||
10204 | return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE); | |
10205 | } | |
10206 | ||
10207 | // Return a tree for the first argument to pass to the interface | |
10208 | // function. | |
10209 | ||
10210 | tree | |
10211 | Interface_field_reference_expression::get_underlying_object_tree( | |
10212 | Translate_context*, | |
10213 | tree expr) | |
10214 | { | |
10215 | if (this->expr_->type()->points_to() != NULL) | |
10216 | expr = build_fold_indirect_ref(expr); | |
10217 | ||
10218 | tree expr_type = TREE_TYPE(expr); | |
10219 | gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE); | |
10220 | ||
10221 | tree field = DECL_CHAIN(TYPE_FIELDS(expr_type)); | |
10222 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0); | |
10223 | ||
10224 | return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE); | |
10225 | } | |
10226 | ||
10227 | // Traversal. | |
10228 | ||
10229 | int | |
10230 | Interface_field_reference_expression::do_traverse(Traverse* traverse) | |
10231 | { | |
10232 | return Expression::traverse(&this->expr_, traverse); | |
10233 | } | |
10234 | ||
10235 | // Return the type of an interface field reference. | |
10236 | ||
10237 | Type* | |
10238 | Interface_field_reference_expression::do_type() | |
10239 | { | |
10240 | Type* expr_type = this->expr_->type(); | |
10241 | ||
10242 | Type* points_to = expr_type->points_to(); | |
10243 | if (points_to != NULL) | |
10244 | expr_type = points_to; | |
10245 | ||
10246 | Interface_type* interface_type = expr_type->interface_type(); | |
10247 | if (interface_type == NULL) | |
10248 | return Type::make_error_type(); | |
10249 | ||
10250 | const Typed_identifier* method = interface_type->find_method(this->name_); | |
10251 | if (method == NULL) | |
10252 | return Type::make_error_type(); | |
10253 | ||
10254 | return method->type(); | |
10255 | } | |
10256 | ||
10257 | // Determine types. | |
10258 | ||
10259 | void | |
10260 | Interface_field_reference_expression::do_determine_type(const Type_context*) | |
10261 | { | |
10262 | this->expr_->determine_type_no_context(); | |
10263 | } | |
10264 | ||
10265 | // Check the types for an interface field reference. | |
10266 | ||
10267 | void | |
10268 | Interface_field_reference_expression::do_check_types(Gogo*) | |
10269 | { | |
10270 | Type* type = this->expr_->type(); | |
10271 | ||
10272 | Type* points_to = type->points_to(); | |
10273 | if (points_to != NULL) | |
10274 | type = points_to; | |
10275 | ||
10276 | Interface_type* interface_type = type->interface_type(); | |
10277 | if (interface_type == NULL) | |
5c491127 | 10278 | { |
10279 | if (!type->is_error_type()) | |
10280 | this->report_error(_("expected interface or pointer to interface")); | |
10281 | } | |
e440a328 | 10282 | else |
10283 | { | |
10284 | const Typed_identifier* method = | |
10285 | interface_type->find_method(this->name_); | |
10286 | if (method == NULL) | |
10287 | { | |
10288 | error_at(this->location(), "method %qs not in interface", | |
10289 | Gogo::message_name(this->name_).c_str()); | |
10290 | this->set_is_error(); | |
10291 | } | |
10292 | } | |
10293 | } | |
10294 | ||
10295 | // Get a tree for a reference to a field in an interface. There is no | |
10296 | // standard tree type representation for this: it's a function | |
10297 | // attached to its first argument, like a Bound_method_expression. | |
10298 | // The only places it may currently be used are in a Call_expression | |
10299 | // or a Go_statement, which will take it apart directly. So this has | |
10300 | // nothing to do at present. | |
10301 | ||
10302 | tree | |
10303 | Interface_field_reference_expression::do_get_tree(Translate_context*) | |
10304 | { | |
10305 | gcc_unreachable(); | |
10306 | } | |
10307 | ||
10308 | // Make a reference to a field in an interface. | |
10309 | ||
10310 | Expression* | |
10311 | Expression::make_interface_field_reference(Expression* expr, | |
10312 | const std::string& field, | |
10313 | source_location location) | |
10314 | { | |
10315 | return new Interface_field_reference_expression(expr, field, location); | |
10316 | } | |
10317 | ||
10318 | // A general selector. This is a Parser_expression for LEFT.NAME. It | |
10319 | // is lowered after we know the type of the left hand side. | |
10320 | ||
10321 | class Selector_expression : public Parser_expression | |
10322 | { | |
10323 | public: | |
10324 | Selector_expression(Expression* left, const std::string& name, | |
10325 | source_location location) | |
10326 | : Parser_expression(EXPRESSION_SELECTOR, location), | |
10327 | left_(left), name_(name) | |
10328 | { } | |
10329 | ||
10330 | protected: | |
10331 | int | |
10332 | do_traverse(Traverse* traverse) | |
10333 | { return Expression::traverse(&this->left_, traverse); } | |
10334 | ||
10335 | Expression* | |
10336 | do_lower(Gogo*, Named_object*, int); | |
10337 | ||
10338 | Expression* | |
10339 | do_copy() | |
10340 | { | |
10341 | return new Selector_expression(this->left_->copy(), this->name_, | |
10342 | this->location()); | |
10343 | } | |
10344 | ||
10345 | private: | |
10346 | Expression* | |
10347 | lower_method_expression(Gogo*); | |
10348 | ||
10349 | // The expression on the left hand side. | |
10350 | Expression* left_; | |
10351 | // The name on the right hand side. | |
10352 | std::string name_; | |
10353 | }; | |
10354 | ||
10355 | // Lower a selector expression once we know the real type of the left | |
10356 | // hand side. | |
10357 | ||
10358 | Expression* | |
10359 | Selector_expression::do_lower(Gogo* gogo, Named_object*, int) | |
10360 | { | |
10361 | Expression* left = this->left_; | |
10362 | if (left->is_type_expression()) | |
10363 | return this->lower_method_expression(gogo); | |
10364 | return Type::bind_field_or_method(gogo, left->type(), left, this->name_, | |
10365 | this->location()); | |
10366 | } | |
10367 | ||
10368 | // Lower a method expression T.M or (*T).M. We turn this into a | |
10369 | // function literal. | |
10370 | ||
10371 | Expression* | |
10372 | Selector_expression::lower_method_expression(Gogo* gogo) | |
10373 | { | |
10374 | source_location location = this->location(); | |
10375 | Type* type = this->left_->type(); | |
10376 | const std::string& name(this->name_); | |
10377 | ||
10378 | bool is_pointer; | |
10379 | if (type->points_to() == NULL) | |
10380 | is_pointer = false; | |
10381 | else | |
10382 | { | |
10383 | is_pointer = true; | |
10384 | type = type->points_to(); | |
10385 | } | |
10386 | Named_type* nt = type->named_type(); | |
10387 | if (nt == NULL) | |
10388 | { | |
10389 | error_at(location, | |
10390 | ("method expression requires named type or " | |
10391 | "pointer to named type")); | |
10392 | return Expression::make_error(location); | |
10393 | } | |
10394 | ||
10395 | bool is_ambiguous; | |
10396 | Method* method = nt->method_function(name, &is_ambiguous); | |
ab1468c3 | 10397 | const Typed_identifier* imethod = NULL; |
dcc8506b | 10398 | if (method == NULL && !is_pointer) |
ab1468c3 | 10399 | { |
10400 | Interface_type* it = nt->interface_type(); | |
10401 | if (it != NULL) | |
10402 | imethod = it->find_method(name); | |
10403 | } | |
10404 | ||
10405 | if (method == NULL && imethod == NULL) | |
e440a328 | 10406 | { |
10407 | if (!is_ambiguous) | |
dcc8506b | 10408 | error_at(location, "type %<%s%s%> has no method %<%s%>", |
10409 | is_pointer ? "*" : "", | |
e440a328 | 10410 | nt->message_name().c_str(), |
10411 | Gogo::message_name(name).c_str()); | |
10412 | else | |
dcc8506b | 10413 | error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>", |
e440a328 | 10414 | Gogo::message_name(name).c_str(), |
dcc8506b | 10415 | is_pointer ? "*" : "", |
e440a328 | 10416 | nt->message_name().c_str()); |
10417 | return Expression::make_error(location); | |
10418 | } | |
10419 | ||
ab1468c3 | 10420 | if (method != NULL && !is_pointer && !method->is_value_method()) |
e440a328 | 10421 | { |
10422 | error_at(location, "method requires pointer (use %<(*%s).%s)%>", | |
10423 | nt->message_name().c_str(), | |
10424 | Gogo::message_name(name).c_str()); | |
10425 | return Expression::make_error(location); | |
10426 | } | |
10427 | ||
10428 | // Build a new function type in which the receiver becomes the first | |
10429 | // argument. | |
ab1468c3 | 10430 | Function_type* method_type; |
10431 | if (method != NULL) | |
10432 | { | |
10433 | method_type = method->type(); | |
10434 | gcc_assert(method_type->is_method()); | |
10435 | } | |
10436 | else | |
10437 | { | |
10438 | method_type = imethod->type()->function_type(); | |
10439 | gcc_assert(method_type != NULL && !method_type->is_method()); | |
10440 | } | |
e440a328 | 10441 | |
10442 | const char* const receiver_name = "$this"; | |
10443 | Typed_identifier_list* parameters = new Typed_identifier_list(); | |
10444 | parameters->push_back(Typed_identifier(receiver_name, this->left_->type(), | |
10445 | location)); | |
10446 | ||
10447 | const Typed_identifier_list* method_parameters = method_type->parameters(); | |
10448 | if (method_parameters != NULL) | |
10449 | { | |
10450 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); | |
10451 | p != method_parameters->end(); | |
10452 | ++p) | |
10453 | parameters->push_back(*p); | |
10454 | } | |
10455 | ||
10456 | const Typed_identifier_list* method_results = method_type->results(); | |
10457 | Typed_identifier_list* results; | |
10458 | if (method_results == NULL) | |
10459 | results = NULL; | |
10460 | else | |
10461 | { | |
10462 | results = new Typed_identifier_list(); | |
10463 | for (Typed_identifier_list::const_iterator p = method_results->begin(); | |
10464 | p != method_results->end(); | |
10465 | ++p) | |
10466 | results->push_back(*p); | |
10467 | } | |
10468 | ||
10469 | Function_type* fntype = Type::make_function_type(NULL, parameters, results, | |
10470 | location); | |
10471 | if (method_type->is_varargs()) | |
10472 | fntype->set_is_varargs(); | |
10473 | ||
10474 | // We generate methods which always takes a pointer to the receiver | |
10475 | // as their first argument. If this is for a pointer type, we can | |
10476 | // simply reuse the existing function. We use an internal hack to | |
10477 | // get the right type. | |
10478 | ||
ab1468c3 | 10479 | if (method != NULL && is_pointer) |
e440a328 | 10480 | { |
10481 | Named_object* mno = (method->needs_stub_method() | |
10482 | ? method->stub_object() | |
10483 | : method->named_object()); | |
10484 | Expression* f = Expression::make_func_reference(mno, NULL, location); | |
10485 | f = Expression::make_cast(fntype, f, location); | |
10486 | Type_conversion_expression* tce = | |
10487 | static_cast<Type_conversion_expression*>(f); | |
10488 | tce->set_may_convert_function_types(); | |
10489 | return f; | |
10490 | } | |
10491 | ||
10492 | Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false, | |
10493 | location); | |
10494 | ||
10495 | Named_object* vno = gogo->lookup(receiver_name, NULL); | |
10496 | gcc_assert(vno != NULL); | |
10497 | Expression* ve = Expression::make_var_reference(vno, location); | |
ab1468c3 | 10498 | Expression* bm; |
10499 | if (method != NULL) | |
10500 | bm = Type::bind_field_or_method(gogo, nt, ve, name, location); | |
10501 | else | |
10502 | bm = Expression::make_interface_field_reference(ve, name, location); | |
f690b0bb | 10503 | |
10504 | // Even though we found the method above, if it has an error type we | |
10505 | // may see an error here. | |
10506 | if (bm->is_error_expression()) | |
463fe805 | 10507 | { |
10508 | gogo->finish_function(location); | |
10509 | return bm; | |
10510 | } | |
e440a328 | 10511 | |
10512 | Expression_list* args; | |
10513 | if (method_parameters == NULL) | |
10514 | args = NULL; | |
10515 | else | |
10516 | { | |
10517 | args = new Expression_list(); | |
10518 | for (Typed_identifier_list::const_iterator p = method_parameters->begin(); | |
10519 | p != method_parameters->end(); | |
10520 | ++p) | |
10521 | { | |
10522 | vno = gogo->lookup(p->name(), NULL); | |
10523 | gcc_assert(vno != NULL); | |
10524 | args->push_back(Expression::make_var_reference(vno, location)); | |
10525 | } | |
10526 | } | |
10527 | ||
10528 | Call_expression* call = Expression::make_call(bm, args, | |
10529 | method_type->is_varargs(), | |
10530 | location); | |
10531 | ||
10532 | size_t count = call->result_count(); | |
10533 | Statement* s; | |
10534 | if (count == 0) | |
10535 | s = Statement::make_statement(call); | |
10536 | else | |
10537 | { | |
10538 | Expression_list* retvals = new Expression_list(); | |
10539 | if (count <= 1) | |
10540 | retvals->push_back(call); | |
10541 | else | |
10542 | { | |
10543 | for (size_t i = 0; i < count; ++i) | |
10544 | retvals->push_back(Expression::make_call_result(call, i)); | |
10545 | } | |
be2fc38d | 10546 | s = Statement::make_return_statement(retvals, location); |
e440a328 | 10547 | } |
10548 | gogo->add_statement(s); | |
10549 | ||
10550 | gogo->finish_function(location); | |
10551 | ||
10552 | return Expression::make_func_reference(no, NULL, location); | |
10553 | } | |
10554 | ||
10555 | // Make a selector expression. | |
10556 | ||
10557 | Expression* | |
10558 | Expression::make_selector(Expression* left, const std::string& name, | |
10559 | source_location location) | |
10560 | { | |
10561 | return new Selector_expression(left, name, location); | |
10562 | } | |
10563 | ||
10564 | // Implement the builtin function new. | |
10565 | ||
10566 | class Allocation_expression : public Expression | |
10567 | { | |
10568 | public: | |
10569 | Allocation_expression(Type* type, source_location location) | |
10570 | : Expression(EXPRESSION_ALLOCATION, location), | |
10571 | type_(type) | |
10572 | { } | |
10573 | ||
10574 | protected: | |
10575 | int | |
10576 | do_traverse(Traverse* traverse) | |
10577 | { return Type::traverse(this->type_, traverse); } | |
10578 | ||
10579 | Type* | |
10580 | do_type() | |
10581 | { return Type::make_pointer_type(this->type_); } | |
10582 | ||
10583 | void | |
10584 | do_determine_type(const Type_context*) | |
10585 | { } | |
10586 | ||
10587 | void | |
10588 | do_check_types(Gogo*); | |
10589 | ||
10590 | Expression* | |
10591 | do_copy() | |
10592 | { return new Allocation_expression(this->type_, this->location()); } | |
10593 | ||
10594 | tree | |
10595 | do_get_tree(Translate_context*); | |
10596 | ||
10597 | private: | |
10598 | // The type we are allocating. | |
10599 | Type* type_; | |
10600 | }; | |
10601 | ||
10602 | // Check the type of an allocation expression. | |
10603 | ||
10604 | void | |
10605 | Allocation_expression::do_check_types(Gogo*) | |
10606 | { | |
10607 | if (this->type_->function_type() != NULL) | |
10608 | this->report_error(_("invalid new of function type")); | |
10609 | } | |
10610 | ||
10611 | // Return a tree for an allocation expression. | |
10612 | ||
10613 | tree | |
10614 | Allocation_expression::do_get_tree(Translate_context* context) | |
10615 | { | |
10616 | tree type_tree = this->type_->get_tree(context->gogo()); | |
19824ddb | 10617 | if (type_tree == error_mark_node) |
10618 | return error_mark_node; | |
e440a328 | 10619 | tree size_tree = TYPE_SIZE_UNIT(type_tree); |
10620 | tree space = context->gogo()->allocate_memory(this->type_, size_tree, | |
10621 | this->location()); | |
19824ddb | 10622 | if (space == error_mark_node) |
10623 | return error_mark_node; | |
e440a328 | 10624 | return fold_convert(build_pointer_type(type_tree), space); |
10625 | } | |
10626 | ||
10627 | // Make an allocation expression. | |
10628 | ||
10629 | Expression* | |
10630 | Expression::make_allocation(Type* type, source_location location) | |
10631 | { | |
10632 | return new Allocation_expression(type, location); | |
10633 | } | |
10634 | ||
10635 | // Implement the builtin function make. | |
10636 | ||
10637 | class Make_expression : public Expression | |
10638 | { | |
10639 | public: | |
10640 | Make_expression(Type* type, Expression_list* args, source_location location) | |
10641 | : Expression(EXPRESSION_MAKE, location), | |
10642 | type_(type), args_(args) | |
10643 | { } | |
10644 | ||
10645 | protected: | |
10646 | int | |
10647 | do_traverse(Traverse* traverse); | |
10648 | ||
10649 | Type* | |
10650 | do_type() | |
10651 | { return this->type_; } | |
10652 | ||
10653 | void | |
10654 | do_determine_type(const Type_context*); | |
10655 | ||
10656 | void | |
10657 | do_check_types(Gogo*); | |
10658 | ||
10659 | Expression* | |
10660 | do_copy() | |
10661 | { | |
10662 | return new Make_expression(this->type_, this->args_->copy(), | |
10663 | this->location()); | |
10664 | } | |
10665 | ||
10666 | tree | |
10667 | do_get_tree(Translate_context*); | |
10668 | ||
10669 | private: | |
10670 | // The type we are making. | |
10671 | Type* type_; | |
10672 | // The arguments to pass to the make routine. | |
10673 | Expression_list* args_; | |
10674 | }; | |
10675 | ||
10676 | // Traversal. | |
10677 | ||
10678 | int | |
10679 | Make_expression::do_traverse(Traverse* traverse) | |
10680 | { | |
10681 | if (this->args_ != NULL | |
10682 | && this->args_->traverse(traverse) == TRAVERSE_EXIT) | |
10683 | return TRAVERSE_EXIT; | |
10684 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10685 | return TRAVERSE_EXIT; | |
10686 | return TRAVERSE_CONTINUE; | |
10687 | } | |
10688 | ||
10689 | // Set types of arguments. | |
10690 | ||
10691 | void | |
10692 | Make_expression::do_determine_type(const Type_context*) | |
10693 | { | |
10694 | if (this->args_ != NULL) | |
10695 | { | |
10696 | Type_context context(Type::lookup_integer_type("int"), false); | |
10697 | for (Expression_list::const_iterator pe = this->args_->begin(); | |
10698 | pe != this->args_->end(); | |
10699 | ++pe) | |
10700 | (*pe)->determine_type(&context); | |
10701 | } | |
10702 | } | |
10703 | ||
10704 | // Check types for a make expression. | |
10705 | ||
10706 | void | |
10707 | Make_expression::do_check_types(Gogo*) | |
10708 | { | |
10709 | if (this->type_->channel_type() == NULL | |
10710 | && this->type_->map_type() == NULL | |
10711 | && (this->type_->array_type() == NULL | |
10712 | || this->type_->array_type()->length() != NULL)) | |
10713 | this->report_error(_("invalid type for make function")); | |
10714 | else if (!this->type_->check_make_expression(this->args_, this->location())) | |
10715 | this->set_is_error(); | |
10716 | } | |
10717 | ||
10718 | // Return a tree for a make expression. | |
10719 | ||
10720 | tree | |
10721 | Make_expression::do_get_tree(Translate_context* context) | |
10722 | { | |
10723 | return this->type_->make_expression_tree(context, this->args_, | |
10724 | this->location()); | |
10725 | } | |
10726 | ||
10727 | // Make a make expression. | |
10728 | ||
10729 | Expression* | |
10730 | Expression::make_make(Type* type, Expression_list* args, | |
10731 | source_location location) | |
10732 | { | |
10733 | return new Make_expression(type, args, location); | |
10734 | } | |
10735 | ||
10736 | // Construct a struct. | |
10737 | ||
10738 | class Struct_construction_expression : public Expression | |
10739 | { | |
10740 | public: | |
10741 | Struct_construction_expression(Type* type, Expression_list* vals, | |
10742 | source_location location) | |
10743 | : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location), | |
10744 | type_(type), vals_(vals) | |
10745 | { } | |
10746 | ||
10747 | // Return whether this is a constant initializer. | |
10748 | bool | |
10749 | is_constant_struct() const; | |
10750 | ||
10751 | protected: | |
10752 | int | |
10753 | do_traverse(Traverse* traverse); | |
10754 | ||
10755 | Type* | |
10756 | do_type() | |
10757 | { return this->type_; } | |
10758 | ||
10759 | void | |
10760 | do_determine_type(const Type_context*); | |
10761 | ||
10762 | void | |
10763 | do_check_types(Gogo*); | |
10764 | ||
10765 | Expression* | |
10766 | do_copy() | |
10767 | { | |
10768 | return new Struct_construction_expression(this->type_, this->vals_->copy(), | |
10769 | this->location()); | |
10770 | } | |
10771 | ||
10772 | bool | |
10773 | do_is_addressable() const | |
10774 | { return true; } | |
10775 | ||
10776 | tree | |
10777 | do_get_tree(Translate_context*); | |
10778 | ||
10779 | void | |
10780 | do_export(Export*) const; | |
10781 | ||
10782 | private: | |
10783 | // The type of the struct to construct. | |
10784 | Type* type_; | |
10785 | // The list of values, in order of the fields in the struct. A NULL | |
10786 | // entry means that the field should be zero-initialized. | |
10787 | Expression_list* vals_; | |
10788 | }; | |
10789 | ||
10790 | // Traversal. | |
10791 | ||
10792 | int | |
10793 | Struct_construction_expression::do_traverse(Traverse* traverse) | |
10794 | { | |
10795 | if (this->vals_ != NULL | |
10796 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
10797 | return TRAVERSE_EXIT; | |
10798 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
10799 | return TRAVERSE_EXIT; | |
10800 | return TRAVERSE_CONTINUE; | |
10801 | } | |
10802 | ||
10803 | // Return whether this is a constant initializer. | |
10804 | ||
10805 | bool | |
10806 | Struct_construction_expression::is_constant_struct() const | |
10807 | { | |
10808 | if (this->vals_ == NULL) | |
10809 | return true; | |
10810 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10811 | pv != this->vals_->end(); | |
10812 | ++pv) | |
10813 | { | |
10814 | if (*pv != NULL | |
10815 | && !(*pv)->is_constant() | |
10816 | && (!(*pv)->is_composite_literal() | |
10817 | || (*pv)->is_nonconstant_composite_literal())) | |
10818 | return false; | |
10819 | } | |
10820 | ||
10821 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10822 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10823 | pf != fields->end(); | |
10824 | ++pf) | |
10825 | { | |
10826 | // There are no constant constructors for interfaces. | |
10827 | if (pf->type()->interface_type() != NULL) | |
10828 | return false; | |
10829 | } | |
10830 | ||
10831 | return true; | |
10832 | } | |
10833 | ||
10834 | // Final type determination. | |
10835 | ||
10836 | void | |
10837 | Struct_construction_expression::do_determine_type(const Type_context*) | |
10838 | { | |
10839 | if (this->vals_ == NULL) | |
10840 | return; | |
10841 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10842 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10843 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10844 | pf != fields->end(); | |
10845 | ++pf, ++pv) | |
10846 | { | |
10847 | if (pv == this->vals_->end()) | |
10848 | return; | |
10849 | if (*pv != NULL) | |
10850 | { | |
10851 | Type_context subcontext(pf->type(), false); | |
10852 | (*pv)->determine_type(&subcontext); | |
10853 | } | |
10854 | } | |
a6cb4c0e | 10855 | // Extra values are an error we will report elsewhere; we still want |
10856 | // to determine the type to avoid knockon errors. | |
10857 | for (; pv != this->vals_->end(); ++pv) | |
10858 | (*pv)->determine_type_no_context(); | |
e440a328 | 10859 | } |
10860 | ||
10861 | // Check types. | |
10862 | ||
10863 | void | |
10864 | Struct_construction_expression::do_check_types(Gogo*) | |
10865 | { | |
10866 | if (this->vals_ == NULL) | |
10867 | return; | |
10868 | ||
10869 | Struct_type* st = this->type_->struct_type(); | |
10870 | if (this->vals_->size() > st->field_count()) | |
10871 | { | |
10872 | this->report_error(_("too many expressions for struct")); | |
10873 | return; | |
10874 | } | |
10875 | ||
10876 | const Struct_field_list* fields = st->fields(); | |
10877 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10878 | int i = 0; | |
10879 | for (Struct_field_list::const_iterator pf = fields->begin(); | |
10880 | pf != fields->end(); | |
10881 | ++pf, ++pv, ++i) | |
10882 | { | |
10883 | if (pv == this->vals_->end()) | |
10884 | { | |
10885 | this->report_error(_("too few expressions for struct")); | |
10886 | break; | |
10887 | } | |
10888 | ||
10889 | if (*pv == NULL) | |
10890 | continue; | |
10891 | ||
10892 | std::string reason; | |
10893 | if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason)) | |
10894 | { | |
10895 | if (reason.empty()) | |
10896 | error_at((*pv)->location(), | |
10897 | "incompatible type for field %d in struct construction", | |
10898 | i + 1); | |
10899 | else | |
10900 | error_at((*pv)->location(), | |
10901 | ("incompatible type for field %d in " | |
10902 | "struct construction (%s)"), | |
10903 | i + 1, reason.c_str()); | |
10904 | this->set_is_error(); | |
10905 | } | |
10906 | } | |
10907 | gcc_assert(pv == this->vals_->end()); | |
10908 | } | |
10909 | ||
10910 | // Return a tree for constructing a struct. | |
10911 | ||
10912 | tree | |
10913 | Struct_construction_expression::do_get_tree(Translate_context* context) | |
10914 | { | |
10915 | Gogo* gogo = context->gogo(); | |
10916 | ||
10917 | if (this->vals_ == NULL) | |
10918 | return this->type_->get_init_tree(gogo, false); | |
10919 | ||
10920 | tree type_tree = this->type_->get_tree(gogo); | |
10921 | if (type_tree == error_mark_node) | |
10922 | return error_mark_node; | |
10923 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); | |
10924 | ||
10925 | bool is_constant = true; | |
10926 | const Struct_field_list* fields = this->type_->struct_type()->fields(); | |
10927 | VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc, | |
10928 | fields->size()); | |
10929 | Struct_field_list::const_iterator pf = fields->begin(); | |
10930 | Expression_list::const_iterator pv = this->vals_->begin(); | |
10931 | for (tree field = TYPE_FIELDS(type_tree); | |
10932 | field != NULL_TREE; | |
10933 | field = DECL_CHAIN(field), ++pf) | |
10934 | { | |
10935 | gcc_assert(pf != fields->end()); | |
10936 | ||
10937 | tree val; | |
10938 | if (pv == this->vals_->end()) | |
10939 | val = pf->type()->get_init_tree(gogo, false); | |
10940 | else if (*pv == NULL) | |
10941 | { | |
10942 | val = pf->type()->get_init_tree(gogo, false); | |
10943 | ++pv; | |
10944 | } | |
10945 | else | |
10946 | { | |
10947 | val = Expression::convert_for_assignment(context, pf->type(), | |
10948 | (*pv)->type(), | |
10949 | (*pv)->get_tree(context), | |
10950 | this->location()); | |
10951 | ++pv; | |
10952 | } | |
10953 | ||
10954 | if (val == error_mark_node || TREE_TYPE(val) == error_mark_node) | |
10955 | return error_mark_node; | |
10956 | ||
10957 | constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL); | |
10958 | elt->index = field; | |
10959 | elt->value = val; | |
10960 | if (!TREE_CONSTANT(val)) | |
10961 | is_constant = false; | |
10962 | } | |
10963 | gcc_assert(pf == fields->end()); | |
10964 | ||
10965 | tree ret = build_constructor(type_tree, elts); | |
10966 | if (is_constant) | |
10967 | TREE_CONSTANT(ret) = 1; | |
10968 | return ret; | |
10969 | } | |
10970 | ||
10971 | // Export a struct construction. | |
10972 | ||
10973 | void | |
10974 | Struct_construction_expression::do_export(Export* exp) const | |
10975 | { | |
10976 | exp->write_c_string("convert("); | |
10977 | exp->write_type(this->type_); | |
10978 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
10979 | pv != this->vals_->end(); | |
10980 | ++pv) | |
10981 | { | |
10982 | exp->write_c_string(", "); | |
10983 | if (*pv != NULL) | |
10984 | (*pv)->export_expression(exp); | |
10985 | } | |
10986 | exp->write_c_string(")"); | |
10987 | } | |
10988 | ||
10989 | // Make a struct composite literal. This used by the thunk code. | |
10990 | ||
10991 | Expression* | |
10992 | Expression::make_struct_composite_literal(Type* type, Expression_list* vals, | |
10993 | source_location location) | |
10994 | { | |
10995 | gcc_assert(type->struct_type() != NULL); | |
10996 | return new Struct_construction_expression(type, vals, location); | |
10997 | } | |
10998 | ||
10999 | // Construct an array. This class is not used directly; instead we | |
11000 | // use the child classes, Fixed_array_construction_expression and | |
11001 | // Open_array_construction_expression. | |
11002 | ||
11003 | class Array_construction_expression : public Expression | |
11004 | { | |
11005 | protected: | |
11006 | Array_construction_expression(Expression_classification classification, | |
11007 | Type* type, Expression_list* vals, | |
11008 | source_location location) | |
11009 | : Expression(classification, location), | |
11010 | type_(type), vals_(vals) | |
11011 | { } | |
11012 | ||
11013 | public: | |
11014 | // Return whether this is a constant initializer. | |
11015 | bool | |
11016 | is_constant_array() const; | |
11017 | ||
11018 | // Return the number of elements. | |
11019 | size_t | |
11020 | element_count() const | |
11021 | { return this->vals_ == NULL ? 0 : this->vals_->size(); } | |
11022 | ||
11023 | protected: | |
11024 | int | |
11025 | do_traverse(Traverse* traverse); | |
11026 | ||
11027 | Type* | |
11028 | do_type() | |
11029 | { return this->type_; } | |
11030 | ||
11031 | void | |
11032 | do_determine_type(const Type_context*); | |
11033 | ||
11034 | void | |
11035 | do_check_types(Gogo*); | |
11036 | ||
11037 | bool | |
11038 | do_is_addressable() const | |
11039 | { return true; } | |
11040 | ||
11041 | void | |
11042 | do_export(Export*) const; | |
11043 | ||
11044 | // The list of values. | |
11045 | Expression_list* | |
11046 | vals() | |
11047 | { return this->vals_; } | |
11048 | ||
11049 | // Get a constructor tree for the array values. | |
11050 | tree | |
11051 | get_constructor_tree(Translate_context* context, tree type_tree); | |
11052 | ||
11053 | private: | |
11054 | // The type of the array to construct. | |
11055 | Type* type_; | |
11056 | // The list of values. | |
11057 | Expression_list* vals_; | |
11058 | }; | |
11059 | ||
11060 | // Traversal. | |
11061 | ||
11062 | int | |
11063 | Array_construction_expression::do_traverse(Traverse* traverse) | |
11064 | { | |
11065 | if (this->vals_ != NULL | |
11066 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11067 | return TRAVERSE_EXIT; | |
11068 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
11069 | return TRAVERSE_EXIT; | |
11070 | return TRAVERSE_CONTINUE; | |
11071 | } | |
11072 | ||
11073 | // Return whether this is a constant initializer. | |
11074 | ||
11075 | bool | |
11076 | Array_construction_expression::is_constant_array() const | |
11077 | { | |
11078 | if (this->vals_ == NULL) | |
11079 | return true; | |
11080 | ||
11081 | // There are no constant constructors for interfaces. | |
11082 | if (this->type_->array_type()->element_type()->interface_type() != NULL) | |
11083 | return false; | |
11084 | ||
11085 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11086 | pv != this->vals_->end(); | |
11087 | ++pv) | |
11088 | { | |
11089 | if (*pv != NULL | |
11090 | && !(*pv)->is_constant() | |
11091 | && (!(*pv)->is_composite_literal() | |
11092 | || (*pv)->is_nonconstant_composite_literal())) | |
11093 | return false; | |
11094 | } | |
11095 | return true; | |
11096 | } | |
11097 | ||
11098 | // Final type determination. | |
11099 | ||
11100 | void | |
11101 | Array_construction_expression::do_determine_type(const Type_context*) | |
11102 | { | |
11103 | if (this->vals_ == NULL) | |
11104 | return; | |
11105 | Type_context subcontext(this->type_->array_type()->element_type(), false); | |
11106 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11107 | pv != this->vals_->end(); | |
11108 | ++pv) | |
11109 | { | |
11110 | if (*pv != NULL) | |
11111 | (*pv)->determine_type(&subcontext); | |
11112 | } | |
11113 | } | |
11114 | ||
11115 | // Check types. | |
11116 | ||
11117 | void | |
11118 | Array_construction_expression::do_check_types(Gogo*) | |
11119 | { | |
11120 | if (this->vals_ == NULL) | |
11121 | return; | |
11122 | ||
11123 | Array_type* at = this->type_->array_type(); | |
11124 | int i = 0; | |
11125 | Type* element_type = at->element_type(); | |
11126 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11127 | pv != this->vals_->end(); | |
11128 | ++pv, ++i) | |
11129 | { | |
11130 | if (*pv != NULL | |
11131 | && !Type::are_assignable(element_type, (*pv)->type(), NULL)) | |
11132 | { | |
11133 | error_at((*pv)->location(), | |
11134 | "incompatible type for element %d in composite literal", | |
11135 | i + 1); | |
11136 | this->set_is_error(); | |
11137 | } | |
11138 | } | |
11139 | ||
11140 | Expression* length = at->length(); | |
11141 | if (length != NULL) | |
11142 | { | |
11143 | mpz_t val; | |
11144 | mpz_init(val); | |
11145 | Type* type; | |
11146 | if (at->length()->integer_constant_value(true, val, &type)) | |
11147 | { | |
11148 | if (this->vals_->size() > mpz_get_ui(val)) | |
11149 | this->report_error(_("too many elements in composite literal")); | |
11150 | } | |
11151 | mpz_clear(val); | |
11152 | } | |
11153 | } | |
11154 | ||
11155 | // Get a constructor tree for the array values. | |
11156 | ||
11157 | tree | |
11158 | Array_construction_expression::get_constructor_tree(Translate_context* context, | |
11159 | tree type_tree) | |
11160 | { | |
11161 | VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc, | |
11162 | (this->vals_ == NULL | |
11163 | ? 0 | |
11164 | : this->vals_->size())); | |
11165 | Type* element_type = this->type_->array_type()->element_type(); | |
11166 | bool is_constant = true; | |
11167 | if (this->vals_ != NULL) | |
11168 | { | |
11169 | size_t i = 0; | |
11170 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11171 | pv != this->vals_->end(); | |
11172 | ++pv, ++i) | |
11173 | { | |
11174 | constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL); | |
11175 | elt->index = size_int(i); | |
11176 | if (*pv == NULL) | |
11177 | elt->value = element_type->get_init_tree(context->gogo(), false); | |
11178 | else | |
11179 | { | |
11180 | tree value_tree = (*pv)->get_tree(context); | |
11181 | elt->value = Expression::convert_for_assignment(context, | |
11182 | element_type, | |
11183 | (*pv)->type(), | |
11184 | value_tree, | |
11185 | this->location()); | |
11186 | } | |
11187 | if (elt->value == error_mark_node) | |
11188 | return error_mark_node; | |
11189 | if (!TREE_CONSTANT(elt->value)) | |
11190 | is_constant = false; | |
11191 | } | |
11192 | } | |
11193 | ||
11194 | tree ret = build_constructor(type_tree, values); | |
11195 | if (is_constant) | |
11196 | TREE_CONSTANT(ret) = 1; | |
11197 | return ret; | |
11198 | } | |
11199 | ||
11200 | // Export an array construction. | |
11201 | ||
11202 | void | |
11203 | Array_construction_expression::do_export(Export* exp) const | |
11204 | { | |
11205 | exp->write_c_string("convert("); | |
11206 | exp->write_type(this->type_); | |
11207 | if (this->vals_ != NULL) | |
11208 | { | |
11209 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11210 | pv != this->vals_->end(); | |
11211 | ++pv) | |
11212 | { | |
11213 | exp->write_c_string(", "); | |
11214 | if (*pv != NULL) | |
11215 | (*pv)->export_expression(exp); | |
11216 | } | |
11217 | } | |
11218 | exp->write_c_string(")"); | |
11219 | } | |
11220 | ||
11221 | // Construct a fixed array. | |
11222 | ||
11223 | class Fixed_array_construction_expression : | |
11224 | public Array_construction_expression | |
11225 | { | |
11226 | public: | |
11227 | Fixed_array_construction_expression(Type* type, Expression_list* vals, | |
11228 | source_location location) | |
11229 | : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION, | |
11230 | type, vals, location) | |
11231 | { | |
11232 | gcc_assert(type->array_type() != NULL | |
11233 | && type->array_type()->length() != NULL); | |
11234 | } | |
11235 | ||
11236 | protected: | |
11237 | Expression* | |
11238 | do_copy() | |
11239 | { | |
11240 | return new Fixed_array_construction_expression(this->type(), | |
11241 | (this->vals() == NULL | |
11242 | ? NULL | |
11243 | : this->vals()->copy()), | |
11244 | this->location()); | |
11245 | } | |
11246 | ||
11247 | tree | |
11248 | do_get_tree(Translate_context*); | |
11249 | }; | |
11250 | ||
11251 | // Return a tree for constructing a fixed array. | |
11252 | ||
11253 | tree | |
11254 | Fixed_array_construction_expression::do_get_tree(Translate_context* context) | |
11255 | { | |
11256 | return this->get_constructor_tree(context, | |
11257 | this->type()->get_tree(context->gogo())); | |
11258 | } | |
11259 | ||
11260 | // Construct an open array. | |
11261 | ||
11262 | class Open_array_construction_expression : public Array_construction_expression | |
11263 | { | |
11264 | public: | |
11265 | Open_array_construction_expression(Type* type, Expression_list* vals, | |
11266 | source_location location) | |
11267 | : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION, | |
11268 | type, vals, location) | |
11269 | { | |
11270 | gcc_assert(type->array_type() != NULL | |
11271 | && type->array_type()->length() == NULL); | |
11272 | } | |
11273 | ||
11274 | protected: | |
11275 | // Note that taking the address of an open array literal is invalid. | |
11276 | ||
11277 | Expression* | |
11278 | do_copy() | |
11279 | { | |
11280 | return new Open_array_construction_expression(this->type(), | |
11281 | (this->vals() == NULL | |
11282 | ? NULL | |
11283 | : this->vals()->copy()), | |
11284 | this->location()); | |
11285 | } | |
11286 | ||
11287 | tree | |
11288 | do_get_tree(Translate_context*); | |
11289 | }; | |
11290 | ||
11291 | // Return a tree for constructing an open array. | |
11292 | ||
11293 | tree | |
11294 | Open_array_construction_expression::do_get_tree(Translate_context* context) | |
11295 | { | |
f9c68f17 | 11296 | Array_type* array_type = this->type()->array_type(); |
11297 | if (array_type == NULL) | |
11298 | { | |
5c13bd80 | 11299 | gcc_assert(this->type()->is_error()); |
f9c68f17 | 11300 | return error_mark_node; |
11301 | } | |
11302 | ||
11303 | Type* element_type = array_type->element_type(); | |
e440a328 | 11304 | tree element_type_tree = element_type->get_tree(context->gogo()); |
3d60812e | 11305 | if (element_type_tree == error_mark_node) |
11306 | return error_mark_node; | |
11307 | ||
e440a328 | 11308 | tree values; |
11309 | tree length_tree; | |
11310 | if (this->vals() == NULL || this->vals()->empty()) | |
11311 | { | |
11312 | // We need to create a unique value. | |
11313 | tree max = size_int(0); | |
11314 | tree constructor_type = build_array_type(element_type_tree, | |
11315 | build_index_type(max)); | |
11316 | if (constructor_type == error_mark_node) | |
11317 | return error_mark_node; | |
11318 | VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1); | |
11319 | constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL); | |
11320 | elt->index = size_int(0); | |
11321 | elt->value = element_type->get_init_tree(context->gogo(), false); | |
11322 | values = build_constructor(constructor_type, vec); | |
11323 | if (TREE_CONSTANT(elt->value)) | |
11324 | TREE_CONSTANT(values) = 1; | |
11325 | length_tree = size_int(0); | |
11326 | } | |
11327 | else | |
11328 | { | |
11329 | tree max = size_int(this->vals()->size() - 1); | |
11330 | tree constructor_type = build_array_type(element_type_tree, | |
11331 | build_index_type(max)); | |
11332 | if (constructor_type == error_mark_node) | |
11333 | return error_mark_node; | |
11334 | values = this->get_constructor_tree(context, constructor_type); | |
11335 | length_tree = size_int(this->vals()->size()); | |
11336 | } | |
11337 | ||
11338 | if (values == error_mark_node) | |
11339 | return error_mark_node; | |
11340 | ||
11341 | bool is_constant_initializer = TREE_CONSTANT(values); | |
d8829beb | 11342 | |
11343 | // We have to copy the initial values into heap memory if we are in | |
11344 | // a function or if the values are not constants. We also have to | |
11345 | // copy them if they may contain pointers in a non-constant context, | |
11346 | // as otherwise the garbage collector won't see them. | |
11347 | bool copy_to_heap = (context->function() != NULL | |
11348 | || !is_constant_initializer | |
11349 | || (element_type->has_pointer() | |
11350 | && !context->is_const())); | |
e440a328 | 11351 | |
11352 | if (is_constant_initializer) | |
11353 | { | |
11354 | tree tmp = build_decl(this->location(), VAR_DECL, | |
11355 | create_tmp_var_name("C"), TREE_TYPE(values)); | |
11356 | DECL_EXTERNAL(tmp) = 0; | |
11357 | TREE_PUBLIC(tmp) = 0; | |
11358 | TREE_STATIC(tmp) = 1; | |
11359 | DECL_ARTIFICIAL(tmp) = 1; | |
d8829beb | 11360 | if (copy_to_heap) |
e440a328 | 11361 | { |
d8829beb | 11362 | // If we are not copying the value to the heap, we will only |
11363 | // initialize the value once, so we can use this directly | |
11364 | // rather than copying it. In that case we can't make it | |
11365 | // read-only, because the program is permitted to change it. | |
e440a328 | 11366 | TREE_READONLY(tmp) = 1; |
11367 | TREE_CONSTANT(tmp) = 1; | |
11368 | } | |
11369 | DECL_INITIAL(tmp) = values; | |
11370 | rest_of_decl_compilation(tmp, 1, 0); | |
11371 | values = tmp; | |
11372 | } | |
11373 | ||
11374 | tree space; | |
11375 | tree set; | |
d8829beb | 11376 | if (!copy_to_heap) |
e440a328 | 11377 | { |
d8829beb | 11378 | // the initializer will only run once. |
e440a328 | 11379 | space = build_fold_addr_expr(values); |
11380 | set = NULL_TREE; | |
11381 | } | |
11382 | else | |
11383 | { | |
11384 | tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values)); | |
11385 | space = context->gogo()->allocate_memory(element_type, memsize, | |
11386 | this->location()); | |
11387 | space = save_expr(space); | |
11388 | ||
11389 | tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space); | |
11390 | tree ref = build_fold_indirect_ref_loc(this->location(), s); | |
11391 | TREE_THIS_NOTRAP(ref) = 1; | |
11392 | set = build2(MODIFY_EXPR, void_type_node, ref, values); | |
11393 | } | |
11394 | ||
11395 | // Build a constructor for the open array. | |
11396 | ||
11397 | tree type_tree = this->type()->get_tree(context->gogo()); | |
3d60812e | 11398 | if (type_tree == error_mark_node) |
11399 | return error_mark_node; | |
e440a328 | 11400 | gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE); |
11401 | ||
11402 | VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3); | |
11403 | ||
11404 | constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL); | |
11405 | tree field = TYPE_FIELDS(type_tree); | |
11406 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0); | |
11407 | elt->index = field; | |
11408 | elt->value = fold_convert(TREE_TYPE(field), space); | |
11409 | ||
11410 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
11411 | field = DECL_CHAIN(field); | |
11412 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0); | |
11413 | elt->index = field; | |
11414 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
11415 | ||
11416 | elt = VEC_quick_push(constructor_elt, init, NULL); | |
11417 | field = DECL_CHAIN(field); | |
11418 | gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0); | |
11419 | elt->index = field; | |
11420 | elt->value = fold_convert(TREE_TYPE(field), length_tree); | |
11421 | ||
11422 | tree constructor = build_constructor(type_tree, init); | |
3d60812e | 11423 | if (constructor == error_mark_node) |
11424 | return error_mark_node; | |
d8829beb | 11425 | if (!copy_to_heap) |
e440a328 | 11426 | TREE_CONSTANT(constructor) = 1; |
11427 | ||
11428 | if (set == NULL_TREE) | |
11429 | return constructor; | |
11430 | else | |
11431 | return build2(COMPOUND_EXPR, type_tree, set, constructor); | |
11432 | } | |
11433 | ||
11434 | // Make a slice composite literal. This is used by the type | |
11435 | // descriptor code. | |
11436 | ||
11437 | Expression* | |
11438 | Expression::make_slice_composite_literal(Type* type, Expression_list* vals, | |
11439 | source_location location) | |
11440 | { | |
11441 | gcc_assert(type->is_open_array_type()); | |
11442 | return new Open_array_construction_expression(type, vals, location); | |
11443 | } | |
11444 | ||
11445 | // Construct a map. | |
11446 | ||
11447 | class Map_construction_expression : public Expression | |
11448 | { | |
11449 | public: | |
11450 | Map_construction_expression(Type* type, Expression_list* vals, | |
11451 | source_location location) | |
11452 | : Expression(EXPRESSION_MAP_CONSTRUCTION, location), | |
11453 | type_(type), vals_(vals) | |
11454 | { gcc_assert(vals == NULL || vals->size() % 2 == 0); } | |
11455 | ||
11456 | protected: | |
11457 | int | |
11458 | do_traverse(Traverse* traverse); | |
11459 | ||
11460 | Type* | |
11461 | do_type() | |
11462 | { return this->type_; } | |
11463 | ||
11464 | void | |
11465 | do_determine_type(const Type_context*); | |
11466 | ||
11467 | void | |
11468 | do_check_types(Gogo*); | |
11469 | ||
11470 | Expression* | |
11471 | do_copy() | |
11472 | { | |
11473 | return new Map_construction_expression(this->type_, this->vals_->copy(), | |
11474 | this->location()); | |
11475 | } | |
11476 | ||
11477 | tree | |
11478 | do_get_tree(Translate_context*); | |
11479 | ||
11480 | void | |
11481 | do_export(Export*) const; | |
11482 | ||
11483 | private: | |
11484 | // The type of the map to construct. | |
11485 | Type* type_; | |
11486 | // The list of values. | |
11487 | Expression_list* vals_; | |
11488 | }; | |
11489 | ||
11490 | // Traversal. | |
11491 | ||
11492 | int | |
11493 | Map_construction_expression::do_traverse(Traverse* traverse) | |
11494 | { | |
11495 | if (this->vals_ != NULL | |
11496 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11497 | return TRAVERSE_EXIT; | |
11498 | if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
11499 | return TRAVERSE_EXIT; | |
11500 | return TRAVERSE_CONTINUE; | |
11501 | } | |
11502 | ||
11503 | // Final type determination. | |
11504 | ||
11505 | void | |
11506 | Map_construction_expression::do_determine_type(const Type_context*) | |
11507 | { | |
11508 | if (this->vals_ == NULL) | |
11509 | return; | |
11510 | ||
11511 | Map_type* mt = this->type_->map_type(); | |
11512 | Type_context key_context(mt->key_type(), false); | |
11513 | Type_context val_context(mt->val_type(), false); | |
11514 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11515 | pv != this->vals_->end(); | |
11516 | ++pv) | |
11517 | { | |
11518 | (*pv)->determine_type(&key_context); | |
11519 | ++pv; | |
11520 | (*pv)->determine_type(&val_context); | |
11521 | } | |
11522 | } | |
11523 | ||
11524 | // Check types. | |
11525 | ||
11526 | void | |
11527 | Map_construction_expression::do_check_types(Gogo*) | |
11528 | { | |
11529 | if (this->vals_ == NULL) | |
11530 | return; | |
11531 | ||
11532 | Map_type* mt = this->type_->map_type(); | |
11533 | int i = 0; | |
11534 | Type* key_type = mt->key_type(); | |
11535 | Type* val_type = mt->val_type(); | |
11536 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11537 | pv != this->vals_->end(); | |
11538 | ++pv, ++i) | |
11539 | { | |
11540 | if (!Type::are_assignable(key_type, (*pv)->type(), NULL)) | |
11541 | { | |
11542 | error_at((*pv)->location(), | |
11543 | "incompatible type for element %d key in map construction", | |
11544 | i + 1); | |
11545 | this->set_is_error(); | |
11546 | } | |
11547 | ++pv; | |
11548 | if (!Type::are_assignable(val_type, (*pv)->type(), NULL)) | |
11549 | { | |
11550 | error_at((*pv)->location(), | |
11551 | ("incompatible type for element %d value " | |
11552 | "in map construction"), | |
11553 | i + 1); | |
11554 | this->set_is_error(); | |
11555 | } | |
11556 | } | |
11557 | } | |
11558 | ||
11559 | // Return a tree for constructing a map. | |
11560 | ||
11561 | tree | |
11562 | Map_construction_expression::do_get_tree(Translate_context* context) | |
11563 | { | |
11564 | Gogo* gogo = context->gogo(); | |
11565 | source_location loc = this->location(); | |
11566 | ||
11567 | Map_type* mt = this->type_->map_type(); | |
11568 | ||
11569 | // Build a struct to hold the key and value. | |
11570 | tree struct_type = make_node(RECORD_TYPE); | |
11571 | ||
11572 | Type* key_type = mt->key_type(); | |
11573 | tree id = get_identifier("__key"); | |
5845bde6 | 11574 | tree key_type_tree = key_type->get_tree(gogo); |
11575 | if (key_type_tree == error_mark_node) | |
11576 | return error_mark_node; | |
11577 | tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree); | |
e440a328 | 11578 | DECL_CONTEXT(key_field) = struct_type; |
11579 | TYPE_FIELDS(struct_type) = key_field; | |
11580 | ||
11581 | Type* val_type = mt->val_type(); | |
11582 | id = get_identifier("__val"); | |
5845bde6 | 11583 | tree val_type_tree = val_type->get_tree(gogo); |
11584 | if (val_type_tree == error_mark_node) | |
11585 | return error_mark_node; | |
11586 | tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree); | |
e440a328 | 11587 | DECL_CONTEXT(val_field) = struct_type; |
11588 | DECL_CHAIN(key_field) = val_field; | |
11589 | ||
11590 | layout_type(struct_type); | |
11591 | ||
11592 | bool is_constant = true; | |
11593 | size_t i = 0; | |
11594 | tree valaddr; | |
11595 | tree make_tmp; | |
11596 | ||
11597 | if (this->vals_ == NULL || this->vals_->empty()) | |
11598 | { | |
11599 | valaddr = null_pointer_node; | |
11600 | make_tmp = NULL_TREE; | |
11601 | } | |
11602 | else | |
11603 | { | |
11604 | VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc, | |
11605 | this->vals_->size() / 2); | |
11606 | ||
11607 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11608 | pv != this->vals_->end(); | |
11609 | ++pv, ++i) | |
11610 | { | |
11611 | bool one_is_constant = true; | |
11612 | ||
11613 | VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2); | |
11614 | ||
11615 | constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL); | |
11616 | elt->index = key_field; | |
11617 | tree val_tree = (*pv)->get_tree(context); | |
11618 | elt->value = Expression::convert_for_assignment(context, key_type, | |
11619 | (*pv)->type(), | |
11620 | val_tree, loc); | |
11621 | if (elt->value == error_mark_node) | |
11622 | return error_mark_node; | |
11623 | if (!TREE_CONSTANT(elt->value)) | |
11624 | one_is_constant = false; | |
11625 | ||
11626 | ++pv; | |
11627 | ||
11628 | elt = VEC_quick_push(constructor_elt, one, NULL); | |
11629 | elt->index = val_field; | |
11630 | val_tree = (*pv)->get_tree(context); | |
11631 | elt->value = Expression::convert_for_assignment(context, val_type, | |
11632 | (*pv)->type(), | |
11633 | val_tree, loc); | |
11634 | if (elt->value == error_mark_node) | |
11635 | return error_mark_node; | |
11636 | if (!TREE_CONSTANT(elt->value)) | |
11637 | one_is_constant = false; | |
11638 | ||
11639 | elt = VEC_quick_push(constructor_elt, values, NULL); | |
11640 | elt->index = size_int(i); | |
11641 | elt->value = build_constructor(struct_type, one); | |
11642 | if (one_is_constant) | |
11643 | TREE_CONSTANT(elt->value) = 1; | |
11644 | else | |
11645 | is_constant = false; | |
11646 | } | |
11647 | ||
11648 | tree index_type = build_index_type(size_int(i - 1)); | |
11649 | tree array_type = build_array_type(struct_type, index_type); | |
11650 | tree init = build_constructor(array_type, values); | |
11651 | if (is_constant) | |
11652 | TREE_CONSTANT(init) = 1; | |
11653 | tree tmp; | |
11654 | if (current_function_decl != NULL) | |
11655 | { | |
11656 | tmp = create_tmp_var(array_type, get_name(array_type)); | |
11657 | DECL_INITIAL(tmp) = init; | |
11658 | make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp); | |
11659 | TREE_ADDRESSABLE(tmp) = 1; | |
11660 | } | |
11661 | else | |
11662 | { | |
11663 | tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type); | |
11664 | DECL_EXTERNAL(tmp) = 0; | |
11665 | TREE_PUBLIC(tmp) = 0; | |
11666 | TREE_STATIC(tmp) = 1; | |
11667 | DECL_ARTIFICIAL(tmp) = 1; | |
11668 | if (!TREE_CONSTANT(init)) | |
11669 | make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp, | |
11670 | init); | |
11671 | else | |
11672 | { | |
11673 | TREE_READONLY(tmp) = 1; | |
11674 | TREE_CONSTANT(tmp) = 1; | |
11675 | DECL_INITIAL(tmp) = init; | |
11676 | make_tmp = NULL_TREE; | |
11677 | } | |
11678 | rest_of_decl_compilation(tmp, 1, 0); | |
11679 | } | |
11680 | ||
11681 | valaddr = build_fold_addr_expr(tmp); | |
11682 | } | |
11683 | ||
11684 | tree descriptor = gogo->map_descriptor(mt); | |
11685 | ||
11686 | tree type_tree = this->type_->get_tree(gogo); | |
5845bde6 | 11687 | if (type_tree == error_mark_node) |
11688 | return error_mark_node; | |
e440a328 | 11689 | |
11690 | static tree construct_map_fndecl; | |
11691 | tree call = Gogo::call_builtin(&construct_map_fndecl, | |
11692 | loc, | |
11693 | "__go_construct_map", | |
11694 | 6, | |
11695 | type_tree, | |
11696 | TREE_TYPE(descriptor), | |
11697 | descriptor, | |
11698 | sizetype, | |
11699 | size_int(i), | |
11700 | sizetype, | |
11701 | TYPE_SIZE_UNIT(struct_type), | |
11702 | sizetype, | |
11703 | byte_position(val_field), | |
11704 | sizetype, | |
11705 | TYPE_SIZE_UNIT(TREE_TYPE(val_field)), | |
11706 | const_ptr_type_node, | |
11707 | fold_convert(const_ptr_type_node, valaddr)); | |
5fb82b5e | 11708 | if (call == error_mark_node) |
11709 | return error_mark_node; | |
e440a328 | 11710 | |
11711 | tree ret; | |
11712 | if (make_tmp == NULL) | |
11713 | ret = call; | |
11714 | else | |
11715 | ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call); | |
11716 | return ret; | |
11717 | } | |
11718 | ||
11719 | // Export an array construction. | |
11720 | ||
11721 | void | |
11722 | Map_construction_expression::do_export(Export* exp) const | |
11723 | { | |
11724 | exp->write_c_string("convert("); | |
11725 | exp->write_type(this->type_); | |
11726 | for (Expression_list::const_iterator pv = this->vals_->begin(); | |
11727 | pv != this->vals_->end(); | |
11728 | ++pv) | |
11729 | { | |
11730 | exp->write_c_string(", "); | |
11731 | (*pv)->export_expression(exp); | |
11732 | } | |
11733 | exp->write_c_string(")"); | |
11734 | } | |
11735 | ||
11736 | // A general composite literal. This is lowered to a type specific | |
11737 | // version. | |
11738 | ||
11739 | class Composite_literal_expression : public Parser_expression | |
11740 | { | |
11741 | public: | |
11742 | Composite_literal_expression(Type* type, int depth, bool has_keys, | |
11743 | Expression_list* vals, source_location location) | |
11744 | : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location), | |
11745 | type_(type), depth_(depth), vals_(vals), has_keys_(has_keys) | |
11746 | { } | |
11747 | ||
11748 | protected: | |
11749 | int | |
11750 | do_traverse(Traverse* traverse); | |
11751 | ||
11752 | Expression* | |
11753 | do_lower(Gogo*, Named_object*, int); | |
11754 | ||
11755 | Expression* | |
11756 | do_copy() | |
11757 | { | |
11758 | return new Composite_literal_expression(this->type_, this->depth_, | |
11759 | this->has_keys_, | |
11760 | (this->vals_ == NULL | |
11761 | ? NULL | |
11762 | : this->vals_->copy()), | |
11763 | this->location()); | |
11764 | } | |
11765 | ||
11766 | private: | |
11767 | Expression* | |
81c4b26b | 11768 | lower_struct(Gogo*, Type*); |
e440a328 | 11769 | |
11770 | Expression* | |
11771 | lower_array(Type*); | |
11772 | ||
11773 | Expression* | |
11774 | make_array(Type*, Expression_list*); | |
11775 | ||
11776 | Expression* | |
a287720d | 11777 | lower_map(Gogo*, Named_object*, Type*); |
e440a328 | 11778 | |
11779 | // The type of the composite literal. | |
11780 | Type* type_; | |
11781 | // The depth within a list of composite literals within a composite | |
11782 | // literal, when the type is omitted. | |
11783 | int depth_; | |
11784 | // The values to put in the composite literal. | |
11785 | Expression_list* vals_; | |
11786 | // If this is true, then VALS_ is a list of pairs: a key and a | |
11787 | // value. In an array initializer, a missing key will be NULL. | |
11788 | bool has_keys_; | |
11789 | }; | |
11790 | ||
11791 | // Traversal. | |
11792 | ||
11793 | int | |
11794 | Composite_literal_expression::do_traverse(Traverse* traverse) | |
11795 | { | |
11796 | if (this->vals_ != NULL | |
11797 | && this->vals_->traverse(traverse) == TRAVERSE_EXIT) | |
11798 | return TRAVERSE_EXIT; | |
11799 | return Type::traverse(this->type_, traverse); | |
11800 | } | |
11801 | ||
11802 | // Lower a generic composite literal into a specific version based on | |
11803 | // the type. | |
11804 | ||
11805 | Expression* | |
a287720d | 11806 | Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int) |
e440a328 | 11807 | { |
11808 | Type* type = this->type_; | |
11809 | ||
11810 | for (int depth = this->depth_; depth > 0; --depth) | |
11811 | { | |
11812 | if (type->array_type() != NULL) | |
11813 | type = type->array_type()->element_type(); | |
11814 | else if (type->map_type() != NULL) | |
11815 | type = type->map_type()->val_type(); | |
11816 | else | |
11817 | { | |
5c13bd80 | 11818 | if (!type->is_error()) |
e440a328 | 11819 | error_at(this->location(), |
11820 | ("may only omit types within composite literals " | |
11821 | "of slice, array, or map type")); | |
11822 | return Expression::make_error(this->location()); | |
11823 | } | |
11824 | } | |
11825 | ||
5c13bd80 | 11826 | if (type->is_error()) |
e440a328 | 11827 | return Expression::make_error(this->location()); |
11828 | else if (type->struct_type() != NULL) | |
81c4b26b | 11829 | return this->lower_struct(gogo, type); |
e440a328 | 11830 | else if (type->array_type() != NULL) |
11831 | return this->lower_array(type); | |
11832 | else if (type->map_type() != NULL) | |
a287720d | 11833 | return this->lower_map(gogo, function, type); |
e440a328 | 11834 | else |
11835 | { | |
11836 | error_at(this->location(), | |
11837 | ("expected struct, slice, array, or map type " | |
11838 | "for composite literal")); | |
11839 | return Expression::make_error(this->location()); | |
11840 | } | |
11841 | } | |
11842 | ||
11843 | // Lower a struct composite literal. | |
11844 | ||
11845 | Expression* | |
81c4b26b | 11846 | Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) |
e440a328 | 11847 | { |
11848 | source_location location = this->location(); | |
11849 | Struct_type* st = type->struct_type(); | |
11850 | if (this->vals_ == NULL || !this->has_keys_) | |
11851 | return new Struct_construction_expression(type, this->vals_, location); | |
11852 | ||
11853 | size_t field_count = st->field_count(); | |
11854 | std::vector<Expression*> vals(field_count); | |
11855 | Expression_list::const_iterator p = this->vals_->begin(); | |
11856 | while (p != this->vals_->end()) | |
11857 | { | |
11858 | Expression* name_expr = *p; | |
11859 | ||
11860 | ++p; | |
11861 | gcc_assert(p != this->vals_->end()); | |
11862 | Expression* val = *p; | |
11863 | ||
11864 | ++p; | |
11865 | ||
11866 | if (name_expr == NULL) | |
11867 | { | |
11868 | error_at(val->location(), "mixture of field and value initializers"); | |
11869 | return Expression::make_error(location); | |
11870 | } | |
11871 | ||
11872 | bool bad_key = false; | |
11873 | std::string name; | |
81c4b26b | 11874 | const Named_object* no = NULL; |
e440a328 | 11875 | switch (name_expr->classification()) |
11876 | { | |
11877 | case EXPRESSION_UNKNOWN_REFERENCE: | |
11878 | name = name_expr->unknown_expression()->name(); | |
11879 | break; | |
11880 | ||
11881 | case EXPRESSION_CONST_REFERENCE: | |
81c4b26b | 11882 | no = static_cast<Const_expression*>(name_expr)->named_object(); |
e440a328 | 11883 | break; |
11884 | ||
11885 | case EXPRESSION_TYPE: | |
11886 | { | |
11887 | Type* t = name_expr->type(); | |
11888 | Named_type* nt = t->named_type(); | |
11889 | if (nt == NULL) | |
11890 | bad_key = true; | |
11891 | else | |
81c4b26b | 11892 | no = nt->named_object(); |
e440a328 | 11893 | } |
11894 | break; | |
11895 | ||
11896 | case EXPRESSION_VAR_REFERENCE: | |
81c4b26b | 11897 | no = name_expr->var_expression()->named_object(); |
e440a328 | 11898 | break; |
11899 | ||
11900 | case EXPRESSION_FUNC_REFERENCE: | |
81c4b26b | 11901 | no = name_expr->func_expression()->named_object(); |
e440a328 | 11902 | break; |
11903 | ||
11904 | case EXPRESSION_UNARY: | |
11905 | // If there is a local variable around with the same name as | |
11906 | // the field, and this occurs in the closure, then the | |
11907 | // parser may turn the field reference into an indirection | |
11908 | // through the closure. FIXME: This is a mess. | |
11909 | { | |
11910 | bad_key = true; | |
11911 | Unary_expression* ue = static_cast<Unary_expression*>(name_expr); | |
11912 | if (ue->op() == OPERATOR_MULT) | |
11913 | { | |
11914 | Field_reference_expression* fre = | |
11915 | ue->operand()->field_reference_expression(); | |
11916 | if (fre != NULL) | |
11917 | { | |
11918 | Struct_type* st = | |
11919 | fre->expr()->type()->deref()->struct_type(); | |
11920 | if (st != NULL) | |
11921 | { | |
11922 | const Struct_field* sf = st->field(fre->field_index()); | |
11923 | name = sf->field_name(); | |
11924 | char buf[20]; | |
11925 | snprintf(buf, sizeof buf, "%u", fre->field_index()); | |
11926 | size_t buflen = strlen(buf); | |
11927 | if (name.compare(name.length() - buflen, buflen, buf) | |
11928 | == 0) | |
11929 | { | |
11930 | name = name.substr(0, name.length() - buflen); | |
11931 | bad_key = false; | |
11932 | } | |
11933 | } | |
11934 | } | |
11935 | } | |
11936 | } | |
11937 | break; | |
11938 | ||
11939 | default: | |
11940 | bad_key = true; | |
11941 | break; | |
11942 | } | |
11943 | if (bad_key) | |
11944 | { | |
11945 | error_at(name_expr->location(), "expected struct field name"); | |
11946 | return Expression::make_error(location); | |
11947 | } | |
11948 | ||
81c4b26b | 11949 | if (no != NULL) |
11950 | { | |
11951 | name = no->name(); | |
11952 | ||
11953 | // A predefined name won't be packed. If it starts with a | |
11954 | // lower case letter we need to check for that case, because | |
11955 | // the field name will be packed. | |
11956 | if (!Gogo::is_hidden_name(name) | |
11957 | && name[0] >= 'a' | |
11958 | && name[0] <= 'z') | |
11959 | { | |
11960 | Named_object* gno = gogo->lookup_global(name.c_str()); | |
11961 | if (gno == no) | |
11962 | name = gogo->pack_hidden_name(name, false); | |
11963 | } | |
11964 | } | |
11965 | ||
e440a328 | 11966 | unsigned int index; |
11967 | const Struct_field* sf = st->find_local_field(name, &index); | |
11968 | if (sf == NULL) | |
11969 | { | |
11970 | error_at(name_expr->location(), "unknown field %qs in %qs", | |
11971 | Gogo::message_name(name).c_str(), | |
11972 | (type->named_type() != NULL | |
11973 | ? type->named_type()->message_name().c_str() | |
11974 | : "unnamed struct")); | |
11975 | return Expression::make_error(location); | |
11976 | } | |
11977 | if (vals[index] != NULL) | |
11978 | { | |
11979 | error_at(name_expr->location(), | |
11980 | "duplicate value for field %qs in %qs", | |
11981 | Gogo::message_name(name).c_str(), | |
11982 | (type->named_type() != NULL | |
11983 | ? type->named_type()->message_name().c_str() | |
11984 | : "unnamed struct")); | |
11985 | return Expression::make_error(location); | |
11986 | } | |
11987 | ||
11988 | vals[index] = val; | |
11989 | } | |
11990 | ||
11991 | Expression_list* list = new Expression_list; | |
11992 | list->reserve(field_count); | |
11993 | for (size_t i = 0; i < field_count; ++i) | |
11994 | list->push_back(vals[i]); | |
11995 | ||
11996 | return new Struct_construction_expression(type, list, location); | |
11997 | } | |
11998 | ||
11999 | // Lower an array composite literal. | |
12000 | ||
12001 | Expression* | |
12002 | Composite_literal_expression::lower_array(Type* type) | |
12003 | { | |
12004 | source_location location = this->location(); | |
12005 | if (this->vals_ == NULL || !this->has_keys_) | |
12006 | return this->make_array(type, this->vals_); | |
12007 | ||
12008 | std::vector<Expression*> vals; | |
12009 | vals.reserve(this->vals_->size()); | |
12010 | unsigned long index = 0; | |
12011 | Expression_list::const_iterator p = this->vals_->begin(); | |
12012 | while (p != this->vals_->end()) | |
12013 | { | |
12014 | Expression* index_expr = *p; | |
12015 | ||
12016 | ++p; | |
12017 | gcc_assert(p != this->vals_->end()); | |
12018 | Expression* val = *p; | |
12019 | ||
12020 | ++p; | |
12021 | ||
12022 | if (index_expr != NULL) | |
12023 | { | |
12024 | mpz_t ival; | |
12025 | mpz_init(ival); | |
6f6d9955 | 12026 | |
e440a328 | 12027 | Type* dummy; |
12028 | if (!index_expr->integer_constant_value(true, ival, &dummy)) | |
12029 | { | |
12030 | mpz_clear(ival); | |
12031 | error_at(index_expr->location(), | |
12032 | "index expression is not integer constant"); | |
12033 | return Expression::make_error(location); | |
12034 | } | |
6f6d9955 | 12035 | |
e440a328 | 12036 | if (mpz_sgn(ival) < 0) |
12037 | { | |
12038 | mpz_clear(ival); | |
12039 | error_at(index_expr->location(), "index expression is negative"); | |
12040 | return Expression::make_error(location); | |
12041 | } | |
6f6d9955 | 12042 | |
e440a328 | 12043 | index = mpz_get_ui(ival); |
12044 | if (mpz_cmp_ui(ival, index) != 0) | |
12045 | { | |
12046 | mpz_clear(ival); | |
12047 | error_at(index_expr->location(), "index value overflow"); | |
12048 | return Expression::make_error(location); | |
12049 | } | |
6f6d9955 | 12050 | |
12051 | Named_type* ntype = Type::lookup_integer_type("int"); | |
12052 | Integer_type* inttype = ntype->integer_type(); | |
12053 | mpz_t max; | |
12054 | mpz_init_set_ui(max, 1); | |
12055 | mpz_mul_2exp(max, max, inttype->bits() - 1); | |
12056 | bool ok = mpz_cmp(ival, max) < 0; | |
12057 | mpz_clear(max); | |
12058 | if (!ok) | |
12059 | { | |
12060 | mpz_clear(ival); | |
12061 | error_at(index_expr->location(), "index value overflow"); | |
12062 | return Expression::make_error(location); | |
12063 | } | |
12064 | ||
e440a328 | 12065 | mpz_clear(ival); |
6f6d9955 | 12066 | |
12067 | // FIXME: Our representation isn't very good; this avoids | |
12068 | // thrashing. | |
12069 | if (index > 0x1000000) | |
12070 | { | |
12071 | error_at(index_expr->location(), "index too large for compiler"); | |
12072 | return Expression::make_error(location); | |
12073 | } | |
e440a328 | 12074 | } |
12075 | ||
12076 | if (index == vals.size()) | |
12077 | vals.push_back(val); | |
12078 | else | |
12079 | { | |
12080 | if (index > vals.size()) | |
12081 | { | |
12082 | vals.reserve(index + 32); | |
12083 | vals.resize(index + 1, static_cast<Expression*>(NULL)); | |
12084 | } | |
12085 | if (vals[index] != NULL) | |
12086 | { | |
12087 | error_at((index_expr != NULL | |
12088 | ? index_expr->location() | |
12089 | : val->location()), | |
12090 | "duplicate value for index %lu", | |
12091 | index); | |
12092 | return Expression::make_error(location); | |
12093 | } | |
12094 | vals[index] = val; | |
12095 | } | |
12096 | ||
12097 | ++index; | |
12098 | } | |
12099 | ||
12100 | size_t size = vals.size(); | |
12101 | Expression_list* list = new Expression_list; | |
12102 | list->reserve(size); | |
12103 | for (size_t i = 0; i < size; ++i) | |
12104 | list->push_back(vals[i]); | |
12105 | ||
12106 | return this->make_array(type, list); | |
12107 | } | |
12108 | ||
12109 | // Actually build the array composite literal. This handles | |
12110 | // [...]{...}. | |
12111 | ||
12112 | Expression* | |
12113 | Composite_literal_expression::make_array(Type* type, Expression_list* vals) | |
12114 | { | |
12115 | source_location location = this->location(); | |
12116 | Array_type* at = type->array_type(); | |
12117 | if (at->length() != NULL && at->length()->is_nil_expression()) | |
12118 | { | |
12119 | size_t size = vals == NULL ? 0 : vals->size(); | |
12120 | mpz_t vlen; | |
12121 | mpz_init_set_ui(vlen, size); | |
12122 | Expression* elen = Expression::make_integer(&vlen, NULL, location); | |
12123 | mpz_clear(vlen); | |
12124 | at = Type::make_array_type(at->element_type(), elen); | |
12125 | type = at; | |
12126 | } | |
12127 | if (at->length() != NULL) | |
12128 | return new Fixed_array_construction_expression(type, vals, location); | |
12129 | else | |
12130 | return new Open_array_construction_expression(type, vals, location); | |
12131 | } | |
12132 | ||
12133 | // Lower a map composite literal. | |
12134 | ||
12135 | Expression* | |
a287720d | 12136 | Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function, |
12137 | Type* type) | |
e440a328 | 12138 | { |
12139 | source_location location = this->location(); | |
12140 | if (this->vals_ != NULL) | |
12141 | { | |
12142 | if (!this->has_keys_) | |
12143 | { | |
12144 | error_at(location, "map composite literal must have keys"); | |
12145 | return Expression::make_error(location); | |
12146 | } | |
12147 | ||
a287720d | 12148 | for (Expression_list::iterator p = this->vals_->begin(); |
e440a328 | 12149 | p != this->vals_->end(); |
12150 | p += 2) | |
12151 | { | |
12152 | if (*p == NULL) | |
12153 | { | |
12154 | ++p; | |
12155 | error_at((*p)->location(), | |
12156 | "map composite literal must have keys for every value"); | |
12157 | return Expression::make_error(location); | |
12158 | } | |
a287720d | 12159 | // Make sure we have lowered the key; it may not have been |
12160 | // lowered in order to handle keys for struct composite | |
12161 | // literals. Lower it now to get the right error message. | |
12162 | if ((*p)->unknown_expression() != NULL) | |
12163 | { | |
12164 | (*p)->unknown_expression()->clear_is_composite_literal_key(); | |
12165 | gogo->lower_expression(function, &*p); | |
12166 | gcc_assert((*p)->is_error_expression()); | |
12167 | return Expression::make_error(location); | |
12168 | } | |
e440a328 | 12169 | } |
12170 | } | |
12171 | ||
12172 | return new Map_construction_expression(type, this->vals_, location); | |
12173 | } | |
12174 | ||
12175 | // Make a composite literal expression. | |
12176 | ||
12177 | Expression* | |
12178 | Expression::make_composite_literal(Type* type, int depth, bool has_keys, | |
12179 | Expression_list* vals, | |
12180 | source_location location) | |
12181 | { | |
12182 | return new Composite_literal_expression(type, depth, has_keys, vals, | |
12183 | location); | |
12184 | } | |
12185 | ||
12186 | // Return whether this expression is a composite literal. | |
12187 | ||
12188 | bool | |
12189 | Expression::is_composite_literal() const | |
12190 | { | |
12191 | switch (this->classification_) | |
12192 | { | |
12193 | case EXPRESSION_COMPOSITE_LITERAL: | |
12194 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
12195 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
12196 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
12197 | case EXPRESSION_MAP_CONSTRUCTION: | |
12198 | return true; | |
12199 | default: | |
12200 | return false; | |
12201 | } | |
12202 | } | |
12203 | ||
12204 | // Return whether this expression is a composite literal which is not | |
12205 | // constant. | |
12206 | ||
12207 | bool | |
12208 | Expression::is_nonconstant_composite_literal() const | |
12209 | { | |
12210 | switch (this->classification_) | |
12211 | { | |
12212 | case EXPRESSION_STRUCT_CONSTRUCTION: | |
12213 | { | |
12214 | const Struct_construction_expression *psce = | |
12215 | static_cast<const Struct_construction_expression*>(this); | |
12216 | return !psce->is_constant_struct(); | |
12217 | } | |
12218 | case EXPRESSION_FIXED_ARRAY_CONSTRUCTION: | |
12219 | { | |
12220 | const Fixed_array_construction_expression *pace = | |
12221 | static_cast<const Fixed_array_construction_expression*>(this); | |
12222 | return !pace->is_constant_array(); | |
12223 | } | |
12224 | case EXPRESSION_OPEN_ARRAY_CONSTRUCTION: | |
12225 | { | |
12226 | const Open_array_construction_expression *pace = | |
12227 | static_cast<const Open_array_construction_expression*>(this); | |
12228 | return !pace->is_constant_array(); | |
12229 | } | |
12230 | case EXPRESSION_MAP_CONSTRUCTION: | |
12231 | return true; | |
12232 | default: | |
12233 | return false; | |
12234 | } | |
12235 | } | |
12236 | ||
12237 | // Return true if this is a reference to a local variable. | |
12238 | ||
12239 | bool | |
12240 | Expression::is_local_variable() const | |
12241 | { | |
12242 | const Var_expression* ve = this->var_expression(); | |
12243 | if (ve == NULL) | |
12244 | return false; | |
12245 | const Named_object* no = ve->named_object(); | |
12246 | return (no->is_result_variable() | |
12247 | || (no->is_variable() && !no->var_value()->is_global())); | |
12248 | } | |
12249 | ||
12250 | // Class Type_guard_expression. | |
12251 | ||
12252 | // Traversal. | |
12253 | ||
12254 | int | |
12255 | Type_guard_expression::do_traverse(Traverse* traverse) | |
12256 | { | |
12257 | if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT | |
12258 | || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT) | |
12259 | return TRAVERSE_EXIT; | |
12260 | return TRAVERSE_CONTINUE; | |
12261 | } | |
12262 | ||
12263 | // Check types of a type guard expression. The expression must have | |
12264 | // an interface type, but the actual type conversion is checked at run | |
12265 | // time. | |
12266 | ||
12267 | void | |
12268 | Type_guard_expression::do_check_types(Gogo*) | |
12269 | { | |
12270 | // 6g permits using a type guard with unsafe.pointer; we are | |
12271 | // compatible. | |
12272 | Type* expr_type = this->expr_->type(); | |
12273 | if (expr_type->is_unsafe_pointer_type()) | |
12274 | { | |
12275 | if (this->type_->points_to() == NULL | |
12276 | && (this->type_->integer_type() == NULL | |
12277 | || (this->type_->forwarded() | |
12278 | != Type::lookup_integer_type("uintptr")))) | |
12279 | this->report_error(_("invalid unsafe.Pointer conversion")); | |
12280 | } | |
12281 | else if (this->type_->is_unsafe_pointer_type()) | |
12282 | { | |
12283 | if (expr_type->points_to() == NULL | |
12284 | && (expr_type->integer_type() == NULL | |
12285 | || (expr_type->forwarded() | |
12286 | != Type::lookup_integer_type("uintptr")))) | |
12287 | this->report_error(_("invalid unsafe.Pointer conversion")); | |
12288 | } | |
12289 | else if (expr_type->interface_type() == NULL) | |
f725ade8 | 12290 | { |
5c13bd80 | 12291 | if (!expr_type->is_error() && !this->type_->is_error()) |
f725ade8 | 12292 | this->report_error(_("type assertion only valid for interface types")); |
12293 | this->set_is_error(); | |
12294 | } | |
e440a328 | 12295 | else if (this->type_->interface_type() == NULL) |
12296 | { | |
12297 | std::string reason; | |
12298 | if (!expr_type->interface_type()->implements_interface(this->type_, | |
12299 | &reason)) | |
12300 | { | |
5c13bd80 | 12301 | if (!this->type_->is_error()) |
e440a328 | 12302 | { |
f725ade8 | 12303 | if (reason.empty()) |
12304 | this->report_error(_("impossible type assertion: " | |
12305 | "type does not implement interface")); | |
12306 | else | |
12307 | error_at(this->location(), | |
12308 | ("impossible type assertion: " | |
12309 | "type does not implement interface (%s)"), | |
12310 | reason.c_str()); | |
e440a328 | 12311 | } |
f725ade8 | 12312 | this->set_is_error(); |
e440a328 | 12313 | } |
12314 | } | |
12315 | } | |
12316 | ||
12317 | // Return a tree for a type guard expression. | |
12318 | ||
12319 | tree | |
12320 | Type_guard_expression::do_get_tree(Translate_context* context) | |
12321 | { | |
12322 | Gogo* gogo = context->gogo(); | |
12323 | tree expr_tree = this->expr_->get_tree(context); | |
12324 | if (expr_tree == error_mark_node) | |
12325 | return error_mark_node; | |
12326 | Type* expr_type = this->expr_->type(); | |
12327 | if ((this->type_->is_unsafe_pointer_type() | |
12328 | && (expr_type->points_to() != NULL | |
12329 | || expr_type->integer_type() != NULL)) | |
12330 | || (expr_type->is_unsafe_pointer_type() | |
12331 | && this->type_->points_to() != NULL)) | |
12332 | return convert_to_pointer(this->type_->get_tree(gogo), expr_tree); | |
12333 | else if (expr_type->is_unsafe_pointer_type() | |
12334 | && this->type_->integer_type() != NULL) | |
12335 | return convert_to_integer(this->type_->get_tree(gogo), expr_tree); | |
12336 | else if (this->type_->interface_type() != NULL) | |
12337 | return Expression::convert_interface_to_interface(context, this->type_, | |
12338 | this->expr_->type(), | |
12339 | expr_tree, true, | |
12340 | this->location()); | |
12341 | else | |
12342 | return Expression::convert_for_assignment(context, this->type_, | |
12343 | this->expr_->type(), expr_tree, | |
12344 | this->location()); | |
12345 | } | |
12346 | ||
12347 | // Make a type guard expression. | |
12348 | ||
12349 | Expression* | |
12350 | Expression::make_type_guard(Expression* expr, Type* type, | |
12351 | source_location location) | |
12352 | { | |
12353 | return new Type_guard_expression(expr, type, location); | |
12354 | } | |
12355 | ||
12356 | // Class Heap_composite_expression. | |
12357 | ||
12358 | // When you take the address of a composite literal, it is allocated | |
12359 | // on the heap. This class implements that. | |
12360 | ||
12361 | class Heap_composite_expression : public Expression | |
12362 | { | |
12363 | public: | |
12364 | Heap_composite_expression(Expression* expr, source_location location) | |
12365 | : Expression(EXPRESSION_HEAP_COMPOSITE, location), | |
12366 | expr_(expr) | |
12367 | { } | |
12368 | ||
12369 | protected: | |
12370 | int | |
12371 | do_traverse(Traverse* traverse) | |
12372 | { return Expression::traverse(&this->expr_, traverse); } | |
12373 | ||
12374 | Type* | |
12375 | do_type() | |
12376 | { return Type::make_pointer_type(this->expr_->type()); } | |
12377 | ||
12378 | void | |
12379 | do_determine_type(const Type_context*) | |
12380 | { this->expr_->determine_type_no_context(); } | |
12381 | ||
12382 | Expression* | |
12383 | do_copy() | |
12384 | { | |
12385 | return Expression::make_heap_composite(this->expr_->copy(), | |
12386 | this->location()); | |
12387 | } | |
12388 | ||
12389 | tree | |
12390 | do_get_tree(Translate_context*); | |
12391 | ||
12392 | // We only export global objects, and the parser does not generate | |
12393 | // this in global scope. | |
12394 | void | |
12395 | do_export(Export*) const | |
12396 | { gcc_unreachable(); } | |
12397 | ||
12398 | private: | |
12399 | // The composite literal which is being put on the heap. | |
12400 | Expression* expr_; | |
12401 | }; | |
12402 | ||
12403 | // Return a tree which allocates a composite literal on the heap. | |
12404 | ||
12405 | tree | |
12406 | Heap_composite_expression::do_get_tree(Translate_context* context) | |
12407 | { | |
12408 | tree expr_tree = this->expr_->get_tree(context); | |
12409 | if (expr_tree == error_mark_node) | |
12410 | return error_mark_node; | |
12411 | tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree)); | |
12412 | gcc_assert(TREE_CODE(expr_size) == INTEGER_CST); | |
12413 | tree space = context->gogo()->allocate_memory(this->expr_->type(), | |
12414 | expr_size, this->location()); | |
12415 | space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space); | |
12416 | space = save_expr(space); | |
12417 | tree ref = build_fold_indirect_ref_loc(this->location(), space); | |
12418 | TREE_THIS_NOTRAP(ref) = 1; | |
12419 | tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space), | |
12420 | build2(MODIFY_EXPR, void_type_node, ref, expr_tree), | |
12421 | space); | |
12422 | SET_EXPR_LOCATION(ret, this->location()); | |
12423 | return ret; | |
12424 | } | |
12425 | ||
12426 | // Allocate a composite literal on the heap. | |
12427 | ||
12428 | Expression* | |
12429 | Expression::make_heap_composite(Expression* expr, source_location location) | |
12430 | { | |
12431 | return new Heap_composite_expression(expr, location); | |
12432 | } | |
12433 | ||
12434 | // Class Receive_expression. | |
12435 | ||
12436 | // Return the type of a receive expression. | |
12437 | ||
12438 | Type* | |
12439 | Receive_expression::do_type() | |
12440 | { | |
12441 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
12442 | if (channel_type == NULL) | |
12443 | return Type::make_error_type(); | |
12444 | return channel_type->element_type(); | |
12445 | } | |
12446 | ||
12447 | // Check types for a receive expression. | |
12448 | ||
12449 | void | |
12450 | Receive_expression::do_check_types(Gogo*) | |
12451 | { | |
12452 | Type* type = this->channel_->type(); | |
5c13bd80 | 12453 | if (type->is_error()) |
e440a328 | 12454 | { |
12455 | this->set_is_error(); | |
12456 | return; | |
12457 | } | |
12458 | if (type->channel_type() == NULL) | |
12459 | { | |
12460 | this->report_error(_("expected channel")); | |
12461 | return; | |
12462 | } | |
12463 | if (!type->channel_type()->may_receive()) | |
12464 | { | |
12465 | this->report_error(_("invalid receive on send-only channel")); | |
12466 | return; | |
12467 | } | |
12468 | } | |
12469 | ||
12470 | // Get a tree for a receive expression. | |
12471 | ||
12472 | tree | |
12473 | Receive_expression::do_get_tree(Translate_context* context) | |
12474 | { | |
12475 | Channel_type* channel_type = this->channel_->type()->channel_type(); | |
5b8368f4 | 12476 | if (channel_type == NULL) |
12477 | { | |
5c13bd80 | 12478 | gcc_assert(this->channel_->type()->is_error()); |
5b8368f4 | 12479 | return error_mark_node; |
12480 | } | |
e440a328 | 12481 | Type* element_type = channel_type->element_type(); |
12482 | tree element_type_tree = element_type->get_tree(context->gogo()); | |
12483 | ||
12484 | tree channel = this->channel_->get_tree(context); | |
12485 | if (element_type_tree == error_mark_node || channel == error_mark_node) | |
12486 | return error_mark_node; | |
12487 | ||
12488 | return Gogo::receive_from_channel(element_type_tree, channel, | |
12489 | this->for_select_, this->location()); | |
12490 | } | |
12491 | ||
12492 | // Make a receive expression. | |
12493 | ||
12494 | Receive_expression* | |
12495 | Expression::make_receive(Expression* channel, source_location location) | |
12496 | { | |
12497 | return new Receive_expression(channel, location); | |
12498 | } | |
12499 | ||
e440a328 | 12500 | // An expression which evaluates to a pointer to the type descriptor |
12501 | // of a type. | |
12502 | ||
12503 | class Type_descriptor_expression : public Expression | |
12504 | { | |
12505 | public: | |
12506 | Type_descriptor_expression(Type* type, source_location location) | |
12507 | : Expression(EXPRESSION_TYPE_DESCRIPTOR, location), | |
12508 | type_(type) | |
12509 | { } | |
12510 | ||
12511 | protected: | |
12512 | Type* | |
12513 | do_type() | |
12514 | { return Type::make_type_descriptor_ptr_type(); } | |
12515 | ||
12516 | void | |
12517 | do_determine_type(const Type_context*) | |
12518 | { } | |
12519 | ||
12520 | Expression* | |
12521 | do_copy() | |
12522 | { return this; } | |
12523 | ||
12524 | tree | |
12525 | do_get_tree(Translate_context* context) | |
12526 | { return this->type_->type_descriptor_pointer(context->gogo()); } | |
12527 | ||
12528 | private: | |
12529 | // The type for which this is the descriptor. | |
12530 | Type* type_; | |
12531 | }; | |
12532 | ||
12533 | // Make a type descriptor expression. | |
12534 | ||
12535 | Expression* | |
12536 | Expression::make_type_descriptor(Type* type, source_location location) | |
12537 | { | |
12538 | return new Type_descriptor_expression(type, location); | |
12539 | } | |
12540 | ||
12541 | // An expression which evaluates to some characteristic of a type. | |
12542 | // This is only used to initialize fields of a type descriptor. Using | |
12543 | // a new expression class is slightly inefficient but gives us a good | |
12544 | // separation between the frontend and the middle-end with regard to | |
12545 | // how types are laid out. | |
12546 | ||
12547 | class Type_info_expression : public Expression | |
12548 | { | |
12549 | public: | |
12550 | Type_info_expression(Type* type, Type_info type_info) | |
12551 | : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION), | |
12552 | type_(type), type_info_(type_info) | |
12553 | { } | |
12554 | ||
12555 | protected: | |
12556 | Type* | |
12557 | do_type(); | |
12558 | ||
12559 | void | |
12560 | do_determine_type(const Type_context*) | |
12561 | { } | |
12562 | ||
12563 | Expression* | |
12564 | do_copy() | |
12565 | { return this; } | |
12566 | ||
12567 | tree | |
12568 | do_get_tree(Translate_context* context); | |
12569 | ||
12570 | private: | |
12571 | // The type for which we are getting information. | |
12572 | Type* type_; | |
12573 | // What information we want. | |
12574 | Type_info type_info_; | |
12575 | }; | |
12576 | ||
12577 | // The type is chosen to match what the type descriptor struct | |
12578 | // expects. | |
12579 | ||
12580 | Type* | |
12581 | Type_info_expression::do_type() | |
12582 | { | |
12583 | switch (this->type_info_) | |
12584 | { | |
12585 | case TYPE_INFO_SIZE: | |
12586 | return Type::lookup_integer_type("uintptr"); | |
12587 | case TYPE_INFO_ALIGNMENT: | |
12588 | case TYPE_INFO_FIELD_ALIGNMENT: | |
12589 | return Type::lookup_integer_type("uint8"); | |
12590 | default: | |
12591 | gcc_unreachable(); | |
12592 | } | |
12593 | } | |
12594 | ||
12595 | // Return type information in GENERIC. | |
12596 | ||
12597 | tree | |
12598 | Type_info_expression::do_get_tree(Translate_context* context) | |
12599 | { | |
12600 | tree type_tree = this->type_->get_tree(context->gogo()); | |
12601 | if (type_tree == error_mark_node) | |
12602 | return error_mark_node; | |
12603 | ||
12604 | tree val_type_tree = this->type()->get_tree(context->gogo()); | |
12605 | gcc_assert(val_type_tree != error_mark_node); | |
12606 | ||
12607 | if (this->type_info_ == TYPE_INFO_SIZE) | |
12608 | return fold_convert_loc(BUILTINS_LOCATION, val_type_tree, | |
12609 | TYPE_SIZE_UNIT(type_tree)); | |
12610 | else | |
12611 | { | |
637bd3af | 12612 | unsigned int val; |
e440a328 | 12613 | if (this->type_info_ == TYPE_INFO_ALIGNMENT) |
637bd3af | 12614 | val = go_type_alignment(type_tree); |
e440a328 | 12615 | else |
637bd3af | 12616 | val = go_field_alignment(type_tree); |
e440a328 | 12617 | return build_int_cstu(val_type_tree, val); |
12618 | } | |
12619 | } | |
12620 | ||
12621 | // Make a type info expression. | |
12622 | ||
12623 | Expression* | |
12624 | Expression::make_type_info(Type* type, Type_info type_info) | |
12625 | { | |
12626 | return new Type_info_expression(type, type_info); | |
12627 | } | |
12628 | ||
12629 | // An expression which evaluates to the offset of a field within a | |
12630 | // struct. This, like Type_info_expression, q.v., is only used to | |
12631 | // initialize fields of a type descriptor. | |
12632 | ||
12633 | class Struct_field_offset_expression : public Expression | |
12634 | { | |
12635 | public: | |
12636 | Struct_field_offset_expression(Struct_type* type, const Struct_field* field) | |
12637 | : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION), | |
12638 | type_(type), field_(field) | |
12639 | { } | |
12640 | ||
12641 | protected: | |
12642 | Type* | |
12643 | do_type() | |
12644 | { return Type::lookup_integer_type("uintptr"); } | |
12645 | ||
12646 | void | |
12647 | do_determine_type(const Type_context*) | |
12648 | { } | |
12649 | ||
12650 | Expression* | |
12651 | do_copy() | |
12652 | { return this; } | |
12653 | ||
12654 | tree | |
12655 | do_get_tree(Translate_context* context); | |
12656 | ||
12657 | private: | |
12658 | // The type of the struct. | |
12659 | Struct_type* type_; | |
12660 | // The field. | |
12661 | const Struct_field* field_; | |
12662 | }; | |
12663 | ||
12664 | // Return a struct field offset in GENERIC. | |
12665 | ||
12666 | tree | |
12667 | Struct_field_offset_expression::do_get_tree(Translate_context* context) | |
12668 | { | |
12669 | tree type_tree = this->type_->get_tree(context->gogo()); | |
12670 | if (type_tree == error_mark_node) | |
12671 | return error_mark_node; | |
12672 | ||
12673 | tree val_type_tree = this->type()->get_tree(context->gogo()); | |
12674 | gcc_assert(val_type_tree != error_mark_node); | |
12675 | ||
12676 | const Struct_field_list* fields = this->type_->fields(); | |
12677 | tree struct_field_tree = TYPE_FIELDS(type_tree); | |
12678 | Struct_field_list::const_iterator p; | |
12679 | for (p = fields->begin(); | |
12680 | p != fields->end(); | |
12681 | ++p, struct_field_tree = DECL_CHAIN(struct_field_tree)) | |
12682 | { | |
12683 | gcc_assert(struct_field_tree != NULL_TREE); | |
12684 | if (&*p == this->field_) | |
12685 | break; | |
12686 | } | |
12687 | gcc_assert(&*p == this->field_); | |
12688 | ||
12689 | return fold_convert_loc(BUILTINS_LOCATION, val_type_tree, | |
12690 | byte_position(struct_field_tree)); | |
12691 | } | |
12692 | ||
12693 | // Make an expression for a struct field offset. | |
12694 | ||
12695 | Expression* | |
12696 | Expression::make_struct_field_offset(Struct_type* type, | |
12697 | const Struct_field* field) | |
12698 | { | |
12699 | return new Struct_field_offset_expression(type, field); | |
12700 | } | |
12701 | ||
12702 | // An expression which evaluates to the address of an unnamed label. | |
12703 | ||
12704 | class Label_addr_expression : public Expression | |
12705 | { | |
12706 | public: | |
12707 | Label_addr_expression(Label* label, source_location location) | |
12708 | : Expression(EXPRESSION_LABEL_ADDR, location), | |
12709 | label_(label) | |
12710 | { } | |
12711 | ||
12712 | protected: | |
12713 | Type* | |
12714 | do_type() | |
12715 | { return Type::make_pointer_type(Type::make_void_type()); } | |
12716 | ||
12717 | void | |
12718 | do_determine_type(const Type_context*) | |
12719 | { } | |
12720 | ||
12721 | Expression* | |
12722 | do_copy() | |
12723 | { return new Label_addr_expression(this->label_, this->location()); } | |
12724 | ||
12725 | tree | |
6e193e6f | 12726 | do_get_tree(Translate_context* context) |
12727 | { | |
e8816003 | 12728 | return expr_to_tree(this->label_->get_addr(context, this->location())); |
6e193e6f | 12729 | } |
e440a328 | 12730 | |
12731 | private: | |
12732 | // The label whose address we are taking. | |
12733 | Label* label_; | |
12734 | }; | |
12735 | ||
12736 | // Make an expression for the address of an unnamed label. | |
12737 | ||
12738 | Expression* | |
12739 | Expression::make_label_addr(Label* label, source_location location) | |
12740 | { | |
12741 | return new Label_addr_expression(label, location); | |
12742 | } | |
12743 | ||
12744 | // Import an expression. This comes at the end in order to see the | |
12745 | // various class definitions. | |
12746 | ||
12747 | Expression* | |
12748 | Expression::import_expression(Import* imp) | |
12749 | { | |
12750 | int c = imp->peek_char(); | |
12751 | if (imp->match_c_string("- ") | |
12752 | || imp->match_c_string("! ") | |
12753 | || imp->match_c_string("^ ")) | |
12754 | return Unary_expression::do_import(imp); | |
12755 | else if (c == '(') | |
12756 | return Binary_expression::do_import(imp); | |
12757 | else if (imp->match_c_string("true") | |
12758 | || imp->match_c_string("false")) | |
12759 | return Boolean_expression::do_import(imp); | |
12760 | else if (c == '"') | |
12761 | return String_expression::do_import(imp); | |
12762 | else if (c == '-' || (c >= '0' && c <= '9')) | |
12763 | { | |
12764 | // This handles integers, floats and complex constants. | |
12765 | return Integer_expression::do_import(imp); | |
12766 | } | |
12767 | else if (imp->match_c_string("nil")) | |
12768 | return Nil_expression::do_import(imp); | |
12769 | else if (imp->match_c_string("convert")) | |
12770 | return Type_conversion_expression::do_import(imp); | |
12771 | else | |
12772 | { | |
12773 | error_at(imp->location(), "import error: expected expression"); | |
12774 | return Expression::make_error(imp->location()); | |
12775 | } | |
12776 | } | |
12777 | ||
12778 | // Class Expression_list. | |
12779 | ||
12780 | // Traverse the list. | |
12781 | ||
12782 | int | |
12783 | Expression_list::traverse(Traverse* traverse) | |
12784 | { | |
12785 | for (Expression_list::iterator p = this->begin(); | |
12786 | p != this->end(); | |
12787 | ++p) | |
12788 | { | |
12789 | if (*p != NULL) | |
12790 | { | |
12791 | if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT) | |
12792 | return TRAVERSE_EXIT; | |
12793 | } | |
12794 | } | |
12795 | return TRAVERSE_CONTINUE; | |
12796 | } | |
12797 | ||
12798 | // Copy the list. | |
12799 | ||
12800 | Expression_list* | |
12801 | Expression_list::copy() | |
12802 | { | |
12803 | Expression_list* ret = new Expression_list(); | |
12804 | for (Expression_list::iterator p = this->begin(); | |
12805 | p != this->end(); | |
12806 | ++p) | |
12807 | { | |
12808 | if (*p == NULL) | |
12809 | ret->push_back(NULL); | |
12810 | else | |
12811 | ret->push_back((*p)->copy()); | |
12812 | } | |
12813 | return ret; | |
12814 | } | |
12815 | ||
12816 | // Return whether an expression list has an error expression. | |
12817 | ||
12818 | bool | |
12819 | Expression_list::contains_error() const | |
12820 | { | |
12821 | for (Expression_list::const_iterator p = this->begin(); | |
12822 | p != this->end(); | |
12823 | ++p) | |
12824 | if (*p != NULL && (*p)->is_error_expression()) | |
12825 | return true; | |
12826 | return false; | |
12827 | } |