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 // For a normal non-nested function call, that is all we have to do.
1316 if (!this->function_->is_function()
1317 || this->function_->func_value()->enclosing() == NULL)
1319 go_assert(this->closure_ == NULL);
1323 // For a nested function call, we have to always allocate a
1324 // trampoline. If we don't always allocate, then closures will not
1325 // be reliably distinct.
1326 Expression* closure = this->closure_;
1328 if (closure == NULL)
1329 closure_tree = null_pointer_node;
1332 // Get the value of the closure. This will be a pointer to
1333 // space allocated on the heap.
1334 closure_tree = closure->get_tree(context);
1335 if (closure_tree == error_mark_node)
1336 return error_mark_node;
1337 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1340 // Now we need to build some code on the heap. This code will load
1341 // the static chain pointer with the closure and then jump to the
1342 // body of the function. The normal gcc approach is to build the
1343 // code on the stack. Unfortunately we can not do that, as Go
1344 // permits us to return the function pointer.
1346 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1349 // Ast dump for function.
1352 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1354 ast_dump_context->ostream() << this->function_->name();
1355 if (this->closure_ != NULL)
1357 ast_dump_context->ostream() << " {closure = ";
1358 this->closure_->dump_expression(ast_dump_context);
1359 ast_dump_context->ostream() << "}";
1363 // Make a reference to a function in an expression.
1366 Expression::make_func_reference(Named_object* function, Expression* closure,
1369 return new Func_expression(function, closure, location);
1372 // Class Unknown_expression.
1374 // Return the name of an unknown expression.
1377 Unknown_expression::name() const
1379 return this->named_object_->name();
1382 // Lower a reference to an unknown name.
1385 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1387 Location location = this->location();
1388 Named_object* no = this->named_object_;
1390 if (!no->is_unknown())
1394 real = no->unknown_value()->real_named_object();
1397 if (this->is_composite_literal_key_)
1399 if (!this->no_error_message_)
1400 error_at(location, "reference to undefined name %qs",
1401 this->named_object_->message_name().c_str());
1402 return Expression::make_error(location);
1405 switch (real->classification())
1407 case Named_object::NAMED_OBJECT_CONST:
1408 return Expression::make_const_reference(real, location);
1409 case Named_object::NAMED_OBJECT_TYPE:
1410 return Expression::make_type(real->type_value(), location);
1411 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1412 if (this->is_composite_literal_key_)
1414 if (!this->no_error_message_)
1415 error_at(location, "reference to undefined type %qs",
1416 real->message_name().c_str());
1417 return Expression::make_error(location);
1418 case Named_object::NAMED_OBJECT_VAR:
1419 real->var_value()->set_is_used();
1420 return Expression::make_var_reference(real, location);
1421 case Named_object::NAMED_OBJECT_FUNC:
1422 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1423 return Expression::make_func_reference(real, NULL, location);
1424 case Named_object::NAMED_OBJECT_PACKAGE:
1425 if (this->is_composite_literal_key_)
1427 if (!this->no_error_message_)
1428 error_at(location, "unexpected reference to package");
1429 return Expression::make_error(location);
1435 // Dump the ast representation for an unknown expression to a dump context.
1438 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1440 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1444 // Make a reference to an unknown name.
1447 Expression::make_unknown_reference(Named_object* no, Location location)
1449 return new Unknown_expression(no, location);
1452 // A boolean expression.
1454 class Boolean_expression : public Expression
1457 Boolean_expression(bool val, Location location)
1458 : Expression(EXPRESSION_BOOLEAN, location),
1459 val_(val), type_(NULL)
1467 do_is_constant() const
1474 do_determine_type(const Type_context*);
1481 do_get_tree(Translate_context*)
1482 { return this->val_ ? boolean_true_node : boolean_false_node; }
1485 do_export(Export* exp) const
1486 { exp->write_c_string(this->val_ ? "true" : "false"); }
1489 do_dump_expression(Ast_dump_context* ast_dump_context) const
1490 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1495 // The type as determined by context.
1502 Boolean_expression::do_type()
1504 if (this->type_ == NULL)
1505 this->type_ = Type::make_boolean_type();
1509 // Set the type from the context.
1512 Boolean_expression::do_determine_type(const Type_context* context)
1514 if (this->type_ != NULL && !this->type_->is_abstract())
1516 else if (context->type != NULL && context->type->is_boolean_type())
1517 this->type_ = context->type;
1518 else if (!context->may_be_abstract)
1519 this->type_ = Type::lookup_bool_type();
1522 // Import a boolean constant.
1525 Boolean_expression::do_import(Import* imp)
1527 if (imp->peek_char() == 't')
1529 imp->require_c_string("true");
1530 return Expression::make_boolean(true, imp->location());
1534 imp->require_c_string("false");
1535 return Expression::make_boolean(false, imp->location());
1539 // Make a boolean expression.
1542 Expression::make_boolean(bool val, Location location)
1544 return new Boolean_expression(val, location);
1547 // Class String_expression.
1552 String_expression::do_type()
1554 if (this->type_ == NULL)
1555 this->type_ = Type::make_string_type();
1559 // Set the type from the context.
1562 String_expression::do_determine_type(const Type_context* context)
1564 if (this->type_ != NULL && !this->type_->is_abstract())
1566 else if (context->type != NULL && context->type->is_string_type())
1567 this->type_ = context->type;
1568 else if (!context->may_be_abstract)
1569 this->type_ = Type::lookup_string_type();
1572 // Build a string constant.
1575 String_expression::do_get_tree(Translate_context* context)
1577 return context->gogo()->go_string_constant_tree(this->val_);
1580 // Write string literal to string dump.
1583 String_expression::export_string(String_dump* exp,
1584 const String_expression* str)
1587 s.reserve(str->val_.length() * 4 + 2);
1589 for (std::string::const_iterator p = str->val_.begin();
1590 p != str->val_.end();
1593 if (*p == '\\' || *p == '"')
1598 else if (*p >= 0x20 && *p < 0x7f)
1600 else if (*p == '\n')
1602 else if (*p == '\t')
1607 unsigned char c = *p;
1608 unsigned int dig = c >> 4;
1609 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1611 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1615 exp->write_string(s);
1618 // Export a string expression.
1621 String_expression::do_export(Export* exp) const
1623 String_expression::export_string(exp, this);
1626 // Import a string expression.
1629 String_expression::do_import(Import* imp)
1631 imp->require_c_string("\"");
1635 int c = imp->get_char();
1636 if (c == '"' || c == -1)
1639 val += static_cast<char>(c);
1642 c = imp->get_char();
1643 if (c == '\\' || c == '"')
1644 val += static_cast<char>(c);
1651 c = imp->get_char();
1652 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1653 c = imp->get_char();
1654 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1655 char v = (vh << 4) | vl;
1660 error_at(imp->location(), "bad string constant");
1661 return Expression::make_error(imp->location());
1665 return Expression::make_string(val, imp->location());
1668 // Ast dump for string expression.
1671 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1673 String_expression::export_string(ast_dump_context, this);
1676 // Make a string expression.
1679 Expression::make_string(const std::string& val, Location location)
1681 return new String_expression(val, location);
1684 // Make an integer expression.
1686 class Integer_expression : public Expression
1689 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
1691 : Expression(EXPRESSION_INTEGER, location),
1692 type_(type), is_character_constant_(is_character_constant)
1693 { mpz_init_set(this->val_, *val); }
1698 // Write VAL to string dump.
1700 export_integer(String_dump* exp, const mpz_t val);
1702 // Write VAL to dump context.
1704 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1708 do_is_constant() const
1712 do_numeric_constant_value(Numeric_constant* nc) const;
1718 do_determine_type(const Type_context* context);
1721 do_check_types(Gogo*);
1724 do_get_tree(Translate_context*);
1729 if (this->is_character_constant_)
1730 return Expression::make_character(&this->val_, this->type_,
1733 return Expression::make_integer(&this->val_, this->type_,
1738 do_export(Export*) const;
1741 do_dump_expression(Ast_dump_context*) const;
1744 // The integer value.
1748 // Whether this is a character constant.
1749 bool is_character_constant_;
1752 // Return a numeric constant for this expression. We have to mark
1753 // this as a character when appropriate.
1756 Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const
1758 if (this->is_character_constant_)
1759 nc->set_rune(this->type_, this->val_);
1761 nc->set_int(this->type_, this->val_);
1765 // Return the current type. If we haven't set the type yet, we return
1766 // an abstract integer type.
1769 Integer_expression::do_type()
1771 if (this->type_ == NULL)
1773 if (this->is_character_constant_)
1774 this->type_ = Type::make_abstract_character_type();
1776 this->type_ = Type::make_abstract_integer_type();
1781 // Set the type of the integer value. Here we may switch from an
1782 // abstract type to a real type.
1785 Integer_expression::do_determine_type(const Type_context* context)
1787 if (this->type_ != NULL && !this->type_->is_abstract())
1789 else if (context->type != NULL && context->type->is_numeric_type())
1790 this->type_ = context->type;
1791 else if (!context->may_be_abstract)
1793 if (this->is_character_constant_)
1794 this->type_ = Type::lookup_integer_type("int32");
1796 this->type_ = Type::lookup_integer_type("int");
1800 // Check the type of an integer constant.
1803 Integer_expression::do_check_types(Gogo*)
1805 Type* type = this->type_;
1808 Numeric_constant nc;
1809 if (this->is_character_constant_)
1810 nc.set_rune(NULL, this->val_);
1812 nc.set_int(NULL, this->val_);
1813 if (!nc.set_type(type, true, this->location()))
1814 this->set_is_error();
1817 // Get a tree for an integer constant.
1820 Integer_expression::do_get_tree(Translate_context* context)
1822 Gogo* gogo = context->gogo();
1824 if (this->type_ != NULL && !this->type_->is_abstract())
1825 type = type_to_tree(this->type_->get_backend(gogo));
1826 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1828 // We are converting to an abstract floating point type.
1829 Type* ftype = Type::lookup_float_type("float64");
1830 type = type_to_tree(ftype->get_backend(gogo));
1832 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1834 // We are converting to an abstract complex type.
1835 Type* ctype = Type::lookup_complex_type("complex128");
1836 type = type_to_tree(ctype->get_backend(gogo));
1840 // If we still have an abstract type here, then this is being
1841 // used in a constant expression which didn't get reduced for
1842 // some reason. Use a type which will fit the value. We use <,
1843 // not <=, because we need an extra bit for the sign bit.
1844 int bits = mpz_sizeinbase(this->val_, 2);
1845 if (bits < INT_TYPE_SIZE)
1847 Type* t = Type::lookup_integer_type("int");
1848 type = type_to_tree(t->get_backend(gogo));
1852 Type* t = Type::lookup_integer_type("int64");
1853 type = type_to_tree(t->get_backend(gogo));
1856 type = long_long_integer_type_node;
1858 return Expression::integer_constant_tree(this->val_, type);
1861 // Write VAL to export data.
1864 Integer_expression::export_integer(String_dump* exp, const mpz_t val)
1866 char* s = mpz_get_str(NULL, 10, val);
1867 exp->write_c_string(s);
1871 // Export an integer in a constant expression.
1874 Integer_expression::do_export(Export* exp) const
1876 Integer_expression::export_integer(exp, this->val_);
1877 if (this->is_character_constant_)
1878 exp->write_c_string("'");
1879 // A trailing space lets us reliably identify the end of the number.
1880 exp->write_c_string(" ");
1883 // Import an integer, floating point, or complex value. This handles
1884 // all these types because they all start with digits.
1887 Integer_expression::do_import(Import* imp)
1889 std::string num = imp->read_identifier();
1890 imp->require_c_string(" ");
1891 if (!num.empty() && num[num.length() - 1] == 'i')
1894 size_t plus_pos = num.find('+', 1);
1895 size_t minus_pos = num.find('-', 1);
1897 if (plus_pos == std::string::npos)
1899 else if (minus_pos == std::string::npos)
1903 error_at(imp->location(), "bad number in import data: %qs",
1905 return Expression::make_error(imp->location());
1907 if (pos == std::string::npos)
1908 mpfr_set_ui(real, 0, GMP_RNDN);
1911 std::string real_str = num.substr(0, pos);
1912 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1914 error_at(imp->location(), "bad number in import data: %qs",
1916 return Expression::make_error(imp->location());
1920 std::string imag_str;
1921 if (pos == std::string::npos)
1924 imag_str = num.substr(pos);
1925 imag_str = imag_str.substr(0, imag_str.size() - 1);
1927 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1929 error_at(imp->location(), "bad number in import data: %qs",
1931 return Expression::make_error(imp->location());
1933 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1939 else if (num.find('.') == std::string::npos
1940 && num.find('E') == std::string::npos)
1942 bool is_character_constant = (!num.empty()
1943 && num[num.length() - 1] == '\'');
1944 if (is_character_constant)
1945 num = num.substr(0, num.length() - 1);
1947 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1949 error_at(imp->location(), "bad number in import data: %qs",
1951 return Expression::make_error(imp->location());
1954 if (is_character_constant)
1955 ret = Expression::make_character(&val, NULL, imp->location());
1957 ret = Expression::make_integer(&val, NULL, imp->location());
1964 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1966 error_at(imp->location(), "bad number in import data: %qs",
1968 return Expression::make_error(imp->location());
1970 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1975 // Ast dump for integer expression.
1978 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1980 if (this->is_character_constant_)
1981 ast_dump_context->ostream() << '\'';
1982 Integer_expression::export_integer(ast_dump_context, this->val_);
1983 if (this->is_character_constant_)
1984 ast_dump_context->ostream() << '\'';
1987 // Build a new integer value.
1990 Expression::make_integer(const mpz_t* val, Type* type, Location location)
1992 return new Integer_expression(val, type, false, location);
1995 // Build a new character constant value.
1998 Expression::make_character(const mpz_t* val, Type* type, Location location)
2000 return new Integer_expression(val, type, true, location);
2005 class Float_expression : public Expression
2008 Float_expression(const mpfr_t* val, Type* type, Location location)
2009 : Expression(EXPRESSION_FLOAT, location),
2012 mpfr_init_set(this->val_, *val, GMP_RNDN);
2015 // Write VAL to export data.
2017 export_float(String_dump* exp, const mpfr_t val);
2019 // Write VAL to dump file.
2021 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
2025 do_is_constant() const
2029 do_numeric_constant_value(Numeric_constant* nc) const
2031 nc->set_float(this->type_, this->val_);
2039 do_determine_type(const Type_context*);
2042 do_check_types(Gogo*);
2046 { return Expression::make_float(&this->val_, this->type_,
2047 this->location()); }
2050 do_get_tree(Translate_context*);
2053 do_export(Export*) const;
2056 do_dump_expression(Ast_dump_context*) const;
2059 // The floating point value.
2065 // Return the current type. If we haven't set the type yet, we return
2066 // an abstract float type.
2069 Float_expression::do_type()
2071 if (this->type_ == NULL)
2072 this->type_ = Type::make_abstract_float_type();
2076 // Set the type of the float value. Here we may switch from an
2077 // abstract type to a real type.
2080 Float_expression::do_determine_type(const Type_context* context)
2082 if (this->type_ != NULL && !this->type_->is_abstract())
2084 else if (context->type != NULL
2085 && (context->type->integer_type() != NULL
2086 || context->type->float_type() != NULL
2087 || context->type->complex_type() != NULL))
2088 this->type_ = context->type;
2089 else if (!context->may_be_abstract)
2090 this->type_ = Type::lookup_float_type("float64");
2093 // Check the type of a float value.
2096 Float_expression::do_check_types(Gogo*)
2098 Type* type = this->type_;
2101 Numeric_constant nc;
2102 nc.set_float(NULL, this->val_);
2103 if (!nc.set_type(this->type_, true, this->location()))
2104 this->set_is_error();
2107 // Get a tree for a float constant.
2110 Float_expression::do_get_tree(Translate_context* context)
2112 Gogo* gogo = context->gogo();
2114 if (this->type_ != NULL && !this->type_->is_abstract())
2115 type = type_to_tree(this->type_->get_backend(gogo));
2116 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2118 // We have an abstract integer type. We just hope for the best.
2119 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2123 // If we still have an abstract type here, then this is being
2124 // used in a constant expression which didn't get reduced. We
2125 // just use float64 and hope for the best.
2126 Type* ft = Type::lookup_float_type("float64");
2127 type = type_to_tree(ft->get_backend(gogo));
2129 return Expression::float_constant_tree(this->val_, type);
2132 // Write a floating point number to a string dump.
2135 Float_expression::export_float(String_dump *exp, const mpfr_t val)
2138 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2140 exp->write_c_string("-");
2141 exp->write_c_string("0.");
2142 exp->write_c_string(*s == '-' ? s + 1 : s);
2145 snprintf(buf, sizeof buf, "E%ld", exponent);
2146 exp->write_c_string(buf);
2149 // Export a floating point number in a constant expression.
2152 Float_expression::do_export(Export* exp) const
2154 Float_expression::export_float(exp, this->val_);
2155 // A trailing space lets us reliably identify the end of the number.
2156 exp->write_c_string(" ");
2159 // Dump a floating point number to the dump file.
2162 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2164 Float_expression::export_float(ast_dump_context, this->val_);
2167 // Make a float expression.
2170 Expression::make_float(const mpfr_t* val, Type* type, Location location)
2172 return new Float_expression(val, type, location);
2177 class Complex_expression : public Expression
2180 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2182 : Expression(EXPRESSION_COMPLEX, location),
2185 mpfr_init_set(this->real_, *real, GMP_RNDN);
2186 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2189 // Write REAL/IMAG to string dump.
2191 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
2193 // Write REAL/IMAG to dump context.
2195 dump_complex(Ast_dump_context* ast_dump_context,
2196 const mpfr_t real, const mpfr_t val);
2200 do_is_constant() const
2204 do_numeric_constant_value(Numeric_constant* nc) const
2206 nc->set_complex(this->type_, this->real_, this->imag_);
2214 do_determine_type(const Type_context*);
2217 do_check_types(Gogo*);
2222 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2227 do_get_tree(Translate_context*);
2230 do_export(Export*) const;
2233 do_dump_expression(Ast_dump_context*) const;
2238 // The imaginary part;
2240 // The type if known.
2244 // Return the current type. If we haven't set the type yet, we return
2245 // an abstract complex type.
2248 Complex_expression::do_type()
2250 if (this->type_ == NULL)
2251 this->type_ = Type::make_abstract_complex_type();
2255 // Set the type of the complex value. Here we may switch from an
2256 // abstract type to a real type.
2259 Complex_expression::do_determine_type(const Type_context* context)
2261 if (this->type_ != NULL && !this->type_->is_abstract())
2263 else if (context->type != NULL
2264 && context->type->complex_type() != NULL)
2265 this->type_ = context->type;
2266 else if (!context->may_be_abstract)
2267 this->type_ = Type::lookup_complex_type("complex128");
2270 // Check the type of a complex value.
2273 Complex_expression::do_check_types(Gogo*)
2275 Type* type = this->type_;
2278 Numeric_constant nc;
2279 nc.set_complex(NULL, this->real_, this->imag_);
2280 if (!nc.set_type(this->type_, true, this->location()))
2281 this->set_is_error();
2284 // Get a tree for a complex constant.
2287 Complex_expression::do_get_tree(Translate_context* context)
2289 Gogo* gogo = context->gogo();
2291 if (this->type_ != NULL && !this->type_->is_abstract())
2292 type = type_to_tree(this->type_->get_backend(gogo));
2295 // If we still have an abstract type here, this this is being
2296 // used in a constant expression which didn't get reduced. We
2297 // just use complex128 and hope for the best.
2298 Type* ct = Type::lookup_complex_type("complex128");
2299 type = type_to_tree(ct->get_backend(gogo));
2301 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2304 // Write REAL/IMAG to export data.
2307 Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
2310 if (!mpfr_zero_p(real))
2312 Float_expression::export_float(exp, real);
2313 if (mpfr_sgn(imag) > 0)
2314 exp->write_c_string("+");
2316 Float_expression::export_float(exp, imag);
2317 exp->write_c_string("i");
2320 // Export a complex number in a constant expression.
2323 Complex_expression::do_export(Export* exp) const
2325 Complex_expression::export_complex(exp, this->real_, this->imag_);
2326 // A trailing space lets us reliably identify the end of the number.
2327 exp->write_c_string(" ");
2330 // Dump a complex expression to the dump file.
2333 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2335 Complex_expression::export_complex(ast_dump_context,
2340 // Make a complex expression.
2343 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2346 return new Complex_expression(real, imag, type, location);
2349 // Find a named object in an expression.
2351 class Find_named_object : public Traverse
2354 Find_named_object(Named_object* no)
2355 : Traverse(traverse_expressions),
2356 no_(no), found_(false)
2359 // Whether we found the object.
2362 { return this->found_; }
2366 expression(Expression**);
2369 // The object we are looking for.
2371 // Whether we found it.
2375 // A reference to a const in an expression.
2377 class Const_expression : public Expression
2380 Const_expression(Named_object* constant, Location location)
2381 : Expression(EXPRESSION_CONST_REFERENCE, location),
2382 constant_(constant), type_(NULL), seen_(false)
2387 { return this->constant_; }
2389 // Check that the initializer does not refer to the constant itself.
2391 check_for_init_loop();
2395 do_traverse(Traverse*);
2398 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2401 do_is_constant() const
2405 do_numeric_constant_value(Numeric_constant* nc) const;
2408 do_string_constant_value(std::string* val) const;
2413 // The type of a const is set by the declaration, not the use.
2415 do_determine_type(const Type_context*);
2418 do_check_types(Gogo*);
2425 do_get_tree(Translate_context* context);
2427 // When exporting a reference to a const as part of a const
2428 // expression, we export the value. We ignore the fact that it has
2431 do_export(Export* exp) const
2432 { this->constant_->const_value()->expr()->export_expression(exp); }
2435 do_dump_expression(Ast_dump_context*) const;
2439 Named_object* constant_;
2440 // The type of this reference. This is used if the constant has an
2443 // Used to prevent infinite recursion when a constant incorrectly
2444 // refers to itself.
2451 Const_expression::do_traverse(Traverse* traverse)
2453 if (this->type_ != NULL)
2454 return Type::traverse(this->type_, traverse);
2455 return TRAVERSE_CONTINUE;
2458 // Lower a constant expression. This is where we convert the
2459 // predeclared constant iota into an integer value.
2462 Const_expression::do_lower(Gogo* gogo, Named_object*,
2463 Statement_inserter*, int iota_value)
2465 if (this->constant_->const_value()->expr()->classification()
2468 if (iota_value == -1)
2470 error_at(this->location(),
2471 "iota is only defined in const declarations");
2475 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2476 Expression* ret = Expression::make_integer(&val, NULL,
2482 // Make sure that the constant itself has been lowered.
2483 gogo->lower_constant(this->constant_);
2488 // Return a numeric constant value.
2491 Const_expression::do_numeric_constant_value(Numeric_constant* nc) const
2496 Expression* e = this->constant_->const_value()->expr();
2500 bool r = e->numeric_constant_value(nc);
2502 this->seen_ = false;
2505 if (this->type_ != NULL)
2506 ctype = this->type_;
2508 ctype = this->constant_->const_value()->type();
2509 if (r && ctype != NULL)
2511 if (!nc->set_type(ctype, false, this->location()))
2519 Const_expression::do_string_constant_value(std::string* val) const
2524 Expression* e = this->constant_->const_value()->expr();
2527 bool ok = e->string_constant_value(val);
2528 this->seen_ = false;
2533 // Return the type of the const reference.
2536 Const_expression::do_type()
2538 if (this->type_ != NULL)
2541 Named_constant* nc = this->constant_->const_value();
2543 if (this->seen_ || nc->lowering())
2545 this->report_error(_("constant refers to itself"));
2546 this->type_ = Type::make_error_type();
2552 Type* ret = nc->type();
2556 this->seen_ = false;
2560 // During parsing, a named constant may have a NULL type, but we
2561 // must not return a NULL type here.
2562 ret = nc->expr()->type();
2564 this->seen_ = false;
2569 // Set the type of the const reference.
2572 Const_expression::do_determine_type(const Type_context* context)
2574 Type* ctype = this->constant_->const_value()->type();
2575 Type* cetype = (ctype != NULL
2577 : this->constant_->const_value()->expr()->type());
2578 if (ctype != NULL && !ctype->is_abstract())
2580 else if (context->type != NULL
2581 && context->type->is_numeric_type()
2582 && cetype->is_numeric_type())
2583 this->type_ = context->type;
2584 else if (context->type != NULL
2585 && context->type->is_string_type()
2586 && cetype->is_string_type())
2587 this->type_ = context->type;
2588 else if (context->type != NULL
2589 && context->type->is_boolean_type()
2590 && cetype->is_boolean_type())
2591 this->type_ = context->type;
2592 else if (!context->may_be_abstract)
2594 if (cetype->is_abstract())
2595 cetype = cetype->make_non_abstract_type();
2596 this->type_ = cetype;
2600 // Check for a loop in which the initializer of a constant refers to
2601 // the constant itself.
2604 Const_expression::check_for_init_loop()
2606 if (this->type_ != NULL && this->type_->is_error())
2611 this->report_error(_("constant refers to itself"));
2612 this->type_ = Type::make_error_type();
2616 Expression* init = this->constant_->const_value()->expr();
2617 Find_named_object find_named_object(this->constant_);
2620 Expression::traverse(&init, &find_named_object);
2621 this->seen_ = false;
2623 if (find_named_object.found())
2625 if (this->type_ == NULL || !this->type_->is_error())
2627 this->report_error(_("constant refers to itself"));
2628 this->type_ = Type::make_error_type();
2634 // Check types of a const reference.
2637 Const_expression::do_check_types(Gogo*)
2639 if (this->type_ != NULL && this->type_->is_error())
2642 this->check_for_init_loop();
2644 // Check that numeric constant fits in type.
2645 if (this->type_ != NULL && this->type_->is_numeric_type())
2647 Numeric_constant nc;
2648 if (this->constant_->const_value()->expr()->numeric_constant_value(&nc))
2650 if (!nc.set_type(this->type_, true, this->location()))
2651 this->set_is_error();
2656 // Return a tree for the const reference.
2659 Const_expression::do_get_tree(Translate_context* context)
2661 Gogo* gogo = context->gogo();
2663 if (this->type_ == NULL)
2664 type_tree = NULL_TREE;
2667 type_tree = type_to_tree(this->type_->get_backend(gogo));
2668 if (type_tree == error_mark_node)
2669 return error_mark_node;
2672 // If the type has been set for this expression, but the underlying
2673 // object is an abstract int or float, we try to get the abstract
2674 // value. Otherwise we may lose something in the conversion.
2675 if (this->type_ != NULL
2676 && this->type_->is_numeric_type()
2677 && (this->constant_->const_value()->type() == NULL
2678 || this->constant_->const_value()->type()->is_abstract()))
2680 Expression* expr = this->constant_->const_value()->expr();
2681 Numeric_constant nc;
2682 if (expr->numeric_constant_value(&nc)
2683 && nc.set_type(this->type_, false, this->location()))
2685 Expression* e = nc.expression(this->location());
2686 return e->get_tree(context);
2690 tree const_tree = this->constant_->get_tree(gogo, context->function());
2691 if (this->type_ == NULL
2692 || const_tree == error_mark_node
2693 || TREE_TYPE(const_tree) == error_mark_node)
2697 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2698 ret = fold_convert(type_tree, const_tree);
2699 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2700 ret = fold(convert_to_integer(type_tree, const_tree));
2701 else if (TREE_CODE(type_tree) == REAL_TYPE)
2702 ret = fold(convert_to_real(type_tree, const_tree));
2703 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2704 ret = fold(convert_to_complex(type_tree, const_tree));
2710 // Dump ast representation for constant expression.
2713 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2715 ast_dump_context->ostream() << this->constant_->name();
2718 // Make a reference to a constant in an expression.
2721 Expression::make_const_reference(Named_object* constant,
2724 return new Const_expression(constant, location);
2727 // Find a named object in an expression.
2730 Find_named_object::expression(Expression** pexpr)
2732 switch ((*pexpr)->classification())
2734 case Expression::EXPRESSION_CONST_REFERENCE:
2736 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2737 if (ce->named_object() == this->no_)
2740 // We need to check a constant initializer explicitly, as
2741 // loops here will not be caught by the loop checking for
2742 // variable initializers.
2743 ce->check_for_init_loop();
2745 return TRAVERSE_CONTINUE;
2748 case Expression::EXPRESSION_VAR_REFERENCE:
2749 if ((*pexpr)->var_expression()->named_object() == this->no_)
2751 return TRAVERSE_CONTINUE;
2752 case Expression::EXPRESSION_FUNC_REFERENCE:
2753 if ((*pexpr)->func_expression()->named_object() == this->no_)
2755 return TRAVERSE_CONTINUE;
2757 return TRAVERSE_CONTINUE;
2759 this->found_ = true;
2760 return TRAVERSE_EXIT;
2765 class Nil_expression : public Expression
2768 Nil_expression(Location location)
2769 : Expression(EXPRESSION_NIL, location)
2777 do_is_constant() const
2782 { return Type::make_nil_type(); }
2785 do_determine_type(const Type_context*)
2793 do_get_tree(Translate_context*)
2794 { return null_pointer_node; }
2797 do_export(Export* exp) const
2798 { exp->write_c_string("nil"); }
2801 do_dump_expression(Ast_dump_context* ast_dump_context) const
2802 { ast_dump_context->ostream() << "nil"; }
2805 // Import a nil expression.
2808 Nil_expression::do_import(Import* imp)
2810 imp->require_c_string("nil");
2811 return Expression::make_nil(imp->location());
2814 // Make a nil expression.
2817 Expression::make_nil(Location location)
2819 return new Nil_expression(location);
2822 // The value of the predeclared constant iota. This is little more
2823 // than a marker. This will be lowered to an integer in
2824 // Const_expression::do_lower, which is where we know the value that
2827 class Iota_expression : public Parser_expression
2830 Iota_expression(Location location)
2831 : Parser_expression(EXPRESSION_IOTA, location)
2836 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2837 { go_unreachable(); }
2839 // There should only ever be one of these.
2842 { go_unreachable(); }
2845 do_dump_expression(Ast_dump_context* ast_dump_context) const
2846 { ast_dump_context->ostream() << "iota"; }
2849 // Make an iota expression. This is only called for one case: the
2850 // value of the predeclared constant iota.
2853 Expression::make_iota()
2855 static Iota_expression iota_expression(Linemap::unknown_location());
2856 return &iota_expression;
2859 // A type conversion expression.
2861 class Type_conversion_expression : public Expression
2864 Type_conversion_expression(Type* type, Expression* expr,
2866 : Expression(EXPRESSION_CONVERSION, location),
2867 type_(type), expr_(expr), may_convert_function_types_(false)
2870 // Return the type to which we are converting.
2873 { return this->type_; }
2875 // Return the expression which we are converting.
2878 { return this->expr_; }
2880 // Permit converting from one function type to another. This is
2881 // used internally for method expressions.
2883 set_may_convert_function_types()
2885 this->may_convert_function_types_ = true;
2888 // Import a type conversion expression.
2894 do_traverse(Traverse* traverse);
2897 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2900 do_is_constant() const
2901 { return this->expr_->is_constant(); }
2904 do_numeric_constant_value(Numeric_constant*) const;
2907 do_string_constant_value(std::string*) const;
2911 { return this->type_; }
2914 do_determine_type(const Type_context*)
2916 Type_context subcontext(this->type_, false);
2917 this->expr_->determine_type(&subcontext);
2921 do_check_types(Gogo*);
2926 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2931 do_get_tree(Translate_context* context);
2934 do_export(Export*) const;
2937 do_dump_expression(Ast_dump_context*) const;
2940 // The type to convert to.
2942 // The expression to convert.
2944 // True if this is permitted to convert function types. This is
2945 // used internally for method expressions.
2946 bool may_convert_function_types_;
2952 Type_conversion_expression::do_traverse(Traverse* traverse)
2954 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
2955 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
2956 return TRAVERSE_EXIT;
2957 return TRAVERSE_CONTINUE;
2960 // Convert to a constant at lowering time.
2963 Type_conversion_expression::do_lower(Gogo*, Named_object*,
2964 Statement_inserter*, int)
2966 Type* type = this->type_;
2967 Expression* val = this->expr_;
2968 Location location = this->location();
2970 if (type->is_numeric_type())
2972 Numeric_constant nc;
2973 if (val->numeric_constant_value(&nc))
2975 if (!nc.set_type(type, true, location))
2976 return Expression::make_error(location);
2977 return nc.expression(location);
2981 if (type->is_slice_type())
2983 Type* element_type = type->array_type()->element_type()->forwarded();
2984 bool is_byte = (element_type->integer_type() != NULL
2985 && element_type->integer_type()->is_byte());
2986 bool is_rune = (element_type->integer_type() != NULL
2987 && element_type->integer_type()->is_rune());
2988 if (is_byte || is_rune)
2991 if (val->string_constant_value(&s))
2993 Expression_list* vals = new Expression_list();
2996 for (std::string::const_iterator p = s.begin();
3001 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3002 Expression* v = Expression::make_integer(&val,
3011 const char *p = s.data();
3012 const char *pend = s.data() + s.length();
3016 int adv = Lex::fetch_char(p, &c);
3019 warning_at(this->location(), 0,
3020 "invalid UTF-8 encoding");
3025 mpz_init_set_ui(val, c);
3026 Expression* v = Expression::make_integer(&val,
3034 return Expression::make_slice_composite_literal(type, vals,
3043 // Return the constant numeric value if there is one.
3046 Type_conversion_expression::do_numeric_constant_value(
3047 Numeric_constant* nc) const
3049 if (!this->type_->is_numeric_type())
3051 if (!this->expr_->numeric_constant_value(nc))
3053 return nc->set_type(this->type_, false, this->location());
3056 // Return the constant string value if there is one.
3059 Type_conversion_expression::do_string_constant_value(std::string* val) const
3061 if (this->type_->is_string_type()
3062 && this->expr_->type()->integer_type() != NULL)
3064 Numeric_constant nc;
3065 if (this->expr_->numeric_constant_value(&nc))
3068 if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID)
3071 Lex::append_char(ival, true, val, this->location());
3077 // FIXME: Could handle conversion from const []int here.
3082 // Check that types are convertible.
3085 Type_conversion_expression::do_check_types(Gogo*)
3087 Type* type = this->type_;
3088 Type* expr_type = this->expr_->type();
3091 if (type->is_error() || expr_type->is_error())
3093 this->set_is_error();
3097 if (this->may_convert_function_types_
3098 && type->function_type() != NULL
3099 && expr_type->function_type() != NULL)
3102 if (Type::are_convertible(type, expr_type, &reason))
3105 error_at(this->location(), "%s", reason.c_str());
3106 this->set_is_error();
3109 // Get a tree for a type conversion.
3112 Type_conversion_expression::do_get_tree(Translate_context* context)
3114 Gogo* gogo = context->gogo();
3115 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
3116 tree expr_tree = this->expr_->get_tree(context);
3118 if (type_tree == error_mark_node
3119 || expr_tree == error_mark_node
3120 || TREE_TYPE(expr_tree) == error_mark_node)
3121 return error_mark_node;
3123 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3124 return fold_convert(type_tree, expr_tree);
3126 Type* type = this->type_;
3127 Type* expr_type = this->expr_->type();
3129 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3130 ret = Expression::convert_for_assignment(context, type, expr_type,
3131 expr_tree, this->location());
3132 else if (type->integer_type() != NULL)
3134 if (expr_type->integer_type() != NULL
3135 || expr_type->float_type() != NULL
3136 || expr_type->is_unsafe_pointer_type())
3137 ret = fold(convert_to_integer(type_tree, expr_tree));
3141 else if (type->float_type() != NULL)
3143 if (expr_type->integer_type() != NULL
3144 || expr_type->float_type() != NULL)
3145 ret = fold(convert_to_real(type_tree, expr_tree));
3149 else if (type->complex_type() != NULL)
3151 if (expr_type->complex_type() != NULL)
3152 ret = fold(convert_to_complex(type_tree, expr_tree));
3156 else if (type->is_string_type()
3157 && expr_type->integer_type() != NULL)
3159 expr_tree = fold_convert(integer_type_node, expr_tree);
3160 if (host_integerp(expr_tree, 0))
3162 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3164 Lex::append_char(intval, true, &s, this->location());
3165 Expression* se = Expression::make_string(s, this->location());
3166 return se->get_tree(context);
3169 static tree int_to_string_fndecl;
3170 ret = Gogo::call_builtin(&int_to_string_fndecl,
3172 "__go_int_to_string",
3176 fold_convert(integer_type_node, expr_tree));
3178 else if (type->is_string_type() && expr_type->is_slice_type())
3180 if (!DECL_P(expr_tree))
3181 expr_tree = save_expr(expr_tree);
3182 Array_type* a = expr_type->array_type();
3183 Type* e = a->element_type()->forwarded();
3184 go_assert(e->integer_type() != NULL);
3185 tree valptr = fold_convert(const_ptr_type_node,
3186 a->value_pointer_tree(gogo, expr_tree));
3187 tree len = a->length_tree(gogo, expr_tree);
3188 len = fold_convert_loc(this->location().gcc_location(), integer_type_node,
3190 if (e->integer_type()->is_byte())
3192 static tree byte_array_to_string_fndecl;
3193 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3195 "__go_byte_array_to_string",
3198 const_ptr_type_node,
3205 go_assert(e->integer_type()->is_rune());
3206 static tree int_array_to_string_fndecl;
3207 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3209 "__go_int_array_to_string",
3212 const_ptr_type_node,
3218 else if (type->is_slice_type() && expr_type->is_string_type())
3220 Type* e = type->array_type()->element_type()->forwarded();
3221 go_assert(e->integer_type() != NULL);
3222 if (e->integer_type()->is_byte())
3224 tree string_to_byte_array_fndecl = NULL_TREE;
3225 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3227 "__go_string_to_byte_array",
3230 TREE_TYPE(expr_tree),
3235 go_assert(e->integer_type()->is_rune());
3236 tree string_to_int_array_fndecl = NULL_TREE;
3237 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3239 "__go_string_to_int_array",
3242 TREE_TYPE(expr_tree),
3246 else if ((type->is_unsafe_pointer_type()
3247 && expr_type->points_to() != NULL)
3248 || (expr_type->is_unsafe_pointer_type()
3249 && type->points_to() != NULL))
3250 ret = fold_convert(type_tree, expr_tree);
3251 else if (type->is_unsafe_pointer_type()
3252 && expr_type->integer_type() != NULL)
3253 ret = convert_to_pointer(type_tree, expr_tree);
3254 else if (this->may_convert_function_types_
3255 && type->function_type() != NULL
3256 && expr_type->function_type() != NULL)
3257 ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3260 ret = Expression::convert_for_assignment(context, type, expr_type,
3261 expr_tree, this->location());
3266 // Output a type conversion in a constant expression.
3269 Type_conversion_expression::do_export(Export* exp) const
3271 exp->write_c_string("convert(");
3272 exp->write_type(this->type_);
3273 exp->write_c_string(", ");
3274 this->expr_->export_expression(exp);
3275 exp->write_c_string(")");
3278 // Import a type conversion or a struct construction.
3281 Type_conversion_expression::do_import(Import* imp)
3283 imp->require_c_string("convert(");
3284 Type* type = imp->read_type();
3285 imp->require_c_string(", ");
3286 Expression* val = Expression::import_expression(imp);
3287 imp->require_c_string(")");
3288 return Expression::make_cast(type, val, imp->location());
3291 // Dump ast representation for a type conversion expression.
3294 Type_conversion_expression::do_dump_expression(
3295 Ast_dump_context* ast_dump_context) const
3297 ast_dump_context->dump_type(this->type_);
3298 ast_dump_context->ostream() << "(";
3299 ast_dump_context->dump_expression(this->expr_);
3300 ast_dump_context->ostream() << ") ";
3303 // Make a type cast expression.
3306 Expression::make_cast(Type* type, Expression* val, Location location)
3308 if (type->is_error_type() || val->is_error_expression())
3309 return Expression::make_error(location);
3310 return new Type_conversion_expression(type, val, location);
3313 // An unsafe type conversion, used to pass values to builtin functions.
3315 class Unsafe_type_conversion_expression : public Expression
3318 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3320 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3321 type_(type), expr_(expr)
3326 do_traverse(Traverse* traverse);
3330 { return this->type_; }
3333 do_determine_type(const Type_context*)
3334 { this->expr_->determine_type_no_context(); }
3339 return new Unsafe_type_conversion_expression(this->type_,
3340 this->expr_->copy(),
3345 do_get_tree(Translate_context*);
3348 do_dump_expression(Ast_dump_context*) const;
3351 // The type to convert to.
3353 // The expression to convert.
3360 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3362 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3363 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3364 return TRAVERSE_EXIT;
3365 return TRAVERSE_CONTINUE;
3368 // Convert to backend representation.
3371 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3373 // We are only called for a limited number of cases.
3375 Type* t = this->type_;
3376 Type* et = this->expr_->type();
3378 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
3379 tree expr_tree = this->expr_->get_tree(context);
3380 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3381 return error_mark_node;
3383 Location loc = this->location();
3385 bool use_view_convert = false;
3386 if (t->is_slice_type())
3388 go_assert(et->is_slice_type());
3389 use_view_convert = true;
3391 else if (t->map_type() != NULL)
3392 go_assert(et->map_type() != NULL);
3393 else if (t->channel_type() != NULL)
3394 go_assert(et->channel_type() != NULL);
3395 else if (t->points_to() != NULL)
3396 go_assert(et->points_to() != NULL || et->is_nil_type());
3397 else if (et->is_unsafe_pointer_type())
3398 go_assert(t->points_to() != NULL);
3399 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3401 go_assert(et->interface_type() != NULL
3402 && !et->interface_type()->is_empty());
3403 use_view_convert = true;
3405 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3407 go_assert(et->interface_type() != NULL
3408 && et->interface_type()->is_empty());
3409 use_view_convert = true;
3411 else if (t->integer_type() != NULL)
3413 go_assert(et->is_boolean_type()
3414 || et->integer_type() != NULL
3415 || et->function_type() != NULL
3416 || et->points_to() != NULL
3417 || et->map_type() != NULL
3418 || et->channel_type() != NULL);
3419 return convert_to_integer(type_tree, expr_tree);
3424 if (use_view_convert)
3425 return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3428 return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
3431 // Dump ast representation for an unsafe type conversion expression.
3434 Unsafe_type_conversion_expression::do_dump_expression(
3435 Ast_dump_context* ast_dump_context) const
3437 ast_dump_context->dump_type(this->type_);
3438 ast_dump_context->ostream() << "(";
3439 ast_dump_context->dump_expression(this->expr_);
3440 ast_dump_context->ostream() << ") ";
3443 // Make an unsafe type conversion expression.
3446 Expression::make_unsafe_cast(Type* type, Expression* expr,
3449 return new Unsafe_type_conversion_expression(type, expr, location);
3452 // Unary expressions.
3454 class Unary_expression : public Expression
3457 Unary_expression(Operator op, Expression* expr, Location location)
3458 : Expression(EXPRESSION_UNARY, location),
3459 op_(op), escapes_(true), create_temp_(false), expr_(expr)
3462 // Return the operator.
3465 { return this->op_; }
3467 // Return the operand.
3470 { return this->expr_; }
3472 // Record that an address expression does not escape.
3474 set_does_not_escape()
3476 go_assert(this->op_ == OPERATOR_AND);
3477 this->escapes_ = false;
3480 // Record that this is an address expression which should create a
3481 // temporary variable if necessary. This is used for method calls.
3485 go_assert(this->op_ == OPERATOR_AND);
3486 this->create_temp_ = true;
3489 // Apply unary opcode OP to UNC, setting NC. Return true if this
3490 // could be done, false if not. Issue errors for overflow.
3492 eval_constant(Operator op, const Numeric_constant* unc,
3493 Location, Numeric_constant* nc);
3500 do_traverse(Traverse* traverse)
3501 { return Expression::traverse(&this->expr_, traverse); }
3504 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3507 do_is_constant() const;
3510 do_numeric_constant_value(Numeric_constant*) const;
3516 do_determine_type(const Type_context*);
3519 do_check_types(Gogo*);
3524 return Expression::make_unary(this->op_, this->expr_->copy(),