Don't delete old arguments when lowering varargs.
[official-gcc.git] / gcc / go / gofrontend / expressions.cc
blobcebcbca7a0760ab2921e209cf132bacd4ce053a1
1 // expressions.cc -- Go frontend expression handling.
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.
7 #include "go-system.h"
9 #include <gmp.h>
11 #ifndef ENABLE_BUILD_WITH_CXX
12 extern "C"
14 #endif
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"
25 #ifndef ENABLE_BUILD_WITH_CXX
27 #endif
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"
38 // Class Expression.
40 Expression::Expression(Expression_classification classification,
41 source_location location)
42 : classification_(classification), location_(location)
46 Expression::~Expression()
50 // If this expression has a constant integer value, return it.
52 bool
53 Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
54 Type** ptype) const
56 *ptype = NULL;
57 return this->do_integer_constant_value(iota_is_constant, val, ptype);
60 // If this expression has a constant floating point value, return it.
62 bool
63 Expression::float_constant_value(mpfr_t val, Type** ptype) const
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
76 mpfr_set_z(val, ival, GMP_RNDN);
77 ret = true;
79 mpz_clear(ival);
80 return ret;
83 // If this expression has a constant complex value, return it.
85 bool
86 Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
87 Type** ptype) const
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))
95 mpfr_set_ui(imag, 0, GMP_RNDN);
96 return true;
98 return false;
101 // Traverse the expressions.
104 Expression::traverse(Expression** pexpr, Traverse* traverse)
106 Expression* expr = *pexpr;
107 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
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;
115 return expr->do_traverse(traverse);
118 // Traverse subexpressions of this expression.
121 Expression::traverse_subexpressions(Traverse* traverse)
123 return this->do_traverse(traverse);
126 // Default implementation for do_traverse for child classes.
129 Expression::do_traverse(Traverse*)
131 return TRAVERSE_CONTINUE;
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.
138 void
139 Expression::do_discarding_value()
141 this->warn_about_unused_value();
144 // This virtual function is called to export expressions. This will
145 // only be used by expressions which may be constant.
147 void
148 Expression::do_export(Export*) const
150 gcc_unreachable();
153 // Warn that the value of the expression is not used.
155 void
156 Expression::warn_about_unused_value()
158 warning_at(this->location(), OPT_Wunused_value, "value computed is not used");
161 // Note that this expression is an error. This is called by children
162 // when they discover an error.
164 void
165 Expression::set_is_error()
167 this->classification_ = EXPRESSION_ERROR;
170 // For children to call to report an error conveniently.
172 void
173 Expression::report_error(const char* msg)
175 error_at(this->location_, "%s", msg);
176 this->set_is_error();
179 // Set types of variables and constants. This is implemented by the
180 // child class.
182 void
183 Expression::determine_type(const Type_context* context)
185 this->do_determine_type(context);
188 // Set types when there is no context.
190 void
191 Expression::determine_type_no_context()
193 Type_context context;
194 this->do_determine_type(&context);
197 // Return a tree handling any conversions which must be done during
198 // assignment.
200 tree
201 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
202 Type* rhs_type, tree rhs_tree,
203 source_location location)
205 if (lhs_type == rhs_type)
206 return rhs_tree;
208 if (lhs_type->is_error_type() || rhs_type->is_error_type())
209 return error_mark_node;
211 if (lhs_type->is_undefined() || rhs_type->is_undefined())
213 // Make sure we report the error.
214 lhs_type->base();
215 rhs_type->base();
216 return error_mark_node;
219 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
220 return error_mark_node;
222 Gogo* gogo = context->gogo();
224 tree lhs_type_tree = lhs_type->get_tree(gogo);
225 if (lhs_type_tree == error_mark_node)
226 return error_mark_node;
228 if (lhs_type->interface_type() != NULL)
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);
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())
245 // Assigning nil to an open array.
246 gcc_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
248 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
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);
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);
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);
271 tree val = build_constructor(lhs_type_tree, init);
272 TREE_CONSTANT(val) = 1;
274 return val;
276 else if (rhs_type->is_nil_type())
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);
283 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
285 // No conversion is needed.
286 return rhs_tree;
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)
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);
303 else
305 gcc_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
306 return rhs_tree;
310 // Return a tree for a conversion from a non-interface type to an
311 // interface type.
313 tree
314 Expression::convert_type_to_interface(Translate_context* context,
315 Type* lhs_type, Type* rhs_type,
316 tree rhs_tree, source_location location)
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();
322 // Since RHS_TYPE is a static type, we can create the interface
323 // method table at compile time.
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);
330 // This should have been checked already.
331 gcc_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
333 tree lhs_type_tree = lhs_type->get_tree(gogo);
334 if (lhs_type_tree == error_mark_node)
335 return error_mark_node;
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
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)
352 rhs_named_type = rhs_type->deref()->named_type();
353 is_pointer = true;
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);
365 if (first_field_value == error_mark_node)
366 return error_mark_node;
368 // Start building a constructor for the value we will return.
370 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
372 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
373 tree field = TYPE_FIELDS(lhs_type_tree);
374 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
375 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
376 elt->index = field;
377 elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value);
379 elt = VEC_quick_push(constructor_elt, init, NULL);
380 field = DECL_CHAIN(field);
381 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
382 elt->index = field;
384 if (rhs_type->points_to() != NULL)
386 // We are assigning a pointer to the interface; the interface
387 // holds the pointer itself.
388 elt->value = rhs_tree;
389 return build_constructor(lhs_type_tree, init);
392 // We are assigning a non-pointer value to the interface; the
393 // interface gets a copy of the value in the heap.
395 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
397 tree space = gogo->allocate_memory(rhs_type, object_size, location);
398 space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)),
399 space);
400 space = save_expr(space);
402 tree ref = build_fold_indirect_ref_loc(location, space);
403 TREE_THIS_NOTRAP(ref) = 1;
404 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
405 ref, rhs_tree);
407 elt->value = fold_convert_loc(location, TREE_TYPE(field), space);
409 return build2(COMPOUND_EXPR, lhs_type_tree, set,
410 build_constructor(lhs_type_tree, init));
413 // Return a tree for the type descriptor of RHS_TREE, which has
414 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
415 // NULL.
417 tree
418 Expression::get_interface_type_descriptor(Translate_context*,
419 Type* rhs_type, tree rhs_tree,
420 source_location location)
422 tree rhs_type_tree = TREE_TYPE(rhs_tree);
423 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
424 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
425 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
426 NULL_TREE);
427 if (rhs_type->interface_type()->is_empty())
429 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
430 "__type_descriptor") == 0);
431 return v;
434 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
435 == 0);
436 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v)));
437 v = save_expr(v);
438 tree v1 = build_fold_indirect_ref_loc(location, v);
439 gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
440 tree f = TYPE_FIELDS(TREE_TYPE(v1));
441 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
442 == 0);
443 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
445 tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v,
446 fold_convert_loc(location, TREE_TYPE(v),
447 null_pointer_node));
448 tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node);
449 return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1),
450 eq, n, v1);
453 // Return a tree for the conversion of an interface type to an
454 // interface type.
456 tree
457 Expression::convert_interface_to_interface(Translate_context* context,
458 Type *lhs_type, Type *rhs_type,
459 tree rhs_tree, bool for_type_guard,
460 source_location location)
462 Gogo* gogo = context->gogo();
463 Interface_type* lhs_interface_type = lhs_type->interface_type();
464 bool lhs_is_empty = lhs_interface_type->is_empty();
466 tree lhs_type_tree = lhs_type->get_tree(gogo);
467 if (lhs_type_tree == error_mark_node)
468 return error_mark_node;
470 // In the general case this requires runtime examination of the type
471 // method table to match it up with the interface methods.
473 // FIXME: If all of the methods in the right hand side interface
474 // also appear in the left hand side interface, then we don't need
475 // to do a runtime check, although we still need to build a new
476 // method table.
478 // Get the type descriptor for the right hand side. This will be
479 // NULL for a nil interface.
481 if (!DECL_P(rhs_tree))
482 rhs_tree = save_expr(rhs_tree);
484 tree rhs_type_descriptor =
485 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
486 location);
488 // The result is going to be a two element constructor.
490 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
492 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
493 tree field = TYPE_FIELDS(lhs_type_tree);
494 elt->index = field;
496 if (for_type_guard)
498 // A type assertion fails when converting a nil interface.
499 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
500 static tree assert_interface_decl;
501 tree call = Gogo::call_builtin(&assert_interface_decl,
502 location,
503 "__go_assert_interface",
505 ptr_type_node,
506 TREE_TYPE(lhs_type_descriptor),
507 lhs_type_descriptor,
508 TREE_TYPE(rhs_type_descriptor),
509 rhs_type_descriptor);
510 if (call == error_mark_node)
511 return error_mark_node;
512 // This will panic if the interface conversion fails.
513 TREE_NOTHROW(assert_interface_decl) = 0;
514 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
516 else if (lhs_is_empty)
518 // A convertion to an empty interface always succeeds, and the
519 // first field is just the type descriptor of the object.
520 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
521 "__type_descriptor") == 0);
522 gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
523 elt->value = rhs_type_descriptor;
525 else
527 // A conversion to a non-empty interface may fail, but unlike a
528 // type assertion converting nil will always succeed.
529 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
530 == 0);
531 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
532 static tree convert_interface_decl;
533 tree call = Gogo::call_builtin(&convert_interface_decl,
534 location,
535 "__go_convert_interface",
537 ptr_type_node,
538 TREE_TYPE(lhs_type_descriptor),
539 lhs_type_descriptor,
540 TREE_TYPE(rhs_type_descriptor),
541 rhs_type_descriptor);
542 if (call == error_mark_node)
543 return error_mark_node;
544 // This will panic if the interface conversion fails.
545 TREE_NOTHROW(convert_interface_decl) = 0;
546 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
549 // The second field is simply the object pointer.
551 elt = VEC_quick_push(constructor_elt, init, NULL);
552 field = DECL_CHAIN(field);
553 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
554 elt->index = field;
556 tree rhs_type_tree = TREE_TYPE(rhs_tree);
557 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
558 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
559 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
560 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
561 NULL_TREE);
563 return build_constructor(lhs_type_tree, init);
566 // Return a tree for the conversion of an interface type to a
567 // non-interface type.
569 tree
570 Expression::convert_interface_to_type(Translate_context* context,
571 Type *lhs_type, Type* rhs_type,
572 tree rhs_tree, source_location location)
574 Gogo* gogo = context->gogo();
575 tree rhs_type_tree = TREE_TYPE(rhs_tree);
577 tree lhs_type_tree = lhs_type->get_tree(gogo);
578 if (lhs_type_tree == error_mark_node)
579 return error_mark_node;
581 // Call a function to check that the type is valid. The function
582 // will panic with an appropriate runtime type error if the type is
583 // not valid.
585 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
587 if (!DECL_P(rhs_tree))
588 rhs_tree = save_expr(rhs_tree);
590 tree rhs_type_descriptor =
591 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
592 location);
594 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo);
596 static tree check_interface_type_decl;
597 tree call = Gogo::call_builtin(&check_interface_type_decl,
598 location,
599 "__go_check_interface_type",
601 void_type_node,
602 TREE_TYPE(lhs_type_descriptor),
603 lhs_type_descriptor,
604 TREE_TYPE(rhs_type_descriptor),
605 rhs_type_descriptor,
606 TREE_TYPE(rhs_inter_descriptor),
607 rhs_inter_descriptor);
608 if (call == error_mark_node)
609 return error_mark_node;
610 // This call will panic if the conversion is invalid.
611 TREE_NOTHROW(check_interface_type_decl) = 0;
613 // If the call succeeds, pull out the value.
614 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
615 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
616 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
617 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
618 NULL_TREE);
620 // If the value is a pointer, then it is the value we want.
621 // Otherwise it points to the value.
622 if (lhs_type->points_to() == NULL)
624 val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val);
625 val = build_fold_indirect_ref_loc(location, val);
628 return build2(COMPOUND_EXPR, lhs_type_tree, call,
629 fold_convert_loc(location, lhs_type_tree, val));
632 // Convert an expression to a tree. This is implemented by the child
633 // class. Not that it is not in general safe to call this multiple
634 // times for a single expression, but that we don't catch such errors.
636 tree
637 Expression::get_tree(Translate_context* context)
639 // The child may have marked this expression as having an error.
640 if (this->classification_ == EXPRESSION_ERROR)
641 return error_mark_node;
643 return this->do_get_tree(context);
646 // Return a tree for VAL in TYPE.
648 tree
649 Expression::integer_constant_tree(mpz_t val, tree type)
651 if (type == error_mark_node)
652 return error_mark_node;
653 else if (TREE_CODE(type) == INTEGER_TYPE)
654 return double_int_to_tree(type,
655 mpz_get_double_int(type, val, true));
656 else if (TREE_CODE(type) == REAL_TYPE)
658 mpfr_t fval;
659 mpfr_init_set_z(fval, val, GMP_RNDN);
660 tree ret = Expression::float_constant_tree(fval, type);
661 mpfr_clear(fval);
662 return ret;
664 else if (TREE_CODE(type) == COMPLEX_TYPE)
666 mpfr_t fval;
667 mpfr_init_set_z(fval, val, GMP_RNDN);
668 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
669 mpfr_clear(fval);
670 tree imag = build_real_from_int_cst(TREE_TYPE(type),
671 integer_zero_node);
672 return build_complex(type, real, imag);
674 else
675 gcc_unreachable();
678 // Return a tree for VAL in TYPE.
680 tree
681 Expression::float_constant_tree(mpfr_t val, tree type)
683 if (type == error_mark_node)
684 return error_mark_node;
685 else if (TREE_CODE(type) == INTEGER_TYPE)
687 mpz_t ival;
688 mpz_init(ival);
689 mpfr_get_z(ival, val, GMP_RNDN);
690 tree ret = Expression::integer_constant_tree(ival, type);
691 mpz_clear(ival);
692 return ret;
694 else if (TREE_CODE(type) == REAL_TYPE)
696 REAL_VALUE_TYPE r1;
697 real_from_mpfr(&r1, val, type, GMP_RNDN);
698 REAL_VALUE_TYPE r2;
699 real_convert(&r2, TYPE_MODE(type), &r1);
700 return build_real(type, r2);
702 else if (TREE_CODE(type) == COMPLEX_TYPE)
704 REAL_VALUE_TYPE r1;
705 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
706 REAL_VALUE_TYPE r2;
707 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
708 tree imag = build_real_from_int_cst(TREE_TYPE(type),
709 integer_zero_node);
710 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
712 else
713 gcc_unreachable();
716 // Return a tree for REAL/IMAG in TYPE.
718 tree
719 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
721 if (type == error_mark_node)
722 return error_mark_node;
723 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
724 return Expression::float_constant_tree(real, type);
725 else if (TREE_CODE(type) == COMPLEX_TYPE)
727 REAL_VALUE_TYPE r1;
728 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
729 REAL_VALUE_TYPE r2;
730 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
732 REAL_VALUE_TYPE r3;
733 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
734 REAL_VALUE_TYPE r4;
735 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
737 return build_complex(type, build_real(TREE_TYPE(type), r2),
738 build_real(TREE_TYPE(type), r4));
740 else
741 gcc_unreachable();
744 // Return a tree which evaluates to true if VAL, of arbitrary integer
745 // type, is negative or is more than the maximum value of BOUND_TYPE.
746 // If SOFAR is not NULL, it is or'red into the result. The return
747 // value may be NULL if SOFAR is NULL.
749 tree
750 Expression::check_bounds(tree val, tree bound_type, tree sofar,
751 source_location loc)
753 tree val_type = TREE_TYPE(val);
754 tree ret = NULL_TREE;
756 if (!TYPE_UNSIGNED(val_type))
758 ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val,
759 build_int_cst(val_type, 0));
760 if (ret == boolean_false_node)
761 ret = NULL_TREE;
764 if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type))
765 || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type))
767 tree max = TYPE_MAX_VALUE(bound_type);
768 tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val,
769 fold_convert_loc(loc, val_type, max));
770 if (big == boolean_false_node)
772 else if (ret == NULL_TREE)
773 ret = big;
774 else
775 ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
776 ret, big);
779 if (ret == NULL_TREE)
780 return sofar;
781 else if (sofar == NULL_TREE)
782 return ret;
783 else
784 return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
785 sofar, ret);
788 // Error expressions. This are used to avoid cascading errors.
790 class Error_expression : public Expression
792 public:
793 Error_expression(source_location location)
794 : Expression(EXPRESSION_ERROR, location)
797 protected:
798 bool
799 do_is_constant() const
800 { return true; }
802 bool
803 do_integer_constant_value(bool, mpz_t val, Type**) const
805 mpz_set_ui(val, 0);
806 return true;
809 bool
810 do_float_constant_value(mpfr_t val, Type**) const
812 mpfr_set_ui(val, 0, GMP_RNDN);
813 return true;
816 bool
817 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
819 mpfr_set_ui(real, 0, GMP_RNDN);
820 mpfr_set_ui(imag, 0, GMP_RNDN);
821 return true;
824 void
825 do_discarding_value()
828 Type*
829 do_type()
830 { return Type::make_error_type(); }
832 void
833 do_determine_type(const Type_context*)
836 Expression*
837 do_copy()
838 { return this; }
840 bool
841 do_is_addressable() const
842 { return true; }
844 tree
845 do_get_tree(Translate_context*)
846 { return error_mark_node; }
849 Expression*
850 Expression::make_error(source_location location)
852 return new Error_expression(location);
855 // An expression which is really a type. This is used during parsing.
856 // It is an error if these survive after lowering.
858 class
859 Type_expression : public Expression
861 public:
862 Type_expression(Type* type, source_location location)
863 : Expression(EXPRESSION_TYPE, location),
864 type_(type)
867 protected:
869 do_traverse(Traverse* traverse)
870 { return Type::traverse(this->type_, traverse); }
872 Type*
873 do_type()
874 { return this->type_; }
876 void
877 do_determine_type(const Type_context*)
880 void
881 do_check_types(Gogo*)
882 { this->report_error(_("invalid use of type")); }
884 Expression*
885 do_copy()
886 { return this; }
888 tree
889 do_get_tree(Translate_context*)
890 { gcc_unreachable(); }
892 private:
893 // The type which we are representing as an expression.
894 Type* type_;
897 Expression*
898 Expression::make_type(Type* type, source_location location)
900 return new Type_expression(type, location);
903 // Class Parser_expression.
905 Type*
906 Parser_expression::do_type()
908 // We should never really ask for the type of a Parser_expression.
909 // However, it can happen, at least when we have an invalid const
910 // whose initializer refers to the const itself. In that case we
911 // may ask for the type when lowering the const itself.
912 gcc_assert(saw_errors());
913 return Type::make_error_type();
916 // Class Var_expression.
918 // Lower a variable expression. Here we just make sure that the
919 // initialization expression of the variable has been lowered. This
920 // ensures that we will be able to determine the type of the variable
921 // if necessary.
923 Expression*
924 Var_expression::do_lower(Gogo* gogo, Named_object* function, int)
926 if (this->variable_->is_variable())
928 Variable* var = this->variable_->var_value();
929 // This is either a local variable or a global variable. A
930 // reference to a variable which is local to an enclosing
931 // function will be a reference to a field in a closure.
932 if (var->is_global())
933 function = NULL;
934 var->lower_init_expression(gogo, function);
936 return this;
939 // Return the name of the variable.
941 const std::string&
942 Var_expression::name() const
944 return this->variable_->name();
947 // Return the type of a reference to a variable.
949 Type*
950 Var_expression::do_type()
952 if (this->variable_->is_variable())
953 return this->variable_->var_value()->type();
954 else if (this->variable_->is_result_variable())
955 return this->variable_->result_var_value()->type();
956 else
957 gcc_unreachable();
960 // Something takes the address of this variable. This means that we
961 // may want to move the variable onto the heap.
963 void
964 Var_expression::do_address_taken(bool escapes)
966 if (!escapes)
968 else if (this->variable_->is_variable())
969 this->variable_->var_value()->set_address_taken();
970 else if (this->variable_->is_result_variable())
971 this->variable_->result_var_value()->set_address_taken();
972 else
973 gcc_unreachable();
976 // Get the tree for a reference to a variable.
978 tree
979 Var_expression::do_get_tree(Translate_context* context)
981 return this->variable_->get_tree(context->gogo(), context->function());
984 // Make a reference to a variable in an expression.
986 Expression*
987 Expression::make_var_reference(Named_object* var, source_location location)
989 if (var->is_sink())
990 return Expression::make_sink(location);
992 // FIXME: Creating a new object for each reference to a variable is
993 // wasteful.
994 return new Var_expression(var, location);
997 // Class Temporary_reference_expression.
999 // The type.
1001 Type*
1002 Temporary_reference_expression::do_type()
1004 return this->statement_->type();
1007 // Called if something takes the address of this temporary variable.
1008 // We never have to move temporary variables to the heap, but we do
1009 // need to know that they must live in the stack rather than in a
1010 // register.
1012 void
1013 Temporary_reference_expression::do_address_taken(bool)
1015 this->statement_->set_is_address_taken();
1018 // Get a tree referring to the variable.
1020 tree
1021 Temporary_reference_expression::do_get_tree(Translate_context*)
1023 return this->statement_->get_decl();
1026 // Make a reference to a temporary variable.
1028 Expression*
1029 Expression::make_temporary_reference(Temporary_statement* statement,
1030 source_location location)
1032 return new Temporary_reference_expression(statement, location);
1035 // A sink expression--a use of the blank identifier _.
1037 class Sink_expression : public Expression
1039 public:
1040 Sink_expression(source_location location)
1041 : Expression(EXPRESSION_SINK, location),
1042 type_(NULL), var_(NULL_TREE)
1045 protected:
1046 void
1047 do_discarding_value()
1050 Type*
1051 do_type();
1053 void
1054 do_determine_type(const Type_context*);
1056 Expression*
1057 do_copy()
1058 { return new Sink_expression(this->location()); }
1060 tree
1061 do_get_tree(Translate_context*);
1063 private:
1064 // The type of this sink variable.
1065 Type* type_;
1066 // The temporary variable we generate.
1067 tree var_;
1070 // Return the type of a sink expression.
1072 Type*
1073 Sink_expression::do_type()
1075 if (this->type_ == NULL)
1076 return Type::make_sink_type();
1077 return this->type_;
1080 // Determine the type of a sink expression.
1082 void
1083 Sink_expression::do_determine_type(const Type_context* context)
1085 if (context->type != NULL)
1086 this->type_ = context->type;
1089 // Return a temporary variable for a sink expression. This will
1090 // presumably be a write-only variable which the middle-end will drop.
1092 tree
1093 Sink_expression::do_get_tree(Translate_context* context)
1095 if (this->var_ == NULL_TREE)
1097 gcc_assert(this->type_ != NULL && !this->type_->is_sink_type());
1098 this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()),
1099 "blank");
1101 return this->var_;
1104 // Make a sink expression.
1106 Expression*
1107 Expression::make_sink(source_location location)
1109 return new Sink_expression(location);
1112 // Class Func_expression.
1114 // FIXME: Can a function expression appear in a constant expression?
1115 // The value is unchanging. Initializing a constant to the address of
1116 // a function seems like it could work, though there might be little
1117 // point to it.
1119 // Return the name of the function.
1121 const std::string&
1122 Func_expression::name() const
1124 return this->function_->name();
1127 // Traversal.
1130 Func_expression::do_traverse(Traverse* traverse)
1132 return (this->closure_ == NULL
1133 ? TRAVERSE_CONTINUE
1134 : Expression::traverse(&this->closure_, traverse));
1137 // Return the type of a function expression.
1139 Type*
1140 Func_expression::do_type()
1142 if (this->function_->is_function())
1143 return this->function_->func_value()->type();
1144 else if (this->function_->is_function_declaration())
1145 return this->function_->func_declaration_value()->type();
1146 else
1147 gcc_unreachable();
1150 // Get the tree for a function expression without evaluating the
1151 // closure.
1153 tree
1154 Func_expression::get_tree_without_closure(Gogo* gogo)
1156 Function_type* fntype;
1157 if (this->function_->is_function())
1158 fntype = this->function_->func_value()->type();
1159 else if (this->function_->is_function_declaration())
1160 fntype = this->function_->func_declaration_value()->type();
1161 else
1162 gcc_unreachable();
1164 // Builtin functions are handled specially by Call_expression. We
1165 // can't take their address.
1166 if (fntype->is_builtin())
1168 error_at(this->location(), "invalid use of special builtin function %qs",
1169 this->function_->name().c_str());
1170 return error_mark_node;
1173 Named_object* no = this->function_;
1175 tree id = no->get_id(gogo);
1176 if (id == error_mark_node)
1177 return error_mark_node;
1179 tree fndecl;
1180 if (no->is_function())
1181 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1182 else if (no->is_function_declaration())
1183 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1184 else
1185 gcc_unreachable();
1187 if (fndecl == error_mark_node)
1188 return error_mark_node;
1190 return build_fold_addr_expr_loc(this->location(), fndecl);
1193 // Get the tree for a function expression. This is used when we take
1194 // the address of a function rather than simply calling it. If the
1195 // function has a closure, we must use a trampoline.
1197 tree
1198 Func_expression::do_get_tree(Translate_context* context)
1200 Gogo* gogo = context->gogo();
1202 tree fnaddr = this->get_tree_without_closure(gogo);
1203 if (fnaddr == error_mark_node)
1204 return error_mark_node;
1206 gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1207 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1208 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1210 // For a normal non-nested function call, that is all we have to do.
1211 if (!this->function_->is_function()
1212 || this->function_->func_value()->enclosing() == NULL)
1214 gcc_assert(this->closure_ == NULL);
1215 return fnaddr;
1218 // For a nested function call, we have to always allocate a
1219 // trampoline. If we don't always allocate, then closures will not
1220 // be reliably distinct.
1221 Expression* closure = this->closure_;
1222 tree closure_tree;
1223 if (closure == NULL)
1224 closure_tree = null_pointer_node;
1225 else
1227 // Get the value of the closure. This will be a pointer to
1228 // space allocated on the heap.
1229 closure_tree = closure->get_tree(context);
1230 if (closure_tree == error_mark_node)
1231 return error_mark_node;
1232 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1235 // Now we need to build some code on the heap. This code will load
1236 // the static chain pointer with the closure and then jump to the
1237 // body of the function. The normal gcc approach is to build the
1238 // code on the stack. Unfortunately we can not do that, as Go
1239 // permits us to return the function pointer.
1241 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1244 // Make a reference to a function in an expression.
1246 Expression*
1247 Expression::make_func_reference(Named_object* function, Expression* closure,
1248 source_location location)
1250 return new Func_expression(function, closure, location);
1253 // Class Unknown_expression.
1255 // Return the name of an unknown expression.
1257 const std::string&
1258 Unknown_expression::name() const
1260 return this->named_object_->name();
1263 // Lower a reference to an unknown name.
1265 Expression*
1266 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1268 source_location location = this->location();
1269 Named_object* no = this->named_object_;
1270 Named_object* real;
1271 if (!no->is_unknown())
1272 real = no;
1273 else
1275 real = no->unknown_value()->real_named_object();
1276 if (real == NULL)
1278 if (this->is_composite_literal_key_)
1279 return this;
1280 error_at(location, "reference to undefined name %qs",
1281 this->named_object_->message_name().c_str());
1282 return Expression::make_error(location);
1285 switch (real->classification())
1287 case Named_object::NAMED_OBJECT_CONST:
1288 return Expression::make_const_reference(real, location);
1289 case Named_object::NAMED_OBJECT_TYPE:
1290 return Expression::make_type(real->type_value(), location);
1291 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1292 if (this->is_composite_literal_key_)
1293 return this;
1294 error_at(location, "reference to undefined type %qs",
1295 real->message_name().c_str());
1296 return Expression::make_error(location);
1297 case Named_object::NAMED_OBJECT_VAR:
1298 return Expression::make_var_reference(real, location);
1299 case Named_object::NAMED_OBJECT_FUNC:
1300 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1301 return Expression::make_func_reference(real, NULL, location);
1302 case Named_object::NAMED_OBJECT_PACKAGE:
1303 if (this->is_composite_literal_key_)
1304 return this;
1305 error_at(location, "unexpected reference to package");
1306 return Expression::make_error(location);
1307 default:
1308 gcc_unreachable();
1312 // Make a reference to an unknown name.
1314 Expression*
1315 Expression::make_unknown_reference(Named_object* no, source_location location)
1317 gcc_assert(no->resolve()->is_unknown());
1318 return new Unknown_expression(no, location);
1321 // A boolean expression.
1323 class Boolean_expression : public Expression
1325 public:
1326 Boolean_expression(bool val, source_location location)
1327 : Expression(EXPRESSION_BOOLEAN, location),
1328 val_(val), type_(NULL)
1331 static Expression*
1332 do_import(Import*);
1334 protected:
1335 bool
1336 do_is_constant() const
1337 { return true; }
1339 Type*
1340 do_type();
1342 void
1343 do_determine_type(const Type_context*);
1345 Expression*
1346 do_copy()
1347 { return this; }
1349 tree
1350 do_get_tree(Translate_context*)
1351 { return this->val_ ? boolean_true_node : boolean_false_node; }
1353 void
1354 do_export(Export* exp) const
1355 { exp->write_c_string(this->val_ ? "true" : "false"); }
1357 private:
1358 // The constant.
1359 bool val_;
1360 // The type as determined by context.
1361 Type* type_;
1364 // Get the type.
1366 Type*
1367 Boolean_expression::do_type()
1369 if (this->type_ == NULL)
1370 this->type_ = Type::make_boolean_type();
1371 return this->type_;
1374 // Set the type from the context.
1376 void
1377 Boolean_expression::do_determine_type(const Type_context* context)
1379 if (this->type_ != NULL && !this->type_->is_abstract())
1381 else if (context->type != NULL && context->type->is_boolean_type())
1382 this->type_ = context->type;
1383 else if (!context->may_be_abstract)
1384 this->type_ = Type::lookup_bool_type();
1387 // Import a boolean constant.
1389 Expression*
1390 Boolean_expression::do_import(Import* imp)
1392 if (imp->peek_char() == 't')
1394 imp->require_c_string("true");
1395 return Expression::make_boolean(true, imp->location());
1397 else
1399 imp->require_c_string("false");
1400 return Expression::make_boolean(false, imp->location());
1404 // Make a boolean expression.
1406 Expression*
1407 Expression::make_boolean(bool val, source_location location)
1409 return new Boolean_expression(val, location);
1412 // Class String_expression.
1414 // Get the type.
1416 Type*
1417 String_expression::do_type()
1419 if (this->type_ == NULL)
1420 this->type_ = Type::make_string_type();
1421 return this->type_;
1424 // Set the type from the context.
1426 void
1427 String_expression::do_determine_type(const Type_context* context)
1429 if (this->type_ != NULL && !this->type_->is_abstract())
1431 else if (context->type != NULL && context->type->is_string_type())
1432 this->type_ = context->type;
1433 else if (!context->may_be_abstract)
1434 this->type_ = Type::lookup_string_type();
1437 // Build a string constant.
1439 tree
1440 String_expression::do_get_tree(Translate_context* context)
1442 return context->gogo()->go_string_constant_tree(this->val_);
1445 // Export a string expression.
1447 void
1448 String_expression::do_export(Export* exp) const
1450 std::string s;
1451 s.reserve(this->val_.length() * 4 + 2);
1452 s += '"';
1453 for (std::string::const_iterator p = this->val_.begin();
1454 p != this->val_.end();
1455 ++p)
1457 if (*p == '\\' || *p == '"')
1459 s += '\\';
1460 s += *p;
1462 else if (*p >= 0x20 && *p < 0x7f)
1463 s += *p;
1464 else if (*p == '\n')
1465 s += "\\n";
1466 else if (*p == '\t')
1467 s += "\\t";
1468 else
1470 s += "\\x";
1471 unsigned char c = *p;
1472 unsigned int dig = c >> 4;
1473 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1474 dig = c & 0xf;
1475 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1478 s += '"';
1479 exp->write_string(s);
1482 // Import a string expression.
1484 Expression*
1485 String_expression::do_import(Import* imp)
1487 imp->require_c_string("\"");
1488 std::string val;
1489 while (true)
1491 int c = imp->get_char();
1492 if (c == '"' || c == -1)
1493 break;
1494 if (c != '\\')
1495 val += static_cast<char>(c);
1496 else
1498 c = imp->get_char();
1499 if (c == '\\' || c == '"')
1500 val += static_cast<char>(c);
1501 else if (c == 'n')
1502 val += '\n';
1503 else if (c == 't')
1504 val += '\t';
1505 else if (c == 'x')
1507 c = imp->get_char();
1508 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1509 c = imp->get_char();
1510 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1511 char v = (vh << 4) | vl;
1512 val += v;
1514 else
1516 error_at(imp->location(), "bad string constant");
1517 return Expression::make_error(imp->location());
1521 return Expression::make_string(val, imp->location());
1524 // Make a string expression.
1526 Expression*
1527 Expression::make_string(const std::string& val, source_location location)
1529 return new String_expression(val, location);
1532 // Make an integer expression.
1534 class Integer_expression : public Expression
1536 public:
1537 Integer_expression(const mpz_t* val, Type* type, source_location location)
1538 : Expression(EXPRESSION_INTEGER, location),
1539 type_(type)
1540 { mpz_init_set(this->val_, *val); }
1542 static Expression*
1543 do_import(Import*);
1545 // Return whether VAL fits in the type.
1546 static bool
1547 check_constant(mpz_t val, Type*, source_location);
1549 // Write VAL to export data.
1550 static void
1551 export_integer(Export* exp, const mpz_t val);
1553 protected:
1554 bool
1555 do_is_constant() const
1556 { return true; }
1558 bool
1559 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1561 Type*
1562 do_type();
1564 void
1565 do_determine_type(const Type_context* context);
1567 void
1568 do_check_types(Gogo*);
1570 tree
1571 do_get_tree(Translate_context*);
1573 Expression*
1574 do_copy()
1575 { return Expression::make_integer(&this->val_, this->type_,
1576 this->location()); }
1578 void
1579 do_export(Export*) const;
1581 private:
1582 // The integer value.
1583 mpz_t val_;
1584 // The type so far.
1585 Type* type_;
1588 // Return an integer constant value.
1590 bool
1591 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1592 Type** ptype) const
1594 if (this->type_ != NULL)
1595 *ptype = this->type_;
1596 mpz_set(val, this->val_);
1597 return true;
1600 // Return the current type. If we haven't set the type yet, we return
1601 // an abstract integer type.
1603 Type*
1604 Integer_expression::do_type()
1606 if (this->type_ == NULL)
1607 this->type_ = Type::make_abstract_integer_type();
1608 return this->type_;
1611 // Set the type of the integer value. Here we may switch from an
1612 // abstract type to a real type.
1614 void
1615 Integer_expression::do_determine_type(const Type_context* context)
1617 if (this->type_ != NULL && !this->type_->is_abstract())
1619 else if (context->type != NULL
1620 && (context->type->integer_type() != NULL
1621 || context->type->float_type() != NULL
1622 || context->type->complex_type() != NULL))
1623 this->type_ = context->type;
1624 else if (!context->may_be_abstract)
1625 this->type_ = Type::lookup_integer_type("int");
1628 // Return true if the integer VAL fits in the range of the type TYPE.
1629 // Otherwise give an error and return false. TYPE may be NULL.
1631 bool
1632 Integer_expression::check_constant(mpz_t val, Type* type,
1633 source_location location)
1635 if (type == NULL)
1636 return true;
1637 Integer_type* itype = type->integer_type();
1638 if (itype == NULL || itype->is_abstract())
1639 return true;
1641 int bits = mpz_sizeinbase(val, 2);
1643 if (itype->is_unsigned())
1645 // For an unsigned type we can only accept a nonnegative number,
1646 // and we must be able to represent at least BITS.
1647 if (mpz_sgn(val) >= 0
1648 && bits <= itype->bits())
1649 return true;
1651 else
1653 // For a signed type we need an extra bit to indicate the sign.
1654 // We have to handle the most negative integer specially.
1655 if (bits + 1 <= itype->bits()
1656 || (bits <= itype->bits()
1657 && mpz_sgn(val) < 0
1658 && (mpz_scan1(val, 0)
1659 == static_cast<unsigned long>(itype->bits() - 1))
1660 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1661 return true;
1664 error_at(location, "integer constant overflow");
1665 return false;
1668 // Check the type of an integer constant.
1670 void
1671 Integer_expression::do_check_types(Gogo*)
1673 if (this->type_ == NULL)
1674 return;
1675 if (!Integer_expression::check_constant(this->val_, this->type_,
1676 this->location()))
1677 this->set_is_error();
1680 // Get a tree for an integer constant.
1682 tree
1683 Integer_expression::do_get_tree(Translate_context* context)
1685 Gogo* gogo = context->gogo();
1686 tree type;
1687 if (this->type_ != NULL && !this->type_->is_abstract())
1688 type = this->type_->get_tree(gogo);
1689 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1691 // We are converting to an abstract floating point type.
1692 type = Type::lookup_float_type("float64")->get_tree(gogo);
1694 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1696 // We are converting to an abstract complex type.
1697 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
1699 else
1701 // If we still have an abstract type here, then this is being
1702 // used in a constant expression which didn't get reduced for
1703 // some reason. Use a type which will fit the value. We use <,
1704 // not <=, because we need an extra bit for the sign bit.
1705 int bits = mpz_sizeinbase(this->val_, 2);
1706 if (bits < INT_TYPE_SIZE)
1707 type = Type::lookup_integer_type("int")->get_tree(gogo);
1708 else if (bits < 64)
1709 type = Type::lookup_integer_type("int64")->get_tree(gogo);
1710 else
1711 type = long_long_integer_type_node;
1713 return Expression::integer_constant_tree(this->val_, type);
1716 // Write VAL to export data.
1718 void
1719 Integer_expression::export_integer(Export* exp, const mpz_t val)
1721 char* s = mpz_get_str(NULL, 10, val);
1722 exp->write_c_string(s);
1723 free(s);
1726 // Export an integer in a constant expression.
1728 void
1729 Integer_expression::do_export(Export* exp) const
1731 Integer_expression::export_integer(exp, this->val_);
1732 // A trailing space lets us reliably identify the end of the number.
1733 exp->write_c_string(" ");
1736 // Import an integer, floating point, or complex value. This handles
1737 // all these types because they all start with digits.
1739 Expression*
1740 Integer_expression::do_import(Import* imp)
1742 std::string num = imp->read_identifier();
1743 imp->require_c_string(" ");
1744 if (!num.empty() && num[num.length() - 1] == 'i')
1746 mpfr_t real;
1747 size_t plus_pos = num.find('+', 1);
1748 size_t minus_pos = num.find('-', 1);
1749 size_t pos;
1750 if (plus_pos == std::string::npos)
1751 pos = minus_pos;
1752 else if (minus_pos == std::string::npos)
1753 pos = plus_pos;
1754 else
1756 error_at(imp->location(), "bad number in import data: %qs",
1757 num.c_str());
1758 return Expression::make_error(imp->location());
1760 if (pos == std::string::npos)
1761 mpfr_set_ui(real, 0, GMP_RNDN);
1762 else
1764 std::string real_str = num.substr(0, pos);
1765 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1767 error_at(imp->location(), "bad number in import data: %qs",
1768 real_str.c_str());
1769 return Expression::make_error(imp->location());
1773 std::string imag_str;
1774 if (pos == std::string::npos)
1775 imag_str = num;
1776 else
1777 imag_str = num.substr(pos);
1778 imag_str = imag_str.substr(0, imag_str.size() - 1);
1779 mpfr_t imag;
1780 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1782 error_at(imp->location(), "bad number in import data: %qs",
1783 imag_str.c_str());
1784 return Expression::make_error(imp->location());
1786 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1787 imp->location());
1788 mpfr_clear(real);
1789 mpfr_clear(imag);
1790 return ret;
1792 else if (num.find('.') == std::string::npos
1793 && num.find('E') == std::string::npos)
1795 mpz_t val;
1796 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1798 error_at(imp->location(), "bad number in import data: %qs",
1799 num.c_str());
1800 return Expression::make_error(imp->location());
1802 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1803 mpz_clear(val);
1804 return ret;
1806 else
1808 mpfr_t val;
1809 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1811 error_at(imp->location(), "bad number in import data: %qs",
1812 num.c_str());
1813 return Expression::make_error(imp->location());
1815 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1816 mpfr_clear(val);
1817 return ret;
1821 // Build a new integer value.
1823 Expression*
1824 Expression::make_integer(const mpz_t* val, Type* type,
1825 source_location location)
1827 return new Integer_expression(val, type, location);
1830 // Floats.
1832 class Float_expression : public Expression
1834 public:
1835 Float_expression(const mpfr_t* val, Type* type, source_location location)
1836 : Expression(EXPRESSION_FLOAT, location),
1837 type_(type)
1839 mpfr_init_set(this->val_, *val, GMP_RNDN);
1842 // Constrain VAL to fit into TYPE.
1843 static void
1844 constrain_float(mpfr_t val, Type* type);
1846 // Return whether VAL fits in the type.
1847 static bool
1848 check_constant(mpfr_t val, Type*, source_location);
1850 // Write VAL to export data.
1851 static void
1852 export_float(Export* exp, const mpfr_t val);
1854 protected:
1855 bool
1856 do_is_constant() const
1857 { return true; }
1859 bool
1860 do_float_constant_value(mpfr_t val, Type**) const;
1862 Type*
1863 do_type();
1865 void
1866 do_determine_type(const Type_context*);
1868 void
1869 do_check_types(Gogo*);
1871 Expression*
1872 do_copy()
1873 { return Expression::make_float(&this->val_, this->type_,
1874 this->location()); }
1876 tree
1877 do_get_tree(Translate_context*);
1879 void
1880 do_export(Export*) const;
1882 private:
1883 // The floating point value.
1884 mpfr_t val_;
1885 // The type so far.
1886 Type* type_;
1889 // Constrain VAL to fit into TYPE.
1891 void
1892 Float_expression::constrain_float(mpfr_t val, Type* type)
1894 Float_type* ftype = type->float_type();
1895 if (ftype != NULL && !ftype->is_abstract())
1897 tree type_tree = ftype->type_tree();
1898 REAL_VALUE_TYPE rvt;
1899 real_from_mpfr(&rvt, val, type_tree, GMP_RNDN);
1900 real_convert(&rvt, TYPE_MODE(type_tree), &rvt);
1901 mpfr_from_real(val, &rvt, GMP_RNDN);
1905 // Return a floating point constant value.
1907 bool
1908 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1910 if (this->type_ != NULL)
1911 *ptype = this->type_;
1912 mpfr_set(val, this->val_, GMP_RNDN);
1913 return true;
1916 // Return the current type. If we haven't set the type yet, we return
1917 // an abstract float type.
1919 Type*
1920 Float_expression::do_type()
1922 if (this->type_ == NULL)
1923 this->type_ = Type::make_abstract_float_type();
1924 return this->type_;
1927 // Set the type of the float value. Here we may switch from an
1928 // abstract type to a real type.
1930 void
1931 Float_expression::do_determine_type(const Type_context* context)
1933 if (this->type_ != NULL && !this->type_->is_abstract())
1935 else if (context->type != NULL
1936 && (context->type->integer_type() != NULL
1937 || context->type->float_type() != NULL
1938 || context->type->complex_type() != NULL))
1939 this->type_ = context->type;
1940 else if (!context->may_be_abstract)
1941 this->type_ = Type::lookup_float_type("float64");
1944 // Return true if the floating point value VAL fits in the range of
1945 // the type TYPE. Otherwise give an error and return false. TYPE may
1946 // be NULL.
1948 bool
1949 Float_expression::check_constant(mpfr_t val, Type* type,
1950 source_location location)
1952 if (type == NULL)
1953 return true;
1954 Float_type* ftype = type->float_type();
1955 if (ftype == NULL || ftype->is_abstract())
1956 return true;
1958 // A NaN or Infinity always fits in the range of the type.
1959 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1960 return true;
1962 mp_exp_t exp = mpfr_get_exp(val);
1963 mp_exp_t max_exp;
1964 switch (ftype->bits())
1966 case 32:
1967 max_exp = 128;
1968 break;
1969 case 64:
1970 max_exp = 1024;
1971 break;
1972 default:
1973 gcc_unreachable();
1975 if (exp > max_exp)
1977 error_at(location, "floating point constant overflow");
1978 return false;
1980 return true;
1983 // Check the type of a float value.
1985 void
1986 Float_expression::do_check_types(Gogo*)
1988 if (this->type_ == NULL)
1989 return;
1991 if (!Float_expression::check_constant(this->val_, this->type_,
1992 this->location()))
1993 this->set_is_error();
1995 Integer_type* integer_type = this->type_->integer_type();
1996 if (integer_type != NULL)
1998 if (!mpfr_integer_p(this->val_))
1999 this->report_error(_("floating point constant truncated to integer"));
2000 else
2002 gcc_assert(!integer_type->is_abstract());
2003 mpz_t ival;
2004 mpz_init(ival);
2005 mpfr_get_z(ival, this->val_, GMP_RNDN);
2006 Integer_expression::check_constant(ival, integer_type,
2007 this->location());
2008 mpz_clear(ival);
2013 // Get a tree for a float constant.
2015 tree
2016 Float_expression::do_get_tree(Translate_context* context)
2018 Gogo* gogo = context->gogo();
2019 tree type;
2020 if (this->type_ != NULL && !this->type_->is_abstract())
2021 type = this->type_->get_tree(gogo);
2022 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2024 // We have an abstract integer type. We just hope for the best.
2025 type = Type::lookup_integer_type("int")->get_tree(gogo);
2027 else
2029 // If we still have an abstract type here, then this is being
2030 // used in a constant expression which didn't get reduced. We
2031 // just use float64 and hope for the best.
2032 type = Type::lookup_float_type("float64")->get_tree(gogo);
2034 return Expression::float_constant_tree(this->val_, type);
2037 // Write a floating point number to export data.
2039 void
2040 Float_expression::export_float(Export *exp, const mpfr_t val)
2042 mp_exp_t exponent;
2043 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2044 if (*s == '-')
2045 exp->write_c_string("-");
2046 exp->write_c_string("0.");
2047 exp->write_c_string(*s == '-' ? s + 1 : s);
2048 mpfr_free_str(s);
2049 char buf[30];
2050 snprintf(buf, sizeof buf, "E%ld", exponent);
2051 exp->write_c_string(buf);
2054 // Export a floating point number in a constant expression.
2056 void
2057 Float_expression::do_export(Export* exp) const
2059 Float_expression::export_float(exp, this->val_);
2060 // A trailing space lets us reliably identify the end of the number.
2061 exp->write_c_string(" ");
2064 // Make a float expression.
2066 Expression*
2067 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2069 return new Float_expression(val, type, location);
2072 // Complex numbers.
2074 class Complex_expression : public Expression
2076 public:
2077 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2078 source_location location)
2079 : Expression(EXPRESSION_COMPLEX, location),
2080 type_(type)
2082 mpfr_init_set(this->real_, *real, GMP_RNDN);
2083 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2086 // Constrain REAL/IMAG to fit into TYPE.
2087 static void
2088 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2090 // Return whether REAL/IMAG fits in the type.
2091 static bool
2092 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2094 // Write REAL/IMAG to export data.
2095 static void
2096 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2098 protected:
2099 bool
2100 do_is_constant() const
2101 { return true; }
2103 bool
2104 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2106 Type*
2107 do_type();
2109 void
2110 do_determine_type(const Type_context*);
2112 void
2113 do_check_types(Gogo*);
2115 Expression*
2116 do_copy()
2118 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2119 this->location());
2122 tree
2123 do_get_tree(Translate_context*);
2125 void
2126 do_export(Export*) const;
2128 private:
2129 // The real part.
2130 mpfr_t real_;
2131 // The imaginary part;
2132 mpfr_t imag_;
2133 // The type if known.
2134 Type* type_;
2137 // Constrain REAL/IMAG to fit into TYPE.
2139 void
2140 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2142 Complex_type* ctype = type->complex_type();
2143 if (ctype != NULL && !ctype->is_abstract())
2145 tree type_tree = ctype->type_tree();
2147 REAL_VALUE_TYPE rvt;
2148 real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN);
2149 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2150 mpfr_from_real(real, &rvt, GMP_RNDN);
2152 real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN);
2153 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2154 mpfr_from_real(imag, &rvt, GMP_RNDN);
2158 // Return a complex constant value.
2160 bool
2161 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2162 Type** ptype) const
2164 if (this->type_ != NULL)
2165 *ptype = this->type_;
2166 mpfr_set(real, this->real_, GMP_RNDN);
2167 mpfr_set(imag, this->imag_, GMP_RNDN);
2168 return true;
2171 // Return the current type. If we haven't set the type yet, we return
2172 // an abstract complex type.
2174 Type*
2175 Complex_expression::do_type()
2177 if (this->type_ == NULL)
2178 this->type_ = Type::make_abstract_complex_type();
2179 return this->type_;
2182 // Set the type of the complex value. Here we may switch from an
2183 // abstract type to a real type.
2185 void
2186 Complex_expression::do_determine_type(const Type_context* context)
2188 if (this->type_ != NULL && !this->type_->is_abstract())
2190 else if (context->type != NULL
2191 && context->type->complex_type() != NULL)
2192 this->type_ = context->type;
2193 else if (!context->may_be_abstract)
2194 this->type_ = Type::lookup_complex_type("complex128");
2197 // Return true if the complex value REAL/IMAG fits in the range of the
2198 // type TYPE. Otherwise give an error and return false. TYPE may be
2199 // NULL.
2201 bool
2202 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2203 source_location location)
2205 if (type == NULL)
2206 return true;
2207 Complex_type* ctype = type->complex_type();
2208 if (ctype == NULL || ctype->is_abstract())
2209 return true;
2211 mp_exp_t max_exp;
2212 switch (ctype->bits())
2214 case 64:
2215 max_exp = 128;
2216 break;
2217 case 128:
2218 max_exp = 1024;
2219 break;
2220 default:
2221 gcc_unreachable();
2224 // A NaN or Infinity always fits in the range of the type.
2225 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2227 if (mpfr_get_exp(real) > max_exp)
2229 error_at(location, "complex real part constant overflow");
2230 return false;
2234 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2236 if (mpfr_get_exp(imag) > max_exp)
2238 error_at(location, "complex imaginary part constant overflow");
2239 return false;
2243 return true;
2246 // Check the type of a complex value.
2248 void
2249 Complex_expression::do_check_types(Gogo*)
2251 if (this->type_ == NULL)
2252 return;
2254 if (!Complex_expression::check_constant(this->real_, this->imag_,
2255 this->type_, this->location()))
2256 this->set_is_error();
2259 // Get a tree for a complex constant.
2261 tree
2262 Complex_expression::do_get_tree(Translate_context* context)
2264 Gogo* gogo = context->gogo();
2265 tree type;
2266 if (this->type_ != NULL && !this->type_->is_abstract())
2267 type = this->type_->get_tree(gogo);
2268 else
2270 // If we still have an abstract type here, this this is being
2271 // used in a constant expression which didn't get reduced. We
2272 // just use complex128 and hope for the best.
2273 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
2275 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2278 // Write REAL/IMAG to export data.
2280 void
2281 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2282 const mpfr_t imag)
2284 if (!mpfr_zero_p(real))
2286 Float_expression::export_float(exp, real);
2287 if (mpfr_sgn(imag) > 0)
2288 exp->write_c_string("+");
2290 Float_expression::export_float(exp, imag);
2291 exp->write_c_string("i");
2294 // Export a complex number in a constant expression.
2296 void
2297 Complex_expression::do_export(Export* exp) const
2299 Complex_expression::export_complex(exp, this->real_, this->imag_);
2300 // A trailing space lets us reliably identify the end of the number.
2301 exp->write_c_string(" ");
2304 // Make a complex expression.
2306 Expression*
2307 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2308 source_location location)
2310 return new Complex_expression(real, imag, type, location);
2313 // Find a named object in an expression.
2315 class Find_named_object : public Traverse
2317 public:
2318 Find_named_object(Named_object* no)
2319 : Traverse(traverse_expressions),
2320 no_(no), found_(false)
2323 // Whether we found the object.
2324 bool
2325 found() const
2326 { return this->found_; }
2328 protected:
2330 expression(Expression**);
2332 private:
2333 // The object we are looking for.
2334 Named_object* no_;
2335 // Whether we found it.
2336 bool found_;
2339 // A reference to a const in an expression.
2341 class Const_expression : public Expression
2343 public:
2344 Const_expression(Named_object* constant, source_location location)
2345 : Expression(EXPRESSION_CONST_REFERENCE, location),
2346 constant_(constant), type_(NULL), seen_(false)
2349 Named_object*
2350 named_object()
2351 { return this->constant_; }
2353 const std::string&
2354 name() const
2355 { return this->constant_->name(); }
2357 // Check that the initializer does not refer to the constant itself.
2358 void
2359 check_for_init_loop();
2361 protected:
2363 do_traverse(Traverse*);
2365 Expression*
2366 do_lower(Gogo*, Named_object*, int);
2368 bool
2369 do_is_constant() const
2370 { return true; }
2372 bool
2373 do_integer_constant_value(bool, mpz_t val, Type**) const;
2375 bool
2376 do_float_constant_value(mpfr_t val, Type**) const;
2378 bool
2379 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2381 bool
2382 do_string_constant_value(std::string* val) const
2383 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2385 Type*
2386 do_type();
2388 // The type of a const is set by the declaration, not the use.
2389 void
2390 do_determine_type(const Type_context*);
2392 void
2393 do_check_types(Gogo*);
2395 Expression*
2396 do_copy()
2397 { return this; }
2399 tree
2400 do_get_tree(Translate_context* context);
2402 // When exporting a reference to a const as part of a const
2403 // expression, we export the value. We ignore the fact that it has
2404 // a name.
2405 void
2406 do_export(Export* exp) const
2407 { this->constant_->const_value()->expr()->export_expression(exp); }
2409 private:
2410 // The constant.
2411 Named_object* constant_;
2412 // The type of this reference. This is used if the constant has an
2413 // abstract type.
2414 Type* type_;
2415 // Used to prevent infinite recursion when a constant incorrectly
2416 // refers to itself.
2417 mutable bool seen_;
2420 // Traversal.
2423 Const_expression::do_traverse(Traverse* traverse)
2425 if (this->type_ != NULL)
2426 return Type::traverse(this->type_, traverse);
2427 return TRAVERSE_CONTINUE;
2430 // Lower a constant expression. This is where we convert the
2431 // predeclared constant iota into an integer value.
2433 Expression*
2434 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2436 if (this->constant_->const_value()->expr()->classification()
2437 == EXPRESSION_IOTA)
2439 if (iota_value == -1)
2441 error_at(this->location(),
2442 "iota is only defined in const declarations");
2443 iota_value = 0;
2445 mpz_t val;
2446 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2447 Expression* ret = Expression::make_integer(&val, NULL,
2448 this->location());
2449 mpz_clear(val);
2450 return ret;
2453 // Make sure that the constant itself has been lowered.
2454 gogo->lower_constant(this->constant_);
2456 return this;
2459 // Return an integer constant value.
2461 bool
2462 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2463 Type** ptype) const
2465 if (this->seen_)
2466 return false;
2468 Type* ctype;
2469 if (this->type_ != NULL)
2470 ctype = this->type_;
2471 else
2472 ctype = this->constant_->const_value()->type();
2473 if (ctype != NULL && ctype->integer_type() == NULL)
2474 return false;
2476 Expression* e = this->constant_->const_value()->expr();
2478 this->seen_ = true;
2480 Type* t;
2481 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2483 this->seen_ = false;
2485 if (r
2486 && ctype != NULL
2487 && !Integer_expression::check_constant(val, ctype, this->location()))
2488 return false;
2490 *ptype = ctype != NULL ? ctype : t;
2491 return r;
2494 // Return a floating point constant value.
2496 bool
2497 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2499 if (this->seen_)
2500 return false;
2502 Type* ctype;
2503 if (this->type_ != NULL)
2504 ctype = this->type_;
2505 else
2506 ctype = this->constant_->const_value()->type();
2507 if (ctype != NULL && ctype->float_type() == NULL)
2508 return false;
2510 this->seen_ = true;
2512 Type* t;
2513 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2514 &t);
2516 this->seen_ = false;
2518 if (r && ctype != NULL)
2520 if (!Float_expression::check_constant(val, ctype, this->location()))
2521 return false;
2522 Float_expression::constrain_float(val, ctype);
2524 *ptype = ctype != NULL ? ctype : t;
2525 return r;
2528 // Return a complex constant value.
2530 bool
2531 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2532 Type **ptype) const
2534 if (this->seen_)
2535 return false;
2537 Type* ctype;
2538 if (this->type_ != NULL)
2539 ctype = this->type_;
2540 else
2541 ctype = this->constant_->const_value()->type();
2542 if (ctype != NULL && ctype->complex_type() == NULL)
2543 return false;
2545 this->seen_ = true;
2547 Type *t;
2548 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2549 imag,
2550 &t);
2552 this->seen_ = false;
2554 if (r && ctype != NULL)
2556 if (!Complex_expression::check_constant(real, imag, ctype,
2557 this->location()))
2558 return false;
2559 Complex_expression::constrain_complex(real, imag, ctype);
2561 *ptype = ctype != NULL ? ctype : t;
2562 return r;
2565 // Return the type of the const reference.
2567 Type*
2568 Const_expression::do_type()
2570 if (this->type_ != NULL)
2571 return this->type_;
2573 Named_constant* nc = this->constant_->const_value();
2575 if (this->seen_ || nc->lowering())
2577 this->report_error(_("constant refers to itself"));
2578 this->type_ = Type::make_error_type();
2579 return this->type_;
2582 this->seen_ = true;
2584 Type* ret = nc->type();
2586 if (ret != NULL)
2588 this->seen_ = false;
2589 return ret;
2592 // During parsing, a named constant may have a NULL type, but we
2593 // must not return a NULL type here.
2594 ret = nc->expr()->type();
2596 this->seen_ = false;
2598 return ret;
2601 // Set the type of the const reference.
2603 void
2604 Const_expression::do_determine_type(const Type_context* context)
2606 Type* ctype = this->constant_->const_value()->type();
2607 Type* cetype = (ctype != NULL
2608 ? ctype
2609 : this->constant_->const_value()->expr()->type());
2610 if (ctype != NULL && !ctype->is_abstract())
2612 else if (context->type != NULL
2613 && (context->type->integer_type() != NULL
2614 || context->type->float_type() != NULL
2615 || context->type->complex_type() != NULL)
2616 && (cetype->integer_type() != NULL
2617 || cetype->float_type() != NULL
2618 || cetype->complex_type() != NULL))
2619 this->type_ = context->type;
2620 else if (context->type != NULL
2621 && context->type->is_string_type()
2622 && cetype->is_string_type())
2623 this->type_ = context->type;
2624 else if (context->type != NULL
2625 && context->type->is_boolean_type()
2626 && cetype->is_boolean_type())
2627 this->type_ = context->type;
2628 else if (!context->may_be_abstract)
2630 if (cetype->is_abstract())
2631 cetype = cetype->make_non_abstract_type();
2632 this->type_ = cetype;
2636 // Check for a loop in which the initializer of a constant refers to
2637 // the constant itself.
2639 void
2640 Const_expression::check_for_init_loop()
2642 if (this->type_ != NULL && this->type_->is_error_type())
2643 return;
2645 if (this->seen_)
2647 this->report_error(_("constant refers to itself"));
2648 this->type_ = Type::make_error_type();
2649 return;
2652 Expression* init = this->constant_->const_value()->expr();
2653 Find_named_object find_named_object(this->constant_);
2655 this->seen_ = true;
2656 Expression::traverse(&init, &find_named_object);
2657 this->seen_ = false;
2659 if (find_named_object.found())
2661 if (this->type_ == NULL || !this->type_->is_error_type())
2663 this->report_error(_("constant refers to itself"));
2664 this->type_ = Type::make_error_type();
2666 return;
2670 // Check types of a const reference.
2672 void
2673 Const_expression::do_check_types(Gogo*)
2675 if (this->type_ != NULL && this->type_->is_error_type())
2676 return;
2678 this->check_for_init_loop();
2680 if (this->type_ == NULL || this->type_->is_abstract())
2681 return;
2683 // Check for integer overflow.
2684 if (this->type_->integer_type() != NULL)
2686 mpz_t ival;
2687 mpz_init(ival);
2688 Type* dummy;
2689 if (!this->integer_constant_value(true, ival, &dummy))
2691 mpfr_t fval;
2692 mpfr_init(fval);
2693 Expression* cexpr = this->constant_->const_value()->expr();
2694 if (cexpr->float_constant_value(fval, &dummy))
2696 if (!mpfr_integer_p(fval))
2697 this->report_error(_("floating point constant "
2698 "truncated to integer"));
2699 else
2701 mpfr_get_z(ival, fval, GMP_RNDN);
2702 Integer_expression::check_constant(ival, this->type_,
2703 this->location());
2706 mpfr_clear(fval);
2708 mpz_clear(ival);
2712 // Return a tree for the const reference.
2714 tree
2715 Const_expression::do_get_tree(Translate_context* context)
2717 Gogo* gogo = context->gogo();
2718 tree type_tree;
2719 if (this->type_ == NULL)
2720 type_tree = NULL_TREE;
2721 else
2723 type_tree = this->type_->get_tree(gogo);
2724 if (type_tree == error_mark_node)
2725 return error_mark_node;
2728 // If the type has been set for this expression, but the underlying
2729 // object is an abstract int or float, we try to get the abstract
2730 // value. Otherwise we may lose something in the conversion.
2731 if (this->type_ != NULL
2732 && (this->constant_->const_value()->type() == NULL
2733 || this->constant_->const_value()->type()->is_abstract()))
2735 Expression* expr = this->constant_->const_value()->expr();
2736 mpz_t ival;
2737 mpz_init(ival);
2738 Type* t;
2739 if (expr->integer_constant_value(true, ival, &t))
2741 tree ret = Expression::integer_constant_tree(ival, type_tree);
2742 mpz_clear(ival);
2743 return ret;
2745 mpz_clear(ival);
2747 mpfr_t fval;
2748 mpfr_init(fval);
2749 if (expr->float_constant_value(fval, &t))
2751 tree ret = Expression::float_constant_tree(fval, type_tree);
2752 mpfr_clear(fval);
2753 return ret;
2756 mpfr_t imag;
2757 mpfr_init(imag);
2758 if (expr->complex_constant_value(fval, imag, &t))
2760 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2761 mpfr_clear(fval);
2762 mpfr_clear(imag);
2763 return ret;
2765 mpfr_clear(imag);
2766 mpfr_clear(fval);
2769 tree const_tree = this->constant_->get_tree(gogo, context->function());
2770 if (this->type_ == NULL
2771 || const_tree == error_mark_node
2772 || TREE_TYPE(const_tree) == error_mark_node)
2773 return const_tree;
2775 tree ret;
2776 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2777 ret = fold_convert(type_tree, const_tree);
2778 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2779 ret = fold(convert_to_integer(type_tree, const_tree));
2780 else if (TREE_CODE(type_tree) == REAL_TYPE)
2781 ret = fold(convert_to_real(type_tree, const_tree));
2782 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2783 ret = fold(convert_to_complex(type_tree, const_tree));
2784 else
2785 gcc_unreachable();
2786 return ret;
2789 // Make a reference to a constant in an expression.
2791 Expression*
2792 Expression::make_const_reference(Named_object* constant,
2793 source_location location)
2795 return new Const_expression(constant, location);
2798 // Find a named object in an expression.
2801 Find_named_object::expression(Expression** pexpr)
2803 switch ((*pexpr)->classification())
2805 case Expression::EXPRESSION_CONST_REFERENCE:
2807 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2808 if (ce->named_object() == this->no_)
2809 break;
2811 // We need to check a constant initializer explicitly, as
2812 // loops here will not be caught by the loop checking for
2813 // variable initializers.
2814 ce->check_for_init_loop();
2816 return TRAVERSE_CONTINUE;
2819 case Expression::EXPRESSION_VAR_REFERENCE:
2820 if ((*pexpr)->var_expression()->named_object() == this->no_)
2821 break;
2822 return TRAVERSE_CONTINUE;
2823 case Expression::EXPRESSION_FUNC_REFERENCE:
2824 if ((*pexpr)->func_expression()->named_object() == this->no_)
2825 break;
2826 return TRAVERSE_CONTINUE;
2827 default:
2828 return TRAVERSE_CONTINUE;
2830 this->found_ = true;
2831 return TRAVERSE_EXIT;
2834 // The nil value.
2836 class Nil_expression : public Expression
2838 public:
2839 Nil_expression(source_location location)
2840 : Expression(EXPRESSION_NIL, location)
2843 static Expression*
2844 do_import(Import*);
2846 protected:
2847 bool
2848 do_is_constant() const
2849 { return true; }
2851 Type*
2852 do_type()
2853 { return Type::make_nil_type(); }
2855 void
2856 do_determine_type(const Type_context*)
2859 Expression*
2860 do_copy()
2861 { return this; }
2863 tree
2864 do_get_tree(Translate_context*)
2865 { return null_pointer_node; }
2867 void
2868 do_export(Export* exp) const
2869 { exp->write_c_string("nil"); }
2872 // Import a nil expression.
2874 Expression*
2875 Nil_expression::do_import(Import* imp)
2877 imp->require_c_string("nil");
2878 return Expression::make_nil(imp->location());
2881 // Make a nil expression.
2883 Expression*
2884 Expression::make_nil(source_location location)
2886 return new Nil_expression(location);
2889 // The value of the predeclared constant iota. This is little more
2890 // than a marker. This will be lowered to an integer in
2891 // Const_expression::do_lower, which is where we know the value that
2892 // it should have.
2894 class Iota_expression : public Parser_expression
2896 public:
2897 Iota_expression(source_location location)
2898 : Parser_expression(EXPRESSION_IOTA, location)
2901 protected:
2902 Expression*
2903 do_lower(Gogo*, Named_object*, int)
2904 { gcc_unreachable(); }
2906 // There should only ever be one of these.
2907 Expression*
2908 do_copy()
2909 { gcc_unreachable(); }
2912 // Make an iota expression. This is only called for one case: the
2913 // value of the predeclared constant iota.
2915 Expression*
2916 Expression::make_iota()
2918 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2919 return &iota_expression;
2922 // A type conversion expression.
2924 class Type_conversion_expression : public Expression
2926 public:
2927 Type_conversion_expression(Type* type, Expression* expr,
2928 source_location location)
2929 : Expression(EXPRESSION_CONVERSION, location),
2930 type_(type), expr_(expr), may_convert_function_types_(false)
2933 // Return the type to which we are converting.
2934 Type*
2935 type() const
2936 { return this->type_; }
2938 // Return the expression which we are converting.
2939 Expression*
2940 expr() const
2941 { return this->expr_; }
2943 // Permit converting from one function type to another. This is
2944 // used internally for method expressions.
2945 void
2946 set_may_convert_function_types()
2948 this->may_convert_function_types_ = true;
2951 // Import a type conversion expression.
2952 static Expression*
2953 do_import(Import*);
2955 protected:
2957 do_traverse(Traverse* traverse);
2959 Expression*
2960 do_lower(Gogo*, Named_object*, int);
2962 bool
2963 do_is_constant() const
2964 { return this->expr_->is_constant(); }
2966 bool
2967 do_integer_constant_value(bool, mpz_t, Type**) const;
2969 bool
2970 do_float_constant_value(mpfr_t, Type**) const;
2972 bool
2973 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2975 bool
2976 do_string_constant_value(std::string*) const;
2978 Type*
2979 do_type()
2980 { return this->type_; }
2982 void
2983 do_determine_type(const Type_context*)
2985 Type_context subcontext(this->type_, false);
2986 this->expr_->determine_type(&subcontext);
2989 void
2990 do_check_types(Gogo*);
2992 Expression*
2993 do_copy()
2995 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2996 this->location());
2999 tree
3000 do_get_tree(Translate_context* context);
3002 void
3003 do_export(Export*) const;
3005 private:
3006 // The type to convert to.
3007 Type* type_;
3008 // The expression to convert.
3009 Expression* expr_;
3010 // True if this is permitted to convert function types. This is
3011 // used internally for method expressions.
3012 bool may_convert_function_types_;
3015 // Traversal.
3018 Type_conversion_expression::do_traverse(Traverse* traverse)
3020 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3021 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3022 return TRAVERSE_EXIT;
3023 return TRAVERSE_CONTINUE;
3026 // Convert to a constant at lowering time.
3028 Expression*
3029 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
3031 Type* type = this->type_;
3032 Expression* val = this->expr_;
3033 source_location location = this->location();
3035 if (type->integer_type() != NULL)
3037 mpz_t ival;
3038 mpz_init(ival);
3039 Type* dummy;
3040 if (val->integer_constant_value(false, ival, &dummy))
3042 if (!Integer_expression::check_constant(ival, type, location))
3043 mpz_set_ui(ival, 0);
3044 Expression* ret = Expression::make_integer(&ival, type, location);
3045 mpz_clear(ival);
3046 return ret;
3049 mpfr_t fval;
3050 mpfr_init(fval);
3051 if (val->float_constant_value(fval, &dummy))
3053 if (!mpfr_integer_p(fval))
3055 error_at(location,
3056 "floating point constant truncated to integer");
3057 return Expression::make_error(location);
3059 mpfr_get_z(ival, fval, GMP_RNDN);
3060 if (!Integer_expression::check_constant(ival, type, location))
3061 mpz_set_ui(ival, 0);
3062 Expression* ret = Expression::make_integer(&ival, type, location);
3063 mpfr_clear(fval);
3064 mpz_clear(ival);
3065 return ret;
3067 mpfr_clear(fval);
3068 mpz_clear(ival);
3071 if (type->float_type() != NULL)
3073 mpfr_t fval;
3074 mpfr_init(fval);
3075 Type* dummy;
3076 if (val->float_constant_value(fval, &dummy))
3078 if (!Float_expression::check_constant(fval, type, location))
3079 mpfr_set_ui(fval, 0, GMP_RNDN);
3080 Float_expression::constrain_float(fval, type);
3081 Expression *ret = Expression::make_float(&fval, type, location);
3082 mpfr_clear(fval);
3083 return ret;
3085 mpfr_clear(fval);
3088 if (type->complex_type() != NULL)
3090 mpfr_t real;
3091 mpfr_t imag;
3092 mpfr_init(real);
3093 mpfr_init(imag);
3094 Type* dummy;
3095 if (val->complex_constant_value(real, imag, &dummy))
3097 if (!Complex_expression::check_constant(real, imag, type, location))
3099 mpfr_set_ui(real, 0, GMP_RNDN);
3100 mpfr_set_ui(imag, 0, GMP_RNDN);
3102 Complex_expression::constrain_complex(real, imag, type);
3103 Expression* ret = Expression::make_complex(&real, &imag, type,
3104 location);
3105 mpfr_clear(real);
3106 mpfr_clear(imag);
3107 return ret;
3109 mpfr_clear(real);
3110 mpfr_clear(imag);
3113 if (type->is_open_array_type() && type->named_type() == NULL)
3115 Type* element_type = type->array_type()->element_type()->forwarded();
3116 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3117 bool is_int = element_type == Type::lookup_integer_type("int");
3118 if (is_byte || is_int)
3120 std::string s;
3121 if (val->string_constant_value(&s))
3123 Expression_list* vals = new Expression_list();
3124 if (is_byte)
3126 for (std::string::const_iterator p = s.begin();
3127 p != s.end();
3128 p++)
3130 mpz_t val;
3131 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3132 Expression* v = Expression::make_integer(&val,
3133 element_type,
3134 location);
3135 vals->push_back(v);
3136 mpz_clear(val);
3139 else
3141 const char *p = s.data();
3142 const char *pend = s.data() + s.length();
3143 while (p < pend)
3145 unsigned int c;
3146 int adv = Lex::fetch_char(p, &c);
3147 if (adv == 0)
3149 warning_at(this->location(), 0,
3150 "invalid UTF-8 encoding");
3151 adv = 1;
3153 p += adv;
3154 mpz_t val;
3155 mpz_init_set_ui(val, c);
3156 Expression* v = Expression::make_integer(&val,
3157 element_type,
3158 location);
3159 vals->push_back(v);
3160 mpz_clear(val);
3164 return Expression::make_slice_composite_literal(type, vals,
3165 location);
3170 return this;
3173 // Return the constant integer value if there is one.
3175 bool
3176 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3177 mpz_t val,
3178 Type** ptype) const
3180 if (this->type_->integer_type() == NULL)
3181 return false;
3183 mpz_t ival;
3184 mpz_init(ival);
3185 Type* dummy;
3186 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3188 if (!Integer_expression::check_constant(ival, this->type_,
3189 this->location()))
3191 mpz_clear(ival);
3192 return false;
3194 mpz_set(val, ival);
3195 mpz_clear(ival);
3196 *ptype = this->type_;
3197 return true;
3199 mpz_clear(ival);
3201 mpfr_t fval;
3202 mpfr_init(fval);
3203 if (this->expr_->float_constant_value(fval, &dummy))
3205 mpfr_get_z(val, fval, GMP_RNDN);
3206 mpfr_clear(fval);
3207 if (!Integer_expression::check_constant(val, this->type_,
3208 this->location()))
3209 return false;
3210 *ptype = this->type_;
3211 return true;
3213 mpfr_clear(fval);
3215 return false;
3218 // Return the constant floating point value if there is one.
3220 bool
3221 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3222 Type** ptype) const
3224 if (this->type_->float_type() == NULL)
3225 return false;
3227 mpfr_t fval;
3228 mpfr_init(fval);
3229 Type* dummy;
3230 if (this->expr_->float_constant_value(fval, &dummy))
3232 if (!Float_expression::check_constant(fval, this->type_,
3233 this->location()))
3235 mpfr_clear(fval);
3236 return false;
3238 mpfr_set(val, fval, GMP_RNDN);
3239 mpfr_clear(fval);
3240 Float_expression::constrain_float(val, this->type_);
3241 *ptype = this->type_;
3242 return true;
3244 mpfr_clear(fval);
3246 return false;
3249 // Return the constant complex value if there is one.
3251 bool
3252 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3253 mpfr_t imag,
3254 Type **ptype) const
3256 if (this->type_->complex_type() == NULL)
3257 return false;
3259 mpfr_t rval;
3260 mpfr_t ival;
3261 mpfr_init(rval);
3262 mpfr_init(ival);
3263 Type* dummy;
3264 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3266 if (!Complex_expression::check_constant(rval, ival, this->type_,
3267 this->location()))
3269 mpfr_clear(rval);
3270 mpfr_clear(ival);
3271 return false;
3273 mpfr_set(real, rval, GMP_RNDN);
3274 mpfr_set(imag, ival, GMP_RNDN);
3275 mpfr_clear(rval);
3276 mpfr_clear(ival);
3277 Complex_expression::constrain_complex(real, imag, this->type_);
3278 *ptype = this->type_;
3279 return true;
3281 mpfr_clear(rval);
3282 mpfr_clear(ival);
3284 return false;
3287 // Return the constant string value if there is one.
3289 bool
3290 Type_conversion_expression::do_string_constant_value(std::string* val) const
3292 if (this->type_->is_string_type()
3293 && this->expr_->type()->integer_type() != NULL)
3295 mpz_t ival;
3296 mpz_init(ival);
3297 Type* dummy;
3298 if (this->expr_->integer_constant_value(false, ival, &dummy))
3300 unsigned long ulval = mpz_get_ui(ival);
3301 if (mpz_cmp_ui(ival, ulval) == 0)
3303 Lex::append_char(ulval, true, val, this->location());
3304 mpz_clear(ival);
3305 return true;
3308 mpz_clear(ival);
3311 // FIXME: Could handle conversion from const []int here.
3313 return false;
3316 // Check that types are convertible.
3318 void
3319 Type_conversion_expression::do_check_types(Gogo*)
3321 Type* type = this->type_;
3322 Type* expr_type = this->expr_->type();
3323 std::string reason;
3325 if (type->is_error_type()
3326 || type->is_undefined()
3327 || expr_type->is_error_type()
3328 || expr_type->is_undefined())
3330 // Make sure we emit an error for an undefined type.
3331 type->base();
3332 expr_type->base();
3333 this->set_is_error();
3334 return;
3337 if (this->may_convert_function_types_
3338 && type->function_type() != NULL
3339 && expr_type->function_type() != NULL)
3340 return;
3342 if (Type::are_convertible(type, expr_type, &reason))
3343 return;
3345 error_at(this->location(), "%s", reason.c_str());
3346 this->set_is_error();
3349 // Get a tree for a type conversion.
3351 tree
3352 Type_conversion_expression::do_get_tree(Translate_context* context)
3354 Gogo* gogo = context->gogo();
3355 tree type_tree = this->type_->get_tree(gogo);
3356 tree expr_tree = this->expr_->get_tree(context);
3358 if (type_tree == error_mark_node
3359 || expr_tree == error_mark_node
3360 || TREE_TYPE(expr_tree) == error_mark_node)
3361 return error_mark_node;
3363 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3364 return fold_convert(type_tree, expr_tree);
3366 Type* type = this->type_;
3367 Type* expr_type = this->expr_->type();
3368 tree ret;
3369 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3370 ret = Expression::convert_for_assignment(context, type, expr_type,
3371 expr_tree, this->location());
3372 else if (type->integer_type() != NULL)
3374 if (expr_type->integer_type() != NULL
3375 || expr_type->float_type() != NULL
3376 || expr_type->is_unsafe_pointer_type())
3377 ret = fold(convert_to_integer(type_tree, expr_tree));
3378 else
3379 gcc_unreachable();
3381 else if (type->float_type() != NULL)
3383 if (expr_type->integer_type() != NULL
3384 || expr_type->float_type() != NULL)
3385 ret = fold(convert_to_real(type_tree, expr_tree));
3386 else
3387 gcc_unreachable();
3389 else if (type->complex_type() != NULL)
3391 if (expr_type->complex_type() != NULL)
3392 ret = fold(convert_to_complex(type_tree, expr_tree));
3393 else
3394 gcc_unreachable();
3396 else if (type->is_string_type()
3397 && expr_type->integer_type() != NULL)
3399 expr_tree = fold_convert(integer_type_node, expr_tree);
3400 if (host_integerp(expr_tree, 0))
3402 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3403 std::string s;
3404 Lex::append_char(intval, true, &s, this->location());
3405 Expression* se = Expression::make_string(s, this->location());
3406 return se->get_tree(context);
3409 static tree int_to_string_fndecl;
3410 ret = Gogo::call_builtin(&int_to_string_fndecl,
3411 this->location(),
3412 "__go_int_to_string",
3414 type_tree,
3415 integer_type_node,
3416 fold_convert(integer_type_node, expr_tree));
3418 else if (type->is_string_type()
3419 && (expr_type->array_type() != NULL
3420 || (expr_type->points_to() != NULL
3421 && expr_type->points_to()->array_type() != NULL)))
3423 Type* t = expr_type;
3424 if (t->points_to() != NULL)
3426 t = t->points_to();
3427 expr_tree = build_fold_indirect_ref(expr_tree);
3429 if (!DECL_P(expr_tree))
3430 expr_tree = save_expr(expr_tree);
3431 Array_type* a = t->array_type();
3432 Type* e = a->element_type()->forwarded();
3433 gcc_assert(e->integer_type() != NULL);
3434 tree valptr = fold_convert(const_ptr_type_node,
3435 a->value_pointer_tree(gogo, expr_tree));
3436 tree len = a->length_tree(gogo, expr_tree);
3437 len = fold_convert_loc(this->location(), size_type_node, len);
3438 if (e->integer_type()->is_unsigned()
3439 && e->integer_type()->bits() == 8)
3441 static tree byte_array_to_string_fndecl;
3442 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3443 this->location(),
3444 "__go_byte_array_to_string",
3446 type_tree,
3447 const_ptr_type_node,
3448 valptr,
3449 size_type_node,
3450 len);
3452 else
3454 gcc_assert(e == Type::lookup_integer_type("int"));
3455 static tree int_array_to_string_fndecl;
3456 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3457 this->location(),
3458 "__go_int_array_to_string",
3460 type_tree,
3461 const_ptr_type_node,
3462 valptr,
3463 size_type_node,
3464 len);
3467 else if (type->is_open_array_type() && expr_type->is_string_type())
3469 Type* e = type->array_type()->element_type()->forwarded();
3470 gcc_assert(e->integer_type() != NULL);
3471 if (e->integer_type()->is_unsigned()
3472 && e->integer_type()->bits() == 8)
3474 static tree string_to_byte_array_fndecl;
3475 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3476 this->location(),
3477 "__go_string_to_byte_array",
3479 type_tree,
3480 TREE_TYPE(expr_tree),
3481 expr_tree);
3483 else
3485 gcc_assert(e == Type::lookup_integer_type("int"));
3486 static tree string_to_int_array_fndecl;
3487 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3488 this->location(),
3489 "__go_string_to_int_array",
3491 type_tree,
3492 TREE_TYPE(expr_tree),
3493 expr_tree);
3496 else if ((type->is_unsafe_pointer_type()
3497 && expr_type->points_to() != NULL)
3498 || (expr_type->is_unsafe_pointer_type()
3499 && type->points_to() != NULL))
3500 ret = fold_convert(type_tree, expr_tree);
3501 else if (type->is_unsafe_pointer_type()
3502 && expr_type->integer_type() != NULL)
3503 ret = convert_to_pointer(type_tree, expr_tree);
3504 else if (this->may_convert_function_types_
3505 && type->function_type() != NULL
3506 && expr_type->function_type() != NULL)
3507 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3508 else
3509 ret = Expression::convert_for_assignment(context, type, expr_type,
3510 expr_tree, this->location());
3512 return ret;
3515 // Output a type conversion in a constant expression.
3517 void
3518 Type_conversion_expression::do_export(Export* exp) const
3520 exp->write_c_string("convert(");
3521 exp->write_type(this->type_);
3522 exp->write_c_string(", ");
3523 this->expr_->export_expression(exp);
3524 exp->write_c_string(")");
3527 // Import a type conversion or a struct construction.
3529 Expression*
3530 Type_conversion_expression::do_import(Import* imp)
3532 imp->require_c_string("convert(");
3533 Type* type = imp->read_type();
3534 imp->require_c_string(", ");
3535 Expression* val = Expression::import_expression(imp);
3536 imp->require_c_string(")");
3537 return Expression::make_cast(type, val, imp->location());
3540 // Make a type cast expression.
3542 Expression*
3543 Expression::make_cast(Type* type, Expression* val, source_location location)
3545 if (type->is_error_type() || val->is_error_expression())
3546 return Expression::make_error(location);
3547 return new Type_conversion_expression(type, val, location);
3550 // Unary expressions.
3552 class Unary_expression : public Expression
3554 public:
3555 Unary_expression(Operator op, Expression* expr, source_location location)
3556 : Expression(EXPRESSION_UNARY, location),
3557 op_(op), escapes_(true), expr_(expr)
3560 // Return the operator.
3561 Operator
3562 op() const
3563 { return this->op_; }
3565 // Return the operand.
3566 Expression*
3567 operand() const
3568 { return this->expr_; }
3570 // Record that an address expression does not escape.
3571 void
3572 set_does_not_escape()
3574 gcc_assert(this->op_ == OPERATOR_AND);
3575 this->escapes_ = false;
3578 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3579 // could be done, false if not.
3580 static bool
3581 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3582 source_location);
3584 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3585 // could be done, false if not.
3586 static bool
3587 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3589 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3590 // true if this could be done, false if not.
3591 static bool
3592 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3593 mpfr_t imag);
3595 static Expression*
3596 do_import(Import*);
3598 protected:
3600 do_traverse(Traverse* traverse)
3601 { return Expression::traverse(&this->expr_, traverse); }
3603 Expression*
3604 do_lower(Gogo*, Named_object*, int);
3606 bool
3607 do_is_constant() const;
3609 bool
3610 do_integer_constant_value(bool, mpz_t, Type**) const;
3612 bool
3613 do_float_constant_value(mpfr_t, Type**) const;
3615 bool
3616 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3618 Type*
3619 do_type();
3621 void
3622 do_determine_type(const Type_context*);
3624 void
3625 do_check_types(Gogo*);
3627 Expression*
3628 do_copy()
3630 return Expression::make_unary(this->op_, this->expr_->copy(),
3631 this->location());
3634 bool
3635 do_is_addressable() const
3636 { return this->op_ == OPERATOR_MULT; }
3638 tree
3639 do_get_tree(Translate_context*);
3641 void
3642 do_export(Export*) const;
3644 private:
3645 // The unary operator to apply.
3646 Operator op_;
3647 // Normally true. False if this is an address expression which does
3648 // not escape the current function.
3649 bool escapes_;
3650 // The operand.
3651 Expression* expr_;
3654 // If we are taking the address of a composite literal, and the
3655 // contents are not constant, then we want to make a heap composite
3656 // instead.
3658 Expression*
3659 Unary_expression::do_lower(Gogo*, Named_object*, int)
3661 source_location loc = this->location();
3662 Operator op = this->op_;
3663 Expression* expr = this->expr_;
3665 if (op == OPERATOR_MULT && expr->is_type_expression())
3666 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3668 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3669 // moving x to the heap. FIXME: Is it worth doing a real escape
3670 // analysis here? This case is found in math/unsafe.go and is
3671 // therefore worth special casing.
3672 if (op == OPERATOR_MULT)
3674 Expression* e = expr;
3675 while (e->classification() == EXPRESSION_CONVERSION)
3677 Type_conversion_expression* te
3678 = static_cast<Type_conversion_expression*>(e);
3679 e = te->expr();
3682 if (e->classification() == EXPRESSION_UNARY)
3684 Unary_expression* ue = static_cast<Unary_expression*>(e);
3685 if (ue->op_ == OPERATOR_AND)
3687 if (e == expr)
3689 // *&x == x.
3690 return ue->expr_;
3692 ue->set_does_not_escape();
3697 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3698 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3700 Expression* ret = NULL;
3702 mpz_t eval;
3703 mpz_init(eval);
3704 Type* etype;
3705 if (expr->integer_constant_value(false, eval, &etype))
3707 mpz_t val;
3708 mpz_init(val);
3709 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3710 ret = Expression::make_integer(&val, etype, loc);
3711 mpz_clear(val);
3713 mpz_clear(eval);
3714 if (ret != NULL)
3715 return ret;
3717 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
3719 mpfr_t fval;
3720 mpfr_init(fval);
3721 Type* ftype;
3722 if (expr->float_constant_value(fval, &ftype))
3724 mpfr_t val;
3725 mpfr_init(val);
3726 if (Unary_expression::eval_float(op, fval, val))
3727 ret = Expression::make_float(&val, ftype, loc);
3728 mpfr_clear(val);
3730 if (ret != NULL)
3732 mpfr_clear(fval);
3733 return ret;
3736 mpfr_t ival;
3737 mpfr_init(ival);
3738 if (expr->complex_constant_value(fval, ival, &ftype))
3740 mpfr_t real;
3741 mpfr_t imag;
3742 mpfr_init(real);
3743 mpfr_init(imag);
3744 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
3745 ret = Expression::make_complex(&real, &imag, ftype, loc);
3746 mpfr_clear(real);
3747 mpfr_clear(imag);
3749 mpfr_clear(ival);
3750 mpfr_clear(fval);
3751 if (ret != NULL)
3752 return ret;
3756 return this;
3759 // Return whether a unary expression is a constant.
3761 bool
3762 Unary_expression::do_is_constant() const
3764 if (this->op_ == OPERATOR_MULT)
3766 // Indirecting through a pointer is only constant if the object
3767 // to which the expression points is constant, but we currently
3768 // have no way to determine that.
3769 return false;
3771 else if (this->op_ == OPERATOR_AND)
3773 // Taking the address of a variable is constant if it is a
3774 // global variable, not constant otherwise. In other cases
3775 // taking the address is probably not a constant.
3776 Var_expression* ve = this->expr_->var_expression();
3777 if (ve != NULL)
3779 Named_object* no = ve->named_object();
3780 return no->is_variable() && no->var_value()->is_global();
3782 return false;
3784 else
3785 return this->expr_->is_constant();
3788 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3789 // UVAL, if known; it may be NULL. Return true if this could be done,
3790 // false if not.
3792 bool
3793 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3794 source_location location)
3796 switch (op)
3798 case OPERATOR_PLUS:
3799 mpz_set(val, uval);
3800 return true;
3801 case OPERATOR_MINUS:
3802 mpz_neg(val, uval);
3803 return Integer_expression::check_constant(val, utype, location);
3804 case OPERATOR_NOT:
3805 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3806 return true;
3807 case OPERATOR_XOR:
3808 if (utype == NULL
3809 || utype->integer_type() == NULL
3810 || utype->integer_type()->is_abstract())
3811 mpz_com(val, uval);
3812 else
3814 // The number of HOST_WIDE_INTs that it takes to represent
3815 // UVAL.
3816 size_t count = ((mpz_sizeinbase(uval, 2)
3817 + HOST_BITS_PER_WIDE_INT
3818 - 1)
3819 / HOST_BITS_PER_WIDE_INT);
3821 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3822 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3824 size_t ecount;
3825 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3826 gcc_assert(ecount <= count);
3828 // Trim down to the number of words required by the type.
3829 size_t obits = utype->integer_type()->bits();
3830 if (!utype->integer_type()->is_unsigned())
3831 ++obits;
3832 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3833 / HOST_BITS_PER_WIDE_INT);
3834 gcc_assert(ocount <= ocount);
3836 for (size_t i = 0; i < ocount; ++i)
3837 phwi[i] = ~phwi[i];
3839 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3840 if (clearbits != 0)
3841 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3842 >> clearbits);
3844 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3846 delete[] phwi;
3848 return Integer_expression::check_constant(val, utype, location);
3849 case OPERATOR_AND:
3850 case OPERATOR_MULT:
3851 return false;
3852 default:
3853 gcc_unreachable();
3857 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3858 // could be done, false if not.
3860 bool
3861 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
3863 switch (op)
3865 case OPERATOR_PLUS:
3866 mpfr_set(val, uval, GMP_RNDN);
3867 return true;
3868 case OPERATOR_MINUS:
3869 mpfr_neg(val, uval, GMP_RNDN);
3870 return true;
3871 case OPERATOR_NOT:
3872 case OPERATOR_XOR:
3873 case OPERATOR_AND:
3874 case OPERATOR_MULT:
3875 return false;
3876 default:
3877 gcc_unreachable();
3881 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3882 // if this could be done, false if not.
3884 bool
3885 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
3886 mpfr_t real, mpfr_t imag)
3888 switch (op)
3890 case OPERATOR_PLUS:
3891 mpfr_set(real, rval, GMP_RNDN);
3892 mpfr_set(imag, ival, GMP_RNDN);
3893 return true;
3894 case OPERATOR_MINUS:
3895 mpfr_neg(real, rval, GMP_RNDN);
3896 mpfr_neg(imag, ival, GMP_RNDN);
3897 return true;
3898 case OPERATOR_NOT:
3899 case OPERATOR_XOR:
3900 case OPERATOR_AND:
3901 case OPERATOR_MULT:
3902 return false;
3903 default:
3904 gcc_unreachable();
3908 // Return the integral constant value of a unary expression, if it has one.
3910 bool
3911 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
3912 Type** ptype) const
3914 mpz_t uval;
3915 mpz_init(uval);
3916 bool ret;
3917 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
3918 ret = false;
3919 else
3920 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
3921 this->location());
3922 mpz_clear(uval);
3923 return ret;
3926 // Return the floating point constant value of a unary expression, if
3927 // it has one.
3929 bool
3930 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
3932 mpfr_t uval;
3933 mpfr_init(uval);
3934 bool ret;
3935 if (!this->expr_->float_constant_value(uval, ptype))
3936 ret = false;
3937 else
3938 ret = Unary_expression::eval_float(this->op_, uval, val);
3939 mpfr_clear(uval);
3940 return ret;
3943 // Return the complex constant value of a unary expression, if it has
3944 // one.
3946 bool
3947 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
3948 Type** ptype) const
3950 mpfr_t rval;
3951 mpfr_t ival;
3952 mpfr_init(rval);
3953 mpfr_init(ival);
3954 bool ret;
3955 if (!this->expr_->complex_constant_value(rval, ival, ptype))
3956 ret = false;
3957 else
3958 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
3959 mpfr_clear(rval);
3960 mpfr_clear(ival);
3961 return ret;
3964 // Return the type of a unary expression.
3966 Type*
3967 Unary_expression::do_type()
3969 switch (this->op_)
3971 case OPERATOR_PLUS:
3972 case OPERATOR_MINUS:
3973 case OPERATOR_NOT:
3974 case OPERATOR_XOR:
3975 return this->expr_->type();
3977 case OPERATOR_AND:
3978 return Type::make_pointer_type(this->expr_->type());
3980 case OPERATOR_MULT:
3982 Type* subtype = this->expr_->type();
3983 Type* points_to = subtype->points_to();
3984 if (points_to == NULL)
3985 return Type::make_error_type();
3986 return points_to;
3989 default:
3990 gcc_unreachable();
3994 // Determine abstract types for a unary expression.
3996 void
3997 Unary_expression::do_determine_type(const Type_context* context)
3999 switch (this->op_)
4001 case OPERATOR_PLUS:
4002 case OPERATOR_MINUS:
4003 case OPERATOR_NOT:
4004 case OPERATOR_XOR:
4005 this->expr_->determine_type(context);
4006 break;
4008 case OPERATOR_AND:
4009 // Taking the address of something.
4011 Type* subtype = (context->type == NULL
4012 ? NULL
4013 : context->type->points_to());
4014 Type_context subcontext(subtype, false);
4015 this->expr_->determine_type(&subcontext);
4017 break;
4019 case OPERATOR_MULT:
4020 // Indirecting through a pointer.
4022 Type* subtype = (context->type == NULL
4023 ? NULL
4024 : Type::make_pointer_type(context->type));
4025 Type_context subcontext(subtype, false);
4026 this->expr_->determine_type(&subcontext);
4028 break;
4030 default:
4031 gcc_unreachable();
4035 // Check types for a unary expression.
4037 void
4038 Unary_expression::do_check_types(Gogo*)
4040 Type* type = this->expr_->type();
4041 if (type->is_error_type())
4043 this->set_is_error();
4044 return;
4047 switch (this->op_)
4049 case OPERATOR_PLUS:
4050 case OPERATOR_MINUS:
4051 if (type->integer_type() == NULL
4052 && type->float_type() == NULL
4053 && type->complex_type() == NULL)
4054 this->report_error(_("expected numeric type"));
4055 break;
4057 case OPERATOR_NOT:
4058 case OPERATOR_XOR:
4059 if (type->integer_type() == NULL
4060 && !type->is_boolean_type())
4061 this->report_error(_("expected integer or boolean type"));
4062 break;
4064 case OPERATOR_AND:
4065 if (!this->expr_->is_addressable())
4066 this->report_error(_("invalid operand for unary %<&%>"));
4067 else
4068 this->expr_->address_taken(this->escapes_);
4069 break;
4071 case OPERATOR_MULT:
4072 // Indirecting through a pointer.
4073 if (type->points_to() == NULL)
4074 this->report_error(_("expected pointer"));
4075 break;
4077 default:
4078 gcc_unreachable();
4082 // Get a tree for a unary expression.
4084 tree
4085 Unary_expression::do_get_tree(Translate_context* context)
4087 tree expr = this->expr_->get_tree(context);
4088 if (expr == error_mark_node)
4089 return error_mark_node;
4091 source_location loc = this->location();
4092 switch (this->op_)
4094 case OPERATOR_PLUS:
4095 return expr;
4097 case OPERATOR_MINUS:
4099 tree type = TREE_TYPE(expr);
4100 tree compute_type = excess_precision_type(type);
4101 if (compute_type != NULL_TREE)
4102 expr = ::convert(compute_type, expr);
4103 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
4104 (compute_type != NULL_TREE
4105 ? compute_type
4106 : type),
4107 expr);
4108 if (compute_type != NULL_TREE)
4109 ret = ::convert(type, ret);
4110 return ret;
4113 case OPERATOR_NOT:
4114 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4115 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
4116 else
4117 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
4118 build_int_cst(TREE_TYPE(expr), 0));
4120 case OPERATOR_XOR:
4121 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
4123 case OPERATOR_AND:
4124 // We should not see a non-constant constructor here; cases
4125 // where we would see one should have been moved onto the heap
4126 // at parse time. Taking the address of a nonconstant
4127 // constructor will not do what the programmer expects.
4128 gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4129 gcc_assert(TREE_CODE(expr) != ADDR_EXPR);
4131 // Build a decl for a constant constructor.
4132 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4134 tree decl = build_decl(this->location(), VAR_DECL,
4135 create_tmp_var_name("C"), TREE_TYPE(expr));
4136 DECL_EXTERNAL(decl) = 0;
4137 TREE_PUBLIC(decl) = 0;
4138 TREE_READONLY(decl) = 1;
4139 TREE_CONSTANT(decl) = 1;
4140 TREE_STATIC(decl) = 1;
4141 TREE_ADDRESSABLE(decl) = 1;
4142 DECL_ARTIFICIAL(decl) = 1;
4143 DECL_INITIAL(decl) = expr;
4144 rest_of_decl_compilation(decl, 1, 0);
4145 expr = decl;
4148 return build_fold_addr_expr_loc(loc, expr);
4150 case OPERATOR_MULT:
4152 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4154 // If we are dereferencing the pointer to a large struct, we
4155 // need to check for nil. We don't bother to check for small
4156 // structs because we expect the system to crash on a nil
4157 // pointer dereference.
4158 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4159 if (s == -1 || s >= 4096)
4161 if (!DECL_P(expr))
4162 expr = save_expr(expr);
4163 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
4164 expr,
4165 fold_convert(TREE_TYPE(expr),
4166 null_pointer_node));
4167 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4168 loc);
4169 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
4170 build3(COND_EXPR, void_type_node,
4171 compare, crash, NULL_TREE),
4172 expr);
4175 // If the type of EXPR is a recursive pointer type, then we
4176 // need to insert a cast before indirecting.
4177 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4179 Type* pt = this->expr_->type()->points_to();
4180 tree ind = pt->get_tree(context->gogo());
4181 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
4184 return build_fold_indirect_ref_loc(loc, expr);
4187 default:
4188 gcc_unreachable();
4192 // Export a unary expression.
4194 void
4195 Unary_expression::do_export(Export* exp) const
4197 switch (this->op_)
4199 case OPERATOR_PLUS:
4200 exp->write_c_string("+ ");
4201 break;
4202 case OPERATOR_MINUS:
4203 exp->write_c_string("- ");
4204 break;
4205 case OPERATOR_NOT:
4206 exp->write_c_string("! ");
4207 break;
4208 case OPERATOR_XOR:
4209 exp->write_c_string("^ ");
4210 break;
4211 case OPERATOR_AND:
4212 case OPERATOR_MULT:
4213 default:
4214 gcc_unreachable();
4216 this->expr_->export_expression(exp);
4219 // Import a unary expression.
4221 Expression*
4222 Unary_expression::do_import(Import* imp)
4224 Operator op;
4225 switch (imp->get_char())
4227 case '+':
4228 op = OPERATOR_PLUS;
4229 break;
4230 case '-':
4231 op = OPERATOR_MINUS;
4232 break;
4233 case '!':
4234 op = OPERATOR_NOT;
4235 break;
4236 case '^':
4237 op = OPERATOR_XOR;
4238 break;
4239 default:
4240 gcc_unreachable();
4242 imp->require_c_string(" ");
4243 Expression* expr = Expression::import_expression(imp);
4244 return Expression::make_unary(op, expr, imp->location());
4247 // Make a unary expression.
4249 Expression*
4250 Expression::make_unary(Operator op, Expression* expr, source_location location)
4252 return new Unary_expression(op, expr, location);
4255 // If this is an indirection through a pointer, return the expression
4256 // being pointed through. Otherwise return this.
4258 Expression*
4259 Expression::deref()
4261 if (this->classification_ == EXPRESSION_UNARY)
4263 Unary_expression* ue = static_cast<Unary_expression*>(this);
4264 if (ue->op() == OPERATOR_MULT)
4265 return ue->operand();
4267 return this;
4270 // Class Binary_expression.
4272 // Traversal.
4275 Binary_expression::do_traverse(Traverse* traverse)
4277 int t = Expression::traverse(&this->left_, traverse);
4278 if (t == TRAVERSE_EXIT)
4279 return TRAVERSE_EXIT;
4280 return Expression::traverse(&this->right_, traverse);
4283 // Compare integer constants according to OP.
4285 bool
4286 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4287 mpz_t right_val)
4289 int i = mpz_cmp(left_val, right_val);
4290 switch (op)
4292 case OPERATOR_EQEQ:
4293 return i == 0;
4294 case OPERATOR_NOTEQ:
4295 return i != 0;
4296 case OPERATOR_LT:
4297 return i < 0;
4298 case OPERATOR_LE:
4299 return i <= 0;
4300 case OPERATOR_GT:
4301 return i > 0;
4302 case OPERATOR_GE:
4303 return i >= 0;
4304 default:
4305 gcc_unreachable();
4309 // Compare floating point constants according to OP.
4311 bool
4312 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4313 mpfr_t right_val)
4315 int i;
4316 if (type == NULL)
4317 i = mpfr_cmp(left_val, right_val);
4318 else
4320 mpfr_t lv;
4321 mpfr_init_set(lv, left_val, GMP_RNDN);
4322 mpfr_t rv;
4323 mpfr_init_set(rv, right_val, GMP_RNDN);
4324 Float_expression::constrain_float(lv, type);
4325 Float_expression::constrain_float(rv, type);
4326 i = mpfr_cmp(lv, rv);
4327 mpfr_clear(lv);
4328 mpfr_clear(rv);
4330 switch (op)
4332 case OPERATOR_EQEQ:
4333 return i == 0;
4334 case OPERATOR_NOTEQ:
4335 return i != 0;
4336 case OPERATOR_LT:
4337 return i < 0;
4338 case OPERATOR_LE:
4339 return i <= 0;
4340 case OPERATOR_GT:
4341 return i > 0;
4342 case OPERATOR_GE:
4343 return i >= 0;
4344 default:
4345 gcc_unreachable();
4349 // Compare complex constants according to OP. Complex numbers may
4350 // only be compared for equality.
4352 bool
4353 Binary_expression::compare_complex(Operator op, Type* type,
4354 mpfr_t left_real, mpfr_t left_imag,
4355 mpfr_t right_real, mpfr_t right_imag)
4357 bool is_equal;
4358 if (type == NULL)
4359 is_equal = (mpfr_cmp(left_real, right_real) == 0
4360 && mpfr_cmp(left_imag, right_imag) == 0);
4361 else
4363 mpfr_t lr;
4364 mpfr_t li;
4365 mpfr_init_set(lr, left_real, GMP_RNDN);
4366 mpfr_init_set(li, left_imag, GMP_RNDN);
4367 mpfr_t rr;
4368 mpfr_t ri;
4369 mpfr_init_set(rr, right_real, GMP_RNDN);
4370 mpfr_init_set(ri, right_imag, GMP_RNDN);
4371 Complex_expression::constrain_complex(lr, li, type);
4372 Complex_expression::constrain_complex(rr, ri, type);
4373 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4374 mpfr_clear(lr);
4375 mpfr_clear(li);
4376 mpfr_clear(rr);
4377 mpfr_clear(ri);
4379 switch (op)
4381 case OPERATOR_EQEQ:
4382 return is_equal;
4383 case OPERATOR_NOTEQ:
4384 return !is_equal;
4385 default:
4386 gcc_unreachable();
4390 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4391 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4392 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4393 // this could be done, false if not.
4395 bool
4396 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4397 Type* right_type, mpz_t right_val,
4398 source_location location, mpz_t val)
4400 bool is_shift_op = false;
4401 switch (op)
4403 case OPERATOR_OROR:
4404 case OPERATOR_ANDAND:
4405 case OPERATOR_EQEQ:
4406 case OPERATOR_NOTEQ:
4407 case OPERATOR_LT:
4408 case OPERATOR_LE:
4409 case OPERATOR_GT:
4410 case OPERATOR_GE:
4411 // These return boolean values. We should probably handle them
4412 // anyhow in case a type conversion is used on the result.
4413 return false;
4414 case OPERATOR_PLUS:
4415 mpz_add(val, left_val, right_val);
4416 break;
4417 case OPERATOR_MINUS:
4418 mpz_sub(val, left_val, right_val);
4419 break;
4420 case OPERATOR_OR:
4421 mpz_ior(val, left_val, right_val);
4422 break;
4423 case OPERATOR_XOR:
4424 mpz_xor(val, left_val, right_val);
4425 break;
4426 case OPERATOR_MULT:
4427 mpz_mul(val, left_val, right_val);
4428 break;
4429 case OPERATOR_DIV:
4430 if (mpz_sgn(right_val) != 0)
4431 mpz_tdiv_q(val, left_val, right_val);
4432 else
4434 error_at(location, "division by zero");
4435 mpz_set_ui(val, 0);
4436 return true;
4438 break;
4439 case OPERATOR_MOD:
4440 if (mpz_sgn(right_val) != 0)
4441 mpz_tdiv_r(val, left_val, right_val);
4442 else
4444 error_at(location, "division by zero");
4445 mpz_set_ui(val, 0);
4446 return true;
4448 break;
4449 case OPERATOR_LSHIFT:
4451 unsigned long shift = mpz_get_ui(right_val);
4452 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
4454 error_at(location, "shift count overflow");
4455 mpz_set_ui(val, 0);
4456 return true;
4458 mpz_mul_2exp(val, left_val, shift);
4459 is_shift_op = true;
4460 break;
4462 break;
4463 case OPERATOR_RSHIFT:
4465 unsigned long shift = mpz_get_ui(right_val);
4466 if (mpz_cmp_ui(right_val, shift) != 0)
4468 error_at(location, "shift count overflow");
4469 mpz_set_ui(val, 0);
4470 return true;
4472 if (mpz_cmp_ui(left_val, 0) >= 0)
4473 mpz_tdiv_q_2exp(val, left_val, shift);
4474 else
4475 mpz_fdiv_q_2exp(val, left_val, shift);
4476 is_shift_op = true;
4477 break;
4479 break;
4480 case OPERATOR_AND:
4481 mpz_and(val, left_val, right_val);
4482 break;
4483 case OPERATOR_BITCLEAR:
4485 mpz_t tval;
4486 mpz_init(tval);
4487 mpz_com(tval, right_val);
4488 mpz_and(val, left_val, tval);
4489 mpz_clear(tval);
4491 break;
4492 default:
4493 gcc_unreachable();
4496 Type* type = left_type;
4497 if (!is_shift_op)
4499 if (type == NULL)
4500 type = right_type;
4501 else if (type != right_type && right_type != NULL)
4503 if (type->is_abstract())
4504 type = right_type;
4505 else if (!right_type->is_abstract())
4507 // This look like a type error which should be diagnosed
4508 // elsewhere. Don't do anything here, to avoid an
4509 // unhelpful chain of error messages.
4510 return true;
4515 if (type != NULL && !type->is_abstract())
4517 // We have to check the operands too, as we have implicitly
4518 // coerced them to TYPE.
4519 if ((type != left_type
4520 && !Integer_expression::check_constant(left_val, type, location))
4521 || (!is_shift_op
4522 && type != right_type
4523 && !Integer_expression::check_constant(right_val, type,
4524 location))
4525 || !Integer_expression::check_constant(val, type, location))
4526 mpz_set_ui(val, 0);
4529 return true;
4532 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4533 // Return true if this could be done, false if not.
4535 bool
4536 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4537 Type* right_type, mpfr_t right_val,
4538 mpfr_t val, source_location location)
4540 switch (op)
4542 case OPERATOR_OROR:
4543 case OPERATOR_ANDAND:
4544 case OPERATOR_EQEQ:
4545 case OPERATOR_NOTEQ:
4546 case OPERATOR_LT:
4547 case OPERATOR_LE:
4548 case OPERATOR_GT:
4549 case OPERATOR_GE:
4550 // These return boolean values. We should probably handle them
4551 // anyhow in case a type conversion is used on the result.
4552 return false;
4553 case OPERATOR_PLUS:
4554 mpfr_add(val, left_val, right_val, GMP_RNDN);
4555 break;
4556 case OPERATOR_MINUS:
4557 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4558 break;
4559 case OPERATOR_OR:
4560 case OPERATOR_XOR:
4561 case OPERATOR_AND:
4562 case OPERATOR_BITCLEAR:
4563 return false;
4564 case OPERATOR_MULT:
4565 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4566 break;
4567 case OPERATOR_DIV:
4568 if (mpfr_zero_p(right_val))
4569 error_at(location, "division by zero");
4570 mpfr_div(val, left_val, right_val, GMP_RNDN);
4571 break;
4572 case OPERATOR_MOD:
4573 return false;
4574 case OPERATOR_LSHIFT:
4575 case OPERATOR_RSHIFT:
4576 return false;
4577 default:
4578 gcc_unreachable();
4581 Type* type = left_type;
4582 if (type == NULL)
4583 type = right_type;
4584 else if (type != right_type && right_type != NULL)
4586 if (type->is_abstract())
4587 type = right_type;
4588 else if (!right_type->is_abstract())
4590 // This looks like a type error which should be diagnosed
4591 // elsewhere. Don't do anything here, to avoid an unhelpful
4592 // chain of error messages.
4593 return true;
4597 if (type != NULL && !type->is_abstract())
4599 if ((type != left_type
4600 && !Float_expression::check_constant(left_val, type, location))
4601 || (type != right_type
4602 && !Float_expression::check_constant(right_val, type,
4603 location))
4604 || !Float_expression::check_constant(val, type, location))
4605 mpfr_set_ui(val, 0, GMP_RNDN);
4608 return true;
4611 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4612 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4613 // could be done, false if not.
4615 bool
4616 Binary_expression::eval_complex(Operator op, Type* left_type,
4617 mpfr_t left_real, mpfr_t left_imag,
4618 Type *right_type,
4619 mpfr_t right_real, mpfr_t right_imag,
4620 mpfr_t real, mpfr_t imag,
4621 source_location location)
4623 switch (op)
4625 case OPERATOR_OROR:
4626 case OPERATOR_ANDAND:
4627 case OPERATOR_EQEQ:
4628 case OPERATOR_NOTEQ:
4629 case OPERATOR_LT:
4630 case OPERATOR_LE:
4631 case OPERATOR_GT:
4632 case OPERATOR_GE:
4633 // These return boolean values and must be handled differently.
4634 return false;
4635 case OPERATOR_PLUS:
4636 mpfr_add(real, left_real, right_real, GMP_RNDN);
4637 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4638 break;
4639 case OPERATOR_MINUS:
4640 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4641 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4642 break;
4643 case OPERATOR_OR:
4644 case OPERATOR_XOR:
4645 case OPERATOR_AND:
4646 case OPERATOR_BITCLEAR:
4647 return false;
4648 case OPERATOR_MULT:
4650 // You might think that multiplying two complex numbers would
4651 // be simple, and you would be right, until you start to think
4652 // about getting the right answer for infinity. If one
4653 // operand here is infinity and the other is anything other
4654 // than zero or NaN, then we are going to wind up subtracting
4655 // two infinity values. That will give us a NaN, but the
4656 // correct answer is infinity.
4658 mpfr_t lrrr;
4659 mpfr_init(lrrr);
4660 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4662 mpfr_t lrri;
4663 mpfr_init(lrri);
4664 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4666 mpfr_t lirr;
4667 mpfr_init(lirr);
4668 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4670 mpfr_t liri;
4671 mpfr_init(liri);
4672 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4674 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4675 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4677 // If we get NaN on both sides, check whether it should really
4678 // be infinity. The rule is that if either side of the
4679 // complex number is infinity, then the whole value is
4680 // infinity, even if the other side is NaN. So the only case
4681 // we have to fix is the one in which both sides are NaN.
4682 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4683 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4684 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4686 bool is_infinity = false;
4688 mpfr_t lr;
4689 mpfr_t li;
4690 mpfr_init_set(lr, left_real, GMP_RNDN);
4691 mpfr_init_set(li, left_imag, GMP_RNDN);
4693 mpfr_t rr;
4694 mpfr_t ri;
4695 mpfr_init_set(rr, right_real, GMP_RNDN);
4696 mpfr_init_set(ri, right_imag, GMP_RNDN);
4698 // If the left side is infinity, then the result is
4699 // infinity.
4700 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4702 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4703 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4704 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4705 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4706 if (mpfr_nan_p(rr))
4708 mpfr_set_ui(rr, 0, GMP_RNDN);
4709 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4711 if (mpfr_nan_p(ri))
4713 mpfr_set_ui(ri, 0, GMP_RNDN);
4714 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4716 is_infinity = true;
4719 // If the right side is infinity, then the result is
4720 // infinity.
4721 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4723 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4724 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4725 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4726 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4727 if (mpfr_nan_p(lr))
4729 mpfr_set_ui(lr, 0, GMP_RNDN);
4730 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4732 if (mpfr_nan_p(li))
4734 mpfr_set_ui(li, 0, GMP_RNDN);
4735 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4737 is_infinity = true;
4740 // If we got an overflow in the intermediate computations,
4741 // then the result is infinity.
4742 if (!is_infinity
4743 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4744 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4746 if (mpfr_nan_p(lr))
4748 mpfr_set_ui(lr, 0, GMP_RNDN);
4749 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4751 if (mpfr_nan_p(li))
4753 mpfr_set_ui(li, 0, GMP_RNDN);
4754 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4756 if (mpfr_nan_p(rr))
4758 mpfr_set_ui(rr, 0, GMP_RNDN);
4759 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4761 if (mpfr_nan_p(ri))
4763 mpfr_set_ui(ri, 0, GMP_RNDN);
4764 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4766 is_infinity = true;
4769 if (is_infinity)
4771 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4772 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4773 mpfr_mul(lirr, li, rr, GMP_RNDN);
4774 mpfr_mul(liri, li, ri, GMP_RNDN);
4775 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4776 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4777 mpfr_set_inf(real, mpfr_sgn(real));
4778 mpfr_set_inf(imag, mpfr_sgn(imag));
4781 mpfr_clear(lr);
4782 mpfr_clear(li);
4783 mpfr_clear(rr);
4784 mpfr_clear(ri);
4787 mpfr_clear(lrrr);
4788 mpfr_clear(lrri);
4789 mpfr_clear(lirr);
4790 mpfr_clear(liri);
4792 break;
4793 case OPERATOR_DIV:
4795 // For complex division we want to avoid having an
4796 // intermediate overflow turn the whole result in a NaN. We
4797 // scale the values to try to avoid this.
4799 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4800 error_at(location, "division by zero");
4802 mpfr_t rra;
4803 mpfr_t ria;
4804 mpfr_init(rra);
4805 mpfr_init(ria);
4806 mpfr_abs(rra, right_real, GMP_RNDN);
4807 mpfr_abs(ria, right_imag, GMP_RNDN);
4808 mpfr_t t;
4809 mpfr_init(t);
4810 mpfr_max(t, rra, ria, GMP_RNDN);
4812 mpfr_t rr;
4813 mpfr_t ri;
4814 mpfr_init_set(rr, right_real, GMP_RNDN);
4815 mpfr_init_set(ri, right_imag, GMP_RNDN);
4816 long ilogbw = 0;
4817 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4819 ilogbw = mpfr_get_exp(t);
4820 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4821 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4824 mpfr_t denom;
4825 mpfr_init(denom);
4826 mpfr_mul(denom, rr, rr, GMP_RNDN);
4827 mpfr_mul(t, ri, ri, GMP_RNDN);
4828 mpfr_add(denom, denom, t, GMP_RNDN);
4830 mpfr_mul(real, left_real, rr, GMP_RNDN);
4831 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4832 mpfr_add(real, real, t, GMP_RNDN);
4833 mpfr_div(real, real, denom, GMP_RNDN);
4834 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4836 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4837 mpfr_mul(t, left_real, ri, GMP_RNDN);
4838 mpfr_sub(imag, imag, t, GMP_RNDN);
4839 mpfr_div(imag, imag, denom, GMP_RNDN);
4840 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4842 // If we wind up with NaN on both sides, check whether we
4843 // should really have infinity. The rule is that if either
4844 // side of the complex number is infinity, then the whole
4845 // value is infinity, even if the other side is NaN. So the
4846 // only case we have to fix is the one in which both sides are
4847 // NaN.
4848 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4849 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4850 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4852 if (mpfr_zero_p(denom))
4854 mpfr_set_inf(real, mpfr_sgn(rr));
4855 mpfr_mul(real, real, left_real, GMP_RNDN);
4856 mpfr_set_inf(imag, mpfr_sgn(rr));
4857 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4859 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4860 && mpfr_number_p(rr) && mpfr_number_p(ri))
4862 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4863 mpfr_copysign(t, t, left_real, GMP_RNDN);
4865 mpfr_t t2;
4866 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4867 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4869 mpfr_t t3;
4870 mpfr_init(t3);
4871 mpfr_mul(t3, t, rr, GMP_RNDN);
4873 mpfr_t t4;
4874 mpfr_init(t4);
4875 mpfr_mul(t4, t2, ri, GMP_RNDN);
4877 mpfr_add(t3, t3, t4, GMP_RNDN);
4878 mpfr_set_inf(real, mpfr_sgn(t3));
4880 mpfr_mul(t3, t2, rr, GMP_RNDN);
4881 mpfr_mul(t4, t, ri, GMP_RNDN);
4882 mpfr_sub(t3, t3, t4, GMP_RNDN);
4883 mpfr_set_inf(imag, mpfr_sgn(t3));
4885 mpfr_clear(t2);
4886 mpfr_clear(t3);
4887 mpfr_clear(t4);
4889 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
4890 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
4892 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4893 mpfr_copysign(t, t, rr, GMP_RNDN);
4895 mpfr_t t2;
4896 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4897 mpfr_copysign(t2, t2, ri, GMP_RNDN);
4899 mpfr_t t3;
4900 mpfr_init(t3);
4901 mpfr_mul(t3, left_real, t, GMP_RNDN);
4903 mpfr_t t4;
4904 mpfr_init(t4);
4905 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
4907 mpfr_add(t3, t3, t4, GMP_RNDN);
4908 mpfr_set_ui(real, 0, GMP_RNDN);
4909 mpfr_mul(real, real, t3, GMP_RNDN);
4911 mpfr_mul(t3, left_imag, t, GMP_RNDN);
4912 mpfr_mul(t4, left_real, t2, GMP_RNDN);
4913 mpfr_sub(t3, t3, t4, GMP_RNDN);
4914 mpfr_set_ui(imag, 0, GMP_RNDN);
4915 mpfr_mul(imag, imag, t3, GMP_RNDN);
4917 mpfr_clear(t2);
4918 mpfr_clear(t3);
4919 mpfr_clear(t4);
4923 mpfr_clear(denom);
4924 mpfr_clear(rr);
4925 mpfr_clear(ri);
4926 mpfr_clear(t);
4927 mpfr_clear(rra);
4928 mpfr_clear(ria);
4930 break;
4931 case OPERATOR_MOD:
4932 return false;
4933 case OPERATOR_LSHIFT:
4934 case OPERATOR_RSHIFT:
4935 return false;
4936 default:
4937 gcc_unreachable();
4940 Type* type = left_type;
4941 if (type == NULL)
4942 type = right_type;
4943 else if (type != right_type && right_type != NULL)
4945 if (type->is_abstract())
4946 type = right_type;
4947 else if (!right_type->is_abstract())
4949 // This looks like a type error which should be diagnosed
4950 // elsewhere. Don't do anything here, to avoid an unhelpful
4951 // chain of error messages.
4952 return true;
4956 if (type != NULL && !type->is_abstract())
4958 if ((type != left_type
4959 && !Complex_expression::check_constant(left_real, left_imag,
4960 type, location))
4961 || (type != right_type
4962 && !Complex_expression::check_constant(right_real, right_imag,
4963 type, location))
4964 || !Complex_expression::check_constant(real, imag, type,
4965 location))
4967 mpfr_set_ui(real, 0, GMP_RNDN);
4968 mpfr_set_ui(imag, 0, GMP_RNDN);
4972 return true;
4975 // Lower a binary expression. We have to evaluate constant
4976 // expressions now, in order to implement Go's unlimited precision
4977 // constants.
4979 Expression*
4980 Binary_expression::do_lower(Gogo*, Named_object*, int)
4982 source_location location = this->location();
4983 Operator op = this->op_;
4984 Expression* left = this->left_;
4985 Expression* right = this->right_;
4987 const bool is_comparison = (op == OPERATOR_EQEQ
4988 || op == OPERATOR_NOTEQ
4989 || op == OPERATOR_LT
4990 || op == OPERATOR_LE
4991 || op == OPERATOR_GT
4992 || op == OPERATOR_GE);
4994 // Integer constant expressions.
4996 mpz_t left_val;
4997 mpz_init(left_val);
4998 Type* left_type;
4999 mpz_t right_val;
5000 mpz_init(right_val);
5001 Type* right_type;
5002 if (left->integer_constant_value(false, left_val, &left_type)
5003 && right->integer_constant_value(false, right_val, &right_type))
5005 Expression* ret = NULL;
5006 if (left_type != right_type
5007 && left_type != NULL
5008 && right_type != NULL
5009 && left_type->base() != right_type->base()
5010 && op != OPERATOR_LSHIFT
5011 && op != OPERATOR_RSHIFT)
5013 // May be a type error--let it be diagnosed later.
5015 else if (is_comparison)
5017 bool b = Binary_expression::compare_integer(op, left_val,
5018 right_val);
5019 ret = Expression::make_cast(Type::lookup_bool_type(),
5020 Expression::make_boolean(b, location),
5021 location);
5023 else
5025 mpz_t val;
5026 mpz_init(val);
5028 if (Binary_expression::eval_integer(op, left_type, left_val,
5029 right_type, right_val,
5030 location, val))
5032 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5033 Type* type;
5034 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5035 type = left_type;
5036 else if (left_type == NULL)
5037 type = right_type;
5038 else if (right_type == NULL)
5039 type = left_type;
5040 else if (!left_type->is_abstract()
5041 && left_type->named_type() != NULL)
5042 type = left_type;
5043 else if (!right_type->is_abstract()
5044 && right_type->named_type() != NULL)
5045 type = right_type;
5046 else if (!left_type->is_abstract())
5047 type = left_type;
5048 else if (!right_type->is_abstract())
5049 type = right_type;
5050 else if (left_type->float_type() != NULL)
5051 type = left_type;
5052 else if (right_type->float_type() != NULL)
5053 type = right_type;
5054 else if (left_type->complex_type() != NULL)
5055 type = left_type;
5056 else if (right_type->complex_type() != NULL)
5057 type = right_type;
5058 else
5059 type = left_type;
5060 ret = Expression::make_integer(&val, type, location);
5063 mpz_clear(val);
5066 if (ret != NULL)
5068 mpz_clear(right_val);
5069 mpz_clear(left_val);
5070 return ret;
5073 mpz_clear(right_val);
5074 mpz_clear(left_val);
5077 // Floating point constant expressions.
5079 mpfr_t left_val;
5080 mpfr_init(left_val);
5081 Type* left_type;
5082 mpfr_t right_val;
5083 mpfr_init(right_val);
5084 Type* right_type;
5085 if (left->float_constant_value(left_val, &left_type)
5086 && right->float_constant_value(right_val, &right_type))
5088 Expression* ret = NULL;
5089 if (left_type != right_type
5090 && left_type != NULL
5091 && right_type != NULL
5092 && left_type->base() != right_type->base()
5093 && op != OPERATOR_LSHIFT
5094 && op != OPERATOR_RSHIFT)
5096 // May be a type error--let it be diagnosed later.
5098 else if (is_comparison)
5100 bool b = Binary_expression::compare_float(op,
5101 (left_type != NULL
5102 ? left_type
5103 : right_type),
5104 left_val, right_val);
5105 ret = Expression::make_boolean(b, location);
5107 else
5109 mpfr_t val;
5110 mpfr_init(val);
5112 if (Binary_expression::eval_float(op, left_type, left_val,
5113 right_type, right_val, val,
5114 location))
5116 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5117 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5118 Type* type;
5119 if (left_type == NULL)
5120 type = right_type;
5121 else if (right_type == NULL)
5122 type = left_type;
5123 else if (!left_type->is_abstract()
5124 && left_type->named_type() != NULL)
5125 type = left_type;
5126 else if (!right_type->is_abstract()
5127 && right_type->named_type() != NULL)
5128 type = right_type;
5129 else if (!left_type->is_abstract())
5130 type = left_type;
5131 else if (!right_type->is_abstract())
5132 type = right_type;
5133 else if (left_type->float_type() != NULL)
5134 type = left_type;
5135 else if (right_type->float_type() != NULL)
5136 type = right_type;
5137 else
5138 type = left_type;
5139 ret = Expression::make_float(&val, type, location);
5142 mpfr_clear(val);
5145 if (ret != NULL)
5147 mpfr_clear(right_val);
5148 mpfr_clear(left_val);
5149 return ret;
5152 mpfr_clear(right_val);
5153 mpfr_clear(left_val);
5156 // Complex constant expressions.
5158 mpfr_t left_real;
5159 mpfr_t left_imag;
5160 mpfr_init(left_real);
5161 mpfr_init(left_imag);
5162 Type* left_type;
5164 mpfr_t right_real;
5165 mpfr_t right_imag;
5166 mpfr_init(right_real);
5167 mpfr_init(right_imag);
5168 Type* right_type;
5170 if (left->complex_constant_value(left_real, left_imag, &left_type)
5171 && right->complex_constant_value(right_real, right_imag, &right_type))
5173 Expression* ret = NULL;
5174 if (left_type != right_type
5175 && left_type != NULL
5176 && right_type != NULL
5177 && left_type->base() != right_type->base())
5179 // May be a type error--let it be diagnosed later.
5181 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5183 bool b = Binary_expression::compare_complex(op,
5184 (left_type != NULL
5185 ? left_type
5186 : right_type),
5187 left_real,
5188 left_imag,
5189 right_real,
5190 right_imag);
5191 ret = Expression::make_boolean(b, location);
5193 else
5195 mpfr_t real;
5196 mpfr_t imag;
5197 mpfr_init(real);
5198 mpfr_init(imag);
5200 if (Binary_expression::eval_complex(op, left_type,
5201 left_real, left_imag,
5202 right_type,
5203 right_real, right_imag,
5204 real, imag,
5205 location))
5207 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5208 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5209 Type* type;
5210 if (left_type == NULL)
5211 type = right_type;
5212 else if (right_type == NULL)
5213 type = left_type;
5214 else if (!left_type->is_abstract()
5215 && left_type->named_type() != NULL)
5216 type = left_type;
5217 else if (!right_type->is_abstract()
5218 && right_type->named_type() != NULL)
5219 type = right_type;
5220 else if (!left_type->is_abstract())
5221 type = left_type;
5222 else if (!right_type->is_abstract())
5223 type = right_type;
5224 else if (left_type->complex_type() != NULL)
5225 type = left_type;
5226 else if (right_type->complex_type() != NULL)
5227 type = right_type;
5228 else
5229 type = left_type;
5230 ret = Expression::make_complex(&real, &imag, type,
5231 location);
5233 mpfr_clear(real);
5234 mpfr_clear(imag);
5237 if (ret != NULL)
5239 mpfr_clear(left_real);
5240 mpfr_clear(left_imag);
5241 mpfr_clear(right_real);
5242 mpfr_clear(right_imag);
5243 return ret;
5247 mpfr_clear(left_real);
5248 mpfr_clear(left_imag);
5249 mpfr_clear(right_real);
5250 mpfr_clear(right_imag);
5253 // String constant expressions.
5254 if (op == OPERATOR_PLUS
5255 && left->type()->is_string_type()
5256 && right->type()->is_string_type())
5258 std::string left_string;
5259 std::string right_string;
5260 if (left->string_constant_value(&left_string)
5261 && right->string_constant_value(&right_string))
5262 return Expression::make_string(left_string + right_string, location);
5265 return this;
5268 // Return the integer constant value, if it has one.
5270 bool
5271 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5272 Type** ptype) const
5274 mpz_t left_val;
5275 mpz_init(left_val);
5276 Type* left_type;
5277 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5278 &left_type))
5280 mpz_clear(left_val);
5281 return false;
5284 mpz_t right_val;
5285 mpz_init(right_val);
5286 Type* right_type;
5287 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5288 &right_type))
5290 mpz_clear(right_val);
5291 mpz_clear(left_val);
5292 return false;
5295 bool ret;
5296 if (left_type != right_type
5297 && left_type != NULL
5298 && right_type != NULL
5299 && left_type->base() != right_type->base()
5300 && this->op_ != OPERATOR_RSHIFT
5301 && this->op_ != OPERATOR_LSHIFT)
5302 ret = false;
5303 else
5304 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5305 right_type, right_val,
5306 this->location(), val);
5308 mpz_clear(right_val);
5309 mpz_clear(left_val);
5311 if (ret)
5312 *ptype = left_type;
5314 return ret;
5317 // Return the floating point constant value, if it has one.
5319 bool
5320 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5322 mpfr_t left_val;
5323 mpfr_init(left_val);
5324 Type* left_type;
5325 if (!this->left_->float_constant_value(left_val, &left_type))
5327 mpfr_clear(left_val);
5328 return false;
5331 mpfr_t right_val;
5332 mpfr_init(right_val);
5333 Type* right_type;
5334 if (!this->right_->float_constant_value(right_val, &right_type))
5336 mpfr_clear(right_val);
5337 mpfr_clear(left_val);
5338 return false;
5341 bool ret;
5342 if (left_type != right_type
5343 && left_type != NULL
5344 && right_type != NULL
5345 && left_type->base() != right_type->base())
5346 ret = false;
5347 else
5348 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5349 right_type, right_val,
5350 val, this->location());
5352 mpfr_clear(left_val);
5353 mpfr_clear(right_val);
5355 if (ret)
5356 *ptype = left_type;
5358 return ret;
5361 // Return the complex constant value, if it has one.
5363 bool
5364 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5365 Type** ptype) const
5367 mpfr_t left_real;
5368 mpfr_t left_imag;
5369 mpfr_init(left_real);
5370 mpfr_init(left_imag);
5371 Type* left_type;
5372 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5374 mpfr_clear(left_real);
5375 mpfr_clear(left_imag);
5376 return false;
5379 mpfr_t right_real;
5380 mpfr_t right_imag;
5381 mpfr_init(right_real);
5382 mpfr_init(right_imag);
5383 Type* right_type;
5384 if (!this->right_->complex_constant_value(right_real, right_imag,
5385 &right_type))
5387 mpfr_clear(left_real);
5388 mpfr_clear(left_imag);
5389 mpfr_clear(right_real);
5390 mpfr_clear(right_imag);
5391 return false;
5394 bool ret;
5395 if (left_type != right_type
5396 && left_type != NULL
5397 && right_type != NULL
5398 && left_type->base() != right_type->base())
5399 ret = false;
5400 else
5401 ret = Binary_expression::eval_complex(this->op_, left_type,
5402 left_real, left_imag,
5403 right_type,
5404 right_real, right_imag,
5405 real, imag,
5406 this->location());
5407 mpfr_clear(left_real);
5408 mpfr_clear(left_imag);
5409 mpfr_clear(right_real);
5410 mpfr_clear(right_imag);
5412 if (ret)
5413 *ptype = left_type;
5415 return ret;
5418 // Note that the value is being discarded.
5420 void
5421 Binary_expression::do_discarding_value()
5423 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5424 this->right_->discarding_value();
5425 else
5426 this->warn_about_unused_value();
5429 // Get type.
5431 Type*
5432 Binary_expression::do_type()
5434 if (this->classification() == EXPRESSION_ERROR)
5435 return Type::make_error_type();
5437 switch (this->op_)
5439 case OPERATOR_OROR:
5440 case OPERATOR_ANDAND:
5441 case OPERATOR_EQEQ:
5442 case OPERATOR_NOTEQ:
5443 case OPERATOR_LT:
5444 case OPERATOR_LE:
5445 case OPERATOR_GT:
5446 case OPERATOR_GE:
5447 return Type::lookup_bool_type();
5449 case OPERATOR_PLUS:
5450 case OPERATOR_MINUS:
5451 case OPERATOR_OR:
5452 case OPERATOR_XOR:
5453 case OPERATOR_MULT:
5454 case OPERATOR_DIV:
5455 case OPERATOR_MOD:
5456 case OPERATOR_AND:
5457 case OPERATOR_BITCLEAR:
5459 Type* left_type = this->left_->type();
5460 Type* right_type = this->right_->type();
5461 if (left_type->is_error_type())
5462 return left_type;
5463 else if (right_type->is_error_type())
5464 return right_type;
5465 else if (!Type::are_compatible_for_binop(left_type, right_type))
5467 this->report_error(_("incompatible types in binary expression"));
5468 return Type::make_error_type();
5470 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
5471 return left_type;
5472 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5473 return right_type;
5474 else if (!left_type->is_abstract())
5475 return left_type;
5476 else if (!right_type->is_abstract())
5477 return right_type;
5478 else if (left_type->complex_type() != NULL)
5479 return left_type;
5480 else if (right_type->complex_type() != NULL)
5481 return right_type;
5482 else if (left_type->float_type() != NULL)
5483 return left_type;
5484 else if (right_type->float_type() != NULL)
5485 return right_type;
5486 else
5487 return left_type;
5490 case OPERATOR_LSHIFT:
5491 case OPERATOR_RSHIFT:
5492 return this->left_->type();
5494 default:
5495 gcc_unreachable();
5499 // Set type for a binary expression.
5501 void
5502 Binary_expression::do_determine_type(const Type_context* context)
5504 Type* tleft = this->left_->type();
5505 Type* tright = this->right_->type();
5507 // Both sides should have the same type, except for the shift
5508 // operations. For a comparison, we should ignore the incoming
5509 // type.
5511 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5512 || this->op_ == OPERATOR_RSHIFT);
5514 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5515 || this->op_ == OPERATOR_NOTEQ
5516 || this->op_ == OPERATOR_LT
5517 || this->op_ == OPERATOR_LE
5518 || this->op_ == OPERATOR_GT
5519 || this->op_ == OPERATOR_GE);
5521 Type_context subcontext(*context);
5523 if (is_comparison)
5525 // In a comparison, the context does not determine the types of
5526 // the operands.
5527 subcontext.type = NULL;
5530 // Set the context for the left hand operand.
5531 if (is_shift_op)
5533 // The right hand operand plays no role in determining the type
5534 // of the left hand operand. A shift of an abstract integer in
5535 // a string context gets special treatment, which may be a
5536 // language bug.
5537 if (subcontext.type != NULL
5538 && subcontext.type->is_string_type()
5539 && tleft->is_abstract())
5540 error_at(this->location(), "shift of non-integer operand");
5542 else if (!tleft->is_abstract())
5543 subcontext.type = tleft;
5544 else if (!tright->is_abstract())
5545 subcontext.type = tright;
5546 else if (subcontext.type == NULL)
5548 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5549 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5550 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5552 // Both sides have an abstract integer, abstract float, or
5553 // abstract complex type. Just let CONTEXT determine
5554 // whether they may remain abstract or not.
5556 else if (tleft->complex_type() != NULL)
5557 subcontext.type = tleft;
5558 else if (tright->complex_type() != NULL)
5559 subcontext.type = tright;
5560 else if (tleft->float_type() != NULL)
5561 subcontext.type = tleft;
5562 else if (tright->float_type() != NULL)
5563 subcontext.type = tright;
5564 else
5565 subcontext.type = tleft;
5567 if (subcontext.type != NULL && !context->may_be_abstract)
5568 subcontext.type = subcontext.type->make_non_abstract_type();
5571 this->left_->determine_type(&subcontext);
5573 // The context for the right hand operand is the same as for the
5574 // left hand operand, except for a shift operator.
5575 if (is_shift_op)
5577 subcontext.type = Type::lookup_integer_type("uint");
5578 subcontext.may_be_abstract = false;
5581 this->right_->determine_type(&subcontext);
5584 // Report an error if the binary operator OP does not support TYPE.
5585 // Return whether the operation is OK. This should not be used for
5586 // shift.
5588 bool
5589 Binary_expression::check_operator_type(Operator op, Type* type,
5590 source_location location)
5592 switch (op)
5594 case OPERATOR_OROR:
5595 case OPERATOR_ANDAND:
5596 if (!type->is_boolean_type())
5598 error_at(location, "expected boolean type");
5599 return false;
5601 break;
5603 case OPERATOR_EQEQ:
5604 case OPERATOR_NOTEQ:
5605 if (type->integer_type() == NULL
5606 && type->float_type() == NULL
5607 && type->complex_type() == NULL
5608 && !type->is_string_type()
5609 && type->points_to() == NULL
5610 && !type->is_nil_type()
5611 && !type->is_boolean_type()
5612 && type->interface_type() == NULL
5613 && (type->array_type() == NULL
5614 || type->array_type()->length() != NULL)
5615 && type->map_type() == NULL
5616 && type->channel_type() == NULL
5617 && type->function_type() == NULL)
5619 error_at(location,
5620 ("expected integer, floating, complex, string, pointer, "
5621 "boolean, interface, slice, map, channel, "
5622 "or function type"));
5623 return false;
5625 break;
5627 case OPERATOR_LT:
5628 case OPERATOR_LE:
5629 case OPERATOR_GT:
5630 case OPERATOR_GE:
5631 if (type->integer_type() == NULL
5632 && type->float_type() == NULL
5633 && !type->is_string_type())
5635 error_at(location, "expected integer, floating, or string type");
5636 return false;
5638 break;
5640 case OPERATOR_PLUS:
5641 case OPERATOR_PLUSEQ:
5642 if (type->integer_type() == NULL
5643 && type->float_type() == NULL
5644 && type->complex_type() == NULL
5645 && !type->is_string_type())
5647 error_at(location,
5648 "expected integer, floating, complex, or string type");
5649 return false;
5651 break;
5653 case OPERATOR_MINUS:
5654 case OPERATOR_MINUSEQ:
5655 case OPERATOR_MULT:
5656 case OPERATOR_MULTEQ:
5657 case OPERATOR_DIV:
5658 case OPERATOR_DIVEQ:
5659 if (type->integer_type() == NULL
5660 && type->float_type() == NULL
5661 && type->complex_type() == NULL)
5663 error_at(location, "expected integer, floating, or complex type");
5664 return false;
5666 break;
5668 case OPERATOR_MOD:
5669 case OPERATOR_MODEQ:
5670 case OPERATOR_OR:
5671 case OPERATOR_OREQ:
5672 case OPERATOR_AND:
5673 case OPERATOR_ANDEQ:
5674 case OPERATOR_XOR:
5675 case OPERATOR_XOREQ:
5676 case OPERATOR_BITCLEAR:
5677 case OPERATOR_BITCLEAREQ:
5678 if (type->integer_type() == NULL)
5680 error_at(location, "expected integer type");
5681 return false;
5683 break;
5685 default:
5686 gcc_unreachable();
5689 return true;
5692 // Check types.
5694 void
5695 Binary_expression::do_check_types(Gogo*)
5697 if (this->classification() == EXPRESSION_ERROR)
5698 return;
5700 Type* left_type = this->left_->type();
5701 Type* right_type = this->right_->type();
5702 if (left_type->is_error_type() || right_type->is_error_type())
5704 this->set_is_error();
5705 return;
5708 if (this->op_ == OPERATOR_EQEQ
5709 || this->op_ == OPERATOR_NOTEQ
5710 || this->op_ == OPERATOR_LT
5711 || this->op_ == OPERATOR_LE
5712 || this->op_ == OPERATOR_GT
5713 || this->op_ == OPERATOR_GE)
5715 if (!Type::are_assignable(left_type, right_type, NULL)
5716 && !Type::are_assignable(right_type, left_type, NULL))
5718 this->report_error(_("incompatible types in binary expression"));
5719 return;
5721 if (!Binary_expression::check_operator_type(this->op_, left_type,
5722 this->location())
5723 || !Binary_expression::check_operator_type(this->op_, right_type,
5724 this->location()))
5726 this->set_is_error();
5727 return;
5730 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5732 if (!Type::are_compatible_for_binop(left_type, right_type))
5734 this->report_error(_("incompatible types in binary expression"));
5735 return;
5737 if (!Binary_expression::check_operator_type(this->op_, left_type,
5738 this->location()))
5740 this->set_is_error();
5741 return;
5744 else
5746 if (left_type->integer_type() == NULL)
5747 this->report_error(_("shift of non-integer operand"));
5749 if (!right_type->is_abstract()
5750 && (right_type->integer_type() == NULL
5751 || !right_type->integer_type()->is_unsigned()))
5752 this->report_error(_("shift count not unsigned integer"));
5753 else
5755 mpz_t val;
5756 mpz_init(val);
5757 Type* type;
5758 if (this->right_->integer_constant_value(true, val, &type))
5760 if (mpz_sgn(val) < 0)
5761 this->report_error(_("negative shift count"));
5763 mpz_clear(val);
5768 // Get a tree for a binary expression.
5770 tree
5771 Binary_expression::do_get_tree(Translate_context* context)
5773 tree left = this->left_->get_tree(context);
5774 tree right = this->right_->get_tree(context);
5776 if (left == error_mark_node || right == error_mark_node)
5777 return error_mark_node;
5779 enum tree_code code;
5780 bool use_left_type = true;
5781 bool is_shift_op = false;
5782 switch (this->op_)
5784 case OPERATOR_EQEQ:
5785 case OPERATOR_NOTEQ:
5786 case OPERATOR_LT:
5787 case OPERATOR_LE:
5788 case OPERATOR_GT:
5789 case OPERATOR_GE:
5790 return Expression::comparison_tree(context, this->op_,
5791 this->left_->type(), left,
5792 this->right_->type(), right,
5793 this->location());
5795 case OPERATOR_OROR:
5796 code = TRUTH_ORIF_EXPR;
5797 use_left_type = false;
5798 break;
5799 case OPERATOR_ANDAND:
5800 code = TRUTH_ANDIF_EXPR;
5801 use_left_type = false;
5802 break;
5803 case OPERATOR_PLUS:
5804 code = PLUS_EXPR;
5805 break;
5806 case OPERATOR_MINUS:
5807 code = MINUS_EXPR;
5808 break;
5809 case OPERATOR_OR:
5810 code = BIT_IOR_EXPR;
5811 break;
5812 case OPERATOR_XOR:
5813 code = BIT_XOR_EXPR;
5814 break;
5815 case OPERATOR_MULT:
5816 code = MULT_EXPR;
5817 break;
5818 case OPERATOR_DIV:
5820 Type *t = this->left_->type();
5821 if (t->float_type() != NULL || t->complex_type() != NULL)
5822 code = RDIV_EXPR;
5823 else
5824 code = TRUNC_DIV_EXPR;
5826 break;
5827 case OPERATOR_MOD:
5828 code = TRUNC_MOD_EXPR;
5829 break;
5830 case OPERATOR_LSHIFT:
5831 code = LSHIFT_EXPR;
5832 is_shift_op = true;
5833 break;
5834 case OPERATOR_RSHIFT:
5835 code = RSHIFT_EXPR;
5836 is_shift_op = true;
5837 break;
5838 case OPERATOR_AND:
5839 code = BIT_AND_EXPR;
5840 break;
5841 case OPERATOR_BITCLEAR:
5842 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5843 code = BIT_AND_EXPR;
5844 break;
5845 default:
5846 gcc_unreachable();
5849 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5851 if (this->left_->type()->is_string_type())
5853 gcc_assert(this->op_ == OPERATOR_PLUS);
5854 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5855 static tree string_plus_decl;
5856 return Gogo::call_builtin(&string_plus_decl,
5857 this->location(),
5858 "__go_string_plus",
5860 string_type,
5861 string_type,
5862 left,
5863 string_type,
5864 right);
5867 tree compute_type = excess_precision_type(type);
5868 if (compute_type != NULL_TREE)
5870 left = ::convert(compute_type, left);
5871 right = ::convert(compute_type, right);
5874 tree eval_saved = NULL_TREE;
5875 if (is_shift_op)
5877 // Make sure the values are evaluated.
5878 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
5880 left = save_expr(left);
5881 eval_saved = left;
5883 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
5885 right = save_expr(right);
5886 if (eval_saved == NULL_TREE)
5887 eval_saved = right;
5888 else
5889 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
5890 void_type_node, eval_saved, right);
5894 tree ret = fold_build2_loc(this->location(),
5895 code,
5896 compute_type != NULL_TREE ? compute_type : type,
5897 left, right);
5899 if (compute_type != NULL_TREE)
5900 ret = ::convert(type, ret);
5902 // In Go, a shift larger than the size of the type is well-defined.
5903 // This is not true in GENERIC, so we need to insert a conditional.
5904 if (is_shift_op)
5906 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
5907 gcc_assert(this->left_->type()->integer_type() != NULL);
5908 int bits = TYPE_PRECISION(TREE_TYPE(left));
5910 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
5911 build_int_cst_type(TREE_TYPE(right), bits));
5913 tree overflow_result = fold_convert_loc(this->location(),
5914 TREE_TYPE(left),
5915 integer_zero_node);
5916 if (this->op_ == OPERATOR_RSHIFT
5917 && !this->left_->type()->integer_type()->is_unsigned())
5919 tree neg = fold_build2_loc(this->location(), LT_EXPR,
5920 boolean_type_node, left,
5921 fold_convert_loc(this->location(),
5922 TREE_TYPE(left),
5923 integer_zero_node));
5924 tree neg_one = fold_build2_loc(this->location(),
5925 MINUS_EXPR, TREE_TYPE(left),
5926 fold_convert_loc(this->location(),
5927 TREE_TYPE(left),
5928 integer_zero_node),
5929 fold_convert_loc(this->location(),
5930 TREE_TYPE(left),
5931 integer_one_node));
5932 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
5933 TREE_TYPE(left), neg, neg_one,
5934 overflow_result);
5937 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
5938 compare, ret, overflow_result);
5940 if (eval_saved != NULL_TREE)
5941 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
5942 TREE_TYPE(ret), eval_saved, ret);
5945 return ret;
5948 // Export a binary expression.
5950 void
5951 Binary_expression::do_export(Export* exp) const
5953 exp->write_c_string("(");
5954 this->left_->export_expression(exp);
5955 switch (this->op_)
5957 case OPERATOR_OROR:
5958 exp->write_c_string(" || ");
5959 break;
5960 case OPERATOR_ANDAND:
5961 exp->write_c_string(" && ");
5962 break;
5963 case OPERATOR_EQEQ:
5964 exp->write_c_string(" == ");
5965 break;
5966 case OPERATOR_NOTEQ:
5967 exp->write_c_string(" != ");
5968 break;
5969 case OPERATOR_LT:
5970 exp->write_c_string(" < ");
5971 break;
5972 case OPERATOR_LE:
5973 exp->write_c_string(" <= ");
5974 break;
5975 case OPERATOR_GT:
5976 exp->write_c_string(" > ");
5977 break;
5978 case OPERATOR_GE:
5979 exp->write_c_string(" >= ");
5980 break;
5981 case OPERATOR_PLUS:
5982 exp->write_c_string(" + ");
5983 break;
5984 case OPERATOR_MINUS:
5985 exp->write_c_string(" - ");
5986 break;
5987 case OPERATOR_OR:
5988 exp->write_c_string(" | ");
5989 break;
5990 case OPERATOR_XOR:
5991 exp->write_c_string(" ^ ");
5992 break;
5993 case OPERATOR_MULT:
5994 exp->write_c_string(" * ");
5995 break;
5996 case OPERATOR_DIV:
5997 exp->write_c_string(" / ");
5998 break;
5999 case OPERATOR_MOD:
6000 exp->write_c_string(" % ");
6001 break;
6002 case OPERATOR_LSHIFT:
6003 exp->write_c_string(" << ");
6004 break;
6005 case OPERATOR_RSHIFT:
6006 exp->write_c_string(" >> ");
6007 break;
6008 case OPERATOR_AND:
6009 exp->write_c_string(" & ");
6010 break;
6011 case OPERATOR_BITCLEAR:
6012 exp->write_c_string(" &^ ");
6013 break;
6014 default:
6015 gcc_unreachable();
6017 this->right_->export_expression(exp);
6018 exp->write_c_string(")");
6021 // Import a binary expression.
6023 Expression*
6024 Binary_expression::do_import(Import* imp)
6026 imp->require_c_string("(");
6028 Expression* left = Expression::import_expression(imp);
6030 Operator op;
6031 if (imp->match_c_string(" || "))
6033 op = OPERATOR_OROR;
6034 imp->advance(4);
6036 else if (imp->match_c_string(" && "))
6038 op = OPERATOR_ANDAND;
6039 imp->advance(4);
6041 else if (imp->match_c_string(" == "))
6043 op = OPERATOR_EQEQ;
6044 imp->advance(4);
6046 else if (imp->match_c_string(" != "))
6048 op = OPERATOR_NOTEQ;
6049 imp->advance(4);
6051 else if (imp->match_c_string(" < "))
6053 op = OPERATOR_LT;
6054 imp->advance(3);
6056 else if (imp->match_c_string(" <= "))
6058 op = OPERATOR_LE;
6059 imp->advance(4);
6061 else if (imp->match_c_string(" > "))
6063 op = OPERATOR_GT;
6064 imp->advance(3);
6066 else if (imp->match_c_string(" >= "))
6068 op = OPERATOR_GE;
6069 imp->advance(4);
6071 else if (imp->match_c_string(" + "))
6073 op = OPERATOR_PLUS;
6074 imp->advance(3);
6076 else if (imp->match_c_string(" - "))
6078 op = OPERATOR_MINUS;
6079 imp->advance(3);
6081 else if (imp->match_c_string(" | "))
6083 op = OPERATOR_OR;
6084 imp->advance(3);
6086 else if (imp->match_c_string(" ^ "))
6088 op = OPERATOR_XOR;
6089 imp->advance(3);
6091 else if (imp->match_c_string(" * "))
6093 op = OPERATOR_MULT;
6094 imp->advance(3);
6096 else if (imp->match_c_string(" / "))
6098 op = OPERATOR_DIV;
6099 imp->advance(3);
6101 else if (imp->match_c_string(" % "))
6103 op = OPERATOR_MOD;
6104 imp->advance(3);
6106 else if (imp->match_c_string(" << "))
6108 op = OPERATOR_LSHIFT;
6109 imp->advance(4);
6111 else if (imp->match_c_string(" >> "))
6113 op = OPERATOR_RSHIFT;
6114 imp->advance(4);
6116 else if (imp->match_c_string(" & "))
6118 op = OPERATOR_AND;
6119 imp->advance(3);
6121 else if (imp->match_c_string(" &^ "))
6123 op = OPERATOR_BITCLEAR;
6124 imp->advance(4);
6126 else
6128 error_at(imp->location(), "unrecognized binary operator");
6129 return Expression::make_error(imp->location());
6132 Expression* right = Expression::import_expression(imp);
6134 imp->require_c_string(")");
6136 return Expression::make_binary(op, left, right, imp->location());
6139 // Make a binary expression.
6141 Expression*
6142 Expression::make_binary(Operator op, Expression* left, Expression* right,
6143 source_location location)
6145 return new Binary_expression(op, left, right, location);
6148 // Implement a comparison.
6150 tree
6151 Expression::comparison_tree(Translate_context* context, Operator op,
6152 Type* left_type, tree left_tree,
6153 Type* right_type, tree right_tree,
6154 source_location location)
6156 enum tree_code code;
6157 switch (op)
6159 case OPERATOR_EQEQ:
6160 code = EQ_EXPR;
6161 break;
6162 case OPERATOR_NOTEQ:
6163 code = NE_EXPR;
6164 break;
6165 case OPERATOR_LT:
6166 code = LT_EXPR;
6167 break;
6168 case OPERATOR_LE:
6169 code = LE_EXPR;
6170 break;
6171 case OPERATOR_GT:
6172 code = GT_EXPR;
6173 break;
6174 case OPERATOR_GE:
6175 code = GE_EXPR;
6176 break;
6177 default:
6178 gcc_unreachable();
6181 if (left_type->is_string_type() && right_type->is_string_type())
6183 tree string_type = Type::make_string_type()->get_tree(context->gogo());
6184 static tree string_compare_decl;
6185 left_tree = Gogo::call_builtin(&string_compare_decl,
6186 location,
6187 "__go_strcmp",
6189 integer_type_node,
6190 string_type,
6191 left_tree,
6192 string_type,
6193 right_tree);
6194 right_tree = build_int_cst_type(integer_type_node, 0);
6196 else if ((left_type->interface_type() != NULL
6197 && right_type->interface_type() == NULL
6198 && !right_type->is_nil_type())
6199 || (left_type->interface_type() == NULL
6200 && !left_type->is_nil_type()
6201 && right_type->interface_type() != NULL))
6203 // Comparing an interface value to a non-interface value.
6204 if (left_type->interface_type() == NULL)
6206 std::swap(left_type, right_type);
6207 std::swap(left_tree, right_tree);
6210 // The right operand is not an interface. We need to take its
6211 // address if it is not a pointer.
6212 tree make_tmp;
6213 tree arg;
6214 if (right_type->points_to() != NULL)
6216 make_tmp = NULL_TREE;
6217 arg = right_tree;
6219 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6221 make_tmp = NULL_TREE;
6222 arg = build_fold_addr_expr_loc(location, right_tree);
6223 if (DECL_P(right_tree))
6224 TREE_ADDRESSABLE(right_tree) = 1;
6226 else
6228 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6229 get_name(right_tree));
6230 DECL_IGNORED_P(tmp) = 0;
6231 DECL_INITIAL(tmp) = right_tree;
6232 TREE_ADDRESSABLE(tmp) = 1;
6233 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6234 SET_EXPR_LOCATION(make_tmp, location);
6235 arg = build_fold_addr_expr_loc(location, tmp);
6237 arg = fold_convert_loc(location, ptr_type_node, arg);
6239 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
6241 if (left_type->interface_type()->is_empty())
6243 static tree empty_interface_value_compare_decl;
6244 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6245 location,
6246 "__go_empty_interface_value_compare",
6248 integer_type_node,
6249 TREE_TYPE(left_tree),
6250 left_tree,
6251 TREE_TYPE(descriptor),
6252 descriptor,
6253 ptr_type_node,
6254 arg);
6255 if (left_tree == error_mark_node)
6256 return error_mark_node;
6257 // This can panic if the type is not comparable.
6258 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6260 else
6262 static tree interface_value_compare_decl;
6263 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6264 location,
6265 "__go_interface_value_compare",
6267 integer_type_node,
6268 TREE_TYPE(left_tree),
6269 left_tree,
6270 TREE_TYPE(descriptor),
6271 descriptor,
6272 ptr_type_node,
6273 arg);
6274 if (left_tree == error_mark_node)
6275 return error_mark_node;
6276 // This can panic if the type is not comparable.
6277 TREE_NOTHROW(interface_value_compare_decl) = 0;
6279 right_tree = build_int_cst_type(integer_type_node, 0);
6281 if (make_tmp != NULL_TREE)
6282 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6283 left_tree);
6285 else if (left_type->interface_type() != NULL
6286 && right_type->interface_type() != NULL)
6288 if (left_type->interface_type()->is_empty()
6289 && right_type->interface_type()->is_empty())
6291 static tree empty_interface_compare_decl;
6292 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6293 location,
6294 "__go_empty_interface_compare",
6296 integer_type_node,
6297 TREE_TYPE(left_tree),
6298 left_tree,
6299 TREE_TYPE(right_tree),
6300 right_tree);
6301 if (left_tree == error_mark_node)
6302 return error_mark_node;
6303 // This can panic if the type is uncomparable.
6304 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6306 else if (!left_type->interface_type()->is_empty()
6307 && !right_type->interface_type()->is_empty())
6309 static tree interface_compare_decl;
6310 left_tree = Gogo::call_builtin(&interface_compare_decl,
6311 location,
6312 "__go_interface_compare",
6314 integer_type_node,
6315 TREE_TYPE(left_tree),
6316 left_tree,
6317 TREE_TYPE(right_tree),
6318 right_tree);
6319 if (left_tree == error_mark_node)
6320 return error_mark_node;
6321 // This can panic if the type is uncomparable.
6322 TREE_NOTHROW(interface_compare_decl) = 0;
6324 else
6326 if (left_type->interface_type()->is_empty())
6328 gcc_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6329 std::swap(left_type, right_type);
6330 std::swap(left_tree, right_tree);
6332 gcc_assert(!left_type->interface_type()->is_empty());
6333 gcc_assert(right_type->interface_type()->is_empty());
6334 static tree interface_empty_compare_decl;
6335 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6336 location,
6337 "__go_interface_empty_compare",
6339 integer_type_node,
6340 TREE_TYPE(left_tree),
6341 left_tree,
6342 TREE_TYPE(right_tree),
6343 right_tree);
6344 if (left_tree == error_mark_node)
6345 return error_mark_node;
6346 // This can panic if the type is uncomparable.
6347 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6350 right_tree = build_int_cst_type(integer_type_node, 0);
6353 if (left_type->is_nil_type()
6354 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6356 std::swap(left_type, right_type);
6357 std::swap(left_tree, right_tree);
6360 if (right_type->is_nil_type())
6362 if (left_type->array_type() != NULL
6363 && left_type->array_type()->length() == NULL)
6365 Array_type* at = left_type->array_type();
6366 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6367 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6369 else if (left_type->interface_type() != NULL)
6371 // An interface is nil if the first field is nil.
6372 tree left_type_tree = TREE_TYPE(left_tree);
6373 gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6374 tree field = TYPE_FIELDS(left_type_tree);
6375 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6376 field, NULL_TREE);
6377 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6379 else
6381 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6382 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6386 if (left_tree == error_mark_node || right_tree == error_mark_node)
6387 return error_mark_node;
6389 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6390 if (CAN_HAVE_LOCATION_P(ret))
6391 SET_EXPR_LOCATION(ret, location);
6392 return ret;
6395 // Class Bound_method_expression.
6397 // Traversal.
6400 Bound_method_expression::do_traverse(Traverse* traverse)
6402 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6403 return TRAVERSE_EXIT;
6404 return Expression::traverse(&this->method_, traverse);
6407 // Return the type of a bound method expression. The type of this
6408 // object is really the type of the method with no receiver. We
6409 // should be able to get away with just returning the type of the
6410 // method.
6412 Type*
6413 Bound_method_expression::do_type()
6415 return this->method_->type();
6418 // Determine the types of a method expression.
6420 void
6421 Bound_method_expression::do_determine_type(const Type_context*)
6423 this->method_->determine_type_no_context();
6424 Type* mtype = this->method_->type();
6425 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6426 if (fntype == NULL || !fntype->is_method())
6427 this->expr_->determine_type_no_context();
6428 else
6430 Type_context subcontext(fntype->receiver()->type(), false);
6431 this->expr_->determine_type(&subcontext);
6435 // Check the types of a method expression.
6437 void
6438 Bound_method_expression::do_check_types(Gogo*)
6440 Type* type = this->method_->type()->deref();
6441 if (type == NULL
6442 || type->function_type() == NULL
6443 || !type->function_type()->is_method())
6444 this->report_error(_("object is not a method"));
6445 else
6447 Type* rtype = type->function_type()->receiver()->type()->deref();
6448 Type* etype = (this->expr_type_ != NULL
6449 ? this->expr_type_
6450 : this->expr_->type());
6451 etype = etype->deref();
6452 if (!Type::are_identical(rtype, etype, true, NULL))
6453 this->report_error(_("method type does not match object type"));
6457 // Get the tree for a method expression. There is no standard tree
6458 // representation for this. The only places it may currently be used
6459 // are in a Call_expression or a Go_statement, which will take it
6460 // apart directly. So this has nothing to do at present.
6462 tree
6463 Bound_method_expression::do_get_tree(Translate_context*)
6465 error_at(this->location(), "reference to method other than calling it");
6466 return error_mark_node;
6469 // Make a method expression.
6471 Bound_method_expression*
6472 Expression::make_bound_method(Expression* expr, Expression* method,
6473 source_location location)
6475 return new Bound_method_expression(expr, method, location);
6478 // Class Builtin_call_expression. This is used for a call to a
6479 // builtin function.
6481 class Builtin_call_expression : public Call_expression
6483 public:
6484 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6485 bool is_varargs, source_location location);
6487 protected:
6488 // This overrides Call_expression::do_lower.
6489 Expression*
6490 do_lower(Gogo*, Named_object*, int);
6492 bool
6493 do_is_constant() const;
6495 bool
6496 do_integer_constant_value(bool, mpz_t, Type**) const;
6498 bool
6499 do_float_constant_value(mpfr_t, Type**) const;
6501 bool
6502 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6504 Type*
6505 do_type();
6507 void
6508 do_determine_type(const Type_context*);
6510 void
6511 do_check_types(Gogo*);
6513 Expression*
6514 do_copy()
6516 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6517 this->args()->copy(),
6518 this->is_varargs(),
6519 this->location());
6522 tree
6523 do_get_tree(Translate_context*);
6525 void
6526 do_export(Export*) const;
6528 virtual bool
6529 do_is_recover_call() const;
6531 virtual void
6532 do_set_recover_arg(Expression*);
6534 private:
6535 // The builtin functions.
6536 enum Builtin_function_code
6538 BUILTIN_INVALID,
6540 // Predeclared builtin functions.
6541 BUILTIN_APPEND,
6542 BUILTIN_CAP,
6543 BUILTIN_CLOSE,
6544 BUILTIN_CLOSED,
6545 BUILTIN_COMPLEX,
6546 BUILTIN_COPY,
6547 BUILTIN_IMAG,
6548 BUILTIN_LEN,
6549 BUILTIN_MAKE,
6550 BUILTIN_NEW,
6551 BUILTIN_PANIC,
6552 BUILTIN_PRINT,
6553 BUILTIN_PRINTLN,
6554 BUILTIN_REAL,
6555 BUILTIN_RECOVER,
6557 // Builtin functions from the unsafe package.
6558 BUILTIN_ALIGNOF,
6559 BUILTIN_OFFSETOF,
6560 BUILTIN_SIZEOF
6563 Expression*
6564 one_arg() const;
6566 bool
6567 check_one_arg();
6569 static Type*
6570 real_imag_type(Type*);
6572 static Type*
6573 complex_type(Type*);
6575 // A pointer back to the general IR structure. This avoids a global
6576 // variable, or passing it around everywhere.
6577 Gogo* gogo_;
6578 // The builtin function being called.
6579 Builtin_function_code code_;
6580 // Used to stop endless loops when the length of an array uses len
6581 // or cap of the array itself.
6582 mutable bool seen_;
6585 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6586 Expression* fn,
6587 Expression_list* args,
6588 bool is_varargs,
6589 source_location location)
6590 : Call_expression(fn, args, is_varargs, location),
6591 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
6593 Func_expression* fnexp = this->fn()->func_expression();
6594 gcc_assert(fnexp != NULL);
6595 const std::string& name(fnexp->named_object()->name());
6596 if (name == "append")
6597 this->code_ = BUILTIN_APPEND;
6598 else if (name == "cap")
6599 this->code_ = BUILTIN_CAP;
6600 else if (name == "close")
6601 this->code_ = BUILTIN_CLOSE;
6602 else if (name == "closed")
6603 this->code_ = BUILTIN_CLOSED;
6604 else if (name == "complex")
6605 this->code_ = BUILTIN_COMPLEX;
6606 else if (name == "copy")
6607 this->code_ = BUILTIN_COPY;
6608 else if (name == "imag")
6609 this->code_ = BUILTIN_IMAG;
6610 else if (name == "len")
6611 this->code_ = BUILTIN_LEN;
6612 else if (name == "make")
6613 this->code_ = BUILTIN_MAKE;
6614 else if (name == "new")
6615 this->code_ = BUILTIN_NEW;
6616 else if (name == "panic")
6617 this->code_ = BUILTIN_PANIC;
6618 else if (name == "print")
6619 this->code_ = BUILTIN_PRINT;
6620 else if (name == "println")
6621 this->code_ = BUILTIN_PRINTLN;
6622 else if (name == "real")
6623 this->code_ = BUILTIN_REAL;
6624 else if (name == "recover")
6625 this->code_ = BUILTIN_RECOVER;
6626 else if (name == "Alignof")
6627 this->code_ = BUILTIN_ALIGNOF;
6628 else if (name == "Offsetof")
6629 this->code_ = BUILTIN_OFFSETOF;
6630 else if (name == "Sizeof")
6631 this->code_ = BUILTIN_SIZEOF;
6632 else
6633 gcc_unreachable();
6636 // Return whether this is a call to recover. This is a virtual
6637 // function called from the parent class.
6639 bool
6640 Builtin_call_expression::do_is_recover_call() const
6642 if (this->classification() == EXPRESSION_ERROR)
6643 return false;
6644 return this->code_ == BUILTIN_RECOVER;
6647 // Set the argument for a call to recover.
6649 void
6650 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6652 const Expression_list* args = this->args();
6653 gcc_assert(args == NULL || args->empty());
6654 Expression_list* new_args = new Expression_list();
6655 new_args->push_back(arg);
6656 this->set_args(new_args);
6659 // A traversal class which looks for a call expression.
6661 class Find_call_expression : public Traverse
6663 public:
6664 Find_call_expression()
6665 : Traverse(traverse_expressions),
6666 found_(false)
6670 expression(Expression**);
6672 bool
6673 found()
6674 { return this->found_; }
6676 private:
6677 bool found_;
6681 Find_call_expression::expression(Expression** pexpr)
6683 if ((*pexpr)->call_expression() != NULL)
6685 this->found_ = true;
6686 return TRAVERSE_EXIT;
6688 return TRAVERSE_CONTINUE;
6691 // Lower a builtin call expression. This turns new and make into
6692 // specific expressions. We also convert to a constant if we can.
6694 Expression*
6695 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6697 if (this->code_ == BUILTIN_NEW)
6699 const Expression_list* args = this->args();
6700 if (args == NULL || args->size() < 1)
6701 this->report_error(_("not enough arguments"));
6702 else if (args->size() > 1)
6703 this->report_error(_("too many arguments"));
6704 else
6706 Expression* arg = args->front();
6707 if (!arg->is_type_expression())
6709 error_at(arg->location(), "expected type");
6710 this->set_is_error();
6712 else
6713 return Expression::make_allocation(arg->type(), this->location());
6716 else if (this->code_ == BUILTIN_MAKE)
6718 const Expression_list* args = this->args();
6719 if (args == NULL || args->size() < 1)
6720 this->report_error(_("not enough arguments"));
6721 else
6723 Expression* arg = args->front();
6724 if (!arg->is_type_expression())
6726 error_at(arg->location(), "expected type");
6727 this->set_is_error();
6729 else
6731 Expression_list* newargs;
6732 if (args->size() == 1)
6733 newargs = NULL;
6734 else
6736 newargs = new Expression_list();
6737 Expression_list::const_iterator p = args->begin();
6738 ++p;
6739 for (; p != args->end(); ++p)
6740 newargs->push_back(*p);
6742 return Expression::make_make(arg->type(), newargs,
6743 this->location());
6747 else if (this->is_constant())
6749 // We can only lower len and cap if there are no function calls
6750 // in the arguments. Otherwise we have to make the call.
6751 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6753 Expression* arg = this->one_arg();
6754 if (!arg->is_constant())
6756 Find_call_expression find_call;
6757 Expression::traverse(&arg, &find_call);
6758 if (find_call.found())
6759 return this;
6763 mpz_t ival;
6764 mpz_init(ival);
6765 Type* type;
6766 if (this->integer_constant_value(true, ival, &type))
6768 Expression* ret = Expression::make_integer(&ival, type,
6769 this->location());
6770 mpz_clear(ival);
6771 return ret;
6773 mpz_clear(ival);
6775 mpfr_t rval;
6776 mpfr_init(rval);
6777 if (this->float_constant_value(rval, &type))
6779 Expression* ret = Expression::make_float(&rval, type,
6780 this->location());
6781 mpfr_clear(rval);
6782 return ret;
6785 mpfr_t imag;
6786 mpfr_init(imag);
6787 if (this->complex_constant_value(rval, imag, &type))
6789 Expression* ret = Expression::make_complex(&rval, &imag, type,
6790 this->location());
6791 mpfr_clear(rval);
6792 mpfr_clear(imag);
6793 return ret;
6795 mpfr_clear(rval);
6796 mpfr_clear(imag);
6798 else if (this->code_ == BUILTIN_RECOVER)
6800 if (function != NULL)
6801 function->func_value()->set_calls_recover();
6802 else
6804 // Calling recover outside of a function always returns the
6805 // nil empty interface.
6806 Type* eface = Type::make_interface_type(NULL, this->location());
6807 return Expression::make_cast(eface,
6808 Expression::make_nil(this->location()),
6809 this->location());
6812 else if (this->code_ == BUILTIN_APPEND)
6814 // Lower the varargs.
6815 const Expression_list* args = this->args();
6816 if (args == NULL || args->empty())
6817 return this;
6818 Type* slice_type = args->front()->type();
6819 if (!slice_type->is_open_array_type())
6821 error_at(args->front()->location(), "argument 1 must be a slice");
6822 this->set_is_error();
6823 return this;
6825 return this->lower_varargs(gogo, function, slice_type, 2);
6828 return this;
6831 // Return the type of the real or imag functions, given the type of
6832 // the argument. We need to map complex to float, complex64 to
6833 // float32, and complex128 to float64, so it has to be done by name.
6834 // This returns NULL if it can't figure out the type.
6836 Type*
6837 Builtin_call_expression::real_imag_type(Type* arg_type)
6839 if (arg_type == NULL || arg_type->is_abstract())
6840 return NULL;
6841 Named_type* nt = arg_type->named_type();
6842 if (nt == NULL)
6843 return NULL;
6844 while (nt->real_type()->named_type() != NULL)
6845 nt = nt->real_type()->named_type();
6846 if (nt->name() == "complex64")
6847 return Type::lookup_float_type("float32");
6848 else if (nt->name() == "complex128")
6849 return Type::lookup_float_type("float64");
6850 else
6851 return NULL;
6854 // Return the type of the complex function, given the type of one of the
6855 // argments. Like real_imag_type, we have to map by name.
6857 Type*
6858 Builtin_call_expression::complex_type(Type* arg_type)
6860 if (arg_type == NULL || arg_type->is_abstract())
6861 return NULL;
6862 Named_type* nt = arg_type->named_type();
6863 if (nt == NULL)
6864 return NULL;
6865 while (nt->real_type()->named_type() != NULL)
6866 nt = nt->real_type()->named_type();
6867 if (nt->name() == "float32")
6868 return Type::lookup_complex_type("complex64");
6869 else if (nt->name() == "float64")
6870 return Type::lookup_complex_type("complex128");
6871 else
6872 return NULL;
6875 // Return a single argument, or NULL if there isn't one.
6877 Expression*
6878 Builtin_call_expression::one_arg() const
6880 const Expression_list* args = this->args();
6881 if (args->size() != 1)
6882 return NULL;
6883 return args->front();
6886 // Return whether this is constant: len of a string, or len or cap of
6887 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6889 bool
6890 Builtin_call_expression::do_is_constant() const
6892 switch (this->code_)
6894 case BUILTIN_LEN:
6895 case BUILTIN_CAP:
6897 if (this->seen_)
6898 return false;
6900 Expression* arg = this->one_arg();
6901 if (arg == NULL)
6902 return false;
6903 Type* arg_type = arg->type();
6905 if (arg_type->points_to() != NULL
6906 && arg_type->points_to()->array_type() != NULL
6907 && !arg_type->points_to()->is_open_array_type())
6908 arg_type = arg_type->points_to();
6910 if (arg_type->array_type() != NULL
6911 && arg_type->array_type()->length() != NULL)
6912 return true;
6914 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6916 this->seen_ = true;
6917 bool ret = arg->is_constant();
6918 this->seen_ = false;
6919 return ret;
6922 break;
6924 case BUILTIN_SIZEOF:
6925 case BUILTIN_ALIGNOF:
6926 return this->one_arg() != NULL;
6928 case BUILTIN_OFFSETOF:
6930 Expression* arg = this->one_arg();
6931 if (arg == NULL)
6932 return false;
6933 return arg->field_reference_expression() != NULL;
6936 case BUILTIN_COMPLEX:
6938 const Expression_list* args = this->args();
6939 if (args != NULL && args->size() == 2)
6940 return args->front()->is_constant() && args->back()->is_constant();
6942 break;
6944 case BUILTIN_REAL:
6945 case BUILTIN_IMAG:
6947 Expression* arg = this->one_arg();
6948 return arg != NULL && arg->is_constant();
6951 default:
6952 break;
6955 return false;
6958 // Return an integer constant value if possible.
6960 bool
6961 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
6962 mpz_t val,
6963 Type** ptype) const
6965 if (this->code_ == BUILTIN_LEN
6966 || this->code_ == BUILTIN_CAP)
6968 Expression* arg = this->one_arg();
6969 if (arg == NULL)
6970 return false;
6971 Type* arg_type = arg->type();
6973 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6975 std::string sval;
6976 if (arg->string_constant_value(&sval))
6978 mpz_set_ui(val, sval.length());
6979 *ptype = Type::lookup_integer_type("int");
6980 return true;
6984 if (arg_type->points_to() != NULL
6985 && arg_type->points_to()->array_type() != NULL
6986 && !arg_type->points_to()->is_open_array_type())
6987 arg_type = arg_type->points_to();
6989 if (arg_type->array_type() != NULL
6990 && arg_type->array_type()->length() != NULL)
6992 if (this->seen_)
6993 return false;
6994 Expression* e = arg_type->array_type()->length();
6995 this->seen_ = true;
6996 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
6997 this->seen_ = false;
6998 if (r)
7000 *ptype = Type::lookup_integer_type("int");
7001 return true;
7005 else if (this->code_ == BUILTIN_SIZEOF
7006 || this->code_ == BUILTIN_ALIGNOF)
7008 Expression* arg = this->one_arg();
7009 if (arg == NULL)
7010 return false;
7011 Type* arg_type = arg->type();
7012 if (arg_type->is_error_type() || arg_type->is_undefined())
7013 return false;
7014 if (arg_type->is_abstract())
7015 return false;
7016 tree arg_type_tree = arg_type->get_tree(this->gogo_);
7017 if (arg_type_tree == error_mark_node)
7018 return false;
7019 unsigned long val_long;
7020 if (this->code_ == BUILTIN_SIZEOF)
7022 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
7023 gcc_assert(TREE_CODE(type_size) == INTEGER_CST);
7024 if (TREE_INT_CST_HIGH(type_size) != 0)
7025 return false;
7026 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
7027 val_long = static_cast<unsigned long>(val_wide);
7028 if (val_long != val_wide)
7029 return false;
7031 else if (this->code_ == BUILTIN_ALIGNOF)
7033 if (arg->field_reference_expression() == NULL)
7034 val_long = go_type_alignment(arg_type_tree);
7035 else
7037 // Calling unsafe.Alignof(s.f) returns the alignment of
7038 // the type of f when it is used as a field in a struct.
7039 val_long = go_field_alignment(arg_type_tree);
7042 else
7043 gcc_unreachable();
7044 mpz_set_ui(val, val_long);
7045 *ptype = NULL;
7046 return true;
7048 else if (this->code_ == BUILTIN_OFFSETOF)
7050 Expression* arg = this->one_arg();
7051 if (arg == NULL)
7052 return false;
7053 Field_reference_expression* farg = arg->field_reference_expression();
7054 if (farg == NULL)
7055 return false;
7056 Expression* struct_expr = farg->expr();
7057 Type* st = struct_expr->type();
7058 if (st->struct_type() == NULL)
7059 return false;
7060 tree struct_tree = st->get_tree(this->gogo_);
7061 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
7062 tree field = TYPE_FIELDS(struct_tree);
7063 for (unsigned int index = farg->field_index(); index > 0; --index)
7065 field = DECL_CHAIN(field);
7066 gcc_assert(field != NULL_TREE);
7068 HOST_WIDE_INT offset_wide = int_byte_position (field);
7069 if (offset_wide < 0)
7070 return false;
7071 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
7072 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
7073 return false;
7074 mpz_set_ui(val, offset_long);
7075 return true;
7077 return false;
7080 // Return a floating point constant value if possible.
7082 bool
7083 Builtin_call_expression::do_float_constant_value(mpfr_t val,
7084 Type** ptype) const
7086 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7088 Expression* arg = this->one_arg();
7089 if (arg == NULL)
7090 return false;
7092 mpfr_t real;
7093 mpfr_t imag;
7094 mpfr_init(real);
7095 mpfr_init(imag);
7097 bool ret = false;
7098 Type* type;
7099 if (arg->complex_constant_value(real, imag, &type))
7101 if (this->code_ == BUILTIN_REAL)
7102 mpfr_set(val, real, GMP_RNDN);
7103 else
7104 mpfr_set(val, imag, GMP_RNDN);
7105 *ptype = Builtin_call_expression::real_imag_type(type);
7106 ret = true;
7109 mpfr_clear(real);
7110 mpfr_clear(imag);
7111 return ret;
7114 return false;
7117 // Return a complex constant value if possible.
7119 bool
7120 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
7121 Type** ptype) const
7123 if (this->code_ == BUILTIN_COMPLEX)
7125 const Expression_list* args = this->args();
7126 if (args == NULL || args->size() != 2)
7127 return false;
7129 mpfr_t r;
7130 mpfr_init(r);
7131 Type* rtype;
7132 if (!args->front()->float_constant_value(r, &rtype))
7134 mpfr_clear(r);
7135 return false;
7138 mpfr_t i;
7139 mpfr_init(i);
7141 bool ret = false;
7142 Type* itype;
7143 if (args->back()->float_constant_value(i, &itype)
7144 && Type::are_identical(rtype, itype, false, NULL))
7146 mpfr_set(real, r, GMP_RNDN);
7147 mpfr_set(imag, i, GMP_RNDN);
7148 *ptype = Builtin_call_expression::complex_type(rtype);
7149 ret = true;
7152 mpfr_clear(r);
7153 mpfr_clear(i);
7155 return ret;
7158 return false;
7161 // Return the type.
7163 Type*
7164 Builtin_call_expression::do_type()
7166 switch (this->code_)
7168 case BUILTIN_INVALID:
7169 default:
7170 gcc_unreachable();
7172 case BUILTIN_NEW:
7173 case BUILTIN_MAKE:
7175 const Expression_list* args = this->args();
7176 if (args == NULL || args->empty())
7177 return Type::make_error_type();
7178 return Type::make_pointer_type(args->front()->type());
7181 case BUILTIN_CAP:
7182 case BUILTIN_COPY:
7183 case BUILTIN_LEN:
7184 case BUILTIN_ALIGNOF:
7185 case BUILTIN_OFFSETOF:
7186 case BUILTIN_SIZEOF:
7187 return Type::lookup_integer_type("int");
7189 case BUILTIN_CLOSE:
7190 case BUILTIN_PANIC:
7191 case BUILTIN_PRINT:
7192 case BUILTIN_PRINTLN:
7193 return Type::make_void_type();
7195 case BUILTIN_CLOSED:
7196 return Type::lookup_bool_type();
7198 case BUILTIN_RECOVER:
7199 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
7201 case BUILTIN_APPEND:
7203 const Expression_list* args = this->args();
7204 if (args == NULL || args->empty())
7205 return Type::make_error_type();
7206 return args->front()->type();
7209 case BUILTIN_REAL:
7210 case BUILTIN_IMAG:
7212 Expression* arg = this->one_arg();
7213 if (arg == NULL)
7214 return Type::make_error_type();
7215 Type* t = arg->type();
7216 if (t->is_abstract())
7217 t = t->make_non_abstract_type();
7218 t = Builtin_call_expression::real_imag_type(t);
7219 if (t == NULL)
7220 t = Type::make_error_type();
7221 return t;
7224 case BUILTIN_COMPLEX:
7226 const Expression_list* args = this->args();
7227 if (args == NULL || args->size() != 2)
7228 return Type::make_error_type();
7229 Type* t = args->front()->type();
7230 if (t->is_abstract())
7232 t = args->back()->type();
7233 if (t->is_abstract())
7234 t = t->make_non_abstract_type();
7236 t = Builtin_call_expression::complex_type(t);
7237 if (t == NULL)
7238 t = Type::make_error_type();
7239 return t;
7244 // Determine the type.
7246 void
7247 Builtin_call_expression::do_determine_type(const Type_context* context)
7249 this->fn()->determine_type_no_context();
7251 const Expression_list* args = this->args();
7253 bool is_print;
7254 Type* arg_type = NULL;
7255 switch (this->code_)
7257 case BUILTIN_PRINT:
7258 case BUILTIN_PRINTLN:
7259 // Do not force a large integer constant to "int".
7260 is_print = true;
7261 break;
7263 case BUILTIN_REAL:
7264 case BUILTIN_IMAG:
7265 arg_type = Builtin_call_expression::complex_type(context->type);
7266 is_print = false;
7267 break;
7269 case BUILTIN_COMPLEX:
7271 // For the complex function the type of one operand can
7272 // determine the type of the other, as in a binary expression.
7273 arg_type = Builtin_call_expression::real_imag_type(context->type);
7274 if (args != NULL && args->size() == 2)
7276 Type* t1 = args->front()->type();
7277 Type* t2 = args->front()->type();
7278 if (!t1->is_abstract())
7279 arg_type = t1;
7280 else if (!t2->is_abstract())
7281 arg_type = t2;
7283 is_print = false;
7285 break;
7287 default:
7288 is_print = false;
7289 break;
7292 if (args != NULL)
7294 for (Expression_list::const_iterator pa = args->begin();
7295 pa != args->end();
7296 ++pa)
7298 Type_context subcontext;
7299 subcontext.type = arg_type;
7301 if (is_print)
7303 // We want to print large constants, we so can't just
7304 // use the appropriate nonabstract type. Use uint64 for
7305 // an integer if we know it is nonnegative, otherwise
7306 // use int64 for a integer, otherwise use float64 for a
7307 // float or complex128 for a complex.
7308 Type* want_type = NULL;
7309 Type* atype = (*pa)->type();
7310 if (atype->is_abstract())
7312 if (atype->integer_type() != NULL)
7314 mpz_t val;
7315 mpz_init(val);
7316 Type* dummy;
7317 if (this->integer_constant_value(true, val, &dummy)
7318 && mpz_sgn(val) >= 0)
7319 want_type = Type::lookup_integer_type("uint64");
7320 else
7321 want_type = Type::lookup_integer_type("int64");
7322 mpz_clear(val);
7324 else if (atype->float_type() != NULL)
7325 want_type = Type::lookup_float_type("float64");
7326 else if (atype->complex_type() != NULL)
7327 want_type = Type::lookup_complex_type("complex128");
7328 else if (atype->is_abstract_string_type())
7329 want_type = Type::lookup_string_type();
7330 else if (atype->is_abstract_boolean_type())
7331 want_type = Type::lookup_bool_type();
7332 else
7333 gcc_unreachable();
7334 subcontext.type = want_type;
7338 (*pa)->determine_type(&subcontext);
7343 // If there is exactly one argument, return true. Otherwise give an
7344 // error message and return false.
7346 bool
7347 Builtin_call_expression::check_one_arg()
7349 const Expression_list* args = this->args();
7350 if (args == NULL || args->size() < 1)
7352 this->report_error(_("not enough arguments"));
7353 return false;
7355 else if (args->size() > 1)
7357 this->report_error(_("too many arguments"));
7358 return false;
7360 if (args->front()->is_error_expression()
7361 || args->front()->type()->is_error_type()
7362 || args->front()->type()->is_undefined())
7364 this->set_is_error();
7365 return false;
7367 return true;
7370 // Check argument types for a builtin function.
7372 void
7373 Builtin_call_expression::do_check_types(Gogo*)
7375 switch (this->code_)
7377 case BUILTIN_INVALID:
7378 case BUILTIN_NEW:
7379 case BUILTIN_MAKE:
7380 return;
7382 case BUILTIN_LEN:
7383 case BUILTIN_CAP:
7385 // The single argument may be either a string or an array or a
7386 // map or a channel, or a pointer to a closed array.
7387 if (this->check_one_arg())
7389 Type* arg_type = this->one_arg()->type();
7390 if (arg_type->points_to() != NULL
7391 && arg_type->points_to()->array_type() != NULL
7392 && !arg_type->points_to()->is_open_array_type())
7393 arg_type = arg_type->points_to();
7394 if (this->code_ == BUILTIN_CAP)
7396 if (!arg_type->is_error_type()
7397 && arg_type->array_type() == NULL
7398 && arg_type->channel_type() == NULL)
7399 this->report_error(_("argument must be array or slice "
7400 "or channel"));
7402 else
7404 if (!arg_type->is_error_type()
7405 && !arg_type->is_string_type()
7406 && arg_type->array_type() == NULL
7407 && arg_type->map_type() == NULL
7408 && arg_type->channel_type() == NULL)
7409 this->report_error(_("argument must be string or "
7410 "array or slice or map or channel"));
7414 break;
7416 case BUILTIN_PRINT:
7417 case BUILTIN_PRINTLN:
7419 const Expression_list* args = this->args();
7420 if (args == NULL)
7422 if (this->code_ == BUILTIN_PRINT)
7423 warning_at(this->location(), 0,
7424 "no arguments for builtin function %<%s%>",
7425 (this->code_ == BUILTIN_PRINT
7426 ? "print"
7427 : "println"));
7429 else
7431 for (Expression_list::const_iterator p = args->begin();
7432 p != args->end();
7433 ++p)
7435 Type* type = (*p)->type();
7436 if (type->is_error_type()
7437 || type->is_string_type()
7438 || type->integer_type() != NULL
7439 || type->float_type() != NULL
7440 || type->complex_type() != NULL
7441 || type->is_boolean_type()
7442 || type->points_to() != NULL
7443 || type->interface_type() != NULL
7444 || type->channel_type() != NULL
7445 || type->map_type() != NULL
7446 || type->function_type() != NULL
7447 || type->is_open_array_type())
7449 else
7450 this->report_error(_("unsupported argument type to "
7451 "builtin function"));
7455 break;
7457 case BUILTIN_CLOSE:
7458 case BUILTIN_CLOSED:
7459 if (this->check_one_arg())
7461 if (this->one_arg()->type()->channel_type() == NULL)
7462 this->report_error(_("argument must be channel"));
7464 break;
7466 case BUILTIN_PANIC:
7467 case BUILTIN_SIZEOF:
7468 case BUILTIN_ALIGNOF:
7469 this->check_one_arg();
7470 break;
7472 case BUILTIN_RECOVER:
7473 if (this->args() != NULL && !this->args()->empty())
7474 this->report_error(_("too many arguments"));
7475 break;
7477 case BUILTIN_OFFSETOF:
7478 if (this->check_one_arg())
7480 Expression* arg = this->one_arg();
7481 if (arg->field_reference_expression() == NULL)
7482 this->report_error(_("argument must be a field reference"));
7484 break;
7486 case BUILTIN_COPY:
7488 const Expression_list* args = this->args();
7489 if (args == NULL || args->size() < 2)
7491 this->report_error(_("not enough arguments"));
7492 break;
7494 else if (args->size() > 2)
7496 this->report_error(_("too many arguments"));
7497 break;
7499 Type* arg1_type = args->front()->type();
7500 Type* arg2_type = args->back()->type();
7501 if (arg1_type->is_error_type() || arg2_type->is_error_type())
7502 break;
7504 Type* e1;
7505 if (arg1_type->is_open_array_type())
7506 e1 = arg1_type->array_type()->element_type();
7507 else
7509 this->report_error(_("left argument must be a slice"));
7510 break;
7513 Type* e2;
7514 if (arg2_type->is_open_array_type())
7515 e2 = arg2_type->array_type()->element_type();
7516 else if (arg2_type->is_string_type())
7517 e2 = Type::lookup_integer_type("uint8");
7518 else
7520 this->report_error(_("right argument must be a slice or a string"));
7521 break;
7524 if (!Type::are_identical(e1, e2, true, NULL))
7525 this->report_error(_("element types must be the same"));
7527 break;
7529 case BUILTIN_APPEND:
7531 const Expression_list* args = this->args();
7532 if (args == NULL || args->size() < 2)
7534 this->report_error(_("not enough arguments"));
7535 break;
7537 if (args->size() > 2)
7539 this->report_error(_("too many arguments"));
7540 break;
7542 std::string reason;
7543 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7544 &reason))
7546 if (reason.empty())
7547 this->report_error(_("arguments 1 and 2 have different types"));
7548 else
7550 error_at(this->location(),
7551 "arguments 1 and 2 have different types (%s)",
7552 reason.c_str());
7553 this->set_is_error();
7556 break;
7559 case BUILTIN_REAL:
7560 case BUILTIN_IMAG:
7561 if (this->check_one_arg())
7563 if (this->one_arg()->type()->complex_type() == NULL)
7564 this->report_error(_("argument must have complex type"));
7566 break;
7568 case BUILTIN_COMPLEX:
7570 const Expression_list* args = this->args();
7571 if (args == NULL || args->size() < 2)
7572 this->report_error(_("not enough arguments"));
7573 else if (args->size() > 2)
7574 this->report_error(_("too many arguments"));
7575 else if (args->front()->is_error_expression()
7576 || args->front()->type()->is_error_type()
7577 || args->back()->is_error_expression()
7578 || args->back()->type()->is_error_type())
7579 this->set_is_error();
7580 else if (!Type::are_identical(args->front()->type(),
7581 args->back()->type(), true, NULL))
7582 this->report_error(_("complex arguments must have identical types"));
7583 else if (args->front()->type()->float_type() == NULL)
7584 this->report_error(_("complex arguments must have "
7585 "floating-point type"));
7587 break;
7589 default:
7590 gcc_unreachable();
7594 // Return the tree for a builtin function.
7596 tree
7597 Builtin_call_expression::do_get_tree(Translate_context* context)
7599 Gogo* gogo = context->gogo();
7600 source_location location = this->location();
7601 switch (this->code_)
7603 case BUILTIN_INVALID:
7604 case BUILTIN_NEW:
7605 case BUILTIN_MAKE:
7606 gcc_unreachable();
7608 case BUILTIN_LEN:
7609 case BUILTIN_CAP:
7611 const Expression_list* args = this->args();
7612 gcc_assert(args != NULL && args->size() == 1);
7613 Expression* arg = *args->begin();
7614 Type* arg_type = arg->type();
7616 if (this->seen_)
7618 gcc_assert(saw_errors());
7619 return error_mark_node;
7621 this->seen_ = true;
7623 tree arg_tree = arg->get_tree(context);
7625 this->seen_ = false;
7627 if (arg_tree == error_mark_node)
7628 return error_mark_node;
7630 if (arg_type->points_to() != NULL)
7632 arg_type = arg_type->points_to();
7633 gcc_assert(arg_type->array_type() != NULL
7634 && !arg_type->is_open_array_type());
7635 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7636 arg_tree = build_fold_indirect_ref(arg_tree);
7639 tree val_tree;
7640 if (this->code_ == BUILTIN_LEN)
7642 if (arg_type->is_string_type())
7643 val_tree = String_type::length_tree(gogo, arg_tree);
7644 else if (arg_type->array_type() != NULL)
7646 if (this->seen_)
7648 gcc_assert(saw_errors());
7649 return error_mark_node;
7651 this->seen_ = true;
7652 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7653 this->seen_ = false;
7655 else if (arg_type->map_type() != NULL)
7657 static tree map_len_fndecl;
7658 val_tree = Gogo::call_builtin(&map_len_fndecl,
7659 location,
7660 "__go_map_len",
7662 sizetype,
7663 arg_type->get_tree(gogo),
7664 arg_tree);
7666 else if (arg_type->channel_type() != NULL)
7668 static tree chan_len_fndecl;
7669 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7670 location,
7671 "__go_chan_len",
7673 sizetype,
7674 arg_type->get_tree(gogo),
7675 arg_tree);
7677 else
7678 gcc_unreachable();
7680 else
7682 if (arg_type->array_type() != NULL)
7684 if (this->seen_)
7686 gcc_assert(saw_errors());
7687 return error_mark_node;
7689 this->seen_ = true;
7690 val_tree = arg_type->array_type()->capacity_tree(gogo,
7691 arg_tree);
7692 this->seen_ = false;
7694 else if (arg_type->channel_type() != NULL)
7696 static tree chan_cap_fndecl;
7697 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7698 location,
7699 "__go_chan_cap",
7701 sizetype,
7702 arg_type->get_tree(gogo),
7703 arg_tree);
7705 else
7706 gcc_unreachable();
7709 if (val_tree == error_mark_node)
7710 return error_mark_node;
7712 tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo);
7713 if (type_tree == TREE_TYPE(val_tree))
7714 return val_tree;
7715 else
7716 return fold(convert_to_integer(type_tree, val_tree));
7719 case BUILTIN_PRINT:
7720 case BUILTIN_PRINTLN:
7722 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7723 tree stmt_list = NULL_TREE;
7725 const Expression_list* call_args = this->args();
7726 if (call_args != NULL)
7728 for (Expression_list::const_iterator p = call_args->begin();
7729 p != call_args->end();
7730 ++p)
7732 if (is_ln && p != call_args->begin())
7734 static tree print_space_fndecl;
7735 tree call = Gogo::call_builtin(&print_space_fndecl,
7736 location,
7737 "__go_print_space",
7739 void_type_node);
7740 if (call == error_mark_node)
7741 return error_mark_node;
7742 append_to_statement_list(call, &stmt_list);
7745 Type* type = (*p)->type();
7747 tree arg = (*p)->get_tree(context);
7748 if (arg == error_mark_node)
7749 return error_mark_node;
7751 tree* pfndecl;
7752 const char* fnname;
7753 if (type->is_string_type())
7755 static tree print_string_fndecl;
7756 pfndecl = &print_string_fndecl;
7757 fnname = "__go_print_string";
7759 else if (type->integer_type() != NULL
7760 && type->integer_type()->is_unsigned())
7762 static tree print_uint64_fndecl;
7763 pfndecl = &print_uint64_fndecl;
7764 fnname = "__go_print_uint64";
7765 Type* itype = Type::lookup_integer_type("uint64");
7766 arg = fold_convert_loc(location, itype->get_tree(gogo),
7767 arg);
7769 else if (type->integer_type() != NULL)
7771 static tree print_int64_fndecl;
7772 pfndecl = &print_int64_fndecl;
7773 fnname = "__go_print_int64";
7774 Type* itype = Type::lookup_integer_type("int64");
7775 arg = fold_convert_loc(location, itype->get_tree(gogo),
7776 arg);
7778 else if (type->float_type() != NULL)
7780 static tree print_double_fndecl;
7781 pfndecl = &print_double_fndecl;
7782 fnname = "__go_print_double";
7783 arg = fold_convert_loc(location, double_type_node, arg);
7785 else if (type->complex_type() != NULL)
7787 static tree print_complex_fndecl;
7788 pfndecl = &print_complex_fndecl;
7789 fnname = "__go_print_complex";
7790 arg = fold_convert_loc(location, complex_double_type_node,
7791 arg);
7793 else if (type->is_boolean_type())
7795 static tree print_bool_fndecl;
7796 pfndecl = &print_bool_fndecl;
7797 fnname = "__go_print_bool";
7799 else if (type->points_to() != NULL
7800 || type->channel_type() != NULL
7801 || type->map_type() != NULL
7802 || type->function_type() != NULL)
7804 static tree print_pointer_fndecl;
7805 pfndecl = &print_pointer_fndecl;
7806 fnname = "__go_print_pointer";
7807 arg = fold_convert_loc(location, ptr_type_node, arg);
7809 else if (type->interface_type() != NULL)
7811 if (type->interface_type()->is_empty())
7813 static tree print_empty_interface_fndecl;
7814 pfndecl = &print_empty_interface_fndecl;
7815 fnname = "__go_print_empty_interface";
7817 else
7819 static tree print_interface_fndecl;
7820 pfndecl = &print_interface_fndecl;
7821 fnname = "__go_print_interface";
7824 else if (type->is_open_array_type())
7826 static tree print_slice_fndecl;
7827 pfndecl = &print_slice_fndecl;
7828 fnname = "__go_print_slice";
7830 else
7831 gcc_unreachable();
7833 tree call = Gogo::call_builtin(pfndecl,
7834 location,
7835 fnname,
7837 void_type_node,
7838 TREE_TYPE(arg),
7839 arg);
7840 if (call == error_mark_node)
7841 return error_mark_node;
7842 append_to_statement_list(call, &stmt_list);
7846 if (is_ln)
7848 static tree print_nl_fndecl;
7849 tree call = Gogo::call_builtin(&print_nl_fndecl,
7850 location,
7851 "__go_print_nl",
7853 void_type_node);
7854 if (call == error_mark_node)
7855 return error_mark_node;
7856 append_to_statement_list(call, &stmt_list);
7859 return stmt_list;
7862 case BUILTIN_PANIC:
7864 const Expression_list* args = this->args();
7865 gcc_assert(args != NULL && args->size() == 1);
7866 Expression* arg = args->front();
7867 tree arg_tree = arg->get_tree(context);
7868 if (arg_tree == error_mark_node)
7869 return error_mark_node;
7870 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7871 arg_tree = Expression::convert_for_assignment(context, empty,
7872 arg->type(),
7873 arg_tree, location);
7874 static tree panic_fndecl;
7875 tree call = Gogo::call_builtin(&panic_fndecl,
7876 location,
7877 "__go_panic",
7879 void_type_node,
7880 TREE_TYPE(arg_tree),
7881 arg_tree);
7882 if (call == error_mark_node)
7883 return error_mark_node;
7884 // This function will throw an exception.
7885 TREE_NOTHROW(panic_fndecl) = 0;
7886 // This function will not return.
7887 TREE_THIS_VOLATILE(panic_fndecl) = 1;
7888 return call;
7891 case BUILTIN_RECOVER:
7893 // The argument is set when building recover thunks. It's a
7894 // boolean value which is true if we can recover a value now.
7895 const Expression_list* args = this->args();
7896 gcc_assert(args != NULL && args->size() == 1);
7897 Expression* arg = args->front();
7898 tree arg_tree = arg->get_tree(context);
7899 if (arg_tree == error_mark_node)
7900 return error_mark_node;
7902 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7903 tree empty_tree = empty->get_tree(context->gogo());
7905 Type* nil_type = Type::make_nil_type();
7906 Expression* nil = Expression::make_nil(location);
7907 tree nil_tree = nil->get_tree(context);
7908 tree empty_nil_tree = Expression::convert_for_assignment(context,
7909 empty,
7910 nil_type,
7911 nil_tree,
7912 location);
7914 // We need to handle a deferred call to recover specially,
7915 // because it changes whether it can recover a panic or not.
7916 // See test7 in test/recover1.go.
7917 tree call;
7918 if (this->is_deferred())
7920 static tree deferred_recover_fndecl;
7921 call = Gogo::call_builtin(&deferred_recover_fndecl,
7922 location,
7923 "__go_deferred_recover",
7925 empty_tree);
7927 else
7929 static tree recover_fndecl;
7930 call = Gogo::call_builtin(&recover_fndecl,
7931 location,
7932 "__go_recover",
7934 empty_tree);
7936 if (call == error_mark_node)
7937 return error_mark_node;
7938 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
7939 call, empty_nil_tree);
7942 case BUILTIN_CLOSE:
7943 case BUILTIN_CLOSED:
7945 const Expression_list* args = this->args();
7946 gcc_assert(args != NULL && args->size() == 1);
7947 Expression* arg = args->front();
7948 tree arg_tree = arg->get_tree(context);
7949 if (arg_tree == error_mark_node)
7950 return error_mark_node;
7951 if (this->code_ == BUILTIN_CLOSE)
7953 static tree close_fndecl;
7954 return Gogo::call_builtin(&close_fndecl,
7955 location,
7956 "__go_builtin_close",
7958 void_type_node,
7959 TREE_TYPE(arg_tree),
7960 arg_tree);
7962 else
7964 static tree closed_fndecl;
7965 return Gogo::call_builtin(&closed_fndecl,
7966 location,
7967 "__go_builtin_closed",
7969 boolean_type_node,
7970 TREE_TYPE(arg_tree),
7971 arg_tree);
7975 case BUILTIN_SIZEOF:
7976 case BUILTIN_OFFSETOF:
7977 case BUILTIN_ALIGNOF:
7979 mpz_t val;
7980 mpz_init(val);
7981 Type* dummy;
7982 bool b = this->integer_constant_value(true, val, &dummy);
7983 if (!b)
7985 gcc_assert(saw_errors());
7986 return error_mark_node;
7988 tree type = Type::lookup_integer_type("int")->get_tree(gogo);
7989 tree ret = Expression::integer_constant_tree(val, type);
7990 mpz_clear(val);
7991 return ret;
7994 case BUILTIN_COPY:
7996 const Expression_list* args = this->args();
7997 gcc_assert(args != NULL && args->size() == 2);
7998 Expression* arg1 = args->front();
7999 Expression* arg2 = args->back();
8001 tree arg1_tree = arg1->get_tree(context);
8002 tree arg2_tree = arg2->get_tree(context);
8003 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8004 return error_mark_node;
8006 Type* arg1_type = arg1->type();
8007 Array_type* at = arg1_type->array_type();
8008 arg1_tree = save_expr(arg1_tree);
8009 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
8010 tree arg1_len = at->length_tree(gogo, arg1_tree);
8011 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
8012 return error_mark_node;
8014 Type* arg2_type = arg2->type();
8015 tree arg2_val;
8016 tree arg2_len;
8017 if (arg2_type->is_open_array_type())
8019 at = arg2_type->array_type();
8020 arg2_tree = save_expr(arg2_tree);
8021 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8022 arg2_len = at->length_tree(gogo, arg2_tree);
8024 else
8026 arg2_tree = save_expr(arg2_tree);
8027 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8028 arg2_len = String_type::length_tree(gogo, arg2_tree);
8030 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8031 return error_mark_node;
8033 arg1_len = save_expr(arg1_len);
8034 arg2_len = save_expr(arg2_len);
8035 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
8036 fold_build2_loc(location, LT_EXPR,
8037 boolean_type_node,
8038 arg1_len, arg2_len),
8039 arg1_len, arg2_len);
8040 len = save_expr(len);
8042 Type* element_type = at->element_type();
8043 tree element_type_tree = element_type->get_tree(gogo);
8044 if (element_type_tree == error_mark_node)
8045 return error_mark_node;
8046 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8047 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
8048 len);
8049 bytecount = fold_build2_loc(location, MULT_EXPR,
8050 TREE_TYPE(element_size),
8051 bytecount, element_size);
8052 bytecount = fold_convert_loc(location, size_type_node, bytecount);
8054 arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val);
8055 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8057 static tree copy_fndecl;
8058 tree call = Gogo::call_builtin(&copy_fndecl,
8059 location,
8060 "__go_copy",
8062 void_type_node,
8063 ptr_type_node,
8064 arg1_val,
8065 ptr_type_node,
8066 arg2_val,
8067 size_type_node,
8068 bytecount);
8069 if (call == error_mark_node)
8070 return error_mark_node;
8072 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
8073 call, len);
8076 case BUILTIN_APPEND:
8078 const Expression_list* args = this->args();
8079 gcc_assert(args != NULL && args->size() == 2);
8080 Expression* arg1 = args->front();
8081 Expression* arg2 = args->back();
8083 tree arg1_tree = arg1->get_tree(context);
8084 tree arg2_tree = arg2->get_tree(context);
8085 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8086 return error_mark_node;
8088 Array_type* at = arg1->type()->array_type();
8089 Type* element_type = at->element_type();
8091 arg2_tree = Expression::convert_for_assignment(context, at,
8092 arg2->type(),
8093 arg2_tree,
8094 location);
8095 if (arg2_tree == error_mark_node)
8096 return error_mark_node;
8098 arg2_tree = save_expr(arg2_tree);
8099 tree arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8100 tree arg2_len = at->length_tree(gogo, arg2_tree);
8101 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8102 return error_mark_node;
8103 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8104 arg2_len = fold_convert_loc(location, size_type_node, arg2_len);
8106 tree element_type_tree = element_type->get_tree(gogo);
8107 if (element_type_tree == error_mark_node)
8108 return error_mark_node;
8109 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8110 element_size = fold_convert_loc(location, size_type_node,
8111 element_size);
8113 // We rebuild the decl each time since the slice types may
8114 // change.
8115 tree append_fndecl = NULL_TREE;
8116 return Gogo::call_builtin(&append_fndecl,
8117 location,
8118 "__go_append",
8120 TREE_TYPE(arg1_tree),
8121 TREE_TYPE(arg1_tree),
8122 arg1_tree,
8123 ptr_type_node,
8124 arg2_val,
8125 size_type_node,
8126 arg2_len,
8127 size_type_node,
8128 element_size);
8131 case BUILTIN_REAL:
8132 case BUILTIN_IMAG:
8134 const Expression_list* args = this->args();
8135 gcc_assert(args != NULL && args->size() == 1);
8136 Expression* arg = args->front();
8137 tree arg_tree = arg->get_tree(context);
8138 if (arg_tree == error_mark_node)
8139 return error_mark_node;
8140 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8141 if (this->code_ == BUILTIN_REAL)
8142 return fold_build1_loc(location, REALPART_EXPR,
8143 TREE_TYPE(TREE_TYPE(arg_tree)),
8144 arg_tree);
8145 else
8146 return fold_build1_loc(location, IMAGPART_EXPR,
8147 TREE_TYPE(TREE_TYPE(arg_tree)),
8148 arg_tree);
8151 case BUILTIN_COMPLEX:
8153 const Expression_list* args = this->args();
8154 gcc_assert(args != NULL && args->size() == 2);
8155 tree r = args->front()->get_tree(context);
8156 tree i = args->back()->get_tree(context);
8157 if (r == error_mark_node || i == error_mark_node)
8158 return error_mark_node;
8159 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8160 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8161 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8162 return fold_build2_loc(location, COMPLEX_EXPR,
8163 build_complex_type(TREE_TYPE(r)),
8164 r, i);
8167 default:
8168 gcc_unreachable();
8172 // We have to support exporting a builtin call expression, because
8173 // code can set a constant to the result of a builtin expression.
8175 void
8176 Builtin_call_expression::do_export(Export* exp) const
8178 bool ok = false;
8180 mpz_t val;
8181 mpz_init(val);
8182 Type* dummy;
8183 if (this->integer_constant_value(true, val, &dummy))
8185 Integer_expression::export_integer(exp, val);
8186 ok = true;
8188 mpz_clear(val);
8190 if (!ok)
8192 mpfr_t fval;
8193 mpfr_init(fval);
8194 if (this->float_constant_value(fval, &dummy))
8196 Float_expression::export_float(exp, fval);
8197 ok = true;
8199 mpfr_clear(fval);
8202 if (!ok)
8204 mpfr_t real;
8205 mpfr_t imag;
8206 mpfr_init(real);
8207 mpfr_init(imag);
8208 if (this->complex_constant_value(real, imag, &dummy))
8210 Complex_expression::export_complex(exp, real, imag);
8211 ok = true;
8213 mpfr_clear(real);
8214 mpfr_clear(imag);
8217 if (!ok)
8219 error_at(this->location(), "value is not constant");
8220 return;
8223 // A trailing space lets us reliably identify the end of the number.
8224 exp->write_c_string(" ");
8227 // Class Call_expression.
8229 // Traversal.
8232 Call_expression::do_traverse(Traverse* traverse)
8234 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8235 return TRAVERSE_EXIT;
8236 if (this->args_ != NULL)
8238 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8239 return TRAVERSE_EXIT;
8241 return TRAVERSE_CONTINUE;
8244 // Lower a call statement.
8246 Expression*
8247 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
8249 // A type case can look like a function call.
8250 if (this->fn_->is_type_expression()
8251 && this->args_ != NULL
8252 && this->args_->size() == 1)
8253 return Expression::make_cast(this->fn_->type(), this->args_->front(),
8254 this->location());
8256 // Recognize a call to a builtin function.
8257 Func_expression* fne = this->fn_->func_expression();
8258 if (fne != NULL
8259 && fne->named_object()->is_function_declaration()
8260 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8261 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8262 this->is_varargs_, this->location());
8264 // Handle an argument which is a call to a function which returns
8265 // multiple results.
8266 if (this->args_ != NULL
8267 && this->args_->size() == 1
8268 && this->args_->front()->call_expression() != NULL
8269 && this->fn_->type()->function_type() != NULL)
8271 Function_type* fntype = this->fn_->type()->function_type();
8272 size_t rc = this->args_->front()->call_expression()->result_count();
8273 if (rc > 1
8274 && fntype->parameters() != NULL
8275 && (fntype->parameters()->size() == rc
8276 || (fntype->is_varargs()
8277 && fntype->parameters()->size() - 1 <= rc)))
8279 Call_expression* call = this->args_->front()->call_expression();
8280 Expression_list* args = new Expression_list;
8281 for (size_t i = 0; i < rc; ++i)
8282 args->push_back(Expression::make_call_result(call, i));
8283 // We can't return a new call expression here, because this
8284 // one may be referenced by Call_result expressions. We
8285 // also can't delete the old arguments, because we may still
8286 // traverse them somewhere up the call stack. FIXME.
8287 this->args_ = args;
8291 // Handle a call to a varargs function by packaging up the extra
8292 // parameters.
8293 if (this->fn_->type()->function_type() != NULL
8294 && this->fn_->type()->function_type()->is_varargs())
8296 Function_type* fntype = this->fn_->type()->function_type();
8297 const Typed_identifier_list* parameters = fntype->parameters();
8298 gcc_assert(parameters != NULL && !parameters->empty());
8299 Type* varargs_type = parameters->back().type();
8300 return this->lower_varargs(gogo, function, varargs_type,
8301 parameters->size());
8304 return this;
8307 // Lower a call to a varargs function. FUNCTION is the function in
8308 // which the call occurs--it's not the function we are calling.
8309 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8310 // PARAM_COUNT is the number of parameters of the function we are
8311 // calling; the last of these parameters will be the varargs
8312 // parameter.
8314 Expression*
8315 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8316 Type* varargs_type, size_t param_count)
8318 if (this->varargs_are_lowered_)
8319 return this;
8321 source_location loc = this->location();
8323 gcc_assert(param_count > 0);
8324 gcc_assert(varargs_type->is_open_array_type());
8326 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8327 if (arg_count < param_count - 1)
8329 // Not enough arguments; will be caught in check_types.
8330 return this;
8333 Expression_list* old_args = this->args_;
8334 Expression_list* new_args = new Expression_list();
8335 bool push_empty_arg = false;
8336 if (old_args == NULL || old_args->empty())
8338 gcc_assert(param_count == 1);
8339 push_empty_arg = true;
8341 else
8343 Expression_list::const_iterator pa;
8344 int i = 1;
8345 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8347 if (static_cast<size_t>(i) == param_count)
8348 break;
8349 new_args->push_back(*pa);
8352 // We have reached the varargs parameter.
8354 bool issued_error = false;
8355 if (pa == old_args->end())
8356 push_empty_arg = true;
8357 else if (pa + 1 == old_args->end() && this->is_varargs_)
8358 new_args->push_back(*pa);
8359 else if (this->is_varargs_)
8361 this->report_error(_("too many arguments"));
8362 return this;
8364 else
8366 Type* element_type = varargs_type->array_type()->element_type();
8367 Expression_list* vals = new Expression_list;
8368 for (; pa != old_args->end(); ++pa, ++i)
8370 // Check types here so that we get a better message.
8371 Type* patype = (*pa)->type();
8372 source_location paloc = (*pa)->location();
8373 if (!this->check_argument_type(i, element_type, patype,
8374 paloc, issued_error))
8375 continue;
8376 vals->push_back(*pa);
8378 Expression* val =
8379 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8380 new_args->push_back(val);
8384 if (push_empty_arg)
8385 new_args->push_back(Expression::make_nil(loc));
8387 // We can't return a new call expression here, because this one may
8388 // be referenced by Call_result expressions. FIXME.
8389 if (old_args != NULL)
8390 delete old_args;
8391 this->args_ = new_args;
8392 this->varargs_are_lowered_ = true;
8394 // Lower all the new subexpressions.
8395 Expression* ret = this;
8396 gogo->lower_expression(function, &ret);
8397 gcc_assert(ret == this);
8398 return ret;
8401 // Get the function type. Returns NULL if we don't know the type. If
8402 // this returns NULL, and if_ERROR is true, issues an error.
8404 Function_type*
8405 Call_expression::get_function_type() const
8407 return this->fn_->type()->function_type();
8410 // Return the number of values which this call will return.
8412 size_t
8413 Call_expression::result_count() const
8415 const Function_type* fntype = this->get_function_type();
8416 if (fntype == NULL)
8417 return 0;
8418 if (fntype->results() == NULL)
8419 return 0;
8420 return fntype->results()->size();
8423 // Return whether this is a call to the predeclared function recover.
8425 bool
8426 Call_expression::is_recover_call() const
8428 return this->do_is_recover_call();
8431 // Set the argument to the recover function.
8433 void
8434 Call_expression::set_recover_arg(Expression* arg)
8436 this->do_set_recover_arg(arg);
8439 // Virtual functions also implemented by Builtin_call_expression.
8441 bool
8442 Call_expression::do_is_recover_call() const
8444 return false;
8447 void
8448 Call_expression::do_set_recover_arg(Expression*)
8450 gcc_unreachable();
8453 // Get the type.
8455 Type*
8456 Call_expression::do_type()
8458 if (this->type_ != NULL)
8459 return this->type_;
8461 Type* ret;
8462 Function_type* fntype = this->get_function_type();
8463 if (fntype == NULL)
8464 return Type::make_error_type();
8466 const Typed_identifier_list* results = fntype->results();
8467 if (results == NULL)
8468 ret = Type::make_void_type();
8469 else if (results->size() == 1)
8470 ret = results->begin()->type();
8471 else
8472 ret = Type::make_call_multiple_result_type(this);
8474 this->type_ = ret;
8476 return this->type_;
8479 // Determine types for a call expression. We can use the function
8480 // parameter types to set the types of the arguments.
8482 void
8483 Call_expression::do_determine_type(const Type_context*)
8485 this->fn_->determine_type_no_context();
8486 Function_type* fntype = this->get_function_type();
8487 const Typed_identifier_list* parameters = NULL;
8488 if (fntype != NULL)
8489 parameters = fntype->parameters();
8490 if (this->args_ != NULL)
8492 Typed_identifier_list::const_iterator pt;
8493 if (parameters != NULL)
8494 pt = parameters->begin();
8495 for (Expression_list::const_iterator pa = this->args_->begin();
8496 pa != this->args_->end();
8497 ++pa)
8499 if (parameters != NULL && pt != parameters->end())
8501 Type_context subcontext(pt->type(), false);
8502 (*pa)->determine_type(&subcontext);
8503 ++pt;
8505 else
8506 (*pa)->determine_type_no_context();
8511 // Check types for parameter I.
8513 bool
8514 Call_expression::check_argument_type(int i, const Type* parameter_type,
8515 const Type* argument_type,
8516 source_location argument_location,
8517 bool issued_error)
8519 std::string reason;
8520 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8522 if (!issued_error)
8524 if (reason.empty())
8525 error_at(argument_location, "argument %d has incompatible type", i);
8526 else
8527 error_at(argument_location,
8528 "argument %d has incompatible type (%s)",
8529 i, reason.c_str());
8531 this->set_is_error();
8532 return false;
8534 return true;
8537 // Check types.
8539 void
8540 Call_expression::do_check_types(Gogo*)
8542 Function_type* fntype = this->get_function_type();
8543 if (fntype == NULL)
8545 if (!this->fn_->type()->is_error_type())
8546 this->report_error(_("expected function"));
8547 return;
8550 if (fntype->is_method())
8552 // We don't support pointers to methods, so the function has to
8553 // be a bound method expression.
8554 Bound_method_expression* bme = this->fn_->bound_method_expression();
8555 if (bme == NULL)
8557 this->report_error(_("method call without object"));
8558 return;
8560 Type* first_arg_type = bme->first_argument()->type();
8561 if (first_arg_type->points_to() == NULL)
8563 // When passing a value, we need to check that we are
8564 // permitted to copy it.
8565 std::string reason;
8566 if (!Type::are_assignable(fntype->receiver()->type(),
8567 first_arg_type, &reason))
8569 if (reason.empty())
8570 this->report_error(_("incompatible type for receiver"));
8571 else
8573 error_at(this->location(),
8574 "incompatible type for receiver (%s)",
8575 reason.c_str());
8576 this->set_is_error();
8582 // Note that varargs was handled by the lower_varargs() method, so
8583 // we don't have to worry about it here.
8585 const Typed_identifier_list* parameters = fntype->parameters();
8586 if (this->args_ == NULL)
8588 if (parameters != NULL && !parameters->empty())
8589 this->report_error(_("not enough arguments"));
8591 else if (parameters == NULL)
8592 this->report_error(_("too many arguments"));
8593 else
8595 int i = 0;
8596 Typed_identifier_list::const_iterator pt = parameters->begin();
8597 for (Expression_list::const_iterator pa = this->args_->begin();
8598 pa != this->args_->end();
8599 ++pa, ++pt, ++i)
8601 if (pt == parameters->end())
8603 this->report_error(_("too many arguments"));
8604 return;
8606 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8607 (*pa)->location(), false);
8609 if (pt != parameters->end())
8610 this->report_error(_("not enough arguments"));
8614 // Return whether we have to use a temporary variable to ensure that
8615 // we evaluate this call expression in order. If the call returns no
8616 // results then it will inevitably be executed last. If the call
8617 // returns more than one result then it will be used with Call_result
8618 // expressions. So we only have to use a temporary variable if the
8619 // call returns exactly one result.
8621 bool
8622 Call_expression::do_must_eval_in_order() const
8624 return this->result_count() == 1;
8627 // Get the function and the first argument to use when calling a bound
8628 // method.
8630 tree
8631 Call_expression::bound_method_function(Translate_context* context,
8632 Bound_method_expression* bound_method,
8633 tree* first_arg_ptr)
8635 Expression* first_argument = bound_method->first_argument();
8636 tree first_arg = first_argument->get_tree(context);
8637 if (first_arg == error_mark_node)
8638 return error_mark_node;
8640 // We always pass a pointer to the first argument when calling a
8641 // method.
8642 if (first_argument->type()->points_to() == NULL)
8644 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8645 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8646 || DECL_P(first_arg)
8647 || TREE_CODE(first_arg) == INDIRECT_REF
8648 || TREE_CODE(first_arg) == COMPONENT_REF)
8650 first_arg = build_fold_addr_expr(first_arg);
8651 if (DECL_P(first_arg))
8652 TREE_ADDRESSABLE(first_arg) = 1;
8654 else
8656 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8657 get_name(first_arg));
8658 DECL_IGNORED_P(tmp) = 0;
8659 DECL_INITIAL(tmp) = first_arg;
8660 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8661 build1(DECL_EXPR, void_type_node, tmp),
8662 build_fold_addr_expr(tmp));
8663 TREE_ADDRESSABLE(tmp) = 1;
8665 if (first_arg == error_mark_node)
8666 return error_mark_node;
8669 Type* fatype = bound_method->first_argument_type();
8670 if (fatype != NULL)
8672 if (fatype->points_to() == NULL)
8673 fatype = Type::make_pointer_type(fatype);
8674 first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg);
8675 if (first_arg == error_mark_node
8676 || TREE_TYPE(first_arg) == error_mark_node)
8677 return error_mark_node;
8680 *first_arg_ptr = first_arg;
8682 return bound_method->method()->get_tree(context);
8685 // Get the function and the first argument to use when calling an
8686 // interface method.
8688 tree
8689 Call_expression::interface_method_function(
8690 Translate_context* context,
8691 Interface_field_reference_expression* interface_method,
8692 tree* first_arg_ptr)
8694 tree expr = interface_method->expr()->get_tree(context);
8695 if (expr == error_mark_node)
8696 return error_mark_node;
8697 expr = save_expr(expr);
8698 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8699 if (first_arg == error_mark_node)
8700 return error_mark_node;
8701 *first_arg_ptr = first_arg;
8702 return interface_method->get_function_tree(context, expr);
8705 // Build the call expression.
8707 tree
8708 Call_expression::do_get_tree(Translate_context* context)
8710 if (this->tree_ != NULL_TREE)
8711 return this->tree_;
8713 Function_type* fntype = this->get_function_type();
8714 if (fntype == NULL)
8715 return error_mark_node;
8717 if (this->fn_->is_error_expression())
8718 return error_mark_node;
8720 Gogo* gogo = context->gogo();
8721 source_location location = this->location();
8723 Func_expression* func = this->fn_->func_expression();
8724 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8725 Interface_field_reference_expression* interface_method =
8726 this->fn_->interface_field_reference_expression();
8727 const bool has_closure = func != NULL && func->closure() != NULL;
8728 const bool is_method = bound_method != NULL || interface_method != NULL;
8729 gcc_assert(!fntype->is_method() || is_method);
8731 int nargs;
8732 tree* args;
8733 if (this->args_ == NULL || this->args_->empty())
8735 nargs = is_method ? 1 : 0;
8736 args = nargs == 0 ? NULL : new tree[nargs];
8738 else
8740 const Typed_identifier_list* params = fntype->parameters();
8741 gcc_assert(params != NULL);
8743 nargs = this->args_->size();
8744 int i = is_method ? 1 : 0;
8745 nargs += i;
8746 args = new tree[nargs];
8748 Typed_identifier_list::const_iterator pp = params->begin();
8749 Expression_list::const_iterator pe;
8750 for (pe = this->args_->begin();
8751 pe != this->args_->end();
8752 ++pe, ++pp, ++i)
8754 gcc_assert(pp != params->end());
8755 tree arg_val = (*pe)->get_tree(context);
8756 args[i] = Expression::convert_for_assignment(context,
8757 pp->type(),
8758 (*pe)->type(),
8759 arg_val,
8760 location);
8761 if (args[i] == error_mark_node)
8763 delete[] args;
8764 return error_mark_node;
8767 gcc_assert(pp == params->end());
8768 gcc_assert(i == nargs);
8771 tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo)));
8772 if (rettype == error_mark_node)
8774 delete[] args;
8775 return error_mark_node;
8778 tree fn;
8779 if (has_closure)
8780 fn = func->get_tree_without_closure(gogo);
8781 else if (!is_method)
8782 fn = this->fn_->get_tree(context);
8783 else if (bound_method != NULL)
8784 fn = this->bound_method_function(context, bound_method, &args[0]);
8785 else if (interface_method != NULL)
8786 fn = this->interface_method_function(context, interface_method, &args[0]);
8787 else
8788 gcc_unreachable();
8790 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8792 delete[] args;
8793 return error_mark_node;
8796 // This is to support builtin math functions when using 80387 math.
8797 tree fndecl = fn;
8798 if (TREE_CODE(fndecl) == ADDR_EXPR)
8799 fndecl = TREE_OPERAND(fndecl, 0);
8800 tree excess_type = NULL_TREE;
8801 if (DECL_P(fndecl)
8802 && DECL_IS_BUILTIN(fndecl)
8803 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8804 && nargs > 0
8805 && ((SCALAR_FLOAT_TYPE_P(rettype)
8806 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8807 || (COMPLEX_FLOAT_TYPE_P(rettype)
8808 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8810 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8811 if (excess_type != NULL_TREE)
8813 tree excess_fndecl = mathfn_built_in(excess_type,
8814 DECL_FUNCTION_CODE(fndecl));
8815 if (excess_fndecl == NULL_TREE)
8816 excess_type = NULL_TREE;
8817 else
8819 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8820 for (int i = 0; i < nargs; ++i)
8821 args[i] = ::convert(excess_type, args[i]);
8826 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
8827 fn, nargs, args);
8828 delete[] args;
8830 SET_EXPR_LOCATION(ret, location);
8832 if (has_closure)
8834 tree closure_tree = func->closure()->get_tree(context);
8835 if (closure_tree != error_mark_node)
8836 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
8839 // If this is a recursive function type which returns itself, as in
8840 // type F func() F
8841 // we have used ptr_type_node for the return type. Add a cast here
8842 // to the correct type.
8843 if (TREE_TYPE(ret) == ptr_type_node)
8845 tree t = this->type()->get_tree(gogo);
8846 ret = fold_convert_loc(location, t, ret);
8849 if (excess_type != NULL_TREE)
8851 // Calling convert here can undo our excess precision change.
8852 // That may or may not be a bug in convert_to_real.
8853 ret = build1(NOP_EXPR, rettype, ret);
8856 // If there is more than one result, we will refer to the call
8857 // multiple times.
8858 if (fntype->results() != NULL && fntype->results()->size() > 1)
8859 ret = save_expr(ret);
8861 this->tree_ = ret;
8863 return ret;
8866 // Make a call expression.
8868 Call_expression*
8869 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
8870 source_location location)
8872 return new Call_expression(fn, args, is_varargs, location);
8875 // A single result from a call which returns multiple results.
8877 class Call_result_expression : public Expression
8879 public:
8880 Call_result_expression(Call_expression* call, unsigned int index)
8881 : Expression(EXPRESSION_CALL_RESULT, call->location()),
8882 call_(call), index_(index)
8885 protected:
8887 do_traverse(Traverse*);
8889 Type*
8890 do_type();
8892 void
8893 do_determine_type(const Type_context*);
8895 void
8896 do_check_types(Gogo*);
8898 Expression*
8899 do_copy()
8901 return new Call_result_expression(this->call_->call_expression(),
8902 this->index_);
8905 bool
8906 do_must_eval_in_order() const
8907 { return true; }
8909 tree
8910 do_get_tree(Translate_context*);
8912 private:
8913 // The underlying call expression.
8914 Expression* call_;
8915 // Which result we want.
8916 unsigned int index_;
8919 // Traverse a call result.
8922 Call_result_expression::do_traverse(Traverse* traverse)
8924 if (traverse->remember_expression(this->call_))
8926 // We have already traversed the call expression.
8927 return TRAVERSE_CONTINUE;
8929 return Expression::traverse(&this->call_, traverse);
8932 // Get the type.
8934 Type*
8935 Call_result_expression::do_type()
8937 if (this->classification() == EXPRESSION_ERROR)
8938 return Type::make_error_type();
8940 // THIS->CALL_ can be replaced with a temporary reference due to
8941 // Call_expression::do_must_eval_in_order when there is an error.
8942 Call_expression* ce = this->call_->call_expression();
8943 if (ce == NULL)
8945 this->set_is_error();
8946 return Type::make_error_type();
8948 Function_type* fntype = ce->get_function_type();
8949 if (fntype == NULL)
8951 this->set_is_error();
8952 return Type::make_error_type();
8954 const Typed_identifier_list* results = fntype->results();
8955 if (results == NULL)
8957 this->report_error(_("number of results does not match "
8958 "number of values"));
8959 return Type::make_error_type();
8961 Typed_identifier_list::const_iterator pr = results->begin();
8962 for (unsigned int i = 0; i < this->index_; ++i)
8964 if (pr == results->end())
8965 break;
8966 ++pr;
8968 if (pr == results->end())
8970 this->report_error(_("number of results does not match "
8971 "number of values"));
8972 return Type::make_error_type();
8974 return pr->type();
8977 // Check the type. Just make sure that we trigger the warning in
8978 // do_type.
8980 void
8981 Call_result_expression::do_check_types(Gogo*)
8983 this->type();
8986 // Determine the type. We have nothing to do here, but the 0 result
8987 // needs to pass down to the caller.
8989 void
8990 Call_result_expression::do_determine_type(const Type_context*)
8992 if (this->index_ == 0)
8993 this->call_->determine_type_no_context();
8996 // Return the tree.
8998 tree
8999 Call_result_expression::do_get_tree(Translate_context* context)
9001 tree call_tree = this->call_->get_tree(context);
9002 if (call_tree == error_mark_node)
9003 return error_mark_node;
9004 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9006 gcc_assert(saw_errors());
9007 return error_mark_node;
9009 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9010 for (unsigned int i = 0; i < this->index_; ++i)
9012 gcc_assert(field != NULL_TREE);
9013 field = DECL_CHAIN(field);
9015 gcc_assert(field != NULL_TREE);
9016 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
9019 // Make a reference to a single result of a call which returns
9020 // multiple results.
9022 Expression*
9023 Expression::make_call_result(Call_expression* call, unsigned int index)
9025 return new Call_result_expression(call, index);
9028 // Class Index_expression.
9030 // Traversal.
9033 Index_expression::do_traverse(Traverse* traverse)
9035 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9036 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9037 || (this->end_ != NULL
9038 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9039 return TRAVERSE_EXIT;
9040 return TRAVERSE_CONTINUE;
9043 // Lower an index expression. This converts the generic index
9044 // expression into an array index, a string index, or a map index.
9046 Expression*
9047 Index_expression::do_lower(Gogo*, Named_object*, int)
9049 source_location location = this->location();
9050 Expression* left = this->left_;
9051 Expression* start = this->start_;
9052 Expression* end = this->end_;
9054 Type* type = left->type();
9055 if (type->is_error_type())
9056 return Expression::make_error(location);
9057 else if (left->is_type_expression())
9059 error_at(location, "attempt to index type expression");
9060 return Expression::make_error(location);
9062 else if (type->array_type() != NULL)
9063 return Expression::make_array_index(left, start, end, location);
9064 else if (type->points_to() != NULL
9065 && type->points_to()->array_type() != NULL
9066 && !type->points_to()->is_open_array_type())
9068 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9069 location);
9070 return Expression::make_array_index(deref, start, end, location);
9072 else if (type->is_string_type())
9073 return Expression::make_string_index(left, start, end, location);
9074 else if (type->map_type() != NULL)
9076 if (end != NULL)
9078 error_at(location, "invalid slice of map");
9079 return Expression::make_error(location);
9081 Map_index_expression* ret= Expression::make_map_index(left, start,
9082 location);
9083 if (this->is_lvalue_)
9084 ret->set_is_lvalue();
9085 return ret;
9087 else
9089 error_at(location,
9090 "attempt to index object which is not array, string, or map");
9091 return Expression::make_error(location);
9095 // Make an index expression.
9097 Expression*
9098 Expression::make_index(Expression* left, Expression* start, Expression* end,
9099 source_location location)
9101 return new Index_expression(left, start, end, location);
9104 // An array index. This is used for both indexing and slicing.
9106 class Array_index_expression : public Expression
9108 public:
9109 Array_index_expression(Expression* array, Expression* start,
9110 Expression* end, source_location location)
9111 : Expression(EXPRESSION_ARRAY_INDEX, location),
9112 array_(array), start_(start), end_(end), type_(NULL)
9115 protected:
9117 do_traverse(Traverse*);
9119 Type*
9120 do_type();
9122 void
9123 do_determine_type(const Type_context*);
9125 void
9126 do_check_types(Gogo*);
9128 Expression*
9129 do_copy()
9131 return Expression::make_array_index(this->array_->copy(),
9132 this->start_->copy(),
9133 (this->end_ == NULL
9134 ? NULL
9135 : this->end_->copy()),
9136 this->location());
9139 bool
9140 do_is_addressable() const;
9142 void
9143 do_address_taken(bool escapes)
9144 { this->array_->address_taken(escapes); }
9146 tree
9147 do_get_tree(Translate_context*);
9149 private:
9150 // The array we are getting a value from.
9151 Expression* array_;
9152 // The start or only index.
9153 Expression* start_;
9154 // The end index of a slice. This may be NULL for a simple array
9155 // index, or it may be a nil expression for the length of the array.
9156 Expression* end_;
9157 // The type of the expression.
9158 Type* type_;
9161 // Array index traversal.
9164 Array_index_expression::do_traverse(Traverse* traverse)
9166 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9167 return TRAVERSE_EXIT;
9168 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9169 return TRAVERSE_EXIT;
9170 if (this->end_ != NULL)
9172 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9173 return TRAVERSE_EXIT;
9175 return TRAVERSE_CONTINUE;
9178 // Return the type of an array index.
9180 Type*
9181 Array_index_expression::do_type()
9183 if (this->type_ == NULL)
9185 Array_type* type = this->array_->type()->array_type();
9186 if (type == NULL)
9187 this->type_ = Type::make_error_type();
9188 else if (this->end_ == NULL)
9189 this->type_ = type->element_type();
9190 else if (type->is_open_array_type())
9192 // A slice of a slice has the same type as the original
9193 // slice.
9194 this->type_ = this->array_->type()->deref();
9196 else
9198 // A slice of an array is a slice.
9199 this->type_ = Type::make_array_type(type->element_type(), NULL);
9202 return this->type_;
9205 // Set the type of an array index.
9207 void
9208 Array_index_expression::do_determine_type(const Type_context*)
9210 this->array_->determine_type_no_context();
9211 this->start_->determine_type_no_context();
9212 if (this->end_ != NULL)
9213 this->end_->determine_type_no_context();
9216 // Check types of an array index.
9218 void
9219 Array_index_expression::do_check_types(Gogo*)
9221 if (this->start_->type()->integer_type() == NULL)
9222 this->report_error(_("index must be integer"));
9223 if (this->end_ != NULL
9224 && this->end_->type()->integer_type() == NULL
9225 && !this->end_->is_nil_expression())
9226 this->report_error(_("slice end must be integer"));
9228 Array_type* array_type = this->array_->type()->array_type();
9229 if (array_type == NULL)
9231 gcc_assert(this->array_->type()->is_error_type());
9232 return;
9235 unsigned int int_bits =
9236 Type::lookup_integer_type("int")->integer_type()->bits();
9238 Type* dummy;
9239 mpz_t lval;
9240 mpz_init(lval);
9241 bool lval_valid = (array_type->length() != NULL
9242 && array_type->length()->integer_constant_value(true,
9243 lval,
9244 &dummy));
9245 mpz_t ival;
9246 mpz_init(ival);
9247 if (this->start_->integer_constant_value(true, ival, &dummy))
9249 if (mpz_sgn(ival) < 0
9250 || mpz_sizeinbase(ival, 2) >= int_bits
9251 || (lval_valid
9252 && (this->end_ == NULL
9253 ? mpz_cmp(ival, lval) >= 0
9254 : mpz_cmp(ival, lval) > 0)))
9256 error_at(this->start_->location(), "array index out of bounds");
9257 this->set_is_error();
9260 if (this->end_ != NULL && !this->end_->is_nil_expression())
9262 if (this->end_->integer_constant_value(true, ival, &dummy))
9264 if (mpz_sgn(ival) < 0
9265 || mpz_sizeinbase(ival, 2) >= int_bits
9266 || (lval_valid && mpz_cmp(ival, lval) > 0))
9268 error_at(this->end_->location(), "array index out of bounds");
9269 this->set_is_error();
9273 mpz_clear(ival);
9274 mpz_clear(lval);
9276 // A slice of an array requires an addressable array. A slice of a
9277 // slice is always possible.
9278 if (this->end_ != NULL
9279 && !array_type->is_open_array_type()
9280 && !this->array_->is_addressable())
9281 this->report_error(_("array is not addressable"));
9284 // Return whether this expression is addressable.
9286 bool
9287 Array_index_expression::do_is_addressable() const
9289 // A slice expression is not addressable.
9290 if (this->end_ != NULL)
9291 return false;
9293 // An index into a slice is addressable.
9294 if (this->array_->type()->is_open_array_type())
9295 return true;
9297 // An index into an array is addressable if the array is
9298 // addressable.
9299 return this->array_->is_addressable();
9302 // Get a tree for an array index.
9304 tree
9305 Array_index_expression::do_get_tree(Translate_context* context)
9307 Gogo* gogo = context->gogo();
9308 source_location loc = this->location();
9310 Array_type* array_type = this->array_->type()->array_type();
9311 if (array_type == NULL)
9313 gcc_assert(this->array_->type()->is_error_type());
9314 return error_mark_node;
9317 tree type_tree = array_type->get_tree(gogo);
9318 if (type_tree == error_mark_node)
9319 return error_mark_node;
9321 tree array_tree = this->array_->get_tree(context);
9322 if (array_tree == error_mark_node)
9323 return error_mark_node;
9325 if (array_type->length() == NULL && !DECL_P(array_tree))
9326 array_tree = save_expr(array_tree);
9327 tree length_tree = array_type->length_tree(gogo, array_tree);
9328 if (length_tree == error_mark_node)
9329 return error_mark_node;
9330 length_tree = save_expr(length_tree);
9331 tree length_type = TREE_TYPE(length_tree);
9333 tree bad_index = boolean_false_node;
9335 tree start_tree = this->start_->get_tree(context);
9336 if (start_tree == error_mark_node)
9337 return error_mark_node;
9338 if (!DECL_P(start_tree))
9339 start_tree = save_expr(start_tree);
9340 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9341 start_tree = convert_to_integer(length_type, start_tree);
9343 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9344 loc);
9346 start_tree = fold_convert_loc(loc, length_type, start_tree);
9347 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9348 fold_build2_loc(loc,
9349 (this->end_ == NULL
9350 ? GE_EXPR
9351 : GT_EXPR),
9352 boolean_type_node, start_tree,
9353 length_tree));
9355 int code = (array_type->length() != NULL
9356 ? (this->end_ == NULL
9357 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9358 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9359 : (this->end_ == NULL
9360 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9361 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9362 tree crash = Gogo::runtime_error(code, loc);
9364 if (this->end_ == NULL)
9366 // Simple array indexing. This has to return an l-value, so
9367 // wrap the index check into START_TREE.
9368 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9369 build3(COND_EXPR, void_type_node,
9370 bad_index, crash, NULL_TREE),
9371 start_tree);
9372 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9374 if (array_type->length() != NULL)
9376 // Fixed array.
9377 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9378 start_tree, NULL_TREE, NULL_TREE);
9380 else
9382 // Open array.
9383 tree values = array_type->value_pointer_tree(gogo, array_tree);
9384 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9385 if (element_type_tree == error_mark_node)
9386 return error_mark_node;
9387 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9388 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9389 start_tree, element_size);
9390 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9391 TREE_TYPE(values), values, offset);
9392 return build_fold_indirect_ref(ptr);
9396 // Array slice.
9398 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9399 if (capacity_tree == error_mark_node)
9400 return error_mark_node;
9401 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9403 tree end_tree;
9404 if (this->end_->is_nil_expression())
9405 end_tree = length_tree;
9406 else
9408 end_tree = this->end_->get_tree(context);
9409 if (end_tree == error_mark_node)
9410 return error_mark_node;
9411 if (!DECL_P(end_tree))
9412 end_tree = save_expr(end_tree);
9413 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9414 end_tree = convert_to_integer(length_type, end_tree);
9416 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9417 loc);
9419 end_tree = fold_convert_loc(loc, length_type, end_tree);
9421 capacity_tree = save_expr(capacity_tree);
9422 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9423 fold_build2_loc(loc, LT_EXPR,
9424 boolean_type_node,
9425 end_tree, start_tree),
9426 fold_build2_loc(loc, GT_EXPR,
9427 boolean_type_node,
9428 end_tree, capacity_tree));
9429 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9430 bad_index, bad_end);
9433 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9434 if (element_type_tree == error_mark_node)
9435 return error_mark_node;
9436 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9438 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9439 fold_convert_loc(loc, sizetype, start_tree),
9440 element_size);
9442 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9443 if (value_pointer == error_mark_node)
9444 return error_mark_node;
9446 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9447 TREE_TYPE(value_pointer),
9448 value_pointer, offset);
9450 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9451 end_tree, start_tree);
9453 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9454 capacity_tree, start_tree);
9456 tree struct_tree = this->type()->get_tree(gogo);
9457 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9459 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9461 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9462 tree field = TYPE_FIELDS(struct_tree);
9463 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9464 elt->index = field;
9465 elt->value = value_pointer;
9467 elt = VEC_quick_push(constructor_elt, init, NULL);
9468 field = DECL_CHAIN(field);
9469 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9470 elt->index = field;
9471 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9473 elt = VEC_quick_push(constructor_elt, init, NULL);
9474 field = DECL_CHAIN(field);
9475 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9476 elt->index = field;
9477 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9479 tree constructor = build_constructor(struct_tree, init);
9481 if (TREE_CONSTANT(value_pointer)
9482 && TREE_CONSTANT(result_length_tree)
9483 && TREE_CONSTANT(result_capacity_tree))
9484 TREE_CONSTANT(constructor) = 1;
9486 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9487 build3(COND_EXPR, void_type_node,
9488 bad_index, crash, NULL_TREE),
9489 constructor);
9492 // Make an array index expression. END may be NULL.
9494 Expression*
9495 Expression::make_array_index(Expression* array, Expression* start,
9496 Expression* end, source_location location)
9498 // Taking a slice of a composite literal requires moving the literal
9499 // onto the heap.
9500 if (end != NULL && array->is_composite_literal())
9502 array = Expression::make_heap_composite(array, location);
9503 array = Expression::make_unary(OPERATOR_MULT, array, location);
9505 return new Array_index_expression(array, start, end, location);
9508 // A string index. This is used for both indexing and slicing.
9510 class String_index_expression : public Expression
9512 public:
9513 String_index_expression(Expression* string, Expression* start,
9514 Expression* end, source_location location)
9515 : Expression(EXPRESSION_STRING_INDEX, location),
9516 string_(string), start_(start), end_(end)
9519 protected:
9521 do_traverse(Traverse*);
9523 Type*
9524 do_type();
9526 void
9527 do_determine_type(const Type_context*);
9529 void
9530 do_check_types(Gogo*);
9532 Expression*
9533 do_copy()
9535 return Expression::make_string_index(this->string_->copy(),
9536 this->start_->copy(),
9537 (this->end_ == NULL
9538 ? NULL
9539 : this->end_->copy()),
9540 this->location());
9543 tree
9544 do_get_tree(Translate_context*);
9546 private:
9547 // The string we are getting a value from.
9548 Expression* string_;
9549 // The start or only index.
9550 Expression* start_;
9551 // The end index of a slice. This may be NULL for a single index,
9552 // or it may be a nil expression for the length of the string.
9553 Expression* end_;
9556 // String index traversal.
9559 String_index_expression::do_traverse(Traverse* traverse)
9561 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9562 return TRAVERSE_EXIT;
9563 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9564 return TRAVERSE_EXIT;
9565 if (this->end_ != NULL)
9567 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9568 return TRAVERSE_EXIT;
9570 return TRAVERSE_CONTINUE;
9573 // Return the type of a string index.
9575 Type*
9576 String_index_expression::do_type()
9578 if (this->end_ == NULL)
9579 return Type::lookup_integer_type("uint8");
9580 else
9581 return this->string_->type();
9584 // Determine the type of a string index.
9586 void
9587 String_index_expression::do_determine_type(const Type_context*)
9589 this->string_->determine_type_no_context();
9590 this->start_->determine_type_no_context();
9591 if (this->end_ != NULL)
9592 this->end_->determine_type_no_context();
9595 // Check types of a string index.
9597 void
9598 String_index_expression::do_check_types(Gogo*)
9600 if (this->start_->type()->integer_type() == NULL)
9601 this->report_error(_("index must be integer"));
9602 if (this->end_ != NULL
9603 && this->end_->type()->integer_type() == NULL
9604 && !this->end_->is_nil_expression())
9605 this->report_error(_("slice end must be integer"));
9607 std::string sval;
9608 bool sval_valid = this->string_->string_constant_value(&sval);
9610 mpz_t ival;
9611 mpz_init(ival);
9612 Type* dummy;
9613 if (this->start_->integer_constant_value(true, ival, &dummy))
9615 if (mpz_sgn(ival) < 0
9616 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9618 error_at(this->start_->location(), "string index out of bounds");
9619 this->set_is_error();
9622 if (this->end_ != NULL && !this->end_->is_nil_expression())
9624 if (this->end_->integer_constant_value(true, ival, &dummy))
9626 if (mpz_sgn(ival) < 0
9627 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9629 error_at(this->end_->location(), "string index out of bounds");
9630 this->set_is_error();
9634 mpz_clear(ival);
9637 // Get a tree for a string index.
9639 tree
9640 String_index_expression::do_get_tree(Translate_context* context)
9642 source_location loc = this->location();
9644 tree string_tree = this->string_->get_tree(context);
9645 if (string_tree == error_mark_node)
9646 return error_mark_node;
9648 if (this->string_->type()->points_to() != NULL)
9649 string_tree = build_fold_indirect_ref(string_tree);
9650 if (!DECL_P(string_tree))
9651 string_tree = save_expr(string_tree);
9652 tree string_type = TREE_TYPE(string_tree);
9654 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9655 length_tree = save_expr(length_tree);
9656 tree length_type = TREE_TYPE(length_tree);
9658 tree bad_index = boolean_false_node;
9660 tree start_tree = this->start_->get_tree(context);
9661 if (start_tree == error_mark_node)
9662 return error_mark_node;
9663 if (!DECL_P(start_tree))
9664 start_tree = save_expr(start_tree);
9665 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9666 start_tree = convert_to_integer(length_type, start_tree);
9668 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9669 loc);
9671 start_tree = fold_convert_loc(loc, length_type, start_tree);
9673 int code = (this->end_ == NULL
9674 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9675 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9676 tree crash = Gogo::runtime_error(code, loc);
9678 if (this->end_ == NULL)
9680 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9681 bad_index,
9682 fold_build2_loc(loc, GE_EXPR,
9683 boolean_type_node,
9684 start_tree, length_tree));
9686 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9687 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9688 bytes_tree,
9689 fold_convert_loc(loc, sizetype, start_tree));
9690 tree index = build_fold_indirect_ref_loc(loc, ptr);
9692 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9693 build3(COND_EXPR, void_type_node,
9694 bad_index, crash, NULL_TREE),
9695 index);
9697 else
9699 tree end_tree;
9700 if (this->end_->is_nil_expression())
9701 end_tree = build_int_cst(length_type, -1);
9702 else
9704 end_tree = this->end_->get_tree(context);
9705 if (end_tree == error_mark_node)
9706 return error_mark_node;
9707 if (!DECL_P(end_tree))
9708 end_tree = save_expr(end_tree);
9709 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9710 end_tree = convert_to_integer(length_type, end_tree);
9712 bad_index = Expression::check_bounds(end_tree, length_type,
9713 bad_index, loc);
9715 end_tree = fold_convert_loc(loc, length_type, end_tree);
9718 static tree strslice_fndecl;
9719 tree ret = Gogo::call_builtin(&strslice_fndecl,
9720 loc,
9721 "__go_string_slice",
9723 string_type,
9724 string_type,
9725 string_tree,
9726 length_type,
9727 start_tree,
9728 length_type,
9729 end_tree);
9730 if (ret == error_mark_node)
9731 return error_mark_node;
9732 // This will panic if the bounds are out of range for the
9733 // string.
9734 TREE_NOTHROW(strslice_fndecl) = 0;
9736 if (bad_index == boolean_false_node)
9737 return ret;
9738 else
9739 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9740 build3(COND_EXPR, void_type_node,
9741 bad_index, crash, NULL_TREE),
9742 ret);
9746 // Make a string index expression. END may be NULL.
9748 Expression*
9749 Expression::make_string_index(Expression* string, Expression* start,
9750 Expression* end, source_location location)
9752 return new String_index_expression(string, start, end, location);
9755 // Class Map_index.
9757 // Get the type of the map.
9759 Map_type*
9760 Map_index_expression::get_map_type() const
9762 Map_type* mt = this->map_->type()->deref()->map_type();
9763 if (mt == NULL)
9764 gcc_assert(saw_errors());
9765 return mt;
9768 // Map index traversal.
9771 Map_index_expression::do_traverse(Traverse* traverse)
9773 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9774 return TRAVERSE_EXIT;
9775 return Expression::traverse(&this->index_, traverse);
9778 // Return the type of a map index.
9780 Type*
9781 Map_index_expression::do_type()
9783 Map_type* mt = this->get_map_type();
9784 if (mt == NULL)
9785 return Type::make_error_type();
9786 Type* type = mt->val_type();
9787 // If this map index is in a tuple assignment, we actually return a
9788 // pointer to the value type. Tuple_map_assignment_statement is
9789 // responsible for handling this correctly. We need to get the type
9790 // right in case this gets assigned to a temporary variable.
9791 if (this->is_in_tuple_assignment_)
9792 type = Type::make_pointer_type(type);
9793 return type;
9796 // Fix the type of a map index.
9798 void
9799 Map_index_expression::do_determine_type(const Type_context*)
9801 this->map_->determine_type_no_context();
9802 Map_type* mt = this->get_map_type();
9803 Type* key_type = mt == NULL ? NULL : mt->key_type();
9804 Type_context subcontext(key_type, false);
9805 this->index_->determine_type(&subcontext);
9808 // Check types of a map index.
9810 void
9811 Map_index_expression::do_check_types(Gogo*)
9813 std::string reason;
9814 Map_type* mt = this->get_map_type();
9815 if (mt == NULL)
9816 return;
9817 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
9819 if (reason.empty())
9820 this->report_error(_("incompatible type for map index"));
9821 else
9823 error_at(this->location(), "incompatible type for map index (%s)",
9824 reason.c_str());
9825 this->set_is_error();
9830 // Get a tree for a map index.
9832 tree
9833 Map_index_expression::do_get_tree(Translate_context* context)
9835 Map_type* type = this->get_map_type();
9836 if (type == NULL)
9837 return error_mark_node;
9839 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
9840 if (valptr == error_mark_node)
9841 return error_mark_node;
9842 valptr = save_expr(valptr);
9844 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
9846 if (this->is_lvalue_)
9847 return build_fold_indirect_ref(valptr);
9848 else if (this->is_in_tuple_assignment_)
9850 // Tuple_map_assignment_statement is responsible for using this
9851 // appropriately.
9852 return valptr;
9854 else
9856 return fold_build3(COND_EXPR, val_type_tree,
9857 fold_build2(EQ_EXPR, boolean_type_node, valptr,
9858 fold_convert(TREE_TYPE(valptr),
9859 null_pointer_node)),
9860 type->val_type()->get_init_tree(context->gogo(),
9861 false),
9862 build_fold_indirect_ref(valptr));
9866 // Get a tree for the map index. This returns a tree which evaluates
9867 // to a pointer to a value. The pointer will be NULL if the key is
9868 // not in the map.
9870 tree
9871 Map_index_expression::get_value_pointer(Translate_context* context,
9872 bool insert)
9874 Map_type* type = this->get_map_type();
9875 if (type == NULL)
9876 return error_mark_node;
9878 tree map_tree = this->map_->get_tree(context);
9879 tree index_tree = this->index_->get_tree(context);
9880 index_tree = Expression::convert_for_assignment(context, type->key_type(),
9881 this->index_->type(),
9882 index_tree,
9883 this->location());
9884 if (map_tree == error_mark_node || index_tree == error_mark_node)
9885 return error_mark_node;
9887 if (this->map_->type()->points_to() != NULL)
9888 map_tree = build_fold_indirect_ref(map_tree);
9890 // We need to pass in a pointer to the key, so stuff it into a
9891 // variable.
9892 tree tmp;
9893 tree make_tmp;
9894 if (current_function_decl != NULL)
9896 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
9897 DECL_IGNORED_P(tmp) = 0;
9898 DECL_INITIAL(tmp) = index_tree;
9899 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
9900 TREE_ADDRESSABLE(tmp) = 1;
9902 else
9904 tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"),
9905 TREE_TYPE(index_tree));
9906 DECL_EXTERNAL(tmp) = 0;
9907 TREE_PUBLIC(tmp) = 0;
9908 TREE_STATIC(tmp) = 1;
9909 DECL_ARTIFICIAL(tmp) = 1;
9910 if (!TREE_CONSTANT(index_tree))
9911 make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node,
9912 tmp, index_tree);
9913 else
9915 TREE_READONLY(tmp) = 1;
9916 TREE_CONSTANT(tmp) = 1;
9917 DECL_INITIAL(tmp) = index_tree;
9918 make_tmp = NULL_TREE;
9920 rest_of_decl_compilation(tmp, 1, 0);
9922 tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node,
9923 build_fold_addr_expr_loc(this->location(),
9924 tmp));
9926 static tree map_index_fndecl;
9927 tree call = Gogo::call_builtin(&map_index_fndecl,
9928 this->location(),
9929 "__go_map_index",
9931 const_ptr_type_node,
9932 TREE_TYPE(map_tree),
9933 map_tree,
9934 const_ptr_type_node,
9935 tmpref,
9936 boolean_type_node,
9937 (insert
9938 ? boolean_true_node
9939 : boolean_false_node));
9940 if (call == error_mark_node)
9941 return error_mark_node;
9942 // This can panic on a map of interface type if the interface holds
9943 // an uncomparable or unhashable type.
9944 TREE_NOTHROW(map_index_fndecl) = 0;
9946 tree val_type_tree = type->val_type()->get_tree(context->gogo());
9947 if (val_type_tree == error_mark_node)
9948 return error_mark_node;
9949 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
9951 tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call);
9952 if (make_tmp != NULL_TREE)
9953 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
9954 return ret;
9957 // Make a map index expression.
9959 Map_index_expression*
9960 Expression::make_map_index(Expression* map, Expression* index,
9961 source_location location)
9963 return new Map_index_expression(map, index, location);
9966 // Class Field_reference_expression.
9968 // Return the type of a field reference.
9970 Type*
9971 Field_reference_expression::do_type()
9973 Type* type = this->expr_->type();
9974 if (type->is_error_type())
9975 return type;
9976 Struct_type* struct_type = type->struct_type();
9977 gcc_assert(struct_type != NULL);
9978 return struct_type->field(this->field_index_)->type();
9981 // Check the types for a field reference.
9983 void
9984 Field_reference_expression::do_check_types(Gogo*)
9986 Type* type = this->expr_->type();
9987 if (type->is_error_type())
9988 return;
9989 Struct_type* struct_type = type->struct_type();
9990 gcc_assert(struct_type != NULL);
9991 gcc_assert(struct_type->field(this->field_index_) != NULL);
9994 // Get a tree for a field reference.
9996 tree
9997 Field_reference_expression::do_get_tree(Translate_context* context)
9999 tree struct_tree = this->expr_->get_tree(context);
10000 if (struct_tree == error_mark_node
10001 || TREE_TYPE(struct_tree) == error_mark_node)
10002 return error_mark_node;
10003 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
10004 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
10005 if (field == NULL_TREE)
10007 // This can happen for a type which refers to itself indirectly
10008 // and then turns out to be erroneous.
10009 gcc_assert(saw_errors());
10010 return error_mark_node;
10012 for (unsigned int i = this->field_index_; i > 0; --i)
10014 field = DECL_CHAIN(field);
10015 gcc_assert(field != NULL_TREE);
10017 if (TREE_TYPE(field) == error_mark_node)
10018 return error_mark_node;
10019 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
10020 NULL_TREE);
10023 // Make a reference to a qualified identifier in an expression.
10025 Field_reference_expression*
10026 Expression::make_field_reference(Expression* expr, unsigned int field_index,
10027 source_location location)
10029 return new Field_reference_expression(expr, field_index, location);
10032 // Class Interface_field_reference_expression.
10034 // Return a tree for the pointer to the function to call.
10036 tree
10037 Interface_field_reference_expression::get_function_tree(Translate_context*,
10038 tree expr)
10040 if (this->expr_->type()->points_to() != NULL)
10041 expr = build_fold_indirect_ref(expr);
10043 tree expr_type = TREE_TYPE(expr);
10044 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10046 tree field = TYPE_FIELDS(expr_type);
10047 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
10049 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10050 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table)));
10052 table = build_fold_indirect_ref(table);
10053 gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
10055 std::string name = Gogo::unpack_hidden_name(this->name_);
10056 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
10057 field != NULL_TREE;
10058 field = DECL_CHAIN(field))
10060 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
10061 break;
10063 gcc_assert(field != NULL_TREE);
10065 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
10068 // Return a tree for the first argument to pass to the interface
10069 // function.
10071 tree
10072 Interface_field_reference_expression::get_underlying_object_tree(
10073 Translate_context*,
10074 tree expr)
10076 if (this->expr_->type()->points_to() != NULL)
10077 expr = build_fold_indirect_ref(expr);
10079 tree expr_type = TREE_TYPE(expr);
10080 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10082 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
10083 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
10085 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10088 // Traversal.
10091 Interface_field_reference_expression::do_traverse(Traverse* traverse)
10093 return Expression::traverse(&this->expr_, traverse);
10096 // Return the type of an interface field reference.
10098 Type*
10099 Interface_field_reference_expression::do_type()
10101 Type* expr_type = this->expr_->type();
10103 Type* points_to = expr_type->points_to();
10104 if (points_to != NULL)
10105 expr_type = points_to;
10107 Interface_type* interface_type = expr_type->interface_type();
10108 if (interface_type == NULL)
10109 return Type::make_error_type();
10111 const Typed_identifier* method = interface_type->find_method(this->name_);
10112 if (method == NULL)
10113 return Type::make_error_type();
10115 return method->type();
10118 // Determine types.
10120 void
10121 Interface_field_reference_expression::do_determine_type(const Type_context*)
10123 this->expr_->determine_type_no_context();
10126 // Check the types for an interface field reference.
10128 void
10129 Interface_field_reference_expression::do_check_types(Gogo*)
10131 Type* type = this->expr_->type();
10133 Type* points_to = type->points_to();
10134 if (points_to != NULL)
10135 type = points_to;
10137 Interface_type* interface_type = type->interface_type();
10138 if (interface_type == NULL)
10139 this->report_error(_("expected interface or pointer to interface"));
10140 else
10142 const Typed_identifier* method =
10143 interface_type->find_method(this->name_);
10144 if (method == NULL)
10146 error_at(this->location(), "method %qs not in interface",
10147 Gogo::message_name(this->name_).c_str());
10148 this->set_is_error();
10153 // Get a tree for a reference to a field in an interface. There is no
10154 // standard tree type representation for this: it's a function
10155 // attached to its first argument, like a Bound_method_expression.
10156 // The only places it may currently be used are in a Call_expression
10157 // or a Go_statement, which will take it apart directly. So this has
10158 // nothing to do at present.
10160 tree
10161 Interface_field_reference_expression::do_get_tree(Translate_context*)
10163 gcc_unreachable();
10166 // Make a reference to a field in an interface.
10168 Expression*
10169 Expression::make_interface_field_reference(Expression* expr,
10170 const std::string& field,
10171 source_location location)
10173 return new Interface_field_reference_expression(expr, field, location);
10176 // A general selector. This is a Parser_expression for LEFT.NAME. It
10177 // is lowered after we know the type of the left hand side.
10179 class Selector_expression : public Parser_expression
10181 public:
10182 Selector_expression(Expression* left, const std::string& name,
10183 source_location location)
10184 : Parser_expression(EXPRESSION_SELECTOR, location),
10185 left_(left), name_(name)
10188 protected:
10190 do_traverse(Traverse* traverse)
10191 { return Expression::traverse(&this->left_, traverse); }
10193 Expression*
10194 do_lower(Gogo*, Named_object*, int);
10196 Expression*
10197 do_copy()
10199 return new Selector_expression(this->left_->copy(), this->name_,
10200 this->location());
10203 private:
10204 Expression*
10205 lower_method_expression(Gogo*);
10207 // The expression on the left hand side.
10208 Expression* left_;
10209 // The name on the right hand side.
10210 std::string name_;
10213 // Lower a selector expression once we know the real type of the left
10214 // hand side.
10216 Expression*
10217 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
10219 Expression* left = this->left_;
10220 if (left->is_type_expression())
10221 return this->lower_method_expression(gogo);
10222 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
10223 this->location());
10226 // Lower a method expression T.M or (*T).M. We turn this into a
10227 // function literal.
10229 Expression*
10230 Selector_expression::lower_method_expression(Gogo* gogo)
10232 source_location location = this->location();
10233 Type* type = this->left_->type();
10234 const std::string& name(this->name_);
10236 bool is_pointer;
10237 if (type->points_to() == NULL)
10238 is_pointer = false;
10239 else
10241 is_pointer = true;
10242 type = type->points_to();
10244 Named_type* nt = type->named_type();
10245 if (nt == NULL)
10247 error_at(location,
10248 ("method expression requires named type or "
10249 "pointer to named type"));
10250 return Expression::make_error(location);
10253 bool is_ambiguous;
10254 Method* method = nt->method_function(name, &is_ambiguous);
10255 if (method == NULL)
10257 if (!is_ambiguous)
10258 error_at(location, "type %<%s%> has no method %<%s%>",
10259 nt->message_name().c_str(),
10260 Gogo::message_name(name).c_str());
10261 else
10262 error_at(location, "method %<%s%> is ambiguous in type %<%s%>",
10263 Gogo::message_name(name).c_str(),
10264 nt->message_name().c_str());
10265 return Expression::make_error(location);
10268 if (!is_pointer && !method->is_value_method())
10270 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
10271 nt->message_name().c_str(),
10272 Gogo::message_name(name).c_str());
10273 return Expression::make_error(location);
10276 // Build a new function type in which the receiver becomes the first
10277 // argument.
10278 Function_type* method_type = method->type();
10279 gcc_assert(method_type->is_method());
10281 const char* const receiver_name = "$this";
10282 Typed_identifier_list* parameters = new Typed_identifier_list();
10283 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
10284 location));
10286 const Typed_identifier_list* method_parameters = method_type->parameters();
10287 if (method_parameters != NULL)
10289 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10290 p != method_parameters->end();
10291 ++p)
10292 parameters->push_back(*p);
10295 const Typed_identifier_list* method_results = method_type->results();
10296 Typed_identifier_list* results;
10297 if (method_results == NULL)
10298 results = NULL;
10299 else
10301 results = new Typed_identifier_list();
10302 for (Typed_identifier_list::const_iterator p = method_results->begin();
10303 p != method_results->end();
10304 ++p)
10305 results->push_back(*p);
10308 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
10309 location);
10310 if (method_type->is_varargs())
10311 fntype->set_is_varargs();
10313 // We generate methods which always takes a pointer to the receiver
10314 // as their first argument. If this is for a pointer type, we can
10315 // simply reuse the existing function. We use an internal hack to
10316 // get the right type.
10318 if (is_pointer)
10320 Named_object* mno = (method->needs_stub_method()
10321 ? method->stub_object()
10322 : method->named_object());
10323 Expression* f = Expression::make_func_reference(mno, NULL, location);
10324 f = Expression::make_cast(fntype, f, location);
10325 Type_conversion_expression* tce =
10326 static_cast<Type_conversion_expression*>(f);
10327 tce->set_may_convert_function_types();
10328 return f;
10331 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
10332 location);
10334 Named_object* vno = gogo->lookup(receiver_name, NULL);
10335 gcc_assert(vno != NULL);
10336 Expression* ve = Expression::make_var_reference(vno, location);
10337 Expression* bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
10339 // Even though we found the method above, if it has an error type we
10340 // may see an error here.
10341 if (bm->is_error_expression())
10343 gogo->finish_function(location);
10344 return bm;
10347 Expression_list* args;
10348 if (method_parameters == NULL)
10349 args = NULL;
10350 else
10352 args = new Expression_list();
10353 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10354 p != method_parameters->end();
10355 ++p)
10357 vno = gogo->lookup(p->name(), NULL);
10358 gcc_assert(vno != NULL);
10359 args->push_back(Expression::make_var_reference(vno, location));
10363 Call_expression* call = Expression::make_call(bm, args,
10364 method_type->is_varargs(),
10365 location);
10367 size_t count = call->result_count();
10368 Statement* s;
10369 if (count == 0)
10370 s = Statement::make_statement(call);
10371 else
10373 Expression_list* retvals = new Expression_list();
10374 if (count <= 1)
10375 retvals->push_back(call);
10376 else
10378 for (size_t i = 0; i < count; ++i)
10379 retvals->push_back(Expression::make_call_result(call, i));
10381 s = Statement::make_return_statement(no->func_value()->type()->results(),
10382 retvals, location);
10384 gogo->add_statement(s);
10386 gogo->finish_function(location);
10388 return Expression::make_func_reference(no, NULL, location);
10391 // Make a selector expression.
10393 Expression*
10394 Expression::make_selector(Expression* left, const std::string& name,
10395 source_location location)
10397 return new Selector_expression(left, name, location);
10400 // Implement the builtin function new.
10402 class Allocation_expression : public Expression
10404 public:
10405 Allocation_expression(Type* type, source_location location)
10406 : Expression(EXPRESSION_ALLOCATION, location),
10407 type_(type)
10410 protected:
10412 do_traverse(Traverse* traverse)
10413 { return Type::traverse(this->type_, traverse); }
10415 Type*
10416 do_type()
10417 { return Type::make_pointer_type(this->type_); }
10419 void
10420 do_determine_type(const Type_context*)
10423 void
10424 do_check_types(Gogo*);
10426 Expression*
10427 do_copy()
10428 { return new Allocation_expression(this->type_, this->location()); }
10430 tree
10431 do_get_tree(Translate_context*);
10433 private:
10434 // The type we are allocating.
10435 Type* type_;
10438 // Check the type of an allocation expression.
10440 void
10441 Allocation_expression::do_check_types(Gogo*)
10443 if (this->type_->function_type() != NULL)
10444 this->report_error(_("invalid new of function type"));
10447 // Return a tree for an allocation expression.
10449 tree
10450 Allocation_expression::do_get_tree(Translate_context* context)
10452 tree type_tree = this->type_->get_tree(context->gogo());
10453 if (type_tree == error_mark_node)
10454 return error_mark_node;
10455 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10456 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10457 this->location());
10458 if (space == error_mark_node)
10459 return error_mark_node;
10460 return fold_convert(build_pointer_type(type_tree), space);
10463 // Make an allocation expression.
10465 Expression*
10466 Expression::make_allocation(Type* type, source_location location)
10468 return new Allocation_expression(type, location);
10471 // Implement the builtin function make.
10473 class Make_expression : public Expression
10475 public:
10476 Make_expression(Type* type, Expression_list* args, source_location location)
10477 : Expression(EXPRESSION_MAKE, location),
10478 type_(type), args_(args)
10481 protected:
10483 do_traverse(Traverse* traverse);
10485 Type*
10486 do_type()
10487 { return this->type_; }
10489 void
10490 do_determine_type(const Type_context*);
10492 void
10493 do_check_types(Gogo*);
10495 Expression*
10496 do_copy()
10498 return new Make_expression(this->type_, this->args_->copy(),
10499 this->location());
10502 tree
10503 do_get_tree(Translate_context*);
10505 private:
10506 // The type we are making.
10507 Type* type_;
10508 // The arguments to pass to the make routine.
10509 Expression_list* args_;
10512 // Traversal.
10515 Make_expression::do_traverse(Traverse* traverse)
10517 if (this->args_ != NULL
10518 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10519 return TRAVERSE_EXIT;
10520 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10521 return TRAVERSE_EXIT;
10522 return TRAVERSE_CONTINUE;
10525 // Set types of arguments.
10527 void
10528 Make_expression::do_determine_type(const Type_context*)
10530 if (this->args_ != NULL)
10532 Type_context context(Type::lookup_integer_type("int"), false);
10533 for (Expression_list::const_iterator pe = this->args_->begin();
10534 pe != this->args_->end();
10535 ++pe)
10536 (*pe)->determine_type(&context);
10540 // Check types for a make expression.
10542 void
10543 Make_expression::do_check_types(Gogo*)
10545 if (this->type_->channel_type() == NULL
10546 && this->type_->map_type() == NULL
10547 && (this->type_->array_type() == NULL
10548 || this->type_->array_type()->length() != NULL))
10549 this->report_error(_("invalid type for make function"));
10550 else if (!this->type_->check_make_expression(this->args_, this->location()))
10551 this->set_is_error();
10554 // Return a tree for a make expression.
10556 tree
10557 Make_expression::do_get_tree(Translate_context* context)
10559 return this->type_->make_expression_tree(context, this->args_,
10560 this->location());
10563 // Make a make expression.
10565 Expression*
10566 Expression::make_make(Type* type, Expression_list* args,
10567 source_location location)
10569 return new Make_expression(type, args, location);
10572 // Construct a struct.
10574 class Struct_construction_expression : public Expression
10576 public:
10577 Struct_construction_expression(Type* type, Expression_list* vals,
10578 source_location location)
10579 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10580 type_(type), vals_(vals)
10583 // Return whether this is a constant initializer.
10584 bool
10585 is_constant_struct() const;
10587 protected:
10589 do_traverse(Traverse* traverse);
10591 Type*
10592 do_type()
10593 { return this->type_; }
10595 void
10596 do_determine_type(const Type_context*);
10598 void
10599 do_check_types(Gogo*);
10601 Expression*
10602 do_copy()
10604 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10605 this->location());
10608 bool
10609 do_is_addressable() const
10610 { return true; }
10612 tree
10613 do_get_tree(Translate_context*);
10615 void
10616 do_export(Export*) const;
10618 private:
10619 // The type of the struct to construct.
10620 Type* type_;
10621 // The list of values, in order of the fields in the struct. A NULL
10622 // entry means that the field should be zero-initialized.
10623 Expression_list* vals_;
10626 // Traversal.
10629 Struct_construction_expression::do_traverse(Traverse* traverse)
10631 if (this->vals_ != NULL
10632 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10633 return TRAVERSE_EXIT;
10634 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10635 return TRAVERSE_EXIT;
10636 return TRAVERSE_CONTINUE;
10639 // Return whether this is a constant initializer.
10641 bool
10642 Struct_construction_expression::is_constant_struct() const
10644 if (this->vals_ == NULL)
10645 return true;
10646 for (Expression_list::const_iterator pv = this->vals_->begin();
10647 pv != this->vals_->end();
10648 ++pv)
10650 if (*pv != NULL
10651 && !(*pv)->is_constant()
10652 && (!(*pv)->is_composite_literal()
10653 || (*pv)->is_nonconstant_composite_literal()))
10654 return false;
10657 const Struct_field_list* fields = this->type_->struct_type()->fields();
10658 for (Struct_field_list::const_iterator pf = fields->begin();
10659 pf != fields->end();
10660 ++pf)
10662 // There are no constant constructors for interfaces.
10663 if (pf->type()->interface_type() != NULL)
10664 return false;
10667 return true;
10670 // Final type determination.
10672 void
10673 Struct_construction_expression::do_determine_type(const Type_context*)
10675 if (this->vals_ == NULL)
10676 return;
10677 const Struct_field_list* fields = this->type_->struct_type()->fields();
10678 Expression_list::const_iterator pv = this->vals_->begin();
10679 for (Struct_field_list::const_iterator pf = fields->begin();
10680 pf != fields->end();
10681 ++pf, ++pv)
10683 if (pv == this->vals_->end())
10684 return;
10685 if (*pv != NULL)
10687 Type_context subcontext(pf->type(), false);
10688 (*pv)->determine_type(&subcontext);
10691 // Extra values are an error we will report elsewhere; we still want
10692 // to determine the type to avoid knockon errors.
10693 for (; pv != this->vals_->end(); ++pv)
10694 (*pv)->determine_type_no_context();
10697 // Check types.
10699 void
10700 Struct_construction_expression::do_check_types(Gogo*)
10702 if (this->vals_ == NULL)
10703 return;
10705 Struct_type* st = this->type_->struct_type();
10706 if (this->vals_->size() > st->field_count())
10708 this->report_error(_("too many expressions for struct"));
10709 return;
10712 const Struct_field_list* fields = st->fields();
10713 Expression_list::const_iterator pv = this->vals_->begin();
10714 int i = 0;
10715 for (Struct_field_list::const_iterator pf = fields->begin();
10716 pf != fields->end();
10717 ++pf, ++pv, ++i)
10719 if (pv == this->vals_->end())
10721 this->report_error(_("too few expressions for struct"));
10722 break;
10725 if (*pv == NULL)
10726 continue;
10728 std::string reason;
10729 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10731 if (reason.empty())
10732 error_at((*pv)->location(),
10733 "incompatible type for field %d in struct construction",
10734 i + 1);
10735 else
10736 error_at((*pv)->location(),
10737 ("incompatible type for field %d in "
10738 "struct construction (%s)"),
10739 i + 1, reason.c_str());
10740 this->set_is_error();
10743 gcc_assert(pv == this->vals_->end());
10746 // Return a tree for constructing a struct.
10748 tree
10749 Struct_construction_expression::do_get_tree(Translate_context* context)
10751 Gogo* gogo = context->gogo();
10753 if (this->vals_ == NULL)
10754 return this->type_->get_init_tree(gogo, false);
10756 tree type_tree = this->type_->get_tree(gogo);
10757 if (type_tree == error_mark_node)
10758 return error_mark_node;
10759 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10761 bool is_constant = true;
10762 const Struct_field_list* fields = this->type_->struct_type()->fields();
10763 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10764 fields->size());
10765 Struct_field_list::const_iterator pf = fields->begin();
10766 Expression_list::const_iterator pv = this->vals_->begin();
10767 for (tree field = TYPE_FIELDS(type_tree);
10768 field != NULL_TREE;
10769 field = DECL_CHAIN(field), ++pf)
10771 gcc_assert(pf != fields->end());
10773 tree val;
10774 if (pv == this->vals_->end())
10775 val = pf->type()->get_init_tree(gogo, false);
10776 else if (*pv == NULL)
10778 val = pf->type()->get_init_tree(gogo, false);
10779 ++pv;
10781 else
10783 val = Expression::convert_for_assignment(context, pf->type(),
10784 (*pv)->type(),
10785 (*pv)->get_tree(context),
10786 this->location());
10787 ++pv;
10790 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10791 return error_mark_node;
10793 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10794 elt->index = field;
10795 elt->value = val;
10796 if (!TREE_CONSTANT(val))
10797 is_constant = false;
10799 gcc_assert(pf == fields->end());
10801 tree ret = build_constructor(type_tree, elts);
10802 if (is_constant)
10803 TREE_CONSTANT(ret) = 1;
10804 return ret;
10807 // Export a struct construction.
10809 void
10810 Struct_construction_expression::do_export(Export* exp) const
10812 exp->write_c_string("convert(");
10813 exp->write_type(this->type_);
10814 for (Expression_list::const_iterator pv = this->vals_->begin();
10815 pv != this->vals_->end();
10816 ++pv)
10818 exp->write_c_string(", ");
10819 if (*pv != NULL)
10820 (*pv)->export_expression(exp);
10822 exp->write_c_string(")");
10825 // Make a struct composite literal. This used by the thunk code.
10827 Expression*
10828 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
10829 source_location location)
10831 gcc_assert(type->struct_type() != NULL);
10832 return new Struct_construction_expression(type, vals, location);
10835 // Construct an array. This class is not used directly; instead we
10836 // use the child classes, Fixed_array_construction_expression and
10837 // Open_array_construction_expression.
10839 class Array_construction_expression : public Expression
10841 protected:
10842 Array_construction_expression(Expression_classification classification,
10843 Type* type, Expression_list* vals,
10844 source_location location)
10845 : Expression(classification, location),
10846 type_(type), vals_(vals)
10849 public:
10850 // Return whether this is a constant initializer.
10851 bool
10852 is_constant_array() const;
10854 // Return the number of elements.
10855 size_t
10856 element_count() const
10857 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
10859 protected:
10861 do_traverse(Traverse* traverse);
10863 Type*
10864 do_type()
10865 { return this->type_; }
10867 void
10868 do_determine_type(const Type_context*);
10870 void
10871 do_check_types(Gogo*);
10873 bool
10874 do_is_addressable() const
10875 { return true; }
10877 void
10878 do_export(Export*) const;
10880 // The list of values.
10881 Expression_list*
10882 vals()
10883 { return this->vals_; }
10885 // Get a constructor tree for the array values.
10886 tree
10887 get_constructor_tree(Translate_context* context, tree type_tree);
10889 private:
10890 // The type of the array to construct.
10891 Type* type_;
10892 // The list of values.
10893 Expression_list* vals_;
10896 // Traversal.
10899 Array_construction_expression::do_traverse(Traverse* traverse)
10901 if (this->vals_ != NULL
10902 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10903 return TRAVERSE_EXIT;
10904 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10905 return TRAVERSE_EXIT;
10906 return TRAVERSE_CONTINUE;
10909 // Return whether this is a constant initializer.
10911 bool
10912 Array_construction_expression::is_constant_array() const
10914 if (this->vals_ == NULL)
10915 return true;
10917 // There are no constant constructors for interfaces.
10918 if (this->type_->array_type()->element_type()->interface_type() != NULL)
10919 return false;
10921 for (Expression_list::const_iterator pv = this->vals_->begin();
10922 pv != this->vals_->end();
10923 ++pv)
10925 if (*pv != NULL
10926 && !(*pv)->is_constant()
10927 && (!(*pv)->is_composite_literal()
10928 || (*pv)->is_nonconstant_composite_literal()))
10929 return false;
10931 return true;
10934 // Final type determination.
10936 void
10937 Array_construction_expression::do_determine_type(const Type_context*)
10939 if (this->vals_ == NULL)
10940 return;
10941 Type_context subcontext(this->type_->array_type()->element_type(), false);
10942 for (Expression_list::const_iterator pv = this->vals_->begin();
10943 pv != this->vals_->end();
10944 ++pv)
10946 if (*pv != NULL)
10947 (*pv)->determine_type(&subcontext);
10951 // Check types.
10953 void
10954 Array_construction_expression::do_check_types(Gogo*)
10956 if (this->vals_ == NULL)
10957 return;
10959 Array_type* at = this->type_->array_type();
10960 int i = 0;
10961 Type* element_type = at->element_type();
10962 for (Expression_list::const_iterator pv = this->vals_->begin();
10963 pv != this->vals_->end();
10964 ++pv, ++i)
10966 if (*pv != NULL
10967 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
10969 error_at((*pv)->location(),
10970 "incompatible type for element %d in composite literal",
10971 i + 1);
10972 this->set_is_error();
10976 Expression* length = at->length();
10977 if (length != NULL)
10979 mpz_t val;
10980 mpz_init(val);
10981 Type* type;
10982 if (at->length()->integer_constant_value(true, val, &type))
10984 if (this->vals_->size() > mpz_get_ui(val))
10985 this->report_error(_("too many elements in composite literal"));
10987 mpz_clear(val);
10991 // Get a constructor tree for the array values.
10993 tree
10994 Array_construction_expression::get_constructor_tree(Translate_context* context,
10995 tree type_tree)
10997 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
10998 (this->vals_ == NULL
11000 : this->vals_->size()));
11001 Type* element_type = this->type_->array_type()->element_type();
11002 bool is_constant = true;
11003 if (this->vals_ != NULL)
11005 size_t i = 0;
11006 for (Expression_list::const_iterator pv = this->vals_->begin();
11007 pv != this->vals_->end();
11008 ++pv, ++i)
11010 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
11011 elt->index = size_int(i);
11012 if (*pv == NULL)
11013 elt->value = element_type->get_init_tree(context->gogo(), false);
11014 else
11016 tree value_tree = (*pv)->get_tree(context);
11017 elt->value = Expression::convert_for_assignment(context,
11018 element_type,
11019 (*pv)->type(),
11020 value_tree,
11021 this->location());
11023 if (elt->value == error_mark_node)
11024 return error_mark_node;
11025 if (!TREE_CONSTANT(elt->value))
11026 is_constant = false;
11030 tree ret = build_constructor(type_tree, values);
11031 if (is_constant)
11032 TREE_CONSTANT(ret) = 1;
11033 return ret;
11036 // Export an array construction.
11038 void
11039 Array_construction_expression::do_export(Export* exp) const
11041 exp->write_c_string("convert(");
11042 exp->write_type(this->type_);
11043 if (this->vals_ != NULL)
11045 for (Expression_list::const_iterator pv = this->vals_->begin();
11046 pv != this->vals_->end();
11047 ++pv)
11049 exp->write_c_string(", ");
11050 if (*pv != NULL)
11051 (*pv)->export_expression(exp);
11054 exp->write_c_string(")");
11057 // Construct a fixed array.
11059 class Fixed_array_construction_expression :
11060 public Array_construction_expression
11062 public:
11063 Fixed_array_construction_expression(Type* type, Expression_list* vals,
11064 source_location location)
11065 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
11066 type, vals, location)
11068 gcc_assert(type->array_type() != NULL
11069 && type->array_type()->length() != NULL);
11072 protected:
11073 Expression*
11074 do_copy()
11076 return new Fixed_array_construction_expression(this->type(),
11077 (this->vals() == NULL
11078 ? NULL
11079 : this->vals()->copy()),
11080 this->location());
11083 tree
11084 do_get_tree(Translate_context*);
11087 // Return a tree for constructing a fixed array.
11089 tree
11090 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11092 return this->get_constructor_tree(context,
11093 this->type()->get_tree(context->gogo()));
11096 // Construct an open array.
11098 class Open_array_construction_expression : public Array_construction_expression
11100 public:
11101 Open_array_construction_expression(Type* type, Expression_list* vals,
11102 source_location location)
11103 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
11104 type, vals, location)
11106 gcc_assert(type->array_type() != NULL
11107 && type->array_type()->length() == NULL);
11110 protected:
11111 // Note that taking the address of an open array literal is invalid.
11113 Expression*
11114 do_copy()
11116 return new Open_array_construction_expression(this->type(),
11117 (this->vals() == NULL
11118 ? NULL
11119 : this->vals()->copy()),
11120 this->location());
11123 tree
11124 do_get_tree(Translate_context*);
11127 // Return a tree for constructing an open array.
11129 tree
11130 Open_array_construction_expression::do_get_tree(Translate_context* context)
11132 Array_type* array_type = this->type()->array_type();
11133 if (array_type == NULL)
11135 gcc_assert(this->type()->is_error_type());
11136 return error_mark_node;
11139 Type* element_type = array_type->element_type();
11140 tree element_type_tree = element_type->get_tree(context->gogo());
11141 if (element_type_tree == error_mark_node)
11142 return error_mark_node;
11144 tree values;
11145 tree length_tree;
11146 if (this->vals() == NULL || this->vals()->empty())
11148 // We need to create a unique value.
11149 tree max = size_int(0);
11150 tree constructor_type = build_array_type(element_type_tree,
11151 build_index_type(max));
11152 if (constructor_type == error_mark_node)
11153 return error_mark_node;
11154 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
11155 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
11156 elt->index = size_int(0);
11157 elt->value = element_type->get_init_tree(context->gogo(), false);
11158 values = build_constructor(constructor_type, vec);
11159 if (TREE_CONSTANT(elt->value))
11160 TREE_CONSTANT(values) = 1;
11161 length_tree = size_int(0);
11163 else
11165 tree max = size_int(this->vals()->size() - 1);
11166 tree constructor_type = build_array_type(element_type_tree,
11167 build_index_type(max));
11168 if (constructor_type == error_mark_node)
11169 return error_mark_node;
11170 values = this->get_constructor_tree(context, constructor_type);
11171 length_tree = size_int(this->vals()->size());
11174 if (values == error_mark_node)
11175 return error_mark_node;
11177 bool is_constant_initializer = TREE_CONSTANT(values);
11179 // We have to copy the initial values into heap memory if we are in
11180 // a function or if the values are not constants. We also have to
11181 // copy them if they may contain pointers in a non-constant context,
11182 // as otherwise the garbage collector won't see them.
11183 bool copy_to_heap = (context->function() != NULL
11184 || !is_constant_initializer
11185 || (element_type->has_pointer()
11186 && !context->is_const()));
11188 if (is_constant_initializer)
11190 tree tmp = build_decl(this->location(), VAR_DECL,
11191 create_tmp_var_name("C"), TREE_TYPE(values));
11192 DECL_EXTERNAL(tmp) = 0;
11193 TREE_PUBLIC(tmp) = 0;
11194 TREE_STATIC(tmp) = 1;
11195 DECL_ARTIFICIAL(tmp) = 1;
11196 if (copy_to_heap)
11198 // If we are not copying the value to the heap, we will only
11199 // initialize the value once, so we can use this directly
11200 // rather than copying it. In that case we can't make it
11201 // read-only, because the program is permitted to change it.
11202 TREE_READONLY(tmp) = 1;
11203 TREE_CONSTANT(tmp) = 1;
11205 DECL_INITIAL(tmp) = values;
11206 rest_of_decl_compilation(tmp, 1, 0);
11207 values = tmp;
11210 tree space;
11211 tree set;
11212 if (!copy_to_heap)
11214 // the initializer will only run once.
11215 space = build_fold_addr_expr(values);
11216 set = NULL_TREE;
11218 else
11220 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
11221 space = context->gogo()->allocate_memory(element_type, memsize,
11222 this->location());
11223 space = save_expr(space);
11225 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
11226 tree ref = build_fold_indirect_ref_loc(this->location(), s);
11227 TREE_THIS_NOTRAP(ref) = 1;
11228 set = build2(MODIFY_EXPR, void_type_node, ref, values);
11231 // Build a constructor for the open array.
11233 tree type_tree = this->type()->get_tree(context->gogo());
11234 if (type_tree == error_mark_node)
11235 return error_mark_node;
11236 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11238 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
11240 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
11241 tree field = TYPE_FIELDS(type_tree);
11242 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
11243 elt->index = field;
11244 elt->value = fold_convert(TREE_TYPE(field), space);
11246 elt = VEC_quick_push(constructor_elt, init, NULL);
11247 field = DECL_CHAIN(field);
11248 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
11249 elt->index = field;
11250 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11252 elt = VEC_quick_push(constructor_elt, init, NULL);
11253 field = DECL_CHAIN(field);
11254 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
11255 elt->index = field;
11256 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11258 tree constructor = build_constructor(type_tree, init);
11259 if (constructor == error_mark_node)
11260 return error_mark_node;
11261 if (!copy_to_heap)
11262 TREE_CONSTANT(constructor) = 1;
11264 if (set == NULL_TREE)
11265 return constructor;
11266 else
11267 return build2(COMPOUND_EXPR, type_tree, set, constructor);
11270 // Make a slice composite literal. This is used by the type
11271 // descriptor code.
11273 Expression*
11274 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
11275 source_location location)
11277 gcc_assert(type->is_open_array_type());
11278 return new Open_array_construction_expression(type, vals, location);
11281 // Construct a map.
11283 class Map_construction_expression : public Expression
11285 public:
11286 Map_construction_expression(Type* type, Expression_list* vals,
11287 source_location location)
11288 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
11289 type_(type), vals_(vals)
11290 { gcc_assert(vals == NULL || vals->size() % 2 == 0); }
11292 protected:
11294 do_traverse(Traverse* traverse);
11296 Type*
11297 do_type()
11298 { return this->type_; }
11300 void
11301 do_determine_type(const Type_context*);
11303 void
11304 do_check_types(Gogo*);
11306 Expression*
11307 do_copy()
11309 return new Map_construction_expression(this->type_, this->vals_->copy(),
11310 this->location());
11313 tree
11314 do_get_tree(Translate_context*);
11316 void
11317 do_export(Export*) const;
11319 private:
11320 // The type of the map to construct.
11321 Type* type_;
11322 // The list of values.
11323 Expression_list* vals_;
11326 // Traversal.
11329 Map_construction_expression::do_traverse(Traverse* traverse)
11331 if (this->vals_ != NULL
11332 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11333 return TRAVERSE_EXIT;
11334 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11335 return TRAVERSE_EXIT;
11336 return TRAVERSE_CONTINUE;
11339 // Final type determination.
11341 void
11342 Map_construction_expression::do_determine_type(const Type_context*)
11344 if (this->vals_ == NULL)
11345 return;
11347 Map_type* mt = this->type_->map_type();
11348 Type_context key_context(mt->key_type(), false);
11349 Type_context val_context(mt->val_type(), false);
11350 for (Expression_list::const_iterator pv = this->vals_->begin();
11351 pv != this->vals_->end();
11352 ++pv)
11354 (*pv)->determine_type(&key_context);
11355 ++pv;
11356 (*pv)->determine_type(&val_context);
11360 // Check types.
11362 void
11363 Map_construction_expression::do_check_types(Gogo*)
11365 if (this->vals_ == NULL)
11366 return;
11368 Map_type* mt = this->type_->map_type();
11369 int i = 0;
11370 Type* key_type = mt->key_type();
11371 Type* val_type = mt->val_type();
11372 for (Expression_list::const_iterator pv = this->vals_->begin();
11373 pv != this->vals_->end();
11374 ++pv, ++i)
11376 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
11378 error_at((*pv)->location(),
11379 "incompatible type for element %d key in map construction",
11380 i + 1);
11381 this->set_is_error();
11383 ++pv;
11384 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
11386 error_at((*pv)->location(),
11387 ("incompatible type for element %d value "
11388 "in map construction"),
11389 i + 1);
11390 this->set_is_error();
11395 // Return a tree for constructing a map.
11397 tree
11398 Map_construction_expression::do_get_tree(Translate_context* context)
11400 Gogo* gogo = context->gogo();
11401 source_location loc = this->location();
11403 Map_type* mt = this->type_->map_type();
11405 // Build a struct to hold the key and value.
11406 tree struct_type = make_node(RECORD_TYPE);
11408 Type* key_type = mt->key_type();
11409 tree id = get_identifier("__key");
11410 tree key_type_tree = key_type->get_tree(gogo);
11411 if (key_type_tree == error_mark_node)
11412 return error_mark_node;
11413 tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree);
11414 DECL_CONTEXT(key_field) = struct_type;
11415 TYPE_FIELDS(struct_type) = key_field;
11417 Type* val_type = mt->val_type();
11418 id = get_identifier("__val");
11419 tree val_type_tree = val_type->get_tree(gogo);
11420 if (val_type_tree == error_mark_node)
11421 return error_mark_node;
11422 tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree);
11423 DECL_CONTEXT(val_field) = struct_type;
11424 DECL_CHAIN(key_field) = val_field;
11426 layout_type(struct_type);
11428 bool is_constant = true;
11429 size_t i = 0;
11430 tree valaddr;
11431 tree make_tmp;
11433 if (this->vals_ == NULL || this->vals_->empty())
11435 valaddr = null_pointer_node;
11436 make_tmp = NULL_TREE;
11438 else
11440 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11441 this->vals_->size() / 2);
11443 for (Expression_list::const_iterator pv = this->vals_->begin();
11444 pv != this->vals_->end();
11445 ++pv, ++i)
11447 bool one_is_constant = true;
11449 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
11451 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11452 elt->index = key_field;
11453 tree val_tree = (*pv)->get_tree(context);
11454 elt->value = Expression::convert_for_assignment(context, key_type,
11455 (*pv)->type(),
11456 val_tree, loc);
11457 if (elt->value == error_mark_node)
11458 return error_mark_node;
11459 if (!TREE_CONSTANT(elt->value))
11460 one_is_constant = false;
11462 ++pv;
11464 elt = VEC_quick_push(constructor_elt, one, NULL);
11465 elt->index = val_field;
11466 val_tree = (*pv)->get_tree(context);
11467 elt->value = Expression::convert_for_assignment(context, val_type,
11468 (*pv)->type(),
11469 val_tree, loc);
11470 if (elt->value == error_mark_node)
11471 return error_mark_node;
11472 if (!TREE_CONSTANT(elt->value))
11473 one_is_constant = false;
11475 elt = VEC_quick_push(constructor_elt, values, NULL);
11476 elt->index = size_int(i);
11477 elt->value = build_constructor(struct_type, one);
11478 if (one_is_constant)
11479 TREE_CONSTANT(elt->value) = 1;
11480 else
11481 is_constant = false;
11484 tree index_type = build_index_type(size_int(i - 1));
11485 tree array_type = build_array_type(struct_type, index_type);
11486 tree init = build_constructor(array_type, values);
11487 if (is_constant)
11488 TREE_CONSTANT(init) = 1;
11489 tree tmp;
11490 if (current_function_decl != NULL)
11492 tmp = create_tmp_var(array_type, get_name(array_type));
11493 DECL_INITIAL(tmp) = init;
11494 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11495 TREE_ADDRESSABLE(tmp) = 1;
11497 else
11499 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11500 DECL_EXTERNAL(tmp) = 0;
11501 TREE_PUBLIC(tmp) = 0;
11502 TREE_STATIC(tmp) = 1;
11503 DECL_ARTIFICIAL(tmp) = 1;
11504 if (!TREE_CONSTANT(init))
11505 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11506 init);
11507 else
11509 TREE_READONLY(tmp) = 1;
11510 TREE_CONSTANT(tmp) = 1;
11511 DECL_INITIAL(tmp) = init;
11512 make_tmp = NULL_TREE;
11514 rest_of_decl_compilation(tmp, 1, 0);
11517 valaddr = build_fold_addr_expr(tmp);
11520 tree descriptor = gogo->map_descriptor(mt);
11522 tree type_tree = this->type_->get_tree(gogo);
11523 if (type_tree == error_mark_node)
11524 return error_mark_node;
11526 static tree construct_map_fndecl;
11527 tree call = Gogo::call_builtin(&construct_map_fndecl,
11528 loc,
11529 "__go_construct_map",
11531 type_tree,
11532 TREE_TYPE(descriptor),
11533 descriptor,
11534 sizetype,
11535 size_int(i),
11536 sizetype,
11537 TYPE_SIZE_UNIT(struct_type),
11538 sizetype,
11539 byte_position(val_field),
11540 sizetype,
11541 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11542 const_ptr_type_node,
11543 fold_convert(const_ptr_type_node, valaddr));
11544 if (call == error_mark_node)
11545 return error_mark_node;
11547 tree ret;
11548 if (make_tmp == NULL)
11549 ret = call;
11550 else
11551 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11552 return ret;
11555 // Export an array construction.
11557 void
11558 Map_construction_expression::do_export(Export* exp) const
11560 exp->write_c_string("convert(");
11561 exp->write_type(this->type_);
11562 for (Expression_list::const_iterator pv = this->vals_->begin();
11563 pv != this->vals_->end();
11564 ++pv)
11566 exp->write_c_string(", ");
11567 (*pv)->export_expression(exp);
11569 exp->write_c_string(")");
11572 // A general composite literal. This is lowered to a type specific
11573 // version.
11575 class Composite_literal_expression : public Parser_expression
11577 public:
11578 Composite_literal_expression(Type* type, int depth, bool has_keys,
11579 Expression_list* vals, source_location location)
11580 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11581 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11584 protected:
11586 do_traverse(Traverse* traverse);
11588 Expression*
11589 do_lower(Gogo*, Named_object*, int);
11591 Expression*
11592 do_copy()
11594 return new Composite_literal_expression(this->type_, this->depth_,
11595 this->has_keys_,
11596 (this->vals_ == NULL
11597 ? NULL
11598 : this->vals_->copy()),
11599 this->location());
11602 private:
11603 Expression*
11604 lower_struct(Type*);
11606 Expression*
11607 lower_array(Type*);
11609 Expression*
11610 make_array(Type*, Expression_list*);
11612 Expression*
11613 lower_map(Gogo*, Named_object*, Type*);
11615 // The type of the composite literal.
11616 Type* type_;
11617 // The depth within a list of composite literals within a composite
11618 // literal, when the type is omitted.
11619 int depth_;
11620 // The values to put in the composite literal.
11621 Expression_list* vals_;
11622 // If this is true, then VALS_ is a list of pairs: a key and a
11623 // value. In an array initializer, a missing key will be NULL.
11624 bool has_keys_;
11627 // Traversal.
11630 Composite_literal_expression::do_traverse(Traverse* traverse)
11632 if (this->vals_ != NULL
11633 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11634 return TRAVERSE_EXIT;
11635 return Type::traverse(this->type_, traverse);
11638 // Lower a generic composite literal into a specific version based on
11639 // the type.
11641 Expression*
11642 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int)
11644 Type* type = this->type_;
11646 for (int depth = this->depth_; depth > 0; --depth)
11648 if (type->array_type() != NULL)
11649 type = type->array_type()->element_type();
11650 else if (type->map_type() != NULL)
11651 type = type->map_type()->val_type();
11652 else
11654 if (!type->is_error_type())
11655 error_at(this->location(),
11656 ("may only omit types within composite literals "
11657 "of slice, array, or map type"));
11658 return Expression::make_error(this->location());
11662 if (type->is_error_type())
11663 return Expression::make_error(this->location());
11664 else if (type->struct_type() != NULL)
11665 return this->lower_struct(type);
11666 else if (type->array_type() != NULL)
11667 return this->lower_array(type);
11668 else if (type->map_type() != NULL)
11669 return this->lower_map(gogo, function, type);
11670 else
11672 error_at(this->location(),
11673 ("expected struct, slice, array, or map type "
11674 "for composite literal"));
11675 return Expression::make_error(this->location());
11679 // Lower a struct composite literal.
11681 Expression*
11682 Composite_literal_expression::lower_struct(Type* type)
11684 source_location location = this->location();
11685 Struct_type* st = type->struct_type();
11686 if (this->vals_ == NULL || !this->has_keys_)
11687 return new Struct_construction_expression(type, this->vals_, location);
11689 size_t field_count = st->field_count();
11690 std::vector<Expression*> vals(field_count);
11691 Expression_list::const_iterator p = this->vals_->begin();
11692 while (p != this->vals_->end())
11694 Expression* name_expr = *p;
11696 ++p;
11697 gcc_assert(p != this->vals_->end());
11698 Expression* val = *p;
11700 ++p;
11702 if (name_expr == NULL)
11704 error_at(val->location(), "mixture of field and value initializers");
11705 return Expression::make_error(location);
11708 bool bad_key = false;
11709 std::string name;
11710 switch (name_expr->classification())
11712 case EXPRESSION_UNKNOWN_REFERENCE:
11713 name = name_expr->unknown_expression()->name();
11714 break;
11716 case EXPRESSION_CONST_REFERENCE:
11717 name = static_cast<Const_expression*>(name_expr)->name();
11718 break;
11720 case EXPRESSION_TYPE:
11722 Type* t = name_expr->type();
11723 Named_type* nt = t->named_type();
11724 if (nt == NULL)
11725 bad_key = true;
11726 else
11727 name = nt->name();
11729 break;
11731 case EXPRESSION_VAR_REFERENCE:
11732 name = name_expr->var_expression()->name();
11733 break;
11735 case EXPRESSION_FUNC_REFERENCE:
11736 name = name_expr->func_expression()->name();
11737 break;
11739 case EXPRESSION_UNARY:
11740 // If there is a local variable around with the same name as
11741 // the field, and this occurs in the closure, then the
11742 // parser may turn the field reference into an indirection
11743 // through the closure. FIXME: This is a mess.
11745 bad_key = true;
11746 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11747 if (ue->op() == OPERATOR_MULT)
11749 Field_reference_expression* fre =
11750 ue->operand()->field_reference_expression();
11751 if (fre != NULL)
11753 Struct_type* st =
11754 fre->expr()->type()->deref()->struct_type();
11755 if (st != NULL)
11757 const Struct_field* sf = st->field(fre->field_index());
11758 name = sf->field_name();
11759 char buf[20];
11760 snprintf(buf, sizeof buf, "%u", fre->field_index());
11761 size_t buflen = strlen(buf);
11762 if (name.compare(name.length() - buflen, buflen, buf)
11763 == 0)
11765 name = name.substr(0, name.length() - buflen);
11766 bad_key = false;
11772 break;
11774 default:
11775 bad_key = true;
11776 break;
11778 if (bad_key)
11780 error_at(name_expr->location(), "expected struct field name");
11781 return Expression::make_error(location);
11784 unsigned int index;
11785 const Struct_field* sf = st->find_local_field(name, &index);
11786 if (sf == NULL)
11788 error_at(name_expr->location(), "unknown field %qs in %qs",
11789 Gogo::message_name(name).c_str(),
11790 (type->named_type() != NULL
11791 ? type->named_type()->message_name().c_str()
11792 : "unnamed struct"));
11793 return Expression::make_error(location);
11795 if (vals[index] != NULL)
11797 error_at(name_expr->location(),
11798 "duplicate value for field %qs in %qs",
11799 Gogo::message_name(name).c_str(),
11800 (type->named_type() != NULL
11801 ? type->named_type()->message_name().c_str()
11802 : "unnamed struct"));
11803 return Expression::make_error(location);
11806 vals[index] = val;
11809 Expression_list* list = new Expression_list;
11810 list->reserve(field_count);
11811 for (size_t i = 0; i < field_count; ++i)
11812 list->push_back(vals[i]);
11814 return new Struct_construction_expression(type, list, location);
11817 // Lower an array composite literal.
11819 Expression*
11820 Composite_literal_expression::lower_array(Type* type)
11822 source_location location = this->location();
11823 if (this->vals_ == NULL || !this->has_keys_)
11824 return this->make_array(type, this->vals_);
11826 std::vector<Expression*> vals;
11827 vals.reserve(this->vals_->size());
11828 unsigned long index = 0;
11829 Expression_list::const_iterator p = this->vals_->begin();
11830 while (p != this->vals_->end())
11832 Expression* index_expr = *p;
11834 ++p;
11835 gcc_assert(p != this->vals_->end());
11836 Expression* val = *p;
11838 ++p;
11840 if (index_expr != NULL)
11842 mpz_t ival;
11843 mpz_init(ival);
11844 Type* dummy;
11845 if (!index_expr->integer_constant_value(true, ival, &dummy))
11847 mpz_clear(ival);
11848 error_at(index_expr->location(),
11849 "index expression is not integer constant");
11850 return Expression::make_error(location);
11852 if (mpz_sgn(ival) < 0)
11854 mpz_clear(ival);
11855 error_at(index_expr->location(), "index expression is negative");
11856 return Expression::make_error(location);
11858 index = mpz_get_ui(ival);
11859 if (mpz_cmp_ui(ival, index) != 0)
11861 mpz_clear(ival);
11862 error_at(index_expr->location(), "index value overflow");
11863 return Expression::make_error(location);
11865 mpz_clear(ival);
11868 if (index == vals.size())
11869 vals.push_back(val);
11870 else
11872 if (index > vals.size())
11874 vals.reserve(index + 32);
11875 vals.resize(index + 1, static_cast<Expression*>(NULL));
11877 if (vals[index] != NULL)
11879 error_at((index_expr != NULL
11880 ? index_expr->location()
11881 : val->location()),
11882 "duplicate value for index %lu",
11883 index);
11884 return Expression::make_error(location);
11886 vals[index] = val;
11889 ++index;
11892 size_t size = vals.size();
11893 Expression_list* list = new Expression_list;
11894 list->reserve(size);
11895 for (size_t i = 0; i < size; ++i)
11896 list->push_back(vals[i]);
11898 return this->make_array(type, list);
11901 // Actually build the array composite literal. This handles
11902 // [...]{...}.
11904 Expression*
11905 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
11907 source_location location = this->location();
11908 Array_type* at = type->array_type();
11909 if (at->length() != NULL && at->length()->is_nil_expression())
11911 size_t size = vals == NULL ? 0 : vals->size();
11912 mpz_t vlen;
11913 mpz_init_set_ui(vlen, size);
11914 Expression* elen = Expression::make_integer(&vlen, NULL, location);
11915 mpz_clear(vlen);
11916 at = Type::make_array_type(at->element_type(), elen);
11917 type = at;
11919 if (at->length() != NULL)
11920 return new Fixed_array_construction_expression(type, vals, location);
11921 else
11922 return new Open_array_construction_expression(type, vals, location);
11925 // Lower a map composite literal.
11927 Expression*
11928 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
11929 Type* type)
11931 source_location location = this->location();
11932 if (this->vals_ != NULL)
11934 if (!this->has_keys_)
11936 error_at(location, "map composite literal must have keys");
11937 return Expression::make_error(location);
11940 for (Expression_list::iterator p = this->vals_->begin();
11941 p != this->vals_->end();
11942 p += 2)
11944 if (*p == NULL)
11946 ++p;
11947 error_at((*p)->location(),
11948 "map composite literal must have keys for every value");
11949 return Expression::make_error(location);
11951 // Make sure we have lowered the key; it may not have been
11952 // lowered in order to handle keys for struct composite
11953 // literals. Lower it now to get the right error message.
11954 if ((*p)->unknown_expression() != NULL)
11956 (*p)->unknown_expression()->clear_is_composite_literal_key();
11957 gogo->lower_expression(function, &*p);
11958 gcc_assert((*p)->is_error_expression());
11959 return Expression::make_error(location);
11964 return new Map_construction_expression(type, this->vals_, location);
11967 // Make a composite literal expression.
11969 Expression*
11970 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
11971 Expression_list* vals,
11972 source_location location)
11974 return new Composite_literal_expression(type, depth, has_keys, vals,
11975 location);
11978 // Return whether this expression is a composite literal.
11980 bool
11981 Expression::is_composite_literal() const
11983 switch (this->classification_)
11985 case EXPRESSION_COMPOSITE_LITERAL:
11986 case EXPRESSION_STRUCT_CONSTRUCTION:
11987 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
11988 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
11989 case EXPRESSION_MAP_CONSTRUCTION:
11990 return true;
11991 default:
11992 return false;
11996 // Return whether this expression is a composite literal which is not
11997 // constant.
11999 bool
12000 Expression::is_nonconstant_composite_literal() const
12002 switch (this->classification_)
12004 case EXPRESSION_STRUCT_CONSTRUCTION:
12006 const Struct_construction_expression *psce =
12007 static_cast<const Struct_construction_expression*>(this);
12008 return !psce->is_constant_struct();
12010 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12012 const Fixed_array_construction_expression *pace =
12013 static_cast<const Fixed_array_construction_expression*>(this);
12014 return !pace->is_constant_array();
12016 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12018 const Open_array_construction_expression *pace =
12019 static_cast<const Open_array_construction_expression*>(this);
12020 return !pace->is_constant_array();
12022 case EXPRESSION_MAP_CONSTRUCTION:
12023 return true;
12024 default:
12025 return false;
12029 // Return true if this is a reference to a local variable.
12031 bool
12032 Expression::is_local_variable() const
12034 const Var_expression* ve = this->var_expression();
12035 if (ve == NULL)
12036 return false;
12037 const Named_object* no = ve->named_object();
12038 return (no->is_result_variable()
12039 || (no->is_variable() && !no->var_value()->is_global()));
12042 // Class Type_guard_expression.
12044 // Traversal.
12047 Type_guard_expression::do_traverse(Traverse* traverse)
12049 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
12050 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12051 return TRAVERSE_EXIT;
12052 return TRAVERSE_CONTINUE;
12055 // Check types of a type guard expression. The expression must have
12056 // an interface type, but the actual type conversion is checked at run
12057 // time.
12059 void
12060 Type_guard_expression::do_check_types(Gogo*)
12062 // 6g permits using a type guard with unsafe.pointer; we are
12063 // compatible.
12064 Type* expr_type = this->expr_->type();
12065 if (expr_type->is_unsafe_pointer_type())
12067 if (this->type_->points_to() == NULL
12068 && (this->type_->integer_type() == NULL
12069 || (this->type_->forwarded()
12070 != Type::lookup_integer_type("uintptr"))))
12071 this->report_error(_("invalid unsafe.Pointer conversion"));
12073 else if (this->type_->is_unsafe_pointer_type())
12075 if (expr_type->points_to() == NULL
12076 && (expr_type->integer_type() == NULL
12077 || (expr_type->forwarded()
12078 != Type::lookup_integer_type("uintptr"))))
12079 this->report_error(_("invalid unsafe.Pointer conversion"));
12081 else if (expr_type->interface_type() == NULL)
12083 if (!expr_type->is_error_type() && !this->type_->is_error_type())
12084 this->report_error(_("type assertion only valid for interface types"));
12085 this->set_is_error();
12087 else if (this->type_->interface_type() == NULL)
12089 std::string reason;
12090 if (!expr_type->interface_type()->implements_interface(this->type_,
12091 &reason))
12093 if (!this->type_->is_error_type())
12095 if (reason.empty())
12096 this->report_error(_("impossible type assertion: "
12097 "type does not implement interface"));
12098 else
12099 error_at(this->location(),
12100 ("impossible type assertion: "
12101 "type does not implement interface (%s)"),
12102 reason.c_str());
12104 this->set_is_error();
12109 // Return a tree for a type guard expression.
12111 tree
12112 Type_guard_expression::do_get_tree(Translate_context* context)
12114 Gogo* gogo = context->gogo();
12115 tree expr_tree = this->expr_->get_tree(context);
12116 if (expr_tree == error_mark_node)
12117 return error_mark_node;
12118 Type* expr_type = this->expr_->type();
12119 if ((this->type_->is_unsafe_pointer_type()
12120 && (expr_type->points_to() != NULL
12121 || expr_type->integer_type() != NULL))
12122 || (expr_type->is_unsafe_pointer_type()
12123 && this->type_->points_to() != NULL))
12124 return convert_to_pointer(this->type_->get_tree(gogo), expr_tree);
12125 else if (expr_type->is_unsafe_pointer_type()
12126 && this->type_->integer_type() != NULL)
12127 return convert_to_integer(this->type_->get_tree(gogo), expr_tree);
12128 else if (this->type_->interface_type() != NULL)
12129 return Expression::convert_interface_to_interface(context, this->type_,
12130 this->expr_->type(),
12131 expr_tree, true,
12132 this->location());
12133 else
12134 return Expression::convert_for_assignment(context, this->type_,
12135 this->expr_->type(), expr_tree,
12136 this->location());
12139 // Make a type guard expression.
12141 Expression*
12142 Expression::make_type_guard(Expression* expr, Type* type,
12143 source_location location)
12145 return new Type_guard_expression(expr, type, location);
12148 // Class Heap_composite_expression.
12150 // When you take the address of a composite literal, it is allocated
12151 // on the heap. This class implements that.
12153 class Heap_composite_expression : public Expression
12155 public:
12156 Heap_composite_expression(Expression* expr, source_location location)
12157 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
12158 expr_(expr)
12161 protected:
12163 do_traverse(Traverse* traverse)
12164 { return Expression::traverse(&this->expr_, traverse); }
12166 Type*
12167 do_type()
12168 { return Type::make_pointer_type(this->expr_->type()); }
12170 void
12171 do_determine_type(const Type_context*)
12172 { this->expr_->determine_type_no_context(); }
12174 Expression*
12175 do_copy()
12177 return Expression::make_heap_composite(this->expr_->copy(),
12178 this->location());
12181 tree
12182 do_get_tree(Translate_context*);
12184 // We only export global objects, and the parser does not generate
12185 // this in global scope.
12186 void
12187 do_export(Export*) const
12188 { gcc_unreachable(); }
12190 private:
12191 // The composite literal which is being put on the heap.
12192 Expression* expr_;
12195 // Return a tree which allocates a composite literal on the heap.
12197 tree
12198 Heap_composite_expression::do_get_tree(Translate_context* context)
12200 tree expr_tree = this->expr_->get_tree(context);
12201 if (expr_tree == error_mark_node)
12202 return error_mark_node;
12203 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
12204 gcc_assert(TREE_CODE(expr_size) == INTEGER_CST);
12205 tree space = context->gogo()->allocate_memory(this->expr_->type(),
12206 expr_size, this->location());
12207 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
12208 space = save_expr(space);
12209 tree ref = build_fold_indirect_ref_loc(this->location(), space);
12210 TREE_THIS_NOTRAP(ref) = 1;
12211 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
12212 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
12213 space);
12214 SET_EXPR_LOCATION(ret, this->location());
12215 return ret;
12218 // Allocate a composite literal on the heap.
12220 Expression*
12221 Expression::make_heap_composite(Expression* expr, source_location location)
12223 return new Heap_composite_expression(expr, location);
12226 // Class Receive_expression.
12228 // Return the type of a receive expression.
12230 Type*
12231 Receive_expression::do_type()
12233 Channel_type* channel_type = this->channel_->type()->channel_type();
12234 if (channel_type == NULL)
12235 return Type::make_error_type();
12236 return channel_type->element_type();
12239 // Check types for a receive expression.
12241 void
12242 Receive_expression::do_check_types(Gogo*)
12244 Type* type = this->channel_->type();
12245 if (type->is_error_type())
12247 this->set_is_error();
12248 return;
12250 if (type->channel_type() == NULL)
12252 this->report_error(_("expected channel"));
12253 return;
12255 if (!type->channel_type()->may_receive())
12257 this->report_error(_("invalid receive on send-only channel"));
12258 return;
12262 // Get a tree for a receive expression.
12264 tree
12265 Receive_expression::do_get_tree(Translate_context* context)
12267 Channel_type* channel_type = this->channel_->type()->channel_type();
12268 if (channel_type == NULL)
12270 gcc_assert(this->channel_->type()->is_error_type());
12271 return error_mark_node;
12273 Type* element_type = channel_type->element_type();
12274 tree element_type_tree = element_type->get_tree(context->gogo());
12276 tree channel = this->channel_->get_tree(context);
12277 if (element_type_tree == error_mark_node || channel == error_mark_node)
12278 return error_mark_node;
12280 return Gogo::receive_from_channel(element_type_tree, channel,
12281 this->for_select_, this->location());
12284 // Make a receive expression.
12286 Receive_expression*
12287 Expression::make_receive(Expression* channel, source_location location)
12289 return new Receive_expression(channel, location);
12292 // Class Send_expression.
12294 // Traversal.
12297 Send_expression::do_traverse(Traverse* traverse)
12299 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
12300 return TRAVERSE_EXIT;
12301 return Expression::traverse(&this->val_, traverse);
12304 // Get the type.
12306 Type*
12307 Send_expression::do_type()
12309 return Type::lookup_bool_type();
12312 // Set types.
12314 void
12315 Send_expression::do_determine_type(const Type_context*)
12317 this->channel_->determine_type_no_context();
12319 Type* type = this->channel_->type();
12320 Type_context subcontext;
12321 if (type->channel_type() != NULL)
12322 subcontext.type = type->channel_type()->element_type();
12323 this->val_->determine_type(&subcontext);
12326 // Check types.
12328 void
12329 Send_expression::do_check_types(Gogo*)
12331 Type* type = this->channel_->type();
12332 if (type->is_error_type())
12334 this->set_is_error();
12335 return;
12337 Channel_type* channel_type = type->channel_type();
12338 if (channel_type == NULL)
12340 error_at(this->location(), "left operand of %<<-%> must be channel");
12341 this->set_is_error();
12342 return;
12344 Type* element_type = channel_type->element_type();
12345 if (element_type != NULL
12346 && !Type::are_assignable(element_type, this->val_->type(), NULL))
12348 this->report_error(_("incompatible types in send"));
12349 return;
12351 if (!channel_type->may_send())
12353 this->report_error(_("invalid send on receive-only channel"));
12354 return;
12358 // Get a tree for a send expression.
12360 tree
12361 Send_expression::do_get_tree(Translate_context* context)
12363 tree channel = this->channel_->get_tree(context);
12364 tree val = this->val_->get_tree(context);
12365 if (channel == error_mark_node || val == error_mark_node)
12366 return error_mark_node;
12367 Channel_type* channel_type = this->channel_->type()->channel_type();
12368 val = Expression::convert_for_assignment(context,
12369 channel_type->element_type(),
12370 this->val_->type(),
12371 val,
12372 this->location());
12373 return Gogo::send_on_channel(channel, val, this->is_value_discarded_,
12374 this->for_select_, this->location());
12377 // Make a send expression
12379 Send_expression*
12380 Expression::make_send(Expression* channel, Expression* val,
12381 source_location location)
12383 return new Send_expression(channel, val, location);
12386 // An expression which evaluates to a pointer to the type descriptor
12387 // of a type.
12389 class Type_descriptor_expression : public Expression
12391 public:
12392 Type_descriptor_expression(Type* type, source_location location)
12393 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
12394 type_(type)
12397 protected:
12398 Type*
12399 do_type()
12400 { return Type::make_type_descriptor_ptr_type(); }
12402 void
12403 do_determine_type(const Type_context*)
12406 Expression*
12407 do_copy()
12408 { return this; }
12410 tree
12411 do_get_tree(Translate_context* context)
12412 { return this->type_->type_descriptor_pointer(context->gogo()); }
12414 private:
12415 // The type for which this is the descriptor.
12416 Type* type_;
12419 // Make a type descriptor expression.
12421 Expression*
12422 Expression::make_type_descriptor(Type* type, source_location location)
12424 return new Type_descriptor_expression(type, location);
12427 // An expression which evaluates to some characteristic of a type.
12428 // This is only used to initialize fields of a type descriptor. Using
12429 // a new expression class is slightly inefficient but gives us a good
12430 // separation between the frontend and the middle-end with regard to
12431 // how types are laid out.
12433 class Type_info_expression : public Expression
12435 public:
12436 Type_info_expression(Type* type, Type_info type_info)
12437 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
12438 type_(type), type_info_(type_info)
12441 protected:
12442 Type*
12443 do_type();
12445 void
12446 do_determine_type(const Type_context*)
12449 Expression*
12450 do_copy()
12451 { return this; }
12453 tree
12454 do_get_tree(Translate_context* context);
12456 private:
12457 // The type for which we are getting information.
12458 Type* type_;
12459 // What information we want.
12460 Type_info type_info_;
12463 // The type is chosen to match what the type descriptor struct
12464 // expects.
12466 Type*
12467 Type_info_expression::do_type()
12469 switch (this->type_info_)
12471 case TYPE_INFO_SIZE:
12472 return Type::lookup_integer_type("uintptr");
12473 case TYPE_INFO_ALIGNMENT:
12474 case TYPE_INFO_FIELD_ALIGNMENT:
12475 return Type::lookup_integer_type("uint8");
12476 default:
12477 gcc_unreachable();
12481 // Return type information in GENERIC.
12483 tree
12484 Type_info_expression::do_get_tree(Translate_context* context)
12486 tree type_tree = this->type_->get_tree(context->gogo());
12487 if (type_tree == error_mark_node)
12488 return error_mark_node;
12490 tree val_type_tree = this->type()->get_tree(context->gogo());
12491 gcc_assert(val_type_tree != error_mark_node);
12493 if (this->type_info_ == TYPE_INFO_SIZE)
12494 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12495 TYPE_SIZE_UNIT(type_tree));
12496 else
12498 unsigned int val;
12499 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12500 val = go_type_alignment(type_tree);
12501 else
12502 val = go_field_alignment(type_tree);
12503 return build_int_cstu(val_type_tree, val);
12507 // Make a type info expression.
12509 Expression*
12510 Expression::make_type_info(Type* type, Type_info type_info)
12512 return new Type_info_expression(type, type_info);
12515 // An expression which evaluates to the offset of a field within a
12516 // struct. This, like Type_info_expression, q.v., is only used to
12517 // initialize fields of a type descriptor.
12519 class Struct_field_offset_expression : public Expression
12521 public:
12522 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12523 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12524 type_(type), field_(field)
12527 protected:
12528 Type*
12529 do_type()
12530 { return Type::lookup_integer_type("uintptr"); }
12532 void
12533 do_determine_type(const Type_context*)
12536 Expression*
12537 do_copy()
12538 { return this; }
12540 tree
12541 do_get_tree(Translate_context* context);
12543 private:
12544 // The type of the struct.
12545 Struct_type* type_;
12546 // The field.
12547 const Struct_field* field_;
12550 // Return a struct field offset in GENERIC.
12552 tree
12553 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12555 tree type_tree = this->type_->get_tree(context->gogo());
12556 if (type_tree == error_mark_node)
12557 return error_mark_node;
12559 tree val_type_tree = this->type()->get_tree(context->gogo());
12560 gcc_assert(val_type_tree != error_mark_node);
12562 const Struct_field_list* fields = this->type_->fields();
12563 tree struct_field_tree = TYPE_FIELDS(type_tree);
12564 Struct_field_list::const_iterator p;
12565 for (p = fields->begin();
12566 p != fields->end();
12567 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12569 gcc_assert(struct_field_tree != NULL_TREE);
12570 if (&*p == this->field_)
12571 break;
12573 gcc_assert(&*p == this->field_);
12575 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12576 byte_position(struct_field_tree));
12579 // Make an expression for a struct field offset.
12581 Expression*
12582 Expression::make_struct_field_offset(Struct_type* type,
12583 const Struct_field* field)
12585 return new Struct_field_offset_expression(type, field);
12588 // An expression which evaluates to the address of an unnamed label.
12590 class Label_addr_expression : public Expression
12592 public:
12593 Label_addr_expression(Label* label, source_location location)
12594 : Expression(EXPRESSION_LABEL_ADDR, location),
12595 label_(label)
12598 protected:
12599 Type*
12600 do_type()
12601 { return Type::make_pointer_type(Type::make_void_type()); }
12603 void
12604 do_determine_type(const Type_context*)
12607 Expression*
12608 do_copy()
12609 { return new Label_addr_expression(this->label_, this->location()); }
12611 tree
12612 do_get_tree(Translate_context*)
12613 { return this->label_->get_addr(this->location()); }
12615 private:
12616 // The label whose address we are taking.
12617 Label* label_;
12620 // Make an expression for the address of an unnamed label.
12622 Expression*
12623 Expression::make_label_addr(Label* label, source_location location)
12625 return new Label_addr_expression(label, location);
12628 // Import an expression. This comes at the end in order to see the
12629 // various class definitions.
12631 Expression*
12632 Expression::import_expression(Import* imp)
12634 int c = imp->peek_char();
12635 if (imp->match_c_string("- ")
12636 || imp->match_c_string("! ")
12637 || imp->match_c_string("^ "))
12638 return Unary_expression::do_import(imp);
12639 else if (c == '(')
12640 return Binary_expression::do_import(imp);
12641 else if (imp->match_c_string("true")
12642 || imp->match_c_string("false"))
12643 return Boolean_expression::do_import(imp);
12644 else if (c == '"')
12645 return String_expression::do_import(imp);
12646 else if (c == '-' || (c >= '0' && c <= '9'))
12648 // This handles integers, floats and complex constants.
12649 return Integer_expression::do_import(imp);
12651 else if (imp->match_c_string("nil"))
12652 return Nil_expression::do_import(imp);
12653 else if (imp->match_c_string("convert"))
12654 return Type_conversion_expression::do_import(imp);
12655 else
12657 error_at(imp->location(), "import error: expected expression");
12658 return Expression::make_error(imp->location());
12662 // Class Expression_list.
12664 // Traverse the list.
12667 Expression_list::traverse(Traverse* traverse)
12669 for (Expression_list::iterator p = this->begin();
12670 p != this->end();
12671 ++p)
12673 if (*p != NULL)
12675 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12676 return TRAVERSE_EXIT;
12679 return TRAVERSE_CONTINUE;
12682 // Copy the list.
12684 Expression_list*
12685 Expression_list::copy()
12687 Expression_list* ret = new Expression_list();
12688 for (Expression_list::iterator p = this->begin();
12689 p != this->end();
12690 ++p)
12692 if (*p == NULL)
12693 ret->push_back(NULL);
12694 else
12695 ret->push_back((*p)->copy());
12697 return ret;
12700 // Return whether an expression list has an error expression.
12702 bool
12703 Expression_list::contains_error() const
12705 for (Expression_list::const_iterator p = this->begin();
12706 p != this->end();
12707 ++p)
12708 if (*p != NULL && (*p)->is_error_expression())
12709 return true;
12710 return false;