Use mpfr_prec_round, not real_convert, to constraint floats.
[official-gcc.git] / gcc / go / gofrontend / expressions.cc
blob8a1fc349b95759bde7faaefeb3345c6c3d0f0ef3
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.
7 #include "go-system.h"
9 #include <gmp.h>
11 #ifndef ENABLE_BUILD_WITH_CXX
12 extern "C"
14 #endif
16 #include "toplev.h"
17 #include "intl.h"
18 #include "tree.h"
19 #include "gimple.h"
20 #include "tree-iterator.h"
21 #include "convert.h"
22 #include "real.h"
23 #include "realmpfr.h"
25 #ifndef ENABLE_BUILD_WITH_CXX
27 #endif
29 #include "go-c.h"
30 #include "gogo.h"
31 #include "types.h"
32 #include "export.h"
33 #include "import.h"
34 #include "statements.h"
35 #include "lex.h"
36 #include "backend.h"
37 #include "expressions.h"
39 // Class Expression.
41 Expression::Expression(Expression_classification classification,
42 source_location location)
43 : classification_(classification), location_(location)
47 Expression::~Expression()
51 // If this expression has a constant integer value, return it.
53 bool
54 Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
55 Type** ptype) const
57 *ptype = NULL;
58 return this->do_integer_constant_value(iota_is_constant, val, ptype);
61 // If this expression has a constant floating point value, return it.
63 bool
64 Expression::float_constant_value(mpfr_t val, Type** ptype) const
66 *ptype = NULL;
67 if (this->do_float_constant_value(val, ptype))
68 return true;
69 mpz_t ival;
70 mpz_init(ival);
71 Type* t;
72 bool ret;
73 if (!this->do_integer_constant_value(false, ival, &t))
74 ret = false;
75 else
77 mpfr_set_z(val, ival, GMP_RNDN);
78 ret = true;
80 mpz_clear(ival);
81 return ret;
84 // If this expression has a constant complex value, return it.
86 bool
87 Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
88 Type** ptype) const
90 *ptype = NULL;
91 if (this->do_complex_constant_value(real, imag, ptype))
92 return true;
93 Type *t;
94 if (this->float_constant_value(real, &t))
96 mpfr_set_ui(imag, 0, GMP_RNDN);
97 return true;
99 return false;
102 // Traverse the expressions.
105 Expression::traverse(Expression** pexpr, Traverse* traverse)
107 Expression* expr = *pexpr;
108 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
110 int t = traverse->expression(pexpr);
111 if (t == TRAVERSE_EXIT)
112 return TRAVERSE_EXIT;
113 else if (t == TRAVERSE_SKIP_COMPONENTS)
114 return TRAVERSE_CONTINUE;
116 return expr->do_traverse(traverse);
119 // Traverse subexpressions of this expression.
122 Expression::traverse_subexpressions(Traverse* traverse)
124 return this->do_traverse(traverse);
127 // Default implementation for do_traverse for child classes.
130 Expression::do_traverse(Traverse*)
132 return TRAVERSE_CONTINUE;
135 // This virtual function is called by the parser if the value of this
136 // expression is being discarded. By default, we warn. Expressions
137 // with side effects override.
139 void
140 Expression::do_discarding_value()
142 this->warn_about_unused_value();
145 // This virtual function is called to export expressions. This will
146 // only be used by expressions which may be constant.
148 void
149 Expression::do_export(Export*) const
151 gcc_unreachable();
154 // Warn that the value of the expression is not used.
156 void
157 Expression::warn_about_unused_value()
159 warning_at(this->location(), OPT_Wunused_value, "value computed is not used");
162 // Note that this expression is an error. This is called by children
163 // when they discover an error.
165 void
166 Expression::set_is_error()
168 this->classification_ = EXPRESSION_ERROR;
171 // For children to call to report an error conveniently.
173 void
174 Expression::report_error(const char* msg)
176 error_at(this->location_, "%s", msg);
177 this->set_is_error();
180 // Set types of variables and constants. This is implemented by the
181 // child class.
183 void
184 Expression::determine_type(const Type_context* context)
186 this->do_determine_type(context);
189 // Set types when there is no context.
191 void
192 Expression::determine_type_no_context()
194 Type_context context;
195 this->do_determine_type(&context);
198 // Return a tree handling any conversions which must be done during
199 // assignment.
201 tree
202 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
203 Type* rhs_type, tree rhs_tree,
204 source_location location)
206 if (lhs_type == rhs_type)
207 return rhs_tree;
209 if (lhs_type->is_error() || rhs_type->is_error())
210 return error_mark_node;
212 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
213 return error_mark_node;
215 Gogo* gogo = context->gogo();
217 tree lhs_type_tree = lhs_type->get_tree(gogo);
218 if (lhs_type_tree == error_mark_node)
219 return error_mark_node;
221 if (lhs_type->interface_type() != NULL)
223 if (rhs_type->interface_type() == NULL)
224 return Expression::convert_type_to_interface(context, lhs_type,
225 rhs_type, rhs_tree,
226 location);
227 else
228 return Expression::convert_interface_to_interface(context, lhs_type,
229 rhs_type, rhs_tree,
230 false, location);
232 else if (rhs_type->interface_type() != NULL)
233 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
234 rhs_tree, location);
235 else if (lhs_type->is_open_array_type()
236 && rhs_type->is_nil_type())
238 // Assigning nil to an open array.
239 gcc_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
241 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
243 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
244 tree field = TYPE_FIELDS(lhs_type_tree);
245 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
246 "__values") == 0);
247 elt->index = field;
248 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
250 elt = VEC_quick_push(constructor_elt, init, NULL);
251 field = DECL_CHAIN(field);
252 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
253 "__count") == 0);
254 elt->index = field;
255 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
257 elt = VEC_quick_push(constructor_elt, init, NULL);
258 field = DECL_CHAIN(field);
259 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
260 "__capacity") == 0);
261 elt->index = field;
262 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
264 tree val = build_constructor(lhs_type_tree, init);
265 TREE_CONSTANT(val) = 1;
267 return val;
269 else if (rhs_type->is_nil_type())
271 // The left hand side should be a pointer type at the tree
272 // level.
273 gcc_assert(POINTER_TYPE_P(lhs_type_tree));
274 return fold_convert(lhs_type_tree, null_pointer_node);
276 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
278 // No conversion is needed.
279 return rhs_tree;
281 else if (POINTER_TYPE_P(lhs_type_tree)
282 || INTEGRAL_TYPE_P(lhs_type_tree)
283 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
284 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
285 return fold_convert_loc(location, lhs_type_tree, rhs_tree);
286 else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
287 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
289 // This conversion must be permitted by Go, or we wouldn't have
290 // gotten here.
291 gcc_assert(int_size_in_bytes(lhs_type_tree)
292 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
293 return fold_build1_loc(location, VIEW_CONVERT_EXPR, lhs_type_tree,
294 rhs_tree);
296 else
298 gcc_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
299 return rhs_tree;
303 // Return a tree for a conversion from a non-interface type to an
304 // interface type.
306 tree
307 Expression::convert_type_to_interface(Translate_context* context,
308 Type* lhs_type, Type* rhs_type,
309 tree rhs_tree, source_location location)
311 Gogo* gogo = context->gogo();
312 Interface_type* lhs_interface_type = lhs_type->interface_type();
313 bool lhs_is_empty = lhs_interface_type->is_empty();
315 // Since RHS_TYPE is a static type, we can create the interface
316 // method table at compile time.
318 // When setting an interface to nil, we just set both fields to
319 // NULL.
320 if (rhs_type->is_nil_type())
321 return lhs_type->get_init_tree(gogo, false);
323 // This should have been checked already.
324 gcc_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
326 tree lhs_type_tree = lhs_type->get_tree(gogo);
327 if (lhs_type_tree == error_mark_node)
328 return error_mark_node;
330 // An interface is a tuple. If LHS_TYPE is an empty interface type,
331 // then the first field is the type descriptor for RHS_TYPE.
332 // Otherwise it is the interface method table for RHS_TYPE.
333 tree first_field_value;
334 if (lhs_is_empty)
335 first_field_value = rhs_type->type_descriptor_pointer(gogo);
336 else
338 // Build the interface method table for this interface and this
339 // object type: a list of function pointers for each interface
340 // method.
341 Named_type* rhs_named_type = rhs_type->named_type();
342 bool is_pointer = false;
343 if (rhs_named_type == NULL)
345 rhs_named_type = rhs_type->deref()->named_type();
346 is_pointer = true;
348 tree method_table;
349 if (rhs_named_type == NULL)
350 method_table = null_pointer_node;
351 else
352 method_table =
353 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
354 is_pointer);
355 first_field_value = fold_convert_loc(location, const_ptr_type_node,
356 method_table);
358 if (first_field_value == error_mark_node)
359 return error_mark_node;
361 // Start building a constructor for the value we will return.
363 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
365 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
366 tree field = TYPE_FIELDS(lhs_type_tree);
367 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
368 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
369 elt->index = field;
370 elt->value = fold_convert_loc(location, TREE_TYPE(field), first_field_value);
372 elt = VEC_quick_push(constructor_elt, init, NULL);
373 field = DECL_CHAIN(field);
374 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
375 elt->index = field;
377 if (rhs_type->points_to() != NULL)
379 // We are assigning a pointer to the interface; the interface
380 // holds the pointer itself.
381 elt->value = rhs_tree;
382 return build_constructor(lhs_type_tree, init);
385 // We are assigning a non-pointer value to the interface; the
386 // interface gets a copy of the value in the heap.
388 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
390 tree space = gogo->allocate_memory(rhs_type, object_size, location);
391 space = fold_convert_loc(location, build_pointer_type(TREE_TYPE(rhs_tree)),
392 space);
393 space = save_expr(space);
395 tree ref = build_fold_indirect_ref_loc(location, space);
396 TREE_THIS_NOTRAP(ref) = 1;
397 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
398 ref, rhs_tree);
400 elt->value = fold_convert_loc(location, TREE_TYPE(field), space);
402 return build2(COMPOUND_EXPR, lhs_type_tree, set,
403 build_constructor(lhs_type_tree, init));
406 // Return a tree for the type descriptor of RHS_TREE, which has
407 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
408 // NULL.
410 tree
411 Expression::get_interface_type_descriptor(Translate_context*,
412 Type* rhs_type, tree rhs_tree,
413 source_location location)
415 tree rhs_type_tree = TREE_TYPE(rhs_tree);
416 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
417 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
418 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
419 NULL_TREE);
420 if (rhs_type->interface_type()->is_empty())
422 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
423 "__type_descriptor") == 0);
424 return v;
427 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
428 == 0);
429 gcc_assert(POINTER_TYPE_P(TREE_TYPE(v)));
430 v = save_expr(v);
431 tree v1 = build_fold_indirect_ref_loc(location, v);
432 gcc_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
433 tree f = TYPE_FIELDS(TREE_TYPE(v1));
434 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
435 == 0);
436 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
438 tree eq = fold_build2_loc(location, EQ_EXPR, boolean_type_node, v,
439 fold_convert_loc(location, TREE_TYPE(v),
440 null_pointer_node));
441 tree n = fold_convert_loc(location, TREE_TYPE(v1), null_pointer_node);
442 return fold_build3_loc(location, COND_EXPR, TREE_TYPE(v1),
443 eq, n, v1);
446 // Return a tree for the conversion of an interface type to an
447 // interface type.
449 tree
450 Expression::convert_interface_to_interface(Translate_context* context,
451 Type *lhs_type, Type *rhs_type,
452 tree rhs_tree, bool for_type_guard,
453 source_location location)
455 Gogo* gogo = context->gogo();
456 Interface_type* lhs_interface_type = lhs_type->interface_type();
457 bool lhs_is_empty = lhs_interface_type->is_empty();
459 tree lhs_type_tree = lhs_type->get_tree(gogo);
460 if (lhs_type_tree == error_mark_node)
461 return error_mark_node;
463 // In the general case this requires runtime examination of the type
464 // method table to match it up with the interface methods.
466 // FIXME: If all of the methods in the right hand side interface
467 // also appear in the left hand side interface, then we don't need
468 // to do a runtime check, although we still need to build a new
469 // method table.
471 // Get the type descriptor for the right hand side. This will be
472 // NULL for a nil interface.
474 if (!DECL_P(rhs_tree))
475 rhs_tree = save_expr(rhs_tree);
477 tree rhs_type_descriptor =
478 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
479 location);
481 // The result is going to be a two element constructor.
483 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
485 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
486 tree field = TYPE_FIELDS(lhs_type_tree);
487 elt->index = field;
489 if (for_type_guard)
491 // A type assertion fails when converting a nil interface.
492 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
493 static tree assert_interface_decl;
494 tree call = Gogo::call_builtin(&assert_interface_decl,
495 location,
496 "__go_assert_interface",
498 ptr_type_node,
499 TREE_TYPE(lhs_type_descriptor),
500 lhs_type_descriptor,
501 TREE_TYPE(rhs_type_descriptor),
502 rhs_type_descriptor);
503 if (call == error_mark_node)
504 return error_mark_node;
505 // This will panic if the interface conversion fails.
506 TREE_NOTHROW(assert_interface_decl) = 0;
507 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
509 else if (lhs_is_empty)
511 // A convertion to an empty interface always succeeds, and the
512 // first field is just the type descriptor of the object.
513 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
514 "__type_descriptor") == 0);
515 gcc_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
516 elt->value = rhs_type_descriptor;
518 else
520 // A conversion to a non-empty interface may fail, but unlike a
521 // type assertion converting nil will always succeed.
522 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
523 == 0);
524 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
525 static tree convert_interface_decl;
526 tree call = Gogo::call_builtin(&convert_interface_decl,
527 location,
528 "__go_convert_interface",
530 ptr_type_node,
531 TREE_TYPE(lhs_type_descriptor),
532 lhs_type_descriptor,
533 TREE_TYPE(rhs_type_descriptor),
534 rhs_type_descriptor);
535 if (call == error_mark_node)
536 return error_mark_node;
537 // This will panic if the interface conversion fails.
538 TREE_NOTHROW(convert_interface_decl) = 0;
539 elt->value = fold_convert_loc(location, TREE_TYPE(field), call);
542 // The second field is simply the object pointer.
544 elt = VEC_quick_push(constructor_elt, init, NULL);
545 field = DECL_CHAIN(field);
546 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
547 elt->index = field;
549 tree rhs_type_tree = TREE_TYPE(rhs_tree);
550 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
551 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
552 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
553 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
554 NULL_TREE);
556 return build_constructor(lhs_type_tree, init);
559 // Return a tree for the conversion of an interface type to a
560 // non-interface type.
562 tree
563 Expression::convert_interface_to_type(Translate_context* context,
564 Type *lhs_type, Type* rhs_type,
565 tree rhs_tree, source_location location)
567 Gogo* gogo = context->gogo();
568 tree rhs_type_tree = TREE_TYPE(rhs_tree);
570 tree lhs_type_tree = lhs_type->get_tree(gogo);
571 if (lhs_type_tree == error_mark_node)
572 return error_mark_node;
574 // Call a function to check that the type is valid. The function
575 // will panic with an appropriate runtime type error if the type is
576 // not valid.
578 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo);
580 if (!DECL_P(rhs_tree))
581 rhs_tree = save_expr(rhs_tree);
583 tree rhs_type_descriptor =
584 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
585 location);
587 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo);
589 static tree check_interface_type_decl;
590 tree call = Gogo::call_builtin(&check_interface_type_decl,
591 location,
592 "__go_check_interface_type",
594 void_type_node,
595 TREE_TYPE(lhs_type_descriptor),
596 lhs_type_descriptor,
597 TREE_TYPE(rhs_type_descriptor),
598 rhs_type_descriptor,
599 TREE_TYPE(rhs_inter_descriptor),
600 rhs_inter_descriptor);
601 if (call == error_mark_node)
602 return error_mark_node;
603 // This call will panic if the conversion is invalid.
604 TREE_NOTHROW(check_interface_type_decl) = 0;
606 // If the call succeeds, pull out the value.
607 gcc_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
608 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
609 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
610 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
611 NULL_TREE);
613 // If the value is a pointer, then it is the value we want.
614 // Otherwise it points to the value.
615 if (lhs_type->points_to() == NULL)
617 val = fold_convert_loc(location, build_pointer_type(lhs_type_tree), val);
618 val = build_fold_indirect_ref_loc(location, val);
621 return build2(COMPOUND_EXPR, lhs_type_tree, call,
622 fold_convert_loc(location, lhs_type_tree, val));
625 // Convert an expression to a tree. This is implemented by the child
626 // class. Not that it is not in general safe to call this multiple
627 // times for a single expression, but that we don't catch such errors.
629 tree
630 Expression::get_tree(Translate_context* context)
632 // The child may have marked this expression as having an error.
633 if (this->classification_ == EXPRESSION_ERROR)
634 return error_mark_node;
636 return this->do_get_tree(context);
639 // Return a tree for VAL in TYPE.
641 tree
642 Expression::integer_constant_tree(mpz_t val, tree type)
644 if (type == error_mark_node)
645 return error_mark_node;
646 else if (TREE_CODE(type) == INTEGER_TYPE)
647 return double_int_to_tree(type,
648 mpz_get_double_int(type, val, true));
649 else if (TREE_CODE(type) == REAL_TYPE)
651 mpfr_t fval;
652 mpfr_init_set_z(fval, val, GMP_RNDN);
653 tree ret = Expression::float_constant_tree(fval, type);
654 mpfr_clear(fval);
655 return ret;
657 else if (TREE_CODE(type) == COMPLEX_TYPE)
659 mpfr_t fval;
660 mpfr_init_set_z(fval, val, GMP_RNDN);
661 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
662 mpfr_clear(fval);
663 tree imag = build_real_from_int_cst(TREE_TYPE(type),
664 integer_zero_node);
665 return build_complex(type, real, imag);
667 else
668 gcc_unreachable();
671 // Return a tree for VAL in TYPE.
673 tree
674 Expression::float_constant_tree(mpfr_t val, tree type)
676 if (type == error_mark_node)
677 return error_mark_node;
678 else if (TREE_CODE(type) == INTEGER_TYPE)
680 mpz_t ival;
681 mpz_init(ival);
682 mpfr_get_z(ival, val, GMP_RNDN);
683 tree ret = Expression::integer_constant_tree(ival, type);
684 mpz_clear(ival);
685 return ret;
687 else if (TREE_CODE(type) == REAL_TYPE)
689 REAL_VALUE_TYPE r1;
690 real_from_mpfr(&r1, val, type, GMP_RNDN);
691 REAL_VALUE_TYPE r2;
692 real_convert(&r2, TYPE_MODE(type), &r1);
693 return build_real(type, r2);
695 else if (TREE_CODE(type) == COMPLEX_TYPE)
697 REAL_VALUE_TYPE r1;
698 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
699 REAL_VALUE_TYPE r2;
700 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
701 tree imag = build_real_from_int_cst(TREE_TYPE(type),
702 integer_zero_node);
703 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
705 else
706 gcc_unreachable();
709 // Return a tree for REAL/IMAG in TYPE.
711 tree
712 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
714 if (type == error_mark_node)
715 return error_mark_node;
716 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
717 return Expression::float_constant_tree(real, type);
718 else if (TREE_CODE(type) == COMPLEX_TYPE)
720 REAL_VALUE_TYPE r1;
721 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
722 REAL_VALUE_TYPE r2;
723 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
725 REAL_VALUE_TYPE r3;
726 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
727 REAL_VALUE_TYPE r4;
728 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
730 return build_complex(type, build_real(TREE_TYPE(type), r2),
731 build_real(TREE_TYPE(type), r4));
733 else
734 gcc_unreachable();
737 // Return a tree which evaluates to true if VAL, of arbitrary integer
738 // type, is negative or is more than the maximum value of BOUND_TYPE.
739 // If SOFAR is not NULL, it is or'red into the result. The return
740 // value may be NULL if SOFAR is NULL.
742 tree
743 Expression::check_bounds(tree val, tree bound_type, tree sofar,
744 source_location loc)
746 tree val_type = TREE_TYPE(val);
747 tree ret = NULL_TREE;
749 if (!TYPE_UNSIGNED(val_type))
751 ret = fold_build2_loc(loc, LT_EXPR, boolean_type_node, val,
752 build_int_cst(val_type, 0));
753 if (ret == boolean_false_node)
754 ret = NULL_TREE;
757 if ((TYPE_UNSIGNED(val_type) && !TYPE_UNSIGNED(bound_type))
758 || TYPE_SIZE(val_type) > TYPE_SIZE(bound_type))
760 tree max = TYPE_MAX_VALUE(bound_type);
761 tree big = fold_build2_loc(loc, GT_EXPR, boolean_type_node, val,
762 fold_convert_loc(loc, val_type, max));
763 if (big == boolean_false_node)
765 else if (ret == NULL_TREE)
766 ret = big;
767 else
768 ret = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
769 ret, big);
772 if (ret == NULL_TREE)
773 return sofar;
774 else if (sofar == NULL_TREE)
775 return ret;
776 else
777 return fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
778 sofar, ret);
781 // Error expressions. This are used to avoid cascading errors.
783 class Error_expression : public Expression
785 public:
786 Error_expression(source_location location)
787 : Expression(EXPRESSION_ERROR, location)
790 protected:
791 bool
792 do_is_constant() const
793 { return true; }
795 bool
796 do_integer_constant_value(bool, mpz_t val, Type**) const
798 mpz_set_ui(val, 0);
799 return true;
802 bool
803 do_float_constant_value(mpfr_t val, Type**) const
805 mpfr_set_ui(val, 0, GMP_RNDN);
806 return true;
809 bool
810 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
812 mpfr_set_ui(real, 0, GMP_RNDN);
813 mpfr_set_ui(imag, 0, GMP_RNDN);
814 return true;
817 void
818 do_discarding_value()
821 Type*
822 do_type()
823 { return Type::make_error_type(); }
825 void
826 do_determine_type(const Type_context*)
829 Expression*
830 do_copy()
831 { return this; }
833 bool
834 do_is_addressable() const
835 { return true; }
837 tree
838 do_get_tree(Translate_context*)
839 { return error_mark_node; }
842 Expression*
843 Expression::make_error(source_location location)
845 return new Error_expression(location);
848 // An expression which is really a type. This is used during parsing.
849 // It is an error if these survive after lowering.
851 class
852 Type_expression : public Expression
854 public:
855 Type_expression(Type* type, source_location location)
856 : Expression(EXPRESSION_TYPE, location),
857 type_(type)
860 protected:
862 do_traverse(Traverse* traverse)
863 { return Type::traverse(this->type_, traverse); }
865 Type*
866 do_type()
867 { return this->type_; }
869 void
870 do_determine_type(const Type_context*)
873 void
874 do_check_types(Gogo*)
875 { this->report_error(_("invalid use of type")); }
877 Expression*
878 do_copy()
879 { return this; }
881 tree
882 do_get_tree(Translate_context*)
883 { gcc_unreachable(); }
885 private:
886 // The type which we are representing as an expression.
887 Type* type_;
890 Expression*
891 Expression::make_type(Type* type, source_location location)
893 return new Type_expression(type, location);
896 // Class Parser_expression.
898 Type*
899 Parser_expression::do_type()
901 // We should never really ask for the type of a Parser_expression.
902 // However, it can happen, at least when we have an invalid const
903 // whose initializer refers to the const itself. In that case we
904 // may ask for the type when lowering the const itself.
905 gcc_assert(saw_errors());
906 return Type::make_error_type();
909 // Class Var_expression.
911 // Lower a variable expression. Here we just make sure that the
912 // initialization expression of the variable has been lowered. This
913 // ensures that we will be able to determine the type of the variable
914 // if necessary.
916 Expression*
917 Var_expression::do_lower(Gogo* gogo, Named_object* function, int)
919 if (this->variable_->is_variable())
921 Variable* var = this->variable_->var_value();
922 // This is either a local variable or a global variable. A
923 // reference to a variable which is local to an enclosing
924 // function will be a reference to a field in a closure.
925 if (var->is_global())
926 function = NULL;
927 var->lower_init_expression(gogo, function);
929 return this;
932 // Return the type of a reference to a variable.
934 Type*
935 Var_expression::do_type()
937 if (this->variable_->is_variable())
938 return this->variable_->var_value()->type();
939 else if (this->variable_->is_result_variable())
940 return this->variable_->result_var_value()->type();
941 else
942 gcc_unreachable();
945 // Determine the type of a reference to a variable.
947 void
948 Var_expression::do_determine_type(const Type_context*)
950 if (this->variable_->is_variable())
951 this->variable_->var_value()->determine_type();
954 // Something takes the address of this variable. This means that we
955 // may want to move the variable onto the heap.
957 void
958 Var_expression::do_address_taken(bool escapes)
960 if (!escapes)
962 else if (this->variable_->is_variable())
963 this->variable_->var_value()->set_address_taken();
964 else if (this->variable_->is_result_variable())
965 this->variable_->result_var_value()->set_address_taken();
966 else
967 gcc_unreachable();
970 // Get the tree for a reference to a variable.
972 tree
973 Var_expression::do_get_tree(Translate_context* context)
975 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
976 context->function());
977 tree ret = var_to_tree(bvar);
978 if (ret == error_mark_node)
979 return error_mark_node;
980 bool is_in_heap;
981 if (this->variable_->is_variable())
982 is_in_heap = this->variable_->var_value()->is_in_heap();
983 else if (this->variable_->is_result_variable())
984 is_in_heap = this->variable_->result_var_value()->is_in_heap();
985 else
986 gcc_unreachable();
987 if (is_in_heap)
989 ret = build_fold_indirect_ref_loc(this->location(), ret);
990 TREE_THIS_NOTRAP(ret) = 1;
992 return ret;
995 // Make a reference to a variable in an expression.
997 Expression*
998 Expression::make_var_reference(Named_object* var, source_location location)
1000 if (var->is_sink())
1001 return Expression::make_sink(location);
1003 // FIXME: Creating a new object for each reference to a variable is
1004 // wasteful.
1005 return new Var_expression(var, location);
1008 // Class Temporary_reference_expression.
1010 // The type.
1012 Type*
1013 Temporary_reference_expression::do_type()
1015 return this->statement_->type();
1018 // Called if something takes the address of this temporary variable.
1019 // We never have to move temporary variables to the heap, but we do
1020 // need to know that they must live in the stack rather than in a
1021 // register.
1023 void
1024 Temporary_reference_expression::do_address_taken(bool)
1026 this->statement_->set_is_address_taken();
1029 // Get a tree referring to the variable.
1031 tree
1032 Temporary_reference_expression::do_get_tree(Translate_context* context)
1034 Bvariable* bvar = this->statement_->get_backend_variable(context);
1036 // The gcc backend can't represent the same set of recursive types
1037 // that the Go frontend can. In some cases this means that a
1038 // temporary variable won't have the right backend type. Correct
1039 // that here by adding a type cast. We need to use base() to push
1040 // the circularity down one level.
1041 tree ret = var_to_tree(bvar);
1042 if (POINTER_TYPE_P(TREE_TYPE(ret)) && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
1044 tree type_tree = this->type()->base()->get_tree(context->gogo());
1045 ret = fold_convert_loc(this->location(), type_tree, ret);
1047 return ret;
1050 // Make a reference to a temporary variable.
1052 Expression*
1053 Expression::make_temporary_reference(Temporary_statement* statement,
1054 source_location location)
1056 return new Temporary_reference_expression(statement, location);
1059 // A sink expression--a use of the blank identifier _.
1061 class Sink_expression : public Expression
1063 public:
1064 Sink_expression(source_location location)
1065 : Expression(EXPRESSION_SINK, location),
1066 type_(NULL), var_(NULL_TREE)
1069 protected:
1070 void
1071 do_discarding_value()
1074 Type*
1075 do_type();
1077 void
1078 do_determine_type(const Type_context*);
1080 Expression*
1081 do_copy()
1082 { return new Sink_expression(this->location()); }
1084 tree
1085 do_get_tree(Translate_context*);
1087 private:
1088 // The type of this sink variable.
1089 Type* type_;
1090 // The temporary variable we generate.
1091 tree var_;
1094 // Return the type of a sink expression.
1096 Type*
1097 Sink_expression::do_type()
1099 if (this->type_ == NULL)
1100 return Type::make_sink_type();
1101 return this->type_;
1104 // Determine the type of a sink expression.
1106 void
1107 Sink_expression::do_determine_type(const Type_context* context)
1109 if (context->type != NULL)
1110 this->type_ = context->type;
1113 // Return a temporary variable for a sink expression. This will
1114 // presumably be a write-only variable which the middle-end will drop.
1116 tree
1117 Sink_expression::do_get_tree(Translate_context* context)
1119 if (this->var_ == NULL_TREE)
1121 gcc_assert(this->type_ != NULL && !this->type_->is_sink_type());
1122 this->var_ = create_tmp_var(this->type_->get_tree(context->gogo()),
1123 "blank");
1125 return this->var_;
1128 // Make a sink expression.
1130 Expression*
1131 Expression::make_sink(source_location location)
1133 return new Sink_expression(location);
1136 // Class Func_expression.
1138 // FIXME: Can a function expression appear in a constant expression?
1139 // The value is unchanging. Initializing a constant to the address of
1140 // a function seems like it could work, though there might be little
1141 // point to it.
1143 // Traversal.
1146 Func_expression::do_traverse(Traverse* traverse)
1148 return (this->closure_ == NULL
1149 ? TRAVERSE_CONTINUE
1150 : Expression::traverse(&this->closure_, traverse));
1153 // Return the type of a function expression.
1155 Type*
1156 Func_expression::do_type()
1158 if (this->function_->is_function())
1159 return this->function_->func_value()->type();
1160 else if (this->function_->is_function_declaration())
1161 return this->function_->func_declaration_value()->type();
1162 else
1163 gcc_unreachable();
1166 // Get the tree for a function expression without evaluating the
1167 // closure.
1169 tree
1170 Func_expression::get_tree_without_closure(Gogo* gogo)
1172 Function_type* fntype;
1173 if (this->function_->is_function())
1174 fntype = this->function_->func_value()->type();
1175 else if (this->function_->is_function_declaration())
1176 fntype = this->function_->func_declaration_value()->type();
1177 else
1178 gcc_unreachable();
1180 // Builtin functions are handled specially by Call_expression. We
1181 // can't take their address.
1182 if (fntype->is_builtin())
1184 error_at(this->location(), "invalid use of special builtin function %qs",
1185 this->function_->name().c_str());
1186 return error_mark_node;
1189 Named_object* no = this->function_;
1191 tree id = no->get_id(gogo);
1192 if (id == error_mark_node)
1193 return error_mark_node;
1195 tree fndecl;
1196 if (no->is_function())
1197 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1198 else if (no->is_function_declaration())
1199 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1200 else
1201 gcc_unreachable();
1203 if (fndecl == error_mark_node)
1204 return error_mark_node;
1206 return build_fold_addr_expr_loc(this->location(), fndecl);
1209 // Get the tree for a function expression. This is used when we take
1210 // the address of a function rather than simply calling it. If the
1211 // function has a closure, we must use a trampoline.
1213 tree
1214 Func_expression::do_get_tree(Translate_context* context)
1216 Gogo* gogo = context->gogo();
1218 tree fnaddr = this->get_tree_without_closure(gogo);
1219 if (fnaddr == error_mark_node)
1220 return error_mark_node;
1222 gcc_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1223 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1224 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1226 // For a normal non-nested function call, that is all we have to do.
1227 if (!this->function_->is_function()
1228 || this->function_->func_value()->enclosing() == NULL)
1230 gcc_assert(this->closure_ == NULL);
1231 return fnaddr;
1234 // For a nested function call, we have to always allocate a
1235 // trampoline. If we don't always allocate, then closures will not
1236 // be reliably distinct.
1237 Expression* closure = this->closure_;
1238 tree closure_tree;
1239 if (closure == NULL)
1240 closure_tree = null_pointer_node;
1241 else
1243 // Get the value of the closure. This will be a pointer to
1244 // space allocated on the heap.
1245 closure_tree = closure->get_tree(context);
1246 if (closure_tree == error_mark_node)
1247 return error_mark_node;
1248 gcc_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1251 // Now we need to build some code on the heap. This code will load
1252 // the static chain pointer with the closure and then jump to the
1253 // body of the function. The normal gcc approach is to build the
1254 // code on the stack. Unfortunately we can not do that, as Go
1255 // permits us to return the function pointer.
1257 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1260 // Make a reference to a function in an expression.
1262 Expression*
1263 Expression::make_func_reference(Named_object* function, Expression* closure,
1264 source_location location)
1266 return new Func_expression(function, closure, location);
1269 // Class Unknown_expression.
1271 // Return the name of an unknown expression.
1273 const std::string&
1274 Unknown_expression::name() const
1276 return this->named_object_->name();
1279 // Lower a reference to an unknown name.
1281 Expression*
1282 Unknown_expression::do_lower(Gogo*, Named_object*, int)
1284 source_location location = this->location();
1285 Named_object* no = this->named_object_;
1286 Named_object* real;
1287 if (!no->is_unknown())
1288 real = no;
1289 else
1291 real = no->unknown_value()->real_named_object();
1292 if (real == NULL)
1294 if (this->is_composite_literal_key_)
1295 return this;
1296 error_at(location, "reference to undefined name %qs",
1297 this->named_object_->message_name().c_str());
1298 return Expression::make_error(location);
1301 switch (real->classification())
1303 case Named_object::NAMED_OBJECT_CONST:
1304 return Expression::make_const_reference(real, location);
1305 case Named_object::NAMED_OBJECT_TYPE:
1306 return Expression::make_type(real->type_value(), location);
1307 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1308 if (this->is_composite_literal_key_)
1309 return this;
1310 error_at(location, "reference to undefined type %qs",
1311 real->message_name().c_str());
1312 return Expression::make_error(location);
1313 case Named_object::NAMED_OBJECT_VAR:
1314 return Expression::make_var_reference(real, location);
1315 case Named_object::NAMED_OBJECT_FUNC:
1316 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1317 return Expression::make_func_reference(real, NULL, location);
1318 case Named_object::NAMED_OBJECT_PACKAGE:
1319 if (this->is_composite_literal_key_)
1320 return this;
1321 error_at(location, "unexpected reference to package");
1322 return Expression::make_error(location);
1323 default:
1324 gcc_unreachable();
1328 // Make a reference to an unknown name.
1330 Expression*
1331 Expression::make_unknown_reference(Named_object* no, source_location location)
1333 gcc_assert(no->resolve()->is_unknown());
1334 return new Unknown_expression(no, location);
1337 // A boolean expression.
1339 class Boolean_expression : public Expression
1341 public:
1342 Boolean_expression(bool val, source_location location)
1343 : Expression(EXPRESSION_BOOLEAN, location),
1344 val_(val), type_(NULL)
1347 static Expression*
1348 do_import(Import*);
1350 protected:
1351 bool
1352 do_is_constant() const
1353 { return true; }
1355 Type*
1356 do_type();
1358 void
1359 do_determine_type(const Type_context*);
1361 Expression*
1362 do_copy()
1363 { return this; }
1365 tree
1366 do_get_tree(Translate_context*)
1367 { return this->val_ ? boolean_true_node : boolean_false_node; }
1369 void
1370 do_export(Export* exp) const
1371 { exp->write_c_string(this->val_ ? "true" : "false"); }
1373 private:
1374 // The constant.
1375 bool val_;
1376 // The type as determined by context.
1377 Type* type_;
1380 // Get the type.
1382 Type*
1383 Boolean_expression::do_type()
1385 if (this->type_ == NULL)
1386 this->type_ = Type::make_boolean_type();
1387 return this->type_;
1390 // Set the type from the context.
1392 void
1393 Boolean_expression::do_determine_type(const Type_context* context)
1395 if (this->type_ != NULL && !this->type_->is_abstract())
1397 else if (context->type != NULL && context->type->is_boolean_type())
1398 this->type_ = context->type;
1399 else if (!context->may_be_abstract)
1400 this->type_ = Type::lookup_bool_type();
1403 // Import a boolean constant.
1405 Expression*
1406 Boolean_expression::do_import(Import* imp)
1408 if (imp->peek_char() == 't')
1410 imp->require_c_string("true");
1411 return Expression::make_boolean(true, imp->location());
1413 else
1415 imp->require_c_string("false");
1416 return Expression::make_boolean(false, imp->location());
1420 // Make a boolean expression.
1422 Expression*
1423 Expression::make_boolean(bool val, source_location location)
1425 return new Boolean_expression(val, location);
1428 // Class String_expression.
1430 // Get the type.
1432 Type*
1433 String_expression::do_type()
1435 if (this->type_ == NULL)
1436 this->type_ = Type::make_string_type();
1437 return this->type_;
1440 // Set the type from the context.
1442 void
1443 String_expression::do_determine_type(const Type_context* context)
1445 if (this->type_ != NULL && !this->type_->is_abstract())
1447 else if (context->type != NULL && context->type->is_string_type())
1448 this->type_ = context->type;
1449 else if (!context->may_be_abstract)
1450 this->type_ = Type::lookup_string_type();
1453 // Build a string constant.
1455 tree
1456 String_expression::do_get_tree(Translate_context* context)
1458 return context->gogo()->go_string_constant_tree(this->val_);
1461 // Export a string expression.
1463 void
1464 String_expression::do_export(Export* exp) const
1466 std::string s;
1467 s.reserve(this->val_.length() * 4 + 2);
1468 s += '"';
1469 for (std::string::const_iterator p = this->val_.begin();
1470 p != this->val_.end();
1471 ++p)
1473 if (*p == '\\' || *p == '"')
1475 s += '\\';
1476 s += *p;
1478 else if (*p >= 0x20 && *p < 0x7f)
1479 s += *p;
1480 else if (*p == '\n')
1481 s += "\\n";
1482 else if (*p == '\t')
1483 s += "\\t";
1484 else
1486 s += "\\x";
1487 unsigned char c = *p;
1488 unsigned int dig = c >> 4;
1489 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1490 dig = c & 0xf;
1491 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1494 s += '"';
1495 exp->write_string(s);
1498 // Import a string expression.
1500 Expression*
1501 String_expression::do_import(Import* imp)
1503 imp->require_c_string("\"");
1504 std::string val;
1505 while (true)
1507 int c = imp->get_char();
1508 if (c == '"' || c == -1)
1509 break;
1510 if (c != '\\')
1511 val += static_cast<char>(c);
1512 else
1514 c = imp->get_char();
1515 if (c == '\\' || c == '"')
1516 val += static_cast<char>(c);
1517 else if (c == 'n')
1518 val += '\n';
1519 else if (c == 't')
1520 val += '\t';
1521 else if (c == 'x')
1523 c = imp->get_char();
1524 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1525 c = imp->get_char();
1526 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1527 char v = (vh << 4) | vl;
1528 val += v;
1530 else
1532 error_at(imp->location(), "bad string constant");
1533 return Expression::make_error(imp->location());
1537 return Expression::make_string(val, imp->location());
1540 // Make a string expression.
1542 Expression*
1543 Expression::make_string(const std::string& val, source_location location)
1545 return new String_expression(val, location);
1548 // Make an integer expression.
1550 class Integer_expression : public Expression
1552 public:
1553 Integer_expression(const mpz_t* val, Type* type, source_location location)
1554 : Expression(EXPRESSION_INTEGER, location),
1555 type_(type)
1556 { mpz_init_set(this->val_, *val); }
1558 static Expression*
1559 do_import(Import*);
1561 // Return whether VAL fits in the type.
1562 static bool
1563 check_constant(mpz_t val, Type*, source_location);
1565 // Write VAL to export data.
1566 static void
1567 export_integer(Export* exp, const mpz_t val);
1569 protected:
1570 bool
1571 do_is_constant() const
1572 { return true; }
1574 bool
1575 do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1577 Type*
1578 do_type();
1580 void
1581 do_determine_type(const Type_context* context);
1583 void
1584 do_check_types(Gogo*);
1586 tree
1587 do_get_tree(Translate_context*);
1589 Expression*
1590 do_copy()
1591 { return Expression::make_integer(&this->val_, this->type_,
1592 this->location()); }
1594 void
1595 do_export(Export*) const;
1597 private:
1598 // The integer value.
1599 mpz_t val_;
1600 // The type so far.
1601 Type* type_;
1604 // Return an integer constant value.
1606 bool
1607 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1608 Type** ptype) const
1610 if (this->type_ != NULL)
1611 *ptype = this->type_;
1612 mpz_set(val, this->val_);
1613 return true;
1616 // Return the current type. If we haven't set the type yet, we return
1617 // an abstract integer type.
1619 Type*
1620 Integer_expression::do_type()
1622 if (this->type_ == NULL)
1623 this->type_ = Type::make_abstract_integer_type();
1624 return this->type_;
1627 // Set the type of the integer value. Here we may switch from an
1628 // abstract type to a real type.
1630 void
1631 Integer_expression::do_determine_type(const Type_context* context)
1633 if (this->type_ != NULL && !this->type_->is_abstract())
1635 else if (context->type != NULL
1636 && (context->type->integer_type() != NULL
1637 || context->type->float_type() != NULL
1638 || context->type->complex_type() != NULL))
1639 this->type_ = context->type;
1640 else if (!context->may_be_abstract)
1641 this->type_ = Type::lookup_integer_type("int");
1644 // Return true if the integer VAL fits in the range of the type TYPE.
1645 // Otherwise give an error and return false. TYPE may be NULL.
1647 bool
1648 Integer_expression::check_constant(mpz_t val, Type* type,
1649 source_location location)
1651 if (type == NULL)
1652 return true;
1653 Integer_type* itype = type->integer_type();
1654 if (itype == NULL || itype->is_abstract())
1655 return true;
1657 int bits = mpz_sizeinbase(val, 2);
1659 if (itype->is_unsigned())
1661 // For an unsigned type we can only accept a nonnegative number,
1662 // and we must be able to represent at least BITS.
1663 if (mpz_sgn(val) >= 0
1664 && bits <= itype->bits())
1665 return true;
1667 else
1669 // For a signed type we need an extra bit to indicate the sign.
1670 // We have to handle the most negative integer specially.
1671 if (bits + 1 <= itype->bits()
1672 || (bits <= itype->bits()
1673 && mpz_sgn(val) < 0
1674 && (mpz_scan1(val, 0)
1675 == static_cast<unsigned long>(itype->bits() - 1))
1676 && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1677 return true;
1680 error_at(location, "integer constant overflow");
1681 return false;
1684 // Check the type of an integer constant.
1686 void
1687 Integer_expression::do_check_types(Gogo*)
1689 if (this->type_ == NULL)
1690 return;
1691 if (!Integer_expression::check_constant(this->val_, this->type_,
1692 this->location()))
1693 this->set_is_error();
1696 // Get a tree for an integer constant.
1698 tree
1699 Integer_expression::do_get_tree(Translate_context* context)
1701 Gogo* gogo = context->gogo();
1702 tree type;
1703 if (this->type_ != NULL && !this->type_->is_abstract())
1704 type = this->type_->get_tree(gogo);
1705 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1707 // We are converting to an abstract floating point type.
1708 type = Type::lookup_float_type("float64")->get_tree(gogo);
1710 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1712 // We are converting to an abstract complex type.
1713 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
1715 else
1717 // If we still have an abstract type here, then this is being
1718 // used in a constant expression which didn't get reduced for
1719 // some reason. Use a type which will fit the value. We use <,
1720 // not <=, because we need an extra bit for the sign bit.
1721 int bits = mpz_sizeinbase(this->val_, 2);
1722 if (bits < INT_TYPE_SIZE)
1723 type = Type::lookup_integer_type("int")->get_tree(gogo);
1724 else if (bits < 64)
1725 type = Type::lookup_integer_type("int64")->get_tree(gogo);
1726 else
1727 type = long_long_integer_type_node;
1729 return Expression::integer_constant_tree(this->val_, type);
1732 // Write VAL to export data.
1734 void
1735 Integer_expression::export_integer(Export* exp, const mpz_t val)
1737 char* s = mpz_get_str(NULL, 10, val);
1738 exp->write_c_string(s);
1739 free(s);
1742 // Export an integer in a constant expression.
1744 void
1745 Integer_expression::do_export(Export* exp) const
1747 Integer_expression::export_integer(exp, this->val_);
1748 // A trailing space lets us reliably identify the end of the number.
1749 exp->write_c_string(" ");
1752 // Import an integer, floating point, or complex value. This handles
1753 // all these types because they all start with digits.
1755 Expression*
1756 Integer_expression::do_import(Import* imp)
1758 std::string num = imp->read_identifier();
1759 imp->require_c_string(" ");
1760 if (!num.empty() && num[num.length() - 1] == 'i')
1762 mpfr_t real;
1763 size_t plus_pos = num.find('+', 1);
1764 size_t minus_pos = num.find('-', 1);
1765 size_t pos;
1766 if (plus_pos == std::string::npos)
1767 pos = minus_pos;
1768 else if (minus_pos == std::string::npos)
1769 pos = plus_pos;
1770 else
1772 error_at(imp->location(), "bad number in import data: %qs",
1773 num.c_str());
1774 return Expression::make_error(imp->location());
1776 if (pos == std::string::npos)
1777 mpfr_set_ui(real, 0, GMP_RNDN);
1778 else
1780 std::string real_str = num.substr(0, pos);
1781 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1783 error_at(imp->location(), "bad number in import data: %qs",
1784 real_str.c_str());
1785 return Expression::make_error(imp->location());
1789 std::string imag_str;
1790 if (pos == std::string::npos)
1791 imag_str = num;
1792 else
1793 imag_str = num.substr(pos);
1794 imag_str = imag_str.substr(0, imag_str.size() - 1);
1795 mpfr_t imag;
1796 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1798 error_at(imp->location(), "bad number in import data: %qs",
1799 imag_str.c_str());
1800 return Expression::make_error(imp->location());
1802 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1803 imp->location());
1804 mpfr_clear(real);
1805 mpfr_clear(imag);
1806 return ret;
1808 else if (num.find('.') == std::string::npos
1809 && num.find('E') == std::string::npos)
1811 mpz_t val;
1812 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1814 error_at(imp->location(), "bad number in import data: %qs",
1815 num.c_str());
1816 return Expression::make_error(imp->location());
1818 Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1819 mpz_clear(val);
1820 return ret;
1822 else
1824 mpfr_t val;
1825 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1827 error_at(imp->location(), "bad number in import data: %qs",
1828 num.c_str());
1829 return Expression::make_error(imp->location());
1831 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1832 mpfr_clear(val);
1833 return ret;
1837 // Build a new integer value.
1839 Expression*
1840 Expression::make_integer(const mpz_t* val, Type* type,
1841 source_location location)
1843 return new Integer_expression(val, type, location);
1846 // Floats.
1848 class Float_expression : public Expression
1850 public:
1851 Float_expression(const mpfr_t* val, Type* type, source_location location)
1852 : Expression(EXPRESSION_FLOAT, location),
1853 type_(type)
1855 mpfr_init_set(this->val_, *val, GMP_RNDN);
1858 // Constrain VAL to fit into TYPE.
1859 static void
1860 constrain_float(mpfr_t val, Type* type);
1862 // Return whether VAL fits in the type.
1863 static bool
1864 check_constant(mpfr_t val, Type*, source_location);
1866 // Write VAL to export data.
1867 static void
1868 export_float(Export* exp, const mpfr_t val);
1870 protected:
1871 bool
1872 do_is_constant() const
1873 { return true; }
1875 bool
1876 do_float_constant_value(mpfr_t val, Type**) const;
1878 Type*
1879 do_type();
1881 void
1882 do_determine_type(const Type_context*);
1884 void
1885 do_check_types(Gogo*);
1887 Expression*
1888 do_copy()
1889 { return Expression::make_float(&this->val_, this->type_,
1890 this->location()); }
1892 tree
1893 do_get_tree(Translate_context*);
1895 void
1896 do_export(Export*) const;
1898 private:
1899 // The floating point value.
1900 mpfr_t val_;
1901 // The type so far.
1902 Type* type_;
1905 // Constrain VAL to fit into TYPE.
1907 void
1908 Float_expression::constrain_float(mpfr_t val, Type* type)
1910 Float_type* ftype = type->float_type();
1911 if (ftype != NULL && !ftype->is_abstract())
1912 mpfr_prec_round(val, ftype->bits(), GMP_RNDN);
1915 // Return a floating point constant value.
1917 bool
1918 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
1920 if (this->type_ != NULL)
1921 *ptype = this->type_;
1922 mpfr_set(val, this->val_, GMP_RNDN);
1923 return true;
1926 // Return the current type. If we haven't set the type yet, we return
1927 // an abstract float type.
1929 Type*
1930 Float_expression::do_type()
1932 if (this->type_ == NULL)
1933 this->type_ = Type::make_abstract_float_type();
1934 return this->type_;
1937 // Set the type of the float value. Here we may switch from an
1938 // abstract type to a real type.
1940 void
1941 Float_expression::do_determine_type(const Type_context* context)
1943 if (this->type_ != NULL && !this->type_->is_abstract())
1945 else if (context->type != NULL
1946 && (context->type->integer_type() != NULL
1947 || context->type->float_type() != NULL
1948 || context->type->complex_type() != NULL))
1949 this->type_ = context->type;
1950 else if (!context->may_be_abstract)
1951 this->type_ = Type::lookup_float_type("float64");
1954 // Return true if the floating point value VAL fits in the range of
1955 // the type TYPE. Otherwise give an error and return false. TYPE may
1956 // be NULL.
1958 bool
1959 Float_expression::check_constant(mpfr_t val, Type* type,
1960 source_location location)
1962 if (type == NULL)
1963 return true;
1964 Float_type* ftype = type->float_type();
1965 if (ftype == NULL || ftype->is_abstract())
1966 return true;
1968 // A NaN or Infinity always fits in the range of the type.
1969 if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
1970 return true;
1972 mp_exp_t exp = mpfr_get_exp(val);
1973 mp_exp_t max_exp;
1974 switch (ftype->bits())
1976 case 32:
1977 max_exp = 128;
1978 break;
1979 case 64:
1980 max_exp = 1024;
1981 break;
1982 default:
1983 gcc_unreachable();
1985 if (exp > max_exp)
1987 error_at(location, "floating point constant overflow");
1988 return false;
1990 return true;
1993 // Check the type of a float value.
1995 void
1996 Float_expression::do_check_types(Gogo*)
1998 if (this->type_ == NULL)
1999 return;
2001 if (!Float_expression::check_constant(this->val_, this->type_,
2002 this->location()))
2003 this->set_is_error();
2005 Integer_type* integer_type = this->type_->integer_type();
2006 if (integer_type != NULL)
2008 if (!mpfr_integer_p(this->val_))
2009 this->report_error(_("floating point constant truncated to integer"));
2010 else
2012 gcc_assert(!integer_type->is_abstract());
2013 mpz_t ival;
2014 mpz_init(ival);
2015 mpfr_get_z(ival, this->val_, GMP_RNDN);
2016 Integer_expression::check_constant(ival, integer_type,
2017 this->location());
2018 mpz_clear(ival);
2023 // Get a tree for a float constant.
2025 tree
2026 Float_expression::do_get_tree(Translate_context* context)
2028 Gogo* gogo = context->gogo();
2029 tree type;
2030 if (this->type_ != NULL && !this->type_->is_abstract())
2031 type = this->type_->get_tree(gogo);
2032 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2034 // We have an abstract integer type. We just hope for the best.
2035 type = Type::lookup_integer_type("int")->get_tree(gogo);
2037 else
2039 // If we still have an abstract type here, then this is being
2040 // used in a constant expression which didn't get reduced. We
2041 // just use float64 and hope for the best.
2042 type = Type::lookup_float_type("float64")->get_tree(gogo);
2044 return Expression::float_constant_tree(this->val_, type);
2047 // Write a floating point number to export data.
2049 void
2050 Float_expression::export_float(Export *exp, const mpfr_t val)
2052 mp_exp_t exponent;
2053 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2054 if (*s == '-')
2055 exp->write_c_string("-");
2056 exp->write_c_string("0.");
2057 exp->write_c_string(*s == '-' ? s + 1 : s);
2058 mpfr_free_str(s);
2059 char buf[30];
2060 snprintf(buf, sizeof buf, "E%ld", exponent);
2061 exp->write_c_string(buf);
2064 // Export a floating point number in a constant expression.
2066 void
2067 Float_expression::do_export(Export* exp) const
2069 Float_expression::export_float(exp, this->val_);
2070 // A trailing space lets us reliably identify the end of the number.
2071 exp->write_c_string(" ");
2074 // Make a float expression.
2076 Expression*
2077 Expression::make_float(const mpfr_t* val, Type* type, source_location location)
2079 return new Float_expression(val, type, location);
2082 // Complex numbers.
2084 class Complex_expression : public Expression
2086 public:
2087 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2088 source_location location)
2089 : Expression(EXPRESSION_COMPLEX, location),
2090 type_(type)
2092 mpfr_init_set(this->real_, *real, GMP_RNDN);
2093 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2096 // Constrain REAL/IMAG to fit into TYPE.
2097 static void
2098 constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2100 // Return whether REAL/IMAG fits in the type.
2101 static bool
2102 check_constant(mpfr_t real, mpfr_t imag, Type*, source_location);
2104 // Write REAL/IMAG to export data.
2105 static void
2106 export_complex(Export* exp, const mpfr_t real, const mpfr_t val);
2108 protected:
2109 bool
2110 do_is_constant() const
2111 { return true; }
2113 bool
2114 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2116 Type*
2117 do_type();
2119 void
2120 do_determine_type(const Type_context*);
2122 void
2123 do_check_types(Gogo*);
2125 Expression*
2126 do_copy()
2128 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2129 this->location());
2132 tree
2133 do_get_tree(Translate_context*);
2135 void
2136 do_export(Export*) const;
2138 private:
2139 // The real part.
2140 mpfr_t real_;
2141 // The imaginary part;
2142 mpfr_t imag_;
2143 // The type if known.
2144 Type* type_;
2147 // Constrain REAL/IMAG to fit into TYPE.
2149 void
2150 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2152 Complex_type* ctype = type->complex_type();
2153 if (ctype != NULL && !ctype->is_abstract())
2155 mpfr_prec_round(real, ctype->bits() / 2, GMP_RNDN);
2156 mpfr_prec_round(imag, ctype->bits() / 2, GMP_RNDN);
2160 // Return a complex constant value.
2162 bool
2163 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2164 Type** ptype) const
2166 if (this->type_ != NULL)
2167 *ptype = this->type_;
2168 mpfr_set(real, this->real_, GMP_RNDN);
2169 mpfr_set(imag, this->imag_, GMP_RNDN);
2170 return true;
2173 // Return the current type. If we haven't set the type yet, we return
2174 // an abstract complex type.
2176 Type*
2177 Complex_expression::do_type()
2179 if (this->type_ == NULL)
2180 this->type_ = Type::make_abstract_complex_type();
2181 return this->type_;
2184 // Set the type of the complex value. Here we may switch from an
2185 // abstract type to a real type.
2187 void
2188 Complex_expression::do_determine_type(const Type_context* context)
2190 if (this->type_ != NULL && !this->type_->is_abstract())
2192 else if (context->type != NULL
2193 && context->type->complex_type() != NULL)
2194 this->type_ = context->type;
2195 else if (!context->may_be_abstract)
2196 this->type_ = Type::lookup_complex_type("complex128");
2199 // Return true if the complex value REAL/IMAG fits in the range of the
2200 // type TYPE. Otherwise give an error and return false. TYPE may be
2201 // NULL.
2203 bool
2204 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2205 source_location location)
2207 if (type == NULL)
2208 return true;
2209 Complex_type* ctype = type->complex_type();
2210 if (ctype == NULL || ctype->is_abstract())
2211 return true;
2213 mp_exp_t max_exp;
2214 switch (ctype->bits())
2216 case 64:
2217 max_exp = 128;
2218 break;
2219 case 128:
2220 max_exp = 1024;
2221 break;
2222 default:
2223 gcc_unreachable();
2226 // A NaN or Infinity always fits in the range of the type.
2227 if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2229 if (mpfr_get_exp(real) > max_exp)
2231 error_at(location, "complex real part constant overflow");
2232 return false;
2236 if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2238 if (mpfr_get_exp(imag) > max_exp)
2240 error_at(location, "complex imaginary part constant overflow");
2241 return false;
2245 return true;
2248 // Check the type of a complex value.
2250 void
2251 Complex_expression::do_check_types(Gogo*)
2253 if (this->type_ == NULL)
2254 return;
2256 if (!Complex_expression::check_constant(this->real_, this->imag_,
2257 this->type_, this->location()))
2258 this->set_is_error();
2261 // Get a tree for a complex constant.
2263 tree
2264 Complex_expression::do_get_tree(Translate_context* context)
2266 Gogo* gogo = context->gogo();
2267 tree type;
2268 if (this->type_ != NULL && !this->type_->is_abstract())
2269 type = this->type_->get_tree(gogo);
2270 else
2272 // If we still have an abstract type here, this this is being
2273 // used in a constant expression which didn't get reduced. We
2274 // just use complex128 and hope for the best.
2275 type = Type::lookup_complex_type("complex128")->get_tree(gogo);
2277 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2280 // Write REAL/IMAG to export data.
2282 void
2283 Complex_expression::export_complex(Export* exp, const mpfr_t real,
2284 const mpfr_t imag)
2286 if (!mpfr_zero_p(real))
2288 Float_expression::export_float(exp, real);
2289 if (mpfr_sgn(imag) > 0)
2290 exp->write_c_string("+");
2292 Float_expression::export_float(exp, imag);
2293 exp->write_c_string("i");
2296 // Export a complex number in a constant expression.
2298 void
2299 Complex_expression::do_export(Export* exp) const
2301 Complex_expression::export_complex(exp, this->real_, this->imag_);
2302 // A trailing space lets us reliably identify the end of the number.
2303 exp->write_c_string(" ");
2306 // Make a complex expression.
2308 Expression*
2309 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2310 source_location location)
2312 return new Complex_expression(real, imag, type, location);
2315 // Find a named object in an expression.
2317 class Find_named_object : public Traverse
2319 public:
2320 Find_named_object(Named_object* no)
2321 : Traverse(traverse_expressions),
2322 no_(no), found_(false)
2325 // Whether we found the object.
2326 bool
2327 found() const
2328 { return this->found_; }
2330 protected:
2332 expression(Expression**);
2334 private:
2335 // The object we are looking for.
2336 Named_object* no_;
2337 // Whether we found it.
2338 bool found_;
2341 // A reference to a const in an expression.
2343 class Const_expression : public Expression
2345 public:
2346 Const_expression(Named_object* constant, source_location location)
2347 : Expression(EXPRESSION_CONST_REFERENCE, location),
2348 constant_(constant), type_(NULL), seen_(false)
2351 Named_object*
2352 named_object()
2353 { return this->constant_; }
2355 // Check that the initializer does not refer to the constant itself.
2356 void
2357 check_for_init_loop();
2359 protected:
2361 do_traverse(Traverse*);
2363 Expression*
2364 do_lower(Gogo*, Named_object*, int);
2366 bool
2367 do_is_constant() const
2368 { return true; }
2370 bool
2371 do_integer_constant_value(bool, mpz_t val, Type**) const;
2373 bool
2374 do_float_constant_value(mpfr_t val, Type**) const;
2376 bool
2377 do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2379 bool
2380 do_string_constant_value(std::string* val) const
2381 { return this->constant_->const_value()->expr()->string_constant_value(val); }
2383 Type*
2384 do_type();
2386 // The type of a const is set by the declaration, not the use.
2387 void
2388 do_determine_type(const Type_context*);
2390 void
2391 do_check_types(Gogo*);
2393 Expression*
2394 do_copy()
2395 { return this; }
2397 tree
2398 do_get_tree(Translate_context* context);
2400 // When exporting a reference to a const as part of a const
2401 // expression, we export the value. We ignore the fact that it has
2402 // a name.
2403 void
2404 do_export(Export* exp) const
2405 { this->constant_->const_value()->expr()->export_expression(exp); }
2407 private:
2408 // The constant.
2409 Named_object* constant_;
2410 // The type of this reference. This is used if the constant has an
2411 // abstract type.
2412 Type* type_;
2413 // Used to prevent infinite recursion when a constant incorrectly
2414 // refers to itself.
2415 mutable bool seen_;
2418 // Traversal.
2421 Const_expression::do_traverse(Traverse* traverse)
2423 if (this->type_ != NULL)
2424 return Type::traverse(this->type_, traverse);
2425 return TRAVERSE_CONTINUE;
2428 // Lower a constant expression. This is where we convert the
2429 // predeclared constant iota into an integer value.
2431 Expression*
2432 Const_expression::do_lower(Gogo* gogo, Named_object*, int iota_value)
2434 if (this->constant_->const_value()->expr()->classification()
2435 == EXPRESSION_IOTA)
2437 if (iota_value == -1)
2439 error_at(this->location(),
2440 "iota is only defined in const declarations");
2441 iota_value = 0;
2443 mpz_t val;
2444 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2445 Expression* ret = Expression::make_integer(&val, NULL,
2446 this->location());
2447 mpz_clear(val);
2448 return ret;
2451 // Make sure that the constant itself has been lowered.
2452 gogo->lower_constant(this->constant_);
2454 return this;
2457 // Return an integer constant value.
2459 bool
2460 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2461 Type** ptype) const
2463 if (this->seen_)
2464 return false;
2466 Type* ctype;
2467 if (this->type_ != NULL)
2468 ctype = this->type_;
2469 else
2470 ctype = this->constant_->const_value()->type();
2471 if (ctype != NULL && ctype->integer_type() == NULL)
2472 return false;
2474 Expression* e = this->constant_->const_value()->expr();
2476 this->seen_ = true;
2478 Type* t;
2479 bool r = e->integer_constant_value(iota_is_constant, val, &t);
2481 this->seen_ = false;
2483 if (r
2484 && ctype != NULL
2485 && !Integer_expression::check_constant(val, ctype, this->location()))
2486 return false;
2488 *ptype = ctype != NULL ? ctype : t;
2489 return r;
2492 // Return a floating point constant value.
2494 bool
2495 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2497 if (this->seen_)
2498 return false;
2500 Type* ctype;
2501 if (this->type_ != NULL)
2502 ctype = this->type_;
2503 else
2504 ctype = this->constant_->const_value()->type();
2505 if (ctype != NULL && ctype->float_type() == NULL)
2506 return false;
2508 this->seen_ = true;
2510 Type* t;
2511 bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2512 &t);
2514 this->seen_ = false;
2516 if (r && ctype != NULL)
2518 if (!Float_expression::check_constant(val, ctype, this->location()))
2519 return false;
2520 Float_expression::constrain_float(val, ctype);
2522 *ptype = ctype != NULL ? ctype : t;
2523 return r;
2526 // Return a complex constant value.
2528 bool
2529 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2530 Type **ptype) const
2532 if (this->seen_)
2533 return false;
2535 Type* ctype;
2536 if (this->type_ != NULL)
2537 ctype = this->type_;
2538 else
2539 ctype = this->constant_->const_value()->type();
2540 if (ctype != NULL && ctype->complex_type() == NULL)
2541 return false;
2543 this->seen_ = true;
2545 Type *t;
2546 bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2547 imag,
2548 &t);
2550 this->seen_ = false;
2552 if (r && ctype != NULL)
2554 if (!Complex_expression::check_constant(real, imag, ctype,
2555 this->location()))
2556 return false;
2557 Complex_expression::constrain_complex(real, imag, ctype);
2559 *ptype = ctype != NULL ? ctype : t;
2560 return r;
2563 // Return the type of the const reference.
2565 Type*
2566 Const_expression::do_type()
2568 if (this->type_ != NULL)
2569 return this->type_;
2571 Named_constant* nc = this->constant_->const_value();
2573 if (this->seen_ || nc->lowering())
2575 this->report_error(_("constant refers to itself"));
2576 this->type_ = Type::make_error_type();
2577 return this->type_;
2580 this->seen_ = true;
2582 Type* ret = nc->type();
2584 if (ret != NULL)
2586 this->seen_ = false;
2587 return ret;
2590 // During parsing, a named constant may have a NULL type, but we
2591 // must not return a NULL type here.
2592 ret = nc->expr()->type();
2594 this->seen_ = false;
2596 return ret;
2599 // Set the type of the const reference.
2601 void
2602 Const_expression::do_determine_type(const Type_context* context)
2604 Type* ctype = this->constant_->const_value()->type();
2605 Type* cetype = (ctype != NULL
2606 ? ctype
2607 : this->constant_->const_value()->expr()->type());
2608 if (ctype != NULL && !ctype->is_abstract())
2610 else if (context->type != NULL
2611 && (context->type->integer_type() != NULL
2612 || context->type->float_type() != NULL
2613 || context->type->complex_type() != NULL)
2614 && (cetype->integer_type() != NULL
2615 || cetype->float_type() != NULL
2616 || cetype->complex_type() != NULL))
2617 this->type_ = context->type;
2618 else if (context->type != NULL
2619 && context->type->is_string_type()
2620 && cetype->is_string_type())
2621 this->type_ = context->type;
2622 else if (context->type != NULL
2623 && context->type->is_boolean_type()
2624 && cetype->is_boolean_type())
2625 this->type_ = context->type;
2626 else if (!context->may_be_abstract)
2628 if (cetype->is_abstract())
2629 cetype = cetype->make_non_abstract_type();
2630 this->type_ = cetype;
2634 // Check for a loop in which the initializer of a constant refers to
2635 // the constant itself.
2637 void
2638 Const_expression::check_for_init_loop()
2640 if (this->type_ != NULL && this->type_->is_error())
2641 return;
2643 if (this->seen_)
2645 this->report_error(_("constant refers to itself"));
2646 this->type_ = Type::make_error_type();
2647 return;
2650 Expression* init = this->constant_->const_value()->expr();
2651 Find_named_object find_named_object(this->constant_);
2653 this->seen_ = true;
2654 Expression::traverse(&init, &find_named_object);
2655 this->seen_ = false;
2657 if (find_named_object.found())
2659 if (this->type_ == NULL || !this->type_->is_error())
2661 this->report_error(_("constant refers to itself"));
2662 this->type_ = Type::make_error_type();
2664 return;
2668 // Check types of a const reference.
2670 void
2671 Const_expression::do_check_types(Gogo*)
2673 if (this->type_ != NULL && this->type_->is_error())
2674 return;
2676 this->check_for_init_loop();
2678 if (this->type_ == NULL || this->type_->is_abstract())
2679 return;
2681 // Check for integer overflow.
2682 if (this->type_->integer_type() != NULL)
2684 mpz_t ival;
2685 mpz_init(ival);
2686 Type* dummy;
2687 if (!this->integer_constant_value(true, ival, &dummy))
2689 mpfr_t fval;
2690 mpfr_init(fval);
2691 Expression* cexpr = this->constant_->const_value()->expr();
2692 if (cexpr->float_constant_value(fval, &dummy))
2694 if (!mpfr_integer_p(fval))
2695 this->report_error(_("floating point constant "
2696 "truncated to integer"));
2697 else
2699 mpfr_get_z(ival, fval, GMP_RNDN);
2700 Integer_expression::check_constant(ival, this->type_,
2701 this->location());
2704 mpfr_clear(fval);
2706 mpz_clear(ival);
2710 // Return a tree for the const reference.
2712 tree
2713 Const_expression::do_get_tree(Translate_context* context)
2715 Gogo* gogo = context->gogo();
2716 tree type_tree;
2717 if (this->type_ == NULL)
2718 type_tree = NULL_TREE;
2719 else
2721 type_tree = this->type_->get_tree(gogo);
2722 if (type_tree == error_mark_node)
2723 return error_mark_node;
2726 // If the type has been set for this expression, but the underlying
2727 // object is an abstract int or float, we try to get the abstract
2728 // value. Otherwise we may lose something in the conversion.
2729 if (this->type_ != NULL
2730 && (this->constant_->const_value()->type() == NULL
2731 || this->constant_->const_value()->type()->is_abstract()))
2733 Expression* expr = this->constant_->const_value()->expr();
2734 mpz_t ival;
2735 mpz_init(ival);
2736 Type* t;
2737 if (expr->integer_constant_value(true, ival, &t))
2739 tree ret = Expression::integer_constant_tree(ival, type_tree);
2740 mpz_clear(ival);
2741 return ret;
2743 mpz_clear(ival);
2745 mpfr_t fval;
2746 mpfr_init(fval);
2747 if (expr->float_constant_value(fval, &t))
2749 tree ret = Expression::float_constant_tree(fval, type_tree);
2750 mpfr_clear(fval);
2751 return ret;
2754 mpfr_t imag;
2755 mpfr_init(imag);
2756 if (expr->complex_constant_value(fval, imag, &t))
2758 tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2759 mpfr_clear(fval);
2760 mpfr_clear(imag);
2761 return ret;
2763 mpfr_clear(imag);
2764 mpfr_clear(fval);
2767 tree const_tree = this->constant_->get_tree(gogo, context->function());
2768 if (this->type_ == NULL
2769 || const_tree == error_mark_node
2770 || TREE_TYPE(const_tree) == error_mark_node)
2771 return const_tree;
2773 tree ret;
2774 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2775 ret = fold_convert(type_tree, const_tree);
2776 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2777 ret = fold(convert_to_integer(type_tree, const_tree));
2778 else if (TREE_CODE(type_tree) == REAL_TYPE)
2779 ret = fold(convert_to_real(type_tree, const_tree));
2780 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2781 ret = fold(convert_to_complex(type_tree, const_tree));
2782 else
2783 gcc_unreachable();
2784 return ret;
2787 // Make a reference to a constant in an expression.
2789 Expression*
2790 Expression::make_const_reference(Named_object* constant,
2791 source_location location)
2793 return new Const_expression(constant, location);
2796 // Find a named object in an expression.
2799 Find_named_object::expression(Expression** pexpr)
2801 switch ((*pexpr)->classification())
2803 case Expression::EXPRESSION_CONST_REFERENCE:
2805 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2806 if (ce->named_object() == this->no_)
2807 break;
2809 // We need to check a constant initializer explicitly, as
2810 // loops here will not be caught by the loop checking for
2811 // variable initializers.
2812 ce->check_for_init_loop();
2814 return TRAVERSE_CONTINUE;
2817 case Expression::EXPRESSION_VAR_REFERENCE:
2818 if ((*pexpr)->var_expression()->named_object() == this->no_)
2819 break;
2820 return TRAVERSE_CONTINUE;
2821 case Expression::EXPRESSION_FUNC_REFERENCE:
2822 if ((*pexpr)->func_expression()->named_object() == this->no_)
2823 break;
2824 return TRAVERSE_CONTINUE;
2825 default:
2826 return TRAVERSE_CONTINUE;
2828 this->found_ = true;
2829 return TRAVERSE_EXIT;
2832 // The nil value.
2834 class Nil_expression : public Expression
2836 public:
2837 Nil_expression(source_location location)
2838 : Expression(EXPRESSION_NIL, location)
2841 static Expression*
2842 do_import(Import*);
2844 protected:
2845 bool
2846 do_is_constant() const
2847 { return true; }
2849 Type*
2850 do_type()
2851 { return Type::make_nil_type(); }
2853 void
2854 do_determine_type(const Type_context*)
2857 Expression*
2858 do_copy()
2859 { return this; }
2861 tree
2862 do_get_tree(Translate_context*)
2863 { return null_pointer_node; }
2865 void
2866 do_export(Export* exp) const
2867 { exp->write_c_string("nil"); }
2870 // Import a nil expression.
2872 Expression*
2873 Nil_expression::do_import(Import* imp)
2875 imp->require_c_string("nil");
2876 return Expression::make_nil(imp->location());
2879 // Make a nil expression.
2881 Expression*
2882 Expression::make_nil(source_location location)
2884 return new Nil_expression(location);
2887 // The value of the predeclared constant iota. This is little more
2888 // than a marker. This will be lowered to an integer in
2889 // Const_expression::do_lower, which is where we know the value that
2890 // it should have.
2892 class Iota_expression : public Parser_expression
2894 public:
2895 Iota_expression(source_location location)
2896 : Parser_expression(EXPRESSION_IOTA, location)
2899 protected:
2900 Expression*
2901 do_lower(Gogo*, Named_object*, int)
2902 { gcc_unreachable(); }
2904 // There should only ever be one of these.
2905 Expression*
2906 do_copy()
2907 { gcc_unreachable(); }
2910 // Make an iota expression. This is only called for one case: the
2911 // value of the predeclared constant iota.
2913 Expression*
2914 Expression::make_iota()
2916 static Iota_expression iota_expression(UNKNOWN_LOCATION);
2917 return &iota_expression;
2920 // A type conversion expression.
2922 class Type_conversion_expression : public Expression
2924 public:
2925 Type_conversion_expression(Type* type, Expression* expr,
2926 source_location location)
2927 : Expression(EXPRESSION_CONVERSION, location),
2928 type_(type), expr_(expr), may_convert_function_types_(false)
2931 // Return the type to which we are converting.
2932 Type*
2933 type() const
2934 { return this->type_; }
2936 // Return the expression which we are converting.
2937 Expression*
2938 expr() const
2939 { return this->expr_; }
2941 // Permit converting from one function type to another. This is
2942 // used internally for method expressions.
2943 void
2944 set_may_convert_function_types()
2946 this->may_convert_function_types_ = true;
2949 // Import a type conversion expression.
2950 static Expression*
2951 do_import(Import*);
2953 protected:
2955 do_traverse(Traverse* traverse);
2957 Expression*
2958 do_lower(Gogo*, Named_object*, int);
2960 bool
2961 do_is_constant() const
2962 { return this->expr_->is_constant(); }
2964 bool
2965 do_integer_constant_value(bool, mpz_t, Type**) const;
2967 bool
2968 do_float_constant_value(mpfr_t, Type**) const;
2970 bool
2971 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
2973 bool
2974 do_string_constant_value(std::string*) const;
2976 Type*
2977 do_type()
2978 { return this->type_; }
2980 void
2981 do_determine_type(const Type_context*)
2983 Type_context subcontext(this->type_, false);
2984 this->expr_->determine_type(&subcontext);
2987 void
2988 do_check_types(Gogo*);
2990 Expression*
2991 do_copy()
2993 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2994 this->location());
2997 tree
2998 do_get_tree(Translate_context* context);
3000 void
3001 do_export(Export*) const;
3003 private:
3004 // The type to convert to.
3005 Type* type_;
3006 // The expression to convert.
3007 Expression* expr_;
3008 // True if this is permitted to convert function types. This is
3009 // used internally for method expressions.
3010 bool may_convert_function_types_;
3013 // Traversal.
3016 Type_conversion_expression::do_traverse(Traverse* traverse)
3018 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3019 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3020 return TRAVERSE_EXIT;
3021 return TRAVERSE_CONTINUE;
3024 // Convert to a constant at lowering time.
3026 Expression*
3027 Type_conversion_expression::do_lower(Gogo*, Named_object*, int)
3029 Type* type = this->type_;
3030 Expression* val = this->expr_;
3031 source_location location = this->location();
3033 if (type->integer_type() != NULL)
3035 mpz_t ival;
3036 mpz_init(ival);
3037 Type* dummy;
3038 if (val->integer_constant_value(false, ival, &dummy))
3040 if (!Integer_expression::check_constant(ival, type, location))
3041 mpz_set_ui(ival, 0);
3042 Expression* ret = Expression::make_integer(&ival, type, location);
3043 mpz_clear(ival);
3044 return ret;
3047 mpfr_t fval;
3048 mpfr_init(fval);
3049 if (val->float_constant_value(fval, &dummy))
3051 if (!mpfr_integer_p(fval))
3053 error_at(location,
3054 "floating point constant truncated to integer");
3055 return Expression::make_error(location);
3057 mpfr_get_z(ival, fval, GMP_RNDN);
3058 if (!Integer_expression::check_constant(ival, type, location))
3059 mpz_set_ui(ival, 0);
3060 Expression* ret = Expression::make_integer(&ival, type, location);
3061 mpfr_clear(fval);
3062 mpz_clear(ival);
3063 return ret;
3065 mpfr_clear(fval);
3066 mpz_clear(ival);
3069 if (type->float_type() != NULL)
3071 mpfr_t fval;
3072 mpfr_init(fval);
3073 Type* dummy;
3074 if (val->float_constant_value(fval, &dummy))
3076 if (!Float_expression::check_constant(fval, type, location))
3077 mpfr_set_ui(fval, 0, GMP_RNDN);
3078 Float_expression::constrain_float(fval, type);
3079 Expression *ret = Expression::make_float(&fval, type, location);
3080 mpfr_clear(fval);
3081 return ret;
3083 mpfr_clear(fval);
3086 if (type->complex_type() != NULL)
3088 mpfr_t real;
3089 mpfr_t imag;
3090 mpfr_init(real);
3091 mpfr_init(imag);
3092 Type* dummy;
3093 if (val->complex_constant_value(real, imag, &dummy))
3095 if (!Complex_expression::check_constant(real, imag, type, location))
3097 mpfr_set_ui(real, 0, GMP_RNDN);
3098 mpfr_set_ui(imag, 0, GMP_RNDN);
3100 Complex_expression::constrain_complex(real, imag, type);
3101 Expression* ret = Expression::make_complex(&real, &imag, type,
3102 location);
3103 mpfr_clear(real);
3104 mpfr_clear(imag);
3105 return ret;
3107 mpfr_clear(real);
3108 mpfr_clear(imag);
3111 if (type->is_open_array_type() && type->named_type() == NULL)
3113 Type* element_type = type->array_type()->element_type()->forwarded();
3114 bool is_byte = element_type == Type::lookup_integer_type("uint8");
3115 bool is_int = element_type == Type::lookup_integer_type("int");
3116 if (is_byte || is_int)
3118 std::string s;
3119 if (val->string_constant_value(&s))
3121 Expression_list* vals = new Expression_list();
3122 if (is_byte)
3124 for (std::string::const_iterator p = s.begin();
3125 p != s.end();
3126 p++)
3128 mpz_t val;
3129 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3130 Expression* v = Expression::make_integer(&val,
3131 element_type,
3132 location);
3133 vals->push_back(v);
3134 mpz_clear(val);
3137 else
3139 const char *p = s.data();
3140 const char *pend = s.data() + s.length();
3141 while (p < pend)
3143 unsigned int c;
3144 int adv = Lex::fetch_char(p, &c);
3145 if (adv == 0)
3147 warning_at(this->location(), 0,
3148 "invalid UTF-8 encoding");
3149 adv = 1;
3151 p += adv;
3152 mpz_t val;
3153 mpz_init_set_ui(val, c);
3154 Expression* v = Expression::make_integer(&val,
3155 element_type,
3156 location);
3157 vals->push_back(v);
3158 mpz_clear(val);
3162 return Expression::make_slice_composite_literal(type, vals,
3163 location);
3168 return this;
3171 // Return the constant integer value if there is one.
3173 bool
3174 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3175 mpz_t val,
3176 Type** ptype) const
3178 if (this->type_->integer_type() == NULL)
3179 return false;
3181 mpz_t ival;
3182 mpz_init(ival);
3183 Type* dummy;
3184 if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3186 if (!Integer_expression::check_constant(ival, this->type_,
3187 this->location()))
3189 mpz_clear(ival);
3190 return false;
3192 mpz_set(val, ival);
3193 mpz_clear(ival);
3194 *ptype = this->type_;
3195 return true;
3197 mpz_clear(ival);
3199 mpfr_t fval;
3200 mpfr_init(fval);
3201 if (this->expr_->float_constant_value(fval, &dummy))
3203 mpfr_get_z(val, fval, GMP_RNDN);
3204 mpfr_clear(fval);
3205 if (!Integer_expression::check_constant(val, this->type_,
3206 this->location()))
3207 return false;
3208 *ptype = this->type_;
3209 return true;
3211 mpfr_clear(fval);
3213 return false;
3216 // Return the constant floating point value if there is one.
3218 bool
3219 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3220 Type** ptype) const
3222 if (this->type_->float_type() == NULL)
3223 return false;
3225 mpfr_t fval;
3226 mpfr_init(fval);
3227 Type* dummy;
3228 if (this->expr_->float_constant_value(fval, &dummy))
3230 if (!Float_expression::check_constant(fval, this->type_,
3231 this->location()))
3233 mpfr_clear(fval);
3234 return false;
3236 mpfr_set(val, fval, GMP_RNDN);
3237 mpfr_clear(fval);
3238 Float_expression::constrain_float(val, this->type_);
3239 *ptype = this->type_;
3240 return true;
3242 mpfr_clear(fval);
3244 return false;
3247 // Return the constant complex value if there is one.
3249 bool
3250 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3251 mpfr_t imag,
3252 Type **ptype) const
3254 if (this->type_->complex_type() == NULL)
3255 return false;
3257 mpfr_t rval;
3258 mpfr_t ival;
3259 mpfr_init(rval);
3260 mpfr_init(ival);
3261 Type* dummy;
3262 if (this->expr_->complex_constant_value(rval, ival, &dummy))
3264 if (!Complex_expression::check_constant(rval, ival, this->type_,
3265 this->location()))
3267 mpfr_clear(rval);
3268 mpfr_clear(ival);
3269 return false;
3271 mpfr_set(real, rval, GMP_RNDN);
3272 mpfr_set(imag, ival, GMP_RNDN);
3273 mpfr_clear(rval);
3274 mpfr_clear(ival);
3275 Complex_expression::constrain_complex(real, imag, this->type_);
3276 *ptype = this->type_;
3277 return true;
3279 mpfr_clear(rval);
3280 mpfr_clear(ival);
3282 return false;
3285 // Return the constant string value if there is one.
3287 bool
3288 Type_conversion_expression::do_string_constant_value(std::string* val) const
3290 if (this->type_->is_string_type()
3291 && this->expr_->type()->integer_type() != NULL)
3293 mpz_t ival;
3294 mpz_init(ival);
3295 Type* dummy;
3296 if (this->expr_->integer_constant_value(false, ival, &dummy))
3298 unsigned long ulval = mpz_get_ui(ival);
3299 if (mpz_cmp_ui(ival, ulval) == 0)
3301 Lex::append_char(ulval, true, val, this->location());
3302 mpz_clear(ival);
3303 return true;
3306 mpz_clear(ival);
3309 // FIXME: Could handle conversion from const []int here.
3311 return false;
3314 // Check that types are convertible.
3316 void
3317 Type_conversion_expression::do_check_types(Gogo*)
3319 Type* type = this->type_;
3320 Type* expr_type = this->expr_->type();
3321 std::string reason;
3323 if (type->is_error() || expr_type->is_error())
3325 this->set_is_error();
3326 return;
3329 if (this->may_convert_function_types_
3330 && type->function_type() != NULL
3331 && expr_type->function_type() != NULL)
3332 return;
3334 if (Type::are_convertible(type, expr_type, &reason))
3335 return;
3337 error_at(this->location(), "%s", reason.c_str());
3338 this->set_is_error();
3341 // Get a tree for a type conversion.
3343 tree
3344 Type_conversion_expression::do_get_tree(Translate_context* context)
3346 Gogo* gogo = context->gogo();
3347 tree type_tree = this->type_->get_tree(gogo);
3348 tree expr_tree = this->expr_->get_tree(context);
3350 if (type_tree == error_mark_node
3351 || expr_tree == error_mark_node
3352 || TREE_TYPE(expr_tree) == error_mark_node)
3353 return error_mark_node;
3355 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3356 return fold_convert(type_tree, expr_tree);
3358 Type* type = this->type_;
3359 Type* expr_type = this->expr_->type();
3360 tree ret;
3361 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3362 ret = Expression::convert_for_assignment(context, type, expr_type,
3363 expr_tree, this->location());
3364 else if (type->integer_type() != NULL)
3366 if (expr_type->integer_type() != NULL
3367 || expr_type->float_type() != NULL
3368 || expr_type->is_unsafe_pointer_type())
3369 ret = fold(convert_to_integer(type_tree, expr_tree));
3370 else
3371 gcc_unreachable();
3373 else if (type->float_type() != NULL)
3375 if (expr_type->integer_type() != NULL
3376 || expr_type->float_type() != NULL)
3377 ret = fold(convert_to_real(type_tree, expr_tree));
3378 else
3379 gcc_unreachable();
3381 else if (type->complex_type() != NULL)
3383 if (expr_type->complex_type() != NULL)
3384 ret = fold(convert_to_complex(type_tree, expr_tree));
3385 else
3386 gcc_unreachable();
3388 else if (type->is_string_type()
3389 && expr_type->integer_type() != NULL)
3391 expr_tree = fold_convert(integer_type_node, expr_tree);
3392 if (host_integerp(expr_tree, 0))
3394 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3395 std::string s;
3396 Lex::append_char(intval, true, &s, this->location());
3397 Expression* se = Expression::make_string(s, this->location());
3398 return se->get_tree(context);
3401 static tree int_to_string_fndecl;
3402 ret = Gogo::call_builtin(&int_to_string_fndecl,
3403 this->location(),
3404 "__go_int_to_string",
3406 type_tree,
3407 integer_type_node,
3408 fold_convert(integer_type_node, expr_tree));
3410 else if (type->is_string_type()
3411 && (expr_type->array_type() != NULL
3412 || (expr_type->points_to() != NULL
3413 && expr_type->points_to()->array_type() != NULL)))
3415 Type* t = expr_type;
3416 if (t->points_to() != NULL)
3418 t = t->points_to();
3419 expr_tree = build_fold_indirect_ref(expr_tree);
3421 if (!DECL_P(expr_tree))
3422 expr_tree = save_expr(expr_tree);
3423 Array_type* a = t->array_type();
3424 Type* e = a->element_type()->forwarded();
3425 gcc_assert(e->integer_type() != NULL);
3426 tree valptr = fold_convert(const_ptr_type_node,
3427 a->value_pointer_tree(gogo, expr_tree));
3428 tree len = a->length_tree(gogo, expr_tree);
3429 len = fold_convert_loc(this->location(), integer_type_node, len);
3430 if (e->integer_type()->is_unsigned()
3431 && e->integer_type()->bits() == 8)
3433 static tree byte_array_to_string_fndecl;
3434 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3435 this->location(),
3436 "__go_byte_array_to_string",
3438 type_tree,
3439 const_ptr_type_node,
3440 valptr,
3441 integer_type_node,
3442 len);
3444 else
3446 gcc_assert(e == Type::lookup_integer_type("int"));
3447 static tree int_array_to_string_fndecl;
3448 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3449 this->location(),
3450 "__go_int_array_to_string",
3452 type_tree,
3453 const_ptr_type_node,
3454 valptr,
3455 integer_type_node,
3456 len);
3459 else if (type->is_open_array_type() && expr_type->is_string_type())
3461 Type* e = type->array_type()->element_type()->forwarded();
3462 gcc_assert(e->integer_type() != NULL);
3463 if (e->integer_type()->is_unsigned()
3464 && e->integer_type()->bits() == 8)
3466 static tree string_to_byte_array_fndecl;
3467 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3468 this->location(),
3469 "__go_string_to_byte_array",
3471 type_tree,
3472 TREE_TYPE(expr_tree),
3473 expr_tree);
3475 else
3477 gcc_assert(e == Type::lookup_integer_type("int"));
3478 static tree string_to_int_array_fndecl;
3479 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3480 this->location(),
3481 "__go_string_to_int_array",
3483 type_tree,
3484 TREE_TYPE(expr_tree),
3485 expr_tree);
3488 else if ((type->is_unsafe_pointer_type()
3489 && expr_type->points_to() != NULL)
3490 || (expr_type->is_unsafe_pointer_type()
3491 && type->points_to() != NULL))
3492 ret = fold_convert(type_tree, expr_tree);
3493 else if (type->is_unsafe_pointer_type()
3494 && expr_type->integer_type() != NULL)
3495 ret = convert_to_pointer(type_tree, expr_tree);
3496 else if (this->may_convert_function_types_
3497 && type->function_type() != NULL
3498 && expr_type->function_type() != NULL)
3499 ret = fold_convert_loc(this->location(), type_tree, expr_tree);
3500 else
3501 ret = Expression::convert_for_assignment(context, type, expr_type,
3502 expr_tree, this->location());
3504 return ret;
3507 // Output a type conversion in a constant expression.
3509 void
3510 Type_conversion_expression::do_export(Export* exp) const
3512 exp->write_c_string("convert(");
3513 exp->write_type(this->type_);
3514 exp->write_c_string(", ");
3515 this->expr_->export_expression(exp);
3516 exp->write_c_string(")");
3519 // Import a type conversion or a struct construction.
3521 Expression*
3522 Type_conversion_expression::do_import(Import* imp)
3524 imp->require_c_string("convert(");
3525 Type* type = imp->read_type();
3526 imp->require_c_string(", ");
3527 Expression* val = Expression::import_expression(imp);
3528 imp->require_c_string(")");
3529 return Expression::make_cast(type, val, imp->location());
3532 // Make a type cast expression.
3534 Expression*
3535 Expression::make_cast(Type* type, Expression* val, source_location location)
3537 if (type->is_error_type() || val->is_error_expression())
3538 return Expression::make_error(location);
3539 return new Type_conversion_expression(type, val, location);
3542 // An unsafe type conversion, used to pass values to builtin functions.
3544 class Unsafe_type_conversion_expression : public Expression
3546 public:
3547 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3548 source_location location)
3549 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3550 type_(type), expr_(expr)
3553 protected:
3555 do_traverse(Traverse* traverse);
3557 Type*
3558 do_type()
3559 { return this->type_; }
3561 void
3562 do_determine_type(const Type_context*)
3565 Expression*
3566 do_copy()
3568 return new Unsafe_type_conversion_expression(this->type_,
3569 this->expr_->copy(),
3570 this->location());
3573 tree
3574 do_get_tree(Translate_context*);
3576 private:
3577 // The type to convert to.
3578 Type* type_;
3579 // The expression to convert.
3580 Expression* expr_;
3583 // Traversal.
3586 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3588 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3589 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3590 return TRAVERSE_EXIT;
3591 return TRAVERSE_CONTINUE;
3594 // Convert to backend representation.
3596 tree
3597 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3599 // We are only called for a limited number of cases.
3601 Type* t = this->type_;
3602 Type* et = this->expr_->type();
3604 tree type_tree = this->type_->get_tree(context->gogo());
3605 tree expr_tree = this->expr_->get_tree(context);
3606 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3607 return error_mark_node;
3609 source_location loc = this->location();
3611 bool use_view_convert = false;
3612 if (t->is_open_array_type())
3614 gcc_assert(et->is_open_array_type());
3615 use_view_convert = true;
3617 else if (t->map_type() != NULL)
3618 gcc_assert(et->map_type() != NULL);
3619 else if (t->channel_type() != NULL)
3620 gcc_assert(et->channel_type() != NULL);
3621 else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL)
3622 gcc_assert((et->points_to() != NULL
3623 && et->points_to()->channel_type() != NULL)
3624 || et->is_nil_type());
3625 else if (t->is_unsafe_pointer_type())
3626 gcc_assert(et->points_to() != NULL || et->is_nil_type());
3627 else if (et->is_unsafe_pointer_type())
3628 gcc_assert(t->points_to() != NULL);
3629 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3631 gcc_assert(et->interface_type() != NULL
3632 && !et->interface_type()->is_empty());
3633 use_view_convert = true;
3635 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3637 gcc_assert(et->interface_type() != NULL
3638 && et->interface_type()->is_empty());
3639 use_view_convert = true;
3641 else if (t->integer_type() != NULL)
3643 gcc_assert(et->is_boolean_type()
3644 || et->integer_type() != NULL
3645 || et->function_type() != NULL
3646 || et->points_to() != NULL
3647 || et->map_type() != NULL
3648 || et->channel_type() != NULL);
3649 return convert_to_integer(type_tree, expr_tree);
3651 else
3652 gcc_unreachable();
3654 if (use_view_convert)
3655 return fold_build1_loc(loc, VIEW_CONVERT_EXPR, type_tree, expr_tree);
3656 else
3657 return fold_convert_loc(loc, type_tree, expr_tree);
3660 // Make an unsafe type conversion expression.
3662 Expression*
3663 Expression::make_unsafe_cast(Type* type, Expression* expr,
3664 source_location location)
3666 return new Unsafe_type_conversion_expression(type, expr, location);
3669 // Unary expressions.
3671 class Unary_expression : public Expression
3673 public:
3674 Unary_expression(Operator op, Expression* expr, source_location location)
3675 : Expression(EXPRESSION_UNARY, location),
3676 op_(op), escapes_(true), expr_(expr)
3679 // Return the operator.
3680 Operator
3681 op() const
3682 { return this->op_; }
3684 // Return the operand.
3685 Expression*
3686 operand() const
3687 { return this->expr_; }
3689 // Record that an address expression does not escape.
3690 void
3691 set_does_not_escape()
3693 gcc_assert(this->op_ == OPERATOR_AND);
3694 this->escapes_ = false;
3697 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3698 // could be done, false if not.
3699 static bool
3700 eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3701 source_location);
3703 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3704 // could be done, false if not.
3705 static bool
3706 eval_float(Operator op, mpfr_t uval, mpfr_t val);
3708 // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG. Return
3709 // true if this could be done, false if not.
3710 static bool
3711 eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3712 mpfr_t imag);
3714 static Expression*
3715 do_import(Import*);
3717 protected:
3719 do_traverse(Traverse* traverse)
3720 { return Expression::traverse(&this->expr_, traverse); }
3722 Expression*
3723 do_lower(Gogo*, Named_object*, int);
3725 bool
3726 do_is_constant() const;
3728 bool
3729 do_integer_constant_value(bool, mpz_t, Type**) const;
3731 bool
3732 do_float_constant_value(mpfr_t, Type**) const;
3734 bool
3735 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3737 Type*
3738 do_type();
3740 void
3741 do_determine_type(const Type_context*);
3743 void
3744 do_check_types(Gogo*);
3746 Expression*
3747 do_copy()
3749 return Expression::make_unary(this->op_, this->expr_->copy(),
3750 this->location());
3753 bool
3754 do_is_addressable() const
3755 { return this->op_ == OPERATOR_MULT; }
3757 tree
3758 do_get_tree(Translate_context*);
3760 void
3761 do_export(Export*) const;
3763 private:
3764 // The unary operator to apply.
3765 Operator op_;
3766 // Normally true. False if this is an address expression which does
3767 // not escape the current function.
3768 bool escapes_;
3769 // The operand.
3770 Expression* expr_;
3773 // If we are taking the address of a composite literal, and the
3774 // contents are not constant, then we want to make a heap composite
3775 // instead.
3777 Expression*
3778 Unary_expression::do_lower(Gogo*, Named_object*, int)
3780 source_location loc = this->location();
3781 Operator op = this->op_;
3782 Expression* expr = this->expr_;
3784 if (op == OPERATOR_MULT && expr->is_type_expression())
3785 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3787 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3788 // moving x to the heap. FIXME: Is it worth doing a real escape
3789 // analysis here? This case is found in math/unsafe.go and is
3790 // therefore worth special casing.
3791 if (op == OPERATOR_MULT)
3793 Expression* e = expr;
3794 while (e->classification() == EXPRESSION_CONVERSION)
3796 Type_conversion_expression* te
3797 = static_cast<Type_conversion_expression*>(e);
3798 e = te->expr();
3801 if (e->classification() == EXPRESSION_UNARY)
3803 Unary_expression* ue = static_cast<Unary_expression*>(e);
3804 if (ue->op_ == OPERATOR_AND)
3806 if (e == expr)
3808 // *&x == x.
3809 return ue->expr_;
3811 ue->set_does_not_escape();
3816 // Catching an invalid indirection of unsafe.Pointer here avoid
3817 // having to deal with TYPE_VOID in other places.
3818 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3820 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3821 return Expression::make_error(this->location());
3824 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
3825 || op == OPERATOR_NOT || op == OPERATOR_XOR)
3827 Expression* ret = NULL;
3829 mpz_t eval;
3830 mpz_init(eval);
3831 Type* etype;
3832 if (expr->integer_constant_value(false, eval, &etype))
3834 mpz_t val;
3835 mpz_init(val);
3836 if (Unary_expression::eval_integer(op, etype, eval, val, loc))
3837 ret = Expression::make_integer(&val, etype, loc);
3838 mpz_clear(val);
3840 mpz_clear(eval);
3841 if (ret != NULL)
3842 return ret;
3844 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
3846 mpfr_t fval;
3847 mpfr_init(fval);
3848 Type* ftype;
3849 if (expr->float_constant_value(fval, &ftype))
3851 mpfr_t val;
3852 mpfr_init(val);
3853 if (Unary_expression::eval_float(op, fval, val))
3854 ret = Expression::make_float(&val, ftype, loc);
3855 mpfr_clear(val);
3857 if (ret != NULL)
3859 mpfr_clear(fval);
3860 return ret;
3863 mpfr_t ival;
3864 mpfr_init(ival);
3865 if (expr->complex_constant_value(fval, ival, &ftype))
3867 mpfr_t real;
3868 mpfr_t imag;
3869 mpfr_init(real);
3870 mpfr_init(imag);
3871 if (Unary_expression::eval_complex(op, fval, ival, real, imag))
3872 ret = Expression::make_complex(&real, &imag, ftype, loc);
3873 mpfr_clear(real);
3874 mpfr_clear(imag);
3876 mpfr_clear(ival);
3877 mpfr_clear(fval);
3878 if (ret != NULL)
3879 return ret;
3883 return this;
3886 // Return whether a unary expression is a constant.
3888 bool
3889 Unary_expression::do_is_constant() const
3891 if (this->op_ == OPERATOR_MULT)
3893 // Indirecting through a pointer is only constant if the object
3894 // to which the expression points is constant, but we currently
3895 // have no way to determine that.
3896 return false;
3898 else if (this->op_ == OPERATOR_AND)
3900 // Taking the address of a variable is constant if it is a
3901 // global variable, not constant otherwise. In other cases
3902 // taking the address is probably not a constant.
3903 Var_expression* ve = this->expr_->var_expression();
3904 if (ve != NULL)
3906 Named_object* no = ve->named_object();
3907 return no->is_variable() && no->var_value()->is_global();
3909 return false;
3911 else
3912 return this->expr_->is_constant();
3915 // Apply unary opcode OP to UVAL, setting VAL. UTYPE is the type of
3916 // UVAL, if known; it may be NULL. Return true if this could be done,
3917 // false if not.
3919 bool
3920 Unary_expression::eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3921 source_location location)
3923 switch (op)
3925 case OPERATOR_PLUS:
3926 mpz_set(val, uval);
3927 return true;
3928 case OPERATOR_MINUS:
3929 mpz_neg(val, uval);
3930 return Integer_expression::check_constant(val, utype, location);
3931 case OPERATOR_NOT:
3932 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3933 return true;
3934 case OPERATOR_XOR:
3935 if (utype == NULL
3936 || utype->integer_type() == NULL
3937 || utype->integer_type()->is_abstract())
3938 mpz_com(val, uval);
3939 else
3941 // The number of HOST_WIDE_INTs that it takes to represent
3942 // UVAL.
3943 size_t count = ((mpz_sizeinbase(uval, 2)
3944 + HOST_BITS_PER_WIDE_INT
3945 - 1)
3946 / HOST_BITS_PER_WIDE_INT);
3948 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3949 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3951 size_t ecount;
3952 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3953 gcc_assert(ecount <= count);
3955 // Trim down to the number of words required by the type.
3956 size_t obits = utype->integer_type()->bits();
3957 if (!utype->integer_type()->is_unsigned())
3958 ++obits;
3959 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3960 / HOST_BITS_PER_WIDE_INT);
3961 gcc_assert(ocount <= count);
3963 for (size_t i = 0; i < ocount; ++i)
3964 phwi[i] = ~phwi[i];
3966 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3967 if (clearbits != 0)
3968 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3969 >> clearbits);
3971 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3973 delete[] phwi;
3975 return Integer_expression::check_constant(val, utype, location);
3976 case OPERATOR_AND:
3977 case OPERATOR_MULT:
3978 return false;
3979 default:
3980 gcc_unreachable();
3984 // Apply unary opcode OP to UVAL, setting VAL. Return true if this
3985 // could be done, false if not.
3987 bool
3988 Unary_expression::eval_float(Operator op, mpfr_t uval, mpfr_t val)
3990 switch (op)
3992 case OPERATOR_PLUS:
3993 mpfr_set(val, uval, GMP_RNDN);
3994 return true;
3995 case OPERATOR_MINUS:
3996 mpfr_neg(val, uval, GMP_RNDN);
3997 return true;
3998 case OPERATOR_NOT:
3999 case OPERATOR_XOR:
4000 case OPERATOR_AND:
4001 case OPERATOR_MULT:
4002 return false;
4003 default:
4004 gcc_unreachable();
4008 // Apply unary opcode OP to RVAL/IVAL, setting REAL/IMAG. Return true
4009 // if this could be done, false if not.
4011 bool
4012 Unary_expression::eval_complex(Operator op, mpfr_t rval, mpfr_t ival,
4013 mpfr_t real, mpfr_t imag)
4015 switch (op)
4017 case OPERATOR_PLUS:
4018 mpfr_set(real, rval, GMP_RNDN);
4019 mpfr_set(imag, ival, GMP_RNDN);
4020 return true;
4021 case OPERATOR_MINUS:
4022 mpfr_neg(real, rval, GMP_RNDN);
4023 mpfr_neg(imag, ival, GMP_RNDN);
4024 return true;
4025 case OPERATOR_NOT:
4026 case OPERATOR_XOR:
4027 case OPERATOR_AND:
4028 case OPERATOR_MULT:
4029 return false;
4030 default:
4031 gcc_unreachable();
4035 // Return the integral constant value of a unary expression, if it has one.
4037 bool
4038 Unary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
4039 Type** ptype) const
4041 mpz_t uval;
4042 mpz_init(uval);
4043 bool ret;
4044 if (!this->expr_->integer_constant_value(iota_is_constant, uval, ptype))
4045 ret = false;
4046 else
4047 ret = Unary_expression::eval_integer(this->op_, *ptype, uval, val,
4048 this->location());
4049 mpz_clear(uval);
4050 return ret;
4053 // Return the floating point constant value of a unary expression, if
4054 // it has one.
4056 bool
4057 Unary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
4059 mpfr_t uval;
4060 mpfr_init(uval);
4061 bool ret;
4062 if (!this->expr_->float_constant_value(uval, ptype))
4063 ret = false;
4064 else
4065 ret = Unary_expression::eval_float(this->op_, uval, val);
4066 mpfr_clear(uval);
4067 return ret;
4070 // Return the complex constant value of a unary expression, if it has
4071 // one.
4073 bool
4074 Unary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
4075 Type** ptype) const
4077 mpfr_t rval;
4078 mpfr_t ival;
4079 mpfr_init(rval);
4080 mpfr_init(ival);
4081 bool ret;
4082 if (!this->expr_->complex_constant_value(rval, ival, ptype))
4083 ret = false;
4084 else
4085 ret = Unary_expression::eval_complex(this->op_, rval, ival, real, imag);
4086 mpfr_clear(rval);
4087 mpfr_clear(ival);
4088 return ret;
4091 // Return the type of a unary expression.
4093 Type*
4094 Unary_expression::do_type()
4096 switch (this->op_)
4098 case OPERATOR_PLUS:
4099 case OPERATOR_MINUS:
4100 case OPERATOR_NOT:
4101 case OPERATOR_XOR:
4102 return this->expr_->type();
4104 case OPERATOR_AND:
4105 return Type::make_pointer_type(this->expr_->type());
4107 case OPERATOR_MULT:
4109 Type* subtype = this->expr_->type();
4110 Type* points_to = subtype->points_to();
4111 if (points_to == NULL)
4112 return Type::make_error_type();
4113 return points_to;
4116 default:
4117 gcc_unreachable();
4121 // Determine abstract types for a unary expression.
4123 void
4124 Unary_expression::do_determine_type(const Type_context* context)
4126 switch (this->op_)
4128 case OPERATOR_PLUS:
4129 case OPERATOR_MINUS:
4130 case OPERATOR_NOT:
4131 case OPERATOR_XOR:
4132 this->expr_->determine_type(context);
4133 break;
4135 case OPERATOR_AND:
4136 // Taking the address of something.
4138 Type* subtype = (context->type == NULL
4139 ? NULL
4140 : context->type->points_to());
4141 Type_context subcontext(subtype, false);
4142 this->expr_->determine_type(&subcontext);
4144 break;
4146 case OPERATOR_MULT:
4147 // Indirecting through a pointer.
4149 Type* subtype = (context->type == NULL
4150 ? NULL
4151 : Type::make_pointer_type(context->type));
4152 Type_context subcontext(subtype, false);
4153 this->expr_->determine_type(&subcontext);
4155 break;
4157 default:
4158 gcc_unreachable();
4162 // Check types for a unary expression.
4164 void
4165 Unary_expression::do_check_types(Gogo*)
4167 Type* type = this->expr_->type();
4168 if (type->is_error())
4170 this->set_is_error();
4171 return;
4174 switch (this->op_)
4176 case OPERATOR_PLUS:
4177 case OPERATOR_MINUS:
4178 if (type->integer_type() == NULL
4179 && type->float_type() == NULL
4180 && type->complex_type() == NULL)
4181 this->report_error(_("expected numeric type"));
4182 break;
4184 case OPERATOR_NOT:
4185 case OPERATOR_XOR:
4186 if (type->integer_type() == NULL
4187 && !type->is_boolean_type())
4188 this->report_error(_("expected integer or boolean type"));
4189 break;
4191 case OPERATOR_AND:
4192 if (!this->expr_->is_addressable())
4193 this->report_error(_("invalid operand for unary %<&%>"));
4194 else
4195 this->expr_->address_taken(this->escapes_);
4196 break;
4198 case OPERATOR_MULT:
4199 // Indirecting through a pointer.
4200 if (type->points_to() == NULL)
4201 this->report_error(_("expected pointer"));
4202 break;
4204 default:
4205 gcc_unreachable();
4209 // Get a tree for a unary expression.
4211 tree
4212 Unary_expression::do_get_tree(Translate_context* context)
4214 tree expr = this->expr_->get_tree(context);
4215 if (expr == error_mark_node)
4216 return error_mark_node;
4218 source_location loc = this->location();
4219 switch (this->op_)
4221 case OPERATOR_PLUS:
4222 return expr;
4224 case OPERATOR_MINUS:
4226 tree type = TREE_TYPE(expr);
4227 tree compute_type = excess_precision_type(type);
4228 if (compute_type != NULL_TREE)
4229 expr = ::convert(compute_type, expr);
4230 tree ret = fold_build1_loc(loc, NEGATE_EXPR,
4231 (compute_type != NULL_TREE
4232 ? compute_type
4233 : type),
4234 expr);
4235 if (compute_type != NULL_TREE)
4236 ret = ::convert(type, ret);
4237 return ret;
4240 case OPERATOR_NOT:
4241 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
4242 return fold_build1_loc(loc, TRUTH_NOT_EXPR, TREE_TYPE(expr), expr);
4243 else
4244 return fold_build2_loc(loc, NE_EXPR, boolean_type_node, expr,
4245 build_int_cst(TREE_TYPE(expr), 0));
4247 case OPERATOR_XOR:
4248 return fold_build1_loc(loc, BIT_NOT_EXPR, TREE_TYPE(expr), expr);
4250 case OPERATOR_AND:
4251 // We should not see a non-constant constructor here; cases
4252 // where we would see one should have been moved onto the heap
4253 // at parse time. Taking the address of a nonconstant
4254 // constructor will not do what the programmer expects.
4255 gcc_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4256 gcc_assert(TREE_CODE(expr) != ADDR_EXPR);
4258 // Build a decl for a constant constructor.
4259 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4261 tree decl = build_decl(this->location(), VAR_DECL,
4262 create_tmp_var_name("C"), TREE_TYPE(expr));
4263 DECL_EXTERNAL(decl) = 0;
4264 TREE_PUBLIC(decl) = 0;
4265 TREE_READONLY(decl) = 1;
4266 TREE_CONSTANT(decl) = 1;
4267 TREE_STATIC(decl) = 1;
4268 TREE_ADDRESSABLE(decl) = 1;
4269 DECL_ARTIFICIAL(decl) = 1;
4270 DECL_INITIAL(decl) = expr;
4271 rest_of_decl_compilation(decl, 1, 0);
4272 expr = decl;
4275 return build_fold_addr_expr_loc(loc, expr);
4277 case OPERATOR_MULT:
4279 gcc_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4281 // If we are dereferencing the pointer to a large struct, we
4282 // need to check for nil. We don't bother to check for small
4283 // structs because we expect the system to crash on a nil
4284 // pointer dereference.
4285 HOST_WIDE_INT s = int_size_in_bytes(TREE_TYPE(TREE_TYPE(expr)));
4286 if (s == -1 || s >= 4096)
4288 if (!DECL_P(expr))
4289 expr = save_expr(expr);
4290 tree compare = fold_build2_loc(loc, EQ_EXPR, boolean_type_node,
4291 expr,
4292 fold_convert(TREE_TYPE(expr),
4293 null_pointer_node));
4294 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4295 loc);
4296 expr = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(expr),
4297 build3(COND_EXPR, void_type_node,
4298 compare, crash, NULL_TREE),
4299 expr);
4302 // If the type of EXPR is a recursive pointer type, then we
4303 // need to insert a cast before indirecting.
4304 if (TREE_TYPE(TREE_TYPE(expr)) == ptr_type_node)
4306 Type* pt = this->expr_->type()->points_to();
4307 tree ind = pt->get_tree(context->gogo());
4308 expr = fold_convert_loc(loc, build_pointer_type(ind), expr);
4311 return build_fold_indirect_ref_loc(loc, expr);
4314 default:
4315 gcc_unreachable();
4319 // Export a unary expression.
4321 void
4322 Unary_expression::do_export(Export* exp) const
4324 switch (this->op_)
4326 case OPERATOR_PLUS:
4327 exp->write_c_string("+ ");
4328 break;
4329 case OPERATOR_MINUS:
4330 exp->write_c_string("- ");
4331 break;
4332 case OPERATOR_NOT:
4333 exp->write_c_string("! ");
4334 break;
4335 case OPERATOR_XOR:
4336 exp->write_c_string("^ ");
4337 break;
4338 case OPERATOR_AND:
4339 case OPERATOR_MULT:
4340 default:
4341 gcc_unreachable();
4343 this->expr_->export_expression(exp);
4346 // Import a unary expression.
4348 Expression*
4349 Unary_expression::do_import(Import* imp)
4351 Operator op;
4352 switch (imp->get_char())
4354 case '+':
4355 op = OPERATOR_PLUS;
4356 break;
4357 case '-':
4358 op = OPERATOR_MINUS;
4359 break;
4360 case '!':
4361 op = OPERATOR_NOT;
4362 break;
4363 case '^':
4364 op = OPERATOR_XOR;
4365 break;
4366 default:
4367 gcc_unreachable();
4369 imp->require_c_string(" ");
4370 Expression* expr = Expression::import_expression(imp);
4371 return Expression::make_unary(op, expr, imp->location());
4374 // Make a unary expression.
4376 Expression*
4377 Expression::make_unary(Operator op, Expression* expr, source_location location)
4379 return new Unary_expression(op, expr, location);
4382 // If this is an indirection through a pointer, return the expression
4383 // being pointed through. Otherwise return this.
4385 Expression*
4386 Expression::deref()
4388 if (this->classification_ == EXPRESSION_UNARY)
4390 Unary_expression* ue = static_cast<Unary_expression*>(this);
4391 if (ue->op() == OPERATOR_MULT)
4392 return ue->operand();
4394 return this;
4397 // Class Binary_expression.
4399 // Traversal.
4402 Binary_expression::do_traverse(Traverse* traverse)
4404 int t = Expression::traverse(&this->left_, traverse);
4405 if (t == TRAVERSE_EXIT)
4406 return TRAVERSE_EXIT;
4407 return Expression::traverse(&this->right_, traverse);
4410 // Compare integer constants according to OP.
4412 bool
4413 Binary_expression::compare_integer(Operator op, mpz_t left_val,
4414 mpz_t right_val)
4416 int i = mpz_cmp(left_val, right_val);
4417 switch (op)
4419 case OPERATOR_EQEQ:
4420 return i == 0;
4421 case OPERATOR_NOTEQ:
4422 return i != 0;
4423 case OPERATOR_LT:
4424 return i < 0;
4425 case OPERATOR_LE:
4426 return i <= 0;
4427 case OPERATOR_GT:
4428 return i > 0;
4429 case OPERATOR_GE:
4430 return i >= 0;
4431 default:
4432 gcc_unreachable();
4436 // Compare floating point constants according to OP.
4438 bool
4439 Binary_expression::compare_float(Operator op, Type* type, mpfr_t left_val,
4440 mpfr_t right_val)
4442 int i;
4443 if (type == NULL)
4444 i = mpfr_cmp(left_val, right_val);
4445 else
4447 mpfr_t lv;
4448 mpfr_init_set(lv, left_val, GMP_RNDN);
4449 mpfr_t rv;
4450 mpfr_init_set(rv, right_val, GMP_RNDN);
4451 Float_expression::constrain_float(lv, type);
4452 Float_expression::constrain_float(rv, type);
4453 i = mpfr_cmp(lv, rv);
4454 mpfr_clear(lv);
4455 mpfr_clear(rv);
4457 switch (op)
4459 case OPERATOR_EQEQ:
4460 return i == 0;
4461 case OPERATOR_NOTEQ:
4462 return i != 0;
4463 case OPERATOR_LT:
4464 return i < 0;
4465 case OPERATOR_LE:
4466 return i <= 0;
4467 case OPERATOR_GT:
4468 return i > 0;
4469 case OPERATOR_GE:
4470 return i >= 0;
4471 default:
4472 gcc_unreachable();
4476 // Compare complex constants according to OP. Complex numbers may
4477 // only be compared for equality.
4479 bool
4480 Binary_expression::compare_complex(Operator op, Type* type,
4481 mpfr_t left_real, mpfr_t left_imag,
4482 mpfr_t right_real, mpfr_t right_imag)
4484 bool is_equal;
4485 if (type == NULL)
4486 is_equal = (mpfr_cmp(left_real, right_real) == 0
4487 && mpfr_cmp(left_imag, right_imag) == 0);
4488 else
4490 mpfr_t lr;
4491 mpfr_t li;
4492 mpfr_init_set(lr, left_real, GMP_RNDN);
4493 mpfr_init_set(li, left_imag, GMP_RNDN);
4494 mpfr_t rr;
4495 mpfr_t ri;
4496 mpfr_init_set(rr, right_real, GMP_RNDN);
4497 mpfr_init_set(ri, right_imag, GMP_RNDN);
4498 Complex_expression::constrain_complex(lr, li, type);
4499 Complex_expression::constrain_complex(rr, ri, type);
4500 is_equal = mpfr_cmp(lr, rr) == 0 && mpfr_cmp(li, ri) == 0;
4501 mpfr_clear(lr);
4502 mpfr_clear(li);
4503 mpfr_clear(rr);
4504 mpfr_clear(ri);
4506 switch (op)
4508 case OPERATOR_EQEQ:
4509 return is_equal;
4510 case OPERATOR_NOTEQ:
4511 return !is_equal;
4512 default:
4513 gcc_unreachable();
4517 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4518 // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
4519 // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL. Return true if
4520 // this could be done, false if not.
4522 bool
4523 Binary_expression::eval_integer(Operator op, Type* left_type, mpz_t left_val,
4524 Type* right_type, mpz_t right_val,
4525 source_location location, mpz_t val)
4527 bool is_shift_op = false;
4528 switch (op)
4530 case OPERATOR_OROR:
4531 case OPERATOR_ANDAND:
4532 case OPERATOR_EQEQ:
4533 case OPERATOR_NOTEQ:
4534 case OPERATOR_LT:
4535 case OPERATOR_LE:
4536 case OPERATOR_GT:
4537 case OPERATOR_GE:
4538 // These return boolean values. We should probably handle them
4539 // anyhow in case a type conversion is used on the result.
4540 return false;
4541 case OPERATOR_PLUS:
4542 mpz_add(val, left_val, right_val);
4543 break;
4544 case OPERATOR_MINUS:
4545 mpz_sub(val, left_val, right_val);
4546 break;
4547 case OPERATOR_OR:
4548 mpz_ior(val, left_val, right_val);
4549 break;
4550 case OPERATOR_XOR:
4551 mpz_xor(val, left_val, right_val);
4552 break;
4553 case OPERATOR_MULT:
4554 mpz_mul(val, left_val, right_val);
4555 break;
4556 case OPERATOR_DIV:
4557 if (mpz_sgn(right_val) != 0)
4558 mpz_tdiv_q(val, left_val, right_val);
4559 else
4561 error_at(location, "division by zero");
4562 mpz_set_ui(val, 0);
4563 return true;
4565 break;
4566 case OPERATOR_MOD:
4567 if (mpz_sgn(right_val) != 0)
4568 mpz_tdiv_r(val, left_val, right_val);
4569 else
4571 error_at(location, "division by zero");
4572 mpz_set_ui(val, 0);
4573 return true;
4575 break;
4576 case OPERATOR_LSHIFT:
4578 unsigned long shift = mpz_get_ui(right_val);
4579 if (mpz_cmp_ui(right_val, shift) != 0 || shift > 0x100000)
4581 error_at(location, "shift count overflow");
4582 mpz_set_ui(val, 0);
4583 return true;
4585 mpz_mul_2exp(val, left_val, shift);
4586 is_shift_op = true;
4587 break;
4589 break;
4590 case OPERATOR_RSHIFT:
4592 unsigned long shift = mpz_get_ui(right_val);
4593 if (mpz_cmp_ui(right_val, shift) != 0)
4595 error_at(location, "shift count overflow");
4596 mpz_set_ui(val, 0);
4597 return true;
4599 if (mpz_cmp_ui(left_val, 0) >= 0)
4600 mpz_tdiv_q_2exp(val, left_val, shift);
4601 else
4602 mpz_fdiv_q_2exp(val, left_val, shift);
4603 is_shift_op = true;
4604 break;
4606 break;
4607 case OPERATOR_AND:
4608 mpz_and(val, left_val, right_val);
4609 break;
4610 case OPERATOR_BITCLEAR:
4612 mpz_t tval;
4613 mpz_init(tval);
4614 mpz_com(tval, right_val);
4615 mpz_and(val, left_val, tval);
4616 mpz_clear(tval);
4618 break;
4619 default:
4620 gcc_unreachable();
4623 Type* type = left_type;
4624 if (!is_shift_op)
4626 if (type == NULL)
4627 type = right_type;
4628 else if (type != right_type && right_type != NULL)
4630 if (type->is_abstract())
4631 type = right_type;
4632 else if (!right_type->is_abstract())
4634 // This look like a type error which should be diagnosed
4635 // elsewhere. Don't do anything here, to avoid an
4636 // unhelpful chain of error messages.
4637 return true;
4642 if (type != NULL && !type->is_abstract())
4644 // We have to check the operands too, as we have implicitly
4645 // coerced them to TYPE.
4646 if ((type != left_type
4647 && !Integer_expression::check_constant(left_val, type, location))
4648 || (!is_shift_op
4649 && type != right_type
4650 && !Integer_expression::check_constant(right_val, type,
4651 location))
4652 || !Integer_expression::check_constant(val, type, location))
4653 mpz_set_ui(val, 0);
4656 return true;
4659 // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
4660 // Return true if this could be done, false if not.
4662 bool
4663 Binary_expression::eval_float(Operator op, Type* left_type, mpfr_t left_val,
4664 Type* right_type, mpfr_t right_val,
4665 mpfr_t val, source_location location)
4667 switch (op)
4669 case OPERATOR_OROR:
4670 case OPERATOR_ANDAND:
4671 case OPERATOR_EQEQ:
4672 case OPERATOR_NOTEQ:
4673 case OPERATOR_LT:
4674 case OPERATOR_LE:
4675 case OPERATOR_GT:
4676 case OPERATOR_GE:
4677 // These return boolean values. We should probably handle them
4678 // anyhow in case a type conversion is used on the result.
4679 return false;
4680 case OPERATOR_PLUS:
4681 mpfr_add(val, left_val, right_val, GMP_RNDN);
4682 break;
4683 case OPERATOR_MINUS:
4684 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4685 break;
4686 case OPERATOR_OR:
4687 case OPERATOR_XOR:
4688 case OPERATOR_AND:
4689 case OPERATOR_BITCLEAR:
4690 return false;
4691 case OPERATOR_MULT:
4692 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4693 break;
4694 case OPERATOR_DIV:
4695 if (mpfr_zero_p(right_val))
4696 error_at(location, "division by zero");
4697 mpfr_div(val, left_val, right_val, GMP_RNDN);
4698 break;
4699 case OPERATOR_MOD:
4700 return false;
4701 case OPERATOR_LSHIFT:
4702 case OPERATOR_RSHIFT:
4703 return false;
4704 default:
4705 gcc_unreachable();
4708 Type* type = left_type;
4709 if (type == NULL)
4710 type = right_type;
4711 else if (type != right_type && right_type != NULL)
4713 if (type->is_abstract())
4714 type = right_type;
4715 else if (!right_type->is_abstract())
4717 // This looks like a type error which should be diagnosed
4718 // elsewhere. Don't do anything here, to avoid an unhelpful
4719 // chain of error messages.
4720 return true;
4724 if (type != NULL && !type->is_abstract())
4726 if ((type != left_type
4727 && !Float_expression::check_constant(left_val, type, location))
4728 || (type != right_type
4729 && !Float_expression::check_constant(right_val, type,
4730 location))
4731 || !Float_expression::check_constant(val, type, location))
4732 mpfr_set_ui(val, 0, GMP_RNDN);
4735 return true;
4738 // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
4739 // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG. Return true if this
4740 // could be done, false if not.
4742 bool
4743 Binary_expression::eval_complex(Operator op, Type* left_type,
4744 mpfr_t left_real, mpfr_t left_imag,
4745 Type *right_type,
4746 mpfr_t right_real, mpfr_t right_imag,
4747 mpfr_t real, mpfr_t imag,
4748 source_location location)
4750 switch (op)
4752 case OPERATOR_OROR:
4753 case OPERATOR_ANDAND:
4754 case OPERATOR_EQEQ:
4755 case OPERATOR_NOTEQ:
4756 case OPERATOR_LT:
4757 case OPERATOR_LE:
4758 case OPERATOR_GT:
4759 case OPERATOR_GE:
4760 // These return boolean values and must be handled differently.
4761 return false;
4762 case OPERATOR_PLUS:
4763 mpfr_add(real, left_real, right_real, GMP_RNDN);
4764 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4765 break;
4766 case OPERATOR_MINUS:
4767 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4768 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4769 break;
4770 case OPERATOR_OR:
4771 case OPERATOR_XOR:
4772 case OPERATOR_AND:
4773 case OPERATOR_BITCLEAR:
4774 return false;
4775 case OPERATOR_MULT:
4777 // You might think that multiplying two complex numbers would
4778 // be simple, and you would be right, until you start to think
4779 // about getting the right answer for infinity. If one
4780 // operand here is infinity and the other is anything other
4781 // than zero or NaN, then we are going to wind up subtracting
4782 // two infinity values. That will give us a NaN, but the
4783 // correct answer is infinity.
4785 mpfr_t lrrr;
4786 mpfr_init(lrrr);
4787 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4789 mpfr_t lrri;
4790 mpfr_init(lrri);
4791 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4793 mpfr_t lirr;
4794 mpfr_init(lirr);
4795 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4797 mpfr_t liri;
4798 mpfr_init(liri);
4799 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4801 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4802 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4804 // If we get NaN on both sides, check whether it should really
4805 // be infinity. The rule is that if either side of the
4806 // complex number is infinity, then the whole value is
4807 // infinity, even if the other side is NaN. So the only case
4808 // we have to fix is the one in which both sides are NaN.
4809 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4810 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4811 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4813 bool is_infinity = false;
4815 mpfr_t lr;
4816 mpfr_t li;
4817 mpfr_init_set(lr, left_real, GMP_RNDN);
4818 mpfr_init_set(li, left_imag, GMP_RNDN);
4820 mpfr_t rr;
4821 mpfr_t ri;
4822 mpfr_init_set(rr, right_real, GMP_RNDN);
4823 mpfr_init_set(ri, right_imag, GMP_RNDN);
4825 // If the left side is infinity, then the result is
4826 // infinity.
4827 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4829 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4830 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4831 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4832 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4833 if (mpfr_nan_p(rr))
4835 mpfr_set_ui(rr, 0, GMP_RNDN);
4836 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4838 if (mpfr_nan_p(ri))
4840 mpfr_set_ui(ri, 0, GMP_RNDN);
4841 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4843 is_infinity = true;
4846 // If the right side is infinity, then the result is
4847 // infinity.
4848 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4850 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4851 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4852 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4853 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4854 if (mpfr_nan_p(lr))
4856 mpfr_set_ui(lr, 0, GMP_RNDN);
4857 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4859 if (mpfr_nan_p(li))
4861 mpfr_set_ui(li, 0, GMP_RNDN);
4862 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4864 is_infinity = true;
4867 // If we got an overflow in the intermediate computations,
4868 // then the result is infinity.
4869 if (!is_infinity
4870 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4871 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4873 if (mpfr_nan_p(lr))
4875 mpfr_set_ui(lr, 0, GMP_RNDN);
4876 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4878 if (mpfr_nan_p(li))
4880 mpfr_set_ui(li, 0, GMP_RNDN);
4881 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4883 if (mpfr_nan_p(rr))
4885 mpfr_set_ui(rr, 0, GMP_RNDN);
4886 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4888 if (mpfr_nan_p(ri))
4890 mpfr_set_ui(ri, 0, GMP_RNDN);
4891 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4893 is_infinity = true;
4896 if (is_infinity)
4898 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4899 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4900 mpfr_mul(lirr, li, rr, GMP_RNDN);
4901 mpfr_mul(liri, li, ri, GMP_RNDN);
4902 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4903 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4904 mpfr_set_inf(real, mpfr_sgn(real));
4905 mpfr_set_inf(imag, mpfr_sgn(imag));
4908 mpfr_clear(lr);
4909 mpfr_clear(li);
4910 mpfr_clear(rr);
4911 mpfr_clear(ri);
4914 mpfr_clear(lrrr);
4915 mpfr_clear(lrri);
4916 mpfr_clear(lirr);
4917 mpfr_clear(liri);
4919 break;
4920 case OPERATOR_DIV:
4922 // For complex division we want to avoid having an
4923 // intermediate overflow turn the whole result in a NaN. We
4924 // scale the values to try to avoid this.
4926 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4927 error_at(location, "division by zero");
4929 mpfr_t rra;
4930 mpfr_t ria;
4931 mpfr_init(rra);
4932 mpfr_init(ria);
4933 mpfr_abs(rra, right_real, GMP_RNDN);
4934 mpfr_abs(ria, right_imag, GMP_RNDN);
4935 mpfr_t t;
4936 mpfr_init(t);
4937 mpfr_max(t, rra, ria, GMP_RNDN);
4939 mpfr_t rr;
4940 mpfr_t ri;
4941 mpfr_init_set(rr, right_real, GMP_RNDN);
4942 mpfr_init_set(ri, right_imag, GMP_RNDN);
4943 long ilogbw = 0;
4944 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4946 ilogbw = mpfr_get_exp(t);
4947 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4948 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4951 mpfr_t denom;
4952 mpfr_init(denom);
4953 mpfr_mul(denom, rr, rr, GMP_RNDN);
4954 mpfr_mul(t, ri, ri, GMP_RNDN);
4955 mpfr_add(denom, denom, t, GMP_RNDN);
4957 mpfr_mul(real, left_real, rr, GMP_RNDN);
4958 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4959 mpfr_add(real, real, t, GMP_RNDN);
4960 mpfr_div(real, real, denom, GMP_RNDN);
4961 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4963 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4964 mpfr_mul(t, left_real, ri, GMP_RNDN);
4965 mpfr_sub(imag, imag, t, GMP_RNDN);
4966 mpfr_div(imag, imag, denom, GMP_RNDN);
4967 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4969 // If we wind up with NaN on both sides, check whether we
4970 // should really have infinity. The rule is that if either
4971 // side of the complex number is infinity, then the whole
4972 // value is infinity, even if the other side is NaN. So the
4973 // only case we have to fix is the one in which both sides are
4974 // NaN.
4975 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4976 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4977 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4979 if (mpfr_zero_p(denom))
4981 mpfr_set_inf(real, mpfr_sgn(rr));
4982 mpfr_mul(real, real, left_real, GMP_RNDN);
4983 mpfr_set_inf(imag, mpfr_sgn(rr));
4984 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4986 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4987 && mpfr_number_p(rr) && mpfr_number_p(ri))
4989 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4990 mpfr_copysign(t, t, left_real, GMP_RNDN);
4992 mpfr_t t2;
4993 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4994 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4996 mpfr_t t3;
4997 mpfr_init(t3);
4998 mpfr_mul(t3, t, rr, GMP_RNDN);
5000 mpfr_t t4;
5001 mpfr_init(t4);
5002 mpfr_mul(t4, t2, ri, GMP_RNDN);
5004 mpfr_add(t3, t3, t4, GMP_RNDN);
5005 mpfr_set_inf(real, mpfr_sgn(t3));
5007 mpfr_mul(t3, t2, rr, GMP_RNDN);
5008 mpfr_mul(t4, t, ri, GMP_RNDN);
5009 mpfr_sub(t3, t3, t4, GMP_RNDN);
5010 mpfr_set_inf(imag, mpfr_sgn(t3));
5012 mpfr_clear(t2);
5013 mpfr_clear(t3);
5014 mpfr_clear(t4);
5016 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
5017 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
5019 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5020 mpfr_copysign(t, t, rr, GMP_RNDN);
5022 mpfr_t t2;
5023 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5024 mpfr_copysign(t2, t2, ri, GMP_RNDN);
5026 mpfr_t t3;
5027 mpfr_init(t3);
5028 mpfr_mul(t3, left_real, t, GMP_RNDN);
5030 mpfr_t t4;
5031 mpfr_init(t4);
5032 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5034 mpfr_add(t3, t3, t4, GMP_RNDN);
5035 mpfr_set_ui(real, 0, GMP_RNDN);
5036 mpfr_mul(real, real, t3, GMP_RNDN);
5038 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5039 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5040 mpfr_sub(t3, t3, t4, GMP_RNDN);
5041 mpfr_set_ui(imag, 0, GMP_RNDN);
5042 mpfr_mul(imag, imag, t3, GMP_RNDN);
5044 mpfr_clear(t2);
5045 mpfr_clear(t3);
5046 mpfr_clear(t4);
5050 mpfr_clear(denom);
5051 mpfr_clear(rr);
5052 mpfr_clear(ri);
5053 mpfr_clear(t);
5054 mpfr_clear(rra);
5055 mpfr_clear(ria);
5057 break;
5058 case OPERATOR_MOD:
5059 return false;
5060 case OPERATOR_LSHIFT:
5061 case OPERATOR_RSHIFT:
5062 return false;
5063 default:
5064 gcc_unreachable();
5067 Type* type = left_type;
5068 if (type == NULL)
5069 type = right_type;
5070 else if (type != right_type && right_type != NULL)
5072 if (type->is_abstract())
5073 type = right_type;
5074 else if (!right_type->is_abstract())
5076 // This looks like a type error which should be diagnosed
5077 // elsewhere. Don't do anything here, to avoid an unhelpful
5078 // chain of error messages.
5079 return true;
5083 if (type != NULL && !type->is_abstract())
5085 if ((type != left_type
5086 && !Complex_expression::check_constant(left_real, left_imag,
5087 type, location))
5088 || (type != right_type
5089 && !Complex_expression::check_constant(right_real, right_imag,
5090 type, location))
5091 || !Complex_expression::check_constant(real, imag, type,
5092 location))
5094 mpfr_set_ui(real, 0, GMP_RNDN);
5095 mpfr_set_ui(imag, 0, GMP_RNDN);
5099 return true;
5102 // Lower a binary expression. We have to evaluate constant
5103 // expressions now, in order to implement Go's unlimited precision
5104 // constants.
5106 Expression*
5107 Binary_expression::do_lower(Gogo*, Named_object*, int)
5109 source_location location = this->location();
5110 Operator op = this->op_;
5111 Expression* left = this->left_;
5112 Expression* right = this->right_;
5114 const bool is_comparison = (op == OPERATOR_EQEQ
5115 || op == OPERATOR_NOTEQ
5116 || op == OPERATOR_LT
5117 || op == OPERATOR_LE
5118 || op == OPERATOR_GT
5119 || op == OPERATOR_GE);
5121 // Integer constant expressions.
5123 mpz_t left_val;
5124 mpz_init(left_val);
5125 Type* left_type;
5126 mpz_t right_val;
5127 mpz_init(right_val);
5128 Type* right_type;
5129 if (left->integer_constant_value(false, left_val, &left_type)
5130 && right->integer_constant_value(false, right_val, &right_type))
5132 Expression* ret = NULL;
5133 if (left_type != right_type
5134 && left_type != NULL
5135 && right_type != NULL
5136 && left_type->base() != right_type->base()
5137 && op != OPERATOR_LSHIFT
5138 && op != OPERATOR_RSHIFT)
5140 // May be a type error--let it be diagnosed later.
5142 else if (is_comparison)
5144 bool b = Binary_expression::compare_integer(op, left_val,
5145 right_val);
5146 ret = Expression::make_cast(Type::lookup_bool_type(),
5147 Expression::make_boolean(b, location),
5148 location);
5150 else
5152 mpz_t val;
5153 mpz_init(val);
5155 if (Binary_expression::eval_integer(op, left_type, left_val,
5156 right_type, right_val,
5157 location, val))
5159 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND);
5160 Type* type;
5161 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
5162 type = left_type;
5163 else if (left_type == NULL)
5164 type = right_type;
5165 else if (right_type == NULL)
5166 type = left_type;
5167 else if (!left_type->is_abstract()
5168 && left_type->named_type() != NULL)
5169 type = left_type;
5170 else if (!right_type->is_abstract()
5171 && right_type->named_type() != NULL)
5172 type = right_type;
5173 else if (!left_type->is_abstract())
5174 type = left_type;
5175 else if (!right_type->is_abstract())
5176 type = right_type;
5177 else if (left_type->float_type() != NULL)
5178 type = left_type;
5179 else if (right_type->float_type() != NULL)
5180 type = right_type;
5181 else if (left_type->complex_type() != NULL)
5182 type = left_type;
5183 else if (right_type->complex_type() != NULL)
5184 type = right_type;
5185 else
5186 type = left_type;
5187 ret = Expression::make_integer(&val, type, location);
5190 mpz_clear(val);
5193 if (ret != NULL)
5195 mpz_clear(right_val);
5196 mpz_clear(left_val);
5197 return ret;
5200 mpz_clear(right_val);
5201 mpz_clear(left_val);
5204 // Floating point constant expressions.
5206 mpfr_t left_val;
5207 mpfr_init(left_val);
5208 Type* left_type;
5209 mpfr_t right_val;
5210 mpfr_init(right_val);
5211 Type* right_type;
5212 if (left->float_constant_value(left_val, &left_type)
5213 && right->float_constant_value(right_val, &right_type))
5215 Expression* ret = NULL;
5216 if (left_type != right_type
5217 && left_type != NULL
5218 && right_type != NULL
5219 && left_type->base() != right_type->base()
5220 && op != OPERATOR_LSHIFT
5221 && op != OPERATOR_RSHIFT)
5223 // May be a type error--let it be diagnosed later.
5225 else if (is_comparison)
5227 bool b = Binary_expression::compare_float(op,
5228 (left_type != NULL
5229 ? left_type
5230 : right_type),
5231 left_val, right_val);
5232 ret = Expression::make_boolean(b, location);
5234 else
5236 mpfr_t val;
5237 mpfr_init(val);
5239 if (Binary_expression::eval_float(op, left_type, left_val,
5240 right_type, right_val, val,
5241 location))
5243 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5244 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5245 Type* type;
5246 if (left_type == NULL)
5247 type = right_type;
5248 else if (right_type == NULL)
5249 type = left_type;
5250 else if (!left_type->is_abstract()
5251 && left_type->named_type() != NULL)
5252 type = left_type;
5253 else if (!right_type->is_abstract()
5254 && right_type->named_type() != NULL)
5255 type = right_type;
5256 else if (!left_type->is_abstract())
5257 type = left_type;
5258 else if (!right_type->is_abstract())
5259 type = right_type;
5260 else if (left_type->float_type() != NULL)
5261 type = left_type;
5262 else if (right_type->float_type() != NULL)
5263 type = right_type;
5264 else
5265 type = left_type;
5266 ret = Expression::make_float(&val, type, location);
5269 mpfr_clear(val);
5272 if (ret != NULL)
5274 mpfr_clear(right_val);
5275 mpfr_clear(left_val);
5276 return ret;
5279 mpfr_clear(right_val);
5280 mpfr_clear(left_val);
5283 // Complex constant expressions.
5285 mpfr_t left_real;
5286 mpfr_t left_imag;
5287 mpfr_init(left_real);
5288 mpfr_init(left_imag);
5289 Type* left_type;
5291 mpfr_t right_real;
5292 mpfr_t right_imag;
5293 mpfr_init(right_real);
5294 mpfr_init(right_imag);
5295 Type* right_type;
5297 if (left->complex_constant_value(left_real, left_imag, &left_type)
5298 && right->complex_constant_value(right_real, right_imag, &right_type))
5300 Expression* ret = NULL;
5301 if (left_type != right_type
5302 && left_type != NULL
5303 && right_type != NULL
5304 && left_type->base() != right_type->base())
5306 // May be a type error--let it be diagnosed later.
5308 else if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5310 bool b = Binary_expression::compare_complex(op,
5311 (left_type != NULL
5312 ? left_type
5313 : right_type),
5314 left_real,
5315 left_imag,
5316 right_real,
5317 right_imag);
5318 ret = Expression::make_boolean(b, location);
5320 else
5322 mpfr_t real;
5323 mpfr_t imag;
5324 mpfr_init(real);
5325 mpfr_init(imag);
5327 if (Binary_expression::eval_complex(op, left_type,
5328 left_real, left_imag,
5329 right_type,
5330 right_real, right_imag,
5331 real, imag,
5332 location))
5334 gcc_assert(op != OPERATOR_OROR && op != OPERATOR_ANDAND
5335 && op != OPERATOR_LSHIFT && op != OPERATOR_RSHIFT);
5336 Type* type;
5337 if (left_type == NULL)
5338 type = right_type;
5339 else if (right_type == NULL)
5340 type = left_type;
5341 else if (!left_type->is_abstract()
5342 && left_type->named_type() != NULL)
5343 type = left_type;
5344 else if (!right_type->is_abstract()
5345 && right_type->named_type() != NULL)
5346 type = right_type;
5347 else if (!left_type->is_abstract())
5348 type = left_type;
5349 else if (!right_type->is_abstract())
5350 type = right_type;
5351 else if (left_type->complex_type() != NULL)
5352 type = left_type;
5353 else if (right_type->complex_type() != NULL)
5354 type = right_type;
5355 else
5356 type = left_type;
5357 ret = Expression::make_complex(&real, &imag, type,
5358 location);
5360 mpfr_clear(real);
5361 mpfr_clear(imag);
5364 if (ret != NULL)
5366 mpfr_clear(left_real);
5367 mpfr_clear(left_imag);
5368 mpfr_clear(right_real);
5369 mpfr_clear(right_imag);
5370 return ret;
5374 mpfr_clear(left_real);
5375 mpfr_clear(left_imag);
5376 mpfr_clear(right_real);
5377 mpfr_clear(right_imag);
5380 // String constant expressions.
5381 if (op == OPERATOR_PLUS
5382 && left->type()->is_string_type()
5383 && right->type()->is_string_type())
5385 std::string left_string;
5386 std::string right_string;
5387 if (left->string_constant_value(&left_string)
5388 && right->string_constant_value(&right_string))
5389 return Expression::make_string(left_string + right_string, location);
5392 return this;
5395 // Return the integer constant value, if it has one.
5397 bool
5398 Binary_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
5399 Type** ptype) const
5401 mpz_t left_val;
5402 mpz_init(left_val);
5403 Type* left_type;
5404 if (!this->left_->integer_constant_value(iota_is_constant, left_val,
5405 &left_type))
5407 mpz_clear(left_val);
5408 return false;
5411 mpz_t right_val;
5412 mpz_init(right_val);
5413 Type* right_type;
5414 if (!this->right_->integer_constant_value(iota_is_constant, right_val,
5415 &right_type))
5417 mpz_clear(right_val);
5418 mpz_clear(left_val);
5419 return false;
5422 bool ret;
5423 if (left_type != right_type
5424 && left_type != NULL
5425 && right_type != NULL
5426 && left_type->base() != right_type->base()
5427 && this->op_ != OPERATOR_RSHIFT
5428 && this->op_ != OPERATOR_LSHIFT)
5429 ret = false;
5430 else
5431 ret = Binary_expression::eval_integer(this->op_, left_type, left_val,
5432 right_type, right_val,
5433 this->location(), val);
5435 mpz_clear(right_val);
5436 mpz_clear(left_val);
5438 if (ret)
5439 *ptype = left_type;
5441 return ret;
5444 // Return the floating point constant value, if it has one.
5446 bool
5447 Binary_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
5449 mpfr_t left_val;
5450 mpfr_init(left_val);
5451 Type* left_type;
5452 if (!this->left_->float_constant_value(left_val, &left_type))
5454 mpfr_clear(left_val);
5455 return false;
5458 mpfr_t right_val;
5459 mpfr_init(right_val);
5460 Type* right_type;
5461 if (!this->right_->float_constant_value(right_val, &right_type))
5463 mpfr_clear(right_val);
5464 mpfr_clear(left_val);
5465 return false;
5468 bool ret;
5469 if (left_type != right_type
5470 && left_type != NULL
5471 && right_type != NULL
5472 && left_type->base() != right_type->base())
5473 ret = false;
5474 else
5475 ret = Binary_expression::eval_float(this->op_, left_type, left_val,
5476 right_type, right_val,
5477 val, this->location());
5479 mpfr_clear(left_val);
5480 mpfr_clear(right_val);
5482 if (ret)
5483 *ptype = left_type;
5485 return ret;
5488 // Return the complex constant value, if it has one.
5490 bool
5491 Binary_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
5492 Type** ptype) const
5494 mpfr_t left_real;
5495 mpfr_t left_imag;
5496 mpfr_init(left_real);
5497 mpfr_init(left_imag);
5498 Type* left_type;
5499 if (!this->left_->complex_constant_value(left_real, left_imag, &left_type))
5501 mpfr_clear(left_real);
5502 mpfr_clear(left_imag);
5503 return false;
5506 mpfr_t right_real;
5507 mpfr_t right_imag;
5508 mpfr_init(right_real);
5509 mpfr_init(right_imag);
5510 Type* right_type;
5511 if (!this->right_->complex_constant_value(right_real, right_imag,
5512 &right_type))
5514 mpfr_clear(left_real);
5515 mpfr_clear(left_imag);
5516 mpfr_clear(right_real);
5517 mpfr_clear(right_imag);
5518 return false;
5521 bool ret;
5522 if (left_type != right_type
5523 && left_type != NULL
5524 && right_type != NULL
5525 && left_type->base() != right_type->base())
5526 ret = false;
5527 else
5528 ret = Binary_expression::eval_complex(this->op_, left_type,
5529 left_real, left_imag,
5530 right_type,
5531 right_real, right_imag,
5532 real, imag,
5533 this->location());
5534 mpfr_clear(left_real);
5535 mpfr_clear(left_imag);
5536 mpfr_clear(right_real);
5537 mpfr_clear(right_imag);
5539 if (ret)
5540 *ptype = left_type;
5542 return ret;
5545 // Note that the value is being discarded.
5547 void
5548 Binary_expression::do_discarding_value()
5550 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5551 this->right_->discarding_value();
5552 else
5553 this->warn_about_unused_value();
5556 // Get type.
5558 Type*
5559 Binary_expression::do_type()
5561 if (this->classification() == EXPRESSION_ERROR)
5562 return Type::make_error_type();
5564 switch (this->op_)
5566 case OPERATOR_OROR:
5567 case OPERATOR_ANDAND:
5568 case OPERATOR_EQEQ:
5569 case OPERATOR_NOTEQ:
5570 case OPERATOR_LT:
5571 case OPERATOR_LE:
5572 case OPERATOR_GT:
5573 case OPERATOR_GE:
5574 return Type::lookup_bool_type();
5576 case OPERATOR_PLUS:
5577 case OPERATOR_MINUS:
5578 case OPERATOR_OR:
5579 case OPERATOR_XOR:
5580 case OPERATOR_MULT:
5581 case OPERATOR_DIV:
5582 case OPERATOR_MOD:
5583 case OPERATOR_AND:
5584 case OPERATOR_BITCLEAR:
5586 Type* left_type = this->left_->type();
5587 Type* right_type = this->right_->type();
5588 if (left_type->is_error())
5589 return left_type;
5590 else if (right_type->is_error())
5591 return right_type;
5592 else if (!Type::are_compatible_for_binop(left_type, right_type))
5594 this->report_error(_("incompatible types in binary expression"));
5595 return Type::make_error_type();
5597 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
5598 return left_type;
5599 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
5600 return right_type;
5601 else if (!left_type->is_abstract())
5602 return left_type;
5603 else if (!right_type->is_abstract())
5604 return right_type;
5605 else if (left_type->complex_type() != NULL)
5606 return left_type;
5607 else if (right_type->complex_type() != NULL)
5608 return right_type;
5609 else if (left_type->float_type() != NULL)
5610 return left_type;
5611 else if (right_type->float_type() != NULL)
5612 return right_type;
5613 else
5614 return left_type;
5617 case OPERATOR_LSHIFT:
5618 case OPERATOR_RSHIFT:
5619 return this->left_->type();
5621 default:
5622 gcc_unreachable();
5626 // Set type for a binary expression.
5628 void
5629 Binary_expression::do_determine_type(const Type_context* context)
5631 Type* tleft = this->left_->type();
5632 Type* tright = this->right_->type();
5634 // Both sides should have the same type, except for the shift
5635 // operations. For a comparison, we should ignore the incoming
5636 // type.
5638 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5639 || this->op_ == OPERATOR_RSHIFT);
5641 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5642 || this->op_ == OPERATOR_NOTEQ
5643 || this->op_ == OPERATOR_LT
5644 || this->op_ == OPERATOR_LE
5645 || this->op_ == OPERATOR_GT
5646 || this->op_ == OPERATOR_GE);
5648 Type_context subcontext(*context);
5650 if (is_comparison)
5652 // In a comparison, the context does not determine the types of
5653 // the operands.
5654 subcontext.type = NULL;
5657 // Set the context for the left hand operand.
5658 if (is_shift_op)
5660 // The right hand operand plays no role in determining the type
5661 // of the left hand operand. A shift of an abstract integer in
5662 // a string context gets special treatment, which may be a
5663 // language bug.
5664 if (subcontext.type != NULL
5665 && subcontext.type->is_string_type()
5666 && tleft->is_abstract())
5667 error_at(this->location(), "shift of non-integer operand");
5669 else if (!tleft->is_abstract())
5670 subcontext.type = tleft;
5671 else if (!tright->is_abstract())
5672 subcontext.type = tright;
5673 else if (subcontext.type == NULL)
5675 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5676 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5677 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5679 // Both sides have an abstract integer, abstract float, or
5680 // abstract complex type. Just let CONTEXT determine
5681 // whether they may remain abstract or not.
5683 else if (tleft->complex_type() != NULL)
5684 subcontext.type = tleft;
5685 else if (tright->complex_type() != NULL)
5686 subcontext.type = tright;
5687 else if (tleft->float_type() != NULL)
5688 subcontext.type = tleft;
5689 else if (tright->float_type() != NULL)
5690 subcontext.type = tright;
5691 else
5692 subcontext.type = tleft;
5694 if (subcontext.type != NULL && !context->may_be_abstract)
5695 subcontext.type = subcontext.type->make_non_abstract_type();
5698 this->left_->determine_type(&subcontext);
5700 // The context for the right hand operand is the same as for the
5701 // left hand operand, except for a shift operator.
5702 if (is_shift_op)
5704 subcontext.type = Type::lookup_integer_type("uint");
5705 subcontext.may_be_abstract = false;
5708 this->right_->determine_type(&subcontext);
5711 // Report an error if the binary operator OP does not support TYPE.
5712 // Return whether the operation is OK. This should not be used for
5713 // shift.
5715 bool
5716 Binary_expression::check_operator_type(Operator op, Type* type,
5717 source_location location)
5719 switch (op)
5721 case OPERATOR_OROR:
5722 case OPERATOR_ANDAND:
5723 if (!type->is_boolean_type())
5725 error_at(location, "expected boolean type");
5726 return false;
5728 break;
5730 case OPERATOR_EQEQ:
5731 case OPERATOR_NOTEQ:
5732 if (type->integer_type() == NULL
5733 && type->float_type() == NULL
5734 && type->complex_type() == NULL
5735 && !type->is_string_type()
5736 && type->points_to() == NULL
5737 && !type->is_nil_type()
5738 && !type->is_boolean_type()
5739 && type->interface_type() == NULL
5740 && (type->array_type() == NULL
5741 || type->array_type()->length() != NULL)
5742 && type->map_type() == NULL
5743 && type->channel_type() == NULL
5744 && type->function_type() == NULL)
5746 error_at(location,
5747 ("expected integer, floating, complex, string, pointer, "
5748 "boolean, interface, slice, map, channel, "
5749 "or function type"));
5750 return false;
5752 break;
5754 case OPERATOR_LT:
5755 case OPERATOR_LE:
5756 case OPERATOR_GT:
5757 case OPERATOR_GE:
5758 if (type->integer_type() == NULL
5759 && type->float_type() == NULL
5760 && !type->is_string_type())
5762 error_at(location, "expected integer, floating, or string type");
5763 return false;
5765 break;
5767 case OPERATOR_PLUS:
5768 case OPERATOR_PLUSEQ:
5769 if (type->integer_type() == NULL
5770 && type->float_type() == NULL
5771 && type->complex_type() == NULL
5772 && !type->is_string_type())
5774 error_at(location,
5775 "expected integer, floating, complex, or string type");
5776 return false;
5778 break;
5780 case OPERATOR_MINUS:
5781 case OPERATOR_MINUSEQ:
5782 case OPERATOR_MULT:
5783 case OPERATOR_MULTEQ:
5784 case OPERATOR_DIV:
5785 case OPERATOR_DIVEQ:
5786 if (type->integer_type() == NULL
5787 && type->float_type() == NULL
5788 && type->complex_type() == NULL)
5790 error_at(location, "expected integer, floating, or complex type");
5791 return false;
5793 break;
5795 case OPERATOR_MOD:
5796 case OPERATOR_MODEQ:
5797 case OPERATOR_OR:
5798 case OPERATOR_OREQ:
5799 case OPERATOR_AND:
5800 case OPERATOR_ANDEQ:
5801 case OPERATOR_XOR:
5802 case OPERATOR_XOREQ:
5803 case OPERATOR_BITCLEAR:
5804 case OPERATOR_BITCLEAREQ:
5805 if (type->integer_type() == NULL)
5807 error_at(location, "expected integer type");
5808 return false;
5810 break;
5812 default:
5813 gcc_unreachable();
5816 return true;
5819 // Check types.
5821 void
5822 Binary_expression::do_check_types(Gogo*)
5824 if (this->classification() == EXPRESSION_ERROR)
5825 return;
5827 Type* left_type = this->left_->type();
5828 Type* right_type = this->right_->type();
5829 if (left_type->is_error() || right_type->is_error())
5831 this->set_is_error();
5832 return;
5835 if (this->op_ == OPERATOR_EQEQ
5836 || this->op_ == OPERATOR_NOTEQ
5837 || this->op_ == OPERATOR_LT
5838 || this->op_ == OPERATOR_LE
5839 || this->op_ == OPERATOR_GT
5840 || this->op_ == OPERATOR_GE)
5842 if (!Type::are_assignable(left_type, right_type, NULL)
5843 && !Type::are_assignable(right_type, left_type, NULL))
5845 this->report_error(_("incompatible types in binary expression"));
5846 return;
5848 if (!Binary_expression::check_operator_type(this->op_, left_type,
5849 this->location())
5850 || !Binary_expression::check_operator_type(this->op_, right_type,
5851 this->location()))
5853 this->set_is_error();
5854 return;
5857 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5859 if (!Type::are_compatible_for_binop(left_type, right_type))
5861 this->report_error(_("incompatible types in binary expression"));
5862 return;
5864 if (!Binary_expression::check_operator_type(this->op_, left_type,
5865 this->location()))
5867 this->set_is_error();
5868 return;
5871 else
5873 if (left_type->integer_type() == NULL)
5874 this->report_error(_("shift of non-integer operand"));
5876 if (!right_type->is_abstract()
5877 && (right_type->integer_type() == NULL
5878 || !right_type->integer_type()->is_unsigned()))
5879 this->report_error(_("shift count not unsigned integer"));
5880 else
5882 mpz_t val;
5883 mpz_init(val);
5884 Type* type;
5885 if (this->right_->integer_constant_value(true, val, &type))
5887 if (mpz_sgn(val) < 0)
5889 this->report_error(_("negative shift count"));
5890 mpz_set_ui(val, 0);
5891 source_location rloc = this->right_->location();
5892 this->right_ = Expression::make_integer(&val, right_type,
5893 rloc);
5896 mpz_clear(val);
5901 // Get a tree for a binary expression.
5903 tree
5904 Binary_expression::do_get_tree(Translate_context* context)
5906 tree left = this->left_->get_tree(context);
5907 tree right = this->right_->get_tree(context);
5909 if (left == error_mark_node || right == error_mark_node)
5910 return error_mark_node;
5912 enum tree_code code;
5913 bool use_left_type = true;
5914 bool is_shift_op = false;
5915 switch (this->op_)
5917 case OPERATOR_EQEQ:
5918 case OPERATOR_NOTEQ:
5919 case OPERATOR_LT:
5920 case OPERATOR_LE:
5921 case OPERATOR_GT:
5922 case OPERATOR_GE:
5923 return Expression::comparison_tree(context, this->op_,
5924 this->left_->type(), left,
5925 this->right_->type(), right,
5926 this->location());
5928 case OPERATOR_OROR:
5929 code = TRUTH_ORIF_EXPR;
5930 use_left_type = false;
5931 break;
5932 case OPERATOR_ANDAND:
5933 code = TRUTH_ANDIF_EXPR;
5934 use_left_type = false;
5935 break;
5936 case OPERATOR_PLUS:
5937 code = PLUS_EXPR;
5938 break;
5939 case OPERATOR_MINUS:
5940 code = MINUS_EXPR;
5941 break;
5942 case OPERATOR_OR:
5943 code = BIT_IOR_EXPR;
5944 break;
5945 case OPERATOR_XOR:
5946 code = BIT_XOR_EXPR;
5947 break;
5948 case OPERATOR_MULT:
5949 code = MULT_EXPR;
5950 break;
5951 case OPERATOR_DIV:
5953 Type *t = this->left_->type();
5954 if (t->float_type() != NULL || t->complex_type() != NULL)
5955 code = RDIV_EXPR;
5956 else
5957 code = TRUNC_DIV_EXPR;
5959 break;
5960 case OPERATOR_MOD:
5961 code = TRUNC_MOD_EXPR;
5962 break;
5963 case OPERATOR_LSHIFT:
5964 code = LSHIFT_EXPR;
5965 is_shift_op = true;
5966 break;
5967 case OPERATOR_RSHIFT:
5968 code = RSHIFT_EXPR;
5969 is_shift_op = true;
5970 break;
5971 case OPERATOR_AND:
5972 code = BIT_AND_EXPR;
5973 break;
5974 case OPERATOR_BITCLEAR:
5975 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5976 code = BIT_AND_EXPR;
5977 break;
5978 default:
5979 gcc_unreachable();
5982 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5984 if (this->left_->type()->is_string_type())
5986 gcc_assert(this->op_ == OPERATOR_PLUS);
5987 tree string_type = Type::make_string_type()->get_tree(context->gogo());
5988 static tree string_plus_decl;
5989 return Gogo::call_builtin(&string_plus_decl,
5990 this->location(),
5991 "__go_string_plus",
5993 string_type,
5994 string_type,
5995 left,
5996 string_type,
5997 right);
6000 tree compute_type = excess_precision_type(type);
6001 if (compute_type != NULL_TREE)
6003 left = ::convert(compute_type, left);
6004 right = ::convert(compute_type, right);
6007 tree eval_saved = NULL_TREE;
6008 if (is_shift_op)
6010 // Make sure the values are evaluated.
6011 if (!DECL_P(left) && TREE_SIDE_EFFECTS(left))
6013 left = save_expr(left);
6014 eval_saved = left;
6016 if (!DECL_P(right) && TREE_SIDE_EFFECTS(right))
6018 right = save_expr(right);
6019 if (eval_saved == NULL_TREE)
6020 eval_saved = right;
6021 else
6022 eval_saved = fold_build2_loc(this->location(), COMPOUND_EXPR,
6023 void_type_node, eval_saved, right);
6027 tree ret = fold_build2_loc(this->location(),
6028 code,
6029 compute_type != NULL_TREE ? compute_type : type,
6030 left, right);
6032 if (compute_type != NULL_TREE)
6033 ret = ::convert(type, ret);
6035 // In Go, a shift larger than the size of the type is well-defined.
6036 // This is not true in GENERIC, so we need to insert a conditional.
6037 if (is_shift_op)
6039 gcc_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
6040 gcc_assert(this->left_->type()->integer_type() != NULL);
6041 int bits = TYPE_PRECISION(TREE_TYPE(left));
6043 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
6044 build_int_cst_type(TREE_TYPE(right), bits));
6046 tree overflow_result = fold_convert_loc(this->location(),
6047 TREE_TYPE(left),
6048 integer_zero_node);
6049 if (this->op_ == OPERATOR_RSHIFT
6050 && !this->left_->type()->integer_type()->is_unsigned())
6052 tree neg = fold_build2_loc(this->location(), LT_EXPR,
6053 boolean_type_node, left,
6054 fold_convert_loc(this->location(),
6055 TREE_TYPE(left),
6056 integer_zero_node));
6057 tree neg_one = fold_build2_loc(this->location(),
6058 MINUS_EXPR, TREE_TYPE(left),
6059 fold_convert_loc(this->location(),
6060 TREE_TYPE(left),
6061 integer_zero_node),
6062 fold_convert_loc(this->location(),
6063 TREE_TYPE(left),
6064 integer_one_node));
6065 overflow_result = fold_build3_loc(this->location(), COND_EXPR,
6066 TREE_TYPE(left), neg, neg_one,
6067 overflow_result);
6070 ret = fold_build3_loc(this->location(), COND_EXPR, TREE_TYPE(left),
6071 compare, ret, overflow_result);
6073 if (eval_saved != NULL_TREE)
6074 ret = fold_build2_loc(this->location(), COMPOUND_EXPR,
6075 TREE_TYPE(ret), eval_saved, ret);
6078 return ret;
6081 // Export a binary expression.
6083 void
6084 Binary_expression::do_export(Export* exp) const
6086 exp->write_c_string("(");
6087 this->left_->export_expression(exp);
6088 switch (this->op_)
6090 case OPERATOR_OROR:
6091 exp->write_c_string(" || ");
6092 break;
6093 case OPERATOR_ANDAND:
6094 exp->write_c_string(" && ");
6095 break;
6096 case OPERATOR_EQEQ:
6097 exp->write_c_string(" == ");
6098 break;
6099 case OPERATOR_NOTEQ:
6100 exp->write_c_string(" != ");
6101 break;
6102 case OPERATOR_LT:
6103 exp->write_c_string(" < ");
6104 break;
6105 case OPERATOR_LE:
6106 exp->write_c_string(" <= ");
6107 break;
6108 case OPERATOR_GT:
6109 exp->write_c_string(" > ");
6110 break;
6111 case OPERATOR_GE:
6112 exp->write_c_string(" >= ");
6113 break;
6114 case OPERATOR_PLUS:
6115 exp->write_c_string(" + ");
6116 break;
6117 case OPERATOR_MINUS:
6118 exp->write_c_string(" - ");
6119 break;
6120 case OPERATOR_OR:
6121 exp->write_c_string(" | ");
6122 break;
6123 case OPERATOR_XOR:
6124 exp->write_c_string(" ^ ");
6125 break;
6126 case OPERATOR_MULT:
6127 exp->write_c_string(" * ");
6128 break;
6129 case OPERATOR_DIV:
6130 exp->write_c_string(" / ");
6131 break;
6132 case OPERATOR_MOD:
6133 exp->write_c_string(" % ");
6134 break;
6135 case OPERATOR_LSHIFT:
6136 exp->write_c_string(" << ");
6137 break;
6138 case OPERATOR_RSHIFT:
6139 exp->write_c_string(" >> ");
6140 break;
6141 case OPERATOR_AND:
6142 exp->write_c_string(" & ");
6143 break;
6144 case OPERATOR_BITCLEAR:
6145 exp->write_c_string(" &^ ");
6146 break;
6147 default:
6148 gcc_unreachable();
6150 this->right_->export_expression(exp);
6151 exp->write_c_string(")");
6154 // Import a binary expression.
6156 Expression*
6157 Binary_expression::do_import(Import* imp)
6159 imp->require_c_string("(");
6161 Expression* left = Expression::import_expression(imp);
6163 Operator op;
6164 if (imp->match_c_string(" || "))
6166 op = OPERATOR_OROR;
6167 imp->advance(4);
6169 else if (imp->match_c_string(" && "))
6171 op = OPERATOR_ANDAND;
6172 imp->advance(4);
6174 else if (imp->match_c_string(" == "))
6176 op = OPERATOR_EQEQ;
6177 imp->advance(4);
6179 else if (imp->match_c_string(" != "))
6181 op = OPERATOR_NOTEQ;
6182 imp->advance(4);
6184 else if (imp->match_c_string(" < "))
6186 op = OPERATOR_LT;
6187 imp->advance(3);
6189 else if (imp->match_c_string(" <= "))
6191 op = OPERATOR_LE;
6192 imp->advance(4);
6194 else if (imp->match_c_string(" > "))
6196 op = OPERATOR_GT;
6197 imp->advance(3);
6199 else if (imp->match_c_string(" >= "))
6201 op = OPERATOR_GE;
6202 imp->advance(4);
6204 else if (imp->match_c_string(" + "))
6206 op = OPERATOR_PLUS;
6207 imp->advance(3);
6209 else if (imp->match_c_string(" - "))
6211 op = OPERATOR_MINUS;
6212 imp->advance(3);
6214 else if (imp->match_c_string(" | "))
6216 op = OPERATOR_OR;
6217 imp->advance(3);
6219 else if (imp->match_c_string(" ^ "))
6221 op = OPERATOR_XOR;
6222 imp->advance(3);
6224 else if (imp->match_c_string(" * "))
6226 op = OPERATOR_MULT;
6227 imp->advance(3);
6229 else if (imp->match_c_string(" / "))
6231 op = OPERATOR_DIV;
6232 imp->advance(3);
6234 else if (imp->match_c_string(" % "))
6236 op = OPERATOR_MOD;
6237 imp->advance(3);
6239 else if (imp->match_c_string(" << "))
6241 op = OPERATOR_LSHIFT;
6242 imp->advance(4);
6244 else if (imp->match_c_string(" >> "))
6246 op = OPERATOR_RSHIFT;
6247 imp->advance(4);
6249 else if (imp->match_c_string(" & "))
6251 op = OPERATOR_AND;
6252 imp->advance(3);
6254 else if (imp->match_c_string(" &^ "))
6256 op = OPERATOR_BITCLEAR;
6257 imp->advance(4);
6259 else
6261 error_at(imp->location(), "unrecognized binary operator");
6262 return Expression::make_error(imp->location());
6265 Expression* right = Expression::import_expression(imp);
6267 imp->require_c_string(")");
6269 return Expression::make_binary(op, left, right, imp->location());
6272 // Make a binary expression.
6274 Expression*
6275 Expression::make_binary(Operator op, Expression* left, Expression* right,
6276 source_location location)
6278 return new Binary_expression(op, left, right, location);
6281 // Implement a comparison.
6283 tree
6284 Expression::comparison_tree(Translate_context* context, Operator op,
6285 Type* left_type, tree left_tree,
6286 Type* right_type, tree right_tree,
6287 source_location location)
6289 enum tree_code code;
6290 switch (op)
6292 case OPERATOR_EQEQ:
6293 code = EQ_EXPR;
6294 break;
6295 case OPERATOR_NOTEQ:
6296 code = NE_EXPR;
6297 break;
6298 case OPERATOR_LT:
6299 code = LT_EXPR;
6300 break;
6301 case OPERATOR_LE:
6302 code = LE_EXPR;
6303 break;
6304 case OPERATOR_GT:
6305 code = GT_EXPR;
6306 break;
6307 case OPERATOR_GE:
6308 code = GE_EXPR;
6309 break;
6310 default:
6311 gcc_unreachable();
6314 if (left_type->is_string_type() && right_type->is_string_type())
6316 tree string_type = Type::make_string_type()->get_tree(context->gogo());
6317 static tree string_compare_decl;
6318 left_tree = Gogo::call_builtin(&string_compare_decl,
6319 location,
6320 "__go_strcmp",
6322 integer_type_node,
6323 string_type,
6324 left_tree,
6325 string_type,
6326 right_tree);
6327 right_tree = build_int_cst_type(integer_type_node, 0);
6329 else if ((left_type->interface_type() != NULL
6330 && right_type->interface_type() == NULL
6331 && !right_type->is_nil_type())
6332 || (left_type->interface_type() == NULL
6333 && !left_type->is_nil_type()
6334 && right_type->interface_type() != NULL))
6336 // Comparing an interface value to a non-interface value.
6337 if (left_type->interface_type() == NULL)
6339 std::swap(left_type, right_type);
6340 std::swap(left_tree, right_tree);
6343 // The right operand is not an interface. We need to take its
6344 // address if it is not a pointer.
6345 tree make_tmp;
6346 tree arg;
6347 if (right_type->points_to() != NULL)
6349 make_tmp = NULL_TREE;
6350 arg = right_tree;
6352 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree)) || DECL_P(right_tree))
6354 make_tmp = NULL_TREE;
6355 arg = build_fold_addr_expr_loc(location, right_tree);
6356 if (DECL_P(right_tree))
6357 TREE_ADDRESSABLE(right_tree) = 1;
6359 else
6361 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6362 get_name(right_tree));
6363 DECL_IGNORED_P(tmp) = 0;
6364 DECL_INITIAL(tmp) = right_tree;
6365 TREE_ADDRESSABLE(tmp) = 1;
6366 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6367 SET_EXPR_LOCATION(make_tmp, location);
6368 arg = build_fold_addr_expr_loc(location, tmp);
6370 arg = fold_convert_loc(location, ptr_type_node, arg);
6372 tree descriptor = right_type->type_descriptor_pointer(context->gogo());
6374 if (left_type->interface_type()->is_empty())
6376 static tree empty_interface_value_compare_decl;
6377 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6378 location,
6379 "__go_empty_interface_value_compare",
6381 integer_type_node,
6382 TREE_TYPE(left_tree),
6383 left_tree,
6384 TREE_TYPE(descriptor),
6385 descriptor,
6386 ptr_type_node,
6387 arg);
6388 if (left_tree == error_mark_node)
6389 return error_mark_node;
6390 // This can panic if the type is not comparable.
6391 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6393 else
6395 static tree interface_value_compare_decl;
6396 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6397 location,
6398 "__go_interface_value_compare",
6400 integer_type_node,
6401 TREE_TYPE(left_tree),
6402 left_tree,
6403 TREE_TYPE(descriptor),
6404 descriptor,
6405 ptr_type_node,
6406 arg);
6407 if (left_tree == error_mark_node)
6408 return error_mark_node;
6409 // This can panic if the type is not comparable.
6410 TREE_NOTHROW(interface_value_compare_decl) = 0;
6412 right_tree = build_int_cst_type(integer_type_node, 0);
6414 if (make_tmp != NULL_TREE)
6415 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6416 left_tree);
6418 else if (left_type->interface_type() != NULL
6419 && right_type->interface_type() != NULL)
6421 if (left_type->interface_type()->is_empty()
6422 && right_type->interface_type()->is_empty())
6424 static tree empty_interface_compare_decl;
6425 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6426 location,
6427 "__go_empty_interface_compare",
6429 integer_type_node,
6430 TREE_TYPE(left_tree),
6431 left_tree,
6432 TREE_TYPE(right_tree),
6433 right_tree);
6434 if (left_tree == error_mark_node)
6435 return error_mark_node;
6436 // This can panic if the type is uncomparable.
6437 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6439 else if (!left_type->interface_type()->is_empty()
6440 && !right_type->interface_type()->is_empty())
6442 static tree interface_compare_decl;
6443 left_tree = Gogo::call_builtin(&interface_compare_decl,
6444 location,
6445 "__go_interface_compare",
6447 integer_type_node,
6448 TREE_TYPE(left_tree),
6449 left_tree,
6450 TREE_TYPE(right_tree),
6451 right_tree);
6452 if (left_tree == error_mark_node)
6453 return error_mark_node;
6454 // This can panic if the type is uncomparable.
6455 TREE_NOTHROW(interface_compare_decl) = 0;
6457 else
6459 if (left_type->interface_type()->is_empty())
6461 gcc_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6462 std::swap(left_type, right_type);
6463 std::swap(left_tree, right_tree);
6465 gcc_assert(!left_type->interface_type()->is_empty());
6466 gcc_assert(right_type->interface_type()->is_empty());
6467 static tree interface_empty_compare_decl;
6468 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6469 location,
6470 "__go_interface_empty_compare",
6472 integer_type_node,
6473 TREE_TYPE(left_tree),
6474 left_tree,
6475 TREE_TYPE(right_tree),
6476 right_tree);
6477 if (left_tree == error_mark_node)
6478 return error_mark_node;
6479 // This can panic if the type is uncomparable.
6480 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6483 right_tree = build_int_cst_type(integer_type_node, 0);
6486 if (left_type->is_nil_type()
6487 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6489 std::swap(left_type, right_type);
6490 std::swap(left_tree, right_tree);
6493 if (right_type->is_nil_type())
6495 if (left_type->array_type() != NULL
6496 && left_type->array_type()->length() == NULL)
6498 Array_type* at = left_type->array_type();
6499 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6500 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6502 else if (left_type->interface_type() != NULL)
6504 // An interface is nil if the first field is nil.
6505 tree left_type_tree = TREE_TYPE(left_tree);
6506 gcc_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6507 tree field = TYPE_FIELDS(left_type_tree);
6508 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6509 field, NULL_TREE);
6510 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6512 else
6514 gcc_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6515 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6519 if (left_tree == error_mark_node || right_tree == error_mark_node)
6520 return error_mark_node;
6522 tree ret = fold_build2(code, boolean_type_node, left_tree, right_tree);
6523 if (CAN_HAVE_LOCATION_P(ret))
6524 SET_EXPR_LOCATION(ret, location);
6525 return ret;
6528 // Class Bound_method_expression.
6530 // Traversal.
6533 Bound_method_expression::do_traverse(Traverse* traverse)
6535 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
6536 return TRAVERSE_EXIT;
6537 return Expression::traverse(&this->method_, traverse);
6540 // Return the type of a bound method expression. The type of this
6541 // object is really the type of the method with no receiver. We
6542 // should be able to get away with just returning the type of the
6543 // method.
6545 Type*
6546 Bound_method_expression::do_type()
6548 return this->method_->type();
6551 // Determine the types of a method expression.
6553 void
6554 Bound_method_expression::do_determine_type(const Type_context*)
6556 this->method_->determine_type_no_context();
6557 Type* mtype = this->method_->type();
6558 Function_type* fntype = mtype == NULL ? NULL : mtype->function_type();
6559 if (fntype == NULL || !fntype->is_method())
6560 this->expr_->determine_type_no_context();
6561 else
6563 Type_context subcontext(fntype->receiver()->type(), false);
6564 this->expr_->determine_type(&subcontext);
6568 // Check the types of a method expression.
6570 void
6571 Bound_method_expression::do_check_types(Gogo*)
6573 Type* type = this->method_->type()->deref();
6574 if (type == NULL
6575 || type->function_type() == NULL
6576 || !type->function_type()->is_method())
6577 this->report_error(_("object is not a method"));
6578 else
6580 Type* rtype = type->function_type()->receiver()->type()->deref();
6581 Type* etype = (this->expr_type_ != NULL
6582 ? this->expr_type_
6583 : this->expr_->type());
6584 etype = etype->deref();
6585 if (!Type::are_identical(rtype, etype, true, NULL))
6586 this->report_error(_("method type does not match object type"));
6590 // Get the tree for a method expression. There is no standard tree
6591 // representation for this. The only places it may currently be used
6592 // are in a Call_expression or a Go_statement, which will take it
6593 // apart directly. So this has nothing to do at present.
6595 tree
6596 Bound_method_expression::do_get_tree(Translate_context*)
6598 error_at(this->location(), "reference to method other than calling it");
6599 return error_mark_node;
6602 // Make a method expression.
6604 Bound_method_expression*
6605 Expression::make_bound_method(Expression* expr, Expression* method,
6606 source_location location)
6608 return new Bound_method_expression(expr, method, location);
6611 // Class Builtin_call_expression. This is used for a call to a
6612 // builtin function.
6614 class Builtin_call_expression : public Call_expression
6616 public:
6617 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6618 bool is_varargs, source_location location);
6620 protected:
6621 // This overrides Call_expression::do_lower.
6622 Expression*
6623 do_lower(Gogo*, Named_object*, int);
6625 bool
6626 do_is_constant() const;
6628 bool
6629 do_integer_constant_value(bool, mpz_t, Type**) const;
6631 bool
6632 do_float_constant_value(mpfr_t, Type**) const;
6634 bool
6635 do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
6637 Type*
6638 do_type();
6640 void
6641 do_determine_type(const Type_context*);
6643 void
6644 do_check_types(Gogo*);
6646 Expression*
6647 do_copy()
6649 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6650 this->args()->copy(),
6651 this->is_varargs(),
6652 this->location());
6655 tree
6656 do_get_tree(Translate_context*);
6658 void
6659 do_export(Export*) const;
6661 virtual bool
6662 do_is_recover_call() const;
6664 virtual void
6665 do_set_recover_arg(Expression*);
6667 private:
6668 // The builtin functions.
6669 enum Builtin_function_code
6671 BUILTIN_INVALID,
6673 // Predeclared builtin functions.
6674 BUILTIN_APPEND,
6675 BUILTIN_CAP,
6676 BUILTIN_CLOSE,
6677 BUILTIN_COMPLEX,
6678 BUILTIN_COPY,
6679 BUILTIN_IMAG,
6680 BUILTIN_LEN,
6681 BUILTIN_MAKE,
6682 BUILTIN_NEW,
6683 BUILTIN_PANIC,
6684 BUILTIN_PRINT,
6685 BUILTIN_PRINTLN,
6686 BUILTIN_REAL,
6687 BUILTIN_RECOVER,
6689 // Builtin functions from the unsafe package.
6690 BUILTIN_ALIGNOF,
6691 BUILTIN_OFFSETOF,
6692 BUILTIN_SIZEOF
6695 Expression*
6696 one_arg() const;
6698 bool
6699 check_one_arg();
6701 static Type*
6702 real_imag_type(Type*);
6704 static Type*
6705 complex_type(Type*);
6707 // A pointer back to the general IR structure. This avoids a global
6708 // variable, or passing it around everywhere.
6709 Gogo* gogo_;
6710 // The builtin function being called.
6711 Builtin_function_code code_;
6712 // Used to stop endless loops when the length of an array uses len
6713 // or cap of the array itself.
6714 mutable bool seen_;
6717 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6718 Expression* fn,
6719 Expression_list* args,
6720 bool is_varargs,
6721 source_location location)
6722 : Call_expression(fn, args, is_varargs, location),
6723 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
6725 Func_expression* fnexp = this->fn()->func_expression();
6726 gcc_assert(fnexp != NULL);
6727 const std::string& name(fnexp->named_object()->name());
6728 if (name == "append")
6729 this->code_ = BUILTIN_APPEND;
6730 else if (name == "cap")
6731 this->code_ = BUILTIN_CAP;
6732 else if (name == "close")
6733 this->code_ = BUILTIN_CLOSE;
6734 else if (name == "complex")
6735 this->code_ = BUILTIN_COMPLEX;
6736 else if (name == "copy")
6737 this->code_ = BUILTIN_COPY;
6738 else if (name == "imag")
6739 this->code_ = BUILTIN_IMAG;
6740 else if (name == "len")
6741 this->code_ = BUILTIN_LEN;
6742 else if (name == "make")
6743 this->code_ = BUILTIN_MAKE;
6744 else if (name == "new")
6745 this->code_ = BUILTIN_NEW;
6746 else if (name == "panic")
6747 this->code_ = BUILTIN_PANIC;
6748 else if (name == "print")
6749 this->code_ = BUILTIN_PRINT;
6750 else if (name == "println")
6751 this->code_ = BUILTIN_PRINTLN;
6752 else if (name == "real")
6753 this->code_ = BUILTIN_REAL;
6754 else if (name == "recover")
6755 this->code_ = BUILTIN_RECOVER;
6756 else if (name == "Alignof")
6757 this->code_ = BUILTIN_ALIGNOF;
6758 else if (name == "Offsetof")
6759 this->code_ = BUILTIN_OFFSETOF;
6760 else if (name == "Sizeof")
6761 this->code_ = BUILTIN_SIZEOF;
6762 else
6763 gcc_unreachable();
6766 // Return whether this is a call to recover. This is a virtual
6767 // function called from the parent class.
6769 bool
6770 Builtin_call_expression::do_is_recover_call() const
6772 if (this->classification() == EXPRESSION_ERROR)
6773 return false;
6774 return this->code_ == BUILTIN_RECOVER;
6777 // Set the argument for a call to recover.
6779 void
6780 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6782 const Expression_list* args = this->args();
6783 gcc_assert(args == NULL || args->empty());
6784 Expression_list* new_args = new Expression_list();
6785 new_args->push_back(arg);
6786 this->set_args(new_args);
6789 // A traversal class which looks for a call expression.
6791 class Find_call_expression : public Traverse
6793 public:
6794 Find_call_expression()
6795 : Traverse(traverse_expressions),
6796 found_(false)
6800 expression(Expression**);
6802 bool
6803 found()
6804 { return this->found_; }
6806 private:
6807 bool found_;
6811 Find_call_expression::expression(Expression** pexpr)
6813 if ((*pexpr)->call_expression() != NULL)
6815 this->found_ = true;
6816 return TRAVERSE_EXIT;
6818 return TRAVERSE_CONTINUE;
6821 // Lower a builtin call expression. This turns new and make into
6822 // specific expressions. We also convert to a constant if we can.
6824 Expression*
6825 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function, int)
6827 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
6829 this->report_error(_("invalid use of %<...%> with builtin function"));
6830 return Expression::make_error(this->location());
6833 if (this->code_ == BUILTIN_NEW)
6835 const Expression_list* args = this->args();
6836 if (args == NULL || args->size() < 1)
6837 this->report_error(_("not enough arguments"));
6838 else if (args->size() > 1)
6839 this->report_error(_("too many arguments"));
6840 else
6842 Expression* arg = args->front();
6843 if (!arg->is_type_expression())
6845 error_at(arg->location(), "expected type");
6846 this->set_is_error();
6848 else
6849 return Expression::make_allocation(arg->type(), this->location());
6852 else if (this->code_ == BUILTIN_MAKE)
6854 const Expression_list* args = this->args();
6855 if (args == NULL || args->size() < 1)
6856 this->report_error(_("not enough arguments"));
6857 else
6859 Expression* arg = args->front();
6860 if (!arg->is_type_expression())
6862 error_at(arg->location(), "expected type");
6863 this->set_is_error();
6865 else
6867 Expression_list* newargs;
6868 if (args->size() == 1)
6869 newargs = NULL;
6870 else
6872 newargs = new Expression_list();
6873 Expression_list::const_iterator p = args->begin();
6874 ++p;
6875 for (; p != args->end(); ++p)
6876 newargs->push_back(*p);
6878 return Expression::make_make(arg->type(), newargs,
6879 this->location());
6883 else if (this->is_constant())
6885 // We can only lower len and cap if there are no function calls
6886 // in the arguments. Otherwise we have to make the call.
6887 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6889 Expression* arg = this->one_arg();
6890 if (!arg->is_constant())
6892 Find_call_expression find_call;
6893 Expression::traverse(&arg, &find_call);
6894 if (find_call.found())
6895 return this;
6899 mpz_t ival;
6900 mpz_init(ival);
6901 Type* type;
6902 if (this->integer_constant_value(true, ival, &type))
6904 Expression* ret = Expression::make_integer(&ival, type,
6905 this->location());
6906 mpz_clear(ival);
6907 return ret;
6909 mpz_clear(ival);
6911 mpfr_t rval;
6912 mpfr_init(rval);
6913 if (this->float_constant_value(rval, &type))
6915 Expression* ret = Expression::make_float(&rval, type,
6916 this->location());
6917 mpfr_clear(rval);
6918 return ret;
6921 mpfr_t imag;
6922 mpfr_init(imag);
6923 if (this->complex_constant_value(rval, imag, &type))
6925 Expression* ret = Expression::make_complex(&rval, &imag, type,
6926 this->location());
6927 mpfr_clear(rval);
6928 mpfr_clear(imag);
6929 return ret;
6931 mpfr_clear(rval);
6932 mpfr_clear(imag);
6934 else if (this->code_ == BUILTIN_RECOVER)
6936 if (function != NULL)
6937 function->func_value()->set_calls_recover();
6938 else
6940 // Calling recover outside of a function always returns the
6941 // nil empty interface.
6942 Type* eface = Type::make_interface_type(NULL, this->location());
6943 return Expression::make_cast(eface,
6944 Expression::make_nil(this->location()),
6945 this->location());
6948 else if (this->code_ == BUILTIN_APPEND)
6950 // Lower the varargs.
6951 const Expression_list* args = this->args();
6952 if (args == NULL || args->empty())
6953 return this;
6954 Type* slice_type = args->front()->type();
6955 if (!slice_type->is_open_array_type())
6957 error_at(args->front()->location(), "argument 1 must be a slice");
6958 this->set_is_error();
6959 return this;
6961 return this->lower_varargs(gogo, function, slice_type, 2);
6964 return this;
6967 // Return the type of the real or imag functions, given the type of
6968 // the argument. We need to map complex to float, complex64 to
6969 // float32, and complex128 to float64, so it has to be done by name.
6970 // This returns NULL if it can't figure out the type.
6972 Type*
6973 Builtin_call_expression::real_imag_type(Type* arg_type)
6975 if (arg_type == NULL || arg_type->is_abstract())
6976 return NULL;
6977 Named_type* nt = arg_type->named_type();
6978 if (nt == NULL)
6979 return NULL;
6980 while (nt->real_type()->named_type() != NULL)
6981 nt = nt->real_type()->named_type();
6982 if (nt->name() == "complex64")
6983 return Type::lookup_float_type("float32");
6984 else if (nt->name() == "complex128")
6985 return Type::lookup_float_type("float64");
6986 else
6987 return NULL;
6990 // Return the type of the complex function, given the type of one of the
6991 // argments. Like real_imag_type, we have to map by name.
6993 Type*
6994 Builtin_call_expression::complex_type(Type* arg_type)
6996 if (arg_type == NULL || arg_type->is_abstract())
6997 return NULL;
6998 Named_type* nt = arg_type->named_type();
6999 if (nt == NULL)
7000 return NULL;
7001 while (nt->real_type()->named_type() != NULL)
7002 nt = nt->real_type()->named_type();
7003 if (nt->name() == "float32")
7004 return Type::lookup_complex_type("complex64");
7005 else if (nt->name() == "float64")
7006 return Type::lookup_complex_type("complex128");
7007 else
7008 return NULL;
7011 // Return a single argument, or NULL if there isn't one.
7013 Expression*
7014 Builtin_call_expression::one_arg() const
7016 const Expression_list* args = this->args();
7017 if (args->size() != 1)
7018 return NULL;
7019 return args->front();
7022 // Return whether this is constant: len of a string, or len or cap of
7023 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7025 bool
7026 Builtin_call_expression::do_is_constant() const
7028 switch (this->code_)
7030 case BUILTIN_LEN:
7031 case BUILTIN_CAP:
7033 if (this->seen_)
7034 return false;
7036 Expression* arg = this->one_arg();
7037 if (arg == NULL)
7038 return false;
7039 Type* arg_type = arg->type();
7041 if (arg_type->points_to() != NULL
7042 && arg_type->points_to()->array_type() != NULL
7043 && !arg_type->points_to()->is_open_array_type())
7044 arg_type = arg_type->points_to();
7046 if (arg_type->array_type() != NULL
7047 && arg_type->array_type()->length() != NULL)
7048 return true;
7050 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7052 this->seen_ = true;
7053 bool ret = arg->is_constant();
7054 this->seen_ = false;
7055 return ret;
7058 break;
7060 case BUILTIN_SIZEOF:
7061 case BUILTIN_ALIGNOF:
7062 return this->one_arg() != NULL;
7064 case BUILTIN_OFFSETOF:
7066 Expression* arg = this->one_arg();
7067 if (arg == NULL)
7068 return false;
7069 return arg->field_reference_expression() != NULL;
7072 case BUILTIN_COMPLEX:
7074 const Expression_list* args = this->args();
7075 if (args != NULL && args->size() == 2)
7076 return args->front()->is_constant() && args->back()->is_constant();
7078 break;
7080 case BUILTIN_REAL:
7081 case BUILTIN_IMAG:
7083 Expression* arg = this->one_arg();
7084 return arg != NULL && arg->is_constant();
7087 default:
7088 break;
7091 return false;
7094 // Return an integer constant value if possible.
7096 bool
7097 Builtin_call_expression::do_integer_constant_value(bool iota_is_constant,
7098 mpz_t val,
7099 Type** ptype) const
7101 if (this->code_ == BUILTIN_LEN
7102 || this->code_ == BUILTIN_CAP)
7104 Expression* arg = this->one_arg();
7105 if (arg == NULL)
7106 return false;
7107 Type* arg_type = arg->type();
7109 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7111 std::string sval;
7112 if (arg->string_constant_value(&sval))
7114 mpz_set_ui(val, sval.length());
7115 *ptype = Type::lookup_integer_type("int");
7116 return true;
7120 if (arg_type->points_to() != NULL
7121 && arg_type->points_to()->array_type() != NULL
7122 && !arg_type->points_to()->is_open_array_type())
7123 arg_type = arg_type->points_to();
7125 if (arg_type->array_type() != NULL
7126 && arg_type->array_type()->length() != NULL)
7128 if (this->seen_)
7129 return false;
7130 Expression* e = arg_type->array_type()->length();
7131 this->seen_ = true;
7132 bool r = e->integer_constant_value(iota_is_constant, val, ptype);
7133 this->seen_ = false;
7134 if (r)
7136 *ptype = Type::lookup_integer_type("int");
7137 return true;
7141 else if (this->code_ == BUILTIN_SIZEOF
7142 || this->code_ == BUILTIN_ALIGNOF)
7144 Expression* arg = this->one_arg();
7145 if (arg == NULL)
7146 return false;
7147 Type* arg_type = arg->type();
7148 if (arg_type->is_error())
7149 return false;
7150 if (arg_type->is_abstract())
7151 return false;
7152 if (arg_type->named_type() != NULL)
7153 arg_type->named_type()->convert(this->gogo_);
7154 tree arg_type_tree = arg_type->get_tree(this->gogo_);
7155 if (arg_type_tree == error_mark_node)
7156 return false;
7157 unsigned long val_long;
7158 if (this->code_ == BUILTIN_SIZEOF)
7160 tree type_size = TYPE_SIZE_UNIT(arg_type_tree);
7161 gcc_assert(TREE_CODE(type_size) == INTEGER_CST);
7162 if (TREE_INT_CST_HIGH(type_size) != 0)
7163 return false;
7164 unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(type_size);
7165 val_long = static_cast<unsigned long>(val_wide);
7166 if (val_long != val_wide)
7167 return false;
7169 else if (this->code_ == BUILTIN_ALIGNOF)
7171 if (arg->field_reference_expression() == NULL)
7172 val_long = go_type_alignment(arg_type_tree);
7173 else
7175 // Calling unsafe.Alignof(s.f) returns the alignment of
7176 // the type of f when it is used as a field in a struct.
7177 val_long = go_field_alignment(arg_type_tree);
7180 else
7181 gcc_unreachable();
7182 mpz_set_ui(val, val_long);
7183 *ptype = NULL;
7184 return true;
7186 else if (this->code_ == BUILTIN_OFFSETOF)
7188 Expression* arg = this->one_arg();
7189 if (arg == NULL)
7190 return false;
7191 Field_reference_expression* farg = arg->field_reference_expression();
7192 if (farg == NULL)
7193 return false;
7194 Expression* struct_expr = farg->expr();
7195 Type* st = struct_expr->type();
7196 if (st->struct_type() == NULL)
7197 return false;
7198 if (st->named_type() != NULL)
7199 st->named_type()->convert(this->gogo_);
7200 tree struct_tree = st->get_tree(this->gogo_);
7201 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
7202 tree field = TYPE_FIELDS(struct_tree);
7203 for (unsigned int index = farg->field_index(); index > 0; --index)
7205 field = DECL_CHAIN(field);
7206 gcc_assert(field != NULL_TREE);
7208 HOST_WIDE_INT offset_wide = int_byte_position (field);
7209 if (offset_wide < 0)
7210 return false;
7211 unsigned long offset_long = static_cast<unsigned long>(offset_wide);
7212 if (offset_long != static_cast<unsigned HOST_WIDE_INT>(offset_wide))
7213 return false;
7214 mpz_set_ui(val, offset_long);
7215 return true;
7217 return false;
7220 // Return a floating point constant value if possible.
7222 bool
7223 Builtin_call_expression::do_float_constant_value(mpfr_t val,
7224 Type** ptype) const
7226 if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7228 Expression* arg = this->one_arg();
7229 if (arg == NULL)
7230 return false;
7232 mpfr_t real;
7233 mpfr_t imag;
7234 mpfr_init(real);
7235 mpfr_init(imag);
7237 bool ret = false;
7238 Type* type;
7239 if (arg->complex_constant_value(real, imag, &type))
7241 if (this->code_ == BUILTIN_REAL)
7242 mpfr_set(val, real, GMP_RNDN);
7243 else
7244 mpfr_set(val, imag, GMP_RNDN);
7245 *ptype = Builtin_call_expression::real_imag_type(type);
7246 ret = true;
7249 mpfr_clear(real);
7250 mpfr_clear(imag);
7251 return ret;
7254 return false;
7257 // Return a complex constant value if possible.
7259 bool
7260 Builtin_call_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
7261 Type** ptype) const
7263 if (this->code_ == BUILTIN_COMPLEX)
7265 const Expression_list* args = this->args();
7266 if (args == NULL || args->size() != 2)
7267 return false;
7269 mpfr_t r;
7270 mpfr_init(r);
7271 Type* rtype;
7272 if (!args->front()->float_constant_value(r, &rtype))
7274 mpfr_clear(r);
7275 return false;
7278 mpfr_t i;
7279 mpfr_init(i);
7281 bool ret = false;
7282 Type* itype;
7283 if (args->back()->float_constant_value(i, &itype)
7284 && Type::are_identical(rtype, itype, false, NULL))
7286 mpfr_set(real, r, GMP_RNDN);
7287 mpfr_set(imag, i, GMP_RNDN);
7288 *ptype = Builtin_call_expression::complex_type(rtype);
7289 ret = true;
7292 mpfr_clear(r);
7293 mpfr_clear(i);
7295 return ret;
7298 return false;
7301 // Return the type.
7303 Type*
7304 Builtin_call_expression::do_type()
7306 switch (this->code_)
7308 case BUILTIN_INVALID:
7309 default:
7310 gcc_unreachable();
7312 case BUILTIN_NEW:
7313 case BUILTIN_MAKE:
7315 const Expression_list* args = this->args();
7316 if (args == NULL || args->empty())
7317 return Type::make_error_type();
7318 return Type::make_pointer_type(args->front()->type());
7321 case BUILTIN_CAP:
7322 case BUILTIN_COPY:
7323 case BUILTIN_LEN:
7324 case BUILTIN_ALIGNOF:
7325 case BUILTIN_OFFSETOF:
7326 case BUILTIN_SIZEOF:
7327 return Type::lookup_integer_type("int");
7329 case BUILTIN_CLOSE:
7330 case BUILTIN_PANIC:
7331 case BUILTIN_PRINT:
7332 case BUILTIN_PRINTLN:
7333 return Type::make_void_type();
7335 case BUILTIN_RECOVER:
7336 return Type::make_interface_type(NULL, BUILTINS_LOCATION);
7338 case BUILTIN_APPEND:
7340 const Expression_list* args = this->args();
7341 if (args == NULL || args->empty())
7342 return Type::make_error_type();
7343 return args->front()->type();
7346 case BUILTIN_REAL:
7347 case BUILTIN_IMAG:
7349 Expression* arg = this->one_arg();
7350 if (arg == NULL)
7351 return Type::make_error_type();
7352 Type* t = arg->type();
7353 if (t->is_abstract())
7354 t = t->make_non_abstract_type();
7355 t = Builtin_call_expression::real_imag_type(t);
7356 if (t == NULL)
7357 t = Type::make_error_type();
7358 return t;
7361 case BUILTIN_COMPLEX:
7363 const Expression_list* args = this->args();
7364 if (args == NULL || args->size() != 2)
7365 return Type::make_error_type();
7366 Type* t = args->front()->type();
7367 if (t->is_abstract())
7369 t = args->back()->type();
7370 if (t->is_abstract())
7371 t = t->make_non_abstract_type();
7373 t = Builtin_call_expression::complex_type(t);
7374 if (t == NULL)
7375 t = Type::make_error_type();
7376 return t;
7381 // Determine the type.
7383 void
7384 Builtin_call_expression::do_determine_type(const Type_context* context)
7386 if (!this->determining_types())
7387 return;
7389 this->fn()->determine_type_no_context();
7391 const Expression_list* args = this->args();
7393 bool is_print;
7394 Type* arg_type = NULL;
7395 switch (this->code_)
7397 case BUILTIN_PRINT:
7398 case BUILTIN_PRINTLN:
7399 // Do not force a large integer constant to "int".
7400 is_print = true;
7401 break;
7403 case BUILTIN_REAL:
7404 case BUILTIN_IMAG:
7405 arg_type = Builtin_call_expression::complex_type(context->type);
7406 is_print = false;
7407 break;
7409 case BUILTIN_COMPLEX:
7411 // For the complex function the type of one operand can
7412 // determine the type of the other, as in a binary expression.
7413 arg_type = Builtin_call_expression::real_imag_type(context->type);
7414 if (args != NULL && args->size() == 2)
7416 Type* t1 = args->front()->type();
7417 Type* t2 = args->front()->type();
7418 if (!t1->is_abstract())
7419 arg_type = t1;
7420 else if (!t2->is_abstract())
7421 arg_type = t2;
7423 is_print = false;
7425 break;
7427 default:
7428 is_print = false;
7429 break;
7432 if (args != NULL)
7434 for (Expression_list::const_iterator pa = args->begin();
7435 pa != args->end();
7436 ++pa)
7438 Type_context subcontext;
7439 subcontext.type = arg_type;
7441 if (is_print)
7443 // We want to print large constants, we so can't just
7444 // use the appropriate nonabstract type. Use uint64 for
7445 // an integer if we know it is nonnegative, otherwise
7446 // use int64 for a integer, otherwise use float64 for a
7447 // float or complex128 for a complex.
7448 Type* want_type = NULL;
7449 Type* atype = (*pa)->type();
7450 if (atype->is_abstract())
7452 if (atype->integer_type() != NULL)
7454 mpz_t val;
7455 mpz_init(val);
7456 Type* dummy;
7457 if (this->integer_constant_value(true, val, &dummy)
7458 && mpz_sgn(val) >= 0)
7459 want_type = Type::lookup_integer_type("uint64");
7460 else
7461 want_type = Type::lookup_integer_type("int64");
7462 mpz_clear(val);
7464 else if (atype->float_type() != NULL)
7465 want_type = Type::lookup_float_type("float64");
7466 else if (atype->complex_type() != NULL)
7467 want_type = Type::lookup_complex_type("complex128");
7468 else if (atype->is_abstract_string_type())
7469 want_type = Type::lookup_string_type();
7470 else if (atype->is_abstract_boolean_type())
7471 want_type = Type::lookup_bool_type();
7472 else
7473 gcc_unreachable();
7474 subcontext.type = want_type;
7478 (*pa)->determine_type(&subcontext);
7483 // If there is exactly one argument, return true. Otherwise give an
7484 // error message and return false.
7486 bool
7487 Builtin_call_expression::check_one_arg()
7489 const Expression_list* args = this->args();
7490 if (args == NULL || args->size() < 1)
7492 this->report_error(_("not enough arguments"));
7493 return false;
7495 else if (args->size() > 1)
7497 this->report_error(_("too many arguments"));
7498 return false;
7500 if (args->front()->is_error_expression()
7501 || args->front()->type()->is_error())
7503 this->set_is_error();
7504 return false;
7506 return true;
7509 // Check argument types for a builtin function.
7511 void
7512 Builtin_call_expression::do_check_types(Gogo*)
7514 switch (this->code_)
7516 case BUILTIN_INVALID:
7517 case BUILTIN_NEW:
7518 case BUILTIN_MAKE:
7519 return;
7521 case BUILTIN_LEN:
7522 case BUILTIN_CAP:
7524 // The single argument may be either a string or an array or a
7525 // map or a channel, or a pointer to a closed array.
7526 if (this->check_one_arg())
7528 Type* arg_type = this->one_arg()->type();
7529 if (arg_type->points_to() != NULL
7530 && arg_type->points_to()->array_type() != NULL
7531 && !arg_type->points_to()->is_open_array_type())
7532 arg_type = arg_type->points_to();
7533 if (this->code_ == BUILTIN_CAP)
7535 if (!arg_type->is_error()
7536 && arg_type->array_type() == NULL
7537 && arg_type->channel_type() == NULL)
7538 this->report_error(_("argument must be array or slice "
7539 "or channel"));
7541 else
7543 if (!arg_type->is_error()
7544 && !arg_type->is_string_type()
7545 && arg_type->array_type() == NULL
7546 && arg_type->map_type() == NULL
7547 && arg_type->channel_type() == NULL)
7548 this->report_error(_("argument must be string or "
7549 "array or slice or map or channel"));
7553 break;
7555 case BUILTIN_PRINT:
7556 case BUILTIN_PRINTLN:
7558 const Expression_list* args = this->args();
7559 if (args == NULL)
7561 if (this->code_ == BUILTIN_PRINT)
7562 warning_at(this->location(), 0,
7563 "no arguments for builtin function %<%s%>",
7564 (this->code_ == BUILTIN_PRINT
7565 ? "print"
7566 : "println"));
7568 else
7570 for (Expression_list::const_iterator p = args->begin();
7571 p != args->end();
7572 ++p)
7574 Type* type = (*p)->type();
7575 if (type->is_error()
7576 || type->is_string_type()
7577 || type->integer_type() != NULL
7578 || type->float_type() != NULL
7579 || type->complex_type() != NULL
7580 || type->is_boolean_type()
7581 || type->points_to() != NULL
7582 || type->interface_type() != NULL
7583 || type->channel_type() != NULL
7584 || type->map_type() != NULL
7585 || type->function_type() != NULL
7586 || type->is_open_array_type())
7588 else
7589 this->report_error(_("unsupported argument type to "
7590 "builtin function"));
7594 break;
7596 case BUILTIN_CLOSE:
7597 if (this->check_one_arg())
7599 if (this->one_arg()->type()->channel_type() == NULL)
7600 this->report_error(_("argument must be channel"));
7602 break;
7604 case BUILTIN_PANIC:
7605 case BUILTIN_SIZEOF:
7606 case BUILTIN_ALIGNOF:
7607 this->check_one_arg();
7608 break;
7610 case BUILTIN_RECOVER:
7611 if (this->args() != NULL && !this->args()->empty())
7612 this->report_error(_("too many arguments"));
7613 break;
7615 case BUILTIN_OFFSETOF:
7616 if (this->check_one_arg())
7618 Expression* arg = this->one_arg();
7619 if (arg->field_reference_expression() == NULL)
7620 this->report_error(_("argument must be a field reference"));
7622 break;
7624 case BUILTIN_COPY:
7626 const Expression_list* args = this->args();
7627 if (args == NULL || args->size() < 2)
7629 this->report_error(_("not enough arguments"));
7630 break;
7632 else if (args->size() > 2)
7634 this->report_error(_("too many arguments"));
7635 break;
7637 Type* arg1_type = args->front()->type();
7638 Type* arg2_type = args->back()->type();
7639 if (arg1_type->is_error() || arg2_type->is_error())
7640 break;
7642 Type* e1;
7643 if (arg1_type->is_open_array_type())
7644 e1 = arg1_type->array_type()->element_type();
7645 else
7647 this->report_error(_("left argument must be a slice"));
7648 break;
7651 Type* e2;
7652 if (arg2_type->is_open_array_type())
7653 e2 = arg2_type->array_type()->element_type();
7654 else if (arg2_type->is_string_type())
7655 e2 = Type::lookup_integer_type("uint8");
7656 else
7658 this->report_error(_("right argument must be a slice or a string"));
7659 break;
7662 if (!Type::are_identical(e1, e2, true, NULL))
7663 this->report_error(_("element types must be the same"));
7665 break;
7667 case BUILTIN_APPEND:
7669 const Expression_list* args = this->args();
7670 if (args == NULL || args->size() < 2)
7672 this->report_error(_("not enough arguments"));
7673 break;
7675 if (args->size() > 2)
7677 this->report_error(_("too many arguments"));
7678 break;
7680 std::string reason;
7681 if (!Type::are_assignable(args->front()->type(), args->back()->type(),
7682 &reason))
7684 if (reason.empty())
7685 this->report_error(_("arguments 1 and 2 have different types"));
7686 else
7688 error_at(this->location(),
7689 "arguments 1 and 2 have different types (%s)",
7690 reason.c_str());
7691 this->set_is_error();
7694 break;
7697 case BUILTIN_REAL:
7698 case BUILTIN_IMAG:
7699 if (this->check_one_arg())
7701 if (this->one_arg()->type()->complex_type() == NULL)
7702 this->report_error(_("argument must have complex type"));
7704 break;
7706 case BUILTIN_COMPLEX:
7708 const Expression_list* args = this->args();
7709 if (args == NULL || args->size() < 2)
7710 this->report_error(_("not enough arguments"));
7711 else if (args->size() > 2)
7712 this->report_error(_("too many arguments"));
7713 else if (args->front()->is_error_expression()
7714 || args->front()->type()->is_error()
7715 || args->back()->is_error_expression()
7716 || args->back()->type()->is_error())
7717 this->set_is_error();
7718 else if (!Type::are_identical(args->front()->type(),
7719 args->back()->type(), true, NULL))
7720 this->report_error(_("complex arguments must have identical types"));
7721 else if (args->front()->type()->float_type() == NULL)
7722 this->report_error(_("complex arguments must have "
7723 "floating-point type"));
7725 break;
7727 default:
7728 gcc_unreachable();
7732 // Return the tree for a builtin function.
7734 tree
7735 Builtin_call_expression::do_get_tree(Translate_context* context)
7737 Gogo* gogo = context->gogo();
7738 source_location location = this->location();
7739 switch (this->code_)
7741 case BUILTIN_INVALID:
7742 case BUILTIN_NEW:
7743 case BUILTIN_MAKE:
7744 gcc_unreachable();
7746 case BUILTIN_LEN:
7747 case BUILTIN_CAP:
7749 const Expression_list* args = this->args();
7750 gcc_assert(args != NULL && args->size() == 1);
7751 Expression* arg = *args->begin();
7752 Type* arg_type = arg->type();
7754 if (this->seen_)
7756 gcc_assert(saw_errors());
7757 return error_mark_node;
7759 this->seen_ = true;
7761 tree arg_tree = arg->get_tree(context);
7763 this->seen_ = false;
7765 if (arg_tree == error_mark_node)
7766 return error_mark_node;
7768 if (arg_type->points_to() != NULL)
7770 arg_type = arg_type->points_to();
7771 gcc_assert(arg_type->array_type() != NULL
7772 && !arg_type->is_open_array_type());
7773 gcc_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7774 arg_tree = build_fold_indirect_ref(arg_tree);
7777 tree val_tree;
7778 if (this->code_ == BUILTIN_LEN)
7780 if (arg_type->is_string_type())
7781 val_tree = String_type::length_tree(gogo, arg_tree);
7782 else if (arg_type->array_type() != NULL)
7784 if (this->seen_)
7786 gcc_assert(saw_errors());
7787 return error_mark_node;
7789 this->seen_ = true;
7790 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7791 this->seen_ = false;
7793 else if (arg_type->map_type() != NULL)
7795 static tree map_len_fndecl;
7796 val_tree = Gogo::call_builtin(&map_len_fndecl,
7797 location,
7798 "__go_map_len",
7800 integer_type_node,
7801 arg_type->get_tree(gogo),
7802 arg_tree);
7804 else if (arg_type->channel_type() != NULL)
7806 static tree chan_len_fndecl;
7807 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7808 location,
7809 "__go_chan_len",
7811 integer_type_node,
7812 arg_type->get_tree(gogo),
7813 arg_tree);
7815 else
7816 gcc_unreachable();
7818 else
7820 if (arg_type->array_type() != NULL)
7822 if (this->seen_)
7824 gcc_assert(saw_errors());
7825 return error_mark_node;
7827 this->seen_ = true;
7828 val_tree = arg_type->array_type()->capacity_tree(gogo,
7829 arg_tree);
7830 this->seen_ = false;
7832 else if (arg_type->channel_type() != NULL)
7834 static tree chan_cap_fndecl;
7835 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7836 location,
7837 "__go_chan_cap",
7839 integer_type_node,
7840 arg_type->get_tree(gogo),
7841 arg_tree);
7843 else
7844 gcc_unreachable();
7847 if (val_tree == error_mark_node)
7848 return error_mark_node;
7850 tree type_tree = Type::lookup_integer_type("int")->get_tree(gogo);
7851 if (type_tree == TREE_TYPE(val_tree))
7852 return val_tree;
7853 else
7854 return fold(convert_to_integer(type_tree, val_tree));
7857 case BUILTIN_PRINT:
7858 case BUILTIN_PRINTLN:
7860 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7861 tree stmt_list = NULL_TREE;
7863 const Expression_list* call_args = this->args();
7864 if (call_args != NULL)
7866 for (Expression_list::const_iterator p = call_args->begin();
7867 p != call_args->end();
7868 ++p)
7870 if (is_ln && p != call_args->begin())
7872 static tree print_space_fndecl;
7873 tree call = Gogo::call_builtin(&print_space_fndecl,
7874 location,
7875 "__go_print_space",
7877 void_type_node);
7878 if (call == error_mark_node)
7879 return error_mark_node;
7880 append_to_statement_list(call, &stmt_list);
7883 Type* type = (*p)->type();
7885 tree arg = (*p)->get_tree(context);
7886 if (arg == error_mark_node)
7887 return error_mark_node;
7889 tree* pfndecl;
7890 const char* fnname;
7891 if (type->is_string_type())
7893 static tree print_string_fndecl;
7894 pfndecl = &print_string_fndecl;
7895 fnname = "__go_print_string";
7897 else if (type->integer_type() != NULL
7898 && type->integer_type()->is_unsigned())
7900 static tree print_uint64_fndecl;
7901 pfndecl = &print_uint64_fndecl;
7902 fnname = "__go_print_uint64";
7903 Type* itype = Type::lookup_integer_type("uint64");
7904 arg = fold_convert_loc(location, itype->get_tree(gogo),
7905 arg);
7907 else if (type->integer_type() != NULL)
7909 static tree print_int64_fndecl;
7910 pfndecl = &print_int64_fndecl;
7911 fnname = "__go_print_int64";
7912 Type* itype = Type::lookup_integer_type("int64");
7913 arg = fold_convert_loc(location, itype->get_tree(gogo),
7914 arg);
7916 else if (type->float_type() != NULL)
7918 static tree print_double_fndecl;
7919 pfndecl = &print_double_fndecl;
7920 fnname = "__go_print_double";
7921 arg = fold_convert_loc(location, double_type_node, arg);
7923 else if (type->complex_type() != NULL)
7925 static tree print_complex_fndecl;
7926 pfndecl = &print_complex_fndecl;
7927 fnname = "__go_print_complex";
7928 arg = fold_convert_loc(location, complex_double_type_node,
7929 arg);
7931 else if (type->is_boolean_type())
7933 static tree print_bool_fndecl;
7934 pfndecl = &print_bool_fndecl;
7935 fnname = "__go_print_bool";
7937 else if (type->points_to() != NULL
7938 || type->channel_type() != NULL
7939 || type->map_type() != NULL
7940 || type->function_type() != NULL)
7942 static tree print_pointer_fndecl;
7943 pfndecl = &print_pointer_fndecl;
7944 fnname = "__go_print_pointer";
7945 arg = fold_convert_loc(location, ptr_type_node, arg);
7947 else if (type->interface_type() != NULL)
7949 if (type->interface_type()->is_empty())
7951 static tree print_empty_interface_fndecl;
7952 pfndecl = &print_empty_interface_fndecl;
7953 fnname = "__go_print_empty_interface";
7955 else
7957 static tree print_interface_fndecl;
7958 pfndecl = &print_interface_fndecl;
7959 fnname = "__go_print_interface";
7962 else if (type->is_open_array_type())
7964 static tree print_slice_fndecl;
7965 pfndecl = &print_slice_fndecl;
7966 fnname = "__go_print_slice";
7968 else
7969 gcc_unreachable();
7971 tree call = Gogo::call_builtin(pfndecl,
7972 location,
7973 fnname,
7975 void_type_node,
7976 TREE_TYPE(arg),
7977 arg);
7978 if (call == error_mark_node)
7979 return error_mark_node;
7980 append_to_statement_list(call, &stmt_list);
7984 if (is_ln)
7986 static tree print_nl_fndecl;
7987 tree call = Gogo::call_builtin(&print_nl_fndecl,
7988 location,
7989 "__go_print_nl",
7991 void_type_node);
7992 if (call == error_mark_node)
7993 return error_mark_node;
7994 append_to_statement_list(call, &stmt_list);
7997 return stmt_list;
8000 case BUILTIN_PANIC:
8002 const Expression_list* args = this->args();
8003 gcc_assert(args != NULL && args->size() == 1);
8004 Expression* arg = args->front();
8005 tree arg_tree = arg->get_tree(context);
8006 if (arg_tree == error_mark_node)
8007 return error_mark_node;
8008 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
8009 arg_tree = Expression::convert_for_assignment(context, empty,
8010 arg->type(),
8011 arg_tree, location);
8012 static tree panic_fndecl;
8013 tree call = Gogo::call_builtin(&panic_fndecl,
8014 location,
8015 "__go_panic",
8017 void_type_node,
8018 TREE_TYPE(arg_tree),
8019 arg_tree);
8020 if (call == error_mark_node)
8021 return error_mark_node;
8022 // This function will throw an exception.
8023 TREE_NOTHROW(panic_fndecl) = 0;
8024 // This function will not return.
8025 TREE_THIS_VOLATILE(panic_fndecl) = 1;
8026 return call;
8029 case BUILTIN_RECOVER:
8031 // The argument is set when building recover thunks. It's a
8032 // boolean value which is true if we can recover a value now.
8033 const Expression_list* args = this->args();
8034 gcc_assert(args != NULL && args->size() == 1);
8035 Expression* arg = args->front();
8036 tree arg_tree = arg->get_tree(context);
8037 if (arg_tree == error_mark_node)
8038 return error_mark_node;
8040 Type *empty = Type::make_interface_type(NULL, BUILTINS_LOCATION);
8041 tree empty_tree = empty->get_tree(context->gogo());
8043 Type* nil_type = Type::make_nil_type();
8044 Expression* nil = Expression::make_nil(location);
8045 tree nil_tree = nil->get_tree(context);
8046 tree empty_nil_tree = Expression::convert_for_assignment(context,
8047 empty,
8048 nil_type,
8049 nil_tree,
8050 location);
8052 // We need to handle a deferred call to recover specially,
8053 // because it changes whether it can recover a panic or not.
8054 // See test7 in test/recover1.go.
8055 tree call;
8056 if (this->is_deferred())
8058 static tree deferred_recover_fndecl;
8059 call = Gogo::call_builtin(&deferred_recover_fndecl,
8060 location,
8061 "__go_deferred_recover",
8063 empty_tree);
8065 else
8067 static tree recover_fndecl;
8068 call = Gogo::call_builtin(&recover_fndecl,
8069 location,
8070 "__go_recover",
8072 empty_tree);
8074 if (call == error_mark_node)
8075 return error_mark_node;
8076 return fold_build3_loc(location, COND_EXPR, empty_tree, arg_tree,
8077 call, empty_nil_tree);
8080 case BUILTIN_CLOSE:
8082 const Expression_list* args = this->args();
8083 gcc_assert(args != NULL && args->size() == 1);
8084 Expression* arg = args->front();
8085 tree arg_tree = arg->get_tree(context);
8086 if (arg_tree == error_mark_node)
8087 return error_mark_node;
8088 static tree close_fndecl;
8089 return Gogo::call_builtin(&close_fndecl,
8090 location,
8091 "__go_builtin_close",
8093 void_type_node,
8094 TREE_TYPE(arg_tree),
8095 arg_tree);
8098 case BUILTIN_SIZEOF:
8099 case BUILTIN_OFFSETOF:
8100 case BUILTIN_ALIGNOF:
8102 mpz_t val;
8103 mpz_init(val);
8104 Type* dummy;
8105 bool b = this->integer_constant_value(true, val, &dummy);
8106 if (!b)
8108 gcc_assert(saw_errors());
8109 return error_mark_node;
8111 tree type = Type::lookup_integer_type("int")->get_tree(gogo);
8112 tree ret = Expression::integer_constant_tree(val, type);
8113 mpz_clear(val);
8114 return ret;
8117 case BUILTIN_COPY:
8119 const Expression_list* args = this->args();
8120 gcc_assert(args != NULL && args->size() == 2);
8121 Expression* arg1 = args->front();
8122 Expression* arg2 = args->back();
8124 tree arg1_tree = arg1->get_tree(context);
8125 tree arg2_tree = arg2->get_tree(context);
8126 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8127 return error_mark_node;
8129 Type* arg1_type = arg1->type();
8130 Array_type* at = arg1_type->array_type();
8131 arg1_tree = save_expr(arg1_tree);
8132 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
8133 tree arg1_len = at->length_tree(gogo, arg1_tree);
8134 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
8135 return error_mark_node;
8137 Type* arg2_type = arg2->type();
8138 tree arg2_val;
8139 tree arg2_len;
8140 if (arg2_type->is_open_array_type())
8142 at = arg2_type->array_type();
8143 arg2_tree = save_expr(arg2_tree);
8144 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8145 arg2_len = at->length_tree(gogo, arg2_tree);
8147 else
8149 arg2_tree = save_expr(arg2_tree);
8150 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8151 arg2_len = String_type::length_tree(gogo, arg2_tree);
8153 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8154 return error_mark_node;
8156 arg1_len = save_expr(arg1_len);
8157 arg2_len = save_expr(arg2_len);
8158 tree len = fold_build3_loc(location, COND_EXPR, TREE_TYPE(arg1_len),
8159 fold_build2_loc(location, LT_EXPR,
8160 boolean_type_node,
8161 arg1_len, arg2_len),
8162 arg1_len, arg2_len);
8163 len = save_expr(len);
8165 Type* element_type = at->element_type();
8166 tree element_type_tree = element_type->get_tree(gogo);
8167 if (element_type_tree == error_mark_node)
8168 return error_mark_node;
8169 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8170 tree bytecount = fold_convert_loc(location, TREE_TYPE(element_size),
8171 len);
8172 bytecount = fold_build2_loc(location, MULT_EXPR,
8173 TREE_TYPE(element_size),
8174 bytecount, element_size);
8175 bytecount = fold_convert_loc(location, size_type_node, bytecount);
8177 arg1_val = fold_convert_loc(location, ptr_type_node, arg1_val);
8178 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8180 static tree copy_fndecl;
8181 tree call = Gogo::call_builtin(&copy_fndecl,
8182 location,
8183 "__go_copy",
8185 void_type_node,
8186 ptr_type_node,
8187 arg1_val,
8188 ptr_type_node,
8189 arg2_val,
8190 size_type_node,
8191 bytecount);
8192 if (call == error_mark_node)
8193 return error_mark_node;
8195 return fold_build2_loc(location, COMPOUND_EXPR, TREE_TYPE(len),
8196 call, len);
8199 case BUILTIN_APPEND:
8201 const Expression_list* args = this->args();
8202 gcc_assert(args != NULL && args->size() == 2);
8203 Expression* arg1 = args->front();
8204 Expression* arg2 = args->back();
8206 tree arg1_tree = arg1->get_tree(context);
8207 tree arg2_tree = arg2->get_tree(context);
8208 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8209 return error_mark_node;
8211 Array_type* at = arg1->type()->array_type();
8212 Type* element_type = at->element_type();
8214 arg2_tree = Expression::convert_for_assignment(context, at,
8215 arg2->type(),
8216 arg2_tree,
8217 location);
8218 if (arg2_tree == error_mark_node)
8219 return error_mark_node;
8221 arg2_tree = save_expr(arg2_tree);
8222 tree arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8223 tree arg2_len = at->length_tree(gogo, arg2_tree);
8224 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8225 return error_mark_node;
8226 arg2_val = fold_convert_loc(location, ptr_type_node, arg2_val);
8227 arg2_len = fold_convert_loc(location, size_type_node, arg2_len);
8229 tree element_type_tree = element_type->get_tree(gogo);
8230 if (element_type_tree == error_mark_node)
8231 return error_mark_node;
8232 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8233 element_size = fold_convert_loc(location, size_type_node,
8234 element_size);
8236 // We rebuild the decl each time since the slice types may
8237 // change.
8238 tree append_fndecl = NULL_TREE;
8239 return Gogo::call_builtin(&append_fndecl,
8240 location,
8241 "__go_append",
8243 TREE_TYPE(arg1_tree),
8244 TREE_TYPE(arg1_tree),
8245 arg1_tree,
8246 ptr_type_node,
8247 arg2_val,
8248 size_type_node,
8249 arg2_len,
8250 size_type_node,
8251 element_size);
8254 case BUILTIN_REAL:
8255 case BUILTIN_IMAG:
8257 const Expression_list* args = this->args();
8258 gcc_assert(args != NULL && args->size() == 1);
8259 Expression* arg = args->front();
8260 tree arg_tree = arg->get_tree(context);
8261 if (arg_tree == error_mark_node)
8262 return error_mark_node;
8263 gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8264 if (this->code_ == BUILTIN_REAL)
8265 return fold_build1_loc(location, REALPART_EXPR,
8266 TREE_TYPE(TREE_TYPE(arg_tree)),
8267 arg_tree);
8268 else
8269 return fold_build1_loc(location, IMAGPART_EXPR,
8270 TREE_TYPE(TREE_TYPE(arg_tree)),
8271 arg_tree);
8274 case BUILTIN_COMPLEX:
8276 const Expression_list* args = this->args();
8277 gcc_assert(args != NULL && args->size() == 2);
8278 tree r = args->front()->get_tree(context);
8279 tree i = args->back()->get_tree(context);
8280 if (r == error_mark_node || i == error_mark_node)
8281 return error_mark_node;
8282 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8283 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8284 gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8285 return fold_build2_loc(location, COMPLEX_EXPR,
8286 build_complex_type(TREE_TYPE(r)),
8287 r, i);
8290 default:
8291 gcc_unreachable();
8295 // We have to support exporting a builtin call expression, because
8296 // code can set a constant to the result of a builtin expression.
8298 void
8299 Builtin_call_expression::do_export(Export* exp) const
8301 bool ok = false;
8303 mpz_t val;
8304 mpz_init(val);
8305 Type* dummy;
8306 if (this->integer_constant_value(true, val, &dummy))
8308 Integer_expression::export_integer(exp, val);
8309 ok = true;
8311 mpz_clear(val);
8313 if (!ok)
8315 mpfr_t fval;
8316 mpfr_init(fval);
8317 if (this->float_constant_value(fval, &dummy))
8319 Float_expression::export_float(exp, fval);
8320 ok = true;
8322 mpfr_clear(fval);
8325 if (!ok)
8327 mpfr_t real;
8328 mpfr_t imag;
8329 mpfr_init(real);
8330 mpfr_init(imag);
8331 if (this->complex_constant_value(real, imag, &dummy))
8333 Complex_expression::export_complex(exp, real, imag);
8334 ok = true;
8336 mpfr_clear(real);
8337 mpfr_clear(imag);
8340 if (!ok)
8342 error_at(this->location(), "value is not constant");
8343 return;
8346 // A trailing space lets us reliably identify the end of the number.
8347 exp->write_c_string(" ");
8350 // Class Call_expression.
8352 // Traversal.
8355 Call_expression::do_traverse(Traverse* traverse)
8357 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8358 return TRAVERSE_EXIT;
8359 if (this->args_ != NULL)
8361 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8362 return TRAVERSE_EXIT;
8364 return TRAVERSE_CONTINUE;
8367 // Lower a call statement.
8369 Expression*
8370 Call_expression::do_lower(Gogo* gogo, Named_object* function, int)
8372 // A type case can look like a function call.
8373 if (this->fn_->is_type_expression()
8374 && this->args_ != NULL
8375 && this->args_->size() == 1)
8376 return Expression::make_cast(this->fn_->type(), this->args_->front(),
8377 this->location());
8379 // Recognize a call to a builtin function.
8380 Func_expression* fne = this->fn_->func_expression();
8381 if (fne != NULL
8382 && fne->named_object()->is_function_declaration()
8383 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8384 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8385 this->is_varargs_, this->location());
8387 // Handle an argument which is a call to a function which returns
8388 // multiple results.
8389 if (this->args_ != NULL
8390 && this->args_->size() == 1
8391 && this->args_->front()->call_expression() != NULL
8392 && this->fn_->type()->function_type() != NULL)
8394 Function_type* fntype = this->fn_->type()->function_type();
8395 size_t rc = this->args_->front()->call_expression()->result_count();
8396 if (rc > 1
8397 && fntype->parameters() != NULL
8398 && (fntype->parameters()->size() == rc
8399 || (fntype->is_varargs()
8400 && fntype->parameters()->size() - 1 <= rc)))
8402 Call_expression* call = this->args_->front()->call_expression();
8403 Expression_list* args = new Expression_list;
8404 for (size_t i = 0; i < rc; ++i)
8405 args->push_back(Expression::make_call_result(call, i));
8406 // We can't return a new call expression here, because this
8407 // one may be referenced by Call_result expressions. We
8408 // also can't delete the old arguments, because we may still
8409 // traverse them somewhere up the call stack. FIXME.
8410 this->args_ = args;
8414 // Handle a call to a varargs function by packaging up the extra
8415 // parameters.
8416 if (this->fn_->type()->function_type() != NULL
8417 && this->fn_->type()->function_type()->is_varargs())
8419 Function_type* fntype = this->fn_->type()->function_type();
8420 const Typed_identifier_list* parameters = fntype->parameters();
8421 gcc_assert(parameters != NULL && !parameters->empty());
8422 Type* varargs_type = parameters->back().type();
8423 return this->lower_varargs(gogo, function, varargs_type,
8424 parameters->size());
8427 return this;
8430 // Lower a call to a varargs function. FUNCTION is the function in
8431 // which the call occurs--it's not the function we are calling.
8432 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8433 // PARAM_COUNT is the number of parameters of the function we are
8434 // calling; the last of these parameters will be the varargs
8435 // parameter.
8437 Expression*
8438 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8439 Type* varargs_type, size_t param_count)
8441 if (this->varargs_are_lowered_)
8442 return this;
8444 source_location loc = this->location();
8446 gcc_assert(param_count > 0);
8447 gcc_assert(varargs_type->is_open_array_type());
8449 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8450 if (arg_count < param_count - 1)
8452 // Not enough arguments; will be caught in check_types.
8453 return this;
8456 Expression_list* old_args = this->args_;
8457 Expression_list* new_args = new Expression_list();
8458 bool push_empty_arg = false;
8459 if (old_args == NULL || old_args->empty())
8461 gcc_assert(param_count == 1);
8462 push_empty_arg = true;
8464 else
8466 Expression_list::const_iterator pa;
8467 int i = 1;
8468 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8470 if (static_cast<size_t>(i) == param_count)
8471 break;
8472 new_args->push_back(*pa);
8475 // We have reached the varargs parameter.
8477 bool issued_error = false;
8478 if (pa == old_args->end())
8479 push_empty_arg = true;
8480 else if (pa + 1 == old_args->end() && this->is_varargs_)
8481 new_args->push_back(*pa);
8482 else if (this->is_varargs_)
8484 this->report_error(_("too many arguments"));
8485 return this;
8487 else
8489 Type* element_type = varargs_type->array_type()->element_type();
8490 Expression_list* vals = new Expression_list;
8491 for (; pa != old_args->end(); ++pa, ++i)
8493 // Check types here so that we get a better message.
8494 Type* patype = (*pa)->type();
8495 source_location paloc = (*pa)->location();
8496 if (!this->check_argument_type(i, element_type, patype,
8497 paloc, issued_error))
8498 continue;
8499 vals->push_back(*pa);
8501 Expression* val =
8502 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8503 new_args->push_back(val);
8507 if (push_empty_arg)
8508 new_args->push_back(Expression::make_nil(loc));
8510 // We can't return a new call expression here, because this one may
8511 // be referenced by Call_result expressions. FIXME.
8512 if (old_args != NULL)
8513 delete old_args;
8514 this->args_ = new_args;
8515 this->varargs_are_lowered_ = true;
8517 // Lower all the new subexpressions.
8518 Expression* ret = this;
8519 gogo->lower_expression(function, &ret);
8520 gcc_assert(ret == this);
8521 return ret;
8524 // Get the function type. Returns NULL if we don't know the type. If
8525 // this returns NULL, and if_ERROR is true, issues an error.
8527 Function_type*
8528 Call_expression::get_function_type() const
8530 return this->fn_->type()->function_type();
8533 // Return the number of values which this call will return.
8535 size_t
8536 Call_expression::result_count() const
8538 const Function_type* fntype = this->get_function_type();
8539 if (fntype == NULL)
8540 return 0;
8541 if (fntype->results() == NULL)
8542 return 0;
8543 return fntype->results()->size();
8546 // Return whether this is a call to the predeclared function recover.
8548 bool
8549 Call_expression::is_recover_call() const
8551 return this->do_is_recover_call();
8554 // Set the argument to the recover function.
8556 void
8557 Call_expression::set_recover_arg(Expression* arg)
8559 this->do_set_recover_arg(arg);
8562 // Virtual functions also implemented by Builtin_call_expression.
8564 bool
8565 Call_expression::do_is_recover_call() const
8567 return false;
8570 void
8571 Call_expression::do_set_recover_arg(Expression*)
8573 gcc_unreachable();
8576 // Get the type.
8578 Type*
8579 Call_expression::do_type()
8581 if (this->type_ != NULL)
8582 return this->type_;
8584 Type* ret;
8585 Function_type* fntype = this->get_function_type();
8586 if (fntype == NULL)
8587 return Type::make_error_type();
8589 const Typed_identifier_list* results = fntype->results();
8590 if (results == NULL)
8591 ret = Type::make_void_type();
8592 else if (results->size() == 1)
8593 ret = results->begin()->type();
8594 else
8595 ret = Type::make_call_multiple_result_type(this);
8597 this->type_ = ret;
8599 return this->type_;
8602 // Determine types for a call expression. We can use the function
8603 // parameter types to set the types of the arguments.
8605 void
8606 Call_expression::do_determine_type(const Type_context*)
8608 if (!this->determining_types())
8609 return;
8611 this->fn_->determine_type_no_context();
8612 Function_type* fntype = this->get_function_type();
8613 const Typed_identifier_list* parameters = NULL;
8614 if (fntype != NULL)
8615 parameters = fntype->parameters();
8616 if (this->args_ != NULL)
8618 Typed_identifier_list::const_iterator pt;
8619 if (parameters != NULL)
8620 pt = parameters->begin();
8621 for (Expression_list::const_iterator pa = this->args_->begin();
8622 pa != this->args_->end();
8623 ++pa)
8625 if (parameters != NULL && pt != parameters->end())
8627 Type_context subcontext(pt->type(), false);
8628 (*pa)->determine_type(&subcontext);
8629 ++pt;
8631 else
8632 (*pa)->determine_type_no_context();
8637 // Called when determining types for a Call_expression. Return true
8638 // if we should go ahead, false if they have already been determined.
8640 bool
8641 Call_expression::determining_types()
8643 if (this->types_are_determined_)
8644 return false;
8645 else
8647 this->types_are_determined_ = true;
8648 return true;
8652 // Check types for parameter I.
8654 bool
8655 Call_expression::check_argument_type(int i, const Type* parameter_type,
8656 const Type* argument_type,
8657 source_location argument_location,
8658 bool issued_error)
8660 std::string reason;
8661 if (!Type::are_assignable(parameter_type, argument_type, &reason))
8663 if (!issued_error)
8665 if (reason.empty())
8666 error_at(argument_location, "argument %d has incompatible type", i);
8667 else
8668 error_at(argument_location,
8669 "argument %d has incompatible type (%s)",
8670 i, reason.c_str());
8672 this->set_is_error();
8673 return false;
8675 return true;
8678 // Check types.
8680 void
8681 Call_expression::do_check_types(Gogo*)
8683 Function_type* fntype = this->get_function_type();
8684 if (fntype == NULL)
8686 if (!this->fn_->type()->is_error())
8687 this->report_error(_("expected function"));
8688 return;
8691 if (fntype->is_method())
8693 // We don't support pointers to methods, so the function has to
8694 // be a bound method expression.
8695 Bound_method_expression* bme = this->fn_->bound_method_expression();
8696 if (bme == NULL)
8698 this->report_error(_("method call without object"));
8699 return;
8701 Type* first_arg_type = bme->first_argument()->type();
8702 if (first_arg_type->points_to() == NULL)
8704 // When passing a value, we need to check that we are
8705 // permitted to copy it. The language permits copying
8706 // hidden fields for a method receiver.
8707 std::string reason;
8708 if (!Type::are_assignable_hidden_ok(fntype->receiver()->type(),
8709 first_arg_type, &reason))
8711 if (reason.empty())
8712 this->report_error(_("incompatible type for receiver"));
8713 else
8715 error_at(this->location(),
8716 "incompatible type for receiver (%s)",
8717 reason.c_str());
8718 this->set_is_error();
8724 // Note that varargs was handled by the lower_varargs() method, so
8725 // we don't have to worry about it here.
8727 const Typed_identifier_list* parameters = fntype->parameters();
8728 if (this->args_ == NULL)
8730 if (parameters != NULL && !parameters->empty())
8731 this->report_error(_("not enough arguments"));
8733 else if (parameters == NULL)
8734 this->report_error(_("too many arguments"));
8735 else
8737 int i = 0;
8738 Typed_identifier_list::const_iterator pt = parameters->begin();
8739 for (Expression_list::const_iterator pa = this->args_->begin();
8740 pa != this->args_->end();
8741 ++pa, ++pt, ++i)
8743 if (pt == parameters->end())
8745 this->report_error(_("too many arguments"));
8746 return;
8748 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8749 (*pa)->location(), false);
8751 if (pt != parameters->end())
8752 this->report_error(_("not enough arguments"));
8756 // Return whether we have to use a temporary variable to ensure that
8757 // we evaluate this call expression in order. If the call returns no
8758 // results then it will inevitably be executed last. If the call
8759 // returns more than one result then it will be used with Call_result
8760 // expressions. So we only have to use a temporary variable if the
8761 // call returns exactly one result.
8763 bool
8764 Call_expression::do_must_eval_in_order() const
8766 return this->result_count() == 1;
8769 // Get the function and the first argument to use when calling a bound
8770 // method.
8772 tree
8773 Call_expression::bound_method_function(Translate_context* context,
8774 Bound_method_expression* bound_method,
8775 tree* first_arg_ptr)
8777 Expression* first_argument = bound_method->first_argument();
8778 tree first_arg = first_argument->get_tree(context);
8779 if (first_arg == error_mark_node)
8780 return error_mark_node;
8782 // We always pass a pointer to the first argument when calling a
8783 // method.
8784 if (first_argument->type()->points_to() == NULL)
8786 tree pointer_to_arg_type = build_pointer_type(TREE_TYPE(first_arg));
8787 if (TREE_ADDRESSABLE(TREE_TYPE(first_arg))
8788 || DECL_P(first_arg)
8789 || TREE_CODE(first_arg) == INDIRECT_REF
8790 || TREE_CODE(first_arg) == COMPONENT_REF)
8792 first_arg = build_fold_addr_expr(first_arg);
8793 if (DECL_P(first_arg))
8794 TREE_ADDRESSABLE(first_arg) = 1;
8796 else
8798 tree tmp = create_tmp_var(TREE_TYPE(first_arg),
8799 get_name(first_arg));
8800 DECL_IGNORED_P(tmp) = 0;
8801 DECL_INITIAL(tmp) = first_arg;
8802 first_arg = build2(COMPOUND_EXPR, pointer_to_arg_type,
8803 build1(DECL_EXPR, void_type_node, tmp),
8804 build_fold_addr_expr(tmp));
8805 TREE_ADDRESSABLE(tmp) = 1;
8807 if (first_arg == error_mark_node)
8808 return error_mark_node;
8811 Type* fatype = bound_method->first_argument_type();
8812 if (fatype != NULL)
8814 if (fatype->points_to() == NULL)
8815 fatype = Type::make_pointer_type(fatype);
8816 first_arg = fold_convert(fatype->get_tree(context->gogo()), first_arg);
8817 if (first_arg == error_mark_node
8818 || TREE_TYPE(first_arg) == error_mark_node)
8819 return error_mark_node;
8822 *first_arg_ptr = first_arg;
8824 return bound_method->method()->get_tree(context);
8827 // Get the function and the first argument to use when calling an
8828 // interface method.
8830 tree
8831 Call_expression::interface_method_function(
8832 Translate_context* context,
8833 Interface_field_reference_expression* interface_method,
8834 tree* first_arg_ptr)
8836 tree expr = interface_method->expr()->get_tree(context);
8837 if (expr == error_mark_node)
8838 return error_mark_node;
8839 expr = save_expr(expr);
8840 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
8841 if (first_arg == error_mark_node)
8842 return error_mark_node;
8843 *first_arg_ptr = first_arg;
8844 return interface_method->get_function_tree(context, expr);
8847 // Build the call expression.
8849 tree
8850 Call_expression::do_get_tree(Translate_context* context)
8852 if (this->tree_ != NULL_TREE)
8853 return this->tree_;
8855 Function_type* fntype = this->get_function_type();
8856 if (fntype == NULL)
8857 return error_mark_node;
8859 if (this->fn_->is_error_expression())
8860 return error_mark_node;
8862 Gogo* gogo = context->gogo();
8863 source_location location = this->location();
8865 Func_expression* func = this->fn_->func_expression();
8866 Bound_method_expression* bound_method = this->fn_->bound_method_expression();
8867 Interface_field_reference_expression* interface_method =
8868 this->fn_->interface_field_reference_expression();
8869 const bool has_closure = func != NULL && func->closure() != NULL;
8870 const bool is_method = bound_method != NULL || interface_method != NULL;
8871 gcc_assert(!fntype->is_method() || is_method);
8873 int nargs;
8874 tree* args;
8875 if (this->args_ == NULL || this->args_->empty())
8877 nargs = is_method ? 1 : 0;
8878 args = nargs == 0 ? NULL : new tree[nargs];
8880 else
8882 const Typed_identifier_list* params = fntype->parameters();
8883 gcc_assert(params != NULL);
8885 nargs = this->args_->size();
8886 int i = is_method ? 1 : 0;
8887 nargs += i;
8888 args = new tree[nargs];
8890 Typed_identifier_list::const_iterator pp = params->begin();
8891 Expression_list::const_iterator pe;
8892 for (pe = this->args_->begin();
8893 pe != this->args_->end();
8894 ++pe, ++pp, ++i)
8896 gcc_assert(pp != params->end());
8897 tree arg_val = (*pe)->get_tree(context);
8898 args[i] = Expression::convert_for_assignment(context,
8899 pp->type(),
8900 (*pe)->type(),
8901 arg_val,
8902 location);
8903 if (args[i] == error_mark_node)
8905 delete[] args;
8906 return error_mark_node;
8909 gcc_assert(pp == params->end());
8910 gcc_assert(i == nargs);
8913 tree rettype = TREE_TYPE(TREE_TYPE(fntype->get_tree(gogo)));
8914 if (rettype == error_mark_node)
8916 delete[] args;
8917 return error_mark_node;
8920 tree fn;
8921 if (has_closure)
8922 fn = func->get_tree_without_closure(gogo);
8923 else if (!is_method)
8924 fn = this->fn_->get_tree(context);
8925 else if (bound_method != NULL)
8926 fn = this->bound_method_function(context, bound_method, &args[0]);
8927 else if (interface_method != NULL)
8928 fn = this->interface_method_function(context, interface_method, &args[0]);
8929 else
8930 gcc_unreachable();
8932 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
8934 delete[] args;
8935 return error_mark_node;
8938 tree fndecl = fn;
8939 if (TREE_CODE(fndecl) == ADDR_EXPR)
8940 fndecl = TREE_OPERAND(fndecl, 0);
8942 // Add a type cast in case the type of the function is a recursive
8943 // type which refers to itself.
8944 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
8946 tree fnt = fntype->get_tree(gogo);
8947 if (fnt == error_mark_node)
8948 return error_mark_node;
8949 fn = fold_convert_loc(location, fnt, fn);
8952 // This is to support builtin math functions when using 80387 math.
8953 tree excess_type = NULL_TREE;
8954 if (TREE_CODE(fndecl) == FUNCTION_DECL
8955 && DECL_IS_BUILTIN(fndecl)
8956 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
8957 && nargs > 0
8958 && ((SCALAR_FLOAT_TYPE_P(rettype)
8959 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
8960 || (COMPLEX_FLOAT_TYPE_P(rettype)
8961 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
8963 excess_type = excess_precision_type(TREE_TYPE(args[0]));
8964 if (excess_type != NULL_TREE)
8966 tree excess_fndecl = mathfn_built_in(excess_type,
8967 DECL_FUNCTION_CODE(fndecl));
8968 if (excess_fndecl == NULL_TREE)
8969 excess_type = NULL_TREE;
8970 else
8972 fn = build_fold_addr_expr_loc(location, excess_fndecl);
8973 for (int i = 0; i < nargs; ++i)
8974 args[i] = ::convert(excess_type, args[i]);
8979 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
8980 fn, nargs, args);
8981 delete[] args;
8983 SET_EXPR_LOCATION(ret, location);
8985 if (has_closure)
8987 tree closure_tree = func->closure()->get_tree(context);
8988 if (closure_tree != error_mark_node)
8989 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
8992 // If this is a recursive function type which returns itself, as in
8993 // type F func() F
8994 // we have used ptr_type_node for the return type. Add a cast here
8995 // to the correct type.
8996 if (TREE_TYPE(ret) == ptr_type_node)
8998 tree t = this->type()->base()->get_tree(gogo);
8999 ret = fold_convert_loc(location, t, ret);
9002 if (excess_type != NULL_TREE)
9004 // Calling convert here can undo our excess precision change.
9005 // That may or may not be a bug in convert_to_real.
9006 ret = build1(NOP_EXPR, rettype, ret);
9009 // If there is more than one result, we will refer to the call
9010 // multiple times.
9011 if (fntype->results() != NULL && fntype->results()->size() > 1)
9012 ret = save_expr(ret);
9014 this->tree_ = ret;
9016 return ret;
9019 // Make a call expression.
9021 Call_expression*
9022 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
9023 source_location location)
9025 return new Call_expression(fn, args, is_varargs, location);
9028 // A single result from a call which returns multiple results.
9030 class Call_result_expression : public Expression
9032 public:
9033 Call_result_expression(Call_expression* call, unsigned int index)
9034 : Expression(EXPRESSION_CALL_RESULT, call->location()),
9035 call_(call), index_(index)
9038 protected:
9040 do_traverse(Traverse*);
9042 Type*
9043 do_type();
9045 void
9046 do_determine_type(const Type_context*);
9048 void
9049 do_check_types(Gogo*);
9051 Expression*
9052 do_copy()
9054 return new Call_result_expression(this->call_->call_expression(),
9055 this->index_);
9058 bool
9059 do_must_eval_in_order() const
9060 { return true; }
9062 tree
9063 do_get_tree(Translate_context*);
9065 private:
9066 // The underlying call expression.
9067 Expression* call_;
9068 // Which result we want.
9069 unsigned int index_;
9072 // Traverse a call result.
9075 Call_result_expression::do_traverse(Traverse* traverse)
9077 if (traverse->remember_expression(this->call_))
9079 // We have already traversed the call expression.
9080 return TRAVERSE_CONTINUE;
9082 return Expression::traverse(&this->call_, traverse);
9085 // Get the type.
9087 Type*
9088 Call_result_expression::do_type()
9090 if (this->classification() == EXPRESSION_ERROR)
9091 return Type::make_error_type();
9093 // THIS->CALL_ can be replaced with a temporary reference due to
9094 // Call_expression::do_must_eval_in_order when there is an error.
9095 Call_expression* ce = this->call_->call_expression();
9096 if (ce == NULL)
9098 this->set_is_error();
9099 return Type::make_error_type();
9101 Function_type* fntype = ce->get_function_type();
9102 if (fntype == NULL)
9104 this->set_is_error();
9105 return Type::make_error_type();
9107 const Typed_identifier_list* results = fntype->results();
9108 if (results == NULL)
9110 this->report_error(_("number of results does not match "
9111 "number of values"));
9112 return Type::make_error_type();
9114 Typed_identifier_list::const_iterator pr = results->begin();
9115 for (unsigned int i = 0; i < this->index_; ++i)
9117 if (pr == results->end())
9118 break;
9119 ++pr;
9121 if (pr == results->end())
9123 this->report_error(_("number of results does not match "
9124 "number of values"));
9125 return Type::make_error_type();
9127 return pr->type();
9130 // Check the type. Just make sure that we trigger the warning in
9131 // do_type.
9133 void
9134 Call_result_expression::do_check_types(Gogo*)
9136 this->type();
9139 // Determine the type. We have nothing to do here, but the 0 result
9140 // needs to pass down to the caller.
9142 void
9143 Call_result_expression::do_determine_type(const Type_context*)
9145 this->call_->determine_type_no_context();
9148 // Return the tree.
9150 tree
9151 Call_result_expression::do_get_tree(Translate_context* context)
9153 tree call_tree = this->call_->get_tree(context);
9154 if (call_tree == error_mark_node)
9155 return error_mark_node;
9156 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9158 gcc_assert(saw_errors());
9159 return error_mark_node;
9161 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9162 for (unsigned int i = 0; i < this->index_; ++i)
9164 gcc_assert(field != NULL_TREE);
9165 field = DECL_CHAIN(field);
9167 gcc_assert(field != NULL_TREE);
9168 return build3(COMPONENT_REF, TREE_TYPE(field), call_tree, field, NULL_TREE);
9171 // Make a reference to a single result of a call which returns
9172 // multiple results.
9174 Expression*
9175 Expression::make_call_result(Call_expression* call, unsigned int index)
9177 return new Call_result_expression(call, index);
9180 // Class Index_expression.
9182 // Traversal.
9185 Index_expression::do_traverse(Traverse* traverse)
9187 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9188 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9189 || (this->end_ != NULL
9190 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9191 return TRAVERSE_EXIT;
9192 return TRAVERSE_CONTINUE;
9195 // Lower an index expression. This converts the generic index
9196 // expression into an array index, a string index, or a map index.
9198 Expression*
9199 Index_expression::do_lower(Gogo*, Named_object*, int)
9201 source_location location = this->location();
9202 Expression* left = this->left_;
9203 Expression* start = this->start_;
9204 Expression* end = this->end_;
9206 Type* type = left->type();
9207 if (type->is_error())
9208 return Expression::make_error(location);
9209 else if (left->is_type_expression())
9211 error_at(location, "attempt to index type expression");
9212 return Expression::make_error(location);
9214 else if (type->array_type() != NULL)
9215 return Expression::make_array_index(left, start, end, location);
9216 else if (type->points_to() != NULL
9217 && type->points_to()->array_type() != NULL
9218 && !type->points_to()->is_open_array_type())
9220 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9221 location);
9222 return Expression::make_array_index(deref, start, end, location);
9224 else if (type->is_string_type())
9225 return Expression::make_string_index(left, start, end, location);
9226 else if (type->map_type() != NULL)
9228 if (end != NULL)
9230 error_at(location, "invalid slice of map");
9231 return Expression::make_error(location);
9233 Map_index_expression* ret= Expression::make_map_index(left, start,
9234 location);
9235 if (this->is_lvalue_)
9236 ret->set_is_lvalue();
9237 return ret;
9239 else
9241 error_at(location,
9242 "attempt to index object which is not array, string, or map");
9243 return Expression::make_error(location);
9247 // Make an index expression.
9249 Expression*
9250 Expression::make_index(Expression* left, Expression* start, Expression* end,
9251 source_location location)
9253 return new Index_expression(left, start, end, location);
9256 // An array index. This is used for both indexing and slicing.
9258 class Array_index_expression : public Expression
9260 public:
9261 Array_index_expression(Expression* array, Expression* start,
9262 Expression* end, source_location location)
9263 : Expression(EXPRESSION_ARRAY_INDEX, location),
9264 array_(array), start_(start), end_(end), type_(NULL)
9267 protected:
9269 do_traverse(Traverse*);
9271 Type*
9272 do_type();
9274 void
9275 do_determine_type(const Type_context*);
9277 void
9278 do_check_types(Gogo*);
9280 Expression*
9281 do_copy()
9283 return Expression::make_array_index(this->array_->copy(),
9284 this->start_->copy(),
9285 (this->end_ == NULL
9286 ? NULL
9287 : this->end_->copy()),
9288 this->location());
9291 bool
9292 do_is_addressable() const;
9294 void
9295 do_address_taken(bool escapes)
9296 { this->array_->address_taken(escapes); }
9298 tree
9299 do_get_tree(Translate_context*);
9301 private:
9302 // The array we are getting a value from.
9303 Expression* array_;
9304 // The start or only index.
9305 Expression* start_;
9306 // The end index of a slice. This may be NULL for a simple array
9307 // index, or it may be a nil expression for the length of the array.
9308 Expression* end_;
9309 // The type of the expression.
9310 Type* type_;
9313 // Array index traversal.
9316 Array_index_expression::do_traverse(Traverse* traverse)
9318 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9319 return TRAVERSE_EXIT;
9320 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9321 return TRAVERSE_EXIT;
9322 if (this->end_ != NULL)
9324 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9325 return TRAVERSE_EXIT;
9327 return TRAVERSE_CONTINUE;
9330 // Return the type of an array index.
9332 Type*
9333 Array_index_expression::do_type()
9335 if (this->type_ == NULL)
9337 Array_type* type = this->array_->type()->array_type();
9338 if (type == NULL)
9339 this->type_ = Type::make_error_type();
9340 else if (this->end_ == NULL)
9341 this->type_ = type->element_type();
9342 else if (type->is_open_array_type())
9344 // A slice of a slice has the same type as the original
9345 // slice.
9346 this->type_ = this->array_->type()->deref();
9348 else
9350 // A slice of an array is a slice.
9351 this->type_ = Type::make_array_type(type->element_type(), NULL);
9354 return this->type_;
9357 // Set the type of an array index.
9359 void
9360 Array_index_expression::do_determine_type(const Type_context*)
9362 this->array_->determine_type_no_context();
9363 this->start_->determine_type_no_context();
9364 if (this->end_ != NULL)
9365 this->end_->determine_type_no_context();
9368 // Check types of an array index.
9370 void
9371 Array_index_expression::do_check_types(Gogo*)
9373 if (this->start_->type()->integer_type() == NULL)
9374 this->report_error(_("index must be integer"));
9375 if (this->end_ != NULL
9376 && this->end_->type()->integer_type() == NULL
9377 && !this->end_->is_nil_expression())
9378 this->report_error(_("slice end must be integer"));
9380 Array_type* array_type = this->array_->type()->array_type();
9381 if (array_type == NULL)
9383 gcc_assert(this->array_->type()->is_error());
9384 return;
9387 unsigned int int_bits =
9388 Type::lookup_integer_type("int")->integer_type()->bits();
9390 Type* dummy;
9391 mpz_t lval;
9392 mpz_init(lval);
9393 bool lval_valid = (array_type->length() != NULL
9394 && array_type->length()->integer_constant_value(true,
9395 lval,
9396 &dummy));
9397 mpz_t ival;
9398 mpz_init(ival);
9399 if (this->start_->integer_constant_value(true, ival, &dummy))
9401 if (mpz_sgn(ival) < 0
9402 || mpz_sizeinbase(ival, 2) >= int_bits
9403 || (lval_valid
9404 && (this->end_ == NULL
9405 ? mpz_cmp(ival, lval) >= 0
9406 : mpz_cmp(ival, lval) > 0)))
9408 error_at(this->start_->location(), "array index out of bounds");
9409 this->set_is_error();
9412 if (this->end_ != NULL && !this->end_->is_nil_expression())
9414 if (this->end_->integer_constant_value(true, ival, &dummy))
9416 if (mpz_sgn(ival) < 0
9417 || mpz_sizeinbase(ival, 2) >= int_bits
9418 || (lval_valid && mpz_cmp(ival, lval) > 0))
9420 error_at(this->end_->location(), "array index out of bounds");
9421 this->set_is_error();
9425 mpz_clear(ival);
9426 mpz_clear(lval);
9428 // A slice of an array requires an addressable array. A slice of a
9429 // slice is always possible.
9430 if (this->end_ != NULL && !array_type->is_open_array_type())
9432 if (!this->array_->is_addressable())
9433 this->report_error(_("array is not addressable"));
9434 else
9435 this->array_->address_taken(true);
9439 // Return whether this expression is addressable.
9441 bool
9442 Array_index_expression::do_is_addressable() const
9444 // A slice expression is not addressable.
9445 if (this->end_ != NULL)
9446 return false;
9448 // An index into a slice is addressable.
9449 if (this->array_->type()->is_open_array_type())
9450 return true;
9452 // An index into an array is addressable if the array is
9453 // addressable.
9454 return this->array_->is_addressable();
9457 // Get a tree for an array index.
9459 tree
9460 Array_index_expression::do_get_tree(Translate_context* context)
9462 Gogo* gogo = context->gogo();
9463 source_location loc = this->location();
9465 Array_type* array_type = this->array_->type()->array_type();
9466 if (array_type == NULL)
9468 gcc_assert(this->array_->type()->is_error());
9469 return error_mark_node;
9472 tree type_tree = array_type->get_tree(gogo);
9473 if (type_tree == error_mark_node)
9474 return error_mark_node;
9476 tree array_tree = this->array_->get_tree(context);
9477 if (array_tree == error_mark_node)
9478 return error_mark_node;
9480 if (array_type->length() == NULL && !DECL_P(array_tree))
9481 array_tree = save_expr(array_tree);
9482 tree length_tree = array_type->length_tree(gogo, array_tree);
9483 if (length_tree == error_mark_node)
9484 return error_mark_node;
9485 length_tree = save_expr(length_tree);
9486 tree length_type = TREE_TYPE(length_tree);
9488 tree bad_index = boolean_false_node;
9490 tree start_tree = this->start_->get_tree(context);
9491 if (start_tree == error_mark_node)
9492 return error_mark_node;
9493 if (!DECL_P(start_tree))
9494 start_tree = save_expr(start_tree);
9495 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9496 start_tree = convert_to_integer(length_type, start_tree);
9498 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9499 loc);
9501 start_tree = fold_convert_loc(loc, length_type, start_tree);
9502 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node, bad_index,
9503 fold_build2_loc(loc,
9504 (this->end_ == NULL
9505 ? GE_EXPR
9506 : GT_EXPR),
9507 boolean_type_node, start_tree,
9508 length_tree));
9510 int code = (array_type->length() != NULL
9511 ? (this->end_ == NULL
9512 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9513 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9514 : (this->end_ == NULL
9515 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9516 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9517 tree crash = Gogo::runtime_error(code, loc);
9519 if (this->end_ == NULL)
9521 // Simple array indexing. This has to return an l-value, so
9522 // wrap the index check into START_TREE.
9523 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9524 build3(COND_EXPR, void_type_node,
9525 bad_index, crash, NULL_TREE),
9526 start_tree);
9527 start_tree = fold_convert_loc(loc, sizetype, start_tree);
9529 if (array_type->length() != NULL)
9531 // Fixed array.
9532 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9533 start_tree, NULL_TREE, NULL_TREE);
9535 else
9537 // Open array.
9538 tree values = array_type->value_pointer_tree(gogo, array_tree);
9539 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9540 if (element_type_tree == error_mark_node)
9541 return error_mark_node;
9542 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9543 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9544 start_tree, element_size);
9545 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9546 TREE_TYPE(values), values, offset);
9547 return build_fold_indirect_ref(ptr);
9551 // Array slice.
9553 tree capacity_tree = array_type->capacity_tree(gogo, array_tree);
9554 if (capacity_tree == error_mark_node)
9555 return error_mark_node;
9556 capacity_tree = fold_convert_loc(loc, length_type, capacity_tree);
9558 tree end_tree;
9559 if (this->end_->is_nil_expression())
9560 end_tree = length_tree;
9561 else
9563 end_tree = this->end_->get_tree(context);
9564 if (end_tree == error_mark_node)
9565 return error_mark_node;
9566 if (!DECL_P(end_tree))
9567 end_tree = save_expr(end_tree);
9568 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9569 end_tree = convert_to_integer(length_type, end_tree);
9571 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9572 loc);
9574 end_tree = fold_convert_loc(loc, length_type, end_tree);
9576 capacity_tree = save_expr(capacity_tree);
9577 tree bad_end = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9578 fold_build2_loc(loc, LT_EXPR,
9579 boolean_type_node,
9580 end_tree, start_tree),
9581 fold_build2_loc(loc, GT_EXPR,
9582 boolean_type_node,
9583 end_tree, capacity_tree));
9584 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9585 bad_index, bad_end);
9588 tree element_type_tree = array_type->element_type()->get_tree(gogo);
9589 if (element_type_tree == error_mark_node)
9590 return error_mark_node;
9591 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9593 tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype,
9594 fold_convert_loc(loc, sizetype, start_tree),
9595 element_size);
9597 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9598 if (value_pointer == error_mark_node)
9599 return error_mark_node;
9601 value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR,
9602 TREE_TYPE(value_pointer),
9603 value_pointer, offset);
9605 tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9606 end_tree, start_tree);
9608 tree result_capacity_tree = fold_build2_loc(loc, MINUS_EXPR, length_type,
9609 capacity_tree, start_tree);
9611 tree struct_tree = this->type()->get_tree(gogo);
9612 gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9614 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9616 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9617 tree field = TYPE_FIELDS(struct_tree);
9618 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9619 elt->index = field;
9620 elt->value = value_pointer;
9622 elt = VEC_quick_push(constructor_elt, init, NULL);
9623 field = DECL_CHAIN(field);
9624 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9625 elt->index = field;
9626 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_length_tree);
9628 elt = VEC_quick_push(constructor_elt, init, NULL);
9629 field = DECL_CHAIN(field);
9630 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9631 elt->index = field;
9632 elt->value = fold_convert_loc(loc, TREE_TYPE(field), result_capacity_tree);
9634 tree constructor = build_constructor(struct_tree, init);
9636 if (TREE_CONSTANT(value_pointer)
9637 && TREE_CONSTANT(result_length_tree)
9638 && TREE_CONSTANT(result_capacity_tree))
9639 TREE_CONSTANT(constructor) = 1;
9641 return fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(constructor),
9642 build3(COND_EXPR, void_type_node,
9643 bad_index, crash, NULL_TREE),
9644 constructor);
9647 // Make an array index expression. END may be NULL.
9649 Expression*
9650 Expression::make_array_index(Expression* array, Expression* start,
9651 Expression* end, source_location location)
9653 // Taking a slice of a composite literal requires moving the literal
9654 // onto the heap.
9655 if (end != NULL && array->is_composite_literal())
9657 array = Expression::make_heap_composite(array, location);
9658 array = Expression::make_unary(OPERATOR_MULT, array, location);
9660 return new Array_index_expression(array, start, end, location);
9663 // A string index. This is used for both indexing and slicing.
9665 class String_index_expression : public Expression
9667 public:
9668 String_index_expression(Expression* string, Expression* start,
9669 Expression* end, source_location location)
9670 : Expression(EXPRESSION_STRING_INDEX, location),
9671 string_(string), start_(start), end_(end)
9674 protected:
9676 do_traverse(Traverse*);
9678 Type*
9679 do_type();
9681 void
9682 do_determine_type(const Type_context*);
9684 void
9685 do_check_types(Gogo*);
9687 Expression*
9688 do_copy()
9690 return Expression::make_string_index(this->string_->copy(),
9691 this->start_->copy(),
9692 (this->end_ == NULL
9693 ? NULL
9694 : this->end_->copy()),
9695 this->location());
9698 tree
9699 do_get_tree(Translate_context*);
9701 private:
9702 // The string we are getting a value from.
9703 Expression* string_;
9704 // The start or only index.
9705 Expression* start_;
9706 // The end index of a slice. This may be NULL for a single index,
9707 // or it may be a nil expression for the length of the string.
9708 Expression* end_;
9711 // String index traversal.
9714 String_index_expression::do_traverse(Traverse* traverse)
9716 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
9717 return TRAVERSE_EXIT;
9718 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9719 return TRAVERSE_EXIT;
9720 if (this->end_ != NULL)
9722 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9723 return TRAVERSE_EXIT;
9725 return TRAVERSE_CONTINUE;
9728 // Return the type of a string index.
9730 Type*
9731 String_index_expression::do_type()
9733 if (this->end_ == NULL)
9734 return Type::lookup_integer_type("uint8");
9735 else
9736 return this->string_->type();
9739 // Determine the type of a string index.
9741 void
9742 String_index_expression::do_determine_type(const Type_context*)
9744 this->string_->determine_type_no_context();
9745 this->start_->determine_type_no_context();
9746 if (this->end_ != NULL)
9747 this->end_->determine_type_no_context();
9750 // Check types of a string index.
9752 void
9753 String_index_expression::do_check_types(Gogo*)
9755 if (this->start_->type()->integer_type() == NULL)
9756 this->report_error(_("index must be integer"));
9757 if (this->end_ != NULL
9758 && this->end_->type()->integer_type() == NULL
9759 && !this->end_->is_nil_expression())
9760 this->report_error(_("slice end must be integer"));
9762 std::string sval;
9763 bool sval_valid = this->string_->string_constant_value(&sval);
9765 mpz_t ival;
9766 mpz_init(ival);
9767 Type* dummy;
9768 if (this->start_->integer_constant_value(true, ival, &dummy))
9770 if (mpz_sgn(ival) < 0
9771 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
9773 error_at(this->start_->location(), "string index out of bounds");
9774 this->set_is_error();
9777 if (this->end_ != NULL && !this->end_->is_nil_expression())
9779 if (this->end_->integer_constant_value(true, ival, &dummy))
9781 if (mpz_sgn(ival) < 0
9782 || (sval_valid && mpz_cmp_ui(ival, sval.length()) > 0))
9784 error_at(this->end_->location(), "string index out of bounds");
9785 this->set_is_error();
9789 mpz_clear(ival);
9792 // Get a tree for a string index.
9794 tree
9795 String_index_expression::do_get_tree(Translate_context* context)
9797 source_location loc = this->location();
9799 tree string_tree = this->string_->get_tree(context);
9800 if (string_tree == error_mark_node)
9801 return error_mark_node;
9803 if (this->string_->type()->points_to() != NULL)
9804 string_tree = build_fold_indirect_ref(string_tree);
9805 if (!DECL_P(string_tree))
9806 string_tree = save_expr(string_tree);
9807 tree string_type = TREE_TYPE(string_tree);
9809 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
9810 length_tree = save_expr(length_tree);
9811 tree length_type = TREE_TYPE(length_tree);
9813 tree bad_index = boolean_false_node;
9815 tree start_tree = this->start_->get_tree(context);
9816 if (start_tree == error_mark_node)
9817 return error_mark_node;
9818 if (!DECL_P(start_tree))
9819 start_tree = save_expr(start_tree);
9820 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9821 start_tree = convert_to_integer(length_type, start_tree);
9823 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9824 loc);
9826 start_tree = fold_convert_loc(loc, length_type, start_tree);
9828 int code = (this->end_ == NULL
9829 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
9830 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
9831 tree crash = Gogo::runtime_error(code, loc);
9833 if (this->end_ == NULL)
9835 bad_index = fold_build2_loc(loc, TRUTH_OR_EXPR, boolean_type_node,
9836 bad_index,
9837 fold_build2_loc(loc, GE_EXPR,
9838 boolean_type_node,
9839 start_tree, length_tree));
9841 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
9842 tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(bytes_tree),
9843 bytes_tree,
9844 fold_convert_loc(loc, sizetype, start_tree));
9845 tree index = build_fold_indirect_ref_loc(loc, ptr);
9847 return build2(COMPOUND_EXPR, TREE_TYPE(index),
9848 build3(COND_EXPR, void_type_node,
9849 bad_index, crash, NULL_TREE),
9850 index);
9852 else
9854 tree end_tree;
9855 if (this->end_->is_nil_expression())
9856 end_tree = build_int_cst(length_type, -1);
9857 else
9859 end_tree = this->end_->get_tree(context);
9860 if (end_tree == error_mark_node)
9861 return error_mark_node;
9862 if (!DECL_P(end_tree))
9863 end_tree = save_expr(end_tree);
9864 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9865 end_tree = convert_to_integer(length_type, end_tree);
9867 bad_index = Expression::check_bounds(end_tree, length_type,
9868 bad_index, loc);
9870 end_tree = fold_convert_loc(loc, length_type, end_tree);
9873 static tree strslice_fndecl;
9874 tree ret = Gogo::call_builtin(&strslice_fndecl,
9875 loc,
9876 "__go_string_slice",
9878 string_type,
9879 string_type,
9880 string_tree,
9881 length_type,
9882 start_tree,
9883 length_type,
9884 end_tree);
9885 if (ret == error_mark_node)
9886 return error_mark_node;
9887 // This will panic if the bounds are out of range for the
9888 // string.
9889 TREE_NOTHROW(strslice_fndecl) = 0;
9891 if (bad_index == boolean_false_node)
9892 return ret;
9893 else
9894 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
9895 build3(COND_EXPR, void_type_node,
9896 bad_index, crash, NULL_TREE),
9897 ret);
9901 // Make a string index expression. END may be NULL.
9903 Expression*
9904 Expression::make_string_index(Expression* string, Expression* start,
9905 Expression* end, source_location location)
9907 return new String_index_expression(string, start, end, location);
9910 // Class Map_index.
9912 // Get the type of the map.
9914 Map_type*
9915 Map_index_expression::get_map_type() const
9917 Map_type* mt = this->map_->type()->deref()->map_type();
9918 if (mt == NULL)
9919 gcc_assert(saw_errors());
9920 return mt;
9923 // Map index traversal.
9926 Map_index_expression::do_traverse(Traverse* traverse)
9928 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
9929 return TRAVERSE_EXIT;
9930 return Expression::traverse(&this->index_, traverse);
9933 // Return the type of a map index.
9935 Type*
9936 Map_index_expression::do_type()
9938 Map_type* mt = this->get_map_type();
9939 if (mt == NULL)
9940 return Type::make_error_type();
9941 Type* type = mt->val_type();
9942 // If this map index is in a tuple assignment, we actually return a
9943 // pointer to the value type. Tuple_map_assignment_statement is
9944 // responsible for handling this correctly. We need to get the type
9945 // right in case this gets assigned to a temporary variable.
9946 if (this->is_in_tuple_assignment_)
9947 type = Type::make_pointer_type(type);
9948 return type;
9951 // Fix the type of a map index.
9953 void
9954 Map_index_expression::do_determine_type(const Type_context*)
9956 this->map_->determine_type_no_context();
9957 Map_type* mt = this->get_map_type();
9958 Type* key_type = mt == NULL ? NULL : mt->key_type();
9959 Type_context subcontext(key_type, false);
9960 this->index_->determine_type(&subcontext);
9963 // Check types of a map index.
9965 void
9966 Map_index_expression::do_check_types(Gogo*)
9968 std::string reason;
9969 Map_type* mt = this->get_map_type();
9970 if (mt == NULL)
9971 return;
9972 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
9974 if (reason.empty())
9975 this->report_error(_("incompatible type for map index"));
9976 else
9978 error_at(this->location(), "incompatible type for map index (%s)",
9979 reason.c_str());
9980 this->set_is_error();
9985 // Get a tree for a map index.
9987 tree
9988 Map_index_expression::do_get_tree(Translate_context* context)
9990 Map_type* type = this->get_map_type();
9991 if (type == NULL)
9992 return error_mark_node;
9994 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
9995 if (valptr == error_mark_node)
9996 return error_mark_node;
9997 valptr = save_expr(valptr);
9999 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
10001 if (this->is_lvalue_)
10002 return build_fold_indirect_ref(valptr);
10003 else if (this->is_in_tuple_assignment_)
10005 // Tuple_map_assignment_statement is responsible for using this
10006 // appropriately.
10007 return valptr;
10009 else
10011 return fold_build3(COND_EXPR, val_type_tree,
10012 fold_build2(EQ_EXPR, boolean_type_node, valptr,
10013 fold_convert(TREE_TYPE(valptr),
10014 null_pointer_node)),
10015 type->val_type()->get_init_tree(context->gogo(),
10016 false),
10017 build_fold_indirect_ref(valptr));
10021 // Get a tree for the map index. This returns a tree which evaluates
10022 // to a pointer to a value. The pointer will be NULL if the key is
10023 // not in the map.
10025 tree
10026 Map_index_expression::get_value_pointer(Translate_context* context,
10027 bool insert)
10029 Map_type* type = this->get_map_type();
10030 if (type == NULL)
10031 return error_mark_node;
10033 tree map_tree = this->map_->get_tree(context);
10034 tree index_tree = this->index_->get_tree(context);
10035 index_tree = Expression::convert_for_assignment(context, type->key_type(),
10036 this->index_->type(),
10037 index_tree,
10038 this->location());
10039 if (map_tree == error_mark_node || index_tree == error_mark_node)
10040 return error_mark_node;
10042 if (this->map_->type()->points_to() != NULL)
10043 map_tree = build_fold_indirect_ref(map_tree);
10045 // We need to pass in a pointer to the key, so stuff it into a
10046 // variable.
10047 tree tmp;
10048 tree make_tmp;
10049 if (current_function_decl != NULL)
10051 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
10052 DECL_IGNORED_P(tmp) = 0;
10053 DECL_INITIAL(tmp) = index_tree;
10054 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
10055 TREE_ADDRESSABLE(tmp) = 1;
10057 else
10059 tmp = build_decl(this->location(), VAR_DECL, create_tmp_var_name("M"),
10060 TREE_TYPE(index_tree));
10061 DECL_EXTERNAL(tmp) = 0;
10062 TREE_PUBLIC(tmp) = 0;
10063 TREE_STATIC(tmp) = 1;
10064 DECL_ARTIFICIAL(tmp) = 1;
10065 if (!TREE_CONSTANT(index_tree))
10066 make_tmp = fold_build2_loc(this->location(), INIT_EXPR, void_type_node,
10067 tmp, index_tree);
10068 else
10070 TREE_READONLY(tmp) = 1;
10071 TREE_CONSTANT(tmp) = 1;
10072 DECL_INITIAL(tmp) = index_tree;
10073 make_tmp = NULL_TREE;
10075 rest_of_decl_compilation(tmp, 1, 0);
10077 tree tmpref = fold_convert_loc(this->location(), const_ptr_type_node,
10078 build_fold_addr_expr_loc(this->location(),
10079 tmp));
10081 static tree map_index_fndecl;
10082 tree call = Gogo::call_builtin(&map_index_fndecl,
10083 this->location(),
10084 "__go_map_index",
10086 const_ptr_type_node,
10087 TREE_TYPE(map_tree),
10088 map_tree,
10089 const_ptr_type_node,
10090 tmpref,
10091 boolean_type_node,
10092 (insert
10093 ? boolean_true_node
10094 : boolean_false_node));
10095 if (call == error_mark_node)
10096 return error_mark_node;
10097 // This can panic on a map of interface type if the interface holds
10098 // an uncomparable or unhashable type.
10099 TREE_NOTHROW(map_index_fndecl) = 0;
10101 tree val_type_tree = type->val_type()->get_tree(context->gogo());
10102 if (val_type_tree == error_mark_node)
10103 return error_mark_node;
10104 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
10106 tree ret = fold_convert_loc(this->location(), ptr_val_type_tree, call);
10107 if (make_tmp != NULL_TREE)
10108 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
10109 return ret;
10112 // Make a map index expression.
10114 Map_index_expression*
10115 Expression::make_map_index(Expression* map, Expression* index,
10116 source_location location)
10118 return new Map_index_expression(map, index, location);
10121 // Class Field_reference_expression.
10123 // Return the type of a field reference.
10125 Type*
10126 Field_reference_expression::do_type()
10128 Type* type = this->expr_->type();
10129 if (type->is_error())
10130 return type;
10131 Struct_type* struct_type = type->struct_type();
10132 gcc_assert(struct_type != NULL);
10133 return struct_type->field(this->field_index_)->type();
10136 // Check the types for a field reference.
10138 void
10139 Field_reference_expression::do_check_types(Gogo*)
10141 Type* type = this->expr_->type();
10142 if (type->is_error())
10143 return;
10144 Struct_type* struct_type = type->struct_type();
10145 gcc_assert(struct_type != NULL);
10146 gcc_assert(struct_type->field(this->field_index_) != NULL);
10149 // Get a tree for a field reference.
10151 tree
10152 Field_reference_expression::do_get_tree(Translate_context* context)
10154 tree struct_tree = this->expr_->get_tree(context);
10155 if (struct_tree == error_mark_node
10156 || TREE_TYPE(struct_tree) == error_mark_node)
10157 return error_mark_node;
10158 gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
10159 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
10160 if (field == NULL_TREE)
10162 // This can happen for a type which refers to itself indirectly
10163 // and then turns out to be erroneous.
10164 gcc_assert(saw_errors());
10165 return error_mark_node;
10167 for (unsigned int i = this->field_index_; i > 0; --i)
10169 field = DECL_CHAIN(field);
10170 gcc_assert(field != NULL_TREE);
10172 if (TREE_TYPE(field) == error_mark_node)
10173 return error_mark_node;
10174 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
10175 NULL_TREE);
10178 // Make a reference to a qualified identifier in an expression.
10180 Field_reference_expression*
10181 Expression::make_field_reference(Expression* expr, unsigned int field_index,
10182 source_location location)
10184 return new Field_reference_expression(expr, field_index, location);
10187 // Class Interface_field_reference_expression.
10189 // Return a tree for the pointer to the function to call.
10191 tree
10192 Interface_field_reference_expression::get_function_tree(Translate_context*,
10193 tree expr)
10195 if (this->expr_->type()->points_to() != NULL)
10196 expr = build_fold_indirect_ref(expr);
10198 tree expr_type = TREE_TYPE(expr);
10199 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10201 tree field = TYPE_FIELDS(expr_type);
10202 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
10204 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10205 gcc_assert(POINTER_TYPE_P(TREE_TYPE(table)));
10207 table = build_fold_indirect_ref(table);
10208 gcc_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
10210 std::string name = Gogo::unpack_hidden_name(this->name_);
10211 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
10212 field != NULL_TREE;
10213 field = DECL_CHAIN(field))
10215 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
10216 break;
10218 gcc_assert(field != NULL_TREE);
10220 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
10223 // Return a tree for the first argument to pass to the interface
10224 // function.
10226 tree
10227 Interface_field_reference_expression::get_underlying_object_tree(
10228 Translate_context*,
10229 tree expr)
10231 if (this->expr_->type()->points_to() != NULL)
10232 expr = build_fold_indirect_ref(expr);
10234 tree expr_type = TREE_TYPE(expr);
10235 gcc_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10237 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
10238 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
10240 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10243 // Traversal.
10246 Interface_field_reference_expression::do_traverse(Traverse* traverse)
10248 return Expression::traverse(&this->expr_, traverse);
10251 // Return the type of an interface field reference.
10253 Type*
10254 Interface_field_reference_expression::do_type()
10256 Type* expr_type = this->expr_->type();
10258 Type* points_to = expr_type->points_to();
10259 if (points_to != NULL)
10260 expr_type = points_to;
10262 Interface_type* interface_type = expr_type->interface_type();
10263 if (interface_type == NULL)
10264 return Type::make_error_type();
10266 const Typed_identifier* method = interface_type->find_method(this->name_);
10267 if (method == NULL)
10268 return Type::make_error_type();
10270 return method->type();
10273 // Determine types.
10275 void
10276 Interface_field_reference_expression::do_determine_type(const Type_context*)
10278 this->expr_->determine_type_no_context();
10281 // Check the types for an interface field reference.
10283 void
10284 Interface_field_reference_expression::do_check_types(Gogo*)
10286 Type* type = this->expr_->type();
10288 Type* points_to = type->points_to();
10289 if (points_to != NULL)
10290 type = points_to;
10292 Interface_type* interface_type = type->interface_type();
10293 if (interface_type == NULL)
10295 if (!type->is_error_type())
10296 this->report_error(_("expected interface or pointer to interface"));
10298 else
10300 const Typed_identifier* method =
10301 interface_type->find_method(this->name_);
10302 if (method == NULL)
10304 error_at(this->location(), "method %qs not in interface",
10305 Gogo::message_name(this->name_).c_str());
10306 this->set_is_error();
10311 // Get a tree for a reference to a field in an interface. There is no
10312 // standard tree type representation for this: it's a function
10313 // attached to its first argument, like a Bound_method_expression.
10314 // The only places it may currently be used are in a Call_expression
10315 // or a Go_statement, which will take it apart directly. So this has
10316 // nothing to do at present.
10318 tree
10319 Interface_field_reference_expression::do_get_tree(Translate_context*)
10321 gcc_unreachable();
10324 // Make a reference to a field in an interface.
10326 Expression*
10327 Expression::make_interface_field_reference(Expression* expr,
10328 const std::string& field,
10329 source_location location)
10331 return new Interface_field_reference_expression(expr, field, location);
10334 // A general selector. This is a Parser_expression for LEFT.NAME. It
10335 // is lowered after we know the type of the left hand side.
10337 class Selector_expression : public Parser_expression
10339 public:
10340 Selector_expression(Expression* left, const std::string& name,
10341 source_location location)
10342 : Parser_expression(EXPRESSION_SELECTOR, location),
10343 left_(left), name_(name)
10346 protected:
10348 do_traverse(Traverse* traverse)
10349 { return Expression::traverse(&this->left_, traverse); }
10351 Expression*
10352 do_lower(Gogo*, Named_object*, int);
10354 Expression*
10355 do_copy()
10357 return new Selector_expression(this->left_->copy(), this->name_,
10358 this->location());
10361 private:
10362 Expression*
10363 lower_method_expression(Gogo*);
10365 // The expression on the left hand side.
10366 Expression* left_;
10367 // The name on the right hand side.
10368 std::string name_;
10371 // Lower a selector expression once we know the real type of the left
10372 // hand side.
10374 Expression*
10375 Selector_expression::do_lower(Gogo* gogo, Named_object*, int)
10377 Expression* left = this->left_;
10378 if (left->is_type_expression())
10379 return this->lower_method_expression(gogo);
10380 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
10381 this->location());
10384 // Lower a method expression T.M or (*T).M. We turn this into a
10385 // function literal.
10387 Expression*
10388 Selector_expression::lower_method_expression(Gogo* gogo)
10390 source_location location = this->location();
10391 Type* type = this->left_->type();
10392 const std::string& name(this->name_);
10394 bool is_pointer;
10395 if (type->points_to() == NULL)
10396 is_pointer = false;
10397 else
10399 is_pointer = true;
10400 type = type->points_to();
10402 Named_type* nt = type->named_type();
10403 if (nt == NULL)
10405 error_at(location,
10406 ("method expression requires named type or "
10407 "pointer to named type"));
10408 return Expression::make_error(location);
10411 bool is_ambiguous;
10412 Method* method = nt->method_function(name, &is_ambiguous);
10413 const Typed_identifier* imethod = NULL;
10414 if (method == NULL && !is_pointer)
10416 Interface_type* it = nt->interface_type();
10417 if (it != NULL)
10418 imethod = it->find_method(name);
10421 if (method == NULL && imethod == NULL)
10423 if (!is_ambiguous)
10424 error_at(location, "type %<%s%s%> has no method %<%s%>",
10425 is_pointer ? "*" : "",
10426 nt->message_name().c_str(),
10427 Gogo::message_name(name).c_str());
10428 else
10429 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
10430 Gogo::message_name(name).c_str(),
10431 is_pointer ? "*" : "",
10432 nt->message_name().c_str());
10433 return Expression::make_error(location);
10436 if (method != NULL && !is_pointer && !method->is_value_method())
10438 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
10439 nt->message_name().c_str(),
10440 Gogo::message_name(name).c_str());
10441 return Expression::make_error(location);
10444 // Build a new function type in which the receiver becomes the first
10445 // argument.
10446 Function_type* method_type;
10447 if (method != NULL)
10449 method_type = method->type();
10450 gcc_assert(method_type->is_method());
10452 else
10454 method_type = imethod->type()->function_type();
10455 gcc_assert(method_type != NULL && !method_type->is_method());
10458 const char* const receiver_name = "$this";
10459 Typed_identifier_list* parameters = new Typed_identifier_list();
10460 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
10461 location));
10463 const Typed_identifier_list* method_parameters = method_type->parameters();
10464 if (method_parameters != NULL)
10466 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10467 p != method_parameters->end();
10468 ++p)
10469 parameters->push_back(*p);
10472 const Typed_identifier_list* method_results = method_type->results();
10473 Typed_identifier_list* results;
10474 if (method_results == NULL)
10475 results = NULL;
10476 else
10478 results = new Typed_identifier_list();
10479 for (Typed_identifier_list::const_iterator p = method_results->begin();
10480 p != method_results->end();
10481 ++p)
10482 results->push_back(*p);
10485 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
10486 location);
10487 if (method_type->is_varargs())
10488 fntype->set_is_varargs();
10490 // We generate methods which always takes a pointer to the receiver
10491 // as their first argument. If this is for a pointer type, we can
10492 // simply reuse the existing function. We use an internal hack to
10493 // get the right type.
10495 if (method != NULL && is_pointer)
10497 Named_object* mno = (method->needs_stub_method()
10498 ? method->stub_object()
10499 : method->named_object());
10500 Expression* f = Expression::make_func_reference(mno, NULL, location);
10501 f = Expression::make_cast(fntype, f, location);
10502 Type_conversion_expression* tce =
10503 static_cast<Type_conversion_expression*>(f);
10504 tce->set_may_convert_function_types();
10505 return f;
10508 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
10509 location);
10511 Named_object* vno = gogo->lookup(receiver_name, NULL);
10512 gcc_assert(vno != NULL);
10513 Expression* ve = Expression::make_var_reference(vno, location);
10514 Expression* bm;
10515 if (method != NULL)
10516 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
10517 else
10518 bm = Expression::make_interface_field_reference(ve, name, location);
10520 // Even though we found the method above, if it has an error type we
10521 // may see an error here.
10522 if (bm->is_error_expression())
10524 gogo->finish_function(location);
10525 return bm;
10528 Expression_list* args;
10529 if (method_parameters == NULL)
10530 args = NULL;
10531 else
10533 args = new Expression_list();
10534 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10535 p != method_parameters->end();
10536 ++p)
10538 vno = gogo->lookup(p->name(), NULL);
10539 gcc_assert(vno != NULL);
10540 args->push_back(Expression::make_var_reference(vno, location));
10544 Call_expression* call = Expression::make_call(bm, args,
10545 method_type->is_varargs(),
10546 location);
10548 size_t count = call->result_count();
10549 Statement* s;
10550 if (count == 0)
10551 s = Statement::make_statement(call);
10552 else
10554 Expression_list* retvals = new Expression_list();
10555 if (count <= 1)
10556 retvals->push_back(call);
10557 else
10559 for (size_t i = 0; i < count; ++i)
10560 retvals->push_back(Expression::make_call_result(call, i));
10562 s = Statement::make_return_statement(retvals, location);
10564 gogo->add_statement(s);
10566 gogo->finish_function(location);
10568 return Expression::make_func_reference(no, NULL, location);
10571 // Make a selector expression.
10573 Expression*
10574 Expression::make_selector(Expression* left, const std::string& name,
10575 source_location location)
10577 return new Selector_expression(left, name, location);
10580 // Implement the builtin function new.
10582 class Allocation_expression : public Expression
10584 public:
10585 Allocation_expression(Type* type, source_location location)
10586 : Expression(EXPRESSION_ALLOCATION, location),
10587 type_(type)
10590 protected:
10592 do_traverse(Traverse* traverse)
10593 { return Type::traverse(this->type_, traverse); }
10595 Type*
10596 do_type()
10597 { return Type::make_pointer_type(this->type_); }
10599 void
10600 do_determine_type(const Type_context*)
10603 void
10604 do_check_types(Gogo*);
10606 Expression*
10607 do_copy()
10608 { return new Allocation_expression(this->type_, this->location()); }
10610 tree
10611 do_get_tree(Translate_context*);
10613 private:
10614 // The type we are allocating.
10615 Type* type_;
10618 // Check the type of an allocation expression.
10620 void
10621 Allocation_expression::do_check_types(Gogo*)
10623 if (this->type_->function_type() != NULL)
10624 this->report_error(_("invalid new of function type"));
10627 // Return a tree for an allocation expression.
10629 tree
10630 Allocation_expression::do_get_tree(Translate_context* context)
10632 tree type_tree = this->type_->get_tree(context->gogo());
10633 if (type_tree == error_mark_node)
10634 return error_mark_node;
10635 tree size_tree = TYPE_SIZE_UNIT(type_tree);
10636 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
10637 this->location());
10638 if (space == error_mark_node)
10639 return error_mark_node;
10640 return fold_convert(build_pointer_type(type_tree), space);
10643 // Make an allocation expression.
10645 Expression*
10646 Expression::make_allocation(Type* type, source_location location)
10648 return new Allocation_expression(type, location);
10651 // Implement the builtin function make.
10653 class Make_expression : public Expression
10655 public:
10656 Make_expression(Type* type, Expression_list* args, source_location location)
10657 : Expression(EXPRESSION_MAKE, location),
10658 type_(type), args_(args)
10661 protected:
10663 do_traverse(Traverse* traverse);
10665 Type*
10666 do_type()
10667 { return this->type_; }
10669 void
10670 do_determine_type(const Type_context*);
10672 void
10673 do_check_types(Gogo*);
10675 Expression*
10676 do_copy()
10678 return new Make_expression(this->type_, this->args_->copy(),
10679 this->location());
10682 tree
10683 do_get_tree(Translate_context*);
10685 private:
10686 // The type we are making.
10687 Type* type_;
10688 // The arguments to pass to the make routine.
10689 Expression_list* args_;
10692 // Traversal.
10695 Make_expression::do_traverse(Traverse* traverse)
10697 if (this->args_ != NULL
10698 && this->args_->traverse(traverse) == TRAVERSE_EXIT)
10699 return TRAVERSE_EXIT;
10700 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10701 return TRAVERSE_EXIT;
10702 return TRAVERSE_CONTINUE;
10705 // Set types of arguments.
10707 void
10708 Make_expression::do_determine_type(const Type_context*)
10710 if (this->args_ != NULL)
10712 Type_context context(Type::lookup_integer_type("int"), false);
10713 for (Expression_list::const_iterator pe = this->args_->begin();
10714 pe != this->args_->end();
10715 ++pe)
10716 (*pe)->determine_type(&context);
10720 // Check types for a make expression.
10722 void
10723 Make_expression::do_check_types(Gogo*)
10725 if (this->type_->channel_type() == NULL
10726 && this->type_->map_type() == NULL
10727 && (this->type_->array_type() == NULL
10728 || this->type_->array_type()->length() != NULL))
10729 this->report_error(_("invalid type for make function"));
10730 else if (!this->type_->check_make_expression(this->args_, this->location()))
10731 this->set_is_error();
10734 // Return a tree for a make expression.
10736 tree
10737 Make_expression::do_get_tree(Translate_context* context)
10739 return this->type_->make_expression_tree(context, this->args_,
10740 this->location());
10743 // Make a make expression.
10745 Expression*
10746 Expression::make_make(Type* type, Expression_list* args,
10747 source_location location)
10749 return new Make_expression(type, args, location);
10752 // Construct a struct.
10754 class Struct_construction_expression : public Expression
10756 public:
10757 Struct_construction_expression(Type* type, Expression_list* vals,
10758 source_location location)
10759 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
10760 type_(type), vals_(vals)
10763 // Return whether this is a constant initializer.
10764 bool
10765 is_constant_struct() const;
10767 protected:
10769 do_traverse(Traverse* traverse);
10771 Type*
10772 do_type()
10773 { return this->type_; }
10775 void
10776 do_determine_type(const Type_context*);
10778 void
10779 do_check_types(Gogo*);
10781 Expression*
10782 do_copy()
10784 return new Struct_construction_expression(this->type_, this->vals_->copy(),
10785 this->location());
10788 bool
10789 do_is_addressable() const
10790 { return true; }
10792 tree
10793 do_get_tree(Translate_context*);
10795 void
10796 do_export(Export*) const;
10798 private:
10799 // The type of the struct to construct.
10800 Type* type_;
10801 // The list of values, in order of the fields in the struct. A NULL
10802 // entry means that the field should be zero-initialized.
10803 Expression_list* vals_;
10806 // Traversal.
10809 Struct_construction_expression::do_traverse(Traverse* traverse)
10811 if (this->vals_ != NULL
10812 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
10813 return TRAVERSE_EXIT;
10814 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
10815 return TRAVERSE_EXIT;
10816 return TRAVERSE_CONTINUE;
10819 // Return whether this is a constant initializer.
10821 bool
10822 Struct_construction_expression::is_constant_struct() const
10824 if (this->vals_ == NULL)
10825 return true;
10826 for (Expression_list::const_iterator pv = this->vals_->begin();
10827 pv != this->vals_->end();
10828 ++pv)
10830 if (*pv != NULL
10831 && !(*pv)->is_constant()
10832 && (!(*pv)->is_composite_literal()
10833 || (*pv)->is_nonconstant_composite_literal()))
10834 return false;
10837 const Struct_field_list* fields = this->type_->struct_type()->fields();
10838 for (Struct_field_list::const_iterator pf = fields->begin();
10839 pf != fields->end();
10840 ++pf)
10842 // There are no constant constructors for interfaces.
10843 if (pf->type()->interface_type() != NULL)
10844 return false;
10847 return true;
10850 // Final type determination.
10852 void
10853 Struct_construction_expression::do_determine_type(const Type_context*)
10855 if (this->vals_ == NULL)
10856 return;
10857 const Struct_field_list* fields = this->type_->struct_type()->fields();
10858 Expression_list::const_iterator pv = this->vals_->begin();
10859 for (Struct_field_list::const_iterator pf = fields->begin();
10860 pf != fields->end();
10861 ++pf, ++pv)
10863 if (pv == this->vals_->end())
10864 return;
10865 if (*pv != NULL)
10867 Type_context subcontext(pf->type(), false);
10868 (*pv)->determine_type(&subcontext);
10871 // Extra values are an error we will report elsewhere; we still want
10872 // to determine the type to avoid knockon errors.
10873 for (; pv != this->vals_->end(); ++pv)
10874 (*pv)->determine_type_no_context();
10877 // Check types.
10879 void
10880 Struct_construction_expression::do_check_types(Gogo*)
10882 if (this->vals_ == NULL)
10883 return;
10885 Struct_type* st = this->type_->struct_type();
10886 if (this->vals_->size() > st->field_count())
10888 this->report_error(_("too many expressions for struct"));
10889 return;
10892 const Struct_field_list* fields = st->fields();
10893 Expression_list::const_iterator pv = this->vals_->begin();
10894 int i = 0;
10895 for (Struct_field_list::const_iterator pf = fields->begin();
10896 pf != fields->end();
10897 ++pf, ++pv, ++i)
10899 if (pv == this->vals_->end())
10901 this->report_error(_("too few expressions for struct"));
10902 break;
10905 if (*pv == NULL)
10906 continue;
10908 std::string reason;
10909 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
10911 if (reason.empty())
10912 error_at((*pv)->location(),
10913 "incompatible type for field %d in struct construction",
10914 i + 1);
10915 else
10916 error_at((*pv)->location(),
10917 ("incompatible type for field %d in "
10918 "struct construction (%s)"),
10919 i + 1, reason.c_str());
10920 this->set_is_error();
10923 gcc_assert(pv == this->vals_->end());
10926 // Return a tree for constructing a struct.
10928 tree
10929 Struct_construction_expression::do_get_tree(Translate_context* context)
10931 Gogo* gogo = context->gogo();
10933 if (this->vals_ == NULL)
10934 return this->type_->get_init_tree(gogo, false);
10936 tree type_tree = this->type_->get_tree(gogo);
10937 if (type_tree == error_mark_node)
10938 return error_mark_node;
10939 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
10941 bool is_constant = true;
10942 const Struct_field_list* fields = this->type_->struct_type()->fields();
10943 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
10944 fields->size());
10945 Struct_field_list::const_iterator pf = fields->begin();
10946 Expression_list::const_iterator pv = this->vals_->begin();
10947 for (tree field = TYPE_FIELDS(type_tree);
10948 field != NULL_TREE;
10949 field = DECL_CHAIN(field), ++pf)
10951 gcc_assert(pf != fields->end());
10953 tree val;
10954 if (pv == this->vals_->end())
10955 val = pf->type()->get_init_tree(gogo, false);
10956 else if (*pv == NULL)
10958 val = pf->type()->get_init_tree(gogo, false);
10959 ++pv;
10961 else
10963 val = Expression::convert_for_assignment(context, pf->type(),
10964 (*pv)->type(),
10965 (*pv)->get_tree(context),
10966 this->location());
10967 ++pv;
10970 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
10971 return error_mark_node;
10973 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
10974 elt->index = field;
10975 elt->value = val;
10976 if (!TREE_CONSTANT(val))
10977 is_constant = false;
10979 gcc_assert(pf == fields->end());
10981 tree ret = build_constructor(type_tree, elts);
10982 if (is_constant)
10983 TREE_CONSTANT(ret) = 1;
10984 return ret;
10987 // Export a struct construction.
10989 void
10990 Struct_construction_expression::do_export(Export* exp) const
10992 exp->write_c_string("convert(");
10993 exp->write_type(this->type_);
10994 for (Expression_list::const_iterator pv = this->vals_->begin();
10995 pv != this->vals_->end();
10996 ++pv)
10998 exp->write_c_string(", ");
10999 if (*pv != NULL)
11000 (*pv)->export_expression(exp);
11002 exp->write_c_string(")");
11005 // Make a struct composite literal. This used by the thunk code.
11007 Expression*
11008 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
11009 source_location location)
11011 gcc_assert(type->struct_type() != NULL);
11012 return new Struct_construction_expression(type, vals, location);
11015 // Construct an array. This class is not used directly; instead we
11016 // use the child classes, Fixed_array_construction_expression and
11017 // Open_array_construction_expression.
11019 class Array_construction_expression : public Expression
11021 protected:
11022 Array_construction_expression(Expression_classification classification,
11023 Type* type, Expression_list* vals,
11024 source_location location)
11025 : Expression(classification, location),
11026 type_(type), vals_(vals)
11029 public:
11030 // Return whether this is a constant initializer.
11031 bool
11032 is_constant_array() const;
11034 // Return the number of elements.
11035 size_t
11036 element_count() const
11037 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
11039 protected:
11041 do_traverse(Traverse* traverse);
11043 Type*
11044 do_type()
11045 { return this->type_; }
11047 void
11048 do_determine_type(const Type_context*);
11050 void
11051 do_check_types(Gogo*);
11053 bool
11054 do_is_addressable() const
11055 { return true; }
11057 void
11058 do_export(Export*) const;
11060 // The list of values.
11061 Expression_list*
11062 vals()
11063 { return this->vals_; }
11065 // Get a constructor tree for the array values.
11066 tree
11067 get_constructor_tree(Translate_context* context, tree type_tree);
11069 private:
11070 // The type of the array to construct.
11071 Type* type_;
11072 // The list of values.
11073 Expression_list* vals_;
11076 // Traversal.
11079 Array_construction_expression::do_traverse(Traverse* traverse)
11081 if (this->vals_ != NULL
11082 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11083 return TRAVERSE_EXIT;
11084 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11085 return TRAVERSE_EXIT;
11086 return TRAVERSE_CONTINUE;
11089 // Return whether this is a constant initializer.
11091 bool
11092 Array_construction_expression::is_constant_array() const
11094 if (this->vals_ == NULL)
11095 return true;
11097 // There are no constant constructors for interfaces.
11098 if (this->type_->array_type()->element_type()->interface_type() != NULL)
11099 return false;
11101 for (Expression_list::const_iterator pv = this->vals_->begin();
11102 pv != this->vals_->end();
11103 ++pv)
11105 if (*pv != NULL
11106 && !(*pv)->is_constant()
11107 && (!(*pv)->is_composite_literal()
11108 || (*pv)->is_nonconstant_composite_literal()))
11109 return false;
11111 return true;
11114 // Final type determination.
11116 void
11117 Array_construction_expression::do_determine_type(const Type_context*)
11119 if (this->vals_ == NULL)
11120 return;
11121 Type_context subcontext(this->type_->array_type()->element_type(), false);
11122 for (Expression_list::const_iterator pv = this->vals_->begin();
11123 pv != this->vals_->end();
11124 ++pv)
11126 if (*pv != NULL)
11127 (*pv)->determine_type(&subcontext);
11131 // Check types.
11133 void
11134 Array_construction_expression::do_check_types(Gogo*)
11136 if (this->vals_ == NULL)
11137 return;
11139 Array_type* at = this->type_->array_type();
11140 int i = 0;
11141 Type* element_type = at->element_type();
11142 for (Expression_list::const_iterator pv = this->vals_->begin();
11143 pv != this->vals_->end();
11144 ++pv, ++i)
11146 if (*pv != NULL
11147 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
11149 error_at((*pv)->location(),
11150 "incompatible type for element %d in composite literal",
11151 i + 1);
11152 this->set_is_error();
11156 Expression* length = at->length();
11157 if (length != NULL)
11159 mpz_t val;
11160 mpz_init(val);
11161 Type* type;
11162 if (at->length()->integer_constant_value(true, val, &type))
11164 if (this->vals_->size() > mpz_get_ui(val))
11165 this->report_error(_("too many elements in composite literal"));
11167 mpz_clear(val);
11171 // Get a constructor tree for the array values.
11173 tree
11174 Array_construction_expression::get_constructor_tree(Translate_context* context,
11175 tree type_tree)
11177 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11178 (this->vals_ == NULL
11180 : this->vals_->size()));
11181 Type* element_type = this->type_->array_type()->element_type();
11182 bool is_constant = true;
11183 if (this->vals_ != NULL)
11185 size_t i = 0;
11186 for (Expression_list::const_iterator pv = this->vals_->begin();
11187 pv != this->vals_->end();
11188 ++pv, ++i)
11190 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
11191 elt->index = size_int(i);
11192 if (*pv == NULL)
11193 elt->value = element_type->get_init_tree(context->gogo(), false);
11194 else
11196 tree value_tree = (*pv)->get_tree(context);
11197 elt->value = Expression::convert_for_assignment(context,
11198 element_type,
11199 (*pv)->type(),
11200 value_tree,
11201 this->location());
11203 if (elt->value == error_mark_node)
11204 return error_mark_node;
11205 if (!TREE_CONSTANT(elt->value))
11206 is_constant = false;
11210 tree ret = build_constructor(type_tree, values);
11211 if (is_constant)
11212 TREE_CONSTANT(ret) = 1;
11213 return ret;
11216 // Export an array construction.
11218 void
11219 Array_construction_expression::do_export(Export* exp) const
11221 exp->write_c_string("convert(");
11222 exp->write_type(this->type_);
11223 if (this->vals_ != NULL)
11225 for (Expression_list::const_iterator pv = this->vals_->begin();
11226 pv != this->vals_->end();
11227 ++pv)
11229 exp->write_c_string(", ");
11230 if (*pv != NULL)
11231 (*pv)->export_expression(exp);
11234 exp->write_c_string(")");
11237 // Construct a fixed array.
11239 class Fixed_array_construction_expression :
11240 public Array_construction_expression
11242 public:
11243 Fixed_array_construction_expression(Type* type, Expression_list* vals,
11244 source_location location)
11245 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
11246 type, vals, location)
11248 gcc_assert(type->array_type() != NULL
11249 && type->array_type()->length() != NULL);
11252 protected:
11253 Expression*
11254 do_copy()
11256 return new Fixed_array_construction_expression(this->type(),
11257 (this->vals() == NULL
11258 ? NULL
11259 : this->vals()->copy()),
11260 this->location());
11263 tree
11264 do_get_tree(Translate_context*);
11267 // Return a tree for constructing a fixed array.
11269 tree
11270 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11272 return this->get_constructor_tree(context,
11273 this->type()->get_tree(context->gogo()));
11276 // Construct an open array.
11278 class Open_array_construction_expression : public Array_construction_expression
11280 public:
11281 Open_array_construction_expression(Type* type, Expression_list* vals,
11282 source_location location)
11283 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
11284 type, vals, location)
11286 gcc_assert(type->array_type() != NULL
11287 && type->array_type()->length() == NULL);
11290 protected:
11291 // Note that taking the address of an open array literal is invalid.
11293 Expression*
11294 do_copy()
11296 return new Open_array_construction_expression(this->type(),
11297 (this->vals() == NULL
11298 ? NULL
11299 : this->vals()->copy()),
11300 this->location());
11303 tree
11304 do_get_tree(Translate_context*);
11307 // Return a tree for constructing an open array.
11309 tree
11310 Open_array_construction_expression::do_get_tree(Translate_context* context)
11312 Array_type* array_type = this->type()->array_type();
11313 if (array_type == NULL)
11315 gcc_assert(this->type()->is_error());
11316 return error_mark_node;
11319 Type* element_type = array_type->element_type();
11320 tree element_type_tree = element_type->get_tree(context->gogo());
11321 if (element_type_tree == error_mark_node)
11322 return error_mark_node;
11324 tree values;
11325 tree length_tree;
11326 if (this->vals() == NULL || this->vals()->empty())
11328 // We need to create a unique value.
11329 tree max = size_int(0);
11330 tree constructor_type = build_array_type(element_type_tree,
11331 build_index_type(max));
11332 if (constructor_type == error_mark_node)
11333 return error_mark_node;
11334 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
11335 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
11336 elt->index = size_int(0);
11337 elt->value = element_type->get_init_tree(context->gogo(), false);
11338 values = build_constructor(constructor_type, vec);
11339 if (TREE_CONSTANT(elt->value))
11340 TREE_CONSTANT(values) = 1;
11341 length_tree = size_int(0);
11343 else
11345 tree max = size_int(this->vals()->size() - 1);
11346 tree constructor_type = build_array_type(element_type_tree,
11347 build_index_type(max));
11348 if (constructor_type == error_mark_node)
11349 return error_mark_node;
11350 values = this->get_constructor_tree(context, constructor_type);
11351 length_tree = size_int(this->vals()->size());
11354 if (values == error_mark_node)
11355 return error_mark_node;
11357 bool is_constant_initializer = TREE_CONSTANT(values);
11359 // We have to copy the initial values into heap memory if we are in
11360 // a function or if the values are not constants. We also have to
11361 // copy them if they may contain pointers in a non-constant context,
11362 // as otherwise the garbage collector won't see them.
11363 bool copy_to_heap = (context->function() != NULL
11364 || !is_constant_initializer
11365 || (element_type->has_pointer()
11366 && !context->is_const()));
11368 if (is_constant_initializer)
11370 tree tmp = build_decl(this->location(), VAR_DECL,
11371 create_tmp_var_name("C"), TREE_TYPE(values));
11372 DECL_EXTERNAL(tmp) = 0;
11373 TREE_PUBLIC(tmp) = 0;
11374 TREE_STATIC(tmp) = 1;
11375 DECL_ARTIFICIAL(tmp) = 1;
11376 if (copy_to_heap)
11378 // If we are not copying the value to the heap, we will only
11379 // initialize the value once, so we can use this directly
11380 // rather than copying it. In that case we can't make it
11381 // read-only, because the program is permitted to change it.
11382 TREE_READONLY(tmp) = 1;
11383 TREE_CONSTANT(tmp) = 1;
11385 DECL_INITIAL(tmp) = values;
11386 rest_of_decl_compilation(tmp, 1, 0);
11387 values = tmp;
11390 tree space;
11391 tree set;
11392 if (!copy_to_heap)
11394 // the initializer will only run once.
11395 space = build_fold_addr_expr(values);
11396 set = NULL_TREE;
11398 else
11400 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
11401 space = context->gogo()->allocate_memory(element_type, memsize,
11402 this->location());
11403 space = save_expr(space);
11405 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
11406 tree ref = build_fold_indirect_ref_loc(this->location(), s);
11407 TREE_THIS_NOTRAP(ref) = 1;
11408 set = build2(MODIFY_EXPR, void_type_node, ref, values);
11411 // Build a constructor for the open array.
11413 tree type_tree = this->type()->get_tree(context->gogo());
11414 if (type_tree == error_mark_node)
11415 return error_mark_node;
11416 gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11418 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
11420 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
11421 tree field = TYPE_FIELDS(type_tree);
11422 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
11423 elt->index = field;
11424 elt->value = fold_convert(TREE_TYPE(field), space);
11426 elt = VEC_quick_push(constructor_elt, init, NULL);
11427 field = DECL_CHAIN(field);
11428 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
11429 elt->index = field;
11430 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11432 elt = VEC_quick_push(constructor_elt, init, NULL);
11433 field = DECL_CHAIN(field);
11434 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
11435 elt->index = field;
11436 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11438 tree constructor = build_constructor(type_tree, init);
11439 if (constructor == error_mark_node)
11440 return error_mark_node;
11441 if (!copy_to_heap)
11442 TREE_CONSTANT(constructor) = 1;
11444 if (set == NULL_TREE)
11445 return constructor;
11446 else
11447 return build2(COMPOUND_EXPR, type_tree, set, constructor);
11450 // Make a slice composite literal. This is used by the type
11451 // descriptor code.
11453 Expression*
11454 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
11455 source_location location)
11457 gcc_assert(type->is_open_array_type());
11458 return new Open_array_construction_expression(type, vals, location);
11461 // Construct a map.
11463 class Map_construction_expression : public Expression
11465 public:
11466 Map_construction_expression(Type* type, Expression_list* vals,
11467 source_location location)
11468 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
11469 type_(type), vals_(vals)
11470 { gcc_assert(vals == NULL || vals->size() % 2 == 0); }
11472 protected:
11474 do_traverse(Traverse* traverse);
11476 Type*
11477 do_type()
11478 { return this->type_; }
11480 void
11481 do_determine_type(const Type_context*);
11483 void
11484 do_check_types(Gogo*);
11486 Expression*
11487 do_copy()
11489 return new Map_construction_expression(this->type_, this->vals_->copy(),
11490 this->location());
11493 tree
11494 do_get_tree(Translate_context*);
11496 void
11497 do_export(Export*) const;
11499 private:
11500 // The type of the map to construct.
11501 Type* type_;
11502 // The list of values.
11503 Expression_list* vals_;
11506 // Traversal.
11509 Map_construction_expression::do_traverse(Traverse* traverse)
11511 if (this->vals_ != NULL
11512 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11513 return TRAVERSE_EXIT;
11514 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11515 return TRAVERSE_EXIT;
11516 return TRAVERSE_CONTINUE;
11519 // Final type determination.
11521 void
11522 Map_construction_expression::do_determine_type(const Type_context*)
11524 if (this->vals_ == NULL)
11525 return;
11527 Map_type* mt = this->type_->map_type();
11528 Type_context key_context(mt->key_type(), false);
11529 Type_context val_context(mt->val_type(), false);
11530 for (Expression_list::const_iterator pv = this->vals_->begin();
11531 pv != this->vals_->end();
11532 ++pv)
11534 (*pv)->determine_type(&key_context);
11535 ++pv;
11536 (*pv)->determine_type(&val_context);
11540 // Check types.
11542 void
11543 Map_construction_expression::do_check_types(Gogo*)
11545 if (this->vals_ == NULL)
11546 return;
11548 Map_type* mt = this->type_->map_type();
11549 int i = 0;
11550 Type* key_type = mt->key_type();
11551 Type* val_type = mt->val_type();
11552 for (Expression_list::const_iterator pv = this->vals_->begin();
11553 pv != this->vals_->end();
11554 ++pv, ++i)
11556 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
11558 error_at((*pv)->location(),
11559 "incompatible type for element %d key in map construction",
11560 i + 1);
11561 this->set_is_error();
11563 ++pv;
11564 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
11566 error_at((*pv)->location(),
11567 ("incompatible type for element %d value "
11568 "in map construction"),
11569 i + 1);
11570 this->set_is_error();
11575 // Return a tree for constructing a map.
11577 tree
11578 Map_construction_expression::do_get_tree(Translate_context* context)
11580 Gogo* gogo = context->gogo();
11581 source_location loc = this->location();
11583 Map_type* mt = this->type_->map_type();
11585 // Build a struct to hold the key and value.
11586 tree struct_type = make_node(RECORD_TYPE);
11588 Type* key_type = mt->key_type();
11589 tree id = get_identifier("__key");
11590 tree key_type_tree = key_type->get_tree(gogo);
11591 if (key_type_tree == error_mark_node)
11592 return error_mark_node;
11593 tree key_field = build_decl(loc, FIELD_DECL, id, key_type_tree);
11594 DECL_CONTEXT(key_field) = struct_type;
11595 TYPE_FIELDS(struct_type) = key_field;
11597 Type* val_type = mt->val_type();
11598 id = get_identifier("__val");
11599 tree val_type_tree = val_type->get_tree(gogo);
11600 if (val_type_tree == error_mark_node)
11601 return error_mark_node;
11602 tree val_field = build_decl(loc, FIELD_DECL, id, val_type_tree);
11603 DECL_CONTEXT(val_field) = struct_type;
11604 DECL_CHAIN(key_field) = val_field;
11606 layout_type(struct_type);
11608 bool is_constant = true;
11609 size_t i = 0;
11610 tree valaddr;
11611 tree make_tmp;
11613 if (this->vals_ == NULL || this->vals_->empty())
11615 valaddr = null_pointer_node;
11616 make_tmp = NULL_TREE;
11618 else
11620 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11621 this->vals_->size() / 2);
11623 for (Expression_list::const_iterator pv = this->vals_->begin();
11624 pv != this->vals_->end();
11625 ++pv, ++i)
11627 bool one_is_constant = true;
11629 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
11631 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
11632 elt->index = key_field;
11633 tree val_tree = (*pv)->get_tree(context);
11634 elt->value = Expression::convert_for_assignment(context, key_type,
11635 (*pv)->type(),
11636 val_tree, loc);
11637 if (elt->value == error_mark_node)
11638 return error_mark_node;
11639 if (!TREE_CONSTANT(elt->value))
11640 one_is_constant = false;
11642 ++pv;
11644 elt = VEC_quick_push(constructor_elt, one, NULL);
11645 elt->index = val_field;
11646 val_tree = (*pv)->get_tree(context);
11647 elt->value = Expression::convert_for_assignment(context, val_type,
11648 (*pv)->type(),
11649 val_tree, loc);
11650 if (elt->value == error_mark_node)
11651 return error_mark_node;
11652 if (!TREE_CONSTANT(elt->value))
11653 one_is_constant = false;
11655 elt = VEC_quick_push(constructor_elt, values, NULL);
11656 elt->index = size_int(i);
11657 elt->value = build_constructor(struct_type, one);
11658 if (one_is_constant)
11659 TREE_CONSTANT(elt->value) = 1;
11660 else
11661 is_constant = false;
11664 tree index_type = build_index_type(size_int(i - 1));
11665 tree array_type = build_array_type(struct_type, index_type);
11666 tree init = build_constructor(array_type, values);
11667 if (is_constant)
11668 TREE_CONSTANT(init) = 1;
11669 tree tmp;
11670 if (current_function_decl != NULL)
11672 tmp = create_tmp_var(array_type, get_name(array_type));
11673 DECL_INITIAL(tmp) = init;
11674 make_tmp = fold_build1_loc(loc, DECL_EXPR, void_type_node, tmp);
11675 TREE_ADDRESSABLE(tmp) = 1;
11677 else
11679 tmp = build_decl(loc, VAR_DECL, create_tmp_var_name("M"), array_type);
11680 DECL_EXTERNAL(tmp) = 0;
11681 TREE_PUBLIC(tmp) = 0;
11682 TREE_STATIC(tmp) = 1;
11683 DECL_ARTIFICIAL(tmp) = 1;
11684 if (!TREE_CONSTANT(init))
11685 make_tmp = fold_build2_loc(loc, INIT_EXPR, void_type_node, tmp,
11686 init);
11687 else
11689 TREE_READONLY(tmp) = 1;
11690 TREE_CONSTANT(tmp) = 1;
11691 DECL_INITIAL(tmp) = init;
11692 make_tmp = NULL_TREE;
11694 rest_of_decl_compilation(tmp, 1, 0);
11697 valaddr = build_fold_addr_expr(tmp);
11700 tree descriptor = gogo->map_descriptor(mt);
11702 tree type_tree = this->type_->get_tree(gogo);
11703 if (type_tree == error_mark_node)
11704 return error_mark_node;
11706 static tree construct_map_fndecl;
11707 tree call = Gogo::call_builtin(&construct_map_fndecl,
11708 loc,
11709 "__go_construct_map",
11711 type_tree,
11712 TREE_TYPE(descriptor),
11713 descriptor,
11714 sizetype,
11715 size_int(i),
11716 sizetype,
11717 TYPE_SIZE_UNIT(struct_type),
11718 sizetype,
11719 byte_position(val_field),
11720 sizetype,
11721 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
11722 const_ptr_type_node,
11723 fold_convert(const_ptr_type_node, valaddr));
11724 if (call == error_mark_node)
11725 return error_mark_node;
11727 tree ret;
11728 if (make_tmp == NULL)
11729 ret = call;
11730 else
11731 ret = fold_build2_loc(loc, COMPOUND_EXPR, type_tree, make_tmp, call);
11732 return ret;
11735 // Export an array construction.
11737 void
11738 Map_construction_expression::do_export(Export* exp) const
11740 exp->write_c_string("convert(");
11741 exp->write_type(this->type_);
11742 for (Expression_list::const_iterator pv = this->vals_->begin();
11743 pv != this->vals_->end();
11744 ++pv)
11746 exp->write_c_string(", ");
11747 (*pv)->export_expression(exp);
11749 exp->write_c_string(")");
11752 // A general composite literal. This is lowered to a type specific
11753 // version.
11755 class Composite_literal_expression : public Parser_expression
11757 public:
11758 Composite_literal_expression(Type* type, int depth, bool has_keys,
11759 Expression_list* vals, source_location location)
11760 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
11761 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
11764 protected:
11766 do_traverse(Traverse* traverse);
11768 Expression*
11769 do_lower(Gogo*, Named_object*, int);
11771 Expression*
11772 do_copy()
11774 return new Composite_literal_expression(this->type_, this->depth_,
11775 this->has_keys_,
11776 (this->vals_ == NULL
11777 ? NULL
11778 : this->vals_->copy()),
11779 this->location());
11782 private:
11783 Expression*
11784 lower_struct(Gogo*, Type*);
11786 Expression*
11787 lower_array(Type*);
11789 Expression*
11790 make_array(Type*, Expression_list*);
11792 Expression*
11793 lower_map(Gogo*, Named_object*, Type*);
11795 // The type of the composite literal.
11796 Type* type_;
11797 // The depth within a list of composite literals within a composite
11798 // literal, when the type is omitted.
11799 int depth_;
11800 // The values to put in the composite literal.
11801 Expression_list* vals_;
11802 // If this is true, then VALS_ is a list of pairs: a key and a
11803 // value. In an array initializer, a missing key will be NULL.
11804 bool has_keys_;
11807 // Traversal.
11810 Composite_literal_expression::do_traverse(Traverse* traverse)
11812 if (this->vals_ != NULL
11813 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11814 return TRAVERSE_EXIT;
11815 return Type::traverse(this->type_, traverse);
11818 // Lower a generic composite literal into a specific version based on
11819 // the type.
11821 Expression*
11822 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int)
11824 Type* type = this->type_;
11826 for (int depth = this->depth_; depth > 0; --depth)
11828 if (type->array_type() != NULL)
11829 type = type->array_type()->element_type();
11830 else if (type->map_type() != NULL)
11831 type = type->map_type()->val_type();
11832 else
11834 if (!type->is_error())
11835 error_at(this->location(),
11836 ("may only omit types within composite literals "
11837 "of slice, array, or map type"));
11838 return Expression::make_error(this->location());
11842 if (type->is_error())
11843 return Expression::make_error(this->location());
11844 else if (type->struct_type() != NULL)
11845 return this->lower_struct(gogo, type);
11846 else if (type->array_type() != NULL)
11847 return this->lower_array(type);
11848 else if (type->map_type() != NULL)
11849 return this->lower_map(gogo, function, type);
11850 else
11852 error_at(this->location(),
11853 ("expected struct, slice, array, or map type "
11854 "for composite literal"));
11855 return Expression::make_error(this->location());
11859 // Lower a struct composite literal.
11861 Expression*
11862 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
11864 source_location location = this->location();
11865 Struct_type* st = type->struct_type();
11866 if (this->vals_ == NULL || !this->has_keys_)
11867 return new Struct_construction_expression(type, this->vals_, location);
11869 size_t field_count = st->field_count();
11870 std::vector<Expression*> vals(field_count);
11871 Expression_list::const_iterator p = this->vals_->begin();
11872 while (p != this->vals_->end())
11874 Expression* name_expr = *p;
11876 ++p;
11877 gcc_assert(p != this->vals_->end());
11878 Expression* val = *p;
11880 ++p;
11882 if (name_expr == NULL)
11884 error_at(val->location(), "mixture of field and value initializers");
11885 return Expression::make_error(location);
11888 bool bad_key = false;
11889 std::string name;
11890 const Named_object* no = NULL;
11891 switch (name_expr->classification())
11893 case EXPRESSION_UNKNOWN_REFERENCE:
11894 name = name_expr->unknown_expression()->name();
11895 break;
11897 case EXPRESSION_CONST_REFERENCE:
11898 no = static_cast<Const_expression*>(name_expr)->named_object();
11899 break;
11901 case EXPRESSION_TYPE:
11903 Type* t = name_expr->type();
11904 Named_type* nt = t->named_type();
11905 if (nt == NULL)
11906 bad_key = true;
11907 else
11908 no = nt->named_object();
11910 break;
11912 case EXPRESSION_VAR_REFERENCE:
11913 no = name_expr->var_expression()->named_object();
11914 break;
11916 case EXPRESSION_FUNC_REFERENCE:
11917 no = name_expr->func_expression()->named_object();
11918 break;
11920 case EXPRESSION_UNARY:
11921 // If there is a local variable around with the same name as
11922 // the field, and this occurs in the closure, then the
11923 // parser may turn the field reference into an indirection
11924 // through the closure. FIXME: This is a mess.
11926 bad_key = true;
11927 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
11928 if (ue->op() == OPERATOR_MULT)
11930 Field_reference_expression* fre =
11931 ue->operand()->field_reference_expression();
11932 if (fre != NULL)
11934 Struct_type* st =
11935 fre->expr()->type()->deref()->struct_type();
11936 if (st != NULL)
11938 const Struct_field* sf = st->field(fre->field_index());
11939 name = sf->field_name();
11940 char buf[20];
11941 snprintf(buf, sizeof buf, "%u", fre->field_index());
11942 size_t buflen = strlen(buf);
11943 if (name.compare(name.length() - buflen, buflen, buf)
11944 == 0)
11946 name = name.substr(0, name.length() - buflen);
11947 bad_key = false;
11953 break;
11955 default:
11956 bad_key = true;
11957 break;
11959 if (bad_key)
11961 error_at(name_expr->location(), "expected struct field name");
11962 return Expression::make_error(location);
11965 if (no != NULL)
11967 name = no->name();
11969 // A predefined name won't be packed. If it starts with a
11970 // lower case letter we need to check for that case, because
11971 // the field name will be packed.
11972 if (!Gogo::is_hidden_name(name)
11973 && name[0] >= 'a'
11974 && name[0] <= 'z')
11976 Named_object* gno = gogo->lookup_global(name.c_str());
11977 if (gno == no)
11978 name = gogo->pack_hidden_name(name, false);
11982 unsigned int index;
11983 const Struct_field* sf = st->find_local_field(name, &index);
11984 if (sf == NULL)
11986 error_at(name_expr->location(), "unknown field %qs in %qs",
11987 Gogo::message_name(name).c_str(),
11988 (type->named_type() != NULL
11989 ? type->named_type()->message_name().c_str()
11990 : "unnamed struct"));
11991 return Expression::make_error(location);
11993 if (vals[index] != NULL)
11995 error_at(name_expr->location(),
11996 "duplicate value for field %qs in %qs",
11997 Gogo::message_name(name).c_str(),
11998 (type->named_type() != NULL
11999 ? type->named_type()->message_name().c_str()
12000 : "unnamed struct"));
12001 return Expression::make_error(location);
12004 vals[index] = val;
12007 Expression_list* list = new Expression_list;
12008 list->reserve(field_count);
12009 for (size_t i = 0; i < field_count; ++i)
12010 list->push_back(vals[i]);
12012 return new Struct_construction_expression(type, list, location);
12015 // Lower an array composite literal.
12017 Expression*
12018 Composite_literal_expression::lower_array(Type* type)
12020 source_location location = this->location();
12021 if (this->vals_ == NULL || !this->has_keys_)
12022 return this->make_array(type, this->vals_);
12024 std::vector<Expression*> vals;
12025 vals.reserve(this->vals_->size());
12026 unsigned long index = 0;
12027 Expression_list::const_iterator p = this->vals_->begin();
12028 while (p != this->vals_->end())
12030 Expression* index_expr = *p;
12032 ++p;
12033 gcc_assert(p != this->vals_->end());
12034 Expression* val = *p;
12036 ++p;
12038 if (index_expr != NULL)
12040 mpz_t ival;
12041 mpz_init(ival);
12043 Type* dummy;
12044 if (!index_expr->integer_constant_value(true, ival, &dummy))
12046 mpz_clear(ival);
12047 error_at(index_expr->location(),
12048 "index expression is not integer constant");
12049 return Expression::make_error(location);
12052 if (mpz_sgn(ival) < 0)
12054 mpz_clear(ival);
12055 error_at(index_expr->location(), "index expression is negative");
12056 return Expression::make_error(location);
12059 index = mpz_get_ui(ival);
12060 if (mpz_cmp_ui(ival, index) != 0)
12062 mpz_clear(ival);
12063 error_at(index_expr->location(), "index value overflow");
12064 return Expression::make_error(location);
12067 Named_type* ntype = Type::lookup_integer_type("int");
12068 Integer_type* inttype = ntype->integer_type();
12069 mpz_t max;
12070 mpz_init_set_ui(max, 1);
12071 mpz_mul_2exp(max, max, inttype->bits() - 1);
12072 bool ok = mpz_cmp(ival, max) < 0;
12073 mpz_clear(max);
12074 if (!ok)
12076 mpz_clear(ival);
12077 error_at(index_expr->location(), "index value overflow");
12078 return Expression::make_error(location);
12081 mpz_clear(ival);
12083 // FIXME: Our representation isn't very good; this avoids
12084 // thrashing.
12085 if (index > 0x1000000)
12087 error_at(index_expr->location(), "index too large for compiler");
12088 return Expression::make_error(location);
12092 if (index == vals.size())
12093 vals.push_back(val);
12094 else
12096 if (index > vals.size())
12098 vals.reserve(index + 32);
12099 vals.resize(index + 1, static_cast<Expression*>(NULL));
12101 if (vals[index] != NULL)
12103 error_at((index_expr != NULL
12104 ? index_expr->location()
12105 : val->location()),
12106 "duplicate value for index %lu",
12107 index);
12108 return Expression::make_error(location);
12110 vals[index] = val;
12113 ++index;
12116 size_t size = vals.size();
12117 Expression_list* list = new Expression_list;
12118 list->reserve(size);
12119 for (size_t i = 0; i < size; ++i)
12120 list->push_back(vals[i]);
12122 return this->make_array(type, list);
12125 // Actually build the array composite literal. This handles
12126 // [...]{...}.
12128 Expression*
12129 Composite_literal_expression::make_array(Type* type, Expression_list* vals)
12131 source_location location = this->location();
12132 Array_type* at = type->array_type();
12133 if (at->length() != NULL && at->length()->is_nil_expression())
12135 size_t size = vals == NULL ? 0 : vals->size();
12136 mpz_t vlen;
12137 mpz_init_set_ui(vlen, size);
12138 Expression* elen = Expression::make_integer(&vlen, NULL, location);
12139 mpz_clear(vlen);
12140 at = Type::make_array_type(at->element_type(), elen);
12141 type = at;
12143 if (at->length() != NULL)
12144 return new Fixed_array_construction_expression(type, vals, location);
12145 else
12146 return new Open_array_construction_expression(type, vals, location);
12149 // Lower a map composite literal.
12151 Expression*
12152 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
12153 Type* type)
12155 source_location location = this->location();
12156 if (this->vals_ != NULL)
12158 if (!this->has_keys_)
12160 error_at(location, "map composite literal must have keys");
12161 return Expression::make_error(location);
12164 for (Expression_list::iterator p = this->vals_->begin();
12165 p != this->vals_->end();
12166 p += 2)
12168 if (*p == NULL)
12170 ++p;
12171 error_at((*p)->location(),
12172 "map composite literal must have keys for every value");
12173 return Expression::make_error(location);
12175 // Make sure we have lowered the key; it may not have been
12176 // lowered in order to handle keys for struct composite
12177 // literals. Lower it now to get the right error message.
12178 if ((*p)->unknown_expression() != NULL)
12180 (*p)->unknown_expression()->clear_is_composite_literal_key();
12181 gogo->lower_expression(function, &*p);
12182 gcc_assert((*p)->is_error_expression());
12183 return Expression::make_error(location);
12188 return new Map_construction_expression(type, this->vals_, location);
12191 // Make a composite literal expression.
12193 Expression*
12194 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
12195 Expression_list* vals,
12196 source_location location)
12198 return new Composite_literal_expression(type, depth, has_keys, vals,
12199 location);
12202 // Return whether this expression is a composite literal.
12204 bool
12205 Expression::is_composite_literal() const
12207 switch (this->classification_)
12209 case EXPRESSION_COMPOSITE_LITERAL:
12210 case EXPRESSION_STRUCT_CONSTRUCTION:
12211 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12212 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12213 case EXPRESSION_MAP_CONSTRUCTION:
12214 return true;
12215 default:
12216 return false;
12220 // Return whether this expression is a composite literal which is not
12221 // constant.
12223 bool
12224 Expression::is_nonconstant_composite_literal() const
12226 switch (this->classification_)
12228 case EXPRESSION_STRUCT_CONSTRUCTION:
12230 const Struct_construction_expression *psce =
12231 static_cast<const Struct_construction_expression*>(this);
12232 return !psce->is_constant_struct();
12234 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12236 const Fixed_array_construction_expression *pace =
12237 static_cast<const Fixed_array_construction_expression*>(this);
12238 return !pace->is_constant_array();
12240 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12242 const Open_array_construction_expression *pace =
12243 static_cast<const Open_array_construction_expression*>(this);
12244 return !pace->is_constant_array();
12246 case EXPRESSION_MAP_CONSTRUCTION:
12247 return true;
12248 default:
12249 return false;
12253 // Return true if this is a reference to a local variable.
12255 bool
12256 Expression::is_local_variable() const
12258 const Var_expression* ve = this->var_expression();
12259 if (ve == NULL)
12260 return false;
12261 const Named_object* no = ve->named_object();
12262 return (no->is_result_variable()
12263 || (no->is_variable() && !no->var_value()->is_global()));
12266 // Class Type_guard_expression.
12268 // Traversal.
12271 Type_guard_expression::do_traverse(Traverse* traverse)
12273 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
12274 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12275 return TRAVERSE_EXIT;
12276 return TRAVERSE_CONTINUE;
12279 // Check types of a type guard expression. The expression must have
12280 // an interface type, but the actual type conversion is checked at run
12281 // time.
12283 void
12284 Type_guard_expression::do_check_types(Gogo*)
12286 // 6g permits using a type guard with unsafe.pointer; we are
12287 // compatible.
12288 Type* expr_type = this->expr_->type();
12289 if (expr_type->is_unsafe_pointer_type())
12291 if (this->type_->points_to() == NULL
12292 && (this->type_->integer_type() == NULL
12293 || (this->type_->forwarded()
12294 != Type::lookup_integer_type("uintptr"))))
12295 this->report_error(_("invalid unsafe.Pointer conversion"));
12297 else if (this->type_->is_unsafe_pointer_type())
12299 if (expr_type->points_to() == NULL
12300 && (expr_type->integer_type() == NULL
12301 || (expr_type->forwarded()
12302 != Type::lookup_integer_type("uintptr"))))
12303 this->report_error(_("invalid unsafe.Pointer conversion"));
12305 else if (expr_type->interface_type() == NULL)
12307 if (!expr_type->is_error() && !this->type_->is_error())
12308 this->report_error(_("type assertion only valid for interface types"));
12309 this->set_is_error();
12311 else if (this->type_->interface_type() == NULL)
12313 std::string reason;
12314 if (!expr_type->interface_type()->implements_interface(this->type_,
12315 &reason))
12317 if (!this->type_->is_error())
12319 if (reason.empty())
12320 this->report_error(_("impossible type assertion: "
12321 "type does not implement interface"));
12322 else
12323 error_at(this->location(),
12324 ("impossible type assertion: "
12325 "type does not implement interface (%s)"),
12326 reason.c_str());
12328 this->set_is_error();
12333 // Return a tree for a type guard expression.
12335 tree
12336 Type_guard_expression::do_get_tree(Translate_context* context)
12338 Gogo* gogo = context->gogo();
12339 tree expr_tree = this->expr_->get_tree(context);
12340 if (expr_tree == error_mark_node)
12341 return error_mark_node;
12342 Type* expr_type = this->expr_->type();
12343 if ((this->type_->is_unsafe_pointer_type()
12344 && (expr_type->points_to() != NULL
12345 || expr_type->integer_type() != NULL))
12346 || (expr_type->is_unsafe_pointer_type()
12347 && this->type_->points_to() != NULL))
12348 return convert_to_pointer(this->type_->get_tree(gogo), expr_tree);
12349 else if (expr_type->is_unsafe_pointer_type()
12350 && this->type_->integer_type() != NULL)
12351 return convert_to_integer(this->type_->get_tree(gogo), expr_tree);
12352 else if (this->type_->interface_type() != NULL)
12353 return Expression::convert_interface_to_interface(context, this->type_,
12354 this->expr_->type(),
12355 expr_tree, true,
12356 this->location());
12357 else
12358 return Expression::convert_for_assignment(context, this->type_,
12359 this->expr_->type(), expr_tree,
12360 this->location());
12363 // Make a type guard expression.
12365 Expression*
12366 Expression::make_type_guard(Expression* expr, Type* type,
12367 source_location location)
12369 return new Type_guard_expression(expr, type, location);
12372 // Class Heap_composite_expression.
12374 // When you take the address of a composite literal, it is allocated
12375 // on the heap. This class implements that.
12377 class Heap_composite_expression : public Expression
12379 public:
12380 Heap_composite_expression(Expression* expr, source_location location)
12381 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
12382 expr_(expr)
12385 protected:
12387 do_traverse(Traverse* traverse)
12388 { return Expression::traverse(&this->expr_, traverse); }
12390 Type*
12391 do_type()
12392 { return Type::make_pointer_type(this->expr_->type()); }
12394 void
12395 do_determine_type(const Type_context*)
12396 { this->expr_->determine_type_no_context(); }
12398 Expression*
12399 do_copy()
12401 return Expression::make_heap_composite(this->expr_->copy(),
12402 this->location());
12405 tree
12406 do_get_tree(Translate_context*);
12408 // We only export global objects, and the parser does not generate
12409 // this in global scope.
12410 void
12411 do_export(Export*) const
12412 { gcc_unreachable(); }
12414 private:
12415 // The composite literal which is being put on the heap.
12416 Expression* expr_;
12419 // Return a tree which allocates a composite literal on the heap.
12421 tree
12422 Heap_composite_expression::do_get_tree(Translate_context* context)
12424 tree expr_tree = this->expr_->get_tree(context);
12425 if (expr_tree == error_mark_node)
12426 return error_mark_node;
12427 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
12428 gcc_assert(TREE_CODE(expr_size) == INTEGER_CST);
12429 tree space = context->gogo()->allocate_memory(this->expr_->type(),
12430 expr_size, this->location());
12431 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
12432 space = save_expr(space);
12433 tree ref = build_fold_indirect_ref_loc(this->location(), space);
12434 TREE_THIS_NOTRAP(ref) = 1;
12435 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
12436 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
12437 space);
12438 SET_EXPR_LOCATION(ret, this->location());
12439 return ret;
12442 // Allocate a composite literal on the heap.
12444 Expression*
12445 Expression::make_heap_composite(Expression* expr, source_location location)
12447 return new Heap_composite_expression(expr, location);
12450 // Class Receive_expression.
12452 // Return the type of a receive expression.
12454 Type*
12455 Receive_expression::do_type()
12457 Channel_type* channel_type = this->channel_->type()->channel_type();
12458 if (channel_type == NULL)
12459 return Type::make_error_type();
12460 return channel_type->element_type();
12463 // Check types for a receive expression.
12465 void
12466 Receive_expression::do_check_types(Gogo*)
12468 Type* type = this->channel_->type();
12469 if (type->is_error())
12471 this->set_is_error();
12472 return;
12474 if (type->channel_type() == NULL)
12476 this->report_error(_("expected channel"));
12477 return;
12479 if (!type->channel_type()->may_receive())
12481 this->report_error(_("invalid receive on send-only channel"));
12482 return;
12486 // Get a tree for a receive expression.
12488 tree
12489 Receive_expression::do_get_tree(Translate_context* context)
12491 Channel_type* channel_type = this->channel_->type()->channel_type();
12492 if (channel_type == NULL)
12494 gcc_assert(this->channel_->type()->is_error());
12495 return error_mark_node;
12497 Type* element_type = channel_type->element_type();
12498 tree element_type_tree = element_type->get_tree(context->gogo());
12500 tree channel = this->channel_->get_tree(context);
12501 if (element_type_tree == error_mark_node || channel == error_mark_node)
12502 return error_mark_node;
12504 return Gogo::receive_from_channel(element_type_tree, channel,
12505 this->for_select_, this->location());
12508 // Make a receive expression.
12510 Receive_expression*
12511 Expression::make_receive(Expression* channel, source_location location)
12513 return new Receive_expression(channel, location);
12516 // An expression which evaluates to a pointer to the type descriptor
12517 // of a type.
12519 class Type_descriptor_expression : public Expression
12521 public:
12522 Type_descriptor_expression(Type* type, source_location location)
12523 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
12524 type_(type)
12527 protected:
12528 Type*
12529 do_type()
12530 { return Type::make_type_descriptor_ptr_type(); }
12532 void
12533 do_determine_type(const Type_context*)
12536 Expression*
12537 do_copy()
12538 { return this; }
12540 tree
12541 do_get_tree(Translate_context* context)
12542 { return this->type_->type_descriptor_pointer(context->gogo()); }
12544 private:
12545 // The type for which this is the descriptor.
12546 Type* type_;
12549 // Make a type descriptor expression.
12551 Expression*
12552 Expression::make_type_descriptor(Type* type, source_location location)
12554 return new Type_descriptor_expression(type, location);
12557 // An expression which evaluates to some characteristic of a type.
12558 // This is only used to initialize fields of a type descriptor. Using
12559 // a new expression class is slightly inefficient but gives us a good
12560 // separation between the frontend and the middle-end with regard to
12561 // how types are laid out.
12563 class Type_info_expression : public Expression
12565 public:
12566 Type_info_expression(Type* type, Type_info type_info)
12567 : Expression(EXPRESSION_TYPE_INFO, BUILTINS_LOCATION),
12568 type_(type), type_info_(type_info)
12571 protected:
12572 Type*
12573 do_type();
12575 void
12576 do_determine_type(const Type_context*)
12579 Expression*
12580 do_copy()
12581 { return this; }
12583 tree
12584 do_get_tree(Translate_context* context);
12586 private:
12587 // The type for which we are getting information.
12588 Type* type_;
12589 // What information we want.
12590 Type_info type_info_;
12593 // The type is chosen to match what the type descriptor struct
12594 // expects.
12596 Type*
12597 Type_info_expression::do_type()
12599 switch (this->type_info_)
12601 case TYPE_INFO_SIZE:
12602 return Type::lookup_integer_type("uintptr");
12603 case TYPE_INFO_ALIGNMENT:
12604 case TYPE_INFO_FIELD_ALIGNMENT:
12605 return Type::lookup_integer_type("uint8");
12606 default:
12607 gcc_unreachable();
12611 // Return type information in GENERIC.
12613 tree
12614 Type_info_expression::do_get_tree(Translate_context* context)
12616 tree type_tree = this->type_->get_tree(context->gogo());
12617 if (type_tree == error_mark_node)
12618 return error_mark_node;
12620 tree val_type_tree = this->type()->get_tree(context->gogo());
12621 gcc_assert(val_type_tree != error_mark_node);
12623 if (this->type_info_ == TYPE_INFO_SIZE)
12624 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12625 TYPE_SIZE_UNIT(type_tree));
12626 else
12628 unsigned int val;
12629 if (this->type_info_ == TYPE_INFO_ALIGNMENT)
12630 val = go_type_alignment(type_tree);
12631 else
12632 val = go_field_alignment(type_tree);
12633 return build_int_cstu(val_type_tree, val);
12637 // Make a type info expression.
12639 Expression*
12640 Expression::make_type_info(Type* type, Type_info type_info)
12642 return new Type_info_expression(type, type_info);
12645 // An expression which evaluates to the offset of a field within a
12646 // struct. This, like Type_info_expression, q.v., is only used to
12647 // initialize fields of a type descriptor.
12649 class Struct_field_offset_expression : public Expression
12651 public:
12652 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
12653 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET, BUILTINS_LOCATION),
12654 type_(type), field_(field)
12657 protected:
12658 Type*
12659 do_type()
12660 { return Type::lookup_integer_type("uintptr"); }
12662 void
12663 do_determine_type(const Type_context*)
12666 Expression*
12667 do_copy()
12668 { return this; }
12670 tree
12671 do_get_tree(Translate_context* context);
12673 private:
12674 // The type of the struct.
12675 Struct_type* type_;
12676 // The field.
12677 const Struct_field* field_;
12680 // Return a struct field offset in GENERIC.
12682 tree
12683 Struct_field_offset_expression::do_get_tree(Translate_context* context)
12685 tree type_tree = this->type_->get_tree(context->gogo());
12686 if (type_tree == error_mark_node)
12687 return error_mark_node;
12689 tree val_type_tree = this->type()->get_tree(context->gogo());
12690 gcc_assert(val_type_tree != error_mark_node);
12692 const Struct_field_list* fields = this->type_->fields();
12693 tree struct_field_tree = TYPE_FIELDS(type_tree);
12694 Struct_field_list::const_iterator p;
12695 for (p = fields->begin();
12696 p != fields->end();
12697 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
12699 gcc_assert(struct_field_tree != NULL_TREE);
12700 if (&*p == this->field_)
12701 break;
12703 gcc_assert(&*p == this->field_);
12705 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
12706 byte_position(struct_field_tree));
12709 // Make an expression for a struct field offset.
12711 Expression*
12712 Expression::make_struct_field_offset(Struct_type* type,
12713 const Struct_field* field)
12715 return new Struct_field_offset_expression(type, field);
12718 // An expression which evaluates to the address of an unnamed label.
12720 class Label_addr_expression : public Expression
12722 public:
12723 Label_addr_expression(Label* label, source_location location)
12724 : Expression(EXPRESSION_LABEL_ADDR, location),
12725 label_(label)
12728 protected:
12729 Type*
12730 do_type()
12731 { return Type::make_pointer_type(Type::make_void_type()); }
12733 void
12734 do_determine_type(const Type_context*)
12737 Expression*
12738 do_copy()
12739 { return new Label_addr_expression(this->label_, this->location()); }
12741 tree
12742 do_get_tree(Translate_context* context)
12744 return expr_to_tree(this->label_->get_addr(context, this->location()));
12747 private:
12748 // The label whose address we are taking.
12749 Label* label_;
12752 // Make an expression for the address of an unnamed label.
12754 Expression*
12755 Expression::make_label_addr(Label* label, source_location location)
12757 return new Label_addr_expression(label, location);
12760 // Import an expression. This comes at the end in order to see the
12761 // various class definitions.
12763 Expression*
12764 Expression::import_expression(Import* imp)
12766 int c = imp->peek_char();
12767 if (imp->match_c_string("- ")
12768 || imp->match_c_string("! ")
12769 || imp->match_c_string("^ "))
12770 return Unary_expression::do_import(imp);
12771 else if (c == '(')
12772 return Binary_expression::do_import(imp);
12773 else if (imp->match_c_string("true")
12774 || imp->match_c_string("false"))
12775 return Boolean_expression::do_import(imp);
12776 else if (c == '"')
12777 return String_expression::do_import(imp);
12778 else if (c == '-' || (c >= '0' && c <= '9'))
12780 // This handles integers, floats and complex constants.
12781 return Integer_expression::do_import(imp);
12783 else if (imp->match_c_string("nil"))
12784 return Nil_expression::do_import(imp);
12785 else if (imp->match_c_string("convert"))
12786 return Type_conversion_expression::do_import(imp);
12787 else
12789 error_at(imp->location(), "import error: expected expression");
12790 return Expression::make_error(imp->location());
12794 // Class Expression_list.
12796 // Traverse the list.
12799 Expression_list::traverse(Traverse* traverse)
12801 for (Expression_list::iterator p = this->begin();
12802 p != this->end();
12803 ++p)
12805 if (*p != NULL)
12807 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
12808 return TRAVERSE_EXIT;
12811 return TRAVERSE_CONTINUE;
12814 // Copy the list.
12816 Expression_list*
12817 Expression_list::copy()
12819 Expression_list* ret = new Expression_list();
12820 for (Expression_list::iterator p = this->begin();
12821 p != this->end();
12822 ++p)
12824 if (*p == NULL)
12825 ret->push_back(NULL);
12826 else
12827 ret->push_back((*p)->copy());
12829 return ret;
12832 // Return whether an expression list has an error expression.
12834 bool
12835 Expression_list::contains_error() const
12837 for (Expression_list::const_iterator p = this->begin();
12838 p != this->end();
12839 ++p)
12840 if (*p != NULL && (*p)->is_error_expression())
12841 return true;
12842 return false;