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 // This will panic if the interface conversion fails.
509 TREE_NOTHROW(assert_interface_decl
) = 0;
510 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
512 else if (lhs_is_empty
)
514 // A convertion to an empty interface always succeeds, and the
515 // first field is just the type descriptor of the object.
516 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),
517 "__type_descriptor") == 0);
518 gcc_assert(TREE_TYPE(field
) == TREE_TYPE(rhs_type_descriptor
));
519 elt
->value
= rhs_type_descriptor
;
523 // A conversion to a non-empty interface may fail, but unlike a
524 // type assertion converting nil will always succeed.
525 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods")
527 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
528 static tree convert_interface_decl
;
529 tree call
= Gogo::call_builtin(&convert_interface_decl
,
531 "__go_convert_interface",
534 TREE_TYPE(lhs_type_descriptor
),
536 TREE_TYPE(rhs_type_descriptor
),
537 rhs_type_descriptor
);
538 // This will panic if the interface conversion fails.
539 TREE_NOTHROW(convert_interface_decl
) = 0;
540 elt
->value
= fold_convert_loc(location
, TREE_TYPE(field
), call
);
543 // The second field is simply the object pointer.
545 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
546 field
= DECL_CHAIN(field
);
547 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
550 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
551 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
552 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
553 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
554 elt
->value
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
557 return build_constructor(lhs_type_tree
, init
);
560 // Return a tree for the conversion of an interface type to a
561 // non-interface type.
564 Expression::convert_interface_to_type(Translate_context
* context
,
565 Type
*lhs_type
, Type
* rhs_type
,
566 tree rhs_tree
, source_location location
)
568 Gogo
* gogo
= context
->gogo();
569 tree rhs_type_tree
= TREE_TYPE(rhs_tree
);
571 tree lhs_type_tree
= lhs_type
->get_tree(gogo
);
572 if (lhs_type_tree
== error_mark_node
)
573 return error_mark_node
;
575 // Call a function to check that the type is valid. The function
576 // will panic with an appropriate runtime type error if the type is
579 tree lhs_type_descriptor
= lhs_type
->type_descriptor_pointer(gogo
);
581 if (!DECL_P(rhs_tree
))
582 rhs_tree
= save_expr(rhs_tree
);
584 tree rhs_type_descriptor
=
585 Expression::get_interface_type_descriptor(context
, rhs_type
, rhs_tree
,
588 tree rhs_inter_descriptor
= rhs_type
->type_descriptor_pointer(gogo
);
590 static tree check_interface_type_decl
;
591 tree call
= Gogo::call_builtin(&check_interface_type_decl
,
593 "__go_check_interface_type",
596 TREE_TYPE(lhs_type_descriptor
),
598 TREE_TYPE(rhs_type_descriptor
),
600 TREE_TYPE(rhs_inter_descriptor
),
601 rhs_inter_descriptor
);
602 // This call will panic if the conversion is invalid.
603 TREE_NOTHROW(check_interface_type_decl
) = 0;
605 // If the call succeeds, pull out the value.
606 gcc_assert(TREE_CODE(rhs_type_tree
) == RECORD_TYPE
);
607 tree rhs_field
= DECL_CHAIN(TYPE_FIELDS(rhs_type_tree
));
608 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field
)), "__object") == 0);
609 tree val
= build3(COMPONENT_REF
, TREE_TYPE(rhs_field
), rhs_tree
, rhs_field
,
612 // If the value is a pointer, then it is the value we want.
613 // Otherwise it points to the value.
614 if (lhs_type
->points_to() == NULL
)
616 val
= fold_convert_loc(location
, build_pointer_type(lhs_type_tree
), val
);
617 val
= build_fold_indirect_ref_loc(location
, val
);
620 return build2(COMPOUND_EXPR
, lhs_type_tree
, call
,
621 fold_convert_loc(location
, lhs_type_tree
, val
));
624 // Convert an expression to a tree. This is implemented by the child
625 // class. Not that it is not in general safe to call this multiple
626 // times for a single expression, but that we don't catch such errors.
629 Expression::get_tree(Translate_context
* context
)
631 // The child may have marked this expression as having an error.
632 if (this->classification_
== EXPRESSION_ERROR
)
633 return error_mark_node
;
635 return this->do_get_tree(context
);
638 // Return a tree for VAL in TYPE.
641 Expression::integer_constant_tree(mpz_t val
, tree type
)
643 if (type
== error_mark_node
)
644 return error_mark_node
;
645 else if (TREE_CODE(type
) == INTEGER_TYPE
)
646 return double_int_to_tree(type
,
647 mpz_get_double_int(type
, val
, true));
648 else if (TREE_CODE(type
) == REAL_TYPE
)
651 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
652 tree ret
= Expression::float_constant_tree(fval
, type
);
656 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
659 mpfr_init_set_z(fval
, val
, GMP_RNDN
);
660 tree real
= Expression::float_constant_tree(fval
, TREE_TYPE(type
));
662 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
664 return build_complex(type
, real
, imag
);
670 // Return a tree for VAL in TYPE.
673 Expression::float_constant_tree(mpfr_t val
, tree type
)
675 if (type
== error_mark_node
)
676 return error_mark_node
;
677 else if (TREE_CODE(type
) == INTEGER_TYPE
)
681 mpfr_get_z(ival
, val
, GMP_RNDN
);
682 tree ret
= Expression::integer_constant_tree(ival
, type
);
686 else if (TREE_CODE(type
) == REAL_TYPE
)
689 real_from_mpfr(&r1
, val
, type
, GMP_RNDN
);
691 real_convert(&r2
, TYPE_MODE(type
), &r1
);
692 return build_real(type
, r2
);
694 else if (TREE_CODE(type
) == COMPLEX_TYPE
)
697 real_from_mpfr(&r1
, val
, TREE_TYPE(type
), GMP_RNDN
);
699 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
700 tree imag
= build_real_from_int_cst(TREE_TYPE(type
),
702 return build_complex(type
, build_real(TREE_TYPE(type
), r2
), imag
);
708 // Return a tree for REAL/IMAG in TYPE.
711 Expression::complex_constant_tree(mpfr_t real
, mpfr_t imag
, tree type
)
713 if (TREE_CODE(type
) == COMPLEX_TYPE
)
716 real_from_mpfr(&r1
, real
, TREE_TYPE(type
), GMP_RNDN
);
718 real_convert(&r2
, TYPE_MODE(TREE_TYPE(type
)), &r1
);
721 real_from_mpfr(&r3
, imag
, TREE_TYPE(type
), GMP_RNDN
);
723 real_convert(&r4
, TYPE_MODE(TREE_TYPE(type
)), &r3
);
725 return build_complex(type
, build_real(TREE_TYPE(type
), r2
),
726 build_real(TREE_TYPE(type
), r4
));
732 // Return a tree which evaluates to true if VAL, of arbitrary integer
733 // type, is negative or is more than the maximum value of BOUND_TYPE.
734 // If SOFAR is not NULL, it is or'red into the result. The return
735 // value may be NULL if SOFAR is NULL.
738 Expression::check_bounds(tree val
, tree bound_type
, tree sofar
,
741 tree val_type
= TREE_TYPE(val
);
742 tree ret
= NULL_TREE
;
744 if (!TYPE_UNSIGNED(val_type
))
746 ret
= fold_build2_loc(loc
, LT_EXPR
, boolean_type_node
, val
,
747 build_int_cst(val_type
, 0));
748 if (ret
== boolean_false_node
)
752 if ((TYPE_UNSIGNED(val_type
) && !TYPE_UNSIGNED(bound_type
))
753 || TYPE_SIZE(val_type
) > TYPE_SIZE(bound_type
))
755 tree max
= TYPE_MAX_VALUE(bound_type
);
756 tree big
= fold_build2_loc(loc
, GT_EXPR
, boolean_type_node
, val
,
757 fold_convert_loc(loc
, val_type
, max
));
758 if (big
== boolean_false_node
)
760 else if (ret
== NULL_TREE
)
763 ret
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
767 if (ret
== NULL_TREE
)
769 else if (sofar
== NULL_TREE
)
772 return fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
776 // Error expressions. This are used to avoid cascading errors.
778 class Error_expression
: public Expression
781 Error_expression(source_location location
)
782 : Expression(EXPRESSION_ERROR
, location
)
787 do_is_constant() const
791 do_integer_constant_value(bool, mpz_t val
, Type
**) const
798 do_float_constant_value(mpfr_t val
, Type
**) const
800 mpfr_set_ui(val
, 0, GMP_RNDN
);
805 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const
807 mpfr_set_ui(real
, 0, GMP_RNDN
);
808 mpfr_set_ui(imag
, 0, GMP_RNDN
);
813 do_discarding_value()
818 { return Type::make_error_type(); }
821 do_determine_type(const Type_context
*)
829 do_is_addressable() const
833 do_get_tree(Translate_context
*)
834 { return error_mark_node
; }
838 Expression::make_error(source_location location
)
840 return new Error_expression(location
);
843 // An expression which is really a type. This is used during parsing.
844 // It is an error if these survive after lowering.
847 Type_expression
: public Expression
850 Type_expression(Type
* type
, source_location location
)
851 : Expression(EXPRESSION_TYPE
, location
),
857 do_traverse(Traverse
* traverse
)
858 { return Type::traverse(this->type_
, traverse
); }
862 { return this->type_
; }
865 do_determine_type(const Type_context
*)
869 do_check_types(Gogo
*)
870 { this->report_error(_("invalid use of type")); }
877 do_get_tree(Translate_context
*)
878 { gcc_unreachable(); }
881 // The type which we are representing as an expression.
886 Expression::make_type(Type
* type
, source_location location
)
888 return new Type_expression(type
, location
);
891 // Class Var_expression.
893 // Lower a variable expression. Here we just make sure that the
894 // initialization expression of the variable has been lowered. This
895 // ensures that we will be able to determine the type of the variable
899 Var_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
901 if (this->variable_
->is_variable())
903 Variable
* var
= this->variable_
->var_value();
904 // This is either a local variable or a global variable. A
905 // reference to a variable which is local to an enclosing
906 // function will be a reference to a field in a closure.
907 if (var
->is_global())
909 var
->lower_init_expression(gogo
, function
);
914 // Return the name of the variable.
917 Var_expression::name() const
919 return this->variable_
->name();
922 // Return the type of a reference to a variable.
925 Var_expression::do_type()
927 if (this->variable_
->is_variable())
928 return this->variable_
->var_value()->type();
929 else if (this->variable_
->is_result_variable())
930 return this->variable_
->result_var_value()->type();
935 // Something takes the address of this variable. This means that we
936 // may want to move the variable onto the heap.
939 Var_expression::do_address_taken(bool escapes
)
943 else if (this->variable_
->is_variable())
944 this->variable_
->var_value()->set_address_taken();
945 else if (this->variable_
->is_result_variable())
946 this->variable_
->result_var_value()->set_address_taken();
951 // Get the tree for a reference to a variable.
954 Var_expression::do_get_tree(Translate_context
* context
)
956 return this->variable_
->get_tree(context
->gogo(), context
->function());
959 // Make a reference to a variable in an expression.
962 Expression::make_var_reference(Named_object
* var
, source_location location
)
965 return Expression::make_sink(location
);
967 // FIXME: Creating a new object for each reference to a variable is
969 return new Var_expression(var
, location
);
972 // Class Temporary_reference_expression.
977 Temporary_reference_expression::do_type()
979 return this->statement_
->type();
982 // Called if something takes the address of this temporary variable.
983 // We never have to move temporary variables to the heap, but we do
984 // need to know that they must live in the stack rather than in a
988 Temporary_reference_expression::do_address_taken(bool)
990 this->statement_
->set_is_address_taken();
993 // Get a tree referring to the variable.
996 Temporary_reference_expression::do_get_tree(Translate_context
*)
998 return this->statement_
->get_decl();
1001 // Make a reference to a temporary variable.
1004 Expression::make_temporary_reference(Temporary_statement
* statement
,
1005 source_location location
)
1007 return new Temporary_reference_expression(statement
, location
);
1010 // A sink expression--a use of the blank identifier _.
1012 class Sink_expression
: public Expression
1015 Sink_expression(source_location location
)
1016 : Expression(EXPRESSION_SINK
, location
),
1017 type_(NULL
), var_(NULL_TREE
)
1022 do_discarding_value()
1029 do_determine_type(const Type_context
*);
1033 { return new Sink_expression(this->location()); }
1036 do_get_tree(Translate_context
*);
1039 // The type of this sink variable.
1041 // The temporary variable we generate.
1045 // Return the type of a sink expression.
1048 Sink_expression::do_type()
1050 if (this->type_
== NULL
)
1051 return Type::make_sink_type();
1055 // Determine the type of a sink expression.
1058 Sink_expression::do_determine_type(const Type_context
* context
)
1060 if (context
->type
!= NULL
)
1061 this->type_
= context
->type
;
1064 // Return a temporary variable for a sink expression. This will
1065 // presumably be a write-only variable which the middle-end will drop.
1068 Sink_expression::do_get_tree(Translate_context
* context
)
1070 if (this->var_
== NULL_TREE
)
1072 gcc_assert(this->type_
!= NULL
&& !this->type_
->is_sink_type());
1073 this->var_
= create_tmp_var(this->type_
->get_tree(context
->gogo()),
1079 // Make a sink expression.
1082 Expression::make_sink(source_location location
)
1084 return new Sink_expression(location
);
1087 // Class Func_expression.
1089 // FIXME: Can a function expression appear in a constant expression?
1090 // The value is unchanging. Initializing a constant to the address of
1091 // a function seems like it could work, though there might be little
1094 // Return the name of the function.
1097 Func_expression::name() const
1099 return this->function_
->name();
1105 Func_expression::do_traverse(Traverse
* traverse
)
1107 return (this->closure_
== NULL
1109 : Expression::traverse(&this->closure_
, traverse
));
1112 // Return the type of a function expression.
1115 Func_expression::do_type()
1117 if (this->function_
->is_function())
1118 return this->function_
->func_value()->type();
1119 else if (this->function_
->is_function_declaration())
1120 return this->function_
->func_declaration_value()->type();
1125 // Get the tree for a function expression without evaluating the
1129 Func_expression::get_tree_without_closure(Gogo
* gogo
)
1131 Function_type
* fntype
;
1132 if (this->function_
->is_function())
1133 fntype
= this->function_
->func_value()->type();
1134 else if (this->function_
->is_function_declaration())
1135 fntype
= this->function_
->func_declaration_value()->type();
1139 // Builtin functions are handled specially by Call_expression. We
1140 // can't take their address.
1141 if (fntype
->is_builtin())
1143 error_at(this->location(), "invalid use of special builtin function %qs",
1144 this->function_
->name().c_str());
1145 return error_mark_node
;
1148 Named_object
* no
= this->function_
;
1149 tree id
= this->function_
->get_id(gogo
);
1151 if (no
->is_function())
1152 fndecl
= no
->func_value()->get_or_make_decl(gogo
, no
, id
);
1153 else if (no
->is_function_declaration())
1154 fndecl
= no
->func_declaration_value()->get_or_make_decl(gogo
, no
, id
);
1158 return build_fold_addr_expr_loc(this->location(), fndecl
);
1161 // Get the tree for a function expression. This is used when we take
1162 // the address of a function rather than simply calling it. If the
1163 // function has a closure, we must use a trampoline.
1166 Func_expression::do_get_tree(Translate_context
* context
)
1168 Gogo
* gogo
= context
->gogo();
1170 tree fnaddr
= this->get_tree_without_closure(gogo
);
1171 if (fnaddr
== error_mark_node
)
1172 return error_mark_node
;
1174 gcc_assert(TREE_CODE(fnaddr
) == ADDR_EXPR
1175 && TREE_CODE(TREE_OPERAND(fnaddr
, 0)) == FUNCTION_DECL
);
1176 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr
, 0)) = 1;
1178 // For a normal non-nested function call, that is all we have to do.
1179 if (!this->function_
->is_function()
1180 || this->function_
->func_value()->enclosing() == NULL
)
1182 gcc_assert(this->closure_
== NULL
);
1186 // For a nested function call, we have to always allocate a
1187 // trampoline. If we don't always allocate, then closures will not
1188 // be reliably distinct.
1189 Expression
* closure
= this->closure_
;
1191 if (closure
== NULL
)
1192 closure_tree
= null_pointer_node
;
1195 // Get the value of the closure. This will be a pointer to
1196 // space allocated on the heap.
1197 closure_tree
= closure
->get_tree(context
);
1198 if (closure_tree
== error_mark_node
)
1199 return error_mark_node
;
1200 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree
)));
1203 // Now we need to build some code on the heap. This code will load
1204 // the static chain pointer with the closure and then jump to the
1205 // body of the function. The normal gcc approach is to build the
1206 // code on the stack. Unfortunately we can not do that, as Go
1207 // permits us to return the function pointer.
1209 return gogo
->make_trampoline(fnaddr
, closure_tree
, this->location());
1212 // Make a reference to a function in an expression.
1215 Expression::make_func_reference(Named_object
* function
, Expression
* closure
,
1216 source_location location
)
1218 return new Func_expression(function
, closure
, location
);
1221 // Class Unknown_expression.
1223 // Return the name of an unknown expression.
1226 Unknown_expression::name() const
1228 return this->named_object_
->name();
1231 // Lower a reference to an unknown name.
1234 Unknown_expression::do_lower(Gogo
*, Named_object
*, int)
1236 source_location location
= this->location();
1237 Named_object
* no
= this->named_object_
;
1238 Named_object
* real
= no
->unknown_value()->real_named_object();
1241 if (this->is_composite_literal_key_
)
1243 error_at(location
, "reference to undefined name %qs",
1244 this->named_object_
->message_name().c_str());
1245 return Expression::make_error(location
);
1247 switch (real
->classification())
1249 case Named_object::NAMED_OBJECT_CONST
:
1250 return Expression::make_const_reference(real
, location
);
1251 case Named_object::NAMED_OBJECT_TYPE
:
1252 return Expression::make_type(real
->type_value(), location
);
1253 case Named_object::NAMED_OBJECT_TYPE_DECLARATION
:
1254 if (this->is_composite_literal_key_
)
1256 error_at(location
, "reference to undefined type %qs",
1257 real
->message_name().c_str());
1258 return Expression::make_error(location
);
1259 case Named_object::NAMED_OBJECT_VAR
:
1260 return Expression::make_var_reference(real
, location
);
1261 case Named_object::NAMED_OBJECT_FUNC
:
1262 case Named_object::NAMED_OBJECT_FUNC_DECLARATION
:
1263 return Expression::make_func_reference(real
, NULL
, location
);
1264 case Named_object::NAMED_OBJECT_PACKAGE
:
1265 if (this->is_composite_literal_key_
)
1267 error_at(location
, "unexpected reference to package");
1268 return Expression::make_error(location
);
1274 // Make a reference to an unknown name.
1277 Expression::make_unknown_reference(Named_object
* no
, source_location location
)
1279 gcc_assert(no
->resolve()->is_unknown());
1280 return new Unknown_expression(no
, location
);
1283 // A boolean expression.
1285 class Boolean_expression
: public Expression
1288 Boolean_expression(bool val
, source_location location
)
1289 : Expression(EXPRESSION_BOOLEAN
, location
),
1290 val_(val
), type_(NULL
)
1298 do_is_constant() const
1305 do_determine_type(const Type_context
*);
1312 do_get_tree(Translate_context
*)
1313 { return this->val_
? boolean_true_node
: boolean_false_node
; }
1316 do_export(Export
* exp
) const
1317 { exp
->write_c_string(this->val_
? "true" : "false"); }
1322 // The type as determined by context.
1329 Boolean_expression::do_type()
1331 if (this->type_
== NULL
)
1332 this->type_
= Type::make_boolean_type();
1336 // Set the type from the context.
1339 Boolean_expression::do_determine_type(const Type_context
* context
)
1341 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1343 else if (context
->type
!= NULL
&& context
->type
->is_boolean_type())
1344 this->type_
= context
->type
;
1345 else if (!context
->may_be_abstract
)
1346 this->type_
= Type::lookup_bool_type();
1349 // Import a boolean constant.
1352 Boolean_expression::do_import(Import
* imp
)
1354 if (imp
->peek_char() == 't')
1356 imp
->require_c_string("true");
1357 return Expression::make_boolean(true, imp
->location());
1361 imp
->require_c_string("false");
1362 return Expression::make_boolean(false, imp
->location());
1366 // Make a boolean expression.
1369 Expression::make_boolean(bool val
, source_location location
)
1371 return new Boolean_expression(val
, location
);
1374 // Class String_expression.
1379 String_expression::do_type()
1381 if (this->type_
== NULL
)
1382 this->type_
= Type::make_string_type();
1386 // Set the type from the context.
1389 String_expression::do_determine_type(const Type_context
* context
)
1391 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1393 else if (context
->type
!= NULL
&& context
->type
->is_string_type())
1394 this->type_
= context
->type
;
1395 else if (!context
->may_be_abstract
)
1396 this->type_
= Type::lookup_string_type();
1399 // Build a string constant.
1402 String_expression::do_get_tree(Translate_context
* context
)
1404 return context
->gogo()->go_string_constant_tree(this->val_
);
1407 // Export a string expression.
1410 String_expression::do_export(Export
* exp
) const
1413 s
.reserve(this->val_
.length() * 4 + 2);
1415 for (std::string::const_iterator p
= this->val_
.begin();
1416 p
!= this->val_
.end();
1419 if (*p
== '\\' || *p
== '"')
1424 else if (*p
>= 0x20 && *p
< 0x7f)
1426 else if (*p
== '\n')
1428 else if (*p
== '\t')
1433 unsigned char c
= *p
;
1434 unsigned int dig
= c
>> 4;
1435 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1437 s
+= dig
< 10 ? '0' + dig
: 'A' + dig
- 10;
1441 exp
->write_string(s
);
1444 // Import a string expression.
1447 String_expression::do_import(Import
* imp
)
1449 imp
->require_c_string("\"");
1453 int c
= imp
->get_char();
1454 if (c
== '"' || c
== -1)
1457 val
+= static_cast<char>(c
);
1460 c
= imp
->get_char();
1461 if (c
== '\\' || c
== '"')
1462 val
+= static_cast<char>(c
);
1469 c
= imp
->get_char();
1470 unsigned int vh
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1471 c
= imp
->get_char();
1472 unsigned int vl
= c
>= '0' && c
<= '9' ? c
- '0' : c
- 'A' + 10;
1473 char v
= (vh
<< 4) | vl
;
1478 error_at(imp
->location(), "bad string constant");
1479 return Expression::make_error(imp
->location());
1483 return Expression::make_string(val
, imp
->location());
1486 // Make a string expression.
1489 Expression::make_string(const std::string
& val
, source_location location
)
1491 return new String_expression(val
, location
);
1494 // Make an integer expression.
1496 class Integer_expression
: public Expression
1499 Integer_expression(const mpz_t
* val
, Type
* type
, source_location location
)
1500 : Expression(EXPRESSION_INTEGER
, location
),
1502 { mpz_init_set(this->val_
, *val
); }
1507 // Return whether VAL fits in the type.
1509 check_constant(mpz_t val
, Type
*, source_location
);
1511 // Write VAL to export data.
1513 export_integer(Export
* exp
, const mpz_t val
);
1517 do_is_constant() const
1521 do_integer_constant_value(bool, mpz_t val
, Type
** ptype
) const;
1527 do_determine_type(const Type_context
* context
);
1530 do_check_types(Gogo
*);
1533 do_get_tree(Translate_context
*);
1537 { return Expression::make_integer(&this->val_
, this->type_
,
1538 this->location()); }
1541 do_export(Export
*) const;
1544 // The integer value.
1550 // Return an integer constant value.
1553 Integer_expression::do_integer_constant_value(bool, mpz_t val
,
1556 if (this->type_
!= NULL
)
1557 *ptype
= this->type_
;
1558 mpz_set(val
, this->val_
);
1562 // Return the current type. If we haven't set the type yet, we return
1563 // an abstract integer type.
1566 Integer_expression::do_type()
1568 if (this->type_
== NULL
)
1569 this->type_
= Type::make_abstract_integer_type();
1573 // Set the type of the integer value. Here we may switch from an
1574 // abstract type to a real type.
1577 Integer_expression::do_determine_type(const Type_context
* context
)
1579 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1581 else if (context
->type
!= NULL
1582 && (context
->type
->integer_type() != NULL
1583 || context
->type
->float_type() != NULL
1584 || context
->type
->complex_type() != NULL
))
1585 this->type_
= context
->type
;
1586 else if (!context
->may_be_abstract
)
1587 this->type_
= Type::lookup_integer_type("int");
1590 // Return true if the integer VAL fits in the range of the type TYPE.
1591 // Otherwise give an error and return false. TYPE may be NULL.
1594 Integer_expression::check_constant(mpz_t val
, Type
* type
,
1595 source_location location
)
1599 Integer_type
* itype
= type
->integer_type();
1600 if (itype
== NULL
|| itype
->is_abstract())
1603 int bits
= mpz_sizeinbase(val
, 2);
1605 if (itype
->is_unsigned())
1607 // For an unsigned type we can only accept a nonnegative number,
1608 // and we must be able to represent at least BITS.
1609 if (mpz_sgn(val
) >= 0
1610 && bits
<= itype
->bits())
1615 // For a signed type we need an extra bit to indicate the sign.
1616 // We have to handle the most negative integer specially.
1617 if (bits
+ 1 <= itype
->bits()
1618 || (bits
<= itype
->bits()
1620 && (mpz_scan1(val
, 0)
1621 == static_cast<unsigned long>(itype
->bits() - 1))
1622 && mpz_scan0(val
, itype
->bits()) == ULONG_MAX
))
1626 error_at(location
, "integer constant overflow");
1630 // Check the type of an integer constant.
1633 Integer_expression::do_check_types(Gogo
*)
1635 if (this->type_
== NULL
)
1637 if (!Integer_expression::check_constant(this->val_
, this->type_
,
1639 this->set_is_error();
1642 // Get a tree for an integer constant.
1645 Integer_expression::do_get_tree(Translate_context
* context
)
1647 Gogo
* gogo
= context
->gogo();
1649 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1650 type
= this->type_
->get_tree(gogo
);
1651 else if (this->type_
!= NULL
&& this->type_
->float_type() != NULL
)
1653 // We are converting to an abstract floating point type.
1654 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
1656 else if (this->type_
!= NULL
&& this->type_
->complex_type() != NULL
)
1658 // We are converting to an abstract complex type.
1659 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
1663 // If we still have an abstract type here, then this is being
1664 // used in a constant expression which didn't get reduced for
1665 // some reason. Use a type which will fit the value. We use <,
1666 // not <=, because we need an extra bit for the sign bit.
1667 int bits
= mpz_sizeinbase(this->val_
, 2);
1668 if (bits
< INT_TYPE_SIZE
)
1669 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
1671 type
= Type::lookup_integer_type("int64")->get_tree(gogo
);
1673 type
= long_long_integer_type_node
;
1675 return Expression::integer_constant_tree(this->val_
, type
);
1678 // Write VAL to export data.
1681 Integer_expression::export_integer(Export
* exp
, const mpz_t val
)
1683 char* s
= mpz_get_str(NULL
, 10, val
);
1684 exp
->write_c_string(s
);
1688 // Export an integer in a constant expression.
1691 Integer_expression::do_export(Export
* exp
) const
1693 Integer_expression::export_integer(exp
, this->val_
);
1694 // A trailing space lets us reliably identify the end of the number.
1695 exp
->write_c_string(" ");
1698 // Import an integer, floating point, or complex value. This handles
1699 // all these types because they all start with digits.
1702 Integer_expression::do_import(Import
* imp
)
1704 std::string num
= imp
->read_identifier();
1705 imp
->require_c_string(" ");
1706 if (!num
.empty() && num
[num
.length() - 1] == 'i')
1709 size_t plus_pos
= num
.find('+', 1);
1710 size_t minus_pos
= num
.find('-', 1);
1712 if (plus_pos
== std::string::npos
)
1714 else if (minus_pos
== std::string::npos
)
1718 error_at(imp
->location(), "bad number in import data: %qs",
1720 return Expression::make_error(imp
->location());
1722 if (pos
== std::string::npos
)
1723 mpfr_set_ui(real
, 0, GMP_RNDN
);
1726 std::string real_str
= num
.substr(0, pos
);
1727 if (mpfr_init_set_str(real
, real_str
.c_str(), 10, GMP_RNDN
) != 0)
1729 error_at(imp
->location(), "bad number in import data: %qs",
1731 return Expression::make_error(imp
->location());
1735 std::string imag_str
;
1736 if (pos
== std::string::npos
)
1739 imag_str
= num
.substr(pos
);
1740 imag_str
= imag_str
.substr(0, imag_str
.size() - 1);
1742 if (mpfr_init_set_str(imag
, imag_str
.c_str(), 10, GMP_RNDN
) != 0)
1744 error_at(imp
->location(), "bad number in import data: %qs",
1746 return Expression::make_error(imp
->location());
1748 Expression
* ret
= Expression::make_complex(&real
, &imag
, NULL
,
1754 else if (num
.find('.') == std::string::npos
1755 && num
.find('E') == std::string::npos
)
1758 if (mpz_init_set_str(val
, num
.c_str(), 10) != 0)
1760 error_at(imp
->location(), "bad number in import data: %qs",
1762 return Expression::make_error(imp
->location());
1764 Expression
* ret
= Expression::make_integer(&val
, NULL
, imp
->location());
1771 if (mpfr_init_set_str(val
, num
.c_str(), 10, GMP_RNDN
) != 0)
1773 error_at(imp
->location(), "bad number in import data: %qs",
1775 return Expression::make_error(imp
->location());
1777 Expression
* ret
= Expression::make_float(&val
, NULL
, imp
->location());
1783 // Build a new integer value.
1786 Expression::make_integer(const mpz_t
* val
, Type
* type
,
1787 source_location location
)
1789 return new Integer_expression(val
, type
, location
);
1794 class Float_expression
: public Expression
1797 Float_expression(const mpfr_t
* val
, Type
* type
, source_location location
)
1798 : Expression(EXPRESSION_FLOAT
, location
),
1801 mpfr_init_set(this->val_
, *val
, GMP_RNDN
);
1804 // Constrain VAL to fit into TYPE.
1806 constrain_float(mpfr_t val
, Type
* type
);
1808 // Return whether VAL fits in the type.
1810 check_constant(mpfr_t val
, Type
*, source_location
);
1812 // Write VAL to export data.
1814 export_float(Export
* exp
, const mpfr_t val
);
1818 do_is_constant() const
1822 do_float_constant_value(mpfr_t val
, Type
**) const;
1828 do_determine_type(const Type_context
*);
1831 do_check_types(Gogo
*);
1835 { return Expression::make_float(&this->val_
, this->type_
,
1836 this->location()); }
1839 do_get_tree(Translate_context
*);
1842 do_export(Export
*) const;
1845 // The floating point value.
1851 // Constrain VAL to fit into TYPE.
1854 Float_expression::constrain_float(mpfr_t val
, Type
* type
)
1856 Float_type
* ftype
= type
->float_type();
1857 if (ftype
!= NULL
&& !ftype
->is_abstract())
1859 tree type_tree
= ftype
->type_tree();
1860 REAL_VALUE_TYPE rvt
;
1861 real_from_mpfr(&rvt
, val
, type_tree
, GMP_RNDN
);
1862 real_convert(&rvt
, TYPE_MODE(type_tree
), &rvt
);
1863 mpfr_from_real(val
, &rvt
, GMP_RNDN
);
1867 // Return a floating point constant value.
1870 Float_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
1872 if (this->type_
!= NULL
)
1873 *ptype
= this->type_
;
1874 mpfr_set(val
, this->val_
, GMP_RNDN
);
1878 // Return the current type. If we haven't set the type yet, we return
1879 // an abstract float type.
1882 Float_expression::do_type()
1884 if (this->type_
== NULL
)
1885 this->type_
= Type::make_abstract_float_type();
1889 // Set the type of the float value. Here we may switch from an
1890 // abstract type to a real type.
1893 Float_expression::do_determine_type(const Type_context
* context
)
1895 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1897 else if (context
->type
!= NULL
1898 && (context
->type
->integer_type() != NULL
1899 || context
->type
->float_type() != NULL
1900 || context
->type
->complex_type() != NULL
))
1901 this->type_
= context
->type
;
1902 else if (!context
->may_be_abstract
)
1903 this->type_
= Type::lookup_float_type("float");
1906 // Return true if the floating point value VAL fits in the range of
1907 // the type TYPE. Otherwise give an error and return false. TYPE may
1911 Float_expression::check_constant(mpfr_t val
, Type
* type
,
1912 source_location location
)
1916 Float_type
* ftype
= type
->float_type();
1917 if (ftype
== NULL
|| ftype
->is_abstract())
1920 // A NaN or Infinity always fits in the range of the type.
1921 if (mpfr_nan_p(val
) || mpfr_inf_p(val
) || mpfr_zero_p(val
))
1924 mp_exp_t exp
= mpfr_get_exp(val
);
1926 switch (ftype
->bits())
1939 error_at(location
, "floating point constant overflow");
1945 // Check the type of a float value.
1948 Float_expression::do_check_types(Gogo
*)
1950 if (this->type_
== NULL
)
1953 if (!Float_expression::check_constant(this->val_
, this->type_
,
1955 this->set_is_error();
1957 Integer_type
* integer_type
= this->type_
->integer_type();
1958 if (integer_type
!= NULL
)
1960 if (!mpfr_integer_p(this->val_
))
1961 this->report_error(_("floating point constant truncated to integer"));
1964 gcc_assert(!integer_type
->is_abstract());
1967 mpfr_get_z(ival
, this->val_
, GMP_RNDN
);
1968 Integer_expression::check_constant(ival
, integer_type
,
1975 // Get a tree for a float constant.
1978 Float_expression::do_get_tree(Translate_context
* context
)
1980 Gogo
* gogo
= context
->gogo();
1982 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
1983 type
= this->type_
->get_tree(gogo
);
1984 else if (this->type_
!= NULL
&& this->type_
->integer_type() != NULL
)
1986 // We have an abstract integer type. We just hope for the best.
1987 type
= Type::lookup_integer_type("int")->get_tree(gogo
);
1991 // If we still have an abstract type here, then this is being
1992 // used in a constant expression which didn't get reduced. We
1993 // just use float64 and hope for the best.
1994 type
= Type::lookup_float_type("float64")->get_tree(gogo
);
1996 return Expression::float_constant_tree(this->val_
, type
);
1999 // Write a floating point number to export data.
2002 Float_expression::export_float(Export
*exp
, const mpfr_t val
)
2005 char* s
= mpfr_get_str(NULL
, &exponent
, 10, 0, val
, GMP_RNDN
);
2007 exp
->write_c_string("-");
2008 exp
->write_c_string("0.");
2009 exp
->write_c_string(*s
== '-' ? s
+ 1 : s
);
2012 snprintf(buf
, sizeof buf
, "E%ld", exponent
);
2013 exp
->write_c_string(buf
);
2016 // Export a floating point number in a constant expression.
2019 Float_expression::do_export(Export
* exp
) const
2021 Float_expression::export_float(exp
, this->val_
);
2022 // A trailing space lets us reliably identify the end of the number.
2023 exp
->write_c_string(" ");
2026 // Make a float expression.
2029 Expression::make_float(const mpfr_t
* val
, Type
* type
, source_location location
)
2031 return new Float_expression(val
, type
, location
);
2036 class Complex_expression
: public Expression
2039 Complex_expression(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2040 source_location location
)
2041 : Expression(EXPRESSION_COMPLEX
, location
),
2044 mpfr_init_set(this->real_
, *real
, GMP_RNDN
);
2045 mpfr_init_set(this->imag_
, *imag
, GMP_RNDN
);
2048 // Constrain REAL/IMAG to fit into TYPE.
2050 constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
);
2052 // Return whether REAL/IMAG fits in the type.
2054 check_constant(mpfr_t real
, mpfr_t imag
, Type
*, source_location
);
2056 // Write REAL/IMAG to export data.
2058 export_complex(Export
* exp
, const mpfr_t real
, const mpfr_t val
);
2062 do_is_constant() const
2066 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2072 do_determine_type(const Type_context
*);
2075 do_check_types(Gogo
*);
2080 return Expression::make_complex(&this->real_
, &this->imag_
, this->type_
,
2085 do_get_tree(Translate_context
*);
2088 do_export(Export
*) const;
2093 // The imaginary part;
2095 // The type if known.
2099 // Constrain REAL/IMAG to fit into TYPE.
2102 Complex_expression::constrain_complex(mpfr_t real
, mpfr_t imag
, Type
* type
)
2104 Complex_type
* ctype
= type
->complex_type();
2105 if (ctype
!= NULL
&& !ctype
->is_abstract())
2107 tree type_tree
= ctype
->type_tree();
2109 REAL_VALUE_TYPE rvt
;
2110 real_from_mpfr(&rvt
, real
, TREE_TYPE(type_tree
), GMP_RNDN
);
2111 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2112 mpfr_from_real(real
, &rvt
, GMP_RNDN
);
2114 real_from_mpfr(&rvt
, imag
, TREE_TYPE(type_tree
), GMP_RNDN
);
2115 real_convert(&rvt
, TYPE_MODE(TREE_TYPE(type_tree
)), &rvt
);
2116 mpfr_from_real(imag
, &rvt
, GMP_RNDN
);
2120 // Return a complex constant value.
2123 Complex_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2126 if (this->type_
!= NULL
)
2127 *ptype
= this->type_
;
2128 mpfr_set(real
, this->real_
, GMP_RNDN
);
2129 mpfr_set(imag
, this->imag_
, GMP_RNDN
);
2133 // Return the current type. If we haven't set the type yet, we return
2134 // an abstract complex type.
2137 Complex_expression::do_type()
2139 if (this->type_
== NULL
)
2140 this->type_
= Type::make_abstract_complex_type();
2144 // Set the type of the complex value. Here we may switch from an
2145 // abstract type to a real type.
2148 Complex_expression::do_determine_type(const Type_context
* context
)
2150 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2152 else if (context
->type
!= NULL
2153 && context
->type
->complex_type() != NULL
)
2154 this->type_
= context
->type
;
2155 else if (!context
->may_be_abstract
)
2156 this->type_
= Type::lookup_complex_type("complex");
2159 // Return true if the complex value REAL/IMAG fits in the range of the
2160 // type TYPE. Otherwise give an error and return false. TYPE may be
2164 Complex_expression::check_constant(mpfr_t real
, mpfr_t imag
, Type
* type
,
2165 source_location location
)
2169 Complex_type
* ctype
= type
->complex_type();
2170 if (ctype
== NULL
|| ctype
->is_abstract())
2174 switch (ctype
->bits())
2186 // A NaN or Infinity always fits in the range of the type.
2187 if (!mpfr_nan_p(real
) && !mpfr_inf_p(real
) && !mpfr_zero_p(real
))
2189 if (mpfr_get_exp(real
) > max_exp
)
2191 error_at(location
, "complex real part constant overflow");
2196 if (!mpfr_nan_p(imag
) && !mpfr_inf_p(imag
) && !mpfr_zero_p(imag
))
2198 if (mpfr_get_exp(imag
) > max_exp
)
2200 error_at(location
, "complex imaginary part constant overflow");
2208 // Check the type of a complex value.
2211 Complex_expression::do_check_types(Gogo
*)
2213 if (this->type_
== NULL
)
2216 if (!Complex_expression::check_constant(this->real_
, this->imag_
,
2217 this->type_
, this->location()))
2218 this->set_is_error();
2221 // Get a tree for a complex constant.
2224 Complex_expression::do_get_tree(Translate_context
* context
)
2226 Gogo
* gogo
= context
->gogo();
2228 if (this->type_
!= NULL
&& !this->type_
->is_abstract())
2229 type
= this->type_
->get_tree(gogo
);
2232 // If we still have an abstract type here, this this is being
2233 // used in a constant expression which didn't get reduced. We
2234 // just use complex128 and hope for the best.
2235 type
= Type::lookup_complex_type("complex128")->get_tree(gogo
);
2237 return Expression::complex_constant_tree(this->real_
, this->imag_
, type
);
2240 // Write REAL/IMAG to export data.
2243 Complex_expression::export_complex(Export
* exp
, const mpfr_t real
,
2246 if (!mpfr_zero_p(real
))
2248 Float_expression::export_float(exp
, real
);
2249 if (mpfr_sgn(imag
) > 0)
2250 exp
->write_c_string("+");
2252 Float_expression::export_float(exp
, imag
);
2253 exp
->write_c_string("i");
2256 // Export a complex number in a constant expression.
2259 Complex_expression::do_export(Export
* exp
) const
2261 Complex_expression::export_complex(exp
, this->real_
, this->imag_
);
2262 // A trailing space lets us reliably identify the end of the number.
2263 exp
->write_c_string(" ");
2266 // Make a complex expression.
2269 Expression::make_complex(const mpfr_t
* real
, const mpfr_t
* imag
, Type
* type
,
2270 source_location location
)
2272 return new Complex_expression(real
, imag
, type
, location
);
2275 // A reference to a const in an expression.
2277 class Const_expression
: public Expression
2280 Const_expression(Named_object
* constant
, source_location location
)
2281 : Expression(EXPRESSION_CONST_REFERENCE
, location
),
2282 constant_(constant
), type_(NULL
)
2287 { return this->constant_
->name(); }
2291 do_lower(Gogo
*, Named_object
*, int);
2294 do_is_constant() const
2298 do_integer_constant_value(bool, mpz_t val
, Type
**) const;
2301 do_float_constant_value(mpfr_t val
, Type
**) const;
2304 do_complex_constant_value(mpfr_t real
, mpfr_t imag
, Type
**) const;
2307 do_string_constant_value(std::string
* val
) const
2308 { return this->constant_
->const_value()->expr()->string_constant_value(val
); }
2313 // The type of a const is set by the declaration, not the use.
2315 do_determine_type(const Type_context
*);
2318 do_check_types(Gogo
*);
2325 do_get_tree(Translate_context
* context
);
2327 // When exporting a reference to a const as part of a const
2328 // expression, we export the value. We ignore the fact that it has
2331 do_export(Export
* exp
) const
2332 { this->constant_
->const_value()->expr()->export_expression(exp
); }
2336 Named_object
* constant_
;
2337 // The type of this reference. This is used if the constant has an
2342 // Lower a constant expression. This is where we convert the
2343 // predeclared constant iota into an integer value.
2346 Const_expression::do_lower(Gogo
* gogo
, Named_object
*, int iota_value
)
2348 if (this->constant_
->const_value()->expr()->classification()
2351 if (iota_value
== -1)
2353 error_at(this->location(),
2354 "iota is only defined in const declarations");
2358 mpz_init_set_ui(val
, static_cast<unsigned long>(iota_value
));
2359 Expression
* ret
= Expression::make_integer(&val
, NULL
,
2365 // Make sure that the constant itself has been lowered.
2366 gogo
->lower_constant(this->constant_
);
2371 // Return an integer constant value.
2374 Const_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
2378 if (this->type_
!= NULL
)
2379 ctype
= this->type_
;
2381 ctype
= this->constant_
->const_value()->type();
2382 if (ctype
!= NULL
&& ctype
->integer_type() == NULL
)
2385 Expression
* e
= this->constant_
->const_value()->expr();
2387 bool r
= e
->integer_constant_value(iota_is_constant
, val
, &t
);
2391 && !Integer_expression::check_constant(val
, ctype
, this->location()))
2394 *ptype
= ctype
!= NULL
? ctype
: t
;
2398 // Return a floating point constant value.
2401 Const_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
2404 if (this->type_
!= NULL
)
2405 ctype
= this->type_
;
2407 ctype
= this->constant_
->const_value()->type();
2408 if (ctype
!= NULL
&& ctype
->float_type() == NULL
)
2412 bool r
= this->constant_
->const_value()->expr()->float_constant_value(val
,
2414 if (r
&& ctype
!= NULL
)
2416 if (!Float_expression::check_constant(val
, ctype
, this->location()))
2418 Float_expression::constrain_float(val
, ctype
);
2420 *ptype
= ctype
!= NULL
? ctype
: t
;
2424 // Return a complex constant value.
2427 Const_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
2431 if (this->type_
!= NULL
)
2432 ctype
= this->type_
;
2434 ctype
= this->constant_
->const_value()->type();
2435 if (ctype
!= NULL
&& ctype
->complex_type() == NULL
)
2439 bool r
= this->constant_
->const_value()->expr()->complex_constant_value(real
,
2442 if (r
&& ctype
!= NULL
)
2444 if (!Complex_expression::check_constant(real
, imag
, ctype
,
2447 Complex_expression::constrain_complex(real
, imag
, ctype
);
2449 *ptype
= ctype
!= NULL
? ctype
: t
;
2453 // Return the type of the const reference.
2456 Const_expression::do_type()
2458 if (this->type_
!= NULL
)
2460 Named_constant
* nc
= this->constant_
->const_value();
2461 Type
* ret
= nc
->type();
2464 // During parsing, a named constant may have a NULL type, but we
2465 // must not return a NULL type here.
2466 return nc
->expr()->type();
2469 // Set the type of the const reference.
2472 Const_expression::do_determine_type(const Type_context
* context
)
2474 Type
* ctype
= this->constant_
->const_value()->type();
2475 Type
* cetype
= (ctype
!= NULL
2477 : this->constant_
->const_value()->expr()->type());
2478 if (ctype
!= NULL
&& !ctype
->is_abstract())
2480 else if (context
->type
!= NULL
2481 && (context
->type
->integer_type() != NULL
2482 || context
->type
->float_type() != NULL
2483 || context
->type
->complex_type() != NULL
)
2484 && (cetype
->integer_type() != NULL
2485 || cetype
->float_type() != NULL
2486 || cetype
->complex_type() != NULL
))
2487 this->type_
= context
->type
;
2488 else if (context
->type
!= NULL
2489 && context
->type
->is_string_type()
2490 && cetype
->is_string_type())
2491 this->type_
= context
->type
;
2492 else if (context
->type
!= NULL
2493 && context
->type
->is_boolean_type()
2494 && cetype
->is_boolean_type())
2495 this->type_
= context
->type
;
2496 else if (!context
->may_be_abstract
)
2498 if (cetype
->is_abstract())
2499 cetype
= cetype
->make_non_abstract_type();
2500 this->type_
= cetype
;
2504 // Check types of a const reference.
2507 Const_expression::do_check_types(Gogo
*)
2509 if (this->type_
== NULL
|| this->type_
->is_abstract())
2512 // Check for integer overflow.
2513 if (this->type_
->integer_type() != NULL
)
2518 if (!this->integer_constant_value(true, ival
, &dummy
))
2522 Expression
* cexpr
= this->constant_
->const_value()->expr();
2523 if (cexpr
->float_constant_value(fval
, &dummy
))
2525 if (!mpfr_integer_p(fval
))
2526 this->report_error(_("floating point constant "
2527 "truncated to integer"));
2530 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2531 Integer_expression::check_constant(ival
, this->type_
,
2541 // Return a tree for the const reference.
2544 Const_expression::do_get_tree(Translate_context
* context
)
2546 Gogo
* gogo
= context
->gogo();
2548 if (this->type_
== NULL
)
2549 type_tree
= NULL_TREE
;
2552 type_tree
= this->type_
->get_tree(gogo
);
2553 if (type_tree
== error_mark_node
)
2554 return error_mark_node
;
2557 // If the type has been set for this expression, but the underlying
2558 // object is an abstract int or float, we try to get the abstract
2559 // value. Otherwise we may lose something in the conversion.
2560 if (this->type_
!= NULL
2561 && this->constant_
->const_value()->type()->is_abstract())
2563 Expression
* expr
= this->constant_
->const_value()->expr();
2567 if (expr
->integer_constant_value(true, ival
, &t
))
2569 tree ret
= Expression::integer_constant_tree(ival
, type_tree
);
2577 if (expr
->float_constant_value(fval
, &t
))
2579 tree ret
= Expression::float_constant_tree(fval
, type_tree
);
2586 if (expr
->complex_constant_value(fval
, imag
, &t
))
2588 tree ret
= Expression::complex_constant_tree(fval
, imag
, type_tree
);
2597 tree const_tree
= this->constant_
->get_tree(gogo
, context
->function());
2598 if (this->type_
== NULL
2599 || const_tree
== error_mark_node
2600 || TREE_TYPE(const_tree
) == error_mark_node
)
2604 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree
)))
2605 ret
= fold_convert(type_tree
, const_tree
);
2606 else if (TREE_CODE(type_tree
) == INTEGER_TYPE
)
2607 ret
= fold(convert_to_integer(type_tree
, const_tree
));
2608 else if (TREE_CODE(type_tree
) == REAL_TYPE
)
2609 ret
= fold(convert_to_real(type_tree
, const_tree
));
2610 else if (TREE_CODE(type_tree
) == COMPLEX_TYPE
)
2611 ret
= fold(convert_to_complex(type_tree
, const_tree
));
2617 // Make a reference to a constant in an expression.
2620 Expression::make_const_reference(Named_object
* constant
,
2621 source_location location
)
2623 return new Const_expression(constant
, location
);
2628 class Nil_expression
: public Expression
2631 Nil_expression(source_location location
)
2632 : Expression(EXPRESSION_NIL
, location
)
2640 do_is_constant() const
2645 { return Type::make_nil_type(); }
2648 do_determine_type(const Type_context
*)
2656 do_get_tree(Translate_context
*)
2657 { return null_pointer_node
; }
2660 do_export(Export
* exp
) const
2661 { exp
->write_c_string("nil"); }
2664 // Import a nil expression.
2667 Nil_expression::do_import(Import
* imp
)
2669 imp
->require_c_string("nil");
2670 return Expression::make_nil(imp
->location());
2673 // Make a nil expression.
2676 Expression::make_nil(source_location location
)
2678 return new Nil_expression(location
);
2681 // The value of the predeclared constant iota. This is little more
2682 // than a marker. This will be lowered to an integer in
2683 // Const_expression::do_lower, which is where we know the value that
2686 class Iota_expression
: public Parser_expression
2689 Iota_expression(source_location location
)
2690 : Parser_expression(EXPRESSION_IOTA
, location
)
2695 do_lower(Gogo
*, Named_object
*, int)
2696 { gcc_unreachable(); }
2698 // There should only ever be one of these.
2701 { gcc_unreachable(); }
2704 // Make an iota expression. This is only called for one case: the
2705 // value of the predeclared constant iota.
2708 Expression::make_iota()
2710 static Iota_expression
iota_expression(UNKNOWN_LOCATION
);
2711 return &iota_expression
;
2714 // A type conversion expression.
2716 class Type_conversion_expression
: public Expression
2719 Type_conversion_expression(Type
* type
, Expression
* expr
,
2720 source_location location
)
2721 : Expression(EXPRESSION_CONVERSION
, location
),
2722 type_(type
), expr_(expr
), may_convert_function_types_(false)
2725 // Return the type to which we are converting.
2728 { return this->type_
; }
2730 // Return the expression which we are converting.
2733 { return this->expr_
; }
2735 // Permit converting from one function type to another. This is
2736 // used internally for method expressions.
2738 set_may_convert_function_types()
2740 this->may_convert_function_types_
= true;
2743 // Import a type conversion expression.
2749 do_traverse(Traverse
* traverse
);
2752 do_lower(Gogo
*, Named_object
*, int);
2755 do_is_constant() const
2756 { return this->expr_
->is_constant(); }
2759 do_integer_constant_value(bool, mpz_t
, Type
**) const;
2762 do_float_constant_value(mpfr_t
, Type
**) const;
2765 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
2768 do_string_constant_value(std::string
*) const;
2772 { return this->type_
; }
2775 do_determine_type(const Type_context
*)
2777 Type_context
subcontext(this->type_
, false);
2778 this->expr_
->determine_type(&subcontext
);
2782 do_check_types(Gogo
*);
2787 return new Type_conversion_expression(this->type_
, this->expr_
->copy(),
2792 do_get_tree(Translate_context
* context
);
2795 do_export(Export
*) const;
2798 // The type to convert to.
2800 // The expression to convert.
2802 // True if this is permitted to convert function types. This is
2803 // used internally for method expressions.
2804 bool may_convert_function_types_
;
2810 Type_conversion_expression::do_traverse(Traverse
* traverse
)
2812 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
2813 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
2814 return TRAVERSE_EXIT
;
2815 return TRAVERSE_CONTINUE
;
2818 // Convert to a constant at lowering time.
2821 Type_conversion_expression::do_lower(Gogo
*, Named_object
*, int)
2823 Type
* type
= this->type_
;
2824 Expression
* val
= this->expr_
;
2825 source_location location
= this->location();
2827 if (type
->integer_type() != NULL
)
2832 if (val
->integer_constant_value(false, ival
, &dummy
))
2834 if (!Integer_expression::check_constant(ival
, type
, location
))
2835 mpz_set_ui(ival
, 0);
2836 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2843 if (val
->float_constant_value(fval
, &dummy
))
2845 if (!mpfr_integer_p(fval
))
2848 "floating point constant truncated to integer");
2849 return Expression::make_error(location
);
2851 mpfr_get_z(ival
, fval
, GMP_RNDN
);
2852 if (!Integer_expression::check_constant(ival
, type
, location
))
2853 mpz_set_ui(ival
, 0);
2854 Expression
* ret
= Expression::make_integer(&ival
, type
, location
);
2863 if (type
->float_type() != NULL
)
2868 if (val
->float_constant_value(fval
, &dummy
))
2870 if (!Float_expression::check_constant(fval
, type
, location
))
2871 mpfr_set_ui(fval
, 0, GMP_RNDN
);
2872 Float_expression::constrain_float(fval
, type
);
2873 Expression
*ret
= Expression::make_float(&fval
, type
, location
);
2880 if (type
->complex_type() != NULL
)
2887 if (val
->complex_constant_value(real
, imag
, &dummy
))
2889 if (!Complex_expression::check_constant(real
, imag
, type
, location
))
2891 mpfr_set_ui(real
, 0, GMP_RNDN
);
2892 mpfr_set_ui(imag
, 0, GMP_RNDN
);
2894 Complex_expression::constrain_complex(real
, imag
, type
);
2895 Expression
* ret
= Expression::make_complex(&real
, &imag
, type
,
2905 if (type
->is_open_array_type() && type
->named_type() == NULL
)
2907 Type
* element_type
= type
->array_type()->element_type()->forwarded();
2908 bool is_byte
= element_type
== Type::lookup_integer_type("uint8");
2909 bool is_int
= element_type
== Type::lookup_integer_type("int");
2910 if (is_byte
|| is_int
)
2913 if (val
->string_constant_value(&s
))
2915 Expression_list
* vals
= new Expression_list();
2918 for (std::string::const_iterator p
= s
.begin();
2923 mpz_init_set_ui(val
, static_cast<unsigned char>(*p
));
2924 Expression
* v
= Expression::make_integer(&val
,
2933 const char *p
= s
.data();
2934 const char *pend
= s
.data() + s
.length();
2938 int adv
= Lex::fetch_char(p
, &c
);
2941 warning_at(this->location(), 0,
2942 "invalid UTF-8 encoding");
2947 mpz_init_set_ui(val
, c
);
2948 Expression
* v
= Expression::make_integer(&val
,
2956 return Expression::make_slice_composite_literal(type
, vals
,
2965 // Return the constant integer value if there is one.
2968 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant
,
2972 if (this->type_
->integer_type() == NULL
)
2978 if (this->expr_
->integer_constant_value(iota_is_constant
, ival
, &dummy
))
2980 if (!Integer_expression::check_constant(ival
, this->type_
,
2988 *ptype
= this->type_
;
2995 if (this->expr_
->float_constant_value(fval
, &dummy
))
2997 mpfr_get_z(val
, fval
, GMP_RNDN
);
2999 if (!Integer_expression::check_constant(val
, this->type_
,
3002 *ptype
= this->type_
;
3010 // Return the constant floating point value if there is one.
3013 Type_conversion_expression::do_float_constant_value(mpfr_t val
,
3016 if (this->type_
->float_type() == NULL
)
3022 if (this->expr_
->float_constant_value(fval
, &dummy
))
3024 if (!Float_expression::check_constant(fval
, this->type_
,
3030 mpfr_set(val
, fval
, GMP_RNDN
);
3032 Float_expression::constrain_float(val
, this->type_
);
3033 *ptype
= this->type_
;
3041 // Return the constant complex value if there is one.
3044 Type_conversion_expression::do_complex_constant_value(mpfr_t real
,
3048 if (this->type_
->complex_type() == NULL
)
3056 if (this->expr_
->complex_constant_value(rval
, ival
, &dummy
))
3058 if (!Complex_expression::check_constant(rval
, ival
, this->type_
,
3065 mpfr_set(real
, rval
, GMP_RNDN
);
3066 mpfr_set(imag
, ival
, GMP_RNDN
);
3069 Complex_expression::constrain_complex(real
, imag
, this->type_
);
3070 *ptype
= this->type_
;
3079 // Return the constant string value if there is one.
3082 Type_conversion_expression::do_string_constant_value(std::string
* val
) const
3084 if (this->type_
->is_string_type()
3085 && this->expr_
->type()->integer_type() != NULL
)
3090 if (this->expr_
->integer_constant_value(false, ival
, &dummy
))
3092 unsigned long ulval
= mpz_get_ui(ival
);
3093 if (mpz_cmp_ui(ival
, ulval
) == 0)
3095 Lex::append_char(ulval
, true, val
, this->location());
3103 // FIXME: Could handle conversion from const []int here.
3108 // Check that types are convertible.
3111 Type_conversion_expression::do_check_types(Gogo
*)
3113 Type
* type
= this->type_
;
3114 Type
* expr_type
= this->expr_
->type();
3117 if (this->may_convert_function_types_
3118 && type
->function_type() != NULL
3119 && expr_type
->function_type() != NULL
)
3122 if (Type::are_convertible(type
, expr_type
, &reason
))
3125 error_at(this->location(), "%s", reason
.c_str());
3126 this->set_is_error();
3129 // Get a tree for a type conversion.
3132 Type_conversion_expression::do_get_tree(Translate_context
* context
)
3134 Gogo
* gogo
= context
->gogo();
3135 tree type_tree
= this->type_
->get_tree(gogo
);
3136 tree expr_tree
= this->expr_
->get_tree(context
);
3138 if (type_tree
== error_mark_node
3139 || expr_tree
== error_mark_node
3140 || TREE_TYPE(expr_tree
) == error_mark_node
)
3141 return error_mark_node
;
3143 if (TYPE_MAIN_VARIANT(type_tree
) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree
)))
3144 return fold_convert(type_tree
, expr_tree
);
3146 Type
* type
= this->type_
;
3147 Type
* expr_type
= this->expr_
->type();
3149 if (type
->interface_type() != NULL
|| expr_type
->interface_type() != NULL
)
3150 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3151 expr_tree
, this->location());
3152 else if (type
->integer_type() != NULL
)
3154 if (expr_type
->integer_type() != NULL
3155 || expr_type
->float_type() != NULL
3156 || expr_type
->is_unsafe_pointer_type())
3157 ret
= fold(convert_to_integer(type_tree
, expr_tree
));
3161 else if (type
->float_type() != NULL
)
3163 if (expr_type
->integer_type() != NULL
3164 || expr_type
->float_type() != NULL
)
3165 ret
= fold(convert_to_real(type_tree
, expr_tree
));
3169 else if (type
->complex_type() != NULL
)
3171 if (expr_type
->complex_type() != NULL
)
3172 ret
= fold(convert_to_complex(type_tree
, expr_tree
));
3176 else if (type
->is_string_type()
3177 && expr_type
->integer_type() != NULL
)
3179 expr_tree
= fold_convert(integer_type_node
, expr_tree
);
3180 if (host_integerp(expr_tree
, 0))
3182 HOST_WIDE_INT intval
= tree_low_cst(expr_tree
, 0);
3184 Lex::append_char(intval
, true, &s
, this->location());
3185 Expression
* se
= Expression::make_string(s
, this->location());
3186 return se
->get_tree(context
);
3189 static tree int_to_string_fndecl
;
3190 ret
= Gogo::call_builtin(&int_to_string_fndecl
,
3192 "__go_int_to_string",
3196 fold_convert(integer_type_node
, expr_tree
));
3198 else if (type
->is_string_type()
3199 && (expr_type
->array_type() != NULL
3200 || (expr_type
->points_to() != NULL
3201 && expr_type
->points_to()->array_type() != NULL
)))
3203 Type
* t
= expr_type
;
3204 if (t
->points_to() != NULL
)
3207 expr_tree
= build_fold_indirect_ref(expr_tree
);
3209 if (!DECL_P(expr_tree
))
3210 expr_tree
= save_expr(expr_tree
);
3211 Array_type
* a
= t
->array_type();
3212 Type
* e
= a
->element_type()->forwarded();
3213 gcc_assert(e
->integer_type() != NULL
);
3214 tree valptr
= fold_convert(const_ptr_type_node
,
3215 a
->value_pointer_tree(gogo
, expr_tree
));
3216 tree len
= a
->length_tree(gogo
, expr_tree
);
3217 len
= fold_convert_loc(this->location(), size_type_node
, len
);
3218 if (e
->integer_type()->is_unsigned()
3219 && e
->integer_type()->bits() == 8)
3221 static tree byte_array_to_string_fndecl
;
3222 ret
= Gogo::call_builtin(&byte_array_to_string_fndecl
,
3224 "__go_byte_array_to_string",
3227 const_ptr_type_node
,
3234 gcc_assert(e
== Type::lookup_integer_type("int"));
3235 static tree int_array_to_string_fndecl
;
3236 ret
= Gogo::call_builtin(&int_array_to_string_fndecl
,
3238 "__go_int_array_to_string",
3241 const_ptr_type_node
,
3247 else if (type
->is_open_array_type() && expr_type
->is_string_type())
3249 Type
* e
= type
->array_type()->element_type()->forwarded();
3250 gcc_assert(e
->integer_type() != NULL
);
3251 if (e
->integer_type()->is_unsigned()
3252 && e
->integer_type()->bits() == 8)
3254 static tree string_to_byte_array_fndecl
;
3255 ret
= Gogo::call_builtin(&string_to_byte_array_fndecl
,
3257 "__go_string_to_byte_array",
3260 TREE_TYPE(expr_tree
),
3265 gcc_assert(e
== Type::lookup_integer_type("int"));
3266 static tree string_to_int_array_fndecl
;
3267 ret
= Gogo::call_builtin(&string_to_int_array_fndecl
,
3269 "__go_string_to_int_array",
3272 TREE_TYPE(expr_tree
),
3276 else if ((type
->is_unsafe_pointer_type()
3277 && expr_type
->points_to() != NULL
)
3278 || (expr_type
->is_unsafe_pointer_type()
3279 && type
->points_to() != NULL
))
3280 ret
= fold_convert(type_tree
, expr_tree
);
3281 else if (type
->is_unsafe_pointer_type()
3282 && expr_type
->integer_type() != NULL
)
3283 ret
= convert_to_pointer(type_tree
, expr_tree
);
3284 else if (this->may_convert_function_types_
3285 && type
->function_type() != NULL
3286 && expr_type
->function_type() != NULL
)
3287 ret
= fold_convert_loc(this->location(), type_tree
, expr_tree
);
3289 ret
= Expression::convert_for_assignment(context
, type
, expr_type
,
3290 expr_tree
, this->location());
3295 // Output a type conversion in a constant expression.
3298 Type_conversion_expression::do_export(Export
* exp
) const
3300 exp
->write_c_string("convert(");
3301 exp
->write_type(this->type_
);
3302 exp
->write_c_string(", ");
3303 this->expr_
->export_expression(exp
);
3304 exp
->write_c_string(")");
3307 // Import a type conversion or a struct construction.
3310 Type_conversion_expression::do_import(Import
* imp
)
3312 imp
->require_c_string("convert(");
3313 Type
* type
= imp
->read_type();
3314 imp
->require_c_string(", ");
3315 Expression
* val
= Expression::import_expression(imp
);
3316 imp
->require_c_string(")");
3317 return Expression::make_cast(type
, val
, imp
->location());
3320 // Make a type cast expression.
3323 Expression::make_cast(Type
* type
, Expression
* val
, source_location location
)
3325 if (type
->is_error_type() || val
->is_error_expression())
3326 return Expression::make_error(location
);
3327 return new Type_conversion_expression(type
, val
, location
);
3330 // Unary expressions.
3332 class Unary_expression
: public Expression
3335 Unary_expression(Operator op
, Expression
* expr
, source_location location
)
3336 : Expression(EXPRESSION_UNARY
, location
),
3337 op_(op
), escapes_(true), expr_(expr
)
3340 // Return the operator.
3343 { return this->op_
; }
3345 // Return the operand.
3348 { return this->expr_
; }
3350 // Record that an address expression does not escape.
3352 set_does_not_escape()
3354 gcc_assert(this->op_
== OPERATOR_AND
);
3355 this->escapes_
= false;
3358 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3359 // could be done, false if not.
3361 eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3364 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3365 // could be done, false if not.
3367 eval_float(Operator op
, mpfr_t uval
, mpfr_t val
);
3369 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3370 // true if this could be done, false if not.
3372 eval_complex(Operator op
, mpfr_t ureal
, mpfr_t uimag
, mpfr_t real
,
3380 do_traverse(Traverse
* traverse
)
3381 { return Expression::traverse(&this->expr_
, traverse
); }
3384 do_lower(Gogo
*, Named_object
*, int);
3387 do_is_constant() const;
3390 do_integer_constant_value(bool, mpz_t
, Type
**) const;
3393 do_float_constant_value(mpfr_t
, Type
**) const;
3396 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
3402 do_determine_type(const Type_context
*);
3405 do_check_types(Gogo
*);
3410 return Expression::make_unary(this->op_
, this->expr_
->copy(),
3415 do_is_addressable() const
3416 { return this->op_
== OPERATOR_MULT
; }
3419 do_get_tree(Translate_context
*);
3422 do_export(Export
*) const;
3425 // The unary operator to apply.
3427 // Normally true. False if this is an address expression which does
3428 // not escape the current function.
3434 // If we are taking the address of a composite literal, and the
3435 // contents are not constant, then we want to make a heap composite
3439 Unary_expression::do_lower(Gogo
*, Named_object
*, int)
3441 source_location loc
= this->location();
3442 Operator op
= this->op_
;
3443 Expression
* expr
= this->expr_
;
3445 if (op
== OPERATOR_MULT
&& expr
->is_type_expression())
3446 return Expression::make_type(Type::make_pointer_type(expr
->type()), loc
);
3448 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3449 // moving x to the heap. FIXME: Is it worth doing a real escape
3450 // analysis here? This case is found in math/unsafe.go and is
3451 // therefore worth special casing.
3452 if (op
== OPERATOR_MULT
)
3454 Expression
* e
= expr
;
3455 while (e
->classification() == EXPRESSION_CONVERSION
)
3457 Type_conversion_expression
* te
3458 = static_cast<Type_conversion_expression
*>(e
);
3462 if (e
->classification() == EXPRESSION_UNARY
)
3464 Unary_expression
* ue
= static_cast<Unary_expression
*>(e
);
3465 if (ue
->op_
== OPERATOR_AND
)
3472 ue
->set_does_not_escape();
3477 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
3478 || op
== OPERATOR_NOT
|| op
== OPERATOR_XOR
)
3480 Expression
* ret
= NULL
;
3485 if (expr
->integer_constant_value(false, eval
, &etype
))
3489 if (Unary_expression::eval_integer(op
, etype
, eval
, val
, loc
))
3490 ret
= Expression::make_integer(&val
, etype
, loc
);
3497 if (op
== OPERATOR_PLUS
|| op
== OPERATOR_MINUS
)
3502 if (expr
->float_constant_value(fval
, &ftype
))
3506 if (Unary_expression::eval_float(op
, fval
, val
))
3507 ret
= Expression::make_float(&val
, ftype
, loc
);
3518 if (expr
->complex_constant_value(fval
, ival
, &ftype
))
3524 if (Unary_expression::eval_complex(op
, fval
, ival
, real
, imag
))
3525 ret
= Expression::make_complex(&real
, &imag
, ftype
, loc
);
3539 // Return whether a unary expression is a constant.
3542 Unary_expression::do_is_constant() const
3544 if (this->op_
== OPERATOR_MULT
)
3546 // Indirecting through a pointer is only constant if the object
3547 // to which the expression points is constant, but we currently
3548 // have no way to determine that.
3551 else if (this->op_
== OPERATOR_AND
)
3553 // Taking the address of a variable is constant if it is a
3554 // global variable, not constant otherwise. In other cases
3555 // taking the address is probably not a constant.
3556 Var_expression
* ve
= this->expr_
->var_expression();
3559 Named_object
* no
= ve
->named_object();
3560 return no
->is_variable() && no
->var_value()->is_global();
3565 return this->expr_
->is_constant();
3568 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3569 // UVAL, if known; it may be NULL. Return true if this could be done,
3573 Unary_expression::eval_integer(Operator op
, Type
* utype
, mpz_t uval
, mpz_t val
,
3574 source_location location
)
3581 case OPERATOR_MINUS
:
3583 return Integer_expression::check_constant(val
, utype
, location
);
3585 mpz_set_ui(val
, mpz_cmp_si(uval
, 0) == 0 ? 1 : 0);
3589 || utype
->integer_type() == NULL
3590 || utype
->integer_type()->is_abstract())
3594 // The number of HOST_WIDE_INTs that it takes to represent
3596 size_t count
= ((mpz_sizeinbase(uval
, 2)
3597 + HOST_BITS_PER_WIDE_INT
3599 / HOST_BITS_PER_WIDE_INT
);
3601 unsigned HOST_WIDE_INT
* phwi
= new unsigned HOST_WIDE_INT
[count
];
3602 memset(phwi
, 0, count
* sizeof(HOST_WIDE_INT
));
3605 mpz_export(phwi
, &ecount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, uval
);
3606 gcc_assert(ecount
<= count
);
3608 // Trim down to the number of words required by the type.
3609 size_t obits
= utype
->integer_type()->bits();
3610 if (!utype
->integer_type()->is_unsigned())
3612 size_t ocount
= ((obits
+ HOST_BITS_PER_WIDE_INT
- 1)
3613 / HOST_BITS_PER_WIDE_INT
);
3614 gcc_assert(ocount
<= ocount
);
3616 for (size_t i
= 0; i
< ocount
; ++i
)
3619 size_t clearbits
= ocount
* HOST_BITS_PER_WIDE_INT
- obits
;
3621 phwi
[ocount
- 1] &= (((unsigned HOST_WIDE_INT
) (HOST_WIDE_INT
) -1)
3624 mpz_import(val
, ocount
, -1, sizeof(HOST_WIDE_INT
), 0, 0, phwi
);
3628 return Integer_expression::check_constant(val
, utype
, location
);
3637 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3638 // could be done, false if not.
3641 Unary_expression::eval_float(Operator op
, mpfr_t uval
, mpfr_t val
)
3646 mpfr_set(val
, uval
, GMP_RNDN
);
3648 case OPERATOR_MINUS
:
3649 mpfr_neg(val
, uval
, GMP_RNDN
);
3661 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
3662 // if this could be done, false if not.
3665 Unary_expression::eval_complex(Operator op
, mpfr_t rval
, mpfr_t ival
,
3666 mpfr_t real
, mpfr_t imag
)
3671 mpfr_set(real
, rval
, GMP_RNDN
);
3672 mpfr_set(imag
, ival
, GMP_RNDN
);
3674 case OPERATOR_MINUS
:
3675 mpfr_neg(real
, rval
, GMP_RNDN
);
3676 mpfr_neg(imag
, ival
, GMP_RNDN
);
3688 // Return the integral constant value of a unary expression, if it has one.
3691 Unary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
3697 if (!this->expr_
->integer_constant_value(iota_is_constant
, uval
, ptype
))
3700 ret
= Unary_expression::eval_integer(this->op_
, *ptype
, uval
, val
,
3706 // Return the floating point constant value of a unary expression, if
3710 Unary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
3715 if (!this->expr_
->float_constant_value(uval
, ptype
))
3718 ret
= Unary_expression::eval_float(this->op_
, uval
, val
);
3723 // Return the complex constant value of a unary expression, if it has
3727 Unary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
3735 if (!this->expr_
->complex_constant_value(rval
, ival
, ptype
))
3738 ret
= Unary_expression::eval_complex(this->op_
, rval
, ival
, real
, imag
);
3744 // Return the type of a unary expression.
3747 Unary_expression::do_type()
3752 case OPERATOR_MINUS
:
3755 return this->expr_
->type();
3758 return Type::make_pointer_type(this->expr_
->type());
3762 Type
* subtype
= this->expr_
->type();
3763 Type
* points_to
= subtype
->points_to();
3764 if (points_to
== NULL
)
3765 return Type::make_error_type();
3774 // Determine abstract types for a unary expression.
3777 Unary_expression::do_determine_type(const Type_context
* context
)
3782 case OPERATOR_MINUS
:
3785 this->expr_
->determine_type(context
);
3789 // Taking the address of something.
3791 Type
* subtype
= (context
->type
== NULL
3793 : context
->type
->points_to());
3794 Type_context
subcontext(subtype
, false);
3795 this->expr_
->determine_type(&subcontext
);
3800 // Indirecting through a pointer.
3802 Type
* subtype
= (context
->type
== NULL
3804 : Type::make_pointer_type(context
->type
));
3805 Type_context
subcontext(subtype
, false);
3806 this->expr_
->determine_type(&subcontext
);
3815 // Check types for a unary expression.
3818 Unary_expression::do_check_types(Gogo
*)
3823 case OPERATOR_MINUS
:
3825 Type
* type
= this->expr_
->type();
3826 if (type
->integer_type() == NULL
3827 && type
->float_type() == NULL
3828 && type
->complex_type() == NULL
3829 && !type
->is_error_type())
3830 this->report_error(_("expected numeric type"));
3837 Type
* type
= this->expr_
->type();
3838 if (type
->integer_type() == NULL
3839 && !type
->is_boolean_type()
3840 && !type
->is_error_type())
3841 this->report_error(_("expected integer or boolean type"));
3846 if (!this->expr_
->is_addressable())
3847 this->report_error(_("invalid operand for unary %<&%>"));
3849 this->expr_
->address_taken(this->escapes_
);
3853 // Indirecting through a pointer.
3855 Type
* type
= this->expr_
->type();
3856 if (type
->points_to() == NULL
3857 && !type
->is_error_type())
3858 this->report_error(_("expected pointer"));
3867 // Get a tree for a unary expression.
3870 Unary_expression::do_get_tree(Translate_context
* context
)
3872 tree expr
= this->expr_
->get_tree(context
);
3873 if (expr
== error_mark_node
)
3874 return error_mark_node
;
3876 source_location loc
= this->location();
3882 case OPERATOR_MINUS
:
3884 tree type
= TREE_TYPE(expr
);
3885 tree compute_type
= excess_precision_type(type
);
3886 if (compute_type
!= NULL_TREE
)
3887 expr
= ::convert(compute_type
, expr
);
3888 tree ret
= fold_build1_loc(loc
, NEGATE_EXPR
,
3889 (compute_type
!= NULL_TREE
3893 if (compute_type
!= NULL_TREE
)
3894 ret
= ::convert(type
, ret
);
3899 if (TREE_CODE(TREE_TYPE(expr
)) == BOOLEAN_TYPE
)
3900 return fold_build1_loc(loc
, TRUTH_NOT_EXPR
, TREE_TYPE(expr
), expr
);
3902 return fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, expr
,
3903 build_int_cst(TREE_TYPE(expr
), 0));
3906 return fold_build1_loc(loc
, BIT_NOT_EXPR
, TREE_TYPE(expr
), expr
);
3909 // We should not see a non-constant constructor here; cases
3910 // where we would see one should have been moved onto the heap
3911 // at parse time. Taking the address of a nonconstant
3912 // constructor will not do what the programmer expects.
3913 gcc_assert(TREE_CODE(expr
) != CONSTRUCTOR
|| TREE_CONSTANT(expr
));
3914 gcc_assert(TREE_CODE(expr
) != ADDR_EXPR
);
3916 // Build a decl for a constant constructor.
3917 if (TREE_CODE(expr
) == CONSTRUCTOR
&& TREE_CONSTANT(expr
))
3919 tree decl
= build_decl(this->location(), VAR_DECL
,
3920 create_tmp_var_name("C"), TREE_TYPE(expr
));
3921 DECL_EXTERNAL(decl
) = 0;
3922 TREE_PUBLIC(decl
) = 0;
3923 TREE_READONLY(decl
) = 1;
3924 TREE_CONSTANT(decl
) = 1;
3925 TREE_STATIC(decl
) = 1;
3926 TREE_ADDRESSABLE(decl
) = 1;
3927 DECL_ARTIFICIAL(decl
) = 1;
3928 DECL_INITIAL(decl
) = expr
;
3929 rest_of_decl_compilation(decl
, 1, 0);
3933 return build_fold_addr_expr_loc(loc
, expr
);
3937 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr
)));
3939 // If we are dereferencing the pointer to a large struct, we
3940 // need to check for nil. We don't bother to check for small
3941 // structs because we expect the system to crash on a nil
3942 // pointer dereference.
3943 HOST_WIDE_INT s
= int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr
)));
3944 if (s
== -1 || s
>= 4096)
3947 expr
= save_expr(expr
);
3948 tree compare
= fold_build2_loc(loc
, EQ_EXPR
, boolean_type_node
,
3950 fold_convert(TREE_TYPE(expr
),
3951 null_pointer_node
));
3952 tree crash
= Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE
,
3954 expr
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(expr
),
3955 build3(COND_EXPR
, void_type_node
,
3956 compare
, crash
, NULL_TREE
),
3960 // If the type of EXPR is a recursive pointer type, then we
3961 // need to insert a cast before indirecting.
3962 if (TREE_TYPE(TREE_TYPE(expr
)) == ptr_type_node
)
3964 Type
* pt
= this->expr_
->type()->points_to();
3965 tree ind
= pt
->get_tree(context
->gogo());
3966 expr
= fold_convert_loc(loc
, build_pointer_type(ind
), expr
);
3969 return build_fold_indirect_ref_loc(loc
, expr
);
3977 // Export a unary expression.
3980 Unary_expression::do_export(Export
* exp
) const
3985 exp
->write_c_string("+ ");
3987 case OPERATOR_MINUS
:
3988 exp
->write_c_string("- ");
3991 exp
->write_c_string("! ");
3994 exp
->write_c_string("^ ");
4001 this->expr_
->export_expression(exp
);
4004 // Import a unary expression.
4007 Unary_expression::do_import(Import
* imp
)
4010 switch (imp
->get_char())
4016 op
= OPERATOR_MINUS
;
4027 imp
->require_c_string(" ");
4028 Expression
* expr
= Expression::import_expression(imp
);
4029 return Expression::make_unary(op
, expr
, imp
->location());
4032 // Make a unary expression.
4035 Expression::make_unary(Operator op
, Expression
* expr
, source_location location
)
4037 return new Unary_expression(op
, expr
, location
);
4040 // If this is an indirection through a pointer, return the expression
4041 // being pointed through. Otherwise return this.
4046 if (this->classification_
== EXPRESSION_UNARY
)
4048 Unary_expression
* ue
= static_cast<Unary_expression
*>(this);
4049 if (ue
->op() == OPERATOR_MULT
)
4050 return ue
->operand();
4055 // Class Binary_expression.
4060 Binary_expression::do_traverse(Traverse
* traverse
)
4062 int t
= Expression::traverse(&this->left_
, traverse
);
4063 if (t
== TRAVERSE_EXIT
)
4064 return TRAVERSE_EXIT
;
4065 return Expression::traverse(&this->right_
, traverse
);
4068 // Compare integer constants according to OP.
4071 Binary_expression::compare_integer(Operator op
, mpz_t left_val
,
4074 int i
= mpz_cmp(left_val
, right_val
);
4079 case OPERATOR_NOTEQ
:
4094 // Compare floating point constants according to OP.
4097 Binary_expression::compare_float(Operator op
, Type
* type
, mpfr_t left_val
,
4102 i
= mpfr_cmp(left_val
, right_val
);
4106 mpfr_init_set(lv
, left_val
, GMP_RNDN
);
4108 mpfr_init_set(rv
, right_val
, GMP_RNDN
);
4109 Float_expression::constrain_float(lv
, type
);
4110 Float_expression::constrain_float(rv
, type
);
4111 i
= mpfr_cmp(lv
, rv
);
4119 case OPERATOR_NOTEQ
:
4134 // Compare complex constants according to OP. Complex numbers may
4135 // only be compared for equality.
4138 Binary_expression::compare_complex(Operator op
, Type
* type
,
4139 mpfr_t left_real
, mpfr_t left_imag
,
4140 mpfr_t right_real
, mpfr_t right_imag
)
4144 is_equal
= (mpfr_cmp(left_real
, right_real
) == 0
4145 && mpfr_cmp(left_imag
, right_imag
) == 0);
4150 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4151 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4154 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4155 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4156 Complex_expression::constrain_complex(lr
, li
, type
);
4157 Complex_expression::constrain_complex(rr
, ri
, type
);
4158 is_equal
= mpfr_cmp(lr
, rr
) == 0 && mpfr_cmp(li
, ri
) == 0;
4168 case OPERATOR_NOTEQ
:
4175 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4176 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4177 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4178 // this could be done, false if not.
4181 Binary_expression::eval_integer(Operator op
, Type
* left_type
, mpz_t left_val
,
4182 Type
* right_type
, mpz_t right_val
,
4183 source_location location
, mpz_t val
)
4185 bool is_shift_op
= false;
4189 case OPERATOR_ANDAND
:
4191 case OPERATOR_NOTEQ
:
4196 // These return boolean values. We should probably handle them
4197 // anyhow in case a type conversion is used on the result.
4200 mpz_add(val
, left_val
, right_val
);
4202 case OPERATOR_MINUS
:
4203 mpz_sub(val
, left_val
, right_val
);
4206 mpz_ior(val
, left_val
, right_val
);
4209 mpz_xor(val
, left_val
, right_val
);
4212 mpz_mul(val
, left_val
, right_val
);
4215 if (mpz_sgn(right_val
) != 0)
4216 mpz_tdiv_q(val
, left_val
, right_val
);
4219 error_at(location
, "division by zero");
4225 if (mpz_sgn(right_val
) != 0)
4226 mpz_tdiv_r(val
, left_val
, right_val
);
4229 error_at(location
, "division by zero");
4234 case OPERATOR_LSHIFT
:
4236 unsigned long shift
= mpz_get_ui(right_val
);
4237 if (mpz_cmp_ui(right_val
, shift
) != 0)
4239 error_at(location
, "shift count overflow");
4243 mpz_mul_2exp(val
, left_val
, shift
);
4248 case OPERATOR_RSHIFT
:
4250 unsigned long shift
= mpz_get_ui(right_val
);
4251 if (mpz_cmp_ui(right_val
, shift
) != 0)
4253 error_at(location
, "shift count overflow");
4257 if (mpz_cmp_ui(left_val
, 0) >= 0)
4258 mpz_tdiv_q_2exp(val
, left_val
, shift
);
4260 mpz_fdiv_q_2exp(val
, left_val
, shift
);
4266 mpz_and(val
, left_val
, right_val
);
4268 case OPERATOR_BITCLEAR
:
4272 mpz_com(tval
, right_val
);
4273 mpz_and(val
, left_val
, tval
);
4281 Type
* type
= left_type
;
4286 else if (type
!= right_type
&& right_type
!= NULL
)
4288 if (type
->is_abstract())
4290 else if (!right_type
->is_abstract())
4292 // This look like a type error which should be diagnosed
4293 // elsewhere. Don't do anything here, to avoid an
4294 // unhelpful chain of error messages.
4300 if (type
!= NULL
&& !type
->is_abstract())
4302 // We have to check the operands too, as we have implicitly
4303 // coerced them to TYPE.
4304 if ((type
!= left_type
4305 && !Integer_expression::check_constant(left_val
, type
, location
))
4307 && type
!= right_type
4308 && !Integer_expression::check_constant(right_val
, type
,
4310 || !Integer_expression::check_constant(val
, type
, location
))
4317 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4318 // Return true if this could be done, false if not.
4321 Binary_expression::eval_float(Operator op
, Type
* left_type
, mpfr_t left_val
,
4322 Type
* right_type
, mpfr_t right_val
,
4323 mpfr_t val
, source_location location
)
4328 case OPERATOR_ANDAND
:
4330 case OPERATOR_NOTEQ
:
4335 // These return boolean values. We should probably handle them
4336 // anyhow in case a type conversion is used on the result.
4339 mpfr_add(val
, left_val
, right_val
, GMP_RNDN
);
4341 case OPERATOR_MINUS
:
4342 mpfr_sub(val
, left_val
, right_val
, GMP_RNDN
);
4347 case OPERATOR_BITCLEAR
:
4350 mpfr_mul(val
, left_val
, right_val
, GMP_RNDN
);
4353 if (mpfr_zero_p(right_val
))
4354 error_at(location
, "division by zero");
4355 mpfr_div(val
, left_val
, right_val
, GMP_RNDN
);
4359 case OPERATOR_LSHIFT
:
4360 case OPERATOR_RSHIFT
:
4366 Type
* type
= left_type
;
4369 else if (type
!= right_type
&& right_type
!= NULL
)
4371 if (type
->is_abstract())
4373 else if (!right_type
->is_abstract())
4375 // This looks like a type error which should be diagnosed
4376 // elsewhere. Don't do anything here, to avoid an unhelpful
4377 // chain of error messages.
4382 if (type
!= NULL
&& !type
->is_abstract())
4384 if ((type
!= left_type
4385 && !Float_expression::check_constant(left_val
, type
, location
))
4386 || (type
!= right_type
4387 && !Float_expression::check_constant(right_val
, type
,
4389 || !Float_expression::check_constant(val
, type
, location
))
4390 mpfr_set_ui(val
, 0, GMP_RNDN
);
4396 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4397 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4398 // could be done, false if not.
4401 Binary_expression::eval_complex(Operator op
, Type
* left_type
,
4402 mpfr_t left_real
, mpfr_t left_imag
,
4404 mpfr_t right_real
, mpfr_t right_imag
,
4405 mpfr_t real
, mpfr_t imag
,
4406 source_location location
)
4411 case OPERATOR_ANDAND
:
4413 case OPERATOR_NOTEQ
:
4418 // These return boolean values and must be handled differently.
4421 mpfr_add(real
, left_real
, right_real
, GMP_RNDN
);
4422 mpfr_add(imag
, left_imag
, right_imag
, GMP_RNDN
);
4424 case OPERATOR_MINUS
:
4425 mpfr_sub(real
, left_real
, right_real
, GMP_RNDN
);
4426 mpfr_sub(imag
, left_imag
, right_imag
, GMP_RNDN
);
4431 case OPERATOR_BITCLEAR
:
4435 // You might think that multiplying two complex numbers would
4436 // be simple, and you would be right, until you start to think
4437 // about getting the right answer for infinity. If one
4438 // operand here is infinity and the other is anything other
4439 // than zero or NaN, then we are going to wind up subtracting
4440 // two infinity values. That will give us a NaN, but the
4441 // correct answer is infinity.
4445 mpfr_mul(lrrr
, left_real
, right_real
, GMP_RNDN
);
4449 mpfr_mul(lrri
, left_real
, right_imag
, GMP_RNDN
);
4453 mpfr_mul(lirr
, left_imag
, right_real
, GMP_RNDN
);
4457 mpfr_mul(liri
, left_imag
, right_imag
, GMP_RNDN
);
4459 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4460 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4462 // If we get NaN on both sides, check whether it should really
4463 // be infinity. The rule is that if either side of the
4464 // complex number is infinity, then the whole value is
4465 // infinity, even if the other side is NaN. So the only case
4466 // we have to fix is the one in which both sides are NaN.
4467 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4468 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4469 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4471 bool is_infinity
= false;
4475 mpfr_init_set(lr
, left_real
, GMP_RNDN
);
4476 mpfr_init_set(li
, left_imag
, GMP_RNDN
);
4480 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4481 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4483 // If the left side is infinity, then the result is
4485 if (mpfr_inf_p(lr
) || mpfr_inf_p(li
))
4487 mpfr_set_ui(lr
, mpfr_inf_p(lr
) ? 1 : 0, GMP_RNDN
);
4488 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4489 mpfr_set_ui(li
, mpfr_inf_p(li
) ? 1 : 0, GMP_RNDN
);
4490 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4493 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4494 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4498 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4499 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4504 // If the right side is infinity, then the result is
4506 if (mpfr_inf_p(rr
) || mpfr_inf_p(ri
))
4508 mpfr_set_ui(rr
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4509 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4510 mpfr_set_ui(ri
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4511 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4514 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4515 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4519 mpfr_set_ui(li
, 0, GMP_RNDN
);
4520 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4525 // If we got an overflow in the intermediate computations,
4526 // then the result is infinity.
4528 && (mpfr_inf_p(lrrr
) || mpfr_inf_p(lrri
)
4529 || mpfr_inf_p(lirr
) || mpfr_inf_p(liri
)))
4533 mpfr_set_ui(lr
, 0, GMP_RNDN
);
4534 mpfr_copysign(lr
, lr
, left_real
, GMP_RNDN
);
4538 mpfr_set_ui(li
, 0, GMP_RNDN
);
4539 mpfr_copysign(li
, li
, left_imag
, GMP_RNDN
);
4543 mpfr_set_ui(rr
, 0, GMP_RNDN
);
4544 mpfr_copysign(rr
, rr
, right_real
, GMP_RNDN
);
4548 mpfr_set_ui(ri
, 0, GMP_RNDN
);
4549 mpfr_copysign(ri
, ri
, right_imag
, GMP_RNDN
);
4556 mpfr_mul(lrrr
, lr
, rr
, GMP_RNDN
);
4557 mpfr_mul(lrri
, lr
, ri
, GMP_RNDN
);
4558 mpfr_mul(lirr
, li
, rr
, GMP_RNDN
);
4559 mpfr_mul(liri
, li
, ri
, GMP_RNDN
);
4560 mpfr_sub(real
, lrrr
, liri
, GMP_RNDN
);
4561 mpfr_add(imag
, lrri
, lirr
, GMP_RNDN
);
4562 mpfr_set_inf(real
, mpfr_sgn(real
));
4563 mpfr_set_inf(imag
, mpfr_sgn(imag
));
4580 // For complex division we want to avoid having an
4581 // intermediate overflow turn the whole result in a NaN. We
4582 // scale the values to try to avoid this.
4584 if (mpfr_zero_p(right_real
) && mpfr_zero_p(right_imag
))
4585 error_at(location
, "division by zero");
4591 mpfr_abs(rra
, right_real
, GMP_RNDN
);
4592 mpfr_abs(ria
, right_imag
, GMP_RNDN
);
4595 mpfr_max(t
, rra
, ria
, GMP_RNDN
);
4599 mpfr_init_set(rr
, right_real
, GMP_RNDN
);
4600 mpfr_init_set(ri
, right_imag
, GMP_RNDN
);
4602 if (!mpfr_inf_p(t
) && !mpfr_nan_p(t
) && !mpfr_zero_p(t
))
4604 ilogbw
= mpfr_get_exp(t
);
4605 mpfr_mul_2si(rr
, rr
, - ilogbw
, GMP_RNDN
);
4606 mpfr_mul_2si(ri
, ri
, - ilogbw
, GMP_RNDN
);
4611 mpfr_mul(denom
, rr
, rr
, GMP_RNDN
);
4612 mpfr_mul(t
, ri
, ri
, GMP_RNDN
);
4613 mpfr_add(denom
, denom
, t
, GMP_RNDN
);
4615 mpfr_mul(real
, left_real
, rr
, GMP_RNDN
);
4616 mpfr_mul(t
, left_imag
, ri
, GMP_RNDN
);
4617 mpfr_add(real
, real
, t
, GMP_RNDN
);
4618 mpfr_div(real
, real
, denom
, GMP_RNDN
);
4619 mpfr_mul_2si(real
, real
, - ilogbw
, GMP_RNDN
);
4621 mpfr_mul(imag
, left_imag
, rr
, GMP_RNDN
);
4622 mpfr_mul(t
, left_real
, ri
, GMP_RNDN
);
4623 mpfr_sub(imag
, imag
, t
, GMP_RNDN
);
4624 mpfr_div(imag
, imag
, denom
, GMP_RNDN
);
4625 mpfr_mul_2si(imag
, imag
, - ilogbw
, GMP_RNDN
);
4627 // If we wind up with NaN on both sides, check whether we
4628 // should really have infinity. The rule is that if either
4629 // side of the complex number is infinity, then the whole
4630 // value is infinity, even if the other side is NaN. So the
4631 // only case we have to fix is the one in which both sides are
4633 if (mpfr_nan_p(real
) && mpfr_nan_p(imag
)
4634 && (!mpfr_nan_p(left_real
) || !mpfr_nan_p(left_imag
))
4635 && (!mpfr_nan_p(right_real
) || !mpfr_nan_p(right_imag
)))
4637 if (mpfr_zero_p(denom
))
4639 mpfr_set_inf(real
, mpfr_sgn(rr
));
4640 mpfr_mul(real
, real
, left_real
, GMP_RNDN
);
4641 mpfr_set_inf(imag
, mpfr_sgn(rr
));
4642 mpfr_mul(imag
, imag
, left_imag
, GMP_RNDN
);
4644 else if ((mpfr_inf_p(left_real
) || mpfr_inf_p(left_imag
))
4645 && mpfr_number_p(rr
) && mpfr_number_p(ri
))
4647 mpfr_set_ui(t
, mpfr_inf_p(left_real
) ? 1 : 0, GMP_RNDN
);
4648 mpfr_copysign(t
, t
, left_real
, GMP_RNDN
);
4651 mpfr_init_set_ui(t2
, mpfr_inf_p(left_imag
) ? 1 : 0, GMP_RNDN
);
4652 mpfr_copysign(t2
, t2
, left_imag
, GMP_RNDN
);
4656 mpfr_mul(t3
, t
, rr
, GMP_RNDN
);
4660 mpfr_mul(t4
, t2
, ri
, GMP_RNDN
);
4662 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4663 mpfr_set_inf(real
, mpfr_sgn(t3
));
4665 mpfr_mul(t3
, t2
, rr
, GMP_RNDN
);
4666 mpfr_mul(t4
, t
, ri
, GMP_RNDN
);
4667 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4668 mpfr_set_inf(imag
, mpfr_sgn(t3
));
4674 else if ((mpfr_inf_p(right_real
) || mpfr_inf_p(right_imag
))
4675 && mpfr_number_p(left_real
) && mpfr_number_p(left_imag
))
4677 mpfr_set_ui(t
, mpfr_inf_p(rr
) ? 1 : 0, GMP_RNDN
);
4678 mpfr_copysign(t
, t
, rr
, GMP_RNDN
);
4681 mpfr_init_set_ui(t2
, mpfr_inf_p(ri
) ? 1 : 0, GMP_RNDN
);
4682 mpfr_copysign(t2
, t2
, ri
, GMP_RNDN
);
4686 mpfr_mul(t3
, left_real
, t
, GMP_RNDN
);
4690 mpfr_mul(t4
, left_imag
, t2
, GMP_RNDN
);
4692 mpfr_add(t3
, t3
, t4
, GMP_RNDN
);
4693 mpfr_set_ui(real
, 0, GMP_RNDN
);
4694 mpfr_mul(real
, real
, t3
, GMP_RNDN
);
4696 mpfr_mul(t3
, left_imag
, t
, GMP_RNDN
);
4697 mpfr_mul(t4
, left_real
, t2
, GMP_RNDN
);
4698 mpfr_sub(t3
, t3
, t4
, GMP_RNDN
);
4699 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4700 mpfr_mul(imag
, imag
, t3
, GMP_RNDN
);
4718 case OPERATOR_LSHIFT
:
4719 case OPERATOR_RSHIFT
:
4725 Type
* type
= left_type
;
4728 else if (type
!= right_type
&& right_type
!= NULL
)
4730 if (type
->is_abstract())
4732 else if (!right_type
->is_abstract())
4734 // This looks like a type error which should be diagnosed
4735 // elsewhere. Don't do anything here, to avoid an unhelpful
4736 // chain of error messages.
4741 if (type
!= NULL
&& !type
->is_abstract())
4743 if ((type
!= left_type
4744 && !Complex_expression::check_constant(left_real
, left_imag
,
4746 || (type
!= right_type
4747 && !Complex_expression::check_constant(right_real
, right_imag
,
4749 || !Complex_expression::check_constant(real
, imag
, type
,
4752 mpfr_set_ui(real
, 0, GMP_RNDN
);
4753 mpfr_set_ui(imag
, 0, GMP_RNDN
);
4760 // Lower a binary expression. We have to evaluate constant
4761 // expressions now, in order to implement Go's unlimited precision
4765 Binary_expression::do_lower(Gogo
*, Named_object
*, int)
4767 source_location location
= this->location();
4768 Operator op
= this->op_
;
4769 Expression
* left
= this->left_
;
4770 Expression
* right
= this->right_
;
4772 const bool is_comparison
= (op
== OPERATOR_EQEQ
4773 || op
== OPERATOR_NOTEQ
4774 || op
== OPERATOR_LT
4775 || op
== OPERATOR_LE
4776 || op
== OPERATOR_GT
4777 || op
== OPERATOR_GE
);
4779 // Integer constant expressions.
4785 mpz_init(right_val
);
4787 if (left
->integer_constant_value(false, left_val
, &left_type
)
4788 && right
->integer_constant_value(false, right_val
, &right_type
))
4790 Expression
* ret
= NULL
;
4791 if (left_type
!= right_type
4792 && left_type
!= NULL
4793 && right_type
!= NULL
4794 && left_type
->base() != right_type
->base()
4795 && op
!= OPERATOR_LSHIFT
4796 && op
!= OPERATOR_RSHIFT
)
4798 // May be a type error--let it be diagnosed later.
4800 else if (is_comparison
)
4802 bool b
= Binary_expression::compare_integer(op
, left_val
,
4804 ret
= Expression::make_cast(Type::lookup_bool_type(),
4805 Expression::make_boolean(b
, location
),
4813 if (Binary_expression::eval_integer(op
, left_type
, left_val
,
4814 right_type
, right_val
,
4817 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
);
4819 if (op
== OPERATOR_LSHIFT
|| op
== OPERATOR_RSHIFT
)
4821 else if (left_type
== NULL
)
4823 else if (right_type
== NULL
)
4825 else if (!left_type
->is_abstract()
4826 && left_type
->named_type() != NULL
)
4828 else if (!right_type
->is_abstract()
4829 && right_type
->named_type() != NULL
)
4831 else if (!left_type
->is_abstract())
4833 else if (!right_type
->is_abstract())
4835 else if (left_type
->float_type() != NULL
)
4837 else if (right_type
->float_type() != NULL
)
4839 else if (left_type
->complex_type() != NULL
)
4841 else if (right_type
->complex_type() != NULL
)
4845 ret
= Expression::make_integer(&val
, type
, location
);
4853 mpz_clear(right_val
);
4854 mpz_clear(left_val
);
4858 mpz_clear(right_val
);
4859 mpz_clear(left_val
);
4862 // Floating point constant expressions.
4865 mpfr_init(left_val
);
4868 mpfr_init(right_val
);
4870 if (left
->float_constant_value(left_val
, &left_type
)
4871 && right
->float_constant_value(right_val
, &right_type
))
4873 Expression
* ret
= NULL
;
4874 if (left_type
!= right_type
4875 && left_type
!= NULL
4876 && right_type
!= NULL
4877 && left_type
->base() != right_type
->base()
4878 && op
!= OPERATOR_LSHIFT
4879 && op
!= OPERATOR_RSHIFT
)
4881 // May be a type error--let it be diagnosed later.
4883 else if (is_comparison
)
4885 bool b
= Binary_expression::compare_float(op
,
4889 left_val
, right_val
);
4890 ret
= Expression::make_boolean(b
, location
);
4897 if (Binary_expression::eval_float(op
, left_type
, left_val
,
4898 right_type
, right_val
, val
,
4901 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
4902 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
4904 if (left_type
== NULL
)
4906 else if (right_type
== NULL
)
4908 else if (!left_type
->is_abstract()
4909 && left_type
->named_type() != NULL
)
4911 else if (!right_type
->is_abstract()
4912 && right_type
->named_type() != NULL
)
4914 else if (!left_type
->is_abstract())
4916 else if (!right_type
->is_abstract())
4918 else if (left_type
->float_type() != NULL
)
4920 else if (right_type
->float_type() != NULL
)
4924 ret
= Expression::make_float(&val
, type
, location
);
4932 mpfr_clear(right_val
);
4933 mpfr_clear(left_val
);
4937 mpfr_clear(right_val
);
4938 mpfr_clear(left_val
);
4941 // Complex constant expressions.
4945 mpfr_init(left_real
);
4946 mpfr_init(left_imag
);
4951 mpfr_init(right_real
);
4952 mpfr_init(right_imag
);
4955 if (left
->complex_constant_value(left_real
, left_imag
, &left_type
)
4956 && right
->complex_constant_value(right_real
, right_imag
, &right_type
))
4958 Expression
* ret
= NULL
;
4959 if (left_type
!= right_type
4960 && left_type
!= NULL
4961 && right_type
!= NULL
4962 && left_type
->base() != right_type
->base())
4964 // May be a type error--let it be diagnosed later.
4966 else if (is_comparison
)
4968 bool b
= Binary_expression::compare_complex(op
,
4976 ret
= Expression::make_boolean(b
, location
);
4985 if (Binary_expression::eval_complex(op
, left_type
,
4986 left_real
, left_imag
,
4988 right_real
, right_imag
,
4992 gcc_assert(op
!= OPERATOR_OROR
&& op
!= OPERATOR_ANDAND
4993 && op
!= OPERATOR_LSHIFT
&& op
!= OPERATOR_RSHIFT
);
4995 if (left_type
== NULL
)
4997 else if (right_type
== NULL
)
4999 else if (!left_type
->is_abstract()
5000 && left_type
->named_type() != NULL
)
5002 else if (!right_type
->is_abstract()
5003 && right_type
->named_type() != NULL
)
5005 else if (!left_type
->is_abstract())
5007 else if (!right_type
->is_abstract())
5009 else if (left_type
->complex_type() != NULL
)
5011 else if (right_type
->complex_type() != NULL
)
5015 ret
= Expression::make_complex(&real
, &imag
, type
,
5024 mpfr_clear(left_real
);
5025 mpfr_clear(left_imag
);
5026 mpfr_clear(right_real
);
5027 mpfr_clear(right_imag
);
5032 mpfr_clear(left_real
);
5033 mpfr_clear(left_imag
);
5034 mpfr_clear(right_real
);
5035 mpfr_clear(right_imag
);
5038 // String constant expressions.
5039 if (op
== OPERATOR_PLUS
5040 && left
->type()->is_string_type()
5041 && right
->type()->is_string_type())
5043 std::string left_string
;
5044 std::string right_string
;
5045 if (left
->string_constant_value(&left_string
)
5046 && right
->string_constant_value(&right_string
))
5047 return Expression::make_string(left_string
+ right_string
, location
);
5053 // Return the integer constant value, if it has one.
5056 Binary_expression::do_integer_constant_value(bool iota_is_constant
, mpz_t val
,
5062 if (!this->left_
->integer_constant_value(iota_is_constant
, left_val
,
5065 mpz_clear(left_val
);
5070 mpz_init(right_val
);
5072 if (!this->right_
->integer_constant_value(iota_is_constant
, right_val
,
5075 mpz_clear(right_val
);
5076 mpz_clear(left_val
);
5081 if (left_type
!= right_type
5082 && left_type
!= NULL
5083 && right_type
!= NULL
5084 && left_type
->base() != right_type
->base()
5085 && this->op_
!= OPERATOR_RSHIFT
5086 && this->op_
!= OPERATOR_LSHIFT
)
5089 ret
= Binary_expression::eval_integer(this->op_
, left_type
, left_val
,
5090 right_type
, right_val
,
5091 this->location(), val
);
5093 mpz_clear(right_val
);
5094 mpz_clear(left_val
);
5102 // Return the floating point constant value, if it has one.
5105 Binary_expression::do_float_constant_value(mpfr_t val
, Type
** ptype
) const
5108 mpfr_init(left_val
);
5110 if (!this->left_
->float_constant_value(left_val
, &left_type
))
5112 mpfr_clear(left_val
);
5117 mpfr_init(right_val
);
5119 if (!this->right_
->float_constant_value(right_val
, &right_type
))
5121 mpfr_clear(right_val
);
5122 mpfr_clear(left_val
);
5127 if (left_type
!= right_type
5128 && left_type
!= NULL
5129 && right_type
!= NULL
5130 && left_type
->base() != right_type
->base())
5133 ret
= Binary_expression::eval_float(this->op_
, left_type
, left_val
,
5134 right_type
, right_val
,
5135 val
, this->location());
5137 mpfr_clear(left_val
);
5138 mpfr_clear(right_val
);
5146 // Return the complex constant value, if it has one.
5149 Binary_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
5154 mpfr_init(left_real
);
5155 mpfr_init(left_imag
);
5157 if (!this->left_
->complex_constant_value(left_real
, left_imag
, &left_type
))
5159 mpfr_clear(left_real
);
5160 mpfr_clear(left_imag
);
5166 mpfr_init(right_real
);
5167 mpfr_init(right_imag
);
5169 if (!this->right_
->complex_constant_value(right_real
, right_imag
,
5172 mpfr_clear(left_real
);
5173 mpfr_clear(left_imag
);
5174 mpfr_clear(right_real
);
5175 mpfr_clear(right_imag
);
5180 if (left_type
!= right_type
5181 && left_type
!= NULL
5182 && right_type
!= NULL
5183 && left_type
->base() != right_type
->base())
5186 ret
= Binary_expression::eval_complex(this->op_
, left_type
,
5187 left_real
, left_imag
,
5189 right_real
, right_imag
,
5192 mpfr_clear(left_real
);
5193 mpfr_clear(left_imag
);
5194 mpfr_clear(right_real
);
5195 mpfr_clear(right_imag
);
5203 // Note that the value is being discarded.
5206 Binary_expression::do_discarding_value()
5208 if (this->op_
== OPERATOR_OROR
|| this->op_
== OPERATOR_ANDAND
)
5209 this->right_
->discarding_value();
5211 this->warn_about_unused_value();
5217 Binary_expression::do_type()
5222 case OPERATOR_ANDAND
:
5224 case OPERATOR_NOTEQ
:
5229 return Type::lookup_bool_type();
5232 case OPERATOR_MINUS
:
5239 case OPERATOR_BITCLEAR
:
5241 Type
* left_type
= this->left_
->type();
5242 Type
* right_type
= this->right_
->type();
5243 if (!left_type
->is_abstract() && left_type
->named_type() != NULL
)
5245 else if (!right_type
->is_abstract() && right_type
->named_type() != NULL
)
5247 else if (!left_type
->is_abstract())
5249 else if (!right_type
->is_abstract())
5251 else if (left_type
->complex_type() != NULL
)
5253 else if (right_type
->complex_type() != NULL
)
5255 else if (left_type
->float_type() != NULL
)
5257 else if (right_type
->float_type() != NULL
)
5263 case OPERATOR_LSHIFT
:
5264 case OPERATOR_RSHIFT
:
5265 return this->left_
->type();
5272 // Set type for a binary expression.
5275 Binary_expression::do_determine_type(const Type_context
* context
)
5277 Type
* tleft
= this->left_
->type();
5278 Type
* tright
= this->right_
->type();
5280 // Both sides should have the same type, except for the shift
5281 // operations. For a comparison, we should ignore the incoming
5284 bool is_shift_op
= (this->op_
== OPERATOR_LSHIFT
5285 || this->op_
== OPERATOR_RSHIFT
);
5287 bool is_comparison
= (this->op_
== OPERATOR_EQEQ
5288 || this->op_
== OPERATOR_NOTEQ
5289 || this->op_
== OPERATOR_LT
5290 || this->op_
== OPERATOR_LE
5291 || this->op_
== OPERATOR_GT
5292 || this->op_
== OPERATOR_GE
);
5294 Type_context
subcontext(*context
);
5298 // In a comparison, the context does not determine the types of
5300 subcontext
.type
= NULL
;
5303 // Set the context for the left hand operand.
5306 // The right hand operand plays no role in determining the type
5307 // of the left hand operand. A shift of an abstract integer in
5308 // a string context gets special treatment, which may be a
5310 if (subcontext
.type
!= NULL
5311 && subcontext
.type
->is_string_type()
5312 && tleft
->is_abstract())
5313 error_at(this->location(), "shift of non-integer operand");
5315 else if (!tleft
->is_abstract())
5316 subcontext
.type
= tleft
;
5317 else if (!tright
->is_abstract())
5318 subcontext
.type
= tright
;
5319 else if (subcontext
.type
== NULL
)
5321 if ((tleft
->integer_type() != NULL
&& tright
->integer_type() != NULL
)
5322 || (tleft
->float_type() != NULL
&& tright
->float_type() != NULL
)
5323 || (tleft
->complex_type() != NULL
&& tright
->complex_type() != NULL
))
5325 // Both sides have an abstract integer, abstract float, or
5326 // abstract complex type. Just let CONTEXT determine
5327 // whether they may remain abstract or not.
5329 else if (tleft
->complex_type() != NULL
)
5330 subcontext
.type
= tleft
;
5331 else if (tright
->complex_type() != NULL
)
5332 subcontext
.type
= tright
;
5333 else if (tleft
->float_type() != NULL
)
5334 subcontext
.type
= tleft
;
5335 else if (tright
->float_type() != NULL
)
5336 subcontext
.type
= tright
;
5338 subcontext
.type
= tleft
;
5341 this->left_
->determine_type(&subcontext
);
5343 // The context for the right hand operand is the same as for the
5344 // left hand operand, except for a shift operator.
5347 subcontext
.type
= Type::lookup_integer_type("uint");
5348 subcontext
.may_be_abstract
= false;
5351 this->right_
->determine_type(&subcontext
);
5354 // Report an error if the binary operator OP does not support TYPE.
5355 // Return whether the operation is OK. This should not be used for
5359 Binary_expression::check_operator_type(Operator op
, Type
* type
,
5360 source_location location
)
5365 case OPERATOR_ANDAND
:
5366 if (!type
->is_boolean_type())
5368 error_at(location
, "expected boolean type");
5374 case OPERATOR_NOTEQ
:
5375 if (type
->integer_type() == NULL
5376 && type
->float_type() == NULL
5377 && type
->complex_type() == NULL
5378 && !type
->is_string_type()
5379 && type
->points_to() == NULL
5380 && !type
->is_nil_type()
5381 && !type
->is_boolean_type()
5382 && type
->interface_type() == NULL
5383 && (type
->array_type() == NULL
5384 || type
->array_type()->length() != NULL
)
5385 && type
->map_type() == NULL
5386 && type
->channel_type() == NULL
5387 && type
->function_type() == NULL
)
5390 ("expected integer, floating, complex, string, pointer, "
5391 "boolean, interface, slice, map, channel, "
5392 "or function type"));
5401 if (type
->integer_type() == NULL
5402 && type
->float_type() == NULL
5403 && !type
->is_string_type())
5405 error_at(location
, "expected integer, floating, or string type");
5411 case OPERATOR_PLUSEQ
:
5412 if (type
->integer_type() == NULL
5413 && type
->float_type() == NULL
5414 && type
->complex_type() == NULL
5415 && !type
->is_string_type())
5418 "expected integer, floating, complex, or string type");
5423 case OPERATOR_MINUS
:
5424 case OPERATOR_MINUSEQ
:
5426 case OPERATOR_MULTEQ
:
5428 case OPERATOR_DIVEQ
:
5429 if (type
->integer_type() == NULL
5430 && type
->float_type() == NULL
5431 && type
->complex_type() == NULL
)
5433 error_at(location
, "expected integer, floating, or complex type");
5439 case OPERATOR_MODEQ
:
5443 case OPERATOR_ANDEQ
:
5445 case OPERATOR_XOREQ
:
5446 case OPERATOR_BITCLEAR
:
5447 case OPERATOR_BITCLEAREQ
:
5448 if (type
->integer_type() == NULL
)
5450 error_at(location
, "expected integer type");
5465 Binary_expression::do_check_types(Gogo
*)
5467 Type
* left_type
= this->left_
->type();
5468 Type
* right_type
= this->right_
->type();
5469 if (left_type
->is_error_type() || right_type
->is_error_type())
5472 if (this->op_
== OPERATOR_EQEQ
5473 || this->op_
== OPERATOR_NOTEQ
5474 || this->op_
== OPERATOR_LT
5475 || this->op_
== OPERATOR_LE
5476 || this->op_
== OPERATOR_GT
5477 || this->op_
== OPERATOR_GE
)
5479 if (!Type::are_assignable(left_type
, right_type
, NULL
)
5480 && !Type::are_assignable(right_type
, left_type
, NULL
))
5482 this->report_error(_("incompatible types in binary expression"));
5485 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5487 || !Binary_expression::check_operator_type(this->op_
, right_type
,
5490 this->set_is_error();
5494 else if (this->op_
!= OPERATOR_LSHIFT
&& this->op_
!= OPERATOR_RSHIFT
)
5496 if (!Type::are_compatible_for_binop(left_type
, right_type
))
5498 this->report_error(_("incompatible types in binary expression"));
5501 if (!Binary_expression::check_operator_type(this->op_
, left_type
,
5504 this->set_is_error();
5510 if (left_type
->integer_type() == NULL
)
5511 this->report_error(_("shift of non-integer operand"));
5513 if (!right_type
->is_abstract()
5514 && (right_type
->integer_type() == NULL
5515 || !right_type
->integer_type()->is_unsigned()))
5516 this->report_error(_("shift count not unsigned integer"));
5522 if (this->right_
->integer_constant_value(true, val
, &type
))
5524 if (mpz_sgn(val
) < 0)
5525 this->report_error(_("negative shift count"));
5532 // Get a tree for a binary expression.
5535 Binary_expression::do_get_tree(Translate_context
* context
)
5537 tree left
= this->left_
->get_tree(context
);
5538 tree right
= this->right_
->get_tree(context
);
5540 if (left
== error_mark_node
|| right
== error_mark_node
)
5541 return error_mark_node
;
5543 enum tree_code code
;
5544 bool use_left_type
= true;
5545 bool is_shift_op
= false;
5549 case OPERATOR_NOTEQ
:
5554 return Expression::comparison_tree(context
, this->op_
,
5555 this->left_
->type(), left
,
5556 this->right_
->type(), right
,
5560 code
= TRUTH_ORIF_EXPR
;
5561 use_left_type
= false;
5563 case OPERATOR_ANDAND
:
5564 code
= TRUTH_ANDIF_EXPR
;
5565 use_left_type
= false;
5570 case OPERATOR_MINUS
:
5574 code
= BIT_IOR_EXPR
;
5577 code
= BIT_XOR_EXPR
;
5584 Type
*t
= this->left_
->type();
5585 if (t
->float_type() != NULL
|| t
->complex_type() != NULL
)
5588 code
= TRUNC_DIV_EXPR
;
5592 code
= TRUNC_MOD_EXPR
;
5594 case OPERATOR_LSHIFT
:
5598 case OPERATOR_RSHIFT
:
5603 code
= BIT_AND_EXPR
;
5605 case OPERATOR_BITCLEAR
:
5606 right
= fold_build1(BIT_NOT_EXPR
, TREE_TYPE(right
), right
);
5607 code
= BIT_AND_EXPR
;
5613 tree type
= use_left_type
? TREE_TYPE(left
) : TREE_TYPE(right
);
5615 if (this->left_
->type()->is_string_type())
5617 gcc_assert(this->op_
== OPERATOR_PLUS
);
5618 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5619 static tree string_plus_decl
;
5620 return Gogo::call_builtin(&string_plus_decl
,
5631 tree compute_type
= excess_precision_type(type
);
5632 if (compute_type
!= NULL_TREE
)
5634 left
= ::convert(compute_type
, left
);
5635 right
= ::convert(compute_type
, right
);
5638 tree eval_saved
= NULL_TREE
;
5642 left
= save_expr(left
);
5644 right
= save_expr(right
);
5645 // Make sure the values are evaluated.
5646 eval_saved
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5647 void_type_node
, left
, right
);
5650 tree ret
= fold_build2_loc(this->location(),
5652 compute_type
!= NULL_TREE
? compute_type
: type
,
5655 if (compute_type
!= NULL_TREE
)
5656 ret
= ::convert(type
, ret
);
5658 // In Go, a shift larger than the size of the type is well-defined.
5659 // This is not true in GENERIC, so we need to insert a conditional.
5662 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left
)));
5663 gcc_assert(this->left_
->type()->integer_type() != NULL
);
5664 int bits
= TYPE_PRECISION(TREE_TYPE(left
));
5666 tree compare
= fold_build2(LT_EXPR
, boolean_type_node
, right
,
5667 build_int_cst_type(TREE_TYPE(right
), bits
));
5669 tree overflow_result
= fold_convert_loc(this->location(),
5672 if (this->op_
== OPERATOR_RSHIFT
5673 && !this->left_
->type()->integer_type()->is_unsigned())
5675 tree neg
= fold_build2_loc(this->location(), LT_EXPR
,
5676 boolean_type_node
, left
,
5677 fold_convert_loc(this->location(),
5679 integer_zero_node
));
5680 tree neg_one
= fold_build2_loc(this->location(),
5681 MINUS_EXPR
, TREE_TYPE(left
),
5682 fold_convert_loc(this->location(),
5685 fold_convert_loc(this->location(),
5688 overflow_result
= fold_build3_loc(this->location(), COND_EXPR
,
5689 TREE_TYPE(left
), neg
, neg_one
,
5693 ret
= fold_build3_loc(this->location(), COND_EXPR
, TREE_TYPE(left
),
5694 compare
, ret
, overflow_result
);
5696 ret
= fold_build2_loc(this->location(), COMPOUND_EXPR
,
5697 TREE_TYPE(ret
), eval_saved
, ret
);
5703 // Export a binary expression.
5706 Binary_expression::do_export(Export
* exp
) const
5708 exp
->write_c_string("(");
5709 this->left_
->export_expression(exp
);
5713 exp
->write_c_string(" || ");
5715 case OPERATOR_ANDAND
:
5716 exp
->write_c_string(" && ");
5719 exp
->write_c_string(" == ");
5721 case OPERATOR_NOTEQ
:
5722 exp
->write_c_string(" != ");
5725 exp
->write_c_string(" < ");
5728 exp
->write_c_string(" <= ");
5731 exp
->write_c_string(" > ");
5734 exp
->write_c_string(" >= ");
5737 exp
->write_c_string(" + ");
5739 case OPERATOR_MINUS
:
5740 exp
->write_c_string(" - ");
5743 exp
->write_c_string(" | ");
5746 exp
->write_c_string(" ^ ");
5749 exp
->write_c_string(" * ");
5752 exp
->write_c_string(" / ");
5755 exp
->write_c_string(" % ");
5757 case OPERATOR_LSHIFT
:
5758 exp
->write_c_string(" << ");
5760 case OPERATOR_RSHIFT
:
5761 exp
->write_c_string(" >> ");
5764 exp
->write_c_string(" & ");
5766 case OPERATOR_BITCLEAR
:
5767 exp
->write_c_string(" &^ ");
5772 this->right_
->export_expression(exp
);
5773 exp
->write_c_string(")");
5776 // Import a binary expression.
5779 Binary_expression::do_import(Import
* imp
)
5781 imp
->require_c_string("(");
5783 Expression
* left
= Expression::import_expression(imp
);
5786 if (imp
->match_c_string(" || "))
5791 else if (imp
->match_c_string(" && "))
5793 op
= OPERATOR_ANDAND
;
5796 else if (imp
->match_c_string(" == "))
5801 else if (imp
->match_c_string(" != "))
5803 op
= OPERATOR_NOTEQ
;
5806 else if (imp
->match_c_string(" < "))
5811 else if (imp
->match_c_string(" <= "))
5816 else if (imp
->match_c_string(" > "))
5821 else if (imp
->match_c_string(" >= "))
5826 else if (imp
->match_c_string(" + "))
5831 else if (imp
->match_c_string(" - "))
5833 op
= OPERATOR_MINUS
;
5836 else if (imp
->match_c_string(" | "))
5841 else if (imp
->match_c_string(" ^ "))
5846 else if (imp
->match_c_string(" * "))
5851 else if (imp
->match_c_string(" / "))
5856 else if (imp
->match_c_string(" % "))
5861 else if (imp
->match_c_string(" << "))
5863 op
= OPERATOR_LSHIFT
;
5866 else if (imp
->match_c_string(" >> "))
5868 op
= OPERATOR_RSHIFT
;
5871 else if (imp
->match_c_string(" & "))
5876 else if (imp
->match_c_string(" &^ "))
5878 op
= OPERATOR_BITCLEAR
;
5883 error_at(imp
->location(), "unrecognized binary operator");
5884 return Expression::make_error(imp
->location());
5887 Expression
* right
= Expression::import_expression(imp
);
5889 imp
->require_c_string(")");
5891 return Expression::make_binary(op
, left
, right
, imp
->location());
5894 // Make a binary expression.
5897 Expression::make_binary(Operator op
, Expression
* left
, Expression
* right
,
5898 source_location location
)
5900 return new Binary_expression(op
, left
, right
, location
);
5903 // Implement a comparison.
5906 Expression::comparison_tree(Translate_context
* context
, Operator op
,
5907 Type
* left_type
, tree left_tree
,
5908 Type
* right_type
, tree right_tree
,
5909 source_location location
)
5911 enum tree_code code
;
5917 case OPERATOR_NOTEQ
:
5936 if (left_type
->is_string_type())
5938 gcc_assert(right_type
->is_string_type());
5939 tree string_type
= Type::make_string_type()->get_tree(context
->gogo());
5940 static tree string_compare_decl
;
5941 left_tree
= Gogo::call_builtin(&string_compare_decl
,
5950 right_tree
= build_int_cst_type(integer_type_node
, 0);
5953 if ((left_type
->interface_type() != NULL
5954 && right_type
->interface_type() == NULL
5955 && !right_type
->is_nil_type())
5956 || (left_type
->interface_type() == NULL
5957 && !left_type
->is_nil_type()
5958 && right_type
->interface_type() != NULL
))
5960 // Comparing an interface value to a non-interface value.
5961 if (left_type
->interface_type() == NULL
)
5963 std::swap(left_type
, right_type
);
5964 std::swap(left_tree
, right_tree
);
5967 // The right operand is not an interface. We need to take its
5968 // address if it is not a pointer.
5971 if (right_type
->points_to() != NULL
)
5973 make_tmp
= NULL_TREE
;
5976 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree
)) || DECL_P(right_tree
))
5978 make_tmp
= NULL_TREE
;
5979 arg
= build_fold_addr_expr_loc(location
, right_tree
);
5980 if (DECL_P(right_tree
))
5981 TREE_ADDRESSABLE(right_tree
) = 1;
5985 tree tmp
= create_tmp_var(TREE_TYPE(right_tree
),
5986 get_name(right_tree
));
5987 DECL_IGNORED_P(tmp
) = 0;
5988 DECL_INITIAL(tmp
) = right_tree
;
5989 TREE_ADDRESSABLE(tmp
) = 1;
5990 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
5991 SET_EXPR_LOCATION(make_tmp
, location
);
5992 arg
= build_fold_addr_expr_loc(location
, tmp
);
5994 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
5996 tree descriptor
= right_type
->type_descriptor_pointer(context
->gogo());
5998 if (left_type
->interface_type()->is_empty())
6000 static tree empty_interface_value_compare_decl
;
6001 left_tree
= Gogo::call_builtin(&empty_interface_value_compare_decl
,
6003 "__go_empty_interface_value_compare",
6006 TREE_TYPE(left_tree
),
6008 TREE_TYPE(descriptor
),
6012 // This can panic if the type is not comparable.
6013 TREE_NOTHROW(empty_interface_value_compare_decl
) = 0;
6017 static tree interface_value_compare_decl
;
6018 left_tree
= Gogo::call_builtin(&interface_value_compare_decl
,
6020 "__go_interface_value_compare",
6023 TREE_TYPE(left_tree
),
6025 TREE_TYPE(descriptor
),
6029 // This can panic if the type is not comparable.
6030 TREE_NOTHROW(interface_value_compare_decl
) = 0;
6032 right_tree
= build_int_cst_type(integer_type_node
, 0);
6034 if (make_tmp
!= NULL_TREE
)
6035 left_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(left_tree
), make_tmp
,
6038 else if (left_type
->interface_type() != NULL
6039 && right_type
->interface_type() != NULL
)
6041 if (left_type
->interface_type()->is_empty())
6043 gcc_assert(right_type
->interface_type()->is_empty());
6044 static tree empty_interface_compare_decl
;
6045 left_tree
= Gogo::call_builtin(&empty_interface_compare_decl
,
6047 "__go_empty_interface_compare",
6050 TREE_TYPE(left_tree
),
6052 TREE_TYPE(right_tree
),
6054 // This can panic if the type is uncomparable.
6055 TREE_NOTHROW(empty_interface_compare_decl
) = 0;
6059 gcc_assert(!right_type
->interface_type()->is_empty());
6060 static tree interface_compare_decl
;
6061 left_tree
= Gogo::call_builtin(&interface_compare_decl
,
6063 "__go_interface_compare",
6066 TREE_TYPE(left_tree
),
6068 TREE_TYPE(right_tree
),
6070 // This can panic if the type is uncomparable.
6071 TREE_NOTHROW(interface_compare_decl
) = 0;
6073 right_tree
= build_int_cst_type(integer_type_node
, 0);
6076 if (left_type
->is_nil_type()
6077 && (op
== OPERATOR_EQEQ
|| op
== OPERATOR_NOTEQ
))
6079 std::swap(left_type
, right_type
);
6080 std::swap(left_tree
, right_tree
);
6083 if (right_type
->is_nil_type())
6085 if (left_type
->array_type() != NULL
6086 && left_type
->array_type()->length() == NULL
)
6088 Array_type
* at
= left_type
->array_type();
6089 left_tree
= at
->value_pointer_tree(context
->gogo(), left_tree
);
6090 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6092 else if (left_type
->interface_type() != NULL
)
6094 // An interface is nil if the first field is nil.
6095 tree left_type_tree
= TREE_TYPE(left_tree
);
6096 gcc_assert(TREE_CODE(left_type_tree
) == RECORD_TYPE
);
6097 tree field
= TYPE_FIELDS(left_type_tree
);
6098 left_tree
= build3(COMPONENT_REF
, TREE_TYPE(field
), left_tree
,
6100 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6104 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree
)));
6105 right_tree
= fold_convert(TREE_TYPE(left_tree
), null_pointer_node
);
6109 tree ret
= fold_build2(code
, boolean_type_node
, left_tree
, right_tree
);
6110 if (CAN_HAVE_LOCATION_P(ret
))
6111 SET_EXPR_LOCATION(ret
, location
);
6115 // Class Bound_method_expression.
6120 Bound_method_expression::do_traverse(Traverse
* traverse
)
6122 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
)
6123 return TRAVERSE_EXIT
;
6124 return Expression::traverse(&this->method_
, traverse
);
6127 // Return the type of a bound method expression. The type of this
6128 // object is really the type of the method with no receiver. We
6129 // should be able to get away with just returning the type of the
6133 Bound_method_expression::do_type()
6135 return this->method_
->type();
6138 // Determine the types of a method expression.
6141 Bound_method_expression::do_determine_type(const Type_context
*)
6143 this->method_
->determine_type_no_context();
6144 Type
* mtype
= this->method_
->type();
6145 Function_type
* fntype
= mtype
== NULL
? NULL
: mtype
->function_type();
6146 if (fntype
== NULL
|| !fntype
->is_method())
6147 this->expr_
->determine_type_no_context();
6150 Type_context
subcontext(fntype
->receiver()->type(), false);
6151 this->expr_
->determine_type(&subcontext
);
6155 // Check the types of a method expression.
6158 Bound_method_expression::do_check_types(Gogo
*)
6160 Type
* type
= this->method_
->type()->deref();
6162 || type
->function_type() == NULL
6163 || !type
->function_type()->is_method())
6164 this->report_error(_("object is not a method"));
6167 Type
* rtype
= type
->function_type()->receiver()->type()->deref();
6168 Type
* etype
= (this->expr_type_
!= NULL
6170 : this->expr_
->type());
6171 etype
= etype
->deref();
6172 if (!Type::are_identical(rtype
, etype
, NULL
))
6173 this->report_error(_("method type does not match object type"));
6177 // Get the tree for a method expression. There is no standard tree
6178 // representation for this. The only places it may currently be used
6179 // are in a Call_expression or a Go_statement, which will take it
6180 // apart directly. So this has nothing to do at present.
6183 Bound_method_expression::do_get_tree(Translate_context
*)
6188 // Make a method expression.
6190 Bound_method_expression
*
6191 Expression::make_bound_method(Expression
* expr
, Expression
* method
,
6192 source_location location
)
6194 return new Bound_method_expression(expr
, method
, location
);
6197 // Class Builtin_call_expression. This is used for a call to a
6198 // builtin function.
6200 class Builtin_call_expression
: public Call_expression
6203 Builtin_call_expression(Gogo
* gogo
, Expression
* fn
, Expression_list
* args
,
6204 bool is_varargs
, source_location location
);
6207 // This overrides Call_expression::do_lower.
6209 do_lower(Gogo
*, Named_object
*, int);
6212 do_is_constant() const;
6215 do_integer_constant_value(bool, mpz_t
, Type
**) const;
6218 do_float_constant_value(mpfr_t
, Type
**) const;
6221 do_complex_constant_value(mpfr_t
, mpfr_t
, Type
**) const;
6227 do_determine_type(const Type_context
*);
6230 do_check_types(Gogo
*);
6235 return new Builtin_call_expression(this->gogo_
, this->fn()->copy(),
6236 this->args()->copy(),
6242 do_get_tree(Translate_context
*);
6245 do_export(Export
*) const;
6248 do_is_recover_call() const;
6251 do_set_recover_arg(Expression
*);
6254 // The builtin functions.
6255 enum Builtin_function_code
6259 // Predeclared builtin functions.
6276 // Builtin functions from the unsafe package.
6289 real_imag_type(Type
*);
6294 // A pointer back to the general IR structure. This avoids a global
6295 // variable, or passing it around everywhere.
6297 // The builtin function being called.
6298 Builtin_function_code code_
;
6301 Builtin_call_expression::Builtin_call_expression(Gogo
* gogo
,
6303 Expression_list
* args
,
6305 source_location location
)
6306 : Call_expression(fn
, args
, is_varargs
, location
),
6307 gogo_(gogo
), code_(BUILTIN_INVALID
)
6309 Func_expression
* fnexp
= this->fn()->func_expression();
6310 gcc_assert(fnexp
!= NULL
);
6311 const std::string
& name(fnexp
->named_object()->name());
6312 if (name
== "append")
6313 this->code_
= BUILTIN_APPEND
;
6314 else if (name
== "cap")
6315 this->code_
= BUILTIN_CAP
;
6316 else if (name
== "close")
6317 this->code_
= BUILTIN_CLOSE
;
6318 else if (name
== "closed")
6319 this->code_
= BUILTIN_CLOSED
;
6320 else if (name
== "cmplx")
6321 this->code_
= BUILTIN_CMPLX
;
6322 else if (name
== "copy")
6323 this->code_
= BUILTIN_COPY
;
6324 else if (name
== "imag")
6325 this->code_
= BUILTIN_IMAG
;
6326 else if (name
== "len")
6327 this->code_
= BUILTIN_LEN
;
6328 else if (name
== "make")
6329 this->code_
= BUILTIN_MAKE
;
6330 else if (name
== "new")
6331 this->code_
= BUILTIN_NEW
;
6332 else if (name
== "panic")
6333 this->code_
= BUILTIN_PANIC
;
6334 else if (name
== "print")
6335 this->code_
= BUILTIN_PRINT
;
6336 else if (name
== "println")
6337 this->code_
= BUILTIN_PRINTLN
;
6338 else if (name
== "real")
6339 this->code_
= BUILTIN_REAL
;
6340 else if (name
== "recover")
6341 this->code_
= BUILTIN_RECOVER
;
6342 else if (name
== "Alignof")
6343 this->code_
= BUILTIN_ALIGNOF
;
6344 else if (name
== "Offsetof")
6345 this->code_
= BUILTIN_OFFSETOF
;
6346 else if (name
== "Sizeof")
6347 this->code_
= BUILTIN_SIZEOF
;
6352 // Return whether this is a call to recover. This is a virtual
6353 // function called from the parent class.
6356 Builtin_call_expression::do_is_recover_call() const
6358 if (this->classification() == EXPRESSION_ERROR
)
6360 return this->code_
== BUILTIN_RECOVER
;
6363 // Set the argument for a call to recover.
6366 Builtin_call_expression::do_set_recover_arg(Expression
* arg
)
6368 const Expression_list
* args
= this->args();
6369 gcc_assert(args
== NULL
|| args
->empty());
6370 Expression_list
* new_args
= new Expression_list();
6371 new_args
->push_back(arg
);
6372 this->set_args(new_args
);
6375 // A traversal class which looks for a call expression.
6377 class Find_call_expression
: public Traverse
6380 Find_call_expression()
6381 : Traverse(traverse_expressions
),
6386 expression(Expression
**);
6390 { return this->found_
; }
6397 Find_call_expression::expression(Expression
** pexpr
)
6399 if ((*pexpr
)->call_expression() != NULL
)
6401 this->found_
= true;
6402 return TRAVERSE_EXIT
;
6404 return TRAVERSE_CONTINUE
;
6407 // Lower a builtin call expression. This turns new and make into
6408 // specific expressions. We also convert to a constant if we can.
6411 Builtin_call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
6413 if (this->code_
== BUILTIN_NEW
)
6415 const Expression_list
* args
= this->args();
6416 if (args
== NULL
|| args
->size() < 1)
6417 this->report_error(_("not enough arguments"));
6418 else if (args
->size() > 1)
6419 this->report_error(_("too many arguments"));
6422 Expression
* arg
= args
->front();
6423 if (!arg
->is_type_expression())
6425 error_at(arg
->location(), "expected type");
6426 this->set_is_error();
6429 return Expression::make_allocation(arg
->type(), this->location());
6432 else if (this->code_
== BUILTIN_MAKE
)
6434 const Expression_list
* args
= this->args();
6435 if (args
== NULL
|| args
->size() < 1)
6436 this->report_error(_("not enough arguments"));
6439 Expression
* arg
= args
->front();
6440 if (!arg
->is_type_expression())
6442 error_at(arg
->location(), "expected type");
6443 this->set_is_error();
6447 Expression_list
* newargs
;
6448 if (args
->size() == 1)
6452 newargs
= new Expression_list();
6453 Expression_list::const_iterator p
= args
->begin();
6455 for (; p
!= args
->end(); ++p
)
6456 newargs
->push_back(*p
);
6458 return Expression::make_make(arg
->type(), newargs
,
6463 else if (this->is_constant())
6465 // We can only lower len and cap if there are no function calls
6466 // in the arguments. Otherwise we have to make the call.
6467 if (this->code_
== BUILTIN_LEN
|| this->code_
== BUILTIN_CAP
)
6469 Expression
* arg
= this->one_arg();
6470 if (!arg
->is_constant())
6472 Find_call_expression find_call
;
6473 Expression::traverse(&arg
, &find_call
);
6474 if (find_call
.found())
6482 if (this->integer_constant_value(true, ival
, &type
))
6484 Expression
* ret
= Expression::make_integer(&ival
, type
,
6493 if (this->float_constant_value(rval
, &type
))
6495 Expression
* ret
= Expression::make_float(&rval
, type
,
6503 if (this->complex_constant_value(rval
, imag
, &type
))
6505 Expression
* ret
= Expression::make_complex(&rval
, &imag
, type
,
6514 else if (this->code_
== BUILTIN_RECOVER
)
6516 if (function
!= NULL
)
6517 function
->func_value()->set_calls_recover();
6520 // Calling recover outside of a function always returns the
6521 // nil empty interface.
6522 Type
* eface
= Type::make_interface_type(NULL
, this->location());
6523 return Expression::make_cast(eface
,
6524 Expression::make_nil(this->location()),
6528 else if (this->code_
== BUILTIN_APPEND
)
6530 // Lower the varargs.
6531 const Expression_list
* args
= this->args();
6532 if (args
== NULL
|| args
->empty())
6534 Type
* slice_type
= args
->front()->type();
6535 if (!slice_type
->is_open_array_type())
6537 error_at(args
->front()->location(), "argument 1 must be a slice");
6538 this->set_is_error();
6541 return this->lower_varargs(gogo
, function
, slice_type
, 2);
6547 // Return the type of the real or imag functions, given the type of
6548 // the argument. We need to map complex to float, complex64 to
6549 // float32, and complex128 to float64, so it has to be done by name.
6550 // This returns NULL if it can't figure out the type.
6553 Builtin_call_expression::real_imag_type(Type
* arg_type
)
6555 if (arg_type
== NULL
|| arg_type
->is_abstract())
6557 Named_type
* nt
= arg_type
->named_type();
6560 while (nt
->real_type()->named_type() != NULL
)
6561 nt
= nt
->real_type()->named_type();
6562 if (nt
->name() == "complex")
6563 return Type::lookup_float_type("float");
6564 else if (nt
->name() == "complex64")
6565 return Type::lookup_float_type("float32");
6566 else if (nt
->name() == "complex128")
6567 return Type::lookup_float_type("float64");
6572 // Return the type of the cmplx function, given the type of one of the
6573 // argments. Like real_imag_type, we have to map by name.
6576 Builtin_call_expression::cmplx_type(Type
* arg_type
)
6578 if (arg_type
== NULL
|| arg_type
->is_abstract())
6580 Named_type
* nt
= arg_type
->named_type();
6583 while (nt
->real_type()->named_type() != NULL
)
6584 nt
= nt
->real_type()->named_type();
6585 if (nt
->name() == "float")
6586 return Type::lookup_complex_type("complex");
6587 else if (nt
->name() == "float32")
6588 return Type::lookup_complex_type("complex64");
6589 else if (nt
->name() == "float64")
6590 return Type::lookup_complex_type("complex128");
6595 // Return a single argument, or NULL if there isn't one.
6598 Builtin_call_expression::one_arg() const
6600 const Expression_list
* args
= this->args();
6601 if (args
->size() != 1)
6603 return args
->front();
6606 // Return whether this is constant: len of a string, or len or cap of
6607 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
6610 Builtin_call_expression::do_is_constant() const
6612 switch (this->code_
)
6617 Expression
* arg
= this->one_arg();
6620 Type
* arg_type
= arg
->type();
6622 if (arg_type
->points_to() != NULL
6623 && arg_type
->points_to()->array_type() != NULL
6624 && !arg_type
->points_to()->is_open_array_type())
6625 arg_type
= arg_type
->points_to();
6627 if (arg_type
->array_type() != NULL
6628 && arg_type
->array_type()->length() != NULL
)
6629 return arg_type
->array_type()->length()->is_constant();
6631 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6632 return arg
->is_constant();
6636 case BUILTIN_SIZEOF
:
6637 case BUILTIN_ALIGNOF
:
6638 return this->one_arg() != NULL
;
6640 case BUILTIN_OFFSETOF
:
6642 Expression
* arg
= this->one_arg();
6645 return arg
->field_reference_expression() != NULL
;
6650 const Expression_list
* args
= this->args();
6651 if (args
!= NULL
&& args
->size() == 2)
6652 return args
->front()->is_constant() && args
->back()->is_constant();
6659 Expression
* arg
= this->one_arg();
6660 return arg
!= NULL
&& arg
->is_constant();
6670 // Return an integer constant value if possible.
6673 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant
,
6677 if (this->code_
== BUILTIN_LEN
6678 || this->code_
== BUILTIN_CAP
)
6680 Expression
* arg
= this->one_arg();
6683 Type
* arg_type
= arg
->type();
6685 if (this->code_
== BUILTIN_LEN
&& arg_type
->is_string_type())
6688 if (arg
->string_constant_value(&sval
))
6690 mpz_set_ui(val
, sval
.length());
6691 *ptype
= Type::lookup_integer_type("int");
6696 if (arg_type
->points_to() != NULL
6697 && arg_type
->points_to()->array_type() != NULL
6698 && !arg_type
->points_to()->is_open_array_type())
6699 arg_type
= arg_type
->points_to();
6701 if (arg_type
->array_type() != NULL
6702 && arg_type
->array_type()->length() != NULL
)
6704 Expression
* e
= arg_type
->array_type()->length();
6705 if (e
->integer_constant_value(iota_is_constant
, val
, ptype
))
6707 *ptype
= Type::lookup_integer_type("int");
6712 else if (this->code_
== BUILTIN_SIZEOF
6713 || this->code_
== BUILTIN_ALIGNOF
)
6715 Expression
* arg
= this->one_arg();
6718 Type
* arg_type
= arg
->type();
6719 if (arg_type
->is_error_type())
6721 if (arg_type
->is_abstract())
6723 tree arg_type_tree
= arg_type
->get_tree(this->gogo_
);
6724 unsigned long val_long
;
6725 if (this->code_
== BUILTIN_SIZEOF
)
6727 tree type_size
= TYPE_SIZE_UNIT(arg_type_tree
);
6728 gcc_assert(TREE_CODE(type_size
) == INTEGER_CST
);
6729 if (TREE_INT_CST_HIGH(type_size
) != 0)
6731 unsigned HOST_WIDE_INT val_wide
= TREE_INT_CST_LOW(type_size
);
6732 val_long
= static_cast<unsigned long>(val_wide
);
6733 if (val_long
!= val_wide
)
6736 else if (this->code_
== BUILTIN_ALIGNOF
)
6738 if (arg
->field_reference_expression() == NULL
)
6739 val_long
= go_type_alignment(arg_type_tree
);
6742 // Calling unsafe.Alignof(s.f) returns the alignment of
6743 // the type of f when it is used as a field in a struct.
6744 val_long
= go_field_alignment(arg_type_tree
);
6749 mpz_set_ui(val
, val_long
);
6753 else if (this->code_
== BUILTIN_OFFSETOF
)
6755 Expression
* arg
= this->one_arg();
6758 Field_reference_expression
* farg
= arg
->field_reference_expression();
6761 Expression
* struct_expr
= farg
->expr();
6762 Type
* st
= struct_expr
->type();
6763 if (st
->struct_type() == NULL
)
6765 tree struct_tree
= st
->get_tree(this->gogo_
);
6766 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
6767 tree field
= TYPE_FIELDS(struct_tree
);
6768 for (unsigned int index
= farg
->field_index(); index
> 0; --index
)
6770 field
= DECL_CHAIN(field
);
6771 gcc_assert(field
!= NULL_TREE
);
6773 HOST_WIDE_INT offset_wide
= int_byte_position (field
);
6774 if (offset_wide
< 0)
6776 unsigned long offset_long
= static_cast<unsigned long>(offset_wide
);
6777 if (offset_long
!= static_cast<unsigned HOST_WIDE_INT
>(offset_wide
))
6779 mpz_set_ui(val
, offset_long
);
6785 // Return a floating point constant value if possible.
6788 Builtin_call_expression::do_float_constant_value(mpfr_t val
,
6791 if (this->code_
== BUILTIN_REAL
|| this->code_
== BUILTIN_IMAG
)
6793 Expression
* arg
= this->one_arg();
6804 if (arg
->complex_constant_value(real
, imag
, &type
))
6806 if (this->code_
== BUILTIN_REAL
)
6807 mpfr_set(val
, real
, GMP_RNDN
);
6809 mpfr_set(val
, imag
, GMP_RNDN
);
6810 *ptype
= Builtin_call_expression::real_imag_type(type
);
6822 // Return a complex constant value if possible.
6825 Builtin_call_expression::do_complex_constant_value(mpfr_t real
, mpfr_t imag
,
6828 if (this->code_
== BUILTIN_CMPLX
)
6830 const Expression_list
* args
= this->args();
6831 if (args
== NULL
|| args
->size() != 2)
6837 if (!args
->front()->float_constant_value(r
, &rtype
))
6848 if (args
->back()->float_constant_value(i
, &itype
)
6849 && Type::are_identical(rtype
, itype
, NULL
))
6851 mpfr_set(real
, r
, GMP_RNDN
);
6852 mpfr_set(imag
, i
, GMP_RNDN
);
6853 *ptype
= Builtin_call_expression::cmplx_type(rtype
);
6869 Builtin_call_expression::do_type()
6871 switch (this->code_
)
6873 case BUILTIN_INVALID
:
6880 const Expression_list
* args
= this->args();
6881 if (args
== NULL
|| args
->empty())
6882 return Type::make_error_type();
6883 return Type::make_pointer_type(args
->front()->type());
6889 case BUILTIN_ALIGNOF
:
6890 case BUILTIN_OFFSETOF
:
6891 case BUILTIN_SIZEOF
:
6892 return Type::lookup_integer_type("int");
6897 case BUILTIN_PRINTLN
:
6898 return Type::make_void_type();
6900 case BUILTIN_CLOSED
:
6901 return Type::lookup_bool_type();
6903 case BUILTIN_RECOVER
:
6904 return Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
6906 case BUILTIN_APPEND
:
6908 const Expression_list
* args
= this->args();
6909 if (args
== NULL
|| args
->empty())
6910 return Type::make_error_type();
6911 return args
->front()->type();
6917 Expression
* arg
= this->one_arg();
6919 return Type::make_error_type();
6920 Type
* t
= arg
->type();
6921 if (t
->is_abstract())
6922 t
= t
->make_non_abstract_type();
6923 t
= Builtin_call_expression::real_imag_type(t
);
6925 t
= Type::make_error_type();
6931 const Expression_list
* args
= this->args();
6932 if (args
== NULL
|| args
->size() != 2)
6933 return Type::make_error_type();
6934 Type
* t
= args
->front()->type();
6935 if (t
->is_abstract())
6937 t
= args
->back()->type();
6938 if (t
->is_abstract())
6939 t
= t
->make_non_abstract_type();
6941 t
= Builtin_call_expression::cmplx_type(t
);
6943 t
= Type::make_error_type();
6949 // Determine the type.
6952 Builtin_call_expression::do_determine_type(const Type_context
* context
)
6954 this->fn()->determine_type_no_context();
6956 const Expression_list
* args
= this->args();
6959 Type
* arg_type
= NULL
;
6960 switch (this->code_
)
6963 case BUILTIN_PRINTLN
:
6964 // Do not force a large integer constant to "int".
6970 arg_type
= Builtin_call_expression::cmplx_type(context
->type
);
6976 // For the cmplx function the type of one operand can
6977 // determine the type of the other, as in a binary expression.
6978 arg_type
= Builtin_call_expression::real_imag_type(context
->type
);
6979 if (args
!= NULL
&& args
->size() == 2)
6981 Type
* t1
= args
->front()->type();
6982 Type
* t2
= args
->front()->type();
6983 if (!t1
->is_abstract())
6985 else if (!t2
->is_abstract())
6999 for (Expression_list::const_iterator pa
= args
->begin();
7003 Type_context subcontext
;
7004 subcontext
.type
= arg_type
;
7008 // We want to print large constants, we so can't just
7009 // use the appropriate nonabstract type. Use uint64 for
7010 // an integer if we know it is nonnegative, otherwise
7011 // use int64 for a integer, otherwise use float64 for a
7012 // float or complex128 for a complex.
7013 Type
* want_type
= NULL
;
7014 Type
* atype
= (*pa
)->type();
7015 if (atype
->is_abstract())
7017 if (atype
->integer_type() != NULL
)
7022 if (this->integer_constant_value(true, val
, &dummy
)
7023 && mpz_sgn(val
) >= 0)
7024 want_type
= Type::lookup_integer_type("uint64");
7026 want_type
= Type::lookup_integer_type("int64");
7029 else if (atype
->float_type() != NULL
)
7030 want_type
= Type::lookup_float_type("float64");
7031 else if (atype
->complex_type() != NULL
)
7032 want_type
= Type::lookup_complex_type("complex128");
7033 else if (atype
->is_abstract_string_type())
7034 want_type
= Type::lookup_string_type();
7035 else if (atype
->is_abstract_boolean_type())
7036 want_type
= Type::lookup_bool_type();
7039 subcontext
.type
= want_type
;
7043 (*pa
)->determine_type(&subcontext
);
7048 // If there is exactly one argument, return true. Otherwise give an
7049 // error message and return false.
7052 Builtin_call_expression::check_one_arg()
7054 const Expression_list
* args
= this->args();
7055 if (args
== NULL
|| args
->size() < 1)
7057 this->report_error(_("not enough arguments"));
7060 else if (args
->size() > 1)
7062 this->report_error(_("too many arguments"));
7065 if (args
->front()->is_error_expression()
7066 || args
->front()->type()->is_error_type())
7068 this->set_is_error();
7074 // Check argument types for a builtin function.
7077 Builtin_call_expression::do_check_types(Gogo
*)
7079 switch (this->code_
)
7081 case BUILTIN_INVALID
:
7089 // The single argument may be either a string or an array or a
7090 // map or a channel, or a pointer to a closed array.
7091 if (this->check_one_arg())
7093 Type
* arg_type
= this->one_arg()->type();
7094 if (arg_type
->points_to() != NULL
7095 && arg_type
->points_to()->array_type() != NULL
7096 && !arg_type
->points_to()->is_open_array_type())
7097 arg_type
= arg_type
->points_to();
7098 if (this->code_
== BUILTIN_CAP
)
7100 if (!arg_type
->is_error_type()
7101 && arg_type
->array_type() == NULL
7102 && arg_type
->channel_type() == NULL
)
7103 this->report_error(_("argument must be array or slice "
7108 if (!arg_type
->is_error_type()
7109 && !arg_type
->is_string_type()
7110 && arg_type
->array_type() == NULL
7111 && arg_type
->map_type() == NULL
7112 && arg_type
->channel_type() == NULL
)
7113 this->report_error(_("argument must be string or "
7114 "array or slice or map or channel"));
7121 case BUILTIN_PRINTLN
:
7123 const Expression_list
* args
= this->args();
7126 if (this->code_
== BUILTIN_PRINT
)
7127 warning_at(this->location(), 0,
7128 "no arguments for builtin function %<%s%>",
7129 (this->code_
== BUILTIN_PRINT
7135 for (Expression_list::const_iterator p
= args
->begin();
7139 Type
* type
= (*p
)->type();
7140 if (type
->is_error_type()
7141 || type
->is_string_type()
7142 || type
->integer_type() != NULL
7143 || type
->float_type() != NULL
7144 || type
->complex_type() != NULL
7145 || type
->is_boolean_type()
7146 || type
->points_to() != NULL
7147 || type
->interface_type() != NULL
7148 || type
->channel_type() != NULL
7149 || type
->map_type() != NULL
7150 || type
->function_type() != NULL
7151 || type
->is_open_array_type())
7154 this->report_error(_("unsupported argument type to "
7155 "builtin function"));
7162 case BUILTIN_CLOSED
:
7163 if (this->check_one_arg())
7165 if (this->one_arg()->type()->channel_type() == NULL
)
7166 this->report_error(_("argument must be channel"));
7171 case BUILTIN_SIZEOF
:
7172 case BUILTIN_ALIGNOF
:
7173 this->check_one_arg();
7176 case BUILTIN_RECOVER
:
7177 if (this->args() != NULL
&& !this->args()->empty())
7178 this->report_error(_("too many arguments"));
7181 case BUILTIN_OFFSETOF
:
7182 if (this->check_one_arg())
7184 Expression
* arg
= this->one_arg();
7185 if (arg
->field_reference_expression() == NULL
)
7186 this->report_error(_("argument must be a field reference"));
7192 const Expression_list
* args
= this->args();
7193 if (args
== NULL
|| args
->size() < 2)
7195 this->report_error(_("not enough arguments"));
7198 else if (args
->size() > 2)
7200 this->report_error(_("too many arguments"));
7203 Type
* arg1_type
= args
->front()->type();
7204 Type
* arg2_type
= args
->back()->type();
7205 if (arg1_type
->is_error_type() || arg2_type
->is_error_type())
7209 if (arg1_type
->is_open_array_type())
7210 e1
= arg1_type
->array_type()->element_type();
7213 this->report_error(_("left argument must be a slice"));
7218 if (arg2_type
->is_open_array_type())
7219 e2
= arg2_type
->array_type()->element_type();
7220 else if (arg2_type
->is_string_type())
7221 e2
= Type::lookup_integer_type("uint8");
7224 this->report_error(_("right argument must be a slice or a string"));
7228 if (!Type::are_identical(e1
, e2
, NULL
))
7229 this->report_error(_("element types must be the same"));
7233 case BUILTIN_APPEND
:
7235 const Expression_list
* args
= this->args();
7236 if (args
== NULL
|| args
->empty())
7238 this->report_error(_("not enough arguments"));
7241 /* Lowering varargs should have left us with 2 arguments. */
7242 gcc_assert(args
->size() == 2);
7244 if (!Type::are_assignable(args
->front()->type(), args
->back()->type(),
7248 this->report_error(_("arguments 1 and 2 have different types"));
7251 error_at(this->location(),
7252 "arguments 1 and 2 have different types (%s)",
7254 this->set_is_error();
7262 if (this->check_one_arg())
7264 if (this->one_arg()->type()->complex_type() == NULL
)
7265 this->report_error(_("argument must have complex type"));
7271 const Expression_list
* args
= this->args();
7272 if (args
== NULL
|| args
->size() < 2)
7273 this->report_error(_("not enough arguments"));
7274 else if (args
->size() > 2)
7275 this->report_error(_("too many arguments"));
7276 else if (args
->front()->is_error_expression()
7277 || args
->front()->type()->is_error_type()
7278 || args
->back()->is_error_expression()
7279 || args
->back()->type()->is_error_type())
7280 this->set_is_error();
7281 else if (!Type::are_identical(args
->front()->type(),
7282 args
->back()->type(), NULL
))
7283 this->report_error(_("cmplx arguments must have identical types"));
7284 else if (args
->front()->type()->float_type() == NULL
)
7285 this->report_error(_("cmplx arguments must have "
7286 "floating-point type"));
7295 // Return the tree for a builtin function.
7298 Builtin_call_expression::do_get_tree(Translate_context
* context
)
7300 Gogo
* gogo
= context
->gogo();
7301 source_location location
= this->location();
7302 switch (this->code_
)
7304 case BUILTIN_INVALID
:
7312 const Expression_list
* args
= this->args();
7313 gcc_assert(args
!= NULL
&& args
->size() == 1);
7314 Expression
* arg
= *args
->begin();
7315 Type
* arg_type
= arg
->type();
7316 tree arg_tree
= arg
->get_tree(context
);
7317 if (arg_tree
== error_mark_node
)
7318 return error_mark_node
;
7320 if (arg_type
->points_to() != NULL
)
7322 arg_type
= arg_type
->points_to();
7323 gcc_assert(arg_type
->array_type() != NULL
7324 && !arg_type
->is_open_array_type());
7325 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree
)));
7326 arg_tree
= build_fold_indirect_ref(arg_tree
);
7330 if (this->code_
== BUILTIN_LEN
)
7332 if (arg_type
->is_string_type())
7333 val_tree
= String_type::length_tree(gogo
, arg_tree
);
7334 else if (arg_type
->array_type() != NULL
)
7335 val_tree
= arg_type
->array_type()->length_tree(gogo
, arg_tree
);
7336 else if (arg_type
->map_type() != NULL
)
7338 static tree map_len_fndecl
;
7339 val_tree
= Gogo::call_builtin(&map_len_fndecl
,
7344 arg_type
->get_tree(gogo
),
7347 else if (arg_type
->channel_type() != NULL
)
7349 static tree chan_len_fndecl
;
7350 val_tree
= Gogo::call_builtin(&chan_len_fndecl
,
7355 arg_type
->get_tree(gogo
),
7363 if (arg_type
->array_type() != NULL
)
7364 val_tree
= arg_type
->array_type()->capacity_tree(gogo
, arg_tree
);
7365 else if (arg_type
->channel_type() != NULL
)
7367 static tree chan_cap_fndecl
;
7368 val_tree
= Gogo::call_builtin(&chan_cap_fndecl
,
7373 arg_type
->get_tree(gogo
),
7380 tree type_tree
= Type::lookup_integer_type("int")->get_tree(gogo
);
7381 if (type_tree
== TREE_TYPE(val_tree
))
7384 return fold(convert_to_integer(type_tree
, val_tree
));
7388 case BUILTIN_PRINTLN
:
7390 const bool is_ln
= this->code_
== BUILTIN_PRINTLN
;
7391 tree stmt_list
= NULL_TREE
;
7393 const Expression_list
* call_args
= this->args();
7394 if (call_args
!= NULL
)
7396 for (Expression_list::const_iterator p
= call_args
->begin();
7397 p
!= call_args
->end();
7400 if (is_ln
&& p
!= call_args
->begin())
7402 static tree print_space_fndecl
;
7403 tree call
= Gogo::call_builtin(&print_space_fndecl
,
7408 append_to_statement_list(call
, &stmt_list
);
7411 Type
* type
= (*p
)->type();
7413 tree arg
= (*p
)->get_tree(context
);
7414 if (arg
== error_mark_node
)
7415 return error_mark_node
;
7419 if (type
->is_string_type())
7421 static tree print_string_fndecl
;
7422 pfndecl
= &print_string_fndecl
;
7423 fnname
= "__go_print_string";
7425 else if (type
->integer_type() != NULL
7426 && type
->integer_type()->is_unsigned())
7428 static tree print_uint64_fndecl
;
7429 pfndecl
= &print_uint64_fndecl
;
7430 fnname
= "__go_print_uint64";
7431 Type
* itype
= Type::lookup_integer_type("uint64");
7432 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7435 else if (type
->integer_type() != NULL
)
7437 static tree print_int64_fndecl
;
7438 pfndecl
= &print_int64_fndecl
;
7439 fnname
= "__go_print_int64";
7440 Type
* itype
= Type::lookup_integer_type("int64");
7441 arg
= fold_convert_loc(location
, itype
->get_tree(gogo
),
7444 else if (type
->float_type() != NULL
)
7446 static tree print_double_fndecl
;
7447 pfndecl
= &print_double_fndecl
;
7448 fnname
= "__go_print_double";
7449 arg
= fold_convert_loc(location
, double_type_node
, arg
);
7451 else if (type
->complex_type() != NULL
)
7453 static tree print_complex_fndecl
;
7454 pfndecl
= &print_complex_fndecl
;
7455 fnname
= "__go_print_complex";
7456 arg
= fold_convert_loc(location
, complex_double_type_node
,
7459 else if (type
->is_boolean_type())
7461 static tree print_bool_fndecl
;
7462 pfndecl
= &print_bool_fndecl
;
7463 fnname
= "__go_print_bool";
7465 else if (type
->points_to() != NULL
7466 || type
->channel_type() != NULL
7467 || type
->map_type() != NULL
7468 || type
->function_type() != NULL
)
7470 static tree print_pointer_fndecl
;
7471 pfndecl
= &print_pointer_fndecl
;
7472 fnname
= "__go_print_pointer";
7473 arg
= fold_convert_loc(location
, ptr_type_node
, arg
);
7475 else if (type
->interface_type() != NULL
)
7477 if (type
->interface_type()->is_empty())
7479 static tree print_empty_interface_fndecl
;
7480 pfndecl
= &print_empty_interface_fndecl
;
7481 fnname
= "__go_print_empty_interface";
7485 static tree print_interface_fndecl
;
7486 pfndecl
= &print_interface_fndecl
;
7487 fnname
= "__go_print_interface";
7490 else if (type
->is_open_array_type())
7492 static tree print_slice_fndecl
;
7493 pfndecl
= &print_slice_fndecl
;
7494 fnname
= "__go_print_slice";
7499 tree call
= Gogo::call_builtin(pfndecl
,
7506 append_to_statement_list(call
, &stmt_list
);
7512 static tree print_nl_fndecl
;
7513 tree call
= Gogo::call_builtin(&print_nl_fndecl
,
7518 append_to_statement_list(call
, &stmt_list
);
7526 const Expression_list
* args
= this->args();
7527 gcc_assert(args
!= NULL
&& args
->size() == 1);
7528 Expression
* arg
= args
->front();
7529 tree arg_tree
= arg
->get_tree(context
);
7530 if (arg_tree
== error_mark_node
)
7531 return error_mark_node
;
7532 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7533 arg_tree
= Expression::convert_for_assignment(context
, empty
,
7535 arg_tree
, location
);
7536 static tree panic_fndecl
;
7537 tree call
= Gogo::call_builtin(&panic_fndecl
,
7542 TREE_TYPE(arg_tree
),
7544 // This function will throw an exception.
7545 TREE_NOTHROW(panic_fndecl
) = 0;
7546 // This function will not return.
7547 TREE_THIS_VOLATILE(panic_fndecl
) = 1;
7551 case BUILTIN_RECOVER
:
7553 // The argument is set when building recover thunks. It's a
7554 // boolean value which is true if we can recover a value now.
7555 const Expression_list
* args
= this->args();
7556 gcc_assert(args
!= NULL
&& args
->size() == 1);
7557 Expression
* arg
= args
->front();
7558 tree arg_tree
= arg
->get_tree(context
);
7559 if (arg_tree
== error_mark_node
)
7560 return error_mark_node
;
7562 Type
*empty
= Type::make_interface_type(NULL
, BUILTINS_LOCATION
);
7563 tree empty_tree
= empty
->get_tree(context
->gogo());
7565 Type
* nil_type
= Type::make_nil_type();
7566 Expression
* nil
= Expression::make_nil(location
);
7567 tree nil_tree
= nil
->get_tree(context
);
7568 tree empty_nil_tree
= Expression::convert_for_assignment(context
,
7574 // We need to handle a deferred call to recover specially,
7575 // because it changes whether it can recover a panic or not.
7576 // See test7 in test/recover1.go.
7578 if (this->is_deferred())
7580 static tree deferred_recover_fndecl
;
7581 call
= Gogo::call_builtin(&deferred_recover_fndecl
,
7583 "__go_deferred_recover",
7589 static tree recover_fndecl
;
7590 call
= Gogo::call_builtin(&recover_fndecl
,
7596 return fold_build3_loc(location
, COND_EXPR
, empty_tree
, arg_tree
,
7597 call
, empty_nil_tree
);
7601 case BUILTIN_CLOSED
:
7603 const Expression_list
* args
= this->args();
7604 gcc_assert(args
!= NULL
&& args
->size() == 1);
7605 Expression
* arg
= args
->front();
7606 tree arg_tree
= arg
->get_tree(context
);
7607 if (arg_tree
== error_mark_node
)
7608 return error_mark_node
;
7609 if (this->code_
== BUILTIN_CLOSE
)
7611 static tree close_fndecl
;
7612 return Gogo::call_builtin(&close_fndecl
,
7614 "__go_builtin_close",
7617 TREE_TYPE(arg_tree
),
7622 static tree closed_fndecl
;
7623 return Gogo::call_builtin(&closed_fndecl
,
7625 "__go_builtin_closed",
7628 TREE_TYPE(arg_tree
),
7633 case BUILTIN_SIZEOF
:
7634 case BUILTIN_OFFSETOF
:
7635 case BUILTIN_ALIGNOF
:
7640 bool b
= this->integer_constant_value(true, val
, &dummy
);
7642 tree type
= Type::lookup_integer_type("int")->get_tree(gogo
);
7643 tree ret
= Expression::integer_constant_tree(val
, type
);
7650 const Expression_list
* args
= this->args();
7651 gcc_assert(args
!= NULL
&& args
->size() == 2);
7652 Expression
* arg1
= args
->front();
7653 Expression
* arg2
= args
->back();
7655 tree arg1_tree
= arg1
->get_tree(context
);
7656 tree arg2_tree
= arg2
->get_tree(context
);
7657 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7658 return error_mark_node
;
7660 Type
* arg1_type
= arg1
->type();
7661 Array_type
* at
= arg1_type
->array_type();
7662 arg1_tree
= save_expr(arg1_tree
);
7663 tree arg1_val
= at
->value_pointer_tree(gogo
, arg1_tree
);
7664 tree arg1_len
= at
->length_tree(gogo
, arg1_tree
);
7666 Type
* arg2_type
= arg2
->type();
7669 if (arg2_type
->is_open_array_type())
7671 at
= arg2_type
->array_type();
7672 arg2_tree
= save_expr(arg2_tree
);
7673 arg2_val
= at
->value_pointer_tree(gogo
, arg2_tree
);
7674 arg2_len
= at
->length_tree(gogo
, arg2_tree
);
7678 arg2_tree
= save_expr(arg2_tree
);
7679 arg2_val
= String_type::bytes_tree(gogo
, arg2_tree
);
7680 arg2_len
= String_type::length_tree(gogo
, arg2_tree
);
7683 arg1_len
= save_expr(arg1_len
);
7684 arg2_len
= save_expr(arg2_len
);
7685 tree len
= fold_build3_loc(location
, COND_EXPR
, TREE_TYPE(arg1_len
),
7686 fold_build2_loc(location
, LT_EXPR
,
7688 arg1_len
, arg2_len
),
7689 arg1_len
, arg2_len
);
7690 len
= save_expr(len
);
7692 Type
* element_type
= at
->element_type();
7693 tree element_type_tree
= element_type
->get_tree(gogo
);
7694 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
7695 tree bytecount
= fold_convert_loc(location
, TREE_TYPE(element_size
),
7697 bytecount
= fold_build2_loc(location
, MULT_EXPR
,
7698 TREE_TYPE(element_size
),
7699 bytecount
, element_size
);
7700 bytecount
= fold_convert_loc(location
, size_type_node
, bytecount
);
7702 tree call
= build_call_expr_loc(location
,
7703 built_in_decls
[BUILT_IN_MEMMOVE
],
7704 3, arg1_val
, arg2_val
, bytecount
);
7706 return fold_build2_loc(location
, COMPOUND_EXPR
, TREE_TYPE(len
),
7710 case BUILTIN_APPEND
:
7712 const Expression_list
* args
= this->args();
7713 gcc_assert(args
!= NULL
&& args
->size() == 2);
7714 Expression
* arg1
= args
->front();
7715 Expression
* arg2
= args
->back();
7717 tree arg1_tree
= arg1
->get_tree(context
);
7718 tree arg2_tree
= arg2
->get_tree(context
);
7719 if (arg1_tree
== error_mark_node
|| arg2_tree
== error_mark_node
)
7720 return error_mark_node
;
7722 tree descriptor_tree
= arg1
->type()->type_descriptor_pointer(gogo
);
7724 // We rebuild the decl each time since the slice types may
7726 tree append_fndecl
= NULL_TREE
;
7727 return Gogo::call_builtin(&append_fndecl
,
7731 TREE_TYPE(arg1_tree
),
7732 TREE_TYPE(descriptor_tree
),
7734 TREE_TYPE(arg1_tree
),
7736 TREE_TYPE(arg2_tree
),
7743 const Expression_list
* args
= this->args();
7744 gcc_assert(args
!= NULL
&& args
->size() == 1);
7745 Expression
* arg
= args
->front();
7746 tree arg_tree
= arg
->get_tree(context
);
7747 if (arg_tree
== error_mark_node
)
7748 return error_mark_node
;
7749 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree
)));
7750 if (this->code_
== BUILTIN_REAL
)
7751 return fold_build1_loc(location
, REALPART_EXPR
,
7752 TREE_TYPE(TREE_TYPE(arg_tree
)),
7755 return fold_build1_loc(location
, IMAGPART_EXPR
,
7756 TREE_TYPE(TREE_TYPE(arg_tree
)),
7762 const Expression_list
* args
= this->args();
7763 gcc_assert(args
!= NULL
&& args
->size() == 2);
7764 tree r
= args
->front()->get_tree(context
);
7765 tree i
= args
->back()->get_tree(context
);
7766 if (r
== error_mark_node
|| i
== error_mark_node
)
7767 return error_mark_node
;
7768 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r
))
7769 == TYPE_MAIN_VARIANT(TREE_TYPE(i
)));
7770 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r
)));
7771 return fold_build2_loc(location
, COMPLEX_EXPR
,
7772 build_complex_type(TREE_TYPE(r
)),
7781 // We have to support exporting a builtin call expression, because
7782 // code can set a constant to the result of a builtin expression.
7785 Builtin_call_expression::do_export(Export
* exp
) const
7792 if (this->integer_constant_value(true, val
, &dummy
))
7794 Integer_expression::export_integer(exp
, val
);
7803 if (this->float_constant_value(fval
, &dummy
))
7805 Float_expression::export_float(exp
, fval
);
7817 if (this->complex_constant_value(real
, imag
, &dummy
))
7819 Complex_expression::export_complex(exp
, real
, imag
);
7828 error_at(this->location(), "value is not constant");
7832 // A trailing space lets us reliably identify the end of the number.
7833 exp
->write_c_string(" ");
7836 // Class Call_expression.
7841 Call_expression::do_traverse(Traverse
* traverse
)
7843 if (Expression::traverse(&this->fn_
, traverse
) == TRAVERSE_EXIT
)
7844 return TRAVERSE_EXIT
;
7845 if (this->args_
!= NULL
)
7847 if (this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
7848 return TRAVERSE_EXIT
;
7850 return TRAVERSE_CONTINUE
;
7853 // Lower a call statement.
7856 Call_expression::do_lower(Gogo
* gogo
, Named_object
* function
, int)
7858 // A type case can look like a function call.
7859 if (this->fn_
->is_type_expression()
7860 && this->args_
!= NULL
7861 && this->args_
->size() == 1)
7862 return Expression::make_cast(this->fn_
->type(), this->args_
->front(),
7865 // Recognize a call to a builtin function.
7866 Func_expression
* fne
= this->fn_
->func_expression();
7868 && fne
->named_object()->is_function_declaration()
7869 && fne
->named_object()->func_declaration_value()->type()->is_builtin())
7870 return new Builtin_call_expression(gogo
, this->fn_
, this->args_
,
7871 this->is_varargs_
, this->location());
7873 // Handle an argument which is a call to a function which returns
7874 // multiple results.
7875 if (this->args_
!= NULL
7876 && this->args_
->size() == 1
7877 && this->args_
->front()->call_expression() != NULL
7878 && this->fn_
->type()->function_type() != NULL
)
7880 Function_type
* fntype
= this->fn_
->type()->function_type();
7881 size_t rc
= this->args_
->front()->call_expression()->result_count();
7883 && fntype
->parameters() != NULL
7884 && (fntype
->parameters()->size() == rc
7885 || (fntype
->is_varargs()
7886 && fntype
->parameters()->size() - 1 <= rc
)))
7888 Call_expression
* call
= this->args_
->front()->call_expression();
7889 Expression_list
* args
= new Expression_list
;
7890 for (size_t i
= 0; i
< rc
; ++i
)
7891 args
->push_back(Expression::make_call_result(call
, i
));
7892 // We can't return a new call expression here, because this
7893 // one may be referenced by Call_result expressions. FIXME.
7899 // Handle a call to a varargs function by packaging up the extra
7901 if (this->fn_
->type()->function_type() != NULL
7902 && this->fn_
->type()->function_type()->is_varargs())
7904 Function_type
* fntype
= this->fn_
->type()->function_type();
7905 const Typed_identifier_list
* parameters
= fntype
->parameters();
7906 gcc_assert(parameters
!= NULL
&& !parameters
->empty());
7907 Type
* varargs_type
= parameters
->back().type();
7908 return this->lower_varargs(gogo
, function
, varargs_type
,
7909 parameters
->size());
7915 // Lower a call to a varargs function. FUNCTION is the function in
7916 // which the call occurs--it's not the function we are calling.
7917 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
7918 // PARAM_COUNT is the number of parameters of the function we are
7919 // calling; the last of these parameters will be the varargs
7923 Call_expression::lower_varargs(Gogo
* gogo
, Named_object
* function
,
7924 Type
* varargs_type
, size_t param_count
)
7926 if (this->varargs_are_lowered_
)
7929 source_location loc
= this->location();
7931 gcc_assert(param_count
> 0);
7932 gcc_assert(varargs_type
->is_open_array_type());
7934 size_t arg_count
= this->args_
== NULL
? 0 : this->args_
->size();
7935 if (arg_count
< param_count
- 1)
7937 // Not enough arguments; will be caught in check_types.
7941 Expression_list
* old_args
= this->args_
;
7942 Expression_list
* new_args
= new Expression_list();
7943 bool push_empty_arg
= false;
7944 if (old_args
== NULL
|| old_args
->empty())
7946 gcc_assert(param_count
== 1);
7947 push_empty_arg
= true;
7951 Expression_list::const_iterator pa
;
7953 for (pa
= old_args
->begin(); pa
!= old_args
->end(); ++pa
, ++i
)
7955 if (static_cast<size_t>(i
) == param_count
)
7957 new_args
->push_back(*pa
);
7960 // We have reached the varargs parameter.
7962 bool issued_error
= false;
7963 if (pa
== old_args
->end())
7964 push_empty_arg
= true;
7965 else if (pa
+ 1 == old_args
->end() && this->is_varargs_
)
7966 new_args
->push_back(*pa
);
7967 else if (this->is_varargs_
)
7969 this->report_error(_("too many arguments"));
7972 else if (pa
+ 1 == old_args
->end()
7973 && this->is_compatible_varargs_argument(function
, *pa
,
7976 new_args
->push_back(*pa
);
7979 Type
* element_type
= varargs_type
->array_type()->element_type();
7980 Expression_list
* vals
= new Expression_list
;
7981 for (; pa
!= old_args
->end(); ++pa
, ++i
)
7983 // Check types here so that we get a better message.
7984 Type
* patype
= (*pa
)->type();
7985 source_location paloc
= (*pa
)->location();
7986 if (!this->check_argument_type(i
, element_type
, patype
,
7987 paloc
, issued_error
))
7989 vals
->push_back(*pa
);
7992 Expression::make_slice_composite_literal(varargs_type
, vals
, loc
);
7993 new_args
->push_back(val
);
7998 new_args
->push_back(Expression::make_nil(loc
));
8000 // We can't return a new call expression here, because this one may
8001 // be referenced by Call_result expressions. FIXME.
8002 if (old_args
!= NULL
)
8004 this->args_
= new_args
;
8005 this->varargs_are_lowered_
= true;
8007 // Lower all the new subexpressions.
8008 Expression
* ret
= this;
8009 gogo
->lower_expression(function
, &ret
);
8010 gcc_assert(ret
== this);
8014 // Return true if ARG is a varargs argment which should be passed to
8015 // the varargs parameter of type PARAM_TYPE without wrapping. ARG
8016 // will be the last argument passed in the call, and PARAM_TYPE will
8017 // be the type of the last parameter of the varargs function being
8021 Call_expression::is_compatible_varargs_argument(Named_object
* function
,
8026 *issued_error
= false;
8028 Type
* var_type
= NULL
;
8030 // The simple case is passing the varargs parameter of the caller.
8031 Var_expression
* ve
= arg
->var_expression();
8032 if (ve
!= NULL
&& ve
->named_object()->is_variable())
8034 Variable
* var
= ve
->named_object()->var_value();
8035 if (var
->is_varargs_parameter())
8036 var_type
= var
->type();
8039 // The complex case is passing the varargs parameter of some
8040 // enclosing function. This will look like passing down *c.f where
8041 // c is the closure variable and f is a field in the closure.
8042 if (function
!= NULL
8043 && function
->func_value()->needs_closure()
8044 && arg
->classification() == EXPRESSION_UNARY
)
8046 Unary_expression
* ue
= static_cast<Unary_expression
*>(arg
);
8047 if (ue
->op() == OPERATOR_MULT
)
8049 Field_reference_expression
* fre
=
8050 ue
->operand()->deref()->field_reference_expression();
8053 Var_expression
* ve
= fre
->expr()->deref()->var_expression();
8056 Named_object
* no
= ve
->named_object();
8057 Function
* f
= function
->func_value();
8058 if (no
== f
->closure_var())
8060 // At this point we know that this indeed a
8061 // reference to some enclosing variable. Now we
8062 // need to figure out whether that variable is a
8063 // varargs parameter.
8064 Named_object
* enclosing
=
8065 f
->enclosing_var(fre
->field_index());
8066 Variable
* var
= enclosing
->var_value();
8067 if (var
->is_varargs_parameter())
8068 var_type
= var
->type();
8075 if (var_type
== NULL
)
8078 // We only match if the parameter is the same, with an identical
8080 Array_type
* var_at
= var_type
->array_type();
8081 gcc_assert(var_at
!= NULL
);
8082 Array_type
* param_at
= param_type
->array_type();
8083 if (param_at
!= NULL
8084 && Type::are_identical(var_at
->element_type(),
8085 param_at
->element_type(), NULL
))
8087 error_at(arg
->location(), "... mismatch: passing ...T as ...");
8088 *issued_error
= true;
8092 // Get the function type. Returns NULL if we don't know the type. If
8093 // this returns NULL, and if_ERROR is true, issues an error.
8096 Call_expression::get_function_type() const
8098 return this->fn_
->type()->function_type();
8101 // Return the number of values which this call will return.
8104 Call_expression::result_count() const
8106 const Function_type
* fntype
= this->get_function_type();
8109 if (fntype
->results() == NULL
)
8111 return fntype
->results()->size();
8114 // Return whether this is a call to the predeclared function recover.
8117 Call_expression::is_recover_call() const
8119 return this->do_is_recover_call();
8122 // Set the argument to the recover function.
8125 Call_expression::set_recover_arg(Expression
* arg
)
8127 this->do_set_recover_arg(arg
);
8130 // Virtual functions also implemented by Builtin_call_expression.
8133 Call_expression::do_is_recover_call() const
8139 Call_expression::do_set_recover_arg(Expression
*)
8147 Call_expression::do_type()
8149 if (this->type_
!= NULL
)
8153 Function_type
* fntype
= this->get_function_type();
8155 return Type::make_error_type();
8157 const Typed_identifier_list
* results
= fntype
->results();
8158 if (results
== NULL
)
8159 ret
= Type::make_void_type();
8160 else if (results
->size() == 1)
8161 ret
= results
->begin()->type();
8163 ret
= Type::make_call_multiple_result_type(this);
8170 // Determine types for a call expression. We can use the function
8171 // parameter types to set the types of the arguments.
8174 Call_expression::do_determine_type(const Type_context
*)
8176 this->fn_
->determine_type_no_context();
8177 Function_type
* fntype
= this->get_function_type();
8178 const Typed_identifier_list
* parameters
= NULL
;
8180 parameters
= fntype
->parameters();
8181 if (this->args_
!= NULL
)
8183 Typed_identifier_list::const_iterator pt
;
8184 if (parameters
!= NULL
)
8185 pt
= parameters
->begin();
8186 for (Expression_list::const_iterator pa
= this->args_
->begin();
8187 pa
!= this->args_
->end();
8190 if (parameters
!= NULL
&& pt
!= parameters
->end())
8192 Type_context
subcontext(pt
->type(), false);
8193 (*pa
)->determine_type(&subcontext
);
8197 (*pa
)->determine_type_no_context();
8202 // Check types for parameter I.
8205 Call_expression::check_argument_type(int i
, const Type
* parameter_type
,
8206 const Type
* argument_type
,
8207 source_location argument_location
,
8211 if (!Type::are_assignable(parameter_type
, argument_type
, &reason
))
8216 error_at(argument_location
, "argument %d has incompatible type", i
);
8218 error_at(argument_location
,
8219 "argument %d has incompatible type (%s)",
8222 this->set_is_error();
8231 Call_expression::do_check_types(Gogo
*)
8233 Function_type
* fntype
= this->get_function_type();
8236 if (!this->fn_
->type()->is_error_type())
8237 this->report_error(_("expected function"));
8241 if (fntype
->is_method())
8243 // We don't support pointers to methods, so the function has to
8244 // be a bound method expression.
8245 Bound_method_expression
* bme
= this->fn_
->bound_method_expression();
8248 this->report_error(_("method call without object"));
8251 Type
* first_arg_type
= bme
->first_argument()->type();
8252 if (first_arg_type
->points_to() == NULL
)
8254 // When passing a value, we need to check that we are
8255 // permitted to copy it.
8257 if (!Type::are_assignable(fntype
->receiver()->type(),
8258 first_arg_type
, &reason
))
8261 this->report_error(_("incompatible type for receiver"));
8264 error_at(this->location(),
8265 "incompatible type for receiver (%s)",
8267 this->set_is_error();
8273 // Note that varargs was handled by the lower_varargs() method, so
8274 // we don't have to worry about it here.
8276 const Typed_identifier_list
* parameters
= fntype
->parameters();
8277 if (this->args_
== NULL
)
8279 if (parameters
!= NULL
&& !parameters
->empty())
8280 this->report_error(_("not enough arguments"));
8282 else if (parameters
== NULL
)
8283 this->report_error(_("too many arguments"));
8287 Typed_identifier_list::const_iterator pt
= parameters
->begin();
8288 for (Expression_list::const_iterator pa
= this->args_
->begin();
8289 pa
!= this->args_
->end();
8292 if (pt
== parameters
->end())
8294 this->report_error(_("too many arguments"));
8297 this->check_argument_type(i
+ 1, pt
->type(), (*pa
)->type(),
8298 (*pa
)->location(), false);
8300 if (pt
!= parameters
->end())
8301 this->report_error(_("not enough arguments"));
8305 // Return whether we have to use a temporary variable to ensure that
8306 // we evaluate this call expression in order. If the call returns no
8307 // results then it will inevitably be executed last. If the call
8308 // returns more than one result then it will be used with Call_result
8309 // expressions. So we only have to use a temporary variable if the
8310 // call returns exactly one result.
8313 Call_expression::do_must_eval_in_order() const
8315 return this->result_count() == 1;
8318 // Get the function and the first argument to use when calling a bound
8322 Call_expression::bound_method_function(Translate_context
* context
,
8323 Bound_method_expression
* bound_method
,
8324 tree
* first_arg_ptr
)
8326 Expression
* first_argument
= bound_method
->first_argument();
8327 tree first_arg
= first_argument
->get_tree(context
);
8328 if (first_arg
== error_mark_node
)
8329 return error_mark_node
;
8331 // We always pass a pointer to the first argument when calling a
8333 if (first_argument
->type()->points_to() == NULL
)
8335 tree pointer_to_arg_type
= build_pointer_type(TREE_TYPE(first_arg
));
8336 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg
))
8337 || DECL_P(first_arg
)
8338 || TREE_CODE(first_arg
) == INDIRECT_REF
8339 || TREE_CODE(first_arg
) == COMPONENT_REF
)
8341 first_arg
= build_fold_addr_expr(first_arg
);
8342 if (DECL_P(first_arg
))
8343 TREE_ADDRESSABLE(first_arg
) = 1;
8347 tree tmp
= create_tmp_var(TREE_TYPE(first_arg
),
8348 get_name(first_arg
));
8349 DECL_IGNORED_P(tmp
) = 0;
8350 DECL_INITIAL(tmp
) = first_arg
;
8351 first_arg
= build2(COMPOUND_EXPR
, pointer_to_arg_type
,
8352 build1(DECL_EXPR
, void_type_node
, tmp
),
8353 build_fold_addr_expr(tmp
));
8354 TREE_ADDRESSABLE(tmp
) = 1;
8356 if (first_arg
== error_mark_node
)
8357 return error_mark_node
;
8360 Type
* fatype
= bound_method
->first_argument_type();
8363 if (fatype
->points_to() == NULL
)
8364 fatype
= Type::make_pointer_type(fatype
);
8365 first_arg
= fold_convert(fatype
->get_tree(context
->gogo()), first_arg
);
8366 if (first_arg
== error_mark_node
8367 || TREE_TYPE(first_arg
) == error_mark_node
)
8368 return error_mark_node
;
8371 *first_arg_ptr
= first_arg
;
8373 return bound_method
->method()->get_tree(context
);
8376 // Get the function and the first argument to use when calling an
8377 // interface method.
8380 Call_expression::interface_method_function(
8381 Translate_context
* context
,
8382 Interface_field_reference_expression
* interface_method
,
8383 tree
* first_arg_ptr
)
8385 tree expr
= interface_method
->expr()->get_tree(context
);
8386 if (expr
== error_mark_node
)
8387 return error_mark_node
;
8388 expr
= save_expr(expr
);
8389 tree first_arg
= interface_method
->get_underlying_object_tree(context
, expr
);
8390 if (first_arg
== error_mark_node
)
8391 return error_mark_node
;
8392 *first_arg_ptr
= first_arg
;
8393 return interface_method
->get_function_tree(context
, expr
);
8396 // Build the call expression.
8399 Call_expression::do_get_tree(Translate_context
* context
)
8401 if (this->tree_
!= NULL_TREE
)
8404 Function_type
* fntype
= this->get_function_type();
8406 return error_mark_node
;
8408 if (this->fn_
->is_error_expression())
8409 return error_mark_node
;
8411 Gogo
* gogo
= context
->gogo();
8412 source_location location
= this->location();
8414 Func_expression
* func
= this->fn_
->func_expression();
8415 Bound_method_expression
* bound_method
= this->fn_
->bound_method_expression();
8416 Interface_field_reference_expression
* interface_method
=
8417 this->fn_
->interface_field_reference_expression();
8418 const bool has_closure
= func
!= NULL
&& func
->closure() != NULL
;
8419 const bool is_method
= bound_method
!= NULL
|| interface_method
!= NULL
;
8420 gcc_assert(!fntype
->is_method() || is_method
);
8424 if (this->args_
== NULL
|| this->args_
->empty())
8426 nargs
= is_method
? 1 : 0;
8427 args
= nargs
== 0 ? NULL
: new tree
[nargs
];
8431 const Typed_identifier_list
* params
= fntype
->parameters();
8432 gcc_assert(params
!= NULL
);
8434 nargs
= this->args_
->size();
8435 int i
= is_method
? 1 : 0;
8437 args
= new tree
[nargs
];
8439 Typed_identifier_list::const_iterator pp
= params
->begin();
8440 Expression_list::const_iterator pe
;
8441 for (pe
= this->args_
->begin();
8442 pe
!= this->args_
->end();
8445 tree arg_val
= (*pe
)->get_tree(context
);
8446 args
[i
] = Expression::convert_for_assignment(context
,
8451 if (args
[i
] == error_mark_node
)
8452 return error_mark_node
;
8454 gcc_assert(pp
== params
->end());
8455 gcc_assert(i
== nargs
);
8458 tree rettype
= TREE_TYPE(TREE_TYPE(fntype
->get_tree(gogo
)));
8459 if (rettype
== error_mark_node
)
8460 return error_mark_node
;
8464 fn
= func
->get_tree_without_closure(gogo
);
8465 else if (!is_method
)
8466 fn
= this->fn_
->get_tree(context
);
8467 else if (bound_method
!= NULL
)
8468 fn
= this->bound_method_function(context
, bound_method
, &args
[0]);
8469 else if (interface_method
!= NULL
)
8470 fn
= this->interface_method_function(context
, interface_method
, &args
[0]);
8474 if (fn
== error_mark_node
|| TREE_TYPE(fn
) == error_mark_node
)
8475 return error_mark_node
;
8477 // This is to support builtin math functions when using 80387 math.
8479 if (TREE_CODE(fndecl
) == ADDR_EXPR
)
8480 fndecl
= TREE_OPERAND(fndecl
, 0);
8481 tree excess_type
= NULL_TREE
;
8483 && DECL_IS_BUILTIN(fndecl
)
8484 && DECL_BUILT_IN_CLASS(fndecl
) == BUILT_IN_NORMAL
8486 && ((SCALAR_FLOAT_TYPE_P(rettype
)
8487 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args
[0])))
8488 || (COMPLEX_FLOAT_TYPE_P(rettype
)
8489 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args
[0])))))
8491 excess_type
= excess_precision_type(TREE_TYPE(args
[0]));
8492 if (excess_type
!= NULL_TREE
)
8494 tree excess_fndecl
= mathfn_built_in(excess_type
,
8495 DECL_FUNCTION_CODE(fndecl
));
8496 if (excess_fndecl
== NULL_TREE
)
8497 excess_type
= NULL_TREE
;
8500 fn
= build_fold_addr_expr_loc(location
, excess_fndecl
);
8501 for (int i
= 0; i
< nargs
; ++i
)
8502 args
[i
] = ::convert(excess_type
, args
[i
]);
8507 tree ret
= build_call_array(excess_type
!= NULL_TREE
? excess_type
: rettype
,
8511 SET_EXPR_LOCATION(ret
, location
);
8515 tree closure_tree
= func
->closure()->get_tree(context
);
8516 if (closure_tree
!= error_mark_node
)
8517 CALL_EXPR_STATIC_CHAIN(ret
) = closure_tree
;
8520 // If this is a recursive function type which returns itself, as in
8522 // we have used ptr_type_node for the return type. Add a cast here
8523 // to the correct type.
8524 if (TREE_TYPE(ret
) == ptr_type_node
)
8526 tree t
= this->type()->get_tree(gogo
);
8527 ret
= fold_convert_loc(location
, t
, ret
);
8530 if (excess_type
!= NULL_TREE
)
8532 // Calling convert here can undo our excess precision change.
8533 // That may or may not be a bug in convert_to_real.
8534 ret
= build1(NOP_EXPR
, rettype
, ret
);
8537 // If there is more than one result, we will refer to the call
8539 if (fntype
->results() != NULL
&& fntype
->results()->size() > 1)
8540 ret
= save_expr(ret
);
8547 // Make a call expression.
8550 Expression::make_call(Expression
* fn
, Expression_list
* args
, bool is_varargs
,
8551 source_location location
)
8553 return new Call_expression(fn
, args
, is_varargs
, location
);
8556 // A single result from a call which returns multiple results.
8558 class Call_result_expression
: public Expression
8561 Call_result_expression(Call_expression
* call
, unsigned int index
)
8562 : Expression(EXPRESSION_CALL_RESULT
, call
->location()),
8563 call_(call
), index_(index
)
8568 do_traverse(Traverse
*);
8574 do_determine_type(const Type_context
*);
8577 do_check_types(Gogo
*);
8582 return new Call_result_expression(this->call_
->call_expression(),
8587 do_must_eval_in_order() const
8591 do_get_tree(Translate_context
*);
8594 // The underlying call expression.
8596 // Which result we want.
8597 unsigned int index_
;
8600 // Traverse a call result.
8603 Call_result_expression::do_traverse(Traverse
* traverse
)
8605 if (traverse
->remember_expression(this->call_
))
8607 // We have already traversed the call expression.
8608 return TRAVERSE_CONTINUE
;
8610 return Expression::traverse(&this->call_
, traverse
);
8616 Call_result_expression::do_type()
8618 // THIS->CALL_ can be replaced with a temporary reference due to
8619 // Call_expression::do_must_eval_in_order when there is an error.
8620 Call_expression
* ce
= this->call_
->call_expression();
8622 return Type::make_error_type();
8623 Function_type
* fntype
= ce
->get_function_type();
8625 return Type::make_error_type();
8626 const Typed_identifier_list
* results
= fntype
->results();
8627 Typed_identifier_list::const_iterator pr
= results
->begin();
8628 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8630 if (pr
== results
->end())
8631 return Type::make_error_type();
8634 if (pr
== results
->end())
8635 return Type::make_error_type();
8639 // Check the type. This is where we give an error if we're trying to
8640 // extract too many values from a call.
8643 Call_result_expression::do_check_types(Gogo
*)
8646 Call_expression
* ce
= this->call_
->call_expression();
8648 ok
= this->index_
< ce
->result_count();
8651 // This can happen when the call returns a single value but we
8652 // are asking for the second result.
8653 if (this->call_
->is_error_expression())
8658 error_at(this->location(),
8659 "number of results does not match number of values");
8662 // Determine the type. We have nothing to do here, but the 0 result
8663 // needs to pass down to the caller.
8666 Call_result_expression::do_determine_type(const Type_context
*)
8668 if (this->index_
== 0)
8669 this->call_
->determine_type_no_context();
8675 Call_result_expression::do_get_tree(Translate_context
* context
)
8677 tree call_tree
= this->call_
->get_tree(context
);
8678 if (call_tree
== error_mark_node
)
8679 return error_mark_node
;
8680 gcc_assert(TREE_CODE(TREE_TYPE(call_tree
)) == RECORD_TYPE
);
8681 tree field
= TYPE_FIELDS(TREE_TYPE(call_tree
));
8682 for (unsigned int i
= 0; i
< this->index_
; ++i
)
8684 gcc_assert(field
!= NULL_TREE
);
8685 field
= DECL_CHAIN(field
);
8687 gcc_assert(field
!= NULL_TREE
);
8688 return build3(COMPONENT_REF
, TREE_TYPE(field
), call_tree
, field
, NULL_TREE
);
8691 // Make a reference to a single result of a call which returns
8692 // multiple results.
8695 Expression::make_call_result(Call_expression
* call
, unsigned int index
)
8697 return new Call_result_expression(call
, index
);
8700 // Class Index_expression.
8705 Index_expression::do_traverse(Traverse
* traverse
)
8707 if (Expression::traverse(&this->left_
, traverse
) == TRAVERSE_EXIT
8708 || Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
8709 || (this->end_
!= NULL
8710 && Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
))
8711 return TRAVERSE_EXIT
;
8712 return TRAVERSE_CONTINUE
;
8715 // Lower an index expression. This converts the generic index
8716 // expression into an array index, a string index, or a map index.
8719 Index_expression::do_lower(Gogo
*, Named_object
*, int)
8721 source_location location
= this->location();
8722 Expression
* left
= this->left_
;
8723 Expression
* start
= this->start_
;
8724 Expression
* end
= this->end_
;
8726 Type
* type
= left
->type();
8727 if (type
->is_error_type())
8728 return Expression::make_error(location
);
8729 else if (type
->array_type() != NULL
)
8730 return Expression::make_array_index(left
, start
, end
, location
);
8731 else if (type
->points_to() != NULL
8732 && type
->points_to()->array_type() != NULL
8733 && !type
->points_to()->is_open_array_type())
8735 Expression
* deref
= Expression::make_unary(OPERATOR_MULT
, left
,
8737 return Expression::make_array_index(deref
, start
, end
, location
);
8739 else if (type
->is_string_type())
8740 return Expression::make_string_index(left
, start
, end
, location
);
8741 else if (type
->map_type() != NULL
)
8745 error_at(location
, "invalid slice of map");
8746 return Expression::make_error(location
);
8748 Map_index_expression
* ret
= Expression::make_map_index(left
, start
,
8750 if (this->is_lvalue_
)
8751 ret
->set_is_lvalue();
8757 "attempt to index object which is not array, string, or map");
8758 return Expression::make_error(location
);
8762 // Make an index expression.
8765 Expression::make_index(Expression
* left
, Expression
* start
, Expression
* end
,
8766 source_location location
)
8768 return new Index_expression(left
, start
, end
, location
);
8771 // An array index. This is used for both indexing and slicing.
8773 class Array_index_expression
: public Expression
8776 Array_index_expression(Expression
* array
, Expression
* start
,
8777 Expression
* end
, source_location location
)
8778 : Expression(EXPRESSION_ARRAY_INDEX
, location
),
8779 array_(array
), start_(start
), end_(end
), type_(NULL
)
8784 do_traverse(Traverse
*);
8790 do_determine_type(const Type_context
*);
8793 do_check_types(Gogo
*);
8798 return Expression::make_array_index(this->array_
->copy(),
8799 this->start_
->copy(),
8802 : this->end_
->copy()),
8807 do_is_addressable() const;
8810 do_address_taken(bool escapes
)
8811 { this->array_
->address_taken(escapes
); }
8814 do_get_tree(Translate_context
*);
8817 // The array we are getting a value from.
8819 // The start or only index.
8821 // The end index of a slice. This may be NULL for a simple array
8822 // index, or it may be a nil expression for the length of the array.
8824 // The type of the expression.
8828 // Array index traversal.
8831 Array_index_expression::do_traverse(Traverse
* traverse
)
8833 if (Expression::traverse(&this->array_
, traverse
) == TRAVERSE_EXIT
)
8834 return TRAVERSE_EXIT
;
8835 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
8836 return TRAVERSE_EXIT
;
8837 if (this->end_
!= NULL
)
8839 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
8840 return TRAVERSE_EXIT
;
8842 return TRAVERSE_CONTINUE
;
8845 // Return the type of an array index.
8848 Array_index_expression::do_type()
8850 if (this->type_
== NULL
)
8852 Array_type
* type
= this->array_
->type()->array_type();
8854 this->type_
= Type::make_error_type();
8855 else if (this->end_
== NULL
)
8856 this->type_
= type
->element_type();
8857 else if (type
->is_open_array_type())
8859 // A slice of a slice has the same type as the original
8861 this->type_
= this->array_
->type()->deref();
8865 // A slice of an array is a slice.
8866 this->type_
= Type::make_array_type(type
->element_type(), NULL
);
8872 // Set the type of an array index.
8875 Array_index_expression::do_determine_type(const Type_context
*)
8877 this->array_
->determine_type_no_context();
8878 Type_context
subcontext(NULL
, true);
8879 this->start_
->determine_type(&subcontext
);
8880 if (this->end_
!= NULL
)
8881 this->end_
->determine_type(&subcontext
);
8884 // Check types of an array index.
8887 Array_index_expression::do_check_types(Gogo
*)
8889 if (this->start_
->type()->integer_type() == NULL
)
8890 this->report_error(_("index must be integer"));
8891 if (this->end_
!= NULL
8892 && this->end_
->type()->integer_type() == NULL
8893 && !this->end_
->is_nil_expression())
8894 this->report_error(_("slice end must be integer"));
8896 Array_type
* array_type
= this->array_
->type()->array_type();
8897 gcc_assert(array_type
!= NULL
);
8899 unsigned int int_bits
=
8900 Type::lookup_integer_type("int")->integer_type()->bits();
8905 bool lval_valid
= (array_type
->length() != NULL
8906 && array_type
->length()->integer_constant_value(true,
8911 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
8913 if (mpz_sgn(ival
) < 0
8914 || mpz_sizeinbase(ival
, 2) >= int_bits
8916 && (this->end_
== NULL
8917 ? mpz_cmp(ival
, lval
) >= 0
8918 : mpz_cmp(ival
, lval
) > 0)))
8920 error_at(this->start_
->location(), "array index out of bounds");
8921 this->set_is_error();
8924 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
8926 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
8928 if (mpz_sgn(ival
) < 0
8929 || mpz_sizeinbase(ival
, 2) >= int_bits
8930 || (lval_valid
&& mpz_cmp(ival
, lval
) > 0))
8932 error_at(this->end_
->location(), "array index out of bounds");
8933 this->set_is_error();
8940 // A slice of an array requires an addressable array. A slice of a
8941 // slice is always possible.
8942 if (this->end_
!= NULL
8943 && !array_type
->is_open_array_type()
8944 && !this->array_
->is_addressable())
8945 this->report_error(_("array is not addressable"));
8948 // Return whether this expression is addressable.
8951 Array_index_expression::do_is_addressable() const
8953 // A slice expression is not addressable.
8954 if (this->end_
!= NULL
)
8957 // An index into a slice is addressable.
8958 if (this->array_
->type()->is_open_array_type())
8961 // An index into an array is addressable if the array is
8963 return this->array_
->is_addressable();
8966 // Get a tree for an array index.
8969 Array_index_expression::do_get_tree(Translate_context
* context
)
8971 Gogo
* gogo
= context
->gogo();
8972 source_location loc
= this->location();
8974 Array_type
* array_type
= this->array_
->type()->array_type();
8975 gcc_assert(array_type
!= NULL
);
8977 tree type_tree
= array_type
->get_tree(gogo
);
8979 tree array_tree
= this->array_
->get_tree(context
);
8980 if (array_tree
== error_mark_node
)
8981 return error_mark_node
;
8983 if (array_type
->length() == NULL
&& !DECL_P(array_tree
))
8984 array_tree
= save_expr(array_tree
);
8985 tree length_tree
= array_type
->length_tree(gogo
, array_tree
);
8986 length_tree
= save_expr(length_tree
);
8987 tree length_type
= TREE_TYPE(length_tree
);
8989 tree bad_index
= boolean_false_node
;
8991 tree start_tree
= this->start_
->get_tree(context
);
8992 if (start_tree
== error_mark_node
)
8993 return error_mark_node
;
8994 if (!DECL_P(start_tree
))
8995 start_tree
= save_expr(start_tree
);
8996 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
8997 start_tree
= convert_to_integer(length_type
, start_tree
);
8999 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9002 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9003 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
, bad_index
,
9004 fold_build2_loc(loc
,
9008 boolean_type_node
, start_tree
,
9011 int code
= (array_type
->length() != NULL
9012 ? (this->end_
== NULL
9013 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9014 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS
)
9015 : (this->end_
== NULL
9016 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9017 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS
));
9018 tree crash
= Gogo::runtime_error(code
, loc
);
9020 if (this->end_
== NULL
)
9022 // Simple array indexing. This has to return an l-value, so
9023 // wrap the index check into START_TREE.
9024 start_tree
= build2(COMPOUND_EXPR
, TREE_TYPE(start_tree
),
9025 build3(COND_EXPR
, void_type_node
,
9026 bad_index
, crash
, NULL_TREE
),
9028 start_tree
= fold_convert_loc(loc
, sizetype
, start_tree
);
9030 if (array_type
->length() != NULL
)
9033 return build4(ARRAY_REF
, TREE_TYPE(type_tree
), array_tree
,
9034 start_tree
, NULL_TREE
, NULL_TREE
);
9039 tree values
= array_type
->value_pointer_tree(gogo
, array_tree
);
9040 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9041 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9042 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9043 start_tree
, element_size
);
9044 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9045 TREE_TYPE(values
), values
, offset
);
9046 return build_fold_indirect_ref(ptr
);
9052 tree capacity_tree
= array_type
->capacity_tree(gogo
, array_tree
);
9053 capacity_tree
= fold_convert_loc(loc
, length_type
, capacity_tree
);
9056 if (this->end_
->is_nil_expression())
9057 end_tree
= length_tree
;
9060 end_tree
= this->end_
->get_tree(context
);
9061 if (end_tree
== error_mark_node
)
9062 return error_mark_node
;
9063 if (!DECL_P(end_tree
))
9064 end_tree
= save_expr(end_tree
);
9065 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9066 end_tree
= convert_to_integer(length_type
, end_tree
);
9068 bad_index
= Expression::check_bounds(end_tree
, length_type
, bad_index
,
9071 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9073 capacity_tree
= save_expr(capacity_tree
);
9074 tree bad_end
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9075 fold_build2_loc(loc
, LT_EXPR
,
9077 end_tree
, start_tree
),
9078 fold_build2_loc(loc
, GT_EXPR
,
9080 end_tree
, capacity_tree
));
9081 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9082 bad_index
, bad_end
);
9085 tree element_type_tree
= array_type
->element_type()->get_tree(gogo
);
9086 tree element_size
= TYPE_SIZE_UNIT(element_type_tree
);
9088 tree offset
= fold_build2_loc(loc
, MULT_EXPR
, sizetype
,
9089 fold_convert_loc(loc
, sizetype
, start_tree
),
9092 tree value_pointer
= array_type
->value_pointer_tree(gogo
, array_tree
);
9094 value_pointer
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
,
9095 TREE_TYPE(value_pointer
),
9096 value_pointer
, offset
);
9098 tree result_length_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9099 end_tree
, start_tree
);
9101 tree result_capacity_tree
= fold_build2_loc(loc
, MINUS_EXPR
, length_type
,
9102 capacity_tree
, start_tree
);
9104 tree struct_tree
= this->type()->get_tree(gogo
);
9105 gcc_assert(TREE_CODE(struct_tree
) == RECORD_TYPE
);
9107 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
9109 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9110 tree field
= TYPE_FIELDS(struct_tree
);
9111 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
9113 elt
->value
= value_pointer
;
9115 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9116 field
= DECL_CHAIN(field
);
9117 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
9119 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_length_tree
);
9121 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
9122 field
= DECL_CHAIN(field
);
9123 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
9125 elt
->value
= fold_convert_loc(loc
, TREE_TYPE(field
), result_capacity_tree
);
9127 tree constructor
= build_constructor(struct_tree
, init
);
9129 if (TREE_CONSTANT(value_pointer
)
9130 && TREE_CONSTANT(result_length_tree
)
9131 && TREE_CONSTANT(result_capacity_tree
))
9132 TREE_CONSTANT(constructor
) = 1;
9134 return fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(constructor
),
9135 build3(COND_EXPR
, void_type_node
,
9136 bad_index
, crash
, NULL_TREE
),
9140 // Make an array index expression. END may be NULL.
9143 Expression::make_array_index(Expression
* array
, Expression
* start
,
9144 Expression
* end
, source_location location
)
9146 // Taking a slice of a composite literal requires moving the literal
9148 if (end
!= NULL
&& array
->is_composite_literal())
9150 array
= Expression::make_heap_composite(array
, location
);
9151 array
= Expression::make_unary(OPERATOR_MULT
, array
, location
);
9153 return new Array_index_expression(array
, start
, end
, location
);
9156 // A string index. This is used for both indexing and slicing.
9158 class String_index_expression
: public Expression
9161 String_index_expression(Expression
* string
, Expression
* start
,
9162 Expression
* end
, source_location location
)
9163 : Expression(EXPRESSION_STRING_INDEX
, location
),
9164 string_(string
), start_(start
), end_(end
)
9169 do_traverse(Traverse
*);
9175 do_determine_type(const Type_context
*);
9178 do_check_types(Gogo
*);
9183 return Expression::make_string_index(this->string_
->copy(),
9184 this->start_
->copy(),
9187 : this->end_
->copy()),
9192 do_get_tree(Translate_context
*);
9195 // The string we are getting a value from.
9196 Expression
* string_
;
9197 // The start or only index.
9199 // The end index of a slice. This may be NULL for a single index,
9200 // or it may be a nil expression for the length of the string.
9204 // String index traversal.
9207 String_index_expression::do_traverse(Traverse
* traverse
)
9209 if (Expression::traverse(&this->string_
, traverse
) == TRAVERSE_EXIT
)
9210 return TRAVERSE_EXIT
;
9211 if (Expression::traverse(&this->start_
, traverse
) == TRAVERSE_EXIT
)
9212 return TRAVERSE_EXIT
;
9213 if (this->end_
!= NULL
)
9215 if (Expression::traverse(&this->end_
, traverse
) == TRAVERSE_EXIT
)
9216 return TRAVERSE_EXIT
;
9218 return TRAVERSE_CONTINUE
;
9221 // Return the type of a string index.
9224 String_index_expression::do_type()
9226 if (this->end_
== NULL
)
9227 return Type::lookup_integer_type("uint8");
9229 return Type::make_string_type();
9232 // Determine the type of a string index.
9235 String_index_expression::do_determine_type(const Type_context
*)
9237 this->string_
->determine_type_no_context();
9238 Type_context
subcontext(NULL
, true);
9239 this->start_
->determine_type(&subcontext
);
9240 if (this->end_
!= NULL
)
9241 this->end_
->determine_type(&subcontext
);
9244 // Check types of a string index.
9247 String_index_expression::do_check_types(Gogo
*)
9249 if (this->start_
->type()->integer_type() == NULL
)
9250 this->report_error(_("index must be integer"));
9251 if (this->end_
!= NULL
9252 && this->end_
->type()->integer_type() == NULL
9253 && !this->end_
->is_nil_expression())
9254 this->report_error(_("slice end must be integer"));
9257 bool sval_valid
= this->string_
->string_constant_value(&sval
);
9262 if (this->start_
->integer_constant_value(true, ival
, &dummy
))
9264 if (mpz_sgn(ival
) < 0
9265 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) >= 0))
9267 error_at(this->start_
->location(), "string index out of bounds");
9268 this->set_is_error();
9271 if (this->end_
!= NULL
&& !this->end_
->is_nil_expression())
9273 if (this->end_
->integer_constant_value(true, ival
, &dummy
))
9275 if (mpz_sgn(ival
) < 0
9276 || (sval_valid
&& mpz_cmp_ui(ival
, sval
.length()) > 0))
9278 error_at(this->end_
->location(), "string index out of bounds");
9279 this->set_is_error();
9286 // Get a tree for a string index.
9289 String_index_expression::do_get_tree(Translate_context
* context
)
9291 source_location loc
= this->location();
9293 tree string_tree
= this->string_
->get_tree(context
);
9294 if (string_tree
== error_mark_node
)
9295 return error_mark_node
;
9297 if (this->string_
->type()->points_to() != NULL
)
9298 string_tree
= build_fold_indirect_ref(string_tree
);
9299 if (!DECL_P(string_tree
))
9300 string_tree
= save_expr(string_tree
);
9301 tree string_type
= TREE_TYPE(string_tree
);
9303 tree length_tree
= String_type::length_tree(context
->gogo(), string_tree
);
9304 length_tree
= save_expr(length_tree
);
9305 tree length_type
= TREE_TYPE(length_tree
);
9307 tree bad_index
= boolean_false_node
;
9309 tree start_tree
= this->start_
->get_tree(context
);
9310 if (start_tree
== error_mark_node
)
9311 return error_mark_node
;
9312 if (!DECL_P(start_tree
))
9313 start_tree
= save_expr(start_tree
);
9314 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree
)))
9315 start_tree
= convert_to_integer(length_type
, start_tree
);
9317 bad_index
= Expression::check_bounds(start_tree
, length_type
, bad_index
,
9320 start_tree
= fold_convert_loc(loc
, length_type
, start_tree
);
9322 int code
= (this->end_
== NULL
9323 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9324 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS
);
9325 tree crash
= Gogo::runtime_error(code
, loc
);
9327 if (this->end_
== NULL
)
9329 bad_index
= fold_build2_loc(loc
, TRUTH_OR_EXPR
, boolean_type_node
,
9331 fold_build2_loc(loc
, GE_EXPR
,
9333 start_tree
, length_tree
));
9335 tree bytes_tree
= String_type::bytes_tree(context
->gogo(), string_tree
);
9336 tree ptr
= fold_build2_loc(loc
, POINTER_PLUS_EXPR
, TREE_TYPE(bytes_tree
),
9338 fold_convert_loc(loc
, sizetype
, start_tree
));
9339 tree index
= build_fold_indirect_ref_loc(loc
, ptr
);
9341 return build2(COMPOUND_EXPR
, TREE_TYPE(index
),
9342 build3(COND_EXPR
, void_type_node
,
9343 bad_index
, crash
, NULL_TREE
),
9349 if (this->end_
->is_nil_expression())
9350 end_tree
= build_int_cst(length_type
, -1);
9353 end_tree
= this->end_
->get_tree(context
);
9354 if (end_tree
== error_mark_node
)
9355 return error_mark_node
;
9356 if (!DECL_P(end_tree
))
9357 end_tree
= save_expr(end_tree
);
9358 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree
)))
9359 end_tree
= convert_to_integer(length_type
, end_tree
);
9361 bad_index
= Expression::check_bounds(end_tree
, length_type
,
9364 end_tree
= fold_convert_loc(loc
, length_type
, end_tree
);
9367 static tree strslice_fndecl
;
9368 tree ret
= Gogo::call_builtin(&strslice_fndecl
,
9370 "__go_string_slice",
9379 // This will panic if the bounds are out of range for the
9381 TREE_NOTHROW(strslice_fndecl
) = 0;
9383 if (bad_index
== boolean_false_node
)
9386 return build2(COMPOUND_EXPR
, TREE_TYPE(ret
),
9387 build3(COND_EXPR
, void_type_node
,
9388 bad_index
, crash
, NULL_TREE
),
9393 // Make a string index expression. END may be NULL.
9396 Expression::make_string_index(Expression
* string
, Expression
* start
,
9397 Expression
* end
, source_location location
)
9399 return new String_index_expression(string
, start
, end
, location
);
9404 // Get the type of the map.
9407 Map_index_expression::get_map_type() const
9409 Map_type
* mt
= this->map_
->type()->deref()->map_type();
9410 gcc_assert(mt
!= NULL
);
9414 // Map index traversal.
9417 Map_index_expression::do_traverse(Traverse
* traverse
)
9419 if (Expression::traverse(&this->map_
, traverse
) == TRAVERSE_EXIT
)
9420 return TRAVERSE_EXIT
;
9421 return Expression::traverse(&this->index_
, traverse
);
9424 // Return the type of a map index.
9427 Map_index_expression::do_type()
9429 Type
* type
= this->get_map_type()->val_type();
9430 // If this map index is in a tuple assignment, we actually return a
9431 // pointer to the value type. Tuple_map_assignment_statement is
9432 // responsible for handling this correctly. We need to get the type
9433 // right in case this gets assigned to a temporary variable.
9434 if (this->is_in_tuple_assignment_
)
9435 type
= Type::make_pointer_type(type
);
9439 // Fix the type of a map index.
9442 Map_index_expression::do_determine_type(const Type_context
*)
9444 this->map_
->determine_type_no_context();
9445 Type_context
subcontext(this->get_map_type()->key_type(), false);
9446 this->index_
->determine_type(&subcontext
);
9449 // Check types of a map index.
9452 Map_index_expression::do_check_types(Gogo
*)
9455 if (!Type::are_assignable(this->get_map_type()->key_type(),
9456 this->index_
->type(), &reason
))
9459 this->report_error(_("incompatible type for map index"));
9462 error_at(this->location(), "incompatible type for map index (%s)",
9464 this->set_is_error();
9469 // Get a tree for a map index.
9472 Map_index_expression::do_get_tree(Translate_context
* context
)
9474 Map_type
* type
= this->get_map_type();
9476 tree valptr
= this->get_value_pointer(context
, this->is_lvalue_
);
9477 if (valptr
== error_mark_node
)
9478 return error_mark_node
;
9479 valptr
= save_expr(valptr
);
9481 tree val_type_tree
= TREE_TYPE(TREE_TYPE(valptr
));
9483 if (this->is_lvalue_
)
9484 return build_fold_indirect_ref(valptr
);
9485 else if (this->is_in_tuple_assignment_
)
9487 // Tuple_map_assignment_statement is responsible for using this
9493 return fold_build3(COND_EXPR
, val_type_tree
,
9494 fold_build2(EQ_EXPR
, boolean_type_node
, valptr
,
9495 fold_convert(TREE_TYPE(valptr
),
9496 null_pointer_node
)),
9497 type
->val_type()->get_init_tree(context
->gogo(),
9499 build_fold_indirect_ref(valptr
));
9503 // Get a tree for the map index. This returns a tree which evaluates
9504 // to a pointer to a value. The pointer will be NULL if the key is
9508 Map_index_expression::get_value_pointer(Translate_context
* context
,
9511 Map_type
* type
= this->get_map_type();
9513 tree map_tree
= this->map_
->get_tree(context
);
9514 tree index_tree
= this->index_
->get_tree(context
);
9515 index_tree
= Expression::convert_for_assignment(context
, type
->key_type(),
9516 this->index_
->type(),
9519 if (map_tree
== error_mark_node
|| index_tree
== error_mark_node
)
9520 return error_mark_node
;
9522 if (this->map_
->type()->points_to() != NULL
)
9523 map_tree
= build_fold_indirect_ref(map_tree
);
9525 // We need to pass in a pointer to the key, so stuff it into a
9527 tree tmp
= create_tmp_var(TREE_TYPE(index_tree
), get_name(index_tree
));
9528 DECL_IGNORED_P(tmp
) = 0;
9529 DECL_INITIAL(tmp
) = index_tree
;
9530 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
9531 tree tmpref
= fold_convert(const_ptr_type_node
, build_fold_addr_expr(tmp
));
9532 TREE_ADDRESSABLE(tmp
) = 1;
9534 static tree map_index_fndecl
;
9535 tree call
= Gogo::call_builtin(&map_index_fndecl
,
9539 const_ptr_type_node
,
9540 TREE_TYPE(map_tree
),
9542 const_ptr_type_node
,
9547 : boolean_false_node
));
9548 // This can panic on a map of interface type if the interface holds
9549 // an uncomparable or unhashable type.
9550 TREE_NOTHROW(map_index_fndecl
) = 0;
9552 tree val_type_tree
= type
->val_type()->get_tree(context
->gogo());
9553 if (val_type_tree
== error_mark_node
)
9554 return error_mark_node
;
9555 tree ptr_val_type_tree
= build_pointer_type(val_type_tree
);
9557 return build2(COMPOUND_EXPR
, ptr_val_type_tree
,
9559 fold_convert(ptr_val_type_tree
, call
));
9562 // Make a map index expression.
9564 Map_index_expression
*
9565 Expression::make_map_index(Expression
* map
, Expression
* index
,
9566 source_location location
)
9568 return new Map_index_expression(map
, index
, location
);
9571 // Class Field_reference_expression.
9573 // Return the type of a field reference.
9576 Field_reference_expression::do_type()
9578 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9579 gcc_assert(struct_type
!= NULL
);
9580 return struct_type
->field(this->field_index_
)->type();
9583 // Check the types for a field reference.
9586 Field_reference_expression::do_check_types(Gogo
*)
9588 Struct_type
* struct_type
= this->expr_
->type()->struct_type();
9589 gcc_assert(struct_type
!= NULL
);
9590 gcc_assert(struct_type
->field(this->field_index_
) != NULL
);
9593 // Get a tree for a field reference.
9596 Field_reference_expression::do_get_tree(Translate_context
* context
)
9598 tree struct_tree
= this->expr_
->get_tree(context
);
9599 if (struct_tree
== error_mark_node
9600 || TREE_TYPE(struct_tree
) == error_mark_node
)
9601 return error_mark_node
;
9602 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree
)) == RECORD_TYPE
);
9603 tree field
= TYPE_FIELDS(TREE_TYPE(struct_tree
));
9604 gcc_assert(field
!= NULL_TREE
);
9605 for (unsigned int i
= this->field_index_
; i
> 0; --i
)
9607 field
= DECL_CHAIN(field
);
9608 gcc_assert(field
!= NULL_TREE
);
9610 return build3(COMPONENT_REF
, TREE_TYPE(field
), struct_tree
, field
,
9614 // Make a reference to a qualified identifier in an expression.
9616 Field_reference_expression
*
9617 Expression::make_field_reference(Expression
* expr
, unsigned int field_index
,
9618 source_location location
)
9620 return new Field_reference_expression(expr
, field_index
, location
);
9623 // Class Interface_field_reference_expression.
9625 // Return a tree for the pointer to the function to call.
9628 Interface_field_reference_expression::get_function_tree(Translate_context
*,
9631 if (this->expr_
->type()->points_to() != NULL
)
9632 expr
= build_fold_indirect_ref(expr
);
9634 tree expr_type
= TREE_TYPE(expr
);
9635 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9637 tree field
= TYPE_FIELDS(expr_type
);
9638 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__methods") == 0);
9640 tree table
= build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9641 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table
)));
9643 table
= build_fold_indirect_ref(table
);
9644 gcc_assert(TREE_CODE(TREE_TYPE(table
)) == RECORD_TYPE
);
9646 std::string name
= Gogo::unpack_hidden_name(this->name_
);
9647 for (field
= DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table
)));
9649 field
= DECL_CHAIN(field
))
9651 if (name
== IDENTIFIER_POINTER(DECL_NAME(field
)))
9654 gcc_assert(field
!= NULL_TREE
);
9656 return build3(COMPONENT_REF
, TREE_TYPE(field
), table
, field
, NULL_TREE
);
9659 // Return a tree for the first argument to pass to the interface
9663 Interface_field_reference_expression::get_underlying_object_tree(
9667 if (this->expr_
->type()->points_to() != NULL
)
9668 expr
= build_fold_indirect_ref(expr
);
9670 tree expr_type
= TREE_TYPE(expr
);
9671 gcc_assert(TREE_CODE(expr_type
) == RECORD_TYPE
);
9673 tree field
= DECL_CHAIN(TYPE_FIELDS(expr_type
));
9674 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__object") == 0);
9676 return build3(COMPONENT_REF
, TREE_TYPE(field
), expr
, field
, NULL_TREE
);
9682 Interface_field_reference_expression::do_traverse(Traverse
* traverse
)
9684 return Expression::traverse(&this->expr_
, traverse
);
9687 // Return the type of an interface field reference.
9690 Interface_field_reference_expression::do_type()
9692 Type
* expr_type
= this->expr_
->type();
9694 Type
* points_to
= expr_type
->points_to();
9695 if (points_to
!= NULL
)
9696 expr_type
= points_to
;
9698 Interface_type
* interface_type
= expr_type
->interface_type();
9699 if (interface_type
== NULL
)
9700 return Type::make_error_type();
9702 const Typed_identifier
* method
= interface_type
->find_method(this->name_
);
9704 return Type::make_error_type();
9706 return method
->type();
9712 Interface_field_reference_expression::do_determine_type(const Type_context
*)
9714 this->expr_
->determine_type_no_context();
9717 // Check the types for an interface field reference.
9720 Interface_field_reference_expression::do_check_types(Gogo
*)
9722 Type
* type
= this->expr_
->type();
9724 Type
* points_to
= type
->points_to();
9725 if (points_to
!= NULL
)
9728 Interface_type
* interface_type
= type
->interface_type();
9729 if (interface_type
== NULL
)
9730 this->report_error(_("expected interface or pointer to interface"));
9733 const Typed_identifier
* method
=
9734 interface_type
->find_method(this->name_
);
9737 error_at(this->location(), "method %qs not in interface",
9738 Gogo::message_name(this->name_
).c_str());
9739 this->set_is_error();
9744 // Get a tree for a reference to a field in an interface. There is no
9745 // standard tree type representation for this: it's a function
9746 // attached to its first argument, like a Bound_method_expression.
9747 // The only places it may currently be used are in a Call_expression
9748 // or a Go_statement, which will take it apart directly. So this has
9749 // nothing to do at present.
9752 Interface_field_reference_expression::do_get_tree(Translate_context
*)
9757 // Make a reference to a field in an interface.
9760 Expression::make_interface_field_reference(Expression
* expr
,
9761 const std::string
& field
,
9762 source_location location
)
9764 return new Interface_field_reference_expression(expr
, field
, location
);
9767 // A general selector. This is a Parser_expression for LEFT.NAME. It
9768 // is lowered after we know the type of the left hand side.
9770 class Selector_expression
: public Parser_expression
9773 Selector_expression(Expression
* left
, const std::string
& name
,
9774 source_location location
)
9775 : Parser_expression(EXPRESSION_SELECTOR
, location
),
9776 left_(left
), name_(name
)
9781 do_traverse(Traverse
* traverse
)
9782 { return Expression::traverse(&this->left_
, traverse
); }
9785 do_lower(Gogo
*, Named_object
*, int);
9790 return new Selector_expression(this->left_
->copy(), this->name_
,
9796 lower_method_expression(Gogo
*);
9798 // The expression on the left hand side.
9800 // The name on the right hand side.
9804 // Lower a selector expression once we know the real type of the left
9808 Selector_expression::do_lower(Gogo
* gogo
, Named_object
*, int)
9810 Expression
* left
= this->left_
;
9811 if (left
->is_type_expression())
9812 return this->lower_method_expression(gogo
);
9813 return Type::bind_field_or_method(gogo
, left
->type(), left
, this->name_
,
9817 // Lower a method expression T.M or (*T).M. We turn this into a
9818 // function literal.
9821 Selector_expression::lower_method_expression(Gogo
* gogo
)
9823 source_location location
= this->location();
9824 Type
* type
= this->left_
->type();
9825 const std::string
& name(this->name_
);
9828 if (type
->points_to() == NULL
)
9833 type
= type
->points_to();
9835 Named_type
* nt
= type
->named_type();
9839 ("method expression requires named type or "
9840 "pointer to named type"));
9841 return Expression::make_error(location
);
9845 Method
* method
= nt
->method_function(name
, &is_ambiguous
);
9849 error_at(location
, "type %<%s%> has no method %<%s%>",
9850 nt
->message_name().c_str(),
9851 Gogo::message_name(name
).c_str());
9853 error_at(location
, "method %<%s%> is ambiguous in type %<%s%>",
9854 Gogo::message_name(name
).c_str(),
9855 nt
->message_name().c_str());
9856 return Expression::make_error(location
);
9859 if (!is_pointer
&& !method
->is_value_method())
9861 error_at(location
, "method requires pointer (use %<(*%s).%s)%>",
9862 nt
->message_name().c_str(),
9863 Gogo::message_name(name
).c_str());
9864 return Expression::make_error(location
);
9867 // Build a new function type in which the receiver becomes the first
9869 Function_type
* method_type
= method
->type();
9870 gcc_assert(method_type
->is_method());
9872 const char* const receiver_name
= "$this";
9873 Typed_identifier_list
* parameters
= new Typed_identifier_list();
9874 parameters
->push_back(Typed_identifier(receiver_name
, this->left_
->type(),
9877 const Typed_identifier_list
* method_parameters
= method_type
->parameters();
9878 if (method_parameters
!= NULL
)
9880 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
9881 p
!= method_parameters
->end();
9883 parameters
->push_back(*p
);
9886 const Typed_identifier_list
* method_results
= method_type
->results();
9887 Typed_identifier_list
* results
;
9888 if (method_results
== NULL
)
9892 results
= new Typed_identifier_list();
9893 for (Typed_identifier_list::const_iterator p
= method_results
->begin();
9894 p
!= method_results
->end();
9896 results
->push_back(*p
);
9899 Function_type
* fntype
= Type::make_function_type(NULL
, parameters
, results
,
9901 if (method_type
->is_varargs())
9902 fntype
->set_is_varargs();
9904 // We generate methods which always takes a pointer to the receiver
9905 // as their first argument. If this is for a pointer type, we can
9906 // simply reuse the existing function. We use an internal hack to
9907 // get the right type.
9911 Named_object
* mno
= (method
->needs_stub_method()
9912 ? method
->stub_object()
9913 : method
->named_object());
9914 Expression
* f
= Expression::make_func_reference(mno
, NULL
, location
);
9915 f
= Expression::make_cast(fntype
, f
, location
);
9916 Type_conversion_expression
* tce
=
9917 static_cast<Type_conversion_expression
*>(f
);
9918 tce
->set_may_convert_function_types();
9922 Named_object
* no
= gogo
->start_function(Gogo::thunk_name(), fntype
, false,
9925 Named_object
* vno
= gogo
->lookup(receiver_name
, NULL
);
9926 gcc_assert(vno
!= NULL
);
9927 Expression
* ve
= Expression::make_var_reference(vno
, location
);
9928 Expression
* bm
= Type::bind_field_or_method(gogo
, nt
, ve
, name
, location
);
9929 gcc_assert(bm
!= NULL
&& !bm
->is_error_expression());
9931 Expression_list
* args
;
9932 if (method_parameters
== NULL
)
9936 args
= new Expression_list();
9937 for (Typed_identifier_list::const_iterator p
= method_parameters
->begin();
9938 p
!= method_parameters
->end();
9941 vno
= gogo
->lookup(p
->name(), NULL
);
9942 gcc_assert(vno
!= NULL
);
9943 args
->push_back(Expression::make_var_reference(vno
, location
));
9947 Call_expression
* call
= Expression::make_call(bm
, args
,
9948 method_type
->is_varargs(),
9951 size_t count
= call
->result_count();
9954 s
= Statement::make_statement(call
);
9957 Expression_list
* retvals
= new Expression_list();
9959 retvals
->push_back(call
);
9962 for (size_t i
= 0; i
< count
; ++i
)
9963 retvals
->push_back(Expression::make_call_result(call
, i
));
9965 s
= Statement::make_return_statement(no
->func_value()->type()->results(),
9968 gogo
->add_statement(s
);
9970 gogo
->finish_function(location
);
9972 return Expression::make_func_reference(no
, NULL
, location
);
9975 // Make a selector expression.
9978 Expression::make_selector(Expression
* left
, const std::string
& name
,
9979 source_location location
)
9981 return new Selector_expression(left
, name
, location
);
9984 // Implement the builtin function new.
9986 class Allocation_expression
: public Expression
9989 Allocation_expression(Type
* type
, source_location location
)
9990 : Expression(EXPRESSION_ALLOCATION
, location
),
9996 do_traverse(Traverse
* traverse
)
9997 { return Type::traverse(this->type_
, traverse
); }
10001 { return Type::make_pointer_type(this->type_
); }
10004 do_determine_type(const Type_context
*)
10008 do_check_types(Gogo
*);
10012 { return new Allocation_expression(this->type_
, this->location()); }
10015 do_get_tree(Translate_context
*);
10018 // The type we are allocating.
10022 // Check the type of an allocation expression.
10025 Allocation_expression::do_check_types(Gogo
*)
10027 if (this->type_
->function_type() != NULL
)
10028 this->report_error(_("invalid new of function type"));
10031 // Return a tree for an allocation expression.
10034 Allocation_expression::do_get_tree(Translate_context
* context
)
10036 tree type_tree
= this->type_
->get_tree(context
->gogo());
10037 tree size_tree
= TYPE_SIZE_UNIT(type_tree
);
10038 tree space
= context
->gogo()->allocate_memory(this->type_
, size_tree
,
10040 return fold_convert(build_pointer_type(type_tree
), space
);
10043 // Make an allocation expression.
10046 Expression::make_allocation(Type
* type
, source_location location
)
10048 return new Allocation_expression(type
, location
);
10051 // Implement the builtin function make.
10053 class Make_expression
: public Expression
10056 Make_expression(Type
* type
, Expression_list
* args
, source_location location
)
10057 : Expression(EXPRESSION_MAKE
, location
),
10058 type_(type
), args_(args
)
10063 do_traverse(Traverse
* traverse
);
10067 { return this->type_
; }
10070 do_determine_type(const Type_context
*);
10073 do_check_types(Gogo
*);
10078 return new Make_expression(this->type_
, this->args_
->copy(),
10083 do_get_tree(Translate_context
*);
10086 // The type we are making.
10088 // The arguments to pass to the make routine.
10089 Expression_list
* args_
;
10095 Make_expression::do_traverse(Traverse
* traverse
)
10097 if (this->args_
!= NULL
10098 && this->args_
->traverse(traverse
) == TRAVERSE_EXIT
)
10099 return TRAVERSE_EXIT
;
10100 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10101 return TRAVERSE_EXIT
;
10102 return TRAVERSE_CONTINUE
;
10105 // Set types of arguments.
10108 Make_expression::do_determine_type(const Type_context
*)
10110 if (this->args_
!= NULL
)
10112 Type_context
context(Type::lookup_integer_type("int"), false);
10113 for (Expression_list::const_iterator pe
= this->args_
->begin();
10114 pe
!= this->args_
->end();
10116 (*pe
)->determine_type(&context
);
10120 // Check types for a make expression.
10123 Make_expression::do_check_types(Gogo
*)
10125 if (this->type_
->channel_type() == NULL
10126 && this->type_
->map_type() == NULL
10127 && (this->type_
->array_type() == NULL
10128 || this->type_
->array_type()->length() != NULL
))
10129 this->report_error(_("invalid type for make function"));
10130 else if (!this->type_
->check_make_expression(this->args_
, this->location()))
10131 this->set_is_error();
10134 // Return a tree for a make expression.
10137 Make_expression::do_get_tree(Translate_context
* context
)
10139 return this->type_
->make_expression_tree(context
, this->args_
,
10143 // Make a make expression.
10146 Expression::make_make(Type
* type
, Expression_list
* args
,
10147 source_location location
)
10149 return new Make_expression(type
, args
, location
);
10152 // Construct a struct.
10154 class Struct_construction_expression
: public Expression
10157 Struct_construction_expression(Type
* type
, Expression_list
* vals
,
10158 source_location location
)
10159 : Expression(EXPRESSION_STRUCT_CONSTRUCTION
, location
),
10160 type_(type
), vals_(vals
)
10163 // Return whether this is a constant initializer.
10165 is_constant_struct() const;
10169 do_traverse(Traverse
* traverse
);
10173 { return this->type_
; }
10176 do_determine_type(const Type_context
*);
10179 do_check_types(Gogo
*);
10184 return new Struct_construction_expression(this->type_
, this->vals_
->copy(),
10189 do_is_addressable() const
10193 do_get_tree(Translate_context
*);
10196 do_export(Export
*) const;
10199 // The type of the struct to construct.
10201 // The list of values, in order of the fields in the struct. A NULL
10202 // entry means that the field should be zero-initialized.
10203 Expression_list
* vals_
;
10209 Struct_construction_expression::do_traverse(Traverse
* traverse
)
10211 if (this->vals_
!= NULL
10212 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10213 return TRAVERSE_EXIT
;
10214 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10215 return TRAVERSE_EXIT
;
10216 return TRAVERSE_CONTINUE
;
10219 // Return whether this is a constant initializer.
10222 Struct_construction_expression::is_constant_struct() const
10224 if (this->vals_
== NULL
)
10226 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10227 pv
!= this->vals_
->end();
10231 && !(*pv
)->is_constant()
10232 && (!(*pv
)->is_composite_literal()
10233 || (*pv
)->is_nonconstant_composite_literal()))
10237 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10238 for (Struct_field_list::const_iterator pf
= fields
->begin();
10239 pf
!= fields
->end();
10242 // There are no constant constructors for interfaces.
10243 if (pf
->type()->interface_type() != NULL
)
10250 // Final type determination.
10253 Struct_construction_expression::do_determine_type(const Type_context
*)
10255 if (this->vals_
== NULL
)
10257 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10258 Expression_list::const_iterator pv
= this->vals_
->begin();
10259 for (Struct_field_list::const_iterator pf
= fields
->begin();
10260 pf
!= fields
->end();
10263 if (pv
== this->vals_
->end())
10267 Type_context
subcontext(pf
->type(), false);
10268 (*pv
)->determine_type(&subcontext
);
10276 Struct_construction_expression::do_check_types(Gogo
*)
10278 if (this->vals_
== NULL
)
10281 Struct_type
* st
= this->type_
->struct_type();
10282 if (this->vals_
->size() > st
->field_count())
10284 this->report_error(_("too many expressions for struct"));
10288 const Struct_field_list
* fields
= st
->fields();
10289 Expression_list::const_iterator pv
= this->vals_
->begin();
10291 for (Struct_field_list::const_iterator pf
= fields
->begin();
10292 pf
!= fields
->end();
10295 if (pv
== this->vals_
->end())
10297 this->report_error(_("too few expressions for struct"));
10304 std::string reason
;
10305 if (!Type::are_assignable(pf
->type(), (*pv
)->type(), &reason
))
10307 if (reason
.empty())
10308 error_at((*pv
)->location(),
10309 "incompatible type for field %d in struct construction",
10312 error_at((*pv
)->location(),
10313 ("incompatible type for field %d in "
10314 "struct construction (%s)"),
10315 i
+ 1, reason
.c_str());
10316 this->set_is_error();
10319 gcc_assert(pv
== this->vals_
->end());
10322 // Return a tree for constructing a struct.
10325 Struct_construction_expression::do_get_tree(Translate_context
* context
)
10327 Gogo
* gogo
= context
->gogo();
10329 if (this->vals_
== NULL
)
10330 return this->type_
->get_init_tree(gogo
, false);
10332 tree type_tree
= this->type_
->get_tree(gogo
);
10333 if (type_tree
== error_mark_node
)
10334 return error_mark_node
;
10335 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10337 bool is_constant
= true;
10338 const Struct_field_list
* fields
= this->type_
->struct_type()->fields();
10339 VEC(constructor_elt
,gc
)* elts
= VEC_alloc(constructor_elt
, gc
,
10341 Struct_field_list::const_iterator pf
= fields
->begin();
10342 Expression_list::const_iterator pv
= this->vals_
->begin();
10343 for (tree field
= TYPE_FIELDS(type_tree
);
10344 field
!= NULL_TREE
;
10345 field
= DECL_CHAIN(field
), ++pf
)
10347 gcc_assert(pf
!= fields
->end());
10350 if (pv
== this->vals_
->end())
10351 val
= pf
->type()->get_init_tree(gogo
, false);
10352 else if (*pv
== NULL
)
10354 val
= pf
->type()->get_init_tree(gogo
, false);
10359 val
= Expression::convert_for_assignment(context
, pf
->type(),
10361 (*pv
)->get_tree(context
),
10366 if (val
== error_mark_node
|| TREE_TYPE(val
) == error_mark_node
)
10367 return error_mark_node
;
10369 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, elts
, NULL
);
10370 elt
->index
= field
;
10372 if (!TREE_CONSTANT(val
))
10373 is_constant
= false;
10375 gcc_assert(pf
== fields
->end());
10377 tree ret
= build_constructor(type_tree
, elts
);
10379 TREE_CONSTANT(ret
) = 1;
10383 // Export a struct construction.
10386 Struct_construction_expression::do_export(Export
* exp
) const
10388 exp
->write_c_string("convert(");
10389 exp
->write_type(this->type_
);
10390 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10391 pv
!= this->vals_
->end();
10394 exp
->write_c_string(", ");
10396 (*pv
)->export_expression(exp
);
10398 exp
->write_c_string(")");
10401 // Make a struct composite literal. This used by the thunk code.
10404 Expression::make_struct_composite_literal(Type
* type
, Expression_list
* vals
,
10405 source_location location
)
10407 gcc_assert(type
->struct_type() != NULL
);
10408 return new Struct_construction_expression(type
, vals
, location
);
10411 // Construct an array. This class is not used directly; instead we
10412 // use the child classes, Fixed_array_construction_expression and
10413 // Open_array_construction_expression.
10415 class Array_construction_expression
: public Expression
10418 Array_construction_expression(Expression_classification classification
,
10419 Type
* type
, Expression_list
* vals
,
10420 source_location location
)
10421 : Expression(classification
, location
),
10422 type_(type
), vals_(vals
)
10426 // Return whether this is a constant initializer.
10428 is_constant_array() const;
10430 // Return the number of elements.
10432 element_count() const
10433 { return this->vals_
== NULL
? 0 : this->vals_
->size(); }
10437 do_traverse(Traverse
* traverse
);
10441 { return this->type_
; }
10444 do_determine_type(const Type_context
*);
10447 do_check_types(Gogo
*);
10450 do_is_addressable() const
10454 do_export(Export
*) const;
10456 // The list of values.
10459 { return this->vals_
; }
10461 // Get a constructor tree for the array values.
10463 get_constructor_tree(Translate_context
* context
, tree type_tree
);
10466 // The type of the array to construct.
10468 // The list of values.
10469 Expression_list
* vals_
;
10475 Array_construction_expression::do_traverse(Traverse
* traverse
)
10477 if (this->vals_
!= NULL
10478 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10479 return TRAVERSE_EXIT
;
10480 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10481 return TRAVERSE_EXIT
;
10482 return TRAVERSE_CONTINUE
;
10485 // Return whether this is a constant initializer.
10488 Array_construction_expression::is_constant_array() const
10490 if (this->vals_
== NULL
)
10493 // There are no constant constructors for interfaces.
10494 if (this->type_
->array_type()->element_type()->interface_type() != NULL
)
10497 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10498 pv
!= this->vals_
->end();
10502 && !(*pv
)->is_constant()
10503 && (!(*pv
)->is_composite_literal()
10504 || (*pv
)->is_nonconstant_composite_literal()))
10510 // Final type determination.
10513 Array_construction_expression::do_determine_type(const Type_context
*)
10515 if (this->vals_
== NULL
)
10517 Type_context
subcontext(this->type_
->array_type()->element_type(), false);
10518 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10519 pv
!= this->vals_
->end();
10523 (*pv
)->determine_type(&subcontext
);
10530 Array_construction_expression::do_check_types(Gogo
*)
10532 if (this->vals_
== NULL
)
10535 Array_type
* at
= this->type_
->array_type();
10537 Type
* element_type
= at
->element_type();
10538 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10539 pv
!= this->vals_
->end();
10543 && !Type::are_assignable(element_type
, (*pv
)->type(), NULL
))
10545 error_at((*pv
)->location(),
10546 "incompatible type for element %d in composite literal",
10548 this->set_is_error();
10552 Expression
* length
= at
->length();
10553 if (length
!= NULL
)
10558 if (at
->length()->integer_constant_value(true, val
, &type
))
10560 if (this->vals_
->size() > mpz_get_ui(val
))
10561 this->report_error(_("too many elements in composite literal"));
10567 // Get a constructor tree for the array values.
10570 Array_construction_expression::get_constructor_tree(Translate_context
* context
,
10573 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10574 (this->vals_
== NULL
10576 : this->vals_
->size()));
10577 Type
* element_type
= this->type_
->array_type()->element_type();
10578 bool is_constant
= true;
10579 if (this->vals_
!= NULL
)
10582 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10583 pv
!= this->vals_
->end();
10586 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
10587 elt
->index
= size_int(i
);
10589 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10592 tree value_tree
= (*pv
)->get_tree(context
);
10593 elt
->value
= Expression::convert_for_assignment(context
,
10599 if (elt
->value
== error_mark_node
)
10600 return error_mark_node
;
10601 if (!TREE_CONSTANT(elt
->value
))
10602 is_constant
= false;
10606 tree ret
= build_constructor(type_tree
, values
);
10608 TREE_CONSTANT(ret
) = 1;
10612 // Export an array construction.
10615 Array_construction_expression::do_export(Export
* exp
) const
10617 exp
->write_c_string("convert(");
10618 exp
->write_type(this->type_
);
10619 if (this->vals_
!= NULL
)
10621 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10622 pv
!= this->vals_
->end();
10625 exp
->write_c_string(", ");
10627 (*pv
)->export_expression(exp
);
10630 exp
->write_c_string(")");
10633 // Construct a fixed array.
10635 class Fixed_array_construction_expression
:
10636 public Array_construction_expression
10639 Fixed_array_construction_expression(Type
* type
, Expression_list
* vals
,
10640 source_location location
)
10641 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION
,
10642 type
, vals
, location
)
10644 gcc_assert(type
->array_type() != NULL
10645 && type
->array_type()->length() != NULL
);
10652 return new Fixed_array_construction_expression(this->type(),
10653 (this->vals() == NULL
10655 : this->vals()->copy()),
10660 do_get_tree(Translate_context
*);
10663 // Return a tree for constructing a fixed array.
10666 Fixed_array_construction_expression::do_get_tree(Translate_context
* context
)
10668 return this->get_constructor_tree(context
,
10669 this->type()->get_tree(context
->gogo()));
10672 // Construct an open array.
10674 class Open_array_construction_expression
: public Array_construction_expression
10677 Open_array_construction_expression(Type
* type
, Expression_list
* vals
,
10678 source_location location
)
10679 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION
,
10680 type
, vals
, location
)
10682 gcc_assert(type
->array_type() != NULL
10683 && type
->array_type()->length() == NULL
);
10687 // Note that taking the address of an open array literal is invalid.
10692 return new Open_array_construction_expression(this->type(),
10693 (this->vals() == NULL
10695 : this->vals()->copy()),
10700 do_get_tree(Translate_context
*);
10703 // Return a tree for constructing an open array.
10706 Open_array_construction_expression::do_get_tree(Translate_context
* context
)
10708 Type
* element_type
= this->type()->array_type()->element_type();
10709 tree element_type_tree
= element_type
->get_tree(context
->gogo());
10712 if (this->vals() == NULL
|| this->vals()->empty())
10714 // We need to create a unique value.
10715 tree max
= size_int(0);
10716 tree constructor_type
= build_array_type(element_type_tree
,
10717 build_index_type(max
));
10718 if (constructor_type
== error_mark_node
)
10719 return error_mark_node
;
10720 VEC(constructor_elt
,gc
)* vec
= VEC_alloc(constructor_elt
, gc
, 1);
10721 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, vec
, NULL
);
10722 elt
->index
= size_int(0);
10723 elt
->value
= element_type
->get_init_tree(context
->gogo(), false);
10724 values
= build_constructor(constructor_type
, vec
);
10725 if (TREE_CONSTANT(elt
->value
))
10726 TREE_CONSTANT(values
) = 1;
10727 length_tree
= size_int(0);
10731 tree max
= size_int(this->vals()->size() - 1);
10732 tree constructor_type
= build_array_type(element_type_tree
,
10733 build_index_type(max
));
10734 if (constructor_type
== error_mark_node
)
10735 return error_mark_node
;
10736 values
= this->get_constructor_tree(context
, constructor_type
);
10737 length_tree
= size_int(this->vals()->size());
10740 if (values
== error_mark_node
)
10741 return error_mark_node
;
10743 bool is_constant_initializer
= TREE_CONSTANT(values
);
10744 bool is_in_function
= context
->function() != NULL
;
10746 if (is_constant_initializer
)
10748 tree tmp
= build_decl(this->location(), VAR_DECL
,
10749 create_tmp_var_name("C"), TREE_TYPE(values
));
10750 DECL_EXTERNAL(tmp
) = 0;
10751 TREE_PUBLIC(tmp
) = 0;
10752 TREE_STATIC(tmp
) = 1;
10753 DECL_ARTIFICIAL(tmp
) = 1;
10754 if (is_in_function
)
10756 // If this is not a function, we will only initialize the
10757 // value once, so we can use this directly rather than
10758 // copying it. In that case we can't make it read-only,
10759 // because the program is permitted to change it.
10760 TREE_READONLY(tmp
) = 1;
10761 TREE_CONSTANT(tmp
) = 1;
10763 DECL_INITIAL(tmp
) = values
;
10764 rest_of_decl_compilation(tmp
, 1, 0);
10770 if (!is_in_function
&& is_constant_initializer
)
10772 // Outside of a function, we know the initializer will only run
10774 space
= build_fold_addr_expr(values
);
10779 tree memsize
= TYPE_SIZE_UNIT(TREE_TYPE(values
));
10780 space
= context
->gogo()->allocate_memory(element_type
, memsize
,
10782 space
= save_expr(space
);
10784 tree s
= fold_convert(build_pointer_type(TREE_TYPE(values
)), space
);
10785 tree ref
= build_fold_indirect_ref_loc(this->location(), s
);
10786 TREE_THIS_NOTRAP(ref
) = 1;
10787 set
= build2(MODIFY_EXPR
, void_type_node
, ref
, values
);
10790 // Build a constructor for the open array.
10792 tree type_tree
= this->type()->get_tree(context
->gogo());
10793 gcc_assert(TREE_CODE(type_tree
) == RECORD_TYPE
);
10795 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
10797 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
10798 tree field
= TYPE_FIELDS(type_tree
);
10799 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
10800 elt
->index
= field
;
10801 elt
->value
= fold_convert(TREE_TYPE(field
), space
);
10803 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
10804 field
= DECL_CHAIN(field
);
10805 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
10806 elt
->index
= field
;
10807 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
10809 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
10810 field
= DECL_CHAIN(field
);
10811 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)),"__capacity") == 0);
10812 elt
->index
= field
;
10813 elt
->value
= fold_convert(TREE_TYPE(field
), length_tree
);
10815 tree constructor
= build_constructor(type_tree
, init
);
10816 if (!is_in_function
&& is_constant_initializer
)
10817 TREE_CONSTANT(constructor
) = 1;
10819 if (set
== NULL_TREE
)
10820 return constructor
;
10822 return build2(COMPOUND_EXPR
, type_tree
, set
, constructor
);
10825 // Make a slice composite literal. This is used by the type
10826 // descriptor code.
10829 Expression::make_slice_composite_literal(Type
* type
, Expression_list
* vals
,
10830 source_location location
)
10832 gcc_assert(type
->is_open_array_type());
10833 return new Open_array_construction_expression(type
, vals
, location
);
10836 // Construct a map.
10838 class Map_construction_expression
: public Expression
10841 Map_construction_expression(Type
* type
, Expression_list
* vals
,
10842 source_location location
)
10843 : Expression(EXPRESSION_MAP_CONSTRUCTION
, location
),
10844 type_(type
), vals_(vals
)
10845 { gcc_assert(vals
== NULL
|| vals
->size() % 2 == 0); }
10849 do_traverse(Traverse
* traverse
);
10853 { return this->type_
; }
10856 do_determine_type(const Type_context
*);
10859 do_check_types(Gogo
*);
10864 return new Map_construction_expression(this->type_
, this->vals_
->copy(),
10869 do_get_tree(Translate_context
*);
10872 do_export(Export
*) const;
10875 // The type of the map to construct.
10877 // The list of values.
10878 Expression_list
* vals_
;
10884 Map_construction_expression::do_traverse(Traverse
* traverse
)
10886 if (this->vals_
!= NULL
10887 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
10888 return TRAVERSE_EXIT
;
10889 if (Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
10890 return TRAVERSE_EXIT
;
10891 return TRAVERSE_CONTINUE
;
10894 // Final type determination.
10897 Map_construction_expression::do_determine_type(const Type_context
*)
10899 if (this->vals_
== NULL
)
10902 Map_type
* mt
= this->type_
->map_type();
10903 Type_context
key_context(mt
->key_type(), false);
10904 Type_context
val_context(mt
->val_type(), false);
10905 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10906 pv
!= this->vals_
->end();
10909 (*pv
)->determine_type(&key_context
);
10911 (*pv
)->determine_type(&val_context
);
10918 Map_construction_expression::do_check_types(Gogo
*)
10920 if (this->vals_
== NULL
)
10923 Map_type
* mt
= this->type_
->map_type();
10925 Type
* key_type
= mt
->key_type();
10926 Type
* val_type
= mt
->val_type();
10927 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10928 pv
!= this->vals_
->end();
10931 if (!Type::are_assignable(key_type
, (*pv
)->type(), NULL
))
10933 error_at((*pv
)->location(),
10934 "incompatible type for element %d key in map construction",
10936 this->set_is_error();
10939 if (!Type::are_assignable(val_type
, (*pv
)->type(), NULL
))
10941 error_at((*pv
)->location(),
10942 ("incompatible type for element %d value "
10943 "in map construction"),
10945 this->set_is_error();
10950 // Return a tree for constructing a map.
10953 Map_construction_expression::do_get_tree(Translate_context
* context
)
10955 Gogo
* gogo
= context
->gogo();
10956 source_location loc
= this->location();
10958 Map_type
* mt
= this->type_
->map_type();
10960 // Build a struct to hold the key and value.
10961 tree struct_type
= make_node(RECORD_TYPE
);
10963 Type
* key_type
= mt
->key_type();
10964 tree id
= get_identifier("__key");
10965 tree key_field
= build_decl(loc
, FIELD_DECL
, id
, key_type
->get_tree(gogo
));
10966 DECL_CONTEXT(key_field
) = struct_type
;
10967 TYPE_FIELDS(struct_type
) = key_field
;
10969 Type
* val_type
= mt
->val_type();
10970 id
= get_identifier("__val");
10971 tree val_field
= build_decl(loc
, FIELD_DECL
, id
, val_type
->get_tree(gogo
));
10972 DECL_CONTEXT(val_field
) = struct_type
;
10973 DECL_CHAIN(key_field
) = val_field
;
10975 layout_type(struct_type
);
10977 bool is_constant
= true;
10982 if (this->vals_
== NULL
|| this->vals_
->empty())
10984 valaddr
= null_pointer_node
;
10985 make_tmp
= NULL_TREE
;
10989 VEC(constructor_elt
,gc
)* values
= VEC_alloc(constructor_elt
, gc
,
10990 this->vals_
->size() / 2);
10992 for (Expression_list::const_iterator pv
= this->vals_
->begin();
10993 pv
!= this->vals_
->end();
10996 bool one_is_constant
= true;
10998 VEC(constructor_elt
,gc
)* one
= VEC_alloc(constructor_elt
, gc
, 2);
11000 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11001 elt
->index
= key_field
;
11002 tree val_tree
= (*pv
)->get_tree(context
);
11003 elt
->value
= Expression::convert_for_assignment(context
, key_type
,
11006 if (elt
->value
== error_mark_node
)
11007 return error_mark_node
;
11008 if (!TREE_CONSTANT(elt
->value
))
11009 one_is_constant
= false;
11013 elt
= VEC_quick_push(constructor_elt
, one
, NULL
);
11014 elt
->index
= val_field
;
11015 val_tree
= (*pv
)->get_tree(context
);
11016 elt
->value
= Expression::convert_for_assignment(context
, val_type
,
11019 if (elt
->value
== error_mark_node
)
11020 return error_mark_node
;
11021 if (!TREE_CONSTANT(elt
->value
))
11022 one_is_constant
= false;
11024 elt
= VEC_quick_push(constructor_elt
, values
, NULL
);
11025 elt
->index
= size_int(i
);
11026 elt
->value
= build_constructor(struct_type
, one
);
11027 if (one_is_constant
)
11028 TREE_CONSTANT(elt
->value
) = 1;
11030 is_constant
= false;
11033 tree index_type
= build_index_type(size_int(i
- 1));
11034 tree array_type
= build_array_type(struct_type
, index_type
);
11035 tree init
= build_constructor(array_type
, values
);
11037 TREE_CONSTANT(init
) = 1;
11039 if (current_function_decl
!= NULL
)
11041 tmp
= create_tmp_var(array_type
, get_name(array_type
));
11042 DECL_INITIAL(tmp
) = init
;
11043 make_tmp
= fold_build1_loc(loc
, DECL_EXPR
, void_type_node
, tmp
);
11044 TREE_ADDRESSABLE(tmp
) = 1;
11048 tmp
= build_decl(loc
, VAR_DECL
, create_tmp_var_name("M"), array_type
);
11049 DECL_EXTERNAL(tmp
) = 0;
11050 TREE_PUBLIC(tmp
) = 0;
11051 TREE_STATIC(tmp
) = 1;
11052 DECL_ARTIFICIAL(tmp
) = 1;
11053 if (!TREE_CONSTANT(init
))
11054 make_tmp
= fold_build2_loc(loc
, INIT_EXPR
, void_type_node
, tmp
,
11058 TREE_READONLY(tmp
) = 1;
11059 TREE_CONSTANT(tmp
) = 1;
11060 DECL_INITIAL(tmp
) = init
;
11061 make_tmp
= NULL_TREE
;
11063 rest_of_decl_compilation(tmp
, 1, 0);
11066 valaddr
= build_fold_addr_expr(tmp
);
11069 tree descriptor
= gogo
->map_descriptor(mt
);
11071 tree type_tree
= this->type_
->get_tree(gogo
);
11073 static tree construct_map_fndecl
;
11074 tree call
= Gogo::call_builtin(&construct_map_fndecl
,
11076 "__go_construct_map",
11079 TREE_TYPE(descriptor
),
11084 TYPE_SIZE_UNIT(struct_type
),
11086 byte_position(val_field
),
11088 TYPE_SIZE_UNIT(TREE_TYPE(val_field
)),
11089 const_ptr_type_node
,
11090 fold_convert(const_ptr_type_node
, valaddr
));
11093 if (make_tmp
== NULL
)
11096 ret
= fold_build2_loc(loc
, COMPOUND_EXPR
, type_tree
, make_tmp
, call
);
11100 // Export an array construction.
11103 Map_construction_expression::do_export(Export
* exp
) const
11105 exp
->write_c_string("convert(");
11106 exp
->write_type(this->type_
);
11107 for (Expression_list::const_iterator pv
= this->vals_
->begin();
11108 pv
!= this->vals_
->end();
11111 exp
->write_c_string(", ");
11112 (*pv
)->export_expression(exp
);
11114 exp
->write_c_string(")");
11117 // A general composite literal. This is lowered to a type specific
11120 class Composite_literal_expression
: public Parser_expression
11123 Composite_literal_expression(Type
* type
, int depth
, bool has_keys
,
11124 Expression_list
* vals
, source_location location
)
11125 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL
, location
),
11126 type_(type
), depth_(depth
), vals_(vals
), has_keys_(has_keys
)
11131 do_traverse(Traverse
* traverse
);
11134 do_lower(Gogo
*, Named_object
*, int);
11139 return new Composite_literal_expression(this->type_
, this->depth_
,
11141 (this->vals_
== NULL
11143 : this->vals_
->copy()),
11149 lower_struct(Type
*);
11152 lower_array(Type
*);
11155 make_array(Type
*, Expression_list
*);
11160 // The type of the composite literal.
11162 // The depth within a list of composite literals within a composite
11163 // literal, when the type is omitted.
11165 // The values to put in the composite literal.
11166 Expression_list
* vals_
;
11167 // If this is true, then VALS_ is a list of pairs: a key and a
11168 // value. In an array initializer, a missing key will be NULL.
11175 Composite_literal_expression::do_traverse(Traverse
* traverse
)
11177 if (this->vals_
!= NULL
11178 && this->vals_
->traverse(traverse
) == TRAVERSE_EXIT
)
11179 return TRAVERSE_EXIT
;
11180 return Type::traverse(this->type_
, traverse
);
11183 // Lower a generic composite literal into a specific version based on
11187 Composite_literal_expression::do_lower(Gogo
*, Named_object
*, int)
11189 Type
* type
= this->type_
;
11191 for (int depth
= this->depth_
; depth
> 0; --depth
)
11193 if (type
->array_type() != NULL
)
11194 type
= type
->array_type()->element_type();
11195 else if (type
->map_type() != NULL
)
11196 type
= type
->map_type()->val_type();
11199 if (!type
->is_error_type())
11200 error_at(this->location(),
11201 ("may only omit types within composite literals "
11202 "of slice, array, or map type"));
11203 return Expression::make_error(this->location());
11207 if (type
->is_error_type())
11208 return Expression::make_error(this->location());
11209 else if (type
->struct_type() != NULL
)
11210 return this->lower_struct(type
);
11211 else if (type
->array_type() != NULL
)
11212 return this->lower_array(type
);
11213 else if (type
->map_type() != NULL
)
11214 return this->lower_map(type
);
11217 error_at(this->location(),
11218 ("expected struct, slice, array, or map type "
11219 "for composite literal"));
11220 return Expression::make_error(this->location());
11224 // Lower a struct composite literal.
11227 Composite_literal_expression::lower_struct(Type
* type
)
11229 source_location location
= this->location();
11230 Struct_type
* st
= type
->struct_type();
11231 if (this->vals_
== NULL
|| !this->has_keys_
)
11232 return new Struct_construction_expression(type
, this->vals_
, location
);
11234 size_t field_count
= st
->field_count();
11235 std::vector
<Expression
*> vals(field_count
);
11236 Expression_list::const_iterator p
= this->vals_
->begin();
11237 while (p
!= this->vals_
->end())
11239 Expression
* name_expr
= *p
;
11242 gcc_assert(p
!= this->vals_
->end());
11243 Expression
* val
= *p
;
11247 if (name_expr
== NULL
)
11249 error_at(val
->location(), "mixture of field and value initializers");
11250 return Expression::make_error(location
);
11253 bool bad_key
= false;
11255 switch (name_expr
->classification())
11257 case EXPRESSION_UNKNOWN_REFERENCE
:
11258 name
= name_expr
->unknown_expression()->name();
11261 case EXPRESSION_CONST_REFERENCE
:
11262 name
= static_cast<Const_expression
*>(name_expr
)->name();
11265 case EXPRESSION_TYPE
:
11267 Type
* t
= name_expr
->type();
11268 Named_type
* nt
= t
->named_type();
11276 case EXPRESSION_VAR_REFERENCE
:
11277 name
= name_expr
->var_expression()->name();
11280 case EXPRESSION_FUNC_REFERENCE
:
11281 name
= name_expr
->func_expression()->name();
11284 case EXPRESSION_UNARY
:
11285 // If there is a local variable around with the same name as
11286 // the field, and this occurs in the closure, then the
11287 // parser may turn the field reference into an indirection
11288 // through the closure. FIXME: This is a mess.
11291 Unary_expression
* ue
= static_cast<Unary_expression
*>(name_expr
);
11292 if (ue
->op() == OPERATOR_MULT
)
11294 Field_reference_expression
* fre
=
11295 ue
->operand()->field_reference_expression();
11299 fre
->expr()->type()->deref()->struct_type();
11302 const Struct_field
* sf
= st
->field(fre
->field_index());
11303 name
= sf
->field_name();
11305 snprintf(buf
, sizeof buf
, "%u", fre
->field_index());
11306 size_t buflen
= strlen(buf
);
11307 if (name
.compare(name
.length() - buflen
, buflen
, buf
)
11310 name
= name
.substr(0, name
.length() - buflen
);
11325 error_at(name_expr
->location(), "expected struct field name");
11326 return Expression::make_error(location
);
11329 unsigned int index
;
11330 const Struct_field
* sf
= st
->find_local_field(name
, &index
);
11333 error_at(name_expr
->location(), "unknown field %qs in %qs",
11334 Gogo::message_name(name
).c_str(),
11335 (type
->named_type() != NULL
11336 ? type
->named_type()->message_name().c_str()
11337 : "unnamed struct"));
11338 return Expression::make_error(location
);
11340 if (vals
[index
] != NULL
)
11342 error_at(name_expr
->location(),
11343 "duplicate value for field %qs in %qs",
11344 Gogo::message_name(name
).c_str(),
11345 (type
->named_type() != NULL
11346 ? type
->named_type()->message_name().c_str()
11347 : "unnamed struct"));
11348 return Expression::make_error(location
);
11354 Expression_list
* list
= new Expression_list
;
11355 list
->reserve(field_count
);
11356 for (size_t i
= 0; i
< field_count
; ++i
)
11357 list
->push_back(vals
[i
]);
11359 return new Struct_construction_expression(type
, list
, location
);
11362 // Lower an array composite literal.
11365 Composite_literal_expression::lower_array(Type
* type
)
11367 source_location location
= this->location();
11368 if (this->vals_
== NULL
|| !this->has_keys_
)
11369 return this->make_array(type
, this->vals_
);
11371 std::vector
<Expression
*> vals
;
11372 vals
.reserve(this->vals_
->size());
11373 unsigned long index
= 0;
11374 Expression_list::const_iterator p
= this->vals_
->begin();
11375 while (p
!= this->vals_
->end())
11377 Expression
* index_expr
= *p
;
11380 gcc_assert(p
!= this->vals_
->end());
11381 Expression
* val
= *p
;
11385 if (index_expr
!= NULL
)
11390 if (!index_expr
->integer_constant_value(true, ival
, &dummy
))
11393 error_at(index_expr
->location(),
11394 "index expression is not integer constant");
11395 return Expression::make_error(location
);
11397 if (mpz_sgn(ival
) < 0)
11400 error_at(index_expr
->location(), "index expression is negative");
11401 return Expression::make_error(location
);
11403 index
= mpz_get_ui(ival
);
11404 if (mpz_cmp_ui(ival
, index
) != 0)
11407 error_at(index_expr
->location(), "index value overflow");
11408 return Expression::make_error(location
);
11413 if (index
== vals
.size())
11414 vals
.push_back(val
);
11417 if (index
> vals
.size())
11419 vals
.reserve(index
+ 32);
11420 vals
.resize(index
+ 1, static_cast<Expression
*>(NULL
));
11422 if (vals
[index
] != NULL
)
11424 error_at((index_expr
!= NULL
11425 ? index_expr
->location()
11426 : val
->location()),
11427 "duplicate value for index %lu",
11429 return Expression::make_error(location
);
11437 size_t size
= vals
.size();
11438 Expression_list
* list
= new Expression_list
;
11439 list
->reserve(size
);
11440 for (size_t i
= 0; i
< size
; ++i
)
11441 list
->push_back(vals
[i
]);
11443 return this->make_array(type
, list
);
11446 // Actually build the array composite literal. This handles
11450 Composite_literal_expression::make_array(Type
* type
, Expression_list
* vals
)
11452 source_location location
= this->location();
11453 Array_type
* at
= type
->array_type();
11454 if (at
->length() != NULL
&& at
->length()->is_nil_expression())
11456 size_t size
= vals
== NULL
? 0 : vals
->size();
11458 mpz_init_set_ui(vlen
, size
);
11459 Expression
* elen
= Expression::make_integer(&vlen
, NULL
, location
);
11461 at
= Type::make_array_type(at
->element_type(), elen
);
11464 if (at
->length() != NULL
)
11465 return new Fixed_array_construction_expression(type
, vals
, location
);
11467 return new Open_array_construction_expression(type
, vals
, location
);
11470 // Lower a map composite literal.
11473 Composite_literal_expression::lower_map(Type
* type
)
11475 source_location location
= this->location();
11476 if (this->vals_
!= NULL
)
11478 if (!this->has_keys_
)
11480 error_at(location
, "map composite literal must have keys");
11481 return Expression::make_error(location
);
11484 for (Expression_list::const_iterator p
= this->vals_
->begin();
11485 p
!= this->vals_
->end();
11491 error_at((*p
)->location(),
11492 "map composite literal must have keys for every value");
11493 return Expression::make_error(location
);
11498 return new Map_construction_expression(type
, this->vals_
, location
);
11501 // Make a composite literal expression.
11504 Expression::make_composite_literal(Type
* type
, int depth
, bool has_keys
,
11505 Expression_list
* vals
,
11506 source_location location
)
11508 return new Composite_literal_expression(type
, depth
, has_keys
, vals
,
11512 // Return whether this expression is a composite literal.
11515 Expression::is_composite_literal() const
11517 switch (this->classification_
)
11519 case EXPRESSION_COMPOSITE_LITERAL
:
11520 case EXPRESSION_STRUCT_CONSTRUCTION
:
11521 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11522 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11523 case EXPRESSION_MAP_CONSTRUCTION
:
11530 // Return whether this expression is a composite literal which is not
11534 Expression::is_nonconstant_composite_literal() const
11536 switch (this->classification_
)
11538 case EXPRESSION_STRUCT_CONSTRUCTION
:
11540 const Struct_construction_expression
*psce
=
11541 static_cast<const Struct_construction_expression
*>(this);
11542 return !psce
->is_constant_struct();
11544 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION
:
11546 const Fixed_array_construction_expression
*pace
=
11547 static_cast<const Fixed_array_construction_expression
*>(this);
11548 return !pace
->is_constant_array();
11550 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION
:
11552 const Open_array_construction_expression
*pace
=
11553 static_cast<const Open_array_construction_expression
*>(this);
11554 return !pace
->is_constant_array();
11556 case EXPRESSION_MAP_CONSTRUCTION
:
11563 // Return true if this is a reference to a local variable.
11566 Expression::is_local_variable() const
11568 const Var_expression
* ve
= this->var_expression();
11571 const Named_object
* no
= ve
->named_object();
11572 return (no
->is_result_variable()
11573 || (no
->is_variable() && !no
->var_value()->is_global()));
11576 // Class Type_guard_expression.
11581 Type_guard_expression::do_traverse(Traverse
* traverse
)
11583 if (Expression::traverse(&this->expr_
, traverse
) == TRAVERSE_EXIT
11584 || Type::traverse(this->type_
, traverse
) == TRAVERSE_EXIT
)
11585 return TRAVERSE_EXIT
;
11586 return TRAVERSE_CONTINUE
;
11589 // Check types of a type guard expression. The expression must have
11590 // an interface type, but the actual type conversion is checked at run
11594 Type_guard_expression::do_check_types(Gogo
*)
11596 // 6g permits using a type guard with unsafe.pointer; we are
11598 Type
* expr_type
= this->expr_
->type();
11599 if (expr_type
->is_unsafe_pointer_type())
11601 if (this->type_
->points_to() == NULL
11602 && (this->type_
->integer_type() == NULL
11603 || (this->type_
->forwarded()
11604 != Type::lookup_integer_type("uintptr"))))
11605 this->report_error(_("invalid unsafe.Pointer conversion"));
11607 else if (this->type_
->is_unsafe_pointer_type())
11609 if (expr_type
->points_to() == NULL
11610 && (expr_type
->integer_type() == NULL
11611 || (expr_type
->forwarded()
11612 != Type::lookup_integer_type("uintptr"))))
11613 this->report_error(_("invalid unsafe.Pointer conversion"));
11615 else if (expr_type
->interface_type() == NULL
)
11616 this->report_error(_("type assertion only valid for interface types"));
11617 else if (this->type_
->interface_type() == NULL
)
11619 std::string reason
;
11620 if (!expr_type
->interface_type()->implements_interface(this->type_
,
11623 if (reason
.empty())
11624 this->report_error(_("impossible type assertion: "
11625 "type does not implement interface"));
11628 error_at(this->location(),
11629 ("impossible type assertion: "
11630 "type does not implement interface (%s)"),
11632 this->set_is_error();
11638 // Return a tree for a type guard expression.
11641 Type_guard_expression::do_get_tree(Translate_context
* context
)
11643 Gogo
* gogo
= context
->gogo();
11644 tree expr_tree
= this->expr_
->get_tree(context
);
11645 if (expr_tree
== error_mark_node
)
11646 return error_mark_node
;
11647 Type
* expr_type
= this->expr_
->type();
11648 if ((this->type_
->is_unsafe_pointer_type()
11649 && (expr_type
->points_to() != NULL
11650 || expr_type
->integer_type() != NULL
))
11651 || (expr_type
->is_unsafe_pointer_type()
11652 && this->type_
->points_to() != NULL
))
11653 return convert_to_pointer(this->type_
->get_tree(gogo
), expr_tree
);
11654 else if (expr_type
->is_unsafe_pointer_type()
11655 && this->type_
->integer_type() != NULL
)
11656 return convert_to_integer(this->type_
->get_tree(gogo
), expr_tree
);
11657 else if (this->type_
->interface_type() != NULL
)
11658 return Expression::convert_interface_to_interface(context
, this->type_
,
11659 this->expr_
->type(),
11663 return Expression::convert_for_assignment(context
, this->type_
,
11664 this->expr_
->type(), expr_tree
,
11668 // Make a type guard expression.
11671 Expression::make_type_guard(Expression
* expr
, Type
* type
,
11672 source_location location
)
11674 return new Type_guard_expression(expr
, type
, location
);
11677 // Class Heap_composite_expression.
11679 // When you take the address of a composite literal, it is allocated
11680 // on the heap. This class implements that.
11682 class Heap_composite_expression
: public Expression
11685 Heap_composite_expression(Expression
* expr
, source_location location
)
11686 : Expression(EXPRESSION_HEAP_COMPOSITE
, location
),
11692 do_traverse(Traverse
* traverse
)
11693 { return Expression::traverse(&this->expr_
, traverse
); }
11697 { return Type::make_pointer_type(this->expr_
->type()); }
11700 do_determine_type(const Type_context
*)
11701 { this->expr_
->determine_type_no_context(); }
11706 return Expression::make_heap_composite(this->expr_
->copy(),
11711 do_get_tree(Translate_context
*);
11713 // We only export global objects, and the parser does not generate
11714 // this in global scope.
11716 do_export(Export
*) const
11717 { gcc_unreachable(); }
11720 // The composite literal which is being put on the heap.
11724 // Return a tree which allocates a composite literal on the heap.
11727 Heap_composite_expression::do_get_tree(Translate_context
* context
)
11729 tree expr_tree
= this->expr_
->get_tree(context
);
11730 if (expr_tree
== error_mark_node
)
11731 return error_mark_node
;
11732 tree expr_size
= TYPE_SIZE_UNIT(TREE_TYPE(expr_tree
));
11733 gcc_assert(TREE_CODE(expr_size
) == INTEGER_CST
);
11734 tree space
= context
->gogo()->allocate_memory(this->expr_
->type(),
11735 expr_size
, this->location());
11736 space
= fold_convert(build_pointer_type(TREE_TYPE(expr_tree
)), space
);
11737 space
= save_expr(space
);
11738 tree ref
= build_fold_indirect_ref_loc(this->location(), space
);
11739 TREE_THIS_NOTRAP(ref
) = 1;
11740 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
11741 build2(MODIFY_EXPR
, void_type_node
, ref
, expr_tree
),
11743 SET_EXPR_LOCATION(ret
, this->location());
11747 // Allocate a composite literal on the heap.
11750 Expression::make_heap_composite(Expression
* expr
, source_location location
)
11752 return new Heap_composite_expression(expr
, location
);
11755 // Class Receive_expression.
11757 // Return the type of a receive expression.
11760 Receive_expression::do_type()
11762 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
11763 if (channel_type
== NULL
)
11764 return Type::make_error_type();
11765 return channel_type
->element_type();
11768 // Check types for a receive expression.
11771 Receive_expression::do_check_types(Gogo
*)
11773 Type
* type
= this->channel_
->type();
11774 if (type
->is_error_type())
11776 this->set_is_error();
11779 if (type
->channel_type() == NULL
)
11781 this->report_error(_("expected channel"));
11784 if (!type
->channel_type()->may_receive())
11786 this->report_error(_("invalid receive on send-only channel"));
11791 // Get a tree for a receive expression.
11794 Receive_expression::do_get_tree(Translate_context
* context
)
11796 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
11797 gcc_assert(channel_type
!= NULL
);
11798 Type
* element_type
= channel_type
->element_type();
11799 tree element_type_tree
= element_type
->get_tree(context
->gogo());
11801 tree channel
= this->channel_
->get_tree(context
);
11802 if (element_type_tree
== error_mark_node
|| channel
== error_mark_node
)
11803 return error_mark_node
;
11805 return Gogo::receive_from_channel(element_type_tree
, channel
,
11806 this->for_select_
, this->location());
11809 // Make a receive expression.
11811 Receive_expression
*
11812 Expression::make_receive(Expression
* channel
, source_location location
)
11814 return new Receive_expression(channel
, location
);
11817 // Class Send_expression.
11822 Send_expression::do_traverse(Traverse
* traverse
)
11824 if (Expression::traverse(&this->channel_
, traverse
) == TRAVERSE_EXIT
)
11825 return TRAVERSE_EXIT
;
11826 return Expression::traverse(&this->val_
, traverse
);
11832 Send_expression::do_type()
11834 return Type::lookup_bool_type();
11840 Send_expression::do_determine_type(const Type_context
*)
11842 this->channel_
->determine_type_no_context();
11844 Type
* type
= this->channel_
->type();
11845 Type_context subcontext
;
11846 if (type
->channel_type() != NULL
)
11847 subcontext
.type
= type
->channel_type()->element_type();
11848 this->val_
->determine_type(&subcontext
);
11854 Send_expression::do_check_types(Gogo
*)
11856 Type
* type
= this->channel_
->type();
11857 if (type
->is_error_type())
11859 this->set_is_error();
11862 Channel_type
* channel_type
= type
->channel_type();
11863 if (channel_type
== NULL
)
11865 error_at(this->location(), "left operand of %<<-%> must be channel");
11866 this->set_is_error();
11869 Type
* element_type
= channel_type
->element_type();
11870 if (element_type
!= NULL
11871 && !Type::are_assignable(element_type
, this->val_
->type(), NULL
))
11873 this->report_error(_("incompatible types in send"));
11876 if (!channel_type
->may_send())
11878 this->report_error(_("invalid send on receive-only channel"));
11883 // Get a tree for a send expression.
11886 Send_expression::do_get_tree(Translate_context
* context
)
11888 tree channel
= this->channel_
->get_tree(context
);
11889 tree val
= this->val_
->get_tree(context
);
11890 if (channel
== error_mark_node
|| val
== error_mark_node
)
11891 return error_mark_node
;
11892 Channel_type
* channel_type
= this->channel_
->type()->channel_type();
11893 val
= Expression::convert_for_assignment(context
,
11894 channel_type
->element_type(),
11895 this->val_
->type(),
11898 return Gogo::send_on_channel(channel
, val
, this->is_value_discarded_
,
11899 this->for_select_
, this->location());
11902 // Make a send expression
11905 Expression::make_send(Expression
* channel
, Expression
* val
,
11906 source_location location
)
11908 return new Send_expression(channel
, val
, location
);
11911 // An expression which evaluates to a pointer to the type descriptor
11914 class Type_descriptor_expression
: public Expression
11917 Type_descriptor_expression(Type
* type
, source_location location
)
11918 : Expression(EXPRESSION_TYPE_DESCRIPTOR
, location
),
11925 { return Type::make_type_descriptor_ptr_type(); }
11928 do_determine_type(const Type_context
*)
11936 do_get_tree(Translate_context
* context
)
11937 { return this->type_
->type_descriptor_pointer(context
->gogo()); }
11940 // The type for which this is the descriptor.
11944 // Make a type descriptor expression.
11947 Expression::make_type_descriptor(Type
* type
, source_location location
)
11949 return new Type_descriptor_expression(type
, location
);
11952 // An expression which evaluates to some characteristic of a type.
11953 // This is only used to initialize fields of a type descriptor. Using
11954 // a new expression class is slightly inefficient but gives us a good
11955 // separation between the frontend and the middle-end with regard to
11956 // how types are laid out.
11958 class Type_info_expression
: public Expression
11961 Type_info_expression(Type
* type
, Type_info type_info
)
11962 : Expression(EXPRESSION_TYPE_INFO
, BUILTINS_LOCATION
),
11963 type_(type
), type_info_(type_info
)
11971 do_determine_type(const Type_context
*)
11979 do_get_tree(Translate_context
* context
);
11982 // The type for which we are getting information.
11984 // What information we want.
11985 Type_info type_info_
;
11988 // The type is chosen to match what the type descriptor struct
11992 Type_info_expression::do_type()
11994 switch (this->type_info_
)
11996 case TYPE_INFO_SIZE
:
11997 return Type::lookup_integer_type("uintptr");
11998 case TYPE_INFO_ALIGNMENT
:
11999 case TYPE_INFO_FIELD_ALIGNMENT
:
12000 return Type::lookup_integer_type("uint8");
12006 // Return type information in GENERIC.
12009 Type_info_expression::do_get_tree(Translate_context
* context
)
12011 tree type_tree
= this->type_
->get_tree(context
->gogo());
12012 if (type_tree
== error_mark_node
)
12013 return error_mark_node
;
12015 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12016 gcc_assert(val_type_tree
!= error_mark_node
);
12018 if (this->type_info_
== TYPE_INFO_SIZE
)
12019 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12020 TYPE_SIZE_UNIT(type_tree
));
12024 if (this->type_info_
== TYPE_INFO_ALIGNMENT
)
12025 val
= go_type_alignment(type_tree
);
12027 val
= go_field_alignment(type_tree
);
12028 return build_int_cstu(val_type_tree
, val
);
12032 // Make a type info expression.
12035 Expression::make_type_info(Type
* type
, Type_info type_info
)
12037 return new Type_info_expression(type
, type_info
);
12040 // An expression which evaluates to the offset of a field within a
12041 // struct. This, like Type_info_expression, q.v., is only used to
12042 // initialize fields of a type descriptor.
12044 class Struct_field_offset_expression
: public Expression
12047 Struct_field_offset_expression(Struct_type
* type
, const Struct_field
* field
)
12048 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET
, BUILTINS_LOCATION
),
12049 type_(type
), field_(field
)
12055 { return Type::lookup_integer_type("uintptr"); }
12058 do_determine_type(const Type_context
*)
12066 do_get_tree(Translate_context
* context
);
12069 // The type of the struct.
12070 Struct_type
* type_
;
12072 const Struct_field
* field_
;
12075 // Return a struct field offset in GENERIC.
12078 Struct_field_offset_expression::do_get_tree(Translate_context
* context
)
12080 tree type_tree
= this->type_
->get_tree(context
->gogo());
12081 if (type_tree
== error_mark_node
)
12082 return error_mark_node
;
12084 tree val_type_tree
= this->type()->get_tree(context
->gogo());
12085 gcc_assert(val_type_tree
!= error_mark_node
);
12087 const Struct_field_list
* fields
= this->type_
->fields();
12088 tree struct_field_tree
= TYPE_FIELDS(type_tree
);
12089 Struct_field_list::const_iterator p
;
12090 for (p
= fields
->begin();
12091 p
!= fields
->end();
12092 ++p
, struct_field_tree
= DECL_CHAIN(struct_field_tree
))
12094 gcc_assert(struct_field_tree
!= NULL_TREE
);
12095 if (&*p
== this->field_
)
12098 gcc_assert(&*p
== this->field_
);
12100 return fold_convert_loc(BUILTINS_LOCATION
, val_type_tree
,
12101 byte_position(struct_field_tree
));
12104 // Make an expression for a struct field offset.
12107 Expression::make_struct_field_offset(Struct_type
* type
,
12108 const Struct_field
* field
)
12110 return new Struct_field_offset_expression(type
, field
);
12113 // An expression which evaluates to the address of an unnamed label.
12115 class Label_addr_expression
: public Expression
12118 Label_addr_expression(Label
* label
, source_location location
)
12119 : Expression(EXPRESSION_LABEL_ADDR
, location
),
12126 { return Type::make_pointer_type(Type::make_void_type()); }
12129 do_determine_type(const Type_context
*)
12134 { return new Label_addr_expression(this->label_
, this->location()); }
12137 do_get_tree(Translate_context
*)
12138 { return this->label_
->get_addr(this->location()); }
12141 // The label whose address we are taking.
12145 // Make an expression for the address of an unnamed label.
12148 Expression::make_label_addr(Label
* label
, source_location location
)
12150 return new Label_addr_expression(label
, location
);
12153 // Import an expression. This comes at the end in order to see the
12154 // various class definitions.
12157 Expression::import_expression(Import
* imp
)
12159 int c
= imp
->peek_char();
12160 if (imp
->match_c_string("- ")
12161 || imp
->match_c_string("! ")
12162 || imp
->match_c_string("^ "))
12163 return Unary_expression::do_import(imp
);
12165 return Binary_expression::do_import(imp
);
12166 else if (imp
->match_c_string("true")
12167 || imp
->match_c_string("false"))
12168 return Boolean_expression::do_import(imp
);
12170 return String_expression::do_import(imp
);
12171 else if (c
== '-' || (c
>= '0' && c
<= '9'))
12173 // This handles integers, floats and complex constants.
12174 return Integer_expression::do_import(imp
);
12176 else if (imp
->match_c_string("nil"))
12177 return Nil_expression::do_import(imp
);
12178 else if (imp
->match_c_string("convert"))
12179 return Type_conversion_expression::do_import(imp
);
12182 error_at(imp
->location(), "import error: expected expression");
12183 return Expression::make_error(imp
->location());
12187 // Class Expression_list.
12189 // Traverse the list.
12192 Expression_list::traverse(Traverse
* traverse
)
12194 for (Expression_list::iterator p
= this->begin();
12200 if (Expression::traverse(&*p
, traverse
) == TRAVERSE_EXIT
)
12201 return TRAVERSE_EXIT
;
12204 return TRAVERSE_CONTINUE
;
12210 Expression_list::copy()
12212 Expression_list
* ret
= new Expression_list();
12213 for (Expression_list::iterator p
= this->begin();
12218 ret
->push_back(NULL
);
12220 ret
->push_back((*p
)->copy());
12225 // Return whether an expression list has an error expression.
12228 Expression_list::contains_error() const
12230 for (Expression_list::const_iterator p
= this->begin();
12233 if (*p
!= NULL
&& (*p
)->is_error_expression())