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