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_abstract() && left_type
->named_type() != NULL
)
5401 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5403 else if (!left_type
->is_abstract())
5405 else if (!right_type
->is_abstract())
5407 else if (left_type
->complex_type() != NULL
)
5409 else if (right_type
->complex_type() != NULL
)
5411 else if (left_type
->float_type() != NULL
)
5413 else if (right_type
->float_type() != NULL
)
5419 case OPERATOR_LSHIFT
:
5420 case OPERATOR_RSHIFT
:
5421 return this->left_
->type();
5428 // Set type for a binary expression.
5431 Binary_expression::do_determine_type(const Type_context
* context
)
5433 Type
* tleft
= this->left_
->type();
5434 Type
* tright
= this->right_
->type();
5436 // Both sides should have the same type, except for the shift
5437 // operations. For a comparison, we should ignore the incoming
5440 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5441 || this->op_
== OPERATOR_RSHIFT
);
5443 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5444 || this->op_
== OPERATOR_NOTEQ
5445 || this->op_
== OPERATOR_LT
5446 || this->op_
== OPERATOR_LE
5447 || this->op_
== OPERATOR_GT
5448 || this->op_
== OPERATOR_GE
);
5450 Type_context
subcontext(*context
);
5454 // In a comparison, the context does not determine the types of
5456 subcontext
.type
= NULL
;
5459 // Set the context for the left hand operand.
5462 // The right hand operand plays no role in determining the type
5463 // of the left hand operand. A shift of an abstract integer in
5464 // a string context gets special treatment, which may be a
5466 if (subcontext
.type
!= NULL
5467 && subcontext
.type
->is_string_type()
5468 && tleft
->is_abstract())
5469 error_at(this->location(), "shift of non-integer operand");
5471 else if (!tleft
->is_abstract())
5472 subcontext
.type
= tleft
;
5473 else if (!tright
->is_abstract())
5474 subcontext
.type
= tright
;
5475 else if (subcontext
.type
== NULL
)
5477 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5478 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5479 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5481 // Both sides have an abstract integer, abstract float, or
5482 // abstract complex type. Just let CONTEXT determine
5483 // whether they may remain abstract or not.
5485 else if (tleft
->complex_type() != NULL
)
5486 subcontext
.type
= tleft
;
5487 else if (tright
->complex_type() != NULL
)
5488 subcontext
.type
= tright
;
5489 else if (tleft
->float_type() != NULL
)
5490 subcontext
.type
= tleft
;
5491 else if (tright
->float_type() != NULL
)
5492 subcontext
.type
= tright
;
5494 subcontext
.type
= tleft
;
5497 this->left_
->determine_type(&subcontext
);
5499 // The context for the right hand operand is the same as for the
5500 // left hand operand, except for a shift operator.
5503 subcontext
.type
= Type::lookup_integer_type("uint");
5504 subcontext
.may_be_abstract
= false;
5507 this->right_
->determine_type(&subcontext
);
5510 // Report an error if the binary operator OP does not support TYPE.
5511 // Return whether the operation is OK. This should not be used for
5515 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5516 source_location location
)
5521 case OPERATOR_ANDAND
:
5522 if (!type
->is_boolean_type())
5524 error_at(location
, "expected boolean type");
5530 case OPERATOR_NOTEQ
:
5531 if (type
->integer_type() == NULL
5532 && type
->float_type() == NULL
5533 && type
->complex_type() == NULL
5534 && !type
->is_string_type()
5535 && type
->points_to() == NULL
5536 && !type
->is_nil_type()
5537 && !type
->is_boolean_type()
5538 && type
->interface_type() == NULL
5539 && (type
->array_type() == NULL
5540 || type
->array_type()->length() != NULL
)
5541 && type
->map_type() == NULL
5542 && type
->channel_type() == NULL
5543 && type
->function_type() == NULL
)
5546 ("expected integer, floating, complex, string, pointer, "
5547 "boolean, interface, slice, map, channel, "
5548 "or function type"));
5557 if (type
->integer_type() == NULL
5558 && type
->float_type() == NULL
5559 && !type
->is_string_type())
5561 error_at(location
, "expected integer, floating, or string type");
5567 case OPERATOR_PLUSEQ
:
5568 if (type
->integer_type() == NULL
5569 && type
->float_type() == NULL
5570 && type
->complex_type() == NULL
5571 && !type
->is_string_type())
5574 "expected integer, floating, complex, or string type");
5579 case OPERATOR_MINUS
:
5580 case OPERATOR_MINUSEQ
:
5582 case OPERATOR_MULTEQ
:
5584 case OPERATOR_DIVEQ
:
5585 if (type
->integer_type() == NULL
5586 && type
->float_type() == NULL
5587 && type
->complex_type() == NULL
)
5589 error_at(location
, "expected integer, floating, or complex type");
5595 case OPERATOR_MODEQ
:
5599 case OPERATOR_ANDEQ
:
5601 case OPERATOR_XOREQ
:
5602 case OPERATOR_BITCLEAR
:
5603 case OPERATOR_BITCLEAREQ
:
5604 if (type
->integer_type() == NULL
)
5606 error_at(location
, "expected integer type");
5621 Binary_expression::do_check_types(Gogo
*)
5623 Type
* left_type
= this->left_
->type();
5624 Type
* right_type
= this->right_
->type();
5625 if (left_type
->is_error_type() || right_type
->is_error_type())
5627 this->set_is_error();
5631 if (this->op_
== OPERATOR_EQEQ
5632 || this->op_
== OPERATOR_NOTEQ
5633 || this->op_
== OPERATOR_LT
5634 || this->op_
== OPERATOR_LE
5635 || this->op_
== OPERATOR_GT
5636 || this->op_
== OPERATOR_GE
)
5638 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5639 && !Type::are_assignable(right_type
, left_type
, NULL
))
5641 this->report_error(_("incompatible types in binary expression"));
5644 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5646 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5649 this->set_is_error();
5653 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5655 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5657 this->report_error(_("incompatible types in binary expression"));
5660 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5663 this->set_is_error();
5669 if (left_type
->integer_type() == NULL
)
5670 this->report_error(_("shift of non-integer operand"));
5672 if (!right_type
->is_abstract()
5673 && (right_type
->integer_type() == NULL
5674 || !right_type
->integer_type()->is_unsigned()))
5675 this->report_error(_("shift count not unsigned integer"));
5681 if (this->right_
->integer_constant_value(true, val
, &type
))
5683 if (mpz_sgn(val
) < 0)
5684 this->report_error(_("negative shift count"));
5691 // Get a tree for a binary expression.
5694 Binary_expression::do_get_tree(Translate_context
* context
)
5696 tree left
= this->left_
->get_tree(context
);
5697 tree right
= this->right_
->get_tree(context
);
5699 if (left
== error_mark_node
|| right
== error_mark_node
)
5700 return error_mark_node
;
5702 enum tree_code code
;
5703 bool use_left_type
= true;
5704 bool is_shift_op
= false;
5708 case OPERATOR_NOTEQ
:
5713 return Expression::comparison_tree(context
, this->op_
,
5714 this->left_
->type(), left
,
5715 this->right_
->type(), right
,
5719 code
= TRUTH_ORIF_EXPR
;
5720 use_left_type
= false;
5722 case OPERATOR_ANDAND
:
5723 code
= TRUTH_ANDIF_EXPR
;
5724 use_left_type
= false;
5729 case OPERATOR_MINUS
:
5733 code
= BIT_IOR_EXPR
;
5736 code
= BIT_XOR_EXPR
;
5743 Type
*t
= this->left_
->type();
5744 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5747 code
= TRUNC_DIV_EXPR
;
5751 code
= TRUNC_MOD_EXPR
;
5753 case OPERATOR_LSHIFT
:
5757 case OPERATOR_RSHIFT
:
5762 code
= BIT_AND_EXPR
;
5764 case OPERATOR_BITCLEAR
:
5765 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5766 code
= BIT_AND_EXPR
;
5772 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5774 if (this->left_
->type()->is_string_type())
5776 gcc_assert(this->op_
== OPERATOR_PLUS
);
5777 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5778 static tree string_plus_decl
;
5779 return Gogo::call_builtin(&string_plus_decl
,
5790 tree compute_type
= excess_precision_type(type
);
5791 if (compute_type
!= NULL_TREE
)
5793 left
= ::convert(compute_type
, left
);
5794 right
= ::convert(compute_type
, right
);
5797 tree eval_saved
= NULL_TREE
;
5801 left
= save_expr(left
);
5803 right
= save_expr(right
);
5804 // Make sure the values are evaluated.
5805 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5806 void_type_node
, left
, right
);
5809 tree ret
= fold_build2_loc(this->location(),
5811 compute_type
!= NULL_TREE
? compute_type
: type
,
5814 if (compute_type
!= NULL_TREE
)
5815 ret
= ::convert(type
, ret
);
5817 // In Go, a shift larger than the size of the type is well-defined.
5818 // This is not true in GENERIC, so we need to insert a conditional.
5821 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5822 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5823 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5825 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5826 build_int_cst_type(TREE_TYPE(right
), bits
));
5828 tree overflow_result
= fold_convert_loc(this->location(),
5831 if (this->op_
== OPERATOR_RSHIFT
5832 && !this->left_
->type()->integer_type()->is_unsigned())
5834 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5835 boolean_type_node
, left
,
5836 fold_convert_loc(this->location(),
5838 integer_zero_node
));
5839 tree neg_one
= fold_build2_loc(this->location(),
5840 MINUS_EXPR
, TREE_TYPE(left
),
5841 fold_convert_loc(this->location(),
5844 fold_convert_loc(this->location(),
5847 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5848 TREE_TYPE(left
), neg
, neg_one
,
5852 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5853 compare
, ret
, overflow_result
);
5855 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5856 TREE_TYPE(ret
), eval_saved
, ret
);
5862 // Export a binary expression.
5865 Binary_expression::do_export(Export
* exp
) const
5867 exp
->write_c_string("(");
5868 this->left_
->export_expression(exp
);
5872 exp
->write_c_string(" || ");
5874 case OPERATOR_ANDAND
:
5875 exp
->write_c_string(" && ");
5878 exp
->write_c_string(" == ");
5880 case OPERATOR_NOTEQ
:
5881 exp
->write_c_string(" != ");
5884 exp
->write_c_string(" < ");
5887 exp
->write_c_string(" <= ");
5890 exp
->write_c_string(" > ");
5893 exp
->write_c_string(" >= ");
5896 exp
->write_c_string(" + ");
5898 case OPERATOR_MINUS
:
5899 exp
->write_c_string(" - ");
5902 exp
->write_c_string(" | ");
5905 exp
->write_c_string(" ^ ");
5908 exp
->write_c_string(" * ");
5911 exp
->write_c_string(" / ");
5914 exp
->write_c_string(" % ");
5916 case OPERATOR_LSHIFT
:
5917 exp
->write_c_string(" << ");
5919 case OPERATOR_RSHIFT
:
5920 exp
->write_c_string(" >> ");
5923 exp
->write_c_string(" & ");
5925 case OPERATOR_BITCLEAR
:
5926 exp
->write_c_string(" &^ ");
5931 this->right_
->export_expression(exp
);
5932 exp
->write_c_string(")");
5935 // Import a binary expression.
5938 Binary_expression::do_import(Import
* imp
)
5940 imp
->require_c_string("(");
5942 Expression
* left
= Expression::import_expression(imp
);
5945 if (imp
->match_c_string(" || "))
5950 else if (imp
->match_c_string(" && "))
5952 op
= OPERATOR_ANDAND
;
5955 else if (imp
->match_c_string(" == "))
5960 else if (imp
->match_c_string(" != "))
5962 op
= OPERATOR_NOTEQ
;
5965 else if (imp
->match_c_string(" < "))
5970 else if (imp
->match_c_string(" <= "))
5975 else if (imp
->match_c_string(" > "))
5980 else if (imp
->match_c_string(" >= "))
5985 else if (imp
->match_c_string(" + "))
5990 else if (imp
->match_c_string(" - "))
5992 op
= OPERATOR_MINUS
;
5995 else if (imp
->match_c_string(" | "))
6000 else if (imp
->match_c_string(" ^ "))
6005 else if (imp
->match_c_string(" * "))
6010 else if (imp
->match_c_string(" / "))
6015 else if (imp
->match_c_string(" % "))
6020 else if (imp
->match_c_string(" << "))
6022 op
= OPERATOR_LSHIFT
;
6025 else if (imp
->match_c_string(" >> "))
6027 op
= OPERATOR_RSHIFT
;
6030 else if (imp
->match_c_string(" & "))
6035 else if (imp
->match_c_string(" &^ "))
6037 op
= OPERATOR_BITCLEAR
;
6042 error_at(imp
->location(), "unrecognized binary operator");
6043 return Expression::make_error(imp
->location());
6046 Expression
* right
= Expression::import_expression(imp
);
6048 imp
->require_c_string(")");
6050 return Expression::make_binary(op
, left
, right
, imp
->location());
6053 // Make a binary expression.
6056 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6057 source_location location
)
6059 return new Binary_expression(op
, left
, right
, location
);
6062 // Implement a comparison.
6065 Expression::comparison_tree(Translate_context
* context
, Operator op
,
6066 Type
* left_type
, tree left_tree
,
6067 Type
* right_type
, tree right_tree
,
6068 source_location location
)
6070 enum tree_code code
;
6076 case OPERATOR_NOTEQ
:
6095 if (left_type
->is_string_type() && right_type
->is_string_type())
6097 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
6098 static tree string_compare_decl
;
6099 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6108 right_tree
= build_int_cst_type(integer_type_node
, 0);
6110 else if ((left_type
->interface_type() != NULL
6111 && right_type
->interface_type() == NULL
6112 && !right_type
->is_nil_type())
6113 || (left_type
->interface_type() == NULL
6114 && !left_type
->is_nil_type()
6115 && right_type
->interface_type() != NULL
))
6117 // Comparing an interface value to a non-interface value.
6118 if (left_type
->interface_type() == NULL
)
6120 std::swap(left_type
, right_type
);
6121 std::swap(left_tree
, right_tree
);
6124 // The right operand is not an interface. We need to take its
6125 // address if it is not a pointer.
6128 if (right_type
->points_to() != NULL
)
6130 make_tmp
= NULL_TREE
;
6133 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
6135 make_tmp
= NULL_TREE
;
6136 arg
= build_fold_addr_expr_loc(location
, right_tree
);
6137 if (DECL_P(right_tree
))
6138 TREE_ADDRESSABLE(right_tree
) = 1;
6142 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6143 get_name(right_tree
));
6144 DECL_IGNORED_P(tmp
) = 0;
6145 DECL_INITIAL(tmp
) = right_tree
;
6146 TREE_ADDRESSABLE(tmp
) = 1;
6147 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6148 SET_EXPR_LOCATION(make_tmp
, location
);
6149 arg
= build_fold_addr_expr_loc(location
, tmp
);
6151 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6153 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6155 if (left_type
->interface_type()->is_empty())
6157 static tree empty_interface_value_compare_decl
;
6158 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6160 "__go_empty_interface_value_compare",
6163 TREE_TYPE(left_tree
),
6165 TREE_TYPE(descriptor
),
6169 if (left_tree
== error_mark_node
)
6170 return error_mark_node
;
6171 // This can panic if the type is not comparable.
6172 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6176 static tree interface_value_compare_decl
;
6177 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6179 "__go_interface_value_compare",
6182 TREE_TYPE(left_tree
),
6184 TREE_TYPE(descriptor
),
6188 if (left_tree
== error_mark_node
)
6189 return error_mark_node
;
6190 // This can panic if the type is not comparable.
6191 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6193 right_tree
= build_int_cst_type(integer_type_node
, 0);
6195 if (make_tmp
!= NULL_TREE
)
6196 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6199 else if (left_type
->interface_type() != NULL
6200 && right_type
->interface_type() != NULL
)
6202 if (left_type
->interface_type()->is_empty())
6204 gcc_assert(right_type
->interface_type()->is_empty());
6205 static tree empty_interface_compare_decl
;
6206 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6208 "__go_empty_interface_compare",
6211 TREE_TYPE(left_tree
),
6213 TREE_TYPE(right_tree
),
6215 if (left_tree
== error_mark_node
)
6216 return error_mark_node
;
6217 // This can panic if the type is uncomparable.
6218 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6222 gcc_assert(!right_type
->interface_type()->is_empty());
6223 static tree interface_compare_decl
;
6224 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6226 "__go_interface_compare",
6229 TREE_TYPE(left_tree
),
6231 TREE_TYPE(right_tree
),
6233 if (left_tree
== error_mark_node
)
6234 return error_mark_node
;
6235 // This can panic if the type is uncomparable.
6236 TREE_NOTHROW(interface_compare_decl
) = 0;
6238 right_tree
= build_int_cst_type(integer_type_node
, 0);
6241 if (left_type
->is_nil_type()
6242 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6244 std::swap(left_type
, right_type
);
6245 std::swap(left_tree
, right_tree
);
6248 if (right_type
->is_nil_type())
6250 if (left_type
->array_type() != NULL
6251 && left_type
->array_type()->length() == NULL
)
6253 Array_type
* at
= left_type
->array_type();
6254 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6255 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6257 else if (left_type
->interface_type() != NULL
)
6259 // An interface is nil if the first field is nil.
6260 tree left_type_tree
= TREE_TYPE(left_tree
);
6261 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6262 tree field
= TYPE_FIELDS(left_type_tree
);
6263 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6265 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6269 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6270 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6274 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6275 return error_mark_node
;
6277 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6278 if (CAN_HAVE_LOCATION_P(ret
))
6279 SET_EXPR_LOCATION(ret
, location
);
6283 // Class Bound_method_expression.
6288 Bound_method_expression::do_traverse(Traverse
* traverse
)
6290 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6291 return TRAVERSE_EXIT
;
6292 return Expression::traverse(&this->method_
, traverse
);
6295 // Return the type of a bound method expression. The type of this
6296 // object is really the type of the method with no receiver. We
6297 // should be able to get away with just returning the type of the
6301 Bound_method_expression::do_type()
6303 return this->method_
->type();
6306 // Determine the types of a method expression.
6309 Bound_method_expression::do_determine_type(const Type_context
*)
6311 this->method_
->determine_type_no_context();
6312 Type
* mtype
= this->method_
->type();
6313 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6314 if (fntype
== NULL
|| !fntype
->is_method())
6315 this->expr_
->determine_type_no_context();
6318 Type_context
subcontext(fntype
->receiver()->type(), false);
6319 this->expr_
->determine_type(&subcontext
);
6323 // Check the types of a method expression.
6326 Bound_method_expression::do_check_types(Gogo
*)
6328 Type
* type
= this->method_
->type()->deref();
6330 || type
->function_type() == NULL
6331 || !type
->function_type()->is_method())
6332 this->report_error(_("object is not a method"));
6335 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6336 Type
* etype
= (this->expr_type_
!= NULL
6338 : this->expr_
->type());
6339 etype
= etype
->deref();
6340 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6341 this->report_error(_("method type does not match object type"));
6345 // Get the tree for a method expression. There is no standard tree
6346 // representation for this. The only places it may currently be used
6347 // are in a Call_expression or a Go_statement, which will take it
6348 // apart directly. So this has nothing to do at present.
6351 Bound_method_expression::do_get_tree(Translate_context
*)
6356 // Make a method expression.
6358 Bound_method_expression
*
6359 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6360 source_location location
)
6362 return new Bound_method_expression(expr
, method
, location
);
6365 // Class Builtin_call_expression. This is used for a call to a
6366 // builtin function.
6368 class Builtin_call_expression
: public Call_expression
6371 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6372 bool is_varargs
, source_location location
);
6375 // This overrides Call_expression::do_lower.
6377 do_lower(Gogo
*, Named_object
*, int);
6380 do_is_constant() const;
6383 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6386 do_float_constant_value(mpfr_t
, Type
**) const;
6389 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6395 do_determine_type(const Type_context
*);
6398 do_check_types(Gogo
*);
6403 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6404 this->args()->copy(),
6410 do_get_tree(Translate_context
*);
6413 do_export(Export
*) const;
6416 do_is_recover_call() const;
6419 do_set_recover_arg(Expression
*);
6422 // The builtin functions.
6423 enum Builtin_function_code
6427 // Predeclared builtin functions.
6444 // Builtin functions from the unsafe package.
6457 real_imag_type(Type
*);
6462 // A pointer back to the general IR structure. This avoids a global
6463 // variable, or passing it around everywhere.
6465 // The builtin function being called.
6466 Builtin_function_code code_
;
6467 // Used to stop endless loops when the length of an array uses len
6468 // or cap of the array itself.
6472 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6474 Expression_list
* args
,
6476 source_location location
)
6477 : Call_expression(fn
, args
, is_varargs
, location
),
6478 gogo_(gogo
), code_(BUILTIN_INVALID
), seen_(false)
6480 Func_expression
* fnexp
= this->fn()->func_expression();
6481 gcc_assert(fnexp
!= NULL
);
6482 const std::string
& name(fnexp
->named_object()->name());
6483 if (name
== "append")
6484 this->code_
= BUILTIN_APPEND
;
6485 else if (name
== "cap")
6486 this->code_
= BUILTIN_CAP
;
6487 else if (name
== "close")
6488 this->code_
= BUILTIN_CLOSE
;
6489 else if (name
== "closed")
6490 this->code_
= BUILTIN_CLOSED
;
6491 else if (name
== "cmplx")
6492 this->code_
= BUILTIN_CMPLX
;
6493 else if (name
== "copy")
6494 this->code_
= BUILTIN_COPY
;
6495 else if (name
== "imag")
6496 this->code_
= BUILTIN_IMAG
;
6497 else if (name
== "len")
6498 this->code_
= BUILTIN_LEN
;
6499 else if (name
== "make")
6500 this->code_
= BUILTIN_MAKE
;
6501 else if (name
== "new")
6502 this->code_
= BUILTIN_NEW
;
6503 else if (name
== "panic")
6504 this->code_
= BUILTIN_PANIC
;
6505 else if (name
== "print")
6506 this->code_
= BUILTIN_PRINT
;
6507 else if (name
== "println")
6508 this->code_
= BUILTIN_PRINTLN
;
6509 else if (name
== "real")
6510 this->code_
= BUILTIN_REAL
;
6511 else if (name
== "recover")
6512 this->code_
= BUILTIN_RECOVER
;
6513 else if (name
== "Alignof")
6514 this->code_
= BUILTIN_ALIGNOF
;
6515 else if (name
== "Offsetof")
6516 this->code_
= BUILTIN_OFFSETOF
;
6517 else if (name
== "Sizeof")
6518 this->code_
= BUILTIN_SIZEOF
;
6523 // Return whether this is a call to recover. This is a virtual
6524 // function called from the parent class.
6527 Builtin_call_expression::do_is_recover_call() const
6529 if (this->classification() == EXPRESSION_ERROR
)
6531 return this->code_
== BUILTIN_RECOVER
;
6534 // Set the argument for a call to recover.
6537 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6539 const Expression_list
* args
= this->args();
6540 gcc_assert(args
== NULL
|| args
->empty());
6541 Expression_list
* new_args
= new Expression_list();
6542 new_args
->push_back(arg
);
6543 this->set_args(new_args
);
6546 // A traversal class which looks for a call expression.
6548 class Find_call_expression
: public Traverse
6551 Find_call_expression()
6552 : Traverse(traverse_expressions
),
6557 expression(Expression
**);
6561 { return this->found_
; }
6568 Find_call_expression::expression(Expression
** pexpr
)
6570 if ((*pexpr
)->call_expression() != NULL
)
6572 this->found_
= true;
6573 return TRAVERSE_EXIT
;
6575 return TRAVERSE_CONTINUE
;
6578 // Lower a builtin call expression. This turns new and make into
6579 // specific expressions. We also convert to a constant if we can.
6582 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6584 if (this->code_
== BUILTIN_NEW
)
6586 const Expression_list
* args
= this->args();
6587 if (args
== NULL
|| args
->size() < 1)
6588 this->report_error(_("not enough arguments"));
6589 else if (args
->size() > 1)
6590 this->report_error(_("too many arguments"));
6593 Expression
* arg
= args
->front();
6594 if (!arg
->is_type_expression())
6596 error_at(arg
->location(), "expected type");
6597 this->set_is_error();
6600 return Expression::make_allocation(arg
->type(), this->location());
6603 else if (this->code_
== BUILTIN_MAKE
)
6605 const Expression_list
* args
= this->args();
6606 if (args
== NULL
|| args
->size() < 1)
6607 this->report_error(_("not enough arguments"));
6610 Expression
* arg
= args
->front();
6611 if (!arg
->is_type_expression())
6613 error_at(arg
->location(), "expected type");
6614 this->set_is_error();
6618 Expression_list
* newargs
;
6619 if (args
->size() == 1)
6623 newargs
= new Expression_list();
6624 Expression_list::const_iterator p
= args
->begin();
6626 for (; p
!= args
->end(); ++p
)
6627 newargs
->push_back(*p
);
6629 return Expression::make_make(arg
->type(), newargs
,
6634 else if (this->is_constant())
6636 // We can only lower len and cap if there are no function calls
6637 // in the arguments. Otherwise we have to make the call.
6638 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6640 Expression
* arg
= this->one_arg();
6641 if (!arg
->is_constant())
6643 Find_call_expression find_call
;
6644 Expression::traverse(&arg
, &find_call
);
6645 if (find_call
.found())
6653 if (this->integer_constant_value(true, ival
, &type
))
6655 Expression
* ret
= Expression::make_integer(&ival
, type
,
6664 if (this->float_constant_value(rval
, &type
))
6666 Expression
* ret
= Expression::make_float(&rval
, type
,
6674 if (this->complex_constant_value(rval
, imag
, &type
))
6676 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6685 else if (this->code_
== BUILTIN_RECOVER
)
6687 if (function
!= NULL
)
6688 function
->func_value()->set_calls_recover();
6691 // Calling recover outside of a function always returns the
6692 // nil empty interface.
6693 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6694 return Expression::make_cast(eface
,
6695 Expression::make_nil(this->location()),
6699 else if (this->code_
== BUILTIN_APPEND
)
6701 // Lower the varargs.
6702 const Expression_list
* args
= this->args();
6703 if (args
== NULL
|| args
->empty())
6705 Type
* slice_type
= args
->front()->type();
6706 if (!slice_type
->is_open_array_type())
6708 error_at(args
->front()->location(), "argument 1 must be a slice");
6709 this->set_is_error();
6712 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6718 // Return the type of the real or imag functions, given the type of
6719 // the argument. We need to map complex to float, complex64 to
6720 // float32, and complex128 to float64, so it has to be done by name.
6721 // This returns NULL if it can't figure out the type.
6724 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6726 if (arg_type
== NULL
|| arg_type
->is_abstract())
6728 Named_type
* nt
= arg_type
->named_type();
6731 while (nt
->real_type()->named_type() != NULL
)
6732 nt
= nt
->real_type()->named_type();
6733 if (nt
->name() == "complex")
6734 return Type::lookup_float_type("float");
6735 else if (nt
->name() == "complex64")
6736 return Type::lookup_float_type("float32");
6737 else if (nt
->name() == "complex128")
6738 return Type::lookup_float_type("float64");
6743 // Return the type of the cmplx function, given the type of one of the
6744 // argments. Like real_imag_type, we have to map by name.
6747 Builtin_call_expression::cmplx_type(Type
* arg_type
)
6749 if (arg_type
== NULL
|| arg_type
->is_abstract())
6751 Named_type
* nt
= arg_type
->named_type();
6754 while (nt
->real_type()->named_type() != NULL
)
6755 nt
= nt
->real_type()->named_type();
6756 if (nt
->name() == "float")
6757 return Type::lookup_complex_type("complex");
6758 else if (nt
->name() == "float32")
6759 return Type::lookup_complex_type("complex64");
6760 else if (nt
->name() == "float64")
6761 return Type::lookup_complex_type("complex128");
6766 // Return a single argument, or NULL if there isn't one.
6769 Builtin_call_expression::one_arg() const
6771 const Expression_list
* args
= this->args();
6772 if (args
->size() != 1)
6774 return args
->front();
6777 // Return whether this is constant: len of a string, or len or cap of
6778 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6781 Builtin_call_expression::do_is_constant() const
6783 switch (this->code_
)
6791 Expression
* arg
= this->one_arg();
6794 Type
* arg_type
= arg
->type();
6796 if (arg_type
->points_to() != NULL
6797 && arg_type
->points_to()->array_type() != NULL
6798 && !arg_type
->points_to()->is_open_array_type())
6799 arg_type
= arg_type
->points_to();
6801 if (arg_type
->array_type() != NULL
6802 && arg_type
->array_type()->length() != NULL
)
6805 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6808 bool ret
= arg
->is_constant();
6809 this->seen_
= false;
6815 case BUILTIN_SIZEOF
:
6816 case BUILTIN_ALIGNOF
:
6817 return this->one_arg() != NULL
;
6819 case BUILTIN_OFFSETOF
:
6821 Expression
* arg
= this->one_arg();
6824 return arg
->field_reference_expression() != NULL
;
6829 const Expression_list
* args
= this->args();
6830 if (args
!= NULL
&& args
->size() == 2)
6831 return args
->front()->is_constant() && args
->back()->is_constant();
6838 Expression
* arg
= this->one_arg();
6839 return arg
!= NULL
&& arg
->is_constant();
6849 // Return an integer constant value if possible.
6852 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6856 if (this->code_
== BUILTIN_LEN
6857 || this->code_
== BUILTIN_CAP
)
6859 Expression
* arg
= this->one_arg();
6862 Type
* arg_type
= arg
->type();
6864 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6867 if (arg
->string_constant_value(&sval
))
6869 mpz_set_ui(val
, sval
.length());
6870 *ptype
= Type::lookup_integer_type("int");
6875 if (arg_type
->points_to() != NULL
6876 && arg_type
->points_to()->array_type() != NULL
6877 && !arg_type
->points_to()->is_open_array_type())
6878 arg_type
= arg_type
->points_to();
6880 if (arg_type
->array_type() != NULL
6881 && arg_type
->array_type()->length() != NULL
)
6885 Expression
* e
= arg_type
->array_type()->length();
6887 bool r
= e
->integer_constant_value(iota_is_constant
, val
, ptype
);
6888 this->seen_
= false;
6891 *ptype
= Type::lookup_integer_type("int");
6896 else if (this->code_
== BUILTIN_SIZEOF
6897 || this->code_
== BUILTIN_ALIGNOF
)
6899 Expression
* arg
= this->one_arg();
6902 Type
* arg_type
= arg
->type();
6903 if (arg_type
->is_error_type() || arg_type
->is_undefined())
6905 if (arg_type
->is_abstract())
6907 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
6908 unsigned long val_long
;
6909 if (this->code_
== BUILTIN_SIZEOF
)
6911 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
6912 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
6913 if (TREE_INT_CST_HIGH(type_size
) != 0)
6915 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
6916 val_long
= static_cast<unsigned long>(val_wide
);
6917 if (val_long
!= val_wide
)
6920 else if (this->code_
== BUILTIN_ALIGNOF
)
6922 if (arg
->field_reference_expression() == NULL
)
6923 val_long
= go_type_alignment(arg_type_tree
);
6926 // Calling unsafe.Alignof(s.f) returns the alignment of
6927 // the type of f when it is used as a field in a struct.
6928 val_long
= go_field_alignment(arg_type_tree
);
6933 mpz_set_ui(val
, val_long
);
6937 else if (this->code_
== BUILTIN_OFFSETOF
)
6939 Expression
* arg
= this->one_arg();
6942 Field_reference_expression
* farg
= arg
->field_reference_expression();
6945 Expression
* struct_expr
= farg
->expr();
6946 Type
* st
= struct_expr
->type();
6947 if (st
->struct_type() == NULL
)
6949 tree struct_tree
= st
->get_tree(this->gogo_
);
6950 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
6951 tree field
= TYPE_FIELDS(struct_tree
);
6952 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
6954 field
= DECL_CHAIN(field
);
6955 gcc_assert(field
!= NULL_TREE
);
6957 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
6958 if (offset_wide
< 0)
6960 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
6961 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
6963 mpz_set_ui(val
, offset_long
);
6969 // Return a floating point constant value if possible.
6972 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
6975 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
6977 Expression
* arg
= this->one_arg();
6988 if (arg
->complex_constant_value(real
, imag
, &type
))
6990 if (this->code_
== BUILTIN_REAL
)
6991 mpfr_set(val
, real
, GMP_RNDN
);
6993 mpfr_set(val
, imag
, GMP_RNDN
);
6994 *ptype
= Builtin_call_expression::real_imag_type(type
);
7006 // Return a complex constant value if possible.
7009 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
7012 if (this->code_
== BUILTIN_CMPLX
)
7014 const Expression_list
* args
= this->args();
7015 if (args
== NULL
|| args
->size() != 2)
7021 if (!args
->front()->float_constant_value(r
, &rtype
))
7032 if (args
->back()->float_constant_value(i
, &itype
)
7033 && Type::are_identical(rtype
, itype
, false, NULL
))
7035 mpfr_set(real
, r
, GMP_RNDN
);
7036 mpfr_set(imag
, i
, GMP_RNDN
);
7037 *ptype
= Builtin_call_expression::cmplx_type(rtype
);
7053 Builtin_call_expression::do_type()
7055 switch (this->code_
)
7057 case BUILTIN_INVALID
:
7064 const Expression_list
* args
= this->args();
7065 if (args
== NULL
|| args
->empty())
7066 return Type::make_error_type();
7067 return Type::make_pointer_type(args
->front()->type());
7073 case BUILTIN_ALIGNOF
:
7074 case BUILTIN_OFFSETOF
:
7075 case BUILTIN_SIZEOF
:
7076 return Type::lookup_integer_type("int");
7081 case BUILTIN_PRINTLN
:
7082 return Type::make_void_type();
7084 case BUILTIN_CLOSED
:
7085 return Type::lookup_bool_type();
7087 case BUILTIN_RECOVER
:
7088 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7090 case BUILTIN_APPEND
:
7092 const Expression_list
* args
= this->args();
7093 if (args
== NULL
|| args
->empty())
7094 return Type::make_error_type();
7095 return args
->front()->type();
7101 Expression
* arg
= this->one_arg();
7103 return Type::make_error_type();
7104 Type
* t
= arg
->type();
7105 if (t
->is_abstract())
7106 t
= t
->make_non_abstract_type();
7107 t
= Builtin_call_expression::real_imag_type(t
);
7109 t
= Type::make_error_type();
7115 const Expression_list
* args
= this->args();
7116 if (args
== NULL
|| args
->size() != 2)
7117 return Type::make_error_type();
7118 Type
* t
= args
->front()->type();
7119 if (t
->is_abstract())
7121 t
= args
->back()->type();
7122 if (t
->is_abstract())
7123 t
= t
->make_non_abstract_type();
7125 t
= Builtin_call_expression::cmplx_type(t
);
7127 t
= Type::make_error_type();
7133 // Determine the type.
7136 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7138 this->fn()->determine_type_no_context();
7140 const Expression_list
* args
= this->args();
7143 Type
* arg_type
= NULL
;
7144 switch (this->code_
)
7147 case BUILTIN_PRINTLN
:
7148 // Do not force a large integer constant to "int".
7154 arg_type
= Builtin_call_expression::cmplx_type(context
->type
);
7160 // For the cmplx function the type of one operand can
7161 // determine the type of the other, as in a binary expression.
7162 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7163 if (args
!= NULL
&& args
->size() == 2)
7165 Type
* t1
= args
->front()->type();
7166 Type
* t2
= args
->front()->type();
7167 if (!t1
->is_abstract())
7169 else if (!t2
->is_abstract())
7183 for (Expression_list::const_iterator pa
= args
->begin();
7187 Type_context subcontext
;
7188 subcontext
.type
= arg_type
;
7192 // We want to print large constants, we so can't just
7193 // use the appropriate nonabstract type. Use uint64 for
7194 // an integer if we know it is nonnegative, otherwise
7195 // use int64 for a integer, otherwise use float64 for a
7196 // float or complex128 for a complex.
7197 Type
* want_type
= NULL
;
7198 Type
* atype
= (*pa
)->type();
7199 if (atype
->is_abstract())
7201 if (atype
->integer_type() != NULL
)
7206 if (this->integer_constant_value(true, val
, &dummy
)
7207 && mpz_sgn(val
) >= 0)
7208 want_type
= Type::lookup_integer_type("uint64");
7210 want_type
= Type::lookup_integer_type("int64");
7213 else if (atype
->float_type() != NULL
)
7214 want_type
= Type::lookup_float_type("float64");
7215 else if (atype
->complex_type() != NULL
)
7216 want_type
= Type::lookup_complex_type("complex128");
7217 else if (atype
->is_abstract_string_type())
7218 want_type
= Type::lookup_string_type();
7219 else if (atype
->is_abstract_boolean_type())
7220 want_type
= Type::lookup_bool_type();
7223 subcontext
.type
= want_type
;
7227 (*pa
)->determine_type(&subcontext
);
7232 // If there is exactly one argument, return true. Otherwise give an
7233 // error message and return false.
7236 Builtin_call_expression::check_one_arg()
7238 const Expression_list
* args
= this->args();
7239 if (args
== NULL
|| args
->size() < 1)
7241 this->report_error(_("not enough arguments"));
7244 else if (args
->size() > 1)
7246 this->report_error(_("too many arguments"));
7249 if (args
->front()->is_error_expression()
7250 || args
->front()->type()->is_error_type()
7251 || args
->front()->type()->is_undefined())
7253 this->set_is_error();
7259 // Check argument types for a builtin function.
7262 Builtin_call_expression::do_check_types(Gogo
*)
7264 switch (this->code_
)
7266 case BUILTIN_INVALID
:
7274 // The single argument may be either a string or an array or a
7275 // map or a channel, or a pointer to a closed array.
7276 if (this->check_one_arg())
7278 Type
* arg_type
= this->one_arg()->type();
7279 if (arg_type
->points_to() != NULL
7280 && arg_type
->points_to()->array_type() != NULL
7281 && !arg_type
->points_to()->is_open_array_type())
7282 arg_type
= arg_type
->points_to();
7283 if (this->code_
== BUILTIN_CAP
)
7285 if (!arg_type
->is_error_type()
7286 && arg_type
->array_type() == NULL
7287 && arg_type
->channel_type() == NULL
)
7288 this->report_error(_("argument must be array or slice "
7293 if (!arg_type
->is_error_type()
7294 && !arg_type
->is_string_type()
7295 && arg_type
->array_type() == NULL
7296 && arg_type
->map_type() == NULL
7297 && arg_type
->channel_type() == NULL
)
7298 this->report_error(_("argument must be string or "
7299 "array or slice or map or channel"));
7306 case BUILTIN_PRINTLN
:
7308 const Expression_list
* args
= this->args();
7311 if (this->code_
== BUILTIN_PRINT
)
7312 warning_at(this->location(), 0,
7313 "no arguments for builtin function %<%s%>",
7314 (this->code_
== BUILTIN_PRINT
7320 for (Expression_list::const_iterator p
= args
->begin();
7324 Type
* type
= (*p
)->type();
7325 if (type
->is_error_type()
7326 || type
->is_string_type()
7327 || type
->integer_type() != NULL
7328 || type
->float_type() != NULL
7329 || type
->complex_type() != NULL
7330 || type
->is_boolean_type()
7331 || type
->points_to() != NULL
7332 || type
->interface_type() != NULL
7333 || type
->channel_type() != NULL
7334 || type
->map_type() != NULL
7335 || type
->function_type() != NULL
7336 || type
->is_open_array_type())
7339 this->report_error(_("unsupported argument type to "
7340 "builtin function"));
7347 case BUILTIN_CLOSED
:
7348 if (this->check_one_arg())
7350 if (this->one_arg()->type()->channel_type() == NULL
)
7351 this->report_error(_("argument must be channel"));
7356 case BUILTIN_SIZEOF
:
7357 case BUILTIN_ALIGNOF
:
7358 this->check_one_arg();
7361 case BUILTIN_RECOVER
:
7362 if (this->args() != NULL
&& !this->args()->empty())
7363 this->report_error(_("too many arguments"));
7366 case BUILTIN_OFFSETOF
:
7367 if (this->check_one_arg())
7369 Expression
* arg
= this->one_arg();
7370 if (arg
->field_reference_expression() == NULL
)
7371 this->report_error(_("argument must be a field reference"));
7377 const Expression_list
* args
= this->args();
7378 if (args
== NULL
|| args
->size() < 2)
7380 this->report_error(_("not enough arguments"));
7383 else if (args
->size() > 2)
7385 this->report_error(_("too many arguments"));
7388 Type
* arg1_type
= args
->front()->type();
7389 Type
* arg2_type
= args
->back()->type();
7390 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7394 if (arg1_type
->is_open_array_type())
7395 e1
= arg1_type
->array_type()->element_type();
7398 this->report_error(_("left argument must be a slice"));
7403 if (arg2_type
->is_open_array_type())
7404 e2
= arg2_type
->array_type()->element_type();
7405 else if (arg2_type
->is_string_type())
7406 e2
= Type::lookup_integer_type("uint8");
7409 this->report_error(_("right argument must be a slice or a string"));
7413 if (!Type::are_identical(e1
, e2
, true, NULL
))
7414 this->report_error(_("element types must be the same"));
7418 case BUILTIN_APPEND
:
7420 const Expression_list
* args
= this->args();
7421 if (args
== NULL
|| args
->size() < 2)
7423 this->report_error(_("not enough arguments"));
7426 if (args
->size() > 2)
7428 this->report_error(_("too many arguments"));
7432 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7436 this->report_error(_("arguments 1 and 2 have different types"));
7439 error_at(this->location(),
7440 "arguments 1 and 2 have different types (%s)",
7442 this->set_is_error();
7450 if (this->check_one_arg())
7452 if (this->one_arg()->type()->complex_type() == NULL
)
7453 this->report_error(_("argument must have complex type"));
7459 const Expression_list
* args
= this->args();
7460 if (args
== NULL
|| args
->size() < 2)
7461 this->report_error(_("not enough arguments"));
7462 else if (args
->size() > 2)
7463 this->report_error(_("too many arguments"));
7464 else if (args
->front()->is_error_expression()
7465 || args
->front()->type()->is_error_type()
7466 || args
->back()->is_error_expression()
7467 || args
->back()->type()->is_error_type())
7468 this->set_is_error();
7469 else if (!Type::are_identical(args
->front()->type(),
7470 args
->back()->type(), true, NULL
))
7471 this->report_error(_("cmplx arguments must have identical types"));
7472 else if (args
->front()->type()->float_type() == NULL
)
7473 this->report_error(_("cmplx arguments must have "
7474 "floating-point type"));
7483 // Return the tree for a builtin function.
7486 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7488 Gogo
* gogo
= context
->gogo();
7489 source_location location
= this->location();
7490 switch (this->code_
)
7492 case BUILTIN_INVALID
:
7500 const Expression_list
* args
= this->args();
7501 gcc_assert(args
!= NULL
&& args
->size() == 1);
7502 Expression
* arg
= *args
->begin();
7503 Type
* arg_type
= arg
->type();
7507 gcc_assert(saw_errors());
7508 return error_mark_node
;
7512 tree arg_tree
= arg
->get_tree(context
);
7514 this->seen_
= false;
7516 if (arg_tree
== error_mark_node
)
7517 return error_mark_node
;
7519 if (arg_type
->points_to() != NULL
)
7521 arg_type
= arg_type
->points_to();
7522 gcc_assert(arg_type
->array_type() != NULL
7523 && !arg_type
->is_open_array_type());
7524 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7525 arg_tree
= build_fold_indirect_ref(arg_tree
);
7529 if (this->code_
== BUILTIN_LEN
)
7531 if (arg_type
->is_string_type())
7532 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7533 else if (arg_type
->array_type() != NULL
)
7537 gcc_assert(saw_errors());
7538 return error_mark_node
;
7541 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7542 this->seen_
= false;
7544 else if (arg_type
->map_type() != NULL
)
7546 static tree map_len_fndecl
;
7547 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7552 arg_type
->get_tree(gogo
),
7555 else if (arg_type
->channel_type() != NULL
)
7557 static tree chan_len_fndecl
;
7558 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7563 arg_type
->get_tree(gogo
),
7571 if (arg_type
->array_type() != NULL
)
7575 gcc_assert(saw_errors());
7576 return error_mark_node
;
7579 val_tree
= arg_type
->array_type()->capacity_tree(gogo
,
7581 this->seen_
= false;
7583 else if (arg_type
->channel_type() != NULL
)
7585 static tree chan_cap_fndecl
;
7586 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7591 arg_type
->get_tree(gogo
),
7598 if (val_tree
== error_mark_node
)
7599 return error_mark_node
;
7601 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7602 if (type_tree
== TREE_TYPE(val_tree
))
7605 return fold(convert_to_integer(type_tree
, val_tree
));
7609 case BUILTIN_PRINTLN
:
7611 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7612 tree stmt_list
= NULL_TREE
;
7614 const Expression_list
* call_args
= this->args();
7615 if (call_args
!= NULL
)
7617 for (Expression_list::const_iterator p
= call_args
->begin();
7618 p
!= call_args
->end();
7621 if (is_ln
&& p
!= call_args
->begin())
7623 static tree print_space_fndecl
;
7624 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7629 if (call
== error_mark_node
)
7630 return error_mark_node
;
7631 append_to_statement_list(call
, &stmt_list
);
7634 Type
* type
= (*p
)->type();
7636 tree arg
= (*p
)->get_tree(context
);
7637 if (arg
== error_mark_node
)
7638 return error_mark_node
;
7642 if (type
->is_string_type())
7644 static tree print_string_fndecl
;
7645 pfndecl
= &print_string_fndecl
;
7646 fnname
= "__go_print_string";
7648 else if (type
->integer_type() != NULL
7649 && type
->integer_type()->is_unsigned())
7651 static tree print_uint64_fndecl
;
7652 pfndecl
= &print_uint64_fndecl
;
7653 fnname
= "__go_print_uint64";
7654 Type
* itype
= Type::lookup_integer_type("uint64");
7655 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7658 else if (type
->integer_type() != NULL
)
7660 static tree print_int64_fndecl
;
7661 pfndecl
= &print_int64_fndecl
;
7662 fnname
= "__go_print_int64";
7663 Type
* itype
= Type::lookup_integer_type("int64");
7664 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7667 else if (type
->float_type() != NULL
)
7669 static tree print_double_fndecl
;
7670 pfndecl
= &print_double_fndecl
;
7671 fnname
= "__go_print_double";
7672 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7674 else if (type
->complex_type() != NULL
)
7676 static tree print_complex_fndecl
;
7677 pfndecl
= &print_complex_fndecl
;
7678 fnname
= "__go_print_complex";
7679 arg
= fold_convert_loc(location
, complex_double_type_node
,
7682 else if (type
->is_boolean_type())
7684 static tree print_bool_fndecl
;
7685 pfndecl
= &print_bool_fndecl
;
7686 fnname
= "__go_print_bool";
7688 else if (type
->points_to() != NULL
7689 || type
->channel_type() != NULL
7690 || type
->map_type() != NULL
7691 || type
->function_type() != NULL
)
7693 static tree print_pointer_fndecl
;
7694 pfndecl
= &print_pointer_fndecl
;
7695 fnname
= "__go_print_pointer";
7696 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7698 else if (type
->interface_type() != NULL
)
7700 if (type
->interface_type()->is_empty())
7702 static tree print_empty_interface_fndecl
;
7703 pfndecl
= &print_empty_interface_fndecl
;
7704 fnname
= "__go_print_empty_interface";
7708 static tree print_interface_fndecl
;
7709 pfndecl
= &print_interface_fndecl
;
7710 fnname
= "__go_print_interface";
7713 else if (type
->is_open_array_type())
7715 static tree print_slice_fndecl
;
7716 pfndecl
= &print_slice_fndecl
;
7717 fnname
= "__go_print_slice";
7722 tree call
= Gogo::call_builtin(pfndecl
,
7729 if (call
== error_mark_node
)
7730 return error_mark_node
;
7731 append_to_statement_list(call
, &stmt_list
);
7737 static tree print_nl_fndecl
;
7738 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7743 if (call
== error_mark_node
)
7744 return error_mark_node
;
7745 append_to_statement_list(call
, &stmt_list
);
7753 const Expression_list
* args
= this->args();
7754 gcc_assert(args
!= NULL
&& args
->size() == 1);
7755 Expression
* arg
= args
->front();
7756 tree arg_tree
= arg
->get_tree(context
);
7757 if (arg_tree
== error_mark_node
)
7758 return error_mark_node
;
7759 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7760 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7762 arg_tree
, location
);
7763 static tree panic_fndecl
;
7764 tree call
= Gogo::call_builtin(&panic_fndecl
,
7769 TREE_TYPE(arg_tree
),
7771 if (call
== error_mark_node
)
7772 return error_mark_node
;
7773 // This function will throw an exception.
7774 TREE_NOTHROW(panic_fndecl
) = 0;
7775 // This function will not return.
7776 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7780 case BUILTIN_RECOVER
:
7782 // The argument is set when building recover thunks. It's a
7783 // boolean value which is true if we can recover a value now.
7784 const Expression_list
* args
= this->args();
7785 gcc_assert(args
!= NULL
&& args
->size() == 1);
7786 Expression
* arg
= args
->front();
7787 tree arg_tree
= arg
->get_tree(context
);
7788 if (arg_tree
== error_mark_node
)
7789 return error_mark_node
;
7791 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7792 tree empty_tree
= empty
->get_tree(context
->gogo());
7794 Type
* nil_type
= Type::make_nil_type();
7795 Expression
* nil
= Expression::make_nil(location
);
7796 tree nil_tree
= nil
->get_tree(context
);
7797 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7803 // We need to handle a deferred call to recover specially,
7804 // because it changes whether it can recover a panic or not.
7805 // See test7 in test/recover1.go.
7807 if (this->is_deferred())
7809 static tree deferred_recover_fndecl
;
7810 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7812 "__go_deferred_recover",
7818 static tree recover_fndecl
;
7819 call
= Gogo::call_builtin(&recover_fndecl
,
7825 if (call
== error_mark_node
)
7826 return error_mark_node
;
7827 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7828 call
, empty_nil_tree
);
7832 case BUILTIN_CLOSED
:
7834 const Expression_list
* args
= this->args();
7835 gcc_assert(args
!= NULL
&& args
->size() == 1);
7836 Expression
* arg
= args
->front();
7837 tree arg_tree
= arg
->get_tree(context
);
7838 if (arg_tree
== error_mark_node
)
7839 return error_mark_node
;
7840 if (this->code_
== BUILTIN_CLOSE
)
7842 static tree close_fndecl
;
7843 return Gogo::call_builtin(&close_fndecl
,
7845 "__go_builtin_close",
7848 TREE_TYPE(arg_tree
),
7853 static tree closed_fndecl
;
7854 return Gogo::call_builtin(&closed_fndecl
,
7856 "__go_builtin_closed",
7859 TREE_TYPE(arg_tree
),
7864 case BUILTIN_SIZEOF
:
7865 case BUILTIN_OFFSETOF
:
7866 case BUILTIN_ALIGNOF
:
7871 bool b
= this->integer_constant_value(true, val
, &dummy
);
7873 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7874 tree ret
= Expression::integer_constant_tree(val
, type
);
7881 const Expression_list
* args
= this->args();
7882 gcc_assert(args
!= NULL
&& args
->size() == 2);
7883 Expression
* arg1
= args
->front();
7884 Expression
* arg2
= args
->back();
7886 tree arg1_tree
= arg1
->get_tree(context
);
7887 tree arg2_tree
= arg2
->get_tree(context
);
7888 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7889 return error_mark_node
;
7891 Type
* arg1_type
= arg1
->type();
7892 Array_type
* at
= arg1_type
->array_type();
7893 arg1_tree
= save_expr(arg1_tree
);
7894 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
7895 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
7896 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
7897 return error_mark_node
;
7899 Type
* arg2_type
= arg2
->type();
7902 if (arg2_type
->is_open_array_type())
7904 at
= arg2_type
->array_type();
7905 arg2_tree
= save_expr(arg2_tree
);
7906 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7907 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7911 arg2_tree
= save_expr(arg2_tree
);
7912 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
7913 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
7915 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7916 return error_mark_node
;
7918 arg1_len
= save_expr(arg1_len
);
7919 arg2_len
= save_expr(arg2_len
);
7920 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
7921 fold_build2_loc(location
, LT_EXPR
,
7923 arg1_len
, arg2_len
),
7924 arg1_len
, arg2_len
);
7925 len
= save_expr(len
);
7927 Type
* element_type
= at
->element_type();
7928 tree element_type_tree
= element_type
->get_tree(gogo
);
7929 if (element_type_tree
== error_mark_node
)
7930 return error_mark_node
;
7931 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7932 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
7934 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
7935 TREE_TYPE(element_size
),
7936 bytecount
, element_size
);
7937 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
7939 arg1_val
= fold_convert_loc(location
, ptr_type_node
, arg1_val
);
7940 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7942 static tree copy_fndecl
;
7943 tree call
= Gogo::call_builtin(©_fndecl
,
7954 if (call
== error_mark_node
)
7955 return error_mark_node
;
7957 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
7961 case BUILTIN_APPEND
:
7963 const Expression_list
* args
= this->args();
7964 gcc_assert(args
!= NULL
&& args
->size() == 2);
7965 Expression
* arg1
= args
->front();
7966 Expression
* arg2
= args
->back();
7968 tree arg1_tree
= arg1
->get_tree(context
);
7969 tree arg2_tree
= arg2
->get_tree(context
);
7970 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7971 return error_mark_node
;
7973 Array_type
* at
= arg1
->type()->array_type();
7974 Type
* element_type
= at
->element_type();
7976 arg2_tree
= Expression::convert_for_assignment(context
, at
,
7980 if (arg2_tree
== error_mark_node
)
7981 return error_mark_node
;
7983 arg2_tree
= save_expr(arg2_tree
);
7984 tree arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7985 tree arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7986 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7987 return error_mark_node
;
7988 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7989 arg2_len
= fold_convert_loc(location
, size_type_node
, arg2_len
);
7991 tree element_type_tree
= element_type
->get_tree(gogo
);
7992 if (element_type_tree
== error_mark_node
)
7993 return error_mark_node
;
7994 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7995 element_size
= fold_convert_loc(location
, size_type_node
,
7998 // We rebuild the decl each time since the slice types may
8000 tree append_fndecl
= NULL_TREE
;
8001 return Gogo::call_builtin(&append_fndecl
,
8005 TREE_TYPE(arg1_tree
),
8006 TREE_TYPE(arg1_tree
),
8019 const Expression_list
* args
= this->args();
8020 gcc_assert(args
!= NULL
&& args
->size() == 1);
8021 Expression
* arg
= args
->front();
8022 tree arg_tree
= arg
->get_tree(context
);
8023 if (arg_tree
== error_mark_node
)
8024 return error_mark_node
;
8025 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
8026 if (this->code_
== BUILTIN_REAL
)
8027 return fold_build1_loc(location
, REALPART_EXPR
,
8028 TREE_TYPE(TREE_TYPE(arg_tree
)),
8031 return fold_build1_loc(location
, IMAGPART_EXPR
,
8032 TREE_TYPE(TREE_TYPE(arg_tree
)),
8038 const Expression_list
* args
= this->args();
8039 gcc_assert(args
!= NULL
&& args
->size() == 2);
8040 tree r
= args
->front()->get_tree(context
);
8041 tree i
= args
->back()->get_tree(context
);
8042 if (r
== error_mark_node
|| i
== error_mark_node
)
8043 return error_mark_node
;
8044 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
8045 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
8046 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
8047 return fold_build2_loc(location
, COMPLEX_EXPR
,
8048 build_complex_type(TREE_TYPE(r
)),
8057 // We have to support exporting a builtin call expression, because
8058 // code can set a constant to the result of a builtin expression.
8061 Builtin_call_expression::do_export(Export
* exp
) const
8068 if (this->integer_constant_value(true, val
, &dummy
))
8070 Integer_expression::export_integer(exp
, val
);
8079 if (this->float_constant_value(fval
, &dummy
))
8081 Float_expression::export_float(exp
, fval
);
8093 if (this->complex_constant_value(real
, imag
, &dummy
))
8095 Complex_expression::export_complex(exp
, real
, imag
);
8104 error_at(this->location(), "value is not constant");
8108 // A trailing space lets us reliably identify the end of the number.
8109 exp
->write_c_string(" ");
8112 // Class Call_expression.
8117 Call_expression::do_traverse(Traverse
* traverse
)
8119 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8120 return TRAVERSE_EXIT
;
8121 if (this->args_
!= NULL
)
8123 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8124 return TRAVERSE_EXIT
;
8126 return TRAVERSE_CONTINUE
;
8129 // Lower a call statement.
8132 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
8134 // A type case can look like a function call.
8135 if (this->fn_
->is_type_expression()
8136 && this->args_
!= NULL
8137 && this->args_
->size() == 1)
8138 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8141 // Recognize a call to a builtin function.
8142 Func_expression
* fne
= this->fn_
->func_expression();
8144 && fne
->named_object()->is_function_declaration()
8145 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
8146 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8147 this->is_varargs_
, this->location());
8149 // Handle an argument which is a call to a function which returns
8150 // multiple results.
8151 if (this->args_
!= NULL
8152 && this->args_
->size() == 1
8153 && this->args_
->front()->call_expression() != NULL
8154 && this->fn_
->type()->function_type() != NULL
)
8156 Function_type
* fntype
= this->fn_
->type()->function_type();
8157 size_t rc
= this->args_
->front()->call_expression()->result_count();
8159 && fntype
->parameters() != NULL
8160 && (fntype
->parameters()->size() == rc
8161 || (fntype
->is_varargs()
8162 && fntype
->parameters()->size() - 1 <= rc
)))
8164 Call_expression
* call
= this->args_
->front()->call_expression();
8165 Expression_list
* args
= new Expression_list
;
8166 for (size_t i
= 0; i
< rc
; ++i
)
8167 args
->push_back(Expression::make_call_result(call
, i
));
8168 // We can't return a new call expression here, because this
8169 // one may be referenced by Call_result expressions. FIXME.
8175 // Handle a call to a varargs function by packaging up the extra
8177 if (this->fn_
->type()->function_type() != NULL
8178 && this->fn_
->type()->function_type()->is_varargs())
8180 Function_type
* fntype
= this->fn_
->type()->function_type();
8181 const Typed_identifier_list
* parameters
= fntype
->parameters();
8182 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
8183 Type
* varargs_type
= parameters
->back().type();
8184 return this->lower_varargs(gogo
, function
, varargs_type
,
8185 parameters
->size());
8191 // Lower a call to a varargs function. FUNCTION is the function in
8192 // which the call occurs--it's not the function we are calling.
8193 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8194 // PARAM_COUNT is the number of parameters of the function we are
8195 // calling; the last of these parameters will be the varargs
8199 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8200 Type
* varargs_type
, size_t param_count
)
8202 if (this->varargs_are_lowered_
)
8205 source_location loc
= this->location();
8207 gcc_assert(param_count
> 0);
8208 gcc_assert(varargs_type
->is_open_array_type());
8210 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8211 if (arg_count
< param_count
- 1)
8213 // Not enough arguments; will be caught in check_types.
8217 Expression_list
* old_args
= this->args_
;
8218 Expression_list
* new_args
= new Expression_list();
8219 bool push_empty_arg
= false;
8220 if (old_args
== NULL
|| old_args
->empty())
8222 gcc_assert(param_count
== 1);
8223 push_empty_arg
= true;
8227 Expression_list::const_iterator pa
;
8229 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8231 if (static_cast<size_t>(i
) == param_count
)
8233 new_args
->push_back(*pa
);
8236 // We have reached the varargs parameter.
8238 bool issued_error
= false;
8239 if (pa
== old_args
->end())
8240 push_empty_arg
= true;
8241 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8242 new_args
->push_back(*pa
);
8243 else if (this->is_varargs_
)
8245 this->report_error(_("too many arguments"));
8248 else if (pa
+ 1 == old_args
->end()
8249 && this->is_compatible_varargs_argument(function
, *pa
,
8252 new_args
->push_back(*pa
);
8255 Type
* element_type
= varargs_type
->array_type()->element_type();
8256 Expression_list
* vals
= new Expression_list
;
8257 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8259 // Check types here so that we get a better message.
8260 Type
* patype
= (*pa
)->type();
8261 source_location paloc
= (*pa
)->location();
8262 if (!this->check_argument_type(i
, element_type
, patype
,
8263 paloc
, issued_error
))
8265 vals
->push_back(*pa
);
8268 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8269 new_args
->push_back(val
);
8274 new_args
->push_back(Expression::make_nil(loc
));
8276 // We can't return a new call expression here, because this one may
8277 // be referenced by Call_result expressions. FIXME.
8278 if (old_args
!= NULL
)
8280 this->args_
= new_args
;
8281 this->varargs_are_lowered_
= true;
8283 // Lower all the new subexpressions.
8284 Expression
* ret
= this;
8285 gogo
->lower_expression(function
, &ret
);
8286 gcc_assert(ret
== this);
8290 // Return true if ARG is a varargs argment which should be passed to
8291 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8292 // will be the last argument passed in the call, and PARAM_TYPE will
8293 // be the type of the last parameter of the varargs function being
8297 Call_expression::is_compatible_varargs_argument(Named_object
* function
,
8302 *issued_error
= false;
8304 Type
* var_type
= NULL
;
8306 // The simple case is passing the varargs parameter of the caller.
8307 Var_expression
* ve
= arg
->var_expression();
8308 if (ve
!= NULL
&& ve
->named_object()->is_variable())
8310 Variable
* var
= ve
->named_object()->var_value();
8311 if (var
->is_varargs_parameter())
8312 var_type
= var
->type();
8315 // The complex case is passing the varargs parameter of some
8316 // enclosing function. This will look like passing down *c.f where
8317 // c is the closure variable and f is a field in the closure.
8318 if (function
!= NULL
8319 && function
->func_value()->needs_closure()
8320 && arg
->classification() == EXPRESSION_UNARY
)
8322 Unary_expression
* ue
= static_cast<Unary_expression
*>(arg
);
8323 if (ue
->op() == OPERATOR_MULT
)
8325 Field_reference_expression
* fre
=
8326 ue
->operand()->deref()->field_reference_expression();
8329 Var_expression
* ve
= fre
->expr()->deref()->var_expression();
8332 Named_object
* no
= ve
->named_object();
8333 Function
* f
= function
->func_value();
8334 if (no
== f
->closure_var())
8336 // At this point we know that this indeed a
8337 // reference to some enclosing variable. Now we
8338 // need to figure out whether that variable is a
8339 // varargs parameter.
8340 Named_object
* enclosing
=
8341 f
->enclosing_var(fre
->field_index());
8342 Variable
* var
= enclosing
->var_value();
8343 if (var
->is_varargs_parameter())
8344 var_type
= var
->type();
8351 if (var_type
== NULL
)
8354 // We only match if the parameter is the same, with an identical
8356 Array_type
* var_at
= var_type
->array_type();
8357 gcc_assert(var_at
!= NULL
);
8358 Array_type
* param_at
= param_type
->array_type();
8359 if (param_at
!= NULL
8360 && Type::are_identical(var_at
->element_type(),
8361 param_at
->element_type(), true, NULL
))
8363 error_at(arg
->location(), "... mismatch: passing ...T as ...");
8364 *issued_error
= true;
8368 // Get the function type. Returns NULL if we don't know the type. If
8369 // this returns NULL, and if_ERROR is true, issues an error.
8372 Call_expression::get_function_type() const
8374 return this->fn_
->type()->function_type();
8377 // Return the number of values which this call will return.
8380 Call_expression::result_count() const
8382 const Function_type
* fntype
= this->get_function_type();
8385 if (fntype
->results() == NULL
)
8387 return fntype
->results()->size();
8390 // Return whether this is a call to the predeclared function recover.
8393 Call_expression::is_recover_call() const
8395 return this->do_is_recover_call();
8398 // Set the argument to the recover function.
8401 Call_expression::set_recover_arg(Expression
* arg
)
8403 this->do_set_recover_arg(arg
);
8406 // Virtual functions also implemented by Builtin_call_expression.
8409 Call_expression::do_is_recover_call() const
8415 Call_expression::do_set_recover_arg(Expression
*)
8423 Call_expression::do_type()
8425 if (this->type_
!= NULL
)
8429 Function_type
* fntype
= this->get_function_type();
8431 return Type::make_error_type();
8433 const Typed_identifier_list
* results
= fntype
->results();
8434 if (results
== NULL
)
8435 ret
= Type::make_void_type();
8436 else if (results
->size() == 1)
8437 ret
= results
->begin()->type();
8439 ret
= Type::make_call_multiple_result_type(this);
8446 // Determine types for a call expression. We can use the function
8447 // parameter types to set the types of the arguments.
8450 Call_expression::do_determine_type(const Type_context
*)
8452 this->fn_
->determine_type_no_context();
8453 Function_type
* fntype
= this->get_function_type();
8454 const Typed_identifier_list
* parameters
= NULL
;
8456 parameters
= fntype
->parameters();
8457 if (this->args_
!= NULL
)
8459 Typed_identifier_list::const_iterator pt
;
8460 if (parameters
!= NULL
)
8461 pt
= parameters
->begin();
8462 for (Expression_list::const_iterator pa
= this->args_
->begin();
8463 pa
!= this->args_
->end();
8466 if (parameters
!= NULL
&& pt
!= parameters
->end())
8468 Type_context
subcontext(pt
->type(), false);
8469 (*pa
)->determine_type(&subcontext
);
8473 (*pa
)->determine_type_no_context();
8478 // Check types for parameter I.
8481 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8482 const Type
* argument_type
,
8483 source_location argument_location
,
8487 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8492 error_at(argument_location
, "argument %d has incompatible type", i
);
8494 error_at(argument_location
,
8495 "argument %d has incompatible type (%s)",
8498 this->set_is_error();
8507 Call_expression::do_check_types(Gogo
*)
8509 Function_type
* fntype
= this->get_function_type();
8512 if (!this->fn_
->type()->is_error_type())
8513 this->report_error(_("expected function"));
8517 if (fntype
->is_method())
8519 // We don't support pointers to methods, so the function has to
8520 // be a bound method expression.
8521 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8524 this->report_error(_("method call without object"));
8527 Type
* first_arg_type
= bme
->first_argument()->type();
8528 if (first_arg_type
->points_to() == NULL
)
8530 // When passing a value, we need to check that we are
8531 // permitted to copy it.
8533 if (!Type::are_assignable(fntype
->receiver()->type(),
8534 first_arg_type
, &reason
))
8537 this->report_error(_("incompatible type for receiver"));
8540 error_at(this->location(),
8541 "incompatible type for receiver (%s)",
8543 this->set_is_error();
8549 // Note that varargs was handled by the lower_varargs() method, so
8550 // we don't have to worry about it here.
8552 const Typed_identifier_list
* parameters
= fntype
->parameters();
8553 if (this->args_
== NULL
)
8555 if (parameters
!= NULL
&& !parameters
->empty())
8556 this->report_error(_("not enough arguments"));
8558 else if (parameters
== NULL
)
8559 this->report_error(_("too many arguments"));
8563 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8564 for (Expression_list::const_iterator pa
= this->args_
->begin();
8565 pa
!= this->args_
->end();
8568 if (pt
== parameters
->end())
8570 this->report_error(_("too many arguments"));
8573 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8574 (*pa
)->location(), false);
8576 if (pt
!= parameters
->end())
8577 this->report_error(_("not enough arguments"));
8581 // Return whether we have to use a temporary variable to ensure that
8582 // we evaluate this call expression in order. If the call returns no
8583 // results then it will inevitably be executed last. If the call
8584 // returns more than one result then it will be used with Call_result
8585 // expressions. So we only have to use a temporary variable if the
8586 // call returns exactly one result.
8589 Call_expression::do_must_eval_in_order() const
8591 return this->result_count() == 1;
8594 // Get the function and the first argument to use when calling a bound
8598 Call_expression::bound_method_function(Translate_context
* context
,
8599 Bound_method_expression
* bound_method
,
8600 tree
* first_arg_ptr
)
8602 Expression
* first_argument
= bound_method
->first_argument();
8603 tree first_arg
= first_argument
->get_tree(context
);
8604 if (first_arg
== error_mark_node
)
8605 return error_mark_node
;
8607 // We always pass a pointer to the first argument when calling a
8609 if (first_argument
->type()->points_to() == NULL
)
8611 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8612 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8613 || DECL_P(first_arg
)
8614 || TREE_CODE(first_arg
) == INDIRECT_REF
8615 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8617 first_arg
= build_fold_addr_expr(first_arg
);
8618 if (DECL_P(first_arg
))
8619 TREE_ADDRESSABLE(first_arg
) = 1;
8623 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8624 get_name(first_arg
));
8625 DECL_IGNORED_P(tmp
) = 0;
8626 DECL_INITIAL(tmp
) = first_arg
;
8627 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8628 build1(DECL_EXPR
, void_type_node
, tmp
),
8629 build_fold_addr_expr(tmp
));
8630 TREE_ADDRESSABLE(tmp
) = 1;
8632 if (first_arg
== error_mark_node
)
8633 return error_mark_node
;
8636 Type
* fatype
= bound_method
->first_argument_type();
8639 if (fatype
->points_to() == NULL
)
8640 fatype
= Type::make_pointer_type(fatype
);
8641 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8642 if (first_arg
== error_mark_node
8643 || TREE_TYPE(first_arg
) == error_mark_node
)
8644 return error_mark_node
;
8647 *first_arg_ptr
= first_arg
;
8649 return bound_method
->method()->get_tree(context
);
8652 // Get the function and the first argument to use when calling an
8653 // interface method.
8656 Call_expression::interface_method_function(
8657 Translate_context
* context
,
8658 Interface_field_reference_expression
* interface_method
,
8659 tree
* first_arg_ptr
)
8661 tree expr
= interface_method
->expr()->get_tree(context
);
8662 if (expr
== error_mark_node
)
8663 return error_mark_node
;
8664 expr
= save_expr(expr
);
8665 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8666 if (first_arg
== error_mark_node
)
8667 return error_mark_node
;
8668 *first_arg_ptr
= first_arg
;
8669 return interface_method
->get_function_tree(context
, expr
);
8672 // Build the call expression.
8675 Call_expression::do_get_tree(Translate_context
* context
)
8677 if (this->tree_
!= NULL_TREE
)
8680 Function_type
* fntype
= this->get_function_type();
8682 return error_mark_node
;
8684 if (this->fn_
->is_error_expression())
8685 return error_mark_node
;
8687 Gogo
* gogo
= context
->gogo();
8688 source_location location
= this->location();
8690 Func_expression
* func
= this->fn_
->func_expression();
8691 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8692 Interface_field_reference_expression
* interface_method
=
8693 this->fn_
->interface_field_reference_expression();
8694 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8695 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8696 gcc_assert(!fntype
->is_method() || is_method
);
8700 if (this->args_
== NULL
|| this->args_
->empty())
8702 nargs
= is_method
? 1 : 0;
8703 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8707 const Typed_identifier_list
* params
= fntype
->parameters();
8708 gcc_assert(params
!= NULL
);
8710 nargs
= this->args_
->size();
8711 int i
= is_method
? 1 : 0;
8713 args
= new tree
[nargs
];
8715 Typed_identifier_list::const_iterator pp
= params
->begin();
8716 Expression_list::const_iterator pe
;
8717 for (pe
= this->args_
->begin();
8718 pe
!= this->args_
->end();
8721 gcc_assert(pp
!= params
->end());
8722 tree arg_val
= (*pe
)->get_tree(context
);
8723 args
[i
] = Expression::convert_for_assignment(context
,
8728 if (args
[i
] == error_mark_node
)
8731 return error_mark_node
;
8734 gcc_assert(pp
== params
->end());
8735 gcc_assert(i
== nargs
);
8738 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8739 if (rettype
== error_mark_node
)
8742 return error_mark_node
;
8747 fn
= func
->get_tree_without_closure(gogo
);
8748 else if (!is_method
)
8749 fn
= this->fn_
->get_tree(context
);
8750 else if (bound_method
!= NULL
)
8751 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8752 else if (interface_method
!= NULL
)
8753 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8757 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8760 return error_mark_node
;
8763 // This is to support builtin math functions when using 80387 math.
8765 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8766 fndecl
= TREE_OPERAND(fndecl
, 0);
8767 tree excess_type
= NULL_TREE
;
8769 && DECL_IS_BUILTIN(fndecl
)
8770 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8772 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8773 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8774 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8775 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8777 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8778 if (excess_type
!= NULL_TREE
)
8780 tree excess_fndecl
= mathfn_built_in(excess_type
,
8781 DECL_FUNCTION_CODE(fndecl
));
8782 if (excess_fndecl
== NULL_TREE
)
8783 excess_type
= NULL_TREE
;
8786 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8787 for (int i
= 0; i
< nargs
; ++i
)
8788 args
[i
] = ::convert(excess_type
, args
[i
]);
8793 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8797 SET_EXPR_LOCATION(ret
, location
);
8801 tree closure_tree
= func
->closure()->get_tree(context
);
8802 if (closure_tree
!= error_mark_node
)
8803 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8806 // If this is a recursive function type which returns itself, as in
8808 // we have used ptr_type_node for the return type. Add a cast here
8809 // to the correct type.
8810 if (TREE_TYPE(ret
) == ptr_type_node
)
8812 tree t
= this->type()->get_tree(gogo
);
8813 ret
= fold_convert_loc(location
, t
, ret
);
8816 if (excess_type
!= NULL_TREE
)
8818 // Calling convert here can undo our excess precision change.
8819 // That may or may not be a bug in convert_to_real.
8820 ret
= build1(NOP_EXPR
, rettype
, ret
);
8823 // If there is more than one result, we will refer to the call
8825 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8826 ret
= save_expr(ret
);
8833 // Make a call expression.
8836 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8837 source_location location
)
8839 return new Call_expression(fn
, args
, is_varargs
, location
);
8842 // A single result from a call which returns multiple results.
8844 class Call_result_expression
: public Expression
8847 Call_result_expression(Call_expression
* call
, unsigned int index
)
8848 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8849 call_(call
), index_(index
)
8854 do_traverse(Traverse
*);
8860 do_determine_type(const Type_context
*);
8863 do_check_types(Gogo
*);
8868 return new Call_result_expression(this->call_
->call_expression(),
8873 do_must_eval_in_order() const
8877 do_get_tree(Translate_context
*);
8880 // The underlying call expression.
8882 // Which result we want.
8883 unsigned int index_
;
8886 // Traverse a call result.
8889 Call_result_expression::do_traverse(Traverse
* traverse
)
8891 if (traverse
->remember_expression(this->call_
))
8893 // We have already traversed the call expression.
8894 return TRAVERSE_CONTINUE
;
8896 return Expression::traverse(&this->call_
, traverse
);
8902 Call_result_expression::do_type()
8904 if (this->classification() == EXPRESSION_ERROR
)
8905 return Type::make_error_type();
8907 // THIS->CALL_ can be replaced with a temporary reference due to
8908 // Call_expression::do_must_eval_in_order when there is an error.
8909 Call_expression
* ce
= this->call_
->call_expression();
8912 this->set_is_error();
8913 return Type::make_error_type();
8915 Function_type
* fntype
= ce
->get_function_type();
8918 this->set_is_error();
8919 return Type::make_error_type();
8921 const Typed_identifier_list
* results
= fntype
->results();
8922 if (results
== NULL
)
8924 this->report_error(_("number of results does not match "
8925 "number of values"));
8926 return Type::make_error_type();
8928 Typed_identifier_list::const_iterator pr
= results
->begin();
8929 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8931 if (pr
== results
->end())
8935 if (pr
== results
->end())
8937 this->report_error(_("number of results does not match "
8938 "number of values"));
8939 return Type::make_error_type();
8944 // Check the type. Just make sure that we trigger the warning in
8948 Call_result_expression::do_check_types(Gogo
*)
8953 // Determine the type. We have nothing to do here, but the 0 result
8954 // needs to pass down to the caller.
8957 Call_result_expression::do_determine_type(const Type_context
*)
8959 if (this->index_
== 0)
8960 this->call_
->determine_type_no_context();
8966 Call_result_expression::do_get_tree(Translate_context
* context
)
8968 tree call_tree
= this->call_
->get_tree(context
);
8969 if (call_tree
== error_mark_node
)
8970 return error_mark_node
;
8971 if (TREE_CODE(TREE_TYPE(call_tree
)) != RECORD_TYPE
)
8973 gcc_assert(saw_errors());
8974 return error_mark_node
;
8976 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
8977 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8979 gcc_assert(field
!= NULL_TREE
);
8980 field
= DECL_CHAIN(field
);
8982 gcc_assert(field
!= NULL_TREE
);
8983 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
8986 // Make a reference to a single result of a call which returns
8987 // multiple results.
8990 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
8992 return new Call_result_expression(call
, index
);
8995 // Class Index_expression.
9000 Index_expression::do_traverse(Traverse
* traverse
)
9002 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
9003 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
9004 || (this->end_
!= NULL
9005 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
9006 return TRAVERSE_EXIT
;
9007 return TRAVERSE_CONTINUE
;
9010 // Lower an index expression. This converts the generic index
9011 // expression into an array index, a string index, or a map index.
9014 Index_expression::do_lower(Gogo
*, Named_object
*, int)
9016 source_location location
= this->location();
9017 Expression
* left
= this->left_
;
9018 Expression
* start
= this->start_
;
9019 Expression
* end
= this->end_
;
9021 Type
* type
= left
->type();
9022 if (type
->is_error_type())
9023 return Expression::make_error(location
);
9024 else if (type
->array_type() != NULL
)
9025 return Expression::make_array_index(left
, start
, end
, location
);
9026 else if (type
->points_to() != NULL
9027 && type
->points_to()->array_type() != NULL
9028 && !type
->points_to()->is_open_array_type())
9030 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
9032 return Expression::make_array_index(deref
, start
, end
, location
);
9034 else if (type
->is_string_type())
9035 return Expression::make_string_index(left
, start
, end
, location
);
9036 else if (type
->map_type() != NULL
)
9040 error_at(location
, "invalid slice of map");
9041 return Expression::make_error(location
);
9043 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
9045 if (this->is_lvalue_
)
9046 ret
->set_is_lvalue();
9052 "attempt to index object which is not array, string, or map");
9053 return Expression::make_error(location
);
9057 // Make an index expression.
9060 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
9061 source_location location
)
9063 return new Index_expression(left
, start
, end
, location
);
9066 // An array index. This is used for both indexing and slicing.
9068 class Array_index_expression
: public Expression
9071 Array_index_expression(Expression
* array
, Expression
* start
,
9072 Expression
* end
, source_location location
)
9073 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
9074 array_(array
), start_(start
), end_(end
), type_(NULL
)
9079 do_traverse(Traverse
*);
9085 do_determine_type(const Type_context
*);
9088 do_check_types(Gogo
*);
9093 return Expression::make_array_index(this->array_
->copy(),
9094 this->start_
->copy(),
9097 : this->end_
->copy()),
9102 do_is_addressable() const;
9105 do_address_taken(bool escapes
)
9106 { this->array_
->address_taken(escapes
); }
9109 do_get_tree(Translate_context
*);
9112 // The array we are getting a value from.
9114 // The start or only index.
9116 // The end index of a slice. This may be NULL for a simple array
9117 // index, or it may be a nil expression for the length of the array.
9119 // The type of the expression.
9123 // Array index traversal.
9126 Array_index_expression::do_traverse(Traverse
* traverse
)
9128 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9129 return TRAVERSE_EXIT
;
9130 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9131 return TRAVERSE_EXIT
;
9132 if (this->end_
!= NULL
)
9134 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9135 return TRAVERSE_EXIT
;
9137 return TRAVERSE_CONTINUE
;
9140 // Return the type of an array index.
9143 Array_index_expression::do_type()
9145 if (this->type_
== NULL
)
9147 Array_type
* type
= this->array_
->type()->array_type();
9149 this->type_
= Type::make_error_type();
9150 else if (this->end_
== NULL
)
9151 this->type_
= type
->element_type();
9152 else if (type
->is_open_array_type())
9154 // A slice of a slice has the same type as the original
9156 this->type_
= this->array_
->type()->deref();
9160 // A slice of an array is a slice.
9161 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9167 // Set the type of an array index.
9170 Array_index_expression::do_determine_type(const Type_context
*)
9172 this->array_
->determine_type_no_context();
9173 Type_context
subcontext(NULL
, true);
9174 this->start_
->determine_type(&subcontext
);
9175 if (this->end_
!= NULL
)
9176 this->end_
->determine_type(&subcontext
);
9179 // Check types of an array index.
9182 Array_index_expression::do_check_types(Gogo
*)
9184 if (this->start_
->type()->integer_type() == NULL
)
9185 this->report_error(_("index must be integer"));
9186 if (this->end_
!= NULL
9187 && this->end_
->type()->integer_type() == NULL
9188 && !this->end_
->is_nil_expression())
9189 this->report_error(_("slice end must be integer"));
9191 Array_type
* array_type
= this->array_
->type()->array_type();
9192 if (array_type
== NULL
)
9194 gcc_assert(this->array_
->type()->is_error_type());
9198 unsigned int int_bits
=
9199 Type::lookup_integer_type("int")->integer_type()->bits();
9204 bool lval_valid
= (array_type
->length() != NULL
9205 && array_type
->length()->integer_constant_value(true,
9210 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9212 if (mpz_sgn(ival
) < 0
9213 || mpz_sizeinbase(ival
, 2) >= int_bits
9215 && (this->end_
== NULL
9216 ? mpz_cmp(ival
, lval
) >= 0
9217 : mpz_cmp(ival
, lval
) > 0)))
9219 error_at(this->start_
->location(), "array index out of bounds");
9220 this->set_is_error();
9223 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9225 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9227 if (mpz_sgn(ival
) < 0
9228 || mpz_sizeinbase(ival
, 2) >= int_bits
9229 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
9231 error_at(this->end_
->location(), "array index out of bounds");
9232 this->set_is_error();
9239 // A slice of an array requires an addressable array. A slice of a
9240 // slice is always possible.
9241 if (this->end_
!= NULL
9242 && !array_type
->is_open_array_type()
9243 && !this->array_
->is_addressable())
9244 this->report_error(_("array is not addressable"));
9247 // Return whether this expression is addressable.
9250 Array_index_expression::do_is_addressable() const
9252 // A slice expression is not addressable.
9253 if (this->end_
!= NULL
)
9256 // An index into a slice is addressable.
9257 if (this->array_
->type()->is_open_array_type())
9260 // An index into an array is addressable if the array is
9262 return this->array_
->is_addressable();
9265 // Get a tree for an array index.
9268 Array_index_expression::do_get_tree(Translate_context
* context
)
9270 Gogo
* gogo
= context
->gogo();
9271 source_location loc
= this->location();
9273 Array_type
* array_type
= this->array_
->type()->array_type();
9274 if (array_type
== NULL
)
9276 gcc_assert(this->array_
->type()->is_error_type());
9277 return error_mark_node
;
9280 tree type_tree
= array_type
->get_tree(gogo
);
9281 if (type_tree
== error_mark_node
)
9282 return error_mark_node
;
9284 tree array_tree
= this->array_
->get_tree(context
);
9285 if (array_tree
== error_mark_node
)
9286 return error_mark_node
;
9288 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9289 array_tree
= save_expr(array_tree
);
9290 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9291 if (length_tree
== error_mark_node
)
9292 return error_mark_node
;
9293 length_tree
= save_expr(length_tree
);
9294 tree length_type
= TREE_TYPE(length_tree
);
9296 tree bad_index
= boolean_false_node
;
9298 tree start_tree
= this->start_
->get_tree(context
);
9299 if (start_tree
== error_mark_node
)
9300 return error_mark_node
;
9301 if (!DECL_P(start_tree
))
9302 start_tree
= save_expr(start_tree
);
9303 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9304 start_tree
= convert_to_integer(length_type
, start_tree
);
9306 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9309 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9310 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9311 fold_build2_loc(loc
,
9315 boolean_type_node
, start_tree
,
9318 int code
= (array_type
->length() != NULL
9319 ? (this->end_
== NULL
9320 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9321 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9322 : (this->end_
== NULL
9323 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9324 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9325 tree crash
= Gogo::runtime_error(code
, loc
);
9327 if (this->end_
== NULL
)
9329 // Simple array indexing. This has to return an l-value, so
9330 // wrap the index check into START_TREE.
9331 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9332 build3(COND_EXPR
, void_type_node
,
9333 bad_index
, crash
, NULL_TREE
),
9335 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9337 if (array_type
->length() != NULL
)
9340 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9341 start_tree
, NULL_TREE
, NULL_TREE
);
9346 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9347 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9348 if (element_type_tree
== error_mark_node
)
9349 return error_mark_node
;
9350 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9351 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9352 start_tree
, element_size
);
9353 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9354 TREE_TYPE(values
), values
, offset
);
9355 return build_fold_indirect_ref(ptr
);
9361 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9362 if (capacity_tree
== error_mark_node
)
9363 return error_mark_node
;
9364 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9367 if (this->end_
->is_nil_expression())
9368 end_tree
= length_tree
;
9371 end_tree
= this->end_
->get_tree(context
);
9372 if (end_tree
== error_mark_node
)
9373 return error_mark_node
;
9374 if (!DECL_P(end_tree
))
9375 end_tree
= save_expr(end_tree
);
9376 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9377 end_tree
= convert_to_integer(length_type
, end_tree
);
9379 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9382 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9384 capacity_tree
= save_expr(capacity_tree
);
9385 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9386 fold_build2_loc(loc
, LT_EXPR
,
9388 end_tree
, start_tree
),
9389 fold_build2_loc(loc
, GT_EXPR
,
9391 end_tree
, capacity_tree
));
9392 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9393 bad_index
, bad_end
);
9396 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9397 if (element_type_tree
== error_mark_node
)
9398 return error_mark_node
;
9399 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9401 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9402 fold_convert_loc(loc
, sizetype
, start_tree
),
9405 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9406 if (value_pointer
== error_mark_node
)
9407 return error_mark_node
;
9409 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9410 TREE_TYPE(value_pointer
),
9411 value_pointer
, offset
);
9413 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9414 end_tree
, start_tree
);
9416 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9417 capacity_tree
, start_tree
);
9419 tree struct_tree
= this->type()->get_tree(gogo
);
9420 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9422 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9424 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9425 tree field
= TYPE_FIELDS(struct_tree
);
9426 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9428 elt
->value
= value_pointer
;
9430 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9431 field
= DECL_CHAIN(field
);
9432 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9434 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9436 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9437 field
= DECL_CHAIN(field
);
9438 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9440 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9442 tree constructor
= build_constructor(struct_tree
, init
);
9444 if (TREE_CONSTANT(value_pointer
)
9445 && TREE_CONSTANT(result_length_tree
)
9446 && TREE_CONSTANT(result_capacity_tree
))
9447 TREE_CONSTANT(constructor
) = 1;
9449 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9450 build3(COND_EXPR
, void_type_node
,
9451 bad_index
, crash
, NULL_TREE
),
9455 // Make an array index expression. END may be NULL.
9458 Expression::make_array_index(Expression
* array
, Expression
* start
,
9459 Expression
* end
, source_location location
)
9461 // Taking a slice of a composite literal requires moving the literal
9463 if (end
!= NULL
&& array
->is_composite_literal())
9465 array
= Expression::make_heap_composite(array
, location
);
9466 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9468 return new Array_index_expression(array
, start
, end
, location
);
9471 // A string index. This is used for both indexing and slicing.
9473 class String_index_expression
: public Expression
9476 String_index_expression(Expression
* string
, Expression
* start
,
9477 Expression
* end
, source_location location
)
9478 : Expression(EXPRESSION_STRING_INDEX
, location
),
9479 string_(string
), start_(start
), end_(end
)
9484 do_traverse(Traverse
*);
9490 do_determine_type(const Type_context
*);
9493 do_check_types(Gogo
*);
9498 return Expression::make_string_index(this->string_
->copy(),
9499 this->start_
->copy(),
9502 : this->end_
->copy()),
9507 do_get_tree(Translate_context
*);
9510 // The string we are getting a value from.
9511 Expression
* string_
;
9512 // The start or only index.
9514 // The end index of a slice. This may be NULL for a single index,
9515 // or it may be a nil expression for the length of the string.
9519 // String index traversal.
9522 String_index_expression::do_traverse(Traverse
* traverse
)
9524 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9525 return TRAVERSE_EXIT
;
9526 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9527 return TRAVERSE_EXIT
;
9528 if (this->end_
!= NULL
)
9530 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9531 return TRAVERSE_EXIT
;
9533 return TRAVERSE_CONTINUE
;
9536 // Return the type of a string index.
9539 String_index_expression::do_type()
9541 if (this->end_
== NULL
)
9542 return Type::lookup_integer_type("uint8");
9544 return Type::make_string_type();
9547 // Determine the type of a string index.
9550 String_index_expression::do_determine_type(const Type_context
*)
9552 this->string_
->determine_type_no_context();
9553 Type_context
subcontext(NULL
, true);
9554 this->start_
->determine_type(&subcontext
);
9555 if (this->end_
!= NULL
)
9556 this->end_
->determine_type(&subcontext
);
9559 // Check types of a string index.
9562 String_index_expression::do_check_types(Gogo
*)
9564 if (this->start_
->type()->integer_type() == NULL
)
9565 this->report_error(_("index must be integer"));
9566 if (this->end_
!= NULL
9567 && this->end_
->type()->integer_type() == NULL
9568 && !this->end_
->is_nil_expression())
9569 this->report_error(_("slice end must be integer"));
9572 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9577 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9579 if (mpz_sgn(ival
) < 0
9580 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9582 error_at(this->start_
->location(), "string index out of bounds");
9583 this->set_is_error();
9586 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9588 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9590 if (mpz_sgn(ival
) < 0
9591 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9593 error_at(this->end_
->location(), "string index out of bounds");
9594 this->set_is_error();
9601 // Get a tree for a string index.
9604 String_index_expression::do_get_tree(Translate_context
* context
)
9606 source_location loc
= this->location();
9608 tree string_tree
= this->string_
->get_tree(context
);
9609 if (string_tree
== error_mark_node
)
9610 return error_mark_node
;
9612 if (this->string_
->type()->points_to() != NULL
)
9613 string_tree
= build_fold_indirect_ref(string_tree
);
9614 if (!DECL_P(string_tree
))
9615 string_tree
= save_expr(string_tree
);
9616 tree string_type
= TREE_TYPE(string_tree
);
9618 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9619 length_tree
= save_expr(length_tree
);
9620 tree length_type
= TREE_TYPE(length_tree
);
9622 tree bad_index
= boolean_false_node
;
9624 tree start_tree
= this->start_
->get_tree(context
);
9625 if (start_tree
== error_mark_node
)
9626 return error_mark_node
;
9627 if (!DECL_P(start_tree
))
9628 start_tree
= save_expr(start_tree
);
9629 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9630 start_tree
= convert_to_integer(length_type
, start_tree
);
9632 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9635 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9637 int code
= (this->end_
== NULL
9638 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9639 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9640 tree crash
= Gogo::runtime_error(code
, loc
);
9642 if (this->end_
== NULL
)
9644 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9646 fold_build2_loc(loc
, GE_EXPR
,
9648 start_tree
, length_tree
));
9650 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9651 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9653 fold_convert_loc(loc
, sizetype
, start_tree
));
9654 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9656 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9657 build3(COND_EXPR
, void_type_node
,
9658 bad_index
, crash
, NULL_TREE
),
9664 if (this->end_
->is_nil_expression())
9665 end_tree
= build_int_cst(length_type
, -1);
9668 end_tree
= this->end_
->get_tree(context
);
9669 if (end_tree
== error_mark_node
)
9670 return error_mark_node
;
9671 if (!DECL_P(end_tree
))
9672 end_tree
= save_expr(end_tree
);
9673 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9674 end_tree
= convert_to_integer(length_type
, end_tree
);
9676 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9679 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9682 static tree strslice_fndecl
;
9683 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9685 "__go_string_slice",
9694 if (ret
== error_mark_node
)
9695 return error_mark_node
;
9696 // This will panic if the bounds are out of range for the
9698 TREE_NOTHROW(strslice_fndecl
) = 0;
9700 if (bad_index
== boolean_false_node
)
9703 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9704 build3(COND_EXPR
, void_type_node
,
9705 bad_index
, crash
, NULL_TREE
),
9710 // Make a string index expression. END may be NULL.
9713 Expression::make_string_index(Expression
* string
, Expression
* start
,
9714 Expression
* end
, source_location location
)
9716 return new String_index_expression(string
, start
, end
, location
);
9721 // Get the type of the map.
9724 Map_index_expression::get_map_type() const
9726 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9728 gcc_assert(saw_errors());
9732 // Map index traversal.
9735 Map_index_expression::do_traverse(Traverse
* traverse
)
9737 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9738 return TRAVERSE_EXIT
;
9739 return Expression::traverse(&this->index_
, traverse
);
9742 // Return the type of a map index.
9745 Map_index_expression::do_type()
9747 Map_type
* mt
= this->get_map_type();
9749 return Type::make_error_type();
9750 Type
* type
= mt
->val_type();
9751 // If this map index is in a tuple assignment, we actually return a
9752 // pointer to the value type. Tuple_map_assignment_statement is
9753 // responsible for handling this correctly. We need to get the type
9754 // right in case this gets assigned to a temporary variable.
9755 if (this->is_in_tuple_assignment_
)
9756 type
= Type::make_pointer_type(type
);
9760 // Fix the type of a map index.
9763 Map_index_expression::do_determine_type(const Type_context
*)
9765 this->map_
->determine_type_no_context();
9766 Map_type
* mt
= this->get_map_type();
9767 Type
* key_type
= mt
== NULL
? NULL
: mt
->key_type();
9768 Type_context
subcontext(key_type
, false);
9769 this->index_
->determine_type(&subcontext
);
9772 // Check types of a map index.
9775 Map_index_expression::do_check_types(Gogo
*)
9778 Map_type
* mt
= this->get_map_type();
9781 if (!Type::are_assignable(mt
->key_type(), this->index_
->type(), &reason
))
9784 this->report_error(_("incompatible type for map index"));
9787 error_at(this->location(), "incompatible type for map index (%s)",
9789 this->set_is_error();
9794 // Get a tree for a map index.
9797 Map_index_expression::do_get_tree(Translate_context
* context
)
9799 Map_type
* type
= this->get_map_type();
9801 return error_mark_node
;
9803 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9804 if (valptr
== error_mark_node
)
9805 return error_mark_node
;
9806 valptr
= save_expr(valptr
);
9808 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9810 if (this->is_lvalue_
)
9811 return build_fold_indirect_ref(valptr
);
9812 else if (this->is_in_tuple_assignment_
)
9814 // Tuple_map_assignment_statement is responsible for using this
9820 return fold_build3(COND_EXPR
, val_type_tree
,
9821 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9822 fold_convert(TREE_TYPE(valptr
),
9823 null_pointer_node
)),
9824 type
->val_type()->get_init_tree(context
->gogo(),
9826 build_fold_indirect_ref(valptr
));
9830 // Get a tree for the map index. This returns a tree which evaluates
9831 // to a pointer to a value. The pointer will be NULL if the key is
9835 Map_index_expression::get_value_pointer(Translate_context
* context
,
9838 Map_type
* type
= this->get_map_type();
9840 return error_mark_node
;
9842 tree map_tree
= this->map_
->get_tree(context
);
9843 tree index_tree
= this->index_
->get_tree(context
);
9844 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9845 this->index_
->type(),
9848 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9849 return error_mark_node
;
9851 if (this->map_
->type()->points_to() != NULL
)
9852 map_tree
= build_fold_indirect_ref(map_tree
);
9854 // We need to pass in a pointer to the key, so stuff it into a
9856 tree tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9857 DECL_IGNORED_P(tmp
) = 0;
9858 DECL_INITIAL(tmp
) = index_tree
;
9859 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9860 tree tmpref
= fold_convert(const_ptr_type_node
, build_fold_addr_expr(tmp
));
9861 TREE_ADDRESSABLE(tmp
) = 1;
9863 static tree map_index_fndecl
;
9864 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9868 const_ptr_type_node
,
9869 TREE_TYPE(map_tree
),
9871 const_ptr_type_node
,
9876 : boolean_false_node
));
9877 if (call
== error_mark_node
)
9878 return error_mark_node
;
9879 // This can panic on a map of interface type if the interface holds
9880 // an uncomparable or unhashable type.
9881 TREE_NOTHROW(map_index_fndecl
) = 0;
9883 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9884 if (val_type_tree
== error_mark_node
)
9885 return error_mark_node
;
9886 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9888 return build2(COMPOUND_EXPR
, ptr_val_type_tree
,
9890 fold_convert(ptr_val_type_tree
, call
));
9893 // Make a map index expression.
9895 Map_index_expression
*
9896 Expression::make_map_index(Expression
* map
, Expression
* index
,
9897 source_location location
)
9899 return new Map_index_expression(map
, index
, location
);
9902 // Class Field_reference_expression.
9904 // Return the type of a field reference.
9907 Field_reference_expression::do_type()
9909 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9910 gcc_assert(struct_type
!= NULL
);
9911 return struct_type
->field(this->field_index_
)->type();
9914 // Check the types for a field reference.
9917 Field_reference_expression::do_check_types(Gogo
*)
9919 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9920 gcc_assert(struct_type
!= NULL
);
9921 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9924 // Get a tree for a field reference.
9927 Field_reference_expression::do_get_tree(Translate_context
* context
)
9929 tree struct_tree
= this->expr_
->get_tree(context
);
9930 if (struct_tree
== error_mark_node
9931 || TREE_TYPE(struct_tree
) == error_mark_node
)
9932 return error_mark_node
;
9933 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
9934 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
9935 if (field
== NULL_TREE
)
9937 // This can happen for a type which refers to itself indirectly
9938 // and then turns out to be erroneous.
9939 gcc_assert(saw_errors());
9940 return error_mark_node
;
9942 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
9944 field
= DECL_CHAIN(field
);
9945 gcc_assert(field
!= NULL_TREE
);
9947 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
9951 // Make a reference to a qualified identifier in an expression.
9953 Field_reference_expression
*
9954 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
9955 source_location location
)
9957 return new Field_reference_expression(expr
, field_index
, location
);
9960 // Class Interface_field_reference_expression.
9962 // Return a tree for the pointer to the function to call.
9965 Interface_field_reference_expression::get_function_tree(Translate_context
*,
9968 if (this->expr_
->type()->points_to() != NULL
)
9969 expr
= build_fold_indirect_ref(expr
);
9971 tree expr_type
= TREE_TYPE(expr
);
9972 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9974 tree field
= TYPE_FIELDS(expr_type
);
9975 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
9977 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9978 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
9980 table
= build_fold_indirect_ref(table
);
9981 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
9983 std::string name
= Gogo::unpack_hidden_name(this->name_
);
9984 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
9986 field
= DECL_CHAIN(field
))
9988 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
9991 gcc_assert(field
!= NULL_TREE
);
9993 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
9996 // Return a tree for the first argument to pass to the interface
10000 Interface_field_reference_expression::get_underlying_object_tree(
10001 Translate_context
*,
10004 if (this->expr_
->type()->points_to() != NULL
)
10005 expr
= build_fold_indirect_ref(expr
);
10007 tree expr_type
= TREE_TYPE(expr
);
10008 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
10010 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
10011 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
10013 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
10019 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
10021 return Expression::traverse(&this->expr_
, traverse
);
10024 // Return the type of an interface field reference.
10027 Interface_field_reference_expression::do_type()
10029 Type
* expr_type
= this->expr_
->type();
10031 Type
* points_to
= expr_type
->points_to();
10032 if (points_to
!= NULL
)
10033 expr_type
= points_to
;
10035 Interface_type
* interface_type
= expr_type
->interface_type();
10036 if (interface_type
== NULL
)
10037 return Type::make_error_type();
10039 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
10040 if (method
== NULL
)
10041 return Type::make_error_type();
10043 return method
->type();
10046 // Determine types.
10049 Interface_field_reference_expression::do_determine_type(const Type_context
*)
10051 this->expr_
->determine_type_no_context();
10054 // Check the types for an interface field reference.
10057 Interface_field_reference_expression::do_check_types(Gogo
*)
10059 Type
* type
= this->expr_
->type();
10061 Type
* points_to
= type
->points_to();
10062 if (points_to
!= NULL
)
10065 Interface_type
* interface_type
= type
->interface_type();
10066 if (interface_type
== NULL
)
10067 this->report_error(_("expected interface or pointer to interface"));
10070 const Typed_identifier
* method
=
10071 interface_type
->find_method(this->name_
);
10072 if (method
== NULL
)
10074 error_at(this->location(), "method %qs not in interface",
10075 Gogo::message_name(this->name_
).c_str());
10076 this->set_is_error();
10081 // Get a tree for a reference to a field in an interface. There is no
10082 // standard tree type representation for this: it's a function
10083 // attached to its first argument, like a Bound_method_expression.
10084 // The only places it may currently be used are in a Call_expression
10085 // or a Go_statement, which will take it apart directly. So this has
10086 // nothing to do at present.
10089 Interface_field_reference_expression::do_get_tree(Translate_context
*)
10094 // Make a reference to a field in an interface.
10097 Expression::make_interface_field_reference(Expression
* expr
,
10098 const std::string
& field
,
10099 source_location location
)
10101 return new Interface_field_reference_expression(expr
, field
, location
);
10104 // A general selector. This is a Parser_expression for LEFT.NAME. It
10105 // is lowered after we know the type of the left hand side.
10107 class Selector_expression
: public Parser_expression
10110 Selector_expression(Expression
* left
, const std::string
& name
,
10111 source_location location
)
10112 : Parser_expression(EXPRESSION_SELECTOR
, location
),
10113 left_(left
), name_(name
)
10118 do_traverse(Traverse
* traverse
)
10119 { return Expression::traverse(&this->left_
, traverse
); }
10122 do_lower(Gogo
*, Named_object
*, int);
10127 return new Selector_expression(this->left_
->copy(), this->name_
,
10133 lower_method_expression(Gogo
*);
10135 // The expression on the left hand side.
10137 // The name on the right hand side.
10141 // Lower a selector expression once we know the real type of the left
10145 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
10147 Expression
* left
= this->left_
;
10148 if (left
->is_type_expression())
10149 return this->lower_method_expression(gogo
);
10150 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
10154 // Lower a method expression T.M or (*T).M. We turn this into a
10155 // function literal.
10158 Selector_expression::lower_method_expression(Gogo
* gogo
)
10160 source_location location
= this->location();
10161 Type
* type
= this->left_
->type();
10162 const std::string
& name(this->name_
);
10165 if (type
->points_to() == NULL
)
10166 is_pointer
= false;
10170 type
= type
->points_to();
10172 Named_type
* nt
= type
->named_type();
10176 ("method expression requires named type or "
10177 "pointer to named type"));
10178 return Expression::make_error(location
);
10182 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
10183 if (method
== NULL
)
10186 error_at(location
, "type %<%s%> has no method %<%s%>",
10187 nt
->message_name().c_str(),
10188 Gogo::message_name(name
).c_str());
10190 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
10191 Gogo::message_name(name
).c_str(),
10192 nt
->message_name().c_str());
10193 return Expression::make_error(location
);
10196 if (!is_pointer
&& !method
->is_value_method())
10198 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
10199 nt
->message_name().c_str(),
10200 Gogo::message_name(name
).c_str());
10201 return Expression::make_error(location
);
10204 // Build a new function type in which the receiver becomes the first
10206 Function_type
* method_type
= method
->type();
10207 gcc_assert(method_type
->is_method());
10209 const char* const receiver_name
= "$this";
10210 Typed_identifier_list
* parameters
= new Typed_identifier_list();
10211 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
10214 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
10215 if (method_parameters
!= NULL
)
10217 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10218 p
!= method_parameters
->end();
10220 parameters
->push_back(*p
);
10223 const Typed_identifier_list
* method_results
= method_type
->results();
10224 Typed_identifier_list
* results
;
10225 if (method_results
== NULL
)
10229 results
= new Typed_identifier_list();
10230 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
10231 p
!= method_results
->end();
10233 results
->push_back(*p
);
10236 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10238 if (method_type
->is_varargs())
10239 fntype
->set_is_varargs();
10241 // We generate methods which always takes a pointer to the receiver
10242 // as their first argument. If this is for a pointer type, we can
10243 // simply reuse the existing function. We use an internal hack to
10244 // get the right type.
10248 Named_object
* mno
= (method
->needs_stub_method()
10249 ? method
->stub_object()
10250 : method
->named_object());
10251 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10252 f
= Expression::make_cast(fntype
, f
, location
);
10253 Type_conversion_expression
* tce
=
10254 static_cast<Type_conversion_expression
*>(f
);
10255 tce
->set_may_convert_function_types();
10259 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10262 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10263 gcc_assert(vno
!= NULL
);
10264 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10265 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10266 gcc_assert(bm
!= NULL
&& !bm
->is_error_expression());
10268 Expression_list
* args
;
10269 if (method_parameters
== NULL
)
10273 args
= new Expression_list();
10274 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10275 p
!= method_parameters
->end();
10278 vno
= gogo
->lookup(p
->name(), NULL
);
10279 gcc_assert(vno
!= NULL
);
10280 args
->push_back(Expression::make_var_reference(vno
, location
));
10284 Call_expression
* call
= Expression::make_call(bm
, args
,
10285 method_type
->is_varargs(),
10288 size_t count
= call
->result_count();
10291 s
= Statement::make_statement(call
);
10294 Expression_list
* retvals
= new Expression_list();
10296 retvals
->push_back(call
);
10299 for (size_t i
= 0; i
< count
; ++i
)
10300 retvals
->push_back(Expression::make_call_result(call
, i
));
10302 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
10303 retvals
, location
);
10305 gogo
->add_statement(s
);
10307 gogo
->finish_function(location
);
10309 return Expression::make_func_reference(no
, NULL
, location
);
10312 // Make a selector expression.
10315 Expression::make_selector(Expression
* left
, const std::string
& name
,
10316 source_location location
)
10318 return new Selector_expression(left
, name
, location
);
10321 // Implement the builtin function new.
10323 class Allocation_expression
: public Expression
10326 Allocation_expression(Type
* type
, source_location location
)
10327 : Expression(EXPRESSION_ALLOCATION
, location
),
10333 do_traverse(Traverse
* traverse
)
10334 { return Type::traverse(this->type_
, traverse
); }
10338 { return Type::make_pointer_type(this->type_
); }
10341 do_determine_type(const Type_context
*)
10345 do_check_types(Gogo
*);
10349 { return new Allocation_expression(this->type_
, this->location()); }
10352 do_get_tree(Translate_context
*);
10355 // The type we are allocating.
10359 // Check the type of an allocation expression.
10362 Allocation_expression::do_check_types(Gogo
*)
10364 if (this->type_
->function_type() != NULL
)
10365 this->report_error(_("invalid new of function type"));
10368 // Return a tree for an allocation expression.
10371 Allocation_expression::do_get_tree(Translate_context
* context
)
10373 tree type_tree
= this->type_
->get_tree(context
->gogo());
10374 if (type_tree
== error_mark_node
)
10375 return error_mark_node
;
10376 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10377 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10379 if (space
== error_mark_node
)
10380 return error_mark_node
;
10381 return fold_convert(build_pointer_type(type_tree
), space
);
10384 // Make an allocation expression.
10387 Expression::make_allocation(Type
* type
, source_location location
)
10389 return new Allocation_expression(type
, location
);
10392 // Implement the builtin function make.
10394 class Make_expression
: public Expression
10397 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10398 : Expression(EXPRESSION_MAKE
, location
),
10399 type_(type
), args_(args
)
10404 do_traverse(Traverse
* traverse
);
10408 { return this->type_
; }
10411 do_determine_type(const Type_context
*);
10414 do_check_types(Gogo
*);
10419 return new Make_expression(this->type_
, this->args_
->copy(),
10424 do_get_tree(Translate_context
*);
10427 // The type we are making.
10429 // The arguments to pass to the make routine.
10430 Expression_list
* args_
;
10436 Make_expression::do_traverse(Traverse
* traverse
)
10438 if (this->args_
!= NULL
10439 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10440 return TRAVERSE_EXIT
;
10441 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10442 return TRAVERSE_EXIT
;
10443 return TRAVERSE_CONTINUE
;
10446 // Set types of arguments.
10449 Make_expression::do_determine_type(const Type_context
*)
10451 if (this->args_
!= NULL
)
10453 Type_context
context(Type::lookup_integer_type("int"), false);
10454 for (Expression_list::const_iterator pe
= this->args_
->begin();
10455 pe
!= this->args_
->end();
10457 (*pe
)->determine_type(&context
);
10461 // Check types for a make expression.
10464 Make_expression::do_check_types(Gogo
*)
10466 if (this->type_
->channel_type() == NULL
10467 && this->type_
->map_type() == NULL
10468 && (this->type_
->array_type() == NULL
10469 || this->type_
->array_type()->length() != NULL
))
10470 this->report_error(_("invalid type for make function"));
10471 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10472 this->set_is_error();
10475 // Return a tree for a make expression.
10478 Make_expression::do_get_tree(Translate_context
* context
)
10480 return this->type_
->make_expression_tree(context
, this->args_
,
10484 // Make a make expression.
10487 Expression::make_make(Type
* type
, Expression_list
* args
,
10488 source_location location
)
10490 return new Make_expression(type
, args
, location
);
10493 // Construct a struct.
10495 class Struct_construction_expression
: public Expression
10498 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10499 source_location location
)
10500 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10501 type_(type
), vals_(vals
)
10504 // Return whether this is a constant initializer.
10506 is_constant_struct() const;
10510 do_traverse(Traverse
* traverse
);
10514 { return this->type_
; }
10517 do_determine_type(const Type_context
*);
10520 do_check_types(Gogo
*);
10525 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10530 do_is_addressable() const
10534 do_get_tree(Translate_context
*);
10537 do_export(Export
*) const;
10540 // The type of the struct to construct.
10542 // The list of values, in order of the fields in the struct. A NULL
10543 // entry means that the field should be zero-initialized.
10544 Expression_list
* vals_
;
10550 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10552 if (this->vals_
!= NULL
10553 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10554 return TRAVERSE_EXIT
;
10555 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10556 return TRAVERSE_EXIT
;
10557 return TRAVERSE_CONTINUE
;
10560 // Return whether this is a constant initializer.
10563 Struct_construction_expression::is_constant_struct() const
10565 if (this->vals_
== NULL
)
10567 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10568 pv
!= this->vals_
->end();
10572 && !(*pv
)->is_constant()
10573 && (!(*pv
)->is_composite_literal()
10574 || (*pv
)->is_nonconstant_composite_literal()))
10578 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10579 for (Struct_field_list::const_iterator pf
= fields
->begin();
10580 pf
!= fields
->end();
10583 // There are no constant constructors for interfaces.
10584 if (pf
->type()->interface_type() != NULL
)
10591 // Final type determination.
10594 Struct_construction_expression::do_determine_type(const Type_context
*)
10596 if (this->vals_
== NULL
)
10598 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10599 Expression_list::const_iterator pv
= this->vals_
->begin();
10600 for (Struct_field_list::const_iterator pf
= fields
->begin();
10601 pf
!= fields
->end();
10604 if (pv
== this->vals_
->end())
10608 Type_context
subcontext(pf
->type(), false);
10609 (*pv
)->determine_type(&subcontext
);
10617 Struct_construction_expression::do_check_types(Gogo
*)
10619 if (this->vals_
== NULL
)
10622 Struct_type
* st
= this->type_
->struct_type();
10623 if (this->vals_
->size() > st
->field_count())
10625 this->report_error(_("too many expressions for struct"));
10629 const Struct_field_list
* fields
= st
->fields();
10630 Expression_list::const_iterator pv
= this->vals_
->begin();
10632 for (Struct_field_list::const_iterator pf
= fields
->begin();
10633 pf
!= fields
->end();
10636 if (pv
== this->vals_
->end())
10638 this->report_error(_("too few expressions for struct"));
10645 std::string reason
;
10646 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10648 if (reason
.empty())
10649 error_at((*pv
)->location(),
10650 "incompatible type for field %d in struct construction",
10653 error_at((*pv
)->location(),
10654 ("incompatible type for field %d in "
10655 "struct construction (%s)"),
10656 i
+ 1, reason
.c_str());
10657 this->set_is_error();
10660 gcc_assert(pv
== this->vals_
->end());
10663 // Return a tree for constructing a struct.
10666 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10668 Gogo
* gogo
= context
->gogo();
10670 if (this->vals_
== NULL
)
10671 return this->type_
->get_init_tree(gogo
, false);
10673 tree type_tree
= this->type_
->get_tree(gogo
);
10674 if (type_tree
== error_mark_node
)
10675 return error_mark_node
;
10676 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10678 bool is_constant
= true;
10679 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10680 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10682 Struct_field_list::const_iterator pf
= fields
->begin();
10683 Expression_list::const_iterator pv
= this->vals_
->begin();
10684 for (tree field
= TYPE_FIELDS(type_tree
);
10685 field
!= NULL_TREE
;
10686 field
= DECL_CHAIN(field
), ++pf
)
10688 gcc_assert(pf
!= fields
->end());
10691 if (pv
== this->vals_
->end())
10692 val
= pf
->type()->get_init_tree(gogo
, false);
10693 else if (*pv
== NULL
)
10695 val
= pf
->type()->get_init_tree(gogo
, false);
10700 val
= Expression::convert_for_assignment(context
, pf
->type(),
10702 (*pv
)->get_tree(context
),
10707 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10708 return error_mark_node
;
10710 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10711 elt
->index
= field
;
10713 if (!TREE_CONSTANT(val
))
10714 is_constant
= false;
10716 gcc_assert(pf
== fields
->end());
10718 tree ret
= build_constructor(type_tree
, elts
);
10720 TREE_CONSTANT(ret
) = 1;
10724 // Export a struct construction.
10727 Struct_construction_expression::do_export(Export
* exp
) const
10729 exp
->write_c_string("convert(");
10730 exp
->write_type(this->type_
);
10731 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10732 pv
!= this->vals_
->end();
10735 exp
->write_c_string(", ");
10737 (*pv
)->export_expression(exp
);
10739 exp
->write_c_string(")");
10742 // Make a struct composite literal. This used by the thunk code.
10745 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10746 source_location location
)
10748 gcc_assert(type
->struct_type() != NULL
);
10749 return new Struct_construction_expression(type
, vals
, location
);
10752 // Construct an array. This class is not used directly; instead we
10753 // use the child classes, Fixed_array_construction_expression and
10754 // Open_array_construction_expression.
10756 class Array_construction_expression
: public Expression
10759 Array_construction_expression(Expression_classification classification
,
10760 Type
* type
, Expression_list
* vals
,
10761 source_location location
)
10762 : Expression(classification
, location
),
10763 type_(type
), vals_(vals
)
10767 // Return whether this is a constant initializer.
10769 is_constant_array() const;
10771 // Return the number of elements.
10773 element_count() const
10774 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10778 do_traverse(Traverse
* traverse
);
10782 { return this->type_
; }
10785 do_determine_type(const Type_context
*);
10788 do_check_types(Gogo
*);
10791 do_is_addressable() const
10795 do_export(Export
*) const;
10797 // The list of values.
10800 { return this->vals_
; }
10802 // Get a constructor tree for the array values.
10804 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10807 // The type of the array to construct.
10809 // The list of values.
10810 Expression_list
* vals_
;
10816 Array_construction_expression::do_traverse(Traverse
* traverse
)
10818 if (this->vals_
!= NULL
10819 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10820 return TRAVERSE_EXIT
;
10821 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10822 return TRAVERSE_EXIT
;
10823 return TRAVERSE_CONTINUE
;
10826 // Return whether this is a constant initializer.
10829 Array_construction_expression::is_constant_array() const
10831 if (this->vals_
== NULL
)
10834 // There are no constant constructors for interfaces.
10835 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10838 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10839 pv
!= this->vals_
->end();
10843 && !(*pv
)->is_constant()
10844 && (!(*pv
)->is_composite_literal()
10845 || (*pv
)->is_nonconstant_composite_literal()))
10851 // Final type determination.
10854 Array_construction_expression::do_determine_type(const Type_context
*)
10856 if (this->vals_
== NULL
)
10858 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10859 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10860 pv
!= this->vals_
->end();
10864 (*pv
)->determine_type(&subcontext
);
10871 Array_construction_expression::do_check_types(Gogo
*)
10873 if (this->vals_
== NULL
)
10876 Array_type
* at
= this->type_
->array_type();
10878 Type
* element_type
= at
->element_type();
10879 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10880 pv
!= this->vals_
->end();
10884 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10886 error_at((*pv
)->location(),
10887 "incompatible type for element %d in composite literal",
10889 this->set_is_error();
10893 Expression
* length
= at
->length();
10894 if (length
!= NULL
)
10899 if (at
->length()->integer_constant_value(true, val
, &type
))
10901 if (this->vals_
->size() > mpz_get_ui(val
))
10902 this->report_error(_("too many elements in composite literal"));
10908 // Get a constructor tree for the array values.
10911 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10914 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10915 (this->vals_
== NULL
10917 : this->vals_
->size()));
10918 Type
* element_type
= this->type_
->array_type()->element_type();
10919 bool is_constant
= true;
10920 if (this->vals_
!= NULL
)
10923 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10924 pv
!= this->vals_
->end();
10927 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
10928 elt
->index
= size_int(i
);
10930 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10933 tree value_tree
= (*pv
)->get_tree(context
);
10934 elt
->value
= Expression::convert_for_assignment(context
,
10940 if (elt
->value
== error_mark_node
)
10941 return error_mark_node
;
10942 if (!TREE_CONSTANT(elt
->value
))
10943 is_constant
= false;
10947 tree ret
= build_constructor(type_tree
, values
);
10949 TREE_CONSTANT(ret
) = 1;
10953 // Export an array construction.
10956 Array_construction_expression::do_export(Export
* exp
) const
10958 exp
->write_c_string("convert(");
10959 exp
->write_type(this->type_
);
10960 if (this->vals_
!= NULL
)
10962 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10963 pv
!= this->vals_
->end();
10966 exp
->write_c_string(", ");
10968 (*pv
)->export_expression(exp
);
10971 exp
->write_c_string(")");
10974 // Construct a fixed array.
10976 class Fixed_array_construction_expression
:
10977 public Array_construction_expression
10980 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
10981 source_location location
)
10982 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
10983 type
, vals
, location
)
10985 gcc_assert(type
->array_type() != NULL
10986 && type
->array_type()->length() != NULL
);
10993 return new Fixed_array_construction_expression(this->type(),
10994 (this->vals() == NULL
10996 : this->vals()->copy()),
11001 do_get_tree(Translate_context
*);
11004 // Return a tree for constructing a fixed array.
11007 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
11009 return this->get_constructor_tree(context
,
11010 this->type()->get_tree(context
->gogo()));
11013 // Construct an open array.
11015 class Open_array_construction_expression
: public Array_construction_expression
11018 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
11019 source_location location
)
11020 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
11021 type
, vals
, location
)
11023 gcc_assert(type
->array_type() != NULL
11024 && type
->array_type()->length() == NULL
);
11028 // Note that taking the address of an open array literal is invalid.
11033 return new Open_array_construction_expression(this->type(),
11034 (this->vals() == NULL
11036 : this->vals()->copy()),
11041 do_get_tree(Translate_context
*);
11044 // Return a tree for constructing an open array.
11047 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
11049 Array_type
* array_type
= this->type()->array_type();
11050 if (array_type
== NULL
)
11052 gcc_assert(this->type()->is_error_type());
11053 return error_mark_node
;
11056 Type
* element_type
= array_type
->element_type();
11057 tree element_type_tree
= element_type
->get_tree(context
->gogo());
11058 if (element_type_tree
== error_mark_node
)
11059 return error_mark_node
;
11063 if (this->vals() == NULL
|| this->vals()->empty())
11065 // We need to create a unique value.
11066 tree max
= size_int(0);
11067 tree constructor_type
= build_array_type(element_type_tree
,
11068 build_index_type(max
));
11069 if (constructor_type
== error_mark_node
)
11070 return error_mark_node
;
11071 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
11072 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
11073 elt
->index
= size_int(0);
11074 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
11075 values
= build_constructor(constructor_type
, vec
);
11076 if (TREE_CONSTANT(elt
->value
))
11077 TREE_CONSTANT(values
) = 1;
11078 length_tree
= size_int(0);
11082 tree max
= size_int(this->vals()->size() - 1);
11083 tree constructor_type
= build_array_type(element_type_tree
,
11084 build_index_type(max
));
11085 if (constructor_type
== error_mark_node
)
11086 return error_mark_node
;
11087 values
= this->get_constructor_tree(context
, constructor_type
);
11088 length_tree
= size_int(this->vals()->size());
11091 if (values
== error_mark_node
)
11092 return error_mark_node
;
11094 bool is_constant_initializer
= TREE_CONSTANT(values
);
11095 bool is_in_function
= context
->function() != NULL
;
11097 if (is_constant_initializer
)
11099 tree tmp
= build_decl(this->location(), VAR_DECL
,
11100 create_tmp_var_name("C"), TREE_TYPE(values
));
11101 DECL_EXTERNAL(tmp
) = 0;
11102 TREE_PUBLIC(tmp
) = 0;
11103 TREE_STATIC(tmp
) = 1;
11104 DECL_ARTIFICIAL(tmp
) = 1;
11105 if (is_in_function
)
11107 // If this is not a function, we will only initialize the
11108 // value once, so we can use this directly rather than
11109 // copying it. In that case we can't make it read-only,
11110 // because the program is permitted to change it.
11111 TREE_READONLY(tmp
) = 1;
11112 TREE_CONSTANT(tmp
) = 1;
11114 DECL_INITIAL(tmp
) = values
;
11115 rest_of_decl_compilation(tmp
, 1, 0);
11121 if (!is_in_function
&& is_constant_initializer
)
11123 // Outside of a function, we know the initializer will only run
11125 space
= build_fold_addr_expr(values
);
11130 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
11131 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
11133 space
= save_expr(space
);
11135 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
11136 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
11137 TREE_THIS_NOTRAP(ref
) = 1;
11138 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
11141 // Build a constructor for the open array.
11143 tree type_tree
= this->type()->get_tree(context
->gogo());
11144 if (type_tree
== error_mark_node
)
11145 return error_mark_node
;
11146 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11148 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
11150 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11151 tree field
= TYPE_FIELDS(type_tree
);
11152 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
11153 elt
->index
= field
;
11154 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
11156 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11157 field
= DECL_CHAIN(field
);
11158 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
11159 elt
->index
= field
;
11160 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11162 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11163 field
= DECL_CHAIN(field
);
11164 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
11165 elt
->index
= field
;
11166 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11168 tree constructor
= build_constructor(type_tree
, init
);
11169 if (constructor
== error_mark_node
)
11170 return error_mark_node
;
11171 if (!is_in_function
&& is_constant_initializer
)
11172 TREE_CONSTANT(constructor
) = 1;
11174 if (set
== NULL_TREE
)
11175 return constructor
;
11177 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
11180 // Make a slice composite literal. This is used by the type
11181 // descriptor code.
11184 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
11185 source_location location
)
11187 gcc_assert(type
->is_open_array_type());
11188 return new Open_array_construction_expression(type
, vals
, location
);
11191 // Construct a map.
11193 class Map_construction_expression
: public Expression
11196 Map_construction_expression(Type
* type
, Expression_list
* vals
,
11197 source_location location
)
11198 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
11199 type_(type
), vals_(vals
)
11200 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
11204 do_traverse(Traverse
* traverse
);
11208 { return this->type_
; }
11211 do_determine_type(const Type_context
*);
11214 do_check_types(Gogo
*);
11219 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
11224 do_get_tree(Translate_context
*);
11227 do_export(Export
*) const;
11230 // The type of the map to construct.
11232 // The list of values.
11233 Expression_list
* vals_
;
11239 Map_construction_expression::do_traverse(Traverse
* traverse
)
11241 if (this->vals_
!= NULL
11242 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11243 return TRAVERSE_EXIT
;
11244 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11245 return TRAVERSE_EXIT
;
11246 return TRAVERSE_CONTINUE
;
11249 // Final type determination.
11252 Map_construction_expression::do_determine_type(const Type_context
*)
11254 if (this->vals_
== NULL
)
11257 Map_type
* mt
= this->type_
->map_type();
11258 Type_context
key_context(mt
->key_type(), false);
11259 Type_context
val_context(mt
->val_type(), false);
11260 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11261 pv
!= this->vals_
->end();
11264 (*pv
)->determine_type(&key_context
);
11266 (*pv
)->determine_type(&val_context
);
11273 Map_construction_expression::do_check_types(Gogo
*)
11275 if (this->vals_
== NULL
)
11278 Map_type
* mt
= this->type_
->map_type();
11280 Type
* key_type
= mt
->key_type();
11281 Type
* val_type
= mt
->val_type();
11282 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11283 pv
!= this->vals_
->end();
11286 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11288 error_at((*pv
)->location(),
11289 "incompatible type for element %d key in map construction",
11291 this->set_is_error();
11294 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11296 error_at((*pv
)->location(),
11297 ("incompatible type for element %d value "
11298 "in map construction"),
11300 this->set_is_error();
11305 // Return a tree for constructing a map.
11308 Map_construction_expression::do_get_tree(Translate_context
* context
)
11310 Gogo
* gogo
= context
->gogo();
11311 source_location loc
= this->location();
11313 Map_type
* mt
= this->type_
->map_type();
11315 // Build a struct to hold the key and value.
11316 tree struct_type
= make_node(RECORD_TYPE
);
11318 Type
* key_type
= mt
->key_type();
11319 tree id
= get_identifier("__key");
11320 tree key_type_tree
= key_type
->get_tree(gogo
);
11321 if (key_type_tree
== error_mark_node
)
11322 return error_mark_node
;
11323 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type_tree
);
11324 DECL_CONTEXT(key_field
) = struct_type
;
11325 TYPE_FIELDS(struct_type
) = key_field
;
11327 Type
* val_type
= mt
->val_type();
11328 id
= get_identifier("__val");
11329 tree val_type_tree
= val_type
->get_tree(gogo
);
11330 if (val_type_tree
== error_mark_node
)
11331 return error_mark_node
;
11332 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type_tree
);
11333 DECL_CONTEXT(val_field
) = struct_type
;
11334 DECL_CHAIN(key_field
) = val_field
;
11336 layout_type(struct_type
);
11338 bool is_constant
= true;
11343 if (this->vals_
== NULL
|| this->vals_
->empty())
11345 valaddr
= null_pointer_node
;
11346 make_tmp
= NULL_TREE
;
11350 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11351 this->vals_
->size() / 2);
11353 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11354 pv
!= this->vals_
->end();
11357 bool one_is_constant
= true;
11359 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11361 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11362 elt
->index
= key_field
;
11363 tree val_tree
= (*pv
)->get_tree(context
);
11364 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11367 if (elt
->value
== error_mark_node
)
11368 return error_mark_node
;
11369 if (!TREE_CONSTANT(elt
->value
))
11370 one_is_constant
= false;
11374 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11375 elt
->index
= val_field
;
11376 val_tree
= (*pv
)->get_tree(context
);
11377 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11380 if (elt
->value
== error_mark_node
)
11381 return error_mark_node
;
11382 if (!TREE_CONSTANT(elt
->value
))
11383 one_is_constant
= false;
11385 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11386 elt
->index
= size_int(i
);
11387 elt
->value
= build_constructor(struct_type
, one
);
11388 if (one_is_constant
)
11389 TREE_CONSTANT(elt
->value
) = 1;
11391 is_constant
= false;
11394 tree index_type
= build_index_type(size_int(i
- 1));
11395 tree array_type
= build_array_type(struct_type
, index_type
);
11396 tree init
= build_constructor(array_type
, values
);
11398 TREE_CONSTANT(init
) = 1;
11400 if (current_function_decl
!= NULL
)
11402 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11403 DECL_INITIAL(tmp
) = init
;
11404 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11405 TREE_ADDRESSABLE(tmp
) = 1;
11409 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11410 DECL_EXTERNAL(tmp
) = 0;
11411 TREE_PUBLIC(tmp
) = 0;
11412 TREE_STATIC(tmp
) = 1;
11413 DECL_ARTIFICIAL(tmp
) = 1;
11414 if (!TREE_CONSTANT(init
))
11415 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11419 TREE_READONLY(tmp
) = 1;
11420 TREE_CONSTANT(tmp
) = 1;
11421 DECL_INITIAL(tmp
) = init
;
11422 make_tmp
= NULL_TREE
;
11424 rest_of_decl_compilation(tmp
, 1, 0);
11427 valaddr
= build_fold_addr_expr(tmp
);
11430 tree descriptor
= gogo
->map_descriptor(mt
);
11432 tree type_tree
= this->type_
->get_tree(gogo
);
11433 if (type_tree
== error_mark_node
)
11434 return error_mark_node
;
11436 static tree construct_map_fndecl
;
11437 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11439 "__go_construct_map",
11442 TREE_TYPE(descriptor
),
11447 TYPE_SIZE_UNIT(struct_type
),
11449 byte_position(val_field
),
11451 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11452 const_ptr_type_node
,
11453 fold_convert(const_ptr_type_node
, valaddr
));
11454 if (call
== error_mark_node
)
11455 return error_mark_node
;
11458 if (make_tmp
== NULL
)
11461 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11465 // Export an array construction.
11468 Map_construction_expression::do_export(Export
* exp
) const
11470 exp
->write_c_string("convert(");
11471 exp
->write_type(this->type_
);
11472 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11473 pv
!= this->vals_
->end();
11476 exp
->write_c_string(", ");
11477 (*pv
)->export_expression(exp
);
11479 exp
->write_c_string(")");
11482 // A general composite literal. This is lowered to a type specific
11485 class Composite_literal_expression
: public Parser_expression
11488 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11489 Expression_list
* vals
, source_location location
)
11490 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11491 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11496 do_traverse(Traverse
* traverse
);
11499 do_lower(Gogo
*, Named_object
*, int);
11504 return new Composite_literal_expression(this->type_
, this->depth_
,
11506 (this->vals_
== NULL
11508 : this->vals_
->copy()),
11514 lower_struct(Type
*);
11517 lower_array(Type
*);
11520 make_array(Type
*, Expression_list
*);
11523 lower_map(Gogo
*, Named_object
*, Type
*);
11525 // The type of the composite literal.
11527 // The depth within a list of composite literals within a composite
11528 // literal, when the type is omitted.
11530 // The values to put in the composite literal.
11531 Expression_list
* vals_
;
11532 // If this is true, then VALS_ is a list of pairs: a key and a
11533 // value. In an array initializer, a missing key will be NULL.
11540 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11542 if (this->vals_
!= NULL
11543 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11544 return TRAVERSE_EXIT
;
11545 return Type::traverse(this->type_
, traverse
);
11548 // Lower a generic composite literal into a specific version based on
11552 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11554 Type
* type
= this->type_
;
11556 for (int depth
= this->depth_
; depth
> 0; --depth
)
11558 if (type
->array_type() != NULL
)
11559 type
= type
->array_type()->element_type();
11560 else if (type
->map_type() != NULL
)
11561 type
= type
->map_type()->val_type();
11564 if (!type
->is_error_type())
11565 error_at(this->location(),
11566 ("may only omit types within composite literals "
11567 "of slice, array, or map type"));
11568 return Expression::make_error(this->location());
11572 if (type
->is_error_type())
11573 return Expression::make_error(this->location());
11574 else if (type
->struct_type() != NULL
)
11575 return this->lower_struct(type
);
11576 else if (type
->array_type() != NULL
)
11577 return this->lower_array(type
);
11578 else if (type
->map_type() != NULL
)
11579 return this->lower_map(gogo
, function
, type
);
11582 error_at(this->location(),
11583 ("expected struct, slice, array, or map type "
11584 "for composite literal"));
11585 return Expression::make_error(this->location());
11589 // Lower a struct composite literal.
11592 Composite_literal_expression::lower_struct(Type
* type
)
11594 source_location location
= this->location();
11595 Struct_type
* st
= type
->struct_type();
11596 if (this->vals_
== NULL
|| !this->has_keys_
)
11597 return new Struct_construction_expression(type
, this->vals_
, location
);
11599 size_t field_count
= st
->field_count();
11600 std::vector
<Expression
*> vals(field_count
);
11601 Expression_list::const_iterator p
= this->vals_
->begin();
11602 while (p
!= this->vals_
->end())
11604 Expression
* name_expr
= *p
;
11607 gcc_assert(p
!= this->vals_
->end());
11608 Expression
* val
= *p
;
11612 if (name_expr
== NULL
)
11614 error_at(val
->location(), "mixture of field and value initializers");
11615 return Expression::make_error(location
);
11618 bool bad_key
= false;
11620 switch (name_expr
->classification())
11622 case EXPRESSION_UNKNOWN_REFERENCE
:
11623 name
= name_expr
->unknown_expression()->name();
11626 case EXPRESSION_CONST_REFERENCE
:
11627 name
= static_cast<Const_expression
*>(name_expr
)->name();
11630 case EXPRESSION_TYPE
:
11632 Type
* t
= name_expr
->type();
11633 Named_type
* nt
= t
->named_type();
11641 case EXPRESSION_VAR_REFERENCE
:
11642 name
= name_expr
->var_expression()->name();
11645 case EXPRESSION_FUNC_REFERENCE
:
11646 name
= name_expr
->func_expression()->name();
11649 case EXPRESSION_UNARY
:
11650 // If there is a local variable around with the same name as
11651 // the field, and this occurs in the closure, then the
11652 // parser may turn the field reference into an indirection
11653 // through the closure. FIXME: This is a mess.
11656 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11657 if (ue
->op() == OPERATOR_MULT
)
11659 Field_reference_expression
* fre
=
11660 ue
->operand()->field_reference_expression();
11664 fre
->expr()->type()->deref()->struct_type();
11667 const Struct_field
* sf
= st
->field(fre
->field_index());
11668 name
= sf
->field_name();
11670 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11671 size_t buflen
= strlen(buf
);
11672 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11675 name
= name
.substr(0, name
.length() - buflen
);
11690 error_at(name_expr
->location(), "expected struct field name");
11691 return Expression::make_error(location
);
11694 unsigned int index
;
11695 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11698 error_at(name_expr
->location(), "unknown field %qs in %qs",
11699 Gogo::message_name(name
).c_str(),
11700 (type
->named_type() != NULL
11701 ? type
->named_type()->message_name().c_str()
11702 : "unnamed struct"));
11703 return Expression::make_error(location
);
11705 if (vals
[index
] != NULL
)
11707 error_at(name_expr
->location(),
11708 "duplicate value for field %qs in %qs",
11709 Gogo::message_name(name
).c_str(),
11710 (type
->named_type() != NULL
11711 ? type
->named_type()->message_name().c_str()
11712 : "unnamed struct"));
11713 return Expression::make_error(location
);
11719 Expression_list
* list
= new Expression_list
;
11720 list
->reserve(field_count
);
11721 for (size_t i
= 0; i
< field_count
; ++i
)
11722 list
->push_back(vals
[i
]);
11724 return new Struct_construction_expression(type
, list
, location
);
11727 // Lower an array composite literal.
11730 Composite_literal_expression::lower_array(Type
* type
)
11732 source_location location
= this->location();
11733 if (this->vals_
== NULL
|| !this->has_keys_
)
11734 return this->make_array(type
, this->vals_
);
11736 std::vector
<Expression
*> vals
;
11737 vals
.reserve(this->vals_
->size());
11738 unsigned long index
= 0;
11739 Expression_list::const_iterator p
= this->vals_
->begin();
11740 while (p
!= this->vals_
->end())
11742 Expression
* index_expr
= *p
;
11745 gcc_assert(p
!= this->vals_
->end());
11746 Expression
* val
= *p
;
11750 if (index_expr
!= NULL
)
11755 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11758 error_at(index_expr
->location(),
11759 "index expression is not integer constant");
11760 return Expression::make_error(location
);
11762 if (mpz_sgn(ival
) < 0)
11765 error_at(index_expr
->location(), "index expression is negative");
11766 return Expression::make_error(location
);
11768 index
= mpz_get_ui(ival
);
11769 if (mpz_cmp_ui(ival
, index
) != 0)
11772 error_at(index_expr
->location(), "index value overflow");
11773 return Expression::make_error(location
);
11778 if (index
== vals
.size())
11779 vals
.push_back(val
);
11782 if (index
> vals
.size())
11784 vals
.reserve(index
+ 32);
11785 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11787 if (vals
[index
] != NULL
)
11789 error_at((index_expr
!= NULL
11790 ? index_expr
->location()
11791 : val
->location()),
11792 "duplicate value for index %lu",
11794 return Expression::make_error(location
);
11802 size_t size
= vals
.size();
11803 Expression_list
* list
= new Expression_list
;
11804 list
->reserve(size
);
11805 for (size_t i
= 0; i
< size
; ++i
)
11806 list
->push_back(vals
[i
]);
11808 return this->make_array(type
, list
);
11811 // Actually build the array composite literal. This handles
11815 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11817 source_location location
= this->location();
11818 Array_type
* at
= type
->array_type();
11819 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11821 size_t size
= vals
== NULL
? 0 : vals
->size();
11823 mpz_init_set_ui(vlen
, size
);
11824 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11826 at
= Type::make_array_type(at
->element_type(), elen
);
11829 if (at
->length() != NULL
)
11830 return new Fixed_array_construction_expression(type
, vals
, location
);
11832 return new Open_array_construction_expression(type
, vals
, location
);
11835 // Lower a map composite literal.
11838 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
11841 source_location location
= this->location();
11842 if (this->vals_
!= NULL
)
11844 if (!this->has_keys_
)
11846 error_at(location
, "map composite literal must have keys");
11847 return Expression::make_error(location
);
11850 for (Expression_list::iterator p
= this->vals_
->begin();
11851 p
!= this->vals_
->end();
11857 error_at((*p
)->location(),
11858 "map composite literal must have keys for every value");
11859 return Expression::make_error(location
);
11861 // Make sure we have lowered the key; it may not have been
11862 // lowered in order to handle keys for struct composite
11863 // literals. Lower it now to get the right error message.
11864 if ((*p
)->unknown_expression() != NULL
)
11866 (*p
)->unknown_expression()->clear_is_composite_literal_key();
11867 gogo
->lower_expression(function
, &*p
);
11868 gcc_assert((*p
)->is_error_expression());
11869 return Expression::make_error(location
);
11874 return new Map_construction_expression(type
, this->vals_
, location
);
11877 // Make a composite literal expression.
11880 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11881 Expression_list
* vals
,
11882 source_location location
)
11884 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11888 // Return whether this expression is a composite literal.
11891 Expression::is_composite_literal() const
11893 switch (this->classification_
)
11895 case EXPRESSION_COMPOSITE_LITERAL
:
11896 case EXPRESSION_STRUCT_CONSTRUCTION
:
11897 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11898 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11899 case EXPRESSION_MAP_CONSTRUCTION
:
11906 // Return whether this expression is a composite literal which is not
11910 Expression::is_nonconstant_composite_literal() const
11912 switch (this->classification_
)
11914 case EXPRESSION_STRUCT_CONSTRUCTION
:
11916 const Struct_construction_expression
*psce
=
11917 static_cast<const Struct_construction_expression
*>(this);
11918 return !psce
->is_constant_struct();
11920 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11922 const Fixed_array_construction_expression
*pace
=
11923 static_cast<const Fixed_array_construction_expression
*>(this);
11924 return !pace
->is_constant_array();
11926 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11928 const Open_array_construction_expression
*pace
=
11929 static_cast<const Open_array_construction_expression
*>(this);
11930 return !pace
->is_constant_array();
11932 case EXPRESSION_MAP_CONSTRUCTION
:
11939 // Return true if this is a reference to a local variable.
11942 Expression::is_local_variable() const
11944 const Var_expression
* ve
= this->var_expression();
11947 const Named_object
* no
= ve
->named_object();
11948 return (no
->is_result_variable()
11949 || (no
->is_variable() && !no
->var_value()->is_global()));
11952 // Class Type_guard_expression.
11957 Type_guard_expression::do_traverse(Traverse
* traverse
)
11959 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
11960 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11961 return TRAVERSE_EXIT
;
11962 return TRAVERSE_CONTINUE
;
11965 // Check types of a type guard expression. The expression must have
11966 // an interface type, but the actual type conversion is checked at run
11970 Type_guard_expression::do_check_types(Gogo
*)
11972 // 6g permits using a type guard with unsafe.pointer; we are
11974 Type
* expr_type
= this->expr_
->type();
11975 if (expr_type
->is_unsafe_pointer_type())
11977 if (this->type_
->points_to() == NULL
11978 && (this->type_
->integer_type() == NULL
11979 || (this->type_
->forwarded()
11980 != Type::lookup_integer_type("uintptr"))))
11981 this->report_error(_("invalid unsafe.Pointer conversion"));
11983 else if (this->type_
->is_unsafe_pointer_type())
11985 if (expr_type
->points_to() == NULL
11986 && (expr_type
->integer_type() == NULL
11987 || (expr_type
->forwarded()
11988 != Type::lookup_integer_type("uintptr"))))
11989 this->report_error(_("invalid unsafe.Pointer conversion"));
11991 else if (expr_type
->interface_type() == NULL
)
11993 if (!expr_type
->is_error_type() && !this->type_
->is_error_type())
11994 this->report_error(_("type assertion only valid for interface types"));
11995 this->set_is_error();
11997 else if (this->type_
->interface_type() == NULL
)
11999 std::string reason
;
12000 if (!expr_type
->interface_type()->implements_interface(this->type_
,
12003 if (!this->type_
->is_error_type())
12005 if (reason
.empty())
12006 this->report_error(_("impossible type assertion: "
12007 "type does not implement interface"));
12009 error_at(this->location(),
12010 ("impossible type assertion: "
12011 "type does not implement interface (%s)"),
12014 this->set_is_error();
12019 // Return a tree for a type guard expression.
12022 Type_guard_expression::do_get_tree(Translate_context
* context
)
12024 Gogo
* gogo
= context
->gogo();
12025 tree expr_tree
= this->expr_
->get_tree(context
);
12026 if (expr_tree
== error_mark_node
)
12027 return error_mark_node
;
12028 Type
* expr_type
= this->expr_
->type();
12029 if ((this->type_
->is_unsafe_pointer_type()
12030 && (expr_type
->points_to() != NULL
12031 || expr_type
->integer_type() != NULL
))
12032 || (expr_type
->is_unsafe_pointer_type()
12033 && this->type_
->points_to() != NULL
))
12034 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
12035 else if (expr_type
->is_unsafe_pointer_type()
12036 && this->type_
->integer_type() != NULL
)
12037 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
12038 else if (this->type_
->interface_type() != NULL
)
12039 return Expression::convert_interface_to_interface(context
, this->type_
,
12040 this->expr_
->type(),
12044 return Expression::convert_for_assignment(context
, this->type_
,
12045 this->expr_
->type(), expr_tree
,
12049 // Make a type guard expression.
12052 Expression::make_type_guard(Expression
* expr
, Type
* type
,
12053 source_location location
)
12055 return new Type_guard_expression(expr
, type
, location
);
12058 // Class Heap_composite_expression.
12060 // When you take the address of a composite literal, it is allocated
12061 // on the heap. This class implements that.
12063 class Heap_composite_expression
: public Expression
12066 Heap_composite_expression(Expression
* expr
, source_location location
)
12067 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
12073 do_traverse(Traverse
* traverse
)
12074 { return Expression::traverse(&this->expr_
, traverse
); }
12078 { return Type::make_pointer_type(this->expr_
->type()); }
12081 do_determine_type(const Type_context
*)
12082 { this->expr_
->determine_type_no_context(); }
12087 return Expression::make_heap_composite(this->expr_
->copy(),
12092 do_get_tree(Translate_context
*);
12094 // We only export global objects, and the parser does not generate
12095 // this in global scope.
12097 do_export(Export
*) const
12098 { gcc_unreachable(); }
12101 // The composite literal which is being put on the heap.
12105 // Return a tree which allocates a composite literal on the heap.
12108 Heap_composite_expression::do_get_tree(Translate_context
* context
)
12110 tree expr_tree
= this->expr_
->get_tree(context
);
12111 if (expr_tree
== error_mark_node
)
12112 return error_mark_node
;
12113 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
12114 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
12115 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
12116 expr_size
, this->location());
12117 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
12118 space
= save_expr(space
);
12119 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
12120 TREE_THIS_NOTRAP(ref
) = 1;
12121 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
12122 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
12124 SET_EXPR_LOCATION(ret
, this->location());
12128 // Allocate a composite literal on the heap.
12131 Expression::make_heap_composite(Expression
* expr
, source_location location
)
12133 return new Heap_composite_expression(expr
, location
);
12136 // Class Receive_expression.
12138 // Return the type of a receive expression.
12141 Receive_expression::do_type()
12143 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12144 if (channel_type
== NULL
)
12145 return Type::make_error_type();
12146 return channel_type
->element_type();
12149 // Check types for a receive expression.
12152 Receive_expression::do_check_types(Gogo
*)
12154 Type
* type
= this->channel_
->type();
12155 if (type
->is_error_type())
12157 this->set_is_error();
12160 if (type
->channel_type() == NULL
)
12162 this->report_error(_("expected channel"));
12165 if (!type
->channel_type()->may_receive())
12167 this->report_error(_("invalid receive on send-only channel"));
12172 // Get a tree for a receive expression.
12175 Receive_expression::do_get_tree(Translate_context
* context
)
12177 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12178 gcc_assert(channel_type
!= NULL
);
12179 Type
* element_type
= channel_type
->element_type();
12180 tree element_type_tree
= element_type
->get_tree(context
->gogo());
12182 tree channel
= this->channel_
->get_tree(context
);
12183 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
12184 return error_mark_node
;
12186 return Gogo::receive_from_channel(element_type_tree
, channel
,
12187 this->for_select_
, this->location());
12190 // Make a receive expression.
12192 Receive_expression
*
12193 Expression::make_receive(Expression
* channel
, source_location location
)
12195 return new Receive_expression(channel
, location
);
12198 // Class Send_expression.
12203 Send_expression::do_traverse(Traverse
* traverse
)
12205 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
12206 return TRAVERSE_EXIT
;
12207 return Expression::traverse(&this->val_
, traverse
);
12213 Send_expression::do_type()
12215 return Type::lookup_bool_type();
12221 Send_expression::do_determine_type(const Type_context
*)
12223 this->channel_
->determine_type_no_context();
12225 Type
* type
= this->channel_
->type();
12226 Type_context subcontext
;
12227 if (type
->channel_type() != NULL
)
12228 subcontext
.type
= type
->channel_type()->element_type();
12229 this->val_
->determine_type(&subcontext
);
12235 Send_expression::do_check_types(Gogo
*)
12237 Type
* type
= this->channel_
->type();
12238 if (type
->is_error_type())
12240 this->set_is_error();
12243 Channel_type
* channel_type
= type
->channel_type();
12244 if (channel_type
== NULL
)
12246 error_at(this->location(), "left operand of %<<-%> must be channel");
12247 this->set_is_error();
12250 Type
* element_type
= channel_type
->element_type();
12251 if (element_type
!= NULL
12252 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
12254 this->report_error(_("incompatible types in send"));
12257 if (!channel_type
->may_send())
12259 this->report_error(_("invalid send on receive-only channel"));
12264 // Get a tree for a send expression.
12267 Send_expression::do_get_tree(Translate_context
* context
)
12269 tree channel
= this->channel_
->get_tree(context
);
12270 tree val
= this->val_
->get_tree(context
);
12271 if (channel
== error_mark_node
|| val
== error_mark_node
)
12272 return error_mark_node
;
12273 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12274 val
= Expression::convert_for_assignment(context
,
12275 channel_type
->element_type(),
12276 this->val_
->type(),
12279 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
12280 this->for_select_
, this->location());
12283 // Make a send expression
12286 Expression::make_send(Expression
* channel
, Expression
* val
,
12287 source_location location
)
12289 return new Send_expression(channel
, val
, location
);
12292 // An expression which evaluates to a pointer to the type descriptor
12295 class Type_descriptor_expression
: public Expression
12298 Type_descriptor_expression(Type
* type
, source_location location
)
12299 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12306 { return Type::make_type_descriptor_ptr_type(); }
12309 do_determine_type(const Type_context
*)
12317 do_get_tree(Translate_context
* context
)
12318 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12321 // The type for which this is the descriptor.
12325 // Make a type descriptor expression.
12328 Expression::make_type_descriptor(Type
* type
, source_location location
)
12330 return new Type_descriptor_expression(type
, location
);
12333 // An expression which evaluates to some characteristic of a type.
12334 // This is only used to initialize fields of a type descriptor. Using
12335 // a new expression class is slightly inefficient but gives us a good
12336 // separation between the frontend and the middle-end with regard to
12337 // how types are laid out.
12339 class Type_info_expression
: public Expression
12342 Type_info_expression(Type
* type
, Type_info type_info
)
12343 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12344 type_(type
), type_info_(type_info
)
12352 do_determine_type(const Type_context
*)
12360 do_get_tree(Translate_context
* context
);
12363 // The type for which we are getting information.
12365 // What information we want.
12366 Type_info type_info_
;
12369 // The type is chosen to match what the type descriptor struct
12373 Type_info_expression::do_type()
12375 switch (this->type_info_
)
12377 case TYPE_INFO_SIZE
:
12378 return Type::lookup_integer_type("uintptr");
12379 case TYPE_INFO_ALIGNMENT
:
12380 case TYPE_INFO_FIELD_ALIGNMENT
:
12381 return Type::lookup_integer_type("uint8");
12387 // Return type information in GENERIC.
12390 Type_info_expression::do_get_tree(Translate_context
* context
)
12392 tree type_tree
= this->type_
->get_tree(context
->gogo());
12393 if (type_tree
== error_mark_node
)
12394 return error_mark_node
;
12396 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12397 gcc_assert(val_type_tree
!= error_mark_node
);
12399 if (this->type_info_
== TYPE_INFO_SIZE
)
12400 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12401 TYPE_SIZE_UNIT(type_tree
));
12405 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12406 val
= go_type_alignment(type_tree
);
12408 val
= go_field_alignment(type_tree
);
12409 return build_int_cstu(val_type_tree
, val
);
12413 // Make a type info expression.
12416 Expression::make_type_info(Type
* type
, Type_info type_info
)
12418 return new Type_info_expression(type
, type_info
);
12421 // An expression which evaluates to the offset of a field within a
12422 // struct. This, like Type_info_expression, q.v., is only used to
12423 // initialize fields of a type descriptor.
12425 class Struct_field_offset_expression
: public Expression
12428 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12429 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12430 type_(type
), field_(field
)
12436 { return Type::lookup_integer_type("uintptr"); }
12439 do_determine_type(const Type_context
*)
12447 do_get_tree(Translate_context
* context
);
12450 // The type of the struct.
12451 Struct_type
* type_
;
12453 const Struct_field
* field_
;
12456 // Return a struct field offset in GENERIC.
12459 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12461 tree type_tree
= this->type_
->get_tree(context
->gogo());
12462 if (type_tree
== error_mark_node
)
12463 return error_mark_node
;
12465 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12466 gcc_assert(val_type_tree
!= error_mark_node
);
12468 const Struct_field_list
* fields
= this->type_
->fields();
12469 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12470 Struct_field_list::const_iterator p
;
12471 for (p
= fields
->begin();
12472 p
!= fields
->end();
12473 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12475 gcc_assert(struct_field_tree
!= NULL_TREE
);
12476 if (&*p
== this->field_
)
12479 gcc_assert(&*p
== this->field_
);
12481 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12482 byte_position(struct_field_tree
));
12485 // Make an expression for a struct field offset.
12488 Expression::make_struct_field_offset(Struct_type
* type
,
12489 const Struct_field
* field
)
12491 return new Struct_field_offset_expression(type
, field
);
12494 // An expression which evaluates to the address of an unnamed label.
12496 class Label_addr_expression
: public Expression
12499 Label_addr_expression(Label
* label
, source_location location
)
12500 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12507 { return Type::make_pointer_type(Type::make_void_type()); }
12510 do_determine_type(const Type_context
*)
12515 { return new Label_addr_expression(this->label_
, this->location()); }
12518 do_get_tree(Translate_context
*)
12519 { return this->label_
->get_addr(this->location()); }
12522 // The label whose address we are taking.
12526 // Make an expression for the address of an unnamed label.
12529 Expression::make_label_addr(Label
* label
, source_location location
)
12531 return new Label_addr_expression(label
, location
);
12534 // Import an expression. This comes at the end in order to see the
12535 // various class definitions.
12538 Expression::import_expression(Import
* imp
)
12540 int c
= imp
->peek_char();
12541 if (imp
->match_c_string("- ")
12542 || imp
->match_c_string("! ")
12543 || imp
->match_c_string("^ "))
12544 return Unary_expression::do_import(imp
);
12546 return Binary_expression::do_import(imp
);
12547 else if (imp
->match_c_string("true")
12548 || imp
->match_c_string("false"))
12549 return Boolean_expression::do_import(imp
);
12551 return String_expression::do_import(imp
);
12552 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12554 // This handles integers, floats and complex constants.
12555 return Integer_expression::do_import(imp
);
12557 else if (imp
->match_c_string("nil"))
12558 return Nil_expression::do_import(imp
);
12559 else if (imp
->match_c_string("convert"))
12560 return Type_conversion_expression::do_import(imp
);
12563 error_at(imp
->location(), "import error: expected expression");
12564 return Expression::make_error(imp
->location());
12568 // Class Expression_list.
12570 // Traverse the list.
12573 Expression_list::traverse(Traverse
* traverse
)
12575 for (Expression_list::iterator p
= this->begin();
12581 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12582 return TRAVERSE_EXIT
;
12585 return TRAVERSE_CONTINUE
;
12591 Expression_list::copy()
12593 Expression_list
* ret
= new Expression_list();
12594 for (Expression_list::iterator p
= this->begin();
12599 ret
->push_back(NULL
);
12601 ret
->push_back((*p
)->copy());
12606 // Return whether an expression list has an error expression.
12609 Expression_list::contains_error() const
12611 for (Expression_list::const_iterator p
= this->begin();
12614 if (*p
!= NULL
&& (*p
)->is_error_expression())