1 // expressions.cc -- Go frontend expression handling.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #ifndef ENABLE_BUILD_WITH_CXX
20 #include "tree-iterator.h"
25 #ifndef ENABLE_BUILD_WITH_CXX
34 #include "statements.h"
38 #include "expressions.h"
43 Expression::Expression(Expression_classification classification,
45 : classification_(classification), location_(location)
49 Expression::~Expression()
53 // If this expression has a constant integer value, return it.
56 Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
60 return this->do_integer_constant_value(iota_is_constant, val, ptype);
63 // If this expression has a constant floating point value, return it.
66 Expression::float_constant_value(mpfr_t val, Type** ptype) const
69 if (this->do_float_constant_value(val, ptype))
75 if (!this->do_integer_constant_value(false, ival, &t))
79 mpfr_set_z(val, ival, GMP_RNDN);
86 // If this expression has a constant complex value, return it.
89 Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
93 if (this->do_complex_constant_value(real, imag, ptype))
96 if (this->float_constant_value(real, &t))
98 mpfr_set_ui(imag, 0, GMP_RNDN);
104 // Traverse the expressions.
107 Expression::traverse(Expression** pexpr, Traverse* traverse)
109 Expression* expr = *pexpr;
110 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
112 int t = traverse->expression(pexpr);
113 if (t == TRAVERSE_EXIT)
114 return TRAVERSE_EXIT;
115 else if (t == TRAVERSE_SKIP_COMPONENTS)
116 return TRAVERSE_CONTINUE;
118 return expr->do_traverse(traverse);
121 // Traverse subexpressions of this expression.
124 Expression::traverse_subexpressions(Traverse* traverse)
126 return this->do_traverse(traverse);
129 // Default implementation for do_traverse for child classes.
132 Expression::do_traverse(Traverse*)
134 return TRAVERSE_CONTINUE;
137 // This virtual function is called by the parser if the value of this
138 // expression is being discarded. By default, we give an error.
139 // Expressions with side effects override.
142 Expression::do_discarding_value()
144 this->unused_value_error();
147 // This virtual function is called to export expressions. This will
148 // only be used by expressions which may be constant.
151 Expression::do_export(Export*) const
156 // Give an error saying that the value of the expression is not used.
159 Expression::unused_value_error()
161 error_at(this->location(), "value computed is not used");
164 // Note that this expression is an error. This is called by children
165 // when they discover an error.
168 Expression::set_is_error()
170 this->classification_ = EXPRESSION_ERROR;
173 // For children to call to report an error conveniently.
176 Expression::report_error(const char* msg)
178 error_at(this->location_, "%s", msg);
179 this->set_is_error();
182 // Set types of variables and constants. This is implemented by the
186 Expression::determine_type(const Type_context* context)
188 this->do_determine_type(context);
191 // Set types when there is no context.
194 Expression::determine_type_no_context()
196 Type_context context;
197 this->do_determine_type(&context);
200 // Return a tree handling any conversions which must be done during
204 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
205 Type* rhs_type, tree rhs_tree,
208 if (lhs_type == rhs_type)
211 if (lhs_type->is_error() || rhs_type->is_error())
212 return error_mark_node;
214 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
215 return error_mark_node;
217 Gogo* gogo = context->gogo();
219 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
220 if (lhs_type_tree == error_mark_node)
221 return error_mark_node;
223 if (lhs_type->interface_type() != NULL)
225 if (rhs_type->interface_type() == NULL)
226 return Expression::convert_type_to_interface(context, lhs_type,
230 return Expression::convert_interface_to_interface(context, lhs_type,
234 else if (rhs_type->interface_type() != NULL)
235 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
237 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
239 // Assigning nil to an open array.
240 go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
242 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
244 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
245 tree field = TYPE_FIELDS(lhs_type_tree);
246 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
249 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
251 elt = VEC_quick_push(constructor_elt, init, NULL);
252 field = DECL_CHAIN(field);
253 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
256 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
258 elt = VEC_quick_push(constructor_elt, init, NULL);
259 field = DECL_CHAIN(field);
260 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
263 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
265 tree val = build_constructor(lhs_type_tree, init);
266 TREE_CONSTANT(val) = 1;
270 else if (rhs_type->is_nil_type())
272 // The left hand side should be a pointer type at the tree
274 go_assert(POINTER_TYPE_P(lhs_type_tree));
275 return fold_convert(lhs_type_tree, null_pointer_node);
277 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
279 // No conversion is needed.
282 else if (POINTER_TYPE_P(lhs_type_tree)
283 || INTEGRAL_TYPE_P(lhs_type_tree)
284 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
285 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
286 return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree);
287 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
288 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
290 // This conversion must be permitted by Go, or we wouldn't have
292 go_assert(int_size_in_bytes(lhs_type_tree)
293 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
294 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
295 lhs_type_tree, rhs_tree);
299 go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
304 // Return a tree for a conversion from a non-interface type to an
308 Expression::convert_type_to_interface(Translate_context* context,
309 Type* lhs_type, Type* rhs_type,
310 tree rhs_tree, Location location)
312 Gogo* gogo = context->gogo();
313 Interface_type* lhs_interface_type = lhs_type->interface_type();
314 bool lhs_is_empty = lhs_interface_type->is_empty();
316 // Since RHS_TYPE is a static type, we can create the interface
317 // method table at compile time.
319 // When setting an interface to nil, we just set both fields to
321 if (rhs_type->is_nil_type())
323 Btype* lhs_btype = lhs_type->get_backend(gogo);
324 return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
327 // This should have been checked already.
328 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
330 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
331 if (lhs_type_tree == error_mark_node)
332 return error_mark_node;
334 // An interface is a tuple. If LHS_TYPE is an empty interface type,
335 // then the first field is the type descriptor for RHS_TYPE.
336 // Otherwise it is the interface method table for RHS_TYPE.
337 tree first_field_value;
339 first_field_value = rhs_type->type_descriptor_pointer(gogo, location);
342 // Build the interface method table for this interface and this
343 // object type: a list of function pointers for each interface
345 Named_type* rhs_named_type = rhs_type->named_type();
346 bool is_pointer = false;
347 if (rhs_named_type == NULL)
349 rhs_named_type = rhs_type->deref()->named_type();
353 if (rhs_named_type == NULL)
354 method_table = null_pointer_node;
357 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
359 first_field_value = fold_convert_loc(location.gcc_location(),
360 const_ptr_type_node, method_table);
362 if (first_field_value == error_mark_node)
363 return error_mark_node;
365 // Start building a constructor for the value we will return.
367 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
369 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
370 tree field = TYPE_FIELDS(lhs_type_tree);
371 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
372 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
374 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
377 elt = VEC_quick_push(constructor_elt, init, NULL);
378 field = DECL_CHAIN(field);
379 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
382 if (rhs_type->points_to() != NULL)
384 // We are assigning a pointer to the interface; the interface
385 // holds the pointer itself.
386 elt->value = rhs_tree;
387 return build_constructor(lhs_type_tree, init);
390 // We are assigning a non-pointer value to the interface; the
391 // interface gets a copy of the value in the heap.
393 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
395 tree space = gogo->allocate_memory(rhs_type, object_size, location);
396 space = fold_convert_loc(location.gcc_location(),
397 build_pointer_type(TREE_TYPE(rhs_tree)), space);
398 space = save_expr(space);
400 tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space);
401 TREE_THIS_NOTRAP(ref) = 1;
402 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
403 void_type_node, ref, rhs_tree);
405 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
408 return build2(COMPOUND_EXPR, lhs_type_tree, set,
409 build_constructor(lhs_type_tree, init));
412 // Return a tree for the type descriptor of RHS_TREE, which has
413 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
417 Expression::get_interface_type_descriptor(Translate_context*,
418 Type* rhs_type, tree rhs_tree,
421 tree rhs_type_tree = TREE_TYPE(rhs_tree);
422 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
423 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
424 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
426 if (rhs_type->interface_type()->is_empty())
428 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
429 "__type_descriptor") == 0);
433 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
435 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
437 tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v);
438 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
439 tree f = TYPE_FIELDS(TREE_TYPE(v1));
440 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
442 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
444 tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node,
445 v, fold_convert_loc(location.gcc_location(),
448 tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1),
450 return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1),
454 // Return a tree for the conversion of an interface type to an
458 Expression::convert_interface_to_interface(Translate_context* context,
459 Type *lhs_type, Type *rhs_type,
460 tree rhs_tree, bool for_type_guard,
463 Gogo* gogo = context->gogo();
464 Interface_type* lhs_interface_type = lhs_type->interface_type();
465 bool lhs_is_empty = lhs_interface_type->is_empty();
467 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
468 if (lhs_type_tree == error_mark_node)
469 return error_mark_node;
471 // In the general case this requires runtime examination of the type
472 // method table to match it up with the interface methods.
474 // FIXME: If all of the methods in the right hand side interface
475 // also appear in the left hand side interface, then we don't need
476 // to do a runtime check, although we still need to build a new
479 // Get the type descriptor for the right hand side. This will be
480 // NULL for a nil interface.
482 if (!DECL_P(rhs_tree))
483 rhs_tree = save_expr(rhs_tree);
485 tree rhs_type_descriptor =
486 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
489 // The result is going to be a two element constructor.
491 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
493 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
494 tree field = TYPE_FIELDS(lhs_type_tree);
499 // A type assertion fails when converting a nil interface.
500 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
502 static tree assert_interface_decl;
503 tree call = Gogo::call_builtin(&assert_interface_decl,
505 "__go_assert_interface",
508 TREE_TYPE(lhs_type_descriptor),
510 TREE_TYPE(rhs_type_descriptor),
511 rhs_type_descriptor);
512 if (call == error_mark_node)
513 return error_mark_node;
514 // This will panic if the interface conversion fails.
515 TREE_NOTHROW(assert_interface_decl) = 0;
516 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
519 else if (lhs_is_empty)
521 // A convertion to an empty interface always succeeds, and the
522 // first field is just the type descriptor of the object.
523 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
524 "__type_descriptor") == 0);
525 go_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
526 elt->value = rhs_type_descriptor;
530 // A conversion to a non-empty interface may fail, but unlike a
531 // type assertion converting nil will always succeed.
532 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
534 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
536 static tree convert_interface_decl;
537 tree call = Gogo::call_builtin(&convert_interface_decl,
539 "__go_convert_interface",
542 TREE_TYPE(lhs_type_descriptor),
544 TREE_TYPE(rhs_type_descriptor),
545 rhs_type_descriptor);
546 if (call == error_mark_node)
547 return error_mark_node;
548 // This will panic if the interface conversion fails.
549 TREE_NOTHROW(convert_interface_decl) = 0;
550 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
554 // The second field is simply the object pointer.
556 elt = VEC_quick_push(constructor_elt, init, NULL);
557 field = DECL_CHAIN(field);
558 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
561 tree rhs_type_tree = TREE_TYPE(rhs_tree);
562 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
563 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
564 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
565 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
568 return build_constructor(lhs_type_tree, init);
571 // Return a tree for the conversion of an interface type to a
572 // non-interface type.
575 Expression::convert_interface_to_type(Translate_context* context,
576 Type *lhs_type, Type* rhs_type,
577 tree rhs_tree, Location location)
579 Gogo* gogo = context->gogo();
580 tree rhs_type_tree = TREE_TYPE(rhs_tree);
582 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
583 if (lhs_type_tree == error_mark_node)
584 return error_mark_node;
586 // Call a function to check that the type is valid. The function
587 // will panic with an appropriate runtime type error if the type is
590 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location);
592 if (!DECL_P(rhs_tree))
593 rhs_tree = save_expr(rhs_tree);
595 tree rhs_type_descriptor =
596 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
599 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo,
602 static tree check_interface_type_decl;
603 tree call = Gogo::call_builtin(&check_interface_type_decl,
605 "__go_check_interface_type",
608 TREE_TYPE(lhs_type_descriptor),
610 TREE_TYPE(rhs_type_descriptor),
612 TREE_TYPE(rhs_inter_descriptor),
613 rhs_inter_descriptor);
614 if (call == error_mark_node)
615 return error_mark_node;
616 // This call will panic if the conversion is invalid.
617 TREE_NOTHROW(check_interface_type_decl) = 0;
619 // If the call succeeds, pull out the value.
620 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
621 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
622 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
623 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
626 // If the value is a pointer, then it is the value we want.
627 // Otherwise it points to the value.
628 if (lhs_type->points_to() == NULL)
630 val = fold_convert_loc(location.gcc_location(),
631 build_pointer_type(lhs_type_tree), val);
632 val = build_fold_indirect_ref_loc(location.gcc_location(), val);
635 return build2(COMPOUND_EXPR, lhs_type_tree, call,
636 fold_convert_loc(location.gcc_location(), lhs_type_tree, val));
639 // Convert an expression to a tree. This is implemented by the child
640 // class. Not that it is not in general safe to call this multiple
641 // times for a single expression, but that we don't catch such errors.
644 Expression::get_tree(Translate_context* context)
646 // The child may have marked this expression as having an error.
647 if (this->classification_ == EXPRESSION_ERROR)
648 return error_mark_node;
650 return this->do_get_tree(context);
653 // Return a tree for VAL in TYPE.
656 Expression::integer_constant_tree(mpz_t val, tree type)
658 if (type == error_mark_node)
659 return error_mark_node;
660 else if (TREE_CODE(type) == INTEGER_TYPE)
661 return double_int_to_tree(type,
662 mpz_get_double_int(type, val, true));
663 else if (TREE_CODE(type) == REAL_TYPE)
666 mpfr_init_set_z(fval, val, GMP_RNDN);
667 tree ret = Expression::float_constant_tree(fval, type);
671 else if (TREE_CODE(type) == COMPLEX_TYPE)
674 mpfr_init_set_z(fval, val, GMP_RNDN);
675 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
677 tree imag = build_real_from_int_cst(TREE_TYPE(type),
679 return build_complex(type, real, imag);
685 // Return a tree for VAL in TYPE.
688 Expression::float_constant_tree(mpfr_t val, tree type)
690 if (type == error_mark_node)
691 return error_mark_node;
692 else if (TREE_CODE(type) == INTEGER_TYPE)
696 mpfr_get_z(ival, val, GMP_RNDN);
697 tree ret = Expression::integer_constant_tree(ival, type);
701 else if (TREE_CODE(type) == REAL_TYPE)
704 real_from_mpfr(&r1, val, type, GMP_RNDN);
706 real_convert(&r2, TYPE_MODE(type), &r1);
707 return build_real(type, r2);
709 else if (TREE_CODE(type) == COMPLEX_TYPE)
712 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
714 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
715 tree imag = build_real_from_int_cst(TREE_TYPE(type),
717 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
723 // Return a tree for REAL/IMAG in TYPE.
726 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
728 if (type == error_mark_node)
729 return error_mark_node;
730 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
731 return Expression::float_constant_tree(real, type);
732 else if (TREE_CODE(type) == COMPLEX_TYPE)
735 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
737 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
740 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
742 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
744 return build_complex(type, build_real(TREE_TYPE(type), r2),
745 build_real(TREE_TYPE(type), r4));
751 // Return a tree which evaluates to true if VAL, of arbitrary integer
752 // type, is negative or is more than the maximum value of BOUND_TYPE.
753 // If SOFAR is not NULL, it is or'red into the result. The return
754 // value may be NULL if SOFAR is NULL.
757 Expression::check_bounds(tree val, tree bound_type, tree sofar,
760 tree val_type = TREE_TYPE(val);
761 tree ret = NULL_TREE;
763 if (!TYPE_UNSIGNED(val_type))
765 ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val,
766 build_int_cst(val_type, 0));
767 if (ret == boolean_false_node)
771 HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
772 HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
773 go_assert(val_type_size != -1 && bound_type_size != -1);
774 if (val_type_size > bound_type_size
775 || (val_type_size == bound_type_size
776 && TYPE_UNSIGNED(val_type)
777 && !TYPE_UNSIGNED(bound_type)))
779 tree max = TYPE_MAX_VALUE(bound_type);
780 tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node,
781 val, fold_convert_loc(loc.gcc_location(),
783 if (big == boolean_false_node)
785 else if (ret == NULL_TREE)
788 ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
789 boolean_type_node, ret, big);
792 if (ret == NULL_TREE)
794 else if (sofar == NULL_TREE)
797 return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node,
802 Expression::dump_expression(Ast_dump_context* ast_dump_context) const
804 this->do_dump_expression(ast_dump_context);
807 // Error expressions. This are used to avoid cascading errors.
809 class Error_expression : public Expression
812 Error_expression(Location location)
813 : Expression(EXPRESSION_ERROR, location)
818 do_is_constant() const
822 do_integer_constant_value(bool, mpz_t val, Type**) const
829 do_float_constant_value(mpfr_t val, Type**) const
831 mpfr_set_ui(val, 0, GMP_RNDN);
836 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
838 mpfr_set_ui(real, 0, GMP_RNDN);
839 mpfr_set_ui(imag, 0, GMP_RNDN);
844 do_discarding_value()
849 { return Type::make_error_type(); }
852 do_determine_type(const Type_context*)
860 do_is_addressable() const
864 do_get_tree(Translate_context*)
865 { return error_mark_node; }
868 do_dump_expression(Ast_dump_context*) const;
871 // Dump the ast representation for an error expression to a dump context.
874 Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
876 ast_dump_context->ostream() << "_Error_" ;
880 Expression::make_error(Location location)
882 return new Error_expression(location);
885 // An expression which is really a type. This is used during parsing.
886 // It is an error if these survive after lowering.
889 Type_expression : public Expression
892 Type_expression(Type* type, Location location)
893 : Expression(EXPRESSION_TYPE, location),
899 do_traverse(Traverse* traverse)
900 { return Type::traverse(this->type_, traverse); }
904 { return this->type_; }
907 do_determine_type(const Type_context*)
911 do_check_types(Gogo*)
912 { this->report_error(_("invalid use of type")); }
919 do_get_tree(Translate_context*)
920 { go_unreachable(); }
922 void do_dump_expression(Ast_dump_context*) const;
925 // The type which we are representing as an expression.
930 Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
932 ast_dump_context->dump_type(this->type_);
936 Expression::make_type(Type* type, Location location)
938 return new Type_expression(type, location);
941 // Class Parser_expression.
944 Parser_expression::do_type()
946 // We should never really ask for the type of a Parser_expression.
947 // However, it can happen, at least when we have an invalid const
948 // whose initializer refers to the const itself. In that case we
949 // may ask for the type when lowering the const itself.
950 go_assert(saw_errors());
951 return Type::make_error_type();
954 // Class Var_expression.
956 // Lower a variable expression. Here we just make sure that the
957 // initialization expression of the variable has been lowered. This
958 // ensures that we will be able to determine the type of the variable
962 Var_expression::do_lower(Gogo* gogo, Named_object* function,
963 Statement_inserter* inserter, int)
965 if (this->variable_->is_variable())
967 Variable* var = this->variable_->var_value();
968 // This is either a local variable or a global variable. A
969 // reference to a variable which is local to an enclosing
970 // function will be a reference to a field in a closure.
971 if (var->is_global())
976 var->lower_init_expression(gogo, function, inserter);
981 // Return the type of a reference to a variable.
984 Var_expression::do_type()
986 if (this->variable_->is_variable())
987 return this->variable_->var_value()->type();
988 else if (this->variable_->is_result_variable())
989 return this->variable_->result_var_value()->type();
994 // Determine the type of a reference to a variable.
997 Var_expression::do_determine_type(const Type_context*)
999 if (this->variable_->is_variable())
1000 this->variable_->var_value()->determine_type();
1003 // Something takes the address of this variable. This means that we
1004 // may want to move the variable onto the heap.
1007 Var_expression::do_address_taken(bool escapes)
1011 if (this->variable_->is_variable())
1012 this->variable_->var_value()->set_non_escaping_address_taken();
1013 else if (this->variable_->is_result_variable())
1014 this->variable_->result_var_value()->set_non_escaping_address_taken();
1020 if (this->variable_->is_variable())
1021 this->variable_->var_value()->set_address_taken();
1022 else if (this->variable_->is_result_variable())
1023 this->variable_->result_var_value()->set_address_taken();
1029 // Get the tree for a reference to a variable.
1032 Var_expression::do_get_tree(Translate_context* context)
1034 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
1035 context->function());
1036 tree ret = var_to_tree(bvar);
1037 if (ret == error_mark_node)
1038 return error_mark_node;
1040 if (this->variable_->is_variable())
1041 is_in_heap = this->variable_->var_value()->is_in_heap();
1042 else if (this->variable_->is_result_variable())
1043 is_in_heap = this->variable_->result_var_value()->is_in_heap();
1048 ret = build_fold_indirect_ref_loc(this->location().gcc_location(), ret);
1049 TREE_THIS_NOTRAP(ret) = 1;
1054 // Ast dump for variable expression.
1057 Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1059 ast_dump_context->ostream() << this->variable_->name() ;
1062 // Make a reference to a variable in an expression.
1065 Expression::make_var_reference(Named_object* var, Location location)
1068 return Expression::make_sink(location);
1070 // FIXME: Creating a new object for each reference to a variable is
1072 return new Var_expression(var, location);
1075 // Class Temporary_reference_expression.
1080 Temporary_reference_expression::do_type()
1082 return this->statement_->type();
1085 // Called if something takes the address of this temporary variable.
1086 // We never have to move temporary variables to the heap, but we do
1087 // need to know that they must live in the stack rather than in a
1091 Temporary_reference_expression::do_address_taken(bool)
1093 this->statement_->set_is_address_taken();
1096 // Get a tree referring to the variable.
1099 Temporary_reference_expression::do_get_tree(Translate_context* context)
1101 Bvariable* bvar = this->statement_->get_backend_variable(context);
1103 // The gcc backend can't represent the same set of recursive types
1104 // that the Go frontend can. In some cases this means that a
1105 // temporary variable won't have the right backend type. Correct
1106 // that here by adding a type cast. We need to use base() to push
1107 // the circularity down one level.
1108 tree ret = var_to_tree(bvar);
1109 if (!this->is_lvalue_
1110 && POINTER_TYPE_P(TREE_TYPE(ret))
1111 && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
1113 Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1114 tree type_tree = type_to_tree(type_btype);
1115 ret = fold_convert_loc(this->location().gcc_location(), type_tree, ret);
1120 // Ast dump for temporary reference.
1123 Temporary_reference_expression::do_dump_expression(
1124 Ast_dump_context* ast_dump_context) const
1126 ast_dump_context->dump_temp_variable_name(this->statement_);
1129 // Make a reference to a temporary variable.
1131 Temporary_reference_expression*
1132 Expression::make_temporary_reference(Temporary_statement* statement,
1135 return new Temporary_reference_expression(statement, location);
1138 // A sink expression--a use of the blank identifier _.
1140 class Sink_expression : public Expression
1143 Sink_expression(Location location)
1144 : Expression(EXPRESSION_SINK, location),
1145 type_(NULL), var_(NULL_TREE)
1150 do_discarding_value()
1157 do_determine_type(const Type_context*);
1161 { return new Sink_expression(this->location()); }
1164 do_get_tree(Translate_context*);
1167 do_dump_expression(Ast_dump_context*) const;
1170 // The type of this sink variable.
1172 // The temporary variable we generate.
1176 // Return the type of a sink expression.
1179 Sink_expression::do_type()
1181 if (this->type_ == NULL)
1182 return Type::make_sink_type();
1186 // Determine the type of a sink expression.
1189 Sink_expression::do_determine_type(const Type_context* context)
1191 if (context->type != NULL)
1192 this->type_ = context->type;
1195 // Return a temporary variable for a sink expression. This will
1196 // presumably be a write-only variable which the middle-end will drop.
1199 Sink_expression::do_get_tree(Translate_context* context)
1201 if (this->var_ == NULL_TREE)
1203 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
1204 Btype* bt = this->type_->get_backend(context->gogo());
1205 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
1210 // Ast dump for sink expression.
1213 Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1215 ast_dump_context->ostream() << "_" ;
1218 // Make a sink expression.
1221 Expression::make_sink(Location location)
1223 return new Sink_expression(location);
1226 // Class Func_expression.
1228 // FIXME: Can a function expression appear in a constant expression?
1229 // The value is unchanging. Initializing a constant to the address of
1230 // a function seems like it could work, though there might be little
1236 Func_expression::do_traverse(Traverse* traverse)
1238 return (this->closure_ == NULL
1240 : Expression::traverse(&this->closure_, traverse));
1243 // Return the type of a function expression.
1246 Func_expression::do_type()
1248 if (this->function_->is_function())
1249 return this->function_->func_value()->type();
1250 else if (this->function_->is_function_declaration())
1251 return this->function_->func_declaration_value()->type();
1256 // Get the tree for a function expression without evaluating the
1260 Func_expression::get_tree_without_closure(Gogo* gogo)
1262 Function_type* fntype;
1263 if (this->function_->is_function())
1264 fntype = this->function_->func_value()->type();
1265 else if (this->function_->is_function_declaration())
1266 fntype = this->function_->func_declaration_value()->type();
1270 // Builtin functions are handled specially by Call_expression. We
1271 // can't take their address.
1272 if (fntype->is_builtin())
1274 error_at(this->location(), "invalid use of special builtin function %qs",
1275 this->function_->name().c_str());
1276 return error_mark_node;
1279 Named_object* no = this->function_;
1281 tree id = no->get_id(gogo);
1282 if (id == error_mark_node)
1283 return error_mark_node;
1286 if (no->is_function())
1287 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1288 else if (no->is_function_declaration())
1289 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1293 if (fndecl == error_mark_node)
1294 return error_mark_node;
1296 return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl);
1299 // Get the tree for a function expression. This is used when we take
1300 // the address of a function rather than simply calling it. If the
1301 // function has a closure, we must use a trampoline.
1304 Func_expression::do_get_tree(Translate_context* context)
1306 Gogo* gogo = context->gogo();
1308 tree fnaddr = this->get_tree_without_closure(gogo);
1309 if (fnaddr == error_mark_node)
1310 return error_mark_node;
1312 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1313 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1314 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1316 // For a normal non-nested function call, that is all we have to do.
1317 if (!this->function_->is_function()
1318 || this->function_->func_value()->enclosing() == NULL)
1320 go_assert(this->closure_ == NULL);
1324 // For a nested function call, we have to always allocate a
1325 // trampoline. If we don't always allocate, then closures will not
1326 // be reliably distinct.
1327 Expression* closure = this->closure_;
1329 if (closure == NULL)
1330 closure_tree = null_pointer_node;
1333 // Get the value of the closure. This will be a pointer to
1334 // space allocated on the heap.
1335 closure_tree = closure->get_tree(context);
1336 if (closure_tree == error_mark_node)
1337 return error_mark_node;
1338 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1341 // Now we need to build some code on the heap. This code will load
1342 // the static chain pointer with the closure and then jump to the
1343 // body of the function. The normal gcc approach is to build the
1344 // code on the stack. Unfortunately we can not do that, as Go
1345 // permits us to return the function pointer.
1347 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1350 // Ast dump for function.
1353 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1355 ast_dump_context->ostream() << this->function_->name();
1356 if (this->closure_ != NULL)
1358 ast_dump_context->ostream() << " {closure = ";
1359 this->closure_->dump_expression(ast_dump_context);
1360 ast_dump_context->ostream() << "}";
1364 // Make a reference to a function in an expression.
1367 Expression::make_func_reference(Named_object* function, Expression* closure,
1370 return new Func_expression(function, closure, location);
1373 // Class Unknown_expression.
1375 // Return the name of an unknown expression.
1378 Unknown_expression::name() const
1380 return this->named_object_->name();
1383 // Lower a reference to an unknown name.
1386 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1388 Location location = this->location();
1389 Named_object* no = this->named_object_;
1391 if (!no->is_unknown())
1395 real = no->unknown_value()->real_named_object();
1398 if (this->is_composite_literal_key_)
1400 error_at(location, "reference to undefined name %qs",
1401 this->named_object_->message_name().c_str());
1402 return Expression::make_error(location);
1405 switch (real->classification())
1407 case Named_object::NAMED_OBJECT_CONST:
1408 return Expression::make_const_reference(real, location);
1409 case Named_object::NAMED_OBJECT_TYPE:
1410 return Expression::make_type(real->type_value(), location);
1411 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1412 if (this->is_composite_literal_key_)
1414 error_at(location, "reference to undefined type %qs",
1415 real->message_name().c_str());
1416 return Expression::make_error(location);
1417 case Named_object::NAMED_OBJECT_VAR:
1418 return Expression::make_var_reference(real, location);
1419 case Named_object::NAMED_OBJECT_FUNC:
1420 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1421 return Expression::make_func_reference(real, NULL, location);
1422 case Named_object::NAMED_OBJECT_PACKAGE:
1423 if (this->is_composite_literal_key_)
1425 error_at(location, "unexpected reference to package");
1426 return Expression::make_error(location);
1432 // Dump the ast representation for an unknown expression to a dump context.
1435 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1437 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1441 // Make a reference to an unknown name.
1444 Expression::make_unknown_reference(Named_object* no, Location location)
1446 return new Unknown_expression(no, location);
1449 // A boolean expression.
1451 class Boolean_expression : public Expression
1454 Boolean_expression(bool val, Location location)
1455 : Expression(EXPRESSION_BOOLEAN, location),
1456 val_(val), type_(NULL)
1464 do_is_constant() const
1471 do_determine_type(const Type_context*);
1478 do_get_tree(Translate_context*)
1479 { return this->val_ ? boolean_true_node : boolean_false_node; }
1482 do_export(Export* exp) const
1483 { exp->write_c_string(this->val_ ? "true" : "false"); }
1486 do_dump_expression(Ast_dump_context* ast_dump_context) const
1487 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1492 // The type as determined by context.
1499 Boolean_expression::do_type()
1501 if (this->type_ == NULL)
1502 this->type_ = Type::make_boolean_type();
1506 // Set the type from the context.
1509 Boolean_expression::do_determine_type(const Type_context* context)
1511 if (this->type_ != NULL && !this->type_->is_abstract())
1513 else if (context->type != NULL && context->type->is_boolean_type())
1514 this->type_ = context->type;
1515 else if (!context->may_be_abstract)
1516 this->type_ = Type::lookup_bool_type();
1519 // Import a boolean constant.
1522 Boolean_expression::do_import(Import* imp)
1524 if (imp->peek_char() == 't')
1526 imp->require_c_string("true");
1527 return Expression::make_boolean(true, imp->location());
1531 imp->require_c_string("false");
1532 return Expression::make_boolean(false, imp->location());
1536 // Make a boolean expression.
1539 Expression::make_boolean(bool val, Location location)
1541 return new Boolean_expression(val, location);
1544 // Class String_expression.
1549 String_expression::do_type()
1551 if (this->type_ == NULL)
1552 this->type_ = Type::make_string_type();
1556 // Set the type from the context.
1559 String_expression::do_determine_type(const Type_context* context)
1561 if (this->type_ != NULL && !this->type_->is_abstract())
1563 else if (context->type != NULL && context->type->is_string_type())
1564 this->type_ = context->type;
1565 else if (!context->may_be_abstract)
1566 this->type_ = Type::lookup_string_type();
1569 // Build a string constant.
1572 String_expression::do_get_tree(Translate_context* context)
1574 return context->gogo()->go_string_constant_tree(this->val_);
1577 // Write string literal to string dump.
1580 String_expression::export_string(String_dump* exp,
1581 const String_expression* str)
1584 s.reserve(str->val_.length() * 4 + 2);
1586 for (std::string::const_iterator p = str->val_.begin();
1587 p != str->val_.end();
1590 if (*p == '\\' || *p == '"')
1595 else if (*p >= 0x20 && *p < 0x7f)
1597 else if (*p == '\n')
1599 else if (*p == '\t')
1604 unsigned char c = *p;
1605 unsigned int dig = c >> 4;
1606 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1608 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1612 exp->write_string(s);
1615 // Export a string expression.
1618 String_expression::do_export(Export* exp) const
1620 String_expression::export_string(exp, this);
1623 // Import a string expression.
1626 String_expression::do_import(Import* imp)
1628 imp->require_c_string("\"");
1632 int c = imp->get_char();
1633 if (c == '"' || c == -1)
1636 val += static_cast<char>(c);
1639 c = imp->get_char();
1640 if (c == '\\' || c == '"')
1641 val += static_cast<char>(c);
1648 c = imp->get_char();
1649 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1650 c = imp->get_char();
1651 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1652 char v = (vh << 4) | vl;
1657 error_at(imp->location(), "bad string constant");
1658 return Expression::make_error(imp->location());
1662 return Expression::make_string(val, imp->location());
1665 // Ast dump for string expression.
1668 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1670 String_expression::export_string(ast_dump_context, this);
1673 // Make a string expression.
1676 Expression::make_string(const std::string& val, Location location)
1678 return new String_expression(val, location);
1681 // Make an integer expression.
1683 class Integer_expression : public Expression
1686 Integer_expression(const mpz_t* val, Type* type, Location location)
1687 : Expression(EXPRESSION_INTEGER, location),
1689 { mpz_init_set(this->val_, *val); }
1694 // Return whether VAL fits in the type.
1696 check_constant(mpz_t val, Type*, Location);
1698 // Write VAL to string dump.
1700 export_integer(String_dump* exp, const mpz_t val);
1702 // Write VAL to dump context.
1704 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1708 do_is_constant() const
1712 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1718 do_determine_type(const Type_context* context);
1721 do_check_types(Gogo*);
1724 do_get_tree(Translate_context*);
1728 { return Expression::make_integer(&this->val_, this->type_,
1729 this->location()); }
1732 do_export(Export*) const;
1735 do_dump_expression(Ast_dump_context*) const;
1738 // The integer value.
1744 // Return an integer constant value.
1747 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1750 if (this->type_ != NULL)
1751 *ptype = this->type_;
1752 mpz_set(val, this->val_);
1756 // Return the current type. If we haven't set the type yet, we return
1757 // an abstract integer type.
1760 Integer_expression::do_type()
1762 if (this->type_ == NULL)
1763 this->type_ = Type::make_abstract_integer_type();
1767 // Set the type of the integer value. Here we may switch from an
1768 // abstract type to a real type.
1771 Integer_expression::do_determine_type(const Type_context* context)
1773 if (this->type_ != NULL && !this->type_->is_abstract())
1775 else if (context->type != NULL
1776 && (context->type->integer_type() != NULL
1777 || context->type->float_type() != NULL
1778 || context->type->complex_type() != NULL))
1779 this->type_ = context->type;
1780 else if (!context->may_be_abstract)
1781 this->type_ = Type::lookup_integer_type("int");
1784 // Return true if the integer VAL fits in the range of the type TYPE.
1785 // Otherwise give an error and return false. TYPE may be NULL.
1788 Integer_expression::check_constant(mpz_t val, Type* type,
1793 Integer_type* itype = type->integer_type();
1794 if (itype == NULL || itype->is_abstract())
1797 int bits = mpz_sizeinbase(val, 2);
1799 if (itype->is_unsigned())
1801 // For an unsigned type we can only accept a nonnegative number,
1802 // and we must be able to represent at least BITS.
1803 if (mpz_sgn(val) >= 0
1804 && bits <= itype->bits())
1809 // For a signed type we need an extra bit to indicate the sign.
1810 // We have to handle the most negative integer specially.
1811 if (bits + 1 <= itype->bits()
1812 || (bits <= itype->bits()
1814 && (mpz_scan1(val, 0)
1815 == static_cast<unsigned long>(itype->bits() - 1))
1816 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1820 error_at(location, "integer constant overflow");
1824 // Check the type of an integer constant.
1827 Integer_expression::do_check_types(Gogo*)
1829 if (this->type_ == NULL)
1831 if (!Integer_expression::check_constant(this->val_, this->type_,
1833 this->set_is_error();
1836 // Get a tree for an integer constant.
1839 Integer_expression::do_get_tree(Translate_context* context)
1841 Gogo* gogo = context->gogo();
1843 if (this->type_ != NULL && !this->type_->is_abstract())
1844 type = type_to_tree(this->type_->get_backend(gogo));
1845 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1847 // We are converting to an abstract floating point type.
1848 Type* ftype = Type::lookup_float_type("float64");
1849 type = type_to_tree(ftype->get_backend(gogo));
1851 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1853 // We are converting to an abstract complex type.
1854 Type* ctype = Type::lookup_complex_type("complex128");
1855 type = type_to_tree(ctype->get_backend(gogo));
1859 // If we still have an abstract type here, then this is being
1860 // used in a constant expression which didn't get reduced for
1861 // some reason. Use a type which will fit the value. We use <,
1862 // not <=, because we need an extra bit for the sign bit.
1863 int bits = mpz_sizeinbase(this->val_, 2);
1864 if (bits < INT_TYPE_SIZE)
1866 Type* t = Type::lookup_integer_type("int");
1867 type = type_to_tree(t->get_backend(gogo));
1871 Type* t = Type::lookup_integer_type("int64");
1872 type = type_to_tree(t->get_backend(gogo));
1875 type = long_long_integer_type_node;
1877 return Expression::integer_constant_tree(this->val_, type);
1880 // Write VAL to export data.
1883 Integer_expression::export_integer(String_dump* exp, const mpz_t val)
1885 char* s = mpz_get_str(NULL, 10, val);
1886 exp->write_c_string(s);
1890 // Export an integer in a constant expression.
1893 Integer_expression::do_export(Export* exp) const
1895 Integer_expression::export_integer(exp, this->val_);
1896 // A trailing space lets us reliably identify the end of the number.
1897 exp->write_c_string(" ");
1900 // Import an integer, floating point, or complex value. This handles
1901 // all these types because they all start with digits.
1904 Integer_expression::do_import(Import* imp)
1906 std::string num = imp->read_identifier();
1907 imp->require_c_string(" ");
1908 if (!num.empty() && num[num.length() - 1] == 'i')
1911 size_t plus_pos = num.find('+', 1);
1912 size_t minus_pos = num.find('-', 1);
1914 if (plus_pos == std::string::npos)
1916 else if (minus_pos == std::string::npos)
1920 error_at(imp->location(), "bad number in import data: %qs",
1922 return Expression::make_error(imp->location());
1924 if (pos == std::string::npos)
1925 mpfr_set_ui(real, 0, GMP_RNDN);
1928 std::string real_str = num.substr(0, pos);
1929 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1931 error_at(imp->location(), "bad number in import data: %qs",
1933 return Expression::make_error(imp->location());
1937 std::string imag_str;
1938 if (pos == std::string::npos)
1941 imag_str = num.substr(pos);
1942 imag_str = imag_str.substr(0, imag_str.size() - 1);
1944 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1946 error_at(imp->location(), "bad number in import data: %qs",
1948 return Expression::make_error(imp->location());
1950 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1956 else if (num.find('.') == std::string::npos
1957 && num.find('E') == std::string::npos)
1960 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1962 error_at(imp->location(), "bad number in import data: %qs",
1964 return Expression::make_error(imp->location());
1966 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1973 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1975 error_at(imp->location(), "bad number in import data: %qs",
1977 return Expression::make_error(imp->location());
1979 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1984 // Ast dump for integer expression.
1987 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1989 Integer_expression::export_integer(ast_dump_context, this->val_);
1992 // Build a new integer value.
1995 Expression::make_integer(const mpz_t* val, Type* type,
1998 return new Integer_expression(val, type, location);
2003 class Float_expression : public Expression
2006 Float_expression(const mpfr_t* val, Type* type, Location location)
2007 : Expression(EXPRESSION_FLOAT, location),
2010 mpfr_init_set(this->val_, *val, GMP_RNDN);
2013 // Constrain VAL to fit into TYPE.
2015 constrain_float(mpfr_t val, Type* type);
2017 // Return whether VAL fits in the type.
2019 check_constant(mpfr_t val, Type*, Location);
2021 // Write VAL to export data.
2023 export_float(String_dump* exp, const mpfr_t val);
2025 // Write VAL to dump file.
2027 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
2031 do_is_constant() const
2035 do_float_constant_value(mpfr_t val, Type**) const;
2041 do_determine_type(const Type_context*);
2044 do_check_types(Gogo*);
2048 { return Expression::make_float(&this->val_, this->type_,
2049 this->location()); }
2052 do_get_tree(Translate_context*);
2055 do_export(Export*) const;
2058 do_dump_expression(Ast_dump_context*) const;
2061 // The floating point value.
2067 // Constrain VAL to fit into TYPE.
2070 Float_expression::constrain_float(mpfr_t val, Type* type)
2072 Float_type* ftype = type->float_type();
2073 if (ftype != NULL && !ftype->is_abstract())
2074 mpfr_prec_round(val, ftype->bits(), GMP_RNDN);
2077 // Return a floating point constant value.
2080 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2082 if (this->type_ != NULL)
2083 *ptype = this->type_;
2084 mpfr_set(val, this->val_, GMP_RNDN);
2088 // Return the current type. If we haven't set the type yet, we return
2089 // an abstract float type.
2092 Float_expression::do_type()
2094 if (this->type_ == NULL)
2095 this->type_ = Type::make_abstract_float_type();
2099 // Set the type of the float value. Here we may switch from an
2100 // abstract type to a real type.
2103 Float_expression::do_determine_type(const Type_context* context)
2105 if (this->type_ != NULL && !this->type_->is_abstract())
2107 else if (context->type != NULL
2108 && (context->type->integer_type() != NULL
2109 || context->type->float_type() != NULL
2110 || context->type->complex_type() != NULL))
2111 this->type_ = context->type;
2112 else if (!context->may_be_abstract)
2113 this->type_ = Type::lookup_float_type("float64");
2116 // Return true if the floating point value VAL fits in the range of
2117 // the type TYPE. Otherwise give an error and return false. TYPE may
2121 Float_expression::check_constant(mpfr_t val, Type* type,
2126 Float_type* ftype = type->float_type();
2127 if (ftype == NULL || ftype->is_abstract())
2130 // A NaN or Infinity always fits in the range of the type.
2131 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
2134 mp_exp_t exp = mpfr_get_exp(val);
2136 switch (ftype->bits())
2149 error_at(location, "floating point constant overflow");
2155 // Check the type of a float value.
2158 Float_expression::do_check_types(Gogo*)
2160 if (this->type_ == NULL)
2163 if (!Float_expression::check_constant(this->val_, this->type_,
2165 this->set_is_error();
2167 Integer_type* integer_type = this->type_->integer_type();
2168 if (integer_type != NULL)
2170 if (!mpfr_integer_p(this->val_))
2171 this->report_error(_("floating point constant truncated to integer"));
2174 go_assert(!integer_type->is_abstract());
2177 mpfr_get_z(ival, this->val_, GMP_RNDN);
2178 Integer_expression::check_constant(ival, integer_type,
2185 // Get a tree for a float constant.
2188 Float_expression::do_get_tree(Translate_context* context)
2190 Gogo* gogo = context->gogo();
2192 if (this->type_ != NULL && !this->type_->is_abstract())
2193 type = type_to_tree(this->type_->get_backend(gogo));
2194 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2196 // We have an abstract integer type. We just hope for the best.
2197 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2201 // If we still have an abstract type here, then this is being
2202 // used in a constant expression which didn't get reduced. We
2203 // just use float64 and hope for the best.
2204 Type* ft = Type::lookup_float_type("float64");
2205 type = type_to_tree(ft->get_backend(gogo));
2207 return Expression::float_constant_tree(this->val_, type);
2210 // Write a floating point number to a string dump.
2213 Float_expression::export_float(String_dump *exp, const mpfr_t val)
2216 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2218 exp->write_c_string("-");
2219 exp->write_c_string("0.");
2220 exp->write_c_string(*s == '-' ? s + 1 : s);
2223 snprintf(buf, sizeof buf, "E%ld", exponent);
2224 exp->write_c_string(buf);
2227 // Export a floating point number in a constant expression.
2230 Float_expression::do_export(Export* exp) const
2232 Float_expression::export_float(exp, this->val_);
2233 // A trailing space lets us reliably identify the end of the number.
2234 exp->write_c_string(" ");
2237 // Dump a floating point number to the dump file.
2240 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2242 Float_expression::export_float(ast_dump_context, this->val_);
2245 // Make a float expression.
2248 Expression::make_float(const mpfr_t* val, Type* type, Location location)
2250 return new Float_expression(val, type, location);
2255 class Complex_expression : public Expression
2258 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2260 : Expression(EXPRESSION_COMPLEX, location),
2263 mpfr_init_set(this->real_, *real, GMP_RNDN);
2264 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2267 // Constrain REAL/IMAG to fit into TYPE.
2269 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2271 // Return whether REAL/IMAG fits in the type.
2273 check_constant(mpfr_t real, mpfr_t imag, Type*, Location);
2275 // Write REAL/IMAG to string dump.
2277 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
2279 // Write REAL/IMAG to dump context.
2281 dump_complex(Ast_dump_context* ast_dump_context,
2282 const mpfr_t real, const mpfr_t val);
2286 do_is_constant() const
2290 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2296 do_determine_type(const Type_context*);
2299 do_check_types(Gogo*);
2304 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2309 do_get_tree(Translate_context*);
2312 do_export(Export*) const;
2315 do_dump_expression(Ast_dump_context*) const;
2320 // The imaginary part;
2322 // The type if known.
2326 // Constrain REAL/IMAG to fit into TYPE.
2329 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2331 Complex_type* ctype = type->complex_type();
2332 if (ctype != NULL && !ctype->is_abstract())
2334 mpfr_prec_round(real, ctype->bits() / 2, GMP_RNDN);
2335 mpfr_prec_round(imag, ctype->bits() / 2, GMP_RNDN);
2339 // Return a complex constant value.
2342 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2345 if (this->type_ != NULL)
2346 *ptype = this->type_;
2347 mpfr_set(real, this->real_, GMP_RNDN);
2348 mpfr_set(imag, this->imag_, GMP_RNDN);
2352 // Return the current type. If we haven't set the type yet, we return
2353 // an abstract complex type.
2356 Complex_expression::do_type()
2358 if (this->type_ == NULL)
2359 this->type_ = Type::make_abstract_complex_type();
2363 // Set the type of the complex value. Here we may switch from an
2364 // abstract type to a real type.
2367 Complex_expression::do_determine_type(const Type_context* context)
2369 if (this->type_ != NULL && !this->type_->is_abstract())
2371 else if (context->type != NULL
2372 && context->type->complex_type() != NULL)
2373 this->type_ = context->type;
2374 else if (!context->may_be_abstract)
2375 this->type_ = Type::lookup_complex_type("complex128");
2378 // Return true if the complex value REAL/IMAG fits in the range of the
2379 // type TYPE. Otherwise give an error and return false. TYPE may be
2383 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2388 Complex_type* ctype = type->complex_type();
2389 if (ctype == NULL || ctype->is_abstract())
2393 switch (ctype->bits())
2405 // A NaN or Infinity always fits in the range of the type.
2406 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2408 if (mpfr_get_exp(real) > max_exp)
2410 error_at(location, "complex real part constant overflow");
2415 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2417 if (mpfr_get_exp(imag) > max_exp)
2419 error_at(location, "complex imaginary part constant overflow");
2427 // Check the type of a complex value.
2430 Complex_expression::do_check_types(Gogo*)
2432 if (this->type_ == NULL)
2435 if (!Complex_expression::check_constant(this->real_, this->imag_,
2436 this->type_, this->location()))
2437 this->set_is_error();
2440 // Get a tree for a complex constant.
2443 Complex_expression::do_get_tree(Translate_context* context)
2445 Gogo* gogo = context->gogo();
2447 if (this->type_ != NULL && !this->type_->is_abstract())
2448 type = type_to_tree(this->type_->get_backend(gogo));
2451 // If we still have an abstract type here, this this is being
2452 // used in a constant expression which didn't get reduced. We
2453 // just use complex128 and hope for the best.
2454 Type* ct = Type::lookup_complex_type("complex128");
2455 type = type_to_tree(ct->get_backend(gogo));
2457 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2460 // Write REAL/IMAG to export data.
2463 Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
2466 if (!mpfr_zero_p(real))
2468 Float_expression::export_float(exp, real);
2469 if (mpfr_sgn(imag) > 0)
2470 exp->write_c_string("+");
2472 Float_expression::export_float(exp, imag);
2473 exp->write_c_string("i");
2476 // Export a complex number in a constant expression.
2479 Complex_expression::do_export(Export* exp) const
2481 Complex_expression::export_complex(exp, this->real_, this->imag_);
2482 // A trailing space lets us reliably identify the end of the number.
2483 exp->write_c_string(" ");
2486 // Dump a complex expression to the dump file.
2489 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2491 Complex_expression::export_complex(ast_dump_context,
2496 // Make a complex expression.
2499 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2502 return new Complex_expression(real, imag, type, location);
2505 // Find a named object in an expression.
2507 class Find_named_object : public Traverse
2510 Find_named_object(Named_object* no)
2511 : Traverse(traverse_expressions),
2512 no_(no), found_(false)
2515 // Whether we found the object.
2518 { return this->found_; }
2522 expression(Expression**);
2525 // The object we are looking for.
2527 // Whether we found it.
2531 // A reference to a const in an expression.
2533 class Const_expression : public Expression
2536 Const_expression(Named_object* constant, Location location)
2537 : Expression(EXPRESSION_CONST_REFERENCE, location),
2538 constant_(constant), type_(NULL), seen_(false)
2543 { return this->constant_; }
2545 // Check that the initializer does not refer to the constant itself.
2547 check_for_init_loop();
2551 do_traverse(Traverse*);
2554 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2557 do_is_constant() const
2561 do_integer_constant_value(bool, mpz_t val, Type**) const;
2564 do_float_constant_value(mpfr_t val, Type**) const;
2567 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2570 do_string_constant_value(std::string* val) const
2571 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2576 // The type of a const is set by the declaration, not the use.
2578 do_determine_type(const Type_context*);
2581 do_check_types(Gogo*);
2588 do_get_tree(Translate_context* context);
2590 // When exporting a reference to a const as part of a const
2591 // expression, we export the value. We ignore the fact that it has
2594 do_export(Export* exp) const
2595 { this->constant_->const_value()->expr()->export_expression(exp); }
2598 do_dump_expression(Ast_dump_context*) const;
2602 Named_object* constant_;
2603 // The type of this reference. This is used if the constant has an
2606 // Used to prevent infinite recursion when a constant incorrectly
2607 // refers to itself.
2614 Const_expression::do_traverse(Traverse* traverse)
2616 if (this->type_ != NULL)
2617 return Type::traverse(this->type_, traverse);
2618 return TRAVERSE_CONTINUE;
2621 // Lower a constant expression. This is where we convert the
2622 // predeclared constant iota into an integer value.
2625 Const_expression::do_lower(Gogo* gogo, Named_object*,
2626 Statement_inserter*, int iota_value)
2628 if (this->constant_->const_value()->expr()->classification()
2631 if (iota_value == -1)
2633 error_at(this->location(),
2634 "iota is only defined in const declarations");
2638 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2639 Expression* ret = Expression::make_integer(&val, NULL,
2645 // Make sure that the constant itself has been lowered.
2646 gogo->lower_constant(this->constant_);
2651 // Return an integer constant value.
2654 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2661 if (this->type_ != NULL)
2662 ctype = this->type_;
2664 ctype = this->constant_->const_value()->type();
2665 if (ctype != NULL && ctype->integer_type() == NULL)
2668 Expression* e = this->constant_->const_value()->expr();
2673 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2675 this->seen_ = false;
2679 && !Integer_expression::check_constant(val, ctype, this->location()))
2682 *ptype = ctype != NULL ? ctype : t;
2686 // Return a floating point constant value.
2689 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2695 if (this->type_ != NULL)
2696 ctype = this->type_;
2698 ctype = this->constant_->const_value()->type();
2699 if (ctype != NULL && ctype->float_type() == NULL)
2705 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2708 this->seen_ = false;
2710 if (r && ctype != NULL)
2712 if (!Float_expression::check_constant(val, ctype, this->location()))
2714 Float_expression::constrain_float(val, ctype);
2716 *ptype = ctype != NULL ? ctype : t;
2720 // Return a complex constant value.
2723 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2730 if (this->type_ != NULL)
2731 ctype = this->type_;
2733 ctype = this->constant_->const_value()->type();
2734 if (ctype != NULL && ctype->complex_type() == NULL)
2740 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2744 this->seen_ = false;
2746 if (r && ctype != NULL)
2748 if (!Complex_expression::check_constant(real, imag, ctype,
2751 Complex_expression::constrain_complex(real, imag, ctype);
2753 *ptype = ctype != NULL ? ctype : t;
2757 // Return the type of the const reference.
2760 Const_expression::do_type()
2762 if (this->type_ != NULL)
2765 Named_constant* nc = this->constant_->const_value();
2767 if (this->seen_ || nc->lowering())
2769 this->report_error(_("constant refers to itself"));
2770 this->type_ = Type::make_error_type();
2776 Type* ret = nc->type();
2780 this->seen_ = false;
2784 // During parsing, a named constant may have a NULL type, but we
2785 // must not return a NULL type here.
2786 ret = nc->expr()->type();
2788 this->seen_ = false;
2793 // Set the type of the const reference.
2796 Const_expression::do_determine_type(const Type_context* context)
2798 Type* ctype = this->constant_->const_value()->type();
2799 Type* cetype = (ctype != NULL
2801 : this->constant_->const_value()->expr()->type());
2802 if (ctype != NULL && !ctype->is_abstract())
2804 else if (context->type != NULL
2805 && (context->type->integer_type() != NULL
2806 || context->type->float_type() != NULL
2807 || context->type->complex_type() != NULL)
2808 && (cetype->integer_type() != NULL
2809 || cetype->float_type() != NULL
2810 || cetype->complex_type() != NULL))
2811 this->type_ = context->type;
2812 else if (context->type != NULL
2813 && context->type->is_string_type()
2814 && cetype->is_string_type())
2815 this->type_ = context->type;
2816 else if (context->type != NULL
2817 && context->type->is_boolean_type()
2818 && cetype->is_boolean_type())
2819 this->type_ = context->type;
2820 else if (!context->may_be_abstract)
2822 if (cetype->is_abstract())
2823 cetype = cetype->make_non_abstract_type();
2824 this->type_ = cetype;
2828 // Check for a loop in which the initializer of a constant refers to
2829 // the constant itself.
2832 Const_expression::check_for_init_loop()
2834 if (this->type_ != NULL && this->type_->is_error())
2839 this->report_error(_("constant refers to itself"));
2840 this->type_ = Type::make_error_type();
2844 Expression* init = this->constant_->const_value()->expr();
2845 Find_named_object find_named_object(this->constant_);
2848 Expression::traverse(&init, &find_named_object);
2849 this->seen_ = false;
2851 if (find_named_object.found())
2853 if (this->type_ == NULL || !this->type_->is_error())
2855 this->report_error(_("constant refers to itself"));
2856 this->type_ = Type::make_error_type();
2862 // Check types of a const reference.
2865 Const_expression::do_check_types(Gogo*)
2867 if (this->type_ != NULL && this->type_->is_error())
2870 this->check_for_init_loop();
2872 if (this->type_ == NULL || this->type_->is_abstract())
2875 // Check for integer overflow.
2876 if (this->type_->integer_type() != NULL)
2881 if (!this->integer_constant_value(true, ival, &dummy))
2885 Expression* cexpr = this->constant_->const_value()->expr();
2886 if (cexpr->float_constant_value(fval, &dummy))
2888 if (!mpfr_integer_p(fval))
2889 this->report_error(_("floating point constant "
2890 "truncated to integer"));
2893 mpfr_get_z(ival, fval, GMP_RNDN);
2894 Integer_expression::check_constant(ival, this->type_,
2904 // Return a tree for the const reference.
2907 Const_expression::do_get_tree(Translate_context* context)
2909 Gogo* gogo = context->gogo();
2911 if (this->type_ == NULL)
2912 type_tree = NULL_TREE;
2915 type_tree = type_to_tree(this->type_->get_backend(gogo));
2916 if (type_tree == error_mark_node)
2917 return error_mark_node;
2920 // If the type has been set for this expression, but the underlying
2921 // object is an abstract int or float, we try to get the abstract
2922 // value. Otherwise we may lose something in the conversion.
2923 if (this->type_ != NULL
2924 && (this->constant_->const_value()->type() == NULL
2925 || this->constant_->const_value()->type()->is_abstract()))
2927 Expression* expr = this->constant_->const_value()->expr();
2931 if (expr->integer_constant_value(true, ival, &t))
2933 tree ret = Expression::integer_constant_tree(ival, type_tree);
2941 if (expr->float_constant_value(fval, &t))
2943 tree ret = Expression::float_constant_tree(fval, type_tree);
2950 if (expr->complex_constant_value(fval, imag, &t))
2952 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2961 tree const_tree = this->constant_->get_tree(gogo, context->function());
2962 if (this->type_ == NULL
2963 || const_tree == error_mark_node
2964 || TREE_TYPE(const_tree) == error_mark_node)
2968 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2969 ret = fold_convert(type_tree, const_tree);
2970 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2971 ret = fold(convert_to_integer(type_tree, const_tree));
2972 else if (TREE_CODE(type_tree) == REAL_TYPE)
2973 ret = fold(convert_to_real(type_tree, const_tree));
2974 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2975 ret = fold(convert_to_complex(type_tree, const_tree));
2981 // Dump ast representation for constant expression.
2984 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2986 ast_dump_context->ostream() << this->constant_->name();
2989 // Make a reference to a constant in an expression.
2992 Expression::make_const_reference(Named_object* constant,
2995 return new Const_expression(constant, location);
2998 // Find a named object in an expression.
3001 Find_named_object::expression(Expression** pexpr)
3003 switch ((*pexpr)->classification())
3005 case Expression::EXPRESSION_CONST_REFERENCE:
3007 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
3008 if (ce->named_object() == this->no_)
3011 // We need to check a constant initializer explicitly, as
3012 // loops here will not be caught by the loop checking for
3013 // variable initializers.
3014 ce->check_for_init_loop();
3016 return TRAVERSE_CONTINUE;
3019 case Expression::EXPRESSION_VAR_REFERENCE:
3020 if ((*pexpr)->var_expression()->named_object() == this->no_)
3022 return TRAVERSE_CONTINUE;
3023 case Expression::EXPRESSION_FUNC_REFERENCE:
3024 if ((*pexpr)->func_expression()->named_object() == this->no_)
3026 return TRAVERSE_CONTINUE;
3028 return TRAVERSE_CONTINUE;
3030 this->found_ = true;
3031 return TRAVERSE_EXIT;
3036 class Nil_expression : public Expression
3039 Nil_expression(Location location)
3040 : Expression(EXPRESSION_NIL, location)
3048 do_is_constant() const
3053 { return Type::make_nil_type(); }
3056 do_determine_type(const Type_context*)
3064 do_get_tree(Translate_context*)
3065 { return null_pointer_node; }
3068 do_export(Export* exp) const
3069 { exp->write_c_string("nil"); }
3072 do_dump_expression(Ast_dump_context* ast_dump_context) const
3073 { ast_dump_context->ostream() << "nil"; }
3076 // Import a nil expression.
3079 Nil_expression::do_import(Import* imp)
3081 imp->require_c_string("nil");
3082 return Expression::make_nil(imp->location());
3085 // Make a nil expression.
3088 Expression::make_nil(Location location)
3090 return new Nil_expression(location);
3093 // The value of the predeclared constant iota. This is little more
3094 // than a marker. This will be lowered to an integer in
3095 // Const_expression::do_lower, which is where we know the value that
3098 class Iota_expression : public Parser_expression
3101 Iota_expression(Location location)
3102 : Parser_expression(EXPRESSION_IOTA, location)
3107 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
3108 { go_unreachable(); }
3110 // There should only ever be one of these.
3113 { go_unreachable(); }
3116 do_dump_expression(Ast_dump_context* ast_dump_context) const
3117 { ast_dump_context->ostream() << "iota"; }
3120 // Make an iota expression. This is only called for one case: the
3121 // value of the predeclared constant iota.
3124 Expression::make_iota()
3126 static Iota_expression iota_expression(Linemap::unknown_location());
3127 return &iota_expression;
3130 // A type conversion expression.
3132 class Type_conversion_expression : public Expression
3135 Type_conversion_expression(Type* type, Expression* expr,
3137 : Expression(EXPRESSION_CONVERSION, location),
3138 type_(type), expr_(expr), may_convert_function_types_(false)
3141 // Return the type to which we are converting.
3144 { return this->type_; }
3146 // Return the expression which we are converting.
3149 { return this->expr_; }
3151 // Permit converting from one function type to another. This is
3152 // used internally for method expressions.
3154 set_may_convert_function_types()
3156 this->may_convert_function_types_ = true;
3159 // Import a type conversion expression.
3165 do_traverse(Traverse* traverse);
3168 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3171 do_is_constant() const
3172 { return this->expr_->is_constant(); }
3175 do_integer_constant_value(bool, mpz_t, Type**) const;
3178 do_float_constant_value(mpfr_t, Type**) const;
3181 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3184 do_string_constant_value(std::string*) const;
3188 { return this->type_; }
3191 do_determine_type(const Type_context*)
3193 Type_context subcontext(this->type_, false);
3194 this->expr_->determine_type(&subcontext);
3198 do_check_types(Gogo*);
3203 return new Type_conversion_expression(this->type_, this->expr_->copy(),
3208 do_get_tree(Translate_context* context);
3211 do_export(Export*) const;
3214 do_dump_expression(Ast_dump_context*) const;
3217 // The type to convert to.
3219 // The expression to convert.
3221 // True if this is permitted to convert function types. This is
3222 // used internally for method expressions.
3223 bool may_convert_function_types_;
3229 Type_conversion_expression::do_traverse(Traverse* traverse)
3231 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3232 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3233 return TRAVERSE_EXIT;
3234 return TRAVERSE_CONTINUE;
3237 // Convert to a constant at lowering time.
3240 Type_conversion_expression::do_lower(Gogo*, Named_object*,
3241 Statement_inserter*, int)
3243 Type* type = this->type_;
3244 Expression* val = this->expr_;
3245 Location location = this->location();
3247 if (type->integer_type() != NULL)
3252 if (val->integer_constant_value(false, ival, &dummy))
3254 if (!Integer_expression::check_constant(ival, type, location))
3255 mpz_set_ui(ival, 0);
3256 Expression* ret = Expression::make_integer(&ival, type, location);
3263 if (val->float_constant_value(fval, &dummy))
3265 if (!mpfr_integer_p(fval))
3268 "floating point constant truncated to integer");
3269 return Expression::make_error(location);
3271 mpfr_get_z(ival, fval, GMP_RNDN);
3272 if (!Integer_expression::check_constant(ival, type, location))
3273 mpz_set_ui(ival, 0);
3274 Expression* ret = Expression::make_integer(&ival, type, location);
3283 if (type->float_type() != NULL)
3288 if (val->float_constant_value(fval, &dummy))
3290 if (!Float_expression::check_constant(fval, type, location))
3291 mpfr_set_ui(fval, 0, GMP_RNDN);
3292 Float_expression::constrain_float(fval, type);
3293 Expression *ret = Expression::make_float(&fval, type, location);
3300 if (type->complex_type() != NULL)
3307 if (val->complex_constant_value(real, imag, &dummy))
3309 if (!Complex_expression::check_constant(real, imag, type, location))
3311 mpfr_set_ui(real, 0, GMP_RNDN);
3312 mpfr_set_ui(imag, 0, GMP_RNDN);
3314 Complex_expression::constrain_complex(real, imag, type);
3315 Expression* ret = Expression::make_complex(&real, &imag, type,
3325 if (type->is_slice_type())
3327 Type* element_type = type->array_type()->element_type()->forwarded();
3328 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3329 bool is_int = element_type == Type::lookup_integer_type("int");
3330 if (is_byte || is_int)
3333 if (val->string_constant_value(&s))
3335 Expression_list* vals = new Expression_list();
3338 for (std::string::const_iterator p = s.begin();
3343 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3344 Expression* v = Expression::make_integer(&val,
3353 const char *p = s.data();
3354 const char *pend = s.data() + s.length();
3358 int adv = Lex::fetch_char(p, &c);
3361 warning_at(this->location(), 0,
3362 "invalid UTF-8 encoding");
3367 mpz_init_set_ui(val, c);
3368 Expression* v = Expression::make_integer(&val,
3376 return Expression::make_slice_composite_literal(type, vals,
3385 // Return the constant integer value if there is one.
3388 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3392 if (this->type_->integer_type() == NULL)
3398 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3400 if (!Integer_expression::check_constant(ival, this->type_,
3408 *ptype = this->type_;
3415 if (this->expr_->float_constant_value(fval, &dummy))
3417 mpfr_get_z(val, fval, GMP_RNDN);
3419 if (!Integer_expression::check_constant(val, this->type_,
3422 *ptype = this->type_;
3430 // Return the constant floating point value if there is one.
3433 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3436 if (this->type_->float_type() == NULL)
3442 if (this->expr_->float_constant_value(fval, &dummy))
3444 if (!Float_expression::check_constant(fval, this->type_,
3450 mpfr_set(val, fval, GMP_RNDN);
3452 Float_expression::constrain_float(val, this->type_);
3453 *ptype = this->type_;
3461 // Return the constant complex value if there is one.
3464 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3468 if (this->type_->complex_type() == NULL)
3476 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3478 if (!Complex_expression::check_constant(rval, ival, this->type_,
3485 mpfr_set(real, rval, GMP_RNDN);
3486 mpfr_set(imag, ival, GMP_RNDN);
3489 Complex_expression::constrain_complex(real, imag, this->type_);
3490 *ptype = this->type_;
3499 // Return the constant string value if there is one.
3502 Type_conversion_expression::do_string_constant_value(std::string* val) const
3504 if (this->type_->is_string_type()
3505 && this->expr_->type()->integer_type() != NULL)
3510 if (this->expr_->integer_constant_value(false, ival, &dummy))
3512 unsigned long ulval = mpz_get_ui(ival);
3513 if (mpz_cmp_ui(ival, ulval) == 0)
3515 Lex::append_char(ulval, true, val, this->location());
3523 // FIXME: Could handle conversion from const []int here.
3528 // Check that types are convertible.
3531 Type_conversion_expression::do_check_types(Gogo*)
3533 Type* type = this->type_;
3534 Type* expr_type = this->expr_->type();
3537 if (type->is_error() || expr_type->is_error())
3539 this->set_is_error();
3543 if (this->may_convert_function_types_
3544 && type->function_type() != NULL
3545 && expr_type->function_type() != NULL)
3548 if (Type::are_convertible(type, expr_type, &reason))
3551 error_at(this->location(), "%s", reason.c_str());
3552 this->set_is_error();
3555 // Get a tree for a type conversion.
3558 Type_conversion_expression::do_get_tree(Translate_context* context)
3560 Gogo* gogo = context->gogo();
3561 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
3562 tree expr_tree = this->expr_->get_tree(context);
3564 if (type_tree == error_mark_node
3565 || expr_tree == error_mark_node
3566 || TREE_TYPE(expr_tree) == error_mark_node)
3567 return error_mark_node;
3569 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3570 return fold_convert(type_tree, expr_tree);
3572 Type* type = this->type_;
3573 Type* expr_type = this->expr_->type();
3575 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3576 ret = Expression::convert_for_assignment(context, type, expr_type,
3577 expr_tree, this->location());
3578 else if (type->integer_type() != NULL)
3580 if (expr_type->integer_type() != NULL
3581 || expr_type->float_type() != NULL
3582 || expr_type->is_unsafe_pointer_type())
3583 ret = fold(convert_to_integer(type_tree, expr_tree));
3587 else if (type->float_type() != NULL)
3589 if (expr_type->integer_type() != NULL
3590 || expr_type->float_type() != NULL)
3591 ret = fold(convert_to_real(type_tree, expr_tree));
3595 else if (type->complex_type() != NULL)
3597 if (expr_type->complex_type() != NULL)
3598 ret = fold(convert_to_complex(type_tree, expr_tree));
3602 else if (type->is_string_type()
3603 && expr_type->integer_type() != NULL)
3605 expr_tree = fold_convert(integer_type_node, expr_tree);
3606 if (host_integerp(expr_tree, 0))
3608 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3610 Lex::append_char(intval, true, &s, this->location());
3611 Expression* se = Expression::make_string(s, this->location());
3612 return se->get_tree(context);
3615 static tree int_to_string_fndecl;
3616 ret = Gogo::call_builtin(&int_to_string_fndecl,
3618 "__go_int_to_string",
3622 fold_convert(integer_type_node, expr_tree));
3624 else if (type->is_string_type() && expr_type->is_slice_type())
3626 if (!DECL_P(expr_tree))
3627 expr_tree = save_expr(expr_tree);
3628 Array_type* a = expr_type->array_type();
3629 Type* e = a->element_type()->forwarded();
3630 go_assert(e->integer_type() != NULL);
3631 tree valptr = fold_convert(const_ptr_type_node,
3632 a->value_pointer_tree(gogo, expr_tree));
3633 tree len = a->length_tree(gogo, expr_tree);
3634 len = fold_convert_loc(this->location().gcc_location(), integer_type_node,
3636 if (e->integer_type()->is_unsigned()
3637 && e->integer_type()->bits() == 8)
3639 static tree byte_array_to_string_fndecl;
3640 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3642 "__go_byte_array_to_string",
3645 const_ptr_type_node,
3652 go_assert(e == Type::lookup_integer_type("int"));
3653 static tree int_array_to_string_fndecl;
3654 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3656 "__go_int_array_to_string",
3659 const_ptr_type_node,
3665 else if (type->is_slice_type() && expr_type->is_string_type())
3667 Type* e = type->array_type()->element_type()->forwarded();
3668 go_assert(e->integer_type() != NULL);
3669 if (e->integer_type()->is_unsigned()
3670 && e->integer_type()->bits() == 8)
3672 static tree string_to_byte_array_fndecl;
3673 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3675 "__go_string_to_byte_array",
3678 TREE_TYPE(expr_tree),
3683 go_assert(e == Type::lookup_integer_type("int"));
3684 static tree string_to_int_array_fndecl;
3685 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3687 "__go_string_to_int_array",
3690 TREE_TYPE(expr_tree),
3694 else if ((type->is_unsafe_pointer_type()
3695 && expr_type->points_to() != NULL)
3696 || (expr_type->is_unsafe_pointer_type()
3697 && type->points_to() != NULL))
3698 ret = fold_convert(type_tree, expr_tree);
3699 else if (type->is_unsafe_pointer_type()
3700 && expr_type->integer_type() != NULL)
3701 ret = convert_to_pointer(type_tree, expr_tree);
3702 else if (this->may_convert_function_types_
3703 && type->function_type() != NULL
3704 && expr_type->function_type() != NULL)
3705 ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3708 ret = Expression::convert_for_assignment(context, type, expr_type,
3709 expr_tree, this->location());
3714 // Output a type conversion in a constant expression.
3717 Type_conversion_expression::do_export(Export* exp) const
3719 exp->write_c_string("convert(");
3720 exp->write_type(this->type_);
3721 exp->write_c_string(", ");
3722 this->expr_->export_expression(exp);
3723 exp->write_c_string(")");
3726 // Import a type conversion or a struct construction.
3729 Type_conversion_expression::do_import(Import* imp)
3731 imp->require_c_string("convert(");
3732 Type* type = imp->read_type();
3733 imp->require_c_string(", ");
3734 Expression* val = Expression::import_expression(imp);
3735 imp->require_c_string(")");
3736 return Expression::make_cast(type, val, imp->location());
3739 // Dump ast representation for a type conversion expression.
3742 Type_conversion_expression::do_dump_expression(
3743 Ast_dump_context* ast_dump_context) const
3745 ast_dump_context->dump_type(this->type_);
3746 ast_dump_context->ostream() << "(";
3747 ast_dump_context->dump_expression(this->expr_);
3748 ast_dump_context->ostream() << ") ";
3751 // Make a type cast expression.
3754 Expression::make_cast(Type* type, Expression* val, Location location)
3756 if (type->is_error_type() || val->is_error_expression())
3757 return Expression::make_error(location);
3758 return new Type_conversion_expression(type, val, location);
3761 // An unsafe type conversion, used to pass values to builtin functions.
3763 class Unsafe_type_conversion_expression : public Expression
3766 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3768 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3769 type_(type), expr_(expr)
3774 do_traverse(Traverse* traverse);
3778 { return this->type_; }
3781 do_determine_type(const Type_context*)
3782 { this->expr_->determine_type_no_context(); }
3787 return new Unsafe_type_conversion_expression(this->type_,
3788 this->expr_->copy(),
3793 do_get_tree(Translate_context*);
3796 do_dump_expression(Ast_dump_context*) const;
3799 // The type to convert to.
3801 // The expression to convert.
3808 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3810 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3811 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3812 return TRAVERSE_EXIT;
3813 return TRAVERSE_CONTINUE;
3816 // Convert to backend representation.
3819 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3821 // We are only called for a limited number of cases.
3823 Type* t = this->type_;
3824 Type* et = this->expr_->type();
3826 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
3827 tree expr_tree = this->expr_->get_tree(context);
3828 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3829 return error_mark_node;
3831 Location loc = this->location();
3833 bool use_view_convert = false;
3834 if (t->is_slice_type())
3836 go_assert(et->is_slice_type());
3837 use_view_convert = true;
3839 else if (t->map_type() != NULL)
3840 go_assert(et->map_type() != NULL);
3841 else if (t->channel_type() != NULL)
3842 go_assert(et->channel_type() != NULL);
3843 else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL)
3844 go_assert((et->points_to() != NULL
3845 && et->points_to()->channel_type() != NULL)
3846 || et->is_nil_type());
3847 else if (t->points_to() != NULL)
3848 go_assert(et->points_to() != NULL || et->is_nil_type());
3849 else if (et->is_unsafe_pointer_type())
3850 go_assert(t->points_to() != NULL);
3851 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3853 go_assert(et->interface_type() != NULL
3854 && !et->interface_type()->is_empty());
3855 use_view_convert = true;
3857 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3859 go_assert(et->interface_type() != NULL
3860 && et->interface_type()->is_empty());
3861 use_view_convert = true;
3863 else if (t->integer_type() != NULL)
3865 go_assert(et->is_boolean_type()
3866 || et->integer_type() != NULL
3867 || et->function_type() != NULL
3868 || et->points_to() != NULL
3869 || et->map_type() != NULL
3870 || et->channel_type() != NULL);
3871 return convert_to_integer(type_tree, expr_tree);
3876 if (use_view_convert)
3877 return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3880 return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
3883 // Dump ast representation for an unsafe type conversion expression.
3886 Unsafe_type_conversion_expression::do_dump_expression(
3887 Ast_dump_context* ast_dump_context) const
3889 ast_dump_context->dump_type(this->type_);
3890 ast_dump_context->ostream() << "(";
3891 ast_dump_context->dump_expression(this->expr_);
3892 ast_dump_context->ostream() << ") ";
3895 // Make an unsafe type conversion expression.
3898 Expression::make_unsafe_cast(Type* type, Expression* expr,
3901 return new Unsafe_type_conversion_expression(type, expr, location);
3904 // Unary expressions.
3906 class Unary_expression : public Expression
3909 Unary_expression(Operator op, Expression* expr, Location location)
3910 : Expression(EXPRESSION_UNARY, location),
3911 op_(op), escapes_(true), create_temp_(false), expr_(expr)
3914 // Return the operator.
3917 { return this->op_; }
3919 // Return the operand.
3922 { return this->expr_; }
3924 // Record that an address expression does not escape.
3926 set_does_not_escape()
3928 go_assert(this->op_ == OPERATOR_AND);
3929 this->escapes_ = false;
3932 // Record that this is an address expression which should create a
3933 // temporary variable if necessary. This is used for method calls.
3937 go_assert(this->op_ == OPERATOR_AND);
3938 this->create_temp_ = true;
3941 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3942 // could be done, false if not.
3944 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3947 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3948 // could be done, false if not.
3950 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3952 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3953 // true if this could be done, false if not.
3955 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3963 do_traverse(Traverse* traverse)
3964 { return Expression::traverse(&this->expr_, traverse); }
3967 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3970 do_is_constant() const;
3973 do_integer_constant_value(bool, mpz_t, Type**) const;
3976 do_float_constant_value(mpfr_t, Type**) const;
3979 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3985 do_determine_type(const Type_context*);
3988 do_check_types(Gogo*);
3993 return Expression::make_unary(this->op_, this->expr_->copy(),
3998 do_must_eval_subexpressions_in_order(int*) const
3999 { return this->op_ == OPERATOR_MULT; }
4002 do_is_addressable() const
4003 { return this->op_ == OPERATOR_MULT; }
4006 do_get_tree(Translate_context*);
4009 do_export(Export*) const;
4012 do_dump_expression(Ast_dump_context*) const;
4015 // The unary operator to apply.
4017 // Normally true. False if this is an address expression which does
4018 // not escape the current function.
4020 // True if this is an address expression which should create a
4021 // temporary variable if necessary.
4027 // If we are taking the address of a composite literal, and the
4028 // contents are not constant, then we want to make a heap composite
4032 Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
4034 Location loc = this->location();
4035 Operator op = this->op_;
4036 Expression* expr = this->expr_;
4038 if (op == OPERATOR_MULT && expr->is_type_expression())
4039 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
4041 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
4042 // moving x to the heap. FIXME: Is it worth doing a real escape
4043 // analysis here? This case is found in math/unsafe.go and is
4044 // therefore worth special casing.
4045 if (op == OPERATOR_MULT)
4047 Expression* e = expr;
4048 while (e->classification() == EXPRESSION_CONVERSION)
4050 Type_conversion_expression* te
4051 = static_cast<Type_conversion_expression*>(e);
4055 if (e->classification() == EXPRESSION_UNARY)
4057 Unary_expression* ue = static_cast<Unary_expression*>(e);
4058 if (ue->op_ == OPERATOR_AND)
4065 ue->set_does_not_escape();
4070 // Catching an invalid indirection of unsafe.Pointer here avoid
4071 // having to deal with TYPE_VOID in other places.
4072 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
4074 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
4075 return Expression::make_error(this->location());
4078 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
4079 || op == OPERATOR_NOT || op == OPERATOR_XOR)
4081 Expression* ret = NULL;
4086 if (expr->integer_constant_value(false, eval, &etype))
4090 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
4091 ret = Expression::make_integer(&val, etype, loc);
4098 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
4103 if (expr->float_constant_value(fval, &ftype))
4107 if (Unary_expression::eval_float(op, fval, val))
4108 ret = Expression::make_float(&val, ftype, loc);
4119 if (expr->complex_constant_value(fval, ival, &ftype))