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