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"
36 #include "expressions.h"
40 Expression::Expression(Expression_classification classification
,
41 source_location location
)
42 : classification_(classification
), location_(location
)
46 Expression::~Expression()
50 // If this expression has a constant integer value, return it.
53 Expression::integer_constant_value(bool iota_is_constant
, mpz_t val
,
57 return this->do_integer_constant_value(iota_is_constant
, val
, ptype
);
60 // If this expression has a constant floating point value, return it.
63 Expression::float_constant_value(mpfr_t val
, Type
** ptype
) const
66 if (this->do_float_constant_value(val
, ptype
))
72 if (!this->do_integer_constant_value(false, ival
, &t
))
76 mpfr_set_z(val
, ival
, GMP_RNDN
);
83 // If this expression has a constant complex value, return it.
86 Expression::complex_constant_value(mpfr_t real
, mpfr_t imag
,
90 if (this->do_complex_constant_value(real
, imag
, ptype
))
93 if (this->float_constant_value(real
, &t
))
95 mpfr_set_ui(imag
, 0, GMP_RNDN
);
101 // Traverse the expressions.
104 Expression::traverse(Expression
** pexpr
, Traverse
* traverse
)
106 Expression
* expr
= *pexpr
;
107 if ((traverse
->traverse_mask() & Traverse::traverse_expressions
) != 0)
109 int t
= traverse
->expression(pexpr
);
110 if (t
== TRAVERSE_EXIT
)
111 return TRAVERSE_EXIT
;
112 else if (t
== TRAVERSE_SKIP_COMPONENTS
)
113 return TRAVERSE_CONTINUE
;
115 return expr
->do_traverse(traverse
);
118 // Traverse subexpressions of this expression.
121 Expression::traverse_subexpressions(Traverse
* traverse
)
123 return this->do_traverse(traverse
);
126 // Default implementation for do_traverse for child classes.
129 Expression::do_traverse(Traverse
*)
131 return TRAVERSE_CONTINUE
;
134 // This virtual function is called by the parser if the value of this
135 // expression is being discarded. By default, we warn. Expressions
136 // with side effects override.
139 Expression::do_discarding_value()
141 this->warn_about_unused_value();
144 // This virtual function is called to export expressions. This will
145 // only be used by expressions which may be constant.
148 Expression::do_export(Export
*) const
153 // Warn that the value of the expression is not used.
156 Expression::warn_about_unused_value()
158 warning_at(this->location(), OPT_Wunused_value
, "value computed is not used");
161 // Note that this expression is an error. This is called by children
162 // when they discover an error.
165 Expression::set_is_error()
167 this->classification_
= EXPRESSION_ERROR
;
170 // For children to call to report an error conveniently.
173 Expression::report_error(const char* msg
)
175 error_at(this->location_
, "%s", msg
);
176 this->set_is_error();
179 // Set types of variables and constants. This is implemented by the
183 Expression::determine_type(const Type_context
* context
)
185 this->do_determine_type(context
);
188 // Set types when there is no context.
191 Expression::determine_type_no_context()
193 Type_context context
;
194 this->do_determine_type(&context
);
197 // Return a tree handling any conversions which must be done during
201 Expression::convert_for_assignment(Translate_context
* context
, Type
* lhs_type
,
202 Type
* rhs_type
, tree rhs_tree
,
203 source_location location
)
205 if (lhs_type
== rhs_type
)
208 if (lhs_type
->is_error_type() || rhs_type
->is_error_type())
209 return error_mark_node
;
211 if (lhs_type
->is_undefined() || rhs_type
->is_undefined())
213 // Make sure we report the error.
216 return error_mark_node
;
219 if (rhs_tree
== error_mark_node
|| TREE_TYPE(rhs_tree
) == error_mark_node
)
220 return error_mark_node
;
222 Gogo
* gogo
= context
->gogo();
224 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
225 if (lhs_type_tree
== error_mark_node
)
226 return error_mark_node
;
228 if (lhs_type
->interface_type() != NULL
)
230 if (rhs_type
->interface_type() == NULL
)
231 return Expression::convert_type_to_interface(context
, lhs_type
,
235 return Expression::convert_interface_to_interface(context
, lhs_type
,
239 else if (rhs_type
->interface_type() != NULL
)
240 return Expression::convert_interface_to_type(context
, lhs_type
, rhs_type
,
242 else if (lhs_type
->is_open_array_type()
243 && rhs_type
->is_nil_type())
245 // Assigning nil to an open array.
246 gcc_assert(TREE_CODE(lhs_type_tree
) == RECORD_TYPE
);
248 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
250 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
251 tree field
= TYPE_FIELDS(lhs_type_tree
);
252 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
255 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
257 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
258 field
= DECL_CHAIN(field
);
259 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
262 elt
->value
= fold_convert(TREE_TYPE(field
), integer_zero_node
);
264 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
265 field
= DECL_CHAIN(field
);
266 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
269 elt
->value
= fold_convert(TREE_TYPE(field
), integer_zero_node
);
271 tree val
= build_constructor(lhs_type_tree
, init
);
272 TREE_CONSTANT(val
) = 1;
276 else if (rhs_type
->is_nil_type())
278 // The left hand side should be a pointer type at the tree
280 gcc_assert(POINTER_TYPE_P(lhs_type_tree
));
281 return fold_convert(lhs_type_tree
, null_pointer_node
);
283 else if (lhs_type_tree
== TREE_TYPE(rhs_tree
))
285 // No conversion is needed.
288 else if (POINTER_TYPE_P(lhs_type_tree
)
289 || INTEGRAL_TYPE_P(lhs_type_tree
)
290 || SCALAR_FLOAT_TYPE_P(lhs_type_tree
)
291 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree
))
292 return fold_convert_loc(location
, lhs_type_tree
, rhs_tree
);
293 else if (TREE_CODE(lhs_type_tree
) == RECORD_TYPE
294 && TREE_CODE(TREE_TYPE(rhs_tree
)) == RECORD_TYPE
)
296 // This conversion must be permitted by Go, or we wouldn't have
298 gcc_assert(int_size_in_bytes(lhs_type_tree
)
299 == int_size_in_bytes(TREE_TYPE(rhs_tree
)));
300 return fold_build1_loc(location
, VIEW_CONVERT_EXPR
, lhs_type_tree
,
305 gcc_assert(useless_type_conversion_p(lhs_type_tree
, TREE_TYPE(rhs_tree
)));
310 // Return a tree for a conversion from a non-interface type to an
314 Expression::convert_type_to_interface(Translate_context
* context
,
315 Type
* lhs_type
, Type
* rhs_type
,
316 tree rhs_tree
, source_location location
)
318 Gogo
* gogo
= context
->gogo();
319 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
320 bool lhs_is_empty
= lhs_interface_type
->is_empty();
322 // Since RHS_TYPE is a static type, we can create the interface
323 // method table at compile time.
325 // When setting an interface to nil, we just set both fields to
327 if (rhs_type
->is_nil_type())
328 return lhs_type
->get_init_tree(gogo
, false);
330 // This should have been checked already.
331 gcc_assert(lhs_interface_type
->implements_interface(rhs_type
, NULL
));
333 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
334 if (lhs_type_tree
== error_mark_node
)
335 return error_mark_node
;
337 // An interface is a tuple. If LHS_TYPE is an empty interface type,
338 // then the first field is the type descriptor for RHS_TYPE.
339 // Otherwise it is the interface method table for RHS_TYPE.
340 tree first_field_value
;
342 first_field_value
= rhs_type
->type_descriptor_pointer(gogo
);
345 // Build the interface method table for this interface and this
346 // object type: a list of function pointers for each interface
348 Named_type
* rhs_named_type
= rhs_type
->named_type();
349 bool is_pointer
= false;
350 if (rhs_named_type
== NULL
)
352 rhs_named_type
= rhs_type
->deref()->named_type();
356 if (rhs_named_type
== NULL
)
357 method_table
= null_pointer_node
;
360 rhs_named_type
->interface_method_table(gogo
, lhs_interface_type
,
362 first_field_value
= fold_convert_loc(location
, const_ptr_type_node
,
366 // Start building a constructor for the value we will return.
368 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
370 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
371 tree field
= TYPE_FIELDS(lhs_type_tree
);
372 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
373 (lhs_is_empty
? "__type_descriptor" : "__methods")) == 0);
375 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), first_field_value
);
377 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
378 field
= DECL_CHAIN(field
);
379 gcc_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
, build_pointer_type(TREE_TYPE(rhs_tree
)),
398 space
= save_expr(space
);
400 tree ref
= build_fold_indirect_ref_loc(location
, space
);
401 TREE_THIS_NOTRAP(ref
) = 1;
402 tree set
= fold_build2_loc(location
, MODIFY_EXPR
, void_type_node
,
405 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), space
);
407 return build2(COMPOUND_EXPR
, lhs_type_tree
, set
,
408 build_constructor(lhs_type_tree
, init
));
411 // Return a tree for the type descriptor of RHS_TREE, which has
412 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
416 Expression::get_interface_type_descriptor(Translate_context
*,
417 Type
* rhs_type
, tree rhs_tree
,
418 source_location location
)
420 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
421 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
422 tree rhs_field
= TYPE_FIELDS(rhs_type_tree
);
423 tree v
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
425 if (rhs_type
->interface_type()->is_empty())
427 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)),
428 "__type_descriptor") == 0);
432 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__methods")
434 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v
)));
436 tree v1
= build_fold_indirect_ref_loc(location
, v
);
437 gcc_assert(TREE_CODE(TREE_TYPE(v1
)) == RECORD_TYPE
);
438 tree f
= TYPE_FIELDS(TREE_TYPE(v1
));
439 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f
)), "__type_descriptor")
441 v1
= build3(COMPONENT_REF
, TREE_TYPE(f
), v1
, f
, NULL_TREE
);
443 tree eq
= fold_build2_loc(location
, EQ_EXPR
, boolean_type_node
, v
,
444 fold_convert_loc(location
, TREE_TYPE(v
),
446 tree n
= fold_convert_loc(location
, TREE_TYPE(v1
), null_pointer_node
);
447 return fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(v1
),
451 // Return a tree for the conversion of an interface type to an
455 Expression::convert_interface_to_interface(Translate_context
* context
,
456 Type
*lhs_type
, Type
*rhs_type
,
457 tree rhs_tree
, bool for_type_guard
,
458 source_location location
)
460 Gogo
* gogo
= context
->gogo();
461 Interface_type
* lhs_interface_type
= lhs_type
->interface_type();
462 bool lhs_is_empty
= lhs_interface_type
->is_empty();
464 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
465 if (lhs_type_tree
== error_mark_node
)
466 return error_mark_node
;
468 // In the general case this requires runtime examination of the type
469 // method table to match it up with the interface methods.
471 // FIXME: If all of the methods in the right hand side interface
472 // also appear in the left hand side interface, then we don't need
473 // to do a runtime check, although we still need to build a new
476 // Get the type descriptor for the right hand side. This will be
477 // NULL for a nil interface.
479 if (!DECL_P(rhs_tree
))
480 rhs_tree
= save_expr(rhs_tree
);
482 tree rhs_type_descriptor
=
483 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
486 // The result is going to be a two element constructor.
488 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
490 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
491 tree field
= TYPE_FIELDS(lhs_type_tree
);
496 // A type assertion fails when converting a nil interface.
497 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
498 static tree assert_interface_decl
;
499 tree call
= Gogo::call_builtin(&assert_interface_decl
,
501 "__go_assert_interface",
504 TREE_TYPE(lhs_type_descriptor
),
506 TREE_TYPE(rhs_type_descriptor
),
507 rhs_type_descriptor
);
508 if (call
== error_mark_node
)
509 return error_mark_node
;
510 // This will panic if the interface conversion fails.
511 TREE_NOTHROW(assert_interface_decl
) = 0;
512 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
514 else if (lhs_is_empty
)
516 // A convertion to an empty interface always succeeds, and the
517 // first field is just the type descriptor of the object.
518 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
519 "__type_descriptor") == 0);
520 gcc_assert(TREE_TYPE(field
) == TREE_TYPE(rhs_type_descriptor
));
521 elt
->value
= rhs_type_descriptor
;
525 // A conversion to a non-empty interface may fail, but unlike a
526 // type assertion converting nil will always succeed.
527 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods")
529 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
530 static tree convert_interface_decl
;
531 tree call
= Gogo::call_builtin(&convert_interface_decl
,
533 "__go_convert_interface",
536 TREE_TYPE(lhs_type_descriptor
),
538 TREE_TYPE(rhs_type_descriptor
),
539 rhs_type_descriptor
);
540 if (call
== error_mark_node
)
541 return error_mark_node
;
542 // This will panic if the interface conversion fails.
543 TREE_NOTHROW(convert_interface_decl
) = 0;
544 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
547 // The second field is simply the object pointer.
549 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
550 field
= DECL_CHAIN(field
);
551 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
554 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
555 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
556 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
557 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
558 elt
->value
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
561 return build_constructor(lhs_type_tree
, init
);
564 // Return a tree for the conversion of an interface type to a
565 // non-interface type.
568 Expression::convert_interface_to_type(Translate_context
* context
,
569 Type
*lhs_type
, Type
* rhs_type
,
570 tree rhs_tree
, source_location location
)
572 Gogo
* gogo
= context
->gogo();
573 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
575 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
576 if (lhs_type_tree
== error_mark_node
)
577 return error_mark_node
;
579 // Call a function to check that the type is valid. The function
580 // will panic with an appropriate runtime type error if the type is
583 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
585 if (!DECL_P(rhs_tree
))
586 rhs_tree
= save_expr(rhs_tree
);
588 tree rhs_type_descriptor
=
589 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
592 tree rhs_inter_descriptor
= rhs_type
->type_descriptor_pointer(gogo
);
594 static tree check_interface_type_decl
;
595 tree call
= Gogo::call_builtin(&check_interface_type_decl
,
597 "__go_check_interface_type",
600 TREE_TYPE(lhs_type_descriptor
),
602 TREE_TYPE(rhs_type_descriptor
),
604 TREE_TYPE(rhs_inter_descriptor
),
605 rhs_inter_descriptor
);
606 if (call
== error_mark_node
)
607 return error_mark_node
;
608 // This call will panic if the conversion is invalid.
609 TREE_NOTHROW(check_interface_type_decl
) = 0;
611 // If the call succeeds, pull out the value.
612 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
613 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
614 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
615 tree val
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
618 // If the value is a pointer, then it is the value we want.
619 // Otherwise it points to the value.
620 if (lhs_type
->points_to() == NULL
)
622 val
= fold_convert_loc(location
, build_pointer_type(lhs_type_tree
), val
);
623 val
= build_fold_indirect_ref_loc(location
, val
);
626 return build2(COMPOUND_EXPR
, lhs_type_tree
, call
,
627 fold_convert_loc(location
, lhs_type_tree
, val
));
630 // Convert an expression to a tree. This is implemented by the child
631 // class. Not that it is not in general safe to call this multiple
632 // times for a single expression, but that we don't catch such errors.
635 Expression::get_tree(Translate_context
* context
)
637 // The child may have marked this expression as having an error.
638 if (this->classification_
== EXPRESSION_ERROR
)
639 return error_mark_node
;
641 return this->do_get_tree(context
);
644 // Return a tree for VAL in TYPE.
647 Expression::integer_constant_tree(mpz_t val
, tree type
)
649 if (type
== error_mark_node
)
650 return error_mark_node
;
651 else if (TREE_CODE(type
) == INTEGER_TYPE
)
652 return double_int_to_tree(type
,
653 mpz_get_double_int(type
, val
, true));
654 else if (TREE_CODE(type
) == REAL_TYPE
)
657 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
658 tree ret
= Expression::float_constant_tree(fval
, type
);
662 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
665 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
666 tree real
= Expression::float_constant_tree(fval
, TREE_TYPE(type
));
668 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
670 return build_complex(type
, real
, imag
);
676 // Return a tree for VAL in TYPE.
679 Expression::float_constant_tree(mpfr_t val
, tree type
)
681 if (type
== error_mark_node
)
682 return error_mark_node
;
683 else if (TREE_CODE(type
) == INTEGER_TYPE
)
687 mpfr_get_z(ival
, val
, GMP_RNDN
);
688 tree ret
= Expression::integer_constant_tree(ival
, type
);
692 else if (TREE_CODE(type
) == REAL_TYPE
)
695 real_from_mpfr(&r1
, val
, type
, GMP_RNDN
);
697 real_convert(&r2
, TYPE_MODE(type
), &r1
);
698 return build_real(type
, r2
);
700 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
703 real_from_mpfr(&r1
, val
, TREE_TYPE(type
), GMP_RNDN
);
705 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
706 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
708 return build_complex(type
, build_real(TREE_TYPE(type
), r2
), imag
);
714 // Return a tree for REAL/IMAG in TYPE.
717 Expression::complex_constant_tree(mpfr_t real
, mpfr_t imag
, tree type
)
719 if (TREE_CODE(type
) == COMPLEX_TYPE
)
722 real_from_mpfr(&r1
, real
, TREE_TYPE(type
), GMP_RNDN
);
724 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
727 real_from_mpfr(&r3
, imag
, TREE_TYPE(type
), GMP_RNDN
);
729 real_convert(&r4
, TYPE_MODE(TREE_TYPE(type
)), &r3
);
731 return build_complex(type
, build_real(TREE_TYPE(type
), r2
),
732 build_real(TREE_TYPE(type
), r4
));
738 // Return a tree which evaluates to true if VAL, of arbitrary integer
739 // type, is negative or is more than the maximum value of BOUND_TYPE.
740 // If SOFAR is not NULL, it is or'red into the result. The return
741 // value may be NULL if SOFAR is NULL.
744 Expression::check_bounds(tree val
, tree bound_type
, tree sofar
,
747 tree val_type
= TREE_TYPE(val
);
748 tree ret
= NULL_TREE
;
750 if (!TYPE_UNSIGNED(val_type
))
752 ret
= fold_build2_loc(loc
, LT_EXPR
, boolean_type_node
, val
,
753 build_int_cst(val_type
, 0));
754 if (ret
== boolean_false_node
)
758 if ((TYPE_UNSIGNED(val_type
) && !TYPE_UNSIGNED(bound_type
))
759 || TYPE_SIZE(val_type
) > TYPE_SIZE(bound_type
))
761 tree max
= TYPE_MAX_VALUE(bound_type
);
762 tree big
= fold_build2_loc(loc
, GT_EXPR
, boolean_type_node
, val
,
763 fold_convert_loc(loc
, val_type
, max
));
764 if (big
== boolean_false_node
)
766 else if (ret
== NULL_TREE
)
769 ret
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
773 if (ret
== NULL_TREE
)
775 else if (sofar
== NULL_TREE
)
778 return fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
782 // Error expressions. This are used to avoid cascading errors.
784 class Error_expression
: public Expression
787 Error_expression(source_location location
)
788 : Expression(EXPRESSION_ERROR
, location
)
793 do_is_constant() const
797 do_integer_constant_value(bool, mpz_t val
, Type
**) const
804 do_float_constant_value(mpfr_t val
, Type
**) const
806 mpfr_set_ui(val
, 0, GMP_RNDN
);
811 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const
813 mpfr_set_ui(real
, 0, GMP_RNDN
);
814 mpfr_set_ui(imag
, 0, GMP_RNDN
);
819 do_discarding_value()
824 { return Type::make_error_type(); }
827 do_determine_type(const Type_context
*)
835 do_is_addressable() const
839 do_get_tree(Translate_context
*)
840 { return error_mark_node
; }
844 Expression::make_error(source_location location
)
846 return new Error_expression(location
);
849 // An expression which is really a type. This is used during parsing.
850 // It is an error if these survive after lowering.
853 Type_expression
: public Expression
856 Type_expression(Type
* type
, source_location location
)
857 : Expression(EXPRESSION_TYPE
, location
),
863 do_traverse(Traverse
* traverse
)
864 { return Type::traverse(this->type_
, traverse
); }
868 { return this->type_
; }
871 do_determine_type(const Type_context
*)
875 do_check_types(Gogo
*)
876 { this->report_error(_("invalid use of type")); }
883 do_get_tree(Translate_context
*)
884 { gcc_unreachable(); }
887 // The type which we are representing as an expression.
892 Expression::make_type(Type
* type
, source_location location
)
894 return new Type_expression(type
, location
);
897 // Class Parser_expression.
900 Parser_expression::do_type()
902 // We should never really ask for the type of a Parser_expression.
903 // However, it can happen, at least when we have an invalid const
904 // whose initializer refers to the const itself. In that case we
905 // may ask for the type when lowering the const itself.
906 gcc_assert(saw_errors());
907 return Type::make_error_type();
910 // Class Var_expression.
912 // Lower a variable expression. Here we just make sure that the
913 // initialization expression of the variable has been lowered. This
914 // ensures that we will be able to determine the type of the variable
918 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
920 if (this->variable_
->is_variable())
922 Variable
* var
= this->variable_
->var_value();
923 // This is either a local variable or a global variable. A
924 // reference to a variable which is local to an enclosing
925 // function will be a reference to a field in a closure.
926 if (var
->is_global())
928 var
->lower_init_expression(gogo
, function
);
933 // Return the name of the variable.
936 Var_expression::name() const
938 return this->variable_
->name();
941 // Return the type of a reference to a variable.
944 Var_expression::do_type()
946 if (this->variable_
->is_variable())
947 return this->variable_
->var_value()->type();
948 else if (this->variable_
->is_result_variable())
949 return this->variable_
->result_var_value()->type();
954 // Something takes the address of this variable. This means that we
955 // may want to move the variable onto the heap.
958 Var_expression::do_address_taken(bool escapes
)
962 else if (this->variable_
->is_variable())
963 this->variable_
->var_value()->set_address_taken();
964 else if (this->variable_
->is_result_variable())
965 this->variable_
->result_var_value()->set_address_taken();
970 // Get the tree for a reference to a variable.
973 Var_expression::do_get_tree(Translate_context
* context
)
975 return this->variable_
->get_tree(context
->gogo(), context
->function());
978 // Make a reference to a variable in an expression.
981 Expression::make_var_reference(Named_object
* var
, source_location location
)
984 return Expression::make_sink(location
);
986 // FIXME: Creating a new object for each reference to a variable is
988 return new Var_expression(var
, location
);
991 // Class Temporary_reference_expression.
996 Temporary_reference_expression::do_type()
998 return this->statement_
->type();
1001 // Called if something takes the address of this temporary variable.
1002 // We never have to move temporary variables to the heap, but we do
1003 // need to know that they must live in the stack rather than in a
1007 Temporary_reference_expression::do_address_taken(bool)
1009 this->statement_
->set_is_address_taken();
1012 // Get a tree referring to the variable.
1015 Temporary_reference_expression::do_get_tree(Translate_context
*)
1017 return this->statement_
->get_decl();
1020 // Make a reference to a temporary variable.
1023 Expression::make_temporary_reference(Temporary_statement
* statement
,
1024 source_location location
)
1026 return new Temporary_reference_expression(statement
, location
);
1029 // A sink expression--a use of the blank identifier _.
1031 class Sink_expression
: public Expression
1034 Sink_expression(source_location location
)
1035 : Expression(EXPRESSION_SINK
, location
),
1036 type_(NULL
), var_(NULL_TREE
)
1041 do_discarding_value()
1048 do_determine_type(const Type_context
*);
1052 { return new Sink_expression(this->location()); }
1055 do_get_tree(Translate_context
*);
1058 // The type of this sink variable.
1060 // The temporary variable we generate.
1064 // Return the type of a sink expression.
1067 Sink_expression::do_type()
1069 if (this->type_
== NULL
)
1070 return Type::make_sink_type();
1074 // Determine the type of a sink expression.
1077 Sink_expression::do_determine_type(const Type_context
* context
)
1079 if (context
->type
!= NULL
)
1080 this->type_
= context
->type
;
1083 // Return a temporary variable for a sink expression. This will
1084 // presumably be a write-only variable which the middle-end will drop.
1087 Sink_expression::do_get_tree(Translate_context
* context
)
1089 if (this->var_
== NULL_TREE
)
1091 gcc_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1092 this->var_
= create_tmp_var(this->type_
->get_tree(context
->gogo()),
1098 // Make a sink expression.
1101 Expression::make_sink(source_location location
)
1103 return new Sink_expression(location
);
1106 // Class Func_expression.
1108 // FIXME: Can a function expression appear in a constant expression?
1109 // The value is unchanging. Initializing a constant to the address of
1110 // a function seems like it could work, though there might be little
1113 // Return the name of the function.
1116 Func_expression::name() const
1118 return this->function_
->name();
1124 Func_expression::do_traverse(Traverse
* traverse
)
1126 return (this->closure_
== NULL
1128 : Expression::traverse(&this->closure_
, traverse
));
1131 // Return the type of a function expression.
1134 Func_expression::do_type()
1136 if (this->function_
->is_function())
1137 return this->function_
->func_value()->type();
1138 else if (this->function_
->is_function_declaration())
1139 return this->function_
->func_declaration_value()->type();
1144 // Get the tree for a function expression without evaluating the
1148 Func_expression::get_tree_without_closure(Gogo
* gogo
)
1150 Function_type
* fntype
;
1151 if (this->function_
->is_function())
1152 fntype
= this->function_
->func_value()->type();
1153 else if (this->function_
->is_function_declaration())
1154 fntype
= this->function_
->func_declaration_value()->type();
1158 // Builtin functions are handled specially by Call_expression. We
1159 // can't take their address.
1160 if (fntype
->is_builtin())
1162 error_at(this->location(), "invalid use of special builtin function %qs",
1163 this->function_
->name().c_str());
1164 return error_mark_node
;
1167 Named_object
* no
= this->function_
;
1169 tree id
= no
->get_id(gogo
);
1170 if (id
== error_mark_node
)
1171 return error_mark_node
;
1174 if (no
->is_function())
1175 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
, id
);
1176 else if (no
->is_function_declaration())
1177 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
, id
);
1181 if (fndecl
== error_mark_node
)
1182 return error_mark_node
;
1184 return build_fold_addr_expr_loc(this->location(), fndecl
);
1187 // Get the tree for a function expression. This is used when we take
1188 // the address of a function rather than simply calling it. If the
1189 // function has a closure, we must use a trampoline.
1192 Func_expression::do_get_tree(Translate_context
* context
)
1194 Gogo
* gogo
= context
->gogo();
1196 tree fnaddr
= this->get_tree_without_closure(gogo
);
1197 if (fnaddr
== error_mark_node
)
1198 return error_mark_node
;
1200 gcc_assert(TREE_CODE(fnaddr
) == ADDR_EXPR
1201 && TREE_CODE(TREE_OPERAND(fnaddr
, 0)) == FUNCTION_DECL
);
1202 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr
, 0)) = 1;
1204 // For a normal non-nested function call, that is all we have to do.
1205 if (!this->function_
->is_function()
1206 || this->function_
->func_value()->enclosing() == NULL
)
1208 gcc_assert(this->closure_
== NULL
);
1212 // For a nested function call, we have to always allocate a
1213 // trampoline. If we don't always allocate, then closures will not
1214 // be reliably distinct.
1215 Expression
* closure
= this->closure_
;
1217 if (closure
== NULL
)
1218 closure_tree
= null_pointer_node
;
1221 // Get the value of the closure. This will be a pointer to
1222 // space allocated on the heap.
1223 closure_tree
= closure
->get_tree(context
);
1224 if (closure_tree
== error_mark_node
)
1225 return error_mark_node
;
1226 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree
)));
1229 // Now we need to build some code on the heap. This code will load
1230 // the static chain pointer with the closure and then jump to the
1231 // body of the function. The normal gcc approach is to build the
1232 // code on the stack. Unfortunately we can not do that, as Go
1233 // permits us to return the function pointer.
1235 return gogo
->make_trampoline(fnaddr
, closure_tree
, this->location());
1238 // Make a reference to a function in an expression.
1241 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1242 source_location location
)
1244 return new Func_expression(function
, closure
, location
);
1247 // Class Unknown_expression.
1249 // Return the name of an unknown expression.
1252 Unknown_expression::name() const
1254 return this->named_object_
->name();
1257 // Lower a reference to an unknown name.
1260 Unknown_expression::do_lower(Gogo
*, Named_object
*, int)
1262 source_location location
= this->location();
1263 Named_object
* no
= this->named_object_
;
1265 if (!no
->is_unknown())
1269 real
= no
->unknown_value()->real_named_object();
1272 if (this->is_composite_literal_key_
)
1274 error_at(location
, "reference to undefined name %qs",
1275 this->named_object_
->message_name().c_str());
1276 return Expression::make_error(location
);
1279 switch (real
->classification())
1281 case Named_object::NAMED_OBJECT_CONST
:
1282 return Expression::make_const_reference(real
, location
);
1283 case Named_object::NAMED_OBJECT_TYPE
:
1284 return Expression::make_type(real
->type_value(), location
);
1285 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1286 if (this->is_composite_literal_key_
)
1288 error_at(location
, "reference to undefined type %qs",
1289 real
->message_name().c_str());
1290 return Expression::make_error(location
);
1291 case Named_object::NAMED_OBJECT_VAR
:
1292 return Expression::make_var_reference(real
, location
);
1293 case Named_object::NAMED_OBJECT_FUNC
:
1294 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1295 return Expression::make_func_reference(real
, NULL
, location
);
1296 case Named_object::NAMED_OBJECT_PACKAGE
:
1297 if (this->is_composite_literal_key_
)
1299 error_at(location
, "unexpected reference to package");
1300 return Expression::make_error(location
);
1306 // Make a reference to an unknown name.
1309 Expression::make_unknown_reference(Named_object
* no
, source_location location
)
1311 gcc_assert(no
->resolve()->is_unknown());
1312 return new Unknown_expression(no
, location
);
1315 // A boolean expression.
1317 class Boolean_expression
: public Expression
1320 Boolean_expression(bool val
, source_location location
)
1321 : Expression(EXPRESSION_BOOLEAN
, location
),
1322 val_(val
), type_(NULL
)
1330 do_is_constant() const
1337 do_determine_type(const Type_context
*);
1344 do_get_tree(Translate_context
*)
1345 { return this->val_
? boolean_true_node
: boolean_false_node
; }
1348 do_export(Export
* exp
) const
1349 { exp
->write_c_string(this->val_
? "true" : "false"); }
1354 // The type as determined by context.
1361 Boolean_expression::do_type()
1363 if (this->type_
== NULL
)
1364 this->type_
= Type::make_boolean_type();
1368 // Set the type from the context.
1371 Boolean_expression::do_determine_type(const Type_context
* context
)
1373 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1375 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1376 this->type_
= context
->type
;
1377 else if (!context
->may_be_abstract
)
1378 this->type_
= Type::lookup_bool_type();
1381 // Import a boolean constant.
1384 Boolean_expression::do_import(Import
* imp
)
1386 if (imp
->peek_char() == 't')
1388 imp
->require_c_string("true");
1389 return Expression::make_boolean(true, imp
->location());
1393 imp
->require_c_string("false");
1394 return Expression::make_boolean(false, imp
->location());
1398 // Make a boolean expression.
1401 Expression::make_boolean(bool val
, source_location location
)
1403 return new Boolean_expression(val
, location
);
1406 // Class String_expression.
1411 String_expression::do_type()
1413 if (this->type_
== NULL
)
1414 this->type_
= Type::make_string_type();
1418 // Set the type from the context.
1421 String_expression::do_determine_type(const Type_context
* context
)
1423 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1425 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1426 this->type_
= context
->type
;
1427 else if (!context
->may_be_abstract
)
1428 this->type_
= Type::lookup_string_type();
1431 // Build a string constant.
1434 String_expression::do_get_tree(Translate_context
* context
)
1436 return context
->gogo()->go_string_constant_tree(this->val_
);
1439 // Export a string expression.
1442 String_expression::do_export(Export
* exp
) const
1445 s
.reserve(this->val_
.length() * 4 + 2);
1447 for (std::string::const_iterator p
= this->val_
.begin();
1448 p
!= this->val_
.end();
1451 if (*p
== '\\' || *p
== '"')
1456 else if (*p
>= 0x20 && *p
< 0x7f)
1458 else if (*p
== '\n')
1460 else if (*p
== '\t')
1465 unsigned char c
= *p
;
1466 unsigned int dig
= c
>> 4;
1467 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1469 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1473 exp
->write_string(s
);
1476 // Import a string expression.
1479 String_expression::do_import(Import
* imp
)
1481 imp
->require_c_string("\"");
1485 int c
= imp
->get_char();
1486 if (c
== '"' || c
== -1)
1489 val
+= static_cast<char>(c
);
1492 c
= imp
->get_char();
1493 if (c
== '\\' || c
== '"')
1494 val
+= static_cast<char>(c
);
1501 c
= imp
->get_char();
1502 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1503 c
= imp
->get_char();
1504 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1505 char v
= (vh
<< 4) | vl
;
1510 error_at(imp
->location(), "bad string constant");
1511 return Expression::make_error(imp
->location());
1515 return Expression::make_string(val
, imp
->location());
1518 // Make a string expression.
1521 Expression::make_string(const std::string
& val
, source_location location
)
1523 return new String_expression(val
, location
);
1526 // Make an integer expression.
1528 class Integer_expression
: public Expression
1531 Integer_expression(const mpz_t
* val
, Type
* type
, source_location location
)
1532 : Expression(EXPRESSION_INTEGER
, location
),
1534 { mpz_init_set(this->val_
, *val
); }
1539 // Return whether VAL fits in the type.
1541 check_constant(mpz_t val
, Type
*, source_location
);
1543 // Write VAL to export data.
1545 export_integer(Export
* exp
, const mpz_t val
);
1549 do_is_constant() const
1553 do_integer_constant_value(bool, mpz_t val
, Type
** ptype
) const;
1559 do_determine_type(const Type_context
* context
);
1562 do_check_types(Gogo
*);
1565 do_get_tree(Translate_context
*);
1569 { return Expression::make_integer(&this->val_
, this->type_
,
1570 this->location()); }
1573 do_export(Export
*) const;
1576 // The integer value.
1582 // Return an integer constant value.
1585 Integer_expression::do_integer_constant_value(bool, mpz_t val
,
1588 if (this->type_
!= NULL
)
1589 *ptype
= this->type_
;
1590 mpz_set(val
, this->val_
);
1594 // Return the current type. If we haven't set the type yet, we return
1595 // an abstract integer type.
1598 Integer_expression::do_type()
1600 if (this->type_
== NULL
)
1601 this->type_
= Type::make_abstract_integer_type();
1605 // Set the type of the integer value. Here we may switch from an
1606 // abstract type to a real type.
1609 Integer_expression::do_determine_type(const Type_context
* context
)
1611 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1613 else if (context
->type
!= NULL
1614 && (context
->type
->integer_type() != NULL
1615 || context
->type
->float_type() != NULL
1616 || context
->type
->complex_type() != NULL
))
1617 this->type_
= context
->type
;
1618 else if (!context
->may_be_abstract
)
1619 this->type_
= Type::lookup_integer_type("int");
1622 // Return true if the integer VAL fits in the range of the type TYPE.
1623 // Otherwise give an error and return false. TYPE may be NULL.
1626 Integer_expression::check_constant(mpz_t val
, Type
* type
,
1627 source_location location
)
1631 Integer_type
* itype
= type
->integer_type();
1632 if (itype
== NULL
|| itype
->is_abstract())
1635 int bits
= mpz_sizeinbase(val
, 2);
1637 if (itype
->is_unsigned())
1639 // For an unsigned type we can only accept a nonnegative number,
1640 // and we must be able to represent at least BITS.
1641 if (mpz_sgn(val
) >= 0
1642 && bits
<= itype
->bits())
1647 // For a signed type we need an extra bit to indicate the sign.
1648 // We have to handle the most negative integer specially.
1649 if (bits
+ 1 <= itype
->bits()
1650 || (bits
<= itype
->bits()
1652 && (mpz_scan1(val
, 0)
1653 == static_cast<unsigned long>(itype
->bits() - 1))
1654 && mpz_scan0(val
, itype
->bits()) == ULONG_MAX
))
1658 error_at(location
, "integer constant overflow");
1662 // Check the type of an integer constant.
1665 Integer_expression::do_check_types(Gogo
*)
1667 if (this->type_
== NULL
)
1669 if (!Integer_expression::check_constant(this->val_
, this->type_
,
1671 this->set_is_error();
1674 // Get a tree for an integer constant.
1677 Integer_expression::do_get_tree(Translate_context
* context
)
1679 Gogo
* gogo
= context
->gogo();
1681 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1682 type
= this->type_
->get_tree(gogo
);
1683 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1685 // We are converting to an abstract floating point type.
1686 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
1688 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1690 // We are converting to an abstract complex type.
1691 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
1695 // If we still have an abstract type here, then this is being
1696 // used in a constant expression which didn't get reduced for
1697 // some reason. Use a type which will fit the value. We use <,
1698 // not <=, because we need an extra bit for the sign bit.
1699 int bits
= mpz_sizeinbase(this->val_
, 2);
1700 if (bits
< INT_TYPE_SIZE
)
1701 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
1703 type
= Type::lookup_integer_type("int64")->get_tree(gogo
);
1705 type
= long_long_integer_type_node
;
1707 return Expression::integer_constant_tree(this->val_
, type
);
1710 // Write VAL to export data.
1713 Integer_expression::export_integer(Export
* exp
, const mpz_t val
)
1715 char* s
= mpz_get_str(NULL
, 10, val
);
1716 exp
->write_c_string(s
);
1720 // Export an integer in a constant expression.
1723 Integer_expression::do_export(Export
* exp
) const
1725 Integer_expression::export_integer(exp
, this->val_
);
1726 // A trailing space lets us reliably identify the end of the number.
1727 exp
->write_c_string(" ");
1730 // Import an integer, floating point, or complex value. This handles
1731 // all these types because they all start with digits.
1734 Integer_expression::do_import(Import
* imp
)
1736 std::string num
= imp
->read_identifier();
1737 imp
->require_c_string(" ");
1738 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1741 size_t plus_pos
= num
.find('+', 1);
1742 size_t minus_pos
= num
.find('-', 1);
1744 if (plus_pos
== std::string::npos
)
1746 else if (minus_pos
== std::string::npos
)
1750 error_at(imp
->location(), "bad number in import data: %qs",
1752 return Expression::make_error(imp
->location());
1754 if (pos
== std::string::npos
)
1755 mpfr_set_ui(real
, 0, GMP_RNDN
);
1758 std::string real_str
= num
.substr(0, pos
);
1759 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1761 error_at(imp
->location(), "bad number in import data: %qs",
1763 return Expression::make_error(imp
->location());
1767 std::string imag_str
;
1768 if (pos
== std::string::npos
)
1771 imag_str
= num
.substr(pos
);
1772 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
1774 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
1776 error_at(imp
->location(), "bad number in import data: %qs",
1778 return Expression::make_error(imp
->location());
1780 Expression
* ret
= Expression::make_complex(&real
, &imag
, NULL
,
1786 else if (num
.find('.') == std::string::npos
1787 && num
.find('E') == std::string::npos
)
1790 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
1792 error_at(imp
->location(), "bad number in import data: %qs",
1794 return Expression::make_error(imp
->location());
1796 Expression
* ret
= Expression::make_integer(&val
, NULL
, imp
->location());
1803 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
1805 error_at(imp
->location(), "bad number in import data: %qs",
1807 return Expression::make_error(imp
->location());
1809 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
1815 // Build a new integer value.
1818 Expression::make_integer(const mpz_t
* val
, Type
* type
,
1819 source_location location
)
1821 return new Integer_expression(val
, type
, location
);
1826 class Float_expression
: public Expression
1829 Float_expression(const mpfr_t
* val
, Type
* type
, source_location location
)
1830 : Expression(EXPRESSION_FLOAT
, location
),
1833 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
1836 // Constrain VAL to fit into TYPE.
1838 constrain_float(mpfr_t val
, Type
* type
);
1840 // Return whether VAL fits in the type.
1842 check_constant(mpfr_t val
, Type
*, source_location
);
1844 // Write VAL to export data.
1846 export_float(Export
* exp
, const mpfr_t val
);
1850 do_is_constant() const
1854 do_float_constant_value(mpfr_t val
, Type
**) const;
1860 do_determine_type(const Type_context
*);
1863 do_check_types(Gogo
*);
1867 { return Expression::make_float(&this->val_
, this->type_
,
1868 this->location()); }
1871 do_get_tree(Translate_context
*);
1874 do_export(Export
*) const;
1877 // The floating point value.
1883 // Constrain VAL to fit into TYPE.
1886 Float_expression::constrain_float(mpfr_t val
, Type
* type
)
1888 Float_type
* ftype
= type
->float_type();
1889 if (ftype
!= NULL
&& !ftype
->is_abstract())
1891 tree type_tree
= ftype
->type_tree();
1892 REAL_VALUE_TYPE rvt
;
1893 real_from_mpfr(&rvt
, val
, type_tree
, GMP_RNDN
);
1894 real_convert(&rvt
, TYPE_MODE(type_tree
), &rvt
);
1895 mpfr_from_real(val
, &rvt
, GMP_RNDN
);
1899 // Return a floating point constant value.
1902 Float_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
1904 if (this->type_
!= NULL
)
1905 *ptype
= this->type_
;
1906 mpfr_set(val
, this->val_
, GMP_RNDN
);
1910 // Return the current type. If we haven't set the type yet, we return
1911 // an abstract float type.
1914 Float_expression::do_type()
1916 if (this->type_
== NULL
)
1917 this->type_
= Type::make_abstract_float_type();
1921 // Set the type of the float value. Here we may switch from an
1922 // abstract type to a real type.
1925 Float_expression::do_determine_type(const Type_context
* context
)
1927 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1929 else if (context
->type
!= NULL
1930 && (context
->type
->integer_type() != NULL
1931 || context
->type
->float_type() != NULL
1932 || context
->type
->complex_type() != NULL
))
1933 this->type_
= context
->type
;
1934 else if (!context
->may_be_abstract
)
1935 this->type_
= Type::lookup_float_type("float");
1938 // Return true if the floating point value VAL fits in the range of
1939 // the type TYPE. Otherwise give an error and return false. TYPE may
1943 Float_expression::check_constant(mpfr_t val
, Type
* type
,
1944 source_location location
)
1948 Float_type
* ftype
= type
->float_type();
1949 if (ftype
== NULL
|| ftype
->is_abstract())
1952 // A NaN or Infinity always fits in the range of the type.
1953 if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
1956 mp_exp_t exp
= mpfr_get_exp(val
);
1958 switch (ftype
->bits())
1971 error_at(location
, "floating point constant overflow");
1977 // Check the type of a float value.
1980 Float_expression::do_check_types(Gogo
*)
1982 if (this->type_
== NULL
)
1985 if (!Float_expression::check_constant(this->val_
, this->type_
,
1987 this->set_is_error();
1989 Integer_type
* integer_type
= this->type_
->integer_type();
1990 if (integer_type
!= NULL
)
1992 if (!mpfr_integer_p(this->val_
))
1993 this->report_error(_("floating point constant truncated to integer"));
1996 gcc_assert(!integer_type
->is_abstract());
1999 mpfr_get_z(ival
, this->val_
, GMP_RNDN
);
2000 Integer_expression::check_constant(ival
, integer_type
,
2007 // Get a tree for a float constant.
2010 Float_expression::do_get_tree(Translate_context
* context
)
2012 Gogo
* gogo
= context
->gogo();
2014 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2015 type
= this->type_
->get_tree(gogo
);
2016 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
2018 // We have an abstract integer type. We just hope for the best.
2019 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
2023 // If we still have an abstract type here, then this is being
2024 // used in a constant expression which didn't get reduced. We
2025 // just use float64 and hope for the best.
2026 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
2028 return Expression::float_constant_tree(this->val_
, type
);
2031 // Write a floating point number to export data.
2034 Float_expression::export_float(Export
*exp
, const mpfr_t val
)
2037 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2039 exp
->write_c_string("-");
2040 exp
->write_c_string("0.");
2041 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2044 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2045 exp
->write_c_string(buf
);
2048 // Export a floating point number in a constant expression.
2051 Float_expression::do_export(Export
* exp
) const
2053 Float_expression::export_float(exp
, this->val_
);
2054 // A trailing space lets us reliably identify the end of the number.
2055 exp
->write_c_string(" ");
2058 // Make a float expression.
2061 Expression::make_float(const mpfr_t
* val
, Type
* type
, source_location location
)
2063 return new Float_expression(val
, type
, location
);
2068 class Complex_expression
: public Expression
2071 Complex_expression(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2072 source_location location
)
2073 : Expression(EXPRESSION_COMPLEX
, location
),
2076 mpfr_init_set(this->real_
, *real
, GMP_RNDN
);
2077 mpfr_init_set(this->imag_
, *imag
, GMP_RNDN
);
2080 // Constrain REAL/IMAG to fit into TYPE.
2082 constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
);
2084 // Return whether REAL/IMAG fits in the type.
2086 check_constant(mpfr_t real
, mpfr_t imag
, Type
*, source_location
);
2088 // Write REAL/IMAG to export data.
2090 export_complex(Export
* exp
, const mpfr_t real
, const mpfr_t val
);
2094 do_is_constant() const
2098 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2104 do_determine_type(const Type_context
*);
2107 do_check_types(Gogo
*);
2112 return Expression::make_complex(&this->real_
, &this->imag_
, this->type_
,
2117 do_get_tree(Translate_context
*);
2120 do_export(Export
*) const;
2125 // The imaginary part;
2127 // The type if known.
2131 // Constrain REAL/IMAG to fit into TYPE.
2134 Complex_expression::constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
)
2136 Complex_type
* ctype
= type
->complex_type();
2137 if (ctype
!= NULL
&& !ctype
->is_abstract())
2139 tree type_tree
= ctype
->type_tree();
2141 REAL_VALUE_TYPE rvt
;
2142 real_from_mpfr(&rvt
, real
, TREE_TYPE(type_tree
), GMP_RNDN
);
2143 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2144 mpfr_from_real(real
, &rvt
, GMP_RNDN
);
2146 real_from_mpfr(&rvt
, imag
, TREE_TYPE(type_tree
), GMP_RNDN
);
2147 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2148 mpfr_from_real(imag
, &rvt
, GMP_RNDN
);
2152 // Return a complex constant value.
2155 Complex_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2158 if (this->type_
!= NULL
)
2159 *ptype
= this->type_
;
2160 mpfr_set(real
, this->real_
, GMP_RNDN
);
2161 mpfr_set(imag
, this->imag_
, GMP_RNDN
);
2165 // Return the current type. If we haven't set the type yet, we return
2166 // an abstract complex type.
2169 Complex_expression::do_type()
2171 if (this->type_
== NULL
)
2172 this->type_
= Type::make_abstract_complex_type();
2176 // Set the type of the complex value. Here we may switch from an
2177 // abstract type to a real type.
2180 Complex_expression::do_determine_type(const Type_context
* context
)
2182 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2184 else if (context
->type
!= NULL
2185 && context
->type
->complex_type() != NULL
)
2186 this->type_
= context
->type
;
2187 else if (!context
->may_be_abstract
)
2188 this->type_
= Type::lookup_complex_type("complex");
2191 // Return true if the complex value REAL/IMAG fits in the range of the
2192 // type TYPE. Otherwise give an error and return false. TYPE may be
2196 Complex_expression::check_constant(mpfr_t real
, mpfr_t imag
, Type
* type
,
2197 source_location location
)
2201 Complex_type
* ctype
= type
->complex_type();
2202 if (ctype
== NULL
|| ctype
->is_abstract())
2206 switch (ctype
->bits())
2218 // A NaN or Infinity always fits in the range of the type.
2219 if (!mpfr_nan_p(real
) && !mpfr_inf_p(real
) && !mpfr_zero_p(real
))
2221 if (mpfr_get_exp(real
) > max_exp
)
2223 error_at(location
, "complex real part constant overflow");
2228 if (!mpfr_nan_p(imag
) && !mpfr_inf_p(imag
) && !mpfr_zero_p(imag
))
2230 if (mpfr_get_exp(imag
) > max_exp
)
2232 error_at(location
, "complex imaginary part constant overflow");
2240 // Check the type of a complex value.
2243 Complex_expression::do_check_types(Gogo
*)
2245 if (this->type_
== NULL
)
2248 if (!Complex_expression::check_constant(this->real_
, this->imag_
,
2249 this->type_
, this->location()))
2250 this->set_is_error();
2253 // Get a tree for a complex constant.
2256 Complex_expression::do_get_tree(Translate_context
* context
)
2258 Gogo
* gogo
= context
->gogo();
2260 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2261 type
= this->type_
->get_tree(gogo
);
2264 // If we still have an abstract type here, this this is being
2265 // used in a constant expression which didn't get reduced. We
2266 // just use complex128 and hope for the best.
2267 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
2269 return Expression::complex_constant_tree(this->real_
, this->imag_
, type
);
2272 // Write REAL/IMAG to export data.
2275 Complex_expression::export_complex(Export
* exp
, const mpfr_t real
,
2278 if (!mpfr_zero_p(real
))
2280 Float_expression::export_float(exp
, real
);
2281 if (mpfr_sgn(imag
) > 0)
2282 exp
->write_c_string("+");
2284 Float_expression::export_float(exp
, imag
);
2285 exp
->write_c_string("i");
2288 // Export a complex number in a constant expression.
2291 Complex_expression::do_export(Export
* exp
) const
2293 Complex_expression::export_complex(exp
, this->real_
, this->imag_
);
2294 // A trailing space lets us reliably identify the end of the number.
2295 exp
->write_c_string(" ");
2298 // Make a complex expression.
2301 Expression::make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2302 source_location location
)
2304 return new Complex_expression(real
, imag
, type
, location
);
2307 // Find a named object in an expression.
2309 class Find_named_object
: public Traverse
2312 Find_named_object(Named_object
* no
)
2313 : Traverse(traverse_expressions
),
2314 no_(no
), found_(false)
2317 // Whether we found the object.
2320 { return this->found_
; }
2324 expression(Expression
**);
2327 // The object we are looking for.
2329 // Whether we found it.
2333 // A reference to a const in an expression.
2335 class Const_expression
: public Expression
2338 Const_expression(Named_object
* constant
, source_location location
)
2339 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2340 constant_(constant
), type_(NULL
), seen_(false)
2345 { return this->constant_
; }
2349 { return this->constant_
->name(); }
2353 do_lower(Gogo
*, Named_object
*, int);
2356 do_is_constant() const
2360 do_integer_constant_value(bool, mpz_t val
, Type
**) const;
2363 do_float_constant_value(mpfr_t val
, Type
**) const;
2366 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2369 do_string_constant_value(std::string
* val
) const
2370 { return this->constant_
->const_value()->expr()->string_constant_value(val
); }
2375 // The type of a const is set by the declaration, not the use.
2377 do_determine_type(const Type_context
*);
2380 do_check_types(Gogo
*);
2387 do_get_tree(Translate_context
* context
);
2389 // When exporting a reference to a const as part of a const
2390 // expression, we export the value. We ignore the fact that it has
2393 do_export(Export
* exp
) const
2394 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2398 Named_object
* constant_
;
2399 // The type of this reference. This is used if the constant has an
2402 // Used to prevent infinite recursion when a constant incorrectly
2403 // refers to itself.
2407 // Lower a constant expression. This is where we convert the
2408 // predeclared constant iota into an integer value.
2411 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2413 if (this->constant_
->const_value()->expr()->classification()
2416 if (iota_value
== -1)
2418 error_at(this->location(),
2419 "iota is only defined in const declarations");
2423 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2424 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2430 // Make sure that the constant itself has been lowered.
2431 gogo
->lower_constant(this->constant_
);
2436 // Return an integer constant value.
2439 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2446 if (this->type_
!= NULL
)
2447 ctype
= this->type_
;
2449 ctype
= this->constant_
->const_value()->type();
2450 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2453 Expression
* e
= this->constant_
->const_value()->expr();
2458 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2460 this->seen_
= false;
2464 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2467 *ptype
= ctype
!= NULL
? ctype
: t
;
2471 // Return a floating point constant value.
2474 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2480 if (this->type_
!= NULL
)
2481 ctype
= this->type_
;
2483 ctype
= this->constant_
->const_value()->type();
2484 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2490 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2493 this->seen_
= false;
2495 if (r
&& ctype
!= NULL
)
2497 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2499 Float_expression::constrain_float(val
, ctype
);
2501 *ptype
= ctype
!= NULL
? ctype
: t
;
2505 // Return a complex constant value.
2508 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2515 if (this->type_
!= NULL
)
2516 ctype
= this->type_
;
2518 ctype
= this->constant_
->const_value()->type();
2519 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2525 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2529 this->seen_
= false;
2531 if (r
&& ctype
!= NULL
)
2533 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2536 Complex_expression::constrain_complex(real
, imag
, ctype
);
2538 *ptype
= ctype
!= NULL
? ctype
: t
;
2542 // Return the type of the const reference.
2545 Const_expression::do_type()
2547 if (this->type_
!= NULL
)
2550 Named_constant
* nc
= this->constant_
->const_value();
2552 if (this->seen_
|| nc
->lowering())
2554 this->report_error(_("constant refers to itself"));
2555 this->type_
= Type::make_error_type();
2561 Type
* ret
= nc
->type();
2565 this->seen_
= false;
2569 // During parsing, a named constant may have a NULL type, but we
2570 // must not return a NULL type here.
2571 ret
= nc
->expr()->type();
2573 this->seen_
= false;
2578 // Set the type of the const reference.
2581 Const_expression::do_determine_type(const Type_context
* context
)
2583 Type
* ctype
= this->constant_
->const_value()->type();
2584 Type
* cetype
= (ctype
!= NULL
2586 : this->constant_
->const_value()->expr()->type());
2587 if (ctype
!= NULL
&& !ctype
->is_abstract())
2589 else if (context
->type
!= NULL
2590 && (context
->type
->integer_type() != NULL
2591 || context
->type
->float_type() != NULL
2592 || context
->type
->complex_type() != NULL
)
2593 && (cetype
->integer_type() != NULL
2594 || cetype
->float_type() != NULL
2595 || cetype
->complex_type() != NULL
))
2596 this->type_
= context
->type
;
2597 else if (context
->type
!= NULL
2598 && context
->type
->is_string_type()
2599 && cetype
->is_string_type())
2600 this->type_
= context
->type
;
2601 else if (context
->type
!= NULL
2602 && context
->type
->is_boolean_type()
2603 && cetype
->is_boolean_type())
2604 this->type_
= context
->type
;
2605 else if (!context
->may_be_abstract
)
2607 if (cetype
->is_abstract())
2608 cetype
= cetype
->make_non_abstract_type();
2609 this->type_
= cetype
;
2613 // Check types of a const reference.
2616 Const_expression::do_check_types(Gogo
*)
2618 if (this->type_
!= NULL
&& this->type_
->is_error_type())
2621 Expression
* init
= this->constant_
->const_value()->expr();
2622 Find_named_object
find_named_object(this->constant_
);
2623 Expression::traverse(&init
, &find_named_object
);
2624 if (find_named_object
.found())
2626 this->report_error(_("constant refers to itself"));
2627 this->type_
= Type::make_error_type();
2631 if (this->type_
== NULL
|| this->type_
->is_abstract())
2634 // Check for integer overflow.
2635 if (this->type_
->integer_type() != NULL
)
2640 if (!this->integer_constant_value(true, ival
, &dummy
))
2644 Expression
* cexpr
= this->constant_
->const_value()->expr();
2645 if (cexpr
->float_constant_value(fval
, &dummy
))
2647 if (!mpfr_integer_p(fval
))
2648 this->report_error(_("floating point constant "
2649 "truncated to integer"));
2652 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2653 Integer_expression::check_constant(ival
, this->type_
,
2663 // Return a tree for the const reference.
2666 Const_expression::do_get_tree(Translate_context
* context
)
2668 Gogo
* gogo
= context
->gogo();
2670 if (this->type_
== NULL
)
2671 type_tree
= NULL_TREE
;
2674 type_tree
= this->type_
->get_tree(gogo
);
2675 if (type_tree
== error_mark_node
)
2676 return error_mark_node
;
2679 // If the type has been set for this expression, but the underlying
2680 // object is an abstract int or float, we try to get the abstract
2681 // value. Otherwise we may lose something in the conversion.
2682 if (this->type_
!= NULL
2683 && (this->constant_
->const_value()->type() == NULL
2684 || this->constant_
->const_value()->type()->is_abstract()))
2686 Expression
* expr
= this->constant_
->const_value()->expr();
2690 if (expr
->integer_constant_value(true, ival
, &t
))
2692 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2700 if (expr
->float_constant_value(fval
, &t
))
2702 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2709 if (expr
->complex_constant_value(fval
, imag
, &t
))
2711 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2720 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2721 if (this->type_
== NULL
2722 || const_tree
== error_mark_node
2723 || TREE_TYPE(const_tree
) == error_mark_node
)
2727 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2728 ret
= fold_convert(type_tree
, const_tree
);
2729 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2730 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2731 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2732 ret
= fold(convert_to_real(type_tree
, const_tree
));
2733 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2734 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2740 // Make a reference to a constant in an expression.
2743 Expression::make_const_reference(Named_object
* constant
,
2744 source_location location
)
2746 return new Const_expression(constant
, location
);
2749 // Find a named object in an expression.
2752 Find_named_object::expression(Expression
** pexpr
)
2754 switch ((*pexpr
)->classification())
2756 case Expression::EXPRESSION_CONST_REFERENCE
:
2757 if (static_cast<Const_expression
*>(*pexpr
)->named_object() == this->no_
)
2759 return TRAVERSE_CONTINUE
;
2760 case Expression::EXPRESSION_VAR_REFERENCE
:
2761 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2763 return TRAVERSE_CONTINUE
;
2764 case Expression::EXPRESSION_FUNC_REFERENCE
:
2765 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2767 return TRAVERSE_CONTINUE
;
2769 return TRAVERSE_CONTINUE
;
2771 this->found_
= true;
2772 return TRAVERSE_EXIT
;
2777 class Nil_expression
: public Expression
2780 Nil_expression(source_location location
)
2781 : Expression(EXPRESSION_NIL
, location
)
2789 do_is_constant() const
2794 { return Type::make_nil_type(); }
2797 do_determine_type(const Type_context
*)
2805 do_get_tree(Translate_context
*)
2806 { return null_pointer_node
; }
2809 do_export(Export
* exp
) const
2810 { exp
->write_c_string("nil"); }
2813 // Import a nil expression.
2816 Nil_expression::do_import(Import
* imp
)
2818 imp
->require_c_string("nil");
2819 return Expression::make_nil(imp
->location());
2822 // Make a nil expression.
2825 Expression::make_nil(source_location location
)
2827 return new Nil_expression(location
);
2830 // The value of the predeclared constant iota. This is little more
2831 // than a marker. This will be lowered to an integer in
2832 // Const_expression::do_lower, which is where we know the value that
2835 class Iota_expression
: public Parser_expression
2838 Iota_expression(source_location location
)
2839 : Parser_expression(EXPRESSION_IOTA
, location
)
2844 do_lower(Gogo
*, Named_object
*, int)
2845 { gcc_unreachable(); }
2847 // There should only ever be one of these.
2850 { gcc_unreachable(); }
2853 // Make an iota expression. This is only called for one case: the
2854 // value of the predeclared constant iota.
2857 Expression::make_iota()
2859 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2860 return &iota_expression
;
2863 // A type conversion expression.
2865 class Type_conversion_expression
: public Expression
2868 Type_conversion_expression(Type
* type
, Expression
* expr
,
2869 source_location location
)
2870 : Expression(EXPRESSION_CONVERSION
, location
),
2871 type_(type
), expr_(expr
), may_convert_function_types_(false)
2874 // Return the type to which we are converting.
2877 { return this->type_
; }
2879 // Return the expression which we are converting.
2882 { return this->expr_
; }
2884 // Permit converting from one function type to another. This is
2885 // used internally for method expressions.
2887 set_may_convert_function_types()
2889 this->may_convert_function_types_
= true;
2892 // Import a type conversion expression.
2898 do_traverse(Traverse
* traverse
);
2901 do_lower(Gogo
*, Named_object
*, int);
2904 do_is_constant() const
2905 { return this->expr_
->is_constant(); }
2908 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2911 do_float_constant_value(mpfr_t
, Type
**) const;
2914 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2917 do_string_constant_value(std::string
*) const;
2921 { return this->type_
; }
2924 do_determine_type(const Type_context
*)
2926 Type_context
subcontext(this->type_
, false);
2927 this->expr_
->determine_type(&subcontext
);
2931 do_check_types(Gogo
*);
2936 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2941 do_get_tree(Translate_context
* context
);
2944 do_export(Export
*) const;
2947 // The type to convert to.
2949 // The expression to convert.
2951 // True if this is permitted to convert function types. This is
2952 // used internally for method expressions.
2953 bool may_convert_function_types_
;
2959 Type_conversion_expression::do_traverse(Traverse
* traverse
)
2961 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
2962 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
2963 return TRAVERSE_EXIT
;
2964 return TRAVERSE_CONTINUE
;
2967 // Convert to a constant at lowering time.
2970 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
2972 Type
* type
= this->type_
;
2973 Expression
* val
= this->expr_
;
2974 source_location location
= this->location();
2976 if (type
->integer_type() != NULL
)
2981 if (val
->integer_constant_value(false, ival
, &dummy
))
2983 if (!Integer_expression::check_constant(ival
, type
, location
))
2984 mpz_set_ui(ival
, 0);
2985 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2992 if (val
->float_constant_value(fval
, &dummy
))
2994 if (!mpfr_integer_p(fval
))
2997 "floating point constant truncated to integer");
2998 return Expression::make_error(location
);
3000 mpfr_get_z(ival
, fval
, GMP_RNDN
);
3001 if (!Integer_expression::check_constant(ival
, type
, location
))
3002 mpz_set_ui(ival
, 0);
3003 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
3012 if (type
->float_type() != NULL
)
3017 if (val
->float_constant_value(fval
, &dummy
))
3019 if (!Float_expression::check_constant(fval
, type
, location
))
3020 mpfr_set_ui(fval
, 0, GMP_RNDN
);
3021 Float_expression::constrain_float(fval
, type
);
3022 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
3029 if (type
->complex_type() != NULL
)
3036 if (val
->complex_constant_value(real
, imag
, &dummy
))
3038 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
3040 mpfr_set_ui(real
, 0, GMP_RNDN
);
3041 mpfr_set_ui(imag
, 0, GMP_RNDN
);
3043 Complex_expression::constrain_complex(real
, imag
, type
);
3044 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
3054 if (type
->is_open_array_type() && type
->named_type() == NULL
)
3056 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3057 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
3058 bool is_int
= element_type
== Type::lookup_integer_type("int");
3059 if (is_byte
|| is_int
)
3062 if (val
->string_constant_value(&s
))
3064 Expression_list
* vals
= new Expression_list();
3067 for (std::string::const_iterator p
= s
.begin();
3072 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
3073 Expression
* v
= Expression::make_integer(&val
,
3082 const char *p
= s
.data();
3083 const char *pend
= s
.data() + s
.length();
3087 int adv
= Lex::fetch_char(p
, &c
);
3090 warning_at(this->location(), 0,
3091 "invalid UTF-8 encoding");
3096 mpz_init_set_ui(val
, c
);
3097 Expression
* v
= Expression::make_integer(&val
,
3105 return Expression::make_slice_composite_literal(type
, vals
,
3114 // Return the constant integer value if there is one.
3117 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
3121 if (this->type_
->integer_type() == NULL
)
3127 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
3129 if (!Integer_expression::check_constant(ival
, this->type_
,
3137 *ptype
= this->type_
;
3144 if (this->expr_
->float_constant_value(fval
, &dummy
))
3146 mpfr_get_z(val
, fval
, GMP_RNDN
);
3148 if (!Integer_expression::check_constant(val
, this->type_
,
3151 *ptype
= this->type_
;
3159 // Return the constant floating point value if there is one.
3162 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3165 if (this->type_
->float_type() == NULL
)
3171 if (this->expr_
->float_constant_value(fval
, &dummy
))
3173 if (!Float_expression::check_constant(fval
, this->type_
,
3179 mpfr_set(val
, fval
, GMP_RNDN
);
3181 Float_expression::constrain_float(val
, this->type_
);
3182 *ptype
= this->type_
;
3190 // Return the constant complex value if there is one.
3193 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3197 if (this->type_
->complex_type() == NULL
)
3205 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3207 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3214 mpfr_set(real
, rval
, GMP_RNDN
);
3215 mpfr_set(imag
, ival
, GMP_RNDN
);
3218 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3219 *ptype
= this->type_
;
3228 // Return the constant string value if there is one.
3231 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3233 if (this->type_
->is_string_type()
3234 && this->expr_
->type()->integer_type() != NULL
)
3239 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3241 unsigned long ulval
= mpz_get_ui(ival
);
3242 if (mpz_cmp_ui(ival
, ulval
) == 0)
3244 Lex::append_char(ulval
, true, val
, this->location());
3252 // FIXME: Could handle conversion from const []int here.
3257 // Check that types are convertible.
3260 Type_conversion_expression::do_check_types(Gogo
*)
3262 Type
* type
= this->type_
;
3263 Type
* expr_type
= this->expr_
->type();
3266 if (type
->is_error_type()
3267 || type
->is_undefined()
3268 || expr_type
->is_error_type()
3269 || expr_type
->is_undefined())
3271 // Make sure we emit an error for an undefined type.
3274 this->set_is_error();
3278 if (this->may_convert_function_types_
3279 && type
->function_type() != NULL
3280 && expr_type
->function_type() != NULL
)
3283 if (Type::are_convertible(type
, expr_type
, &reason
))
3286 error_at(this->location(), "%s", reason
.c_str());
3287 this->set_is_error();
3290 // Get a tree for a type conversion.
3293 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3295 Gogo
* gogo
= context
->gogo();
3296 tree type_tree
= this->type_
->get_tree(gogo
);
3297 tree expr_tree
= this->expr_
->get_tree(context
);
3299 if (type_tree
== error_mark_node
3300 || expr_tree
== error_mark_node
3301 || TREE_TYPE(expr_tree
) == error_mark_node
)
3302 return error_mark_node
;
3304 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3305 return fold_convert(type_tree
, expr_tree
);
3307 Type
* type
= this->type_
;
3308 Type
* expr_type
= this->expr_
->type();
3310 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3311 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3312 expr_tree
, this->location());
3313 else if (type
->integer_type() != NULL
)
3315 if (expr_type
->integer_type() != NULL
3316 || expr_type
->float_type() != NULL
3317 || expr_type
->is_unsafe_pointer_type())
3318 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3322 else if (type
->float_type() != NULL
)
3324 if (expr_type
->integer_type() != NULL
3325 || expr_type
->float_type() != NULL
)
3326 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3330 else if (type
->complex_type() != NULL
)
3332 if (expr_type
->complex_type() != NULL
)
3333 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3337 else if (type
->is_string_type()
3338 && expr_type
->integer_type() != NULL
)
3340 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3341 if (host_integerp(expr_tree
, 0))
3343 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3345 Lex::append_char(intval
, true, &s
, this->location());
3346 Expression
* se
= Expression::make_string(s
, this->location());
3347 return se
->get_tree(context
);
3350 static tree int_to_string_fndecl
;
3351 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3353 "__go_int_to_string",
3357 fold_convert(integer_type_node
, expr_tree
));
3359 else if (type
->is_string_type()
3360 && (expr_type
->array_type() != NULL
3361 || (expr_type
->points_to() != NULL
3362 && expr_type
->points_to()->array_type() != NULL
)))
3364 Type
* t
= expr_type
;
3365 if (t
->points_to() != NULL
)
3368 expr_tree
= build_fold_indirect_ref(expr_tree
);
3370 if (!DECL_P(expr_tree
))
3371 expr_tree
= save_expr(expr_tree
);
3372 Array_type
* a
= t
->array_type();
3373 Type
* e
= a
->element_type()->forwarded();
3374 gcc_assert(e
->integer_type() != NULL
);
3375 tree valptr
= fold_convert(const_ptr_type_node
,
3376 a
->value_pointer_tree(gogo
, expr_tree
));
3377 tree len
= a
->length_tree(gogo
, expr_tree
);
3378 len
= fold_convert_loc(this->location(), size_type_node
, len
);
3379 if (e
->integer_type()->is_unsigned()
3380 && e
->integer_type()->bits() == 8)
3382 static tree byte_array_to_string_fndecl
;
3383 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3385 "__go_byte_array_to_string",
3388 const_ptr_type_node
,
3395 gcc_assert(e
== Type::lookup_integer_type("int"));
3396 static tree int_array_to_string_fndecl
;
3397 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3399 "__go_int_array_to_string",
3402 const_ptr_type_node
,
3408 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3410 Type
* e
= type
->array_type()->element_type()->forwarded();
3411 gcc_assert(e
->integer_type() != NULL
);
3412 if (e
->integer_type()->is_unsigned()
3413 && e
->integer_type()->bits() == 8)
3415 static tree string_to_byte_array_fndecl
;
3416 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3418 "__go_string_to_byte_array",
3421 TREE_TYPE(expr_tree
),
3426 gcc_assert(e
== Type::lookup_integer_type("int"));
3427 static tree string_to_int_array_fndecl
;
3428 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3430 "__go_string_to_int_array",
3433 TREE_TYPE(expr_tree
),
3437 else if ((type
->is_unsafe_pointer_type()
3438 && expr_type
->points_to() != NULL
)
3439 || (expr_type
->is_unsafe_pointer_type()
3440 && type
->points_to() != NULL
))
3441 ret
= fold_convert(type_tree
, expr_tree
);
3442 else if (type
->is_unsafe_pointer_type()
3443 && expr_type
->integer_type() != NULL
)
3444 ret
= convert_to_pointer(type_tree
, expr_tree
);
3445 else if (this->may_convert_function_types_
3446 && type
->function_type() != NULL
3447 && expr_type
->function_type() != NULL
)
3448 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3450 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3451 expr_tree
, this->location());
3456 // Output a type conversion in a constant expression.
3459 Type_conversion_expression::do_export(Export
* exp
) const
3461 exp
->write_c_string("convert(");
3462 exp
->write_type(this->type_
);
3463 exp
->write_c_string(", ");
3464 this->expr_
->export_expression(exp
);
3465 exp
->write_c_string(")");
3468 // Import a type conversion or a struct construction.
3471 Type_conversion_expression::do_import(Import
* imp
)
3473 imp
->require_c_string("convert(");
3474 Type
* type
= imp
->read_type();
3475 imp
->require_c_string(", ");
3476 Expression
* val
= Expression::import_expression(imp
);
3477 imp
->require_c_string(")");
3478 return Expression::make_cast(type
, val
, imp
->location());
3481 // Make a type cast expression.
3484 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3486 if (type
->is_error_type() || val
->is_error_expression())
3487 return Expression::make_error(location
);
3488 return new Type_conversion_expression(type
, val
, location
);
3491 // Unary expressions.
3493 class Unary_expression
: public Expression
3496 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3497 : Expression(EXPRESSION_UNARY
, location
),
3498 op_(op
), escapes_(true), expr_(expr
)
3501 // Return the operator.
3504 { return this->op_
; }
3506 // Return the operand.
3509 { return this->expr_
; }
3511 // Record that an address expression does not escape.
3513 set_does_not_escape()
3515 gcc_assert(this->op_
== OPERATOR_AND
);
3516 this->escapes_
= false;
3519 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3520 // could be done, false if not.
3522 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3525 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3526 // could be done, false if not.
3528 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3530 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3531 // true if this could be done, false if not.
3533 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3541 do_traverse(Traverse
* traverse
)
3542 { return Expression::traverse(&this->expr_
, traverse
); }
3545 do_lower(Gogo
*, Named_object
*, int);
3548 do_is_constant() const;
3551 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3554 do_float_constant_value(mpfr_t
, Type
**) const;
3557 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3563 do_determine_type(const Type_context
*);
3566 do_check_types(Gogo
*);
3571 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3576 do_is_addressable() const
3577 { return this->op_
== OPERATOR_MULT
; }
3580 do_get_tree(Translate_context
*);
3583 do_export(Export
*) const;
3586 // The unary operator to apply.
3588 // Normally true. False if this is an address expression which does
3589 // not escape the current function.
3595 // If we are taking the address of a composite literal, and the
3596 // contents are not constant, then we want to make a heap composite
3600 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3602 source_location loc
= this->location();
3603 Operator op
= this->op_
;
3604 Expression
* expr
= this->expr_
;
3606 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3607 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3609 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3610 // moving x to the heap. FIXME: Is it worth doing a real escape
3611 // analysis here? This case is found in math/unsafe.go and is
3612 // therefore worth special casing.
3613 if (op
== OPERATOR_MULT
)
3615 Expression
* e
= expr
;
3616 while (e
->classification() == EXPRESSION_CONVERSION
)
3618 Type_conversion_expression
* te
3619 = static_cast<Type_conversion_expression
*>(e
);
3623 if (e
->classification() == EXPRESSION_UNARY
)
3625 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3626 if (ue
->op_
== OPERATOR_AND
)
3633 ue
->set_does_not_escape();
3638 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3639 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3641 Expression
* ret
= NULL
;
3646 if (expr
->integer_constant_value(false, eval
, &etype
))
3650 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3651 ret
= Expression::make_integer(&val
, etype
, loc
);
3658 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3663 if (expr
->float_constant_value(fval
, &ftype
))
3667 if (Unary_expression::eval_float(op
, fval
, val
))
3668 ret
= Expression::make_float(&val
, ftype
, loc
);
3679 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3685 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3686 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3700 // Return whether a unary expression is a constant.
3703 Unary_expression::do_is_constant() const
3705 if (this->op_
== OPERATOR_MULT
)
3707 // Indirecting through a pointer is only constant if the object
3708 // to which the expression points is constant, but we currently
3709 // have no way to determine that.
3712 else if (this->op_
== OPERATOR_AND
)
3714 // Taking the address of a variable is constant if it is a
3715 // global variable, not constant otherwise. In other cases
3716 // taking the address is probably not a constant.
3717 Var_expression
* ve
= this->expr_
->var_expression();
3720 Named_object
* no
= ve
->named_object();
3721 return no
->is_variable() && no
->var_value()->is_global();
3726 return this->expr_
->is_constant();
3729 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3730 // UVAL, if known; it may be NULL. Return true if this could be done,
3734 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3735 source_location location
)
3742 case OPERATOR_MINUS
:
3744 return Integer_expression::check_constant(val
, utype
, location
);
3746 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3750 || utype
->integer_type() == NULL
3751 || utype
->integer_type()->is_abstract())
3755 // The number of HOST_WIDE_INTs that it takes to represent
3757 size_t count
= ((mpz_sizeinbase(uval
, 2)
3758 + HOST_BITS_PER_WIDE_INT
3760 / HOST_BITS_PER_WIDE_INT
);
3762 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3763 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3766 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3767 gcc_assert(ecount
<= count
);
3769 // Trim down to the number of words required by the type.
3770 size_t obits
= utype
->integer_type()->bits();
3771 if (!utype
->integer_type()->is_unsigned())
3773 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3774 / HOST_BITS_PER_WIDE_INT
);
3775 gcc_assert(ocount
<= ocount
);
3777 for (size_t i
= 0; i
< ocount
; ++i
)
3780 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3782 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3785 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3789 return Integer_expression::check_constant(val
, utype
, location
);
3798 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3799 // could be done, false if not.
3802 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3807 mpfr_set(val
, uval
, GMP_RNDN
);
3809 case OPERATOR_MINUS
:
3810 mpfr_neg(val
, uval
, GMP_RNDN
);
3822 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3823 // if this could be done, false if not.
3826 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
3827 mpfr_t real
, mpfr_t imag
)
3832 mpfr_set(real
, rval
, GMP_RNDN
);
3833 mpfr_set(imag
, ival
, GMP_RNDN
);
3835 case OPERATOR_MINUS
:
3836 mpfr_neg(real
, rval
, GMP_RNDN
);
3837 mpfr_neg(imag
, ival
, GMP_RNDN
);
3849 // Return the integral constant value of a unary expression, if it has one.
3852 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
3858 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
3861 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
3867 // Return the floating point constant value of a unary expression, if
3871 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
3876 if (!this->expr_
->float_constant_value(uval
, ptype
))
3879 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
3884 // Return the complex constant value of a unary expression, if it has
3888 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
3896 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
3899 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
3905 // Return the type of a unary expression.
3908 Unary_expression::do_type()
3913 case OPERATOR_MINUS
:
3916 return this->expr_
->type();
3919 return Type::make_pointer_type(this->expr_
->type());
3923 Type
* subtype
= this->expr_
->type();
3924 Type
* points_to
= subtype
->points_to();
3925 if (points_to
== NULL
)
3926 return Type::make_error_type();
3935 // Determine abstract types for a unary expression.
3938 Unary_expression::do_determine_type(const Type_context
* context
)
3943 case OPERATOR_MINUS
:
3946 this->expr_
->determine_type(context
);
3950 // Taking the address of something.
3952 Type
* subtype
= (context
->type
== NULL
3954 : context
->type
->points_to());
3955 Type_context
subcontext(subtype
, false);
3956 this->expr_
->determine_type(&subcontext
);
3961 // Indirecting through a pointer.
3963 Type
* subtype
= (context
->type
== NULL
3965 : Type::make_pointer_type(context
->type
));
3966 Type_context
subcontext(subtype
, false);
3967 this->expr_
->determine_type(&subcontext
);
3976 // Check types for a unary expression.
3979 Unary_expression::do_check_types(Gogo
*)
3981 Type
* type
= this->expr_
->type();
3982 if (type
->is_error_type())
3984 this->set_is_error();
3991 case OPERATOR_MINUS
:
3992 if (type
->integer_type() == NULL
3993 && type
->float_type() == NULL
3994 && type
->complex_type() == NULL
)
3995 this->report_error(_("expected numeric type"));
4000 if (type
->integer_type() == NULL
4001 && !type
->is_boolean_type())
4002 this->report_error(_("expected integer or boolean type"));
4006 if (!this->expr_
->is_addressable())
4007 this->report_error(_("invalid operand for unary %<&%>"));
4009 this->expr_
->address_taken(this->escapes_
);
4013 // Indirecting through a pointer.
4014 if (type
->points_to() == NULL
)
4015 this->report_error(_("expected pointer"));
4023 // Get a tree for a unary expression.
4026 Unary_expression::do_get_tree(Translate_context
* context
)
4028 tree expr
= this->expr_
->get_tree(context
);
4029 if (expr
== error_mark_node
)
4030 return error_mark_node
;
4032 source_location loc
= this->location();
4038 case OPERATOR_MINUS
:
4040 tree type
= TREE_TYPE(expr
);
4041 tree compute_type
= excess_precision_type(type
);
4042 if (compute_type
!= NULL_TREE
)
4043 expr
= ::convert(compute_type
, expr
);
4044 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
4045 (compute_type
!= NULL_TREE
4049 if (compute_type
!= NULL_TREE
)
4050 ret
= ::convert(type
, ret
);
4055 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
4056 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4058 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
4059 build_int_cst(TREE_TYPE(expr
), 0));
4062 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4065 // We should not see a non-constant constructor here; cases
4066 // where we would see one should have been moved onto the heap
4067 // at parse time. Taking the address of a nonconstant
4068 // constructor will not do what the programmer expects.
4069 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
4070 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
4072 // Build a decl for a constant constructor.
4073 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
4075 tree decl
= build_decl(this->location(), VAR_DECL
,
4076 create_tmp_var_name("C"), TREE_TYPE(expr
));
4077 DECL_EXTERNAL(decl
) = 0;
4078 TREE_PUBLIC(decl
) = 0;
4079 TREE_READONLY(decl
) = 1;
4080 TREE_CONSTANT(decl
) = 1;
4081 TREE_STATIC(decl
) = 1;
4082 TREE_ADDRESSABLE(decl
) = 1;
4083 DECL_ARTIFICIAL(decl
) = 1;
4084 DECL_INITIAL(decl
) = expr
;
4085 rest_of_decl_compilation(decl
, 1, 0);
4089 return build_fold_addr_expr_loc(loc
, expr
);
4093 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
4095 // If we are dereferencing the pointer to a large struct, we
4096 // need to check for nil. We don't bother to check for small
4097 // structs because we expect the system to crash on a nil
4098 // pointer dereference.
4099 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
4100 if (s
== -1 || s
>= 4096)
4103 expr
= save_expr(expr
);
4104 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
4106 fold_convert(TREE_TYPE(expr
),
4107 null_pointer_node
));
4108 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4110 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
4111 build3(COND_EXPR
, void_type_node
,
4112 compare
, crash
, NULL_TREE
),
4116 // If the type of EXPR is a recursive pointer type, then we
4117 // need to insert a cast before indirecting.
4118 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
4120 Type
* pt
= this->expr_
->type()->points_to();
4121 tree ind
= pt
->get_tree(context
->gogo());
4122 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
4125 return build_fold_indirect_ref_loc(loc
, expr
);
4133 // Export a unary expression.
4136 Unary_expression::do_export(Export
* exp
) const
4141 exp
->write_c_string("+ ");
4143 case OPERATOR_MINUS
:
4144 exp
->write_c_string("- ");
4147 exp
->write_c_string("! ");
4150 exp
->write_c_string("^ ");
4157 this->expr_
->export_expression(exp
);
4160 // Import a unary expression.
4163 Unary_expression::do_import(Import
* imp
)
4166 switch (imp
->get_char())
4172 op
= OPERATOR_MINUS
;
4183 imp
->require_c_string(" ");
4184 Expression
* expr
= Expression::import_expression(imp
);
4185 return Expression::make_unary(op
, expr
, imp
->location());
4188 // Make a unary expression.
4191 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4193 return new Unary_expression(op
, expr
, location
);
4196 // If this is an indirection through a pointer, return the expression
4197 // being pointed through. Otherwise return this.
4202 if (this->classification_
== EXPRESSION_UNARY
)
4204 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4205 if (ue
->op() == OPERATOR_MULT
)
4206 return ue
->operand();
4211 // Class Binary_expression.
4216 Binary_expression::do_traverse(Traverse
* traverse
)
4218 int t
= Expression::traverse(&this->left_
, traverse
);
4219 if (t
== TRAVERSE_EXIT
)
4220 return TRAVERSE_EXIT
;
4221 return Expression::traverse(&this->right_
, traverse
);
4224 // Compare integer constants according to OP.
4227 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4230 int i
= mpz_cmp(left_val
, right_val
);
4235 case OPERATOR_NOTEQ
:
4250 // Compare floating point constants according to OP.
4253 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4258 i
= mpfr_cmp(left_val
, right_val
);
4262 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4264 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4265 Float_expression::constrain_float(lv
, type
);
4266 Float_expression::constrain_float(rv
, type
);
4267 i
= mpfr_cmp(lv
, rv
);
4275 case OPERATOR_NOTEQ
:
4290 // Compare complex constants according to OP. Complex numbers may
4291 // only be compared for equality.
4294 Binary_expression::compare_complex(Operator op
, Type
* type
,
4295 mpfr_t left_real
, mpfr_t left_imag
,
4296 mpfr_t right_real
, mpfr_t right_imag
)
4300 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4301 && mpfr_cmp(left_imag
, right_imag
) == 0);
4306 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4307 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4310 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4311 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4312 Complex_expression::constrain_complex(lr
, li
, type
);
4313 Complex_expression::constrain_complex(rr
, ri
, type
);
4314 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4324 case OPERATOR_NOTEQ
:
4331 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4332 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4333 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4334 // this could be done, false if not.
4337 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4338 Type
* right_type
, mpz_t right_val
,
4339 source_location location
, mpz_t val
)
4341 bool is_shift_op
= false;
4345 case OPERATOR_ANDAND
:
4347 case OPERATOR_NOTEQ
:
4352 // These return boolean values. We should probably handle them
4353 // anyhow in case a type conversion is used on the result.
4356 mpz_add(val
, left_val
, right_val
);
4358 case OPERATOR_MINUS
:
4359 mpz_sub(val
, left_val
, right_val
);
4362 mpz_ior(val
, left_val
, right_val
);
4365 mpz_xor(val
, left_val
, right_val
);
4368 mpz_mul(val
, left_val
, right_val
);
4371 if (mpz_sgn(right_val
) != 0)
4372 mpz_tdiv_q(val
, left_val
, right_val
);
4375 error_at(location
, "division by zero");
4381 if (mpz_sgn(right_val
) != 0)
4382 mpz_tdiv_r(val
, left_val
, right_val
);
4385 error_at(location
, "division by zero");
4390 case OPERATOR_LSHIFT
:
4392 unsigned long shift
= mpz_get_ui(right_val
);
4393 if (mpz_cmp_ui(right_val
, shift
) != 0)
4395 error_at(location
, "shift count overflow");
4399 mpz_mul_2exp(val
, left_val
, shift
);
4404 case OPERATOR_RSHIFT
:
4406 unsigned long shift
= mpz_get_ui(right_val
);
4407 if (mpz_cmp_ui(right_val
, shift
) != 0)
4409 error_at(location
, "shift count overflow");
4413 if (mpz_cmp_ui(left_val
, 0) >= 0)
4414 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4416 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4422 mpz_and(val
, left_val
, right_val
);
4424 case OPERATOR_BITCLEAR
:
4428 mpz_com(tval
, right_val
);
4429 mpz_and(val
, left_val
, tval
);
4437 Type
* type
= left_type
;
4442 else if (type
!= right_type
&& right_type
!= NULL
)
4444 if (type
->is_abstract())
4446 else if (!right_type
->is_abstract())
4448 // This look like a type error which should be diagnosed
4449 // elsewhere. Don't do anything here, to avoid an
4450 // unhelpful chain of error messages.
4456 if (type
!= NULL
&& !type
->is_abstract())
4458 // We have to check the operands too, as we have implicitly
4459 // coerced them to TYPE.
4460 if ((type
!= left_type
4461 && !Integer_expression::check_constant(left_val
, type
, location
))
4463 && type
!= right_type
4464 && !Integer_expression::check_constant(right_val
, type
,
4466 || !Integer_expression::check_constant(val
, type
, location
))
4473 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4474 // Return true if this could be done, false if not.
4477 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4478 Type
* right_type
, mpfr_t right_val
,
4479 mpfr_t val
, source_location location
)
4484 case OPERATOR_ANDAND
:
4486 case OPERATOR_NOTEQ
:
4491 // These return boolean values. We should probably handle them
4492 // anyhow in case a type conversion is used on the result.
4495 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4497 case OPERATOR_MINUS
:
4498 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4503 case OPERATOR_BITCLEAR
:
4506 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4509 if (mpfr_zero_p(right_val
))
4510 error_at(location
, "division by zero");
4511 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4515 case OPERATOR_LSHIFT
:
4516 case OPERATOR_RSHIFT
:
4522 Type
* type
= left_type
;
4525 else if (type
!= right_type
&& right_type
!= NULL
)
4527 if (type
->is_abstract())
4529 else if (!right_type
->is_abstract())
4531 // This looks like a type error which should be diagnosed
4532 // elsewhere. Don't do anything here, to avoid an unhelpful
4533 // chain of error messages.
4538 if (type
!= NULL
&& !type
->is_abstract())
4540 if ((type
!= left_type
4541 && !Float_expression::check_constant(left_val
, type
, location
))
4542 || (type
!= right_type
4543 && !Float_expression::check_constant(right_val
, type
,
4545 || !Float_expression::check_constant(val
, type
, location
))
4546 mpfr_set_ui(val
, 0, GMP_RNDN
);
4552 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4553 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4554 // could be done, false if not.
4557 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4558 mpfr_t left_real
, mpfr_t left_imag
,
4560 mpfr_t right_real
, mpfr_t right_imag
,
4561 mpfr_t real
, mpfr_t imag
,
4562 source_location location
)
4567 case OPERATOR_ANDAND
:
4569 case OPERATOR_NOTEQ
:
4574 // These return boolean values and must be handled differently.
4577 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4578 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4580 case OPERATOR_MINUS
:
4581 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4582 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4587 case OPERATOR_BITCLEAR
:
4591 // You might think that multiplying two complex numbers would
4592 // be simple, and you would be right, until you start to think
4593 // about getting the right answer for infinity. If one
4594 // operand here is infinity and the other is anything other
4595 // than zero or NaN, then we are going to wind up subtracting
4596 // two infinity values. That will give us a NaN, but the
4597 // correct answer is infinity.
4601 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4605 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4609 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4613 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4615 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4616 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4618 // If we get NaN on both sides, check whether it should really
4619 // be infinity. The rule is that if either side of the
4620 // complex number is infinity, then the whole value is
4621 // infinity, even if the other side is NaN. So the only case
4622 // we have to fix is the one in which both sides are NaN.
4623 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4624 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4625 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4627 bool is_infinity
= false;
4631 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4632 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4636 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4637 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4639 // If the left side is infinity, then the result is
4641 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4643 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4644 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4645 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4646 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4649 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4650 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4654 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4655 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4660 // If the right side is infinity, then the result is
4662 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4664 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4665 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4666 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4667 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4670 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4671 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4675 mpfr_set_ui(li
, 0, GMP_RNDN
);
4676 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4681 // If we got an overflow in the intermediate computations,
4682 // then the result is infinity.
4684 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4685 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4689 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4690 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4694 mpfr_set_ui(li
, 0, GMP_RNDN
);
4695 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4699 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4700 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4704 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4705 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4712 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4713 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4714 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4715 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4716 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4717 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4718 mpfr_set_inf(real
, mpfr_sgn(real
));
4719 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4736 // For complex division we want to avoid having an
4737 // intermediate overflow turn the whole result in a NaN. We
4738 // scale the values to try to avoid this.
4740 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4741 error_at(location
, "division by zero");
4747 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4748 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4751 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4755 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4756 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4758 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4760 ilogbw
= mpfr_get_exp(t
);
4761 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4762 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4767 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4768 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4769 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4771 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4772 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4773 mpfr_add(real
, real
, t
, GMP_RNDN
);
4774 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4775 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4777 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4778 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4779 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4780 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4781 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4783 // If we wind up with NaN on both sides, check whether we
4784 // should really have infinity. The rule is that if either
4785 // side of the complex number is infinity, then the whole
4786 // value is infinity, even if the other side is NaN. So the
4787 // only case we have to fix is the one in which both sides are
4789 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4790 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4791 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4793 if (mpfr_zero_p(denom
))
4795 mpfr_set_inf(real
, mpfr_sgn(rr
));
4796 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4797 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4798 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4800 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4801 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4803 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4804 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4807 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4808 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4812 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4816 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4818 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4819 mpfr_set_inf(real
, mpfr_sgn(t3
));
4821 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4822 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4823 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4824 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4830 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4831 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4833 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4834 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4837 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4838 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4842 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
4846 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
4848 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4849 mpfr_set_ui(real
, 0, GMP_RNDN
);
4850 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
4852 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
4853 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
4854 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4855 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4856 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
4874 case OPERATOR_LSHIFT
:
4875 case OPERATOR_RSHIFT
:
4881 Type
* type
= left_type
;
4884 else if (type
!= right_type
&& right_type
!= NULL
)
4886 if (type
->is_abstract())
4888 else if (!right_type
->is_abstract())
4890 // This looks like a type error which should be diagnosed
4891 // elsewhere. Don't do anything here, to avoid an unhelpful
4892 // chain of error messages.
4897 if (type
!= NULL
&& !type
->is_abstract())
4899 if ((type
!= left_type
4900 && !Complex_expression::check_constant(left_real
, left_imag
,
4902 || (type
!= right_type
4903 && !Complex_expression::check_constant(right_real
, right_imag
,
4905 || !Complex_expression::check_constant(real
, imag
, type
,
4908 mpfr_set_ui(real
, 0, GMP_RNDN
);
4909 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4916 // Lower a binary expression. We have to evaluate constant
4917 // expressions now, in order to implement Go's unlimited precision
4921 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
4923 source_location location
= this->location();
4924 Operator op
= this->op_
;
4925 Expression
* left
= this->left_
;
4926 Expression
* right
= this->right_
;
4928 const bool is_comparison
= (op
== OPERATOR_EQEQ
4929 || op
== OPERATOR_NOTEQ
4930 || op
== OPERATOR_LT
4931 || op
== OPERATOR_LE
4932 || op
== OPERATOR_GT
4933 || op
== OPERATOR_GE
);
4935 // Integer constant expressions.
4941 mpz_init(right_val
);
4943 if (left
->integer_constant_value(false, left_val
, &left_type
)
4944 && right
->integer_constant_value(false, right_val
, &right_type
))
4946 Expression
* ret
= NULL
;
4947 if (left_type
!= right_type
4948 && left_type
!= NULL
4949 && right_type
!= NULL
4950 && left_type
->base() != right_type
->base()
4951 && op
!= OPERATOR_LSHIFT
4952 && op
!= OPERATOR_RSHIFT
)
4954 // May be a type error--let it be diagnosed later.
4956 else if (is_comparison
)
4958 bool b
= Binary_expression::compare_integer(op
, left_val
,
4960 ret
= Expression::make_cast(Type::lookup_bool_type(),
4961 Expression::make_boolean(b
, location
),
4969 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
4970 right_type
, right_val
,
4973 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
4975 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4977 else if (left_type
== NULL
)
4979 else if (right_type
== NULL
)
4981 else if (!left_type
->is_abstract()
4982 && left_type
->named_type() != NULL
)
4984 else if (!right_type
->is_abstract()
4985 && right_type
->named_type() != NULL
)
4987 else if (!left_type
->is_abstract())
4989 else if (!right_type
->is_abstract())
4991 else if (left_type
->float_type() != NULL
)
4993 else if (right_type
->float_type() != NULL
)
4995 else if (left_type
->complex_type() != NULL
)
4997 else if (right_type
->complex_type() != NULL
)
5001 ret
= Expression::make_integer(&val
, type
, location
);
5009 mpz_clear(right_val
);
5010 mpz_clear(left_val
);
5014 mpz_clear(right_val
);
5015 mpz_clear(left_val
);
5018 // Floating point constant expressions.
5021 mpfr_init(left_val
);
5024 mpfr_init(right_val
);
5026 if (left
->float_constant_value(left_val
, &left_type
)
5027 && right
->float_constant_value(right_val
, &right_type
))
5029 Expression
* ret
= NULL
;
5030 if (left_type
!= right_type
5031 && left_type
!= NULL
5032 && right_type
!= NULL
5033 && left_type
->base() != right_type
->base()
5034 && op
!= OPERATOR_LSHIFT
5035 && op
!= OPERATOR_RSHIFT
)
5037 // May be a type error--let it be diagnosed later.
5039 else if (is_comparison
)
5041 bool b
= Binary_expression::compare_float(op
,
5045 left_val
, right_val
);
5046 ret
= Expression::make_boolean(b
, location
);
5053 if (Binary_expression::eval_float(op
, left_type
, left_val
,
5054 right_type
, right_val
, val
,
5057 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5058 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5060 if (left_type
== NULL
)
5062 else if (right_type
== NULL
)
5064 else if (!left_type
->is_abstract()
5065 && left_type
->named_type() != NULL
)
5067 else if (!right_type
->is_abstract()
5068 && right_type
->named_type() != NULL
)
5070 else if (!left_type
->is_abstract())
5072 else if (!right_type
->is_abstract())
5074 else if (left_type
->float_type() != NULL
)
5076 else if (right_type
->float_type() != NULL
)
5080 ret
= Expression::make_float(&val
, type
, location
);
5088 mpfr_clear(right_val
);
5089 mpfr_clear(left_val
);
5093 mpfr_clear(right_val
);
5094 mpfr_clear(left_val
);
5097 // Complex constant expressions.
5101 mpfr_init(left_real
);
5102 mpfr_init(left_imag
);
5107 mpfr_init(right_real
);
5108 mpfr_init(right_imag
);
5111 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
5112 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
5114 Expression
* ret
= NULL
;
5115 if (left_type
!= right_type
5116 && left_type
!= NULL
5117 && right_type
!= NULL
5118 && left_type
->base() != right_type
->base())
5120 // May be a type error--let it be diagnosed later.
5122 else if (is_comparison
)
5124 bool b
= Binary_expression::compare_complex(op
,
5132 ret
= Expression::make_boolean(b
, location
);
5141 if (Binary_expression::eval_complex(op
, left_type
,
5142 left_real
, left_imag
,
5144 right_real
, right_imag
,
5148 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5149 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5151 if (left_type
== NULL
)
5153 else if (right_type
== NULL
)
5155 else if (!left_type
->is_abstract()
5156 && left_type
->named_type() != NULL
)
5158 else if (!right_type
->is_abstract()
5159 && right_type
->named_type() != NULL
)
5161 else if (!left_type
->is_abstract())
5163 else if (!right_type
->is_abstract())
5165 else if (left_type
->complex_type() != NULL
)
5167 else if (right_type
->complex_type() != NULL
)
5171 ret
= Expression::make_complex(&real
, &imag
, type
,
5180 mpfr_clear(left_real
);
5181 mpfr_clear(left_imag
);
5182 mpfr_clear(right_real
);
5183 mpfr_clear(right_imag
);
5188 mpfr_clear(left_real
);
5189 mpfr_clear(left_imag
);
5190 mpfr_clear(right_real
);
5191 mpfr_clear(right_imag
);
5194 // String constant expressions.
5195 if (op
== OPERATOR_PLUS
5196 && left
->type()->is_string_type()
5197 && right
->type()->is_string_type())
5199 std::string left_string
;
5200 std::string right_string
;
5201 if (left
->string_constant_value(&left_string
)
5202 && right
->string_constant_value(&right_string
))
5203 return Expression::make_string(left_string
+ right_string
, location
);
5209 // Return the integer constant value, if it has one.
5212 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5218 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5221 mpz_clear(left_val
);
5226 mpz_init(right_val
);
5228 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5231 mpz_clear(right_val
);
5232 mpz_clear(left_val
);
5237 if (left_type
!= right_type
5238 && left_type
!= NULL
5239 && right_type
!= NULL
5240 && left_type
->base() != right_type
->base()
5241 && this->op_
!= OPERATOR_RSHIFT
5242 && this->op_
!= OPERATOR_LSHIFT
)
5245 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5246 right_type
, right_val
,
5247 this->location(), val
);
5249 mpz_clear(right_val
);
5250 mpz_clear(left_val
);
5258 // Return the floating point constant value, if it has one.
5261 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5264 mpfr_init(left_val
);
5266 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5268 mpfr_clear(left_val
);
5273 mpfr_init(right_val
);
5275 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5277 mpfr_clear(right_val
);
5278 mpfr_clear(left_val
);
5283 if (left_type
!= right_type
5284 && left_type
!= NULL
5285 && right_type
!= NULL
5286 && left_type
->base() != right_type
->base())
5289 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5290 right_type
, right_val
,
5291 val
, this->location());
5293 mpfr_clear(left_val
);
5294 mpfr_clear(right_val
);
5302 // Return the complex constant value, if it has one.
5305 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5310 mpfr_init(left_real
);
5311 mpfr_init(left_imag
);
5313 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5315 mpfr_clear(left_real
);
5316 mpfr_clear(left_imag
);
5322 mpfr_init(right_real
);
5323 mpfr_init(right_imag
);
5325 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5328 mpfr_clear(left_real
);
5329 mpfr_clear(left_imag
);
5330 mpfr_clear(right_real
);
5331 mpfr_clear(right_imag
);
5336 if (left_type
!= right_type
5337 && left_type
!= NULL
5338 && right_type
!= NULL
5339 && left_type
->base() != right_type
->base())
5342 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5343 left_real
, left_imag
,
5345 right_real
, right_imag
,
5348 mpfr_clear(left_real
);
5349 mpfr_clear(left_imag
);
5350 mpfr_clear(right_real
);
5351 mpfr_clear(right_imag
);
5359 // Note that the value is being discarded.
5362 Binary_expression::do_discarding_value()
5364 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5365 this->right_
->discarding_value();
5367 this->warn_about_unused_value();
5373 Binary_expression::do_type()
5378 case OPERATOR_ANDAND
:
5380 case OPERATOR_NOTEQ
:
5385 return Type::lookup_bool_type();
5388 case OPERATOR_MINUS
:
5395 case OPERATOR_BITCLEAR
:
5397 Type
* left_type
= this->left_
->type();
5398 Type
* right_type
= this->right_
->type();
5399 if (left_type
->is_error_type())
5401 else if (right_type
->is_error_type())
5403 else if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5405 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5407 else if (!left_type
->is_abstract())
5409 else if (!right_type
->is_abstract())
5411 else if (left_type
->complex_type() != NULL
)
5413 else if (right_type
->complex_type() != NULL
)
5415 else if (left_type
->float_type() != NULL
)
5417 else if (right_type
->float_type() != NULL
)
5423 case OPERATOR_LSHIFT
:
5424 case OPERATOR_RSHIFT
:
5425 return this->left_
->type();
5432 // Set type for a binary expression.
5435 Binary_expression::do_determine_type(const Type_context
* context
)
5437 Type
* tleft
= this->left_
->type();
5438 Type
* tright
= this->right_
->type();
5440 // Both sides should have the same type, except for the shift
5441 // operations. For a comparison, we should ignore the incoming
5444 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5445 || this->op_
== OPERATOR_RSHIFT
);
5447 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5448 || this->op_
== OPERATOR_NOTEQ
5449 || this->op_
== OPERATOR_LT
5450 || this->op_
== OPERATOR_LE
5451 || this->op_
== OPERATOR_GT
5452 || this->op_
== OPERATOR_GE
);
5454 Type_context
subcontext(*context
);
5458 // In a comparison, the context does not determine the types of
5460 subcontext
.type
= NULL
;
5463 // Set the context for the left hand operand.
5466 // The right hand operand plays no role in determining the type
5467 // of the left hand operand. A shift of an abstract integer in
5468 // a string context gets special treatment, which may be a
5470 if (subcontext
.type
!= NULL
5471 && subcontext
.type
->is_string_type()
5472 && tleft
->is_abstract())
5473 error_at(this->location(), "shift of non-integer operand");
5475 else if (!tleft
->is_abstract())
5476 subcontext
.type
= tleft
;
5477 else if (!tright
->is_abstract())
5478 subcontext
.type
= tright
;
5479 else if (subcontext
.type
== NULL
)
5481 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5482 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5483 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5485 // Both sides have an abstract integer, abstract float, or
5486 // abstract complex type. Just let CONTEXT determine
5487 // whether they may remain abstract or not.
5489 else if (tleft
->complex_type() != NULL
)
5490 subcontext
.type
= tleft
;
5491 else if (tright
->complex_type() != NULL
)
5492 subcontext
.type
= tright
;
5493 else if (tleft
->float_type() != NULL
)
5494 subcontext
.type
= tleft
;
5495 else if (tright
->float_type() != NULL
)
5496 subcontext
.type
= tright
;
5498 subcontext
.type
= tleft
;
5501 this->left_
->determine_type(&subcontext
);
5503 // The context for the right hand operand is the same as for the
5504 // left hand operand, except for a shift operator.
5507 subcontext
.type
= Type::lookup_integer_type("uint");
5508 subcontext
.may_be_abstract
= false;
5511 this->right_
->determine_type(&subcontext
);
5514 // Report an error if the binary operator OP does not support TYPE.
5515 // Return whether the operation is OK. This should not be used for
5519 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5520 source_location location
)
5525 case OPERATOR_ANDAND
:
5526 if (!type
->is_boolean_type())
5528 error_at(location
, "expected boolean type");
5534 case OPERATOR_NOTEQ
:
5535 if (type
->integer_type() == NULL
5536 && type
->float_type() == NULL
5537 && type
->complex_type() == NULL
5538 && !type
->is_string_type()
5539 && type
->points_to() == NULL
5540 && !type
->is_nil_type()
5541 && !type
->is_boolean_type()
5542 && type
->interface_type() == NULL
5543 && (type
->array_type() == NULL
5544 || type
->array_type()->length() != NULL
)
5545 && type
->map_type() == NULL
5546 && type
->channel_type() == NULL
5547 && type
->function_type() == NULL
)
5550 ("expected integer, floating, complex, string, pointer, "
5551 "boolean, interface, slice, map, channel, "
5552 "or function type"));
5561 if (type
->integer_type() == NULL
5562 && type
->float_type() == NULL
5563 && !type
->is_string_type())
5565 error_at(location
, "expected integer, floating, or string type");
5571 case OPERATOR_PLUSEQ
:
5572 if (type
->integer_type() == NULL
5573 && type
->float_type() == NULL
5574 && type
->complex_type() == NULL
5575 && !type
->is_string_type())
5578 "expected integer, floating, complex, or string type");
5583 case OPERATOR_MINUS
:
5584 case OPERATOR_MINUSEQ
:
5586 case OPERATOR_MULTEQ
:
5588 case OPERATOR_DIVEQ
:
5589 if (type
->integer_type() == NULL
5590 && type
->float_type() == NULL
5591 && type
->complex_type() == NULL
)
5593 error_at(location
, "expected integer, floating, or complex type");
5599 case OPERATOR_MODEQ
:
5603 case OPERATOR_ANDEQ
:
5605 case OPERATOR_XOREQ
:
5606 case OPERATOR_BITCLEAR
:
5607 case OPERATOR_BITCLEAREQ
:
5608 if (type
->integer_type() == NULL
)
5610 error_at(location
, "expected integer type");
5625 Binary_expression::do_check_types(Gogo
*)
5627 Type
* left_type
= this->left_
->type();
5628 Type
* right_type
= this->right_
->type();
5629 if (left_type
->is_error_type() || right_type
->is_error_type())
5631 this->set_is_error();
5635 if (this->op_
== OPERATOR_EQEQ
5636 || this->op_
== OPERATOR_NOTEQ
5637 || this->op_
== OPERATOR_LT
5638 || this->op_
== OPERATOR_LE
5639 || this->op_
== OPERATOR_GT
5640 || this->op_
== OPERATOR_GE
)
5642 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5643 && !Type::are_assignable(right_type
, left_type
, NULL
))
5645 this->report_error(_("incompatible types in binary expression"));
5648 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5650 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5653 this->set_is_error();
5657 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5659 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5661 this->report_error(_("incompatible types in binary expression"));
5664 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5667 this->set_is_error();
5673 if (left_type
->integer_type() == NULL
)
5674 this->report_error(_("shift of non-integer operand"));
5676 if (!right_type
->is_abstract()
5677 && (right_type
->integer_type() == NULL
5678 || !right_type
->integer_type()->is_unsigned()))
5679 this->report_error(_("shift count not unsigned integer"));
5685 if (this->right_
->integer_constant_value(true, val
, &type
))
5687 if (mpz_sgn(val
) < 0)
5688 this->report_error(_("negative shift count"));
5695 // Get a tree for a binary expression.
5698 Binary_expression::do_get_tree(Translate_context
* context
)
5700 tree left
= this->left_
->get_tree(context
);
5701 tree right
= this->right_
->get_tree(context
);
5703 if (left
== error_mark_node
|| right
== error_mark_node
)
5704 return error_mark_node
;
5706 enum tree_code code
;
5707 bool use_left_type
= true;
5708 bool is_shift_op
= false;
5712 case OPERATOR_NOTEQ
:
5717 return Expression::comparison_tree(context
, this->op_
,
5718 this->left_
->type(), left
,
5719 this->right_
->type(), right
,
5723 code
= TRUTH_ORIF_EXPR
;
5724 use_left_type
= false;
5726 case OPERATOR_ANDAND
:
5727 code
= TRUTH_ANDIF_EXPR
;
5728 use_left_type
= false;
5733 case OPERATOR_MINUS
:
5737 code
= BIT_IOR_EXPR
;
5740 code
= BIT_XOR_EXPR
;
5747 Type
*t
= this->left_
->type();
5748 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5751 code
= TRUNC_DIV_EXPR
;
5755 code
= TRUNC_MOD_EXPR
;
5757 case OPERATOR_LSHIFT
:
5761 case OPERATOR_RSHIFT
:
5766 code
= BIT_AND_EXPR
;
5768 case OPERATOR_BITCLEAR
:
5769 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5770 code
= BIT_AND_EXPR
;
5776 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5778 if (this->left_
->type()->is_string_type())
5780 gcc_assert(this->op_
== OPERATOR_PLUS
);
5781 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5782 static tree string_plus_decl
;
5783 return Gogo::call_builtin(&string_plus_decl
,
5794 tree compute_type
= excess_precision_type(type
);
5795 if (compute_type
!= NULL_TREE
)
5797 left
= ::convert(compute_type
, left
);
5798 right
= ::convert(compute_type
, right
);
5801 tree eval_saved
= NULL_TREE
;
5805 left
= save_expr(left
);
5807 right
= save_expr(right
);
5808 // Make sure the values are evaluated.
5809 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5810 void_type_node
, left
, right
);
5813 tree ret
= fold_build2_loc(this->location(),
5815 compute_type
!= NULL_TREE
? compute_type
: type
,
5818 if (compute_type
!= NULL_TREE
)
5819 ret
= ::convert(type
, ret
);
5821 // In Go, a shift larger than the size of the type is well-defined.
5822 // This is not true in GENERIC, so we need to insert a conditional.
5825 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5826 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5827 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5829 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5830 build_int_cst_type(TREE_TYPE(right
), bits
));
5832 tree overflow_result
= fold_convert_loc(this->location(),
5835 if (this->op_
== OPERATOR_RSHIFT
5836 && !this->left_
->type()->integer_type()->is_unsigned())
5838 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5839 boolean_type_node
, left
,
5840 fold_convert_loc(this->location(),
5842 integer_zero_node
));
5843 tree neg_one
= fold_build2_loc(this->location(),
5844 MINUS_EXPR
, TREE_TYPE(left
),
5845 fold_convert_loc(this->location(),
5848 fold_convert_loc(this->location(),
5851 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5852 TREE_TYPE(left
), neg
, neg_one
,
5856 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5857 compare
, ret
, overflow_result
);
5859 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5860 TREE_TYPE(ret
), eval_saved
, ret
);
5866 // Export a binary expression.
5869 Binary_expression::do_export(Export
* exp
) const
5871 exp
->write_c_string("(");
5872 this->left_
->export_expression(exp
);
5876 exp
->write_c_string(" || ");
5878 case OPERATOR_ANDAND
:
5879 exp
->write_c_string(" && ");
5882 exp
->write_c_string(" == ");
5884 case OPERATOR_NOTEQ
:
5885 exp
->write_c_string(" != ");
5888 exp
->write_c_string(" < ");
5891 exp
->write_c_string(" <= ");
5894 exp
->write_c_string(" > ");
5897 exp
->write_c_string(" >= ");
5900 exp
->write_c_string(" + ");
5902 case OPERATOR_MINUS
:
5903 exp
->write_c_string(" - ");
5906 exp
->write_c_string(" | ");
5909 exp
->write_c_string(" ^ ");
5912 exp
->write_c_string(" * ");
5915 exp
->write_c_string(" / ");
5918 exp
->write_c_string(" % ");
5920 case OPERATOR_LSHIFT
:
5921 exp
->write_c_string(" << ");
5923 case OPERATOR_RSHIFT
:
5924 exp
->write_c_string(" >> ");
5927 exp
->write_c_string(" & ");
5929 case OPERATOR_BITCLEAR
:
5930 exp
->write_c_string(" &^ ");
5935 this->right_
->export_expression(exp
);
5936 exp
->write_c_string(")");
5939 // Import a binary expression.
5942 Binary_expression::do_import(Import
* imp
)
5944 imp
->require_c_string("(");
5946 Expression
* left
= Expression::import_expression(imp
);
5949 if (imp
->match_c_string(" || "))
5954 else if (imp
->match_c_string(" && "))
5956 op
= OPERATOR_ANDAND
;
5959 else if (imp
->match_c_string(" == "))
5964 else if (imp
->match_c_string(" != "))
5966 op
= OPERATOR_NOTEQ
;
5969 else if (imp
->match_c_string(" < "))
5974 else if (imp
->match_c_string(" <= "))
5979 else if (imp
->match_c_string(" > "))
5984 else if (imp
->match_c_string(" >= "))
5989 else if (imp
->match_c_string(" + "))
5994 else if (imp
->match_c_string(" - "))
5996 op
= OPERATOR_MINUS
;
5999 else if (imp
->match_c_string(" | "))
6004 else if (imp
->match_c_string(" ^ "))
6009 else if (imp
->match_c_string(" * "))
6014 else if (imp
->match_c_string(" / "))
6019 else if (imp
->match_c_string(" % "))
6024 else if (imp
->match_c_string(" << "))
6026 op
= OPERATOR_LSHIFT
;
6029 else if (imp
->match_c_string(" >> "))
6031 op
= OPERATOR_RSHIFT
;
6034 else if (imp
->match_c_string(" & "))
6039 else if (imp
->match_c_string(" &^ "))
6041 op
= OPERATOR_BITCLEAR
;
6046 error_at(imp
->location(), "unrecognized binary operator");
6047 return Expression::make_error(imp
->location());
6050 Expression
* right
= Expression::import_expression(imp
);
6052 imp
->require_c_string(")");
6054 return Expression::make_binary(op
, left
, right
, imp
->location());
6057 // Make a binary expression.
6060 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6061 source_location location
)
6063 return new Binary_expression(op
, left
, right
, location
);
6066 // Implement a comparison.
6069 Expression::comparison_tree(Translate_context
* context
, Operator op
,
6070 Type
* left_type
, tree left_tree
,
6071 Type
* right_type
, tree right_tree
,
6072 source_location location
)
6074 enum tree_code code
;
6080 case OPERATOR_NOTEQ
:
6099 if (left_type
->is_string_type() && right_type
->is_string_type())
6101 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
6102 static tree string_compare_decl
;
6103 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6112 right_tree
= build_int_cst_type(integer_type_node
, 0);
6114 else if ((left_type
->interface_type() != NULL
6115 && right_type
->interface_type() == NULL
6116 && !right_type
->is_nil_type())
6117 || (left_type
->interface_type() == NULL
6118 && !left_type
->is_nil_type()
6119 && right_type
->interface_type() != NULL
))
6121 // Comparing an interface value to a non-interface value.
6122 if (left_type
->interface_type() == NULL
)
6124 std::swap(left_type
, right_type
);
6125 std::swap(left_tree
, right_tree
);
6128 // The right operand is not an interface. We need to take its
6129 // address if it is not a pointer.
6132 if (right_type
->points_to() != NULL
)
6134 make_tmp
= NULL_TREE
;
6137 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
6139 make_tmp
= NULL_TREE
;
6140 arg
= build_fold_addr_expr_loc(location
, right_tree
);
6141 if (DECL_P(right_tree
))
6142 TREE_ADDRESSABLE(right_tree
) = 1;
6146 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6147 get_name(right_tree
));
6148 DECL_IGNORED_P(tmp
) = 0;
6149 DECL_INITIAL(tmp
) = right_tree
;
6150 TREE_ADDRESSABLE(tmp
) = 1;
6151 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6152 SET_EXPR_LOCATION(make_tmp
, location
);
6153 arg
= build_fold_addr_expr_loc(location
, tmp
);
6155 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6157 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6159 if (left_type
->interface_type()->is_empty())
6161 static tree empty_interface_value_compare_decl
;
6162 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6164 "__go_empty_interface_value_compare",
6167 TREE_TYPE(left_tree
),
6169 TREE_TYPE(descriptor
),
6173 if (left_tree
== error_mark_node
)
6174 return error_mark_node
;
6175 // This can panic if the type is not comparable.
6176 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6180 static tree interface_value_compare_decl
;
6181 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6183 "__go_interface_value_compare",
6186 TREE_TYPE(left_tree
),
6188 TREE_TYPE(descriptor
),
6192 if (left_tree
== error_mark_node
)
6193 return error_mark_node
;
6194 // This can panic if the type is not comparable.
6195 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6197 right_tree
= build_int_cst_type(integer_type_node
, 0);
6199 if (make_tmp
!= NULL_TREE
)
6200 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6203 else if (left_type
->interface_type() != NULL
6204 && right_type
->interface_type() != NULL
)
6206 if (left_type
->interface_type()->is_empty())
6208 gcc_assert(right_type
->interface_type()->is_empty());
6209 static tree empty_interface_compare_decl
;
6210 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6212 "__go_empty_interface_compare",
6215 TREE_TYPE(left_tree
),
6217 TREE_TYPE(right_tree
),
6219 if (left_tree
== error_mark_node
)
6220 return error_mark_node
;
6221 // This can panic if the type is uncomparable.
6222 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6226 gcc_assert(!right_type
->interface_type()->is_empty());
6227 static tree interface_compare_decl
;
6228 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6230 "__go_interface_compare",
6233 TREE_TYPE(left_tree
),
6235 TREE_TYPE(right_tree
),
6237 if (left_tree
== error_mark_node
)
6238 return error_mark_node
;
6239 // This can panic if the type is uncomparable.
6240 TREE_NOTHROW(interface_compare_decl
) = 0;
6242 right_tree
= build_int_cst_type(integer_type_node
, 0);
6245 if (left_type
->is_nil_type()
6246 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6248 std::swap(left_type
, right_type
);
6249 std::swap(left_tree
, right_tree
);
6252 if (right_type
->is_nil_type())
6254 if (left_type
->array_type() != NULL
6255 && left_type
->array_type()->length() == NULL
)
6257 Array_type
* at
= left_type
->array_type();
6258 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6259 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6261 else if (left_type
->interface_type() != NULL
)
6263 // An interface is nil if the first field is nil.
6264 tree left_type_tree
= TREE_TYPE(left_tree
);
6265 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6266 tree field
= TYPE_FIELDS(left_type_tree
);
6267 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6269 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6273 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6274 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6278 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6279 return error_mark_node
;
6281 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6282 if (CAN_HAVE_LOCATION_P(ret
))
6283 SET_EXPR_LOCATION(ret
, location
);
6287 // Class Bound_method_expression.
6292 Bound_method_expression::do_traverse(Traverse
* traverse
)
6294 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6295 return TRAVERSE_EXIT
;
6296 return Expression::traverse(&this->method_
, traverse
);
6299 // Return the type of a bound method expression. The type of this
6300 // object is really the type of the method with no receiver. We
6301 // should be able to get away with just returning the type of the
6305 Bound_method_expression::do_type()
6307 return this->method_
->type();
6310 // Determine the types of a method expression.
6313 Bound_method_expression::do_determine_type(const Type_context
*)
6315 this->method_
->determine_type_no_context();
6316 Type
* mtype
= this->method_
->type();
6317 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6318 if (fntype
== NULL
|| !fntype
->is_method())
6319 this->expr_
->determine_type_no_context();
6322 Type_context
subcontext(fntype
->receiver()->type(), false);
6323 this->expr_
->determine_type(&subcontext
);
6327 // Check the types of a method expression.
6330 Bound_method_expression::do_check_types(Gogo
*)
6332 Type
* type
= this->method_
->type()->deref();
6334 || type
->function_type() == NULL
6335 || !type
->function_type()->is_method())
6336 this->report_error(_("object is not a method"));
6339 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6340 Type
* etype
= (this->expr_type_
!= NULL
6342 : this->expr_
->type());
6343 etype
= etype
->deref();
6344 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6345 this->report_error(_("method type does not match object type"));
6349 // Get the tree for a method expression. There is no standard tree
6350 // representation for this. The only places it may currently be used
6351 // are in a Call_expression or a Go_statement, which will take it
6352 // apart directly. So this has nothing to do at present.
6355 Bound_method_expression::do_get_tree(Translate_context
*)
6360 // Make a method expression.
6362 Bound_method_expression
*
6363 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6364 source_location location
)
6366 return new Bound_method_expression(expr
, method
, location
);
6369 // Class Builtin_call_expression. This is used for a call to a
6370 // builtin function.
6372 class Builtin_call_expression
: public Call_expression
6375 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6376 bool is_varargs
, source_location location
);
6379 // This overrides Call_expression::do_lower.
6381 do_lower(Gogo
*, Named_object
*, int);
6384 do_is_constant() const;
6387 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6390 do_float_constant_value(mpfr_t
, Type
**) const;
6393 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6399 do_determine_type(const Type_context
*);
6402 do_check_types(Gogo
*);
6407 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6408 this->args()->copy(),
6414 do_get_tree(Translate_context
*);
6417 do_export(Export
*) const;
6420 do_is_recover_call() const;
6423 do_set_recover_arg(Expression
*);
6426 // The builtin functions.
6427 enum Builtin_function_code
6431 // Predeclared builtin functions.
6448 // Builtin functions from the unsafe package.
6461 real_imag_type(Type
*);
6466 // A pointer back to the general IR structure. This avoids a global
6467 // variable, or passing it around everywhere.
6469 // The builtin function being called.
6470 Builtin_function_code code_
;
6471 // Used to stop endless loops when the length of an array uses len
6472 // or cap of the array itself.
6476 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6478 Expression_list
* args
,
6480 source_location location
)
6481 : Call_expression(fn
, args
, is_varargs
, location
),
6482 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false)
6484 Func_expression
* fnexp
= this->fn()->func_expression();
6485 gcc_assert(fnexp
!= NULL
);
6486 const std::string
& name(fnexp
->named_object()->name());
6487 if (name
== "append")
6488 this->code_
= BUILTIN_APPEND
;
6489 else if (name
== "cap")
6490 this->code_
= BUILTIN_CAP
;
6491 else if (name
== "close")
6492 this->code_
= BUILTIN_CLOSE
;
6493 else if (name
== "closed")
6494 this->code_
= BUILTIN_CLOSED
;
6495 else if (name
== "cmplx")
6496 this->code_
= BUILTIN_CMPLX
;
6497 else if (name
== "copy")
6498 this->code_
= BUILTIN_COPY
;
6499 else if (name
== "imag")
6500 this->code_
= BUILTIN_IMAG
;
6501 else if (name
== "len")
6502 this->code_
= BUILTIN_LEN
;
6503 else if (name
== "make")
6504 this->code_
= BUILTIN_MAKE
;
6505 else if (name
== "new")
6506 this->code_
= BUILTIN_NEW
;
6507 else if (name
== "panic")
6508 this->code_
= BUILTIN_PANIC
;
6509 else if (name
== "print")
6510 this->code_
= BUILTIN_PRINT
;
6511 else if (name
== "println")
6512 this->code_
= BUILTIN_PRINTLN
;
6513 else if (name
== "real")
6514 this->code_
= BUILTIN_REAL
;
6515 else if (name
== "recover")
6516 this->code_
= BUILTIN_RECOVER
;
6517 else if (name
== "Alignof")
6518 this->code_
= BUILTIN_ALIGNOF
;
6519 else if (name
== "Offsetof")
6520 this->code_
= BUILTIN_OFFSETOF
;
6521 else if (name
== "Sizeof")
6522 this->code_
= BUILTIN_SIZEOF
;
6527 // Return whether this is a call to recover. This is a virtual
6528 // function called from the parent class.
6531 Builtin_call_expression::do_is_recover_call() const
6533 if (this->classification() == EXPRESSION_ERROR
)
6535 return this->code_
== BUILTIN_RECOVER
;
6538 // Set the argument for a call to recover.
6541 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6543 const Expression_list
* args
= this->args();
6544 gcc_assert(args
== NULL
|| args
->empty());
6545 Expression_list
* new_args
= new Expression_list();
6546 new_args
->push_back(arg
);
6547 this->set_args(new_args
);
6550 // A traversal class which looks for a call expression.
6552 class Find_call_expression
: public Traverse
6555 Find_call_expression()
6556 : Traverse(traverse_expressions
),
6561 expression(Expression
**);
6565 { return this->found_
; }
6572 Find_call_expression::expression(Expression
** pexpr
)
6574 if ((*pexpr
)->call_expression() != NULL
)
6576 this->found_
= true;
6577 return TRAVERSE_EXIT
;
6579 return TRAVERSE_CONTINUE
;
6582 // Lower a builtin call expression. This turns new and make into
6583 // specific expressions. We also convert to a constant if we can.
6586 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6588 if (this->code_
== BUILTIN_NEW
)
6590 const Expression_list
* args
= this->args();
6591 if (args
== NULL
|| args
->size() < 1)
6592 this->report_error(_("not enough arguments"));
6593 else if (args
->size() > 1)
6594 this->report_error(_("too many arguments"));
6597 Expression
* arg
= args
->front();
6598 if (!arg
->is_type_expression())
6600 error_at(arg
->location(), "expected type");
6601 this->set_is_error();
6604 return Expression::make_allocation(arg
->type(), this->location());
6607 else if (this->code_
== BUILTIN_MAKE
)
6609 const Expression_list
* args
= this->args();
6610 if (args
== NULL
|| args
->size() < 1)
6611 this->report_error(_("not enough arguments"));
6614 Expression
* arg
= args
->front();
6615 if (!arg
->is_type_expression())
6617 error_at(arg
->location(), "expected type");
6618 this->set_is_error();
6622 Expression_list
* newargs
;
6623 if (args
->size() == 1)
6627 newargs
= new Expression_list();
6628 Expression_list::const_iterator p
= args
->begin();
6630 for (; p
!= args
->end(); ++p
)
6631 newargs
->push_back(*p
);
6633 return Expression::make_make(arg
->type(), newargs
,
6638 else if (this->is_constant())
6640 // We can only lower len and cap if there are no function calls
6641 // in the arguments. Otherwise we have to make the call.
6642 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6644 Expression
* arg
= this->one_arg();
6645 if (!arg
->is_constant())
6647 Find_call_expression find_call
;
6648 Expression::traverse(&arg
, &find_call
);
6649 if (find_call
.found())
6657 if (this->integer_constant_value(true, ival
, &type
))
6659 Expression
* ret
= Expression::make_integer(&ival
, type
,
6668 if (this->float_constant_value(rval
, &type
))
6670 Expression
* ret
= Expression::make_float(&rval
, type
,
6678 if (this->complex_constant_value(rval
, imag
, &type
))
6680 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6689 else if (this->code_
== BUILTIN_RECOVER
)
6691 if (function
!= NULL
)
6692 function
->func_value()->set_calls_recover();
6695 // Calling recover outside of a function always returns the
6696 // nil empty interface.
6697 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6698 return Expression::make_cast(eface
,
6699 Expression::make_nil(this->location()),
6703 else if (this->code_
== BUILTIN_APPEND
)
6705 // Lower the varargs.
6706 const Expression_list
* args
= this->args();
6707 if (args
== NULL
|| args
->empty())
6709 Type
* slice_type
= args
->front()->type();
6710 if (!slice_type
->is_open_array_type())
6712 error_at(args
->front()->location(), "argument 1 must be a slice");
6713 this->set_is_error();
6716 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6722 // Return the type of the real or imag functions, given the type of
6723 // the argument. We need to map complex to float, complex64 to
6724 // float32, and complex128 to float64, so it has to be done by name.
6725 // This returns NULL if it can't figure out the type.
6728 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6730 if (arg_type
== NULL
|| arg_type
->is_abstract())
6732 Named_type
* nt
= arg_type
->named_type();
6735 while (nt
->real_type()->named_type() != NULL
)
6736 nt
= nt
->real_type()->named_type();
6737 if (nt
->name() == "complex")
6738 return Type::lookup_float_type("float");
6739 else if (nt
->name() == "complex64")
6740 return Type::lookup_float_type("float32");
6741 else if (nt
->name() == "complex128")
6742 return Type::lookup_float_type("float64");
6747 // Return the type of the cmplx function, given the type of one of the
6748 // argments. Like real_imag_type, we have to map by name.
6751 Builtin_call_expression::cmplx_type(Type
* arg_type
)
6753 if (arg_type
== NULL
|| arg_type
->is_abstract())
6755 Named_type
* nt
= arg_type
->named_type();
6758 while (nt
->real_type()->named_type() != NULL
)
6759 nt
= nt
->real_type()->named_type();
6760 if (nt
->name() == "float")
6761 return Type::lookup_complex_type("complex");
6762 else if (nt
->name() == "float32")
6763 return Type::lookup_complex_type("complex64");
6764 else if (nt
->name() == "float64")
6765 return Type::lookup_complex_type("complex128");
6770 // Return a single argument, or NULL if there isn't one.
6773 Builtin_call_expression::one_arg() const
6775 const Expression_list
* args
= this->args();
6776 if (args
->size() != 1)
6778 return args
->front();
6781 // Return whether this is constant: len of a string, or len or cap of
6782 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6785 Builtin_call_expression::do_is_constant() const
6787 switch (this->code_
)
6795 Expression
* arg
= this->one_arg();
6798 Type
* arg_type
= arg
->type();
6800 if (arg_type
->points_to() != NULL
6801 && arg_type
->points_to()->array_type() != NULL
6802 && !arg_type
->points_to()->is_open_array_type())
6803 arg_type
= arg_type
->points_to();
6805 if (arg_type
->array_type() != NULL
6806 && arg_type
->array_type()->length() != NULL
)
6809 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6812 bool ret
= arg
->is_constant();
6813 this->seen_
= false;
6819 case BUILTIN_SIZEOF
:
6820 case BUILTIN_ALIGNOF
:
6821 return this->one_arg() != NULL
;
6823 case BUILTIN_OFFSETOF
:
6825 Expression
* arg
= this->one_arg();
6828 return arg
->field_reference_expression() != NULL
;
6833 const Expression_list
* args
= this->args();
6834 if (args
!= NULL
&& args
->size() == 2)
6835 return args
->front()->is_constant() && args
->back()->is_constant();
6842 Expression
* arg
= this->one_arg();
6843 return arg
!= NULL
&& arg
->is_constant();
6853 // Return an integer constant value if possible.
6856 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6860 if (this->code_
== BUILTIN_LEN
6861 || this->code_
== BUILTIN_CAP
)
6863 Expression
* arg
= this->one_arg();
6866 Type
* arg_type
= arg
->type();
6868 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6871 if (arg
->string_constant_value(&sval
))
6873 mpz_set_ui(val
, sval
.length());
6874 *ptype
= Type::lookup_integer_type("int");
6879 if (arg_type
->points_to() != NULL
6880 && arg_type
->points_to()->array_type() != NULL
6881 && !arg_type
->points_to()->is_open_array_type())
6882 arg_type
= arg_type
->points_to();
6884 if (arg_type
->array_type() != NULL
6885 && arg_type
->array_type()->length() != NULL
)
6889 Expression
* e
= arg_type
->array_type()->length();
6891 bool r
= e
->integer_constant_value(iota_is_constant
, val
, ptype
);
6892 this->seen_
= false;
6895 *ptype
= Type::lookup_integer_type("int");
6900 else if (this->code_
== BUILTIN_SIZEOF
6901 || this->code_
== BUILTIN_ALIGNOF
)
6903 Expression
* arg
= this->one_arg();
6906 Type
* arg_type
= arg
->type();
6907 if (arg_type
->is_error_type() || arg_type
->is_undefined())
6909 if (arg_type
->is_abstract())
6911 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
6912 unsigned long val_long
;
6913 if (this->code_
== BUILTIN_SIZEOF
)
6915 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
6916 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
6917 if (TREE_INT_CST_HIGH(type_size
) != 0)
6919 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
6920 val_long
= static_cast<unsigned long>(val_wide
);
6921 if (val_long
!= val_wide
)
6924 else if (this->code_
== BUILTIN_ALIGNOF
)
6926 if (arg
->field_reference_expression() == NULL
)
6927 val_long
= go_type_alignment(arg_type_tree
);
6930 // Calling unsafe.Alignof(s.f) returns the alignment of
6931 // the type of f when it is used as a field in a struct.
6932 val_long
= go_field_alignment(arg_type_tree
);
6937 mpz_set_ui(val
, val_long
);
6941 else if (this->code_
== BUILTIN_OFFSETOF
)
6943 Expression
* arg
= this->one_arg();
6946 Field_reference_expression
* farg
= arg
->field_reference_expression();
6949 Expression
* struct_expr
= farg
->expr();
6950 Type
* st
= struct_expr
->type();
6951 if (st
->struct_type() == NULL
)
6953 tree struct_tree
= st
->get_tree(this->gogo_
);
6954 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
6955 tree field
= TYPE_FIELDS(struct_tree
);
6956 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
6958 field
= DECL_CHAIN(field
);
6959 gcc_assert(field
!= NULL_TREE
);
6961 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
6962 if (offset_wide
< 0)
6964 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
6965 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
6967 mpz_set_ui(val
, offset_long
);
6973 // Return a floating point constant value if possible.
6976 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
6979 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
6981 Expression
* arg
= this->one_arg();
6992 if (arg
->complex_constant_value(real
, imag
, &type
))
6994 if (this->code_
== BUILTIN_REAL
)
6995 mpfr_set(val
, real
, GMP_RNDN
);
6997 mpfr_set(val
, imag
, GMP_RNDN
);
6998 *ptype
= Builtin_call_expression::real_imag_type(type
);
7010 // Return a complex constant value if possible.
7013 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
7016 if (this->code_
== BUILTIN_CMPLX
)
7018 const Expression_list
* args
= this->args();
7019 if (args
== NULL
|| args
->size() != 2)
7025 if (!args
->front()->float_constant_value(r
, &rtype
))
7036 if (args
->back()->float_constant_value(i
, &itype
)
7037 && Type::are_identical(rtype
, itype
, false, NULL
))
7039 mpfr_set(real
, r
, GMP_RNDN
);
7040 mpfr_set(imag
, i
, GMP_RNDN
);
7041 *ptype
= Builtin_call_expression::cmplx_type(rtype
);
7057 Builtin_call_expression::do_type()
7059 switch (this->code_
)
7061 case BUILTIN_INVALID
:
7068 const Expression_list
* args
= this->args();
7069 if (args
== NULL
|| args
->empty())
7070 return Type::make_error_type();
7071 return Type::make_pointer_type(args
->front()->type());
7077 case BUILTIN_ALIGNOF
:
7078 case BUILTIN_OFFSETOF
:
7079 case BUILTIN_SIZEOF
:
7080 return Type::lookup_integer_type("int");
7085 case BUILTIN_PRINTLN
:
7086 return Type::make_void_type();
7088 case BUILTIN_CLOSED
:
7089 return Type::lookup_bool_type();
7091 case BUILTIN_RECOVER
:
7092 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7094 case BUILTIN_APPEND
:
7096 const Expression_list
* args
= this->args();
7097 if (args
== NULL
|| args
->empty())
7098 return Type::make_error_type();
7099 return args
->front()->type();
7105 Expression
* arg
= this->one_arg();
7107 return Type::make_error_type();
7108 Type
* t
= arg
->type();
7109 if (t
->is_abstract())
7110 t
= t
->make_non_abstract_type();
7111 t
= Builtin_call_expression::real_imag_type(t
);
7113 t
= Type::make_error_type();
7119 const Expression_list
* args
= this->args();
7120 if (args
== NULL
|| args
->size() != 2)
7121 return Type::make_error_type();
7122 Type
* t
= args
->front()->type();
7123 if (t
->is_abstract())
7125 t
= args
->back()->type();
7126 if (t
->is_abstract())
7127 t
= t
->make_non_abstract_type();
7129 t
= Builtin_call_expression::cmplx_type(t
);
7131 t
= Type::make_error_type();
7137 // Determine the type.
7140 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7142 this->fn()->determine_type_no_context();
7144 const Expression_list
* args
= this->args();
7147 Type
* arg_type
= NULL
;
7148 switch (this->code_
)
7151 case BUILTIN_PRINTLN
:
7152 // Do not force a large integer constant to "int".
7158 arg_type
= Builtin_call_expression::cmplx_type(context
->type
);
7164 // For the cmplx function the type of one operand can
7165 // determine the type of the other, as in a binary expression.
7166 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7167 if (args
!= NULL
&& args
->size() == 2)
7169 Type
* t1
= args
->front()->type();
7170 Type
* t2
= args
->front()->type();
7171 if (!t1
->is_abstract())
7173 else if (!t2
->is_abstract())
7187 for (Expression_list::const_iterator pa
= args
->begin();
7191 Type_context subcontext
;
7192 subcontext
.type
= arg_type
;
7196 // We want to print large constants, we so can't just
7197 // use the appropriate nonabstract type. Use uint64 for
7198 // an integer if we know it is nonnegative, otherwise
7199 // use int64 for a integer, otherwise use float64 for a
7200 // float or complex128 for a complex.
7201 Type
* want_type
= NULL
;
7202 Type
* atype
= (*pa
)->type();
7203 if (atype
->is_abstract())
7205 if (atype
->integer_type() != NULL
)
7210 if (this->integer_constant_value(true, val
, &dummy
)
7211 && mpz_sgn(val
) >= 0)
7212 want_type
= Type::lookup_integer_type("uint64");
7214 want_type
= Type::lookup_integer_type("int64");
7217 else if (atype
->float_type() != NULL
)
7218 want_type
= Type::lookup_float_type("float64");
7219 else if (atype
->complex_type() != NULL
)
7220 want_type
= Type::lookup_complex_type("complex128");
7221 else if (atype
->is_abstract_string_type())
7222 want_type
= Type::lookup_string_type();
7223 else if (atype
->is_abstract_boolean_type())
7224 want_type
= Type::lookup_bool_type();
7227 subcontext
.type
= want_type
;
7231 (*pa
)->determine_type(&subcontext
);
7236 // If there is exactly one argument, return true. Otherwise give an
7237 // error message and return false.
7240 Builtin_call_expression::check_one_arg()
7242 const Expression_list
* args
= this->args();
7243 if (args
== NULL
|| args
->size() < 1)
7245 this->report_error(_("not enough arguments"));
7248 else if (args
->size() > 1)
7250 this->report_error(_("too many arguments"));
7253 if (args
->front()->is_error_expression()
7254 || args
->front()->type()->is_error_type()
7255 || args
->front()->type()->is_undefined())
7257 this->set_is_error();
7263 // Check argument types for a builtin function.
7266 Builtin_call_expression::do_check_types(Gogo
*)
7268 switch (this->code_
)
7270 case BUILTIN_INVALID
:
7278 // The single argument may be either a string or an array or a
7279 // map or a channel, or a pointer to a closed array.
7280 if (this->check_one_arg())
7282 Type
* arg_type
= this->one_arg()->type();
7283 if (arg_type
->points_to() != NULL
7284 && arg_type
->points_to()->array_type() != NULL
7285 && !arg_type
->points_to()->is_open_array_type())
7286 arg_type
= arg_type
->points_to();
7287 if (this->code_
== BUILTIN_CAP
)
7289 if (!arg_type
->is_error_type()
7290 && arg_type
->array_type() == NULL
7291 && arg_type
->channel_type() == NULL
)
7292 this->report_error(_("argument must be array or slice "
7297 if (!arg_type
->is_error_type()
7298 && !arg_type
->is_string_type()
7299 && arg_type
->array_type() == NULL
7300 && arg_type
->map_type() == NULL
7301 && arg_type
->channel_type() == NULL
)
7302 this->report_error(_("argument must be string or "
7303 "array or slice or map or channel"));
7310 case BUILTIN_PRINTLN
:
7312 const Expression_list
* args
= this->args();
7315 if (this->code_
== BUILTIN_PRINT
)
7316 warning_at(this->location(), 0,
7317 "no arguments for builtin function %<%s%>",
7318 (this->code_
== BUILTIN_PRINT
7324 for (Expression_list::const_iterator p
= args
->begin();
7328 Type
* type
= (*p
)->type();
7329 if (type
->is_error_type()
7330 || type
->is_string_type()
7331 || type
->integer_type() != NULL
7332 || type
->float_type() != NULL
7333 || type
->complex_type() != NULL
7334 || type
->is_boolean_type()
7335 || type
->points_to() != NULL
7336 || type
->interface_type() != NULL
7337 || type
->channel_type() != NULL
7338 || type
->map_type() != NULL
7339 || type
->function_type() != NULL
7340 || type
->is_open_array_type())
7343 this->report_error(_("unsupported argument type to "
7344 "builtin function"));
7351 case BUILTIN_CLOSED
:
7352 if (this->check_one_arg())
7354 if (this->one_arg()->type()->channel_type() == NULL
)
7355 this->report_error(_("argument must be channel"));
7360 case BUILTIN_SIZEOF
:
7361 case BUILTIN_ALIGNOF
:
7362 this->check_one_arg();
7365 case BUILTIN_RECOVER
:
7366 if (this->args() != NULL
&& !this->args()->empty())
7367 this->report_error(_("too many arguments"));
7370 case BUILTIN_OFFSETOF
:
7371 if (this->check_one_arg())
7373 Expression
* arg
= this->one_arg();
7374 if (arg
->field_reference_expression() == NULL
)
7375 this->report_error(_("argument must be a field reference"));
7381 const Expression_list
* args
= this->args();
7382 if (args
== NULL
|| args
->size() < 2)
7384 this->report_error(_("not enough arguments"));
7387 else if (args
->size() > 2)
7389 this->report_error(_("too many arguments"));
7392 Type
* arg1_type
= args
->front()->type();
7393 Type
* arg2_type
= args
->back()->type();
7394 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7398 if (arg1_type
->is_open_array_type())
7399 e1
= arg1_type
->array_type()->element_type();
7402 this->report_error(_("left argument must be a slice"));
7407 if (arg2_type
->is_open_array_type())
7408 e2
= arg2_type
->array_type()->element_type();
7409 else if (arg2_type
->is_string_type())
7410 e2
= Type::lookup_integer_type("uint8");
7413 this->report_error(_("right argument must be a slice or a string"));
7417 if (!Type::are_identical(e1
, e2
, true, NULL
))
7418 this->report_error(_("element types must be the same"));
7422 case BUILTIN_APPEND
:
7424 const Expression_list
* args
= this->args();
7425 if (args
== NULL
|| args
->size() < 2)
7427 this->report_error(_("not enough arguments"));
7430 if (args
->size() > 2)
7432 this->report_error(_("too many arguments"));
7436 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7440 this->report_error(_("arguments 1 and 2 have different types"));
7443 error_at(this->location(),
7444 "arguments 1 and 2 have different types (%s)",
7446 this->set_is_error();
7454 if (this->check_one_arg())
7456 if (this->one_arg()->type()->complex_type() == NULL
)
7457 this->report_error(_("argument must have complex type"));
7463 const Expression_list
* args
= this->args();
7464 if (args
== NULL
|| args
->size() < 2)
7465 this->report_error(_("not enough arguments"));
7466 else if (args
->size() > 2)
7467 this->report_error(_("too many arguments"));
7468 else if (args
->front()->is_error_expression()
7469 || args
->front()->type()->is_error_type()
7470 || args
->back()->is_error_expression()
7471 || args
->back()->type()->is_error_type())
7472 this->set_is_error();
7473 else if (!Type::are_identical(args
->front()->type(),
7474 args
->back()->type(), true, NULL
))
7475 this->report_error(_("cmplx arguments must have identical types"));
7476 else if (args
->front()->type()->float_type() == NULL
)
7477 this->report_error(_("cmplx arguments must have "
7478 "floating-point type"));
7487 // Return the tree for a builtin function.
7490 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7492 Gogo
* gogo
= context
->gogo();
7493 source_location location
= this->location();
7494 switch (this->code_
)
7496 case BUILTIN_INVALID
:
7504 const Expression_list
* args
= this->args();
7505 gcc_assert(args
!= NULL
&& args
->size() == 1);
7506 Expression
* arg
= *args
->begin();
7507 Type
* arg_type
= arg
->type();
7511 gcc_assert(saw_errors());
7512 return error_mark_node
;
7516 tree arg_tree
= arg
->get_tree(context
);
7518 this->seen_
= false;
7520 if (arg_tree
== error_mark_node
)
7521 return error_mark_node
;
7523 if (arg_type
->points_to() != NULL
)
7525 arg_type
= arg_type
->points_to();
7526 gcc_assert(arg_type
->array_type() != NULL
7527 && !arg_type
->is_open_array_type());
7528 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7529 arg_tree
= build_fold_indirect_ref(arg_tree
);
7533 if (this->code_
== BUILTIN_LEN
)
7535 if (arg_type
->is_string_type())
7536 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7537 else if (arg_type
->array_type() != NULL
)
7541 gcc_assert(saw_errors());
7542 return error_mark_node
;
7545 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7546 this->seen_
= false;
7548 else if (arg_type
->map_type() != NULL
)
7550 static tree map_len_fndecl
;
7551 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7556 arg_type
->get_tree(gogo
),
7559 else if (arg_type
->channel_type() != NULL
)
7561 static tree chan_len_fndecl
;
7562 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7567 arg_type
->get_tree(gogo
),
7575 if (arg_type
->array_type() != NULL
)
7579 gcc_assert(saw_errors());
7580 return error_mark_node
;
7583 val_tree
= arg_type
->array_type()->capacity_tree(gogo
,
7585 this->seen_
= false;
7587 else if (arg_type
->channel_type() != NULL
)
7589 static tree chan_cap_fndecl
;
7590 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7595 arg_type
->get_tree(gogo
),
7602 if (val_tree
== error_mark_node
)
7603 return error_mark_node
;
7605 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7606 if (type_tree
== TREE_TYPE(val_tree
))
7609 return fold(convert_to_integer(type_tree
, val_tree
));
7613 case BUILTIN_PRINTLN
:
7615 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7616 tree stmt_list
= NULL_TREE
;
7618 const Expression_list
* call_args
= this->args();
7619 if (call_args
!= NULL
)
7621 for (Expression_list::const_iterator p
= call_args
->begin();
7622 p
!= call_args
->end();
7625 if (is_ln
&& p
!= call_args
->begin())
7627 static tree print_space_fndecl
;
7628 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7633 if (call
== error_mark_node
)
7634 return error_mark_node
;
7635 append_to_statement_list(call
, &stmt_list
);
7638 Type
* type
= (*p
)->type();
7640 tree arg
= (*p
)->get_tree(context
);
7641 if (arg
== error_mark_node
)
7642 return error_mark_node
;
7646 if (type
->is_string_type())
7648 static tree print_string_fndecl
;
7649 pfndecl
= &print_string_fndecl
;
7650 fnname
= "__go_print_string";
7652 else if (type
->integer_type() != NULL
7653 && type
->integer_type()->is_unsigned())
7655 static tree print_uint64_fndecl
;
7656 pfndecl
= &print_uint64_fndecl
;
7657 fnname
= "__go_print_uint64";
7658 Type
* itype
= Type::lookup_integer_type("uint64");
7659 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7662 else if (type
->integer_type() != NULL
)
7664 static tree print_int64_fndecl
;
7665 pfndecl
= &print_int64_fndecl
;
7666 fnname
= "__go_print_int64";
7667 Type
* itype
= Type::lookup_integer_type("int64");
7668 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7671 else if (type
->float_type() != NULL
)
7673 static tree print_double_fndecl
;
7674 pfndecl
= &print_double_fndecl
;
7675 fnname
= "__go_print_double";
7676 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7678 else if (type
->complex_type() != NULL
)
7680 static tree print_complex_fndecl
;
7681 pfndecl
= &print_complex_fndecl
;
7682 fnname
= "__go_print_complex";
7683 arg
= fold_convert_loc(location
, complex_double_type_node
,
7686 else if (type
->is_boolean_type())
7688 static tree print_bool_fndecl
;
7689 pfndecl
= &print_bool_fndecl
;
7690 fnname
= "__go_print_bool";
7692 else if (type
->points_to() != NULL
7693 || type
->channel_type() != NULL
7694 || type
->map_type() != NULL
7695 || type
->function_type() != NULL
)
7697 static tree print_pointer_fndecl
;
7698 pfndecl
= &print_pointer_fndecl
;
7699 fnname
= "__go_print_pointer";
7700 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7702 else if (type
->interface_type() != NULL
)
7704 if (type
->interface_type()->is_empty())
7706 static tree print_empty_interface_fndecl
;
7707 pfndecl
= &print_empty_interface_fndecl
;
7708 fnname
= "__go_print_empty_interface";
7712 static tree print_interface_fndecl
;
7713 pfndecl
= &print_interface_fndecl
;
7714 fnname
= "__go_print_interface";
7717 else if (type
->is_open_array_type())
7719 static tree print_slice_fndecl
;
7720 pfndecl
= &print_slice_fndecl
;
7721 fnname
= "__go_print_slice";
7726 tree call
= Gogo::call_builtin(pfndecl
,
7733 if (call
== error_mark_node
)
7734 return error_mark_node
;
7735 append_to_statement_list(call
, &stmt_list
);
7741 static tree print_nl_fndecl
;
7742 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7747 if (call
== error_mark_node
)
7748 return error_mark_node
;
7749 append_to_statement_list(call
, &stmt_list
);
7757 const Expression_list
* args
= this->args();
7758 gcc_assert(args
!= NULL
&& args
->size() == 1);
7759 Expression
* arg
= args
->front();
7760 tree arg_tree
= arg
->get_tree(context
);
7761 if (arg_tree
== error_mark_node
)
7762 return error_mark_node
;
7763 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7764 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7766 arg_tree
, location
);
7767 static tree panic_fndecl
;
7768 tree call
= Gogo::call_builtin(&panic_fndecl
,
7773 TREE_TYPE(arg_tree
),
7775 if (call
== error_mark_node
)
7776 return error_mark_node
;
7777 // This function will throw an exception.
7778 TREE_NOTHROW(panic_fndecl
) = 0;
7779 // This function will not return.
7780 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7784 case BUILTIN_RECOVER
:
7786 // The argument is set when building recover thunks. It's a
7787 // boolean value which is true if we can recover a value now.
7788 const Expression_list
* args
= this->args();
7789 gcc_assert(args
!= NULL
&& args
->size() == 1);
7790 Expression
* arg
= args
->front();
7791 tree arg_tree
= arg
->get_tree(context
);
7792 if (arg_tree
== error_mark_node
)
7793 return error_mark_node
;
7795 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7796 tree empty_tree
= empty
->get_tree(context
->gogo());
7798 Type
* nil_type
= Type::make_nil_type();
7799 Expression
* nil
= Expression::make_nil(location
);
7800 tree nil_tree
= nil
->get_tree(context
);
7801 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7807 // We need to handle a deferred call to recover specially,
7808 // because it changes whether it can recover a panic or not.
7809 // See test7 in test/recover1.go.
7811 if (this->is_deferred())
7813 static tree deferred_recover_fndecl
;
7814 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7816 "__go_deferred_recover",
7822 static tree recover_fndecl
;
7823 call
= Gogo::call_builtin(&recover_fndecl
,
7829 if (call
== error_mark_node
)
7830 return error_mark_node
;
7831 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7832 call
, empty_nil_tree
);
7836 case BUILTIN_CLOSED
:
7838 const Expression_list
* args
= this->args();
7839 gcc_assert(args
!= NULL
&& args
->size() == 1);
7840 Expression
* arg
= args
->front();
7841 tree arg_tree
= arg
->get_tree(context
);
7842 if (arg_tree
== error_mark_node
)
7843 return error_mark_node
;
7844 if (this->code_
== BUILTIN_CLOSE
)
7846 static tree close_fndecl
;
7847 return Gogo::call_builtin(&close_fndecl
,
7849 "__go_builtin_close",
7852 TREE_TYPE(arg_tree
),
7857 static tree closed_fndecl
;
7858 return Gogo::call_builtin(&closed_fndecl
,
7860 "__go_builtin_closed",
7863 TREE_TYPE(arg_tree
),
7868 case BUILTIN_SIZEOF
:
7869 case BUILTIN_OFFSETOF
:
7870 case BUILTIN_ALIGNOF
:
7875 bool b
= this->integer_constant_value(true, val
, &dummy
);
7877 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7878 tree ret
= Expression::integer_constant_tree(val
, type
);
7885 const Expression_list
* args
= this->args();
7886 gcc_assert(args
!= NULL
&& args
->size() == 2);
7887 Expression
* arg1
= args
->front();
7888 Expression
* arg2
= args
->back();
7890 tree arg1_tree
= arg1
->get_tree(context
);
7891 tree arg2_tree
= arg2
->get_tree(context
);
7892 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7893 return error_mark_node
;
7895 Type
* arg1_type
= arg1
->type();
7896 Array_type
* at
= arg1_type
->array_type();
7897 arg1_tree
= save_expr(arg1_tree
);
7898 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
7899 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
7900 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
7901 return error_mark_node
;
7903 Type
* arg2_type
= arg2
->type();
7906 if (arg2_type
->is_open_array_type())
7908 at
= arg2_type
->array_type();
7909 arg2_tree
= save_expr(arg2_tree
);
7910 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7911 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7915 arg2_tree
= save_expr(arg2_tree
);
7916 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
7917 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
7919 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7920 return error_mark_node
;
7922 arg1_len
= save_expr(arg1_len
);
7923 arg2_len
= save_expr(arg2_len
);
7924 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
7925 fold_build2_loc(location
, LT_EXPR
,
7927 arg1_len
, arg2_len
),
7928 arg1_len
, arg2_len
);
7929 len
= save_expr(len
);
7931 Type
* element_type
= at
->element_type();
7932 tree element_type_tree
= element_type
->get_tree(gogo
);
7933 if (element_type_tree
== error_mark_node
)
7934 return error_mark_node
;
7935 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7936 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
7938 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
7939 TREE_TYPE(element_size
),
7940 bytecount
, element_size
);
7941 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
7943 arg1_val
= fold_convert_loc(location
, ptr_type_node
, arg1_val
);
7944 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7946 static tree copy_fndecl
;
7947 tree call
= Gogo::call_builtin(©_fndecl
,
7958 if (call
== error_mark_node
)
7959 return error_mark_node
;
7961 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
7965 case BUILTIN_APPEND
:
7967 const Expression_list
* args
= this->args();
7968 gcc_assert(args
!= NULL
&& args
->size() == 2);
7969 Expression
* arg1
= args
->front();
7970 Expression
* arg2
= args
->back();
7972 tree arg1_tree
= arg1
->get_tree(context
);
7973 tree arg2_tree
= arg2
->get_tree(context
);
7974 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7975 return error_mark_node
;
7977 Array_type
* at
= arg1
->type()->array_type();
7978 Type
* element_type
= at
->element_type();
7980 arg2_tree
= Expression::convert_for_assignment(context
, at
,
7984 if (arg2_tree
== error_mark_node
)
7985 return error_mark_node
;
7987 arg2_tree
= save_expr(arg2_tree
);
7988 tree arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7989 tree arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7990 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7991 return error_mark_node
;
7992 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7993 arg2_len
= fold_convert_loc(location
, size_type_node
, arg2_len
);
7995 tree element_type_tree
= element_type
->get_tree(gogo
);
7996 if (element_type_tree
== error_mark_node
)
7997 return error_mark_node
;
7998 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7999 element_size
= fold_convert_loc(location
, size_type_node
,
8002 // We rebuild the decl each time since the slice types may
8004 tree append_fndecl
= NULL_TREE
;
8005 return Gogo::call_builtin(&append_fndecl
,
8009 TREE_TYPE(arg1_tree
),
8010 TREE_TYPE(arg1_tree
),
8023 const Expression_list
* args
= this->args();
8024 gcc_assert(args
!= NULL
&& args
->size() == 1);
8025 Expression
* arg
= args
->front();
8026 tree arg_tree
= arg
->get_tree(context
);
8027 if (arg_tree
== error_mark_node
)
8028 return error_mark_node
;
8029 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
8030 if (this->code_
== BUILTIN_REAL
)
8031 return fold_build1_loc(location
, REALPART_EXPR
,
8032 TREE_TYPE(TREE_TYPE(arg_tree
)),
8035 return fold_build1_loc(location
, IMAGPART_EXPR
,
8036 TREE_TYPE(TREE_TYPE(arg_tree
)),
8042 const Expression_list
* args
= this->args();
8043 gcc_assert(args
!= NULL
&& args
->size() == 2);
8044 tree r
= args
->front()->get_tree(context
);
8045 tree i
= args
->back()->get_tree(context
);
8046 if (r
== error_mark_node
|| i
== error_mark_node
)
8047 return error_mark_node
;
8048 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
8049 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
8050 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
8051 return fold_build2_loc(location
, COMPLEX_EXPR
,
8052 build_complex_type(TREE_TYPE(r
)),
8061 // We have to support exporting a builtin call expression, because
8062 // code can set a constant to the result of a builtin expression.
8065 Builtin_call_expression::do_export(Export
* exp
) const
8072 if (this->integer_constant_value(true, val
, &dummy
))
8074 Integer_expression::export_integer(exp
, val
);
8083 if (this->float_constant_value(fval
, &dummy
))
8085 Float_expression::export_float(exp
, fval
);
8097 if (this->complex_constant_value(real
, imag
, &dummy
))
8099 Complex_expression::export_complex(exp
, real
, imag
);
8108 error_at(this->location(), "value is not constant");
8112 // A trailing space lets us reliably identify the end of the number.
8113 exp
->write_c_string(" ");
8116 // Class Call_expression.
8121 Call_expression::do_traverse(Traverse
* traverse
)
8123 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8124 return TRAVERSE_EXIT
;
8125 if (this->args_
!= NULL
)
8127 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8128 return TRAVERSE_EXIT
;
8130 return TRAVERSE_CONTINUE
;
8133 // Lower a call statement.
8136 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
8138 // A type case can look like a function call.
8139 if (this->fn_
->is_type_expression()
8140 && this->args_
!= NULL
8141 && this->args_
->size() == 1)
8142 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8145 // Recognize a call to a builtin function.
8146 Func_expression
* fne
= this->fn_
->func_expression();
8148 && fne
->named_object()->is_function_declaration()
8149 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
8150 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8151 this->is_varargs_
, this->location());
8153 // Handle an argument which is a call to a function which returns
8154 // multiple results.
8155 if (this->args_
!= NULL
8156 && this->args_
->size() == 1
8157 && this->args_
->front()->call_expression() != NULL
8158 && this->fn_
->type()->function_type() != NULL
)
8160 Function_type
* fntype
= this->fn_
->type()->function_type();
8161 size_t rc
= this->args_
->front()->call_expression()->result_count();
8163 && fntype
->parameters() != NULL
8164 && (fntype
->parameters()->size() == rc
8165 || (fntype
->is_varargs()
8166 && fntype
->parameters()->size() - 1 <= rc
)))
8168 Call_expression
* call
= this->args_
->front()->call_expression();
8169 Expression_list
* args
= new Expression_list
;
8170 for (size_t i
= 0; i
< rc
; ++i
)
8171 args
->push_back(Expression::make_call_result(call
, i
));
8172 // We can't return a new call expression here, because this
8173 // one may be referenced by Call_result expressions. FIXME.
8179 // Handle a call to a varargs function by packaging up the extra
8181 if (this->fn_
->type()->function_type() != NULL
8182 && this->fn_
->type()->function_type()->is_varargs())
8184 Function_type
* fntype
= this->fn_
->type()->function_type();
8185 const Typed_identifier_list
* parameters
= fntype
->parameters();
8186 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
8187 Type
* varargs_type
= parameters
->back().type();
8188 return this->lower_varargs(gogo
, function
, varargs_type
,
8189 parameters
->size());
8195 // Lower a call to a varargs function. FUNCTION is the function in
8196 // which the call occurs--it's not the function we are calling.
8197 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8198 // PARAM_COUNT is the number of parameters of the function we are
8199 // calling; the last of these parameters will be the varargs
8203 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8204 Type
* varargs_type
, size_t param_count
)
8206 if (this->varargs_are_lowered_
)
8209 source_location loc
= this->location();
8211 gcc_assert(param_count
> 0);
8212 gcc_assert(varargs_type
->is_open_array_type());
8214 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8215 if (arg_count
< param_count
- 1)
8217 // Not enough arguments; will be caught in check_types.
8221 Expression_list
* old_args
= this->args_
;
8222 Expression_list
* new_args
= new Expression_list();
8223 bool push_empty_arg
= false;
8224 if (old_args
== NULL
|| old_args
->empty())
8226 gcc_assert(param_count
== 1);
8227 push_empty_arg
= true;
8231 Expression_list::const_iterator pa
;
8233 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8235 if (static_cast<size_t>(i
) == param_count
)
8237 new_args
->push_back(*pa
);
8240 // We have reached the varargs parameter.
8242 bool issued_error
= false;
8243 if (pa
== old_args
->end())
8244 push_empty_arg
= true;
8245 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8246 new_args
->push_back(*pa
);
8247 else if (this->is_varargs_
)
8249 this->report_error(_("too many arguments"));
8252 else if (pa
+ 1 == old_args
->end()
8253 && this->is_compatible_varargs_argument(function
, *pa
,
8256 new_args
->push_back(*pa
);
8259 Type
* element_type
= varargs_type
->array_type()->element_type();
8260 Expression_list
* vals
= new Expression_list
;
8261 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8263 // Check types here so that we get a better message.
8264 Type
* patype
= (*pa
)->type();
8265 source_location paloc
= (*pa
)->location();
8266 if (!this->check_argument_type(i
, element_type
, patype
,
8267 paloc
, issued_error
))
8269 vals
->push_back(*pa
);
8272 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8273 new_args
->push_back(val
);
8278 new_args
->push_back(Expression::make_nil(loc
));
8280 // We can't return a new call expression here, because this one may
8281 // be referenced by Call_result expressions. FIXME.
8282 if (old_args
!= NULL
)
8284 this->args_
= new_args
;
8285 this->varargs_are_lowered_
= true;
8287 // Lower all the new subexpressions.
8288 Expression
* ret
= this;
8289 gogo
->lower_expression(function
, &ret
);
8290 gcc_assert(ret
== this);
8294 // Return true if ARG is a varargs argment which should be passed to
8295 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8296 // will be the last argument passed in the call, and PARAM_TYPE will
8297 // be the type of the last parameter of the varargs function being
8301 Call_expression::is_compatible_varargs_argument(Named_object
* function
,
8306 *issued_error
= false;
8308 Type
* var_type
= NULL
;
8310 // The simple case is passing the varargs parameter of the caller.
8311 Var_expression
* ve
= arg
->var_expression();
8312 if (ve
!= NULL
&& ve
->named_object()->is_variable())
8314 Variable
* var
= ve
->named_object()->var_value();
8315 if (var
->is_varargs_parameter())
8316 var_type
= var
->type();
8319 // The complex case is passing the varargs parameter of some
8320 // enclosing function. This will look like passing down *c.f where
8321 // c is the closure variable and f is a field in the closure.
8322 if (function
!= NULL
8323 && function
->func_value()->needs_closure()
8324 && arg
->classification() == EXPRESSION_UNARY
)
8326 Unary_expression
* ue
= static_cast<Unary_expression
*>(arg
);
8327 if (ue
->op() == OPERATOR_MULT
)
8329 Field_reference_expression
* fre
=
8330 ue
->operand()->deref()->field_reference_expression();
8333 Var_expression
* ve
= fre
->expr()->deref()->var_expression();
8336 Named_object
* no
= ve
->named_object();
8337 Function
* f
= function
->func_value();
8338 if (no
== f
->closure_var())
8340 // At this point we know that this indeed a
8341 // reference to some enclosing variable. Now we
8342 // need to figure out whether that variable is a
8343 // varargs parameter.
8344 Named_object
* enclosing
=
8345 f
->enclosing_var(fre
->field_index());
8346 Variable
* var
= enclosing
->var_value();
8347 if (var
->is_varargs_parameter())
8348 var_type
= var
->type();
8355 if (var_type
== NULL
)
8358 // We only match if the parameter is the same, with an identical
8360 Array_type
* var_at
= var_type
->array_type();
8361 gcc_assert(var_at
!= NULL
);
8362 Array_type
* param_at
= param_type
->array_type();
8363 if (param_at
!= NULL
8364 && Type::are_identical(var_at
->element_type(),
8365 param_at
->element_type(), true, NULL
))
8367 error_at(arg
->location(), "... mismatch: passing ...T as ...");
8368 *issued_error
= true;
8372 // Get the function type. Returns NULL if we don't know the type. If
8373 // this returns NULL, and if_ERROR is true, issues an error.
8376 Call_expression::get_function_type() const
8378 return this->fn_
->type()->function_type();
8381 // Return the number of values which this call will return.
8384 Call_expression::result_count() const
8386 const Function_type
* fntype
= this->get_function_type();
8389 if (fntype
->results() == NULL
)
8391 return fntype
->results()->size();
8394 // Return whether this is a call to the predeclared function recover.
8397 Call_expression::is_recover_call() const
8399 return this->do_is_recover_call();
8402 // Set the argument to the recover function.
8405 Call_expression::set_recover_arg(Expression
* arg
)
8407 this->do_set_recover_arg(arg
);
8410 // Virtual functions also implemented by Builtin_call_expression.
8413 Call_expression::do_is_recover_call() const
8419 Call_expression::do_set_recover_arg(Expression
*)
8427 Call_expression::do_type()
8429 if (this->type_
!= NULL
)
8433 Function_type
* fntype
= this->get_function_type();
8435 return Type::make_error_type();
8437 const Typed_identifier_list
* results
= fntype
->results();
8438 if (results
== NULL
)
8439 ret
= Type::make_void_type();
8440 else if (results
->size() == 1)
8441 ret
= results
->begin()->type();
8443 ret
= Type::make_call_multiple_result_type(this);
8450 // Determine types for a call expression. We can use the function
8451 // parameter types to set the types of the arguments.
8454 Call_expression::do_determine_type(const Type_context
*)
8456 this->fn_
->determine_type_no_context();
8457 Function_type
* fntype
= this->get_function_type();
8458 const Typed_identifier_list
* parameters
= NULL
;
8460 parameters
= fntype
->parameters();
8461 if (this->args_
!= NULL
)
8463 Typed_identifier_list::const_iterator pt
;
8464 if (parameters
!= NULL
)
8465 pt
= parameters
->begin();
8466 for (Expression_list::const_iterator pa
= this->args_
->begin();
8467 pa
!= this->args_
->end();
8470 if (parameters
!= NULL
&& pt
!= parameters
->end())
8472 Type_context
subcontext(pt
->type(), false);
8473 (*pa
)->determine_type(&subcontext
);
8477 (*pa
)->determine_type_no_context();
8482 // Check types for parameter I.
8485 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8486 const Type
* argument_type
,
8487 source_location argument_location
,
8491 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8496 error_at(argument_location
, "argument %d has incompatible type", i
);
8498 error_at(argument_location
,
8499 "argument %d has incompatible type (%s)",
8502 this->set_is_error();
8511 Call_expression::do_check_types(Gogo
*)
8513 Function_type
* fntype
= this->get_function_type();
8516 if (!this->fn_
->type()->is_error_type())
8517 this->report_error(_("expected function"));
8521 if (fntype
->is_method())
8523 // We don't support pointers to methods, so the function has to
8524 // be a bound method expression.
8525 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8528 this->report_error(_("method call without object"));
8531 Type
* first_arg_type
= bme
->first_argument()->type();
8532 if (first_arg_type
->points_to() == NULL
)
8534 // When passing a value, we need to check that we are
8535 // permitted to copy it.
8537 if (!Type::are_assignable(fntype
->receiver()->type(),
8538 first_arg_type
, &reason
))
8541 this->report_error(_("incompatible type for receiver"));
8544 error_at(this->location(),
8545 "incompatible type for receiver (%s)",
8547 this->set_is_error();
8553 // Note that varargs was handled by the lower_varargs() method, so
8554 // we don't have to worry about it here.
8556 const Typed_identifier_list
* parameters
= fntype
->parameters();
8557 if (this->args_
== NULL
)
8559 if (parameters
!= NULL
&& !parameters
->empty())
8560 this->report_error(_("not enough arguments"));
8562 else if (parameters
== NULL
)
8563 this->report_error(_("too many arguments"));
8567 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8568 for (Expression_list::const_iterator pa
= this->args_
->begin();
8569 pa
!= this->args_
->end();
8572 if (pt
== parameters
->end())
8574 this->report_error(_("too many arguments"));
8577 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8578 (*pa
)->location(), false);
8580 if (pt
!= parameters
->end())
8581 this->report_error(_("not enough arguments"));
8585 // Return whether we have to use a temporary variable to ensure that
8586 // we evaluate this call expression in order. If the call returns no
8587 // results then it will inevitably be executed last. If the call
8588 // returns more than one result then it will be used with Call_result
8589 // expressions. So we only have to use a temporary variable if the
8590 // call returns exactly one result.
8593 Call_expression::do_must_eval_in_order() const
8595 return this->result_count() == 1;
8598 // Get the function and the first argument to use when calling a bound
8602 Call_expression::bound_method_function(Translate_context
* context
,
8603 Bound_method_expression
* bound_method
,
8604 tree
* first_arg_ptr
)
8606 Expression
* first_argument
= bound_method
->first_argument();
8607 tree first_arg
= first_argument
->get_tree(context
);
8608 if (first_arg
== error_mark_node
)
8609 return error_mark_node
;
8611 // We always pass a pointer to the first argument when calling a
8613 if (first_argument
->type()->points_to() == NULL
)
8615 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8616 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8617 || DECL_P(first_arg
)
8618 || TREE_CODE(first_arg
) == INDIRECT_REF
8619 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8621 first_arg
= build_fold_addr_expr(first_arg
);
8622 if (DECL_P(first_arg
))
8623 TREE_ADDRESSABLE(first_arg
) = 1;
8627 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8628 get_name(first_arg
));
8629 DECL_IGNORED_P(tmp
) = 0;
8630 DECL_INITIAL(tmp
) = first_arg
;
8631 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8632 build1(DECL_EXPR
, void_type_node
, tmp
),
8633 build_fold_addr_expr(tmp
));
8634 TREE_ADDRESSABLE(tmp
) = 1;
8636 if (first_arg
== error_mark_node
)
8637 return error_mark_node
;
8640 Type
* fatype
= bound_method
->first_argument_type();
8643 if (fatype
->points_to() == NULL
)
8644 fatype
= Type::make_pointer_type(fatype
);
8645 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8646 if (first_arg
== error_mark_node
8647 || TREE_TYPE(first_arg
) == error_mark_node
)
8648 return error_mark_node
;
8651 *first_arg_ptr
= first_arg
;
8653 return bound_method
->method()->get_tree(context
);
8656 // Get the function and the first argument to use when calling an
8657 // interface method.
8660 Call_expression::interface_method_function(
8661 Translate_context
* context
,
8662 Interface_field_reference_expression
* interface_method
,
8663 tree
* first_arg_ptr
)
8665 tree expr
= interface_method
->expr()->get_tree(context
);
8666 if (expr
== error_mark_node
)
8667 return error_mark_node
;
8668 expr
= save_expr(expr
);
8669 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8670 if (first_arg
== error_mark_node
)
8671 return error_mark_node
;
8672 *first_arg_ptr
= first_arg
;
8673 return interface_method
->get_function_tree(context
, expr
);
8676 // Build the call expression.
8679 Call_expression::do_get_tree(Translate_context
* context
)
8681 if (this->tree_
!= NULL_TREE
)
8684 Function_type
* fntype
= this->get_function_type();
8686 return error_mark_node
;
8688 if (this->fn_
->is_error_expression())
8689 return error_mark_node
;
8691 Gogo
* gogo
= context
->gogo();
8692 source_location location
= this->location();
8694 Func_expression
* func
= this->fn_
->func_expression();
8695 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8696 Interface_field_reference_expression
* interface_method
=
8697 this->fn_
->interface_field_reference_expression();
8698 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8699 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8700 gcc_assert(!fntype
->is_method() || is_method
);
8704 if (this->args_
== NULL
|| this->args_
->empty())
8706 nargs
= is_method
? 1 : 0;
8707 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8711 const Typed_identifier_list
* params
= fntype
->parameters();
8712 gcc_assert(params
!= NULL
);
8714 nargs
= this->args_
->size();
8715 int i
= is_method
? 1 : 0;
8717 args
= new tree
[nargs
];
8719 Typed_identifier_list::const_iterator pp
= params
->begin();
8720 Expression_list::const_iterator pe
;
8721 for (pe
= this->args_
->begin();
8722 pe
!= this->args_
->end();
8725 gcc_assert(pp
!= params
->end());
8726 tree arg_val
= (*pe
)->get_tree(context
);
8727 args
[i
] = Expression::convert_for_assignment(context
,
8732 if (args
[i
] == error_mark_node
)
8735 return error_mark_node
;
8738 gcc_assert(pp
== params
->end());
8739 gcc_assert(i
== nargs
);
8742 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8743 if (rettype
== error_mark_node
)
8746 return error_mark_node
;
8751 fn
= func
->get_tree_without_closure(gogo
);
8752 else if (!is_method
)
8753 fn
= this->fn_
->get_tree(context
);
8754 else if (bound_method
!= NULL
)
8755 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8756 else if (interface_method
!= NULL
)
8757 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8761 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8764 return error_mark_node
;
8767 // This is to support builtin math functions when using 80387 math.
8769 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8770 fndecl
= TREE_OPERAND(fndecl
, 0);
8771 tree excess_type
= NULL_TREE
;
8773 && DECL_IS_BUILTIN(fndecl
)
8774 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8776 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8777 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8778 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8779 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8781 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8782 if (excess_type
!= NULL_TREE
)
8784 tree excess_fndecl
= mathfn_built_in(excess_type
,
8785 DECL_FUNCTION_CODE(fndecl
));
8786 if (excess_fndecl
== NULL_TREE
)
8787 excess_type
= NULL_TREE
;
8790 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8791 for (int i
= 0; i
< nargs
; ++i
)
8792 args
[i
] = ::convert(excess_type
, args
[i
]);
8797 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8801 SET_EXPR_LOCATION(ret
, location
);
8805 tree closure_tree
= func
->closure()->get_tree(context
);
8806 if (closure_tree
!= error_mark_node
)
8807 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8810 // If this is a recursive function type which returns itself, as in
8812 // we have used ptr_type_node for the return type. Add a cast here
8813 // to the correct type.
8814 if (TREE_TYPE(ret
) == ptr_type_node
)
8816 tree t
= this->type()->get_tree(gogo
);
8817 ret
= fold_convert_loc(location
, t
, ret
);
8820 if (excess_type
!= NULL_TREE
)
8822 // Calling convert here can undo our excess precision change.
8823 // That may or may not be a bug in convert_to_real.
8824 ret
= build1(NOP_EXPR
, rettype
, ret
);
8827 // If there is more than one result, we will refer to the call
8829 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8830 ret
= save_expr(ret
);
8837 // Make a call expression.
8840 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8841 source_location location
)
8843 return new Call_expression(fn
, args
, is_varargs
, location
);
8846 // A single result from a call which returns multiple results.
8848 class Call_result_expression
: public Expression
8851 Call_result_expression(Call_expression
* call
, unsigned int index
)
8852 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8853 call_(call
), index_(index
)
8858 do_traverse(Traverse
*);
8864 do_determine_type(const Type_context
*);
8867 do_check_types(Gogo
*);
8872 return new Call_result_expression(this->call_
->call_expression(),
8877 do_must_eval_in_order() const
8881 do_get_tree(Translate_context
*);
8884 // The underlying call expression.
8886 // Which result we want.
8887 unsigned int index_
;
8890 // Traverse a call result.
8893 Call_result_expression::do_traverse(Traverse
* traverse
)
8895 if (traverse
->remember_expression(this->call_
))
8897 // We have already traversed the call expression.
8898 return TRAVERSE_CONTINUE
;
8900 return Expression::traverse(&this->call_
, traverse
);
8906 Call_result_expression::do_type()
8908 if (this->classification() == EXPRESSION_ERROR
)
8909 return Type::make_error_type();
8911 // THIS->CALL_ can be replaced with a temporary reference due to
8912 // Call_expression::do_must_eval_in_order when there is an error.
8913 Call_expression
* ce
= this->call_
->call_expression();
8916 this->set_is_error();
8917 return Type::make_error_type();
8919 Function_type
* fntype
= ce
->get_function_type();
8922 this->set_is_error();
8923 return Type::make_error_type();
8925 const Typed_identifier_list
* results
= fntype
->results();
8926 if (results
== NULL
)
8928 this->report_error(_("number of results does not match "
8929 "number of values"));
8930 return Type::make_error_type();
8932 Typed_identifier_list::const_iterator pr
= results
->begin();
8933 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8935 if (pr
== results
->end())
8939 if (pr
== results
->end())
8941 this->report_error(_("number of results does not match "
8942 "number of values"));
8943 return Type::make_error_type();
8948 // Check the type. Just make sure that we trigger the warning in
8952 Call_result_expression::do_check_types(Gogo
*)
8957 // Determine the type. We have nothing to do here, but the 0 result
8958 // needs to pass down to the caller.
8961 Call_result_expression::do_determine_type(const Type_context
*)
8963 if (this->index_
== 0)
8964 this->call_
->determine_type_no_context();
8970 Call_result_expression::do_get_tree(Translate_context
* context
)
8972 tree call_tree
= this->call_
->get_tree(context
);
8973 if (call_tree
== error_mark_node
)
8974 return error_mark_node
;
8975 if (TREE_CODE(TREE_TYPE(call_tree
)) != RECORD_TYPE
)
8977 gcc_assert(saw_errors());
8978 return error_mark_node
;
8980 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
8981 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8983 gcc_assert(field
!= NULL_TREE
);
8984 field
= DECL_CHAIN(field
);
8986 gcc_assert(field
!= NULL_TREE
);
8987 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
8990 // Make a reference to a single result of a call which returns
8991 // multiple results.
8994 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
8996 return new Call_result_expression(call
, index
);
8999 // Class Index_expression.
9004 Index_expression::do_traverse(Traverse
* traverse
)
9006 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
9007 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
9008 || (this->end_
!= NULL
9009 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
9010 return TRAVERSE_EXIT
;
9011 return TRAVERSE_CONTINUE
;
9014 // Lower an index expression. This converts the generic index
9015 // expression into an array index, a string index, or a map index.
9018 Index_expression::do_lower(Gogo
*, Named_object
*, int)
9020 source_location location
= this->location();
9021 Expression
* left
= this->left_
;
9022 Expression
* start
= this->start_
;
9023 Expression
* end
= this->end_
;
9025 Type
* type
= left
->type();
9026 if (type
->is_error_type())
9027 return Expression::make_error(location
);
9028 else if (type
->array_type() != NULL
)
9029 return Expression::make_array_index(left
, start
, end
, location
);
9030 else if (type
->points_to() != NULL
9031 && type
->points_to()->array_type() != NULL
9032 && !type
->points_to()->is_open_array_type())
9034 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
9036 return Expression::make_array_index(deref
, start
, end
, location
);
9038 else if (type
->is_string_type())
9039 return Expression::make_string_index(left
, start
, end
, location
);
9040 else if (type
->map_type() != NULL
)
9044 error_at(location
, "invalid slice of map");
9045 return Expression::make_error(location
);
9047 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
9049 if (this->is_lvalue_
)
9050 ret
->set_is_lvalue();
9056 "attempt to index object which is not array, string, or map");
9057 return Expression::make_error(location
);
9061 // Make an index expression.
9064 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
9065 source_location location
)
9067 return new Index_expression(left
, start
, end
, location
);
9070 // An array index. This is used for both indexing and slicing.
9072 class Array_index_expression
: public Expression
9075 Array_index_expression(Expression
* array
, Expression
* start
,
9076 Expression
* end
, source_location location
)
9077 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
9078 array_(array
), start_(start
), end_(end
), type_(NULL
)
9083 do_traverse(Traverse
*);
9089 do_determine_type(const Type_context
*);
9092 do_check_types(Gogo
*);
9097 return Expression::make_array_index(this->array_
->copy(),
9098 this->start_
->copy(),
9101 : this->end_
->copy()),
9106 do_is_addressable() const;
9109 do_address_taken(bool escapes
)
9110 { this->array_
->address_taken(escapes
); }
9113 do_get_tree(Translate_context
*);
9116 // The array we are getting a value from.
9118 // The start or only index.
9120 // The end index of a slice. This may be NULL for a simple array
9121 // index, or it may be a nil expression for the length of the array.
9123 // The type of the expression.
9127 // Array index traversal.
9130 Array_index_expression::do_traverse(Traverse
* traverse
)
9132 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9133 return TRAVERSE_EXIT
;
9134 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9135 return TRAVERSE_EXIT
;
9136 if (this->end_
!= NULL
)
9138 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9139 return TRAVERSE_EXIT
;
9141 return TRAVERSE_CONTINUE
;
9144 // Return the type of an array index.
9147 Array_index_expression::do_type()
9149 if (this->type_
== NULL
)
9151 Array_type
* type
= this->array_
->type()->array_type();
9153 this->type_
= Type::make_error_type();
9154 else if (this->end_
== NULL
)
9155 this->type_
= type
->element_type();
9156 else if (type
->is_open_array_type())
9158 // A slice of a slice has the same type as the original
9160 this->type_
= this->array_
->type()->deref();
9164 // A slice of an array is a slice.
9165 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9171 // Set the type of an array index.
9174 Array_index_expression::do_determine_type(const Type_context
*)
9176 this->array_
->determine_type_no_context();
9177 Type_context
subcontext(NULL
, true);
9178 this->start_
->determine_type(&subcontext
);
9179 if (this->end_
!= NULL
)
9180 this->end_
->determine_type(&subcontext
);
9183 // Check types of an array index.
9186 Array_index_expression::do_check_types(Gogo
*)
9188 if (this->start_
->type()->integer_type() == NULL
)
9189 this->report_error(_("index must be integer"));
9190 if (this->end_
!= NULL
9191 && this->end_
->type()->integer_type() == NULL
9192 && !this->end_
->is_nil_expression())
9193 this->report_error(_("slice end must be integer"));
9195 Array_type
* array_type
= this->array_
->type()->array_type();
9196 if (array_type
== NULL
)
9198 gcc_assert(this->array_
->type()->is_error_type());
9202 unsigned int int_bits
=
9203 Type::lookup_integer_type("int")->integer_type()->bits();
9208 bool lval_valid
= (array_type
->length() != NULL
9209 && array_type
->length()->integer_constant_value(true,
9214 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9216 if (mpz_sgn(ival
) < 0
9217 || mpz_sizeinbase(ival
, 2) >= int_bits
9219 && (this->end_
== NULL
9220 ? mpz_cmp(ival
, lval
) >= 0
9221 : mpz_cmp(ival
, lval
) > 0)))
9223 error_at(this->start_
->location(), "array index out of bounds");
9224 this->set_is_error();
9227 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9229 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9231 if (mpz_sgn(ival
) < 0
9232 || mpz_sizeinbase(ival
, 2) >= int_bits
9233 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
9235 error_at(this->end_
->location(), "array index out of bounds");
9236 this->set_is_error();
9243 // A slice of an array requires an addressable array. A slice of a
9244 // slice is always possible.
9245 if (this->end_
!= NULL
9246 && !array_type
->is_open_array_type()
9247 && !this->array_
->is_addressable())
9248 this->report_error(_("array is not addressable"));
9251 // Return whether this expression is addressable.
9254 Array_index_expression::do_is_addressable() const
9256 // A slice expression is not addressable.
9257 if (this->end_
!= NULL
)
9260 // An index into a slice is addressable.
9261 if (this->array_
->type()->is_open_array_type())
9264 // An index into an array is addressable if the array is
9266 return this->array_
->is_addressable();
9269 // Get a tree for an array index.
9272 Array_index_expression::do_get_tree(Translate_context
* context
)
9274 Gogo
* gogo
= context
->gogo();
9275 source_location loc
= this->location();
9277 Array_type
* array_type
= this->array_
->type()->array_type();
9278 if (array_type
== NULL
)
9280 gcc_assert(this->array_
->type()->is_error_type());
9281 return error_mark_node
;
9284 tree type_tree
= array_type
->get_tree(gogo
);
9285 if (type_tree
== error_mark_node
)
9286 return error_mark_node
;
9288 tree array_tree
= this->array_
->get_tree(context
);
9289 if (array_tree
== error_mark_node
)
9290 return error_mark_node
;
9292 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9293 array_tree
= save_expr(array_tree
);
9294 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9295 if (length_tree
== error_mark_node
)
9296 return error_mark_node
;
9297 length_tree
= save_expr(length_tree
);
9298 tree length_type
= TREE_TYPE(length_tree
);
9300 tree bad_index
= boolean_false_node
;
9302 tree start_tree
= this->start_
->get_tree(context
);
9303 if (start_tree
== error_mark_node
)
9304 return error_mark_node
;
9305 if (!DECL_P(start_tree
))
9306 start_tree
= save_expr(start_tree
);
9307 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9308 start_tree
= convert_to_integer(length_type
, start_tree
);
9310 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9313 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9314 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9315 fold_build2_loc(loc
,
9319 boolean_type_node
, start_tree
,
9322 int code
= (array_type
->length() != NULL
9323 ? (this->end_
== NULL
9324 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9325 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9326 : (this->end_
== NULL
9327 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9328 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9329 tree crash
= Gogo::runtime_error(code
, loc
);
9331 if (this->end_
== NULL
)
9333 // Simple array indexing. This has to return an l-value, so
9334 // wrap the index check into START_TREE.
9335 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9336 build3(COND_EXPR
, void_type_node
,
9337 bad_index
, crash
, NULL_TREE
),
9339 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9341 if (array_type
->length() != NULL
)
9344 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9345 start_tree
, NULL_TREE
, NULL_TREE
);
9350 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9351 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9352 if (element_type_tree
== error_mark_node
)
9353 return error_mark_node
;
9354 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9355 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9356 start_tree
, element_size
);
9357 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9358 TREE_TYPE(values
), values
, offset
);
9359 return build_fold_indirect_ref(ptr
);
9365 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9366 if (capacity_tree
== error_mark_node
)
9367 return error_mark_node
;
9368 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9371 if (this->end_
->is_nil_expression())
9372 end_tree
= length_tree
;
9375 end_tree
= this->end_
->get_tree(context
);
9376 if (end_tree
== error_mark_node
)
9377 return error_mark_node
;
9378 if (!DECL_P(end_tree
))
9379 end_tree
= save_expr(end_tree
);
9380 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9381 end_tree
= convert_to_integer(length_type
, end_tree
);
9383 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9386 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9388 capacity_tree
= save_expr(capacity_tree
);
9389 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9390 fold_build2_loc(loc
, LT_EXPR
,
9392 end_tree
, start_tree
),
9393 fold_build2_loc(loc
, GT_EXPR
,
9395 end_tree
, capacity_tree
));
9396 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9397 bad_index
, bad_end
);
9400 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9401 if (element_type_tree
== error_mark_node
)
9402 return error_mark_node
;
9403 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9405 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9406 fold_convert_loc(loc
, sizetype
, start_tree
),
9409 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9410 if (value_pointer
== error_mark_node
)
9411 return error_mark_node
;
9413 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9414 TREE_TYPE(value_pointer
),
9415 value_pointer
, offset
);
9417 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9418 end_tree
, start_tree
);
9420 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9421 capacity_tree
, start_tree
);
9423 tree struct_tree
= this->type()->get_tree(gogo
);
9424 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9426 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9428 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9429 tree field
= TYPE_FIELDS(struct_tree
);
9430 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9432 elt
->value
= value_pointer
;
9434 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9435 field
= DECL_CHAIN(field
);
9436 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9438 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9440 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9441 field
= DECL_CHAIN(field
);
9442 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9444 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9446 tree constructor
= build_constructor(struct_tree
, init
);
9448 if (TREE_CONSTANT(value_pointer
)
9449 && TREE_CONSTANT(result_length_tree
)
9450 && TREE_CONSTANT(result_capacity_tree
))
9451 TREE_CONSTANT(constructor
) = 1;
9453 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9454 build3(COND_EXPR
, void_type_node
,
9455 bad_index
, crash
, NULL_TREE
),
9459 // Make an array index expression. END may be NULL.
9462 Expression::make_array_index(Expression
* array
, Expression
* start
,
9463 Expression
* end
, source_location location
)
9465 // Taking a slice of a composite literal requires moving the literal
9467 if (end
!= NULL
&& array
->is_composite_literal())
9469 array
= Expression::make_heap_composite(array
, location
);
9470 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9472 return new Array_index_expression(array
, start
, end
, location
);
9475 // A string index. This is used for both indexing and slicing.
9477 class String_index_expression
: public Expression
9480 String_index_expression(Expression
* string
, Expression
* start
,
9481 Expression
* end
, source_location location
)
9482 : Expression(EXPRESSION_STRING_INDEX
, location
),
9483 string_(string
), start_(start
), end_(end
)
9488 do_traverse(Traverse
*);
9494 do_determine_type(const Type_context
*);
9497 do_check_types(Gogo
*);
9502 return Expression::make_string_index(this->string_
->copy(),
9503 this->start_
->copy(),
9506 : this->end_
->copy()),
9511 do_get_tree(Translate_context
*);
9514 // The string we are getting a value from.
9515 Expression
* string_
;
9516 // The start or only index.
9518 // The end index of a slice. This may be NULL for a single index,
9519 // or it may be a nil expression for the length of the string.
9523 // String index traversal.
9526 String_index_expression::do_traverse(Traverse
* traverse
)
9528 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9529 return TRAVERSE_EXIT
;
9530 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9531 return TRAVERSE_EXIT
;
9532 if (this->end_
!= NULL
)
9534 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9535 return TRAVERSE_EXIT
;
9537 return TRAVERSE_CONTINUE
;
9540 // Return the type of a string index.
9543 String_index_expression::do_type()
9545 if (this->end_
== NULL
)
9546 return Type::lookup_integer_type("uint8");
9548 return this->string_
->type();
9551 // Determine the type of a string index.
9554 String_index_expression::do_determine_type(const Type_context
*)
9556 this->string_
->determine_type_no_context();
9557 Type_context
subcontext(NULL
, true);
9558 this->start_
->determine_type(&subcontext
);
9559 if (this->end_
!= NULL
)
9560 this->end_
->determine_type(&subcontext
);
9563 // Check types of a string index.
9566 String_index_expression::do_check_types(Gogo
*)
9568 if (this->start_
->type()->integer_type() == NULL
)
9569 this->report_error(_("index must be integer"));
9570 if (this->end_
!= NULL
9571 && this->end_
->type()->integer_type() == NULL
9572 && !this->end_
->is_nil_expression())
9573 this->report_error(_("slice end must be integer"));
9576 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9581 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9583 if (mpz_sgn(ival
) < 0
9584 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9586 error_at(this->start_
->location(), "string index out of bounds");
9587 this->set_is_error();
9590 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9592 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9594 if (mpz_sgn(ival
) < 0
9595 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9597 error_at(this->end_
->location(), "string index out of bounds");
9598 this->set_is_error();
9605 // Get a tree for a string index.
9608 String_index_expression::do_get_tree(Translate_context
* context
)
9610 source_location loc
= this->location();
9612 tree string_tree
= this->string_
->get_tree(context
);
9613 if (string_tree
== error_mark_node
)
9614 return error_mark_node
;
9616 if (this->string_
->type()->points_to() != NULL
)
9617 string_tree
= build_fold_indirect_ref(string_tree
);
9618 if (!DECL_P(string_tree
))
9619 string_tree
= save_expr(string_tree
);
9620 tree string_type
= TREE_TYPE(string_tree
);
9622 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9623 length_tree
= save_expr(length_tree
);
9624 tree length_type
= TREE_TYPE(length_tree
);
9626 tree bad_index
= boolean_false_node
;
9628 tree start_tree
= this->start_
->get_tree(context
);
9629 if (start_tree
== error_mark_node
)
9630 return error_mark_node
;
9631 if (!DECL_P(start_tree
))
9632 start_tree
= save_expr(start_tree
);
9633 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9634 start_tree
= convert_to_integer(length_type
, start_tree
);
9636 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9639 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9641 int code
= (this->end_
== NULL
9642 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9643 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9644 tree crash
= Gogo::runtime_error(code
, loc
);
9646 if (this->end_
== NULL
)
9648 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9650 fold_build2_loc(loc
, GE_EXPR
,
9652 start_tree
, length_tree
));
9654 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9655 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9657 fold_convert_loc(loc
, sizetype
, start_tree
));
9658 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9660 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9661 build3(COND_EXPR
, void_type_node
,
9662 bad_index
, crash
, NULL_TREE
),
9668 if (this->end_
->is_nil_expression())
9669 end_tree
= build_int_cst(length_type
, -1);
9672 end_tree
= this->end_
->get_tree(context
);
9673 if (end_tree
== error_mark_node
)
9674 return error_mark_node
;
9675 if (!DECL_P(end_tree
))
9676 end_tree
= save_expr(end_tree
);
9677 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9678 end_tree
= convert_to_integer(length_type
, end_tree
);
9680 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9683 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9686 static tree strslice_fndecl
;
9687 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9689 "__go_string_slice",
9698 if (ret
== error_mark_node
)
9699 return error_mark_node
;
9700 // This will panic if the bounds are out of range for the
9702 TREE_NOTHROW(strslice_fndecl
) = 0;
9704 if (bad_index
== boolean_false_node
)
9707 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9708 build3(COND_EXPR
, void_type_node
,
9709 bad_index
, crash
, NULL_TREE
),
9714 // Make a string index expression. END may be NULL.
9717 Expression::make_string_index(Expression
* string
, Expression
* start
,
9718 Expression
* end
, source_location location
)
9720 return new String_index_expression(string
, start
, end
, location
);
9725 // Get the type of the map.
9728 Map_index_expression::get_map_type() const
9730 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9732 gcc_assert(saw_errors());
9736 // Map index traversal.
9739 Map_index_expression::do_traverse(Traverse
* traverse
)
9741 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9742 return TRAVERSE_EXIT
;
9743 return Expression::traverse(&this->index_
, traverse
);
9746 // Return the type of a map index.
9749 Map_index_expression::do_type()
9751 Map_type
* mt
= this->get_map_type();
9753 return Type::make_error_type();
9754 Type
* type
= mt
->val_type();
9755 // If this map index is in a tuple assignment, we actually return a
9756 // pointer to the value type. Tuple_map_assignment_statement is
9757 // responsible for handling this correctly. We need to get the type
9758 // right in case this gets assigned to a temporary variable.
9759 if (this->is_in_tuple_assignment_
)
9760 type
= Type::make_pointer_type(type
);
9764 // Fix the type of a map index.
9767 Map_index_expression::do_determine_type(const Type_context
*)
9769 this->map_
->determine_type_no_context();
9770 Map_type
* mt
= this->get_map_type();
9771 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
9772 Type_context
subcontext(key_type
, false);
9773 this->index_
->determine_type(&subcontext
);
9776 // Check types of a map index.
9779 Map_index_expression::do_check_types(Gogo
*)
9782 Map_type
* mt
= this->get_map_type();
9785 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
9788 this->report_error(_("incompatible type for map index"));
9791 error_at(this->location(), "incompatible type for map index (%s)",
9793 this->set_is_error();
9798 // Get a tree for a map index.
9801 Map_index_expression::do_get_tree(Translate_context
* context
)
9803 Map_type
* type
= this->get_map_type();
9805 return error_mark_node
;
9807 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9808 if (valptr
== error_mark_node
)
9809 return error_mark_node
;
9810 valptr
= save_expr(valptr
);
9812 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9814 if (this->is_lvalue_
)
9815 return build_fold_indirect_ref(valptr
);
9816 else if (this->is_in_tuple_assignment_
)
9818 // Tuple_map_assignment_statement is responsible for using this
9824 return fold_build3(COND_EXPR
, val_type_tree
,
9825 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9826 fold_convert(TREE_TYPE(valptr
),
9827 null_pointer_node
)),
9828 type
->val_type()->get_init_tree(context
->gogo(),
9830 build_fold_indirect_ref(valptr
));
9834 // Get a tree for the map index. This returns a tree which evaluates
9835 // to a pointer to a value. The pointer will be NULL if the key is
9839 Map_index_expression::get_value_pointer(Translate_context
* context
,
9842 Map_type
* type
= this->get_map_type();
9844 return error_mark_node
;
9846 tree map_tree
= this->map_
->get_tree(context
);
9847 tree index_tree
= this->index_
->get_tree(context
);
9848 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9849 this->index_
->type(),
9852 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9853 return error_mark_node
;
9855 if (this->map_
->type()->points_to() != NULL
)
9856 map_tree
= build_fold_indirect_ref(map_tree
);
9858 // We need to pass in a pointer to the key, so stuff it into a
9860 tree tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9861 DECL_IGNORED_P(tmp
) = 0;
9862 DECL_INITIAL(tmp
) = index_tree
;
9863 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9864 tree tmpref
= fold_convert(const_ptr_type_node
, build_fold_addr_expr(tmp
));
9865 TREE_ADDRESSABLE(tmp
) = 1;
9867 static tree map_index_fndecl
;
9868 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9872 const_ptr_type_node
,
9873 TREE_TYPE(map_tree
),
9875 const_ptr_type_node
,
9880 : boolean_false_node
));
9881 if (call
== error_mark_node
)
9882 return error_mark_node
;
9883 // This can panic on a map of interface type if the interface holds
9884 // an uncomparable or unhashable type.
9885 TREE_NOTHROW(map_index_fndecl
) = 0;
9887 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9888 if (val_type_tree
== error_mark_node
)
9889 return error_mark_node
;
9890 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9892 return build2(COMPOUND_EXPR
, ptr_val_type_tree
,
9894 fold_convert(ptr_val_type_tree
, call
));
9897 // Make a map index expression.
9899 Map_index_expression
*
9900 Expression::make_map_index(Expression
* map
, Expression
* index
,
9901 source_location location
)
9903 return new Map_index_expression(map
, index
, location
);
9906 // Class Field_reference_expression.
9908 // Return the type of a field reference.
9911 Field_reference_expression::do_type()
9913 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9914 gcc_assert(struct_type
!= NULL
);
9915 return struct_type
->field(this->field_index_
)->type();
9918 // Check the types for a field reference.
9921 Field_reference_expression::do_check_types(Gogo
*)
9923 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9924 gcc_assert(struct_type
!= NULL
);
9925 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9928 // Get a tree for a field reference.
9931 Field_reference_expression::do_get_tree(Translate_context
* context
)
9933 tree struct_tree
= this->expr_
->get_tree(context
);
9934 if (struct_tree
== error_mark_node
9935 || TREE_TYPE(struct_tree
) == error_mark_node
)
9936 return error_mark_node
;
9937 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
9938 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
9939 if (field
== NULL_TREE
)
9941 // This can happen for a type which refers to itself indirectly
9942 // and then turns out to be erroneous.
9943 gcc_assert(saw_errors());
9944 return error_mark_node
;
9946 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
9948 field
= DECL_CHAIN(field
);
9949 gcc_assert(field
!= NULL_TREE
);
9951 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
9955 // Make a reference to a qualified identifier in an expression.
9957 Field_reference_expression
*
9958 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
9959 source_location location
)
9961 return new Field_reference_expression(expr
, field_index
, location
);
9964 // Class Interface_field_reference_expression.
9966 // Return a tree for the pointer to the function to call.
9969 Interface_field_reference_expression::get_function_tree(Translate_context
*,
9972 if (this->expr_
->type()->points_to() != NULL
)
9973 expr
= build_fold_indirect_ref(expr
);
9975 tree expr_type
= TREE_TYPE(expr
);
9976 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9978 tree field
= TYPE_FIELDS(expr_type
);
9979 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
9981 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9982 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
9984 table
= build_fold_indirect_ref(table
);
9985 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
9987 std::string name
= Gogo::unpack_hidden_name(this->name_
);
9988 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
9990 field
= DECL_CHAIN(field
))
9992 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
9995 gcc_assert(field
!= NULL_TREE
);
9997 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
10000 // Return a tree for the first argument to pass to the interface
10004 Interface_field_reference_expression::get_underlying_object_tree(
10005 Translate_context
*,
10008 if (this->expr_
->type()->points_to() != NULL
)
10009 expr
= build_fold_indirect_ref(expr
);
10011 tree expr_type
= TREE_TYPE(expr
);
10012 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10014 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
10015 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
10017 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10023 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
10025 return Expression::traverse(&this->expr_
, traverse
);
10028 // Return the type of an interface field reference.
10031 Interface_field_reference_expression::do_type()
10033 Type
* expr_type
= this->expr_
->type();
10035 Type
* points_to
= expr_type
->points_to();
10036 if (points_to
!= NULL
)
10037 expr_type
= points_to
;
10039 Interface_type
* interface_type
= expr_type
->interface_type();
10040 if (interface_type
== NULL
)
10041 return Type::make_error_type();
10043 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
10044 if (method
== NULL
)
10045 return Type::make_error_type();
10047 return method
->type();
10050 // Determine types.
10053 Interface_field_reference_expression::do_determine_type(const Type_context
*)
10055 this->expr_
->determine_type_no_context();
10058 // Check the types for an interface field reference.
10061 Interface_field_reference_expression::do_check_types(Gogo
*)
10063 Type
* type
= this->expr_
->type();
10065 Type
* points_to
= type
->points_to();
10066 if (points_to
!= NULL
)
10069 Interface_type
* interface_type
= type
->interface_type();
10070 if (interface_type
== NULL
)
10071 this->report_error(_("expected interface or pointer to interface"));
10074 const Typed_identifier
* method
=
10075 interface_type
->find_method(this->name_
);
10076 if (method
== NULL
)
10078 error_at(this->location(), "method %qs not in interface",
10079 Gogo::message_name(this->name_
).c_str());
10080 this->set_is_error();
10085 // Get a tree for a reference to a field in an interface. There is no
10086 // standard tree type representation for this: it's a function
10087 // attached to its first argument, like a Bound_method_expression.
10088 // The only places it may currently be used are in a Call_expression
10089 // or a Go_statement, which will take it apart directly. So this has
10090 // nothing to do at present.
10093 Interface_field_reference_expression::do_get_tree(Translate_context
*)
10098 // Make a reference to a field in an interface.
10101 Expression::make_interface_field_reference(Expression
* expr
,
10102 const std::string
& field
,
10103 source_location location
)
10105 return new Interface_field_reference_expression(expr
, field
, location
);
10108 // A general selector. This is a Parser_expression for LEFT.NAME. It
10109 // is lowered after we know the type of the left hand side.
10111 class Selector_expression
: public Parser_expression
10114 Selector_expression(Expression
* left
, const std::string
& name
,
10115 source_location location
)
10116 : Parser_expression(EXPRESSION_SELECTOR
, location
),
10117 left_(left
), name_(name
)
10122 do_traverse(Traverse
* traverse
)
10123 { return Expression::traverse(&this->left_
, traverse
); }
10126 do_lower(Gogo
*, Named_object
*, int);
10131 return new Selector_expression(this->left_
->copy(), this->name_
,
10137 lower_method_expression(Gogo
*);
10139 // The expression on the left hand side.
10141 // The name on the right hand side.
10145 // Lower a selector expression once we know the real type of the left
10149 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
10151 Expression
* left
= this->left_
;
10152 if (left
->is_type_expression())
10153 return this->lower_method_expression(gogo
);
10154 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
10158 // Lower a method expression T.M or (*T).M. We turn this into a
10159 // function literal.
10162 Selector_expression::lower_method_expression(Gogo
* gogo
)
10164 source_location location
= this->location();
10165 Type
* type
= this->left_
->type();
10166 const std::string
& name(this->name_
);
10169 if (type
->points_to() == NULL
)
10170 is_pointer
= false;
10174 type
= type
->points_to();
10176 Named_type
* nt
= type
->named_type();
10180 ("method expression requires named type or "
10181 "pointer to named type"));
10182 return Expression::make_error(location
);
10186 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
10187 if (method
== NULL
)
10190 error_at(location
, "type %<%s%> has no method %<%s%>",
10191 nt
->message_name().c_str(),
10192 Gogo::message_name(name
).c_str());
10194 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
10195 Gogo::message_name(name
).c_str(),
10196 nt
->message_name().c_str());
10197 return Expression::make_error(location
);
10200 if (!is_pointer
&& !method
->is_value_method())
10202 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
10203 nt
->message_name().c_str(),
10204 Gogo::message_name(name
).c_str());
10205 return Expression::make_error(location
);
10208 // Build a new function type in which the receiver becomes the first
10210 Function_type
* method_type
= method
->type();
10211 gcc_assert(method_type
->is_method());
10213 const char* const receiver_name
= "$this";
10214 Typed_identifier_list
* parameters
= new Typed_identifier_list();
10215 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
10218 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
10219 if (method_parameters
!= NULL
)
10221 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10222 p
!= method_parameters
->end();
10224 parameters
->push_back(*p
);
10227 const Typed_identifier_list
* method_results
= method_type
->results();
10228 Typed_identifier_list
* results
;
10229 if (method_results
== NULL
)
10233 results
= new Typed_identifier_list();
10234 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
10235 p
!= method_results
->end();
10237 results
->push_back(*p
);
10240 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10242 if (method_type
->is_varargs())
10243 fntype
->set_is_varargs();
10245 // We generate methods which always takes a pointer to the receiver
10246 // as their first argument. If this is for a pointer type, we can
10247 // simply reuse the existing function. We use an internal hack to
10248 // get the right type.
10252 Named_object
* mno
= (method
->needs_stub_method()
10253 ? method
->stub_object()
10254 : method
->named_object());
10255 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10256 f
= Expression::make_cast(fntype
, f
, location
);
10257 Type_conversion_expression
* tce
=
10258 static_cast<Type_conversion_expression
*>(f
);
10259 tce
->set_may_convert_function_types();
10263 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10266 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10267 gcc_assert(vno
!= NULL
);
10268 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10269 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10270 gcc_assert(bm
!= NULL
&& !bm
->is_error_expression());
10272 Expression_list
* args
;
10273 if (method_parameters
== NULL
)
10277 args
= new Expression_list();
10278 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10279 p
!= method_parameters
->end();
10282 vno
= gogo
->lookup(p
->name(), NULL
);
10283 gcc_assert(vno
!= NULL
);
10284 args
->push_back(Expression::make_var_reference(vno
, location
));
10288 Call_expression
* call
= Expression::make_call(bm
, args
,
10289 method_type
->is_varargs(),
10292 size_t count
= call
->result_count();
10295 s
= Statement::make_statement(call
);
10298 Expression_list
* retvals
= new Expression_list();
10300 retvals
->push_back(call
);
10303 for (size_t i
= 0; i
< count
; ++i
)
10304 retvals
->push_back(Expression::make_call_result(call
, i
));
10306 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
10307 retvals
, location
);
10309 gogo
->add_statement(s
);
10311 gogo
->finish_function(location
);
10313 return Expression::make_func_reference(no
, NULL
, location
);
10316 // Make a selector expression.
10319 Expression::make_selector(Expression
* left
, const std::string
& name
,
10320 source_location location
)
10322 return new Selector_expression(left
, name
, location
);
10325 // Implement the builtin function new.
10327 class Allocation_expression
: public Expression
10330 Allocation_expression(Type
* type
, source_location location
)
10331 : Expression(EXPRESSION_ALLOCATION
, location
),
10337 do_traverse(Traverse
* traverse
)
10338 { return Type::traverse(this->type_
, traverse
); }
10342 { return Type::make_pointer_type(this->type_
); }
10345 do_determine_type(const Type_context
*)
10349 do_check_types(Gogo
*);
10353 { return new Allocation_expression(this->type_
, this->location()); }
10356 do_get_tree(Translate_context
*);
10359 // The type we are allocating.
10363 // Check the type of an allocation expression.
10366 Allocation_expression::do_check_types(Gogo
*)
10368 if (this->type_
->function_type() != NULL
)
10369 this->report_error(_("invalid new of function type"));
10372 // Return a tree for an allocation expression.
10375 Allocation_expression::do_get_tree(Translate_context
* context
)
10377 tree type_tree
= this->type_
->get_tree(context
->gogo());
10378 if (type_tree
== error_mark_node
)
10379 return error_mark_node
;
10380 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10381 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10383 if (space
== error_mark_node
)
10384 return error_mark_node
;
10385 return fold_convert(build_pointer_type(type_tree
), space
);
10388 // Make an allocation expression.
10391 Expression::make_allocation(Type
* type
, source_location location
)
10393 return new Allocation_expression(type
, location
);
10396 // Implement the builtin function make.
10398 class Make_expression
: public Expression
10401 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10402 : Expression(EXPRESSION_MAKE
, location
),
10403 type_(type
), args_(args
)
10408 do_traverse(Traverse
* traverse
);
10412 { return this->type_
; }
10415 do_determine_type(const Type_context
*);
10418 do_check_types(Gogo
*);
10423 return new Make_expression(this->type_
, this->args_
->copy(),
10428 do_get_tree(Translate_context
*);
10431 // The type we are making.
10433 // The arguments to pass to the make routine.
10434 Expression_list
* args_
;
10440 Make_expression::do_traverse(Traverse
* traverse
)
10442 if (this->args_
!= NULL
10443 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10444 return TRAVERSE_EXIT
;
10445 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10446 return TRAVERSE_EXIT
;
10447 return TRAVERSE_CONTINUE
;
10450 // Set types of arguments.
10453 Make_expression::do_determine_type(const Type_context
*)
10455 if (this->args_
!= NULL
)
10457 Type_context
context(Type::lookup_integer_type("int"), false);
10458 for (Expression_list::const_iterator pe
= this->args_
->begin();
10459 pe
!= this->args_
->end();
10461 (*pe
)->determine_type(&context
);
10465 // Check types for a make expression.
10468 Make_expression::do_check_types(Gogo
*)
10470 if (this->type_
->channel_type() == NULL
10471 && this->type_
->map_type() == NULL
10472 && (this->type_
->array_type() == NULL
10473 || this->type_
->array_type()->length() != NULL
))
10474 this->report_error(_("invalid type for make function"));
10475 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10476 this->set_is_error();
10479 // Return a tree for a make expression.
10482 Make_expression::do_get_tree(Translate_context
* context
)
10484 return this->type_
->make_expression_tree(context
, this->args_
,
10488 // Make a make expression.
10491 Expression::make_make(Type
* type
, Expression_list
* args
,
10492 source_location location
)
10494 return new Make_expression(type
, args
, location
);
10497 // Construct a struct.
10499 class Struct_construction_expression
: public Expression
10502 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10503 source_location location
)
10504 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10505 type_(type
), vals_(vals
)
10508 // Return whether this is a constant initializer.
10510 is_constant_struct() const;
10514 do_traverse(Traverse
* traverse
);
10518 { return this->type_
; }
10521 do_determine_type(const Type_context
*);
10524 do_check_types(Gogo
*);
10529 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10534 do_is_addressable() const
10538 do_get_tree(Translate_context
*);
10541 do_export(Export
*) const;
10544 // The type of the struct to construct.
10546 // The list of values, in order of the fields in the struct. A NULL
10547 // entry means that the field should be zero-initialized.
10548 Expression_list
* vals_
;
10554 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10556 if (this->vals_
!= NULL
10557 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10558 return TRAVERSE_EXIT
;
10559 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10560 return TRAVERSE_EXIT
;
10561 return TRAVERSE_CONTINUE
;
10564 // Return whether this is a constant initializer.
10567 Struct_construction_expression::is_constant_struct() const
10569 if (this->vals_
== NULL
)
10571 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10572 pv
!= this->vals_
->end();
10576 && !(*pv
)->is_constant()
10577 && (!(*pv
)->is_composite_literal()
10578 || (*pv
)->is_nonconstant_composite_literal()))
10582 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10583 for (Struct_field_list::const_iterator pf
= fields
->begin();
10584 pf
!= fields
->end();
10587 // There are no constant constructors for interfaces.
10588 if (pf
->type()->interface_type() != NULL
)
10595 // Final type determination.
10598 Struct_construction_expression::do_determine_type(const Type_context
*)
10600 if (this->vals_
== NULL
)
10602 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10603 Expression_list::const_iterator pv
= this->vals_
->begin();
10604 for (Struct_field_list::const_iterator pf
= fields
->begin();
10605 pf
!= fields
->end();
10608 if (pv
== this->vals_
->end())
10612 Type_context
subcontext(pf
->type(), false);
10613 (*pv
)->determine_type(&subcontext
);
10616 // Extra values are an error we will report elsewhere; we still want
10617 // to determine the type to avoid knockon errors.
10618 for (; pv
!= this->vals_
->end(); ++pv
)
10619 (*pv
)->determine_type_no_context();
10625 Struct_construction_expression::do_check_types(Gogo
*)
10627 if (this->vals_
== NULL
)
10630 Struct_type
* st
= this->type_
->struct_type();
10631 if (this->vals_
->size() > st
->field_count())
10633 this->report_error(_("too many expressions for struct"));
10637 const Struct_field_list
* fields
= st
->fields();
10638 Expression_list::const_iterator pv
= this->vals_
->begin();
10640 for (Struct_field_list::const_iterator pf
= fields
->begin();
10641 pf
!= fields
->end();
10644 if (pv
== this->vals_
->end())
10646 this->report_error(_("too few expressions for struct"));
10653 std::string reason
;
10654 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10656 if (reason
.empty())
10657 error_at((*pv
)->location(),
10658 "incompatible type for field %d in struct construction",
10661 error_at((*pv
)->location(),
10662 ("incompatible type for field %d in "
10663 "struct construction (%s)"),
10664 i
+ 1, reason
.c_str());
10665 this->set_is_error();
10668 gcc_assert(pv
== this->vals_
->end());
10671 // Return a tree for constructing a struct.
10674 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10676 Gogo
* gogo
= context
->gogo();
10678 if (this->vals_
== NULL
)
10679 return this->type_
->get_init_tree(gogo
, false);
10681 tree type_tree
= this->type_
->get_tree(gogo
);
10682 if (type_tree
== error_mark_node
)
10683 return error_mark_node
;
10684 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10686 bool is_constant
= true;
10687 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10688 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10690 Struct_field_list::const_iterator pf
= fields
->begin();
10691 Expression_list::const_iterator pv
= this->vals_
->begin();
10692 for (tree field
= TYPE_FIELDS(type_tree
);
10693 field
!= NULL_TREE
;
10694 field
= DECL_CHAIN(field
), ++pf
)
10696 gcc_assert(pf
!= fields
->end());
10699 if (pv
== this->vals_
->end())
10700 val
= pf
->type()->get_init_tree(gogo
, false);
10701 else if (*pv
== NULL
)
10703 val
= pf
->type()->get_init_tree(gogo
, false);
10708 val
= Expression::convert_for_assignment(context
, pf
->type(),
10710 (*pv
)->get_tree(context
),
10715 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10716 return error_mark_node
;
10718 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10719 elt
->index
= field
;
10721 if (!TREE_CONSTANT(val
))
10722 is_constant
= false;
10724 gcc_assert(pf
== fields
->end());
10726 tree ret
= build_constructor(type_tree
, elts
);
10728 TREE_CONSTANT(ret
) = 1;
10732 // Export a struct construction.
10735 Struct_construction_expression::do_export(Export
* exp
) const
10737 exp
->write_c_string("convert(");
10738 exp
->write_type(this->type_
);
10739 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10740 pv
!= this->vals_
->end();
10743 exp
->write_c_string(", ");
10745 (*pv
)->export_expression(exp
);
10747 exp
->write_c_string(")");
10750 // Make a struct composite literal. This used by the thunk code.
10753 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10754 source_location location
)
10756 gcc_assert(type
->struct_type() != NULL
);
10757 return new Struct_construction_expression(type
, vals
, location
);
10760 // Construct an array. This class is not used directly; instead we
10761 // use the child classes, Fixed_array_construction_expression and
10762 // Open_array_construction_expression.
10764 class Array_construction_expression
: public Expression
10767 Array_construction_expression(Expression_classification classification
,
10768 Type
* type
, Expression_list
* vals
,
10769 source_location location
)
10770 : Expression(classification
, location
),
10771 type_(type
), vals_(vals
)
10775 // Return whether this is a constant initializer.
10777 is_constant_array() const;
10779 // Return the number of elements.
10781 element_count() const
10782 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10786 do_traverse(Traverse
* traverse
);
10790 { return this->type_
; }
10793 do_determine_type(const Type_context
*);
10796 do_check_types(Gogo
*);
10799 do_is_addressable() const
10803 do_export(Export
*) const;
10805 // The list of values.
10808 { return this->vals_
; }
10810 // Get a constructor tree for the array values.
10812 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10815 // The type of the array to construct.
10817 // The list of values.
10818 Expression_list
* vals_
;
10824 Array_construction_expression::do_traverse(Traverse
* traverse
)
10826 if (this->vals_
!= NULL
10827 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10828 return TRAVERSE_EXIT
;
10829 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10830 return TRAVERSE_EXIT
;
10831 return TRAVERSE_CONTINUE
;
10834 // Return whether this is a constant initializer.
10837 Array_construction_expression::is_constant_array() const
10839 if (this->vals_
== NULL
)
10842 // There are no constant constructors for interfaces.
10843 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10846 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10847 pv
!= this->vals_
->end();
10851 && !(*pv
)->is_constant()
10852 && (!(*pv
)->is_composite_literal()
10853 || (*pv
)->is_nonconstant_composite_literal()))
10859 // Final type determination.
10862 Array_construction_expression::do_determine_type(const Type_context
*)
10864 if (this->vals_
== NULL
)
10866 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10867 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10868 pv
!= this->vals_
->end();
10872 (*pv
)->determine_type(&subcontext
);
10879 Array_construction_expression::do_check_types(Gogo
*)
10881 if (this->vals_
== NULL
)
10884 Array_type
* at
= this->type_
->array_type();
10886 Type
* element_type
= at
->element_type();
10887 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10888 pv
!= this->vals_
->end();
10892 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10894 error_at((*pv
)->location(),
10895 "incompatible type for element %d in composite literal",
10897 this->set_is_error();
10901 Expression
* length
= at
->length();
10902 if (length
!= NULL
)
10907 if (at
->length()->integer_constant_value(true, val
, &type
))
10909 if (this->vals_
->size() > mpz_get_ui(val
))
10910 this->report_error(_("too many elements in composite literal"));
10916 // Get a constructor tree for the array values.
10919 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10922 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10923 (this->vals_
== NULL
10925 : this->vals_
->size()));
10926 Type
* element_type
= this->type_
->array_type()->element_type();
10927 bool is_constant
= true;
10928 if (this->vals_
!= NULL
)
10931 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10932 pv
!= this->vals_
->end();
10935 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
10936 elt
->index
= size_int(i
);
10938 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10941 tree value_tree
= (*pv
)->get_tree(context
);
10942 elt
->value
= Expression::convert_for_assignment(context
,
10948 if (elt
->value
== error_mark_node
)
10949 return error_mark_node
;
10950 if (!TREE_CONSTANT(elt
->value
))
10951 is_constant
= false;
10955 tree ret
= build_constructor(type_tree
, values
);
10957 TREE_CONSTANT(ret
) = 1;
10961 // Export an array construction.
10964 Array_construction_expression::do_export(Export
* exp
) const
10966 exp
->write_c_string("convert(");
10967 exp
->write_type(this->type_
);
10968 if (this->vals_
!= NULL
)
10970 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10971 pv
!= this->vals_
->end();
10974 exp
->write_c_string(", ");
10976 (*pv
)->export_expression(exp
);
10979 exp
->write_c_string(")");
10982 // Construct a fixed array.
10984 class Fixed_array_construction_expression
:
10985 public Array_construction_expression
10988 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
10989 source_location location
)
10990 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
10991 type
, vals
, location
)
10993 gcc_assert(type
->array_type() != NULL
10994 && type
->array_type()->length() != NULL
);
11001 return new Fixed_array_construction_expression(this->type(),
11002 (this->vals() == NULL
11004 : this->vals()->copy()),
11009 do_get_tree(Translate_context
*);
11012 // Return a tree for constructing a fixed array.
11015 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
11017 return this->get_constructor_tree(context
,
11018 this->type()->get_tree(context
->gogo()));
11021 // Construct an open array.
11023 class Open_array_construction_expression
: public Array_construction_expression
11026 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
11027 source_location location
)
11028 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
11029 type
, vals
, location
)
11031 gcc_assert(type
->array_type() != NULL
11032 && type
->array_type()->length() == NULL
);
11036 // Note that taking the address of an open array literal is invalid.
11041 return new Open_array_construction_expression(this->type(),
11042 (this->vals() == NULL
11044 : this->vals()->copy()),
11049 do_get_tree(Translate_context
*);
11052 // Return a tree for constructing an open array.
11055 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
11057 Array_type
* array_type
= this->type()->array_type();
11058 if (array_type
== NULL
)
11060 gcc_assert(this->type()->is_error_type());
11061 return error_mark_node
;
11064 Type
* element_type
= array_type
->element_type();
11065 tree element_type_tree
= element_type
->get_tree(context
->gogo());
11066 if (element_type_tree
== error_mark_node
)
11067 return error_mark_node
;
11071 if (this->vals() == NULL
|| this->vals()->empty())
11073 // We need to create a unique value.
11074 tree max
= size_int(0);
11075 tree constructor_type
= build_array_type(element_type_tree
,
11076 build_index_type(max
));
11077 if (constructor_type
== error_mark_node
)
11078 return error_mark_node
;
11079 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
11080 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
11081 elt
->index
= size_int(0);
11082 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
11083 values
= build_constructor(constructor_type
, vec
);
11084 if (TREE_CONSTANT(elt
->value
))
11085 TREE_CONSTANT(values
) = 1;
11086 length_tree
= size_int(0);
11090 tree max
= size_int(this->vals()->size() - 1);
11091 tree constructor_type
= build_array_type(element_type_tree
,
11092 build_index_type(max
));
11093 if (constructor_type
== error_mark_node
)
11094 return error_mark_node
;
11095 values
= this->get_constructor_tree(context
, constructor_type
);
11096 length_tree
= size_int(this->vals()->size());
11099 if (values
== error_mark_node
)
11100 return error_mark_node
;
11102 bool is_constant_initializer
= TREE_CONSTANT(values
);
11103 bool is_in_function
= context
->function() != NULL
;
11105 if (is_constant_initializer
)
11107 tree tmp
= build_decl(this->location(), VAR_DECL
,
11108 create_tmp_var_name("C"), TREE_TYPE(values
));
11109 DECL_EXTERNAL(tmp
) = 0;
11110 TREE_PUBLIC(tmp
) = 0;
11111 TREE_STATIC(tmp
) = 1;
11112 DECL_ARTIFICIAL(tmp
) = 1;
11113 if (is_in_function
)
11115 // If this is not a function, we will only initialize the
11116 // value once, so we can use this directly rather than
11117 // copying it. In that case we can't make it read-only,
11118 // because the program is permitted to change it.
11119 TREE_READONLY(tmp
) = 1;
11120 TREE_CONSTANT(tmp
) = 1;
11122 DECL_INITIAL(tmp
) = values
;
11123 rest_of_decl_compilation(tmp
, 1, 0);
11129 if (!is_in_function
&& is_constant_initializer
)
11131 // Outside of a function, we know the initializer will only run
11133 space
= build_fold_addr_expr(values
);
11138 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
11139 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
11141 space
= save_expr(space
);
11143 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
11144 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
11145 TREE_THIS_NOTRAP(ref
) = 1;
11146 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
11149 // Build a constructor for the open array.
11151 tree type_tree
= this->type()->get_tree(context
->gogo());
11152 if (type_tree
== error_mark_node
)
11153 return error_mark_node
;
11154 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11156 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
11158 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11159 tree field
= TYPE_FIELDS(type_tree
);
11160 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
11161 elt
->index
= field
;
11162 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
11164 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11165 field
= DECL_CHAIN(field
);
11166 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
11167 elt
->index
= field
;
11168 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11170 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11171 field
= DECL_CHAIN(field
);
11172 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
11173 elt
->index
= field
;
11174 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11176 tree constructor
= build_constructor(type_tree
, init
);
11177 if (constructor
== error_mark_node
)
11178 return error_mark_node
;
11179 if (!is_in_function
&& is_constant_initializer
)
11180 TREE_CONSTANT(constructor
) = 1;
11182 if (set
== NULL_TREE
)
11183 return constructor
;
11185 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
11188 // Make a slice composite literal. This is used by the type
11189 // descriptor code.
11192 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
11193 source_location location
)
11195 gcc_assert(type
->is_open_array_type());
11196 return new Open_array_construction_expression(type
, vals
, location
);
11199 // Construct a map.
11201 class Map_construction_expression
: public Expression
11204 Map_construction_expression(Type
* type
, Expression_list
* vals
,
11205 source_location location
)
11206 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
11207 type_(type
), vals_(vals
)
11208 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
11212 do_traverse(Traverse
* traverse
);
11216 { return this->type_
; }
11219 do_determine_type(const Type_context
*);
11222 do_check_types(Gogo
*);
11227 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
11232 do_get_tree(Translate_context
*);
11235 do_export(Export
*) const;
11238 // The type of the map to construct.
11240 // The list of values.
11241 Expression_list
* vals_
;
11247 Map_construction_expression::do_traverse(Traverse
* traverse
)
11249 if (this->vals_
!= NULL
11250 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11251 return TRAVERSE_EXIT
;
11252 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11253 return TRAVERSE_EXIT
;
11254 return TRAVERSE_CONTINUE
;
11257 // Final type determination.
11260 Map_construction_expression::do_determine_type(const Type_context
*)
11262 if (this->vals_
== NULL
)
11265 Map_type
* mt
= this->type_
->map_type();
11266 Type_context
key_context(mt
->key_type(), false);
11267 Type_context
val_context(mt
->val_type(), false);
11268 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11269 pv
!= this->vals_
->end();
11272 (*pv
)->determine_type(&key_context
);
11274 (*pv
)->determine_type(&val_context
);
11281 Map_construction_expression::do_check_types(Gogo
*)
11283 if (this->vals_
== NULL
)
11286 Map_type
* mt
= this->type_
->map_type();
11288 Type
* key_type
= mt
->key_type();
11289 Type
* val_type
= mt
->val_type();
11290 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11291 pv
!= this->vals_
->end();
11294 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11296 error_at((*pv
)->location(),
11297 "incompatible type for element %d key in map construction",
11299 this->set_is_error();
11302 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11304 error_at((*pv
)->location(),
11305 ("incompatible type for element %d value "
11306 "in map construction"),
11308 this->set_is_error();
11313 // Return a tree for constructing a map.
11316 Map_construction_expression::do_get_tree(Translate_context
* context
)
11318 Gogo
* gogo
= context
->gogo();
11319 source_location loc
= this->location();
11321 Map_type
* mt
= this->type_
->map_type();
11323 // Build a struct to hold the key and value.
11324 tree struct_type
= make_node(RECORD_TYPE
);
11326 Type
* key_type
= mt
->key_type();
11327 tree id
= get_identifier("__key");
11328 tree key_type_tree
= key_type
->get_tree(gogo
);
11329 if (key_type_tree
== error_mark_node
)
11330 return error_mark_node
;
11331 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type_tree
);
11332 DECL_CONTEXT(key_field
) = struct_type
;
11333 TYPE_FIELDS(struct_type
) = key_field
;
11335 Type
* val_type
= mt
->val_type();
11336 id
= get_identifier("__val");
11337 tree val_type_tree
= val_type
->get_tree(gogo
);
11338 if (val_type_tree
== error_mark_node
)
11339 return error_mark_node
;
11340 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type_tree
);
11341 DECL_CONTEXT(val_field
) = struct_type
;
11342 DECL_CHAIN(key_field
) = val_field
;
11344 layout_type(struct_type
);
11346 bool is_constant
= true;
11351 if (this->vals_
== NULL
|| this->vals_
->empty())
11353 valaddr
= null_pointer_node
;
11354 make_tmp
= NULL_TREE
;
11358 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11359 this->vals_
->size() / 2);
11361 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11362 pv
!= this->vals_
->end();
11365 bool one_is_constant
= true;
11367 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11369 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11370 elt
->index
= key_field
;
11371 tree val_tree
= (*pv
)->get_tree(context
);
11372 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11375 if (elt
->value
== error_mark_node
)
11376 return error_mark_node
;
11377 if (!TREE_CONSTANT(elt
->value
))
11378 one_is_constant
= false;
11382 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11383 elt
->index
= val_field
;
11384 val_tree
= (*pv
)->get_tree(context
);
11385 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11388 if (elt
->value
== error_mark_node
)
11389 return error_mark_node
;
11390 if (!TREE_CONSTANT(elt
->value
))
11391 one_is_constant
= false;
11393 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11394 elt
->index
= size_int(i
);
11395 elt
->value
= build_constructor(struct_type
, one
);
11396 if (one_is_constant
)
11397 TREE_CONSTANT(elt
->value
) = 1;
11399 is_constant
= false;
11402 tree index_type
= build_index_type(size_int(i
- 1));
11403 tree array_type
= build_array_type(struct_type
, index_type
);
11404 tree init
= build_constructor(array_type
, values
);
11406 TREE_CONSTANT(init
) = 1;
11408 if (current_function_decl
!= NULL
)
11410 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11411 DECL_INITIAL(tmp
) = init
;
11412 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11413 TREE_ADDRESSABLE(tmp
) = 1;
11417 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11418 DECL_EXTERNAL(tmp
) = 0;
11419 TREE_PUBLIC(tmp
) = 0;
11420 TREE_STATIC(tmp
) = 1;
11421 DECL_ARTIFICIAL(tmp
) = 1;
11422 if (!TREE_CONSTANT(init
))
11423 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11427 TREE_READONLY(tmp
) = 1;
11428 TREE_CONSTANT(tmp
) = 1;
11429 DECL_INITIAL(tmp
) = init
;
11430 make_tmp
= NULL_TREE
;
11432 rest_of_decl_compilation(tmp
, 1, 0);
11435 valaddr
= build_fold_addr_expr(tmp
);
11438 tree descriptor
= gogo
->map_descriptor(mt
);
11440 tree type_tree
= this->type_
->get_tree(gogo
);
11441 if (type_tree
== error_mark_node
)
11442 return error_mark_node
;
11444 static tree construct_map_fndecl
;
11445 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11447 "__go_construct_map",
11450 TREE_TYPE(descriptor
),
11455 TYPE_SIZE_UNIT(struct_type
),
11457 byte_position(val_field
),
11459 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11460 const_ptr_type_node
,
11461 fold_convert(const_ptr_type_node
, valaddr
));
11462 if (call
== error_mark_node
)
11463 return error_mark_node
;
11466 if (make_tmp
== NULL
)
11469 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11473 // Export an array construction.
11476 Map_construction_expression::do_export(Export
* exp
) const
11478 exp
->write_c_string("convert(");
11479 exp
->write_type(this->type_
);
11480 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11481 pv
!= this->vals_
->end();
11484 exp
->write_c_string(", ");
11485 (*pv
)->export_expression(exp
);
11487 exp
->write_c_string(")");
11490 // A general composite literal. This is lowered to a type specific
11493 class Composite_literal_expression
: public Parser_expression
11496 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11497 Expression_list
* vals
, source_location location
)
11498 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11499 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11504 do_traverse(Traverse
* traverse
);
11507 do_lower(Gogo
*, Named_object
*, int);
11512 return new Composite_literal_expression(this->type_
, this->depth_
,
11514 (this->vals_
== NULL
11516 : this->vals_
->copy()),
11522 lower_struct(Type
*);
11525 lower_array(Type
*);
11528 make_array(Type
*, Expression_list
*);
11531 lower_map(Gogo
*, Named_object
*, Type
*);
11533 // The type of the composite literal.
11535 // The depth within a list of composite literals within a composite
11536 // literal, when the type is omitted.
11538 // The values to put in the composite literal.
11539 Expression_list
* vals_
;
11540 // If this is true, then VALS_ is a list of pairs: a key and a
11541 // value. In an array initializer, a missing key will be NULL.
11548 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11550 if (this->vals_
!= NULL
11551 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11552 return TRAVERSE_EXIT
;
11553 return Type::traverse(this->type_
, traverse
);
11556 // Lower a generic composite literal into a specific version based on
11560 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11562 Type
* type
= this->type_
;
11564 for (int depth
= this->depth_
; depth
> 0; --depth
)
11566 if (type
->array_type() != NULL
)
11567 type
= type
->array_type()->element_type();
11568 else if (type
->map_type() != NULL
)
11569 type
= type
->map_type()->val_type();
11572 if (!type
->is_error_type())
11573 error_at(this->location(),
11574 ("may only omit types within composite literals "
11575 "of slice, array, or map type"));
11576 return Expression::make_error(this->location());
11580 if (type
->is_error_type())
11581 return Expression::make_error(this->location());
11582 else if (type
->struct_type() != NULL
)
11583 return this->lower_struct(type
);
11584 else if (type
->array_type() != NULL
)
11585 return this->lower_array(type
);
11586 else if (type
->map_type() != NULL
)
11587 return this->lower_map(gogo
, function
, type
);
11590 error_at(this->location(),
11591 ("expected struct, slice, array, or map type "
11592 "for composite literal"));
11593 return Expression::make_error(this->location());
11597 // Lower a struct composite literal.
11600 Composite_literal_expression::lower_struct(Type
* type
)
11602 source_location location
= this->location();
11603 Struct_type
* st
= type
->struct_type();
11604 if (this->vals_
== NULL
|| !this->has_keys_
)
11605 return new Struct_construction_expression(type
, this->vals_
, location
);
11607 size_t field_count
= st
->field_count();
11608 std::vector
<Expression
*> vals(field_count
);
11609 Expression_list::const_iterator p
= this->vals_
->begin();
11610 while (p
!= this->vals_
->end())
11612 Expression
* name_expr
= *p
;
11615 gcc_assert(p
!= this->vals_
->end());
11616 Expression
* val
= *p
;
11620 if (name_expr
== NULL
)
11622 error_at(val
->location(), "mixture of field and value initializers");
11623 return Expression::make_error(location
);
11626 bool bad_key
= false;
11628 switch (name_expr
->classification())
11630 case EXPRESSION_UNKNOWN_REFERENCE
:
11631 name
= name_expr
->unknown_expression()->name();
11634 case EXPRESSION_CONST_REFERENCE
:
11635 name
= static_cast<Const_expression
*>(name_expr
)->name();
11638 case EXPRESSION_TYPE
:
11640 Type
* t
= name_expr
->type();
11641 Named_type
* nt
= t
->named_type();
11649 case EXPRESSION_VAR_REFERENCE
:
11650 name
= name_expr
->var_expression()->name();
11653 case EXPRESSION_FUNC_REFERENCE
:
11654 name
= name_expr
->func_expression()->name();
11657 case EXPRESSION_UNARY
:
11658 // If there is a local variable around with the same name as
11659 // the field, and this occurs in the closure, then the
11660 // parser may turn the field reference into an indirection
11661 // through the closure. FIXME: This is a mess.
11664 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11665 if (ue
->op() == OPERATOR_MULT
)
11667 Field_reference_expression
* fre
=
11668 ue
->operand()->field_reference_expression();
11672 fre
->expr()->type()->deref()->struct_type();
11675 const Struct_field
* sf
= st
->field(fre
->field_index());
11676 name
= sf
->field_name();
11678 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11679 size_t buflen
= strlen(buf
);
11680 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11683 name
= name
.substr(0, name
.length() - buflen
);
11698 error_at(name_expr
->location(), "expected struct field name");
11699 return Expression::make_error(location
);
11702 unsigned int index
;
11703 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11706 error_at(name_expr
->location(), "unknown field %qs in %qs",
11707 Gogo::message_name(name
).c_str(),
11708 (type
->named_type() != NULL
11709 ? type
->named_type()->message_name().c_str()
11710 : "unnamed struct"));
11711 return Expression::make_error(location
);
11713 if (vals
[index
] != NULL
)
11715 error_at(name_expr
->location(),
11716 "duplicate value for field %qs in %qs",
11717 Gogo::message_name(name
).c_str(),
11718 (type
->named_type() != NULL
11719 ? type
->named_type()->message_name().c_str()
11720 : "unnamed struct"));
11721 return Expression::make_error(location
);
11727 Expression_list
* list
= new Expression_list
;
11728 list
->reserve(field_count
);
11729 for (size_t i
= 0; i
< field_count
; ++i
)
11730 list
->push_back(vals
[i
]);
11732 return new Struct_construction_expression(type
, list
, location
);
11735 // Lower an array composite literal.
11738 Composite_literal_expression::lower_array(Type
* type
)
11740 source_location location
= this->location();
11741 if (this->vals_
== NULL
|| !this->has_keys_
)
11742 return this->make_array(type
, this->vals_
);
11744 std::vector
<Expression
*> vals
;
11745 vals
.reserve(this->vals_
->size());
11746 unsigned long index
= 0;
11747 Expression_list::const_iterator p
= this->vals_
->begin();
11748 while (p
!= this->vals_
->end())
11750 Expression
* index_expr
= *p
;
11753 gcc_assert(p
!= this->vals_
->end());
11754 Expression
* val
= *p
;
11758 if (index_expr
!= NULL
)
11763 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11766 error_at(index_expr
->location(),
11767 "index expression is not integer constant");
11768 return Expression::make_error(location
);
11770 if (mpz_sgn(ival
) < 0)
11773 error_at(index_expr
->location(), "index expression is negative");
11774 return Expression::make_error(location
);
11776 index
= mpz_get_ui(ival
);
11777 if (mpz_cmp_ui(ival
, index
) != 0)
11780 error_at(index_expr
->location(), "index value overflow");
11781 return Expression::make_error(location
);
11786 if (index
== vals
.size())
11787 vals
.push_back(val
);
11790 if (index
> vals
.size())
11792 vals
.reserve(index
+ 32);
11793 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11795 if (vals
[index
] != NULL
)
11797 error_at((index_expr
!= NULL
11798 ? index_expr
->location()
11799 : val
->location()),
11800 "duplicate value for index %lu",
11802 return Expression::make_error(location
);
11810 size_t size
= vals
.size();
11811 Expression_list
* list
= new Expression_list
;
11812 list
->reserve(size
);
11813 for (size_t i
= 0; i
< size
; ++i
)
11814 list
->push_back(vals
[i
]);
11816 return this->make_array(type
, list
);
11819 // Actually build the array composite literal. This handles
11823 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11825 source_location location
= this->location();
11826 Array_type
* at
= type
->array_type();
11827 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11829 size_t size
= vals
== NULL
? 0 : vals
->size();
11831 mpz_init_set_ui(vlen
, size
);
11832 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11834 at
= Type::make_array_type(at
->element_type(), elen
);
11837 if (at
->length() != NULL
)
11838 return new Fixed_array_construction_expression(type
, vals
, location
);
11840 return new Open_array_construction_expression(type
, vals
, location
);
11843 // Lower a map composite literal.
11846 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
11849 source_location location
= this->location();
11850 if (this->vals_
!= NULL
)
11852 if (!this->has_keys_
)
11854 error_at(location
, "map composite literal must have keys");
11855 return Expression::make_error(location
);
11858 for (Expression_list::iterator p
= this->vals_
->begin();
11859 p
!= this->vals_
->end();
11865 error_at((*p
)->location(),
11866 "map composite literal must have keys for every value");
11867 return Expression::make_error(location
);
11869 // Make sure we have lowered the key; it may not have been
11870 // lowered in order to handle keys for struct composite
11871 // literals. Lower it now to get the right error message.
11872 if ((*p
)->unknown_expression() != NULL
)
11874 (*p
)->unknown_expression()->clear_is_composite_literal_key();
11875 gogo
->lower_expression(function
, &*p
);
11876 gcc_assert((*p
)->is_error_expression());
11877 return Expression::make_error(location
);
11882 return new Map_construction_expression(type
, this->vals_
, location
);
11885 // Make a composite literal expression.
11888 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11889 Expression_list
* vals
,
11890 source_location location
)
11892 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11896 // Return whether this expression is a composite literal.
11899 Expression::is_composite_literal() const
11901 switch (this->classification_
)
11903 case EXPRESSION_COMPOSITE_LITERAL
:
11904 case EXPRESSION_STRUCT_CONSTRUCTION
:
11905 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11906 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11907 case EXPRESSION_MAP_CONSTRUCTION
:
11914 // Return whether this expression is a composite literal which is not
11918 Expression::is_nonconstant_composite_literal() const
11920 switch (this->classification_
)
11922 case EXPRESSION_STRUCT_CONSTRUCTION
:
11924 const Struct_construction_expression
*psce
=
11925 static_cast<const Struct_construction_expression
*>(this);
11926 return !psce
->is_constant_struct();
11928 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11930 const Fixed_array_construction_expression
*pace
=
11931 static_cast<const Fixed_array_construction_expression
*>(this);
11932 return !pace
->is_constant_array();
11934 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11936 const Open_array_construction_expression
*pace
=
11937 static_cast<const Open_array_construction_expression
*>(this);
11938 return !pace
->is_constant_array();
11940 case EXPRESSION_MAP_CONSTRUCTION
:
11947 // Return true if this is a reference to a local variable.
11950 Expression::is_local_variable() const
11952 const Var_expression
* ve
= this->var_expression();
11955 const Named_object
* no
= ve
->named_object();
11956 return (no
->is_result_variable()
11957 || (no
->is_variable() && !no
->var_value()->is_global()));
11960 // Class Type_guard_expression.
11965 Type_guard_expression::do_traverse(Traverse
* traverse
)
11967 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
11968 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11969 return TRAVERSE_EXIT
;
11970 return TRAVERSE_CONTINUE
;
11973 // Check types of a type guard expression. The expression must have
11974 // an interface type, but the actual type conversion is checked at run
11978 Type_guard_expression::do_check_types(Gogo
*)
11980 // 6g permits using a type guard with unsafe.pointer; we are
11982 Type
* expr_type
= this->expr_
->type();
11983 if (expr_type
->is_unsafe_pointer_type())
11985 if (this->type_
->points_to() == NULL
11986 && (this->type_
->integer_type() == NULL
11987 || (this->type_
->forwarded()
11988 != Type::lookup_integer_type("uintptr"))))
11989 this->report_error(_("invalid unsafe.Pointer conversion"));
11991 else if (this->type_
->is_unsafe_pointer_type())
11993 if (expr_type
->points_to() == NULL
11994 && (expr_type
->integer_type() == NULL
11995 || (expr_type
->forwarded()
11996 != Type::lookup_integer_type("uintptr"))))
11997 this->report_error(_("invalid unsafe.Pointer conversion"));
11999 else if (expr_type
->interface_type() == NULL
)
12001 if (!expr_type
->is_error_type() && !this->type_
->is_error_type())
12002 this->report_error(_("type assertion only valid for interface types"));
12003 this->set_is_error();
12005 else if (this->type_
->interface_type() == NULL
)
12007 std::string reason
;
12008 if (!expr_type
->interface_type()->implements_interface(this->type_
,
12011 if (!this->type_
->is_error_type())
12013 if (reason
.empty())
12014 this->report_error(_("impossible type assertion: "
12015 "type does not implement interface"));
12017 error_at(this->location(),
12018 ("impossible type assertion: "
12019 "type does not implement interface (%s)"),
12022 this->set_is_error();
12027 // Return a tree for a type guard expression.
12030 Type_guard_expression::do_get_tree(Translate_context
* context
)
12032 Gogo
* gogo
= context
->gogo();
12033 tree expr_tree
= this->expr_
->get_tree(context
);
12034 if (expr_tree
== error_mark_node
)
12035 return error_mark_node
;
12036 Type
* expr_type
= this->expr_
->type();
12037 if ((this->type_
->is_unsafe_pointer_type()
12038 && (expr_type
->points_to() != NULL
12039 || expr_type
->integer_type() != NULL
))
12040 || (expr_type
->is_unsafe_pointer_type()
12041 && this->type_
->points_to() != NULL
))
12042 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
12043 else if (expr_type
->is_unsafe_pointer_type()
12044 && this->type_
->integer_type() != NULL
)
12045 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
12046 else if (this->type_
->interface_type() != NULL
)
12047 return Expression::convert_interface_to_interface(context
, this->type_
,
12048 this->expr_
->type(),
12052 return Expression::convert_for_assignment(context
, this->type_
,
12053 this->expr_
->type(), expr_tree
,
12057 // Make a type guard expression.
12060 Expression::make_type_guard(Expression
* expr
, Type
* type
,
12061 source_location location
)
12063 return new Type_guard_expression(expr
, type
, location
);
12066 // Class Heap_composite_expression.
12068 // When you take the address of a composite literal, it is allocated
12069 // on the heap. This class implements that.
12071 class Heap_composite_expression
: public Expression
12074 Heap_composite_expression(Expression
* expr
, source_location location
)
12075 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
12081 do_traverse(Traverse
* traverse
)
12082 { return Expression::traverse(&this->expr_
, traverse
); }
12086 { return Type::make_pointer_type(this->expr_
->type()); }
12089 do_determine_type(const Type_context
*)
12090 { this->expr_
->determine_type_no_context(); }
12095 return Expression::make_heap_composite(this->expr_
->copy(),
12100 do_get_tree(Translate_context
*);
12102 // We only export global objects, and the parser does not generate
12103 // this in global scope.
12105 do_export(Export
*) const
12106 { gcc_unreachable(); }
12109 // The composite literal which is being put on the heap.
12113 // Return a tree which allocates a composite literal on the heap.
12116 Heap_composite_expression::do_get_tree(Translate_context
* context
)
12118 tree expr_tree
= this->expr_
->get_tree(context
);
12119 if (expr_tree
== error_mark_node
)
12120 return error_mark_node
;
12121 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
12122 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
12123 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
12124 expr_size
, this->location());
12125 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
12126 space
= save_expr(space
);
12127 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
12128 TREE_THIS_NOTRAP(ref
) = 1;
12129 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
12130 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
12132 SET_EXPR_LOCATION(ret
, this->location());
12136 // Allocate a composite literal on the heap.
12139 Expression::make_heap_composite(Expression
* expr
, source_location location
)
12141 return new Heap_composite_expression(expr
, location
);
12144 // Class Receive_expression.
12146 // Return the type of a receive expression.
12149 Receive_expression::do_type()
12151 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12152 if (channel_type
== NULL
)
12153 return Type::make_error_type();
12154 return channel_type
->element_type();
12157 // Check types for a receive expression.
12160 Receive_expression::do_check_types(Gogo
*)
12162 Type
* type
= this->channel_
->type();
12163 if (type
->is_error_type())
12165 this->set_is_error();
12168 if (type
->channel_type() == NULL
)
12170 this->report_error(_("expected channel"));
12173 if (!type
->channel_type()->may_receive())
12175 this->report_error(_("invalid receive on send-only channel"));
12180 // Get a tree for a receive expression.
12183 Receive_expression::do_get_tree(Translate_context
* context
)
12185 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12186 gcc_assert(channel_type
!= NULL
);
12187 Type
* element_type
= channel_type
->element_type();
12188 tree element_type_tree
= element_type
->get_tree(context
->gogo());
12190 tree channel
= this->channel_
->get_tree(context
);
12191 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
12192 return error_mark_node
;
12194 return Gogo::receive_from_channel(element_type_tree
, channel
,
12195 this->for_select_
, this->location());
12198 // Make a receive expression.
12200 Receive_expression
*
12201 Expression::make_receive(Expression
* channel
, source_location location
)
12203 return new Receive_expression(channel
, location
);
12206 // Class Send_expression.
12211 Send_expression::do_traverse(Traverse
* traverse
)
12213 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
12214 return TRAVERSE_EXIT
;
12215 return Expression::traverse(&this->val_
, traverse
);
12221 Send_expression::do_type()
12223 return Type::lookup_bool_type();
12229 Send_expression::do_determine_type(const Type_context
*)
12231 this->channel_
->determine_type_no_context();
12233 Type
* type
= this->channel_
->type();
12234 Type_context subcontext
;
12235 if (type
->channel_type() != NULL
)
12236 subcontext
.type
= type
->channel_type()->element_type();
12237 this->val_
->determine_type(&subcontext
);
12243 Send_expression::do_check_types(Gogo
*)
12245 Type
* type
= this->channel_
->type();
12246 if (type
->is_error_type())
12248 this->set_is_error();
12251 Channel_type
* channel_type
= type
->channel_type();
12252 if (channel_type
== NULL
)
12254 error_at(this->location(), "left operand of %<<-%> must be channel");
12255 this->set_is_error();
12258 Type
* element_type
= channel_type
->element_type();
12259 if (element_type
!= NULL
12260 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
12262 this->report_error(_("incompatible types in send"));
12265 if (!channel_type
->may_send())
12267 this->report_error(_("invalid send on receive-only channel"));
12272 // Get a tree for a send expression.
12275 Send_expression::do_get_tree(Translate_context
* context
)
12277 tree channel
= this->channel_
->get_tree(context
);
12278 tree val
= this->val_
->get_tree(context
);
12279 if (channel
== error_mark_node
|| val
== error_mark_node
)
12280 return error_mark_node
;
12281 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12282 val
= Expression::convert_for_assignment(context
,
12283 channel_type
->element_type(),
12284 this->val_
->type(),
12287 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
12288 this->for_select_
, this->location());
12291 // Make a send expression
12294 Expression::make_send(Expression
* channel
, Expression
* val
,
12295 source_location location
)
12297 return new Send_expression(channel
, val
, location
);
12300 // An expression which evaluates to a pointer to the type descriptor
12303 class Type_descriptor_expression
: public Expression
12306 Type_descriptor_expression(Type
* type
, source_location location
)
12307 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12314 { return Type::make_type_descriptor_ptr_type(); }
12317 do_determine_type(const Type_context
*)
12325 do_get_tree(Translate_context
* context
)
12326 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12329 // The type for which this is the descriptor.
12333 // Make a type descriptor expression.
12336 Expression::make_type_descriptor(Type
* type
, source_location location
)
12338 return new Type_descriptor_expression(type
, location
);
12341 // An expression which evaluates to some characteristic of a type.
12342 // This is only used to initialize fields of a type descriptor. Using
12343 // a new expression class is slightly inefficient but gives us a good
12344 // separation between the frontend and the middle-end with regard to
12345 // how types are laid out.
12347 class Type_info_expression
: public Expression
12350 Type_info_expression(Type
* type
, Type_info type_info
)
12351 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12352 type_(type
), type_info_(type_info
)
12360 do_determine_type(const Type_context
*)
12368 do_get_tree(Translate_context
* context
);
12371 // The type for which we are getting information.
12373 // What information we want.
12374 Type_info type_info_
;
12377 // The type is chosen to match what the type descriptor struct
12381 Type_info_expression::do_type()
12383 switch (this->type_info_
)
12385 case TYPE_INFO_SIZE
:
12386 return Type::lookup_integer_type("uintptr");
12387 case TYPE_INFO_ALIGNMENT
:
12388 case TYPE_INFO_FIELD_ALIGNMENT
:
12389 return Type::lookup_integer_type("uint8");
12395 // Return type information in GENERIC.
12398 Type_info_expression::do_get_tree(Translate_context
* context
)
12400 tree type_tree
= this->type_
->get_tree(context
->gogo());
12401 if (type_tree
== error_mark_node
)
12402 return error_mark_node
;
12404 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12405 gcc_assert(val_type_tree
!= error_mark_node
);
12407 if (this->type_info_
== TYPE_INFO_SIZE
)
12408 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12409 TYPE_SIZE_UNIT(type_tree
));
12413 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12414 val
= go_type_alignment(type_tree
);
12416 val
= go_field_alignment(type_tree
);
12417 return build_int_cstu(val_type_tree
, val
);
12421 // Make a type info expression.
12424 Expression::make_type_info(Type
* type
, Type_info type_info
)
12426 return new Type_info_expression(type
, type_info
);
12429 // An expression which evaluates to the offset of a field within a
12430 // struct. This, like Type_info_expression, q.v., is only used to
12431 // initialize fields of a type descriptor.
12433 class Struct_field_offset_expression
: public Expression
12436 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12437 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12438 type_(type
), field_(field
)
12444 { return Type::lookup_integer_type("uintptr"); }
12447 do_determine_type(const Type_context
*)
12455 do_get_tree(Translate_context
* context
);
12458 // The type of the struct.
12459 Struct_type
* type_
;
12461 const Struct_field
* field_
;
12464 // Return a struct field offset in GENERIC.
12467 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12469 tree type_tree
= this->type_
->get_tree(context
->gogo());
12470 if (type_tree
== error_mark_node
)
12471 return error_mark_node
;
12473 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12474 gcc_assert(val_type_tree
!= error_mark_node
);
12476 const Struct_field_list
* fields
= this->type_
->fields();
12477 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12478 Struct_field_list::const_iterator p
;
12479 for (p
= fields
->begin();
12480 p
!= fields
->end();
12481 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12483 gcc_assert(struct_field_tree
!= NULL_TREE
);
12484 if (&*p
== this->field_
)
12487 gcc_assert(&*p
== this->field_
);
12489 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12490 byte_position(struct_field_tree
));
12493 // Make an expression for a struct field offset.
12496 Expression::make_struct_field_offset(Struct_type
* type
,
12497 const Struct_field
* field
)
12499 return new Struct_field_offset_expression(type
, field
);
12502 // An expression which evaluates to the address of an unnamed label.
12504 class Label_addr_expression
: public Expression
12507 Label_addr_expression(Label
* label
, source_location location
)
12508 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12515 { return Type::make_pointer_type(Type::make_void_type()); }
12518 do_determine_type(const Type_context
*)
12523 { return new Label_addr_expression(this->label_
, this->location()); }
12526 do_get_tree(Translate_context
*)
12527 { return this->label_
->get_addr(this->location()); }
12530 // The label whose address we are taking.
12534 // Make an expression for the address of an unnamed label.
12537 Expression::make_label_addr(Label
* label
, source_location location
)
12539 return new Label_addr_expression(label
, location
);
12542 // Import an expression. This comes at the end in order to see the
12543 // various class definitions.
12546 Expression::import_expression(Import
* imp
)
12548 int c
= imp
->peek_char();
12549 if (imp
->match_c_string("- ")
12550 || imp
->match_c_string("! ")
12551 || imp
->match_c_string("^ "))
12552 return Unary_expression::do_import(imp
);
12554 return Binary_expression::do_import(imp
);
12555 else if (imp
->match_c_string("true")
12556 || imp
->match_c_string("false"))
12557 return Boolean_expression::do_import(imp
);
12559 return String_expression::do_import(imp
);
12560 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12562 // This handles integers, floats and complex constants.
12563 return Integer_expression::do_import(imp
);
12565 else if (imp
->match_c_string("nil"))
12566 return Nil_expression::do_import(imp
);
12567 else if (imp
->match_c_string("convert"))
12568 return Type_conversion_expression::do_import(imp
);
12571 error_at(imp
->location(), "import error: expected expression");
12572 return Expression::make_error(imp
->location());
12576 // Class Expression_list.
12578 // Traverse the list.
12581 Expression_list::traverse(Traverse
* traverse
)
12583 for (Expression_list::iterator p
= this->begin();
12589 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12590 return TRAVERSE_EXIT
;
12593 return TRAVERSE_CONTINUE
;
12599 Expression_list::copy()
12601 Expression_list
* ret
= new Expression_list();
12602 for (Expression_list::iterator p
= this->begin();
12607 ret
->push_back(NULL
);
12609 ret
->push_back((*p
)->copy());
12614 // Return whether an expression list has an error expression.
12617 Expression_list::contains_error() const
12619 for (Expression_list::const_iterator p
= this->begin();
12622 if (*p
!= NULL
&& (*p
)->is_error_expression())