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