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();
99 // This virtual function is called to export expressions. This will
100 // only be used by expressions which may be constant.
103 Expression::do_export(Export*) const
108 // Give an error saying that the value of the expression is not used.
111 Expression::unused_value_error()
113 this->report_error(_("value computed is not used"));
116 // Note that this expression is an error. This is called by children
117 // when they discover an error.
120 Expression::set_is_error()
122 this->classification_ = EXPRESSION_ERROR;
125 // For children to call to report an error conveniently.
128 Expression::report_error(const char* msg)
130 error_at(this->location_, "%s", msg);
131 this->set_is_error();
134 // Set types of variables and constants. This is implemented by the
138 Expression::determine_type(const Type_context* context)
140 this->do_determine_type(context);
143 // Set types when there is no context.
146 Expression::determine_type_no_context()
148 Type_context context;
149 this->do_determine_type(&context);
152 // Return a tree handling any conversions which must be done during
156 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
157 Type* rhs_type, tree rhs_tree,
160 if (lhs_type->is_error() || rhs_type->is_error())
161 return error_mark_node;
163 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
164 return error_mark_node;
166 Gogo* gogo = context->gogo();
168 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
169 if (lhs_type_tree == error_mark_node)
170 return error_mark_node;
172 if (lhs_type->forwarded() != rhs_type->forwarded()
173 && lhs_type->interface_type() != NULL)
175 if (rhs_type->interface_type() == NULL)
176 return Expression::convert_type_to_interface(context, lhs_type,
180 return Expression::convert_interface_to_interface(context, lhs_type,
184 else if (lhs_type->forwarded() != rhs_type->forwarded()
185 && rhs_type->interface_type() != NULL)
186 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
188 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
190 // Assigning nil to an open array.
191 go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
193 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
195 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
196 tree field = TYPE_FIELDS(lhs_type_tree);
197 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
200 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
202 elt = VEC_quick_push(constructor_elt, init, NULL);
203 field = DECL_CHAIN(field);
204 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
207 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
209 elt = VEC_quick_push(constructor_elt, init, NULL);
210 field = DECL_CHAIN(field);
211 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
214 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
216 tree val = build_constructor(lhs_type_tree, init);
217 TREE_CONSTANT(val) = 1;
221 else if (rhs_type->is_nil_type())
223 // The left hand side should be a pointer type at the tree
225 go_assert(POINTER_TYPE_P(lhs_type_tree));
226 return fold_convert(lhs_type_tree, null_pointer_node);
228 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
230 // No conversion is needed.
233 else if (POINTER_TYPE_P(lhs_type_tree)
234 || INTEGRAL_TYPE_P(lhs_type_tree)
235 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
236 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
237 return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree);
238 else if ((TREE_CODE(lhs_type_tree) == RECORD_TYPE
239 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
240 || (TREE_CODE(lhs_type_tree) == ARRAY_TYPE
241 && TREE_CODE(TREE_TYPE(rhs_tree)) == ARRAY_TYPE))
243 // Avoid confusion from zero sized variables which may be
244 // represented as non-zero-sized.
245 if (int_size_in_bytes(lhs_type_tree) == 0
246 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
249 // This conversion must be permitted by Go, or we wouldn't have
251 go_assert(int_size_in_bytes(lhs_type_tree)
252 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
253 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
254 lhs_type_tree, rhs_tree);
258 go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
263 // Return a tree for a conversion from a non-interface type to an
267 Expression::convert_type_to_interface(Translate_context* context,
268 Type* lhs_type, Type* rhs_type,
269 tree rhs_tree, Location location)
271 Gogo* gogo = context->gogo();
272 Interface_type* lhs_interface_type = lhs_type->interface_type();
273 bool lhs_is_empty = lhs_interface_type->is_empty();
275 // Since RHS_TYPE is a static type, we can create the interface
276 // method table at compile time.
278 // When setting an interface to nil, we just set both fields to
280 if (rhs_type->is_nil_type())
282 Btype* lhs_btype = lhs_type->get_backend(gogo);
283 return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
286 // This should have been checked already.
287 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
289 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
290 if (lhs_type_tree == error_mark_node)
291 return error_mark_node;
293 // An interface is a tuple. If LHS_TYPE is an empty interface type,
294 // then the first field is the type descriptor for RHS_TYPE.
295 // Otherwise it is the interface method table for RHS_TYPE.
296 tree first_field_value;
298 first_field_value = rhs_type->type_descriptor_pointer(gogo, location);
301 // Build the interface method table for this interface and this
302 // object type: a list of function pointers for each interface
304 Named_type* rhs_named_type = rhs_type->named_type();
305 Struct_type* rhs_struct_type = rhs_type->struct_type();
306 bool is_pointer = false;
307 if (rhs_named_type == NULL && rhs_struct_type == NULL)
309 rhs_named_type = rhs_type->deref()->named_type();
310 rhs_struct_type = rhs_type->deref()->struct_type();
314 if (rhs_named_type != NULL)
316 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
318 else if (rhs_struct_type != NULL)
320 rhs_struct_type->interface_method_table(gogo, lhs_interface_type,
323 method_table = null_pointer_node;
324 first_field_value = fold_convert_loc(location.gcc_location(),
325 const_ptr_type_node, method_table);
327 if (first_field_value == error_mark_node)
328 return error_mark_node;
330 // Start building a constructor for the value we will return.
332 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
334 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
335 tree field = TYPE_FIELDS(lhs_type_tree);
336 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
337 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
339 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
342 elt = VEC_quick_push(constructor_elt, init, NULL);
343 field = DECL_CHAIN(field);
344 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
347 if (rhs_type->points_to() != NULL)
349 // We are assigning a pointer to the interface; the interface
350 // holds the pointer itself.
351 elt->value = rhs_tree;
352 return build_constructor(lhs_type_tree, init);
355 // We are assigning a non-pointer value to the interface; the
356 // interface gets a copy of the value in the heap.
358 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
360 tree space = gogo->allocate_memory(rhs_type, object_size, location);
361 space = fold_convert_loc(location.gcc_location(),
362 build_pointer_type(TREE_TYPE(rhs_tree)), space);
363 space = save_expr(space);
365 tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space);
366 TREE_THIS_NOTRAP(ref) = 1;
367 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
368 void_type_node, ref, rhs_tree);
370 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
373 return build2(COMPOUND_EXPR, lhs_type_tree, set,
374 build_constructor(lhs_type_tree, init));
377 // Return a tree for the type descriptor of RHS_TREE, which has
378 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
382 Expression::get_interface_type_descriptor(Translate_context*,
383 Type* rhs_type, tree rhs_tree,
386 tree rhs_type_tree = TREE_TYPE(rhs_tree);
387 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
388 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
389 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
391 if (rhs_type->interface_type()->is_empty())
393 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
394 "__type_descriptor") == 0);
398 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
400 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
402 tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v);
403 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
404 tree f = TYPE_FIELDS(TREE_TYPE(v1));
405 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
407 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
409 tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node,
410 v, fold_convert_loc(location.gcc_location(),
413 tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1),
415 return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1),
419 // Return a tree for the conversion of an interface type to an
423 Expression::convert_interface_to_interface(Translate_context* context,
424 Type *lhs_type, Type *rhs_type,
425 tree rhs_tree, bool for_type_guard,
428 Gogo* gogo = context->gogo();
429 Interface_type* lhs_interface_type = lhs_type->interface_type();
430 bool lhs_is_empty = lhs_interface_type->is_empty();
432 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
433 if (lhs_type_tree == error_mark_node)
434 return error_mark_node;
436 // In the general case this requires runtime examination of the type
437 // method table to match it up with the interface methods.
439 // FIXME: If all of the methods in the right hand side interface
440 // also appear in the left hand side interface, then we don't need
441 // to do a runtime check, although we still need to build a new
444 // Get the type descriptor for the right hand side. This will be
445 // NULL for a nil interface.
447 if (!DECL_P(rhs_tree))
448 rhs_tree = save_expr(rhs_tree);
450 tree rhs_type_descriptor =
451 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
454 // The result is going to be a two element constructor.
456 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
458 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
459 tree field = TYPE_FIELDS(lhs_type_tree);
464 // A type assertion fails when converting a nil interface.
465 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
467 static tree assert_interface_decl;
468 tree call = Gogo::call_builtin(&assert_interface_decl,
470 "__go_assert_interface",
473 TREE_TYPE(lhs_type_descriptor),
475 TREE_TYPE(rhs_type_descriptor),
476 rhs_type_descriptor);
477 if (call == error_mark_node)
478 return error_mark_node;
479 // This will panic if the interface conversion fails.
480 TREE_NOTHROW(assert_interface_decl) = 0;
481 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
484 else if (lhs_is_empty)
486 // A convertion to an empty interface always succeeds, and the
487 // first field is just the type descriptor of the object.
488 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
489 "__type_descriptor") == 0);
490 elt->value = fold_convert_loc(location.gcc_location(),
491 TREE_TYPE(field), rhs_type_descriptor);
495 // A conversion to a non-empty interface may fail, but unlike a
496 // type assertion converting nil will always succeed.
497 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
499 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
501 static tree convert_interface_decl;
502 tree call = Gogo::call_builtin(&convert_interface_decl,
504 "__go_convert_interface",
507 TREE_TYPE(lhs_type_descriptor),
509 TREE_TYPE(rhs_type_descriptor),
510 rhs_type_descriptor);
511 if (call == error_mark_node)
512 return error_mark_node;
513 // This will panic if the interface conversion fails.
514 TREE_NOTHROW(convert_interface_decl) = 0;
515 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
519 // The second field is simply the object pointer.
521 elt = VEC_quick_push(constructor_elt, init, NULL);
522 field = DECL_CHAIN(field);
523 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
526 tree rhs_type_tree = TREE_TYPE(rhs_tree);
527 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
528 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
529 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
530 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
533 return build_constructor(lhs_type_tree, init);
536 // Return a tree for the conversion of an interface type to a
537 // non-interface type.
540 Expression::convert_interface_to_type(Translate_context* context,
541 Type *lhs_type, Type* rhs_type,
542 tree rhs_tree, Location location)
544 Gogo* gogo = context->gogo();
545 tree rhs_type_tree = TREE_TYPE(rhs_tree);
547 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
548 if (lhs_type_tree == error_mark_node)
549 return error_mark_node;
551 // Call a function to check that the type is valid. The function
552 // will panic with an appropriate runtime type error if the type is
555 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location);
557 if (!DECL_P(rhs_tree))
558 rhs_tree = save_expr(rhs_tree);
560 tree rhs_type_descriptor =
561 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
564 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo,
567 static tree check_interface_type_decl;
568 tree call = Gogo::call_builtin(&check_interface_type_decl,
570 "__go_check_interface_type",
573 TREE_TYPE(lhs_type_descriptor),
575 TREE_TYPE(rhs_type_descriptor),
577 TREE_TYPE(rhs_inter_descriptor),
578 rhs_inter_descriptor);
579 if (call == error_mark_node)
580 return error_mark_node;
581 // This call will panic if the conversion is invalid.
582 TREE_NOTHROW(check_interface_type_decl) = 0;
584 // If the call succeeds, pull out the value.
585 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
586 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
587 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
588 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
591 // If the value is a pointer, then it is the value we want.
592 // Otherwise it points to the value.
593 if (lhs_type->points_to() == NULL)
595 val = fold_convert_loc(location.gcc_location(),
596 build_pointer_type(lhs_type_tree), val);
597 val = build_fold_indirect_ref_loc(location.gcc_location(), val);
600 return build2(COMPOUND_EXPR, lhs_type_tree, call,
601 fold_convert_loc(location.gcc_location(), lhs_type_tree, val));
604 // Convert an expression to a tree. This is implemented by the child
605 // class. Not that it is not in general safe to call this multiple
606 // times for a single expression, but that we don't catch such errors.
609 Expression::get_tree(Translate_context* context)
611 // The child may have marked this expression as having an error.
612 if (this->classification_ == EXPRESSION_ERROR)
613 return error_mark_node;
615 return this->do_get_tree(context);
618 // Return a tree for VAL in TYPE.
621 Expression::integer_constant_tree(mpz_t val, tree type)
623 if (type == error_mark_node)
624 return error_mark_node;
625 else if (TREE_CODE(type) == INTEGER_TYPE)
626 return double_int_to_tree(type,
627 mpz_get_double_int(type, val, true));
628 else if (TREE_CODE(type) == REAL_TYPE)
631 mpfr_init_set_z(fval, val, GMP_RNDN);
632 tree ret = Expression::float_constant_tree(fval, type);
636 else if (TREE_CODE(type) == COMPLEX_TYPE)
639 mpfr_init_set_z(fval, val, GMP_RNDN);
640 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
642 tree imag = build_real_from_int_cst(TREE_TYPE(type),
644 return build_complex(type, real, imag);
650 // Return a tree for VAL in TYPE.
653 Expression::float_constant_tree(mpfr_t val, tree type)
655 if (type == error_mark_node)
656 return error_mark_node;
657 else if (TREE_CODE(type) == INTEGER_TYPE)
661 mpfr_get_z(ival, val, GMP_RNDN);
662 tree ret = Expression::integer_constant_tree(ival, type);
666 else if (TREE_CODE(type) == REAL_TYPE)
669 real_from_mpfr(&r1, val, type, GMP_RNDN);
671 real_convert(&r2, TYPE_MODE(type), &r1);
672 return build_real(type, r2);
674 else if (TREE_CODE(type) == COMPLEX_TYPE)
677 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
679 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
680 tree imag = build_real_from_int_cst(TREE_TYPE(type),
682 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
688 // Return a tree for REAL/IMAG in TYPE.
691 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
693 if (type == error_mark_node)
694 return error_mark_node;
695 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
696 return Expression::float_constant_tree(real, type);
697 else if (TREE_CODE(type) == COMPLEX_TYPE)
700 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
702 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
705 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
707 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
709 return build_complex(type, build_real(TREE_TYPE(type), r2),
710 build_real(TREE_TYPE(type), r4));
716 // Return a tree which evaluates to true if VAL, of arbitrary integer
717 // type, is negative or is more than the maximum value of BOUND_TYPE.
718 // If SOFAR is not NULL, it is or'red into the result. The return
719 // value may be NULL if SOFAR is NULL.
722 Expression::check_bounds(tree val, tree bound_type, tree sofar,
725 tree val_type = TREE_TYPE(val);
726 tree ret = NULL_TREE;
728 if (!TYPE_UNSIGNED(val_type))
730 ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val,
731 build_int_cst(val_type, 0));
732 if (ret == boolean_false_node)
736 HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
737 HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
738 go_assert(val_type_size != -1 && bound_type_size != -1);
739 if (val_type_size > bound_type_size
740 || (val_type_size == bound_type_size
741 && TYPE_UNSIGNED(val_type)
742 && !TYPE_UNSIGNED(bound_type)))
744 tree max = TYPE_MAX_VALUE(bound_type);
745 tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node,
746 val, fold_convert_loc(loc.gcc_location(),
748 if (big == boolean_false_node)
750 else if (ret == NULL_TREE)
753 ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
754 boolean_type_node, ret, big);
757 if (ret == NULL_TREE)
759 else if (sofar == NULL_TREE)
762 return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node,
767 Expression::dump_expression(Ast_dump_context* ast_dump_context) const
769 this->do_dump_expression(ast_dump_context);
772 // Error expressions. This are used to avoid cascading errors.
774 class Error_expression : public Expression
777 Error_expression(Location location)
778 : Expression(EXPRESSION_ERROR, location)
783 do_is_constant() const
787 do_numeric_constant_value(Numeric_constant* nc) const
789 nc->set_unsigned_long(NULL, 0);
794 do_discarding_value()
799 { return Type::make_error_type(); }
802 do_determine_type(const Type_context*)
810 do_is_addressable() const
814 do_get_tree(Translate_context*)
815 { return error_mark_node; }
818 do_dump_expression(Ast_dump_context*) const;
821 // Dump the ast representation for an error expression to a dump context.
824 Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
826 ast_dump_context->ostream() << "_Error_" ;
830 Expression::make_error(Location location)
832 return new Error_expression(location);
835 // An expression which is really a type. This is used during parsing.
836 // It is an error if these survive after lowering.
839 Type_expression : public Expression
842 Type_expression(Type* type, Location location)
843 : Expression(EXPRESSION_TYPE, location),
849 do_traverse(Traverse* traverse)
850 { return Type::traverse(this->type_, traverse); }
854 { return this->type_; }
857 do_determine_type(const Type_context*)
861 do_check_types(Gogo*)
862 { this->report_error(_("invalid use of type")); }
869 do_get_tree(Translate_context*)
870 { go_unreachable(); }
872 void do_dump_expression(Ast_dump_context*) const;
875 // The type which we are representing as an expression.
880 Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
882 ast_dump_context->dump_type(this->type_);
886 Expression::make_type(Type* type, Location location)
888 return new Type_expression(type, location);
891 // Class Parser_expression.
894 Parser_expression::do_type()
896 // We should never really ask for the type of a Parser_expression.
897 // However, it can happen, at least when we have an invalid const
898 // whose initializer refers to the const itself. In that case we
899 // may ask for the type when lowering the const itself.
900 go_assert(saw_errors());
901 return Type::make_error_type();
904 // Class Var_expression.
906 // Lower a variable expression. Here we just make sure that the
907 // initialization expression of the variable has been lowered. This
908 // ensures that we will be able to determine the type of the variable
912 Var_expression::do_lower(Gogo* gogo, Named_object* function,
913 Statement_inserter* inserter, int)
915 if (this->variable_->is_variable())
917 Variable* var = this->variable_->var_value();
918 // This is either a local variable or a global variable. A
919 // reference to a variable which is local to an enclosing
920 // function will be a reference to a field in a closure.
921 if (var->is_global())
926 var->lower_init_expression(gogo, function, inserter);
931 // Return the type of a reference to a variable.
934 Var_expression::do_type()
936 if (this->variable_->is_variable())
937 return this->variable_->var_value()->type();
938 else if (this->variable_->is_result_variable())
939 return this->variable_->result_var_value()->type();
944 // Determine the type of a reference to a variable.
947 Var_expression::do_determine_type(const Type_context*)
949 if (this->variable_->is_variable())
950 this->variable_->var_value()->determine_type();
953 // Something takes the address of this variable. This means that we
954 // may want to move the variable onto the heap.
957 Var_expression::do_address_taken(bool escapes)
961 if (this->variable_->is_variable())
962 this->variable_->var_value()->set_non_escaping_address_taken();
963 else if (this->variable_->is_result_variable())
964 this->variable_->result_var_value()->set_non_escaping_address_taken();
970 if (this->variable_->is_variable())
971 this->variable_->var_value()->set_address_taken();
972 else if (this->variable_->is_result_variable())
973 this->variable_->result_var_value()->set_address_taken();
979 // Get the tree for a reference to a variable.
982 Var_expression::do_get_tree(Translate_context* context)
984 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
985 context->function());
986 tree ret = var_to_tree(bvar);
987 if (ret == error_mark_node)
988 return error_mark_node;
990 if (this->variable_->is_variable())
991 is_in_heap = this->variable_->var_value()->is_in_heap();
992 else if (this->variable_->is_result_variable())
993 is_in_heap = this->variable_->result_var_value()->is_in_heap();
998 ret = build_fold_indirect_ref_loc(this->location().gcc_location(), ret);
999 TREE_THIS_NOTRAP(ret) = 1;
1004 // Ast dump for variable expression.
1007 Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1009 ast_dump_context->ostream() << this->variable_->name() ;
1012 // Make a reference to a variable in an expression.
1015 Expression::make_var_reference(Named_object* var, Location location)
1018 return Expression::make_sink(location);
1020 // FIXME: Creating a new object for each reference to a variable is
1022 return new Var_expression(var, location);
1025 // Class Temporary_reference_expression.
1030 Temporary_reference_expression::do_type()
1032 return this->statement_->type();
1035 // Called if something takes the address of this temporary variable.
1036 // We never have to move temporary variables to the heap, but we do
1037 // need to know that they must live in the stack rather than in a
1041 Temporary_reference_expression::do_address_taken(bool)
1043 this->statement_->set_is_address_taken();
1046 // Get a tree referring to the variable.
1049 Temporary_reference_expression::do_get_tree(Translate_context* context)
1051 Bvariable* bvar = this->statement_->get_backend_variable(context);
1053 // The gcc backend can't represent the same set of recursive types
1054 // that the Go frontend can. In some cases this means that a
1055 // temporary variable won't have the right backend type. Correct
1056 // that here by adding a type cast. We need to use base() to push
1057 // the circularity down one level.
1058 tree ret = var_to_tree(bvar);
1059 if (!this->is_lvalue_
1060 && POINTER_TYPE_P(TREE_TYPE(ret))
1061 && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
1063 Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1064 tree type_tree = type_to_tree(type_btype);
1065 ret = fold_convert_loc(this->location().gcc_location(), type_tree, ret);
1070 // Ast dump for temporary reference.
1073 Temporary_reference_expression::do_dump_expression(
1074 Ast_dump_context* ast_dump_context) const
1076 ast_dump_context->dump_temp_variable_name(this->statement_);
1079 // Make a reference to a temporary variable.
1081 Temporary_reference_expression*
1082 Expression::make_temporary_reference(Temporary_statement* statement,
1085 return new Temporary_reference_expression(statement, location);
1088 // Class Set_and_use_temporary_expression.
1093 Set_and_use_temporary_expression::do_type()
1095 return this->statement_->type();
1098 // Take the address.
1101 Set_and_use_temporary_expression::do_address_taken(bool)
1103 this->statement_->set_is_address_taken();
1106 // Return the backend representation.
1109 Set_and_use_temporary_expression::do_get_tree(Translate_context* context)
1111 Bvariable* bvar = this->statement_->get_backend_variable(context);
1112 tree var_tree = var_to_tree(bvar);
1113 tree expr_tree = this->expr_->get_tree(context);
1114 if (var_tree == error_mark_node || expr_tree == error_mark_node)
1115 return error_mark_node;
1116 Location loc = this->location();
1117 return build2_loc(loc.gcc_location(), COMPOUND_EXPR, TREE_TYPE(var_tree),
1118 build2_loc(loc.gcc_location(), MODIFY_EXPR, void_type_node,
1119 var_tree, expr_tree),
1126 Set_and_use_temporary_expression::do_dump_expression(
1127 Ast_dump_context* ast_dump_context) const
1129 ast_dump_context->ostream() << '(';
1130 ast_dump_context->dump_temp_variable_name(this->statement_);
1131 ast_dump_context->ostream() << " = ";
1132 this->expr_->dump_expression(ast_dump_context);
1133 ast_dump_context->ostream() << ')';
1136 // Make a set-and-use temporary.
1138 Set_and_use_temporary_expression*
1139 Expression::make_set_and_use_temporary(Temporary_statement* statement,
1140 Expression* expr, Location location)
1142 return new Set_and_use_temporary_expression(statement, expr, location);
1145 // A sink expression--a use of the blank identifier _.
1147 class Sink_expression : public Expression
1150 Sink_expression(Location location)
1151 : Expression(EXPRESSION_SINK, location),
1152 type_(NULL), var_(NULL_TREE)
1157 do_discarding_value()
1164 do_determine_type(const Type_context*);
1168 { return new Sink_expression(this->location()); }
1171 do_get_tree(Translate_context*);
1174 do_dump_expression(Ast_dump_context*) const;
1177 // The type of this sink variable.
1179 // The temporary variable we generate.
1183 // Return the type of a sink expression.
1186 Sink_expression::do_type()
1188 if (this->type_ == NULL)
1189 return Type::make_sink_type();
1193 // Determine the type of a sink expression.
1196 Sink_expression::do_determine_type(const Type_context* context)
1198 if (context->type != NULL)
1199 this->type_ = context->type;
1202 // Return a temporary variable for a sink expression. This will
1203 // presumably be a write-only variable which the middle-end will drop.
1206 Sink_expression::do_get_tree(Translate_context* context)
1208 if (this->var_ == NULL_TREE)
1210 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
1211 Btype* bt = this->type_->get_backend(context->gogo());
1212 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
1217 // Ast dump for sink expression.
1220 Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1222 ast_dump_context->ostream() << "_" ;
1225 // Make a sink expression.
1228 Expression::make_sink(Location location)
1230 return new Sink_expression(location);
1233 // Class Func_expression.
1235 // FIXME: Can a function expression appear in a constant expression?
1236 // The value is unchanging. Initializing a constant to the address of
1237 // a function seems like it could work, though there might be little
1243 Func_expression::do_traverse(Traverse* traverse)
1245 return (this->closure_ == NULL
1247 : Expression::traverse(&this->closure_, traverse));
1250 // Return the type of a function expression.
1253 Func_expression::do_type()
1255 if (this->function_->is_function())
1256 return this->function_->func_value()->type();
1257 else if (this->function_->is_function_declaration())
1258 return this->function_->func_declaration_value()->type();
1263 // Get the tree for a function expression without evaluating the
1267 Func_expression::get_tree_without_closure(Gogo* gogo)
1269 Function_type* fntype;
1270 if (this->function_->is_function())
1271 fntype = this->function_->func_value()->type();
1272 else if (this->function_->is_function_declaration())
1273 fntype = this->function_->func_declaration_value()->type();
1277 // Builtin functions are handled specially by Call_expression. We
1278 // can't take their address.
1279 if (fntype->is_builtin())
1281 error_at(this->location(),
1282 "invalid use of special builtin function %qs; must be called",
1283 this->function_->name().c_str());
1284 return error_mark_node;
1287 Named_object* no = this->function_;
1289 tree id = no->get_id(gogo);
1290 if (id == error_mark_node)
1291 return error_mark_node;
1294 if (no->is_function())
1295 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1296 else if (no->is_function_declaration())
1297 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1301 if (fndecl == error_mark_node)
1302 return error_mark_node;
1304 return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl);
1307 // Get the tree for a function expression. This is used when we take
1308 // the address of a function rather than simply calling it. If the
1309 // function has a closure, we must use a trampoline.
1312 Func_expression::do_get_tree(Translate_context* context)
1314 Gogo* gogo = context->gogo();
1316 tree fnaddr = this->get_tree_without_closure(gogo);
1317 if (fnaddr == error_mark_node)
1318 return error_mark_node;
1320 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1321 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1322 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1324 // If there is no closure, that is all have to do.
1325 if (this->closure_ == NULL)
1328 go_assert(this->function_->func_value()->enclosing() != NULL);
1330 // Get the value of the closure. This will be a pointer to space
1331 // allocated on the heap.
1332 tree closure_tree = this->closure_->get_tree(context);
1333 if (closure_tree == error_mark_node)
1334 return error_mark_node;
1335 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1337 // Now we need to build some code on the heap. This code will load
1338 // the static chain pointer with the closure and then jump to the
1339 // body of the function. The normal gcc approach is to build the
1340 // code on the stack. Unfortunately we can not do that, as Go
1341 // permits us to return the function pointer.
1343 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1346 // Ast dump for function.
1349 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1351 ast_dump_context->ostream() << this->function_->name();
1352 if (this->closure_ != NULL)
1354 ast_dump_context->ostream() << " {closure = ";
1355 this->closure_->dump_expression(ast_dump_context);
1356 ast_dump_context->ostream() << "}";
1360 // Make a reference to a function in an expression.
1363 Expression::make_func_reference(Named_object* function, Expression* closure,
1366 return new Func_expression(function, closure, location);
1369 // Class Unknown_expression.
1371 // Return the name of an unknown expression.
1374 Unknown_expression::name() const
1376 return this->named_object_->name();
1379 // Lower a reference to an unknown name.
1382 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1384 Location location = this->location();
1385 Named_object* no = this->named_object_;
1387 if (!no->is_unknown())
1391 real = no->unknown_value()->real_named_object();
1394 if (this->is_composite_literal_key_)
1396 if (!this->no_error_message_)
1397 error_at(location, "reference to undefined name %qs",
1398 this->named_object_->message_name().c_str());
1399 return Expression::make_error(location);
1402 switch (real->classification())
1404 case Named_object::NAMED_OBJECT_CONST:
1405 return Expression::make_const_reference(real, location);
1406 case Named_object::NAMED_OBJECT_TYPE:
1407 return Expression::make_type(real->type_value(), location);
1408 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1409 if (this->is_composite_literal_key_)
1411 if (!this->no_error_message_)
1412 error_at(location, "reference to undefined type %qs",
1413 real->message_name().c_str());
1414 return Expression::make_error(location);
1415 case Named_object::NAMED_OBJECT_VAR:
1416 real->var_value()->set_is_used();
1417 return Expression::make_var_reference(real, location);
1418 case Named_object::NAMED_OBJECT_FUNC:
1419 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1420 return Expression::make_func_reference(real, NULL, location);
1421 case Named_object::NAMED_OBJECT_PACKAGE:
1422 if (this->is_composite_literal_key_)
1424 if (!this->no_error_message_)
1425 error_at(location, "unexpected reference to package");
1426 return Expression::make_error(location);
1432 // Dump the ast representation for an unknown expression to a dump context.
1435 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1437 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1441 // Make a reference to an unknown name.
1444 Expression::make_unknown_reference(Named_object* no, Location location)
1446 return new Unknown_expression(no, location);
1449 // A boolean expression.
1451 class Boolean_expression : public Expression
1454 Boolean_expression(bool val, Location location)
1455 : Expression(EXPRESSION_BOOLEAN, location),
1456 val_(val), type_(NULL)
1464 do_is_constant() const
1471 do_determine_type(const Type_context*);
1478 do_get_tree(Translate_context*)
1479 { return this->val_ ? boolean_true_node : boolean_false_node; }
1482 do_export(Export* exp) const
1483 { exp->write_c_string(this->val_ ? "true" : "false"); }
1486 do_dump_expression(Ast_dump_context* ast_dump_context) const
1487 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1492 // The type as determined by context.
1499 Boolean_expression::do_type()
1501 if (this->type_ == NULL)
1502 this->type_ = Type::make_boolean_type();
1506 // Set the type from the context.
1509 Boolean_expression::do_determine_type(const Type_context* context)
1511 if (this->type_ != NULL && !this->type_->is_abstract())
1513 else if (context->type != NULL && context->type->is_boolean_type())
1514 this->type_ = context->type;
1515 else if (!context->may_be_abstract)
1516 this->type_ = Type::lookup_bool_type();
1519 // Import a boolean constant.
1522 Boolean_expression::do_import(Import* imp)
1524 if (imp->peek_char() == 't')
1526 imp->require_c_string("true");
1527 return Expression::make_boolean(true, imp->location());
1531 imp->require_c_string("false");
1532 return Expression::make_boolean(false, imp->location());
1536 // Make a boolean expression.
1539 Expression::make_boolean(bool val, Location location)
1541 return new Boolean_expression(val, location);
1544 // Class String_expression.
1549 String_expression::do_type()
1551 if (this->type_ == NULL)
1552 this->type_ = Type::make_string_type();
1556 // Set the type from the context.
1559 String_expression::do_determine_type(const Type_context* context)
1561 if (this->type_ != NULL && !this->type_->is_abstract())
1563 else if (context->type != NULL && context->type->is_string_type())
1564 this->type_ = context->type;
1565 else if (!context->may_be_abstract)
1566 this->type_ = Type::lookup_string_type();
1569 // Build a string constant.
1572 String_expression::do_get_tree(Translate_context* context)
1574 return context->gogo()->go_string_constant_tree(this->val_);
1577 // Write string literal to string dump.
1580 String_expression::export_string(String_dump* exp,
1581 const String_expression* str)
1584 s.reserve(str->val_.length() * 4 + 2);
1586 for (std::string::const_iterator p = str->val_.begin();
1587 p != str->val_.end();
1590 if (*p == '\\' || *p == '"')
1595 else if (*p >= 0x20 && *p < 0x7f)
1597 else if (*p == '\n')
1599 else if (*p == '\t')
1604 unsigned char c = *p;
1605 unsigned int dig = c >> 4;
1606 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1608 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1612 exp->write_string(s);
1615 // Export a string expression.
1618 String_expression::do_export(Export* exp) const
1620 String_expression::export_string(exp, this);
1623 // Import a string expression.
1626 String_expression::do_import(Import* imp)
1628 imp->require_c_string("\"");
1632 int c = imp->get_char();
1633 if (c == '"' || c == -1)
1636 val += static_cast<char>(c);
1639 c = imp->get_char();
1640 if (c == '\\' || c == '"')
1641 val += static_cast<char>(c);
1648 c = imp->get_char();
1649 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1650 c = imp->get_char();
1651 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1652 char v = (vh << 4) | vl;
1657 error_at(imp->location(), "bad string constant");
1658 return Expression::make_error(imp->location());
1662 return Expression::make_string(val, imp->location());
1665 // Ast dump for string expression.
1668 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1670 String_expression::export_string(ast_dump_context, this);
1673 // Make a string expression.
1676 Expression::make_string(const std::string& val, Location location)
1678 return new String_expression(val, location);
1681 // Make an integer expression.
1683 class Integer_expression : public Expression
1686 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
1688 : Expression(EXPRESSION_INTEGER, location),
1689 type_(type), is_character_constant_(is_character_constant)
1690 { mpz_init_set(this->val_, *val); }
1695 // Write VAL to string dump.
1697 export_integer(String_dump* exp, const mpz_t val);
1699 // Write VAL to dump context.
1701 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1705 do_is_constant() const
1709 do_numeric_constant_value(Numeric_constant* nc) const;
1715 do_determine_type(const Type_context* context);
1718 do_check_types(Gogo*);
1721 do_get_tree(Translate_context*);
1726 if (this->is_character_constant_)
1727 return Expression::make_character(&this->val_, this->type_,
1730 return Expression::make_integer(&this->val_, this->type_,
1735 do_export(Export*) const;
1738 do_dump_expression(Ast_dump_context*) const;
1741 // The integer value.
1745 // Whether this is a character constant.
1746 bool is_character_constant_;
1749 // Return a numeric constant for this expression. We have to mark
1750 // this as a character when appropriate.
1753 Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const
1755 if (this->is_character_constant_)
1756 nc->set_rune(this->type_, this->val_);
1758 nc->set_int(this->type_, this->val_);
1762 // Return the current type. If we haven't set the type yet, we return
1763 // an abstract integer type.
1766 Integer_expression::do_type()
1768 if (this->type_ == NULL)
1770 if (this->is_character_constant_)
1771 this->type_ = Type::make_abstract_character_type();
1773 this->type_ = Type::make_abstract_integer_type();
1778 // Set the type of the integer value. Here we may switch from an
1779 // abstract type to a real type.
1782 Integer_expression::do_determine_type(const Type_context* context)
1784 if (this->type_ != NULL && !this->type_->is_abstract())
1786 else if (context->type != NULL && context->type->is_numeric_type())
1787 this->type_ = context->type;
1788 else if (!context->may_be_abstract)
1790 if (this->is_character_constant_)
1791 this->type_ = Type::lookup_integer_type("int32");
1793 this->type_ = Type::lookup_integer_type("int");
1797 // Check the type of an integer constant.
1800 Integer_expression::do_check_types(Gogo*)
1802 Type* type = this->type_;
1805 Numeric_constant nc;
1806 if (this->is_character_constant_)
1807 nc.set_rune(NULL, this->val_);
1809 nc.set_int(NULL, this->val_);
1810 if (!nc.set_type(type, true, this->location()))
1811 this->set_is_error();
1814 // Get a tree for an integer constant.
1817 Integer_expression::do_get_tree(Translate_context* context)
1819 Gogo* gogo = context->gogo();
1821 if (this->type_ != NULL && !this->type_->is_abstract())
1822 type = type_to_tree(this->type_->get_backend(gogo));
1823 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1825 // We are converting to an abstract floating point type.
1826 Type* ftype = Type::lookup_float_type("float64");
1827 type = type_to_tree(ftype->get_backend(gogo));
1829 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1831 // We are converting to an abstract complex type.
1832 Type* ctype = Type::lookup_complex_type("complex128");
1833 type = type_to_tree(ctype->get_backend(gogo));
1837 // If we still have an abstract type here, then this is being
1838 // used in a constant expression which didn't get reduced for
1839 // some reason. Use a type which will fit the value. We use <,
1840 // not <=, because we need an extra bit for the sign bit.
1841 int bits = mpz_sizeinbase(this->val_, 2);
1842 if (bits < INT_TYPE_SIZE)
1844 Type* t = Type::lookup_integer_type("int");
1845 type = type_to_tree(t->get_backend(gogo));
1849 Type* t = Type::lookup_integer_type("int64");
1850 type = type_to_tree(t->get_backend(gogo));
1853 type = long_long_integer_type_node;
1855 return Expression::integer_constant_tree(this->val_, type);
1858 // Write VAL to export data.
1861 Integer_expression::export_integer(String_dump* exp, const mpz_t val)
1863 char* s = mpz_get_str(NULL, 10, val);
1864 exp->write_c_string(s);
1868 // Export an integer in a constant expression.
1871 Integer_expression::do_export(Export* exp) const
1873 Integer_expression::export_integer(exp, this->val_);
1874 if (this->is_character_constant_)
1875 exp->write_c_string("'");
1876 // A trailing space lets us reliably identify the end of the number.
1877 exp->write_c_string(" ");
1880 // Import an integer, floating point, or complex value. This handles
1881 // all these types because they all start with digits.
1884 Integer_expression::do_import(Import* imp)
1886 std::string num = imp->read_identifier();
1887 imp->require_c_string(" ");
1888 if (!num.empty() && num[num.length() - 1] == 'i')
1891 size_t plus_pos = num.find('+', 1);
1892 size_t minus_pos = num.find('-', 1);
1894 if (plus_pos == std::string::npos)
1896 else if (minus_pos == std::string::npos)
1900 error_at(imp->location(), "bad number in import data: %qs",
1902 return Expression::make_error(imp->location());
1904 if (pos == std::string::npos)
1905 mpfr_set_ui(real, 0, GMP_RNDN);
1908 std::string real_str = num.substr(0, pos);
1909 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1911 error_at(imp->location(), "bad number in import data: %qs",
1913 return Expression::make_error(imp->location());
1917 std::string imag_str;
1918 if (pos == std::string::npos)
1921 imag_str = num.substr(pos);
1922 imag_str = imag_str.substr(0, imag_str.size() - 1);
1924 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1926 error_at(imp->location(), "bad number in import data: %qs",
1928 return Expression::make_error(imp->location());
1930 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1936 else if (num.find('.') == std::string::npos
1937 && num.find('E') == std::string::npos)
1939 bool is_character_constant = (!num.empty()
1940 && num[num.length() - 1] == '\'');
1941 if (is_character_constant)
1942 num = num.substr(0, num.length() - 1);
1944 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1946 error_at(imp->location(), "bad number in import data: %qs",
1948 return Expression::make_error(imp->location());
1951 if (is_character_constant)
1952 ret = Expression::make_character(&val, NULL, imp->location());
1954 ret = Expression::make_integer(&val, NULL, imp->location());
1961 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1963 error_at(imp->location(), "bad number in import data: %qs",
1965 return Expression::make_error(imp->location());
1967 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1972 // Ast dump for integer expression.
1975 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1977 if (this->is_character_constant_)
1978 ast_dump_context->ostream() << '\'';
1979 Integer_expression::export_integer(ast_dump_context, this->val_);
1980 if (this->is_character_constant_)
1981 ast_dump_context->ostream() << '\'';
1984 // Build a new integer value.
1987 Expression::make_integer(const mpz_t* val, Type* type, Location location)
1989 return new Integer_expression(val, type, false, location);
1992 // Build a new character constant value.
1995 Expression::make_character(const mpz_t* val, Type* type, Location location)
1997 return new Integer_expression(val, type, true, location);
2002 class Float_expression : public Expression
2005 Float_expression(const mpfr_t* val, Type* type, Location location)
2006 : Expression(EXPRESSION_FLOAT, location),
2009 mpfr_init_set(this->val_, *val, GMP_RNDN);
2012 // Write VAL to export data.
2014 export_float(String_dump* exp, const mpfr_t val);
2016 // Write VAL to dump file.
2018 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
2022 do_is_constant() const
2026 do_numeric_constant_value(Numeric_constant* nc) const
2028 nc->set_float(this->type_, this->val_);
2036 do_determine_type(const Type_context*);
2039 do_check_types(Gogo*);
2043 { return Expression::make_float(&this->val_, this->type_,
2044 this->location()); }
2047 do_get_tree(Translate_context*);
2050 do_export(Export*) const;
2053 do_dump_expression(Ast_dump_context*) const;
2056 // The floating point value.
2062 // Return the current type. If we haven't set the type yet, we return
2063 // an abstract float type.
2066 Float_expression::do_type()
2068 if (this->type_ == NULL)
2069 this->type_ = Type::make_abstract_float_type();
2073 // Set the type of the float value. Here we may switch from an
2074 // abstract type to a real type.
2077 Float_expression::do_determine_type(const Type_context* context)
2079 if (this->type_ != NULL && !this->type_->is_abstract())
2081 else if (context->type != NULL
2082 && (context->type->integer_type() != NULL
2083 || context->type->float_type() != NULL
2084 || context->type->complex_type() != NULL))
2085 this->type_ = context->type;
2086 else if (!context->may_be_abstract)
2087 this->type_ = Type::lookup_float_type("float64");
2090 // Check the type of a float value.
2093 Float_expression::do_check_types(Gogo*)
2095 Type* type = this->type_;
2098 Numeric_constant nc;
2099 nc.set_float(NULL, this->val_);
2100 if (!nc.set_type(this->type_, true, this->location()))
2101 this->set_is_error();
2104 // Get a tree for a float constant.
2107 Float_expression::do_get_tree(Translate_context* context)
2109 Gogo* gogo = context->gogo();
2111 if (this->type_ != NULL && !this->type_->is_abstract())
2112 type = type_to_tree(this->type_->get_backend(gogo));
2113 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2115 // We have an abstract integer type. We just hope for the best.
2116 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2120 // If we still have an abstract type here, then this is being
2121 // used in a constant expression which didn't get reduced. We
2122 // just use float64 and hope for the best.
2123 Type* ft = Type::lookup_float_type("float64");
2124 type = type_to_tree(ft->get_backend(gogo));
2126 return Expression::float_constant_tree(this->val_, type);
2129 // Write a floating point number to a string dump.
2132 Float_expression::export_float(String_dump *exp, const mpfr_t val)
2135 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2137 exp->write_c_string("-");
2138 exp->write_c_string("0.");
2139 exp->write_c_string(*s == '-' ? s + 1 : s);
2142 snprintf(buf, sizeof buf, "E%ld", exponent);
2143 exp->write_c_string(buf);
2146 // Export a floating point number in a constant expression.
2149 Float_expression::do_export(Export* exp) const
2151 Float_expression::export_float(exp, this->val_);
2152 // A trailing space lets us reliably identify the end of the number.
2153 exp->write_c_string(" ");
2156 // Dump a floating point number to the dump file.
2159 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2161 Float_expression::export_float(ast_dump_context, this->val_);
2164 // Make a float expression.
2167 Expression::make_float(const mpfr_t* val, Type* type, Location location)
2169 return new Float_expression(val, type, location);
2174 class Complex_expression : public Expression
2177 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2179 : Expression(EXPRESSION_COMPLEX, location),
2182 mpfr_init_set(this->real_, *real, GMP_RNDN);
2183 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2186 // Write REAL/IMAG to string dump.
2188 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
2190 // Write REAL/IMAG to dump context.
2192 dump_complex(Ast_dump_context* ast_dump_context,
2193 const mpfr_t real, const mpfr_t val);
2197 do_is_constant() const
2201 do_numeric_constant_value(Numeric_constant* nc) const
2203 nc->set_complex(this->type_, this->real_, this->imag_);
2211 do_determine_type(const Type_context*);
2214 do_check_types(Gogo*);
2219 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2224 do_get_tree(Translate_context*);
2227 do_export(Export*) const;
2230 do_dump_expression(Ast_dump_context*) const;
2235 // The imaginary part;
2237 // The type if known.
2241 // Return the current type. If we haven't set the type yet, we return
2242 // an abstract complex type.
2245 Complex_expression::do_type()
2247 if (this->type_ == NULL)
2248 this->type_ = Type::make_abstract_complex_type();
2252 // Set the type of the complex value. Here we may switch from an
2253 // abstract type to a real type.
2256 Complex_expression::do_determine_type(const Type_context* context)
2258 if (this->type_ != NULL && !this->type_->is_abstract())
2260 else if (context->type != NULL
2261 && context->type->complex_type() != NULL)
2262 this->type_ = context->type;
2263 else if (!context->may_be_abstract)
2264 this->type_ = Type::lookup_complex_type("complex128");
2267 // Check the type of a complex value.
2270 Complex_expression::do_check_types(Gogo*)
2272 Type* type = this->type_;
2275 Numeric_constant nc;
2276 nc.set_complex(NULL, this->real_, this->imag_);
2277 if (!nc.set_type(this->type_, true, this->location()))
2278 this->set_is_error();
2281 // Get a tree for a complex constant.
2284 Complex_expression::do_get_tree(Translate_context* context)
2286 Gogo* gogo = context->gogo();
2288 if (this->type_ != NULL && !this->type_->is_abstract())
2289 type = type_to_tree(this->type_->get_backend(gogo));
2292 // If we still have an abstract type here, this this is being
2293 // used in a constant expression which didn't get reduced. We
2294 // just use complex128 and hope for the best.
2295 Type* ct = Type::lookup_complex_type("complex128");
2296 type = type_to_tree(ct->get_backend(gogo));
2298 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2301 // Write REAL/IMAG to export data.
2304 Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
2307 if (!mpfr_zero_p(real))
2309 Float_expression::export_float(exp, real);
2310 if (mpfr_sgn(imag) > 0)
2311 exp->write_c_string("+");
2313 Float_expression::export_float(exp, imag);
2314 exp->write_c_string("i");
2317 // Export a complex number in a constant expression.
2320 Complex_expression::do_export(Export* exp) const
2322 Complex_expression::export_complex(exp, this->real_, this->imag_);
2323 // A trailing space lets us reliably identify the end of the number.
2324 exp->write_c_string(" ");
2327 // Dump a complex expression to the dump file.
2330 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2332 Complex_expression::export_complex(ast_dump_context,
2337 // Make a complex expression.
2340 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2343 return new Complex_expression(real, imag, type, location);
2346 // Find a named object in an expression.
2348 class Find_named_object : public Traverse
2351 Find_named_object(Named_object* no)
2352 : Traverse(traverse_expressions),
2353 no_(no), found_(false)
2356 // Whether we found the object.
2359 { return this->found_; }
2363 expression(Expression**);
2366 // The object we are looking for.
2368 // Whether we found it.
2372 // A reference to a const in an expression.
2374 class Const_expression : public Expression
2377 Const_expression(Named_object* constant, Location location)
2378 : Expression(EXPRESSION_CONST_REFERENCE, location),
2379 constant_(constant), type_(NULL), seen_(false)
2384 { return this->constant_; }
2386 // Check that the initializer does not refer to the constant itself.
2388 check_for_init_loop();
2392 do_traverse(Traverse*);
2395 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2398 do_is_constant() const
2402 do_numeric_constant_value(Numeric_constant* nc) const;
2405 do_string_constant_value(std::string* val) const;
2410 // The type of a const is set by the declaration, not the use.
2412 do_determine_type(const Type_context*);
2415 do_check_types(Gogo*);
2422 do_get_tree(Translate_context* context);
2424 // When exporting a reference to a const as part of a const
2425 // expression, we export the value. We ignore the fact that it has
2428 do_export(Export* exp) const
2429 { this->constant_->const_value()->expr()->export_expression(exp); }
2432 do_dump_expression(Ast_dump_context*) const;
2436 Named_object* constant_;
2437 // The type of this reference. This is used if the constant has an
2440 // Used to prevent infinite recursion when a constant incorrectly
2441 // refers to itself.
2448 Const_expression::do_traverse(Traverse* traverse)
2450 if (this->type_ != NULL)
2451 return Type::traverse(this->type_, traverse);
2452 return TRAVERSE_CONTINUE;
2455 // Lower a constant expression. This is where we convert the
2456 // predeclared constant iota into an integer value.
2459 Const_expression::do_lower(Gogo* gogo, Named_object*,
2460 Statement_inserter*, int iota_value)
2462 if (this->constant_->const_value()->expr()->classification()
2465 if (iota_value == -1)
2467 error_at(this->location(),
2468 "iota is only defined in const declarations");
2472 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2473 Expression* ret = Expression::make_integer(&val, NULL,
2479 // Make sure that the constant itself has been lowered.
2480 gogo->lower_constant(this->constant_);
2485 // Return a numeric constant value.
2488 Const_expression::do_numeric_constant_value(Numeric_constant* nc) const
2493 Expression* e = this->constant_->const_value()->expr();
2497 bool r = e->numeric_constant_value(nc);
2499 this->seen_ = false;
2502 if (this->type_ != NULL)
2503 ctype = this->type_;
2505 ctype = this->constant_->const_value()->type();
2506 if (r && ctype != NULL)
2508 if (!nc->set_type(ctype, false, this->location()))
2516 Const_expression::do_string_constant_value(std::string* val) const
2521 Expression* e = this->constant_->const_value()->expr();
2524 bool ok = e->string_constant_value(val);
2525 this->seen_ = false;
2530 // Return the type of the const reference.
2533 Const_expression::do_type()
2535 if (this->type_ != NULL)
2538 Named_constant* nc = this->constant_->const_value();
2540 if (this->seen_ || nc->lowering())
2542 this->report_error(_("constant refers to itself"));
2543 this->type_ = Type::make_error_type();
2549 Type* ret = nc->type();
2553 this->seen_ = false;
2557 // During parsing, a named constant may have a NULL type, but we
2558 // must not return a NULL type here.
2559 ret = nc->expr()->type();
2561 this->seen_ = false;
2566 // Set the type of the const reference.
2569 Const_expression::do_determine_type(const Type_context* context)
2571 Type* ctype = this->constant_->const_value()->type();
2572 Type* cetype = (ctype != NULL
2574 : this->constant_->const_value()->expr()->type());
2575 if (ctype != NULL && !ctype->is_abstract())
2577 else if (context->type != NULL
2578 && context->type->is_numeric_type()
2579 && cetype->is_numeric_type())
2580 this->type_ = context->type;
2581 else if (context->type != NULL
2582 && context->type->is_string_type()
2583 && cetype->is_string_type())
2584 this->type_ = context->type;
2585 else if (context->type != NULL
2586 && context->type->is_boolean_type()
2587 && cetype->is_boolean_type())
2588 this->type_ = context->type;
2589 else if (!context->may_be_abstract)
2591 if (cetype->is_abstract())
2592 cetype = cetype->make_non_abstract_type();
2593 this->type_ = cetype;
2597 // Check for a loop in which the initializer of a constant refers to
2598 // the constant itself.
2601 Const_expression::check_for_init_loop()
2603 if (this->type_ != NULL && this->type_->is_error())
2608 this->report_error(_("constant refers to itself"));
2609 this->type_ = Type::make_error_type();
2613 Expression* init = this->constant_->const_value()->expr();
2614 Find_named_object find_named_object(this->constant_);
2617 Expression::traverse(&init, &find_named_object);
2618 this->seen_ = false;
2620 if (find_named_object.found())
2622 if (this->type_ == NULL || !this->type_->is_error())
2624 this->report_error(_("constant refers to itself"));
2625 this->type_ = Type::make_error_type();
2631 // Check types of a const reference.
2634 Const_expression::do_check_types(Gogo*)
2636 if (this->type_ != NULL && this->type_->is_error())
2639 this->check_for_init_loop();
2641 // Check that numeric constant fits in type.
2642 if (this->type_ != NULL && this->type_->is_numeric_type())
2644 Numeric_constant nc;
2645 if (this->constant_->const_value()->expr()->numeric_constant_value(&nc))
2647 if (!nc.set_type(this->type_, true, this->location()))
2648 this->set_is_error();
2653 // Return a tree for the const reference.
2656 Const_expression::do_get_tree(Translate_context* context)
2658 Gogo* gogo = context->gogo();
2660 if (this->type_ == NULL)
2661 type_tree = NULL_TREE;
2664 type_tree = type_to_tree(this->type_->get_backend(gogo));
2665 if (type_tree == error_mark_node)
2666 return error_mark_node;
2669 // If the type has been set for this expression, but the underlying
2670 // object is an abstract int or float, we try to get the abstract
2671 // value. Otherwise we may lose something in the conversion.
2672 if (this->type_ != NULL
2673 && this->type_->is_numeric_type()
2674 && (this->constant_->const_value()->type() == NULL
2675 || this->constant_->const_value()->type()->is_abstract()))
2677 Expression* expr = this->constant_->const_value()->expr();
2678 Numeric_constant nc;
2679 if (expr->numeric_constant_value(&nc)
2680 && nc.set_type(this->type_, false, this->location()))
2682 Expression* e = nc.expression(this->location());
2683 return e->get_tree(context);
2687 tree const_tree = this->constant_->get_tree(gogo, context->function());
2688 if (this->type_ == NULL
2689 || const_tree == error_mark_node
2690 || TREE_TYPE(const_tree) == error_mark_node)
2694 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2695 ret = fold_convert(type_tree, const_tree);
2696 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2697 ret = fold(convert_to_integer(type_tree, const_tree));
2698 else if (TREE_CODE(type_tree) == REAL_TYPE)
2699 ret = fold(convert_to_real(type_tree, const_tree));
2700 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2701 ret = fold(convert_to_complex(type_tree, const_tree));
2707 // Dump ast representation for constant expression.
2710 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2712 ast_dump_context->ostream() << this->constant_->name();
2715 // Make a reference to a constant in an expression.
2718 Expression::make_const_reference(Named_object* constant,
2721 return new Const_expression(constant, location);
2724 // Find a named object in an expression.
2727 Find_named_object::expression(Expression** pexpr)
2729 switch ((*pexpr)->classification())
2731 case Expression::EXPRESSION_CONST_REFERENCE:
2733 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2734 if (ce->named_object() == this->no_)
2737 // We need to check a constant initializer explicitly, as
2738 // loops here will not be caught by the loop checking for
2739 // variable initializers.
2740 ce->check_for_init_loop();
2742 return TRAVERSE_CONTINUE;
2745 case Expression::EXPRESSION_VAR_REFERENCE:
2746 if ((*pexpr)->var_expression()->named_object() == this->no_)
2748 return TRAVERSE_CONTINUE;
2749 case Expression::EXPRESSION_FUNC_REFERENCE:
2750 if ((*pexpr)->func_expression()->named_object() == this->no_)
2752 return TRAVERSE_CONTINUE;
2754 return TRAVERSE_CONTINUE;
2756 this->found_ = true;
2757 return TRAVERSE_EXIT;
2762 class Nil_expression : public Expression
2765 Nil_expression(Location location)
2766 : Expression(EXPRESSION_NIL, location)
2774 do_is_constant() const
2779 { return Type::make_nil_type(); }
2782 do_determine_type(const Type_context*)
2790 do_get_tree(Translate_context*)
2791 { return null_pointer_node; }
2794 do_export(Export* exp) const
2795 { exp->write_c_string("nil"); }
2798 do_dump_expression(Ast_dump_context* ast_dump_context) const
2799 { ast_dump_context->ostream() << "nil"; }
2802 // Import a nil expression.
2805 Nil_expression::do_import(Import* imp)
2807 imp->require_c_string("nil");
2808 return Expression::make_nil(imp->location());
2811 // Make a nil expression.
2814 Expression::make_nil(Location location)
2816 return new Nil_expression(location);
2819 // The value of the predeclared constant iota. This is little more
2820 // than a marker. This will be lowered to an integer in
2821 // Const_expression::do_lower, which is where we know the value that
2824 class Iota_expression : public Parser_expression
2827 Iota_expression(Location location)
2828 : Parser_expression(EXPRESSION_IOTA, location)
2833 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2834 { go_unreachable(); }
2836 // There should only ever be one of these.
2839 { go_unreachable(); }
2842 do_dump_expression(Ast_dump_context* ast_dump_context) const
2843 { ast_dump_context->ostream() << "iota"; }
2846 // Make an iota expression. This is only called for one case: the
2847 // value of the predeclared constant iota.
2850 Expression::make_iota()
2852 static Iota_expression iota_expression(Linemap::unknown_location());
2853 return &iota_expression;
2856 // A type conversion expression.
2858 class Type_conversion_expression : public Expression
2861 Type_conversion_expression(Type* type, Expression* expr,
2863 : Expression(EXPRESSION_CONVERSION, location),
2864 type_(type), expr_(expr), may_convert_function_types_(false)
2867 // Return the type to which we are converting.
2870 { return this->type_; }
2872 // Return the expression which we are converting.
2875 { return this->expr_; }
2877 // Permit converting from one function type to another. This is
2878 // used internally for method expressions.
2880 set_may_convert_function_types()
2882 this->may_convert_function_types_ = true;
2885 // Import a type conversion expression.
2891 do_traverse(Traverse* traverse);
2894 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2897 do_is_constant() const
2898 { return this->expr_->is_constant(); }
2901 do_numeric_constant_value(Numeric_constant*) const;
2904 do_string_constant_value(std::string*) const;
2908 { return this->type_; }
2911 do_determine_type(const Type_context*)
2913 Type_context subcontext(this->type_, false);
2914 this->expr_->determine_type(&subcontext);
2918 do_check_types(Gogo*);
2923 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2928 do_get_tree(Translate_context* context);
2931 do_export(Export*) const;
2934 do_dump_expression(Ast_dump_context*) const;
2937 // The type to convert to.
2939 // The expression to convert.
2941 // True if this is permitted to convert function types. This is
2942 // used internally for method expressions.
2943 bool may_convert_function_types_;
2949 Type_conversion_expression::do_traverse(Traverse* traverse)
2951 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
2952 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
2953 return TRAVERSE_EXIT;
2954 return TRAVERSE_CONTINUE;
2957 // Convert to a constant at lowering time.
2960 Type_conversion_expression::do_lower(Gogo*, Named_object*,
2961 Statement_inserter*, int)
2963 Type* type = this->type_;
2964 Expression* val = this->expr_;
2965 Location location = this->location();
2967 if (type->is_numeric_type())
2969 Numeric_constant nc;
2970 if (val->numeric_constant_value(&nc))
2972 if (!nc.set_type(type, true, location))
2973 return Expression::make_error(location);
2974 return nc.expression(location);
2978 if (type->is_slice_type())
2980 Type* element_type = type->array_type()->element_type()->forwarded();
2981 bool is_byte = (element_type->integer_type() != NULL
2982 && element_type->integer_type()->is_byte());
2983 bool is_rune = (element_type->integer_type() != NULL
2984 && element_type->integer_type()->is_rune());
2985 if (is_byte || is_rune)
2988 if (val->string_constant_value(&s))
2990 Expression_list* vals = new Expression_list();
2993 for (std::string::const_iterator p = s.begin();
2998 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
2999 Expression* v = Expression::make_integer(&val,
3008 const char *p = s.data();
3009 const char *pend = s.data() + s.length();
3013 int adv = Lex::fetch_char(p, &c);
3016 warning_at(this->location(), 0,
3017 "invalid UTF-8 encoding");
3022 mpz_init_set_ui(val, c);
3023 Expression* v = Expression::make_integer(&val,
3031 return Expression::make_slice_composite_literal(type, vals,
3040 // Return the constant numeric value if there is one.
3043 Type_conversion_expression::do_numeric_constant_value(
3044 Numeric_constant* nc) const
3046 if (!this->type_->is_numeric_type())
3048 if (!this->expr_->numeric_constant_value(nc))
3050 return nc->set_type(this->type_, false, this->location());
3053 // Return the constant string value if there is one.
3056 Type_conversion_expression::do_string_constant_value(std::string* val) const
3058 if (this->type_->is_string_type()
3059 && this->expr_->type()->integer_type() != NULL)
3061 Numeric_constant nc;
3062 if (this->expr_->numeric_constant_value(&nc))
3065 if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID)
3068 Lex::append_char(ival, true, val, this->location());
3074 // FIXME: Could handle conversion from const []int here.
3079 // Check that types are convertible.
3082 Type_conversion_expression::do_check_types(Gogo*)
3084 Type* type = this->type_;
3085 Type* expr_type = this->expr_->type();
3088 if (type->is_error() || expr_type->is_error())
3090 this->set_is_error();
3094 if (this->may_convert_function_types_
3095 && type->function_type() != NULL
3096 && expr_type->function_type() != NULL)
3099 if (Type::are_convertible(type, expr_type, &reason))
3102 error_at(this->location(), "%s", reason.c_str());
3103 this->set_is_error();
3106 // Get a tree for a type conversion.
3109 Type_conversion_expression::do_get_tree(Translate_context* context)
3111 Gogo* gogo = context->gogo();
3112 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
3113 tree expr_tree = this->expr_->get_tree(context);
3115 if (type_tree == error_mark_node
3116 || expr_tree == error_mark_node
3117 || TREE_TYPE(expr_tree) == error_mark_node)
3118 return error_mark_node;
3120 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3121 return fold_convert(type_tree, expr_tree);
3123 Type* type = this->type_;
3124 Type* expr_type = this->expr_->type();
3126 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3127 ret = Expression::convert_for_assignment(context, type, expr_type,
3128 expr_tree, this->location());
3129 else if (type->integer_type() != NULL)
3131 if (expr_type->integer_type() != NULL
3132 || expr_type->float_type() != NULL
3133 || expr_type->is_unsafe_pointer_type())
3134 ret = fold(convert_to_integer(type_tree, expr_tree));
3138 else if (type->float_type() != NULL)
3140 if (expr_type->integer_type() != NULL
3141 || expr_type->float_type() != NULL)
3142 ret = fold(convert_to_real(type_tree, expr_tree));
3146 else if (type->complex_type() != NULL)
3148 if (expr_type->complex_type() != NULL)
3149 ret = fold(convert_to_complex(type_tree, expr_tree));
3153 else if (type->is_string_type()
3154 && expr_type->integer_type() != NULL)
3156 expr_tree = fold_convert(integer_type_node, expr_tree);
3157 if (host_integerp(expr_tree, 0))
3159 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3161 Lex::append_char(intval, true, &s, this->location());
3162 Expression* se = Expression::make_string(s, this->location());
3163 return se->get_tree(context);
3166 static tree int_to_string_fndecl;
3167 ret = Gogo::call_builtin(&int_to_string_fndecl,
3169 "__go_int_to_string",
3173 fold_convert(integer_type_node, expr_tree));
3175 else if (type->is_string_type() && expr_type->is_slice_type())
3177 if (!DECL_P(expr_tree))
3178 expr_tree = save_expr(expr_tree);
3179 Array_type* a = expr_type->array_type();
3180 Type* e = a->element_type()->forwarded();
3181 go_assert(e->integer_type() != NULL);
3182 tree valptr = fold_convert(const_ptr_type_node,
3183 a->value_pointer_tree(gogo, expr_tree));
3184 tree len = a->length_tree(gogo, expr_tree);
3185 len = fold_convert_loc(this->location().gcc_location(), integer_type_node,
3187 if (e->integer_type()->is_byte())
3189 static tree byte_array_to_string_fndecl;
3190 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3192 "__go_byte_array_to_string",
3195 const_ptr_type_node,
3202 go_assert(e->integer_type()->is_rune());
3203 static tree int_array_to_string_fndecl;
3204 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3206 "__go_int_array_to_string",
3209 const_ptr_type_node,
3215 else if (type->is_slice_type() && expr_type->is_string_type())
3217 Type* e = type->array_type()->element_type()->forwarded();
3218 go_assert(e->integer_type() != NULL);
3219 if (e->integer_type()->is_byte())
3221 tree string_to_byte_array_fndecl = NULL_TREE;
3222 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3224 "__go_string_to_byte_array",
3227 TREE_TYPE(expr_tree),
3232 go_assert(e->integer_type()->is_rune());
3233 tree string_to_int_array_fndecl = NULL_TREE;
3234 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3236 "__go_string_to_int_array",
3239 TREE_TYPE(expr_tree),
3243 else if ((type->is_unsafe_pointer_type()
3244 && expr_type->points_to() != NULL)
3245 || (expr_type->is_unsafe_pointer_type()
3246 && type->points_to() != NULL))
3247 ret = fold_convert(type_tree, expr_tree);
3248 else if (type->is_unsafe_pointer_type()
3249 && expr_type->integer_type() != NULL)
3250 ret = convert_to_pointer(type_tree, expr_tree);
3251 else if (this->may_convert_function_types_
3252 && type->function_type() != NULL
3253 && expr_type->function_type() != NULL)
3254 ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3257 ret = Expression::convert_for_assignment(context, type, expr_type,
3258 expr_tree, this->location());
3263 // Output a type conversion in a constant expression.
3266 Type_conversion_expression::do_export(Export* exp) const
3268 exp->write_c_string("convert(");
3269 exp->write_type(this->type_);
3270 exp->write_c_string(", ");
3271 this->expr_->export_expression(exp);
3272 exp->write_c_string(")");
3275 // Import a type conversion or a struct construction.
3278 Type_conversion_expression::do_import(Import* imp)
3280 imp->require_c_string("convert(");
3281 Type* type = imp->read_type();
3282 imp->require_c_string(", ");
3283 Expression* val = Expression::import_expression(imp);
3284 imp->require_c_string(")");
3285 return Expression::make_cast(type, val, imp->location());
3288 // Dump ast representation for a type conversion expression.
3291 Type_conversion_expression::do_dump_expression(
3292 Ast_dump_context* ast_dump_context) const
3294 ast_dump_context->dump_type(this->type_);
3295 ast_dump_context->ostream() << "(";
3296 ast_dump_context->dump_expression(this->expr_);
3297 ast_dump_context->ostream() << ") ";
3300 // Make a type cast expression.
3303 Expression::make_cast(Type* type, Expression* val, Location location)
3305 if (type->is_error_type() || val->is_error_expression())
3306 return Expression::make_error(location);
3307 return new Type_conversion_expression(type, val, location);
3310 // An unsafe type conversion, used to pass values to builtin functions.
3312 class Unsafe_type_conversion_expression : public Expression
3315 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3317 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3318 type_(type), expr_(expr)
3323 do_traverse(Traverse* traverse);
3327 { return this->type_; }
3330 do_determine_type(const Type_context*)
3331 { this->expr_->determine_type_no_context(); }
3336 return new Unsafe_type_conversion_expression(this->type_,
3337 this->expr_->copy(),
3342 do_get_tree(Translate_context*);
3345 do_dump_expression(Ast_dump_context*) const;
3348 // The type to convert to.
3350 // The expression to convert.
3357 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3359 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3360 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3361 return TRAVERSE_EXIT;
3362 return TRAVERSE_CONTINUE;
3365 // Convert to backend representation.
3368 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3370 // We are only called for a limited number of cases.
3372 Type* t = this->type_;
3373 Type* et = this->expr_->type();
3375 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
3376 tree expr_tree = this->expr_->get_tree(context);
3377 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3378 return error_mark_node;
3380 Location loc = this->location();
3382 bool use_view_convert = false;
3383 if (t->is_slice_type())
3385 go_assert(et->is_slice_type());
3386 use_view_convert = true;
3388 else if (t->map_type() != NULL)
3389 go_assert(et->map_type() != NULL);
3390 else if (t->channel_type() != NULL)
3391 go_assert(et->channel_type() != NULL);
3392 else if (t->points_to() != NULL)
3393 go_assert(et->points_to() != NULL || et->is_nil_type());
3394 else if (et->is_unsafe_pointer_type())
3395 go_assert(t->points_to() != NULL);
3396 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3398 go_assert(et->interface_type() != NULL
3399 && !et->interface_type()->is_empty());
3400 use_view_convert = true;
3402 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3404 go_assert(et->interface_type() != NULL
3405 && et->interface_type()->is_empty());
3406 use_view_convert = true;
3408 else if (t->integer_type() != NULL)
3410 go_assert(et->is_boolean_type()
3411 || et->integer_type() != NULL
3412 || et->function_type() != NULL
3413 || et->points_to() != NULL
3414 || et->map_type() != NULL
3415 || et->channel_type() != NULL);
3416 return convert_to_integer(type_tree, expr_tree);
3421 if (use_view_convert)
3422 return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3425 return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
3428 // Dump ast representation for an unsafe type conversion expression.
3431 Unsafe_type_conversion_expression::do_dump_expression(
3432 Ast_dump_context* ast_dump_context) const
3434 ast_dump_context->dump_type(this->type_);
3435 ast_dump_context->ostream() << "(";
3436 ast_dump_context->dump_expression(this->expr_);
3437 ast_dump_context->ostream() << ") ";
3440 // Make an unsafe type conversion expression.
3443 Expression::make_unsafe_cast(Type* type, Expression* expr,
3446 return new Unsafe_type_conversion_expression(type, expr, location);
3449 // Unary expressions.
3451 class Unary_expression : public Expression
3454 Unary_expression(Operator op, Expression* expr, Location location)
3455 : Expression(EXPRESSION_UNARY, location),
3456 op_(op), escapes_(true), create_temp_(false), expr_(expr)
3459 // Return the operator.
3462 { return this->op_; }
3464 // Return the operand.
3467 { return this->expr_; }
3469 // Record that an address expression does not escape.
3471 set_does_not_escape()
3473 go_assert(this->op_ == OPERATOR_AND);
3474 this->escapes_ = false;
3477 // Record that this is an address expression which should create a
3478 // temporary variable if necessary. This is used for method calls.
3482 go_assert(this->op_ == OPERATOR_AND);
3483 this->create_temp_ = true;
3486 // Apply unary opcode OP to UNC, setting NC. Return true if this
3487 // could be done, false if not. Issue errors for overflow.
3489 eval_constant(Operator op, const Numeric_constant* unc,
3490 Location, Numeric_constant* nc);
3497 do_traverse(Traverse* traverse)
3498 { return Expression::traverse(&this->expr_, traverse); }
3501 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3504 do_is_constant() const;
3507 do_numeric_constant_value(Numeric_constant*) const;
3513 do_determine_type(const Type_context*);
3516 do_check_types(Gogo*);
3521 return Expression::make_unary(this->op_, this->expr_->copy(),
3526 do_must_eval_subexpressions_in_order(int*) const
3527 { return this->op_ == OPERATOR_MULT; }
3530 do_is_addressable() const
3531 { return this->op_ == OPERATOR_MULT; }
3534 do_get_tree(Translate_context*);
3537 do_export(Export*) const;
3540 do_dump_expression(Ast_dump_context*) const;
3543 // The unary operator to apply.
3545 // Normally true. False if this is an address expression which does
3546 // not escape the current function.
3548 // True if this is an address expression which should create a
3549 // temporary variable if necessary.
3555 // If we are taking the address of a composite literal, and the
3556 // contents are not constant, then we want to make a heap composite
3560 Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
3562 Location loc = this->location();
3563 Operator op = this->op_;
3564 Expression* expr = this->expr_;
3566 if (op == OPERATOR_MULT && expr->is_type_expression())
3567 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3569 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3570 // moving x to the heap. FIXME: Is it worth doing a real escape
3571 // analysis here? This case is found in math/unsafe.go and is
3572 // therefore worth special casing.
3573 if (op == OPERATOR_MULT)
3575 Expression* e = expr;
3576 while (e->classification() == EXPRESSION_CONVERSION)
3578 Type_conversion_expression* te
3579 = static_cast<Type_conversion_expression*>(e);
3583 if (e->classification() == EXPRESSION_UNARY)
3585 Unary_expression* ue = static_cast<Unary_expression*>(e);
3586 if (ue->op_ == OPERATOR_AND)
3593 ue->set_does_not_escape();
3598 // Catching an invalid indirection of unsafe.Pointer here avoid
3599 // having to deal with TYPE_VOID in other places.
3600 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3602 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3603 return Expression::make_error(this->location());
3606 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR)
3608 Numeric_constant nc;
3609 if (expr->numeric_constant_value(&nc))
3611 Numeric_constant result;
3612 if (Unary_expression::eval_constant(op, &nc, loc, &result))
3613 return result.expression(loc);
3620 // Return whether a unary expression is a constant.
3623 Unary_expression::do_is_constant() const
3625 if (this->op_ == OPERATOR_MULT)
3627 // Indirecting through a pointer is only constant if the object
3628 // to which the expression points is constant, but we currently
3629 // have no way to determine that.
3632 else if (this->op_ == OPERATOR_AND)
3634 // Taking the address of a variable is constant if it is a
3635 // global variable, not constant otherwise. In other cases
3636 // taking the address is probably not a constant.
3637 Var_expression* ve = this->expr_->var_expression();
3640 Named_object* no = ve->named_object();
3641 return no->is_variable() && no->var_value()->is_global();
3646 return this->expr_->is_constant();
3649 // Apply unary opcode OP to UNC, setting NC. Return true if this
3650 // could be done, false if not. Issue errors for overflow.
3653 Unary_expression::eval_constant(Operator op, const Numeric_constant* unc,
3654 Location location, Numeric_constant* nc)
3662 case OPERATOR_MINUS:
3663 if (unc->is_int() || unc->is_rune())
3665 else if (unc->is_float())
3668 unc->get_float(&uval);
3671 mpfr_neg(val, uval, GMP_RNDN);
3672 nc->set_float(unc->type(), val);
3677 else if (unc->is_complex())
3679 mpfr_t ureal, uimag;
3680 unc->get_complex(&ureal, &uimag);
3684 mpfr_neg(real, ureal, GMP_RNDN);
3685 mpfr_neg(imag, uimag, GMP_RNDN);
3686 nc->set_complex(unc->type(), real, imag);
3708 if (!unc->is_int() && !unc->is_rune())
3713 unc->get_rune(&uval);
3715 unc->get_int(&uval);
3721 case OPERATOR_MINUS:
3726 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3731 Type* utype = unc->type();
3732 if (utype->integer_type() == NULL
3733 || utype->integer_type()->is_abstract())
3737 // The number of HOST_WIDE_INTs that it takes to represent
3739 size_t count = ((mpz_sizeinbase(uval, 2)
3740 + HOST_BITS_PER_WIDE_INT
3742 / HOST_BITS_PER_WIDE_INT);
3744 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3745 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3747 size_t obits = utype->integer_type()->bits();
3749 if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0)
3752 mpz_init_set_ui(adj, 1);
3753 mpz_mul_2exp(adj, adj, obits);
3754 mpz_add(uval, uval, adj);
3759 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3760 go_assert(ecount <= count);
3762 // Trim down to the number of words required by the type.
3763 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3764 / HOST_BITS_PER_WIDE_INT);
3765 go_assert(ocount <= count);
3767 for (size_t i = 0; i < ocount; ++i)
3770 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3772 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3775 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3777 if (!utype->integer_type()->is_unsigned()
3778 && mpz_tstbit(val, obits - 1))
3781 mpz_init_set_ui(adj, 1);
3782 mpz_mul_2exp(adj, adj, obits);
3783 mpz_sub(val, val, adj);
3797 nc->set_rune(NULL, val);
3799 nc->set_int(NULL, val);
3804 return nc->set_type(unc->type(), true, location);
3807 // Return the integral constant value of a unary expression, if it has one.
3810 Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const
3812 Numeric_constant unc;
3813 if (!this->expr_->numeric_constant_value(&unc))
3815 return Unary_expression::eval_constant(this->op_, &unc, this->location(),
3819 // Return the type of a unary expression.
3822 Unary_expression::do_type()
3827 case OPERATOR_MINUS:
3830 return this->expr_->type();
3833 return Type::make_pointer_type(this->expr_->type());
3837 Type* subtype = this->expr_->type();
3838 Type* points_to = subtype->points_to();
3839 if (points_to == NULL)
3840 return Type::make_error_type();
3849 // Determine abstract types for a unary expression.
3852 Unary_expression::do_determine_type(const Type_context* context)
3857 case OPERATOR_MINUS:
3860 this->expr_->determine_type(context);
3864 // Taking the address of something.
3866 Type* subtype = (context->type == NULL
3868 : context->type->points_to());
3869 Type_context subcontext(subtype, false);
3870 this->expr_->determine_type(&subcontext);
3875 // Indirecting through a pointer.
3877 Type* subtype = (context->type == NULL
3879 : Type::make_pointer_type(context->type));
3880 Type_context subcontext(subtype, false);
3881 this->expr_->determine_type(&subcontext);
3890 // Check types for a unary expression.
3893 Unary_expression::do_check_types(Gogo*)
3895 Type* type = this->expr_->type();
3896 if (type->is_error())
3898 this->set_is_error();
3905 case OPERATOR_MINUS:
3906 if (type->integer_type() == NULL
3907 && type->float_type() == NULL
3908 && type->complex_type() == NULL)
3909 this->report_error(_("expected numeric type"));
3913 if (!type->is_boolean_type())
3914 this->report_error(_("expected boolean type"));
3918 if (type->integer_type() == NULL
3919 && !type->is_boolean_type())
3920 this->report_error(_("expected integer or boolean type"));
3924 if (!this->expr_->is_addressable())
3926 if (!this->create_temp_)
3927 this->report_error(_("invalid operand for unary %<&%>"));
3930 this->expr_->address_taken(this->escapes_);
3934 // Indirecting through a pointer.
3935 if (type->points_to() == NULL)
3936 this->report_error(_("expected pointer"));
3944 // Get a tree for a unary expression.
3947 Unary_expression::do_get_tree(Translate_context* context)
3949 Location loc = this->location();
3951 // Taking the address of a set-and-use-temporary expression requires
3952 // setting the temporary and then taking the address.
3953 if (this->op_ == OPERATOR_AND)
3955 Set_and_use_temporary_expression* sut =
3956 this->expr_->set_and_use_temporary_expression();
3959 Temporary_statement* temp = sut->temporary();
3960 Bvariable* bvar = temp->get_backend_variable(context);
3961 tree var_tree = var_to_tree(bvar);
3962 Expression* val = sut->expression();
3963 tree val_tree = val->get_tree(context);
3964 if (var_tree == error_mark_node || val_tree == error_mark_node)
3965 return error_mark_node;
3966 tree addr_tree = build_fold_addr_expr_loc(loc.gcc_location(),
3968 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
3969 TREE_TYPE(addr_tree),
3970 build2_loc(sut->location().gcc_location(),
3971 MODIFY_EXPR, void_type_node,
3972 var_tree, val_tree),
3977 tree expr = this->expr_->get_tree(context);
3978 if (expr == error_mark_node)
3979 return error_mark_node;
3986 case OPERATOR_MINUS:
3988 tree type = TREE_TYPE(expr);
3989 tree compute_type = excess_precision_type(type);
3990 if (compute_type != NULL_TREE)
3991 expr = ::convert(compute_type, expr);
3992 tree ret = fold_build1_loc(loc.gcc_location(), NEGATE_EXPR,
3993 (compute_type != NULL_TREE
3997 if (compute_type != NULL_TREE)
3998 ret = ::convert(type, ret);
4003 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4004 return fold_build1_loc(loc.gcc_location(), TRUTH_NOT_EXPR,
4005 TREE_TYPE(expr), expr);
4007 return fold_build2_loc(loc.gcc_location(), NE_EXPR, boolean_type_node,
4008 expr, build_int_cst(TREE_TYPE(expr), 0));
4011 return fold_build1_loc(loc.gcc_location(), BIT_NOT_EXPR, TREE_TYPE(expr),
4015 if (!this->create_temp_)
4017 // We should not see a non-constant constructor here; cases
4018 // where we would see one should have been moved onto the
4019 // heap at parse time. Taking the address of a nonconstant
4020 // constructor will not do what the programmer expects.
4021 go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4022 go_assert(TREE_CODE(expr) != ADDR_EXPR);
4025 // Build a decl for a constant constructor.
4026 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4028 tree decl = build_decl(this->location().gcc_location(), VAR_DECL,
4029 create_tmp_var_name("C"), TREE_TYPE(expr));
4030 DECL_EXTERNAL(decl) = 0;
4031 TREE_PUBLIC(decl) = 0;
4032 TREE_READONLY(decl) = 1;
4033 TREE_CONSTANT(decl) = 1;
4034 TREE_STATIC(decl) = 1;
4035 TREE_ADDRESSABLE(decl) = 1;
4036 DECL_ARTIFICIAL(decl) = 1;
4037 DECL_INITIAL(decl) = expr;
4038 rest_of_decl_compilation(decl, 1, 0);
4042 if (this->create_temp_
4043 && !TREE_ADDRESSABLE(TREE_TYPE(expr))
4044 && (TREE_CODE(expr) == CONST_DECL || !DECL_P(expr))
4045 && TREE_CODE(expr) != INDIRECT_REF
4046 && TREE_CODE(expr) != COMPONENT_REF)
4048 if (current_function_decl != NULL)
4050 tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr));
4051 DECL_IGNORED_P(tmp) = 1;
4052 DECL_INITIAL(tmp) = expr;
4053 TREE_ADDRESSABLE(tmp) = 1;
4054 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4055 build_pointer_type(TREE_TYPE(expr)),
4056 build1_loc(loc.gcc_location(), DECL_EXPR,
4057 void_type_node, tmp),
4058 build_fold_addr_expr_loc(loc.gcc_location(),
4063 tree tmp = build_decl(loc.gcc_location(), VAR_DECL,
4064 create_tmp_var_name("A"), TREE_TYPE(expr));
4065 DECL_EXTERNAL(tmp) = 0;
4066 TREE_PUBLIC(tmp) = 0;
4067 TREE_STATIC(tmp) = 1;
4068 DECL_ARTIFICIAL(tmp) = 1;
4069 TREE_ADDRESSABLE(tmp) = 1;
4071 if (!TREE_CONSTANT(expr))
4072 make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR,
4073 void_type_node, tmp, expr);
4076 TREE_READONLY(tmp) = 1;
4077 TREE_CONSTANT(tmp) = 1;
4078 DECL_INITIAL(tmp) = expr;
4079 make_tmp = NULL_TREE;
4081 rest_of_decl_compilation(tmp, 1, 0);
4082 tree addr = build_fold_addr_expr_loc(loc.gcc_location(), tmp);
4083 if (make_tmp == NULL_TREE)
4085 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4086 TREE_TYPE(addr), make_tmp, addr);
4090 return build_fold_addr_expr_loc(loc.gcc_location(), expr);
4094 go_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4096 // If we are dereferencing the pointer to a large struct, we
4097 // need to check for nil. We don't bother to check for small
4098 // structs because we expect the system to crash on a nil
4099 // pointer dereference.
4100 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr));
4101 if (!VOID_TYPE_P(target_type_tree))
4103 HOST_WIDE_INT s = int_size_in_bytes(target_type_tree);
4104 if (s == -1 || s >= 4096)