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