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"
27 #ifndef ENABLE_BUILD_WITH_CXX
36 #include "statements.h"
38 #include "expressions.h"
42 Expression::Expression(Expression_classification classification,
43 source_location location)
44 : classification_(classification), location_(location)
48 Expression::~Expression()
52 // If this expression has a constant integer value, return it.
55 Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
59 return this->do_integer_constant_value(iota_is_constant, val, ptype);
62 // If this expression has a constant floating point value, return it.
65 Expression::float_constant_value(mpfr_t val, Type** ptype) const
68 if (this->do_float_constant_value(val, ptype))
74 if (!this->do_integer_constant_value(false, ival, &t))
78 mpfr_set_z(val, ival, GMP_RNDN);
85 // If this expression has a constant complex value, return it.
88 Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
92 if (this->do_complex_constant_value(real, imag, ptype))
95 if (this->float_constant_value(real, &t))
97 mpfr_set_ui(imag, 0, GMP_RNDN);
103 // Traverse the expressions.
106 Expression::traverse(Expression** pexpr, Traverse* traverse)
108 Expression* expr = *pexpr;
109 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
111 int t = traverse->expression(pexpr);
112 if (t == TRAVERSE_EXIT)
113 return TRAVERSE_EXIT;
114 else if (t == TRAVERSE_SKIP_COMPONENTS)
115 return TRAVERSE_CONTINUE;
117 return expr->do_traverse(traverse);
120 // Traverse subexpressions of this expression.
123 Expression::traverse_subexpressions(Traverse* traverse)
125 return this->do_traverse(traverse);
128 // Default implementation for do_traverse for child classes.
131 Expression::do_traverse(Traverse*)
133 return TRAVERSE_CONTINUE;
136 // This virtual function is called by the parser if the value of this
137 // expression is being discarded. By default, we warn. Expressions
138 // with side effects override.
141 Expression::do_discarding_value()
143 this->warn_about_unused_value();
146 // This virtual function is called to export expressions. This will
147 // only be used by expressions which may be constant.
150 Expression::do_export(Export*) const
155 // Warn that the value of the expression is not used.
158 Expression::warn_about_unused_value()
160 warning_at(this->location(), OPT_Wunused_value, "value computed is not used");
163 // Note that this expression is an error. This is called by children
164 // when they discover an error.
167 Expression::set_is_error()
169 this->classification_ = EXPRESSION_ERROR;
172 // For children to call to report an error conveniently.
175 Expression::report_error(const char* msg)
177 error_at(this->location_, "%s", msg);
178 this->set_is_error();
181 // Set types of variables and constants. This is implemented by the
185 Expression::determine_type(const Type_context* context)
187 this->do_determine_type(context);
190 // Set types when there is no context.
193 Expression::determine_type_no_context()
195 Type_context context;
196 this->do_determine_type(&context);
199 // Return a tree handling any conversions which must be done during
203 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
204 Type* rhs_type, tree rhs_tree,
205 source_location location)
207 if (lhs_type == rhs_type)
210 if (lhs_type->is_error_type() || rhs_type->is_error_type())
211 return error_mark_node;
213 if (lhs_type->is_undefined() || rhs_type->is_undefined())
215 // Make sure we report the error.
218 return error_mark_node;
221 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
222 return error_mark_node;
224 Gogo* gogo = context->gogo();
226 tree lhs_type_tree = lhs_type->get_tree(gogo);
227 if (lhs_type_tree == error_mark_node)
228 return error_mark_node;
230 if (lhs_type->interface_type() != NULL)
232 if (rhs_type->interface_type() == NULL)
233 return Expression::convert_type_to_interface(context, lhs_type,
237 return Expression::convert_interface_to_interface(context, lhs_type,
241 else if (rhs_type->interface_type() != NULL)
242 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
244 else if (lhs_type->is_open_array_type()
245 && rhs_type->is_nil_type())
247 // Assigning nil to an open array.
248 gcc_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
250 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
252 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
253 tree field = TYPE_FIELDS(lhs_type_tree);
254 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
257 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
259 elt = VEC_quick_push(constructor_elt, init, NULL);
260 field = DECL_CHAIN(field);
261 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
264 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
266 elt = VEC_quick_push(constructor_elt, init, NULL);
267 field = DECL_CHAIN(field);
268 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
271 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
273 tree val = build_constructor(lhs_type_tree, init);
274 TREE_CONSTANT(val) = 1;
278 else if (rhs_type->is_nil_type())
280 // The left hand side should be a pointer type at the tree
282 gcc_assert(POINTER_TYPE_P(lhs_type_tree));
283 return fold_convert(lhs_type_tree, null_pointer_node);
285 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
287 // No conversion is needed.
290 else if (POINTER_TYPE_P(lhs_type_tree)
291 || INTEGRAL_TYPE_P(lhs_type_tree)
292 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
293 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
294 return fold_convert_loc(location, lhs_type_tree, rhs_tree);
295 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
296 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
298 // This conversion must be permitted by Go, or we wouldn't have
300 gcc_assert(int_size_in_bytes(lhs_type_tree)
301 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
302 return fold_build1_loc(location, VIEW_CONVERT_EXPR, lhs_type_tree,
307 gcc_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
312 // Return a tree for a conversion from a non-interface type to an
316 Expression::convert_type_to_interface(Translate_context* context,
317 Type* lhs_type, Type* rhs_type,
318 tree rhs_tree, source_location location)
320 Gogo* gogo = context->gogo();
321 Interface_type* lhs_interface_type = lhs_type->interface_type();
322 bool lhs_is_empty = lhs_interface_type->is_empty();
324 // Since RHS_TYPE is a static type, we can create the interface
325 // method table at compile time.
327 // When setting an interface to nil, we just set both fields to
329 if (rhs_type->is_nil_type())
330 return lhs_type->get_init_tree(gogo, false);
332 // This should have been checked already.
333 gcc_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
335 tree lhs_type_tree = lhs_type->get_tree(gogo);
336 if (lhs_type_tree == error_mark_node)
337 return error_mark_node;
339 // An interface is a tuple. If LHS_TYPE is an empty interface type,
340 // then the first field is the type descriptor for RHS_TYPE.
341 // Otherwise it is the interface method table for RHS_TYPE.
342 tree first_field_value;
344 first_field_value = rhs_type->type_descriptor_pointer(gogo);
347 // Build the interface method table for this interface and this
348 // object type: a list of function pointers for each interface
350 Named_type* rhs_named_type = rhs_type->named_type();
351 bool is_pointer = false;
352 if (rhs_named_type == NULL)
354 rhs_named_type = rhs_type->deref()->named_type();
358 if (rhs_named_type == NULL)
359 method_table = null_pointer_node;
362 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
364 first_field_value = fold_convert_loc(location, const_ptr_type_node,
368 // Start building a constructor for the value we will return.
370 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
372 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
373 tree field = TYPE_FIELDS(lhs_type_tree);
374 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
375 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
377 elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value);
379 elt = VEC_quick_push(constructor_elt, init, NULL);
380 field = DECL_CHAIN(field);
381 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
384 if (rhs_type->points_to() != NULL)
386 // We are assigning a pointer to the interface; the interface
387 // holds the pointer itself.
388 elt->value = rhs_tree;
389 return build_constructor(lhs_type_tree, init);
392 // We are assigning a non-pointer value to the interface; the
393 // interface gets a copy of the value in the heap.
395 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
397 tree space = gogo->allocate_memory(rhs_type, object_size, location);
398 space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)),
400 space = save_expr(space);
402 tree ref = build_fold_indirect_ref_loc(location, space);
403 TREE_THIS_NOTRAP(ref) = 1;
404 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
407 elt->value = fold_convert_loc(location, TREE_TYPE(field), space);
409 return build2(COMPOUND_EXPR, lhs_type_tree, set,
410 build_constructor(lhs_type_tree, init));
413 // Return a tree for the type descriptor of RHS_TREE, which has
414 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
418 Expression::get_interface_type_descriptor(Translate_context*,
419 Type* rhs_type, tree rhs_tree,
420 source_location location)
422 tree rhs_type_tree = TREE_TYPE(rhs_tree);
423 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
424 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
425 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
427 if (rhs_type->interface_type()->is_empty())
429 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
430 "__type_descriptor") == 0);
434 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
436 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v)));
438 tree v1 = build_fold_indirect_ref_loc(location, v);
439 gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
440 tree f = TYPE_FIELDS(TREE_TYPE(v1));
441 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
443 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
445 tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v,
446 fold_convert_loc(location, TREE_TYPE(v),
448 tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node);
449 return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1),
453 // Return a tree for the conversion of an interface type to an
457 Expression::convert_interface_to_interface(Translate_context* context,
458 Type *lhs_type, Type *rhs_type,
459 tree rhs_tree, bool for_type_guard,
460 source_location location)
462 Gogo* gogo = context->gogo();
463 Interface_type* lhs_interface_type = lhs_type->interface_type();
464 bool lhs_is_empty = lhs_interface_type->is_empty();
466 tree lhs_type_tree = lhs_type->get_tree(gogo);
467 if (lhs_type_tree == error_mark_node)
468 return error_mark_node;
470 // In the general case this requires runtime examination of the type
471 // method table to match it up with the interface methods.
473 // FIXME: If all of the methods in the right hand side interface
474 // also appear in the left hand side interface, then we don't need
475 // to do a runtime check, although we still need to build a new
478 // Get the type descriptor for the right hand side. This will be
479 // NULL for a nil interface.
481 if (!DECL_P(rhs_tree))
482 rhs_tree = save_expr(rhs_tree);
484 tree rhs_type_descriptor =
485 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
488 // The result is going to be a two element constructor.
490 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
492 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
493 tree field = TYPE_FIELDS(lhs_type_tree);
498 // A type assertion fails when converting a nil interface.
499 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
500 static tree assert_interface_decl;
501 tree call = Gogo::call_builtin(&assert_interface_decl,
503 "__go_assert_interface",
506 TREE_TYPE(lhs_type_descriptor),
508 TREE_TYPE(rhs_type_descriptor),
509 rhs_type_descriptor);
510 // This will panic if the interface conversion fails.
511 TREE_NOTHROW(assert_interface_decl) = 0;
512 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
514 else if (lhs_is_empty)
516 // A convertion to an empty interface always succeeds, and the
517 // first field is just the type descriptor of the object.
518 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
519 "__type_descriptor") == 0);
520 gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
521 elt->value = rhs_type_descriptor;
525 // A conversion to a non-empty interface may fail, but unlike a
526 // type assertion converting nil will always succeed.
527 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
529 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
530 static tree convert_interface_decl;
531 tree call = Gogo::call_builtin(&convert_interface_decl,
533 "__go_convert_interface",
536 TREE_TYPE(lhs_type_descriptor),
538 TREE_TYPE(rhs_type_descriptor),
539 rhs_type_descriptor);
540 // This will panic if the interface conversion fails.
541 TREE_NOTHROW(convert_interface_decl) = 0;
542 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
545 // The second field is simply the object pointer.
547 elt = VEC_quick_push(constructor_elt, init, NULL);
548 field = DECL_CHAIN(field);
549 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
552 tree rhs_type_tree = TREE_TYPE(rhs_tree);
553 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
554 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
555 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
556 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
559 return build_constructor(lhs_type_tree, init);
562 // Return a tree for the conversion of an interface type to a
563 // non-interface type.
566 Expression::convert_interface_to_type(Translate_context* context,
567 Type *lhs_type, Type* rhs_type,
568 tree rhs_tree, source_location location)
570 Gogo* gogo = context->gogo();
571 tree rhs_type_tree = TREE_TYPE(rhs_tree);
573 tree lhs_type_tree = lhs_type->get_tree(gogo);
574 if (lhs_type_tree == error_mark_node)
575 return error_mark_node;
577 // Call a function to check that the type is valid. The function
578 // will panic with an appropriate runtime type error if the type is
581 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
583 if (!DECL_P(rhs_tree))
584 rhs_tree = save_expr(rhs_tree);
586 tree rhs_type_descriptor =
587 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
590 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo);
592 static tree check_interface_type_decl;
593 tree call = Gogo::call_builtin(&check_interface_type_decl,
595 "__go_check_interface_type",
598 TREE_TYPE(lhs_type_descriptor),
600 TREE_TYPE(rhs_type_descriptor),
602 TREE_TYPE(rhs_inter_descriptor),
603 rhs_inter_descriptor);
604 // This call will panic if the conversion is invalid.
605 TREE_NOTHROW(check_interface_type_decl) = 0;
607 // If the call succeeds, pull out the value.
608 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
609 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
610 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
611 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
614 // If the value is a pointer, then it is the value we want.
615 // Otherwise it points to the value.
616 if (lhs_type->points_to() == NULL)
618 val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val);
619 val = build_fold_indirect_ref_loc(location, val);
622 return build2(COMPOUND_EXPR, lhs_type_tree, call,
623 fold_convert_loc(location, lhs_type_tree, val));
626 // Convert an expression to a tree. This is implemented by the child
627 // class. Not that it is not in general safe to call this multiple
628 // times for a single expression, but that we don't catch such errors.
631 Expression::get_tree(Translate_context* context)
633 // The child may have marked this expression as having an error.
634 if (this->classification_ == EXPRESSION_ERROR)
635 return error_mark_node;
637 return this->do_get_tree(context);
640 // Return a tree for VAL in TYPE.
643 Expression::integer_constant_tree(mpz_t val, tree type)
645 if (type == error_mark_node)
646 return error_mark_node;
647 else if (TREE_CODE(type) == INTEGER_TYPE)
648 return double_int_to_tree(type,
649 mpz_get_double_int(type, val, true));
650 else if (TREE_CODE(type) == REAL_TYPE)
653 mpfr_init_set_z(fval, val, GMP_RNDN);
654 tree ret = Expression::float_constant_tree(fval, type);
658 else if (TREE_CODE(type) == COMPLEX_TYPE)
661 mpfr_init_set_z(fval, val, GMP_RNDN);
662 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
664 tree imag = build_real_from_int_cst(TREE_TYPE(type),
666 return build_complex(type, real, imag);
672 // Return a tree for VAL in TYPE.
675 Expression::float_constant_tree(mpfr_t val, tree type)
677 if (type == error_mark_node)
678 return error_mark_node;
679 else if (TREE_CODE(type) == INTEGER_TYPE)
683 mpfr_get_z(ival, val, GMP_RNDN);
684 tree ret = Expression::integer_constant_tree(ival, type);
688 else if (TREE_CODE(type) == REAL_TYPE)
691 real_from_mpfr(&r1, val, type, GMP_RNDN);
693 real_convert(&r2, TYPE_MODE(type), &r1);
694 return build_real(type, r2);
696 else if (TREE_CODE(type) == COMPLEX_TYPE)
699 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
701 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
702 tree imag = build_real_from_int_cst(TREE_TYPE(type),
704 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
710 // Return a tree for REAL/IMAG in TYPE.
713 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
715 if (TREE_CODE(type) == COMPLEX_TYPE)
718 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
720 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
723 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
725 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
727 return build_complex(type, build_real(TREE_TYPE(type), r2),
728 build_real(TREE_TYPE(type), r4));
734 // Return a tree which evaluates to true if VAL, of arbitrary integer
735 // type, is negative or is more than the maximum value of BOUND_TYPE.
736 // If SOFAR is not NULL, it is or'red into the result. The return
737 // value may be NULL if SOFAR is NULL.
740 Expression::check_bounds(tree val, tree bound_type, tree sofar,
743 tree val_type = TREE_TYPE(val);
744 tree ret = NULL_TREE;
746 if (!TYPE_UNSIGNED(val_type))
748 ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val,
749 build_int_cst(val_type, 0));
750 if (ret == boolean_false_node)
754 if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type))
755 || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type))
757 tree max = TYPE_MAX_VALUE(bound_type);
758 tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val,
759 fold_convert_loc(loc, val_type, max));
760 if (big == boolean_false_node)
762 else if (ret == NULL_TREE)
765 ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
769 if (ret == NULL_TREE)
771 else if (sofar == NULL_TREE)
774 return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
778 // Error expressions. This are used to avoid cascading errors.
780 class Error_expression : public Expression
783 Error_expression(source_location location)
784 : Expression(EXPRESSION_ERROR, location)
789 do_is_constant() const
793 do_integer_constant_value(bool, mpz_t val, Type**) const
800 do_float_constant_value(mpfr_t val, Type**) const
802 mpfr_set_ui(val, 0, GMP_RNDN);
807 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
809 mpfr_set_ui(real, 0, GMP_RNDN);
810 mpfr_set_ui(imag, 0, GMP_RNDN);
815 do_discarding_value()
820 { return Type::make_error_type(); }
823 do_determine_type(const Type_context*)
831 do_is_addressable() const
835 do_get_tree(Translate_context*)
836 { return error_mark_node; }
840 Expression::make_error(source_location location)
842 return new Error_expression(location);
845 // An expression which is really a type. This is used during parsing.
846 // It is an error if these survive after lowering.
849 Type_expression : public Expression
852 Type_expression(Type* type, source_location location)
853 : Expression(EXPRESSION_TYPE, location),
859 do_traverse(Traverse* traverse)
860 { return Type::traverse(this->type_, traverse); }
864 { return this->type_; }
867 do_determine_type(const Type_context*)
871 do_check_types(Gogo*)
872 { this->report_error(_("invalid use of type")); }
879 do_get_tree(Translate_context*)
880 { gcc_unreachable(); }
883 // The type which we are representing as an expression.
888 Expression::make_type(Type* type, source_location location)
890 return new Type_expression(type, location);
893 // Class Var_expression.
895 // Lower a variable expression. Here we just make sure that the
896 // initialization expression of the variable has been lowered. This
897 // ensures that we will be able to determine the type of the variable
901 Var_expression::do_lower(Gogo* gogo, Named_object* function, int)
903 if (this->variable_->is_variable())
905 Variable* var = this->variable_->var_value();
906 // This is either a local variable or a global variable. A
907 // reference to a variable which is local to an enclosing
908 // function will be a reference to a field in a closure.
909 if (var->is_global())
911 var->lower_init_expression(gogo, function);
916 // Return the name of the variable.
919 Var_expression::name() const
921 return this->variable_->name();
924 // Return the type of a reference to a variable.
927 Var_expression::do_type()
929 if (this->variable_->is_variable())
930 return this->variable_->var_value()->type();
931 else if (this->variable_->is_result_variable())
932 return this->variable_->result_var_value()->type();
937 // Something takes the address of this variable. This means that we
938 // may want to move the variable onto the heap.
941 Var_expression::do_address_taken(bool escapes)
945 else if (this->variable_->is_variable())
946 this->variable_->var_value()->set_address_taken();
947 else if (this->variable_->is_result_variable())
948 this->variable_->result_var_value()->set_address_taken();
953 // Get the tree for a reference to a variable.
956 Var_expression::do_get_tree(Translate_context* context)
958 return this->variable_->get_tree(context->gogo(), context->function());
961 // Make a reference to a variable in an expression.
964 Expression::make_var_reference(Named_object* var, source_location location)
967 return Expression::make_sink(location);
969 // FIXME: Creating a new object for each reference to a variable is
971 return new Var_expression(var, location);
974 // Class Temporary_reference_expression.
979 Temporary_reference_expression::do_type()
981 return this->statement_->type();
984 // Called if something takes the address of this temporary variable.
985 // We never have to move temporary variables to the heap, but we do
986 // need to know that they must live in the stack rather than in a
990 Temporary_reference_expression::do_address_taken(bool)
992 this->statement_->set_is_address_taken();
995 // Get a tree referring to the variable.
998 Temporary_reference_expression::do_get_tree(Translate_context*)
1000 return this->statement_->get_decl();
1003 // Make a reference to a temporary variable.
1006 Expression::make_temporary_reference(Temporary_statement* statement,
1007 source_location location)
1009 return new Temporary_reference_expression(statement, location);
1012 // A sink expression--a use of the blank identifier _.
1014 class Sink_expression : public Expression
1017 Sink_expression(source_location location)
1018 : Expression(EXPRESSION_SINK, location),
1019 type_(NULL), var_(NULL_TREE)
1024 do_discarding_value()
1031 do_determine_type(const Type_context*);
1035 { return new Sink_expression(this->location()); }
1038 do_get_tree(Translate_context*);
1041 // The type of this sink variable.
1043 // The temporary variable we generate.
1047 // Return the type of a sink expression.
1050 Sink_expression::do_type()
1052 if (this->type_ == NULL)
1053 return Type::make_sink_type();
1057 // Determine the type of a sink expression.
1060 Sink_expression::do_determine_type(const Type_context* context)
1062 if (context->type != NULL)
1063 this->type_ = context->type;
1066 // Return a temporary variable for a sink expression. This will
1067 // presumably be a write-only variable which the middle-end will drop.
1070 Sink_expression::do_get_tree(Translate_context* context)
1072 if (this->var_ == NULL_TREE)
1074 gcc_assert(this->type_ != NULL && !this->type_->is_sink_type());
1075 this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()),
1081 // Make a sink expression.
1084 Expression::make_sink(source_location location)
1086 return new Sink_expression(location);
1089 // Class Func_expression.
1091 // FIXME: Can a function expression appear in a constant expression?
1092 // The value is unchanging. Initializing a constant to the address of
1093 // a function seems like it could work, though there might be little
1096 // Return the name of the function.
1099 Func_expression::name() const
1101 return this->function_->name();
1107 Func_expression::do_traverse(Traverse* traverse)
1109 return (this->closure_ == NULL
1111 : Expression::traverse(&this->closure_, traverse));
1114 // Return the type of a function expression.
1117 Func_expression::do_type()
1119 if (this->function_->is_function())
1120 return this->function_->func_value()->type();
1121 else if (this->function_->is_function_declaration())
1122 return this->function_->func_declaration_value()->type();
1127 // Get the tree for a function expression without evaluating the
1131 Func_expression::get_tree_without_closure(Gogo* gogo)
1133 Function_type* fntype;
1134 if (this->function_->is_function())
1135 fntype = this->function_->func_value()->type();
1136 else if (this->function_->is_function_declaration())
1137 fntype = this->function_->func_declaration_value()->type();
1141 // Builtin functions are handled specially by Call_expression. We
1142 // can't take their address.
1143 if (fntype->is_builtin())
1145 error_at(this->location(), "invalid use of special builtin function %qs",
1146 this->function_->name().c_str());
1147 return error_mark_node;
1150 Named_object* no = this->function_;
1151 tree id = this->function_->get_id(gogo);
1153 if (no->is_function())
1154 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1155 else if (no->is_function_declaration())
1156 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1160 return build_fold_addr_expr_loc(this->location(), fndecl);
1163 // Get the tree for a function expression. This is used when we take
1164 // the address of a function rather than simply calling it. If the
1165 // function has a closure, we must use a trampoline.
1168 Func_expression::do_get_tree(Translate_context* context)
1170 Gogo* gogo = context->gogo();
1172 tree fnaddr = this->get_tree_without_closure(gogo);
1173 if (fnaddr == error_mark_node)
1174 return error_mark_node;
1176 gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1177 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1178 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1180 // For a normal non-nested function call, that is all we have to do.
1181 if (!this->function_->is_function()
1182 || this->function_->func_value()->enclosing() == NULL)
1184 gcc_assert(this->closure_ == NULL);
1188 // For a nested function call, we have to always allocate a
1189 // trampoline. If we don't always allocate, then closures will not
1190 // be reliably distinct.
1191 Expression* closure = this->closure_;
1193 if (closure == NULL)
1194 closure_tree = null_pointer_node;
1197 // Get the value of the closure. This will be a pointer to
1198 // space allocated on the heap.
1199 closure_tree = closure->get_tree(context);
1200 if (closure_tree == error_mark_node)
1201 return error_mark_node;
1202 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1205 // Now we need to build some code on the heap. This code will load
1206 // the static chain pointer with the closure and then jump to the
1207 // body of the function. The normal gcc approach is to build the
1208 // code on the stack. Unfortunately we can not do that, as Go
1209 // permits us to return the function pointer.
1211 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1214 // Make a reference to a function in an expression.
1217 Expression::make_func_reference(Named_object* function, Expression* closure,
1218 source_location location)
1220 return new Func_expression(function, closure, location);
1223 // Class Unknown_expression.
1225 // Return the name of an unknown expression.
1228 Unknown_expression::name() const
1230 return this->named_object_->name();
1233 // Lower a reference to an unknown name.
1236 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1238 source_location location = this->location();
1239 Named_object* no = this->named_object_;
1240 Named_object* real = no->unknown_value()->real_named_object();
1243 if (this->is_composite_literal_key_)
1245 error_at(location, "reference to undefined name %qs",
1246 this->named_object_->message_name().c_str());
1247 return Expression::make_error(location);
1249 switch (real->classification())
1251 case Named_object::NAMED_OBJECT_CONST:
1252 return Expression::make_const_reference(real, location);
1253 case Named_object::NAMED_OBJECT_TYPE:
1254 return Expression::make_type(real->type_value(), location);
1255 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1256 if (this->is_composite_literal_key_)
1258 error_at(location, "reference to undefined type %qs",
1259 real->message_name().c_str());
1260 return Expression::make_error(location);
1261 case Named_object::NAMED_OBJECT_VAR:
1262 return Expression::make_var_reference(real, location);
1263 case Named_object::NAMED_OBJECT_FUNC:
1264 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1265 return Expression::make_func_reference(real, NULL, location);
1266 case Named_object::NAMED_OBJECT_PACKAGE:
1267 if (this->is_composite_literal_key_)
1269 error_at(location, "unexpected reference to package");
1270 return Expression::make_error(location);
1276 // Make a reference to an unknown name.
1279 Expression::make_unknown_reference(Named_object* no, source_location location)
1281 gcc_assert(no->resolve()->is_unknown());
1282 return new Unknown_expression(no, location);
1285 // A boolean expression.
1287 class Boolean_expression : public Expression
1290 Boolean_expression(bool val, source_location location)
1291 : Expression(EXPRESSION_BOOLEAN, location),
1292 val_(val), type_(NULL)
1300 do_is_constant() const
1307 do_determine_type(const Type_context*);
1314 do_get_tree(Translate_context*)
1315 { return this->val_ ? boolean_true_node : boolean_false_node; }
1318 do_export(Export* exp) const
1319 { exp->write_c_string(this->val_ ? "true" : "false"); }
1324 // The type as determined by context.
1331 Boolean_expression::do_type()
1333 if (this->type_ == NULL)
1334 this->type_ = Type::make_boolean_type();
1338 // Set the type from the context.
1341 Boolean_expression::do_determine_type(const Type_context* context)
1343 if (this->type_ != NULL && !this->type_->is_abstract())
1345 else if (context->type != NULL && context->type->is_boolean_type())
1346 this->type_ = context->type;
1347 else if (!context->may_be_abstract)
1348 this->type_ = Type::lookup_bool_type();
1351 // Import a boolean constant.
1354 Boolean_expression::do_import(Import* imp)
1356 if (imp->peek_char() == 't')
1358 imp->require_c_string("true");
1359 return Expression::make_boolean(true, imp->location());
1363 imp->require_c_string("false");
1364 return Expression::make_boolean(false, imp->location());
1368 // Make a boolean expression.
1371 Expression::make_boolean(bool val, source_location location)
1373 return new Boolean_expression(val, location);
1376 // Class String_expression.
1381 String_expression::do_type()
1383 if (this->type_ == NULL)
1384 this->type_ = Type::make_string_type();
1388 // Set the type from the context.
1391 String_expression::do_determine_type(const Type_context* context)
1393 if (this->type_ != NULL && !this->type_->is_abstract())
1395 else if (context->type != NULL && context->type->is_string_type())
1396 this->type_ = context->type;
1397 else if (!context->may_be_abstract)
1398 this->type_ = Type::lookup_string_type();
1401 // Build a string constant.
1404 String_expression::do_get_tree(Translate_context* context)
1406 return context->gogo()->go_string_constant_tree(this->val_);
1409 // Export a string expression.
1412 String_expression::do_export(Export* exp) const
1415 s.reserve(this->val_.length() * 4 + 2);
1417 for (std::string::const_iterator p = this->val_.begin();
1418 p != this->val_.end();
1421 if (*p == '\\' || *p == '"')
1426 else if (*p >= 0x20 && *p < 0x7f)
1428 else if (*p == '\n')
1430 else if (*p == '\t')
1435 unsigned char c = *p;
1436 unsigned int dig = c >> 4;
1437 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1439 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1443 exp->write_string(s);
1446 // Import a string expression.
1449 String_expression::do_import(Import* imp)
1451 imp->require_c_string("\"");
1455 int c = imp->get_char();
1456 if (c == '"' || c == -1)
1459 val += static_cast<char>(c);
1462 c = imp->get_char();
1463 if (c == '\\' || c == '"')
1464 val += static_cast<char>(c);
1471 c = imp->get_char();
1472 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1473 c = imp->get_char();
1474 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1475 char v = (vh << 4) | vl;
1480 error_at(imp->location(), "bad string constant");
1481 return Expression::make_error(imp->location());
1485 return Expression::make_string(val, imp->location());
1488 // Make a string expression.
1491 Expression::make_string(const std::string& val, source_location location)
1493 return new String_expression(val, location);
1496 // Make an integer expression.
1498 class Integer_expression : public Expression
1501 Integer_expression(const mpz_t* val, Type* type, source_location location)
1502 : Expression(EXPRESSION_INTEGER, location),
1504 { mpz_init_set(this->val_, *val); }
1509 // Return whether VAL fits in the type.
1511 check_constant(mpz_t val, Type*, source_location);
1513 // Write VAL to export data.
1515 export_integer(Export* exp, const mpz_t val);
1519 do_is_constant() const
1523 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1529 do_determine_type(const Type_context* context);
1532 do_check_types(Gogo*);
1535 do_get_tree(Translate_context*);
1539 { return Expression::make_integer(&this->val_, this->type_,
1540 this->location()); }
1543 do_export(Export*) const;
1546 // The integer value.
1552 // Return an integer constant value.
1555 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1558 if (this->type_ != NULL)
1559 *ptype = this->type_;
1560 mpz_set(val, this->val_);
1564 // Return the current type. If we haven't set the type yet, we return
1565 // an abstract integer type.
1568 Integer_expression::do_type()
1570 if (this->type_ == NULL)
1571 this->type_ = Type::make_abstract_integer_type();
1575 // Set the type of the integer value. Here we may switch from an
1576 // abstract type to a real type.
1579 Integer_expression::do_determine_type(const Type_context* context)
1581 if (this->type_ != NULL && !this->type_->is_abstract())
1583 else if (context->type != NULL
1584 && (context->type->integer_type() != NULL
1585 || context->type->float_type() != NULL
1586 || context->type->complex_type() != NULL))
1587 this->type_ = context->type;
1588 else if (!context->may_be_abstract)
1589 this->type_ = Type::lookup_integer_type("int");
1592 // Return true if the integer VAL fits in the range of the type TYPE.
1593 // Otherwise give an error and return false. TYPE may be NULL.
1596 Integer_expression::check_constant(mpz_t val, Type* type,
1597 source_location location)
1601 Integer_type* itype = type->integer_type();
1602 if (itype == NULL || itype->is_abstract())
1605 int bits = mpz_sizeinbase(val, 2);
1607 if (itype->is_unsigned())
1609 // For an unsigned type we can only accept a nonnegative number,
1610 // and we must be able to represent at least BITS.
1611 if (mpz_sgn(val) >= 0
1612 && bits <= itype->bits())
1617 // For a signed type we need an extra bit to indicate the sign.
1618 // We have to handle the most negative integer specially.
1619 if (bits + 1 <= itype->bits()
1620 || (bits <= itype->bits()
1622 && (mpz_scan1(val, 0)
1623 == static_cast<unsigned long>(itype->bits() - 1))
1624 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1628 error_at(location, "integer constant overflow");
1632 // Check the type of an integer constant.
1635 Integer_expression::do_check_types(Gogo*)
1637 if (this->type_ == NULL)
1639 if (!Integer_expression::check_constant(this->val_, this->type_,
1641 this->set_is_error();
1644 // Get a tree for an integer constant.
1647 Integer_expression::do_get_tree(Translate_context* context)
1649 Gogo* gogo = context->gogo();
1651 if (this->type_ != NULL && !this->type_->is_abstract())
1652 type = this->type_->get_tree(gogo);
1653 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1655 // We are converting to an abstract floating point type.
1656 type = Type::lookup_float_type("float64")->get_tree(gogo);
1658 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1660 // We are converting to an abstract complex type.
1661 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
1665 // If we still have an abstract type here, then this is being
1666 // used in a constant expression which didn't get reduced for
1667 // some reason. Use a type which will fit the value. We use <,
1668 // not <=, because we need an extra bit for the sign bit.
1669 int bits = mpz_sizeinbase(this->val_, 2);
1670 if (bits < INT_TYPE_SIZE)
1671 type = Type::lookup_integer_type("int")->get_tree(gogo);
1673 type = Type::lookup_integer_type("int64")->get_tree(gogo);
1675 type = long_long_integer_type_node;
1677 return Expression::integer_constant_tree(this->val_, type);
1680 // Write VAL to export data.
1683 Integer_expression::export_integer(Export* exp, const mpz_t val)
1685 char* s = mpz_get_str(NULL, 10, val);
1686 exp->write_c_string(s);
1690 // Export an integer in a constant expression.
1693 Integer_expression::do_export(Export* exp) const
1695 Integer_expression::export_integer(exp, this->val_);
1696 // A trailing space lets us reliably identify the end of the number.
1697 exp->write_c_string(" ");
1700 // Import an integer, floating point, or complex value. This handles
1701 // all these types because they all start with digits.
1704 Integer_expression::do_import(Import* imp)
1706 std::string num = imp->read_identifier();
1707 imp->require_c_string(" ");
1708 if (!num.empty() && num[num.length() - 1] == 'i')
1711 size_t plus_pos = num.find('+', 1);
1712 size_t minus_pos = num.find('-', 1);
1714 if (plus_pos == std::string::npos)
1716 else if (minus_pos == std::string::npos)
1720 error_at(imp->location(), "bad number in import data: %qs",
1722 return Expression::make_error(imp->location());
1724 if (pos == std::string::npos)
1725 mpfr_set_ui(real, 0, GMP_RNDN);
1728 std::string real_str = num.substr(0, pos);
1729 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1731 error_at(imp->location(), "bad number in import data: %qs",
1733 return Expression::make_error(imp->location());
1737 std::string imag_str;
1738 if (pos == std::string::npos)
1741 imag_str = num.substr(pos);
1742 imag_str = imag_str.substr(0, imag_str.size() - 1);
1744 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1746 error_at(imp->location(), "bad number in import data: %qs",
1748 return Expression::make_error(imp->location());
1750 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1756 else if (num.find('.') == std::string::npos
1757 && num.find('E') == std::string::npos)
1760 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1762 error_at(imp->location(), "bad number in import data: %qs",
1764 return Expression::make_error(imp->location());
1766 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1773 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1775 error_at(imp->location(), "bad number in import data: %qs",
1777 return Expression::make_error(imp->location());
1779 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1785 // Build a new integer value.
1788 Expression::make_integer(const mpz_t* val, Type* type,
1789 source_location location)
1791 return new Integer_expression(val, type, location);
1796 class Float_expression : public Expression
1799 Float_expression(const mpfr_t* val, Type* type, source_location location)
1800 : Expression(EXPRESSION_FLOAT, location),
1803 mpfr_init_set(this->val_, *val, GMP_RNDN);
1806 // Constrain VAL to fit into TYPE.
1808 constrain_float(mpfr_t val, Type* type);
1810 // Return whether VAL fits in the type.
1812 check_constant(mpfr_t val, Type*, source_location);
1814 // Write VAL to export data.
1816 export_float(Export* exp, const mpfr_t val);
1820 do_is_constant() const
1824 do_float_constant_value(mpfr_t val, Type**) const;
1830 do_determine_type(const Type_context*);
1833 do_check_types(Gogo*);
1837 { return Expression::make_float(&this->val_, this->type_,
1838 this->location()); }
1841 do_get_tree(Translate_context*);
1844 do_export(Export*) const;
1847 // The floating point value.
1853 // Constrain VAL to fit into TYPE.
1856 Float_expression::constrain_float(mpfr_t val, Type* type)
1858 Float_type* ftype = type->float_type();
1859 if (ftype != NULL && !ftype->is_abstract())
1861 tree type_tree = ftype->type_tree();
1862 REAL_VALUE_TYPE rvt;
1863 real_from_mpfr(&rvt, val, type_tree, GMP_RNDN);
1864 real_convert(&rvt, TYPE_MODE(type_tree), &rvt);
1865 mpfr_from_real(val, &rvt, GMP_RNDN);
1869 // Return a floating point constant value.
1872 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1874 if (this->type_ != NULL)
1875 *ptype = this->type_;
1876 mpfr_set(val, this->val_, GMP_RNDN);
1880 // Return the current type. If we haven't set the type yet, we return
1881 // an abstract float type.
1884 Float_expression::do_type()
1886 if (this->type_ == NULL)
1887 this->type_ = Type::make_abstract_float_type();
1891 // Set the type of the float value. Here we may switch from an
1892 // abstract type to a real type.
1895 Float_expression::do_determine_type(const Type_context* context)
1897 if (this->type_ != NULL && !this->type_->is_abstract())
1899 else if (context->type != NULL
1900 && (context->type->integer_type() != NULL
1901 || context->type->float_type() != NULL
1902 || context->type->complex_type() != NULL))
1903 this->type_ = context->type;
1904 else if (!context->may_be_abstract)
1905 this->type_ = Type::lookup_float_type("float");
1908 // Return true if the floating point value VAL fits in the range of
1909 // the type TYPE. Otherwise give an error and return false. TYPE may
1913 Float_expression::check_constant(mpfr_t val, Type* type,
1914 source_location location)
1918 Float_type* ftype = type->float_type();
1919 if (ftype == NULL || ftype->is_abstract())
1922 // A NaN or Infinity always fits in the range of the type.
1923 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1926 mp_exp_t exp = mpfr_get_exp(val);
1928 switch (ftype->bits())
1941 error_at(location, "floating point constant overflow");
1947 // Check the type of a float value.
1950 Float_expression::do_check_types(Gogo*)
1952 if (this->type_ == NULL)
1955 if (!Float_expression::check_constant(this->val_, this->type_,
1957 this->set_is_error();
1959 Integer_type* integer_type = this->type_->integer_type();
1960 if (integer_type != NULL)
1962 if (!mpfr_integer_p(this->val_))
1963 this->report_error(_("floating point constant truncated to integer"));
1966 gcc_assert(!integer_type->is_abstract());
1969 mpfr_get_z(ival, this->val_, GMP_RNDN);
1970 Integer_expression::check_constant(ival, integer_type,
1977 // Get a tree for a float constant.
1980 Float_expression::do_get_tree(Translate_context* context)
1982 Gogo* gogo = context->gogo();
1984 if (this->type_ != NULL && !this->type_->is_abstract())
1985 type = this->type_->get_tree(gogo);
1986 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
1988 // We have an abstract integer type. We just hope for the best.
1989 type = Type::lookup_integer_type("int")->get_tree(gogo);
1993 // If we still have an abstract type here, then this is being
1994 // used in a constant expression which didn't get reduced. We
1995 // just use float64 and hope for the best.
1996 type = Type::lookup_float_type("float64")->get_tree(gogo);
1998 return Expression::float_constant_tree(this->val_, type);
2001 // Write a floating point number to export data.
2004 Float_expression::export_float(Export *exp, const mpfr_t val)
2007 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2009 exp->write_c_string("-");
2010 exp->write_c_string("0.");
2011 exp->write_c_string(*s == '-' ? s + 1 : s);
2014 snprintf(buf, sizeof buf, "E%ld", exponent);
2015 exp->write_c_string(buf);
2018 // Export a floating point number in a constant expression.
2021 Float_expression::do_export(Export* exp) const
2023 Float_expression::export_float(exp, this->val_);
2024 // A trailing space lets us reliably identify the end of the number.
2025 exp->write_c_string(" ");
2028 // Make a float expression.
2031 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2033 return new Float_expression(val, type, location);
2038 class Complex_expression : public Expression
2041 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2042 source_location location)
2043 : Expression(EXPRESSION_COMPLEX, location),
2046 mpfr_init_set(this->real_, *real, GMP_RNDN);
2047 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2050 // Constrain REAL/IMAG to fit into TYPE.
2052 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2054 // Return whether REAL/IMAG fits in the type.
2056 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2058 // Write REAL/IMAG to export data.
2060 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2064 do_is_constant() const
2068 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2074 do_determine_type(const Type_context*);
2077 do_check_types(Gogo*);
2082 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2087 do_get_tree(Translate_context*);
2090 do_export(Export*) const;
2095 // The imaginary part;
2097 // The type if known.
2101 // Constrain REAL/IMAG to fit into TYPE.
2104 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2106 Complex_type* ctype = type->complex_type();
2107 if (ctype != NULL && !ctype->is_abstract())
2109 tree type_tree = ctype->type_tree();
2111 REAL_VALUE_TYPE rvt;
2112 real_from_mpfr(&rvt, real, TREE_TYPE(type_tree), GMP_RNDN);
2113 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2114 mpfr_from_real(real, &rvt, GMP_RNDN);
2116 real_from_mpfr(&rvt, imag, TREE_TYPE(type_tree), GMP_RNDN);
2117 real_convert(&rvt, TYPE_MODE(TREE_TYPE(type_tree)), &rvt);
2118 mpfr_from_real(imag, &rvt, GMP_RNDN);
2122 // Return a complex constant value.
2125 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2128 if (this->type_ != NULL)
2129 *ptype = this->type_;
2130 mpfr_set(real, this->real_, GMP_RNDN);
2131 mpfr_set(imag, this->imag_, GMP_RNDN);
2135 // Return the current type. If we haven't set the type yet, we return
2136 // an abstract complex type.
2139 Complex_expression::do_type()
2141 if (this->type_ == NULL)
2142 this->type_ = Type::make_abstract_complex_type();
2146 // Set the type of the complex value. Here we may switch from an
2147 // abstract type to a real type.
2150 Complex_expression::do_determine_type(const Type_context* context)
2152 if (this->type_ != NULL && !this->type_->is_abstract())
2154 else if (context->type != NULL
2155 && context->type->complex_type() != NULL)
2156 this->type_ = context->type;
2157 else if (!context->may_be_abstract)
2158 this->type_ = Type::lookup_complex_type("complex");
2161 // Return true if the complex value REAL/IMAG fits in the range of the
2162 // type TYPE. Otherwise give an error and return false. TYPE may be
2166 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2167 source_location location)
2171 Complex_type* ctype = type->complex_type();
2172 if (ctype == NULL || ctype->is_abstract())
2176 switch (ctype->bits())
2188 // A NaN or Infinity always fits in the range of the type.
2189 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2191 if (mpfr_get_exp(real) > max_exp)
2193 error_at(location, "complex real part constant overflow");
2198 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2200 if (mpfr_get_exp(imag) > max_exp)
2202 error_at(location, "complex imaginary part constant overflow");
2210 // Check the type of a complex value.
2213 Complex_expression::do_check_types(Gogo*)
2215 if (this->type_ == NULL)
2218 if (!Complex_expression::check_constant(this->real_, this->imag_,
2219 this->type_, this->location()))
2220 this->set_is_error();
2223 // Get a tree for a complex constant.
2226 Complex_expression::do_get_tree(Translate_context* context)
2228 Gogo* gogo = context->gogo();
2230 if (this->type_ != NULL && !this->type_->is_abstract())
2231 type = this->type_->get_tree(gogo);
2234 // If we still have an abstract type here, this this is being
2235 // used in a constant expression which didn't get reduced. We
2236 // just use complex128 and hope for the best.
2237 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
2239 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2242 // Write REAL/IMAG to export data.
2245 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2248 if (!mpfr_zero_p(real))
2250 Float_expression::export_float(exp, real);
2251 if (mpfr_sgn(imag) > 0)
2252 exp->write_c_string("+");
2254 Float_expression::export_float(exp, imag);
2255 exp->write_c_string("i");
2258 // Export a complex number in a constant expression.
2261 Complex_expression::do_export(Export* exp) const
2263 Complex_expression::export_complex(exp, this->real_, this->imag_);
2264 // A trailing space lets us reliably identify the end of the number.
2265 exp->write_c_string(" ");
2268 // Make a complex expression.
2271 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2272 source_location location)
2274 return new Complex_expression(real, imag, type, location);
2277 // A reference to a const in an expression.
2279 class Const_expression : public Expression
2282 Const_expression(Named_object* constant, source_location location)
2283 : Expression(EXPRESSION_CONST_REFERENCE, location),
2284 constant_(constant), type_(NULL)
2289 { return this->constant_->name(); }
2293 do_lower(Gogo*, Named_object*, int);
2296 do_is_constant() const
2300 do_integer_constant_value(bool, mpz_t val, Type**) const;
2303 do_float_constant_value(mpfr_t val, Type**) const;
2306 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2309 do_string_constant_value(std::string* val) const
2310 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2315 // The type of a const is set by the declaration, not the use.
2317 do_determine_type(const Type_context*);
2320 do_check_types(Gogo*);
2327 do_get_tree(Translate_context* context);
2329 // When exporting a reference to a const as part of a const
2330 // expression, we export the value. We ignore the fact that it has
2333 do_export(Export* exp) const
2334 { this->constant_->const_value()->expr()->export_expression(exp); }
2338 Named_object* constant_;
2339 // The type of this reference. This is used if the constant has an
2344 // Lower a constant expression. This is where we convert the
2345 // predeclared constant iota into an integer value.
2348 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2350 if (this->constant_->const_value()->expr()->classification()
2353 if (iota_value == -1)
2355 error_at(this->location(),
2356 "iota is only defined in const declarations");
2360 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2361 Expression* ret = Expression::make_integer(&val, NULL,
2367 // Make sure that the constant itself has been lowered.
2368 gogo->lower_constant(this->constant_);
2373 // Return an integer constant value.
2376 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2380 if (this->type_ != NULL)
2381 ctype = this->type_;
2383 ctype = this->constant_->const_value()->type();
2384 if (ctype != NULL && ctype->integer_type() == NULL)
2387 Expression* e = this->constant_->const_value()->expr();
2389 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2393 && !Integer_expression::check_constant(val, ctype, this->location()))
2396 *ptype = ctype != NULL ? ctype : t;
2400 // Return a floating point constant value.
2403 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2406 if (this->type_ != NULL)
2407 ctype = this->type_;
2409 ctype = this->constant_->const_value()->type();
2410 if (ctype != NULL && ctype->float_type() == NULL)
2414 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2416 if (r && ctype != NULL)
2418 if (!Float_expression::check_constant(val, ctype, this->location()))
2420 Float_expression::constrain_float(val, ctype);
2422 *ptype = ctype != NULL ? ctype : t;
2426 // Return a complex constant value.
2429 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2433 if (this->type_ != NULL)
2434 ctype = this->type_;
2436 ctype = this->constant_->const_value()->type();
2437 if (ctype != NULL && ctype->complex_type() == NULL)
2441 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2444 if (r && ctype != NULL)
2446 if (!Complex_expression::check_constant(real, imag, ctype,
2449 Complex_expression::constrain_complex(real, imag, ctype);
2451 *ptype = ctype != NULL ? ctype : t;
2455 // Return the type of the const reference.
2458 Const_expression::do_type()
2460 if (this->type_ != NULL)
2462 Named_constant* nc = this->constant_->const_value();
2463 Type* ret = nc->type();
2466 // During parsing, a named constant may have a NULL type, but we
2467 // must not return a NULL type here.
2468 return nc->expr()->type();
2471 // Set the type of the const reference.
2474 Const_expression::do_determine_type(const Type_context* context)
2476 Type* ctype = this->constant_->const_value()->type();
2477 Type* cetype = (ctype != NULL
2479 : this->constant_->const_value()->expr()->type());
2480 if (ctype != NULL && !ctype->is_abstract())
2482 else if (context->type != NULL
2483 && (context->type->integer_type() != NULL
2484 || context->type->float_type() != NULL
2485 || context->type->complex_type() != NULL)
2486 && (cetype->integer_type() != NULL
2487 || cetype->float_type() != NULL
2488 || cetype->complex_type() != NULL))
2489 this->type_ = context->type;
2490 else if (context->type != NULL
2491 && context->type->is_string_type()
2492 && cetype->is_string_type())
2493 this->type_ = context->type;
2494 else if (context->type != NULL
2495 && context->type->is_boolean_type()
2496 && cetype->is_boolean_type())
2497 this->type_ = context->type;
2498 else if (!context->may_be_abstract)
2500 if (cetype->is_abstract())
2501 cetype = cetype->make_non_abstract_type();
2502 this->type_ = cetype;
2506 // Check types of a const reference.
2509 Const_expression::do_check_types(Gogo*)
2511 if (this->type_ == NULL || this->type_->is_abstract())
2514 // Check for integer overflow.
2515 if (this->type_->integer_type() != NULL)
2520 if (!this->integer_constant_value(true, ival, &dummy))
2524 Expression* cexpr = this->constant_->const_value()->expr();
2525 if (cexpr->float_constant_value(fval, &dummy))
2527 if (!mpfr_integer_p(fval))
2528 this->report_error(_("floating point constant "
2529 "truncated to integer"));
2532 mpfr_get_z(ival, fval, GMP_RNDN);
2533 Integer_expression::check_constant(ival, this->type_,
2543 // Return a tree for the const reference.
2546 Const_expression::do_get_tree(Translate_context* context)
2548 Gogo* gogo = context->gogo();
2550 if (this->type_ == NULL)
2551 type_tree = NULL_TREE;
2554 type_tree = this->type_->get_tree(gogo);
2555 if (type_tree == error_mark_node)
2556 return error_mark_node;
2559 // If the type has been set for this expression, but the underlying
2560 // object is an abstract int or float, we try to get the abstract
2561 // value. Otherwise we may lose something in the conversion.
2562 if (this->type_ != NULL
2563 && this->constant_->const_value()->type()->is_abstract())
2565 Expression* expr = this->constant_->const_value()->expr();
2569 if (expr->integer_constant_value(true, ival, &t))
2571 tree ret = Expression::integer_constant_tree(ival, type_tree);
2579 if (expr->float_constant_value(fval, &t))
2581 tree ret = Expression::float_constant_tree(fval, type_tree);
2588 if (expr->complex_constant_value(fval, imag, &t))
2590 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2599 tree const_tree = this->constant_->get_tree(gogo, context->function());
2600 if (this->type_ == NULL
2601 || const_tree == error_mark_node
2602 || TREE_TYPE(const_tree) == error_mark_node)
2606 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2607 ret = fold_convert(type_tree, const_tree);
2608 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2609 ret = fold(convert_to_integer(type_tree, const_tree));
2610 else if (TREE_CODE(type_tree) == REAL_TYPE)
2611 ret = fold(convert_to_real(type_tree, const_tree));
2612 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2613 ret = fold(convert_to_complex(type_tree, const_tree));
2619 // Make a reference to a constant in an expression.
2622 Expression::make_const_reference(Named_object* constant,
2623 source_location location)
2625 return new Const_expression(constant, location);
2630 class Nil_expression : public Expression
2633 Nil_expression(source_location location)
2634 : Expression(EXPRESSION_NIL, location)
2642 do_is_constant() const
2647 { return Type::make_nil_type(); }
2650 do_determine_type(const Type_context*)
2658 do_get_tree(Translate_context*)
2659 { return null_pointer_node; }
2662 do_export(Export* exp) const
2663 { exp->write_c_string("nil"); }
2666 // Import a nil expression.
2669 Nil_expression::do_import(Import* imp)
2671 imp->require_c_string("nil");
2672 return Expression::make_nil(imp->location());
2675 // Make a nil expression.
2678 Expression::make_nil(source_location location)
2680 return new Nil_expression(location);
2683 // The value of the predeclared constant iota. This is little more
2684 // than a marker. This will be lowered to an integer in
2685 // Const_expression::do_lower, which is where we know the value that
2688 class Iota_expression : public Parser_expression
2691 Iota_expression(source_location location)
2692 : Parser_expression(EXPRESSION_IOTA, location)
2697 do_lower(Gogo*, Named_object*, int)
2698 { gcc_unreachable(); }
2700 // There should only ever be one of these.
2703 { gcc_unreachable(); }
2706 // Make an iota expression. This is only called for one case: the
2707 // value of the predeclared constant iota.
2710 Expression::make_iota()
2712 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2713 return &iota_expression;
2716 // A type conversion expression.
2718 class Type_conversion_expression : public Expression
2721 Type_conversion_expression(Type* type, Expression* expr,
2722 source_location location)
2723 : Expression(EXPRESSION_CONVERSION, location),
2724 type_(type), expr_(expr), may_convert_function_types_(false)
2727 // Return the type to which we are converting.
2730 { return this->type_; }
2732 // Return the expression which we are converting.
2735 { return this->expr_; }
2737 // Permit converting from one function type to another. This is
2738 // used internally for method expressions.
2740 set_may_convert_function_types()
2742 this->may_convert_function_types_ = true;
2745 // Import a type conversion expression.
2751 do_traverse(Traverse* traverse);
2754 do_lower(Gogo*, Named_object*, int);
2757 do_is_constant() const
2758 { return this->expr_->is_constant(); }
2761 do_integer_constant_value(bool, mpz_t, Type**) const;
2764 do_float_constant_value(mpfr_t, Type**) const;
2767 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2770 do_string_constant_value(std::string*) const;
2774 { return this->type_; }
2777 do_determine_type(const Type_context*)
2779 Type_context subcontext(this->type_, false);
2780 this->expr_->determine_type(&subcontext);
2784 do_check_types(Gogo*);
2789 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2794 do_get_tree(Translate_context* context);
2797 do_export(Export*) const;
2800 // The type to convert to.
2802 // The expression to convert.
2804 // True if this is permitted to convert function types. This is
2805 // used internally for method expressions.
2806 bool may_convert_function_types_;
2812 Type_conversion_expression::do_traverse(Traverse* traverse)
2814 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
2815 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
2816 return TRAVERSE_EXIT;
2817 return TRAVERSE_CONTINUE;
2820 // Convert to a constant at lowering time.
2823 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
2825 Type* type = this->type_;
2826 Expression* val = this->expr_;
2827 source_location location = this->location();
2829 if (type->integer_type() != NULL)
2834 if (val->integer_constant_value(false, ival, &dummy))
2836 if (!Integer_expression::check_constant(ival, type, location))
2837 mpz_set_ui(ival, 0);
2838 Expression* ret = Expression::make_integer(&ival, type, location);
2845 if (val->float_constant_value(fval, &dummy))
2847 if (!mpfr_integer_p(fval))
2850 "floating point constant truncated to integer");
2851 return Expression::make_error(location);
2853 mpfr_get_z(ival, fval, GMP_RNDN);
2854 if (!Integer_expression::check_constant(ival, type, location))
2855 mpz_set_ui(ival, 0);
2856 Expression* ret = Expression::make_integer(&ival, type, location);
2865 if (type->float_type() != NULL)
2870 if (val->float_constant_value(fval, &dummy))
2872 if (!Float_expression::check_constant(fval, type, location))
2873 mpfr_set_ui(fval, 0, GMP_RNDN);
2874 Float_expression::constrain_float(fval, type);
2875 Expression *ret = Expression::make_float(&fval, type, location);
2882 if (type->complex_type() != NULL)
2889 if (val->complex_constant_value(real, imag, &dummy))
2891 if (!Complex_expression::check_constant(real, imag, type, location))
2893 mpfr_set_ui(real, 0, GMP_RNDN);
2894 mpfr_set_ui(imag, 0, GMP_RNDN);
2896 Complex_expression::constrain_complex(real, imag, type);
2897 Expression* ret = Expression::make_complex(&real, &imag, type,
2907 if (type->is_open_array_type() && type->named_type() == NULL)
2909 Type* element_type = type->array_type()->element_type()->forwarded();
2910 bool is_byte = element_type == Type::lookup_integer_type("uint8");
2911 bool is_int = element_type == Type::lookup_integer_type("int");
2912 if (is_byte || is_int)
2915 if (val->string_constant_value(&s))
2917 Expression_list* vals = new Expression_list();
2920 for (std::string::const_iterator p = s.begin();
2925 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
2926 Expression* v = Expression::make_integer(&val,
2935 const char *p = s.data();
2936 const char *pend = s.data() + s.length();
2940 int adv = Lex::fetch_char(p, &c);
2943 warning_at(this->location(), 0,
2944 "invalid UTF-8 encoding");
2949 mpz_init_set_ui(val, c);
2950 Expression* v = Expression::make_integer(&val,
2958 return Expression::make_slice_composite_literal(type, vals,
2967 // Return the constant integer value if there is one.
2970 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
2974 if (this->type_->integer_type() == NULL)
2980 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
2982 if (!Integer_expression::check_constant(ival, this->type_,
2990 *ptype = this->type_;
2997 if (this->expr_->float_constant_value(fval, &dummy))
2999 mpfr_get_z(val, fval, GMP_RNDN);
3001 if (!Integer_expression::check_constant(val, this->type_,
3004 *ptype = this->type_;
3012 // Return the constant floating point value if there is one.
3015 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3018 if (this->type_->float_type() == NULL)
3024 if (this->expr_->float_constant_value(fval, &dummy))
3026 if (!Float_expression::check_constant(fval, this->type_,
3032 mpfr_set(val, fval, GMP_RNDN);
3034 Float_expression::constrain_float(val, this->type_);
3035 *ptype = this->type_;
3043 // Return the constant complex value if there is one.
3046 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3050 if (this->type_->complex_type() == NULL)
3058 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3060 if (!Complex_expression::check_constant(rval, ival, this->type_,
3067 mpfr_set(real, rval, GMP_RNDN);
3068 mpfr_set(imag, ival, GMP_RNDN);
3071 Complex_expression::constrain_complex(real, imag, this->type_);
3072 *ptype = this->type_;
3081 // Return the constant string value if there is one.
3084 Type_conversion_expression::do_string_constant_value(std::string* val) const
3086 if (this->type_->is_string_type()
3087 && this->expr_->type()->integer_type() != NULL)
3092 if (this->expr_->integer_constant_value(false, ival, &dummy))
3094 unsigned long ulval = mpz_get_ui(ival);
3095 if (mpz_cmp_ui(ival, ulval) == 0)
3097 Lex::append_char(ulval, true, val, this->location());
3105 // FIXME: Could handle conversion from const []int here.
3110 // Check that types are convertible.
3113 Type_conversion_expression::do_check_types(Gogo*)
3115 Type* type = this->type_;
3116 Type* expr_type = this->expr_->type();
3119 if (this->may_convert_function_types_
3120 && type->function_type() != NULL
3121 && expr_type->function_type() != NULL)
3124 if (Type::are_convertible(type, expr_type, &reason))
3127 error_at(this->location(), "%s", reason.c_str());
3128 this->set_is_error();
3131 // Get a tree for a type conversion.
3134 Type_conversion_expression::do_get_tree(Translate_context* context)
3136 Gogo* gogo = context->gogo();
3137 tree type_tree = this->type_->get_tree(gogo);
3138 tree expr_tree = this->expr_->get_tree(context);
3140 if (type_tree == error_mark_node
3141 || expr_tree == error_mark_node
3142 || TREE_TYPE(expr_tree) == error_mark_node)
3143 return error_mark_node;
3145 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3146 return fold_convert(type_tree, expr_tree);
3148 Type* type = this->type_;
3149 Type* expr_type = this->expr_->type();
3151 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3152 ret = Expression::convert_for_assignment(context, type, expr_type,
3153 expr_tree, this->location());
3154 else if (type->integer_type() != NULL)
3156 if (expr_type->integer_type() != NULL
3157 || expr_type->float_type() != NULL
3158 || expr_type->is_unsafe_pointer_type())
3159 ret = fold(convert_to_integer(type_tree, expr_tree));
3163 else if (type->float_type() != NULL)
3165 if (expr_type->integer_type() != NULL
3166 || expr_type->float_type() != NULL)
3167 ret = fold(convert_to_real(type_tree, expr_tree));
3171 else if (type->complex_type() != NULL)
3173 if (expr_type->complex_type() != NULL)
3174 ret = fold(convert_to_complex(type_tree, expr_tree));
3178 else if (type->is_string_type()
3179 && expr_type->integer_type() != NULL)
3181 expr_tree = fold_convert(integer_type_node, expr_tree);
3182 if (host_integerp(expr_tree, 0))
3184 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3186 Lex::append_char(intval, true, &s, this->location());
3187 Expression* se = Expression::make_string(s, this->location());
3188 return se->get_tree(context);
3191 static tree int_to_string_fndecl;
3192 ret = Gogo::call_builtin(&int_to_string_fndecl,
3194 "__go_int_to_string",
3198 fold_convert(integer_type_node, expr_tree));
3200 else if (type->is_string_type()
3201 && (expr_type->array_type() != NULL
3202 || (expr_type->points_to() != NULL
3203 && expr_type->points_to()->array_type() != NULL)))
3205 Type* t = expr_type;
3206 if (t->points_to() != NULL)
3209 expr_tree = build_fold_indirect_ref(expr_tree);
3211 if (!DECL_P(expr_tree))
3212 expr_tree = save_expr(expr_tree);
3213 Array_type* a = t->array_type();
3214 Type* e = a->element_type()->forwarded();
3215 gcc_assert(e->integer_type() != NULL);
3216 tree valptr = fold_convert(const_ptr_type_node,
3217 a->value_pointer_tree(gogo, expr_tree));
3218 tree len = a->length_tree(gogo, expr_tree);
3219 len = fold_convert_loc(this->location(), size_type_node, len);
3220 if (e->integer_type()->is_unsigned()
3221 && e->integer_type()->bits() == 8)
3223 static tree byte_array_to_string_fndecl;
3224 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3226 "__go_byte_array_to_string",
3229 const_ptr_type_node,
3236 gcc_assert(e == Type::lookup_integer_type("int"));
3237 static tree int_array_to_string_fndecl;
3238 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3240 "__go_int_array_to_string",
3243 const_ptr_type_node,
3249 else if (type->is_open_array_type() && expr_type->is_string_type())
3251 Type* e = type->array_type()->element_type()->forwarded();
3252 gcc_assert(e->integer_type() != NULL);
3253 if (e->integer_type()->is_unsigned()
3254 && e->integer_type()->bits() == 8)
3256 static tree string_to_byte_array_fndecl;
3257 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3259 "__go_string_to_byte_array",
3262 TREE_TYPE(expr_tree),
3267 gcc_assert(e == Type::lookup_integer_type("int"));
3268 static tree string_to_int_array_fndecl;
3269 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3271 "__go_string_to_int_array",
3274 TREE_TYPE(expr_tree),
3278 else if ((type->is_unsafe_pointer_type()
3279 && expr_type->points_to() != NULL)
3280 || (expr_type->is_unsafe_pointer_type()
3281 && type->points_to() != NULL))
3282 ret = fold_convert(type_tree, expr_tree);
3283 else if (type->is_unsafe_pointer_type()
3284 && expr_type->integer_type() != NULL)
3285 ret = convert_to_pointer(type_tree, expr_tree);
3286 else if (this->may_convert_function_types_
3287 && type->function_type() != NULL
3288 && expr_type->function_type() != NULL)
3289 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3291 ret = Expression::convert_for_assignment(context, type, expr_type,
3292 expr_tree, this->location());
3297 // Output a type conversion in a constant expression.
3300 Type_conversion_expression::do_export(Export* exp) const
3302 exp->write_c_string("convert(");
3303 exp->write_type(this->type_);
3304 exp->write_c_string(", ");
3305 this->expr_->export_expression(exp);
3306 exp->write_c_string(")");
3309 // Import a type conversion or a struct construction.
3312 Type_conversion_expression::do_import(Import* imp)
3314 imp->require_c_string("convert(");
3315 Type* type = imp->read_type();
3316 imp->require_c_string(", ");
3317 Expression* val = Expression::import_expression(imp);
3318 imp->require_c_string(")");
3319 return Expression::make_cast(type, val, imp->location());
3322 // Make a type cast expression.
3325 Expression::make_cast(Type* type, Expression* val, source_location location)
3327 if (type->is_error_type() || val->is_error_expression())
3328 return Expression::make_error(location);
3329 return new Type_conversion_expression(type, val, location);
3332 // Unary expressions.
3334 class Unary_expression : public Expression
3337 Unary_expression(Operator op, Expression* expr, source_location location)
3338 : Expression(EXPRESSION_UNARY, location),
3339 op_(op), escapes_(true), expr_(expr)
3342 // Return the operator.
3345 { return this->op_; }
3347 // Return the operand.
3350 { return this->expr_; }
3352 // Record that an address expression does not escape.
3354 set_does_not_escape()
3356 gcc_assert(this->op_ == OPERATOR_AND);
3357 this->escapes_ = false;
3360 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3361 // could be done, false if not.
3363 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3366 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3367 // could be done, false if not.
3369 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3371 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3372 // true if this could be done, false if not.
3374 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3382 do_traverse(Traverse* traverse)
3383 { return Expression::traverse(&this->expr_, traverse); }
3386 do_lower(Gogo*, Named_object*, int);
3389 do_is_constant() const;
3392 do_integer_constant_value(bool, mpz_t, Type**) const;
3395 do_float_constant_value(mpfr_t, Type**) const;
3398 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3404 do_determine_type(const Type_context*);
3407 do_check_types(Gogo*);
3412 return Expression::make_unary(this->op_, this->expr_->copy(),
3417 do_is_addressable() const
3418 { return this->op_ == OPERATOR_MULT; }
3421 do_get_tree(Translate_context*);
3424 do_export(Export*) const;
3427 // The unary operator to apply.
3429 // Normally true. False if this is an address expression which does
3430 // not escape the current function.
3436 // If we are taking the address of a composite literal, and the
3437 // contents are not constant, then we want to make a heap composite
3441 Unary_expression::do_lower(Gogo*, Named_object*, int)
3443 source_location loc = this->location();
3444 Operator op = this->op_;
3445 Expression* expr = this->expr_;
3447 if (op == OPERATOR_MULT && expr->is_type_expression())
3448 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3450 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3451 // moving x to the heap. FIXME: Is it worth doing a real escape
3452 // analysis here? This case is found in math/unsafe.go and is
3453 // therefore worth special casing.
3454 if (op == OPERATOR_MULT)
3456 Expression* e = expr;
3457 while (e->classification() == EXPRESSION_CONVERSION)
3459 Type_conversion_expression* te
3460 = static_cast<Type_conversion_expression*>(e);
3464 if (e->classification() == EXPRESSION_UNARY)
3466 Unary_expression* ue = static_cast<Unary_expression*>(e);
3467 if (ue->op_ == OPERATOR_AND)
3474 ue->set_does_not_escape();
3479 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3480 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3482 Expression* ret = NULL;
3487 if (expr->integer_constant_value(false, eval, &etype))
3491 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3492 ret = Expression::make_integer(&val, etype, loc);
3499 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
3504 if (expr->float_constant_value(fval, &ftype))
3508 if (Unary_expression::eval_float(op, fval, val))
3509 ret = Expression::make_float(&val, ftype, loc);
3520 if (expr->complex_constant_value(fval, ival, &ftype))
3526 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
3527 ret = Expression::make_complex(&real, &imag, ftype, loc);
3541 // Return whether a unary expression is a constant.
3544 Unary_expression::do_is_constant() const
3546 if (this->op_ == OPERATOR_MULT)
3548 // Indirecting through a pointer is only constant if the object
3549 // to which the expression points is constant, but we currently
3550 // have no way to determine that.
3553 else if (this->op_ == OPERATOR_AND)
3555 // Taking the address of a variable is constant if it is a
3556 // global variable, not constant otherwise. In other cases
3557 // taking the address is probably not a constant.
3558 Var_expression* ve = this->expr_->var_expression();
3561 Named_object* no = ve->named_object();
3562 return no->is_variable() && no->var_value()->is_global();
3567 return this->expr_->is_constant();
3570 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3571 // UVAL, if known; it may be NULL. Return true if this could be done,
3575 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3576 source_location location)
3583 case OPERATOR_MINUS:
3585 return Integer_expression::check_constant(val, utype, location);
3587 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3591 || utype->integer_type() == NULL
3592 || utype->integer_type()->is_abstract())
3596 // The number of HOST_WIDE_INTs that it takes to represent
3598 size_t count = ((mpz_sizeinbase(uval, 2)
3599 + HOST_BITS_PER_WIDE_INT
3601 / HOST_BITS_PER_WIDE_INT);
3603 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3604 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3607 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3608 gcc_assert(ecount <= count);
3610 // Trim down to the number of words required by the type.
3611 size_t obits = utype->integer_type()->bits();
3612 if (!utype->integer_type()->is_unsigned())
3614 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3615 / HOST_BITS_PER_WIDE_INT);
3616 gcc_assert(ocount <= ocount);
3618 for (size_t i = 0; i < ocount; ++i)
3621 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3623 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3626 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3630 return Integer_expression::check_constant(val, utype, location);
3639 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3640 // could be done, false if not.
3643 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
3648 mpfr_set(val, uval, GMP_RNDN);
3650 case OPERATOR_MINUS:
3651 mpfr_neg(val, uval, GMP_RNDN);
3663 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3664 // if this could be done, false if not.
3667 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
3668 mpfr_t real, mpfr_t imag)
3673 mpfr_set(real, rval, GMP_RNDN);
3674 mpfr_set(imag, ival, GMP_RNDN);
3676 case OPERATOR_MINUS:
3677 mpfr_neg(real, rval, GMP_RNDN);
3678 mpfr_neg(imag, ival, GMP_RNDN);
3690 // Return the integral constant value of a unary expression, if it has one.
3693 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
3699 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
3702 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
3708 // Return the floating point constant value of a unary expression, if
3712 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
3717 if (!this->expr_->float_constant_value(uval, ptype))
3720 ret = Unary_expression::eval_float(this->op_, uval, val);
3725 // Return the complex constant value of a unary expression, if it has
3729 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
3737 if (!this->expr_->complex_constant_value(rval, ival, ptype))
3740 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
3746 // Return the type of a unary expression.
3749 Unary_expression::do_type()
3754 case OPERATOR_MINUS:
3757 return this->expr_->type();
3760 return Type::make_pointer_type(this->expr_->type());
3764 Type* subtype = this->expr_->type();
3765 Type* points_to = subtype->points_to();
3766 if (points_to == NULL)
3767 return Type::make_error_type();
3776 // Determine abstract types for a unary expression.
3779 Unary_expression::do_determine_type(const Type_context* context)
3784 case OPERATOR_MINUS:
3787 this->expr_->determine_type(context);
3791 // Taking the address of something.
3793 Type* subtype = (context->type == NULL
3795 : context->type->points_to());
3796 Type_context subcontext(subtype, false);
3797 this->expr_->determine_type(&subcontext);
3802 // Indirecting through a pointer.
3804 Type* subtype = (context->type == NULL
3806 : Type::make_pointer_type(context->type));
3807 Type_context subcontext(subtype, false);
3808 this->expr_->determine_type(&subcontext);
3817 // Check types for a unary expression.
3820 Unary_expression::do_check_types(Gogo*)
3825 case OPERATOR_MINUS:
3827 Type* type = this->expr_->type();
3828 if (type->integer_type() == NULL
3829 && type->float_type() == NULL
3830 && type->complex_type() == NULL
3831 && !type->is_error_type())
3832 this->report_error(_("expected numeric type"));
3839 Type* type = this->expr_->type();
3840 if (type->integer_type() == NULL
3841 && !type->is_boolean_type()
3842 && !type->is_error_type())
3843 this->report_error(_("expected integer or boolean type"));
3848 if (!this->expr_->is_addressable())
3849 this->report_error(_("invalid operand for unary %<&%>"));
3851 this->expr_->address_taken(this->escapes_);
3855 // Indirecting through a pointer.
3857 Type* type = this->expr_->type();
3858 if (type->points_to() == NULL
3859 && !type->is_error_type())
3860 this->report_error(_("expected pointer"));
3869 // Get a tree for a unary expression.
3872 Unary_expression::do_get_tree(Translate_context* context)
3874 tree expr = this->expr_->get_tree(context);
3875 if (expr == error_mark_node)
3876 return error_mark_node;
3878 source_location loc = this->location();
3884 case OPERATOR_MINUS:
3886 tree type = TREE_TYPE(expr);
3887 tree compute_type = excess_precision_type(type);
3888 if (compute_type != NULL_TREE)
3889 expr = ::convert(compute_type, expr);
3890 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
3891 (compute_type != NULL_TREE
3895 if (compute_type != NULL_TREE)
3896 ret = ::convert(type, ret);
3901 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
3902 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
3904 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
3905 build_int_cst(TREE_TYPE(expr), 0));
3908 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
3911 // We should not see a non-constant constructor here; cases
3912 // where we would see one should have been moved onto the heap
3913 // at parse time. Taking the address of a nonconstant
3914 // constructor will not do what the programmer expects.
3915 gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
3916 gcc_assert(TREE_CODE(expr) != ADDR_EXPR);
3918 // Build a decl for a constant constructor.
3919 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
3921 tree decl = build_decl(this->location(), VAR_DECL,
3922 create_tmp_var_name("C"), TREE_TYPE(expr));
3923 DECL_EXTERNAL(decl) = 0;
3924 TREE_PUBLIC(decl) = 0;
3925 TREE_READONLY(decl) = 1;
3926 TREE_CONSTANT(decl) = 1;
3927 TREE_STATIC(decl) = 1;
3928 TREE_ADDRESSABLE(decl) = 1;
3929 DECL_ARTIFICIAL(decl) = 1;
3930 DECL_INITIAL(decl) = expr;
3931 rest_of_decl_compilation(decl, 1, 0);
3935 return build_fold_addr_expr_loc(loc, expr);
3939 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
3941 // If we are dereferencing the pointer to a large struct, we
3942 // need to check for nil. We don't bother to check for small
3943 // structs because we expect the system to crash on a nil
3944 // pointer dereference.
3945 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
3946 if (s == -1 || s >= 4096)
3949 expr = save_expr(expr);
3950 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
3952 fold_convert(TREE_TYPE(expr),
3953 null_pointer_node));
3954 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
3956 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
3957 build3(COND_EXPR, void_type_node,
3958 compare, crash, NULL_TREE),
3962 // If the type of EXPR is a recursive pointer type, then we
3963 // need to insert a cast before indirecting.
3964 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
3966 Type* pt = this->expr_->type()->points_to();
3967 tree ind = pt->get_tree(context->gogo());
3968 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
3971 return build_fold_indirect_ref_loc(loc, expr);
3979 // Export a unary expression.
3982 Unary_expression::do_export(Export* exp) const
3987 exp->write_c_string("+ ");
3989 case OPERATOR_MINUS:
3990 exp->write_c_string("- ");
3993 exp->write_c_string("! ");
3996 exp->write_c_string("^ ");
4003 this->expr_->export_expression(exp);
4006 // Import a unary expression.
4009 Unary_expression::do_import(Import* imp)
4012 switch (imp->get_char())
4018 op = OPERATOR_MINUS;
4029 imp->require_c_string(" ");
4030 Expression* expr = Expression::import_expression(imp);
4031 return Expression::make_unary(op, expr, imp->location());
4034 // Make a unary expression.
4037 Expression::make_unary(Operator op, Expression* expr, source_location location)
4039 return new Unary_expression(op, expr, location);
4042 // If this is an indirection through a pointer, return the expression
4043 // being pointed through. Otherwise return this.
4048 if (this->classification_ == EXPRESSION_UNARY)
4050 Unary_expression* ue = static_cast<Unary_expression*>(this);
4051 if (ue->op() == OPERATOR_MULT)
4052 return ue->operand();
4057 // Class Binary_expression.
4062 Binary_expression::do_traverse(Traverse* traverse)
4064 int t = Expression::traverse(&this->left_, traverse);
4065 if (t == TRAVERSE_EXIT)
4066 return TRAVERSE_EXIT;
4067 return Expression::traverse(&this->right_, traverse);
4070 // Compare integer constants according to OP.
4073 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4076 int i = mpz_cmp(left_val, right_val);
4081 case OPERATOR_NOTEQ:
4096 // Compare floating point constants according to OP.
4099 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4104 i = mpfr_cmp(left_val, right_val);
4108 mpfr_init_set(lv, left_val, GMP_RNDN);
4110 mpfr_init_set(rv, right_val, GMP_RNDN);
4111 Float_expression::constrain_float(lv, type);
4112 Float_expression::constrain_float(rv, type);
4113 i = mpfr_cmp(lv, rv);
4121 case OPERATOR_NOTEQ:
4136 // Compare complex constants according to OP. Complex numbers may
4137 // only be compared for equality.
4140 Binary_expression::compare_complex(Operator op, Type* type,
4141 mpfr_t left_real, mpfr_t left_imag,
4142 mpfr_t right_real, mpfr_t right_imag)
4146 is_equal = (mpfr_cmp(left_real, right_real) == 0
4147 && mpfr_cmp(left_imag, right_imag) == 0);
4152 mpfr_init_set(lr, left_real, GMP_RNDN);
4153 mpfr_init_set(li, left_imag, GMP_RNDN);
4156 mpfr_init_set(rr, right_real, GMP_RNDN);
4157 mpfr_init_set(ri, right_imag, GMP_RNDN);
4158 Complex_expression::constrain_complex(lr, li, type);
4159 Complex_expression::constrain_complex(rr, ri, type);
4160 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4170 case OPERATOR_NOTEQ:
4177 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4178 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4179 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4180 // this could be done, false if not.
4183 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4184 Type* right_type, mpz_t right_val,
4185 source_location location, mpz_t val)
4187 bool is_shift_op = false;
4191 case OPERATOR_ANDAND:
4193 case OPERATOR_NOTEQ:
4198 // These return boolean values. We should probably handle them
4199 // anyhow in case a type conversion is used on the result.
4202 mpz_add(val, left_val, right_val);
4204 case OPERATOR_MINUS:
4205 mpz_sub(val, left_val, right_val);
4208 mpz_ior(val, left_val, right_val);
4211 mpz_xor(val, left_val, right_val);
4214 mpz_mul(val, left_val, right_val);
4217 if (mpz_sgn(right_val) != 0)
4218 mpz_tdiv_q(val, left_val, right_val);
4221 error_at(location, "division by zero");
4227 if (mpz_sgn(right_val) != 0)
4228 mpz_tdiv_r(val, left_val, right_val);
4231 error_at(location, "division by zero");
4236 case OPERATOR_LSHIFT:
4238 unsigned long shift = mpz_get_ui(right_val);
4239 if (mpz_cmp_ui(right_val, shift) != 0)
4241 error_at(location, "shift count overflow");
4245 mpz_mul_2exp(val, left_val, shift);
4250 case OPERATOR_RSHIFT:
4252 unsigned long shift = mpz_get_ui(right_val);
4253 if (mpz_cmp_ui(right_val, shift) != 0)
4255 error_at(location, "shift count overflow");
4259 if (mpz_cmp_ui(left_val, 0) >= 0)
4260 mpz_tdiv_q_2exp(val, left_val, shift);
4262 mpz_fdiv_q_2exp(val, left_val, shift);
4268 mpz_and(val, left_val, right_val);
4270 case OPERATOR_BITCLEAR:
4274 mpz_com(tval, right_val);
4275 mpz_and(val, left_val, tval);
4283 Type* type = left_type;
4288 else if (type != right_type && right_type != NULL)
4290 if (type->is_abstract())
4292 else if (!right_type->is_abstract())
4294 // This look like a type error which should be diagnosed
4295 // elsewhere. Don't do anything here, to avoid an
4296 // unhelpful chain of error messages.
4302 if (type != NULL && !type->is_abstract())
4304 // We have to check the operands too, as we have implicitly
4305 // coerced them to TYPE.
4306 if ((type != left_type
4307 && !Integer_expression::check_constant(left_val, type, location))
4309 && type != right_type
4310 && !Integer_expression::check_constant(right_val, type,
4312 || !Integer_expression::check_constant(val, type, location))
4319 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4320 // Return true if this could be done, false if not.
4323 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4324 Type* right_type, mpfr_t right_val,
4325 mpfr_t val, source_location location)
4330 case OPERATOR_ANDAND:
4332 case OPERATOR_NOTEQ:
4337 // These return boolean values. We should probably handle them
4338 // anyhow in case a type conversion is used on the result.
4341 mpfr_add(val, left_val, right_val, GMP_RNDN);
4343 case OPERATOR_MINUS:
4344 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4349 case OPERATOR_BITCLEAR:
4352 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4355 if (mpfr_zero_p(right_val))
4356 error_at(location, "division by zero");
4357 mpfr_div(val, left_val, right_val, GMP_RNDN);
4361 case OPERATOR_LSHIFT:
4362 case OPERATOR_RSHIFT:
4368 Type* type = left_type;
4371 else if (type != right_type && right_type != NULL)
4373 if (type->is_abstract())
4375 else if (!right_type->is_abstract())
4377 // This looks like a type error which should be diagnosed
4378 // elsewhere. Don't do anything here, to avoid an unhelpful
4379 // chain of error messages.
4384 if (type != NULL && !type->is_abstract())
4386 if ((type != left_type
4387 && !Float_expression::check_constant(left_val, type, location))
4388 || (type != right_type
4389 && !Float_expression::check_constant(right_val, type,
4391 || !Float_expression::check_constant(val, type, location))
4392 mpfr_set_ui(val, 0, GMP_RNDN);
4398 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4399 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4400 // could be done, false if not.
4403 Binary_expression::eval_complex(Operator op, Type* left_type,
4404 mpfr_t left_real, mpfr_t left_imag,
4406 mpfr_t right_real, mpfr_t right_imag,
4407 mpfr_t real, mpfr_t imag,
4408 source_location location)
4413 case OPERATOR_ANDAND:
4415 case OPERATOR_NOTEQ:
4420 // These return boolean values and must be handled differently.
4423 mpfr_add(real, left_real, right_real, GMP_RNDN);
4424 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4426 case OPERATOR_MINUS:
4427 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4428 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4433 case OPERATOR_BITCLEAR:
4437 // You might think that multiplying two complex numbers would
4438 // be simple, and you would be right, until you start to think
4439 // about getting the right answer for infinity. If one
4440 // operand here is infinity and the other is anything other
4441 // than zero or NaN, then we are going to wind up subtracting
4442 // two infinity values. That will give us a NaN, but the
4443 // correct answer is infinity.
4447 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4451 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4455 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4459 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4461 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4462 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4464 // If we get NaN on both sides, check whether it should really
4465 // be infinity. The rule is that if either side of the
4466 // complex number is infinity, then the whole value is
4467 // infinity, even if the other side is NaN. So the only case
4468 // we have to fix is the one in which both sides are NaN.
4469 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4470 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4471 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4473 bool is_infinity = false;
4477 mpfr_init_set(lr, left_real, GMP_RNDN);
4478 mpfr_init_set(li, left_imag, GMP_RNDN);
4482 mpfr_init_set(rr, right_real, GMP_RNDN);
4483 mpfr_init_set(ri, right_imag, GMP_RNDN);
4485 // If the left side is infinity, then the result is
4487 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4489 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4490 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4491 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4492 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4495 mpfr_set_ui(rr, 0, GMP_RNDN);
4496 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4500 mpfr_set_ui(ri, 0, GMP_RNDN);
4501 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4506 // If the right side is infinity, then the result is
4508 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4510 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4511 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4512 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4513 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4516 mpfr_set_ui(lr, 0, GMP_RNDN);
4517 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4521 mpfr_set_ui(li, 0, GMP_RNDN);
4522 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4527 // If we got an overflow in the intermediate computations,
4528 // then the result is infinity.
4530 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4531 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4535 mpfr_set_ui(lr, 0, GMP_RNDN);
4536 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4540 mpfr_set_ui(li, 0, GMP_RNDN);
4541 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4545 mpfr_set_ui(rr, 0, GMP_RNDN);
4546 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4550 mpfr_set_ui(ri, 0, GMP_RNDN);
4551 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4558 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4559 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4560 mpfr_mul(lirr, li, rr, GMP_RNDN);
4561 mpfr_mul(liri, li, ri, GMP_RNDN);
4562 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4563 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4564 mpfr_set_inf(real, mpfr_sgn(real));
4565 mpfr_set_inf(imag, mpfr_sgn(imag));
4582 // For complex division we want to avoid having an
4583 // intermediate overflow turn the whole result in a NaN. We
4584 // scale the values to try to avoid this.
4586 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4587 error_at(location, "division by zero");
4593 mpfr_abs(rra, right_real, GMP_RNDN);
4594 mpfr_abs(ria, right_imag, GMP_RNDN);
4597 mpfr_max(t, rra, ria, GMP_RNDN);
4601 mpfr_init_set(rr, right_real, GMP_RNDN);
4602 mpfr_init_set(ri, right_imag, GMP_RNDN);
4604 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4606 ilogbw = mpfr_get_exp(t);
4607 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4608 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4613 mpfr_mul(denom, rr, rr, GMP_RNDN);
4614 mpfr_mul(t, ri, ri, GMP_RNDN);
4615 mpfr_add(denom, denom, t, GMP_RNDN);
4617 mpfr_mul(real, left_real, rr, GMP_RNDN);
4618 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4619 mpfr_add(real, real, t, GMP_RNDN);
4620 mpfr_div(real, real, denom, GMP_RNDN);
4621 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4623 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4624 mpfr_mul(t, left_real, ri, GMP_RNDN);
4625 mpfr_sub(imag, imag, t, GMP_RNDN);
4626 mpfr_div(imag, imag, denom, GMP_RNDN);
4627 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4629 // If we wind up with NaN on both sides, check whether we
4630 // should really have infinity. The rule is that if either
4631 // side of the complex number is infinity, then the whole
4632 // value is infinity, even if the other side is NaN. So the
4633 // only case we have to fix is the one in which both sides are
4635 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4636 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4637 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4639 if (mpfr_zero_p(denom))
4641 mpfr_set_inf(real, mpfr_sgn(rr));
4642 mpfr_mul(real, real, left_real, GMP_RNDN);
4643 mpfr_set_inf(imag, mpfr_sgn(rr));
4644 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4646 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4647 && mpfr_number_p(rr) && mpfr_number_p(ri))
4649 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4650 mpfr_copysign(t, t, left_real, GMP_RNDN);
4653 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4654 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4658 mpfr_mul(t3, t, rr, GMP_RNDN);
4662 mpfr_mul(t4, t2, ri, GMP_RNDN);
4664 mpfr_add(t3, t3, t4, GMP_RNDN);
4665 mpfr_set_inf(real, mpfr_sgn(t3));
4667 mpfr_mul(t3, t2, rr, GMP_RNDN);
4668 mpfr_mul(t4, t, ri, GMP_RNDN);
4669 mpfr_sub(t3, t3, t4, GMP_RNDN);
4670 mpfr_set_inf(imag, mpfr_sgn(t3));
4676 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
4677 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
4679 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4680 mpfr_copysign(t, t, rr, GMP_RNDN);
4683 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4684 mpfr_copysign(t2, t2, ri, GMP_RNDN);
4688 mpfr_mul(t3, left_real, t, GMP_RNDN);
4692 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
4694 mpfr_add(t3, t3, t4, GMP_RNDN);
4695 mpfr_set_ui(real, 0, GMP_RNDN);
4696 mpfr_mul(real, real, t3, GMP_RNDN);
4698 mpfr_mul(t3, left_imag, t, GMP_RNDN);
4699 mpfr_mul(t4, left_real, t2, GMP_RNDN);
4700 mpfr_sub(t3, t3, t4, GMP_RNDN);
4701 mpfr_set_ui(imag, 0, GMP_RNDN);
4702 mpfr_mul(imag, imag, t3, GMP_RNDN);
4720 case OPERATOR_LSHIFT:
4721 case OPERATOR_RSHIFT:
4727 Type* type = left_type;
4730 else if (type != right_type && right_type != NULL)
4732 if (type->is_abstract())
4734 else if (!right_type->is_abstract())
4736 // This looks like a type error which should be diagnosed
4737 // elsewhere. Don't do anything here, to avoid an unhelpful
4738 // chain of error messages.
4743 if (type != NULL && !type->is_abstract())
4745 if ((type != left_type
4746 && !Complex_expression::check_constant(left_real, left_imag,
4748 || (type != right_type
4749 && !Complex_expression::check_constant(right_real, right_imag,
4751 || !Complex_expression::check_constant(real, imag, type,
4754 mpfr_set_ui(real, 0, GMP_RNDN);
4755 mpfr_set_ui(imag, 0, GMP_RNDN);
4762 // Lower a binary expression. We have to evaluate constant
4763 // expressions now, in order to implement Go's unlimited precision
4767 Binary_expression::do_lower(Gogo*, Named_object*, int)
4769 source_location location = this->location();
4770 Operator op = this->op_;
4771 Expression* left = this->left_;
4772 Expression* right = this->right_;
4774 const bool is_comparison = (op == OPERATOR_EQEQ
4775 || op == OPERATOR_NOTEQ
4776 || op == OPERATOR_LT
4777 || op == OPERATOR_LE
4778 || op == OPERATOR_GT
4779 || op == OPERATOR_GE);
4781 // Integer constant expressions.
4787 mpz_init(right_val);
4789 if (left->integer_constant_value(false, left_val, &left_type)
4790 && right->integer_constant_value(false, right_val, &right_type))
4792 Expression* ret = NULL;
4793 if (left_type != right_type
4794 && left_type != NULL
4795 && right_type != NULL
4796 && left_type->base() != right_type->base()
4797 && op != OPERATOR_LSHIFT
4798 && op != OPERATOR_RSHIFT)
4800 // May be a type error--let it be diagnosed later.
4802 else if (is_comparison)
4804 bool b = Binary_expression::compare_integer(op, left_val,
4806 ret = Expression::make_cast(Type::lookup_bool_type(),
4807 Expression::make_boolean(b, location),
4815 if (Binary_expression::eval_integer(op, left_type, left_val,
4816 right_type, right_val,
4819 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
4821 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
4823 else if (left_type == NULL)
4825 else if (right_type == NULL)
4827 else if (!left_type->is_abstract()
4828 && left_type->named_type() != NULL)
4830 else if (!right_type->is_abstract()
4831 && right_type->named_type() != NULL)
4833 else if (!left_type->is_abstract())
4835 else if (!right_type->is_abstract())
4837 else if (left_type->float_type() != NULL)
4839 else if (right_type->float_type() != NULL)
4841 else if (left_type->complex_type() != NULL)
4843 else if (right_type->complex_type() != NULL)
4847 ret = Expression::make_integer(&val, type, location);
4855 mpz_clear(right_val);
4856 mpz_clear(left_val);
4860 mpz_clear(right_val);
4861 mpz_clear(left_val);
4864 // Floating point constant expressions.
4867 mpfr_init(left_val);
4870 mpfr_init(right_val);
4872 if (left->float_constant_value(left_val, &left_type)
4873 && right->float_constant_value(right_val, &right_type))
4875 Expression* ret = NULL;
4876 if (left_type != right_type
4877 && left_type != NULL
4878 && right_type != NULL
4879 && left_type->base() != right_type->base()
4880 && op != OPERATOR_LSHIFT
4881 && op != OPERATOR_RSHIFT)
4883 // May be a type error--let it be diagnosed later.
4885 else if (is_comparison)
4887 bool b = Binary_expression::compare_float(op,
4891 left_val, right_val);
4892 ret = Expression::make_boolean(b, location);
4899 if (Binary_expression::eval_float(op, left_type, left_val,
4900 right_type, right_val, val,
4903 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
4904 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
4906 if (left_type == NULL)
4908 else if (right_type == NULL)
4910 else if (!left_type->is_abstract()
4911 && left_type->named_type() != NULL)
4913 else if (!right_type->is_abstract()
4914 && right_type->named_type() != NULL)
4916 else if (!left_type->is_abstract())
4918 else if (!right_type->is_abstract())
4920 else if (left_type->float_type() != NULL)
4922 else if (right_type->float_type() != NULL)
4926 ret = Expression::make_float(&val, type, location);
4934 mpfr_clear(right_val);
4935 mpfr_clear(left_val);
4939 mpfr_clear(right_val);
4940 mpfr_clear(left_val);
4943 // Complex constant expressions.
4947 mpfr_init(left_real);
4948 mpfr_init(left_imag);
4953 mpfr_init(right_real);
4954 mpfr_init(right_imag);
4957 if (left->complex_constant_value(left_real, left_imag, &left_type)
4958 && right->complex_constant_value(right_real, right_imag, &right_type))
4960 Expression* ret = NULL;
4961 if (left_type != right_type
4962 && left_type != NULL
4963 && right_type != NULL
4964 && left_type->base() != right_type->base())
4966 // May be a type error--let it be diagnosed later.
4968 else if (is_comparison)
4970 bool b = Binary_expression::compare_complex(op,
4978 ret = Expression::make_boolean(b, location);
4987 if (Binary_expression::eval_complex(op, left_type,
4988 left_real, left_imag,
4990 right_real, right_imag,
4994 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
4995 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
4997 if (left_type == NULL)
4999 else if (right_type == NULL)
5001 else if (!left_type->is_abstract()
5002 && left_type->named_type() != NULL)
5004 else if (!right_type->is_abstract()
5005 && right_type->named_type() != NULL)
5007 else if (!left_type->is_abstract())
5009 else if (!right_type->is_abstract())
5011 else if (left_type->complex_type() != NULL)
5013 else if (right_type->complex_type() != NULL)
5017 ret = Expression::make_complex(&real, &imag, type,
5026 mpfr_clear(left_real);
5027 mpfr_clear(left_imag);
5028 mpfr_clear(right_real);
5029 mpfr_clear(right_imag);
5034 mpfr_clear(left_real);
5035 mpfr_clear(left_imag);
5036 mpfr_clear(right_real);
5037 mpfr_clear(right_imag);
5040 // String constant expressions.
5041 if (op == OPERATOR_PLUS
5042 && left->type()->is_string_type()
5043 && right->type()->is_string_type())
5045 std::string left_string;
5046 std::string right_string;
5047 if (left->string_constant_value(&left_string)
5048 && right->string_constant_value(&right_string))
5049 return Expression::make_string(left_string + right_string, location);
5055 // Return the integer constant value, if it has one.
5058 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5064 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5067 mpz_clear(left_val);
5072 mpz_init(right_val);
5074 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5077 mpz_clear(right_val);
5078 mpz_clear(left_val);
5083 if (left_type != right_type
5084 && left_type != NULL
5085 && right_type != NULL
5086 && left_type->base() != right_type->base()
5087 && this->op_ != OPERATOR_RSHIFT
5088 && this->op_ != OPERATOR_LSHIFT)
5091 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5092 right_type, right_val,
5093 this->location(), val);
5095 mpz_clear(right_val);
5096 mpz_clear(left_val);
5104 // Return the floating point constant value, if it has one.
5107 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5110 mpfr_init(left_val);
5112 if (!this->left_->float_constant_value(left_val, &left_type))
5114 mpfr_clear(left_val);
5119 mpfr_init(right_val);
5121 if (!this->right_->float_constant_value(right_val, &right_type))
5123 mpfr_clear(right_val);
5124 mpfr_clear(left_val);
5129 if (left_type != right_type
5130 && left_type != NULL
5131 && right_type != NULL
5132 && left_type->base() != right_type->base())
5135 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5136 right_type, right_val,
5137 val, this->location());
5139 mpfr_clear(left_val);
5140 mpfr_clear(right_val);
5148 // Return the complex constant value, if it has one.
5151 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5156 mpfr_init(left_real);
5157 mpfr_init(left_imag);
5159 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5161 mpfr_clear(left_real);
5162 mpfr_clear(left_imag);
5168 mpfr_init(right_real);
5169 mpfr_init(right_imag);
5171 if (!this->right_->complex_constant_value(right_real, right_imag,
5174 mpfr_clear(left_real);
5175 mpfr_clear(left_imag);
5176 mpfr_clear(right_real);
5177 mpfr_clear(right_imag);
5182 if (left_type != right_type
5183 && left_type != NULL
5184 && right_type != NULL
5185 && left_type->base() != right_type->base())
5188 ret = Binary_expression::eval_complex(this->op_, left_type,
5189 left_real, left_imag,
5191 right_real, right_imag,
5194 mpfr_clear(left_real);
5195 mpfr_clear(left_imag);
5196 mpfr_clear(right_real);
5197 mpfr_clear(right_imag);
5205 // Note that the value is being discarded.
5208 Binary_expression::do_discarding_value()
5210 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5211 this->right_->discarding_value();
5213 this->warn_about_unused_value();
5219 Binary_expression::do_type()
5224 case OPERATOR_ANDAND:
5226 case OPERATOR_NOTEQ:
5231 return Type::lookup_bool_type();
5234 case OPERATOR_MINUS:
5241 case OPERATOR_BITCLEAR:
5243 Type* left_type = this->left_->type();
5244 Type* right_type = this->right_->type();
5245 if (!left_type->is_abstract() && left_type->named_type() != NULL)
5247 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5249 else if (!left_type->is_abstract())
5251 else if (!right_type->is_abstract())
5253 else if (left_type->complex_type() != NULL)
5255 else if (right_type->complex_type() != NULL)
5257 else if (left_type->float_type() != NULL)
5259 else if (right_type->float_type() != NULL)
5265 case OPERATOR_LSHIFT:
5266 case OPERATOR_RSHIFT:
5267 return this->left_->type();
5274 // Set type for a binary expression.
5277 Binary_expression::do_determine_type(const Type_context* context)
5279 Type* tleft = this->left_->type();
5280 Type* tright = this->right_->type();
5282 // Both sides should have the same type, except for the shift
5283 // operations. For a comparison, we should ignore the incoming
5286 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5287 || this->op_ == OPERATOR_RSHIFT);
5289 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5290 || this->op_ == OPERATOR_NOTEQ
5291 || this->op_ == OPERATOR_LT
5292 || this->op_ == OPERATOR_LE
5293 || this->op_ == OPERATOR_GT
5294 || this->op_ == OPERATOR_GE);
5296 Type_context subcontext(*context);
5300 // In a comparison, the context does not determine the types of
5302 subcontext.type = NULL;
5305 // Set the context for the left hand operand.
5308 // The right hand operand plays no role in determining the type
5309 // of the left hand operand. A shift of an abstract integer in
5310 // a string context gets special treatment, which may be a
5312 if (subcontext.type != NULL
5313 && subcontext.type->is_string_type()
5314 && tleft->is_abstract())
5315 error_at(this->location(), "shift of non-integer operand");
5317 else if (!tleft->is_abstract())
5318 subcontext.type = tleft;
5319 else if (!tright->is_abstract())
5320 subcontext.type = tright;
5321 else if (subcontext.type == NULL)
5323 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5324 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5325 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5327 // Both sides have an abstract integer, abstract float, or
5328 // abstract complex type. Just let CONTEXT determine
5329 // whether they may remain abstract or not.
5331 else if (tleft->complex_type() != NULL)
5332 subcontext.type = tleft;
5333 else if (tright->complex_type() != NULL)
5334 subcontext.type = tright;
5335 else if (tleft->float_type() != NULL)
5336 subcontext.type = tleft;
5337 else if (tright->float_type() != NULL)
5338 subcontext.type = tright;
5340 subcontext.type = tleft;
5343 this->left_->determine_type(&subcontext);
5345 // The context for the right hand operand is the same as for the
5346 // left hand operand, except for a shift operator.
5349 subcontext.type = Type::lookup_integer_type("uint");
5350 subcontext.may_be_abstract = false;
5353 this->right_->determine_type(&subcontext);
5356 // Report an error if the binary operator OP does not support TYPE.
5357 // Return whether the operation is OK. This should not be used for
5361 Binary_expression::check_operator_type(Operator op, Type* type,
5362 source_location location)
5367 case OPERATOR_ANDAND:
5368 if (!type->is_boolean_type())
5370 error_at(location, "expected boolean type");
5376 case OPERATOR_NOTEQ:
5377 if (type->integer_type() == NULL
5378 && type->float_type() == NULL
5379 && type->complex_type() == NULL
5380 && !type->is_string_type()
5381 && type->points_to() == NULL
5382 && !type->is_nil_type()
5383 && !type->is_boolean_type()
5384 && type->interface_type() == NULL
5385 && (type->array_type() == NULL
5386 || type->array_type()->length() != NULL)
5387 && type->map_type() == NULL
5388 && type->channel_type() == NULL
5389 && type->function_type() == NULL)
5392 ("expected integer, floating, complex, string, pointer, "
5393 "boolean, interface, slice, map, channel, "
5394 "or function type"));
5403 if (type->integer_type() == NULL
5404 && type->float_type() == NULL
5405 && !type->is_string_type())
5407 error_at(location, "expected integer, floating, or string type");
5413 case OPERATOR_PLUSEQ:
5414 if (type->integer_type() == NULL
5415 && type->float_type() == NULL
5416 && type->complex_type() == NULL
5417 && !type->is_string_type())
5420 "expected integer, floating, complex, or string type");
5425 case OPERATOR_MINUS:
5426 case OPERATOR_MINUSEQ:
5428 case OPERATOR_MULTEQ:
5430 case OPERATOR_DIVEQ:
5431 if (type->integer_type() == NULL
5432 && type->float_type() == NULL
5433 && type->complex_type() == NULL)
5435 error_at(location, "expected integer, floating, or complex type");
5441 case OPERATOR_MODEQ:
5445 case OPERATOR_ANDEQ:
5447 case OPERATOR_XOREQ:
5448 case OPERATOR_BITCLEAR:
5449 case OPERATOR_BITCLEAREQ:
5450 if (type->integer_type() == NULL)
5452 error_at(location, "expected integer type");
5467 Binary_expression::do_check_types(Gogo*)
5469 Type* left_type = this->left_->type();
5470 Type* right_type = this->right_->type();
5471 if (left_type->is_error_type() || right_type->is_error_type())
5474 if (this->op_ == OPERATOR_EQEQ
5475 || this->op_ == OPERATOR_NOTEQ
5476 || this->op_ == OPERATOR_LT
5477 || this->op_ == OPERATOR_LE
5478 || this->op_ == OPERATOR_GT
5479 || this->op_ == OPERATOR_GE)
5481 if (!Type::are_assignable(left_type, right_type, NULL)
5482 && !Type::are_assignable(right_type, left_type, NULL))
5484 this->report_error(_("incompatible types in binary expression"));
5487 if (!Binary_expression::check_operator_type(this->op_, left_type,
5489 || !Binary_expression::check_operator_type(this->op_, right_type,
5492 this->set_is_error();
5496 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5498 if (!Type::are_compatible_for_binop(left_type, right_type))
5500 this->report_error(_("incompatible types in binary expression"));
5503 if (!Binary_expression::check_operator_type(this->op_, left_type,
5506 this->set_is_error();
5512 if (left_type->integer_type() == NULL)
5513 this->report_error(_("shift of non-integer operand"));
5515 if (!right_type->is_abstract()
5516 && (right_type->integer_type() == NULL
5517 || !right_type->integer_type()->is_unsigned()))
5518 this->report_error(_("shift count not unsigned integer"));
5524 if (this->right_->integer_constant_value(true, val, &type))
5526 if (mpz_sgn(val) < 0)
5527 this->report_error(_("negative shift count"));
5534 // Get a tree for a binary expression.
5537 Binary_expression::do_get_tree(Translate_context* context)
5539 tree left = this->left_->get_tree(context);
5540 tree right = this->right_->get_tree(context);
5542 if (left == error_mark_node || right == error_mark_node)
5543 return error_mark_node;
5545 enum tree_code code;
5546 bool use_left_type = true;
5547 bool is_shift_op = false;
5551 case OPERATOR_NOTEQ:
5556 return Expression::comparison_tree(context, this->op_,
5557 this->left_->type(), left,
5558 this->right_->type(), right,
5562 code = TRUTH_ORIF_EXPR;
5563 use_left_type = false;
5565 case OPERATOR_ANDAND:
5566 code = TRUTH_ANDIF_EXPR;
5567 use_left_type = false;
5572 case OPERATOR_MINUS:
5576 code = BIT_IOR_EXPR;
5579 code = BIT_XOR_EXPR;
5586 Type *t = this->left_->type();
5587 if (t->float_type() != NULL || t->complex_type() != NULL)
5590 code = TRUNC_DIV_EXPR;
5594 code = TRUNC_MOD_EXPR;
5596 case OPERATOR_LSHIFT:
5600 case OPERATOR_RSHIFT:
5605 code = BIT_AND_EXPR;
5607 case OPERATOR_BITCLEAR:
5608 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5609 code = BIT_AND_EXPR;
5615 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5617 if (this->left_->type()->is_string_type())
5619 gcc_assert(this->op_ == OPERATOR_PLUS);
5620 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5621 static tree string_plus_decl;
5622 return Gogo::call_builtin(&string_plus_decl,
5633 tree compute_type = excess_precision_type(type);
5634 if (compute_type != NULL_TREE)
5636 left = ::convert(compute_type, left);
5637 right = ::convert(compute_type, right);
5640 tree eval_saved = NULL_TREE;
5644 left = save_expr(left);
5646 right = save_expr(right);
5647 // Make sure the values are evaluated.
5648 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
5649 void_type_node, left, right);
5652 tree ret = fold_build2_loc(this->location(),
5654 compute_type != NULL_TREE ? compute_type : type,
5657 if (compute_type != NULL_TREE)
5658 ret = ::convert(type, ret);
5660 // In Go, a shift larger than the size of the type is well-defined.
5661 // This is not true in GENERIC, so we need to insert a conditional.
5664 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
5665 gcc_assert(this->left_->type()->integer_type() != NULL);
5666 int bits = TYPE_PRECISION(TREE_TYPE(left));
5668 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
5669 build_int_cst_type(TREE_TYPE(right), bits));
5671 tree overflow_result = fold_convert_loc(this->location(),
5674 if (this->op_ == OPERATOR_RSHIFT
5675 && !this->left_->type()->integer_type()->is_unsigned())
5677 tree neg = fold_build2_loc(this->location(), LT_EXPR,
5678 boolean_type_node, left,
5679 fold_convert_loc(this->location(),
5681 integer_zero_node));
5682 tree neg_one = fold_build2_loc(this->location(),
5683 MINUS_EXPR, TREE_TYPE(left),
5684 fold_convert_loc(this->location(),
5687 fold_convert_loc(this->location(),
5690 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
5691 TREE_TYPE(left), neg, neg_one,
5695 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
5696 compare, ret, overflow_result);
5698 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
5699 TREE_TYPE(ret), eval_saved, ret);
5705 // Export a binary expression.
5708 Binary_expression::do_export(Export* exp) const
5710 exp->write_c_string("(");
5711 this->left_->export_expression(exp);
5715 exp->write_c_string(" || ");
5717 case OPERATOR_ANDAND:
5718 exp->write_c_string(" && ");
5721 exp->write_c_string(" == ");
5723 case OPERATOR_NOTEQ:
5724 exp->write_c_string(" != ");
5727 exp->write_c_string(" < ");
5730 exp->write_c_string(" <= ");
5733 exp->write_c_string(" > ");
5736 exp->write_c_string(" >= ");
5739 exp->write_c_string(" + ");
5741 case OPERATOR_MINUS:
5742 exp->write_c_string(" - ");
5745 exp->write_c_string(" | ");
5748 exp->write_c_string(" ^ ");
5751 exp->write_c_string(" * ");
5754 exp->write_c_string(" / ");
5757 exp->write_c_string(" % ");
5759 case OPERATOR_LSHIFT:
5760 exp->write_c_string(" << ");
5762 case OPERATOR_RSHIFT:
5763 exp->write_c_string(" >> ");
5766 exp->write_c_string(" & ");
5768 case OPERATOR_BITCLEAR:
5769 exp->write_c_string(" &^ ");
5774 this->right_->export_expression(exp);
5775 exp->write_c_string(")");
5778 // Import a binary expression.
5781 Binary_expression::do_import(Import* imp)
5783 imp->require_c_string("(");
5785 Expression* left = Expression::import_expression(imp);
5788 if (imp->match_c_string(" || "))
5793 else if (imp->match_c_string(" && "))
5795 op = OPERATOR_ANDAND;
5798 else if (imp->match_c_string(" == "))
5803 else if (imp->match_c_string(" != "))
5805 op = OPERATOR_NOTEQ;
5808 else if (imp->match_c_string(" < "))
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(" - "))
5835 op = OPERATOR_MINUS;
5838 else if (imp->match_c_string(" | "))
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(" << "))
5865 op = OPERATOR_LSHIFT;
5868 else if (imp->match_c_string(" >> "))
5870 op = OPERATOR_RSHIFT;
5873 else if (imp->match_c_string(" & "))
5878 else if (imp->match_c_string(" &^ "))
5880 op = OPERATOR_BITCLEAR;
5885 error_at(imp->location(), "unrecognized binary operator");
5886 return Expression::make_error(imp->location());
5889 Expression* right = Expression::import_expression(imp);
5891 imp->require_c_string(")");
5893 return Expression::make_binary(op, left, right, imp->location());
5896 // Make a binary expression.
5899 Expression::make_binary(Operator op, Expression* left, Expression* right,
5900 source_location location)
5902 return new Binary_expression(op, left, right, location);
5905 // Implement a comparison.
5908 Expression::comparison_tree(Translate_context* context, Operator op,
5909 Type* left_type, tree left_tree,
5910 Type* right_type, tree right_tree,
5911 source_location location)
5913 enum tree_code code;
5919 case OPERATOR_NOTEQ:
5938 if (left_type->is_string_type())
5940 gcc_assert(right_type->is_string_type());
5941 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5942 static tree string_compare_decl;
5943 left_tree = Gogo::call_builtin(&string_compare_decl,
5952 right_tree = build_int_cst_type(integer_type_node, 0);
5955 if ((left_type->interface_type() != NULL
5956 && right_type->interface_type() == NULL
5957 && !right_type->is_nil_type())
5958 || (left_type->interface_type() == NULL
5959 && !left_type->is_nil_type()
5960 && right_type->interface_type() != NULL))
5962 // Comparing an interface value to a non-interface value.
5963 if (left_type->interface_type() == NULL)
5965 std::swap(left_type, right_type);
5966 std::swap(left_tree, right_tree);
5969 // The right operand is not an interface. We need to take its
5970 // address if it is not a pointer.
5973 if (right_type->points_to() != NULL)
5975 make_tmp = NULL_TREE;
5978 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
5980 make_tmp = NULL_TREE;
5981 arg = build_fold_addr_expr_loc(location, right_tree);
5982 if (DECL_P(right_tree))
5983 TREE_ADDRESSABLE(right_tree) = 1;
5987 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
5988 get_name(right_tree));
5989 DECL_IGNORED_P(tmp) = 0;
5990 DECL_INITIAL(tmp) = right_tree;
5991 TREE_ADDRESSABLE(tmp) = 1;
5992 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
5993 SET_EXPR_LOCATION(make_tmp, location);
5994 arg = build_fold_addr_expr_loc(location, tmp);
5996 arg = fold_convert_loc(location, ptr_type_node, arg);
5998 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
6000 if (left_type->interface_type()->is_empty())
6002 static tree empty_interface_value_compare_decl;
6003 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6005 "__go_empty_interface_value_compare",
6008 TREE_TYPE(left_tree),
6010 TREE_TYPE(descriptor),
6014 // This can panic if the type is not comparable.
6015 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6019 static tree interface_value_compare_decl;
6020 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6022 "__go_interface_value_compare",
6025 TREE_TYPE(left_tree),
6027 TREE_TYPE(descriptor),
6031 // This can panic if the type is not comparable.
6032 TREE_NOTHROW(interface_value_compare_decl) = 0;
6034 right_tree = build_int_cst_type(integer_type_node, 0);
6036 if (make_tmp != NULL_TREE)
6037 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6040 else if (left_type->interface_type() != NULL
6041 && right_type->interface_type() != NULL)
6043 if (left_type->interface_type()->is_empty())
6045 gcc_assert(right_type->interface_type()->is_empty());
6046 static tree empty_interface_compare_decl;
6047 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6049 "__go_empty_interface_compare",
6052 TREE_TYPE(left_tree),
6054 TREE_TYPE(right_tree),
6056 // This can panic if the type is uncomparable.
6057 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6061 gcc_assert(!right_type->interface_type()->is_empty());
6062 static tree interface_compare_decl;
6063 left_tree = Gogo::call_builtin(&interface_compare_decl,
6065 "__go_interface_compare",
6068 TREE_TYPE(left_tree),
6070 TREE_TYPE(right_tree),
6072 // This can panic if the type is uncomparable.
6073 TREE_NOTHROW(interface_compare_decl) = 0;
6075 right_tree = build_int_cst_type(integer_type_node, 0);
6078 if (left_type->is_nil_type()
6079 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6081 std::swap(left_type, right_type);
6082 std::swap(left_tree, right_tree);
6085 if (right_type->is_nil_type())
6087 if (left_type->array_type() != NULL
6088 && left_type->array_type()->length() == NULL)
6090 Array_type* at = left_type->array_type();
6091 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6092 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6094 else if (left_type->interface_type() != NULL)
6096 // An interface is nil if the first field is nil.
6097 tree left_type_tree = TREE_TYPE(left_tree);
6098 gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6099 tree field = TYPE_FIELDS(left_type_tree);
6100 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6102 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6106 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6107 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6111 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6112 if (CAN_HAVE_LOCATION_P(ret))
6113 SET_EXPR_LOCATION(ret, location);
6117 // Class Bound_method_expression.
6122 Bound_method_expression::do_traverse(Traverse* traverse)
6124 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6125 return TRAVERSE_EXIT;
6126 return Expression::traverse(&this->method_, traverse);
6129 // Return the type of a bound method expression. The type of this
6130 // object is really the type of the method with no receiver. We
6131 // should be able to get away with just returning the type of the
6135 Bound_method_expression::do_type()
6137 return this->method_->type();
6140 // Determine the types of a method expression.
6143 Bound_method_expression::do_determine_type(const Type_context*)
6145 this->method_->determine_type_no_context();
6146 Type* mtype = this->method_->type();
6147 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6148 if (fntype == NULL || !fntype->is_method())
6149 this->expr_->determine_type_no_context();
6152 Type_context subcontext(fntype->receiver()->type(), false);
6153 this->expr_->determine_type(&subcontext);
6157 // Check the types of a method expression.
6160 Bound_method_expression::do_check_types(Gogo*)
6162 Type* type = this->method_->type()->deref();
6164 || type->function_type() == NULL
6165 || !type->function_type()->is_method())
6166 this->report_error(_("object is not a method"));
6169 Type* rtype = type->function_type()->receiver()->type()->deref();
6170 Type* etype = (this->expr_type_ != NULL
6172 : this->expr_->type());
6173 etype = etype->deref();
6174 if (!Type::are_identical(rtype, etype, NULL))
6175 this->report_error(_("method type does not match object type"));
6179 // Get the tree for a method expression. There is no standard tree
6180 // representation for this. The only places it may currently be used
6181 // are in a Call_expression or a Go_statement, which will take it
6182 // apart directly. So this has nothing to do at present.
6185 Bound_method_expression::do_get_tree(Translate_context*)
6190 // Make a method expression.
6192 Bound_method_expression*
6193 Expression::make_bound_method(Expression* expr, Expression* method,
6194 source_location location)
6196 return new Bound_method_expression(expr, method, location);
6199 // Class Builtin_call_expression. This is used for a call to a
6200 // builtin function.
6202 class Builtin_call_expression : public Call_expression
6205 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6206 bool is_varargs, source_location location);
6209 // This overrides Call_expression::do_lower.
6211 do_lower(Gogo*, Named_object*, int);
6214 do_is_constant() const;
6217 do_integer_constant_value(bool, mpz_t, Type**) const;
6220 do_float_constant_value(mpfr_t, Type**) const;
6223 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6229 do_determine_type(const Type_context*);
6232 do_check_types(Gogo*);
6237 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6238 this->args()->copy(),
6244 do_get_tree(Translate_context*);
6247 do_export(Export*) const;
6250 do_is_recover_call() const;
6253 do_set_recover_arg(Expression*);
6256 // The builtin functions.
6257 enum Builtin_function_code
6261 // Predeclared builtin functions.
6278 // Builtin functions from the unsafe package.
6291 real_imag_type(Type*);
6296 // A pointer back to the general IR structure. This avoids a global
6297 // variable, or passing it around everywhere.
6299 // The builtin function being called.
6300 Builtin_function_code code_;
6303 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6305 Expression_list* args,
6307 source_location location)
6308 : Call_expression(fn, args, is_varargs, location),
6309 gogo_(gogo), code_(BUILTIN_INVALID)
6311 Func_expression* fnexp = this->fn()->func_expression();
6312 gcc_assert(fnexp != NULL);
6313 const std::string& name(fnexp->named_object()->name());
6314 if (name == "append")
6315 this->code_ = BUILTIN_APPEND;
6316 else if (name == "cap")
6317 this->code_ = BUILTIN_CAP;
6318 else if (name == "close")
6319 this->code_ = BUILTIN_CLOSE;
6320 else if (name == "closed")
6321 this->code_ = BUILTIN_CLOSED;
6322 else if (name == "cmplx")
6323 this->code_ = BUILTIN_CMPLX;
6324 else if (name == "copy")
6325 this->code_ = BUILTIN_COPY;
6326 else if (name == "imag")
6327 this->code_ = BUILTIN_IMAG;
6328 else if (name == "len")
6329 this->code_ = BUILTIN_LEN;
6330 else if (name == "make")
6331 this->code_ = BUILTIN_MAKE;
6332 else if (name == "new")
6333 this->code_ = BUILTIN_NEW;
6334 else if (name == "panic")
6335 this->code_ = BUILTIN_PANIC;
6336 else if (name == "print")
6337 this->code_ = BUILTIN_PRINT;
6338 else if (name == "println")
6339 this->code_ = BUILTIN_PRINTLN;
6340 else if (name == "real")
6341 this->code_ = BUILTIN_REAL;
6342 else if (name == "recover")
6343 this->code_ = BUILTIN_RECOVER;
6344 else if (name == "Alignof")
6345 this->code_ = BUILTIN_ALIGNOF;
6346 else if (name == "Offsetof")
6347 this->code_ = BUILTIN_OFFSETOF;
6348 else if (name == "Sizeof")
6349 this->code_ = BUILTIN_SIZEOF;
6354 // Return whether this is a call to recover. This is a virtual
6355 // function called from the parent class.
6358 Builtin_call_expression::do_is_recover_call() const
6360 if (this->classification() == EXPRESSION_ERROR)
6362 return this->code_ == BUILTIN_RECOVER;
6365 // Set the argument for a call to recover.
6368 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6370 const Expression_list* args = this->args();
6371 gcc_assert(args == NULL || args->empty());
6372 Expression_list* new_args = new Expression_list();
6373 new_args->push_back(arg);
6374 this->set_args(new_args);
6377 // A traversal class which looks for a call expression.
6379 class Find_call_expression : public Traverse
6382 Find_call_expression()
6383 : Traverse(traverse_expressions),
6388 expression(Expression**);
6392 { return this->found_; }
6399 Find_call_expression::expression(Expression** pexpr)
6401 if ((*pexpr)->call_expression() != NULL)
6403 this->found_ = true;
6404 return TRAVERSE_EXIT;
6406 return TRAVERSE_CONTINUE;
6409 // Lower a builtin call expression. This turns new and make into
6410 // specific expressions. We also convert to a constant if we can.
6413 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6415 if (this->code_ == BUILTIN_NEW)
6417 const Expression_list* args = this->args();
6418 if (args == NULL || args->size() < 1)
6419 this->report_error(_("not enough arguments"));
6420 else if (args->size() > 1)
6421 this->report_error(_("too many arguments"));
6424 Expression* arg = args->front();
6425 if (!arg->is_type_expression())
6427 error_at(arg->location(), "expected type");
6428 this->set_is_error();
6431 return Expression::make_allocation(arg->type(), this->location());
6434 else if (this->code_ == BUILTIN_MAKE)
6436 const Expression_list* args = this->args();
6437 if (args == NULL || args->size() < 1)
6438 this->report_error(_("not enough arguments"));
6441 Expression* arg = args->front();
6442 if (!arg->is_type_expression())
6444 error_at(arg->location(), "expected type");
6445 this->set_is_error();
6449 Expression_list* newargs;
6450 if (args->size() == 1)
6454 newargs = new Expression_list();
6455 Expression_list::const_iterator p = args->begin();
6457 for (; p != args->end(); ++p)
6458 newargs->push_back(*p);
6460 return Expression::make_make(arg->type(), newargs,
6465 else if (this->is_constant())
6467 // We can only lower len and cap if there are no function calls
6468 // in the arguments. Otherwise we have to make the call.
6469 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6471 Expression* arg = this->one_arg();
6472 if (!arg->is_constant())
6474 Find_call_expression find_call;
6475 Expression::traverse(&arg, &find_call);
6476 if (find_call.found())
6484 if (this->integer_constant_value(true, ival, &type))
6486 Expression* ret = Expression::make_integer(&ival, type,
6495 if (this->float_constant_value(rval, &type))
6497 Expression* ret = Expression::make_float(&rval, type,
6505 if (this->complex_constant_value(rval, imag, &type))
6507 Expression* ret = Expression::make_complex(&rval, &imag, type,
6516 else if (this->code_ == BUILTIN_RECOVER)
6518 if (function != NULL)
6519 function->func_value()->set_calls_recover();
6522 // Calling recover outside of a function always returns the
6523 // nil empty interface.
6524 Type* eface = Type::make_interface_type(NULL, this->location());
6525 return Expression::make_cast(eface,
6526 Expression::make_nil(this->location()),
6530 else if (this->code_ == BUILTIN_APPEND)
6532 // Lower the varargs.
6533 const Expression_list* args = this->args();
6534 if (args == NULL || args->empty())
6536 Type* slice_type = args->front()->type();
6537 if (!slice_type->is_open_array_type())
6539 error_at(args->front()->location(), "argument 1 must be a slice");
6540 this->set_is_error();
6543 return this->lower_varargs(gogo, function, slice_type, 2);
6549 // Return the type of the real or imag functions, given the type of
6550 // the argument. We need to map complex to float, complex64 to
6551 // float32, and complex128 to float64, so it has to be done by name.
6552 // This returns NULL if it can't figure out the type.
6555 Builtin_call_expression::real_imag_type(Type* arg_type)
6557 if (arg_type == NULL || arg_type->is_abstract())
6559 Named_type* nt = arg_type->named_type();
6562 while (nt->real_type()->named_type() != NULL)
6563 nt = nt->real_type()->named_type();
6564 if (nt->name() == "complex")
6565 return Type::lookup_float_type("float");
6566 else if (nt->name() == "complex64")
6567 return Type::lookup_float_type("float32");
6568 else if (nt->name() == "complex128")
6569 return Type::lookup_float_type("float64");
6574 // Return the type of the cmplx function, given the type of one of the
6575 // argments. Like real_imag_type, we have to map by name.
6578 Builtin_call_expression::cmplx_type(Type* arg_type)
6580 if (arg_type == NULL || arg_type->is_abstract())
6582 Named_type* nt = arg_type->named_type();
6585 while (nt->real_type()->named_type() != NULL)
6586 nt = nt->real_type()->named_type();
6587 if (nt->name() == "float")
6588 return Type::lookup_complex_type("complex");
6589 else if (nt->name() == "float32")
6590 return Type::lookup_complex_type("complex64");
6591 else if (nt->name() == "float64")
6592 return Type::lookup_complex_type("complex128");
6597 // Return a single argument, or NULL if there isn't one.
6600 Builtin_call_expression::one_arg() const
6602 const Expression_list* args = this->args();
6603 if (args->size() != 1)
6605 return args->front();
6608 // Return whether this is constant: len of a string, or len or cap of
6609 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6612 Builtin_call_expression::do_is_constant() const
6614 switch (this->code_)
6619 Expression* arg = this->one_arg();
6622 Type* arg_type = arg->type();
6624 if (arg_type->points_to() != NULL
6625 && arg_type->points_to()->array_type() != NULL
6626 && !arg_type->points_to()->is_open_array_type())
6627 arg_type = arg_type->points_to();
6629 if (arg_type->array_type() != NULL
6630 && arg_type->array_type()->length() != NULL)
6631 return arg_type->array_type()->length()->is_constant();
6633 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6634 return arg->is_constant();
6638 case BUILTIN_SIZEOF:
6639 case BUILTIN_ALIGNOF:
6640 return this->one_arg() != NULL;
6642 case BUILTIN_OFFSETOF:
6644 Expression* arg = this->one_arg();
6647 return arg->field_reference_expression() != NULL;
6652 const Expression_list* args = this->args();
6653 if (args != NULL && args->size() == 2)
6654 return args->front()->is_constant() && args->back()->is_constant();
6661 Expression* arg = this->one_arg();
6662 return arg != NULL && arg->is_constant();
6672 // Return an integer constant value if possible.
6675 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
6679 if (this->code_ == BUILTIN_LEN
6680 || this->code_ == BUILTIN_CAP)
6682 Expression* arg = this->one_arg();
6685 Type* arg_type = arg->type();
6687 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
6690 if (arg->string_constant_value(&sval))
6692 mpz_set_ui(val, sval.length());
6693 *ptype = Type::lookup_integer_type("int");
6698 if (arg_type->points_to() != NULL
6699 && arg_type->points_to()->array_type() != NULL
6700 && !arg_type->points_to()->is_open_array_type())
6701 arg_type = arg_type->points_to();
6703 if (arg_type->array_type() != NULL
6704 && arg_type->array_type()->length() != NULL)
6706 Expression* e = arg_type->array_type()->length();
6707 if (e->integer_constant_value(iota_is_constant, val, ptype))
6709 *ptype = Type::lookup_integer_type("int");
6714 else if (this->code_ == BUILTIN_SIZEOF
6715 || this->code_ == BUILTIN_ALIGNOF)
6717 Expression* arg = this->one_arg();
6720 Type* arg_type = arg->type();
6721 if (arg_type->is_error_type())
6723 if (arg_type->is_abstract())
6725 tree arg_type_tree = arg_type->get_tree(this->gogo_);
6726 unsigned long val_long;
6727 if (this->code_ == BUILTIN_SIZEOF)
6729 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
6730 gcc_assert(TREE_CODE(type_size) == INTEGER_CST);
6731 if (TREE_INT_CST_HIGH(type_size) != 0)
6733 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
6734 val_long = static_cast<unsigned long>(val_wide);
6735 if (val_long != val_wide)
6738 else if (this->code_ == BUILTIN_ALIGNOF)
6740 val_long = TYPE_ALIGN(arg_type_tree);
6741 if (arg->field_reference_expression() != NULL)
6743 // Calling unsafe.Alignof(s.f) returns the alignment of
6744 // the type of f when it is used as a field in a struct.
6745 #ifdef BIGGEST_FIELD_ALIGNMENT
6746 if (val_long > BIGGEST_FIELD_ALIGNMENT)
6747 val_long = BIGGEST_FIELD_ALIGNMENT;
6749 #ifdef ADJUST_FIELD_ALIGN
6750 // A separate declaration avoids a warning promoted to
6751 // an error if ADJUST_FIELD_ALIGN ignores FIELD.
6753 field = build_decl(UNKNOWN_LOCATION, FIELD_DECL, NULL,
6755 val_long = ADJUST_FIELD_ALIGN(field, val_long);
6758 val_long /= BITS_PER_UNIT;
6762 mpz_set_ui(val, val_long);
6766 else if (this->code_ == BUILTIN_OFFSETOF)
6768 Expression* arg = this->one_arg();
6771 Field_reference_expression* farg = arg->field_reference_expression();
6774 Expression* struct_expr = farg->expr();
6775 Type* st = struct_expr->type();
6776 if (st->struct_type() == NULL)
6778 tree struct_tree = st->get_tree(this->gogo_);
6779 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
6780 tree field = TYPE_FIELDS(struct_tree);
6781 for (unsigned int index = farg->field_index(); index > 0; --index)
6783 field = DECL_CHAIN(field);
6784 gcc_assert(field != NULL_TREE);
6786 HOST_WIDE_INT offset_wide = int_byte_position (field);
6787 if (offset_wide < 0)
6789 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
6790 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
6792 mpz_set_ui(val, offset_long);
6798 // Return a floating point constant value if possible.
6801 Builtin_call_expression::do_float_constant_value(mpfr_t val,
6804 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
6806 Expression* arg = this->one_arg();
6817 if (arg->complex_constant_value(real, imag, &type))
6819 if (this->code_ == BUILTIN_REAL)
6820 mpfr_set(val, real, GMP_RNDN);
6822 mpfr_set(val, imag, GMP_RNDN);
6823 *ptype = Builtin_call_expression::real_imag_type(type);
6835 // Return a complex constant value if possible.
6838 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
6841 if (this->code_ == BUILTIN_CMPLX)
6843 const Expression_list* args = this->args();
6844 if (args == NULL || args->size() != 2)
6850 if (!args->front()->float_constant_value(r, &rtype))
6861 if (args->back()->float_constant_value(i, &itype)
6862 && Type::are_identical(rtype, itype, NULL))
6864 mpfr_set(real, r, GMP_RNDN);
6865 mpfr_set(imag, i, GMP_RNDN);
6866 *ptype = Builtin_call_expression::cmplx_type(rtype);
6882 Builtin_call_expression::do_type()
6884 switch (this->code_)
6886 case BUILTIN_INVALID:
6893 const Expression_list* args = this->args();
6894 if (args == NULL || args->empty())
6895 return Type::make_error_type();
6896 return Type::make_pointer_type(args->front()->type());
6902 case BUILTIN_ALIGNOF:
6903 case BUILTIN_OFFSETOF:
6904 case BUILTIN_SIZEOF:
6905 return Type::lookup_integer_type("int");
6910 case BUILTIN_PRINTLN:
6911 return Type::make_void_type();
6913 case BUILTIN_CLOSED:
6914 return Type::lookup_bool_type();
6916 case BUILTIN_RECOVER:
6917 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
6919 case BUILTIN_APPEND:
6921 const Expression_list* args = this->args();
6922 if (args == NULL || args->empty())
6923 return Type::make_error_type();
6924 return args->front()->type();
6930 Expression* arg = this->one_arg();
6932 return Type::make_error_type();
6933 Type* t = arg->type();
6934 if (t->is_abstract())
6935 t = t->make_non_abstract_type();
6936 t = Builtin_call_expression::real_imag_type(t);
6938 t = Type::make_error_type();
6944 const Expression_list* args = this->args();
6945 if (args == NULL || args->size() != 2)
6946 return Type::make_error_type();
6947 Type* t = args->front()->type();
6948 if (t->is_abstract())
6950 t = args->back()->type();
6951 if (t->is_abstract())
6952 t = t->make_non_abstract_type();
6954 t = Builtin_call_expression::cmplx_type(t);
6956 t = Type::make_error_type();
6962 // Determine the type.
6965 Builtin_call_expression::do_determine_type(const Type_context* context)
6967 this->fn()->determine_type_no_context();
6969 const Expression_list* args = this->args();
6972 Type* arg_type = NULL;
6973 switch (this->code_)
6976 case BUILTIN_PRINTLN:
6977 // Do not force a large integer constant to "int".
6983 arg_type = Builtin_call_expression::cmplx_type(context->type);
6989 // For the cmplx function the type of one operand can
6990 // determine the type of the other, as in a binary expression.
6991 arg_type = Builtin_call_expression::real_imag_type(context->type);
6992 if (args != NULL && args->size() == 2)
6994 Type* t1 = args->front()->type();
6995 Type* t2 = args->front()->type();
6996 if (!t1->is_abstract())
6998 else if (!t2->is_abstract())
7012 for (Expression_list::const_iterator pa = args->begin();
7016 Type_context subcontext;
7017 subcontext.type = arg_type;
7021 // We want to print large constants, we so can't just
7022 // use the appropriate nonabstract type. Use uint64 for
7023 // an integer if we know it is nonnegative, otherwise
7024 // use int64 for a integer, otherwise use float64 for a
7025 // float or complex128 for a complex.
7026 Type* want_type = NULL;
7027 Type* atype = (*pa)->type();
7028 if (atype->is_abstract())
7030 if (atype->integer_type() != NULL)
7035 if (this->integer_constant_value(true, val, &dummy)
7036 && mpz_sgn(val) >= 0)
7037 want_type = Type::lookup_integer_type("uint64");
7039 want_type = Type::lookup_integer_type("int64");
7042 else if (atype->float_type() != NULL)
7043 want_type = Type::lookup_float_type("float64");
7044 else if (atype->complex_type() != NULL)
7045 want_type = Type::lookup_complex_type("complex128");
7046 else if (atype->is_abstract_string_type())
7047 want_type = Type::lookup_string_type();
7048 else if (atype->is_abstract_boolean_type())
7049 want_type = Type::lookup_bool_type();
7052 subcontext.type = want_type;
7056 (*pa)->determine_type(&subcontext);
7061 // If there is exactly one argument, return true. Otherwise give an
7062 // error message and return false.
7065 Builtin_call_expression::check_one_arg()
7067 const Expression_list* args = this->args();
7068 if (args == NULL || args->size() < 1)
7070 this->report_error(_("not enough arguments"));
7073 else if (args->size() > 1)
7075 this->report_error(_("too many arguments"));
7078 if (args->front()->is_error_expression()
7079 || args->front()->type()->is_error_type())
7081 this->set_is_error();
7087 // Check argument types for a builtin function.
7090 Builtin_call_expression::do_check_types(Gogo*)
7092 switch (this->code_)
7094 case BUILTIN_INVALID:
7102 // The single argument may be either a string or an array or a
7103 // map or a channel, or a pointer to a closed array.
7104 if (this->check_one_arg())
7106 Type* arg_type = this->one_arg()->type();
7107 if (arg_type->points_to() != NULL
7108 && arg_type->points_to()->array_type() != NULL
7109 && !arg_type->points_to()->is_open_array_type())
7110 arg_type = arg_type->points_to();
7111 if (this->code_ == BUILTIN_CAP)
7113 if (!arg_type->is_error_type()
7114 && arg_type->array_type() == NULL
7115 && arg_type->channel_type() == NULL)
7116 this->report_error(_("argument must be array or slice "
7121 if (!arg_type->is_error_type()
7122 && !arg_type->is_string_type()
7123 && arg_type->array_type() == NULL
7124 && arg_type->map_type() == NULL
7125 && arg_type->channel_type() == NULL)
7126 this->report_error(_("argument must be string or "
7127 "array or slice or map or channel"));
7134 case BUILTIN_PRINTLN:
7136 const Expression_list* args = this->args();
7139 if (this->code_ == BUILTIN_PRINT)
7140 warning_at(this->location(), 0,
7141 "no arguments for builtin function %<%s%>",
7142 (this->code_ == BUILTIN_PRINT
7148 for (Expression_list::const_iterator p = args->begin();
7152 Type* type = (*p)->type();
7153 if (type->is_error_type()
7154 || type->is_string_type()
7155 || type->integer_type() != NULL
7156 || type->float_type() != NULL
7157 || type->complex_type() != NULL
7158 || type->is_boolean_type()
7159 || type->points_to() != NULL
7160 || type->interface_type() != NULL
7161 || type->channel_type() != NULL
7162 || type->map_type() != NULL
7163 || type->function_type() != NULL
7164 || type->is_open_array_type())
7167 this->report_error(_("unsupported argument type to "
7168 "builtin function"));
7175 case BUILTIN_CLOSED:
7176 if (this->check_one_arg())
7178 if (this->one_arg()->type()->channel_type() == NULL)
7179 this->report_error(_("argument must be channel"));
7184 case BUILTIN_SIZEOF:
7185 case BUILTIN_ALIGNOF:
7186 this->check_one_arg();
7189 case BUILTIN_RECOVER:
7190 if (this->args() != NULL && !this->args()->empty())
7191 this->report_error(_("too many arguments"));
7194 case BUILTIN_OFFSETOF:
7195 if (this->check_one_arg())
7197 Expression* arg = this->one_arg();
7198 if (arg->field_reference_expression() == NULL)
7199 this->report_error(_("argument must be a field reference"));
7205 const Expression_list* args = this->args();
7206 if (args == NULL || args->size() < 2)
7208 this->report_error(_("not enough arguments"));
7211 else if (args->size() > 2)
7213 this->report_error(_("too many arguments"));
7216 Type* arg1_type = args->front()->type();
7217 Type* arg2_type = args->back()->type();
7218 if (arg1_type->is_error_type() || arg2_type->is_error_type())
7222 if (arg1_type->is_open_array_type())
7223 e1 = arg1_type->array_type()->element_type();
7226 this->report_error(_("left argument must be a slice"));
7231 if (arg2_type->is_open_array_type())
7232 e2 = arg2_type->array_type()->element_type();
7233 else if (arg2_type->is_string_type())
7234 e2 = Type::lookup_integer_type("uint8");
7237 this->report_error(_("right argument must be a slice or a string"));
7241 if (!Type::are_identical(e1, e2, NULL))
7242 this->report_error(_("element types must be the same"));
7246 case BUILTIN_APPEND:
7248 const Expression_list* args = this->args();
7249 if (args == NULL || args->empty())
7251 this->report_error(_("not enough arguments"));
7254 /* Lowering varargs should have left us with 2 arguments. */
7255 gcc_assert(args->size() == 2);
7257 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7261 this->report_error(_("arguments 1 and 2 have different types"));
7264 error_at(this->location(),
7265 "arguments 1 and 2 have different types (%s)",
7267 this->set_is_error();
7275 if (this->check_one_arg())
7277 if (this->one_arg()->type()->complex_type() == NULL)
7278 this->report_error(_("argument must have complex type"));
7284 const Expression_list* args = this->args();
7285 if (args == NULL || args->size() < 2)
7286 this->report_error(_("not enough arguments"));
7287 else if (args->size() > 2)
7288 this->report_error(_("too many arguments"));
7289 else if (args->front()->is_error_expression()
7290 || args->front()->type()->is_error_type()
7291 || args->back()->is_error_expression()
7292 || args->back()->type()->is_error_type())
7293 this->set_is_error();
7294 else if (!Type::are_identical(args->front()->type(),
7295 args->back()->type(), NULL))
7296 this->report_error(_("cmplx arguments must have identical types"));
7297 else if (args->front()->type()->float_type() == NULL)
7298 this->report_error(_("cmplx arguments must have "
7299 "floating-point type"));
7308 // Return the tree for a builtin function.
7311 Builtin_call_expression::do_get_tree(Translate_context* context)
7313 Gogo* gogo = context->gogo();
7314 source_location location = this->location();
7315 switch (this->code_)
7317 case BUILTIN_INVALID:
7325 const Expression_list* args = this->args();
7326 gcc_assert(args != NULL && args->size() == 1);
7327 Expression* arg = *args->begin();
7328 Type* arg_type = arg->type();
7329 tree arg_tree = arg->get_tree(context);
7330 if (arg_tree == error_mark_node)
7331 return error_mark_node;
7333 if (arg_type->points_to() != NULL)
7335 arg_type = arg_type->points_to();
7336 gcc_assert(arg_type->array_type() != NULL
7337 && !arg_type->is_open_array_type());
7338 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7339 arg_tree = build_fold_indirect_ref(arg_tree);
7343 if (this->code_ == BUILTIN_LEN)
7345 if (arg_type->is_string_type())
7346 val_tree = String_type::length_tree(gogo, arg_tree);
7347 else if (arg_type->array_type() != NULL)
7348 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7349 else if (arg_type->map_type() != NULL)
7351 static tree map_len_fndecl;
7352 val_tree = Gogo::call_builtin(&map_len_fndecl,
7357 arg_type->get_tree(gogo),
7360 else if (arg_type->channel_type() != NULL)
7362 static tree chan_len_fndecl;
7363 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7368 arg_type->get_tree(gogo),
7376 if (arg_type->array_type() != NULL)
7377 val_tree = arg_type->array_type()->capacity_tree(gogo, arg_tree);
7378 else if (arg_type->channel_type() != NULL)
7380 static tree chan_cap_fndecl;
7381 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7386 arg_type->get_tree(gogo),
7393 tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo);
7394 if (type_tree == TREE_TYPE(val_tree))
7397 return fold(convert_to_integer(type_tree, val_tree));
7401 case BUILTIN_PRINTLN:
7403 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7404 tree stmt_list = NULL_TREE;
7406 const Expression_list* call_args = this->args();
7407 if (call_args != NULL)
7409 for (Expression_list::const_iterator p = call_args->begin();
7410 p != call_args->end();
7413 if (is_ln && p != call_args->begin())
7415 static tree print_space_fndecl;
7416 tree call = Gogo::call_builtin(&print_space_fndecl,
7421 append_to_statement_list(call, &stmt_list);
7424 Type* type = (*p)->type();
7426 tree arg = (*p)->get_tree(context);
7427 if (arg == error_mark_node)
7428 return error_mark_node;
7432 if (type->is_string_type())
7434 static tree print_string_fndecl;
7435 pfndecl = &print_string_fndecl;
7436 fnname = "__go_print_string";
7438 else if (type->integer_type() != NULL
7439 && type->integer_type()->is_unsigned())
7441 static tree print_uint64_fndecl;
7442 pfndecl = &print_uint64_fndecl;
7443 fnname = "__go_print_uint64";
7444 Type* itype = Type::lookup_integer_type("uint64");
7445 arg = fold_convert_loc(location, itype->get_tree(gogo),
7448 else if (type->integer_type() != NULL)
7450 static tree print_int64_fndecl;
7451 pfndecl = &print_int64_fndecl;
7452 fnname = "__go_print_int64";
7453 Type* itype = Type::lookup_integer_type("int64");
7454 arg = fold_convert_loc(location, itype->get_tree(gogo),
7457 else if (type->float_type() != NULL)
7459 static tree print_double_fndecl;
7460 pfndecl = &print_double_fndecl;
7461 fnname = "__go_print_double";
7462 arg = fold_convert_loc(location, double_type_node, arg);
7464 else if (type->complex_type() != NULL)
7466 static tree print_complex_fndecl;
7467 pfndecl = &print_complex_fndecl;
7468 fnname = "__go_print_complex";
7469 arg = fold_convert_loc(location, complex_double_type_node,
7472 else if (type->is_boolean_type())
7474 static tree print_bool_fndecl;
7475 pfndecl = &print_bool_fndecl;
7476 fnname = "__go_print_bool";
7478 else if (type->points_to() != NULL
7479 || type->channel_type() != NULL
7480 || type->map_type() != NULL
7481 || type->function_type() != NULL)
7483 static tree print_pointer_fndecl;
7484 pfndecl = &print_pointer_fndecl;
7485 fnname = "__go_print_pointer";
7486 arg = fold_convert_loc(location, ptr_type_node, arg);
7488 else if (type->interface_type() != NULL)
7490 if (type->interface_type()->is_empty())
7492 static tree print_empty_interface_fndecl;
7493 pfndecl = &print_empty_interface_fndecl;
7494 fnname = "__go_print_empty_interface";
7498 static tree print_interface_fndecl;
7499 pfndecl = &print_interface_fndecl;
7500 fnname = "__go_print_interface";
7503 else if (type->is_open_array_type())
7505 static tree print_slice_fndecl;
7506 pfndecl = &print_slice_fndecl;
7507 fnname = "__go_print_slice";
7512 tree call = Gogo::call_builtin(pfndecl,
7519 append_to_statement_list(call, &stmt_list);
7525 static tree print_nl_fndecl;
7526 tree call = Gogo::call_builtin(&print_nl_fndecl,
7531 append_to_statement_list(call, &stmt_list);
7539 const Expression_list* args = this->args();
7540 gcc_assert(args != NULL && args->size() == 1);
7541 Expression* arg = args->front();
7542 tree arg_tree = arg->get_tree(context);
7543 if (arg_tree == error_mark_node)
7544 return error_mark_node;
7545 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7546 arg_tree = Expression::convert_for_assignment(context, empty,
7548 arg_tree, location);
7549 static tree panic_fndecl;
7550 tree call = Gogo::call_builtin(&panic_fndecl,
7555 TREE_TYPE(arg_tree),
7557 // This function will throw an exception.
7558 TREE_NOTHROW(panic_fndecl) = 0;
7559 // This function will not return.
7560 TREE_THIS_VOLATILE(panic_fndecl) = 1;
7564 case BUILTIN_RECOVER:
7566 // The argument is set when building recover thunks. It's a
7567 // boolean value which is true if we can recover a value now.
7568 const Expression_list* args = this->args();
7569 gcc_assert(args != NULL && args->size() == 1);
7570 Expression* arg = args->front();
7571 tree arg_tree = arg->get_tree(context);
7572 if (arg_tree == error_mark_node)
7573 return error_mark_node;
7575 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
7576 tree empty_tree = empty->get_tree(context->gogo());
7578 Type* nil_type = Type::make_nil_type();
7579 Expression* nil = Expression::make_nil(location);
7580 tree nil_tree = nil->get_tree(context);
7581 tree empty_nil_tree = Expression::convert_for_assignment(context,
7587 // We need to handle a deferred call to recover specially,
7588 // because it changes whether it can recover a panic or not.
7589 // See test7 in test/recover1.go.
7591 if (this->is_deferred())
7593 static tree deferred_recover_fndecl;
7594 call = Gogo::call_builtin(&deferred_recover_fndecl,
7596 "__go_deferred_recover",
7602 static tree recover_fndecl;
7603 call = Gogo::call_builtin(&recover_fndecl,
7609 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
7610 call, empty_nil_tree);
7614 case BUILTIN_CLOSED:
7616 const Expression_list* args = this->args();
7617 gcc_assert(args != NULL && args->size() == 1);
7618 Expression* arg = args->front();
7619 tree arg_tree = arg->get_tree(context);
7620 if (arg_tree == error_mark_node)
7621 return error_mark_node;
7622 if (this->code_ == BUILTIN_CLOSE)
7624 static tree close_fndecl;
7625 return Gogo::call_builtin(&close_fndecl,
7627 "__go_builtin_close",
7630 TREE_TYPE(arg_tree),
7635 static tree closed_fndecl;
7636 return Gogo::call_builtin(&closed_fndecl,
7638 "__go_builtin_closed",
7641 TREE_TYPE(arg_tree),
7646 case BUILTIN_SIZEOF:
7647 case BUILTIN_OFFSETOF:
7648 case BUILTIN_ALIGNOF:
7653 bool b = this->integer_constant_value(true, val, &dummy);
7655 tree type = Type::lookup_integer_type("int")->get_tree(gogo);
7656 tree ret = Expression::integer_constant_tree(val, type);
7663 const Expression_list* args = this->args();
7664 gcc_assert(args != NULL && args->size() == 2);
7665 Expression* arg1 = args->front();
7666 Expression* arg2 = args->back();
7668 tree arg1_tree = arg1->get_tree(context);
7669 tree arg2_tree = arg2->get_tree(context);
7670 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
7671 return error_mark_node;
7673 Type* arg1_type = arg1->type();
7674 Array_type* at = arg1_type->array_type();
7675 arg1_tree = save_expr(arg1_tree);
7676 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
7677 tree arg1_len = at->length_tree(gogo, arg1_tree);
7679 Type* arg2_type = arg2->type();
7682 if (arg2_type->is_open_array_type())
7684 at = arg2_type->array_type();
7685 arg2_tree = save_expr(arg2_tree);
7686 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
7687 arg2_len = at->length_tree(gogo, arg2_tree);
7691 arg2_tree = save_expr(arg2_tree);
7692 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
7693 arg2_len = String_type::length_tree(gogo, arg2_tree);
7696 arg1_len = save_expr(arg1_len);
7697 arg2_len = save_expr(arg2_len);
7698 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
7699 fold_build2_loc(location, LT_EXPR,
7701 arg1_len, arg2_len),
7702 arg1_len, arg2_len);
7703 len = save_expr(len);
7705 Type* element_type = at->element_type();
7706 tree element_type_tree = element_type->get_tree(gogo);
7707 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
7708 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
7710 bytecount = fold_build2_loc(location, MULT_EXPR,
7711 TREE_TYPE(element_size),
7712 bytecount, element_size);
7713 bytecount = fold_convert_loc(location, size_type_node, bytecount);
7715 tree call = build_call_expr_loc(location,
7716 built_in_decls[BUILT_IN_MEMMOVE],
7717 3, arg1_val, arg2_val, bytecount);
7719 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
7723 case BUILTIN_APPEND:
7725 const Expression_list* args = this->args();
7726 gcc_assert(args != NULL && args->size() == 2);
7727 Expression* arg1 = args->front();
7728 Expression* arg2 = args->back();
7730 tree arg1_tree = arg1->get_tree(context);
7731 tree arg2_tree = arg2->get_tree(context);
7732 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
7733 return error_mark_node;
7735 tree descriptor_tree = arg1->type()->type_descriptor_pointer(gogo);
7737 // We rebuild the decl each time since the slice types may
7739 tree append_fndecl = NULL_TREE;
7740 return Gogo::call_builtin(&append_fndecl,
7744 TREE_TYPE(arg1_tree),
7745 TREE_TYPE(descriptor_tree),
7747 TREE_TYPE(arg1_tree),
7749 TREE_TYPE(arg2_tree),
7756 const Expression_list* args = this->args();
7757 gcc_assert(args != NULL && args->size() == 1);
7758 Expression* arg = args->front();
7759 tree arg_tree = arg->get_tree(context);
7760 if (arg_tree == error_mark_node)
7761 return error_mark_node;
7762 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
7763 if (this->code_ == BUILTIN_REAL)
7764 return fold_build1_loc(location, REALPART_EXPR,
7765 TREE_TYPE(TREE_TYPE(arg_tree)),
7768 return fold_build1_loc(location, IMAGPART_EXPR,
7769 TREE_TYPE(TREE_TYPE(arg_tree)),
7775 const Expression_list* args = this->args();
7776 gcc_assert(args != NULL && args->size() == 2);
7777 tree r = args->front()->get_tree(context);
7778 tree i = args->back()->get_tree(context);
7779 if (r == error_mark_node || i == error_mark_node)
7780 return error_mark_node;
7781 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
7782 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
7783 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
7784 return fold_build2_loc(location, COMPLEX_EXPR,
7785 build_complex_type(TREE_TYPE(r)),
7794 // We have to support exporting a builtin call expression, because
7795 // code can set a constant to the result of a builtin expression.
7798 Builtin_call_expression::do_export(Export* exp) const
7805 if (this->integer_constant_value(true, val, &dummy))
7807 Integer_expression::export_integer(exp, val);
7816 if (this->float_constant_value(fval, &dummy))
7818 Float_expression::export_float(exp, fval);
7830 if (this->complex_constant_value(real, imag, &dummy))
7832 Complex_expression::export_complex(exp, real, imag);
7841 error_at(this->location(), "value is not constant");
7845 // A trailing space lets us reliably identify the end of the number.
7846 exp->write_c_string(" ");
7849 // Class Call_expression.
7854 Call_expression::do_traverse(Traverse* traverse)
7856 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
7857 return TRAVERSE_EXIT;
7858 if (this->args_ != NULL)
7860 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
7861 return TRAVERSE_EXIT;
7863 return TRAVERSE_CONTINUE;
7866 // Lower a call statement.
7869 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
7871 // A type case can look like a function call.
7872 if (this->fn_->is_type_expression()
7873 && this->args_ != NULL
7874 && this->args_->size() == 1)
7875 return Expression::make_cast(this->fn_->type(), this->args_->front(),
7878 // Recognize a call to a builtin function.
7879 Func_expression* fne = this->fn_->func_expression();
7881 && fne->named_object()->is_function_declaration()
7882 && fne->named_object()->func_declaration_value()->type()->is_builtin())
7883 return new Builtin_call_expression(gogo, this->fn_, this->args_,
7884 this->is_varargs_, this->location());
7886 // Handle an argument which is a call to a function which returns
7887 // multiple results.
7888 if (this->args_ != NULL
7889 && this->args_->size() == 1
7890 && this->args_->front()->call_expression() != NULL
7891 && this->fn_->type()->function_type() != NULL)
7893 Function_type* fntype = this->fn_->type()->function_type();
7894 size_t rc = this->args_->front()->call_expression()->result_count();
7896 && fntype->parameters() != NULL
7897 && (fntype->parameters()->size() == rc
7898 || (fntype->is_varargs()
7899 && fntype->parameters()->size() - 1 <= rc)))
7901 Call_expression* call = this->args_->front()->call_expression();
7902 Expression_list* args = new Expression_list;
7903 for (size_t i = 0; i < rc; ++i)
7904 args->push_back(Expression::make_call_result(call, i));
7905 // We can't return a new call expression here, because this
7906 // one may be referenced by Call_result expressions. FIXME.
7912 // Handle a call to a varargs function by packaging up the extra
7914 if (this->fn_->type()->function_type() != NULL
7915 && this->fn_->type()->function_type()->is_varargs())
7917 Function_type* fntype = this->fn_->type()->function_type();
7918 const Typed_identifier_list* parameters = fntype->parameters();
7919 gcc_assert(parameters != NULL && !parameters->empty());
7920 Type* varargs_type = parameters->back().type();
7921 return this->lower_varargs(gogo, function, varargs_type,
7922 parameters->size());
7928 // Lower a call to a varargs function. FUNCTION is the function in
7929 // which the call occurs--it's not the function we are calling.
7930 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
7931 // PARAM_COUNT is the number of parameters of the function we are
7932 // calling; the last of these parameters will be the varargs
7936 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
7937 Type* varargs_type, size_t param_count)
7939 if (this->varargs_are_lowered_)
7942 source_location loc = this->location();
7944 gcc_assert(param_count > 0);
7945 gcc_assert(varargs_type->is_open_array_type());
7947 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
7948 if (arg_count < param_count - 1)
7950 // Not enough arguments; will be caught in check_types.
7954 Expression_list* old_args = this->args_;
7955 Expression_list* new_args = new Expression_list();
7956 bool push_empty_arg = false;
7957 if (old_args == NULL || old_args->empty())
7959 gcc_assert(param_count == 1);
7960 push_empty_arg = true;
7964 Expression_list::const_iterator pa;
7966 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
7968 if (static_cast<size_t>(i) == param_count)
7970 new_args->push_back(*pa);
7973 // We have reached the varargs parameter.
7975 bool issued_error = false;
7976 if (pa == old_args->end())
7977 push_empty_arg = true;
7978 else if (pa + 1 == old_args->end() && this->is_varargs_)
7979 new_args->push_back(*pa);
7980 else if (this->is_varargs_)
7982 this->report_error(_("too many arguments"));
7985 else if (pa + 1 == old_args->end()
7986 && this->is_compatible_varargs_argument(function, *pa,
7989 new_args->push_back(*pa);
7992 Type* element_type = varargs_type->array_type()->element_type();
7993 Expression_list* vals = new Expression_list;
7994 for (; pa != old_args->end(); ++pa, ++i)
7996 // Check types here so that we get a better message.
7997 Type* patype = (*pa)->type();
7998 source_location paloc = (*pa)->location();
7999 if (!this->check_argument_type(i, element_type, patype,
8000 paloc, issued_error))
8002 vals->push_back(*pa);
8005 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8006 new_args->push_back(val);
8011 new_args->push_back(Expression::make_nil(loc));
8013 // We can't return a new call expression here, because this one may
8014 // be referenced by Call_result expressions. FIXME.
8015 if (old_args != NULL)
8017 this->args_ = new_args;
8018 this->varargs_are_lowered_ = true;
8020 // Lower all the new subexpressions.
8021 Expression* ret = this;
8022 gogo->lower_expression(function, &ret);
8023 gcc_assert(ret == this);
8027 // Return true if ARG is a varargs argment which should be passed to
8028 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8029 // will be the last argument passed in the call, and PARAM_TYPE will
8030 // be the type of the last parameter of the varargs function being
8034 Call_expression::is_compatible_varargs_argument(Named_object* function,
8039 *issued_error = false;
8041 Type* var_type = NULL;
8043 // The simple case is passing the varargs parameter of the caller.
8044 Var_expression* ve = arg->var_expression();
8045 if (ve != NULL && ve->named_object()->is_variable())
8047 Variable* var = ve->named_object()->var_value();
8048 if (var->is_varargs_parameter())
8049 var_type = var->type();
8052 // The complex case is passing the varargs parameter of some
8053 // enclosing function. This will look like passing down *c.f where
8054 // c is the closure variable and f is a field in the closure.
8055 if (function != NULL
8056 && function->func_value()->needs_closure()
8057 && arg->classification() == EXPRESSION_UNARY)
8059 Unary_expression* ue = static_cast<Unary_expression*>(arg);
8060 if (ue->op() == OPERATOR_MULT)
8062 Field_reference_expression* fre =
8063 ue->operand()->deref()->field_reference_expression();
8066 Var_expression* ve = fre->expr()->deref()->var_expression();
8069 Named_object* no = ve->named_object();
8070 Function* f = function->func_value();
8071 if (no == f->closure_var())
8073 // At this point we know that this indeed a
8074 // reference to some enclosing variable. Now we
8075 // need to figure out whether that variable is a
8076 // varargs parameter.
8077 Named_object* enclosing =
8078 f->enclosing_var(fre->field_index());
8079 Variable* var = enclosing->var_value();
8080 if (var->is_varargs_parameter())
8081 var_type = var->type();
8088 if (var_type == NULL)
8091 // We only match if the parameter is the same, with an identical
8093 Array_type* var_at = var_type->array_type();
8094 gcc_assert(var_at != NULL);
8095 Array_type* param_at = param_type->array_type();
8096 if (param_at != NULL
8097 && Type::are_identical(var_at->element_type(),
8098 param_at->element_type(), NULL))
8100 error_at(arg->location(), "... mismatch: passing ...T as ...");
8101 *issued_error = true;
8105 // Get the function type. Returns NULL if we don't know the type. If
8106 // this returns NULL, and if_ERROR is true, issues an error.
8109 Call_expression::get_function_type() const
8111 return this->fn_->type()->function_type();
8114 // Return the number of values which this call will return.
8117 Call_expression::result_count() const
8119 const Function_type* fntype = this->get_function_type();
8122 if (fntype->results() == NULL)
8124 return fntype->results()->size();
8127 // Return whether this is a call to the predeclared function recover.
8130 Call_expression::is_recover_call() const
8132 return this->do_is_recover_call();
8135 // Set the argument to the recover function.
8138 Call_expression::set_recover_arg(Expression* arg)
8140 this->do_set_recover_arg(arg);
8143 // Virtual functions also implemented by Builtin_call_expression.
8146 Call_expression::do_is_recover_call() const
8152 Call_expression::do_set_recover_arg(Expression*)
8160 Call_expression::do_type()
8162 if (this->type_ != NULL)
8166 Function_type* fntype = this->get_function_type();
8168 return Type::make_error_type();
8170 const Typed_identifier_list* results = fntype->results();
8171 if (results == NULL)
8172 ret = Type::make_void_type();
8173 else if (results->size() == 1)
8174 ret = results->begin()->type();
8176 ret = Type::make_call_multiple_result_type(this);
8183 // Determine types for a call expression. We can use the function
8184 // parameter types to set the types of the arguments.
8187 Call_expression::do_determine_type(const Type_context*)
8189 this->fn_->determine_type_no_context();
8190 Function_type* fntype = this->get_function_type();
8191 const Typed_identifier_list* parameters = NULL;
8193 parameters = fntype->parameters();
8194 if (this->args_ != NULL)
8196 Typed_identifier_list::const_iterator pt;
8197 if (parameters != NULL)
8198 pt = parameters->begin();
8199 for (Expression_list::const_iterator pa = this->args_->begin();
8200 pa != this->args_->end();
8203 if (parameters != NULL && pt != parameters->end())
8205 Type_context subcontext(pt->type(), false);
8206 (*pa)->determine_type(&subcontext);
8210 (*pa)->determine_type_no_context();
8215 // Check types for parameter I.
8218 Call_expression::check_argument_type(int i, const Type* parameter_type,
8219 const Type* argument_type,
8220 source_location argument_location,
8224 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8229 error_at(argument_location, "argument %d has incompatible type", i);
8231 error_at(argument_location,
8232 "argument %d has incompatible type (%s)",
8235 this->set_is_error();
8244 Call_expression::do_check_types(Gogo*)
8246 Function_type* fntype = this->get_function_type();
8249 if (!this->fn_->type()->is_error_type())
8250 this->report_error(_("expected function"));
8254 if (fntype->is_method())
8256 // We don't support pointers to methods, so the function has to
8257 // be a bound method expression.
8258 Bound_method_expression* bme = this->fn_->bound_method_expression();
8261 this->report_error(_("method call without object"));
8264 Type* first_arg_type = bme->first_argument()->type();
8265 if (first_arg_type->points_to() == NULL)
8267 // When passing a value, we need to check that we are
8268 // permitted to copy it.
8270 if (!Type::are_assignable(fntype->receiver()->type(),
8271 first_arg_type, &reason))
8274 this->report_error(_("incompatible type for receiver"));
8277 error_at(this->location(),
8278 "incompatible type for receiver (%s)",
8280 this->set_is_error();
8286 // Note that varargs was handled by the lower_varargs() method, so
8287 // we don't have to worry about it here.
8289 const Typed_identifier_list* parameters = fntype->parameters();
8290 if (this->args_ == NULL)
8292 if (parameters != NULL && !parameters->empty())
8293 this->report_error(_("not enough arguments"));
8295 else if (parameters == NULL)
8296 this->report_error(_("too many arguments"));
8300 Typed_identifier_list::const_iterator pt = parameters->begin();
8301 for (Expression_list::const_iterator pa = this->args_->begin();
8302 pa != this->args_->end();
8305 if (pt == parameters->end())
8307 this->report_error(_("too many arguments"));
8310 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8311 (*pa)->location(), false);
8313 if (pt != parameters->end())
8314 this->report_error(_("not enough arguments"));
8318 // Return whether we have to use a temporary variable to ensure that
8319 // we evaluate this call expression in order. If the call returns no
8320 // results then it will inevitably be executed last. If the call
8321 // returns more than one result then it will be used with Call_result
8322 // expressions. So we only have to use a temporary variable if the
8323 // call returns exactly one result.
8326 Call_expression::do_must_eval_in_order() const
8328 return this->result_count() == 1;
8331 // Get the function and the first argument to use when calling a bound
8335 Call_expression::bound_method_function(Translate_context* context,
8336 Bound_method_expression* bound_method,
8337 tree* first_arg_ptr)
8339 Expression* first_argument = bound_method->first_argument();
8340 tree first_arg = first_argument->get_tree(context);
8341 if (first_arg == error_mark_node)
8342 return error_mark_node;
8344 // We always pass a pointer to the first argument when calling a
8346 if (first_argument->type()->points_to() == NULL)
8348 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8349 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8350 || DECL_P(first_arg)
8351 || TREE_CODE(first_arg) == INDIRECT_REF
8352 || TREE_CODE(first_arg) == COMPONENT_REF)
8354 first_arg = build_fold_addr_expr(first_arg);
8355 if (DECL_P(first_arg))
8356 TREE_ADDRESSABLE(first_arg) = 1;
8360 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8361 get_name(first_arg));
8362 DECL_IGNORED_P(tmp) = 0;
8363 DECL_INITIAL(tmp) = first_arg;
8364 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8365 build1(DECL_EXPR, void_type_node, tmp),
8366 build_fold_addr_expr(tmp));
8367 TREE_ADDRESSABLE(tmp) = 1;
8369 if (first_arg == error_mark_node)
8370 return error_mark_node;
8373 Type* fatype = bound_method->first_argument_type();
8376 if (fatype->points_to() == NULL)
8377 fatype = Type::make_pointer_type(fatype);
8378 first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg);
8379 if (first_arg == error_mark_node
8380 || TREE_TYPE(first_arg) == error_mark_node)
8381 return error_mark_node;
8384 *first_arg_ptr = first_arg;
8386 return bound_method->method()->get_tree(context);
8389 // Get the function and the first argument to use when calling an
8390 // interface method.
8393 Call_expression::interface_method_function(
8394 Translate_context* context,
8395 Interface_field_reference_expression* interface_method,
8396 tree* first_arg_ptr)
8398 tree expr = interface_method->expr()->get_tree(context);
8399 if (expr == error_mark_node)
8400 return error_mark_node;
8401 expr = save_expr(expr);
8402 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8403 if (first_arg == error_mark_node)
8404 return error_mark_node;
8405 *first_arg_ptr = first_arg;
8406 return interface_method->get_function_tree(context, expr);
8409 // Build the call expression.
8412 Call_expression::do_get_tree(Translate_context* context)
8414 if (this->tree_ != NULL_TREE)
8417 Function_type* fntype = this->get_function_type();
8419 return error_mark_node;
8421 if (this->fn_->is_error_expression())
8422 return error_mark_node;
8424 Gogo* gogo = context->gogo();
8425 source_location location = this->location();
8427 Func_expression* func = this->fn_->func_expression();
8428 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8429 Interface_field_reference_expression* interface_method =
8430 this->fn_->interface_field_reference_expression();
8431 const bool has_closure = func != NULL && func->closure() != NULL;
8432 const bool is_method = bound_method != NULL || interface_method != NULL;
8433 gcc_assert(!fntype->is_method() || is_method);
8437 if (this->args_ == NULL || this->args_->empty())
8439 nargs = is_method ? 1 : 0;
8440 args = nargs == 0 ? NULL : new tree[nargs];
8444 const Typed_identifier_list* params = fntype->parameters();
8445 gcc_assert(params != NULL);
8447 nargs = this->args_->size();
8448 int i = is_method ? 1 : 0;
8450 args = new tree[nargs];
8452 Typed_identifier_list::const_iterator pp = params->begin();
8453 Expression_list::const_iterator pe;
8454 for (pe = this->args_->begin();
8455 pe != this->args_->end();
8458 tree arg_val = (*pe)->get_tree(context);
8459 args[i] = Expression::convert_for_assignment(context,
8464 if (args[i] == error_mark_node)
8465 return error_mark_node;
8467 gcc_assert(pp == params->end());
8468 gcc_assert(i == nargs);
8471 tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo)));
8472 if (rettype == error_mark_node)
8473 return error_mark_node;
8477 fn = func->get_tree_without_closure(gogo);
8478 else if (!is_method)
8479 fn = this->fn_->get_tree(context);
8480 else if (bound_method != NULL)
8481 fn = this->bound_method_function(context, bound_method, &args[0]);
8482 else if (interface_method != NULL)
8483 fn = this->interface_method_function(context, interface_method, &args[0]);
8487 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8488 return error_mark_node;
8490 // This is to support builtin math functions when using 80387 math.
8492 if (TREE_CODE(fndecl) == ADDR_EXPR)
8493 fndecl = TREE_OPERAND(fndecl, 0);
8494 tree excess_type = NULL_TREE;
8496 && DECL_IS_BUILTIN(fndecl)
8497 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8499 && ((SCALAR_FLOAT_TYPE_P(rettype)
8500 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8501 || (COMPLEX_FLOAT_TYPE_P(rettype)
8502 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8504 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8505 if (excess_type != NULL_TREE)
8507 tree excess_fndecl = mathfn_built_in(excess_type,
8508 DECL_FUNCTION_CODE(fndecl));
8509 if (excess_fndecl == NULL_TREE)
8510 excess_type = NULL_TREE;
8513 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8514 for (int i = 0; i < nargs; ++i)
8515 args[i] = ::convert(excess_type, args[i]);
8520 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
8524 SET_EXPR_LOCATION(ret, location);
8528 tree closure_tree = func->closure()->get_tree(context);
8529 if (closure_tree != error_mark_node)
8530 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
8533 // If this is a recursive function type which returns itself, as in
8535 // we have used ptr_type_node for the return type. Add a cast here
8536 // to the correct type.
8537 if (TREE_TYPE(ret) == ptr_type_node)
8539 tree t = this->type()->get_tree(gogo);
8540 ret = fold_convert_loc(location, t, ret);
8543 if (excess_type != NULL_TREE)
8545 // Calling convert here can undo our excess precision change.
8546 // That may or may not be a bug in convert_to_real.
8547 ret = build1(NOP_EXPR, rettype, ret);
8550 // If there is more than one result, we will refer to the call
8552 if (fntype->results() != NULL && fntype->results()->size() > 1)
8553 ret = save_expr(ret);
8560 // Make a call expression.
8563 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
8564 source_location location)
8566 return new Call_expression(fn, args, is_varargs, location);
8569 // A single result from a call which returns multiple results.
8571 class Call_result_expression : public Expression
8574 Call_result_expression(Call_expression* call, unsigned int index)
8575 : Expression(EXPRESSION_CALL_RESULT, call->location()),
8576 call_(call), index_(index)
8581 do_traverse(Traverse*);
8587 do_determine_type(const Type_context*);
8590 do_check_types(Gogo*);
8595 return new Call_result_expression(this->call_->call_expression(),
8600 do_must_eval_in_order() const
8604 do_get_tree(Translate_context*);
8607 // The underlying call expression.
8609 // Which result we want.
8610 unsigned int index_;
8613 // Traverse a call result.
8616 Call_result_expression::do_traverse(Traverse* traverse)
8618 if (traverse->remember_expression(this->call_))
8620 // We have already traversed the call expression.
8621 return TRAVERSE_CONTINUE;
8623 return Expression::traverse(&this->call_, traverse);
8629 Call_result_expression::do_type()
8631 // THIS->CALL_ can be replaced with a temporary reference due to
8632 // Call_expression::do_must_eval_in_order when there is an error.
8633 Call_expression* ce = this->call_->call_expression();
8635 return Type::make_error_type();
8636 Function_type* fntype = ce->get_function_type();
8638 return Type::make_error_type();
8639 const Typed_identifier_list* results = fntype->results();
8640 Typed_identifier_list::const_iterator pr = results->begin();
8641 for (unsigned int i = 0; i < this->index_; ++i)
8643 if (pr == results->end())
8644 return Type::make_error_type();
8647 if (pr == results->end())
8648 return Type::make_error_type();
8652 // Check the type. This is where we give an error if we're trying to
8653 // extract too many values from a call.
8656 Call_result_expression::do_check_types(Gogo*)
8659 Call_expression* ce = this->call_->call_expression();
8661 ok = this->index_ < ce->result_count();
8664 // This can happen when the call returns a single value but we
8665 // are asking for the second result.
8666 if (this->call_->is_error_expression())
8671 error_at(this->location(),
8672 "number of results does not match number of values");
8675 // Determine the type. We have nothing to do here, but the 0 result
8676 // needs to pass down to the caller.
8679 Call_result_expression::do_determine_type(const Type_context*)
8681 if (this->index_ == 0)
8682 this->call_->determine_type_no_context();
8688 Call_result_expression::do_get_tree(Translate_context* context)
8690 tree call_tree = this->call_->get_tree(context);
8691 if (call_tree == error_mark_node)
8692 return error_mark_node;
8693 gcc_assert(TREE_CODE(TREE_TYPE(call_tree)) == RECORD_TYPE);
8694 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
8695 for (unsigned int i = 0; i < this->index_; ++i)
8697 gcc_assert(field != NULL_TREE);
8698 field = DECL_CHAIN(field);
8700 gcc_assert(field != NULL_TREE);
8701 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
8704 // Make a reference to a single result of a call which returns
8705 // multiple results.
8708 Expression::make_call_result(Call_expression* call, unsigned int index)
8710 return new Call_result_expression(call, index);
8713 // Class Index_expression.
8718 Index_expression::do_traverse(Traverse* traverse)
8720 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
8721 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
8722 || (this->end_ != NULL
8723 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
8724 return TRAVERSE_EXIT;
8725 return TRAVERSE_CONTINUE;
8728 // Lower an index expression. This converts the generic index
8729 // expression into an array index, a string index, or a map index.
8732 Index_expression::do_lower(Gogo*, Named_object*, int)
8734 source_location location = this->location();
8735 Expression* left = this->left_;
8736 Expression* start = this->start_;
8737 Expression* end = this->end_;
8739 Type* type = left->type();
8740 if (type->is_error_type())
8741 return Expression::make_error(location);
8742 else if (type->array_type() != NULL)
8743 return Expression::make_array_index(left, start, end, location);
8744 else if (type->points_to() != NULL
8745 && type->points_to()->array_type() != NULL
8746 && !type->points_to()->is_open_array_type())
8748 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
8750 return Expression::make_array_index(deref, start, end, location);
8752 else if (type->is_string_type())
8753 return Expression::make_string_index(left, start, end, location);
8754 else if (type->map_type() != NULL)
8758 error_at(location, "invalid slice of map");
8759 return Expression::make_error(location);
8761 Map_index_expression* ret= Expression::make_map_index(left, start,
8763 if (this->is_lvalue_)
8764 ret->set_is_lvalue();
8770 "attempt to index object which is not array, string, or map");
8771 return Expression::make_error(location);
8775 // Make an index expression.
8778 Expression::make_index(Expression* left, Expression* start, Expression* end,
8779 source_location location)
8781 return new Index_expression(left, start, end, location);
8784 // An array index. This is used for both indexing and slicing.
8786 class Array_index_expression : public Expression
8789 Array_index_expression(Expression* array, Expression* start,
8790 Expression* end, source_location location)
8791 : Expression(EXPRESSION_ARRAY_INDEX, location),
8792 array_(array), start_(start), end_(end), type_(NULL)
8797 do_traverse(Traverse*);
8803 do_determine_type(const Type_context*);
8806 do_check_types(Gogo*);
8811 return Expression::make_array_index(this->array_->copy(),
8812 this->start_->copy(),
8815 : this->end_->copy()),
8820 do_is_addressable() const;
8823 do_address_taken(bool escapes)
8824 { this->array_->address_taken(escapes); }
8827 do_get_tree(Translate_context*);
8830 // The array we are getting a value from.
8832 // The start or only index.
8834 // The end index of a slice. This may be NULL for a simple array
8835 // index, or it may be a nil expression for the length of the array.
8837 // The type of the expression.
8841 // Array index traversal.
8844 Array_index_expression::do_traverse(Traverse* traverse)
8846 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
8847 return TRAVERSE_EXIT;
8848 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
8849 return TRAVERSE_EXIT;
8850 if (this->end_ != NULL)
8852 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
8853 return TRAVERSE_EXIT;
8855 return TRAVERSE_CONTINUE;
8858 // Return the type of an array index.
8861 Array_index_expression::do_type()
8863 if (this->type_ == NULL)
8865 Array_type* type = this->array_->type()->array_type();
8867 this->type_ = Type::make_error_type();
8868 else if (this->end_ == NULL)
8869 this->type_ = type->element_type();
8870 else if (type->is_open_array_type())
8872 // A slice of a slice has the same type as the original
8874 this->type_ = this->array_->type()->deref();
8878 // A slice of an array is a slice.
8879 this->type_ = Type::make_array_type(type->element_type(), NULL);
8885 // Set the type of an array index.
8888 Array_index_expression::do_determine_type(const Type_context*)
8890 this->array_->determine_type_no_context();
8891 Type_context subcontext(NULL, true);
8892 this->start_->determine_type(&subcontext);
8893 if (this->end_ != NULL)
8894 this->end_->determine_type(&subcontext);
8897 // Check types of an array index.
8900 Array_index_expression::do_check_types(Gogo*)
8902 if (this->start_->type()->integer_type() == NULL)
8903 this->report_error(_("index must be integer"));
8904 if (this->end_ != NULL
8905 && this->end_->type()->integer_type() == NULL
8906 && !this->end_->is_nil_expression())
8907 this->report_error(_("slice end must be integer"));
8909 Array_type* array_type = this->array_->type()->array_type();
8910 gcc_assert(array_type != NULL);
8912 unsigned int int_bits =
8913 Type::lookup_integer_type("int")->integer_type()->bits();
8918 bool lval_valid = (array_type->length() != NULL
8919 && array_type->length()->integer_constant_value(true,
8924 if (this->start_->integer_constant_value(true, ival, &dummy))
8926 if (mpz_sgn(ival) < 0
8927 || mpz_sizeinbase(ival, 2) >= int_bits
8929 && (this->end_ == NULL
8930 ? mpz_cmp(ival, lval) >= 0
8931 : mpz_cmp(ival, lval) > 0)))
8933 error_at(this->start_->location(), "array index out of bounds");
8934 this->set_is_error();
8937 if (this->end_ != NULL && !this->end_->is_nil_expression())
8939 if (this->end_->integer_constant_value(true, ival, &dummy))
8941 if (mpz_sgn(ival) < 0
8942 || mpz_sizeinbase(ival, 2) >= int_bits
8943 || (lval_valid && mpz_cmp(ival, lval) > 0))
8945 error_at(this->end_->location(), "array index out of bounds");
8946 this->set_is_error();
8953 // A slice of an array requires an addressable array. A slice of a
8954 // slice is always possible.
8955 if (this->end_ != NULL
8956 && !array_type->is_open_array_type()
8957 && !this->array_->is_addressable())
8958 this->report_error(_("array is not addressable"));
8961 // Return whether this expression is addressable.
8964 Array_index_expression::do_is_addressable() const
8966 // A slice expression is not addressable.
8967 if (this->end_ != NULL)
8970 // An index into a slice is addressable.
8971 if (this->array_->type()->is_open_array_type())
8974 // An index into an array is addressable if the array is
8976 return this->array_->is_addressable();
8979 // Get a tree for an array index.
8982 Array_index_expression::do_get_tree(Translate_context* context)
8984 Gogo* gogo = context->gogo();
8985 source_location loc = this->location();
8987 Array_type* array_type = this->array_->type()->array_type();
8988 gcc_assert(array_type != NULL);
8990 tree type_tree = array_type->get_tree(gogo);
8992 tree array_tree = this->array_->get_tree(context);
8993 if (array_tree == error_mark_node)
8994 return error_mark_node;
8996 if (array_type->length() == NULL && !DECL_P(array_tree))
8997 array_tree = save_expr(array_tree);
8998 tree length_tree = array_type->length_tree(gogo, array_tree);
8999 length_tree = save_expr(length_tree);
9000 tree length_type = TREE_TYPE(length_tree);
9002 tree bad_index = boolean_false_node;
9004 tree start_tree = this->start_->get_tree(context);
9005 if (start_tree == error_mark_node)
9006 return error_mark_node;
9007 if (!DECL_P(start_tree))
9008 start_tree = save_expr(start_tree);
9009 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9010 start_tree = convert_to_integer(length_type, start_tree);
9012 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9015 start_tree = fold_convert_loc(loc, length_type, start_tree);
9016 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9017 fold_build2_loc(loc,
9021 boolean_type_node, start_tree,
9024 int code = (array_type->length() != NULL
9025 ? (this->end_ == NULL
9026 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9027 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9028 : (this->end_ == NULL
9029 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9030 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9031 tree crash = Gogo::runtime_error(code, loc);
9033 if (this->end_ == NULL)
9035 // Simple array indexing. This has to return an l-value, so
9036 // wrap the index check into START_TREE.
9037 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9038 build3(COND_EXPR, void_type_node,
9039 bad_index, crash, NULL_TREE),
9041 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9043 if (array_type->length() != NULL)
9046 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9047 start_tree, NULL_TREE, NULL_TREE);
9052 tree values = array_type->value_pointer_tree(gogo, array_tree);
9053 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9054 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9055 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9056 start_tree, element_size);
9057 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9058 TREE_TYPE(values), values, offset);
9059 return build_fold_indirect_ref(ptr);
9065 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9066 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9069 if (this->end_->is_nil_expression())
9070 end_tree = length_tree;
9073 end_tree = this->end_->get_tree(context);
9074 if (end_tree == error_mark_node)
9075 return error_mark_node;
9076 if (!DECL_P(end_tree))
9077 end_tree = save_expr(end_tree);
9078 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9079 end_tree = convert_to_integer(length_type, end_tree);
9081 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9084 end_tree = fold_convert_loc(loc, length_type, end_tree);
9086 capacity_tree = save_expr(capacity_tree);
9087 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9088 fold_build2_loc(loc, LT_EXPR,
9090 end_tree, start_tree),
9091 fold_build2_loc(loc, GT_EXPR,
9093 end_tree, capacity_tree));
9094 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9095 bad_index, bad_end);
9098 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9099 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9101 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9102 fold_convert_loc(loc, sizetype, start_tree),
9105 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9107 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9108 TREE_TYPE(value_pointer),
9109 value_pointer, offset);
9111 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9112 end_tree, start_tree);
9114 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9115 capacity_tree, start_tree);
9117 tree struct_tree = this->type()->get_tree(gogo);
9118 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9120 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9122 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9123 tree field = TYPE_FIELDS(struct_tree);
9124 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9126 elt->value = value_pointer;
9128 elt = VEC_quick_push(constructor_elt, init, NULL);
9129 field = DECL_CHAIN(field);
9130 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9132 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9134 elt = VEC_quick_push(constructor_elt, init, NULL);
9135 field = DECL_CHAIN(field);
9136 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9138 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9140 tree constructor = build_constructor(struct_tree, init);
9142 if (TREE_CONSTANT(value_pointer)
9143 && TREE_CONSTANT(result_length_tree)
9144 && TREE_CONSTANT(result_capacity_tree))
9145 TREE_CONSTANT(constructor) = 1;
9147 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9148 build3(COND_EXPR, void_type_node,
9149 bad_index, crash, NULL_TREE),
9153 // Make an array index expression. END may be NULL.
9156 Expression::make_array_index(Expression* array, Expression* start,
9157 Expression* end, source_location location)
9159 // Taking a slice of a composite literal requires moving the literal
9161 if (end != NULL && array->is_composite_literal())
9163 array = Expression::make_heap_composite(array, location);
9164 array = Expression::make_unary(OPERATOR_MULT, array, location);
9166 return new Array_index_expression(array, start, end, location);
9169 // A string index. This is used for both indexing and slicing.
9171 class String_index_expression : public Expression
9174 String_index_expression(Expression* string, Expression* start,
9175 Expression* end, source_location location)
9176 : Expression(EXPRESSION_STRING_INDEX, location),
9177 string_(string), start_(start), end_(end)
9182 do_traverse(Traverse*);
9188 do_determine_type(const Type_context*);
9191 do_check_types(Gogo*);
9196 return Expression::make_string_index(this->string_->copy(),
9197 this->start_->copy(),
9200 : this->end_->copy()),
9205 do_get_tree(Translate_context*);
9208 // The string we are getting a value from.
9209 Expression* string_;
9210 // The start or only index.
9212 // The end index of a slice. This may be NULL for a single index,
9213 // or it may be a nil expression for the length of the string.
9217 // String index traversal.
9220 String_index_expression::do_traverse(Traverse* traverse)
9222 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9223 return TRAVERSE_EXIT;
9224 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9225 return TRAVERSE_EXIT;
9226 if (this->end_ != NULL)
9228 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9229 return TRAVERSE_EXIT;
9231 return TRAVERSE_CONTINUE;
9234 // Return the type of a string index.
9237 String_index_expression::do_type()
9239 if (this->end_ == NULL)
9240 return Type::lookup_integer_type("uint8");
9242 return Type::make_string_type();
9245 // Determine the type of a string index.
9248 String_index_expression::do_determine_type(const Type_context*)
9250 this->string_->determine_type_no_context();
9251 Type_context subcontext(NULL, true);
9252 this->start_->determine_type(&subcontext);
9253 if (this->end_ != NULL)
9254 this->end_->determine_type(&subcontext);
9257 // Check types of a string index.
9260 String_index_expression::do_check_types(Gogo*)
9262 if (this->start_->type()->integer_type() == NULL)
9263 this->report_error(_("index must be integer"));
9264 if (this->end_ != NULL
9265 && this->end_->type()->integer_type() == NULL
9266 && !this->end_->is_nil_expression())
9267 this->report_error(_("slice end must be integer"));
9270 bool sval_valid = this->string_->string_constant_value(&sval);
9275 if (this->start_->integer_constant_value(true, ival, &dummy))
9277 if (mpz_sgn(ival) < 0
9278 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9280 error_at(this->start_->location(), "string index out of bounds");
9281 this->set_is_error();
9284 if (this->end_ != NULL && !this->end_->is_nil_expression())
9286 if (this->end_->integer_constant_value(true, ival, &dummy))
9288 if (mpz_sgn(ival) < 0
9289 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9291 error_at(this->end_->location(), "string index out of bounds");
9292 this->set_is_error();
9299 // Get a tree for a string index.
9302 String_index_expression::do_get_tree(Translate_context* context)
9304 source_location loc = this->location();
9306 tree string_tree = this->string_->get_tree(context);
9307 if (string_tree == error_mark_node)
9308 return error_mark_node;
9310 if (this->string_->type()->points_to() != NULL)
9311 string_tree = build_fold_indirect_ref(string_tree);
9312 if (!DECL_P(string_tree))
9313 string_tree = save_expr(string_tree);
9314 tree string_type = TREE_TYPE(string_tree);
9316 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9317 length_tree = save_expr(length_tree);
9318 tree length_type = TREE_TYPE(length_tree);
9320 tree bad_index = boolean_false_node;
9322 tree start_tree = this->start_->get_tree(context);
9323 if (start_tree == error_mark_node)
9324 return error_mark_node;
9325 if (!DECL_P(start_tree))
9326 start_tree = save_expr(start_tree);
9327 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9328 start_tree = convert_to_integer(length_type, start_tree);
9330 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9333 start_tree = fold_convert_loc(loc, length_type, start_tree);
9335 int code = (this->end_ == NULL
9336 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9337 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9338 tree crash = Gogo::runtime_error(code, loc);
9340 if (this->end_ == NULL)
9342 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9344 fold_build2_loc(loc, GE_EXPR,
9346 start_tree, length_tree));
9348 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9349 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9351 fold_convert_loc(loc, sizetype, start_tree));
9352 tree index = build_fold_indirect_ref_loc(loc, ptr);
9354 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9355 build3(COND_EXPR, void_type_node,
9356 bad_index, crash, NULL_TREE),
9362 if (this->end_->is_nil_expression())
9363 end_tree = build_int_cst(length_type, -1);
9366 end_tree = this->end_->get_tree(context);
9367 if (end_tree == error_mark_node)
9368 return error_mark_node;
9369 if (!DECL_P(end_tree))
9370 end_tree = save_expr(end_tree);
9371 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9372 end_tree = convert_to_integer(length_type, end_tree);
9374 bad_index = Expression::check_bounds(end_tree, length_type,
9377 end_tree = fold_convert_loc(loc, length_type, end_tree);
9380 static tree strslice_fndecl;
9381 tree ret = Gogo::call_builtin(&strslice_fndecl,
9383 "__go_string_slice",
9392 // This will panic if the bounds are out of range for the
9394 TREE_NOTHROW(strslice_fndecl) = 0;
9396 if (bad_index == boolean_false_node)
9399 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9400 build3(COND_EXPR, void_type_node,
9401 bad_index, crash, NULL_TREE),
9406 // Make a string index expression. END may be NULL.
9409 Expression::make_string_index(Expression* string, Expression* start,
9410 Expression* end, source_location location)
9412 return new String_index_expression(string, start, end, location);
9417 // Get the type of the map.
9420 Map_index_expression::get_map_type() const
9422 Map_type* mt = this->map_->type()->deref()->map_type();
9423 gcc_assert(mt != NULL);
9427 // Map index traversal.
9430 Map_index_expression::do_traverse(Traverse* traverse)
9432 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9433 return TRAVERSE_EXIT;
9434 return Expression::traverse(&this->index_, traverse);
9437 // Return the type of a map index.
9440 Map_index_expression::do_type()
9442 Type* type = this->get_map_type()->val_type();
9443 // If this map index is in a tuple assignment, we actually return a
9444 // pointer to the value type. Tuple_map_assignment_statement is
9445 // responsible for handling this correctly. We need to get the type
9446 // right in case this gets assigned to a temporary variable.
9447 if (this->is_in_tuple_assignment_)
9448 type = Type::make_pointer_type(type);
9452 // Fix the type of a map index.
9455 Map_index_expression::do_determine_type(const Type_context*)
9457 this->map_->determine_type_no_context();
9458 Type_context subcontext(this->get_map_type()->key_type(), false);
9459 this->index_->determine_type(&subcontext);
9462 // Check types of a map index.
9465 Map_index_expression::do_check_types(Gogo*)
9468 if (!Type::are_assignable(this->get_map_type()->key_type(),
9469 this->index_->type(), &reason))
9472 this->report_error(_("incompatible type for map index"));
9475 error_at(this->location(), "incompatible type for map index (%s)",
9477 this->set_is_error();
9482 // Get a tree for a map index.
9485 Map_index_expression::do_get_tree(Translate_context* context)
9487 Map_type* type = this->get_map_type();
9489 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
9490 if (valptr == error_mark_node)
9491 return error_mark_node;
9492 valptr = save_expr(valptr);
9494 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
9496 if (this->is_lvalue_)
9497 return build_fold_indirect_ref(valptr);
9498 else if (this->is_in_tuple_assignment_)
9500 // Tuple_map_assignment_statement is responsible for using this
9506 return fold_build3(COND_EXPR, val_type_tree,
9507 fold_build2(EQ_EXPR, boolean_type_node, valptr,
9508 fold_convert(TREE_TYPE(valptr),
9509 null_pointer_node)),
9510 type->val_type()->get_init_tree(context->gogo(),
9512 build_fold_indirect_ref(valptr));
9516 // Get a tree for the map index. This returns a tree which evaluates
9517 // to a pointer to a value. The pointer will be NULL if the key is
9521 Map_index_expression::get_value_pointer(Translate_context* context,
9524 Map_type* type = this->get_map_type();
9526 tree map_tree = this->map_->get_tree(context);
9527 tree index_tree = this->index_->get_tree(context);
9528 index_tree = Expression::convert_for_assignment(context, type->key_type(),
9529 this->index_->type(),
9532 if (map_tree == error_mark_node || index_tree == error_mark_node)
9533 return error_mark_node;
9535 if (this->map_->type()->points_to() != NULL)
9536 map_tree = build_fold_indirect_ref(map_tree);
9538 // We need to pass in a pointer to the key, so stuff it into a
9540 tree tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
9541 DECL_IGNORED_P(tmp) = 0;
9542 DECL_INITIAL(tmp) = index_tree;
9543 tree make_tmp = build1(DECL_EXPR, void_type_node, tmp);
9544 tree tmpref = fold_convert(const_ptr_type_node, build_fold_addr_expr(tmp));
9545 TREE_ADDRESSABLE(tmp) = 1;
9547 static tree map_index_fndecl;
9548 tree call = Gogo::call_builtin(&map_index_fndecl,
9552 const_ptr_type_node,
9553 TREE_TYPE(map_tree),
9555 const_ptr_type_node,
9560 : boolean_false_node));
9561 // This can panic on a map of interface type if the interface holds
9562 // an uncomparable or unhashable type.
9563 TREE_NOTHROW(map_index_fndecl) = 0;
9565 tree val_type_tree = type->val_type()->get_tree(context->gogo());
9566 if (val_type_tree == error_mark_node)
9567 return error_mark_node;
9568 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
9570 return build2(COMPOUND_EXPR, ptr_val_type_tree,
9572 fold_convert(ptr_val_type_tree, call));
9575 // Make a map index expression.
9577 Map_index_expression*
9578 Expression::make_map_index(Expression* map, Expression* index,
9579 source_location location)
9581 return new Map_index_expression(map, index, location);
9584 // Class Field_reference_expression.
9586 // Return the type of a field reference.
9589 Field_reference_expression::do_type()
9591 Struct_type* struct_type = this->expr_->type()->struct_type();
9592 gcc_assert(struct_type != NULL);
9593 return struct_type->field(this->field_index_)->type();
9596 // Check the types for a field reference.
9599 Field_reference_expression::do_check_types(Gogo*)
9601 Struct_type* struct_type = this->expr_->type()->struct_type();
9602 gcc_assert(struct_type != NULL);
9603 gcc_assert(struct_type->field(this->field_index_) != NULL);
9606 // Get a tree for a field reference.
9609 Field_reference_expression::do_get_tree(Translate_context* context)
9611 tree struct_tree = this->expr_->get_tree(context);
9612 if (struct_tree == error_mark_node
9613 || TREE_TYPE(struct_tree) == error_mark_node)
9614 return error_mark_node;
9615 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
9616 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
9617 gcc_assert(field != NULL_TREE);
9618 for (unsigned int i = this->field_index_; i > 0; --i)
9620 field = DECL_CHAIN(field);
9621 gcc_assert(field != NULL_TREE);
9623 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
9627 // Make a reference to a qualified identifier in an expression.
9629 Field_reference_expression*
9630 Expression::make_field_reference(Expression* expr, unsigned int field_index,
9631 source_location location)
9633 return new Field_reference_expression(expr, field_index, location);
9636 // Class Interface_field_reference_expression.
9638 // Return a tree for the pointer to the function to call.
9641 Interface_field_reference_expression::get_function_tree(Translate_context*,
9644 if (this->expr_->type()->points_to() != NULL)
9645 expr = build_fold_indirect_ref(expr);
9647 tree expr_type = TREE_TYPE(expr);
9648 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
9650 tree field = TYPE_FIELDS(expr_type);
9651 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
9653 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
9654 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table)));
9656 table = build_fold_indirect_ref(table);
9657 gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
9659 std::string name = Gogo::unpack_hidden_name(this->name_);
9660 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
9662 field = DECL_CHAIN(field))
9664 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
9667 gcc_assert(field != NULL_TREE);
9669 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
9672 // Return a tree for the first argument to pass to the interface
9676 Interface_field_reference_expression::get_underlying_object_tree(
9680 if (this->expr_->type()->points_to() != NULL)
9681 expr = build_fold_indirect_ref(expr);
9683 tree expr_type = TREE_TYPE(expr);
9684 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
9686 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
9687 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
9689 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
9695 Interface_field_reference_expression::do_traverse(Traverse* traverse)
9697 return Expression::traverse(&this->expr_, traverse);
9700 // Return the type of an interface field reference.
9703 Interface_field_reference_expression::do_type()
9705 Type* expr_type = this->expr_->type();
9707 Type* points_to = expr_type->points_to();
9708 if (points_to != NULL)
9709 expr_type = points_to;
9711 Interface_type* interface_type = expr_type->interface_type();
9712 if (interface_type == NULL)
9713 return Type::make_error_type();
9715 const Typed_identifier* method = interface_type->find_method(this->name_);
9717 return Type::make_error_type();
9719 return method->type();
9725 Interface_field_reference_expression::do_determine_type(const Type_context*)
9727 this->expr_->determine_type_no_context();
9730 // Check the types for an interface field reference.
9733 Interface_field_reference_expression::do_check_types(Gogo*)
9735 Type* type = this->expr_->type();
9737 Type* points_to = type->points_to();
9738 if (points_to != NULL)
9741 Interface_type* interface_type = type->interface_type();
9742 if (interface_type == NULL)
9743 this->report_error(_("expected interface or pointer to interface"));
9746 const Typed_identifier* method =
9747 interface_type->find_method(this->name_);
9750 error_at(this->location(), "method %qs not in interface",
9751 Gogo::message_name(this->name_).c_str());
9752 this->set_is_error();
9757 // Get a tree for a reference to a field in an interface. There is no
9758 // standard tree type representation for this: it's a function
9759 // attached to its first argument, like a Bound_method_expression.
9760 // The only places it may currently be used are in a Call_expression
9761 // or a Go_statement, which will take it apart directly. So this has
9762 // nothing to do at present.
9765 Interface_field_reference_expression::do_get_tree(Translate_context*)
9770 // Make a reference to a field in an interface.
9773 Expression::make_interface_field_reference(Expression* expr,
9774 const std::string& field,
9775 source_location location)
9777 return new Interface_field_reference_expression(expr, field, location);
9780 // A general selector. This is a Parser_expression for LEFT.NAME. It
9781 // is lowered after we know the type of the left hand side.
9783 class Selector_expression : public Parser_expression
9786 Selector_expression(Expression* left, const std::string& name,
9787 source_location location)
9788 : Parser_expression(EXPRESSION_SELECTOR, location),
9789 left_(left), name_(name)
9794 do_traverse(Traverse* traverse)
9795 { return Expression::traverse(&this->left_, traverse); }
9798 do_lower(Gogo*, Named_object*, int);
9803 return new Selector_expression(this->left_->copy(), this->name_,
9809 lower_method_expression(Gogo*);
9811 // The expression on the left hand side.
9813 // The name on the right hand side.
9817 // Lower a selector expression once we know the real type of the left
9821 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
9823 Expression* left = this->left_;
9824 if (left->is_type_expression())
9825 return this->lower_method_expression(gogo);
9826 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
9830 // Lower a method expression T.M or (*T).M. We turn this into a
9831 // function literal.
9834 Selector_expression::lower_method_expression(Gogo* gogo)
9836 source_location location = this->location();
9837 Type* type = this->left_->type();
9838 const std::string& name(this->name_);
9841 if (type->points_to() == NULL)
9846 type = type->points_to();
9848 Named_type* nt = type->named_type();
9852 ("method expression requires named type or "
9853 "pointer to named type"));
9854 return Expression::make_error(location);
9858 Method* method = nt->method_function(name, &is_ambiguous);
9862 error_at(location, "type %<%s%> has no method %<%s%>",
9863 nt->message_name().c_str(),
9864 Gogo::message_name(name).c_str());
9866 error_at(location, "method %<%s%> is ambiguous in type %<%s%>",
9867 Gogo::message_name(name).c_str(),
9868 nt->message_name().c_str());
9869 return Expression::make_error(location);
9872 if (!is_pointer && !method->is_value_method())
9874 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
9875 nt->message_name().c_str(),
9876 Gogo::message_name(name).c_str());
9877 return Expression::make_error(location);
9880 // Build a new function type in which the receiver becomes the first
9882 Function_type* method_type = method->type();
9883 gcc_assert(method_type->is_method());
9885 const char* const receiver_name = "$this";
9886 Typed_identifier_list* parameters = new Typed_identifier_list();
9887 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
9890 const Typed_identifier_list* method_parameters = method_type->parameters();
9891 if (method_parameters != NULL)
9893 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
9894 p != method_parameters->end();
9896 parameters->push_back(*p);
9899 const Typed_identifier_list* method_results = method_type->results();
9900 Typed_identifier_list* results;
9901 if (method_results == NULL)
9905 results = new Typed_identifier_list();
9906 for (Typed_identifier_list::const_iterator p = method_results->begin();
9907 p != method_results->end();
9909 results->push_back(*p);
9912 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
9914 if (method_type->is_varargs())
9915 fntype->set_is_varargs();
9917 // We generate methods which always takes a pointer to the receiver
9918 // as their first argument. If this is for a pointer type, we can
9919 // simply reuse the existing function. We use an internal hack to
9920 // get the right type.
9924 Named_object* mno = (method->needs_stub_method()
9925 ? method->stub_object()
9926 : method->named_object());
9927 Expression* f = Expression::make_func_reference(mno, NULL, location);
9928 f = Expression::make_cast(fntype, f, location);
9929 Type_conversion_expression* tce =
9930 static_cast<Type_conversion_expression*>(f);
9931 tce->set_may_convert_function_types();
9935 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
9938 Named_object* vno = gogo->lookup(receiver_name, NULL);
9939 gcc_assert(vno != NULL);
9940 Expression* ve = Expression::make_var_reference(vno, location);
9941 Expression* bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
9942 gcc_assert(bm != NULL && !bm->is_error_expression());
9944 Expression_list* args;
9945 if (method_parameters == NULL)
9949 args = new Expression_list();
9950 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
9951 p != method_parameters->end();
9954 vno = gogo->lookup(p->name(), NULL);
9955 gcc_assert(vno != NULL);
9956 args->push_back(Expression::make_var_reference(vno, location));
9960 Call_expression* call = Expression::make_call(bm, args,
9961 method_type->is_varargs(),
9964 size_t count = call->result_count();
9967 s = Statement::make_statement(call);
9970 Expression_list* retvals = new Expression_list();
9972 retvals->push_back(call);
9975 for (size_t i = 0; i < count; ++i)
9976 retvals->push_back(Expression::make_call_result(call, i));
9978 s = Statement::make_return_statement(no->func_value()->type()->results(),
9981 gogo->add_statement(s);
9983 gogo->finish_function(location);
9985 return Expression::make_func_reference(no, NULL, location);
9988 // Make a selector expression.
9991 Expression::make_selector(Expression* left, const std::string& name,
9992 source_location location)
9994 return new Selector_expression(left, name, location);
9997 // Implement the builtin function new.
9999 class Allocation_expression : public Expression
10002 Allocation_expression(Type* type, source_location location)
10003 : Expression(EXPRESSION_ALLOCATION, location),
10009 do_traverse(Traverse* traverse)
10010 { return Type::traverse(this->type_, traverse); }
10014 { return Type::make_pointer_type(this->type_); }
10017 do_determine_type(const Type_context*)
10021 do_check_types(Gogo*);
10025 { return new Allocation_expression(this->type_, this->location()); }
10028 do_get_tree(Translate_context*);
10031 // The type we are allocating.
10035 // Check the type of an allocation expression.
10038 Allocation_expression::do_check_types(Gogo*)
10040 if (this->type_->function_type() != NULL)
10041 this->report_error(_("invalid new of function type"));
10044 // Return a tree for an allocation expression.
10047 Allocation_expression::do_get_tree(Translate_context* context)
10049 tree type_tree = this->type_->get_tree(context->gogo());
10050 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10051 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10053 return fold_convert(build_pointer_type(type_tree), space);
10056 // Make an allocation expression.
10059 Expression::make_allocation(Type* type, source_location location)
10061 return new Allocation_expression(type, location);
10064 // Implement the builtin function make.
10066 class Make_expression : public Expression
10069 Make_expression(Type* type, Expression_list* args, source_location location)
10070 : Expression(EXPRESSION_MAKE, location),
10071 type_(type), args_(args)
10076 do_traverse(Traverse* traverse);
10080 { return this->type_; }
10083 do_determine_type(const Type_context*);
10086 do_check_types(Gogo*);
10091 return new Make_expression(this->type_, this->args_->copy(),
10096 do_get_tree(Translate_context*);
10099 // The type we are making.
10101 // The arguments to pass to the make routine.
10102 Expression_list* args_;
10108 Make_expression::do_traverse(Traverse* traverse)
10110 if (this->args_ != NULL
10111 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10112 return TRAVERSE_EXIT;
10113 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10114 return TRAVERSE_EXIT;
10115 return TRAVERSE_CONTINUE;
10118 // Set types of arguments.
10121 Make_expression::do_determine_type(const Type_context*)
10123 if (this->args_ != NULL)
10125 Type_context context(Type::lookup_integer_type("int"), false);
10126 for (Expression_list::const_iterator pe = this->args_->begin();
10127 pe != this->args_->end();
10129 (*pe)->determine_type(&context);
10133 // Check types for a make expression.
10136 Make_expression::do_check_types(Gogo*)
10138 if (this->type_->channel_type() == NULL
10139 && this->type_->map_type() == NULL
10140 && (this->type_->array_type() == NULL
10141 || this->type_->array_type()->length() != NULL))
10142 this->report_error(_("invalid type for make function"));
10143 else if (!this->type_->check_make_expression(this->args_, this->location()))
10144 this->set_is_error();
10147 // Return a tree for a make expression.
10150 Make_expression::do_get_tree(Translate_context* context)
10152 return this->type_->make_expression_tree(context, this->args_,
10156 // Make a make expression.
10159 Expression::make_make(Type* type, Expression_list* args,
10160 source_location location)
10162 return new Make_expression(type, args, location);
10165 // Construct a struct.
10167 class Struct_construction_expression : public Expression
10170 Struct_construction_expression(Type* type, Expression_list* vals,
10171 source_location location)
10172 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10173 type_(type), vals_(vals)
10176 // Return whether this is a constant initializer.
10178 is_constant_struct() const;
10182 do_traverse(Traverse* traverse);
10186 { return this->type_; }
10189 do_determine_type(const Type_context*);
10192 do_check_types(Gogo*);
10197 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10202 do_is_addressable() const
10206 do_get_tree(Translate_context*);
10209 do_export(Export*) const;
10212 // The type of the struct to construct.
10214 // The list of values, in order of the fields in the struct. A NULL
10215 // entry means that the field should be zero-initialized.
10216 Expression_list* vals_;
10222 Struct_construction_expression::do_traverse(Traverse* traverse)
10224 if (this->vals_ != NULL
10225 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10226 return TRAVERSE_EXIT;
10227 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10228 return TRAVERSE_EXIT;
10229 return TRAVERSE_CONTINUE;
10232 // Return whether this is a constant initializer.
10235 Struct_construction_expression::is_constant_struct() const
10237 if (this->vals_ == NULL)
10239 for (Expression_list::const_iterator pv = this->vals_->begin();
10240 pv != this->vals_->end();
10244 && !(*pv)->is_constant()
10245 && (!(*pv)->is_composite_literal()
10246 || (*pv)->is_nonconstant_composite_literal()))
10250 const Struct_field_list* fields = this->type_->struct_type()->fields();
10251 for (Struct_field_list::const_iterator pf = fields->begin();
10252 pf != fields->end();
10255 // There are no constant constructors for interfaces.
10256 if (pf->type()->interface_type() != NULL)
10263 // Final type determination.
10266 Struct_construction_expression::do_determine_type(const Type_context*)
10268 if (this->vals_ == NULL)
10270 const Struct_field_list* fields = this->type_->struct_type()->fields();
10271 Expression_list::const_iterator pv = this->vals_->begin();
10272 for (Struct_field_list::const_iterator pf = fields->begin();
10273 pf != fields->end();
10276 if (pv == this->vals_->end())
10280 Type_context subcontext(pf->type(), false);
10281 (*pv)->determine_type(&subcontext);
10289 Struct_construction_expression::do_check_types(Gogo*)
10291 if (this->vals_ == NULL)
10294 Struct_type* st = this->type_->struct_type();
10295 if (this->vals_->size() > st->field_count())
10297 this->report_error(_("too many expressions for struct"));
10301 const Struct_field_list* fields = st->fields();
10302 Expression_list::const_iterator pv = this->vals_->begin();
10304 for (Struct_field_list::const_iterator pf = fields->begin();
10305 pf != fields->end();
10308 if (pv == this->vals_->end())
10310 this->report_error(_("too few expressions for struct"));
10317 std::string reason;
10318 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10320 if (reason.empty())
10321 error_at((*pv)->location(),
10322 "incompatible type for field %d in struct construction",
10325 error_at((*pv)->location(),
10326 ("incompatible type for field %d in "
10327 "struct construction (%s)"),
10328 i + 1, reason.c_str());
10329 this->set_is_error();
10332 gcc_assert(pv == this->vals_->end());
10335 // Return a tree for constructing a struct.
10338 Struct_construction_expression::do_get_tree(Translate_context* context)
10340 Gogo* gogo = context->gogo();
10342 if (this->vals_ == NULL)
10343 return this->type_->get_init_tree(gogo, false);
10345 tree type_tree = this->type_->get_tree(gogo);
10346 if (type_tree == error_mark_node)
10347 return error_mark_node;
10348 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10350 bool is_constant = true;
10351 const Struct_field_list* fields = this->type_->struct_type()->fields();
10352 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10354 Struct_field_list::const_iterator pf = fields->begin();
10355 Expression_list::const_iterator pv = this->vals_->begin();
10356 for (tree field = TYPE_FIELDS(type_tree);
10357 field != NULL_TREE;
10358 field = DECL_CHAIN(field), ++pf)
10360 gcc_assert(pf != fields->end());
10363 if (pv == this->vals_->end())
10364 val = pf->type()->get_init_tree(gogo, false);
10365 else if (*pv == NULL)
10367 val = pf->type()->get_init_tree(gogo, false);
10372 val = Expression::convert_for_assignment(context, pf->type(),
10374 (*pv)->get_tree(context),
10379 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10380 return error_mark_node;
10382 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10383 elt->index = field;
10385 if (!TREE_CONSTANT(val))
10386 is_constant = false;
10388 gcc_assert(pf == fields->end());
10390 tree ret = build_constructor(type_tree, elts);
10392 TREE_CONSTANT(ret) = 1;
10396 // Export a struct construction.
10399 Struct_construction_expression::do_export(Export* exp) const
10401 exp->write_c_string("convert(");
10402 exp->write_type(this->type_);
10403 for (Expression_list::const_iterator pv = this->vals_->begin();
10404 pv != this->vals_->end();
10407 exp->write_c_string(", ");
10409 (*pv)->export_expression(exp);
10411 exp->write_c_string(")");
10414 // Make a struct composite literal. This used by the thunk code.
10417 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
10418 source_location location)
10420 gcc_assert(type->struct_type() != NULL);
10421 return new Struct_construction_expression(type, vals, location);
10424 // Construct an array. This class is not used directly; instead we
10425 // use the child classes, Fixed_array_construction_expression and
10426 // Open_array_construction_expression.
10428 class Array_construction_expression : public Expression
10431 Array_construction_expression(Expression_classification classification,
10432 Type* type, Expression_list* vals,
10433 source_location location)
10434 : Expression(classification, location),
10435 type_(type), vals_(vals)
10439 // Return whether this is a constant initializer.
10441 is_constant_array() const;
10443 // Return the number of elements.
10445 element_count() const
10446 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
10450 do_traverse(Traverse* traverse);
10454 { return this->type_; }
10457 do_determine_type(const Type_context*);
10460 do_check_types(Gogo*);
10463 do_is_addressable() const
10467 do_export(Export*) const;
10469 // The list of values.
10472 { return this->vals_; }
10474 // Get a constructor tree for the array values.
10476 get_constructor_tree(Translate_context* context, tree type_tree);
10479 // The type of the array to construct.
10481 // The list of values.
10482 Expression_list* vals_;
10488 Array_construction_expression::do_traverse(Traverse* traverse)
10490 if (this->vals_ != NULL
10491 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10492 return TRAVERSE_EXIT;
10493 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10494 return TRAVERSE_EXIT;
10495 return TRAVERSE_CONTINUE;
10498 // Return whether this is a constant initializer.
10501 Array_construction_expression::is_constant_array() const
10503 if (this->vals_ == NULL)
10506 // There are no constant constructors for interfaces.
10507 if (this->type_->array_type()->element_type()->interface_type() != NULL)
10510 for (Expression_list::const_iterator pv = this->vals_->begin();
10511 pv != this->vals_->end();
10515 && !(*pv)->is_constant()
10516 && (!(*pv)->is_composite_literal()
10517 || (*pv)->is_nonconstant_composite_literal()))
10523 // Final type determination.
10526 Array_construction_expression::do_determine_type(const Type_context*)
10528 if (this->vals_ == NULL)
10530 Type_context subcontext(this->type_->array_type()->element_type(), false);
10531 for (Expression_list::const_iterator pv = this->vals_->begin();
10532 pv != this->vals_->end();
10536 (*pv)->determine_type(&subcontext);
10543 Array_construction_expression::do_check_types(Gogo*)
10545 if (this->vals_ == NULL)
10548 Array_type* at = this->type_->array_type();
10550 Type* element_type = at->element_type();
10551 for (Expression_list::const_iterator pv = this->vals_->begin();
10552 pv != this->vals_->end();
10556 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
10558 error_at((*pv)->location(),
10559 "incompatible type for element %d in composite literal",
10561 this->set_is_error();
10565 Expression* length = at->length();
10566 if (length != NULL)
10571 if (at->length()->integer_constant_value(true, val, &type))
10573 if (this->vals_->size() > mpz_get_ui(val))
10574 this->report_error(_("too many elements in composite literal"));
10580 // Get a constructor tree for the array values.
10583 Array_construction_expression::get_constructor_tree(Translate_context* context,
10586 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
10587 (this->vals_ == NULL
10589 : this->vals_->size()));
10590 Type* element_type = this->type_->array_type()->element_type();
10591 bool is_constant = true;
10592 if (this->vals_ != NULL)
10595 for (Expression_list::const_iterator pv = this->vals_->begin();
10596 pv != this->vals_->end();
10599 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
10600 elt->index = size_int(i);
10602 elt->value = element_type->get_init_tree(context->gogo(), false);
10605 tree value_tree = (*pv)->get_tree(context);
10606 elt->value = Expression::convert_for_assignment(context,
10612 if (elt->value == error_mark_node)
10613 return error_mark_node;
10614 if (!TREE_CONSTANT(elt->value))
10615 is_constant = false;
10619 tree ret = build_constructor(type_tree, values);
10621 TREE_CONSTANT(ret) = 1;
10625 // Export an array construction.
10628 Array_construction_expression::do_export(Export* exp) const
10630 exp->write_c_string("convert(");
10631 exp->write_type(this->type_);
10632 if (this->vals_ != NULL)
10634 for (Expression_list::const_iterator pv = this->vals_->begin();
10635 pv != this->vals_->end();
10638 exp->write_c_string(", ");
10640 (*pv)->export_expression(exp);
10643 exp->write_c_string(")");
10646 // Construct a fixed array.
10648 class Fixed_array_construction_expression :
10649 public Array_construction_expression
10652 Fixed_array_construction_expression(Type* type, Expression_list* vals,
10653 source_location location)
10654 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
10655 type, vals, location)
10657 gcc_assert(type->array_type() != NULL
10658 && type->array_type()->length() != NULL);
10665 return new Fixed_array_construction_expression(this->type(),
10666 (this->vals() == NULL
10668 : this->vals()->copy()),
10673 do_get_tree(Translate_context*);
10676 // Return a tree for constructing a fixed array.
10679 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
10681 return this->get_constructor_tree(context,
10682 this->type()->get_tree(context->gogo()));
10685 // Construct an open array.
10687 class Open_array_construction_expression : public Array_construction_expression
10690 Open_array_construction_expression(Type* type, Expression_list* vals,
10691 source_location location)
10692 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
10693 type, vals, location)
10695 gcc_assert(type->array_type() != NULL
10696 && type->array_type()->length() == NULL);
10700 // Note that taking the address of an open array literal is invalid.
10705 return new Open_array_construction_expression(this->type(),
10706 (this->vals() == NULL
10708 : this->vals()->copy()),
10713 do_get_tree(Translate_context*);
10716 // Return a tree for constructing an open array.
10719 Open_array_construction_expression::do_get_tree(Translate_context* context)
10721 Type* element_type = this->type()->array_type()->element_type();
10722 tree element_type_tree = element_type->get_tree(context->gogo());
10725 if (this->vals() == NULL || this->vals()->empty())
10727 // We need to create a unique value.
10728 tree max = size_int(0);
10729 tree constructor_type = build_array_type(element_type_tree,
10730 build_index_type(max));
10731 if (constructor_type == error_mark_node)
10732 return error_mark_node;
10733 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
10734 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
10735 elt->index = size_int(0);
10736 elt->value = element_type->get_init_tree(context->gogo(), false);
10737 values = build_constructor(constructor_type, vec);
10738 if (TREE_CONSTANT(elt->value))
10739 TREE_CONSTANT(values) = 1;
10740 length_tree = size_int(0);
10744 tree max = size_int(this->vals()->size() - 1);
10745 tree constructor_type = build_array_type(element_type_tree,
10746 build_index_type(max));
10747 if (constructor_type == error_mark_node)
10748 return error_mark_node;
10749 values = this->get_constructor_tree(context, constructor_type);
10750 length_tree = size_int(this->vals()->size());
10753 if (values == error_mark_node)
10754 return error_mark_node;
10756 bool is_constant_initializer = TREE_CONSTANT(values);
10757 bool is_in_function = context->function() != NULL;
10759 if (is_constant_initializer)
10761 tree tmp = build_decl(this->location(), VAR_DECL,
10762 create_tmp_var_name("C"), TREE_TYPE(values));
10763 DECL_EXTERNAL(tmp) = 0;
10764 TREE_PUBLIC(tmp) = 0;
10765 TREE_STATIC(tmp) = 1;
10766 DECL_ARTIFICIAL(tmp) = 1;
10767 if (is_in_function)
10769 // If this is not a function, we will only initialize the
10770 // value once, so we can use this directly rather than
10771 // copying it. In that case we can't make it read-only,
10772 // because the program is permitted to change it.
10773 TREE_READONLY(tmp) = 1;
10774 TREE_CONSTANT(tmp) = 1;
10776 DECL_INITIAL(tmp) = values;
10777 rest_of_decl_compilation(tmp, 1, 0);
10783 if (!is_in_function && is_constant_initializer)
10785 // Outside of a function, we know the initializer will only run
10787 space = build_fold_addr_expr(values);
10792 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
10793 space = context->gogo()->allocate_memory(element_type, memsize,
10795 space = save_expr(space);
10797 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
10798 tree ref = build_fold_indirect_ref_loc(this->location(), s);
10799 TREE_THIS_NOTRAP(ref) = 1;
10800 set = build2(MODIFY_EXPR, void_type_node, ref, values);
10803 // Build a constructor for the open array.
10805 tree type_tree = this->type()->get_tree(context->gogo());
10806 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10808 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
10810 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
10811 tree field = TYPE_FIELDS(type_tree);
10812 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
10813 elt->index = field;
10814 elt->value = fold_convert(TREE_TYPE(field), space);
10816 elt = VEC_quick_push(constructor_elt, init, NULL);
10817 field = DECL_CHAIN(field);
10818 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
10819 elt->index = field;
10820 elt->value = fold_convert(TREE_TYPE(field), length_tree);
10822 elt = VEC_quick_push(constructor_elt, init, NULL);
10823 field = DECL_CHAIN(field);
10824 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
10825 elt->index = field;
10826 elt->value = fold_convert(TREE_TYPE(field), length_tree);
10828 tree constructor = build_constructor(type_tree, init);
10829 if (!is_in_function && is_constant_initializer)
10830 TREE_CONSTANT(constructor) = 1;
10832 if (set == NULL_TREE)
10833 return constructor;
10835 return build2(COMPOUND_EXPR, type_tree, set, constructor);
10838 // Make a slice composite literal. This is used by the type
10839 // descriptor code.
10842 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
10843 source_location location)
10845 gcc_assert(type->is_open_array_type());
10846 return new Open_array_construction_expression(type, vals, location);
10849 // Construct a map.
10851 class Map_construction_expression : public Expression
10854 Map_construction_expression(Type* type, Expression_list* vals,
10855 source_location location)
10856 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
10857 type_(type), vals_(vals)
10858 { gcc_assert(vals == NULL || vals->size() % 2 == 0); }
10862 do_traverse(Traverse* traverse);
10866 { return this->type_; }
10869 do_determine_type(const Type_context*);
10872 do_check_types(Gogo*);
10877 return new Map_construction_expression(this->type_, this->vals_->copy(),
10882 do_get_tree(Translate_context*);
10885 do_export(Export*) const;
10888 // The type of the map to construct.
10890 // The list of values.
10891 Expression_list* vals_;
10897 Map_construction_expression::do_traverse(Traverse* traverse)
10899 if (this->vals_ != NULL
10900 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10901 return TRAVERSE_EXIT;
10902 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10903 return TRAVERSE_EXIT;
10904 return TRAVERSE_CONTINUE;
10907 // Final type determination.
10910 Map_construction_expression::do_determine_type(const Type_context*)
10912 if (this->vals_ == NULL)
10915 Map_type* mt = this->type_->map_type();
10916 Type_context key_context(mt->key_type(), false);
10917 Type_context val_context(mt->val_type(), false);
10918 for (Expression_list::const_iterator pv = this->vals_->begin();
10919 pv != this->vals_->end();
10922 (*pv)->determine_type(&key_context);
10924 (*pv)->determine_type(&val_context);
10931 Map_construction_expression::do_check_types(Gogo*)
10933 if (this->vals_ == NULL)
10936 Map_type* mt = this->type_->map_type();
10938 Type* key_type = mt->key_type();
10939 Type* val_type = mt->val_type();
10940 for (Expression_list::const_iterator pv = this->vals_->begin();
10941 pv != this->vals_->end();
10944 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
10946 error_at((*pv)->location(),
10947 "incompatible type for element %d key in map construction",
10949 this->set_is_error();
10952 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
10954 error_at((*pv)->location(),
10955 ("incompatible type for element %d value "
10956 "in map construction"),
10958 this->set_is_error();
10963 // Return a tree for constructing a map.
10966 Map_construction_expression::do_get_tree(Translate_context* context)
10968 Gogo* gogo = context->gogo();
10969 source_location loc = this->location();
10971 Map_type* mt = this->type_->map_type();
10973 // Build a struct to hold the key and value.
10974 tree struct_type = make_node(RECORD_TYPE);
10976 Type* key_type = mt->key_type();
10977 tree id = get_identifier("__key");
10978 tree key_field = build_decl(loc, FIELD_DECL, id, key_type->get_tree(gogo));
10979 DECL_CONTEXT(key_field) = struct_type;
10980 TYPE_FIELDS(struct_type) = key_field;
10982 Type* val_type = mt->val_type();
10983 id = get_identifier("__val");
10984 tree val_field = build_decl(loc, FIELD_DECL, id, val_type->get_tree(gogo));
10985 DECL_CONTEXT(val_field) = struct_type;
10986 DECL_CHAIN(key_field) = val_field;
10988 layout_type(struct_type);
10990 bool is_constant = true;
10995 if (this->vals_ == NULL || this->vals_->empty())
10997 valaddr = null_pointer_node;
10998 make_tmp = NULL_TREE;
11002 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11003 this->vals_->size() / 2);
11005 for (Expression_list::const_iterator pv = this->vals_->begin();
11006 pv != this->vals_->end();
11009 bool one_is_constant = true;
11011 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
11013 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11014 elt->index = key_field;
11015 tree val_tree = (*pv)->get_tree(context);
11016 elt->value = Expression::convert_for_assignment(context, key_type,
11019 if (elt->value == error_mark_node)
11020 return error_mark_node;
11021 if (!TREE_CONSTANT(elt->value))
11022 one_is_constant = false;
11026 elt = VEC_quick_push(constructor_elt, one, NULL);
11027 elt->index = val_field;
11028 val_tree = (*pv)->get_tree(context);
11029 elt->value = Expression::convert_for_assignment(context, val_type,
11032 if (elt->value == error_mark_node)
11033 return error_mark_node;
11034 if (!TREE_CONSTANT(elt->value))
11035 one_is_constant = false;
11037 elt = VEC_quick_push(constructor_elt, values, NULL);
11038 elt->index = size_int(i);
11039 elt->value = build_constructor(struct_type, one);
11040 if (one_is_constant)
11041 TREE_CONSTANT(elt->value) = 1;
11043 is_constant = false;
11046 tree index_type = build_index_type(size_int(i - 1));
11047 tree array_type = build_array_type(struct_type, index_type);
11048 tree init = build_constructor(array_type, values);
11050 TREE_CONSTANT(init) = 1;
11052 if (current_function_decl != NULL)
11054 tmp = create_tmp_var(array_type, get_name(array_type));
11055 DECL_INITIAL(tmp) = init;
11056 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11057 TREE_ADDRESSABLE(tmp) = 1;
11061 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11062 DECL_EXTERNAL(tmp) = 0;
11063 TREE_PUBLIC(tmp) = 0;
11064 TREE_STATIC(tmp) = 1;
11065 DECL_ARTIFICIAL(tmp) = 1;
11066 if (!TREE_CONSTANT(init))
11067 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11071 TREE_READONLY(tmp) = 1;
11072 TREE_CONSTANT(tmp) = 1;
11073 DECL_INITIAL(tmp) = init;
11074 make_tmp = NULL_TREE;
11076 rest_of_decl_compilation(tmp, 1, 0);
11079 valaddr = build_fold_addr_expr(tmp);
11082 tree descriptor = gogo->map_descriptor(mt);
11084 tree type_tree = this->type_->get_tree(gogo);
11086 static tree construct_map_fndecl;
11087 tree call = Gogo::call_builtin(&construct_map_fndecl,
11089 "__go_construct_map",
11092 TREE_TYPE(descriptor),
11097 TYPE_SIZE_UNIT(struct_type),
11099 byte_position(val_field),
11101 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11102 const_ptr_type_node,
11103 fold_convert(const_ptr_type_node, valaddr));
11106 if (make_tmp == NULL)
11109 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11113 // Export an array construction.
11116 Map_construction_expression::do_export(Export* exp) const
11118 exp->write_c_string("convert(");
11119 exp->write_type(this->type_);
11120 for (Expression_list::const_iterator pv = this->vals_->begin();
11121 pv != this->vals_->end();
11124 exp->write_c_string(", ");
11125 (*pv)->export_expression(exp);
11127 exp->write_c_string(")");
11130 // A general composite literal. This is lowered to a type specific
11133 class Composite_literal_expression : public Parser_expression
11136 Composite_literal_expression(Type* type, int depth, bool has_keys,
11137 Expression_list* vals, source_location location)
11138 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11139 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11144 do_traverse(Traverse* traverse);
11147 do_lower(Gogo*, Named_object*, int);
11152 return new Composite_literal_expression(this->type_, this->depth_,
11154 (this->vals_ == NULL
11156 : this->vals_->copy()),
11162 lower_struct(Type*);
11165 lower_array(Type*);
11168 make_array(Type*, Expression_list*);
11173 // The type of the composite literal.
11175 // The depth within a list of composite literals within a composite
11176 // literal, when the type is omitted.
11178 // The values to put in the composite literal.
11179 Expression_list* vals_;
11180 // If this is true, then VALS_ is a list of pairs: a key and a
11181 // value. In an array initializer, a missing key will be NULL.
11188 Composite_literal_expression::do_traverse(Traverse* traverse)
11190 if (this->vals_ != NULL
11191 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11192 return TRAVERSE_EXIT;
11193 return Type::traverse(this->type_, traverse);
11196 // Lower a generic composite literal into a specific version based on
11200 Composite_literal_expression::do_lower(Gogo*, Named_object*, int)
11202 Type* type = this->type_;
11204 for (int depth = this->depth_; depth > 0; --depth)
11206 if (type->array_type() != NULL)
11207 type = type->array_type()->element_type();
11208 else if (type->map_type() != NULL)
11209 type = type->map_type()->val_type();
11212 if (!type->is_error_type())
11213 error_at(this->location(),
11214 ("may only omit types within composite literals "
11215 "of slice, array, or map type"));
11216 return Expression::make_error(this->location());
11220 if (type->is_error_type())
11221 return Expression::make_error(this->location());
11222 else if (type->struct_type() != NULL)
11223 return this->lower_struct(type);
11224 else if (type->array_type() != NULL)
11225 return this->lower_array(type);
11226 else if (type->map_type() != NULL)
11227 return this->lower_map(type);
11230 error_at(this->location(),
11231 ("expected struct, slice, array, or map type "
11232 "for composite literal"));
11233 return Expression::make_error(this->location());
11237 // Lower a struct composite literal.
11240 Composite_literal_expression::lower_struct(Type* type)
11242 source_location location = this->location();
11243 Struct_type* st = type->struct_type();
11244 if (this->vals_ == NULL || !this->has_keys_)
11245 return new Struct_construction_expression(type, this->vals_, location);
11247 size_t field_count = st->field_count();
11248 std::vector<Expression*> vals(field_count);
11249 Expression_list::const_iterator p = this->vals_->begin();
11250 while (p != this->vals_->end())
11252 Expression* name_expr = *p;
11255 gcc_assert(p != this->vals_->end());
11256 Expression* val = *p;
11260 if (name_expr == NULL)
11262 error_at(val->location(), "mixture of field and value initializers");
11263 return Expression::make_error(location);
11266 bool bad_key = false;
11268 switch (name_expr->classification())
11270 case EXPRESSION_UNKNOWN_REFERENCE:
11271 name = name_expr->unknown_expression()->name();
11274 case EXPRESSION_CONST_REFERENCE:
11275 name = static_cast<Const_expression*>(name_expr)->name();
11278 case EXPRESSION_TYPE:
11280 Type* t = name_expr->type();
11281 Named_type* nt = t->named_type();
11289 case EXPRESSION_VAR_REFERENCE:
11290 name = name_expr->var_expression()->name();
11293 case EXPRESSION_FUNC_REFERENCE:
11294 name = name_expr->func_expression()->name();
11297 case EXPRESSION_UNARY:
11298 // If there is a local variable around with the same name as
11299 // the field, and this occurs in the closure, then the
11300 // parser may turn the field reference into an indirection
11301 // through the closure. FIXME: This is a mess.
11304 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11305 if (ue->op() == OPERATOR_MULT)
11307 Field_reference_expression* fre =
11308 ue->operand()->field_reference_expression();
11312 fre->expr()->type()->deref()->struct_type();
11315 const Struct_field* sf = st->field(fre->field_index());
11316 name = sf->field_name();
11318 snprintf(buf, sizeof buf, "%u", fre->field_index());
11319 size_t buflen = strlen(buf);
11320 if (name.compare(name.length() - buflen, buflen, buf)
11323 name = name.substr(0, name.length() - buflen);
11338 error_at(name_expr->location(), "expected struct field name");
11339 return Expression::make_error(location);
11342 unsigned int index;
11343 const Struct_field* sf = st->find_local_field(name, &index);
11346 error_at(name_expr->location(), "unknown field %qs in %qs",
11347 Gogo::message_name(name).c_str(),
11348 (type->named_type() != NULL
11349 ? type->named_type()->message_name().c_str()
11350 : "unnamed struct"));
11351 return Expression::make_error(location);
11353 if (vals[index] != NULL)
11355 error_at(name_expr->location(),
11356 "duplicate value for field %qs in %qs",
11357 Gogo::message_name(name).c_str(),
11358 (type->named_type() != NULL
11359 ? type->named_type()->message_name().c_str()
11360 : "unnamed struct"));
11361 return Expression::make_error(location);
11367 Expression_list* list = new Expression_list;
11368 list->reserve(field_count);
11369 for (size_t i = 0; i < field_count; ++i)
11370 list->push_back(vals[i]);
11372 return new Struct_construction_expression(type, list, location);
11375 // Lower an array composite literal.
11378 Composite_literal_expression::lower_array(Type* type)
11380 source_location location = this->location();
11381 if (this->vals_ == NULL || !this->has_keys_)
11382 return this->make_array(type, this->vals_);
11384 std::vector<Expression*> vals;
11385 vals.reserve(this->vals_->size());
11386 unsigned long index = 0;
11387 Expression_list::const_iterator p = this->vals_->begin();
11388 while (p != this->vals_->end())
11390 Expression* index_expr = *p;
11393 gcc_assert(p != this->vals_->end());
11394 Expression* val = *p;
11398 if (index_expr != NULL)
11403 if (!index_expr->integer_constant_value(true, ival, &dummy))
11406 error_at(index_expr->location(),
11407 "index expression is not integer constant");
11408 return Expression::make_error(location);
11410 if (mpz_sgn(ival) < 0)
11413 error_at(index_expr->location(), "index expression is negative");
11414 return Expression::make_error(location);
11416 index = mpz_get_ui(ival);
11417 if (mpz_cmp_ui(ival, index) != 0)
11420 error_at(index_expr->location(), "index value overflow");
11421 return Expression::make_error(location);
11426 if (index == vals.size())
11427 vals.push_back(val);
11430 if (index > vals.size())
11432 vals.reserve(index + 32);
11433 vals.resize(index + 1, static_cast<Expression*>(NULL));
11435 if (vals[index] != NULL)
11437 error_at((index_expr != NULL
11438 ? index_expr->location()
11439 : val->location()),
11440 "duplicate value for index %lu",
11442 return Expression::make_error(location);
11450 size_t size = vals.size();
11451 Expression_list* list = new Expression_list;
11452 list->reserve(size);
11453 for (size_t i = 0; i < size; ++i)
11454 list->push_back(vals[i]);
11456 return this->make_array(type, list);
11459 // Actually build the array composite literal. This handles
11463 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
11465 source_location location = this->location();
11466 Array_type* at = type->array_type();
11467 if (at->length() != NULL && at->length()->is_nil_expression())
11469 size_t size = vals == NULL ? 0 : vals->size();
11471 mpz_init_set_ui(vlen, size);
11472 Expression* elen = Expression::make_integer(&vlen, NULL, location);
11474 at = Type::make_array_type(at->element_type(), elen);
11477 if (at->length() != NULL)
11478 return new Fixed_array_construction_expression(type, vals, location);
11480 return new Open_array_construction_expression(type, vals, location);
11483 // Lower a map composite literal.
11486 Composite_literal_expression::lower_map(Type* type)
11488 source_location location = this->location();
11489 if (this->vals_ != NULL)
11491 if (!this->has_keys_)
11493 error_at(location, "map composite literal must have keys");
11494 return Expression::make_error(location);
11497 for (Expression_list::const_iterator p = this->vals_->begin();
11498 p != this->vals_->end();
11504 error_at((*p)->location(),
11505 "map composite literal must have keys for every value");
11506 return Expression::make_error(location);
11511 return new Map_construction_expression(type, this->vals_, location);
11514 // Make a composite literal expression.
11517 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
11518 Expression_list* vals,
11519 source_location location)
11521 return new Composite_literal_expression(type, depth, has_keys, vals,
11525 // Return whether this expression is a composite literal.
11528 Expression::is_composite_literal() const
11530 switch (this->classification_)
11532 case EXPRESSION_COMPOSITE_LITERAL:
11533 case EXPRESSION_STRUCT_CONSTRUCTION:
11534 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
11535 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
11536 case EXPRESSION_MAP_CONSTRUCTION:
11543 // Return whether this expression is a composite literal which is not
11547 Expression::is_nonconstant_composite_literal() const
11549 switch (this->classification_)
11551 case EXPRESSION_STRUCT_CONSTRUCTION:
11553 const Struct_construction_expression *psce =
11554 static_cast<const Struct_construction_expression*>(this);
11555 return !psce->is_constant_struct();
11557 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
11559 const Fixed_array_construction_expression *pace =
11560 static_cast<const Fixed_array_construction_expression*>(this);
11561 return !pace->is_constant_array();
11563 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
11565 const Open_array_construction_expression *pace =
11566 static_cast<const Open_array_construction_expression*>(this);
11567 return !pace->is_constant_array();
11569 case EXPRESSION_MAP_CONSTRUCTION:
11576 // Return true if this is a reference to a local variable.
11579 Expression::is_local_variable() const
11581 const Var_expression* ve = this->var_expression();
11584 const Named_object* no = ve->named_object();
11585 return (no->is_result_variable()
11586 || (no->is_variable() && !no->var_value()->is_global()));
11589 // Class Type_guard_expression.
11594 Type_guard_expression::do_traverse(Traverse* traverse)
11596 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
11597 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11598 return TRAVERSE_EXIT;
11599 return TRAVERSE_CONTINUE;
11602 // Check types of a type guard expression. The expression must have
11603 // an interface type, but the actual type conversion is checked at run
11607 Type_guard_expression::do_check_types(Gogo*)
11609 // 6g permits using a type guard with unsafe.pointer; we are
11611 Type* expr_type = this->expr_->type();
11612 if (expr_type->is_unsafe_pointer_type())
11614 if (this->type_->points_to() == NULL
11615 && (this->type_->integer_type() == NULL
11616 || (this->type_->forwarded()
11617 != Type::lookup_integer_type("uintptr"))))
11618 this->report_error(_("invalid unsafe.Pointer conversion"));
11620 else if (this->type_->is_unsafe_pointer_type())
11622 if (expr_type->points_to() == NULL
11623 && (expr_type->integer_type() == NULL
11624 || (expr_type->forwarded()
11625 != Type::lookup_integer_type("uintptr"))))
11626 this->report_error(_("invalid unsafe.Pointer conversion"));
11628 else if (expr_type->interface_type() == NULL)
11629 this->report_error(_("type assertion only valid for interface types"));
11630 else if (this->type_->interface_type() == NULL)
11632 std::string reason;
11633 if (!expr_type->interface_type()->implements_interface(this->type_,
11636 if (reason.empty())
11637 this->report_error(_("impossible type assertion: "
11638 "type does not implement interface"));
11641 error_at(this->location(),
11642 ("impossible type assertion: "
11643 "type does not implement interface (%s)"),
11645 this->set_is_error();
11651 // Return a tree for a type guard expression.
11654 Type_guard_expression::do_get_tree(Translate_context* context)
11656 Gogo* gogo = context->gogo();
11657 tree expr_tree = this->expr_->get_tree(context);
11658 if (expr_tree == error_mark_node)
11659 return error_mark_node;
11660 Type* expr_type = this->expr_->type();
11661 if ((this->type_->is_unsafe_pointer_type()
11662 && (expr_type->points_to() != NULL
11663 || expr_type->integer_type() != NULL))
11664 || (expr_type->is_unsafe_pointer_type()
11665 && this->type_->points_to() != NULL))
11666 return convert_to_pointer(this->type_->get_tree(gogo), expr_tree);
11667 else if (expr_type->is_unsafe_pointer_type()
11668 && this->type_->integer_type() != NULL)
11669 return convert_to_integer(this->type_->get_tree(gogo), expr_tree);
11670 else if (this->type_->interface_type() != NULL)
11671 return Expression::convert_interface_to_interface(context, this->type_,
11672 this->expr_->type(),
11676 return Expression::convert_for_assignment(context, this->type_,
11677 this->expr_->type(), expr_tree,
11681 // Make a type guard expression.
11684 Expression::make_type_guard(Expression* expr, Type* type,
11685 source_location location)
11687 return new Type_guard_expression(expr, type, location);
11690 // Class Heap_composite_expression.
11692 // When you take the address of a composite literal, it is allocated
11693 // on the heap. This class implements that.
11695 class Heap_composite_expression : public Expression
11698 Heap_composite_expression(Expression* expr, source_location location)
11699 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
11705 do_traverse(Traverse* traverse)
11706 { return Expression::traverse(&this->expr_, traverse); }
11710 { return Type::make_pointer_type(this->expr_->type()); }
11713 do_determine_type(const Type_context*)
11714 { this->expr_->determine_type_no_context(); }
11719 return Expression::make_heap_composite(this->expr_->copy(),
11724 do_get_tree(Translate_context*);
11726 // We only export global objects, and the parser does not generate
11727 // this in global scope.
11729 do_export(Export*) const
11730 { gcc_unreachable(); }
11733 // The composite literal which is being put on the heap.
11737 // Return a tree which allocates a composite literal on the heap.
11740 Heap_composite_expression::do_get_tree(Translate_context* context)
11742 tree expr_tree = this->expr_->get_tree(context);
11743 if (expr_tree == error_mark_node)
11744 return error_mark_node;
11745 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
11746 gcc_assert(TREE_CODE(expr_size) == INTEGER_CST);
11747 tree space = context->gogo()->allocate_memory(this->expr_->type(),
11748 expr_size, this->location());
11749 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
11750 space = save_expr(space);
11751 tree ref = build_fold_indirect_ref_loc(this->location(), space);
11752 TREE_THIS_NOTRAP(ref) = 1;
11753 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
11754 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
11756 SET_EXPR_LOCATION(ret, this->location());
11760 // Allocate a composite literal on the heap.
11763 Expression::make_heap_composite(Expression* expr, source_location location)
11765 return new Heap_composite_expression(expr, location);
11768 // Class Receive_expression.
11770 // Return the type of a receive expression.
11773 Receive_expression::do_type()
11775 Channel_type* channel_type = this->channel_->type()->channel_type();
11776 if (channel_type == NULL)
11777 return Type::make_error_type();
11778 return channel_type->element_type();
11781 // Check types for a receive expression.
11784 Receive_expression::do_check_types(Gogo*)
11786 Type* type = this->channel_->type();
11787 if (type->is_error_type())
11789 this->set_is_error();
11792 if (type->channel_type() == NULL)
11794 this->report_error(_("expected channel"));
11797 if (!type->channel_type()->may_receive())
11799 this->report_error(_("invalid receive on send-only channel"));
11804 // Get a tree for a receive expression.
11807 Receive_expression::do_get_tree(Translate_context* context)
11809 Channel_type* channel_type = this->channel_->type()->channel_type();
11810 gcc_assert(channel_type != NULL);
11811 Type* element_type = channel_type->element_type();
11812 tree element_type_tree = element_type->get_tree(context->gogo());
11814 tree channel = this->channel_->get_tree(context);
11815 if (element_type_tree == error_mark_node || channel == error_mark_node)
11816 return error_mark_node;
11818 return Gogo::receive_from_channel(element_type_tree, channel,
11819 this->for_select_, this->location());
11822 // Make a receive expression.
11824 Receive_expression*
11825 Expression::make_receive(Expression* channel, source_location location)
11827 return new Receive_expression(channel, location);
11830 // Class Send_expression.
11835 Send_expression::do_traverse(Traverse* traverse)
11837 if (Expression::traverse(&this->channel_, traverse) == TRAVERSE_EXIT)
11838 return TRAVERSE_EXIT;
11839 return Expression::traverse(&this->val_, traverse);
11845 Send_expression::do_type()
11847 return Type::lookup_bool_type();
11853 Send_expression::do_determine_type(const Type_context*)
11855 this->channel_->determine_type_no_context();
11857 Type* type = this->channel_->type();
11858 Type_context subcontext;
11859 if (type->channel_type() != NULL)
11860 subcontext.type = type->channel_type()->element_type();
11861 this->val_->determine_type(&subcontext);
11867 Send_expression::do_check_types(Gogo*)
11869 Type* type = this->channel_->type();
11870 if (type->is_error_type())
11872 this->set_is_error();
11875 Channel_type* channel_type = type->channel_type();
11876 if (channel_type == NULL)
11878 error_at(this->location(), "left operand of %<<-%> must be channel");
11879 this->set_is_error();
11882 Type* element_type = channel_type->element_type();
11883 if (element_type != NULL
11884 && !Type::are_assignable(element_type, this->val_->type(), NULL))
11886 this->report_error(_("incompatible types in send"));
11889 if (!channel_type->may_send())
11891 this->report_error(_("invalid send on receive-only channel"));
11896 // Get a tree for a send expression.
11899 Send_expression::do_get_tree(Translate_context* context)
11901 tree channel = this->channel_->get_tree(context);
11902 tree val = this->val_->get_tree(context);
11903 if (channel == error_mark_node || val == error_mark_node)
11904 return error_mark_node;
11905 Channel_type* channel_type = this->channel_->type()->channel_type();
11906 val = Expression::convert_for_assignment(context,
11907 channel_type->element_type(),
11908 this->val_->type(),
11911 return Gogo::send_on_channel(channel, val, this->is_value_discarded_,
11912 this->for_select_, this->location());
11915 // Make a send expression
11918 Expression::make_send(Expression* channel, Expression* val,
11919 source_location location)
11921 return new Send_expression(channel, val, location);
11924 // An expression which evaluates to a pointer to the type descriptor
11927 class Type_descriptor_expression : public Expression
11930 Type_descriptor_expression(Type* type, source_location location)
11931 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
11938 { return Type::make_type_descriptor_ptr_type(); }
11941 do_determine_type(const Type_context*)
11949 do_get_tree(Translate_context* context)
11950 { return this->type_->type_descriptor_pointer(context->gogo()); }
11953 // The type for which this is the descriptor.
11957 // Make a type descriptor expression.
11960 Expression::make_type_descriptor(Type* type, source_location location)
11962 return new Type_descriptor_expression(type, location);
11965 // An expression which evaluates to some characteristic of a type.
11966 // This is only used to initialize fields of a type descriptor. Using
11967 // a new expression class is slightly inefficient but gives us a good
11968 // separation between the frontend and the middle-end with regard to
11969 // how types are laid out.
11971 class Type_info_expression : public Expression
11974 Type_info_expression(Type* type, Type_info type_info)
11975 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
11976 type_(type), type_info_(type_info)
11984 do_determine_type(const Type_context*)
11992 do_get_tree(Translate_context* context);
11995 // The type for which we are getting information.
11997 // What information we want.
11998 Type_info type_info_;
12001 // The type is chosen to match what the type descriptor struct
12005 Type_info_expression::do_type()
12007 switch (this->type_info_)
12009 case TYPE_INFO_SIZE:
12010 return Type::lookup_integer_type("uintptr");
12011 case TYPE_INFO_ALIGNMENT:
12012 case TYPE_INFO_FIELD_ALIGNMENT:
12013 return Type::lookup_integer_type("uint8");
12019 // Return type information in GENERIC.
12022 Type_info_expression::do_get_tree(Translate_context* context)
12024 tree type_tree = this->type_->get_tree(context->gogo());
12025 if (type_tree == error_mark_node)
12026 return error_mark_node;
12028 tree val_type_tree = this->type()->get_tree(context->gogo());
12029 gcc_assert(val_type_tree != error_mark_node);
12031 if (this->type_info_ == TYPE_INFO_SIZE)
12032 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12033 TYPE_SIZE_UNIT(type_tree));
12036 unsigned HOST_WIDE_INT val;
12037 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12038 val = TYPE_ALIGN_UNIT(type_tree);
12041 gcc_assert(this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT);
12042 val = TYPE_ALIGN(type_tree);
12043 #ifdef BIGGEST_FIELD_ALIGMENT
12044 if (val > BIGGEST_FIELD_ALIGNMENT)
12045 val = BIGGEST_FIELD_ALIGNMENT;
12047 #ifdef ADJUST_FIELD_ALIGN
12049 tree f = build_decl(UNKNOWN_LOCATION, FIELD_DECL, NULL, type_tree);
12050 val = ADJUST_FIELD_ALIGN(f, val);
12053 val /= BITS_PER_UNIT;
12056 return build_int_cstu(val_type_tree, val);
12060 // Make a type info expression.
12063 Expression::make_type_info(Type* type, Type_info type_info)
12065 return new Type_info_expression(type, type_info);
12068 // An expression which evaluates to the offset of a field within a
12069 // struct. This, like Type_info_expression, q.v., is only used to
12070 // initialize fields of a type descriptor.
12072 class Struct_field_offset_expression : public Expression
12075 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12076 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12077 type_(type), field_(field)
12083 { return Type::lookup_integer_type("uintptr"); }
12086 do_determine_type(const Type_context*)
12094 do_get_tree(Translate_context* context);
12097 // The type of the struct.
12098 Struct_type* type_;
12100 const Struct_field* field_;
12103 // Return a struct field offset in GENERIC.
12106 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12108 tree type_tree = this->type_->get_tree(context->gogo());
12109 if (type_tree == error_mark_node)
12110 return error_mark_node;
12112 tree val_type_tree = this->type()->get_tree(context->gogo());
12113 gcc_assert(val_type_tree != error_mark_node);
12115 const Struct_field_list* fields = this->type_->fields();
12116 tree struct_field_tree = TYPE_FIELDS(type_tree);
12117 Struct_field_list::const_iterator p;
12118 for (p = fields->begin();
12119 p != fields->end();
12120 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12122 gcc_assert(struct_field_tree != NULL_TREE);
12123 if (&*p == this->field_)
12126 gcc_assert(&*p == this->field_);
12128 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12129 byte_position(struct_field_tree));
12132 // Make an expression for a struct field offset.
12135 Expression::make_struct_field_offset(Struct_type* type,
12136 const Struct_field* field)
12138 return new Struct_field_offset_expression(type, field);
12141 // An expression which evaluates to the address of an unnamed label.
12143 class Label_addr_expression : public Expression
12146 Label_addr_expression(Label* label, source_location location)
12147 : Expression(EXPRESSION_LABEL_ADDR, location),
12154 { return Type::make_pointer_type(Type::make_void_type()); }
12157 do_determine_type(const Type_context*)
12162 { return new Label_addr_expression(this->label_, this->location()); }
12165 do_get_tree(Translate_context*)
12166 { return this->label_->get_addr(this->location()); }
12169 // The label whose address we are taking.
12173 // Make an expression for the address of an unnamed label.
12176 Expression::make_label_addr(Label* label, source_location location)
12178 return new Label_addr_expression(label, location);
12181 // Import an expression. This comes at the end in order to see the
12182 // various class definitions.
12185 Expression::import_expression(Import* imp)
12187 int c = imp->peek_char();
12188 if (imp->match_c_string("- ")
12189 || imp->match_c_string("! ")
12190 || imp->match_c_string("^ "))
12191 return Unary_expression::do_import(imp);
12193 return Binary_expression::do_import(imp);
12194 else if (imp->match_c_string("true")
12195 || imp->match_c_string("false"))
12196 return Boolean_expression::do_import(imp);
12198 return String_expression::do_import(imp);
12199 else if (c == '-' || (c >= '0' && c <= '9'))
12201 // This handles integers, floats and complex constants.
12202 return Integer_expression::do_import(imp);
12204 else if (imp->match_c_string("nil"))
12205 return Nil_expression::do_import(imp);
12206 else if (imp->match_c_string("convert"))
12207 return Type_conversion_expression::do_import(imp);
12210 error_at(imp->location(), "import error: expected expression");
12211 return Expression::make_error(imp->location());
12215 // Class Expression_list.
12217 // Traverse the list.
12220 Expression_list::traverse(Traverse* traverse)
12222 for (Expression_list::iterator p = this->begin();
12228 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12229 return TRAVERSE_EXIT;
12232 return TRAVERSE_CONTINUE;
12238 Expression_list::copy()
12240 Expression_list* ret = new Expression_list();
12241 for (Expression_list::iterator p = this->begin();
12246 ret->push_back(NULL);
12248 ret->push_back((*p)->copy());
12253 // Return whether an expression list has an error expression.
12256 Expression_list::contains_error() const
12258 for (Expression_list::const_iterator p = this->begin();
12261 if (*p != NULL && (*p)->is_error_expression())