Check for error type when getting type of binary expression.
[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_error_type())
5400 return left_type;
5401 else if (right_type->is_error_type())
5402 return right_type;
5403 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
5404 return left_type;
5405 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5406 return right_type;
5407 else if (!left_type->is_abstract())
5408 return left_type;
5409 else if (!right_type->is_abstract())
5410 return right_type;
5411 else if (left_type->complex_type() != NULL)
5412 return left_type;
5413 else if (right_type->complex_type() != NULL)
5414 return right_type;
5415 else if (left_type->float_type() != NULL)
5416 return left_type;
5417 else if (right_type->float_type() != NULL)
5418 return right_type;
5419 else
5420 return left_type;
5421 }
5422
5423 case OPERATOR_LSHIFT:
5424 case OPERATOR_RSHIFT:
5425 return this->left_->type();
5426
5427 default:
5428 gcc_unreachable();
5429 }
5430 }
5431
5432 // Set type for a binary expression.
5433
5434 void
5435 Binary_expression::do_determine_type(const Type_context* context)
5436 {
5437 Type* tleft = this->left_->type();
5438 Type* tright = this->right_->type();
5439
5440 // Both sides should have the same type, except for the shift
5441 // operations. For a comparison, we should ignore the incoming
5442 // type.
5443
5444 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5445 || this->op_ == OPERATOR_RSHIFT);
5446
5447 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5448 || this->op_ == OPERATOR_NOTEQ
5449 || this->op_ == OPERATOR_LT
5450 || this->op_ == OPERATOR_LE
5451 || this->op_ == OPERATOR_GT
5452 || this->op_ == OPERATOR_GE);
5453
5454 Type_context subcontext(*context);
5455
5456 if (is_comparison)
5457 {
5458 // In a comparison, the context does not determine the types of
5459 // the operands.
5460 subcontext.type = NULL;
5461 }
5462
5463 // Set the context for the left hand operand.
5464 if (is_shift_op)
5465 {
5466 // The right hand operand plays no role in determining the type
5467 // of the left hand operand. A shift of an abstract integer in
5468 // a string context gets special treatment, which may be a
5469 // language bug.
5470 if (subcontext.type != NULL
5471 && subcontext.type->is_string_type()
5472 && tleft->is_abstract())
5473 error_at(this->location(), "shift of non-integer operand");
5474 }
5475 else if (!tleft->is_abstract())
5476 subcontext.type = tleft;
5477 else if (!tright->is_abstract())
5478 subcontext.type = tright;
5479 else if (subcontext.type == NULL)
5480 {
5481 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5482 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5483 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5484 {
5485 // Both sides have an abstract integer, abstract float, or
5486 // abstract complex type. Just let CONTEXT determine
5487 // whether they may remain abstract or not.
5488 }
5489 else if (tleft->complex_type() != NULL)
5490 subcontext.type = tleft;
5491 else if (tright->complex_type() != NULL)
5492 subcontext.type = tright;
5493 else if (tleft->float_type() != NULL)
5494 subcontext.type = tleft;
5495 else if (tright->float_type() != NULL)
5496 subcontext.type = tright;
5497 else
5498 subcontext.type = tleft;
5499 }
5500
5501 this->left_->determine_type(&subcontext);
5502
5503 // The context for the right hand operand is the same as for the
5504 // left hand operand, except for a shift operator.
5505 if (is_shift_op)
5506 {
5507 subcontext.type = Type::lookup_integer_type("uint");
5508 subcontext.may_be_abstract = false;
5509 }
5510
5511 this->right_->determine_type(&subcontext);
5512 }
5513
5514 // Report an error if the binary operator OP does not support TYPE.
5515 // Return whether the operation is OK. This should not be used for
5516 // shift.
5517
5518 bool
5519 Binary_expression::check_operator_type(Operator op, Type* type,
5520 source_location location)
5521 {
5522 switch (op)
5523 {
5524 case OPERATOR_OROR:
5525 case OPERATOR_ANDAND:
5526 if (!type->is_boolean_type())
5527 {
5528 error_at(location, "expected boolean type");
5529 return false;
5530 }
5531 break;
5532
5533 case OPERATOR_EQEQ:
5534 case OPERATOR_NOTEQ:
5535 if (type->integer_type() == NULL
5536 && type->float_type() == NULL
5537 && type->complex_type() == NULL
5538 && !type->is_string_type()
5539 && type->points_to() == NULL
5540 && !type->is_nil_type()
5541 && !type->is_boolean_type()
5542 && type->interface_type() == NULL
5543 && (type->array_type() == NULL
5544 || type->array_type()->length() != NULL)
5545 && type->map_type() == NULL
5546 && type->channel_type() == NULL
5547 && type->function_type() == NULL)
5548 {
5549 error_at(location,
5550 ("expected integer, floating, complex, string, pointer, "
5551 "boolean, interface, slice, map, channel, "
5552 "or function type"));
5553 return false;
5554 }
5555 break;
5556
5557 case OPERATOR_LT:
5558 case OPERATOR_LE:
5559 case OPERATOR_GT:
5560 case OPERATOR_GE:
5561 if (type->integer_type() == NULL
5562 && type->float_type() == NULL
5563 && !type->is_string_type())
5564 {
5565 error_at(location, "expected integer, floating, or string type");
5566 return false;
5567 }
5568 break;
5569
5570 case OPERATOR_PLUS:
5571 case OPERATOR_PLUSEQ:
5572 if (type->integer_type() == NULL
5573 && type->float_type() == NULL
5574 && type->complex_type() == NULL
5575 && !type->is_string_type())
5576 {
5577 error_at(location,
5578 "expected integer, floating, complex, or string type");
5579 return false;
5580 }
5581 break;
5582
5583 case OPERATOR_MINUS:
5584 case OPERATOR_MINUSEQ:
5585 case OPERATOR_MULT:
5586 case OPERATOR_MULTEQ:
5587 case OPERATOR_DIV:
5588 case OPERATOR_DIVEQ:
5589 if (type->integer_type() == NULL
5590 && type->float_type() == NULL
5591 && type->complex_type() == NULL)
5592 {
5593 error_at(location, "expected integer, floating, or complex type");
5594 return false;
5595 }
5596 break;
5597
5598 case OPERATOR_MOD:
5599 case OPERATOR_MODEQ:
5600 case OPERATOR_OR:
5601 case OPERATOR_OREQ:
5602 case OPERATOR_AND:
5603 case OPERATOR_ANDEQ:
5604 case OPERATOR_XOR:
5605 case OPERATOR_XOREQ:
5606 case OPERATOR_BITCLEAR:
5607 case OPERATOR_BITCLEAREQ:
5608 if (type->integer_type() == NULL)
5609 {
5610 error_at(location, "expected integer type");
5611 return false;
5612 }
5613 break;
5614
5615 default:
5616 gcc_unreachable();
5617 }
5618
5619 return true;
5620 }
5621
5622 // Check types.
5623
5624 void
5625 Binary_expression::do_check_types(Gogo*)
5626 {
5627 Type* left_type = this->left_->type();
5628 Type* right_type = this->right_->type();
5629 if (left_type->is_error_type() || right_type->is_error_type())
5630 {
5631 this->set_is_error();
5632 return;
5633 }
5634
5635 if (this->op_ == OPERATOR_EQEQ
5636 || this->op_ == OPERATOR_NOTEQ
5637 || this->op_ == OPERATOR_LT
5638 || this->op_ == OPERATOR_LE
5639 || this->op_ == OPERATOR_GT
5640 || this->op_ == OPERATOR_GE)
5641 {
5642 if (!Type::are_assignable(left_type, right_type, NULL)
5643 && !Type::are_assignable(right_type, left_type, NULL))
5644 {
5645 this->report_error(_("incompatible types in binary expression"));
5646 return;
5647 }
5648 if (!Binary_expression::check_operator_type(this->op_, left_type,
5649 this->location())
5650 || !Binary_expression::check_operator_type(this->op_, right_type,
5651 this->location()))
5652 {
5653 this->set_is_error();
5654 return;
5655 }
5656 }
5657 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5658 {
5659 if (!Type::are_compatible_for_binop(left_type, right_type))
5660 {
5661 this->report_error(_("incompatible types in binary expression"));
5662 return;
5663 }
5664 if (!Binary_expression::check_operator_type(this->op_, left_type,
5665 this->location()))
5666 {
5667 this->set_is_error();
5668 return;
5669 }
5670 }
5671 else
5672 {
5673 if (left_type->integer_type() == NULL)
5674 this->report_error(_("shift of non-integer operand"));
5675
5676 if (!right_type->is_abstract()
5677 && (right_type->integer_type() == NULL
5678 || !right_type->integer_type()->is_unsigned()))
5679 this->report_error(_("shift count not unsigned integer"));
5680 else
5681 {
5682 mpz_t val;
5683 mpz_init(val);
5684 Type* type;
5685 if (this->right_->integer_constant_value(true, val, &type))
5686 {
5687 if (mpz_sgn(val) < 0)
5688 this->report_error(_("negative shift count"));
5689 }
5690 mpz_clear(val);
5691 }
5692 }
5693 }
5694
5695 // Get a tree for a binary expression.
5696
5697 tree
5698 Binary_expression::do_get_tree(Translate_context* context)
5699 {
5700 tree left = this->left_->get_tree(context);
5701 tree right = this->right_->get_tree(context);
5702
5703 if (left == error_mark_node || right == error_mark_node)
5704 return error_mark_node;
5705
5706 enum tree_code code;
5707 bool use_left_type = true;
5708 bool is_shift_op = false;
5709 switch (this->op_)
5710 {
5711 case OPERATOR_EQEQ:
5712 case OPERATOR_NOTEQ:
5713 case OPERATOR_LT:
5714 case OPERATOR_LE:
5715 case OPERATOR_GT:
5716 case OPERATOR_GE:
5717 return Expression::comparison_tree(context, this->op_,
5718 this->left_->type(), left,
5719 this->right_->type(), right,
5720 this->location());
5721
5722 case OPERATOR_OROR:
5723 code = TRUTH_ORIF_EXPR;
5724 use_left_type = false;
5725 break;
5726 case OPERATOR_ANDAND:
5727 code = TRUTH_ANDIF_EXPR;
5728 use_left_type = false;
5729 break;
5730 case OPERATOR_PLUS:
5731 code = PLUS_EXPR;
5732 break;
5733 case OPERATOR_MINUS:
5734 code = MINUS_EXPR;
5735 break;
5736 case OPERATOR_OR:
5737 code = BIT_IOR_EXPR;
5738 break;
5739 case OPERATOR_XOR:
5740 code = BIT_XOR_EXPR;
5741 break;
5742 case OPERATOR_MULT:
5743 code = MULT_EXPR;
5744 break;
5745 case OPERATOR_DIV:
5746 {
5747 Type *t = this->left_->type();
5748 if (t->float_type() != NULL || t->complex_type() != NULL)
5749 code = RDIV_EXPR;
5750 else
5751 code = TRUNC_DIV_EXPR;
5752 }
5753 break;
5754 case OPERATOR_MOD:
5755 code = TRUNC_MOD_EXPR;
5756 break;
5757 case OPERATOR_LSHIFT:
5758 code = LSHIFT_EXPR;
5759 is_shift_op = true;
5760 break;
5761 case OPERATOR_RSHIFT:
5762 code = RSHIFT_EXPR;
5763 is_shift_op = true;
5764 break;
5765 case OPERATOR_AND:
5766 code = BIT_AND_EXPR;
5767 break;
5768 case OPERATOR_BITCLEAR:
5769 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5770 code = BIT_AND_EXPR;
5771 break;
5772 default:
5773 gcc_unreachable();
5774 }
5775
5776 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5777
5778 if (this->left_->type()->is_string_type())
5779 {
5780 gcc_assert(this->op_ == OPERATOR_PLUS);
5781 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5782 static tree string_plus_decl;
5783 return Gogo::call_builtin(&string_plus_decl,
5784 this->location(),
5785 "__go_string_plus",
5786 2,
5787 string_type,
5788 string_type,
5789 left,
5790 string_type,
5791 right);
5792 }
5793
5794 tree compute_type = excess_precision_type(type);
5795 if (compute_type != NULL_TREE)
5796 {
5797 left = ::convert(compute_type, left);
5798 right = ::convert(compute_type, right);
5799 }
5800
5801 tree eval_saved = NULL_TREE;
5802 if (is_shift_op)
5803 {
5804 if (!DECL_P(left))
5805 left = save_expr(left);
5806 if (!DECL_P(right))
5807 right = save_expr(right);
5808 // Make sure the values are evaluated.
5809 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
5810 void_type_node, left, right);
5811 }
5812
5813 tree ret = fold_build2_loc(this->location(),
5814 code,
5815 compute_type != NULL_TREE ? compute_type : type,
5816 left, right);
5817
5818 if (compute_type != NULL_TREE)
5819 ret = ::convert(type, ret);
5820
5821 // In Go, a shift larger than the size of the type is well-defined.
5822 // This is not true in GENERIC, so we need to insert a conditional.
5823 if (is_shift_op)
5824 {
5825 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
5826 gcc_assert(this->left_->type()->integer_type() != NULL);
5827 int bits = TYPE_PRECISION(TREE_TYPE(left));
5828
5829 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
5830 build_int_cst_type(TREE_TYPE(right), bits));
5831
5832 tree overflow_result = fold_convert_loc(this->location(),
5833 TREE_TYPE(left),
5834 integer_zero_node);
5835 if (this->op_ == OPERATOR_RSHIFT
5836 && !this->left_->type()->integer_type()->is_unsigned())
5837 {
5838 tree neg = fold_build2_loc(this->location(), LT_EXPR,
5839 boolean_type_node, left,
5840 fold_convert_loc(this->location(),
5841 TREE_TYPE(left),
5842 integer_zero_node));
5843 tree neg_one = fold_build2_loc(this->location(),
5844 MINUS_EXPR, TREE_TYPE(left),
5845 fold_convert_loc(this->location(),
5846 TREE_TYPE(left),
5847 integer_zero_node),
5848 fold_convert_loc(this->location(),
5849 TREE_TYPE(left),
5850 integer_one_node));
5851 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
5852 TREE_TYPE(left), neg, neg_one,
5853 overflow_result);
5854 }
5855
5856 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
5857 compare, ret, overflow_result);
5858
5859 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
5860 TREE_TYPE(ret), eval_saved, ret);
5861 }
5862
5863 return ret;
5864 }
5865
5866 // Export a binary expression.
5867
5868 void
5869 Binary_expression::do_export(Export* exp) const
5870 {
5871 exp->write_c_string("(");
5872 this->left_->export_expression(exp);
5873 switch (this->op_)
5874 {
5875 case OPERATOR_OROR:
5876 exp->write_c_string(" || ");
5877 break;
5878 case OPERATOR_ANDAND:
5879 exp->write_c_string(" && ");
5880 break;
5881 case OPERATOR_EQEQ:
5882 exp->write_c_string(" == ");
5883 break;
5884 case OPERATOR_NOTEQ:
5885 exp->write_c_string(" != ");
5886 break;
5887 case OPERATOR_LT:
5888 exp->write_c_string(" < ");
5889 break;
5890 case OPERATOR_LE:
5891 exp->write_c_string(" <= ");
5892 break;
5893 case OPERATOR_GT:
5894 exp->write_c_string(" > ");
5895 break;
5896 case OPERATOR_GE:
5897 exp->write_c_string(" >= ");
5898 break;
5899 case OPERATOR_PLUS:
5900 exp->write_c_string(" + ");
5901 break;
5902 case OPERATOR_MINUS:
5903 exp->write_c_string(" - ");
5904 break;
5905 case OPERATOR_OR:
5906 exp->write_c_string(" | ");
5907 break;
5908 case OPERATOR_XOR:
5909 exp->write_c_string(" ^ ");
5910 break;
5911 case OPERATOR_MULT:
5912 exp->write_c_string(" * ");
5913 break;
5914 case OPERATOR_DIV:
5915 exp->write_c_string(" / ");
5916 break;
5917 case OPERATOR_MOD:
5918 exp->write_c_string(" % ");
5919 break;
5920 case OPERATOR_LSHIFT:
5921 exp->write_c_string(" << ");
5922 break;
5923 case OPERATOR_RSHIFT:
5924 exp->write_c_string(" >> ");
5925 break;
5926 case OPERATOR_AND:
5927 exp->write_c_string(" & ");
5928 break;
5929 case OPERATOR_BITCLEAR:
5930 exp->write_c_string(" &^ ");
5931 break;
5932 default:
5933 gcc_unreachable();
5934 }
5935 this->right_->export_expression(exp);
5936 exp->write_c_string(")");
5937 }
5938
5939 // Import a binary expression.
5940
5941 Expression*
5942 Binary_expression::do_import(Import* imp)
5943 {
5944 imp->require_c_string("(");
5945
5946 Expression* left = Expression::import_expression(imp);
5947
5948 Operator op;
5949 if (imp->match_c_string(" || "))
5950 {
5951 op = OPERATOR_OROR;
5952 imp->advance(4);
5953 }
5954 else if (imp->match_c_string(" && "))
5955 {
5956 op = OPERATOR_ANDAND;
5957 imp->advance(4);
5958 }
5959 else if (imp->match_c_string(" == "))
5960 {
5961 op = OPERATOR_EQEQ;
5962 imp->advance(4);
5963 }
5964 else if (imp->match_c_string(" != "))
5965 {
5966 op = OPERATOR_NOTEQ;
5967 imp->advance(4);
5968 }
5969 else if (imp->match_c_string(" < "))
5970 {
5971 op = OPERATOR_LT;
5972 imp->advance(3);
5973 }
5974 else if (imp->match_c_string(" <= "))
5975 {
5976 op = OPERATOR_LE;
5977 imp->advance(4);
5978 }
5979 else if (imp->match_c_string(" > "))
5980 {
5981 op = OPERATOR_GT;
5982 imp->advance(3);
5983 }
5984 else if (imp->match_c_string(" >= "))
5985 {
5986 op = OPERATOR_GE;
5987 imp->advance(4);
5988 }
5989 else if (imp->match_c_string(" + "))
5990 {
5991 op = OPERATOR_PLUS;
5992 imp->advance(3);
5993 }
5994 else if (imp->match_c_string(" - "))
5995 {
5996 op = OPERATOR_MINUS;
5997 imp->advance(3);
5998 }
5999 else if (imp->match_c_string(" | "))
6000 {
6001 op = OPERATOR_OR;
6002 imp->advance(3);
6003 }
6004 else if (imp->match_c_string(" ^ "))
6005 {
6006 op = OPERATOR_XOR;
6007 imp->advance(3);
6008 }
6009 else if (imp->match_c_string(" * "))
6010 {
6011 op = OPERATOR_MULT;
6012 imp->advance(3);
6013 }
6014 else if (imp->match_c_string(" / "))
6015 {
6016 op = OPERATOR_DIV;
6017 imp->advance(3);
6018 }
6019 else if (imp->match_c_string(" % "))
6020 {
6021 op = OPERATOR_MOD;
6022 imp->advance(3);
6023 }
6024 else if (imp->match_c_string(" << "))
6025 {
6026 op = OPERATOR_LSHIFT;
6027 imp->advance(4);
6028 }
6029 else if (imp->match_c_string(" >> "))
6030 {
6031 op = OPERATOR_RSHIFT;
6032 imp->advance(4);
6033 }
6034 else if (imp->match_c_string(" & "))
6035 {
6036 op = OPERATOR_AND;
6037 imp->advance(3);
6038 }
6039 else if (imp->match_c_string(" &^ "))
6040 {
6041 op = OPERATOR_BITCLEAR;
6042 imp->advance(4);
6043 }
6044 else
6045 {
6046 error_at(imp->location(), "unrecognized binary operator");
6047 return Expression::make_error(imp->location());
6048 }
6049
6050 Expression* right = Expression::import_expression(imp);
6051
6052 imp->require_c_string(")");
6053
6054 return Expression::make_binary(op, left, right, imp->location());
6055 }
6056
6057 // Make a binary expression.
6058
6059 Expression*
6060 Expression::make_binary(Operator op, Expression* left, Expression* right,
6061 source_location location)
6062 {
6063 return new Binary_expression(op, left, right, location);
6064 }
6065
6066 // Implement a comparison.
6067
6068 tree
6069 Expression::comparison_tree(Translate_context* context, Operator op,
6070 Type* left_type, tree left_tree,
6071 Type* right_type, tree right_tree,
6072 source_location location)
6073 {
6074 enum tree_code code;
6075 switch (op)
6076 {
6077 case OPERATOR_EQEQ:
6078 code = EQ_EXPR;
6079 break;
6080 case OPERATOR_NOTEQ:
6081 code = NE_EXPR;
6082 break;
6083 case OPERATOR_LT:
6084 code = LT_EXPR;
6085 break;
6086 case OPERATOR_LE:
6087 code = LE_EXPR;
6088 break;
6089 case OPERATOR_GT:
6090 code = GT_EXPR;
6091 break;
6092 case OPERATOR_GE:
6093 code = GE_EXPR;
6094 break;
6095 default:
6096 gcc_unreachable();
6097 }
6098
6099 if (left_type->is_string_type() && right_type->is_string_type())
6100 {
6101 tree string_type = Type::make_string_type()->get_tree(context->gogo());
6102 static tree string_compare_decl;
6103 left_tree = Gogo::call_builtin(&string_compare_decl,
6104 location,
6105 "__go_strcmp",
6106 2,
6107 integer_type_node,
6108 string_type,
6109 left_tree,
6110 string_type,
6111 right_tree);
6112 right_tree = build_int_cst_type(integer_type_node, 0);
6113 }
6114 else if ((left_type->interface_type() != NULL
6115 && right_type->interface_type() == NULL
6116 && !right_type->is_nil_type())
6117 || (left_type->interface_type() == NULL
6118 && !left_type->is_nil_type()
6119 && right_type->interface_type() != NULL))
6120 {
6121 // Comparing an interface value to a non-interface value.
6122 if (left_type->interface_type() == NULL)
6123 {
6124 std::swap(left_type, right_type);
6125 std::swap(left_tree, right_tree);
6126 }
6127
6128 // The right operand is not an interface. We need to take its
6129 // address if it is not a pointer.
6130 tree make_tmp;
6131 tree arg;
6132 if (right_type->points_to() != NULL)
6133 {
6134 make_tmp = NULL_TREE;
6135 arg = right_tree;
6136 }
6137 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6138 {
6139 make_tmp = NULL_TREE;
6140 arg = build_fold_addr_expr_loc(location, right_tree);
6141 if (DECL_P(right_tree))
6142 TREE_ADDRESSABLE(right_tree) = 1;
6143 }
6144 else
6145 {
6146 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6147 get_name(right_tree));
6148 DECL_IGNORED_P(tmp) = 0;
6149 DECL_INITIAL(tmp) = right_tree;
6150 TREE_ADDRESSABLE(tmp) = 1;
6151 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6152 SET_EXPR_LOCATION(make_tmp, location);
6153 arg = build_fold_addr_expr_loc(location, tmp);
6154 }
6155 arg = fold_convert_loc(location, ptr_type_node, arg);
6156
6157 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
6158
6159 if (left_type->interface_type()->is_empty())
6160 {
6161 static tree empty_interface_value_compare_decl;
6162 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6163 location,
6164 "__go_empty_interface_value_compare",
6165 3,
6166 integer_type_node,
6167 TREE_TYPE(left_tree),
6168 left_tree,
6169 TREE_TYPE(descriptor),
6170 descriptor,
6171 ptr_type_node,
6172 arg);
6173 if (left_tree == error_mark_node)
6174 return error_mark_node;
6175 // This can panic if the type is not comparable.
6176 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6177 }
6178 else
6179 {
6180 static tree interface_value_compare_decl;
6181 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6182 location,
6183 "__go_interface_value_compare",
6184 3,
6185 integer_type_node,
6186 TREE_TYPE(left_tree),
6187 left_tree,
6188 TREE_TYPE(descriptor),
6189 descriptor,
6190 ptr_type_node,
6191 arg);
6192 if (left_tree == error_mark_node)
6193 return error_mark_node;
6194 // This can panic if the type is not comparable.
6195 TREE_NOTHROW(interface_value_compare_decl) = 0;
6196 }
6197 right_tree = build_int_cst_type(integer_type_node, 0);
6198
6199 if (make_tmp != NULL_TREE)
6200 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6201 left_tree);
6202 }
6203 else if (left_type->interface_type() != NULL
6204 && right_type->interface_type() != NULL)
6205 {
6206 if (left_type->interface_type()->is_empty())
6207 {
6208 gcc_assert(right_type->interface_type()->is_empty());
6209 static tree empty_interface_compare_decl;
6210 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6211 location,
6212 "__go_empty_interface_compare",
6213 2,
6214 integer_type_node,
6215 TREE_TYPE(left_tree),
6216 left_tree,
6217 TREE_TYPE(right_tree),
6218 right_tree);
6219 if (left_tree == error_mark_node)
6220 return error_mark_node;
6221 // This can panic if the type is uncomparable.
6222 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6223 }
6224 else
6225 {
6226 gcc_assert(!right_type->interface_type()->is_empty());
6227 static tree interface_compare_decl;
6228 left_tree = Gogo::call_builtin(&interface_compare_decl,
6229 location,
6230 "__go_interface_compare",
6231 2,
6232 integer_type_node,
6233 TREE_TYPE(left_tree),
6234 left_tree,
6235 TREE_TYPE(right_tree),
6236 right_tree);
6237 if (left_tree == error_mark_node)
6238 return error_mark_node;
6239 // This can panic if the type is uncomparable.
6240 TREE_NOTHROW(interface_compare_decl) = 0;
6241 }
6242 right_tree = build_int_cst_type(integer_type_node, 0);
6243 }
6244
6245 if (left_type->is_nil_type()
6246 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6247 {
6248 std::swap(left_type, right_type);
6249 std::swap(left_tree, right_tree);
6250 }
6251
6252 if (right_type->is_nil_type())
6253 {
6254 if (left_type->array_type() != NULL
6255 && left_type->array_type()->length() == NULL)
6256 {
6257 Array_type* at = left_type->array_type();
6258 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6259 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6260 }
6261 else if (left_type->interface_type() != NULL)
6262 {
6263 // An interface is nil if the first field is nil.
6264 tree left_type_tree = TREE_TYPE(left_tree);
6265 gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6266 tree field = TYPE_FIELDS(left_type_tree);
6267 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6268 field, NULL_TREE);
6269 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6270 }
6271 else
6272 {
6273 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6274 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6275 }
6276 }
6277
6278 if (left_tree == error_mark_node || right_tree == error_mark_node)
6279 return error_mark_node;
6280
6281 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6282 if (CAN_HAVE_LOCATION_P(ret))
6283 SET_EXPR_LOCATION(ret, location);
6284 return ret;
6285 }
6286
6287 // Class Bound_method_expression.
6288
6289 // Traversal.
6290
6291 int
6292 Bound_method_expression::do_traverse(Traverse* traverse)
6293 {
6294 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6295 return TRAVERSE_EXIT;
6296 return Expression::traverse(&this->method_, traverse);
6297 }
6298
6299 // Return the type of a bound method expression. The type of this
6300 // object is really the type of the method with no receiver. We
6301 // should be able to get away with just returning the type of the
6302 // method.
6303
6304 Type*
6305 Bound_method_expression::do_type()
6306 {
6307 return this->method_->type();
6308 }
6309
6310 // Determine the types of a method expression.
6311
6312 void
6313 Bound_method_expression::do_determine_type(const Type_context*)
6314 {
6315 this->method_->determine_type_no_context();
6316 Type* mtype = this->method_->type();
6317 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6318 if (fntype == NULL || !fntype->is_method())
6319 this->expr_->determine_type_no_context();
6320 else
6321 {
6322 Type_context subcontext(fntype->receiver()->type(), false);
6323 this->expr_->determine_type(&subcontext);
6324 }
6325 }
6326
6327 // Check the types of a method expression.
6328
6329 void
6330 Bound_method_expression::do_check_types(Gogo*)
6331 {
6332 Type* type = this->method_->type()->deref();
6333 if (type == NULL
6334 || type->function_type() == NULL
6335 || !type->function_type()->is_method())
6336 this->report_error(_("object is not a method"));
6337 else
6338 {
6339 Type* rtype = type->function_type()->receiver()->type()->deref();
6340 Type* etype = (this->expr_type_ != NULL
6341 ? this->expr_type_
6342 : this->expr_->type());
6343 etype = etype->deref();
6344 if (!Type::are_identical(rtype, etype, true, NULL))
6345 this->report_error(_("method type does not match object type"));
6346 }
6347 }
6348
6349 // Get the tree for a method expression. There is no standard tree
6350 // representation for this. The only places it may currently be used
6351 // are in a Call_expression or a Go_statement, which will take it
6352 // apart directly. So this has nothing to do at present.
6353
6354 tree
6355 Bound_method_expression::do_get_tree(Translate_context*)
6356 {
6357 gcc_unreachable();
6358 }
6359
6360 // Make a method expression.
6361
6362 Bound_method_expression*
6363 Expression::make_bound_method(Expression* expr, Expression* method,
6364 source_location location)
6365 {
6366 return new Bound_method_expression(expr, method, location);
6367 }
6368
6369 // Class Builtin_call_expression. This is used for a call to a
6370 // builtin function.
6371
6372 class Builtin_call_expression : public Call_expression
6373 {
6374 public:
6375 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6376 bool is_varargs, source_location location);
6377
6378 protected:
6379 // This overrides Call_expression::do_lower.
6380 Expression*
6381 do_lower(Gogo*, Named_object*, int);
6382
6383 bool
6384 do_is_constant() const;
6385
6386 bool
6387 do_integer_constant_value(bool, mpz_t, Type**) const;
6388
6389 bool
6390 do_float_constant_value(mpfr_t, Type**) const;
6391
6392 bool
6393 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6394
6395 Type*
6396 do_type();
6397
6398 void
6399 do_determine_type(const Type_context*);
6400
6401 void
6402 do_check_types(Gogo*);
6403
6404 Expression*
6405 do_copy()
6406 {
6407 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6408 this->args()->copy(),
6409 this->is_varargs(),
6410 this->location());
6411 }
6412
6413 tree
6414 do_get_tree(Translate_context*);
6415
6416 void
6417 do_export(Export*) const;
6418
6419 virtual bool
6420 do_is_recover_call() const;
6421
6422 virtual void
6423 do_set_recover_arg(Expression*);
6424
6425 private:
6426 // The builtin functions.
6427 enum Builtin_function_code
6428 {
6429 BUILTIN_INVALID,
6430
6431 // Predeclared builtin functions.
6432 BUILTIN_APPEND,
6433 BUILTIN_CAP,
6434 BUILTIN_CLOSE,
6435 BUILTIN_CLOSED,
6436 BUILTIN_CMPLX,
6437 BUILTIN_COPY,
6438 BUILTIN_IMAG,
6439 BUILTIN_LEN,
6440 BUILTIN_MAKE,
6441 BUILTIN_NEW,
6442 BUILTIN_PANIC,
6443 BUILTIN_PRINT,
6444 BUILTIN_PRINTLN,
6445 BUILTIN_REAL,
6446 BUILTIN_RECOVER,
6447
6448 // Builtin functions from the unsafe package.
6449 BUILTIN_ALIGNOF,
6450 BUILTIN_OFFSETOF,
6451 BUILTIN_SIZEOF
6452 };
6453
6454 Expression*
6455 one_arg() const;
6456
6457 bool
6458 check_one_arg();
6459
6460 static Type*
6461 real_imag_type(Type*);
6462
6463 static Type*
6464 cmplx_type(Type*);
6465
6466 // A pointer back to the general IR structure. This avoids a global
6467 // variable, or passing it around everywhere.
6468 Gogo* gogo_;
6469 // The builtin function being called.
6470 Builtin_function_code code_;
6471 // Used to stop endless loops when the length of an array uses len
6472 // or cap of the array itself.
6473 mutable bool seen_;
6474 };
6475
6476 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6477 Expression* fn,
6478 Expression_list* args,
6479 bool is_varargs,
6480 source_location location)
6481 : Call_expression(fn, args, is_varargs, location),
6482 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
6483 {
6484 Func_expression* fnexp = this->fn()->func_expression();
6485 gcc_assert(fnexp != NULL);
6486 const std::string& name(fnexp->named_object()->name());
6487 if (name == "append")
6488 this->code_ = BUILTIN_APPEND;
6489 else if (name == "cap")
6490 this->code_ = BUILTIN_CAP;
6491 else if (name == "close")
6492 this->code_ = BUILTIN_CLOSE;
6493 else if (name == "closed")
6494 this->code_ = BUILTIN_CLOSED;
6495 else if (name == "cmplx")
6496 this->code_ = BUILTIN_CMPLX;
6497 else if (name == "copy")
6498 this->code_ = BUILTIN_COPY;
6499 else if (name == "imag")
6500 this->code_ = BUILTIN_IMAG;
6501 else if (name == "len")
6502 this->code_ = BUILTIN_LEN;
6503 else if (name == "make")
6504 this->code_ = BUILTIN_MAKE;
6505 else if (name == "new")
6506 this->code_ = BUILTIN_NEW;
6507 else if (name == "panic")
6508 this->code_ = BUILTIN_PANIC;
6509 else if (name == "print")
6510 this->code_ = BUILTIN_PRINT;
6511 else if (name == "println")
6512 this->code_ = BUILTIN_PRINTLN;
6513 else if (name == "real")
6514 this->code_ = BUILTIN_REAL;
6515 else if (name == "recover")
6516 this->code_ = BUILTIN_RECOVER;
6517 else if (name == "Alignof")
6518 this->code_ = BUILTIN_ALIGNOF;
6519 else if (name == "Offsetof")
6520 this->code_ = BUILTIN_OFFSETOF;
6521 else if (name == "Sizeof")
6522 this->code_ = BUILTIN_SIZEOF;
6523 else
6524 gcc_unreachable();
6525 }
6526
6527 // Return whether this is a call to recover. This is a virtual
6528 // function called from the parent class.
6529
6530 bool
6531 Builtin_call_expression::do_is_recover_call() const
6532 {
6533 if (this->classification() == EXPRESSION_ERROR)
6534 return false;
6535 return this->code_ == BUILTIN_RECOVER;
6536 }
6537
6538 // Set the argument for a call to recover.
6539
6540 void
6541 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6542 {
6543 const Expression_list* args = this->args();
6544 gcc_assert(args == NULL || args->empty());
6545 Expression_list* new_args = new Expression_list();
6546 new_args->push_back(arg);
6547 this->set_args(new_args);
6548 }
6549
6550 // A traversal class which looks for a call expression.
6551
6552 class Find_call_expression : public Traverse
6553 {
6554 public:
6555 Find_call_expression()
6556 : Traverse(traverse_expressions),
6557 found_(false)
6558 { }
6559
6560 int
6561 expression(Expression**);
6562
6563 bool
6564 found()
6565 { return this->found_; }
6566
6567 private:
6568 bool found_;
6569 };
6570
6571 int
6572 Find_call_expression::expression(Expression** pexpr)
6573 {
6574 if ((*pexpr)->call_expression() != NULL)
6575 {
6576 this->found_ = true;
6577 return TRAVERSE_EXIT;
6578 }
6579 return TRAVERSE_CONTINUE;
6580 }
6581
6582 // Lower a builtin call expression. This turns new and make into
6583 // specific expressions. We also convert to a constant if we can.
6584
6585 Expression*
6586 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6587 {
6588 if (this->code_ == BUILTIN_NEW)
6589 {
6590 const Expression_list* args = this->args();
6591 if (args == NULL || args->size() < 1)
6592 this->report_error(_("not enough arguments"));
6593 else if (args->size() > 1)
6594 this->report_error(_("too many arguments"));
6595 else
6596 {
6597 Expression* arg = args->front();
6598 if (!arg->is_type_expression())
6599 {
6600 error_at(arg->location(), "expected type");
6601 this->set_is_error();
6602 }
6603 else
6604 return Expression::make_allocation(arg->type(), this->location());
6605 }
6606 }
6607 else if (this->code_ == BUILTIN_MAKE)
6608 {
6609 const Expression_list* args = this->args();
6610 if (args == NULL || args->size() < 1)
6611 this->report_error(_("not enough arguments"));
6612 else
6613 {
6614 Expression* arg = args->front();
6615 if (!arg->is_type_expression())
6616 {
6617 error_at(arg->location(), "expected type");
6618 this->set_is_error();
6619 }
6620 else
6621 {
6622 Expression_list* newargs;
6623 if (args->size() == 1)
6624 newargs = NULL;
6625 else
6626 {
6627 newargs = new Expression_list();
6628 Expression_list::const_iterator p = args->begin();
6629 ++p;
6630 for (; p != args->end(); ++p)
6631 newargs->push_back(*p);
6632 }
6633 return Expression::make_make(arg->type(), newargs,
6634 this->location());
6635 }
6636 }
6637 }
6638 else if (this->is_constant())
6639 {
6640 // We can only lower len and cap if there are no function calls
6641 // in the arguments. Otherwise we have to make the call.
6642 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6643 {
6644 Expression* arg = this->one_arg();
6645 if (!arg->is_constant())
6646 {
6647 Find_call_expression find_call;
6648 Expression::traverse(&arg, &find_call);
6649 if (find_call.found())
6650 return this;
6651 }
6652 }
6653
6654 mpz_t ival;
6655 mpz_init(ival);
6656 Type* type;
6657 if (this->integer_constant_value(true, ival, &type))
6658 {
6659 Expression* ret = Expression::make_integer(&ival, type,
6660 this->location());
6661 mpz_clear(ival);
6662 return ret;
6663 }
6664 mpz_clear(ival);
6665
6666 mpfr_t rval;
6667 mpfr_init(rval);
6668 if (this->float_constant_value(rval, &type))
6669 {
6670 Expression* ret = Expression::make_float(&rval, type,
6671 this->location());
6672 mpfr_clear(rval);
6673 return ret;
6674 }
6675
6676 mpfr_t imag;
6677 mpfr_init(imag);
6678 if (this->complex_constant_value(rval, imag, &type))
6679 {
6680 Expression* ret = Expression::make_complex(&rval, &imag, type,
6681 this->location());
6682 mpfr_clear(rval);
6683 mpfr_clear(imag);
6684 return ret;
6685 }
6686 mpfr_clear(rval);
6687 mpfr_clear(imag);
6688 }
6689 else if (this->code_ == BUILTIN_RECOVER)
6690 {
6691 if (function != NULL)
6692 function->func_value()->set_calls_recover();
6693 else
6694 {
6695 // Calling recover outside of a function always returns the
6696 // nil empty interface.
6697 Type* eface = Type::make_interface_type(NULL, this->location());
6698 return Expression::make_cast(eface,
6699 Expression::make_nil(this->location()),
6700 this->location());
6701 }
6702 }
6703 else if (this->code_ == BUILTIN_APPEND)
6704 {
6705 // Lower the varargs.
6706 const Expression_list* args = this->args();
6707 if (args == NULL || args->empty())
6708 return this;
6709 Type* slice_type = args->front()->type();
6710 if (!slice_type->is_open_array_type())
6711 {
6712 error_at(args->front()->location(), "argument 1 must be a slice");
6713 this->set_is_error();
6714 return this;
6715 }
6716 return this->lower_varargs(gogo, function, slice_type, 2);
6717 }
6718
6719 return this;
6720 }
6721
6722 // Return the type of the real or imag functions, given the type of
6723 // the argument. We need to map complex to float, complex64 to
6724 // float32, and complex128 to float64, so it has to be done by name.
6725 // This returns NULL if it can't figure out the type.
6726
6727 Type*
6728 Builtin_call_expression::real_imag_type(Type* arg_type)
6729 {
6730 if (arg_type == NULL || arg_type->is_abstract())
6731 return NULL;
6732 Named_type* nt = arg_type->named_type();
6733 if (nt == NULL)
6734 return NULL;
6735 while (nt->real_type()->named_type() != NULL)
6736 nt = nt->real_type()->named_type();
6737 if (nt->name() == "complex")
6738 return Type::lookup_float_type("float");
6739 else if (nt->name() == "complex64")
6740 return Type::lookup_float_type("float32");
6741 else if (nt->name() == "complex128")
6742 return Type::lookup_float_type("float64");
6743 else
6744 return NULL;
6745 }
6746
6747 // Return the type of the cmplx function, given the type of one of the
6748 // argments. Like real_imag_type, we have to map by name.
6749
6750 Type*
6751 Builtin_call_expression::cmplx_type(Type* arg_type)
6752 {
6753 if (arg_type == NULL || arg_type->is_abstract())
6754 return NULL;
6755 Named_type* nt = arg_type->named_type();
6756 if (nt == NULL)
6757 return NULL;
6758 while (nt->real_type()->named_type() != NULL)
6759 nt = nt->real_type()->named_type();
6760 if (nt->name() == "float")
6761 return Type::lookup_complex_type("complex");
6762 else if (nt->name() == "float32")
6763 return Type::lookup_complex_type("complex64");
6764 else if (nt->name() == "float64")
6765 return Type::lookup_complex_type("complex128");
6766 else
6767 return NULL;
6768 }
6769
6770 // Return a single argument, or NULL if there isn't one.
6771
6772 Expression*
6773 Builtin_call_expression::one_arg() const
6774 {
6775 const Expression_list* args = this->args();
6776 if (args->size() != 1)
6777 return NULL;
6778 return args->front();
6779 }
6780
6781 // Return whether this is constant: len of a string, or len or cap of
6782 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6783
6784 bool
6785 Builtin_call_expression::do_is_constant() const
6786 {
6787 switch (this->code_)
6788 {
6789 case BUILTIN_LEN:
6790 case BUILTIN_CAP:
6791 {
6792 if (this->seen_)
6793 return false;
6794
6795 Expression* arg = this->one_arg();
6796 if (arg == NULL)
6797 return false;
6798 Type* arg_type = arg->type();
6799
6800 if (arg_type->points_to() != NULL
6801 && arg_type->points_to()->array_type() != NULL
6802 && !arg_type->points_to()->is_open_array_type())
6803 arg_type = arg_type->points_to();
6804
6805 if (arg_type->array_type() != NULL
6806 && arg_type->array_type()->length() != NULL)
6807 return true;
6808
6809 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6810 {
6811 this->seen_ = true;
6812 bool ret = arg->is_constant();
6813 this->seen_ = false;
6814 return ret;
6815 }
6816 }
6817 break;
6818
6819 case BUILTIN_SIZEOF:
6820 case BUILTIN_ALIGNOF:
6821 return this->one_arg() != NULL;
6822
6823 case BUILTIN_OFFSETOF:
6824 {
6825 Expression* arg = this->one_arg();
6826 if (arg == NULL)
6827 return false;
6828 return arg->field_reference_expression() != NULL;
6829 }
6830
6831 case BUILTIN_CMPLX:
6832 {
6833 const Expression_list* args = this->args();
6834 if (args != NULL && args->size() == 2)
6835 return args->front()->is_constant() && args->back()->is_constant();
6836 }
6837 break;
6838
6839 case BUILTIN_REAL:
6840 case BUILTIN_IMAG:
6841 {
6842 Expression* arg = this->one_arg();
6843 return arg != NULL && arg->is_constant();
6844 }
6845
6846 default:
6847 break;
6848 }
6849
6850 return false;
6851 }
6852
6853 // Return an integer constant value if possible.
6854
6855 bool
6856 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
6857 mpz_t val,
6858 Type** ptype) const
6859 {
6860 if (this->code_ == BUILTIN_LEN
6861 || this->code_ == BUILTIN_CAP)
6862 {
6863 Expression* arg = this->one_arg();
6864 if (arg == NULL)
6865 return false;
6866 Type* arg_type = arg->type();
6867
6868 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6869 {
6870 std::string sval;
6871 if (arg->string_constant_value(&sval))
6872 {
6873 mpz_set_ui(val, sval.length());
6874 *ptype = Type::lookup_integer_type("int");
6875 return true;
6876 }
6877 }
6878
6879 if (arg_type->points_to() != NULL
6880 && arg_type->points_to()->array_type() != NULL
6881 && !arg_type->points_to()->is_open_array_type())
6882 arg_type = arg_type->points_to();
6883
6884 if (arg_type->array_type() != NULL
6885 && arg_type->array_type()->length() != NULL)
6886 {
6887 if (this->seen_)
6888 return false;
6889 Expression* e = arg_type->array_type()->length();
6890 this->seen_ = true;
6891 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
6892 this->seen_ = false;
6893 if (r)
6894 {
6895 *ptype = Type::lookup_integer_type("int");
6896 return true;
6897 }
6898 }
6899 }
6900 else if (this->code_ == BUILTIN_SIZEOF
6901 || this->code_ == BUILTIN_ALIGNOF)
6902 {
6903 Expression* arg = this->one_arg();
6904 if (arg == NULL)
6905 return false;
6906 Type* arg_type = arg->type();
6907 if (arg_type->is_error_type() || arg_type->is_undefined())
6908 return false;
6909 if (arg_type->is_abstract())
6910 return false;
6911 tree arg_type_tree = arg_type->get_tree(this->gogo_);
6912 unsigned long val_long;
6913 if (this->code_ == BUILTIN_SIZEOF)
6914 {
6915 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
6916 gcc_assert(TREE_CODE(type_size) == INTEGER_CST);
6917 if (TREE_INT_CST_HIGH(type_size) != 0)
6918 return false;
6919 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
6920 val_long = static_cast<unsigned long>(val_wide);
6921 if (val_long != val_wide)
6922 return false;
6923 }
6924 else if (this->code_ == BUILTIN_ALIGNOF)
6925 {
6926 if (arg->field_reference_expression() == NULL)
6927 val_long = go_type_alignment(arg_type_tree);
6928 else
6929 {
6930 // Calling unsafe.Alignof(s.f) returns the alignment of
6931 // the type of f when it is used as a field in a struct.
6932 val_long = go_field_alignment(arg_type_tree);
6933 }
6934 }
6935 else
6936 gcc_unreachable();
6937 mpz_set_ui(val, val_long);
6938 *ptype = NULL;
6939 return true;
6940 }
6941 else if (this->code_ == BUILTIN_OFFSETOF)
6942 {
6943 Expression* arg = this->one_arg();
6944 if (arg == NULL)
6945 return false;
6946 Field_reference_expression* farg = arg->field_reference_expression();
6947 if (farg == NULL)
6948 return false;
6949 Expression* struct_expr = farg->expr();
6950 Type* st = struct_expr->type();
6951 if (st->struct_type() == NULL)
6952 return false;
6953 tree struct_tree = st->get_tree(this->gogo_);
6954 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
6955 tree field = TYPE_FIELDS(struct_tree);
6956 for (unsigned int index = farg->field_index(); index > 0; --index)
6957 {
6958 field = DECL_CHAIN(field);
6959 gcc_assert(field != NULL_TREE);
6960 }
6961 HOST_WIDE_INT offset_wide = int_byte_position (field);
6962 if (offset_wide < 0)
6963 return false;
6964 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
6965 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
6966 return false;
6967 mpz_set_ui(val, offset_long);
6968 return true;
6969 }
6970 return false;
6971 }
6972
6973 // Return a floating point constant value if possible.
6974
6975 bool
6976 Builtin_call_expression::do_float_constant_value(mpfr_t val,
6977 Type** ptype) const
6978 {
6979 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
6980 {
6981 Expression* arg = this->one_arg();
6982 if (arg == NULL)
6983 return false;
6984
6985 mpfr_t real;
6986 mpfr_t imag;
6987 mpfr_init(real);
6988 mpfr_init(imag);
6989
6990 bool ret = false;
6991 Type* type;
6992 if (arg->complex_constant_value(real, imag, &type))
6993 {
6994 if (this->code_ == BUILTIN_REAL)
6995 mpfr_set(val, real, GMP_RNDN);
6996 else
6997 mpfr_set(val, imag, GMP_RNDN);
6998 *ptype = Builtin_call_expression::real_imag_type(type);
6999 ret = true;
7000 }
7001
7002 mpfr_clear(real);
7003 mpfr_clear(imag);
7004 return ret;
7005 }
7006
7007 return false;
7008 }
7009
7010 // Return a complex constant value if possible.
7011
7012 bool
7013 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
7014 Type** ptype) const
7015 {
7016 if (this->code_ == BUILTIN_CMPLX)
7017 {
7018 const Expression_list* args = this->args();
7019 if (args == NULL || args->size() != 2)
7020 return false;
7021
7022 mpfr_t r;
7023 mpfr_init(r);
7024 Type* rtype;
7025 if (!args->front()->float_constant_value(r, &rtype))
7026 {
7027 mpfr_clear(r);
7028 return false;
7029 }
7030
7031 mpfr_t i;
7032 mpfr_init(i);
7033
7034 bool ret = false;
7035 Type* itype;
7036 if (args->back()->float_constant_value(i, &itype)
7037 && Type::are_identical(rtype, itype, false, NULL))
7038 {
7039 mpfr_set(real, r, GMP_RNDN);
7040 mpfr_set(imag, i, GMP_RNDN);
7041 *ptype = Builtin_call_expression::cmplx_type(rtype);
7042 ret = true;
7043 }
7044
7045 mpfr_clear(r);
7046 mpfr_clear(i);
7047
7048 return ret;
7049 }
7050
7051 return false;
7052 }
7053
7054 // Return the type.
7055
7056 Type*
7057 Builtin_call_expression::do_type()
7058 {
7059 switch (this->code_)
7060 {
7061 case BUILTIN_INVALID:
7062 default:
7063 gcc_unreachable();
7064
7065 case BUILTIN_NEW:
7066 case BUILTIN_MAKE:
7067 {
7068 const Expression_list* args = this->args();
7069 if (args == NULL || args->empty())
7070 return Type::make_error_type();
7071 return Type::make_pointer_type(args->front()->type());
7072 }
7073
7074 case BUILTIN_CAP:
7075 case BUILTIN_COPY:
7076 case BUILTIN_LEN:
7077 case BUILTIN_ALIGNOF:
7078 case BUILTIN_OFFSETOF:
7079 case BUILTIN_SIZEOF:
7080 return Type::lookup_integer_type("int");
7081
7082 case BUILTIN_CLOSE:
7083 case BUILTIN_PANIC:
7084 case BUILTIN_PRINT:
7085 case BUILTIN_PRINTLN:
7086 return Type::make_void_type();
7087
7088 case BUILTIN_CLOSED:
7089 return Type::lookup_bool_type();
7090
7091 case BUILTIN_RECOVER:
7092 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
7093
7094 case BUILTIN_APPEND:
7095 {
7096 const Expression_list* args = this->args();
7097 if (args == NULL || args->empty())
7098 return Type::make_error_type();
7099 return args->front()->type();
7100 }
7101
7102 case BUILTIN_REAL:
7103 case BUILTIN_IMAG:
7104 {
7105 Expression* arg = this->one_arg();
7106 if (arg == NULL)
7107 return Type::make_error_type();
7108 Type* t = arg->type();
7109 if (t->is_abstract())
7110 t = t->make_non_abstract_type();
7111 t = Builtin_call_expression::real_imag_type(t);
7112 if (t == NULL)
7113 t = Type::make_error_type();
7114 return t;
7115 }
7116
7117 case BUILTIN_CMPLX:
7118 {
7119 const Expression_list* args = this->args();
7120 if (args == NULL || args->size() != 2)
7121 return Type::make_error_type();
7122 Type* t = args->front()->type();
7123 if (t->is_abstract())
7124 {
7125 t = args->back()->type();
7126 if (t->is_abstract())
7127 t = t->make_non_abstract_type();
7128 }
7129 t = Builtin_call_expression::cmplx_type(t);
7130 if (t == NULL)
7131 t = Type::make_error_type();
7132 return t;
7133 }
7134 }
7135 }
7136
7137 // Determine the type.
7138
7139 void
7140 Builtin_call_expression::do_determine_type(const Type_context* context)
7141 {
7142 this->fn()->determine_type_no_context();
7143
7144 const Expression_list* args = this->args();
7145
7146 bool is_print;
7147 Type* arg_type = NULL;
7148 switch (this->code_)
7149 {
7150 case BUILTIN_PRINT:
7151 case BUILTIN_PRINTLN:
7152 // Do not force a large integer constant to "int".
7153 is_print = true;
7154 break;
7155
7156 case BUILTIN_REAL:
7157 case BUILTIN_IMAG:
7158 arg_type = Builtin_call_expression::cmplx_type(context->type);
7159 is_print = false;
7160 break;
7161
7162 case BUILTIN_CMPLX:
7163 {
7164 // For the cmplx function the type of one operand can
7165 // determine the type of the other, as in a binary expression.
7166 arg_type = Builtin_call_expression::real_imag_type(context->type);
7167 if (args != NULL && args->size() == 2)
7168 {
7169 Type* t1 = args->front()->type();
7170 Type* t2 = args->front()->type();
7171 if (!t1->is_abstract())
7172 arg_type = t1;
7173 else if (!t2->is_abstract())
7174 arg_type = t2;
7175 }
7176 is_print = false;
7177 }
7178 break;
7179
7180 default:
7181 is_print = false;
7182 break;
7183 }
7184
7185 if (args != NULL)
7186 {
7187 for (Expression_list::const_iterator pa = args->begin();
7188 pa != args->end();
7189 ++pa)
7190 {
7191 Type_context subcontext;
7192 subcontext.type = arg_type;
7193
7194 if (is_print)
7195 {
7196 // We want to print large constants, we so can't just
7197 // use the appropriate nonabstract type. Use uint64 for
7198 // an integer if we know it is nonnegative, otherwise
7199 // use int64 for a integer, otherwise use float64 for a
7200 // float or complex128 for a complex.
7201 Type* want_type = NULL;
7202 Type* atype = (*pa)->type();
7203 if (atype->is_abstract())
7204 {
7205 if (atype->integer_type() != NULL)
7206 {
7207 mpz_t val;
7208 mpz_init(val);
7209 Type* dummy;
7210 if (this->integer_constant_value(true, val, &dummy)
7211 && mpz_sgn(val) >= 0)
7212 want_type = Type::lookup_integer_type("uint64");
7213 else
7214 want_type = Type::lookup_integer_type("int64");
7215 mpz_clear(val);
7216 }
7217 else if (atype->float_type() != NULL)
7218 want_type = Type::lookup_float_type("float64");
7219 else if (atype->complex_type() != NULL)
7220 want_type = Type::lookup_complex_type("complex128");
7221 else if (atype->is_abstract_string_type())
7222 want_type = Type::lookup_string_type();
7223 else if (atype->is_abstract_boolean_type())
7224 want_type = Type::lookup_bool_type();
7225 else
7226 gcc_unreachable();
7227 subcontext.type = want_type;
7228 }
7229 }
7230
7231 (*pa)->determine_type(&subcontext);
7232 }
7233 }
7234 }
7235
7236 // If there is exactly one argument, return true. Otherwise give an
7237 // error message and return false.
7238
7239 bool
7240 Builtin_call_expression::check_one_arg()
7241 {
7242 const Expression_list* args = this->args();
7243 if (args == NULL || args->size() < 1)
7244 {
7245 this->report_error(_("not enough arguments"));
7246 return false;
7247 }
7248 else if (args->size() > 1)
7249 {
7250 this->report_error(_("too many arguments"));
7251 return false;
7252 }
7253 if (args->front()->is_error_expression()
7254 || args->front()->type()->is_error_type()
7255 || args->front()->type()->is_undefined())
7256 {
7257 this->set_is_error();
7258 return false;
7259 }
7260 return true;
7261 }
7262
7263 // Check argument types for a builtin function.
7264
7265 void
7266 Builtin_call_expression::do_check_types(Gogo*)
7267 {
7268 switch (this->code_)
7269 {
7270 case BUILTIN_INVALID:
7271 case BUILTIN_NEW:
7272 case BUILTIN_MAKE:
7273 return;
7274
7275 case BUILTIN_LEN:
7276 case BUILTIN_CAP:
7277 {
7278 // The single argument may be either a string or an array or a
7279 // map or a channel, or a pointer to a closed array.
7280 if (this->check_one_arg())
7281 {
7282 Type* arg_type = this->one_arg()->type();
7283 if (arg_type->points_to() != NULL
7284 && arg_type->points_to()->array_type() != NULL
7285 && !arg_type->points_to()->is_open_array_type())
7286 arg_type = arg_type->points_to();
7287 if (this->code_ == BUILTIN_CAP)
7288 {
7289 if (!arg_type->is_error_type()
7290 && arg_type->array_type() == NULL
7291 && arg_type->channel_type() == NULL)
7292 this->report_error(_("argument must be array or slice "
7293 "or channel"));
7294 }
7295 else
7296 {
7297 if (!arg_type->is_error_type()
7298 && !arg_type->is_string_type()
7299 && arg_type->array_type() == NULL
7300 && arg_type->map_type() == NULL
7301 && arg_type->channel_type() == NULL)
7302 this->report_error(_("argument must be string or "
7303 "array or slice or map or channel"));
7304 }
7305 }
7306 }
7307 break;
7308
7309 case BUILTIN_PRINT:
7310 case BUILTIN_PRINTLN:
7311 {
7312 const Expression_list* args = this->args();
7313 if (args == NULL)
7314 {
7315 if (this->code_ == BUILTIN_PRINT)
7316 warning_at(this->location(), 0,
7317 "no arguments for builtin function %<%s%>",
7318 (this->code_ == BUILTIN_PRINT
7319 ? "print"
7320 : "println"));
7321 }
7322 else
7323 {
7324 for (Expression_list::const_iterator p = args->begin();
7325 p != args->end();
7326 ++p)
7327 {
7328 Type* type = (*p)->type();
7329 if (type->is_error_type()
7330 || type->is_string_type()
7331 || type->integer_type() != NULL
7332 || type->float_type() != NULL
7333 || type->complex_type() != NULL
7334 || type->is_boolean_type()
7335 || type->points_to() != NULL
7336 || type->interface_type() != NULL
7337 || type->channel_type() != NULL
7338 || type->map_type() != NULL
7339 || type->function_type() != NULL
7340 || type->is_open_array_type())
7341 ;
7342 else
7343 this->report_error(_("unsupported argument type to "
7344 "builtin function"));
7345 }
7346 }
7347 }
7348 break;
7349
7350 case BUILTIN_CLOSE:
7351 case BUILTIN_CLOSED:
7352 if (this->check_one_arg())
7353 {
7354 if (this->one_arg()->type()->channel_type() == NULL)
7355 this->report_error(_("argument must be channel"));
7356 }
7357 break;
7358
7359 case BUILTIN_PANIC:
7360 case BUILTIN_SIZEOF:
7361 case BUILTIN_ALIGNOF:
7362 this->check_one_arg();
7363 break;
7364
7365 case BUILTIN_RECOVER:
7366 if (this->args() != NULL && !this->args()->empty())
7367 this->report_error(_("too many arguments"));
7368 break;
7369
7370 case BUILTIN_OFFSETOF:
7371 if (this->check_one_arg())
7372 {
7373 Expression* arg = this->one_arg();
7374 if (arg->field_reference_expression() == NULL)
7375 this->report_error(_("argument must be a field reference"));
7376 }
7377 break;
7378
7379 case BUILTIN_COPY:
7380 {
7381 const Expression_list* args = this->args();
7382 if (args == NULL || args->size() < 2)
7383 {
7384 this->report_error(_("not enough arguments"));
7385 break;
7386 }
7387 else if (args->size() > 2)
7388 {
7389 this->report_error(_("too many arguments"));
7390 break;
7391 }
7392 Type* arg1_type = args->front()->type();
7393 Type* arg2_type = args->back()->type();
7394 if (arg1_type->is_error_type() || arg2_type->is_error_type())
7395 break;
7396
7397 Type* e1;
7398 if (arg1_type->is_open_array_type())
7399 e1 = arg1_type->array_type()->element_type();
7400 else
7401 {
7402 this->report_error(_("left argument must be a slice"));
7403 break;
7404 }
7405
7406 Type* e2;
7407 if (arg2_type->is_open_array_type())
7408 e2 = arg2_type->array_type()->element_type();
7409 else if (arg2_type->is_string_type())
7410 e2 = Type::lookup_integer_type("uint8");
7411 else
7412 {
7413 this->report_error(_("right argument must be a slice or a string"));
7414 break;
7415 }
7416
7417 if (!Type::are_identical(e1, e2, true, NULL))
7418 this->report_error(_("element types must be the same"));
7419 }
7420 break;
7421
7422 case BUILTIN_APPEND:
7423 {
7424 const Expression_list* args = this->args();
7425 if (args == NULL || args->size() < 2)
7426 {
7427 this->report_error(_("not enough arguments"));
7428 break;
7429 }
7430 if (args->size() > 2)
7431 {
7432 this->report_error(_("too many arguments"));
7433 break;
7434 }
7435 std::string reason;
7436 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7437 &reason))
7438 {
7439 if (reason.empty())
7440 this->report_error(_("arguments 1 and 2 have different types"));
7441 else
7442 {
7443 error_at(this->location(),
7444 "arguments 1 and 2 have different types (%s)",
7445 reason.c_str());
7446 this->set_is_error();
7447 }
7448 }
7449 break;
7450 }
7451
7452 case BUILTIN_REAL:
7453 case BUILTIN_IMAG:
7454 if (this->check_one_arg())
7455 {
7456 if (this->one_arg()->type()->complex_type() == NULL)
7457 this->report_error(_("argument must have complex type"));
7458 }
7459 break;
7460
7461 case BUILTIN_CMPLX:
7462 {
7463 const Expression_list* args = this->args();
7464 if (args == NULL || args->size() < 2)
7465 this->report_error(_("not enough arguments"));
7466 else if (args->size() > 2)
7467 this->report_error(_("too many arguments"));
7468 else if (args->front()->is_error_expression()
7469 || args->front()->type()->is_error_type()
7470 || args->back()->is_error_expression()
7471 || args->back()->type()->is_error_type())
7472 this->set_is_error();
7473 else if (!Type::are_identical(args->front()->type(),
7474 args->back()->type(), true, NULL))
7475 this->report_error(_("cmplx arguments must have identical types"));
7476 else if (args->front()->type()->float_type() == NULL)
7477 this->report_error(_("cmplx arguments must have "
7478 "floating-point type"));
7479 }
7480 break;
7481
7482 default:
7483 gcc_unreachable();
7484 }
7485 }
7486
7487 // Return the tree for a builtin function.
7488
7489 tree
7490 Builtin_call_expression::do_get_tree(Translate_context* context)
7491 {
7492 Gogo* gogo = context->gogo();
7493 source_location location = this->location();
7494 switch (this->code_)
7495 {
7496 case BUILTIN_INVALID:
7497 case BUILTIN_NEW:
7498 case BUILTIN_MAKE:
7499 gcc_unreachable();
7500
7501 case BUILTIN_LEN:
7502 case BUILTIN_CAP:
7503 {
7504 const Expression_list* args = this->args();
7505 gcc_assert(args != NULL && args->size() == 1);
7506 Expression* arg = *args->begin();
7507 Type* arg_type = arg->type();
7508
7509 if (this->seen_)
7510 {
7511 gcc_assert(saw_errors());
7512 return error_mark_node;
7513 }
7514 this->seen_ = true;
7515
7516 tree arg_tree = arg->get_tree(context);
7517
7518 this->seen_ = false;
7519
7520 if (arg_tree == error_mark_node)
7521 return error_mark_node;
7522
7523 if (arg_type->points_to() != NULL)
7524 {
7525 arg_type = arg_type->points_to();
7526 gcc_assert(arg_type->array_type() != NULL
7527 && !arg_type->is_open_array_type());
7528 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7529 arg_tree = build_fold_indirect_ref(arg_tree);
7530 }
7531
7532 tree val_tree;
7533 if (this->code_ == BUILTIN_LEN)
7534 {
7535 if (arg_type->is_string_type())
7536 val_tree = String_type::length_tree(gogo, arg_tree);
7537 else if (arg_type->array_type() != NULL)
7538 {
7539 if (this->seen_)
7540 {
7541 gcc_assert(saw_errors());
7542 return error_mark_node;
7543 }
7544 this->seen_ = true;
7545 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7546 this->seen_ = false;
7547 }
7548 else if (arg_type->map_type() != NULL)
7549 {
7550 static tree map_len_fndecl;
7551 val_tree = Gogo::call_builtin(&map_len_fndecl,
7552 location,
7553 "__go_map_len",
7554 1,
7555 sizetype,
7556 arg_type->get_tree(gogo),
7557 arg_tree);
7558 }
7559 else if (arg_type->channel_type() != NULL)
7560 {
7561 static tree chan_len_fndecl;
7562 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7563 location,
7564 "__go_chan_len",
7565 1,
7566 sizetype,
7567 arg_type->get_tree(gogo),
7568 arg_tree);
7569 }
7570 else
7571 gcc_unreachable();
7572 }
7573 else
7574 {
7575 if (arg_type->array_type() != NULL)
7576 {
7577 if (this->seen_)
7578 {
7579 gcc_assert(saw_errors());
7580 return error_mark_node;
7581 }
7582 this->seen_ = true;
7583 val_tree = arg_type->array_type()->capacity_tree(gogo,
7584 arg_tree);
7585 this->seen_ = false;
7586 }
7587 else if (arg_type->channel_type() != NULL)
7588 {
7589 static tree chan_cap_fndecl;
7590 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7591 location,
7592 "__go_chan_cap",
7593 1,
7594 sizetype,
7595 arg_type->get_tree(gogo),
7596 arg_tree);
7597 }
7598 else
7599 gcc_unreachable();
7600 }
7601
7602 if (val_tree == error_mark_node)
7603 return error_mark_node;
7604
7605 tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo);
7606 if (type_tree == TREE_TYPE(val_tree))
7607 return val_tree;
7608 else
7609 return fold(convert_to_integer(type_tree, val_tree));
7610 }
7611
7612 case BUILTIN_PRINT:
7613 case BUILTIN_PRINTLN:
7614 {
7615 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7616 tree stmt_list = NULL_TREE;
7617
7618 const Expression_list* call_args = this->args();
7619 if (call_args != NULL)
7620 {
7621 for (Expression_list::const_iterator p = call_args->begin();
7622 p != call_args->end();
7623 ++p)
7624 {
7625 if (is_ln && p != call_args->begin())
7626 {
7627 static tree print_space_fndecl;
7628 tree call = Gogo::call_builtin(&print_space_fndecl,
7629 location,
7630 "__go_print_space",
7631 0,
7632 void_type_node);
7633 if (call == error_mark_node)
7634 return error_mark_node;
7635 append_to_statement_list(call, &stmt_list);
7636 }
7637
7638 Type* type = (*p)->type();
7639
7640 tree arg = (*p)->get_tree(context);
7641 if (arg == error_mark_node)
7642 return error_mark_node;
7643
7644 tree* pfndecl;
7645 const char* fnname;
7646 if (type->is_string_type())
7647 {
7648 static tree print_string_fndecl;
7649 pfndecl = &print_string_fndecl;
7650 fnname = "__go_print_string";
7651 }
7652 else if (type->integer_type() != NULL
7653 && type->integer_type()->is_unsigned())
7654 {
7655 static tree print_uint64_fndecl;
7656 pfndecl = &print_uint64_fndecl;
7657 fnname = "__go_print_uint64";
7658 Type* itype = Type::lookup_integer_type("uint64");
7659 arg = fold_convert_loc(location, itype->get_tree(gogo),
7660 arg);
7661 }
7662 else if (type->integer_type() != NULL)
7663 {
7664 static tree print_int64_fndecl;
7665 pfndecl = &print_int64_fndecl;
7666 fnname = "__go_print_int64";
7667 Type* itype = Type::lookup_integer_type("int64");
7668 arg = fold_convert_loc(location, itype->get_tree(gogo),
7669 arg);
7670 }
7671 else if (type->float_type() != NULL)
7672 {
7673 static tree print_double_fndecl;
7674 pfndecl = &print_double_fndecl;
7675 fnname = "__go_print_double";
7676 arg = fold_convert_loc(location, double_type_node, arg);
7677 }
7678 else if (type->complex_type() != NULL)
7679 {
7680 static tree print_complex_fndecl;
7681 pfndecl = &print_complex_fndecl;
7682 fnname = "__go_print_complex";
7683 arg = fold_convert_loc(location, complex_double_type_node,
7684 arg);
7685 }
7686 else if (type->is_boolean_type())
7687 {
7688 static tree print_bool_fndecl;
7689 pfndecl = &print_bool_fndecl;
7690 fnname = "__go_print_bool";
7691 }
7692 else if (type->points_to() != NULL
7693 || type->channel_type() != NULL
7694 || type->map_type() != NULL
7695 || type->function_type() != NULL)
7696 {
7697 static tree print_pointer_fndecl;
7698 pfndecl = &print_pointer_fndecl;
7699 fnname = "__go_print_pointer";
7700 arg = fold_convert_loc(location, ptr_type_node, arg);
7701 }
7702 else if (type->interface_type() != NULL)
7703 {
7704 if (type->interface_type()->is_empty())
7705 {
7706 static tree print_empty_interface_fndecl;
7707 pfndecl = &print_empty_interface_fndecl;
7708 fnname = "__go_print_empty_interface";
7709 }
7710 else
7711 {
7712 static tree print_interface_fndecl;
7713 pfndecl = &print_interface_fndecl;
7714 fnname = "__go_print_interface";
7715 }
7716 }
7717 else if (type->is_open_array_type())
7718 {
7719 static tree print_slice_fndecl;
7720 pfndecl = &print_slice_fndecl;
7721 fnname = "__go_print_slice";
7722 }
7723 else
7724 gcc_unreachable();
7725
7726 tree call = Gogo::call_builtin(pfndecl,
7727 location,
7728 fnname,
7729 1,
7730 void_type_node,
7731 TREE_TYPE(arg),
7732 arg);
7733 if (call == error_mark_node)
7734 return error_mark_node;
7735 append_to_statement_list(call, &stmt_list);
7736 }
7737 }
7738
7739 if (is_ln)
7740 {
7741 static tree print_nl_fndecl;
7742 tree call = Gogo::call_builtin(&print_nl_fndecl,
7743 location,
7744 "__go_print_nl",
7745 0,
7746 void_type_node);
7747 if (call == error_mark_node)
7748 return error_mark_node;
7749 append_to_statement_list(call, &stmt_list);
7750 }
7751
7752 return stmt_list;
7753 }
7754
7755 case BUILTIN_PANIC:
7756 {
7757 const Expression_list* args = this->args();
7758 gcc_assert(args != NULL && args->size() == 1);
7759 Expression* arg = args->front();
7760 tree arg_tree = arg->get_tree(context);
7761 if (arg_tree == error_mark_node)
7762 return error_mark_node;
7763 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7764 arg_tree = Expression::convert_for_assignment(context, empty,
7765 arg->type(),
7766 arg_tree, location);
7767 static tree panic_fndecl;
7768 tree call = Gogo::call_builtin(&panic_fndecl,
7769 location,
7770 "__go_panic",
7771 1,
7772 void_type_node,
7773 TREE_TYPE(arg_tree),
7774 arg_tree);
7775 if (call == error_mark_node)
7776 return error_mark_node;
7777 // This function will throw an exception.
7778 TREE_NOTHROW(panic_fndecl) = 0;
7779 // This function will not return.
7780 TREE_THIS_VOLATILE(panic_fndecl) = 1;
7781 return call;
7782 }
7783
7784 case BUILTIN_RECOVER:
7785 {
7786 // The argument is set when building recover thunks. It's a
7787 // boolean value which is true if we can recover a value now.
7788 const Expression_list* args = this->args();
7789 gcc_assert(args != NULL && args->size() == 1);
7790 Expression* arg = args->front();
7791 tree arg_tree = arg->get_tree(context);
7792 if (arg_tree == error_mark_node)
7793 return error_mark_node;
7794
7795 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7796 tree empty_tree = empty->get_tree(context->gogo());
7797
7798 Type* nil_type = Type::make_nil_type();
7799 Expression* nil = Expression::make_nil(location);
7800 tree nil_tree = nil->get_tree(context);
7801 tree empty_nil_tree = Expression::convert_for_assignment(context,
7802 empty,
7803 nil_type,
7804 nil_tree,
7805 location);
7806
7807 // We need to handle a deferred call to recover specially,
7808 // because it changes whether it can recover a panic or not.
7809 // See test7 in test/recover1.go.
7810 tree call;
7811 if (this->is_deferred())
7812 {
7813 static tree deferred_recover_fndecl;
7814 call = Gogo::call_builtin(&deferred_recover_fndecl,
7815 location,
7816 "__go_deferred_recover",
7817 0,
7818 empty_tree);
7819 }
7820 else
7821 {
7822 static tree recover_fndecl;
7823 call = Gogo::call_builtin(&recover_fndecl,
7824 location,
7825 "__go_recover",
7826 0,
7827 empty_tree);
7828 }
7829 if (call == error_mark_node)
7830 return error_mark_node;
7831 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
7832 call, empty_nil_tree);
7833 }
7834
7835 case BUILTIN_CLOSE:
7836 case BUILTIN_CLOSED:
7837 {
7838 const Expression_list* args = this->args();
7839 gcc_assert(args != NULL && args->size() == 1);
7840 Expression* arg = args->front();
7841 tree arg_tree = arg->get_tree(context);
7842 if (arg_tree == error_mark_node)
7843 return error_mark_node;
7844 if (this->code_ == BUILTIN_CLOSE)
7845 {
7846 static tree close_fndecl;
7847 return Gogo::call_builtin(&close_fndecl,
7848 location,
7849 "__go_builtin_close",
7850 1,
7851 void_type_node,
7852 TREE_TYPE(arg_tree),
7853 arg_tree);
7854 }
7855 else
7856 {
7857 static tree closed_fndecl;
7858 return Gogo::call_builtin(&closed_fndecl,
7859 location,
7860 "__go_builtin_closed",
7861 1,
7862 boolean_type_node,
7863 TREE_TYPE(arg_tree),
7864 arg_tree);
7865 }
7866 }
7867
7868 case BUILTIN_SIZEOF:
7869 case BUILTIN_OFFSETOF:
7870 case BUILTIN_ALIGNOF:
7871 {
7872 mpz_t val;
7873 mpz_init(val);
7874 Type* dummy;
7875 bool b = this->integer_constant_value(true, val, &dummy);
7876 gcc_assert(b);
7877 tree type = Type::lookup_integer_type("int")->get_tree(gogo);
7878 tree ret = Expression::integer_constant_tree(val, type);
7879 mpz_clear(val);
7880 return ret;
7881 }
7882
7883 case BUILTIN_COPY:
7884 {
7885 const Expression_list* args = this->args();
7886 gcc_assert(args != NULL && args->size() == 2);
7887 Expression* arg1 = args->front();
7888 Expression* arg2 = args->back();
7889
7890 tree arg1_tree = arg1->get_tree(context);
7891 tree arg2_tree = arg2->get_tree(context);
7892 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
7893 return error_mark_node;
7894
7895 Type* arg1_type = arg1->type();
7896 Array_type* at = arg1_type->array_type();
7897 arg1_tree = save_expr(arg1_tree);
7898 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
7899 tree arg1_len = at->length_tree(gogo, arg1_tree);
7900 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
7901 return error_mark_node;
7902
7903 Type* arg2_type = arg2->type();
7904 tree arg2_val;
7905 tree arg2_len;
7906 if (arg2_type->is_open_array_type())
7907 {
7908 at = arg2_type->array_type();
7909 arg2_tree = save_expr(arg2_tree);
7910 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
7911 arg2_len = at->length_tree(gogo, arg2_tree);
7912 }
7913 else
7914 {
7915 arg2_tree = save_expr(arg2_tree);
7916 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
7917 arg2_len = String_type::length_tree(gogo, arg2_tree);
7918 }
7919 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
7920 return error_mark_node;
7921
7922 arg1_len = save_expr(arg1_len);
7923 arg2_len = save_expr(arg2_len);
7924 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
7925 fold_build2_loc(location, LT_EXPR,
7926 boolean_type_node,
7927 arg1_len, arg2_len),
7928 arg1_len, arg2_len);
7929 len = save_expr(len);
7930
7931 Type* element_type = at->element_type();
7932 tree element_type_tree = element_type->get_tree(gogo);
7933 if (element_type_tree == error_mark_node)
7934 return error_mark_node;
7935 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
7936 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
7937 len);
7938 bytecount = fold_build2_loc(location, MULT_EXPR,
7939 TREE_TYPE(element_size),
7940 bytecount, element_size);
7941 bytecount = fold_convert_loc(location, size_type_node, bytecount);
7942
7943 arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val);
7944 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
7945
7946 static tree copy_fndecl;
7947 tree call = Gogo::call_builtin(&copy_fndecl,
7948 location,
7949 "__go_copy",
7950 3,
7951 void_type_node,
7952 ptr_type_node,
7953 arg1_val,
7954 ptr_type_node,
7955 arg2_val,
7956 size_type_node,
7957 bytecount);
7958 if (call == error_mark_node)
7959 return error_mark_node;
7960
7961 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
7962 call, len);
7963 }
7964
7965 case BUILTIN_APPEND:
7966 {
7967 const Expression_list* args = this->args();
7968 gcc_assert(args != NULL && args->size() == 2);
7969 Expression* arg1 = args->front();
7970 Expression* arg2 = args->back();
7971
7972 tree arg1_tree = arg1->get_tree(context);
7973 tree arg2_tree = arg2->get_tree(context);
7974 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
7975 return error_mark_node;
7976
7977 Array_type* at = arg1->type()->array_type();
7978 Type* element_type = at->element_type();
7979
7980 arg2_tree = Expression::convert_for_assignment(context, at,
7981 arg2->type(),
7982 arg2_tree,
7983 location);
7984 if (arg2_tree == error_mark_node)
7985 return error_mark_node;
7986
7987 arg2_tree = save_expr(arg2_tree);
7988 tree arg2_val = at->value_pointer_tree(gogo, arg2_tree);
7989 tree arg2_len = at->length_tree(gogo, arg2_tree);
7990 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
7991 return error_mark_node;
7992 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
7993 arg2_len = fold_convert_loc(location, size_type_node, arg2_len);
7994
7995 tree element_type_tree = element_type->get_tree(gogo);
7996 if (element_type_tree == error_mark_node)
7997 return error_mark_node;
7998 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
7999 element_size = fold_convert_loc(location, size_type_node,
8000 element_size);
8001
8002 // We rebuild the decl each time since the slice types may
8003 // change.
8004 tree append_fndecl = NULL_TREE;
8005 return Gogo::call_builtin(&append_fndecl,
8006 location,
8007 "__go_append",
8008 4,
8009 TREE_TYPE(arg1_tree),
8010 TREE_TYPE(arg1_tree),
8011 arg1_tree,
8012 ptr_type_node,
8013 arg2_val,
8014 size_type_node,
8015 arg2_len,
8016 size_type_node,
8017 element_size);
8018 }
8019
8020 case BUILTIN_REAL:
8021 case BUILTIN_IMAG:
8022 {
8023 const Expression_list* args = this->args();
8024 gcc_assert(args != NULL && args->size() == 1);
8025 Expression* arg = args->front();
8026 tree arg_tree = arg->get_tree(context);
8027 if (arg_tree == error_mark_node)
8028 return error_mark_node;
8029 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8030 if (this->code_ == BUILTIN_REAL)
8031 return fold_build1_loc(location, REALPART_EXPR,
8032 TREE_TYPE(TREE_TYPE(arg_tree)),
8033 arg_tree);
8034 else
8035 return fold_build1_loc(location, IMAGPART_EXPR,
8036 TREE_TYPE(TREE_TYPE(arg_tree)),
8037 arg_tree);
8038 }
8039
8040 case BUILTIN_CMPLX:
8041 {
8042 const Expression_list* args = this->args();
8043 gcc_assert(args != NULL && args->size() == 2);
8044 tree r = args->front()->get_tree(context);
8045 tree i = args->back()->get_tree(context);
8046 if (r == error_mark_node || i == error_mark_node)
8047 return error_mark_node;
8048 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8049 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8050 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8051 return fold_build2_loc(location, COMPLEX_EXPR,
8052 build_complex_type(TREE_TYPE(r)),
8053 r, i);
8054 }
8055
8056 default:
8057 gcc_unreachable();
8058 }
8059 }
8060
8061 // We have to support exporting a builtin call expression, because
8062 // code can set a constant to the result of a builtin expression.
8063
8064 void
8065 Builtin_call_expression::do_export(Export* exp) const
8066 {
8067 bool ok = false;
8068
8069 mpz_t val;
8070 mpz_init(val);
8071 Type* dummy;
8072 if (this->integer_constant_value(true, val, &dummy))
8073 {
8074 Integer_expression::export_integer(exp, val);
8075 ok = true;
8076 }
8077 mpz_clear(val);
8078
8079 if (!ok)
8080 {
8081 mpfr_t fval;
8082 mpfr_init(fval);
8083 if (this->float_constant_value(fval, &dummy))
8084 {
8085 Float_expression::export_float(exp, fval);
8086 ok = true;
8087 }
8088 mpfr_clear(fval);
8089 }
8090
8091 if (!ok)
8092 {
8093 mpfr_t real;
8094 mpfr_t imag;
8095 mpfr_init(real);
8096 mpfr_init(imag);
8097 if (this->complex_constant_value(real, imag, &dummy))
8098 {
8099 Complex_expression::export_complex(exp, real, imag);
8100 ok = true;
8101 }
8102 mpfr_clear(real);
8103 mpfr_clear(imag);
8104 }
8105
8106 if (!ok)
8107 {
8108 error_at(this->location(), "value is not constant");
8109 return;
8110 }
8111
8112 // A trailing space lets us reliably identify the end of the number.
8113 exp->write_c_string(" ");
8114 }
8115
8116 // Class Call_expression.
8117
8118 // Traversal.
8119
8120 int
8121 Call_expression::do_traverse(Traverse* traverse)
8122 {
8123 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8124 return TRAVERSE_EXIT;
8125 if (this->args_ != NULL)
8126 {
8127 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8128 return TRAVERSE_EXIT;
8129 }
8130 return TRAVERSE_CONTINUE;
8131 }
8132
8133 // Lower a call statement.
8134
8135 Expression*
8136 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
8137 {
8138 // A type case can look like a function call.
8139 if (this->fn_->is_type_expression()
8140 && this->args_ != NULL
8141 && this->args_->size() == 1)
8142 return Expression::make_cast(this->fn_->type(), this->args_->front(),
8143 this->location());
8144
8145 // Recognize a call to a builtin function.
8146 Func_expression* fne = this->fn_->func_expression();
8147 if (fne != NULL
8148 && fne->named_object()->is_function_declaration()
8149 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8150 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8151 this->is_varargs_, this->location());
8152
8153 // Handle an argument which is a call to a function which returns
8154 // multiple results.
8155 if (this->args_ != NULL
8156 && this->args_->size() == 1
8157 && this->args_->front()->call_expression() != NULL
8158 && this->fn_->type()->function_type() != NULL)
8159 {
8160 Function_type* fntype = this->fn_->type()->function_type();
8161 size_t rc = this->args_->front()->call_expression()->result_count();
8162 if (rc > 1
8163 && fntype->parameters() != NULL
8164 && (fntype->parameters()->size() == rc
8165 || (fntype->is_varargs()
8166 && fntype->parameters()->size() - 1 <= rc)))
8167 {
8168 Call_expression* call = this->args_->front()->call_expression();
8169 Expression_list* args = new Expression_list;
8170 for (size_t i = 0; i < rc; ++i)
8171 args->push_back(Expression::make_call_result(call, i));
8172 // We can't return a new call expression here, because this
8173 // one may be referenced by Call_result expressions. FIXME.
8174 delete this->args_;
8175 this->args_ = args;
8176 }
8177 }
8178
8179 // Handle a call to a varargs function by packaging up the extra
8180 // parameters.
8181 if (this->fn_->type()->function_type() != NULL
8182 && this->fn_->type()->function_type()->is_varargs())
8183 {
8184 Function_type* fntype = this->fn_->type()->function_type();
8185 const Typed_identifier_list* parameters = fntype->parameters();
8186 gcc_assert(parameters != NULL && !parameters->empty());
8187 Type* varargs_type = parameters->back().type();
8188 return this->lower_varargs(gogo, function, varargs_type,
8189 parameters->size());
8190 }
8191
8192 return this;
8193 }
8194
8195 // Lower a call to a varargs function. FUNCTION is the function in
8196 // which the call occurs--it's not the function we are calling.
8197 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8198 // PARAM_COUNT is the number of parameters of the function we are
8199 // calling; the last of these parameters will be the varargs
8200 // parameter.
8201
8202 Expression*
8203 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8204 Type* varargs_type, size_t param_count)
8205 {
8206 if (this->varargs_are_lowered_)
8207 return this;
8208
8209 source_location loc = this->location();
8210
8211 gcc_assert(param_count > 0);
8212 gcc_assert(varargs_type->is_open_array_type());
8213
8214 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8215 if (arg_count < param_count - 1)
8216 {
8217 // Not enough arguments; will be caught in check_types.
8218 return this;
8219 }
8220
8221 Expression_list* old_args = this->args_;
8222 Expression_list* new_args = new Expression_list();
8223 bool push_empty_arg = false;
8224 if (old_args == NULL || old_args->empty())
8225 {
8226 gcc_assert(param_count == 1);
8227 push_empty_arg = true;
8228 }
8229 else
8230 {
8231 Expression_list::const_iterator pa;
8232 int i = 1;
8233 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8234 {
8235 if (static_cast<size_t>(i) == param_count)
8236 break;
8237 new_args->push_back(*pa);
8238 }
8239
8240 // We have reached the varargs parameter.
8241
8242 bool issued_error = false;
8243 if (pa == old_args->end())
8244 push_empty_arg = true;
8245 else if (pa + 1 == old_args->end() && this->is_varargs_)
8246 new_args->push_back(*pa);
8247 else if (this->is_varargs_)
8248 {
8249 this->report_error(_("too many arguments"));
8250 return this;
8251 }
8252 else if (pa + 1 == old_args->end()
8253 && this->is_compatible_varargs_argument(function, *pa,
8254 varargs_type,
8255 &issued_error))
8256 new_args->push_back(*pa);
8257 else
8258 {
8259 Type* element_type = varargs_type->array_type()->element_type();
8260 Expression_list* vals = new Expression_list;
8261 for (; pa != old_args->end(); ++pa, ++i)
8262 {
8263 // Check types here so that we get a better message.
8264 Type* patype = (*pa)->type();
8265 source_location paloc = (*pa)->location();
8266 if (!this->check_argument_type(i, element_type, patype,
8267 paloc, issued_error))
8268 continue;
8269 vals->push_back(*pa);
8270 }
8271 Expression* val =
8272 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8273 new_args->push_back(val);
8274 }
8275 }
8276
8277 if (push_empty_arg)
8278 new_args->push_back(Expression::make_nil(loc));
8279
8280 // We can't return a new call expression here, because this one may
8281 // be referenced by Call_result expressions. FIXME.
8282 if (old_args != NULL)
8283 delete old_args;
8284 this->args_ = new_args;
8285 this->varargs_are_lowered_ = true;
8286
8287 // Lower all the new subexpressions.
8288 Expression* ret = this;
8289 gogo->lower_expression(function, &ret);
8290 gcc_assert(ret == this);
8291 return ret;
8292 }
8293
8294 // Return true if ARG is a varargs argment which should be passed to
8295 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8296 // will be the last argument passed in the call, and PARAM_TYPE will
8297 // be the type of the last parameter of the varargs function being
8298 // called.
8299
8300 bool
8301 Call_expression::is_compatible_varargs_argument(Named_object* function,
8302 Expression* arg,
8303 Type* param_type,
8304 bool* issued_error)
8305 {
8306 *issued_error = false;
8307
8308 Type* var_type = NULL;
8309
8310 // The simple case is passing the varargs parameter of the caller.
8311 Var_expression* ve = arg->var_expression();
8312 if (ve != NULL && ve->named_object()->is_variable())
8313 {
8314 Variable* var = ve->named_object()->var_value();
8315 if (var->is_varargs_parameter())
8316 var_type = var->type();
8317 }
8318
8319 // The complex case is passing the varargs parameter of some
8320 // enclosing function. This will look like passing down *c.f where
8321 // c is the closure variable and f is a field in the closure.
8322 if (function != NULL
8323 && function->func_value()->needs_closure()
8324 && arg->classification() == EXPRESSION_UNARY)
8325 {
8326 Unary_expression* ue = static_cast<Unary_expression*>(arg);
8327 if (ue->op() == OPERATOR_MULT)
8328 {
8329 Field_reference_expression* fre =
8330 ue->operand()->deref()->field_reference_expression();
8331 if (fre != NULL)
8332 {
8333 Var_expression* ve = fre->expr()->deref()->var_expression();
8334 if (ve != NULL)
8335 {
8336 Named_object* no = ve->named_object();
8337 Function* f = function->func_value();
8338 if (no == f->closure_var())
8339 {
8340 // At this point we know that this indeed a
8341 // reference to some enclosing variable. Now we
8342 // need to figure out whether that variable is a
8343 // varargs parameter.
8344 Named_object* enclosing =
8345 f->enclosing_var(fre->field_index());
8346 Variable* var = enclosing->var_value();
8347 if (var->is_varargs_parameter())
8348 var_type = var->type();
8349 }
8350 }
8351 }
8352 }
8353 }
8354
8355 if (var_type == NULL)
8356 return false;
8357
8358 // We only match if the parameter is the same, with an identical
8359 // type.
8360 Array_type* var_at = var_type->array_type();
8361 gcc_assert(var_at != NULL);
8362 Array_type* param_at = param_type->array_type();
8363 if (param_at != NULL
8364 && Type::are_identical(var_at->element_type(),
8365 param_at->element_type(), true, NULL))
8366 return true;
8367 error_at(arg->location(), "... mismatch: passing ...T as ...");
8368 *issued_error = true;
8369 return false;
8370 }
8371
8372 // Get the function type. Returns NULL if we don't know the type. If
8373 // this returns NULL, and if_ERROR is true, issues an error.
8374
8375 Function_type*
8376 Call_expression::get_function_type() const
8377 {
8378 return this->fn_->type()->function_type();
8379 }
8380
8381 // Return the number of values which this call will return.
8382
8383 size_t
8384 Call_expression::result_count() const
8385 {
8386 const Function_type* fntype = this->get_function_type();
8387 if (fntype == NULL)
8388 return 0;
8389 if (fntype->results() == NULL)
8390 return 0;
8391 return fntype->results()->size();
8392 }
8393
8394 // Return whether this is a call to the predeclared function recover.
8395
8396 bool
8397 Call_expression::is_recover_call() const
8398 {
8399 return this->do_is_recover_call();
8400 }
8401
8402 // Set the argument to the recover function.
8403
8404 void
8405 Call_expression::set_recover_arg(Expression* arg)
8406 {
8407 this->do_set_recover_arg(arg);
8408 }
8409
8410 // Virtual functions also implemented by Builtin_call_expression.
8411
8412 bool
8413 Call_expression::do_is_recover_call() const
8414 {
8415 return false;
8416 }
8417
8418 void
8419 Call_expression::do_set_recover_arg(Expression*)
8420 {
8421 gcc_unreachable();
8422 }
8423
8424 // Get the type.
8425
8426 Type*
8427 Call_expression::do_type()
8428 {
8429 if (this->type_ != NULL)
8430 return this->type_;
8431
8432 Type* ret;
8433 Function_type* fntype = this->get_function_type();
8434 if (fntype == NULL)
8435 return Type::make_error_type();
8436
8437 const Typed_identifier_list* results = fntype->results();
8438 if (results == NULL)
8439 ret = Type::make_void_type();
8440 else if (results->size() == 1)
8441 ret = results->begin()->type();
8442 else
8443 ret = Type::make_call_multiple_result_type(this);
8444
8445 this->type_ = ret;
8446
8447 return this->type_;
8448 }
8449
8450 // Determine types for a call expression. We can use the function
8451 // parameter types to set the types of the arguments.
8452
8453 void
8454 Call_expression::do_determine_type(const Type_context*)
8455 {
8456 this->fn_->determine_type_no_context();
8457 Function_type* fntype = this->get_function_type();
8458 const Typed_identifier_list* parameters = NULL;
8459 if (fntype != NULL)
8460 parameters = fntype->parameters();
8461 if (this->args_ != NULL)
8462 {
8463 Typed_identifier_list::const_iterator pt;
8464 if (parameters != NULL)
8465 pt = parameters->begin();
8466 for (Expression_list::const_iterator pa = this->args_->begin();
8467 pa != this->args_->end();
8468 ++pa)
8469 {
8470 if (parameters != NULL && pt != parameters->end())
8471 {
8472 Type_context subcontext(pt->type(), false);
8473 (*pa)->determine_type(&subcontext);
8474 ++pt;
8475 }
8476 else
8477 (*pa)->determine_type_no_context();
8478 }
8479 }
8480 }
8481
8482 // Check types for parameter I.
8483
8484 bool
8485 Call_expression::check_argument_type(int i, const Type* parameter_type,
8486 const Type* argument_type,
8487 source_location argument_location,
8488 bool issued_error)
8489 {
8490 std::string reason;
8491 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8492 {
8493 if (!issued_error)
8494 {
8495 if (reason.empty())
8496 error_at(argument_location, "argument %d has incompatible type", i);
8497 else
8498 error_at(argument_location,
8499 "argument %d has incompatible type (%s)",
8500 i, reason.c_str());
8501 }
8502 this->set_is_error();
8503 return false;
8504 }
8505 return true;
8506 }
8507
8508 // Check types.
8509
8510 void
8511 Call_expression::do_check_types(Gogo*)
8512 {
8513 Function_type* fntype = this->get_function_type();
8514 if (fntype == NULL)
8515 {
8516 if (!this->fn_->type()->is_error_type())
8517 this->report_error(_("expected function"));
8518 return;
8519 }
8520
8521 if (fntype->is_method())
8522 {
8523 // We don't support pointers to methods, so the function has to
8524 // be a bound method expression.
8525 Bound_method_expression* bme = this->fn_->bound_method_expression();
8526 if (bme == NULL)
8527 {
8528 this->report_error(_("method call without object"));
8529 return;
8530 }
8531 Type* first_arg_type = bme->first_argument()->type();
8532 if (first_arg_type->points_to() == NULL)
8533 {
8534 // When passing a value, we need to check that we are
8535 // permitted to copy it.
8536 std::string reason;
8537 if (!Type::are_assignable(fntype->receiver()->type(),
8538 first_arg_type, &reason))
8539 {
8540 if (reason.empty())
8541 this->report_error(_("incompatible type for receiver"));
8542 else
8543 {
8544 error_at(this->location(),
8545 "incompatible type for receiver (%s)",
8546 reason.c_str());
8547 this->set_is_error();
8548 }
8549 }
8550 }
8551 }
8552
8553 // Note that varargs was handled by the lower_varargs() method, so
8554 // we don't have to worry about it here.
8555
8556 const Typed_identifier_list* parameters = fntype->parameters();
8557 if (this->args_ == NULL)
8558 {
8559 if (parameters != NULL && !parameters->empty())
8560 this->report_error(_("not enough arguments"));
8561 }
8562 else if (parameters == NULL)
8563 this->report_error(_("too many arguments"));
8564 else
8565 {
8566 int i = 0;
8567 Typed_identifier_list::const_iterator pt = parameters->begin();
8568 for (Expression_list::const_iterator pa = this->args_->begin();
8569 pa != this->args_->end();
8570 ++pa, ++pt, ++i)
8571 {
8572 if (pt == parameters->end())
8573 {
8574 this->report_error(_("too many arguments"));
8575 return;
8576 }
8577 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8578 (*pa)->location(), false);
8579 }
8580 if (pt != parameters->end())
8581 this->report_error(_("not enough arguments"));
8582 }
8583 }
8584
8585 // Return whether we have to use a temporary variable to ensure that
8586 // we evaluate this call expression in order. If the call returns no
8587 // results then it will inevitably be executed last. If the call
8588 // returns more than one result then it will be used with Call_result
8589 // expressions. So we only have to use a temporary variable if the
8590 // call returns exactly one result.
8591
8592 bool
8593 Call_expression::do_must_eval_in_order() const
8594 {
8595 return this->result_count() == 1;
8596 }
8597
8598 // Get the function and the first argument to use when calling a bound
8599 // method.
8600
8601 tree
8602 Call_expression::bound_method_function(Translate_context* context,
8603 Bound_method_expression* bound_method,
8604 tree* first_arg_ptr)
8605 {
8606 Expression* first_argument = bound_method->first_argument();
8607 tree first_arg = first_argument->get_tree(context);
8608 if (first_arg == error_mark_node)
8609 return error_mark_node;
8610
8611 // We always pass a pointer to the first argument when calling a
8612 // method.
8613 if (first_argument->type()->points_to() == NULL)
8614 {
8615 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8616 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8617 || DECL_P(first_arg)
8618 || TREE_CODE(first_arg) == INDIRECT_REF
8619 || TREE_CODE(first_arg) == COMPONENT_REF)
8620 {
8621 first_arg = build_fold_addr_expr(first_arg);
8622 if (DECL_P(first_arg))
8623 TREE_ADDRESSABLE(first_arg) = 1;
8624 }
8625 else
8626 {
8627 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8628 get_name(first_arg));
8629 DECL_IGNORED_P(tmp) = 0;
8630 DECL_INITIAL(tmp) = first_arg;
8631 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8632 build1(DECL_EXPR, void_type_node, tmp),
8633 build_fold_addr_expr(tmp));
8634 TREE_ADDRESSABLE(tmp) = 1;
8635 }
8636 if (first_arg == error_mark_node)
8637 return error_mark_node;
8638 }
8639
8640 Type* fatype = bound_method->first_argument_type();
8641 if (fatype != NULL)
8642 {
8643 if (fatype->points_to() == NULL)
8644 fatype = Type::make_pointer_type(fatype);
8645 first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg);
8646 if (first_arg == error_mark_node
8647 || TREE_TYPE(first_arg) == error_mark_node)
8648 return error_mark_node;
8649 }
8650
8651 *first_arg_ptr = first_arg;
8652
8653 return bound_method->method()->get_tree(context);
8654 }
8655
8656 // Get the function and the first argument to use when calling an
8657 // interface method.
8658
8659 tree
8660 Call_expression::interface_method_function(
8661 Translate_context* context,
8662 Interface_field_reference_expression* interface_method,
8663 tree* first_arg_ptr)
8664 {
8665 tree expr = interface_method->expr()->get_tree(context);
8666 if (expr == error_mark_node)
8667 return error_mark_node;
8668 expr = save_expr(expr);
8669 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8670 if (first_arg == error_mark_node)
8671 return error_mark_node;
8672 *first_arg_ptr = first_arg;
8673 return interface_method->get_function_tree(context, expr);
8674 }
8675
8676 // Build the call expression.
8677
8678 tree
8679 Call_expression::do_get_tree(Translate_context* context)
8680 {
8681 if (this->tree_ != NULL_TREE)
8682 return this->tree_;
8683
8684 Function_type* fntype = this->get_function_type();
8685 if (fntype == NULL)
8686 return error_mark_node;
8687
8688 if (this->fn_->is_error_expression())
8689 return error_mark_node;
8690
8691 Gogo* gogo = context->gogo();
8692 source_location location = this->location();
8693
8694 Func_expression* func = this->fn_->func_expression();
8695 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8696 Interface_field_reference_expression* interface_method =
8697 this->fn_->interface_field_reference_expression();
8698 const bool has_closure = func != NULL && func->closure() != NULL;
8699 const bool is_method = bound_method != NULL || interface_method != NULL;
8700 gcc_assert(!fntype->is_method() || is_method);
8701
8702 int nargs;
8703 tree* args;
8704 if (this->args_ == NULL || this->args_->empty())
8705 {
8706 nargs = is_method ? 1 : 0;
8707 args = nargs == 0 ? NULL : new tree[nargs];
8708 }
8709 else
8710 {
8711 const Typed_identifier_list* params = fntype->parameters();
8712 gcc_assert(params != NULL);
8713
8714 nargs = this->args_->size();
8715 int i = is_method ? 1 : 0;
8716 nargs += i;
8717 args = new tree[nargs];
8718
8719 Typed_identifier_list::const_iterator pp = params->begin();
8720 Expression_list::const_iterator pe;
8721 for (pe = this->args_->begin();
8722 pe != this->args_->end();
8723 ++pe, ++pp, ++i)
8724 {
8725 gcc_assert(pp != params->end());
8726 tree arg_val = (*pe)->get_tree(context);
8727 args[i] = Expression::convert_for_assignment(context,
8728 pp->type(),
8729 (*pe)->type(),
8730 arg_val,
8731 location);
8732 if (args[i] == error_mark_node)
8733 {
8734 delete[] args;
8735 return error_mark_node;
8736 }
8737 }
8738 gcc_assert(pp == params->end());
8739 gcc_assert(i == nargs);
8740 }
8741
8742 tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo)));
8743 if (rettype == error_mark_node)
8744 {
8745 delete[] args;
8746 return error_mark_node;
8747 }
8748
8749 tree fn;
8750 if (has_closure)
8751 fn = func->get_tree_without_closure(gogo);
8752 else if (!is_method)
8753 fn = this->fn_->get_tree(context);
8754 else if (bound_method != NULL)
8755 fn = this->bound_method_function(context, bound_method, &args[0]);
8756 else if (interface_method != NULL)
8757 fn = this->interface_method_function(context, interface_method, &args[0]);
8758 else
8759 gcc_unreachable();
8760
8761 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8762 {
8763 delete[] args;
8764 return error_mark_node;
8765 }
8766
8767 // This is to support builtin math functions when using 80387 math.
8768 tree fndecl = fn;
8769 if (TREE_CODE(fndecl) == ADDR_EXPR)
8770 fndecl = TREE_OPERAND(fndecl, 0);
8771 tree excess_type = NULL_TREE;
8772 if (DECL_P(fndecl)
8773 && DECL_IS_BUILTIN(fndecl)
8774 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8775 && nargs > 0
8776 && ((SCALAR_FLOAT_TYPE_P(rettype)
8777 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8778 || (COMPLEX_FLOAT_TYPE_P(rettype)
8779 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8780 {
8781 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8782 if (excess_type != NULL_TREE)
8783 {
8784 tree excess_fndecl = mathfn_built_in(excess_type,
8785 DECL_FUNCTION_CODE(fndecl));
8786 if (excess_fndecl == NULL_TREE)
8787 excess_type = NULL_TREE;
8788 else
8789 {
8790 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8791 for (int i = 0; i < nargs; ++i)
8792 args[i] = ::convert(excess_type, args[i]);
8793 }
8794 }
8795 }
8796
8797 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
8798 fn, nargs, args);
8799 delete[] args;
8800
8801 SET_EXPR_LOCATION(ret, location);
8802
8803 if (has_closure)
8804 {
8805 tree closure_tree = func->closure()->get_tree(context);
8806 if (closure_tree != error_mark_node)
8807 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
8808 }
8809
8810 // If this is a recursive function type which returns itself, as in
8811 // type F func() F
8812 // we have used ptr_type_node for the return type. Add a cast here
8813 // to the correct type.
8814 if (TREE_TYPE(ret) == ptr_type_node)
8815 {
8816 tree t = this->type()->get_tree(gogo);
8817 ret = fold_convert_loc(location, t, ret);
8818 }
8819
8820 if (excess_type != NULL_TREE)
8821 {
8822 // Calling convert here can undo our excess precision change.
8823 // That may or may not be a bug in convert_to_real.
8824 ret = build1(NOP_EXPR, rettype, ret);
8825 }
8826
8827 // If there is more than one result, we will refer to the call
8828 // multiple times.
8829 if (fntype->results() != NULL && fntype->results()->size() > 1)
8830 ret = save_expr(ret);
8831
8832 this->tree_ = ret;
8833
8834 return ret;
8835 }
8836
8837 // Make a call expression.
8838
8839 Call_expression*
8840 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
8841 source_location location)
8842 {
8843 return new Call_expression(fn, args, is_varargs, location);
8844 }
8845
8846 // A single result from a call which returns multiple results.
8847
8848 class Call_result_expression : public Expression
8849 {
8850 public:
8851 Call_result_expression(Call_expression* call, unsigned int index)
8852 : Expression(EXPRESSION_CALL_RESULT, call->location()),
8853 call_(call), index_(index)
8854 { }
8855
8856 protected:
8857 int
8858 do_traverse(Traverse*);
8859
8860 Type*
8861 do_type();
8862
8863 void
8864 do_determine_type(const Type_context*);
8865
8866 void
8867 do_check_types(Gogo*);
8868
8869 Expression*
8870 do_copy()
8871 {
8872 return new Call_result_expression(this->call_->call_expression(),
8873 this->index_);
8874 }
8875
8876 bool
8877 do_must_eval_in_order() const
8878 { return true; }
8879
8880 tree
8881 do_get_tree(Translate_context*);
8882
8883 private:
8884 // The underlying call expression.
8885 Expression* call_;
8886 // Which result we want.
8887 unsigned int index_;
8888 };
8889
8890 // Traverse a call result.
8891
8892 int
8893 Call_result_expression::do_traverse(Traverse* traverse)
8894 {
8895 if (traverse->remember_expression(this->call_))
8896 {
8897 // We have already traversed the call expression.
8898 return TRAVERSE_CONTINUE;
8899 }
8900 return Expression::traverse(&this->call_, traverse);
8901 }
8902
8903 // Get the type.
8904
8905 Type*
8906 Call_result_expression::do_type()
8907 {
8908 if (this->classification() == EXPRESSION_ERROR)
8909 return Type::make_error_type();
8910
8911 // THIS->CALL_ can be replaced with a temporary reference due to
8912 // Call_expression::do_must_eval_in_order when there is an error.
8913 Call_expression* ce = this->call_->call_expression();
8914 if (ce == NULL)
8915 {
8916 this->set_is_error();
8917 return Type::make_error_type();
8918 }
8919 Function_type* fntype = ce->get_function_type();
8920 if (fntype == NULL)
8921 {
8922 this->set_is_error();
8923 return Type::make_error_type();
8924 }
8925 const Typed_identifier_list* results = fntype->results();
8926 if (results == NULL)
8927 {
8928 this->report_error(_("number of results does not match "
8929 "number of values"));
8930 return Type::make_error_type();
8931 }
8932 Typed_identifier_list::const_iterator pr = results->begin();
8933 for (unsigned int i = 0; i < this->index_; ++i)
8934 {
8935 if (pr == results->end())
8936 break;
8937 ++pr;
8938 }
8939 if (pr == results->end())
8940 {
8941 this->report_error(_("number of results does not match "
8942 "number of values"));
8943 return Type::make_error_type();
8944 }
8945 return pr->type();
8946 }
8947
8948 // Check the type. Just make sure that we trigger the warning in
8949 // do_type.
8950
8951 void
8952 Call_result_expression::do_check_types(Gogo*)
8953 {
8954 this->type();
8955 }
8956
8957 // Determine the type. We have nothing to do here, but the 0 result
8958 // needs to pass down to the caller.
8959
8960 void
8961 Call_result_expression::do_determine_type(const Type_context*)
8962 {
8963 if (this->index_ == 0)
8964 this->call_->determine_type_no_context();
8965 }
8966
8967 // Return the tree.
8968
8969 tree
8970 Call_result_expression::do_get_tree(Translate_context* context)
8971 {
8972 tree call_tree = this->call_->get_tree(context);
8973 if (call_tree == error_mark_node)
8974 return error_mark_node;
8975 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
8976 {
8977 gcc_assert(saw_errors());
8978 return error_mark_node;
8979 }
8980 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
8981 for (unsigned int i = 0; i < this->index_; ++i)
8982 {
8983 gcc_assert(field != NULL_TREE);
8984 field = DECL_CHAIN(field);
8985 }
8986 gcc_assert(field != NULL_TREE);
8987 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
8988 }
8989
8990 // Make a reference to a single result of a call which returns
8991 // multiple results.
8992
8993 Expression*
8994 Expression::make_call_result(Call_expression* call, unsigned int index)
8995 {
8996 return new Call_result_expression(call, index);
8997 }
8998
8999 // Class Index_expression.
9000
9001 // Traversal.
9002
9003 int
9004 Index_expression::do_traverse(Traverse* traverse)
9005 {
9006 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9007 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9008 || (this->end_ != NULL
9009 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9010 return TRAVERSE_EXIT;
9011 return TRAVERSE_CONTINUE;
9012 }
9013
9014 // Lower an index expression. This converts the generic index
9015 // expression into an array index, a string index, or a map index.
9016
9017 Expression*
9018 Index_expression::do_lower(Gogo*, Named_object*, int)
9019 {
9020 source_location location = this->location();
9021 Expression* left = this->left_;
9022 Expression* start = this->start_;
9023 Expression* end = this->end_;
9024
9025 Type* type = left->type();
9026 if (type->is_error_type())
9027 return Expression::make_error(location);
9028 else if (type->array_type() != NULL)
9029 return Expression::make_array_index(left, start, end, location);
9030 else if (type->points_to() != NULL
9031 && type->points_to()->array_type() != NULL
9032 && !type->points_to()->is_open_array_type())
9033 {
9034 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9035 location);
9036 return Expression::make_array_index(deref, start, end, location);
9037 }
9038 else if (type->is_string_type())
9039 return Expression::make_string_index(left, start, end, location);
9040 else if (type->map_type() != NULL)
9041 {
9042 if (end != NULL)
9043 {
9044 error_at(location, "invalid slice of map");
9045 return Expression::make_error(location);
9046 }
9047 Map_index_expression* ret= Expression::make_map_index(left, start,
9048 location);
9049 if (this->is_lvalue_)
9050 ret->set_is_lvalue();
9051 return ret;
9052 }
9053 else
9054 {
9055 error_at(location,
9056 "attempt to index object which is not array, string, or map");
9057 return Expression::make_error(location);
9058 }
9059 }
9060
9061 // Make an index expression.
9062
9063 Expression*
9064 Expression::make_index(Expression* left, Expression* start, Expression* end,
9065 source_location location)
9066 {
9067 return new Index_expression(left, start, end, location);
9068 }
9069
9070 // An array index. This is used for both indexing and slicing.
9071
9072 class Array_index_expression : public Expression
9073 {
9074 public:
9075 Array_index_expression(Expression* array, Expression* start,
9076 Expression* end, source_location location)
9077 : Expression(EXPRESSION_ARRAY_INDEX, location),
9078 array_(array), start_(start), end_(end), type_(NULL)
9079 { }
9080
9081 protected:
9082 int
9083 do_traverse(Traverse*);
9084
9085 Type*
9086 do_type();
9087
9088 void
9089 do_determine_type(const Type_context*);
9090
9091 void
9092 do_check_types(Gogo*);
9093
9094 Expression*
9095 do_copy()
9096 {
9097 return Expression::make_array_index(this->array_->copy(),
9098 this->start_->copy(),
9099 (this->end_ == NULL
9100 ? NULL
9101 : this->end_->copy()),
9102 this->location());
9103 }
9104
9105 bool
9106 do_is_addressable() const;
9107
9108 void
9109 do_address_taken(bool escapes)
9110 { this->array_->address_taken(escapes); }
9111
9112 tree
9113 do_get_tree(Translate_context*);
9114
9115 private:
9116 // The array we are getting a value from.
9117 Expression* array_;
9118 // The start or only index.
9119 Expression* start_;
9120 // The end index of a slice. This may be NULL for a simple array
9121 // index, or it may be a nil expression for the length of the array.
9122 Expression* end_;
9123 // The type of the expression.
9124 Type* type_;
9125 };
9126
9127 // Array index traversal.
9128
9129 int
9130 Array_index_expression::do_traverse(Traverse* traverse)
9131 {
9132 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9133 return TRAVERSE_EXIT;
9134 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9135 return TRAVERSE_EXIT;
9136 if (this->end_ != NULL)
9137 {
9138 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9139 return TRAVERSE_EXIT;
9140 }
9141 return TRAVERSE_CONTINUE;
9142 }
9143
9144 // Return the type of an array index.
9145
9146 Type*
9147 Array_index_expression::do_type()
9148 {
9149 if (this->type_ == NULL)
9150 {
9151 Array_type* type = this->array_->type()->array_type();
9152 if (type == NULL)
9153 this->type_ = Type::make_error_type();
9154 else if (this->end_ == NULL)
9155 this->type_ = type->element_type();
9156 else if (type->is_open_array_type())
9157 {
9158 // A slice of a slice has the same type as the original
9159 // slice.
9160 this->type_ = this->array_->type()->deref();
9161 }
9162 else
9163 {
9164 // A slice of an array is a slice.
9165 this->type_ = Type::make_array_type(type->element_type(), NULL);
9166 }
9167 }
9168 return this->type_;
9169 }
9170
9171 // Set the type of an array index.
9172
9173 void
9174 Array_index_expression::do_determine_type(const Type_context*)
9175 {
9176 this->array_->determine_type_no_context();
9177 Type_context subcontext(NULL, true);
9178 this->start_->determine_type(&subcontext);
9179 if (this->end_ != NULL)
9180 this->end_->determine_type(&subcontext);
9181 }
9182
9183 // Check types of an array index.
9184
9185 void
9186 Array_index_expression::do_check_types(Gogo*)
9187 {
9188 if (this->start_->type()->integer_type() == NULL)
9189 this->report_error(_("index must be integer"));
9190 if (this->end_ != NULL
9191 && this->end_->type()->integer_type() == NULL
9192 && !this->end_->is_nil_expression())
9193 this->report_error(_("slice end must be integer"));
9194
9195 Array_type* array_type = this->array_->type()->array_type();
9196 if (array_type == NULL)
9197 {
9198 gcc_assert(this->array_->type()->is_error_type());
9199 return;
9200 }
9201
9202 unsigned int int_bits =
9203 Type::lookup_integer_type("int")->integer_type()->bits();
9204
9205 Type* dummy;
9206 mpz_t lval;
9207 mpz_init(lval);
9208 bool lval_valid = (array_type->length() != NULL
9209 && array_type->length()->integer_constant_value(true,
9210 lval,
9211 &dummy));
9212 mpz_t ival;
9213 mpz_init(ival);
9214 if (this->start_->integer_constant_value(true, ival, &dummy))
9215 {
9216 if (mpz_sgn(ival) < 0
9217 || mpz_sizeinbase(ival, 2) >= int_bits
9218 || (lval_valid
9219 && (this->end_ == NULL
9220 ? mpz_cmp(ival, lval) >= 0
9221 : mpz_cmp(ival, lval) > 0)))
9222 {
9223 error_at(this->start_->location(), "array index out of bounds");
9224 this->set_is_error();
9225 }
9226 }
9227 if (this->end_ != NULL && !this->end_->is_nil_expression())
9228 {
9229 if (this->end_->integer_constant_value(true, ival, &dummy))
9230 {
9231 if (mpz_sgn(ival) < 0
9232 || mpz_sizeinbase(ival, 2) >= int_bits
9233 || (lval_valid && mpz_cmp(ival, lval) > 0))
9234 {
9235 error_at(this->end_->location(), "array index out of bounds");
9236 this->set_is_error();
9237 }
9238 }
9239 }
9240 mpz_clear(ival);
9241 mpz_clear(lval);
9242
9243 // A slice of an array requires an addressable array. A slice of a
9244 // slice is always possible.
9245 if (this->end_ != NULL
9246 && !array_type->is_open_array_type()
9247 && !this->array_->is_addressable())
9248 this->report_error(_("array is not addressable"));
9249 }
9250
9251 // Return whether this expression is addressable.
9252
9253 bool
9254 Array_index_expression::do_is_addressable() const
9255 {
9256 // A slice expression is not addressable.
9257 if (this->end_ != NULL)
9258 return false;
9259
9260 // An index into a slice is addressable.
9261 if (this->array_->type()->is_open_array_type())
9262 return true;
9263
9264 // An index into an array is addressable if the array is
9265 // addressable.
9266 return this->array_->is_addressable();
9267 }
9268
9269 // Get a tree for an array index.
9270
9271 tree
9272 Array_index_expression::do_get_tree(Translate_context* context)
9273 {
9274 Gogo* gogo = context->gogo();
9275 source_location loc = this->location();
9276
9277 Array_type* array_type = this->array_->type()->array_type();
9278 if (array_type == NULL)
9279 {
9280 gcc_assert(this->array_->type()->is_error_type());
9281 return error_mark_node;
9282 }
9283
9284 tree type_tree = array_type->get_tree(gogo);
9285 if (type_tree == error_mark_node)
9286 return error_mark_node;
9287
9288 tree array_tree = this->array_->get_tree(context);
9289 if (array_tree == error_mark_node)
9290 return error_mark_node;
9291
9292 if (array_type->length() == NULL && !DECL_P(array_tree))
9293 array_tree = save_expr(array_tree);
9294 tree length_tree = array_type->length_tree(gogo, array_tree);
9295 if (length_tree == error_mark_node)
9296 return error_mark_node;
9297 length_tree = save_expr(length_tree);
9298 tree length_type = TREE_TYPE(length_tree);
9299
9300 tree bad_index = boolean_false_node;
9301
9302 tree start_tree = this->start_->get_tree(context);
9303 if (start_tree == error_mark_node)
9304 return error_mark_node;
9305 if (!DECL_P(start_tree))
9306 start_tree = save_expr(start_tree);
9307 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9308 start_tree = convert_to_integer(length_type, start_tree);
9309
9310 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9311 loc);
9312
9313 start_tree = fold_convert_loc(loc, length_type, start_tree);
9314 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9315 fold_build2_loc(loc,
9316 (this->end_ == NULL
9317 ? GE_EXPR
9318 : GT_EXPR),
9319 boolean_type_node, start_tree,
9320 length_tree));
9321
9322 int code = (array_type->length() != NULL
9323 ? (this->end_ == NULL
9324 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9325 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9326 : (this->end_ == NULL
9327 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9328 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9329 tree crash = Gogo::runtime_error(code, loc);
9330
9331 if (this->end_ == NULL)
9332 {
9333 // Simple array indexing. This has to return an l-value, so
9334 // wrap the index check into START_TREE.
9335 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9336 build3(COND_EXPR, void_type_node,
9337 bad_index, crash, NULL_TREE),
9338 start_tree);
9339 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9340
9341 if (array_type->length() != NULL)
9342 {
9343 // Fixed array.
9344 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9345 start_tree, NULL_TREE, NULL_TREE);
9346 }
9347 else
9348 {
9349 // Open array.
9350 tree values = array_type->value_pointer_tree(gogo, array_tree);
9351 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9352 if (element_type_tree == error_mark_node)
9353 return error_mark_node;
9354 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9355 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9356 start_tree, element_size);
9357 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9358 TREE_TYPE(values), values, offset);
9359 return build_fold_indirect_ref(ptr);
9360 }
9361 }
9362
9363 // Array slice.
9364
9365 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9366 if (capacity_tree == error_mark_node)
9367 return error_mark_node;
9368 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9369
9370 tree end_tree;
9371 if (this->end_->is_nil_expression())
9372 end_tree = length_tree;
9373 else
9374 {
9375 end_tree = this->end_->get_tree(context);
9376 if (end_tree == error_mark_node)
9377 return error_mark_node;
9378 if (!DECL_P(end_tree))
9379 end_tree = save_expr(end_tree);
9380 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9381 end_tree = convert_to_integer(length_type, end_tree);
9382
9383 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9384 loc);
9385
9386 end_tree = fold_convert_loc(loc, length_type, end_tree);
9387
9388 capacity_tree = save_expr(capacity_tree);
9389 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9390 fold_build2_loc(loc, LT_EXPR,
9391 boolean_type_node,
9392 end_tree, start_tree),
9393 fold_build2_loc(loc, GT_EXPR,
9394 boolean_type_node,
9395 end_tree, capacity_tree));
9396 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9397 bad_index, bad_end);
9398 }
9399
9400 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9401 if (element_type_tree == error_mark_node)
9402 return error_mark_node;
9403 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9404
9405 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9406 fold_convert_loc(loc, sizetype, start_tree),
9407 element_size);
9408
9409 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9410 if (value_pointer == error_mark_node)
9411 return error_mark_node;
9412
9413 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9414 TREE_TYPE(value_pointer),
9415 value_pointer, offset);
9416
9417 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9418 end_tree, start_tree);
9419
9420 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9421 capacity_tree, start_tree);
9422
9423 tree struct_tree = this->type()->get_tree(gogo);
9424 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9425
9426 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9427
9428 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9429 tree field = TYPE_FIELDS(struct_tree);
9430 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9431 elt->index = field;
9432 elt->value = value_pointer;
9433
9434 elt = VEC_quick_push(constructor_elt, init, NULL);
9435 field = DECL_CHAIN(field);
9436 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9437 elt->index = field;
9438 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9439
9440 elt = VEC_quick_push(constructor_elt, init, NULL);
9441 field = DECL_CHAIN(field);
9442 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9443 elt->index = field;
9444 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9445
9446 tree constructor = build_constructor(struct_tree, init);
9447
9448 if (TREE_CONSTANT(value_pointer)
9449 && TREE_CONSTANT(result_length_tree)
9450 && TREE_CONSTANT(result_capacity_tree))
9451 TREE_CONSTANT(constructor) = 1;
9452
9453 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9454 build3(COND_EXPR, void_type_node,
9455 bad_index, crash, NULL_TREE),
9456 constructor);
9457 }
9458
9459 // Make an array index expression. END may be NULL.
9460
9461 Expression*
9462 Expression::make_array_index(Expression* array, Expression* start,
9463 Expression* end, source_location location)
9464 {
9465 // Taking a slice of a composite literal requires moving the literal
9466 // onto the heap.
9467 if (end != NULL && array->is_composite_literal())
9468 {
9469 array = Expression::make_heap_composite(array, location);
9470 array = Expression::make_unary(OPERATOR_MULT, array, location);
9471 }
9472 return new Array_index_expression(array, start, end, location);
9473 }
9474
9475 // A string index. This is used for both indexing and slicing.
9476
9477 class String_index_expression : public Expression
9478 {
9479 public:
9480 String_index_expression(Expression* string, Expression* start,
9481 Expression* end, source_location location)
9482 : Expression(EXPRESSION_STRING_INDEX, location),
9483 string_(string), start_(start), end_(end)
9484 { }
9485
9486 protected:
9487 int
9488 do_traverse(Traverse*);
9489
9490 Type*
9491 do_type();
9492
9493 void
9494 do_determine_type(const Type_context*);
9495
9496 void
9497 do_check_types(Gogo*);
9498
9499 Expression*
9500 do_copy()
9501 {
9502 return Expression::make_string_index(this->string_->copy(),
9503 this->start_->copy(),
9504 (this->end_ == NULL
9505 ? NULL
9506 : this->end_->copy()),
9507 this->location());
9508 }
9509
9510 tree
9511 do_get_tree(Translate_context*);
9512
9513 private:
9514 // The string we are getting a value from.
9515 Expression* string_;
9516 // The start or only index.
9517 Expression* start_;
9518 // The end index of a slice. This may be NULL for a single index,
9519 // or it may be a nil expression for the length of the string.
9520 Expression* end_;
9521 };
9522
9523 // String index traversal.
9524
9525 int
9526 String_index_expression::do_traverse(Traverse* traverse)
9527 {
9528 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9529 return TRAVERSE_EXIT;
9530 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9531 return TRAVERSE_EXIT;
9532 if (this->end_ != NULL)
9533 {
9534 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9535 return TRAVERSE_EXIT;
9536 }
9537 return TRAVERSE_CONTINUE;
9538 }
9539
9540 // Return the type of a string index.
9541
9542 Type*
9543 String_index_expression::do_type()
9544 {
9545 if (this->end_ == NULL)
9546 return Type::lookup_integer_type("uint8");
9547 else
9548 return this->string_->type();
9549 }
9550
9551 // Determine the type of a string index.
9552
9553 void
9554 String_index_expression::do_determine_type(const Type_context*)
9555 {
9556 this->string_->determine_type_no_context();
9557 Type_context subcontext(NULL, true);
9558 this->start_->determine_type(&subcontext);
9559 if (this->end_ != NULL)
9560 this->end_->determine_type(&subcontext);
9561 }
9562
9563 // Check types of a string index.
9564
9565 void
9566 String_index_expression::do_check_types(Gogo*)
9567 {
9568 if (this->start_->type()->integer_type() == NULL)
9569 this->report_error(_("index must be integer"));
9570 if (this->end_ != NULL
9571 && this->end_->type()->integer_type() == NULL
9572 && !this->end_->is_nil_expression())
9573 this->report_error(_("slice end must be integer"));
9574
9575 std::string sval;
9576 bool sval_valid = this->string_->string_constant_value(&sval);
9577
9578 mpz_t ival;
9579 mpz_init(ival);
9580 Type* dummy;
9581 if (this->start_->integer_constant_value(true, ival, &dummy))
9582 {
9583 if (mpz_sgn(ival) < 0
9584 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9585 {
9586 error_at(this->start_->location(), "string index out of bounds");
9587 this->set_is_error();
9588 }
9589 }
9590 if (this->end_ != NULL && !this->end_->is_nil_expression())
9591 {
9592 if (this->end_->integer_constant_value(true, ival, &dummy))
9593 {
9594 if (mpz_sgn(ival) < 0
9595 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9596 {
9597 error_at(this->end_->location(), "string index out of bounds");
9598 this->set_is_error();
9599 }
9600 }
9601 }
9602 mpz_clear(ival);
9603 }
9604
9605 // Get a tree for a string index.
9606
9607 tree
9608 String_index_expression::do_get_tree(Translate_context* context)
9609 {
9610 source_location loc = this->location();
9611
9612 tree string_tree = this->string_->get_tree(context);
9613 if (string_tree == error_mark_node)
9614 return error_mark_node;
9615
9616 if (this->string_->type()->points_to() != NULL)
9617 string_tree = build_fold_indirect_ref(string_tree);
9618 if (!DECL_P(string_tree))
9619 string_tree = save_expr(string_tree);
9620 tree string_type = TREE_TYPE(string_tree);
9621
9622 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9623 length_tree = save_expr(length_tree);
9624 tree length_type = TREE_TYPE(length_tree);
9625
9626 tree bad_index = boolean_false_node;
9627
9628 tree start_tree = this->start_->get_tree(context);
9629 if (start_tree == error_mark_node)
9630 return error_mark_node;
9631 if (!DECL_P(start_tree))
9632 start_tree = save_expr(start_tree);
9633 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9634 start_tree = convert_to_integer(length_type, start_tree);
9635
9636 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9637 loc);
9638
9639 start_tree = fold_convert_loc(loc, length_type, start_tree);
9640
9641 int code = (this->end_ == NULL
9642 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9643 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9644 tree crash = Gogo::runtime_error(code, loc);
9645
9646 if (this->end_ == NULL)
9647 {
9648 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9649 bad_index,
9650 fold_build2_loc(loc, GE_EXPR,
9651 boolean_type_node,
9652 start_tree, length_tree));
9653
9654 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9655 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9656 bytes_tree,
9657 fold_convert_loc(loc, sizetype, start_tree));
9658 tree index = build_fold_indirect_ref_loc(loc, ptr);
9659
9660 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9661 build3(COND_EXPR, void_type_node,
9662 bad_index, crash, NULL_TREE),
9663 index);
9664 }
9665 else
9666 {
9667 tree end_tree;
9668 if (this->end_->is_nil_expression())
9669 end_tree = build_int_cst(length_type, -1);
9670 else
9671 {
9672 end_tree = this->end_->get_tree(context);
9673 if (end_tree == error_mark_node)
9674 return error_mark_node;
9675 if (!DECL_P(end_tree))
9676 end_tree = save_expr(end_tree);
9677 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9678 end_tree = convert_to_integer(length_type, end_tree);
9679
9680 bad_index = Expression::check_bounds(end_tree, length_type,
9681 bad_index, loc);
9682
9683 end_tree = fold_convert_loc(loc, length_type, end_tree);
9684 }
9685
9686 static tree strslice_fndecl;
9687 tree ret = Gogo::call_builtin(&strslice_fndecl,
9688 loc,
9689 "__go_string_slice",
9690 3,
9691 string_type,
9692 string_type,
9693 string_tree,
9694 length_type,
9695 start_tree,
9696 length_type,
9697 end_tree);
9698 if (ret == error_mark_node)
9699 return error_mark_node;
9700 // This will panic if the bounds are out of range for the
9701 // string.
9702 TREE_NOTHROW(strslice_fndecl) = 0;
9703
9704 if (bad_index == boolean_false_node)
9705 return ret;
9706 else
9707 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9708 build3(COND_EXPR, void_type_node,
9709 bad_index, crash, NULL_TREE),
9710 ret);
9711 }
9712 }
9713
9714 // Make a string index expression. END may be NULL.
9715
9716 Expression*
9717 Expression::make_string_index(Expression* string, Expression* start,
9718 Expression* end, source_location location)
9719 {
9720 return new String_index_expression(string, start, end, location);
9721 }
9722
9723 // Class Map_index.
9724
9725 // Get the type of the map.
9726
9727 Map_type*
9728 Map_index_expression::get_map_type() const
9729 {
9730 Map_type* mt = this->map_->type()->deref()->map_type();
9731 if (mt == NULL)
9732 gcc_assert(saw_errors());
9733 return mt;
9734 }
9735
9736 // Map index traversal.
9737
9738 int
9739 Map_index_expression::do_traverse(Traverse* traverse)
9740 {
9741 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9742 return TRAVERSE_EXIT;
9743 return Expression::traverse(&this->index_, traverse);
9744 }
9745
9746 // Return the type of a map index.
9747
9748 Type*
9749 Map_index_expression::do_type()
9750 {
9751 Map_type* mt = this->get_map_type();
9752 if (mt == NULL)
9753 return Type::make_error_type();
9754 Type* type = mt->val_type();
9755 // If this map index is in a tuple assignment, we actually return a
9756 // pointer to the value type. Tuple_map_assignment_statement is
9757 // responsible for handling this correctly. We need to get the type
9758 // right in case this gets assigned to a temporary variable.
9759 if (this->is_in_tuple_assignment_)
9760 type = Type::make_pointer_type(type);
9761 return type;
9762 }
9763
9764 // Fix the type of a map index.
9765
9766 void
9767 Map_index_expression::do_determine_type(const Type_context*)
9768 {
9769 this->map_->determine_type_no_context();
9770 Map_type* mt = this->get_map_type();
9771 Type* key_type = mt == NULL ? NULL : mt->key_type();
9772 Type_context subcontext(key_type, false);
9773 this->index_->determine_type(&subcontext);
9774 }
9775
9776 // Check types of a map index.
9777
9778 void
9779 Map_index_expression::do_check_types(Gogo*)
9780 {
9781 std::string reason;
9782 Map_type* mt = this->get_map_type();
9783 if (mt == NULL)
9784 return;
9785 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
9786 {
9787 if (reason.empty())
9788 this->report_error(_("incompatible type for map index"));
9789 else
9790 {
9791 error_at(this->location(), "incompatible type for map index (%s)",
9792 reason.c_str());
9793 this->set_is_error();
9794 }
9795 }
9796 }
9797
9798 // Get a tree for a map index.
9799
9800 tree
9801 Map_index_expression::do_get_tree(Translate_context* context)
9802 {
9803 Map_type* type = this->get_map_type();
9804 if (type == NULL)
9805 return error_mark_node;
9806
9807 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
9808 if (valptr == error_mark_node)
9809 return error_mark_node;
9810 valptr = save_expr(valptr);
9811
9812 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
9813
9814 if (this->is_lvalue_)
9815 return build_fold_indirect_ref(valptr);
9816 else if (this->is_in_tuple_assignment_)
9817 {
9818 // Tuple_map_assignment_statement is responsible for using this
9819 // appropriately.
9820 return valptr;
9821 }
9822 else
9823 {
9824 return fold_build3(COND_EXPR, val_type_tree,
9825 fold_build2(EQ_EXPR, boolean_type_node, valptr,
9826 fold_convert(TREE_TYPE(valptr),
9827 null_pointer_node)),
9828 type->val_type()->get_init_tree(context->gogo(),
9829 false),
9830 build_fold_indirect_ref(valptr));
9831 }
9832 }
9833
9834 // Get a tree for the map index. This returns a tree which evaluates
9835 // to a pointer to a value. The pointer will be NULL if the key is
9836 // not in the map.
9837
9838 tree
9839 Map_index_expression::get_value_pointer(Translate_context* context,
9840 bool insert)
9841 {
9842 Map_type* type = this->get_map_type();
9843 if (type == NULL)
9844 return error_mark_node;
9845
9846 tree map_tree = this->map_->get_tree(context);
9847 tree index_tree = this->index_->get_tree(context);
9848 index_tree = Expression::convert_for_assignment(context, type->key_type(),
9849 this->index_->type(),
9850 index_tree,
9851 this->location());
9852 if (map_tree == error_mark_node || index_tree == error_mark_node)
9853 return error_mark_node;
9854
9855 if (this->map_->type()->points_to() != NULL)
9856 map_tree = build_fold_indirect_ref(map_tree);
9857
9858 // We need to pass in a pointer to the key, so stuff it into a
9859 // variable.
9860 tree tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
9861 DECL_IGNORED_P(tmp) = 0;
9862 DECL_INITIAL(tmp) = index_tree;
9863 tree make_tmp = build1(DECL_EXPR, void_type_node, tmp);
9864 tree tmpref = fold_convert(const_ptr_type_node, build_fold_addr_expr(tmp));
9865 TREE_ADDRESSABLE(tmp) = 1;
9866
9867 static tree map_index_fndecl;
9868 tree call = Gogo::call_builtin(&map_index_fndecl,
9869 this->location(),
9870 "__go_map_index",
9871 3,
9872 const_ptr_type_node,
9873 TREE_TYPE(map_tree),
9874 map_tree,
9875 const_ptr_type_node,
9876 tmpref,
9877 boolean_type_node,
9878 (insert
9879 ? boolean_true_node
9880 : boolean_false_node));
9881 if (call == error_mark_node)
9882 return error_mark_node;
9883 // This can panic on a map of interface type if the interface holds
9884 // an uncomparable or unhashable type.
9885 TREE_NOTHROW(map_index_fndecl) = 0;
9886
9887 tree val_type_tree = type->val_type()->get_tree(context->gogo());
9888 if (val_type_tree == error_mark_node)
9889 return error_mark_node;
9890 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
9891
9892 return build2(COMPOUND_EXPR, ptr_val_type_tree,
9893 make_tmp,
9894 fold_convert(ptr_val_type_tree, call));
9895 }
9896
9897 // Make a map index expression.
9898
9899 Map_index_expression*
9900 Expression::make_map_index(Expression* map, Expression* index,
9901 source_location location)
9902 {
9903 return new Map_index_expression(map, index, location);
9904 }
9905
9906 // Class Field_reference_expression.
9907
9908 // Return the type of a field reference.
9909
9910 Type*
9911 Field_reference_expression::do_type()
9912 {
9913 Struct_type* struct_type = this->expr_->type()->struct_type();
9914 gcc_assert(struct_type != NULL);
9915 return struct_type->field(this->field_index_)->type();
9916 }
9917
9918 // Check the types for a field reference.
9919
9920 void
9921 Field_reference_expression::do_check_types(Gogo*)
9922 {
9923 Struct_type* struct_type = this->expr_->type()->struct_type();
9924 gcc_assert(struct_type != NULL);
9925 gcc_assert(struct_type->field(this->field_index_) != NULL);
9926 }
9927
9928 // Get a tree for a field reference.
9929
9930 tree
9931 Field_reference_expression::do_get_tree(Translate_context* context)
9932 {
9933 tree struct_tree = this->expr_->get_tree(context);
9934 if (struct_tree == error_mark_node
9935 || TREE_TYPE(struct_tree) == error_mark_node)
9936 return error_mark_node;
9937 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
9938 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
9939 if (field == NULL_TREE)
9940 {
9941 // This can happen for a type which refers to itself indirectly
9942 // and then turns out to be erroneous.
9943 gcc_assert(saw_errors());
9944 return error_mark_node;
9945 }
9946 for (unsigned int i = this->field_index_; i > 0; --i)
9947 {
9948 field = DECL_CHAIN(field);
9949 gcc_assert(field != NULL_TREE);
9950 }
9951 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
9952 NULL_TREE);
9953 }
9954
9955 // Make a reference to a qualified identifier in an expression.
9956
9957 Field_reference_expression*
9958 Expression::make_field_reference(Expression* expr, unsigned int field_index,
9959 source_location location)
9960 {
9961 return new Field_reference_expression(expr, field_index, location);
9962 }
9963
9964 // Class Interface_field_reference_expression.
9965
9966 // Return a tree for the pointer to the function to call.
9967
9968 tree
9969 Interface_field_reference_expression::get_function_tree(Translate_context*,
9970 tree expr)
9971 {
9972 if (this->expr_->type()->points_to() != NULL)
9973 expr = build_fold_indirect_ref(expr);
9974
9975 tree expr_type = TREE_TYPE(expr);
9976 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
9977
9978 tree field = TYPE_FIELDS(expr_type);
9979 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
9980
9981 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
9982 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table)));
9983
9984 table = build_fold_indirect_ref(table);
9985 gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
9986
9987 std::string name = Gogo::unpack_hidden_name(this->name_);
9988 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
9989 field != NULL_TREE;
9990 field = DECL_CHAIN(field))
9991 {
9992 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
9993 break;
9994 }
9995 gcc_assert(field != NULL_TREE);
9996
9997 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
9998 }
9999
10000 // Return a tree for the first argument to pass to the interface
10001 // function.
10002
10003 tree
10004 Interface_field_reference_expression::get_underlying_object_tree(
10005 Translate_context*,
10006 tree expr)
10007 {
10008 if (this->expr_->type()->points_to() != NULL)
10009 expr = build_fold_indirect_ref(expr);
10010
10011 tree expr_type = TREE_TYPE(expr);
10012 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10013
10014 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
10015 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
10016
10017 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10018 }
10019
10020 // Traversal.
10021
10022 int
10023 Interface_field_reference_expression::do_traverse(Traverse* traverse)
10024 {
10025 return Expression::traverse(&this->expr_, traverse);
10026 }
10027
10028 // Return the type of an interface field reference.
10029
10030 Type*
10031 Interface_field_reference_expression::do_type()
10032 {
10033 Type* expr_type = this->expr_->type();
10034
10035 Type* points_to = expr_type->points_to();
10036 if (points_to != NULL)
10037 expr_type = points_to;
10038
10039 Interface_type* interface_type = expr_type->interface_type();
10040 if (interface_type == NULL)
10041 return Type::make_error_type();
10042
10043 const Typed_identifier* method = interface_type->find_method(this->name_);
10044 if (method == NULL)
10045 return Type::make_error_type();
10046
10047 return method->type();
10048 }
10049
10050 // Determine types.
10051
10052 void
10053 Interface_field_reference_expression::do_determine_type(const Type_context*)
10054 {
10055 this->expr_->determine_type_no_context();
10056 }
10057
10058 // Check the types for an interface field reference.
10059
10060 void
10061 Interface_field_reference_expression::do_check_types(Gogo*)
10062 {
10063 Type* type = this->expr_->type();
10064
10065 Type* points_to = type->points_to();
10066 if (points_to != NULL)
10067 type = points_to;
10068
10069 Interface_type* interface_type = type->interface_type();
10070 if (interface_type == NULL)
10071 this->report_error(_("expected interface or pointer to interface"));
10072 else
10073 {
10074 const Typed_identifier* method =
10075 interface_type->find_method(this->name_);
10076 if (method == NULL)
10077 {
10078 error_at(this->location(), "method %qs not in interface",
10079 Gogo::message_name(this->name_).c_str());
10080 this->set_is_error();
10081 }
10082 }
10083 }
10084
10085 // Get a tree for a reference to a field in an interface. There is no
10086 // standard tree type representation for this: it's a function
10087 // attached to its first argument, like a Bound_method_expression.
10088 // The only places it may currently be used are in a Call_expression
10089 // or a Go_statement, which will take it apart directly. So this has
10090 // nothing to do at present.
10091
10092 tree
10093 Interface_field_reference_expression::do_get_tree(Translate_context*)
10094 {
10095 gcc_unreachable();
10096 }
10097
10098 // Make a reference to a field in an interface.
10099
10100 Expression*
10101 Expression::make_interface_field_reference(Expression* expr,
10102 const std::string& field,
10103 source_location location)
10104 {
10105 return new Interface_field_reference_expression(expr, field, location);
10106 }
10107
10108 // A general selector. This is a Parser_expression for LEFT.NAME. It
10109 // is lowered after we know the type of the left hand side.
10110
10111 class Selector_expression : public Parser_expression
10112 {
10113 public:
10114 Selector_expression(Expression* left, const std::string& name,
10115 source_location location)
10116 : Parser_expression(EXPRESSION_SELECTOR, location),
10117 left_(left), name_(name)
10118 { }
10119
10120 protected:
10121 int
10122 do_traverse(Traverse* traverse)
10123 { return Expression::traverse(&this->left_, traverse); }
10124
10125 Expression*
10126 do_lower(Gogo*, Named_object*, int);
10127
10128 Expression*
10129 do_copy()
10130 {
10131 return new Selector_expression(this->left_->copy(), this->name_,
10132 this->location());
10133 }
10134
10135 private:
10136 Expression*
10137 lower_method_expression(Gogo*);
10138
10139 // The expression on the left hand side.
10140 Expression* left_;
10141 // The name on the right hand side.
10142 std::string name_;
10143 };
10144
10145 // Lower a selector expression once we know the real type of the left
10146 // hand side.
10147
10148 Expression*
10149 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
10150 {
10151 Expression* left = this->left_;
10152 if (left->is_type_expression())
10153 return this->lower_method_expression(gogo);
10154 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
10155 this->location());
10156 }
10157
10158 // Lower a method expression T.M or (*T).M. We turn this into a
10159 // function literal.
10160
10161 Expression*
10162 Selector_expression::lower_method_expression(Gogo* gogo)
10163 {
10164 source_location location = this->location();
10165 Type* type = this->left_->type();
10166 const std::string& name(this->name_);
10167
10168 bool is_pointer;
10169 if (type->points_to() == NULL)
10170 is_pointer = false;
10171 else
10172 {
10173 is_pointer = true;
10174 type = type->points_to();
10175 }
10176 Named_type* nt = type->named_type();
10177 if (nt == NULL)
10178 {
10179 error_at(location,
10180 ("method expression requires named type or "
10181 "pointer to named type"));
10182 return Expression::make_error(location);
10183 }
10184
10185 bool is_ambiguous;
10186 Method* method = nt->method_function(name, &is_ambiguous);
10187 if (method == NULL)
10188 {
10189 if (!is_ambiguous)
10190 error_at(location, "type %<%s%> has no method %<%s%>",
10191 nt->message_name().c_str(),
10192 Gogo::message_name(name).c_str());
10193 else
10194 error_at(location, "method %<%s%> is ambiguous in type %<%s%>",
10195 Gogo::message_name(name).c_str(),
10196 nt->message_name().c_str());
10197 return Expression::make_error(location);
10198 }
10199
10200 if (!is_pointer && !method->is_value_method())
10201 {
10202 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
10203 nt->message_name().c_str(),
10204 Gogo::message_name(name).c_str());
10205 return Expression::make_error(location);
10206 }
10207
10208 // Build a new function type in which the receiver becomes the first
10209 // argument.
10210 Function_type* method_type = method->type();
10211 gcc_assert(method_type->is_method());
10212
10213 const char* const receiver_name = "$this";
10214 Typed_identifier_list* parameters = new Typed_identifier_list();
10215 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
10216 location));
10217
10218 const Typed_identifier_list* method_parameters = method_type->parameters();
10219 if (method_parameters != NULL)
10220 {
10221 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10222 p != method_parameters->end();
10223 ++p)
10224 parameters->push_back(*p);
10225 }
10226
10227 const Typed_identifier_list* method_results = method_type->results();
10228 Typed_identifier_list* results;
10229 if (method_results == NULL)
10230 results = NULL;
10231 else
10232 {
10233 results = new Typed_identifier_list();
10234 for (Typed_identifier_list::const_iterator p = method_results->begin();
10235 p != method_results->end();
10236 ++p)
10237 results->push_back(*p);
10238 }
10239
10240 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
10241 location);
10242 if (method_type->is_varargs())
10243 fntype->set_is_varargs();
10244
10245 // We generate methods which always takes a pointer to the receiver
10246 // as their first argument. If this is for a pointer type, we can
10247 // simply reuse the existing function. We use an internal hack to
10248 // get the right type.
10249
10250 if (is_pointer)
10251 {
10252 Named_object* mno = (method->needs_stub_method()
10253 ? method->stub_object()
10254 : method->named_object());
10255 Expression* f = Expression::make_func_reference(mno, NULL, location);
10256 f = Expression::make_cast(fntype, f, location);
10257 Type_conversion_expression* tce =
10258 static_cast<Type_conversion_expression*>(f);
10259 tce->set_may_convert_function_types();
10260 return f;
10261 }
10262
10263 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
10264 location);
10265
10266 Named_object* vno = gogo->lookup(receiver_name, NULL);
10267 gcc_assert(vno != NULL);
10268 Expression* ve = Expression::make_var_reference(vno, location);
10269 Expression* bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
10270 gcc_assert(bm != NULL && !bm->is_error_expression());
10271
10272 Expression_list* args;
10273 if (method_parameters == NULL)
10274 args = NULL;
10275 else
10276 {
10277 args = new Expression_list();
10278 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10279 p != method_parameters->end();
10280 ++p)
10281 {
10282 vno = gogo->lookup(p->name(), NULL);
10283 gcc_assert(vno != NULL);
10284 args->push_back(Expression::make_var_reference(vno, location));
10285 }
10286 }
10287
10288 Call_expression* call = Expression::make_call(bm, args,
10289 method_type->is_varargs(),
10290 location);
10291
10292 size_t count = call->result_count();
10293 Statement* s;
10294 if (count == 0)
10295 s = Statement::make_statement(call);
10296 else
10297 {
10298 Expression_list* retvals = new Expression_list();
10299 if (count <= 1)
10300 retvals->push_back(call);
10301 else
10302 {
10303 for (size_t i = 0; i < count; ++i)
10304 retvals->push_back(Expression::make_call_result(call, i));
10305 }
10306 s = Statement::make_return_statement(no->func_value()->type()->results(),
10307 retvals, location);
10308 }
10309 gogo->add_statement(s);
10310
10311 gogo->finish_function(location);
10312
10313 return Expression::make_func_reference(no, NULL, location);
10314 }
10315
10316 // Make a selector expression.
10317
10318 Expression*
10319 Expression::make_selector(Expression* left, const std::string& name,
10320 source_location location)
10321 {
10322 return new Selector_expression(left, name, location);
10323 }
10324
10325 // Implement the builtin function new.
10326
10327 class Allocation_expression : public Expression
10328 {
10329 public:
10330 Allocation_expression(Type* type, source_location location)
10331 : Expression(EXPRESSION_ALLOCATION, location),
10332 type_(type)
10333 { }
10334
10335 protected:
10336 int
10337 do_traverse(Traverse* traverse)
10338 { return Type::traverse(this->type_, traverse); }
10339
10340 Type*
10341 do_type()
10342 { return Type::make_pointer_type(this->type_); }
10343
10344 void
10345 do_determine_type(const Type_context*)
10346 { }
10347
10348 void
10349 do_check_types(Gogo*);
10350
10351 Expression*
10352 do_copy()
10353 { return new Allocation_expression(this->type_, this->location()); }
10354
10355 tree
10356 do_get_tree(Translate_context*);
10357
10358 private:
10359 // The type we are allocating.
10360 Type* type_;
10361 };
10362
10363 // Check the type of an allocation expression.
10364
10365 void
10366 Allocation_expression::do_check_types(Gogo*)
10367 {
10368 if (this->type_->function_type() != NULL)
10369 this->report_error(_("invalid new of function type"));
10370 }
10371
10372 // Return a tree for an allocation expression.
10373
10374 tree
10375 Allocation_expression::do_get_tree(Translate_context* context)
10376 {
10377 tree type_tree = this->type_->get_tree(context->gogo());
10378 if (type_tree == error_mark_node)
10379 return error_mark_node;
10380 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10381 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10382 this->location());
10383 if (space == error_mark_node)
10384 return error_mark_node;
10385 return fold_convert(build_pointer_type(type_tree), space);
10386 }
10387
10388 // Make an allocation expression.
10389
10390 Expression*
10391 Expression::make_allocation(Type* type, source_location location)
10392 {
10393 return new Allocation_expression(type, location);
10394 }
10395
10396 // Implement the builtin function make.
10397
10398 class Make_expression : public Expression
10399 {
10400 public:
10401 Make_expression(Type* type, Expression_list* args, source_location location)
10402 : Expression(EXPRESSION_MAKE, location),
10403 type_(type), args_(args)
10404 { }
10405
10406 protected:
10407 int
10408 do_traverse(Traverse* traverse);
10409
10410 Type*
10411 do_type()
10412 { return this->type_; }
10413
10414 void
10415 do_determine_type(const Type_context*);
10416
10417 void
10418 do_check_types(Gogo*);
10419
10420 Expression*
10421 do_copy()
10422 {
10423 return new Make_expression(this->type_, this->args_->copy(),
10424 this->location());
10425 }
10426
10427 tree
10428 do_get_tree(Translate_context*);
10429
10430 private:
10431 // The type we are making.
10432 Type* type_;
10433 // The arguments to pass to the make routine.
10434 Expression_list* args_;
10435 };
10436
10437 // Traversal.
10438
10439 int
10440 Make_expression::do_traverse(Traverse* traverse)
10441 {
10442 if (this->args_ != NULL
10443 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10444 return TRAVERSE_EXIT;
10445 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10446 return TRAVERSE_EXIT;
10447 return TRAVERSE_CONTINUE;
10448 }
10449
10450 // Set types of arguments.
10451
10452 void
10453 Make_expression::do_determine_type(const Type_context*)
10454 {
10455 if (this->args_ != NULL)
10456 {
10457 Type_context context(Type::lookup_integer_type("int"), false);
10458 for (Expression_list::const_iterator pe = this->args_->begin();
10459 pe != this->args_->end();
10460 ++pe)
10461 (*pe)->determine_type(&context);
10462 }
10463 }
10464
10465 // Check types for a make expression.
10466
10467 void
10468 Make_expression::do_check_types(Gogo*)
10469 {
10470 if (this->type_->channel_type() == NULL
10471 && this->type_->map_type() == NULL
10472 && (this->type_->array_type() == NULL
10473 || this->type_->array_type()->length() != NULL))
10474 this->report_error(_("invalid type for make function"));
10475 else if (!this->type_->check_make_expression(this->args_, this->location()))
10476 this->set_is_error();
10477 }
10478
10479 // Return a tree for a make expression.
10480
10481 tree
10482 Make_expression::do_get_tree(Translate_context* context)
10483 {
10484 return this->type_->make_expression_tree(context, this->args_,
10485 this->location());
10486 }
10487
10488 // Make a make expression.
10489
10490 Expression*
10491 Expression::make_make(Type* type, Expression_list* args,
10492 source_location location)
10493 {
10494 return new Make_expression(type, args, location);
10495 }
10496
10497 // Construct a struct.
10498
10499 class Struct_construction_expression : public Expression
10500 {
10501 public:
10502 Struct_construction_expression(Type* type, Expression_list* vals,
10503 source_location location)
10504 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10505 type_(type), vals_(vals)
10506 { }
10507
10508 // Return whether this is a constant initializer.
10509 bool
10510 is_constant_struct() const;
10511
10512 protected:
10513 int
10514 do_traverse(Traverse* traverse);
10515
10516 Type*
10517 do_type()
10518 { return this->type_; }
10519
10520 void
10521 do_determine_type(const Type_context*);
10522
10523 void
10524 do_check_types(Gogo*);
10525
10526 Expression*
10527 do_copy()
10528 {
10529 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10530 this->location());
10531 }
10532
10533 bool
10534 do_is_addressable() const
10535 { return true; }
10536
10537 tree
10538 do_get_tree(Translate_context*);
10539
10540 void
10541 do_export(Export*) const;
10542
10543 private:
10544 // The type of the struct to construct.
10545 Type* type_;
10546 // The list of values, in order of the fields in the struct. A NULL
10547 // entry means that the field should be zero-initialized.
10548 Expression_list* vals_;
10549 };
10550
10551 // Traversal.
10552
10553 int
10554 Struct_construction_expression::do_traverse(Traverse* traverse)
10555 {
10556 if (this->vals_ != NULL
10557 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10558 return TRAVERSE_EXIT;
10559 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10560 return TRAVERSE_EXIT;
10561 return TRAVERSE_CONTINUE;
10562 }
10563
10564 // Return whether this is a constant initializer.
10565
10566 bool
10567 Struct_construction_expression::is_constant_struct() const
10568 {
10569 if (this->vals_ == NULL)
10570 return true;
10571 for (Expression_list::const_iterator pv = this->vals_->begin();
10572 pv != this->vals_->end();
10573 ++pv)
10574 {
10575 if (*pv != NULL
10576 && !(*pv)->is_constant()
10577 && (!(*pv)->is_composite_literal()
10578 || (*pv)->is_nonconstant_composite_literal()))
10579 return false;
10580 }
10581
10582 const Struct_field_list* fields = this->type_->struct_type()->fields();
10583 for (Struct_field_list::const_iterator pf = fields->begin();
10584 pf != fields->end();
10585 ++pf)
10586 {
10587 // There are no constant constructors for interfaces.
10588 if (pf->type()->interface_type() != NULL)
10589 return false;
10590 }
10591
10592 return true;
10593 }
10594
10595 // Final type determination.
10596
10597 void
10598 Struct_construction_expression::do_determine_type(const Type_context*)
10599 {
10600 if (this->vals_ == NULL)
10601 return;
10602 const Struct_field_list* fields = this->type_->struct_type()->fields();
10603 Expression_list::const_iterator pv = this->vals_->begin();
10604 for (Struct_field_list::const_iterator pf = fields->begin();
10605 pf != fields->end();
10606 ++pf, ++pv)
10607 {
10608 if (pv == this->vals_->end())
10609 return;
10610 if (*pv != NULL)
10611 {
10612 Type_context subcontext(pf->type(), false);
10613 (*pv)->determine_type(&subcontext);
10614 }
10615 }
10616 // Extra values are an error we will report elsewhere; we still want
10617 // to determine the type to avoid knockon errors.
10618 for (; pv != this->vals_->end(); ++pv)
10619 (*pv)->determine_type_no_context();
10620 }
10621
10622 // Check types.
10623
10624 void
10625 Struct_construction_expression::do_check_types(Gogo*)
10626 {
10627 if (this->vals_ == NULL)
10628 return;
10629
10630 Struct_type* st = this->type_->struct_type();
10631 if (this->vals_->size() > st->field_count())
10632 {
10633 this->report_error(_("too many expressions for struct"));
10634 return;
10635 }
10636
10637 const Struct_field_list* fields = st->fields();
10638 Expression_list::const_iterator pv = this->vals_->begin();
10639 int i = 0;
10640 for (Struct_field_list::const_iterator pf = fields->begin();
10641 pf != fields->end();
10642 ++pf, ++pv, ++i)
10643 {
10644 if (pv == this->vals_->end())
10645 {
10646 this->report_error(_("too few expressions for struct"));
10647 break;
10648 }
10649
10650 if (*pv == NULL)
10651 continue;
10652
10653 std::string reason;
10654 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10655 {
10656 if (reason.empty())
10657 error_at((*pv)->location(),
10658 "incompatible type for field %d in struct construction",
10659 i + 1);
10660 else
10661 error_at((*pv)->location(),
10662 ("incompatible type for field %d in "
10663 "struct construction (%s)"),
10664 i + 1, reason.c_str());
10665 this->set_is_error();
10666 }
10667 }
10668 gcc_assert(pv == this->vals_->end());
10669 }
10670
10671 // Return a tree for constructing a struct.
10672
10673 tree
10674 Struct_construction_expression::do_get_tree(Translate_context* context)
10675 {
10676 Gogo* gogo = context->gogo();
10677
10678 if (this->vals_ == NULL)
10679 return this->type_->get_init_tree(gogo, false);
10680
10681 tree type_tree = this->type_->get_tree(gogo);
10682 if (type_tree == error_mark_node)
10683 return error_mark_node;
10684 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10685
10686 bool is_constant = true;
10687 const Struct_field_list* fields = this->type_->struct_type()->fields();
10688 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10689 fields->size());
10690 Struct_field_list::const_iterator pf = fields->begin();
10691 Expression_list::const_iterator pv = this->vals_->begin();
10692 for (tree field = TYPE_FIELDS(type_tree);
10693 field != NULL_TREE;
10694 field = DECL_CHAIN(field), ++pf)
10695 {
10696 gcc_assert(pf != fields->end());
10697
10698 tree val;
10699 if (pv == this->vals_->end())
10700 val = pf->type()->get_init_tree(gogo, false);
10701 else if (*pv == NULL)
10702 {
10703 val = pf->type()->get_init_tree(gogo, false);
10704 ++pv;
10705 }
10706 else
10707 {
10708 val = Expression::convert_for_assignment(context, pf->type(),
10709 (*pv)->type(),
10710 (*pv)->get_tree(context),
10711 this->location());
10712 ++pv;
10713 }
10714
10715 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10716 return error_mark_node;
10717
10718 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10719 elt->index = field;
10720 elt->value = val;
10721 if (!TREE_CONSTANT(val))
10722 is_constant = false;
10723 }
10724 gcc_assert(pf == fields->end());
10725
10726 tree ret = build_constructor(type_tree, elts);
10727 if (is_constant)
10728 TREE_CONSTANT(ret) = 1;
10729 return ret;
10730 }
10731
10732 // Export a struct construction.
10733
10734 void
10735 Struct_construction_expression::do_export(Export* exp) const
10736 {
10737 exp->write_c_string("convert(");
10738 exp->write_type(this->type_);
10739 for (Expression_list::const_iterator pv = this->vals_->begin();
10740 pv != this->vals_->end();
10741 ++pv)
10742 {
10743 exp->write_c_string(", ");
10744 if (*pv != NULL)
10745 (*pv)->export_expression(exp);
10746 }
10747 exp->write_c_string(")");
10748 }
10749
10750 // Make a struct composite literal. This used by the thunk code.
10751
10752 Expression*
10753 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
10754 source_location location)
10755 {
10756 gcc_assert(type->struct_type() != NULL);
10757 return new Struct_construction_expression(type, vals, location);
10758 }
10759
10760 // Construct an array. This class is not used directly; instead we
10761 // use the child classes, Fixed_array_construction_expression and
10762 // Open_array_construction_expression.
10763
10764 class Array_construction_expression : public Expression
10765 {
10766 protected:
10767 Array_construction_expression(Expression_classification classification,
10768 Type* type, Expression_list* vals,
10769 source_location location)
10770 : Expression(classification, location),
10771 type_(type), vals_(vals)
10772 { }
10773
10774 public:
10775 // Return whether this is a constant initializer.
10776 bool
10777 is_constant_array() const;
10778
10779 // Return the number of elements.
10780 size_t
10781 element_count() const
10782 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
10783
10784 protected:
10785 int
10786 do_traverse(Traverse* traverse);
10787
10788 Type*
10789 do_type()
10790 { return this->type_; }
10791
10792 void
10793 do_determine_type(const Type_context*);
10794
10795 void
10796 do_check_types(Gogo*);
10797
10798 bool
10799 do_is_addressable() const
10800 { return true; }
10801
10802 void
10803 do_export(Export*) const;
10804
10805 // The list of values.
10806 Expression_list*
10807 vals()
10808 { return this->vals_; }
10809
10810 // Get a constructor tree for the array values.
10811 tree
10812 get_constructor_tree(Translate_context* context, tree type_tree);
10813
10814 private:
10815 // The type of the array to construct.
10816 Type* type_;
10817 // The list of values.
10818 Expression_list* vals_;
10819 };
10820
10821 // Traversal.
10822
10823 int
10824 Array_construction_expression::do_traverse(Traverse* traverse)
10825 {
10826 if (this->vals_ != NULL
10827 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10828 return TRAVERSE_EXIT;
10829 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10830 return TRAVERSE_EXIT;
10831 return TRAVERSE_CONTINUE;
10832 }
10833
10834 // Return whether this is a constant initializer.
10835
10836 bool
10837 Array_construction_expression::is_constant_array() const
10838 {
10839 if (this->vals_ == NULL)
10840 return true;
10841
10842 // There are no constant constructors for interfaces.
10843 if (this->type_->array_type()->element_type()->interface_type() != NULL)
10844 return false;
10845
10846 for (Expression_list::const_iterator pv = this->vals_->begin();
10847 pv != this->vals_->end();
10848 ++pv)
10849 {
10850 if (*pv != NULL
10851 && !(*pv)->is_constant()
10852 && (!(*pv)->is_composite_literal()
10853 || (*pv)->is_nonconstant_composite_literal()))
10854 return false;
10855 }
10856 return true;
10857 }
10858
10859 // Final type determination.
10860
10861 void
10862 Array_construction_expression::do_determine_type(const Type_context*)
10863 {
10864 if (this->vals_ == NULL)
10865 return;
10866 Type_context subcontext(this->type_->array_type()->element_type(), false);
10867 for (Expression_list::const_iterator pv = this->vals_->begin();
10868 pv != this->vals_->end();
10869 ++pv)
10870 {
10871 if (*pv != NULL)
10872 (*pv)->determine_type(&subcontext);
10873 }
10874 }
10875
10876 // Check types.
10877
10878 void
10879 Array_construction_expression::do_check_types(Gogo*)
10880 {
10881 if (this->vals_ == NULL)
10882 return;
10883
10884 Array_type* at = this->type_->array_type();
10885 int i = 0;
10886 Type* element_type = at->element_type();
10887 for (Expression_list::const_iterator pv = this->vals_->begin();
10888 pv != this->vals_->end();
10889 ++pv, ++i)
10890 {
10891 if (*pv != NULL
10892 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
10893 {
10894 error_at((*pv)->location(),
10895 "incompatible type for element %d in composite literal",
10896 i + 1);
10897 this->set_is_error();
10898 }
10899 }
10900
10901 Expression* length = at->length();
10902 if (length != NULL)
10903 {
10904 mpz_t val;
10905 mpz_init(val);
10906 Type* type;
10907 if (at->length()->integer_constant_value(true, val, &type))
10908 {
10909 if (this->vals_->size() > mpz_get_ui(val))
10910 this->report_error(_("too many elements in composite literal"));
10911 }
10912 mpz_clear(val);
10913 }
10914 }
10915
10916 // Get a constructor tree for the array values.
10917
10918 tree
10919 Array_construction_expression::get_constructor_tree(Translate_context* context,
10920 tree type_tree)
10921 {
10922 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
10923 (this->vals_ == NULL
10924 ? 0
10925 : this->vals_->size()));
10926 Type* element_type = this->type_->array_type()->element_type();
10927 bool is_constant = true;
10928 if (this->vals_ != NULL)
10929 {
10930 size_t i = 0;
10931 for (Expression_list::const_iterator pv = this->vals_->begin();
10932 pv != this->vals_->end();
10933 ++pv, ++i)
10934 {
10935 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
10936 elt->index = size_int(i);
10937 if (*pv == NULL)
10938 elt->value = element_type->get_init_tree(context->gogo(), false);
10939 else
10940 {
10941 tree value_tree = (*pv)->get_tree(context);
10942 elt->value = Expression::convert_for_assignment(context,
10943 element_type,
10944 (*pv)->type(),
10945 value_tree,
10946 this->location());
10947 }
10948 if (elt->value == error_mark_node)
10949 return error_mark_node;
10950 if (!TREE_CONSTANT(elt->value))
10951 is_constant = false;
10952 }
10953 }
10954
10955 tree ret = build_constructor(type_tree, values);
10956 if (is_constant)
10957 TREE_CONSTANT(ret) = 1;
10958 return ret;
10959 }
10960
10961 // Export an array construction.
10962
10963 void
10964 Array_construction_expression::do_export(Export* exp) const
10965 {
10966 exp->write_c_string("convert(");
10967 exp->write_type(this->type_);
10968 if (this->vals_ != NULL)
10969 {
10970 for (Expression_list::const_iterator pv = this->vals_->begin();
10971 pv != this->vals_->end();
10972 ++pv)
10973 {
10974 exp->write_c_string(", ");
10975 if (*pv != NULL)
10976 (*pv)->export_expression(exp);
10977 }
10978 }
10979 exp->write_c_string(")");
10980 }
10981
10982 // Construct a fixed array.
10983
10984 class Fixed_array_construction_expression :
10985 public Array_construction_expression
10986 {
10987 public:
10988 Fixed_array_construction_expression(Type* type, Expression_list* vals,
10989 source_location location)
10990 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
10991 type, vals, location)
10992 {
10993 gcc_assert(type->array_type() != NULL
10994 && type->array_type()->length() != NULL);
10995 }
10996
10997 protected:
10998 Expression*
10999 do_copy()
11000 {
11001 return new Fixed_array_construction_expression(this->type(),
11002 (this->vals() == NULL
11003 ? NULL
11004 : this->vals()->copy()),
11005 this->location());
11006 }
11007
11008 tree
11009 do_get_tree(Translate_context*);
11010 };
11011
11012 // Return a tree for constructing a fixed array.
11013
11014 tree
11015 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11016 {
11017 return this->get_constructor_tree(context,
11018 this->type()->get_tree(context->gogo()));
11019 }
11020
11021 // Construct an open array.
11022
11023 class Open_array_construction_expression : public Array_construction_expression
11024 {
11025 public:
11026 Open_array_construction_expression(Type* type, Expression_list* vals,
11027 source_location location)
11028 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
11029 type, vals, location)
11030 {
11031 gcc_assert(type->array_type() != NULL
11032 && type->array_type()->length() == NULL);
11033 }
11034
11035 protected:
11036 // Note that taking the address of an open array literal is invalid.
11037
11038 Expression*
11039 do_copy()
11040 {
11041 return new Open_array_construction_expression(this->type(),
11042 (this->vals() == NULL
11043 ? NULL
11044 : this->vals()->copy()),
11045 this->location());
11046 }
11047
11048 tree
11049 do_get_tree(Translate_context*);
11050 };
11051
11052 // Return a tree for constructing an open array.
11053
11054 tree
11055 Open_array_construction_expression::do_get_tree(Translate_context* context)
11056 {
11057 Array_type* array_type = this->type()->array_type();
11058 if (array_type == NULL)
11059 {
11060 gcc_assert(this->type()->is_error_type());
11061 return error_mark_node;
11062 }
11063
11064 Type* element_type = array_type->element_type();
11065 tree element_type_tree = element_type->get_tree(context->gogo());
11066 if (element_type_tree == error_mark_node)
11067 return error_mark_node;
11068
11069 tree values;
11070 tree length_tree;
11071 if (this->vals() == NULL || this->vals()->empty())
11072 {
11073 // We need to create a unique value.
11074 tree max = size_int(0);
11075 tree constructor_type = build_array_type(element_type_tree,
11076 build_index_type(max));
11077 if (constructor_type == error_mark_node)
11078 return error_mark_node;
11079 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
11080 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
11081 elt->index = size_int(0);
11082 elt->value = element_type->get_init_tree(context->gogo(), false);
11083 values = build_constructor(constructor_type, vec);
11084 if (TREE_CONSTANT(elt->value))
11085 TREE_CONSTANT(values) = 1;
11086 length_tree = size_int(0);
11087 }
11088 else
11089 {
11090 tree max = size_int(this->vals()->size() - 1);
11091 tree constructor_type = build_array_type(element_type_tree,
11092 build_index_type(max));
11093 if (constructor_type == error_mark_node)
11094 return error_mark_node;
11095 values = this->get_constructor_tree(context, constructor_type);
11096 length_tree = size_int(this->vals()->size());
11097 }
11098
11099 if (values == error_mark_node)
11100 return error_mark_node;
11101
11102 bool is_constant_initializer = TREE_CONSTANT(values);
11103 bool is_in_function = context->function() != NULL;
11104
11105 if (is_constant_initializer)
11106 {
11107 tree tmp = build_decl(this->location(), VAR_DECL,
11108 create_tmp_var_name("C"), TREE_TYPE(values));
11109 DECL_EXTERNAL(tmp) = 0;
11110 TREE_PUBLIC(tmp) = 0;
11111 TREE_STATIC(tmp) = 1;
11112 DECL_ARTIFICIAL(tmp) = 1;
11113 if (is_in_function)
11114 {
11115 // If this is not a function, we will only initialize the
11116 // value once, so we can use this directly rather than
11117 // copying it. In that case we can't make it read-only,
11118 // because the program is permitted to change it.
11119 TREE_READONLY(tmp) = 1;
11120 TREE_CONSTANT(tmp) = 1;
11121 }
11122 DECL_INITIAL(tmp) = values;
11123 rest_of_decl_compilation(tmp, 1, 0);
11124 values = tmp;
11125 }
11126
11127 tree space;
11128 tree set;
11129 if (!is_in_function && is_constant_initializer)
11130 {
11131 // Outside of a function, we know the initializer will only run
11132 // once.
11133 space = build_fold_addr_expr(values);
11134 set = NULL_TREE;
11135 }
11136 else
11137 {
11138 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
11139 space = context->gogo()->allocate_memory(element_type, memsize,
11140 this->location());
11141 space = save_expr(space);
11142
11143 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
11144 tree ref = build_fold_indirect_ref_loc(this->location(), s);
11145 TREE_THIS_NOTRAP(ref) = 1;
11146 set = build2(MODIFY_EXPR, void_type_node, ref, values);
11147 }
11148
11149 // Build a constructor for the open array.
11150
11151 tree type_tree = this->type()->get_tree(context->gogo());
11152 if (type_tree == error_mark_node)
11153 return error_mark_node;
11154 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11155
11156 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
11157
11158 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
11159 tree field = TYPE_FIELDS(type_tree);
11160 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
11161 elt->index = field;
11162 elt->value = fold_convert(TREE_TYPE(field), space);
11163
11164 elt = VEC_quick_push(constructor_elt, init, NULL);
11165 field = DECL_CHAIN(field);
11166 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
11167 elt->index = field;
11168 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11169
11170 elt = VEC_quick_push(constructor_elt, init, NULL);
11171 field = DECL_CHAIN(field);
11172 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
11173 elt->index = field;
11174 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11175
11176 tree constructor = build_constructor(type_tree, init);
11177 if (constructor == error_mark_node)
11178 return error_mark_node;
11179 if (!is_in_function && is_constant_initializer)
11180 TREE_CONSTANT(constructor) = 1;
11181
11182 if (set == NULL_TREE)
11183 return constructor;
11184 else
11185 return build2(COMPOUND_EXPR, type_tree, set, constructor);
11186 }
11187
11188 // Make a slice composite literal. This is used by the type
11189 // descriptor code.
11190
11191 Expression*
11192 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
11193 source_location location)
11194 {
11195 gcc_assert(type->is_open_array_type());
11196 return new Open_array_construction_expression(type, vals, location);
11197 }
11198
11199 // Construct a map.
11200
11201 class Map_construction_expression : public Expression
11202 {
11203 public:
11204 Map_construction_expression(Type* type, Expression_list* vals,
11205 source_location location)
11206 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
11207 type_(type), vals_(vals)
11208 { gcc_assert(vals == NULL || vals->size() % 2 == 0); }
11209
11210 protected:
11211 int
11212 do_traverse(Traverse* traverse);
11213
11214 Type*
11215 do_type()
11216 { return this->type_; }
11217
11218 void
11219 do_determine_type(const Type_context*);
11220
11221 void
11222 do_check_types(Gogo*);
11223
11224 Expression*
11225 do_copy()
11226 {
11227 return new Map_construction_expression(this->type_, this->vals_->copy(),
11228 this->location());
11229 }
11230
11231 tree
11232 do_get_tree(Translate_context*);
11233
11234 void
11235 do_export(Export*) const;
11236
11237 private:
11238 // The type of the map to construct.
11239 Type* type_;
11240 // The list of values.
11241 Expression_list* vals_;
11242 };
11243
11244 // Traversal.
11245
11246 int
11247 Map_construction_expression::do_traverse(Traverse* traverse)
11248 {
11249 if (this->vals_ != NULL
11250 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11251 return TRAVERSE_EXIT;
11252 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11253 return TRAVERSE_EXIT;
11254 return TRAVERSE_CONTINUE;
11255 }
11256
11257 // Final type determination.
11258
11259 void
11260 Map_construction_expression::do_determine_type(const Type_context*)
11261 {
11262 if (this->vals_ == NULL)
11263 return;
11264
11265 Map_type* mt = this->type_->map_type();
11266 Type_context key_context(mt->key_type(), false);
11267 Type_context val_context(mt->val_type(), false);
11268 for (Expression_list::const_iterator pv = this->vals_->begin();
11269 pv != this->vals_->end();
11270 ++pv)
11271 {
11272 (*pv)->determine_type(&key_context);
11273 ++pv;
11274 (*pv)->determine_type(&val_context);
11275 }
11276 }
11277
11278 // Check types.
11279
11280 void
11281 Map_construction_expression::do_check_types(Gogo*)
11282 {
11283 if (this->vals_ == NULL)
11284 return;
11285
11286 Map_type* mt = this->type_->map_type();
11287 int i = 0;
11288 Type* key_type = mt->key_type();
11289 Type* val_type = mt->val_type();
11290 for (Expression_list::const_iterator pv = this->vals_->begin();
11291 pv != this->vals_->end();
11292 ++pv, ++i)
11293 {
11294 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
11295 {
11296 error_at((*pv)->location(),
11297 "incompatible type for element %d key in map construction",
11298 i + 1);
11299 this->set_is_error();
11300 }
11301 ++pv;
11302 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
11303 {
11304 error_at((*pv)->location(),
11305 ("incompatible type for element %d value "
11306 "in map construction"),
11307 i + 1);
11308 this->set_is_error();
11309 }
11310 }
11311 }
11312
11313 // Return a tree for constructing a map.
11314
11315 tree
11316 Map_construction_expression::do_get_tree(Translate_context* context)
11317 {
11318 Gogo* gogo = context->gogo();
11319 source_location loc = this->location();
11320
11321 Map_type* mt = this->type_->map_type();
11322
11323 // Build a struct to hold the key and value.
11324 tree struct_type = make_node(RECORD_TYPE);
11325
11326 Type* key_type = mt->key_type();
11327 tree id = get_identifier("__key");
11328 tree key_type_tree = key_type->get_tree(gogo);
11329 if (key_type_tree == error_mark_node)
11330 return error_mark_node;
11331 tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree);
11332 DECL_CONTEXT(key_field) = struct_type;
11333 TYPE_FIELDS(struct_type) = key_field;
11334
11335 Type* val_type = mt->val_type();
11336 id = get_identifier("__val");
11337 tree val_type_tree = val_type->get_tree(gogo);
11338 if (val_type_tree == error_mark_node)
11339 return error_mark_node;
11340 tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree);
11341 DECL_CONTEXT(val_field) = struct_type;
11342 DECL_CHAIN(key_field) = val_field;
11343
11344 layout_type(struct_type);
11345
11346 bool is_constant = true;
11347 size_t i = 0;
11348 tree valaddr;
11349 tree make_tmp;
11350
11351 if (this->vals_ == NULL || this->vals_->empty())
11352 {
11353 valaddr = null_pointer_node;
11354 make_tmp = NULL_TREE;
11355 }
11356 else
11357 {
11358 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11359 this->vals_->size() / 2);
11360
11361 for (Expression_list::const_iterator pv = this->vals_->begin();
11362 pv != this->vals_->end();
11363 ++pv, ++i)
11364 {
11365 bool one_is_constant = true;
11366
11367 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
11368
11369 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11370 elt->index = key_field;
11371 tree val_tree = (*pv)->get_tree(context);
11372 elt->value = Expression::convert_for_assignment(context, key_type,
11373 (*pv)->type(),
11374 val_tree, loc);
11375 if (elt->value == error_mark_node)
11376 return error_mark_node;
11377 if (!TREE_CONSTANT(elt->value))
11378 one_is_constant = false;
11379
11380 ++pv;
11381
11382 elt = VEC_quick_push(constructor_elt, one, NULL);
11383 elt->index = val_field;
11384 val_tree = (*pv)->get_tree(context);
11385 elt->value = Expression::convert_for_assignment(context, val_type,
11386 (*pv)->type(),
11387 val_tree, loc);
11388 if (elt->value == error_mark_node)
11389 return error_mark_node;
11390 if (!TREE_CONSTANT(elt->value))
11391 one_is_constant = false;
11392
11393 elt = VEC_quick_push(constructor_elt, values, NULL);
11394 elt->index = size_int(i);
11395 elt->value = build_constructor(struct_type, one);
11396 if (one_is_constant)
11397 TREE_CONSTANT(elt->value) = 1;
11398 else
11399 is_constant = false;
11400 }
11401
11402 tree index_type = build_index_type(size_int(i - 1));
11403 tree array_type = build_array_type(struct_type, index_type);
11404 tree init = build_constructor(array_type, values);
11405 if (is_constant)
11406 TREE_CONSTANT(init) = 1;
11407 tree tmp;
11408 if (current_function_decl != NULL)
11409 {
11410 tmp = create_tmp_var(array_type, get_name(array_type));
11411 DECL_INITIAL(tmp) = init;
11412 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11413 TREE_ADDRESSABLE(tmp) = 1;
11414 }
11415 else
11416 {
11417 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11418 DECL_EXTERNAL(tmp) = 0;
11419 TREE_PUBLIC(tmp) = 0;
11420 TREE_STATIC(tmp) = 1;
11421 DECL_ARTIFICIAL(tmp) = 1;
11422 if (!TREE_CONSTANT(init))
11423 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11424 init);
11425 else
11426 {
11427 TREE_READONLY(tmp) = 1;
11428 TREE_CONSTANT(tmp) = 1;
11429 DECL_INITIAL(tmp) = init;
11430 make_tmp = NULL_TREE;
11431 }
11432 rest_of_decl_compilation(tmp, 1, 0);
11433 }
11434
11435 valaddr = build_fold_addr_expr(tmp);
11436 }
11437
11438 tree descriptor = gogo->map_descriptor(mt);
11439
11440 tree type_tree = this->type_->get_tree(gogo);
11441 if (type_tree == error_mark_node)
11442 return error_mark_node;
11443
11444 static tree construct_map_fndecl;
11445 tree call = Gogo::call_builtin(&construct_map_fndecl,
11446 loc,
11447 "__go_construct_map",
11448 6,
11449 type_tree,
11450 TREE_TYPE(descriptor),
11451 descriptor,
11452 sizetype,
11453 size_int(i),
11454 sizetype,
11455 TYPE_SIZE_UNIT(struct_type),
11456 sizetype,
11457 byte_position(val_field),
11458 sizetype,
11459 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11460 const_ptr_type_node,
11461 fold_convert(const_ptr_type_node, valaddr));
11462 if (call == error_mark_node)
11463 return error_mark_node;
11464
11465 tree ret;
11466 if (make_tmp == NULL)
11467 ret = call;
11468 else
11469 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11470 return ret;
11471 }
11472
11473 // Export an array construction.
11474
11475 void
11476 Map_construction_expression::do_export(Export* exp) const
11477 {
11478 exp->write_c_string("convert(");
11479 exp->write_type(this->type_);
11480 for (Expression_list::const_iterator pv = this->vals_->begin();
11481 pv != this->vals_->end();
11482 ++pv)
11483 {
11484 exp->write_c_string(", ");
11485 (*pv)->export_expression(exp);
11486 }
11487 exp->write_c_string(")");
11488 }
11489
11490 // A general composite literal. This is lowered to a type specific
11491 // version.
11492
11493 class Composite_literal_expression : public Parser_expression
11494 {
11495 public:
11496 Composite_literal_expression(Type* type, int depth, bool has_keys,
11497 Expression_list* vals, source_location location)
11498 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11499 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11500 { }
11501
11502 protected:
11503 int
11504 do_traverse(Traverse* traverse);
11505
11506 Expression*
11507 do_lower(Gogo*, Named_object*, int);
11508
11509 Expression*
11510 do_copy()
11511 {
11512 return new Composite_literal_expression(this->type_, this->depth_,
11513 this->has_keys_,
11514 (this->vals_ == NULL
11515 ? NULL
11516 : this->vals_->copy()),
11517 this->location());
11518 }
11519
11520 private:
11521 Expression*
11522 lower_struct(Type*);
11523
11524 Expression*
11525 lower_array(Type*);
11526
11527 Expression*
11528 make_array(Type*, Expression_list*);
11529
11530 Expression*
11531 lower_map(Gogo*, Named_object*, Type*);
11532
11533 // The type of the composite literal.
11534 Type* type_;
11535 // The depth within a list of composite literals within a composite
11536 // literal, when the type is omitted.
11537 int depth_;
11538 // The values to put in the composite literal.
11539 Expression_list* vals_;
11540 // If this is true, then VALS_ is a list of pairs: a key and a
11541 // value. In an array initializer, a missing key will be NULL.
11542 bool has_keys_;
11543 };
11544
11545 // Traversal.
11546
11547 int
11548 Composite_literal_expression::do_traverse(Traverse* traverse)
11549 {
11550 if (this->vals_ != NULL
11551 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11552 return TRAVERSE_EXIT;
11553 return Type::traverse(this->type_, traverse);
11554 }
11555
11556 // Lower a generic composite literal into a specific version based on
11557 // the type.
11558
11559 Expression*
11560 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int)
11561 {
11562 Type* type = this->type_;
11563
11564 for (int depth = this->depth_; depth > 0; --depth)
11565 {
11566 if (type->array_type() != NULL)
11567 type = type->array_type()->element_type();
11568 else if (type->map_type() != NULL)
11569 type = type->map_type()->val_type();
11570 else
11571 {
11572 if (!type->is_error_type())
11573 error_at(this->location(),
11574 ("may only omit types within composite literals "
11575 "of slice, array, or map type"));
11576 return Expression::make_error(this->location());
11577 }
11578 }
11579
11580 if (type->is_error_type())
11581 return Expression::make_error(this->location());
11582 else if (type->struct_type() != NULL)
11583 return this->lower_struct(type);
11584 else if (type->array_type() != NULL)
11585 return this->lower_array(type);
11586 else if (type->map_type() != NULL)
11587 return this->lower_map(gogo, function, type);
11588 else
11589 {
11590 error_at(this->location(),
11591 ("expected struct, slice, array, or map type "
11592 "for composite literal"));
11593 return Expression::make_error(this->location());
11594 }
11595 }
11596
11597 // Lower a struct composite literal.
11598
11599 Expression*
11600 Composite_literal_expression::lower_struct(Type* type)
11601 {
11602 source_location location = this->location();
11603 Struct_type* st = type->struct_type();
11604 if (this->vals_ == NULL || !this->has_keys_)
11605 return new Struct_construction_expression(type, this->vals_, location);
11606
11607 size_t field_count = st->field_count();
11608 std::vector<Expression*> vals(field_count);
11609 Expression_list::const_iterator p = this->vals_->begin();
11610 while (p != this->vals_->end())
11611 {
11612 Expression* name_expr = *p;
11613
11614 ++p;
11615 gcc_assert(p != this->vals_->end());
11616 Expression* val = *p;
11617
11618 ++p;
11619
11620 if (name_expr == NULL)
11621 {
11622 error_at(val->location(), "mixture of field and value initializers");
11623 return Expression::make_error(location);
11624 }
11625
11626 bool bad_key = false;
11627 std::string name;
11628 switch (name_expr->classification())
11629 {
11630 case EXPRESSION_UNKNOWN_REFERENCE:
11631 name = name_expr->unknown_expression()->name();
11632 break;
11633
11634 case EXPRESSION_CONST_REFERENCE:
11635 name = static_cast<Const_expression*>(name_expr)->name();
11636 break;
11637
11638 case EXPRESSION_TYPE:
11639 {
11640 Type* t = name_expr->type();
11641 Named_type* nt = t->named_type();
11642 if (nt == NULL)
11643 bad_key = true;
11644 else
11645 name = nt->name();
11646 }
11647 break;
11648
11649 case EXPRESSION_VAR_REFERENCE:
11650 name = name_expr->var_expression()->name();
11651 break;
11652
11653 case EXPRESSION_FUNC_REFERENCE:
11654 name = name_expr->func_expression()->name();
11655 break;
11656
11657 case EXPRESSION_UNARY:
11658 // If there is a local variable around with the same name as
11659 // the field, and this occurs in the closure, then the
11660 // parser may turn the field reference into an indirection
11661 // through the closure. FIXME: This is a mess.
11662 {
11663 bad_key = true;
11664 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11665 if (ue->op() == OPERATOR_MULT)
11666 {
11667 Field_reference_expression* fre =
11668 ue->operand()->field_reference_expression();
11669 if (fre != NULL)
11670 {
11671 Struct_type* st =
11672 fre->expr()->type()->deref()->struct_type();
11673 if (st != NULL)
11674 {
11675 const Struct_field* sf = st->field(fre->field_index());
11676 name = sf->field_name();
11677 char buf[20];
11678 snprintf(buf, sizeof buf, "%u", fre->field_index());
11679 size_t buflen = strlen(buf);
11680 if (name.compare(name.length() - buflen, buflen, buf)
11681 == 0)
11682 {
11683 name = name.substr(0, name.length() - buflen);
11684 bad_key = false;
11685 }
11686 }
11687 }
11688 }
11689 }
11690 break;
11691
11692 default:
11693 bad_key = true;
11694 break;
11695 }
11696 if (bad_key)
11697 {
11698 error_at(name_expr->location(), "expected struct field name");
11699 return Expression::make_error(location);
11700 }
11701
11702 unsigned int index;
11703 const Struct_field* sf = st->find_local_field(name, &index);
11704 if (sf == NULL)
11705 {
11706 error_at(name_expr->location(), "unknown field %qs in %qs",
11707 Gogo::message_name(name).c_str(),
11708 (type->named_type() != NULL
11709 ? type->named_type()->message_name().c_str()
11710 : "unnamed struct"));
11711 return Expression::make_error(location);
11712 }
11713 if (vals[index] != NULL)
11714 {
11715 error_at(name_expr->location(),
11716 "duplicate value for field %qs in %qs",
11717 Gogo::message_name(name).c_str(),
11718 (type->named_type() != NULL
11719 ? type->named_type()->message_name().c_str()
11720 : "unnamed struct"));
11721 return Expression::make_error(location);
11722 }
11723
11724 vals[index] = val;
11725 }
11726
11727 Expression_list* list = new Expression_list;
11728 list->reserve(field_count);
11729 for (size_t i = 0; i < field_count; ++i)
11730 list->push_back(vals[i]);
11731
11732 return new Struct_construction_expression(type, list, location);
11733 }
11734
11735 // Lower an array composite literal.
11736
11737 Expression*
11738 Composite_literal_expression::lower_array(Type* type)
11739 {
11740 source_location location = this->location();
11741 if (this->vals_ == NULL || !this->has_keys_)
11742 return this->make_array(type, this->vals_);
11743
11744 std::vector<Expression*> vals;
11745 vals.reserve(this->vals_->size());
11746 unsigned long index = 0;
11747 Expression_list::const_iterator p = this->vals_->begin();
11748 while (p != this->vals_->end())
11749 {
11750 Expression* index_expr = *p;
11751
11752 ++p;
11753 gcc_assert(p != this->vals_->end());
11754 Expression* val = *p;
11755
11756 ++p;
11757
11758 if (index_expr != NULL)
11759 {
11760 mpz_t ival;
11761 mpz_init(ival);
11762 Type* dummy;
11763 if (!index_expr->integer_constant_value(true, ival, &dummy))
11764 {
11765 mpz_clear(ival);
11766 error_at(index_expr->location(),
11767 "index expression is not integer constant");
11768 return Expression::make_error(location);
11769 }
11770 if (mpz_sgn(ival) < 0)
11771 {
11772 mpz_clear(ival);
11773 error_at(index_expr->location(), "index expression is negative");
11774 return Expression::make_error(location);
11775 }
11776 index = mpz_get_ui(ival);
11777 if (mpz_cmp_ui(ival, index) != 0)
11778 {
11779 mpz_clear(ival);
11780 error_at(index_expr->location(), "index value overflow");
11781 return Expression::make_error(location);
11782 }
11783 mpz_clear(ival);
11784 }
11785
11786 if (index == vals.size())
11787 vals.push_back(val);
11788 else
11789 {
11790 if (index > vals.size())
11791 {
11792 vals.reserve(index + 32);
11793 vals.resize(index + 1, static_cast<Expression*>(NULL));
11794 }
11795 if (vals[index] != NULL)
11796 {
11797 error_at((index_expr != NULL
11798 ? index_expr->location()
11799 : val->location()),
11800 "duplicate value for index %lu",
11801 index);
11802 return Expression::make_error(location);
11803 }
11804 vals[index] = val;
11805 }
11806
11807 ++index;
11808 }
11809
11810 size_t size = vals.size();
11811 Expression_list* list = new Expression_list;
11812 list->reserve(size);
11813 for (size_t i = 0; i < size; ++i)
11814 list->push_back(vals[i]);
11815
11816 return this->make_array(type, list);
11817 }
11818
11819 // Actually build the array composite literal. This handles
11820 // [...]{...}.
11821
11822 Expression*
11823 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
11824 {
11825 source_location location = this->location();
11826 Array_type* at = type->array_type();
11827 if (at->length() != NULL && at->length()->is_nil_expression())
11828 {
11829 size_t size = vals == NULL ? 0 : vals->size();
11830 mpz_t vlen;
11831 mpz_init_set_ui(vlen, size);
11832 Expression* elen = Expression::make_integer(&vlen, NULL, location);
11833 mpz_clear(vlen);
11834 at = Type::make_array_type(at->element_type(), elen);
11835 type = at;
11836 }
11837 if (at->length() != NULL)
11838 return new Fixed_array_construction_expression(type, vals, location);
11839 else
11840 return new Open_array_construction_expression(type, vals, location);
11841 }
11842
11843 // Lower a map composite literal.
11844
11845 Expression*
11846 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
11847 Type* type)
11848 {
11849 source_location location = this->location();
11850 if (this->vals_ != NULL)
11851 {
11852 if (!this->has_keys_)
11853 {
11854 error_at(location, "map composite literal must have keys");
11855 return Expression::make_error(location);
11856 }
11857
11858 for (Expression_list::iterator p = this->vals_->begin();
11859 p != this->vals_->end();
11860 p += 2)
11861 {
11862 if (*p == NULL)
11863 {
11864 ++p;
11865 error_at((*p)->location(),
11866 "map composite literal must have keys for every value");
11867 return Expression::make_error(location);
11868 }
11869 // Make sure we have lowered the key; it may not have been
11870 // lowered in order to handle keys for struct composite
11871 // literals. Lower it now to get the right error message.
11872 if ((*p)->unknown_expression() != NULL)
11873 {
11874 (*p)->unknown_expression()->clear_is_composite_literal_key();
11875 gogo->lower_expression(function, &*p);
11876 gcc_assert((*p)->is_error_expression());
11877 return Expression::make_error(location);
11878 }
11879 }
11880 }
11881
11882 return new Map_construction_expression(type, this->vals_, location);
11883 }
11884
11885 // Make a composite literal expression.
11886
11887 Expression*
11888 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
11889 Expression_list* vals,
11890 source_location location)
11891 {
11892 return new Composite_literal_expression(type, depth, has_keys, vals,
11893 location);
11894 }
11895
11896 // Return whether this expression is a composite literal.
11897
11898 bool
11899 Expression::is_composite_literal() const
11900 {
11901 switch (this->classification_)
11902 {
11903 case EXPRESSION_COMPOSITE_LITERAL:
11904 case EXPRESSION_STRUCT_CONSTRUCTION:
11905 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
11906 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
11907 case EXPRESSION_MAP_CONSTRUCTION:
11908 return true;
11909 default:
11910 return false;
11911 }
11912 }
11913
11914 // Return whether this expression is a composite literal which is not
11915 // constant.
11916
11917 bool
11918 Expression::is_nonconstant_composite_literal() const
11919 {
11920 switch (this->classification_)
11921 {
11922 case EXPRESSION_STRUCT_CONSTRUCTION:
11923 {
11924 const Struct_construction_expression *psce =
11925 static_cast<const Struct_construction_expression*>(this);
11926 return !psce->is_constant_struct();
11927 }
11928 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
11929 {
11930 const Fixed_array_construction_expression *pace =
11931 static_cast<const Fixed_array_construction_expression*>(this);
11932 return !pace->is_constant_array();
11933 }
11934 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
11935 {
11936 const Open_array_construction_expression *pace =
11937 static_cast<const Open_array_construction_expression*>(this);
11938 return !pace->is_constant_array();
11939 }
11940 case EXPRESSION_MAP_CONSTRUCTION:
11941 return true;
11942 default:
11943 return false;
11944 }
11945 }
11946
11947 // Return true if this is a reference to a local variable.
11948
11949 bool
11950 Expression::is_local_variable() const
11951 {
11952 const Var_expression* ve = this->var_expression();
11953 if (ve == NULL)
11954 return false;
11955 const Named_object* no = ve->named_object();
11956 return (no->is_result_variable()
11957 || (no->is_variable() && !no->var_value()->is_global()));
11958 }
11959
11960 // Class Type_guard_expression.
11961
11962 // Traversal.
11963
11964 int
11965 Type_guard_expression::do_traverse(Traverse* traverse)
11966 {
11967 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
11968 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11969 return TRAVERSE_EXIT;
11970 return TRAVERSE_CONTINUE;
11971 }
11972
11973 // Check types of a type guard expression. The expression must have
11974 // an interface type, but the actual type conversion is checked at run
11975 // time.
11976
11977 void
11978 Type_guard_expression::do_check_types(Gogo*)
11979 {
11980 // 6g permits using a type guard with unsafe.pointer; we are
11981 // compatible.
11982 Type* expr_type = this->expr_->type();
11983 if (expr_type->is_unsafe_pointer_type())
11984 {
11985 if (this->type_->points_to() == NULL
11986 && (this->type_->integer_type() == NULL
11987 || (this->type_->forwarded()
11988 != Type::lookup_integer_type("uintptr"))))
11989 this->report_error(_("invalid unsafe.Pointer conversion"));
11990 }
11991 else if (this->type_->is_unsafe_pointer_type())
11992 {
11993 if (expr_type->points_to() == NULL
11994 && (expr_type->integer_type() == NULL
11995 || (expr_type->forwarded()
11996 != Type::lookup_integer_type("uintptr"))))
11997 this->report_error(_("invalid unsafe.Pointer conversion"));
11998 }
11999 else if (expr_type->interface_type() == NULL)
12000 {
12001 if (!expr_type->is_error_type() && !this->type_->is_error_type())
12002 this->report_error(_("type assertion only valid for interface types"));
12003 this->set_is_error();
12004 }
12005 else if (this->type_->interface_type() == NULL)
12006 {
12007 std::string reason;
12008 if (!expr_type->interface_type()->implements_interface(this->type_,
12009 &reason))
12010 {
12011 if (!this->type_->is_error_type())
12012 {
12013 if (reason.empty())
12014 this->report_error(_("impossible type assertion: "
12015 "type does not implement interface"));
12016 else
12017 error_at(this->location(),
12018 ("impossible type assertion: "
12019 "type does not implement interface (%s)"),
12020 reason.c_str());
12021 }
12022 this->set_is_error();
12023 }
12024 }
12025 }
12026
12027 // Return a tree for a type guard expression.
12028
12029 tree
12030 Type_guard_expression::do_get_tree(Translate_context* context)
12031 {
12032 Gogo* gogo = context->gogo();
12033 tree expr_tree = this->expr_->get_tree(context);
12034 if (expr_tree == error_mark_node)
12035 return error_mark_node;
12036 Type* expr_type = this->expr_->type();
12037 if ((this->type_->is_unsafe_pointer_type()
12038 && (expr_type->points_to() != NULL
12039 || expr_type->integer_type() != NULL))
12040 || (expr_type->is_unsafe_pointer_type()
12041 && this->type_->points_to() != NULL))
12042 return convert_to_pointer(this->type_->get_tree(gogo), expr_tree);
12043 else if (expr_type->is_unsafe_pointer_type()
12044 && this->type_->integer_type() != NULL)
12045 return convert_to_integer(this->type_->get_tree(gogo), expr_tree);
12046 else if (this->type_->interface_type() != NULL)
12047 return Expression::convert_interface_to_interface(context, this->type_,
12048 this->expr_->type(),
12049 expr_tree, true,
12050 this->location());
12051 else
12052 return Expression::convert_for_assignment(context, this->type_,
12053 this->expr_->type(), expr_tree,
12054 this->location());
12055 }
12056
12057 // Make a type guard expression.
12058
12059 Expression*
12060 Expression::make_type_guard(Expression* expr, Type* type,
12061 source_location location)
12062 {
12063 return new Type_guard_expression(expr, type, location);
12064 }
12065
12066 // Class Heap_composite_expression.
12067
12068 // When you take the address of a composite literal, it is allocated
12069 // on the heap. This class implements that.
12070
12071 class Heap_composite_expression : public Expression
12072 {
12073 public:
12074 Heap_composite_expression(Expression* expr, source_location location)
12075 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
12076 expr_(expr)
12077 { }
12078
12079 protected:
12080 int
12081 do_traverse(Traverse* traverse)
12082 { return Expression::traverse(&this->expr_, traverse); }
12083
12084 Type*
12085 do_type()
12086 { return Type::make_pointer_type(this->expr_->type()); }
12087
12088 void
12089 do_determine_type(const Type_context*)
12090 { this->expr_->determine_type_no_context(); }
12091
12092 Expression*
12093 do_copy()
12094 {
12095 return Expression::make_heap_composite(this->expr_->copy(),
12096 this->location());
12097 }
12098
12099 tree
12100 do_get_tree(Translate_context*);
12101
12102 // We only export global objects, and the parser does not generate
12103 // this in global scope.
12104 void
12105 do_export(Export*) const
12106 { gcc_unreachable(); }
12107
12108 private:
12109 // The composite literal which is being put on the heap.
12110 Expression* expr_;
12111 };
12112
12113 // Return a tree which allocates a composite literal on the heap.
12114
12115 tree
12116 Heap_composite_expression::do_get_tree(Translate_context* context)
12117 {
12118 tree expr_tree = this->expr_->get_tree(context);
12119 if (expr_tree == error_mark_node)
12120 return error_mark_node;
12121 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
12122 gcc_assert(TREE_CODE(expr_size) == INTEGER_CST);
12123 tree space = context->gogo()->allocate_memory(this->expr_->type(),
12124 expr_size, this->location());
12125 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
12126 space = save_expr(space);
12127 tree ref = build_fold_indirect_ref_loc(this->location(), space);
12128 TREE_THIS_NOTRAP(ref) = 1;
12129 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
12130 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
12131 space);
12132 SET_EXPR_LOCATION(ret, this->location());
12133 return ret;
12134 }
12135
12136 // Allocate a composite literal on the heap.
12137
12138 Expression*
12139 Expression::make_heap_composite(Expression* expr, source_location location)
12140 {
12141 return new Heap_composite_expression(expr, location);
12142 }
12143
12144 // Class Receive_expression.
12145
12146 // Return the type of a receive expression.
12147
12148 Type*
12149 Receive_expression::do_type()
12150 {
12151 Channel_type* channel_type = this->channel_->type()->channel_type();
12152 if (channel_type == NULL)
12153 return Type::make_error_type();
12154 return channel_type->element_type();
12155 }
12156
12157 // Check types for a receive expression.
12158
12159 void
12160 Receive_expression::do_check_types(Gogo*)
12161 {
12162 Type* type = this->channel_->type();
12163 if (type->is_error_type())
12164 {
12165 this->set_is_error();
12166 return;
12167 }
12168 if (type->channel_type() == NULL)
12169 {
12170 this->report_error(_("expected channel"));
12171 return;
12172 }
12173 if (!type->channel_type()->may_receive())
12174 {
12175 this->report_error(_("invalid receive on send-only channel"));
12176 return;
12177 }
12178 }
12179
12180 // Get a tree for a receive expression.
12181
12182 tree
12183 Receive_expression::do_get_tree(Translate_context* context)
12184 {
12185 Channel_type* channel_type = this->channel_->type()->channel_type();
12186 gcc_assert(channel_type != NULL);
12187 Type* element_type = channel_type->element_type();
12188 tree element_type_tree = element_type->get_tree(context->gogo());
12189
12190 tree channel = this->channel_->get_tree(context);
12191 if (element_type_tree == error_mark_node || channel == error_mark_node)
12192 return error_mark_node;
12193
12194 return Gogo::receive_from_channel(element_type_tree, channel,
12195 this->for_select_, this->location());
12196 }
12197
12198 // Make a receive expression.
12199
12200 Receive_expression*
12201 Expression::make_receive(Expression* channel, source_location location)
12202 {
12203 return new Receive_expression(channel, location);
12204 }
12205
12206 // Class Send_expression.
12207
12208 // Traversal.
12209
12210 int
12211 Send_expression::do_traverse(Traverse* traverse)
12212 {
12213 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
12214 return TRAVERSE_EXIT;
12215 return Expression::traverse(&this->val_, traverse);
12216 }
12217
12218 // Get the type.
12219
12220 Type*
12221 Send_expression::do_type()
12222 {
12223 return Type::lookup_bool_type();
12224 }
12225
12226 // Set types.
12227
12228 void
12229 Send_expression::do_determine_type(const Type_context*)
12230 {
12231 this->channel_->determine_type_no_context();
12232
12233 Type* type = this->channel_->type();
12234 Type_context subcontext;
12235 if (type->channel_type() != NULL)
12236 subcontext.type = type->channel_type()->element_type();
12237 this->val_->determine_type(&subcontext);
12238 }
12239
12240 // Check types.
12241
12242 void
12243 Send_expression::do_check_types(Gogo*)
12244 {
12245 Type* type = this->channel_->type();
12246 if (type->is_error_type())
12247 {
12248 this->set_is_error();
12249 return;
12250 }
12251 Channel_type* channel_type = type->channel_type();
12252 if (channel_type == NULL)
12253 {
12254 error_at(this->location(), "left operand of %<<-%> must be channel");
12255 this->set_is_error();
12256 return;
12257 }
12258 Type* element_type = channel_type->element_type();
12259 if (element_type != NULL
12260 && !Type::are_assignable(element_type, this->val_->type(), NULL))
12261 {
12262 this->report_error(_("incompatible types in send"));
12263 return;
12264 }
12265 if (!channel_type->may_send())
12266 {
12267 this->report_error(_("invalid send on receive-only channel"));
12268 return;
12269 }
12270 }
12271
12272 // Get a tree for a send expression.
12273
12274 tree
12275 Send_expression::do_get_tree(Translate_context* context)
12276 {
12277 tree channel = this->channel_->get_tree(context);
12278 tree val = this->val_->get_tree(context);
12279 if (channel == error_mark_node || val == error_mark_node)
12280 return error_mark_node;
12281 Channel_type* channel_type = this->channel_->type()->channel_type();
12282 val = Expression::convert_for_assignment(context,
12283 channel_type->element_type(),
12284 this->val_->type(),
12285 val,
12286 this->location());
12287 return Gogo::send_on_channel(channel, val, this->is_value_discarded_,
12288 this->for_select_, this->location());
12289 }
12290
12291 // Make a send expression
12292
12293 Send_expression*
12294 Expression::make_send(Expression* channel, Expression* val,
12295 source_location location)
12296 {
12297 return new Send_expression(channel, val, location);
12298 }
12299
12300 // An expression which evaluates to a pointer to the type descriptor
12301 // of a type.
12302
12303 class Type_descriptor_expression : public Expression
12304 {
12305 public:
12306 Type_descriptor_expression(Type* type, source_location location)
12307 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
12308 type_(type)
12309 { }
12310
12311 protected:
12312 Type*
12313 do_type()
12314 { return Type::make_type_descriptor_ptr_type(); }
12315
12316 void
12317 do_determine_type(const Type_context*)
12318 { }
12319
12320 Expression*
12321 do_copy()
12322 { return this; }
12323
12324 tree
12325 do_get_tree(Translate_context* context)
12326 { return this->type_->type_descriptor_pointer(context->gogo()); }
12327
12328 private:
12329 // The type for which this is the descriptor.
12330 Type* type_;
12331 };
12332
12333 // Make a type descriptor expression.
12334
12335 Expression*
12336 Expression::make_type_descriptor(Type* type, source_location location)
12337 {
12338 return new Type_descriptor_expression(type, location);
12339 }
12340
12341 // An expression which evaluates to some characteristic of a type.
12342 // This is only used to initialize fields of a type descriptor. Using
12343 // a new expression class is slightly inefficient but gives us a good
12344 // separation between the frontend and the middle-end with regard to
12345 // how types are laid out.
12346
12347 class Type_info_expression : public Expression
12348 {
12349 public:
12350 Type_info_expression(Type* type, Type_info type_info)
12351 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
12352 type_(type), type_info_(type_info)
12353 { }
12354
12355 protected:
12356 Type*
12357 do_type();
12358
12359 void
12360 do_determine_type(const Type_context*)
12361 { }
12362
12363 Expression*
12364 do_copy()
12365 { return this; }
12366
12367 tree
12368 do_get_tree(Translate_context* context);
12369
12370 private:
12371 // The type for which we are getting information.
12372 Type* type_;
12373 // What information we want.
12374 Type_info type_info_;
12375 };
12376
12377 // The type is chosen to match what the type descriptor struct
12378 // expects.
12379
12380 Type*
12381 Type_info_expression::do_type()
12382 {
12383 switch (this->type_info_)
12384 {
12385 case TYPE_INFO_SIZE:
12386 return Type::lookup_integer_type("uintptr");
12387 case TYPE_INFO_ALIGNMENT:
12388 case TYPE_INFO_FIELD_ALIGNMENT:
12389 return Type::lookup_integer_type("uint8");
12390 default:
12391 gcc_unreachable();
12392 }
12393 }
12394
12395 // Return type information in GENERIC.
12396
12397 tree
12398 Type_info_expression::do_get_tree(Translate_context* context)
12399 {
12400 tree type_tree = this->type_->get_tree(context->gogo());
12401 if (type_tree == error_mark_node)
12402 return error_mark_node;
12403
12404 tree val_type_tree = this->type()->get_tree(context->gogo());
12405 gcc_assert(val_type_tree != error_mark_node);
12406
12407 if (this->type_info_ == TYPE_INFO_SIZE)
12408 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12409 TYPE_SIZE_UNIT(type_tree));
12410 else
12411 {
12412 unsigned int val;
12413 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12414 val = go_type_alignment(type_tree);
12415 else
12416 val = go_field_alignment(type_tree);
12417 return build_int_cstu(val_type_tree, val);
12418 }
12419 }
12420
12421 // Make a type info expression.
12422
12423 Expression*
12424 Expression::make_type_info(Type* type, Type_info type_info)
12425 {
12426 return new Type_info_expression(type, type_info);
12427 }
12428
12429 // An expression which evaluates to the offset of a field within a
12430 // struct. This, like Type_info_expression, q.v., is only used to
12431 // initialize fields of a type descriptor.
12432
12433 class Struct_field_offset_expression : public Expression
12434 {
12435 public:
12436 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12437 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12438 type_(type), field_(field)
12439 { }
12440
12441 protected:
12442 Type*
12443 do_type()
12444 { return Type::lookup_integer_type("uintptr"); }
12445
12446 void
12447 do_determine_type(const Type_context*)
12448 { }
12449
12450 Expression*
12451 do_copy()
12452 { return this; }
12453
12454 tree
12455 do_get_tree(Translate_context* context);
12456
12457 private:
12458 // The type of the struct.
12459 Struct_type* type_;
12460 // The field.
12461 const Struct_field* field_;
12462 };
12463
12464 // Return a struct field offset in GENERIC.
12465
12466 tree
12467 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12468 {
12469 tree type_tree = this->type_->get_tree(context->gogo());
12470 if (type_tree == error_mark_node)
12471 return error_mark_node;
12472
12473 tree val_type_tree = this->type()->get_tree(context->gogo());
12474 gcc_assert(val_type_tree != error_mark_node);
12475
12476 const Struct_field_list* fields = this->type_->fields();
12477 tree struct_field_tree = TYPE_FIELDS(type_tree);
12478 Struct_field_list::const_iterator p;
12479 for (p = fields->begin();
12480 p != fields->end();
12481 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12482 {
12483 gcc_assert(struct_field_tree != NULL_TREE);
12484 if (&*p == this->field_)
12485 break;
12486 }
12487 gcc_assert(&*p == this->field_);
12488
12489 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12490 byte_position(struct_field_tree));
12491 }
12492
12493 // Make an expression for a struct field offset.
12494
12495 Expression*
12496 Expression::make_struct_field_offset(Struct_type* type,
12497 const Struct_field* field)
12498 {
12499 return new Struct_field_offset_expression(type, field);
12500 }
12501
12502 // An expression which evaluates to the address of an unnamed label.
12503
12504 class Label_addr_expression : public Expression
12505 {
12506 public:
12507 Label_addr_expression(Label* label, source_location location)
12508 : Expression(EXPRESSION_LABEL_ADDR, location),
12509 label_(label)
12510 { }
12511
12512 protected:
12513 Type*
12514 do_type()
12515 { return Type::make_pointer_type(Type::make_void_type()); }
12516
12517 void
12518 do_determine_type(const Type_context*)
12519 { }
12520
12521 Expression*
12522 do_copy()
12523 { return new Label_addr_expression(this->label_, this->location()); }
12524
12525 tree
12526 do_get_tree(Translate_context*)
12527 { return this->label_->get_addr(this->location()); }
12528
12529 private:
12530 // The label whose address we are taking.
12531 Label* label_;
12532 };
12533
12534 // Make an expression for the address of an unnamed label.
12535
12536 Expression*
12537 Expression::make_label_addr(Label* label, source_location location)
12538 {
12539 return new Label_addr_expression(label, location);
12540 }
12541
12542 // Import an expression. This comes at the end in order to see the
12543 // various class definitions.
12544
12545 Expression*
12546 Expression::import_expression(Import* imp)
12547 {
12548 int c = imp->peek_char();
12549 if (imp->match_c_string("- ")
12550 || imp->match_c_string("! ")
12551 || imp->match_c_string("^ "))
12552 return Unary_expression::do_import(imp);
12553 else if (c == '(')
12554 return Binary_expression::do_import(imp);
12555 else if (imp->match_c_string("true")
12556 || imp->match_c_string("false"))
12557 return Boolean_expression::do_import(imp);
12558 else if (c == '"')
12559 return String_expression::do_import(imp);
12560 else if (c == '-' || (c >= '0' && c <= '9'))
12561 {
12562 // This handles integers, floats and complex constants.
12563 return Integer_expression::do_import(imp);
12564 }
12565 else if (imp->match_c_string("nil"))
12566 return Nil_expression::do_import(imp);
12567 else if (imp->match_c_string("convert"))
12568 return Type_conversion_expression::do_import(imp);
12569 else
12570 {
12571 error_at(imp->location(), "import error: expected expression");
12572 return Expression::make_error(imp->location());
12573 }
12574 }
12575
12576 // Class Expression_list.
12577
12578 // Traverse the list.
12579
12580 int
12581 Expression_list::traverse(Traverse* traverse)
12582 {
12583 for (Expression_list::iterator p = this->begin();
12584 p != this->end();
12585 ++p)
12586 {
12587 if (*p != NULL)
12588 {
12589 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12590 return TRAVERSE_EXIT;
12591 }
12592 }
12593 return TRAVERSE_CONTINUE;
12594 }
12595
12596 // Copy the list.
12597
12598 Expression_list*
12599 Expression_list::copy()
12600 {
12601 Expression_list* ret = new Expression_list();
12602 for (Expression_list::iterator p = this->begin();
12603 p != this->end();
12604 ++p)
12605 {
12606 if (*p == NULL)
12607 ret->push_back(NULL);
12608 else
12609 ret->push_back((*p)->copy());
12610 }
12611 return ret;
12612 }
12613
12614 // Return whether an expression list has an error expression.
12615
12616 bool
12617 Expression_list::contains_error() const
12618 {
12619 for (Expression_list::const_iterator p = this->begin();
12620 p != this->end();
12621 ++p)
12622 if (*p != NULL && (*p)->is_error_expression())
12623 return true;
12624 return false;
12625 }