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