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 Var_expression.
899 // Lower a variable expression. Here we just make sure that the
900 // initialization expression of the variable has been lowered. This
901 // ensures that we will be able to determine the type of the variable
905 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
907 if (this->variable_
->is_variable())
909 Variable
* var
= this->variable_
->var_value();
910 // This is either a local variable or a global variable. A
911 // reference to a variable which is local to an enclosing
912 // function will be a reference to a field in a closure.
913 if (var
->is_global())
915 var
->lower_init_expression(gogo
, function
);
920 // Return the name of the variable.
923 Var_expression::name() const
925 return this->variable_
->name();
928 // Return the type of a reference to a variable.
931 Var_expression::do_type()
933 if (this->variable_
->is_variable())
934 return this->variable_
->var_value()->type();
935 else if (this->variable_
->is_result_variable())
936 return this->variable_
->result_var_value()->type();
941 // Something takes the address of this variable. This means that we
942 // may want to move the variable onto the heap.
945 Var_expression::do_address_taken(bool escapes
)
949 else if (this->variable_
->is_variable())
950 this->variable_
->var_value()->set_address_taken();
951 else if (this->variable_
->is_result_variable())
952 this->variable_
->result_var_value()->set_address_taken();
957 // Get the tree for a reference to a variable.
960 Var_expression::do_get_tree(Translate_context
* context
)
962 return this->variable_
->get_tree(context
->gogo(), context
->function());
965 // Make a reference to a variable in an expression.
968 Expression::make_var_reference(Named_object
* var
, source_location location
)
971 return Expression::make_sink(location
);
973 // FIXME: Creating a new object for each reference to a variable is
975 return new Var_expression(var
, location
);
978 // Class Temporary_reference_expression.
983 Temporary_reference_expression::do_type()
985 return this->statement_
->type();
988 // Called if something takes the address of this temporary variable.
989 // We never have to move temporary variables to the heap, but we do
990 // need to know that they must live in the stack rather than in a
994 Temporary_reference_expression::do_address_taken(bool)
996 this->statement_
->set_is_address_taken();
999 // Get a tree referring to the variable.
1002 Temporary_reference_expression::do_get_tree(Translate_context
*)
1004 return this->statement_
->get_decl();
1007 // Make a reference to a temporary variable.
1010 Expression::make_temporary_reference(Temporary_statement
* statement
,
1011 source_location location
)
1013 return new Temporary_reference_expression(statement
, location
);
1016 // A sink expression--a use of the blank identifier _.
1018 class Sink_expression
: public Expression
1021 Sink_expression(source_location location
)
1022 : Expression(EXPRESSION_SINK
, location
),
1023 type_(NULL
), var_(NULL_TREE
)
1028 do_discarding_value()
1035 do_determine_type(const Type_context
*);
1039 { return new Sink_expression(this->location()); }
1042 do_get_tree(Translate_context
*);
1045 // The type of this sink variable.
1047 // The temporary variable we generate.
1051 // Return the type of a sink expression.
1054 Sink_expression::do_type()
1056 if (this->type_
== NULL
)
1057 return Type::make_sink_type();
1061 // Determine the type of a sink expression.
1064 Sink_expression::do_determine_type(const Type_context
* context
)
1066 if (context
->type
!= NULL
)
1067 this->type_
= context
->type
;
1070 // Return a temporary variable for a sink expression. This will
1071 // presumably be a write-only variable which the middle-end will drop.
1074 Sink_expression::do_get_tree(Translate_context
* context
)
1076 if (this->var_
== NULL_TREE
)
1078 gcc_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1079 this->var_
= create_tmp_var(this->type_
->get_tree(context
->gogo()),
1085 // Make a sink expression.
1088 Expression::make_sink(source_location location
)
1090 return new Sink_expression(location
);
1093 // Class Func_expression.
1095 // FIXME: Can a function expression appear in a constant expression?
1096 // The value is unchanging. Initializing a constant to the address of
1097 // a function seems like it could work, though there might be little
1100 // Return the name of the function.
1103 Func_expression::name() const
1105 return this->function_
->name();
1111 Func_expression::do_traverse(Traverse
* traverse
)
1113 return (this->closure_
== NULL
1115 : Expression::traverse(&this->closure_
, traverse
));
1118 // Return the type of a function expression.
1121 Func_expression::do_type()
1123 if (this->function_
->is_function())
1124 return this->function_
->func_value()->type();
1125 else if (this->function_
->is_function_declaration())
1126 return this->function_
->func_declaration_value()->type();
1131 // Get the tree for a function expression without evaluating the
1135 Func_expression::get_tree_without_closure(Gogo
* gogo
)
1137 Function_type
* fntype
;
1138 if (this->function_
->is_function())
1139 fntype
= this->function_
->func_value()->type();
1140 else if (this->function_
->is_function_declaration())
1141 fntype
= this->function_
->func_declaration_value()->type();
1145 // Builtin functions are handled specially by Call_expression. We
1146 // can't take their address.
1147 if (fntype
->is_builtin())
1149 error_at(this->location(), "invalid use of special builtin function %qs",
1150 this->function_
->name().c_str());
1151 return error_mark_node
;
1154 Named_object
* no
= this->function_
;
1156 tree id
= no
->get_id(gogo
);
1157 if (id
== error_mark_node
)
1158 return error_mark_node
;
1161 if (no
->is_function())
1162 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
, id
);
1163 else if (no
->is_function_declaration())
1164 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
, id
);
1168 if (fndecl
== error_mark_node
)
1169 return error_mark_node
;
1171 return build_fold_addr_expr_loc(this->location(), fndecl
);
1174 // Get the tree for a function expression. This is used when we take
1175 // the address of a function rather than simply calling it. If the
1176 // function has a closure, we must use a trampoline.
1179 Func_expression::do_get_tree(Translate_context
* context
)
1181 Gogo
* gogo
= context
->gogo();
1183 tree fnaddr
= this->get_tree_without_closure(gogo
);
1184 if (fnaddr
== error_mark_node
)
1185 return error_mark_node
;
1187 gcc_assert(TREE_CODE(fnaddr
) == ADDR_EXPR
1188 && TREE_CODE(TREE_OPERAND(fnaddr
, 0)) == FUNCTION_DECL
);
1189 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr
, 0)) = 1;
1191 // For a normal non-nested function call, that is all we have to do.
1192 if (!this->function_
->is_function()
1193 || this->function_
->func_value()->enclosing() == NULL
)
1195 gcc_assert(this->closure_
== NULL
);
1199 // For a nested function call, we have to always allocate a
1200 // trampoline. If we don't always allocate, then closures will not
1201 // be reliably distinct.
1202 Expression
* closure
= this->closure_
;
1204 if (closure
== NULL
)
1205 closure_tree
= null_pointer_node
;
1208 // Get the value of the closure. This will be a pointer to
1209 // space allocated on the heap.
1210 closure_tree
= closure
->get_tree(context
);
1211 if (closure_tree
== error_mark_node
)
1212 return error_mark_node
;
1213 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree
)));
1216 // Now we need to build some code on the heap. This code will load
1217 // the static chain pointer with the closure and then jump to the
1218 // body of the function. The normal gcc approach is to build the
1219 // code on the stack. Unfortunately we can not do that, as Go
1220 // permits us to return the function pointer.
1222 return gogo
->make_trampoline(fnaddr
, closure_tree
, this->location());
1225 // Make a reference to a function in an expression.
1228 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1229 source_location location
)
1231 return new Func_expression(function
, closure
, location
);
1234 // Class Unknown_expression.
1236 // Return the name of an unknown expression.
1239 Unknown_expression::name() const
1241 return this->named_object_
->name();
1244 // Lower a reference to an unknown name.
1247 Unknown_expression::do_lower(Gogo
*, Named_object
*, int)
1249 source_location location
= this->location();
1250 Named_object
* no
= this->named_object_
;
1251 Named_object
* real
= no
->unknown_value()->real_named_object();
1254 if (this->is_composite_literal_key_
)
1256 error_at(location
, "reference to undefined name %qs",
1257 this->named_object_
->message_name().c_str());
1258 return Expression::make_error(location
);
1260 switch (real
->classification())
1262 case Named_object::NAMED_OBJECT_CONST
:
1263 return Expression::make_const_reference(real
, location
);
1264 case Named_object::NAMED_OBJECT_TYPE
:
1265 return Expression::make_type(real
->type_value(), location
);
1266 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1267 if (this->is_composite_literal_key_
)
1269 error_at(location
, "reference to undefined type %qs",
1270 real
->message_name().c_str());
1271 return Expression::make_error(location
);
1272 case Named_object::NAMED_OBJECT_VAR
:
1273 return Expression::make_var_reference(real
, location
);
1274 case Named_object::NAMED_OBJECT_FUNC
:
1275 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1276 return Expression::make_func_reference(real
, NULL
, location
);
1277 case Named_object::NAMED_OBJECT_PACKAGE
:
1278 if (this->is_composite_literal_key_
)
1280 error_at(location
, "unexpected reference to package");
1281 return Expression::make_error(location
);
1287 // Make a reference to an unknown name.
1290 Expression::make_unknown_reference(Named_object
* no
, source_location location
)
1292 gcc_assert(no
->resolve()->is_unknown());
1293 return new Unknown_expression(no
, location
);
1296 // A boolean expression.
1298 class Boolean_expression
: public Expression
1301 Boolean_expression(bool val
, source_location location
)
1302 : Expression(EXPRESSION_BOOLEAN
, location
),
1303 val_(val
), type_(NULL
)
1311 do_is_constant() const
1318 do_determine_type(const Type_context
*);
1325 do_get_tree(Translate_context
*)
1326 { return this->val_
? boolean_true_node
: boolean_false_node
; }
1329 do_export(Export
* exp
) const
1330 { exp
->write_c_string(this->val_
? "true" : "false"); }
1335 // The type as determined by context.
1342 Boolean_expression::do_type()
1344 if (this->type_
== NULL
)
1345 this->type_
= Type::make_boolean_type();
1349 // Set the type from the context.
1352 Boolean_expression::do_determine_type(const Type_context
* context
)
1354 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1356 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1357 this->type_
= context
->type
;
1358 else if (!context
->may_be_abstract
)
1359 this->type_
= Type::lookup_bool_type();
1362 // Import a boolean constant.
1365 Boolean_expression::do_import(Import
* imp
)
1367 if (imp
->peek_char() == 't')
1369 imp
->require_c_string("true");
1370 return Expression::make_boolean(true, imp
->location());
1374 imp
->require_c_string("false");
1375 return Expression::make_boolean(false, imp
->location());
1379 // Make a boolean expression.
1382 Expression::make_boolean(bool val
, source_location location
)
1384 return new Boolean_expression(val
, location
);
1387 // Class String_expression.
1392 String_expression::do_type()
1394 if (this->type_
== NULL
)
1395 this->type_
= Type::make_string_type();
1399 // Set the type from the context.
1402 String_expression::do_determine_type(const Type_context
* context
)
1404 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1406 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1407 this->type_
= context
->type
;
1408 else if (!context
->may_be_abstract
)
1409 this->type_
= Type::lookup_string_type();
1412 // Build a string constant.
1415 String_expression::do_get_tree(Translate_context
* context
)
1417 return context
->gogo()->go_string_constant_tree(this->val_
);
1420 // Export a string expression.
1423 String_expression::do_export(Export
* exp
) const
1426 s
.reserve(this->val_
.length() * 4 + 2);
1428 for (std::string::const_iterator p
= this->val_
.begin();
1429 p
!= this->val_
.end();
1432 if (*p
== '\\' || *p
== '"')
1437 else if (*p
>= 0x20 && *p
< 0x7f)
1439 else if (*p
== '\n')
1441 else if (*p
== '\t')
1446 unsigned char c
= *p
;
1447 unsigned int dig
= c
>> 4;
1448 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1450 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1454 exp
->write_string(s
);
1457 // Import a string expression.
1460 String_expression::do_import(Import
* imp
)
1462 imp
->require_c_string("\"");
1466 int c
= imp
->get_char();
1467 if (c
== '"' || c
== -1)
1470 val
+= static_cast<char>(c
);
1473 c
= imp
->get_char();
1474 if (c
== '\\' || c
== '"')
1475 val
+= static_cast<char>(c
);
1482 c
= imp
->get_char();
1483 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1484 c
= imp
->get_char();
1485 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1486 char v
= (vh
<< 4) | vl
;
1491 error_at(imp
->location(), "bad string constant");
1492 return Expression::make_error(imp
->location());
1496 return Expression::make_string(val
, imp
->location());
1499 // Make a string expression.
1502 Expression::make_string(const std::string
& val
, source_location location
)
1504 return new String_expression(val
, location
);
1507 // Make an integer expression.
1509 class Integer_expression
: public Expression
1512 Integer_expression(const mpz_t
* val
, Type
* type
, source_location location
)
1513 : Expression(EXPRESSION_INTEGER
, location
),
1515 { mpz_init_set(this->val_
, *val
); }
1520 // Return whether VAL fits in the type.
1522 check_constant(mpz_t val
, Type
*, source_location
);
1524 // Write VAL to export data.
1526 export_integer(Export
* exp
, const mpz_t val
);
1530 do_is_constant() const
1534 do_integer_constant_value(bool, mpz_t val
, Type
** ptype
) const;
1540 do_determine_type(const Type_context
* context
);
1543 do_check_types(Gogo
*);
1546 do_get_tree(Translate_context
*);
1550 { return Expression::make_integer(&this->val_
, this->type_
,
1551 this->location()); }
1554 do_export(Export
*) const;
1557 // The integer value.
1563 // Return an integer constant value.
1566 Integer_expression::do_integer_constant_value(bool, mpz_t val
,
1569 if (this->type_
!= NULL
)
1570 *ptype
= this->type_
;
1571 mpz_set(val
, this->val_
);
1575 // Return the current type. If we haven't set the type yet, we return
1576 // an abstract integer type.
1579 Integer_expression::do_type()
1581 if (this->type_
== NULL
)
1582 this->type_
= Type::make_abstract_integer_type();
1586 // Set the type of the integer value. Here we may switch from an
1587 // abstract type to a real type.
1590 Integer_expression::do_determine_type(const Type_context
* context
)
1592 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1594 else if (context
->type
!= NULL
1595 && (context
->type
->integer_type() != NULL
1596 || context
->type
->float_type() != NULL
1597 || context
->type
->complex_type() != NULL
))
1598 this->type_
= context
->type
;
1599 else if (!context
->may_be_abstract
)
1600 this->type_
= Type::lookup_integer_type("int");
1603 // Return true if the integer VAL fits in the range of the type TYPE.
1604 // Otherwise give an error and return false. TYPE may be NULL.
1607 Integer_expression::check_constant(mpz_t val
, Type
* type
,
1608 source_location location
)
1612 Integer_type
* itype
= type
->integer_type();
1613 if (itype
== NULL
|| itype
->is_abstract())
1616 int bits
= mpz_sizeinbase(val
, 2);
1618 if (itype
->is_unsigned())
1620 // For an unsigned type we can only accept a nonnegative number,
1621 // and we must be able to represent at least BITS.
1622 if (mpz_sgn(val
) >= 0
1623 && bits
<= itype
->bits())
1628 // For a signed type we need an extra bit to indicate the sign.
1629 // We have to handle the most negative integer specially.
1630 if (bits
+ 1 <= itype
->bits()
1631 || (bits
<= itype
->bits()
1633 && (mpz_scan1(val
, 0)
1634 == static_cast<unsigned long>(itype
->bits() - 1))
1635 && mpz_scan0(val
, itype
->bits()) == ULONG_MAX
))
1639 error_at(location
, "integer constant overflow");
1643 // Check the type of an integer constant.
1646 Integer_expression::do_check_types(Gogo
*)
1648 if (this->type_
== NULL
)
1650 if (!Integer_expression::check_constant(this->val_
, this->type_
,
1652 this->set_is_error();
1655 // Get a tree for an integer constant.
1658 Integer_expression::do_get_tree(Translate_context
* context
)
1660 Gogo
* gogo
= context
->gogo();
1662 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1663 type
= this->type_
->get_tree(gogo
);
1664 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1666 // We are converting to an abstract floating point type.
1667 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
1669 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1671 // We are converting to an abstract complex type.
1672 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
1676 // If we still have an abstract type here, then this is being
1677 // used in a constant expression which didn't get reduced for
1678 // some reason. Use a type which will fit the value. We use <,
1679 // not <=, because we need an extra bit for the sign bit.
1680 int bits
= mpz_sizeinbase(this->val_
, 2);
1681 if (bits
< INT_TYPE_SIZE
)
1682 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
1684 type
= Type::lookup_integer_type("int64")->get_tree(gogo
);
1686 type
= long_long_integer_type_node
;
1688 return Expression::integer_constant_tree(this->val_
, type
);
1691 // Write VAL to export data.
1694 Integer_expression::export_integer(Export
* exp
, const mpz_t val
)
1696 char* s
= mpz_get_str(NULL
, 10, val
);
1697 exp
->write_c_string(s
);
1701 // Export an integer in a constant expression.
1704 Integer_expression::do_export(Export
* exp
) const
1706 Integer_expression::export_integer(exp
, this->val_
);
1707 // A trailing space lets us reliably identify the end of the number.
1708 exp
->write_c_string(" ");
1711 // Import an integer, floating point, or complex value. This handles
1712 // all these types because they all start with digits.
1715 Integer_expression::do_import(Import
* imp
)
1717 std::string num
= imp
->read_identifier();
1718 imp
->require_c_string(" ");
1719 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1722 size_t plus_pos
= num
.find('+', 1);
1723 size_t minus_pos
= num
.find('-', 1);
1725 if (plus_pos
== std::string::npos
)
1727 else if (minus_pos
== std::string::npos
)
1731 error_at(imp
->location(), "bad number in import data: %qs",
1733 return Expression::make_error(imp
->location());
1735 if (pos
== std::string::npos
)
1736 mpfr_set_ui(real
, 0, GMP_RNDN
);
1739 std::string real_str
= num
.substr(0, pos
);
1740 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1742 error_at(imp
->location(), "bad number in import data: %qs",
1744 return Expression::make_error(imp
->location());
1748 std::string imag_str
;
1749 if (pos
== std::string::npos
)
1752 imag_str
= num
.substr(pos
);
1753 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
1755 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
1757 error_at(imp
->location(), "bad number in import data: %qs",
1759 return Expression::make_error(imp
->location());
1761 Expression
* ret
= Expression::make_complex(&real
, &imag
, NULL
,
1767 else if (num
.find('.') == std::string::npos
1768 && num
.find('E') == std::string::npos
)
1771 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
1773 error_at(imp
->location(), "bad number in import data: %qs",
1775 return Expression::make_error(imp
->location());
1777 Expression
* ret
= Expression::make_integer(&val
, NULL
, imp
->location());
1784 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
1786 error_at(imp
->location(), "bad number in import data: %qs",
1788 return Expression::make_error(imp
->location());
1790 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
1796 // Build a new integer value.
1799 Expression::make_integer(const mpz_t
* val
, Type
* type
,
1800 source_location location
)
1802 return new Integer_expression(val
, type
, location
);
1807 class Float_expression
: public Expression
1810 Float_expression(const mpfr_t
* val
, Type
* type
, source_location location
)
1811 : Expression(EXPRESSION_FLOAT
, location
),
1814 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
1817 // Constrain VAL to fit into TYPE.
1819 constrain_float(mpfr_t val
, Type
* type
);
1821 // Return whether VAL fits in the type.
1823 check_constant(mpfr_t val
, Type
*, source_location
);
1825 // Write VAL to export data.
1827 export_float(Export
* exp
, const mpfr_t val
);
1831 do_is_constant() const
1835 do_float_constant_value(mpfr_t val
, Type
**) const;
1841 do_determine_type(const Type_context
*);
1844 do_check_types(Gogo
*);
1848 { return Expression::make_float(&this->val_
, this->type_
,
1849 this->location()); }
1852 do_get_tree(Translate_context
*);
1855 do_export(Export
*) const;
1858 // The floating point value.
1864 // Constrain VAL to fit into TYPE.
1867 Float_expression::constrain_float(mpfr_t val
, Type
* type
)
1869 Float_type
* ftype
= type
->float_type();
1870 if (ftype
!= NULL
&& !ftype
->is_abstract())
1872 tree type_tree
= ftype
->type_tree();
1873 REAL_VALUE_TYPE rvt
;
1874 real_from_mpfr(&rvt
, val
, type_tree
, GMP_RNDN
);
1875 real_convert(&rvt
, TYPE_MODE(type_tree
), &rvt
);
1876 mpfr_from_real(val
, &rvt
, GMP_RNDN
);
1880 // Return a floating point constant value.
1883 Float_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
1885 if (this->type_
!= NULL
)
1886 *ptype
= this->type_
;
1887 mpfr_set(val
, this->val_
, GMP_RNDN
);
1891 // Return the current type. If we haven't set the type yet, we return
1892 // an abstract float type.
1895 Float_expression::do_type()
1897 if (this->type_
== NULL
)
1898 this->type_
= Type::make_abstract_float_type();
1902 // Set the type of the float value. Here we may switch from an
1903 // abstract type to a real type.
1906 Float_expression::do_determine_type(const Type_context
* context
)
1908 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1910 else if (context
->type
!= NULL
1911 && (context
->type
->integer_type() != NULL
1912 || context
->type
->float_type() != NULL
1913 || context
->type
->complex_type() != NULL
))
1914 this->type_
= context
->type
;
1915 else if (!context
->may_be_abstract
)
1916 this->type_
= Type::lookup_float_type("float");
1919 // Return true if the floating point value VAL fits in the range of
1920 // the type TYPE. Otherwise give an error and return false. TYPE may
1924 Float_expression::check_constant(mpfr_t val
, Type
* type
,
1925 source_location location
)
1929 Float_type
* ftype
= type
->float_type();
1930 if (ftype
== NULL
|| ftype
->is_abstract())
1933 // A NaN or Infinity always fits in the range of the type.
1934 if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
1937 mp_exp_t exp
= mpfr_get_exp(val
);
1939 switch (ftype
->bits())
1952 error_at(location
, "floating point constant overflow");
1958 // Check the type of a float value.
1961 Float_expression::do_check_types(Gogo
*)
1963 if (this->type_
== NULL
)
1966 if (!Float_expression::check_constant(this->val_
, this->type_
,
1968 this->set_is_error();
1970 Integer_type
* integer_type
= this->type_
->integer_type();
1971 if (integer_type
!= NULL
)
1973 if (!mpfr_integer_p(this->val_
))
1974 this->report_error(_("floating point constant truncated to integer"));
1977 gcc_assert(!integer_type
->is_abstract());
1980 mpfr_get_z(ival
, this->val_
, GMP_RNDN
);
1981 Integer_expression::check_constant(ival
, integer_type
,
1988 // Get a tree for a float constant.
1991 Float_expression::do_get_tree(Translate_context
* context
)
1993 Gogo
* gogo
= context
->gogo();
1995 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1996 type
= this->type_
->get_tree(gogo
);
1997 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
1999 // We have an abstract integer type. We just hope for the best.
2000 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
2004 // If we still have an abstract type here, then this is being
2005 // used in a constant expression which didn't get reduced. We
2006 // just use float64 and hope for the best.
2007 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
2009 return Expression::float_constant_tree(this->val_
, type
);
2012 // Write a floating point number to export data.
2015 Float_expression::export_float(Export
*exp
, const mpfr_t val
)
2018 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2020 exp
->write_c_string("-");
2021 exp
->write_c_string("0.");
2022 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2025 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2026 exp
->write_c_string(buf
);
2029 // Export a floating point number in a constant expression.
2032 Float_expression::do_export(Export
* exp
) const
2034 Float_expression::export_float(exp
, this->val_
);
2035 // A trailing space lets us reliably identify the end of the number.
2036 exp
->write_c_string(" ");
2039 // Make a float expression.
2042 Expression::make_float(const mpfr_t
* val
, Type
* type
, source_location location
)
2044 return new Float_expression(val
, type
, location
);
2049 class Complex_expression
: public Expression
2052 Complex_expression(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2053 source_location location
)
2054 : Expression(EXPRESSION_COMPLEX
, location
),
2057 mpfr_init_set(this->real_
, *real
, GMP_RNDN
);
2058 mpfr_init_set(this->imag_
, *imag
, GMP_RNDN
);
2061 // Constrain REAL/IMAG to fit into TYPE.
2063 constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
);
2065 // Return whether REAL/IMAG fits in the type.
2067 check_constant(mpfr_t real
, mpfr_t imag
, Type
*, source_location
);
2069 // Write REAL/IMAG to export data.
2071 export_complex(Export
* exp
, const mpfr_t real
, const mpfr_t val
);
2075 do_is_constant() const
2079 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2085 do_determine_type(const Type_context
*);
2088 do_check_types(Gogo
*);
2093 return Expression::make_complex(&this->real_
, &this->imag_
, this->type_
,
2098 do_get_tree(Translate_context
*);
2101 do_export(Export
*) const;
2106 // The imaginary part;
2108 // The type if known.
2112 // Constrain REAL/IMAG to fit into TYPE.
2115 Complex_expression::constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
)
2117 Complex_type
* ctype
= type
->complex_type();
2118 if (ctype
!= NULL
&& !ctype
->is_abstract())
2120 tree type_tree
= ctype
->type_tree();
2122 REAL_VALUE_TYPE rvt
;
2123 real_from_mpfr(&rvt
, real
, TREE_TYPE(type_tree
), GMP_RNDN
);
2124 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2125 mpfr_from_real(real
, &rvt
, GMP_RNDN
);
2127 real_from_mpfr(&rvt
, imag
, TREE_TYPE(type_tree
), GMP_RNDN
);
2128 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2129 mpfr_from_real(imag
, &rvt
, GMP_RNDN
);
2133 // Return a complex constant value.
2136 Complex_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2139 if (this->type_
!= NULL
)
2140 *ptype
= this->type_
;
2141 mpfr_set(real
, this->real_
, GMP_RNDN
);
2142 mpfr_set(imag
, this->imag_
, GMP_RNDN
);
2146 // Return the current type. If we haven't set the type yet, we return
2147 // an abstract complex type.
2150 Complex_expression::do_type()
2152 if (this->type_
== NULL
)
2153 this->type_
= Type::make_abstract_complex_type();
2157 // Set the type of the complex value. Here we may switch from an
2158 // abstract type to a real type.
2161 Complex_expression::do_determine_type(const Type_context
* context
)
2163 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2165 else if (context
->type
!= NULL
2166 && context
->type
->complex_type() != NULL
)
2167 this->type_
= context
->type
;
2168 else if (!context
->may_be_abstract
)
2169 this->type_
= Type::lookup_complex_type("complex");
2172 // Return true if the complex value REAL/IMAG fits in the range of the
2173 // type TYPE. Otherwise give an error and return false. TYPE may be
2177 Complex_expression::check_constant(mpfr_t real
, mpfr_t imag
, Type
* type
,
2178 source_location location
)
2182 Complex_type
* ctype
= type
->complex_type();
2183 if (ctype
== NULL
|| ctype
->is_abstract())
2187 switch (ctype
->bits())
2199 // A NaN or Infinity always fits in the range of the type.
2200 if (!mpfr_nan_p(real
) && !mpfr_inf_p(real
) && !mpfr_zero_p(real
))
2202 if (mpfr_get_exp(real
) > max_exp
)
2204 error_at(location
, "complex real part constant overflow");
2209 if (!mpfr_nan_p(imag
) && !mpfr_inf_p(imag
) && !mpfr_zero_p(imag
))
2211 if (mpfr_get_exp(imag
) > max_exp
)
2213 error_at(location
, "complex imaginary part constant overflow");
2221 // Check the type of a complex value.
2224 Complex_expression::do_check_types(Gogo
*)
2226 if (this->type_
== NULL
)
2229 if (!Complex_expression::check_constant(this->real_
, this->imag_
,
2230 this->type_
, this->location()))
2231 this->set_is_error();
2234 // Get a tree for a complex constant.
2237 Complex_expression::do_get_tree(Translate_context
* context
)
2239 Gogo
* gogo
= context
->gogo();
2241 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2242 type
= this->type_
->get_tree(gogo
);
2245 // If we still have an abstract type here, this this is being
2246 // used in a constant expression which didn't get reduced. We
2247 // just use complex128 and hope for the best.
2248 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
2250 return Expression::complex_constant_tree(this->real_
, this->imag_
, type
);
2253 // Write REAL/IMAG to export data.
2256 Complex_expression::export_complex(Export
* exp
, const mpfr_t real
,
2259 if (!mpfr_zero_p(real
))
2261 Float_expression::export_float(exp
, real
);
2262 if (mpfr_sgn(imag
) > 0)
2263 exp
->write_c_string("+");
2265 Float_expression::export_float(exp
, imag
);
2266 exp
->write_c_string("i");
2269 // Export a complex number in a constant expression.
2272 Complex_expression::do_export(Export
* exp
) const
2274 Complex_expression::export_complex(exp
, this->real_
, this->imag_
);
2275 // A trailing space lets us reliably identify the end of the number.
2276 exp
->write_c_string(" ");
2279 // Make a complex expression.
2282 Expression::make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2283 source_location location
)
2285 return new Complex_expression(real
, imag
, type
, location
);
2288 // Find a named object in an expression.
2290 class Find_named_object
: public Traverse
2293 Find_named_object(Named_object
* no
)
2294 : Traverse(traverse_expressions
),
2295 no_(no
), found_(false)
2298 // Whether we found the object.
2301 { return this->found_
; }
2305 expression(Expression
**);
2308 // The object we are looking for.
2310 // Whether we found it.
2314 // A reference to a const in an expression.
2316 class Const_expression
: public Expression
2319 Const_expression(Named_object
* constant
, source_location location
)
2320 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2321 constant_(constant
), type_(NULL
), seen_(false)
2326 { return this->constant_
; }
2330 { return this->constant_
->name(); }
2334 do_lower(Gogo
*, Named_object
*, int);
2337 do_is_constant() const
2341 do_integer_constant_value(bool, mpz_t val
, Type
**) const;
2344 do_float_constant_value(mpfr_t val
, Type
**) const;
2347 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2350 do_string_constant_value(std::string
* val
) const
2351 { return this->constant_
->const_value()->expr()->string_constant_value(val
); }
2356 // The type of a const is set by the declaration, not the use.
2358 do_determine_type(const Type_context
*);
2361 do_check_types(Gogo
*);
2368 do_get_tree(Translate_context
* context
);
2370 // When exporting a reference to a const as part of a const
2371 // expression, we export the value. We ignore the fact that it has
2374 do_export(Export
* exp
) const
2375 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2379 Named_object
* constant_
;
2380 // The type of this reference. This is used if the constant has an
2383 // Used to prevent infinite recursion when a constant incorrectly
2384 // refers to itself.
2388 // Lower a constant expression. This is where we convert the
2389 // predeclared constant iota into an integer value.
2392 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2394 if (this->constant_
->const_value()->expr()->classification()
2397 if (iota_value
== -1)
2399 error_at(this->location(),
2400 "iota is only defined in const declarations");
2404 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2405 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2411 // Make sure that the constant itself has been lowered.
2412 gogo
->lower_constant(this->constant_
);
2417 // Return an integer constant value.
2420 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2427 if (this->type_
!= NULL
)
2428 ctype
= this->type_
;
2430 ctype
= this->constant_
->const_value()->type();
2431 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2434 Expression
* e
= this->constant_
->const_value()->expr();
2439 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2441 this->seen_
= false;
2445 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2448 *ptype
= ctype
!= NULL
? ctype
: t
;
2452 // Return a floating point constant value.
2455 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2461 if (this->type_
!= NULL
)
2462 ctype
= this->type_
;
2464 ctype
= this->constant_
->const_value()->type();
2465 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2471 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2474 this->seen_
= false;
2476 if (r
&& ctype
!= NULL
)
2478 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2480 Float_expression::constrain_float(val
, ctype
);
2482 *ptype
= ctype
!= NULL
? ctype
: t
;
2486 // Return a complex constant value.
2489 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2496 if (this->type_
!= NULL
)
2497 ctype
= this->type_
;
2499 ctype
= this->constant_
->const_value()->type();
2500 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2506 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2510 this->seen_
= false;
2512 if (r
&& ctype
!= NULL
)
2514 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2517 Complex_expression::constrain_complex(real
, imag
, ctype
);
2519 *ptype
= ctype
!= NULL
? ctype
: t
;
2523 // Return the type of the const reference.
2526 Const_expression::do_type()
2528 if (this->type_
!= NULL
)
2533 this->report_error(_("constant refers to itself"));
2534 this->type_
= Type::make_error_type();
2540 Named_constant
* nc
= this->constant_
->const_value();
2541 Type
* ret
= nc
->type();
2545 this->seen_
= false;
2549 // During parsing, a named constant may have a NULL type, but we
2550 // must not return a NULL type here.
2551 ret
= nc
->expr()->type();
2553 this->seen_
= false;
2558 // Set the type of the const reference.
2561 Const_expression::do_determine_type(const Type_context
* context
)
2563 Type
* ctype
= this->constant_
->const_value()->type();
2564 Type
* cetype
= (ctype
!= NULL
2566 : this->constant_
->const_value()->expr()->type());
2567 if (ctype
!= NULL
&& !ctype
->is_abstract())
2569 else if (context
->type
!= NULL
2570 && (context
->type
->integer_type() != NULL
2571 || context
->type
->float_type() != NULL
2572 || context
->type
->complex_type() != NULL
)
2573 && (cetype
->integer_type() != NULL
2574 || cetype
->float_type() != NULL
2575 || cetype
->complex_type() != NULL
))
2576 this->type_
= context
->type
;
2577 else if (context
->type
!= NULL
2578 && context
->type
->is_string_type()
2579 && cetype
->is_string_type())
2580 this->type_
= context
->type
;
2581 else if (context
->type
!= NULL
2582 && context
->type
->is_boolean_type()
2583 && cetype
->is_boolean_type())
2584 this->type_
= context
->type
;
2585 else if (!context
->may_be_abstract
)
2587 if (cetype
->is_abstract())
2588 cetype
= cetype
->make_non_abstract_type();
2589 this->type_
= cetype
;
2593 // Check types of a const reference.
2596 Const_expression::do_check_types(Gogo
*)
2598 if (this->type_
!= NULL
&& this->type_
->is_error_type())
2601 Expression
* init
= this->constant_
->const_value()->expr();
2602 Find_named_object
find_named_object(this->constant_
);
2603 Expression::traverse(&init
, &find_named_object
);
2604 if (find_named_object
.found())
2606 this->report_error(_("constant refers to itself"));
2607 this->type_
= Type::make_error_type();
2611 if (this->type_
== NULL
|| this->type_
->is_abstract())
2614 // Check for integer overflow.
2615 if (this->type_
->integer_type() != NULL
)
2620 if (!this->integer_constant_value(true, ival
, &dummy
))
2624 Expression
* cexpr
= this->constant_
->const_value()->expr();
2625 if (cexpr
->float_constant_value(fval
, &dummy
))
2627 if (!mpfr_integer_p(fval
))
2628 this->report_error(_("floating point constant "
2629 "truncated to integer"));
2632 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2633 Integer_expression::check_constant(ival
, this->type_
,
2643 // Return a tree for the const reference.
2646 Const_expression::do_get_tree(Translate_context
* context
)
2648 Gogo
* gogo
= context
->gogo();
2650 if (this->type_
== NULL
)
2651 type_tree
= NULL_TREE
;
2654 type_tree
= this->type_
->get_tree(gogo
);
2655 if (type_tree
== error_mark_node
)
2656 return error_mark_node
;
2659 // If the type has been set for this expression, but the underlying
2660 // object is an abstract int or float, we try to get the abstract
2661 // value. Otherwise we may lose something in the conversion.
2662 if (this->type_
!= NULL
2663 && this->constant_
->const_value()->type()->is_abstract())
2665 Expression
* expr
= this->constant_
->const_value()->expr();
2669 if (expr
->integer_constant_value(true, ival
, &t
))
2671 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2679 if (expr
->float_constant_value(fval
, &t
))
2681 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2688 if (expr
->complex_constant_value(fval
, imag
, &t
))
2690 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2699 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2700 if (this->type_
== NULL
2701 || const_tree
== error_mark_node
2702 || TREE_TYPE(const_tree
) == error_mark_node
)
2706 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2707 ret
= fold_convert(type_tree
, const_tree
);
2708 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2709 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2710 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2711 ret
= fold(convert_to_real(type_tree
, const_tree
));
2712 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2713 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2719 // Make a reference to a constant in an expression.
2722 Expression::make_const_reference(Named_object
* constant
,
2723 source_location location
)
2725 return new Const_expression(constant
, location
);
2728 // Find a named object in an expression.
2731 Find_named_object::expression(Expression
** pexpr
)
2733 switch ((*pexpr
)->classification())
2735 case Expression::EXPRESSION_CONST_REFERENCE
:
2736 if (static_cast<Const_expression
*>(*pexpr
)->named_object() == this->no_
)
2738 return TRAVERSE_CONTINUE
;
2739 case Expression::EXPRESSION_VAR_REFERENCE
:
2740 if ((*pexpr
)->var_expression()->named_object() == this->no_
)
2742 return TRAVERSE_CONTINUE
;
2743 case Expression::EXPRESSION_FUNC_REFERENCE
:
2744 if ((*pexpr
)->func_expression()->named_object() == this->no_
)
2746 return TRAVERSE_CONTINUE
;
2748 return TRAVERSE_CONTINUE
;
2750 this->found_
= true;
2751 return TRAVERSE_EXIT
;
2756 class Nil_expression
: public Expression
2759 Nil_expression(source_location location
)
2760 : Expression(EXPRESSION_NIL
, location
)
2768 do_is_constant() const
2773 { return Type::make_nil_type(); }
2776 do_determine_type(const Type_context
*)
2784 do_get_tree(Translate_context
*)
2785 { return null_pointer_node
; }
2788 do_export(Export
* exp
) const
2789 { exp
->write_c_string("nil"); }
2792 // Import a nil expression.
2795 Nil_expression::do_import(Import
* imp
)
2797 imp
->require_c_string("nil");
2798 return Expression::make_nil(imp
->location());
2801 // Make a nil expression.
2804 Expression::make_nil(source_location location
)
2806 return new Nil_expression(location
);
2809 // The value of the predeclared constant iota. This is little more
2810 // than a marker. This will be lowered to an integer in
2811 // Const_expression::do_lower, which is where we know the value that
2814 class Iota_expression
: public Parser_expression
2817 Iota_expression(source_location location
)
2818 : Parser_expression(EXPRESSION_IOTA
, location
)
2823 do_lower(Gogo
*, Named_object
*, int)
2824 { gcc_unreachable(); }
2826 // There should only ever be one of these.
2829 { gcc_unreachable(); }
2832 // Make an iota expression. This is only called for one case: the
2833 // value of the predeclared constant iota.
2836 Expression::make_iota()
2838 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2839 return &iota_expression
;
2842 // A type conversion expression.
2844 class Type_conversion_expression
: public Expression
2847 Type_conversion_expression(Type
* type
, Expression
* expr
,
2848 source_location location
)
2849 : Expression(EXPRESSION_CONVERSION
, location
),
2850 type_(type
), expr_(expr
), may_convert_function_types_(false)
2853 // Return the type to which we are converting.
2856 { return this->type_
; }
2858 // Return the expression which we are converting.
2861 { return this->expr_
; }
2863 // Permit converting from one function type to another. This is
2864 // used internally for method expressions.
2866 set_may_convert_function_types()
2868 this->may_convert_function_types_
= true;
2871 // Import a type conversion expression.
2877 do_traverse(Traverse
* traverse
);
2880 do_lower(Gogo
*, Named_object
*, int);
2883 do_is_constant() const
2884 { return this->expr_
->is_constant(); }
2887 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2890 do_float_constant_value(mpfr_t
, Type
**) const;
2893 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2896 do_string_constant_value(std::string
*) const;
2900 { return this->type_
; }
2903 do_determine_type(const Type_context
*)
2905 Type_context
subcontext(this->type_
, false);
2906 this->expr_
->determine_type(&subcontext
);
2910 do_check_types(Gogo
*);
2915 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2920 do_get_tree(Translate_context
* context
);
2923 do_export(Export
*) const;
2926 // The type to convert to.
2928 // The expression to convert.
2930 // True if this is permitted to convert function types. This is
2931 // used internally for method expressions.
2932 bool may_convert_function_types_
;
2938 Type_conversion_expression::do_traverse(Traverse
* traverse
)
2940 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
2941 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
2942 return TRAVERSE_EXIT
;
2943 return TRAVERSE_CONTINUE
;
2946 // Convert to a constant at lowering time.
2949 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
2951 Type
* type
= this->type_
;
2952 Expression
* val
= this->expr_
;
2953 source_location location
= this->location();
2955 if (type
->integer_type() != NULL
)
2960 if (val
->integer_constant_value(false, ival
, &dummy
))
2962 if (!Integer_expression::check_constant(ival
, type
, location
))
2963 mpz_set_ui(ival
, 0);
2964 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2971 if (val
->float_constant_value(fval
, &dummy
))
2973 if (!mpfr_integer_p(fval
))
2976 "floating point constant truncated to integer");
2977 return Expression::make_error(location
);
2979 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2980 if (!Integer_expression::check_constant(ival
, type
, location
))
2981 mpz_set_ui(ival
, 0);
2982 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2991 if (type
->float_type() != NULL
)
2996 if (val
->float_constant_value(fval
, &dummy
))
2998 if (!Float_expression::check_constant(fval
, type
, location
))
2999 mpfr_set_ui(fval
, 0, GMP_RNDN
);
3000 Float_expression::constrain_float(fval
, type
);
3001 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
3008 if (type
->complex_type() != NULL
)
3015 if (val
->complex_constant_value(real
, imag
, &dummy
))
3017 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
3019 mpfr_set_ui(real
, 0, GMP_RNDN
);
3020 mpfr_set_ui(imag
, 0, GMP_RNDN
);
3022 Complex_expression::constrain_complex(real
, imag
, type
);
3023 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
3033 if (type
->is_open_array_type() && type
->named_type() == NULL
)
3035 Type
* element_type
= type
->array_type()->element_type()->forwarded();
3036 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
3037 bool is_int
= element_type
== Type::lookup_integer_type("int");
3038 if (is_byte
|| is_int
)
3041 if (val
->string_constant_value(&s
))
3043 Expression_list
* vals
= new Expression_list();
3046 for (std::string::const_iterator p
= s
.begin();
3051 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
3052 Expression
* v
= Expression::make_integer(&val
,
3061 const char *p
= s
.data();
3062 const char *pend
= s
.data() + s
.length();
3066 int adv
= Lex::fetch_char(p
, &c
);
3069 warning_at(this->location(), 0,
3070 "invalid UTF-8 encoding");
3075 mpz_init_set_ui(val
, c
);
3076 Expression
* v
= Expression::make_integer(&val
,
3084 return Expression::make_slice_composite_literal(type
, vals
,
3093 // Return the constant integer value if there is one.
3096 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
3100 if (this->type_
->integer_type() == NULL
)
3106 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
3108 if (!Integer_expression::check_constant(ival
, this->type_
,
3116 *ptype
= this->type_
;
3123 if (this->expr_
->float_constant_value(fval
, &dummy
))
3125 mpfr_get_z(val
, fval
, GMP_RNDN
);
3127 if (!Integer_expression::check_constant(val
, this->type_
,
3130 *ptype
= this->type_
;
3138 // Return the constant floating point value if there is one.
3141 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3144 if (this->type_
->float_type() == NULL
)
3150 if (this->expr_
->float_constant_value(fval
, &dummy
))
3152 if (!Float_expression::check_constant(fval
, this->type_
,
3158 mpfr_set(val
, fval
, GMP_RNDN
);
3160 Float_expression::constrain_float(val
, this->type_
);
3161 *ptype
= this->type_
;
3169 // Return the constant complex value if there is one.
3172 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3176 if (this->type_
->complex_type() == NULL
)
3184 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3186 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3193 mpfr_set(real
, rval
, GMP_RNDN
);
3194 mpfr_set(imag
, ival
, GMP_RNDN
);
3197 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3198 *ptype
= this->type_
;
3207 // Return the constant string value if there is one.
3210 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3212 if (this->type_
->is_string_type()
3213 && this->expr_
->type()->integer_type() != NULL
)
3218 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3220 unsigned long ulval
= mpz_get_ui(ival
);
3221 if (mpz_cmp_ui(ival
, ulval
) == 0)
3223 Lex::append_char(ulval
, true, val
, this->location());
3231 // FIXME: Could handle conversion from const []int here.
3236 // Check that types are convertible.
3239 Type_conversion_expression::do_check_types(Gogo
*)
3241 Type
* type
= this->type_
;
3242 Type
* expr_type
= this->expr_
->type();
3245 if (this->may_convert_function_types_
3246 && type
->function_type() != NULL
3247 && expr_type
->function_type() != NULL
)
3250 if (Type::are_convertible(type
, expr_type
, &reason
))
3253 error_at(this->location(), "%s", reason
.c_str());
3254 this->set_is_error();
3257 // Get a tree for a type conversion.
3260 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3262 Gogo
* gogo
= context
->gogo();
3263 tree type_tree
= this->type_
->get_tree(gogo
);
3264 tree expr_tree
= this->expr_
->get_tree(context
);
3266 if (type_tree
== error_mark_node
3267 || expr_tree
== error_mark_node
3268 || TREE_TYPE(expr_tree
) == error_mark_node
)
3269 return error_mark_node
;
3271 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3272 return fold_convert(type_tree
, expr_tree
);
3274 Type
* type
= this->type_
;
3275 Type
* expr_type
= this->expr_
->type();
3277 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3278 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3279 expr_tree
, this->location());
3280 else if (type
->integer_type() != NULL
)
3282 if (expr_type
->integer_type() != NULL
3283 || expr_type
->float_type() != NULL
3284 || expr_type
->is_unsafe_pointer_type())
3285 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3289 else if (type
->float_type() != NULL
)
3291 if (expr_type
->integer_type() != NULL
3292 || expr_type
->float_type() != NULL
)
3293 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3297 else if (type
->complex_type() != NULL
)
3299 if (expr_type
->complex_type() != NULL
)
3300 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3304 else if (type
->is_string_type()
3305 && expr_type
->integer_type() != NULL
)
3307 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3308 if (host_integerp(expr_tree
, 0))
3310 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3312 Lex::append_char(intval
, true, &s
, this->location());
3313 Expression
* se
= Expression::make_string(s
, this->location());
3314 return se
->get_tree(context
);
3317 static tree int_to_string_fndecl
;
3318 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3320 "__go_int_to_string",
3324 fold_convert(integer_type_node
, expr_tree
));
3326 else if (type
->is_string_type()
3327 && (expr_type
->array_type() != NULL
3328 || (expr_type
->points_to() != NULL
3329 && expr_type
->points_to()->array_type() != NULL
)))
3331 Type
* t
= expr_type
;
3332 if (t
->points_to() != NULL
)
3335 expr_tree
= build_fold_indirect_ref(expr_tree
);
3337 if (!DECL_P(expr_tree
))
3338 expr_tree
= save_expr(expr_tree
);
3339 Array_type
* a
= t
->array_type();
3340 Type
* e
= a
->element_type()->forwarded();
3341 gcc_assert(e
->integer_type() != NULL
);
3342 tree valptr
= fold_convert(const_ptr_type_node
,
3343 a
->value_pointer_tree(gogo
, expr_tree
));
3344 tree len
= a
->length_tree(gogo
, expr_tree
);
3345 len
= fold_convert_loc(this->location(), size_type_node
, len
);
3346 if (e
->integer_type()->is_unsigned()
3347 && e
->integer_type()->bits() == 8)
3349 static tree byte_array_to_string_fndecl
;
3350 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3352 "__go_byte_array_to_string",
3355 const_ptr_type_node
,
3362 gcc_assert(e
== Type::lookup_integer_type("int"));
3363 static tree int_array_to_string_fndecl
;
3364 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3366 "__go_int_array_to_string",
3369 const_ptr_type_node
,
3375 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3377 Type
* e
= type
->array_type()->element_type()->forwarded();
3378 gcc_assert(e
->integer_type() != NULL
);
3379 if (e
->integer_type()->is_unsigned()
3380 && e
->integer_type()->bits() == 8)
3382 static tree string_to_byte_array_fndecl
;
3383 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3385 "__go_string_to_byte_array",
3388 TREE_TYPE(expr_tree
),
3393 gcc_assert(e
== Type::lookup_integer_type("int"));
3394 static tree string_to_int_array_fndecl
;
3395 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3397 "__go_string_to_int_array",
3400 TREE_TYPE(expr_tree
),
3404 else if ((type
->is_unsafe_pointer_type()
3405 && expr_type
->points_to() != NULL
)
3406 || (expr_type
->is_unsafe_pointer_type()
3407 && type
->points_to() != NULL
))
3408 ret
= fold_convert(type_tree
, expr_tree
);
3409 else if (type
->is_unsafe_pointer_type()
3410 && expr_type
->integer_type() != NULL
)
3411 ret
= convert_to_pointer(type_tree
, expr_tree
);
3412 else if (this->may_convert_function_types_
3413 && type
->function_type() != NULL
3414 && expr_type
->function_type() != NULL
)
3415 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3417 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3418 expr_tree
, this->location());
3423 // Output a type conversion in a constant expression.
3426 Type_conversion_expression::do_export(Export
* exp
) const
3428 exp
->write_c_string("convert(");
3429 exp
->write_type(this->type_
);
3430 exp
->write_c_string(", ");
3431 this->expr_
->export_expression(exp
);
3432 exp
->write_c_string(")");
3435 // Import a type conversion or a struct construction.
3438 Type_conversion_expression::do_import(Import
* imp
)
3440 imp
->require_c_string("convert(");
3441 Type
* type
= imp
->read_type();
3442 imp
->require_c_string(", ");
3443 Expression
* val
= Expression::import_expression(imp
);
3444 imp
->require_c_string(")");
3445 return Expression::make_cast(type
, val
, imp
->location());
3448 // Make a type cast expression.
3451 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3453 if (type
->is_error_type() || val
->is_error_expression())
3454 return Expression::make_error(location
);
3455 return new Type_conversion_expression(type
, val
, location
);
3458 // Unary expressions.
3460 class Unary_expression
: public Expression
3463 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3464 : Expression(EXPRESSION_UNARY
, location
),
3465 op_(op
), escapes_(true), expr_(expr
)
3468 // Return the operator.
3471 { return this->op_
; }
3473 // Return the operand.
3476 { return this->expr_
; }
3478 // Record that an address expression does not escape.
3480 set_does_not_escape()
3482 gcc_assert(this->op_
== OPERATOR_AND
);
3483 this->escapes_
= false;
3486 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3487 // could be done, false if not.
3489 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3492 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3493 // could be done, false if not.
3495 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3497 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3498 // true if this could be done, false if not.
3500 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3508 do_traverse(Traverse
* traverse
)
3509 { return Expression::traverse(&this->expr_
, traverse
); }
3512 do_lower(Gogo
*, Named_object
*, int);
3515 do_is_constant() const;
3518 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3521 do_float_constant_value(mpfr_t
, Type
**) const;
3524 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3530 do_determine_type(const Type_context
*);
3533 do_check_types(Gogo
*);
3538 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3543 do_is_addressable() const
3544 { return this->op_
== OPERATOR_MULT
; }
3547 do_get_tree(Translate_context
*);
3550 do_export(Export
*) const;
3553 // The unary operator to apply.
3555 // Normally true. False if this is an address expression which does
3556 // not escape the current function.
3562 // If we are taking the address of a composite literal, and the
3563 // contents are not constant, then we want to make a heap composite
3567 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3569 source_location loc
= this->location();
3570 Operator op
= this->op_
;
3571 Expression
* expr
= this->expr_
;
3573 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3574 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3576 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3577 // moving x to the heap. FIXME: Is it worth doing a real escape
3578 // analysis here? This case is found in math/unsafe.go and is
3579 // therefore worth special casing.
3580 if (op
== OPERATOR_MULT
)
3582 Expression
* e
= expr
;
3583 while (e
->classification() == EXPRESSION_CONVERSION
)
3585 Type_conversion_expression
* te
3586 = static_cast<Type_conversion_expression
*>(e
);
3590 if (e
->classification() == EXPRESSION_UNARY
)
3592 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3593 if (ue
->op_
== OPERATOR_AND
)
3600 ue
->set_does_not_escape();
3605 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3606 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3608 Expression
* ret
= NULL
;
3613 if (expr
->integer_constant_value(false, eval
, &etype
))
3617 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3618 ret
= Expression::make_integer(&val
, etype
, loc
);
3625 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3630 if (expr
->float_constant_value(fval
, &ftype
))
3634 if (Unary_expression::eval_float(op
, fval
, val
))
3635 ret
= Expression::make_float(&val
, ftype
, loc
);
3646 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3652 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3653 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3667 // Return whether a unary expression is a constant.
3670 Unary_expression::do_is_constant() const
3672 if (this->op_
== OPERATOR_MULT
)
3674 // Indirecting through a pointer is only constant if the object
3675 // to which the expression points is constant, but we currently
3676 // have no way to determine that.
3679 else if (this->op_
== OPERATOR_AND
)
3681 // Taking the address of a variable is constant if it is a
3682 // global variable, not constant otherwise. In other cases
3683 // taking the address is probably not a constant.
3684 Var_expression
* ve
= this->expr_
->var_expression();
3687 Named_object
* no
= ve
->named_object();
3688 return no
->is_variable() && no
->var_value()->is_global();
3693 return this->expr_
->is_constant();
3696 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3697 // UVAL, if known; it may be NULL. Return true if this could be done,
3701 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3702 source_location location
)
3709 case OPERATOR_MINUS
:
3711 return Integer_expression::check_constant(val
, utype
, location
);
3713 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3717 || utype
->integer_type() == NULL
3718 || utype
->integer_type()->is_abstract())
3722 // The number of HOST_WIDE_INTs that it takes to represent
3724 size_t count
= ((mpz_sizeinbase(uval
, 2)
3725 + HOST_BITS_PER_WIDE_INT
3727 / HOST_BITS_PER_WIDE_INT
);
3729 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3730 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3733 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3734 gcc_assert(ecount
<= count
);
3736 // Trim down to the number of words required by the type.
3737 size_t obits
= utype
->integer_type()->bits();
3738 if (!utype
->integer_type()->is_unsigned())
3740 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3741 / HOST_BITS_PER_WIDE_INT
);
3742 gcc_assert(ocount
<= ocount
);
3744 for (size_t i
= 0; i
< ocount
; ++i
)
3747 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3749 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3752 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3756 return Integer_expression::check_constant(val
, utype
, location
);
3765 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3766 // could be done, false if not.
3769 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3774 mpfr_set(val
, uval
, GMP_RNDN
);
3776 case OPERATOR_MINUS
:
3777 mpfr_neg(val
, uval
, GMP_RNDN
);
3789 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3790 // if this could be done, false if not.
3793 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
3794 mpfr_t real
, mpfr_t imag
)
3799 mpfr_set(real
, rval
, GMP_RNDN
);
3800 mpfr_set(imag
, ival
, GMP_RNDN
);
3802 case OPERATOR_MINUS
:
3803 mpfr_neg(real
, rval
, GMP_RNDN
);
3804 mpfr_neg(imag
, ival
, GMP_RNDN
);
3816 // Return the integral constant value of a unary expression, if it has one.
3819 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
3825 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
3828 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
3834 // Return the floating point constant value of a unary expression, if
3838 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
3843 if (!this->expr_
->float_constant_value(uval
, ptype
))
3846 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
3851 // Return the complex constant value of a unary expression, if it has
3855 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
3863 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
3866 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
3872 // Return the type of a unary expression.
3875 Unary_expression::do_type()
3880 case OPERATOR_MINUS
:
3883 return this->expr_
->type();
3886 return Type::make_pointer_type(this->expr_
->type());
3890 Type
* subtype
= this->expr_
->type();
3891 Type
* points_to
= subtype
->points_to();
3892 if (points_to
== NULL
)
3893 return Type::make_error_type();
3902 // Determine abstract types for a unary expression.
3905 Unary_expression::do_determine_type(const Type_context
* context
)
3910 case OPERATOR_MINUS
:
3913 this->expr_
->determine_type(context
);
3917 // Taking the address of something.
3919 Type
* subtype
= (context
->type
== NULL
3921 : context
->type
->points_to());
3922 Type_context
subcontext(subtype
, false);
3923 this->expr_
->determine_type(&subcontext
);
3928 // Indirecting through a pointer.
3930 Type
* subtype
= (context
->type
== NULL
3932 : Type::make_pointer_type(context
->type
));
3933 Type_context
subcontext(subtype
, false);
3934 this->expr_
->determine_type(&subcontext
);
3943 // Check types for a unary expression.
3946 Unary_expression::do_check_types(Gogo
*)
3948 Type
* type
= this->expr_
->type();
3949 if (type
->is_error_type())
3951 this->set_is_error();
3958 case OPERATOR_MINUS
:
3959 if (type
->integer_type() == NULL
3960 && type
->float_type() == NULL
3961 && type
->complex_type() == NULL
)
3962 this->report_error(_("expected numeric type"));
3967 if (type
->integer_type() == NULL
3968 && !type
->is_boolean_type())
3969 this->report_error(_("expected integer or boolean type"));
3973 if (!this->expr_
->is_addressable())
3974 this->report_error(_("invalid operand for unary %<&%>"));
3976 this->expr_
->address_taken(this->escapes_
);
3980 // Indirecting through a pointer.
3981 if (type
->points_to() == NULL
)
3982 this->report_error(_("expected pointer"));
3990 // Get a tree for a unary expression.
3993 Unary_expression::do_get_tree(Translate_context
* context
)
3995 tree expr
= this->expr_
->get_tree(context
);
3996 if (expr
== error_mark_node
)
3997 return error_mark_node
;
3999 source_location loc
= this->location();
4005 case OPERATOR_MINUS
:
4007 tree type
= TREE_TYPE(expr
);
4008 tree compute_type
= excess_precision_type(type
);
4009 if (compute_type
!= NULL_TREE
)
4010 expr
= ::convert(compute_type
, expr
);
4011 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
4012 (compute_type
!= NULL_TREE
4016 if (compute_type
!= NULL_TREE
)
4017 ret
= ::convert(type
, ret
);
4022 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
4023 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4025 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
4026 build_int_cst(TREE_TYPE(expr
), 0));
4029 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
4032 // We should not see a non-constant constructor here; cases
4033 // where we would see one should have been moved onto the heap
4034 // at parse time. Taking the address of a nonconstant
4035 // constructor will not do what the programmer expects.
4036 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
4037 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
4039 // Build a decl for a constant constructor.
4040 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
4042 tree decl
= build_decl(this->location(), VAR_DECL
,
4043 create_tmp_var_name("C"), TREE_TYPE(expr
));
4044 DECL_EXTERNAL(decl
) = 0;
4045 TREE_PUBLIC(decl
) = 0;
4046 TREE_READONLY(decl
) = 1;
4047 TREE_CONSTANT(decl
) = 1;
4048 TREE_STATIC(decl
) = 1;
4049 TREE_ADDRESSABLE(decl
) = 1;
4050 DECL_ARTIFICIAL(decl
) = 1;
4051 DECL_INITIAL(decl
) = expr
;
4052 rest_of_decl_compilation(decl
, 1, 0);
4056 return build_fold_addr_expr_loc(loc
, expr
);
4060 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
4062 // If we are dereferencing the pointer to a large struct, we
4063 // need to check for nil. We don't bother to check for small
4064 // structs because we expect the system to crash on a nil
4065 // pointer dereference.
4066 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
4067 if (s
== -1 || s
>= 4096)
4070 expr
= save_expr(expr
);
4071 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
4073 fold_convert(TREE_TYPE(expr
),
4074 null_pointer_node
));
4075 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
4077 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
4078 build3(COND_EXPR
, void_type_node
,
4079 compare
, crash
, NULL_TREE
),
4083 // If the type of EXPR is a recursive pointer type, then we
4084 // need to insert a cast before indirecting.
4085 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
4087 Type
* pt
= this->expr_
->type()->points_to();
4088 tree ind
= pt
->get_tree(context
->gogo());
4089 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
4092 return build_fold_indirect_ref_loc(loc
, expr
);
4100 // Export a unary expression.
4103 Unary_expression::do_export(Export
* exp
) const
4108 exp
->write_c_string("+ ");
4110 case OPERATOR_MINUS
:
4111 exp
->write_c_string("- ");
4114 exp
->write_c_string("! ");
4117 exp
->write_c_string("^ ");
4124 this->expr_
->export_expression(exp
);
4127 // Import a unary expression.
4130 Unary_expression::do_import(Import
* imp
)
4133 switch (imp
->get_char())
4139 op
= OPERATOR_MINUS
;
4150 imp
->require_c_string(" ");
4151 Expression
* expr
= Expression::import_expression(imp
);
4152 return Expression::make_unary(op
, expr
, imp
->location());
4155 // Make a unary expression.
4158 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4160 return new Unary_expression(op
, expr
, location
);
4163 // If this is an indirection through a pointer, return the expression
4164 // being pointed through. Otherwise return this.
4169 if (this->classification_
== EXPRESSION_UNARY
)
4171 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4172 if (ue
->op() == OPERATOR_MULT
)
4173 return ue
->operand();
4178 // Class Binary_expression.
4183 Binary_expression::do_traverse(Traverse
* traverse
)
4185 int t
= Expression::traverse(&this->left_
, traverse
);
4186 if (t
== TRAVERSE_EXIT
)
4187 return TRAVERSE_EXIT
;
4188 return Expression::traverse(&this->right_
, traverse
);
4191 // Compare integer constants according to OP.
4194 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4197 int i
= mpz_cmp(left_val
, right_val
);
4202 case OPERATOR_NOTEQ
:
4217 // Compare floating point constants according to OP.
4220 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4225 i
= mpfr_cmp(left_val
, right_val
);
4229 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4231 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4232 Float_expression::constrain_float(lv
, type
);
4233 Float_expression::constrain_float(rv
, type
);
4234 i
= mpfr_cmp(lv
, rv
);
4242 case OPERATOR_NOTEQ
:
4257 // Compare complex constants according to OP. Complex numbers may
4258 // only be compared for equality.
4261 Binary_expression::compare_complex(Operator op
, Type
* type
,
4262 mpfr_t left_real
, mpfr_t left_imag
,
4263 mpfr_t right_real
, mpfr_t right_imag
)
4267 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4268 && mpfr_cmp(left_imag
, right_imag
) == 0);
4273 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4274 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4277 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4278 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4279 Complex_expression::constrain_complex(lr
, li
, type
);
4280 Complex_expression::constrain_complex(rr
, ri
, type
);
4281 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4291 case OPERATOR_NOTEQ
:
4298 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4299 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4300 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4301 // this could be done, false if not.
4304 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4305 Type
* right_type
, mpz_t right_val
,
4306 source_location location
, mpz_t val
)
4308 bool is_shift_op
= false;
4312 case OPERATOR_ANDAND
:
4314 case OPERATOR_NOTEQ
:
4319 // These return boolean values. We should probably handle them
4320 // anyhow in case a type conversion is used on the result.
4323 mpz_add(val
, left_val
, right_val
);
4325 case OPERATOR_MINUS
:
4326 mpz_sub(val
, left_val
, right_val
);
4329 mpz_ior(val
, left_val
, right_val
);
4332 mpz_xor(val
, left_val
, right_val
);
4335 mpz_mul(val
, left_val
, right_val
);
4338 if (mpz_sgn(right_val
) != 0)
4339 mpz_tdiv_q(val
, left_val
, right_val
);
4342 error_at(location
, "division by zero");
4348 if (mpz_sgn(right_val
) != 0)
4349 mpz_tdiv_r(val
, left_val
, right_val
);
4352 error_at(location
, "division by zero");
4357 case OPERATOR_LSHIFT
:
4359 unsigned long shift
= mpz_get_ui(right_val
);
4360 if (mpz_cmp_ui(right_val
, shift
) != 0)
4362 error_at(location
, "shift count overflow");
4366 mpz_mul_2exp(val
, left_val
, shift
);
4371 case OPERATOR_RSHIFT
:
4373 unsigned long shift
= mpz_get_ui(right_val
);
4374 if (mpz_cmp_ui(right_val
, shift
) != 0)
4376 error_at(location
, "shift count overflow");
4380 if (mpz_cmp_ui(left_val
, 0) >= 0)
4381 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4383 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4389 mpz_and(val
, left_val
, right_val
);
4391 case OPERATOR_BITCLEAR
:
4395 mpz_com(tval
, right_val
);
4396 mpz_and(val
, left_val
, tval
);
4404 Type
* type
= left_type
;
4409 else if (type
!= right_type
&& right_type
!= NULL
)
4411 if (type
->is_abstract())
4413 else if (!right_type
->is_abstract())
4415 // This look like a type error which should be diagnosed
4416 // elsewhere. Don't do anything here, to avoid an
4417 // unhelpful chain of error messages.
4423 if (type
!= NULL
&& !type
->is_abstract())
4425 // We have to check the operands too, as we have implicitly
4426 // coerced them to TYPE.
4427 if ((type
!= left_type
4428 && !Integer_expression::check_constant(left_val
, type
, location
))
4430 && type
!= right_type
4431 && !Integer_expression::check_constant(right_val
, type
,
4433 || !Integer_expression::check_constant(val
, type
, location
))
4440 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4441 // Return true if this could be done, false if not.
4444 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4445 Type
* right_type
, mpfr_t right_val
,
4446 mpfr_t val
, source_location location
)
4451 case OPERATOR_ANDAND
:
4453 case OPERATOR_NOTEQ
:
4458 // These return boolean values. We should probably handle them
4459 // anyhow in case a type conversion is used on the result.
4462 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4464 case OPERATOR_MINUS
:
4465 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4470 case OPERATOR_BITCLEAR
:
4473 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4476 if (mpfr_zero_p(right_val
))
4477 error_at(location
, "division by zero");
4478 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4482 case OPERATOR_LSHIFT
:
4483 case OPERATOR_RSHIFT
:
4489 Type
* type
= left_type
;
4492 else if (type
!= right_type
&& right_type
!= NULL
)
4494 if (type
->is_abstract())
4496 else if (!right_type
->is_abstract())
4498 // This looks like a type error which should be diagnosed
4499 // elsewhere. Don't do anything here, to avoid an unhelpful
4500 // chain of error messages.
4505 if (type
!= NULL
&& !type
->is_abstract())
4507 if ((type
!= left_type
4508 && !Float_expression::check_constant(left_val
, type
, location
))
4509 || (type
!= right_type
4510 && !Float_expression::check_constant(right_val
, type
,
4512 || !Float_expression::check_constant(val
, type
, location
))
4513 mpfr_set_ui(val
, 0, GMP_RNDN
);
4519 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4520 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4521 // could be done, false if not.
4524 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4525 mpfr_t left_real
, mpfr_t left_imag
,
4527 mpfr_t right_real
, mpfr_t right_imag
,
4528 mpfr_t real
, mpfr_t imag
,
4529 source_location location
)
4534 case OPERATOR_ANDAND
:
4536 case OPERATOR_NOTEQ
:
4541 // These return boolean values and must be handled differently.
4544 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4545 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4547 case OPERATOR_MINUS
:
4548 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4549 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4554 case OPERATOR_BITCLEAR
:
4558 // You might think that multiplying two complex numbers would
4559 // be simple, and you would be right, until you start to think
4560 // about getting the right answer for infinity. If one
4561 // operand here is infinity and the other is anything other
4562 // than zero or NaN, then we are going to wind up subtracting
4563 // two infinity values. That will give us a NaN, but the
4564 // correct answer is infinity.
4568 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4572 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4576 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4580 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4582 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4583 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4585 // If we get NaN on both sides, check whether it should really
4586 // be infinity. The rule is that if either side of the
4587 // complex number is infinity, then the whole value is
4588 // infinity, even if the other side is NaN. So the only case
4589 // we have to fix is the one in which both sides are NaN.
4590 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4591 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4592 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4594 bool is_infinity
= false;
4598 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4599 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4603 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4604 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4606 // If the left side is infinity, then the result is
4608 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4610 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4611 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4612 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4613 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4616 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4617 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4621 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4622 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4627 // If the right side is infinity, then the result is
4629 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4631 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4632 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4633 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4634 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4637 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4638 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4642 mpfr_set_ui(li
, 0, GMP_RNDN
);
4643 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4648 // If we got an overflow in the intermediate computations,
4649 // then the result is infinity.
4651 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4652 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4656 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4657 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4661 mpfr_set_ui(li
, 0, GMP_RNDN
);
4662 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4666 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4667 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4671 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4672 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4679 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4680 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4681 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4682 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4683 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4684 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4685 mpfr_set_inf(real
, mpfr_sgn(real
));
4686 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4703 // For complex division we want to avoid having an
4704 // intermediate overflow turn the whole result in a NaN. We
4705 // scale the values to try to avoid this.
4707 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4708 error_at(location
, "division by zero");
4714 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4715 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4718 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4722 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4723 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4725 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4727 ilogbw
= mpfr_get_exp(t
);
4728 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4729 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4734 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4735 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4736 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4738 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4739 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4740 mpfr_add(real
, real
, t
, GMP_RNDN
);
4741 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4742 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4744 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4745 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4746 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4747 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4748 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4750 // If we wind up with NaN on both sides, check whether we
4751 // should really have infinity. The rule is that if either
4752 // side of the complex number is infinity, then the whole
4753 // value is infinity, even if the other side is NaN. So the
4754 // only case we have to fix is the one in which both sides are
4756 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4757 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4758 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4760 if (mpfr_zero_p(denom
))
4762 mpfr_set_inf(real
, mpfr_sgn(rr
));
4763 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4764 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4765 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4767 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4768 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4770 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4771 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4774 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4775 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4779 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4783 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4785 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4786 mpfr_set_inf(real
, mpfr_sgn(t3
));
4788 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4789 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4790 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4791 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4797 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4798 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4800 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4801 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4804 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4805 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4809 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
4813 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
4815 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4816 mpfr_set_ui(real
, 0, GMP_RNDN
);
4817 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
4819 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
4820 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
4821 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4822 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4823 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
4841 case OPERATOR_LSHIFT
:
4842 case OPERATOR_RSHIFT
:
4848 Type
* type
= left_type
;
4851 else if (type
!= right_type
&& right_type
!= NULL
)
4853 if (type
->is_abstract())
4855 else if (!right_type
->is_abstract())
4857 // This looks like a type error which should be diagnosed
4858 // elsewhere. Don't do anything here, to avoid an unhelpful
4859 // chain of error messages.
4864 if (type
!= NULL
&& !type
->is_abstract())
4866 if ((type
!= left_type
4867 && !Complex_expression::check_constant(left_real
, left_imag
,
4869 || (type
!= right_type
4870 && !Complex_expression::check_constant(right_real
, right_imag
,
4872 || !Complex_expression::check_constant(real
, imag
, type
,
4875 mpfr_set_ui(real
, 0, GMP_RNDN
);
4876 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4883 // Lower a binary expression. We have to evaluate constant
4884 // expressions now, in order to implement Go's unlimited precision
4888 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
4890 source_location location
= this->location();
4891 Operator op
= this->op_
;
4892 Expression
* left
= this->left_
;
4893 Expression
* right
= this->right_
;
4895 const bool is_comparison
= (op
== OPERATOR_EQEQ
4896 || op
== OPERATOR_NOTEQ
4897 || op
== OPERATOR_LT
4898 || op
== OPERATOR_LE
4899 || op
== OPERATOR_GT
4900 || op
== OPERATOR_GE
);
4902 // Integer constant expressions.
4908 mpz_init(right_val
);
4910 if (left
->integer_constant_value(false, left_val
, &left_type
)
4911 && right
->integer_constant_value(false, right_val
, &right_type
))
4913 Expression
* ret
= NULL
;
4914 if (left_type
!= right_type
4915 && left_type
!= NULL
4916 && right_type
!= NULL
4917 && left_type
->base() != right_type
->base()
4918 && op
!= OPERATOR_LSHIFT
4919 && op
!= OPERATOR_RSHIFT
)
4921 // May be a type error--let it be diagnosed later.
4923 else if (is_comparison
)
4925 bool b
= Binary_expression::compare_integer(op
, left_val
,
4927 ret
= Expression::make_cast(Type::lookup_bool_type(),
4928 Expression::make_boolean(b
, location
),
4936 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
4937 right_type
, right_val
,
4940 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
4942 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4944 else if (left_type
== NULL
)
4946 else if (right_type
== NULL
)
4948 else if (!left_type
->is_abstract()
4949 && left_type
->named_type() != NULL
)
4951 else if (!right_type
->is_abstract()
4952 && right_type
->named_type() != NULL
)
4954 else if (!left_type
->is_abstract())
4956 else if (!right_type
->is_abstract())
4958 else if (left_type
->float_type() != NULL
)
4960 else if (right_type
->float_type() != NULL
)
4962 else if (left_type
->complex_type() != NULL
)
4964 else if (right_type
->complex_type() != NULL
)
4968 ret
= Expression::make_integer(&val
, type
, location
);
4976 mpz_clear(right_val
);
4977 mpz_clear(left_val
);
4981 mpz_clear(right_val
);
4982 mpz_clear(left_val
);
4985 // Floating point constant expressions.
4988 mpfr_init(left_val
);
4991 mpfr_init(right_val
);
4993 if (left
->float_constant_value(left_val
, &left_type
)
4994 && right
->float_constant_value(right_val
, &right_type
))
4996 Expression
* ret
= NULL
;
4997 if (left_type
!= right_type
4998 && left_type
!= NULL
4999 && right_type
!= NULL
5000 && left_type
->base() != right_type
->base()
5001 && op
!= OPERATOR_LSHIFT
5002 && op
!= OPERATOR_RSHIFT
)
5004 // May be a type error--let it be diagnosed later.
5006 else if (is_comparison
)
5008 bool b
= Binary_expression::compare_float(op
,
5012 left_val
, right_val
);
5013 ret
= Expression::make_boolean(b
, location
);
5020 if (Binary_expression::eval_float(op
, left_type
, left_val
,
5021 right_type
, right_val
, val
,
5024 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5025 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5027 if (left_type
== NULL
)
5029 else if (right_type
== NULL
)
5031 else if (!left_type
->is_abstract()
5032 && left_type
->named_type() != NULL
)
5034 else if (!right_type
->is_abstract()
5035 && right_type
->named_type() != NULL
)
5037 else if (!left_type
->is_abstract())
5039 else if (!right_type
->is_abstract())
5041 else if (left_type
->float_type() != NULL
)
5043 else if (right_type
->float_type() != NULL
)
5047 ret
= Expression::make_float(&val
, type
, location
);
5055 mpfr_clear(right_val
);
5056 mpfr_clear(left_val
);
5060 mpfr_clear(right_val
);
5061 mpfr_clear(left_val
);
5064 // Complex constant expressions.
5068 mpfr_init(left_real
);
5069 mpfr_init(left_imag
);
5074 mpfr_init(right_real
);
5075 mpfr_init(right_imag
);
5078 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
5079 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
5081 Expression
* ret
= NULL
;
5082 if (left_type
!= right_type
5083 && left_type
!= NULL
5084 && right_type
!= NULL
5085 && left_type
->base() != right_type
->base())
5087 // May be a type error--let it be diagnosed later.
5089 else if (is_comparison
)
5091 bool b
= Binary_expression::compare_complex(op
,
5099 ret
= Expression::make_boolean(b
, location
);
5108 if (Binary_expression::eval_complex(op
, left_type
,
5109 left_real
, left_imag
,
5111 right_real
, right_imag
,
5115 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
5116 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
5118 if (left_type
== NULL
)
5120 else if (right_type
== NULL
)
5122 else if (!left_type
->is_abstract()
5123 && left_type
->named_type() != NULL
)
5125 else if (!right_type
->is_abstract()
5126 && right_type
->named_type() != NULL
)
5128 else if (!left_type
->is_abstract())
5130 else if (!right_type
->is_abstract())
5132 else if (left_type
->complex_type() != NULL
)
5134 else if (right_type
->complex_type() != NULL
)
5138 ret
= Expression::make_complex(&real
, &imag
, type
,
5147 mpfr_clear(left_real
);
5148 mpfr_clear(left_imag
);
5149 mpfr_clear(right_real
);
5150 mpfr_clear(right_imag
);
5155 mpfr_clear(left_real
);
5156 mpfr_clear(left_imag
);
5157 mpfr_clear(right_real
);
5158 mpfr_clear(right_imag
);
5161 // String constant expressions.
5162 if (op
== OPERATOR_PLUS
5163 && left
->type()->is_string_type()
5164 && right
->type()->is_string_type())
5166 std::string left_string
;
5167 std::string right_string
;
5168 if (left
->string_constant_value(&left_string
)
5169 && right
->string_constant_value(&right_string
))
5170 return Expression::make_string(left_string
+ right_string
, location
);
5176 // Return the integer constant value, if it has one.
5179 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5185 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5188 mpz_clear(left_val
);
5193 mpz_init(right_val
);
5195 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5198 mpz_clear(right_val
);
5199 mpz_clear(left_val
);
5204 if (left_type
!= right_type
5205 && left_type
!= NULL
5206 && right_type
!= NULL
5207 && left_type
->base() != right_type
->base()
5208 && this->op_
!= OPERATOR_RSHIFT
5209 && this->op_
!= OPERATOR_LSHIFT
)
5212 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5213 right_type
, right_val
,
5214 this->location(), val
);
5216 mpz_clear(right_val
);
5217 mpz_clear(left_val
);
5225 // Return the floating point constant value, if it has one.
5228 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5231 mpfr_init(left_val
);
5233 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5235 mpfr_clear(left_val
);
5240 mpfr_init(right_val
);
5242 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5244 mpfr_clear(right_val
);
5245 mpfr_clear(left_val
);
5250 if (left_type
!= right_type
5251 && left_type
!= NULL
5252 && right_type
!= NULL
5253 && left_type
->base() != right_type
->base())
5256 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5257 right_type
, right_val
,
5258 val
, this->location());
5260 mpfr_clear(left_val
);
5261 mpfr_clear(right_val
);
5269 // Return the complex constant value, if it has one.
5272 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5277 mpfr_init(left_real
);
5278 mpfr_init(left_imag
);
5280 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5282 mpfr_clear(left_real
);
5283 mpfr_clear(left_imag
);
5289 mpfr_init(right_real
);
5290 mpfr_init(right_imag
);
5292 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5295 mpfr_clear(left_real
);
5296 mpfr_clear(left_imag
);
5297 mpfr_clear(right_real
);
5298 mpfr_clear(right_imag
);
5303 if (left_type
!= right_type
5304 && left_type
!= NULL
5305 && right_type
!= NULL
5306 && left_type
->base() != right_type
->base())
5309 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5310 left_real
, left_imag
,
5312 right_real
, right_imag
,
5315 mpfr_clear(left_real
);
5316 mpfr_clear(left_imag
);
5317 mpfr_clear(right_real
);
5318 mpfr_clear(right_imag
);
5326 // Note that the value is being discarded.
5329 Binary_expression::do_discarding_value()
5331 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5332 this->right_
->discarding_value();
5334 this->warn_about_unused_value();
5340 Binary_expression::do_type()
5345 case OPERATOR_ANDAND
:
5347 case OPERATOR_NOTEQ
:
5352 return Type::lookup_bool_type();
5355 case OPERATOR_MINUS
:
5362 case OPERATOR_BITCLEAR
:
5364 Type
* left_type
= this->left_
->type();
5365 Type
* right_type
= this->right_
->type();
5366 if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5368 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5370 else if (!left_type
->is_abstract())
5372 else if (!right_type
->is_abstract())
5374 else if (left_type
->complex_type() != NULL
)
5376 else if (right_type
->complex_type() != NULL
)
5378 else if (left_type
->float_type() != NULL
)
5380 else if (right_type
->float_type() != NULL
)
5386 case OPERATOR_LSHIFT
:
5387 case OPERATOR_RSHIFT
:
5388 return this->left_
->type();
5395 // Set type for a binary expression.
5398 Binary_expression::do_determine_type(const Type_context
* context
)
5400 Type
* tleft
= this->left_
->type();
5401 Type
* tright
= this->right_
->type();
5403 // Both sides should have the same type, except for the shift
5404 // operations. For a comparison, we should ignore the incoming
5407 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5408 || this->op_
== OPERATOR_RSHIFT
);
5410 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5411 || this->op_
== OPERATOR_NOTEQ
5412 || this->op_
== OPERATOR_LT
5413 || this->op_
== OPERATOR_LE
5414 || this->op_
== OPERATOR_GT
5415 || this->op_
== OPERATOR_GE
);
5417 Type_context
subcontext(*context
);
5421 // In a comparison, the context does not determine the types of
5423 subcontext
.type
= NULL
;
5426 // Set the context for the left hand operand.
5429 // The right hand operand plays no role in determining the type
5430 // of the left hand operand. A shift of an abstract integer in
5431 // a string context gets special treatment, which may be a
5433 if (subcontext
.type
!= NULL
5434 && subcontext
.type
->is_string_type()
5435 && tleft
->is_abstract())
5436 error_at(this->location(), "shift of non-integer operand");
5438 else if (!tleft
->is_abstract())
5439 subcontext
.type
= tleft
;
5440 else if (!tright
->is_abstract())
5441 subcontext
.type
= tright
;
5442 else if (subcontext
.type
== NULL
)
5444 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5445 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5446 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5448 // Both sides have an abstract integer, abstract float, or
5449 // abstract complex type. Just let CONTEXT determine
5450 // whether they may remain abstract or not.
5452 else if (tleft
->complex_type() != NULL
)
5453 subcontext
.type
= tleft
;
5454 else if (tright
->complex_type() != NULL
)
5455 subcontext
.type
= tright
;
5456 else if (tleft
->float_type() != NULL
)
5457 subcontext
.type
= tleft
;
5458 else if (tright
->float_type() != NULL
)
5459 subcontext
.type
= tright
;
5461 subcontext
.type
= tleft
;
5464 this->left_
->determine_type(&subcontext
);
5466 // The context for the right hand operand is the same as for the
5467 // left hand operand, except for a shift operator.
5470 subcontext
.type
= Type::lookup_integer_type("uint");
5471 subcontext
.may_be_abstract
= false;
5474 this->right_
->determine_type(&subcontext
);
5477 // Report an error if the binary operator OP does not support TYPE.
5478 // Return whether the operation is OK. This should not be used for
5482 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5483 source_location location
)
5488 case OPERATOR_ANDAND
:
5489 if (!type
->is_boolean_type())
5491 error_at(location
, "expected boolean type");
5497 case OPERATOR_NOTEQ
:
5498 if (type
->integer_type() == NULL
5499 && type
->float_type() == NULL
5500 && type
->complex_type() == NULL
5501 && !type
->is_string_type()
5502 && type
->points_to() == NULL
5503 && !type
->is_nil_type()
5504 && !type
->is_boolean_type()
5505 && type
->interface_type() == NULL
5506 && (type
->array_type() == NULL
5507 || type
->array_type()->length() != NULL
)
5508 && type
->map_type() == NULL
5509 && type
->channel_type() == NULL
5510 && type
->function_type() == NULL
)
5513 ("expected integer, floating, complex, string, pointer, "
5514 "boolean, interface, slice, map, channel, "
5515 "or function type"));
5524 if (type
->integer_type() == NULL
5525 && type
->float_type() == NULL
5526 && !type
->is_string_type())
5528 error_at(location
, "expected integer, floating, or string type");
5534 case OPERATOR_PLUSEQ
:
5535 if (type
->integer_type() == NULL
5536 && type
->float_type() == NULL
5537 && type
->complex_type() == NULL
5538 && !type
->is_string_type())
5541 "expected integer, floating, complex, or string type");
5546 case OPERATOR_MINUS
:
5547 case OPERATOR_MINUSEQ
:
5549 case OPERATOR_MULTEQ
:
5551 case OPERATOR_DIVEQ
:
5552 if (type
->integer_type() == NULL
5553 && type
->float_type() == NULL
5554 && type
->complex_type() == NULL
)
5556 error_at(location
, "expected integer, floating, or complex type");
5562 case OPERATOR_MODEQ
:
5566 case OPERATOR_ANDEQ
:
5568 case OPERATOR_XOREQ
:
5569 case OPERATOR_BITCLEAR
:
5570 case OPERATOR_BITCLEAREQ
:
5571 if (type
->integer_type() == NULL
)
5573 error_at(location
, "expected integer type");
5588 Binary_expression::do_check_types(Gogo
*)
5590 Type
* left_type
= this->left_
->type();
5591 Type
* right_type
= this->right_
->type();
5592 if (left_type
->is_error_type() || right_type
->is_error_type())
5594 this->set_is_error();
5598 if (this->op_
== OPERATOR_EQEQ
5599 || this->op_
== OPERATOR_NOTEQ
5600 || this->op_
== OPERATOR_LT
5601 || this->op_
== OPERATOR_LE
5602 || this->op_
== OPERATOR_GT
5603 || this->op_
== OPERATOR_GE
)
5605 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5606 && !Type::are_assignable(right_type
, left_type
, NULL
))
5608 this->report_error(_("incompatible types in binary expression"));
5611 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5613 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5616 this->set_is_error();
5620 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5622 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5624 this->report_error(_("incompatible types in binary expression"));
5627 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5630 this->set_is_error();
5636 if (left_type
->integer_type() == NULL
)
5637 this->report_error(_("shift of non-integer operand"));
5639 if (!right_type
->is_abstract()
5640 && (right_type
->integer_type() == NULL
5641 || !right_type
->integer_type()->is_unsigned()))
5642 this->report_error(_("shift count not unsigned integer"));
5648 if (this->right_
->integer_constant_value(true, val
, &type
))
5650 if (mpz_sgn(val
) < 0)
5651 this->report_error(_("negative shift count"));
5658 // Get a tree for a binary expression.
5661 Binary_expression::do_get_tree(Translate_context
* context
)
5663 tree left
= this->left_
->get_tree(context
);
5664 tree right
= this->right_
->get_tree(context
);
5666 if (left
== error_mark_node
|| right
== error_mark_node
)
5667 return error_mark_node
;
5669 enum tree_code code
;
5670 bool use_left_type
= true;
5671 bool is_shift_op
= false;
5675 case OPERATOR_NOTEQ
:
5680 return Expression::comparison_tree(context
, this->op_
,
5681 this->left_
->type(), left
,
5682 this->right_
->type(), right
,
5686 code
= TRUTH_ORIF_EXPR
;
5687 use_left_type
= false;
5689 case OPERATOR_ANDAND
:
5690 code
= TRUTH_ANDIF_EXPR
;
5691 use_left_type
= false;
5696 case OPERATOR_MINUS
:
5700 code
= BIT_IOR_EXPR
;
5703 code
= BIT_XOR_EXPR
;
5710 Type
*t
= this->left_
->type();
5711 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5714 code
= TRUNC_DIV_EXPR
;
5718 code
= TRUNC_MOD_EXPR
;
5720 case OPERATOR_LSHIFT
:
5724 case OPERATOR_RSHIFT
:
5729 code
= BIT_AND_EXPR
;
5731 case OPERATOR_BITCLEAR
:
5732 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5733 code
= BIT_AND_EXPR
;
5739 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5741 if (this->left_
->type()->is_string_type())
5743 gcc_assert(this->op_
== OPERATOR_PLUS
);
5744 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5745 static tree string_plus_decl
;
5746 return Gogo::call_builtin(&string_plus_decl
,
5757 tree compute_type
= excess_precision_type(type
);
5758 if (compute_type
!= NULL_TREE
)
5760 left
= ::convert(compute_type
, left
);
5761 right
= ::convert(compute_type
, right
);
5764 tree eval_saved
= NULL_TREE
;
5768 left
= save_expr(left
);
5770 right
= save_expr(right
);
5771 // Make sure the values are evaluated.
5772 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5773 void_type_node
, left
, right
);
5776 tree ret
= fold_build2_loc(this->location(),
5778 compute_type
!= NULL_TREE
? compute_type
: type
,
5781 if (compute_type
!= NULL_TREE
)
5782 ret
= ::convert(type
, ret
);
5784 // In Go, a shift larger than the size of the type is well-defined.
5785 // This is not true in GENERIC, so we need to insert a conditional.
5788 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5789 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5790 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5792 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5793 build_int_cst_type(TREE_TYPE(right
), bits
));
5795 tree overflow_result
= fold_convert_loc(this->location(),
5798 if (this->op_
== OPERATOR_RSHIFT
5799 && !this->left_
->type()->integer_type()->is_unsigned())
5801 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5802 boolean_type_node
, left
,
5803 fold_convert_loc(this->location(),
5805 integer_zero_node
));
5806 tree neg_one
= fold_build2_loc(this->location(),
5807 MINUS_EXPR
, TREE_TYPE(left
),
5808 fold_convert_loc(this->location(),
5811 fold_convert_loc(this->location(),
5814 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5815 TREE_TYPE(left
), neg
, neg_one
,
5819 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5820 compare
, ret
, overflow_result
);
5822 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5823 TREE_TYPE(ret
), eval_saved
, ret
);
5829 // Export a binary expression.
5832 Binary_expression::do_export(Export
* exp
) const
5834 exp
->write_c_string("(");
5835 this->left_
->export_expression(exp
);
5839 exp
->write_c_string(" || ");
5841 case OPERATOR_ANDAND
:
5842 exp
->write_c_string(" && ");
5845 exp
->write_c_string(" == ");
5847 case OPERATOR_NOTEQ
:
5848 exp
->write_c_string(" != ");
5851 exp
->write_c_string(" < ");
5854 exp
->write_c_string(" <= ");
5857 exp
->write_c_string(" > ");
5860 exp
->write_c_string(" >= ");
5863 exp
->write_c_string(" + ");
5865 case OPERATOR_MINUS
:
5866 exp
->write_c_string(" - ");
5869 exp
->write_c_string(" | ");
5872 exp
->write_c_string(" ^ ");
5875 exp
->write_c_string(" * ");
5878 exp
->write_c_string(" / ");
5881 exp
->write_c_string(" % ");
5883 case OPERATOR_LSHIFT
:
5884 exp
->write_c_string(" << ");
5886 case OPERATOR_RSHIFT
:
5887 exp
->write_c_string(" >> ");
5890 exp
->write_c_string(" & ");
5892 case OPERATOR_BITCLEAR
:
5893 exp
->write_c_string(" &^ ");
5898 this->right_
->export_expression(exp
);
5899 exp
->write_c_string(")");
5902 // Import a binary expression.
5905 Binary_expression::do_import(Import
* imp
)
5907 imp
->require_c_string("(");
5909 Expression
* left
= Expression::import_expression(imp
);
5912 if (imp
->match_c_string(" || "))
5917 else if (imp
->match_c_string(" && "))
5919 op
= OPERATOR_ANDAND
;
5922 else if (imp
->match_c_string(" == "))
5927 else if (imp
->match_c_string(" != "))
5929 op
= OPERATOR_NOTEQ
;
5932 else if (imp
->match_c_string(" < "))
5937 else if (imp
->match_c_string(" <= "))
5942 else if (imp
->match_c_string(" > "))
5947 else if (imp
->match_c_string(" >= "))
5952 else if (imp
->match_c_string(" + "))
5957 else if (imp
->match_c_string(" - "))
5959 op
= OPERATOR_MINUS
;
5962 else if (imp
->match_c_string(" | "))
5967 else if (imp
->match_c_string(" ^ "))
5972 else if (imp
->match_c_string(" * "))
5977 else if (imp
->match_c_string(" / "))
5982 else if (imp
->match_c_string(" % "))
5987 else if (imp
->match_c_string(" << "))
5989 op
= OPERATOR_LSHIFT
;
5992 else if (imp
->match_c_string(" >> "))
5994 op
= OPERATOR_RSHIFT
;
5997 else if (imp
->match_c_string(" & "))
6002 else if (imp
->match_c_string(" &^ "))
6004 op
= OPERATOR_BITCLEAR
;
6009 error_at(imp
->location(), "unrecognized binary operator");
6010 return Expression::make_error(imp
->location());
6013 Expression
* right
= Expression::import_expression(imp
);
6015 imp
->require_c_string(")");
6017 return Expression::make_binary(op
, left
, right
, imp
->location());
6020 // Make a binary expression.
6023 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
6024 source_location location
)
6026 return new Binary_expression(op
, left
, right
, location
);
6029 // Implement a comparison.
6032 Expression::comparison_tree(Translate_context
* context
, Operator op
,
6033 Type
* left_type
, tree left_tree
,
6034 Type
* right_type
, tree right_tree
,
6035 source_location location
)
6037 enum tree_code code
;
6043 case OPERATOR_NOTEQ
:
6062 if (left_type
->is_string_type() && right_type
->is_string_type())
6064 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
6065 static tree string_compare_decl
;
6066 left_tree
= Gogo::call_builtin(&string_compare_decl
,
6075 right_tree
= build_int_cst_type(integer_type_node
, 0);
6077 else if ((left_type
->interface_type() != NULL
6078 && right_type
->interface_type() == NULL
6079 && !right_type
->is_nil_type())
6080 || (left_type
->interface_type() == NULL
6081 && !left_type
->is_nil_type()
6082 && right_type
->interface_type() != NULL
))
6084 // Comparing an interface value to a non-interface value.
6085 if (left_type
->interface_type() == NULL
)
6087 std::swap(left_type
, right_type
);
6088 std::swap(left_tree
, right_tree
);
6091 // The right operand is not an interface. We need to take its
6092 // address if it is not a pointer.
6095 if (right_type
->points_to() != NULL
)
6097 make_tmp
= NULL_TREE
;
6100 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
6102 make_tmp
= NULL_TREE
;
6103 arg
= build_fold_addr_expr_loc(location
, right_tree
);
6104 if (DECL_P(right_tree
))
6105 TREE_ADDRESSABLE(right_tree
) = 1;
6109 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
6110 get_name(right_tree
));
6111 DECL_IGNORED_P(tmp
) = 0;
6112 DECL_INITIAL(tmp
) = right_tree
;
6113 TREE_ADDRESSABLE(tmp
) = 1;
6114 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
6115 SET_EXPR_LOCATION(make_tmp
, location
);
6116 arg
= build_fold_addr_expr_loc(location
, tmp
);
6118 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
6120 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
6122 if (left_type
->interface_type()->is_empty())
6124 static tree empty_interface_value_compare_decl
;
6125 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6127 "__go_empty_interface_value_compare",
6130 TREE_TYPE(left_tree
),
6132 TREE_TYPE(descriptor
),
6136 if (left_tree
== error_mark_node
)
6137 return error_mark_node
;
6138 // This can panic if the type is not comparable.
6139 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6143 static tree interface_value_compare_decl
;
6144 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6146 "__go_interface_value_compare",
6149 TREE_TYPE(left_tree
),
6151 TREE_TYPE(descriptor
),
6155 if (left_tree
== error_mark_node
)
6156 return error_mark_node
;
6157 // This can panic if the type is not comparable.
6158 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6160 right_tree
= build_int_cst_type(integer_type_node
, 0);
6162 if (make_tmp
!= NULL_TREE
)
6163 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6166 else if (left_type
->interface_type() != NULL
6167 && right_type
->interface_type() != NULL
)
6169 if (left_type
->interface_type()->is_empty())
6171 gcc_assert(right_type
->interface_type()->is_empty());
6172 static tree empty_interface_compare_decl
;
6173 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6175 "__go_empty_interface_compare",
6178 TREE_TYPE(left_tree
),
6180 TREE_TYPE(right_tree
),
6182 if (left_tree
== error_mark_node
)
6183 return error_mark_node
;
6184 // This can panic if the type is uncomparable.
6185 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6189 gcc_assert(!right_type
->interface_type()->is_empty());
6190 static tree interface_compare_decl
;
6191 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6193 "__go_interface_compare",
6196 TREE_TYPE(left_tree
),
6198 TREE_TYPE(right_tree
),
6200 if (left_tree
== error_mark_node
)
6201 return error_mark_node
;
6202 // This can panic if the type is uncomparable.
6203 TREE_NOTHROW(interface_compare_decl
) = 0;
6205 right_tree
= build_int_cst_type(integer_type_node
, 0);
6208 if (left_type
->is_nil_type()
6209 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6211 std::swap(left_type
, right_type
);
6212 std::swap(left_tree
, right_tree
);
6215 if (right_type
->is_nil_type())
6217 if (left_type
->array_type() != NULL
6218 && left_type
->array_type()->length() == NULL
)
6220 Array_type
* at
= left_type
->array_type();
6221 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6222 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6224 else if (left_type
->interface_type() != NULL
)
6226 // An interface is nil if the first field is nil.
6227 tree left_type_tree
= TREE_TYPE(left_tree
);
6228 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6229 tree field
= TYPE_FIELDS(left_type_tree
);
6230 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6232 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6236 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6237 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6241 if (left_tree
== error_mark_node
|| right_tree
== error_mark_node
)
6242 return error_mark_node
;
6244 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6245 if (CAN_HAVE_LOCATION_P(ret
))
6246 SET_EXPR_LOCATION(ret
, location
);
6250 // Class Bound_method_expression.
6255 Bound_method_expression::do_traverse(Traverse
* traverse
)
6257 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6258 return TRAVERSE_EXIT
;
6259 return Expression::traverse(&this->method_
, traverse
);
6262 // Return the type of a bound method expression. The type of this
6263 // object is really the type of the method with no receiver. We
6264 // should be able to get away with just returning the type of the
6268 Bound_method_expression::do_type()
6270 return this->method_
->type();
6273 // Determine the types of a method expression.
6276 Bound_method_expression::do_determine_type(const Type_context
*)
6278 this->method_
->determine_type_no_context();
6279 Type
* mtype
= this->method_
->type();
6280 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6281 if (fntype
== NULL
|| !fntype
->is_method())
6282 this->expr_
->determine_type_no_context();
6285 Type_context
subcontext(fntype
->receiver()->type(), false);
6286 this->expr_
->determine_type(&subcontext
);
6290 // Check the types of a method expression.
6293 Bound_method_expression::do_check_types(Gogo
*)
6295 Type
* type
= this->method_
->type()->deref();
6297 || type
->function_type() == NULL
6298 || !type
->function_type()->is_method())
6299 this->report_error(_("object is not a method"));
6302 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6303 Type
* etype
= (this->expr_type_
!= NULL
6305 : this->expr_
->type());
6306 etype
= etype
->deref();
6307 if (!Type::are_identical(rtype
, etype
, true, NULL
))
6308 this->report_error(_("method type does not match object type"));
6312 // Get the tree for a method expression. There is no standard tree
6313 // representation for this. The only places it may currently be used
6314 // are in a Call_expression or a Go_statement, which will take it
6315 // apart directly. So this has nothing to do at present.
6318 Bound_method_expression::do_get_tree(Translate_context
*)
6323 // Make a method expression.
6325 Bound_method_expression
*
6326 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6327 source_location location
)
6329 return new Bound_method_expression(expr
, method
, location
);
6332 // Class Builtin_call_expression. This is used for a call to a
6333 // builtin function.
6335 class Builtin_call_expression
: public Call_expression
6338 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6339 bool is_varargs
, source_location location
);
6342 // This overrides Call_expression::do_lower.
6344 do_lower(Gogo
*, Named_object
*, int);
6347 do_is_constant() const;
6350 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6353 do_float_constant_value(mpfr_t
, Type
**) const;
6356 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6362 do_determine_type(const Type_context
*);
6365 do_check_types(Gogo
*);
6370 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6371 this->args()->copy(),
6377 do_get_tree(Translate_context
*);
6380 do_export(Export
*) const;
6383 do_is_recover_call() const;
6386 do_set_recover_arg(Expression
*);
6389 // The builtin functions.
6390 enum Builtin_function_code
6394 // Predeclared builtin functions.
6411 // Builtin functions from the unsafe package.
6424 real_imag_type(Type
*);
6429 // A pointer back to the general IR structure. This avoids a global
6430 // variable, or passing it around everywhere.
6432 // The builtin function being called.
6433 Builtin_function_code code_
;
6436 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6438 Expression_list
* args
,
6440 source_location location
)
6441 : Call_expression(fn
, args
, is_varargs
, location
),
6442 gogo_(gogo
), code_(BUILTIN_INVALID
)
6444 Func_expression
* fnexp
= this->fn()->func_expression();
6445 gcc_assert(fnexp
!= NULL
);
6446 const std::string
& name(fnexp
->named_object()->name());
6447 if (name
== "append")
6448 this->code_
= BUILTIN_APPEND
;
6449 else if (name
== "cap")
6450 this->code_
= BUILTIN_CAP
;
6451 else if (name
== "close")
6452 this->code_
= BUILTIN_CLOSE
;
6453 else if (name
== "closed")
6454 this->code_
= BUILTIN_CLOSED
;
6455 else if (name
== "cmplx")
6456 this->code_
= BUILTIN_CMPLX
;
6457 else if (name
== "copy")
6458 this->code_
= BUILTIN_COPY
;
6459 else if (name
== "imag")
6460 this->code_
= BUILTIN_IMAG
;
6461 else if (name
== "len")
6462 this->code_
= BUILTIN_LEN
;
6463 else if (name
== "make")
6464 this->code_
= BUILTIN_MAKE
;
6465 else if (name
== "new")
6466 this->code_
= BUILTIN_NEW
;
6467 else if (name
== "panic")
6468 this->code_
= BUILTIN_PANIC
;
6469 else if (name
== "print")
6470 this->code_
= BUILTIN_PRINT
;
6471 else if (name
== "println")
6472 this->code_
= BUILTIN_PRINTLN
;
6473 else if (name
== "real")
6474 this->code_
= BUILTIN_REAL
;
6475 else if (name
== "recover")
6476 this->code_
= BUILTIN_RECOVER
;
6477 else if (name
== "Alignof")
6478 this->code_
= BUILTIN_ALIGNOF
;
6479 else if (name
== "Offsetof")
6480 this->code_
= BUILTIN_OFFSETOF
;
6481 else if (name
== "Sizeof")
6482 this->code_
= BUILTIN_SIZEOF
;
6487 // Return whether this is a call to recover. This is a virtual
6488 // function called from the parent class.
6491 Builtin_call_expression::do_is_recover_call() const
6493 if (this->classification() == EXPRESSION_ERROR
)
6495 return this->code_
== BUILTIN_RECOVER
;
6498 // Set the argument for a call to recover.
6501 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6503 const Expression_list
* args
= this->args();
6504 gcc_assert(args
== NULL
|| args
->empty());
6505 Expression_list
* new_args
= new Expression_list();
6506 new_args
->push_back(arg
);
6507 this->set_args(new_args
);
6510 // A traversal class which looks for a call expression.
6512 class Find_call_expression
: public Traverse
6515 Find_call_expression()
6516 : Traverse(traverse_expressions
),
6521 expression(Expression
**);
6525 { return this->found_
; }
6532 Find_call_expression::expression(Expression
** pexpr
)
6534 if ((*pexpr
)->call_expression() != NULL
)
6536 this->found_
= true;
6537 return TRAVERSE_EXIT
;
6539 return TRAVERSE_CONTINUE
;
6542 // Lower a builtin call expression. This turns new and make into
6543 // specific expressions. We also convert to a constant if we can.
6546 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6548 if (this->code_
== BUILTIN_NEW
)
6550 const Expression_list
* args
= this->args();
6551 if (args
== NULL
|| args
->size() < 1)
6552 this->report_error(_("not enough arguments"));
6553 else if (args
->size() > 1)
6554 this->report_error(_("too many arguments"));
6557 Expression
* arg
= args
->front();
6558 if (!arg
->is_type_expression())
6560 error_at(arg
->location(), "expected type");
6561 this->set_is_error();
6564 return Expression::make_allocation(arg
->type(), this->location());
6567 else if (this->code_
== BUILTIN_MAKE
)
6569 const Expression_list
* args
= this->args();
6570 if (args
== NULL
|| args
->size() < 1)
6571 this->report_error(_("not enough arguments"));
6574 Expression
* arg
= args
->front();
6575 if (!arg
->is_type_expression())
6577 error_at(arg
->location(), "expected type");
6578 this->set_is_error();
6582 Expression_list
* newargs
;
6583 if (args
->size() == 1)
6587 newargs
= new Expression_list();
6588 Expression_list::const_iterator p
= args
->begin();
6590 for (; p
!= args
->end(); ++p
)
6591 newargs
->push_back(*p
);
6593 return Expression::make_make(arg
->type(), newargs
,
6598 else if (this->is_constant())
6600 // We can only lower len and cap if there are no function calls
6601 // in the arguments. Otherwise we have to make the call.
6602 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6604 Expression
* arg
= this->one_arg();
6605 if (!arg
->is_constant())
6607 Find_call_expression find_call
;
6608 Expression::traverse(&arg
, &find_call
);
6609 if (find_call
.found())
6617 if (this->integer_constant_value(true, ival
, &type
))
6619 Expression
* ret
= Expression::make_integer(&ival
, type
,
6628 if (this->float_constant_value(rval
, &type
))
6630 Expression
* ret
= Expression::make_float(&rval
, type
,
6638 if (this->complex_constant_value(rval
, imag
, &type
))
6640 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6649 else if (this->code_
== BUILTIN_RECOVER
)
6651 if (function
!= NULL
)
6652 function
->func_value()->set_calls_recover();
6655 // Calling recover outside of a function always returns the
6656 // nil empty interface.
6657 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6658 return Expression::make_cast(eface
,
6659 Expression::make_nil(this->location()),
6663 else if (this->code_
== BUILTIN_APPEND
)
6665 // Lower the varargs.
6666 const Expression_list
* args
= this->args();
6667 if (args
== NULL
|| args
->empty())
6669 Type
* slice_type
= args
->front()->type();
6670 if (!slice_type
->is_open_array_type())
6672 error_at(args
->front()->location(), "argument 1 must be a slice");
6673 this->set_is_error();
6676 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6682 // Return the type of the real or imag functions, given the type of
6683 // the argument. We need to map complex to float, complex64 to
6684 // float32, and complex128 to float64, so it has to be done by name.
6685 // This returns NULL if it can't figure out the type.
6688 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6690 if (arg_type
== NULL
|| arg_type
->is_abstract())
6692 Named_type
* nt
= arg_type
->named_type();
6695 while (nt
->real_type()->named_type() != NULL
)
6696 nt
= nt
->real_type()->named_type();
6697 if (nt
->name() == "complex")
6698 return Type::lookup_float_type("float");
6699 else if (nt
->name() == "complex64")
6700 return Type::lookup_float_type("float32");
6701 else if (nt
->name() == "complex128")
6702 return Type::lookup_float_type("float64");
6707 // Return the type of the cmplx function, given the type of one of the
6708 // argments. Like real_imag_type, we have to map by name.
6711 Builtin_call_expression::cmplx_type(Type
* arg_type
)
6713 if (arg_type
== NULL
|| arg_type
->is_abstract())
6715 Named_type
* nt
= arg_type
->named_type();
6718 while (nt
->real_type()->named_type() != NULL
)
6719 nt
= nt
->real_type()->named_type();
6720 if (nt
->name() == "float")
6721 return Type::lookup_complex_type("complex");
6722 else if (nt
->name() == "float32")
6723 return Type::lookup_complex_type("complex64");
6724 else if (nt
->name() == "float64")
6725 return Type::lookup_complex_type("complex128");
6730 // Return a single argument, or NULL if there isn't one.
6733 Builtin_call_expression::one_arg() const
6735 const Expression_list
* args
= this->args();
6736 if (args
->size() != 1)
6738 return args
->front();
6741 // Return whether this is constant: len of a string, or len or cap of
6742 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6745 Builtin_call_expression::do_is_constant() const
6747 switch (this->code_
)
6752 Expression
* arg
= this->one_arg();
6755 Type
* arg_type
= arg
->type();
6757 if (arg_type
->points_to() != NULL
6758 && arg_type
->points_to()->array_type() != NULL
6759 && !arg_type
->points_to()->is_open_array_type())
6760 arg_type
= arg_type
->points_to();
6762 if (arg_type
->array_type() != NULL
6763 && arg_type
->array_type()->length() != NULL
)
6764 return arg_type
->array_type()->length()->is_constant();
6766 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6767 return arg
->is_constant();
6771 case BUILTIN_SIZEOF
:
6772 case BUILTIN_ALIGNOF
:
6773 return this->one_arg() != NULL
;
6775 case BUILTIN_OFFSETOF
:
6777 Expression
* arg
= this->one_arg();
6780 return arg
->field_reference_expression() != NULL
;
6785 const Expression_list
* args
= this->args();
6786 if (args
!= NULL
&& args
->size() == 2)
6787 return args
->front()->is_constant() && args
->back()->is_constant();
6794 Expression
* arg
= this->one_arg();
6795 return arg
!= NULL
&& arg
->is_constant();
6805 // Return an integer constant value if possible.
6808 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6812 if (this->code_
== BUILTIN_LEN
6813 || this->code_
== BUILTIN_CAP
)
6815 Expression
* arg
= this->one_arg();
6818 Type
* arg_type
= arg
->type();
6820 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6823 if (arg
->string_constant_value(&sval
))
6825 mpz_set_ui(val
, sval
.length());
6826 *ptype
= Type::lookup_integer_type("int");
6831 if (arg_type
->points_to() != NULL
6832 && arg_type
->points_to()->array_type() != NULL
6833 && !arg_type
->points_to()->is_open_array_type())
6834 arg_type
= arg_type
->points_to();
6836 if (arg_type
->array_type() != NULL
6837 && arg_type
->array_type()->length() != NULL
)
6839 Expression
* e
= arg_type
->array_type()->length();
6840 if (e
->integer_constant_value(iota_is_constant
, val
, ptype
))
6842 *ptype
= Type::lookup_integer_type("int");
6847 else if (this->code_
== BUILTIN_SIZEOF
6848 || this->code_
== BUILTIN_ALIGNOF
)
6850 Expression
* arg
= this->one_arg();
6853 Type
* arg_type
= arg
->type();
6854 if (arg_type
->is_error_type() || arg_type
->is_undefined())
6856 if (arg_type
->is_abstract())
6858 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
6859 unsigned long val_long
;
6860 if (this->code_
== BUILTIN_SIZEOF
)
6862 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
6863 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
6864 if (TREE_INT_CST_HIGH(type_size
) != 0)
6866 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
6867 val_long
= static_cast<unsigned long>(val_wide
);
6868 if (val_long
!= val_wide
)
6871 else if (this->code_
== BUILTIN_ALIGNOF
)
6873 if (arg
->field_reference_expression() == NULL
)
6874 val_long
= go_type_alignment(arg_type_tree
);
6877 // Calling unsafe.Alignof(s.f) returns the alignment of
6878 // the type of f when it is used as a field in a struct.
6879 val_long
= go_field_alignment(arg_type_tree
);
6884 mpz_set_ui(val
, val_long
);
6888 else if (this->code_
== BUILTIN_OFFSETOF
)
6890 Expression
* arg
= this->one_arg();
6893 Field_reference_expression
* farg
= arg
->field_reference_expression();
6896 Expression
* struct_expr
= farg
->expr();
6897 Type
* st
= struct_expr
->type();
6898 if (st
->struct_type() == NULL
)
6900 tree struct_tree
= st
->get_tree(this->gogo_
);
6901 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
6902 tree field
= TYPE_FIELDS(struct_tree
);
6903 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
6905 field
= DECL_CHAIN(field
);
6906 gcc_assert(field
!= NULL_TREE
);
6908 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
6909 if (offset_wide
< 0)
6911 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
6912 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
6914 mpz_set_ui(val
, offset_long
);
6920 // Return a floating point constant value if possible.
6923 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
6926 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
6928 Expression
* arg
= this->one_arg();
6939 if (arg
->complex_constant_value(real
, imag
, &type
))
6941 if (this->code_
== BUILTIN_REAL
)
6942 mpfr_set(val
, real
, GMP_RNDN
);
6944 mpfr_set(val
, imag
, GMP_RNDN
);
6945 *ptype
= Builtin_call_expression::real_imag_type(type
);
6957 // Return a complex constant value if possible.
6960 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
6963 if (this->code_
== BUILTIN_CMPLX
)
6965 const Expression_list
* args
= this->args();
6966 if (args
== NULL
|| args
->size() != 2)
6972 if (!args
->front()->float_constant_value(r
, &rtype
))
6983 if (args
->back()->float_constant_value(i
, &itype
)
6984 && Type::are_identical(rtype
, itype
, false, NULL
))
6986 mpfr_set(real
, r
, GMP_RNDN
);
6987 mpfr_set(imag
, i
, GMP_RNDN
);
6988 *ptype
= Builtin_call_expression::cmplx_type(rtype
);
7004 Builtin_call_expression::do_type()
7006 switch (this->code_
)
7008 case BUILTIN_INVALID
:
7015 const Expression_list
* args
= this->args();
7016 if (args
== NULL
|| args
->empty())
7017 return Type::make_error_type();
7018 return Type::make_pointer_type(args
->front()->type());
7024 case BUILTIN_ALIGNOF
:
7025 case BUILTIN_OFFSETOF
:
7026 case BUILTIN_SIZEOF
:
7027 return Type::lookup_integer_type("int");
7032 case BUILTIN_PRINTLN
:
7033 return Type::make_void_type();
7035 case BUILTIN_CLOSED
:
7036 return Type::lookup_bool_type();
7038 case BUILTIN_RECOVER
:
7039 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7041 case BUILTIN_APPEND
:
7043 const Expression_list
* args
= this->args();
7044 if (args
== NULL
|| args
->empty())
7045 return Type::make_error_type();
7046 return args
->front()->type();
7052 Expression
* arg
= this->one_arg();
7054 return Type::make_error_type();
7055 Type
* t
= arg
->type();
7056 if (t
->is_abstract())
7057 t
= t
->make_non_abstract_type();
7058 t
= Builtin_call_expression::real_imag_type(t
);
7060 t
= Type::make_error_type();
7066 const Expression_list
* args
= this->args();
7067 if (args
== NULL
|| args
->size() != 2)
7068 return Type::make_error_type();
7069 Type
* t
= args
->front()->type();
7070 if (t
->is_abstract())
7072 t
= args
->back()->type();
7073 if (t
->is_abstract())
7074 t
= t
->make_non_abstract_type();
7076 t
= Builtin_call_expression::cmplx_type(t
);
7078 t
= Type::make_error_type();
7084 // Determine the type.
7087 Builtin_call_expression::do_determine_type(const Type_context
* context
)
7089 this->fn()->determine_type_no_context();
7091 const Expression_list
* args
= this->args();
7094 Type
* arg_type
= NULL
;
7095 switch (this->code_
)
7098 case BUILTIN_PRINTLN
:
7099 // Do not force a large integer constant to "int".
7105 arg_type
= Builtin_call_expression::cmplx_type(context
->type
);
7111 // For the cmplx function the type of one operand can
7112 // determine the type of the other, as in a binary expression.
7113 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
7114 if (args
!= NULL
&& args
->size() == 2)
7116 Type
* t1
= args
->front()->type();
7117 Type
* t2
= args
->front()->type();
7118 if (!t1
->is_abstract())
7120 else if (!t2
->is_abstract())
7134 for (Expression_list::const_iterator pa
= args
->begin();
7138 Type_context subcontext
;
7139 subcontext
.type
= arg_type
;
7143 // We want to print large constants, we so can't just
7144 // use the appropriate nonabstract type. Use uint64 for
7145 // an integer if we know it is nonnegative, otherwise
7146 // use int64 for a integer, otherwise use float64 for a
7147 // float or complex128 for a complex.
7148 Type
* want_type
= NULL
;
7149 Type
* atype
= (*pa
)->type();
7150 if (atype
->is_abstract())
7152 if (atype
->integer_type() != NULL
)
7157 if (this->integer_constant_value(true, val
, &dummy
)
7158 && mpz_sgn(val
) >= 0)
7159 want_type
= Type::lookup_integer_type("uint64");
7161 want_type
= Type::lookup_integer_type("int64");
7164 else if (atype
->float_type() != NULL
)
7165 want_type
= Type::lookup_float_type("float64");
7166 else if (atype
->complex_type() != NULL
)
7167 want_type
= Type::lookup_complex_type("complex128");
7168 else if (atype
->is_abstract_string_type())
7169 want_type
= Type::lookup_string_type();
7170 else if (atype
->is_abstract_boolean_type())
7171 want_type
= Type::lookup_bool_type();
7174 subcontext
.type
= want_type
;
7178 (*pa
)->determine_type(&subcontext
);
7183 // If there is exactly one argument, return true. Otherwise give an
7184 // error message and return false.
7187 Builtin_call_expression::check_one_arg()
7189 const Expression_list
* args
= this->args();
7190 if (args
== NULL
|| args
->size() < 1)
7192 this->report_error(_("not enough arguments"));
7195 else if (args
->size() > 1)
7197 this->report_error(_("too many arguments"));
7200 if (args
->front()->is_error_expression()
7201 || args
->front()->type()->is_error_type()
7202 || args
->front()->type()->is_undefined())
7204 this->set_is_error();
7210 // Check argument types for a builtin function.
7213 Builtin_call_expression::do_check_types(Gogo
*)
7215 switch (this->code_
)
7217 case BUILTIN_INVALID
:
7225 // The single argument may be either a string or an array or a
7226 // map or a channel, or a pointer to a closed array.
7227 if (this->check_one_arg())
7229 Type
* arg_type
= this->one_arg()->type();
7230 if (arg_type
->points_to() != NULL
7231 && arg_type
->points_to()->array_type() != NULL
7232 && !arg_type
->points_to()->is_open_array_type())
7233 arg_type
= arg_type
->points_to();
7234 if (this->code_
== BUILTIN_CAP
)
7236 if (!arg_type
->is_error_type()
7237 && arg_type
->array_type() == NULL
7238 && arg_type
->channel_type() == NULL
)
7239 this->report_error(_("argument must be array or slice "
7244 if (!arg_type
->is_error_type()
7245 && !arg_type
->is_string_type()
7246 && arg_type
->array_type() == NULL
7247 && arg_type
->map_type() == NULL
7248 && arg_type
->channel_type() == NULL
)
7249 this->report_error(_("argument must be string or "
7250 "array or slice or map or channel"));
7257 case BUILTIN_PRINTLN
:
7259 const Expression_list
* args
= this->args();
7262 if (this->code_
== BUILTIN_PRINT
)
7263 warning_at(this->location(), 0,
7264 "no arguments for builtin function %<%s%>",
7265 (this->code_
== BUILTIN_PRINT
7271 for (Expression_list::const_iterator p
= args
->begin();
7275 Type
* type
= (*p
)->type();
7276 if (type
->is_error_type()
7277 || type
->is_string_type()
7278 || type
->integer_type() != NULL
7279 || type
->float_type() != NULL
7280 || type
->complex_type() != NULL
7281 || type
->is_boolean_type()
7282 || type
->points_to() != NULL
7283 || type
->interface_type() != NULL
7284 || type
->channel_type() != NULL
7285 || type
->map_type() != NULL
7286 || type
->function_type() != NULL
7287 || type
->is_open_array_type())
7290 this->report_error(_("unsupported argument type to "
7291 "builtin function"));
7298 case BUILTIN_CLOSED
:
7299 if (this->check_one_arg())
7301 if (this->one_arg()->type()->channel_type() == NULL
)
7302 this->report_error(_("argument must be channel"));
7307 case BUILTIN_SIZEOF
:
7308 case BUILTIN_ALIGNOF
:
7309 this->check_one_arg();
7312 case BUILTIN_RECOVER
:
7313 if (this->args() != NULL
&& !this->args()->empty())
7314 this->report_error(_("too many arguments"));
7317 case BUILTIN_OFFSETOF
:
7318 if (this->check_one_arg())
7320 Expression
* arg
= this->one_arg();
7321 if (arg
->field_reference_expression() == NULL
)
7322 this->report_error(_("argument must be a field reference"));
7328 const Expression_list
* args
= this->args();
7329 if (args
== NULL
|| args
->size() < 2)
7331 this->report_error(_("not enough arguments"));
7334 else if (args
->size() > 2)
7336 this->report_error(_("too many arguments"));
7339 Type
* arg1_type
= args
->front()->type();
7340 Type
* arg2_type
= args
->back()->type();
7341 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7345 if (arg1_type
->is_open_array_type())
7346 e1
= arg1_type
->array_type()->element_type();
7349 this->report_error(_("left argument must be a slice"));
7354 if (arg2_type
->is_open_array_type())
7355 e2
= arg2_type
->array_type()->element_type();
7356 else if (arg2_type
->is_string_type())
7357 e2
= Type::lookup_integer_type("uint8");
7360 this->report_error(_("right argument must be a slice or a string"));
7364 if (!Type::are_identical(e1
, e2
, true, NULL
))
7365 this->report_error(_("element types must be the same"));
7369 case BUILTIN_APPEND
:
7371 const Expression_list
* args
= this->args();
7372 if (args
== NULL
|| args
->empty())
7374 this->report_error(_("not enough arguments"));
7377 /* Lowering varargs should have left us with 2 arguments. */
7378 gcc_assert(args
->size() == 2);
7380 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7384 this->report_error(_("arguments 1 and 2 have different types"));
7387 error_at(this->location(),
7388 "arguments 1 and 2 have different types (%s)",
7390 this->set_is_error();
7398 if (this->check_one_arg())
7400 if (this->one_arg()->type()->complex_type() == NULL
)
7401 this->report_error(_("argument must have complex type"));
7407 const Expression_list
* args
= this->args();
7408 if (args
== NULL
|| args
->size() < 2)
7409 this->report_error(_("not enough arguments"));
7410 else if (args
->size() > 2)
7411 this->report_error(_("too many arguments"));
7412 else if (args
->front()->is_error_expression()
7413 || args
->front()->type()->is_error_type()
7414 || args
->back()->is_error_expression()
7415 || args
->back()->type()->is_error_type())
7416 this->set_is_error();
7417 else if (!Type::are_identical(args
->front()->type(),
7418 args
->back()->type(), true, NULL
))
7419 this->report_error(_("cmplx arguments must have identical types"));
7420 else if (args
->front()->type()->float_type() == NULL
)
7421 this->report_error(_("cmplx arguments must have "
7422 "floating-point type"));
7431 // Return the tree for a builtin function.
7434 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7436 Gogo
* gogo
= context
->gogo();
7437 source_location location
= this->location();
7438 switch (this->code_
)
7440 case BUILTIN_INVALID
:
7448 const Expression_list
* args
= this->args();
7449 gcc_assert(args
!= NULL
&& args
->size() == 1);
7450 Expression
* arg
= *args
->begin();
7451 Type
* arg_type
= arg
->type();
7452 tree arg_tree
= arg
->get_tree(context
);
7453 if (arg_tree
== error_mark_node
)
7454 return error_mark_node
;
7456 if (arg_type
->points_to() != NULL
)
7458 arg_type
= arg_type
->points_to();
7459 gcc_assert(arg_type
->array_type() != NULL
7460 && !arg_type
->is_open_array_type());
7461 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7462 arg_tree
= build_fold_indirect_ref(arg_tree
);
7466 if (this->code_
== BUILTIN_LEN
)
7468 if (arg_type
->is_string_type())
7469 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7470 else if (arg_type
->array_type() != NULL
)
7471 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7472 else if (arg_type
->map_type() != NULL
)
7474 static tree map_len_fndecl
;
7475 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7480 arg_type
->get_tree(gogo
),
7483 else if (arg_type
->channel_type() != NULL
)
7485 static tree chan_len_fndecl
;
7486 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7491 arg_type
->get_tree(gogo
),
7499 if (arg_type
->array_type() != NULL
)
7500 val_tree
= arg_type
->array_type()->capacity_tree(gogo
, arg_tree
);
7501 else if (arg_type
->channel_type() != NULL
)
7503 static tree chan_cap_fndecl
;
7504 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7509 arg_type
->get_tree(gogo
),
7516 if (val_tree
== error_mark_node
)
7517 return error_mark_node
;
7519 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7520 if (type_tree
== TREE_TYPE(val_tree
))
7523 return fold(convert_to_integer(type_tree
, val_tree
));
7527 case BUILTIN_PRINTLN
:
7529 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7530 tree stmt_list
= NULL_TREE
;
7532 const Expression_list
* call_args
= this->args();
7533 if (call_args
!= NULL
)
7535 for (Expression_list::const_iterator p
= call_args
->begin();
7536 p
!= call_args
->end();
7539 if (is_ln
&& p
!= call_args
->begin())
7541 static tree print_space_fndecl
;
7542 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7547 if (call
== error_mark_node
)
7548 return error_mark_node
;
7549 append_to_statement_list(call
, &stmt_list
);
7552 Type
* type
= (*p
)->type();
7554 tree arg
= (*p
)->get_tree(context
);
7555 if (arg
== error_mark_node
)
7556 return error_mark_node
;
7560 if (type
->is_string_type())
7562 static tree print_string_fndecl
;
7563 pfndecl
= &print_string_fndecl
;
7564 fnname
= "__go_print_string";
7566 else if (type
->integer_type() != NULL
7567 && type
->integer_type()->is_unsigned())
7569 static tree print_uint64_fndecl
;
7570 pfndecl
= &print_uint64_fndecl
;
7571 fnname
= "__go_print_uint64";
7572 Type
* itype
= Type::lookup_integer_type("uint64");
7573 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7576 else if (type
->integer_type() != NULL
)
7578 static tree print_int64_fndecl
;
7579 pfndecl
= &print_int64_fndecl
;
7580 fnname
= "__go_print_int64";
7581 Type
* itype
= Type::lookup_integer_type("int64");
7582 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7585 else if (type
->float_type() != NULL
)
7587 static tree print_double_fndecl
;
7588 pfndecl
= &print_double_fndecl
;
7589 fnname
= "__go_print_double";
7590 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7592 else if (type
->complex_type() != NULL
)
7594 static tree print_complex_fndecl
;
7595 pfndecl
= &print_complex_fndecl
;
7596 fnname
= "__go_print_complex";
7597 arg
= fold_convert_loc(location
, complex_double_type_node
,
7600 else if (type
->is_boolean_type())
7602 static tree print_bool_fndecl
;
7603 pfndecl
= &print_bool_fndecl
;
7604 fnname
= "__go_print_bool";
7606 else if (type
->points_to() != NULL
7607 || type
->channel_type() != NULL
7608 || type
->map_type() != NULL
7609 || type
->function_type() != NULL
)
7611 static tree print_pointer_fndecl
;
7612 pfndecl
= &print_pointer_fndecl
;
7613 fnname
= "__go_print_pointer";
7614 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7616 else if (type
->interface_type() != NULL
)
7618 if (type
->interface_type()->is_empty())
7620 static tree print_empty_interface_fndecl
;
7621 pfndecl
= &print_empty_interface_fndecl
;
7622 fnname
= "__go_print_empty_interface";
7626 static tree print_interface_fndecl
;
7627 pfndecl
= &print_interface_fndecl
;
7628 fnname
= "__go_print_interface";
7631 else if (type
->is_open_array_type())
7633 static tree print_slice_fndecl
;
7634 pfndecl
= &print_slice_fndecl
;
7635 fnname
= "__go_print_slice";
7640 tree call
= Gogo::call_builtin(pfndecl
,
7647 if (call
== error_mark_node
)
7648 return error_mark_node
;
7649 append_to_statement_list(call
, &stmt_list
);
7655 static tree print_nl_fndecl
;
7656 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7661 if (call
== error_mark_node
)
7662 return error_mark_node
;
7663 append_to_statement_list(call
, &stmt_list
);
7671 const Expression_list
* args
= this->args();
7672 gcc_assert(args
!= NULL
&& args
->size() == 1);
7673 Expression
* arg
= args
->front();
7674 tree arg_tree
= arg
->get_tree(context
);
7675 if (arg_tree
== error_mark_node
)
7676 return error_mark_node
;
7677 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7678 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7680 arg_tree
, location
);
7681 static tree panic_fndecl
;
7682 tree call
= Gogo::call_builtin(&panic_fndecl
,
7687 TREE_TYPE(arg_tree
),
7689 if (call
== error_mark_node
)
7690 return error_mark_node
;
7691 // This function will throw an exception.
7692 TREE_NOTHROW(panic_fndecl
) = 0;
7693 // This function will not return.
7694 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7698 case BUILTIN_RECOVER
:
7700 // The argument is set when building recover thunks. It's a
7701 // boolean value which is true if we can recover a value now.
7702 const Expression_list
* args
= this->args();
7703 gcc_assert(args
!= NULL
&& args
->size() == 1);
7704 Expression
* arg
= args
->front();
7705 tree arg_tree
= arg
->get_tree(context
);
7706 if (arg_tree
== error_mark_node
)
7707 return error_mark_node
;
7709 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7710 tree empty_tree
= empty
->get_tree(context
->gogo());
7712 Type
* nil_type
= Type::make_nil_type();
7713 Expression
* nil
= Expression::make_nil(location
);
7714 tree nil_tree
= nil
->get_tree(context
);
7715 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7721 // We need to handle a deferred call to recover specially,
7722 // because it changes whether it can recover a panic or not.
7723 // See test7 in test/recover1.go.
7725 if (this->is_deferred())
7727 static tree deferred_recover_fndecl
;
7728 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7730 "__go_deferred_recover",
7736 static tree recover_fndecl
;
7737 call
= Gogo::call_builtin(&recover_fndecl
,
7743 if (call
== error_mark_node
)
7744 return error_mark_node
;
7745 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7746 call
, empty_nil_tree
);
7750 case BUILTIN_CLOSED
:
7752 const Expression_list
* args
= this->args();
7753 gcc_assert(args
!= NULL
&& args
->size() == 1);
7754 Expression
* arg
= args
->front();
7755 tree arg_tree
= arg
->get_tree(context
);
7756 if (arg_tree
== error_mark_node
)
7757 return error_mark_node
;
7758 if (this->code_
== BUILTIN_CLOSE
)
7760 static tree close_fndecl
;
7761 return Gogo::call_builtin(&close_fndecl
,
7763 "__go_builtin_close",
7766 TREE_TYPE(arg_tree
),
7771 static tree closed_fndecl
;
7772 return Gogo::call_builtin(&closed_fndecl
,
7774 "__go_builtin_closed",
7777 TREE_TYPE(arg_tree
),
7782 case BUILTIN_SIZEOF
:
7783 case BUILTIN_OFFSETOF
:
7784 case BUILTIN_ALIGNOF
:
7789 bool b
= this->integer_constant_value(true, val
, &dummy
);
7791 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7792 tree ret
= Expression::integer_constant_tree(val
, type
);
7799 const Expression_list
* args
= this->args();
7800 gcc_assert(args
!= NULL
&& args
->size() == 2);
7801 Expression
* arg1
= args
->front();
7802 Expression
* arg2
= args
->back();
7804 tree arg1_tree
= arg1
->get_tree(context
);
7805 tree arg2_tree
= arg2
->get_tree(context
);
7806 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7807 return error_mark_node
;
7809 Type
* arg1_type
= arg1
->type();
7810 Array_type
* at
= arg1_type
->array_type();
7811 arg1_tree
= save_expr(arg1_tree
);
7812 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
7813 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
7814 if (arg1_val
== error_mark_node
|| arg1_len
== error_mark_node
)
7815 return error_mark_node
;
7817 Type
* arg2_type
= arg2
->type();
7820 if (arg2_type
->is_open_array_type())
7822 at
= arg2_type
->array_type();
7823 arg2_tree
= save_expr(arg2_tree
);
7824 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7825 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7829 arg2_tree
= save_expr(arg2_tree
);
7830 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
7831 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
7833 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7834 return error_mark_node
;
7836 arg1_len
= save_expr(arg1_len
);
7837 arg2_len
= save_expr(arg2_len
);
7838 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
7839 fold_build2_loc(location
, LT_EXPR
,
7841 arg1_len
, arg2_len
),
7842 arg1_len
, arg2_len
);
7843 len
= save_expr(len
);
7845 Type
* element_type
= at
->element_type();
7846 tree element_type_tree
= element_type
->get_tree(gogo
);
7847 if (element_type_tree
== error_mark_node
)
7848 return error_mark_node
;
7849 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7850 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
7852 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
7853 TREE_TYPE(element_size
),
7854 bytecount
, element_size
);
7855 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
7857 arg1_val
= fold_convert_loc(location
, ptr_type_node
, arg1_val
);
7858 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7860 static tree copy_fndecl
;
7861 tree call
= Gogo::call_builtin(©_fndecl
,
7872 if (call
== error_mark_node
)
7873 return error_mark_node
;
7875 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
7879 case BUILTIN_APPEND
:
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 Array_type
* at
= arg1
->type()->array_type();
7887 Type
* element_type
= at
->element_type();
7889 tree arg1_tree
= arg1
->get_tree(context
);
7890 tree arg2_tree
= arg2
->get_tree(context
);
7891 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7892 return error_mark_node
;
7894 Array_type
* at2
= arg2
->type()->array_type();
7895 arg2_tree
= save_expr(arg2_tree
);
7896 tree arg2_val
= at2
->value_pointer_tree(gogo
, arg2_tree
);
7897 tree arg2_len
= at2
->length_tree(gogo
, arg2_tree
);
7898 if (arg2_val
== error_mark_node
|| arg2_len
== error_mark_node
)
7899 return error_mark_node
;
7900 arg2_val
= fold_convert_loc(location
, ptr_type_node
, arg2_val
);
7901 arg2_len
= fold_convert_loc(location
, size_type_node
, arg2_len
);
7903 tree element_type_tree
= element_type
->get_tree(gogo
);
7904 if (element_type_tree
== error_mark_node
)
7905 return error_mark_node
;
7906 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7907 element_size
= fold_convert_loc(location
, size_type_node
,
7910 // We rebuild the decl each time since the slice types may
7912 tree append_fndecl
= NULL_TREE
;
7913 return Gogo::call_builtin(&append_fndecl
,
7917 TREE_TYPE(arg1_tree
),
7918 TREE_TYPE(arg1_tree
),
7931 const Expression_list
* args
= this->args();
7932 gcc_assert(args
!= NULL
&& args
->size() == 1);
7933 Expression
* arg
= args
->front();
7934 tree arg_tree
= arg
->get_tree(context
);
7935 if (arg_tree
== error_mark_node
)
7936 return error_mark_node
;
7937 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
7938 if (this->code_
== BUILTIN_REAL
)
7939 return fold_build1_loc(location
, REALPART_EXPR
,
7940 TREE_TYPE(TREE_TYPE(arg_tree
)),
7943 return fold_build1_loc(location
, IMAGPART_EXPR
,
7944 TREE_TYPE(TREE_TYPE(arg_tree
)),
7950 const Expression_list
* args
= this->args();
7951 gcc_assert(args
!= NULL
&& args
->size() == 2);
7952 tree r
= args
->front()->get_tree(context
);
7953 tree i
= args
->back()->get_tree(context
);
7954 if (r
== error_mark_node
|| i
== error_mark_node
)
7955 return error_mark_node
;
7956 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
7957 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
7958 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
7959 return fold_build2_loc(location
, COMPLEX_EXPR
,
7960 build_complex_type(TREE_TYPE(r
)),
7969 // We have to support exporting a builtin call expression, because
7970 // code can set a constant to the result of a builtin expression.
7973 Builtin_call_expression::do_export(Export
* exp
) const
7980 if (this->integer_constant_value(true, val
, &dummy
))
7982 Integer_expression::export_integer(exp
, val
);
7991 if (this->float_constant_value(fval
, &dummy
))
7993 Float_expression::export_float(exp
, fval
);
8005 if (this->complex_constant_value(real
, imag
, &dummy
))
8007 Complex_expression::export_complex(exp
, real
, imag
);
8016 error_at(this->location(), "value is not constant");
8020 // A trailing space lets us reliably identify the end of the number.
8021 exp
->write_c_string(" ");
8024 // Class Call_expression.
8029 Call_expression::do_traverse(Traverse
* traverse
)
8031 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
8032 return TRAVERSE_EXIT
;
8033 if (this->args_
!= NULL
)
8035 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
8036 return TRAVERSE_EXIT
;
8038 return TRAVERSE_CONTINUE
;
8041 // Lower a call statement.
8044 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
8046 // A type case can look like a function call.
8047 if (this->fn_
->is_type_expression()
8048 && this->args_
!= NULL
8049 && this->args_
->size() == 1)
8050 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
8053 // Recognize a call to a builtin function.
8054 Func_expression
* fne
= this->fn_
->func_expression();
8056 && fne
->named_object()->is_function_declaration()
8057 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
8058 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
8059 this->is_varargs_
, this->location());
8061 // Handle an argument which is a call to a function which returns
8062 // multiple results.
8063 if (this->args_
!= NULL
8064 && this->args_
->size() == 1
8065 && this->args_
->front()->call_expression() != NULL
8066 && this->fn_
->type()->function_type() != NULL
)
8068 Function_type
* fntype
= this->fn_
->type()->function_type();
8069 size_t rc
= this->args_
->front()->call_expression()->result_count();
8071 && fntype
->parameters() != NULL
8072 && (fntype
->parameters()->size() == rc
8073 || (fntype
->is_varargs()
8074 && fntype
->parameters()->size() - 1 <= rc
)))
8076 Call_expression
* call
= this->args_
->front()->call_expression();
8077 Expression_list
* args
= new Expression_list
;
8078 for (size_t i
= 0; i
< rc
; ++i
)
8079 args
->push_back(Expression::make_call_result(call
, i
));
8080 // We can't return a new call expression here, because this
8081 // one may be referenced by Call_result expressions. FIXME.
8087 // Handle a call to a varargs function by packaging up the extra
8089 if (this->fn_
->type()->function_type() != NULL
8090 && this->fn_
->type()->function_type()->is_varargs())
8092 Function_type
* fntype
= this->fn_
->type()->function_type();
8093 const Typed_identifier_list
* parameters
= fntype
->parameters();
8094 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
8095 Type
* varargs_type
= parameters
->back().type();
8096 return this->lower_varargs(gogo
, function
, varargs_type
,
8097 parameters
->size());
8103 // Lower a call to a varargs function. FUNCTION is the function in
8104 // which the call occurs--it's not the function we are calling.
8105 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8106 // PARAM_COUNT is the number of parameters of the function we are
8107 // calling; the last of these parameters will be the varargs
8111 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
8112 Type
* varargs_type
, size_t param_count
)
8114 if (this->varargs_are_lowered_
)
8117 source_location loc
= this->location();
8119 gcc_assert(param_count
> 0);
8120 gcc_assert(varargs_type
->is_open_array_type());
8122 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
8123 if (arg_count
< param_count
- 1)
8125 // Not enough arguments; will be caught in check_types.
8129 Expression_list
* old_args
= this->args_
;
8130 Expression_list
* new_args
= new Expression_list();
8131 bool push_empty_arg
= false;
8132 if (old_args
== NULL
|| old_args
->empty())
8134 gcc_assert(param_count
== 1);
8135 push_empty_arg
= true;
8139 Expression_list::const_iterator pa
;
8141 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
8143 if (static_cast<size_t>(i
) == param_count
)
8145 new_args
->push_back(*pa
);
8148 // We have reached the varargs parameter.
8150 bool issued_error
= false;
8151 if (pa
== old_args
->end())
8152 push_empty_arg
= true;
8153 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
8154 new_args
->push_back(*pa
);
8155 else if (this->is_varargs_
)
8157 this->report_error(_("too many arguments"));
8160 else if (pa
+ 1 == old_args
->end()
8161 && this->is_compatible_varargs_argument(function
, *pa
,
8164 new_args
->push_back(*pa
);
8167 Type
* element_type
= varargs_type
->array_type()->element_type();
8168 Expression_list
* vals
= new Expression_list
;
8169 for (; pa
!= old_args
->end(); ++pa
, ++i
)
8171 // Check types here so that we get a better message.
8172 Type
* patype
= (*pa
)->type();
8173 source_location paloc
= (*pa
)->location();
8174 if (!this->check_argument_type(i
, element_type
, patype
,
8175 paloc
, issued_error
))
8177 vals
->push_back(*pa
);
8180 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
8181 new_args
->push_back(val
);
8186 new_args
->push_back(Expression::make_nil(loc
));
8188 // We can't return a new call expression here, because this one may
8189 // be referenced by Call_result expressions. FIXME.
8190 if (old_args
!= NULL
)
8192 this->args_
= new_args
;
8193 this->varargs_are_lowered_
= true;
8195 // Lower all the new subexpressions.
8196 Expression
* ret
= this;
8197 gogo
->lower_expression(function
, &ret
);
8198 gcc_assert(ret
== this);
8202 // Return true if ARG is a varargs argment which should be passed to
8203 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8204 // will be the last argument passed in the call, and PARAM_TYPE will
8205 // be the type of the last parameter of the varargs function being
8209 Call_expression::is_compatible_varargs_argument(Named_object
* function
,
8214 *issued_error
= false;
8216 Type
* var_type
= NULL
;
8218 // The simple case is passing the varargs parameter of the caller.
8219 Var_expression
* ve
= arg
->var_expression();
8220 if (ve
!= NULL
&& ve
->named_object()->is_variable())
8222 Variable
* var
= ve
->named_object()->var_value();
8223 if (var
->is_varargs_parameter())
8224 var_type
= var
->type();
8227 // The complex case is passing the varargs parameter of some
8228 // enclosing function. This will look like passing down *c.f where
8229 // c is the closure variable and f is a field in the closure.
8230 if (function
!= NULL
8231 && function
->func_value()->needs_closure()
8232 && arg
->classification() == EXPRESSION_UNARY
)
8234 Unary_expression
* ue
= static_cast<Unary_expression
*>(arg
);
8235 if (ue
->op() == OPERATOR_MULT
)
8237 Field_reference_expression
* fre
=
8238 ue
->operand()->deref()->field_reference_expression();
8241 Var_expression
* ve
= fre
->expr()->deref()->var_expression();
8244 Named_object
* no
= ve
->named_object();
8245 Function
* f
= function
->func_value();
8246 if (no
== f
->closure_var())
8248 // At this point we know that this indeed a
8249 // reference to some enclosing variable. Now we
8250 // need to figure out whether that variable is a
8251 // varargs parameter.
8252 Named_object
* enclosing
=
8253 f
->enclosing_var(fre
->field_index());
8254 Variable
* var
= enclosing
->var_value();
8255 if (var
->is_varargs_parameter())
8256 var_type
= var
->type();
8263 if (var_type
== NULL
)
8266 // We only match if the parameter is the same, with an identical
8268 Array_type
* var_at
= var_type
->array_type();
8269 gcc_assert(var_at
!= NULL
);
8270 Array_type
* param_at
= param_type
->array_type();
8271 if (param_at
!= NULL
8272 && Type::are_identical(var_at
->element_type(),
8273 param_at
->element_type(), true, NULL
))
8275 error_at(arg
->location(), "... mismatch: passing ...T as ...");
8276 *issued_error
= true;
8280 // Get the function type. Returns NULL if we don't know the type. If
8281 // this returns NULL, and if_ERROR is true, issues an error.
8284 Call_expression::get_function_type() const
8286 return this->fn_
->type()->function_type();
8289 // Return the number of values which this call will return.
8292 Call_expression::result_count() const
8294 const Function_type
* fntype
= this->get_function_type();
8297 if (fntype
->results() == NULL
)
8299 return fntype
->results()->size();
8302 // Return whether this is a call to the predeclared function recover.
8305 Call_expression::is_recover_call() const
8307 return this->do_is_recover_call();
8310 // Set the argument to the recover function.
8313 Call_expression::set_recover_arg(Expression
* arg
)
8315 this->do_set_recover_arg(arg
);
8318 // Virtual functions also implemented by Builtin_call_expression.
8321 Call_expression::do_is_recover_call() const
8327 Call_expression::do_set_recover_arg(Expression
*)
8335 Call_expression::do_type()
8337 if (this->type_
!= NULL
)
8341 Function_type
* fntype
= this->get_function_type();
8343 return Type::make_error_type();
8345 const Typed_identifier_list
* results
= fntype
->results();
8346 if (results
== NULL
)
8347 ret
= Type::make_void_type();
8348 else if (results
->size() == 1)
8349 ret
= results
->begin()->type();
8351 ret
= Type::make_call_multiple_result_type(this);
8358 // Determine types for a call expression. We can use the function
8359 // parameter types to set the types of the arguments.
8362 Call_expression::do_determine_type(const Type_context
*)
8364 this->fn_
->determine_type_no_context();
8365 Function_type
* fntype
= this->get_function_type();
8366 const Typed_identifier_list
* parameters
= NULL
;
8368 parameters
= fntype
->parameters();
8369 if (this->args_
!= NULL
)
8371 Typed_identifier_list::const_iterator pt
;
8372 if (parameters
!= NULL
)
8373 pt
= parameters
->begin();
8374 for (Expression_list::const_iterator pa
= this->args_
->begin();
8375 pa
!= this->args_
->end();
8378 if (parameters
!= NULL
&& pt
!= parameters
->end())
8380 Type_context
subcontext(pt
->type(), false);
8381 (*pa
)->determine_type(&subcontext
);
8385 (*pa
)->determine_type_no_context();
8390 // Check types for parameter I.
8393 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8394 const Type
* argument_type
,
8395 source_location argument_location
,
8399 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8404 error_at(argument_location
, "argument %d has incompatible type", i
);
8406 error_at(argument_location
,
8407 "argument %d has incompatible type (%s)",
8410 this->set_is_error();
8419 Call_expression::do_check_types(Gogo
*)
8421 Function_type
* fntype
= this->get_function_type();
8424 if (!this->fn_
->type()->is_error_type())
8425 this->report_error(_("expected function"));
8429 if (fntype
->is_method())
8431 // We don't support pointers to methods, so the function has to
8432 // be a bound method expression.
8433 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8436 this->report_error(_("method call without object"));
8439 Type
* first_arg_type
= bme
->first_argument()->type();
8440 if (first_arg_type
->points_to() == NULL
)
8442 // When passing a value, we need to check that we are
8443 // permitted to copy it.
8445 if (!Type::are_assignable(fntype
->receiver()->type(),
8446 first_arg_type
, &reason
))
8449 this->report_error(_("incompatible type for receiver"));
8452 error_at(this->location(),
8453 "incompatible type for receiver (%s)",
8455 this->set_is_error();
8461 // Note that varargs was handled by the lower_varargs() method, so
8462 // we don't have to worry about it here.
8464 const Typed_identifier_list
* parameters
= fntype
->parameters();
8465 if (this->args_
== NULL
)
8467 if (parameters
!= NULL
&& !parameters
->empty())
8468 this->report_error(_("not enough arguments"));
8470 else if (parameters
== NULL
)
8471 this->report_error(_("too many arguments"));
8475 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8476 for (Expression_list::const_iterator pa
= this->args_
->begin();
8477 pa
!= this->args_
->end();
8480 if (pt
== parameters
->end())
8482 this->report_error(_("too many arguments"));
8485 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8486 (*pa
)->location(), false);
8488 if (pt
!= parameters
->end())
8489 this->report_error(_("not enough arguments"));
8493 // Return whether we have to use a temporary variable to ensure that
8494 // we evaluate this call expression in order. If the call returns no
8495 // results then it will inevitably be executed last. If the call
8496 // returns more than one result then it will be used with Call_result
8497 // expressions. So we only have to use a temporary variable if the
8498 // call returns exactly one result.
8501 Call_expression::do_must_eval_in_order() const
8503 return this->result_count() == 1;
8506 // Get the function and the first argument to use when calling a bound
8510 Call_expression::bound_method_function(Translate_context
* context
,
8511 Bound_method_expression
* bound_method
,
8512 tree
* first_arg_ptr
)
8514 Expression
* first_argument
= bound_method
->first_argument();
8515 tree first_arg
= first_argument
->get_tree(context
);
8516 if (first_arg
== error_mark_node
)
8517 return error_mark_node
;
8519 // We always pass a pointer to the first argument when calling a
8521 if (first_argument
->type()->points_to() == NULL
)
8523 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8524 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8525 || DECL_P(first_arg
)
8526 || TREE_CODE(first_arg
) == INDIRECT_REF
8527 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8529 first_arg
= build_fold_addr_expr(first_arg
);
8530 if (DECL_P(first_arg
))
8531 TREE_ADDRESSABLE(first_arg
) = 1;
8535 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8536 get_name(first_arg
));
8537 DECL_IGNORED_P(tmp
) = 0;
8538 DECL_INITIAL(tmp
) = first_arg
;
8539 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8540 build1(DECL_EXPR
, void_type_node
, tmp
),
8541 build_fold_addr_expr(tmp
));
8542 TREE_ADDRESSABLE(tmp
) = 1;
8544 if (first_arg
== error_mark_node
)
8545 return error_mark_node
;
8548 Type
* fatype
= bound_method
->first_argument_type();
8551 if (fatype
->points_to() == NULL
)
8552 fatype
= Type::make_pointer_type(fatype
);
8553 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8554 if (first_arg
== error_mark_node
8555 || TREE_TYPE(first_arg
) == error_mark_node
)
8556 return error_mark_node
;
8559 *first_arg_ptr
= first_arg
;
8561 return bound_method
->method()->get_tree(context
);
8564 // Get the function and the first argument to use when calling an
8565 // interface method.
8568 Call_expression::interface_method_function(
8569 Translate_context
* context
,
8570 Interface_field_reference_expression
* interface_method
,
8571 tree
* first_arg_ptr
)
8573 tree expr
= interface_method
->expr()->get_tree(context
);
8574 if (expr
== error_mark_node
)
8575 return error_mark_node
;
8576 expr
= save_expr(expr
);
8577 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8578 if (first_arg
== error_mark_node
)
8579 return error_mark_node
;
8580 *first_arg_ptr
= first_arg
;
8581 return interface_method
->get_function_tree(context
, expr
);
8584 // Build the call expression.
8587 Call_expression::do_get_tree(Translate_context
* context
)
8589 if (this->tree_
!= NULL_TREE
)
8592 Function_type
* fntype
= this->get_function_type();
8594 return error_mark_node
;
8596 if (this->fn_
->is_error_expression())
8597 return error_mark_node
;
8599 Gogo
* gogo
= context
->gogo();
8600 source_location location
= this->location();
8602 Func_expression
* func
= this->fn_
->func_expression();
8603 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8604 Interface_field_reference_expression
* interface_method
=
8605 this->fn_
->interface_field_reference_expression();
8606 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8607 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8608 gcc_assert(!fntype
->is_method() || is_method
);
8612 if (this->args_
== NULL
|| this->args_
->empty())
8614 nargs
= is_method
? 1 : 0;
8615 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8619 const Typed_identifier_list
* params
= fntype
->parameters();
8620 gcc_assert(params
!= NULL
);
8622 nargs
= this->args_
->size();
8623 int i
= is_method
? 1 : 0;
8625 args
= new tree
[nargs
];
8627 Typed_identifier_list::const_iterator pp
= params
->begin();
8628 Expression_list::const_iterator pe
;
8629 for (pe
= this->args_
->begin();
8630 pe
!= this->args_
->end();
8633 gcc_assert(pp
!= params
->end());
8634 tree arg_val
= (*pe
)->get_tree(context
);
8635 args
[i
] = Expression::convert_for_assignment(context
,
8640 if (args
[i
] == error_mark_node
)
8641 return error_mark_node
;
8643 gcc_assert(pp
== params
->end());
8644 gcc_assert(i
== nargs
);
8647 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8648 if (rettype
== error_mark_node
)
8649 return error_mark_node
;
8653 fn
= func
->get_tree_without_closure(gogo
);
8654 else if (!is_method
)
8655 fn
= this->fn_
->get_tree(context
);
8656 else if (bound_method
!= NULL
)
8657 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8658 else if (interface_method
!= NULL
)
8659 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8663 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8664 return error_mark_node
;
8666 // This is to support builtin math functions when using 80387 math.
8668 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8669 fndecl
= TREE_OPERAND(fndecl
, 0);
8670 tree excess_type
= NULL_TREE
;
8672 && DECL_IS_BUILTIN(fndecl
)
8673 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8675 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8676 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8677 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8678 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8680 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8681 if (excess_type
!= NULL_TREE
)
8683 tree excess_fndecl
= mathfn_built_in(excess_type
,
8684 DECL_FUNCTION_CODE(fndecl
));
8685 if (excess_fndecl
== NULL_TREE
)
8686 excess_type
= NULL_TREE
;
8689 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8690 for (int i
= 0; i
< nargs
; ++i
)
8691 args
[i
] = ::convert(excess_type
, args
[i
]);
8696 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8700 SET_EXPR_LOCATION(ret
, location
);
8704 tree closure_tree
= func
->closure()->get_tree(context
);
8705 if (closure_tree
!= error_mark_node
)
8706 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8709 // If this is a recursive function type which returns itself, as in
8711 // we have used ptr_type_node for the return type. Add a cast here
8712 // to the correct type.
8713 if (TREE_TYPE(ret
) == ptr_type_node
)
8715 tree t
= this->type()->get_tree(gogo
);
8716 ret
= fold_convert_loc(location
, t
, ret
);
8719 if (excess_type
!= NULL_TREE
)
8721 // Calling convert here can undo our excess precision change.
8722 // That may or may not be a bug in convert_to_real.
8723 ret
= build1(NOP_EXPR
, rettype
, ret
);
8726 // If there is more than one result, we will refer to the call
8728 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8729 ret
= save_expr(ret
);
8736 // Make a call expression.
8739 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8740 source_location location
)
8742 return new Call_expression(fn
, args
, is_varargs
, location
);
8745 // A single result from a call which returns multiple results.
8747 class Call_result_expression
: public Expression
8750 Call_result_expression(Call_expression
* call
, unsigned int index
)
8751 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8752 call_(call
), index_(index
)
8757 do_traverse(Traverse
*);
8763 do_determine_type(const Type_context
*);
8766 do_check_types(Gogo
*);
8771 return new Call_result_expression(this->call_
->call_expression(),
8776 do_must_eval_in_order() const
8780 do_get_tree(Translate_context
*);
8783 // The underlying call expression.
8785 // Which result we want.
8786 unsigned int index_
;
8789 // Traverse a call result.
8792 Call_result_expression::do_traverse(Traverse
* traverse
)
8794 if (traverse
->remember_expression(this->call_
))
8796 // We have already traversed the call expression.
8797 return TRAVERSE_CONTINUE
;
8799 return Expression::traverse(&this->call_
, traverse
);
8805 Call_result_expression::do_type()
8807 if (this->classification() == EXPRESSION_ERROR
)
8808 return Type::make_error_type();
8810 // THIS->CALL_ can be replaced with a temporary reference due to
8811 // Call_expression::do_must_eval_in_order when there is an error.
8812 Call_expression
* ce
= this->call_
->call_expression();
8814 return Type::make_error_type();
8815 Function_type
* fntype
= ce
->get_function_type();
8817 return Type::make_error_type();
8818 const Typed_identifier_list
* results
= fntype
->results();
8819 if (results
== NULL
)
8821 this->report_error(_("number of results does not match "
8822 "number of values"));
8823 return Type::make_error_type();
8825 Typed_identifier_list::const_iterator pr
= results
->begin();
8826 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8828 if (pr
== results
->end())
8832 if (pr
== results
->end())
8834 this->report_error(_("number of results does not match "
8835 "number of values"));
8836 return Type::make_error_type();
8841 // Check the type. Just make sure that we trigger the warning in
8845 Call_result_expression::do_check_types(Gogo
*)
8850 // Determine the type. We have nothing to do here, but the 0 result
8851 // needs to pass down to the caller.
8854 Call_result_expression::do_determine_type(const Type_context
*)
8856 if (this->index_
== 0)
8857 this->call_
->determine_type_no_context();
8863 Call_result_expression::do_get_tree(Translate_context
* context
)
8865 tree call_tree
= this->call_
->get_tree(context
);
8866 if (call_tree
== error_mark_node
)
8867 return error_mark_node
;
8868 gcc_assert(TREE_CODE(TREE_TYPE(call_tree
)) == RECORD_TYPE
);
8869 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
8870 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8872 gcc_assert(field
!= NULL_TREE
);
8873 field
= DECL_CHAIN(field
);
8875 gcc_assert(field
!= NULL_TREE
);
8876 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
8879 // Make a reference to a single result of a call which returns
8880 // multiple results.
8883 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
8885 return new Call_result_expression(call
, index
);
8888 // Class Index_expression.
8893 Index_expression::do_traverse(Traverse
* traverse
)
8895 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
8896 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
8897 || (this->end_
!= NULL
8898 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
8899 return TRAVERSE_EXIT
;
8900 return TRAVERSE_CONTINUE
;
8903 // Lower an index expression. This converts the generic index
8904 // expression into an array index, a string index, or a map index.
8907 Index_expression::do_lower(Gogo
*, Named_object
*, int)
8909 source_location location
= this->location();
8910 Expression
* left
= this->left_
;
8911 Expression
* start
= this->start_
;
8912 Expression
* end
= this->end_
;
8914 Type
* type
= left
->type();
8915 if (type
->is_error_type())
8916 return Expression::make_error(location
);
8917 else if (type
->array_type() != NULL
)
8918 return Expression::make_array_index(left
, start
, end
, location
);
8919 else if (type
->points_to() != NULL
8920 && type
->points_to()->array_type() != NULL
8921 && !type
->points_to()->is_open_array_type())
8923 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
8925 return Expression::make_array_index(deref
, start
, end
, location
);
8927 else if (type
->is_string_type())
8928 return Expression::make_string_index(left
, start
, end
, location
);
8929 else if (type
->map_type() != NULL
)
8933 error_at(location
, "invalid slice of map");
8934 return Expression::make_error(location
);
8936 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
8938 if (this->is_lvalue_
)
8939 ret
->set_is_lvalue();
8945 "attempt to index object which is not array, string, or map");
8946 return Expression::make_error(location
);
8950 // Make an index expression.
8953 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
8954 source_location location
)
8956 return new Index_expression(left
, start
, end
, location
);
8959 // An array index. This is used for both indexing and slicing.
8961 class Array_index_expression
: public Expression
8964 Array_index_expression(Expression
* array
, Expression
* start
,
8965 Expression
* end
, source_location location
)
8966 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
8967 array_(array
), start_(start
), end_(end
), type_(NULL
)
8972 do_traverse(Traverse
*);
8978 do_determine_type(const Type_context
*);
8981 do_check_types(Gogo
*);
8986 return Expression::make_array_index(this->array_
->copy(),
8987 this->start_
->copy(),
8990 : this->end_
->copy()),
8995 do_is_addressable() const;
8998 do_address_taken(bool escapes
)
8999 { this->array_
->address_taken(escapes
); }
9002 do_get_tree(Translate_context
*);
9005 // The array we are getting a value from.
9007 // The start or only index.
9009 // The end index of a slice. This may be NULL for a simple array
9010 // index, or it may be a nil expression for the length of the array.
9012 // The type of the expression.
9016 // Array index traversal.
9019 Array_index_expression::do_traverse(Traverse
* traverse
)
9021 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
9022 return TRAVERSE_EXIT
;
9023 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9024 return TRAVERSE_EXIT
;
9025 if (this->end_
!= NULL
)
9027 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9028 return TRAVERSE_EXIT
;
9030 return TRAVERSE_CONTINUE
;
9033 // Return the type of an array index.
9036 Array_index_expression::do_type()
9038 if (this->type_
== NULL
)
9040 Array_type
* type
= this->array_
->type()->array_type();
9042 this->type_
= Type::make_error_type();
9043 else if (this->end_
== NULL
)
9044 this->type_
= type
->element_type();
9045 else if (type
->is_open_array_type())
9047 // A slice of a slice has the same type as the original
9049 this->type_
= this->array_
->type()->deref();
9053 // A slice of an array is a slice.
9054 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
9060 // Set the type of an array index.
9063 Array_index_expression::do_determine_type(const Type_context
*)
9065 this->array_
->determine_type_no_context();
9066 Type_context
subcontext(NULL
, true);
9067 this->start_
->determine_type(&subcontext
);
9068 if (this->end_
!= NULL
)
9069 this->end_
->determine_type(&subcontext
);
9072 // Check types of an array index.
9075 Array_index_expression::do_check_types(Gogo
*)
9077 if (this->start_
->type()->integer_type() == NULL
)
9078 this->report_error(_("index must be integer"));
9079 if (this->end_
!= NULL
9080 && this->end_
->type()->integer_type() == NULL
9081 && !this->end_
->is_nil_expression())
9082 this->report_error(_("slice end must be integer"));
9084 Array_type
* array_type
= this->array_
->type()->array_type();
9085 gcc_assert(array_type
!= NULL
);
9087 unsigned int int_bits
=
9088 Type::lookup_integer_type("int")->integer_type()->bits();
9093 bool lval_valid
= (array_type
->length() != NULL
9094 && array_type
->length()->integer_constant_value(true,
9099 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9101 if (mpz_sgn(ival
) < 0
9102 || mpz_sizeinbase(ival
, 2) >= int_bits
9104 && (this->end_
== NULL
9105 ? mpz_cmp(ival
, lval
) >= 0
9106 : mpz_cmp(ival
, lval
) > 0)))
9108 error_at(this->start_
->location(), "array index out of bounds");
9109 this->set_is_error();
9112 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9114 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9116 if (mpz_sgn(ival
) < 0
9117 || mpz_sizeinbase(ival
, 2) >= int_bits
9118 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
9120 error_at(this->end_
->location(), "array index out of bounds");
9121 this->set_is_error();
9128 // A slice of an array requires an addressable array. A slice of a
9129 // slice is always possible.
9130 if (this->end_
!= NULL
9131 && !array_type
->is_open_array_type()
9132 && !this->array_
->is_addressable())
9133 this->report_error(_("array is not addressable"));
9136 // Return whether this expression is addressable.
9139 Array_index_expression::do_is_addressable() const
9141 // A slice expression is not addressable.
9142 if (this->end_
!= NULL
)
9145 // An index into a slice is addressable.
9146 if (this->array_
->type()->is_open_array_type())
9149 // An index into an array is addressable if the array is
9151 return this->array_
->is_addressable();
9154 // Get a tree for an array index.
9157 Array_index_expression::do_get_tree(Translate_context
* context
)
9159 Gogo
* gogo
= context
->gogo();
9160 source_location loc
= this->location();
9162 Array_type
* array_type
= this->array_
->type()->array_type();
9163 if (array_type
== NULL
)
9165 gcc_assert(this->array_
->type()->is_error_type());
9166 return error_mark_node
;
9169 tree type_tree
= array_type
->get_tree(gogo
);
9170 if (type_tree
== error_mark_node
)
9171 return error_mark_node
;
9173 tree array_tree
= this->array_
->get_tree(context
);
9174 if (array_tree
== error_mark_node
)
9175 return error_mark_node
;
9177 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
9178 array_tree
= save_expr(array_tree
);
9179 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
9180 if (length_tree
== error_mark_node
)
9181 return error_mark_node
;
9182 length_tree
= save_expr(length_tree
);
9183 tree length_type
= TREE_TYPE(length_tree
);
9185 tree bad_index
= boolean_false_node
;
9187 tree start_tree
= this->start_
->get_tree(context
);
9188 if (start_tree
== error_mark_node
)
9189 return error_mark_node
;
9190 if (!DECL_P(start_tree
))
9191 start_tree
= save_expr(start_tree
);
9192 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9193 start_tree
= convert_to_integer(length_type
, start_tree
);
9195 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9198 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9199 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9200 fold_build2_loc(loc
,
9204 boolean_type_node
, start_tree
,
9207 int code
= (array_type
->length() != NULL
9208 ? (this->end_
== NULL
9209 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9210 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9211 : (this->end_
== NULL
9212 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9213 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9214 tree crash
= Gogo::runtime_error(code
, loc
);
9216 if (this->end_
== NULL
)
9218 // Simple array indexing. This has to return an l-value, so
9219 // wrap the index check into START_TREE.
9220 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9221 build3(COND_EXPR
, void_type_node
,
9222 bad_index
, crash
, NULL_TREE
),
9224 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9226 if (array_type
->length() != NULL
)
9229 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9230 start_tree
, NULL_TREE
, NULL_TREE
);
9235 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9236 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9237 if (element_type_tree
== error_mark_node
)
9238 return error_mark_node
;
9239 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9240 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9241 start_tree
, element_size
);
9242 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9243 TREE_TYPE(values
), values
, offset
);
9244 return build_fold_indirect_ref(ptr
);
9250 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9251 if (capacity_tree
== error_mark_node
)
9252 return error_mark_node
;
9253 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9256 if (this->end_
->is_nil_expression())
9257 end_tree
= length_tree
;
9260 end_tree
= this->end_
->get_tree(context
);
9261 if (end_tree
== error_mark_node
)
9262 return error_mark_node
;
9263 if (!DECL_P(end_tree
))
9264 end_tree
= save_expr(end_tree
);
9265 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9266 end_tree
= convert_to_integer(length_type
, end_tree
);
9268 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9271 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9273 capacity_tree
= save_expr(capacity_tree
);
9274 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9275 fold_build2_loc(loc
, LT_EXPR
,
9277 end_tree
, start_tree
),
9278 fold_build2_loc(loc
, GT_EXPR
,
9280 end_tree
, capacity_tree
));
9281 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9282 bad_index
, bad_end
);
9285 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9286 if (element_type_tree
== error_mark_node
)
9287 return error_mark_node
;
9288 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9290 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9291 fold_convert_loc(loc
, sizetype
, start_tree
),
9294 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9295 if (value_pointer
== error_mark_node
)
9296 return error_mark_node
;
9298 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9299 TREE_TYPE(value_pointer
),
9300 value_pointer
, offset
);
9302 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9303 end_tree
, start_tree
);
9305 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9306 capacity_tree
, start_tree
);
9308 tree struct_tree
= this->type()->get_tree(gogo
);
9309 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9311 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9313 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9314 tree field
= TYPE_FIELDS(struct_tree
);
9315 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9317 elt
->value
= value_pointer
;
9319 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9320 field
= DECL_CHAIN(field
);
9321 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9323 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9325 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9326 field
= DECL_CHAIN(field
);
9327 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9329 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9331 tree constructor
= build_constructor(struct_tree
, init
);
9333 if (TREE_CONSTANT(value_pointer
)
9334 && TREE_CONSTANT(result_length_tree
)
9335 && TREE_CONSTANT(result_capacity_tree
))
9336 TREE_CONSTANT(constructor
) = 1;
9338 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9339 build3(COND_EXPR
, void_type_node
,
9340 bad_index
, crash
, NULL_TREE
),
9344 // Make an array index expression. END may be NULL.
9347 Expression::make_array_index(Expression
* array
, Expression
* start
,
9348 Expression
* end
, source_location location
)
9350 // Taking a slice of a composite literal requires moving the literal
9352 if (end
!= NULL
&& array
->is_composite_literal())
9354 array
= Expression::make_heap_composite(array
, location
);
9355 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9357 return new Array_index_expression(array
, start
, end
, location
);
9360 // A string index. This is used for both indexing and slicing.
9362 class String_index_expression
: public Expression
9365 String_index_expression(Expression
* string
, Expression
* start
,
9366 Expression
* end
, source_location location
)
9367 : Expression(EXPRESSION_STRING_INDEX
, location
),
9368 string_(string
), start_(start
), end_(end
)
9373 do_traverse(Traverse
*);
9379 do_determine_type(const Type_context
*);
9382 do_check_types(Gogo
*);
9387 return Expression::make_string_index(this->string_
->copy(),
9388 this->start_
->copy(),
9391 : this->end_
->copy()),
9396 do_get_tree(Translate_context
*);
9399 // The string we are getting a value from.
9400 Expression
* string_
;
9401 // The start or only index.
9403 // The end index of a slice. This may be NULL for a single index,
9404 // or it may be a nil expression for the length of the string.
9408 // String index traversal.
9411 String_index_expression::do_traverse(Traverse
* traverse
)
9413 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9414 return TRAVERSE_EXIT
;
9415 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9416 return TRAVERSE_EXIT
;
9417 if (this->end_
!= NULL
)
9419 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9420 return TRAVERSE_EXIT
;
9422 return TRAVERSE_CONTINUE
;
9425 // Return the type of a string index.
9428 String_index_expression::do_type()
9430 if (this->end_
== NULL
)
9431 return Type::lookup_integer_type("uint8");
9433 return Type::make_string_type();
9436 // Determine the type of a string index.
9439 String_index_expression::do_determine_type(const Type_context
*)
9441 this->string_
->determine_type_no_context();
9442 Type_context
subcontext(NULL
, true);
9443 this->start_
->determine_type(&subcontext
);
9444 if (this->end_
!= NULL
)
9445 this->end_
->determine_type(&subcontext
);
9448 // Check types of a string index.
9451 String_index_expression::do_check_types(Gogo
*)
9453 if (this->start_
->type()->integer_type() == NULL
)
9454 this->report_error(_("index must be integer"));
9455 if (this->end_
!= NULL
9456 && this->end_
->type()->integer_type() == NULL
9457 && !this->end_
->is_nil_expression())
9458 this->report_error(_("slice end must be integer"));
9461 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9466 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9468 if (mpz_sgn(ival
) < 0
9469 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9471 error_at(this->start_
->location(), "string index out of bounds");
9472 this->set_is_error();
9475 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9477 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9479 if (mpz_sgn(ival
) < 0
9480 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9482 error_at(this->end_
->location(), "string index out of bounds");
9483 this->set_is_error();
9490 // Get a tree for a string index.
9493 String_index_expression::do_get_tree(Translate_context
* context
)
9495 source_location loc
= this->location();
9497 tree string_tree
= this->string_
->get_tree(context
);
9498 if (string_tree
== error_mark_node
)
9499 return error_mark_node
;
9501 if (this->string_
->type()->points_to() != NULL
)
9502 string_tree
= build_fold_indirect_ref(string_tree
);
9503 if (!DECL_P(string_tree
))
9504 string_tree
= save_expr(string_tree
);
9505 tree string_type
= TREE_TYPE(string_tree
);
9507 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9508 length_tree
= save_expr(length_tree
);
9509 tree length_type
= TREE_TYPE(length_tree
);
9511 tree bad_index
= boolean_false_node
;
9513 tree start_tree
= this->start_
->get_tree(context
);
9514 if (start_tree
== error_mark_node
)
9515 return error_mark_node
;
9516 if (!DECL_P(start_tree
))
9517 start_tree
= save_expr(start_tree
);
9518 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9519 start_tree
= convert_to_integer(length_type
, start_tree
);
9521 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9524 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9526 int code
= (this->end_
== NULL
9527 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9528 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9529 tree crash
= Gogo::runtime_error(code
, loc
);
9531 if (this->end_
== NULL
)
9533 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9535 fold_build2_loc(loc
, GE_EXPR
,
9537 start_tree
, length_tree
));
9539 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9540 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9542 fold_convert_loc(loc
, sizetype
, start_tree
));
9543 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9545 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9546 build3(COND_EXPR
, void_type_node
,
9547 bad_index
, crash
, NULL_TREE
),
9553 if (this->end_
->is_nil_expression())
9554 end_tree
= build_int_cst(length_type
, -1);
9557 end_tree
= this->end_
->get_tree(context
);
9558 if (end_tree
== error_mark_node
)
9559 return error_mark_node
;
9560 if (!DECL_P(end_tree
))
9561 end_tree
= save_expr(end_tree
);
9562 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9563 end_tree
= convert_to_integer(length_type
, end_tree
);
9565 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9568 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9571 static tree strslice_fndecl
;
9572 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9574 "__go_string_slice",
9583 if (ret
== error_mark_node
)
9584 return error_mark_node
;
9585 // This will panic if the bounds are out of range for the
9587 TREE_NOTHROW(strslice_fndecl
) = 0;
9589 if (bad_index
== boolean_false_node
)
9592 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9593 build3(COND_EXPR
, void_type_node
,
9594 bad_index
, crash
, NULL_TREE
),
9599 // Make a string index expression. END may be NULL.
9602 Expression::make_string_index(Expression
* string
, Expression
* start
,
9603 Expression
* end
, source_location location
)
9605 return new String_index_expression(string
, start
, end
, location
);
9610 // Get the type of the map.
9613 Map_index_expression::get_map_type() const
9615 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9616 gcc_assert(mt
!= NULL
);
9620 // Map index traversal.
9623 Map_index_expression::do_traverse(Traverse
* traverse
)
9625 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9626 return TRAVERSE_EXIT
;
9627 return Expression::traverse(&this->index_
, traverse
);
9630 // Return the type of a map index.
9633 Map_index_expression::do_type()
9635 Type
* type
= this->get_map_type()->val_type();
9636 // If this map index is in a tuple assignment, we actually return a
9637 // pointer to the value type. Tuple_map_assignment_statement is
9638 // responsible for handling this correctly. We need to get the type
9639 // right in case this gets assigned to a temporary variable.
9640 if (this->is_in_tuple_assignment_
)
9641 type
= Type::make_pointer_type(type
);
9645 // Fix the type of a map index.
9648 Map_index_expression::do_determine_type(const Type_context
*)
9650 this->map_
->determine_type_no_context();
9651 Type_context
subcontext(this->get_map_type()->key_type(), false);
9652 this->index_
->determine_type(&subcontext
);
9655 // Check types of a map index.
9658 Map_index_expression::do_check_types(Gogo
*)
9661 if (!Type::are_assignable(this->get_map_type()->key_type(),
9662 this->index_
->type(), &reason
))
9665 this->report_error(_("incompatible type for map index"));
9668 error_at(this->location(), "incompatible type for map index (%s)",
9670 this->set_is_error();
9675 // Get a tree for a map index.
9678 Map_index_expression::do_get_tree(Translate_context
* context
)
9680 Map_type
* type
= this->get_map_type();
9682 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9683 if (valptr
== error_mark_node
)
9684 return error_mark_node
;
9685 valptr
= save_expr(valptr
);
9687 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9689 if (this->is_lvalue_
)
9690 return build_fold_indirect_ref(valptr
);
9691 else if (this->is_in_tuple_assignment_
)
9693 // Tuple_map_assignment_statement is responsible for using this
9699 return fold_build3(COND_EXPR
, val_type_tree
,
9700 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9701 fold_convert(TREE_TYPE(valptr
),
9702 null_pointer_node
)),
9703 type
->val_type()->get_init_tree(context
->gogo(),
9705 build_fold_indirect_ref(valptr
));
9709 // Get a tree for the map index. This returns a tree which evaluates
9710 // to a pointer to a value. The pointer will be NULL if the key is
9714 Map_index_expression::get_value_pointer(Translate_context
* context
,
9717 Map_type
* type
= this->get_map_type();
9719 tree map_tree
= this->map_
->get_tree(context
);
9720 tree index_tree
= this->index_
->get_tree(context
);
9721 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9722 this->index_
->type(),
9725 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9726 return error_mark_node
;
9728 if (this->map_
->type()->points_to() != NULL
)
9729 map_tree
= build_fold_indirect_ref(map_tree
);
9731 // We need to pass in a pointer to the key, so stuff it into a
9733 tree tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9734 DECL_IGNORED_P(tmp
) = 0;
9735 DECL_INITIAL(tmp
) = index_tree
;
9736 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9737 tree tmpref
= fold_convert(const_ptr_type_node
, build_fold_addr_expr(tmp
));
9738 TREE_ADDRESSABLE(tmp
) = 1;
9740 static tree map_index_fndecl
;
9741 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9745 const_ptr_type_node
,
9746 TREE_TYPE(map_tree
),
9748 const_ptr_type_node
,
9753 : boolean_false_node
));
9754 if (call
== error_mark_node
)
9755 return error_mark_node
;
9756 // This can panic on a map of interface type if the interface holds
9757 // an uncomparable or unhashable type.
9758 TREE_NOTHROW(map_index_fndecl
) = 0;
9760 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9761 if (val_type_tree
== error_mark_node
)
9762 return error_mark_node
;
9763 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9765 return build2(COMPOUND_EXPR
, ptr_val_type_tree
,
9767 fold_convert(ptr_val_type_tree
, call
));
9770 // Make a map index expression.
9772 Map_index_expression
*
9773 Expression::make_map_index(Expression
* map
, Expression
* index
,
9774 source_location location
)
9776 return new Map_index_expression(map
, index
, location
);
9779 // Class Field_reference_expression.
9781 // Return the type of a field reference.
9784 Field_reference_expression::do_type()
9786 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9787 gcc_assert(struct_type
!= NULL
);
9788 return struct_type
->field(this->field_index_
)->type();
9791 // Check the types for a field reference.
9794 Field_reference_expression::do_check_types(Gogo
*)
9796 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9797 gcc_assert(struct_type
!= NULL
);
9798 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9801 // Get a tree for a field reference.
9804 Field_reference_expression::do_get_tree(Translate_context
* context
)
9806 tree struct_tree
= this->expr_
->get_tree(context
);
9807 if (struct_tree
== error_mark_node
9808 || TREE_TYPE(struct_tree
) == error_mark_node
)
9809 return error_mark_node
;
9810 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
9811 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
9812 if (field
== NULL_TREE
)
9814 // This can happen for a type which refers to itself indirectly
9815 // and then turns out to be erroneous.
9816 gcc_assert(saw_errors());
9817 return error_mark_node
;
9819 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
9821 field
= DECL_CHAIN(field
);
9822 gcc_assert(field
!= NULL_TREE
);
9824 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
9828 // Make a reference to a qualified identifier in an expression.
9830 Field_reference_expression
*
9831 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
9832 source_location location
)
9834 return new Field_reference_expression(expr
, field_index
, location
);
9837 // Class Interface_field_reference_expression.
9839 // Return a tree for the pointer to the function to call.
9842 Interface_field_reference_expression::get_function_tree(Translate_context
*,
9845 if (this->expr_
->type()->points_to() != NULL
)
9846 expr
= build_fold_indirect_ref(expr
);
9848 tree expr_type
= TREE_TYPE(expr
);
9849 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9851 tree field
= TYPE_FIELDS(expr_type
);
9852 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
9854 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9855 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
9857 table
= build_fold_indirect_ref(table
);
9858 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
9860 std::string name
= Gogo::unpack_hidden_name(this->name_
);
9861 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
9863 field
= DECL_CHAIN(field
))
9865 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
9868 gcc_assert(field
!= NULL_TREE
);
9870 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
9873 // Return a tree for the first argument to pass to the interface
9877 Interface_field_reference_expression::get_underlying_object_tree(
9881 if (this->expr_
->type()->points_to() != NULL
)
9882 expr
= build_fold_indirect_ref(expr
);
9884 tree expr_type
= TREE_TYPE(expr
);
9885 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9887 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
9888 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
9890 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9896 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
9898 return Expression::traverse(&this->expr_
, traverse
);
9901 // Return the type of an interface field reference.
9904 Interface_field_reference_expression::do_type()
9906 Type
* expr_type
= this->expr_
->type();
9908 Type
* points_to
= expr_type
->points_to();
9909 if (points_to
!= NULL
)
9910 expr_type
= points_to
;
9912 Interface_type
* interface_type
= expr_type
->interface_type();
9913 if (interface_type
== NULL
)
9914 return Type::make_error_type();
9916 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
9918 return Type::make_error_type();
9920 return method
->type();
9926 Interface_field_reference_expression::do_determine_type(const Type_context
*)
9928 this->expr_
->determine_type_no_context();
9931 // Check the types for an interface field reference.
9934 Interface_field_reference_expression::do_check_types(Gogo
*)
9936 Type
* type
= this->expr_
->type();
9938 Type
* points_to
= type
->points_to();
9939 if (points_to
!= NULL
)
9942 Interface_type
* interface_type
= type
->interface_type();
9943 if (interface_type
== NULL
)
9944 this->report_error(_("expected interface or pointer to interface"));
9947 const Typed_identifier
* method
=
9948 interface_type
->find_method(this->name_
);
9951 error_at(this->location(), "method %qs not in interface",
9952 Gogo::message_name(this->name_
).c_str());
9953 this->set_is_error();
9958 // Get a tree for a reference to a field in an interface. There is no
9959 // standard tree type representation for this: it's a function
9960 // attached to its first argument, like a Bound_method_expression.
9961 // The only places it may currently be used are in a Call_expression
9962 // or a Go_statement, which will take it apart directly. So this has
9963 // nothing to do at present.
9966 Interface_field_reference_expression::do_get_tree(Translate_context
*)
9971 // Make a reference to a field in an interface.
9974 Expression::make_interface_field_reference(Expression
* expr
,
9975 const std::string
& field
,
9976 source_location location
)
9978 return new Interface_field_reference_expression(expr
, field
, location
);
9981 // A general selector. This is a Parser_expression for LEFT.NAME. It
9982 // is lowered after we know the type of the left hand side.
9984 class Selector_expression
: public Parser_expression
9987 Selector_expression(Expression
* left
, const std::string
& name
,
9988 source_location location
)
9989 : Parser_expression(EXPRESSION_SELECTOR
, location
),
9990 left_(left
), name_(name
)
9995 do_traverse(Traverse
* traverse
)
9996 { return Expression::traverse(&this->left_
, traverse
); }
9999 do_lower(Gogo
*, Named_object
*, int);
10004 return new Selector_expression(this->left_
->copy(), this->name_
,
10010 lower_method_expression(Gogo
*);
10012 // The expression on the left hand side.
10014 // The name on the right hand side.
10018 // Lower a selector expression once we know the real type of the left
10022 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
10024 Expression
* left
= this->left_
;
10025 if (left
->is_type_expression())
10026 return this->lower_method_expression(gogo
);
10027 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
10031 // Lower a method expression T.M or (*T).M. We turn this into a
10032 // function literal.
10035 Selector_expression::lower_method_expression(Gogo
* gogo
)
10037 source_location location
= this->location();
10038 Type
* type
= this->left_
->type();
10039 const std::string
& name(this->name_
);
10042 if (type
->points_to() == NULL
)
10043 is_pointer
= false;
10047 type
= type
->points_to();
10049 Named_type
* nt
= type
->named_type();
10053 ("method expression requires named type or "
10054 "pointer to named type"));
10055 return Expression::make_error(location
);
10059 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
10060 if (method
== NULL
)
10063 error_at(location
, "type %<%s%> has no method %<%s%>",
10064 nt
->message_name().c_str(),
10065 Gogo::message_name(name
).c_str());
10067 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
10068 Gogo::message_name(name
).c_str(),
10069 nt
->message_name().c_str());
10070 return Expression::make_error(location
);
10073 if (!is_pointer
&& !method
->is_value_method())
10075 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
10076 nt
->message_name().c_str(),
10077 Gogo::message_name(name
).c_str());
10078 return Expression::make_error(location
);
10081 // Build a new function type in which the receiver becomes the first
10083 Function_type
* method_type
= method
->type();
10084 gcc_assert(method_type
->is_method());
10086 const char* const receiver_name
= "$this";
10087 Typed_identifier_list
* parameters
= new Typed_identifier_list();
10088 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
10091 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
10092 if (method_parameters
!= NULL
)
10094 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10095 p
!= method_parameters
->end();
10097 parameters
->push_back(*p
);
10100 const Typed_identifier_list
* method_results
= method_type
->results();
10101 Typed_identifier_list
* results
;
10102 if (method_results
== NULL
)
10106 results
= new Typed_identifier_list();
10107 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
10108 p
!= method_results
->end();
10110 results
->push_back(*p
);
10113 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
10115 if (method_type
->is_varargs())
10116 fntype
->set_is_varargs();
10118 // We generate methods which always takes a pointer to the receiver
10119 // as their first argument. If this is for a pointer type, we can
10120 // simply reuse the existing function. We use an internal hack to
10121 // get the right type.
10125 Named_object
* mno
= (method
->needs_stub_method()
10126 ? method
->stub_object()
10127 : method
->named_object());
10128 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
10129 f
= Expression::make_cast(fntype
, f
, location
);
10130 Type_conversion_expression
* tce
=
10131 static_cast<Type_conversion_expression
*>(f
);
10132 tce
->set_may_convert_function_types();
10136 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
10139 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
10140 gcc_assert(vno
!= NULL
);
10141 Expression
* ve
= Expression::make_var_reference(vno
, location
);
10142 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
10143 gcc_assert(bm
!= NULL
&& !bm
->is_error_expression());
10145 Expression_list
* args
;
10146 if (method_parameters
== NULL
)
10150 args
= new Expression_list();
10151 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
10152 p
!= method_parameters
->end();
10155 vno
= gogo
->lookup(p
->name(), NULL
);
10156 gcc_assert(vno
!= NULL
);
10157 args
->push_back(Expression::make_var_reference(vno
, location
));
10161 Call_expression
* call
= Expression::make_call(bm
, args
,
10162 method_type
->is_varargs(),
10165 size_t count
= call
->result_count();
10168 s
= Statement::make_statement(call
);
10171 Expression_list
* retvals
= new Expression_list();
10173 retvals
->push_back(call
);
10176 for (size_t i
= 0; i
< count
; ++i
)
10177 retvals
->push_back(Expression::make_call_result(call
, i
));
10179 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
10180 retvals
, location
);
10182 gogo
->add_statement(s
);
10184 gogo
->finish_function(location
);
10186 return Expression::make_func_reference(no
, NULL
, location
);
10189 // Make a selector expression.
10192 Expression::make_selector(Expression
* left
, const std::string
& name
,
10193 source_location location
)
10195 return new Selector_expression(left
, name
, location
);
10198 // Implement the builtin function new.
10200 class Allocation_expression
: public Expression
10203 Allocation_expression(Type
* type
, source_location location
)
10204 : Expression(EXPRESSION_ALLOCATION
, location
),
10210 do_traverse(Traverse
* traverse
)
10211 { return Type::traverse(this->type_
, traverse
); }
10215 { return Type::make_pointer_type(this->type_
); }
10218 do_determine_type(const Type_context
*)
10222 do_check_types(Gogo
*);
10226 { return new Allocation_expression(this->type_
, this->location()); }
10229 do_get_tree(Translate_context
*);
10232 // The type we are allocating.
10236 // Check the type of an allocation expression.
10239 Allocation_expression::do_check_types(Gogo
*)
10241 if (this->type_
->function_type() != NULL
)
10242 this->report_error(_("invalid new of function type"));
10245 // Return a tree for an allocation expression.
10248 Allocation_expression::do_get_tree(Translate_context
* context
)
10250 tree type_tree
= this->type_
->get_tree(context
->gogo());
10251 if (type_tree
== error_mark_node
)
10252 return error_mark_node
;
10253 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10254 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10256 if (space
== error_mark_node
)
10257 return error_mark_node
;
10258 return fold_convert(build_pointer_type(type_tree
), space
);
10261 // Make an allocation expression.
10264 Expression::make_allocation(Type
* type
, source_location location
)
10266 return new Allocation_expression(type
, location
);
10269 // Implement the builtin function make.
10271 class Make_expression
: public Expression
10274 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10275 : Expression(EXPRESSION_MAKE
, location
),
10276 type_(type
), args_(args
)
10281 do_traverse(Traverse
* traverse
);
10285 { return this->type_
; }
10288 do_determine_type(const Type_context
*);
10291 do_check_types(Gogo
*);
10296 return new Make_expression(this->type_
, this->args_
->copy(),
10301 do_get_tree(Translate_context
*);
10304 // The type we are making.
10306 // The arguments to pass to the make routine.
10307 Expression_list
* args_
;
10313 Make_expression::do_traverse(Traverse
* traverse
)
10315 if (this->args_
!= NULL
10316 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10317 return TRAVERSE_EXIT
;
10318 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10319 return TRAVERSE_EXIT
;
10320 return TRAVERSE_CONTINUE
;
10323 // Set types of arguments.
10326 Make_expression::do_determine_type(const Type_context
*)
10328 if (this->args_
!= NULL
)
10330 Type_context
context(Type::lookup_integer_type("int"), false);
10331 for (Expression_list::const_iterator pe
= this->args_
->begin();
10332 pe
!= this->args_
->end();
10334 (*pe
)->determine_type(&context
);
10338 // Check types for a make expression.
10341 Make_expression::do_check_types(Gogo
*)
10343 if (this->type_
->channel_type() == NULL
10344 && this->type_
->map_type() == NULL
10345 && (this->type_
->array_type() == NULL
10346 || this->type_
->array_type()->length() != NULL
))
10347 this->report_error(_("invalid type for make function"));
10348 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10349 this->set_is_error();
10352 // Return a tree for a make expression.
10355 Make_expression::do_get_tree(Translate_context
* context
)
10357 return this->type_
->make_expression_tree(context
, this->args_
,
10361 // Make a make expression.
10364 Expression::make_make(Type
* type
, Expression_list
* args
,
10365 source_location location
)
10367 return new Make_expression(type
, args
, location
);
10370 // Construct a struct.
10372 class Struct_construction_expression
: public Expression
10375 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10376 source_location location
)
10377 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10378 type_(type
), vals_(vals
)
10381 // Return whether this is a constant initializer.
10383 is_constant_struct() const;
10387 do_traverse(Traverse
* traverse
);
10391 { return this->type_
; }
10394 do_determine_type(const Type_context
*);
10397 do_check_types(Gogo
*);
10402 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10407 do_is_addressable() const
10411 do_get_tree(Translate_context
*);
10414 do_export(Export
*) const;
10417 // The type of the struct to construct.
10419 // The list of values, in order of the fields in the struct. A NULL
10420 // entry means that the field should be zero-initialized.
10421 Expression_list
* vals_
;
10427 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10429 if (this->vals_
!= NULL
10430 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10431 return TRAVERSE_EXIT
;
10432 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10433 return TRAVERSE_EXIT
;
10434 return TRAVERSE_CONTINUE
;
10437 // Return whether this is a constant initializer.
10440 Struct_construction_expression::is_constant_struct() const
10442 if (this->vals_
== NULL
)
10444 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10445 pv
!= this->vals_
->end();
10449 && !(*pv
)->is_constant()
10450 && (!(*pv
)->is_composite_literal()
10451 || (*pv
)->is_nonconstant_composite_literal()))
10455 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10456 for (Struct_field_list::const_iterator pf
= fields
->begin();
10457 pf
!= fields
->end();
10460 // There are no constant constructors for interfaces.
10461 if (pf
->type()->interface_type() != NULL
)
10468 // Final type determination.
10471 Struct_construction_expression::do_determine_type(const Type_context
*)
10473 if (this->vals_
== NULL
)
10475 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10476 Expression_list::const_iterator pv
= this->vals_
->begin();
10477 for (Struct_field_list::const_iterator pf
= fields
->begin();
10478 pf
!= fields
->end();
10481 if (pv
== this->vals_
->end())
10485 Type_context
subcontext(pf
->type(), false);
10486 (*pv
)->determine_type(&subcontext
);
10494 Struct_construction_expression::do_check_types(Gogo
*)
10496 if (this->vals_
== NULL
)
10499 Struct_type
* st
= this->type_
->struct_type();
10500 if (this->vals_
->size() > st
->field_count())
10502 this->report_error(_("too many expressions for struct"));
10506 const Struct_field_list
* fields
= st
->fields();
10507 Expression_list::const_iterator pv
= this->vals_
->begin();
10509 for (Struct_field_list::const_iterator pf
= fields
->begin();
10510 pf
!= fields
->end();
10513 if (pv
== this->vals_
->end())
10515 this->report_error(_("too few expressions for struct"));
10522 std::string reason
;
10523 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10525 if (reason
.empty())
10526 error_at((*pv
)->location(),
10527 "incompatible type for field %d in struct construction",
10530 error_at((*pv
)->location(),
10531 ("incompatible type for field %d in "
10532 "struct construction (%s)"),
10533 i
+ 1, reason
.c_str());
10534 this->set_is_error();
10537 gcc_assert(pv
== this->vals_
->end());
10540 // Return a tree for constructing a struct.
10543 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10545 Gogo
* gogo
= context
->gogo();
10547 if (this->vals_
== NULL
)
10548 return this->type_
->get_init_tree(gogo
, false);
10550 tree type_tree
= this->type_
->get_tree(gogo
);
10551 if (type_tree
== error_mark_node
)
10552 return error_mark_node
;
10553 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10555 bool is_constant
= true;
10556 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10557 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10559 Struct_field_list::const_iterator pf
= fields
->begin();
10560 Expression_list::const_iterator pv
= this->vals_
->begin();
10561 for (tree field
= TYPE_FIELDS(type_tree
);
10562 field
!= NULL_TREE
;
10563 field
= DECL_CHAIN(field
), ++pf
)
10565 gcc_assert(pf
!= fields
->end());
10568 if (pv
== this->vals_
->end())
10569 val
= pf
->type()->get_init_tree(gogo
, false);
10570 else if (*pv
== NULL
)
10572 val
= pf
->type()->get_init_tree(gogo
, false);
10577 val
= Expression::convert_for_assignment(context
, pf
->type(),
10579 (*pv
)->get_tree(context
),
10584 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10585 return error_mark_node
;
10587 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10588 elt
->index
= field
;
10590 if (!TREE_CONSTANT(val
))
10591 is_constant
= false;
10593 gcc_assert(pf
== fields
->end());
10595 tree ret
= build_constructor(type_tree
, elts
);
10597 TREE_CONSTANT(ret
) = 1;
10601 // Export a struct construction.
10604 Struct_construction_expression::do_export(Export
* exp
) const
10606 exp
->write_c_string("convert(");
10607 exp
->write_type(this->type_
);
10608 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10609 pv
!= this->vals_
->end();
10612 exp
->write_c_string(", ");
10614 (*pv
)->export_expression(exp
);
10616 exp
->write_c_string(")");
10619 // Make a struct composite literal. This used by the thunk code.
10622 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10623 source_location location
)
10625 gcc_assert(type
->struct_type() != NULL
);
10626 return new Struct_construction_expression(type
, vals
, location
);
10629 // Construct an array. This class is not used directly; instead we
10630 // use the child classes, Fixed_array_construction_expression and
10631 // Open_array_construction_expression.
10633 class Array_construction_expression
: public Expression
10636 Array_construction_expression(Expression_classification classification
,
10637 Type
* type
, Expression_list
* vals
,
10638 source_location location
)
10639 : Expression(classification
, location
),
10640 type_(type
), vals_(vals
)
10644 // Return whether this is a constant initializer.
10646 is_constant_array() const;
10648 // Return the number of elements.
10650 element_count() const
10651 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10655 do_traverse(Traverse
* traverse
);
10659 { return this->type_
; }
10662 do_determine_type(const Type_context
*);
10665 do_check_types(Gogo
*);
10668 do_is_addressable() const
10672 do_export(Export
*) const;
10674 // The list of values.
10677 { return this->vals_
; }
10679 // Get a constructor tree for the array values.
10681 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10684 // The type of the array to construct.
10686 // The list of values.
10687 Expression_list
* vals_
;
10693 Array_construction_expression::do_traverse(Traverse
* traverse
)
10695 if (this->vals_
!= NULL
10696 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10697 return TRAVERSE_EXIT
;
10698 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10699 return TRAVERSE_EXIT
;
10700 return TRAVERSE_CONTINUE
;
10703 // Return whether this is a constant initializer.
10706 Array_construction_expression::is_constant_array() const
10708 if (this->vals_
== NULL
)
10711 // There are no constant constructors for interfaces.
10712 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10715 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10716 pv
!= this->vals_
->end();
10720 && !(*pv
)->is_constant()
10721 && (!(*pv
)->is_composite_literal()
10722 || (*pv
)->is_nonconstant_composite_literal()))
10728 // Final type determination.
10731 Array_construction_expression::do_determine_type(const Type_context
*)
10733 if (this->vals_
== NULL
)
10735 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10736 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10737 pv
!= this->vals_
->end();
10741 (*pv
)->determine_type(&subcontext
);
10748 Array_construction_expression::do_check_types(Gogo
*)
10750 if (this->vals_
== NULL
)
10753 Array_type
* at
= this->type_
->array_type();
10755 Type
* element_type
= at
->element_type();
10756 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10757 pv
!= this->vals_
->end();
10761 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10763 error_at((*pv
)->location(),
10764 "incompatible type for element %d in composite literal",
10766 this->set_is_error();
10770 Expression
* length
= at
->length();
10771 if (length
!= NULL
)
10776 if (at
->length()->integer_constant_value(true, val
, &type
))
10778 if (this->vals_
->size() > mpz_get_ui(val
))
10779 this->report_error(_("too many elements in composite literal"));
10785 // Get a constructor tree for the array values.
10788 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10791 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10792 (this->vals_
== NULL
10794 : this->vals_
->size()));
10795 Type
* element_type
= this->type_
->array_type()->element_type();
10796 bool is_constant
= true;
10797 if (this->vals_
!= NULL
)
10800 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10801 pv
!= this->vals_
->end();
10804 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
10805 elt
->index
= size_int(i
);
10807 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10810 tree value_tree
= (*pv
)->get_tree(context
);
10811 elt
->value
= Expression::convert_for_assignment(context
,
10817 if (elt
->value
== error_mark_node
)
10818 return error_mark_node
;
10819 if (!TREE_CONSTANT(elt
->value
))
10820 is_constant
= false;
10824 tree ret
= build_constructor(type_tree
, values
);
10826 TREE_CONSTANT(ret
) = 1;
10830 // Export an array construction.
10833 Array_construction_expression::do_export(Export
* exp
) const
10835 exp
->write_c_string("convert(");
10836 exp
->write_type(this->type_
);
10837 if (this->vals_
!= NULL
)
10839 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10840 pv
!= this->vals_
->end();
10843 exp
->write_c_string(", ");
10845 (*pv
)->export_expression(exp
);
10848 exp
->write_c_string(")");
10851 // Construct a fixed array.
10853 class Fixed_array_construction_expression
:
10854 public Array_construction_expression
10857 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
10858 source_location location
)
10859 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
10860 type
, vals
, location
)
10862 gcc_assert(type
->array_type() != NULL
10863 && type
->array_type()->length() != NULL
);
10870 return new Fixed_array_construction_expression(this->type(),
10871 (this->vals() == NULL
10873 : this->vals()->copy()),
10878 do_get_tree(Translate_context
*);
10881 // Return a tree for constructing a fixed array.
10884 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
10886 return this->get_constructor_tree(context
,
10887 this->type()->get_tree(context
->gogo()));
10890 // Construct an open array.
10892 class Open_array_construction_expression
: public Array_construction_expression
10895 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
10896 source_location location
)
10897 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
10898 type
, vals
, location
)
10900 gcc_assert(type
->array_type() != NULL
10901 && type
->array_type()->length() == NULL
);
10905 // Note that taking the address of an open array literal is invalid.
10910 return new Open_array_construction_expression(this->type(),
10911 (this->vals() == NULL
10913 : this->vals()->copy()),
10918 do_get_tree(Translate_context
*);
10921 // Return a tree for constructing an open array.
10924 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
10926 Type
* element_type
= this->type()->array_type()->element_type();
10927 tree element_type_tree
= element_type
->get_tree(context
->gogo());
10928 if (element_type_tree
== error_mark_node
)
10929 return error_mark_node
;
10933 if (this->vals() == NULL
|| this->vals()->empty())
10935 // We need to create a unique value.
10936 tree max
= size_int(0);
10937 tree constructor_type
= build_array_type(element_type_tree
,
10938 build_index_type(max
));
10939 if (constructor_type
== error_mark_node
)
10940 return error_mark_node
;
10941 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
10942 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
10943 elt
->index
= size_int(0);
10944 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10945 values
= build_constructor(constructor_type
, vec
);
10946 if (TREE_CONSTANT(elt
->value
))
10947 TREE_CONSTANT(values
) = 1;
10948 length_tree
= size_int(0);
10952 tree max
= size_int(this->vals()->size() - 1);
10953 tree constructor_type
= build_array_type(element_type_tree
,
10954 build_index_type(max
));
10955 if (constructor_type
== error_mark_node
)
10956 return error_mark_node
;
10957 values
= this->get_constructor_tree(context
, constructor_type
);
10958 length_tree
= size_int(this->vals()->size());
10961 if (values
== error_mark_node
)
10962 return error_mark_node
;
10964 bool is_constant_initializer
= TREE_CONSTANT(values
);
10965 bool is_in_function
= context
->function() != NULL
;
10967 if (is_constant_initializer
)
10969 tree tmp
= build_decl(this->location(), VAR_DECL
,
10970 create_tmp_var_name("C"), TREE_TYPE(values
));
10971 DECL_EXTERNAL(tmp
) = 0;
10972 TREE_PUBLIC(tmp
) = 0;
10973 TREE_STATIC(tmp
) = 1;
10974 DECL_ARTIFICIAL(tmp
) = 1;
10975 if (is_in_function
)
10977 // If this is not a function, we will only initialize the
10978 // value once, so we can use this directly rather than
10979 // copying it. In that case we can't make it read-only,
10980 // because the program is permitted to change it.
10981 TREE_READONLY(tmp
) = 1;
10982 TREE_CONSTANT(tmp
) = 1;
10984 DECL_INITIAL(tmp
) = values
;
10985 rest_of_decl_compilation(tmp
, 1, 0);
10991 if (!is_in_function
&& is_constant_initializer
)
10993 // Outside of a function, we know the initializer will only run
10995 space
= build_fold_addr_expr(values
);
11000 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
11001 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
11003 space
= save_expr(space
);
11005 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
11006 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
11007 TREE_THIS_NOTRAP(ref
) = 1;
11008 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
11011 // Build a constructor for the open array.
11013 tree type_tree
= this->type()->get_tree(context
->gogo());
11014 if (type_tree
== error_mark_node
)
11015 return error_mark_node
;
11016 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
11018 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
11020 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11021 tree field
= TYPE_FIELDS(type_tree
);
11022 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
11023 elt
->index
= field
;
11024 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
11026 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11027 field
= DECL_CHAIN(field
);
11028 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
11029 elt
->index
= field
;
11030 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11032 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
11033 field
= DECL_CHAIN(field
);
11034 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
11035 elt
->index
= field
;
11036 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
11038 tree constructor
= build_constructor(type_tree
, init
);
11039 if (constructor
== error_mark_node
)
11040 return error_mark_node
;
11041 if (!is_in_function
&& is_constant_initializer
)
11042 TREE_CONSTANT(constructor
) = 1;
11044 if (set
== NULL_TREE
)
11045 return constructor
;
11047 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
11050 // Make a slice composite literal. This is used by the type
11051 // descriptor code.
11054 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
11055 source_location location
)
11057 gcc_assert(type
->is_open_array_type());
11058 return new Open_array_construction_expression(type
, vals
, location
);
11061 // Construct a map.
11063 class Map_construction_expression
: public Expression
11066 Map_construction_expression(Type
* type
, Expression_list
* vals
,
11067 source_location location
)
11068 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
11069 type_(type
), vals_(vals
)
11070 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
11074 do_traverse(Traverse
* traverse
);
11078 { return this->type_
; }
11081 do_determine_type(const Type_context
*);
11084 do_check_types(Gogo
*);
11089 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
11094 do_get_tree(Translate_context
*);
11097 do_export(Export
*) const;
11100 // The type of the map to construct.
11102 // The list of values.
11103 Expression_list
* vals_
;
11109 Map_construction_expression::do_traverse(Traverse
* traverse
)
11111 if (this->vals_
!= NULL
11112 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11113 return TRAVERSE_EXIT
;
11114 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11115 return TRAVERSE_EXIT
;
11116 return TRAVERSE_CONTINUE
;
11119 // Final type determination.
11122 Map_construction_expression::do_determine_type(const Type_context
*)
11124 if (this->vals_
== NULL
)
11127 Map_type
* mt
= this->type_
->map_type();
11128 Type_context
key_context(mt
->key_type(), false);
11129 Type_context
val_context(mt
->val_type(), false);
11130 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11131 pv
!= this->vals_
->end();
11134 (*pv
)->determine_type(&key_context
);
11136 (*pv
)->determine_type(&val_context
);
11143 Map_construction_expression::do_check_types(Gogo
*)
11145 if (this->vals_
== NULL
)
11148 Map_type
* mt
= this->type_
->map_type();
11150 Type
* key_type
= mt
->key_type();
11151 Type
* val_type
= mt
->val_type();
11152 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11153 pv
!= this->vals_
->end();
11156 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
11158 error_at((*pv
)->location(),
11159 "incompatible type for element %d key in map construction",
11161 this->set_is_error();
11164 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
11166 error_at((*pv
)->location(),
11167 ("incompatible type for element %d value "
11168 "in map construction"),
11170 this->set_is_error();
11175 // Return a tree for constructing a map.
11178 Map_construction_expression::do_get_tree(Translate_context
* context
)
11180 Gogo
* gogo
= context
->gogo();
11181 source_location loc
= this->location();
11183 Map_type
* mt
= this->type_
->map_type();
11185 // Build a struct to hold the key and value.
11186 tree struct_type
= make_node(RECORD_TYPE
);
11188 Type
* key_type
= mt
->key_type();
11189 tree id
= get_identifier("__key");
11190 tree key_type_tree
= key_type
->get_tree(gogo
);
11191 if (key_type_tree
== error_mark_node
)
11192 return error_mark_node
;
11193 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type_tree
);
11194 DECL_CONTEXT(key_field
) = struct_type
;
11195 TYPE_FIELDS(struct_type
) = key_field
;
11197 Type
* val_type
= mt
->val_type();
11198 id
= get_identifier("__val");
11199 tree val_type_tree
= val_type
->get_tree(gogo
);
11200 if (val_type_tree
== error_mark_node
)
11201 return error_mark_node
;
11202 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type_tree
);
11203 DECL_CONTEXT(val_field
) = struct_type
;
11204 DECL_CHAIN(key_field
) = val_field
;
11206 layout_type(struct_type
);
11208 bool is_constant
= true;
11213 if (this->vals_
== NULL
|| this->vals_
->empty())
11215 valaddr
= null_pointer_node
;
11216 make_tmp
= NULL_TREE
;
11220 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
11221 this->vals_
->size() / 2);
11223 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11224 pv
!= this->vals_
->end();
11227 bool one_is_constant
= true;
11229 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11231 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11232 elt
->index
= key_field
;
11233 tree val_tree
= (*pv
)->get_tree(context
);
11234 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11237 if (elt
->value
== error_mark_node
)
11238 return error_mark_node
;
11239 if (!TREE_CONSTANT(elt
->value
))
11240 one_is_constant
= false;
11244 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11245 elt
->index
= val_field
;
11246 val_tree
= (*pv
)->get_tree(context
);
11247 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11250 if (elt
->value
== error_mark_node
)
11251 return error_mark_node
;
11252 if (!TREE_CONSTANT(elt
->value
))
11253 one_is_constant
= false;
11255 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11256 elt
->index
= size_int(i
);
11257 elt
->value
= build_constructor(struct_type
, one
);
11258 if (one_is_constant
)
11259 TREE_CONSTANT(elt
->value
) = 1;
11261 is_constant
= false;
11264 tree index_type
= build_index_type(size_int(i
- 1));
11265 tree array_type
= build_array_type(struct_type
, index_type
);
11266 tree init
= build_constructor(array_type
, values
);
11268 TREE_CONSTANT(init
) = 1;
11270 if (current_function_decl
!= NULL
)
11272 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11273 DECL_INITIAL(tmp
) = init
;
11274 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11275 TREE_ADDRESSABLE(tmp
) = 1;
11279 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11280 DECL_EXTERNAL(tmp
) = 0;
11281 TREE_PUBLIC(tmp
) = 0;
11282 TREE_STATIC(tmp
) = 1;
11283 DECL_ARTIFICIAL(tmp
) = 1;
11284 if (!TREE_CONSTANT(init
))
11285 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11289 TREE_READONLY(tmp
) = 1;
11290 TREE_CONSTANT(tmp
) = 1;
11291 DECL_INITIAL(tmp
) = init
;
11292 make_tmp
= NULL_TREE
;
11294 rest_of_decl_compilation(tmp
, 1, 0);
11297 valaddr
= build_fold_addr_expr(tmp
);
11300 tree descriptor
= gogo
->map_descriptor(mt
);
11302 tree type_tree
= this->type_
->get_tree(gogo
);
11303 if (type_tree
== error_mark_node
)
11304 return error_mark_node
;
11306 static tree construct_map_fndecl
;
11307 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11309 "__go_construct_map",
11312 TREE_TYPE(descriptor
),
11317 TYPE_SIZE_UNIT(struct_type
),
11319 byte_position(val_field
),
11321 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11322 const_ptr_type_node
,
11323 fold_convert(const_ptr_type_node
, valaddr
));
11324 if (call
== error_mark_node
)
11325 return error_mark_node
;
11328 if (make_tmp
== NULL
)
11331 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11335 // Export an array construction.
11338 Map_construction_expression::do_export(Export
* exp
) const
11340 exp
->write_c_string("convert(");
11341 exp
->write_type(this->type_
);
11342 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11343 pv
!= this->vals_
->end();
11346 exp
->write_c_string(", ");
11347 (*pv
)->export_expression(exp
);
11349 exp
->write_c_string(")");
11352 // A general composite literal. This is lowered to a type specific
11355 class Composite_literal_expression
: public Parser_expression
11358 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11359 Expression_list
* vals
, source_location location
)
11360 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11361 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11366 do_traverse(Traverse
* traverse
);
11369 do_lower(Gogo
*, Named_object
*, int);
11374 return new Composite_literal_expression(this->type_
, this->depth_
,
11376 (this->vals_
== NULL
11378 : this->vals_
->copy()),
11384 lower_struct(Type
*);
11387 lower_array(Type
*);
11390 make_array(Type
*, Expression_list
*);
11393 lower_map(Gogo
*, Named_object
*, Type
*);
11395 // The type of the composite literal.
11397 // The depth within a list of composite literals within a composite
11398 // literal, when the type is omitted.
11400 // The values to put in the composite literal.
11401 Expression_list
* vals_
;
11402 // If this is true, then VALS_ is a list of pairs: a key and a
11403 // value. In an array initializer, a missing key will be NULL.
11410 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11412 if (this->vals_
!= NULL
11413 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11414 return TRAVERSE_EXIT
;
11415 return Type::traverse(this->type_
, traverse
);
11418 // Lower a generic composite literal into a specific version based on
11422 Composite_literal_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
11424 Type
* type
= this->type_
;
11426 for (int depth
= this->depth_
; depth
> 0; --depth
)
11428 if (type
->array_type() != NULL
)
11429 type
= type
->array_type()->element_type();
11430 else if (type
->map_type() != NULL
)
11431 type
= type
->map_type()->val_type();
11434 if (!type
->is_error_type())
11435 error_at(this->location(),
11436 ("may only omit types within composite literals "
11437 "of slice, array, or map type"));
11438 return Expression::make_error(this->location());
11442 if (type
->is_error_type())
11443 return Expression::make_error(this->location());
11444 else if (type
->struct_type() != NULL
)
11445 return this->lower_struct(type
);
11446 else if (type
->array_type() != NULL
)
11447 return this->lower_array(type
);
11448 else if (type
->map_type() != NULL
)
11449 return this->lower_map(gogo
, function
, type
);
11452 error_at(this->location(),
11453 ("expected struct, slice, array, or map type "
11454 "for composite literal"));
11455 return Expression::make_error(this->location());
11459 // Lower a struct composite literal.
11462 Composite_literal_expression::lower_struct(Type
* type
)
11464 source_location location
= this->location();
11465 Struct_type
* st
= type
->struct_type();
11466 if (this->vals_
== NULL
|| !this->has_keys_
)
11467 return new Struct_construction_expression(type
, this->vals_
, location
);
11469 size_t field_count
= st
->field_count();
11470 std::vector
<Expression
*> vals(field_count
);
11471 Expression_list::const_iterator p
= this->vals_
->begin();
11472 while (p
!= this->vals_
->end())
11474 Expression
* name_expr
= *p
;
11477 gcc_assert(p
!= this->vals_
->end());
11478 Expression
* val
= *p
;
11482 if (name_expr
== NULL
)
11484 error_at(val
->location(), "mixture of field and value initializers");
11485 return Expression::make_error(location
);
11488 bool bad_key
= false;
11490 switch (name_expr
->classification())
11492 case EXPRESSION_UNKNOWN_REFERENCE
:
11493 name
= name_expr
->unknown_expression()->name();
11496 case EXPRESSION_CONST_REFERENCE
:
11497 name
= static_cast<Const_expression
*>(name_expr
)->name();
11500 case EXPRESSION_TYPE
:
11502 Type
* t
= name_expr
->type();
11503 Named_type
* nt
= t
->named_type();
11511 case EXPRESSION_VAR_REFERENCE
:
11512 name
= name_expr
->var_expression()->name();
11515 case EXPRESSION_FUNC_REFERENCE
:
11516 name
= name_expr
->func_expression()->name();
11519 case EXPRESSION_UNARY
:
11520 // If there is a local variable around with the same name as
11521 // the field, and this occurs in the closure, then the
11522 // parser may turn the field reference into an indirection
11523 // through the closure. FIXME: This is a mess.
11526 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11527 if (ue
->op() == OPERATOR_MULT
)
11529 Field_reference_expression
* fre
=
11530 ue
->operand()->field_reference_expression();
11534 fre
->expr()->type()->deref()->struct_type();
11537 const Struct_field
* sf
= st
->field(fre
->field_index());
11538 name
= sf
->field_name();
11540 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11541 size_t buflen
= strlen(buf
);
11542 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11545 name
= name
.substr(0, name
.length() - buflen
);
11560 error_at(name_expr
->location(), "expected struct field name");
11561 return Expression::make_error(location
);
11564 unsigned int index
;
11565 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11568 error_at(name_expr
->location(), "unknown field %qs in %qs",
11569 Gogo::message_name(name
).c_str(),
11570 (type
->named_type() != NULL
11571 ? type
->named_type()->message_name().c_str()
11572 : "unnamed struct"));
11573 return Expression::make_error(location
);
11575 if (vals
[index
] != NULL
)
11577 error_at(name_expr
->location(),
11578 "duplicate value for field %qs in %qs",
11579 Gogo::message_name(name
).c_str(),
11580 (type
->named_type() != NULL
11581 ? type
->named_type()->message_name().c_str()
11582 : "unnamed struct"));
11583 return Expression::make_error(location
);
11589 Expression_list
* list
= new Expression_list
;
11590 list
->reserve(field_count
);
11591 for (size_t i
= 0; i
< field_count
; ++i
)
11592 list
->push_back(vals
[i
]);
11594 return new Struct_construction_expression(type
, list
, location
);
11597 // Lower an array composite literal.
11600 Composite_literal_expression::lower_array(Type
* type
)
11602 source_location location
= this->location();
11603 if (this->vals_
== NULL
|| !this->has_keys_
)
11604 return this->make_array(type
, this->vals_
);
11606 std::vector
<Expression
*> vals
;
11607 vals
.reserve(this->vals_
->size());
11608 unsigned long index
= 0;
11609 Expression_list::const_iterator p
= this->vals_
->begin();
11610 while (p
!= this->vals_
->end())
11612 Expression
* index_expr
= *p
;
11615 gcc_assert(p
!= this->vals_
->end());
11616 Expression
* val
= *p
;
11620 if (index_expr
!= NULL
)
11625 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11628 error_at(index_expr
->location(),
11629 "index expression is not integer constant");
11630 return Expression::make_error(location
);
11632 if (mpz_sgn(ival
) < 0)
11635 error_at(index_expr
->location(), "index expression is negative");
11636 return Expression::make_error(location
);
11638 index
= mpz_get_ui(ival
);
11639 if (mpz_cmp_ui(ival
, index
) != 0)
11642 error_at(index_expr
->location(), "index value overflow");
11643 return Expression::make_error(location
);
11648 if (index
== vals
.size())
11649 vals
.push_back(val
);
11652 if (index
> vals
.size())
11654 vals
.reserve(index
+ 32);
11655 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11657 if (vals
[index
] != NULL
)
11659 error_at((index_expr
!= NULL
11660 ? index_expr
->location()
11661 : val
->location()),
11662 "duplicate value for index %lu",
11664 return Expression::make_error(location
);
11672 size_t size
= vals
.size();
11673 Expression_list
* list
= new Expression_list
;
11674 list
->reserve(size
);
11675 for (size_t i
= 0; i
< size
; ++i
)
11676 list
->push_back(vals
[i
]);
11678 return this->make_array(type
, list
);
11681 // Actually build the array composite literal. This handles
11685 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11687 source_location location
= this->location();
11688 Array_type
* at
= type
->array_type();
11689 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11691 size_t size
= vals
== NULL
? 0 : vals
->size();
11693 mpz_init_set_ui(vlen
, size
);
11694 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11696 at
= Type::make_array_type(at
->element_type(), elen
);
11699 if (at
->length() != NULL
)
11700 return new Fixed_array_construction_expression(type
, vals
, location
);
11702 return new Open_array_construction_expression(type
, vals
, location
);
11705 // Lower a map composite literal.
11708 Composite_literal_expression::lower_map(Gogo
* gogo
, Named_object
* function
,
11711 source_location location
= this->location();
11712 if (this->vals_
!= NULL
)
11714 if (!this->has_keys_
)
11716 error_at(location
, "map composite literal must have keys");
11717 return Expression::make_error(location
);
11720 for (Expression_list::iterator p
= this->vals_
->begin();
11721 p
!= this->vals_
->end();
11727 error_at((*p
)->location(),
11728 "map composite literal must have keys for every value");
11729 return Expression::make_error(location
);
11731 // Make sure we have lowered the key; it may not have been
11732 // lowered in order to handle keys for struct composite
11733 // literals. Lower it now to get the right error message.
11734 if ((*p
)->unknown_expression() != NULL
)
11736 (*p
)->unknown_expression()->clear_is_composite_literal_key();
11737 gogo
->lower_expression(function
, &*p
);
11738 gcc_assert((*p
)->is_error_expression());
11739 return Expression::make_error(location
);
11744 return new Map_construction_expression(type
, this->vals_
, location
);
11747 // Make a composite literal expression.
11750 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11751 Expression_list
* vals
,
11752 source_location location
)
11754 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11758 // Return whether this expression is a composite literal.
11761 Expression::is_composite_literal() const
11763 switch (this->classification_
)
11765 case EXPRESSION_COMPOSITE_LITERAL
:
11766 case EXPRESSION_STRUCT_CONSTRUCTION
:
11767 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11768 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11769 case EXPRESSION_MAP_CONSTRUCTION
:
11776 // Return whether this expression is a composite literal which is not
11780 Expression::is_nonconstant_composite_literal() const
11782 switch (this->classification_
)
11784 case EXPRESSION_STRUCT_CONSTRUCTION
:
11786 const Struct_construction_expression
*psce
=
11787 static_cast<const Struct_construction_expression
*>(this);
11788 return !psce
->is_constant_struct();
11790 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11792 const Fixed_array_construction_expression
*pace
=
11793 static_cast<const Fixed_array_construction_expression
*>(this);
11794 return !pace
->is_constant_array();
11796 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11798 const Open_array_construction_expression
*pace
=
11799 static_cast<const Open_array_construction_expression
*>(this);
11800 return !pace
->is_constant_array();
11802 case EXPRESSION_MAP_CONSTRUCTION
:
11809 // Return true if this is a reference to a local variable.
11812 Expression::is_local_variable() const
11814 const Var_expression
* ve
= this->var_expression();
11817 const Named_object
* no
= ve
->named_object();
11818 return (no
->is_result_variable()
11819 || (no
->is_variable() && !no
->var_value()->is_global()));
11822 // Class Type_guard_expression.
11827 Type_guard_expression::do_traverse(Traverse
* traverse
)
11829 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
11830 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11831 return TRAVERSE_EXIT
;
11832 return TRAVERSE_CONTINUE
;
11835 // Check types of a type guard expression. The expression must have
11836 // an interface type, but the actual type conversion is checked at run
11840 Type_guard_expression::do_check_types(Gogo
*)
11842 // 6g permits using a type guard with unsafe.pointer; we are
11844 Type
* expr_type
= this->expr_
->type();
11845 if (expr_type
->is_unsafe_pointer_type())
11847 if (this->type_
->points_to() == NULL
11848 && (this->type_
->integer_type() == NULL
11849 || (this->type_
->forwarded()
11850 != Type::lookup_integer_type("uintptr"))))
11851 this->report_error(_("invalid unsafe.Pointer conversion"));
11853 else if (this->type_
->is_unsafe_pointer_type())
11855 if (expr_type
->points_to() == NULL
11856 && (expr_type
->integer_type() == NULL
11857 || (expr_type
->forwarded()
11858 != Type::lookup_integer_type("uintptr"))))
11859 this->report_error(_("invalid unsafe.Pointer conversion"));
11861 else if (expr_type
->interface_type() == NULL
)
11863 if (!expr_type
->is_error_type() && !this->type_
->is_error_type())
11864 this->report_error(_("type assertion only valid for interface types"));
11865 this->set_is_error();
11867 else if (this->type_
->interface_type() == NULL
)
11869 std::string reason
;
11870 if (!expr_type
->interface_type()->implements_interface(this->type_
,
11873 if (!this->type_
->is_error_type())
11875 if (reason
.empty())
11876 this->report_error(_("impossible type assertion: "
11877 "type does not implement interface"));
11879 error_at(this->location(),
11880 ("impossible type assertion: "
11881 "type does not implement interface (%s)"),
11884 this->set_is_error();
11889 // Return a tree for a type guard expression.
11892 Type_guard_expression::do_get_tree(Translate_context
* context
)
11894 Gogo
* gogo
= context
->gogo();
11895 tree expr_tree
= this->expr_
->get_tree(context
);
11896 if (expr_tree
== error_mark_node
)
11897 return error_mark_node
;
11898 Type
* expr_type
= this->expr_
->type();
11899 if ((this->type_
->is_unsafe_pointer_type()
11900 && (expr_type
->points_to() != NULL
11901 || expr_type
->integer_type() != NULL
))
11902 || (expr_type
->is_unsafe_pointer_type()
11903 && this->type_
->points_to() != NULL
))
11904 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
11905 else if (expr_type
->is_unsafe_pointer_type()
11906 && this->type_
->integer_type() != NULL
)
11907 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
11908 else if (this->type_
->interface_type() != NULL
)
11909 return Expression::convert_interface_to_interface(context
, this->type_
,
11910 this->expr_
->type(),
11914 return Expression::convert_for_assignment(context
, this->type_
,
11915 this->expr_
->type(), expr_tree
,
11919 // Make a type guard expression.
11922 Expression::make_type_guard(Expression
* expr
, Type
* type
,
11923 source_location location
)
11925 return new Type_guard_expression(expr
, type
, location
);
11928 // Class Heap_composite_expression.
11930 // When you take the address of a composite literal, it is allocated
11931 // on the heap. This class implements that.
11933 class Heap_composite_expression
: public Expression
11936 Heap_composite_expression(Expression
* expr
, source_location location
)
11937 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
11943 do_traverse(Traverse
* traverse
)
11944 { return Expression::traverse(&this->expr_
, traverse
); }
11948 { return Type::make_pointer_type(this->expr_
->type()); }
11951 do_determine_type(const Type_context
*)
11952 { this->expr_
->determine_type_no_context(); }
11957 return Expression::make_heap_composite(this->expr_
->copy(),
11962 do_get_tree(Translate_context
*);
11964 // We only export global objects, and the parser does not generate
11965 // this in global scope.
11967 do_export(Export
*) const
11968 { gcc_unreachable(); }
11971 // The composite literal which is being put on the heap.
11975 // Return a tree which allocates a composite literal on the heap.
11978 Heap_composite_expression::do_get_tree(Translate_context
* context
)
11980 tree expr_tree
= this->expr_
->get_tree(context
);
11981 if (expr_tree
== error_mark_node
)
11982 return error_mark_node
;
11983 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
11984 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
11985 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
11986 expr_size
, this->location());
11987 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
11988 space
= save_expr(space
);
11989 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
11990 TREE_THIS_NOTRAP(ref
) = 1;
11991 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
11992 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
11994 SET_EXPR_LOCATION(ret
, this->location());
11998 // Allocate a composite literal on the heap.
12001 Expression::make_heap_composite(Expression
* expr
, source_location location
)
12003 return new Heap_composite_expression(expr
, location
);
12006 // Class Receive_expression.
12008 // Return the type of a receive expression.
12011 Receive_expression::do_type()
12013 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12014 if (channel_type
== NULL
)
12015 return Type::make_error_type();
12016 return channel_type
->element_type();
12019 // Check types for a receive expression.
12022 Receive_expression::do_check_types(Gogo
*)
12024 Type
* type
= this->channel_
->type();
12025 if (type
->is_error_type())
12027 this->set_is_error();
12030 if (type
->channel_type() == NULL
)
12032 this->report_error(_("expected channel"));
12035 if (!type
->channel_type()->may_receive())
12037 this->report_error(_("invalid receive on send-only channel"));
12042 // Get a tree for a receive expression.
12045 Receive_expression::do_get_tree(Translate_context
* context
)
12047 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12048 gcc_assert(channel_type
!= NULL
);
12049 Type
* element_type
= channel_type
->element_type();
12050 tree element_type_tree
= element_type
->get_tree(context
->gogo());
12052 tree channel
= this->channel_
->get_tree(context
);
12053 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
12054 return error_mark_node
;
12056 return Gogo::receive_from_channel(element_type_tree
, channel
,
12057 this->for_select_
, this->location());
12060 // Make a receive expression.
12062 Receive_expression
*
12063 Expression::make_receive(Expression
* channel
, source_location location
)
12065 return new Receive_expression(channel
, location
);
12068 // Class Send_expression.
12073 Send_expression::do_traverse(Traverse
* traverse
)
12075 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
12076 return TRAVERSE_EXIT
;
12077 return Expression::traverse(&this->val_
, traverse
);
12083 Send_expression::do_type()
12085 return Type::lookup_bool_type();
12091 Send_expression::do_determine_type(const Type_context
*)
12093 this->channel_
->determine_type_no_context();
12095 Type
* type
= this->channel_
->type();
12096 Type_context subcontext
;
12097 if (type
->channel_type() != NULL
)
12098 subcontext
.type
= type
->channel_type()->element_type();
12099 this->val_
->determine_type(&subcontext
);
12105 Send_expression::do_check_types(Gogo
*)
12107 Type
* type
= this->channel_
->type();
12108 if (type
->is_error_type())
12110 this->set_is_error();
12113 Channel_type
* channel_type
= type
->channel_type();
12114 if (channel_type
== NULL
)
12116 error_at(this->location(), "left operand of %<<-%> must be channel");
12117 this->set_is_error();
12120 Type
* element_type
= channel_type
->element_type();
12121 if (element_type
!= NULL
12122 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
12124 this->report_error(_("incompatible types in send"));
12127 if (!channel_type
->may_send())
12129 this->report_error(_("invalid send on receive-only channel"));
12134 // Get a tree for a send expression.
12137 Send_expression::do_get_tree(Translate_context
* context
)
12139 tree channel
= this->channel_
->get_tree(context
);
12140 tree val
= this->val_
->get_tree(context
);
12141 if (channel
== error_mark_node
|| val
== error_mark_node
)
12142 return error_mark_node
;
12143 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
12144 val
= Expression::convert_for_assignment(context
,
12145 channel_type
->element_type(),
12146 this->val_
->type(),
12149 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
12150 this->for_select_
, this->location());
12153 // Make a send expression
12156 Expression::make_send(Expression
* channel
, Expression
* val
,
12157 source_location location
)
12159 return new Send_expression(channel
, val
, location
);
12162 // An expression which evaluates to a pointer to the type descriptor
12165 class Type_descriptor_expression
: public Expression
12168 Type_descriptor_expression(Type
* type
, source_location location
)
12169 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
12176 { return Type::make_type_descriptor_ptr_type(); }
12179 do_determine_type(const Type_context
*)
12187 do_get_tree(Translate_context
* context
)
12188 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
12191 // The type for which this is the descriptor.
12195 // Make a type descriptor expression.
12198 Expression::make_type_descriptor(Type
* type
, source_location location
)
12200 return new Type_descriptor_expression(type
, location
);
12203 // An expression which evaluates to some characteristic of a type.
12204 // This is only used to initialize fields of a type descriptor. Using
12205 // a new expression class is slightly inefficient but gives us a good
12206 // separation between the frontend and the middle-end with regard to
12207 // how types are laid out.
12209 class Type_info_expression
: public Expression
12212 Type_info_expression(Type
* type
, Type_info type_info
)
12213 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
12214 type_(type
), type_info_(type_info
)
12222 do_determine_type(const Type_context
*)
12230 do_get_tree(Translate_context
* context
);
12233 // The type for which we are getting information.
12235 // What information we want.
12236 Type_info type_info_
;
12239 // The type is chosen to match what the type descriptor struct
12243 Type_info_expression::do_type()
12245 switch (this->type_info_
)
12247 case TYPE_INFO_SIZE
:
12248 return Type::lookup_integer_type("uintptr");
12249 case TYPE_INFO_ALIGNMENT
:
12250 case TYPE_INFO_FIELD_ALIGNMENT
:
12251 return Type::lookup_integer_type("uint8");
12257 // Return type information in GENERIC.
12260 Type_info_expression::do_get_tree(Translate_context
* context
)
12262 tree type_tree
= this->type_
->get_tree(context
->gogo());
12263 if (type_tree
== error_mark_node
)
12264 return error_mark_node
;
12266 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12267 gcc_assert(val_type_tree
!= error_mark_node
);
12269 if (this->type_info_
== TYPE_INFO_SIZE
)
12270 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12271 TYPE_SIZE_UNIT(type_tree
));
12275 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12276 val
= go_type_alignment(type_tree
);
12278 val
= go_field_alignment(type_tree
);
12279 return build_int_cstu(val_type_tree
, val
);
12283 // Make a type info expression.
12286 Expression::make_type_info(Type
* type
, Type_info type_info
)
12288 return new Type_info_expression(type
, type_info
);
12291 // An expression which evaluates to the offset of a field within a
12292 // struct. This, like Type_info_expression, q.v., is only used to
12293 // initialize fields of a type descriptor.
12295 class Struct_field_offset_expression
: public Expression
12298 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12299 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12300 type_(type
), field_(field
)
12306 { return Type::lookup_integer_type("uintptr"); }
12309 do_determine_type(const Type_context
*)
12317 do_get_tree(Translate_context
* context
);
12320 // The type of the struct.
12321 Struct_type
* type_
;
12323 const Struct_field
* field_
;
12326 // Return a struct field offset in GENERIC.
12329 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12331 tree type_tree
= this->type_
->get_tree(context
->gogo());
12332 if (type_tree
== error_mark_node
)
12333 return error_mark_node
;
12335 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12336 gcc_assert(val_type_tree
!= error_mark_node
);
12338 const Struct_field_list
* fields
= this->type_
->fields();
12339 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12340 Struct_field_list::const_iterator p
;
12341 for (p
= fields
->begin();
12342 p
!= fields
->end();
12343 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12345 gcc_assert(struct_field_tree
!= NULL_TREE
);
12346 if (&*p
== this->field_
)
12349 gcc_assert(&*p
== this->field_
);
12351 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12352 byte_position(struct_field_tree
));
12355 // Make an expression for a struct field offset.
12358 Expression::make_struct_field_offset(Struct_type
* type
,
12359 const Struct_field
* field
)
12361 return new Struct_field_offset_expression(type
, field
);
12364 // An expression which evaluates to the address of an unnamed label.
12366 class Label_addr_expression
: public Expression
12369 Label_addr_expression(Label
* label
, source_location location
)
12370 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12377 { return Type::make_pointer_type(Type::make_void_type()); }
12380 do_determine_type(const Type_context
*)
12385 { return new Label_addr_expression(this->label_
, this->location()); }
12388 do_get_tree(Translate_context
*)
12389 { return this->label_
->get_addr(this->location()); }
12392 // The label whose address we are taking.
12396 // Make an expression for the address of an unnamed label.
12399 Expression::make_label_addr(Label
* label
, source_location location
)
12401 return new Label_addr_expression(label
, location
);
12404 // Import an expression. This comes at the end in order to see the
12405 // various class definitions.
12408 Expression::import_expression(Import
* imp
)
12410 int c
= imp
->peek_char();
12411 if (imp
->match_c_string("- ")
12412 || imp
->match_c_string("! ")
12413 || imp
->match_c_string("^ "))
12414 return Unary_expression::do_import(imp
);
12416 return Binary_expression::do_import(imp
);
12417 else if (imp
->match_c_string("true")
12418 || imp
->match_c_string("false"))
12419 return Boolean_expression::do_import(imp
);
12421 return String_expression::do_import(imp
);
12422 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12424 // This handles integers, floats and complex constants.
12425 return Integer_expression::do_import(imp
);
12427 else if (imp
->match_c_string("nil"))
12428 return Nil_expression::do_import(imp
);
12429 else if (imp
->match_c_string("convert"))
12430 return Type_conversion_expression::do_import(imp
);
12433 error_at(imp
->location(), "import error: expected expression");
12434 return Expression::make_error(imp
->location());
12438 // Class Expression_list.
12440 // Traverse the list.
12443 Expression_list::traverse(Traverse
* traverse
)
12445 for (Expression_list::iterator p
= this->begin();
12451 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12452 return TRAVERSE_EXIT
;
12455 return TRAVERSE_CONTINUE
;
12461 Expression_list::copy()
12463 Expression_list
* ret
= new Expression_list();
12464 for (Expression_list::iterator p
= this->begin();
12469 ret
->push_back(NULL
);
12471 ret
->push_back((*p
)->copy());
12476 // Return whether an expression list has an error expression.
12479 Expression_list::contains_error() const
12481 for (Expression_list::const_iterator p
= this->begin();
12484 if (*p
!= NULL
&& (*p
)->is_error_expression())