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.
11 #ifndef ENABLE_BUILD_WITH_CXX
20 #include "tree-iterator.h"
25 #ifndef ENABLE_BUILD_WITH_CXX
34 #include "statements.h"
36 #include "expressions.h"
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.
53 Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
57 return this->do_integer_constant_value(iota_is_constant, val, ptype);
60 // If this expression has a constant floating point value, return it.
63 Expression::float_constant_value(mpfr_t val, Type** ptype) const
66 if (this->do_float_constant_value(val, ptype))
72 if (!this->do_integer_constant_value(false, ival, &t))
76 mpfr_set_z(val, ival, GMP_RNDN);
83 // If this expression has a constant complex value, return it.
86 Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
90 if (this->do_complex_constant_value(real, imag, ptype))
93 if (this->float_constant_value(real, &t))
95 mpfr_set_ui(imag, 0, GMP_RNDN);
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.
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.
148 Expression::do_export(Export*) const
153 // Warn that the value of the expression is not used.
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.
165 Expression::set_is_error()
167 this->classification_ = EXPRESSION_ERROR;
170 // For children to call to report an error conveniently.
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
183 Expression::determine_type(const Type_context* context)
185 this->do_determine_type(context);
188 // Set types when there is no context.
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
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)
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.
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,
235 return Expression::convert_interface_to_interface(context, lhs_type,
239 else if (rhs_type->interface_type() != NULL)
240 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
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)),
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)),
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)),
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;
276 else if (rhs_type->is_nil_type())
278 // The left hand side should be a pointer type at the tree
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.
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
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,
305 gcc_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
310 // Return a tree for a conversion from a non-interface type to an
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
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;
342 first_field_value = rhs_type->type_descriptor_pointer(gogo);
345 // Build the interface method table for this interface and this
346 // object type: a list of function pointers for each interface
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();
356 if (rhs_named_type == NULL)
357 method_table = null_pointer_node;
360 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
362 first_field_value = fold_convert_loc(location, const_ptr_type_node,
366 // Start building a constructor for the value we will return.
368 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
370 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
371 tree field = TYPE_FIELDS(lhs_type_tree);
372 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
373 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
375 elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value);
377 elt = VEC_quick_push(constructor_elt, init, NULL);
378 field = DECL_CHAIN(field);
379 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
382 if (rhs_type->points_to() != NULL)
384 // We are assigning a pointer to the interface; the interface
385 // holds the pointer itself.
386 elt->value = rhs_tree;
387 return build_constructor(lhs_type_tree, init);
390 // We are assigning a non-pointer value to the interface; the
391 // interface gets a copy of the value in the heap.
393 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
395 tree space = gogo->allocate_memory(rhs_type, object_size, location);
396 space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)),
398 space = save_expr(space);
400 tree ref = build_fold_indirect_ref_loc(location, space);
401 TREE_THIS_NOTRAP(ref) = 1;
402 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
405 elt->value = fold_convert_loc(location, TREE_TYPE(field), space);
407 return build2(COMPOUND_EXPR, lhs_type_tree, set,
408 build_constructor(lhs_type_tree, init));
411 // Return a tree for the type descriptor of RHS_TREE, which has
412 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
416 Expression::get_interface_type_descriptor(Translate_context*,
417 Type* rhs_type, tree rhs_tree,
418 source_location location)
420 tree rhs_type_tree = TREE_TYPE(rhs_tree);
421 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
422 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
423 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
425 if (rhs_type->interface_type()->is_empty())
427 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
428 "__type_descriptor") == 0);
432 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
434 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v)));
436 tree v1 = build_fold_indirect_ref_loc(location, v);
437 gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
438 tree f = TYPE_FIELDS(TREE_TYPE(v1));
439 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
441 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
443 tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v,
444 fold_convert_loc(location, TREE_TYPE(v),
446 tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node);
447 return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1),
451 // Return a tree for the conversion of an interface type to an
455 Expression::convert_interface_to_interface(Translate_context* context,
456 Type *lhs_type, Type *rhs_type,
457 tree rhs_tree, bool for_type_guard,
458 source_location location)
460 Gogo* gogo = context->gogo();
461 Interface_type* lhs_interface_type = lhs_type->interface_type();
462 bool lhs_is_empty = lhs_interface_type->is_empty();
464 tree lhs_type_tree = lhs_type->get_tree(gogo);
465 if (lhs_type_tree == error_mark_node)
466 return error_mark_node;
468 // In the general case this requires runtime examination of the type
469 // method table to match it up with the interface methods.
471 // FIXME: If all of the methods in the right hand side interface
472 // also appear in the left hand side interface, then we don't need
473 // to do a runtime check, although we still need to build a new
476 // Get the type descriptor for the right hand side. This will be
477 // NULL for a nil interface.
479 if (!DECL_P(rhs_tree))
480 rhs_tree = save_expr(rhs_tree);
482 tree rhs_type_descriptor =
483 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
486 // The result is going to be a two element constructor.
488 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
490 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
491 tree field = TYPE_FIELDS(lhs_type_tree);
496 // A type assertion fails when converting a nil interface.
497 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
498 static tree assert_interface_decl;
499 tree call = Gogo::call_builtin(&assert_interface_decl,
501 "__go_assert_interface",
504 TREE_TYPE(lhs_type_descriptor),
506 TREE_TYPE(rhs_type_descriptor),
507 rhs_type_descriptor);
508 // This will panic if the interface conversion fails.
509 TREE_NOTHROW(assert_interface_decl) = 0;
510 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
512 else if (lhs_is_empty)
514 // A convertion to an empty interface always succeeds, and the
515 // first field is just the type descriptor of the object.
516 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
517 "__type_descriptor") == 0);
518 gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
519 elt->value = rhs_type_descriptor;
523 // A conversion to a non-empty interface may fail, but unlike a
524 // type assertion converting nil will always succeed.
525 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
527 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
528 static tree convert_interface_decl;
529 tree call = Gogo::call_builtin(&convert_interface_decl,
531 "__go_convert_interface",
534 TREE_TYPE(lhs_type_descriptor),
536 TREE_TYPE(rhs_type_descriptor),
537 rhs_type_descriptor);
538 // This will panic if the interface conversion fails.
539 TREE_NOTHROW(convert_interface_decl) = 0;
540 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
543 // The second field is simply the object pointer.
545 elt = VEC_quick_push(constructor_elt, init, NULL);
546 field = DECL_CHAIN(field);
547 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
550 tree rhs_type_tree = TREE_TYPE(rhs_tree);
551 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
552 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
553 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
554 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
557 return build_constructor(lhs_type_tree, init);
560 // Return a tree for the conversion of an interface type to a
561 // non-interface type.
564 Expression::convert_interface_to_type(Translate_context* context,
565 Type *lhs_type, Type* rhs_type,
566 tree rhs_tree, source_location location)
568 Gogo* gogo = context->gogo();
569 tree rhs_type_tree = TREE_TYPE(rhs_tree);
571 tree lhs_type_tree = lhs_type->get_tree(gogo);
572 if (lhs_type_tree == error_mark_node)
573 return error_mark_node;
575 // Call a function to check that the type is valid. The function
576 // will panic with an appropriate runtime type error if the type is
579 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
581 if (!DECL_P(rhs_tree))
582 rhs_tree = save_expr(rhs_tree);
584 tree rhs_type_descriptor =
585 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
588 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo);
590 static tree check_interface_type_decl;
591 tree call = Gogo::call_builtin(&check_interface_type_decl,
593 "__go_check_interface_type",
596 TREE_TYPE(lhs_type_descriptor),
598 TREE_TYPE(rhs_type_descriptor),
600 TREE_TYPE(rhs_inter_descriptor),
601 rhs_inter_descriptor);
602 // This call will panic if the conversion is invalid.
603 TREE_NOTHROW(check_interface_type_decl) = 0;
605 // If the call succeeds, pull out the value.
606 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
607 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
608 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
609 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
612 // If the value is a pointer, then it is the value we want.
613 // Otherwise it points to the value.
614 if (lhs_type->points_to() == NULL)
616 val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val);
617 val = build_fold_indirect_ref_loc(location, val);
620 return build2(COMPOUND_EXPR, lhs_type_tree, call,
621 fold_convert_loc(location, lhs_type_tree, val));
624 // Convert an expression to a tree. This is implemented by the child
625 // class. Not that it is not in general safe to call this multiple
626 // times for a single expression, but that we don't catch such errors.
629 Expression::get_tree(Translate_context* context)
631 // The child may have marked this expression as having an error.
632 if (this->classification_ == EXPRESSION_ERROR)
633 return error_mark_node;
635 return this->do_get_tree(context);
638 // Return a tree for VAL in TYPE.
641 Expression::integer_constant_tree(mpz_t val, tree type)
643 if (type == error_mark_node)
644 return error_mark_node;
645 else if (TREE_CODE(type) == INTEGER_TYPE)
646 return double_int_to_tree(type,
647 mpz_get_double_int(type, val, true));
648 else if (TREE_CODE(type) == REAL_TYPE)
651 mpfr_init_set_z(fval, val, GMP_RNDN);
652 tree ret = Expression::float_constant_tree(fval, type);
656 else if (TREE_CODE(type) == COMPLEX_TYPE)
659 mpfr_init_set_z(fval, val, GMP_RNDN);
660 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
662 tree imag = build_real_from_int_cst(TREE_TYPE(type),
664 return build_complex(type, real, imag);
670 // Return a tree for VAL in TYPE.
673 Expression::float_constant_tree(mpfr_t val, tree type)
675 if (type == error_mark_node)
676 return error_mark_node;
677 else if (TREE_CODE(type) == INTEGER_TYPE)
681 mpfr_get_z(ival, val, GMP_RNDN);
682 tree ret = Expression::integer_constant_tree(ival, type);
686 else if (TREE_CODE(type) == REAL_TYPE)
689 real_from_mpfr(&r1, val, type, GMP_RNDN);
691 real_convert(&r2, TYPE_MODE(type), &r1);
692 return build_real(type, r2);
694 else if (TREE_CODE(type) == COMPLEX_TYPE)
697 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
699 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
700 tree imag = build_real_from_int_cst(TREE_TYPE(type),
702 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
708 // Return a tree for REAL/IMAG in TYPE.
711 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
713 if (TREE_CODE(type) == COMPLEX_TYPE)
716 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
718 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
721 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
723 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
725 return build_complex(type, build_real(TREE_TYPE(type), r2),
726 build_real(TREE_TYPE(type), r4));
732 // Return a tree which evaluates to true if VAL, of arbitrary integer
733 // type, is negative or is more than the maximum value of BOUND_TYPE.
734 // If SOFAR is not NULL, it is or'red into the result. The return
735 // value may be NULL if SOFAR is NULL.
738 Expression::check_bounds(tree val, tree bound_type, tree sofar,
741 tree val_type = TREE_TYPE(val);
742 tree ret = NULL_TREE;
744 if (!TYPE_UNSIGNED(val_type))
746 ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val,
747 build_int_cst(val_type, 0));
748 if (ret == boolean_false_node)
752 if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type))
753 || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type))
755 tree max = TYPE_MAX_VALUE(bound_type);
756 tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val,
757 fold_convert_loc(loc, val_type, max));
758 if (big == boolean_false_node)
760 else if (ret == NULL_TREE)
763 ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
767 if (ret == NULL_TREE)
769 else if (sofar == NULL_TREE)
772 return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
776 // Error expressions. This are used to avoid cascading errors.
778 class Error_expression : public Expression
781 Error_expression(source_location location)
782 : Expression(EXPRESSION_ERROR, location)
787 do_is_constant() const
791 do_integer_constant_value(bool, mpz_t val, Type**) const
798 do_float_constant_value(mpfr_t val, Type**) const
800 mpfr_set_ui(val, 0, GMP_RNDN);
805 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
807 mpfr_set_ui(real, 0, GMP_RNDN);
808 mpfr_set_ui(imag, 0, GMP_RNDN);
813 do_discarding_value()
818 { return Type::make_error_type(); }
821 do_determine_type(const Type_context*)
829 do_is_addressable() const
833 do_get_tree(Translate_context*)
834 { return error_mark_node; }
838 Expression::make_error(source_location location)
840 return new Error_expression(location);
843 // An expression which is really a type. This is used during parsing.
844 // It is an error if these survive after lowering.
847 Type_expression : public Expression
850 Type_expression(Type* type, source_location location)
851 : Expression(EXPRESSION_TYPE, location),
857 do_traverse(Traverse* traverse)
858 { return Type::traverse(this->type_, traverse); }
862 { return this->type_; }
865 do_determine_type(const Type_context*)
869 do_check_types(Gogo*)
870 { this->report_error(_("invalid use of type")); }
877 do_get_tree(Translate_context*)
878 { gcc_unreachable(); }
881 // The type which we are representing as an expression.
886 Expression::make_type(Type* type, source_location location)
888 return new Type_expression(type, location);
891 // Class Var_expression.
893 // Lower a variable expression. Here we just make sure that the
894 // initialization expression of the variable has been lowered. This
895 // ensures that we will be able to determine the type of the variable
899 Var_expression::do_lower(Gogo* gogo, Named_object* function, int)
901 if (this->variable_->is_variable())
903 Variable* var = this->variable_->var_value();
904 // This is either a local variable or a global variable. A
905 // reference to a variable which is local to an enclosing
906 // function will be a reference to a field in a closure.
907 if (var->is_global())
909 var->lower_init_expression(gogo, function);
914 // Return the name of the variable.
917 Var_expression::name() const
919 return this->variable_->name();
922 // Return the type of a reference to a variable.
925 Var_expression::do_type()
927 if (this->variable_->is_variable())
928 return this->variable_->var_value()->type();
929 else if (this->variable_->is_result_variable())
930 return this->variable_->result_var_value()->type();
935 // Something takes the address of this variable. This means that we
936 // may want to move the variable onto the heap.
939 Var_expression::do_address_taken(bool escapes)
943 else if (this->variable_->is_variable())
944 this->variable_->var_value()->set_address_taken();
945 else if (this->variable_->is_result_variable())
946 this->variable_->result_var_value()->set_address_taken();
951 // Get the tree for a reference to a variable.
954 Var_expression::do_get_tree(Translate_context* context)
956 return this->variable_->get_tree(context->gogo(), context->function());
959 // Make a reference to a variable in an expression.
962 Expression::make_var_reference(Named_object* var, source_location location)
965 return Expression::make_sink(location);
967 // FIXME: Creating a new object for each reference to a variable is
969 return new Var_expression(var, location);
972 // Class Temporary_reference_expression.
977 Temporary_reference_expression::do_type()
979 return this->statement_->type();
982 // Called if something takes the address of this temporary variable.
983 // We never have to move temporary variables to the heap, but we do
984 // need to know that they must live in the stack rather than in a
988 Temporary_reference_expression::do_address_taken(bool)
990 this->statement_->set_is_address_taken();
993 // Get a tree referring to the variable.
996 Temporary_reference_expression::do_get_tree(Translate_context*)
998 return this->statement_->get_decl();
1001 // Make a reference to a temporary variable.
1004 Expression::make_temporary_reference(Temporary_statement* statement,
1005 source_location location)
1007 return new Temporary_reference_expression(statement, location);
1010 // A sink expression--a use of the blank identifier _.
1012 class Sink_expression : public Expression
1015 Sink_expression(source_location location)
1016 : Expression(EXPRESSION_SINK, location),
1017 type_(NULL), var_(NULL_TREE)
1022 do_discarding_value()
1029 do_determine_type(const Type_context*);
1033 { return new Sink_expression(this->location()); }
1036 do_get_tree(Translate_context*);
1039 // The type of this sink variable.
1041 // The temporary variable we generate.
1045 // Return the type of a sink expression.
1048 Sink_expression::do_type()
1050 if (this->type_ == NULL)
1051 return Type::make_sink_type();
1055 // Determine the type of a sink expression.
1058 Sink_expression::do_determine_type(const Type_context* context)
1060 if (context->type != NULL)
1061 this->type_ = context->type;
1064 // Return a temporary variable for a sink expression. This will
1065 // presumably be a write-only variable which the middle-end will drop.
1068 Sink_expression::do_get_tree(Translate_context* context)
1070 if (this->var_ == NULL_TREE)
1072 gcc_assert(this->type_ != NULL && !this->type_->is_sink_type());
1073 this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()),
1079 // Make a sink expression.
1082 Expression::make_sink(source_location location)
1084 return new Sink_expression(location);
1087 // Class Func_expression.
1089 // FIXME: Can a function expression appear in a constant expression?
1090 // The value is unchanging. Initializing a constant to the address of
1091 // a function seems like it could work, though there might be little
1094 // Return the name of the function.
1097 Func_expression::name() const
1099 return this->function_->name();
1105 Func_expression::do_traverse(Traverse* traverse)
1107 return (this->closure_ == NULL
1109 : Expression::traverse(&this->closure_, traverse));
1112 // Return the type of a function expression.
1115 Func_expression::do_type()
1117 if (this->function_->is_function())
1118 return this->function_->func_value()->type();
1119 else if (this->function_->is_function_declaration())
1120 return this->function_->func_declaration_value()->type();
1125 // Get the tree for a function expression without evaluating the
1129 Func_expression::get_tree_without_closure(Gogo* gogo)
1131 Function_type* fntype;
1132 if (this->function_->is_function())
1133 fntype = this->function_->func_value()->type();
1134 else if (this->function_->is_function_declaration())
1135 fntype = this->function_->func_declaration_value()->type();
1139 // Builtin functions are handled specially by Call_expression. We
1140 // can't take their address.
1141 if (fntype->is_builtin())
1143 error_at(this->location(), "invalid use of special builtin function %qs",
1144 this->function_->name().c_str());
1145 return error_mark_node;
1148 Named_object* no = this->function_;
1150 tree id = no->get_id(gogo);
1151 if (id == error_mark_node)
1152 return error_mark_node;
1155 if (no->is_function())
1156 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1157 else if (no->is_function_declaration())
1158 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1162 if (fndecl == error_mark_node)
1163 return error_mark_node;
1165 return build_fold_addr_expr_loc(this->location(), fndecl);
1168 // Get the tree for a function expression. This is used when we take
1169 // the address of a function rather than simply calling it. If the
1170 // function has a closure, we must use a trampoline.
1173 Func_expression::do_get_tree(Translate_context* context)
1175 Gogo* gogo = context->gogo();
1177 tree fnaddr = this->get_tree_without_closure(gogo);
1178 if (fnaddr == error_mark_node)
1179 return error_mark_node;
1181 gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1182 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1183 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1185 // For a normal non-nested function call, that is all we have to do.
1186 if (!this->function_->is_function()
1187 || this->function_->func_value()->enclosing() == NULL)
1189 gcc_assert(this->closure_ == NULL);
1193 // For a nested function call, we have to always allocate a
1194 // trampoline. If we don't always allocate, then closures will not
1195 // be reliably distinct.
1196 Expression* closure = this->closure_;
1198 if (closure == NULL)
1199 closure_tree = null_pointer_node;
1202 // Get the value of the closure. This will be a pointer to
1203 // space allocated on the heap.
1204 closure_tree = closure->get_tree(context);
1205 if (closure_tree == error_mark_node)
1206 return error_mark_node;
1207 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1210 // Now we need to build some code on the heap. This code will load
1211 // the static chain pointer with the closure and then jump to the
1212 // body of the function. The normal gcc approach is to build the
1213 // code on the stack. Unfortunately we can not do that, as Go
1214 // permits us to return the function pointer.
1216 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1219 // Make a reference to a function in an expression.
1222 Expression::make_func_reference(Named_object* function, Expression* closure,
1223 source_location location)
1225 return new Func_expression(function, closure, location);
1228 // Class Unknown_expression.
1230 // Return the name of an unknown expression.
1233 Unknown_expression::name() const
1235 return this->named_object_->name();
1238 // Lower a reference to an unknown name.
1241 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1243 source_location location = this->location();
1244 Named_object* no = this->named_object_;
1245 Named_object* real = no->unknown_value()->real_named_object();
1248 if (this->is_composite_literal_key_)
1250 error_at(location, "reference to undefined name %qs",
1251 this->named_object_->message_name().c_str());
1252 return Expression::make_error(location);
1254 switch (real->classification())
1256 case Named_object::NAMED_OBJECT_CONST:
1257 return Expression::make_const_reference(real, location);
1258 case Named_object::NAMED_OBJECT_TYPE:
1259 return Expression::make_type(real->type_value(), location);
1260 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1261 if (this->is_composite_literal_key_)
1263 error_at(location, "reference to undefined type %qs",
1264 real->message_name().c_str());
1265 return Expression::make_error(location);
1266 case Named_object::NAMED_OBJECT_VAR:
1267 return Expression::make_var_reference(real, location);
1268 case Named_object::NAMED_OBJECT_FUNC:
1269 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1270 return Expression::make_func_reference(real, NULL, location);
1271 case Named_object::NAMED_OBJECT_PACKAGE:
1272 if (this->is_composite_literal_key_)
1274 error_at(location, "unexpected reference to package");
1275 return Expression::make_error(location);
1281 // Make a reference to an unknown name.
1284 Expression::make_unknown_reference(Named_object* no, source_location location)
1286 gcc_assert(no->resolve()->is_unknown());
1287 return new Unknown_expression(no, location);
1290 // A boolean expression.
1292 class Boolean_expression : public Expression
1295 Boolean_expression(bool val, source_location location)
1296 : Expression(EXPRESSION_BOOLEAN, location),
1297 val_(val), type_(NULL)
1305 do_is_constant() const
1312 do_determine_type(const Type_context*);
1319 do_get_tree(Translate_context*)
1320 { return this->val_ ? boolean_true_node : boolean_false_node; }
1323 do_export(Export* exp) const
1324 { exp->write_c_string(this->val_ ? "true" : "false"); }
1329 // The type as determined by context.
1336 Boolean_expression::do_type()
1338 if (this->type_ == NULL)
1339 this->type_ = Type::make_boolean_type();
1343 // Set the type from the context.
1346 Boolean_expression::do_determine_type(const Type_context* context)
1348 if (this->type_ != NULL && !this->type_->is_abstract())
1350 else if (context->type != NULL && context->type->is_boolean_type())
1351 this->type_ = context->type;
1352 else if (!context->may_be_abstract)
1353 this->type_ = Type::lookup_bool_type();
1356 // Import a boolean constant.
1359 Boolean_expression::do_import(Import* imp)
1361 if (imp->peek_char() == 't')
1363 imp->require_c_string("true");
1364 return Expression::make_boolean(true, imp->location());
1368 imp->require_c_string("false");
1369 return Expression::make_boolean(false, imp->location());
1373 // Make a boolean expression.
1376 Expression::make_boolean(bool val, source_location location)
1378 return new Boolean_expression(val, location);
1381 // Class String_expression.
1386 String_expression::do_type()
1388 if (this->type_ == NULL)
1389 this->type_ = Type::make_string_type();
1393 // Set the type from the context.
1396 String_expression::do_determine_type(const Type_context* context)
1398 if (this->type_ != NULL && !this->type_->is_abstract())
1400 else if (context->type != NULL && context->type->is_string_type())
1401 this->type_ = context->type;
1402 else if (!context->may_be_abstract)
1403 this->type_ = Type::lookup_string_type();
1406 // Build a string constant.
1409 String_expression::do_get_tree(Translate_context* context)
1411 return context->gogo()->go_string_constant_tree(this->val_);
1414 // Export a string expression.
1417 String_expression::do_export(Export* exp) const
1420 s.reserve(this->val_.length() * 4 + 2);
1422 for (std::string::const_iterator p = this->val_.begin();
1423 p != this->val_.end();
1426 if (*p == '\\' || *p == '"')
1431 else if (*p >= 0x20 && *p < 0x7f)
1433 else if (*p == '\n')
1435 else if (*p == '\t')
1440 unsigned char c = *p;
1441 unsigned int dig = c >> 4;
1442 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1444 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1448 exp->write_string(s);
1451 // Import a string expression.
1454 String_expression::do_import(Import* imp)
1456 imp->require_c_string("\"");
1460 int c = imp->get_char();
1461 if (c == '"' || c == -1)
1464 val += static_cast<char>(c);
1467 c = imp->get_char();
1468 if (c == '\\' || c == '"')
1469 val += static_cast<char>(c);
1476 c = imp->get_char();
1477 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1478 c = imp->get_char();
1479 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1480 char v = (vh << 4) | vl;
1485 error_at(imp->location(), "bad string constant");
1486 return Expression::make_error(imp->location());
1490 return Expression::make_string(val, imp->location());
1493 // Make a string expression.
1496 Expression::make_string(const std::string& val, source_location location)
1498 return new String_expression(val, location);
1501 // Make an integer expression.
1503 class Integer_expression : public Expression
1506 Integer_expression(const mpz_t* val, Type* type, source_location location)
1507 : Expression(EXPRESSION_INTEGER, location),
1509 { mpz_init_set(this->val_, *val); }
1514 // Return whether VAL fits in the type.
1516 check_constant(mpz_t val, Type*, source_location);
1518 // Write VAL to export data.
1520 export_integer(Export* exp, const mpz_t val);
1524 do_is_constant() const
1528 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1534 do_determine_type(const Type_context* context);
1537 do_check_types(Gogo*);
1540 do_get_tree(Translate_context*);
1544 { return Expression::make_integer(&this->val_, this->type_,
1545 this->location()); }
1548 do_export(Export*) const;
1551 // The integer value.
1557 // Return an integer constant value.
1560 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1563 if (this->type_ != NULL)
1564 *ptype = this->type_;
1565 mpz_set(val, this->val_);
1569 // Return the current type. If we haven't set the type yet, we return
1570 // an abstract integer type.
1573 Integer_expression::do_type()
1575 if (this->type_ == NULL)
1576 this->type_ = Type::make_abstract_integer_type();
1580 // Set the type of the integer value. Here we may switch from an
1581 // abstract type to a real type.
1584 Integer_expression::do_determine_type(const Type_context* context)
1586 if (this->type_ != NULL && !this->type_->is_abstract())
1588 else if (context->type != NULL
1589 && (context->type->integer_type() != NULL
1590 || context->type->float_type() != NULL
1591 || context->type->complex_type() != NULL))
1592 this->type_ = context->type;
1593 else if (!context->may_be_abstract)
1594 this->type_ = Type::lookup_integer_type("int");
1597 // Return true if the integer VAL fits in the range of the type TYPE.
1598 // Otherwise give an error and return false. TYPE may be NULL.
1601 Integer_expression::check_constant(mpz_t val, Type* type,
1602 source_location location)
1606 Integer_type* itype = type->integer_type();
1607 if (itype == NULL || itype->is_abstract())
1610 int bits = mpz_sizeinbase(val, 2);
1612 if (itype->is_unsigned())
1614 // For an unsigned type we can only accept a nonnegative number,
1615 // and we must be able to represent at least BITS.
1616 if (mpz_sgn(val) >= 0
1617 && bits <= itype->bits())
1622 // For a signed type we need an extra bit to indicate the sign.
1623 // We have to handle the most negative integer specially.
1624 if (bits + 1 <= itype->bits()
1625 || (bits <= itype->bits()
1627 && (mpz_scan1(val, 0)
1628 == static_cast<unsigned long>(itype->bits() - 1))
1629 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1633 error_at(location, "integer constant overflow");
1637 // Check the type of an integer constant.
1640 Integer_expression::do_check_types(Gogo*)
1642 if (this->type_ == NULL)
1644 if (!Integer_expression::check_constant(this->val_, this->type_,
1646 this->set_is_error();
1649 // Get a tree for an integer constant.
1652 Integer_expression::do_get_tree(Translate_context* context)
1654 Gogo* gogo = context->gogo();
1656 if (this->type_ != NULL && !this->type_->is_abstract())
1657 type = this->type_->get_tree(gogo);
1658 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1660 // We are converting to an abstract floating point type.
1661 type = Type::lookup_float_type("float64")->get_tree(gogo);
1663 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1665 // We are converting to an abstract complex type.
1666 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
1670 // If we still have an abstract type here, then this is being
1671 // used in a constant expression which didn't get reduced for
1672 // some reason. Use a type which will fit the value. We use <,
1673 // not <=, because we need an extra bit for the sign bit.
1674 int bits = mpz_sizeinbase(this->val_, 2);
1675 if (bits < INT_TYPE_SIZE)
1676 type = Type::lookup_integer_type("int")->get_tree(gogo);
1678 type = Type::lookup_integer_type("int64")->get_tree(gogo);
1680 type = long_long_integer_type_node;
1682 return Expression::integer_constant_tree(this->val_, type);
1685 // Write VAL to export data.
1688 Integer_expression::export_integer(Export* exp, const mpz_t val)
1690 char* s = mpz_get_str(NULL, 10, val);
1691 exp->write_c_string(s);
1695 // Export an integer in a constant expression.
1698 Integer_expression::do_export(Export* exp) const
1700 Integer_expression::export_integer(exp, this->val_);
1701 // A trailing space lets us reliably identify the end of the number.
1702 exp->write_c_string(" ");
1705 // Import an integer, floating point, or complex value. This handles
1706 // all these types because they all start with digits.
1709 Integer_expression::do_import(Import* imp)
1711 std::string num = imp->read_identifier();
1712 imp->require_c_string(" ");
1713 if (!num.empty() && num[num.length() - 1] == 'i')
1716 size_t plus_pos = num.find('+', 1);
1717 size_t minus_pos = num.find('-', 1);
1719 if (plus_pos == std::string::npos)
1721 else if (minus_pos == std::string::npos)
1725 error_at(imp->location(), "bad number in import data: %qs",
1727 return Expression::make_error(imp->location());
1729 if (pos == std::string::npos)
1730 mpfr_set_ui(real, 0, GMP_RNDN);
1733 std::string real_str = num.substr(0, pos);
1734 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1736 error_at(imp->location(), "bad number in import data: %qs",
1738 return Expression::make_error(imp->location());
1742 std::string imag_str;
1743 if (pos == std::string::npos)
1746 imag_str = num.substr(pos);
1747 imag_str = imag_str.substr(0, imag_str.size() - 1);
1749 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1751 error_at(imp->location(), "bad number in import data: %qs",
1753 return Expression::make_error(imp->location());
1755 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1761 else if (num.find('.') == std::string::npos
1762 && num.find('E') == std::string::npos)
1765 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1767 error_at(imp->location(), "bad number in import data: %qs",
1769 return Expression::make_error(imp->location());
1771 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1778 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1780 error_at(imp->location(), "bad number in import data: %qs",
1782 return Expression::make_error(imp->location());
1784 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1790 // Build a new integer value.
1793 Expression::make_integer(const mpz_t* val, Type* type,
1794 source_location location)
1796 return new Integer_expression(val, type, location);
1801 class Float_expression : public Expression
1804 Float_expression(const mpfr_t* val, Type* type, source_location location)
1805 : Expression(EXPRESSION_FLOAT, location),
1808 mpfr_init_set(this->val_, *val, GMP_RNDN);
1811 // Constrain VAL to fit into TYPE.
1813 constrain_float(mpfr_t val, Type* type);
1815 // Return whether VAL fits in the type.
1817 check_constant(mpfr_t val, Type*, source_location);
1819 // Write VAL to export data.
1821 export_float(Export* exp, const mpfr_t val);
1825 do_is_constant() const
1829 do_float_constant_value(mpfr_t val, Type**) const;
1835 do_determine_type(const Type_context*);
1838 do_check_types(Gogo*);
1842 { return Expression::make_float(&this->val_, this->type_,
1843 this->location()); }
1846 do_get_tree(Translate_context*);
1849 do_export(Export*) const;
1852 // The floating point value.
1858 // Constrain VAL to fit into TYPE.
1861 Float_expression::constrain_float(mpfr_t val, Type* type)
1863 Float_type* ftype = type->float_type();
1864 if (ftype != NULL && !ftype->is_abstract())
1866 tree type_tree = ftype->type_tree();
1867 REAL_VALUE_TYPE rvt;
1868 real_from_mpfr(&rvt, val, type_tree, GMP_RNDN);
1869 real_convert(&rvt, TYPE_MODE(type_tree), &rvt);
1870 mpfr_from_real(val, &rvt, GMP_RNDN);
1874 // Return a floating point constant value.
1877 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1879 if (this->type_ != NULL)
1880 *ptype = this->type_;
1881 mpfr_set(val, this->val_, GMP_RNDN);
1885 // Return the current type. If we haven't set the type yet, we return
1886 // an abstract float type.
1889 Float_expression::do_type()
1891 if (this->type_ == NULL)
1892 this->type_ = Type::make_abstract_float_type();
1896 // Set the type of the float value. Here we may switch from an
1897 // abstract type to a real type.
1900 Float_expression::do_determine_type(const Type_context* context)
1902 if (this->type_ != NULL && !this->type_->is_abstract())
1904 else if (context->type != NULL
1905 && (context->type->integer_type() != NULL
1906 || context->type->float_type() != NULL
1907 || context->type->complex_type() != NULL))
1908 this->type_ = context->type;
1909 else if (!context->may_be_abstract)
1910 this->type_ = Type::lookup_float_type("float");
1913 // Return true if the floating point value VAL fits in the range of
1914 // the type TYPE. Otherwise give an error and return false. TYPE may
1918 Float_expression::check_constant(mpfr_t val, Type* type,
1919 source_location location)
1923 Float_type* ftype = type->float_type();
1924 if (ftype == NULL || ftype->is_abstract())
1927 // A NaN or Infinity always fits in the range of the type.
1928 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1931 mp_exp_t exp = mpfr_get_exp(val);
1933 switch (ftype->bits())
1946 error_at(location, "floating point constant overflow");
1952 // Check the type of a float value.
1955 Float_expression::do_check_types(Gogo*)
1957 if (this->type_ == NULL)
1960 if (!Float_expression::check_constant(this->val_, this->type_,
1962 this->set_is_error();
1964 Integer_type* integer_type = this->type_->integer_type();
1965 if (integer_type != NULL)
1967 if (!mpfr_integer_p(this->val_))
1968 this->report_error(_("floating point constant truncated to integer"));
1971 gcc_assert(!integer_type->is_abstract());
1974 mpfr_get_z(ival, this->val_, GMP_RNDN);
1975 Integer_expression::check_constant(ival, integer_type,
1982 // Get a tree for a float constant.
1985 Float_expression::do_get_tree(Translate_context* context)
1987 Gogo* gogo = context->gogo();
1989 if (this->type_ != NULL && !this->type_->is_abstract())
1990 type = this->type_->get_tree(gogo);
1991 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
1993 // We have an abstract integer type. We just hope for the best.
1994 type = Type::lookup_integer_type("int")->get_tree(gogo);
1998 // If we still have an abstract type here, then this is being
1999 // used in a constant expression which didn't get reduced. We
2000 // just use float64 and hope for the best.
2001 type = Type::lookup_float_type("float64")->get_tree(gogo);
2003 return Expression::float_constant_tree(this->val_, type);
2006 // Write a floating point number to export data.
2009 Float_expression::export_float(Export *exp, const mpfr_t val)
2012 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2014 exp->write_c_string("-");
2015 exp->write_c_string("0.");
2016 exp->write_c_string(*s == '-' ? s + 1 : s);
2019 snprintf(buf, sizeof buf, "E%ld", exponent);
2020 exp->write_c_string(buf);
2023 // Export a floating point number in a constant expression.
2026 Float_expression::do_export(Export* exp) const
2028 Float_expression::export_float(exp, this->val_);
2029 // A trailing space lets us reliably identify the end of the number.
2030 exp->write_c_string(" ");
2033 // Make a float expression.
2036 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2038 return new Float_expression(val, type, location);
2043 class Complex_expression : public Expression
2046 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2047 source_location location)
2048 : Expression(EXPRESSION_COMPLEX, location),
2051 mpfr_init_set(this->real_, *real, GMP_RNDN);
2052 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2055 // Constrain REAL/IMAG to fit into TYPE.
2057 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2059 // Return whether REAL/IMAG fits in the type.
2061 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2063 // Write REAL/IMAG to export data.
2065 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2069 do_is_constant() const
2073 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2079 do_determine_type(const Type_context*);
2082 do_check_types(Gogo*);
2087 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2092 do_get_tree(Translate_context*);
2095 do_export(Export*) const;
2100 // The imaginary part;
2102 // The type if known.
2106 // Constrain REAL/IMAG to fit into TYPE.
2109 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2111 Complex_type* ctype = type->complex_type();
2112 if (ctype != NULL && !ctype->is_abstract())
2114 tree type_tree = ctype->type_tree();
2116 REAL_VALUE_TYPE rvt;
2117 real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN);
2118 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2119 mpfr_from_real(real, &rvt, GMP_RNDN);
2121 real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN);
2122 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2123 mpfr_from_real(imag, &rvt, GMP_RNDN);
2127 // Return a complex constant value.
2130 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2133 if (this->type_ != NULL)
2134 *ptype = this->type_;
2135 mpfr_set(real, this->real_, GMP_RNDN);
2136 mpfr_set(imag, this->imag_, GMP_RNDN);
2140 // Return the current type. If we haven't set the type yet, we return
2141 // an abstract complex type.
2144 Complex_expression::do_type()
2146 if (this->type_ == NULL)
2147 this->type_ = Type::make_abstract_complex_type();
2151 // Set the type of the complex value. Here we may switch from an
2152 // abstract type to a real type.
2155 Complex_expression::do_determine_type(const Type_context* context)
2157 if (this->type_ != NULL && !this->type_->is_abstract())
2159 else if (context->type != NULL
2160 && context->type->complex_type() != NULL)
2161 this->type_ = context->type;
2162 else if (!context->may_be_abstract)
2163 this->type_ = Type::lookup_complex_type("complex");
2166 // Return true if the complex value REAL/IMAG fits in the range of the
2167 // type TYPE. Otherwise give an error and return false. TYPE may be
2171 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2172 source_location location)
2176 Complex_type* ctype = type->complex_type();
2177 if (ctype == NULL || ctype->is_abstract())
2181 switch (ctype->bits())
2193 // A NaN or Infinity always fits in the range of the type.
2194 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2196 if (mpfr_get_exp(real) > max_exp)
2198 error_at(location, "complex real part constant overflow");
2203 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2205 if (mpfr_get_exp(imag) > max_exp)
2207 error_at(location, "complex imaginary part constant overflow");
2215 // Check the type of a complex value.
2218 Complex_expression::do_check_types(Gogo*)
2220 if (this->type_ == NULL)
2223 if (!Complex_expression::check_constant(this->real_, this->imag_,
2224 this->type_, this->location()))
2225 this->set_is_error();
2228 // Get a tree for a complex constant.
2231 Complex_expression::do_get_tree(Translate_context* context)
2233 Gogo* gogo = context->gogo();
2235 if (this->type_ != NULL && !this->type_->is_abstract())
2236 type = this->type_->get_tree(gogo);
2239 // If we still have an abstract type here, this this is being
2240 // used in a constant expression which didn't get reduced. We
2241 // just use complex128 and hope for the best.
2242 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
2244 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2247 // Write REAL/IMAG to export data.
2250 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2253 if (!mpfr_zero_p(real))
2255 Float_expression::export_float(exp, real);
2256 if (mpfr_sgn(imag) > 0)
2257 exp->write_c_string("+");
2259 Float_expression::export_float(exp, imag);
2260 exp->write_c_string("i");
2263 // Export a complex number in a constant expression.
2266 Complex_expression::do_export(Export* exp) const
2268 Complex_expression::export_complex(exp, this->real_, this->imag_);
2269 // A trailing space lets us reliably identify the end of the number.
2270 exp->write_c_string(" ");
2273 // Make a complex expression.
2276 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2277 source_location location)
2279 return new Complex_expression(real, imag, type, location);
2282 // A reference to a const in an expression.
2284 class Const_expression : public Expression
2287 Const_expression(Named_object* constant, source_location location)
2288 : Expression(EXPRESSION_CONST_REFERENCE, location),
2289 constant_(constant), type_(NULL)
2294 { return this->constant_->name(); }
2298 do_lower(Gogo*, Named_object*, int);
2301 do_is_constant() const
2305 do_integer_constant_value(bool, mpz_t val, Type**) const;
2308 do_float_constant_value(mpfr_t val, Type**) const;
2311 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2314 do_string_constant_value(std::string* val) const
2315 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2320 // The type of a const is set by the declaration, not the use.
2322 do_determine_type(const Type_context*);
2325 do_check_types(Gogo*);
2332 do_get_tree(Translate_context* context);
2334 // When exporting a reference to a const as part of a const
2335 // expression, we export the value. We ignore the fact that it has
2338 do_export(Export* exp) const
2339 { this->constant_->const_value()->expr()->export_expression(exp); }
2343 Named_object* constant_;
2344 // The type of this reference. This is used if the constant has an
2349 // Lower a constant expression. This is where we convert the
2350 // predeclared constant iota into an integer value.
2353 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2355 if (this->constant_->const_value()->expr()->classification()
2358 if (iota_value == -1)
2360 error_at(this->location(),
2361 "iota is only defined in const declarations");
2365 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2366 Expression* ret = Expression::make_integer(&val, NULL,
2372 // Make sure that the constant itself has been lowered.
2373 gogo->lower_constant(this->constant_);
2378 // Return an integer constant value.
2381 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2385 if (this->type_ != NULL)
2386 ctype = this->type_;
2388 ctype = this->constant_->const_value()->type();
2389 if (ctype != NULL && ctype->integer_type() == NULL)
2392 Expression* e = this->constant_->const_value()->expr();
2394 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2398 && !Integer_expression::check_constant(val, ctype, this->location()))
2401 *ptype = ctype != NULL ? ctype : t;
2405 // Return a floating point constant value.
2408 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2411 if (this->type_ != NULL)
2412 ctype = this->type_;
2414 ctype = this->constant_->const_value()->type();
2415 if (ctype != NULL && ctype->float_type() == NULL)
2419 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2421 if (r && ctype != NULL)
2423 if (!Float_expression::check_constant(val, ctype, this->location()))
2425 Float_expression::constrain_float(val, ctype);
2427 *ptype = ctype != NULL ? ctype : t;
2431 // Return a complex constant value.
2434 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2438 if (this->type_ != NULL)
2439 ctype = this->type_;
2441 ctype = this->constant_->const_value()->type();
2442 if (ctype != NULL && ctype->complex_type() == NULL)
2446 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2449 if (r && ctype != NULL)
2451 if (!Complex_expression::check_constant(real, imag, ctype,
2454 Complex_expression::constrain_complex(real, imag, ctype);
2456 *ptype = ctype != NULL ? ctype : t;
2460 // Return the type of the const reference.
2463 Const_expression::do_type()
2465 if (this->type_ != NULL)
2467 Named_constant* nc = this->constant_->const_value();
2468 Type* ret = nc->type();
2471 // During parsing, a named constant may have a NULL type, but we
2472 // must not return a NULL type here.
2473 return nc->expr()->type();
2476 // Set the type of the const reference.
2479 Const_expression::do_determine_type(const Type_context* context)
2481 Type* ctype = this->constant_->const_value()->type();
2482 Type* cetype = (ctype != NULL
2484 : this->constant_->const_value()->expr()->type());
2485 if (ctype != NULL && !ctype->is_abstract())
2487 else if (context->type != NULL
2488 && (context->type->integer_type() != NULL
2489 || context->type->float_type() != NULL
2490 || context->type->complex_type() != NULL)
2491 && (cetype->integer_type() != NULL
2492 || cetype->float_type() != NULL
2493 || cetype->complex_type() != NULL))
2494 this->type_ = context->type;
2495 else if (context->type != NULL
2496 && context->type->is_string_type()
2497 && cetype->is_string_type())
2498 this->type_ = context->type;
2499 else if (context->type != NULL
2500 && context->type->is_boolean_type()
2501 && cetype->is_boolean_type())
2502 this->type_ = context->type;
2503 else if (!context->may_be_abstract)
2505 if (cetype->is_abstract())
2506 cetype = cetype->make_non_abstract_type();
2507 this->type_ = cetype;
2511 // Check types of a const reference.
2514 Const_expression::do_check_types(Gogo*)
2516 if (this->type_ == NULL || this->type_->is_abstract())
2519 // Check for integer overflow.
2520 if (this->type_->integer_type() != NULL)
2525 if (!this->integer_constant_value(true, ival, &dummy))
2529 Expression* cexpr = this->constant_->const_value()->expr();
2530 if (cexpr->float_constant_value(fval, &dummy))
2532 if (!mpfr_integer_p(fval))
2533 this->report_error(_("floating point constant "
2534 "truncated to integer"));
2537 mpfr_get_z(ival, fval, GMP_RNDN);
2538 Integer_expression::check_constant(ival, this->type_,
2548 // Return a tree for the const reference.
2551 Const_expression::do_get_tree(Translate_context* context)
2553 Gogo* gogo = context->gogo();
2555 if (this->type_ == NULL)
2556 type_tree = NULL_TREE;
2559 type_tree = this->type_->get_tree(gogo);
2560 if (type_tree == error_mark_node)
2561 return error_mark_node;
2564 // If the type has been set for this expression, but the underlying
2565 // object is an abstract int or float, we try to get the abstract
2566 // value. Otherwise we may lose something in the conversion.
2567 if (this->type_ != NULL
2568 && this->constant_->const_value()->type()->is_abstract())
2570 Expression* expr = this->constant_->const_value()->expr();
2574 if (expr->integer_constant_value(true, ival, &t))
2576 tree ret = Expression::integer_constant_tree(ival, type_tree);
2584 if (expr->float_constant_value(fval, &t))
2586 tree ret = Expression::float_constant_tree(fval, type_tree);
2593 if (expr->complex_constant_value(fval, imag, &t))
2595 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2604 tree const_tree = this->constant_->get_tree(gogo, context->function());
2605 if (this->type_ == NULL
2606 || const_tree == error_mark_node
2607 || TREE_TYPE(const_tree) == error_mark_node)
2611 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2612 ret = fold_convert(type_tree, const_tree);
2613 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2614 ret = fold(convert_to_integer(type_tree, const_tree));
2615 else if (TREE_CODE(type_tree) == REAL_TYPE)
2616 ret = fold(convert_to_real(type_tree, const_tree));
2617 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2618 ret = fold(convert_to_complex(type_tree, const_tree));
2624 // Make a reference to a constant in an expression.
2627 Expression::make_const_reference(Named_object* constant,
2628 source_location location)
2630 return new Const_expression(constant, location);
2635 class Nil_expression : public Expression
2638 Nil_expression(source_location location)
2639 : Expression(EXPRESSION_NIL, location)
2647 do_is_constant() const
2652 { return Type::make_nil_type(); }
2655 do_determine_type(const Type_context*)
2663 do_get_tree(Translate_context*)
2664 { return null_pointer_node; }
2667 do_export(Export* exp) const
2668 { exp->write_c_string("nil"); }
2671 // Import a nil expression.
2674 Nil_expression::do_import(Import* imp)
2676 imp->require_c_string("nil");
2677 return Expression::make_nil(imp->location());
2680 // Make a nil expression.
2683 Expression::make_nil(source_location location)
2685 return new Nil_expression(location);
2688 // The value of the predeclared constant iota. This is little more
2689 // than a marker. This will be lowered to an integer in
2690 // Const_expression::do_lower, which is where we know the value that
2693 class Iota_expression : public Parser_expression
2696 Iota_expression(source_location location)
2697 : Parser_expression(EXPRESSION_IOTA, location)
2702 do_lower(Gogo*, Named_object*, int)
2703 { gcc_unreachable(); }
2705 // There should only ever be one of these.
2708 { gcc_unreachable(); }
2711 // Make an iota expression. This is only called for one case: the
2712 // value of the predeclared constant iota.
2715 Expression::make_iota()
2717 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2718 return &iota_expression;
2721 // A type conversion expression.
2723 class Type_conversion_expression : public Expression
2726 Type_conversion_expression(Type* type, Expression* expr,
2727 source_location location)
2728 : Expression(EXPRESSION_CONVERSION, location),
2729 type_(type), expr_(expr), may_convert_function_types_(false)
2732 // Return the type to which we are converting.
2735 { return this->type_; }
2737 // Return the expression which we are converting.
2740 { return this->expr_; }
2742 // Permit converting from one function type to another. This is
2743 // used internally for method expressions.
2745 set_may_convert_function_types()
2747 this->may_convert_function_types_ = true;
2750 // Import a type conversion expression.
2756 do_traverse(Traverse* traverse);
2759 do_lower(Gogo*, Named_object*, int);
2762 do_is_constant() const
2763 { return this->expr_->is_constant(); }
2766 do_integer_constant_value(bool, mpz_t, Type**) const;
2769 do_float_constant_value(mpfr_t, Type**) const;
2772 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2775 do_string_constant_value(std::string*) const;
2779 { return this->type_; }
2782 do_determine_type(const Type_context*)
2784 Type_context subcontext(this->type_, false);
2785 this->expr_->determine_type(&subcontext);
2789 do_check_types(Gogo*);
2794 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2799 do_get_tree(Translate_context* context);
2802 do_export(Export*) const;
2805 // The type to convert to.
2807 // The expression to convert.
2809 // True if this is permitted to convert function types. This is
2810 // used internally for method expressions.
2811 bool may_convert_function_types_;
2817 Type_conversion_expression::do_traverse(Traverse* traverse)
2819 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
2820 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
2821 return TRAVERSE_EXIT;
2822 return TRAVERSE_CONTINUE;
2825 // Convert to a constant at lowering time.
2828 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
2830 Type* type = this->type_;
2831 Expression* val = this->expr_;
2832 source_location location = this->location();
2834 if (type->integer_type() != NULL)
2839 if (val->integer_constant_value(false, ival, &dummy))
2841 if (!Integer_expression::check_constant(ival, type, location))
2842 mpz_set_ui(ival, 0);
2843 Expression* ret = Expression::make_integer(&ival, type, location);
2850 if (val->float_constant_value(fval, &dummy))
2852 if (!mpfr_integer_p(fval))
2855 "floating point constant truncated to integer");
2856 return Expression::make_error(location);
2858 mpfr_get_z(ival, fval, GMP_RNDN);
2859 if (!Integer_expression::check_constant(ival, type, location))
2860 mpz_set_ui(ival, 0);
2861 Expression* ret = Expression::make_integer(&ival, type, location);
2870 if (type->float_type() != NULL)
2875 if (val->float_constant_value(fval, &dummy))
2877 if (!Float_expression::check_constant(fval, type, location))
2878 mpfr_set_ui(fval, 0, GMP_RNDN);
2879 Float_expression::constrain_float(fval, type);
2880 Expression *ret = Expression::make_float(&fval, type, location);
2887 if (type->complex_type() != NULL)
2894 if (val->complex_constant_value(real, imag, &dummy))
2896 if (!Complex_expression::check_constant(real, imag, type, location))
2898 mpfr_set_ui(real, 0, GMP_RNDN);
2899 mpfr_set_ui(imag, 0, GMP_RNDN);
2901 Complex_expression::constrain_complex(real, imag, type);
2902 Expression* ret = Expression::make_complex(&real, &imag, type,
2912 if (type->is_open_array_type() && type->named_type() == NULL)
2914 Type* element_type = type->array_type()->element_type()->forwarded();
2915 bool is_byte = element_type == Type::lookup_integer_type("uint8");
2916 bool is_int = element_type == Type::lookup_integer_type("int");
2917 if (is_byte || is_int)
2920 if (val->string_constant_value(&s))
2922 Expression_list* vals = new Expression_list();
2925 for (std::string::const_iterator p = s.begin();
2930 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
2931 Expression* v = Expression::make_integer(&val,
2940 const char *p = s.data();
2941 const char *pend = s.data() + s.length();
2945 int adv = Lex::fetch_char(p, &c);
2948 warning_at(this->location(), 0,
2949 "invalid UTF-8 encoding");
2954 mpz_init_set_ui(val, c);
2955 Expression* v = Expression::make_integer(&val,
2963 return Expression::make_slice_composite_literal(type, vals,
2972 // Return the constant integer value if there is one.
2975 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
2979 if (this->type_->integer_type() == NULL)
2985 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
2987 if (!Integer_expression::check_constant(ival, this->type_,
2995 *ptype = this->type_;
3002 if (this->expr_->float_constant_value(fval, &dummy))
3004 mpfr_get_z(val, fval, GMP_RNDN);
3006 if (!Integer_expression::check_constant(val, this->type_,
3009 *ptype = this->type_;
3017 // Return the constant floating point value if there is one.
3020 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3023 if (this->type_->float_type() == NULL)
3029 if (this->expr_->float_constant_value(fval, &dummy))
3031 if (!Float_expression::check_constant(fval, this->type_,
3037 mpfr_set(val, fval, GMP_RNDN);
3039 Float_expression::constrain_float(val, this->type_);
3040 *ptype = this->type_;
3048 // Return the constant complex value if there is one.
3051 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3055 if (this->type_->complex_type() == NULL)
3063 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3065 if (!Complex_expression::check_constant(rval, ival, this->type_,
3072 mpfr_set(real, rval, GMP_RNDN);
3073 mpfr_set(imag, ival, GMP_RNDN);
3076 Complex_expression::constrain_complex(real, imag, this->type_);
3077 *ptype = this->type_;
3086 // Return the constant string value if there is one.
3089 Type_conversion_expression::do_string_constant_value(std::string* val) const
3091 if (this->type_->is_string_type()
3092 && this->expr_->type()->integer_type() != NULL)
3097 if (this->expr_->integer_constant_value(false, ival, &dummy))
3099 unsigned long ulval = mpz_get_ui(ival);
3100 if (mpz_cmp_ui(ival, ulval) == 0)
3102 Lex::append_char(ulval, true, val, this->location());
3110 // FIXME: Could handle conversion from const []int here.
3115 // Check that types are convertible.
3118 Type_conversion_expression::do_check_types(Gogo*)
3120 Type* type = this->type_;
3121 Type* expr_type = this->expr_->type();
3124 if (this->may_convert_function_types_
3125 && type->function_type() != NULL
3126 && expr_type->function_type() != NULL)
3129 if (Type::are_convertible(type, expr_type, &reason))
3132 error_at(this->location(), "%s", reason.c_str());
3133 this->set_is_error();
3136 // Get a tree for a type conversion.
3139 Type_conversion_expression::do_get_tree(Translate_context* context)
3141 Gogo* gogo = context->gogo();
3142 tree type_tree = this->type_->get_tree(gogo);
3143 tree expr_tree = this->expr_->get_tree(context);
3145 if (type_tree == error_mark_node
3146 || expr_tree == error_mark_node
3147 || TREE_TYPE(expr_tree) == error_mark_node)
3148 return error_mark_node;
3150 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3151 return fold_convert(type_tree, expr_tree);
3153 Type* type = this->type_;
3154 Type* expr_type = this->expr_->type();
3156 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3157 ret = Expression::convert_for_assignment(context, type, expr_type,
3158 expr_tree, this->location());
3159 else if (type->integer_type() != NULL)
3161 if (expr_type->integer_type() != NULL
3162 || expr_type->float_type() != NULL
3163 || expr_type->is_unsafe_pointer_type())
3164 ret = fold(convert_to_integer(type_tree, expr_tree));
3168 else if (type->float_type() != NULL)
3170 if (expr_type->integer_type() != NULL
3171 || expr_type->float_type() != NULL)
3172 ret = fold(convert_to_real(type_tree, expr_tree));
3176 else if (type->complex_type() != NULL)
3178 if (expr_type->complex_type() != NULL)
3179 ret = fold(convert_to_complex(type_tree, expr_tree));
3183 else if (type->is_string_type()
3184 && expr_type->integer_type() != NULL)
3186 expr_tree = fold_convert(integer_type_node, expr_tree);
3187 if (host_integerp(expr_tree, 0))
3189 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3191 Lex::append_char(intval, true, &s, this->location());
3192 Expression* se = Expression::make_string(s, this->location());
3193 return se->get_tree(context);
3196 static tree int_to_string_fndecl;
3197 ret = Gogo::call_builtin(&int_to_string_fndecl,
3199 "__go_int_to_string",
3203 fold_convert(integer_type_node, expr_tree));
3205 else if (type->is_string_type()
3206 && (expr_type->array_type() != NULL
3207 || (expr_type->points_to() != NULL
3208 && expr_type->points_to()->array_type() != NULL)))
3210 Type* t = expr_type;
3211 if (t->points_to() != NULL)
3214 expr_tree = build_fold_indirect_ref(expr_tree);
3216 if (!DECL_P(expr_tree))
3217 expr_tree = save_expr(expr_tree);
3218 Array_type* a = t->array_type();
3219 Type* e = a->element_type()->forwarded();
3220 gcc_assert(e->integer_type() != NULL);
3221 tree valptr = fold_convert(const_ptr_type_node,
3222 a->value_pointer_tree(gogo, expr_tree));
3223 tree len = a->length_tree(gogo, expr_tree);
3224 len = fold_convert_loc(this->location(), size_type_node, len);
3225 if (e->integer_type()->is_unsigned()
3226 && e->integer_type()->bits() == 8)
3228 static tree byte_array_to_string_fndecl;
3229 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3231 "__go_byte_array_to_string",
3234 const_ptr_type_node,
3241 gcc_assert(e == Type::lookup_integer_type("int"));
3242 static tree int_array_to_string_fndecl;
3243 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3245 "__go_int_array_to_string",
3248 const_ptr_type_node,
3254 else if (type->is_open_array_type() && expr_type->is_string_type())
3256 Type* e = type->array_type()->element_type()->forwarded();
3257 gcc_assert(e->integer_type() != NULL);
3258 if (e->integer_type()->is_unsigned()
3259 && e->integer_type()->bits() == 8)
3261 static tree string_to_byte_array_fndecl;
3262 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3264 "__go_string_to_byte_array",
3267 TREE_TYPE(expr_tree),
3272 gcc_assert(e == Type::lookup_integer_type("int"));
3273 static tree string_to_int_array_fndecl;
3274 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3276 "__go_string_to_int_array",
3279 TREE_TYPE(expr_tree),
3283 else if ((type->is_unsafe_pointer_type()
3284 && expr_type->points_to() != NULL)
3285 || (expr_type->is_unsafe_pointer_type()
3286 && type->points_to() != NULL))
3287 ret = fold_convert(type_tree, expr_tree);
3288 else if (type->is_unsafe_pointer_type()
3289 && expr_type->integer_type() != NULL)
3290 ret = convert_to_pointer(type_tree, expr_tree);
3291 else if (this->may_convert_function_types_
3292 && type->function_type() != NULL
3293 && expr_type->function_type() != NULL)
3294 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3296 ret = Expression::convert_for_assignment(context, type, expr_type,
3297 expr_tree, this->location());
3302 // Output a type conversion in a constant expression.
3305 Type_conversion_expression::do_export(Export* exp) const
3307 exp->write_c_string("convert(");
3308 exp->write_type(this->type_);
3309 exp->write_c_string(", ");
3310 this->expr_->export_expression(exp);
3311 exp->write_c_string(")");
3314 // Import a type conversion or a struct construction.
3317 Type_conversion_expression::do_import(Import* imp)
3319 imp->require_c_string("convert(");
3320 Type* type = imp->read_type();
3321 imp->require_c_string(", ");
3322 Expression* val = Expression::import_expression(imp);
3323 imp->require_c_string(")");
3324 return Expression::make_cast(type, val, imp->location());
3327 // Make a type cast expression.
3330 Expression::make_cast(Type* type, Expression* val, source_location location)
3332 if (type->is_error_type() || val->is_error_expression())
3333 return Expression::make_error(location);
3334 return new Type_conversion_expression(type, val, location);
3337 // Unary expressions.
3339 class Unary_expression : public Expression
3342 Unary_expression(Operator op, Expression* expr, source_location location)
3343 : Expression(EXPRESSION_UNARY, location),
3344 op_(op), escapes_(true), expr_(expr)
3347 // Return the operator.
3350 { return this->op_; }
3352 // Return the operand.
3355 { return this->expr_; }
3357 // Record that an address expression does not escape.
3359 set_does_not_escape()
3361 gcc_assert(this->op_ == OPERATOR_AND);
3362 this->escapes_ = false;
3365 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3366 // could be done, false if not.
3368 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3371 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3372 // could be done, false if not.
3374 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3376 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3377 // true if this could be done, false if not.
3379 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3387 do_traverse(Traverse* traverse)
3388 { return Expression::traverse(&this->expr_, traverse); }
3391 do_lower(Gogo*, Named_object*, int);
3394 do_is_constant() const;
3397 do_integer_constant_value(bool, mpz_t, Type**) const;
3400 do_float_constant_value(mpfr_t, Type**) const;
3403 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3409 do_determine_type(const Type_context*);
3412 do_check_types(Gogo*);
3417 return Expression::make_unary(this->op_, this->expr_->copy(),
3422 do_is_addressable() const
3423 { return this->op_ == OPERATOR_MULT; }
3426 do_get_tree(Translate_context*);
3429 do_export(Export*) const;
3432 // The unary operator to apply.
3434 // Normally true. False if this is an address expression which does
3435 // not escape the current function.
3441 // If we are taking the address of a composite literal, and the
3442 // contents are not constant, then we want to make a heap composite
3446 Unary_expression::do_lower(Gogo*, Named_object*, int)
3448 source_location loc = this->location();
3449 Operator op = this->op_;
3450 Expression* expr = this->expr_;
3452 if (op == OPERATOR_MULT && expr->is_type_expression())
3453 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3455 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3456 // moving x to the heap. FIXME: Is it worth doing a real escape
3457 // analysis here? This case is found in math/unsafe.go and is
3458 // therefore worth special casing.
3459 if (op == OPERATOR_MULT)
3461 Expression* e = expr;
3462 while (e->classification() == EXPRESSION_CONVERSION)
3464 Type_conversion_expression* te
3465 = static_cast<Type_conversion_expression*>(e);
3469 if (e->classification() == EXPRESSION_UNARY)
3471 Unary_expression* ue = static_cast<Unary_expression*>(e);
3472 if (ue->op_ == OPERATOR_AND)
3479 ue->set_does_not_escape();
3484 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3485 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3487 Expression* ret = NULL;
3492 if (expr->integer_constant_value(false, eval, &etype))
3496 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3497 ret = Expression::make_integer(&val, etype, loc);
3504 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
3509 if (expr->float_constant_value(fval, &ftype))
3513 if (Unary_expression::eval_float(op, fval, val))
3514 ret = Expression::make_float(&val, ftype, loc);
3525 if (expr->complex_constant_value(fval, ival, &ftype))
3531 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
3532 ret = Expression::make_complex(&real, &imag, ftype, loc);
3546 // Return whether a unary expression is a constant.
3549 Unary_expression::do_is_constant() const
3551 if (this->op_ == OPERATOR_MULT)
3553 // Indirecting through a pointer is only constant if the object
3554 // to which the expression points is constant, but we currently
3555 // have no way to determine that.
3558 else if (this->op_ == OPERATOR_AND)
3560 // Taking the address of a variable is constant if it is a
3561 // global variable, not constant otherwise. In other cases
3562 // taking the address is probably not a constant.
3563 Var_expression* ve = this->expr_->var_expression();
3566 Named_object* no = ve->named_object();
3567 return no->is_variable() && no->var_value()->is_global();
3572 return this->expr_->is_constant();
3575 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3576 // UVAL, if known; it may be NULL. Return true if this could be done,
3580 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3581 source_location location)
3588 case OPERATOR_MINUS:
3590 return Integer_expression::check_constant(val, utype, location);
3592 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3596 || utype->integer_type() == NULL
3597 || utype->integer_type()->is_abstract())
3601 // The number of HOST_WIDE_INTs that it takes to represent
3603 size_t count = ((mpz_sizeinbase(uval, 2)
3604 + HOST_BITS_PER_WIDE_INT
3606 / HOST_BITS_PER_WIDE_INT);
3608 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3609 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3612 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3613 gcc_assert(ecount <= count);
3615 // Trim down to the number of words required by the type.
3616 size_t obits = utype->integer_type()->bits();
3617 if (!utype->integer_type()->is_unsigned())
3619 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3620 / HOST_BITS_PER_WIDE_INT);
3621 gcc_assert(ocount <= ocount);
3623 for (size_t i = 0; i < ocount; ++i)
3626 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3628 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3631 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3635 return Integer_expression::check_constant(val, utype, location);
3644 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3645 // could be done, false if not.
3648 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
3653 mpfr_set(val, uval, GMP_RNDN);
3655 case OPERATOR_MINUS:
3656 mpfr_neg(val, uval, GMP_RNDN);
3668 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3669 // if this could be done, false if not.
3672 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
3673 mpfr_t real, mpfr_t imag)
3678 mpfr_set(real, rval, GMP_RNDN);
3679 mpfr_set(imag, ival, GMP_RNDN);
3681 case OPERATOR_MINUS:
3682 mpfr_neg(real, rval, GMP_RNDN);
3683 mpfr_neg(imag, ival, GMP_RNDN);
3695 // Return the integral constant value of a unary expression, if it has one.
3698 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
3704 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
3707 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
3713 // Return the floating point constant value of a unary expression, if
3717 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
3722 if (!this->expr_->float_constant_value(uval, ptype))
3725 ret = Unary_expression::eval_float(this->op_, uval, val);
3730 // Return the complex constant value of a unary expression, if it has
3734 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
3742 if (!this->expr_->complex_constant_value(rval, ival, ptype))
3745 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
3751 // Return the type of a unary expression.
3754 Unary_expression::do_type()
3759 case OPERATOR_MINUS:
3762 return this->expr_->type();
3765 return Type::make_pointer_type(this->expr_->type());
3769 Type* subtype = this->expr_->type();
3770 Type* points_to = subtype->points_to();
3771 if (points_to == NULL)
3772 return Type::make_error_type();
3781 // Determine abstract types for a unary expression.
3784 Unary_expression::do_determine_type(const Type_context* context)
3789 case OPERATOR_MINUS:
3792 this->expr_->determine_type(context);
3796 // Taking the address of something.
3798 Type* subtype = (context->type == NULL
3800 : context->type->points_to());
3801 Type_context subcontext(subtype, false);
3802 this->expr_->determine_type(&subcontext);
3807 // Indirecting through a pointer.
3809 Type* subtype = (context->type == NULL
3811 : Type::make_pointer_type(context->type));
3812 Type_context subcontext(subtype, false);
3813 this->expr_->determine_type(&subcontext);
3822 // Check types for a unary expression.
3825 Unary_expression::do_check_types(Gogo*)
3830 case OPERATOR_MINUS:
3832 Type* type = this->expr_->type();
3833 if (type->integer_type() == NULL
3834 && type->float_type() == NULL
3835 && type->complex_type() == NULL
3836 && !type->is_error_type())
3837 this->report_error(_("expected numeric type"));
3844 Type* type = this->expr_->type();
3845 if (type->integer_type() == NULL
3846 && !type->is_boolean_type()
3847 && !type->is_error_type())
3848 this->report_error(_("expected integer or boolean type"));
3853 if (!this->expr_->is_addressable())
3854 this->report_error(_("invalid operand for unary %<&%>"));
3856 this->expr_->address_taken(this->escapes_);
3860 // Indirecting through a pointer.
3862 Type* type = this->expr_->type();
3863 if (type->points_to() == NULL
3864 && !type->is_error_type())
3865 this->report_error(_("expected pointer"));
3874 // Get a tree for a unary expression.
3877 Unary_expression::do_get_tree(Translate_context* context)
3879 tree expr = this->expr_->get_tree(context);
3880 if (expr == error_mark_node)
3881 return error_mark_node;
3883 source_location loc = this->location();
3889 case OPERATOR_MINUS:
3891 tree type = TREE_TYPE(expr);
3892 tree compute_type = excess_precision_type(type);
3893 if (compute_type != NULL_TREE)
3894 expr = ::convert(compute_type, expr);
3895 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
3896 (compute_type != NULL_TREE
3900 if (compute_type != NULL_TREE)
3901 ret = ::convert(type, ret);
3906 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
3907 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
3909 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
3910 build_int_cst(TREE_TYPE(expr), 0));
3913 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
3916 // We should not see a non-constant constructor here; cases
3917 // where we would see one should have been moved onto the heap
3918 // at parse time. Taking the address of a nonconstant
3919 // constructor will not do what the programmer expects.
3920 gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
3921 gcc_assert(TREE_CODE(expr) != ADDR_EXPR);
3923 // Build a decl for a constant constructor.
3924 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
3926 tree decl = build_decl(this->location(), VAR_DECL,
3927 create_tmp_var_name("C"), TREE_TYPE(expr));
3928 DECL_EXTERNAL(decl) = 0;
3929 TREE_PUBLIC(decl) = 0;
3930 TREE_READONLY(decl) = 1;
3931 TREE_CONSTANT(decl) = 1;
3932 TREE_STATIC(decl) = 1;
3933 TREE_ADDRESSABLE(decl) = 1;
3934 DECL_ARTIFICIAL(decl) = 1;
3935 DECL_INITIAL(decl) = expr;
3936 rest_of_decl_compilation(decl, 1, 0);
3940 return build_fold_addr_expr_loc(loc, expr);
3944 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
3946 // If we are dereferencing the pointer to a large struct, we
3947 // need to check for nil. We don't bother to check for small
3948 // structs because we expect the system to crash on a nil
3949 // pointer dereference.
3950 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
3951 if (s == -1 || s >= 4096)
3954 expr = save_expr(expr);
3955 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
3957 fold_convert(TREE_TYPE(expr),
3958 null_pointer_node));
3959 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
3961 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
3962 build3(COND_EXPR, void_type_node,
3963 compare, crash, NULL_TREE),
3967 // If the type of EXPR is a recursive pointer type, then we
3968 // need to insert a cast before indirecting.
3969 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
3971 Type* pt = this->expr_->type()->points_to();
3972 tree ind = pt->get_tree(context->gogo());
3973 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
3976 return build_fold_indirect_ref_loc(loc, expr);
3984 // Export a unary expression.
3987 Unary_expression::do_export(Export* exp) const
3992 exp->write_c_string("+ ");
3994 case OPERATOR_MINUS:
3995 exp->write_c_string("- ");
3998 exp->write_c_string("! ");
4001 exp->write_c_string("^ ");
4008 this->expr_->export_expression(exp);
4011 // Import a unary expression.
4014 Unary_expression::do_import(Import* imp)
4017 switch (imp->get_char())
4023 op = OPERATOR_MINUS;
4034 imp->require_c_string(" ");
4035 Expression* expr = Expression::import_expression(imp);
4036 return Expression::make_unary(op, expr, imp->location());
4039 // Make a unary expression.
4042 Expression::make_unary(Operator op, Expression* expr, source_location location)
4044 return new Unary_expression(op, expr, location);
4047 // If this is an indirection through a pointer, return the expression
4048 // being pointed through. Otherwise return this.
4053 if (this->classification_ == EXPRESSION_UNARY)
4055 Unary_expression* ue = static_cast<Unary_expression*>(this);
4056 if (ue->op() == OPERATOR_MULT)
4057 return ue->operand();
4062 // Class Binary_expression.
4067 Binary_expression::do_traverse(Traverse* traverse)
4069 int t = Expression::traverse(&this->left_, traverse);
4070 if (t == TRAVERSE_EXIT)
4071 return TRAVERSE_EXIT;
4072 return Expression::traverse(&this->right_, traverse);
4075 // Compare integer constants according to OP.
4078 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4081 int i = mpz_cmp(left_val, right_val);
4086 case OPERATOR_NOTEQ:
4101 // Compare floating point constants according to OP.
4104 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4109 i = mpfr_cmp(left_val, right_val);
4113 mpfr_init_set(lv, left_val, GMP_RNDN);
4115 mpfr_init_set(rv, right_val, GMP_RNDN);
4116 Float_expression::constrain_float(lv, type);
4117 Float_expression::constrain_float(rv, type);
4118 i = mpfr_cmp(lv, rv);
4126 case OPERATOR_NOTEQ:
4141 // Compare complex constants according to OP. Complex numbers may
4142 // only be compared for equality.
4145 Binary_expression::compare_complex(Operator op, Type* type,
4146 mpfr_t left_real, mpfr_t left_imag,
4147 mpfr_t right_real, mpfr_t right_imag)
4151 is_equal = (mpfr_cmp(left_real, right_real) == 0
4152 && mpfr_cmp(left_imag, right_imag) == 0);
4157 mpfr_init_set(lr, left_real, GMP_RNDN);
4158 mpfr_init_set(li, left_imag, GMP_RNDN);
4161 mpfr_init_set(rr, right_real, GMP_RNDN);
4162 mpfr_init_set(ri, right_imag, GMP_RNDN);
4163 Complex_expression::constrain_complex(lr, li, type);
4164 Complex_expression::constrain_complex(rr, ri, type);
4165 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4175 case OPERATOR_NOTEQ:
4182 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4183 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4184 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4185 // this could be done, false if not.
4188 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4189 Type* right_type, mpz_t right_val,
4190 source_location location, mpz_t val)
4192 bool is_shift_op = false;
4196 case OPERATOR_ANDAND:
4198 case OPERATOR_NOTEQ:
4203 // These return boolean values. We should probably handle them
4204 // anyhow in case a type conversion is used on the result.
4207 mpz_add(val, left_val, right_val);
4209 case OPERATOR_MINUS:
4210 mpz_sub(val, left_val, right_val);
4213 mpz_ior(val, left_val, right_val);
4216 mpz_xor(val, left_val, right_val);
4219 mpz_mul(val, left_val, right_val);
4222 if (mpz_sgn(right_val) != 0)
4223 mpz_tdiv_q(val, left_val, right_val);
4226 error_at(location, "division by zero");
4232 if (mpz_sgn(right_val) != 0)
4233 mpz_tdiv_r(val, left_val, right_val);
4236 error_at(location, "division by zero");
4241 case OPERATOR_LSHIFT:
4243 unsigned long shift = mpz_get_ui(right_val);
4244 if (mpz_cmp_ui(right_val, shift) != 0)
4246 error_at(location, "shift count overflow");
4250 mpz_mul_2exp(val, left_val, shift);
4255 case OPERATOR_RSHIFT:
4257 unsigned long shift = mpz_get_ui(right_val);
4258 if (mpz_cmp_ui(right_val, shift) != 0)
4260 error_at(location, "shift count overflow");
4264 if (mpz_cmp_ui(left_val, 0) >= 0)
4265 mpz_tdiv_q_2exp(val, left_val, shift);
4267 mpz_fdiv_q_2exp(val, left_val, shift);
4273 mpz_and(val, left_val, right_val);
4275 case OPERATOR_BITCLEAR:
4279 mpz_com(tval, right_val);
4280 mpz_and(val, left_val, tval);
4288 Type* type = left_type;
4293 else if (type != right_type && right_type != NULL)
4295 if (type->is_abstract())
4297 else if (!right_type->is_abstract())
4299 // This look like a type error which should be diagnosed
4300 // elsewhere. Don't do anything here, to avoid an
4301 // unhelpful chain of error messages.
4307 if (type != NULL && !type->is_abstract())
4309 // We have to check the operands too, as we have implicitly
4310 // coerced them to TYPE.
4311 if ((type != left_type
4312 && !Integer_expression::check_constant(left_val, type, location))
4314 && type != right_type
4315 && !Integer_expression::check_constant(right_val, type,
4317 || !Integer_expression::check_constant(val, type, location))
4324 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4325 // Return true if this could be done, false if not.
4328 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4329 Type* right_type, mpfr_t right_val,
4330 mpfr_t val, source_location location)
4335 case OPERATOR_ANDAND:
4337 case OPERATOR_NOTEQ:
4342 // These return boolean values. We should probably handle them
4343 // anyhow in case a type conversion is used on the result.
4346 mpfr_add(val, left_val, right_val, GMP_RNDN);
4348 case OPERATOR_MINUS:
4349 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4354 case OPERATOR_BITCLEAR:
4357 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4360 if (mpfr_zero_p(right_val))
4361 error_at(location, "division by zero");
4362 mpfr_div(val, left_val, right_val, GMP_RNDN);
4366 case OPERATOR_LSHIFT:
4367 case OPERATOR_RSHIFT:
4373 Type* type = left_type;
4376 else if (type != right_type && right_type != NULL)
4378 if (type->is_abstract())
4380 else if (!right_type->is_abstract())
4382 // This looks like a type error which should be diagnosed
4383 // elsewhere. Don't do anything here, to avoid an unhelpful
4384 // chain of error messages.
4389 if (type != NULL && !type->is_abstract())
4391 if ((type != left_type
4392 && !Float_expression::check_constant(left_val, type, location))
4393 || (type != right_type
4394 && !Float_expression::check_constant(right_val, type,
4396 || !Float_expression::check_constant(val, type, location))
4397 mpfr_set_ui(val, 0, GMP_RNDN);
4403 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4404 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4405 // could be done, false if not.
4408 Binary_expression::eval_complex(Operator op, Type* left_type,
4409 mpfr_t left_real, mpfr_t left_imag,
4411 mpfr_t right_real, mpfr_t right_imag,
4412 mpfr_t real, mpfr_t imag,
4413 source_location location)
4418 case OPERATOR_ANDAND:
4420 case OPERATOR_NOTEQ:
4425 // These return boolean values and must be handled differently.
4428 mpfr_add(real, left_real, right_real, GMP_RNDN);
4429 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4431 case OPERATOR_MINUS:
4432 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4433 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4438 case OPERATOR_BITCLEAR:
4442 // You might think that multiplying two complex numbers would
4443 // be simple, and you would be right, until you start to think
4444 // about getting the right answer for infinity. If one
4445 // operand here is infinity and the other is anything other
4446 // than zero or NaN, then we are going to wind up subtracting
4447 // two infinity values. That will give us a NaN, but the
4448 // correct answer is infinity.
4452 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4456 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4460 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4464 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4466 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4467 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4469 // If we get NaN on both sides, check whether it should really
4470 // be infinity. The rule is that if either side of the
4471 // complex number is infinity, then the whole value is
4472 // infinity, even if the other side is NaN. So the only case
4473 // we have to fix is the one in which both sides are NaN.
4474 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4475 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4476 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4478 bool is_infinity = false;
4482 mpfr_init_set(lr, left_real, GMP_RNDN);
4483 mpfr_init_set(li, left_imag, GMP_RNDN);
4487 mpfr_init_set(rr, right_real, GMP_RNDN);
4488 mpfr_init_set(ri, right_imag, GMP_RNDN);
4490 // If the left side is infinity, then the result is
4492 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4494 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4495 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4496 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4497 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4500 mpfr_set_ui(rr, 0, GMP_RNDN);
4501 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4505 mpfr_set_ui(ri, 0, GMP_RNDN);
4506 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4511 // If the right side is infinity, then the result is
4513 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4515 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4516 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4517 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4518 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4521 mpfr_set_ui(lr, 0, GMP_RNDN);
4522 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4526 mpfr_set_ui(li, 0, GMP_RNDN);
4527 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4532 // If we got an overflow in the intermediate computations,
4533 // then the result is infinity.
4535 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4536 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4540 mpfr_set_ui(lr, 0, GMP_RNDN);
4541 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4545 mpfr_set_ui(li, 0, GMP_RNDN);
4546 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4550 mpfr_set_ui(rr, 0, GMP_RNDN);
4551 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4555 mpfr_set_ui(ri, 0, GMP_RNDN);
4556 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4563 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4564 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4565 mpfr_mul(lirr, li, rr, GMP_RNDN);
4566 mpfr_mul(liri, li, ri, GMP_RNDN);
4567 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4568 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4569 mpfr_set_inf(real, mpfr_sgn(real));
4570 mpfr_set_inf(imag, mpfr_sgn(imag));
4587 // For complex division we want to avoid having an
4588 // intermediate overflow turn the whole result in a NaN. We
4589 // scale the values to try to avoid this.
4591 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4592 error_at(location, "division by zero");
4598 mpfr_abs(rra, right_real, GMP_RNDN);
4599 mpfr_abs(ria, right_imag, GMP_RNDN);
4602 mpfr_max(t, rra, ria, GMP_RNDN);
4606 mpfr_init_set(rr, right_real, GMP_RNDN);
4607 mpfr_init_set(ri, right_imag, GMP_RNDN);
4609 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4611 ilogbw = mpfr_get_exp(t);
4612 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4613 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4618 mpfr_mul(denom, rr, rr, GMP_RNDN);
4619 mpfr_mul(t, ri, ri, GMP_RNDN);
4620 mpfr_add(denom, denom, t, GMP_RNDN);
4622 mpfr_mul(real, left_real, rr, GMP_RNDN);
4623 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4624 mpfr_add(real, real, t, GMP_RNDN);
4625 mpfr_div(real, real, denom, GMP_RNDN);
4626 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4628 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4629 mpfr_mul(t, left_real, ri, GMP_RNDN);
4630 mpfr_sub(imag, imag, t, GMP_RNDN);
4631 mpfr_div(imag, imag, denom, GMP_RNDN);
4632 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4634 // If we wind up with NaN on both sides, check whether we
4635 // should really have infinity. The rule is that if either
4636 // side of the complex number is infinity, then the whole
4637 // value is infinity, even if the other side is NaN. So the
4638 // only case we have to fix is the one in which both sides are
4640 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4641 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4642 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4644 if (mpfr_zero_p(denom))
4646 mpfr_set_inf(real, mpfr_sgn(rr));
4647 mpfr_mul(real, real, left_real, GMP_RNDN);
4648 mpfr_set_inf(imag, mpfr_sgn(rr));
4649 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4651 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4652 && mpfr_number_p(rr) && mpfr_number_p(ri))
4654 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4655 mpfr_copysign(t, t, left_real, GMP_RNDN);
4658 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4659 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4663 mpfr_mul(t3, t, rr, GMP_RNDN);
4667 mpfr_mul(t4, t2, ri, GMP_RNDN);
4669 mpfr_add(t3, t3, t4, GMP_RNDN);
4670 mpfr_set_inf(real, mpfr_sgn(t3));
4672 mpfr_mul(t3, t2, rr, GMP_RNDN);
4673 mpfr_mul(t4, t, ri, GMP_RNDN);
4674 mpfr_sub(t3, t3, t4, GMP_RNDN);
4675 mpfr_set_inf(imag, mpfr_sgn(t3));
4681 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
4682 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
4684 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4685 mpfr_copysign(t, t, rr, GMP_RNDN);
4688 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4689 mpfr_copysign(t2, t2, ri, GMP_RNDN);
4693 mpfr_mul(t3, left_real, t, GMP_RNDN);
4697 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
4699 mpfr_add(t3, t3, t4, GMP_RNDN);
4700 mpfr_set_ui(real, 0, GMP_RNDN);
4701 mpfr_mul(real, real, t3, GMP_RNDN);
4703 mpfr_mul(t3, left_imag, t, GMP_RNDN);
4704 mpfr_mul(t4, left_real, t2, GMP_RNDN);
4705 mpfr_sub(t3, t3, t4, GMP_RNDN);
4706 mpfr_set_ui(imag, 0, GMP_RNDN);
4707 mpfr_mul(imag, imag, t3, GMP_RNDN);
4725 case OPERATOR_LSHIFT:
4726 case OPERATOR_RSHIFT:
4732 Type* type = left_type;
4735 else if (type != right_type && right_type != NULL)
4737 if (type->is_abstract())
4739 else if (!right_type->is_abstract())
4741 // This looks like a type error which should be diagnosed
4742 // elsewhere. Don't do anything here, to avoid an unhelpful
4743 // chain of error messages.
4748 if (type != NULL && !type->is_abstract())
4750 if ((type != left_type
4751 && !Complex_expression::check_constant(left_real, left_imag,
4753 || (type != right_type
4754 && !Complex_expression::check_constant(right_real, right_imag,
4756 || !Complex_expression::check_constant(real, imag, type,
4759 mpfr_set_ui(real, 0, GMP_RNDN);
4760 mpfr_set_ui(imag, 0, GMP_RNDN);
4767 // Lower a binary expression. We have to evaluate constant
4768 // expressions now, in order to implement Go's unlimited precision
4772 Binary_expression::do_lower(Gogo*, Named_object*, int)
4774 source_location location = this->location();
4775 Operator op = this->op_;
4776 Expression* left = this->left_;
4777 Expression* right = this->right_;
4779 const bool is_comparison = (op == OPERATOR_EQEQ
4780 || op == OPERATOR_NOTEQ
4781 || op == OPERATOR_LT
4782 || op == OPERATOR_LE
4783 || op == OPERATOR_GT
4784 || op == OPERATOR_GE);
4786 // Integer constant expressions.
4792 mpz_init(right_val);
4794 if (left->integer_constant_value(false, left_val, &left_type)
4795 && right->integer_constant_value(false, right_val, &right_type))
4797 Expression* ret = NULL;
4798 if (left_type != right_type
4799 && left_type != NULL
4800 && right_type != NULL
4801 && left_type->base() != right_type->base()
4802 && op != OPERATOR_LSHIFT
4803 && op != OPERATOR_RSHIFT)
4805 // May be a type error--let it be diagnosed later.
4807 else if (is_comparison)
4809 bool b = Binary_expression::compare_integer(op, left_val,
4811 ret = Expression::make_cast(Type::lookup_bool_type(),
4812 Expression::make_boolean(b, location),
4820 if (Binary_expression::eval_integer(op, left_type, left_val,
4821 right_type, right_val,
4824 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
4826 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
4828 else if (left_type == NULL)
4830 else if (right_type == NULL)
4832 else if (!left_type->is_abstract()
4833 && left_type->named_type() != NULL)
4835 else if (!right_type->is_abstract()
4836 && right_type->named_type() != NULL)
4838 else if (!left_type->is_abstract())
4840 else if (!right_type->is_abstract())
4842 else if (left_type->float_type() != NULL)
4844 else if (right_type->float_type() != NULL)
4846 else if (left_type->complex_type() != NULL)
4848 else if (right_type->complex_type() != NULL)
4852 ret = Expression::make_integer(&val, type, location);
4860 mpz_clear(right_val);
4861 mpz_clear(left_val);
4865 mpz_clear(right_val);
4866 mpz_clear(left_val);
4869 // Floating point constant expressions.
4872 mpfr_init(left_val);
4875 mpfr_init(right_val);
4877 if (left->float_constant_value(left_val, &left_type)
4878 && right->float_constant_value(right_val, &right_type))
4880 Expression* ret = NULL;
4881 if (left_type != right_type
4882 && left_type != NULL
4883 && right_type != NULL
4884 && left_type->base() != right_type->base()
4885 && op != OPERATOR_LSHIFT
4886 && op != OPERATOR_RSHIFT)
4888 // May be a type error--let it be diagnosed later.
4890 else if (is_comparison)
4892 bool b = Binary_expression::compare_float(op,
4896 left_val, right_val);
4897 ret = Expression::make_boolean(b, location);
4904 if (Binary_expression::eval_float(op, left_type, left_val,
4905 right_type, right_val, val,
4908 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
4909 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
4911 if (left_type == NULL)
4913 else if (right_type == NULL)
4915 else if (!left_type->is_abstract()
4916 && left_type->named_type() != NULL)
4918 else if (!right_type->is_abstract()
4919 && right_type->named_type() != NULL)
4921 else if (!left_type->is_abstract())
4923 else if (!right_type->is_abstract())
4925 else if (left_type->float_type() != NULL)
4927 else if (right_type->float_type() != NULL)
4931 ret = Expression::make_float(&val, type, location);
4939 mpfr_clear(right_val);
4940 mpfr_clear(left_val);
4944 mpfr_clear(right_val);
4945 mpfr_clear(left_val);
4948 // Complex constant expressions.
4952 mpfr_init(left_real);
4953 mpfr_init(left_imag);
4958 mpfr_init(right_real);
4959 mpfr_init(right_imag);
4962 if (left->complex_constant_value(left_real, left_imag, &left_type)
4963 && right->complex_constant_value(right_real, right_imag, &right_type))
4965 Expression* ret = NULL;
4966 if (left_type != right_type
4967 && left_type != NULL
4968 && right_type != NULL
4969 && left_type->base() != right_type->base())
4971 // May be a type error--let it be diagnosed later.
4973 else if (is_comparison)
4975 bool b = Binary_expression::compare_complex(op,
4983 ret = Expression::make_boolean(b, location);
4992 if (Binary_expression::eval_complex(op, left_type,
4993 left_real, left_imag,
4995 right_real, right_imag,
4999 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5000 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5002 if (left_type == NULL)
5004 else if (right_type == NULL)
5006 else if (!left_type->is_abstract()
5007 && left_type->named_type() != NULL)
5009 else if (!right_type->is_abstract()
5010 && right_type->named_type() != NULL)
5012 else if (!left_type->is_abstract())
5014 else if (!right_type->is_abstract())
5016 else if (left_type->complex_type() != NULL)
5018 else if (right_type->complex_type() != NULL)
5022 ret = Expression::make_complex(&real, &imag, type,
5031 mpfr_clear(left_real);
5032 mpfr_clear(left_imag);
5033 mpfr_clear(right_real);
5034 mpfr_clear(right_imag);
5039 mpfr_clear(left_real);
5040 mpfr_clear(left_imag);
5041 mpfr_clear(right_real);
5042 mpfr_clear(right_imag);
5045 // String constant expressions.
5046 if (op == OPERATOR_PLUS
5047 && left->type()->is_string_type()
5048 && right->type()->is_string_type())
5050 std::string left_string;
5051 std::string right_string;
5052 if (left->string_constant_value(&left_string)
5053 && right->string_constant_value(&right_string))
5054 return Expression::make_string(left_string + right_string, location);
5060 // Return the integer constant value, if it has one.
5063 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5069 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5072 mpz_clear(left_val);
5077 mpz_init(right_val);
5079 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5082 mpz_clear(right_val);
5083 mpz_clear(left_val);
5088 if (left_type != right_type
5089 && left_type != NULL
5090 && right_type != NULL
5091 && left_type->base() != right_type->base()
5092 && this->op_ != OPERATOR_RSHIFT
5093 && this->op_ != OPERATOR_LSHIFT)
5096 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5097 right_type, right_val,
5098 this->location(), val);
5100 mpz_clear(right_val);
5101 mpz_clear(left_val);
5109 // Return the floating point constant value, if it has one.
5112 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5115 mpfr_init(left_val);
5117 if (!this->left_->float_constant_value(left_val, &left_type))
5119 mpfr_clear(left_val);
5124 mpfr_init(right_val);
5126 if (!this->right_->float_constant_value(right_val, &right_type))
5128 mpfr_clear(right_val);
5129 mpfr_clear(left_val);
5134 if (left_type != right_type
5135 && left_type != NULL
5136 && right_type != NULL
5137 && left_type->base() != right_type->base())
5140 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5141 right_type, right_val,
5142 val, this->location());
5144 mpfr_clear(left_val);
5145 mpfr_clear(right_val);
5153 // Return the complex constant value, if it has one.
5156 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5161 mpfr_init(left_real);
5162 mpfr_init(left_imag);
5164 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5166 mpfr_clear(left_real);
5167 mpfr_clear(left_imag);
5173 mpfr_init(right_real);
5174 mpfr_init(right_imag);
5176 if (!this->right_->complex_constant_value(right_real, right_imag,
5179 mpfr_clear(left_real);
5180 mpfr_clear(left_imag);
5181 mpfr_clear(right_real);
5182 mpfr_clear(right_imag);
5187 if (left_type != right_type
5188 && left_type != NULL
5189 && right_type != NULL
5190 && left_type->base() != right_type->base())
5193 ret = Binary_expression::eval_complex(this->op_, left_type,
5194 left_real, left_imag,
5196 right_real, right_imag,
5199 mpfr_clear(left_real);
5200 mpfr_clear(left_imag);
5201 mpfr_clear(right_real);
5202 mpfr_clear(right_imag);
5210 // Note that the value is being discarded.
5213 Binary_expression::do_discarding_value()
5215 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5216 this->right_->discarding_value();
5218 this->warn_about_unused_value();
5224 Binary_expression::do_type()
5229 case OPERATOR_ANDAND:
5231 case OPERATOR_NOTEQ:
5236 return Type::lookup_bool_type();
5239 case OPERATOR_MINUS:
5246 case OPERATOR_BITCLEAR:
5248 Type* left_type = this->left_->type();
5249 Type* right_type = this->right_->type();
5250 if (!left_type->is_abstract() && left_type->named_type() != NULL)
5252 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5254 else if (!left_type->is_abstract())
5256 else if (!right_type->is_abstract())
5258 else if (left_type->complex_type() != NULL)
5260 else if (right_type->complex_type() != NULL)
5262 else if (left_type->float_type() != NULL)
5264 else if (right_type->float_type() != NULL)
5270 case OPERATOR_LSHIFT:
5271 case OPERATOR_RSHIFT:
5272 return this->left_->type();
5279 // Set type for a binary expression.
5282 Binary_expression::do_determine_type(const Type_context* context)
5284 Type* tleft = this->left_->type();
5285 Type* tright = this->right_->type();
5287 // Both sides should have the same type, except for the shift
5288 // operations. For a comparison, we should ignore the incoming
5291 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5292 || this->op_ == OPERATOR_RSHIFT);
5294 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5295 || this->op_ == OPERATOR_NOTEQ
5296 || this->op_ == OPERATOR_LT
5297 || this->op_ == OPERATOR_LE
5298 || this->op_ == OPERATOR_GT
5299 || this->op_ == OPERATOR_GE);
5301 Type_context subcontext(*context);
5305 // In a comparison, the context does not determine the types of
5307 subcontext.type = NULL;
5310 // Set the context for the left hand operand.
5313 // The right hand operand plays no role in determining the type
5314 // of the left hand operand. A shift of an abstract integer in
5315 // a string context gets special treatment, which may be a
5317 if (subcontext.type != NULL
5318 && subcontext.type->is_string_type()
5319 && tleft->is_abstract())
5320 error_at(this->location(), "shift of non-integer operand");
5322 else if (!tleft->is_abstract())
5323 subcontext.type = tleft;
5324 else if (!tright->is_abstract())
5325 subcontext.type = tright;
5326 else if (subcontext.type == NULL)
5328 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5329 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5330 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5332 // Both sides have an abstract integer, abstract float, or
5333 // abstract complex type. Just let CONTEXT determine
5334 // whether they may remain abstract or not.
5336 else if (tleft->complex_type() != NULL)
5337 subcontext.type = tleft;
5338 else if (tright->complex_type() != NULL)
5339 subcontext.type = tright;
5340 else if (tleft->float_type() != NULL)
5341 subcontext.type = tleft;
5342 else if (tright->float_type() != NULL)
5343 subcontext.type = tright;
5345 subcontext.type = tleft;
5348 this->left_->determine_type(&subcontext);
5350 // The context for the right hand operand is the same as for the
5351 // left hand operand, except for a shift operator.
5354 subcontext.type = Type::lookup_integer_type("uint");
5355 subcontext.may_be_abstract = false;
5358 this->right_->determine_type(&subcontext);
5361 // Report an error if the binary operator OP does not support TYPE.
5362 // Return whether the operation is OK. This should not be used for
5366 Binary_expression::check_operator_type(Operator op, Type* type,
5367 source_location location)
5372 case OPERATOR_ANDAND:
5373 if (!type->is_boolean_type())
5375 error_at(location, "expected boolean type");
5381 case OPERATOR_NOTEQ:
5382 if (type->integer_type() == NULL
5383 && type->float_type() == NULL
5384 && type->complex_type() == NULL
5385 && !type->is_string_type()
5386 && type->points_to() == NULL
5387 && !type->is_nil_type()
5388 && !type->is_boolean_type()
5389 && type->interface_type() == NULL
5390 && (type->array_type() == NULL
5391 || type->array_type()->length() != NULL)
5392 && type->map_type() == NULL
5393 && type->channel_type() == NULL
5394 && type->function_type() == NULL)
5397 ("expected integer, floating, complex, string, pointer, "
5398 "boolean, interface, slice, map, channel, "
5399 "or function type"));
5408 if (type->integer_type() == NULL
5409 && type->float_type() == NULL
5410 && !type->is_string_type())
5412 error_at(location, "expected integer, floating, or string type");
5418 case OPERATOR_PLUSEQ:
5419 if (type->integer_type() == NULL
5420 && type->float_type() == NULL
5421 && type->complex_type() == NULL
5422 && !type->is_string_type())
5425 "expected integer, floating, complex, or string type");
5430 case OPERATOR_MINUS:
5431 case OPERATOR_MINUSEQ:
5433 case OPERATOR_MULTEQ:
5435 case OPERATOR_DIVEQ:
5436 if (type->integer_type() == NULL
5437 && type->float_type() == NULL
5438 && type->complex_type() == NULL)
5440 error_at(location, "expected integer, floating, or complex type");
5446 case OPERATOR_MODEQ:
5450 case OPERATOR_ANDEQ:
5452 case OPERATOR_XOREQ:
5453 case OPERATOR_BITCLEAR:
5454 case OPERATOR_BITCLEAREQ:
5455 if (type->integer_type() == NULL)
5457 error_at(location, "expected integer type");
5472 Binary_expression::do_check_types(Gogo*)
5474 Type* left_type = this->left_->type();
5475 Type* right_type = this->right_->type();
5476 if (left_type->is_error_type() || right_type->is_error_type())
5479 if (this->op_ == OPERATOR_EQEQ
5480 || this->op_ == OPERATOR_NOTEQ
5481 || this->op_ == OPERATOR_LT
5482 || this->op_ == OPERATOR_LE
5483 || this->op_ == OPERATOR_GT
5484 || this->op_ == OPERATOR_GE)
5486 if (!Type::are_assignable(left_type, right_type, NULL)
5487 && !Type::are_assignable(right_type, left_type, NULL))
5489 this->report_error(_("incompatible types in binary expression"));
5492 if (!Binary_expression::check_operator_type(this->op_, left_type,
5494 || !Binary_expression::check_operator_type(this->op_, right_type,
5497 this->set_is_error();
5501 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5503 if (!Type::are_compatible_for_binop(left_type, right_type))
5505 this->report_error(_("incompatible types in binary expression"));
5508 if (!Binary_expression::check_operator_type(this->op_, left_type,
5511 this->set_is_error();
5517 if (left_type->integer_type() == NULL)
5518 this->report_error(_("shift of non-integer operand"));
5520 if (!right_type->is_abstract()
5521 && (right_type->integer_type() == NULL
5522 || !right_type->integer_type()->is_unsigned()))
5523 this->report_error(_("shift count not unsigned integer"));
5529 if (this->right_->integer_constant_value(true, val, &type))
5531 if (mpz_sgn(val) < 0)
5532 this->report_error(_("negative shift count"));
5539 // Get a tree for a binary expression.
5542 Binary_expression::do_get_tree(Translate_context* context)
5544 tree left = this->left_->get_tree(context);
5545 tree right = this->right_->get_tree(context);
5547 if (left == error_mark_node || right == error_mark_node)
5548 return error_mark_node;
5550 enum tree_code code;
5551 bool use_left_type = true;
5552 bool is_shift_op = false;
5556 case OPERATOR_NOTEQ:
5561 return Expression::comparison_tree(context, this->op_,
5562 this->left_->type(), left,
5563 this->right_->type(), right,
5567 code = TRUTH_ORIF_EXPR;
5568 use_left_type = false;
5570 case OPERATOR_ANDAND:
5571 code = TRUTH_ANDIF_EXPR;
5572 use_left_type = false;
5577 case OPERATOR_MINUS:
5581 code = BIT_IOR_EXPR;
5584 code = BIT_XOR_EXPR;
5591 Type *t = this->left_->type();
5592 if (t->float_type() != NULL || t->complex_type() != NULL)
5595 code = TRUNC_DIV_EXPR;
5599 code = TRUNC_MOD_EXPR;
5601 case OPERATOR_LSHIFT:
5605 case OPERATOR_RSHIFT:
5610 code = BIT_AND_EXPR;
5612 case OPERATOR_BITCLEAR:
5613 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5614 code = BIT_AND_EXPR;
5620 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5622 if (this->left_->type()->is_string_type())
5624 gcc_assert(this->op_ == OPERATOR_PLUS);
5625 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5626 static tree string_plus_decl;
5627 return Gogo::call_builtin(&string_plus_decl,
5638 tree compute_type = excess_precision_type(type);
5639 if (compute_type != NULL_TREE)
5641 left = ::convert(compute_type, left);
5642 right = ::convert(compute_type, right);
5645 tree eval_saved = NULL_TREE;
5649 left = save_expr(left);
5651 right = save_expr(right);
5652 // Make sure the values are evaluated.
5653 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
5654 void_type_node, left, right);
5657 tree ret = fold_build2_loc(this->location(),
5659 compute_type != NULL_TREE ? compute_type : type,
5662 if (compute_type != NULL_TREE)
5663 ret = ::convert(type, ret);
5665 // In Go, a shift larger than the size of the type is well-defined.
5666 // This is not true in GENERIC, so we need to insert a conditional.
5669 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
5670 gcc_assert(this->left_->type()->integer_type() != NULL);
5671 int bits = TYPE_PRECISION(TREE_TYPE(left));
5673 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
5674 build_int_cst_type(TREE_TYPE(right), bits));
5676 tree overflow_result = fold_convert_loc(this->location(),
5679 if (this->op_ == OPERATOR_RSHIFT
5680 && !this->left_->type()->integer_type()->is_unsigned())
5682 tree neg = fold_build2_loc(this->location(), LT_EXPR,
5683 boolean_type_node, left,
5684 fold_convert_loc(this->location(),
5686 integer_zero_node));
5687 tree neg_one = fold_build2_loc(this->location(),
5688 MINUS_EXPR, TREE_TYPE(left),
5689 fold_convert_loc(this->location(),
5692 fold_convert_loc(this->location(),
5695 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
5696 TREE_TYPE(left), neg, neg_one,
5700 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
5701 compare, ret, overflow_result);
5703 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
5704 TREE_TYPE(ret), eval_saved, ret);
5710 // Export a binary expression.
5713 Binary_expression::do_export(Export* exp) const
5715 exp->write_c_string("(");
5716 this->left_->export_expression(exp);
5720 exp->write_c_string(" || ");
5722 case OPERATOR_ANDAND:
5723 exp->write_c_string(" && ");
5726 exp->write_c_string(" == ");
5728 case OPERATOR_NOTEQ:
5729 exp->write_c_string(" != ");
5732 exp->write_c_string(" < ");
5735 exp->write_c_string(" <= ");
5738 exp->write_c_string(" > ");
5741 exp->write_c_string(" >= ");
5744 exp->write_c_string(" + ");
5746 case OPERATOR_MINUS:
5747 exp->write_c_string(" - ");
5750 exp->write_c_string(" | ");
5753 exp->write_c_string(" ^ ");
5756 exp->write_c_string(" * ");
5759 exp->write_c_string(" / ");
5762 exp->write_c_string(" % ");
5764 case OPERATOR_LSHIFT:
5765 exp->write_c_string(" << ");
5767 case OPERATOR_RSHIFT:
5768 exp->write_c_string(" >> ");
5771 exp->write_c_string(" & ");
5773 case OPERATOR_BITCLEAR:
5774 exp->write_c_string(" &^ ");
5779 this->right_->export_expression(exp);
5780 exp->write_c_string(")");
5783 // Import a binary expression.
5786 Binary_expression::do_import(Import* imp)
5788 imp->require_c_string("(");
5790 Expression* left = Expression::import_expression(imp);
5793 if (imp->match_c_string(" || "))
5798 else if (imp->match_c_string(" && "))
5800 op = OPERATOR_ANDAND;
5803 else if (imp->match_c_string(" == "))
5808 else if (imp->match_c_string(" != "))
5810 op = OPERATOR_NOTEQ;
5813 else if (imp->match_c_string(" < "))
5818 else if (imp->match_c_string(" <= "))
5823 else if (imp->match_c_string(" > "))
5828 else if (imp->match_c_string(" >= "))
5833 else if (imp->match_c_string(" + "))
5838 else if (imp->match_c_string(" - "))
5840 op = OPERATOR_MINUS;
5843 else if (imp->match_c_string(" | "))
5848 else if (imp->match_c_string(" ^ "))
5853 else if (imp->match_c_string(" * "))
5858 else if (imp->match_c_string(" / "))
5863 else if (imp->match_c_string(" % "))
5868 else if (imp->match_c_string(" << "))
5870 op = OPERATOR_LSHIFT;
5873 else if (imp->match_c_string(" >> "))
5875 op = OPERATOR_RSHIFT;
5878 else if (imp->match_c_string(" & "))
5883 else if (imp->match_c_string(" &^ "))
5885 op = OPERATOR_BITCLEAR;
5890 error_at(imp->location(), "unrecognized binary operator");
5891 return Expression::make_error(imp->location());
5894 Expression* right = Expression::import_expression(imp);
5896 imp->require_c_string(")");
5898 return Expression::make_binary(op, left, right, imp->location());
5901 // Make a binary expression.
5904 Expression::make_binary(Operator op, Expression* left, Expression* right,
5905 source_location location)
5907 return new Binary_expression(op, left, right, location);
5910 // Implement a comparison.
5913 Expression::comparison_tree(Translate_context* context, Operator op,
5914 Type* left_type, tree left_tree,
5915 Type* right_type, tree right_tree,
5916 source_location location)
5918 enum tree_code code;
5924 case OPERATOR_NOTEQ:
5943 if (left_type->is_string_type())
5945 gcc_assert(right_type->is_string_type());
5946 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5947 static tree string_compare_decl;
5948 left_tree = Gogo::call_builtin(&string_compare_decl,
5957 right_tree = build_int_cst_type(integer_type_node, 0);
5960 if ((left_type->interface_type() != NULL
5961 && right_type->interface_type() == NULL
5962 && !right_type->is_nil_type())
5963 || (left_type->interface_type() == NULL
5964 && !left_type->is_nil_type()
5965 && right_type->interface_type() != NULL))
5967 // Comparing an interface value to a non-interface value.
5968 if (left_type->interface_type() == NULL)
5970 std::swap(left_type, right_type);
5971 std::swap(left_tree, right_tree);
5974 // The right operand is not an interface. We need to take its
5975 // address if it is not a pointer.
5978 if (right_type->points_to() != NULL)
5980 make_tmp = NULL_TREE;
5983 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
5985 make_tmp = NULL_TREE;
5986 arg = build_fold_addr_expr_loc(location, right_tree);
5987 if (DECL_P(right_tree))
5988 TREE_ADDRESSABLE(right_tree) = 1;
5992 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
5993 get_name(right_tree));
5994 DECL_IGNORED_P(tmp) = 0;
5995 DECL_INITIAL(tmp) = right_tree;
5996 TREE_ADDRESSABLE(tmp) = 1;
5997 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
5998 SET_EXPR_LOCATION(make_tmp, location);
5999 arg = build_fold_addr_expr_loc(location, tmp);
6001 arg = fold_convert_loc(location, ptr_type_node, arg);
6003 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
6005 if (left_type->interface_type()->is_empty())
6007 static tree empty_interface_value_compare_decl;
6008 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6010 "__go_empty_interface_value_compare",
6013 TREE_TYPE(left_tree),
6015 TREE_TYPE(descriptor),
6019 // This can panic if the type is not comparable.
6020 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6024 static tree interface_value_compare_decl;
6025 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6027 "__go_interface_value_compare",
6030 TREE_TYPE(left_tree),
6032 TREE_TYPE(descriptor),
6036 // This can panic if the type is not comparable.
6037 TREE_NOTHROW(interface_value_compare_decl) = 0;
6039 right_tree = build_int_cst_type(integer_type_node, 0);
6041 if (make_tmp != NULL_TREE)
6042 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6045 else if (left_type->interface_type() != NULL
6046 && right_type->interface_type() != NULL)
6048 if (left_type->interface_type()->is_empty())
6050 gcc_assert(right_type->interface_type()->is_empty());
6051 static tree empty_interface_compare_decl;
6052 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6054 "__go_empty_interface_compare",
6057 TREE_TYPE(left_tree),
6059 TREE_TYPE(right_tree),
6061 // This can panic if the type is uncomparable.
6062 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6066 gcc_assert(!right_type->interface_type()->is_empty());
6067 static tree interface_compare_decl;
6068 left_tree = Gogo::call_builtin(&interface_compare_decl,
6070 "__go_interface_compare",
6073 TREE_TYPE(left_tree),
6075 TREE_TYPE(right_tree),
6077 // This can panic if the type is uncomparable.
6078 TREE_NOTHROW(interface_compare_decl) = 0;
6080 right_tree = build_int_cst_type(integer_type_node, 0);
6083 if (left_type->is_nil_type()
6084 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6086 std::swap(left_type, right_type);
6087 std::swap(left_tree, right_tree);
6090 if (right_type->is_nil_type())
6092 if (left_type->array_type() != NULL
6093 && left_type->array_type()->length() == NULL)
6095 Array_type* at = left_type->array_type();
6096 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6097 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6099 else if (left_type->interface_type() != NULL)
6101 // An interface is nil if the first field is nil.
6102 tree left_type_tree = TREE_TYPE(left_tree);
6103 gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6104 tree field = TYPE_FIELDS(left_type_tree);
6105 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6107 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6111 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6112 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6116 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6117 if (CAN_HAVE_LOCATION_P(ret))
6118 SET_EXPR_LOCATION(ret, location);
6122 // Class Bound_method_expression.
6127 Bound_method_expression::do_traverse(Traverse* traverse)
6129 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6130 return TRAVERSE_EXIT;
6131 return Expression::traverse(&this->method_, traverse);
6134 // Return the type of a bound method expression. The type of this
6135 // object is really the type of the method with no receiver. We
6136 // should be able to get away with just returning the type of the
6140 Bound_method_expression::do_type()
6142 return this->method_->type();
6145 // Determine the types of a method expression.
6148 Bound_method_expression::do_determine_type(const Type_context*)
6150 this->method_->determine_type_no_context();
6151 Type* mtype = this->method_->type();
6152 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6153 if (fntype == NULL || !fntype->is_method())
6154 this->expr_->determine_type_no_context();
6157 Type_context subcontext(fntype->receiver()->type(), false);
6158 this->expr_->determine_type(&subcontext);
6162 // Check the types of a method expression.
6165 Bound_method_expression::do_check_types(Gogo*)
6167 Type* type = this->method_->type()->deref();
6169 || type->function_type() == NULL
6170 || !type->function_type()->is_method())
6171 this->report_error(_("object is not a method"));
6174 Type* rtype = type->function_type()->receiver()->type()->deref();
6175 Type* etype = (this->expr_type_ != NULL
6177 : this->expr_->type());
6178 etype = etype->deref();
6179 if (!Type::are_identical(rtype, etype, NULL))
6180 this->report_error(_("method type does not match object type"));
6184 // Get the tree for a method expression. There is no standard tree
6185 // representation for this. The only places it may currently be used
6186 // are in a Call_expression or a Go_statement, which will take it
6187 // apart directly. So this has nothing to do at present.
6190 Bound_method_expression::do_get_tree(Translate_context*)
6195 // Make a method expression.
6197 Bound_method_expression*
6198 Expression::make_bound_method(Expression* expr, Expression* method,
6199 source_location location)
6201 return new Bound_method_expression(expr, method, location);
6204 // Class Builtin_call_expression. This is used for a call to a
6205 // builtin function.
6207 class Builtin_call_expression : public Call_expression
6210 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6211 bool is_varargs, source_location location);
6214 // This overrides Call_expression::do_lower.
6216 do_lower(Gogo*, Named_object*, int);
6219 do_is_constant() const;
6222 do_integer_constant_value(bool, mpz_t, Type**) const;
6225 do_float_constant_value(mpfr_t, Type**) const;
6228 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6234 do_determine_type(const Type_context*);
6237 do_check_types(Gogo*);
6242 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6243 this->args()->copy(),
6249 do_get_tree(Translate_context*);
6252 do_export(Export*) const;
6255 do_is_recover_call() const;
6258 do_set_recover_arg(Expression*);
6261 // The builtin functions.
6262 enum Builtin_function_code
6266 // Predeclared builtin functions.
6283 // Builtin functions from the unsafe package.
6296 real_imag_type(Type*);
6301 // A pointer back to the general IR structure. This avoids a global
6302 // variable, or passing it around everywhere.
6304 // The builtin function being called.
6305 Builtin_function_code code_;
6308 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6310 Expression_list* args,
6312 source_location location)
6313 : Call_expression(fn, args, is_varargs, location),
6314 gogo_(gogo), code_(BUILTIN_INVALID)
6316 Func_expression* fnexp = this->fn()->func_expression();
6317 gcc_assert(fnexp != NULL);
6318 const std::string& name(fnexp->named_object()->name());
6319 if (name == "append")
6320 this->code_ = BUILTIN_APPEND;
6321 else if (name == "cap")
6322 this->code_ = BUILTIN_CAP;
6323 else if (name == "close")
6324 this->code_ = BUILTIN_CLOSE;
6325 else if (name == "closed")
6326 this->code_ = BUILTIN_CLOSED;
6327 else if (name == "cmplx")
6328 this->code_ = BUILTIN_CMPLX;
6329 else if (name == "copy")
6330 this->code_ = BUILTIN_COPY;
6331 else if (name == "imag")
6332 this->code_ = BUILTIN_IMAG;
6333 else if (name == "len")
6334 this->code_ = BUILTIN_LEN;
6335 else if (name == "make")
6336 this->code_ = BUILTIN_MAKE;
6337 else if (name == "new")
6338 this->code_ = BUILTIN_NEW;
6339 else if (name == "panic")
6340 this->code_ = BUILTIN_PANIC;
6341 else if (name == "print")
6342 this->code_ = BUILTIN_PRINT;
6343 else if (name == "println")
6344 this->code_ = BUILTIN_PRINTLN;
6345 else if (name == "real")
6346 this->code_ = BUILTIN_REAL;
6347 else if (name == "recover")
6348 this->code_ = BUILTIN_RECOVER;
6349 else if (name == "Alignof")
6350 this->code_ = BUILTIN_ALIGNOF;
6351 else if (name == "Offsetof")
6352 this->code_ = BUILTIN_OFFSETOF;
6353 else if (name == "Sizeof")
6354 this->code_ = BUILTIN_SIZEOF;
6359 // Return whether this is a call to recover. This is a virtual
6360 // function called from the parent class.
6363 Builtin_call_expression::do_is_recover_call() const
6365 if (this->classification() == EXPRESSION_ERROR)
6367 return this->code_ == BUILTIN_RECOVER;
6370 // Set the argument for a call to recover.
6373 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6375 const Expression_list* args = this->args();
6376 gcc_assert(args == NULL || args->empty());
6377 Expression_list* new_args = new Expression_list();
6378 new_args->push_back(arg);
6379 this->set_args(new_args);
6382 // A traversal class which looks for a call expression.
6384 class Find_call_expression : public Traverse
6387 Find_call_expression()
6388 : Traverse(traverse_expressions),
6393 expression(Expression**);
6397 { return this->found_; }
6404 Find_call_expression::expression(Expression** pexpr)
6406 if ((*pexpr)->call_expression() != NULL)
6408 this->found_ = true;
6409 return TRAVERSE_EXIT;
6411 return TRAVERSE_CONTINUE;
6414 // Lower a builtin call expression. This turns new and make into
6415 // specific expressions. We also convert to a constant if we can.
6418 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6420 if (this->code_ == BUILTIN_NEW)
6422 const Expression_list* args = this->args();
6423 if (args == NULL || args->size() < 1)
6424 this->report_error(_("not enough arguments"));
6425 else if (args->size() > 1)
6426 this->report_error(_("too many arguments"));
6429 Expression* arg = args->front();
6430 if (!arg->is_type_expression())
6432 error_at(arg->location(), "expected type");
6433 this->set_is_error();
6436 return Expression::make_allocation(arg->type(), this->location());
6439 else if (this->code_ == BUILTIN_MAKE)
6441 const Expression_list* args = this->args();
6442 if (args == NULL || args->size() < 1)
6443 this->report_error(_("not enough arguments"));
6446 Expression* arg = args->front();
6447 if (!arg->is_type_expression())
6449 error_at(arg->location(), "expected type");
6450 this->set_is_error();
6454 Expression_list* newargs;
6455 if (args->size() == 1)
6459 newargs = new Expression_list();
6460 Expression_list::const_iterator p = args->begin();
6462 for (; p != args->end(); ++p)
6463 newargs->push_back(*p);
6465 return Expression::make_make(arg->type(), newargs,
6470 else if (this->is_constant())
6472 // We can only lower len and cap if there are no function calls
6473 // in the arguments. Otherwise we have to make the call.
6474 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6476 Expression* arg = this->one_arg();
6477 if (!arg->is_constant())
6479 Find_call_expression find_call;
6480 Expression::traverse(&arg, &find_call);
6481 if (find_call.found())
6489 if (this->integer_constant_value(true, ival, &type))
6491 Expression* ret = Expression::make_integer(&ival, type,
6500 if (this->float_constant_value(rval, &type))
6502 Expression* ret = Expression::make_float(&rval, type,
6510 if (this->complex_constant_value(rval, imag, &type))
6512 Expression* ret = Expression::make_complex(&rval, &imag, type,
6521 else if (this->code_ == BUILTIN_RECOVER)
6523 if (function != NULL)
6524 function->func_value()->set_calls_recover();
6527 // Calling recover outside of a function always returns the
6528 // nil empty interface.
6529 Type* eface = Type::make_interface_type(NULL, this->location());
6530 return Expression::make_cast(eface,
6531 Expression::make_nil(this->location()),
6535 else if (this->code_ == BUILTIN_APPEND)
6537 // Lower the varargs.
6538 const Expression_list* args = this->args();
6539 if (args == NULL || args->empty())
6541 Type* slice_type = args->front()->type();
6542 if (!slice_type->is_open_array_type())
6544 error_at(args->front()->location(), "argument 1 must be a slice");
6545 this->set_is_error();
6548 return this->lower_varargs(gogo, function, slice_type, 2);
6554 // Return the type of the real or imag functions, given the type of
6555 // the argument. We need to map complex to float, complex64 to
6556 // float32, and complex128 to float64, so it has to be done by name.
6557 // This returns NULL if it can't figure out the type.
6560 Builtin_call_expression::real_imag_type(Type* arg_type)
6562 if (arg_type == NULL || arg_type->is_abstract())
6564 Named_type* nt = arg_type->named_type();
6567 while (nt->real_type()->named_type() != NULL)
6568 nt = nt->real_type()->named_type();
6569 if (nt->name() == "complex")
6570 return Type::lookup_float_type("float");
6571 else if (nt->name() == "complex64")
6572 return Type::lookup_float_type("float32");
6573 else if (nt->name() == "complex128")
6574 return Type::lookup_float_type("float64");
6579 // Return the type of the cmplx function, given the type of one of the
6580 // argments. Like real_imag_type, we have to map by name.
6583 Builtin_call_expression::cmplx_type(Type* arg_type)
6585 if (arg_type == NULL || arg_type->is_abstract())
6587 Named_type* nt = arg_type->named_type();
6590 while (nt->real_type()->named_type() != NULL)
6591 nt = nt->real_type()->named_type();
6592 if (nt->name() == "float")
6593 return Type::lookup_complex_type("complex");
6594 else if (nt->name() == "float32")
6595 return Type::lookup_complex_type("complex64");
6596 else if (nt->name() == "float64")
6597 return Type::lookup_complex_type("complex128");
6602 // Return a single argument, or NULL if there isn't one.
6605 Builtin_call_expression::one_arg() const
6607 const Expression_list* args = this->args();
6608 if (args->size() != 1)
6610 return args->front();
6613 // Return whether this is constant: len of a string, or len or cap of
6614 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6617 Builtin_call_expression::do_is_constant() const
6619 switch (this->code_)
6624 Expression* arg = this->one_arg();
6627 Type* arg_type = arg->type();
6629 if (arg_type->points_to() != NULL
6630 && arg_type->points_to()->array_type() != NULL
6631 && !arg_type->points_to()->is_open_array_type())
6632 arg_type = arg_type->points_to();
6634 if (arg_type->array_type() != NULL
6635 && arg_type->array_type()->length() != NULL)
6636 return arg_type->array_type()->length()->is_constant();
6638 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6639 return arg->is_constant();
6643 case BUILTIN_SIZEOF:
6644 case BUILTIN_ALIGNOF:
6645 return this->one_arg() != NULL;
6647 case BUILTIN_OFFSETOF:
6649 Expression* arg = this->one_arg();
6652 return arg->field_reference_expression() != NULL;
6657 const Expression_list* args = this->args();
6658 if (args != NULL && args->size() == 2)
6659 return args->front()->is_constant() && args->back()->is_constant();
6666 Expression* arg = this->one_arg();
6667 return arg != NULL && arg->is_constant();
6677 // Return an integer constant value if possible.
6680 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
6684 if (this->code_ == BUILTIN_LEN
6685 || this->code_ == BUILTIN_CAP)
6687 Expression* arg = this->one_arg();
6690 Type* arg_type = arg->type();
6692 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6695 if (arg->string_constant_value(&sval))
6697 mpz_set_ui(val, sval.length());
6698 *ptype = Type::lookup_integer_type("int");
6703 if (arg_type->points_to() != NULL
6704 && arg_type->points_to()->array_type() != NULL
6705 && !arg_type->points_to()->is_open_array_type())
6706 arg_type = arg_type->points_to();
6708 if (arg_type->array_type() != NULL
6709 && arg_type->array_type()->length() != NULL)
6711 Expression* e = arg_type->array_type()->length();
6712 if (e->integer_constant_value(iota_is_constant, val, ptype))
6714 *ptype = Type::lookup_integer_type("int");
6719 else if (this->code_ == BUILTIN_SIZEOF
6720 || this->code_ == BUILTIN_ALIGNOF)
6722 Expression* arg = this->one_arg();
6725 Type* arg_type = arg->type();
6726 if (arg_type->is_error_type())
6728 if (arg_type->is_abstract())
6730 tree arg_type_tree = arg_type->get_tree(this->gogo_);
6731 unsigned long val_long;
6732 if (this->code_ == BUILTIN_SIZEOF)
6734 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
6735 gcc_assert(TREE_CODE(type_size) == INTEGER_CST);
6736 if (TREE_INT_CST_HIGH(type_size) != 0)
6738 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
6739 val_long = static_cast<unsigned long>(val_wide);
6740 if (val_long != val_wide)
6743 else if (this->code_ == BUILTIN_ALIGNOF)
6745 if (arg->field_reference_expression() == NULL)
6746 val_long = go_type_alignment(arg_type_tree);
6749 // Calling unsafe.Alignof(s.f) returns the alignment of
6750 // the type of f when it is used as a field in a struct.
6751 val_long = go_field_alignment(arg_type_tree);
6756 mpz_set_ui(val, val_long);
6760 else if (this->code_ == BUILTIN_OFFSETOF)
6762 Expression* arg = this->one_arg();
6765 Field_reference_expression* farg = arg->field_reference_expression();
6768 Expression* struct_expr = farg->expr();
6769 Type* st = struct_expr->type();
6770 if (st->struct_type() == NULL)
6772 tree struct_tree = st->get_tree(this->gogo_);
6773 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
6774 tree field = TYPE_FIELDS(struct_tree);
6775 for (unsigned int index = farg->field_index(); index > 0; --index)
6777 field = DECL_CHAIN(field);
6778 gcc_assert(field != NULL_TREE);
6780 HOST_WIDE_INT offset_wide = int_byte_position (field);
6781 if (offset_wide < 0)
6783 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
6784 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
6786 mpz_set_ui(val, offset_long);
6792 // Return a floating point constant value if possible.
6795 Builtin_call_expression::do_float_constant_value(mpfr_t val,
6798 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
6800 Expression* arg = this->one_arg();
6811 if (arg->complex_constant_value(real, imag, &type))
6813 if (this->code_ == BUILTIN_REAL)
6814 mpfr_set(val, real, GMP_RNDN);
6816 mpfr_set(val, imag, GMP_RNDN);
6817 *ptype = Builtin_call_expression::real_imag_type(type);
6829 // Return a complex constant value if possible.
6832 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
6835 if (this->code_ == BUILTIN_CMPLX)
6837 const Expression_list* args = this->args();
6838 if (args == NULL || args->size() != 2)
6844 if (!args->front()->float_constant_value(r, &rtype))
6855 if (args->back()->float_constant_value(i, &itype)
6856 && Type::are_identical(rtype, itype, NULL))
6858 mpfr_set(real, r, GMP_RNDN);
6859 mpfr_set(imag, i, GMP_RNDN);
6860 *ptype = Builtin_call_expression::cmplx_type(rtype);
6876 Builtin_call_expression::do_type()
6878 switch (this->code_)
6880 case BUILTIN_INVALID:
6887 const Expression_list* args = this->args();
6888 if (args == NULL || args->empty())
6889 return Type::make_error_type();
6890 return Type::make_pointer_type(args->front()->type());
6896 case BUILTIN_ALIGNOF:
6897 case BUILTIN_OFFSETOF:
6898 case BUILTIN_SIZEOF:
6899 return Type::lookup_integer_type("int");
6904 case BUILTIN_PRINTLN:
6905 return Type::make_void_type();
6907 case BUILTIN_CLOSED:
6908 return Type::lookup_bool_type();
6910 case BUILTIN_RECOVER:
6911 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
6913 case BUILTIN_APPEND:
6915 const Expression_list* args = this->args();
6916 if (args == NULL || args->empty())
6917 return Type::make_error_type();
6918 return args->front()->type();
6924 Expression* arg = this->one_arg();
6926 return Type::make_error_type();
6927 Type* t = arg->type();
6928 if (t->is_abstract())
6929 t = t->make_non_abstract_type();
6930 t = Builtin_call_expression::real_imag_type(t);
6932 t = Type::make_error_type();
6938 const Expression_list* args = this->args();
6939 if (args == NULL || args->size() != 2)
6940 return Type::make_error_type();
6941 Type* t = args->front()->type();
6942 if (t->is_abstract())
6944 t = args->back()->type();
6945 if (t->is_abstract())
6946 t = t->make_non_abstract_type();
6948 t = Builtin_call_expression::cmplx_type(t);
6950 t = Type::make_error_type();
6956 // Determine the type.
6959 Builtin_call_expression::do_determine_type(const Type_context* context)
6961 this->fn()->determine_type_no_context();
6963 const Expression_list* args = this->args();
6966 Type* arg_type = NULL;
6967 switch (this->code_)
6970 case BUILTIN_PRINTLN:
6971 // Do not force a large integer constant to "int".
6977 arg_type = Builtin_call_expression::cmplx_type(context->type);
6983 // For the cmplx function the type of one operand can
6984 // determine the type of the other, as in a binary expression.
6985 arg_type = Builtin_call_expression::real_imag_type(context->type);
6986 if (args != NULL && args->size() == 2)
6988 Type* t1 = args->front()->type();
6989 Type* t2 = args->front()->type();
6990 if (!t1->is_abstract())
6992 else if (!t2->is_abstract())
7006 for (Expression_list::const_iterator pa = args->begin();
7010 Type_context subcontext;
7011 subcontext.type = arg_type;
7015 // We want to print large constants, we so can't just
7016 // use the appropriate nonabstract type. Use uint64 for
7017 // an integer if we know it is nonnegative, otherwise
7018 // use int64 for a integer, otherwise use float64 for a
7019 // float or complex128 for a complex.
7020 Type* want_type = NULL;
7021 Type* atype = (*pa)->type();
7022 if (atype->is_abstract())
7024 if (atype->integer_type() != NULL)
7029 if (this->integer_constant_value(true, val, &dummy)
7030 && mpz_sgn(val) >= 0)
7031 want_type = Type::lookup_integer_type("uint64");
7033 want_type = Type::lookup_integer_type("int64");
7036 else if (atype->float_type() != NULL)
7037 want_type = Type::lookup_float_type("float64");
7038 else if (atype->complex_type() != NULL)
7039 want_type = Type::lookup_complex_type("complex128");
7040 else if (atype->is_abstract_string_type())
7041 want_type = Type::lookup_string_type();
7042 else if (atype->is_abstract_boolean_type())
7043 want_type = Type::lookup_bool_type();
7046 subcontext.type = want_type;
7050 (*pa)->determine_type(&subcontext);
7055 // If there is exactly one argument, return true. Otherwise give an
7056 // error message and return false.
7059 Builtin_call_expression::check_one_arg()
7061 const Expression_list* args = this->args();
7062 if (args == NULL || args->size() < 1)
7064 this->report_error(_("not enough arguments"));
7067 else if (args->size() > 1)
7069 this->report_error(_("too many arguments"));
7072 if (args->front()->is_error_expression()
7073 || args->front()->type()->is_error_type())
7075 this->set_is_error();
7081 // Check argument types for a builtin function.
7084 Builtin_call_expression::do_check_types(Gogo*)
7086 switch (this->code_)
7088 case BUILTIN_INVALID:
7096 // The single argument may be either a string or an array or a
7097 // map or a channel, or a pointer to a closed array.
7098 if (this->check_one_arg())
7100 Type* arg_type = this->one_arg()->type();
7101 if (arg_type->points_to() != NULL
7102 && arg_type->points_to()->array_type() != NULL
7103 && !arg_type->points_to()->is_open_array_type())
7104 arg_type = arg_type->points_to();
7105 if (this->code_ == BUILTIN_CAP)
7107 if (!arg_type->is_error_type()
7108 && arg_type->array_type() == NULL
7109 && arg_type->channel_type() == NULL)
7110 this->report_error(_("argument must be array or slice "
7115 if (!arg_type->is_error_type()
7116 && !arg_type->is_string_type()
7117 && arg_type->array_type() == NULL
7118 && arg_type->map_type() == NULL
7119 && arg_type->channel_type() == NULL)
7120 this->report_error(_("argument must be string or "
7121 "array or slice or map or channel"));
7128 case BUILTIN_PRINTLN:
7130 const Expression_list* args = this->args();
7133 if (this->code_ == BUILTIN_PRINT)
7134 warning_at(this->location(), 0,
7135 "no arguments for builtin function %<%s%>",
7136 (this->code_ == BUILTIN_PRINT
7142 for (Expression_list::const_iterator p = args->begin();
7146 Type* type = (*p)->type();
7147 if (type->is_error_type()
7148 || type->is_string_type()
7149 || type->integer_type() != NULL
7150 || type->float_type() != NULL
7151 || type->complex_type() != NULL
7152 || type->is_boolean_type()
7153 || type->points_to() != NULL
7154 || type->interface_type() != NULL
7155 || type->channel_type() != NULL
7156 || type->map_type() != NULL
7157 || type->function_type() != NULL
7158 || type->is_open_array_type())
7161 this->report_error(_("unsupported argument type to "
7162 "builtin function"));
7169 case BUILTIN_CLOSED:
7170 if (this->check_one_arg())
7172 if (this->one_arg()->type()->channel_type() == NULL)
7173 this->report_error(_("argument must be channel"));
7178 case BUILTIN_SIZEOF:
7179 case BUILTIN_ALIGNOF:
7180 this->check_one_arg();
7183 case BUILTIN_RECOVER:
7184 if (this->args() != NULL && !this->args()->empty())
7185 this->report_error(_("too many arguments"));
7188 case BUILTIN_OFFSETOF:
7189 if (this->check_one_arg())
7191 Expression* arg = this->one_arg();
7192 if (arg->field_reference_expression() == NULL)
7193 this->report_error(_("argument must be a field reference"));
7199 const Expression_list* args = this->args();
7200 if (args == NULL || args->size() < 2)
7202 this->report_error(_("not enough arguments"));
7205 else if (args->size() > 2)
7207 this->report_error(_("too many arguments"));
7210 Type* arg1_type = args->front()->type();
7211 Type* arg2_type = args->back()->type();
7212 if (arg1_type->is_error_type() || arg2_type->is_error_type())
7216 if (arg1_type->is_open_array_type())
7217 e1 = arg1_type->array_type()->element_type();
7220 this->report_error(_("left argument must be a slice"));
7225 if (arg2_type->is_open_array_type())
7226 e2 = arg2_type->array_type()->element_type();
7227 else if (arg2_type->is_string_type())
7228 e2 = Type::lookup_integer_type("uint8");
7231 this->report_error(_("right argument must be a slice or a string"));
7235 if (!Type::are_identical(e1, e2, NULL))
7236 this->report_error(_("element types must be the same"));
7240 case BUILTIN_APPEND:
7242 const Expression_list* args = this->args();
7243 if (args == NULL || args->empty())
7245 this->report_error(_("not enough arguments"));
7248 /* Lowering varargs should have left us with 2 arguments. */
7249 gcc_assert(args->size() == 2);
7251 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7255 this->report_error(_("arguments 1 and 2 have different types"));
7258 error_at(this->location(),
7259 "arguments 1 and 2 have different types (%s)",
7261 this->set_is_error();
7269 if (this->check_one_arg())
7271 if (this->one_arg()->type()->complex_type() == NULL)
7272 this->report_error(_("argument must have complex type"));
7278 const Expression_list* args = this->args();
7279 if (args == NULL || args->size() < 2)
7280 this->report_error(_("not enough arguments"));
7281 else if (args->size() > 2)
7282 this->report_error(_("too many arguments"));
7283 else if (args->front()->is_error_expression()
7284 || args->front()->type()->is_error_type()
7285 || args->back()->is_error_expression()
7286 || args->back()->type()->is_error_type())
7287 this->set_is_error();
7288 else if (!Type::are_identical(args->front()->type(),
7289 args->back()->type(), NULL))
7290 this->report_error(_("cmplx arguments must have identical types"));
7291 else if (args->front()->type()->float_type() == NULL)
7292 this->report_error(_("cmplx arguments must have "
7293 "floating-point type"));
7302 // Return the tree for a builtin function.
7305 Builtin_call_expression::do_get_tree(Translate_context* context)
7307 Gogo* gogo = context->gogo();
7308 source_location location = this->location();
7309 switch (this->code_)
7311 case BUILTIN_INVALID:
7319 const Expression_list* args = this->args();
7320 gcc_assert(args != NULL && args->size() == 1);
7321 Expression* arg = *args->begin();
7322 Type* arg_type = arg->type();
7323 tree arg_tree = arg->get_tree(context);
7324 if (arg_tree == error_mark_node)
7325 return error_mark_node;
7327 if (arg_type->points_to() != NULL)
7329 arg_type = arg_type->points_to();
7330 gcc_assert(arg_type->array_type() != NULL
7331 && !arg_type->is_open_array_type());
7332 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7333 arg_tree = build_fold_indirect_ref(arg_tree);
7337 if (this->code_ == BUILTIN_LEN)
7339 if (arg_type->is_string_type())
7340 val_tree = String_type::length_tree(gogo, arg_tree);
7341 else if (arg_type->array_type() != NULL)
7342 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7343 else if (arg_type->map_type() != NULL)
7345 static tree map_len_fndecl;
7346 val_tree = Gogo::call_builtin(&map_len_fndecl,
7351 arg_type->get_tree(gogo),
7354 else if (arg_type->channel_type() != NULL)
7356 static tree chan_len_fndecl;
7357 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7362 arg_type->get_tree(gogo),
7370 if (arg_type->array_type() != NULL)
7371 val_tree = arg_type->array_type()->capacity_tree(gogo, arg_tree);
7372 else if (arg_type->channel_type() != NULL)
7374 static tree chan_cap_fndecl;
7375 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7380 arg_type->get_tree(gogo),
7387 tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo);
7388 if (type_tree == TREE_TYPE(val_tree))
7391 return fold(convert_to_integer(type_tree, val_tree));
7395 case BUILTIN_PRINTLN:
7397 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7398 tree stmt_list = NULL_TREE;
7400 const Expression_list* call_args = this->args();
7401 if (call_args != NULL)
7403 for (Expression_list::const_iterator p = call_args->begin();
7404 p != call_args->end();
7407 if (is_ln && p != call_args->begin())
7409 static tree print_space_fndecl;
7410 tree call = Gogo::call_builtin(&print_space_fndecl,
7415 append_to_statement_list(call, &stmt_list);
7418 Type* type = (*p)->type();
7420 tree arg = (*p)->get_tree(context);
7421 if (arg == error_mark_node)
7422 return error_mark_node;
7426 if (type->is_string_type())
7428 static tree print_string_fndecl;
7429 pfndecl = &print_string_fndecl;
7430 fnname = "__go_print_string";
7432 else if (type->integer_type() != NULL
7433 && type->integer_type()->is_unsigned())
7435 static tree print_uint64_fndecl;
7436 pfndecl = &print_uint64_fndecl;
7437 fnname = "__go_print_uint64";
7438 Type* itype = Type::lookup_integer_type("uint64");
7439 arg = fold_convert_loc(location, itype->get_tree(gogo),
7442 else if (type->integer_type() != NULL)
7444 static tree print_int64_fndecl;
7445 pfndecl = &print_int64_fndecl;
7446 fnname = "__go_print_int64";
7447 Type* itype = Type::lookup_integer_type("int64");
7448 arg = fold_convert_loc(location, itype->get_tree(gogo),
7451 else if (type->float_type() != NULL)
7453 static tree print_double_fndecl;
7454 pfndecl = &print_double_fndecl;
7455 fnname = "__go_print_double";
7456 arg = fold_convert_loc(location, double_type_node, arg);
7458 else if (type->complex_type() != NULL)
7460 static tree print_complex_fndecl;
7461 pfndecl = &print_complex_fndecl;
7462 fnname = "__go_print_complex";
7463 arg = fold_convert_loc(location, complex_double_type_node,
7466 else if (type->is_boolean_type())
7468 static tree print_bool_fndecl;
7469 pfndecl = &print_bool_fndecl;
7470 fnname = "__go_print_bool";
7472 else if (type->points_to() != NULL
7473 || type->channel_type() != NULL
7474 || type->map_type() != NULL
7475 || type->function_type() != NULL)
7477 static tree print_pointer_fndecl;
7478 pfndecl = &print_pointer_fndecl;
7479 fnname = "__go_print_pointer";
7480 arg = fold_convert_loc(location, ptr_type_node, arg);
7482 else if (type->interface_type() != NULL)
7484 if (type->interface_type()->is_empty())
7486 static tree print_empty_interface_fndecl;
7487 pfndecl = &print_empty_interface_fndecl;
7488 fnname = "__go_print_empty_interface";
7492 static tree print_interface_fndecl;
7493 pfndecl = &print_interface_fndecl;
7494 fnname = "__go_print_interface";
7497 else if (type->is_open_array_type())
7499 static tree print_slice_fndecl;
7500 pfndecl = &print_slice_fndecl;
7501 fnname = "__go_print_slice";
7506 tree call = Gogo::call_builtin(pfndecl,
7513 append_to_statement_list(call, &stmt_list);
7519 static tree print_nl_fndecl;
7520 tree call = Gogo::call_builtin(&print_nl_fndecl,
7525 append_to_statement_list(call, &stmt_list);
7533 const Expression_list* args = this->args();
7534 gcc_assert(args != NULL && args->size() == 1);
7535 Expression* arg = args->front();
7536 tree arg_tree = arg->get_tree(context);
7537 if (arg_tree == error_mark_node)
7538 return error_mark_node;
7539 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7540 arg_tree = Expression::convert_for_assignment(context, empty,
7542 arg_tree, location);
7543 static tree panic_fndecl;
7544 tree call = Gogo::call_builtin(&panic_fndecl,
7549 TREE_TYPE(arg_tree),
7551 // This function will throw an exception.
7552 TREE_NOTHROW(panic_fndecl) = 0;
7553 // This function will not return.
7554 TREE_THIS_VOLATILE(panic_fndecl) = 1;
7558 case BUILTIN_RECOVER:
7560 // The argument is set when building recover thunks. It's a
7561 // boolean value which is true if we can recover a value now.
7562 const Expression_list* args = this->args();
7563 gcc_assert(args != NULL && args->size() == 1);
7564 Expression* arg = args->front();
7565 tree arg_tree = arg->get_tree(context);
7566 if (arg_tree == error_mark_node)
7567 return error_mark_node;
7569 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7570 tree empty_tree = empty->get_tree(context->gogo());
7572 Type* nil_type = Type::make_nil_type();
7573 Expression* nil = Expression::make_nil(location);
7574 tree nil_tree = nil->get_tree(context);
7575 tree empty_nil_tree = Expression::convert_for_assignment(context,
7581 // We need to handle a deferred call to recover specially,
7582 // because it changes whether it can recover a panic or not.
7583 // See test7 in test/recover1.go.
7585 if (this->is_deferred())
7587 static tree deferred_recover_fndecl;
7588 call = Gogo::call_builtin(&deferred_recover_fndecl,
7590 "__go_deferred_recover",
7596 static tree recover_fndecl;
7597 call = Gogo::call_builtin(&recover_fndecl,
7603 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
7604 call, empty_nil_tree);
7608 case BUILTIN_CLOSED:
7610 const Expression_list* args = this->args();
7611 gcc_assert(args != NULL && args->size() == 1);
7612 Expression* arg = args->front();
7613 tree arg_tree = arg->get_tree(context);
7614 if (arg_tree == error_mark_node)
7615 return error_mark_node;
7616 if (this->code_ == BUILTIN_CLOSE)
7618 static tree close_fndecl;
7619 return Gogo::call_builtin(&close_fndecl,
7621 "__go_builtin_close",
7624 TREE_TYPE(arg_tree),
7629 static tree closed_fndecl;
7630 return Gogo::call_builtin(&closed_fndecl,
7632 "__go_builtin_closed",
7635 TREE_TYPE(arg_tree),
7640 case BUILTIN_SIZEOF:
7641 case BUILTIN_OFFSETOF:
7642 case BUILTIN_ALIGNOF:
7647 bool b = this->integer_constant_value(true, val, &dummy);
7649 tree type = Type::lookup_integer_type("int")->get_tree(gogo);
7650 tree ret = Expression::integer_constant_tree(val, type);
7657 const Expression_list* args = this->args();
7658 gcc_assert(args != NULL && args->size() == 2);
7659 Expression* arg1 = args->front();
7660 Expression* arg2 = args->back();
7662 tree arg1_tree = arg1->get_tree(context);
7663 tree arg2_tree = arg2->get_tree(context);
7664 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
7665 return error_mark_node;
7667 Type* arg1_type = arg1->type();
7668 Array_type* at = arg1_type->array_type();
7669 arg1_tree = save_expr(arg1_tree);
7670 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
7671 tree arg1_len = at->length_tree(gogo, arg1_tree);
7673 Type* arg2_type = arg2->type();
7676 if (arg2_type->is_open_array_type())
7678 at = arg2_type->array_type();
7679 arg2_tree = save_expr(arg2_tree);
7680 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
7681 arg2_len = at->length_tree(gogo, arg2_tree);
7685 arg2_tree = save_expr(arg2_tree);
7686 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
7687 arg2_len = String_type::length_tree(gogo, arg2_tree);
7690 arg1_len = save_expr(arg1_len);
7691 arg2_len = save_expr(arg2_len);
7692 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
7693 fold_build2_loc(location, LT_EXPR,
7695 arg1_len, arg2_len),
7696 arg1_len, arg2_len);
7697 len = save_expr(len);
7699 Type* element_type = at->element_type();
7700 tree element_type_tree = element_type->get_tree(gogo);
7701 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
7702 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
7704 bytecount = fold_build2_loc(location, MULT_EXPR,
7705 TREE_TYPE(element_size),
7706 bytecount, element_size);
7707 bytecount = fold_convert_loc(location, size_type_node, bytecount);
7709 tree call = build_call_expr_loc(location,
7710 built_in_decls[BUILT_IN_MEMMOVE],
7711 3, arg1_val, arg2_val, bytecount);
7713 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
7717 case BUILTIN_APPEND:
7719 const Expression_list* args = this->args();
7720 gcc_assert(args != NULL && args->size() == 2);
7721 Expression* arg1 = args->front();
7722 Expression* arg2 = args->back();
7724 tree arg1_tree = arg1->get_tree(context);
7725 tree arg2_tree = arg2->get_tree(context);
7726 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
7727 return error_mark_node;
7729 tree descriptor_tree = arg1->type()->type_descriptor_pointer(gogo);
7731 // We rebuild the decl each time since the slice types may
7733 tree append_fndecl = NULL_TREE;
7734 return Gogo::call_builtin(&append_fndecl,
7738 TREE_TYPE(arg1_tree),
7739 TREE_TYPE(descriptor_tree),
7741 TREE_TYPE(arg1_tree),
7743 TREE_TYPE(arg2_tree),
7750 const Expression_list* args = this->args();
7751 gcc_assert(args != NULL && args->size() == 1);
7752 Expression* arg = args->front();
7753 tree arg_tree = arg->get_tree(context);
7754 if (arg_tree == error_mark_node)
7755 return error_mark_node;
7756 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
7757 if (this->code_ == BUILTIN_REAL)
7758 return fold_build1_loc(location, REALPART_EXPR,
7759 TREE_TYPE(TREE_TYPE(arg_tree)),
7762 return fold_build1_loc(location, IMAGPART_EXPR,
7763 TREE_TYPE(TREE_TYPE(arg_tree)),
7769 const Expression_list* args = this->args();
7770 gcc_assert(args != NULL && args->size() == 2);
7771 tree r = args->front()->get_tree(context);
7772 tree i = args->back()->get_tree(context);
7773 if (r == error_mark_node || i == error_mark_node)
7774 return error_mark_node;
7775 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
7776 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
7777 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
7778 return fold_build2_loc(location, COMPLEX_EXPR,
7779 build_complex_type(TREE_TYPE(r)),
7788 // We have to support exporting a builtin call expression, because
7789 // code can set a constant to the result of a builtin expression.
7792 Builtin_call_expression::do_export(Export* exp) const
7799 if (this->integer_constant_value(true, val, &dummy))
7801 Integer_expression::export_integer(exp, val);
7810 if (this->float_constant_value(fval, &dummy))
7812 Float_expression::export_float(exp, fval);
7824 if (this->complex_constant_value(real, imag, &dummy))
7826 Complex_expression::export_complex(exp, real, imag);
7835 error_at(this->location(), "value is not constant");
7839 // A trailing space lets us reliably identify the end of the number.
7840 exp->write_c_string(" ");
7843 // Class Call_expression.
7848 Call_expression::do_traverse(Traverse* traverse)
7850 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
7851 return TRAVERSE_EXIT;
7852 if (this->args_ != NULL)
7854 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
7855 return TRAVERSE_EXIT;
7857 return TRAVERSE_CONTINUE;
7860 // Lower a call statement.
7863 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
7865 // A type case can look like a function call.
7866 if (this->fn_->is_type_expression()
7867 && this->args_ != NULL
7868 && this->args_->size() == 1)
7869 return Expression::make_cast(this->fn_->type(), this->args_->front(),
7872 // Recognize a call to a builtin function.
7873 Func_expression* fne = this->fn_->func_expression();
7875 && fne->named_object()->is_function_declaration()
7876 && fne->named_object()->func_declaration_value()->type()->is_builtin())
7877 return new Builtin_call_expression(gogo, this->fn_, this->args_,
7878 this->is_varargs_, this->location());
7880 // Handle an argument which is a call to a function which returns
7881 // multiple results.
7882 if (this->args_ != NULL
7883 && this->args_->size() == 1
7884 && this->args_->front()->call_expression() != NULL
7885 && this->fn_->type()->function_type() != NULL)
7887 Function_type* fntype = this->fn_->type()->function_type();
7888 size_t rc = this->args_->front()->call_expression()->result_count();
7890 && fntype->parameters() != NULL
7891 && (fntype->parameters()->size() == rc
7892 || (fntype->is_varargs()
7893 && fntype->parameters()->size() - 1 <= rc)))
7895 Call_expression* call = this->args_->front()->call_expression();
7896 Expression_list* args = new Expression_list;
7897 for (size_t i = 0; i < rc; ++i)
7898 args->push_back(Expression::make_call_result(call, i));
7899 // We can't return a new call expression here, because this
7900 // one may be referenced by Call_result expressions. FIXME.
7906 // Handle a call to a varargs function by packaging up the extra
7908 if (this->fn_->type()->function_type() != NULL
7909 && this->fn_->type()->function_type()->is_varargs())
7911 Function_type* fntype = this->fn_->type()->function_type();
7912 const Typed_identifier_list* parameters = fntype->parameters();
7913 gcc_assert(parameters != NULL && !parameters->empty());
7914 Type* varargs_type = parameters->back().type();
7915 return this->lower_varargs(gogo, function, varargs_type,
7916 parameters->size());
7922 // Lower a call to a varargs function. FUNCTION is the function in
7923 // which the call occurs--it's not the function we are calling.
7924 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
7925 // PARAM_COUNT is the number of parameters of the function we are
7926 // calling; the last of these parameters will be the varargs
7930 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
7931 Type* varargs_type, size_t param_count)
7933 if (this->varargs_are_lowered_)
7936 source_location loc = this->location();
7938 gcc_assert(param_count > 0);
7939 gcc_assert(varargs_type->is_open_array_type());
7941 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
7942 if (arg_count < param_count - 1)
7944 // Not enough arguments; will be caught in check_types.
7948 Expression_list* old_args = this->args_;
7949 Expression_list* new_args = new Expression_list();
7950 bool push_empty_arg = false;
7951 if (old_args == NULL || old_args->empty())
7953 gcc_assert(param_count == 1);
7954 push_empty_arg = true;
7958 Expression_list::const_iterator pa;
7960 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
7962 if (static_cast<size_t>(i) == param_count)
7964 new_args->push_back(*pa);
7967 // We have reached the varargs parameter.
7969 bool issued_error = false;
7970 if (pa == old_args->end())
7971 push_empty_arg = true;
7972 else if (pa + 1 == old_args->end() && this->is_varargs_)
7973 new_args->push_back(*pa);
7974 else if (this->is_varargs_)
7976 this->report_error(_("too many arguments"));
7979 else if (pa + 1 == old_args->end()
7980 && this->is_compatible_varargs_argument(function, *pa,
7983 new_args->push_back(*pa);
7986 Type* element_type = varargs_type->array_type()->element_type();
7987 Expression_list* vals = new Expression_list;
7988 for (; pa != old_args->end(); ++pa, ++i)
7990 // Check types here so that we get a better message.
7991 Type* patype = (*pa)->type();
7992 source_location paloc = (*pa)->location();
7993 if (!this->check_argument_type(i, element_type, patype,
7994 paloc, issued_error))
7996 vals->push_back(*pa);
7999 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8000 new_args->push_back(val);
8005 new_args->push_back(Expression::make_nil(loc));
8007 // We can't return a new call expression here, because this one may
8008 // be referenced by Call_result expressions. FIXME.
8009 if (old_args != NULL)
8011 this->args_ = new_args;
8012 this->varargs_are_lowered_ = true;
8014 // Lower all the new subexpressions.
8015 Expression* ret = this;
8016 gogo->lower_expression(function, &ret);
8017 gcc_assert(ret == this);
8021 // Return true if ARG is a varargs argment which should be passed to
8022 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8023 // will be the last argument passed in the call, and PARAM_TYPE will
8024 // be the type of the last parameter of the varargs function being
8028 Call_expression::is_compatible_varargs_argument(Named_object* function,
8033 *issued_error = false;
8035 Type* var_type = NULL;
8037 // The simple case is passing the varargs parameter of the caller.
8038 Var_expression* ve = arg->var_expression();
8039 if (ve != NULL && ve->named_object()->is_variable())
8041 Variable* var = ve->named_object()->var_value();
8042 if (var->is_varargs_parameter())
8043 var_type = var->type();
8046 // The complex case is passing the varargs parameter of some
8047 // enclosing function. This will look like passing down *c.f where
8048 // c is the closure variable and f is a field in the closure.
8049 if (function != NULL
8050 && function->func_value()->needs_closure()
8051 && arg->classification() == EXPRESSION_UNARY)
8053 Unary_expression* ue = static_cast<Unary_expression*>(arg);
8054 if (ue->op() == OPERATOR_MULT)
8056 Field_reference_expression* fre =
8057 ue->operand()->deref()->field_reference_expression();
8060 Var_expression* ve = fre->expr()->deref()->var_expression();
8063 Named_object* no = ve->named_object();
8064 Function* f = function->func_value();
8065 if (no == f->closure_var())
8067 // At this point we know that this indeed a
8068 // reference to some enclosing variable. Now we
8069 // need to figure out whether that variable is a
8070 // varargs parameter.
8071 Named_object* enclosing =
8072 f->enclosing_var(fre->field_index());
8073 Variable* var = enclosing->var_value();
8074 if (var->is_varargs_parameter())
8075 var_type = var->type();
8082 if (var_type == NULL)
8085 // We only match if the parameter is the same, with an identical
8087 Array_type* var_at = var_type->array_type();
8088 gcc_assert(var_at != NULL);
8089 Array_type* param_at = param_type->array_type();
8090 if (param_at != NULL
8091 && Type::are_identical(var_at->element_type(),
8092 param_at->element_type(), NULL))
8094 error_at(arg->location(), "... mismatch: passing ...T as ...");
8095 *issued_error = true;
8099 // Get the function type. Returns NULL if we don't know the type. If
8100 // this returns NULL, and if_ERROR is true, issues an error.
8103 Call_expression::get_function_type() const
8105 return this->fn_->type()->function_type();
8108 // Return the number of values which this call will return.
8111 Call_expression::result_count() const
8113 const Function_type* fntype = this->get_function_type();
8116 if (fntype->results() == NULL)
8118 return fntype->results()->size();
8121 // Return whether this is a call to the predeclared function recover.
8124 Call_expression::is_recover_call() const
8126 return this->do_is_recover_call();
8129 // Set the argument to the recover function.
8132 Call_expression::set_recover_arg(Expression* arg)
8134 this->do_set_recover_arg(arg);
8137 // Virtual functions also implemented by Builtin_call_expression.
8140 Call_expression::do_is_recover_call() const
8146 Call_expression::do_set_recover_arg(Expression*)
8154 Call_expression::do_type()
8156 if (this->type_ != NULL)
8160 Function_type* fntype = this->get_function_type();
8162 return Type::make_error_type();
8164 const Typed_identifier_list* results = fntype->results();
8165 if (results == NULL)
8166 ret = Type::make_void_type();
8167 else if (results->size() == 1)
8168 ret = results->begin()->type();
8170 ret = Type::make_call_multiple_result_type(this);
8177 // Determine types for a call expression. We can use the function
8178 // parameter types to set the types of the arguments.
8181 Call_expression::do_determine_type(const Type_context*)
8183 this->fn_->determine_type_no_context();
8184 Function_type* fntype = this->get_function_type();
8185 const Typed_identifier_list* parameters = NULL;
8187 parameters = fntype->parameters();
8188 if (this->args_ != NULL)
8190 Typed_identifier_list::const_iterator pt;
8191 if (parameters != NULL)
8192 pt = parameters->begin();
8193 for (Expression_list::const_iterator pa = this->args_->begin();
8194 pa != this->args_->end();
8197 if (parameters != NULL && pt != parameters->end())
8199 Type_context subcontext(pt->type(), false);
8200 (*pa)->determine_type(&subcontext);
8204 (*pa)->determine_type_no_context();
8209 // Check types for parameter I.
8212 Call_expression::check_argument_type(int i, const Type* parameter_type,
8213 const Type* argument_type,
8214 source_location argument_location,
8218 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8223 error_at(argument_location, "argument %d has incompatible type", i);
8225 error_at(argument_location,
8226 "argument %d has incompatible type (%s)",
8229 this->set_is_error();
8238 Call_expression::do_check_types(Gogo*)
8240 Function_type* fntype = this->get_function_type();
8243 if (!this->fn_->type()->is_error_type())
8244 this->report_error(_("expected function"));
8248 if (fntype->is_method())
8250 // We don't support pointers to methods, so the function has to
8251 // be a bound method expression.
8252 Bound_method_expression* bme = this->fn_->bound_method_expression();
8255 this->report_error(_("method call without object"));
8258 Type* first_arg_type = bme->first_argument()->type();
8259 if (first_arg_type->points_to() == NULL)
8261 // When passing a value, we need to check that we are
8262 // permitted to copy it.
8264 if (!Type::are_assignable(fntype->receiver()->type(),
8265 first_arg_type, &reason))
8268 this->report_error(_("incompatible type for receiver"));
8271 error_at(this->location(),
8272 "incompatible type for receiver (%s)",
8274 this->set_is_error();
8280 // Note that varargs was handled by the lower_varargs() method, so
8281 // we don't have to worry about it here.
8283 const Typed_identifier_list* parameters = fntype->parameters();
8284 if (this->args_ == NULL)
8286 if (parameters != NULL && !parameters->empty())
8287 this->report_error(_("not enough arguments"));
8289 else if (parameters == NULL)
8290 this->report_error(_("too many arguments"));
8294 Typed_identifier_list::const_iterator pt = parameters->begin();
8295 for (Expression_list::const_iterator pa = this->args_->begin();
8296 pa != this->args_->end();
8299 if (pt == parameters->end())
8301 this->report_error(_("too many arguments"));
8304 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8305 (*pa)->location(), false);
8307 if (pt != parameters->end())
8308 this->report_error(_("not enough arguments"));
8312 // Return whether we have to use a temporary variable to ensure that
8313 // we evaluate this call expression in order. If the call returns no
8314 // results then it will inevitably be executed last. If the call
8315 // returns more than one result then it will be used with Call_result
8316 // expressions. So we only have to use a temporary variable if the
8317 // call returns exactly one result.
8320 Call_expression::do_must_eval_in_order() const
8322 return this->result_count() == 1;
8325 // Get the function and the first argument to use when calling a bound
8329 Call_expression::bound_method_function(Translate_context* context,
8330 Bound_method_expression* bound_method,
8331 tree* first_arg_ptr)
8333 Expression* first_argument = bound_method->first_argument();
8334 tree first_arg = first_argument->get_tree(context);
8335 if (first_arg == error_mark_node)
8336 return error_mark_node;
8338 // We always pass a pointer to the first argument when calling a
8340 if (first_argument->type()->points_to() == NULL)
8342 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8343 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8344 || DECL_P(first_arg)
8345 || TREE_CODE(first_arg) == INDIRECT_REF
8346 || TREE_CODE(first_arg) == COMPONENT_REF)
8348 first_arg = build_fold_addr_expr(first_arg);
8349 if (DECL_P(first_arg))
8350 TREE_ADDRESSABLE(first_arg) = 1;
8354 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8355 get_name(first_arg));
8356 DECL_IGNORED_P(tmp) = 0;
8357 DECL_INITIAL(tmp) = first_arg;
8358 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8359 build1(DECL_EXPR, void_type_node, tmp),
8360 build_fold_addr_expr(tmp));
8361 TREE_ADDRESSABLE(tmp) = 1;
8363 if (first_arg == error_mark_node)
8364 return error_mark_node;
8367 Type* fatype = bound_method->first_argument_type();
8370 if (fatype->points_to() == NULL)
8371 fatype = Type::make_pointer_type(fatype);
8372 first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg);
8373 if (first_arg == error_mark_node
8374 || TREE_TYPE(first_arg) == error_mark_node)
8375 return error_mark_node;
8378 *first_arg_ptr = first_arg;
8380 return bound_method->method()->get_tree(context);
8383 // Get the function and the first argument to use when calling an
8384 // interface method.
8387 Call_expression::interface_method_function(
8388 Translate_context* context,
8389 Interface_field_reference_expression* interface_method,
8390 tree* first_arg_ptr)
8392 tree expr = interface_method->expr()->get_tree(context);
8393 if (expr == error_mark_node)
8394 return error_mark_node;
8395 expr = save_expr(expr);
8396 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8397 if (first_arg == error_mark_node)
8398 return error_mark_node;
8399 *first_arg_ptr = first_arg;
8400 return interface_method->get_function_tree(context, expr);
8403 // Build the call expression.
8406 Call_expression::do_get_tree(Translate_context* context)
8408 if (this->tree_ != NULL_TREE)
8411 Function_type* fntype = this->get_function_type();
8413 return error_mark_node;
8415 if (this->fn_->is_error_expression())
8416 return error_mark_node;
8418 Gogo* gogo = context->gogo();
8419 source_location location = this->location();
8421 Func_expression* func = this->fn_->func_expression();
8422 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8423 Interface_field_reference_expression* interface_method =
8424 this->fn_->interface_field_reference_expression();
8425 const bool has_closure = func != NULL && func->closure() != NULL;
8426 const bool is_method = bound_method != NULL || interface_method != NULL;
8427 gcc_assert(!fntype->is_method() || is_method);
8431 if (this->args_ == NULL || this->args_->empty())
8433 nargs = is_method ? 1 : 0;
8434 args = nargs == 0 ? NULL : new tree[nargs];
8438 const Typed_identifier_list* params = fntype->parameters();
8439 gcc_assert(params != NULL);
8441 nargs = this->args_->size();
8442 int i = is_method ? 1 : 0;
8444 args = new tree[nargs];
8446 Typed_identifier_list::const_iterator pp = params->begin();
8447 Expression_list::const_iterator pe;
8448 for (pe = this->args_->begin();
8449 pe != this->args_->end();
8452 tree arg_val = (*pe)->get_tree(context);
8453 args[i] = Expression::convert_for_assignment(context,
8458 if (args[i] == error_mark_node)
8459 return error_mark_node;
8461 gcc_assert(pp == params->end());
8462 gcc_assert(i == nargs);
8465 tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo)));
8466 if (rettype == error_mark_node)
8467 return error_mark_node;
8471 fn = func->get_tree_without_closure(gogo);
8472 else if (!is_method)
8473 fn = this->fn_->get_tree(context);
8474 else if (bound_method != NULL)
8475 fn = this->bound_method_function(context, bound_method, &args[0]);
8476 else if (interface_method != NULL)
8477 fn = this->interface_method_function(context, interface_method, &args[0]);
8481 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8482 return error_mark_node;
8484 // This is to support builtin math functions when using 80387 math.
8486 if (TREE_CODE(fndecl) == ADDR_EXPR)
8487 fndecl = TREE_OPERAND(fndecl, 0);
8488 tree excess_type = NULL_TREE;
8490 && DECL_IS_BUILTIN(fndecl)
8491 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8493 && ((SCALAR_FLOAT_TYPE_P(rettype)
8494 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8495 || (COMPLEX_FLOAT_TYPE_P(rettype)
8496 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8498 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8499 if (excess_type != NULL_TREE)
8501 tree excess_fndecl = mathfn_built_in(excess_type,
8502 DECL_FUNCTION_CODE(fndecl));
8503 if (excess_fndecl == NULL_TREE)
8504 excess_type = NULL_TREE;
8507 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8508 for (int i = 0; i < nargs; ++i)
8509 args[i] = ::convert(excess_type, args[i]);
8514 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
8518 SET_EXPR_LOCATION(ret, location);
8522 tree closure_tree = func->closure()->get_tree(context);
8523 if (closure_tree != error_mark_node)
8524 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
8527 // If this is a recursive function type which returns itself, as in
8529 // we have used ptr_type_node for the return type. Add a cast here
8530 // to the correct type.
8531 if (TREE_TYPE(ret) == ptr_type_node)
8533 tree t = this->type()->get_tree(gogo);
8534 ret = fold_convert_loc(location, t, ret);
8537 if (excess_type != NULL_TREE)
8539 // Calling convert here can undo our excess precision change.
8540 // That may or may not be a bug in convert_to_real.
8541 ret = build1(NOP_EXPR, rettype, ret);
8544 // If there is more than one result, we will refer to the call
8546 if (fntype->results() != NULL && fntype->results()->size() > 1)
8547 ret = save_expr(ret);
8554 // Make a call expression.
8557 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
8558 source_location location)
8560 return new Call_expression(fn, args, is_varargs, location);
8563 // A single result from a call which returns multiple results.
8565 class Call_result_expression : public Expression
8568 Call_result_expression(Call_expression* call, unsigned int index)
8569 : Expression(EXPRESSION_CALL_RESULT, call->location()),
8570 call_(call), index_(index)
8575 do_traverse(Traverse*);
8581 do_determine_type(const Type_context*);
8584 do_check_types(Gogo*);
8589 return new Call_result_expression(this->call_->call_expression(),
8594 do_must_eval_in_order() const
8598 do_get_tree(Translate_context*);
8601 // The underlying call expression.
8603 // Which result we want.
8604 unsigned int index_;
8607 // Traverse a call result.
8610 Call_result_expression::do_traverse(Traverse* traverse)
8612 if (traverse->remember_expression(this->call_))
8614 // We have already traversed the call expression.
8615 return TRAVERSE_CONTINUE;
8617 return Expression::traverse(&this->call_, traverse);
8623 Call_result_expression::do_type()
8625 // THIS->CALL_ can be replaced with a temporary reference due to
8626 // Call_expression::do_must_eval_in_order when there is an error.
8627 Call_expression* ce = this->call_->call_expression();
8629 return Type::make_error_type();
8630 Function_type* fntype = ce->get_function_type();
8632 return Type::make_error_type();
8633 const Typed_identifier_list* results = fntype->results();
8634 Typed_identifier_list::const_iterator pr = results->begin();
8635 for (unsigned int i = 0; i < this->index_; ++i)
8637 if (pr == results->end())
8638 return Type::make_error_type();
8641 if (pr == results->end())
8642 return Type::make_error_type();
8646 // Check the type. This is where we give an error if we're trying to
8647 // extract too many values from a call.
8650 Call_result_expression::do_check_types(Gogo*)
8653 Call_expression* ce = this->call_->call_expression();
8655 ok = this->index_ < ce->result_count();
8658 // This can happen when the call returns a single value but we
8659 // are asking for the second result.
8660 if (this->call_->is_error_expression())
8665 this->report_error(_("number of results does not match number of values"));
8668 // Determine the type. We have nothing to do here, but the 0 result
8669 // needs to pass down to the caller.
8672 Call_result_expression::do_determine_type(const Type_context*)
8674 if (this->index_ == 0)
8675 this->call_->determine_type_no_context();
8681 Call_result_expression::do_get_tree(Translate_context* context)
8683 tree call_tree = this->call_->get_tree(context);
8684 if (call_tree == error_mark_node)
8685 return error_mark_node;
8686 gcc_assert(TREE_CODE(TREE_TYPE(call_tree)) == RECORD_TYPE);
8687 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
8688 for (unsigned int i = 0; i < this->index_; ++i)
8690 gcc_assert(field != NULL_TREE);
8691 field = DECL_CHAIN(field);
8693 gcc_assert(field != NULL_TREE);
8694 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
8697 // Make a reference to a single result of a call which returns
8698 // multiple results.
8701 Expression::make_call_result(Call_expression* call, unsigned int index)
8703 return new Call_result_expression(call, index);
8706 // Class Index_expression.
8711 Index_expression::do_traverse(Traverse* traverse)
8713 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
8714 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
8715 || (this->end_ != NULL
8716 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
8717 return TRAVERSE_EXIT;
8718 return TRAVERSE_CONTINUE;
8721 // Lower an index expression. This converts the generic index
8722 // expression into an array index, a string index, or a map index.
8725 Index_expression::do_lower(Gogo*, Named_object*, int)
8727 source_location location = this->location();
8728 Expression* left = this->left_;
8729 Expression* start = this->start_;
8730 Expression* end = this->end_;
8732 Type* type = left->type();
8733 if (type->is_error_type())
8734 return Expression::make_error(location);
8735 else if (type->array_type() != NULL)
8736 return Expression::make_array_index(left, start, end, location);
8737 else if (type->points_to() != NULL
8738 && type->points_to()->array_type() != NULL
8739 && !type->points_to()->is_open_array_type())
8741 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
8743 return Expression::make_array_index(deref, start, end, location);
8745 else if (type->is_string_type())
8746 return Expression::make_string_index(left, start, end, location);
8747 else if (type->map_type() != NULL)
8751 error_at(location, "invalid slice of map");
8752 return Expression::make_error(location);
8754 Map_index_expression* ret= Expression::make_map_index(left, start,
8756 if (this->is_lvalue_)
8757 ret->set_is_lvalue();
8763 "attempt to index object which is not array, string, or map");
8764 return Expression::make_error(location);
8768 // Make an index expression.
8771 Expression::make_index(Expression* left, Expression* start, Expression* end,
8772 source_location location)
8774 return new Index_expression(left, start, end, location);
8777 // An array index. This is used for both indexing and slicing.
8779 class Array_index_expression : public Expression
8782 Array_index_expression(Expression* array, Expression* start,
8783 Expression* end, source_location location)
8784 : Expression(EXPRESSION_ARRAY_INDEX, location),
8785 array_(array), start_(start), end_(end), type_(NULL)
8790 do_traverse(Traverse*);
8796 do_determine_type(const Type_context*);
8799 do_check_types(Gogo*);
8804 return Expression::make_array_index(this->array_->copy(),
8805 this->start_->copy(),
8808 : this->end_->copy()),
8813 do_is_addressable() const;
8816 do_address_taken(bool escapes)
8817 { this->array_->address_taken(escapes); }
8820 do_get_tree(Translate_context*);
8823 // The array we are getting a value from.
8825 // The start or only index.
8827 // The end index of a slice. This may be NULL for a simple array
8828 // index, or it may be a nil expression for the length of the array.
8830 // The type of the expression.
8834 // Array index traversal.
8837 Array_index_expression::do_traverse(Traverse* traverse)
8839 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
8840 return TRAVERSE_EXIT;
8841 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
8842 return TRAVERSE_EXIT;
8843 if (this->end_ != NULL)
8845 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
8846 return TRAVERSE_EXIT;
8848 return TRAVERSE_CONTINUE;
8851 // Return the type of an array index.
8854 Array_index_expression::do_type()
8856 if (this->type_ == NULL)
8858 Array_type* type = this->array_->type()->array_type();
8860 this->type_ = Type::make_error_type();
8861 else if (this->end_ == NULL)
8862 this->type_ = type->element_type();
8863 else if (type->is_open_array_type())
8865 // A slice of a slice has the same type as the original
8867 this->type_ = this->array_->type()->deref();
8871 // A slice of an array is a slice.
8872 this->type_ = Type::make_array_type(type->element_type(), NULL);
8878 // Set the type of an array index.
8881 Array_index_expression::do_determine_type(const Type_context*)
8883 this->array_->determine_type_no_context();
8884 Type_context subcontext(NULL, true);
8885 this->start_->determine_type(&subcontext);
8886 if (this->end_ != NULL)
8887 this->end_->determine_type(&subcontext);
8890 // Check types of an array index.
8893 Array_index_expression::do_check_types(Gogo*)
8895 if (this->start_->type()->integer_type() == NULL)
8896 this->report_error(_("index must be integer"));
8897 if (this->end_ != NULL
8898 && this->end_->type()->integer_type() == NULL
8899 && !this->end_->is_nil_expression())
8900 this->report_error(_("slice end must be integer"));
8902 Array_type* array_type = this->array_->type()->array_type();
8903 gcc_assert(array_type != NULL);
8905 unsigned int int_bits =
8906 Type::lookup_integer_type("int")->integer_type()->bits();
8911 bool lval_valid = (array_type->length() != NULL
8912 && array_type->length()->integer_constant_value(true,
8917 if (this->start_->integer_constant_value(true, ival, &dummy))
8919 if (mpz_sgn(ival) < 0
8920 || mpz_sizeinbase(ival, 2) >= int_bits
8922 && (this->end_ == NULL
8923 ? mpz_cmp(ival, lval) >= 0
8924 : mpz_cmp(ival, lval) > 0)))
8926 error_at(this->start_->location(), "array index out of bounds");
8927 this->set_is_error();
8930 if (this->end_ != NULL && !this->end_->is_nil_expression())
8932 if (this->end_->integer_constant_value(true, ival, &dummy))
8934 if (mpz_sgn(ival) < 0
8935 || mpz_sizeinbase(ival, 2) >= int_bits
8936 || (lval_valid && mpz_cmp(ival, lval) > 0))
8938 error_at(this->end_->location(), "array index out of bounds");
8939 this->set_is_error();
8946 // A slice of an array requires an addressable array. A slice of a
8947 // slice is always possible.
8948 if (this->end_ != NULL
8949 && !array_type->is_open_array_type()
8950 && !this->array_->is_addressable())
8951 this->report_error(_("array is not addressable"));
8954 // Return whether this expression is addressable.
8957 Array_index_expression::do_is_addressable() const
8959 // A slice expression is not addressable.
8960 if (this->end_ != NULL)
8963 // An index into a slice is addressable.
8964 if (this->array_->type()->is_open_array_type())
8967 // An index into an array is addressable if the array is
8969 return this->array_->is_addressable();
8972 // Get a tree for an array index.
8975 Array_index_expression::do_get_tree(Translate_context* context)
8977 Gogo* gogo = context->gogo();
8978 source_location loc = this->location();
8980 Array_type* array_type = this->array_->type()->array_type();
8981 gcc_assert(array_type != NULL);
8983 tree type_tree = array_type->get_tree(gogo);
8985 tree array_tree = this->array_->get_tree(context);
8986 if (array_tree == error_mark_node)
8987 return error_mark_node;
8989 if (array_type->length() == NULL && !DECL_P(array_tree))
8990 array_tree = save_expr(array_tree);
8991 tree length_tree = array_type->length_tree(gogo, array_tree);
8992 length_tree = save_expr(length_tree);
8993 tree length_type = TREE_TYPE(length_tree);
8995 tree bad_index = boolean_false_node;
8997 tree start_tree = this->start_->get_tree(context);
8998 if (start_tree == error_mark_node)
8999 return error_mark_node;
9000 if (!DECL_P(start_tree))
9001 start_tree = save_expr(start_tree);
9002 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9003 start_tree = convert_to_integer(length_type, start_tree);
9005 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9008 start_tree = fold_convert_loc(loc, length_type, start_tree);
9009 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9010 fold_build2_loc(loc,
9014 boolean_type_node, start_tree,
9017 int code = (array_type->length() != NULL
9018 ? (this->end_ == NULL
9019 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9020 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9021 : (this->end_ == NULL
9022 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9023 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9024 tree crash = Gogo::runtime_error(code, loc);
9026 if (this->end_ == NULL)
9028 // Simple array indexing. This has to return an l-value, so
9029 // wrap the index check into START_TREE.
9030 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9031 build3(COND_EXPR, void_type_node,
9032 bad_index, crash, NULL_TREE),
9034 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9036 if (array_type->length() != NULL)
9039 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9040 start_tree, NULL_TREE, NULL_TREE);
9045 tree values = array_type->value_pointer_tree(gogo, array_tree);
9046 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9047 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9048 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9049 start_tree, element_size);
9050 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9051 TREE_TYPE(values), values, offset);
9052 return build_fold_indirect_ref(ptr);
9058 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9059 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9062 if (this->end_->is_nil_expression())
9063 end_tree = length_tree;
9066 end_tree = this->end_->get_tree(context);
9067 if (end_tree == error_mark_node)
9068 return error_mark_node;
9069 if (!DECL_P(end_tree))
9070 end_tree = save_expr(end_tree);
9071 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9072 end_tree = convert_to_integer(length_type, end_tree);
9074 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9077 end_tree = fold_convert_loc(loc, length_type, end_tree);
9079 capacity_tree = save_expr(capacity_tree);
9080 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9081 fold_build2_loc(loc, LT_EXPR,
9083 end_tree, start_tree),
9084 fold_build2_loc(loc, GT_EXPR,
9086 end_tree, capacity_tree));
9087 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9088 bad_index, bad_end);
9091 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9092 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9094 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9095 fold_convert_loc(loc, sizetype, start_tree),
9098 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9100 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9101 TREE_TYPE(value_pointer),
9102 value_pointer, offset);
9104 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9105 end_tree, start_tree);
9107 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9108 capacity_tree, start_tree);
9110 tree struct_tree = this->type()->get_tree(gogo);
9111 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9113 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9115 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9116 tree field = TYPE_FIELDS(struct_tree);
9117 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9119 elt->value = value_pointer;
9121 elt = VEC_quick_push(constructor_elt, init, NULL);
9122 field = DECL_CHAIN(field);
9123 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9125 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9127 elt = VEC_quick_push(constructor_elt, init, NULL);
9128 field = DECL_CHAIN(field);
9129 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9131 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9133 tree constructor = build_constructor(struct_tree, init);
9135 if (TREE_CONSTANT(value_pointer)
9136 && TREE_CONSTANT(result_length_tree)
9137 && TREE_CONSTANT(result_capacity_tree))
9138 TREE_CONSTANT(constructor) = 1;
9140 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9141 build3(COND_EXPR, void_type_node,
9142 bad_index, crash, NULL_TREE),
9146 // Make an array index expression. END may be NULL.
9149 Expression::make_array_index(Expression* array, Expression* start,
9150 Expression* end, source_location location)
9152 // Taking a slice of a composite literal requires moving the literal
9154 if (end != NULL && array->is_composite_literal())
9156 array = Expression::make_heap_composite(array, location);
9157 array = Expression::make_unary(OPERATOR_MULT, array, location);
9159 return new Array_index_expression(array, start, end, location);
9162 // A string index. This is used for both indexing and slicing.
9164 class String_index_expression : public Expression
9167 String_index_expression(Expression* string, Expression* start,
9168 Expression* end, source_location location)
9169 : Expression(EXPRESSION_STRING_INDEX, location),
9170 string_(string), start_(start), end_(end)
9175 do_traverse(Traverse*);
9181 do_determine_type(const Type_context*);
9184 do_check_types(Gogo*);
9189 return Expression::make_string_index(this->string_->copy(),
9190 this->start_->copy(),
9193 : this->end_->copy()),
9198 do_get_tree(Translate_context*);
9201 // The string we are getting a value from.
9202 Expression* string_;
9203 // The start or only index.
9205 // The end index of a slice. This may be NULL for a single index,
9206 // or it may be a nil expression for the length of the string.
9210 // String index traversal.
9213 String_index_expression::do_traverse(Traverse* traverse)
9215 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9216 return TRAVERSE_EXIT;
9217 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9218 return TRAVERSE_EXIT;
9219 if (this->end_ != NULL)
9221 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9222 return TRAVERSE_EXIT;
9224 return TRAVERSE_CONTINUE;
9227 // Return the type of a string index.
9230 String_index_expression::do_type()
9232 if (this->end_ == NULL)
9233 return Type::lookup_integer_type("uint8");
9235 return Type::make_string_type();
9238 // Determine the type of a string index.
9241 String_index_expression::do_determine_type(const Type_context*)
9243 this->string_->determine_type_no_context();
9244 Type_context subcontext(NULL, true);
9245 this->start_->determine_type(&subcontext);
9246 if (this->end_ != NULL)
9247 this->end_->determine_type(&subcontext);
9250 // Check types of a string index.
9253 String_index_expression::do_check_types(Gogo*)
9255 if (this->start_->type()->integer_type() == NULL)
9256 this->report_error(_("index must be integer"));
9257 if (this->end_ != NULL
9258 && this->end_->type()->integer_type() == NULL
9259 && !this->end_->is_nil_expression())
9260 this->report_error(_("slice end must be integer"));
9263 bool sval_valid = this->string_->string_constant_value(&sval);
9268 if (this->start_->integer_constant_value(true, ival, &dummy))
9270 if (mpz_sgn(ival) < 0
9271 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9273 error_at(this->start_->location(), "string index out of bounds");
9274 this->set_is_error();
9277 if (this->end_ != NULL && !this->end_->is_nil_expression())
9279 if (this->end_->integer_constant_value(true, ival, &dummy))
9281 if (mpz_sgn(ival) < 0
9282 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9284 error_at(this->end_->location(), "string index out of bounds");
9285 this->set_is_error();
9292 // Get a tree for a string index.
9295 String_index_expression::do_get_tree(Translate_context* context)
9297 source_location loc = this->location();
9299 tree string_tree = this->string_->get_tree(context);
9300 if (string_tree == error_mark_node)
9301 return error_mark_node;
9303 if (this->string_->type()->points_to() != NULL)
9304 string_tree = build_fold_indirect_ref(string_tree);
9305 if (!DECL_P(string_tree))
9306 string_tree = save_expr(string_tree);
9307 tree string_type = TREE_TYPE(string_tree);
9309 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9310 length_tree = save_expr(length_tree);
9311 tree length_type = TREE_TYPE(length_tree);
9313 tree bad_index = boolean_false_node;
9315 tree start_tree = this->start_->get_tree(context);
9316 if (start_tree == error_mark_node)
9317 return error_mark_node;
9318 if (!DECL_P(start_tree))
9319 start_tree = save_expr(start_tree);
9320 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9321 start_tree = convert_to_integer(length_type, start_tree);
9323 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9326 start_tree = fold_convert_loc(loc, length_type, start_tree);
9328 int code = (this->end_ == NULL
9329 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9330 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9331 tree crash = Gogo::runtime_error(code, loc);
9333 if (this->end_ == NULL)
9335 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9337 fold_build2_loc(loc, GE_EXPR,
9339 start_tree, length_tree));
9341 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9342 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9344 fold_convert_loc(loc, sizetype, start_tree));
9345 tree index = build_fold_indirect_ref_loc(loc, ptr);
9347 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9348 build3(COND_EXPR, void_type_node,
9349 bad_index, crash, NULL_TREE),
9355 if (this->end_->is_nil_expression())
9356 end_tree = build_int_cst(length_type, -1);
9359 end_tree = this->end_->get_tree(context);
9360 if (end_tree == error_mark_node)
9361 return error_mark_node;
9362 if (!DECL_P(end_tree))
9363 end_tree = save_expr(end_tree);
9364 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9365 end_tree = convert_to_integer(length_type, end_tree);
9367 bad_index = Expression::check_bounds(end_tree, length_type,
9370 end_tree = fold_convert_loc(loc, length_type, end_tree);
9373 static tree strslice_fndecl;
9374 tree ret = Gogo::call_builtin(&strslice_fndecl,
9376 "__go_string_slice",
9385 // This will panic if the bounds are out of range for the
9387 TREE_NOTHROW(strslice_fndecl) = 0;
9389 if (bad_index == boolean_false_node)
9392 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9393 build3(COND_EXPR, void_type_node,
9394 bad_index, crash, NULL_TREE),
9399 // Make a string index expression. END may be NULL.
9402 Expression::make_string_index(Expression* string, Expression* start,
9403 Expression* end, source_location location)
9405 return new String_index_expression(string, start, end, location);
9410 // Get the type of the map.
9413 Map_index_expression::get_map_type() const
9415 Map_type* mt = this->map_->type()->deref()->map_type();
9416 gcc_assert(mt != NULL);
9420 // Map index traversal.
9423 Map_index_expression::do_traverse(Traverse* traverse)
9425 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9426 return TRAVERSE_EXIT;
9427 return Expression::traverse(&this->index_, traverse);
9430 // Return the type of a map index.
9433 Map_index_expression::do_type()
9435 Type* type = this->get_map_type()->val_type();
9436 // If this map index is in a tuple assignment, we actually return a
9437 // pointer to the value type. Tuple_map_assignment_statement is
9438 // responsible for handling this correctly. We need to get the type
9439 // right in case this gets assigned to a temporary variable.
9440 if (this->is_in_tuple_assignment_)
9441 type = Type::make_pointer_type(type);
9445 // Fix the type of a map index.
9448 Map_index_expression::do_determine_type(const Type_context*)
9450 this->map_->determine_type_no_context();
9451 Type_context subcontext(this->get_map_type()->key_type(), false);
9452 this->index_->determine_type(&subcontext);
9455 // Check types of a map index.
9458 Map_index_expression::do_check_types(Gogo*)
9461 if (!Type::are_assignable(this->get_map_type()->key_type(),
9462 this->index_->type(), &reason))
9465 this->report_error(_("incompatible type for map index"));
9468 error_at(this->location(), "incompatible type for map index (%s)",
9470 this->set_is_error();
9475 // Get a tree for a map index.
9478 Map_index_expression::do_get_tree(Translate_context* context)
9480 Map_type* type = this->get_map_type();
9482 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
9483 if (valptr == error_mark_node)
9484 return error_mark_node;
9485 valptr = save_expr(valptr);
9487 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
9489 if (this->is_lvalue_)
9490 return build_fold_indirect_ref(valptr);
9491 else if (this->is_in_tuple_assignment_)
9493 // Tuple_map_assignment_statement is responsible for using this
9499 return fold_build3(COND_EXPR, val_type_tree,
9500 fold_build2(EQ_EXPR, boolean_type_node, valptr,
9501 fold_convert(TREE_TYPE(valptr),
9502 null_pointer_node)),
9503 type->val_type()->get_init_tree(context->gogo(),
9505 build_fold_indirect_ref(valptr));
9509 // Get a tree for the map index. This returns a tree which evaluates
9510 // to a pointer to a value. The pointer will be NULL if the key is
9514 Map_index_expression::get_value_pointer(Translate_context* context,
9517 Map_type* type = this->get_map_type();
9519 tree map_tree = this->map_->get_tree(context);
9520 tree index_tree = this->index_->get_tree(context);
9521 index_tree = Expression::convert_for_assignment(context, type->key_type(),
9522 this->index_->type(),
9525 if (map_tree == error_mark_node || index_tree == error_mark_node)
9526 return error_mark_node;
9528 if (this->map_->type()->points_to() != NULL)
9529 map_tree = build_fold_indirect_ref(map_tree);
9531 // We need to pass in a pointer to the key, so stuff it into a
9533 tree tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
9534 DECL_IGNORED_P(tmp) = 0;
9535 DECL_INITIAL(tmp) = index_tree;
9536 tree make_tmp = build1(DECL_EXPR, void_type_node, tmp);
9537 tree tmpref = fold_convert(const_ptr_type_node, build_fold_addr_expr(tmp));
9538 TREE_ADDRESSABLE(tmp) = 1;
9540 static tree map_index_fndecl;
9541 tree call = Gogo::call_builtin(&map_index_fndecl,
9545 const_ptr_type_node,
9546 TREE_TYPE(map_tree),
9548 const_ptr_type_node,
9553 : boolean_false_node));
9554 // This can panic on a map of interface type if the interface holds
9555 // an uncomparable or unhashable type.
9556 TREE_NOTHROW(map_index_fndecl) = 0;
9558 tree val_type_tree = type->val_type()->get_tree(context->gogo());
9559 if (val_type_tree == error_mark_node)
9560 return error_mark_node;
9561 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
9563 return build2(COMPOUND_EXPR, ptr_val_type_tree,
9565 fold_convert(ptr_val_type_tree, call));
9568 // Make a map index expression.
9570 Map_index_expression*
9571 Expression::make_map_index(Expression* map, Expression* index,
9572 source_location location)
9574 return new Map_index_expression(map, index, location);
9577 // Class Field_reference_expression.
9579 // Return the type of a field reference.
9582 Field_reference_expression::do_type()
9584 Struct_type* struct_type = this->expr_->type()->struct_type();
9585 gcc_assert(struct_type != NULL);
9586 return struct_type->field(this->field_index_)->type();
9589 // Check the types for a field reference.
9592 Field_reference_expression::do_check_types(Gogo*)
9594 Struct_type* struct_type = this->expr_->type()->struct_type();
9595 gcc_assert(struct_type != NULL);
9596 gcc_assert(struct_type->field(this->field_index_) != NULL);
9599 // Get a tree for a field reference.
9602 Field_reference_expression::do_get_tree(Translate_context* context)
9604 tree struct_tree = this->expr_->get_tree(context);
9605 if (struct_tree == error_mark_node
9606 || TREE_TYPE(struct_tree) == error_mark_node)
9607 return error_mark_node;
9608 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
9609 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
9610 gcc_assert(field != NULL_TREE);
9611 for (unsigned int i = this->field_index_; i > 0; --i)
9613 field = DECL_CHAIN(field);
9614 gcc_assert(field != NULL_TREE);
9616 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
9620 // Make a reference to a qualified identifier in an expression.
9622 Field_reference_expression*
9623 Expression::make_field_reference(Expression* expr, unsigned int field_index,
9624 source_location location)
9626 return new Field_reference_expression(expr, field_index, location);
9629 // Class Interface_field_reference_expression.
9631 // Return a tree for the pointer to the function to call.
9634 Interface_field_reference_expression::get_function_tree(Translate_context*,
9637 if (this->expr_->type()->points_to() != NULL)
9638 expr = build_fold_indirect_ref(expr);
9640 tree expr_type = TREE_TYPE(expr);
9641 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
9643 tree field = TYPE_FIELDS(expr_type);
9644 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
9646 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
9647 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table)));
9649 table = build_fold_indirect_ref(table);
9650 gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
9652 std::string name = Gogo::unpack_hidden_name(this->name_);
9653 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
9655 field = DECL_CHAIN(field))
9657 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
9660 gcc_assert(field != NULL_TREE);
9662 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
9665 // Return a tree for the first argument to pass to the interface
9669 Interface_field_reference_expression::get_underlying_object_tree(
9673 if (this->expr_->type()->points_to() != NULL)
9674 expr = build_fold_indirect_ref(expr);
9676 tree expr_type = TREE_TYPE(expr);
9677 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
9679 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
9680 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
9682 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
9688 Interface_field_reference_expression::do_traverse(Traverse* traverse)
9690 return Expression::traverse(&this->expr_, traverse);
9693 // Return the type of an interface field reference.
9696 Interface_field_reference_expression::do_type()
9698 Type* expr_type = this->expr_->type();
9700 Type* points_to = expr_type->points_to();
9701 if (points_to != NULL)
9702 expr_type = points_to;
9704 Interface_type* interface_type = expr_type->interface_type();
9705 if (interface_type == NULL)
9706 return Type::make_error_type();
9708 const Typed_identifier* method = interface_type->find_method(this->name_);
9710 return Type::make_error_type();
9712 return method->type();
9718 Interface_field_reference_expression::do_determine_type(const Type_context*)
9720 this->expr_->determine_type_no_context();
9723 // Check the types for an interface field reference.
9726 Interface_field_reference_expression::do_check_types(Gogo*)
9728 Type* type = this->expr_->type();
9730 Type* points_to = type->points_to();
9731 if (points_to != NULL)
9734 Interface_type* interface_type = type->interface_type();
9735 if (interface_type == NULL)
9736 this->report_error(_("expected interface or pointer to interface"));
9739 const Typed_identifier* method =
9740 interface_type->find_method(this->name_);
9743 error_at(this->location(), "method %qs not in interface",
9744 Gogo::message_name(this->name_).c_str());
9745 this->set_is_error();
9750 // Get a tree for a reference to a field in an interface. There is no
9751 // standard tree type representation for this: it's a function
9752 // attached to its first argument, like a Bound_method_expression.
9753 // The only places it may currently be used are in a Call_expression
9754 // or a Go_statement, which will take it apart directly. So this has
9755 // nothing to do at present.
9758 Interface_field_reference_expression::do_get_tree(Translate_context*)
9763 // Make a reference to a field in an interface.
9766 Expression::make_interface_field_reference(Expression* expr,
9767 const std::string& field,
9768 source_location location)
9770 return new Interface_field_reference_expression(expr, field, location);
9773 // A general selector. This is a Parser_expression for LEFT.NAME. It
9774 // is lowered after we know the type of the left hand side.
9776 class Selector_expression : public Parser_expression
9779 Selector_expression(Expression* left, const std::string& name,
9780 source_location location)
9781 : Parser_expression(EXPRESSION_SELECTOR, location),
9782 left_(left), name_(name)
9787 do_traverse(Traverse* traverse)
9788 { return Expression::traverse(&this->left_, traverse); }
9791 do_lower(Gogo*, Named_object*, int);
9796 return new Selector_expression(this->left_->copy(), this->name_,
9802 lower_method_expression(Gogo*);
9804 // The expression on the left hand side.
9806 // The name on the right hand side.
9810 // Lower a selector expression once we know the real type of the left
9814 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
9816 Expression* left = this->left_;
9817 if (left->is_type_expression())
9818 return this->lower_method_expression(gogo);
9819 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
9823 // Lower a method expression T.M or (*T).M. We turn this into a
9824 // function literal.
9827 Selector_expression::lower_method_expression(Gogo* gogo)
9829 source_location location = this->location();
9830 Type* type = this->left_->type();
9831 const std::string& name(this->name_);
9834 if (type->points_to() == NULL)
9839 type = type->points_to();
9841 Named_type* nt = type->named_type();
9845 ("method expression requires named type or "
9846 "pointer to named type"));
9847 return Expression::make_error(location);
9851 Method* method = nt->method_function(name, &is_ambiguous);
9855 error_at(location, "type %<%s%> has no method %<%s%>",
9856 nt->message_name().c_str(),
9857 Gogo::message_name(name).c_str());
9859 error_at(location, "method %<%s%> is ambiguous in type %<%s%>",
9860 Gogo::message_name(name).c_str(),
9861 nt->message_name().c_str());
9862 return Expression::make_error(location);
9865 if (!is_pointer && !method->is_value_method())
9867 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
9868 nt->message_name().c_str(),
9869 Gogo::message_name(name).c_str());
9870 return Expression::make_error(location);
9873 // Build a new function type in which the receiver becomes the first
9875 Function_type* method_type = method->type();
9876 gcc_assert(method_type->is_method());
9878 const char* const receiver_name = "$this";
9879 Typed_identifier_list* parameters = new Typed_identifier_list();
9880 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
9883 const Typed_identifier_list* method_parameters = method_type->parameters();
9884 if (method_parameters != NULL)
9886 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
9887 p != method_parameters->end();
9889 parameters->push_back(*p);
9892 const Typed_identifier_list* method_results = method_type->results();
9893 Typed_identifier_list* results;
9894 if (method_results == NULL)
9898 results = new Typed_identifier_list();
9899 for (Typed_identifier_list::const_iterator p = method_results->begin();
9900 p != method_results->end();
9902 results->push_back(*p);
9905 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
9907 if (method_type->is_varargs())
9908 fntype->set_is_varargs();
9910 // We generate methods which always takes a pointer to the receiver
9911 // as their first argument. If this is for a pointer type, we can
9912 // simply reuse the existing function. We use an internal hack to
9913 // get the right type.
9917 Named_object* mno = (method->needs_stub_method()
9918 ? method->stub_object()
9919 : method->named_object());
9920 Expression* f = Expression::make_func_reference(mno, NULL, location);
9921 f = Expression::make_cast(fntype, f, location);
9922 Type_conversion_expression* tce =
9923 static_cast<Type_conversion_expression*>(f);
9924 tce->set_may_convert_function_types();
9928 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
9931 Named_object* vno = gogo->lookup(receiver_name, NULL);
9932 gcc_assert(vno != NULL);
9933 Expression* ve = Expression::make_var_reference(vno, location);
9934 Expression* bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
9935 gcc_assert(bm != NULL && !bm->is_error_expression());
9937 Expression_list* args;
9938 if (method_parameters == NULL)
9942 args = new Expression_list();
9943 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
9944 p != method_parameters->end();
9947 vno = gogo->lookup(p->name(), NULL);
9948 gcc_assert(vno != NULL);
9949 args->push_back(Expression::make_var_reference(vno, location));
9953 Call_expression* call = Expression::make_call(bm, args,
9954 method_type->is_varargs(),
9957 size_t count = call->result_count();
9960 s = Statement::make_statement(call);
9963 Expression_list* retvals = new Expression_list();
9965 retvals->push_back(call);
9968 for (size_t i = 0; i < count; ++i)
9969 retvals->push_back(Expression::make_call_result(call, i));
9971 s = Statement::make_return_statement(no->func_value()->type()->results(),
9974 gogo->add_statement(s);
9976 gogo->finish_function(location);
9978 return Expression::make_func_reference(no, NULL, location);
9981 // Make a selector expression.
9984 Expression::make_selector(Expression* left, const std::string& name,
9985 source_location location)
9987 return new Selector_expression(left, name, location);
9990 // Implement the builtin function new.
9992 class Allocation_expression : public Expression
9995 Allocation_expression(Type* type, source_location location)
9996 : Expression(EXPRESSION_ALLOCATION, location),
10002 do_traverse(Traverse* traverse)
10003 { return Type::traverse(this->type_, traverse); }
10007 { return Type::make_pointer_type(this->type_); }
10010 do_determine_type(const Type_context*)
10014 do_check_types(Gogo*);
10018 { return new Allocation_expression(this->type_, this->location()); }
10021 do_get_tree(Translate_context*);
10024 // The type we are allocating.
10028 // Check the type of an allocation expression.
10031 Allocation_expression::do_check_types(Gogo*)
10033 if (this->type_->function_type() != NULL)
10034 this->report_error(_("invalid new of function type"));
10037 // Return a tree for an allocation expression.
10040 Allocation_expression::do_get_tree(Translate_context* context)
10042 tree type_tree = this->type_->get_tree(context->gogo());
10043 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10044 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10046 return fold_convert(build_pointer_type(type_tree), space);
10049 // Make an allocation expression.
10052 Expression::make_allocation(Type* type, source_location location)
10054 return new Allocation_expression(type, location);
10057 // Implement the builtin function make.
10059 class Make_expression : public Expression
10062 Make_expression(Type* type, Expression_list* args, source_location location)
10063 : Expression(EXPRESSION_MAKE, location),
10064 type_(type), args_(args)
10069 do_traverse(Traverse* traverse);
10073 { return this->type_; }
10076 do_determine_type(const Type_context*);
10079 do_check_types(Gogo*);
10084 return new Make_expression(this->type_, this->args_->copy(),
10089 do_get_tree(Translate_context*);
10092 // The type we are making.
10094 // The arguments to pass to the make routine.
10095 Expression_list* args_;
10101 Make_expression::do_traverse(Traverse* traverse)
10103 if (this->args_ != NULL
10104 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10105 return TRAVERSE_EXIT;
10106 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10107 return TRAVERSE_EXIT;
10108 return TRAVERSE_CONTINUE;
10111 // Set types of arguments.
10114 Make_expression::do_determine_type(const Type_context*)
10116 if (this->args_ != NULL)
10118 Type_context context(Type::lookup_integer_type("int"), false);
10119 for (Expression_list::const_iterator pe = this->args_->begin();
10120 pe != this->args_->end();
10122 (*pe)->determine_type(&context);
10126 // Check types for a make expression.
10129 Make_expression::do_check_types(Gogo*)
10131 if (this->type_->channel_type() == NULL
10132 && this->type_->map_type() == NULL
10133 && (this->type_->array_type() == NULL
10134 || this->type_->array_type()->length() != NULL))
10135 this->report_error(_("invalid type for make function"));
10136 else if (!this->type_->check_make_expression(this->args_, this->location()))
10137 this->set_is_error();
10140 // Return a tree for a make expression.
10143 Make_expression::do_get_tree(Translate_context* context)
10145 return this->type_->make_expression_tree(context, this->args_,
10149 // Make a make expression.
10152 Expression::make_make(Type* type, Expression_list* args,
10153 source_location location)
10155 return new Make_expression(type, args, location);
10158 // Construct a struct.
10160 class Struct_construction_expression : public Expression
10163 Struct_construction_expression(Type* type, Expression_list* vals,
10164 source_location location)
10165 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10166 type_(type), vals_(vals)
10169 // Return whether this is a constant initializer.
10171 is_constant_struct() const;
10175 do_traverse(Traverse* traverse);
10179 { return this->type_; }
10182 do_determine_type(const Type_context*);
10185 do_check_types(Gogo*);
10190 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10195 do_is_addressable() const
10199 do_get_tree(Translate_context*);
10202 do_export(Export*) const;
10205 // The type of the struct to construct.
10207 // The list of values, in order of the fields in the struct. A NULL
10208 // entry means that the field should be zero-initialized.
10209 Expression_list* vals_;
10215 Struct_construction_expression::do_traverse(Traverse* traverse)
10217 if (this->vals_ != NULL
10218 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10219 return TRAVERSE_EXIT;
10220 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10221 return TRAVERSE_EXIT;
10222 return TRAVERSE_CONTINUE;
10225 // Return whether this is a constant initializer.
10228 Struct_construction_expression::is_constant_struct() const
10230 if (this->vals_ == NULL)
10232 for (Expression_list::const_iterator pv = this->vals_->begin();
10233 pv != this->vals_->end();
10237 && !(*pv)->is_constant()
10238 && (!(*pv)->is_composite_literal()
10239 || (*pv)->is_nonconstant_composite_literal()))
10243 const Struct_field_list* fields = this->type_->struct_type()->fields();
10244 for (Struct_field_list::const_iterator pf = fields->begin();
10245 pf != fields->end();
10248 // There are no constant constructors for interfaces.
10249 if (pf->type()->interface_type() != NULL)
10256 // Final type determination.
10259 Struct_construction_expression::do_determine_type(const Type_context*)
10261 if (this->vals_ == NULL)
10263 const Struct_field_list* fields = this->type_->struct_type()->fields();
10264 Expression_list::const_iterator pv = this->vals_->begin();
10265 for (Struct_field_list::const_iterator pf = fields->begin();
10266 pf != fields->end();
10269 if (pv == this->vals_->end())
10273 Type_context subcontext(pf->type(), false);
10274 (*pv)->determine_type(&subcontext);
10282 Struct_construction_expression::do_check_types(Gogo*)
10284 if (this->vals_ == NULL)
10287 Struct_type* st = this->type_->struct_type();
10288 if (this->vals_->size() > st->field_count())
10290 this->report_error(_("too many expressions for struct"));
10294 const Struct_field_list* fields = st->fields();
10295 Expression_list::const_iterator pv = this->vals_->begin();
10297 for (Struct_field_list::const_iterator pf = fields->begin();
10298 pf != fields->end();
10301 if (pv == this->vals_->end())
10303 this->report_error(_("too few expressions for struct"));
10310 std::string reason;
10311 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10313 if (reason.empty())
10314 error_at((*pv)->location(),
10315 "incompatible type for field %d in struct construction",
10318 error_at((*pv)->location(),
10319 ("incompatible type for field %d in "
10320 "struct construction (%s)"),
10321 i + 1, reason.c_str());
10322 this->set_is_error();
10325 gcc_assert(pv == this->vals_->end());
10328 // Return a tree for constructing a struct.
10331 Struct_construction_expression::do_get_tree(Translate_context* context)
10333 Gogo* gogo = context->gogo();
10335 if (this->vals_ == NULL)
10336 return this->type_->get_init_tree(gogo, false);
10338 tree type_tree = this->type_->get_tree(gogo);
10339 if (type_tree == error_mark_node)
10340 return error_mark_node;
10341 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10343 bool is_constant = true;
10344 const Struct_field_list* fields = this->type_->struct_type()->fields();
10345 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10347 Struct_field_list::const_iterator pf = fields->begin();
10348 Expression_list::const_iterator pv = this->vals_->begin();
10349 for (tree field = TYPE_FIELDS(type_tree);
10350 field != NULL_TREE;
10351 field = DECL_CHAIN(field), ++pf)
10353 gcc_assert(pf != fields->end());
10356 if (pv == this->vals_->end())
10357 val = pf->type()->get_init_tree(gogo, false);
10358 else if (*pv == NULL)
10360 val = pf->type()->get_init_tree(gogo, false);
10365 val = Expression::convert_for_assignment(context, pf->type(),
10367 (*pv)->get_tree(context),
10372 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10373 return error_mark_node;
10375 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10376 elt->index = field;
10378 if (!TREE_CONSTANT(val))
10379 is_constant = false;
10381 gcc_assert(pf == fields->end());
10383 tree ret = build_constructor(type_tree, elts);
10385 TREE_CONSTANT(ret) = 1;
10389 // Export a struct construction.
10392 Struct_construction_expression::do_export(Export* exp) const
10394 exp->write_c_string("convert(");
10395 exp->write_type(this->type_);
10396 for (Expression_list::const_iterator pv = this->vals_->begin();
10397 pv != this->vals_->end();
10400 exp->write_c_string(", ");
10402 (*pv)->export_expression(exp);
10404 exp->write_c_string(")");
10407 // Make a struct composite literal. This used by the thunk code.
10410 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
10411 source_location location)
10413 gcc_assert(type->struct_type() != NULL);
10414 return new Struct_construction_expression(type, vals, location);
10417 // Construct an array. This class is not used directly; instead we
10418 // use the child classes, Fixed_array_construction_expression and
10419 // Open_array_construction_expression.
10421 class Array_construction_expression : public Expression
10424 Array_construction_expression(Expression_classification classification,
10425 Type* type, Expression_list* vals,
10426 source_location location)
10427 : Expression(classification, location),
10428 type_(type), vals_(vals)
10432 // Return whether this is a constant initializer.
10434 is_constant_array() const;
10436 // Return the number of elements.
10438 element_count() const
10439 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
10443 do_traverse(Traverse* traverse);
10447 { return this->type_; }
10450 do_determine_type(const Type_context*);
10453 do_check_types(Gogo*);
10456 do_is_addressable() const
10460 do_export(Export*) const;
10462 // The list of values.
10465 { return this->vals_; }
10467 // Get a constructor tree for the array values.
10469 get_constructor_tree(Translate_context* context, tree type_tree);
10472 // The type of the array to construct.
10474 // The list of values.
10475 Expression_list* vals_;
10481 Array_construction_expression::do_traverse(Traverse* traverse)
10483 if (this->vals_ != NULL
10484 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10485 return TRAVERSE_EXIT;
10486 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10487 return TRAVERSE_EXIT;
10488 return TRAVERSE_CONTINUE;
10491 // Return whether this is a constant initializer.
10494 Array_construction_expression::is_constant_array() const
10496 if (this->vals_ == NULL)
10499 // There are no constant constructors for interfaces.
10500 if (this->type_->array_type()->element_type()->interface_type() != NULL)
10503 for (Expression_list::const_iterator pv = this->vals_->begin();
10504 pv != this->vals_->end();
10508 && !(*pv)->is_constant()
10509 && (!(*pv)->is_composite_literal()
10510 || (*pv)->is_nonconstant_composite_literal()))
10516 // Final type determination.
10519 Array_construction_expression::do_determine_type(const Type_context*)
10521 if (this->vals_ == NULL)
10523 Type_context subcontext(this->type_->array_type()->element_type(), false);
10524 for (Expression_list::const_iterator pv = this->vals_->begin();
10525 pv != this->vals_->end();
10529 (*pv)->determine_type(&subcontext);
10536 Array_construction_expression::do_check_types(Gogo*)
10538 if (this->vals_ == NULL)
10541 Array_type* at = this->type_->array_type();
10543 Type* element_type = at->element_type();
10544 for (Expression_list::const_iterator pv = this->vals_->begin();
10545 pv != this->vals_->end();
10549 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
10551 error_at((*pv)->location(),
10552 "incompatible type for element %d in composite literal",
10554 this->set_is_error();
10558 Expression* length = at->length();
10559 if (length != NULL)
10564 if (at->length()->integer_constant_value(true, val, &type))
10566 if (this->vals_->size() > mpz_get_ui(val))
10567 this->report_error(_("too many elements in composite literal"));
10573 // Get a constructor tree for the array values.
10576 Array_construction_expression::get_constructor_tree(Translate_context* context,
10579 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
10580 (this->vals_ == NULL
10582 : this->vals_->size()));
10583 Type* element_type = this->type_->array_type()->element_type();
10584 bool is_constant = true;
10585 if (this->vals_ != NULL)
10588 for (Expression_list::const_iterator pv = this->vals_->begin();
10589 pv != this->vals_->end();
10592 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
10593 elt->index = size_int(i);
10595 elt->value = element_type->get_init_tree(context->gogo(), false);
10598 tree value_tree = (*pv)->get_tree(context);
10599 elt->value = Expression::convert_for_assignment(context,
10605 if (elt->value == error_mark_node)
10606 return error_mark_node;
10607 if (!TREE_CONSTANT(elt->value))
10608 is_constant = false;
10612 tree ret = build_constructor(type_tree, values);
10614 TREE_CONSTANT(ret) = 1;
10618 // Export an array construction.
10621 Array_construction_expression::do_export(Export* exp) const
10623 exp->write_c_string("convert(");
10624 exp->write_type(this->type_);
10625 if (this->vals_ != NULL)
10627 for (Expression_list::const_iterator pv = this->vals_->begin();
10628 pv != this->vals_->end();
10631 exp->write_c_string(", ");
10633 (*pv)->export_expression(exp);
10636 exp->write_c_string(")");
10639 // Construct a fixed array.
10641 class Fixed_array_construction_expression :
10642 public Array_construction_expression
10645 Fixed_array_construction_expression(Type* type, Expression_list* vals,
10646 source_location location)
10647 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
10648 type, vals, location)
10650 gcc_assert(type->array_type() != NULL
10651 && type->array_type()->length() != NULL);
10658 return new Fixed_array_construction_expression(this->type(),
10659 (this->vals() == NULL
10661 : this->vals()->copy()),
10666 do_get_tree(Translate_context*);
10669 // Return a tree for constructing a fixed array.
10672 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
10674 return this->get_constructor_tree(context,
10675 this->type()->get_tree(context->gogo()));
10678 // Construct an open array.
10680 class Open_array_construction_expression : public Array_construction_expression
10683 Open_array_construction_expression(Type* type, Expression_list* vals,
10684 source_location location)
10685 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
10686 type, vals, location)
10688 gcc_assert(type->array_type() != NULL
10689 && type->array_type()->length() == NULL);
10693 // Note that taking the address of an open array literal is invalid.
10698 return new Open_array_construction_expression(this->type(),
10699 (this->vals() == NULL
10701 : this->vals()->copy()),
10706 do_get_tree(Translate_context*);
10709 // Return a tree for constructing an open array.
10712 Open_array_construction_expression::do_get_tree(Translate_context* context)
10714 Type* element_type = this->type()->array_type()->element_type();
10715 tree element_type_tree = element_type->get_tree(context->gogo());
10718 if (this->vals() == NULL || this->vals()->empty())
10720 // We need to create a unique value.
10721 tree max = size_int(0);
10722 tree constructor_type = build_array_type(element_type_tree,
10723 build_index_type(max));
10724 if (constructor_type == error_mark_node)
10725 return error_mark_node;
10726 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
10727 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
10728 elt->index = size_int(0);
10729 elt->value = element_type->get_init_tree(context->gogo(), false);
10730 values = build_constructor(constructor_type, vec);
10731 if (TREE_CONSTANT(elt->value))
10732 TREE_CONSTANT(values) = 1;
10733 length_tree = size_int(0);
10737 tree max = size_int(this->vals()->size() - 1);
10738 tree constructor_type = build_array_type(element_type_tree,
10739 build_index_type(max));
10740 if (constructor_type == error_mark_node)
10741 return error_mark_node;
10742 values = this->get_constructor_tree(context, constructor_type);
10743 length_tree = size_int(this->vals()->size());
10746 if (values == error_mark_node)
10747 return error_mark_node;
10749 bool is_constant_initializer = TREE_CONSTANT(values);
10750 bool is_in_function = context->function() != NULL;
10752 if (is_constant_initializer)
10754 tree tmp = build_decl(this->location(), VAR_DECL,
10755 create_tmp_var_name("C"), TREE_TYPE(values));
10756 DECL_EXTERNAL(tmp) = 0;
10757 TREE_PUBLIC(tmp) = 0;
10758 TREE_STATIC(tmp) = 1;
10759 DECL_ARTIFICIAL(tmp) = 1;
10760 if (is_in_function)
10762 // If this is not a function, we will only initialize the
10763 // value once, so we can use this directly rather than
10764 // copying it. In that case we can't make it read-only,
10765 // because the program is permitted to change it.
10766 TREE_READONLY(tmp) = 1;
10767 TREE_CONSTANT(tmp) = 1;
10769 DECL_INITIAL(tmp) = values;
10770 rest_of_decl_compilation(tmp, 1, 0);
10776 if (!is_in_function && is_constant_initializer)
10778 // Outside of a function, we know the initializer will only run
10780 space = build_fold_addr_expr(values);
10785 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
10786 space = context->gogo()->allocate_memory(element_type, memsize,
10788 space = save_expr(space);
10790 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
10791 tree ref = build_fold_indirect_ref_loc(this->location(), s);
10792 TREE_THIS_NOTRAP(ref) = 1;
10793 set = build2(MODIFY_EXPR, void_type_node, ref, values);
10796 // Build a constructor for the open array.
10798 tree type_tree = this->type()->get_tree(context->gogo());
10799 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10801 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
10803 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
10804 tree field = TYPE_FIELDS(type_tree);
10805 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
10806 elt->index = field;
10807 elt->value = fold_convert(TREE_TYPE(field), space);
10809 elt = VEC_quick_push(constructor_elt, init, NULL);
10810 field = DECL_CHAIN(field);
10811 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
10812 elt->index = field;
10813 elt->value = fold_convert(TREE_TYPE(field), length_tree);
10815 elt = VEC_quick_push(constructor_elt, init, NULL);
10816 field = DECL_CHAIN(field);
10817 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
10818 elt->index = field;
10819 elt->value = fold_convert(TREE_TYPE(field), length_tree);
10821 tree constructor = build_constructor(type_tree, init);
10822 if (!is_in_function && is_constant_initializer)
10823 TREE_CONSTANT(constructor) = 1;
10825 if (set == NULL_TREE)
10826 return constructor;
10828 return build2(COMPOUND_EXPR, type_tree, set, constructor);
10831 // Make a slice composite literal. This is used by the type
10832 // descriptor code.
10835 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
10836 source_location location)
10838 gcc_assert(type->is_open_array_type());
10839 return new Open_array_construction_expression(type, vals, location);
10842 // Construct a map.
10844 class Map_construction_expression : public Expression
10847 Map_construction_expression(Type* type, Expression_list* vals,
10848 source_location location)
10849 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
10850 type_(type), vals_(vals)
10851 { gcc_assert(vals == NULL || vals->size() % 2 == 0); }
10855 do_traverse(Traverse* traverse);
10859 { return this->type_; }
10862 do_determine_type(const Type_context*);
10865 do_check_types(Gogo*);
10870 return new Map_construction_expression(this->type_, this->vals_->copy(),
10875 do_get_tree(Translate_context*);
10878 do_export(Export*) const;
10881 // The type of the map to construct.
10883 // The list of values.
10884 Expression_list* vals_;
10890 Map_construction_expression::do_traverse(Traverse* traverse)
10892 if (this->vals_ != NULL
10893 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10894 return TRAVERSE_EXIT;
10895 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10896 return TRAVERSE_EXIT;
10897 return TRAVERSE_CONTINUE;
10900 // Final type determination.
10903 Map_construction_expression::do_determine_type(const Type_context*)
10905 if (this->vals_ == NULL)
10908 Map_type* mt = this->type_->map_type();
10909 Type_context key_context(mt->key_type(), false);
10910 Type_context val_context(mt->val_type(), false);
10911 for (Expression_list::const_iterator pv = this->vals_->begin();
10912 pv != this->vals_->end();
10915 (*pv)->determine_type(&key_context);
10917 (*pv)->determine_type(&val_context);
10924 Map_construction_expression::do_check_types(Gogo*)
10926 if (this->vals_ == NULL)
10929 Map_type* mt = this->type_->map_type();
10931 Type* key_type = mt->key_type();
10932 Type* val_type = mt->val_type();
10933 for (Expression_list::const_iterator pv = this->vals_->begin();
10934 pv != this->vals_->end();
10937 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
10939 error_at((*pv)->location(),
10940 "incompatible type for element %d key in map construction",
10942 this->set_is_error();
10945 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
10947 error_at((*pv)->location(),
10948 ("incompatible type for element %d value "
10949 "in map construction"),
10951 this->set_is_error();
10956 // Return a tree for constructing a map.
10959 Map_construction_expression::do_get_tree(Translate_context* context)
10961 Gogo* gogo = context->gogo();
10962 source_location loc = this->location();
10964 Map_type* mt = this->type_->map_type();
10966 // Build a struct to hold the key and value.
10967 tree struct_type = make_node(RECORD_TYPE);
10969 Type* key_type = mt->key_type();
10970 tree id = get_identifier("__key");
10971 tree key_field = build_decl(loc, FIELD_DECL, id, key_type->get_tree(gogo));
10972 DECL_CONTEXT(key_field) = struct_type;
10973 TYPE_FIELDS(struct_type) = key_field;
10975 Type* val_type = mt->val_type();
10976 id = get_identifier("__val");
10977 tree val_field = build_decl(loc, FIELD_DECL, id, val_type->get_tree(gogo));
10978 DECL_CONTEXT(val_field) = struct_type;
10979 DECL_CHAIN(key_field) = val_field;
10981 layout_type(struct_type);
10983 bool is_constant = true;
10988 if (this->vals_ == NULL || this->vals_->empty())
10990 valaddr = null_pointer_node;
10991 make_tmp = NULL_TREE;
10995 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
10996 this->vals_->size() / 2);
10998 for (Expression_list::const_iterator pv = this->vals_->begin();
10999 pv != this->vals_->end();
11002 bool one_is_constant = true;
11004 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
11006 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11007 elt->index = key_field;
11008 tree val_tree = (*pv)->get_tree(context);
11009 elt->value = Expression::convert_for_assignment(context, key_type,
11012 if (elt->value == error_mark_node)
11013 return error_mark_node;
11014 if (!TREE_CONSTANT(elt->value))
11015 one_is_constant = false;
11019 elt = VEC_quick_push(constructor_elt, one, NULL);
11020 elt->index = val_field;
11021 val_tree = (*pv)->get_tree(context);
11022 elt->value = Expression::convert_for_assignment(context, val_type,
11025 if (elt->value == error_mark_node)
11026 return error_mark_node;
11027 if (!TREE_CONSTANT(elt->value))
11028 one_is_constant = false;
11030 elt = VEC_quick_push(constructor_elt, values, NULL);
11031 elt->index = size_int(i);
11032 elt->value = build_constructor(struct_type, one);
11033 if (one_is_constant)
11034 TREE_CONSTANT(elt->value) = 1;
11036 is_constant = false;
11039 tree index_type = build_index_type(size_int(i - 1));
11040 tree array_type = build_array_type(struct_type, index_type);
11041 tree init = build_constructor(array_type, values);
11043 TREE_CONSTANT(init) = 1;
11045 if (current_function_decl != NULL)
11047 tmp = create_tmp_var(array_type, get_name(array_type));
11048 DECL_INITIAL(tmp) = init;
11049 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11050 TREE_ADDRESSABLE(tmp) = 1;
11054 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11055 DECL_EXTERNAL(tmp) = 0;
11056 TREE_PUBLIC(tmp) = 0;
11057 TREE_STATIC(tmp) = 1;
11058 DECL_ARTIFICIAL(tmp) = 1;
11059 if (!TREE_CONSTANT(init))
11060 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11064 TREE_READONLY(tmp) = 1;
11065 TREE_CONSTANT(tmp) = 1;
11066 DECL_INITIAL(tmp) = init;
11067 make_tmp = NULL_TREE;
11069 rest_of_decl_compilation(tmp, 1, 0);
11072 valaddr = build_fold_addr_expr(tmp);
11075 tree descriptor = gogo->map_descriptor(mt);
11077 tree type_tree = this->type_->get_tree(gogo);
11079 static tree construct_map_fndecl;
11080 tree call = Gogo::call_builtin(&construct_map_fndecl,
11082 "__go_construct_map",
11085 TREE_TYPE(descriptor),
11090 TYPE_SIZE_UNIT(struct_type),
11092 byte_position(val_field),
11094 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11095 const_ptr_type_node,
11096 fold_convert(const_ptr_type_node, valaddr));
11099 if (make_tmp == NULL)
11102 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11106 // Export an array construction.
11109 Map_construction_expression::do_export(Export* exp) const
11111 exp->write_c_string("convert(");
11112 exp->write_type(this->type_);
11113 for (Expression_list::const_iterator pv = this->vals_->begin();
11114 pv != this->vals_->end();
11117 exp->write_c_string(", ");
11118 (*pv)->export_expression(exp);
11120 exp->write_c_string(")");
11123 // A general composite literal. This is lowered to a type specific
11126 class Composite_literal_expression : public Parser_expression
11129 Composite_literal_expression(Type* type, int depth, bool has_keys,
11130 Expression_list* vals, source_location location)
11131 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11132 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11137 do_traverse(Traverse* traverse);
11140 do_lower(Gogo*, Named_object*, int);
11145 return new Composite_literal_expression(this->type_, this->depth_,
11147 (this->vals_ == NULL
11149 : this->vals_->copy()),
11155 lower_struct(Type*);
11158 lower_array(Type*);
11161 make_array(Type*, Expression_list*);
11164 lower_map(Gogo*, Named_object*, Type*);
11166 // The type of the composite literal.
11168 // The depth within a list of composite literals within a composite
11169 // literal, when the type is omitted.
11171 // The values to put in the composite literal.
11172 Expression_list* vals_;
11173 // If this is true, then VALS_ is a list of pairs: a key and a
11174 // value. In an array initializer, a missing key will be NULL.
11181 Composite_literal_expression::do_traverse(Traverse* traverse)
11183 if (this->vals_ != NULL
11184 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11185 return TRAVERSE_EXIT;
11186 return Type::traverse(this->type_, traverse);
11189 // Lower a generic composite literal into a specific version based on
11193 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int)
11195 Type* type = this->type_;
11197 for (int depth = this->depth_; depth > 0; --depth)
11199 if (type->array_type() != NULL)
11200 type = type->array_type()->element_type();
11201 else if (type->map_type() != NULL)
11202 type = type->map_type()->val_type();
11205 if (!type->is_error_type())
11206 error_at(this->location(),
11207 ("may only omit types within composite literals "
11208 "of slice, array, or map type"));
11209 return Expression::make_error(this->location());
11213 if (type->is_error_type())
11214 return Expression::make_error(this->location());
11215 else if (type->struct_type() != NULL)
11216 return this->lower_struct(type);
11217 else if (type->array_type() != NULL)
11218 return this->lower_array(type);
11219 else if (type->map_type() != NULL)
11220 return this->lower_map(gogo, function, type);
11223 error_at(this->location(),
11224 ("expected struct, slice, array, or map type "
11225 "for composite literal"));
11226 return Expression::make_error(this->location());
11230 // Lower a struct composite literal.
11233 Composite_literal_expression::lower_struct(Type* type)
11235 source_location location = this->location();
11236 Struct_type* st = type->struct_type();
11237 if (this->vals_ == NULL || !this->has_keys_)
11238 return new Struct_construction_expression(type, this->vals_, location);
11240 size_t field_count = st->field_count();
11241 std::vector<Expression*> vals(field_count);
11242 Expression_list::const_iterator p = this->vals_->begin();
11243 while (p != this->vals_->end())
11245 Expression* name_expr = *p;
11248 gcc_assert(p != this->vals_->end());
11249 Expression* val = *p;
11253 if (name_expr == NULL)
11255 error_at(val->location(), "mixture of field and value initializers");
11256 return Expression::make_error(location);
11259 bool bad_key = false;
11261 switch (name_expr->classification())
11263 case EXPRESSION_UNKNOWN_REFERENCE:
11264 name = name_expr->unknown_expression()->name();
11267 case EXPRESSION_CONST_REFERENCE:
11268 name = static_cast<Const_expression*>(name_expr)->name();
11271 case EXPRESSION_TYPE:
11273 Type* t = name_expr->type();
11274 Named_type* nt = t->named_type();
11282 case EXPRESSION_VAR_REFERENCE:
11283 name = name_expr->var_expression()->name();
11286 case EXPRESSION_FUNC_REFERENCE:
11287 name = name_expr->func_expression()->name();
11290 case EXPRESSION_UNARY:
11291 // If there is a local variable around with the same name as
11292 // the field, and this occurs in the closure, then the
11293 // parser may turn the field reference into an indirection
11294 // through the closure. FIXME: This is a mess.
11297 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11298 if (ue->op() == OPERATOR_MULT)
11300 Field_reference_expression* fre =
11301 ue->operand()->field_reference_expression();
11305 fre->expr()->type()->deref()->struct_type();
11308 const Struct_field* sf = st->field(fre->field_index());
11309 name = sf->field_name();
11311 snprintf(buf, sizeof buf, "%u", fre->field_index());
11312 size_t buflen = strlen(buf);
11313 if (name.compare(name.length() - buflen, buflen, buf)
11316 name = name.substr(0, name.length() - buflen);
11331 error_at(name_expr->location(), "expected struct field name");
11332 return Expression::make_error(location);
11335 unsigned int index;
11336 const Struct_field* sf = st->find_local_field(name, &index);
11339 error_at(name_expr->location(), "unknown field %qs in %qs",
11340 Gogo::message_name(name).c_str(),
11341 (type->named_type() != NULL
11342 ? type->named_type()->message_name().c_str()
11343 : "unnamed struct"));
11344 return Expression::make_error(location);
11346 if (vals[index] != NULL)
11348 error_at(name_expr->location(),
11349 "duplicate value for field %qs in %qs",
11350 Gogo::message_name(name).c_str(),
11351 (type->named_type() != NULL
11352 ? type->named_type()->message_name().c_str()
11353 : "unnamed struct"));
11354 return Expression::make_error(location);
11360 Expression_list* list = new Expression_list;
11361 list->reserve(field_count);
11362 for (size_t i = 0; i < field_count; ++i)
11363 list->push_back(vals[i]);
11365 return new Struct_construction_expression(type, list, location);
11368 // Lower an array composite literal.
11371 Composite_literal_expression::lower_array(Type* type)
11373 source_location location = this->location();
11374 if (this->vals_ == NULL || !this->has_keys_)
11375 return this->make_array(type, this->vals_);
11377 std::vector<Expression*> vals;
11378 vals.reserve(this->vals_->size());
11379 unsigned long index = 0;
11380 Expression_list::const_iterator p = this->vals_->begin();
11381 while (p != this->vals_->end())
11383 Expression* index_expr = *p;
11386 gcc_assert(p != this->vals_->end());
11387 Expression* val = *p;
11391 if (index_expr != NULL)
11396 if (!index_expr->integer_constant_value(true, ival, &dummy))
11399 error_at(index_expr->location(),
11400 "index expression is not integer constant");
11401 return Expression::make_error(location);
11403 if (mpz_sgn(ival) < 0)
11406 error_at(index_expr->location(), "index expression is negative");
11407 return Expression::make_error(location);
11409 index = mpz_get_ui(ival);
11410 if (mpz_cmp_ui(ival, index) != 0)
11413 error_at(index_expr->location(), "index value overflow");
11414 return Expression::make_error(location);
11419 if (index == vals.size())
11420 vals.push_back(val);
11423 if (index > vals.size())
11425 vals.reserve(index + 32);
11426 vals.resize(index + 1, static_cast<Expression*>(NULL));
11428 if (vals[index] != NULL)
11430 error_at((index_expr != NULL
11431 ? index_expr->location()
11432 : val->location()),
11433 "duplicate value for index %lu",
11435 return Expression::make_error(location);
11443 size_t size = vals.size();
11444 Expression_list* list = new Expression_list;
11445 list->reserve(size);
11446 for (size_t i = 0; i < size; ++i)
11447 list->push_back(vals[i]);
11449 return this->make_array(type, list);
11452 // Actually build the array composite literal. This handles
11456 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
11458 source_location location = this->location();
11459 Array_type* at = type->array_type();
11460 if (at->length() != NULL && at->length()->is_nil_expression())
11462 size_t size = vals == NULL ? 0 : vals->size();
11464 mpz_init_set_ui(vlen, size);
11465 Expression* elen = Expression::make_integer(&vlen, NULL, location);
11467 at = Type::make_array_type(at->element_type(), elen);
11470 if (at->length() != NULL)
11471 return new Fixed_array_construction_expression(type, vals, location);
11473 return new Open_array_construction_expression(type, vals, location);
11476 // Lower a map composite literal.
11479 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
11482 source_location location = this->location();
11483 if (this->vals_ != NULL)
11485 if (!this->has_keys_)
11487 error_at(location, "map composite literal must have keys");
11488 return Expression::make_error(location);
11491 for (Expression_list::iterator p = this->vals_->begin();
11492 p != this->vals_->end();
11498 error_at((*p)->location(),
11499 "map composite literal must have keys for every value");
11500 return Expression::make_error(location);
11502 // Make sure we have lowered the key; it may not have been
11503 // lowered in order to handle keys for struct composite
11504 // literals. Lower it now to get the right error message.
11505 if ((*p)->unknown_expression() != NULL)
11507 (*p)->unknown_expression()->clear_is_composite_literal_key();
11508 gogo->lower_expression(function, &*p);
11509 gcc_assert((*p)->is_error_expression());
11510 return Expression::make_error(location);
11515 return new Map_construction_expression(type, this->vals_, location);
11518 // Make a composite literal expression.
11521 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
11522 Expression_list* vals,
11523 source_location location)
11525 return new Composite_literal_expression(type, depth, has_keys, vals,
11529 // Return whether this expression is a composite literal.
11532 Expression::is_composite_literal() const
11534 switch (this->classification_)
11536 case EXPRESSION_COMPOSITE_LITERAL:
11537 case EXPRESSION_STRUCT_CONSTRUCTION:
11538 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
11539 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
11540 case EXPRESSION_MAP_CONSTRUCTION:
11547 // Return whether this expression is a composite literal which is not
11551 Expression::is_nonconstant_composite_literal() const
11553 switch (this->classification_)
11555 case EXPRESSION_STRUCT_CONSTRUCTION:
11557 const Struct_construction_expression *psce =
11558 static_cast<const Struct_construction_expression*>(this);
11559 return !psce->is_constant_struct();
11561 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
11563 const Fixed_array_construction_expression *pace =
11564 static_cast<const Fixed_array_construction_expression*>(this);
11565 return !pace->is_constant_array();
11567 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
11569 const Open_array_construction_expression *pace =
11570 static_cast<const Open_array_construction_expression*>(this);
11571 return !pace->is_constant_array();
11573 case EXPRESSION_MAP_CONSTRUCTION:
11580 // Return true if this is a reference to a local variable.
11583 Expression::is_local_variable() const
11585 const Var_expression* ve = this->var_expression();
11588 const Named_object* no = ve->named_object();
11589 return (no->is_result_variable()
11590 || (no->is_variable() && !no->var_value()->is_global()));
11593 // Class Type_guard_expression.
11598 Type_guard_expression::do_traverse(Traverse* traverse)
11600 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
11601 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11602 return TRAVERSE_EXIT;
11603 return TRAVERSE_CONTINUE;
11606 // Check types of a type guard expression. The expression must have
11607 // an interface type, but the actual type conversion is checked at run
11611 Type_guard_expression::do_check_types(Gogo*)
11613 // 6g permits using a type guard with unsafe.pointer; we are
11615 Type* expr_type = this->expr_->type();
11616 if (expr_type->is_unsafe_pointer_type())
11618 if (this->type_->points_to() == NULL
11619 && (this->type_->integer_type() == NULL
11620 || (this->type_->forwarded()
11621 != Type::lookup_integer_type("uintptr"))))
11622 this->report_error(_("invalid unsafe.Pointer conversion"));
11624 else if (this->type_->is_unsafe_pointer_type())
11626 if (expr_type->points_to() == NULL
11627 && (expr_type->integer_type() == NULL
11628 || (expr_type->forwarded()
11629 != Type::lookup_integer_type("uintptr"))))
11630 this->report_error(_("invalid unsafe.Pointer conversion"));
11632 else if (expr_type->interface_type() == NULL)
11634 if (!expr_type->is_error_type() && !this->type_->is_error_type())
11635 this->report_error(_("type assertion only valid for interface types"));
11636 this->set_is_error();
11638 else if (this->type_->interface_type() == NULL)
11640 std::string reason;
11641 if (!expr_type->interface_type()->implements_interface(this->type_,
11644 if (!this->type_->is_error_type())
11646 if (reason.empty())
11647 this->report_error(_("impossible type assertion: "
11648 "type does not implement interface"));
11650 error_at(this->location(),
11651 ("impossible type assertion: "
11652 "type does not implement interface (%s)"),
11655 this->set_is_error();
11660 // Return a tree for a type guard expression.
11663 Type_guard_expression::do_get_tree(Translate_context* context)
11665 Gogo* gogo = context->gogo();
11666 tree expr_tree = this->expr_->get_tree(context);
11667 if (expr_tree == error_mark_node)
11668 return error_mark_node;
11669 Type* expr_type = this->expr_->type();
11670 if ((this->type_->is_unsafe_pointer_type()
11671 && (expr_type->points_to() != NULL
11672 || expr_type->integer_type() != NULL))
11673 || (expr_type->is_unsafe_pointer_type()
11674 && this->type_->points_to() != NULL))
11675 return convert_to_pointer(this->type_->get_tree(gogo), expr_tree);
11676 else if (expr_type->is_unsafe_pointer_type()
11677 && this->type_->integer_type() != NULL)
11678 return convert_to_integer(this->type_->get_tree(gogo), expr_tree);
11679 else if (this->type_->interface_type() != NULL)
11680 return Expression::convert_interface_to_interface(context, this->type_,
11681 this->expr_->type(),
11685 return Expression::convert_for_assignment(context, this->type_,
11686 this->expr_->type(), expr_tree,
11690 // Make a type guard expression.
11693 Expression::make_type_guard(Expression* expr, Type* type,
11694 source_location location)
11696 return new Type_guard_expression(expr, type, location);
11699 // Class Heap_composite_expression.
11701 // When you take the address of a composite literal, it is allocated
11702 // on the heap. This class implements that.
11704 class Heap_composite_expression : public Expression
11707 Heap_composite_expression(Expression* expr, source_location location)
11708 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
11714 do_traverse(Traverse* traverse)
11715 { return Expression::traverse(&this->expr_, traverse); }
11719 { return Type::make_pointer_type(this->expr_->type()); }
11722 do_determine_type(const Type_context*)
11723 { this->expr_->determine_type_no_context(); }
11728 return Expression::make_heap_composite(this->expr_->copy(),
11733 do_get_tree(Translate_context*);
11735 // We only export global objects, and the parser does not generate
11736 // this in global scope.
11738 do_export(Export*) const
11739 { gcc_unreachable(); }
11742 // The composite literal which is being put on the heap.
11746 // Return a tree which allocates a composite literal on the heap.
11749 Heap_composite_expression::do_get_tree(Translate_context* context)
11751 tree expr_tree = this->expr_->get_tree(context);
11752 if (expr_tree == error_mark_node)
11753 return error_mark_node;
11754 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
11755 gcc_assert(TREE_CODE(expr_size) == INTEGER_CST);
11756 tree space = context->gogo()->allocate_memory(this->expr_->type(),
11757 expr_size, this->location());
11758 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
11759 space = save_expr(space);
11760 tree ref = build_fold_indirect_ref_loc(this->location(), space);
11761 TREE_THIS_NOTRAP(ref) = 1;
11762 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
11763 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
11765 SET_EXPR_LOCATION(ret, this->location());
11769 // Allocate a composite literal on the heap.
11772 Expression::make_heap_composite(Expression* expr, source_location location)
11774 return new Heap_composite_expression(expr, location);
11777 // Class Receive_expression.
11779 // Return the type of a receive expression.
11782 Receive_expression::do_type()
11784 Channel_type* channel_type = this->channel_->type()->channel_type();
11785 if (channel_type == NULL)
11786 return Type::make_error_type();
11787 return channel_type->element_type();
11790 // Check types for a receive expression.
11793 Receive_expression::do_check_types(Gogo*)
11795 Type* type = this->channel_->type();
11796 if (type->is_error_type())
11798 this->set_is_error();
11801 if (type->channel_type() == NULL)
11803 this->report_error(_("expected channel"));
11806 if (!type->channel_type()->may_receive())
11808 this->report_error(_("invalid receive on send-only channel"));
11813 // Get a tree for a receive expression.
11816 Receive_expression::do_get_tree(Translate_context* context)
11818 Channel_type* channel_type = this->channel_->type()->channel_type();
11819 gcc_assert(channel_type != NULL);
11820 Type* element_type = channel_type->element_type();
11821 tree element_type_tree = element_type->get_tree(context->gogo());
11823 tree channel = this->channel_->get_tree(context);
11824 if (element_type_tree == error_mark_node || channel == error_mark_node)
11825 return error_mark_node;
11827 return Gogo::receive_from_channel(element_type_tree, channel,
11828 this->for_select_, this->location());
11831 // Make a receive expression.
11833 Receive_expression*
11834 Expression::make_receive(Expression* channel, source_location location)
11836 return new Receive_expression(channel, location);
11839 // Class Send_expression.
11844 Send_expression::do_traverse(Traverse* traverse)
11846 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
11847 return TRAVERSE_EXIT;
11848 return Expression::traverse(&this->val_, traverse);
11854 Send_expression::do_type()
11856 return Type::lookup_bool_type();
11862 Send_expression::do_determine_type(const Type_context*)
11864 this->channel_->determine_type_no_context();
11866 Type* type = this->channel_->type();
11867 Type_context subcontext;
11868 if (type->channel_type() != NULL)
11869 subcontext.type = type->channel_type()->element_type();
11870 this->val_->determine_type(&subcontext);
11876 Send_expression::do_check_types(Gogo*)
11878 Type* type = this->channel_->type();
11879 if (type->is_error_type())
11881 this->set_is_error();
11884 Channel_type* channel_type = type->channel_type();
11885 if (channel_type == NULL)
11887 error_at(this->location(), "left operand of %<<-%> must be channel");
11888 this->set_is_error();
11891 Type* element_type = channel_type->element_type();
11892 if (element_type != NULL
11893 && !Type::are_assignable(element_type, this->val_->type(), NULL))
11895 this->report_error(_("incompatible types in send"));
11898 if (!channel_type->may_send())
11900 this->report_error(_("invalid send on receive-only channel"));
11905 // Get a tree for a send expression.
11908 Send_expression::do_get_tree(Translate_context* context)
11910 tree channel = this->channel_->get_tree(context);
11911 tree val = this->val_->get_tree(context);
11912 if (channel == error_mark_node || val == error_mark_node)
11913 return error_mark_node;
11914 Channel_type* channel_type = this->channel_->type()->channel_type();
11915 val = Expression::convert_for_assignment(context,
11916 channel_type->element_type(),
11917 this->val_->type(),
11920 return Gogo::send_on_channel(channel, val, this->is_value_discarded_,
11921 this->for_select_, this->location());
11924 // Make a send expression
11927 Expression::make_send(Expression* channel, Expression* val,
11928 source_location location)
11930 return new Send_expression(channel, val, location);
11933 // An expression which evaluates to a pointer to the type descriptor
11936 class Type_descriptor_expression : public Expression
11939 Type_descriptor_expression(Type* type, source_location location)
11940 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
11947 { return Type::make_type_descriptor_ptr_type(); }
11950 do_determine_type(const Type_context*)
11958 do_get_tree(Translate_context* context)
11959 { return this->type_->type_descriptor_pointer(context->gogo()); }
11962 // The type for which this is the descriptor.
11966 // Make a type descriptor expression.
11969 Expression::make_type_descriptor(Type* type, source_location location)
11971 return new Type_descriptor_expression(type, location);
11974 // An expression which evaluates to some characteristic of a type.
11975 // This is only used to initialize fields of a type descriptor. Using
11976 // a new expression class is slightly inefficient but gives us a good
11977 // separation between the frontend and the middle-end with regard to
11978 // how types are laid out.
11980 class Type_info_expression : public Expression
11983 Type_info_expression(Type* type, Type_info type_info)
11984 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
11985 type_(type), type_info_(type_info)
11993 do_determine_type(const Type_context*)
12001 do_get_tree(Translate_context* context);
12004 // The type for which we are getting information.
12006 // What information we want.
12007 Type_info type_info_;
12010 // The type is chosen to match what the type descriptor struct
12014 Type_info_expression::do_type()
12016 switch (this->type_info_)
12018 case TYPE_INFO_SIZE:
12019 return Type::lookup_integer_type("uintptr");
12020 case TYPE_INFO_ALIGNMENT:
12021 case TYPE_INFO_FIELD_ALIGNMENT:
12022 return Type::lookup_integer_type("uint8");
12028 // Return type information in GENERIC.
12031 Type_info_expression::do_get_tree(Translate_context* context)
12033 tree type_tree = this->type_->get_tree(context->gogo());
12034 if (type_tree == error_mark_node)
12035 return error_mark_node;
12037 tree val_type_tree = this->type()->get_tree(context->gogo());
12038 gcc_assert(val_type_tree != error_mark_node);
12040 if (this->type_info_ == TYPE_INFO_SIZE)
12041 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12042 TYPE_SIZE_UNIT(type_tree));
12046 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12047 val = go_type_alignment(type_tree);
12049 val = go_field_alignment(type_tree);
12050 return build_int_cstu(val_type_tree, val);
12054 // Make a type info expression.
12057 Expression::make_type_info(Type* type, Type_info type_info)
12059 return new Type_info_expression(type, type_info);
12062 // An expression which evaluates to the offset of a field within a
12063 // struct. This, like Type_info_expression, q.v., is only used to
12064 // initialize fields of a type descriptor.
12066 class Struct_field_offset_expression : public Expression
12069 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12070 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12071 type_(type), field_(field)
12077 { return Type::lookup_integer_type("uintptr"); }
12080 do_determine_type(const Type_context*)
12088 do_get_tree(Translate_context* context);
12091 // The type of the struct.
12092 Struct_type* type_;
12094 const Struct_field* field_;
12097 // Return a struct field offset in GENERIC.
12100 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12102 tree type_tree = this->type_->get_tree(context->gogo());
12103 if (type_tree == error_mark_node)
12104 return error_mark_node;
12106 tree val_type_tree = this->type()->get_tree(context->gogo());
12107 gcc_assert(val_type_tree != error_mark_node);
12109 const Struct_field_list* fields = this->type_->fields();
12110 tree struct_field_tree = TYPE_FIELDS(type_tree);
12111 Struct_field_list::const_iterator p;
12112 for (p = fields->begin();
12113 p != fields->end();
12114 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12116 gcc_assert(struct_field_tree != NULL_TREE);
12117 if (&*p == this->field_)
12120 gcc_assert(&*p == this->field_);
12122 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12123 byte_position(struct_field_tree));
12126 // Make an expression for a struct field offset.
12129 Expression::make_struct_field_offset(Struct_type* type,
12130 const Struct_field* field)
12132 return new Struct_field_offset_expression(type, field);
12135 // An expression which evaluates to the address of an unnamed label.
12137 class Label_addr_expression : public Expression
12140 Label_addr_expression(Label* label, source_location location)
12141 : Expression(EXPRESSION_LABEL_ADDR, location),
12148 { return Type::make_pointer_type(Type::make_void_type()); }
12151 do_determine_type(const Type_context*)
12156 { return new Label_addr_expression(this->label_, this->location()); }
12159 do_get_tree(Translate_context*)
12160 { return this->label_->get_addr(this->location()); }
12163 // The label whose address we are taking.
12167 // Make an expression for the address of an unnamed label.
12170 Expression::make_label_addr(Label* label, source_location location)
12172 return new Label_addr_expression(label, location);
12175 // Import an expression. This comes at the end in order to see the
12176 // various class definitions.
12179 Expression::import_expression(Import* imp)
12181 int c = imp->peek_char();
12182 if (imp->match_c_string("- ")
12183 || imp->match_c_string("! ")
12184 || imp->match_c_string("^ "))
12185 return Unary_expression::do_import(imp);
12187 return Binary_expression::do_import(imp);
12188 else if (imp->match_c_string("true")
12189 || imp->match_c_string("false"))
12190 return Boolean_expression::do_import(imp);
12192 return String_expression::do_import(imp);
12193 else if (c == '-' || (c >= '0' && c <= '9'))
12195 // This handles integers, floats and complex constants.
12196 return Integer_expression::do_import(imp);
12198 else if (imp->match_c_string("nil"))
12199 return Nil_expression::do_import(imp);
12200 else if (imp->match_c_string("convert"))
12201 return Type_conversion_expression::do_import(imp);
12204 error_at(imp->location(), "import error: expected expression");
12205 return Expression::make_error(imp->location());
12209 // Class Expression_list.
12211 // Traverse the list.
12214 Expression_list::traverse(Traverse* traverse)
12216 for (Expression_list::iterator p = this->begin();
12222 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12223 return TRAVERSE_EXIT;
12226 return TRAVERSE_CONTINUE;
12232 Expression_list::copy()
12234 Expression_list* ret = new Expression_list();
12235 for (Expression_list::iterator p = this->begin();
12240 ret->push_back(NULL);
12242 ret->push_back((*p)->copy());
12247 // Return whether an expression list has an error expression.
12250 Expression_list::contains_error() const
12252 for (Expression_list::const_iterator p = this->begin();
12255 if (*p != NULL && (*p)->is_error_expression())