re PR bootstrap/46776 (gogo-tree.cc uses TRAMPOLINE_ALIGNMENT and TRAMPOLINE_SIZE)
[gcc.git] / gcc / go / gofrontend / expressions.cc
1 // expressions.cc -- Go frontend expression handling.
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 #include "go-system.h"
8
9 #include <gmp.h>
10
11 #ifndef ENABLE_BUILD_WITH_CXX
12 extern "C"
13 {
14 #endif
15
16 #include "toplev.h"
17 #include "intl.h"
18 #include "tree.h"
19 #include "gimple.h"
20 #include "tree-iterator.h"
21 #include "convert.h"
22 #include "real.h"
23 #include "realmpfr.h"
24
25 #ifndef ENABLE_BUILD_WITH_CXX
26 }
27 #endif
28
29 #include "go-c.h"
30 #include "gogo.h"
31 #include "types.h"
32 #include "export.h"
33 #include "import.h"
34 #include "statements.h"
35 #include "lex.h"
36 #include "expressions.h"
37
38 // Class Expression.
39
40 Expression::Expression(Expression_classification classification,
41 source_location location)
42 : classification_(classification), location_(location)
43 {
44 }
45
46 Expression::~Expression()
47 {
48 }
49
50 // If this expression has a constant integer value, return it.
51
52 bool
53 Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
54 Type** ptype) const
55 {
56 *ptype = NULL;
57 return this->do_integer_constant_value(iota_is_constant, val, ptype);
58 }
59
60 // If this expression has a constant floating point value, return it.
61
62 bool
63 Expression::float_constant_value(mpfr_t val, Type** ptype) const
64 {
65 *ptype = NULL;
66 if (this->do_float_constant_value(val, ptype))
67 return true;
68 mpz_t ival;
69 mpz_init(ival);
70 Type* t;
71 bool ret;
72 if (!this->do_integer_constant_value(false, ival, &t))
73 ret = false;
74 else
75 {
76 mpfr_set_z(val, ival, GMP_RNDN);
77 ret = true;
78 }
79 mpz_clear(ival);
80 return ret;
81 }
82
83 // If this expression has a constant complex value, return it.
84
85 bool
86 Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
87 Type** ptype) const
88 {
89 *ptype = NULL;
90 if (this->do_complex_constant_value(real, imag, ptype))
91 return true;
92 Type *t;
93 if (this->float_constant_value(real, &t))
94 {
95 mpfr_set_ui(imag, 0, GMP_RNDN);
96 return true;
97 }
98 return false;
99 }
100
101 // Traverse the expressions.
102
103 int
104 Expression::traverse(Expression** pexpr, Traverse* traverse)
105 {
106 Expression* expr = *pexpr;
107 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
108 {
109 int t = traverse->expression(pexpr);
110 if (t == TRAVERSE_EXIT)
111 return TRAVERSE_EXIT;
112 else if (t == TRAVERSE_SKIP_COMPONENTS)
113 return TRAVERSE_CONTINUE;
114 }
115 return expr->do_traverse(traverse);
116 }
117
118 // Traverse subexpressions of this expression.
119
120 int
121 Expression::traverse_subexpressions(Traverse* traverse)
122 {
123 return this->do_traverse(traverse);
124 }
125
126 // Default implementation for do_traverse for child classes.
127
128 int
129 Expression::do_traverse(Traverse*)
130 {
131 return TRAVERSE_CONTINUE;
132 }
133
134 // This virtual function is called by the parser if the value of this
135 // expression is being discarded. By default, we warn. Expressions
136 // with side effects override.
137
138 void
139 Expression::do_discarding_value()
140 {
141 this->warn_about_unused_value();
142 }
143
144 // This virtual function is called to export expressions. This will
145 // only be used by expressions which may be constant.
146
147 void
148 Expression::do_export(Export*) const
149 {
150 gcc_unreachable();
151 }
152
153 // Warn that the value of the expression is not used.
154
155 void
156 Expression::warn_about_unused_value()
157 {
158 warning_at(this->location(), OPT_Wunused_value, "value computed is not used");
159 }
160
161 // Note that this expression is an error. This is called by children
162 // when they discover an error.
163
164 void
165 Expression::set_is_error()
166 {
167 this->classification_ = EXPRESSION_ERROR;
168 }
169
170 // For children to call to report an error conveniently.
171
172 void
173 Expression::report_error(const char* msg)
174 {
175 error_at(this->location_, "%s", msg);
176 this->set_is_error();
177 }
178
179 // Set types of variables and constants. This is implemented by the
180 // child class.
181
182 void
183 Expression::determine_type(const Type_context* context)
184 {
185 this->do_determine_type(context);
186 }
187
188 // Set types when there is no context.
189
190 void
191 Expression::determine_type_no_context()
192 {
193 Type_context context;
194 this->do_determine_type(&context);
195 }
196
197 // Return a tree handling any conversions which must be done during
198 // assignment.
199
200 tree
201 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
202 Type* rhs_type, tree rhs_tree,
203 source_location location)
204 {
205 if (lhs_type == rhs_type)
206 return rhs_tree;
207
208 if (lhs_type->is_error_type() || rhs_type->is_error_type())
209 return error_mark_node;
210
211 if (lhs_type->is_undefined() || rhs_type->is_undefined())
212 {
213 // Make sure we report the error.
214 lhs_type->base();
215 rhs_type->base();
216 return error_mark_node;
217 }
218
219 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
220 return error_mark_node;
221
222 Gogo* gogo = context->gogo();
223
224 tree lhs_type_tree = lhs_type->get_tree(gogo);
225 if (lhs_type_tree == error_mark_node)
226 return error_mark_node;
227
228 if (lhs_type->interface_type() != NULL)
229 {
230 if (rhs_type->interface_type() == NULL)
231 return Expression::convert_type_to_interface(context, lhs_type,
232 rhs_type, rhs_tree,
233 location);
234 else
235 return Expression::convert_interface_to_interface(context, lhs_type,
236 rhs_type, rhs_tree,
237 false, location);
238 }
239 else if (rhs_type->interface_type() != NULL)
240 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
241 rhs_tree, location);
242 else if (lhs_type->is_open_array_type()
243 && rhs_type->is_nil_type())
244 {
245 // Assigning nil to an open array.
246 gcc_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
247
248 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
249
250 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
251 tree field = TYPE_FIELDS(lhs_type_tree);
252 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
253 "__values") == 0);
254 elt->index = field;
255 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
256
257 elt = VEC_quick_push(constructor_elt, init, NULL);
258 field = DECL_CHAIN(field);
259 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
260 "__count") == 0);
261 elt->index = field;
262 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
263
264 elt = VEC_quick_push(constructor_elt, init, NULL);
265 field = DECL_CHAIN(field);
266 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
267 "__capacity") == 0);
268 elt->index = field;
269 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
270
271 tree val = build_constructor(lhs_type_tree, init);
272 TREE_CONSTANT(val) = 1;
273
274 return val;
275 }
276 else if (rhs_type->is_nil_type())
277 {
278 // The left hand side should be a pointer type at the tree
279 // level.
280 gcc_assert(POINTER_TYPE_P(lhs_type_tree));
281 return fold_convert(lhs_type_tree, null_pointer_node);
282 }
283 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
284 {
285 // No conversion is needed.
286 return rhs_tree;
287 }
288 else if (POINTER_TYPE_P(lhs_type_tree)
289 || INTEGRAL_TYPE_P(lhs_type_tree)
290 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
291 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
292 return fold_convert_loc(location, lhs_type_tree, rhs_tree);
293 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
294 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
295 {
296 // This conversion must be permitted by Go, or we wouldn't have
297 // gotten here.
298 gcc_assert(int_size_in_bytes(lhs_type_tree)
299 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
300 return fold_build1_loc(location, VIEW_CONVERT_EXPR, lhs_type_tree,
301 rhs_tree);
302 }
303 else
304 {
305 gcc_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
306 return rhs_tree;
307 }
308 }
309
310 // Return a tree for a conversion from a non-interface type to an
311 // interface type.
312
313 tree
314 Expression::convert_type_to_interface(Translate_context* context,
315 Type* lhs_type, Type* rhs_type,
316 tree rhs_tree, source_location location)
317 {
318 Gogo* gogo = context->gogo();
319 Interface_type* lhs_interface_type = lhs_type->interface_type();
320 bool lhs_is_empty = lhs_interface_type->is_empty();
321
322 // Since RHS_TYPE is a static type, we can create the interface
323 // method table at compile time.
324
325 // When setting an interface to nil, we just set both fields to
326 // NULL.
327 if (rhs_type->is_nil_type())
328 return lhs_type->get_init_tree(gogo, false);
329
330 // This should have been checked already.
331 gcc_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
332
333 tree lhs_type_tree = lhs_type->get_tree(gogo);
334 if (lhs_type_tree == error_mark_node)
335 return error_mark_node;
336
337 // An interface is a tuple. If LHS_TYPE is an empty interface type,
338 // then the first field is the type descriptor for RHS_TYPE.
339 // Otherwise it is the interface method table for RHS_TYPE.
340 tree first_field_value;
341 if (lhs_is_empty)
342 first_field_value = rhs_type->type_descriptor_pointer(gogo);
343 else
344 {
345 // Build the interface method table for this interface and this
346 // object type: a list of function pointers for each interface
347 // method.
348 Named_type* rhs_named_type = rhs_type->named_type();
349 bool is_pointer = false;
350 if (rhs_named_type == NULL)
351 {
352 rhs_named_type = rhs_type->deref()->named_type();
353 is_pointer = true;
354 }
355 tree method_table;
356 if (rhs_named_type == NULL)
357 method_table = null_pointer_node;
358 else
359 method_table =
360 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
361 is_pointer);
362 first_field_value = fold_convert_loc(location, const_ptr_type_node,
363 method_table);
364 }
365
366 // Start building a constructor for the value we will return.
367
368 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
369
370 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
371 tree field = TYPE_FIELDS(lhs_type_tree);
372 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
373 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
374 elt->index = field;
375 elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value);
376
377 elt = VEC_quick_push(constructor_elt, init, NULL);
378 field = DECL_CHAIN(field);
379 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
380 elt->index = field;
381
382 if (rhs_type->points_to() != NULL)
383 {
384 // We are assigning a pointer to the interface; the interface
385 // holds the pointer itself.
386 elt->value = rhs_tree;
387 return build_constructor(lhs_type_tree, init);
388 }
389
390 // We are assigning a non-pointer value to the interface; the
391 // interface gets a copy of the value in the heap.
392
393 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
394
395 tree space = gogo->allocate_memory(rhs_type, object_size, location);
396 space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)),
397 space);
398 space = save_expr(space);
399
400 tree ref = build_fold_indirect_ref_loc(location, space);
401 TREE_THIS_NOTRAP(ref) = 1;
402 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
403 ref, rhs_tree);
404
405 elt->value = fold_convert_loc(location, TREE_TYPE(field), space);
406
407 return build2(COMPOUND_EXPR, lhs_type_tree, set,
408 build_constructor(lhs_type_tree, init));
409 }
410
411 // Return a tree for the type descriptor of RHS_TREE, which has
412 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
413 // NULL.
414
415 tree
416 Expression::get_interface_type_descriptor(Translate_context*,
417 Type* rhs_type, tree rhs_tree,
418 source_location location)
419 {
420 tree rhs_type_tree = TREE_TYPE(rhs_tree);
421 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
422 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
423 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
424 NULL_TREE);
425 if (rhs_type->interface_type()->is_empty())
426 {
427 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
428 "__type_descriptor") == 0);
429 return v;
430 }
431
432 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
433 == 0);
434 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v)));
435 v = save_expr(v);
436 tree v1 = build_fold_indirect_ref_loc(location, v);
437 gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
438 tree f = TYPE_FIELDS(TREE_TYPE(v1));
439 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
440 == 0);
441 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
442
443 tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v,
444 fold_convert_loc(location, TREE_TYPE(v),
445 null_pointer_node));
446 tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node);
447 return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1),
448 eq, n, v1);
449 }
450
451 // Return a tree for the conversion of an interface type to an
452 // interface type.
453
454 tree
455 Expression::convert_interface_to_interface(Translate_context* context,
456 Type *lhs_type, Type *rhs_type,
457 tree rhs_tree, bool for_type_guard,
458 source_location location)
459 {
460 Gogo* gogo = context->gogo();
461 Interface_type* lhs_interface_type = lhs_type->interface_type();
462 bool lhs_is_empty = lhs_interface_type->is_empty();
463
464 tree lhs_type_tree = lhs_type->get_tree(gogo);
465 if (lhs_type_tree == error_mark_node)
466 return error_mark_node;
467
468 // In the general case this requires runtime examination of the type
469 // method table to match it up with the interface methods.
470
471 // FIXME: If all of the methods in the right hand side interface
472 // also appear in the left hand side interface, then we don't need
473 // to do a runtime check, although we still need to build a new
474 // method table.
475
476 // Get the type descriptor for the right hand side. This will be
477 // NULL for a nil interface.
478
479 if (!DECL_P(rhs_tree))
480 rhs_tree = save_expr(rhs_tree);
481
482 tree rhs_type_descriptor =
483 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
484 location);
485
486 // The result is going to be a two element constructor.
487
488 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
489
490 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
491 tree field = TYPE_FIELDS(lhs_type_tree);
492 elt->index = field;
493
494 if (for_type_guard)
495 {
496 // A type assertion fails when converting a nil interface.
497 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
498 static tree assert_interface_decl;
499 tree call = Gogo::call_builtin(&assert_interface_decl,
500 location,
501 "__go_assert_interface",
502 2,
503 ptr_type_node,
504 TREE_TYPE(lhs_type_descriptor),
505 lhs_type_descriptor,
506 TREE_TYPE(rhs_type_descriptor),
507 rhs_type_descriptor);
508 // This will panic if the interface conversion fails.
509 TREE_NOTHROW(assert_interface_decl) = 0;
510 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
511 }
512 else if (lhs_is_empty)
513 {
514 // A convertion to an empty interface always succeeds, and the
515 // first field is just the type descriptor of the object.
516 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
517 "__type_descriptor") == 0);
518 gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
519 elt->value = rhs_type_descriptor;
520 }
521 else
522 {
523 // A conversion to a non-empty interface may fail, but unlike a
524 // type assertion converting nil will always succeed.
525 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
526 == 0);
527 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
528 static tree convert_interface_decl;
529 tree call = Gogo::call_builtin(&convert_interface_decl,
530 location,
531 "__go_convert_interface",
532 2,
533 ptr_type_node,
534 TREE_TYPE(lhs_type_descriptor),
535 lhs_type_descriptor,
536 TREE_TYPE(rhs_type_descriptor),
537 rhs_type_descriptor);
538 // This will panic if the interface conversion fails.
539 TREE_NOTHROW(convert_interface_decl) = 0;
540 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
541 }
542
543 // The second field is simply the object pointer.
544
545 elt = VEC_quick_push(constructor_elt, init, NULL);
546 field = DECL_CHAIN(field);
547 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
548 elt->index = field;
549
550 tree rhs_type_tree = TREE_TYPE(rhs_tree);
551 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
552 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
553 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
554 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
555 NULL_TREE);
556
557 return build_constructor(lhs_type_tree, init);
558 }
559
560 // Return a tree for the conversion of an interface type to a
561 // non-interface type.
562
563 tree
564 Expression::convert_interface_to_type(Translate_context* context,
565 Type *lhs_type, Type* rhs_type,
566 tree rhs_tree, source_location location)
567 {
568 Gogo* gogo = context->gogo();
569 tree rhs_type_tree = TREE_TYPE(rhs_tree);
570
571 tree lhs_type_tree = lhs_type->get_tree(gogo);
572 if (lhs_type_tree == error_mark_node)
573 return error_mark_node;
574
575 // Call a function to check that the type is valid. The function
576 // will panic with an appropriate runtime type error if the type is
577 // not valid.
578
579 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
580
581 if (!DECL_P(rhs_tree))
582 rhs_tree = save_expr(rhs_tree);
583
584 tree rhs_type_descriptor =
585 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
586 location);
587
588 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo);
589
590 static tree check_interface_type_decl;
591 tree call = Gogo::call_builtin(&check_interface_type_decl,
592 location,
593 "__go_check_interface_type",
594 3,
595 void_type_node,
596 TREE_TYPE(lhs_type_descriptor),
597 lhs_type_descriptor,
598 TREE_TYPE(rhs_type_descriptor),
599 rhs_type_descriptor,
600 TREE_TYPE(rhs_inter_descriptor),
601 rhs_inter_descriptor);
602 // This call will panic if the conversion is invalid.
603 TREE_NOTHROW(check_interface_type_decl) = 0;
604
605 // If the call succeeds, pull out the value.
606 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
607 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
608 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
609 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
610 NULL_TREE);
611
612 // If the value is a pointer, then it is the value we want.
613 // Otherwise it points to the value.
614 if (lhs_type->points_to() == NULL)
615 {
616 val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val);
617 val = build_fold_indirect_ref_loc(location, val);
618 }
619
620 return build2(COMPOUND_EXPR, lhs_type_tree, call,
621 fold_convert_loc(location, lhs_type_tree, val));
622 }
623
624 // Convert an expression to a tree. This is implemented by the child
625 // class. Not that it is not in general safe to call this multiple
626 // times for a single expression, but that we don't catch such errors.
627
628 tree
629 Expression::get_tree(Translate_context* context)
630 {
631 // The child may have marked this expression as having an error.
632 if (this->classification_ == EXPRESSION_ERROR)
633 return error_mark_node;
634
635 return this->do_get_tree(context);
636 }
637
638 // Return a tree for VAL in TYPE.
639
640 tree
641 Expression::integer_constant_tree(mpz_t val, tree type)
642 {
643 if (type == error_mark_node)
644 return error_mark_node;
645 else if (TREE_CODE(type) == INTEGER_TYPE)
646 return double_int_to_tree(type,
647 mpz_get_double_int(type, val, true));
648 else if (TREE_CODE(type) == REAL_TYPE)
649 {
650 mpfr_t fval;
651 mpfr_init_set_z(fval, val, GMP_RNDN);
652 tree ret = Expression::float_constant_tree(fval, type);
653 mpfr_clear(fval);
654 return ret;
655 }
656 else if (TREE_CODE(type) == COMPLEX_TYPE)
657 {
658 mpfr_t fval;
659 mpfr_init_set_z(fval, val, GMP_RNDN);
660 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
661 mpfr_clear(fval);
662 tree imag = build_real_from_int_cst(TREE_TYPE(type),
663 integer_zero_node);
664 return build_complex(type, real, imag);
665 }
666 else
667 gcc_unreachable();
668 }
669
670 // Return a tree for VAL in TYPE.
671
672 tree
673 Expression::float_constant_tree(mpfr_t val, tree type)
674 {
675 if (type == error_mark_node)
676 return error_mark_node;
677 else if (TREE_CODE(type) == INTEGER_TYPE)
678 {
679 mpz_t ival;
680 mpz_init(ival);
681 mpfr_get_z(ival, val, GMP_RNDN);
682 tree ret = Expression::integer_constant_tree(ival, type);
683 mpz_clear(ival);
684 return ret;
685 }
686 else if (TREE_CODE(type) == REAL_TYPE)
687 {
688 REAL_VALUE_TYPE r1;
689 real_from_mpfr(&r1, val, type, GMP_RNDN);
690 REAL_VALUE_TYPE r2;
691 real_convert(&r2, TYPE_MODE(type), &r1);
692 return build_real(type, r2);
693 }
694 else if (TREE_CODE(type) == COMPLEX_TYPE)
695 {
696 REAL_VALUE_TYPE r1;
697 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
698 REAL_VALUE_TYPE r2;
699 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
700 tree imag = build_real_from_int_cst(TREE_TYPE(type),
701 integer_zero_node);
702 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
703 }
704 else
705 gcc_unreachable();
706 }
707
708 // Return a tree for REAL/IMAG in TYPE.
709
710 tree
711 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
712 {
713 if (TREE_CODE(type) == COMPLEX_TYPE)
714 {
715 REAL_VALUE_TYPE r1;
716 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
717 REAL_VALUE_TYPE r2;
718 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
719
720 REAL_VALUE_TYPE r3;
721 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
722 REAL_VALUE_TYPE r4;
723 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
724
725 return build_complex(type, build_real(TREE_TYPE(type), r2),
726 build_real(TREE_TYPE(type), r4));
727 }
728 else
729 gcc_unreachable();
730 }
731
732 // Return a tree which evaluates to true if VAL, of arbitrary integer
733 // type, is negative or is more than the maximum value of BOUND_TYPE.
734 // If SOFAR is not NULL, it is or'red into the result. The return
735 // value may be NULL if SOFAR is NULL.
736
737 tree
738 Expression::check_bounds(tree val, tree bound_type, tree sofar,
739 source_location loc)
740 {
741 tree val_type = TREE_TYPE(val);
742 tree ret = NULL_TREE;
743
744 if (!TYPE_UNSIGNED(val_type))
745 {
746 ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val,
747 build_int_cst(val_type, 0));
748 if (ret == boolean_false_node)
749 ret = NULL_TREE;
750 }
751
752 if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type))
753 || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type))
754 {
755 tree max = TYPE_MAX_VALUE(bound_type);
756 tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val,
757 fold_convert_loc(loc, val_type, max));
758 if (big == boolean_false_node)
759 ;
760 else if (ret == NULL_TREE)
761 ret = big;
762 else
763 ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
764 ret, big);
765 }
766
767 if (ret == NULL_TREE)
768 return sofar;
769 else if (sofar == NULL_TREE)
770 return ret;
771 else
772 return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
773 sofar, ret);
774 }
775
776 // Error expressions. This are used to avoid cascading errors.
777
778 class Error_expression : public Expression
779 {
780 public:
781 Error_expression(source_location location)
782 : Expression(EXPRESSION_ERROR, location)
783 { }
784
785 protected:
786 bool
787 do_is_constant() const
788 { return true; }
789
790 bool
791 do_integer_constant_value(bool, mpz_t val, Type**) const
792 {
793 mpz_set_ui(val, 0);
794 return true;
795 }
796
797 bool
798 do_float_constant_value(mpfr_t val, Type**) const
799 {
800 mpfr_set_ui(val, 0, GMP_RNDN);
801 return true;
802 }
803
804 bool
805 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
806 {
807 mpfr_set_ui(real, 0, GMP_RNDN);
808 mpfr_set_ui(imag, 0, GMP_RNDN);
809 return true;
810 }
811
812 void
813 do_discarding_value()
814 { }
815
816 Type*
817 do_type()
818 { return Type::make_error_type(); }
819
820 void
821 do_determine_type(const Type_context*)
822 { }
823
824 Expression*
825 do_copy()
826 { return this; }
827
828 bool
829 do_is_addressable() const
830 { return true; }
831
832 tree
833 do_get_tree(Translate_context*)
834 { return error_mark_node; }
835 };
836
837 Expression*
838 Expression::make_error(source_location location)
839 {
840 return new Error_expression(location);
841 }
842
843 // An expression which is really a type. This is used during parsing.
844 // It is an error if these survive after lowering.
845
846 class
847 Type_expression : public Expression
848 {
849 public:
850 Type_expression(Type* type, source_location location)
851 : Expression(EXPRESSION_TYPE, location),
852 type_(type)
853 { }
854
855 protected:
856 int
857 do_traverse(Traverse* traverse)
858 { return Type::traverse(this->type_, traverse); }
859
860 Type*
861 do_type()
862 { return this->type_; }
863
864 void
865 do_determine_type(const Type_context*)
866 { }
867
868 void
869 do_check_types(Gogo*)
870 { this->report_error(_("invalid use of type")); }
871
872 Expression*
873 do_copy()
874 { return this; }
875
876 tree
877 do_get_tree(Translate_context*)
878 { gcc_unreachable(); }
879
880 private:
881 // The type which we are representing as an expression.
882 Type* type_;
883 };
884
885 Expression*
886 Expression::make_type(Type* type, source_location location)
887 {
888 return new Type_expression(type, location);
889 }
890
891 // Class Var_expression.
892
893 // Lower a variable expression. Here we just make sure that the
894 // initialization expression of the variable has been lowered. This
895 // ensures that we will be able to determine the type of the variable
896 // if necessary.
897
898 Expression*
899 Var_expression::do_lower(Gogo* gogo, Named_object* function, int)
900 {
901 if (this->variable_->is_variable())
902 {
903 Variable* var = this->variable_->var_value();
904 // This is either a local variable or a global variable. A
905 // reference to a variable which is local to an enclosing
906 // function will be a reference to a field in a closure.
907 if (var->is_global())
908 function = NULL;
909 var->lower_init_expression(gogo, function);
910 }
911 return this;
912 }
913
914 // Return the name of the variable.
915
916 const std::string&
917 Var_expression::name() const
918 {
919 return this->variable_->name();
920 }
921
922 // Return the type of a reference to a variable.
923
924 Type*
925 Var_expression::do_type()
926 {
927 if (this->variable_->is_variable())
928 return this->variable_->var_value()->type();
929 else if (this->variable_->is_result_variable())
930 return this->variable_->result_var_value()->type();
931 else
932 gcc_unreachable();
933 }
934
935 // Something takes the address of this variable. This means that we
936 // may want to move the variable onto the heap.
937
938 void
939 Var_expression::do_address_taken(bool escapes)
940 {
941 if (!escapes)
942 ;
943 else if (this->variable_->is_variable())
944 this->variable_->var_value()->set_address_taken();
945 else if (this->variable_->is_result_variable())
946 this->variable_->result_var_value()->set_address_taken();
947 else
948 gcc_unreachable();
949 }
950
951 // Get the tree for a reference to a variable.
952
953 tree
954 Var_expression::do_get_tree(Translate_context* context)
955 {
956 return this->variable_->get_tree(context->gogo(), context->function());
957 }
958
959 // Make a reference to a variable in an expression.
960
961 Expression*
962 Expression::make_var_reference(Named_object* var, source_location location)
963 {
964 if (var->is_sink())
965 return Expression::make_sink(location);
966
967 // FIXME: Creating a new object for each reference to a variable is
968 // wasteful.
969 return new Var_expression(var, location);
970 }
971
972 // Class Temporary_reference_expression.
973
974 // The type.
975
976 Type*
977 Temporary_reference_expression::do_type()
978 {
979 return this->statement_->type();
980 }
981
982 // Called if something takes the address of this temporary variable.
983 // We never have to move temporary variables to the heap, but we do
984 // need to know that they must live in the stack rather than in a
985 // register.
986
987 void
988 Temporary_reference_expression::do_address_taken(bool)
989 {
990 this->statement_->set_is_address_taken();
991 }
992
993 // Get a tree referring to the variable.
994
995 tree
996 Temporary_reference_expression::do_get_tree(Translate_context*)
997 {
998 return this->statement_->get_decl();
999 }
1000
1001 // Make a reference to a temporary variable.
1002
1003 Expression*
1004 Expression::make_temporary_reference(Temporary_statement* statement,
1005 source_location location)
1006 {
1007 return new Temporary_reference_expression(statement, location);
1008 }
1009
1010 // A sink expression--a use of the blank identifier _.
1011
1012 class Sink_expression : public Expression
1013 {
1014 public:
1015 Sink_expression(source_location location)
1016 : Expression(EXPRESSION_SINK, location),
1017 type_(NULL), var_(NULL_TREE)
1018 { }
1019
1020 protected:
1021 void
1022 do_discarding_value()
1023 { }
1024
1025 Type*
1026 do_type();
1027
1028 void
1029 do_determine_type(const Type_context*);
1030
1031 Expression*
1032 do_copy()
1033 { return new Sink_expression(this->location()); }
1034
1035 tree
1036 do_get_tree(Translate_context*);
1037
1038 private:
1039 // The type of this sink variable.
1040 Type* type_;
1041 // The temporary variable we generate.
1042 tree var_;
1043 };
1044
1045 // Return the type of a sink expression.
1046
1047 Type*
1048 Sink_expression::do_type()
1049 {
1050 if (this->type_ == NULL)
1051 return Type::make_sink_type();
1052 return this->type_;
1053 }
1054
1055 // Determine the type of a sink expression.
1056
1057 void
1058 Sink_expression::do_determine_type(const Type_context* context)
1059 {
1060 if (context->type != NULL)
1061 this->type_ = context->type;
1062 }
1063
1064 // Return a temporary variable for a sink expression. This will
1065 // presumably be a write-only variable which the middle-end will drop.
1066
1067 tree
1068 Sink_expression::do_get_tree(Translate_context* context)
1069 {
1070 if (this->var_ == NULL_TREE)
1071 {
1072 gcc_assert(this->type_ != NULL && !this->type_->is_sink_type());
1073 this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()),
1074 "blank");
1075 }
1076 return this->var_;
1077 }
1078
1079 // Make a sink expression.
1080
1081 Expression*
1082 Expression::make_sink(source_location location)
1083 {
1084 return new Sink_expression(location);
1085 }
1086
1087 // Class Func_expression.
1088
1089 // FIXME: Can a function expression appear in a constant expression?
1090 // The value is unchanging. Initializing a constant to the address of
1091 // a function seems like it could work, though there might be little
1092 // point to it.
1093
1094 // Return the name of the function.
1095
1096 const std::string&
1097 Func_expression::name() const
1098 {
1099 return this->function_->name();
1100 }
1101
1102 // Traversal.
1103
1104 int
1105 Func_expression::do_traverse(Traverse* traverse)
1106 {
1107 return (this->closure_ == NULL
1108 ? TRAVERSE_CONTINUE
1109 : Expression::traverse(&this->closure_, traverse));
1110 }
1111
1112 // Return the type of a function expression.
1113
1114 Type*
1115 Func_expression::do_type()
1116 {
1117 if (this->function_->is_function())
1118 return this->function_->func_value()->type();
1119 else if (this->function_->is_function_declaration())
1120 return this->function_->func_declaration_value()->type();
1121 else
1122 gcc_unreachable();
1123 }
1124
1125 // Get the tree for a function expression without evaluating the
1126 // closure.
1127
1128 tree
1129 Func_expression::get_tree_without_closure(Gogo* gogo)
1130 {
1131 Function_type* fntype;
1132 if (this->function_->is_function())
1133 fntype = this->function_->func_value()->type();
1134 else if (this->function_->is_function_declaration())
1135 fntype = this->function_->func_declaration_value()->type();
1136 else
1137 gcc_unreachable();
1138
1139 // Builtin functions are handled specially by Call_expression. We
1140 // can't take their address.
1141 if (fntype->is_builtin())
1142 {
1143 error_at(this->location(), "invalid use of special builtin function %qs",
1144 this->function_->name().c_str());
1145 return error_mark_node;
1146 }
1147
1148 Named_object* no = this->function_;
1149 tree id = this->function_->get_id(gogo);
1150 tree fndecl;
1151 if (no->is_function())
1152 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1153 else if (no->is_function_declaration())
1154 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1155 else
1156 gcc_unreachable();
1157
1158 return build_fold_addr_expr_loc(this->location(), fndecl);
1159 }
1160
1161 // Get the tree for a function expression. This is used when we take
1162 // the address of a function rather than simply calling it. If the
1163 // function has a closure, we must use a trampoline.
1164
1165 tree
1166 Func_expression::do_get_tree(Translate_context* context)
1167 {
1168 Gogo* gogo = context->gogo();
1169
1170 tree fnaddr = this->get_tree_without_closure(gogo);
1171 if (fnaddr == error_mark_node)
1172 return error_mark_node;
1173
1174 gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1175 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1176 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1177
1178 // For a normal non-nested function call, that is all we have to do.
1179 if (!this->function_->is_function()
1180 || this->function_->func_value()->enclosing() == NULL)
1181 {
1182 gcc_assert(this->closure_ == NULL);
1183 return fnaddr;
1184 }
1185
1186 // For a nested function call, we have to always allocate a
1187 // trampoline. If we don't always allocate, then closures will not
1188 // be reliably distinct.
1189 Expression* closure = this->closure_;
1190 tree closure_tree;
1191 if (closure == NULL)
1192 closure_tree = null_pointer_node;
1193 else
1194 {
1195 // Get the value of the closure. This will be a pointer to
1196 // space allocated on the heap.
1197 closure_tree = closure->get_tree(context);
1198 if (closure_tree == error_mark_node)
1199 return error_mark_node;
1200 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1201 }
1202
1203 // Now we need to build some code on the heap. This code will load
1204 // the static chain pointer with the closure and then jump to the
1205 // body of the function. The normal gcc approach is to build the
1206 // code on the stack. Unfortunately we can not do that, as Go
1207 // permits us to return the function pointer.
1208
1209 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1210 }
1211
1212 // Make a reference to a function in an expression.
1213
1214 Expression*
1215 Expression::make_func_reference(Named_object* function, Expression* closure,
1216 source_location location)
1217 {
1218 return new Func_expression(function, closure, location);
1219 }
1220
1221 // Class Unknown_expression.
1222
1223 // Return the name of an unknown expression.
1224
1225 const std::string&
1226 Unknown_expression::name() const
1227 {
1228 return this->named_object_->name();
1229 }
1230
1231 // Lower a reference to an unknown name.
1232
1233 Expression*
1234 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1235 {
1236 source_location location = this->location();
1237 Named_object* no = this->named_object_;
1238 Named_object* real = no->unknown_value()->real_named_object();
1239 if (real == NULL)
1240 {
1241 if (this->is_composite_literal_key_)
1242 return this;
1243 error_at(location, "reference to undefined name %qs",
1244 this->named_object_->message_name().c_str());
1245 return Expression::make_error(location);
1246 }
1247 switch (real->classification())
1248 {
1249 case Named_object::NAMED_OBJECT_CONST:
1250 return Expression::make_const_reference(real, location);
1251 case Named_object::NAMED_OBJECT_TYPE:
1252 return Expression::make_type(real->type_value(), location);
1253 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1254 if (this->is_composite_literal_key_)
1255 return this;
1256 error_at(location, "reference to undefined type %qs",
1257 real->message_name().c_str());
1258 return Expression::make_error(location);
1259 case Named_object::NAMED_OBJECT_VAR:
1260 return Expression::make_var_reference(real, location);
1261 case Named_object::NAMED_OBJECT_FUNC:
1262 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1263 return Expression::make_func_reference(real, NULL, location);
1264 case Named_object::NAMED_OBJECT_PACKAGE:
1265 if (this->is_composite_literal_key_)
1266 return this;
1267 error_at(location, "unexpected reference to package");
1268 return Expression::make_error(location);
1269 default:
1270 gcc_unreachable();
1271 }
1272 }
1273
1274 // Make a reference to an unknown name.
1275
1276 Expression*
1277 Expression::make_unknown_reference(Named_object* no, source_location location)
1278 {
1279 gcc_assert(no->resolve()->is_unknown());
1280 return new Unknown_expression(no, location);
1281 }
1282
1283 // A boolean expression.
1284
1285 class Boolean_expression : public Expression
1286 {
1287 public:
1288 Boolean_expression(bool val, source_location location)
1289 : Expression(EXPRESSION_BOOLEAN, location),
1290 val_(val), type_(NULL)
1291 { }
1292
1293 static Expression*
1294 do_import(Import*);
1295
1296 protected:
1297 bool
1298 do_is_constant() const
1299 { return true; }
1300
1301 Type*
1302 do_type();
1303
1304 void
1305 do_determine_type(const Type_context*);
1306
1307 Expression*
1308 do_copy()
1309 { return this; }
1310
1311 tree
1312 do_get_tree(Translate_context*)
1313 { return this->val_ ? boolean_true_node : boolean_false_node; }
1314
1315 void
1316 do_export(Export* exp) const
1317 { exp->write_c_string(this->val_ ? "true" : "false"); }
1318
1319 private:
1320 // The constant.
1321 bool val_;
1322 // The type as determined by context.
1323 Type* type_;
1324 };
1325
1326 // Get the type.
1327
1328 Type*
1329 Boolean_expression::do_type()
1330 {
1331 if (this->type_ == NULL)
1332 this->type_ = Type::make_boolean_type();
1333 return this->type_;
1334 }
1335
1336 // Set the type from the context.
1337
1338 void
1339 Boolean_expression::do_determine_type(const Type_context* context)
1340 {
1341 if (this->type_ != NULL && !this->type_->is_abstract())
1342 ;
1343 else if (context->type != NULL && context->type->is_boolean_type())
1344 this->type_ = context->type;
1345 else if (!context->may_be_abstract)
1346 this->type_ = Type::lookup_bool_type();
1347 }
1348
1349 // Import a boolean constant.
1350
1351 Expression*
1352 Boolean_expression::do_import(Import* imp)
1353 {
1354 if (imp->peek_char() == 't')
1355 {
1356 imp->require_c_string("true");
1357 return Expression::make_boolean(true, imp->location());
1358 }
1359 else
1360 {
1361 imp->require_c_string("false");
1362 return Expression::make_boolean(false, imp->location());
1363 }
1364 }
1365
1366 // Make a boolean expression.
1367
1368 Expression*
1369 Expression::make_boolean(bool val, source_location location)
1370 {
1371 return new Boolean_expression(val, location);
1372 }
1373
1374 // Class String_expression.
1375
1376 // Get the type.
1377
1378 Type*
1379 String_expression::do_type()
1380 {
1381 if (this->type_ == NULL)
1382 this->type_ = Type::make_string_type();
1383 return this->type_;
1384 }
1385
1386 // Set the type from the context.
1387
1388 void
1389 String_expression::do_determine_type(const Type_context* context)
1390 {
1391 if (this->type_ != NULL && !this->type_->is_abstract())
1392 ;
1393 else if (context->type != NULL && context->type->is_string_type())
1394 this->type_ = context->type;
1395 else if (!context->may_be_abstract)
1396 this->type_ = Type::lookup_string_type();
1397 }
1398
1399 // Build a string constant.
1400
1401 tree
1402 String_expression::do_get_tree(Translate_context* context)
1403 {
1404 return context->gogo()->go_string_constant_tree(this->val_);
1405 }
1406
1407 // Export a string expression.
1408
1409 void
1410 String_expression::do_export(Export* exp) const
1411 {
1412 std::string s;
1413 s.reserve(this->val_.length() * 4 + 2);
1414 s += '"';
1415 for (std::string::const_iterator p = this->val_.begin();
1416 p != this->val_.end();
1417 ++p)
1418 {
1419 if (*p == '\\' || *p == '"')
1420 {
1421 s += '\\';
1422 s += *p;
1423 }
1424 else if (*p >= 0x20 && *p < 0x7f)
1425 s += *p;
1426 else if (*p == '\n')
1427 s += "\\n";
1428 else if (*p == '\t')
1429 s += "\\t";
1430 else
1431 {
1432 s += "\\x";
1433 unsigned char c = *p;
1434 unsigned int dig = c >> 4;
1435 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1436 dig = c & 0xf;
1437 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1438 }
1439 }
1440 s += '"';
1441 exp->write_string(s);
1442 }
1443
1444 // Import a string expression.
1445
1446 Expression*
1447 String_expression::do_import(Import* imp)
1448 {
1449 imp->require_c_string("\"");
1450 std::string val;
1451 while (true)
1452 {
1453 int c = imp->get_char();
1454 if (c == '"' || c == -1)
1455 break;
1456 if (c != '\\')
1457 val += static_cast<char>(c);
1458 else
1459 {
1460 c = imp->get_char();
1461 if (c == '\\' || c == '"')
1462 val += static_cast<char>(c);
1463 else if (c == 'n')
1464 val += '\n';
1465 else if (c == 't')
1466 val += '\t';
1467 else if (c == 'x')
1468 {
1469 c = imp->get_char();
1470 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1471 c = imp->get_char();
1472 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1473 char v = (vh << 4) | vl;
1474 val += v;
1475 }
1476 else
1477 {
1478 error_at(imp->location(), "bad string constant");
1479 return Expression::make_error(imp->location());
1480 }
1481 }
1482 }
1483 return Expression::make_string(val, imp->location());
1484 }
1485
1486 // Make a string expression.
1487
1488 Expression*
1489 Expression::make_string(const std::string& val, source_location location)
1490 {
1491 return new String_expression(val, location);
1492 }
1493
1494 // Make an integer expression.
1495
1496 class Integer_expression : public Expression
1497 {
1498 public:
1499 Integer_expression(const mpz_t* val, Type* type, source_location location)
1500 : Expression(EXPRESSION_INTEGER, location),
1501 type_(type)
1502 { mpz_init_set(this->val_, *val); }
1503
1504 static Expression*
1505 do_import(Import*);
1506
1507 // Return whether VAL fits in the type.
1508 static bool
1509 check_constant(mpz_t val, Type*, source_location);
1510
1511 // Write VAL to export data.
1512 static void
1513 export_integer(Export* exp, const mpz_t val);
1514
1515 protected:
1516 bool
1517 do_is_constant() const
1518 { return true; }
1519
1520 bool
1521 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1522
1523 Type*
1524 do_type();
1525
1526 void
1527 do_determine_type(const Type_context* context);
1528
1529 void
1530 do_check_types(Gogo*);
1531
1532 tree
1533 do_get_tree(Translate_context*);
1534
1535 Expression*
1536 do_copy()
1537 { return Expression::make_integer(&this->val_, this->type_,
1538 this->location()); }
1539
1540 void
1541 do_export(Export*) const;
1542
1543 private:
1544 // The integer value.
1545 mpz_t val_;
1546 // The type so far.
1547 Type* type_;
1548 };
1549
1550 // Return an integer constant value.
1551
1552 bool
1553 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1554 Type** ptype) const
1555 {
1556 if (this->type_ != NULL)
1557 *ptype = this->type_;
1558 mpz_set(val, this->val_);
1559 return true;
1560 }
1561
1562 // Return the current type. If we haven't set the type yet, we return
1563 // an abstract integer type.
1564
1565 Type*
1566 Integer_expression::do_type()
1567 {
1568 if (this->type_ == NULL)
1569 this->type_ = Type::make_abstract_integer_type();
1570 return this->type_;
1571 }
1572
1573 // Set the type of the integer value. Here we may switch from an
1574 // abstract type to a real type.
1575
1576 void
1577 Integer_expression::do_determine_type(const Type_context* context)
1578 {
1579 if (this->type_ != NULL && !this->type_->is_abstract())
1580 ;
1581 else if (context->type != NULL
1582 && (context->type->integer_type() != NULL
1583 || context->type->float_type() != NULL
1584 || context->type->complex_type() != NULL))
1585 this->type_ = context->type;
1586 else if (!context->may_be_abstract)
1587 this->type_ = Type::lookup_integer_type("int");
1588 }
1589
1590 // Return true if the integer VAL fits in the range of the type TYPE.
1591 // Otherwise give an error and return false. TYPE may be NULL.
1592
1593 bool
1594 Integer_expression::check_constant(mpz_t val, Type* type,
1595 source_location location)
1596 {
1597 if (type == NULL)
1598 return true;
1599 Integer_type* itype = type->integer_type();
1600 if (itype == NULL || itype->is_abstract())
1601 return true;
1602
1603 int bits = mpz_sizeinbase(val, 2);
1604
1605 if (itype->is_unsigned())
1606 {
1607 // For an unsigned type we can only accept a nonnegative number,
1608 // and we must be able to represent at least BITS.
1609 if (mpz_sgn(val) >= 0
1610 && bits <= itype->bits())
1611 return true;
1612 }
1613 else
1614 {
1615 // For a signed type we need an extra bit to indicate the sign.
1616 // We have to handle the most negative integer specially.
1617 if (bits + 1 <= itype->bits()
1618 || (bits <= itype->bits()
1619 && mpz_sgn(val) < 0
1620 && (mpz_scan1(val, 0)
1621 == static_cast<unsigned long>(itype->bits() - 1))
1622 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1623 return true;
1624 }
1625
1626 error_at(location, "integer constant overflow");
1627 return false;
1628 }
1629
1630 // Check the type of an integer constant.
1631
1632 void
1633 Integer_expression::do_check_types(Gogo*)
1634 {
1635 if (this->type_ == NULL)
1636 return;
1637 if (!Integer_expression::check_constant(this->val_, this->type_,
1638 this->location()))
1639 this->set_is_error();
1640 }
1641
1642 // Get a tree for an integer constant.
1643
1644 tree
1645 Integer_expression::do_get_tree(Translate_context* context)
1646 {
1647 Gogo* gogo = context->gogo();
1648 tree type;
1649 if (this->type_ != NULL && !this->type_->is_abstract())
1650 type = this->type_->get_tree(gogo);
1651 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1652 {
1653 // We are converting to an abstract floating point type.
1654 type = Type::lookup_float_type("float64")->get_tree(gogo);
1655 }
1656 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1657 {
1658 // We are converting to an abstract complex type.
1659 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
1660 }
1661 else
1662 {
1663 // If we still have an abstract type here, then this is being
1664 // used in a constant expression which didn't get reduced for
1665 // some reason. Use a type which will fit the value. We use <,
1666 // not <=, because we need an extra bit for the sign bit.
1667 int bits = mpz_sizeinbase(this->val_, 2);
1668 if (bits < INT_TYPE_SIZE)
1669 type = Type::lookup_integer_type("int")->get_tree(gogo);
1670 else if (bits < 64)
1671 type = Type::lookup_integer_type("int64")->get_tree(gogo);
1672 else
1673 type = long_long_integer_type_node;
1674 }
1675 return Expression::integer_constant_tree(this->val_, type);
1676 }
1677
1678 // Write VAL to export data.
1679
1680 void
1681 Integer_expression::export_integer(Export* exp, const mpz_t val)
1682 {
1683 char* s = mpz_get_str(NULL, 10, val);
1684 exp->write_c_string(s);
1685 free(s);
1686 }
1687
1688 // Export an integer in a constant expression.
1689
1690 void
1691 Integer_expression::do_export(Export* exp) const
1692 {
1693 Integer_expression::export_integer(exp, this->val_);
1694 // A trailing space lets us reliably identify the end of the number.
1695 exp->write_c_string(" ");
1696 }
1697
1698 // Import an integer, floating point, or complex value. This handles
1699 // all these types because they all start with digits.
1700
1701 Expression*
1702 Integer_expression::do_import(Import* imp)
1703 {
1704 std::string num = imp->read_identifier();
1705 imp->require_c_string(" ");
1706 if (!num.empty() && num[num.length() - 1] == 'i')
1707 {
1708 mpfr_t real;
1709 size_t plus_pos = num.find('+', 1);
1710 size_t minus_pos = num.find('-', 1);
1711 size_t pos;
1712 if (plus_pos == std::string::npos)
1713 pos = minus_pos;
1714 else if (minus_pos == std::string::npos)
1715 pos = plus_pos;
1716 else
1717 {
1718 error_at(imp->location(), "bad number in import data: %qs",
1719 num.c_str());
1720 return Expression::make_error(imp->location());
1721 }
1722 if (pos == std::string::npos)
1723 mpfr_set_ui(real, 0, GMP_RNDN);
1724 else
1725 {
1726 std::string real_str = num.substr(0, pos);
1727 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1728 {
1729 error_at(imp->location(), "bad number in import data: %qs",
1730 real_str.c_str());
1731 return Expression::make_error(imp->location());
1732 }
1733 }
1734
1735 std::string imag_str;
1736 if (pos == std::string::npos)
1737 imag_str = num;
1738 else
1739 imag_str = num.substr(pos);
1740 imag_str = imag_str.substr(0, imag_str.size() - 1);
1741 mpfr_t imag;
1742 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1743 {
1744 error_at(imp->location(), "bad number in import data: %qs",
1745 imag_str.c_str());
1746 return Expression::make_error(imp->location());
1747 }
1748 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1749 imp->location());
1750 mpfr_clear(real);
1751 mpfr_clear(imag);
1752 return ret;
1753 }
1754 else if (num.find('.') == std::string::npos
1755 && num.find('E') == std::string::npos)
1756 {
1757 mpz_t val;
1758 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1759 {
1760 error_at(imp->location(), "bad number in import data: %qs",
1761 num.c_str());
1762 return Expression::make_error(imp->location());
1763 }
1764 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1765 mpz_clear(val);
1766 return ret;
1767 }
1768 else
1769 {
1770 mpfr_t val;
1771 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1772 {
1773 error_at(imp->location(), "bad number in import data: %qs",
1774 num.c_str());
1775 return Expression::make_error(imp->location());
1776 }
1777 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1778 mpfr_clear(val);
1779 return ret;
1780 }
1781 }
1782
1783 // Build a new integer value.
1784
1785 Expression*
1786 Expression::make_integer(const mpz_t* val, Type* type,
1787 source_location location)
1788 {
1789 return new Integer_expression(val, type, location);
1790 }
1791
1792 // Floats.
1793
1794 class Float_expression : public Expression
1795 {
1796 public:
1797 Float_expression(const mpfr_t* val, Type* type, source_location location)
1798 : Expression(EXPRESSION_FLOAT, location),
1799 type_(type)
1800 {
1801 mpfr_init_set(this->val_, *val, GMP_RNDN);
1802 }
1803
1804 // Constrain VAL to fit into TYPE.
1805 static void
1806 constrain_float(mpfr_t val, Type* type);
1807
1808 // Return whether VAL fits in the type.
1809 static bool
1810 check_constant(mpfr_t val, Type*, source_location);
1811
1812 // Write VAL to export data.
1813 static void
1814 export_float(Export* exp, const mpfr_t val);
1815
1816 protected:
1817 bool
1818 do_is_constant() const
1819 { return true; }
1820
1821 bool
1822 do_float_constant_value(mpfr_t val, Type**) const;
1823
1824 Type*
1825 do_type();
1826
1827 void
1828 do_determine_type(const Type_context*);
1829
1830 void
1831 do_check_types(Gogo*);
1832
1833 Expression*
1834 do_copy()
1835 { return Expression::make_float(&this->val_, this->type_,
1836 this->location()); }
1837
1838 tree
1839 do_get_tree(Translate_context*);
1840
1841 void
1842 do_export(Export*) const;
1843
1844 private:
1845 // The floating point value.
1846 mpfr_t val_;
1847 // The type so far.
1848 Type* type_;
1849 };
1850
1851 // Constrain VAL to fit into TYPE.
1852
1853 void
1854 Float_expression::constrain_float(mpfr_t val, Type* type)
1855 {
1856 Float_type* ftype = type->float_type();
1857 if (ftype != NULL && !ftype->is_abstract())
1858 {
1859 tree type_tree = ftype->type_tree();
1860 REAL_VALUE_TYPE rvt;
1861 real_from_mpfr(&rvt, val, type_tree, GMP_RNDN);
1862 real_convert(&rvt, TYPE_MODE(type_tree), &rvt);
1863 mpfr_from_real(val, &rvt, GMP_RNDN);
1864 }
1865 }
1866
1867 // Return a floating point constant value.
1868
1869 bool
1870 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1871 {
1872 if (this->type_ != NULL)
1873 *ptype = this->type_;
1874 mpfr_set(val, this->val_, GMP_RNDN);
1875 return true;
1876 }
1877
1878 // Return the current type. If we haven't set the type yet, we return
1879 // an abstract float type.
1880
1881 Type*
1882 Float_expression::do_type()
1883 {
1884 if (this->type_ == NULL)
1885 this->type_ = Type::make_abstract_float_type();
1886 return this->type_;
1887 }
1888
1889 // Set the type of the float value. Here we may switch from an
1890 // abstract type to a real type.
1891
1892 void
1893 Float_expression::do_determine_type(const Type_context* context)
1894 {
1895 if (this->type_ != NULL && !this->type_->is_abstract())
1896 ;
1897 else if (context->type != NULL
1898 && (context->type->integer_type() != NULL
1899 || context->type->float_type() != NULL
1900 || context->type->complex_type() != NULL))
1901 this->type_ = context->type;
1902 else if (!context->may_be_abstract)
1903 this->type_ = Type::lookup_float_type("float");
1904 }
1905
1906 // Return true if the floating point value VAL fits in the range of
1907 // the type TYPE. Otherwise give an error and return false. TYPE may
1908 // be NULL.
1909
1910 bool
1911 Float_expression::check_constant(mpfr_t val, Type* type,
1912 source_location location)
1913 {
1914 if (type == NULL)
1915 return true;
1916 Float_type* ftype = type->float_type();
1917 if (ftype == NULL || ftype->is_abstract())
1918 return true;
1919
1920 // A NaN or Infinity always fits in the range of the type.
1921 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1922 return true;
1923
1924 mp_exp_t exp = mpfr_get_exp(val);
1925 mp_exp_t max_exp;
1926 switch (ftype->bits())
1927 {
1928 case 32:
1929 max_exp = 128;
1930 break;
1931 case 64:
1932 max_exp = 1024;
1933 break;
1934 default:
1935 gcc_unreachable();
1936 }
1937 if (exp > max_exp)
1938 {
1939 error_at(location, "floating point constant overflow");
1940 return false;
1941 }
1942 return true;
1943 }
1944
1945 // Check the type of a float value.
1946
1947 void
1948 Float_expression::do_check_types(Gogo*)
1949 {
1950 if (this->type_ == NULL)
1951 return;
1952
1953 if (!Float_expression::check_constant(this->val_, this->type_,
1954 this->location()))
1955 this->set_is_error();
1956
1957 Integer_type* integer_type = this->type_->integer_type();
1958 if (integer_type != NULL)
1959 {
1960 if (!mpfr_integer_p(this->val_))
1961 this->report_error(_("floating point constant truncated to integer"));
1962 else
1963 {
1964 gcc_assert(!integer_type->is_abstract());
1965 mpz_t ival;
1966 mpz_init(ival);
1967 mpfr_get_z(ival, this->val_, GMP_RNDN);
1968 Integer_expression::check_constant(ival, integer_type,
1969 this->location());
1970 mpz_clear(ival);
1971 }
1972 }
1973 }
1974
1975 // Get a tree for a float constant.
1976
1977 tree
1978 Float_expression::do_get_tree(Translate_context* context)
1979 {
1980 Gogo* gogo = context->gogo();
1981 tree type;
1982 if (this->type_ != NULL && !this->type_->is_abstract())
1983 type = this->type_->get_tree(gogo);
1984 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
1985 {
1986 // We have an abstract integer type. We just hope for the best.
1987 type = Type::lookup_integer_type("int")->get_tree(gogo);
1988 }
1989 else
1990 {
1991 // If we still have an abstract type here, then this is being
1992 // used in a constant expression which didn't get reduced. We
1993 // just use float64 and hope for the best.
1994 type = Type::lookup_float_type("float64")->get_tree(gogo);
1995 }
1996 return Expression::float_constant_tree(this->val_, type);
1997 }
1998
1999 // Write a floating point number to export data.
2000
2001 void
2002 Float_expression::export_float(Export *exp, const mpfr_t val)
2003 {
2004 mp_exp_t exponent;
2005 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2006 if (*s == '-')
2007 exp->write_c_string("-");
2008 exp->write_c_string("0.");
2009 exp->write_c_string(*s == '-' ? s + 1 : s);
2010 mpfr_free_str(s);
2011 char buf[30];
2012 snprintf(buf, sizeof buf, "E%ld", exponent);
2013 exp->write_c_string(buf);
2014 }
2015
2016 // Export a floating point number in a constant expression.
2017
2018 void
2019 Float_expression::do_export(Export* exp) const
2020 {
2021 Float_expression::export_float(exp, this->val_);
2022 // A trailing space lets us reliably identify the end of the number.
2023 exp->write_c_string(" ");
2024 }
2025
2026 // Make a float expression.
2027
2028 Expression*
2029 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2030 {
2031 return new Float_expression(val, type, location);
2032 }
2033
2034 // Complex numbers.
2035
2036 class Complex_expression : public Expression
2037 {
2038 public:
2039 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2040 source_location location)
2041 : Expression(EXPRESSION_COMPLEX, location),
2042 type_(type)
2043 {
2044 mpfr_init_set(this->real_, *real, GMP_RNDN);
2045 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2046 }
2047
2048 // Constrain REAL/IMAG to fit into TYPE.
2049 static void
2050 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2051
2052 // Return whether REAL/IMAG fits in the type.
2053 static bool
2054 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2055
2056 // Write REAL/IMAG to export data.
2057 static void
2058 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2059
2060 protected:
2061 bool
2062 do_is_constant() const
2063 { return true; }
2064
2065 bool
2066 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2067
2068 Type*
2069 do_type();
2070
2071 void
2072 do_determine_type(const Type_context*);
2073
2074 void
2075 do_check_types(Gogo*);
2076
2077 Expression*
2078 do_copy()
2079 {
2080 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2081 this->location());
2082 }
2083
2084 tree
2085 do_get_tree(Translate_context*);
2086
2087 void
2088 do_export(Export*) const;
2089
2090 private:
2091 // The real part.
2092 mpfr_t real_;
2093 // The imaginary part;
2094 mpfr_t imag_;
2095 // The type if known.
2096 Type* type_;
2097 };
2098
2099 // Constrain REAL/IMAG to fit into TYPE.
2100
2101 void
2102 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2103 {
2104 Complex_type* ctype = type->complex_type();
2105 if (ctype != NULL && !ctype->is_abstract())
2106 {
2107 tree type_tree = ctype->type_tree();
2108
2109 REAL_VALUE_TYPE rvt;
2110 real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN);
2111 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2112 mpfr_from_real(real, &rvt, GMP_RNDN);
2113
2114 real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN);
2115 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2116 mpfr_from_real(imag, &rvt, GMP_RNDN);
2117 }
2118 }
2119
2120 // Return a complex constant value.
2121
2122 bool
2123 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2124 Type** ptype) const
2125 {
2126 if (this->type_ != NULL)
2127 *ptype = this->type_;
2128 mpfr_set(real, this->real_, GMP_RNDN);
2129 mpfr_set(imag, this->imag_, GMP_RNDN);
2130 return true;
2131 }
2132
2133 // Return the current type. If we haven't set the type yet, we return
2134 // an abstract complex type.
2135
2136 Type*
2137 Complex_expression::do_type()
2138 {
2139 if (this->type_ == NULL)
2140 this->type_ = Type::make_abstract_complex_type();
2141 return this->type_;
2142 }
2143
2144 // Set the type of the complex value. Here we may switch from an
2145 // abstract type to a real type.
2146
2147 void
2148 Complex_expression::do_determine_type(const Type_context* context)
2149 {
2150 if (this->type_ != NULL && !this->type_->is_abstract())
2151 ;
2152 else if (context->type != NULL
2153 && context->type->complex_type() != NULL)
2154 this->type_ = context->type;
2155 else if (!context->may_be_abstract)
2156 this->type_ = Type::lookup_complex_type("complex");
2157 }
2158
2159 // Return true if the complex value REAL/IMAG fits in the range of the
2160 // type TYPE. Otherwise give an error and return false. TYPE may be
2161 // NULL.
2162
2163 bool
2164 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2165 source_location location)
2166 {
2167 if (type == NULL)
2168 return true;
2169 Complex_type* ctype = type->complex_type();
2170 if (ctype == NULL || ctype->is_abstract())
2171 return true;
2172
2173 mp_exp_t max_exp;
2174 switch (ctype->bits())
2175 {
2176 case 64:
2177 max_exp = 128;
2178 break;
2179 case 128:
2180 max_exp = 1024;
2181 break;
2182 default:
2183 gcc_unreachable();
2184 }
2185
2186 // A NaN or Infinity always fits in the range of the type.
2187 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2188 {
2189 if (mpfr_get_exp(real) > max_exp)
2190 {
2191 error_at(location, "complex real part constant overflow");
2192 return false;
2193 }
2194 }
2195
2196 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2197 {
2198 if (mpfr_get_exp(imag) > max_exp)
2199 {
2200 error_at(location, "complex imaginary part constant overflow");
2201 return false;
2202 }
2203 }
2204
2205 return true;
2206 }
2207
2208 // Check the type of a complex value.
2209
2210 void
2211 Complex_expression::do_check_types(Gogo*)
2212 {
2213 if (this->type_ == NULL)
2214 return;
2215
2216 if (!Complex_expression::check_constant(this->real_, this->imag_,
2217 this->type_, this->location()))
2218 this->set_is_error();
2219 }
2220
2221 // Get a tree for a complex constant.
2222
2223 tree
2224 Complex_expression::do_get_tree(Translate_context* context)
2225 {
2226 Gogo* gogo = context->gogo();
2227 tree type;
2228 if (this->type_ != NULL && !this->type_->is_abstract())
2229 type = this->type_->get_tree(gogo);
2230 else
2231 {
2232 // If we still have an abstract type here, this this is being
2233 // used in a constant expression which didn't get reduced. We
2234 // just use complex128 and hope for the best.
2235 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
2236 }
2237 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2238 }
2239
2240 // Write REAL/IMAG to export data.
2241
2242 void
2243 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2244 const mpfr_t imag)
2245 {
2246 if (!mpfr_zero_p(real))
2247 {
2248 Float_expression::export_float(exp, real);
2249 if (mpfr_sgn(imag) > 0)
2250 exp->write_c_string("+");
2251 }
2252 Float_expression::export_float(exp, imag);
2253 exp->write_c_string("i");
2254 }
2255
2256 // Export a complex number in a constant expression.
2257
2258 void
2259 Complex_expression::do_export(Export* exp) const
2260 {
2261 Complex_expression::export_complex(exp, this->real_, this->imag_);
2262 // A trailing space lets us reliably identify the end of the number.
2263 exp->write_c_string(" ");
2264 }
2265
2266 // Make a complex expression.
2267
2268 Expression*
2269 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2270 source_location location)
2271 {
2272 return new Complex_expression(real, imag, type, location);
2273 }
2274
2275 // A reference to a const in an expression.
2276
2277 class Const_expression : public Expression
2278 {
2279 public:
2280 Const_expression(Named_object* constant, source_location location)
2281 : Expression(EXPRESSION_CONST_REFERENCE, location),
2282 constant_(constant), type_(NULL)
2283 { }
2284
2285 const std::string&
2286 name() const
2287 { return this->constant_->name(); }
2288
2289 protected:
2290 Expression*
2291 do_lower(Gogo*, Named_object*, int);
2292
2293 bool
2294 do_is_constant() const
2295 { return true; }
2296
2297 bool
2298 do_integer_constant_value(bool, mpz_t val, Type**) const;
2299
2300 bool
2301 do_float_constant_value(mpfr_t val, Type**) const;
2302
2303 bool
2304 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2305
2306 bool
2307 do_string_constant_value(std::string* val) const
2308 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2309
2310 Type*
2311 do_type();
2312
2313 // The type of a const is set by the declaration, not the use.
2314 void
2315 do_determine_type(const Type_context*);
2316
2317 void
2318 do_check_types(Gogo*);
2319
2320 Expression*
2321 do_copy()
2322 { return this; }
2323
2324 tree
2325 do_get_tree(Translate_context* context);
2326
2327 // When exporting a reference to a const as part of a const
2328 // expression, we export the value. We ignore the fact that it has
2329 // a name.
2330 void
2331 do_export(Export* exp) const
2332 { this->constant_->const_value()->expr()->export_expression(exp); }
2333
2334 private:
2335 // The constant.
2336 Named_object* constant_;
2337 // The type of this reference. This is used if the constant has an
2338 // abstract type.
2339 Type* type_;
2340 };
2341
2342 // Lower a constant expression. This is where we convert the
2343 // predeclared constant iota into an integer value.
2344
2345 Expression*
2346 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2347 {
2348 if (this->constant_->const_value()->expr()->classification()
2349 == EXPRESSION_IOTA)
2350 {
2351 if (iota_value == -1)
2352 {
2353 error_at(this->location(),
2354 "iota is only defined in const declarations");
2355 iota_value = 0;
2356 }
2357 mpz_t val;
2358 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2359 Expression* ret = Expression::make_integer(&val, NULL,
2360 this->location());
2361 mpz_clear(val);
2362 return ret;
2363 }
2364
2365 // Make sure that the constant itself has been lowered.
2366 gogo->lower_constant(this->constant_);
2367
2368 return this;
2369 }
2370
2371 // Return an integer constant value.
2372
2373 bool
2374 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2375 Type** ptype) const
2376 {
2377 Type* ctype;
2378 if (this->type_ != NULL)
2379 ctype = this->type_;
2380 else
2381 ctype = this->constant_->const_value()->type();
2382 if (ctype != NULL && ctype->integer_type() == NULL)
2383 return false;
2384
2385 Expression* e = this->constant_->const_value()->expr();
2386 Type* t;
2387 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2388
2389 if (r
2390 && ctype != NULL
2391 && !Integer_expression::check_constant(val, ctype, this->location()))
2392 return false;
2393
2394 *ptype = ctype != NULL ? ctype : t;
2395 return r;
2396 }
2397
2398 // Return a floating point constant value.
2399
2400 bool
2401 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2402 {
2403 Type* ctype;
2404 if (this->type_ != NULL)
2405 ctype = this->type_;
2406 else
2407 ctype = this->constant_->const_value()->type();
2408 if (ctype != NULL && ctype->float_type() == NULL)
2409 return false;
2410
2411 Type* t;
2412 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2413 &t);
2414 if (r && ctype != NULL)
2415 {
2416 if (!Float_expression::check_constant(val, ctype, this->location()))
2417 return false;
2418 Float_expression::constrain_float(val, ctype);
2419 }
2420 *ptype = ctype != NULL ? ctype : t;
2421 return r;
2422 }
2423
2424 // Return a complex constant value.
2425
2426 bool
2427 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2428 Type **ptype) const
2429 {
2430 Type* ctype;
2431 if (this->type_ != NULL)
2432 ctype = this->type_;
2433 else
2434 ctype = this->constant_->const_value()->type();
2435 if (ctype != NULL && ctype->complex_type() == NULL)
2436 return false;
2437
2438 Type *t;
2439 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2440 imag,
2441 &t);
2442 if (r && ctype != NULL)
2443 {
2444 if (!Complex_expression::check_constant(real, imag, ctype,
2445 this->location()))
2446 return false;
2447 Complex_expression::constrain_complex(real, imag, ctype);
2448 }
2449 *ptype = ctype != NULL ? ctype : t;
2450 return r;
2451 }
2452
2453 // Return the type of the const reference.
2454
2455 Type*
2456 Const_expression::do_type()
2457 {
2458 if (this->type_ != NULL)
2459 return this->type_;
2460 Named_constant* nc = this->constant_->const_value();
2461 Type* ret = nc->type();
2462 if (ret != NULL)
2463 return ret;
2464 // During parsing, a named constant may have a NULL type, but we
2465 // must not return a NULL type here.
2466 return nc->expr()->type();
2467 }
2468
2469 // Set the type of the const reference.
2470
2471 void
2472 Const_expression::do_determine_type(const Type_context* context)
2473 {
2474 Type* ctype = this->constant_->const_value()->type();
2475 Type* cetype = (ctype != NULL
2476 ? ctype
2477 : this->constant_->const_value()->expr()->type());
2478 if (ctype != NULL && !ctype->is_abstract())
2479 ;
2480 else if (context->type != NULL
2481 && (context->type->integer_type() != NULL
2482 || context->type->float_type() != NULL
2483 || context->type->complex_type() != NULL)
2484 && (cetype->integer_type() != NULL
2485 || cetype->float_type() != NULL
2486 || cetype->complex_type() != NULL))
2487 this->type_ = context->type;
2488 else if (context->type != NULL
2489 && context->type->is_string_type()
2490 && cetype->is_string_type())
2491 this->type_ = context->type;
2492 else if (context->type != NULL
2493 && context->type->is_boolean_type()
2494 && cetype->is_boolean_type())
2495 this->type_ = context->type;
2496 else if (!context->may_be_abstract)
2497 {
2498 if (cetype->is_abstract())
2499 cetype = cetype->make_non_abstract_type();
2500 this->type_ = cetype;
2501 }
2502 }
2503
2504 // Check types of a const reference.
2505
2506 void
2507 Const_expression::do_check_types(Gogo*)
2508 {
2509 if (this->type_ == NULL || this->type_->is_abstract())
2510 return;
2511
2512 // Check for integer overflow.
2513 if (this->type_->integer_type() != NULL)
2514 {
2515 mpz_t ival;
2516 mpz_init(ival);
2517 Type* dummy;
2518 if (!this->integer_constant_value(true, ival, &dummy))
2519 {
2520 mpfr_t fval;
2521 mpfr_init(fval);
2522 Expression* cexpr = this->constant_->const_value()->expr();
2523 if (cexpr->float_constant_value(fval, &dummy))
2524 {
2525 if (!mpfr_integer_p(fval))
2526 this->report_error(_("floating point constant "
2527 "truncated to integer"));
2528 else
2529 {
2530 mpfr_get_z(ival, fval, GMP_RNDN);
2531 Integer_expression::check_constant(ival, this->type_,
2532 this->location());
2533 }
2534 }
2535 mpfr_clear(fval);
2536 }
2537 mpz_clear(ival);
2538 }
2539 }
2540
2541 // Return a tree for the const reference.
2542
2543 tree
2544 Const_expression::do_get_tree(Translate_context* context)
2545 {
2546 Gogo* gogo = context->gogo();
2547 tree type_tree;
2548 if (this->type_ == NULL)
2549 type_tree = NULL_TREE;
2550 else
2551 {
2552 type_tree = this->type_->get_tree(gogo);
2553 if (type_tree == error_mark_node)
2554 return error_mark_node;
2555 }
2556
2557 // If the type has been set for this expression, but the underlying
2558 // object is an abstract int or float, we try to get the abstract
2559 // value. Otherwise we may lose something in the conversion.
2560 if (this->type_ != NULL
2561 && this->constant_->const_value()->type()->is_abstract())
2562 {
2563 Expression* expr = this->constant_->const_value()->expr();
2564 mpz_t ival;
2565 mpz_init(ival);
2566 Type* t;
2567 if (expr->integer_constant_value(true, ival, &t))
2568 {
2569 tree ret = Expression::integer_constant_tree(ival, type_tree);
2570 mpz_clear(ival);
2571 return ret;
2572 }
2573 mpz_clear(ival);
2574
2575 mpfr_t fval;
2576 mpfr_init(fval);
2577 if (expr->float_constant_value(fval, &t))
2578 {
2579 tree ret = Expression::float_constant_tree(fval, type_tree);
2580 mpfr_clear(fval);
2581 return ret;
2582 }
2583
2584 mpfr_t imag;
2585 mpfr_init(imag);
2586 if (expr->complex_constant_value(fval, imag, &t))
2587 {
2588 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2589 mpfr_clear(fval);
2590 mpfr_clear(imag);
2591 return ret;
2592 }
2593 mpfr_clear(imag);
2594 mpfr_clear(fval);
2595 }
2596
2597 tree const_tree = this->constant_->get_tree(gogo, context->function());
2598 if (this->type_ == NULL
2599 || const_tree == error_mark_node
2600 || TREE_TYPE(const_tree) == error_mark_node)
2601 return const_tree;
2602
2603 tree ret;
2604 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2605 ret = fold_convert(type_tree, const_tree);
2606 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2607 ret = fold(convert_to_integer(type_tree, const_tree));
2608 else if (TREE_CODE(type_tree) == REAL_TYPE)
2609 ret = fold(convert_to_real(type_tree, const_tree));
2610 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2611 ret = fold(convert_to_complex(type_tree, const_tree));
2612 else
2613 gcc_unreachable();
2614 return ret;
2615 }
2616
2617 // Make a reference to a constant in an expression.
2618
2619 Expression*
2620 Expression::make_const_reference(Named_object* constant,
2621 source_location location)
2622 {
2623 return new Const_expression(constant, location);
2624 }
2625
2626 // The nil value.
2627
2628 class Nil_expression : public Expression
2629 {
2630 public:
2631 Nil_expression(source_location location)
2632 : Expression(EXPRESSION_NIL, location)
2633 { }
2634
2635 static Expression*
2636 do_import(Import*);
2637
2638 protected:
2639 bool
2640 do_is_constant() const
2641 { return true; }
2642
2643 Type*
2644 do_type()
2645 { return Type::make_nil_type(); }
2646
2647 void
2648 do_determine_type(const Type_context*)
2649 { }
2650
2651 Expression*
2652 do_copy()
2653 { return this; }
2654
2655 tree
2656 do_get_tree(Translate_context*)
2657 { return null_pointer_node; }
2658
2659 void
2660 do_export(Export* exp) const
2661 { exp->write_c_string("nil"); }
2662 };
2663
2664 // Import a nil expression.
2665
2666 Expression*
2667 Nil_expression::do_import(Import* imp)
2668 {
2669 imp->require_c_string("nil");
2670 return Expression::make_nil(imp->location());
2671 }
2672
2673 // Make a nil expression.
2674
2675 Expression*
2676 Expression::make_nil(source_location location)
2677 {
2678 return new Nil_expression(location);
2679 }
2680
2681 // The value of the predeclared constant iota. This is little more
2682 // than a marker. This will be lowered to an integer in
2683 // Const_expression::do_lower, which is where we know the value that
2684 // it should have.
2685
2686 class Iota_expression : public Parser_expression
2687 {
2688 public:
2689 Iota_expression(source_location location)
2690 : Parser_expression(EXPRESSION_IOTA, location)
2691 { }
2692
2693 protected:
2694 Expression*
2695 do_lower(Gogo*, Named_object*, int)
2696 { gcc_unreachable(); }
2697
2698 // There should only ever be one of these.
2699 Expression*
2700 do_copy()
2701 { gcc_unreachable(); }
2702 };
2703
2704 // Make an iota expression. This is only called for one case: the
2705 // value of the predeclared constant iota.
2706
2707 Expression*
2708 Expression::make_iota()
2709 {
2710 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2711 return &iota_expression;
2712 }
2713
2714 // A type conversion expression.
2715
2716 class Type_conversion_expression : public Expression
2717 {
2718 public:
2719 Type_conversion_expression(Type* type, Expression* expr,
2720 source_location location)
2721 : Expression(EXPRESSION_CONVERSION, location),
2722 type_(type), expr_(expr), may_convert_function_types_(false)
2723 { }
2724
2725 // Return the type to which we are converting.
2726 Type*
2727 type() const
2728 { return this->type_; }
2729
2730 // Return the expression which we are converting.
2731 Expression*
2732 expr() const
2733 { return this->expr_; }
2734
2735 // Permit converting from one function type to another. This is
2736 // used internally for method expressions.
2737 void
2738 set_may_convert_function_types()
2739 {
2740 this->may_convert_function_types_ = true;
2741 }
2742
2743 // Import a type conversion expression.
2744 static Expression*
2745 do_import(Import*);
2746
2747 protected:
2748 int
2749 do_traverse(Traverse* traverse);
2750
2751 Expression*
2752 do_lower(Gogo*, Named_object*, int);
2753
2754 bool
2755 do_is_constant() const
2756 { return this->expr_->is_constant(); }
2757
2758 bool
2759 do_integer_constant_value(bool, mpz_t, Type**) const;
2760
2761 bool
2762 do_float_constant_value(mpfr_t, Type**) const;
2763
2764 bool
2765 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2766
2767 bool
2768 do_string_constant_value(std::string*) const;
2769
2770 Type*
2771 do_type()
2772 { return this->type_; }
2773
2774 void
2775 do_determine_type(const Type_context*)
2776 {
2777 Type_context subcontext(this->type_, false);
2778 this->expr_->determine_type(&subcontext);
2779 }
2780
2781 void
2782 do_check_types(Gogo*);
2783
2784 Expression*
2785 do_copy()
2786 {
2787 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2788 this->location());
2789 }
2790
2791 tree
2792 do_get_tree(Translate_context* context);
2793
2794 void
2795 do_export(Export*) const;
2796
2797 private:
2798 // The type to convert to.
2799 Type* type_;
2800 // The expression to convert.
2801 Expression* expr_;
2802 // True if this is permitted to convert function types. This is
2803 // used internally for method expressions.
2804 bool may_convert_function_types_;
2805 };
2806
2807 // Traversal.
2808
2809 int
2810 Type_conversion_expression::do_traverse(Traverse* traverse)
2811 {
2812 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
2813 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
2814 return TRAVERSE_EXIT;
2815 return TRAVERSE_CONTINUE;
2816 }
2817
2818 // Convert to a constant at lowering time.
2819
2820 Expression*
2821 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
2822 {
2823 Type* type = this->type_;
2824 Expression* val = this->expr_;
2825 source_location location = this->location();
2826
2827 if (type->integer_type() != NULL)
2828 {
2829 mpz_t ival;
2830 mpz_init(ival);
2831 Type* dummy;
2832 if (val->integer_constant_value(false, ival, &dummy))
2833 {
2834 if (!Integer_expression::check_constant(ival, type, location))
2835 mpz_set_ui(ival, 0);
2836 Expression* ret = Expression::make_integer(&ival, type, location);
2837 mpz_clear(ival);
2838 return ret;
2839 }
2840
2841 mpfr_t fval;
2842 mpfr_init(fval);
2843 if (val->float_constant_value(fval, &dummy))
2844 {
2845 if (!mpfr_integer_p(fval))
2846 {
2847 error_at(location,
2848 "floating point constant truncated to integer");
2849 return Expression::make_error(location);
2850 }
2851 mpfr_get_z(ival, fval, GMP_RNDN);
2852 if (!Integer_expression::check_constant(ival, type, location))
2853 mpz_set_ui(ival, 0);
2854 Expression* ret = Expression::make_integer(&ival, type, location);
2855 mpfr_clear(fval);
2856 mpz_clear(ival);
2857 return ret;
2858 }
2859 mpfr_clear(fval);
2860 mpz_clear(ival);
2861 }
2862
2863 if (type->float_type() != NULL)
2864 {
2865 mpfr_t fval;
2866 mpfr_init(fval);
2867 Type* dummy;
2868 if (val->float_constant_value(fval, &dummy))
2869 {
2870 if (!Float_expression::check_constant(fval, type, location))
2871 mpfr_set_ui(fval, 0, GMP_RNDN);
2872 Float_expression::constrain_float(fval, type);
2873 Expression *ret = Expression::make_float(&fval, type, location);
2874 mpfr_clear(fval);
2875 return ret;
2876 }
2877 mpfr_clear(fval);
2878 }
2879
2880 if (type->complex_type() != NULL)
2881 {
2882 mpfr_t real;
2883 mpfr_t imag;
2884 mpfr_init(real);
2885 mpfr_init(imag);
2886 Type* dummy;
2887 if (val->complex_constant_value(real, imag, &dummy))
2888 {
2889 if (!Complex_expression::check_constant(real, imag, type, location))
2890 {
2891 mpfr_set_ui(real, 0, GMP_RNDN);
2892 mpfr_set_ui(imag, 0, GMP_RNDN);
2893 }
2894 Complex_expression::constrain_complex(real, imag, type);
2895 Expression* ret = Expression::make_complex(&real, &imag, type,
2896 location);
2897 mpfr_clear(real);
2898 mpfr_clear(imag);
2899 return ret;
2900 }
2901 mpfr_clear(real);
2902 mpfr_clear(imag);
2903 }
2904
2905 if (type->is_open_array_type() && type->named_type() == NULL)
2906 {
2907 Type* element_type = type->array_type()->element_type()->forwarded();
2908 bool is_byte = element_type == Type::lookup_integer_type("uint8");
2909 bool is_int = element_type == Type::lookup_integer_type("int");
2910 if (is_byte || is_int)
2911 {
2912 std::string s;
2913 if (val->string_constant_value(&s))
2914 {
2915 Expression_list* vals = new Expression_list();
2916 if (is_byte)
2917 {
2918 for (std::string::const_iterator p = s.begin();
2919 p != s.end();
2920 p++)
2921 {
2922 mpz_t val;
2923 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
2924 Expression* v = Expression::make_integer(&val,
2925 element_type,
2926 location);
2927 vals->push_back(v);
2928 mpz_clear(val);
2929 }
2930 }
2931 else
2932 {
2933 const char *p = s.data();
2934 const char *pend = s.data() + s.length();
2935 while (p < pend)
2936 {
2937 unsigned int c;
2938 int adv = Lex::fetch_char(p, &c);
2939 if (adv == 0)
2940 {
2941 warning_at(this->location(), 0,
2942 "invalid UTF-8 encoding");
2943 adv = 1;
2944 }
2945 p += adv;
2946 mpz_t val;
2947 mpz_init_set_ui(val, c);
2948 Expression* v = Expression::make_integer(&val,
2949 element_type,
2950 location);
2951 vals->push_back(v);
2952 mpz_clear(val);
2953 }
2954 }
2955
2956 return Expression::make_slice_composite_literal(type, vals,
2957 location);
2958 }
2959 }
2960 }
2961
2962 return this;
2963 }
2964
2965 // Return the constant integer value if there is one.
2966
2967 bool
2968 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
2969 mpz_t val,
2970 Type** ptype) const
2971 {
2972 if (this->type_->integer_type() == NULL)
2973 return false;
2974
2975 mpz_t ival;
2976 mpz_init(ival);
2977 Type* dummy;
2978 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
2979 {
2980 if (!Integer_expression::check_constant(ival, this->type_,
2981 this->location()))
2982 {
2983 mpz_clear(ival);
2984 return false;
2985 }
2986 mpz_set(val, ival);
2987 mpz_clear(ival);
2988 *ptype = this->type_;
2989 return true;
2990 }
2991 mpz_clear(ival);
2992
2993 mpfr_t fval;
2994 mpfr_init(fval);
2995 if (this->expr_->float_constant_value(fval, &dummy))
2996 {
2997 mpfr_get_z(val, fval, GMP_RNDN);
2998 mpfr_clear(fval);
2999 if (!Integer_expression::check_constant(val, this->type_,
3000 this->location()))
3001 return false;
3002 *ptype = this->type_;
3003 return true;
3004 }
3005 mpfr_clear(fval);
3006
3007 return false;
3008 }
3009
3010 // Return the constant floating point value if there is one.
3011
3012 bool
3013 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3014 Type** ptype) const
3015 {
3016 if (this->type_->float_type() == NULL)
3017 return false;
3018
3019 mpfr_t fval;
3020 mpfr_init(fval);
3021 Type* dummy;
3022 if (this->expr_->float_constant_value(fval, &dummy))
3023 {
3024 if (!Float_expression::check_constant(fval, this->type_,
3025 this->location()))
3026 {
3027 mpfr_clear(fval);
3028 return false;
3029 }
3030 mpfr_set(val, fval, GMP_RNDN);
3031 mpfr_clear(fval);
3032 Float_expression::constrain_float(val, this->type_);
3033 *ptype = this->type_;
3034 return true;
3035 }
3036 mpfr_clear(fval);
3037
3038 return false;
3039 }
3040
3041 // Return the constant complex value if there is one.
3042
3043 bool
3044 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3045 mpfr_t imag,
3046 Type **ptype) const
3047 {
3048 if (this->type_->complex_type() == NULL)
3049 return false;
3050
3051 mpfr_t rval;
3052 mpfr_t ival;
3053 mpfr_init(rval);
3054 mpfr_init(ival);
3055 Type* dummy;
3056 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3057 {
3058 if (!Complex_expression::check_constant(rval, ival, this->type_,
3059 this->location()))
3060 {
3061 mpfr_clear(rval);
3062 mpfr_clear(ival);
3063 return false;
3064 }
3065 mpfr_set(real, rval, GMP_RNDN);
3066 mpfr_set(imag, ival, GMP_RNDN);
3067 mpfr_clear(rval);
3068 mpfr_clear(ival);
3069 Complex_expression::constrain_complex(real, imag, this->type_);
3070 *ptype = this->type_;
3071 return true;
3072 }
3073 mpfr_clear(rval);
3074 mpfr_clear(ival);
3075
3076 return false;
3077 }
3078
3079 // Return the constant string value if there is one.
3080
3081 bool
3082 Type_conversion_expression::do_string_constant_value(std::string* val) const
3083 {
3084 if (this->type_->is_string_type()
3085 && this->expr_->type()->integer_type() != NULL)
3086 {
3087 mpz_t ival;
3088 mpz_init(ival);
3089 Type* dummy;
3090 if (this->expr_->integer_constant_value(false, ival, &dummy))
3091 {
3092 unsigned long ulval = mpz_get_ui(ival);
3093 if (mpz_cmp_ui(ival, ulval) == 0)
3094 {
3095 Lex::append_char(ulval, true, val, this->location());
3096 mpz_clear(ival);
3097 return true;
3098 }
3099 }
3100 mpz_clear(ival);
3101 }
3102
3103 // FIXME: Could handle conversion from const []int here.
3104
3105 return false;
3106 }
3107
3108 // Check that types are convertible.
3109
3110 void
3111 Type_conversion_expression::do_check_types(Gogo*)
3112 {
3113 Type* type = this->type_;
3114 Type* expr_type = this->expr_->type();
3115 std::string reason;
3116
3117 if (this->may_convert_function_types_
3118 && type->function_type() != NULL
3119 && expr_type->function_type() != NULL)
3120 return;
3121
3122 if (Type::are_convertible(type, expr_type, &reason))
3123 return;
3124
3125 error_at(this->location(), "%s", reason.c_str());
3126 this->set_is_error();
3127 }
3128
3129 // Get a tree for a type conversion.
3130
3131 tree
3132 Type_conversion_expression::do_get_tree(Translate_context* context)
3133 {
3134 Gogo* gogo = context->gogo();
3135 tree type_tree = this->type_->get_tree(gogo);
3136 tree expr_tree = this->expr_->get_tree(context);
3137
3138 if (type_tree == error_mark_node
3139 || expr_tree == error_mark_node
3140 || TREE_TYPE(expr_tree) == error_mark_node)
3141 return error_mark_node;
3142
3143 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3144 return fold_convert(type_tree, expr_tree);
3145
3146 Type* type = this->type_;
3147 Type* expr_type = this->expr_->type();
3148 tree ret;
3149 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3150 ret = Expression::convert_for_assignment(context, type, expr_type,
3151 expr_tree, this->location());
3152 else if (type->integer_type() != NULL)
3153 {
3154 if (expr_type->integer_type() != NULL
3155 || expr_type->float_type() != NULL
3156 || expr_type->is_unsafe_pointer_type())
3157 ret = fold(convert_to_integer(type_tree, expr_tree));
3158 else
3159 gcc_unreachable();
3160 }
3161 else if (type->float_type() != NULL)
3162 {
3163 if (expr_type->integer_type() != NULL
3164 || expr_type->float_type() != NULL)
3165 ret = fold(convert_to_real(type_tree, expr_tree));
3166 else
3167 gcc_unreachable();
3168 }
3169 else if (type->complex_type() != NULL)
3170 {
3171 if (expr_type->complex_type() != NULL)
3172 ret = fold(convert_to_complex(type_tree, expr_tree));
3173 else
3174 gcc_unreachable();
3175 }
3176 else if (type->is_string_type()
3177 && expr_type->integer_type() != NULL)
3178 {
3179 expr_tree = fold_convert(integer_type_node, expr_tree);
3180 if (host_integerp(expr_tree, 0))
3181 {
3182 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3183 std::string s;
3184 Lex::append_char(intval, true, &s, this->location());
3185 Expression* se = Expression::make_string(s, this->location());
3186 return se->get_tree(context);
3187 }
3188
3189 static tree int_to_string_fndecl;
3190 ret = Gogo::call_builtin(&int_to_string_fndecl,
3191 this->location(),
3192 "__go_int_to_string",
3193 1,
3194 type_tree,
3195 integer_type_node,
3196 fold_convert(integer_type_node, expr_tree));
3197 }
3198 else if (type->is_string_type()
3199 && (expr_type->array_type() != NULL
3200 || (expr_type->points_to() != NULL
3201 && expr_type->points_to()->array_type() != NULL)))
3202 {
3203 Type* t = expr_type;
3204 if (t->points_to() != NULL)
3205 {
3206 t = t->points_to();
3207 expr_tree = build_fold_indirect_ref(expr_tree);
3208 }
3209 if (!DECL_P(expr_tree))
3210 expr_tree = save_expr(expr_tree);
3211 Array_type* a = t->array_type();
3212 Type* e = a->element_type()->forwarded();
3213 gcc_assert(e->integer_type() != NULL);
3214 tree valptr = fold_convert(const_ptr_type_node,
3215 a->value_pointer_tree(gogo, expr_tree));
3216 tree len = a->length_tree(gogo, expr_tree);
3217 len = fold_convert_loc(this->location(), size_type_node, len);
3218 if (e->integer_type()->is_unsigned()
3219 && e->integer_type()->bits() == 8)
3220 {
3221 static tree byte_array_to_string_fndecl;
3222 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3223 this->location(),
3224 "__go_byte_array_to_string",
3225 2,
3226 type_tree,
3227 const_ptr_type_node,
3228 valptr,
3229 size_type_node,
3230 len);
3231 }
3232 else
3233 {
3234 gcc_assert(e == Type::lookup_integer_type("int"));
3235 static tree int_array_to_string_fndecl;
3236 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3237 this->location(),
3238 "__go_int_array_to_string",
3239 2,
3240 type_tree,
3241 const_ptr_type_node,
3242 valptr,
3243 size_type_node,
3244 len);
3245 }
3246 }
3247 else if (type->is_open_array_type() && expr_type->is_string_type())
3248 {
3249 Type* e = type->array_type()->element_type()->forwarded();
3250 gcc_assert(e->integer_type() != NULL);
3251 if (e->integer_type()->is_unsigned()
3252 && e->integer_type()->bits() == 8)
3253 {
3254 static tree string_to_byte_array_fndecl;
3255 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3256 this->location(),
3257 "__go_string_to_byte_array",
3258 1,
3259 type_tree,
3260 TREE_TYPE(expr_tree),
3261 expr_tree);
3262 }
3263 else
3264 {
3265 gcc_assert(e == Type::lookup_integer_type("int"));
3266 static tree string_to_int_array_fndecl;
3267 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3268 this->location(),
3269 "__go_string_to_int_array",
3270 1,
3271 type_tree,
3272 TREE_TYPE(expr_tree),
3273 expr_tree);
3274 }
3275 }
3276 else if ((type->is_unsafe_pointer_type()
3277 && expr_type->points_to() != NULL)
3278 || (expr_type->is_unsafe_pointer_type()
3279 && type->points_to() != NULL))
3280 ret = fold_convert(type_tree, expr_tree);
3281 else if (type->is_unsafe_pointer_type()
3282 && expr_type->integer_type() != NULL)
3283 ret = convert_to_pointer(type_tree, expr_tree);
3284 else if (this->may_convert_function_types_
3285 && type->function_type() != NULL
3286 && expr_type->function_type() != NULL)
3287 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3288 else
3289 ret = Expression::convert_for_assignment(context, type, expr_type,
3290 expr_tree, this->location());
3291
3292 return ret;
3293 }
3294
3295 // Output a type conversion in a constant expression.
3296
3297 void
3298 Type_conversion_expression::do_export(Export* exp) const
3299 {
3300 exp->write_c_string("convert(");
3301 exp->write_type(this->type_);
3302 exp->write_c_string(", ");
3303 this->expr_->export_expression(exp);
3304 exp->write_c_string(")");
3305 }
3306
3307 // Import a type conversion or a struct construction.
3308
3309 Expression*
3310 Type_conversion_expression::do_import(Import* imp)
3311 {
3312 imp->require_c_string("convert(");
3313 Type* type = imp->read_type();
3314 imp->require_c_string(", ");
3315 Expression* val = Expression::import_expression(imp);
3316 imp->require_c_string(")");
3317 return Expression::make_cast(type, val, imp->location());
3318 }
3319
3320 // Make a type cast expression.
3321
3322 Expression*
3323 Expression::make_cast(Type* type, Expression* val, source_location location)
3324 {
3325 if (type->is_error_type() || val->is_error_expression())
3326 return Expression::make_error(location);
3327 return new Type_conversion_expression(type, val, location);
3328 }
3329
3330 // Unary expressions.
3331
3332 class Unary_expression : public Expression
3333 {
3334 public:
3335 Unary_expression(Operator op, Expression* expr, source_location location)
3336 : Expression(EXPRESSION_UNARY, location),
3337 op_(op), escapes_(true), expr_(expr)
3338 { }
3339
3340 // Return the operator.
3341 Operator
3342 op() const
3343 { return this->op_; }
3344
3345 // Return the operand.
3346 Expression*
3347 operand() const
3348 { return this->expr_; }
3349
3350 // Record that an address expression does not escape.
3351 void
3352 set_does_not_escape()
3353 {
3354 gcc_assert(this->op_ == OPERATOR_AND);
3355 this->escapes_ = false;
3356 }
3357
3358 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3359 // could be done, false if not.
3360 static bool
3361 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3362 source_location);
3363
3364 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3365 // could be done, false if not.
3366 static bool
3367 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3368
3369 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3370 // true if this could be done, false if not.
3371 static bool
3372 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3373 mpfr_t imag);
3374
3375 static Expression*
3376 do_import(Import*);
3377
3378 protected:
3379 int
3380 do_traverse(Traverse* traverse)
3381 { return Expression::traverse(&this->expr_, traverse); }
3382
3383 Expression*
3384 do_lower(Gogo*, Named_object*, int);
3385
3386 bool
3387 do_is_constant() const;
3388
3389 bool
3390 do_integer_constant_value(bool, mpz_t, Type**) const;
3391
3392 bool
3393 do_float_constant_value(mpfr_t, Type**) const;
3394
3395 bool
3396 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3397
3398 Type*
3399 do_type();
3400
3401 void
3402 do_determine_type(const Type_context*);
3403
3404 void
3405 do_check_types(Gogo*);
3406
3407 Expression*
3408 do_copy()
3409 {
3410 return Expression::make_unary(this->op_, this->expr_->copy(),
3411 this->location());
3412 }
3413
3414 bool
3415 do_is_addressable() const
3416 { return this->op_ == OPERATOR_MULT; }
3417
3418 tree
3419 do_get_tree(Translate_context*);
3420
3421 void
3422 do_export(Export*) const;
3423
3424 private:
3425 // The unary operator to apply.
3426 Operator op_;
3427 // Normally true. False if this is an address expression which does
3428 // not escape the current function.
3429 bool escapes_;
3430 // The operand.
3431 Expression* expr_;
3432 };
3433
3434 // If we are taking the address of a composite literal, and the
3435 // contents are not constant, then we want to make a heap composite
3436 // instead.
3437
3438 Expression*
3439 Unary_expression::do_lower(Gogo*, Named_object*, int)
3440 {
3441 source_location loc = this->location();
3442 Operator op = this->op_;
3443 Expression* expr = this->expr_;
3444
3445 if (op == OPERATOR_MULT && expr->is_type_expression())
3446 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3447
3448 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3449 // moving x to the heap. FIXME: Is it worth doing a real escape
3450 // analysis here? This case is found in math/unsafe.go and is
3451 // therefore worth special casing.
3452 if (op == OPERATOR_MULT)
3453 {
3454 Expression* e = expr;
3455 while (e->classification() == EXPRESSION_CONVERSION)
3456 {
3457 Type_conversion_expression* te
3458 = static_cast<Type_conversion_expression*>(e);
3459 e = te->expr();
3460 }
3461
3462 if (e->classification() == EXPRESSION_UNARY)
3463 {
3464 Unary_expression* ue = static_cast<Unary_expression*>(e);
3465 if (ue->op_ == OPERATOR_AND)
3466 {
3467 if (e == expr)
3468 {
3469 // *&x == x.
3470 return ue->expr_;
3471 }
3472 ue->set_does_not_escape();
3473 }
3474 }
3475 }
3476
3477 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3478 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3479 {
3480 Expression* ret = NULL;
3481
3482 mpz_t eval;
3483 mpz_init(eval);
3484 Type* etype;
3485 if (expr->integer_constant_value(false, eval, &etype))
3486 {
3487 mpz_t val;
3488 mpz_init(val);
3489 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3490 ret = Expression::make_integer(&val, etype, loc);
3491 mpz_clear(val);
3492 }
3493 mpz_clear(eval);
3494 if (ret != NULL)
3495 return ret;
3496
3497 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
3498 {
3499 mpfr_t fval;
3500 mpfr_init(fval);
3501 Type* ftype;
3502 if (expr->float_constant_value(fval, &ftype))
3503 {
3504 mpfr_t val;
3505 mpfr_init(val);
3506 if (Unary_expression::eval_float(op, fval, val))
3507 ret = Expression::make_float(&val, ftype, loc);
3508 mpfr_clear(val);
3509 }
3510 if (ret != NULL)
3511 {
3512 mpfr_clear(fval);
3513 return ret;
3514 }
3515
3516 mpfr_t ival;
3517 mpfr_init(ival);
3518 if (expr->complex_constant_value(fval, ival, &ftype))
3519 {
3520 mpfr_t real;
3521 mpfr_t imag;
3522 mpfr_init(real);
3523 mpfr_init(imag);
3524 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
3525 ret = Expression::make_complex(&real, &imag, ftype, loc);
3526 mpfr_clear(real);
3527 mpfr_clear(imag);
3528 }
3529 mpfr_clear(ival);
3530 mpfr_clear(fval);
3531 if (ret != NULL)
3532 return ret;
3533 }
3534 }
3535
3536 return this;
3537 }
3538
3539 // Return whether a unary expression is a constant.
3540
3541 bool
3542 Unary_expression::do_is_constant() const
3543 {
3544 if (this->op_ == OPERATOR_MULT)
3545 {
3546 // Indirecting through a pointer is only constant if the object
3547 // to which the expression points is constant, but we currently
3548 // have no way to determine that.
3549 return false;
3550 }
3551 else if (this->op_ == OPERATOR_AND)
3552 {
3553 // Taking the address of a variable is constant if it is a
3554 // global variable, not constant otherwise. In other cases
3555 // taking the address is probably not a constant.
3556 Var_expression* ve = this->expr_->var_expression();
3557 if (ve != NULL)
3558 {
3559 Named_object* no = ve->named_object();
3560 return no->is_variable() && no->var_value()->is_global();
3561 }
3562 return false;
3563 }
3564 else
3565 return this->expr_->is_constant();
3566 }
3567
3568 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3569 // UVAL, if known; it may be NULL. Return true if this could be done,
3570 // false if not.
3571
3572 bool
3573 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3574 source_location location)
3575 {
3576 switch (op)
3577 {
3578 case OPERATOR_PLUS:
3579 mpz_set(val, uval);
3580 return true;
3581 case OPERATOR_MINUS:
3582 mpz_neg(val, uval);
3583 return Integer_expression::check_constant(val, utype, location);
3584 case OPERATOR_NOT:
3585 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3586 return true;
3587 case OPERATOR_XOR:
3588 if (utype == NULL
3589 || utype->integer_type() == NULL
3590 || utype->integer_type()->is_abstract())
3591 mpz_com(val, uval);
3592 else
3593 {
3594 // The number of HOST_WIDE_INTs that it takes to represent
3595 // UVAL.
3596 size_t count = ((mpz_sizeinbase(uval, 2)
3597 + HOST_BITS_PER_WIDE_INT
3598 - 1)
3599 / HOST_BITS_PER_WIDE_INT);
3600
3601 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3602 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3603
3604 size_t ecount;
3605 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3606 gcc_assert(ecount <= count);
3607
3608 // Trim down to the number of words required by the type.
3609 size_t obits = utype->integer_type()->bits();
3610 if (!utype->integer_type()->is_unsigned())
3611 ++obits;
3612 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3613 / HOST_BITS_PER_WIDE_INT);
3614 gcc_assert(ocount <= ocount);
3615
3616 for (size_t i = 0; i < ocount; ++i)
3617 phwi[i] = ~phwi[i];
3618
3619 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3620 if (clearbits != 0)
3621 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3622 >> clearbits);
3623
3624 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3625
3626 delete[] phwi;
3627 }
3628 return Integer_expression::check_constant(val, utype, location);
3629 case OPERATOR_AND:
3630 case OPERATOR_MULT:
3631 return false;
3632 default:
3633 gcc_unreachable();
3634 }
3635 }
3636
3637 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3638 // could be done, false if not.
3639
3640 bool
3641 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
3642 {
3643 switch (op)
3644 {
3645 case OPERATOR_PLUS:
3646 mpfr_set(val, uval, GMP_RNDN);
3647 return true;
3648 case OPERATOR_MINUS:
3649 mpfr_neg(val, uval, GMP_RNDN);
3650 return true;
3651 case OPERATOR_NOT:
3652 case OPERATOR_XOR:
3653 case OPERATOR_AND:
3654 case OPERATOR_MULT:
3655 return false;
3656 default:
3657 gcc_unreachable();
3658 }
3659 }
3660
3661 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3662 // if this could be done, false if not.
3663
3664 bool
3665 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
3666 mpfr_t real, mpfr_t imag)
3667 {
3668 switch (op)
3669 {
3670 case OPERATOR_PLUS:
3671 mpfr_set(real, rval, GMP_RNDN);
3672 mpfr_set(imag, ival, GMP_RNDN);
3673 return true;
3674 case OPERATOR_MINUS:
3675 mpfr_neg(real, rval, GMP_RNDN);
3676 mpfr_neg(imag, ival, GMP_RNDN);
3677 return true;
3678 case OPERATOR_NOT:
3679 case OPERATOR_XOR:
3680 case OPERATOR_AND:
3681 case OPERATOR_MULT:
3682 return false;
3683 default:
3684 gcc_unreachable();
3685 }
3686 }
3687
3688 // Return the integral constant value of a unary expression, if it has one.
3689
3690 bool
3691 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
3692 Type** ptype) const
3693 {
3694 mpz_t uval;
3695 mpz_init(uval);
3696 bool ret;
3697 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
3698 ret = false;
3699 else
3700 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
3701 this->location());
3702 mpz_clear(uval);
3703 return ret;
3704 }
3705
3706 // Return the floating point constant value of a unary expression, if
3707 // it has one.
3708
3709 bool
3710 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
3711 {
3712 mpfr_t uval;
3713 mpfr_init(uval);
3714 bool ret;
3715 if (!this->expr_->float_constant_value(uval, ptype))
3716 ret = false;
3717 else
3718 ret = Unary_expression::eval_float(this->op_, uval, val);
3719 mpfr_clear(uval);
3720 return ret;
3721 }
3722
3723 // Return the complex constant value of a unary expression, if it has
3724 // one.
3725
3726 bool
3727 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
3728 Type** ptype) const
3729 {
3730 mpfr_t rval;
3731 mpfr_t ival;
3732 mpfr_init(rval);
3733 mpfr_init(ival);
3734 bool ret;
3735 if (!this->expr_->complex_constant_value(rval, ival, ptype))
3736 ret = false;
3737 else
3738 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
3739 mpfr_clear(rval);
3740 mpfr_clear(ival);
3741 return ret;
3742 }
3743
3744 // Return the type of a unary expression.
3745
3746 Type*
3747 Unary_expression::do_type()
3748 {
3749 switch (this->op_)
3750 {
3751 case OPERATOR_PLUS:
3752 case OPERATOR_MINUS:
3753 case OPERATOR_NOT:
3754 case OPERATOR_XOR:
3755 return this->expr_->type();
3756
3757 case OPERATOR_AND:
3758 return Type::make_pointer_type(this->expr_->type());
3759
3760 case OPERATOR_MULT:
3761 {
3762 Type* subtype = this->expr_->type();
3763 Type* points_to = subtype->points_to();
3764 if (points_to == NULL)
3765 return Type::make_error_type();
3766 return points_to;
3767 }
3768
3769 default:
3770 gcc_unreachable();
3771 }
3772 }
3773
3774 // Determine abstract types for a unary expression.
3775
3776 void
3777 Unary_expression::do_determine_type(const Type_context* context)
3778 {
3779 switch (this->op_)
3780 {
3781 case OPERATOR_PLUS:
3782 case OPERATOR_MINUS:
3783 case OPERATOR_NOT:
3784 case OPERATOR_XOR:
3785 this->expr_->determine_type(context);
3786 break;
3787
3788 case OPERATOR_AND:
3789 // Taking the address of something.
3790 {
3791 Type* subtype = (context->type == NULL
3792 ? NULL
3793 : context->type->points_to());
3794 Type_context subcontext(subtype, false);
3795 this->expr_->determine_type(&subcontext);
3796 }
3797 break;
3798
3799 case OPERATOR_MULT:
3800 // Indirecting through a pointer.
3801 {
3802 Type* subtype = (context->type == NULL
3803 ? NULL
3804 : Type::make_pointer_type(context->type));
3805 Type_context subcontext(subtype, false);
3806 this->expr_->determine_type(&subcontext);
3807 }
3808 break;
3809
3810 default:
3811 gcc_unreachable();
3812 }
3813 }
3814
3815 // Check types for a unary expression.
3816
3817 void
3818 Unary_expression::do_check_types(Gogo*)
3819 {
3820 switch (this->op_)
3821 {
3822 case OPERATOR_PLUS:
3823 case OPERATOR_MINUS:
3824 {
3825 Type* type = this->expr_->type();
3826 if (type->integer_type() == NULL
3827 && type->float_type() == NULL
3828 && type->complex_type() == NULL
3829 && !type->is_error_type())
3830 this->report_error(_("expected numeric type"));
3831 }
3832 break;
3833
3834 case OPERATOR_NOT:
3835 case OPERATOR_XOR:
3836 {
3837 Type* type = this->expr_->type();
3838 if (type->integer_type() == NULL
3839 && !type->is_boolean_type()
3840 && !type->is_error_type())
3841 this->report_error(_("expected integer or boolean type"));
3842 }
3843 break;
3844
3845 case OPERATOR_AND:
3846 if (!this->expr_->is_addressable())
3847 this->report_error(_("invalid operand for unary %<&%>"));
3848 else
3849 this->expr_->address_taken(this->escapes_);
3850 break;
3851
3852 case OPERATOR_MULT:
3853 // Indirecting through a pointer.
3854 {
3855 Type* type = this->expr_->type();
3856 if (type->points_to() == NULL
3857 && !type->is_error_type())
3858 this->report_error(_("expected pointer"));
3859 }
3860 break;
3861
3862 default:
3863 gcc_unreachable();
3864 }
3865 }
3866
3867 // Get a tree for a unary expression.
3868
3869 tree
3870 Unary_expression::do_get_tree(Translate_context* context)
3871 {
3872 tree expr = this->expr_->get_tree(context);
3873 if (expr == error_mark_node)
3874 return error_mark_node;
3875
3876 source_location loc = this->location();
3877 switch (this->op_)
3878 {
3879 case OPERATOR_PLUS:
3880 return expr;
3881
3882 case OPERATOR_MINUS:
3883 {
3884 tree type = TREE_TYPE(expr);
3885 tree compute_type = excess_precision_type(type);
3886 if (compute_type != NULL_TREE)
3887 expr = ::convert(compute_type, expr);
3888 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
3889 (compute_type != NULL_TREE
3890 ? compute_type
3891 : type),
3892 expr);
3893 if (compute_type != NULL_TREE)
3894 ret = ::convert(type, ret);
3895 return ret;
3896 }
3897
3898 case OPERATOR_NOT:
3899 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
3900 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
3901 else
3902 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
3903 build_int_cst(TREE_TYPE(expr), 0));
3904
3905 case OPERATOR_XOR:
3906 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
3907
3908 case OPERATOR_AND:
3909 // We should not see a non-constant constructor here; cases
3910 // where we would see one should have been moved onto the heap
3911 // at parse time. Taking the address of a nonconstant
3912 // constructor will not do what the programmer expects.
3913 gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
3914 gcc_assert(TREE_CODE(expr) != ADDR_EXPR);
3915
3916 // Build a decl for a constant constructor.
3917 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
3918 {
3919 tree decl = build_decl(this->location(), VAR_DECL,
3920 create_tmp_var_name("C"), TREE_TYPE(expr));
3921 DECL_EXTERNAL(decl) = 0;
3922 TREE_PUBLIC(decl) = 0;
3923 TREE_READONLY(decl) = 1;
3924 TREE_CONSTANT(decl) = 1;
3925 TREE_STATIC(decl) = 1;
3926 TREE_ADDRESSABLE(decl) = 1;
3927 DECL_ARTIFICIAL(decl) = 1;
3928 DECL_INITIAL(decl) = expr;
3929 rest_of_decl_compilation(decl, 1, 0);
3930 expr = decl;
3931 }
3932
3933 return build_fold_addr_expr_loc(loc, expr);
3934
3935 case OPERATOR_MULT:
3936 {
3937 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
3938
3939 // If we are dereferencing the pointer to a large struct, we
3940 // need to check for nil. We don't bother to check for small
3941 // structs because we expect the system to crash on a nil
3942 // pointer dereference.
3943 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
3944 if (s == -1 || s >= 4096)
3945 {
3946 if (!DECL_P(expr))
3947 expr = save_expr(expr);
3948 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
3949 expr,
3950 fold_convert(TREE_TYPE(expr),
3951 null_pointer_node));
3952 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
3953 loc);
3954 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
3955 build3(COND_EXPR, void_type_node,
3956 compare, crash, NULL_TREE),
3957 expr);
3958 }
3959
3960 // If the type of EXPR is a recursive pointer type, then we
3961 // need to insert a cast before indirecting.
3962 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
3963 {
3964 Type* pt = this->expr_->type()->points_to();
3965 tree ind = pt->get_tree(context->gogo());
3966 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
3967 }
3968
3969 return build_fold_indirect_ref_loc(loc, expr);
3970 }
3971
3972 default:
3973 gcc_unreachable();
3974 }
3975 }
3976
3977 // Export a unary expression.
3978
3979 void
3980 Unary_expression::do_export(Export* exp) const
3981 {
3982 switch (this->op_)
3983 {
3984 case OPERATOR_PLUS:
3985 exp->write_c_string("+ ");
3986 break;
3987 case OPERATOR_MINUS:
3988 exp->write_c_string("- ");
3989 break;
3990 case OPERATOR_NOT:
3991 exp->write_c_string("! ");
3992 break;
3993 case OPERATOR_XOR:
3994 exp->write_c_string("^ ");
3995 break;
3996 case OPERATOR_AND:
3997 case OPERATOR_MULT:
3998 default:
3999 gcc_unreachable();
4000 }
4001 this->expr_->export_expression(exp);
4002 }
4003
4004 // Import a unary expression.
4005
4006 Expression*
4007 Unary_expression::do_import(Import* imp)
4008 {
4009 Operator op;
4010 switch (imp->get_char())
4011 {
4012 case '+':
4013 op = OPERATOR_PLUS;
4014 break;
4015 case '-':
4016 op = OPERATOR_MINUS;
4017 break;
4018 case '!':
4019 op = OPERATOR_NOT;
4020 break;
4021 case '^':
4022 op = OPERATOR_XOR;
4023 break;
4024 default:
4025 gcc_unreachable();
4026 }
4027 imp->require_c_string(" ");
4028 Expression* expr = Expression::import_expression(imp);
4029 return Expression::make_unary(op, expr, imp->location());
4030 }
4031
4032 // Make a unary expression.
4033
4034 Expression*
4035 Expression::make_unary(Operator op, Expression* expr, source_location location)
4036 {
4037 return new Unary_expression(op, expr, location);
4038 }
4039
4040 // If this is an indirection through a pointer, return the expression
4041 // being pointed through. Otherwise return this.
4042
4043 Expression*
4044 Expression::deref()
4045 {
4046 if (this->classification_ == EXPRESSION_UNARY)
4047 {
4048 Unary_expression* ue = static_cast<Unary_expression*>(this);
4049 if (ue->op() == OPERATOR_MULT)
4050 return ue->operand();
4051 }
4052 return this;
4053 }
4054
4055 // Class Binary_expression.
4056
4057 // Traversal.
4058
4059 int
4060 Binary_expression::do_traverse(Traverse* traverse)
4061 {
4062 int t = Expression::traverse(&this->left_, traverse);
4063 if (t == TRAVERSE_EXIT)
4064 return TRAVERSE_EXIT;
4065 return Expression::traverse(&this->right_, traverse);
4066 }
4067
4068 // Compare integer constants according to OP.
4069
4070 bool
4071 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4072 mpz_t right_val)
4073 {
4074 int i = mpz_cmp(left_val, right_val);
4075 switch (op)
4076 {
4077 case OPERATOR_EQEQ:
4078 return i == 0;
4079 case OPERATOR_NOTEQ:
4080 return i != 0;
4081 case OPERATOR_LT:
4082 return i < 0;
4083 case OPERATOR_LE:
4084 return i <= 0;
4085 case OPERATOR_GT:
4086 return i > 0;
4087 case OPERATOR_GE:
4088 return i >= 0;
4089 default:
4090 gcc_unreachable();
4091 }
4092 }
4093
4094 // Compare floating point constants according to OP.
4095
4096 bool
4097 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4098 mpfr_t right_val)
4099 {
4100 int i;
4101 if (type == NULL)
4102 i = mpfr_cmp(left_val, right_val);
4103 else
4104 {
4105 mpfr_t lv;
4106 mpfr_init_set(lv, left_val, GMP_RNDN);
4107 mpfr_t rv;
4108 mpfr_init_set(rv, right_val, GMP_RNDN);
4109 Float_expression::constrain_float(lv, type);
4110 Float_expression::constrain_float(rv, type);
4111 i = mpfr_cmp(lv, rv);
4112 mpfr_clear(lv);
4113 mpfr_clear(rv);
4114 }
4115 switch (op)
4116 {
4117 case OPERATOR_EQEQ:
4118 return i == 0;
4119 case OPERATOR_NOTEQ:
4120 return i != 0;
4121 case OPERATOR_LT:
4122 return i < 0;
4123 case OPERATOR_LE:
4124 return i <= 0;
4125 case OPERATOR_GT:
4126 return i > 0;
4127 case OPERATOR_GE:
4128 return i >= 0;
4129 default:
4130 gcc_unreachable();
4131 }
4132 }
4133
4134 // Compare complex constants according to OP. Complex numbers may
4135 // only be compared for equality.
4136
4137 bool
4138 Binary_expression::compare_complex(Operator op, Type* type,
4139 mpfr_t left_real, mpfr_t left_imag,
4140 mpfr_t right_real, mpfr_t right_imag)
4141 {
4142 bool is_equal;
4143 if (type == NULL)
4144 is_equal = (mpfr_cmp(left_real, right_real) == 0
4145 && mpfr_cmp(left_imag, right_imag) == 0);
4146 else
4147 {
4148 mpfr_t lr;
4149 mpfr_t li;
4150 mpfr_init_set(lr, left_real, GMP_RNDN);
4151 mpfr_init_set(li, left_imag, GMP_RNDN);
4152 mpfr_t rr;
4153 mpfr_t ri;
4154 mpfr_init_set(rr, right_real, GMP_RNDN);
4155 mpfr_init_set(ri, right_imag, GMP_RNDN);
4156 Complex_expression::constrain_complex(lr, li, type);
4157 Complex_expression::constrain_complex(rr, ri, type);
4158 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4159 mpfr_clear(lr);
4160 mpfr_clear(li);
4161 mpfr_clear(rr);
4162 mpfr_clear(ri);
4163 }
4164 switch (op)
4165 {
4166 case OPERATOR_EQEQ:
4167 return is_equal;
4168 case OPERATOR_NOTEQ:
4169 return !is_equal;
4170 default:
4171 gcc_unreachable();
4172 }
4173 }
4174
4175 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4176 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4177 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4178 // this could be done, false if not.
4179
4180 bool
4181 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4182 Type* right_type, mpz_t right_val,
4183 source_location location, mpz_t val)
4184 {
4185 bool is_shift_op = false;
4186 switch (op)
4187 {
4188 case OPERATOR_OROR:
4189 case OPERATOR_ANDAND:
4190 case OPERATOR_EQEQ:
4191 case OPERATOR_NOTEQ:
4192 case OPERATOR_LT:
4193 case OPERATOR_LE:
4194 case OPERATOR_GT:
4195 case OPERATOR_GE:
4196 // These return boolean values. We should probably handle them
4197 // anyhow in case a type conversion is used on the result.
4198 return false;
4199 case OPERATOR_PLUS:
4200 mpz_add(val, left_val, right_val);
4201 break;
4202 case OPERATOR_MINUS:
4203 mpz_sub(val, left_val, right_val);
4204 break;
4205 case OPERATOR_OR:
4206 mpz_ior(val, left_val, right_val);
4207 break;
4208 case OPERATOR_XOR:
4209 mpz_xor(val, left_val, right_val);
4210 break;
4211 case OPERATOR_MULT:
4212 mpz_mul(val, left_val, right_val);
4213 break;
4214 case OPERATOR_DIV:
4215 if (mpz_sgn(right_val) != 0)
4216 mpz_tdiv_q(val, left_val, right_val);
4217 else
4218 {
4219 error_at(location, "division by zero");
4220 mpz_set_ui(val, 0);
4221 return true;
4222 }
4223 break;
4224 case OPERATOR_MOD:
4225 if (mpz_sgn(right_val) != 0)
4226 mpz_tdiv_r(val, left_val, right_val);
4227 else
4228 {
4229 error_at(location, "division by zero");
4230 mpz_set_ui(val, 0);
4231 return true;
4232 }
4233 break;
4234 case OPERATOR_LSHIFT:
4235 {
4236 unsigned long shift = mpz_get_ui(right_val);
4237 if (mpz_cmp_ui(right_val, shift) != 0)
4238 {
4239 error_at(location, "shift count overflow");
4240 mpz_set_ui(val, 0);
4241 return true;
4242 }
4243 mpz_mul_2exp(val, left_val, shift);
4244 is_shift_op = true;
4245 break;
4246 }
4247 break;
4248 case OPERATOR_RSHIFT:
4249 {
4250 unsigned long shift = mpz_get_ui(right_val);
4251 if (mpz_cmp_ui(right_val, shift) != 0)
4252 {
4253 error_at(location, "shift count overflow");
4254 mpz_set_ui(val, 0);
4255 return true;
4256 }
4257 if (mpz_cmp_ui(left_val, 0) >= 0)
4258 mpz_tdiv_q_2exp(val, left_val, shift);
4259 else
4260 mpz_fdiv_q_2exp(val, left_val, shift);
4261 is_shift_op = true;
4262 break;
4263 }
4264 break;
4265 case OPERATOR_AND:
4266 mpz_and(val, left_val, right_val);
4267 break;
4268 case OPERATOR_BITCLEAR:
4269 {
4270 mpz_t tval;
4271 mpz_init(tval);
4272 mpz_com(tval, right_val);
4273 mpz_and(val, left_val, tval);
4274 mpz_clear(tval);
4275 }
4276 break;
4277 default:
4278 gcc_unreachable();
4279 }
4280
4281 Type* type = left_type;
4282 if (!is_shift_op)
4283 {
4284 if (type == NULL)
4285 type = right_type;
4286 else if (type != right_type && right_type != NULL)
4287 {
4288 if (type->is_abstract())
4289 type = right_type;
4290 else if (!right_type->is_abstract())
4291 {
4292 // This look like a type error which should be diagnosed
4293 // elsewhere. Don't do anything here, to avoid an
4294 // unhelpful chain of error messages.
4295 return true;
4296 }
4297 }
4298 }
4299
4300 if (type != NULL && !type->is_abstract())
4301 {
4302 // We have to check the operands too, as we have implicitly
4303 // coerced them to TYPE.
4304 if ((type != left_type
4305 && !Integer_expression::check_constant(left_val, type, location))
4306 || (!is_shift_op
4307 && type != right_type
4308 && !Integer_expression::check_constant(right_val, type,
4309 location))
4310 || !Integer_expression::check_constant(val, type, location))
4311 mpz_set_ui(val, 0);
4312 }
4313
4314 return true;
4315 }
4316
4317 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4318 // Return true if this could be done, false if not.
4319
4320 bool
4321 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4322 Type* right_type, mpfr_t right_val,
4323 mpfr_t val, source_location location)
4324 {
4325 switch (op)
4326 {
4327 case OPERATOR_OROR:
4328 case OPERATOR_ANDAND:
4329 case OPERATOR_EQEQ:
4330 case OPERATOR_NOTEQ:
4331 case OPERATOR_LT:
4332 case OPERATOR_LE:
4333 case OPERATOR_GT:
4334 case OPERATOR_GE:
4335 // These return boolean values. We should probably handle them
4336 // anyhow in case a type conversion is used on the result.
4337 return false;
4338 case OPERATOR_PLUS:
4339 mpfr_add(val, left_val, right_val, GMP_RNDN);
4340 break;
4341 case OPERATOR_MINUS:
4342 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4343 break;
4344 case OPERATOR_OR:
4345 case OPERATOR_XOR:
4346 case OPERATOR_AND:
4347 case OPERATOR_BITCLEAR:
4348 return false;
4349 case OPERATOR_MULT:
4350 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4351 break;
4352 case OPERATOR_DIV:
4353 if (mpfr_zero_p(right_val))
4354 error_at(location, "division by zero");
4355 mpfr_div(val, left_val, right_val, GMP_RNDN);
4356 break;
4357 case OPERATOR_MOD:
4358 return false;
4359 case OPERATOR_LSHIFT:
4360 case OPERATOR_RSHIFT:
4361 return false;
4362 default:
4363 gcc_unreachable();
4364 }
4365
4366 Type* type = left_type;
4367 if (type == NULL)
4368 type = right_type;
4369 else if (type != right_type && right_type != NULL)
4370 {
4371 if (type->is_abstract())
4372 type = right_type;
4373 else if (!right_type->is_abstract())
4374 {
4375 // This looks like a type error which should be diagnosed
4376 // elsewhere. Don't do anything here, to avoid an unhelpful
4377 // chain of error messages.
4378 return true;
4379 }
4380 }
4381
4382 if (type != NULL && !type->is_abstract())
4383 {
4384 if ((type != left_type
4385 && !Float_expression::check_constant(left_val, type, location))
4386 || (type != right_type
4387 && !Float_expression::check_constant(right_val, type,
4388 location))
4389 || !Float_expression::check_constant(val, type, location))
4390 mpfr_set_ui(val, 0, GMP_RNDN);
4391 }
4392
4393 return true;
4394 }
4395
4396 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4397 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4398 // could be done, false if not.
4399
4400 bool
4401 Binary_expression::eval_complex(Operator op, Type* left_type,
4402 mpfr_t left_real, mpfr_t left_imag,
4403 Type *right_type,
4404 mpfr_t right_real, mpfr_t right_imag,
4405 mpfr_t real, mpfr_t imag,
4406 source_location location)
4407 {
4408 switch (op)
4409 {
4410 case OPERATOR_OROR:
4411 case OPERATOR_ANDAND:
4412 case OPERATOR_EQEQ:
4413 case OPERATOR_NOTEQ:
4414 case OPERATOR_LT:
4415 case OPERATOR_LE:
4416 case OPERATOR_GT:
4417 case OPERATOR_GE:
4418 // These return boolean values and must be handled differently.
4419 return false;
4420 case OPERATOR_PLUS:
4421 mpfr_add(real, left_real, right_real, GMP_RNDN);
4422 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4423 break;
4424 case OPERATOR_MINUS:
4425 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4426 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4427 break;
4428 case OPERATOR_OR:
4429 case OPERATOR_XOR:
4430 case OPERATOR_AND:
4431 case OPERATOR_BITCLEAR:
4432 return false;
4433 case OPERATOR_MULT:
4434 {
4435 // You might think that multiplying two complex numbers would
4436 // be simple, and you would be right, until you start to think
4437 // about getting the right answer for infinity. If one
4438 // operand here is infinity and the other is anything other
4439 // than zero or NaN, then we are going to wind up subtracting
4440 // two infinity values. That will give us a NaN, but the
4441 // correct answer is infinity.
4442
4443 mpfr_t lrrr;
4444 mpfr_init(lrrr);
4445 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4446
4447 mpfr_t lrri;
4448 mpfr_init(lrri);
4449 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4450
4451 mpfr_t lirr;
4452 mpfr_init(lirr);
4453 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4454
4455 mpfr_t liri;
4456 mpfr_init(liri);
4457 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4458
4459 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4460 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4461
4462 // If we get NaN on both sides, check whether it should really
4463 // be infinity. The rule is that if either side of the
4464 // complex number is infinity, then the whole value is
4465 // infinity, even if the other side is NaN. So the only case
4466 // we have to fix is the one in which both sides are NaN.
4467 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4468 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4469 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4470 {
4471 bool is_infinity = false;
4472
4473 mpfr_t lr;
4474 mpfr_t li;
4475 mpfr_init_set(lr, left_real, GMP_RNDN);
4476 mpfr_init_set(li, left_imag, GMP_RNDN);
4477
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
4483 // If the left side is infinity, then the result is
4484 // infinity.
4485 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4486 {
4487 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4488 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4489 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4490 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4491 if (mpfr_nan_p(rr))
4492 {
4493 mpfr_set_ui(rr, 0, GMP_RNDN);
4494 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4495 }
4496 if (mpfr_nan_p(ri))
4497 {
4498 mpfr_set_ui(ri, 0, GMP_RNDN);
4499 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4500 }
4501 is_infinity = true;
4502 }
4503
4504 // If the right side is infinity, then the result is
4505 // infinity.
4506 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4507 {
4508 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4509 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4510 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4511 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4512 if (mpfr_nan_p(lr))
4513 {
4514 mpfr_set_ui(lr, 0, GMP_RNDN);
4515 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4516 }
4517 if (mpfr_nan_p(li))
4518 {
4519 mpfr_set_ui(li, 0, GMP_RNDN);
4520 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4521 }
4522 is_infinity = true;
4523 }
4524
4525 // If we got an overflow in the intermediate computations,
4526 // then the result is infinity.
4527 if (!is_infinity
4528 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4529 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4530 {
4531 if (mpfr_nan_p(lr))
4532 {
4533 mpfr_set_ui(lr, 0, GMP_RNDN);
4534 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4535 }
4536 if (mpfr_nan_p(li))
4537 {
4538 mpfr_set_ui(li, 0, GMP_RNDN);
4539 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4540 }
4541 if (mpfr_nan_p(rr))
4542 {
4543 mpfr_set_ui(rr, 0, GMP_RNDN);
4544 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4545 }
4546 if (mpfr_nan_p(ri))
4547 {
4548 mpfr_set_ui(ri, 0, GMP_RNDN);
4549 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4550 }
4551 is_infinity = true;
4552 }
4553
4554 if (is_infinity)
4555 {
4556 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4557 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4558 mpfr_mul(lirr, li, rr, GMP_RNDN);
4559 mpfr_mul(liri, li, ri, GMP_RNDN);
4560 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4561 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4562 mpfr_set_inf(real, mpfr_sgn(real));
4563 mpfr_set_inf(imag, mpfr_sgn(imag));
4564 }
4565
4566 mpfr_clear(lr);
4567 mpfr_clear(li);
4568 mpfr_clear(rr);
4569 mpfr_clear(ri);
4570 }
4571
4572 mpfr_clear(lrrr);
4573 mpfr_clear(lrri);
4574 mpfr_clear(lirr);
4575 mpfr_clear(liri);
4576 }
4577 break;
4578 case OPERATOR_DIV:
4579 {
4580 // For complex division we want to avoid having an
4581 // intermediate overflow turn the whole result in a NaN. We
4582 // scale the values to try to avoid this.
4583
4584 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4585 error_at(location, "division by zero");
4586
4587 mpfr_t rra;
4588 mpfr_t ria;
4589 mpfr_init(rra);
4590 mpfr_init(ria);
4591 mpfr_abs(rra, right_real, GMP_RNDN);
4592 mpfr_abs(ria, right_imag, GMP_RNDN);
4593 mpfr_t t;
4594 mpfr_init(t);
4595 mpfr_max(t, rra, ria, GMP_RNDN);
4596
4597 mpfr_t rr;
4598 mpfr_t ri;
4599 mpfr_init_set(rr, right_real, GMP_RNDN);
4600 mpfr_init_set(ri, right_imag, GMP_RNDN);
4601 long ilogbw = 0;
4602 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4603 {
4604 ilogbw = mpfr_get_exp(t);
4605 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4606 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4607 }
4608
4609 mpfr_t denom;
4610 mpfr_init(denom);
4611 mpfr_mul(denom, rr, rr, GMP_RNDN);
4612 mpfr_mul(t, ri, ri, GMP_RNDN);
4613 mpfr_add(denom, denom, t, GMP_RNDN);
4614
4615 mpfr_mul(real, left_real, rr, GMP_RNDN);
4616 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4617 mpfr_add(real, real, t, GMP_RNDN);
4618 mpfr_div(real, real, denom, GMP_RNDN);
4619 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4620
4621 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4622 mpfr_mul(t, left_real, ri, GMP_RNDN);
4623 mpfr_sub(imag, imag, t, GMP_RNDN);
4624 mpfr_div(imag, imag, denom, GMP_RNDN);
4625 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4626
4627 // If we wind up with NaN on both sides, check whether we
4628 // should really have infinity. The rule is that if either
4629 // side of the complex number is infinity, then the whole
4630 // value is infinity, even if the other side is NaN. So the
4631 // only case we have to fix is the one in which both sides are
4632 // NaN.
4633 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4634 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4635 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4636 {
4637 if (mpfr_zero_p(denom))
4638 {
4639 mpfr_set_inf(real, mpfr_sgn(rr));
4640 mpfr_mul(real, real, left_real, GMP_RNDN);
4641 mpfr_set_inf(imag, mpfr_sgn(rr));
4642 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4643 }
4644 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4645 && mpfr_number_p(rr) && mpfr_number_p(ri))
4646 {
4647 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4648 mpfr_copysign(t, t, left_real, GMP_RNDN);
4649
4650 mpfr_t t2;
4651 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4652 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4653
4654 mpfr_t t3;
4655 mpfr_init(t3);
4656 mpfr_mul(t3, t, rr, GMP_RNDN);
4657
4658 mpfr_t t4;
4659 mpfr_init(t4);
4660 mpfr_mul(t4, t2, ri, GMP_RNDN);
4661
4662 mpfr_add(t3, t3, t4, GMP_RNDN);
4663 mpfr_set_inf(real, mpfr_sgn(t3));
4664
4665 mpfr_mul(t3, t2, rr, GMP_RNDN);
4666 mpfr_mul(t4, t, ri, GMP_RNDN);
4667 mpfr_sub(t3, t3, t4, GMP_RNDN);
4668 mpfr_set_inf(imag, mpfr_sgn(t3));
4669
4670 mpfr_clear(t2);
4671 mpfr_clear(t3);
4672 mpfr_clear(t4);
4673 }
4674 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
4675 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
4676 {
4677 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4678 mpfr_copysign(t, t, rr, GMP_RNDN);
4679
4680 mpfr_t t2;
4681 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4682 mpfr_copysign(t2, t2, ri, GMP_RNDN);
4683
4684 mpfr_t t3;
4685 mpfr_init(t3);
4686 mpfr_mul(t3, left_real, t, GMP_RNDN);
4687
4688 mpfr_t t4;
4689 mpfr_init(t4);
4690 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
4691
4692 mpfr_add(t3, t3, t4, GMP_RNDN);
4693 mpfr_set_ui(real, 0, GMP_RNDN);
4694 mpfr_mul(real, real, t3, GMP_RNDN);
4695
4696 mpfr_mul(t3, left_imag, t, GMP_RNDN);
4697 mpfr_mul(t4, left_real, t2, GMP_RNDN);
4698 mpfr_sub(t3, t3, t4, GMP_RNDN);
4699 mpfr_set_ui(imag, 0, GMP_RNDN);
4700 mpfr_mul(imag, imag, t3, GMP_RNDN);
4701
4702 mpfr_clear(t2);
4703 mpfr_clear(t3);
4704 mpfr_clear(t4);
4705 }
4706 }
4707
4708 mpfr_clear(denom);
4709 mpfr_clear(rr);
4710 mpfr_clear(ri);
4711 mpfr_clear(t);
4712 mpfr_clear(rra);
4713 mpfr_clear(ria);
4714 }
4715 break;
4716 case OPERATOR_MOD:
4717 return false;
4718 case OPERATOR_LSHIFT:
4719 case OPERATOR_RSHIFT:
4720 return false;
4721 default:
4722 gcc_unreachable();
4723 }
4724
4725 Type* type = left_type;
4726 if (type == NULL)
4727 type = right_type;
4728 else if (type != right_type && right_type != NULL)
4729 {
4730 if (type->is_abstract())
4731 type = right_type;
4732 else if (!right_type->is_abstract())
4733 {
4734 // This looks like a type error which should be diagnosed
4735 // elsewhere. Don't do anything here, to avoid an unhelpful
4736 // chain of error messages.
4737 return true;
4738 }
4739 }
4740
4741 if (type != NULL && !type->is_abstract())
4742 {
4743 if ((type != left_type
4744 && !Complex_expression::check_constant(left_real, left_imag,
4745 type, location))
4746 || (type != right_type
4747 && !Complex_expression::check_constant(right_real, right_imag,
4748 type, location))
4749 || !Complex_expression::check_constant(real, imag, type,
4750 location))
4751 {
4752 mpfr_set_ui(real, 0, GMP_RNDN);
4753 mpfr_set_ui(imag, 0, GMP_RNDN);
4754 }
4755 }
4756
4757 return true;
4758 }
4759
4760 // Lower a binary expression. We have to evaluate constant
4761 // expressions now, in order to implement Go's unlimited precision
4762 // constants.
4763
4764 Expression*
4765 Binary_expression::do_lower(Gogo*, Named_object*, int)
4766 {
4767 source_location location = this->location();
4768 Operator op = this->op_;
4769 Expression* left = this->left_;
4770 Expression* right = this->right_;
4771
4772 const bool is_comparison = (op == OPERATOR_EQEQ
4773 || op == OPERATOR_NOTEQ
4774 || op == OPERATOR_LT
4775 || op == OPERATOR_LE
4776 || op == OPERATOR_GT
4777 || op == OPERATOR_GE);
4778
4779 // Integer constant expressions.
4780 {
4781 mpz_t left_val;
4782 mpz_init(left_val);
4783 Type* left_type;
4784 mpz_t right_val;
4785 mpz_init(right_val);
4786 Type* right_type;
4787 if (left->integer_constant_value(false, left_val, &left_type)
4788 && right->integer_constant_value(false, right_val, &right_type))
4789 {
4790 Expression* ret = NULL;
4791 if (left_type != right_type
4792 && left_type != NULL
4793 && right_type != NULL
4794 && left_type->base() != right_type->base()
4795 && op != OPERATOR_LSHIFT
4796 && op != OPERATOR_RSHIFT)
4797 {
4798 // May be a type error--let it be diagnosed later.
4799 }
4800 else if (is_comparison)
4801 {
4802 bool b = Binary_expression::compare_integer(op, left_val,
4803 right_val);
4804 ret = Expression::make_cast(Type::lookup_bool_type(),
4805 Expression::make_boolean(b, location),
4806 location);
4807 }
4808 else
4809 {
4810 mpz_t val;
4811 mpz_init(val);
4812
4813 if (Binary_expression::eval_integer(op, left_type, left_val,
4814 right_type, right_val,
4815 location, val))
4816 {
4817 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
4818 Type* type;
4819 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
4820 type = left_type;
4821 else if (left_type == NULL)
4822 type = right_type;
4823 else if (right_type == NULL)
4824 type = left_type;
4825 else if (!left_type->is_abstract()
4826 && left_type->named_type() != NULL)
4827 type = left_type;
4828 else if (!right_type->is_abstract()
4829 && right_type->named_type() != NULL)
4830 type = right_type;
4831 else if (!left_type->is_abstract())
4832 type = left_type;
4833 else if (!right_type->is_abstract())
4834 type = right_type;
4835 else if (left_type->float_type() != NULL)
4836 type = left_type;
4837 else if (right_type->float_type() != NULL)
4838 type = right_type;
4839 else if (left_type->complex_type() != NULL)
4840 type = left_type;
4841 else if (right_type->complex_type() != NULL)
4842 type = right_type;
4843 else
4844 type = left_type;
4845 ret = Expression::make_integer(&val, type, location);
4846 }
4847
4848 mpz_clear(val);
4849 }
4850
4851 if (ret != NULL)
4852 {
4853 mpz_clear(right_val);
4854 mpz_clear(left_val);
4855 return ret;
4856 }
4857 }
4858 mpz_clear(right_val);
4859 mpz_clear(left_val);
4860 }
4861
4862 // Floating point constant expressions.
4863 {
4864 mpfr_t left_val;
4865 mpfr_init(left_val);
4866 Type* left_type;
4867 mpfr_t right_val;
4868 mpfr_init(right_val);
4869 Type* right_type;
4870 if (left->float_constant_value(left_val, &left_type)
4871 && right->float_constant_value(right_val, &right_type))
4872 {
4873 Expression* ret = NULL;
4874 if (left_type != right_type
4875 && left_type != NULL
4876 && right_type != NULL
4877 && left_type->base() != right_type->base()
4878 && op != OPERATOR_LSHIFT
4879 && op != OPERATOR_RSHIFT)
4880 {
4881 // May be a type error--let it be diagnosed later.
4882 }
4883 else if (is_comparison)
4884 {
4885 bool b = Binary_expression::compare_float(op,
4886 (left_type != NULL
4887 ? left_type
4888 : right_type),
4889 left_val, right_val);
4890 ret = Expression::make_boolean(b, location);
4891 }
4892 else
4893 {
4894 mpfr_t val;
4895 mpfr_init(val);
4896
4897 if (Binary_expression::eval_float(op, left_type, left_val,
4898 right_type, right_val, val,
4899 location))
4900 {
4901 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
4902 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
4903 Type* type;
4904 if (left_type == NULL)
4905 type = right_type;
4906 else if (right_type == NULL)
4907 type = left_type;
4908 else if (!left_type->is_abstract()
4909 && left_type->named_type() != NULL)
4910 type = left_type;
4911 else if (!right_type->is_abstract()
4912 && right_type->named_type() != NULL)
4913 type = right_type;
4914 else if (!left_type->is_abstract())
4915 type = left_type;
4916 else if (!right_type->is_abstract())
4917 type = right_type;
4918 else if (left_type->float_type() != NULL)
4919 type = left_type;
4920 else if (right_type->float_type() != NULL)
4921 type = right_type;
4922 else
4923 type = left_type;
4924 ret = Expression::make_float(&val, type, location);
4925 }
4926
4927 mpfr_clear(val);
4928 }
4929
4930 if (ret != NULL)
4931 {
4932 mpfr_clear(right_val);
4933 mpfr_clear(left_val);
4934 return ret;
4935 }
4936 }
4937 mpfr_clear(right_val);
4938 mpfr_clear(left_val);
4939 }
4940
4941 // Complex constant expressions.
4942 {
4943 mpfr_t left_real;
4944 mpfr_t left_imag;
4945 mpfr_init(left_real);
4946 mpfr_init(left_imag);
4947 Type* left_type;
4948
4949 mpfr_t right_real;
4950 mpfr_t right_imag;
4951 mpfr_init(right_real);
4952 mpfr_init(right_imag);
4953 Type* right_type;
4954
4955 if (left->complex_constant_value(left_real, left_imag, &left_type)
4956 && right->complex_constant_value(right_real, right_imag, &right_type))
4957 {
4958 Expression* ret = NULL;
4959 if (left_type != right_type
4960 && left_type != NULL
4961 && right_type != NULL
4962 && left_type->base() != right_type->base())
4963 {
4964 // May be a type error--let it be diagnosed later.
4965 }
4966 else if (is_comparison)
4967 {
4968 bool b = Binary_expression::compare_complex(op,
4969 (left_type != NULL
4970 ? left_type
4971 : right_type),
4972 left_real,
4973 left_imag,
4974 right_real,
4975 right_imag);
4976 ret = Expression::make_boolean(b, location);
4977 }
4978 else
4979 {
4980 mpfr_t real;
4981 mpfr_t imag;
4982 mpfr_init(real);
4983 mpfr_init(imag);
4984
4985 if (Binary_expression::eval_complex(op, left_type,
4986 left_real, left_imag,
4987 right_type,
4988 right_real, right_imag,
4989 real, imag,
4990 location))
4991 {
4992 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
4993 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
4994 Type* type;
4995 if (left_type == NULL)
4996 type = right_type;
4997 else if (right_type == NULL)
4998 type = left_type;
4999 else if (!left_type->is_abstract()
5000 && left_type->named_type() != NULL)
5001 type = left_type;
5002 else if (!right_type->is_abstract()
5003 && right_type->named_type() != NULL)
5004 type = right_type;
5005 else if (!left_type->is_abstract())
5006 type = left_type;
5007 else if (!right_type->is_abstract())
5008 type = right_type;
5009 else if (left_type->complex_type() != NULL)
5010 type = left_type;
5011 else if (right_type->complex_type() != NULL)
5012 type = right_type;
5013 else
5014 type = left_type;
5015 ret = Expression::make_complex(&real, &imag, type,
5016 location);
5017 }
5018 mpfr_clear(real);
5019 mpfr_clear(imag);
5020 }
5021
5022 if (ret != NULL)
5023 {
5024 mpfr_clear(left_real);
5025 mpfr_clear(left_imag);
5026 mpfr_clear(right_real);
5027 mpfr_clear(right_imag);
5028 return ret;
5029 }
5030 }
5031
5032 mpfr_clear(left_real);
5033 mpfr_clear(left_imag);
5034 mpfr_clear(right_real);
5035 mpfr_clear(right_imag);
5036 }
5037
5038 // String constant expressions.
5039 if (op == OPERATOR_PLUS
5040 && left->type()->is_string_type()
5041 && right->type()->is_string_type())
5042 {
5043 std::string left_string;
5044 std::string right_string;
5045 if (left->string_constant_value(&left_string)
5046 && right->string_constant_value(&right_string))
5047 return Expression::make_string(left_string + right_string, location);
5048 }
5049
5050 return this;
5051 }
5052
5053 // Return the integer constant value, if it has one.
5054
5055 bool
5056 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5057 Type** ptype) const
5058 {
5059 mpz_t left_val;
5060 mpz_init(left_val);
5061 Type* left_type;
5062 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5063 &left_type))
5064 {
5065 mpz_clear(left_val);
5066 return false;
5067 }
5068
5069 mpz_t right_val;
5070 mpz_init(right_val);
5071 Type* right_type;
5072 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5073 &right_type))
5074 {
5075 mpz_clear(right_val);
5076 mpz_clear(left_val);
5077 return false;
5078 }
5079
5080 bool ret;
5081 if (left_type != right_type
5082 && left_type != NULL
5083 && right_type != NULL
5084 && left_type->base() != right_type->base()
5085 && this->op_ != OPERATOR_RSHIFT
5086 && this->op_ != OPERATOR_LSHIFT)
5087 ret = false;
5088 else
5089 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5090 right_type, right_val,
5091 this->location(), val);
5092
5093 mpz_clear(right_val);
5094 mpz_clear(left_val);
5095
5096 if (ret)
5097 *ptype = left_type;
5098
5099 return ret;
5100 }
5101
5102 // Return the floating point constant value, if it has one.
5103
5104 bool
5105 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5106 {
5107 mpfr_t left_val;
5108 mpfr_init(left_val);
5109 Type* left_type;
5110 if (!this->left_->float_constant_value(left_val, &left_type))
5111 {
5112 mpfr_clear(left_val);
5113 return false;
5114 }
5115
5116 mpfr_t right_val;
5117 mpfr_init(right_val);
5118 Type* right_type;
5119 if (!this->right_->float_constant_value(right_val, &right_type))
5120 {
5121 mpfr_clear(right_val);
5122 mpfr_clear(left_val);
5123 return false;
5124 }
5125
5126 bool ret;
5127 if (left_type != right_type
5128 && left_type != NULL
5129 && right_type != NULL
5130 && left_type->base() != right_type->base())
5131 ret = false;
5132 else
5133 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5134 right_type, right_val,
5135 val, this->location());
5136
5137 mpfr_clear(left_val);
5138 mpfr_clear(right_val);
5139
5140 if (ret)
5141 *ptype = left_type;
5142
5143 return ret;
5144 }
5145
5146 // Return the complex constant value, if it has one.
5147
5148 bool
5149 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5150 Type** ptype) const
5151 {
5152 mpfr_t left_real;
5153 mpfr_t left_imag;
5154 mpfr_init(left_real);
5155 mpfr_init(left_imag);
5156 Type* left_type;
5157 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5158 {
5159 mpfr_clear(left_real);
5160 mpfr_clear(left_imag);
5161 return false;
5162 }
5163
5164 mpfr_t right_real;
5165 mpfr_t right_imag;
5166 mpfr_init(right_real);
5167 mpfr_init(right_imag);
5168 Type* right_type;
5169 if (!this->right_->complex_constant_value(right_real, right_imag,
5170 &right_type))
5171 {
5172 mpfr_clear(left_real);
5173 mpfr_clear(left_imag);
5174 mpfr_clear(right_real);
5175 mpfr_clear(right_imag);
5176 return false;
5177 }
5178
5179 bool ret;
5180 if (left_type != right_type
5181 && left_type != NULL
5182 && right_type != NULL
5183 && left_type->base() != right_type->base())
5184 ret = false;
5185 else
5186 ret = Binary_expression::eval_complex(this->op_, left_type,
5187 left_real, left_imag,
5188 right_type,
5189 right_real, right_imag,
5190 real, imag,
5191 this->location());
5192 mpfr_clear(left_real);
5193 mpfr_clear(left_imag);
5194 mpfr_clear(right_real);
5195 mpfr_clear(right_imag);
5196
5197 if (ret)
5198 *ptype = left_type;
5199
5200 return ret;
5201 }
5202
5203 // Note that the value is being discarded.
5204
5205 void
5206 Binary_expression::do_discarding_value()
5207 {
5208 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5209 this->right_->discarding_value();
5210 else
5211 this->warn_about_unused_value();
5212 }
5213
5214 // Get type.
5215
5216 Type*
5217 Binary_expression::do_type()
5218 {
5219 switch (this->op_)
5220 {
5221 case OPERATOR_OROR:
5222 case OPERATOR_ANDAND:
5223 case OPERATOR_EQEQ:
5224 case OPERATOR_NOTEQ:
5225 case OPERATOR_LT:
5226 case OPERATOR_LE:
5227 case OPERATOR_GT:
5228 case OPERATOR_GE:
5229 return Type::lookup_bool_type();
5230
5231 case OPERATOR_PLUS:
5232 case OPERATOR_MINUS:
5233 case OPERATOR_OR:
5234 case OPERATOR_XOR:
5235 case OPERATOR_MULT:
5236 case OPERATOR_DIV:
5237 case OPERATOR_MOD:
5238 case OPERATOR_AND:
5239 case OPERATOR_BITCLEAR:
5240 {
5241 Type* left_type = this->left_->type();
5242 Type* right_type = this->right_->type();
5243 if (!left_type->is_abstract() && left_type->named_type() != NULL)
5244 return left_type;
5245 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5246 return right_type;
5247 else if (!left_type->is_abstract())
5248 return left_type;
5249 else if (!right_type->is_abstract())
5250 return right_type;
5251 else if (left_type->complex_type() != NULL)
5252 return left_type;
5253 else if (right_type->complex_type() != NULL)
5254 return right_type;
5255 else if (left_type->float_type() != NULL)
5256 return left_type;
5257 else if (right_type->float_type() != NULL)
5258 return right_type;
5259 else
5260 return left_type;
5261 }
5262
5263 case OPERATOR_LSHIFT:
5264 case OPERATOR_RSHIFT:
5265 return this->left_->type();
5266
5267 default:
5268 gcc_unreachable();
5269 }
5270 }
5271
5272 // Set type for a binary expression.
5273
5274 void
5275 Binary_expression::do_determine_type(const Type_context* context)
5276 {
5277 Type* tleft = this->left_->type();
5278 Type* tright = this->right_->type();
5279
5280 // Both sides should have the same type, except for the shift
5281 // operations. For a comparison, we should ignore the incoming
5282 // type.
5283
5284 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5285 || this->op_ == OPERATOR_RSHIFT);
5286
5287 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5288 || this->op_ == OPERATOR_NOTEQ
5289 || this->op_ == OPERATOR_LT
5290 || this->op_ == OPERATOR_LE
5291 || this->op_ == OPERATOR_GT
5292 || this->op_ == OPERATOR_GE);
5293
5294 Type_context subcontext(*context);
5295
5296 if (is_comparison)
5297 {
5298 // In a comparison, the context does not determine the types of
5299 // the operands.
5300 subcontext.type = NULL;
5301 }
5302
5303 // Set the context for the left hand operand.
5304 if (is_shift_op)
5305 {
5306 // The right hand operand plays no role in determining the type
5307 // of the left hand operand. A shift of an abstract integer in
5308 // a string context gets special treatment, which may be a
5309 // language bug.
5310 if (subcontext.type != NULL
5311 && subcontext.type->is_string_type()
5312 && tleft->is_abstract())
5313 error_at(this->location(), "shift of non-integer operand");
5314 }
5315 else if (!tleft->is_abstract())
5316 subcontext.type = tleft;
5317 else if (!tright->is_abstract())
5318 subcontext.type = tright;
5319 else if (subcontext.type == NULL)
5320 {
5321 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5322 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5323 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5324 {
5325 // Both sides have an abstract integer, abstract float, or
5326 // abstract complex type. Just let CONTEXT determine
5327 // whether they may remain abstract or not.
5328 }
5329 else if (tleft->complex_type() != NULL)
5330 subcontext.type = tleft;
5331 else if (tright->complex_type() != NULL)
5332 subcontext.type = tright;
5333 else if (tleft->float_type() != NULL)
5334 subcontext.type = tleft;
5335 else if (tright->float_type() != NULL)
5336 subcontext.type = tright;
5337 else
5338 subcontext.type = tleft;
5339 }
5340
5341 this->left_->determine_type(&subcontext);
5342
5343 // The context for the right hand operand is the same as for the
5344 // left hand operand, except for a shift operator.
5345 if (is_shift_op)
5346 {
5347 subcontext.type = Type::lookup_integer_type("uint");
5348 subcontext.may_be_abstract = false;
5349 }
5350
5351 this->right_->determine_type(&subcontext);
5352 }
5353
5354 // Report an error if the binary operator OP does not support TYPE.
5355 // Return whether the operation is OK. This should not be used for
5356 // shift.
5357
5358 bool
5359 Binary_expression::check_operator_type(Operator op, Type* type,
5360 source_location location)
5361 {
5362 switch (op)
5363 {
5364 case OPERATOR_OROR:
5365 case OPERATOR_ANDAND:
5366 if (!type->is_boolean_type())
5367 {
5368 error_at(location, "expected boolean type");
5369 return false;
5370 }
5371 break;
5372
5373 case OPERATOR_EQEQ:
5374 case OPERATOR_NOTEQ:
5375 if (type->integer_type() == NULL
5376 && type->float_type() == NULL
5377 && type->complex_type() == NULL
5378 && !type->is_string_type()
5379 && type->points_to() == NULL
5380 && !type->is_nil_type()
5381 && !type->is_boolean_type()
5382 && type->interface_type() == NULL
5383 && (type->array_type() == NULL
5384 || type->array_type()->length() != NULL)
5385 && type->map_type() == NULL
5386 && type->channel_type() == NULL
5387 && type->function_type() == NULL)
5388 {
5389 error_at(location,
5390 ("expected integer, floating, complex, string, pointer, "
5391 "boolean, interface, slice, map, channel, "
5392 "or function type"));
5393 return false;
5394 }
5395 break;
5396
5397 case OPERATOR_LT:
5398 case OPERATOR_LE:
5399 case OPERATOR_GT:
5400 case OPERATOR_GE:
5401 if (type->integer_type() == NULL
5402 && type->float_type() == NULL
5403 && !type->is_string_type())
5404 {
5405 error_at(location, "expected integer, floating, or string type");
5406 return false;
5407 }
5408 break;
5409
5410 case OPERATOR_PLUS:
5411 case OPERATOR_PLUSEQ:
5412 if (type->integer_type() == NULL
5413 && type->float_type() == NULL
5414 && type->complex_type() == NULL
5415 && !type->is_string_type())
5416 {
5417 error_at(location,
5418 "expected integer, floating, complex, or string type");
5419 return false;
5420 }
5421 break;
5422
5423 case OPERATOR_MINUS:
5424 case OPERATOR_MINUSEQ:
5425 case OPERATOR_MULT:
5426 case OPERATOR_MULTEQ:
5427 case OPERATOR_DIV:
5428 case OPERATOR_DIVEQ:
5429 if (type->integer_type() == NULL
5430 && type->float_type() == NULL
5431 && type->complex_type() == NULL)
5432 {
5433 error_at(location, "expected integer, floating, or complex type");
5434 return false;
5435 }
5436 break;
5437
5438 case OPERATOR_MOD:
5439 case OPERATOR_MODEQ:
5440 case OPERATOR_OR:
5441 case OPERATOR_OREQ:
5442 case OPERATOR_AND:
5443 case OPERATOR_ANDEQ:
5444 case OPERATOR_XOR:
5445 case OPERATOR_XOREQ:
5446 case OPERATOR_BITCLEAR:
5447 case OPERATOR_BITCLEAREQ:
5448 if (type->integer_type() == NULL)
5449 {
5450 error_at(location, "expected integer type");
5451 return false;
5452 }
5453 break;
5454
5455 default:
5456 gcc_unreachable();
5457 }
5458
5459 return true;
5460 }
5461
5462 // Check types.
5463
5464 void
5465 Binary_expression::do_check_types(Gogo*)
5466 {
5467 Type* left_type = this->left_->type();
5468 Type* right_type = this->right_->type();
5469 if (left_type->is_error_type() || right_type->is_error_type())
5470 return;
5471
5472 if (this->op_ == OPERATOR_EQEQ
5473 || this->op_ == OPERATOR_NOTEQ
5474 || this->op_ == OPERATOR_LT
5475 || this->op_ == OPERATOR_LE
5476 || this->op_ == OPERATOR_GT
5477 || this->op_ == OPERATOR_GE)
5478 {
5479 if (!Type::are_assignable(left_type, right_type, NULL)
5480 && !Type::are_assignable(right_type, left_type, NULL))
5481 {
5482 this->report_error(_("incompatible types in binary expression"));
5483 return;
5484 }
5485 if (!Binary_expression::check_operator_type(this->op_, left_type,
5486 this->location())
5487 || !Binary_expression::check_operator_type(this->op_, right_type,
5488 this->location()))
5489 {
5490 this->set_is_error();
5491 return;
5492 }
5493 }
5494 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5495 {
5496 if (!Type::are_compatible_for_binop(left_type, right_type))
5497 {
5498 this->report_error(_("incompatible types in binary expression"));
5499 return;
5500 }
5501 if (!Binary_expression::check_operator_type(this->op_, left_type,
5502 this->location()))
5503 {
5504 this->set_is_error();
5505 return;
5506 }
5507 }
5508 else
5509 {
5510 if (left_type->integer_type() == NULL)
5511 this->report_error(_("shift of non-integer operand"));
5512
5513 if (!right_type->is_abstract()
5514 && (right_type->integer_type() == NULL
5515 || !right_type->integer_type()->is_unsigned()))
5516 this->report_error(_("shift count not unsigned integer"));
5517 else
5518 {
5519 mpz_t val;
5520 mpz_init(val);
5521 Type* type;
5522 if (this->right_->integer_constant_value(true, val, &type))
5523 {
5524 if (mpz_sgn(val) < 0)
5525 this->report_error(_("negative shift count"));
5526 }
5527 mpz_clear(val);
5528 }
5529 }
5530 }
5531
5532 // Get a tree for a binary expression.
5533
5534 tree
5535 Binary_expression::do_get_tree(Translate_context* context)
5536 {
5537 tree left = this->left_->get_tree(context);
5538 tree right = this->right_->get_tree(context);
5539
5540 if (left == error_mark_node || right == error_mark_node)
5541 return error_mark_node;
5542
5543 enum tree_code code;
5544 bool use_left_type = true;
5545 bool is_shift_op = false;
5546 switch (this->op_)
5547 {
5548 case OPERATOR_EQEQ:
5549 case OPERATOR_NOTEQ:
5550 case OPERATOR_LT:
5551 case OPERATOR_LE:
5552 case OPERATOR_GT:
5553 case OPERATOR_GE:
5554 return Expression::comparison_tree(context, this->op_,
5555 this->left_->type(), left,
5556 this->right_->type(), right,
5557 this->location());
5558
5559 case OPERATOR_OROR:
5560 code = TRUTH_ORIF_EXPR;
5561 use_left_type = false;
5562 break;
5563 case OPERATOR_ANDAND:
5564 code = TRUTH_ANDIF_EXPR;
5565 use_left_type = false;
5566 break;
5567 case OPERATOR_PLUS:
5568 code = PLUS_EXPR;
5569 break;
5570 case OPERATOR_MINUS:
5571 code = MINUS_EXPR;
5572 break;
5573 case OPERATOR_OR:
5574 code = BIT_IOR_EXPR;
5575 break;
5576 case OPERATOR_XOR:
5577 code = BIT_XOR_EXPR;
5578 break;
5579 case OPERATOR_MULT:
5580 code = MULT_EXPR;
5581 break;
5582 case OPERATOR_DIV:
5583 {
5584 Type *t = this->left_->type();
5585 if (t->float_type() != NULL || t->complex_type() != NULL)
5586 code = RDIV_EXPR;
5587 else
5588 code = TRUNC_DIV_EXPR;
5589 }
5590 break;
5591 case OPERATOR_MOD:
5592 code = TRUNC_MOD_EXPR;
5593 break;
5594 case OPERATOR_LSHIFT:
5595 code = LSHIFT_EXPR;
5596 is_shift_op = true;
5597 break;
5598 case OPERATOR_RSHIFT:
5599 code = RSHIFT_EXPR;
5600 is_shift_op = true;
5601 break;
5602 case OPERATOR_AND:
5603 code = BIT_AND_EXPR;
5604 break;
5605 case OPERATOR_BITCLEAR:
5606 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5607 code = BIT_AND_EXPR;
5608 break;
5609 default:
5610 gcc_unreachable();
5611 }
5612
5613 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5614
5615 if (this->left_->type()->is_string_type())
5616 {
5617 gcc_assert(this->op_ == OPERATOR_PLUS);
5618 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5619 static tree string_plus_decl;
5620 return Gogo::call_builtin(&string_plus_decl,
5621 this->location(),
5622 "__go_string_plus",
5623 2,
5624 string_type,
5625 string_type,
5626 left,
5627 string_type,
5628 right);
5629 }
5630
5631 tree compute_type = excess_precision_type(type);
5632 if (compute_type != NULL_TREE)
5633 {
5634 left = ::convert(compute_type, left);
5635 right = ::convert(compute_type, right);
5636 }
5637
5638 tree eval_saved = NULL_TREE;
5639 if (is_shift_op)
5640 {
5641 if (!DECL_P(left))
5642 left = save_expr(left);
5643 if (!DECL_P(right))
5644 right = save_expr(right);
5645 // Make sure the values are evaluated.
5646 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
5647 void_type_node, left, right);
5648 }
5649
5650 tree ret = fold_build2_loc(this->location(),
5651 code,
5652 compute_type != NULL_TREE ? compute_type : type,
5653 left, right);
5654
5655 if (compute_type != NULL_TREE)
5656 ret = ::convert(type, ret);
5657
5658 // In Go, a shift larger than the size of the type is well-defined.
5659 // This is not true in GENERIC, so we need to insert a conditional.
5660 if (is_shift_op)
5661 {
5662 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
5663 gcc_assert(this->left_->type()->integer_type() != NULL);
5664 int bits = TYPE_PRECISION(TREE_TYPE(left));
5665
5666 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
5667 build_int_cst_type(TREE_TYPE(right), bits));
5668
5669 tree overflow_result = fold_convert_loc(this->location(),
5670 TREE_TYPE(left),
5671 integer_zero_node);
5672 if (this->op_ == OPERATOR_RSHIFT
5673 && !this->left_->type()->integer_type()->is_unsigned())
5674 {
5675 tree neg = fold_build2_loc(this->location(), LT_EXPR,
5676 boolean_type_node, left,
5677 fold_convert_loc(this->location(),
5678 TREE_TYPE(left),
5679 integer_zero_node));
5680 tree neg_one = fold_build2_loc(this->location(),
5681 MINUS_EXPR, TREE_TYPE(left),
5682 fold_convert_loc(this->location(),
5683 TREE_TYPE(left),
5684 integer_zero_node),
5685 fold_convert_loc(this->location(),
5686 TREE_TYPE(left),
5687 integer_one_node));
5688 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
5689 TREE_TYPE(left), neg, neg_one,
5690 overflow_result);
5691 }
5692
5693 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
5694 compare, ret, overflow_result);
5695
5696 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
5697 TREE_TYPE(ret), eval_saved, ret);
5698 }
5699
5700 return ret;
5701 }
5702
5703 // Export a binary expression.
5704
5705 void
5706 Binary_expression::do_export(Export* exp) const
5707 {
5708 exp->write_c_string("(");
5709 this->left_->export_expression(exp);
5710 switch (this->op_)
5711 {
5712 case OPERATOR_OROR:
5713 exp->write_c_string(" || ");
5714 break;
5715 case OPERATOR_ANDAND:
5716 exp->write_c_string(" && ");
5717 break;
5718 case OPERATOR_EQEQ:
5719 exp->write_c_string(" == ");
5720 break;
5721 case OPERATOR_NOTEQ:
5722 exp->write_c_string(" != ");
5723 break;
5724 case OPERATOR_LT:
5725 exp->write_c_string(" < ");
5726 break;
5727 case OPERATOR_LE:
5728 exp->write_c_string(" <= ");
5729 break;
5730 case OPERATOR_GT:
5731 exp->write_c_string(" > ");
5732 break;
5733 case OPERATOR_GE:
5734 exp->write_c_string(" >= ");
5735 break;
5736 case OPERATOR_PLUS:
5737 exp->write_c_string(" + ");
5738 break;
5739 case OPERATOR_MINUS:
5740 exp->write_c_string(" - ");
5741 break;
5742 case OPERATOR_OR:
5743 exp->write_c_string(" | ");
5744 break;
5745 case OPERATOR_XOR:
5746 exp->write_c_string(" ^ ");
5747 break;
5748 case OPERATOR_MULT:
5749 exp->write_c_string(" * ");
5750 break;
5751 case OPERATOR_DIV:
5752 exp->write_c_string(" / ");
5753 break;
5754 case OPERATOR_MOD:
5755 exp->write_c_string(" % ");
5756 break;
5757 case OPERATOR_LSHIFT:
5758 exp->write_c_string(" << ");
5759 break;
5760 case OPERATOR_RSHIFT:
5761 exp->write_c_string(" >> ");
5762 break;
5763 case OPERATOR_AND:
5764 exp->write_c_string(" & ");
5765 break;
5766 case OPERATOR_BITCLEAR:
5767 exp->write_c_string(" &^ ");
5768 break;
5769 default:
5770 gcc_unreachable();
5771 }
5772 this->right_->export_expression(exp);
5773 exp->write_c_string(")");
5774 }
5775
5776 // Import a binary expression.
5777
5778 Expression*
5779 Binary_expression::do_import(Import* imp)
5780 {
5781 imp->require_c_string("(");
5782
5783 Expression* left = Expression::import_expression(imp);
5784
5785 Operator op;
5786 if (imp->match_c_string(" || "))
5787 {
5788 op = OPERATOR_OROR;
5789 imp->advance(4);
5790 }
5791 else if (imp->match_c_string(" && "))
5792 {
5793 op = OPERATOR_ANDAND;
5794 imp->advance(4);
5795 }
5796 else if (imp->match_c_string(" == "))
5797 {
5798 op = OPERATOR_EQEQ;
5799 imp->advance(4);
5800 }
5801 else if (imp->match_c_string(" != "))
5802 {
5803 op = OPERATOR_NOTEQ;
5804 imp->advance(4);
5805 }
5806 else if (imp->match_c_string(" < "))
5807 {
5808 op = OPERATOR_LT;
5809 imp->advance(3);
5810 }
5811 else if (imp->match_c_string(" <= "))
5812 {
5813 op = OPERATOR_LE;
5814 imp->advance(4);
5815 }
5816 else if (imp->match_c_string(" > "))
5817 {
5818 op = OPERATOR_GT;
5819 imp->advance(3);
5820 }
5821 else if (imp->match_c_string(" >= "))
5822 {
5823 op = OPERATOR_GE;
5824 imp->advance(4);
5825 }
5826 else if (imp->match_c_string(" + "))
5827 {
5828 op = OPERATOR_PLUS;
5829 imp->advance(3);
5830 }
5831 else if (imp->match_c_string(" - "))
5832 {
5833 op = OPERATOR_MINUS;
5834 imp->advance(3);
5835 }
5836 else if (imp->match_c_string(" | "))
5837 {
5838 op = OPERATOR_OR;
5839 imp->advance(3);
5840 }
5841 else if (imp->match_c_string(" ^ "))
5842 {
5843 op = OPERATOR_XOR;
5844 imp->advance(3);
5845 }
5846 else if (imp->match_c_string(" * "))
5847 {
5848 op = OPERATOR_MULT;
5849 imp->advance(3);
5850 }
5851 else if (imp->match_c_string(" / "))
5852 {
5853 op = OPERATOR_DIV;
5854 imp->advance(3);
5855 }
5856 else if (imp->match_c_string(" % "))
5857 {
5858 op = OPERATOR_MOD;
5859 imp->advance(3);
5860 }
5861 else if (imp->match_c_string(" << "))
5862 {
5863 op = OPERATOR_LSHIFT;
5864 imp->advance(4);
5865 }
5866 else if (imp->match_c_string(" >> "))
5867 {
5868 op = OPERATOR_RSHIFT;
5869 imp->advance(4);
5870 }
5871 else if (imp->match_c_string(" & "))
5872 {
5873 op = OPERATOR_AND;
5874 imp->advance(3);
5875 }
5876 else if (imp->match_c_string(" &^ "))
5877 {
5878 op = OPERATOR_BITCLEAR;
5879 imp->advance(4);
5880 }
5881 else
5882 {
5883 error_at(imp->location(), "unrecognized binary operator");
5884 return Expression::make_error(imp->location());
5885 }
5886
5887 Expression* right = Expression::import_expression(imp);
5888
5889 imp->require_c_string(")");
5890
5891 return Expression::make_binary(op, left, right, imp->location());
5892 }
5893
5894 // Make a binary expression.
5895
5896 Expression*
5897 Expression::make_binary(Operator op, Expression* left, Expression* right,
5898 source_location location)
5899 {
5900 return new Binary_expression(op, left, right, location);
5901 }
5902
5903 // Implement a comparison.
5904
5905 tree
5906 Expression::comparison_tree(Translate_context* context, Operator op,
5907 Type* left_type, tree left_tree,
5908 Type* right_type, tree right_tree,
5909 source_location location)
5910 {
5911 enum tree_code code;
5912 switch (op)
5913 {
5914 case OPERATOR_EQEQ:
5915 code = EQ_EXPR;
5916 break;
5917 case OPERATOR_NOTEQ:
5918 code = NE_EXPR;
5919 break;
5920 case OPERATOR_LT:
5921 code = LT_EXPR;
5922 break;
5923 case OPERATOR_LE:
5924 code = LE_EXPR;
5925 break;
5926 case OPERATOR_GT:
5927 code = GT_EXPR;
5928 break;
5929 case OPERATOR_GE:
5930 code = GE_EXPR;
5931 break;
5932 default:
5933 gcc_unreachable();
5934 }
5935
5936 if (left_type->is_string_type())
5937 {
5938 gcc_assert(right_type->is_string_type());
5939 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5940 static tree string_compare_decl;
5941 left_tree = Gogo::call_builtin(&string_compare_decl,
5942 location,
5943 "__go_strcmp",
5944 2,
5945 integer_type_node,
5946 string_type,
5947 left_tree,
5948 string_type,
5949 right_tree);
5950 right_tree = build_int_cst_type(integer_type_node, 0);
5951 }
5952
5953 if ((left_type->interface_type() != NULL
5954 && right_type->interface_type() == NULL
5955 && !right_type->is_nil_type())
5956 || (left_type->interface_type() == NULL
5957 && !left_type->is_nil_type()
5958 && right_type->interface_type() != NULL))
5959 {
5960 // Comparing an interface value to a non-interface value.
5961 if (left_type->interface_type() == NULL)
5962 {
5963 std::swap(left_type, right_type);
5964 std::swap(left_tree, right_tree);
5965 }
5966
5967 // The right operand is not an interface. We need to take its
5968 // address if it is not a pointer.
5969 tree make_tmp;
5970 tree arg;
5971 if (right_type->points_to() != NULL)
5972 {
5973 make_tmp = NULL_TREE;
5974 arg = right_tree;
5975 }
5976 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
5977 {
5978 make_tmp = NULL_TREE;
5979 arg = build_fold_addr_expr_loc(location, right_tree);
5980 if (DECL_P(right_tree))
5981 TREE_ADDRESSABLE(right_tree) = 1;
5982 }
5983 else
5984 {
5985 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
5986 get_name(right_tree));
5987 DECL_IGNORED_P(tmp) = 0;
5988 DECL_INITIAL(tmp) = right_tree;
5989 TREE_ADDRESSABLE(tmp) = 1;
5990 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
5991 SET_EXPR_LOCATION(make_tmp, location);
5992 arg = build_fold_addr_expr_loc(location, tmp);
5993 }
5994 arg = fold_convert_loc(location, ptr_type_node, arg);
5995
5996 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
5997
5998 if (left_type->interface_type()->is_empty())
5999 {
6000 static tree empty_interface_value_compare_decl;
6001 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6002 location,
6003 "__go_empty_interface_value_compare",
6004 3,
6005 integer_type_node,
6006 TREE_TYPE(left_tree),
6007 left_tree,
6008 TREE_TYPE(descriptor),
6009 descriptor,
6010 ptr_type_node,
6011 arg);
6012 // This can panic if the type is not comparable.
6013 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6014 }
6015 else
6016 {
6017 static tree interface_value_compare_decl;
6018 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6019 location,
6020 "__go_interface_value_compare",
6021 3,
6022 integer_type_node,
6023 TREE_TYPE(left_tree),
6024 left_tree,
6025 TREE_TYPE(descriptor),
6026 descriptor,
6027 ptr_type_node,
6028 arg);
6029 // This can panic if the type is not comparable.
6030 TREE_NOTHROW(interface_value_compare_decl) = 0;
6031 }
6032 right_tree = build_int_cst_type(integer_type_node, 0);
6033
6034 if (make_tmp != NULL_TREE)
6035 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6036 left_tree);
6037 }
6038 else if (left_type->interface_type() != NULL
6039 && right_type->interface_type() != NULL)
6040 {
6041 if (left_type->interface_type()->is_empty())
6042 {
6043 gcc_assert(right_type->interface_type()->is_empty());
6044 static tree empty_interface_compare_decl;
6045 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6046 location,
6047 "__go_empty_interface_compare",
6048 2,
6049 integer_type_node,
6050 TREE_TYPE(left_tree),
6051 left_tree,
6052 TREE_TYPE(right_tree),
6053 right_tree);
6054 // This can panic if the type is uncomparable.
6055 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6056 }
6057 else
6058 {
6059 gcc_assert(!right_type->interface_type()->is_empty());
6060 static tree interface_compare_decl;
6061 left_tree = Gogo::call_builtin(&interface_compare_decl,
6062 location,
6063 "__go_interface_compare",
6064 2,
6065 integer_type_node,
6066 TREE_TYPE(left_tree),
6067 left_tree,
6068 TREE_TYPE(right_tree),
6069 right_tree);
6070 // This can panic if the type is uncomparable.
6071 TREE_NOTHROW(interface_compare_decl) = 0;
6072 }
6073 right_tree = build_int_cst_type(integer_type_node, 0);
6074 }
6075
6076 if (left_type->is_nil_type()
6077 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6078 {
6079 std::swap(left_type, right_type);
6080 std::swap(left_tree, right_tree);
6081 }
6082
6083 if (right_type->is_nil_type())
6084 {
6085 if (left_type->array_type() != NULL
6086 && left_type->array_type()->length() == NULL)
6087 {
6088 Array_type* at = left_type->array_type();
6089 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6090 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6091 }
6092 else if (left_type->interface_type() != NULL)
6093 {
6094 // An interface is nil if the first field is nil.
6095 tree left_type_tree = TREE_TYPE(left_tree);
6096 gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6097 tree field = TYPE_FIELDS(left_type_tree);
6098 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6099 field, NULL_TREE);
6100 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6101 }
6102 else
6103 {
6104 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6105 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6106 }
6107 }
6108
6109 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6110 if (CAN_HAVE_LOCATION_P(ret))
6111 SET_EXPR_LOCATION(ret, location);
6112 return ret;
6113 }
6114
6115 // Class Bound_method_expression.
6116
6117 // Traversal.
6118
6119 int
6120 Bound_method_expression::do_traverse(Traverse* traverse)
6121 {
6122 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6123 return TRAVERSE_EXIT;
6124 return Expression::traverse(&this->method_, traverse);
6125 }
6126
6127 // Return the type of a bound method expression. The type of this
6128 // object is really the type of the method with no receiver. We
6129 // should be able to get away with just returning the type of the
6130 // method.
6131
6132 Type*
6133 Bound_method_expression::do_type()
6134 {
6135 return this->method_->type();
6136 }
6137
6138 // Determine the types of a method expression.
6139
6140 void
6141 Bound_method_expression::do_determine_type(const Type_context*)
6142 {
6143 this->method_->determine_type_no_context();
6144 Type* mtype = this->method_->type();
6145 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6146 if (fntype == NULL || !fntype->is_method())
6147 this->expr_->determine_type_no_context();
6148 else
6149 {
6150 Type_context subcontext(fntype->receiver()->type(), false);
6151 this->expr_->determine_type(&subcontext);
6152 }
6153 }
6154
6155 // Check the types of a method expression.
6156
6157 void
6158 Bound_method_expression::do_check_types(Gogo*)
6159 {
6160 Type* type = this->method_->type()->deref();
6161 if (type == NULL
6162 || type->function_type() == NULL
6163 || !type->function_type()->is_method())
6164 this->report_error(_("object is not a method"));
6165 else
6166 {
6167 Type* rtype = type->function_type()->receiver()->type()->deref();
6168 Type* etype = (this->expr_type_ != NULL
6169 ? this->expr_type_
6170 : this->expr_->type());
6171 etype = etype->deref();
6172 if (!Type::are_identical(rtype, etype, NULL))
6173 this->report_error(_("method type does not match object type"));
6174 }
6175 }
6176
6177 // Get the tree for a method expression. There is no standard tree
6178 // representation for this. The only places it may currently be used
6179 // are in a Call_expression or a Go_statement, which will take it
6180 // apart directly. So this has nothing to do at present.
6181
6182 tree
6183 Bound_method_expression::do_get_tree(Translate_context*)
6184 {
6185 gcc_unreachable();
6186 }
6187
6188 // Make a method expression.
6189
6190 Bound_method_expression*
6191 Expression::make_bound_method(Expression* expr, Expression* method,
6192 source_location location)
6193 {
6194 return new Bound_method_expression(expr, method, location);
6195 }
6196
6197 // Class Builtin_call_expression. This is used for a call to a
6198 // builtin function.
6199
6200 class Builtin_call_expression : public Call_expression
6201 {
6202 public:
6203 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6204 bool is_varargs, source_location location);
6205
6206 protected:
6207 // This overrides Call_expression::do_lower.
6208 Expression*
6209 do_lower(Gogo*, Named_object*, int);
6210
6211 bool
6212 do_is_constant() const;
6213
6214 bool
6215 do_integer_constant_value(bool, mpz_t, Type**) const;
6216
6217 bool
6218 do_float_constant_value(mpfr_t, Type**) const;
6219
6220 bool
6221 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6222
6223 Type*
6224 do_type();
6225
6226 void
6227 do_determine_type(const Type_context*);
6228
6229 void
6230 do_check_types(Gogo*);
6231
6232 Expression*
6233 do_copy()
6234 {
6235 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6236 this->args()->copy(),
6237 this->is_varargs(),
6238 this->location());
6239 }
6240
6241 tree
6242 do_get_tree(Translate_context*);
6243
6244 void
6245 do_export(Export*) const;
6246
6247 virtual bool
6248 do_is_recover_call() const;
6249
6250 virtual void
6251 do_set_recover_arg(Expression*);
6252
6253 private:
6254 // The builtin functions.
6255 enum Builtin_function_code
6256 {
6257 BUILTIN_INVALID,
6258
6259 // Predeclared builtin functions.
6260 BUILTIN_APPEND,
6261 BUILTIN_CAP,
6262 BUILTIN_CLOSE,
6263 BUILTIN_CLOSED,
6264 BUILTIN_CMPLX,
6265 BUILTIN_COPY,
6266 BUILTIN_IMAG,
6267 BUILTIN_LEN,
6268 BUILTIN_MAKE,
6269 BUILTIN_NEW,
6270 BUILTIN_PANIC,
6271 BUILTIN_PRINT,
6272 BUILTIN_PRINTLN,
6273 BUILTIN_REAL,
6274 BUILTIN_RECOVER,
6275
6276 // Builtin functions from the unsafe package.
6277 BUILTIN_ALIGNOF,
6278 BUILTIN_OFFSETOF,
6279 BUILTIN_SIZEOF
6280 };
6281
6282 Expression*
6283 one_arg() const;
6284
6285 bool
6286 check_one_arg();
6287
6288 static Type*
6289 real_imag_type(Type*);
6290
6291 static Type*
6292 cmplx_type(Type*);
6293
6294 // A pointer back to the general IR structure. This avoids a global
6295 // variable, or passing it around everywhere.
6296 Gogo* gogo_;
6297 // The builtin function being called.
6298 Builtin_function_code code_;
6299 };
6300
6301 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6302 Expression* fn,
6303 Expression_list* args,
6304 bool is_varargs,
6305 source_location location)
6306 : Call_expression(fn, args, is_varargs, location),
6307 gogo_(gogo), code_(BUILTIN_INVALID)
6308 {
6309 Func_expression* fnexp = this->fn()->func_expression();
6310 gcc_assert(fnexp != NULL);
6311 const std::string& name(fnexp->named_object()->name());
6312 if (name == "append")
6313 this->code_ = BUILTIN_APPEND;
6314 else if (name == "cap")
6315 this->code_ = BUILTIN_CAP;
6316 else if (name == "close")
6317 this->code_ = BUILTIN_CLOSE;
6318 else if (name == "closed")
6319 this->code_ = BUILTIN_CLOSED;
6320 else if (name == "cmplx")
6321 this->code_ = BUILTIN_CMPLX;
6322 else if (name == "copy")
6323 this->code_ = BUILTIN_COPY;
6324 else if (name == "imag")
6325 this->code_ = BUILTIN_IMAG;
6326 else if (name == "len")
6327 this->code_ = BUILTIN_LEN;
6328 else if (name == "make")
6329 this->code_ = BUILTIN_MAKE;
6330 else if (name == "new")
6331 this->code_ = BUILTIN_NEW;
6332 else if (name == "panic")
6333 this->code_ = BUILTIN_PANIC;
6334 else if (name == "print")
6335 this->code_ = BUILTIN_PRINT;
6336 else if (name == "println")
6337 this->code_ = BUILTIN_PRINTLN;
6338 else if (name == "real")
6339 this->code_ = BUILTIN_REAL;
6340 else if (name == "recover")
6341 this->code_ = BUILTIN_RECOVER;
6342 else if (name == "Alignof")
6343 this->code_ = BUILTIN_ALIGNOF;
6344 else if (name == "Offsetof")
6345 this->code_ = BUILTIN_OFFSETOF;
6346 else if (name == "Sizeof")
6347 this->code_ = BUILTIN_SIZEOF;
6348 else
6349 gcc_unreachable();
6350 }
6351
6352 // Return whether this is a call to recover. This is a virtual
6353 // function called from the parent class.
6354
6355 bool
6356 Builtin_call_expression::do_is_recover_call() const
6357 {
6358 if (this->classification() == EXPRESSION_ERROR)
6359 return false;
6360 return this->code_ == BUILTIN_RECOVER;
6361 }
6362
6363 // Set the argument for a call to recover.
6364
6365 void
6366 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6367 {
6368 const Expression_list* args = this->args();
6369 gcc_assert(args == NULL || args->empty());
6370 Expression_list* new_args = new Expression_list();
6371 new_args->push_back(arg);
6372 this->set_args(new_args);
6373 }
6374
6375 // A traversal class which looks for a call expression.
6376
6377 class Find_call_expression : public Traverse
6378 {
6379 public:
6380 Find_call_expression()
6381 : Traverse(traverse_expressions),
6382 found_(false)
6383 { }
6384
6385 int
6386 expression(Expression**);
6387
6388 bool
6389 found()
6390 { return this->found_; }
6391
6392 private:
6393 bool found_;
6394 };
6395
6396 int
6397 Find_call_expression::expression(Expression** pexpr)
6398 {
6399 if ((*pexpr)->call_expression() != NULL)
6400 {
6401 this->found_ = true;
6402 return TRAVERSE_EXIT;
6403 }
6404 return TRAVERSE_CONTINUE;
6405 }
6406
6407 // Lower a builtin call expression. This turns new and make into
6408 // specific expressions. We also convert to a constant if we can.
6409
6410 Expression*
6411 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6412 {
6413 if (this->code_ == BUILTIN_NEW)
6414 {
6415 const Expression_list* args = this->args();
6416 if (args == NULL || args->size() < 1)
6417 this->report_error(_("not enough arguments"));
6418 else if (args->size() > 1)
6419 this->report_error(_("too many arguments"));
6420 else
6421 {
6422 Expression* arg = args->front();
6423 if (!arg->is_type_expression())
6424 {
6425 error_at(arg->location(), "expected type");
6426 this->set_is_error();
6427 }
6428 else
6429 return Expression::make_allocation(arg->type(), this->location());
6430 }
6431 }
6432 else if (this->code_ == BUILTIN_MAKE)
6433 {
6434 const Expression_list* args = this->args();
6435 if (args == NULL || args->size() < 1)
6436 this->report_error(_("not enough arguments"));
6437 else
6438 {
6439 Expression* arg = args->front();
6440 if (!arg->is_type_expression())
6441 {
6442 error_at(arg->location(), "expected type");
6443 this->set_is_error();
6444 }
6445 else
6446 {
6447 Expression_list* newargs;
6448 if (args->size() == 1)
6449 newargs = NULL;
6450 else
6451 {
6452 newargs = new Expression_list();
6453 Expression_list::const_iterator p = args->begin();
6454 ++p;
6455 for (; p != args->end(); ++p)
6456 newargs->push_back(*p);
6457 }
6458 return Expression::make_make(arg->type(), newargs,
6459 this->location());
6460 }
6461 }
6462 }
6463 else if (this->is_constant())
6464 {
6465 // We can only lower len and cap if there are no function calls
6466 // in the arguments. Otherwise we have to make the call.
6467 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6468 {
6469 Expression* arg = this->one_arg();
6470 if (!arg->is_constant())
6471 {
6472 Find_call_expression find_call;
6473 Expression::traverse(&arg, &find_call);
6474 if (find_call.found())
6475 return this;
6476 }
6477 }
6478
6479 mpz_t ival;
6480 mpz_init(ival);
6481 Type* type;
6482 if (this->integer_constant_value(true, ival, &type))
6483 {
6484 Expression* ret = Expression::make_integer(&ival, type,
6485 this->location());
6486 mpz_clear(ival);
6487 return ret;
6488 }
6489 mpz_clear(ival);
6490
6491 mpfr_t rval;
6492 mpfr_init(rval);
6493 if (this->float_constant_value(rval, &type))
6494 {
6495 Expression* ret = Expression::make_float(&rval, type,
6496 this->location());
6497 mpfr_clear(rval);
6498 return ret;
6499 }
6500
6501 mpfr_t imag;
6502 mpfr_init(imag);
6503 if (this->complex_constant_value(rval, imag, &type))
6504 {
6505 Expression* ret = Expression::make_complex(&rval, &imag, type,
6506 this->location());
6507 mpfr_clear(rval);
6508 mpfr_clear(imag);
6509 return ret;
6510 }
6511 mpfr_clear(rval);
6512 mpfr_clear(imag);
6513 }
6514 else if (this->code_ == BUILTIN_RECOVER)
6515 {
6516 if (function != NULL)
6517 function->func_value()->set_calls_recover();
6518 else
6519 {
6520 // Calling recover outside of a function always returns the
6521 // nil empty interface.
6522 Type* eface = Type::make_interface_type(NULL, this->location());
6523 return Expression::make_cast(eface,
6524 Expression::make_nil(this->location()),
6525 this->location());
6526 }
6527 }
6528 else if (this->code_ == BUILTIN_APPEND)
6529 {
6530 // Lower the varargs.
6531 const Expression_list* args = this->args();
6532 if (args == NULL || args->empty())
6533 return this;
6534 Type* slice_type = args->front()->type();
6535 if (!slice_type->is_open_array_type())
6536 {
6537 error_at(args->front()->location(), "argument 1 must be a slice");
6538 this->set_is_error();
6539 return this;
6540 }
6541 return this->lower_varargs(gogo, function, slice_type, 2);
6542 }
6543
6544 return this;
6545 }
6546
6547 // Return the type of the real or imag functions, given the type of
6548 // the argument. We need to map complex to float, complex64 to
6549 // float32, and complex128 to float64, so it has to be done by name.
6550 // This returns NULL if it can't figure out the type.
6551
6552 Type*
6553 Builtin_call_expression::real_imag_type(Type* arg_type)
6554 {
6555 if (arg_type == NULL || arg_type->is_abstract())
6556 return NULL;
6557 Named_type* nt = arg_type->named_type();
6558 if (nt == NULL)
6559 return NULL;
6560 while (nt->real_type()->named_type() != NULL)
6561 nt = nt->real_type()->named_type();
6562 if (nt->name() == "complex")
6563 return Type::lookup_float_type("float");
6564 else if (nt->name() == "complex64")
6565 return Type::lookup_float_type("float32");
6566 else if (nt->name() == "complex128")
6567 return Type::lookup_float_type("float64");
6568 else
6569 return NULL;
6570 }
6571
6572 // Return the type of the cmplx function, given the type of one of the
6573 // argments. Like real_imag_type, we have to map by name.
6574
6575 Type*
6576 Builtin_call_expression::cmplx_type(Type* arg_type)
6577 {
6578 if (arg_type == NULL || arg_type->is_abstract())
6579 return NULL;
6580 Named_type* nt = arg_type->named_type();
6581 if (nt == NULL)
6582 return NULL;
6583 while (nt->real_type()->named_type() != NULL)
6584 nt = nt->real_type()->named_type();
6585 if (nt->name() == "float")
6586 return Type::lookup_complex_type("complex");
6587 else if (nt->name() == "float32")
6588 return Type::lookup_complex_type("complex64");
6589 else if (nt->name() == "float64")
6590 return Type::lookup_complex_type("complex128");
6591 else
6592 return NULL;
6593 }
6594
6595 // Return a single argument, or NULL if there isn't one.
6596
6597 Expression*
6598 Builtin_call_expression::one_arg() const
6599 {
6600 const Expression_list* args = this->args();
6601 if (args->size() != 1)
6602 return NULL;
6603 return args->front();
6604 }
6605
6606 // Return whether this is constant: len of a string, or len or cap of
6607 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6608
6609 bool
6610 Builtin_call_expression::do_is_constant() const
6611 {
6612 switch (this->code_)
6613 {
6614 case BUILTIN_LEN:
6615 case BUILTIN_CAP:
6616 {
6617 Expression* arg = this->one_arg();
6618 if (arg == NULL)
6619 return false;
6620 Type* arg_type = arg->type();
6621
6622 if (arg_type->points_to() != NULL
6623 && arg_type->points_to()->array_type() != NULL
6624 && !arg_type->points_to()->is_open_array_type())
6625 arg_type = arg_type->points_to();
6626
6627 if (arg_type->array_type() != NULL
6628 && arg_type->array_type()->length() != NULL)
6629 return arg_type->array_type()->length()->is_constant();
6630
6631 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6632 return arg->is_constant();
6633 }
6634 break;
6635
6636 case BUILTIN_SIZEOF:
6637 case BUILTIN_ALIGNOF:
6638 return this->one_arg() != NULL;
6639
6640 case BUILTIN_OFFSETOF:
6641 {
6642 Expression* arg = this->one_arg();
6643 if (arg == NULL)
6644 return false;
6645 return arg->field_reference_expression() != NULL;
6646 }
6647
6648 case BUILTIN_CMPLX:
6649 {
6650 const Expression_list* args = this->args();
6651 if (args != NULL && args->size() == 2)
6652 return args->front()->is_constant() && args->back()->is_constant();
6653 }
6654 break;
6655
6656 case BUILTIN_REAL:
6657 case BUILTIN_IMAG:
6658 {
6659 Expression* arg = this->one_arg();
6660 return arg != NULL && arg->is_constant();
6661 }
6662
6663 default:
6664 break;
6665 }
6666
6667 return false;
6668 }
6669
6670 // Return an integer constant value if possible.
6671
6672 bool
6673 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
6674 mpz_t val,
6675 Type** ptype) const
6676 {
6677 if (this->code_ == BUILTIN_LEN
6678 || this->code_ == BUILTIN_CAP)
6679 {
6680 Expression* arg = this->one_arg();
6681 if (arg == NULL)
6682 return false;
6683 Type* arg_type = arg->type();
6684
6685 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6686 {
6687 std::string sval;
6688 if (arg->string_constant_value(&sval))
6689 {
6690 mpz_set_ui(val, sval.length());
6691 *ptype = Type::lookup_integer_type("int");
6692 return true;
6693 }
6694 }
6695
6696 if (arg_type->points_to() != NULL
6697 && arg_type->points_to()->array_type() != NULL
6698 && !arg_type->points_to()->is_open_array_type())
6699 arg_type = arg_type->points_to();
6700
6701 if (arg_type->array_type() != NULL
6702 && arg_type->array_type()->length() != NULL)
6703 {
6704 Expression* e = arg_type->array_type()->length();
6705 if (e->integer_constant_value(iota_is_constant, val, ptype))
6706 {
6707 *ptype = Type::lookup_integer_type("int");
6708 return true;
6709 }
6710 }
6711 }
6712 else if (this->code_ == BUILTIN_SIZEOF
6713 || this->code_ == BUILTIN_ALIGNOF)
6714 {
6715 Expression* arg = this->one_arg();
6716 if (arg == NULL)
6717 return false;
6718 Type* arg_type = arg->type();
6719 if (arg_type->is_error_type())
6720 return false;
6721 if (arg_type->is_abstract())
6722 return false;
6723 tree arg_type_tree = arg_type->get_tree(this->gogo_);
6724 unsigned long val_long;
6725 if (this->code_ == BUILTIN_SIZEOF)
6726 {
6727 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
6728 gcc_assert(TREE_CODE(type_size) == INTEGER_CST);
6729 if (TREE_INT_CST_HIGH(type_size) != 0)
6730 return false;
6731 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
6732 val_long = static_cast<unsigned long>(val_wide);
6733 if (val_long != val_wide)
6734 return false;
6735 }
6736 else if (this->code_ == BUILTIN_ALIGNOF)
6737 {
6738 if (arg->field_reference_expression() == NULL)
6739 val_long = go_type_alignment(arg_type_tree);
6740 else
6741 {
6742 // Calling unsafe.Alignof(s.f) returns the alignment of
6743 // the type of f when it is used as a field in a struct.
6744 val_long = go_field_alignment(arg_type_tree);
6745 }
6746 }
6747 else
6748 gcc_unreachable();
6749 mpz_set_ui(val, val_long);
6750 *ptype = NULL;
6751 return true;
6752 }
6753 else if (this->code_ == BUILTIN_OFFSETOF)
6754 {
6755 Expression* arg = this->one_arg();
6756 if (arg == NULL)
6757 return false;
6758 Field_reference_expression* farg = arg->field_reference_expression();
6759 if (farg == NULL)
6760 return false;
6761 Expression* struct_expr = farg->expr();
6762 Type* st = struct_expr->type();
6763 if (st->struct_type() == NULL)
6764 return false;
6765 tree struct_tree = st->get_tree(this->gogo_);
6766 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
6767 tree field = TYPE_FIELDS(struct_tree);
6768 for (unsigned int index = farg->field_index(); index > 0; --index)
6769 {
6770 field = DECL_CHAIN(field);
6771 gcc_assert(field != NULL_TREE);
6772 }
6773 HOST_WIDE_INT offset_wide = int_byte_position (field);
6774 if (offset_wide < 0)
6775 return false;
6776 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
6777 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
6778 return false;
6779 mpz_set_ui(val, offset_long);
6780 return true;
6781 }
6782 return false;
6783 }
6784
6785 // Return a floating point constant value if possible.
6786
6787 bool
6788 Builtin_call_expression::do_float_constant_value(mpfr_t val,
6789 Type** ptype) const
6790 {
6791 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
6792 {
6793 Expression* arg = this->one_arg();
6794 if (arg == NULL)
6795 return false;
6796
6797 mpfr_t real;
6798 mpfr_t imag;
6799 mpfr_init(real);
6800 mpfr_init(imag);
6801
6802 bool ret = false;
6803 Type* type;
6804 if (arg->complex_constant_value(real, imag, &type))
6805 {
6806 if (this->code_ == BUILTIN_REAL)
6807 mpfr_set(val, real, GMP_RNDN);
6808 else
6809 mpfr_set(val, imag, GMP_RNDN);
6810 *ptype = Builtin_call_expression::real_imag_type(type);
6811 ret = true;
6812 }
6813
6814 mpfr_clear(real);
6815 mpfr_clear(imag);
6816 return ret;
6817 }
6818
6819 return false;
6820 }
6821
6822 // Return a complex constant value if possible.
6823
6824 bool
6825 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
6826 Type** ptype) const
6827 {
6828 if (this->code_ == BUILTIN_CMPLX)
6829 {
6830 const Expression_list* args = this->args();
6831 if (args == NULL || args->size() != 2)
6832 return false;
6833
6834 mpfr_t r;
6835 mpfr_init(r);
6836 Type* rtype;
6837 if (!args->front()->float_constant_value(r, &rtype))
6838 {
6839 mpfr_clear(r);
6840 return false;
6841 }
6842
6843 mpfr_t i;
6844 mpfr_init(i);
6845
6846 bool ret = false;
6847 Type* itype;
6848 if (args->back()->float_constant_value(i, &itype)
6849 && Type::are_identical(rtype, itype, NULL))
6850 {
6851 mpfr_set(real, r, GMP_RNDN);
6852 mpfr_set(imag, i, GMP_RNDN);
6853 *ptype = Builtin_call_expression::cmplx_type(rtype);
6854 ret = true;
6855 }
6856
6857 mpfr_clear(r);
6858 mpfr_clear(i);
6859
6860 return ret;
6861 }
6862
6863 return false;
6864 }
6865
6866 // Return the type.
6867
6868 Type*
6869 Builtin_call_expression::do_type()
6870 {
6871 switch (this->code_)
6872 {
6873 case BUILTIN_INVALID:
6874 default:
6875 gcc_unreachable();
6876
6877 case BUILTIN_NEW:
6878 case BUILTIN_MAKE:
6879 {
6880 const Expression_list* args = this->args();
6881 if (args == NULL || args->empty())
6882 return Type::make_error_type();
6883 return Type::make_pointer_type(args->front()->type());
6884 }
6885
6886 case BUILTIN_CAP:
6887 case BUILTIN_COPY:
6888 case BUILTIN_LEN:
6889 case BUILTIN_ALIGNOF:
6890 case BUILTIN_OFFSETOF:
6891 case BUILTIN_SIZEOF:
6892 return Type::lookup_integer_type("int");
6893
6894 case BUILTIN_CLOSE:
6895 case BUILTIN_PANIC:
6896 case BUILTIN_PRINT:
6897 case BUILTIN_PRINTLN:
6898 return Type::make_void_type();
6899
6900 case BUILTIN_CLOSED:
6901 return Type::lookup_bool_type();
6902
6903 case BUILTIN_RECOVER:
6904 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
6905
6906 case BUILTIN_APPEND:
6907 {
6908 const Expression_list* args = this->args();
6909 if (args == NULL || args->empty())
6910 return Type::make_error_type();
6911 return args->front()->type();
6912 }
6913
6914 case BUILTIN_REAL:
6915 case BUILTIN_IMAG:
6916 {
6917 Expression* arg = this->one_arg();
6918 if (arg == NULL)
6919 return Type::make_error_type();
6920 Type* t = arg->type();
6921 if (t->is_abstract())
6922 t = t->make_non_abstract_type();
6923 t = Builtin_call_expression::real_imag_type(t);
6924 if (t == NULL)
6925 t = Type::make_error_type();
6926 return t;
6927 }
6928
6929 case BUILTIN_CMPLX:
6930 {
6931 const Expression_list* args = this->args();
6932 if (args == NULL || args->size() != 2)
6933 return Type::make_error_type();
6934 Type* t = args->front()->type();
6935 if (t->is_abstract())
6936 {
6937 t = args->back()->type();
6938 if (t->is_abstract())
6939 t = t->make_non_abstract_type();
6940 }
6941 t = Builtin_call_expression::cmplx_type(t);
6942 if (t == NULL)
6943 t = Type::make_error_type();
6944 return t;
6945 }
6946 }
6947 }
6948
6949 // Determine the type.
6950
6951 void
6952 Builtin_call_expression::do_determine_type(const Type_context* context)
6953 {
6954 this->fn()->determine_type_no_context();
6955
6956 const Expression_list* args = this->args();
6957
6958 bool is_print;
6959 Type* arg_type = NULL;
6960 switch (this->code_)
6961 {
6962 case BUILTIN_PRINT:
6963 case BUILTIN_PRINTLN:
6964 // Do not force a large integer constant to "int".
6965 is_print = true;
6966 break;
6967
6968 case BUILTIN_REAL:
6969 case BUILTIN_IMAG:
6970 arg_type = Builtin_call_expression::cmplx_type(context->type);
6971 is_print = false;
6972 break;
6973
6974 case BUILTIN_CMPLX:
6975 {
6976 // For the cmplx function the type of one operand can
6977 // determine the type of the other, as in a binary expression.
6978 arg_type = Builtin_call_expression::real_imag_type(context->type);
6979 if (args != NULL && args->size() == 2)
6980 {
6981 Type* t1 = args->front()->type();
6982 Type* t2 = args->front()->type();
6983 if (!t1->is_abstract())
6984 arg_type = t1;
6985 else if (!t2->is_abstract())
6986 arg_type = t2;
6987 }
6988 is_print = false;
6989 }
6990 break;
6991
6992 default:
6993 is_print = false;
6994 break;
6995 }
6996
6997 if (args != NULL)
6998 {
6999 for (Expression_list::const_iterator pa = args->begin();
7000 pa != args->end();
7001 ++pa)
7002 {
7003 Type_context subcontext;
7004 subcontext.type = arg_type;
7005
7006 if (is_print)
7007 {
7008 // We want to print large constants, we so can't just
7009 // use the appropriate nonabstract type. Use uint64 for
7010 // an integer if we know it is nonnegative, otherwise
7011 // use int64 for a integer, otherwise use float64 for a
7012 // float or complex128 for a complex.
7013 Type* want_type = NULL;
7014 Type* atype = (*pa)->type();
7015 if (atype->is_abstract())
7016 {
7017 if (atype->integer_type() != NULL)
7018 {
7019 mpz_t val;
7020 mpz_init(val);
7021 Type* dummy;
7022 if (this->integer_constant_value(true, val, &dummy)
7023 && mpz_sgn(val) >= 0)
7024 want_type = Type::lookup_integer_type("uint64");
7025 else
7026 want_type = Type::lookup_integer_type("int64");
7027 mpz_clear(val);
7028 }
7029 else if (atype->float_type() != NULL)
7030 want_type = Type::lookup_float_type("float64");
7031 else if (atype->complex_type() != NULL)
7032 want_type = Type::lookup_complex_type("complex128");
7033 else if (atype->is_abstract_string_type())
7034 want_type = Type::lookup_string_type();
7035 else if (atype->is_abstract_boolean_type())
7036 want_type = Type::lookup_bool_type();
7037 else
7038 gcc_unreachable();
7039 subcontext.type = want_type;
7040 }
7041 }
7042
7043 (*pa)->determine_type(&subcontext);
7044 }
7045 }
7046 }
7047
7048 // If there is exactly one argument, return true. Otherwise give an
7049 // error message and return false.
7050
7051 bool
7052 Builtin_call_expression::check_one_arg()
7053 {
7054 const Expression_list* args = this->args();
7055 if (args == NULL || args->size() < 1)
7056 {
7057 this->report_error(_("not enough arguments"));
7058 return false;
7059 }
7060 else if (args->size() > 1)
7061 {
7062 this->report_error(_("too many arguments"));
7063 return false;
7064 }
7065 if (args->front()->is_error_expression()
7066 || args->front()->type()->is_error_type())
7067 {
7068 this->set_is_error();
7069 return false;
7070 }
7071 return true;
7072 }
7073
7074 // Check argument types for a builtin function.
7075
7076 void
7077 Builtin_call_expression::do_check_types(Gogo*)
7078 {
7079 switch (this->code_)
7080 {
7081 case BUILTIN_INVALID:
7082 case BUILTIN_NEW:
7083 case BUILTIN_MAKE:
7084 return;
7085
7086 case BUILTIN_LEN:
7087 case BUILTIN_CAP:
7088 {
7089 // The single argument may be either a string or an array or a
7090 // map or a channel, or a pointer to a closed array.
7091 if (this->check_one_arg())
7092 {
7093 Type* arg_type = this->one_arg()->type();
7094 if (arg_type->points_to() != NULL
7095 && arg_type->points_to()->array_type() != NULL
7096 && !arg_type->points_to()->is_open_array_type())
7097 arg_type = arg_type->points_to();
7098 if (this->code_ == BUILTIN_CAP)
7099 {
7100 if (!arg_type->is_error_type()
7101 && arg_type->array_type() == NULL
7102 && arg_type->channel_type() == NULL)
7103 this->report_error(_("argument must be array or slice "
7104 "or channel"));
7105 }
7106 else
7107 {
7108 if (!arg_type->is_error_type()
7109 && !arg_type->is_string_type()
7110 && arg_type->array_type() == NULL
7111 && arg_type->map_type() == NULL
7112 && arg_type->channel_type() == NULL)
7113 this->report_error(_("argument must be string or "
7114 "array or slice or map or channel"));
7115 }
7116 }
7117 }
7118 break;
7119
7120 case BUILTIN_PRINT:
7121 case BUILTIN_PRINTLN:
7122 {
7123 const Expression_list* args = this->args();
7124 if (args == NULL)
7125 {
7126 if (this->code_ == BUILTIN_PRINT)
7127 warning_at(this->location(), 0,
7128 "no arguments for builtin function %<%s%>",
7129 (this->code_ == BUILTIN_PRINT
7130 ? "print"
7131 : "println"));
7132 }
7133 else
7134 {
7135 for (Expression_list::const_iterator p = args->begin();
7136 p != args->end();
7137 ++p)
7138 {
7139 Type* type = (*p)->type();
7140 if (type->is_error_type()
7141 || type->is_string_type()
7142 || type->integer_type() != NULL
7143 || type->float_type() != NULL
7144 || type->complex_type() != NULL
7145 || type->is_boolean_type()
7146 || type->points_to() != NULL
7147 || type->interface_type() != NULL
7148 || type->channel_type() != NULL
7149 || type->map_type() != NULL
7150 || type->function_type() != NULL
7151 || type->is_open_array_type())
7152 ;
7153 else
7154 this->report_error(_("unsupported argument type to "
7155 "builtin function"));
7156 }
7157 }
7158 }
7159 break;
7160
7161 case BUILTIN_CLOSE:
7162 case BUILTIN_CLOSED:
7163 if (this->check_one_arg())
7164 {
7165 if (this->one_arg()->type()->channel_type() == NULL)
7166 this->report_error(_("argument must be channel"));
7167 }
7168 break;
7169
7170 case BUILTIN_PANIC:
7171 case BUILTIN_SIZEOF:
7172 case BUILTIN_ALIGNOF:
7173 this->check_one_arg();
7174 break;
7175
7176 case BUILTIN_RECOVER:
7177 if (this->args() != NULL && !this->args()->empty())
7178 this->report_error(_("too many arguments"));
7179 break;
7180
7181 case BUILTIN_OFFSETOF:
7182 if (this->check_one_arg())
7183 {
7184 Expression* arg = this->one_arg();
7185 if (arg->field_reference_expression() == NULL)
7186 this->report_error(_("argument must be a field reference"));
7187 }
7188 break;
7189
7190 case BUILTIN_COPY:
7191 {
7192 const Expression_list* args = this->args();
7193 if (args == NULL || args->size() < 2)
7194 {
7195 this->report_error(_("not enough arguments"));
7196 break;
7197 }
7198 else if (args->size() > 2)
7199 {
7200 this->report_error(_("too many arguments"));
7201 break;
7202 }
7203 Type* arg1_type = args->front()->type();
7204 Type* arg2_type = args->back()->type();
7205 if (arg1_type->is_error_type() || arg2_type->is_error_type())
7206 break;
7207
7208 Type* e1;
7209 if (arg1_type->is_open_array_type())
7210 e1 = arg1_type->array_type()->element_type();
7211 else
7212 {
7213 this->report_error(_("left argument must be a slice"));
7214 break;
7215 }
7216
7217 Type* e2;
7218 if (arg2_type->is_open_array_type())
7219 e2 = arg2_type->array_type()->element_type();
7220 else if (arg2_type->is_string_type())
7221 e2 = Type::lookup_integer_type("uint8");
7222 else
7223 {
7224 this->report_error(_("right argument must be a slice or a string"));
7225 break;
7226 }
7227
7228 if (!Type::are_identical(e1, e2, NULL))
7229 this->report_error(_("element types must be the same"));
7230 }
7231 break;
7232
7233 case BUILTIN_APPEND:
7234 {
7235 const Expression_list* args = this->args();
7236 if (args == NULL || args->empty())
7237 {
7238 this->report_error(_("not enough arguments"));
7239 break;
7240 }
7241 /* Lowering varargs should have left us with 2 arguments. */
7242 gcc_assert(args->size() == 2);
7243 std::string reason;
7244 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7245 &reason))
7246 {
7247 if (reason.empty())
7248 this->report_error(_("arguments 1 and 2 have different types"));
7249 else
7250 {
7251 error_at(this->location(),
7252 "arguments 1 and 2 have different types (%s)",
7253 reason.c_str());
7254 this->set_is_error();
7255 }
7256 }
7257 break;
7258 }
7259
7260 case BUILTIN_REAL:
7261 case BUILTIN_IMAG:
7262 if (this->check_one_arg())
7263 {
7264 if (this->one_arg()->type()->complex_type() == NULL)
7265 this->report_error(_("argument must have complex type"));
7266 }
7267 break;
7268
7269 case BUILTIN_CMPLX:
7270 {
7271 const Expression_list* args = this->args();
7272 if (args == NULL || args->size() < 2)
7273 this->report_error(_("not enough arguments"));
7274 else if (args->size() > 2)
7275 this->report_error(_("too many arguments"));
7276 else if (args->front()->is_error_expression()
7277 || args->front()->type()->is_error_type()
7278 || args->back()->is_error_expression()
7279 || args->back()->type()->is_error_type())
7280 this->set_is_error();
7281 else if (!Type::are_identical(args->front()->type(),
7282 args->back()->type(), NULL))
7283 this->report_error(_("cmplx arguments must have identical types"));
7284 else if (args->front()->type()->float_type() == NULL)
7285 this->report_error(_("cmplx arguments must have "
7286 "floating-point type"));
7287 }
7288 break;
7289
7290 default:
7291 gcc_unreachable();
7292 }
7293 }
7294
7295 // Return the tree for a builtin function.
7296
7297 tree
7298 Builtin_call_expression::do_get_tree(Translate_context* context)
7299 {
7300 Gogo* gogo = context->gogo();
7301 source_location location = this->location();
7302 switch (this->code_)
7303 {
7304 case BUILTIN_INVALID:
7305 case BUILTIN_NEW:
7306 case BUILTIN_MAKE:
7307 gcc_unreachable();
7308
7309 case BUILTIN_LEN:
7310 case BUILTIN_CAP:
7311 {
7312 const Expression_list* args = this->args();
7313 gcc_assert(args != NULL && args->size() == 1);
7314 Expression* arg = *args->begin();
7315 Type* arg_type = arg->type();
7316 tree arg_tree = arg->get_tree(context);
7317 if (arg_tree == error_mark_node)
7318 return error_mark_node;
7319
7320 if (arg_type->points_to() != NULL)
7321 {
7322 arg_type = arg_type->points_to();
7323 gcc_assert(arg_type->array_type() != NULL
7324 && !arg_type->is_open_array_type());
7325 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7326 arg_tree = build_fold_indirect_ref(arg_tree);
7327 }
7328
7329 tree val_tree;
7330 if (this->code_ == BUILTIN_LEN)
7331 {
7332 if (arg_type->is_string_type())
7333 val_tree = String_type::length_tree(gogo, arg_tree);
7334 else if (arg_type->array_type() != NULL)
7335 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7336 else if (arg_type->map_type() != NULL)
7337 {
7338 static tree map_len_fndecl;
7339 val_tree = Gogo::call_builtin(&map_len_fndecl,
7340 location,
7341 "__go_map_len",
7342 1,
7343 sizetype,
7344 arg_type->get_tree(gogo),
7345 arg_tree);
7346 }
7347 else if (arg_type->channel_type() != NULL)
7348 {
7349 static tree chan_len_fndecl;
7350 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7351 location,
7352 "__go_chan_len",
7353 1,
7354 sizetype,
7355 arg_type->get_tree(gogo),
7356 arg_tree);
7357 }
7358 else
7359 gcc_unreachable();
7360 }
7361 else
7362 {
7363 if (arg_type->array_type() != NULL)
7364 val_tree = arg_type->array_type()->capacity_tree(gogo, arg_tree);
7365 else if (arg_type->channel_type() != NULL)
7366 {
7367 static tree chan_cap_fndecl;
7368 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7369 location,
7370 "__go_chan_cap",
7371 1,
7372 sizetype,
7373 arg_type->get_tree(gogo),
7374 arg_tree);
7375 }
7376 else
7377 gcc_unreachable();
7378 }
7379
7380 tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo);
7381 if (type_tree == TREE_TYPE(val_tree))
7382 return val_tree;
7383 else
7384 return fold(convert_to_integer(type_tree, val_tree));
7385 }
7386
7387 case BUILTIN_PRINT:
7388 case BUILTIN_PRINTLN:
7389 {
7390 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7391 tree stmt_list = NULL_TREE;
7392
7393 const Expression_list* call_args = this->args();
7394 if (call_args != NULL)
7395 {
7396 for (Expression_list::const_iterator p = call_args->begin();
7397 p != call_args->end();
7398 ++p)
7399 {
7400 if (is_ln && p != call_args->begin())
7401 {
7402 static tree print_space_fndecl;
7403 tree call = Gogo::call_builtin(&print_space_fndecl,
7404 location,
7405 "__go_print_space",
7406 0,
7407 void_type_node);
7408 append_to_statement_list(call, &stmt_list);
7409 }
7410
7411 Type* type = (*p)->type();
7412
7413 tree arg = (*p)->get_tree(context);
7414 if (arg == error_mark_node)
7415 return error_mark_node;
7416
7417 tree* pfndecl;
7418 const char* fnname;
7419 if (type->is_string_type())
7420 {
7421 static tree print_string_fndecl;
7422 pfndecl = &print_string_fndecl;
7423 fnname = "__go_print_string";
7424 }
7425 else if (type->integer_type() != NULL
7426 && type->integer_type()->is_unsigned())
7427 {
7428 static tree print_uint64_fndecl;
7429 pfndecl = &print_uint64_fndecl;
7430 fnname = "__go_print_uint64";
7431 Type* itype = Type::lookup_integer_type("uint64");
7432 arg = fold_convert_loc(location, itype->get_tree(gogo),
7433 arg);
7434 }
7435 else if (type->integer_type() != NULL)
7436 {
7437 static tree print_int64_fndecl;
7438 pfndecl = &print_int64_fndecl;
7439 fnname = "__go_print_int64";
7440 Type* itype = Type::lookup_integer_type("int64");
7441 arg = fold_convert_loc(location, itype->get_tree(gogo),
7442 arg);
7443 }
7444 else if (type->float_type() != NULL)
7445 {
7446 static tree print_double_fndecl;
7447 pfndecl = &print_double_fndecl;
7448 fnname = "__go_print_double";
7449 arg = fold_convert_loc(location, double_type_node, arg);
7450 }
7451 else if (type->complex_type() != NULL)
7452 {
7453 static tree print_complex_fndecl;
7454 pfndecl = &print_complex_fndecl;
7455 fnname = "__go_print_complex";
7456 arg = fold_convert_loc(location, complex_double_type_node,
7457 arg);
7458 }
7459 else if (type->is_boolean_type())
7460 {
7461 static tree print_bool_fndecl;
7462 pfndecl = &print_bool_fndecl;
7463 fnname = "__go_print_bool";
7464 }
7465 else if (type->points_to() != NULL
7466 || type->channel_type() != NULL
7467 || type->map_type() != NULL
7468 || type->function_type() != NULL)
7469 {
7470 static tree print_pointer_fndecl;
7471 pfndecl = &print_pointer_fndecl;
7472 fnname = "__go_print_pointer";
7473 arg = fold_convert_loc(location, ptr_type_node, arg);
7474 }
7475 else if (type->interface_type() != NULL)
7476 {
7477 if (type->interface_type()->is_empty())
7478 {
7479 static tree print_empty_interface_fndecl;
7480 pfndecl = &print_empty_interface_fndecl;
7481 fnname = "__go_print_empty_interface";
7482 }
7483 else
7484 {
7485 static tree print_interface_fndecl;
7486 pfndecl = &print_interface_fndecl;
7487 fnname = "__go_print_interface";
7488 }
7489 }
7490 else if (type->is_open_array_type())
7491 {
7492 static tree print_slice_fndecl;
7493 pfndecl = &print_slice_fndecl;
7494 fnname = "__go_print_slice";
7495 }
7496 else
7497 gcc_unreachable();
7498
7499 tree call = Gogo::call_builtin(pfndecl,
7500 location,
7501 fnname,
7502 1,
7503 void_type_node,
7504 TREE_TYPE(arg),
7505 arg);
7506 append_to_statement_list(call, &stmt_list);
7507 }
7508 }
7509
7510 if (is_ln)
7511 {
7512 static tree print_nl_fndecl;
7513 tree call = Gogo::call_builtin(&print_nl_fndecl,
7514 location,
7515 "__go_print_nl",
7516 0,
7517 void_type_node);
7518 append_to_statement_list(call, &stmt_list);
7519 }
7520
7521 return stmt_list;
7522 }
7523
7524 case BUILTIN_PANIC:
7525 {
7526 const Expression_list* args = this->args();
7527 gcc_assert(args != NULL && args->size() == 1);
7528 Expression* arg = args->front();
7529 tree arg_tree = arg->get_tree(context);
7530 if (arg_tree == error_mark_node)
7531 return error_mark_node;
7532 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7533 arg_tree = Expression::convert_for_assignment(context, empty,
7534 arg->type(),
7535 arg_tree, location);
7536 static tree panic_fndecl;
7537 tree call = Gogo::call_builtin(&panic_fndecl,
7538 location,
7539 "__go_panic",
7540 1,
7541 void_type_node,
7542 TREE_TYPE(arg_tree),
7543 arg_tree);
7544 // This function will throw an exception.
7545 TREE_NOTHROW(panic_fndecl) = 0;
7546 // This function will not return.
7547 TREE_THIS_VOLATILE(panic_fndecl) = 1;
7548 return call;
7549 }
7550
7551 case BUILTIN_RECOVER:
7552 {
7553 // The argument is set when building recover thunks. It's a
7554 // boolean value which is true if we can recover a value now.
7555 const Expression_list* args = this->args();
7556 gcc_assert(args != NULL && args->size() == 1);
7557 Expression* arg = args->front();
7558 tree arg_tree = arg->get_tree(context);
7559 if (arg_tree == error_mark_node)
7560 return error_mark_node;
7561
7562 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7563 tree empty_tree = empty->get_tree(context->gogo());
7564
7565 Type* nil_type = Type::make_nil_type();
7566 Expression* nil = Expression::make_nil(location);
7567 tree nil_tree = nil->get_tree(context);
7568 tree empty_nil_tree = Expression::convert_for_assignment(context,
7569 empty,
7570 nil_type,
7571 nil_tree,
7572 location);
7573
7574 // We need to handle a deferred call to recover specially,
7575 // because it changes whether it can recover a panic or not.
7576 // See test7 in test/recover1.go.
7577 tree call;
7578 if (this->is_deferred())
7579 {
7580 static tree deferred_recover_fndecl;
7581 call = Gogo::call_builtin(&deferred_recover_fndecl,
7582 location,
7583 "__go_deferred_recover",
7584 0,
7585 empty_tree);
7586 }
7587 else
7588 {
7589 static tree recover_fndecl;
7590 call = Gogo::call_builtin(&recover_fndecl,
7591 location,
7592 "__go_recover",
7593 0,
7594 empty_tree);
7595 }
7596 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
7597 call, empty_nil_tree);
7598 }
7599
7600 case BUILTIN_CLOSE:
7601 case BUILTIN_CLOSED:
7602 {
7603 const Expression_list* args = this->args();
7604 gcc_assert(args != NULL && args->size() == 1);
7605 Expression* arg = args->front();
7606 tree arg_tree = arg->get_tree(context);
7607 if (arg_tree == error_mark_node)
7608 return error_mark_node;
7609 if (this->code_ == BUILTIN_CLOSE)
7610 {
7611 static tree close_fndecl;
7612 return Gogo::call_builtin(&close_fndecl,
7613 location,
7614 "__go_builtin_close",
7615 1,
7616 void_type_node,
7617 TREE_TYPE(arg_tree),
7618 arg_tree);
7619 }
7620 else
7621 {
7622 static tree closed_fndecl;
7623 return Gogo::call_builtin(&closed_fndecl,
7624 location,
7625 "__go_builtin_closed",
7626 1,
7627 boolean_type_node,
7628 TREE_TYPE(arg_tree),
7629 arg_tree);
7630 }
7631 }
7632
7633 case BUILTIN_SIZEOF:
7634 case BUILTIN_OFFSETOF:
7635 case BUILTIN_ALIGNOF:
7636 {
7637 mpz_t val;
7638 mpz_init(val);
7639 Type* dummy;
7640 bool b = this->integer_constant_value(true, val, &dummy);
7641 gcc_assert(b);
7642 tree type = Type::lookup_integer_type("int")->get_tree(gogo);
7643 tree ret = Expression::integer_constant_tree(val, type);
7644 mpz_clear(val);
7645 return ret;
7646 }
7647
7648 case BUILTIN_COPY:
7649 {
7650 const Expression_list* args = this->args();
7651 gcc_assert(args != NULL && args->size() == 2);
7652 Expression* arg1 = args->front();
7653 Expression* arg2 = args->back();
7654
7655 tree arg1_tree = arg1->get_tree(context);
7656 tree arg2_tree = arg2->get_tree(context);
7657 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
7658 return error_mark_node;
7659
7660 Type* arg1_type = arg1->type();
7661 Array_type* at = arg1_type->array_type();
7662 arg1_tree = save_expr(arg1_tree);
7663 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
7664 tree arg1_len = at->length_tree(gogo, arg1_tree);
7665
7666 Type* arg2_type = arg2->type();
7667 tree arg2_val;
7668 tree arg2_len;
7669 if (arg2_type->is_open_array_type())
7670 {
7671 at = arg2_type->array_type();
7672 arg2_tree = save_expr(arg2_tree);
7673 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
7674 arg2_len = at->length_tree(gogo, arg2_tree);
7675 }
7676 else
7677 {
7678 arg2_tree = save_expr(arg2_tree);
7679 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
7680 arg2_len = String_type::length_tree(gogo, arg2_tree);
7681 }
7682
7683 arg1_len = save_expr(arg1_len);
7684 arg2_len = save_expr(arg2_len);
7685 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
7686 fold_build2_loc(location, LT_EXPR,
7687 boolean_type_node,
7688 arg1_len, arg2_len),
7689 arg1_len, arg2_len);
7690 len = save_expr(len);
7691
7692 Type* element_type = at->element_type();
7693 tree element_type_tree = element_type->get_tree(gogo);
7694 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
7695 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
7696 len);
7697 bytecount = fold_build2_loc(location, MULT_EXPR,
7698 TREE_TYPE(element_size),
7699 bytecount, element_size);
7700 bytecount = fold_convert_loc(location, size_type_node, bytecount);
7701
7702 tree call = build_call_expr_loc(location,
7703 built_in_decls[BUILT_IN_MEMMOVE],
7704 3, arg1_val, arg2_val, bytecount);
7705
7706 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
7707 call, len);
7708 }
7709
7710 case BUILTIN_APPEND:
7711 {
7712 const Expression_list* args = this->args();
7713 gcc_assert(args != NULL && args->size() == 2);
7714 Expression* arg1 = args->front();
7715 Expression* arg2 = args->back();
7716
7717 tree arg1_tree = arg1->get_tree(context);
7718 tree arg2_tree = arg2->get_tree(context);
7719 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
7720 return error_mark_node;
7721
7722 tree descriptor_tree = arg1->type()->type_descriptor_pointer(gogo);
7723
7724 // We rebuild the decl each time since the slice types may
7725 // change.
7726 tree append_fndecl = NULL_TREE;
7727 return Gogo::call_builtin(&append_fndecl,
7728 location,
7729 "__go_append",
7730 3,
7731 TREE_TYPE(arg1_tree),
7732 TREE_TYPE(descriptor_tree),
7733 descriptor_tree,
7734 TREE_TYPE(arg1_tree),
7735 arg1_tree,
7736 TREE_TYPE(arg2_tree),
7737 arg2_tree);
7738 }
7739
7740 case BUILTIN_REAL:
7741 case BUILTIN_IMAG:
7742 {
7743 const Expression_list* args = this->args();
7744 gcc_assert(args != NULL && args->size() == 1);
7745 Expression* arg = args->front();
7746 tree arg_tree = arg->get_tree(context);
7747 if (arg_tree == error_mark_node)
7748 return error_mark_node;
7749 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
7750 if (this->code_ == BUILTIN_REAL)
7751 return fold_build1_loc(location, REALPART_EXPR,
7752 TREE_TYPE(TREE_TYPE(arg_tree)),
7753 arg_tree);
7754 else
7755 return fold_build1_loc(location, IMAGPART_EXPR,
7756 TREE_TYPE(TREE_TYPE(arg_tree)),
7757 arg_tree);
7758 }
7759
7760 case BUILTIN_CMPLX:
7761 {
7762 const Expression_list* args = this->args();
7763 gcc_assert(args != NULL && args->size() == 2);
7764 tree r = args->front()->get_tree(context);
7765 tree i = args->back()->get_tree(context);
7766 if (r == error_mark_node || i == error_mark_node)
7767 return error_mark_node;
7768 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
7769 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
7770 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
7771 return fold_build2_loc(location, COMPLEX_EXPR,
7772 build_complex_type(TREE_TYPE(r)),
7773 r, i);
7774 }
7775
7776 default:
7777 gcc_unreachable();
7778 }
7779 }
7780
7781 // We have to support exporting a builtin call expression, because
7782 // code can set a constant to the result of a builtin expression.
7783
7784 void
7785 Builtin_call_expression::do_export(Export* exp) const
7786 {
7787 bool ok = false;
7788
7789 mpz_t val;
7790 mpz_init(val);
7791 Type* dummy;
7792 if (this->integer_constant_value(true, val, &dummy))
7793 {
7794 Integer_expression::export_integer(exp, val);
7795 ok = true;
7796 }
7797 mpz_clear(val);
7798
7799 if (!ok)
7800 {
7801 mpfr_t fval;
7802 mpfr_init(fval);
7803 if (this->float_constant_value(fval, &dummy))
7804 {
7805 Float_expression::export_float(exp, fval);
7806 ok = true;
7807 }
7808 mpfr_clear(fval);
7809 }
7810
7811 if (!ok)
7812 {
7813 mpfr_t real;
7814 mpfr_t imag;
7815 mpfr_init(real);
7816 mpfr_init(imag);
7817 if (this->complex_constant_value(real, imag, &dummy))
7818 {
7819 Complex_expression::export_complex(exp, real, imag);
7820 ok = true;
7821 }
7822 mpfr_clear(real);
7823 mpfr_clear(imag);
7824 }
7825
7826 if (!ok)
7827 {
7828 error_at(this->location(), "value is not constant");
7829 return;
7830 }
7831
7832 // A trailing space lets us reliably identify the end of the number.
7833 exp->write_c_string(" ");
7834 }
7835
7836 // Class Call_expression.
7837
7838 // Traversal.
7839
7840 int
7841 Call_expression::do_traverse(Traverse* traverse)
7842 {
7843 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
7844 return TRAVERSE_EXIT;
7845 if (this->args_ != NULL)
7846 {
7847 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
7848 return TRAVERSE_EXIT;
7849 }
7850 return TRAVERSE_CONTINUE;
7851 }
7852
7853 // Lower a call statement.
7854
7855 Expression*
7856 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
7857 {
7858 // A type case can look like a function call.
7859 if (this->fn_->is_type_expression()
7860 && this->args_ != NULL
7861 && this->args_->size() == 1)
7862 return Expression::make_cast(this->fn_->type(), this->args_->front(),
7863 this->location());
7864
7865 // Recognize a call to a builtin function.
7866 Func_expression* fne = this->fn_->func_expression();
7867 if (fne != NULL
7868 && fne->named_object()->is_function_declaration()
7869 && fne->named_object()->func_declaration_value()->type()->is_builtin())
7870 return new Builtin_call_expression(gogo, this->fn_, this->args_,
7871 this->is_varargs_, this->location());
7872
7873 // Handle an argument which is a call to a function which returns
7874 // multiple results.
7875 if (this->args_ != NULL
7876 && this->args_->size() == 1
7877 && this->args_->front()->call_expression() != NULL
7878 && this->fn_->type()->function_type() != NULL)
7879 {
7880 Function_type* fntype = this->fn_->type()->function_type();
7881 size_t rc = this->args_->front()->call_expression()->result_count();
7882 if (rc > 1
7883 && fntype->parameters() != NULL
7884 && (fntype->parameters()->size() == rc
7885 || (fntype->is_varargs()
7886 && fntype->parameters()->size() - 1 <= rc)))
7887 {
7888 Call_expression* call = this->args_->front()->call_expression();
7889 Expression_list* args = new Expression_list;
7890 for (size_t i = 0; i < rc; ++i)
7891 args->push_back(Expression::make_call_result(call, i));
7892 // We can't return a new call expression here, because this
7893 // one may be referenced by Call_result expressions. FIXME.
7894 delete this->args_;
7895 this->args_ = args;
7896 }
7897 }
7898
7899 // Handle a call to a varargs function by packaging up the extra
7900 // parameters.
7901 if (this->fn_->type()->function_type() != NULL
7902 && this->fn_->type()->function_type()->is_varargs())
7903 {
7904 Function_type* fntype = this->fn_->type()->function_type();
7905 const Typed_identifier_list* parameters = fntype->parameters();
7906 gcc_assert(parameters != NULL && !parameters->empty());
7907 Type* varargs_type = parameters->back().type();
7908 return this->lower_varargs(gogo, function, varargs_type,
7909 parameters->size());
7910 }
7911
7912 return this;
7913 }
7914
7915 // Lower a call to a varargs function. FUNCTION is the function in
7916 // which the call occurs--it's not the function we are calling.
7917 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
7918 // PARAM_COUNT is the number of parameters of the function we are
7919 // calling; the last of these parameters will be the varargs
7920 // parameter.
7921
7922 Expression*
7923 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
7924 Type* varargs_type, size_t param_count)
7925 {
7926 if (this->varargs_are_lowered_)
7927 return this;
7928
7929 source_location loc = this->location();
7930
7931 gcc_assert(param_count > 0);
7932 gcc_assert(varargs_type->is_open_array_type());
7933
7934 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
7935 if (arg_count < param_count - 1)
7936 {
7937 // Not enough arguments; will be caught in check_types.
7938 return this;
7939 }
7940
7941 Expression_list* old_args = this->args_;
7942 Expression_list* new_args = new Expression_list();
7943 bool push_empty_arg = false;
7944 if (old_args == NULL || old_args->empty())
7945 {
7946 gcc_assert(param_count == 1);
7947 push_empty_arg = true;
7948 }
7949 else
7950 {
7951 Expression_list::const_iterator pa;
7952 int i = 1;
7953 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
7954 {
7955 if (static_cast<size_t>(i) == param_count)
7956 break;
7957 new_args->push_back(*pa);
7958 }
7959
7960 // We have reached the varargs parameter.
7961
7962 bool issued_error = false;
7963 if (pa == old_args->end())
7964 push_empty_arg = true;
7965 else if (pa + 1 == old_args->end() && this->is_varargs_)
7966 new_args->push_back(*pa);
7967 else if (this->is_varargs_)
7968 {
7969 this->report_error(_("too many arguments"));
7970 return this;
7971 }
7972 else if (pa + 1 == old_args->end()
7973 && this->is_compatible_varargs_argument(function, *pa,
7974 varargs_type,
7975 &issued_error))
7976 new_args->push_back(*pa);
7977 else
7978 {
7979 Type* element_type = varargs_type->array_type()->element_type();
7980 Expression_list* vals = new Expression_list;
7981 for (; pa != old_args->end(); ++pa, ++i)
7982 {
7983 // Check types here so that we get a better message.
7984 Type* patype = (*pa)->type();
7985 source_location paloc = (*pa)->location();
7986 if (!this->check_argument_type(i, element_type, patype,
7987 paloc, issued_error))
7988 continue;
7989 vals->push_back(*pa);
7990 }
7991 Expression* val =
7992 Expression::make_slice_composite_literal(varargs_type, vals, loc);
7993 new_args->push_back(val);
7994 }
7995 }
7996
7997 if (push_empty_arg)
7998 new_args->push_back(Expression::make_nil(loc));
7999
8000 // We can't return a new call expression here, because this one may
8001 // be referenced by Call_result expressions. FIXME.
8002 if (old_args != NULL)
8003 delete old_args;
8004 this->args_ = new_args;
8005 this->varargs_are_lowered_ = true;
8006
8007 // Lower all the new subexpressions.
8008 Expression* ret = this;
8009 gogo->lower_expression(function, &ret);
8010 gcc_assert(ret == this);
8011 return ret;
8012 }
8013
8014 // Return true if ARG is a varargs argment which should be passed to
8015 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8016 // will be the last argument passed in the call, and PARAM_TYPE will
8017 // be the type of the last parameter of the varargs function being
8018 // called.
8019
8020 bool
8021 Call_expression::is_compatible_varargs_argument(Named_object* function,
8022 Expression* arg,
8023 Type* param_type,
8024 bool* issued_error)
8025 {
8026 *issued_error = false;
8027
8028 Type* var_type = NULL;
8029
8030 // The simple case is passing the varargs parameter of the caller.
8031 Var_expression* ve = arg->var_expression();
8032 if (ve != NULL && ve->named_object()->is_variable())
8033 {
8034 Variable* var = ve->named_object()->var_value();
8035 if (var->is_varargs_parameter())
8036 var_type = var->type();
8037 }
8038
8039 // The complex case is passing the varargs parameter of some
8040 // enclosing function. This will look like passing down *c.f where
8041 // c is the closure variable and f is a field in the closure.
8042 if (function != NULL
8043 && function->func_value()->needs_closure()
8044 && arg->classification() == EXPRESSION_UNARY)
8045 {
8046 Unary_expression* ue = static_cast<Unary_expression*>(arg);
8047 if (ue->op() == OPERATOR_MULT)
8048 {
8049 Field_reference_expression* fre =
8050 ue->operand()->deref()->field_reference_expression();
8051 if (fre != NULL)
8052 {
8053 Var_expression* ve = fre->expr()->deref()->var_expression();
8054 if (ve != NULL)
8055 {
8056 Named_object* no = ve->named_object();
8057 Function* f = function->func_value();
8058 if (no == f->closure_var())
8059 {
8060 // At this point we know that this indeed a
8061 // reference to some enclosing variable. Now we
8062 // need to figure out whether that variable is a
8063 // varargs parameter.
8064 Named_object* enclosing =
8065 f->enclosing_var(fre->field_index());
8066 Variable* var = enclosing->var_value();
8067 if (var->is_varargs_parameter())
8068 var_type = var->type();
8069 }
8070 }
8071 }
8072 }
8073 }
8074
8075 if (var_type == NULL)
8076 return false;
8077
8078 // We only match if the parameter is the same, with an identical
8079 // type.
8080 Array_type* var_at = var_type->array_type();
8081 gcc_assert(var_at != NULL);
8082 Array_type* param_at = param_type->array_type();
8083 if (param_at != NULL
8084 && Type::are_identical(var_at->element_type(),
8085 param_at->element_type(), NULL))
8086 return true;
8087 error_at(arg->location(), "... mismatch: passing ...T as ...");
8088 *issued_error = true;
8089 return false;
8090 }
8091
8092 // Get the function type. Returns NULL if we don't know the type. If
8093 // this returns NULL, and if_ERROR is true, issues an error.
8094
8095 Function_type*
8096 Call_expression::get_function_type() const
8097 {
8098 return this->fn_->type()->function_type();
8099 }
8100
8101 // Return the number of values which this call will return.
8102
8103 size_t
8104 Call_expression::result_count() const
8105 {
8106 const Function_type* fntype = this->get_function_type();
8107 if (fntype == NULL)
8108 return 0;
8109 if (fntype->results() == NULL)
8110 return 0;
8111 return fntype->results()->size();
8112 }
8113
8114 // Return whether this is a call to the predeclared function recover.
8115
8116 bool
8117 Call_expression::is_recover_call() const
8118 {
8119 return this->do_is_recover_call();
8120 }
8121
8122 // Set the argument to the recover function.
8123
8124 void
8125 Call_expression::set_recover_arg(Expression* arg)
8126 {
8127 this->do_set_recover_arg(arg);
8128 }
8129
8130 // Virtual functions also implemented by Builtin_call_expression.
8131
8132 bool
8133 Call_expression::do_is_recover_call() const
8134 {
8135 return false;
8136 }
8137
8138 void
8139 Call_expression::do_set_recover_arg(Expression*)
8140 {
8141 gcc_unreachable();
8142 }
8143
8144 // Get the type.
8145
8146 Type*
8147 Call_expression::do_type()
8148 {
8149 if (this->type_ != NULL)
8150 return this->type_;
8151
8152 Type* ret;
8153 Function_type* fntype = this->get_function_type();
8154 if (fntype == NULL)
8155 return Type::make_error_type();
8156
8157 const Typed_identifier_list* results = fntype->results();
8158 if (results == NULL)
8159 ret = Type::make_void_type();
8160 else if (results->size() == 1)
8161 ret = results->begin()->type();
8162 else
8163 ret = Type::make_call_multiple_result_type(this);
8164
8165 this->type_ = ret;
8166
8167 return this->type_;
8168 }
8169
8170 // Determine types for a call expression. We can use the function
8171 // parameter types to set the types of the arguments.
8172
8173 void
8174 Call_expression::do_determine_type(const Type_context*)
8175 {
8176 this->fn_->determine_type_no_context();
8177 Function_type* fntype = this->get_function_type();
8178 const Typed_identifier_list* parameters = NULL;
8179 if (fntype != NULL)
8180 parameters = fntype->parameters();
8181 if (this->args_ != NULL)
8182 {
8183 Typed_identifier_list::const_iterator pt;
8184 if (parameters != NULL)
8185 pt = parameters->begin();
8186 for (Expression_list::const_iterator pa = this->args_->begin();
8187 pa != this->args_->end();
8188 ++pa)
8189 {
8190 if (parameters != NULL && pt != parameters->end())
8191 {
8192 Type_context subcontext(pt->type(), false);
8193 (*pa)->determine_type(&subcontext);
8194 ++pt;
8195 }
8196 else
8197 (*pa)->determine_type_no_context();
8198 }
8199 }
8200 }
8201
8202 // Check types for parameter I.
8203
8204 bool
8205 Call_expression::check_argument_type(int i, const Type* parameter_type,
8206 const Type* argument_type,
8207 source_location argument_location,
8208 bool issued_error)
8209 {
8210 std::string reason;
8211 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8212 {
8213 if (!issued_error)
8214 {
8215 if (reason.empty())
8216 error_at(argument_location, "argument %d has incompatible type", i);
8217 else
8218 error_at(argument_location,
8219 "argument %d has incompatible type (%s)",
8220 i, reason.c_str());
8221 }
8222 this->set_is_error();
8223 return false;
8224 }
8225 return true;
8226 }
8227
8228 // Check types.
8229
8230 void
8231 Call_expression::do_check_types(Gogo*)
8232 {
8233 Function_type* fntype = this->get_function_type();
8234 if (fntype == NULL)
8235 {
8236 if (!this->fn_->type()->is_error_type())
8237 this->report_error(_("expected function"));
8238 return;
8239 }
8240
8241 if (fntype->is_method())
8242 {
8243 // We don't support pointers to methods, so the function has to
8244 // be a bound method expression.
8245 Bound_method_expression* bme = this->fn_->bound_method_expression();
8246 if (bme == NULL)
8247 {
8248 this->report_error(_("method call without object"));
8249 return;
8250 }
8251 Type* first_arg_type = bme->first_argument()->type();
8252 if (first_arg_type->points_to() == NULL)
8253 {
8254 // When passing a value, we need to check that we are
8255 // permitted to copy it.
8256 std::string reason;
8257 if (!Type::are_assignable(fntype->receiver()->type(),
8258 first_arg_type, &reason))
8259 {
8260 if (reason.empty())
8261 this->report_error(_("incompatible type for receiver"));
8262 else
8263 {
8264 error_at(this->location(),
8265 "incompatible type for receiver (%s)",
8266 reason.c_str());
8267 this->set_is_error();
8268 }
8269 }
8270 }
8271 }
8272
8273 // Note that varargs was handled by the lower_varargs() method, so
8274 // we don't have to worry about it here.
8275
8276 const Typed_identifier_list* parameters = fntype->parameters();
8277 if (this->args_ == NULL)
8278 {
8279 if (parameters != NULL && !parameters->empty())
8280 this->report_error(_("not enough arguments"));
8281 }
8282 else if (parameters == NULL)
8283 this->report_error(_("too many arguments"));
8284 else
8285 {
8286 int i = 0;
8287 Typed_identifier_list::const_iterator pt = parameters->begin();
8288 for (Expression_list::const_iterator pa = this->args_->begin();
8289 pa != this->args_->end();
8290 ++pa, ++pt, ++i)
8291 {
8292 if (pt == parameters->end())
8293 {
8294 this->report_error(_("too many arguments"));
8295 return;
8296 }
8297 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8298 (*pa)->location(), false);
8299 }
8300 if (pt != parameters->end())
8301 this->report_error(_("not enough arguments"));
8302 }
8303 }
8304
8305 // Return whether we have to use a temporary variable to ensure that
8306 // we evaluate this call expression in order. If the call returns no
8307 // results then it will inevitably be executed last. If the call
8308 // returns more than one result then it will be used with Call_result
8309 // expressions. So we only have to use a temporary variable if the
8310 // call returns exactly one result.
8311
8312 bool
8313 Call_expression::do_must_eval_in_order() const
8314 {
8315 return this->result_count() == 1;
8316 }
8317
8318 // Get the function and the first argument to use when calling a bound
8319 // method.
8320
8321 tree
8322 Call_expression::bound_method_function(Translate_context* context,
8323 Bound_method_expression* bound_method,
8324 tree* first_arg_ptr)
8325 {
8326 Expression* first_argument = bound_method->first_argument();
8327 tree first_arg = first_argument->get_tree(context);
8328 if (first_arg == error_mark_node)
8329 return error_mark_node;
8330
8331 // We always pass a pointer to the first argument when calling a
8332 // method.
8333 if (first_argument->type()->points_to() == NULL)
8334 {
8335 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8336 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8337 || DECL_P(first_arg)
8338 || TREE_CODE(first_arg) == INDIRECT_REF
8339 || TREE_CODE(first_arg) == COMPONENT_REF)
8340 {
8341 first_arg = build_fold_addr_expr(first_arg);
8342 if (DECL_P(first_arg))
8343 TREE_ADDRESSABLE(first_arg) = 1;
8344 }
8345 else
8346 {
8347 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8348 get_name(first_arg));
8349 DECL_IGNORED_P(tmp) = 0;
8350 DECL_INITIAL(tmp) = first_arg;
8351 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8352 build1(DECL_EXPR, void_type_node, tmp),
8353 build_fold_addr_expr(tmp));
8354 TREE_ADDRESSABLE(tmp) = 1;
8355 }
8356 if (first_arg == error_mark_node)
8357 return error_mark_node;
8358 }
8359
8360 Type* fatype = bound_method->first_argument_type();
8361 if (fatype != NULL)
8362 {
8363 if (fatype->points_to() == NULL)
8364 fatype = Type::make_pointer_type(fatype);
8365 first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg);
8366 if (first_arg == error_mark_node
8367 || TREE_TYPE(first_arg) == error_mark_node)
8368 return error_mark_node;
8369 }
8370
8371 *first_arg_ptr = first_arg;
8372
8373 return bound_method->method()->get_tree(context);
8374 }
8375
8376 // Get the function and the first argument to use when calling an
8377 // interface method.
8378
8379 tree
8380 Call_expression::interface_method_function(
8381 Translate_context* context,
8382 Interface_field_reference_expression* interface_method,
8383 tree* first_arg_ptr)
8384 {
8385 tree expr = interface_method->expr()->get_tree(context);
8386 if (expr == error_mark_node)
8387 return error_mark_node;
8388 expr = save_expr(expr);
8389 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8390 if (first_arg == error_mark_node)
8391 return error_mark_node;
8392 *first_arg_ptr = first_arg;
8393 return interface_method->get_function_tree(context, expr);
8394 }
8395
8396 // Build the call expression.
8397
8398 tree
8399 Call_expression::do_get_tree(Translate_context* context)
8400 {
8401 if (this->tree_ != NULL_TREE)
8402 return this->tree_;
8403
8404 Function_type* fntype = this->get_function_type();
8405 if (fntype == NULL)
8406 return error_mark_node;
8407
8408 if (this->fn_->is_error_expression())
8409 return error_mark_node;
8410
8411 Gogo* gogo = context->gogo();
8412 source_location location = this->location();
8413
8414 Func_expression* func = this->fn_->func_expression();
8415 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8416 Interface_field_reference_expression* interface_method =
8417 this->fn_->interface_field_reference_expression();
8418 const bool has_closure = func != NULL && func->closure() != NULL;
8419 const bool is_method = bound_method != NULL || interface_method != NULL;
8420 gcc_assert(!fntype->is_method() || is_method);
8421
8422 int nargs;
8423 tree* args;
8424 if (this->args_ == NULL || this->args_->empty())
8425 {
8426 nargs = is_method ? 1 : 0;
8427 args = nargs == 0 ? NULL : new tree[nargs];
8428 }
8429 else
8430 {
8431 const Typed_identifier_list* params = fntype->parameters();
8432 gcc_assert(params != NULL);
8433
8434 nargs = this->args_->size();
8435 int i = is_method ? 1 : 0;
8436 nargs += i;
8437 args = new tree[nargs];
8438
8439 Typed_identifier_list::const_iterator pp = params->begin();
8440 Expression_list::const_iterator pe;
8441 for (pe = this->args_->begin();
8442 pe != this->args_->end();
8443 ++pe, ++pp, ++i)
8444 {
8445 tree arg_val = (*pe)->get_tree(context);
8446 args[i] = Expression::convert_for_assignment(context,
8447 pp->type(),
8448 (*pe)->type(),
8449 arg_val,
8450 location);
8451 if (args[i] == error_mark_node)
8452 return error_mark_node;
8453 }
8454 gcc_assert(pp == params->end());
8455 gcc_assert(i == nargs);
8456 }
8457
8458 tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo)));
8459 if (rettype == error_mark_node)
8460 return error_mark_node;
8461
8462 tree fn;
8463 if (has_closure)
8464 fn = func->get_tree_without_closure(gogo);
8465 else if (!is_method)
8466 fn = this->fn_->get_tree(context);
8467 else if (bound_method != NULL)
8468 fn = this->bound_method_function(context, bound_method, &args[0]);
8469 else if (interface_method != NULL)
8470 fn = this->interface_method_function(context, interface_method, &args[0]);
8471 else
8472 gcc_unreachable();
8473
8474 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8475 return error_mark_node;
8476
8477 // This is to support builtin math functions when using 80387 math.
8478 tree fndecl = fn;
8479 if (TREE_CODE(fndecl) == ADDR_EXPR)
8480 fndecl = TREE_OPERAND(fndecl, 0);
8481 tree excess_type = NULL_TREE;
8482 if (DECL_P(fndecl)
8483 && DECL_IS_BUILTIN(fndecl)
8484 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8485 && nargs > 0
8486 && ((SCALAR_FLOAT_TYPE_P(rettype)
8487 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8488 || (COMPLEX_FLOAT_TYPE_P(rettype)
8489 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8490 {
8491 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8492 if (excess_type != NULL_TREE)
8493 {
8494 tree excess_fndecl = mathfn_built_in(excess_type,
8495 DECL_FUNCTION_CODE(fndecl));
8496 if (excess_fndecl == NULL_TREE)
8497 excess_type = NULL_TREE;
8498 else
8499 {
8500 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8501 for (int i = 0; i < nargs; ++i)
8502 args[i] = ::convert(excess_type, args[i]);
8503 }
8504 }
8505 }
8506
8507 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
8508 fn, nargs, args);
8509 delete[] args;
8510
8511 SET_EXPR_LOCATION(ret, location);
8512
8513 if (has_closure)
8514 {
8515 tree closure_tree = func->closure()->get_tree(context);
8516 if (closure_tree != error_mark_node)
8517 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
8518 }
8519
8520 // If this is a recursive function type which returns itself, as in
8521 // type F func() F
8522 // we have used ptr_type_node for the return type. Add a cast here
8523 // to the correct type.
8524 if (TREE_TYPE(ret) == ptr_type_node)
8525 {
8526 tree t = this->type()->get_tree(gogo);
8527 ret = fold_convert_loc(location, t, ret);
8528 }
8529
8530 if (excess_type != NULL_TREE)
8531 {
8532 // Calling convert here can undo our excess precision change.
8533 // That may or may not be a bug in convert_to_real.
8534 ret = build1(NOP_EXPR, rettype, ret);
8535 }
8536
8537 // If there is more than one result, we will refer to the call
8538 // multiple times.
8539 if (fntype->results() != NULL && fntype->results()->size() > 1)
8540 ret = save_expr(ret);
8541
8542 this->tree_ = ret;
8543
8544 return ret;
8545 }
8546
8547 // Make a call expression.
8548
8549 Call_expression*
8550 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
8551 source_location location)
8552 {
8553 return new Call_expression(fn, args, is_varargs, location);
8554 }
8555
8556 // A single result from a call which returns multiple results.
8557
8558 class Call_result_expression : public Expression
8559 {
8560 public:
8561 Call_result_expression(Call_expression* call, unsigned int index)
8562 : Expression(EXPRESSION_CALL_RESULT, call->location()),
8563 call_(call), index_(index)
8564 { }
8565
8566 protected:
8567 int
8568 do_traverse(Traverse*);
8569
8570 Type*
8571 do_type();
8572
8573 void
8574 do_determine_type(const Type_context*);
8575
8576 void
8577 do_check_types(Gogo*);
8578
8579 Expression*
8580 do_copy()
8581 {
8582 return new Call_result_expression(this->call_->call_expression(),
8583 this->index_);
8584 }
8585
8586 bool
8587 do_must_eval_in_order() const
8588 { return true; }
8589
8590 tree
8591 do_get_tree(Translate_context*);
8592
8593 private:
8594 // The underlying call expression.
8595 Expression* call_;
8596 // Which result we want.
8597 unsigned int index_;
8598 };
8599
8600 // Traverse a call result.
8601
8602 int
8603 Call_result_expression::do_traverse(Traverse* traverse)
8604 {
8605 if (traverse->remember_expression(this->call_))
8606 {
8607 // We have already traversed the call expression.
8608 return TRAVERSE_CONTINUE;
8609 }
8610 return Expression::traverse(&this->call_, traverse);
8611 }
8612
8613 // Get the type.
8614
8615 Type*
8616 Call_result_expression::do_type()
8617 {
8618 // THIS->CALL_ can be replaced with a temporary reference due to
8619 // Call_expression::do_must_eval_in_order when there is an error.
8620 Call_expression* ce = this->call_->call_expression();
8621 if (ce == NULL)
8622 return Type::make_error_type();
8623 Function_type* fntype = ce->get_function_type();
8624 if (fntype == NULL)
8625 return Type::make_error_type();
8626 const Typed_identifier_list* results = fntype->results();
8627 Typed_identifier_list::const_iterator pr = results->begin();
8628 for (unsigned int i = 0; i < this->index_; ++i)
8629 {
8630 if (pr == results->end())
8631 return Type::make_error_type();
8632 ++pr;
8633 }
8634 if (pr == results->end())
8635 return Type::make_error_type();
8636 return pr->type();
8637 }
8638
8639 // Check the type. This is where we give an error if we're trying to
8640 // extract too many values from a call.
8641
8642 void
8643 Call_result_expression::do_check_types(Gogo*)
8644 {
8645 bool ok = true;
8646 Call_expression* ce = this->call_->call_expression();
8647 if (ce != NULL)
8648 ok = this->index_ < ce->result_count();
8649 else
8650 {
8651 // This can happen when the call returns a single value but we
8652 // are asking for the second result.
8653 if (this->call_->is_error_expression())
8654 return;
8655 ok = false;
8656 }
8657 if (!ok)
8658 error_at(this->location(),
8659 "number of results does not match number of values");
8660 }
8661
8662 // Determine the type. We have nothing to do here, but the 0 result
8663 // needs to pass down to the caller.
8664
8665 void
8666 Call_result_expression::do_determine_type(const Type_context*)
8667 {
8668 if (this->index_ == 0)
8669 this->call_->determine_type_no_context();
8670 }
8671
8672 // Return the tree.
8673
8674 tree
8675 Call_result_expression::do_get_tree(Translate_context* context)
8676 {
8677 tree call_tree = this->call_->get_tree(context);
8678 if (call_tree == error_mark_node)
8679 return error_mark_node;
8680 gcc_assert(TREE_CODE(TREE_TYPE(call_tree)) == RECORD_TYPE);
8681 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
8682 for (unsigned int i = 0; i < this->index_; ++i)
8683 {
8684 gcc_assert(field != NULL_TREE);
8685 field = DECL_CHAIN(field);
8686 }
8687 gcc_assert(field != NULL_TREE);
8688 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
8689 }
8690
8691 // Make a reference to a single result of a call which returns
8692 // multiple results.
8693
8694 Expression*
8695 Expression::make_call_result(Call_expression* call, unsigned int index)
8696 {
8697 return new Call_result_expression(call, index);
8698 }
8699
8700 // Class Index_expression.
8701
8702 // Traversal.
8703
8704 int
8705 Index_expression::do_traverse(Traverse* traverse)
8706 {
8707 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
8708 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
8709 || (this->end_ != NULL
8710 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
8711 return TRAVERSE_EXIT;
8712 return TRAVERSE_CONTINUE;
8713 }
8714
8715 // Lower an index expression. This converts the generic index
8716 // expression into an array index, a string index, or a map index.
8717
8718 Expression*
8719 Index_expression::do_lower(Gogo*, Named_object*, int)
8720 {
8721 source_location location = this->location();
8722 Expression* left = this->left_;
8723 Expression* start = this->start_;
8724 Expression* end = this->end_;
8725
8726 Type* type = left->type();
8727 if (type->is_error_type())
8728 return Expression::make_error(location);
8729 else if (type->array_type() != NULL)
8730 return Expression::make_array_index(left, start, end, location);
8731 else if (type->points_to() != NULL
8732 && type->points_to()->array_type() != NULL
8733 && !type->points_to()->is_open_array_type())
8734 {
8735 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
8736 location);
8737 return Expression::make_array_index(deref, start, end, location);
8738 }
8739 else if (type->is_string_type())
8740 return Expression::make_string_index(left, start, end, location);
8741 else if (type->map_type() != NULL)
8742 {
8743 if (end != NULL)
8744 {
8745 error_at(location, "invalid slice of map");
8746 return Expression::make_error(location);
8747 }
8748 Map_index_expression* ret= Expression::make_map_index(left, start,
8749 location);
8750 if (this->is_lvalue_)
8751 ret->set_is_lvalue();
8752 return ret;
8753 }
8754 else
8755 {
8756 error_at(location,
8757 "attempt to index object which is not array, string, or map");
8758 return Expression::make_error(location);
8759 }
8760 }
8761
8762 // Make an index expression.
8763
8764 Expression*
8765 Expression::make_index(Expression* left, Expression* start, Expression* end,
8766 source_location location)
8767 {
8768 return new Index_expression(left, start, end, location);
8769 }
8770
8771 // An array index. This is used for both indexing and slicing.
8772
8773 class Array_index_expression : public Expression
8774 {
8775 public:
8776 Array_index_expression(Expression* array, Expression* start,
8777 Expression* end, source_location location)
8778 : Expression(EXPRESSION_ARRAY_INDEX, location),
8779 array_(array), start_(start), end_(end), type_(NULL)
8780 { }
8781
8782 protected:
8783 int
8784 do_traverse(Traverse*);
8785
8786 Type*
8787 do_type();
8788
8789 void
8790 do_determine_type(const Type_context*);
8791
8792 void
8793 do_check_types(Gogo*);
8794
8795 Expression*
8796 do_copy()
8797 {
8798 return Expression::make_array_index(this->array_->copy(),
8799 this->start_->copy(),
8800 (this->end_ == NULL
8801 ? NULL
8802 : this->end_->copy()),
8803 this->location());
8804 }
8805
8806 bool
8807 do_is_addressable() const;
8808
8809 void
8810 do_address_taken(bool escapes)
8811 { this->array_->address_taken(escapes); }
8812
8813 tree
8814 do_get_tree(Translate_context*);
8815
8816 private:
8817 // The array we are getting a value from.
8818 Expression* array_;
8819 // The start or only index.
8820 Expression* start_;
8821 // The end index of a slice. This may be NULL for a simple array
8822 // index, or it may be a nil expression for the length of the array.
8823 Expression* end_;
8824 // The type of the expression.
8825 Type* type_;
8826 };
8827
8828 // Array index traversal.
8829
8830 int
8831 Array_index_expression::do_traverse(Traverse* traverse)
8832 {
8833 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
8834 return TRAVERSE_EXIT;
8835 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
8836 return TRAVERSE_EXIT;
8837 if (this->end_ != NULL)
8838 {
8839 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
8840 return TRAVERSE_EXIT;
8841 }
8842 return TRAVERSE_CONTINUE;
8843 }
8844
8845 // Return the type of an array index.
8846
8847 Type*
8848 Array_index_expression::do_type()
8849 {
8850 if (this->type_ == NULL)
8851 {
8852 Array_type* type = this->array_->type()->array_type();
8853 if (type == NULL)
8854 this->type_ = Type::make_error_type();
8855 else if (this->end_ == NULL)
8856 this->type_ = type->element_type();
8857 else if (type->is_open_array_type())
8858 {
8859 // A slice of a slice has the same type as the original
8860 // slice.
8861 this->type_ = this->array_->type()->deref();
8862 }
8863 else
8864 {
8865 // A slice of an array is a slice.
8866 this->type_ = Type::make_array_type(type->element_type(), NULL);
8867 }
8868 }
8869 return this->type_;
8870 }
8871
8872 // Set the type of an array index.
8873
8874 void
8875 Array_index_expression::do_determine_type(const Type_context*)
8876 {
8877 this->array_->determine_type_no_context();
8878 Type_context subcontext(NULL, true);
8879 this->start_->determine_type(&subcontext);
8880 if (this->end_ != NULL)
8881 this->end_->determine_type(&subcontext);
8882 }
8883
8884 // Check types of an array index.
8885
8886 void
8887 Array_index_expression::do_check_types(Gogo*)
8888 {
8889 if (this->start_->type()->integer_type() == NULL)
8890 this->report_error(_("index must be integer"));
8891 if (this->end_ != NULL
8892 && this->end_->type()->integer_type() == NULL
8893 && !this->end_->is_nil_expression())
8894 this->report_error(_("slice end must be integer"));
8895
8896 Array_type* array_type = this->array_->type()->array_type();
8897 gcc_assert(array_type != NULL);
8898
8899 unsigned int int_bits =
8900 Type::lookup_integer_type("int")->integer_type()->bits();
8901
8902 Type* dummy;
8903 mpz_t lval;
8904 mpz_init(lval);
8905 bool lval_valid = (array_type->length() != NULL
8906 && array_type->length()->integer_constant_value(true,
8907 lval,
8908 &dummy));
8909 mpz_t ival;
8910 mpz_init(ival);
8911 if (this->start_->integer_constant_value(true, ival, &dummy))
8912 {
8913 if (mpz_sgn(ival) < 0
8914 || mpz_sizeinbase(ival, 2) >= int_bits
8915 || (lval_valid
8916 && (this->end_ == NULL
8917 ? mpz_cmp(ival, lval) >= 0
8918 : mpz_cmp(ival, lval) > 0)))
8919 {
8920 error_at(this->start_->location(), "array index out of bounds");
8921 this->set_is_error();
8922 }
8923 }
8924 if (this->end_ != NULL && !this->end_->is_nil_expression())
8925 {
8926 if (this->end_->integer_constant_value(true, ival, &dummy))
8927 {
8928 if (mpz_sgn(ival) < 0
8929 || mpz_sizeinbase(ival, 2) >= int_bits
8930 || (lval_valid && mpz_cmp(ival, lval) > 0))
8931 {
8932 error_at(this->end_->location(), "array index out of bounds");
8933 this->set_is_error();
8934 }
8935 }
8936 }
8937 mpz_clear(ival);
8938 mpz_clear(lval);
8939
8940 // A slice of an array requires an addressable array. A slice of a
8941 // slice is always possible.
8942 if (this->end_ != NULL
8943 && !array_type->is_open_array_type()
8944 && !this->array_->is_addressable())
8945 this->report_error(_("array is not addressable"));
8946 }
8947
8948 // Return whether this expression is addressable.
8949
8950 bool
8951 Array_index_expression::do_is_addressable() const
8952 {
8953 // A slice expression is not addressable.
8954 if (this->end_ != NULL)
8955 return false;
8956
8957 // An index into a slice is addressable.
8958 if (this->array_->type()->is_open_array_type())
8959 return true;
8960
8961 // An index into an array is addressable if the array is
8962 // addressable.
8963 return this->array_->is_addressable();
8964 }
8965
8966 // Get a tree for an array index.
8967
8968 tree
8969 Array_index_expression::do_get_tree(Translate_context* context)
8970 {
8971 Gogo* gogo = context->gogo();
8972 source_location loc = this->location();
8973
8974 Array_type* array_type = this->array_->type()->array_type();
8975 gcc_assert(array_type != NULL);
8976
8977 tree type_tree = array_type->get_tree(gogo);
8978
8979 tree array_tree = this->array_->get_tree(context);
8980 if (array_tree == error_mark_node)
8981 return error_mark_node;
8982
8983 if (array_type->length() == NULL && !DECL_P(array_tree))
8984 array_tree = save_expr(array_tree);
8985 tree length_tree = array_type->length_tree(gogo, array_tree);
8986 length_tree = save_expr(length_tree);
8987 tree length_type = TREE_TYPE(length_tree);
8988
8989 tree bad_index = boolean_false_node;
8990
8991 tree start_tree = this->start_->get_tree(context);
8992 if (start_tree == error_mark_node)
8993 return error_mark_node;
8994 if (!DECL_P(start_tree))
8995 start_tree = save_expr(start_tree);
8996 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
8997 start_tree = convert_to_integer(length_type, start_tree);
8998
8999 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9000 loc);
9001
9002 start_tree = fold_convert_loc(loc, length_type, start_tree);
9003 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9004 fold_build2_loc(loc,
9005 (this->end_ == NULL
9006 ? GE_EXPR
9007 : GT_EXPR),
9008 boolean_type_node, start_tree,
9009 length_tree));
9010
9011 int code = (array_type->length() != NULL
9012 ? (this->end_ == NULL
9013 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9014 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9015 : (this->end_ == NULL
9016 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9017 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9018 tree crash = Gogo::runtime_error(code, loc);
9019
9020 if (this->end_ == NULL)
9021 {
9022 // Simple array indexing. This has to return an l-value, so
9023 // wrap the index check into START_TREE.
9024 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9025 build3(COND_EXPR, void_type_node,
9026 bad_index, crash, NULL_TREE),
9027 start_tree);
9028 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9029
9030 if (array_type->length() != NULL)
9031 {
9032 // Fixed array.
9033 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9034 start_tree, NULL_TREE, NULL_TREE);
9035 }
9036 else
9037 {
9038 // Open array.
9039 tree values = array_type->value_pointer_tree(gogo, array_tree);
9040 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9041 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9042 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9043 start_tree, element_size);
9044 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9045 TREE_TYPE(values), values, offset);
9046 return build_fold_indirect_ref(ptr);
9047 }
9048 }
9049
9050 // Array slice.
9051
9052 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9053 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9054
9055 tree end_tree;
9056 if (this->end_->is_nil_expression())
9057 end_tree = length_tree;
9058 else
9059 {
9060 end_tree = this->end_->get_tree(context);
9061 if (end_tree == error_mark_node)
9062 return error_mark_node;
9063 if (!DECL_P(end_tree))
9064 end_tree = save_expr(end_tree);
9065 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9066 end_tree = convert_to_integer(length_type, end_tree);
9067
9068 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9069 loc);
9070
9071 end_tree = fold_convert_loc(loc, length_type, end_tree);
9072
9073 capacity_tree = save_expr(capacity_tree);
9074 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9075 fold_build2_loc(loc, LT_EXPR,
9076 boolean_type_node,
9077 end_tree, start_tree),
9078 fold_build2_loc(loc, GT_EXPR,
9079 boolean_type_node,
9080 end_tree, capacity_tree));
9081 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9082 bad_index, bad_end);
9083 }
9084
9085 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9086 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9087
9088 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9089 fold_convert_loc(loc, sizetype, start_tree),
9090 element_size);
9091
9092 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9093
9094 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9095 TREE_TYPE(value_pointer),
9096 value_pointer, offset);
9097
9098 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9099 end_tree, start_tree);
9100
9101 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9102 capacity_tree, start_tree);
9103
9104 tree struct_tree = this->type()->get_tree(gogo);
9105 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9106
9107 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9108
9109 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9110 tree field = TYPE_FIELDS(struct_tree);
9111 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9112 elt->index = field;
9113 elt->value = value_pointer;
9114
9115 elt = VEC_quick_push(constructor_elt, init, NULL);
9116 field = DECL_CHAIN(field);
9117 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9118 elt->index = field;
9119 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9120
9121 elt = VEC_quick_push(constructor_elt, init, NULL);
9122 field = DECL_CHAIN(field);
9123 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9124 elt->index = field;
9125 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9126
9127 tree constructor = build_constructor(struct_tree, init);
9128
9129 if (TREE_CONSTANT(value_pointer)
9130 && TREE_CONSTANT(result_length_tree)
9131 && TREE_CONSTANT(result_capacity_tree))
9132 TREE_CONSTANT(constructor) = 1;
9133
9134 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9135 build3(COND_EXPR, void_type_node,
9136 bad_index, crash, NULL_TREE),
9137 constructor);
9138 }
9139
9140 // Make an array index expression. END may be NULL.
9141
9142 Expression*
9143 Expression::make_array_index(Expression* array, Expression* start,
9144 Expression* end, source_location location)
9145 {
9146 // Taking a slice of a composite literal requires moving the literal
9147 // onto the heap.
9148 if (end != NULL && array->is_composite_literal())
9149 {
9150 array = Expression::make_heap_composite(array, location);
9151 array = Expression::make_unary(OPERATOR_MULT, array, location);
9152 }
9153 return new Array_index_expression(array, start, end, location);
9154 }
9155
9156 // A string index. This is used for both indexing and slicing.
9157
9158 class String_index_expression : public Expression
9159 {
9160 public:
9161 String_index_expression(Expression* string, Expression* start,
9162 Expression* end, source_location location)
9163 : Expression(EXPRESSION_STRING_INDEX, location),
9164 string_(string), start_(start), end_(end)
9165 { }
9166
9167 protected:
9168 int
9169 do_traverse(Traverse*);
9170
9171 Type*
9172 do_type();
9173
9174 void
9175 do_determine_type(const Type_context*);
9176
9177 void
9178 do_check_types(Gogo*);
9179
9180 Expression*
9181 do_copy()
9182 {
9183 return Expression::make_string_index(this->string_->copy(),
9184 this->start_->copy(),
9185 (this->end_ == NULL
9186 ? NULL
9187 : this->end_->copy()),
9188 this->location());
9189 }
9190
9191 tree
9192 do_get_tree(Translate_context*);
9193
9194 private:
9195 // The string we are getting a value from.
9196 Expression* string_;
9197 // The start or only index.
9198 Expression* start_;
9199 // The end index of a slice. This may be NULL for a single index,
9200 // or it may be a nil expression for the length of the string.
9201 Expression* end_;
9202 };
9203
9204 // String index traversal.
9205
9206 int
9207 String_index_expression::do_traverse(Traverse* traverse)
9208 {
9209 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9210 return TRAVERSE_EXIT;
9211 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9212 return TRAVERSE_EXIT;
9213 if (this->end_ != NULL)
9214 {
9215 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9216 return TRAVERSE_EXIT;
9217 }
9218 return TRAVERSE_CONTINUE;
9219 }
9220
9221 // Return the type of a string index.
9222
9223 Type*
9224 String_index_expression::do_type()
9225 {
9226 if (this->end_ == NULL)
9227 return Type::lookup_integer_type("uint8");
9228 else
9229 return Type::make_string_type();
9230 }
9231
9232 // Determine the type of a string index.
9233
9234 void
9235 String_index_expression::do_determine_type(const Type_context*)
9236 {
9237 this->string_->determine_type_no_context();
9238 Type_context subcontext(NULL, true);
9239 this->start_->determine_type(&subcontext);
9240 if (this->end_ != NULL)
9241 this->end_->determine_type(&subcontext);
9242 }
9243
9244 // Check types of a string index.
9245
9246 void
9247 String_index_expression::do_check_types(Gogo*)
9248 {
9249 if (this->start_->type()->integer_type() == NULL)
9250 this->report_error(_("index must be integer"));
9251 if (this->end_ != NULL
9252 && this->end_->type()->integer_type() == NULL
9253 && !this->end_->is_nil_expression())
9254 this->report_error(_("slice end must be integer"));
9255
9256 std::string sval;
9257 bool sval_valid = this->string_->string_constant_value(&sval);
9258
9259 mpz_t ival;
9260 mpz_init(ival);
9261 Type* dummy;
9262 if (this->start_->integer_constant_value(true, ival, &dummy))
9263 {
9264 if (mpz_sgn(ival) < 0
9265 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9266 {
9267 error_at(this->start_->location(), "string index out of bounds");
9268 this->set_is_error();
9269 }
9270 }
9271 if (this->end_ != NULL && !this->end_->is_nil_expression())
9272 {
9273 if (this->end_->integer_constant_value(true, ival, &dummy))
9274 {
9275 if (mpz_sgn(ival) < 0
9276 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9277 {
9278 error_at(this->end_->location(), "string index out of bounds");
9279 this->set_is_error();
9280 }
9281 }
9282 }
9283 mpz_clear(ival);
9284 }
9285
9286 // Get a tree for a string index.
9287
9288 tree
9289 String_index_expression::do_get_tree(Translate_context* context)
9290 {
9291 source_location loc = this->location();
9292
9293 tree string_tree = this->string_->get_tree(context);
9294 if (string_tree == error_mark_node)
9295 return error_mark_node;
9296
9297 if (this->string_->type()->points_to() != NULL)
9298 string_tree = build_fold_indirect_ref(string_tree);
9299 if (!DECL_P(string_tree))
9300 string_tree = save_expr(string_tree);
9301 tree string_type = TREE_TYPE(string_tree);
9302
9303 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9304 length_tree = save_expr(length_tree);
9305 tree length_type = TREE_TYPE(length_tree);
9306
9307 tree bad_index = boolean_false_node;
9308
9309 tree start_tree = this->start_->get_tree(context);
9310 if (start_tree == error_mark_node)
9311 return error_mark_node;
9312 if (!DECL_P(start_tree))
9313 start_tree = save_expr(start_tree);
9314 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9315 start_tree = convert_to_integer(length_type, start_tree);
9316
9317 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9318 loc);
9319
9320 start_tree = fold_convert_loc(loc, length_type, start_tree);
9321
9322 int code = (this->end_ == NULL
9323 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9324 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9325 tree crash = Gogo::runtime_error(code, loc);
9326
9327 if (this->end_ == NULL)
9328 {
9329 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9330 bad_index,
9331 fold_build2_loc(loc, GE_EXPR,
9332 boolean_type_node,
9333 start_tree, length_tree));
9334
9335 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9336 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9337 bytes_tree,
9338 fold_convert_loc(loc, sizetype, start_tree));
9339 tree index = build_fold_indirect_ref_loc(loc, ptr);
9340
9341 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9342 build3(COND_EXPR, void_type_node,
9343 bad_index, crash, NULL_TREE),
9344 index);
9345 }
9346 else
9347 {
9348 tree end_tree;
9349 if (this->end_->is_nil_expression())
9350 end_tree = build_int_cst(length_type, -1);
9351 else
9352 {
9353 end_tree = this->end_->get_tree(context);
9354 if (end_tree == error_mark_node)
9355 return error_mark_node;
9356 if (!DECL_P(end_tree))
9357 end_tree = save_expr(end_tree);
9358 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9359 end_tree = convert_to_integer(length_type, end_tree);
9360
9361 bad_index = Expression::check_bounds(end_tree, length_type,
9362 bad_index, loc);
9363
9364 end_tree = fold_convert_loc(loc, length_type, end_tree);
9365 }
9366
9367 static tree strslice_fndecl;
9368 tree ret = Gogo::call_builtin(&strslice_fndecl,
9369 loc,
9370 "__go_string_slice",
9371 3,
9372 string_type,
9373 string_type,
9374 string_tree,
9375 length_type,
9376 start_tree,
9377 length_type,
9378 end_tree);
9379 // This will panic if the bounds are out of range for the
9380 // string.
9381 TREE_NOTHROW(strslice_fndecl) = 0;
9382
9383 if (bad_index == boolean_false_node)
9384 return ret;
9385 else
9386 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9387 build3(COND_EXPR, void_type_node,
9388 bad_index, crash, NULL_TREE),
9389 ret);
9390 }
9391 }
9392
9393 // Make a string index expression. END may be NULL.
9394
9395 Expression*
9396 Expression::make_string_index(Expression* string, Expression* start,
9397 Expression* end, source_location location)
9398 {
9399 return new String_index_expression(string, start, end, location);
9400 }
9401
9402 // Class Map_index.
9403
9404 // Get the type of the map.
9405
9406 Map_type*
9407 Map_index_expression::get_map_type() const
9408 {
9409 Map_type* mt = this->map_->type()->deref()->map_type();
9410 gcc_assert(mt != NULL);
9411 return mt;
9412 }
9413
9414 // Map index traversal.
9415
9416 int
9417 Map_index_expression::do_traverse(Traverse* traverse)
9418 {
9419 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9420 return TRAVERSE_EXIT;
9421 return Expression::traverse(&this->index_, traverse);
9422 }
9423
9424 // Return the type of a map index.
9425
9426 Type*
9427 Map_index_expression::do_type()
9428 {
9429 Type* type = this->get_map_type()->val_type();
9430 // If this map index is in a tuple assignment, we actually return a
9431 // pointer to the value type. Tuple_map_assignment_statement is
9432 // responsible for handling this correctly. We need to get the type
9433 // right in case this gets assigned to a temporary variable.
9434 if (this->is_in_tuple_assignment_)
9435 type = Type::make_pointer_type(type);
9436 return type;
9437 }
9438
9439 // Fix the type of a map index.
9440
9441 void
9442 Map_index_expression::do_determine_type(const Type_context*)
9443 {
9444 this->map_->determine_type_no_context();
9445 Type_context subcontext(this->get_map_type()->key_type(), false);
9446 this->index_->determine_type(&subcontext);
9447 }
9448
9449 // Check types of a map index.
9450
9451 void
9452 Map_index_expression::do_check_types(Gogo*)
9453 {
9454 std::string reason;
9455 if (!Type::are_assignable(this->get_map_type()->key_type(),
9456 this->index_->type(), &reason))
9457 {
9458 if (reason.empty())
9459 this->report_error(_("incompatible type for map index"));
9460 else
9461 {
9462 error_at(this->location(), "incompatible type for map index (%s)",
9463 reason.c_str());
9464 this->set_is_error();
9465 }
9466 }
9467 }
9468
9469 // Get a tree for a map index.
9470
9471 tree
9472 Map_index_expression::do_get_tree(Translate_context* context)
9473 {
9474 Map_type* type = this->get_map_type();
9475
9476 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
9477 if (valptr == error_mark_node)
9478 return error_mark_node;
9479 valptr = save_expr(valptr);
9480
9481 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
9482
9483 if (this->is_lvalue_)
9484 return build_fold_indirect_ref(valptr);
9485 else if (this->is_in_tuple_assignment_)
9486 {
9487 // Tuple_map_assignment_statement is responsible for using this
9488 // appropriately.
9489 return valptr;
9490 }
9491 else
9492 {
9493 return fold_build3(COND_EXPR, val_type_tree,
9494 fold_build2(EQ_EXPR, boolean_type_node, valptr,
9495 fold_convert(TREE_TYPE(valptr),
9496 null_pointer_node)),
9497 type->val_type()->get_init_tree(context->gogo(),
9498 false),
9499 build_fold_indirect_ref(valptr));
9500 }
9501 }
9502
9503 // Get a tree for the map index. This returns a tree which evaluates
9504 // to a pointer to a value. The pointer will be NULL if the key is
9505 // not in the map.
9506
9507 tree
9508 Map_index_expression::get_value_pointer(Translate_context* context,
9509 bool insert)
9510 {
9511 Map_type* type = this->get_map_type();
9512
9513 tree map_tree = this->map_->get_tree(context);
9514 tree index_tree = this->index_->get_tree(context);
9515 index_tree = Expression::convert_for_assignment(context, type->key_type(),
9516 this->index_->type(),
9517 index_tree,
9518 this->location());
9519 if (map_tree == error_mark_node || index_tree == error_mark_node)
9520 return error_mark_node;
9521
9522 if (this->map_->type()->points_to() != NULL)
9523 map_tree = build_fold_indirect_ref(map_tree);
9524
9525 // We need to pass in a pointer to the key, so stuff it into a
9526 // variable.
9527 tree tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
9528 DECL_IGNORED_P(tmp) = 0;
9529 DECL_INITIAL(tmp) = index_tree;
9530 tree make_tmp = build1(DECL_EXPR, void_type_node, tmp);
9531 tree tmpref = fold_convert(const_ptr_type_node, build_fold_addr_expr(tmp));
9532 TREE_ADDRESSABLE(tmp) = 1;
9533
9534 static tree map_index_fndecl;
9535 tree call = Gogo::call_builtin(&map_index_fndecl,
9536 this->location(),
9537 "__go_map_index",
9538 3,
9539 const_ptr_type_node,
9540 TREE_TYPE(map_tree),
9541 map_tree,
9542 const_ptr_type_node,
9543 tmpref,
9544 boolean_type_node,
9545 (insert
9546 ? boolean_true_node
9547 : boolean_false_node));
9548 // This can panic on a map of interface type if the interface holds
9549 // an uncomparable or unhashable type.
9550 TREE_NOTHROW(map_index_fndecl) = 0;
9551
9552 tree val_type_tree = type->val_type()->get_tree(context->gogo());
9553 if (val_type_tree == error_mark_node)
9554 return error_mark_node;
9555 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
9556
9557 return build2(COMPOUND_EXPR, ptr_val_type_tree,
9558 make_tmp,
9559 fold_convert(ptr_val_type_tree, call));
9560 }
9561
9562 // Make a map index expression.
9563
9564 Map_index_expression*
9565 Expression::make_map_index(Expression* map, Expression* index,
9566 source_location location)
9567 {
9568 return new Map_index_expression(map, index, location);
9569 }
9570
9571 // Class Field_reference_expression.
9572
9573 // Return the type of a field reference.
9574
9575 Type*
9576 Field_reference_expression::do_type()
9577 {
9578 Struct_type* struct_type = this->expr_->type()->struct_type();
9579 gcc_assert(struct_type != NULL);
9580 return struct_type->field(this->field_index_)->type();
9581 }
9582
9583 // Check the types for a field reference.
9584
9585 void
9586 Field_reference_expression::do_check_types(Gogo*)
9587 {
9588 Struct_type* struct_type = this->expr_->type()->struct_type();
9589 gcc_assert(struct_type != NULL);
9590 gcc_assert(struct_type->field(this->field_index_) != NULL);
9591 }
9592
9593 // Get a tree for a field reference.
9594
9595 tree
9596 Field_reference_expression::do_get_tree(Translate_context* context)
9597 {
9598 tree struct_tree = this->expr_->get_tree(context);
9599 if (struct_tree == error_mark_node
9600 || TREE_TYPE(struct_tree) == error_mark_node)
9601 return error_mark_node;
9602 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
9603 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
9604 gcc_assert(field != NULL_TREE);
9605 for (unsigned int i = this->field_index_; i > 0; --i)
9606 {
9607 field = DECL_CHAIN(field);
9608 gcc_assert(field != NULL_TREE);
9609 }
9610 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
9611 NULL_TREE);
9612 }
9613
9614 // Make a reference to a qualified identifier in an expression.
9615
9616 Field_reference_expression*
9617 Expression::make_field_reference(Expression* expr, unsigned int field_index,
9618 source_location location)
9619 {
9620 return new Field_reference_expression(expr, field_index, location);
9621 }
9622
9623 // Class Interface_field_reference_expression.
9624
9625 // Return a tree for the pointer to the function to call.
9626
9627 tree
9628 Interface_field_reference_expression::get_function_tree(Translate_context*,
9629 tree expr)
9630 {
9631 if (this->expr_->type()->points_to() != NULL)
9632 expr = build_fold_indirect_ref(expr);
9633
9634 tree expr_type = TREE_TYPE(expr);
9635 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
9636
9637 tree field = TYPE_FIELDS(expr_type);
9638 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
9639
9640 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
9641 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table)));
9642
9643 table = build_fold_indirect_ref(table);
9644 gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
9645
9646 std::string name = Gogo::unpack_hidden_name(this->name_);
9647 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
9648 field != NULL_TREE;
9649 field = DECL_CHAIN(field))
9650 {
9651 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
9652 break;
9653 }
9654 gcc_assert(field != NULL_TREE);
9655
9656 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
9657 }
9658
9659 // Return a tree for the first argument to pass to the interface
9660 // function.
9661
9662 tree
9663 Interface_field_reference_expression::get_underlying_object_tree(
9664 Translate_context*,
9665 tree expr)
9666 {
9667 if (this->expr_->type()->points_to() != NULL)
9668 expr = build_fold_indirect_ref(expr);
9669
9670 tree expr_type = TREE_TYPE(expr);
9671 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
9672
9673 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
9674 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
9675
9676 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
9677 }
9678
9679 // Traversal.
9680
9681 int
9682 Interface_field_reference_expression::do_traverse(Traverse* traverse)
9683 {
9684 return Expression::traverse(&this->expr_, traverse);
9685 }
9686
9687 // Return the type of an interface field reference.
9688
9689 Type*
9690 Interface_field_reference_expression::do_type()
9691 {
9692 Type* expr_type = this->expr_->type();
9693
9694 Type* points_to = expr_type->points_to();
9695 if (points_to != NULL)
9696 expr_type = points_to;
9697
9698 Interface_type* interface_type = expr_type->interface_type();
9699 if (interface_type == NULL)
9700 return Type::make_error_type();
9701
9702 const Typed_identifier* method = interface_type->find_method(this->name_);
9703 if (method == NULL)
9704 return Type::make_error_type();
9705
9706 return method->type();
9707 }
9708
9709 // Determine types.
9710
9711 void
9712 Interface_field_reference_expression::do_determine_type(const Type_context*)
9713 {
9714 this->expr_->determine_type_no_context();
9715 }
9716
9717 // Check the types for an interface field reference.
9718
9719 void
9720 Interface_field_reference_expression::do_check_types(Gogo*)
9721 {
9722 Type* type = this->expr_->type();
9723
9724 Type* points_to = type->points_to();
9725 if (points_to != NULL)
9726 type = points_to;
9727
9728 Interface_type* interface_type = type->interface_type();
9729 if (interface_type == NULL)
9730 this->report_error(_("expected interface or pointer to interface"));
9731 else
9732 {
9733 const Typed_identifier* method =
9734 interface_type->find_method(this->name_);
9735 if (method == NULL)
9736 {
9737 error_at(this->location(), "method %qs not in interface",
9738 Gogo::message_name(this->name_).c_str());
9739 this->set_is_error();
9740 }
9741 }
9742 }
9743
9744 // Get a tree for a reference to a field in an interface. There is no
9745 // standard tree type representation for this: it's a function
9746 // attached to its first argument, like a Bound_method_expression.
9747 // The only places it may currently be used are in a Call_expression
9748 // or a Go_statement, which will take it apart directly. So this has
9749 // nothing to do at present.
9750
9751 tree
9752 Interface_field_reference_expression::do_get_tree(Translate_context*)
9753 {
9754 gcc_unreachable();
9755 }
9756
9757 // Make a reference to a field in an interface.
9758
9759 Expression*
9760 Expression::make_interface_field_reference(Expression* expr,
9761 const std::string& field,
9762 source_location location)
9763 {
9764 return new Interface_field_reference_expression(expr, field, location);
9765 }
9766
9767 // A general selector. This is a Parser_expression for LEFT.NAME. It
9768 // is lowered after we know the type of the left hand side.
9769
9770 class Selector_expression : public Parser_expression
9771 {
9772 public:
9773 Selector_expression(Expression* left, const std::string& name,
9774 source_location location)
9775 : Parser_expression(EXPRESSION_SELECTOR, location),
9776 left_(left), name_(name)
9777 { }
9778
9779 protected:
9780 int
9781 do_traverse(Traverse* traverse)
9782 { return Expression::traverse(&this->left_, traverse); }
9783
9784 Expression*
9785 do_lower(Gogo*, Named_object*, int);
9786
9787 Expression*
9788 do_copy()
9789 {
9790 return new Selector_expression(this->left_->copy(), this->name_,
9791 this->location());
9792 }
9793
9794 private:
9795 Expression*
9796 lower_method_expression(Gogo*);
9797
9798 // The expression on the left hand side.
9799 Expression* left_;
9800 // The name on the right hand side.
9801 std::string name_;
9802 };
9803
9804 // Lower a selector expression once we know the real type of the left
9805 // hand side.
9806
9807 Expression*
9808 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
9809 {
9810 Expression* left = this->left_;
9811 if (left->is_type_expression())
9812 return this->lower_method_expression(gogo);
9813 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
9814 this->location());
9815 }
9816
9817 // Lower a method expression T.M or (*T).M. We turn this into a
9818 // function literal.
9819
9820 Expression*
9821 Selector_expression::lower_method_expression(Gogo* gogo)
9822 {
9823 source_location location = this->location();
9824 Type* type = this->left_->type();
9825 const std::string& name(this->name_);
9826
9827 bool is_pointer;
9828 if (type->points_to() == NULL)
9829 is_pointer = false;
9830 else
9831 {
9832 is_pointer = true;
9833 type = type->points_to();
9834 }
9835 Named_type* nt = type->named_type();
9836 if (nt == NULL)
9837 {
9838 error_at(location,
9839 ("method expression requires named type or "
9840 "pointer to named type"));
9841 return Expression::make_error(location);
9842 }
9843
9844 bool is_ambiguous;
9845 Method* method = nt->method_function(name, &is_ambiguous);
9846 if (method == NULL)
9847 {
9848 if (!is_ambiguous)
9849 error_at(location, "type %<%s%> has no method %<%s%>",
9850 nt->message_name().c_str(),
9851 Gogo::message_name(name).c_str());
9852 else
9853 error_at(location, "method %<%s%> is ambiguous in type %<%s%>",
9854 Gogo::message_name(name).c_str(),
9855 nt->message_name().c_str());
9856 return Expression::make_error(location);
9857 }
9858
9859 if (!is_pointer && !method->is_value_method())
9860 {
9861 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
9862 nt->message_name().c_str(),
9863 Gogo::message_name(name).c_str());
9864 return Expression::make_error(location);
9865 }
9866
9867 // Build a new function type in which the receiver becomes the first
9868 // argument.
9869 Function_type* method_type = method->type();
9870 gcc_assert(method_type->is_method());
9871
9872 const char* const receiver_name = "$this";
9873 Typed_identifier_list* parameters = new Typed_identifier_list();
9874 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
9875 location));
9876
9877 const Typed_identifier_list* method_parameters = method_type->parameters();
9878 if (method_parameters != NULL)
9879 {
9880 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
9881 p != method_parameters->end();
9882 ++p)
9883 parameters->push_back(*p);
9884 }
9885
9886 const Typed_identifier_list* method_results = method_type->results();
9887 Typed_identifier_list* results;
9888 if (method_results == NULL)
9889 results = NULL;
9890 else
9891 {
9892 results = new Typed_identifier_list();
9893 for (Typed_identifier_list::const_iterator p = method_results->begin();
9894 p != method_results->end();
9895 ++p)
9896 results->push_back(*p);
9897 }
9898
9899 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
9900 location);
9901 if (method_type->is_varargs())
9902 fntype->set_is_varargs();
9903
9904 // We generate methods which always takes a pointer to the receiver
9905 // as their first argument. If this is for a pointer type, we can
9906 // simply reuse the existing function. We use an internal hack to
9907 // get the right type.
9908
9909 if (is_pointer)
9910 {
9911 Named_object* mno = (method->needs_stub_method()
9912 ? method->stub_object()
9913 : method->named_object());
9914 Expression* f = Expression::make_func_reference(mno, NULL, location);
9915 f = Expression::make_cast(fntype, f, location);
9916 Type_conversion_expression* tce =
9917 static_cast<Type_conversion_expression*>(f);
9918 tce->set_may_convert_function_types();
9919 return f;
9920 }
9921
9922 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
9923 location);
9924
9925 Named_object* vno = gogo->lookup(receiver_name, NULL);
9926 gcc_assert(vno != NULL);
9927 Expression* ve = Expression::make_var_reference(vno, location);
9928 Expression* bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
9929 gcc_assert(bm != NULL && !bm->is_error_expression());
9930
9931 Expression_list* args;
9932 if (method_parameters == NULL)
9933 args = NULL;
9934 else
9935 {
9936 args = new Expression_list();
9937 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
9938 p != method_parameters->end();
9939 ++p)
9940 {
9941 vno = gogo->lookup(p->name(), NULL);
9942 gcc_assert(vno != NULL);
9943 args->push_back(Expression::make_var_reference(vno, location));
9944 }
9945 }
9946
9947 Call_expression* call = Expression::make_call(bm, args,
9948 method_type->is_varargs(),
9949 location);
9950
9951 size_t count = call->result_count();
9952 Statement* s;
9953 if (count == 0)
9954 s = Statement::make_statement(call);
9955 else
9956 {
9957 Expression_list* retvals = new Expression_list();
9958 if (count <= 1)
9959 retvals->push_back(call);
9960 else
9961 {
9962 for (size_t i = 0; i < count; ++i)
9963 retvals->push_back(Expression::make_call_result(call, i));
9964 }
9965 s = Statement::make_return_statement(no->func_value()->type()->results(),
9966 retvals, location);
9967 }
9968 gogo->add_statement(s);
9969
9970 gogo->finish_function(location);
9971
9972 return Expression::make_func_reference(no, NULL, location);
9973 }
9974
9975 // Make a selector expression.
9976
9977 Expression*
9978 Expression::make_selector(Expression* left, const std::string& name,
9979 source_location location)
9980 {
9981 return new Selector_expression(left, name, location);
9982 }
9983
9984 // Implement the builtin function new.
9985
9986 class Allocation_expression : public Expression
9987 {
9988 public:
9989 Allocation_expression(Type* type, source_location location)
9990 : Expression(EXPRESSION_ALLOCATION, location),
9991 type_(type)
9992 { }
9993
9994 protected:
9995 int
9996 do_traverse(Traverse* traverse)
9997 { return Type::traverse(this->type_, traverse); }
9998
9999 Type*
10000 do_type()
10001 { return Type::make_pointer_type(this->type_); }
10002
10003 void
10004 do_determine_type(const Type_context*)
10005 { }
10006
10007 void
10008 do_check_types(Gogo*);
10009
10010 Expression*
10011 do_copy()
10012 { return new Allocation_expression(this->type_, this->location()); }
10013
10014 tree
10015 do_get_tree(Translate_context*);
10016
10017 private:
10018 // The type we are allocating.
10019 Type* type_;
10020 };
10021
10022 // Check the type of an allocation expression.
10023
10024 void
10025 Allocation_expression::do_check_types(Gogo*)
10026 {
10027 if (this->type_->function_type() != NULL)
10028 this->report_error(_("invalid new of function type"));
10029 }
10030
10031 // Return a tree for an allocation expression.
10032
10033 tree
10034 Allocation_expression::do_get_tree(Translate_context* context)
10035 {
10036 tree type_tree = this->type_->get_tree(context->gogo());
10037 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10038 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10039 this->location());
10040 return fold_convert(build_pointer_type(type_tree), space);
10041 }
10042
10043 // Make an allocation expression.
10044
10045 Expression*
10046 Expression::make_allocation(Type* type, source_location location)
10047 {
10048 return new Allocation_expression(type, location);
10049 }
10050
10051 // Implement the builtin function make.
10052
10053 class Make_expression : public Expression
10054 {
10055 public:
10056 Make_expression(Type* type, Expression_list* args, source_location location)
10057 : Expression(EXPRESSION_MAKE, location),
10058 type_(type), args_(args)
10059 { }
10060
10061 protected:
10062 int
10063 do_traverse(Traverse* traverse);
10064
10065 Type*
10066 do_type()
10067 { return this->type_; }
10068
10069 void
10070 do_determine_type(const Type_context*);
10071
10072 void
10073 do_check_types(Gogo*);
10074
10075 Expression*
10076 do_copy()
10077 {
10078 return new Make_expression(this->type_, this->args_->copy(),
10079 this->location());
10080 }
10081
10082 tree
10083 do_get_tree(Translate_context*);
10084
10085 private:
10086 // The type we are making.
10087 Type* type_;
10088 // The arguments to pass to the make routine.
10089 Expression_list* args_;
10090 };
10091
10092 // Traversal.
10093
10094 int
10095 Make_expression::do_traverse(Traverse* traverse)
10096 {
10097 if (this->args_ != NULL
10098 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10099 return TRAVERSE_EXIT;
10100 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10101 return TRAVERSE_EXIT;
10102 return TRAVERSE_CONTINUE;
10103 }
10104
10105 // Set types of arguments.
10106
10107 void
10108 Make_expression::do_determine_type(const Type_context*)
10109 {
10110 if (this->args_ != NULL)
10111 {
10112 Type_context context(Type::lookup_integer_type("int"), false);
10113 for (Expression_list::const_iterator pe = this->args_->begin();
10114 pe != this->args_->end();
10115 ++pe)
10116 (*pe)->determine_type(&context);
10117 }
10118 }
10119
10120 // Check types for a make expression.
10121
10122 void
10123 Make_expression::do_check_types(Gogo*)
10124 {
10125 if (this->type_->channel_type() == NULL
10126 && this->type_->map_type() == NULL
10127 && (this->type_->array_type() == NULL
10128 || this->type_->array_type()->length() != NULL))
10129 this->report_error(_("invalid type for make function"));
10130 else if (!this->type_->check_make_expression(this->args_, this->location()))
10131 this->set_is_error();
10132 }
10133
10134 // Return a tree for a make expression.
10135
10136 tree
10137 Make_expression::do_get_tree(Translate_context* context)
10138 {
10139 return this->type_->make_expression_tree(context, this->args_,
10140 this->location());
10141 }
10142
10143 // Make a make expression.
10144
10145 Expression*
10146 Expression::make_make(Type* type, Expression_list* args,
10147 source_location location)
10148 {
10149 return new Make_expression(type, args, location);
10150 }
10151
10152 // Construct a struct.
10153
10154 class Struct_construction_expression : public Expression
10155 {
10156 public:
10157 Struct_construction_expression(Type* type, Expression_list* vals,
10158 source_location location)
10159 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10160 type_(type), vals_(vals)
10161 { }
10162
10163 // Return whether this is a constant initializer.
10164 bool
10165 is_constant_struct() const;
10166
10167 protected:
10168 int
10169 do_traverse(Traverse* traverse);
10170
10171 Type*
10172 do_type()
10173 { return this->type_; }
10174
10175 void
10176 do_determine_type(const Type_context*);
10177
10178 void
10179 do_check_types(Gogo*);
10180
10181 Expression*
10182 do_copy()
10183 {
10184 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10185 this->location());
10186 }
10187
10188 bool
10189 do_is_addressable() const
10190 { return true; }
10191
10192 tree
10193 do_get_tree(Translate_context*);
10194
10195 void
10196 do_export(Export*) const;
10197
10198 private:
10199 // The type of the struct to construct.
10200 Type* type_;
10201 // The list of values, in order of the fields in the struct. A NULL
10202 // entry means that the field should be zero-initialized.
10203 Expression_list* vals_;
10204 };
10205
10206 // Traversal.
10207
10208 int
10209 Struct_construction_expression::do_traverse(Traverse* traverse)
10210 {
10211 if (this->vals_ != NULL
10212 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10213 return TRAVERSE_EXIT;
10214 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10215 return TRAVERSE_EXIT;
10216 return TRAVERSE_CONTINUE;
10217 }
10218
10219 // Return whether this is a constant initializer.
10220
10221 bool
10222 Struct_construction_expression::is_constant_struct() const
10223 {
10224 if (this->vals_ == NULL)
10225 return true;
10226 for (Expression_list::const_iterator pv = this->vals_->begin();
10227 pv != this->vals_->end();
10228 ++pv)
10229 {
10230 if (*pv != NULL
10231 && !(*pv)->is_constant()
10232 && (!(*pv)->is_composite_literal()
10233 || (*pv)->is_nonconstant_composite_literal()))
10234 return false;
10235 }
10236
10237 const Struct_field_list* fields = this->type_->struct_type()->fields();
10238 for (Struct_field_list::const_iterator pf = fields->begin();
10239 pf != fields->end();
10240 ++pf)
10241 {
10242 // There are no constant constructors for interfaces.
10243 if (pf->type()->interface_type() != NULL)
10244 return false;
10245 }
10246
10247 return true;
10248 }
10249
10250 // Final type determination.
10251
10252 void
10253 Struct_construction_expression::do_determine_type(const Type_context*)
10254 {
10255 if (this->vals_ == NULL)
10256 return;
10257 const Struct_field_list* fields = this->type_->struct_type()->fields();
10258 Expression_list::const_iterator pv = this->vals_->begin();
10259 for (Struct_field_list::const_iterator pf = fields->begin();
10260 pf != fields->end();
10261 ++pf, ++pv)
10262 {
10263 if (pv == this->vals_->end())
10264 return;
10265 if (*pv != NULL)
10266 {
10267 Type_context subcontext(pf->type(), false);
10268 (*pv)->determine_type(&subcontext);
10269 }
10270 }
10271 }
10272
10273 // Check types.
10274
10275 void
10276 Struct_construction_expression::do_check_types(Gogo*)
10277 {
10278 if (this->vals_ == NULL)
10279 return;
10280
10281 Struct_type* st = this->type_->struct_type();
10282 if (this->vals_->size() > st->field_count())
10283 {
10284 this->report_error(_("too many expressions for struct"));
10285 return;
10286 }
10287
10288 const Struct_field_list* fields = st->fields();
10289 Expression_list::const_iterator pv = this->vals_->begin();
10290 int i = 0;
10291 for (Struct_field_list::const_iterator pf = fields->begin();
10292 pf != fields->end();
10293 ++pf, ++pv, ++i)
10294 {
10295 if (pv == this->vals_->end())
10296 {
10297 this->report_error(_("too few expressions for struct"));
10298 break;
10299 }
10300
10301 if (*pv == NULL)
10302 continue;
10303
10304 std::string reason;
10305 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10306 {
10307 if (reason.empty())
10308 error_at((*pv)->location(),
10309 "incompatible type for field %d in struct construction",
10310 i + 1);
10311 else
10312 error_at((*pv)->location(),
10313 ("incompatible type for field %d in "
10314 "struct construction (%s)"),
10315 i + 1, reason.c_str());
10316 this->set_is_error();
10317 }
10318 }
10319 gcc_assert(pv == this->vals_->end());
10320 }
10321
10322 // Return a tree for constructing a struct.
10323
10324 tree
10325 Struct_construction_expression::do_get_tree(Translate_context* context)
10326 {
10327 Gogo* gogo = context->gogo();
10328
10329 if (this->vals_ == NULL)
10330 return this->type_->get_init_tree(gogo, false);
10331
10332 tree type_tree = this->type_->get_tree(gogo);
10333 if (type_tree == error_mark_node)
10334 return error_mark_node;
10335 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10336
10337 bool is_constant = true;
10338 const Struct_field_list* fields = this->type_->struct_type()->fields();
10339 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10340 fields->size());
10341 Struct_field_list::const_iterator pf = fields->begin();
10342 Expression_list::const_iterator pv = this->vals_->begin();
10343 for (tree field = TYPE_FIELDS(type_tree);
10344 field != NULL_TREE;
10345 field = DECL_CHAIN(field), ++pf)
10346 {
10347 gcc_assert(pf != fields->end());
10348
10349 tree val;
10350 if (pv == this->vals_->end())
10351 val = pf->type()->get_init_tree(gogo, false);
10352 else if (*pv == NULL)
10353 {
10354 val = pf->type()->get_init_tree(gogo, false);
10355 ++pv;
10356 }
10357 else
10358 {
10359 val = Expression::convert_for_assignment(context, pf->type(),
10360 (*pv)->type(),
10361 (*pv)->get_tree(context),
10362 this->location());
10363 ++pv;
10364 }
10365
10366 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10367 return error_mark_node;
10368
10369 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10370 elt->index = field;
10371 elt->value = val;
10372 if (!TREE_CONSTANT(val))
10373 is_constant = false;
10374 }
10375 gcc_assert(pf == fields->end());
10376
10377 tree ret = build_constructor(type_tree, elts);
10378 if (is_constant)
10379 TREE_CONSTANT(ret) = 1;
10380 return ret;
10381 }
10382
10383 // Export a struct construction.
10384
10385 void
10386 Struct_construction_expression::do_export(Export* exp) const
10387 {
10388 exp->write_c_string("convert(");
10389 exp->write_type(this->type_);
10390 for (Expression_list::const_iterator pv = this->vals_->begin();
10391 pv != this->vals_->end();
10392 ++pv)
10393 {
10394 exp->write_c_string(", ");
10395 if (*pv != NULL)
10396 (*pv)->export_expression(exp);
10397 }
10398 exp->write_c_string(")");
10399 }
10400
10401 // Make a struct composite literal. This used by the thunk code.
10402
10403 Expression*
10404 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
10405 source_location location)
10406 {
10407 gcc_assert(type->struct_type() != NULL);
10408 return new Struct_construction_expression(type, vals, location);
10409 }
10410
10411 // Construct an array. This class is not used directly; instead we
10412 // use the child classes, Fixed_array_construction_expression and
10413 // Open_array_construction_expression.
10414
10415 class Array_construction_expression : public Expression
10416 {
10417 protected:
10418 Array_construction_expression(Expression_classification classification,
10419 Type* type, Expression_list* vals,
10420 source_location location)
10421 : Expression(classification, location),
10422 type_(type), vals_(vals)
10423 { }
10424
10425 public:
10426 // Return whether this is a constant initializer.
10427 bool
10428 is_constant_array() const;
10429
10430 // Return the number of elements.
10431 size_t
10432 element_count() const
10433 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
10434
10435 protected:
10436 int
10437 do_traverse(Traverse* traverse);
10438
10439 Type*
10440 do_type()
10441 { return this->type_; }
10442
10443 void
10444 do_determine_type(const Type_context*);
10445
10446 void
10447 do_check_types(Gogo*);
10448
10449 bool
10450 do_is_addressable() const
10451 { return true; }
10452
10453 void
10454 do_export(Export*) const;
10455
10456 // The list of values.
10457 Expression_list*
10458 vals()
10459 { return this->vals_; }
10460
10461 // Get a constructor tree for the array values.
10462 tree
10463 get_constructor_tree(Translate_context* context, tree type_tree);
10464
10465 private:
10466 // The type of the array to construct.
10467 Type* type_;
10468 // The list of values.
10469 Expression_list* vals_;
10470 };
10471
10472 // Traversal.
10473
10474 int
10475 Array_construction_expression::do_traverse(Traverse* traverse)
10476 {
10477 if (this->vals_ != NULL
10478 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10479 return TRAVERSE_EXIT;
10480 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10481 return TRAVERSE_EXIT;
10482 return TRAVERSE_CONTINUE;
10483 }
10484
10485 // Return whether this is a constant initializer.
10486
10487 bool
10488 Array_construction_expression::is_constant_array() const
10489 {
10490 if (this->vals_ == NULL)
10491 return true;
10492
10493 // There are no constant constructors for interfaces.
10494 if (this->type_->array_type()->element_type()->interface_type() != NULL)
10495 return false;
10496
10497 for (Expression_list::const_iterator pv = this->vals_->begin();
10498 pv != this->vals_->end();
10499 ++pv)
10500 {
10501 if (*pv != NULL
10502 && !(*pv)->is_constant()
10503 && (!(*pv)->is_composite_literal()
10504 || (*pv)->is_nonconstant_composite_literal()))
10505 return false;
10506 }
10507 return true;
10508 }
10509
10510 // Final type determination.
10511
10512 void
10513 Array_construction_expression::do_determine_type(const Type_context*)
10514 {
10515 if (this->vals_ == NULL)
10516 return;
10517 Type_context subcontext(this->type_->array_type()->element_type(), false);
10518 for (Expression_list::const_iterator pv = this->vals_->begin();
10519 pv != this->vals_->end();
10520 ++pv)
10521 {
10522 if (*pv != NULL)
10523 (*pv)->determine_type(&subcontext);
10524 }
10525 }
10526
10527 // Check types.
10528
10529 void
10530 Array_construction_expression::do_check_types(Gogo*)
10531 {
10532 if (this->vals_ == NULL)
10533 return;
10534
10535 Array_type* at = this->type_->array_type();
10536 int i = 0;
10537 Type* element_type = at->element_type();
10538 for (Expression_list::const_iterator pv = this->vals_->begin();
10539 pv != this->vals_->end();
10540 ++pv, ++i)
10541 {
10542 if (*pv != NULL
10543 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
10544 {
10545 error_at((*pv)->location(),
10546 "incompatible type for element %d in composite literal",
10547 i + 1);
10548 this->set_is_error();
10549 }
10550 }
10551
10552 Expression* length = at->length();
10553 if (length != NULL)
10554 {
10555 mpz_t val;
10556 mpz_init(val);
10557 Type* type;
10558 if (at->length()->integer_constant_value(true, val, &type))
10559 {
10560 if (this->vals_->size() > mpz_get_ui(val))
10561 this->report_error(_("too many elements in composite literal"));
10562 }
10563 mpz_clear(val);
10564 }
10565 }
10566
10567 // Get a constructor tree for the array values.
10568
10569 tree
10570 Array_construction_expression::get_constructor_tree(Translate_context* context,
10571 tree type_tree)
10572 {
10573 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
10574 (this->vals_ == NULL
10575 ? 0
10576 : this->vals_->size()));
10577 Type* element_type = this->type_->array_type()->element_type();
10578 bool is_constant = true;
10579 if (this->vals_ != NULL)
10580 {
10581 size_t i = 0;
10582 for (Expression_list::const_iterator pv = this->vals_->begin();
10583 pv != this->vals_->end();
10584 ++pv, ++i)
10585 {
10586 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
10587 elt->index = size_int(i);
10588 if (*pv == NULL)
10589 elt->value = element_type->get_init_tree(context->gogo(), false);
10590 else
10591 {
10592 tree value_tree = (*pv)->get_tree(context);
10593 elt->value = Expression::convert_for_assignment(context,
10594 element_type,
10595 (*pv)->type(),
10596 value_tree,
10597 this->location());
10598 }
10599 if (elt->value == error_mark_node)
10600 return error_mark_node;
10601 if (!TREE_CONSTANT(elt->value))
10602 is_constant = false;
10603 }
10604 }
10605
10606 tree ret = build_constructor(type_tree, values);
10607 if (is_constant)
10608 TREE_CONSTANT(ret) = 1;
10609 return ret;
10610 }
10611
10612 // Export an array construction.
10613
10614 void
10615 Array_construction_expression::do_export(Export* exp) const
10616 {
10617 exp->write_c_string("convert(");
10618 exp->write_type(this->type_);
10619 if (this->vals_ != NULL)
10620 {
10621 for (Expression_list::const_iterator pv = this->vals_->begin();
10622 pv != this->vals_->end();
10623 ++pv)
10624 {
10625 exp->write_c_string(", ");
10626 if (*pv != NULL)
10627 (*pv)->export_expression(exp);
10628 }
10629 }
10630 exp->write_c_string(")");
10631 }
10632
10633 // Construct a fixed array.
10634
10635 class Fixed_array_construction_expression :
10636 public Array_construction_expression
10637 {
10638 public:
10639 Fixed_array_construction_expression(Type* type, Expression_list* vals,
10640 source_location location)
10641 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
10642 type, vals, location)
10643 {
10644 gcc_assert(type->array_type() != NULL
10645 && type->array_type()->length() != NULL);
10646 }
10647
10648 protected:
10649 Expression*
10650 do_copy()
10651 {
10652 return new Fixed_array_construction_expression(this->type(),
10653 (this->vals() == NULL
10654 ? NULL
10655 : this->vals()->copy()),
10656 this->location());
10657 }
10658
10659 tree
10660 do_get_tree(Translate_context*);
10661 };
10662
10663 // Return a tree for constructing a fixed array.
10664
10665 tree
10666 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
10667 {
10668 return this->get_constructor_tree(context,
10669 this->type()->get_tree(context->gogo()));
10670 }
10671
10672 // Construct an open array.
10673
10674 class Open_array_construction_expression : public Array_construction_expression
10675 {
10676 public:
10677 Open_array_construction_expression(Type* type, Expression_list* vals,
10678 source_location location)
10679 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
10680 type, vals, location)
10681 {
10682 gcc_assert(type->array_type() != NULL
10683 && type->array_type()->length() == NULL);
10684 }
10685
10686 protected:
10687 // Note that taking the address of an open array literal is invalid.
10688
10689 Expression*
10690 do_copy()
10691 {
10692 return new Open_array_construction_expression(this->type(),
10693 (this->vals() == NULL
10694 ? NULL
10695 : this->vals()->copy()),
10696 this->location());
10697 }
10698
10699 tree
10700 do_get_tree(Translate_context*);
10701 };
10702
10703 // Return a tree for constructing an open array.
10704
10705 tree
10706 Open_array_construction_expression::do_get_tree(Translate_context* context)
10707 {
10708 Type* element_type = this->type()->array_type()->element_type();
10709 tree element_type_tree = element_type->get_tree(context->gogo());
10710 tree values;
10711 tree length_tree;
10712 if (this->vals() == NULL || this->vals()->empty())
10713 {
10714 // We need to create a unique value.
10715 tree max = size_int(0);
10716 tree constructor_type = build_array_type(element_type_tree,
10717 build_index_type(max));
10718 if (constructor_type == error_mark_node)
10719 return error_mark_node;
10720 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
10721 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
10722 elt->index = size_int(0);
10723 elt->value = element_type->get_init_tree(context->gogo(), false);
10724 values = build_constructor(constructor_type, vec);
10725 if (TREE_CONSTANT(elt->value))
10726 TREE_CONSTANT(values) = 1;
10727 length_tree = size_int(0);
10728 }
10729 else
10730 {
10731 tree max = size_int(this->vals()->size() - 1);
10732 tree constructor_type = build_array_type(element_type_tree,
10733 build_index_type(max));
10734 if (constructor_type == error_mark_node)
10735 return error_mark_node;
10736 values = this->get_constructor_tree(context, constructor_type);
10737 length_tree = size_int(this->vals()->size());
10738 }
10739
10740 if (values == error_mark_node)
10741 return error_mark_node;
10742
10743 bool is_constant_initializer = TREE_CONSTANT(values);
10744 bool is_in_function = context->function() != NULL;
10745
10746 if (is_constant_initializer)
10747 {
10748 tree tmp = build_decl(this->location(), VAR_DECL,
10749 create_tmp_var_name("C"), TREE_TYPE(values));
10750 DECL_EXTERNAL(tmp) = 0;
10751 TREE_PUBLIC(tmp) = 0;
10752 TREE_STATIC(tmp) = 1;
10753 DECL_ARTIFICIAL(tmp) = 1;
10754 if (is_in_function)
10755 {
10756 // If this is not a function, we will only initialize the
10757 // value once, so we can use this directly rather than
10758 // copying it. In that case we can't make it read-only,
10759 // because the program is permitted to change it.
10760 TREE_READONLY(tmp) = 1;
10761 TREE_CONSTANT(tmp) = 1;
10762 }
10763 DECL_INITIAL(tmp) = values;
10764 rest_of_decl_compilation(tmp, 1, 0);
10765 values = tmp;
10766 }
10767
10768 tree space;
10769 tree set;
10770 if (!is_in_function && is_constant_initializer)
10771 {
10772 // Outside of a function, we know the initializer will only run
10773 // once.
10774 space = build_fold_addr_expr(values);
10775 set = NULL_TREE;
10776 }
10777 else
10778 {
10779 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
10780 space = context->gogo()->allocate_memory(element_type, memsize,
10781 this->location());
10782 space = save_expr(space);
10783
10784 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
10785 tree ref = build_fold_indirect_ref_loc(this->location(), s);
10786 TREE_THIS_NOTRAP(ref) = 1;
10787 set = build2(MODIFY_EXPR, void_type_node, ref, values);
10788 }
10789
10790 // Build a constructor for the open array.
10791
10792 tree type_tree = this->type()->get_tree(context->gogo());
10793 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10794
10795 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
10796
10797 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
10798 tree field = TYPE_FIELDS(type_tree);
10799 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
10800 elt->index = field;
10801 elt->value = fold_convert(TREE_TYPE(field), space);
10802
10803 elt = VEC_quick_push(constructor_elt, init, NULL);
10804 field = DECL_CHAIN(field);
10805 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
10806 elt->index = field;
10807 elt->value = fold_convert(TREE_TYPE(field), length_tree);
10808
10809 elt = VEC_quick_push(constructor_elt, init, NULL);
10810 field = DECL_CHAIN(field);
10811 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
10812 elt->index = field;
10813 elt->value = fold_convert(TREE_TYPE(field), length_tree);
10814
10815 tree constructor = build_constructor(type_tree, init);
10816 if (!is_in_function && is_constant_initializer)
10817 TREE_CONSTANT(constructor) = 1;
10818
10819 if (set == NULL_TREE)
10820 return constructor;
10821 else
10822 return build2(COMPOUND_EXPR, type_tree, set, constructor);
10823 }
10824
10825 // Make a slice composite literal. This is used by the type
10826 // descriptor code.
10827
10828 Expression*
10829 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
10830 source_location location)
10831 {
10832 gcc_assert(type->is_open_array_type());
10833 return new Open_array_construction_expression(type, vals, location);
10834 }
10835
10836 // Construct a map.
10837
10838 class Map_construction_expression : public Expression
10839 {
10840 public:
10841 Map_construction_expression(Type* type, Expression_list* vals,
10842 source_location location)
10843 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
10844 type_(type), vals_(vals)
10845 { gcc_assert(vals == NULL || vals->size() % 2 == 0); }
10846
10847 protected:
10848 int
10849 do_traverse(Traverse* traverse);
10850
10851 Type*
10852 do_type()
10853 { return this->type_; }
10854
10855 void
10856 do_determine_type(const Type_context*);
10857
10858 void
10859 do_check_types(Gogo*);
10860
10861 Expression*
10862 do_copy()
10863 {
10864 return new Map_construction_expression(this->type_, this->vals_->copy(),
10865 this->location());
10866 }
10867
10868 tree
10869 do_get_tree(Translate_context*);
10870
10871 void
10872 do_export(Export*) const;
10873
10874 private:
10875 // The type of the map to construct.
10876 Type* type_;
10877 // The list of values.
10878 Expression_list* vals_;
10879 };
10880
10881 // Traversal.
10882
10883 int
10884 Map_construction_expression::do_traverse(Traverse* traverse)
10885 {
10886 if (this->vals_ != NULL
10887 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10888 return TRAVERSE_EXIT;
10889 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10890 return TRAVERSE_EXIT;
10891 return TRAVERSE_CONTINUE;
10892 }
10893
10894 // Final type determination.
10895
10896 void
10897 Map_construction_expression::do_determine_type(const Type_context*)
10898 {
10899 if (this->vals_ == NULL)
10900 return;
10901
10902 Map_type* mt = this->type_->map_type();
10903 Type_context key_context(mt->key_type(), false);
10904 Type_context val_context(mt->val_type(), false);
10905 for (Expression_list::const_iterator pv = this->vals_->begin();
10906 pv != this->vals_->end();
10907 ++pv)
10908 {
10909 (*pv)->determine_type(&key_context);
10910 ++pv;
10911 (*pv)->determine_type(&val_context);
10912 }
10913 }
10914
10915 // Check types.
10916
10917 void
10918 Map_construction_expression::do_check_types(Gogo*)
10919 {
10920 if (this->vals_ == NULL)
10921 return;
10922
10923 Map_type* mt = this->type_->map_type();
10924 int i = 0;
10925 Type* key_type = mt->key_type();
10926 Type* val_type = mt->val_type();
10927 for (Expression_list::const_iterator pv = this->vals_->begin();
10928 pv != this->vals_->end();
10929 ++pv, ++i)
10930 {
10931 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
10932 {
10933 error_at((*pv)->location(),
10934 "incompatible type for element %d key in map construction",
10935 i + 1);
10936 this->set_is_error();
10937 }
10938 ++pv;
10939 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
10940 {
10941 error_at((*pv)->location(),
10942 ("incompatible type for element %d value "
10943 "in map construction"),
10944 i + 1);
10945 this->set_is_error();
10946 }
10947 }
10948 }
10949
10950 // Return a tree for constructing a map.
10951
10952 tree
10953 Map_construction_expression::do_get_tree(Translate_context* context)
10954 {
10955 Gogo* gogo = context->gogo();
10956 source_location loc = this->location();
10957
10958 Map_type* mt = this->type_->map_type();
10959
10960 // Build a struct to hold the key and value.
10961 tree struct_type = make_node(RECORD_TYPE);
10962
10963 Type* key_type = mt->key_type();
10964 tree id = get_identifier("__key");
10965 tree key_field = build_decl(loc, FIELD_DECL, id, key_type->get_tree(gogo));
10966 DECL_CONTEXT(key_field) = struct_type;
10967 TYPE_FIELDS(struct_type) = key_field;
10968
10969 Type* val_type = mt->val_type();
10970 id = get_identifier("__val");
10971 tree val_field = build_decl(loc, FIELD_DECL, id, val_type->get_tree(gogo));
10972 DECL_CONTEXT(val_field) = struct_type;
10973 DECL_CHAIN(key_field) = val_field;
10974
10975 layout_type(struct_type);
10976
10977 bool is_constant = true;
10978 size_t i = 0;
10979 tree valaddr;
10980 tree make_tmp;
10981
10982 if (this->vals_ == NULL || this->vals_->empty())
10983 {
10984 valaddr = null_pointer_node;
10985 make_tmp = NULL_TREE;
10986 }
10987 else
10988 {
10989 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
10990 this->vals_->size() / 2);
10991
10992 for (Expression_list::const_iterator pv = this->vals_->begin();
10993 pv != this->vals_->end();
10994 ++pv, ++i)
10995 {
10996 bool one_is_constant = true;
10997
10998 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
10999
11000 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11001 elt->index = key_field;
11002 tree val_tree = (*pv)->get_tree(context);
11003 elt->value = Expression::convert_for_assignment(context, key_type,
11004 (*pv)->type(),
11005 val_tree, loc);
11006 if (elt->value == error_mark_node)
11007 return error_mark_node;
11008 if (!TREE_CONSTANT(elt->value))
11009 one_is_constant = false;
11010
11011 ++pv;
11012
11013 elt = VEC_quick_push(constructor_elt, one, NULL);
11014 elt->index = val_field;
11015 val_tree = (*pv)->get_tree(context);
11016 elt->value = Expression::convert_for_assignment(context, val_type,
11017 (*pv)->type(),
11018 val_tree, loc);
11019 if (elt->value == error_mark_node)
11020 return error_mark_node;
11021 if (!TREE_CONSTANT(elt->value))
11022 one_is_constant = false;
11023
11024 elt = VEC_quick_push(constructor_elt, values, NULL);
11025 elt->index = size_int(i);
11026 elt->value = build_constructor(struct_type, one);
11027 if (one_is_constant)
11028 TREE_CONSTANT(elt->value) = 1;
11029 else
11030 is_constant = false;
11031 }
11032
11033 tree index_type = build_index_type(size_int(i - 1));
11034 tree array_type = build_array_type(struct_type, index_type);
11035 tree init = build_constructor(array_type, values);
11036 if (is_constant)
11037 TREE_CONSTANT(init) = 1;
11038 tree tmp;
11039 if (current_function_decl != NULL)
11040 {
11041 tmp = create_tmp_var(array_type, get_name(array_type));
11042 DECL_INITIAL(tmp) = init;
11043 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11044 TREE_ADDRESSABLE(tmp) = 1;
11045 }
11046 else
11047 {
11048 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11049 DECL_EXTERNAL(tmp) = 0;
11050 TREE_PUBLIC(tmp) = 0;
11051 TREE_STATIC(tmp) = 1;
11052 DECL_ARTIFICIAL(tmp) = 1;
11053 if (!TREE_CONSTANT(init))
11054 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11055 init);
11056 else
11057 {
11058 TREE_READONLY(tmp) = 1;
11059 TREE_CONSTANT(tmp) = 1;
11060 DECL_INITIAL(tmp) = init;
11061 make_tmp = NULL_TREE;
11062 }
11063 rest_of_decl_compilation(tmp, 1, 0);
11064 }
11065
11066 valaddr = build_fold_addr_expr(tmp);
11067 }
11068
11069 tree descriptor = gogo->map_descriptor(mt);
11070
11071 tree type_tree = this->type_->get_tree(gogo);
11072
11073 static tree construct_map_fndecl;
11074 tree call = Gogo::call_builtin(&construct_map_fndecl,
11075 loc,
11076 "__go_construct_map",
11077 6,
11078 type_tree,
11079 TREE_TYPE(descriptor),
11080 descriptor,
11081 sizetype,
11082 size_int(i),
11083 sizetype,
11084 TYPE_SIZE_UNIT(struct_type),
11085 sizetype,
11086 byte_position(val_field),
11087 sizetype,
11088 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11089 const_ptr_type_node,
11090 fold_convert(const_ptr_type_node, valaddr));
11091
11092 tree ret;
11093 if (make_tmp == NULL)
11094 ret = call;
11095 else
11096 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11097 return ret;
11098 }
11099
11100 // Export an array construction.
11101
11102 void
11103 Map_construction_expression::do_export(Export* exp) const
11104 {
11105 exp->write_c_string("convert(");
11106 exp->write_type(this->type_);
11107 for (Expression_list::const_iterator pv = this->vals_->begin();
11108 pv != this->vals_->end();
11109 ++pv)
11110 {
11111 exp->write_c_string(", ");
11112 (*pv)->export_expression(exp);
11113 }
11114 exp->write_c_string(")");
11115 }
11116
11117 // A general composite literal. This is lowered to a type specific
11118 // version.
11119
11120 class Composite_literal_expression : public Parser_expression
11121 {
11122 public:
11123 Composite_literal_expression(Type* type, int depth, bool has_keys,
11124 Expression_list* vals, source_location location)
11125 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11126 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11127 { }
11128
11129 protected:
11130 int
11131 do_traverse(Traverse* traverse);
11132
11133 Expression*
11134 do_lower(Gogo*, Named_object*, int);
11135
11136 Expression*
11137 do_copy()
11138 {
11139 return new Composite_literal_expression(this->type_, this->depth_,
11140 this->has_keys_,
11141 (this->vals_ == NULL
11142 ? NULL
11143 : this->vals_->copy()),
11144 this->location());
11145 }
11146
11147 private:
11148 Expression*
11149 lower_struct(Type*);
11150
11151 Expression*
11152 lower_array(Type*);
11153
11154 Expression*
11155 make_array(Type*, Expression_list*);
11156
11157 Expression*
11158 lower_map(Type*);
11159
11160 // The type of the composite literal.
11161 Type* type_;
11162 // The depth within a list of composite literals within a composite
11163 // literal, when the type is omitted.
11164 int depth_;
11165 // The values to put in the composite literal.
11166 Expression_list* vals_;
11167 // If this is true, then VALS_ is a list of pairs: a key and a
11168 // value. In an array initializer, a missing key will be NULL.
11169 bool has_keys_;
11170 };
11171
11172 // Traversal.
11173
11174 int
11175 Composite_literal_expression::do_traverse(Traverse* traverse)
11176 {
11177 if (this->vals_ != NULL
11178 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11179 return TRAVERSE_EXIT;
11180 return Type::traverse(this->type_, traverse);
11181 }
11182
11183 // Lower a generic composite literal into a specific version based on
11184 // the type.
11185
11186 Expression*
11187 Composite_literal_expression::do_lower(Gogo*, Named_object*, int)
11188 {
11189 Type* type = this->type_;
11190
11191 for (int depth = this->depth_; depth > 0; --depth)
11192 {
11193 if (type->array_type() != NULL)
11194 type = type->array_type()->element_type();
11195 else if (type->map_type() != NULL)
11196 type = type->map_type()->val_type();
11197 else
11198 {
11199 if (!type->is_error_type())
11200 error_at(this->location(),
11201 ("may only omit types within composite literals "
11202 "of slice, array, or map type"));
11203 return Expression::make_error(this->location());
11204 }
11205 }
11206
11207 if (type->is_error_type())
11208 return Expression::make_error(this->location());
11209 else if (type->struct_type() != NULL)
11210 return this->lower_struct(type);
11211 else if (type->array_type() != NULL)
11212 return this->lower_array(type);
11213 else if (type->map_type() != NULL)
11214 return this->lower_map(type);
11215 else
11216 {
11217 error_at(this->location(),
11218 ("expected struct, slice, array, or map type "
11219 "for composite literal"));
11220 return Expression::make_error(this->location());
11221 }
11222 }
11223
11224 // Lower a struct composite literal.
11225
11226 Expression*
11227 Composite_literal_expression::lower_struct(Type* type)
11228 {
11229 source_location location = this->location();
11230 Struct_type* st = type->struct_type();
11231 if (this->vals_ == NULL || !this->has_keys_)
11232 return new Struct_construction_expression(type, this->vals_, location);
11233
11234 size_t field_count = st->field_count();
11235 std::vector<Expression*> vals(field_count);
11236 Expression_list::const_iterator p = this->vals_->begin();
11237 while (p != this->vals_->end())
11238 {
11239 Expression* name_expr = *p;
11240
11241 ++p;
11242 gcc_assert(p != this->vals_->end());
11243 Expression* val = *p;
11244
11245 ++p;
11246
11247 if (name_expr == NULL)
11248 {
11249 error_at(val->location(), "mixture of field and value initializers");
11250 return Expression::make_error(location);
11251 }
11252
11253 bool bad_key = false;
11254 std::string name;
11255 switch (name_expr->classification())
11256 {
11257 case EXPRESSION_UNKNOWN_REFERENCE:
11258 name = name_expr->unknown_expression()->name();
11259 break;
11260
11261 case EXPRESSION_CONST_REFERENCE:
11262 name = static_cast<Const_expression*>(name_expr)->name();
11263 break;
11264
11265 case EXPRESSION_TYPE:
11266 {
11267 Type* t = name_expr->type();
11268 Named_type* nt = t->named_type();
11269 if (nt == NULL)
11270 bad_key = true;
11271 else
11272 name = nt->name();
11273 }
11274 break;
11275
11276 case EXPRESSION_VAR_REFERENCE:
11277 name = name_expr->var_expression()->name();
11278 break;
11279
11280 case EXPRESSION_FUNC_REFERENCE:
11281 name = name_expr->func_expression()->name();
11282 break;
11283
11284 case EXPRESSION_UNARY:
11285 // If there is a local variable around with the same name as
11286 // the field, and this occurs in the closure, then the
11287 // parser may turn the field reference into an indirection
11288 // through the closure. FIXME: This is a mess.
11289 {
11290 bad_key = true;
11291 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11292 if (ue->op() == OPERATOR_MULT)
11293 {
11294 Field_reference_expression* fre =
11295 ue->operand()->field_reference_expression();
11296 if (fre != NULL)
11297 {
11298 Struct_type* st =
11299 fre->expr()->type()->deref()->struct_type();
11300 if (st != NULL)
11301 {
11302 const Struct_field* sf = st->field(fre->field_index());
11303 name = sf->field_name();
11304 char buf[20];
11305 snprintf(buf, sizeof buf, "%u", fre->field_index());
11306 size_t buflen = strlen(buf);
11307 if (name.compare(name.length() - buflen, buflen, buf)
11308 == 0)
11309 {
11310 name = name.substr(0, name.length() - buflen);
11311 bad_key = false;
11312 }
11313 }
11314 }
11315 }
11316 }
11317 break;
11318
11319 default:
11320 bad_key = true;
11321 break;
11322 }
11323 if (bad_key)
11324 {
11325 error_at(name_expr->location(), "expected struct field name");
11326 return Expression::make_error(location);
11327 }
11328
11329 unsigned int index;
11330 const Struct_field* sf = st->find_local_field(name, &index);
11331 if (sf == NULL)
11332 {
11333 error_at(name_expr->location(), "unknown field %qs in %qs",
11334 Gogo::message_name(name).c_str(),
11335 (type->named_type() != NULL
11336 ? type->named_type()->message_name().c_str()
11337 : "unnamed struct"));
11338 return Expression::make_error(location);
11339 }
11340 if (vals[index] != NULL)
11341 {
11342 error_at(name_expr->location(),
11343 "duplicate value for field %qs in %qs",
11344 Gogo::message_name(name).c_str(),
11345 (type->named_type() != NULL
11346 ? type->named_type()->message_name().c_str()
11347 : "unnamed struct"));
11348 return Expression::make_error(location);
11349 }
11350
11351 vals[index] = val;
11352 }
11353
11354 Expression_list* list = new Expression_list;
11355 list->reserve(field_count);
11356 for (size_t i = 0; i < field_count; ++i)
11357 list->push_back(vals[i]);
11358
11359 return new Struct_construction_expression(type, list, location);
11360 }
11361
11362 // Lower an array composite literal.
11363
11364 Expression*
11365 Composite_literal_expression::lower_array(Type* type)
11366 {
11367 source_location location = this->location();
11368 if (this->vals_ == NULL || !this->has_keys_)
11369 return this->make_array(type, this->vals_);
11370
11371 std::vector<Expression*> vals;
11372 vals.reserve(this->vals_->size());
11373 unsigned long index = 0;
11374 Expression_list::const_iterator p = this->vals_->begin();
11375 while (p != this->vals_->end())
11376 {
11377 Expression* index_expr = *p;
11378
11379 ++p;
11380 gcc_assert(p != this->vals_->end());
11381 Expression* val = *p;
11382
11383 ++p;
11384
11385 if (index_expr != NULL)
11386 {
11387 mpz_t ival;
11388 mpz_init(ival);
11389 Type* dummy;
11390 if (!index_expr->integer_constant_value(true, ival, &dummy))
11391 {
11392 mpz_clear(ival);
11393 error_at(index_expr->location(),
11394 "index expression is not integer constant");
11395 return Expression::make_error(location);
11396 }
11397 if (mpz_sgn(ival) < 0)
11398 {
11399 mpz_clear(ival);
11400 error_at(index_expr->location(), "index expression is negative");
11401 return Expression::make_error(location);
11402 }
11403 index = mpz_get_ui(ival);
11404 if (mpz_cmp_ui(ival, index) != 0)
11405 {
11406 mpz_clear(ival);
11407 error_at(index_expr->location(), "index value overflow");
11408 return Expression::make_error(location);
11409 }
11410 mpz_clear(ival);
11411 }
11412
11413 if (index == vals.size())
11414 vals.push_back(val);
11415 else
11416 {
11417 if (index > vals.size())
11418 {
11419 vals.reserve(index + 32);
11420 vals.resize(index + 1, static_cast<Expression*>(NULL));
11421 }
11422 if (vals[index] != NULL)
11423 {
11424 error_at((index_expr != NULL
11425 ? index_expr->location()
11426 : val->location()),
11427 "duplicate value for index %lu",
11428 index);
11429 return Expression::make_error(location);
11430 }
11431 vals[index] = val;
11432 }
11433
11434 ++index;
11435 }
11436
11437 size_t size = vals.size();
11438 Expression_list* list = new Expression_list;
11439 list->reserve(size);
11440 for (size_t i = 0; i < size; ++i)
11441 list->push_back(vals[i]);
11442
11443 return this->make_array(type, list);
11444 }
11445
11446 // Actually build the array composite literal. This handles
11447 // [...]{...}.
11448
11449 Expression*
11450 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
11451 {
11452 source_location location = this->location();
11453 Array_type* at = type->array_type();
11454 if (at->length() != NULL && at->length()->is_nil_expression())
11455 {
11456 size_t size = vals == NULL ? 0 : vals->size();
11457 mpz_t vlen;
11458 mpz_init_set_ui(vlen, size);
11459 Expression* elen = Expression::make_integer(&vlen, NULL, location);
11460 mpz_clear(vlen);
11461 at = Type::make_array_type(at->element_type(), elen);
11462 type = at;
11463 }
11464 if (at->length() != NULL)
11465 return new Fixed_array_construction_expression(type, vals, location);
11466 else
11467 return new Open_array_construction_expression(type, vals, location);
11468 }
11469
11470 // Lower a map composite literal.
11471
11472 Expression*
11473 Composite_literal_expression::lower_map(Type* type)
11474 {
11475 source_location location = this->location();
11476 if (this->vals_ != NULL)
11477 {
11478 if (!this->has_keys_)
11479 {
11480 error_at(location, "map composite literal must have keys");
11481 return Expression::make_error(location);
11482 }
11483
11484 for (Expression_list::const_iterator p = this->vals_->begin();
11485 p != this->vals_->end();
11486 p += 2)
11487 {
11488 if (*p == NULL)
11489 {
11490 ++p;
11491 error_at((*p)->location(),
11492 "map composite literal must have keys for every value");
11493 return Expression::make_error(location);
11494 }
11495 }
11496 }
11497
11498 return new Map_construction_expression(type, this->vals_, location);
11499 }
11500
11501 // Make a composite literal expression.
11502
11503 Expression*
11504 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
11505 Expression_list* vals,
11506 source_location location)
11507 {
11508 return new Composite_literal_expression(type, depth, has_keys, vals,
11509 location);
11510 }
11511
11512 // Return whether this expression is a composite literal.
11513
11514 bool
11515 Expression::is_composite_literal() const
11516 {
11517 switch (this->classification_)
11518 {
11519 case EXPRESSION_COMPOSITE_LITERAL:
11520 case EXPRESSION_STRUCT_CONSTRUCTION:
11521 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
11522 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
11523 case EXPRESSION_MAP_CONSTRUCTION:
11524 return true;
11525 default:
11526 return false;
11527 }
11528 }
11529
11530 // Return whether this expression is a composite literal which is not
11531 // constant.
11532
11533 bool
11534 Expression::is_nonconstant_composite_literal() const
11535 {
11536 switch (this->classification_)
11537 {
11538 case EXPRESSION_STRUCT_CONSTRUCTION:
11539 {
11540 const Struct_construction_expression *psce =
11541 static_cast<const Struct_construction_expression*>(this);
11542 return !psce->is_constant_struct();
11543 }
11544 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
11545 {
11546 const Fixed_array_construction_expression *pace =
11547 static_cast<const Fixed_array_construction_expression*>(this);
11548 return !pace->is_constant_array();
11549 }
11550 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
11551 {
11552 const Open_array_construction_expression *pace =
11553 static_cast<const Open_array_construction_expression*>(this);
11554 return !pace->is_constant_array();
11555 }
11556 case EXPRESSION_MAP_CONSTRUCTION:
11557 return true;
11558 default:
11559 return false;
11560 }
11561 }
11562
11563 // Return true if this is a reference to a local variable.
11564
11565 bool
11566 Expression::is_local_variable() const
11567 {
11568 const Var_expression* ve = this->var_expression();
11569 if (ve == NULL)
11570 return false;
11571 const Named_object* no = ve->named_object();
11572 return (no->is_result_variable()
11573 || (no->is_variable() && !no->var_value()->is_global()));
11574 }
11575
11576 // Class Type_guard_expression.
11577
11578 // Traversal.
11579
11580 int
11581 Type_guard_expression::do_traverse(Traverse* traverse)
11582 {
11583 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
11584 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11585 return TRAVERSE_EXIT;
11586 return TRAVERSE_CONTINUE;
11587 }
11588
11589 // Check types of a type guard expression. The expression must have
11590 // an interface type, but the actual type conversion is checked at run
11591 // time.
11592
11593 void
11594 Type_guard_expression::do_check_types(Gogo*)
11595 {
11596 // 6g permits using a type guard with unsafe.pointer; we are
11597 // compatible.
11598 Type* expr_type = this->expr_->type();
11599 if (expr_type->is_unsafe_pointer_type())
11600 {
11601 if (this->type_->points_to() == NULL
11602 && (this->type_->integer_type() == NULL
11603 || (this->type_->forwarded()
11604 != Type::lookup_integer_type("uintptr"))))
11605 this->report_error(_("invalid unsafe.Pointer conversion"));
11606 }
11607 else if (this->type_->is_unsafe_pointer_type())
11608 {
11609 if (expr_type->points_to() == NULL
11610 && (expr_type->integer_type() == NULL
11611 || (expr_type->forwarded()
11612 != Type::lookup_integer_type("uintptr"))))
11613 this->report_error(_("invalid unsafe.Pointer conversion"));
11614 }
11615 else if (expr_type->interface_type() == NULL)
11616 this->report_error(_("type assertion only valid for interface types"));
11617 else if (this->type_->interface_type() == NULL)
11618 {
11619 std::string reason;
11620 if (!expr_type->interface_type()->implements_interface(this->type_,
11621 &reason))
11622 {
11623 if (reason.empty())
11624 this->report_error(_("impossible type assertion: "
11625 "type does not implement interface"));
11626 else
11627 {
11628 error_at(this->location(),
11629 ("impossible type assertion: "
11630 "type does not implement interface (%s)"),
11631 reason.c_str());
11632 this->set_is_error();
11633 }
11634 }
11635 }
11636 }
11637
11638 // Return a tree for a type guard expression.
11639
11640 tree
11641 Type_guard_expression::do_get_tree(Translate_context* context)
11642 {
11643 Gogo* gogo = context->gogo();
11644 tree expr_tree = this->expr_->get_tree(context);
11645 if (expr_tree == error_mark_node)
11646 return error_mark_node;
11647 Type* expr_type = this->expr_->type();
11648 if ((this->type_->is_unsafe_pointer_type()
11649 && (expr_type->points_to() != NULL
11650 || expr_type->integer_type() != NULL))
11651 || (expr_type->is_unsafe_pointer_type()
11652 && this->type_->points_to() != NULL))
11653 return convert_to_pointer(this->type_->get_tree(gogo), expr_tree);
11654 else if (expr_type->is_unsafe_pointer_type()
11655 && this->type_->integer_type() != NULL)
11656 return convert_to_integer(this->type_->get_tree(gogo), expr_tree);
11657 else if (this->type_->interface_type() != NULL)
11658 return Expression::convert_interface_to_interface(context, this->type_,
11659 this->expr_->type(),
11660 expr_tree, true,
11661 this->location());
11662 else
11663 return Expression::convert_for_assignment(context, this->type_,
11664 this->expr_->type(), expr_tree,
11665 this->location());
11666 }
11667
11668 // Make a type guard expression.
11669
11670 Expression*
11671 Expression::make_type_guard(Expression* expr, Type* type,
11672 source_location location)
11673 {
11674 return new Type_guard_expression(expr, type, location);
11675 }
11676
11677 // Class Heap_composite_expression.
11678
11679 // When you take the address of a composite literal, it is allocated
11680 // on the heap. This class implements that.
11681
11682 class Heap_composite_expression : public Expression
11683 {
11684 public:
11685 Heap_composite_expression(Expression* expr, source_location location)
11686 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
11687 expr_(expr)
11688 { }
11689
11690 protected:
11691 int
11692 do_traverse(Traverse* traverse)
11693 { return Expression::traverse(&this->expr_, traverse); }
11694
11695 Type*
11696 do_type()
11697 { return Type::make_pointer_type(this->expr_->type()); }
11698
11699 void
11700 do_determine_type(const Type_context*)
11701 { this->expr_->determine_type_no_context(); }
11702
11703 Expression*
11704 do_copy()
11705 {
11706 return Expression::make_heap_composite(this->expr_->copy(),
11707 this->location());
11708 }
11709
11710 tree
11711 do_get_tree(Translate_context*);
11712
11713 // We only export global objects, and the parser does not generate
11714 // this in global scope.
11715 void
11716 do_export(Export*) const
11717 { gcc_unreachable(); }
11718
11719 private:
11720 // The composite literal which is being put on the heap.
11721 Expression* expr_;
11722 };
11723
11724 // Return a tree which allocates a composite literal on the heap.
11725
11726 tree
11727 Heap_composite_expression::do_get_tree(Translate_context* context)
11728 {
11729 tree expr_tree = this->expr_->get_tree(context);
11730 if (expr_tree == error_mark_node)
11731 return error_mark_node;
11732 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
11733 gcc_assert(TREE_CODE(expr_size) == INTEGER_CST);
11734 tree space = context->gogo()->allocate_memory(this->expr_->type(),
11735 expr_size, this->location());
11736 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
11737 space = save_expr(space);
11738 tree ref = build_fold_indirect_ref_loc(this->location(), space);
11739 TREE_THIS_NOTRAP(ref) = 1;
11740 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
11741 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
11742 space);
11743 SET_EXPR_LOCATION(ret, this->location());
11744 return ret;
11745 }
11746
11747 // Allocate a composite literal on the heap.
11748
11749 Expression*
11750 Expression::make_heap_composite(Expression* expr, source_location location)
11751 {
11752 return new Heap_composite_expression(expr, location);
11753 }
11754
11755 // Class Receive_expression.
11756
11757 // Return the type of a receive expression.
11758
11759 Type*
11760 Receive_expression::do_type()
11761 {
11762 Channel_type* channel_type = this->channel_->type()->channel_type();
11763 if (channel_type == NULL)
11764 return Type::make_error_type();
11765 return channel_type->element_type();
11766 }
11767
11768 // Check types for a receive expression.
11769
11770 void
11771 Receive_expression::do_check_types(Gogo*)
11772 {
11773 Type* type = this->channel_->type();
11774 if (type->is_error_type())
11775 {
11776 this->set_is_error();
11777 return;
11778 }
11779 if (type->channel_type() == NULL)
11780 {
11781 this->report_error(_("expected channel"));
11782 return;
11783 }
11784 if (!type->channel_type()->may_receive())
11785 {
11786 this->report_error(_("invalid receive on send-only channel"));
11787 return;
11788 }
11789 }
11790
11791 // Get a tree for a receive expression.
11792
11793 tree
11794 Receive_expression::do_get_tree(Translate_context* context)
11795 {
11796 Channel_type* channel_type = this->channel_->type()->channel_type();
11797 gcc_assert(channel_type != NULL);
11798 Type* element_type = channel_type->element_type();
11799 tree element_type_tree = element_type->get_tree(context->gogo());
11800
11801 tree channel = this->channel_->get_tree(context);
11802 if (element_type_tree == error_mark_node || channel == error_mark_node)
11803 return error_mark_node;
11804
11805 return Gogo::receive_from_channel(element_type_tree, channel,
11806 this->for_select_, this->location());
11807 }
11808
11809 // Make a receive expression.
11810
11811 Receive_expression*
11812 Expression::make_receive(Expression* channel, source_location location)
11813 {
11814 return new Receive_expression(channel, location);
11815 }
11816
11817 // Class Send_expression.
11818
11819 // Traversal.
11820
11821 int
11822 Send_expression::do_traverse(Traverse* traverse)
11823 {
11824 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
11825 return TRAVERSE_EXIT;
11826 return Expression::traverse(&this->val_, traverse);
11827 }
11828
11829 // Get the type.
11830
11831 Type*
11832 Send_expression::do_type()
11833 {
11834 return Type::lookup_bool_type();
11835 }
11836
11837 // Set types.
11838
11839 void
11840 Send_expression::do_determine_type(const Type_context*)
11841 {
11842 this->channel_->determine_type_no_context();
11843
11844 Type* type = this->channel_->type();
11845 Type_context subcontext;
11846 if (type->channel_type() != NULL)
11847 subcontext.type = type->channel_type()->element_type();
11848 this->val_->determine_type(&subcontext);
11849 }
11850
11851 // Check types.
11852
11853 void
11854 Send_expression::do_check_types(Gogo*)
11855 {
11856 Type* type = this->channel_->type();
11857 if (type->is_error_type())
11858 {
11859 this->set_is_error();
11860 return;
11861 }
11862 Channel_type* channel_type = type->channel_type();
11863 if (channel_type == NULL)
11864 {
11865 error_at(this->location(), "left operand of %<<-%> must be channel");
11866 this->set_is_error();
11867 return;
11868 }
11869 Type* element_type = channel_type->element_type();
11870 if (element_type != NULL
11871 && !Type::are_assignable(element_type, this->val_->type(), NULL))
11872 {
11873 this->report_error(_("incompatible types in send"));
11874 return;
11875 }
11876 if (!channel_type->may_send())
11877 {
11878 this->report_error(_("invalid send on receive-only channel"));
11879 return;
11880 }
11881 }
11882
11883 // Get a tree for a send expression.
11884
11885 tree
11886 Send_expression::do_get_tree(Translate_context* context)
11887 {
11888 tree channel = this->channel_->get_tree(context);
11889 tree val = this->val_->get_tree(context);
11890 if (channel == error_mark_node || val == error_mark_node)
11891 return error_mark_node;
11892 Channel_type* channel_type = this->channel_->type()->channel_type();
11893 val = Expression::convert_for_assignment(context,
11894 channel_type->element_type(),
11895 this->val_->type(),
11896 val,
11897 this->location());
11898 return Gogo::send_on_channel(channel, val, this->is_value_discarded_,
11899 this->for_select_, this->location());
11900 }
11901
11902 // Make a send expression
11903
11904 Send_expression*
11905 Expression::make_send(Expression* channel, Expression* val,
11906 source_location location)
11907 {
11908 return new Send_expression(channel, val, location);
11909 }
11910
11911 // An expression which evaluates to a pointer to the type descriptor
11912 // of a type.
11913
11914 class Type_descriptor_expression : public Expression
11915 {
11916 public:
11917 Type_descriptor_expression(Type* type, source_location location)
11918 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
11919 type_(type)
11920 { }
11921
11922 protected:
11923 Type*
11924 do_type()
11925 { return Type::make_type_descriptor_ptr_type(); }
11926
11927 void
11928 do_determine_type(const Type_context*)
11929 { }
11930
11931 Expression*
11932 do_copy()
11933 { return this; }
11934
11935 tree
11936 do_get_tree(Translate_context* context)
11937 { return this->type_->type_descriptor_pointer(context->gogo()); }
11938
11939 private:
11940 // The type for which this is the descriptor.
11941 Type* type_;
11942 };
11943
11944 // Make a type descriptor expression.
11945
11946 Expression*
11947 Expression::make_type_descriptor(Type* type, source_location location)
11948 {
11949 return new Type_descriptor_expression(type, location);
11950 }
11951
11952 // An expression which evaluates to some characteristic of a type.
11953 // This is only used to initialize fields of a type descriptor. Using
11954 // a new expression class is slightly inefficient but gives us a good
11955 // separation between the frontend and the middle-end with regard to
11956 // how types are laid out.
11957
11958 class Type_info_expression : public Expression
11959 {
11960 public:
11961 Type_info_expression(Type* type, Type_info type_info)
11962 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
11963 type_(type), type_info_(type_info)
11964 { }
11965
11966 protected:
11967 Type*
11968 do_type();
11969
11970 void
11971 do_determine_type(const Type_context*)
11972 { }
11973
11974 Expression*
11975 do_copy()
11976 { return this; }
11977
11978 tree
11979 do_get_tree(Translate_context* context);
11980
11981 private:
11982 // The type for which we are getting information.
11983 Type* type_;
11984 // What information we want.
11985 Type_info type_info_;
11986 };
11987
11988 // The type is chosen to match what the type descriptor struct
11989 // expects.
11990
11991 Type*
11992 Type_info_expression::do_type()
11993 {
11994 switch (this->type_info_)
11995 {
11996 case TYPE_INFO_SIZE:
11997 return Type::lookup_integer_type("uintptr");
11998 case TYPE_INFO_ALIGNMENT:
11999 case TYPE_INFO_FIELD_ALIGNMENT:
12000 return Type::lookup_integer_type("uint8");
12001 default:
12002 gcc_unreachable();
12003 }
12004 }
12005
12006 // Return type information in GENERIC.
12007
12008 tree
12009 Type_info_expression::do_get_tree(Translate_context* context)
12010 {
12011 tree type_tree = this->type_->get_tree(context->gogo());
12012 if (type_tree == error_mark_node)
12013 return error_mark_node;
12014
12015 tree val_type_tree = this->type()->get_tree(context->gogo());
12016 gcc_assert(val_type_tree != error_mark_node);
12017
12018 if (this->type_info_ == TYPE_INFO_SIZE)
12019 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12020 TYPE_SIZE_UNIT(type_tree));
12021 else
12022 {
12023 unsigned int val;
12024 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12025 val = go_type_alignment(type_tree);
12026 else
12027 val = go_field_alignment(type_tree);
12028 return build_int_cstu(val_type_tree, val);
12029 }
12030 }
12031
12032 // Make a type info expression.
12033
12034 Expression*
12035 Expression::make_type_info(Type* type, Type_info type_info)
12036 {
12037 return new Type_info_expression(type, type_info);
12038 }
12039
12040 // An expression which evaluates to the offset of a field within a
12041 // struct. This, like Type_info_expression, q.v., is only used to
12042 // initialize fields of a type descriptor.
12043
12044 class Struct_field_offset_expression : public Expression
12045 {
12046 public:
12047 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12048 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12049 type_(type), field_(field)
12050 { }
12051
12052 protected:
12053 Type*
12054 do_type()
12055 { return Type::lookup_integer_type("uintptr"); }
12056
12057 void
12058 do_determine_type(const Type_context*)
12059 { }
12060
12061 Expression*
12062 do_copy()
12063 { return this; }
12064
12065 tree
12066 do_get_tree(Translate_context* context);
12067
12068 private:
12069 // The type of the struct.
12070 Struct_type* type_;
12071 // The field.
12072 const Struct_field* field_;
12073 };
12074
12075 // Return a struct field offset in GENERIC.
12076
12077 tree
12078 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12079 {
12080 tree type_tree = this->type_->get_tree(context->gogo());
12081 if (type_tree == error_mark_node)
12082 return error_mark_node;
12083
12084 tree val_type_tree = this->type()->get_tree(context->gogo());
12085 gcc_assert(val_type_tree != error_mark_node);
12086
12087 const Struct_field_list* fields = this->type_->fields();
12088 tree struct_field_tree = TYPE_FIELDS(type_tree);
12089 Struct_field_list::const_iterator p;
12090 for (p = fields->begin();
12091 p != fields->end();
12092 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12093 {
12094 gcc_assert(struct_field_tree != NULL_TREE);
12095 if (&*p == this->field_)
12096 break;
12097 }
12098 gcc_assert(&*p == this->field_);
12099
12100 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12101 byte_position(struct_field_tree));
12102 }
12103
12104 // Make an expression for a struct field offset.
12105
12106 Expression*
12107 Expression::make_struct_field_offset(Struct_type* type,
12108 const Struct_field* field)
12109 {
12110 return new Struct_field_offset_expression(type, field);
12111 }
12112
12113 // An expression which evaluates to the address of an unnamed label.
12114
12115 class Label_addr_expression : public Expression
12116 {
12117 public:
12118 Label_addr_expression(Label* label, source_location location)
12119 : Expression(EXPRESSION_LABEL_ADDR, location),
12120 label_(label)
12121 { }
12122
12123 protected:
12124 Type*
12125 do_type()
12126 { return Type::make_pointer_type(Type::make_void_type()); }
12127
12128 void
12129 do_determine_type(const Type_context*)
12130 { }
12131
12132 Expression*
12133 do_copy()
12134 { return new Label_addr_expression(this->label_, this->location()); }
12135
12136 tree
12137 do_get_tree(Translate_context*)
12138 { return this->label_->get_addr(this->location()); }
12139
12140 private:
12141 // The label whose address we are taking.
12142 Label* label_;
12143 };
12144
12145 // Make an expression for the address of an unnamed label.
12146
12147 Expression*
12148 Expression::make_label_addr(Label* label, source_location location)
12149 {
12150 return new Label_addr_expression(label, location);
12151 }
12152
12153 // Import an expression. This comes at the end in order to see the
12154 // various class definitions.
12155
12156 Expression*
12157 Expression::import_expression(Import* imp)
12158 {
12159 int c = imp->peek_char();
12160 if (imp->match_c_string("- ")
12161 || imp->match_c_string("! ")
12162 || imp->match_c_string("^ "))
12163 return Unary_expression::do_import(imp);
12164 else if (c == '(')
12165 return Binary_expression::do_import(imp);
12166 else if (imp->match_c_string("true")
12167 || imp->match_c_string("false"))
12168 return Boolean_expression::do_import(imp);
12169 else if (c == '"')
12170 return String_expression::do_import(imp);
12171 else if (c == '-' || (c >= '0' && c <= '9'))
12172 {
12173 // This handles integers, floats and complex constants.
12174 return Integer_expression::do_import(imp);
12175 }
12176 else if (imp->match_c_string("nil"))
12177 return Nil_expression::do_import(imp);
12178 else if (imp->match_c_string("convert"))
12179 return Type_conversion_expression::do_import(imp);
12180 else
12181 {
12182 error_at(imp->location(), "import error: expected expression");
12183 return Expression::make_error(imp->location());
12184 }
12185 }
12186
12187 // Class Expression_list.
12188
12189 // Traverse the list.
12190
12191 int
12192 Expression_list::traverse(Traverse* traverse)
12193 {
12194 for (Expression_list::iterator p = this->begin();
12195 p != this->end();
12196 ++p)
12197 {
12198 if (*p != NULL)
12199 {
12200 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12201 return TRAVERSE_EXIT;
12202 }
12203 }
12204 return TRAVERSE_CONTINUE;
12205 }
12206
12207 // Copy the list.
12208
12209 Expression_list*
12210 Expression_list::copy()
12211 {
12212 Expression_list* ret = new Expression_list();
12213 for (Expression_list::iterator p = this->begin();
12214 p != this->end();
12215 ++p)
12216 {
12217 if (*p == NULL)
12218 ret->push_back(NULL);
12219 else
12220 ret->push_back((*p)->copy());
12221 }
12222 return ret;
12223 }
12224
12225 // Return whether an expression list has an error expression.
12226
12227 bool
12228 Expression_list::contains_error() const
12229 {
12230 for (Expression_list::const_iterator p = this->begin();
12231 p != this->end();
12232 ++p)
12233 if (*p != NULL && (*p)->is_error_expression())
12234 return true;
12235 return false;
12236 }