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