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.
13 #ifndef ENABLE_BUILD_WITH_CXX
22 #include "tree-iterator.h"
27 #ifndef ENABLE_BUILD_WITH_CXX
36 #include "statements.h"
40 #include "expressions.h"
45 Expression::Expression(Expression_classification classification,
47 : classification_(classification), location_(location)
51 Expression::~Expression()
55 // Traverse the expressions.
58 Expression::traverse(Expression** pexpr, Traverse* traverse)
60 Expression* expr = *pexpr;
61 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
63 int t = traverse->expression(pexpr);
64 if (t == TRAVERSE_EXIT)
66 else if (t == TRAVERSE_SKIP_COMPONENTS)
67 return TRAVERSE_CONTINUE;
69 return expr->do_traverse(traverse);
72 // Traverse subexpressions of this expression.
75 Expression::traverse_subexpressions(Traverse* traverse)
77 return this->do_traverse(traverse);
80 // Default implementation for do_traverse for child classes.
83 Expression::do_traverse(Traverse*)
85 return TRAVERSE_CONTINUE;
88 // This virtual function is called by the parser if the value of this
89 // expression is being discarded. By default, we give an error.
90 // Expressions with side effects override.
93 Expression::do_discarding_value()
95 this->unused_value_error();
98 // This virtual function is called to export expressions. This will
99 // only be used by expressions which may be constant.
102 Expression::do_export(Export*) const
107 // Give an error saying that the value of the expression is not used.
110 Expression::unused_value_error()
112 error_at(this->location(), "value computed is not used");
115 // Note that this expression is an error. This is called by children
116 // when they discover an error.
119 Expression::set_is_error()
121 this->classification_ = EXPRESSION_ERROR;
124 // For children to call to report an error conveniently.
127 Expression::report_error(const char* msg)
129 error_at(this->location_, "%s", msg);
130 this->set_is_error();
133 // Set types of variables and constants. This is implemented by the
137 Expression::determine_type(const Type_context* context)
139 this->do_determine_type(context);
142 // Set types when there is no context.
145 Expression::determine_type_no_context()
147 Type_context context;
148 this->do_determine_type(&context);
151 // Return a tree handling any conversions which must be done during
155 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
156 Type* rhs_type, tree rhs_tree,
159 if (lhs_type->is_error() || rhs_type->is_error())
160 return error_mark_node;
162 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
163 return error_mark_node;
165 Gogo* gogo = context->gogo();
167 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
168 if (lhs_type_tree == error_mark_node)
169 return error_mark_node;
171 if (lhs_type != rhs_type && lhs_type->interface_type() != NULL)
173 if (rhs_type->interface_type() == NULL)
174 return Expression::convert_type_to_interface(context, lhs_type,
178 return Expression::convert_interface_to_interface(context, lhs_type,
182 else if (lhs_type != rhs_type && rhs_type->interface_type() != NULL)
183 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
185 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
187 // Assigning nil to an open array.
188 go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
190 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
192 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
193 tree field = TYPE_FIELDS(lhs_type_tree);
194 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
197 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
199 elt = VEC_quick_push(constructor_elt, init, NULL);
200 field = DECL_CHAIN(field);
201 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
204 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
206 elt = VEC_quick_push(constructor_elt, init, NULL);
207 field = DECL_CHAIN(field);
208 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
211 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
213 tree val = build_constructor(lhs_type_tree, init);
214 TREE_CONSTANT(val) = 1;
218 else if (rhs_type->is_nil_type())
220 // The left hand side should be a pointer type at the tree
222 go_assert(POINTER_TYPE_P(lhs_type_tree));
223 return fold_convert(lhs_type_tree, null_pointer_node);
225 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
227 // No conversion is needed.
230 else if (POINTER_TYPE_P(lhs_type_tree)
231 || INTEGRAL_TYPE_P(lhs_type_tree)
232 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
233 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
234 return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree);
235 else if ((TREE_CODE(lhs_type_tree) == RECORD_TYPE
236 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
237 || (TREE_CODE(lhs_type_tree) == ARRAY_TYPE
238 && TREE_CODE(TREE_TYPE(rhs_tree)) == ARRAY_TYPE))
240 // Avoid confusion from zero sized variables which may be
241 // represented as non-zero-sized.
242 if (int_size_in_bytes(lhs_type_tree) == 0
243 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
246 // This conversion must be permitted by Go, or we wouldn't have
248 go_assert(int_size_in_bytes(lhs_type_tree)
249 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
250 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
251 lhs_type_tree, rhs_tree);
255 go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
260 // Return a tree for a conversion from a non-interface type to an
264 Expression::convert_type_to_interface(Translate_context* context,
265 Type* lhs_type, Type* rhs_type,
266 tree rhs_tree, Location location)
268 Gogo* gogo = context->gogo();
269 Interface_type* lhs_interface_type = lhs_type->interface_type();
270 bool lhs_is_empty = lhs_interface_type->is_empty();
272 // Since RHS_TYPE is a static type, we can create the interface
273 // method table at compile time.
275 // When setting an interface to nil, we just set both fields to
277 if (rhs_type->is_nil_type())
279 Btype* lhs_btype = lhs_type->get_backend(gogo);
280 return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
283 // This should have been checked already.
284 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
286 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
287 if (lhs_type_tree == error_mark_node)
288 return error_mark_node;
290 // An interface is a tuple. If LHS_TYPE is an empty interface type,
291 // then the first field is the type descriptor for RHS_TYPE.
292 // Otherwise it is the interface method table for RHS_TYPE.
293 tree first_field_value;
295 first_field_value = rhs_type->type_descriptor_pointer(gogo, location);
298 // Build the interface method table for this interface and this
299 // object type: a list of function pointers for each interface
301 Named_type* rhs_named_type = rhs_type->named_type();
302 bool is_pointer = false;
303 if (rhs_named_type == NULL)
305 rhs_named_type = rhs_type->deref()->named_type();
309 if (rhs_named_type == NULL)
310 method_table = null_pointer_node;
313 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
315 first_field_value = fold_convert_loc(location.gcc_location(),
316 const_ptr_type_node, method_table);
318 if (first_field_value == error_mark_node)
319 return error_mark_node;
321 // Start building a constructor for the value we will return.
323 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
325 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
326 tree field = TYPE_FIELDS(lhs_type_tree);
327 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
328 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
330 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
333 elt = VEC_quick_push(constructor_elt, init, NULL);
334 field = DECL_CHAIN(field);
335 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
338 if (rhs_type->points_to() != NULL)
340 // We are assigning a pointer to the interface; the interface
341 // holds the pointer itself.
342 elt->value = rhs_tree;
343 return build_constructor(lhs_type_tree, init);
346 // We are assigning a non-pointer value to the interface; the
347 // interface gets a copy of the value in the heap.
349 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
351 tree space = gogo->allocate_memory(rhs_type, object_size, location);
352 space = fold_convert_loc(location.gcc_location(),
353 build_pointer_type(TREE_TYPE(rhs_tree)), space);
354 space = save_expr(space);
356 tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space);
357 TREE_THIS_NOTRAP(ref) = 1;
358 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
359 void_type_node, ref, rhs_tree);
361 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
364 return build2(COMPOUND_EXPR, lhs_type_tree, set,
365 build_constructor(lhs_type_tree, init));
368 // Return a tree for the type descriptor of RHS_TREE, which has
369 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
373 Expression::get_interface_type_descriptor(Translate_context*,
374 Type* rhs_type, tree rhs_tree,
377 tree rhs_type_tree = TREE_TYPE(rhs_tree);
378 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
379 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
380 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
382 if (rhs_type->interface_type()->is_empty())
384 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
385 "__type_descriptor") == 0);
389 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
391 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
393 tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v);
394 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
395 tree f = TYPE_FIELDS(TREE_TYPE(v1));
396 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
398 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
400 tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node,
401 v, fold_convert_loc(location.gcc_location(),
404 tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1),
406 return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1),
410 // Return a tree for the conversion of an interface type to an
414 Expression::convert_interface_to_interface(Translate_context* context,
415 Type *lhs_type, Type *rhs_type,
416 tree rhs_tree, bool for_type_guard,
419 Gogo* gogo = context->gogo();
420 Interface_type* lhs_interface_type = lhs_type->interface_type();
421 bool lhs_is_empty = lhs_interface_type->is_empty();
423 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
424 if (lhs_type_tree == error_mark_node)
425 return error_mark_node;
427 // In the general case this requires runtime examination of the type
428 // method table to match it up with the interface methods.
430 // FIXME: If all of the methods in the right hand side interface
431 // also appear in the left hand side interface, then we don't need
432 // to do a runtime check, although we still need to build a new
435 // Get the type descriptor for the right hand side. This will be
436 // NULL for a nil interface.
438 if (!DECL_P(rhs_tree))
439 rhs_tree = save_expr(rhs_tree);
441 tree rhs_type_descriptor =
442 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
445 // The result is going to be a two element constructor.
447 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
449 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
450 tree field = TYPE_FIELDS(lhs_type_tree);
455 // A type assertion fails when converting a nil interface.
456 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
458 static tree assert_interface_decl;
459 tree call = Gogo::call_builtin(&assert_interface_decl,
461 "__go_assert_interface",
464 TREE_TYPE(lhs_type_descriptor),
466 TREE_TYPE(rhs_type_descriptor),
467 rhs_type_descriptor);
468 if (call == error_mark_node)
469 return error_mark_node;
470 // This will panic if the interface conversion fails.
471 TREE_NOTHROW(assert_interface_decl) = 0;
472 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
475 else if (lhs_is_empty)
477 // A convertion to an empty interface always succeeds, and the
478 // first field is just the type descriptor of the object.
479 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
480 "__type_descriptor") == 0);
481 elt->value = fold_convert_loc(location.gcc_location(),
482 TREE_TYPE(field), rhs_type_descriptor);
486 // A conversion to a non-empty interface may fail, but unlike a
487 // type assertion converting nil will always succeed.
488 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
490 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
492 static tree convert_interface_decl;
493 tree call = Gogo::call_builtin(&convert_interface_decl,
495 "__go_convert_interface",
498 TREE_TYPE(lhs_type_descriptor),
500 TREE_TYPE(rhs_type_descriptor),
501 rhs_type_descriptor);
502 if (call == error_mark_node)
503 return error_mark_node;
504 // This will panic if the interface conversion fails.
505 TREE_NOTHROW(convert_interface_decl) = 0;
506 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
510 // The second field is simply the object pointer.
512 elt = VEC_quick_push(constructor_elt, init, NULL);
513 field = DECL_CHAIN(field);
514 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
517 tree rhs_type_tree = TREE_TYPE(rhs_tree);
518 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
519 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
520 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
521 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
524 return build_constructor(lhs_type_tree, init);
527 // Return a tree for the conversion of an interface type to a
528 // non-interface type.
531 Expression::convert_interface_to_type(Translate_context* context,
532 Type *lhs_type, Type* rhs_type,
533 tree rhs_tree, Location location)
535 Gogo* gogo = context->gogo();
536 tree rhs_type_tree = TREE_TYPE(rhs_tree);
538 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
539 if (lhs_type_tree == error_mark_node)
540 return error_mark_node;
542 // Call a function to check that the type is valid. The function
543 // will panic with an appropriate runtime type error if the type is
546 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location);
548 if (!DECL_P(rhs_tree))
549 rhs_tree = save_expr(rhs_tree);
551 tree rhs_type_descriptor =
552 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
555 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo,
558 static tree check_interface_type_decl;
559 tree call = Gogo::call_builtin(&check_interface_type_decl,
561 "__go_check_interface_type",
564 TREE_TYPE(lhs_type_descriptor),
566 TREE_TYPE(rhs_type_descriptor),
568 TREE_TYPE(rhs_inter_descriptor),
569 rhs_inter_descriptor);
570 if (call == error_mark_node)
571 return error_mark_node;
572 // This call will panic if the conversion is invalid.
573 TREE_NOTHROW(check_interface_type_decl) = 0;
575 // If the call succeeds, pull out the value.
576 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
577 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
578 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
579 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
582 // If the value is a pointer, then it is the value we want.
583 // Otherwise it points to the value.
584 if (lhs_type->points_to() == NULL)
586 val = fold_convert_loc(location.gcc_location(),
587 build_pointer_type(lhs_type_tree), val);
588 val = build_fold_indirect_ref_loc(location.gcc_location(), val);
591 return build2(COMPOUND_EXPR, lhs_type_tree, call,
592 fold_convert_loc(location.gcc_location(), lhs_type_tree, val));
595 // Convert an expression to a tree. This is implemented by the child
596 // class. Not that it is not in general safe to call this multiple
597 // times for a single expression, but that we don't catch such errors.
600 Expression::get_tree(Translate_context* context)
602 // The child may have marked this expression as having an error.
603 if (this->classification_ == EXPRESSION_ERROR)
604 return error_mark_node;
606 return this->do_get_tree(context);
609 // Return a tree for VAL in TYPE.
612 Expression::integer_constant_tree(mpz_t val, tree type)
614 if (type == error_mark_node)
615 return error_mark_node;
616 else if (TREE_CODE(type) == INTEGER_TYPE)
617 return double_int_to_tree(type,
618 mpz_get_double_int(type, val, true));
619 else if (TREE_CODE(type) == REAL_TYPE)
622 mpfr_init_set_z(fval, val, GMP_RNDN);
623 tree ret = Expression::float_constant_tree(fval, type);
627 else if (TREE_CODE(type) == COMPLEX_TYPE)
630 mpfr_init_set_z(fval, val, GMP_RNDN);
631 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
633 tree imag = build_real_from_int_cst(TREE_TYPE(type),
635 return build_complex(type, real, imag);
641 // Return a tree for VAL in TYPE.
644 Expression::float_constant_tree(mpfr_t val, tree type)
646 if (type == error_mark_node)
647 return error_mark_node;
648 else if (TREE_CODE(type) == INTEGER_TYPE)
652 mpfr_get_z(ival, val, GMP_RNDN);
653 tree ret = Expression::integer_constant_tree(ival, type);
657 else if (TREE_CODE(type) == REAL_TYPE)
660 real_from_mpfr(&r1, val, type, GMP_RNDN);
662 real_convert(&r2, TYPE_MODE(type), &r1);
663 return build_real(type, r2);
665 else if (TREE_CODE(type) == COMPLEX_TYPE)
668 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
670 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
671 tree imag = build_real_from_int_cst(TREE_TYPE(type),
673 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
679 // Return a tree for REAL/IMAG in TYPE.
682 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
684 if (type == error_mark_node)
685 return error_mark_node;
686 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
687 return Expression::float_constant_tree(real, type);
688 else if (TREE_CODE(type) == COMPLEX_TYPE)
691 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
693 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
696 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
698 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
700 return build_complex(type, build_real(TREE_TYPE(type), r2),
701 build_real(TREE_TYPE(type), r4));
707 // Return a tree which evaluates to true if VAL, of arbitrary integer
708 // type, is negative or is more than the maximum value of BOUND_TYPE.
709 // If SOFAR is not NULL, it is or'red into the result. The return
710 // value may be NULL if SOFAR is NULL.
713 Expression::check_bounds(tree val, tree bound_type, tree sofar,
716 tree val_type = TREE_TYPE(val);
717 tree ret = NULL_TREE;
719 if (!TYPE_UNSIGNED(val_type))
721 ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val,
722 build_int_cst(val_type, 0));
723 if (ret == boolean_false_node)
727 HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
728 HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
729 go_assert(val_type_size != -1 && bound_type_size != -1);
730 if (val_type_size > bound_type_size
731 || (val_type_size == bound_type_size
732 && TYPE_UNSIGNED(val_type)
733 && !TYPE_UNSIGNED(bound_type)))
735 tree max = TYPE_MAX_VALUE(bound_type);
736 tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node,
737 val, fold_convert_loc(loc.gcc_location(),
739 if (big == boolean_false_node)
741 else if (ret == NULL_TREE)
744 ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
745 boolean_type_node, ret, big);
748 if (ret == NULL_TREE)
750 else if (sofar == NULL_TREE)
753 return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node,
758 Expression::dump_expression(Ast_dump_context* ast_dump_context) const
760 this->do_dump_expression(ast_dump_context);
763 // Error expressions. This are used to avoid cascading errors.
765 class Error_expression : public Expression
768 Error_expression(Location location)
769 : Expression(EXPRESSION_ERROR, location)
774 do_is_constant() const
778 do_numeric_constant_value(Numeric_constant* nc) const
780 nc->set_unsigned_long(NULL, 0);
785 do_discarding_value()
790 { return Type::make_error_type(); }
793 do_determine_type(const Type_context*)
801 do_is_addressable() const
805 do_get_tree(Translate_context*)
806 { return error_mark_node; }
809 do_dump_expression(Ast_dump_context*) const;
812 // Dump the ast representation for an error expression to a dump context.
815 Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
817 ast_dump_context->ostream() << "_Error_" ;
821 Expression::make_error(Location location)
823 return new Error_expression(location);
826 // An expression which is really a type. This is used during parsing.
827 // It is an error if these survive after lowering.
830 Type_expression : public Expression
833 Type_expression(Type* type, Location location)
834 : Expression(EXPRESSION_TYPE, location),
840 do_traverse(Traverse* traverse)
841 { return Type::traverse(this->type_, traverse); }
845 { return this->type_; }
848 do_determine_type(const Type_context*)
852 do_check_types(Gogo*)
853 { this->report_error(_("invalid use of type")); }
860 do_get_tree(Translate_context*)
861 { go_unreachable(); }
863 void do_dump_expression(Ast_dump_context*) const;
866 // The type which we are representing as an expression.
871 Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
873 ast_dump_context->dump_type(this->type_);
877 Expression::make_type(Type* type, Location location)
879 return new Type_expression(type, location);
882 // Class Parser_expression.
885 Parser_expression::do_type()
887 // We should never really ask for the type of a Parser_expression.
888 // However, it can happen, at least when we have an invalid const
889 // whose initializer refers to the const itself. In that case we
890 // may ask for the type when lowering the const itself.
891 go_assert(saw_errors());
892 return Type::make_error_type();
895 // Class Var_expression.
897 // Lower a variable expression. Here we just make sure that the
898 // initialization expression of the variable has been lowered. This
899 // ensures that we will be able to determine the type of the variable
903 Var_expression::do_lower(Gogo* gogo, Named_object* function,
904 Statement_inserter* inserter, int)
906 if (this->variable_->is_variable())
908 Variable* var = this->variable_->var_value();
909 // This is either a local variable or a global variable. A
910 // reference to a variable which is local to an enclosing
911 // function will be a reference to a field in a closure.
912 if (var->is_global())
917 var->lower_init_expression(gogo, function, inserter);
922 // Return the type of a reference to a variable.
925 Var_expression::do_type()
927 if (this->variable_->is_variable())
928 return this->variable_->var_value()->type();
929 else if (this->variable_->is_result_variable())
930 return this->variable_->result_var_value()->type();
935 // Determine the type of a reference to a variable.
938 Var_expression::do_determine_type(const Type_context*)
940 if (this->variable_->is_variable())
941 this->variable_->var_value()->determine_type();
944 // Something takes the address of this variable. This means that we
945 // may want to move the variable onto the heap.
948 Var_expression::do_address_taken(bool escapes)
952 if (this->variable_->is_variable())
953 this->variable_->var_value()->set_non_escaping_address_taken();
954 else if (this->variable_->is_result_variable())
955 this->variable_->result_var_value()->set_non_escaping_address_taken();
961 if (this->variable_->is_variable())
962 this->variable_->var_value()->set_address_taken();
963 else if (this->variable_->is_result_variable())
964 this->variable_->result_var_value()->set_address_taken();
970 // Get the tree for a reference to a variable.
973 Var_expression::do_get_tree(Translate_context* context)
975 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
976 context->function());
977 tree ret = var_to_tree(bvar);
978 if (ret == error_mark_node)
979 return error_mark_node;
981 if (this->variable_->is_variable())
982 is_in_heap = this->variable_->var_value()->is_in_heap();
983 else if (this->variable_->is_result_variable())
984 is_in_heap = this->variable_->result_var_value()->is_in_heap();
989 ret = build_fold_indirect_ref_loc(this->location().gcc_location(), ret);
990 TREE_THIS_NOTRAP(ret) = 1;
995 // Ast dump for variable expression.
998 Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1000 ast_dump_context->ostream() << this->variable_->name() ;
1003 // Make a reference to a variable in an expression.
1006 Expression::make_var_reference(Named_object* var, Location location)
1009 return Expression::make_sink(location);
1011 // FIXME: Creating a new object for each reference to a variable is
1013 return new Var_expression(var, location);
1016 // Class Temporary_reference_expression.
1021 Temporary_reference_expression::do_type()
1023 return this->statement_->type();
1026 // Called if something takes the address of this temporary variable.
1027 // We never have to move temporary variables to the heap, but we do
1028 // need to know that they must live in the stack rather than in a
1032 Temporary_reference_expression::do_address_taken(bool)
1034 this->statement_->set_is_address_taken();
1037 // Get a tree referring to the variable.
1040 Temporary_reference_expression::do_get_tree(Translate_context* context)
1042 Bvariable* bvar = this->statement_->get_backend_variable(context);
1044 // The gcc backend can't represent the same set of recursive types
1045 // that the Go frontend can. In some cases this means that a
1046 // temporary variable won't have the right backend type. Correct
1047 // that here by adding a type cast. We need to use base() to push
1048 // the circularity down one level.
1049 tree ret = var_to_tree(bvar);
1050 if (!this->is_lvalue_
1051 && POINTER_TYPE_P(TREE_TYPE(ret))
1052 && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
1054 Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1055 tree type_tree = type_to_tree(type_btype);
1056 ret = fold_convert_loc(this->location().gcc_location(), type_tree, ret);
1061 // Ast dump for temporary reference.
1064 Temporary_reference_expression::do_dump_expression(
1065 Ast_dump_context* ast_dump_context) const
1067 ast_dump_context->dump_temp_variable_name(this->statement_);
1070 // Make a reference to a temporary variable.
1072 Temporary_reference_expression*
1073 Expression::make_temporary_reference(Temporary_statement* statement,
1076 return new Temporary_reference_expression(statement, location);
1079 // Class Set_and_use_temporary_expression.
1084 Set_and_use_temporary_expression::do_type()
1086 return this->statement_->type();
1089 // Take the address.
1092 Set_and_use_temporary_expression::do_address_taken(bool)
1094 this->statement_->set_is_address_taken();
1097 // Return the backend representation.
1100 Set_and_use_temporary_expression::do_get_tree(Translate_context* context)
1102 Bvariable* bvar = this->statement_->get_backend_variable(context);
1103 tree var_tree = var_to_tree(bvar);
1104 tree expr_tree = this->expr_->get_tree(context);
1105 if (var_tree == error_mark_node || expr_tree == error_mark_node)
1106 return error_mark_node;
1107 Location loc = this->location();
1108 return build2_loc(loc.gcc_location(), COMPOUND_EXPR, TREE_TYPE(var_tree),
1109 build2_loc(loc.gcc_location(), MODIFY_EXPR, void_type_node,
1110 var_tree, expr_tree),
1117 Set_and_use_temporary_expression::do_dump_expression(
1118 Ast_dump_context* ast_dump_context) const
1120 ast_dump_context->ostream() << '(';
1121 ast_dump_context->dump_temp_variable_name(this->statement_);
1122 ast_dump_context->ostream() << " = ";
1123 this->expr_->dump_expression(ast_dump_context);
1124 ast_dump_context->ostream() << ')';
1127 // Make a set-and-use temporary.
1129 Set_and_use_temporary_expression*
1130 Expression::make_set_and_use_temporary(Temporary_statement* statement,
1131 Expression* expr, Location location)
1133 return new Set_and_use_temporary_expression(statement, expr, location);
1136 // A sink expression--a use of the blank identifier _.
1138 class Sink_expression : public Expression
1141 Sink_expression(Location location)
1142 : Expression(EXPRESSION_SINK, location),
1143 type_(NULL), var_(NULL_TREE)
1148 do_discarding_value()
1155 do_determine_type(const Type_context*);
1159 { return new Sink_expression(this->location()); }
1162 do_get_tree(Translate_context*);
1165 do_dump_expression(Ast_dump_context*) const;
1168 // The type of this sink variable.
1170 // The temporary variable we generate.
1174 // Return the type of a sink expression.
1177 Sink_expression::do_type()
1179 if (this->type_ == NULL)
1180 return Type::make_sink_type();
1184 // Determine the type of a sink expression.
1187 Sink_expression::do_determine_type(const Type_context* context)
1189 if (context->type != NULL)
1190 this->type_ = context->type;
1193 // Return a temporary variable for a sink expression. This will
1194 // presumably be a write-only variable which the middle-end will drop.
1197 Sink_expression::do_get_tree(Translate_context* context)
1199 if (this->var_ == NULL_TREE)
1201 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
1202 Btype* bt = this->type_->get_backend(context->gogo());
1203 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
1208 // Ast dump for sink expression.
1211 Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1213 ast_dump_context->ostream() << "_" ;
1216 // Make a sink expression.
1219 Expression::make_sink(Location location)
1221 return new Sink_expression(location);
1224 // Class Func_expression.
1226 // FIXME: Can a function expression appear in a constant expression?
1227 // The value is unchanging. Initializing a constant to the address of
1228 // a function seems like it could work, though there might be little
1234 Func_expression::do_traverse(Traverse* traverse)
1236 return (this->closure_ == NULL
1238 : Expression::traverse(&this->closure_, traverse));
1241 // Return the type of a function expression.
1244 Func_expression::do_type()
1246 if (this->function_->is_function())
1247 return this->function_->func_value()->type();
1248 else if (this->function_->is_function_declaration())
1249 return this->function_->func_declaration_value()->type();
1254 // Get the tree for a function expression without evaluating the
1258 Func_expression::get_tree_without_closure(Gogo* gogo)
1260 Function_type* fntype;
1261 if (this->function_->is_function())
1262 fntype = this->function_->func_value()->type();
1263 else if (this->function_->is_function_declaration())
1264 fntype = this->function_->func_declaration_value()->type();
1268 // Builtin functions are handled specially by Call_expression. We
1269 // can't take their address.
1270 if (fntype->is_builtin())
1272 error_at(this->location(),
1273 "invalid use of special builtin function %qs; must be called",
1274 this->function_->name().c_str());
1275 return error_mark_node;
1278 Named_object* no = this->function_;
1280 tree id = no->get_id(gogo);
1281 if (id == error_mark_node)
1282 return error_mark_node;
1285 if (no->is_function())
1286 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1287 else if (no->is_function_declaration())
1288 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1292 if (fndecl == error_mark_node)
1293 return error_mark_node;
1295 return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl);
1298 // Get the tree for a function expression. This is used when we take
1299 // the address of a function rather than simply calling it. If the
1300 // function has a closure, we must use a trampoline.
1303 Func_expression::do_get_tree(Translate_context* context)
1305 Gogo* gogo = context->gogo();
1307 tree fnaddr = this->get_tree_without_closure(gogo);
1308 if (fnaddr == error_mark_node)
1309 return error_mark_node;
1311 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1312 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1313 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1315 // If there is no closure, that is all have to do.
1316 if (this->closure_ == NULL)
1319 go_assert(this->function_->func_value()->enclosing() != NULL);
1321 // Get the value of the closure. This will be a pointer to space
1322 // allocated on the heap.
1323 tree closure_tree = this->closure_->get_tree(context);
1324 if (closure_tree == error_mark_node)
1325 return error_mark_node;
1326 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1328 // Now we need to build some code on the heap. This code will load
1329 // the static chain pointer with the closure and then jump to the
1330 // body of the function. The normal gcc approach is to build the
1331 // code on the stack. Unfortunately we can not do that, as Go
1332 // permits us to return the function pointer.
1334 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1337 // Ast dump for function.
1340 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1342 ast_dump_context->ostream() << this->function_->name();
1343 if (this->closure_ != NULL)
1345 ast_dump_context->ostream() << " {closure = ";
1346 this->closure_->dump_expression(ast_dump_context);
1347 ast_dump_context->ostream() << "}";
1351 // Make a reference to a function in an expression.
1354 Expression::make_func_reference(Named_object* function, Expression* closure,
1357 return new Func_expression(function, closure, location);
1360 // Class Unknown_expression.
1362 // Return the name of an unknown expression.
1365 Unknown_expression::name() const
1367 return this->named_object_->name();
1370 // Lower a reference to an unknown name.
1373 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1375 Location location = this->location();
1376 Named_object* no = this->named_object_;
1378 if (!no->is_unknown())
1382 real = no->unknown_value()->real_named_object();
1385 if (this->is_composite_literal_key_)
1387 if (!this->no_error_message_)
1388 error_at(location, "reference to undefined name %qs",
1389 this->named_object_->message_name().c_str());
1390 return Expression::make_error(location);
1393 switch (real->classification())
1395 case Named_object::NAMED_OBJECT_CONST:
1396 return Expression::make_const_reference(real, location);
1397 case Named_object::NAMED_OBJECT_TYPE:
1398 return Expression::make_type(real->type_value(), location);
1399 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1400 if (this->is_composite_literal_key_)
1402 if (!this->no_error_message_)
1403 error_at(location, "reference to undefined type %qs",
1404 real->message_name().c_str());
1405 return Expression::make_error(location);
1406 case Named_object::NAMED_OBJECT_VAR:
1407 real->var_value()->set_is_used();
1408 return Expression::make_var_reference(real, location);
1409 case Named_object::NAMED_OBJECT_FUNC:
1410 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1411 return Expression::make_func_reference(real, NULL, location);
1412 case Named_object::NAMED_OBJECT_PACKAGE:
1413 if (this->is_composite_literal_key_)
1415 if (!this->no_error_message_)
1416 error_at(location, "unexpected reference to package");
1417 return Expression::make_error(location);
1423 // Dump the ast representation for an unknown expression to a dump context.
1426 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1428 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1432 // Make a reference to an unknown name.
1435 Expression::make_unknown_reference(Named_object* no, Location location)
1437 return new Unknown_expression(no, location);
1440 // A boolean expression.
1442 class Boolean_expression : public Expression
1445 Boolean_expression(bool val, Location location)
1446 : Expression(EXPRESSION_BOOLEAN, location),
1447 val_(val), type_(NULL)
1455 do_is_constant() const
1462 do_determine_type(const Type_context*);
1469 do_get_tree(Translate_context*)
1470 { return this->val_ ? boolean_true_node : boolean_false_node; }
1473 do_export(Export* exp) const
1474 { exp->write_c_string(this->val_ ? "true" : "false"); }
1477 do_dump_expression(Ast_dump_context* ast_dump_context) const
1478 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1483 // The type as determined by context.
1490 Boolean_expression::do_type()
1492 if (this->type_ == NULL)
1493 this->type_ = Type::make_boolean_type();
1497 // Set the type from the context.
1500 Boolean_expression::do_determine_type(const Type_context* context)
1502 if (this->type_ != NULL && !this->type_->is_abstract())
1504 else if (context->type != NULL && context->type->is_boolean_type())
1505 this->type_ = context->type;
1506 else if (!context->may_be_abstract)
1507 this->type_ = Type::lookup_bool_type();
1510 // Import a boolean constant.
1513 Boolean_expression::do_import(Import* imp)
1515 if (imp->peek_char() == 't')
1517 imp->require_c_string("true");
1518 return Expression::make_boolean(true, imp->location());
1522 imp->require_c_string("false");
1523 return Expression::make_boolean(false, imp->location());
1527 // Make a boolean expression.
1530 Expression::make_boolean(bool val, Location location)
1532 return new Boolean_expression(val, location);
1535 // Class String_expression.
1540 String_expression::do_type()
1542 if (this->type_ == NULL)
1543 this->type_ = Type::make_string_type();
1547 // Set the type from the context.
1550 String_expression::do_determine_type(const Type_context* context)
1552 if (this->type_ != NULL && !this->type_->is_abstract())
1554 else if (context->type != NULL && context->type->is_string_type())
1555 this->type_ = context->type;
1556 else if (!context->may_be_abstract)
1557 this->type_ = Type::lookup_string_type();
1560 // Build a string constant.
1563 String_expression::do_get_tree(Translate_context* context)
1565 return context->gogo()->go_string_constant_tree(this->val_);
1568 // Write string literal to string dump.
1571 String_expression::export_string(String_dump* exp,
1572 const String_expression* str)
1575 s.reserve(str->val_.length() * 4 + 2);
1577 for (std::string::const_iterator p = str->val_.begin();
1578 p != str->val_.end();
1581 if (*p == '\\' || *p == '"')
1586 else if (*p >= 0x20 && *p < 0x7f)
1588 else if (*p == '\n')
1590 else if (*p == '\t')
1595 unsigned char c = *p;
1596 unsigned int dig = c >> 4;
1597 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1599 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1603 exp->write_string(s);
1606 // Export a string expression.
1609 String_expression::do_export(Export* exp) const
1611 String_expression::export_string(exp, this);
1614 // Import a string expression.
1617 String_expression::do_import(Import* imp)
1619 imp->require_c_string("\"");
1623 int c = imp->get_char();
1624 if (c == '"' || c == -1)
1627 val += static_cast<char>(c);
1630 c = imp->get_char();
1631 if (c == '\\' || c == '"')
1632 val += static_cast<char>(c);
1639 c = imp->get_char();
1640 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1641 c = imp->get_char();
1642 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1643 char v = (vh << 4) | vl;
1648 error_at(imp->location(), "bad string constant");
1649 return Expression::make_error(imp->location());
1653 return Expression::make_string(val, imp->location());
1656 // Ast dump for string expression.
1659 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1661 String_expression::export_string(ast_dump_context, this);
1664 // Make a string expression.
1667 Expression::make_string(const std::string& val, Location location)
1669 return new String_expression(val, location);
1672 // Make an integer expression.
1674 class Integer_expression : public Expression
1677 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
1679 : Expression(EXPRESSION_INTEGER, location),
1680 type_(type), is_character_constant_(is_character_constant)
1681 { mpz_init_set(this->val_, *val); }
1686 // Write VAL to string dump.
1688 export_integer(String_dump* exp, const mpz_t val);
1690 // Write VAL to dump context.
1692 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1696 do_is_constant() const
1700 do_numeric_constant_value(Numeric_constant* nc) const;
1706 do_determine_type(const Type_context* context);
1709 do_check_types(Gogo*);
1712 do_get_tree(Translate_context*);
1717 if (this->is_character_constant_)
1718 return Expression::make_character(&this->val_, this->type_,
1721 return Expression::make_integer(&this->val_, this->type_,
1726 do_export(Export*) const;
1729 do_dump_expression(Ast_dump_context*) const;
1732 // The integer value.
1736 // Whether this is a character constant.
1737 bool is_character_constant_;
1740 // Return a numeric constant for this expression. We have to mark
1741 // this as a character when appropriate.
1744 Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const
1746 if (this->is_character_constant_)
1747 nc->set_rune(this->type_, this->val_);
1749 nc->set_int(this->type_, this->val_);
1753 // Return the current type. If we haven't set the type yet, we return
1754 // an abstract integer type.
1757 Integer_expression::do_type()
1759 if (this->type_ == NULL)
1761 if (this->is_character_constant_)
1762 this->type_ = Type::make_abstract_character_type();
1764 this->type_ = Type::make_abstract_integer_type();
1769 // Set the type of the integer value. Here we may switch from an
1770 // abstract type to a real type.
1773 Integer_expression::do_determine_type(const Type_context* context)
1775 if (this->type_ != NULL && !this->type_->is_abstract())
1777 else if (context->type != NULL && context->type->is_numeric_type())
1778 this->type_ = context->type;
1779 else if (!context->may_be_abstract)
1781 if (this->is_character_constant_)
1782 this->type_ = Type::lookup_integer_type("int32");
1784 this->type_ = Type::lookup_integer_type("int");
1788 // Check the type of an integer constant.
1791 Integer_expression::do_check_types(Gogo*)
1793 Type* type = this->type_;
1796 Numeric_constant nc;
1797 if (this->is_character_constant_)
1798 nc.set_rune(NULL, this->val_);
1800 nc.set_int(NULL, this->val_);
1801 if (!nc.set_type(type, true, this->location()))
1802 this->set_is_error();
1805 // Get a tree for an integer constant.
1808 Integer_expression::do_get_tree(Translate_context* context)
1810 Gogo* gogo = context->gogo();
1812 if (this->type_ != NULL && !this->type_->is_abstract())
1813 type = type_to_tree(this->type_->get_backend(gogo));
1814 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1816 // We are converting to an abstract floating point type.
1817 Type* ftype = Type::lookup_float_type("float64");
1818 type = type_to_tree(ftype->get_backend(gogo));
1820 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1822 // We are converting to an abstract complex type.
1823 Type* ctype = Type::lookup_complex_type("complex128");
1824 type = type_to_tree(ctype->get_backend(gogo));
1828 // If we still have an abstract type here, then this is being
1829 // used in a constant expression which didn't get reduced for
1830 // some reason. Use a type which will fit the value. We use <,
1831 // not <=, because we need an extra bit for the sign bit.
1832 int bits = mpz_sizeinbase(this->val_, 2);
1833 if (bits < INT_TYPE_SIZE)
1835 Type* t = Type::lookup_integer_type("int");
1836 type = type_to_tree(t->get_backend(gogo));
1840 Type* t = Type::lookup_integer_type("int64");
1841 type = type_to_tree(t->get_backend(gogo));
1844 type = long_long_integer_type_node;
1846 return Expression::integer_constant_tree(this->val_, type);
1849 // Write VAL to export data.
1852 Integer_expression::export_integer(String_dump* exp, const mpz_t val)
1854 char* s = mpz_get_str(NULL, 10, val);
1855 exp->write_c_string(s);
1859 // Export an integer in a constant expression.
1862 Integer_expression::do_export(Export* exp) const
1864 Integer_expression::export_integer(exp, this->val_);
1865 if (this->is_character_constant_)
1866 exp->write_c_string("'");
1867 // A trailing space lets us reliably identify the end of the number.
1868 exp->write_c_string(" ");
1871 // Import an integer, floating point, or complex value. This handles
1872 // all these types because they all start with digits.
1875 Integer_expression::do_import(Import* imp)
1877 std::string num = imp->read_identifier();
1878 imp->require_c_string(" ");
1879 if (!num.empty() && num[num.length() - 1] == 'i')
1882 size_t plus_pos = num.find('+', 1);
1883 size_t minus_pos = num.find('-', 1);
1885 if (plus_pos == std::string::npos)
1887 else if (minus_pos == std::string::npos)
1891 error_at(imp->location(), "bad number in import data: %qs",
1893 return Expression::make_error(imp->location());
1895 if (pos == std::string::npos)
1896 mpfr_set_ui(real, 0, GMP_RNDN);
1899 std::string real_str = num.substr(0, pos);
1900 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1902 error_at(imp->location(), "bad number in import data: %qs",
1904 return Expression::make_error(imp->location());
1908 std::string imag_str;
1909 if (pos == std::string::npos)
1912 imag_str = num.substr(pos);
1913 imag_str = imag_str.substr(0, imag_str.size() - 1);
1915 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1917 error_at(imp->location(), "bad number in import data: %qs",
1919 return Expression::make_error(imp->location());
1921 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1927 else if (num.find('.') == std::string::npos
1928 && num.find('E') == std::string::npos)
1930 bool is_character_constant = (!num.empty()
1931 && num[num.length() - 1] == '\'');
1932 if (is_character_constant)
1933 num = num.substr(0, num.length() - 1);
1935 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1937 error_at(imp->location(), "bad number in import data: %qs",
1939 return Expression::make_error(imp->location());
1942 if (is_character_constant)
1943 ret = Expression::make_character(&val, NULL, imp->location());
1945 ret = Expression::make_integer(&val, NULL, imp->location());
1952 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1954 error_at(imp->location(), "bad number in import data: %qs",
1956 return Expression::make_error(imp->location());
1958 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1963 // Ast dump for integer expression.
1966 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1968 if (this->is_character_constant_)
1969 ast_dump_context->ostream() << '\'';
1970 Integer_expression::export_integer(ast_dump_context, this->val_);
1971 if (this->is_character_constant_)
1972 ast_dump_context->ostream() << '\'';
1975 // Build a new integer value.
1978 Expression::make_integer(const mpz_t* val, Type* type, Location location)
1980 return new Integer_expression(val, type, false, location);
1983 // Build a new character constant value.
1986 Expression::make_character(const mpz_t* val, Type* type, Location location)
1988 return new Integer_expression(val, type, true, location);
1993 class Float_expression : public Expression
1996 Float_expression(const mpfr_t* val, Type* type, Location location)
1997 : Expression(EXPRESSION_FLOAT, location),
2000 mpfr_init_set(this->val_, *val, GMP_RNDN);
2003 // Write VAL to export data.
2005 export_float(String_dump* exp, const mpfr_t val);
2007 // Write VAL to dump file.
2009 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
2013 do_is_constant() const
2017 do_numeric_constant_value(Numeric_constant* nc) const
2019 nc->set_float(this->type_, this->val_);
2027 do_determine_type(const Type_context*);
2030 do_check_types(Gogo*);
2034 { return Expression::make_float(&this->val_, this->type_,
2035 this->location()); }
2038 do_get_tree(Translate_context*);
2041 do_export(Export*) const;
2044 do_dump_expression(Ast_dump_context*) const;
2047 // The floating point value.
2053 // Return the current type. If we haven't set the type yet, we return
2054 // an abstract float type.
2057 Float_expression::do_type()
2059 if (this->type_ == NULL)
2060 this->type_ = Type::make_abstract_float_type();
2064 // Set the type of the float value. Here we may switch from an
2065 // abstract type to a real type.
2068 Float_expression::do_determine_type(const Type_context* context)
2070 if (this->type_ != NULL && !this->type_->is_abstract())
2072 else if (context->type != NULL
2073 && (context->type->integer_type() != NULL
2074 || context->type->float_type() != NULL
2075 || context->type->complex_type() != NULL))
2076 this->type_ = context->type;
2077 else if (!context->may_be_abstract)
2078 this->type_ = Type::lookup_float_type("float64");
2081 // Check the type of a float value.
2084 Float_expression::do_check_types(Gogo*)
2086 Type* type = this->type_;
2089 Numeric_constant nc;
2090 nc.set_float(NULL, this->val_);
2091 if (!nc.set_type(this->type_, true, this->location()))
2092 this->set_is_error();
2095 // Get a tree for a float constant.
2098 Float_expression::do_get_tree(Translate_context* context)
2100 Gogo* gogo = context->gogo();
2102 if (this->type_ != NULL && !this->type_->is_abstract())
2103 type = type_to_tree(this->type_->get_backend(gogo));
2104 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2106 // We have an abstract integer type. We just hope for the best.
2107 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2111 // If we still have an abstract type here, then this is being
2112 // used in a constant expression which didn't get reduced. We
2113 // just use float64 and hope for the best.
2114 Type* ft = Type::lookup_float_type("float64");
2115 type = type_to_tree(ft->get_backend(gogo));
2117 return Expression::float_constant_tree(this->val_, type);
2120 // Write a floating point number to a string dump.
2123 Float_expression::export_float(String_dump *exp, const mpfr_t val)
2126 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2128 exp->write_c_string("-");
2129 exp->write_c_string("0.");
2130 exp->write_c_string(*s == '-' ? s + 1 : s);
2133 snprintf(buf, sizeof buf, "E%ld", exponent);
2134 exp->write_c_string(buf);
2137 // Export a floating point number in a constant expression.
2140 Float_expression::do_export(Export* exp) const
2142 Float_expression::export_float(exp, this->val_);
2143 // A trailing space lets us reliably identify the end of the number.
2144 exp->write_c_string(" ");
2147 // Dump a floating point number to the dump file.
2150 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2152 Float_expression::export_float(ast_dump_context, this->val_);
2155 // Make a float expression.
2158 Expression::make_float(const mpfr_t* val, Type* type, Location location)
2160 return new Float_expression(val, type, location);
2165 class Complex_expression : public Expression
2168 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2170 : Expression(EXPRESSION_COMPLEX, location),
2173 mpfr_init_set(this->real_, *real, GMP_RNDN);
2174 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2177 // Write REAL/IMAG to string dump.
2179 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
2181 // Write REAL/IMAG to dump context.
2183 dump_complex(Ast_dump_context* ast_dump_context,
2184 const mpfr_t real, const mpfr_t val);
2188 do_is_constant() const
2192 do_numeric_constant_value(Numeric_constant* nc) const
2194 nc->set_complex(this->type_, this->real_, this->imag_);
2202 do_determine_type(const Type_context*);
2205 do_check_types(Gogo*);
2210 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2215 do_get_tree(Translate_context*);
2218 do_export(Export*) const;
2221 do_dump_expression(Ast_dump_context*) const;
2226 // The imaginary part;
2228 // The type if known.
2232 // Return the current type. If we haven't set the type yet, we return
2233 // an abstract complex type.
2236 Complex_expression::do_type()
2238 if (this->type_ == NULL)
2239 this->type_ = Type::make_abstract_complex_type();
2243 // Set the type of the complex value. Here we may switch from an
2244 // abstract type to a real type.
2247 Complex_expression::do_determine_type(const Type_context* context)
2249 if (this->type_ != NULL && !this->type_->is_abstract())
2251 else if (context->type != NULL
2252 && context->type->complex_type() != NULL)
2253 this->type_ = context->type;
2254 else if (!context->may_be_abstract)
2255 this->type_ = Type::lookup_complex_type("complex128");
2258 // Check the type of a complex value.
2261 Complex_expression::do_check_types(Gogo*)
2263 Type* type = this->type_;
2266 Numeric_constant nc;
2267 nc.set_complex(NULL, this->real_, this->imag_);
2268 if (!nc.set_type(this->type_, true, this->location()))
2269 this->set_is_error();
2272 // Get a tree for a complex constant.
2275 Complex_expression::do_get_tree(Translate_context* context)
2277 Gogo* gogo = context->gogo();
2279 if (this->type_ != NULL && !this->type_->is_abstract())
2280 type = type_to_tree(this->type_->get_backend(gogo));
2283 // If we still have an abstract type here, this this is being
2284 // used in a constant expression which didn't get reduced. We
2285 // just use complex128 and hope for the best.
2286 Type* ct = Type::lookup_complex_type("complex128");
2287 type = type_to_tree(ct->get_backend(gogo));
2289 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2292 // Write REAL/IMAG to export data.
2295 Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
2298 if (!mpfr_zero_p(real))
2300 Float_expression::export_float(exp, real);
2301 if (mpfr_sgn(imag) > 0)
2302 exp->write_c_string("+");
2304 Float_expression::export_float(exp, imag);
2305 exp->write_c_string("i");
2308 // Export a complex number in a constant expression.
2311 Complex_expression::do_export(Export* exp) const
2313 Complex_expression::export_complex(exp, this->real_, this->imag_);
2314 // A trailing space lets us reliably identify the end of the number.
2315 exp->write_c_string(" ");
2318 // Dump a complex expression to the dump file.
2321 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2323 Complex_expression::export_complex(ast_dump_context,
2328 // Make a complex expression.
2331 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2334 return new Complex_expression(real, imag, type, location);
2337 // Find a named object in an expression.
2339 class Find_named_object : public Traverse
2342 Find_named_object(Named_object* no)
2343 : Traverse(traverse_expressions),
2344 no_(no), found_(false)
2347 // Whether we found the object.
2350 { return this->found_; }
2354 expression(Expression**);
2357 // The object we are looking for.
2359 // Whether we found it.
2363 // A reference to a const in an expression.
2365 class Const_expression : public Expression
2368 Const_expression(Named_object* constant, Location location)
2369 : Expression(EXPRESSION_CONST_REFERENCE, location),
2370 constant_(constant), type_(NULL), seen_(false)
2375 { return this->constant_; }
2377 // Check that the initializer does not refer to the constant itself.
2379 check_for_init_loop();
2383 do_traverse(Traverse*);
2386 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2389 do_is_constant() const
2393 do_numeric_constant_value(Numeric_constant* nc) const;
2396 do_string_constant_value(std::string* val) const;
2401 // The type of a const is set by the declaration, not the use.
2403 do_determine_type(const Type_context*);
2406 do_check_types(Gogo*);
2413 do_get_tree(Translate_context* context);
2415 // When exporting a reference to a const as part of a const
2416 // expression, we export the value. We ignore the fact that it has
2419 do_export(Export* exp) const
2420 { this->constant_->const_value()->expr()->export_expression(exp); }
2423 do_dump_expression(Ast_dump_context*) const;
2427 Named_object* constant_;
2428 // The type of this reference. This is used if the constant has an
2431 // Used to prevent infinite recursion when a constant incorrectly
2432 // refers to itself.
2439 Const_expression::do_traverse(Traverse* traverse)
2441 if (this->type_ != NULL)
2442 return Type::traverse(this->type_, traverse);
2443 return TRAVERSE_CONTINUE;
2446 // Lower a constant expression. This is where we convert the
2447 // predeclared constant iota into an integer value.
2450 Const_expression::do_lower(Gogo* gogo, Named_object*,
2451 Statement_inserter*, int iota_value)
2453 if (this->constant_->const_value()->expr()->classification()
2456 if (iota_value == -1)
2458 error_at(this->location(),
2459 "iota is only defined in const declarations");
2463 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2464 Expression* ret = Expression::make_integer(&val, NULL,
2470 // Make sure that the constant itself has been lowered.
2471 gogo->lower_constant(this->constant_);
2476 // Return a numeric constant value.
2479 Const_expression::do_numeric_constant_value(Numeric_constant* nc) const
2484 Expression* e = this->constant_->const_value()->expr();
2488 bool r = e->numeric_constant_value(nc);
2490 this->seen_ = false;
2493 if (this->type_ != NULL)
2494 ctype = this->type_;
2496 ctype = this->constant_->const_value()->type();
2497 if (r && ctype != NULL)
2499 if (!nc->set_type(ctype, false, this->location()))
2507 Const_expression::do_string_constant_value(std::string* val) const
2512 Expression* e = this->constant_->const_value()->expr();
2515 bool ok = e->string_constant_value(val);
2516 this->seen_ = false;
2521 // Return the type of the const reference.
2524 Const_expression::do_type()
2526 if (this->type_ != NULL)
2529 Named_constant* nc = this->constant_->const_value();
2531 if (this->seen_ || nc->lowering())
2533 this->report_error(_("constant refers to itself"));
2534 this->type_ = Type::make_error_type();
2540 Type* ret = nc->type();
2544 this->seen_ = false;
2548 // During parsing, a named constant may have a NULL type, but we
2549 // must not return a NULL type here.
2550 ret = nc->expr()->type();
2552 this->seen_ = false;
2557 // Set the type of the const reference.
2560 Const_expression::do_determine_type(const Type_context* context)
2562 Type* ctype = this->constant_->const_value()->type();
2563 Type* cetype = (ctype != NULL
2565 : this->constant_->const_value()->expr()->type());
2566 if (ctype != NULL && !ctype->is_abstract())
2568 else if (context->type != NULL
2569 && context->type->is_numeric_type()
2570 && cetype->is_numeric_type())
2571 this->type_ = context->type;
2572 else if (context->type != NULL
2573 && context->type->is_string_type()
2574 && cetype->is_string_type())
2575 this->type_ = context->type;
2576 else if (context->type != NULL
2577 && context->type->is_boolean_type()
2578 && cetype->is_boolean_type())
2579 this->type_ = context->type;
2580 else if (!context->may_be_abstract)
2582 if (cetype->is_abstract())
2583 cetype = cetype->make_non_abstract_type();
2584 this->type_ = cetype;
2588 // Check for a loop in which the initializer of a constant refers to
2589 // the constant itself.
2592 Const_expression::check_for_init_loop()
2594 if (this->type_ != NULL && this->type_->is_error())
2599 this->report_error(_("constant refers to itself"));
2600 this->type_ = Type::make_error_type();
2604 Expression* init = this->constant_->const_value()->expr();
2605 Find_named_object find_named_object(this->constant_);
2608 Expression::traverse(&init, &find_named_object);
2609 this->seen_ = false;
2611 if (find_named_object.found())
2613 if (this->type_ == NULL || !this->type_->is_error())
2615 this->report_error(_("constant refers to itself"));
2616 this->type_ = Type::make_error_type();
2622 // Check types of a const reference.
2625 Const_expression::do_check_types(Gogo*)
2627 if (this->type_ != NULL && this->type_->is_error())
2630 this->check_for_init_loop();
2632 // Check that numeric constant fits in type.
2633 if (this->type_ != NULL && this->type_->is_numeric_type())
2635 Numeric_constant nc;
2636 if (this->constant_->const_value()->expr()->numeric_constant_value(&nc))
2638 if (!nc.set_type(this->type_, true, this->location()))
2639 this->set_is_error();
2644 // Return a tree for the const reference.
2647 Const_expression::do_get_tree(Translate_context* context)
2649 Gogo* gogo = context->gogo();
2651 if (this->type_ == NULL)
2652 type_tree = NULL_TREE;
2655 type_tree = type_to_tree(this->type_->get_backend(gogo));
2656 if (type_tree == error_mark_node)
2657 return error_mark_node;
2660 // If the type has been set for this expression, but the underlying
2661 // object is an abstract int or float, we try to get the abstract
2662 // value. Otherwise we may lose something in the conversion.
2663 if (this->type_ != NULL
2664 && this->type_->is_numeric_type()
2665 && (this->constant_->const_value()->type() == NULL
2666 || this->constant_->const_value()->type()->is_abstract()))
2668 Expression* expr = this->constant_->const_value()->expr();
2669 Numeric_constant nc;
2670 if (expr->numeric_constant_value(&nc)
2671 && nc.set_type(this->type_, false, this->location()))
2673 Expression* e = nc.expression(this->location());
2674 return e->get_tree(context);
2678 tree const_tree = this->constant_->get_tree(gogo, context->function());
2679 if (this->type_ == NULL
2680 || const_tree == error_mark_node
2681 || TREE_TYPE(const_tree) == error_mark_node)
2685 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2686 ret = fold_convert(type_tree, const_tree);
2687 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2688 ret = fold(convert_to_integer(type_tree, const_tree));
2689 else if (TREE_CODE(type_tree) == REAL_TYPE)
2690 ret = fold(convert_to_real(type_tree, const_tree));
2691 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2692 ret = fold(convert_to_complex(type_tree, const_tree));
2698 // Dump ast representation for constant expression.
2701 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2703 ast_dump_context->ostream() << this->constant_->name();
2706 // Make a reference to a constant in an expression.
2709 Expression::make_const_reference(Named_object* constant,
2712 return new Const_expression(constant, location);
2715 // Find a named object in an expression.
2718 Find_named_object::expression(Expression** pexpr)
2720 switch ((*pexpr)->classification())
2722 case Expression::EXPRESSION_CONST_REFERENCE:
2724 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2725 if (ce->named_object() == this->no_)
2728 // We need to check a constant initializer explicitly, as
2729 // loops here will not be caught by the loop checking for
2730 // variable initializers.
2731 ce->check_for_init_loop();
2733 return TRAVERSE_CONTINUE;
2736 case Expression::EXPRESSION_VAR_REFERENCE:
2737 if ((*pexpr)->var_expression()->named_object() == this->no_)
2739 return TRAVERSE_CONTINUE;
2740 case Expression::EXPRESSION_FUNC_REFERENCE:
2741 if ((*pexpr)->func_expression()->named_object() == this->no_)
2743 return TRAVERSE_CONTINUE;
2745 return TRAVERSE_CONTINUE;
2747 this->found_ = true;
2748 return TRAVERSE_EXIT;
2753 class Nil_expression : public Expression
2756 Nil_expression(Location location)
2757 : Expression(EXPRESSION_NIL, location)
2765 do_is_constant() const
2770 { return Type::make_nil_type(); }
2773 do_determine_type(const Type_context*)
2781 do_get_tree(Translate_context*)
2782 { return null_pointer_node; }
2785 do_export(Export* exp) const
2786 { exp->write_c_string("nil"); }
2789 do_dump_expression(Ast_dump_context* ast_dump_context) const
2790 { ast_dump_context->ostream() << "nil"; }
2793 // Import a nil expression.
2796 Nil_expression::do_import(Import* imp)
2798 imp->require_c_string("nil");
2799 return Expression::make_nil(imp->location());
2802 // Make a nil expression.
2805 Expression::make_nil(Location location)
2807 return new Nil_expression(location);
2810 // The value of the predeclared constant iota. This is little more
2811 // than a marker. This will be lowered to an integer in
2812 // Const_expression::do_lower, which is where we know the value that
2815 class Iota_expression : public Parser_expression
2818 Iota_expression(Location location)
2819 : Parser_expression(EXPRESSION_IOTA, location)
2824 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2825 { go_unreachable(); }
2827 // There should only ever be one of these.
2830 { go_unreachable(); }
2833 do_dump_expression(Ast_dump_context* ast_dump_context) const
2834 { ast_dump_context->ostream() << "iota"; }
2837 // Make an iota expression. This is only called for one case: the
2838 // value of the predeclared constant iota.
2841 Expression::make_iota()
2843 static Iota_expression iota_expression(Linemap::unknown_location());
2844 return &iota_expression;
2847 // A type conversion expression.
2849 class Type_conversion_expression : public Expression
2852 Type_conversion_expression(Type* type, Expression* expr,
2854 : Expression(EXPRESSION_CONVERSION, location),
2855 type_(type), expr_(expr), may_convert_function_types_(false)
2858 // Return the type to which we are converting.
2861 { return this->type_; }
2863 // Return the expression which we are converting.
2866 { return this->expr_; }
2868 // Permit converting from one function type to another. This is
2869 // used internally for method expressions.
2871 set_may_convert_function_types()
2873 this->may_convert_function_types_ = true;
2876 // Import a type conversion expression.
2882 do_traverse(Traverse* traverse);
2885 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2888 do_is_constant() const
2889 { return this->expr_->is_constant(); }
2892 do_numeric_constant_value(Numeric_constant*) const;
2895 do_string_constant_value(std::string*) const;
2899 { return this->type_; }
2902 do_determine_type(const Type_context*)
2904 Type_context subcontext(this->type_, false);
2905 this->expr_->determine_type(&subcontext);
2909 do_check_types(Gogo*);
2914 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2919 do_get_tree(Translate_context* context);
2922 do_export(Export*) const;
2925 do_dump_expression(Ast_dump_context*) const;
2928 // The type to convert to.
2930 // The expression to convert.
2932 // True if this is permitted to convert function types. This is
2933 // used internally for method expressions.
2934 bool may_convert_function_types_;
2940 Type_conversion_expression::do_traverse(Traverse* traverse)
2942 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
2943 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
2944 return TRAVERSE_EXIT;
2945 return TRAVERSE_CONTINUE;
2948 // Convert to a constant at lowering time.
2951 Type_conversion_expression::do_lower(Gogo*, Named_object*,
2952 Statement_inserter*, int)
2954 Type* type = this->type_;
2955 Expression* val = this->expr_;
2956 Location location = this->location();
2958 if (type->is_numeric_type())
2960 Numeric_constant nc;
2961 if (val->numeric_constant_value(&nc))
2963 if (!nc.set_type(type, true, location))
2964 return Expression::make_error(location);
2965 return nc.expression(location);
2969 if (type->is_slice_type())
2971 Type* element_type = type->array_type()->element_type()->forwarded();
2972 bool is_byte = (element_type->integer_type() != NULL
2973 && element_type->integer_type()->is_byte());
2974 bool is_rune = (element_type->integer_type() != NULL
2975 && element_type->integer_type()->is_rune());
2976 if (is_byte || is_rune)
2979 if (val->string_constant_value(&s))
2981 Expression_list* vals = new Expression_list();
2984 for (std::string::const_iterator p = s.begin();
2989 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
2990 Expression* v = Expression::make_integer(&val,
2999 const char *p = s.data();
3000 const char *pend = s.data() + s.length();
3004 int adv = Lex::fetch_char(p, &c);
3007 warning_at(this->location(), 0,
3008 "invalid UTF-8 encoding");
3013 mpz_init_set_ui(val, c);
3014 Expression* v = Expression::make_integer(&val,
3022 return Expression::make_slice_composite_literal(type, vals,
3031 // Return the constant numeric value if there is one.
3034 Type_conversion_expression::do_numeric_constant_value(
3035 Numeric_constant* nc) const
3037 if (!this->type_->is_numeric_type())
3039 if (!this->expr_->numeric_constant_value(nc))
3041 return nc->set_type(this->type_, false, this->location());
3044 // Return the constant string value if there is one.
3047 Type_conversion_expression::do_string_constant_value(std::string* val) const
3049 if (this->type_->is_string_type()
3050 && this->expr_->type()->integer_type() != NULL)
3052 Numeric_constant nc;
3053 if (this->expr_->numeric_constant_value(&nc))
3056 if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID)
3059 Lex::append_char(ival, true, val, this->location());
3065 // FIXME: Could handle conversion from const []int here.
3070 // Check that types are convertible.
3073 Type_conversion_expression::do_check_types(Gogo*)
3075 Type* type = this->type_;
3076 Type* expr_type = this->expr_->type();
3079 if (type->is_error() || expr_type->is_error())
3081 this->set_is_error();
3085 if (this->may_convert_function_types_
3086 && type->function_type() != NULL
3087 && expr_type->function_type() != NULL)
3090 if (Type::are_convertible(type, expr_type, &reason))
3093 error_at(this->location(), "%s", reason.c_str());
3094 this->set_is_error();
3097 // Get a tree for a type conversion.
3100 Type_conversion_expression::do_get_tree(Translate_context* context)
3102 Gogo* gogo = context->gogo();
3103 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
3104 tree expr_tree = this->expr_->get_tree(context);
3106 if (type_tree == error_mark_node
3107 || expr_tree == error_mark_node
3108 || TREE_TYPE(expr_tree) == error_mark_node)
3109 return error_mark_node;
3111 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3112 return fold_convert(type_tree, expr_tree);
3114 Type* type = this->type_;
3115 Type* expr_type = this->expr_->type();
3117 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3118 ret = Expression::convert_for_assignment(context, type, expr_type,
3119 expr_tree, this->location());
3120 else if (type->integer_type() != NULL)
3122 if (expr_type->integer_type() != NULL
3123 || expr_type->float_type() != NULL
3124 || expr_type->is_unsafe_pointer_type())
3125 ret = fold(convert_to_integer(type_tree, expr_tree));
3129 else if (type->float_type() != NULL)
3131 if (expr_type->integer_type() != NULL
3132 || expr_type->float_type() != NULL)
3133 ret = fold(convert_to_real(type_tree, expr_tree));
3137 else if (type->complex_type() != NULL)
3139 if (expr_type->complex_type() != NULL)
3140 ret = fold(convert_to_complex(type_tree, expr_tree));
3144 else if (type->is_string_type()
3145 && expr_type->integer_type() != NULL)
3147 expr_tree = fold_convert(integer_type_node, expr_tree);
3148 if (host_integerp(expr_tree, 0))
3150 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3152 Lex::append_char(intval, true, &s, this->location());
3153 Expression* se = Expression::make_string(s, this->location());
3154 return se->get_tree(context);
3157 static tree int_to_string_fndecl;
3158 ret = Gogo::call_builtin(&int_to_string_fndecl,
3160 "__go_int_to_string",
3164 fold_convert(integer_type_node, expr_tree));
3166 else if (type->is_string_type() && expr_type->is_slice_type())
3168 if (!DECL_P(expr_tree))
3169 expr_tree = save_expr(expr_tree);
3170 Array_type* a = expr_type->array_type();
3171 Type* e = a->element_type()->forwarded();
3172 go_assert(e->integer_type() != NULL);
3173 tree valptr = fold_convert(const_ptr_type_node,
3174 a->value_pointer_tree(gogo, expr_tree));
3175 tree len = a->length_tree(gogo, expr_tree);
3176 len = fold_convert_loc(this->location().gcc_location(), integer_type_node,
3178 if (e->integer_type()->is_byte())
3180 static tree byte_array_to_string_fndecl;
3181 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3183 "__go_byte_array_to_string",
3186 const_ptr_type_node,
3193 go_assert(e->integer_type()->is_rune());
3194 static tree int_array_to_string_fndecl;
3195 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3197 "__go_int_array_to_string",
3200 const_ptr_type_node,
3206 else if (type->is_slice_type() && expr_type->is_string_type())
3208 Type* e = type->array_type()->element_type()->forwarded();
3209 go_assert(e->integer_type() != NULL);
3210 if (e->integer_type()->is_byte())
3212 tree string_to_byte_array_fndecl = NULL_TREE;
3213 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3215 "__go_string_to_byte_array",
3218 TREE_TYPE(expr_tree),
3223 go_assert(e->integer_type()->is_rune());
3224 tree string_to_int_array_fndecl = NULL_TREE;
3225 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3227 "__go_string_to_int_array",
3230 TREE_TYPE(expr_tree),
3234 else if ((type->is_unsafe_pointer_type()
3235 && expr_type->points_to() != NULL)
3236 || (expr_type->is_unsafe_pointer_type()
3237 && type->points_to() != NULL))
3238 ret = fold_convert(type_tree, expr_tree);
3239 else if (type->is_unsafe_pointer_type()
3240 && expr_type->integer_type() != NULL)
3241 ret = convert_to_pointer(type_tree, expr_tree);
3242 else if (this->may_convert_function_types_
3243 && type->function_type() != NULL
3244 && expr_type->function_type() != NULL)
3245 ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3248 ret = Expression::convert_for_assignment(context, type, expr_type,
3249 expr_tree, this->location());
3254 // Output a type conversion in a constant expression.
3257 Type_conversion_expression::do_export(Export* exp) const
3259 exp->write_c_string("convert(");
3260 exp->write_type(this->type_);
3261 exp->write_c_string(", ");
3262 this->expr_->export_expression(exp);
3263 exp->write_c_string(")");
3266 // Import a type conversion or a struct construction.
3269 Type_conversion_expression::do_import(Import* imp)
3271 imp->require_c_string("convert(");
3272 Type* type = imp->read_type();
3273 imp->require_c_string(", ");
3274 Expression* val = Expression::import_expression(imp);
3275 imp->require_c_string(")");
3276 return Expression::make_cast(type, val, imp->location());
3279 // Dump ast representation for a type conversion expression.
3282 Type_conversion_expression::do_dump_expression(
3283 Ast_dump_context* ast_dump_context) const
3285 ast_dump_context->dump_type(this->type_);
3286 ast_dump_context->ostream() << "(";
3287 ast_dump_context->dump_expression(this->expr_);
3288 ast_dump_context->ostream() << ") ";
3291 // Make a type cast expression.
3294 Expression::make_cast(Type* type, Expression* val, Location location)
3296 if (type->is_error_type() || val->is_error_expression())
3297 return Expression::make_error(location);
3298 return new Type_conversion_expression(type, val, location);
3301 // An unsafe type conversion, used to pass values to builtin functions.
3303 class Unsafe_type_conversion_expression : public Expression
3306 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3308 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3309 type_(type), expr_(expr)
3314 do_traverse(Traverse* traverse);
3318 { return this->type_; }
3321 do_determine_type(const Type_context*)
3322 { this->expr_->determine_type_no_context(); }
3327 return new Unsafe_type_conversion_expression(this->type_,
3328 this->expr_->copy(),
3333 do_get_tree(Translate_context*);
3336 do_dump_expression(Ast_dump_context*) const;
3339 // The type to convert to.
3341 // The expression to convert.
3348 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3350 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3351 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3352 return TRAVERSE_EXIT;
3353 return TRAVERSE_CONTINUE;
3356 // Convert to backend representation.
3359 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3361 // We are only called for a limited number of cases.
3363 Type* t = this->type_;
3364 Type* et = this->expr_->type();
3366 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
3367 tree expr_tree = this->expr_->get_tree(context);
3368 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3369 return error_mark_node;
3371 Location loc = this->location();
3373 bool use_view_convert = false;
3374 if (t->is_slice_type())
3376 go_assert(et->is_slice_type());
3377 use_view_convert = true;
3379 else if (t->map_type() != NULL)
3380 go_assert(et->map_type() != NULL);
3381 else if (t->channel_type() != NULL)
3382 go_assert(et->channel_type() != NULL);
3383 else if (t->points_to() != NULL)
3384 go_assert(et->points_to() != NULL || et->is_nil_type());
3385 else if (et->is_unsafe_pointer_type())
3386 go_assert(t->points_to() != NULL);
3387 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3389 go_assert(et->interface_type() != NULL
3390 && !et->interface_type()->is_empty());
3391 use_view_convert = true;
3393 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3395 go_assert(et->interface_type() != NULL
3396 && et->interface_type()->is_empty());
3397 use_view_convert = true;
3399 else if (t->integer_type() != NULL)
3401 go_assert(et->is_boolean_type()
3402 || et->integer_type() != NULL
3403 || et->function_type() != NULL
3404 || et->points_to() != NULL
3405 || et->map_type() != NULL
3406 || et->channel_type() != NULL);
3407 return convert_to_integer(type_tree, expr_tree);
3412 if (use_view_convert)
3413 return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3416 return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
3419 // Dump ast representation for an unsafe type conversion expression.
3422 Unsafe_type_conversion_expression::do_dump_expression(
3423 Ast_dump_context* ast_dump_context) const
3425 ast_dump_context->dump_type(this->type_);
3426 ast_dump_context->ostream() << "(";
3427 ast_dump_context->dump_expression(this->expr_);
3428 ast_dump_context->ostream() << ") ";
3431 // Make an unsafe type conversion expression.
3434 Expression::make_unsafe_cast(Type* type, Expression* expr,
3437 return new Unsafe_type_conversion_expression(type, expr, location);
3440 // Unary expressions.
3442 class Unary_expression : public Expression
3445 Unary_expression(Operator op, Expression* expr, Location location)
3446 : Expression(EXPRESSION_UNARY, location),
3447 op_(op), escapes_(true), create_temp_(false), expr_(expr)
3450 // Return the operator.
3453 { return this->op_; }
3455 // Return the operand.
3458 { return this->expr_; }
3460 // Record that an address expression does not escape.
3462 set_does_not_escape()
3464 go_assert(this->op_ == OPERATOR_AND);
3465 this->escapes_ = false;
3468 // Record that this is an address expression which should create a
3469 // temporary variable if necessary. This is used for method calls.
3473 go_assert(this->op_ == OPERATOR_AND);
3474 this->create_temp_ = true;
3477 // Apply unary opcode OP to UNC, setting NC. Return true if this
3478 // could be done, false if not. Issue errors for overflow.
3480 eval_constant(Operator op, const Numeric_constant* unc,
3481 Location, Numeric_constant* nc);
3488 do_traverse(Traverse* traverse)
3489 { return Expression::traverse(&this->expr_, traverse); }
3492 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3495 do_is_constant() const;
3498 do_numeric_constant_value(Numeric_constant*) const;
3504 do_determine_type(const Type_context*);
3507 do_check_types(Gogo*);
3512 return Expression::make_unary(this->op_, this->expr_->copy(),
3517 do_must_eval_subexpressions_in_order(int*) const
3518 { return this->op_ == OPERATOR_MULT; }
3521 do_is_addressable() const
3522 { return this->op_ == OPERATOR_MULT; }
3525 do_get_tree(Translate_context*);
3528 do_export(Export*) const;