1 // expressions.cc -- Go frontend expression handling.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #ifndef ENABLE_BUILD_WITH_CXX
20 #include "tree-iterator.h"
25 #ifndef ENABLE_BUILD_WITH_CXX
34 #include "statements.h"
38 #include "expressions.h"
43 Expression::Expression(Expression_classification classification,
45 : classification_(classification), location_(location)
49 Expression::~Expression()
53 // If this expression has a constant integer value, return it.
56 Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
60 return this->do_integer_constant_value(iota_is_constant, val, ptype);
63 // If this expression has a constant floating point value, return it.
66 Expression::float_constant_value(mpfr_t val, Type** ptype) const
69 if (this->do_float_constant_value(val, ptype))
75 if (!this->do_integer_constant_value(false, ival, &t))
79 mpfr_set_z(val, ival, GMP_RNDN);
86 // If this expression has a constant complex value, return it.
89 Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
93 if (this->do_complex_constant_value(real, imag, ptype))
96 if (this->float_constant_value(real, &t))
98 mpfr_set_ui(imag, 0, GMP_RNDN);
104 // Traverse the expressions.
107 Expression::traverse(Expression** pexpr, Traverse* traverse)
109 Expression* expr = *pexpr;
110 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
112 int t = traverse->expression(pexpr);
113 if (t == TRAVERSE_EXIT)
114 return TRAVERSE_EXIT;
115 else if (t == TRAVERSE_SKIP_COMPONENTS)
116 return TRAVERSE_CONTINUE;
118 return expr->do_traverse(traverse);
121 // Traverse subexpressions of this expression.
124 Expression::traverse_subexpressions(Traverse* traverse)
126 return this->do_traverse(traverse);
129 // Default implementation for do_traverse for child classes.
132 Expression::do_traverse(Traverse*)
134 return TRAVERSE_CONTINUE;
137 // This virtual function is called by the parser if the value of this
138 // expression is being discarded. By default, we give an error.
139 // Expressions with side effects override.
142 Expression::do_discarding_value()
144 this->unused_value_error();
147 // This virtual function is called to export expressions. This will
148 // only be used by expressions which may be constant.
151 Expression::do_export(Export*) const
156 // Give an error saying that the value of the expression is not used.
159 Expression::unused_value_error()
161 error_at(this->location(), "value computed is not used");
164 // Note that this expression is an error. This is called by children
165 // when they discover an error.
168 Expression::set_is_error()
170 this->classification_ = EXPRESSION_ERROR;
173 // For children to call to report an error conveniently.
176 Expression::report_error(const char* msg)
178 error_at(this->location_, "%s", msg);
179 this->set_is_error();
182 // Set types of variables and constants. This is implemented by the
186 Expression::determine_type(const Type_context* context)
188 this->do_determine_type(context);
191 // Set types when there is no context.
194 Expression::determine_type_no_context()
196 Type_context context;
197 this->do_determine_type(&context);
200 // Return a tree handling any conversions which must be done during
204 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
205 Type* rhs_type, tree rhs_tree,
208 if (lhs_type == rhs_type)
211 if (lhs_type->is_error() || rhs_type->is_error())
212 return error_mark_node;
214 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
215 return error_mark_node;
217 Gogo* gogo = context->gogo();
219 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
220 if (lhs_type_tree == error_mark_node)
221 return error_mark_node;
223 if (lhs_type->interface_type() != NULL)
225 if (rhs_type->interface_type() == NULL)
226 return Expression::convert_type_to_interface(context, lhs_type,
230 return Expression::convert_interface_to_interface(context, lhs_type,
234 else if (rhs_type->interface_type() != NULL)
235 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
237 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
239 // Assigning nil to an open array.
240 go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
242 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
244 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
245 tree field = TYPE_FIELDS(lhs_type_tree);
246 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
249 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
251 elt = VEC_quick_push(constructor_elt, init, NULL);
252 field = DECL_CHAIN(field);
253 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
256 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
258 elt = VEC_quick_push(constructor_elt, init, NULL);
259 field = DECL_CHAIN(field);
260 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
263 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
265 tree val = build_constructor(lhs_type_tree, init);
266 TREE_CONSTANT(val) = 1;
270 else if (rhs_type->is_nil_type())
272 // The left hand side should be a pointer type at the tree
274 go_assert(POINTER_TYPE_P(lhs_type_tree));
275 return fold_convert(lhs_type_tree, null_pointer_node);
277 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
279 // No conversion is needed.
282 else if (POINTER_TYPE_P(lhs_type_tree)
283 || INTEGRAL_TYPE_P(lhs_type_tree)
284 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
285 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
286 return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree);
287 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
288 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
290 // This conversion must be permitted by Go, or we wouldn't have
292 go_assert(int_size_in_bytes(lhs_type_tree)
293 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
294 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
295 lhs_type_tree, rhs_tree);
299 go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
304 // Return a tree for a conversion from a non-interface type to an
308 Expression::convert_type_to_interface(Translate_context* context,
309 Type* lhs_type, Type* rhs_type,
310 tree rhs_tree, Location location)
312 Gogo* gogo = context->gogo();
313 Interface_type* lhs_interface_type = lhs_type->interface_type();
314 bool lhs_is_empty = lhs_interface_type->is_empty();
316 // Since RHS_TYPE is a static type, we can create the interface
317 // method table at compile time.
319 // When setting an interface to nil, we just set both fields to
321 if (rhs_type->is_nil_type())
323 Btype* lhs_btype = lhs_type->get_backend(gogo);
324 return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
327 // This should have been checked already.
328 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
330 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
331 if (lhs_type_tree == error_mark_node)
332 return error_mark_node;
334 // An interface is a tuple. If LHS_TYPE is an empty interface type,
335 // then the first field is the type descriptor for RHS_TYPE.
336 // Otherwise it is the interface method table for RHS_TYPE.
337 tree first_field_value;
339 first_field_value = rhs_type->type_descriptor_pointer(gogo, location);
342 // Build the interface method table for this interface and this
343 // object type: a list of function pointers for each interface
345 Named_type* rhs_named_type = rhs_type->named_type();
346 bool is_pointer = false;
347 if (rhs_named_type == NULL)
349 rhs_named_type = rhs_type->deref()->named_type();
353 if (rhs_named_type == NULL)
354 method_table = null_pointer_node;
357 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
359 first_field_value = fold_convert_loc(location.gcc_location(),
360 const_ptr_type_node, method_table);
362 if (first_field_value == error_mark_node)
363 return error_mark_node;
365 // Start building a constructor for the value we will return.
367 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
369 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
370 tree field = TYPE_FIELDS(lhs_type_tree);
371 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
372 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
374 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
377 elt = VEC_quick_push(constructor_elt, init, NULL);
378 field = DECL_CHAIN(field);
379 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
382 if (rhs_type->points_to() != NULL)
384 // We are assigning a pointer to the interface; the interface
385 // holds the pointer itself.
386 elt->value = rhs_tree;
387 return build_constructor(lhs_type_tree, init);
390 // We are assigning a non-pointer value to the interface; the
391 // interface gets a copy of the value in the heap.
393 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
395 tree space = gogo->allocate_memory(rhs_type, object_size, location);
396 space = fold_convert_loc(location.gcc_location(),
397 build_pointer_type(TREE_TYPE(rhs_tree)), space);
398 space = save_expr(space);
400 tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space);
401 TREE_THIS_NOTRAP(ref) = 1;
402 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
403 void_type_node, ref, rhs_tree);
405 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
408 return build2(COMPOUND_EXPR, lhs_type_tree, set,
409 build_constructor(lhs_type_tree, init));
412 // Return a tree for the type descriptor of RHS_TREE, which has
413 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
417 Expression::get_interface_type_descriptor(Translate_context*,
418 Type* rhs_type, tree rhs_tree,
421 tree rhs_type_tree = TREE_TYPE(rhs_tree);
422 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
423 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
424 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
426 if (rhs_type->interface_type()->is_empty())
428 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
429 "__type_descriptor") == 0);
433 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
435 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
437 tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v);
438 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
439 tree f = TYPE_FIELDS(TREE_TYPE(v1));
440 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
442 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
444 tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node,
445 v, fold_convert_loc(location.gcc_location(),
448 tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1),
450 return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1),
454 // Return a tree for the conversion of an interface type to an
458 Expression::convert_interface_to_interface(Translate_context* context,
459 Type *lhs_type, Type *rhs_type,
460 tree rhs_tree, bool for_type_guard,
463 Gogo* gogo = context->gogo();
464 Interface_type* lhs_interface_type = lhs_type->interface_type();
465 bool lhs_is_empty = lhs_interface_type->is_empty();
467 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
468 if (lhs_type_tree == error_mark_node)
469 return error_mark_node;
471 // In the general case this requires runtime examination of the type
472 // method table to match it up with the interface methods.
474 // FIXME: If all of the methods in the right hand side interface
475 // also appear in the left hand side interface, then we don't need
476 // to do a runtime check, although we still need to build a new
479 // Get the type descriptor for the right hand side. This will be
480 // NULL for a nil interface.
482 if (!DECL_P(rhs_tree))
483 rhs_tree = save_expr(rhs_tree);
485 tree rhs_type_descriptor =
486 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
489 // The result is going to be a two element constructor.
491 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
493 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
494 tree field = TYPE_FIELDS(lhs_type_tree);
499 // A type assertion fails when converting a nil interface.
500 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
502 static tree assert_interface_decl;
503 tree call = Gogo::call_builtin(&assert_interface_decl,
505 "__go_assert_interface",
508 TREE_TYPE(lhs_type_descriptor),
510 TREE_TYPE(rhs_type_descriptor),
511 rhs_type_descriptor);
512 if (call == error_mark_node)
513 return error_mark_node;
514 // This will panic if the interface conversion fails.
515 TREE_NOTHROW(assert_interface_decl) = 0;
516 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
519 else if (lhs_is_empty)
521 // A convertion to an empty interface always succeeds, and the
522 // first field is just the type descriptor of the object.
523 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
524 "__type_descriptor") == 0);
525 go_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
526 elt->value = rhs_type_descriptor;
530 // A conversion to a non-empty interface may fail, but unlike a
531 // type assertion converting nil will always succeed.
532 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
534 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
536 static tree convert_interface_decl;
537 tree call = Gogo::call_builtin(&convert_interface_decl,
539 "__go_convert_interface",
542 TREE_TYPE(lhs_type_descriptor),
544 TREE_TYPE(rhs_type_descriptor),
545 rhs_type_descriptor);
546 if (call == error_mark_node)
547 return error_mark_node;
548 // This will panic if the interface conversion fails.
549 TREE_NOTHROW(convert_interface_decl) = 0;
550 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
554 // The second field is simply the object pointer.
556 elt = VEC_quick_push(constructor_elt, init, NULL);
557 field = DECL_CHAIN(field);
558 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
561 tree rhs_type_tree = TREE_TYPE(rhs_tree);
562 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
563 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
564 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
565 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
568 return build_constructor(lhs_type_tree, init);
571 // Return a tree for the conversion of an interface type to a
572 // non-interface type.
575 Expression::convert_interface_to_type(Translate_context* context,
576 Type *lhs_type, Type* rhs_type,
577 tree rhs_tree, Location location)
579 Gogo* gogo = context->gogo();
580 tree rhs_type_tree = TREE_TYPE(rhs_tree);
582 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
583 if (lhs_type_tree == error_mark_node)
584 return error_mark_node;
586 // Call a function to check that the type is valid. The function
587 // will panic with an appropriate runtime type error if the type is
590 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location);
592 if (!DECL_P(rhs_tree))
593 rhs_tree = save_expr(rhs_tree);
595 tree rhs_type_descriptor =
596 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
599 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo,
602 static tree check_interface_type_decl;
603 tree call = Gogo::call_builtin(&check_interface_type_decl,
605 "__go_check_interface_type",
608 TREE_TYPE(lhs_type_descriptor),
610 TREE_TYPE(rhs_type_descriptor),
612 TREE_TYPE(rhs_inter_descriptor),
613 rhs_inter_descriptor);
614 if (call == error_mark_node)
615 return error_mark_node;
616 // This call will panic if the conversion is invalid.
617 TREE_NOTHROW(check_interface_type_decl) = 0;
619 // If the call succeeds, pull out the value.
620 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
621 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
622 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
623 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
626 // If the value is a pointer, then it is the value we want.
627 // Otherwise it points to the value.
628 if (lhs_type->points_to() == NULL)
630 val = fold_convert_loc(location.gcc_location(),
631 build_pointer_type(lhs_type_tree), val);
632 val = build_fold_indirect_ref_loc(location.gcc_location(), val);
635 return build2(COMPOUND_EXPR, lhs_type_tree, call,
636 fold_convert_loc(location.gcc_location(), lhs_type_tree, val));
639 // Convert an expression to a tree. This is implemented by the child
640 // class. Not that it is not in general safe to call this multiple
641 // times for a single expression, but that we don't catch such errors.
644 Expression::get_tree(Translate_context* context)
646 // The child may have marked this expression as having an error.
647 if (this->classification_ == EXPRESSION_ERROR)
648 return error_mark_node;
650 return this->do_get_tree(context);
653 // Return a tree for VAL in TYPE.
656 Expression::integer_constant_tree(mpz_t val, tree type)
658 if (type == error_mark_node)
659 return error_mark_node;
660 else if (TREE_CODE(type) == INTEGER_TYPE)
661 return double_int_to_tree(type,
662 mpz_get_double_int(type, val, true));
663 else if (TREE_CODE(type) == REAL_TYPE)
666 mpfr_init_set_z(fval, val, GMP_RNDN);
667 tree ret = Expression::float_constant_tree(fval, type);
671 else if (TREE_CODE(type) == COMPLEX_TYPE)
674 mpfr_init_set_z(fval, val, GMP_RNDN);
675 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
677 tree imag = build_real_from_int_cst(TREE_TYPE(type),
679 return build_complex(type, real, imag);
685 // Return a tree for VAL in TYPE.
688 Expression::float_constant_tree(mpfr_t val, tree type)
690 if (type == error_mark_node)
691 return error_mark_node;
692 else if (TREE_CODE(type) == INTEGER_TYPE)
696 mpfr_get_z(ival, val, GMP_RNDN);
697 tree ret = Expression::integer_constant_tree(ival, type);
701 else if (TREE_CODE(type) == REAL_TYPE)
704 real_from_mpfr(&r1, val, type, GMP_RNDN);
706 real_convert(&r2, TYPE_MODE(type), &r1);
707 return build_real(type, r2);
709 else if (TREE_CODE(type) == COMPLEX_TYPE)
712 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
714 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
715 tree imag = build_real_from_int_cst(TREE_TYPE(type),
717 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
723 // Return a tree for REAL/IMAG in TYPE.
726 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
728 if (type == error_mark_node)
729 return error_mark_node;
730 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
731 return Expression::float_constant_tree(real, type);
732 else if (TREE_CODE(type) == COMPLEX_TYPE)
735 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
737 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
740 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
742 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
744 return build_complex(type, build_real(TREE_TYPE(type), r2),
745 build_real(TREE_TYPE(type), r4));
751 // Return a tree which evaluates to true if VAL, of arbitrary integer
752 // type, is negative or is more than the maximum value of BOUND_TYPE.
753 // If SOFAR is not NULL, it is or'red into the result. The return
754 // value may be NULL if SOFAR is NULL.
757 Expression::check_bounds(tree val, tree bound_type, tree sofar,
760 tree val_type = TREE_TYPE(val);
761 tree ret = NULL_TREE;
763 if (!TYPE_UNSIGNED(val_type))
765 ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val,
766 build_int_cst(val_type, 0));
767 if (ret == boolean_false_node)
771 HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
772 HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
773 go_assert(val_type_size != -1 && bound_type_size != -1);
774 if (val_type_size > bound_type_size
775 || (val_type_size == bound_type_size
776 && TYPE_UNSIGNED(val_type)
777 && !TYPE_UNSIGNED(bound_type)))
779 tree max = TYPE_MAX_VALUE(bound_type);
780 tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node,
781 val, fold_convert_loc(loc.gcc_location(),
783 if (big == boolean_false_node)
785 else if (ret == NULL_TREE)
788 ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
789 boolean_type_node, ret, big);
792 if (ret == NULL_TREE)
794 else if (sofar == NULL_TREE)
797 return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node,
802 Expression::dump_expression(Ast_dump_context* ast_dump_context) const
804 this->do_dump_expression(ast_dump_context);
807 // Error expressions. This are used to avoid cascading errors.
809 class Error_expression : public Expression
812 Error_expression(Location location)
813 : Expression(EXPRESSION_ERROR, location)
818 do_is_constant() const
822 do_integer_constant_value(bool, mpz_t val, Type**) const
829 do_float_constant_value(mpfr_t val, Type**) const
831 mpfr_set_ui(val, 0, GMP_RNDN);
836 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
838 mpfr_set_ui(real, 0, GMP_RNDN);
839 mpfr_set_ui(imag, 0, GMP_RNDN);
844 do_discarding_value()
849 { return Type::make_error_type(); }
852 do_determine_type(const Type_context*)
860 do_is_addressable() const
864 do_get_tree(Translate_context*)
865 { return error_mark_node; }
868 do_dump_expression(Ast_dump_context*) const;
871 // Dump the ast representation for an error expression to a dump context.
874 Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
876 ast_dump_context->ostream() << "_Error_" ;
880 Expression::make_error(Location location)
882 return new Error_expression(location);
885 // An expression which is really a type. This is used during parsing.
886 // It is an error if these survive after lowering.
889 Type_expression : public Expression
892 Type_expression(Type* type, Location location)
893 : Expression(EXPRESSION_TYPE, location),
899 do_traverse(Traverse* traverse)
900 { return Type::traverse(this->type_, traverse); }
904 { return this->type_; }
907 do_determine_type(const Type_context*)
911 do_check_types(Gogo*)
912 { this->report_error(_("invalid use of type")); }
919 do_get_tree(Translate_context*)
920 { go_unreachable(); }
922 void do_dump_expression(Ast_dump_context*) const;
925 // The type which we are representing as an expression.
930 Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
932 ast_dump_context->dump_type(this->type_);
936 Expression::make_type(Type* type, Location location)
938 return new Type_expression(type, location);
941 // Class Parser_expression.
944 Parser_expression::do_type()
946 // We should never really ask for the type of a Parser_expression.
947 // However, it can happen, at least when we have an invalid const
948 // whose initializer refers to the const itself. In that case we
949 // may ask for the type when lowering the const itself.
950 go_assert(saw_errors());
951 return Type::make_error_type();
954 // Class Var_expression.
956 // Lower a variable expression. Here we just make sure that the
957 // initialization expression of the variable has been lowered. This
958 // ensures that we will be able to determine the type of the variable
962 Var_expression::do_lower(Gogo* gogo, Named_object* function,
963 Statement_inserter* inserter, int)
965 if (this->variable_->is_variable())
967 Variable* var = this->variable_->var_value();
968 // This is either a local variable or a global variable. A
969 // reference to a variable which is local to an enclosing
970 // function will be a reference to a field in a closure.
971 if (var->is_global())
976 var->lower_init_expression(gogo, function, inserter);
981 // Return the type of a reference to a variable.
984 Var_expression::do_type()
986 if (this->variable_->is_variable())
987 return this->variable_->var_value()->type();
988 else if (this->variable_->is_result_variable())
989 return this->variable_->result_var_value()->type();
994 // Determine the type of a reference to a variable.
997 Var_expression::do_determine_type(const Type_context*)
999 if (this->variable_->is_variable())
1000 this->variable_->var_value()->determine_type();
1003 // Something takes the address of this variable. This means that we
1004 // may want to move the variable onto the heap.
1007 Var_expression::do_address_taken(bool escapes)
1011 if (this->variable_->is_variable())
1012 this->variable_->var_value()->set_non_escaping_address_taken();
1013 else if (this->variable_->is_result_variable())
1014 this->variable_->result_var_value()->set_non_escaping_address_taken();
1020 if (this->variable_->is_variable())
1021 this->variable_->var_value()->set_address_taken();
1022 else if (this->variable_->is_result_variable())
1023 this->variable_->result_var_value()->set_address_taken();
1029 // Get the tree for a reference to a variable.
1032 Var_expression::do_get_tree(Translate_context* context)
1034 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
1035 context->function());
1036 tree ret = var_to_tree(bvar);
1037 if (ret == error_mark_node)
1038 return error_mark_node;
1040 if (this->variable_->is_variable())
1041 is_in_heap = this->variable_->var_value()->is_in_heap();
1042 else if (this->variable_->is_result_variable())
1043 is_in_heap = this->variable_->result_var_value()->is_in_heap();
1048 ret = build_fold_indirect_ref_loc(this->location().gcc_location(), ret);
1049 TREE_THIS_NOTRAP(ret) = 1;
1054 // Ast dump for variable expression.
1057 Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1059 ast_dump_context->ostream() << this->variable_->name() ;
1062 // Make a reference to a variable in an expression.
1065 Expression::make_var_reference(Named_object* var, Location location)
1068 return Expression::make_sink(location);
1070 // FIXME: Creating a new object for each reference to a variable is
1072 return new Var_expression(var, location);
1075 // Class Temporary_reference_expression.
1080 Temporary_reference_expression::do_type()
1082 return this->statement_->type();
1085 // Called if something takes the address of this temporary variable.
1086 // We never have to move temporary variables to the heap, but we do
1087 // need to know that they must live in the stack rather than in a
1091 Temporary_reference_expression::do_address_taken(bool)
1093 this->statement_->set_is_address_taken();
1096 // Get a tree referring to the variable.
1099 Temporary_reference_expression::do_get_tree(Translate_context* context)
1101 Bvariable* bvar = this->statement_->get_backend_variable(context);
1103 // The gcc backend can't represent the same set of recursive types
1104 // that the Go frontend can. In some cases this means that a
1105 // temporary variable won't have the right backend type. Correct
1106 // that here by adding a type cast. We need to use base() to push
1107 // the circularity down one level.
1108 tree ret = var_to_tree(bvar);
1109 if (!this->is_lvalue_
1110 && POINTER_TYPE_P(TREE_TYPE(ret))
1111 && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
1113 Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1114 tree type_tree = type_to_tree(type_btype);
1115 ret = fold_convert_loc(this->location().gcc_location(), type_tree, ret);
1120 // Ast dump for temporary reference.
1123 Temporary_reference_expression::do_dump_expression(
1124 Ast_dump_context* ast_dump_context) const
1126 ast_dump_context->dump_temp_variable_name(this->statement_);
1129 // Make a reference to a temporary variable.
1131 Temporary_reference_expression*
1132 Expression::make_temporary_reference(Temporary_statement* statement,
1135 return new Temporary_reference_expression(statement, location);
1138 // Class Set_and_use_temporary_expression.
1143 Set_and_use_temporary_expression::do_type()
1145 return this->statement_->type();
1148 // Take the address.
1151 Set_and_use_temporary_expression::do_address_taken(bool)
1153 this->statement_->set_is_address_taken();
1156 // Return the backend representation.
1159 Set_and_use_temporary_expression::do_get_tree(Translate_context* context)
1161 Bvariable* bvar = this->statement_->get_backend_variable(context);
1162 tree var_tree = var_to_tree(bvar);
1163 tree expr_tree = this->expr_->get_tree(context);
1164 if (var_tree == error_mark_node || expr_tree == error_mark_node)
1165 return error_mark_node;
1166 Location loc = this->location();
1167 return build2_loc(loc.gcc_location(), COMPOUND_EXPR, TREE_TYPE(var_tree),
1168 build2_loc(loc.gcc_location(), MODIFY_EXPR, void_type_node,
1169 var_tree, expr_tree),
1176 Set_and_use_temporary_expression::do_dump_expression(
1177 Ast_dump_context* ast_dump_context) const
1179 ast_dump_context->ostream() << '(';
1180 ast_dump_context->dump_temp_variable_name(this->statement_);
1181 ast_dump_context->ostream() << " = ";
1182 this->expr_->dump_expression(ast_dump_context);
1183 ast_dump_context->ostream() << ')';
1186 // Make a set-and-use temporary.
1188 Set_and_use_temporary_expression*
1189 Expression::make_set_and_use_temporary(Temporary_statement* statement,
1190 Expression* expr, Location location)
1192 return new Set_and_use_temporary_expression(statement, expr, location);
1195 // A sink expression--a use of the blank identifier _.
1197 class Sink_expression : public Expression
1200 Sink_expression(Location location)
1201 : Expression(EXPRESSION_SINK, location),
1202 type_(NULL), var_(NULL_TREE)
1207 do_discarding_value()
1214 do_determine_type(const Type_context*);
1218 { return new Sink_expression(this->location()); }
1221 do_get_tree(Translate_context*);
1224 do_dump_expression(Ast_dump_context*) const;
1227 // The type of this sink variable.
1229 // The temporary variable we generate.
1233 // Return the type of a sink expression.
1236 Sink_expression::do_type()
1238 if (this->type_ == NULL)
1239 return Type::make_sink_type();
1243 // Determine the type of a sink expression.
1246 Sink_expression::do_determine_type(const Type_context* context)
1248 if (context->type != NULL)
1249 this->type_ = context->type;
1252 // Return a temporary variable for a sink expression. This will
1253 // presumably be a write-only variable which the middle-end will drop.
1256 Sink_expression::do_get_tree(Translate_context* context)
1258 if (this->var_ == NULL_TREE)
1260 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
1261 Btype* bt = this->type_->get_backend(context->gogo());
1262 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
1267 // Ast dump for sink expression.
1270 Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1272 ast_dump_context->ostream() << "_" ;
1275 // Make a sink expression.
1278 Expression::make_sink(Location location)
1280 return new Sink_expression(location);
1283 // Class Func_expression.
1285 // FIXME: Can a function expression appear in a constant expression?
1286 // The value is unchanging. Initializing a constant to the address of
1287 // a function seems like it could work, though there might be little
1293 Func_expression::do_traverse(Traverse* traverse)
1295 return (this->closure_ == NULL
1297 : Expression::traverse(&this->closure_, traverse));
1300 // Return the type of a function expression.
1303 Func_expression::do_type()
1305 if (this->function_->is_function())
1306 return this->function_->func_value()->type();
1307 else if (this->function_->is_function_declaration())
1308 return this->function_->func_declaration_value()->type();
1313 // Get the tree for a function expression without evaluating the
1317 Func_expression::get_tree_without_closure(Gogo* gogo)
1319 Function_type* fntype;
1320 if (this->function_->is_function())
1321 fntype = this->function_->func_value()->type();
1322 else if (this->function_->is_function_declaration())
1323 fntype = this->function_->func_declaration_value()->type();
1327 // Builtin functions are handled specially by Call_expression. We
1328 // can't take their address.
1329 if (fntype->is_builtin())
1331 error_at(this->location(),
1332 "invalid use of special builtin function %qs; must be called",
1333 this->function_->name().c_str());
1334 return error_mark_node;
1337 Named_object* no = this->function_;
1339 tree id = no->get_id(gogo);
1340 if (id == error_mark_node)
1341 return error_mark_node;
1344 if (no->is_function())
1345 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1346 else if (no->is_function_declaration())
1347 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1351 if (fndecl == error_mark_node)
1352 return error_mark_node;
1354 return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl);
1357 // Get the tree for a function expression. This is used when we take
1358 // the address of a function rather than simply calling it. If the
1359 // function has a closure, we must use a trampoline.
1362 Func_expression::do_get_tree(Translate_context* context)
1364 Gogo* gogo = context->gogo();
1366 tree fnaddr = this->get_tree_without_closure(gogo);
1367 if (fnaddr == error_mark_node)
1368 return error_mark_node;
1370 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1371 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1372 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1374 // For a normal non-nested function call, that is all we have to do.
1375 if (!this->function_->is_function()
1376 || this->function_->func_value()->enclosing() == NULL)
1378 go_assert(this->closure_ == NULL);
1382 // For a nested function call, we have to always allocate a
1383 // trampoline. If we don't always allocate, then closures will not
1384 // be reliably distinct.
1385 Expression* closure = this->closure_;
1387 if (closure == NULL)
1388 closure_tree = null_pointer_node;
1391 // Get the value of the closure. This will be a pointer to
1392 // space allocated on the heap.
1393 closure_tree = closure->get_tree(context);
1394 if (closure_tree == error_mark_node)
1395 return error_mark_node;
1396 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1399 // Now we need to build some code on the heap. This code will load
1400 // the static chain pointer with the closure and then jump to the
1401 // body of the function. The normal gcc approach is to build the
1402 // code on the stack. Unfortunately we can not do that, as Go
1403 // permits us to return the function pointer.
1405 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1408 // Ast dump for function.
1411 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1413 ast_dump_context->ostream() << this->function_->name();
1414 if (this->closure_ != NULL)
1416 ast_dump_context->ostream() << " {closure = ";
1417 this->closure_->dump_expression(ast_dump_context);
1418 ast_dump_context->ostream() << "}";
1422 // Make a reference to a function in an expression.
1425 Expression::make_func_reference(Named_object* function, Expression* closure,
1428 return new Func_expression(function, closure, location);
1431 // Class Unknown_expression.
1433 // Return the name of an unknown expression.
1436 Unknown_expression::name() const
1438 return this->named_object_->name();
1441 // Lower a reference to an unknown name.
1444 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1446 Location location = this->location();
1447 Named_object* no = this->named_object_;
1449 if (!no->is_unknown())
1453 real = no->unknown_value()->real_named_object();
1456 if (this->is_composite_literal_key_)
1458 if (!this->no_error_message_)
1459 error_at(location, "reference to undefined name %qs",
1460 this->named_object_->message_name().c_str());
1461 return Expression::make_error(location);
1464 switch (real->classification())
1466 case Named_object::NAMED_OBJECT_CONST:
1467 return Expression::make_const_reference(real, location);
1468 case Named_object::NAMED_OBJECT_TYPE:
1469 return Expression::make_type(real->type_value(), location);
1470 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1471 if (this->is_composite_literal_key_)
1473 if (!this->no_error_message_)
1474 error_at(location, "reference to undefined type %qs",
1475 real->message_name().c_str());
1476 return Expression::make_error(location);
1477 case Named_object::NAMED_OBJECT_VAR:
1478 real->var_value()->set_is_used();
1479 return Expression::make_var_reference(real, location);
1480 case Named_object::NAMED_OBJECT_FUNC:
1481 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1482 return Expression::make_func_reference(real, NULL, location);
1483 case Named_object::NAMED_OBJECT_PACKAGE:
1484 if (this->is_composite_literal_key_)
1486 if (!this->no_error_message_)
1487 error_at(location, "unexpected reference to package");
1488 return Expression::make_error(location);
1494 // Dump the ast representation for an unknown expression to a dump context.
1497 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1499 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1503 // Make a reference to an unknown name.
1506 Expression::make_unknown_reference(Named_object* no, Location location)
1508 return new Unknown_expression(no, location);
1511 // A boolean expression.
1513 class Boolean_expression : public Expression
1516 Boolean_expression(bool val, Location location)
1517 : Expression(EXPRESSION_BOOLEAN, location),
1518 val_(val), type_(NULL)
1526 do_is_constant() const
1533 do_determine_type(const Type_context*);
1540 do_get_tree(Translate_context*)
1541 { return this->val_ ? boolean_true_node : boolean_false_node; }
1544 do_export(Export* exp) const
1545 { exp->write_c_string(this->val_ ? "true" : "false"); }
1548 do_dump_expression(Ast_dump_context* ast_dump_context) const
1549 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1554 // The type as determined by context.
1561 Boolean_expression::do_type()
1563 if (this->type_ == NULL)
1564 this->type_ = Type::make_boolean_type();
1568 // Set the type from the context.
1571 Boolean_expression::do_determine_type(const Type_context* context)
1573 if (this->type_ != NULL && !this->type_->is_abstract())
1575 else if (context->type != NULL && context->type->is_boolean_type())
1576 this->type_ = context->type;
1577 else if (!context->may_be_abstract)
1578 this->type_ = Type::lookup_bool_type();
1581 // Import a boolean constant.
1584 Boolean_expression::do_import(Import* imp)
1586 if (imp->peek_char() == 't')
1588 imp->require_c_string("true");
1589 return Expression::make_boolean(true, imp->location());
1593 imp->require_c_string("false");
1594 return Expression::make_boolean(false, imp->location());
1598 // Make a boolean expression.
1601 Expression::make_boolean(bool val, Location location)
1603 return new Boolean_expression(val, location);
1606 // Class String_expression.
1611 String_expression::do_type()
1613 if (this->type_ == NULL)
1614 this->type_ = Type::make_string_type();
1618 // Set the type from the context.
1621 String_expression::do_determine_type(const Type_context* context)
1623 if (this->type_ != NULL && !this->type_->is_abstract())
1625 else if (context->type != NULL && context->type->is_string_type())
1626 this->type_ = context->type;
1627 else if (!context->may_be_abstract)
1628 this->type_ = Type::lookup_string_type();
1631 // Build a string constant.
1634 String_expression::do_get_tree(Translate_context* context)
1636 return context->gogo()->go_string_constant_tree(this->val_);
1639 // Write string literal to string dump.
1642 String_expression::export_string(String_dump* exp,
1643 const String_expression* str)
1646 s.reserve(str->val_.length() * 4 + 2);
1648 for (std::string::const_iterator p = str->val_.begin();
1649 p != str->val_.end();
1652 if (*p == '\\' || *p == '"')
1657 else if (*p >= 0x20 && *p < 0x7f)
1659 else if (*p == '\n')
1661 else if (*p == '\t')
1666 unsigned char c = *p;
1667 unsigned int dig = c >> 4;
1668 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1670 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1674 exp->write_string(s);
1677 // Export a string expression.
1680 String_expression::do_export(Export* exp) const
1682 String_expression::export_string(exp, this);
1685 // Import a string expression.
1688 String_expression::do_import(Import* imp)
1690 imp->require_c_string("\"");
1694 int c = imp->get_char();
1695 if (c == '"' || c == -1)
1698 val += static_cast<char>(c);
1701 c = imp->get_char();
1702 if (c == '\\' || c == '"')
1703 val += static_cast<char>(c);
1710 c = imp->get_char();
1711 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1712 c = imp->get_char();
1713 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1714 char v = (vh << 4) | vl;
1719 error_at(imp->location(), "bad string constant");
1720 return Expression::make_error(imp->location());
1724 return Expression::make_string(val, imp->location());
1727 // Ast dump for string expression.
1730 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1732 String_expression::export_string(ast_dump_context, this);
1735 // Make a string expression.
1738 Expression::make_string(const std::string& val, Location location)
1740 return new String_expression(val, location);
1743 // Make an integer expression.
1745 class Integer_expression : public Expression
1748 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
1750 : Expression(EXPRESSION_INTEGER, location),
1751 type_(type), is_character_constant_(is_character_constant)
1752 { mpz_init_set(this->val_, *val); }
1757 // Return whether VAL fits in the type.
1759 check_constant(mpz_t val, Type*, Location);
1761 // Write VAL to string dump.
1763 export_integer(String_dump* exp, const mpz_t val);
1765 // Write VAL to dump context.
1767 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1771 do_is_constant() const
1775 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1781 do_determine_type(const Type_context* context);
1784 do_check_types(Gogo*);
1787 do_get_tree(Translate_context*);
1792 if (this->is_character_constant_)
1793 return Expression::make_character(&this->val_, this->type_,
1796 return Expression::make_integer(&this->val_, this->type_,
1801 do_export(Export*) const;
1804 do_dump_expression(Ast_dump_context*) const;
1807 // The integer value.
1811 // Whether this is a character constant.
1812 bool is_character_constant_;
1815 // Return an integer constant value.
1818 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1821 if (this->type_ != NULL)
1822 *ptype = this->type_;
1823 mpz_set(val, this->val_);
1827 // Return the current type. If we haven't set the type yet, we return
1828 // an abstract integer type.
1831 Integer_expression::do_type()
1833 if (this->type_ == NULL)
1835 if (this->is_character_constant_)
1836 this->type_ = Type::make_abstract_character_type();
1838 this->type_ = Type::make_abstract_integer_type();
1843 // Set the type of the integer value. Here we may switch from an
1844 // abstract type to a real type.
1847 Integer_expression::do_determine_type(const Type_context* context)
1849 if (this->type_ != NULL && !this->type_->is_abstract())
1851 else if (context->type != NULL
1852 && (context->type->integer_type() != NULL
1853 || context->type->float_type() != NULL
1854 || context->type->complex_type() != NULL))
1855 this->type_ = context->type;
1856 else if (!context->may_be_abstract)
1858 if (this->is_character_constant_)
1859 this->type_ = Type::lookup_integer_type("int32");
1861 this->type_ = Type::lookup_integer_type("int");
1865 // Return true if the integer VAL fits in the range of the type TYPE.
1866 // Otherwise give an error and return false. TYPE may be NULL.
1869 Integer_expression::check_constant(mpz_t val, Type* type,
1874 Integer_type* itype = type->integer_type();
1875 if (itype == NULL || itype->is_abstract())
1878 int bits = mpz_sizeinbase(val, 2);
1880 if (itype->is_unsigned())
1882 // For an unsigned type we can only accept a nonnegative number,
1883 // and we must be able to represent at least BITS.
1884 if (mpz_sgn(val) >= 0
1885 && bits <= itype->bits())
1890 // For a signed type we need an extra bit to indicate the sign.
1891 // We have to handle the most negative integer specially.
1892 if (bits + 1 <= itype->bits()
1893 || (bits <= itype->bits()
1895 && (mpz_scan1(val, 0)
1896 == static_cast<unsigned long>(itype->bits() - 1))
1897 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1901 error_at(location, "integer constant overflow");
1905 // Check the type of an integer constant.
1908 Integer_expression::do_check_types(Gogo*)
1910 if (this->type_ == NULL)
1912 if (!Integer_expression::check_constant(this->val_, this->type_,
1914 this->set_is_error();
1917 // Get a tree for an integer constant.
1920 Integer_expression::do_get_tree(Translate_context* context)
1922 Gogo* gogo = context->gogo();
1924 if (this->type_ != NULL && !this->type_->is_abstract())
1925 type = type_to_tree(this->type_->get_backend(gogo));
1926 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1928 // We are converting to an abstract floating point type.
1929 Type* ftype = Type::lookup_float_type("float64");
1930 type = type_to_tree(ftype->get_backend(gogo));
1932 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1934 // We are converting to an abstract complex type.
1935 Type* ctype = Type::lookup_complex_type("complex128");
1936 type = type_to_tree(ctype->get_backend(gogo));
1940 // If we still have an abstract type here, then this is being
1941 // used in a constant expression which didn't get reduced for
1942 // some reason. Use a type which will fit the value. We use <,
1943 // not <=, because we need an extra bit for the sign bit.
1944 int bits = mpz_sizeinbase(this->val_, 2);
1945 if (bits < INT_TYPE_SIZE)
1947 Type* t = Type::lookup_integer_type("int");
1948 type = type_to_tree(t->get_backend(gogo));
1952 Type* t = Type::lookup_integer_type("int64");
1953 type = type_to_tree(t->get_backend(gogo));
1956 type = long_long_integer_type_node;
1958 return Expression::integer_constant_tree(this->val_, type);
1961 // Write VAL to export data.
1964 Integer_expression::export_integer(String_dump* exp, const mpz_t val)
1966 char* s = mpz_get_str(NULL, 10, val);
1967 exp->write_c_string(s);
1971 // Export an integer in a constant expression.
1974 Integer_expression::do_export(Export* exp) const
1976 Integer_expression::export_integer(exp, this->val_);
1977 if (this->is_character_constant_)
1978 exp->write_c_string("'");
1979 // A trailing space lets us reliably identify the end of the number.
1980 exp->write_c_string(" ");
1983 // Import an integer, floating point, or complex value. This handles
1984 // all these types because they all start with digits.
1987 Integer_expression::do_import(Import* imp)
1989 std::string num = imp->read_identifier();
1990 imp->require_c_string(" ");
1991 if (!num.empty() && num[num.length() - 1] == 'i')
1994 size_t plus_pos = num.find('+', 1);
1995 size_t minus_pos = num.find('-', 1);
1997 if (plus_pos == std::string::npos)
1999 else if (minus_pos == std::string::npos)
2003 error_at(imp->location(), "bad number in import data: %qs",
2005 return Expression::make_error(imp->location());
2007 if (pos == std::string::npos)
2008 mpfr_set_ui(real, 0, GMP_RNDN);
2011 std::string real_str = num.substr(0, pos);
2012 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
2014 error_at(imp->location(), "bad number in import data: %qs",
2016 return Expression::make_error(imp->location());
2020 std::string imag_str;
2021 if (pos == std::string::npos)
2024 imag_str = num.substr(pos);
2025 imag_str = imag_str.substr(0, imag_str.size() - 1);
2027 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
2029 error_at(imp->location(), "bad number in import data: %qs",
2031 return Expression::make_error(imp->location());
2033 Expression* ret = Expression::make_complex(&real, &imag, NULL,
2039 else if (num.find('.') == std::string::npos
2040 && num.find('E') == std::string::npos)
2042 bool is_character_constant = (!num.empty()
2043 && num[num.length() - 1] == '\'');
2044 if (is_character_constant)
2045 num = num.substr(0, num.length() - 1);
2047 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
2049 error_at(imp->location(), "bad number in import data: %qs",
2051 return Expression::make_error(imp->location());
2054 if (is_character_constant)
2055 ret = Expression::make_character(&val, NULL, imp->location());
2057 ret = Expression::make_integer(&val, NULL, imp->location());
2064 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
2066 error_at(imp->location(), "bad number in import data: %qs",
2068 return Expression::make_error(imp->location());
2070 Expression* ret = Expression::make_float(&val, NULL, imp->location());
2075 // Ast dump for integer expression.
2078 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2080 if (this->is_character_constant_)
2081 ast_dump_context->ostream() << '\'';
2082 Integer_expression::export_integer(ast_dump_context, this->val_);
2083 if (this->is_character_constant_)
2084 ast_dump_context->ostream() << '\'';
2087 // Build a new integer value.
2090 Expression::make_integer(const mpz_t* val, Type* type, Location location)
2092 return new Integer_expression(val, type, false, location);
2095 // Build a new character constant value.
2098 Expression::make_character(const mpz_t* val, Type* type, Location location)
2100 return new Integer_expression(val, type, true, location);
2105 class Float_expression : public Expression
2108 Float_expression(const mpfr_t* val, Type* type, Location location)
2109 : Expression(EXPRESSION_FLOAT, location),
2112 mpfr_init_set(this->val_, *val, GMP_RNDN);
2115 // Constrain VAL to fit into TYPE.
2117 constrain_float(mpfr_t val, Type* type);
2119 // Return whether VAL fits in the type.
2121 check_constant(mpfr_t val, Type*, Location);
2123 // Write VAL to export data.
2125 export_float(String_dump* exp, const mpfr_t val);
2127 // Write VAL to dump file.
2129 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
2133 do_is_constant() const
2137 do_float_constant_value(mpfr_t val, Type**) const;
2143 do_determine_type(const Type_context*);
2146 do_check_types(Gogo*);
2150 { return Expression::make_float(&this->val_, this->type_,
2151 this->location()); }
2154 do_get_tree(Translate_context*);
2157 do_export(Export*) const;
2160 do_dump_expression(Ast_dump_context*) const;
2163 // The floating point value.
2169 // Constrain VAL to fit into TYPE.
2172 Float_expression::constrain_float(mpfr_t val, Type* type)
2174 Float_type* ftype = type->float_type();
2175 if (ftype != NULL && !ftype->is_abstract())
2176 mpfr_prec_round(val, ftype->bits(), GMP_RNDN);
2179 // Return a floating point constant value.
2182 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2184 if (this->type_ != NULL)
2185 *ptype = this->type_;
2186 mpfr_set(val, this->val_, GMP_RNDN);
2190 // Return the current type. If we haven't set the type yet, we return
2191 // an abstract float type.
2194 Float_expression::do_type()
2196 if (this->type_ == NULL)
2197 this->type_ = Type::make_abstract_float_type();
2201 // Set the type of the float value. Here we may switch from an
2202 // abstract type to a real type.
2205 Float_expression::do_determine_type(const Type_context* context)
2207 if (this->type_ != NULL && !this->type_->is_abstract())
2209 else if (context->type != NULL
2210 && (context->type->integer_type() != NULL
2211 || context->type->float_type() != NULL
2212 || context->type->complex_type() != NULL))
2213 this->type_ = context->type;
2214 else if (!context->may_be_abstract)
2215 this->type_ = Type::lookup_float_type("float64");
2218 // Return true if the floating point value VAL fits in the range of
2219 // the type TYPE. Otherwise give an error and return false. TYPE may
2223 Float_expression::check_constant(mpfr_t val, Type* type,
2228 Float_type* ftype = type->float_type();
2229 if (ftype == NULL || ftype->is_abstract())
2232 // A NaN or Infinity always fits in the range of the type.
2233 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
2236 mp_exp_t exp = mpfr_get_exp(val);
2238 switch (ftype->bits())
2251 error_at(location, "floating point constant overflow");
2257 // Check the type of a float value.
2260 Float_expression::do_check_types(Gogo*)
2262 if (this->type_ == NULL)
2265 if (!Float_expression::check_constant(this->val_, this->type_,
2267 this->set_is_error();
2269 Integer_type* integer_type = this->type_->integer_type();
2270 if (integer_type != NULL)
2272 if (!mpfr_integer_p(this->val_))
2273 this->report_error(_("floating point constant truncated to integer"));
2276 go_assert(!integer_type->is_abstract());
2279 mpfr_get_z(ival, this->val_, GMP_RNDN);
2280 Integer_expression::check_constant(ival, integer_type,
2287 // Get a tree for a float constant.
2290 Float_expression::do_get_tree(Translate_context* context)
2292 Gogo* gogo = context->gogo();
2294 if (this->type_ != NULL && !this->type_->is_abstract())
2295 type = type_to_tree(this->type_->get_backend(gogo));
2296 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2298 // We have an abstract integer type. We just hope for the best.
2299 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2303 // If we still have an abstract type here, then this is being
2304 // used in a constant expression which didn't get reduced. We
2305 // just use float64 and hope for the best.
2306 Type* ft = Type::lookup_float_type("float64");
2307 type = type_to_tree(ft->get_backend(gogo));
2309 return Expression::float_constant_tree(this->val_, type);
2312 // Write a floating point number to a string dump.
2315 Float_expression::export_float(String_dump *exp, const mpfr_t val)
2318 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2320 exp->write_c_string("-");
2321 exp->write_c_string("0.");
2322 exp->write_c_string(*s == '-' ? s + 1 : s);
2325 snprintf(buf, sizeof buf, "E%ld", exponent);
2326 exp->write_c_string(buf);
2329 // Export a floating point number in a constant expression.
2332 Float_expression::do_export(Export* exp) const
2334 Float_expression::export_float(exp, this->val_);
2335 // A trailing space lets us reliably identify the end of the number.
2336 exp->write_c_string(" ");
2339 // Dump a floating point number to the dump file.
2342 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2344 Float_expression::export_float(ast_dump_context, this->val_);
2347 // Make a float expression.
2350 Expression::make_float(const mpfr_t* val, Type* type, Location location)
2352 return new Float_expression(val, type, location);
2357 class Complex_expression : public Expression
2360 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2362 : Expression(EXPRESSION_COMPLEX, location),
2365 mpfr_init_set(this->real_, *real, GMP_RNDN);
2366 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2369 // Constrain REAL/IMAG to fit into TYPE.
2371 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2373 // Return whether REAL/IMAG fits in the type.
2375 check_constant(mpfr_t real, mpfr_t imag, Type*, Location);
2377 // Write REAL/IMAG to string dump.
2379 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
2381 // Write REAL/IMAG to dump context.
2383 dump_complex(Ast_dump_context* ast_dump_context,
2384 const mpfr_t real, const mpfr_t val);
2388 do_is_constant() const
2392 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2398 do_determine_type(const Type_context*);
2401 do_check_types(Gogo*);
2406 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2411 do_get_tree(Translate_context*);
2414 do_export(Export*) const;
2417 do_dump_expression(Ast_dump_context*) const;
2422 // The imaginary part;
2424 // The type if known.
2428 // Constrain REAL/IMAG to fit into TYPE.
2431 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2433 Complex_type* ctype = type->complex_type();
2434 if (ctype != NULL && !ctype->is_abstract())
2436 mpfr_prec_round(real, ctype->bits() / 2, GMP_RNDN);
2437 mpfr_prec_round(imag, ctype->bits() / 2, GMP_RNDN);
2441 // Return a complex constant value.
2444 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2447 if (this->type_ != NULL)
2448 *ptype = this->type_;
2449 mpfr_set(real, this->real_, GMP_RNDN);
2450 mpfr_set(imag, this->imag_, GMP_RNDN);
2454 // Return the current type. If we haven't set the type yet, we return
2455 // an abstract complex type.
2458 Complex_expression::do_type()
2460 if (this->type_ == NULL)
2461 this->type_ = Type::make_abstract_complex_type();
2465 // Set the type of the complex value. Here we may switch from an
2466 // abstract type to a real type.
2469 Complex_expression::do_determine_type(const Type_context* context)
2471 if (this->type_ != NULL && !this->type_->is_abstract())
2473 else if (context->type != NULL
2474 && context->type->complex_type() != NULL)
2475 this->type_ = context->type;
2476 else if (!context->may_be_abstract)
2477 this->type_ = Type::lookup_complex_type("complex128");
2480 // Return true if the complex value REAL/IMAG fits in the range of the
2481 // type TYPE. Otherwise give an error and return false. TYPE may be
2485 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2490 Complex_type* ctype = type->complex_type();
2491 if (ctype == NULL || ctype->is_abstract())
2495 switch (ctype->bits())
2507 // A NaN or Infinity always fits in the range of the type.
2508 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2510 if (mpfr_get_exp(real) > max_exp)
2512 error_at(location, "complex real part constant overflow");
2517 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2519 if (mpfr_get_exp(imag) > max_exp)
2521 error_at(location, "complex imaginary part constant overflow");
2529 // Check the type of a complex value.
2532 Complex_expression::do_check_types(Gogo*)
2534 if (this->type_ == NULL)
2537 if (!Complex_expression::check_constant(this->real_, this->imag_,
2538 this->type_, this->location()))
2539 this->set_is_error();
2542 // Get a tree for a complex constant.
2545 Complex_expression::do_get_tree(Translate_context* context)
2547 Gogo* gogo = context->gogo();
2549 if (this->type_ != NULL && !this->type_->is_abstract())
2550 type = type_to_tree(this->type_->get_backend(gogo));
2553 // If we still have an abstract type here, this this is being
2554 // used in a constant expression which didn't get reduced. We
2555 // just use complex128 and hope for the best.
2556 Type* ct = Type::lookup_complex_type("complex128");
2557 type = type_to_tree(ct->get_backend(gogo));
2559 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2562 // Write REAL/IMAG to export data.
2565 Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
2568 if (!mpfr_zero_p(real))
2570 Float_expression::export_float(exp, real);
2571 if (mpfr_sgn(imag) > 0)
2572 exp->write_c_string("+");
2574 Float_expression::export_float(exp, imag);
2575 exp->write_c_string("i");
2578 // Export a complex number in a constant expression.
2581 Complex_expression::do_export(Export* exp) const
2583 Complex_expression::export_complex(exp, this->real_, this->imag_);
2584 // A trailing space lets us reliably identify the end of the number.
2585 exp->write_c_string(" ");
2588 // Dump a complex expression to the dump file.
2591 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2593 Complex_expression::export_complex(ast_dump_context,
2598 // Make a complex expression.
2601 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2604 return new Complex_expression(real, imag, type, location);
2607 // Find a named object in an expression.
2609 class Find_named_object : public Traverse
2612 Find_named_object(Named_object* no)
2613 : Traverse(traverse_expressions),
2614 no_(no), found_(false)
2617 // Whether we found the object.
2620 { return this->found_; }
2624 expression(Expression**);
2627 // The object we are looking for.
2629 // Whether we found it.
2633 // A reference to a const in an expression.
2635 class Const_expression : public Expression
2638 Const_expression(Named_object* constant, Location location)
2639 : Expression(EXPRESSION_CONST_REFERENCE, location),
2640 constant_(constant), type_(NULL), seen_(false)
2645 { return this->constant_; }
2647 // Check that the initializer does not refer to the constant itself.
2649 check_for_init_loop();
2653 do_traverse(Traverse*);
2656 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2659 do_is_constant() const
2663 do_integer_constant_value(bool, mpz_t val, Type**) const;
2666 do_float_constant_value(mpfr_t val, Type**) const;
2669 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2672 do_string_constant_value(std::string* val) const
2673 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2678 // The type of a const is set by the declaration, not the use.
2680 do_determine_type(const Type_context*);
2683 do_check_types(Gogo*);
2690 do_get_tree(Translate_context* context);
2692 // When exporting a reference to a const as part of a const
2693 // expression, we export the value. We ignore the fact that it has
2696 do_export(Export* exp) const
2697 { this->constant_->const_value()->expr()->export_expression(exp); }
2700 do_dump_expression(Ast_dump_context*) const;
2704 Named_object* constant_;
2705 // The type of this reference. This is used if the constant has an
2708 // Used to prevent infinite recursion when a constant incorrectly
2709 // refers to itself.
2716 Const_expression::do_traverse(Traverse* traverse)
2718 if (this->type_ != NULL)
2719 return Type::traverse(this->type_, traverse);
2720 return TRAVERSE_CONTINUE;
2723 // Lower a constant expression. This is where we convert the
2724 // predeclared constant iota into an integer value.
2727 Const_expression::do_lower(Gogo* gogo, Named_object*,
2728 Statement_inserter*, int iota_value)
2730 if (this->constant_->const_value()->expr()->classification()
2733 if (iota_value == -1)
2735 error_at(this->location(),
2736 "iota is only defined in const declarations");
2740 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2741 Expression* ret = Expression::make_integer(&val, NULL,
2747 // Make sure that the constant itself has been lowered.
2748 gogo->lower_constant(this->constant_);
2753 // Return an integer constant value.
2756 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2763 if (this->type_ != NULL)
2764 ctype = this->type_;
2766 ctype = this->constant_->const_value()->type();
2767 if (ctype != NULL && ctype->integer_type() == NULL)
2770 Expression* e = this->constant_->const_value()->expr();
2775 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2777 this->seen_ = false;
2781 && !Integer_expression::check_constant(val, ctype, this->location()))
2784 *ptype = ctype != NULL ? ctype : t;
2788 // Return a floating point constant value.
2791 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2797 if (this->type_ != NULL)
2798 ctype = this->type_;
2800 ctype = this->constant_->const_value()->type();
2801 if (ctype != NULL && ctype->float_type() == NULL)
2807 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2810 this->seen_ = false;
2812 if (r && ctype != NULL)
2814 if (!Float_expression::check_constant(val, ctype, this->location()))
2816 Float_expression::constrain_float(val, ctype);
2818 *ptype = ctype != NULL ? ctype : t;
2822 // Return a complex constant value.
2825 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2832 if (this->type_ != NULL)
2833 ctype = this->type_;
2835 ctype = this->constant_->const_value()->type();
2836 if (ctype != NULL && ctype->complex_type() == NULL)
2842 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2846 this->seen_ = false;
2848 if (r && ctype != NULL)
2850 if (!Complex_expression::check_constant(real, imag, ctype,
2853 Complex_expression::constrain_complex(real, imag, ctype);
2855 *ptype = ctype != NULL ? ctype : t;
2859 // Return the type of the const reference.
2862 Const_expression::do_type()
2864 if (this->type_ != NULL)
2867 Named_constant* nc = this->constant_->const_value();
2869 if (this->seen_ || nc->lowering())
2871 this->report_error(_("constant refers to itself"));
2872 this->type_ = Type::make_error_type();
2878 Type* ret = nc->type();
2882 this->seen_ = false;
2886 // During parsing, a named constant may have a NULL type, but we
2887 // must not return a NULL type here.
2888 ret = nc->expr()->type();
2890 this->seen_ = false;
2895 // Set the type of the const reference.
2898 Const_expression::do_determine_type(const Type_context* context)
2900 Type* ctype = this->constant_->const_value()->type();
2901 Type* cetype = (ctype != NULL
2903 : this->constant_->const_value()->expr()->type());
2904 if (ctype != NULL && !ctype->is_abstract())
2906 else if (context->type != NULL
2907 && (context->type->integer_type() != NULL
2908 || context->type->float_type() != NULL
2909 || context->type->complex_type() != NULL)
2910 && (cetype->integer_type() != NULL
2911 || cetype->float_type() != NULL
2912 || cetype->complex_type() != NULL))
2913 this->type_ = context->type;
2914 else if (context->type != NULL
2915 && context->type->is_string_type()
2916 && cetype->is_string_type())
2917 this->type_ = context->type;
2918 else if (context->type != NULL
2919 && context->type->is_boolean_type()
2920 && cetype->is_boolean_type())
2921 this->type_ = context->type;
2922 else if (!context->may_be_abstract)
2924 if (cetype->is_abstract())
2925 cetype = cetype->make_non_abstract_type();
2926 this->type_ = cetype;
2930 // Check for a loop in which the initializer of a constant refers to
2931 // the constant itself.
2934 Const_expression::check_for_init_loop()
2936 if (this->type_ != NULL && this->type_->is_error())
2941 this->report_error(_("constant refers to itself"));
2942 this->type_ = Type::make_error_type();
2946 Expression* init = this->constant_->const_value()->expr();
2947 Find_named_object find_named_object(this->constant_);
2950 Expression::traverse(&init, &find_named_object);
2951 this->seen_ = false;
2953 if (find_named_object.found())
2955 if (this->type_ == NULL || !this->type_->is_error())
2957 this->report_error(_("constant refers to itself"));
2958 this->type_ = Type::make_error_type();
2964 // Check types of a const reference.
2967 Const_expression::do_check_types(Gogo*)
2969 if (this->type_ != NULL && this->type_->is_error())
2972 this->check_for_init_loop();
2974 if (this->type_ == NULL || this->type_->is_abstract())
2977 // Check for integer overflow.
2978 if (this->type_->integer_type() != NULL)
2983 if (!this->integer_constant_value(true, ival, &dummy))
2987 Expression* cexpr = this->constant_->const_value()->expr();
2988 if (cexpr->float_constant_value(fval, &dummy))
2990 if (!mpfr_integer_p(fval))
2991 this->report_error(_("floating point constant "
2992 "truncated to integer"));
2995 mpfr_get_z(ival, fval, GMP_RNDN);
2996 Integer_expression::check_constant(ival, this->type_,
3006 // Return a tree for the const reference.
3009 Const_expression::do_get_tree(Translate_context* context)
3011 Gogo* gogo = context->gogo();
3013 if (this->type_ == NULL)
3014 type_tree = NULL_TREE;
3017 type_tree = type_to_tree(this->type_->get_backend(gogo));
3018 if (type_tree == error_mark_node)
3019 return error_mark_node;
3022 // If the type has been set for this expression, but the underlying
3023 // object is an abstract int or float, we try to get the abstract
3024 // value. Otherwise we may lose something in the conversion.
3025 if (this->type_ != NULL
3026 && (this->constant_->const_value()->type() == NULL
3027 || this->constant_->const_value()->type()->is_abstract()))
3029 Expression* expr = this->constant_->const_value()->expr();
3033 if (expr->integer_constant_value(true, ival, &t))
3035 tree ret = Expression::integer_constant_tree(ival, type_tree);
3043 if (expr->float_constant_value(fval, &t))
3045 tree ret = Expression::float_constant_tree(fval, type_tree);
3052 if (expr->complex_constant_value(fval, imag, &t))
3054 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
3063 tree const_tree = this->constant_->get_tree(gogo, context->function());
3064 if (this->type_ == NULL
3065 || const_tree == error_mark_node
3066 || TREE_TYPE(const_tree) == error_mark_node)
3070 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
3071 ret = fold_convert(type_tree, const_tree);
3072 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
3073 ret = fold(convert_to_integer(type_tree, const_tree));
3074 else if (TREE_CODE(type_tree) == REAL_TYPE)
3075 ret = fold(convert_to_real(type_tree, const_tree));
3076 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
3077 ret = fold(convert_to_complex(type_tree, const_tree));
3083 // Dump ast representation for constant expression.
3086 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
3088 ast_dump_context->ostream() << this->constant_->name();
3091 // Make a reference to a constant in an expression.
3094 Expression::make_const_reference(Named_object* constant,
3097 return new Const_expression(constant, location);
3100 // Find a named object in an expression.
3103 Find_named_object::expression(Expression** pexpr)
3105 switch ((*pexpr)->classification())
3107 case Expression::EXPRESSION_CONST_REFERENCE:
3109 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
3110 if (ce->named_object() == this->no_)
3113 // We need to check a constant initializer explicitly, as
3114 // loops here will not be caught by the loop checking for
3115 // variable initializers.
3116 ce->check_for_init_loop();
3118 return TRAVERSE_CONTINUE;
3121 case Expression::EXPRESSION_VAR_REFERENCE:
3122 if ((*pexpr)->var_expression()->named_object() == this->no_)
3124 return TRAVERSE_CONTINUE;
3125 case Expression::EXPRESSION_FUNC_REFERENCE:
3126 if ((*pexpr)->func_expression()->named_object() == this->no_)
3128 return TRAVERSE_CONTINUE;
3130 return TRAVERSE_CONTINUE;
3132 this->found_ = true;
3133 return TRAVERSE_EXIT;
3138 class Nil_expression : public Expression
3141 Nil_expression(Location location)
3142 : Expression(EXPRESSION_NIL, location)
3150 do_is_constant() const
3155 { return Type::make_nil_type(); }
3158 do_determine_type(const Type_context*)
3166 do_get_tree(Translate_context*)
3167 { return null_pointer_node; }
3170 do_export(Export* exp) const
3171 { exp->write_c_string("nil"); }
3174 do_dump_expression(Ast_dump_context* ast_dump_context) const
3175 { ast_dump_context->ostream() << "nil"; }
3178 // Import a nil expression.
3181 Nil_expression::do_import(Import* imp)
3183 imp->require_c_string("nil");
3184 return Expression::make_nil(imp->location());
3187 // Make a nil expression.
3190 Expression::make_nil(Location location)
3192 return new Nil_expression(location);
3195 // The value of the predeclared constant iota. This is little more
3196 // than a marker. This will be lowered to an integer in
3197 // Const_expression::do_lower, which is where we know the value that
3200 class Iota_expression : public Parser_expression
3203 Iota_expression(Location location)
3204 : Parser_expression(EXPRESSION_IOTA, location)
3209 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
3210 { go_unreachable(); }
3212 // There should only ever be one of these.
3215 { go_unreachable(); }
3218 do_dump_expression(Ast_dump_context* ast_dump_context) const
3219 { ast_dump_context->ostream() << "iota"; }
3222 // Make an iota expression. This is only called for one case: the
3223 // value of the predeclared constant iota.
3226 Expression::make_iota()
3228 static Iota_expression iota_expression(Linemap::unknown_location());
3229 return &iota_expression;
3232 // A type conversion expression.
3234 class Type_conversion_expression : public Expression
3237 Type_conversion_expression(Type* type, Expression* expr,
3239 : Expression(EXPRESSION_CONVERSION, location),
3240 type_(type), expr_(expr), may_convert_function_types_(false)
3243 // Return the type to which we are converting.
3246 { return this->type_; }
3248 // Return the expression which we are converting.
3251 { return this->expr_; }
3253 // Permit converting from one function type to another. This is
3254 // used internally for method expressions.
3256 set_may_convert_function_types()
3258 this->may_convert_function_types_ = true;
3261 // Import a type conversion expression.
3267 do_traverse(Traverse* traverse);
3270 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3273 do_is_constant() const
3274 { return this->expr_->is_constant(); }
3277 do_integer_constant_value(bool, mpz_t, Type**) const;
3280 do_float_constant_value(mpfr_t, Type**) const;
3283 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3286 do_string_constant_value(std::string*) const;
3290 { return this->type_; }
3293 do_determine_type(const Type_context*)
3295 Type_context subcontext(this->type_, false);
3296 this->expr_->determine_type(&subcontext);
3300 do_check_types(Gogo*);
3305 return new Type_conversion_expression(this->type_, this->expr_->copy(),
3310 do_get_tree(Translate_context* context);
3313 do_export(Export*) const;
3316 do_dump_expression(Ast_dump_context*) const;
3319 // The type to convert to.
3321 // The expression to convert.
3323 // True if this is permitted to convert function types. This is
3324 // used internally for method expressions.
3325 bool may_convert_function_types_;
3331 Type_conversion_expression::do_traverse(Traverse* traverse)
3333 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3334 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3335 return TRAVERSE_EXIT;
3336 return TRAVERSE_CONTINUE;
3339 // Convert to a constant at lowering time.
3342 Type_conversion_expression::do_lower(Gogo*, Named_object*,
3343 Statement_inserter*, int)
3345 Type* type = this->type_;
3346 Expression* val = this->expr_;
3347 Location location = this->location();
3349 if (type->integer_type() != NULL)
3354 if (val->integer_constant_value(false, ival, &dummy))
3356 if (!Integer_expression::check_constant(ival, type, location))
3357 mpz_set_ui(ival, 0);
3358 Expression* ret = Expression::make_integer(&ival, type, location);
3365 if (val->float_constant_value(fval, &dummy))
3367 if (!mpfr_integer_p(fval))
3370 "floating point constant truncated to integer");
3371 return Expression::make_error(location);
3373 mpfr_get_z(ival, fval, GMP_RNDN);
3374 if (!Integer_expression::check_constant(ival, type, location))
3375 mpz_set_ui(ival, 0);
3376 Expression* ret = Expression::make_integer(&ival, type, location);
3385 if (type->float_type() != NULL)
3390 if (val->float_constant_value(fval, &dummy))
3392 if (!Float_expression::check_constant(fval, type, location))
3393 mpfr_set_ui(fval, 0, GMP_RNDN);
3394 Float_expression::constrain_float(fval, type);
3395 Expression *ret = Expression::make_float(&fval, type, location);
3402 if (type->complex_type() != NULL)
3409 if (val->complex_constant_value(real, imag, &dummy))
3411 if (!Complex_expression::check_constant(real, imag, type, location))
3413 mpfr_set_ui(real, 0, GMP_RNDN);
3414 mpfr_set_ui(imag, 0, GMP_RNDN);
3416 Complex_expression::constrain_complex(real, imag, type);
3417 Expression* ret = Expression::make_complex(&real, &imag, type,
3427 if (type->is_slice_type())
3429 Type* element_type = type->array_type()->element_type()->forwarded();
3430 bool is_byte = (element_type->integer_type() != NULL
3431 && element_type->integer_type()->is_byte());
3432 bool is_rune = (element_type->integer_type() != NULL
3433 && element_type->integer_type()->is_rune());
3434 if (is_byte || is_rune)
3437 if (val->string_constant_value(&s))
3439 Expression_list* vals = new Expression_list();
3442 for (std::string::const_iterator p = s.begin();
3447 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3448 Expression* v = Expression::make_integer(&val,
3457 const char *p = s.data();
3458 const char *pend = s.data() + s.length();
3462 int adv = Lex::fetch_char(p, &c);
3465 warning_at(this->location(), 0,
3466 "invalid UTF-8 encoding");
3471 mpz_init_set_ui(val, c);
3472 Expression* v = Expression::make_integer(&val,
3480 return Expression::make_slice_composite_literal(type, vals,
3489 // Return the constant integer value if there is one.
3492 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3496 if (this->type_->integer_type() == NULL)
3502 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3504 if (!Integer_expression::check_constant(ival, this->type_,
3512 *ptype = this->type_;
3519 if (this->expr_->float_constant_value(fval, &dummy))
3521 mpfr_get_z(val, fval, GMP_RNDN);
3523 if (!Integer_expression::check_constant(val, this->type_,
3526 *ptype = this->type_;
3534 // Return the constant floating point value if there is one.
3537 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3540 if (this->type_->float_type() == NULL)
3546 if (this->expr_->float_constant_value(fval, &dummy))
3548 if (!Float_expression::check_constant(fval, this->type_,
3554 mpfr_set(val, fval, GMP_RNDN);
3556 Float_expression::constrain_float(val, this->type_);
3557 *ptype = this->type_;
3565 // Return the constant complex value if there is one.
3568 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3572 if (this->type_->complex_type() == NULL)
3580 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3582 if (!Complex_expression::check_constant(rval, ival, this->type_,
3589 mpfr_set(real, rval, GMP_RNDN);
3590 mpfr_set(imag, ival, GMP_RNDN);
3593 Complex_expression::constrain_complex(real, imag, this->type_);
3594 *ptype = this->type_;
3603 // Return the constant string value if there is one.
3606 Type_conversion_expression::do_string_constant_value(std::string* val) const
3608 if (this->type_->is_string_type()
3609 && this->expr_->type()->integer_type() != NULL)
3614 if (this->expr_->integer_constant_value(false, ival, &dummy))
3616 unsigned long ulval = mpz_get_ui(ival);
3617 if (mpz_cmp_ui(ival, ulval) == 0)
3619 Lex::append_char(ulval, true, val, this->location());
3627 // FIXME: Could handle conversion from const []int here.
3632 // Check that types are convertible.
3635 Type_conversion_expression::do_check_types(Gogo*)
3637 Type* type = this->type_;
3638 Type* expr_type = this->expr_->type();
3641 if (type->is_error() || expr_type->is_error())
3643 this->set_is_error();
3647 if (this->may_convert_function_types_
3648 && type->function_type() != NULL
3649 && expr_type->function_type() != NULL)
3652 if (Type::are_convertible(type, expr_type, &reason))
3655 error_at(this->location(), "%s", reason.c_str());
3656 this->set_is_error();
3659 // Get a tree for a type conversion.
3662 Type_conversion_expression::do_get_tree(Translate_context* context)
3664 Gogo* gogo = context->gogo();
3665 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
3666 tree expr_tree = this->expr_->get_tree(context);
3668 if (type_tree == error_mark_node
3669 || expr_tree == error_mark_node
3670 || TREE_TYPE(expr_tree) == error_mark_node)
3671 return error_mark_node;
3673 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3674 return fold_convert(type_tree, expr_tree);
3676 Type* type = this->type_;
3677 Type* expr_type = this->expr_->type();
3679 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3680 ret = Expression::convert_for_assignment(context, type, expr_type,
3681 expr_tree, this->location());
3682 else if (type->integer_type() != NULL)
3684 if (expr_type->integer_type() != NULL
3685 || expr_type->float_type() != NULL
3686 || expr_type->is_unsafe_pointer_type())
3687 ret = fold(convert_to_integer(type_tree, expr_tree));
3691 else if (type->float_type() != NULL)
3693 if (expr_type->integer_type() != NULL
3694 || expr_type->float_type() != NULL)
3695 ret = fold(convert_to_real(type_tree, expr_tree));
3699 else if (type->complex_type() != NULL)
3701 if (expr_type->complex_type() != NULL)
3702 ret = fold(convert_to_complex(type_tree, expr_tree));
3706 else if (type->is_string_type()
3707 && expr_type->integer_type() != NULL)
3709 expr_tree = fold_convert(integer_type_node, expr_tree);
3710 if (host_integerp(expr_tree, 0))
3712 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3714 Lex::append_char(intval, true, &s, this->location());
3715 Expression* se = Expression::make_string(s, this->location());
3716 return se->get_tree(context);
3719 static tree int_to_string_fndecl;
3720 ret = Gogo::call_builtin(&int_to_string_fndecl,
3722 "__go_int_to_string",
3726 fold_convert(integer_type_node, expr_tree));
3728 else if (type->is_string_type() && expr_type->is_slice_type())
3730 if (!DECL_P(expr_tree))
3731 expr_tree = save_expr(expr_tree);
3732 Array_type* a = expr_type->array_type();
3733 Type* e = a->element_type()->forwarded();
3734 go_assert(e->integer_type() != NULL);
3735 tree valptr = fold_convert(const_ptr_type_node,
3736 a->value_pointer_tree(gogo, expr_tree));
3737 tree len = a->length_tree(gogo, expr_tree);
3738 len = fold_convert_loc(this->location().gcc_location(), integer_type_node,
3740 if (e->integer_type()->is_byte())
3742 static tree byte_array_to_string_fndecl;
3743 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3745 "__go_byte_array_to_string",
3748 const_ptr_type_node,
3755 go_assert(e->integer_type()->is_rune());
3756 static tree int_array_to_string_fndecl;
3757 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3759 "__go_int_array_to_string",
3762 const_ptr_type_node,
3768 else if (type->is_slice_type() && expr_type->is_string_type())
3770 Type* e = type->array_type()->element_type()->forwarded();
3771 go_assert(e->integer_type() != NULL);
3772 if (e->integer_type()->is_byte())
3774 tree string_to_byte_array_fndecl = NULL_TREE;
3775 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3777 "__go_string_to_byte_array",
3780 TREE_TYPE(expr_tree),
3785 go_assert(e->integer_type()->is_rune());
3786 tree string_to_int_array_fndecl = NULL_TREE;
3787 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3789 "__go_string_to_int_array",
3792 TREE_TYPE(expr_tree),
3796 else if ((type->is_unsafe_pointer_type()
3797 && expr_type->points_to() != NULL)
3798 || (expr_type->is_unsafe_pointer_type()
3799 && type->points_to() != NULL))
3800 ret = fold_convert(type_tree, expr_tree);
3801 else if (type->is_unsafe_pointer_type()
3802 && expr_type->integer_type() != NULL)
3803 ret = convert_to_pointer(type_tree, expr_tree);
3804 else if (this->may_convert_function_types_
3805 && type->function_type() != NULL
3806 && expr_type->function_type() != NULL)
3807 ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3810 ret = Expression::convert_for_assignment(context, type, expr_type,
3811 expr_tree, this->location());
3816 // Output a type conversion in a constant expression.
3819 Type_conversion_expression::do_export(Export* exp) const
3821 exp->write_c_string("convert(");
3822 exp->write_type(this->type_);
3823 exp->write_c_string(", ");
3824 this->expr_->export_expression(exp);
3825 exp->write_c_string(")");
3828 // Import a type conversion or a struct construction.
3831 Type_conversion_expression::do_import(Import* imp)
3833 imp->require_c_string("convert(");
3834 Type* type = imp->read_type();
3835 imp->require_c_string(", ");
3836 Expression* val = Expression::import_expression(imp);
3837 imp->require_c_string(")");
3838 return Expression::make_cast(type, val, imp->location());
3841 // Dump ast representation for a type conversion expression.
3844 Type_conversion_expression::do_dump_expression(
3845 Ast_dump_context* ast_dump_context) const
3847 ast_dump_context->dump_type(this->type_);
3848 ast_dump_context->ostream() << "(";
3849 ast_dump_context->dump_expression(this->expr_);
3850 ast_dump_context->ostream() << ") ";
3853 // Make a type cast expression.
3856 Expression::make_cast(Type* type, Expression* val, Location location)
3858 if (type->is_error_type() || val->is_error_expression())
3859 return Expression::make_error(location);
3860 return new Type_conversion_expression(type, val, location);
3863 // An unsafe type conversion, used to pass values to builtin functions.
3865 class Unsafe_type_conversion_expression : public Expression
3868 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3870 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3871 type_(type), expr_(expr)
3876 do_traverse(Traverse* traverse);
3880 { return this->type_; }
3883 do_determine_type(const Type_context*)
3884 { this->expr_->determine_type_no_context(); }
3889 return new Unsafe_type_conversion_expression(this->type_,
3890 this->expr_->copy(),
3895 do_get_tree(Translate_context*);
3898 do_dump_expression(Ast_dump_context*) const;
3901 // The type to convert to.
3903 // The expression to convert.
3910 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3912 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3913 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3914 return TRAVERSE_EXIT;
3915 return TRAVERSE_CONTINUE;
3918 // Convert to backend representation.
3921 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3923 // We are only called for a limited number of cases.
3925 Type* t = this->type_;
3926 Type* et = this->expr_->type();
3928 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
3929 tree expr_tree = this->expr_->get_tree(context);
3930 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3931 return error_mark_node;
3933 Location loc = this->location();
3935 bool use_view_convert = false;
3936 if (t->is_slice_type())
3938 go_assert(et->is_slice_type());
3939 use_view_convert = true;
3941 else if (t->map_type() != NULL)
3942 go_assert(et->map_type() != NULL);
3943 else if (t->channel_type() != NULL)
3944 go_assert(et->channel_type() != NULL);
3945 else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL)
3946 go_assert((et->points_to() != NULL
3947 && et->points_to()->channel_type() != NULL)
3948 || et->is_nil_type());
3949 else if (t->points_to() != NULL)
3950 go_assert(et->points_to() != NULL || et->is_nil_type());
3951 else if (et->is_unsafe_pointer_type())
3952 go_assert(t->points_to() != NULL);
3953 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3955 go_assert(et->interface_type() != NULL
3956 && !et->interface_type()->is_empty());
3957 use_view_convert = true;
3959 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3961 go_assert(et->interface_type() != NULL
3962 && et->interface_type()->is_empty());
3963 use_view_convert = true;
3965 else if (t->integer_type() != NULL)
3967 go_assert(et->is_boolean_type()
3968 || et->integer_type() != NULL
3969 || et->function_type() != NULL
3970 || et->points_to() != NULL
3971 || et->map_type() != NULL
3972 || et->channel_type() != NULL);
3973 return convert_to_integer(type_tree, expr_tree);
3978 if (use_view_convert)
3979 return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3982 return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
3985 // Dump ast representation for an unsafe type conversion expression.
3988 Unsafe_type_conversion_expression::do_dump_expression(
3989 Ast_dump_context* ast_dump_context) const
3991 ast_dump_context->dump_type(this->type_);
3992 ast_dump_context->ostream() << "(";
3993 ast_dump_context->dump_expression(this->expr_);
3994 ast_dump_context->ostream() << ") ";
3997 // Make an unsafe type conversion expression.
4000 Expression::make_unsafe_cast(Type* type, Expression* expr,
4003 return new Unsafe_type_conversion_expression(type, expr, location);
4006 // Unary expressions.
4008 class Unary_expression : public Expression
4011 Unary_expression(Operator op, Expression* expr, Location location)
4012 : Expression(EXPRESSION_UNARY, location),
4013 op_(op), escapes_(true), create_temp_(false), expr_(expr)
4016 // Return the operator.
4019 { return this->op_; }
4021 // Return the operand.
4024 { return this->expr_; }
4026 // Record that an address expression does not escape.
4028 set_does_not_escape()
4030 go_assert(this->op_ == OPERATOR_AND);
4031 this->escapes_ = false;
4034 // Record that this is an address expression which should create a
4035 // temporary variable if necessary. This is used for method calls.
4039 go_assert(this->op_ == OPERATOR_AND);
4040 this->create_temp_ = true;
4043 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
4044 // could be done, false if not.
4046 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
4049 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
4050 // could be done, false if not.
4052 eval_float(Operator op, mpfr_t uval, mpfr_t val);
4054 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
4055 // true if this could be done, false if not.
4057 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
4065 do_traverse(Traverse* traverse)
4066 { return Expression::traverse(&this->expr_, traverse); }
4069 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
4072 do_is_constant() const;
4075 do_integer_constant_value(bool, mpz_t, Type**) const;
4078 do_float_constant_value(mpfr_t, Type**) const;
4081 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
4087 do_determine_type(const Type_context*);
4090 do_check_types(Gogo*);
4095 return Expression::make_unary(this->op_, this->expr_->copy(),
4100 do_must_eval_subexpressions_in_order(int*) const
4101 { return this->op_ == OPERATOR_MULT; }
4104 do_is_addressable() const
4105 { return this->op_ == OPERATOR_MULT; }
4108 do_get_tree(Translate_context*);
4111 do_export(Export*) const;
4114 do_dump_expression(Ast_dump_context*) const;
4117 // The unary operator to apply.
4119 // Normally true. False if this is an address expression which does
4120 // not escape the current function.
4122 // True if this is an address expression which should create a
4123 // temporary variable if necessary.
4129 // If we are taking the address of a composite literal, and the
4130 // contents are not constant, then we want to make a heap composite
4134 Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
4136 Location loc = this->location();
4137 Operator op = this->op_;
4138 Expression* expr = this->expr_;
4140 if (op == OPERATOR_MULT && expr->is_type_expression())
4141 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
4143 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
4144 // moving x to the heap. FIXME: Is it worth doing a real escape
4145 // analysis here? This case is found in math/unsafe.go and is
4146 // therefore worth special casing.
4147 if (op == OPERATOR_MULT)
4149 Expression* e = expr;
4150 while (e->classification() == EXPRESSION_CONVERSION)
4152 Type_conversion_expression* te
4153 = static_cast<Type_conversion_expression*>(e);
4157 if (e->classification() == EXPRESSION_UNARY)
4159 Unary_expression* ue = static_cast<Unary_expression*>(e);
4160 if (ue->op_ == OPERATOR_AND)
4167 ue->set_does_not_escape();
4172 // Catching an invalid indirection of unsafe.Pointer here avoid
4173 // having to deal with TYPE_VOID in other places.
4174 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
4176 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
4177 return Expression::make_error(this->location());
4180 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
4181 || op == OPERATOR_NOT || op == OPERATOR_XOR)
4183 Expression* ret = NULL;
4188 if (expr->integer_constant_value(false, eval, &etype))
4192 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
4193 ret = Expression::make_integer(&val, etype, loc);
4200 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
4205 if (expr->float_constant_value(fval, &ftype))
4209 if (Unary_expression::eval_float(op, fval, val))
4210 ret = Expression::make_float(&val, ftype, loc);
4221 if (expr->complex_constant_value(fval, ival, &ftype))
4227 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
4228 ret = Expression::make_complex(&real, &imag, ftype, loc);
4242 // Return whether a unary expression is a constant.
4245 Unary_expression::do_is_constant() const
4247 if (this->op_ == OPERATOR_MULT)
4249 // Indirecting through a pointer is only constant if the object
4250 // to which the expression points is constant, but we currently
4251 // have no way to determine that.
4254 else if (this->op_ == OPERATOR_AND)
4256 // Taking the address of a variable is constant if it is a
4257 // global variable, not constant otherwise. In other cases
4258 // taking the address is probably not a constant.
4259 Var_expression* ve = this->expr_->var_expression();
4262 Named_object* no = ve->named_object();
4263 return no->is_variable() && no->var_value()->is_global();
4268 return this->expr_->is_constant();
4271 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
4272 // UVAL, if known; it may be NULL. Return true if this could be done,
4276 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
4284 case OPERATOR_MINUS:
4286 return Integer_expression::check_constant(val, utype, location);
4288 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
4292 || utype->integer_type() == NULL
4293 || utype->integer_type()->is_abstract())
4297 // The number of HOST_WIDE_INTs that it takes to represent
4299 size_t count = ((mpz_sizeinbase(uval, 2)
4300 + HOST_BITS_PER_WIDE_INT
4302 / HOST_BITS_PER_WIDE_INT);
4304 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
4305 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
4308 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
4309 go_assert(ecount <= count);
4311 // Trim down to the number of words required by the type.
4312 size_t obits = utype->integer_type()->bits();
4313 if (!utype->integer_type()->is_unsigned())
4315 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
4316 / HOST_BITS_PER_WIDE_INT);
4317 go_assert(ocount <= count);
4319 for (size_t i = 0; i < ocount; ++i)
4322 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
4324 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
4327 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
4331 return Integer_expression::check_constant(val, utype, location);
4340 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
4341 // could be done, false if not.
4344 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
4349 mpfr_set(val, uval, GMP_RNDN);
4351 case OPERATOR_MINUS:
4352 mpfr_neg(val, uval, GMP_RNDN);
4364 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
4365 // if this could be done, false if not.
4368 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
4369 mpfr_t real, mpfr_t imag)
4374 mpfr_set(real, rval, GMP_RNDN);
4375 mpfr_set(imag, ival, GMP_RNDN);
4377 case OPERATOR_MINUS:
4378 mpfr_neg(real, rval, GMP_RNDN);
4379 mpfr_neg(imag, ival, GMP_RNDN);
4391 // Return the integral constant value of a unary expression, if it has one.
4394 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
4400 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
4403 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
4409 // Return the floating point constant value of a unary expression, if
4413 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
4418 if (!this->expr_->float_constant_value(uval, ptype))
4421 ret = Unary_expression::eval_float(this->op_, uval, val);
4426 // Return the complex constant value of a unary expression, if it has
4430 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
4438 if (!this->expr_->complex_constant_value(rval, ival, ptype))
4441 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
4447 // Return the type of a unary expression.
4450 Unary_expression::do_type()
4455 case OPERATOR_MINUS:
4458 return this->expr_->type();
4461 return Type::make_pointer_type(this->expr_->type());
4465 Type* subtype = this->expr_->type();
4466 Type* points_to = subtype->points_to();
4467 if (points_to == NULL)
4468 return Type::make_error_type();
4477 // Determine abstract types for a unary expression.
4480 Unary_expression::do_determine_type(const Type_context* context)
4485 case OPERATOR_MINUS:
4488 this->expr_->determine_type(context);
4492 // Taking the address of something.
4494 Type* subtype = (context->type == NULL
4496 : context->type->points_to());
4497 Type_context subcontext(subtype, false);
4498 this->expr_->determine_type(&subcontext);
4503 // Indirecting through a pointer.
4505 Type* subtype = (context->type == NULL
4507 : Type::make_pointer_type(context->type));
4508 Type_context subcontext(subtype, false);
4509 this->expr_->determine_type(&subcontext);
4518 // Check types for a unary expression.
4521 Unary_expression::do_check_types(Gogo*)
4523 Type* type = this->expr_->type();
4524 if (type->is_error())
4526 this->set_is_error();
4533 case OPERATOR_MINUS:
4534 if (type->integer_type() == NULL
4535 && type->float_type() == NULL
4536 && type->complex_type() == NULL)
4537 this->report_error(_("expected numeric type"));
4542 if (type->integer_type() == NULL
4543 && !type->is_boolean_type())
4544 this->report_error(_("expected integer or boolean type"));
4548 if (!this->expr_->is_addressable())
4550 if (!this->create_temp_)
4551 this->report_error(_("invalid operand for unary %<&%>"));
4554 this->expr_->address_taken(this->escapes_);
4558 // Indirecting through a pointer.
4559 if (type->points_to() == NULL)
4560 this->report_error(_("expected pointer"));
4568 // Get a tree for a unary expression.
4571 Unary_expression::do_get_tree(Translate_context* context)
4573 Location loc = this->location();
4575 // Taking the address of a set-and-use-temporary expression requires
4576 // setting the temporary and then taking the address.
4577 if (this->op_ == OPERATOR_AND)
4579 Set_and_use_temporary_expression* sut =
4580 this->expr_->set_and_use_temporary_expression();
4583 Temporary_statement* temp = sut->temporary();
4584 Bvariable* bvar = temp->get_backend_variable(context);
4585 tree var_tree = var_to_tree(bvar);
4586 Expression* val = sut->expression();
4587 tree val_tree = val->get_tree(context);
4588 if (var_tree == error_mark_node || val_tree == error_mark_node)
4589 return error_mark_node;
4590 tree addr_tree = build_fold_addr_expr_loc(loc.gcc_location(),
4592 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4593 TREE_TYPE(addr_tree),
4594 build2_loc(sut->location().gcc_location(),
4595 MODIFY_EXPR, void_type_node,
4596 var_tree, val_tree),
4601 tree expr = this->expr_->get_tree(context);
4602 if (expr == error_mark_node)
4603 return error_mark_node;
4610 case OPERATOR_MINUS:
4612 tree type = TREE_TYPE(expr);
4613 tree compute_type = excess_precision_type(type);
4614 if (compute_type != NULL_TREE)
4615 expr = ::convert(compute_type, expr);
4616 tree ret = fold_build1_loc(loc.gcc_location(), NEGATE_EXPR,
4617 (compute_type != NULL_TREE
4621 if (compute_type != NULL_TREE)
4622 ret = ::convert(type, ret);
4627 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4628 return fold_build1_loc(loc.gcc_location(), TRUTH_NOT_EXPR,
4629 TREE_TYPE(expr), expr);
4631 return fold_build2_loc(loc.gcc_location(), NE_EXPR, boolean_type_node,
4632 expr, build_int_cst(TREE_TYPE(expr), 0));
4635 return fold_build1_loc(loc.gcc_location(), BIT_NOT_EXPR, TREE_TYPE(expr),
4639 if (!this->create_temp_)
4641 // We should not see a non-constant constructor here; cases
4642 // where we would see one should have been moved onto the
4643 // heap at parse time. Taking the address of a nonconstant
4644 // constructor will not do what the programmer expects.
4645 go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4646 go_assert(TREE_CODE(expr) != ADDR_EXPR);
4649 // Build a decl for a constant constructor.
4650 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4652 tree decl = build_decl(this->location().gcc_location(), VAR_DECL,
4653 create_tmp_var_name("C"), TREE_TYPE(expr));
4654 DECL_EXTERNAL(decl) = 0;
4655 TREE_PUBLIC(decl) = 0;
4656 TREE_READONLY(decl) = 1;
4657 TREE_CONSTANT(decl) = 1;
4658 TREE_STATIC(decl) = 1;
4659 TREE_ADDRESSABLE(decl) = 1;
4660 DECL_ARTIFICIAL(decl) = 1;
4661 DECL_INITIAL(decl) = expr;
4662 rest_of_decl_compilation(decl, 1, 0);
4666 if (this->create_temp_
4667 && !TREE_ADDRESSABLE(TREE_TYPE(expr))
4669 && TREE_CODE(expr) != INDIRECT_REF
4670 && TREE_CODE(expr) != COMPONENT_REF)
4672 tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr));
4673 DECL_IGNORED_P(tmp) = 1;
4674 DECL_INITIAL(tmp) = expr;
4675 TREE_ADDRESSABLE(tmp) = 1;
4676 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4677 build_pointer_type(TREE_TYPE(expr)),
4678 build1_loc(loc.gcc_location(), DECL_EXPR,
4679 void_type_node, tmp),
4680 build_fold_addr_expr_loc(loc.gcc_location(), tmp));
4683 return build_fold_addr_expr_loc(loc.gcc_location(), expr);
4687 go_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4689 // If we are dereferencing the pointer to a large struct, we
4690 // need to check for nil. We don't bother to check for small
4691 // structs because we expect the system to crash on a nil
4692 // pointer dereference.
4693 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4694 if (s == -1 || s >= 4096)
4697 expr = save_expr(expr);
4698 tree compare = fold_build2_loc(loc.gcc_location(), EQ_EXPR,
4701 fold_convert(TREE_TYPE(expr),
4702 null_pointer_node));
4703 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4705 expr = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4706 TREE_TYPE(expr), build3(COND_EXPR,
4713 // If the type of EXPR is a recursive pointer type, then we
4714 // need to insert a cast before indirecting.
4715 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4717 Type* pt = this->expr_->type()->points_to();
4718 tree ind = type_to_tree(pt->get_backend(context->gogo()));
4719 expr = fold_convert_loc(loc.gcc_location(),
4720 build_pointer_type(ind), expr);
4723 return build_fold_indirect_ref_loc(loc.gcc_location(), expr);
4731 // Export a unary expression.
4734 Unary_expression::do_export(Export* exp) const
4739 exp->write_c_string("+ ");
4741 case OPERATOR_MINUS:
4742 exp->write_c_string("- ");
4745 exp->write_c_string("! ");
4748 exp->write_c_string("^ ");
4755 this->expr_->export_expression(exp);
4758 // Import a unary expression.
4761 Unary_expression::do_import(Import* imp)
4764 switch (imp->get_char())
4770 op = OPERATOR_MINUS;
4781 imp->require_c_string(" ");
4782 Expression* expr = Expression::import_expression(imp);
4783 return Expression::make_unary(op, expr, imp->location());
4786 // Dump ast representation of an unary expression.
4789 Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
4791 ast_dump_context->dump_operator(this->op_);
4792 ast_dump_context->ostream() << "(";
4793 ast_dump_context->dump_expression(this->expr_);
4794 ast_dump_context->ostream() << ") ";
4797 // Make a unary expression.
4800 Expression::make_unary(Operator op, Expression* expr, Location location)
4802 return new Unary_expression(op, expr, location);
4805 // If this is an indirection through a pointer, return the expression
4806 // being pointed through. Otherwise return this.
4811 if (this->classification_ == EXPRESSION_UNARY)
4813 Unary_expression* ue = static_cast<Unary_expression*>(this);
4814 if (ue->op() == OPERATOR_MULT)
4815 return ue->operand();
4820 // Class Binary_expression.
4825 Binary_expression::do_traverse(Traverse* traverse)
4827 int t = Expression::traverse(&this->left_, traverse);
4828 if (t == TRAVERSE_EXIT)
4829 return TRAVERSE_EXIT;
4830 return Expression::traverse(&this->right_, traverse);
4833 // Compare integer constants according to OP.
4836 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4839 int i = mpz_cmp(left_val, right_val);
4844 case OPERATOR_NOTEQ:
4859 // Compare floating point constants according to OP.
4862 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4867 i = mpfr_cmp(left_val, right_val);
4871 mpfr_init_set(lv, left_val, GMP_RNDN);
4873 mpfr_init_set(rv, right_val, GMP_RNDN);
4874 Float_expression::constrain_float(lv, type);
4875 Float_expression::constrain_float(rv, type);
4876 i = mpfr_cmp(lv, rv);
4884 case OPERATOR_NOTEQ:
4899 // Compare complex constants according to OP. Complex numbers may
4900 // only be compared for equality.
4903 Binary_expression::compare_complex(Operator op, Type* type,
4904 mpfr_t left_real, mpfr_t left_imag,
4905 mpfr_t right_real, mpfr_t right_imag)
4909 is_equal = (mpfr_cmp(left_real, right_real) == 0
4910 && mpfr_cmp(left_imag, right_imag) == 0);
4915 mpfr_init_set(lr, left_real, GMP_RNDN);
4916 mpfr_init_set(li, left_imag, GMP_RNDN);
4919 mpfr_init_set(rr, right_real, GMP_RNDN);
4920 mpfr_init_set(ri, right_imag, GMP_RNDN);
4921 Complex_expression::constrain_complex(lr, li, type);
4922 Complex_expression::constrain_complex(rr, ri, type);
4923 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4933 case OPERATOR_NOTEQ:
4940 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4941 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4942 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4943 // this could be done, false if not.
4946 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4947 Type* right_type, mpz_t right_val,
4948 Location location, mpz_t val)
4950 bool is_shift_op = false;
4954 case OPERATOR_ANDAND:
4956 case OPERATOR_NOTEQ:
4961 // These return boolean values. We should probably handle them
4962 // anyhow in case a type conversion is used on the result.
4965 mpz_add(val, left_val, right_val);
4967 case OPERATOR_MINUS:
4968 mpz_sub(val, left_val, right_val);
4971 mpz_ior(val, left_val, right_val);
4974 mpz_xor(val, left_val, right_val);
4977 mpz_mul(val, left_val, right_val);
4980 if (mpz_sgn(right_val) != 0)
4981 mpz_tdiv_q(val, left_val, right_val);
4984 error_at(location, "division by zero");
4990 if (mpz_sgn(right_val) != 0)
4991 mpz_tdiv_r(val, left_val, right_val);
4994 error_at(location, "division by zero");
4999 case OPERATOR_LSHIFT:
5001 unsigned long shift = mpz_get_ui(right_val);
5002 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
5004 error_at(location, "shift count overflow");
5008 mpz_mul_2exp(val, left_val, shift);
5013 case OPERATOR_RSHIFT:
5015 unsigned long shift = mpz_get_ui(right_val);
5016 if (mpz_cmp_ui(right_val, shift) != 0)
5018 error_at(location, "shift count overflow");
5022 if (mpz_cmp_ui(left_val, 0) >= 0)
5023 mpz_tdiv_q_2exp(val, left_val, shift);
5025 mpz_fdiv_q_2exp(val, left_val, shift);
5031 mpz_and(val, left_val, right_val);
5033 case OPERATOR_BITCLEAR:
5037 mpz_com(tval, right_val);
5038 mpz_and(val, left_val, tval);
5046 Type* type = left_type;
5051 else if (type != right_type && right_type != NULL)
5053 if (type->is_abstract())
5055 else if (!right_type->is_abstract())
5057 // This look like a type error which should be diagnosed
5058 // elsewhere. Don't do anything here, to avoid an
5059 // unhelpful chain of error messages.
5065 if (type != NULL && !type->is_abstract())
5067 // We have to check the operands too, as we have implicitly
5068 // coerced them to TYPE.
5069 if ((type != left_type
5070 && !Integer_expression::check_constant(left_val, type, location))
5072 && type != right_type
5073 && !Integer_expression::check_constant(right_val, type,
5075 || !Integer_expression::check_constant(val, type, location))
5082 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
5083 // Return true if this could be done, false if not.
5086 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
5087 Type* right_type, mpfr_t right_val,
5088 mpfr_t val, Location location)
5093 case OPERATOR_ANDAND:
5095 case OPERATOR_NOTEQ:
5100 // These return boolean values. We should probably handle them
5101 // anyhow in case a type conversion is used on the result.
5104 mpfr_add(val, left_val, right_val, GMP_RNDN);
5106 case OPERATOR_MINUS:
5107 mpfr_sub(val, left_val, right_val, GMP_RNDN);
5112 case OPERATOR_BITCLEAR:
5115 mpfr_mul(val, left_val, right_val, GMP_RNDN);
5118 if (mpfr_zero_p(right_val))
5119 error_at(location, "division by zero");
5120 mpfr_div(val, left_val, right_val, GMP_RNDN);
5124 case OPERATOR_LSHIFT:
5125 case OPERATOR_RSHIFT:
5131 Type* type = left_type;
5134 else if (type != right_type && right_type != NULL)
5136 if (type->is_abstract())
5138 else if (!right_type->is_abstract())
5140 // This looks like a type error which should be diagnosed
5141 // elsewhere. Don't do anything here, to avoid an unhelpful
5142 // chain of error messages.
5147 if (type != NULL && !type->is_abstract())
5149 if ((type != left_type
5150 && !Float_expression::check_constant(left_val, type, location))
5151 || (type != right_type
5152 && !Float_expression::check_constant(right_val, type,
5154 || !Float_expression::check_constant(val, type, location))
5155 mpfr_set_ui(val, 0, GMP_RNDN);
5161 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
5162 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
5163 // could be done, false if not.
5166 Binary_expression::eval_complex(Operator op, Type* left_type,
5167 mpfr_t left_real, mpfr_t left_imag,
5169 mpfr_t right_real, mpfr_t right_imag,
5170 mpfr_t real, mpfr_t imag,
5176 case OPERATOR_ANDAND:
5178 case OPERATOR_NOTEQ:
5183 // These return boolean values and must be handled differently.
5186 mpfr_add(real, left_real, right_real, GMP_RNDN);
5187 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
5189 case OPERATOR_MINUS:
5190 mpfr_sub(real, left_real, right_real, GMP_RNDN);
5191 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
5196 case OPERATOR_BITCLEAR:
5200 // You might think that multiplying two complex numbers would
5201 // be simple, and you would be right, until you start to think
5202 // about getting the right answer for infinity. If one
5203 // operand here is infinity and the other is anything other
5204 // than zero or NaN, then we are going to wind up subtracting
5205 // two infinity values. That will give us a NaN, but the
5206 // correct answer is infinity.
5210 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
5214 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
5218 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
5222 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
5224 mpfr_sub(real, lrrr, liri, GMP_RNDN);
5225 mpfr_add(imag, lrri, lirr, GMP_RNDN);
5227 // If we get NaN on both sides, check whether it should really
5228 // be infinity. The rule is that if either side of the
5229 // complex number is infinity, then the whole value is
5230 // infinity, even if the other side is NaN. So the only case
5231 // we have to fix is the one in which both sides are NaN.
5232 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
5233 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
5234 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
5236 bool is_infinity = false;
5240 mpfr_init_set(lr, left_real, GMP_RNDN);
5241 mpfr_init_set(li, left_imag, GMP_RNDN);
5245 mpfr_init_set(rr, right_real, GMP_RNDN);
5246 mpfr_init_set(ri, right_imag, GMP_RNDN);
5248 // If the left side is infinity, then the result is
5250 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
5252 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
5253 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5254 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
5255 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5258 mpfr_set_ui(rr, 0, GMP_RNDN);
5259 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5263 mpfr_set_ui(ri, 0, GMP_RNDN);
5264 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5269 // If the right side is infinity, then the result is
5271 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
5273 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5274 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5275 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5276 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5279 mpfr_set_ui(lr, 0, GMP_RNDN);
5280 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5284 mpfr_set_ui(li, 0, GMP_RNDN);
5285 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5290 // If we got an overflow in the intermediate computations,
5291 // then the result is infinity.
5293 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
5294 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
5298 mpfr_set_ui(lr, 0, GMP_RNDN);
5299 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5303 mpfr_set_ui(li, 0, GMP_RNDN);
5304 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5308 mpfr_set_ui(rr, 0, GMP_RNDN);
5309 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5313 mpfr_set_ui(ri, 0, GMP_RNDN);
5314 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5321 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
5322 mpfr_mul(lrri, lr, ri, GMP_RNDN);
5323 mpfr_mul(lirr, li, rr, GMP_RNDN);
5324 mpfr_mul(liri, li, ri, GMP_RNDN);
5325 mpfr_sub(real, lrrr, liri, GMP_RNDN);
5326 mpfr_add(imag, lrri, lirr, GMP_RNDN);
5327 mpfr_set_inf(real, mpfr_sgn(real));
5328 mpfr_set_inf(imag, mpfr_sgn(imag));
5345 // For complex division we want to avoid having an
5346 // intermediate overflow turn the whole result in a NaN. We
5347 // scale the values to try to avoid this.
5349 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
5350 error_at(location, "division by zero");
5356 mpfr_abs(rra, right_real, GMP_RNDN);
5357 mpfr_abs(ria, right_imag, GMP_RNDN);
5360 mpfr_max(t, rra, ria, GMP_RNDN);
5364 mpfr_init_set(rr, right_real, GMP_RNDN);
5365 mpfr_init_set(ri, right_imag, GMP_RNDN);
5367 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
5369 ilogbw = mpfr_get_exp(t);
5370 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
5371 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
5376 mpfr_mul(denom, rr, rr, GMP_RNDN);
5377 mpfr_mul(t, ri, ri, GMP_RNDN);
5378 mpfr_add(denom, denom, t, GMP_RNDN);
5380 mpfr_mul(real, left_real, rr, GMP_RNDN);
5381 mpfr_mul(t, left_imag, ri, GMP_RNDN);
5382 mpfr_add(real, real, t, GMP_RNDN);
5383 mpfr_div(real, real, denom, GMP_RNDN);
5384 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
5386 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
5387 mpfr_mul(t, left_real, ri, GMP_RNDN);
5388 mpfr_sub(imag, imag, t, GMP_RNDN);
5389 mpfr_div(imag, imag, denom, GMP_RNDN);
5390 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
5392 // If we wind up with NaN on both sides, check whether we
5393 // should really have infinity. The rule is that if either
5394 // side of the complex number is infinity, then the whole
5395 // value is infinity, even if the other side is NaN. So the
5396 // only case we have to fix is the one in which both sides are
5398 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
5399 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
5400 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
5402 if (mpfr_zero_p(denom))
5404 mpfr_set_inf(real, mpfr_sgn(rr));
5405 mpfr_mul(real, real, left_real, GMP_RNDN);
5406 mpfr_set_inf(imag, mpfr_sgn(rr));
5407 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
5409 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
5410 && mpfr_number_p(rr) && mpfr_number_p(ri))
5412 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
5413 mpfr_copysign(t, t, left_real, GMP_RNDN);
5416 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
5417 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
5421 mpfr_mul(t3, t, rr, GMP_RNDN);
5425 mpfr_mul(t4, t2, ri, GMP_RNDN);
5427 mpfr_add(t3, t3, t4, GMP_RNDN);
5428 mpfr_set_inf(real, mpfr_sgn(t3));
5430 mpfr_mul(t3, t2, rr, GMP_RNDN);
5431 mpfr_mul(t4, t, ri, GMP_RNDN);
5432 mpfr_sub(t3, t3, t4, GMP_RNDN);
5433 mpfr_set_inf(imag, mpfr_sgn(t3));
5439 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
5440 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
5442 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5443 mpfr_copysign(t, t, rr, GMP_RNDN);
5446 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5447 mpfr_copysign(t2, t2, ri, GMP_RNDN);
5451 mpfr_mul(t3, left_real, t, GMP_RNDN);
5455 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5457 mpfr_add(t3, t3, t4, GMP_RNDN);
5458 mpfr_set_ui(real, 0, GMP_RNDN);
5459 mpfr_mul(real, real, t3, GMP_RNDN);
5461 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5462 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5463 mpfr_sub(t3, t3, t4, GMP_RNDN);
5464 mpfr_set_ui(imag, 0, GMP_RNDN);
5465 mpfr_mul(imag, imag, t3, GMP_RNDN);
5483 case OPERATOR_LSHIFT:
5484 case OPERATOR_RSHIFT:
5490 Type* type = left_type;
5493 else if (type != right_type && right_type != NULL)
5495 if (type->is_abstract())
5497 else if (!right_type->is_abstract())
5499 // This looks like a type error which should be diagnosed
5500 // elsewhere. Don't do anything here, to avoid an unhelpful
5501 // chain of error messages.
5506 if (type != NULL && !type->is_abstract())
5508 if ((type != left_type
5509 && !Complex_expression::check_constant(left_real, left_imag,
5511 || (type != right_type
5512 && !Complex_expression::check_constant(right_real, right_imag,
5514 || !Complex_expression::check_constant(real, imag, type,
5517 mpfr_set_ui(real, 0, GMP_RNDN);
5518 mpfr_set_ui(imag, 0, GMP_RNDN);
5525 // Lower a binary expression. We have to evaluate constant
5526 // expressions now, in order to implement Go's unlimited precision
5530 Binary_expression::do_lower(Gogo* gogo, Named_object*,
5531 Statement_inserter* inserter, int)
5533 Location location = this->location();
5534 Operator op = this->op_;
5535 Expression* left = this->left_;
5536 Expression* right = this->right_;
5538 const bool is_comparison = (op == OPERATOR_EQEQ
5539 || op == OPERATOR_NOTEQ
5540 || op == OPERATOR_LT
5541 || op == OPERATOR_LE
5542 || op == OPERATOR_GT
5543 || op == OPERATOR_GE);
5545 // Integer constant expressions.
5551 mpz_init(right_val);
5553 if (left->integer_constant_value(false, left_val, &left_type)
5554 && right->integer_constant_value(false, right_val, &right_type))
5556 Expression* ret = NULL;
5557 if (left_type != right_type
5558 && left_type != NULL
5559 && !left_type->is_abstract()
5560 && right_type != NULL
5561 && !right_type->is_abstract()
5562 && left_type->base() != right_type->base()
5563 && op != OPERATOR_LSHIFT
5564 && op != OPERATOR_RSHIFT)
5566 // May be a type error--let it be diagnosed later.
5568 else if (is_comparison)
5570 bool b = Binary_expression::compare_integer(op, left_val,
5572 ret = Expression::make_cast(Type::lookup_bool_type(),
5573 Expression::make_boolean(b, location),
5581 if (Binary_expression::eval_integer(op, left_type, left_val,
5582 right_type, right_val,
5585 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5587 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5589 else if (left_type == NULL)
5591 else if (right_type == NULL)
5593 else if (!left_type->is_abstract()
5594 && left_type->named_type() != NULL)
5596 else if (!right_type->is_abstract()
5597 && right_type->named_type() != NULL)
5599 else if (!left_type->is_abstract())
5601 else if (!right_type->is_abstract())
5603 else if (left_type->float_type() != NULL)
5605 else if (right_type->float_type() != NULL)
5607 else if (left_type->complex_type() != NULL)
5609 else if (right_type->complex_type() != NULL)
5614 bool is_character = false;
5617 Type* t = this->left_->type();
5618 if (t->integer_type() != NULL
5619 && t->integer_type()->is_rune())
5620 is_character = true;
5621 else if (op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT)
5623 t = this->right_->type();
5624 if (t->integer_type() != NULL
5625 && t->integer_type()->is_rune())
5626 is_character = true;
5631 ret = Expression::make_character(&val, type, location);
5633 ret = Expression::make_integer(&val, type, location);
5641 mpz_clear(right_val);
5642 mpz_clear(left_val);
5646 mpz_clear(right_val);
5647 mpz_clear(left_val);
5650 // Floating point constant expressions.
5653 mpfr_init(left_val);
5656 mpfr_init(right_val);
5658 if (left->float_constant_value(left_val, &left_type)
5659 && right->float_constant_value(right_val, &right_type))
5661 Expression* ret = NULL;
5662 if (left_type != right_type
5663 && left_type != NULL
5664 && right_type != NULL
5665 && left_type->base() != right_type->base()
5666 && op != OPERATOR_LSHIFT
5667 && op != OPERATOR_RSHIFT)
5669 // May be a type error--let it be diagnosed later.
5671 else if (is_comparison)
5673 bool b = Binary_expression::compare_float(op,
5677 left_val, right_val);
5678 ret = Expression::make_boolean(b, location);
5685 if (Binary_expression::eval_float(op, left_type, left_val,
5686 right_type, right_val, val,
5689 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5690 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5692 if (left_type == NULL)
5694 else if (right_type == NULL)
5696 else if (!left_type->is_abstract()
5697 && left_type->named_type() != NULL)
5699 else if (!right_type->is_abstract()
5700 && right_type->named_type() != NULL)
5702 else if (!left_type->is_abstract())
5704 else if (!right_type->is_abstract())
5706 else if (left_type->float_type() != NULL)
5708 else if (right_type->float_type() != NULL)
5712 ret = Expression::make_float(&val, type, location);
5720 mpfr_clear(right_val);
5721 mpfr_clear(left_val);
5725 mpfr_clear(right_val);
5726 mpfr_clear(left_val);
5729 // Complex constant expressions.
5733 mpfr_init(left_real);
5734 mpfr_init(left_imag);
5739 mpfr_init(right_real);
5740 mpfr_init(right_imag);
5743 if (left->complex_constant_value(left_real, left_imag, &left_type)
5744 && right->complex_constant_value(right_real, right_imag, &right_type))
5746 Expression* ret = NULL;
5747 if (left_type != right_type
5748 && left_type != NULL
5749 && right_type != NULL
5750 && left_type->base() != right_type->base())
5752 // May be a type error--let it be diagnosed later.
5754 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5756 bool b = Binary_expression::compare_complex(op,
5764 ret = Expression::make_boolean(b, location);
5773 if (Binary_expression::eval_complex(op, left_type,
5774 left_real, left_imag,
5776 right_real, right_imag,
5780 go_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5781 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5783 if (left_type == NULL)
5785 else if (right_type == NULL)
5787 else if (!left_type->is_abstract()
5788 && left_type->named_type() != NULL)
5790 else if (!right_type->is_abstract()
5791 && right_type->named_type() != NULL)
5793 else if (!left_type->is_abstract())
5795 else if (!right_type->is_abstract())
5797 else if (left_type->complex_type() != NULL)
5799 else if (right_type->complex_type() != NULL)
5803 ret = Expression::make_complex(&real, &imag, type,
5812 mpfr_clear(left_real);
5813 mpfr_clear(left_imag);
5814 mpfr_clear(right_real);
5815 mpfr_clear(right_imag);
5820 mpfr_clear(left_real);
5821 mpfr_clear(left_imag);
5822 mpfr_clear(right_real);
5823 mpfr_clear(right_imag);
5826 // String constant expressions.
5827 if (op == OPERATOR_PLUS
5828 && left->type()->is_string_type()
5829 && right->type()->is_string_type())
5831 std::string left_string;
5832 std::string right_string;
5833 if (left->string_constant_value(&left_string)
5834 && right->string_constant_value(&right_string))
5835 return Expression::make_string(left_string + right_string, location);
5838 // Special case for shift of a floating point constant.
5839 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5842 mpfr_init(left_val);
5845 mpz_init(right_val);
5847 if (left->float_constant_value(left_val, &left_type)
5848 && right->integer_constant_value(false, right_val, &right_type)
5849 && mpfr_integer_p(left_val)
5850 && (left_type == NULL
5851 || left_type->is_abstract()
5852 || left_type->integer_type() != NULL))
5856 mpfr_get_z(left_int, left_val, GMP_RNDN);
5861 Expression* ret = NULL;
5862 if (Binary_expression::eval_integer(op, left_type, left_int,
5863 right_type, right_val,
5865 ret = Expression::make_integer(&val, left_type, location);
5867 mpz_clear(left_int);
5872 mpfr_clear(left_val);
5873 mpz_clear(right_val);
5878 mpfr_clear(left_val);
5879 mpz_clear(right_val);
5882 // Lower struct and array comparisons.
5883 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5885 if (left->type()->struct_type() != NULL)
5886 return this->lower_struct_comparison(gogo, inserter);
5887 else if (left->type()->array_type() != NULL
5888 && !left->type()->is_slice_type())
5889 return this->lower_array_comparison(gogo, inserter);
5895 // Lower a struct comparison.
5898 Binary_expression::lower_struct_comparison(Gogo* gogo,
5899 Statement_inserter* inserter)
5901 Struct_type* st = this->left_->type()->struct_type();
5902 Struct_type* st2 = this->right_->type()->struct_type();
5905 if (st != st2 && !Type::are_identical(st, st2, false, NULL))
5907 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5908 this->right_->type(), NULL))
5911 // See if we can compare using memcmp. As a heuristic, we use
5912 // memcmp rather than field references and comparisons if there are
5913 // more than two fields.
5914 if (st->compare_is_identity(gogo) && st->total_field_count() > 2)
5915 return this->lower_compare_to_memcmp(gogo, inserter);
5917 Location loc = this->location();
5919 Expression* left = this->left_;
5920 Temporary_statement* left_temp = NULL;
5921 if (left->var_expression() == NULL
5922 && left->temporary_reference_expression() == NULL)
5924 left_temp = Statement::make_temporary(left->type(), NULL, loc);
5925 inserter->insert(left_temp);
5926 left = Expression::make_set_and_use_temporary(left_temp, left, loc);
5929 Expression* right = this->right_;
5930 Temporary_statement* right_temp = NULL;
5931 if (right->var_expression() == NULL
5932 && right->temporary_reference_expression() == NULL)
5934 right_temp = Statement::make_temporary(right->type(), NULL, loc);
5935 inserter->insert(right_temp);
5936 right = Expression::make_set_and_use_temporary(right_temp, right, loc);
5939 Expression* ret = Expression::make_boolean(true, loc);
5940 const Struct_field_list* fields = st->fields();
5941 unsigned int field_index = 0;
5942 for (Struct_field_list::const_iterator pf = fields->begin();
5943 pf != fields->end();
5944 ++pf, ++field_index)
5946 if (field_index > 0)
5948 if (left_temp == NULL)
5949 left = left->copy();
5951 left = Expression::make_temporary_reference(left_temp, loc);
5952 if (right_temp == NULL)
5953 right = right->copy();
5955 right = Expression::make_temporary_reference(right_temp, loc);
5957 Expression* f1 = Expression::make_field_reference(left, field_index,
5959 Expression* f2 = Expression::make_field_reference(right, field_index,
5961 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc);
5962 ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc);
5965 if (this->op_ == OPERATOR_NOTEQ)
5966 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5971 // Lower an array comparison.
5974 Binary_expression::lower_array_comparison(Gogo* gogo,
5975 Statement_inserter* inserter)
5977 Array_type* at = this->left_->type()->array_type();
5978 Array_type* at2 = this->right_->type()->array_type();
5981 if (at != at2 && !Type::are_identical(at, at2, false, NULL))
5983 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5984 this->right_->type(), NULL))
5987 // Call memcmp directly if possible. This may let the middle-end
5988 // optimize the call.
5989 if (at->compare_is_identity(gogo))
5990 return this->lower_compare_to_memcmp(gogo, inserter);
5992 // Call the array comparison function.
5993 Named_object* hash_fn;
5994 Named_object* equal_fn;
5995 at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL,
5996 &hash_fn, &equal_fn);
5998 Location loc = this->location();
6000 Expression* func = Expression::make_func_reference(equal_fn, NULL, loc);
6002 Expression_list* args = new Expression_list();
6003 args->push_back(this->operand_address(inserter, this->left_));
6004 args->push_back(this->operand_address(inserter, this->right_));
6005 args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE));
6007 Expression* ret = Expression::make_call(func, args, false, loc);
6009 if (this->op_ == OPERATOR_NOTEQ)
6010 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
6015 // Lower a struct or array comparison to a call to memcmp.
6018 Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter)
6020 Location loc = this->location();
6022 Expression* a1 = this->operand_address(inserter, this->left_);
6023 Expression* a2 = this->operand_address(inserter, this->right_);
6024 Expression* len = Expression::make_type_info(this->left_->type(),
6027 Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len);
6030 mpz_init_set_ui(zval, 0);
6031 Expression* zero = Expression::make_integer(&zval, NULL, loc);
6034 return Expression::make_binary(this->op_, call, zero, loc);
6037 // Return the address of EXPR, cast to unsafe.Pointer.
6040 Binary_expression::operand_address(Statement_inserter* inserter,
6043 Location loc = this->location();
6045 if (!expr->is_addressable())
6047 Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL,
6049 inserter->insert(temp);
6050 expr = Expression::make_set_and_use_temporary(temp, expr, loc);
6052 expr = Expression::make_unary(OPERATOR_AND, expr, loc);
6053 static_cast<Unary_expression*>(expr)->set_does_not_escape();
6054 Type* void_type = Type::make_void_type();
6055 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
6056 return Expression::make_cast(unsafe_pointer_type, expr, loc);
6059 // Return the integer constant value, if it has one.
6062 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
6068 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
6071 mpz_clear(left_val);
6076 mpz_init(right_val);
6078 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
6081 mpz_clear(right_val);
6082 mpz_clear(left_val);
6087 if (left_type != right_type
6088 && left_type != NULL
6089 && right_type != NULL
6090 && left_type->base() != right_type->base()
6091 && this->op_ != OPERATOR_RSHIFT
6092 && this->op_ != OPERATOR_LSHIFT)
6095 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
6096 right_type, right_val,
6097 this->location(), val);
6099 mpz_clear(right_val);
6100 mpz_clear(left_val);
6108 // Return the floating point constant value, if it has one.
6111 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
6114 mpfr_init(left_val);
6116 if (!this->left_->float_constant_value(left_val, &left_type))
6118 mpfr_clear(left_val);
6123 mpfr_init(right_val);
6125 if (!this->right_->float_constant_value(right_val, &right_type))
6127 mpfr_clear(right_val);
6128 mpfr_clear(left_val);
6133 if (left_type != right_type
6134 && left_type != NULL
6135 && right_type != NULL
6136 && left_type->base() != right_type->base())
6139 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
6140 right_type, right_val,
6141 val, this->location());
6143 mpfr_clear(left_val);
6144 mpfr_clear(right_val);
6152 // Return the complex constant value, if it has one.
6155 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
6160 mpfr_init(left_real);
6161 mpfr_init(left_imag);
6163 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
6165 mpfr_clear(left_real);
6166 mpfr_clear(left_imag);
6172 mpfr_init(right_real);
6173 mpfr_init(right_imag);
6175 if (!this->right_->complex_constant_value(right_real, right_imag,
6178 mpfr_clear(left_real);
6179 mpfr_clear(left_imag);
6180 mpfr_clear(right_real);
6181 mpfr_clear(right_imag);
6186 if (left_type != right_type
6187 && left_type != NULL
6188 && right_type != NULL
6189 && left_type->base() != right_type->base())
6192 ret = Binary_expression::eval_complex(this->op_, left_type,
6193 left_real, left_imag,
6195 right_real, right_imag,
6198 mpfr_clear(left_real);
6199 mpfr_clear(left_imag);
6200 mpfr_clear(right_real);
6201 mpfr_clear(right_imag);
6209 // Note that the value is being discarded.
6212 Binary_expression::do_discarding_value()
6214 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
6215 this->right_->discarding_value();
6217 this->unused_value_error();
6223 Binary_expression::do_type()
6225 if (this->classification() == EXPRESSION_ERROR)
6226 return Type::make_error_type();
6231 case OPERATOR_ANDAND:
6233 case OPERATOR_NOTEQ:
6238 return Type::lookup_bool_type();
6241 case OPERATOR_MINUS:
6248 case OPERATOR_BITCLEAR:
6250 Type* left_type = this->left_->type();
6251 Type* right_type = this->right_->type();
6252 if (left_type->is_error())
6254 else if (right_type->is_error())
6256 else if (!Type::are_compatible_for_binop(left_type, right_type))
6258 this->report_error(_("incompatible types in binary expression"));
6259 return Type::make_error_type();
6261 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
6263 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
6265 else if (!left_type->is_abstract())
6267 else if (!right_type->is_abstract())
6269 else if (left_type->complex_type() != NULL)
6271 else if (right_type->complex_type() != NULL)
6273 else if (left_type->float_type() != NULL)
6275 else if (right_type->float_type() != NULL)
6277 else if (left_type->integer_type() != NULL
6278 && left_type->integer_type()->is_rune())
6280 else if (right_type->integer_type() != NULL
6281 && right_type->integer_type()->is_rune())
6287 case OPERATOR_LSHIFT:
6288 case OPERATOR_RSHIFT:
6289 return this->left_->type();
6296 // Set type for a binary expression.
6299 Binary_expression::do_determine_type(const Type_context* context)
6301 Type* tleft = this->left_->type();
6302 Type* tright = this->right_->type();
6304 // Both sides should have the same type, except for the shift
6305 // operations. For a comparison, we should ignore the incoming
6308 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
6309 || this->op_ == OPERATOR_RSHIFT);
6311 bool is_comparison = (this->op_ == OPERATOR_EQEQ
6312 || this->op_ == OPERATOR_NOTEQ
6313 || this->op_ == OPERATOR_LT
6314 || this->op_ == OPERATOR_LE
6315 || this->op_ == OPERATOR_GT
6316 || this->op_ == OPERATOR_GE);
6318 Type_context subcontext(*context);
6322 // In a comparison, the context does not determine the types of
6324 subcontext.type = NULL;
6327 // Set the context for the left hand operand.
6330 // The right hand operand of a shift plays no role in
6331 // determining the type of the left hand operand.
6333 else if (!tleft->is_abstract())
6334 subcontext.type = tleft;
6335 else if (!tright->is_abstract())
6336 subcontext.type = tright;
6337 else if (subcontext.type == NULL)
6339 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
6340 || (tleft->float_type() != NULL && tright->float_type() != NULL)
6341 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
6343 // Both sides have an abstract integer, abstract float, or
6344 // abstract complex type. Just let CONTEXT determine
6345 // whether they may remain abstract or not.
6347 else if (tleft->complex_type() != NULL)
6348 subcontext.type = tleft;
6349 else if (tright->complex_type() != NULL)
6350 subcontext.type = tright;
6351 else if (tleft->float_type() != NULL)
6352 subcontext.type = tleft;
6353 else if (tright->float_type() != NULL)
6354 subcontext.type = tright;
6356 subcontext.type = tleft;
6358 if (subcontext.type != NULL && !context->may_be_abstract)
6359 subcontext.type = subcontext.type->make_non_abstract_type();
6362 this->left_->determine_type(&subcontext);
6366 // We may have inherited an unusable type for the shift operand.
6367 // Give a useful error if that happened.
6368 if (tleft->is_abstract()
6369 && subcontext.type != NULL
6370 && (this->left_->type()->integer_type() == NULL
6371 || (subcontext.type->integer_type() == NULL
6372 && subcontext.type->float_type() == NULL
6373 && subcontext.type->complex_type() == NULL)))
6374 this->report_error(("invalid context-determined non-integer type "
6375 "for shift operand"));
6377 // The context for the right hand operand is the same as for the
6378 // left hand operand, except for a shift operator.
6379 subcontext.type = Type::lookup_integer_type("uint");
6380 subcontext.may_be_abstract = false;
6383 this->right_->determine_type(&subcontext);
6386 // Report an error if the binary operator OP does not support TYPE.
6387 // OTYPE is the type of the other operand. Return whether the
6388 // operation is OK. This should not be used for shift.
6391 Binary_expression::check_operator_type(Operator op, Type* type, Type* otype,
6397 case OPERATOR_ANDAND:
6398 if (!type->is_boolean_type())
6400 error_at(location, "expected boolean type");
6406 case OPERATOR_NOTEQ:
6409 if (!Type::are_compatible_for_comparison(true, type, otype, &reason))
6411 error_at(location, "%s", reason.c_str());
6423 if (!Type::are_compatible_for_comparison(false, type, otype, &reason))
6425 error_at(location, "%s", reason.c_str());
6432 case OPERATOR_PLUSEQ:
6433 if (type->integer_type() == NULL
6434 && type->float_type() == NULL
6435 && type->complex_type() == NULL
6436 && !type->is_string_type())
6439 "expected integer, floating, complex, or string type");
6444 case OPERATOR_MINUS:
6445 case OPERATOR_MINUSEQ:
6447 case OPERATOR_MULTEQ:
6449 case OPERATOR_DIVEQ:
6450 if (type->integer_type() == NULL
6451 && type->float_type() == NULL
6452 && type->complex_type() == NULL)
6454 error_at(location, "expected integer, floating, or complex type");
6460 case OPERATOR_MODEQ:
6464 case OPERATOR_ANDEQ:
6466 case OPERATOR_XOREQ:
6467 case OPERATOR_BITCLEAR:
6468 case OPERATOR_BITCLEAREQ:
6469 if (type->integer_type() == NULL)
6471 error_at(location, "expected integer type");
6486 Binary_expression::do_check_types(Gogo*)
6488 if (this->classification() == EXPRESSION_ERROR)
6491 Type* left_type = this->left_->type();
6492 Type* right_type = this->right_->type();
6493 if (left_type->is_error() || right_type->is_error())
6495 this->set_is_error();
6499 if (this->op_ == OPERATOR_EQEQ
6500 || this->op_ == OPERATOR_NOTEQ
6501 || this->op_ == OPERATOR_LT
6502 || this->op_ == OPERATOR_LE
6503 || this->op_ == OPERATOR_GT
6504 || this->op_ == OPERATOR_GE)
6506 if (!Type::are_assignable(left_type, right_type, NULL)
6507 && !Type::are_assignable(right_type, left_type, NULL))
6509 this->report_error(_("incompatible types in binary expression"));
6512 if (!Binary_expression::check_operator_type(this->op_, left_type,
6515 || !Binary_expression::check_operator_type(this->op_, right_type,
6519 this->set_is_error();
6523 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
6525 if (!Type::are_compatible_for_binop(left_type, right_type))
6527 this->report_error(_("incompatible types in binary expression"));
6530 if (!Binary_expression::check_operator_type(this->op_, left_type,
6534 this->set_is_error();
6540 if (left_type->integer_type() == NULL)
6541 this->report_error(_("shift of non-integer operand"));
6543 if (!right_type->is_abstract()
6544 && (right_type->integer_type() == NULL
6545 || !right_type->integer_type()->is_unsigned()))
6546 this->report_error(_("shift count not unsigned integer"));
6552 if (this->right_->integer_constant_value(true, val, &type))
6554 if (mpz_sgn(val) < 0)
6556 this->report_error(_("negative shift count"));
6558 Location rloc = this->right_->location();
6559 this->right_ = Expression::make_integer(&val, right_type,
6568 // Get a tree for a binary expression.
6571 Binary_expression::do_get_tree(Translate_context* context)
6573 tree left = this->left_->get_tree(context);
6574 tree right = this->right_->get_tree(context);
6576 if (left == error_mark_node || right == error_mark_node)
6577 return error_mark_node;
6579 enum tree_code code;
6580 bool use_left_type = true;
6581 bool is_shift_op = false;
6585 case OPERATOR_NOTEQ:
6590 return Expression::comparison_tree(context, this->op_,
6591 this->left_->type(), left,
6592 this->right_->type(), right,
6596 code = TRUTH_ORIF_EXPR;
6597 use_left_type = false;
6599 case OPERATOR_ANDAND:
6600 code = TRUTH_ANDIF_EXPR;
6601 use_left_type = false;
6606 case OPERATOR_MINUS:
6610 code = BIT_IOR_EXPR;
6613 code = BIT_XOR_EXPR;
6620 Type *t = this->left_->type();
6621 if (t->float_type() != NULL || t->complex_type() != NULL)
6624 code = TRUNC_DIV_EXPR;
6628 code = TRUNC_MOD_EXPR;
6630 case OPERATOR_LSHIFT:
6634 case OPERATOR_RSHIFT:
6639 code = BIT_AND_EXPR;
6641 case OPERATOR_BITCLEAR:
6642 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
6643 code = BIT_AND_EXPR;
6649 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
6651 if (this->left_->type()->is_string_type())
6653 go_assert(this->op_ == OPERATOR_PLUS);
6654 Type* st = Type::make_string_type();
6655 tree string_type = type_to_tree(st->get_backend(context->gogo()));
6656 static tree string_plus_decl;
6657 return Gogo::call_builtin(&string_plus_decl,
6668 tree compute_type = excess_precision_type(type);
6669 if (compute_type != NULL_TREE)
6671 left = ::convert(compute_type, left);
6672 right = ::convert(compute_type, right);
6675 tree eval_saved = NULL_TREE;
6678 // Make sure the values are evaluated.
6679 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
6681 left = save_expr(left);
6684 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
6686 right = save_expr(right);
6687 if (eval_saved == NULL_TREE)
6690 eval_saved = fold_build2_loc(this->location().gcc_location(),
6692 void_type_node, eval_saved, right);
6696 tree ret = fold_build2_loc(this->location().gcc_location(),
6698 compute_type != NULL_TREE ? compute_type : type,
6701 if (compute_type != NULL_TREE)
6702 ret = ::convert(type, ret);
6704 // In Go, a shift larger than the size of the type is well-defined.
6705 // This is not true in GENERIC, so we need to insert a conditional.
6708 go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
6709 go_assert(this->left_->type()->integer_type() != NULL);
6710 int bits = TYPE_PRECISION(TREE_TYPE(left));
6712 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
6713 build_int_cst_type(TREE_TYPE(right), bits));
6715 tree overflow_result = fold_convert_loc(this->location().gcc_location(),
6718 if (this->op_ == OPERATOR_RSHIFT
6719 && !this->left_->type()->integer_type()->is_unsigned())
6722 fold_build2_loc(this->location().gcc_location(), LT_EXPR,
6723 boolean_type_node, left,
6724 fold_convert_loc(this->location().gcc_location(),
6726 integer_zero_node));
6728 fold_build2_loc(this->location().gcc_location(),
6729 MINUS_EXPR, TREE_TYPE(left),
6730 fold_convert_loc(this->location().gcc_location(),
6733 fold_convert_loc(this->location().gcc_location(),
6737 fold_build3_loc(this->location().gcc_location(), COND_EXPR,
6738 TREE_TYPE(left), neg, neg_one,
6742 ret = fold_build3_loc(this->location().gcc_location(), COND_EXPR,
6743 TREE_TYPE(left), compare, ret, overflow_result);
6745 if (eval_saved != NULL_TREE)
6746 ret = fold_build2_loc(this->location().gcc_location(), COMPOUND_EXPR,
6747 TREE_TYPE(ret), eval_saved, ret);
6753 // Export a binary expression.
6756 Binary_expression::do_export(Export* exp) const
6758 exp->write_c_string("(");
6759 this->left_->export_expression(exp);
6763 exp->write_c_string(" || ");
6765 case OPERATOR_ANDAND:
6766 exp->write_c_string(" && ");
6769 exp->write_c_string(" == ");
6771 case OPERATOR_NOTEQ:
6772 exp->write_c_string(" != ");
6775 exp->write_c_string(" < ");
6778 exp->write_c_string(" <= ");
6781 exp->write_c_string(" > ");
6784 exp->write_c_string(" >= ");
6787 exp->write_c_string(" + ");
6789 case OPERATOR_MINUS:
6790 exp->write_c_string(" - ");
6793 exp->write_c_string(" | ");
6796 exp->write_c_string(" ^ ");
6799 exp->write_c_string(" * ");
6802 exp->write_c_string(" / ");
6805 exp->write_c_string(" % ");
6807 case OPERATOR_LSHIFT:
6808 exp->write_c_string(" << ");
6810 case OPERATOR_RSHIFT:
6811 exp->write_c_string(" >> ");
6814 exp->write_c_string(" & ");
6816 case OPERATOR_BITCLEAR:
6817 exp->write_c_string(" &^ ");
6822 this->right_->export_expression(exp);
6823 exp->write_c_string(")");
6826 // Import a binary expression.
6829 Binary_expression::do_import(Import* imp)
6831 imp->require_c_string("(");
6833 Expression* left = Expression::import_expression(imp);
6836 if (imp->match_c_string(" || "))
6841 else if (imp->match_c_string(" && "))
6843 op = OPERATOR_ANDAND;
6846 else if (imp->match_c_string(" == "))
6851 else if (imp->match_c_string(" != "))
6853 op = OPERATOR_NOTEQ;
6856 else if (imp->match_c_string(" < "))
6861 else if (imp->match_c_string(" <= "))
6866 else if (imp->match_c_string(" > "))
6871 else if (imp->match_c_string(" >= "))
6876 else if (imp->match_c_string(" + "))
6881 else if (imp->match_c_string(" - "))
6883 op = OPERATOR_MINUS;
6886 else if (imp->match_c_string(" | "))
6891 else if (imp->match_c_string(" ^ "))
6896 else if (imp->match_c_string(" * "))
6901 else if (imp->match_c_string(" / "))
6906 else if (imp->match_c_string(" % "))
6911 else if (imp->match_c_string(" << "))
6913 op = OPERATOR_LSHIFT;
6916 else if (imp->match_c_string(" >> "))
6918 op = OPERATOR_RSHIFT;
6921 else if (imp->match_c_string(" & "))
6926 else if (imp->match_c_string(" &^ "))
6928 op = OPERATOR_BITCLEAR;
6933 error_at(imp->location(), "unrecognized binary operator");
6934 return Expression::make_error(imp->location());
6937 Expression* right = Expression::import_expression(imp);
6939 imp->require_c_string(")");
6941 return Expression::make_binary(op, left, right, imp->location());
6944 // Dump ast representation of a binary expression.
6947 Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
6949 ast_dump_context->ostream() << "(";
6950 ast_dump_context->dump_expression(this->left_);
6951 ast_dump_context->ostream() << " ";
6952 ast_dump_context->dump_operator(this->op_);
6953 ast_dump_context->ostream() << " ";
6954 ast_dump_context->dump_expression(this->right_);
6955 ast_dump_context->ostream() << ") ";
6958 // Make a binary expression.
6961 Expression::make_binary(Operator op, Expression* left, Expression* right,
6964 return new Binary_expression(op, left, right, location);
6967 // Implement a comparison.
6970 Expression::comparison_tree(Translate_context* context, Operator op,
6971 Type* left_type, tree left_tree,
6972 Type* right_type, tree right_tree,
6975 enum tree_code code;
6981 case OPERATOR_NOTEQ:
7000 if (left_type->is_string_type() && right_type->is_string_type())
7002 Type* st = Type::make_string_type();
7003 tree string_type = type_to_tree(st->get_backend(context->gogo()));
7004 static tree string_compare_decl;
7005 left_tree = Gogo::call_builtin(&string_compare_decl,
7014 right_tree = build_int_cst_type(integer_type_node, 0);
7016 else if ((left_type->interface_type() != NULL
7017 && right_type->interface_type() == NULL
7018 && !right_type->is_nil_type())
7019 || (left_type->interface_type() == NULL
7020 && !left_type->is_nil_type()
7021 && right_type->interface_type() != NULL))
7023 // Comparing an interface value to a non-interface value.
7024 if (left_type->interface_type() == NULL)
7026 std::swap(left_type, right_type);
7027 std::swap(left_tree, right_tree);
7030 // The right operand is not an interface. We need to take its
7031 // address if it is not a pointer.
7034 if (right_type->points_to() != NULL)
7036 make_tmp = NULL_TREE;
7039 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
7041 make_tmp = NULL_TREE;
7042 arg = build_fold_addr_expr_loc(location.gcc_location(), right_tree);
7043 if (DECL_P(right_tree))
7044 TREE_ADDRESSABLE(right_tree) = 1;
7048 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
7049 get_name(right_tree));
7050 DECL_IGNORED_P(tmp) = 0;
7051 DECL_INITIAL(tmp) = right_tree;
7052 TREE_ADDRESSABLE(tmp) = 1;
7053 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
7054 SET_EXPR_LOCATION(make_tmp, location.gcc_location());
7055 arg = build_fold_addr_expr_loc(location.gcc_location(), tmp);
7057 arg = fold_convert_loc(location.gcc_location(), ptr_type_node, arg);
7059 tree descriptor = right_type->type_descriptor_pointer(context->gogo(),
7062 if (left_type->interface_type()->is_empty())
7064 static tree empty_interface_value_compare_decl;
7065 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
7067 "__go_empty_interface_value_compare",
7070 TREE_TYPE(left_tree),
7072 TREE_TYPE(descriptor),
7076 if (left_tree == error_mark_node)
7077 return error_mark_node;
7078 // This can panic if the type is not comparable.
7079 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
7083 static tree interface_value_compare_decl;
7084 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
7086 "__go_interface_value_compare",
7089 TREE_TYPE(left_tree),
7091 TREE_TYPE(descriptor),
7095 if (left_tree == error_mark_node)
7096 return error_mark_node;
7097 // This can panic if the type is not comparable.
7098 TREE_NOTHROW(interface_value_compare_decl) = 0;
7100 right_tree = build_int_cst_type(integer_type_node, 0);
7102 if (make_tmp != NULL_TREE)
7103 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
7106 else if (left_type->interface_type() != NULL
7107 && right_type->interface_type() != NULL)
7109 if (left_type->interface_type()->is_empty()
7110 && right_type->interface_type()->is_empty())
7112 static tree empty_interface_compare_decl;
7113 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
7115 "__go_empty_interface_compare",
7118 TREE_TYPE(left_tree),
7120 TREE_TYPE(right_tree),
7122 if (left_tree == error_mark_node)
7123 return error_mark_node;
7124 // This can panic if the type is uncomparable.
7125 TREE_NOTHROW(empty_interface_compare_decl) = 0;
7127 else if (!left_type->interface_type()->is_empty()
7128 && !right_type->interface_type()->is_empty())
7130 static tree interface_compare_decl;
7131 left_tree = Gogo::call_builtin(&interface_compare_decl,
7133 "__go_interface_compare",
7136 TREE_TYPE(left_tree),
7138 TREE_TYPE(right_tree),
7140 if (left_tree == error_mark_node)
7141 return error_mark_node;
7142 // This can panic if the type is uncomparable.
7143 TREE_NOTHROW(interface_compare_decl) = 0;
7147 if (left_type->interface_type()->is_empty())
7149 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
7150 std::swap(left_type, right_type);
7151 std::swap(left_tree, right_tree);
7153 go_assert(!left_type->interface_type()->is_empty());
7154 go_assert(right_type->interface_type()->is_empty());
7155 static tree interface_empty_compare_decl;
7156 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
7158 "__go_interface_empty_compare",
7161 TREE_TYPE(left_tree),
7163 TREE_TYPE(right_tree),
7165 if (left_tree == error_mark_node)
7166 return error_mark_node;
7167 // This can panic if the type is uncomparable.
7168 TREE_NOTHROW(interface_empty_compare_decl) = 0;
7171 right_tree = build_int_cst_type(integer_type_node, 0);
7174 if (left_type->is_nil_type()
7175 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
7177 std::swap(left_type, right_type);
7178 std::swap(left_tree, right_tree);
7181 if (right_type->is_nil_type())
7183 if (left_type->array_type() != NULL
7184 && left_type->array_type()->length() == NULL)
7186 Array_type* at = left_type->array_type();
7187 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
7188 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
7190 else if (left_type->interface_type() != NULL)
7192 // An interface is nil if the first field is nil.
7193 tree left_type_tree = TREE_TYPE(left_tree);
7194 go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
7195 tree field = TYPE_FIELDS(left_type_tree);
7196 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
7198 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
7202 go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
7203 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
7207 if (left_tree == error_mark_node || right_tree == error_mark_node)
7208 return error_mark_node;
7210 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
7211 if (CAN_HAVE_LOCATION_P(ret))
7212 SET_EXPR_LOCATION(ret, location.gcc_location());
7216 // Class Bound_method_expression.
7221 Bound_method_expression::do_traverse(Traverse* traverse)
7223 return Expression::traverse(&this->expr_, traverse);
7226 // Return the type of a bound method expression. The type of this
7227 // object is really the type of the method with no receiver. We
7228 // should be able to get away with just returning the type of the
7232 Bound_method_expression::do_type()
7234 if (this->method_->is_function())
7235 return this->method_->func_value()->type();
7236 else if (this->method_->is_function_declaration())
7237 return this->method_->func_declaration_value()->type();
7239 return Type::make_error_type();
7242 // Determine the types of a method expression.
7245 Bound_method_expression::do_determine_type(const Type_context*)
7247 Function_type* fntype = this->type()->function_type();
7248 if (fntype == NULL || !fntype->is_method())
7249 this->expr_->determine_type_no_context();
7252 Type_context subcontext(fntype->receiver()->type(), false);
7253 this->expr_->determine_type(&subcontext);
7257 // Check the types of a method expression.
7260 Bound_method_expression::do_check_types(Gogo*)
7262 if (!this->method_->is_function()
7263 && !this->method_->is_function_declaration())
7264 this->report_error(_("object is not a method"));
7267 Type* rtype = this->type()->function_type()->receiver()->type()->deref();
7268 Type* etype = (this->expr_type_ != NULL
7270 : this->expr_->type());
7271 etype = etype->deref();
7272 if (!Type::are_identical(rtype, etype, true, NULL))
7273 this->report_error(_("method type does not match object type"));
7277 // Get the tree for a method expression. There is no standard tree
7278 // representation for this. The only places it may currently be used
7279 // are in a Call_expression or a Go_statement, which will take it
7280 // apart directly. So this has nothing to do at present.
7283 Bound_method_expression::do_get_tree(Translate_context*)
7285 error_at(this->location(), "reference to method other than calling it");
7286 return error_mark_node;
7289 // Dump ast representation of a bound method expression.
7292 Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
7295 if (this->expr_type_ != NULL)
7296 ast_dump_context->ostream() << "(";
7297 ast_dump_context->dump_expression(this->expr_);
7298 if (this->expr_type_ != NULL)
7300 ast_dump_context->ostream() << ":";
7301 ast_dump_context->dump_type(this->expr_type_);
7302 ast_dump_context->ostream() << ")";
7305 ast_dump_context->ostream() << "." << this->method_->name();
7308 // Make a method expression.
7310 Bound_method_expression*
7311 Expression::make_bound_method(Expression* expr, Named_object* method,
7314 return new Bound_method_expression(expr, method, location);
7317 // Class Builtin_call_expression. This is used for a call to a
7318 // builtin function.
7320 class Builtin_call_expression : public Call_expression
7323 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
7324 bool is_varargs, Location location);
7327 // This overrides Call_expression::do_lower.
7329 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
7332 do_is_constant() const;
7335 do_integer_constant_value(bool, mpz_t, Type**) const;
7338 do_float_constant_value(mpfr_t, Type**) const;
7341 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
7344 do_discarding_value();
7350 do_determine_type(const Type_context*);
7353 do_check_types(Gogo*);
7358 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
7359 this->args()->copy(),
7365 do_get_tree(Translate_context*);
7368 do_export(Export*) const;
7371 do_is_recover_call() const;
7374 do_set_recover_arg(Expression*);
7377 // The builtin functions.
7378 enum Builtin_function_code
7382 // Predeclared builtin functions.
7399 // Builtin functions from the unsafe package.
7412 real_imag_type(Type*);
7415 complex_type(Type*);
7421 check_int_value(Expression*);
7423 // A pointer back to the general IR structure. This avoids a global
7424 // variable, or passing it around everywhere.
7426 // The builtin function being called.
7427 Builtin_function_code code_;
7428 // Used to stop endless loops when the length of an array uses len
7429 // or cap of the array itself.
7433 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
7435 Expression_list* args,
7438 : Call_expression(fn, args, is_varargs, location),
7439 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
7441 Func_expression* fnexp = this->fn()->func_expression();
7442 go_assert(fnexp != NULL);
7443 const std::string& name(fnexp->named_object()->name());
7444 if (name == "append")
7445 this->code_ = BUILTIN_APPEND;
7446 else if (name == "cap")
7447 this->code_ = BUILTIN_CAP;
7448 else if (name == "close")
7449 this->code_ = BUILTIN_CLOSE;
7450 else if (name == "complex")
7451 this->code_ = BUILTIN_COMPLEX;
7452 else if (name == "copy")
7453 this->code_ = BUILTIN_COPY;
7454 else if (name == "delete")
7455 this->code_ = BUILTIN_DELETE;
7456 else if (name == "imag")
7457 this->code_ = BUILTIN_IMAG;
7458 else if (name == "len")
7459 this->code_ = BUILTIN_LEN;
7460 else if (name == "make")
7461 this->code_ = BUILTIN_MAKE;
7462 else if (name == "new")
7463 this->code_ = BUILTIN_NEW;
7464 else if (name == "panic")
7465 this->code_ = BUILTIN_PANIC;
7466 else if (name == "print")
7467 this->code_ = BUILTIN_PRINT;
7468 else if (name == "println")
7469 this->code_ = BUILTIN_PRINTLN;
7470 else if (name == "real")
7471 this->code_ = BUILTIN_REAL;
7472 else if (name == "recover")
7473 this->code_ = BUILTIN_RECOVER;
7474 else if (name == "Alignof")
7475 this->code_ = BUILTIN_ALIGNOF;
7476 else if (name == "Offsetof")
7477 this->code_ = BUILTIN_OFFSETOF;
7478 else if (name == "Sizeof")
7479 this->code_ = BUILTIN_SIZEOF;
7484 // Return whether this is a call to recover. This is a virtual
7485 // function called from the parent class.
7488 Builtin_call_expression::do_is_recover_call() const
7490 if (this->classification() == EXPRESSION_ERROR)
7492 return this->code_ == BUILTIN_RECOVER;
7495 // Set the argument for a call to recover.
7498 Builtin_call_expression::do_set_recover_arg(Expression* arg)
7500 const Expression_list* args = this->args();
7501 go_assert(args == NULL || args->empty());
7502 Expression_list* new_args = new Expression_list();
7503 new_args->push_back(arg);
7504 this->set_args(new_args);
7507 // A traversal class which looks for a call expression.
7509 class Find_call_expression : public Traverse
7512 Find_call_expression()
7513 : Traverse(traverse_expressions),
7518 expression(Expression**);
7522 { return this->found_; }
7529 Find_call_expression::expression(Expression** pexpr)
7531 if ((*pexpr)->call_expression() != NULL)
7533 this->found_ = true;
7534 return TRAVERSE_EXIT;
7536 return TRAVERSE_CONTINUE;
7539 // Lower a builtin call expression. This turns new and make into
7540 // specific expressions. We also convert to a constant if we can.
7543 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function,
7544 Statement_inserter* inserter, int)
7546 if (this->classification() == EXPRESSION_ERROR)
7549 Location loc = this->location();
7551 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
7553 this->report_error(_("invalid use of %<...%> with builtin function"));
7554 return Expression::make_error(loc);
7557 if (this->is_constant())
7559 // We can only lower len and cap if there are no function calls
7560 // in the arguments. Otherwise we have to make the call.
7561 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
7563 Expression* arg = this->one_arg();
7564 if (!arg->is_constant())
7566 Find_call_expression find_call;
7567 Expression::traverse(&arg, &find_call);
7568 if (find_call.found())
7576 if (this->integer_constant_value(true, ival, &type))
7578 Expression* ret = Expression::make_integer(&ival, type, loc);
7586 if (this->float_constant_value(rval, &type))
7588 Expression* ret = Expression::make_float(&rval, type, loc);
7595 if (this->complex_constant_value(rval, imag, &type))
7597 Expression* ret = Expression::make_complex(&rval, &imag, type, loc);
7606 switch (this->code_)
7613 const Expression_list* args = this->args();
7614 if (args == NULL || args->size() < 1)
7615 this->report_error(_("not enough arguments"));
7616 else if (args->size() > 1)
7617 this->report_error(_("too many arguments"));
7620 Expression* arg = args->front();
7621 if (!arg->is_type_expression())
7623 error_at(arg->location(), "expected type");
7624 this->set_is_error();
7627 return Expression::make_allocation(arg->type(), loc);
7633 return this->lower_make();
7635 case BUILTIN_RECOVER:
7636 if (function != NULL)
7637 function->func_value()->set_calls_recover();
7640 // Calling recover outside of a function always returns the
7641 // nil empty interface.
7642 Type* eface = Type::make_empty_interface_type(loc);
7643 return Expression::make_cast(eface, Expression::make_nil(loc), loc);
7647 case BUILTIN_APPEND:
7649 // Lower the varargs.
7650 const Expression_list* args = this->args();
7651 if (args == NULL || args->empty())
7653 Type* slice_type = args->front()->type();
7654 if (!slice_type->is_slice_type())
7656 error_at(args->front()->location(), "argument 1 must be a slice");
7657 this->set_is_error();
7660 Type* element_type = slice_type->array_type()->element_type();
7661 this->lower_varargs(gogo, function, inserter,
7662 Type::make_array_type(element_type, NULL),
7667 case BUILTIN_DELETE:
7669 // Lower to a runtime function call.
7670 const Expression_list* args = this->args();
7671 if (args == NULL || args->size() < 2)
7672 this->report_error(_("not enough arguments"));
7673 else if (args->size() > 2)
7674 this->report_error(_("too many arguments"));
7675 else if (args->front()->type()->map_type() == NULL)
7676 this->report_error(_("argument 1 must be a map"));
7679 // Since this function returns no value it must appear in
7680 // a statement by itself, so we don't have to worry about
7681 // order of evaluation of values around it. Evaluate the
7682 // map first to get order of evaluation right.
7683 Map_type* mt = args->front()->type()->map_type();
7684 Temporary_statement* map_temp =
7685 Statement::make_temporary(mt, args->front(), loc);
7686 inserter->insert(map_temp);
7688 Temporary_statement* key_temp =
7689 Statement::make_temporary(mt->key_type(), args->back(), loc);
7690 inserter->insert(key_temp);
7692 Expression* e1 = Expression::make_temporary_reference(map_temp,
7694 Expression* e2 = Expression::make_temporary_reference(key_temp,
7696 e2 = Expression::make_unary(OPERATOR_AND, e2, loc);
7697 return Runtime::make_call(Runtime::MAPDELETE, this->location(),
7707 // Lower a make expression.
7710 Builtin_call_expression::lower_make()
7712 Location loc = this->location();
7714 const Expression_list* args = this->args();
7715 if (args == NULL || args->size() < 1)
7717 this->report_error(_("not enough arguments"));
7718 return Expression::make_error(this->location());
7721 Expression_list::const_iterator parg = args->begin();
7723 Expression* first_arg = *parg;
7724 if (!first_arg->is_type_expression())
7726 error_at(first_arg->location(), "expected type");
7727 this->set_is_error();
7728 return Expression::make_error(this->location());
7730 Type* type = first_arg->type();
7732 bool is_slice = false;
7733 bool is_map = false;
7734 bool is_chan = false;
7735 if (type->is_slice_type())
7737 else if (type->map_type() != NULL)
7739 else if (type->channel_type() != NULL)
7743 this->report_error(_("invalid type for make function"));
7744 return Expression::make_error(this->location());
7747 bool have_big_args = false;
7748 Type* uintptr_type = Type::lookup_integer_type("uintptr");
7749 int uintptr_bits = uintptr_type->integer_type()->bits();
7752 Expression* len_arg;
7753 if (parg == args->end())
7757 this->report_error(_("length required when allocating a slice"));
7758 return Expression::make_error(this->location());
7762 mpz_init_set_ui(zval, 0);
7763 len_arg = Expression::make_integer(&zval, NULL, loc);
7769 if (!this->check_int_value(len_arg))
7771 this->report_error(_("bad size for make"));
7772 return Expression::make_error(this->location());
7774 if (len_arg->type()->integer_type() != NULL
7775 && len_arg->type()->integer_type()->bits() > uintptr_bits)
7776 have_big_args = true;
7780 Expression* cap_arg = NULL;
7781 if (is_slice && parg != args->end())
7784 if (!this->check_int_value(cap_arg))
7786 this->report_error(_("bad capacity when making slice"));
7787 return Expression::make_error(this->location());
7789 if (cap_arg->type()->integer_type() != NULL
7790 && cap_arg->type()->integer_type()->bits() > uintptr_bits)
7791 have_big_args = true;
7795 if (parg != args->end())
7797 this->report_error(_("too many arguments to make"));
7798 return Expression::make_error(this->location());
7801 Location type_loc = first_arg->location();
7802 Expression* type_arg;
7803 if (is_slice || is_chan)
7804 type_arg = Expression::make_type_descriptor(type, type_loc);
7806 type_arg = Expression::make_map_descriptor(type->map_type(), type_loc);
7813 if (cap_arg == NULL)
7814 call = Runtime::make_call((have_big_args
7815 ? Runtime::MAKESLICE1BIG
7816 : Runtime::MAKESLICE1),
7817 loc, 2, type_arg, len_arg);
7819 call = Runtime::make_call((have_big_args
7820 ? Runtime::MAKESLICE2BIG
7821 : Runtime::MAKESLICE2),
7822 loc, 3, type_arg, len_arg, cap_arg);
7825 call = Runtime::make_call((have_big_args
7826 ? Runtime::MAKEMAPBIG
7827 : Runtime::MAKEMAP),
7828 loc, 2, type_arg, len_arg);
7830 call = Runtime::make_call((have_big_args
7831 ? Runtime::MAKECHANBIG
7832 : Runtime::MAKECHAN),
7833 loc, 2, type_arg, len_arg);
7837 return Expression::make_unsafe_cast(type, call, loc);
7840 // Return whether an expression has an integer value. Report an error
7841 // if not. This is used when handling calls to the predeclared make
7845 Builtin_call_expression::check_int_value(Expression* e)
7847 if (e->type()->integer_type() != NULL)
7850 // Check for a floating point constant with integer value.
7855 if (e->float_constant_value(fval, &dummy) && mpfr_integer_p(fval))
7862 mpfr_clear_overflow();
7863 mpfr_clear_erangeflag();
7864 mpfr_get_z(ival, fval, GMP_RNDN);
7865 if (!mpfr_overflow_p()
7866 && !mpfr_erangeflag_p()
7867 && mpz_sgn(ival) >= 0)
7869 Named_type* ntype = Type::lookup_integer_type("int");
7870 Integer_type* inttype = ntype->integer_type();
7872 mpz_init_set_ui(max, 1);
7873 mpz_mul_2exp(max, max, inttype->bits() - 1);
7874 ok = mpz_cmp(ival, max) < 0;
7891 // Return the type of the real or imag functions, given the type of
7892 // the argument. We need to map complex to float, complex64 to
7893 // float32, and complex128 to float64, so it has to be done by name.
7894 // This returns NULL if it can't figure out the type.
7897 Builtin_call_expression::real_imag_type(Type* arg_type)
7899 if (arg_type == NULL || arg_type->is_abstract())
7901 Named_type* nt = arg_type->named_type();
7904 while (nt->real_type()->named_type() != NULL)
7905 nt = nt->real_type()->named_type();
7906 if (nt->name() == "complex64")
7907 return Type::lookup_float_type("float32");
7908 else if (nt->name() == "complex128")
7909 return Type::lookup_float_type("float64");
7914 // Return the type of the complex function, given the type of one of the
7915 // argments. Like real_imag_type, we have to map by name.
7918 Builtin_call_expression::complex_type(Type* arg_type)
7920 if (arg_type == NULL || arg_type->is_abstract())
7922 Named_type* nt = arg_type->named_type();
7925 while (nt->real_type()->named_type() != NULL)
7926 nt = nt->real_type()->named_type();
7927 if (nt->name() == "float32")
7928 return Type::lookup_complex_type("complex64");
7929 else if (nt->name() == "float64")
7930 return Type::lookup_complex_type("complex128");
7935 // Return a single argument, or NULL if there isn't one.
7938 Builtin_call_expression::one_arg() const
7940 const Expression_list* args = this->args();
7941 if (args->size() != 1)
7943 return args->front();
7946 // Return whether this is constant: len of a string, or len or cap of
7947 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7950 Builtin_call_expression::do_is_constant() const
7952 switch (this->code_)
7960 Expression* arg = this->one_arg();
7963 Type* arg_type = arg->type();
7965 if (arg_type->points_to() != NULL
7966 && arg_type->points_to()->array_type() != NULL
7967 && !arg_type->points_to()->is_slice_type())
7968 arg_type = arg_type->points_to();
7970 if (arg_type->array_type() != NULL
7971 && arg_type->array_type()->length() != NULL)
7974 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7977 bool ret = arg->is_constant();
7978 this->seen_ = false;
7984 case BUILTIN_SIZEOF:
7985 case BUILTIN_ALIGNOF:
7986 return this->one_arg() != NULL;
7988 case BUILTIN_OFFSETOF:
7990 Expression* arg = this->one_arg();
7993 return arg->field_reference_expression() != NULL;
7996 case BUILTIN_COMPLEX:
7998 const Expression_list* args = this->args();
7999 if (args != NULL && args->size() == 2)
8000 return args->front()->is_constant() && args->back()->is_constant();
8007 Expression* arg = this->one_arg();
8008 return arg != NULL && arg->is_constant();
8018 // Return an integer constant value if possible.
8021 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
8025 if (this->code_ == BUILTIN_LEN
8026 || this->code_ == BUILTIN_CAP)
8028 Expression* arg = this->one_arg();
8031 Type* arg_type = arg->type();
8033 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
8036 if (arg->string_constant_value(&sval))
8038 mpz_set_ui(val, sval.length());
8039 *ptype = Type::lookup_integer_type("int");
8044 if (arg_type->points_to() != NULL
8045 && arg_type->points_to()->array_type() != NULL
8046 && !arg_type->points_to()->is_slice_type())
8047 arg_type = arg_type->points_to();
8049 if (arg_type->array_type() != NULL
8050 && arg_type->array_type()->length() != NULL)
8054 Expression* e = arg_type->array_type()->length();
8056 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
8057 this->seen_ = false;
8060 *ptype = Type::lookup_integer_type("int");
8065 else if (this->code_ == BUILTIN_SIZEOF
8066 || this->code_ == BUILTIN_ALIGNOF)
8068 Expression* arg = this->one_arg();
8071 Type* arg_type = arg->type();
8072 if (arg_type->is_error())
8074 if (arg_type->is_abstract())
8076 if (arg_type->named_type() != NULL)
8077 arg_type->named_type()->convert(this->gogo_);
8080 if (this->code_ == BUILTIN_SIZEOF)
8082 if (!arg_type->backend_type_size(this->gogo_, &ret))
8085 else if (this->code_ == BUILTIN_ALIGNOF)
8087 if (arg->field_reference_expression() == NULL)
8089 if (!arg_type->backend_type_align(this->gogo_, &ret))
8094 // Calling unsafe.Alignof(s.f) returns the alignment of
8095 // the type of f when it is used as a field in a struct.
8096 if (!arg_type->backend_type_field_align(this->gogo_, &ret))
8103 mpz_set_ui(val, ret);
8107 else if (this->code_ == BUILTIN_OFFSETOF)
8109 Expression* arg = this->one_arg();
8112 Field_reference_expression* farg = arg->field_reference_expression();
8115 Expression* struct_expr = farg->expr();
8116 Type* st = struct_expr->type();
8117 if (st->struct_type() == NULL)
8119 if (st->named_type() != NULL)
8120 st->named_type()->convert(this->gogo_);
8121 unsigned int offset;
8122 if (!st->struct_type()->backend_field_offset(this->gogo_,
8123 farg->field_index(),
8126 mpz_set_ui(val, offset);
8132 // Return a floating point constant value if possible.
8135 Builtin_call_expression::do_float_constant_value(mpfr_t val,
8138 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
8140 Expression* arg = this->one_arg();
8151 if (arg->complex_constant_value(real, imag, &type))
8153 if (this->code_ == BUILTIN_REAL)
8154 mpfr_set(val, real, GMP_RNDN);
8156 mpfr_set(val, imag, GMP_RNDN);
8157 *ptype = Builtin_call_expression::real_imag_type(type);
8169 // Return a complex constant value if possible.
8172 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
8175 if (this->code_ == BUILTIN_COMPLEX)
8177 const Expression_list* args = this->args();
8178 if (args == NULL || args->size() != 2)
8184 if (!args->front()->float_constant_value(r, &rtype))
8195 if (args->back()->float_constant_value(i, &itype)
8196 && Type::are_identical(rtype, itype, false, NULL))
8198 mpfr_set(real, r, GMP_RNDN);
8199 mpfr_set(imag, i, GMP_RNDN);
8200 *ptype = Builtin_call_expression::complex_type(rtype);
8213 // Give an error if we are discarding the value of an expression which
8214 // should not normally be discarded. We don't give an error for
8215 // discarding the value of an ordinary function call, but we do for
8216 // builtin functions, purely for consistency with the gc compiler.
8219 Builtin_call_expression::do_discarding_value()
8221 switch (this->code_)
8223 case BUILTIN_INVALID:
8227 case BUILTIN_APPEND:
8229 case BUILTIN_COMPLEX:
8235 case BUILTIN_ALIGNOF:
8236 case BUILTIN_OFFSETOF:
8237 case BUILTIN_SIZEOF:
8238 this->unused_value_error();
8243 case BUILTIN_DELETE:
8246 case BUILTIN_PRINTLN:
8247 case BUILTIN_RECOVER:
8255 Builtin_call_expression::do_type()
8257 switch (this->code_)
8259 case BUILTIN_INVALID:
8266 const Expression_list* args = this->args();
8267 if (args == NULL || args->empty())
8268 return Type::make_error_type();
8269 return Type::make_pointer_type(args->front()->type());
8275 case BUILTIN_ALIGNOF:
8276 case BUILTIN_OFFSETOF:
8277 case BUILTIN_SIZEOF:
8278 return Type::lookup_integer_type("int");
8281 case BUILTIN_DELETE:
8284 case BUILTIN_PRINTLN:
8285 return Type::make_void_type();
8287 case BUILTIN_RECOVER:
8288 return Type::make_empty_interface_type(Linemap::predeclared_location());
8290 case BUILTIN_APPEND:
8292 const Expression_list* args = this->args();
8293 if (args == NULL || args->empty())
8294 return Type::make_error_type();
8295 return args->front()->type();
8301 Expression* arg = this->one_arg();
8303 return Type::make_error_type();
8304 Type* t = arg->type();
8305 if (t->is_abstract())
8306 t = t->make_non_abstract_type();
8307 t = Builtin_call_expression::real_imag_type(t);
8309 t = Type::make_error_type();
8313 case BUILTIN_COMPLEX:
8315 const Expression_list* args = this->args();
8316 if (args == NULL || args->size() != 2)
8317 return Type::make_error_type();
8318 Type* t = args->front()->type();
8319 if (t->is_abstract())
8321 t = args->back()->type();
8322 if (t->is_abstract())
8323 t = t->make_non_abstract_type();
8325 t = Builtin_call_expression::complex_type(t);
8327 t = Type::make_error_type();
8333 // Determine the type.
8336 Builtin_call_expression::do_determine_type(const Type_context* context)
8338 if (!this->determining_types())
8341 this->fn()->determine_type_no_context();
8343 const Expression_list* args = this->args();
8346 Type* arg_type = NULL;
8347 switch (this->code_)
8350 case BUILTIN_PRINTLN:
8351 // Do not force a large integer constant to "int".
8357 arg_type = Builtin_call_expression::complex_type(context->type);
8361 case BUILTIN_COMPLEX:
8363 // For the complex function the type of one operand can
8364 // determine the type of the other, as in a binary expression.
8365 arg_type = Builtin_call_expression::real_imag_type(context->type);
8366 if (args != NULL && args->size() == 2)
8368 Type* t1 = args->front()->type();
8369 Type* t2 = args->front()->type();
8370 if (!t1->is_abstract())
8372 else if (!t2->is_abstract())
8386 for (Expression_list::const_iterator pa = args->begin();
8390 Type_context subcontext;
8391 subcontext.type = arg_type;
8395 // We want to print large constants, we so can't just
8396 // use the appropriate nonabstract type. Use uint64 for
8397 // an integer if we know it is nonnegative, otherwise
8398 // use int64 for a integer, otherwise use float64 for a
8399 // float or complex128 for a complex.
8400 Type* want_type = NULL;
8401 Type* atype = (*pa)->type();
8402 if (atype->is_abstract())
8404 if (atype->integer_type() != NULL)
8409 if (this->integer_constant_value(true, val, &dummy)
8410 && mpz_sgn(val) >= 0)
8411 want_type = Type::lookup_integer_type("uint64");
8413 want_type = Type::lookup_integer_type("int64");
8416 else if (atype->float_type() != NULL)
8417 want_type = Type::lookup_float_type("float64");
8418 else if (atype->complex_type() != NULL)
8419 want_type = Type::lookup_complex_type("complex128");
8420 else if (atype->is_abstract_string_type())
8421 want_type = Type::lookup_string_type();
8422 else if (atype->is_abstract_boolean_type())
8423 want_type = Type::lookup_bool_type();
8426 subcontext.type = want_type;
8430 (*pa)->determine_type(&subcontext);
8435 // If there is exactly one argument, return true. Otherwise give an
8436 // error message and return false.
8439 Builtin_call_expression::check_one_arg()
8441 const Expression_list* args = this->args();
8442 if (args == NULL || args->size() < 1)
8444 this->report_error(_("not enough arguments"));
8447 else if (args->size() > 1)
8449 this->report_error(_("too many arguments"));
8452 if (args->front()->is_error_expression()
8453 || args->front()->type()->is_error())
8455 this->set_is_error();
8461 // Check argument types for a builtin function.
8464 Builtin_call_expression::do_check_types(Gogo*)
8466 switch (this->code_)
8468 case BUILTIN_INVALID:
8476 // The single argument may be either a string or an array or a
8477 // map or a channel, or a pointer to a closed array.
8478 if (this->check_one_arg())
8480 Type* arg_type = this->one_arg()->type();
8481 if (arg_type->points_to() != NULL
8482 && arg_type->points_to()->array_type() != NULL
8483 && !arg_type->points_to()->is_slice_type())
8484 arg_type = arg_type->points_to();
8485 if (this->code_ == BUILTIN_CAP)
8487 if (!arg_type->is_error()
8488 && arg_type->array_type() == NULL
8489 && arg_type->channel_type() == NULL)
8490 this->report_error(_("argument must be array or slice "
8495 if (!arg_type->is_error()
8496 && !arg_type->is_string_type()
8497 && arg_type->array_type() == NULL
8498 && arg_type->map_type() == NULL
8499 && arg_type->channel_type() == NULL)
8500 this->report_error(_("argument must be string or "
8501 "array or slice or map or channel"));
8508 case BUILTIN_PRINTLN:
8510 const Expression_list* args = this->args();
8513 if (this->code_ == BUILTIN_PRINT)
8514 warning_at(this->location(), 0,
8515 "no arguments for builtin function %<%s%>",
8516 (this->code_ == BUILTIN_PRINT
8522 for (Expression_list::const_iterator p = args->begin();
8526 Type* type = (*p)->type();
8527 if (type->is_error()
8528 || type->is_string_type()
8529 || type->integer_type() != NULL
8530 || type->float_type() != NULL
8531 || type->complex_type() != NULL
8532 || type->is_boolean_type()
8533 || type->points_to() != NULL
8534 || type->interface_type() != NULL
8535 || type->channel_type() != NULL
8536 || type->map_type() != NULL
8537 || type->function_type() != NULL
8538 || type->is_slice_type())
8540 else if ((*p)->is_type_expression())
8542 // If this is a type expression it's going to give
8543 // an error anyhow, so we don't need one here.
8546 this->report_error(_("unsupported argument type to "
8547 "builtin function"));
8554 if (this->check_one_arg())
8556 if (this->one_arg()->type()->channel_type() == NULL)
8557 this->report_error(_("argument must be channel"));
8558 else if (!this->one_arg()->type()->channel_type()->may_send())
8559 this->report_error(_("cannot close receive-only channel"));
8564 case BUILTIN_SIZEOF:
8565 case BUILTIN_ALIGNOF:
8566 this->check_one_arg();
8569 case BUILTIN_RECOVER:
8570 if (this->args() != NULL && !this->args()->empty())
8571 this->report_error(_("too many arguments"));
8574 case BUILTIN_OFFSETOF:
8575 if (this->check_one_arg())
8577 Expression* arg = this->one_arg();
8578 if (arg->field_reference_expression() == NULL)
8579 this->report_error(_("argument must be a field reference"));
8585 const Expression_list* args = this->args();
8586 if (args == NULL || args->size() < 2)
8588 this->report_error(_("not enough arguments"));
8591 else if (args->size() > 2)
8593 this->report_error(_("too many arguments"));
8596 Type* arg1_type = args->front()->type();
8597 Type* arg2_type = args->back()->type();
8598 if (arg1_type->is_error() || arg2_type->is_error())
8602 if (arg1_type->is_slice_type())
8603 e1 = arg1_type->array_type()->element_type();
8606 this->report_error(_("left argument must be a slice"));
8610 if (arg2_type->is_slice_type())
8612 Type* e2 = arg2_type->array_type()->element_type();
8613 if (!Type::are_identical(e1, e2, true, NULL))
8614 this->report_error(_("element types must be the same"));
8616 else if (arg2_type->is_string_type())
8618 if (e1->integer_type() == NULL || !e1->integer_type()->is_byte())
8619 this->report_error(_("first argument must be []byte"));
8622 this->report_error(_("second argument must be slice or string"));
8626 case BUILTIN_APPEND:
8628 const Expression_list* args = this->args();
8629 if (args == NULL || args->size() < 2)
8631 this->report_error(_("not enough arguments"));
8634 if (args->size() > 2)
8636 this->report_error(_("too many arguments"));
8640 // The language permits appending a string to a []byte, as a
8642 if (args->back()->type()->is_string_type())
8644 const Array_type* at = args->front()->type()->array_type();
8645 const Type* e = at->element_type()->forwarded();
8646 if (e->integer_type() != NULL && e->integer_type()->is_byte())
8650 // The language says that the second argument must be
8651 // assignable to a slice of the element type of the first
8652 // argument. We already know the first argument is a slice
8654 Array_type* at = args->front()->type()->array_type();
8655 Type* arg2_type = Type::make_array_type(at->element_type(), NULL);
8657 if (!Type::are_assignable(arg2_type, args->back()->type(), &reason))
8660 this->report_error(_("argument 2 has invalid type"));
8663 error_at(this->location(), "argument 2 has invalid type (%s)",
8665 this->set_is_error();
8673 if (this->check_one_arg())
8675 if (this->one_arg()->type()->complex_type() == NULL)
8676 this->report_error(_("argument must have complex type"));
8680 case BUILTIN_COMPLEX:
8682 const Expression_list* args = this->args();
8683 if (args == NULL || args->size() < 2)
8684 this->report_error(_("not enough arguments"));
8685 else if (args->size() > 2)
8686 this->report_error(_("too many arguments"));
8687 else if (args->front()->is_error_expression()
8688 || args->front()->type()->is_error()
8689 || args->back()->is_error_expression()
8690 || args->back()->type()->is_error())
8691 this->set_is_error();
8692 else if (!Type::are_identical(args->front()->type(),
8693 args->back()->type(), true, NULL))
8694 this->report_error(_("complex arguments must have identical types"));
8695 else if (args->front()->type()->float_type() == NULL)
8696 this->report_error(_("complex arguments must have "
8697 "floating-point type"));
8706 // Return the tree for a builtin function.
8709 Builtin_call_expression::do_get_tree(Translate_context* context)
8711 Gogo* gogo = context->gogo();
8712 Location location = this->location();
8713 switch (this->code_)
8715 case BUILTIN_INVALID:
8723 const Expression_list* args = this->args();
8724 go_assert(args != NULL && args->size() == 1);
8725 Expression* arg = *args->begin();
8726 Type* arg_type = arg->type();
8730 go_assert(saw_errors());
8731 return error_mark_node;
8735 tree arg_tree = arg->get_tree(context);
8737 this->seen_ = false;
8739 if (arg_tree == error_mark_node)
8740 return error_mark_node;
8742 if (arg_type->points_to() != NULL)
8744 arg_type = arg_type->points_to();
8745 go_assert(arg_type->array_type() != NULL
8746 && !arg_type->is_slice_type());
8747 go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
8748 arg_tree = build_fold_indirect_ref(arg_tree);
8752 if (this->code_ == BUILTIN_LEN)
8754 if (arg_type->is_string_type())
8755 val_tree = String_type::length_tree(gogo, arg_tree);
8756 else if (arg_type->array_type() != NULL)
8760 go_assert(saw_errors());
8761 return error_mark_node;
8764 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
8765 this->seen_ = false;
8767 else if (arg_type->map_type() != NULL)
8769 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8770 static tree map_len_fndecl;
8771 val_tree = Gogo::call_builtin(&map_len_fndecl,
8779 else if (arg_type->channel_type() != NULL)
8781 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8782 static tree chan_len_fndecl;
8783 val_tree = Gogo::call_builtin(&chan_len_fndecl,
8796 if (arg_type->array_type() != NULL)
8800 go_assert(saw_errors());
8801 return error_mark_node;
8804 val_tree = arg_type->array_type()->capacity_tree(gogo,
8806 this->seen_ = false;
8808 else if (arg_type->channel_type() != NULL)
8810 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
8811 static tree chan_cap_fndecl;
8812 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
8824 if (val_tree == error_mark_node)
8825 return error_mark_node;
8827 Type* int_type = Type::lookup_integer_type("int");
8828 tree type_tree = type_to_tree(int_type->get_backend(gogo));
8829 if (type_tree == TREE_TYPE(val_tree))
8832 return fold(convert_to_integer(type_tree, val_tree));
8836 case BUILTIN_PRINTLN:
8838 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
8839 tree stmt_list = NULL_TREE;
8841 const Expression_list* call_args = this->args();
8842 if (call_args != NULL)
8844 for (Expression_list::const_iterator p = call_args->begin();
8845 p != call_args->end();
8848 if (is_ln && p != call_args->begin())
8850 static tree print_space_fndecl;
8851 tree call = Gogo::call_builtin(&print_space_fndecl,
8856 if (call == error_mark_node)
8857 return error_mark_node;
8858 append_to_statement_list(call, &stmt_list);
8861 Type* type = (*p)->type();
8863 tree arg = (*p)->get_tree(context);
8864 if (arg == error_mark_node)
8865 return error_mark_node;
8869 if (type->is_string_type())
8871 static tree print_string_fndecl;
8872 pfndecl = &print_string_fndecl;
8873 fnname = "__go_print_string";
8875 else if (type->integer_type() != NULL
8876 && type->integer_type()->is_unsigned())
8878 static tree print_uint64_fndecl;
8879 pfndecl = &print_uint64_fndecl;
8880 fnname = "__go_print_uint64";
8881 Type* itype = Type::lookup_integer_type("uint64");
8882 Btype* bitype = itype->get_backend(gogo);
8883 arg = fold_convert_loc(location.gcc_location(),
8884 type_to_tree(bitype), arg);
8886 else if (type->integer_type() != NULL)
8888 static tree print_int64_fndecl;
8889 pfndecl = &print_int64_fndecl;
8890 fnname = "__go_print_int64";
8891 Type* itype = Type::lookup_integer_type("int64");
8892 Btype* bitype = itype->get_backend(gogo);
8893 arg = fold_convert_loc(location.gcc_location(),
8894 type_to_tree(bitype), arg);
8896 else if (type->float_type() != NULL)
8898 static tree print_double_fndecl;
8899 pfndecl = &print_double_fndecl;
8900 fnname = "__go_print_double";
8901 arg = fold_convert_loc(location.gcc_location(),
8902 double_type_node, arg);
8904 else if (type->complex_type() != NULL)
8906 static tree print_complex_fndecl;
8907 pfndecl = &print_complex_fndecl;
8908 fnname = "__go_print_complex";
8909 arg = fold_convert_loc(location.gcc_location(),
8910 complex_double_type_node, arg);
8912 else if (type->is_boolean_type())
8914 static tree print_bool_fndecl;
8915 pfndecl = &print_bool_fndecl;
8916 fnname = "__go_print_bool";
8918 else if (type->points_to() != NULL
8919 || type->channel_type() != NULL
8920 || type->map_type() != NULL
8921 || type->function_type() != NULL)
8923 static tree print_pointer_fndecl;
8924 pfndecl = &print_pointer_fndecl;
8925 fnname = "__go_print_pointer";
8926 arg = fold_convert_loc(location.gcc_location(),
8927 ptr_type_node, arg);
8929 else if (type->interface_type() != NULL)
8931 if (type->interface_type()->is_empty())
8933 static tree print_empty_interface_fndecl;
8934 pfndecl = &print_empty_interface_fndecl;
8935 fnname = "__go_print_empty_interface";
8939 static tree print_interface_fndecl;
8940 pfndecl = &print_interface_fndecl;
8941 fnname = "__go_print_interface";
8944 else if (type->is_slice_type())
8946 static tree print_slice_fndecl;
8947 pfndecl = &print_slice_fndecl;
8948 fnname = "__go_print_slice";
8953 tree call = Gogo::call_builtin(pfndecl,
8960 if (call == error_mark_node)
8961 return error_mark_node;
8962 append_to_statement_list(call, &stmt_list);
8968 static tree print_nl_fndecl;
8969 tree call = Gogo::call_builtin(&print_nl_fndecl,
8974 if (call == error_mark_node)
8975 return error_mark_node;
8976 append_to_statement_list(call, &stmt_list);
8984 const Expression_list* args = this->args();
8985 go_assert(args != NULL && args->size() == 1);
8986 Expression* arg = args->front();
8987 tree arg_tree = arg->get_tree(context);
8988 if (arg_tree == error_mark_node)
8989 return error_mark_node;
8991 Type::make_empty_interface_type(Linemap::predeclared_location());
8992 arg_tree = Expression::convert_for_assignment(context, empty,
8994 arg_tree, location);
8995 static tree panic_fndecl;
8996 tree call = Gogo::call_builtin(&panic_fndecl,
9001 TREE_TYPE(arg_tree),
9003 if (call == error_mark_node)
9004 return error_mark_node;
9005 // This function will throw an exception.
9006 TREE_NOTHROW(panic_fndecl) = 0;
9007 // This function will not return.
9008 TREE_THIS_VOLATILE(panic_fndecl) = 1;
9012 case BUILTIN_RECOVER:
9014 // The argument is set when building recover thunks. It's a
9015 // boolean value which is true if we can recover a value now.
9016 const Expression_list* args = this->args();
9017 go_assert(args != NULL && args->size() == 1);
9018 Expression* arg = args->front();
9019 tree arg_tree = arg->get_tree(context);
9020 if (arg_tree == error_mark_node)
9021 return error_mark_node;
9024 Type::make_empty_interface_type(Linemap::predeclared_location());
9025 tree empty_tree = type_to_tree(empty->get_backend(context->gogo()));
9027 Type* nil_type = Type::make_nil_type();
9028 Expression* nil = Expression::make_nil(location);
9029 tree nil_tree = nil->get_tree(context);
9030 tree empty_nil_tree = Expression::convert_for_assignment(context,
9036 // We need to handle a deferred call to recover specially,
9037 // because it changes whether it can recover a panic or not.
9038 // See test7 in test/recover1.go.
9040 if (this->is_deferred())
9042 static tree deferred_recover_fndecl;
9043 call = Gogo::call_builtin(&deferred_recover_fndecl,
9045 "__go_deferred_recover",
9051 static tree recover_fndecl;
9052 call = Gogo::call_builtin(&recover_fndecl,
9058 if (call == error_mark_node)
9059 return error_mark_node;
9060 return fold_build3_loc(location.gcc_location(), COND_EXPR, empty_tree,
9061 arg_tree, call, empty_nil_tree);
9066 const Expression_list* args = this->args();
9067 go_assert(args != NULL && args->size() == 1);
9068 Expression* arg = args->front();
9069 tree arg_tree = arg->get_tree(context);
9070 if (arg_tree == error_mark_node)
9071 return error_mark_node;
9072 static tree close_fndecl;
9073 return Gogo::call_builtin(&close_fndecl,
9075 "__go_builtin_close",
9078 TREE_TYPE(arg_tree),
9082 case BUILTIN_SIZEOF:
9083 case BUILTIN_OFFSETOF:
9084 case BUILTIN_ALIGNOF:
9089 bool b = this->integer_constant_value(true, val, &dummy);
9092 go_assert(saw_errors());
9093 return error_mark_node;
9095 Type* int_type = Type::lookup_integer_type("int");
9096 tree type = type_to_tree(int_type->get_backend(gogo));
9097 tree ret = Expression::integer_constant_tree(val, type);
9104 const Expression_list* args = this->args();
9105 go_assert(args != NULL && args->size() == 2);
9106 Expression* arg1 = args->front();
9107 Expression* arg2 = args->back();
9109 tree arg1_tree = arg1->get_tree(context);
9110 tree arg2_tree = arg2->get_tree(context);
9111 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
9112 return error_mark_node;
9114 Type* arg1_type = arg1->type();
9115 Array_type* at = arg1_type->array_type();
9116 arg1_tree = save_expr(arg1_tree);
9117 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
9118 tree arg1_len = at->length_tree(gogo, arg1_tree);
9119 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
9120 return error_mark_node;
9122 Type* arg2_type = arg2->type();
9125 if (arg2_type->is_slice_type())
9127 at = arg2_type->array_type();
9128 arg2_tree = save_expr(arg2_tree);
9129 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
9130 arg2_len = at->length_tree(gogo, arg2_tree);
9134 arg2_tree = save_expr(arg2_tree);
9135 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
9136 arg2_len = String_type::length_tree(gogo, arg2_tree);
9138 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
9139 return error_mark_node;
9141 arg1_len = save_expr(arg1_len);
9142 arg2_len = save_expr(arg2_len);
9143 tree len = fold_build3_loc(location.gcc_location(), COND_EXPR,
9144 TREE_TYPE(arg1_len),
9145 fold_build2_loc(location.gcc_location(),
9146 LT_EXPR, boolean_type_node,
9147 arg1_len, arg2_len),
9148 arg1_len, arg2_len);
9149 len = save_expr(len);
9151 Type* element_type = at->element_type();
9152 Btype* element_btype = element_type->get_backend(gogo);
9153 tree element_type_tree = type_to_tree(element_btype);
9154 if (element_type_tree == error_mark_node)
9155 return error_mark_node;
9156 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9157 tree bytecount = fold_convert_loc(location.gcc_location(),
9158 TREE_TYPE(element_size), len);
9159 bytecount = fold_build2_loc(location.gcc_location(), MULT_EXPR,
9160 TREE_TYPE(element_size),
9161 bytecount, element_size);
9162 bytecount = fold_convert_loc(location.gcc_location(), size_type_node,
9165 arg1_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
9167 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
9170 static tree copy_fndecl;
9171 tree call = Gogo::call_builtin(©_fndecl,
9182 if (call == error_mark_node)
9183 return error_mark_node;
9185 return fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
9186 TREE_TYPE(len), call, len);
9189 case BUILTIN_APPEND:
9191 const Expression_list* args = this->args();
9192 go_assert(args != NULL && args->size() == 2);
9193 Expression* arg1 = args->front();
9194 Expression* arg2 = args->back();
9196 tree arg1_tree = arg1->get_tree(context);
9197 tree arg2_tree = arg2->get_tree(context);
9198 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
9199 return error_mark_node;
9201 Array_type* at = arg1->type()->array_type();
9202 Type* element_type = at->element_type()->forwarded();
9207 if (arg2->type()->is_string_type()
9208 && element_type->integer_type() != NULL
9209 && element_type->integer_type()->is_byte())
9211 arg2_tree = save_expr(arg2_tree);
9212 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
9213 arg2_len = String_type::length_tree(gogo, arg2_tree);
9214 element_size = size_int(1);
9218 arg2_tree = Expression::convert_for_assignment(context, at,
9222 if (arg2_tree == error_mark_node)
9223 return error_mark_node;
9225 arg2_tree = save_expr(arg2_tree);
9227 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
9228 arg2_len = at->length_tree(gogo, arg2_tree);
9230 Btype* element_btype = element_type->get_backend(gogo);
9231 tree element_type_tree = type_to_tree(element_btype);
9232 if (element_type_tree == error_mark_node)
9233 return error_mark_node;
9234 element_size = TYPE_SIZE_UNIT(element_type_tree);
9237 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
9239 arg2_len = fold_convert_loc(location.gcc_location(), size_type_node,
9241 element_size = fold_convert_loc(location.gcc_location(), size_type_node,
9244 if (arg2_val == error_mark_node
9245 || arg2_len == error_mark_node
9246 || element_size == error_mark_node)
9247 return error_mark_node;
9249 // We rebuild the decl each time since the slice types may
9251 tree append_fndecl = NULL_TREE;
9252 return Gogo::call_builtin(&append_fndecl,
9256 TREE_TYPE(arg1_tree),
9257 TREE_TYPE(arg1_tree),
9270 const Expression_list* args = this->args();
9271 go_assert(args != NULL && args->size() == 1);
9272 Expression* arg = args->front();
9273 tree arg_tree = arg->get_tree(context);
9274 if (arg_tree == error_mark_node)
9275 return error_mark_node;
9276 go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
9277 if (this->code_ == BUILTIN_REAL)
9278 return fold_build1_loc(location.gcc_location(), REALPART_EXPR,
9279 TREE_TYPE(TREE_TYPE(arg_tree)),
9282 return fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
9283 TREE_TYPE(TREE_TYPE(arg_tree)),
9287 case BUILTIN_COMPLEX:
9289 const Expression_list* args = this->args();
9290 go_assert(args != NULL && args->size() == 2);
9291 tree r = args->front()->get_tree(context);
9292 tree i = args->back()->get_tree(context);
9293 if (r == error_mark_node || i == error_mark_node)
9294 return error_mark_node;
9295 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
9296 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
9297 go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
9298 return fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
9299 build_complex_type(TREE_TYPE(r)),
9308 // We have to support exporting a builtin call expression, because
9309 // code can set a constant to the result of a builtin expression.
9312 Builtin_call_expression::do_export(Export* exp) const
9319 if (this->integer_constant_value(true, val, &dummy))
9321 Integer_expression::export_integer(exp, val);
9330 if (this->float_constant_value(fval, &dummy))
9332 Float_expression::export_float(exp, fval);
9344 if (this->complex_constant_value(real, imag, &dummy))
9346 Complex_expression::export_complex(exp, real, imag);
9355 error_at(this->location(), "value is not constant");
9359 // A trailing space lets us reliably identify the end of the number.
9360 exp->write_c_string(" ");
9363 // Class Call_expression.
9368 Call_expression::do_traverse(Traverse* traverse)
9370 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
9371 return TRAVERSE_EXIT;
9372 if (this->args_ != NULL)
9374 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
9375 return TRAVERSE_EXIT;
9377 return TRAVERSE_CONTINUE;
9380 // Lower a call statement.
9383 Call_expression::do_lower(Gogo* gogo, Named_object* function,
9384 Statement_inserter* inserter, int)
9386 Location loc = this->location();
9388 // A type cast can look like a function call.
9389 if (this->fn_->is_type_expression()
9390 && this->args_ != NULL
9391 && this->args_->size() == 1)
9392 return Expression::make_cast(this->fn_->type(), this->args_->front(),
9395 // Recognize a call to a builtin function.
9396 Func_expression* fne = this->fn_->func_expression();
9398 && fne->named_object()->is_function_declaration()
9399 && fne->named_object()->func_declaration_value()->type()->is_builtin())
9400 return new Builtin_call_expression(gogo, this->fn_, this->args_,
9401 this->is_varargs_, loc);
9403 // Handle an argument which is a call to a function which returns
9404 // multiple results.
9405 if (this->args_ != NULL
9406 && this->args_->size() == 1
9407 && this->args_->front()->call_expression() != NULL
9408 && this->fn_->type()->function_type() != NULL)
9410 Function_type* fntype = this->fn_->type()->function_type();
9411 size_t rc = this->args_->front()->call_expression()->result_count();
9413 && fntype->parameters() != NULL
9414 && (fntype->parameters()->size() == rc
9415 || (fntype->is_varargs()
9416 && fntype->parameters()->size() - 1 <= rc)))
9418 Call_expression* call = this->args_->front()->call_expression();
9419 Expression_list* args = new Expression_list;
9420 for (size_t i = 0; i < rc; ++i)
9421 args->push_back(Expression::make_call_result(call, i));
9422 // We can't return a new call expression here, because this
9423 // one may be referenced by Call_result expressions. We
9424 // also can't delete the old arguments, because we may still
9425 // traverse them somewhere up the call stack. FIXME.
9430 // If this call returns multiple results, create a temporary
9431 // variable for each result.
9432 size_t rc = this->result_count();
9433 if (rc > 1 && this->results_ == NULL)
9435 std::vector<Temporary_statement*>* temps =
9436 new std::vector<Temporary_statement*>;
9438 const Typed_identifier_list* results =
9439 this->fn_->type()->function_type()->results();
9440 for (Typed_identifier_list::const_iterator p = results->begin();
9441 p != results->end();
9444 Temporary_statement* temp = Statement::make_temporary(p->type(),
9446 inserter->insert(temp);
9447 temps->push_back(temp);
9449 this->results_ = temps;
9452 // Handle a call to a varargs function by packaging up the extra
9454 if (this->fn_->type()->function_type() != NULL
9455 && this->fn_->type()->function_type()->is_varargs())
9457 Function_type* fntype = this->fn_->type()->function_type();
9458 const Typed_identifier_list* parameters = fntype->parameters();
9459 go_assert(parameters != NULL && !parameters->empty());
9460 Type* varargs_type = parameters->back().type();
9461 this->lower_varargs(gogo, function, inserter, varargs_type,
9462 parameters->size());
9465 // If this is call to a method, call the method directly passing the
9466 // object as the first parameter.
9467 Bound_method_expression* bme = this->fn_->bound_method_expression();
9470 Named_object* method = bme->method();
9471 Expression* first_arg = bme->first_argument();
9473 // We always pass a pointer when calling a method.
9474 if (first_arg->type()->points_to() == NULL
9475 && !first_arg->type()->is_error())
9477 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
9478 // We may need to create a temporary variable so that we can
9479 // take the address. We can't do that here because it will
9480 // mess up the order of evaluation.
9481 Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
9482 ue->set_create_temp();
9485 // If we are calling a method which was inherited from an
9486 // embedded struct, and the method did not get a stub, then the
9487 // first type may be wrong.
9488 Type* fatype = bme->first_argument_type();
9491 if (fatype->points_to() == NULL)
9492 fatype = Type::make_pointer_type(fatype);
9493 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
9496 Expression_list* new_args = new Expression_list();
9497 new_args->push_back(first_arg);
9498 if (this->args_ != NULL)
9500 for (Expression_list::const_iterator p = this->args_->begin();
9501 p != this->args_->end();
9503 new_args->push_back(*p);
9506 // We have to change in place because this structure may be
9507 // referenced by Call_result_expressions. We can't delete the
9508 // old arguments, because we may be traversing them up in some
9510 this->args_ = new_args;
9511 this->fn_ = Expression::make_func_reference(method, NULL,
9518 // Lower a call to a varargs function. FUNCTION is the function in
9519 // which the call occurs--it's not the function we are calling.
9520 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
9521 // PARAM_COUNT is the number of parameters of the function we are
9522 // calling; the last of these parameters will be the varargs
9526 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
9527 Statement_inserter* inserter,
9528 Type* varargs_type, size_t param_count)
9530 if (this->varargs_are_lowered_)
9533 Location loc = this->location();
9535 go_assert(param_count > 0);
9536 go_assert(varargs_type->is_slice_type());
9538 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
9539 if (arg_count < param_count - 1)
9541 // Not enough arguments; will be caught in check_types.
9545 Expression_list* old_args = this->args_;
9546 Expression_list* new_args = new Expression_list();
9547 bool push_empty_arg = false;
9548 if (old_args == NULL || old_args->empty())
9550 go_assert(param_count == 1);
9551 push_empty_arg = true;
9555 Expression_list::const_iterator pa;
9557 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
9559 if (static_cast<size_t>(i) == param_count)
9561 new_args->push_back(*pa);
9564 // We have reached the varargs parameter.
9566 bool issued_error = false;
9567 if (pa == old_args->end())
9568 push_empty_arg = true;
9569 else if (pa + 1 == old_args->end() && this->is_varargs_)
9570 new_args->push_back(*pa);
9571 else if (this->is_varargs_)
9573 this->report_error(_("too many arguments"));
9578 Type* element_type = varargs_type->array_type()->element_type();
9579 Expression_list* vals = new Expression_list;
9580 for (; pa != old_args->end(); ++pa, ++i)
9582 // Check types here so that we get a better message.
9583 Type* patype = (*pa)->type();
9584 Location paloc = (*pa)->location();
9585 if (!this->check_argument_type(i, element_type, patype,
9586 paloc, issued_error))
9588 vals->push_back(*pa);
9591 Expression::make_slice_composite_literal(varargs_type, vals, loc);
9592 gogo->lower_expression(function, inserter, &val);
9593 new_args->push_back(val);
9598 new_args->push_back(Expression::make_nil(loc));
9600 // We can't return a new call expression here, because this one may
9601 // be referenced by Call_result expressions. FIXME. We can't
9602 // delete OLD_ARGS because we may have both a Call_expression and a
9603 // Builtin_call_expression which refer to them. FIXME.
9604 this->args_ = new_args;
9605 this->varargs_are_lowered_ = true;
9608 // Get the function type. This can return NULL in error cases.
9611 Call_expression::get_function_type() const
9613 return this->fn_->type()->function_type();
9616 // Return the number of values which this call will return.
9619 Call_expression::result_count() const
9621 const Function_type* fntype = this->get_function_type();
9624 if (fntype->results() == NULL)
9626 return fntype->results()->size();
9629 // Return the temporary which holds a result.
9631 Temporary_statement*
9632 Call_expression::result(size_t i) const
9634 go_assert(this->results_ != NULL
9635 && this->results_->size() > i);
9636 return (*this->results_)[i];
9639 // Return whether this is a call to the predeclared function recover.
9642 Call_expression::is_recover_call() const
9644 return this->do_is_recover_call();
9647 // Set the argument to the recover function.
9650 Call_expression::set_recover_arg(Expression* arg)
9652 this->do_set_recover_arg(arg);
9655 // Virtual functions also implemented by Builtin_call_expression.
9658 Call_expression::do_is_recover_call() const
9664 Call_expression::do_set_recover_arg(Expression*)
9669 // We have found an error with this call expression; return true if
9670 // we should report it.
9673 Call_expression::issue_error()
9675 if (this->issued_error_)
9679 this->issued_error_ = true;
9687 Call_expression::do_type()
9689 if (this->type_ != NULL)
9693 Function_type* fntype = this->get_function_type();
9695 return Type::make_error_type();
9697 const Typed_identifier_list* results = fntype->results();
9698 if (results == NULL)
9699 ret = Type::make_void_type();
9700 else if (results->size() == 1)
9701 ret = results->begin()->type();
9703 ret = Type::make_call_multiple_result_type(this);
9710 // Determine types for a call expression. We can use the function
9711 // parameter types to set the types of the arguments.
9714 Call_expression::do_determine_type(const Type_context*)
9716 if (!this->determining_types())
9719 this->fn_->determine_type_no_context();
9720 Function_type* fntype = this->get_function_type();
9721 const Typed_identifier_list* parameters = NULL;
9723 parameters = fntype->parameters();
9724 if (this->args_ != NULL)
9726 Typed_identifier_list::const_iterator pt;
9727 if (parameters != NULL)
9728 pt = parameters->begin();
9730 for (Expression_list::const_iterator pa = this->args_->begin();
9731 pa != this->args_->end();
9737 // If this is a method, the first argument is the
9739 if (fntype != NULL && fntype->is_method())
9741 Type* rtype = fntype->receiver()->type();
9742 // The receiver is always passed as a pointer.
9743 if (rtype->points_to() == NULL)
9744 rtype = Type::make_pointer_type(rtype);
9745 Type_context subcontext(rtype, false);
9746 (*pa)->determine_type(&subcontext);
9751 if (parameters != NULL && pt != parameters->end())
9753 Type_context subcontext(pt->type(), false);
9754 (*pa)->determine_type(&subcontext);
9758 (*pa)->determine_type_no_context();
9763 // Called when determining types for a Call_expression. Return true
9764 // if we should go ahead, false if they have already been determined.
9767 Call_expression::determining_types()
9769 if (this->types_are_determined_)
9773 this->types_are_determined_ = true;
9778 // Check types for parameter I.
9781 Call_expression::check_argument_type(int i, const Type* parameter_type,
9782 const Type* argument_type,
9783 Location argument_location,
9788 if (this->are_hidden_fields_ok_)
9789 ok = Type::are_assignable_hidden_ok(parameter_type, argument_type,
9792 ok = Type::are_assignable(parameter_type, argument_type, &reason);
9798 error_at(argument_location, "argument %d has incompatible type", i);
9800 error_at(argument_location,
9801 "argument %d has incompatible type (%s)",
9804 this->set_is_error();
9813 Call_expression::do_check_types(Gogo*)
9815 Function_type* fntype = this->get_function_type();
9818 if (!this->fn_->type()->is_error())
9819 this->report_error(_("expected function"));
9823 bool is_method = fntype->is_method();
9826 go_assert(this->args_ != NULL && !this->args_->empty());
9827 Type* rtype = fntype->receiver()->type();
9828 Expression* first_arg = this->args_->front();
9829 // The language permits copying hidden fields for a method
9830 // receiver. We dereference the values since receivers are
9831 // always passed as pointers.
9833 if (!Type::are_assignable_hidden_ok(rtype->deref(),
9834 first_arg->type()->deref(),
9838 this->report_error(_("incompatible type for receiver"));
9841 error_at(this->location(),
9842 "incompatible type for receiver (%s)",
9844 this->set_is_error();
9849 // Note that varargs was handled by the lower_varargs() method, so
9850 // we don't have to worry about it here.
9852 const Typed_identifier_list* parameters = fntype->parameters();
9853 if (this->args_ == NULL)
9855 if (parameters != NULL && !parameters->empty())
9856 this->report_error(_("not enough arguments"));
9858 else if (parameters == NULL)
9860 if (!is_method || this->args_->size() > 1)
9861 this->report_error(_("too many arguments"));
9866 Expression_list::const_iterator pa = this->args_->begin();
9869 for (Typed_identifier_list::const_iterator pt = parameters->begin();
9870 pt != parameters->end();
9873 if (pa == this->args_->end())
9875 this->report_error(_("not enough arguments"));
9878 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
9879 (*pa)->location(), false);
9881 if (pa != this->args_->end())
9882 this->report_error(_("too many arguments"));
9886 // Return whether we have to use a temporary variable to ensure that
9887 // we evaluate this call expression in order. If the call returns no
9888 // results then it will inevitably be executed last.
9891 Call_expression::do_must_eval_in_order() const
9893 return this->result_count() > 0;
9896 // Get the function and the first argument to use when calling an
9897 // interface method.
9900 Call_expression::interface_method_function(
9901 Translate_context* context,
9902 Interface_field_reference_expression* interface_method,
9903 tree* first_arg_ptr)
9905 tree expr = interface_method->expr()->get_tree(context);
9906 if (expr == error_mark_node)
9907 return error_mark_node;
9908 expr = save_expr(expr);
9909 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
9910 if (first_arg == error_mark_node)
9911 return error_mark_node;
9912 *first_arg_ptr = first_arg;
9913 return interface_method->get_function_tree(context, expr);
9916 // Build the call expression.
9919 Call_expression::do_get_tree(Translate_context* context)
9921 if (this->tree_ != NULL_TREE)
9924 Function_type* fntype = this->get_function_type();
9926 return error_mark_node;
9928 if (this->fn_->is_error_expression())
9929 return error_mark_node;
9931 Gogo* gogo = context->gogo();
9932 Location location = this->location();
9934 Func_expression* func = this->fn_->func_expression();
9935 Interface_field_reference_expression* interface_method =
9936 this->fn_->interface_field_reference_expression();
9937 const bool has_closure = func != NULL && func->closure() != NULL;
9938 const bool is_interface_method = interface_method != NULL;
9942 if (this->args_ == NULL || this->args_->empty())
9944 nargs = is_interface_method ? 1 : 0;
9945 args = nargs == 0 ? NULL : new tree[nargs];
9947 else if (fntype->parameters() == NULL || fntype->parameters()->empty())
9949 // Passing a receiver parameter.
9950 go_assert(!is_interface_method
9951 && fntype->is_method()
9952 && this->args_->size() == 1);
9954 args = new tree[nargs];
9955 args[0] = this->args_->front()->get_tree(context);
9959 const Typed_identifier_list* params = fntype->parameters();
9961 nargs = this->args_->size();
9962 int i = is_interface_method ? 1 : 0;
9964 args = new tree[nargs];
9966 Typed_identifier_list::const_iterator pp = params->begin();
9967 Expression_list::const_iterator pe = this->args_->begin();
9968 if (!is_interface_method && fntype->is_method())
9970 args[i] = (*pe)->get_tree(context);
9974 for (; pe != this->args_->end(); ++pe, ++pp, ++i)
9976 go_assert(pp != params->end());
9977 tree arg_val = (*pe)->get_tree(context);
9978 args[i] = Expression::convert_for_assignment(context,
9983 if (args[i] == error_mark_node)
9986 return error_mark_node;
9989 go_assert(pp == params->end());
9990 go_assert(i == nargs);
9993 tree rettype = TREE_TYPE(TREE_TYPE(type_to_tree(fntype->get_backend(gogo))));
9994 if (rettype == error_mark_node)
9997 return error_mark_node;
10002 fn = func->get_tree_without_closure(gogo);
10003 else if (!is_interface_method)
10004 fn = this->fn_->get_tree(context);
10006 fn = this->interface_method_function(context, interface_method, &args[0]);
10008 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
10011 return error_mark_node;
10015 if (TREE_CODE(fndecl) == ADDR_EXPR)
10016 fndecl = TREE_OPERAND(fndecl, 0);
10018 // Add a type cast in case the type of the function is a recursive
10019 // type which refers to itself.
10020 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
10022 tree fnt = type_to_tree(fntype->get_backend(gogo));
10023 if (fnt == error_mark_node)
10024 return error_mark_node;
10025 fn = fold_convert_loc(location.gcc_location(), fnt, fn);
10028 // This is to support builtin math functions when using 80387 math.
10029 tree excess_type = NULL_TREE;
10030 if (TREE_CODE(fndecl) == FUNCTION_DECL
10031 && DECL_IS_BUILTIN(fndecl)
10032 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
10034 && ((SCALAR_FLOAT_TYPE_P(rettype)
10035 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
10036 || (COMPLEX_FLOAT_TYPE_P(rettype)
10037 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
10039 excess_type = excess_precision_type(TREE_TYPE(args[0]));
10040 if (excess_type != NULL_TREE)
10042 tree excess_fndecl = mathfn_built_in(excess_type,
10043 DECL_FUNCTION_CODE(fndecl));
10044 if (excess_fndecl == NULL_TREE)
10045 excess_type = NULL_TREE;
10048 fn = build_fold_addr_expr_loc(location.gcc_location(),
10050 for (int i = 0; i < nargs; ++i)
10052 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
10053 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
10054 args[i] = ::convert(excess_type, args[i]);
10060 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
10064 SET_EXPR_LOCATION(ret, location.gcc_location());
10068 tree closure_tree = func->closure()->get_tree(context);
10069 if (closure_tree != error_mark_node)
10070 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
10073 // If this is a recursive function type which returns itself, as in
10075 // we have used ptr_type_node for the return type. Add a cast here
10076 // to the correct type.
10077 if (TREE_TYPE(ret) == ptr_type_node)
10079 tree t = type_to_tree(this->type()->base()->get_backend(gogo));
10080 ret = fold_convert_loc(location.gcc_location(), t, ret);
10083 if (excess_type != NULL_TREE)
10085 // Calling convert here can undo our excess precision change.
10086 // That may or may not be a bug in convert_to_real.
10087 ret = build1(NOP_EXPR, rettype, ret);
10090 if (this->results_ != NULL)
10091 ret = this->set_results(context, ret);
10098 // Set the result variables if this call returns multiple results.
10101 Call_expression::set_results(Translate_context* context, tree call_tree)
10103 tree stmt_list = NULL_TREE;
10105 call_tree = save_expr(call_tree);
10107 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
10109 go_assert(saw_errors());
10113 Location loc = this->location();
10114 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
10115 size_t rc = this->result_count();
10116 for (size_t i = 0; i < rc; ++i, field = DECL_CHAIN(field))
10118 go_assert(field != NULL_TREE);
10120 Temporary_statement* temp = this->result(i);
10121 Temporary_reference_expression* ref =
10122 Expression::make_temporary_reference(temp, loc);
10123 ref->set_is_lvalue();
10124 tree temp_tree = ref->get_tree(context);
10125 if (temp_tree == error_mark_node)
10128 tree val_tree = build3_loc(loc.gcc_location(), COMPONENT_REF,
10129 TREE_TYPE(field), call_tree, field, NULL_TREE);
10130 tree set_tree = build2_loc(loc.gcc_location(), MODIFY_EXPR,
10131 void_type_node, temp_tree, val_tree);
10133 append_to_statement_list(set_tree, &stmt_list);
10135 go_assert(field == NULL_TREE);
10137 return save_expr(stmt_list);
10140 // Dump ast representation for a call expressin.
10143 Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
10145 this->fn_->dump_expression(ast_dump_context);
10146 ast_dump_context->ostream() << "(";
10148 ast_dump_context->dump_expression_list(this->args_);
10150 ast_dump_context->ostream() << ") ";
10153 // Make a call expression.
10156 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
10159 return new Call_expression(fn, args, is_varargs, location);
10162 // A single result from a call which returns multiple results.
10164 class Call_result_expression : public Expression
10167 Call_result_expression(Call_expression* call, unsigned int index)
10168 : Expression(EXPRESSION_CALL_RESULT, call->location()),
10169 call_(call), index_(index)
10174 do_traverse(Traverse*);
10180 do_determine_type(const Type_context*);
10183 do_check_types(Gogo*);
10188 return new Call_result_expression(this->call_->call_expression(),
10193 do_must_eval_in_order() const
10197 do_get_tree(Translate_context*);
10200 do_dump_expression(Ast_dump_context*) const;
10203 // The underlying call expression.
10205 // Which result we want.
10206 unsigned int index_;
10209 // Traverse a call result.
10212 Call_result_expression::do_traverse(Traverse* traverse)
10214 if (traverse->remember_expression(this->call_))
10216 // We have already traversed the call expression.
10217 return TRAVERSE_CONTINUE;
10219 return Expression::traverse(&this->call_, traverse);
10225 Call_result_expression::do_type()
10227 if (this->classification() == EXPRESSION_ERROR)
10228 return Type::make_error_type();
10230 // THIS->CALL_ can be replaced with a temporary reference due to
10231 // Call_expression::do_must_eval_in_order when there is an error.
10232 Call_expression* ce = this->call_->call_expression();
10235 this->set_is_error();
10236 return Type::make_error_type();
10238 Function_type* fntype = ce->get_function_type();
10239 if (fntype == NULL)
10241 if (ce->issue_error())
10243 if (!ce->fn()->type()->is_error())
10244 this->report_error(_("expected function"));
10246 this->set_is_error();
10247 return Type::make_error_type();
10249 const Typed_identifier_list* results = fntype->results();
10250 if (results == NULL || results->size() < 2)
10252 if (ce->issue_error())
10253 this->report_error(_("number of results does not match "
10254 "number of values"));
10255 return Type::make_error_type();
10257 Typed_identifier_list::const_iterator pr = results->begin();
10258 for (unsigned int i = 0; i < this->index_; ++i)
10260 if (pr == results->end())
10264 if (pr == results->end())
10266 if (ce->issue_error())
10267 this->report_error(_("number of results does not match "
10268 "number of values"));
10269 return Type::make_error_type();
10274 // Check the type. Just make sure that we trigger the warning in
10278 Call_result_expression::do_check_types(Gogo*)
10283 // Determine the type. We have nothing to do here, but the 0 result
10284 // needs to pass down to the caller.
10287 Call_result_expression::do_determine_type(const Type_context*)
10289 this->call_->determine_type_no_context();
10292 // Return the tree. We just refer to the temporary set by the call
10293 // expression. We don't do this at lowering time because it makes it
10294 // hard to evaluate the call at the right time.
10297 Call_result_expression::do_get_tree(Translate_context* context)
10299 Call_expression* ce = this->call_->call_expression();
10300 go_assert(ce != NULL);
10301 Temporary_statement* ts = ce->result(this->index_);
10302 Expression* ref = Expression::make_temporary_reference(ts, this->location());
10303 return ref->get_tree(context);
10306 // Dump ast representation for a call result expression.
10309 Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10312 // FIXME: Wouldn't it be better if the call is assigned to a temporary
10313 // (struct) and the fields are referenced instead.
10314 ast_dump_context->ostream() << this->index_ << "@(";
10315 ast_dump_context->dump_expression(this->call_);
10316 ast_dump_context->ostream() << ")";
10319 // Make a reference to a single result of a call which returns
10320 // multiple results.
10323 Expression::make_call_result(Call_expression* call, unsigned int index)
10325 return new Call_result_expression(call, index);
10328 // Class Index_expression.
10333 Index_expression::do_traverse(Traverse* traverse)
10335 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
10336 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
10337 || (this->end_ != NULL
10338 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
10339 return TRAVERSE_EXIT;
10340 return TRAVERSE_CONTINUE;
10343 // Lower an index expression. This converts the generic index
10344 // expression into an array index, a string index, or a map index.
10347 Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
10349 Location location = this->location();
10350 Expression* left = this->left_;
10351 Expression* start = this->start_;
10352 Expression* end = this->end_;
10354 Type* type = left->type();
10355 if (type->is_error())
10356 return Expression::make_error(location);
10357 else if (left->is_type_expression())
10359 error_at(location, "attempt to index type expression");
10360 return Expression::make_error(location);
10362 else if (type->array_type() != NULL)
10363 return Expression::make_array_index(left, start, end, location);
10364 else if (type->points_to() != NULL
10365 && type->points_to()->array_type() != NULL
10366 && !type->points_to()->is_slice_type())
10368 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
10370 return Expression::make_array_index(deref, start, end, location);
10372 else if (type->is_string_type())
10373 return Expression::make_string_index(left, start, end, location);
10374 else if (type->map_type() != NULL)
10378 error_at(location, "invalid slice of map");
10379 return Expression::make_error(location);
10381 Map_index_expression* ret = Expression::make_map_index(left, start,
10383 if (this->is_lvalue_)
10384 ret->set_is_lvalue();
10390 "attempt to index object which is not array, string, or map");
10391 return Expression::make_error(location);
10395 // Write an indexed expression (expr[expr:expr] or expr[expr]) to a
10399 Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
10400 const Expression* expr,
10401 const Expression* start,
10402 const Expression* end)
10404 expr->dump_expression(ast_dump_context);
10405 ast_dump_context->ostream() << "[";
10406 start->dump_expression(ast_dump_context);
10409 ast_dump_context->ostream() << ":";
10410 end->dump_expression(ast_dump_context);
10412 ast_dump_context->ostream() << "]";
10415 // Dump ast representation for an index expression.
10418 Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10421 Index_expression::dump_index_expression(ast_dump_context, this->left_,
10422 this->start_, this->end_);
10425 // Make an index expression.
10428 Expression::make_index(Expression* left, Expression* start, Expression* end,
10431 return new Index_expression(left, start, end, location);
10434 // An array index. This is used for both indexing and slicing.
10436 class Array_index_expression : public Expression
10439 Array_index_expression(Expression* array, Expression* start,
10440 Expression* end, Location location)
10441 : Expression(EXPRESSION_ARRAY_INDEX, location),
10442 array_(array), start_(start), end_(end), type_(NULL)
10447 do_traverse(Traverse*);
10453 do_determine_type(const Type_context*);
10456 do_check_types(Gogo*);
10461 return Expression::make_array_index(this->array_->copy(),
10462 this->start_->copy(),
10463 (this->end_ == NULL
10465 : this->end_->copy()),
10470 do_must_eval_subexpressions_in_order(int* skip) const
10477 do_is_addressable() const;
10480 do_address_taken(bool escapes)
10481 { this->array_->address_taken(escapes); }
10484 do_get_tree(Translate_context*);
10487 do_dump_expression(Ast_dump_context*) const;
10490 // The array we are getting a value from.
10491 Expression* array_;
10492 // The start or only index.
10493 Expression* start_;
10494 // The end index of a slice. This may be NULL for a simple array
10495 // index, or it may be a nil expression for the length of the array.
10497 // The type of the expression.
10501 // Array index traversal.
10504 Array_index_expression::do_traverse(Traverse* traverse)
10506 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
10507 return TRAVERSE_EXIT;
10508 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10509 return TRAVERSE_EXIT;
10510 if (this->end_ != NULL)
10512 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10513 return TRAVERSE_EXIT;
10515 return TRAVERSE_CONTINUE;
10518 // Return the type of an array index.
10521 Array_index_expression::do_type()
10523 if (this->type_ == NULL)
10525 Array_type* type = this->array_->type()->array_type();
10527 this->type_ = Type::make_error_type();
10528 else if (this->end_ == NULL)
10529 this->type_ = type->element_type();
10530 else if (type->is_slice_type())
10532 // A slice of a slice has the same type as the original
10534 this->type_ = this->array_->type()->deref();
10538 // A slice of an array is a slice.
10539 this->type_ = Type::make_array_type(type->element_type(), NULL);
10542 return this->type_;
10545 // Set the type of an array index.
10548 Array_index_expression::do_determine_type(const Type_context*)
10550 this->array_->determine_type_no_context();
10551 this->start_->determine_type_no_context();
10552 if (this->end_ != NULL)
10553 this->end_->determine_type_no_context();
10556 // Check types of an array index.
10559 Array_index_expression::do_check_types(Gogo*)
10561 if (this->start_->type()->integer_type() == NULL)
10562 this->report_error(_("index must be integer"));
10563 if (this->end_ != NULL
10564 && this->end_->type()->integer_type() == NULL
10565 && !this->end_->type()->is_error()
10566 && !this->end_->is_nil_expression()
10567 && !this->end_->is_error_expression())
10568 this->report_error(_("slice end must be integer"));
10570 Array_type* array_type = this->array_->type()->array_type();
10571 if (array_type == NULL)
10573 go_assert(this->array_->type()->is_error());
10577 unsigned int int_bits =
10578 Type::lookup_integer_type("int")->integer_type()->bits();
10583 bool lval_valid = (array_type->length() != NULL
10584 && array_type->length()->integer_constant_value(true,
10589 if (this->start_->integer_constant_value(true, ival, &dummy))
10591 if (mpz_sgn(ival) < 0
10592 || mpz_sizeinbase(ival, 2) >= int_bits
10594 && (this->end_ == NULL
10595 ? mpz_cmp(ival, lval) >= 0
10596 : mpz_cmp(ival, lval) > 0)))
10598 error_at(this->start_->location(), "array index out of bounds");
10599 this->set_is_error();
10602 if (this->end_ != NULL && !this->end_->is_nil_expression())
10604 if (this->end_->integer_constant_value(true, ival, &dummy))
10606 if (mpz_sgn(ival) < 0
10607 || mpz_sizeinbase(ival, 2) >= int_bits
10608 || (lval_valid && mpz_cmp(ival, lval) > 0))
10610 error_at(this->end_->location(), "array index out of bounds");
10611 this->set_is_error();
10618 // A slice of an array requires an addressable array. A slice of a
10619 // slice is always possible.
10620 if (this->end_ != NULL && !array_type->is_slice_type())
10622 if (!this->array_->is_addressable())
10623 this->report_error(_("slice of unaddressable value"));
10625 this->array_->address_taken(true);
10629 // Return whether this expression is addressable.
10632 Array_index_expression::do_is_addressable() const
10634 // A slice expression is not addressable.
10635 if (this->end_ != NULL)
10638 // An index into a slice is addressable.
10639 if (this->array_->type()->is_slice_type())
10642 // An index into an array is addressable if the array is
10644 return this->array_->is_addressable();
10647 // Get a tree for an array index.
10650 Array_index_expression::do_get_tree(Translate_context* context)
10652 Gogo* gogo = context->gogo();
10653 Location loc = this->location();
10655 Array_type* array_type = this->array_->type()->array_type();
10656 if (array_type == NULL)
10658 go_assert(this->array_->type()->is_error());
10659 return error_mark_node;
10662 tree type_tree = type_to_tree(array_type->get_backend(gogo));
10663 if (type_tree == error_mark_node)
10664 return error_mark_node;
10666 tree array_tree = this->array_->get_tree(context);
10667 if (array_tree == error_mark_node)
10668 return error_mark_node;
10670 if (array_type->length() == NULL && !DECL_P(array_tree))
10671 array_tree = save_expr(array_tree);
10673 tree length_tree = NULL_TREE;
10674 if (this->end_ == NULL || this->end_->is_nil_expression())
10676 length_tree = array_type->length_tree(gogo, array_tree);
10677 if (length_tree == error_mark_node)
10678 return error_mark_node;
10679 length_tree = save_expr(length_tree);
10682 tree capacity_tree = NULL_TREE;
10683 if (this->end_ != NULL)
10685 capacity_tree = array_type->capacity_tree(gogo, array_tree);
10686 if (capacity_tree == error_mark_node)
10687 return error_mark_node;
10688 capacity_tree = save_expr(capacity_tree);
10691 tree length_type = (length_tree != NULL_TREE
10692 ? TREE_TYPE(length_tree)
10693 : TREE_TYPE(capacity_tree));
10695 tree bad_index = boolean_false_node;
10697 tree start_tree = this->start_->get_tree(context);
10698 if (start_tree == error_mark_node)
10699 return error_mark_node;
10700 if (!DECL_P(start_tree))
10701 start_tree = save_expr(start_tree);
10702 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
10703 start_tree = convert_to_integer(length_type, start_tree);
10705 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
10708 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
10709 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10710 boolean_type_node, bad_index,
10711 fold_build2_loc(loc.gcc_location(),
10712 (this->end_ == NULL
10715 boolean_type_node, start_tree,
10716 (this->end_ == NULL
10718 : capacity_tree)));
10720 int code = (array_type->length() != NULL
10721 ? (this->end_ == NULL
10722 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
10723 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
10724 : (this->end_ == NULL
10725 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
10726 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
10727 tree crash = Gogo::runtime_error(code, loc);
10729 if (this->end_ == NULL)
10731 // Simple array indexing. This has to return an l-value, so
10732 // wrap the index check into START_TREE.
10733 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
10734 build3(COND_EXPR, void_type_node,
10735 bad_index, crash, NULL_TREE),
10737 start_tree = fold_convert_loc(loc.gcc_location(), sizetype, start_tree);
10739 if (array_type->length() != NULL)
10742 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
10743 start_tree, NULL_TREE, NULL_TREE);
10748 tree values = array_type->value_pointer_tree(gogo, array_tree);
10749 Type* element_type = array_type->element_type();
10750 Btype* belement_type = element_type->get_backend(gogo);
10751 tree element_type_tree = type_to_tree(belement_type);
10752 if (element_type_tree == error_mark_node)
10753 return error_mark_node;
10754 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
10755 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
10756 start_tree, element_size);
10757 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
10758 TREE_TYPE(values), values, offset);
10759 return build_fold_indirect_ref(ptr);
10766 if (this->end_->is_nil_expression())
10767 end_tree = length_tree;
10770 end_tree = this->end_->get_tree(context);
10771 if (end_tree == error_mark_node)
10772 return error_mark_node;
10773 if (!DECL_P(end_tree))
10774 end_tree = save_expr(end_tree);
10775 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
10776 end_tree = convert_to_integer(length_type, end_tree);
10778 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
10781 end_tree = fold_convert_loc(loc.gcc_location(), length_type, end_tree);
10783 tree bad_end = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10785 fold_build2_loc(loc.gcc_location(),
10786 LT_EXPR, boolean_type_node,
10787 end_tree, start_tree),
10788 fold_build2_loc(loc.gcc_location(),
10789 GT_EXPR, boolean_type_node,
10790 end_tree, capacity_tree));
10791 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10792 boolean_type_node, bad_index, bad_end);
10795 Type* element_type = array_type->element_type();
10796 tree element_type_tree = type_to_tree(element_type->get_backend(gogo));
10797 if (element_type_tree == error_mark_node)
10798 return error_mark_node;
10799 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
10801 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
10802 fold_convert_loc(loc.gcc_location(), sizetype,
10806 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
10807 if (value_pointer == error_mark_node)
10808 return error_mark_node;
10810 value_pointer = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
10811 TREE_TYPE(value_pointer),
10812 value_pointer, offset);
10814 tree result_length_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
10815 length_type, end_tree, start_tree);
10817 tree result_capacity_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
10818 length_type, capacity_tree,
10821 tree struct_tree = type_to_tree(this->type()->get_backend(gogo));
10822 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
10824 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
10826 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
10827 tree field = TYPE_FIELDS(struct_tree);
10828 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
10829 elt->index = field;
10830 elt->value = value_pointer;
10832 elt = VEC_quick_push(constructor_elt, init, NULL);
10833 field = DECL_CHAIN(field);
10834 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
10835 elt->index = field;
10836 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
10837 result_length_tree);
10839 elt = VEC_quick_push(constructor_elt, init, NULL);
10840 field = DECL_CHAIN(field);
10841 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
10842 elt->index = field;
10843 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
10844 result_capacity_tree);
10846 tree constructor = build_constructor(struct_tree, init);
10848 if (TREE_CONSTANT(value_pointer)
10849 && TREE_CONSTANT(result_length_tree)
10850 && TREE_CONSTANT(result_capacity_tree))
10851 TREE_CONSTANT(constructor) = 1;
10853 return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
10854 TREE_TYPE(constructor),
10855 build3(COND_EXPR, void_type_node,
10856 bad_index, crash, NULL_TREE),
10860 // Dump ast representation for an array index expression.
10863 Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10866 Index_expression::dump_index_expression(ast_dump_context, this->array_,
10867 this->start_, this->end_);
10870 // Make an array index expression. END may be NULL.
10873 Expression::make_array_index(Expression* array, Expression* start,
10874 Expression* end, Location location)
10876 return new Array_index_expression(array, start, end, location);
10879 // A string index. This is used for both indexing and slicing.
10881 class String_index_expression : public Expression
10884 String_index_expression(Expression* string, Expression* start,
10885 Expression* end, Location location)
10886 : Expression(EXPRESSION_STRING_INDEX, location),
10887 string_(string), start_(start), end_(end)
10892 do_traverse(Traverse*);
10898 do_determine_type(const Type_context*);
10901 do_check_types(Gogo*);
10906 return Expression::make_string_index(this->string_->copy(),
10907 this->start_->copy(),
10908 (this->end_ == NULL
10910 : this->end_->copy()),
10915 do_must_eval_subexpressions_in_order(int* skip) const
10922 do_get_tree(Translate_context*);
10925 do_dump_expression(Ast_dump_context*) const;
10928 // The string we are getting a value from.
10929 Expression* string_;
10930 // The start or only index.
10931 Expression* start_;
10932 // The end index of a slice. This may be NULL for a single index,
10933 // or it may be a nil expression for the length of the string.
10937 // String index traversal.
10940 String_index_expression::do_traverse(Traverse* traverse)
10942 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
10943 return TRAVERSE_EXIT;
10944 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10945 return TRAVERSE_EXIT;
10946 if (this->end_ != NULL)
10948 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10949 return TRAVERSE_EXIT;
10951 return TRAVERSE_CONTINUE;
10954 // Return the type of a string index.
10957 String_index_expression::do_type()
10959 if (this->end_ == NULL)
10960 return Type::lookup_integer_type("uint8");
10962 return this->string_->type();
10965 // Determine the type of a string index.
10968 String_index_expression::do_determine_type(const Type_context*)
10970 this->string_->determine_type_no_context();
10971 this->start_->determine_type_no_context();
10972 if (this->end_ != NULL)
10973 this->end_->determine_type_no_context();
10976 // Check types of a string index.
10979 String_index_expression::do_check_types(Gogo*)
10981 if (this->start_->type()->integer_type() == NULL)
10982 this->report_error(_("index must be integer"));
10983 if (this->end_ != NULL
10984 && this->end_->type()->integer_type() == NULL
10985 && !this->end_->is_nil_expression())
10986 this->report_error(_("slice end must be integer"));
10989 bool sval_valid = this->string_->string_constant_value(&sval);
10994 if (this->start_->integer_constant_value(true, ival, &dummy))
10996 if (mpz_sgn(ival) < 0
10997 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
10999 error_at(this->start_->location(), "string index out of bounds");
11000 this->set_is_error();
11003 if (this->end_ != NULL && !this->end_->is_nil_expression())
11005 if (this->end_->integer_constant_value(true, ival, &dummy))
11007 if (mpz_sgn(ival) < 0
11008 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
11010 error_at(this->end_->location(), "string index out of bounds");
11011 this->set_is_error();
11018 // Get a tree for a string index.
11021 String_index_expression::do_get_tree(Translate_context* context)
11023 Location loc = this->location();
11025 tree string_tree = this->string_->get_tree(context);
11026 if (string_tree == error_mark_node)
11027 return error_mark_node;
11029 if (this->string_->type()->points_to() != NULL)
11030 string_tree = build_fold_indirect_ref(string_tree);
11031 if (!DECL_P(string_tree))
11032 string_tree = save_expr(string_tree);
11033 tree string_type = TREE_TYPE(string_tree);
11035 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
11036 length_tree = save_expr(length_tree);
11037 tree length_type = TREE_TYPE(length_tree);
11039 tree bad_index = boolean_false_node;
11041 tree start_tree = this->start_->get_tree(context);
11042 if (start_tree == error_mark_node)
11043 return error_mark_node;
11044 if (!DECL_P(start_tree))
11045 start_tree = save_expr(start_tree);
11046 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
11047 start_tree = convert_to_integer(length_type, start_tree);
11049 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
11052 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
11054 int code = (this->end_ == NULL
11055 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
11056 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
11057 tree crash = Gogo::runtime_error(code, loc);
11059 if (this->end_ == NULL)
11061 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
11062 boolean_type_node, bad_index,
11063 fold_build2_loc(loc.gcc_location(), GE_EXPR,
11065 start_tree, length_tree));
11067 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
11068 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
11069 TREE_TYPE(bytes_tree),
11071 fold_convert_loc(loc.gcc_location(), sizetype,
11073 tree index = build_fold_indirect_ref_loc(loc.gcc_location(), ptr);
11075 return build2(COMPOUND_EXPR, TREE_TYPE(index),
11076 build3(COND_EXPR, void_type_node,
11077 bad_index, crash, NULL_TREE),
11083 if (this->end_->is_nil_expression())
11084 end_tree = build_int_cst(length_type, -1);
11087 end_tree = this->end_->get_tree(context);
11088 if (end_tree == error_mark_node)
11089 return error_mark_node;
11090 if (!DECL_P(end_tree))
11091 end_tree = save_expr(end_tree);
11092 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
11093 end_tree = convert_to_integer(length_type, end_tree);
11095 bad_index = Expression::check_bounds(end_tree, length_type,
11098 end_tree = fold_convert_loc(loc.gcc_location(), length_type,
11102 static tree strslice_fndecl;
11103 tree ret = Gogo::call_builtin(&strslice_fndecl,
11105 "__go_string_slice",
11114 if (ret == error_mark_node)
11115 return error_mark_node;
11116 // This will panic if the bounds are out of range for the
11118 TREE_NOTHROW(strslice_fndecl) = 0;
11120 if (bad_index == boolean_false_node)
11123 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
11124 build3(COND_EXPR, void_type_node,
11125 bad_index, crash, NULL_TREE),
11130 // Dump ast representation for a string index expression.
11133 String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11136 Index_expression::dump_index_expression(ast_dump_context, this->string_,
11137 this->start_, this->end_);
11140 // Make a string index expression. END may be NULL.
11143 Expression::make_string_index(Expression* string, Expression* start,
11144 Expression* end, Location location)
11146 return new String_index_expression(string, start, end, location);
11149 // Class Map_index.
11151 // Get the type of the map.
11154 Map_index_expression::get_map_type() const
11156 Map_type* mt = this->map_->type()->deref()->map_type();
11158 go_assert(saw_errors());
11162 // Map index traversal.
11165 Map_index_expression::do_traverse(Traverse* traverse)
11167 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
11168 return TRAVERSE_EXIT;
11169 return Expression::traverse(&this->index_, traverse);
11172 // Return the type of a map index.
11175 Map_index_expression::do_type()
11177 Map_type* mt = this->get_map_type();
11179 return Type::make_error_type();
11180 Type* type = mt->val_type();
11181 // If this map index is in a tuple assignment, we actually return a
11182 // pointer to the value type. Tuple_map_assignment_statement is
11183 // responsible for handling this correctly. We need to get the type
11184 // right in case this gets assigned to a temporary variable.
11185 if (this->is_in_tuple_assignment_)
11186 type = Type::make_pointer_type(type);
11190 // Fix the type of a map index.
11193 Map_index_expression::do_determine_type(const Type_context*)
11195 this->map_->determine_type_no_context();
11196 Map_type* mt = this->get_map_type();
11197 Type* key_type = mt == NULL ? NULL : mt->key_type();
11198 Type_context subcontext(key_type, false);
11199 this->index_->determine_type(&subcontext);
11202 // Check types of a map index.
11205 Map_index_expression::do_check_types(Gogo*)
11207 std::string reason;
11208 Map_type* mt = this->get_map_type();
11211 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
11213 if (reason.empty())
11214 this->report_error(_("incompatible type for map index"));
11217 error_at(this->location(), "incompatible type for map index (%s)",
11219 this->set_is_error();
11224 // Get a tree for a map index.
11227 Map_index_expression::do_get_tree(Translate_context* context)
11229 Map_type* type = this->get_map_type();
11231 return error_mark_node;
11233 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
11234 if (valptr == error_mark_node)
11235 return error_mark_node;
11236 valptr = save_expr(valptr);
11238 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
11240 if (this->is_lvalue_)
11241 return build_fold_indirect_ref(valptr);
11242 else if (this->is_in_tuple_assignment_)
11244 // Tuple_map_assignment_statement is responsible for using this
11250 Gogo* gogo = context->gogo();
11251 Btype* val_btype = type->val_type()->get_backend(gogo);
11252 Bexpression* val_zero = gogo->backend()->zero_expression(val_btype);
11253 return fold_build3(COND_EXPR, val_type_tree,
11254 fold_build2(EQ_EXPR, boolean_type_node, valptr,
11255 fold_convert(TREE_TYPE(valptr),
11256 null_pointer_node)),
11257 expr_to_tree(val_zero),
11258 build_fold_indirect_ref(valptr));
11262 // Get a tree for the map index. This returns a tree which evaluates
11263 // to a pointer to a value. The pointer will be NULL if the key is
11267 Map_index_expression::get_value_pointer(Translate_context* context,
11270 Map_type* type = this->get_map_type();
11272 return error_mark_node;
11274 tree map_tree = this->map_->get_tree(context);
11275 tree index_tree = this->index_->get_tree(context);
11276 index_tree = Expression::convert_for_assignment(context, type->key_type(),
11277 this->index_->type(),
11280 if (map_tree == error_mark_node || index_tree == error_mark_node)
11281 return error_mark_node;
11283 if (this->map_->type()->points_to() != NULL)
11284 map_tree = build_fold_indirect_ref(map_tree);
11286 // We need to pass in a pointer to the key, so stuff it into a
11290 if (current_function_decl != NULL)
11292 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
11293 DECL_IGNORED_P(tmp) = 0;
11294 DECL_INITIAL(tmp) = index_tree;
11295 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
11296 TREE_ADDRESSABLE(tmp) = 1;
11300 tmp = build_decl(this->location().gcc_location(), VAR_DECL,
11301 create_tmp_var_name("M"),
11302 TREE_TYPE(index_tree));
11303 DECL_EXTERNAL(tmp) = 0;
11304 TREE_PUBLIC(tmp) = 0;
11305 TREE_STATIC(tmp) = 1;
11306 DECL_ARTIFICIAL(tmp) = 1;
11307 if (!TREE_CONSTANT(index_tree))
11308 make_tmp = fold_build2_loc(this->location().gcc_location(),
11309 INIT_EXPR, void_type_node,
11313 TREE_READONLY(tmp) = 1;
11314 TREE_CONSTANT(tmp) = 1;
11315 DECL_INITIAL(tmp) = index_tree;
11316 make_tmp = NULL_TREE;
11318 rest_of_decl_compilation(tmp, 1, 0);
11321 fold_convert_loc(this->location().gcc_location(), const_ptr_type_node,
11322 build_fold_addr_expr_loc(this->location().gcc_location(),
11325 static tree map_index_fndecl;
11326 tree call = Gogo::call_builtin(&map_index_fndecl,
11330 const_ptr_type_node,
11331 TREE_TYPE(map_tree),
11333 const_ptr_type_node,
11337 ? boolean_true_node
11338 : boolean_false_node));
11339 if (call == error_mark_node)
11340 return error_mark_node;
11341 // This can panic on a map of interface type if the interface holds
11342 // an uncomparable or unhashable type.
11343 TREE_NOTHROW(map_index_fndecl) = 0;
11345 Type* val_type = type->val_type();
11346 tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo()));
11347 if (val_type_tree == error_mark_node)
11348 return error_mark_node;
11349 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
11351 tree ret = fold_convert_loc(this->location().gcc_location(),
11352 ptr_val_type_tree, call);
11353 if (make_tmp != NULL_TREE)
11354 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
11358 // Dump ast representation for a map index expression
11361 Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11364 Index_expression::dump_index_expression(ast_dump_context,
11365 this->map_, this->index_, NULL);
11368 // Make a map index expression.
11370 Map_index_expression*
11371 Expression::make_map_index(Expression* map, Expression* index,
11374 return new Map_index_expression(map, index, location);
11377 // Class Field_reference_expression.
11379 // Return the type of a field reference.
11382 Field_reference_expression::do_type()
11384 Type* type = this->expr_->type();
11385 if (type->is_error())
11387 Struct_type* struct_type = type->struct_type();
11388 go_assert(struct_type != NULL);
11389 return struct_type->field(this->field_index_)->type();
11392 // Check the types for a field reference.
11395 Field_reference_expression::do_check_types(Gogo*)
11397 Type* type = this->expr_->type();
11398 if (type->is_error())
11400 Struct_type* struct_type = type->struct_type();
11401 go_assert(struct_type != NULL);
11402 go_assert(struct_type->field(this->field_index_) != NULL);
11405 // Get a tree for a field reference.
11408 Field_reference_expression::do_get_tree(Translate_context* context)
11410 tree struct_tree = this->expr_->get_tree(context);
11411 if (struct_tree == error_mark_node
11412 || TREE_TYPE(struct_tree) == error_mark_node)
11413 return error_mark_node;
11414 go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
11415 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
11416 if (field == NULL_TREE)
11418 // This can happen for a type which refers to itself indirectly
11419 // and then turns out to be erroneous.
11420 go_assert(saw_errors());
11421 return error_mark_node;
11423 for (unsigned int i = this->field_index_; i > 0; --i)
11425 field = DECL_CHAIN(field);
11426 go_assert(field != NULL_TREE);
11428 if (TREE_TYPE(field) == error_mark_node)
11429 return error_mark_node;
11430 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
11434 // Dump ast representation for a field reference expression.
11437 Field_reference_expression::do_dump_expression(
11438 Ast_dump_context* ast_dump_context) const
11440 this->expr_->dump_expression(ast_dump_context);
11441 ast_dump_context->ostream() << "." << this->field_index_;
11444 // Make a reference to a qualified identifier in an expression.
11446 Field_reference_expression*
11447 Expression::make_field_reference(Expression* expr, unsigned int field_index,
11450 return new Field_reference_expression(expr, field_index, location);
11453 // Class Interface_field_reference_expression.
11455 // Return a tree for the pointer to the function to call.
11458 Interface_field_reference_expression::get_function_tree(Translate_context*,
11461 if (this->expr_->type()->points_to() != NULL)
11462 expr = build_fold_indirect_ref(expr);
11464 tree expr_type = TREE_TYPE(expr);
11465 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
11467 tree field = TYPE_FIELDS(expr_type);
11468 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
11470 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
11471 go_assert(POINTER_TYPE_P(TREE_TYPE(table)));
11473 table = build_fold_indirect_ref(table);
11474 go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
11476 std::string name = Gogo::unpack_hidden_name(this->name_);
11477 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
11478 field != NULL_TREE;
11479 field = DECL_CHAIN(field))
11481 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
11484 go_assert(field != NULL_TREE);
11486 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
11489 // Return a tree for the first argument to pass to the interface
11493 Interface_field_reference_expression::get_underlying_object_tree(
11494 Translate_context*,
11497 if (this->expr_->type()->points_to() != NULL)
11498 expr = build_fold_indirect_ref(expr);
11500 tree expr_type = TREE_TYPE(expr);
11501 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
11503 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
11504 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
11506 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
11512 Interface_field_reference_expression::do_traverse(Traverse* traverse)
11514 return Expression::traverse(&this->expr_, traverse);
11517 // Return the type of an interface field reference.
11520 Interface_field_reference_expression::do_type()
11522 Type* expr_type = this->expr_->type();
11524 Type* points_to = expr_type->points_to();
11525 if (points_to != NULL)
11526 expr_type = points_to;
11528 Interface_type* interface_type = expr_type->interface_type();
11529 if (interface_type == NULL)
11530 return Type::make_error_type();
11532 const Typed_identifier* method = interface_type->find_method(this->name_);
11533 if (method == NULL)
11534 return Type::make_error_type();
11536 return method->type();
11539 // Determine types.
11542 Interface_field_reference_expression::do_determine_type(const Type_context*)
11544 this->expr_->determine_type_no_context();
11547 // Check the types for an interface field reference.
11550 Interface_field_reference_expression::do_check_types(Gogo*)
11552 Type* type = this->expr_->type();
11554 Type* points_to = type->points_to();
11555 if (points_to != NULL)
11558 Interface_type* interface_type = type->interface_type();
11559 if (interface_type == NULL)
11561 if (!type->is_error_type())
11562 this->report_error(_("expected interface or pointer to interface"));
11566 const Typed_identifier* method =
11567 interface_type->find_method(this->name_);
11568 if (method == NULL)
11570 error_at(this->location(), "method %qs not in interface",
11571 Gogo::message_name(this->name_).c_str());
11572 this->set_is_error();
11577 // Get a tree for a reference to a field in an interface. There is no
11578 // standard tree type representation for this: it's a function
11579 // attached to its first argument, like a Bound_method_expression.
11580 // The only places it may currently be used are in a Call_expression
11581 // or a Go_statement, which will take it apart directly. So this has
11582 // nothing to do at present.
11585 Interface_field_reference_expression::do_get_tree(Translate_context*)
11590 // Dump ast representation for an interface field reference.
11593 Interface_field_reference_expression::do_dump_expression(
11594 Ast_dump_context* ast_dump_context) const
11596 this->expr_->dump_expression(ast_dump_context);
11597 ast_dump_context->ostream() << "." << this->name_;
11600 // Make a reference to a field in an interface.
11603 Expression::make_interface_field_reference(Expression* expr,
11604 const std::string& field,
11607 return new Interface_field_reference_expression(expr, field, location);
11610 // A general selector. This is a Parser_expression for LEFT.NAME. It
11611 // is lowered after we know the type of the left hand side.
11613 class Selector_expression : public Parser_expression
11616 Selector_expression(Expression* left, const std::string& name,
11618 : Parser_expression(EXPRESSION_SELECTOR, location),
11619 left_(left), name_(name)
11624 do_traverse(Traverse* traverse)
11625 { return Expression::traverse(&this->left_, traverse); }
11628 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
11633 return new Selector_expression(this->left_->copy(), this->name_,
11638 do_dump_expression(Ast_dump_context* ast_dump_context) const;
11642 lower_method_expression(Gogo*);
11644 // The expression on the left hand side.
11646 // The name on the right hand side.
11650 // Lower a selector expression once we know the real type of the left
11654 Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*,
11657 Expression* left = this->left_;
11658 if (left->is_type_expression())
11659 return this->lower_method_expression(gogo);
11660 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
11664 // Lower a method expression T.M or (*T).M. We turn this into a
11665 // function literal.
11668 Selector_expression::lower_method_expression(Gogo* gogo)
11670 Location location = this->location();
11671 Type* type = this->left_->type();
11672 const std::string& name(this->name_);
11675 if (type->points_to() == NULL)
11676 is_pointer = false;
11680 type = type->points_to();
11682 Named_type* nt = type->named_type();
11686 ("method expression requires named type or "
11687 "pointer to named type"));
11688 return Expression::make_error(location);
11692 Method* method = nt->method_function(name, &is_ambiguous);
11693 const Typed_identifier* imethod = NULL;
11694 if (method == NULL && !is_pointer)
11696 Interface_type* it = nt->interface_type();
11698 imethod = it->find_method(name);
11701 if (method == NULL && imethod == NULL)
11704 error_at(location, "type %<%s%s%> has no method %<%s%>",
11705 is_pointer ? "*" : "",
11706 nt->message_name().c_str(),
11707 Gogo::message_name(name).c_str());
11709 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
11710 Gogo::message_name(name).c_str(),
11711 is_pointer ? "*" : "",
11712 nt->message_name().c_str());
11713 return Expression::make_error(location);
11716 if (method != NULL && !is_pointer && !method->is_value_method())
11718 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
11719 nt->message_name().c_str(),
11720 Gogo::message_name(name).c_str());
11721 return Expression::make_error(location);
11724 // Build a new function type in which the receiver becomes the first
11726 Function_type* method_type;
11727 if (method != NULL)
11729 method_type = method->type();
11730 go_assert(method_type->is_method());
11734 method_type = imethod->type()->function_type();
11735 go_assert(method_type != NULL && !method_type->is_method());
11738 const char* const receiver_name = "$this";
11739 Typed_identifier_list* parameters = new Typed_identifier_list();
11740 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
11743 const Typed_identifier_list* method_parameters = method_type->parameters();
11744 if (method_parameters != NULL)
11747 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
11748 p != method_parameters->end();
11751 if (!p->name().empty() && p->name() != Import::import_marker)
11752 parameters->push_back(*p);
11756 snprintf(buf, sizeof buf, "$param%d", i);
11757 parameters->push_back(Typed_identifier(buf, p->type(),
11763 const Typed_identifier_list* method_results = method_type->results();
11764 Typed_identifier_list* results;
11765 if (method_results == NULL)
11769 results = new Typed_identifier_list();
11770 for (Typed_identifier_list::const_iterator p = method_results->begin();
11771 p != method_results->end();
11773 results->push_back(*p);
11776 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
11778 if (method_type->is_varargs())
11779 fntype->set_is_varargs();
11781 // We generate methods which always takes a pointer to the receiver
11782 // as their first argument. If this is for a pointer type, we can
11783 // simply reuse the existing function. We use an internal hack to
11784 // get the right type.
11786 if (method != NULL && is_pointer)
11788 Named_object* mno = (method->needs_stub_method()
11789 ? method->stub_object()
11790 : method->named_object());
11791 Expression* f = Expression::make_func_reference(mno, NULL, location);
11792 f = Expression::make_cast(fntype, f, location);
11793 Type_conversion_expression* tce =
11794 static_cast<Type_conversion_expression*>(f);
11795 tce->set_may_convert_function_types();
11799 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
11802 Named_object* vno = gogo->lookup(receiver_name, NULL);
11803 go_assert(vno != NULL);
11804 Expression* ve = Expression::make_var_reference(vno, location);
11806 if (method != NULL)
11807 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
11809 bm = Expression::make_interface_field_reference(ve, name, location);
11811 // Even though we found the method above, if it has an error type we
11812 // may see an error here.
11813 if (bm->is_error_expression())
11815 gogo->finish_function(location);
11819 Expression_list* args;
11820 if (parameters->size() <= 1)
11824 args = new Expression_list();
11825 Typed_identifier_list::const_iterator p = parameters->begin();
11827 for (; p != parameters->end(); ++p)
11829 vno = gogo->lookup(p->name(), NULL);
11830 go_assert(vno != NULL);
11831 args->push_back(Expression::make_var_reference(vno, location));
11835 gogo->start_block(location);
11837 Call_expression* call = Expression::make_call(bm, args,
11838 method_type->is_varargs(),
11841 size_t count = call->result_count();
11844 s = Statement::make_statement(call, true);
11847 Expression_list* retvals = new Expression_list();
11849 retvals->push_back(call);
11852 for (size_t i = 0; i < count; ++i)
11853 retvals->push_back(Expression::make_call_result(call, i));
11855 s = Statement::make_return_statement(retvals, location);
11857 gogo->add_statement(s);
11859 Block* b = gogo->finish_block(location);
11861 gogo->add_block(b, location);
11863 // Lower the call in case there are multiple results.
11864 gogo->lower_block(no, b);
11866 gogo->finish_function(location);
11868 return Expression::make_func_reference(no, NULL, location);
11871 // Dump the ast for a selector expression.
11874 Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11877 ast_dump_context->dump_expression(this->left_);
11878 ast_dump_context->ostream() << ".";
11879 ast_dump_context->ostream() << this->name_;
11882 // Make a selector expression.
11885 Expression::make_selector(Expression* left, const std::string& name,
11888 return new Selector_expression(left, name, location);
11891 // Implement the builtin function new.
11893 class Allocation_expression : public Expression
11896 Allocation_expression(Type* type, Location location)
11897 : Expression(EXPRESSION_ALLOCATION, location),
11903 do_traverse(Traverse* traverse)
11904 { return Type::traverse(this->type_, traverse); }
11908 { return Type::make_pointer_type(this->type_); }
11911 do_determine_type(const Type_context*)
11916 { return new Allocation_expression(this->type_, this->location()); }
11919 do_get_tree(Translate_context*);
11922 do_dump_expression(Ast_dump_context*) const;
11925 // The type we are allocating.
11929 // Return a tree for an allocation expression.
11932 Allocation_expression::do_get_tree(Translate_context* context)
11934 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
11935 if (type_tree == error_mark_node)
11936 return error_mark_node;
11937 tree size_tree = TYPE_SIZE_UNIT(type_tree);
11938 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
11940 if (space == error_mark_node)
11941 return error_mark_node;
11942 return fold_convert(build_pointer_type(type_tree), space);
11945 // Dump ast representation for an allocation expression.
11948 Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11951 ast_dump_context->ostream() << "new(";
11952 ast_dump_context->dump_type(this->type_);
11953 ast_dump_context->ostream() << ")";
11956 // Make an allocation expression.
11959 Expression::make_allocation(Type* type, Location location)
11961 return new Allocation_expression(type, location);
11964 // Construct a struct.
11966 class Struct_construction_expression : public Expression
11969 Struct_construction_expression(Type* type, Expression_list* vals,
11971 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
11972 type_(type), vals_(vals)
11975 // Return whether this is a constant initializer.
11977 is_constant_struct() const;
11981 do_traverse(Traverse* traverse);
11985 { return this->type_; }
11988 do_determine_type(const Type_context*);
11991 do_check_types(Gogo*);
11996 return new Struct_construction_expression(this->type_, this->vals_->copy(),
12001 do_get_tree(Translate_context*);
12004 do_export(Export*) const;
12007 do_dump_expression(Ast_dump_context*) const;
12010 // The type of the struct to construct.
12012 // The list of values, in order of the fields in the struct. A NULL
12013 // entry means that the field should be zero-initialized.
12014 Expression_list* vals_;
12020 Struct_construction_expression::do_traverse(Traverse* traverse)
12022 if (this->vals_ != NULL
12023 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12024 return TRAVERSE_EXIT;
12025 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12026 return TRAVERSE_EXIT;
12027 return TRAVERSE_CONTINUE;
12030 // Return whether this is a constant initializer.
12033 Struct_construction_expression::is_constant_struct() const
12035 if (this->vals_ == NULL)
12037 for (Expression_list::const_iterator pv = this->vals_->begin();
12038 pv != this->vals_->end();
12042 && !(*pv)->is_constant()
12043 && (!(*pv)->is_composite_literal()
12044 || (*pv)->is_nonconstant_composite_literal()))
12048 const Struct_field_list* fields = this->type_->struct_type()->fields();
12049 for (Struct_field_list::const_iterator pf = fields->begin();
12050 pf != fields->end();
12053 // There are no constant constructors for interfaces.
12054 if (pf->type()->interface_type() != NULL)
12061 // Final type determination.
12064 Struct_construction_expression::do_determine_type(const Type_context*)
12066 if (this->vals_ == NULL)
12068 const Struct_field_list* fields = this->type_->struct_type()->fields();
12069 Expression_list::const_iterator pv = this->vals_->begin();
12070 for (Struct_field_list::const_iterator pf = fields->begin();
12071 pf != fields->end();
12074 if (pv == this->vals_->end())
12078 Type_context subcontext(pf->type(), false);
12079 (*pv)->determine_type(&subcontext);
12082 // Extra values are an error we will report elsewhere; we still want
12083 // to determine the type to avoid knockon errors.
12084 for (; pv != this->vals_->end(); ++pv)
12085 (*pv)->determine_type_no_context();
12091 Struct_construction_expression::do_check_types(Gogo*)
12093 if (this->vals_ == NULL)
12096 Struct_type* st = this->type_->struct_type();
12097 if (this->vals_->size() > st->field_count())
12099 this->report_error(_("too many expressions for struct"));
12103 const Struct_field_list* fields = st->fields();
12104 Expression_list::const_iterator pv = this->vals_->begin();
12106 for (Struct_field_list::const_iterator pf = fields->begin();
12107 pf != fields->end();
12110 if (pv == this->vals_->end())
12112 this->report_error(_("too few expressions for struct"));
12119 std::string reason;
12120 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
12122 if (reason.empty())
12123 error_at((*pv)->location(),
12124 "incompatible type for field %d in struct construction",
12127 error_at((*pv)->location(),
12128 ("incompatible type for field %d in "
12129 "struct construction (%s)"),
12130 i + 1, reason.c_str());
12131 this->set_is_error();
12134 go_assert(pv == this->vals_->end());
12137 // Return a tree for constructing a struct.
12140 Struct_construction_expression::do_get_tree(Translate_context* context)
12142 Gogo* gogo = context->gogo();
12144 if (this->vals_ == NULL)
12146 Btype* btype = this->type_->get_backend(gogo);
12147 return expr_to_tree(gogo->backend()->zero_expression(btype));
12150 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
12151 if (type_tree == error_mark_node)
12152 return error_mark_node;
12153 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
12155 bool is_constant = true;
12156 const Struct_field_list* fields = this->type_->struct_type()->fields();
12157 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
12159 Struct_field_list::const_iterator pf = fields->begin();
12160 Expression_list::const_iterator pv = this->vals_->begin();
12161 for (tree field = TYPE_FIELDS(type_tree);
12162 field != NULL_TREE;
12163 field = DECL_CHAIN(field), ++pf)
12165 go_assert(pf != fields->end());
12167 Btype* fbtype = pf->type()->get_backend(gogo);
12170 if (pv == this->vals_->end())
12171 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
12172 else if (*pv == NULL)
12174 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
12179 val = Expression::convert_for_assignment(context, pf->type(),
12181 (*pv)->get_tree(context),
12186 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
12187 return error_mark_node;
12189 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
12190 elt->index = field;
12192 if (!TREE_CONSTANT(val))
12193 is_constant = false;
12195 go_assert(pf == fields->end());
12197 tree ret = build_constructor(type_tree, elts);
12199 TREE_CONSTANT(ret) = 1;
12203 // Export a struct construction.
12206 Struct_construction_expression::do_export(Export* exp) const
12208 exp->write_c_string("convert(");
12209 exp->write_type(this->type_);
12210 for (Expression_list::const_iterator pv = this->vals_->begin();
12211 pv != this->vals_->end();
12214 exp->write_c_string(", ");
12216 (*pv)->export_expression(exp);
12218 exp->write_c_string(")");
12221 // Dump ast representation of a struct construction expression.
12224 Struct_construction_expression::do_dump_expression(
12225 Ast_dump_context* ast_dump_context) const
12227 ast_dump_context->dump_type(this->type_);
12228 ast_dump_context->ostream() << "{";
12229 ast_dump_context->dump_expression_list(this->vals_);
12230 ast_dump_context->ostream() << "}";
12233 // Make a struct composite literal. This used by the thunk code.
12236 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
12239 go_assert(type->struct_type() != NULL);
12240 return new Struct_construction_expression(type, vals, location);
12243 // Construct an array. This class is not used directly; instead we
12244 // use the child classes, Fixed_array_construction_expression and
12245 // Open_array_construction_expression.
12247 class Array_construction_expression : public Expression
12250 Array_construction_expression(Expression_classification classification,
12251 Type* type, Expression_list* vals,
12253 : Expression(classification, location),
12254 type_(type), vals_(vals)
12258 // Return whether this is a constant initializer.
12260 is_constant_array() const;
12262 // Return the number of elements.
12264 element_count() const
12265 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
12269 do_traverse(Traverse* traverse);
12273 { return this->type_; }
12276 do_determine_type(const Type_context*);
12279 do_check_types(Gogo*);
12282 do_export(Export*) const;
12284 // The list of values.
12287 { return this->vals_; }
12289 // Get a constructor tree for the array values.
12291 get_constructor_tree(Translate_context* context, tree type_tree);
12294 do_dump_expression(Ast_dump_context*) const;
12297 // The type of the array to construct.
12299 // The list of values.
12300 Expression_list* vals_;
12306 Array_construction_expression::do_traverse(Traverse* traverse)
12308 if (this->vals_ != NULL
12309 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12310 return TRAVERSE_EXIT;
12311 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12312 return TRAVERSE_EXIT;
12313 return TRAVERSE_CONTINUE;
12316 // Return whether this is a constant initializer.
12319 Array_construction_expression::is_constant_array() const
12321 if (this->vals_ == NULL)
12324 // There are no constant constructors for interfaces.
12325 if (this->type_->array_type()->element_type()->interface_type() != NULL)
12328 for (Expression_list::const_iterator pv = this->vals_->begin();
12329 pv != this->vals_->end();
12333 && !(*pv)->is_constant()
12334 && (!(*pv)->is_composite_literal()
12335 || (*pv)->is_nonconstant_composite_literal()))
12341 // Final type determination.
12344 Array_construction_expression::do_determine_type(const Type_context*)
12346 if (this->vals_ == NULL)
12348 Type_context subcontext(this->type_->array_type()->element_type(), false);
12349 for (Expression_list::const_iterator pv = this->vals_->begin();
12350 pv != this->vals_->end();
12354 (*pv)->determine_type(&subcontext);
12361 Array_construction_expression::do_check_types(Gogo*)
12363 if (this->vals_ == NULL)
12366 Array_type* at = this->type_->array_type();
12368 Type* element_type = at->element_type();
12369 for (Expression_list::const_iterator pv = this->vals_->begin();
12370 pv != this->vals_->end();
12374 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
12376 error_at((*pv)->location(),
12377 "incompatible type for element %d in composite literal",
12379 this->set_is_error();
12383 Expression* length = at->length();
12384 if (length != NULL && !length->is_error_expression())
12389 if (at->length()->integer_constant_value(true, val, &type))
12391 if (this->vals_->size() > mpz_get_ui(val))
12392 this->report_error(_("too many elements in composite literal"));
12398 // Get a constructor tree for the array values.
12401 Array_construction_expression::get_constructor_tree(Translate_context* context,
12404 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
12405 (this->vals_ == NULL
12407 : this->vals_->size()));
12408 Type* element_type = this->type_->array_type()->element_type();
12409 bool is_constant = true;
12410 if (this->vals_ != NULL)
12413 for (Expression_list::const_iterator pv = this->vals_->begin();
12414 pv != this->vals_->end();
12417 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
12418 elt->index = size_int(i);
12421 Gogo* gogo = context->gogo();
12422 Btype* ebtype = element_type->get_backend(gogo);
12423 Bexpression *zv = gogo->backend()->zero_expression(ebtype);
12424 elt->value = expr_to_tree(zv);
12428 tree value_tree = (*pv)->get_tree(context);
12429 elt->value = Expression::convert_for_assignment(context,
12435 if (elt->value == error_mark_node)
12436 return error_mark_node;
12437 if (!TREE_CONSTANT(elt->value))
12438 is_constant = false;
12442 tree ret = build_constructor(type_tree, values);
12444 TREE_CONSTANT(ret) = 1;
12448 // Export an array construction.
12451 Array_construction_expression::do_export(Export* exp) const
12453 exp->write_c_string("convert(");
12454 exp->write_type(this->type_);
12455 if (this->vals_ != NULL)
12457 for (Expression_list::const_iterator pv = this->vals_->begin();
12458 pv != this->vals_->end();
12461 exp->write_c_string(", ");
12463 (*pv)->export_expression(exp);
12466 exp->write_c_string(")");
12469 // Dump ast representation of an array construction expressin.
12472 Array_construction_expression::do_dump_expression(
12473 Ast_dump_context* ast_dump_context) const
12475 Expression* length = this->type_->array_type() != NULL ?
12476 this->type_->array_type()->length() : NULL;
12478 ast_dump_context->ostream() << "[" ;
12479 if (length != NULL)
12481 ast_dump_context->dump_expression(length);
12483 ast_dump_context->ostream() << "]" ;
12484 ast_dump_context->dump_type(this->type_);
12485 ast_dump_context->ostream() << "{" ;
12486 ast_dump_context->dump_expression_list(this->vals_);
12487 ast_dump_context->ostream() << "}" ;
12491 // Construct a fixed array.
12493 class Fixed_array_construction_expression :
12494 public Array_construction_expression
12497 Fixed_array_construction_expression(Type* type, Expression_list* vals,
12499 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
12500 type, vals, location)
12502 go_assert(type->array_type() != NULL
12503 && type->array_type()->length() != NULL);
12510 return new Fixed_array_construction_expression(this->type(),
12511 (this->vals() == NULL
12513 : this->vals()->copy()),
12518 do_get_tree(Translate_context*);
12521 do_dump_expression(Ast_dump_context*);
12524 // Return a tree for constructing a fixed array.
12527 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
12529 Type* type = this->type();
12530 Btype* btype = type->get_backend(context->gogo());
12531 return this->get_constructor_tree(context, type_to_tree(btype));
12534 // Dump ast representation of an array construction expressin.
12537 Fixed_array_construction_expression::do_dump_expression(
12538 Ast_dump_context* ast_dump_context)
12541 ast_dump_context->ostream() << "[";
12542 ast_dump_context->dump_expression (this->type()->array_type()->length());
12543 ast_dump_context->ostream() << "]";
12544 ast_dump_context->dump_type(this->type());
12545 ast_dump_context->ostream() << "{";
12546 ast_dump_context->dump_expression_list(this->vals());
12547 ast_dump_context->ostream() << "}";
12550 // Construct an open array.
12552 class Open_array_construction_expression : public Array_construction_expression
12555 Open_array_construction_expression(Type* type, Expression_list* vals,
12557 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
12558 type, vals, location)
12560 go_assert(type->array_type() != NULL
12561 && type->array_type()->length() == NULL);
12565 // Note that taking the address of an open array literal is invalid.
12570 return new Open_array_construction_expression(this->type(),
12571 (this->vals() == NULL
12573 : this->vals()->copy()),
12578 do_get_tree(Translate_context*);
12581 // Return a tree for constructing an open array.
12584 Open_array_construction_expression::do_get_tree(Translate_context* context)
12586 Array_type* array_type = this->type()->array_type();
12587 if (array_type == NULL)
12589 go_assert(this->type()->is_error());
12590 return error_mark_node;
12593 Type* element_type = array_type->element_type();
12594 Btype* belement_type = element_type->get_backend(context->gogo());
12595 tree element_type_tree = type_to_tree(belement_type);
12596 if (element_type_tree == error_mark_node)
12597 return error_mark_node;
12601 if (this->vals() == NULL || this->vals()->empty())
12603 // We need to create a unique value.
12604 tree max = size_int(0);
12605 tree constructor_type = build_array_type(element_type_tree,
12606 build_index_type(max));
12607 if (constructor_type == error_mark_node)
12608 return error_mark_node;
12609 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
12610 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
12611 elt->index = size_int(0);
12612 Gogo* gogo = context->gogo();
12613 Btype* btype = element_type->get_backend(gogo);
12614 elt->value = expr_to_tree(gogo->backend()->zero_expression(btype));
12615 values = build_constructor(constructor_type, vec);
12616 if (TREE_CONSTANT(elt->value))
12617 TREE_CONSTANT(values) = 1;
12618 length_tree = size_int(0);
12622 tree max = size_int(this->vals()->size() - 1);
12623 tree constructor_type = build_array_type(element_type_tree,
12624 build_index_type(max));
12625 if (constructor_type == error_mark_node)
12626 return error_mark_node;
12627 values = this->get_constructor_tree(context, constructor_type);
12628 length_tree = size_int(this->vals()->size());
12631 if (values == error_mark_node)
12632 return error_mark_node;
12634 bool is_constant_initializer = TREE_CONSTANT(values);
12636 // We have to copy the initial values into heap memory if we are in
12637 // a function or if the values are not constants. We also have to
12638 // copy them if they may contain pointers in a non-constant context,
12639 // as otherwise the garbage collector won't see them.
12640 bool copy_to_heap = (context->function() != NULL
12641 || !is_constant_initializer
12642 || (element_type->has_pointer()
12643 && !context->is_const()));
12645 if (is_constant_initializer)
12647 tree tmp = build_decl(this->location().gcc_location(), VAR_DECL,
12648 create_tmp_var_name("C"), TREE_TYPE(values));
12649 DECL_EXTERNAL(tmp) = 0;
12650 TREE_PUBLIC(tmp) = 0;
12651 TREE_STATIC(tmp) = 1;
12652 DECL_ARTIFICIAL(tmp) = 1;
12655 // If we are not copying the value to the heap, we will only
12656 // initialize the value once, so we can use this directly
12657 // rather than copying it. In that case we can't make it
12658 // read-only, because the program is permitted to change it.
12659 TREE_READONLY(tmp) = 1;
12660 TREE_CONSTANT(tmp) = 1;
12662 DECL_INITIAL(tmp) = values;
12663 rest_of_decl_compilation(tmp, 1, 0);
12671 // the initializer will only run once.
12672 space = build_fold_addr_expr(values);
12677 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
12678 space = context->gogo()->allocate_memory(element_type, memsize,
12680 space = save_expr(space);
12682 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
12683 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
12685 TREE_THIS_NOTRAP(ref) = 1;
12686 set = build2(MODIFY_EXPR, void_type_node, ref, values);
12689 // Build a constructor for the open array.
12691 tree type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
12692 if (type_tree == error_mark_node)
12693 return error_mark_node;
12694 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
12696 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
12698 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
12699 tree field = TYPE_FIELDS(type_tree);
12700 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
12701 elt->index = field;
12702 elt->value = fold_convert(TREE_TYPE(field), space);
12704 elt = VEC_quick_push(constructor_elt, init, NULL);
12705 field = DECL_CHAIN(field);
12706 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
12707 elt->index = field;
12708 elt->value = fold_convert(TREE_TYPE(field), length_tree);
12710 elt = VEC_quick_push(constructor_elt, init, NULL);
12711 field = DECL_CHAIN(field);
12712 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
12713 elt->index = field;
12714 elt->value = fold_convert(TREE_TYPE(field), length_tree);
12716 tree constructor = build_constructor(type_tree, init);
12717 if (constructor == error_mark_node)
12718 return error_mark_node;
12720 TREE_CONSTANT(constructor) = 1;
12722 if (set == NULL_TREE)
12723 return constructor;
12725 return build2(COMPOUND_EXPR, type_tree, set, constructor);
12728 // Make a slice composite literal. This is used by the type
12729 // descriptor code.
12732 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
12735 go_assert(type->is_slice_type());
12736 return new Open_array_construction_expression(type, vals, location);
12739 // Construct a map.
12741 class Map_construction_expression : public Expression
12744 Map_construction_expression(Type* type, Expression_list* vals,
12746 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
12747 type_(type), vals_(vals)
12748 { go_assert(vals == NULL || vals->size() % 2 == 0); }
12752 do_traverse(Traverse* traverse);
12756 { return this->type_; }
12759 do_determine_type(const Type_context*);
12762 do_check_types(Gogo*);
12767 return new Map_construction_expression(this->type_, this->vals_->copy(),
12772 do_get_tree(Translate_context*);
12775 do_export(Export*) const;
12778 do_dump_expression(Ast_dump_context*) const;
12781 // The type of the map to construct.
12783 // The list of values.
12784 Expression_list* vals_;
12790 Map_construction_expression::do_traverse(Traverse* traverse)
12792 if (this->vals_ != NULL
12793 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12794 return TRAVERSE_EXIT;
12795 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12796 return TRAVERSE_EXIT;
12797 return TRAVERSE_CONTINUE;
12800 // Final type determination.
12803 Map_construction_expression::do_determine_type(const Type_context*)
12805 if (this->vals_ == NULL)
12808 Map_type* mt = this->type_->map_type();
12809 Type_context key_context(mt->key_type(), false);
12810 Type_context val_context(mt->val_type(), false);
12811 for (Expression_list::const_iterator pv = this->vals_->begin();
12812 pv != this->vals_->end();
12815 (*pv)->determine_type(&key_context);
12817 (*pv)->determine_type(&val_context);
12824 Map_construction_expression::do_check_types(Gogo*)
12826 if (this->vals_ == NULL)
12829 Map_type* mt = this->type_->map_type();
12831 Type* key_type = mt->key_type();
12832 Type* val_type = mt->val_type();
12833 for (Expression_list::const_iterator pv = this->vals_->begin();
12834 pv != this->vals_->end();
12837 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
12839 error_at((*pv)->location(),
12840 "incompatible type for element %d key in map construction",
12842 this->set_is_error();
12845 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
12847 error_at((*pv)->location(),
12848 ("incompatible type for element %d value "
12849 "in map construction"),
12851 this->set_is_error();
12856 // Return a tree for constructing a map.
12859 Map_construction_expression::do_get_tree(Translate_context* context)
12861 Gogo* gogo = context->gogo();
12862 Location loc = this->location();
12864 Map_type* mt = this->type_->map_type();
12866 // Build a struct to hold the key and value.
12867 tree struct_type = make_node(RECORD_TYPE);
12869 Type* key_type = mt->key_type();
12870 tree id = get_identifier("__key");
12871 tree key_type_tree = type_to_tree(key_type->get_backend(gogo));
12872 if (key_type_tree == error_mark_node)
12873 return error_mark_node;
12874 tree key_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12876 DECL_CONTEXT(key_field) = struct_type;
12877 TYPE_FIELDS(struct_type) = key_field;
12879 Type* val_type = mt->val_type();
12880 id = get_identifier("__val");
12881 tree val_type_tree = type_to_tree(val_type->get_backend(gogo));
12882 if (val_type_tree == error_mark_node)
12883 return error_mark_node;
12884 tree val_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12886 DECL_CONTEXT(val_field) = struct_type;
12887 DECL_CHAIN(key_field) = val_field;
12889 layout_type(struct_type);
12891 bool is_constant = true;
12896 if (this->vals_ == NULL || this->vals_->empty())
12898 valaddr = null_pointer_node;
12899 make_tmp = NULL_TREE;
12903 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
12904 this->vals_->size() / 2);
12906 for (Expression_list::const_iterator pv = this->vals_->begin();
12907 pv != this->vals_->end();
12910 bool one_is_constant = true;
12912 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
12914 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
12915 elt->index = key_field;
12916 tree val_tree = (*pv)->get_tree(context);
12917 elt->value = Expression::convert_for_assignment(context, key_type,
12920 if (elt->value == error_mark_node)
12921 return error_mark_node;
12922 if (!TREE_CONSTANT(elt->value))
12923 one_is_constant = false;
12927 elt = VEC_quick_push(constructor_elt, one, NULL);
12928 elt->index = val_field;
12929 val_tree = (*pv)->get_tree(context);
12930 elt->value = Expression::convert_for_assignment(context, val_type,
12933 if (elt->value == error_mark_node)
12934 return error_mark_node;
12935 if (!TREE_CONSTANT(elt->value))
12936 one_is_constant = false;
12938 elt = VEC_quick_push(constructor_elt, values, NULL);
12939 elt->index = size_int(i);
12940 elt->value = build_constructor(struct_type, one);
12941 if (one_is_constant)
12942 TREE_CONSTANT(elt->value) = 1;
12944 is_constant = false;
12947 tree index_type = build_index_type(size_int(i - 1));
12948 tree array_type = build_array_type(struct_type, index_type);
12949 tree init = build_constructor(array_type, values);
12951 TREE_CONSTANT(init) = 1;
12953 if (current_function_decl != NULL)
12955 tmp = create_tmp_var(array_type, get_name(array_type));
12956 DECL_INITIAL(tmp) = init;
12957 make_tmp = fold_build1_loc(loc.gcc_location(), DECL_EXPR,
12958 void_type_node, tmp);
12959 TREE_ADDRESSABLE(tmp) = 1;
12963 tmp = build_decl(loc.gcc_location(), VAR_DECL,
12964 create_tmp_var_name("M"), array_type);
12965 DECL_EXTERNAL(tmp) = 0;
12966 TREE_PUBLIC(tmp) = 0;
12967 TREE_STATIC(tmp) = 1;
12968 DECL_ARTIFICIAL(tmp) = 1;
12969 if (!TREE_CONSTANT(init))
12970 make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR,
12971 void_type_node, tmp, init);
12974 TREE_READONLY(tmp) = 1;
12975 TREE_CONSTANT(tmp) = 1;
12976 DECL_INITIAL(tmp) = init;
12977 make_tmp = NULL_TREE;
12979 rest_of_decl_compilation(tmp, 1, 0);
12982 valaddr = build_fold_addr_expr(tmp);
12985 tree descriptor = mt->map_descriptor_pointer(gogo, loc);
12987 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
12988 if (type_tree == error_mark_node)
12989 return error_mark_node;
12991 static tree construct_map_fndecl;
12992 tree call = Gogo::call_builtin(&construct_map_fndecl,
12994 "__go_construct_map",
12997 TREE_TYPE(descriptor),
13002 TYPE_SIZE_UNIT(struct_type),
13004 byte_position(val_field),
13006 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
13007 const_ptr_type_node,
13008 fold_convert(const_ptr_type_node, valaddr));
13009 if (call == error_mark_node)
13010 return error_mark_node;
13013 if (make_tmp == NULL)
13016 ret = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, type_tree,
13021 // Export an array construction.
13024 Map_construction_expression::do_export(Export* exp) const
13026 exp->write_c_string("convert(");
13027 exp->write_type(this->type_);
13028 for (Expression_list::const_iterator pv = this->vals_->begin();
13029 pv != this->vals_->end();
13032 exp->write_c_string(", ");
13033 (*pv)->export_expression(exp);
13035 exp->write_c_string(")");
13038 // Dump ast representation for a map construction expression.
13041 Map_construction_expression::do_dump_expression(
13042 Ast_dump_context* ast_dump_context) const
13044 ast_dump_context->ostream() << "{" ;
13045 ast_dump_context->dump_expression_list(this->vals_, true);
13046 ast_dump_context->ostream() << "}";
13049 // A general composite literal. This is lowered to a type specific
13052 class Composite_literal_expression : public Parser_expression
13055 Composite_literal_expression(Type* type, int depth, bool has_keys,
13056 Expression_list* vals, Location location)
13057 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
13058 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
13063 do_traverse(Traverse* traverse);
13066 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
13071 return new Composite_literal_expression(this->type_, this->depth_,
13073 (this->vals_ == NULL
13075 : this->vals_->copy()),
13080 do_dump_expression(Ast_dump_context*) const;
13084 lower_struct(Gogo*, Type*);
13087 lower_array(Type*);
13090 make_array(Type*, Expression_list*);
13093 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
13095 // The type of the composite literal.
13097 // The depth within a list of composite literals within a composite
13098 // literal, when the type is omitted.
13100 // The values to put in the composite literal.
13101 Expression_list* vals_;
13102 // If this is true, then VALS_ is a list of pairs: a key and a
13103 // value. In an array initializer, a missing key will be NULL.
13110 Composite_literal_expression::do_traverse(Traverse* traverse)
13112 if (this->vals_ != NULL
13113 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
13114 return TRAVERSE_EXIT;
13115 return Type::traverse(this->type_, traverse);
13118 // Lower a generic composite literal into a specific version based on
13122 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
13123 Statement_inserter* inserter, int)
13125 Type* type = this->type_;
13127 for (int depth = this->depth_; depth > 0; --depth)
13129 if (type->array_type() != NULL)
13130 type = type->array_type()->element_type();
13131 else if (type->map_type() != NULL)
13132 type = type->map_type()->val_type();
13135 if (!type->is_error())
13136 error_at(this->location(),
13137 ("may only omit types within composite literals "
13138 "of slice, array, or map type"));
13139 return Expression::make_error(this->location());
13143 Type *pt = type->points_to();
13144 bool is_pointer = false;
13152 if (type->is_error())
13153 return Expression::make_error(this->location());
13154 else if (type->struct_type() != NULL)
13155 ret = this->lower_struct(gogo, type);
13156 else if (type->array_type() != NULL)
13157 ret = this->lower_array(type);
13158 else if (type->map_type() != NULL)
13159 ret = this->lower_map(gogo, function, inserter, type);
13162 error_at(this->location(),
13163 ("expected struct, slice, array, or map type "
13164 "for composite literal"));
13165 return Expression::make_error(this->location());
13169 ret = Expression::make_heap_composite(ret, this->location());
13174 // Lower a struct composite literal.
13177 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
13179 Location location = this->location();
13180 Struct_type* st = type->struct_type();
13181 if (this->vals_ == NULL || !this->has_keys_)
13183 if (this->vals_ != NULL
13184 && !this->vals_->empty()
13185 && type->named_type() != NULL
13186 && type->named_type()->named_object()->package() != NULL)
13188 for (Struct_field_list::const_iterator pf = st->fields()->begin();
13189 pf != st->fields()->end();
13192 if (Gogo::is_hidden_name(pf->field_name()))
13193 error_at(this->location(),
13194 "assignment of unexported field %qs in %qs literal",
13195 Gogo::message_name(pf->field_name()).c_str(),
13196 type->named_type()->message_name().c_str());
13200 return new Struct_construction_expression(type, this->vals_, location);
13203 size_t field_count = st->field_count();
13204 std::vector<Expression*> vals(field_count);
13205 Expression_list::const_iterator p = this->vals_->begin();
13206 while (p != this->vals_->end())
13208 Expression* name_expr = *p;
13211 go_assert(p != this->vals_->end());
13212 Expression* val = *p;
13216 if (name_expr == NULL)
13218 error_at(val->location(), "mixture of field and value initializers");
13219 return Expression::make_error(location);
13222 bool bad_key = false;
13224 const Named_object* no = NULL;
13225 switch (name_expr->classification())
13227 case EXPRESSION_UNKNOWN_REFERENCE:
13228 name = name_expr->unknown_expression()->name();
13231 case EXPRESSION_CONST_REFERENCE:
13232 no = static_cast<Const_expression*>(name_expr)->named_object();
13235 case EXPRESSION_TYPE:
13237 Type* t = name_expr->type();
13238 Named_type* nt = t->named_type();
13242 no = nt->named_object();
13246 case EXPRESSION_VAR_REFERENCE:
13247 no = name_expr->var_expression()->named_object();
13250 case EXPRESSION_FUNC_REFERENCE:
13251 no = name_expr->func_expression()->named_object();
13254 case EXPRESSION_UNARY:
13255 // If there is a local variable around with the same name as
13256 // the field, and this occurs in the closure, then the
13257 // parser may turn the field reference into an indirection
13258 // through the closure. FIXME: This is a mess.
13261 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
13262 if (ue->op() == OPERATOR_MULT)
13264 Field_reference_expression* fre =
13265 ue->operand()->field_reference_expression();
13269 fre->expr()->type()->deref()->struct_type();
13272 const Struct_field* sf = st->field(fre->field_index());
13273 name = sf->field_name();
13275 // See below. FIXME.
13276 if (!Gogo::is_hidden_name(name)
13280 if (gogo->lookup_global(name.c_str()) != NULL)
13281 name = gogo->pack_hidden_name(name, false);
13285 snprintf(buf, sizeof buf, "%u", fre->field_index());
13286 size_t buflen = strlen(buf);
13287 if (name.compare(name.length() - buflen, buflen, buf)
13290 name = name.substr(0, name.length() - buflen);
13305 error_at(name_expr->location(), "expected struct field name");
13306 return Expression::make_error(location);
13313 // A predefined name won't be packed. If it starts with a
13314 // lower case letter we need to check for that case, because
13315 // the field name will be packed. FIXME.
13316 if (!Gogo::is_hidden_name(name)
13320 Named_object* gno = gogo->lookup_global(name.c_str());
13322 name = gogo->pack_hidden_name(name, false);
13326 unsigned int index;
13327 const Struct_field* sf = st->find_local_field(name, &index);
13330 error_at(name_expr->location(), "unknown field %qs in %qs",
13331 Gogo::message_name(name).c_str(),
13332 (type->named_type() != NULL
13333 ? type->named_type()->message_name().c_str()
13334 : "unnamed struct"));
13335 return Expression::make_error(location);
13337 if (vals[index] != NULL)
13339 error_at(name_expr->location(),
13340 "duplicate value for field %qs in %qs",
13341 Gogo::message_name(name).c_str(),
13342 (type->named_type() != NULL
13343 ? type->named_type()->message_name().c_str()
13344 : "unnamed struct"));
13345 return Expression::make_error(location);
13348 if (type->named_type() != NULL
13349 && type->named_type()->named_object()->package() != NULL
13350 && Gogo::is_hidden_name(sf->field_name()))
13351 error_at(name_expr->location(),
13352 "assignment of unexported field %qs in %qs literal",
13353 Gogo::message_name(sf->field_name()).c_str(),
13354 type->named_type()->message_name().c_str());
13359 Expression_list* list = new Expression_list;
13360 list->reserve(field_count);
13361 for (size_t i = 0; i < field_count; ++i)
13362 list->push_back(vals[i]);
13364 return new Struct_construction_expression(type, list, location);
13367 // Lower an array composite literal.
13370 Composite_literal_expression::lower_array(Type* type)
13372 Location location = this->location();
13373 if (this->vals_ == NULL || !this->has_keys_)
13374 return this->make_array(type, this->vals_);
13376 std::vector<Expression*> vals;
13377 vals.reserve(this->vals_->size());
13378 unsigned long index = 0;
13379 Expression_list::const_iterator p = this->vals_->begin();
13380 while (p != this->vals_->end())
13382 Expression* index_expr = *p;
13385 go_assert(p != this->vals_->end());
13386 Expression* val = *p;
13390 if (index_expr != NULL)
13396 if (!index_expr->integer_constant_value(true, ival, &dummy))
13399 error_at(index_expr->location(),
13400 "index expression is not integer constant");
13401 return Expression::make_error(location);
13404 if (mpz_sgn(ival) < 0)
13407 error_at(index_expr->location(), "index expression is negative");
13408 return Expression::make_error(location);
13411 index = mpz_get_ui(ival);
13412 if (mpz_cmp_ui(ival, index) != 0)
13415 error_at(index_expr->location(), "index value overflow");
13416 return Expression::make_error(location);
13419 Named_type* ntype = Type::lookup_integer_type("int");
13420 Integer_type* inttype = ntype->integer_type();
13422 mpz_init_set_ui(max, 1);
13423 mpz_mul_2exp(max, max, inttype->bits() - 1);
13424 bool ok = mpz_cmp(ival, max) < 0;
13429 error_at(index_expr->location(), "index value overflow");
13430 return Expression::make_error(location);
13435 // FIXME: Our representation isn't very good; this avoids
13437 if (index > 0x1000000)
13439 error_at(index_expr->location(), "index too large for compiler");
13440 return Expression::make_error(location);
13444 if (index == vals.size())
13445 vals.push_back(val);
13448 if (index > vals.size())
13450 vals.reserve(index + 32);
13451 vals.resize(index + 1, static_cast<Expression*>(NULL));
13453 if (vals[index] != NULL)
13455 error_at((index_expr != NULL
13456 ? index_expr->location()
13457 : val->location()),
13458 "duplicate value for index %lu",
13460 return Expression::make_error(location);
13468 size_t size = vals.size();
13469 Expression_list* list = new Expression_list;
13470 list->reserve(size);
13471 for (size_t i = 0; i < size; ++i)
13472 list->push_back(vals[i]);
13474 return this->make_array(type, list);
13477 // Actually build the array composite literal. This handles
13481 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
13483 Location location = this->location();
13484 Array_type* at = type->array_type();
13485 if (at->length() != NULL && at->length()->is_nil_expression())
13487 size_t size = vals == NULL ? 0 : vals->size();
13489 mpz_init_set_ui(vlen, size);
13490 Expression* elen = Expression::make_integer(&vlen, NULL, location);
13492 at = Type::make_array_type(at->element_type(), elen);
13495 if (at->length() != NULL)
13496 return new Fixed_array_construction_expression(type, vals, location);
13498 return new Open_array_construction_expression(type, vals, location);
13501 // Lower a map composite literal.
13504 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
13505 Statement_inserter* inserter,
13508 Location location = this->location();
13509 if (this->vals_ != NULL)
13511 if (!this->has_keys_)
13513 error_at(location, "map composite literal must have keys");
13514 return Expression::make_error(location);
13517 for (Expression_list::iterator p = this->vals_->begin();
13518 p != this->vals_->end();
13524 error_at((*p)->location(),
13525 "map composite literal must have keys for every value");
13526 return Expression::make_error(location);
13528 // Make sure we have lowered the key; it may not have been
13529 // lowered in order to handle keys for struct composite
13530 // literals. Lower it now to get the right error message.
13531 if ((*p)->unknown_expression() != NULL)
13533 (*p)->unknown_expression()->clear_is_composite_literal_key();
13534 gogo->lower_expression(function, inserter, &*p);
13535 go_assert((*p)->is_error_expression());
13536 return Expression::make_error(location);
13541 return new Map_construction_expression(type, this->vals_, location);
13544 // Dump ast representation for a composite literal expression.
13547 Composite_literal_expression::do_dump_expression(
13548 Ast_dump_context* ast_dump_context) const
13550 ast_dump_context->ostream() << "composite(";
13551 ast_dump_context->dump_type(this->type_);
13552 ast_dump_context->ostream() << ", {";
13553 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
13554 ast_dump_context->ostream() << "})";
13557 // Make a composite literal expression.
13560 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
13561 Expression_list* vals,
13564 return new Composite_literal_expression(type, depth, has_keys, vals,
13568 // Return whether this expression is a composite literal.
13571 Expression::is_composite_literal() const
13573 switch (this->classification_)
13575 case EXPRESSION_COMPOSITE_LITERAL:
13576 case EXPRESSION_STRUCT_CONSTRUCTION:
13577 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
13578 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
13579 case EXPRESSION_MAP_CONSTRUCTION:
13586 // Return whether this expression is a composite literal which is not
13590 Expression::is_nonconstant_composite_literal() const
13592 switch (this->classification_)
13594 case EXPRESSION_STRUCT_CONSTRUCTION:
13596 const Struct_construction_expression *psce =
13597 static_cast<const Struct_construction_expression*>(this);
13598 return !psce->is_constant_struct();
13600 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
13602 const Fixed_array_construction_expression *pace =
13603 static_cast<const Fixed_array_construction_expression*>(this);
13604 return !pace->is_constant_array();
13606 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
13608 const Open_array_construction_expression *pace =
13609 static_cast<const Open_array_construction_expression*>(this);
13610 return !pace->is_constant_array();
13612 case EXPRESSION_MAP_CONSTRUCTION:
13619 // Return true if this is a reference to a local variable.
13622 Expression::is_local_variable() const
13624 const Var_expression* ve = this->var_expression();
13627 const Named_object* no = ve->named_object();
13628 return (no->is_result_variable()
13629 || (no->is_variable() && !no->var_value()->is_global()));
13632 // Class Type_guard_expression.
13637 Type_guard_expression::do_traverse(Traverse* traverse)
13639 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
13640 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
13641 return TRAVERSE_EXIT;
13642 return TRAVERSE_CONTINUE;
13645 // Check types of a type guard expression. The expression must have
13646 // an interface type, but the actual type conversion is checked at run
13650 Type_guard_expression::do_check_types(Gogo*)
13652 // 6g permits using a type guard with unsafe.pointer; we are
13654 Type* expr_type = this->expr_->type();
13655 if (expr_type->is_unsafe_pointer_type())
13657 if (this->type_->points_to() == NULL
13658 && (this->type_->integer_type() == NULL
13659 || (this->type_->forwarded()
13660 != Type::lookup_integer_type("uintptr"))))
13661 this->report_error(_("invalid unsafe.Pointer conversion"));
13663 else if (this->type_->is_unsafe_pointer_type())
13665 if (expr_type->points_to() == NULL
13666 && (expr_type->integer_type() == NULL
13667 || (expr_type->forwarded()
13668 != Type::lookup_integer_type("uintptr"))))
13669 this->report_error(_("invalid unsafe.Pointer conversion"));
13671 else if (expr_type->interface_type() == NULL)
13673 if (!expr_type->is_error() && !this->type_->is_error())
13674 this->report_error(_("type assertion only valid for interface types"));
13675 this->set_is_error();
13677 else if (this->type_->interface_type() == NULL)
13679 std::string reason;
13680 if (!expr_type->interface_type()->implements_interface(this->type_,
13683 if (!this->type_->is_error())
13685 if (reason.empty())
13686 this->report_error(_("impossible type assertion: "
13687 "type does not implement interface"));
13689 error_at(this->location(),
13690 ("impossible type assertion: "
13691 "type does not implement interface (%s)"),
13694 this->set_is_error();
13699 // Return a tree for a type guard expression.
13702 Type_guard_expression::do_get_tree(Translate_context* context)
13704 Gogo* gogo = context->gogo();
13705 tree expr_tree = this->expr_->get_tree(context);
13706 if (expr_tree == error_mark_node)
13707 return error_mark_node;
13708 Type* expr_type = this->expr_->type();
13709 if ((this->type_->is_unsafe_pointer_type()
13710 && (expr_type->points_to() != NULL
13711 || expr_type->integer_type() != NULL))
13712 || (expr_type->is_unsafe_pointer_type()
13713 && this->type_->points_to() != NULL))
13714 return convert_to_pointer(type_to_tree(this->type_->get_backend(gogo)),
13716 else if (expr_type->is_unsafe_pointer_type()
13717 && this->type_->integer_type() != NULL)
13718 return convert_to_integer(type_to_tree(this->type_->get_backend(gogo)),
13720 else if (this->type_->interface_type() != NULL)
13721 return Expression::convert_interface_to_interface(context, this->type_,
13722 this->expr_->type(),
13726 return Expression::convert_for_assignment(context, this->type_,
13727 this->expr_->type(), expr_tree,
13731 // Dump ast representation for a type guard expression.
13734 Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
13737 this->expr_->dump_expression(ast_dump_context);
13738 ast_dump_context->ostream() << ".";
13739 ast_dump_context->dump_type(this->type_);
13742 // Make a type guard expression.
13745 Expression::make_type_guard(Expression* expr, Type* type,
13748 return new Type_guard_expression(expr, type, location);
13751 // Class Heap_composite_expression.
13753 // When you take the address of a composite literal, it is allocated
13754 // on the heap. This class implements that.
13756 class Heap_composite_expression : public Expression
13759 Heap_composite_expression(Expression* expr, Location location)
13760 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
13766 do_traverse(Traverse* traverse)
13767 { return Expression::traverse(&this->expr_, traverse); }
13771 { return Type::make_pointer_type(this->expr_->type()); }
13774 do_determine_type(const Type_context*)
13775 { this->expr_->determine_type_no_context(); }
13780 return Expression::make_heap_composite(this->expr_->copy(),
13785 do_get_tree(Translate_context*);
13787 // We only export global objects, and the parser does not generate
13788 // this in global scope.
13790 do_export(Export*) const
13791 { go_unreachable(); }
13794 do_dump_expression(Ast_dump_context*) const;
13797 // The composite literal which is being put on the heap.
13801 // Return a tree which allocates a composite literal on the heap.
13804 Heap_composite_expression::do_get_tree(Translate_context* context)
13806 tree expr_tree = this->expr_->get_tree(context);
13807 if (expr_tree == error_mark_node)
13808 return error_mark_node;
13809 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
13810 go_assert(TREE_CODE(expr_size) == INTEGER_CST);
13811 tree space = context->gogo()->allocate_memory(this->expr_->type(),
13812 expr_size, this->location());
13813 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
13814 space = save_expr(space);
13815 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
13817 TREE_THIS_NOTRAP(ref) = 1;
13818 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
13819 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
13821 SET_EXPR_LOCATION(ret, this->location().gcc_location());
13825 // Dump ast representation for a heap composite expression.
13828 Heap_composite_expression::do_dump_expression(
13829 Ast_dump_context* ast_dump_context) const
13831 ast_dump_context->ostream() << "&(";
13832 ast_dump_context->dump_expression(this->expr_);
13833 ast_dump_context->ostream() << ")";
13836 // Allocate a composite literal on the heap.
13839 Expression::make_heap_composite(Expression* expr, Location location)
13841 return new Heap_composite_expression(expr, location);
13844 // Class Receive_expression.
13846 // Return the type of a receive expression.
13849 Receive_expression::do_type()
13851 Channel_type* channel_type = this->channel_->type()->channel_type();
13852 if (channel_type == NULL)
13853 return Type::make_error_type();
13854 return channel_type->element_type();
13857 // Check types for a receive expression.
13860 Receive_expression::do_check_types(Gogo*)
13862 Type* type = this->channel_->type();
13863 if (type->is_error())
13865 this->set_is_error();
13868 if (type->channel_type() == NULL)
13870 this->report_error(_("expected channel"));
13873 if (!type->channel_type()->may_receive())
13875 this->report_error(_("invalid receive on send-only channel"));
13880 // Get a tree for a receive expression.
13883 Receive_expression::do_get_tree(Translate_context* context)
13885 Location loc = this->location();
13887 Channel_type* channel_type = this->channel_->type()->channel_type();
13888 if (channel_type == NULL)
13890 go_assert(this->channel_->type()->is_error());
13891 return error_mark_node;
13894 Expression* td = Expression::make_type_descriptor(channel_type, loc);
13895 tree td_tree = td->get_tree(context);
13897 Type* element_type = channel_type->element_type();
13898 Btype* element_type_btype = element_type->get_backend(context->gogo());
13899 tree element_type_tree = type_to_tree(element_type_btype);
13901 tree channel = this->channel_->get_tree(context);
13902 if (element_type_tree == error_mark_node || channel == error_mark_node)
13903 return error_mark_node;
13905 return Gogo::receive_from_channel(element_type_tree, td_tree, channel, loc);
13908 // Dump ast representation for a receive expression.
13911 Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
13913 ast_dump_context->ostream() << " <- " ;
13914 ast_dump_context->dump_expression(channel_);
13917 // Make a receive expression.
13919 Receive_expression*
13920 Expression::make_receive(Expression* channel, Location location)
13922 return new Receive_expression(channel, location);
13925 // An expression which evaluates to a pointer to the type descriptor
13928 class Type_descriptor_expression : public Expression
13931 Type_descriptor_expression(Type* type, Location location)
13932 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
13939 { return Type::make_type_descriptor_ptr_type(); }
13942 do_determine_type(const Type_context*)
13950 do_get_tree(Translate_context* context)
13952 return this->type_->type_descriptor_pointer(context->gogo(),
13957 do_dump_expression(Ast_dump_context*) const;
13960 // The type for which this is the descriptor.
13964 // Dump ast representation for a type descriptor expression.
13967 Type_descriptor_expression::do_dump_expression(
13968 Ast_dump_context* ast_dump_context) const
13970 ast_dump_context->dump_type(this->type_);
13973 // Make a type descriptor expression.
13976 Expression::make_type_descriptor(Type* type, Location location)
13978 return new Type_descriptor_expression(type, location);
13981 // An expression which evaluates to some characteristic of a type.
13982 // This is only used to initialize fields of a type descriptor. Using
13983 // a new expression class is slightly inefficient but gives us a good
13984 // separation between the frontend and the middle-end with regard to
13985 // how types are laid out.
13987 class Type_info_expression : public Expression
13990 Type_info_expression(Type* type, Type_info type_info)
13991 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()),
13992 type_(type), type_info_(type_info)
14000 do_determine_type(const Type_context*)
14008 do_get_tree(Translate_context* context);
14011 do_dump_expression(Ast_dump_context*) const;
14014 // The type for which we are getting information.
14016 // What information we want.
14017 Type_info type_info_;
14020 // The type is chosen to match what the type descriptor struct
14024 Type_info_expression::do_type()
14026 switch (this->type_info_)
14028 case TYPE_INFO_SIZE:
14029 return Type::lookup_integer_type("uintptr");
14030 case TYPE_INFO_ALIGNMENT:
14031 case TYPE_INFO_FIELD_ALIGNMENT:
14032 return Type::lookup_integer_type("uint8");
14038 // Return type information in GENERIC.
14041 Type_info_expression::do_get_tree(Translate_context* context)
14043 Btype* btype = this->type_->get_backend(context->gogo());
14044 Gogo* gogo = context->gogo();
14046 switch (this->type_info_)
14048 case TYPE_INFO_SIZE:
14049 val = gogo->backend()->type_size(btype);
14051 case TYPE_INFO_ALIGNMENT:
14052 val = gogo->backend()->type_alignment(btype);
14054 case TYPE_INFO_FIELD_ALIGNMENT:
14055 val = gogo->backend()->type_field_alignment(btype);
14060 tree val_type_tree = type_to_tree(this->type()->get_backend(gogo));
14061 go_assert(val_type_tree != error_mark_node);
14062 return build_int_cstu(val_type_tree, val);
14065 // Dump ast representation for a type info expression.
14068 Type_info_expression::do_dump_expression(
14069 Ast_dump_context* ast_dump_context) const
14071 ast_dump_context->ostream() << "typeinfo(";
14072 ast_dump_context->dump_type(this->type_);
14073 ast_dump_context->ostream() << ",";
14074 ast_dump_context->ostream() <<
14075 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
14076 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
14077 : this->type_info_ == TYPE_INFO_SIZE ? "size "
14079 ast_dump_context->ostream() << ")";
14082 // Make a type info expression.
14085 Expression::make_type_info(Type* type, Type_info type_info)
14087 return new Type_info_expression(type, type_info);
14090 // An expression which evaluates to the offset of a field within a
14091 // struct. This, like Type_info_expression, q.v., is only used to
14092 // initialize fields of a type descriptor.
14094 class Struct_field_offset_expression : public Expression
14097 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
14098 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET,
14099 Linemap::predeclared_location()),
14100 type_(type), field_(field)
14106 { return Type::lookup_integer_type("uintptr"); }
14109 do_determine_type(const Type_context*)
14117 do_get_tree(Translate_context* context);
14120 do_dump_expression(Ast_dump_context*) const;
14123 // The type of the struct.
14124 Struct_type* type_;
14126 const Struct_field* field_;
14129 // Return a struct field offset in GENERIC.
14132 Struct_field_offset_expression::do_get_tree(Translate_context* context)
14134 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
14135 if (type_tree == error_mark_node)
14136 return error_mark_node;
14138 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
14139 go_assert(val_type_tree != error_mark_node);
14141 const Struct_field_list* fields = this->type_->fields();
14142 tree struct_field_tree = TYPE_FIELDS(type_tree);
14143 Struct_field_list::const_iterator p;
14144 for (p = fields->begin();
14145 p != fields->end();
14146 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
14148 go_assert(struct_field_tree != NULL_TREE);
14149 if (&*p == this->field_)
14152 go_assert(&*p == this->field_);
14154 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
14155 byte_position(struct_field_tree));
14158 // Dump ast representation for a struct field offset expression.
14161 Struct_field_offset_expression::do_dump_expression(
14162 Ast_dump_context* ast_dump_context) const
14164 ast_dump_context->ostream() << "unsafe.Offsetof(";
14165 ast_dump_context->dump_type(this->type_);
14166 ast_dump_context->ostream() << '.';
14167 ast_dump_context->ostream() <<
14168 Gogo::message_name(this->field_->field_name());
14169 ast_dump_context->ostream() << ")";
14172 // Make an expression for a struct field offset.
14175 Expression::make_struct_field_offset(Struct_type* type,
14176 const Struct_field* field)
14178 return new Struct_field_offset_expression(type, field);
14181 // An expression which evaluates to a pointer to the map descriptor of
14184 class Map_descriptor_expression : public Expression
14187 Map_descriptor_expression(Map_type* type, Location location)
14188 : Expression(EXPRESSION_MAP_DESCRIPTOR, location),
14195 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
14198 do_determine_type(const Type_context*)
14206 do_get_tree(Translate_context* context)
14208 return this->type_->map_descriptor_pointer(context->gogo(),
14213 do_dump_expression(Ast_dump_context*) const;
14216 // The type for which this is the descriptor.
14220 // Dump ast representation for a map descriptor expression.
14223 Map_descriptor_expression::do_dump_expression(
14224 Ast_dump_context* ast_dump_context) const
14226 ast_dump_context->ostream() << "map_descriptor(";
14227 ast_dump_context->dump_type(this->type_);
14228 ast_dump_context->ostream() << ")";
14231 // Make a map descriptor expression.
14234 Expression::make_map_descriptor(Map_type* type, Location location)
14236 return new Map_descriptor_expression(type, location);
14239 // An expression which evaluates to the address of an unnamed label.
14241 class Label_addr_expression : public Expression
14244 Label_addr_expression(Label* label, Location location)
14245 : Expression(EXPRESSION_LABEL_ADDR, location),
14252 { return Type::make_pointer_type(Type::make_void_type()); }
14255 do_determine_type(const Type_context*)
14260 { return new Label_addr_expression(this->label_, this->location()); }
14263 do_get_tree(Translate_context* context)
14265 return expr_to_tree(this->label_->get_addr(context, this->location()));
14269 do_dump_expression(Ast_dump_context* ast_dump_context) const
14270 { ast_dump_context->ostream() << this->label_->name(); }
14273 // The label whose address we are taking.
14277 // Make an expression for the address of an unnamed label.
14280 Expression::make_label_addr(Label* label, Location location)
14282 return new Label_addr_expression(label, location);
14285 // Import an expression. This comes at the end in order to see the
14286 // various class definitions.
14289 Expression::import_expression(Import* imp)
14291 int c = imp->peek_char();
14292 if (imp->match_c_string("- ")
14293 || imp->match_c_string("! ")
14294 || imp->match_c_string("^ "))
14295 return Unary_expression::do_import(imp);
14297 return Binary_expression::do_import(imp);
14298 else if (imp->match_c_string("true")
14299 || imp->match_c_string("false"))
14300 return Boolean_expression::do_import(imp);
14302 return String_expression::do_import(imp);
14303 else if (c == '-' || (c >= '0' && c <= '9'))
14305 // This handles integers, floats and complex constants.
14306 return Integer_expression::do_import(imp);
14308 else if (imp->match_c_string("nil"))
14309 return Nil_expression::do_import(imp);
14310 else if (imp->match_c_string("convert"))
14311 return Type_conversion_expression::do_import(imp);
14314 error_at(imp->location(), "import error: expected expression");
14315 return Expression::make_error(imp->location());
14319 // Class Expression_list.
14321 // Traverse the list.
14324 Expression_list::traverse(Traverse* traverse)
14326 for (Expression_list::iterator p = this->begin();
14332 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
14333 return TRAVERSE_EXIT;
14336 return TRAVERSE_CONTINUE;
14342 Expression_list::copy()
14344 Expression_list* ret = new Expression_list();
14345 for (Expression_list::iterator p = this->begin();
14350 ret->push_back(NULL);
14352 ret->push_back((*p)->copy());
14357 // Return whether an expression list has an error expression.
14360 Expression_list::contains_error() const
14362 for (Expression_list::const_iterator p = this->begin();
14365 if (*p != NULL && (*p)->is_error_expression())