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,
44 source_location location)
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,
206 source_location location)
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, 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, VIEW_CONVERT_EXPR, lhs_type_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, source_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, const_ptr_type_node,
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, TREE_TYPE(field), first_field_value);
376 elt = VEC_quick_push(constructor_elt, init, NULL);
377 field = DECL_CHAIN(field);
378 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
381 if (rhs_type->points_to() != NULL)
383 // We are assigning a pointer to the interface; the interface
384 // holds the pointer itself.
385 elt->value = rhs_tree;
386 return build_constructor(lhs_type_tree, init);
389 // We are assigning a non-pointer value to the interface; the
390 // interface gets a copy of the value in the heap.
392 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
394 tree space = gogo->allocate_memory(rhs_type, object_size, location);
395 space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)),
397 space = save_expr(space);
399 tree ref = build_fold_indirect_ref_loc(location, space);
400 TREE_THIS_NOTRAP(ref) = 1;
401 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
404 elt->value = fold_convert_loc(location, TREE_TYPE(field), space);
406 return build2(COMPOUND_EXPR, lhs_type_tree, set,
407 build_constructor(lhs_type_tree, init));
410 // Return a tree for the type descriptor of RHS_TREE, which has
411 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
415 Expression::get_interface_type_descriptor(Translate_context*,
416 Type* rhs_type, tree rhs_tree,
417 source_location location)
419 tree rhs_type_tree = TREE_TYPE(rhs_tree);
420 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
421 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
422 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
424 if (rhs_type->interface_type()->is_empty())
426 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
427 "__type_descriptor") == 0);
431 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
433 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
435 tree v1 = build_fold_indirect_ref_loc(location, v);
436 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
437 tree f = TYPE_FIELDS(TREE_TYPE(v1));
438 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
440 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
442 tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v,
443 fold_convert_loc(location, TREE_TYPE(v),
445 tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node);
446 return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1),
450 // Return a tree for the conversion of an interface type to an
454 Expression::convert_interface_to_interface(Translate_context* context,
455 Type *lhs_type, Type *rhs_type,
456 tree rhs_tree, bool for_type_guard,
457 source_location location)
459 Gogo* gogo = context->gogo();
460 Interface_type* lhs_interface_type = lhs_type->interface_type();
461 bool lhs_is_empty = lhs_interface_type->is_empty();
463 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
464 if (lhs_type_tree == error_mark_node)
465 return error_mark_node;
467 // In the general case this requires runtime examination of the type
468 // method table to match it up with the interface methods.
470 // FIXME: If all of the methods in the right hand side interface
471 // also appear in the left hand side interface, then we don't need
472 // to do a runtime check, although we still need to build a new
475 // Get the type descriptor for the right hand side. This will be
476 // NULL for a nil interface.
478 if (!DECL_P(rhs_tree))
479 rhs_tree = save_expr(rhs_tree);
481 tree rhs_type_descriptor =
482 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
485 // The result is going to be a two element constructor.
487 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
489 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
490 tree field = TYPE_FIELDS(lhs_type_tree);
495 // A type assertion fails when converting a nil interface.
496 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
498 static tree assert_interface_decl;
499 tree call = Gogo::call_builtin(&assert_interface_decl,
501 "__go_assert_interface",
504 TREE_TYPE(lhs_type_descriptor),
506 TREE_TYPE(rhs_type_descriptor),
507 rhs_type_descriptor);
508 if (call == error_mark_node)
509 return error_mark_node;
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 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
519 "__type_descriptor") == 0);
520 go_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 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
529 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
531 static tree convert_interface_decl;
532 tree call = Gogo::call_builtin(&convert_interface_decl,
534 "__go_convert_interface",
537 TREE_TYPE(lhs_type_descriptor),
539 TREE_TYPE(rhs_type_descriptor),
540 rhs_type_descriptor);
541 if (call == error_mark_node)
542 return error_mark_node;
543 // This will panic if the interface conversion fails.
544 TREE_NOTHROW(convert_interface_decl) = 0;
545 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
548 // The second field is simply the object pointer.
550 elt = VEC_quick_push(constructor_elt, init, NULL);
551 field = DECL_CHAIN(field);
552 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
555 tree rhs_type_tree = TREE_TYPE(rhs_tree);
556 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
557 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
558 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
559 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
562 return build_constructor(lhs_type_tree, init);
565 // Return a tree for the conversion of an interface type to a
566 // non-interface type.
569 Expression::convert_interface_to_type(Translate_context* context,
570 Type *lhs_type, Type* rhs_type,
571 tree rhs_tree, source_location location)
573 Gogo* gogo = context->gogo();
574 tree rhs_type_tree = TREE_TYPE(rhs_tree);
576 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
577 if (lhs_type_tree == error_mark_node)
578 return error_mark_node;
580 // Call a function to check that the type is valid. The function
581 // will panic with an appropriate runtime type error if the type is
584 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location);
586 if (!DECL_P(rhs_tree))
587 rhs_tree = save_expr(rhs_tree);
589 tree rhs_type_descriptor =
590 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
593 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo,
596 static tree check_interface_type_decl;
597 tree call = Gogo::call_builtin(&check_interface_type_decl,
599 "__go_check_interface_type",
602 TREE_TYPE(lhs_type_descriptor),
604 TREE_TYPE(rhs_type_descriptor),
606 TREE_TYPE(rhs_inter_descriptor),
607 rhs_inter_descriptor);
608 if (call == error_mark_node)
609 return error_mark_node;
610 // This call will panic if the conversion is invalid.
611 TREE_NOTHROW(check_interface_type_decl) = 0;
613 // If the call succeeds, pull out the value.
614 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
615 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
616 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
617 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
620 // If the value is a pointer, then it is the value we want.
621 // Otherwise it points to the value.
622 if (lhs_type->points_to() == NULL)
624 val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val);
625 val = build_fold_indirect_ref_loc(location, val);
628 return build2(COMPOUND_EXPR, lhs_type_tree, call,
629 fold_convert_loc(location, lhs_type_tree, val));
632 // Convert an expression to a tree. This is implemented by the child
633 // class. Not that it is not in general safe to call this multiple
634 // times for a single expression, but that we don't catch such errors.
637 Expression::get_tree(Translate_context* context)
639 // The child may have marked this expression as having an error.
640 if (this->classification_ == EXPRESSION_ERROR)
641 return error_mark_node;
643 return this->do_get_tree(context);
646 // Return a tree for VAL in TYPE.
649 Expression::integer_constant_tree(mpz_t val, tree type)
651 if (type == error_mark_node)
652 return error_mark_node;
653 else if (TREE_CODE(type) == INTEGER_TYPE)
654 return double_int_to_tree(type,
655 mpz_get_double_int(type, val, true));
656 else if (TREE_CODE(type) == REAL_TYPE)
659 mpfr_init_set_z(fval, val, GMP_RNDN);
660 tree ret = Expression::float_constant_tree(fval, type);
664 else if (TREE_CODE(type) == COMPLEX_TYPE)
667 mpfr_init_set_z(fval, val, GMP_RNDN);
668 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
670 tree imag = build_real_from_int_cst(TREE_TYPE(type),
672 return build_complex(type, real, imag);
678 // Return a tree for VAL in TYPE.
681 Expression::float_constant_tree(mpfr_t val, tree type)
683 if (type == error_mark_node)
684 return error_mark_node;
685 else if (TREE_CODE(type) == INTEGER_TYPE)
689 mpfr_get_z(ival, val, GMP_RNDN);
690 tree ret = Expression::integer_constant_tree(ival, type);
694 else if (TREE_CODE(type) == REAL_TYPE)
697 real_from_mpfr(&r1, val, type, GMP_RNDN);
699 real_convert(&r2, TYPE_MODE(type), &r1);
700 return build_real(type, r2);
702 else if (TREE_CODE(type) == COMPLEX_TYPE)
705 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
707 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
708 tree imag = build_real_from_int_cst(TREE_TYPE(type),
710 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
716 // Return a tree for REAL/IMAG in TYPE.
719 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
721 if (type == error_mark_node)
722 return error_mark_node;
723 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
724 return Expression::float_constant_tree(real, type);
725 else if (TREE_CODE(type) == COMPLEX_TYPE)
728 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
730 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
733 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
735 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
737 return build_complex(type, build_real(TREE_TYPE(type), r2),
738 build_real(TREE_TYPE(type), r4));
744 // Return a tree which evaluates to true if VAL, of arbitrary integer
745 // type, is negative or is more than the maximum value of BOUND_TYPE.
746 // If SOFAR is not NULL, it is or'red into the result. The return
747 // value may be NULL if SOFAR is NULL.
750 Expression::check_bounds(tree val, tree bound_type, tree sofar,
753 tree val_type = TREE_TYPE(val);
754 tree ret = NULL_TREE;
756 if (!TYPE_UNSIGNED(val_type))
758 ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val,
759 build_int_cst(val_type, 0));
760 if (ret == boolean_false_node)
764 HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
765 HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
766 go_assert(val_type_size != -1 && bound_type_size != -1);
767 if (val_type_size > bound_type_size
768 || (val_type_size == bound_type_size
769 && TYPE_UNSIGNED(val_type)
770 && !TYPE_UNSIGNED(bound_type)))
772 tree max = TYPE_MAX_VALUE(bound_type);
773 tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val,
774 fold_convert_loc(loc, val_type, max));
775 if (big == boolean_false_node)
777 else if (ret == NULL_TREE)
780 ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
784 if (ret == NULL_TREE)
786 else if (sofar == NULL_TREE)
789 return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
794 Expression::dump_expression(Ast_dump_context* ast_dump_context) const
796 this->do_dump_expression(ast_dump_context);
799 // Error expressions. This are used to avoid cascading errors.
801 class Error_expression : public Expression
804 Error_expression(source_location location)
805 : Expression(EXPRESSION_ERROR, location)
810 do_is_constant() const
814 do_integer_constant_value(bool, mpz_t val, Type**) const
821 do_float_constant_value(mpfr_t val, Type**) const
823 mpfr_set_ui(val, 0, GMP_RNDN);
828 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
830 mpfr_set_ui(real, 0, GMP_RNDN);
831 mpfr_set_ui(imag, 0, GMP_RNDN);
836 do_discarding_value()
841 { return Type::make_error_type(); }
844 do_determine_type(const Type_context*)
852 do_is_addressable() const
856 do_get_tree(Translate_context*)
857 { return error_mark_node; }
860 do_dump_expression(Ast_dump_context*) const;
863 // Dump the ast representation for an error expression to a dump context.
866 Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
868 ast_dump_context->ostream() << "_Error_" ;
872 Expression::make_error(source_location location)
874 return new Error_expression(location);
877 // An expression which is really a type. This is used during parsing.
878 // It is an error if these survive after lowering.
881 Type_expression : public Expression
884 Type_expression(Type* type, source_location location)
885 : Expression(EXPRESSION_TYPE, location),
891 do_traverse(Traverse* traverse)
892 { return Type::traverse(this->type_, traverse); }
896 { return this->type_; }
899 do_determine_type(const Type_context*)
903 do_check_types(Gogo*)
904 { this->report_error(_("invalid use of type")); }
911 do_get_tree(Translate_context*)
912 { go_unreachable(); }
914 void do_dump_expression(Ast_dump_context*) const;
917 // The type which we are representing as an expression.
922 Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
924 ast_dump_context->dump_type(this->type_);
928 Expression::make_type(Type* type, source_location location)
930 return new Type_expression(type, location);
933 // Class Parser_expression.
936 Parser_expression::do_type()
938 // We should never really ask for the type of a Parser_expression.
939 // However, it can happen, at least when we have an invalid const
940 // whose initializer refers to the const itself. In that case we
941 // may ask for the type when lowering the const itself.
942 go_assert(saw_errors());
943 return Type::make_error_type();
946 // Class Var_expression.
948 // Lower a variable expression. Here we just make sure that the
949 // initialization expression of the variable has been lowered. This
950 // ensures that we will be able to determine the type of the variable
954 Var_expression::do_lower(Gogo* gogo, Named_object* function,
955 Statement_inserter* inserter, int)
957 if (this->variable_->is_variable())
959 Variable* var = this->variable_->var_value();
960 // This is either a local variable or a global variable. A
961 // reference to a variable which is local to an enclosing
962 // function will be a reference to a field in a closure.
963 if (var->is_global())
968 var->lower_init_expression(gogo, function, inserter);
973 // Return the type of a reference to a variable.
976 Var_expression::do_type()
978 if (this->variable_->is_variable())
979 return this->variable_->var_value()->type();
980 else if (this->variable_->is_result_variable())
981 return this->variable_->result_var_value()->type();
986 // Determine the type of a reference to a variable.
989 Var_expression::do_determine_type(const Type_context*)
991 if (this->variable_->is_variable())
992 this->variable_->var_value()->determine_type();
995 // Something takes the address of this variable. This means that we
996 // may want to move the variable onto the heap.
999 Var_expression::do_address_taken(bool escapes)
1003 if (this->variable_->is_variable())
1004 this->variable_->var_value()->set_non_escaping_address_taken();
1005 else if (this->variable_->is_result_variable())
1006 this->variable_->result_var_value()->set_non_escaping_address_taken();
1012 if (this->variable_->is_variable())
1013 this->variable_->var_value()->set_address_taken();
1014 else if (this->variable_->is_result_variable())
1015 this->variable_->result_var_value()->set_address_taken();
1021 // Get the tree for a reference to a variable.
1024 Var_expression::do_get_tree(Translate_context* context)
1026 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
1027 context->function());
1028 tree ret = var_to_tree(bvar);
1029 if (ret == error_mark_node)
1030 return error_mark_node;
1032 if (this->variable_->is_variable())
1033 is_in_heap = this->variable_->var_value()->is_in_heap();
1034 else if (this->variable_->is_result_variable())
1035 is_in_heap = this->variable_->result_var_value()->is_in_heap();
1040 ret = build_fold_indirect_ref_loc(this->location(), ret);
1041 TREE_THIS_NOTRAP(ret) = 1;
1046 // Ast dump for variable expression.
1049 Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1051 ast_dump_context->ostream() << this->variable_->name() ;
1054 // Make a reference to a variable in an expression.
1057 Expression::make_var_reference(Named_object* var, source_location location)
1060 return Expression::make_sink(location);
1062 // FIXME: Creating a new object for each reference to a variable is
1064 return new Var_expression(var, location);
1067 // Class Temporary_reference_expression.
1072 Temporary_reference_expression::do_type()
1074 return this->statement_->type();
1077 // Called if something takes the address of this temporary variable.
1078 // We never have to move temporary variables to the heap, but we do
1079 // need to know that they must live in the stack rather than in a
1083 Temporary_reference_expression::do_address_taken(bool)
1085 this->statement_->set_is_address_taken();
1088 // Get a tree referring to the variable.
1091 Temporary_reference_expression::do_get_tree(Translate_context* context)
1093 Bvariable* bvar = this->statement_->get_backend_variable(context);
1095 // The gcc backend can't represent the same set of recursive types
1096 // that the Go frontend can. In some cases this means that a
1097 // temporary variable won't have the right backend type. Correct
1098 // that here by adding a type cast. We need to use base() to push
1099 // the circularity down one level.
1100 tree ret = var_to_tree(bvar);
1101 if (!this->is_lvalue_
1102 && POINTER_TYPE_P(TREE_TYPE(ret))
1103 && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
1105 Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1106 tree type_tree = type_to_tree(type_btype);
1107 ret = fold_convert_loc(this->location(), type_tree, ret);
1112 // Ast dump for temporary reference.
1115 Temporary_reference_expression::do_dump_expression(
1116 Ast_dump_context* ast_dump_context) const
1118 ast_dump_context->dump_temp_variable_name(this->statement_);
1121 // Make a reference to a temporary variable.
1123 Temporary_reference_expression*
1124 Expression::make_temporary_reference(Temporary_statement* statement,
1125 source_location location)
1127 return new Temporary_reference_expression(statement, location);
1130 // A sink expression--a use of the blank identifier _.
1132 class Sink_expression : public Expression
1135 Sink_expression(source_location location)
1136 : Expression(EXPRESSION_SINK, location),
1137 type_(NULL), var_(NULL_TREE)
1142 do_discarding_value()
1149 do_determine_type(const Type_context*);
1153 { return new Sink_expression(this->location()); }
1156 do_get_tree(Translate_context*);
1159 do_dump_expression(Ast_dump_context*) const;
1162 // The type of this sink variable.
1164 // The temporary variable we generate.
1168 // Return the type of a sink expression.
1171 Sink_expression::do_type()
1173 if (this->type_ == NULL)
1174 return Type::make_sink_type();
1178 // Determine the type of a sink expression.
1181 Sink_expression::do_determine_type(const Type_context* context)
1183 if (context->type != NULL)
1184 this->type_ = context->type;
1187 // Return a temporary variable for a sink expression. This will
1188 // presumably be a write-only variable which the middle-end will drop.
1191 Sink_expression::do_get_tree(Translate_context* context)
1193 if (this->var_ == NULL_TREE)
1195 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
1196 Btype* bt = this->type_->get_backend(context->gogo());
1197 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
1202 // Ast dump for sink expression.
1205 Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1207 ast_dump_context->ostream() << "_" ;
1210 // Make a sink expression.
1213 Expression::make_sink(source_location location)
1215 return new Sink_expression(location);
1218 // Class Func_expression.
1220 // FIXME: Can a function expression appear in a constant expression?
1221 // The value is unchanging. Initializing a constant to the address of
1222 // a function seems like it could work, though there might be little
1228 Func_expression::do_traverse(Traverse* traverse)
1230 return (this->closure_ == NULL
1232 : Expression::traverse(&this->closure_, traverse));
1235 // Return the type of a function expression.
1238 Func_expression::do_type()
1240 if (this->function_->is_function())
1241 return this->function_->func_value()->type();
1242 else if (this->function_->is_function_declaration())
1243 return this->function_->func_declaration_value()->type();
1248 // Get the tree for a function expression without evaluating the
1252 Func_expression::get_tree_without_closure(Gogo* gogo)
1254 Function_type* fntype;
1255 if (this->function_->is_function())
1256 fntype = this->function_->func_value()->type();
1257 else if (this->function_->is_function_declaration())
1258 fntype = this->function_->func_declaration_value()->type();
1262 // Builtin functions are handled specially by Call_expression. We
1263 // can't take their address.
1264 if (fntype->is_builtin())
1266 error_at(this->location(), "invalid use of special builtin function %qs",
1267 this->function_->name().c_str());
1268 return error_mark_node;
1271 Named_object* no = this->function_;
1273 tree id = no->get_id(gogo);
1274 if (id == error_mark_node)
1275 return error_mark_node;
1278 if (no->is_function())
1279 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1280 else if (no->is_function_declaration())
1281 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1285 if (fndecl == error_mark_node)
1286 return error_mark_node;
1288 return build_fold_addr_expr_loc(this->location(), fndecl);
1291 // Get the tree for a function expression. This is used when we take
1292 // the address of a function rather than simply calling it. If the
1293 // function has a closure, we must use a trampoline.
1296 Func_expression::do_get_tree(Translate_context* context)
1298 Gogo* gogo = context->gogo();
1300 tree fnaddr = this->get_tree_without_closure(gogo);
1301 if (fnaddr == error_mark_node)
1302 return error_mark_node;
1304 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1305 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1306 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1308 // For a normal non-nested function call, that is all we have to do.
1309 if (!this->function_->is_function()
1310 || this->function_->func_value()->enclosing() == NULL)
1312 go_assert(this->closure_ == NULL);
1316 // For a nested function call, we have to always allocate a
1317 // trampoline. If we don't always allocate, then closures will not
1318 // be reliably distinct.
1319 Expression* closure = this->closure_;
1321 if (closure == NULL)
1322 closure_tree = null_pointer_node;
1325 // Get the value of the closure. This will be a pointer to
1326 // space allocated on the heap.
1327 closure_tree = closure->get_tree(context);
1328 if (closure_tree == error_mark_node)
1329 return error_mark_node;
1330 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1333 // Now we need to build some code on the heap. This code will load
1334 // the static chain pointer with the closure and then jump to the
1335 // body of the function. The normal gcc approach is to build the
1336 // code on the stack. Unfortunately we can not do that, as Go
1337 // permits us to return the function pointer.
1339 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1342 // Ast dump for function.
1345 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1347 ast_dump_context->ostream() << this->function_->name();
1348 if (this->closure_ != NULL)
1350 ast_dump_context->ostream() << " {closure = ";
1351 this->closure_->dump_expression(ast_dump_context);
1352 ast_dump_context->ostream() << "}";
1356 // Make a reference to a function in an expression.
1359 Expression::make_func_reference(Named_object* function, Expression* closure,
1360 source_location location)
1362 return new Func_expression(function, closure, location);
1365 // Class Unknown_expression.
1367 // Return the name of an unknown expression.
1370 Unknown_expression::name() const
1372 return this->named_object_->name();
1375 // Lower a reference to an unknown name.
1378 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1380 source_location location = this->location();
1381 Named_object* no = this->named_object_;
1383 if (!no->is_unknown())
1387 real = no->unknown_value()->real_named_object();
1390 if (this->is_composite_literal_key_)
1392 error_at(location, "reference to undefined name %qs",
1393 this->named_object_->message_name().c_str());
1394 return Expression::make_error(location);
1397 switch (real->classification())
1399 case Named_object::NAMED_OBJECT_CONST:
1400 return Expression::make_const_reference(real, location);
1401 case Named_object::NAMED_OBJECT_TYPE:
1402 return Expression::make_type(real->type_value(), location);
1403 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1404 if (this->is_composite_literal_key_)
1406 error_at(location, "reference to undefined type %qs",
1407 real->message_name().c_str());
1408 return Expression::make_error(location);
1409 case Named_object::NAMED_OBJECT_VAR:
1410 return Expression::make_var_reference(real, location);
1411 case Named_object::NAMED_OBJECT_FUNC:
1412 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1413 return Expression::make_func_reference(real, NULL, location);
1414 case Named_object::NAMED_OBJECT_PACKAGE:
1415 if (this->is_composite_literal_key_)
1417 error_at(location, "unexpected reference to package");
1418 return Expression::make_error(location);
1424 // Dump the ast representation for an unknown expression to a dump context.
1427 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1429 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1433 // Make a reference to an unknown name.
1436 Expression::make_unknown_reference(Named_object* no, source_location location)
1438 return new Unknown_expression(no, location);
1441 // A boolean expression.
1443 class Boolean_expression : public Expression
1446 Boolean_expression(bool val, source_location location)
1447 : Expression(EXPRESSION_BOOLEAN, location),
1448 val_(val), type_(NULL)
1456 do_is_constant() const
1463 do_determine_type(const Type_context*);
1470 do_get_tree(Translate_context*)
1471 { return this->val_ ? boolean_true_node : boolean_false_node; }
1474 do_export(Export* exp) const
1475 { exp->write_c_string(this->val_ ? "true" : "false"); }
1478 do_dump_expression(Ast_dump_context* ast_dump_context) const
1479 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1484 // The type as determined by context.
1491 Boolean_expression::do_type()
1493 if (this->type_ == NULL)
1494 this->type_ = Type::make_boolean_type();
1498 // Set the type from the context.
1501 Boolean_expression::do_determine_type(const Type_context* context)
1503 if (this->type_ != NULL && !this->type_->is_abstract())
1505 else if (context->type != NULL && context->type->is_boolean_type())
1506 this->type_ = context->type;
1507 else if (!context->may_be_abstract)
1508 this->type_ = Type::lookup_bool_type();
1511 // Import a boolean constant.
1514 Boolean_expression::do_import(Import* imp)
1516 if (imp->peek_char() == 't')
1518 imp->require_c_string("true");
1519 return Expression::make_boolean(true, imp->location());
1523 imp->require_c_string("false");
1524 return Expression::make_boolean(false, imp->location());
1528 // Make a boolean expression.
1531 Expression::make_boolean(bool val, source_location location)
1533 return new Boolean_expression(val, location);
1536 // Class String_expression.
1541 String_expression::do_type()
1543 if (this->type_ == NULL)
1544 this->type_ = Type::make_string_type();
1548 // Set the type from the context.
1551 String_expression::do_determine_type(const Type_context* context)
1553 if (this->type_ != NULL && !this->type_->is_abstract())
1555 else if (context->type != NULL && context->type->is_string_type())
1556 this->type_ = context->type;
1557 else if (!context->may_be_abstract)
1558 this->type_ = Type::lookup_string_type();
1561 // Build a string constant.
1564 String_expression::do_get_tree(Translate_context* context)
1566 return context->gogo()->go_string_constant_tree(this->val_);
1569 // Write string literal to string dump.
1572 String_expression::export_string(String_dump* exp,
1573 const String_expression* str)
1576 s.reserve(str->val_.length() * 4 + 2);
1578 for (std::string::const_iterator p = str->val_.begin();
1579 p != str->val_.end();
1582 if (*p == '\\' || *p == '"')
1587 else if (*p >= 0x20 && *p < 0x7f)
1589 else if (*p == '\n')
1591 else if (*p == '\t')
1596 unsigned char c = *p;
1597 unsigned int dig = c >> 4;
1598 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1600 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1604 exp->write_string(s);
1607 // Export a string expression.
1610 String_expression::do_export(Export* exp) const
1612 String_expression::export_string(exp, this);
1615 // Import a string expression.
1618 String_expression::do_import(Import* imp)
1620 imp->require_c_string("\"");
1624 int c = imp->get_char();
1625 if (c == '"' || c == -1)
1628 val += static_cast<char>(c);
1631 c = imp->get_char();
1632 if (c == '\\' || c == '"')
1633 val += static_cast<char>(c);
1640 c = imp->get_char();
1641 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1642 c = imp->get_char();
1643 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1644 char v = (vh << 4) | vl;
1649 error_at(imp->location(), "bad string constant");
1650 return Expression::make_error(imp->location());
1654 return Expression::make_string(val, imp->location());
1657 // Ast dump for string expression.
1660 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1662 String_expression::export_string(ast_dump_context, this);
1665 // Make a string expression.
1668 Expression::make_string(const std::string& val, source_location location)
1670 return new String_expression(val, location);
1673 // Make an integer expression.
1675 class Integer_expression : public Expression
1678 Integer_expression(const mpz_t* val, Type* type, source_location location)
1679 : Expression(EXPRESSION_INTEGER, location),
1681 { mpz_init_set(this->val_, *val); }
1686 // Return whether VAL fits in the type.
1688 check_constant(mpz_t val, Type*, source_location);
1690 // Write VAL to string dump.
1692 export_integer(String_dump* exp, const mpz_t val);
1694 // Write VAL to dump context.
1696 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1700 do_is_constant() const
1704 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1710 do_determine_type(const Type_context* context);
1713 do_check_types(Gogo*);
1716 do_get_tree(Translate_context*);
1720 { return Expression::make_integer(&this->val_, this->type_,
1721 this->location()); }
1724 do_export(Export*) const;
1727 do_dump_expression(Ast_dump_context*) const;
1730 // The integer value.
1736 // Return an integer constant value.
1739 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1742 if (this->type_ != NULL)
1743 *ptype = this->type_;
1744 mpz_set(val, this->val_);
1748 // Return the current type. If we haven't set the type yet, we return
1749 // an abstract integer type.
1752 Integer_expression::do_type()
1754 if (this->type_ == NULL)
1755 this->type_ = Type::make_abstract_integer_type();
1759 // Set the type of the integer value. Here we may switch from an
1760 // abstract type to a real type.
1763 Integer_expression::do_determine_type(const Type_context* context)
1765 if (this->type_ != NULL && !this->type_->is_abstract())
1767 else if (context->type != NULL
1768 && (context->type->integer_type() != NULL
1769 || context->type->float_type() != NULL
1770 || context->type->complex_type() != NULL))
1771 this->type_ = context->type;
1772 else if (!context->may_be_abstract)
1773 this->type_ = Type::lookup_integer_type("int");
1776 // Return true if the integer VAL fits in the range of the type TYPE.
1777 // Otherwise give an error and return false. TYPE may be NULL.
1780 Integer_expression::check_constant(mpz_t val, Type* type,
1781 source_location location)
1785 Integer_type* itype = type->integer_type();
1786 if (itype == NULL || itype->is_abstract())
1789 int bits = mpz_sizeinbase(val, 2);
1791 if (itype->is_unsigned())
1793 // For an unsigned type we can only accept a nonnegative number,
1794 // and we must be able to represent at least BITS.
1795 if (mpz_sgn(val) >= 0
1796 && bits <= itype->bits())
1801 // For a signed type we need an extra bit to indicate the sign.
1802 // We have to handle the most negative integer specially.
1803 if (bits + 1 <= itype->bits()
1804 || (bits <= itype->bits()
1806 && (mpz_scan1(val, 0)
1807 == static_cast<unsigned long>(itype->bits() - 1))
1808 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1812 error_at(location, "integer constant overflow");
1816 // Check the type of an integer constant.
1819 Integer_expression::do_check_types(Gogo*)
1821 if (this->type_ == NULL)
1823 if (!Integer_expression::check_constant(this->val_, this->type_,
1825 this->set_is_error();
1828 // Get a tree for an integer constant.
1831 Integer_expression::do_get_tree(Translate_context* context)
1833 Gogo* gogo = context->gogo();
1835 if (this->type_ != NULL && !this->type_->is_abstract())
1836 type = type_to_tree(this->type_->get_backend(gogo));
1837 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1839 // We are converting to an abstract floating point type.
1840 Type* ftype = Type::lookup_float_type("float64");
1841 type = type_to_tree(ftype->get_backend(gogo));
1843 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1845 // We are converting to an abstract complex type.
1846 Type* ctype = Type::lookup_complex_type("complex128");
1847 type = type_to_tree(ctype->get_backend(gogo));
1851 // If we still have an abstract type here, then this is being
1852 // used in a constant expression which didn't get reduced for
1853 // some reason. Use a type which will fit the value. We use <,
1854 // not <=, because we need an extra bit for the sign bit.
1855 int bits = mpz_sizeinbase(this->val_, 2);
1856 if (bits < INT_TYPE_SIZE)
1858 Type* t = Type::lookup_integer_type("int");
1859 type = type_to_tree(t->get_backend(gogo));
1863 Type* t = Type::lookup_integer_type("int64");
1864 type = type_to_tree(t->get_backend(gogo));
1867 type = long_long_integer_type_node;
1869 return Expression::integer_constant_tree(this->val_, type);
1872 // Write VAL to export data.
1875 Integer_expression::export_integer(String_dump* exp, const mpz_t val)
1877 char* s = mpz_get_str(NULL, 10, val);
1878 exp->write_c_string(s);
1882 // Export an integer in a constant expression.
1885 Integer_expression::do_export(Export* exp) const
1887 Integer_expression::export_integer(exp, this->val_);
1888 // A trailing space lets us reliably identify the end of the number.
1889 exp->write_c_string(" ");
1892 // Import an integer, floating point, or complex value. This handles
1893 // all these types because they all start with digits.
1896 Integer_expression::do_import(Import* imp)
1898 std::string num = imp->read_identifier();
1899 imp->require_c_string(" ");
1900 if (!num.empty() && num[num.length() - 1] == 'i')
1903 size_t plus_pos = num.find('+', 1);
1904 size_t minus_pos = num.find('-', 1);
1906 if (plus_pos == std::string::npos)
1908 else if (minus_pos == std::string::npos)
1912 error_at(imp->location(), "bad number in import data: %qs",
1914 return Expression::make_error(imp->location());
1916 if (pos == std::string::npos)
1917 mpfr_set_ui(real, 0, GMP_RNDN);
1920 std::string real_str = num.substr(0, pos);
1921 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1923 error_at(imp->location(), "bad number in import data: %qs",
1925 return Expression::make_error(imp->location());
1929 std::string imag_str;
1930 if (pos == std::string::npos)
1933 imag_str = num.substr(pos);
1934 imag_str = imag_str.substr(0, imag_str.size() - 1);
1936 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1938 error_at(imp->location(), "bad number in import data: %qs",
1940 return Expression::make_error(imp->location());
1942 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1948 else if (num.find('.') == std::string::npos
1949 && num.find('E') == std::string::npos)
1952 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1954 error_at(imp->location(), "bad number in import data: %qs",
1956 return Expression::make_error(imp->location());
1958 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1965 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1967 error_at(imp->location(), "bad number in import data: %qs",
1969 return Expression::make_error(imp->location());
1971 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1976 // Ast dump for integer expression.
1979 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1981 Integer_expression::export_integer(ast_dump_context, this->val_);
1984 // Build a new integer value.
1987 Expression::make_integer(const mpz_t* val, Type* type,
1988 source_location location)
1990 return new Integer_expression(val, type, location);
1995 class Float_expression : public Expression
1998 Float_expression(const mpfr_t* val, Type* type, source_location location)
1999 : Expression(EXPRESSION_FLOAT, location),
2002 mpfr_init_set(this->val_, *val, GMP_RNDN);
2005 // Constrain VAL to fit into TYPE.
2007 constrain_float(mpfr_t val, Type* type);
2009 // Return whether VAL fits in the type.
2011 check_constant(mpfr_t val, Type*, source_location);
2013 // Write VAL to export data.
2015 export_float(String_dump* exp, const mpfr_t val);
2017 // Write VAL to dump file.
2019 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
2023 do_is_constant() const
2027 do_float_constant_value(mpfr_t val, Type**) const;
2033 do_determine_type(const Type_context*);
2036 do_check_types(Gogo*);
2040 { return Expression::make_float(&this->val_, this->type_,
2041 this->location()); }
2044 do_get_tree(Translate_context*);
2047 do_export(Export*) const;
2050 do_dump_expression(Ast_dump_context*) const;
2053 // The floating point value.
2059 // Constrain VAL to fit into TYPE.
2062 Float_expression::constrain_float(mpfr_t val, Type* type)
2064 Float_type* ftype = type->float_type();
2065 if (ftype != NULL && !ftype->is_abstract())
2066 mpfr_prec_round(val, ftype->bits(), GMP_RNDN);
2069 // Return a floating point constant value.
2072 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2074 if (this->type_ != NULL)
2075 *ptype = this->type_;
2076 mpfr_set(val, this->val_, GMP_RNDN);
2080 // Return the current type. If we haven't set the type yet, we return
2081 // an abstract float type.
2084 Float_expression::do_type()
2086 if (this->type_ == NULL)
2087 this->type_ = Type::make_abstract_float_type();
2091 // Set the type of the float value. Here we may switch from an
2092 // abstract type to a real type.
2095 Float_expression::do_determine_type(const Type_context* context)
2097 if (this->type_ != NULL && !this->type_->is_abstract())
2099 else if (context->type != NULL
2100 && (context->type->integer_type() != NULL
2101 || context->type->float_type() != NULL
2102 || context->type->complex_type() != NULL))
2103 this->type_ = context->type;
2104 else if (!context->may_be_abstract)
2105 this->type_ = Type::lookup_float_type("float64");
2108 // Return true if the floating point value VAL fits in the range of
2109 // the type TYPE. Otherwise give an error and return false. TYPE may
2113 Float_expression::check_constant(mpfr_t val, Type* type,
2114 source_location location)
2118 Float_type* ftype = type->float_type();
2119 if (ftype == NULL || ftype->is_abstract())
2122 // A NaN or Infinity always fits in the range of the type.
2123 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
2126 mp_exp_t exp = mpfr_get_exp(val);
2128 switch (ftype->bits())
2141 error_at(location, "floating point constant overflow");
2147 // Check the type of a float value.
2150 Float_expression::do_check_types(Gogo*)
2152 if (this->type_ == NULL)
2155 if (!Float_expression::check_constant(this->val_, this->type_,
2157 this->set_is_error();
2159 Integer_type* integer_type = this->type_->integer_type();
2160 if (integer_type != NULL)
2162 if (!mpfr_integer_p(this->val_))
2163 this->report_error(_("floating point constant truncated to integer"));
2166 go_assert(!integer_type->is_abstract());
2169 mpfr_get_z(ival, this->val_, GMP_RNDN);
2170 Integer_expression::check_constant(ival, integer_type,
2177 // Get a tree for a float constant.
2180 Float_expression::do_get_tree(Translate_context* context)
2182 Gogo* gogo = context->gogo();
2184 if (this->type_ != NULL && !this->type_->is_abstract())
2185 type = type_to_tree(this->type_->get_backend(gogo));
2186 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2188 // We have an abstract integer type. We just hope for the best.
2189 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2193 // If we still have an abstract type here, then this is being
2194 // used in a constant expression which didn't get reduced. We
2195 // just use float64 and hope for the best.
2196 Type* ft = Type::lookup_float_type("float64");
2197 type = type_to_tree(ft->get_backend(gogo));
2199 return Expression::float_constant_tree(this->val_, type);
2202 // Write a floating point number to a string dump.
2205 Float_expression::export_float(String_dump *exp, const mpfr_t val)
2208 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2210 exp->write_c_string("-");
2211 exp->write_c_string("0.");
2212 exp->write_c_string(*s == '-' ? s + 1 : s);
2215 snprintf(buf, sizeof buf, "E%ld", exponent);
2216 exp->write_c_string(buf);
2219 // Export a floating point number in a constant expression.
2222 Float_expression::do_export(Export* exp) const
2224 Float_expression::export_float(exp, this->val_);
2225 // A trailing space lets us reliably identify the end of the number.
2226 exp->write_c_string(" ");
2229 // Dump a floating point number to the dump file.
2232 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2234 Float_expression::export_float(ast_dump_context, this->val_);
2237 // Make a float expression.
2240 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2242 return new Float_expression(val, type, location);
2247 class Complex_expression : public Expression
2250 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2251 source_location location)
2252 : Expression(EXPRESSION_COMPLEX, location),
2255 mpfr_init_set(this->real_, *real, GMP_RNDN);
2256 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2259 // Constrain REAL/IMAG to fit into TYPE.
2261 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2263 // Return whether REAL/IMAG fits in the type.
2265 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2267 // Write REAL/IMAG to string dump.
2269 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
2271 // Write REAL/IMAG to dump context.
2273 dump_complex(Ast_dump_context* ast_dump_context,
2274 const mpfr_t real, const mpfr_t val);
2278 do_is_constant() const
2282 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2288 do_determine_type(const Type_context*);
2291 do_check_types(Gogo*);
2296 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2301 do_get_tree(Translate_context*);
2304 do_export(Export*) const;
2307 do_dump_expression(Ast_dump_context*) const;
2312 // The imaginary part;
2314 // The type if known.
2318 // Constrain REAL/IMAG to fit into TYPE.
2321 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2323 Complex_type* ctype = type->complex_type();
2324 if (ctype != NULL && !ctype->is_abstract())
2326 mpfr_prec_round(real, ctype->bits() / 2, GMP_RNDN);
2327 mpfr_prec_round(imag, ctype->bits() / 2, GMP_RNDN);
2331 // Return a complex constant value.
2334 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2337 if (this->type_ != NULL)
2338 *ptype = this->type_;
2339 mpfr_set(real, this->real_, GMP_RNDN);
2340 mpfr_set(imag, this->imag_, GMP_RNDN);
2344 // Return the current type. If we haven't set the type yet, we return
2345 // an abstract complex type.
2348 Complex_expression::do_type()
2350 if (this->type_ == NULL)
2351 this->type_ = Type::make_abstract_complex_type();
2355 // Set the type of the complex value. Here we may switch from an
2356 // abstract type to a real type.
2359 Complex_expression::do_determine_type(const Type_context* context)
2361 if (this->type_ != NULL && !this->type_->is_abstract())
2363 else if (context->type != NULL
2364 && context->type->complex_type() != NULL)
2365 this->type_ = context->type;
2366 else if (!context->may_be_abstract)
2367 this->type_ = Type::lookup_complex_type("complex128");
2370 // Return true if the complex value REAL/IMAG fits in the range of the
2371 // type TYPE. Otherwise give an error and return false. TYPE may be
2375 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2376 source_location location)
2380 Complex_type* ctype = type->complex_type();
2381 if (ctype == NULL || ctype->is_abstract())
2385 switch (ctype->bits())
2397 // A NaN or Infinity always fits in the range of the type.
2398 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2400 if (mpfr_get_exp(real) > max_exp)
2402 error_at(location, "complex real part constant overflow");
2407 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2409 if (mpfr_get_exp(imag) > max_exp)
2411 error_at(location, "complex imaginary part constant overflow");
2419 // Check the type of a complex value.
2422 Complex_expression::do_check_types(Gogo*)
2424 if (this->type_ == NULL)
2427 if (!Complex_expression::check_constant(this->real_, this->imag_,
2428 this->type_, this->location()))
2429 this->set_is_error();
2432 // Get a tree for a complex constant.
2435 Complex_expression::do_get_tree(Translate_context* context)
2437 Gogo* gogo = context->gogo();
2439 if (this->type_ != NULL && !this->type_->is_abstract())
2440 type = type_to_tree(this->type_->get_backend(gogo));
2443 // If we still have an abstract type here, this this is being
2444 // used in a constant expression which didn't get reduced. We
2445 // just use complex128 and hope for the best.
2446 Type* ct = Type::lookup_complex_type("complex128");
2447 type = type_to_tree(ct->get_backend(gogo));
2449 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2452 // Write REAL/IMAG to export data.
2455 Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
2458 if (!mpfr_zero_p(real))
2460 Float_expression::export_float(exp, real);
2461 if (mpfr_sgn(imag) > 0)
2462 exp->write_c_string("+");
2464 Float_expression::export_float(exp, imag);
2465 exp->write_c_string("i");
2468 // Export a complex number in a constant expression.
2471 Complex_expression::do_export(Export* exp) const
2473 Complex_expression::export_complex(exp, this->real_, this->imag_);
2474 // A trailing space lets us reliably identify the end of the number.
2475 exp->write_c_string(" ");
2478 // Dump a complex expression to the dump file.
2481 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2483 Complex_expression::export_complex(ast_dump_context,
2488 // Make a complex expression.
2491 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2492 source_location location)
2494 return new Complex_expression(real, imag, type, location);
2497 // Find a named object in an expression.
2499 class Find_named_object : public Traverse
2502 Find_named_object(Named_object* no)
2503 : Traverse(traverse_expressions),
2504 no_(no), found_(false)
2507 // Whether we found the object.
2510 { return this->found_; }
2514 expression(Expression**);
2517 // The object we are looking for.
2519 // Whether we found it.
2523 // A reference to a const in an expression.
2525 class Const_expression : public Expression
2528 Const_expression(Named_object* constant, source_location location)
2529 : Expression(EXPRESSION_CONST_REFERENCE, location),
2530 constant_(constant), type_(NULL), seen_(false)
2535 { return this->constant_; }
2537 // Check that the initializer does not refer to the constant itself.
2539 check_for_init_loop();
2543 do_traverse(Traverse*);
2546 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2549 do_is_constant() const
2553 do_integer_constant_value(bool, mpz_t val, Type**) const;
2556 do_float_constant_value(mpfr_t val, Type**) const;
2559 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2562 do_string_constant_value(std::string* val) const
2563 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2568 // The type of a const is set by the declaration, not the use.
2570 do_determine_type(const Type_context*);
2573 do_check_types(Gogo*);
2580 do_get_tree(Translate_context* context);
2582 // When exporting a reference to a const as part of a const
2583 // expression, we export the value. We ignore the fact that it has
2586 do_export(Export* exp) const
2587 { this->constant_->const_value()->expr()->export_expression(exp); }
2590 do_dump_expression(Ast_dump_context*) const;
2594 Named_object* constant_;
2595 // The type of this reference. This is used if the constant has an
2598 // Used to prevent infinite recursion when a constant incorrectly
2599 // refers to itself.
2606 Const_expression::do_traverse(Traverse* traverse)
2608 if (this->type_ != NULL)
2609 return Type::traverse(this->type_, traverse);
2610 return TRAVERSE_CONTINUE;
2613 // Lower a constant expression. This is where we convert the
2614 // predeclared constant iota into an integer value.
2617 Const_expression::do_lower(Gogo* gogo, Named_object*,
2618 Statement_inserter*, int iota_value)
2620 if (this->constant_->const_value()->expr()->classification()
2623 if (iota_value == -1)
2625 error_at(this->location(),
2626 "iota is only defined in const declarations");
2630 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2631 Expression* ret = Expression::make_integer(&val, NULL,
2637 // Make sure that the constant itself has been lowered.
2638 gogo->lower_constant(this->constant_);
2643 // Return an integer constant value.
2646 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2653 if (this->type_ != NULL)
2654 ctype = this->type_;
2656 ctype = this->constant_->const_value()->type();
2657 if (ctype != NULL && ctype->integer_type() == NULL)
2660 Expression* e = this->constant_->const_value()->expr();
2665 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2667 this->seen_ = false;
2671 && !Integer_expression::check_constant(val, ctype, this->location()))
2674 *ptype = ctype != NULL ? ctype : t;
2678 // Return a floating point constant value.
2681 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2687 if (this->type_ != NULL)
2688 ctype = this->type_;
2690 ctype = this->constant_->const_value()->type();
2691 if (ctype != NULL && ctype->float_type() == NULL)
2697 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2700 this->seen_ = false;
2702 if (r && ctype != NULL)
2704 if (!Float_expression::check_constant(val, ctype, this->location()))
2706 Float_expression::constrain_float(val, ctype);
2708 *ptype = ctype != NULL ? ctype : t;
2712 // Return a complex constant value.
2715 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2722 if (this->type_ != NULL)
2723 ctype = this->type_;
2725 ctype = this->constant_->const_value()->type();
2726 if (ctype != NULL && ctype->complex_type() == NULL)
2732 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2736 this->seen_ = false;
2738 if (r && ctype != NULL)
2740 if (!Complex_expression::check_constant(real, imag, ctype,
2743 Complex_expression::constrain_complex(real, imag, ctype);
2745 *ptype = ctype != NULL ? ctype : t;
2749 // Return the type of the const reference.
2752 Const_expression::do_type()
2754 if (this->type_ != NULL)
2757 Named_constant* nc = this->constant_->const_value();
2759 if (this->seen_ || nc->lowering())
2761 this->report_error(_("constant refers to itself"));
2762 this->type_ = Type::make_error_type();
2768 Type* ret = nc->type();
2772 this->seen_ = false;
2776 // During parsing, a named constant may have a NULL type, but we
2777 // must not return a NULL type here.
2778 ret = nc->expr()->type();
2780 this->seen_ = false;
2785 // Set the type of the const reference.
2788 Const_expression::do_determine_type(const Type_context* context)
2790 Type* ctype = this->constant_->const_value()->type();
2791 Type* cetype = (ctype != NULL
2793 : this->constant_->const_value()->expr()->type());
2794 if (ctype != NULL && !ctype->is_abstract())
2796 else if (context->type != NULL
2797 && (context->type->integer_type() != NULL
2798 || context->type->float_type() != NULL
2799 || context->type->complex_type() != NULL)
2800 && (cetype->integer_type() != NULL
2801 || cetype->float_type() != NULL
2802 || cetype->complex_type() != NULL))
2803 this->type_ = context->type;
2804 else if (context->type != NULL
2805 && context->type->is_string_type()
2806 && cetype->is_string_type())
2807 this->type_ = context->type;
2808 else if (context->type != NULL
2809 && context->type->is_boolean_type()
2810 && cetype->is_boolean_type())
2811 this->type_ = context->type;
2812 else if (!context->may_be_abstract)
2814 if (cetype->is_abstract())
2815 cetype = cetype->make_non_abstract_type();
2816 this->type_ = cetype;
2820 // Check for a loop in which the initializer of a constant refers to
2821 // the constant itself.
2824 Const_expression::check_for_init_loop()
2826 if (this->type_ != NULL && this->type_->is_error())
2831 this->report_error(_("constant refers to itself"));
2832 this->type_ = Type::make_error_type();
2836 Expression* init = this->constant_->const_value()->expr();
2837 Find_named_object find_named_object(this->constant_);
2840 Expression::traverse(&init, &find_named_object);
2841 this->seen_ = false;
2843 if (find_named_object.found())
2845 if (this->type_ == NULL || !this->type_->is_error())
2847 this->report_error(_("constant refers to itself"));
2848 this->type_ = Type::make_error_type();
2854 // Check types of a const reference.
2857 Const_expression::do_check_types(Gogo*)
2859 if (this->type_ != NULL && this->type_->is_error())
2862 this->check_for_init_loop();
2864 if (this->type_ == NULL || this->type_->is_abstract())
2867 // Check for integer overflow.
2868 if (this->type_->integer_type() != NULL)
2873 if (!this->integer_constant_value(true, ival, &dummy))
2877 Expression* cexpr = this->constant_->const_value()->expr();
2878 if (cexpr->float_constant_value(fval, &dummy))
2880 if (!mpfr_integer_p(fval))
2881 this->report_error(_("floating point constant "
2882 "truncated to integer"));
2885 mpfr_get_z(ival, fval, GMP_RNDN);
2886 Integer_expression::check_constant(ival, this->type_,
2896 // Return a tree for the const reference.
2899 Const_expression::do_get_tree(Translate_context* context)
2901 Gogo* gogo = context->gogo();
2903 if (this->type_ == NULL)
2904 type_tree = NULL_TREE;
2907 type_tree = type_to_tree(this->type_->get_backend(gogo));
2908 if (type_tree == error_mark_node)
2909 return error_mark_node;
2912 // If the type has been set for this expression, but the underlying
2913 // object is an abstract int or float, we try to get the abstract
2914 // value. Otherwise we may lose something in the conversion.
2915 if (this->type_ != NULL
2916 && (this->constant_->const_value()->type() == NULL
2917 || this->constant_->const_value()->type()->is_abstract()))
2919 Expression* expr = this->constant_->const_value()->expr();
2923 if (expr->integer_constant_value(true, ival, &t))
2925 tree ret = Expression::integer_constant_tree(ival, type_tree);
2933 if (expr->float_constant_value(fval, &t))
2935 tree ret = Expression::float_constant_tree(fval, type_tree);
2942 if (expr->complex_constant_value(fval, imag, &t))
2944 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2953 tree const_tree = this->constant_->get_tree(gogo, context->function());
2954 if (this->type_ == NULL
2955 || const_tree == error_mark_node
2956 || TREE_TYPE(const_tree) == error_mark_node)
2960 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2961 ret = fold_convert(type_tree, const_tree);
2962 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2963 ret = fold(convert_to_integer(type_tree, const_tree));
2964 else if (TREE_CODE(type_tree) == REAL_TYPE)
2965 ret = fold(convert_to_real(type_tree, const_tree));
2966 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2967 ret = fold(convert_to_complex(type_tree, const_tree));
2973 // Dump ast representation for constant expression.
2976 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2978 ast_dump_context->ostream() << this->constant_->name();
2981 // Make a reference to a constant in an expression.
2984 Expression::make_const_reference(Named_object* constant,
2985 source_location location)
2987 return new Const_expression(constant, location);
2990 // Find a named object in an expression.
2993 Find_named_object::expression(Expression** pexpr)
2995 switch ((*pexpr)->classification())
2997 case Expression::EXPRESSION_CONST_REFERENCE:
2999 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
3000 if (ce->named_object() == this->no_)
3003 // We need to check a constant initializer explicitly, as
3004 // loops here will not be caught by the loop checking for
3005 // variable initializers.
3006 ce->check_for_init_loop();
3008 return TRAVERSE_CONTINUE;
3011 case Expression::EXPRESSION_VAR_REFERENCE:
3012 if ((*pexpr)->var_expression()->named_object() == this->no_)
3014 return TRAVERSE_CONTINUE;
3015 case Expression::EXPRESSION_FUNC_REFERENCE:
3016 if ((*pexpr)->func_expression()->named_object() == this->no_)
3018 return TRAVERSE_CONTINUE;
3020 return TRAVERSE_CONTINUE;
3022 this->found_ = true;
3023 return TRAVERSE_EXIT;
3028 class Nil_expression : public Expression
3031 Nil_expression(source_location location)
3032 : Expression(EXPRESSION_NIL, location)
3040 do_is_constant() const
3045 { return Type::make_nil_type(); }
3048 do_determine_type(const Type_context*)
3056 do_get_tree(Translate_context*)
3057 { return null_pointer_node; }
3060 do_export(Export* exp) const
3061 { exp->write_c_string("nil"); }
3064 do_dump_expression(Ast_dump_context* ast_dump_context) const
3065 { ast_dump_context->ostream() << "nil"; }
3068 // Import a nil expression.
3071 Nil_expression::do_import(Import* imp)
3073 imp->require_c_string("nil");
3074 return Expression::make_nil(imp->location());
3077 // Make a nil expression.
3080 Expression::make_nil(source_location location)
3082 return new Nil_expression(location);
3085 // The value of the predeclared constant iota. This is little more
3086 // than a marker. This will be lowered to an integer in
3087 // Const_expression::do_lower, which is where we know the value that
3090 class Iota_expression : public Parser_expression
3093 Iota_expression(source_location location)
3094 : Parser_expression(EXPRESSION_IOTA, location)
3099 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
3100 { go_unreachable(); }
3102 // There should only ever be one of these.
3105 { go_unreachable(); }
3108 do_dump_expression(Ast_dump_context* ast_dump_context) const
3109 { ast_dump_context->ostream() << "iota"; }
3112 // Make an iota expression. This is only called for one case: the
3113 // value of the predeclared constant iota.
3116 Expression::make_iota()
3118 static Iota_expression iota_expression(UNKNOWN_LOCATION);
3119 return &iota_expression;
3122 // A type conversion expression.
3124 class Type_conversion_expression : public Expression
3127 Type_conversion_expression(Type* type, Expression* expr,
3128 source_location location)
3129 : Expression(EXPRESSION_CONVERSION, location),
3130 type_(type), expr_(expr), may_convert_function_types_(false)
3133 // Return the type to which we are converting.
3136 { return this->type_; }
3138 // Return the expression which we are converting.
3141 { return this->expr_; }
3143 // Permit converting from one function type to another. This is
3144 // used internally for method expressions.
3146 set_may_convert_function_types()
3148 this->may_convert_function_types_ = true;
3151 // Import a type conversion expression.
3157 do_traverse(Traverse* traverse);
3160 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3163 do_is_constant() const
3164 { return this->expr_->is_constant(); }
3167 do_integer_constant_value(bool, mpz_t, Type**) const;
3170 do_float_constant_value(mpfr_t, Type**) const;
3173 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3176 do_string_constant_value(std::string*) const;
3180 { return this->type_; }
3183 do_determine_type(const Type_context*)
3185 Type_context subcontext(this->type_, false);
3186 this->expr_->determine_type(&subcontext);
3190 do_check_types(Gogo*);
3195 return new Type_conversion_expression(this->type_, this->expr_->copy(),
3200 do_get_tree(Translate_context* context);
3203 do_export(Export*) const;
3206 do_dump_expression(Ast_dump_context*) const;
3209 // The type to convert to.
3211 // The expression to convert.
3213 // True if this is permitted to convert function types. This is
3214 // used internally for method expressions.
3215 bool may_convert_function_types_;
3221 Type_conversion_expression::do_traverse(Traverse* traverse)
3223 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3224 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3225 return TRAVERSE_EXIT;
3226 return TRAVERSE_CONTINUE;
3229 // Convert to a constant at lowering time.
3232 Type_conversion_expression::do_lower(Gogo*, Named_object*,
3233 Statement_inserter*, int)
3235 Type* type = this->type_;
3236 Expression* val = this->expr_;
3237 source_location location = this->location();
3239 if (type->integer_type() != NULL)
3244 if (val->integer_constant_value(false, ival, &dummy))
3246 if (!Integer_expression::check_constant(ival, type, location))
3247 mpz_set_ui(ival, 0);
3248 Expression* ret = Expression::make_integer(&ival, type, location);
3255 if (val->float_constant_value(fval, &dummy))
3257 if (!mpfr_integer_p(fval))
3260 "floating point constant truncated to integer");
3261 return Expression::make_error(location);
3263 mpfr_get_z(ival, fval, GMP_RNDN);
3264 if (!Integer_expression::check_constant(ival, type, location))
3265 mpz_set_ui(ival, 0);
3266 Expression* ret = Expression::make_integer(&ival, type, location);
3275 if (type->float_type() != NULL)
3280 if (val->float_constant_value(fval, &dummy))
3282 if (!Float_expression::check_constant(fval, type, location))
3283 mpfr_set_ui(fval, 0, GMP_RNDN);
3284 Float_expression::constrain_float(fval, type);
3285 Expression *ret = Expression::make_float(&fval, type, location);
3292 if (type->complex_type() != NULL)
3299 if (val->complex_constant_value(real, imag, &dummy))
3301 if (!Complex_expression::check_constant(real, imag, type, location))
3303 mpfr_set_ui(real, 0, GMP_RNDN);
3304 mpfr_set_ui(imag, 0, GMP_RNDN);
3306 Complex_expression::constrain_complex(real, imag, type);
3307 Expression* ret = Expression::make_complex(&real, &imag, type,
3317 if (type->is_slice_type() && type->named_type() == NULL)
3319 Type* element_type = type->array_type()->element_type()->forwarded();
3320 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3321 bool is_int = element_type == Type::lookup_integer_type("int");
3322 if (is_byte || is_int)
3325 if (val->string_constant_value(&s))
3327 Expression_list* vals = new Expression_list();
3330 for (std::string::const_iterator p = s.begin();
3335 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3336 Expression* v = Expression::make_integer(&val,
3345 const char *p = s.data();
3346 const char *pend = s.data() + s.length();
3350 int adv = Lex::fetch_char(p, &c);
3353 warning_at(this->location(), 0,
3354 "invalid UTF-8 encoding");
3359 mpz_init_set_ui(val, c);
3360 Expression* v = Expression::make_integer(&val,
3368 return Expression::make_slice_composite_literal(type, vals,
3377 // Return the constant integer value if there is one.
3380 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3384 if (this->type_->integer_type() == NULL)
3390 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3392 if (!Integer_expression::check_constant(ival, this->type_,
3400 *ptype = this->type_;
3407 if (this->expr_->float_constant_value(fval, &dummy))
3409 mpfr_get_z(val, fval, GMP_RNDN);
3411 if (!Integer_expression::check_constant(val, this->type_,
3414 *ptype = this->type_;
3422 // Return the constant floating point value if there is one.
3425 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3428 if (this->type_->float_type() == NULL)
3434 if (this->expr_->float_constant_value(fval, &dummy))
3436 if (!Float_expression::check_constant(fval, this->type_,
3442 mpfr_set(val, fval, GMP_RNDN);
3444 Float_expression::constrain_float(val, this->type_);
3445 *ptype = this->type_;
3453 // Return the constant complex value if there is one.
3456 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3460 if (this->type_->complex_type() == NULL)
3468 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3470 if (!Complex_expression::check_constant(rval, ival, this->type_,
3477 mpfr_set(real, rval, GMP_RNDN);
3478 mpfr_set(imag, ival, GMP_RNDN);
3481 Complex_expression::constrain_complex(real, imag, this->type_);
3482 *ptype = this->type_;
3491 // Return the constant string value if there is one.
3494 Type_conversion_expression::do_string_constant_value(std::string* val) const
3496 if (this->type_->is_string_type()
3497 && this->expr_->type()->integer_type() != NULL)
3502 if (this->expr_->integer_constant_value(false, ival, &dummy))
3504 unsigned long ulval = mpz_get_ui(ival);
3505 if (mpz_cmp_ui(ival, ulval) == 0)
3507 Lex::append_char(ulval, true, val, this->location());
3515 // FIXME: Could handle conversion from const []int here.
3520 // Check that types are convertible.
3523 Type_conversion_expression::do_check_types(Gogo*)
3525 Type* type = this->type_;
3526 Type* expr_type = this->expr_->type();
3529 if (type->is_error() || expr_type->is_error())
3531 this->set_is_error();
3535 if (this->may_convert_function_types_
3536 && type->function_type() != NULL