compiler: Comparisons return untyped boolean value.
[gcc.git] / gcc / go / gofrontend / expressions.cc
1 // expressions.cc -- Go frontend expression handling.
2
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.
6
7 #include "go-system.h"
8
9 #include <algorithm>
10
11 #include <gmp.h>
12
13 #include "toplev.h"
14 #include "intl.h"
15 #include "tree.h"
16 #include "gimple.h"
17 #include "tree-iterator.h"
18 #include "convert.h"
19 #include "real.h"
20 #include "realmpfr.h"
21
22 #include "go-c.h"
23 #include "gogo.h"
24 #include "types.h"
25 #include "export.h"
26 #include "import.h"
27 #include "statements.h"
28 #include "lex.h"
29 #include "runtime.h"
30 #include "backend.h"
31 #include "expressions.h"
32 #include "ast-dump.h"
33
34 // Class Expression.
35
36 Expression::Expression(Expression_classification classification,
37 Location location)
38 : classification_(classification), location_(location)
39 {
40 }
41
42 Expression::~Expression()
43 {
44 }
45
46 // Traverse the expressions.
47
48 int
49 Expression::traverse(Expression** pexpr, Traverse* traverse)
50 {
51 Expression* expr = *pexpr;
52 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
53 {
54 int t = traverse->expression(pexpr);
55 if (t == TRAVERSE_EXIT)
56 return TRAVERSE_EXIT;
57 else if (t == TRAVERSE_SKIP_COMPONENTS)
58 return TRAVERSE_CONTINUE;
59 }
60 return expr->do_traverse(traverse);
61 }
62
63 // Traverse subexpressions of this expression.
64
65 int
66 Expression::traverse_subexpressions(Traverse* traverse)
67 {
68 return this->do_traverse(traverse);
69 }
70
71 // Default implementation for do_traverse for child classes.
72
73 int
74 Expression::do_traverse(Traverse*)
75 {
76 return TRAVERSE_CONTINUE;
77 }
78
79 // This virtual function is called by the parser if the value of this
80 // expression is being discarded. By default, we give an error.
81 // Expressions with side effects override.
82
83 void
84 Expression::do_discarding_value()
85 {
86 this->unused_value_error();
87 }
88
89 // This virtual function is called to export expressions. This will
90 // only be used by expressions which may be constant.
91
92 void
93 Expression::do_export(Export*) const
94 {
95 go_unreachable();
96 }
97
98 // Give an error saying that the value of the expression is not used.
99
100 void
101 Expression::unused_value_error()
102 {
103 error_at(this->location(), "value computed is not used");
104 }
105
106 // Note that this expression is an error. This is called by children
107 // when they discover an error.
108
109 void
110 Expression::set_is_error()
111 {
112 this->classification_ = EXPRESSION_ERROR;
113 }
114
115 // For children to call to report an error conveniently.
116
117 void
118 Expression::report_error(const char* msg)
119 {
120 error_at(this->location_, "%s", msg);
121 this->set_is_error();
122 }
123
124 // Set types of variables and constants. This is implemented by the
125 // child class.
126
127 void
128 Expression::determine_type(const Type_context* context)
129 {
130 this->do_determine_type(context);
131 }
132
133 // Set types when there is no context.
134
135 void
136 Expression::determine_type_no_context()
137 {
138 Type_context context;
139 this->do_determine_type(&context);
140 }
141
142 // Return a tree handling any conversions which must be done during
143 // assignment.
144
145 tree
146 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
147 Type* rhs_type, tree rhs_tree,
148 Location location)
149 {
150 if (lhs_type->is_error() || rhs_type->is_error())
151 return error_mark_node;
152
153 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
154 return error_mark_node;
155
156 Gogo* gogo = context->gogo();
157
158 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
159 if (lhs_type_tree == error_mark_node)
160 return error_mark_node;
161
162 if (lhs_type->forwarded() != rhs_type->forwarded()
163 && lhs_type->interface_type() != NULL)
164 {
165 if (rhs_type->interface_type() == NULL)
166 return Expression::convert_type_to_interface(context, lhs_type,
167 rhs_type, rhs_tree,
168 location);
169 else
170 return Expression::convert_interface_to_interface(context, lhs_type,
171 rhs_type, rhs_tree,
172 false, location);
173 }
174 else if (lhs_type->forwarded() != rhs_type->forwarded()
175 && rhs_type->interface_type() != NULL)
176 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
177 rhs_tree, location);
178 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
179 {
180 // Assigning nil to an open array.
181 go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
182
183 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
184
185 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
186 tree field = TYPE_FIELDS(lhs_type_tree);
187 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
188 "__values") == 0);
189 elt->index = field;
190 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
191
192 elt = VEC_quick_push(constructor_elt, init, NULL);
193 field = DECL_CHAIN(field);
194 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
195 "__count") == 0);
196 elt->index = field;
197 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
198
199 elt = VEC_quick_push(constructor_elt, init, NULL);
200 field = DECL_CHAIN(field);
201 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
202 "__capacity") == 0);
203 elt->index = field;
204 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
205
206 tree val = build_constructor(lhs_type_tree, init);
207 TREE_CONSTANT(val) = 1;
208
209 return val;
210 }
211 else if (rhs_type->is_nil_type())
212 {
213 // The left hand side should be a pointer type at the tree
214 // level.
215 go_assert(POINTER_TYPE_P(lhs_type_tree));
216 return fold_convert(lhs_type_tree, null_pointer_node);
217 }
218 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
219 {
220 // No conversion is needed.
221 return rhs_tree;
222 }
223 else if (POINTER_TYPE_P(lhs_type_tree)
224 || INTEGRAL_TYPE_P(lhs_type_tree)
225 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
226 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
227 return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree);
228 else if ((TREE_CODE(lhs_type_tree) == RECORD_TYPE
229 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
230 || (TREE_CODE(lhs_type_tree) == ARRAY_TYPE
231 && TREE_CODE(TREE_TYPE(rhs_tree)) == ARRAY_TYPE))
232 {
233 // Avoid confusion from zero sized variables which may be
234 // represented as non-zero-sized.
235 if (int_size_in_bytes(lhs_type_tree) == 0
236 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
237 return rhs_tree;
238
239 // This conversion must be permitted by Go, or we wouldn't have
240 // gotten here.
241 go_assert(int_size_in_bytes(lhs_type_tree)
242 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
243 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
244 lhs_type_tree, rhs_tree);
245 }
246 else
247 {
248 go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
249 return rhs_tree;
250 }
251 }
252
253 // Return a tree for a conversion from a non-interface type to an
254 // interface type.
255
256 tree
257 Expression::convert_type_to_interface(Translate_context* context,
258 Type* lhs_type, Type* rhs_type,
259 tree rhs_tree, Location location)
260 {
261 Gogo* gogo = context->gogo();
262 Interface_type* lhs_interface_type = lhs_type->interface_type();
263 bool lhs_is_empty = lhs_interface_type->is_empty();
264
265 // Since RHS_TYPE is a static type, we can create the interface
266 // method table at compile time.
267
268 // When setting an interface to nil, we just set both fields to
269 // NULL.
270 if (rhs_type->is_nil_type())
271 {
272 Btype* lhs_btype = lhs_type->get_backend(gogo);
273 return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
274 }
275
276 // This should have been checked already.
277 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
278
279 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
280 if (lhs_type_tree == error_mark_node)
281 return error_mark_node;
282
283 // An interface is a tuple. If LHS_TYPE is an empty interface type,
284 // then the first field is the type descriptor for RHS_TYPE.
285 // Otherwise it is the interface method table for RHS_TYPE.
286 tree first_field_value;
287 if (lhs_is_empty)
288 first_field_value = rhs_type->type_descriptor_pointer(gogo, location);
289 else
290 {
291 // Build the interface method table for this interface and this
292 // object type: a list of function pointers for each interface
293 // method.
294 Named_type* rhs_named_type = rhs_type->named_type();
295 bool is_pointer = false;
296 if (rhs_named_type == NULL)
297 {
298 rhs_named_type = rhs_type->deref()->named_type();
299 is_pointer = true;
300 }
301 tree method_table;
302 if (rhs_named_type == NULL)
303 method_table = null_pointer_node;
304 else
305 method_table =
306 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
307 is_pointer);
308 first_field_value = fold_convert_loc(location.gcc_location(),
309 const_ptr_type_node, method_table);
310 }
311 if (first_field_value == error_mark_node)
312 return error_mark_node;
313
314 // Start building a constructor for the value we will return.
315
316 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
317
318 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
319 tree field = TYPE_FIELDS(lhs_type_tree);
320 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
321 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
322 elt->index = field;
323 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
324 first_field_value);
325
326 elt = VEC_quick_push(constructor_elt, init, NULL);
327 field = DECL_CHAIN(field);
328 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
329 elt->index = field;
330
331 if (rhs_type->points_to() != NULL)
332 {
333 // We are assigning a pointer to the interface; the interface
334 // holds the pointer itself.
335 elt->value = rhs_tree;
336 return build_constructor(lhs_type_tree, init);
337 }
338
339 // We are assigning a non-pointer value to the interface; the
340 // interface gets a copy of the value in the heap.
341
342 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
343
344 tree space = gogo->allocate_memory(rhs_type, object_size, location);
345 space = fold_convert_loc(location.gcc_location(),
346 build_pointer_type(TREE_TYPE(rhs_tree)), space);
347 space = save_expr(space);
348
349 tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space);
350 TREE_THIS_NOTRAP(ref) = 1;
351 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
352 void_type_node, ref, rhs_tree);
353
354 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
355 space);
356
357 return build2(COMPOUND_EXPR, lhs_type_tree, set,
358 build_constructor(lhs_type_tree, init));
359 }
360
361 // Return a tree for the type descriptor of RHS_TREE, which has
362 // interface type RHS_TYPE. If RHS_TREE is nil the result will be
363 // NULL.
364
365 tree
366 Expression::get_interface_type_descriptor(Translate_context*,
367 Type* rhs_type, tree rhs_tree,
368 Location location)
369 {
370 tree rhs_type_tree = TREE_TYPE(rhs_tree);
371 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
372 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
373 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
374 NULL_TREE);
375 if (rhs_type->interface_type()->is_empty())
376 {
377 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
378 "__type_descriptor") == 0);
379 return v;
380 }
381
382 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
383 == 0);
384 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
385 v = save_expr(v);
386 tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v);
387 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
388 tree f = TYPE_FIELDS(TREE_TYPE(v1));
389 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
390 == 0);
391 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
392
393 tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node,
394 v, fold_convert_loc(location.gcc_location(),
395 TREE_TYPE(v),
396 null_pointer_node));
397 tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1),
398 null_pointer_node);
399 return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1),
400 eq, n, v1);
401 }
402
403 // Return a tree for the conversion of an interface type to an
404 // interface type.
405
406 tree
407 Expression::convert_interface_to_interface(Translate_context* context,
408 Type *lhs_type, Type *rhs_type,
409 tree rhs_tree, bool for_type_guard,
410 Location location)
411 {
412 Gogo* gogo = context->gogo();
413 Interface_type* lhs_interface_type = lhs_type->interface_type();
414 bool lhs_is_empty = lhs_interface_type->is_empty();
415
416 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
417 if (lhs_type_tree == error_mark_node)
418 return error_mark_node;
419
420 // In the general case this requires runtime examination of the type
421 // method table to match it up with the interface methods.
422
423 // FIXME: If all of the methods in the right hand side interface
424 // also appear in the left hand side interface, then we don't need
425 // to do a runtime check, although we still need to build a new
426 // method table.
427
428 // Get the type descriptor for the right hand side. This will be
429 // NULL for a nil interface.
430
431 if (!DECL_P(rhs_tree))
432 rhs_tree = save_expr(rhs_tree);
433
434 tree rhs_type_descriptor =
435 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
436 location);
437
438 // The result is going to be a two element constructor.
439
440 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
441
442 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
443 tree field = TYPE_FIELDS(lhs_type_tree);
444 elt->index = field;
445
446 if (for_type_guard)
447 {
448 // A type assertion fails when converting a nil interface.
449 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
450 location);
451 static tree assert_interface_decl;
452 tree call = Gogo::call_builtin(&assert_interface_decl,
453 location,
454 "__go_assert_interface",
455 2,
456 ptr_type_node,
457 TREE_TYPE(lhs_type_descriptor),
458 lhs_type_descriptor,
459 TREE_TYPE(rhs_type_descriptor),
460 rhs_type_descriptor);
461 if (call == error_mark_node)
462 return error_mark_node;
463 // This will panic if the interface conversion fails.
464 TREE_NOTHROW(assert_interface_decl) = 0;
465 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
466 call);
467 }
468 else if (lhs_is_empty)
469 {
470 // A convertion to an empty interface always succeeds, and the
471 // first field is just the type descriptor of the object.
472 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
473 "__type_descriptor") == 0);
474 elt->value = fold_convert_loc(location.gcc_location(),
475 TREE_TYPE(field), rhs_type_descriptor);
476 }
477 else
478 {
479 // A conversion to a non-empty interface may fail, but unlike a
480 // type assertion converting nil will always succeed.
481 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
482 == 0);
483 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
484 location);
485 static tree convert_interface_decl;
486 tree call = Gogo::call_builtin(&convert_interface_decl,
487 location,
488 "__go_convert_interface",
489 2,
490 ptr_type_node,
491 TREE_TYPE(lhs_type_descriptor),
492 lhs_type_descriptor,
493 TREE_TYPE(rhs_type_descriptor),
494 rhs_type_descriptor);
495 if (call == error_mark_node)
496 return error_mark_node;
497 // This will panic if the interface conversion fails.
498 TREE_NOTHROW(convert_interface_decl) = 0;
499 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
500 call);
501 }
502
503 // The second field is simply the object pointer.
504
505 elt = VEC_quick_push(constructor_elt, init, NULL);
506 field = DECL_CHAIN(field);
507 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
508 elt->index = field;
509
510 tree rhs_type_tree = TREE_TYPE(rhs_tree);
511 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
512 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
513 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
514 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
515 NULL_TREE);
516
517 return build_constructor(lhs_type_tree, init);
518 }
519
520 // Return a tree for the conversion of an interface type to a
521 // non-interface type.
522
523 tree
524 Expression::convert_interface_to_type(Translate_context* context,
525 Type *lhs_type, Type* rhs_type,
526 tree rhs_tree, Location location)
527 {
528 Gogo* gogo = context->gogo();
529 tree rhs_type_tree = TREE_TYPE(rhs_tree);
530
531 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
532 if (lhs_type_tree == error_mark_node)
533 return error_mark_node;
534
535 // Call a function to check that the type is valid. The function
536 // will panic with an appropriate runtime type error if the type is
537 // not valid.
538
539 tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location);
540
541 if (!DECL_P(rhs_tree))
542 rhs_tree = save_expr(rhs_tree);
543
544 tree rhs_type_descriptor =
545 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
546 location);
547
548 tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo,
549 location);
550
551 static tree check_interface_type_decl;
552 tree call = Gogo::call_builtin(&check_interface_type_decl,
553 location,
554 "__go_check_interface_type",
555 3,
556 void_type_node,
557 TREE_TYPE(lhs_type_descriptor),
558 lhs_type_descriptor,
559 TREE_TYPE(rhs_type_descriptor),
560 rhs_type_descriptor,
561 TREE_TYPE(rhs_inter_descriptor),
562 rhs_inter_descriptor);
563 if (call == error_mark_node)
564 return error_mark_node;
565 // This call will panic if the conversion is invalid.
566 TREE_NOTHROW(check_interface_type_decl) = 0;
567
568 // If the call succeeds, pull out the value.
569 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
570 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
571 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
572 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
573 NULL_TREE);
574
575 // If the value is a pointer, then it is the value we want.
576 // Otherwise it points to the value.
577 if (lhs_type->points_to() == NULL)
578 {
579 val = fold_convert_loc(location.gcc_location(),
580 build_pointer_type(lhs_type_tree), val);
581 val = build_fold_indirect_ref_loc(location.gcc_location(), val);
582 }
583
584 return build2(COMPOUND_EXPR, lhs_type_tree, call,
585 fold_convert_loc(location.gcc_location(), lhs_type_tree, val));
586 }
587
588 // Convert an expression to a tree. This is implemented by the child
589 // class. Not that it is not in general safe to call this multiple
590 // times for a single expression, but that we don't catch such errors.
591
592 tree
593 Expression::get_tree(Translate_context* context)
594 {
595 // The child may have marked this expression as having an error.
596 if (this->classification_ == EXPRESSION_ERROR)
597 return error_mark_node;
598
599 return this->do_get_tree(context);
600 }
601
602 // Return a tree for VAL in TYPE.
603
604 tree
605 Expression::integer_constant_tree(mpz_t val, tree type)
606 {
607 if (type == error_mark_node)
608 return error_mark_node;
609 else if (TREE_CODE(type) == INTEGER_TYPE)
610 return double_int_to_tree(type,
611 mpz_get_double_int(type, val, true));
612 else if (TREE_CODE(type) == REAL_TYPE)
613 {
614 mpfr_t fval;
615 mpfr_init_set_z(fval, val, GMP_RNDN);
616 tree ret = Expression::float_constant_tree(fval, type);
617 mpfr_clear(fval);
618 return ret;
619 }
620 else if (TREE_CODE(type) == COMPLEX_TYPE)
621 {
622 mpfr_t fval;
623 mpfr_init_set_z(fval, val, GMP_RNDN);
624 tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
625 mpfr_clear(fval);
626 tree imag = build_real_from_int_cst(TREE_TYPE(type),
627 integer_zero_node);
628 return build_complex(type, real, imag);
629 }
630 else
631 go_unreachable();
632 }
633
634 // Return a tree for VAL in TYPE.
635
636 tree
637 Expression::float_constant_tree(mpfr_t val, tree type)
638 {
639 if (type == error_mark_node)
640 return error_mark_node;
641 else if (TREE_CODE(type) == INTEGER_TYPE)
642 {
643 mpz_t ival;
644 mpz_init(ival);
645 mpfr_get_z(ival, val, GMP_RNDN);
646 tree ret = Expression::integer_constant_tree(ival, type);
647 mpz_clear(ival);
648 return ret;
649 }
650 else if (TREE_CODE(type) == REAL_TYPE)
651 {
652 REAL_VALUE_TYPE r1;
653 real_from_mpfr(&r1, val, type, GMP_RNDN);
654 REAL_VALUE_TYPE r2;
655 real_convert(&r2, TYPE_MODE(type), &r1);
656 return build_real(type, r2);
657 }
658 else if (TREE_CODE(type) == COMPLEX_TYPE)
659 {
660 REAL_VALUE_TYPE r1;
661 real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
662 REAL_VALUE_TYPE r2;
663 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
664 tree imag = build_real_from_int_cst(TREE_TYPE(type),
665 integer_zero_node);
666 return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
667 }
668 else
669 go_unreachable();
670 }
671
672 // Return a tree for REAL/IMAG in TYPE.
673
674 tree
675 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
676 {
677 if (type == error_mark_node)
678 return error_mark_node;
679 else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
680 return Expression::float_constant_tree(real, type);
681 else if (TREE_CODE(type) == COMPLEX_TYPE)
682 {
683 REAL_VALUE_TYPE r1;
684 real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
685 REAL_VALUE_TYPE r2;
686 real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
687
688 REAL_VALUE_TYPE r3;
689 real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
690 REAL_VALUE_TYPE r4;
691 real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
692
693 return build_complex(type, build_real(TREE_TYPE(type), r2),
694 build_real(TREE_TYPE(type), r4));
695 }
696 else
697 go_unreachable();
698 }
699
700 // Return a tree which evaluates to true if VAL, of arbitrary integer
701 // type, is negative or is more than the maximum value of BOUND_TYPE.
702 // If SOFAR is not NULL, it is or'red into the result. The return
703 // value may be NULL if SOFAR is NULL.
704
705 tree
706 Expression::check_bounds(tree val, tree bound_type, tree sofar,
707 Location loc)
708 {
709 tree val_type = TREE_TYPE(val);
710 tree ret = NULL_TREE;
711
712 if (!TYPE_UNSIGNED(val_type))
713 {
714 ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val,
715 build_int_cst(val_type, 0));
716 if (ret == boolean_false_node)
717 ret = NULL_TREE;
718 }
719
720 HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
721 HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
722 go_assert(val_type_size != -1 && bound_type_size != -1);
723 if (val_type_size > bound_type_size
724 || (val_type_size == bound_type_size
725 && TYPE_UNSIGNED(val_type)
726 && !TYPE_UNSIGNED(bound_type)))
727 {
728 tree max = TYPE_MAX_VALUE(bound_type);
729 tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node,
730 val, fold_convert_loc(loc.gcc_location(),
731 val_type, max));
732 if (big == boolean_false_node)
733 ;
734 else if (ret == NULL_TREE)
735 ret = big;
736 else
737 ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
738 boolean_type_node, ret, big);
739 }
740
741 if (ret == NULL_TREE)
742 return sofar;
743 else if (sofar == NULL_TREE)
744 return ret;
745 else
746 return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node,
747 sofar, ret);
748 }
749
750 void
751 Expression::dump_expression(Ast_dump_context* ast_dump_context) const
752 {
753 this->do_dump_expression(ast_dump_context);
754 }
755
756 // Error expressions. This are used to avoid cascading errors.
757
758 class Error_expression : public Expression
759 {
760 public:
761 Error_expression(Location location)
762 : Expression(EXPRESSION_ERROR, location)
763 { }
764
765 protected:
766 bool
767 do_is_constant() const
768 { return true; }
769
770 bool
771 do_numeric_constant_value(Numeric_constant* nc) const
772 {
773 nc->set_unsigned_long(NULL, 0);
774 return true;
775 }
776
777 void
778 do_discarding_value()
779 { }
780
781 Type*
782 do_type()
783 { return Type::make_error_type(); }
784
785 void
786 do_determine_type(const Type_context*)
787 { }
788
789 Expression*
790 do_copy()
791 { return this; }
792
793 bool
794 do_is_addressable() const
795 { return true; }
796
797 tree
798 do_get_tree(Translate_context*)
799 { return error_mark_node; }
800
801 void
802 do_dump_expression(Ast_dump_context*) const;
803 };
804
805 // Dump the ast representation for an error expression to a dump context.
806
807 void
808 Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
809 {
810 ast_dump_context->ostream() << "_Error_" ;
811 }
812
813 Expression*
814 Expression::make_error(Location location)
815 {
816 return new Error_expression(location);
817 }
818
819 // An expression which is really a type. This is used during parsing.
820 // It is an error if these survive after lowering.
821
822 class
823 Type_expression : public Expression
824 {
825 public:
826 Type_expression(Type* type, Location location)
827 : Expression(EXPRESSION_TYPE, location),
828 type_(type)
829 { }
830
831 protected:
832 int
833 do_traverse(Traverse* traverse)
834 { return Type::traverse(this->type_, traverse); }
835
836 Type*
837 do_type()
838 { return this->type_; }
839
840 void
841 do_determine_type(const Type_context*)
842 { }
843
844 void
845 do_check_types(Gogo*)
846 { this->report_error(_("invalid use of type")); }
847
848 Expression*
849 do_copy()
850 { return this; }
851
852 tree
853 do_get_tree(Translate_context*)
854 { go_unreachable(); }
855
856 void do_dump_expression(Ast_dump_context*) const;
857
858 private:
859 // The type which we are representing as an expression.
860 Type* type_;
861 };
862
863 void
864 Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
865 {
866 ast_dump_context->dump_type(this->type_);
867 }
868
869 Expression*
870 Expression::make_type(Type* type, Location location)
871 {
872 return new Type_expression(type, location);
873 }
874
875 // Class Parser_expression.
876
877 Type*
878 Parser_expression::do_type()
879 {
880 // We should never really ask for the type of a Parser_expression.
881 // However, it can happen, at least when we have an invalid const
882 // whose initializer refers to the const itself. In that case we
883 // may ask for the type when lowering the const itself.
884 go_assert(saw_errors());
885 return Type::make_error_type();
886 }
887
888 // Class Var_expression.
889
890 // Lower a variable expression. Here we just make sure that the
891 // initialization expression of the variable has been lowered. This
892 // ensures that we will be able to determine the type of the variable
893 // if necessary.
894
895 Expression*
896 Var_expression::do_lower(Gogo* gogo, Named_object* function,
897 Statement_inserter* inserter, int)
898 {
899 if (this->variable_->is_variable())
900 {
901 Variable* var = this->variable_->var_value();
902 // This is either a local variable or a global variable. A
903 // reference to a variable which is local to an enclosing
904 // function will be a reference to a field in a closure.
905 if (var->is_global())
906 {
907 function = NULL;
908 inserter = NULL;
909 }
910 var->lower_init_expression(gogo, function, inserter);
911 }
912 return this;
913 }
914
915 // Return the type of a reference to a variable.
916
917 Type*
918 Var_expression::do_type()
919 {
920 if (this->variable_->is_variable())
921 return this->variable_->var_value()->type();
922 else if (this->variable_->is_result_variable())
923 return this->variable_->result_var_value()->type();
924 else
925 go_unreachable();
926 }
927
928 // Determine the type of a reference to a variable.
929
930 void
931 Var_expression::do_determine_type(const Type_context*)
932 {
933 if (this->variable_->is_variable())
934 this->variable_->var_value()->determine_type();
935 }
936
937 // Something takes the address of this variable. This means that we
938 // may want to move the variable onto the heap.
939
940 void
941 Var_expression::do_address_taken(bool escapes)
942 {
943 if (!escapes)
944 {
945 if (this->variable_->is_variable())
946 this->variable_->var_value()->set_non_escaping_address_taken();
947 else if (this->variable_->is_result_variable())
948 this->variable_->result_var_value()->set_non_escaping_address_taken();
949 else
950 go_unreachable();
951 }
952 else
953 {
954 if (this->variable_->is_variable())
955 this->variable_->var_value()->set_address_taken();
956 else if (this->variable_->is_result_variable())
957 this->variable_->result_var_value()->set_address_taken();
958 else
959 go_unreachable();
960 }
961 }
962
963 // Get the tree for a reference to a variable.
964
965 tree
966 Var_expression::do_get_tree(Translate_context* context)
967 {
968 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
969 context->function());
970 tree ret = var_to_tree(bvar);
971 if (ret == error_mark_node)
972 return error_mark_node;
973 bool is_in_heap;
974 if (this->variable_->is_variable())
975 is_in_heap = this->variable_->var_value()->is_in_heap();
976 else if (this->variable_->is_result_variable())
977 is_in_heap = this->variable_->result_var_value()->is_in_heap();
978 else
979 go_unreachable();
980 if (is_in_heap)
981 {
982 ret = build_fold_indirect_ref_loc(this->location().gcc_location(), ret);
983 TREE_THIS_NOTRAP(ret) = 1;
984 }
985 return ret;
986 }
987
988 // Ast dump for variable expression.
989
990 void
991 Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
992 {
993 ast_dump_context->ostream() << this->variable_->name() ;
994 }
995
996 // Make a reference to a variable in an expression.
997
998 Expression*
999 Expression::make_var_reference(Named_object* var, Location location)
1000 {
1001 if (var->is_sink())
1002 return Expression::make_sink(location);
1003
1004 // FIXME: Creating a new object for each reference to a variable is
1005 // wasteful.
1006 return new Var_expression(var, location);
1007 }
1008
1009 // Class Temporary_reference_expression.
1010
1011 // The type.
1012
1013 Type*
1014 Temporary_reference_expression::do_type()
1015 {
1016 return this->statement_->type();
1017 }
1018
1019 // Called if something takes the address of this temporary variable.
1020 // We never have to move temporary variables to the heap, but we do
1021 // need to know that they must live in the stack rather than in a
1022 // register.
1023
1024 void
1025 Temporary_reference_expression::do_address_taken(bool)
1026 {
1027 this->statement_->set_is_address_taken();
1028 }
1029
1030 // Get a tree referring to the variable.
1031
1032 tree
1033 Temporary_reference_expression::do_get_tree(Translate_context* context)
1034 {
1035 Bvariable* bvar = this->statement_->get_backend_variable(context);
1036
1037 // The gcc backend can't represent the same set of recursive types
1038 // that the Go frontend can. In some cases this means that a
1039 // temporary variable won't have the right backend type. Correct
1040 // that here by adding a type cast. We need to use base() to push
1041 // the circularity down one level.
1042 tree ret = var_to_tree(bvar);
1043 if (!this->is_lvalue_
1044 && POINTER_TYPE_P(TREE_TYPE(ret))
1045 && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
1046 {
1047 Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1048 tree type_tree = type_to_tree(type_btype);
1049 ret = fold_convert_loc(this->location().gcc_location(), type_tree, ret);
1050 }
1051 return ret;
1052 }
1053
1054 // Ast dump for temporary reference.
1055
1056 void
1057 Temporary_reference_expression::do_dump_expression(
1058 Ast_dump_context* ast_dump_context) const
1059 {
1060 ast_dump_context->dump_temp_variable_name(this->statement_);
1061 }
1062
1063 // Make a reference to a temporary variable.
1064
1065 Temporary_reference_expression*
1066 Expression::make_temporary_reference(Temporary_statement* statement,
1067 Location location)
1068 {
1069 return new Temporary_reference_expression(statement, location);
1070 }
1071
1072 // Class Set_and_use_temporary_expression.
1073
1074 // Return the type.
1075
1076 Type*
1077 Set_and_use_temporary_expression::do_type()
1078 {
1079 return this->statement_->type();
1080 }
1081
1082 // Take the address.
1083
1084 void
1085 Set_and_use_temporary_expression::do_address_taken(bool)
1086 {
1087 this->statement_->set_is_address_taken();
1088 }
1089
1090 // Return the backend representation.
1091
1092 tree
1093 Set_and_use_temporary_expression::do_get_tree(Translate_context* context)
1094 {
1095 Bvariable* bvar = this->statement_->get_backend_variable(context);
1096 tree var_tree = var_to_tree(bvar);
1097 tree expr_tree = this->expr_->get_tree(context);
1098 if (var_tree == error_mark_node || expr_tree == error_mark_node)
1099 return error_mark_node;
1100 Location loc = this->location();
1101 return build2_loc(loc.gcc_location(), COMPOUND_EXPR, TREE_TYPE(var_tree),
1102 build2_loc(loc.gcc_location(), MODIFY_EXPR, void_type_node,
1103 var_tree, expr_tree),
1104 var_tree);
1105 }
1106
1107 // Dump.
1108
1109 void
1110 Set_and_use_temporary_expression::do_dump_expression(
1111 Ast_dump_context* ast_dump_context) const
1112 {
1113 ast_dump_context->ostream() << '(';
1114 ast_dump_context->dump_temp_variable_name(this->statement_);
1115 ast_dump_context->ostream() << " = ";
1116 this->expr_->dump_expression(ast_dump_context);
1117 ast_dump_context->ostream() << ')';
1118 }
1119
1120 // Make a set-and-use temporary.
1121
1122 Set_and_use_temporary_expression*
1123 Expression::make_set_and_use_temporary(Temporary_statement* statement,
1124 Expression* expr, Location location)
1125 {
1126 return new Set_and_use_temporary_expression(statement, expr, location);
1127 }
1128
1129 // A sink expression--a use of the blank identifier _.
1130
1131 class Sink_expression : public Expression
1132 {
1133 public:
1134 Sink_expression(Location location)
1135 : Expression(EXPRESSION_SINK, location),
1136 type_(NULL), var_(NULL_TREE)
1137 { }
1138
1139 protected:
1140 void
1141 do_discarding_value()
1142 { }
1143
1144 Type*
1145 do_type();
1146
1147 void
1148 do_determine_type(const Type_context*);
1149
1150 Expression*
1151 do_copy()
1152 { return new Sink_expression(this->location()); }
1153
1154 tree
1155 do_get_tree(Translate_context*);
1156
1157 void
1158 do_dump_expression(Ast_dump_context*) const;
1159
1160 private:
1161 // The type of this sink variable.
1162 Type* type_;
1163 // The temporary variable we generate.
1164 tree var_;
1165 };
1166
1167 // Return the type of a sink expression.
1168
1169 Type*
1170 Sink_expression::do_type()
1171 {
1172 if (this->type_ == NULL)
1173 return Type::make_sink_type();
1174 return this->type_;
1175 }
1176
1177 // Determine the type of a sink expression.
1178
1179 void
1180 Sink_expression::do_determine_type(const Type_context* context)
1181 {
1182 if (context->type != NULL)
1183 this->type_ = context->type;
1184 }
1185
1186 // Return a temporary variable for a sink expression. This will
1187 // presumably be a write-only variable which the middle-end will drop.
1188
1189 tree
1190 Sink_expression::do_get_tree(Translate_context* context)
1191 {
1192 if (this->var_ == NULL_TREE)
1193 {
1194 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
1195 Btype* bt = this->type_->get_backend(context->gogo());
1196 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
1197 }
1198 return this->var_;
1199 }
1200
1201 // Ast dump for sink expression.
1202
1203 void
1204 Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1205 {
1206 ast_dump_context->ostream() << "_" ;
1207 }
1208
1209 // Make a sink expression.
1210
1211 Expression*
1212 Expression::make_sink(Location location)
1213 {
1214 return new Sink_expression(location);
1215 }
1216
1217 // Class Func_expression.
1218
1219 // FIXME: Can a function expression appear in a constant expression?
1220 // The value is unchanging. Initializing a constant to the address of
1221 // a function seems like it could work, though there might be little
1222 // point to it.
1223
1224 // Traversal.
1225
1226 int
1227 Func_expression::do_traverse(Traverse* traverse)
1228 {
1229 return (this->closure_ == NULL
1230 ? TRAVERSE_CONTINUE
1231 : Expression::traverse(&this->closure_, traverse));
1232 }
1233
1234 // Return the type of a function expression.
1235
1236 Type*
1237 Func_expression::do_type()
1238 {
1239 if (this->function_->is_function())
1240 return this->function_->func_value()->type();
1241 else if (this->function_->is_function_declaration())
1242 return this->function_->func_declaration_value()->type();
1243 else
1244 go_unreachable();
1245 }
1246
1247 // Get the tree for a function expression without evaluating the
1248 // closure.
1249
1250 tree
1251 Func_expression::get_tree_without_closure(Gogo* gogo)
1252 {
1253 Function_type* fntype;
1254 if (this->function_->is_function())
1255 fntype = this->function_->func_value()->type();
1256 else if (this->function_->is_function_declaration())
1257 fntype = this->function_->func_declaration_value()->type();
1258 else
1259 go_unreachable();
1260
1261 // Builtin functions are handled specially by Call_expression. We
1262 // can't take their address.
1263 if (fntype->is_builtin())
1264 {
1265 error_at(this->location(),
1266 "invalid use of special builtin function %qs; must be called",
1267 this->function_->name().c_str());
1268 return error_mark_node;
1269 }
1270
1271 Named_object* no = this->function_;
1272
1273 tree id = no->get_id(gogo);
1274 if (id == error_mark_node)
1275 return error_mark_node;
1276
1277 tree fndecl;
1278 if (no->is_function())
1279 fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1280 else if (no->is_function_declaration())
1281 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1282 else
1283 go_unreachable();
1284
1285 if (fndecl == error_mark_node)
1286 return error_mark_node;
1287
1288 return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl);
1289 }
1290
1291 // Get the tree for a function expression. This is used when we take
1292 // the address of a function rather than simply calling it. If the
1293 // function has a closure, we must use a trampoline.
1294
1295 tree
1296 Func_expression::do_get_tree(Translate_context* context)
1297 {
1298 Gogo* gogo = context->gogo();
1299
1300 tree fnaddr = this->get_tree_without_closure(gogo);
1301 if (fnaddr == error_mark_node)
1302 return error_mark_node;
1303
1304 go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1305 && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1306 TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1307
1308 // If there is no closure, that is all have to do.
1309 if (this->closure_ == NULL)
1310 return fnaddr;
1311
1312 go_assert(this->function_->func_value()->enclosing() != NULL);
1313
1314 // Get the value of the closure. This will be a pointer to space
1315 // allocated on the heap.
1316 tree closure_tree = this->closure_->get_tree(context);
1317 if (closure_tree == error_mark_node)
1318 return error_mark_node;
1319 go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1320
1321 // Now we need to build some code on the heap. This code will load
1322 // the static chain pointer with the closure and then jump to the
1323 // body of the function. The normal gcc approach is to build the
1324 // code on the stack. Unfortunately we can not do that, as Go
1325 // permits us to return the function pointer.
1326
1327 return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1328 }
1329
1330 // Ast dump for function.
1331
1332 void
1333 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1334 {
1335 ast_dump_context->ostream() << this->function_->name();
1336 if (this->closure_ != NULL)
1337 {
1338 ast_dump_context->ostream() << " {closure = ";
1339 this->closure_->dump_expression(ast_dump_context);
1340 ast_dump_context->ostream() << "}";
1341 }
1342 }
1343
1344 // Make a reference to a function in an expression.
1345
1346 Expression*
1347 Expression::make_func_reference(Named_object* function, Expression* closure,
1348 Location location)
1349 {
1350 return new Func_expression(function, closure, location);
1351 }
1352
1353 // Class Unknown_expression.
1354
1355 // Return the name of an unknown expression.
1356
1357 const std::string&
1358 Unknown_expression::name() const
1359 {
1360 return this->named_object_->name();
1361 }
1362
1363 // Lower a reference to an unknown name.
1364
1365 Expression*
1366 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1367 {
1368 Location location = this->location();
1369 Named_object* no = this->named_object_;
1370 Named_object* real;
1371 if (!no->is_unknown())
1372 real = no;
1373 else
1374 {
1375 real = no->unknown_value()->real_named_object();
1376 if (real == NULL)
1377 {
1378 if (this->is_composite_literal_key_)
1379 return this;
1380 if (!this->no_error_message_)
1381 error_at(location, "reference to undefined name %qs",
1382 this->named_object_->message_name().c_str());
1383 return Expression::make_error(location);
1384 }
1385 }
1386 switch (real->classification())
1387 {
1388 case Named_object::NAMED_OBJECT_CONST:
1389 return Expression::make_const_reference(real, location);
1390 case Named_object::NAMED_OBJECT_TYPE:
1391 return Expression::make_type(real->type_value(), location);
1392 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1393 if (this->is_composite_literal_key_)
1394 return this;
1395 if (!this->no_error_message_)
1396 error_at(location, "reference to undefined type %qs",
1397 real->message_name().c_str());
1398 return Expression::make_error(location);
1399 case Named_object::NAMED_OBJECT_VAR:
1400 real->var_value()->set_is_used();
1401 return Expression::make_var_reference(real, location);
1402 case Named_object::NAMED_OBJECT_FUNC:
1403 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1404 return Expression::make_func_reference(real, NULL, location);
1405 case Named_object::NAMED_OBJECT_PACKAGE:
1406 if (this->is_composite_literal_key_)
1407 return this;
1408 if (!this->no_error_message_)
1409 error_at(location, "unexpected reference to package");
1410 return Expression::make_error(location);
1411 default:
1412 go_unreachable();
1413 }
1414 }
1415
1416 // Dump the ast representation for an unknown expression to a dump context.
1417
1418 void
1419 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1420 {
1421 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1422 << ")";
1423 }
1424
1425 // Make a reference to an unknown name.
1426
1427 Unknown_expression*
1428 Expression::make_unknown_reference(Named_object* no, Location location)
1429 {
1430 return new Unknown_expression(no, location);
1431 }
1432
1433 // A boolean expression.
1434
1435 class Boolean_expression : public Expression
1436 {
1437 public:
1438 Boolean_expression(bool val, Location location)
1439 : Expression(EXPRESSION_BOOLEAN, location),
1440 val_(val), type_(NULL)
1441 { }
1442
1443 static Expression*
1444 do_import(Import*);
1445
1446 protected:
1447 bool
1448 do_is_constant() const
1449 { return true; }
1450
1451 Type*
1452 do_type();
1453
1454 void
1455 do_determine_type(const Type_context*);
1456
1457 Expression*
1458 do_copy()
1459 { return this; }
1460
1461 tree
1462 do_get_tree(Translate_context*)
1463 { return this->val_ ? boolean_true_node : boolean_false_node; }
1464
1465 void
1466 do_export(Export* exp) const
1467 { exp->write_c_string(this->val_ ? "true" : "false"); }
1468
1469 void
1470 do_dump_expression(Ast_dump_context* ast_dump_context) const
1471 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1472
1473 private:
1474 // The constant.
1475 bool val_;
1476 // The type as determined by context.
1477 Type* type_;
1478 };
1479
1480 // Get the type.
1481
1482 Type*
1483 Boolean_expression::do_type()
1484 {
1485 if (this->type_ == NULL)
1486 this->type_ = Type::make_boolean_type();
1487 return this->type_;
1488 }
1489
1490 // Set the type from the context.
1491
1492 void
1493 Boolean_expression::do_determine_type(const Type_context* context)
1494 {
1495 if (this->type_ != NULL && !this->type_->is_abstract())
1496 ;
1497 else if (context->type != NULL && context->type->is_boolean_type())
1498 this->type_ = context->type;
1499 else if (!context->may_be_abstract)
1500 this->type_ = Type::lookup_bool_type();
1501 }
1502
1503 // Import a boolean constant.
1504
1505 Expression*
1506 Boolean_expression::do_import(Import* imp)
1507 {
1508 if (imp->peek_char() == 't')
1509 {
1510 imp->require_c_string("true");
1511 return Expression::make_boolean(true, imp->location());
1512 }
1513 else
1514 {
1515 imp->require_c_string("false");
1516 return Expression::make_boolean(false, imp->location());
1517 }
1518 }
1519
1520 // Make a boolean expression.
1521
1522 Expression*
1523 Expression::make_boolean(bool val, Location location)
1524 {
1525 return new Boolean_expression(val, location);
1526 }
1527
1528 // Class String_expression.
1529
1530 // Get the type.
1531
1532 Type*
1533 String_expression::do_type()
1534 {
1535 if (this->type_ == NULL)
1536 this->type_ = Type::make_string_type();
1537 return this->type_;
1538 }
1539
1540 // Set the type from the context.
1541
1542 void
1543 String_expression::do_determine_type(const Type_context* context)
1544 {
1545 if (this->type_ != NULL && !this->type_->is_abstract())
1546 ;
1547 else if (context->type != NULL && context->type->is_string_type())
1548 this->type_ = context->type;
1549 else if (!context->may_be_abstract)
1550 this->type_ = Type::lookup_string_type();
1551 }
1552
1553 // Build a string constant.
1554
1555 tree
1556 String_expression::do_get_tree(Translate_context* context)
1557 {
1558 return context->gogo()->go_string_constant_tree(this->val_);
1559 }
1560
1561 // Write string literal to string dump.
1562
1563 void
1564 String_expression::export_string(String_dump* exp,
1565 const String_expression* str)
1566 {
1567 std::string s;
1568 s.reserve(str->val_.length() * 4 + 2);
1569 s += '"';
1570 for (std::string::const_iterator p = str->val_.begin();
1571 p != str->val_.end();
1572 ++p)
1573 {
1574 if (*p == '\\' || *p == '"')
1575 {
1576 s += '\\';
1577 s += *p;
1578 }
1579 else if (*p >= 0x20 && *p < 0x7f)
1580 s += *p;
1581 else if (*p == '\n')
1582 s += "\\n";
1583 else if (*p == '\t')
1584 s += "\\t";
1585 else
1586 {
1587 s += "\\x";
1588 unsigned char c = *p;
1589 unsigned int dig = c >> 4;
1590 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1591 dig = c & 0xf;
1592 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1593 }
1594 }
1595 s += '"';
1596 exp->write_string(s);
1597 }
1598
1599 // Export a string expression.
1600
1601 void
1602 String_expression::do_export(Export* exp) const
1603 {
1604 String_expression::export_string(exp, this);
1605 }
1606
1607 // Import a string expression.
1608
1609 Expression*
1610 String_expression::do_import(Import* imp)
1611 {
1612 imp->require_c_string("\"");
1613 std::string val;
1614 while (true)
1615 {
1616 int c = imp->get_char();
1617 if (c == '"' || c == -1)
1618 break;
1619 if (c != '\\')
1620 val += static_cast<char>(c);
1621 else
1622 {
1623 c = imp->get_char();
1624 if (c == '\\' || c == '"')
1625 val += static_cast<char>(c);
1626 else if (c == 'n')
1627 val += '\n';
1628 else if (c == 't')
1629 val += '\t';
1630 else if (c == 'x')
1631 {
1632 c = imp->get_char();
1633 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1634 c = imp->get_char();
1635 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1636 char v = (vh << 4) | vl;
1637 val += v;
1638 }
1639 else
1640 {
1641 error_at(imp->location(), "bad string constant");
1642 return Expression::make_error(imp->location());
1643 }
1644 }
1645 }
1646 return Expression::make_string(val, imp->location());
1647 }
1648
1649 // Ast dump for string expression.
1650
1651 void
1652 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1653 {
1654 String_expression::export_string(ast_dump_context, this);
1655 }
1656
1657 // Make a string expression.
1658
1659 Expression*
1660 Expression::make_string(const std::string& val, Location location)
1661 {
1662 return new String_expression(val, location);
1663 }
1664
1665 // Make an integer expression.
1666
1667 class Integer_expression : public Expression
1668 {
1669 public:
1670 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
1671 Location location)
1672 : Expression(EXPRESSION_INTEGER, location),
1673 type_(type), is_character_constant_(is_character_constant)
1674 { mpz_init_set(this->val_, *val); }
1675
1676 static Expression*
1677 do_import(Import*);
1678
1679 // Write VAL to string dump.
1680 static void
1681 export_integer(String_dump* exp, const mpz_t val);
1682
1683 // Write VAL to dump context.
1684 static void
1685 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1686
1687 protected:
1688 bool
1689 do_is_constant() const
1690 { return true; }
1691
1692 bool
1693 do_numeric_constant_value(Numeric_constant* nc) const;
1694
1695 Type*
1696 do_type();
1697
1698 void
1699 do_determine_type(const Type_context* context);
1700
1701 void
1702 do_check_types(Gogo*);
1703
1704 tree
1705 do_get_tree(Translate_context*);
1706
1707 Expression*
1708 do_copy()
1709 {
1710 if (this->is_character_constant_)
1711 return Expression::make_character(&this->val_, this->type_,
1712 this->location());
1713 else
1714 return Expression::make_integer(&this->val_, this->type_,
1715 this->location());
1716 }
1717
1718 void
1719 do_export(Export*) const;
1720
1721 void
1722 do_dump_expression(Ast_dump_context*) const;
1723
1724 private:
1725 // The integer value.
1726 mpz_t val_;
1727 // The type so far.
1728 Type* type_;
1729 // Whether this is a character constant.
1730 bool is_character_constant_;
1731 };
1732
1733 // Return a numeric constant for this expression. We have to mark
1734 // this as a character when appropriate.
1735
1736 bool
1737 Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const
1738 {
1739 if (this->is_character_constant_)
1740 nc->set_rune(this->type_, this->val_);
1741 else
1742 nc->set_int(this->type_, this->val_);
1743 return true;
1744 }
1745
1746 // Return the current type. If we haven't set the type yet, we return
1747 // an abstract integer type.
1748
1749 Type*
1750 Integer_expression::do_type()
1751 {
1752 if (this->type_ == NULL)
1753 {
1754 if (this->is_character_constant_)
1755 this->type_ = Type::make_abstract_character_type();
1756 else
1757 this->type_ = Type::make_abstract_integer_type();
1758 }
1759 return this->type_;
1760 }
1761
1762 // Set the type of the integer value. Here we may switch from an
1763 // abstract type to a real type.
1764
1765 void
1766 Integer_expression::do_determine_type(const Type_context* context)
1767 {
1768 if (this->type_ != NULL && !this->type_->is_abstract())
1769 ;
1770 else if (context->type != NULL && context->type->is_numeric_type())
1771 this->type_ = context->type;
1772 else if (!context->may_be_abstract)
1773 {
1774 if (this->is_character_constant_)
1775 this->type_ = Type::lookup_integer_type("int32");
1776 else
1777 this->type_ = Type::lookup_integer_type("int");
1778 }
1779 }
1780
1781 // Check the type of an integer constant.
1782
1783 void
1784 Integer_expression::do_check_types(Gogo*)
1785 {
1786 Type* type = this->type_;
1787 if (type == NULL)
1788 return;
1789 Numeric_constant nc;
1790 if (this->is_character_constant_)
1791 nc.set_rune(NULL, this->val_);
1792 else
1793 nc.set_int(NULL, this->val_);
1794 if (!nc.set_type(type, true, this->location()))
1795 this->set_is_error();
1796 }
1797
1798 // Get a tree for an integer constant.
1799
1800 tree
1801 Integer_expression::do_get_tree(Translate_context* context)
1802 {
1803 Gogo* gogo = context->gogo();
1804 tree type;
1805 if (this->type_ != NULL && !this->type_->is_abstract())
1806 type = type_to_tree(this->type_->get_backend(gogo));
1807 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1808 {
1809 // We are converting to an abstract floating point type.
1810 Type* ftype = Type::lookup_float_type("float64");
1811 type = type_to_tree(ftype->get_backend(gogo));
1812 }
1813 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1814 {
1815 // We are converting to an abstract complex type.
1816 Type* ctype = Type::lookup_complex_type("complex128");
1817 type = type_to_tree(ctype->get_backend(gogo));
1818 }
1819 else
1820 {
1821 // If we still have an abstract type here, then this is being
1822 // used in a constant expression which didn't get reduced for
1823 // some reason. Use a type which will fit the value. We use <,
1824 // not <=, because we need an extra bit for the sign bit.
1825 int bits = mpz_sizeinbase(this->val_, 2);
1826 if (bits < INT_TYPE_SIZE)
1827 {
1828 Type* t = Type::lookup_integer_type("int");
1829 type = type_to_tree(t->get_backend(gogo));
1830 }
1831 else if (bits < 64)
1832 {
1833 Type* t = Type::lookup_integer_type("int64");
1834 type = type_to_tree(t->get_backend(gogo));
1835 }
1836 else
1837 type = long_long_integer_type_node;
1838 }
1839 return Expression::integer_constant_tree(this->val_, type);
1840 }
1841
1842 // Write VAL to export data.
1843
1844 void
1845 Integer_expression::export_integer(String_dump* exp, const mpz_t val)
1846 {
1847 char* s = mpz_get_str(NULL, 10, val);
1848 exp->write_c_string(s);
1849 free(s);
1850 }
1851
1852 // Export an integer in a constant expression.
1853
1854 void
1855 Integer_expression::do_export(Export* exp) const
1856 {
1857 Integer_expression::export_integer(exp, this->val_);
1858 if (this->is_character_constant_)
1859 exp->write_c_string("'");
1860 // A trailing space lets us reliably identify the end of the number.
1861 exp->write_c_string(" ");
1862 }
1863
1864 // Import an integer, floating point, or complex value. This handles
1865 // all these types because they all start with digits.
1866
1867 Expression*
1868 Integer_expression::do_import(Import* imp)
1869 {
1870 std::string num = imp->read_identifier();
1871 imp->require_c_string(" ");
1872 if (!num.empty() && num[num.length() - 1] == 'i')
1873 {
1874 mpfr_t real;
1875 size_t plus_pos = num.find('+', 1);
1876 size_t minus_pos = num.find('-', 1);
1877 size_t pos;
1878 if (plus_pos == std::string::npos)
1879 pos = minus_pos;
1880 else if (minus_pos == std::string::npos)
1881 pos = plus_pos;
1882 else
1883 {
1884 error_at(imp->location(), "bad number in import data: %qs",
1885 num.c_str());
1886 return Expression::make_error(imp->location());
1887 }
1888 if (pos == std::string::npos)
1889 mpfr_set_ui(real, 0, GMP_RNDN);
1890 else
1891 {
1892 std::string real_str = num.substr(0, pos);
1893 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1894 {
1895 error_at(imp->location(), "bad number in import data: %qs",
1896 real_str.c_str());
1897 return Expression::make_error(imp->location());
1898 }
1899 }
1900
1901 std::string imag_str;
1902 if (pos == std::string::npos)
1903 imag_str = num;
1904 else
1905 imag_str = num.substr(pos);
1906 imag_str = imag_str.substr(0, imag_str.size() - 1);
1907 mpfr_t imag;
1908 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1909 {
1910 error_at(imp->location(), "bad number in import data: %qs",
1911 imag_str.c_str());
1912 return Expression::make_error(imp->location());
1913 }
1914 Expression* ret = Expression::make_complex(&real, &imag, NULL,
1915 imp->location());
1916 mpfr_clear(real);
1917 mpfr_clear(imag);
1918 return ret;
1919 }
1920 else if (num.find('.') == std::string::npos
1921 && num.find('E') == std::string::npos)
1922 {
1923 bool is_character_constant = (!num.empty()
1924 && num[num.length() - 1] == '\'');
1925 if (is_character_constant)
1926 num = num.substr(0, num.length() - 1);
1927 mpz_t val;
1928 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1929 {
1930 error_at(imp->location(), "bad number in import data: %qs",
1931 num.c_str());
1932 return Expression::make_error(imp->location());
1933 }
1934 Expression* ret;
1935 if (is_character_constant)
1936 ret = Expression::make_character(&val, NULL, imp->location());
1937 else
1938 ret = Expression::make_integer(&val, NULL, imp->location());
1939 mpz_clear(val);
1940 return ret;
1941 }
1942 else
1943 {
1944 mpfr_t val;
1945 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1946 {
1947 error_at(imp->location(), "bad number in import data: %qs",
1948 num.c_str());
1949 return Expression::make_error(imp->location());
1950 }
1951 Expression* ret = Expression::make_float(&val, NULL, imp->location());
1952 mpfr_clear(val);
1953 return ret;
1954 }
1955 }
1956 // Ast dump for integer expression.
1957
1958 void
1959 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1960 {
1961 if (this->is_character_constant_)
1962 ast_dump_context->ostream() << '\'';
1963 Integer_expression::export_integer(ast_dump_context, this->val_);
1964 if (this->is_character_constant_)
1965 ast_dump_context->ostream() << '\'';
1966 }
1967
1968 // Build a new integer value.
1969
1970 Expression*
1971 Expression::make_integer(const mpz_t* val, Type* type, Location location)
1972 {
1973 return new Integer_expression(val, type, false, location);
1974 }
1975
1976 // Build a new character constant value.
1977
1978 Expression*
1979 Expression::make_character(const mpz_t* val, Type* type, Location location)
1980 {
1981 return new Integer_expression(val, type, true, location);
1982 }
1983
1984 // Floats.
1985
1986 class Float_expression : public Expression
1987 {
1988 public:
1989 Float_expression(const mpfr_t* val, Type* type, Location location)
1990 : Expression(EXPRESSION_FLOAT, location),
1991 type_(type)
1992 {
1993 mpfr_init_set(this->val_, *val, GMP_RNDN);
1994 }
1995
1996 // Write VAL to export data.
1997 static void
1998 export_float(String_dump* exp, const mpfr_t val);
1999
2000 // Write VAL to dump file.
2001 static void
2002 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
2003
2004 protected:
2005 bool
2006 do_is_constant() const
2007 { return true; }
2008
2009 bool
2010 do_numeric_constant_value(Numeric_constant* nc) const
2011 {
2012 nc->set_float(this->type_, this->val_);
2013 return true;
2014 }
2015
2016 Type*
2017 do_type();
2018
2019 void
2020 do_determine_type(const Type_context*);
2021
2022 void
2023 do_check_types(Gogo*);
2024
2025 Expression*
2026 do_copy()
2027 { return Expression::make_float(&this->val_, this->type_,
2028 this->location()); }
2029
2030 tree
2031 do_get_tree(Translate_context*);
2032
2033 void
2034 do_export(Export*) const;
2035
2036 void
2037 do_dump_expression(Ast_dump_context*) const;
2038
2039 private:
2040 // The floating point value.
2041 mpfr_t val_;
2042 // The type so far.
2043 Type* type_;
2044 };
2045
2046 // Return the current type. If we haven't set the type yet, we return
2047 // an abstract float type.
2048
2049 Type*
2050 Float_expression::do_type()
2051 {
2052 if (this->type_ == NULL)
2053 this->type_ = Type::make_abstract_float_type();
2054 return this->type_;
2055 }
2056
2057 // Set the type of the float value. Here we may switch from an
2058 // abstract type to a real type.
2059
2060 void
2061 Float_expression::do_determine_type(const Type_context* context)
2062 {
2063 if (this->type_ != NULL && !this->type_->is_abstract())
2064 ;
2065 else if (context->type != NULL
2066 && (context->type->integer_type() != NULL
2067 || context->type->float_type() != NULL
2068 || context->type->complex_type() != NULL))
2069 this->type_ = context->type;
2070 else if (!context->may_be_abstract)
2071 this->type_ = Type::lookup_float_type("float64");
2072 }
2073
2074 // Check the type of a float value.
2075
2076 void
2077 Float_expression::do_check_types(Gogo*)
2078 {
2079 Type* type = this->type_;
2080 if (type == NULL)
2081 return;
2082 Numeric_constant nc;
2083 nc.set_float(NULL, this->val_);
2084 if (!nc.set_type(this->type_, true, this->location()))
2085 this->set_is_error();
2086 }
2087
2088 // Get a tree for a float constant.
2089
2090 tree
2091 Float_expression::do_get_tree(Translate_context* context)
2092 {
2093 Gogo* gogo = context->gogo();
2094 tree type;
2095 if (this->type_ != NULL && !this->type_->is_abstract())
2096 type = type_to_tree(this->type_->get_backend(gogo));
2097 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2098 {
2099 // We have an abstract integer type. We just hope for the best.
2100 type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2101 }
2102 else
2103 {
2104 // If we still have an abstract type here, then this is being
2105 // used in a constant expression which didn't get reduced. We
2106 // just use float64 and hope for the best.
2107 Type* ft = Type::lookup_float_type("float64");
2108 type = type_to_tree(ft->get_backend(gogo));
2109 }
2110 return Expression::float_constant_tree(this->val_, type);
2111 }
2112
2113 // Write a floating point number to a string dump.
2114
2115 void
2116 Float_expression::export_float(String_dump *exp, const mpfr_t val)
2117 {
2118 mp_exp_t exponent;
2119 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2120 if (*s == '-')
2121 exp->write_c_string("-");
2122 exp->write_c_string("0.");
2123 exp->write_c_string(*s == '-' ? s + 1 : s);
2124 mpfr_free_str(s);
2125 char buf[30];
2126 snprintf(buf, sizeof buf, "E%ld", exponent);
2127 exp->write_c_string(buf);
2128 }
2129
2130 // Export a floating point number in a constant expression.
2131
2132 void
2133 Float_expression::do_export(Export* exp) const
2134 {
2135 Float_expression::export_float(exp, this->val_);
2136 // A trailing space lets us reliably identify the end of the number.
2137 exp->write_c_string(" ");
2138 }
2139
2140 // Dump a floating point number to the dump file.
2141
2142 void
2143 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2144 {
2145 Float_expression::export_float(ast_dump_context, this->val_);
2146 }
2147
2148 // Make a float expression.
2149
2150 Expression*
2151 Expression::make_float(const mpfr_t* val, Type* type, Location location)
2152 {
2153 return new Float_expression(val, type, location);
2154 }
2155
2156 // Complex numbers.
2157
2158 class Complex_expression : public Expression
2159 {
2160 public:
2161 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2162 Location location)
2163 : Expression(EXPRESSION_COMPLEX, location),
2164 type_(type)
2165 {
2166 mpfr_init_set(this->real_, *real, GMP_RNDN);
2167 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2168 }
2169
2170 // Write REAL/IMAG to string dump.
2171 static void
2172 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
2173
2174 // Write REAL/IMAG to dump context.
2175 static void
2176 dump_complex(Ast_dump_context* ast_dump_context,
2177 const mpfr_t real, const mpfr_t val);
2178
2179 protected:
2180 bool
2181 do_is_constant() const
2182 { return true; }
2183
2184 bool
2185 do_numeric_constant_value(Numeric_constant* nc) const
2186 {
2187 nc->set_complex(this->type_, this->real_, this->imag_);
2188 return true;
2189 }
2190
2191 Type*
2192 do_type();
2193
2194 void
2195 do_determine_type(const Type_context*);
2196
2197 void
2198 do_check_types(Gogo*);
2199
2200 Expression*
2201 do_copy()
2202 {
2203 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2204 this->location());
2205 }
2206
2207 tree
2208 do_get_tree(Translate_context*);
2209
2210 void
2211 do_export(Export*) const;
2212
2213 void
2214 do_dump_expression(Ast_dump_context*) const;
2215
2216 private:
2217 // The real part.
2218 mpfr_t real_;
2219 // The imaginary part;
2220 mpfr_t imag_;
2221 // The type if known.
2222 Type* type_;
2223 };
2224
2225 // Return the current type. If we haven't set the type yet, we return
2226 // an abstract complex type.
2227
2228 Type*
2229 Complex_expression::do_type()
2230 {
2231 if (this->type_ == NULL)
2232 this->type_ = Type::make_abstract_complex_type();
2233 return this->type_;
2234 }
2235
2236 // Set the type of the complex value. Here we may switch from an
2237 // abstract type to a real type.
2238
2239 void
2240 Complex_expression::do_determine_type(const Type_context* context)
2241 {
2242 if (this->type_ != NULL && !this->type_->is_abstract())
2243 ;
2244 else if (context->type != NULL
2245 && context->type->complex_type() != NULL)
2246 this->type_ = context->type;
2247 else if (!context->may_be_abstract)
2248 this->type_ = Type::lookup_complex_type("complex128");
2249 }
2250
2251 // Check the type of a complex value.
2252
2253 void
2254 Complex_expression::do_check_types(Gogo*)
2255 {
2256 Type* type = this->type_;
2257 if (type == NULL)
2258 return;
2259 Numeric_constant nc;
2260 nc.set_complex(NULL, this->real_, this->imag_);
2261 if (!nc.set_type(this->type_, true, this->location()))
2262 this->set_is_error();
2263 }
2264
2265 // Get a tree for a complex constant.
2266
2267 tree
2268 Complex_expression::do_get_tree(Translate_context* context)
2269 {
2270 Gogo* gogo = context->gogo();
2271 tree type;
2272 if (this->type_ != NULL && !this->type_->is_abstract())
2273 type = type_to_tree(this->type_->get_backend(gogo));
2274 else
2275 {
2276 // If we still have an abstract type here, this this is being
2277 // used in a constant expression which didn't get reduced. We
2278 // just use complex128 and hope for the best.
2279 Type* ct = Type::lookup_complex_type("complex128");
2280 type = type_to_tree(ct->get_backend(gogo));
2281 }
2282 return Expression::complex_constant_tree(this->real_, this->imag_, type);
2283 }
2284
2285 // Write REAL/IMAG to export data.
2286
2287 void
2288 Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
2289 const mpfr_t imag)
2290 {
2291 if (!mpfr_zero_p(real))
2292 {
2293 Float_expression::export_float(exp, real);
2294 if (mpfr_sgn(imag) > 0)
2295 exp->write_c_string("+");
2296 }
2297 Float_expression::export_float(exp, imag);
2298 exp->write_c_string("i");
2299 }
2300
2301 // Export a complex number in a constant expression.
2302
2303 void
2304 Complex_expression::do_export(Export* exp) const
2305 {
2306 Complex_expression::export_complex(exp, this->real_, this->imag_);
2307 // A trailing space lets us reliably identify the end of the number.
2308 exp->write_c_string(" ");
2309 }
2310
2311 // Dump a complex expression to the dump file.
2312
2313 void
2314 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2315 {
2316 Complex_expression::export_complex(ast_dump_context,
2317 this->real_,
2318 this->imag_);
2319 }
2320
2321 // Make a complex expression.
2322
2323 Expression*
2324 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2325 Location location)
2326 {
2327 return new Complex_expression(real, imag, type, location);
2328 }
2329
2330 // Find a named object in an expression.
2331
2332 class Find_named_object : public Traverse
2333 {
2334 public:
2335 Find_named_object(Named_object* no)
2336 : Traverse(traverse_expressions),
2337 no_(no), found_(false)
2338 { }
2339
2340 // Whether we found the object.
2341 bool
2342 found() const
2343 { return this->found_; }
2344
2345 protected:
2346 int
2347 expression(Expression**);
2348
2349 private:
2350 // The object we are looking for.
2351 Named_object* no_;
2352 // Whether we found it.
2353 bool found_;
2354 };
2355
2356 // A reference to a const in an expression.
2357
2358 class Const_expression : public Expression
2359 {
2360 public:
2361 Const_expression(Named_object* constant, Location location)
2362 : Expression(EXPRESSION_CONST_REFERENCE, location),
2363 constant_(constant), type_(NULL), seen_(false)
2364 { }
2365
2366 Named_object*
2367 named_object()
2368 { return this->constant_; }
2369
2370 // Check that the initializer does not refer to the constant itself.
2371 void
2372 check_for_init_loop();
2373
2374 protected:
2375 int
2376 do_traverse(Traverse*);
2377
2378 Expression*
2379 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2380
2381 bool
2382 do_is_constant() const
2383 { return true; }
2384
2385 bool
2386 do_numeric_constant_value(Numeric_constant* nc) const;
2387
2388 bool
2389 do_string_constant_value(std::string* val) const;
2390
2391 Type*
2392 do_type();
2393
2394 // The type of a const is set by the declaration, not the use.
2395 void
2396 do_determine_type(const Type_context*);
2397
2398 void
2399 do_check_types(Gogo*);
2400
2401 Expression*
2402 do_copy()
2403 { return this; }
2404
2405 tree
2406 do_get_tree(Translate_context* context);
2407
2408 // When exporting a reference to a const as part of a const
2409 // expression, we export the value. We ignore the fact that it has
2410 // a name.
2411 void
2412 do_export(Export* exp) const
2413 { this->constant_->const_value()->expr()->export_expression(exp); }
2414
2415 void
2416 do_dump_expression(Ast_dump_context*) const;
2417
2418 private:
2419 // The constant.
2420 Named_object* constant_;
2421 // The type of this reference. This is used if the constant has an
2422 // abstract type.
2423 Type* type_;
2424 // Used to prevent infinite recursion when a constant incorrectly
2425 // refers to itself.
2426 mutable bool seen_;
2427 };
2428
2429 // Traversal.
2430
2431 int
2432 Const_expression::do_traverse(Traverse* traverse)
2433 {
2434 if (this->type_ != NULL)
2435 return Type::traverse(this->type_, traverse);
2436 return TRAVERSE_CONTINUE;
2437 }
2438
2439 // Lower a constant expression. This is where we convert the
2440 // predeclared constant iota into an integer value.
2441
2442 Expression*
2443 Const_expression::do_lower(Gogo* gogo, Named_object*,
2444 Statement_inserter*, int iota_value)
2445 {
2446 if (this->constant_->const_value()->expr()->classification()
2447 == EXPRESSION_IOTA)
2448 {
2449 if (iota_value == -1)
2450 {
2451 error_at(this->location(),
2452 "iota is only defined in const declarations");
2453 iota_value = 0;
2454 }
2455 mpz_t val;
2456 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2457 Expression* ret = Expression::make_integer(&val, NULL,
2458 this->location());
2459 mpz_clear(val);
2460 return ret;
2461 }
2462
2463 // Make sure that the constant itself has been lowered.
2464 gogo->lower_constant(this->constant_);
2465
2466 return this;
2467 }
2468
2469 // Return a numeric constant value.
2470
2471 bool
2472 Const_expression::do_numeric_constant_value(Numeric_constant* nc) const
2473 {
2474 if (this->seen_)
2475 return false;
2476
2477 Expression* e = this->constant_->const_value()->expr();
2478
2479 this->seen_ = true;
2480
2481 bool r = e->numeric_constant_value(nc);
2482
2483 this->seen_ = false;
2484
2485 Type* ctype;
2486 if (this->type_ != NULL)
2487 ctype = this->type_;
2488 else
2489 ctype = this->constant_->const_value()->type();
2490 if (r && ctype != NULL)
2491 {
2492 if (!nc->set_type(ctype, false, this->location()))
2493 return false;
2494 }
2495
2496 return r;
2497 }
2498
2499 bool
2500 Const_expression::do_string_constant_value(std::string* val) const
2501 {
2502 if (this->seen_)
2503 return false;
2504
2505 Expression* e = this->constant_->const_value()->expr();
2506
2507 this->seen_ = true;
2508 bool ok = e->string_constant_value(val);
2509 this->seen_ = false;
2510
2511 return ok;
2512 }
2513
2514 // Return the type of the const reference.
2515
2516 Type*
2517 Const_expression::do_type()
2518 {
2519 if (this->type_ != NULL)
2520 return this->type_;
2521
2522 Named_constant* nc = this->constant_->const_value();
2523
2524 if (this->seen_ || nc->lowering())
2525 {
2526 this->report_error(_("constant refers to itself"));
2527 this->type_ = Type::make_error_type();
2528 return this->type_;
2529 }
2530
2531 this->seen_ = true;
2532
2533 Type* ret = nc->type();
2534
2535 if (ret != NULL)
2536 {
2537 this->seen_ = false;
2538 return ret;
2539 }
2540
2541 // During parsing, a named constant may have a NULL type, but we
2542 // must not return a NULL type here.
2543 ret = nc->expr()->type();
2544
2545 this->seen_ = false;
2546
2547 return ret;
2548 }
2549
2550 // Set the type of the const reference.
2551
2552 void
2553 Const_expression::do_determine_type(const Type_context* context)
2554 {
2555 Type* ctype = this->constant_->const_value()->type();
2556 Type* cetype = (ctype != NULL
2557 ? ctype
2558 : this->constant_->const_value()->expr()->type());
2559 if (ctype != NULL && !ctype->is_abstract())
2560 ;
2561 else if (context->type != NULL
2562 && context->type->is_numeric_type()
2563 && cetype->is_numeric_type())
2564 this->type_ = context->type;
2565 else if (context->type != NULL
2566 && context->type->is_string_type()
2567 && cetype->is_string_type())
2568 this->type_ = context->type;
2569 else if (context->type != NULL
2570 && context->type->is_boolean_type()
2571 && cetype->is_boolean_type())
2572 this->type_ = context->type;
2573 else if (!context->may_be_abstract)
2574 {
2575 if (cetype->is_abstract())
2576 cetype = cetype->make_non_abstract_type();
2577 this->type_ = cetype;
2578 }
2579 }
2580
2581 // Check for a loop in which the initializer of a constant refers to
2582 // the constant itself.
2583
2584 void
2585 Const_expression::check_for_init_loop()
2586 {
2587 if (this->type_ != NULL && this->type_->is_error())
2588 return;
2589
2590 if (this->seen_)
2591 {
2592 this->report_error(_("constant refers to itself"));
2593 this->type_ = Type::make_error_type();
2594 return;
2595 }
2596
2597 Expression* init = this->constant_->const_value()->expr();
2598 Find_named_object find_named_object(this->constant_);
2599
2600 this->seen_ = true;
2601 Expression::traverse(&init, &find_named_object);
2602 this->seen_ = false;
2603
2604 if (find_named_object.found())
2605 {
2606 if (this->type_ == NULL || !this->type_->is_error())
2607 {
2608 this->report_error(_("constant refers to itself"));
2609 this->type_ = Type::make_error_type();
2610 }
2611 return;
2612 }
2613 }
2614
2615 // Check types of a const reference.
2616
2617 void
2618 Const_expression::do_check_types(Gogo*)
2619 {
2620 if (this->type_ != NULL && this->type_->is_error())
2621 return;
2622
2623 this->check_for_init_loop();
2624
2625 // Check that numeric constant fits in type.
2626 if (this->type_ != NULL && this->type_->is_numeric_type())
2627 {
2628 Numeric_constant nc;
2629 if (this->constant_->const_value()->expr()->numeric_constant_value(&nc))
2630 {
2631 if (!nc.set_type(this->type_, true, this->location()))
2632 this->set_is_error();
2633 }
2634 }
2635 }
2636
2637 // Return a tree for the const reference.
2638
2639 tree
2640 Const_expression::do_get_tree(Translate_context* context)
2641 {
2642 Gogo* gogo = context->gogo();
2643 tree type_tree;
2644 if (this->type_ == NULL)
2645 type_tree = NULL_TREE;
2646 else
2647 {
2648 type_tree = type_to_tree(this->type_->get_backend(gogo));
2649 if (type_tree == error_mark_node)
2650 return error_mark_node;
2651 }
2652
2653 // If the type has been set for this expression, but the underlying
2654 // object is an abstract int or float, we try to get the abstract
2655 // value. Otherwise we may lose something in the conversion.
2656 if (this->type_ != NULL
2657 && this->type_->is_numeric_type()
2658 && (this->constant_->const_value()->type() == NULL
2659 || this->constant_->const_value()->type()->is_abstract()))
2660 {
2661 Expression* expr = this->constant_->const_value()->expr();
2662 Numeric_constant nc;
2663 if (expr->numeric_constant_value(&nc)
2664 && nc.set_type(this->type_, false, this->location()))
2665 {
2666 Expression* e = nc.expression(this->location());
2667 return e->get_tree(context);
2668 }
2669 }
2670
2671 tree const_tree = this->constant_->get_tree(gogo, context->function());
2672 if (this->type_ == NULL
2673 || const_tree == error_mark_node
2674 || TREE_TYPE(const_tree) == error_mark_node)
2675 return const_tree;
2676
2677 tree ret;
2678 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2679 ret = fold_convert(type_tree, const_tree);
2680 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2681 ret = fold(convert_to_integer(type_tree, const_tree));
2682 else if (TREE_CODE(type_tree) == REAL_TYPE)
2683 ret = fold(convert_to_real(type_tree, const_tree));
2684 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2685 ret = fold(convert_to_complex(type_tree, const_tree));
2686 else
2687 go_unreachable();
2688 return ret;
2689 }
2690
2691 // Dump ast representation for constant expression.
2692
2693 void
2694 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2695 {
2696 ast_dump_context->ostream() << this->constant_->name();
2697 }
2698
2699 // Make a reference to a constant in an expression.
2700
2701 Expression*
2702 Expression::make_const_reference(Named_object* constant,
2703 Location location)
2704 {
2705 return new Const_expression(constant, location);
2706 }
2707
2708 // Find a named object in an expression.
2709
2710 int
2711 Find_named_object::expression(Expression** pexpr)
2712 {
2713 switch ((*pexpr)->classification())
2714 {
2715 case Expression::EXPRESSION_CONST_REFERENCE:
2716 {
2717 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2718 if (ce->named_object() == this->no_)
2719 break;
2720
2721 // We need to check a constant initializer explicitly, as
2722 // loops here will not be caught by the loop checking for
2723 // variable initializers.
2724 ce->check_for_init_loop();
2725
2726 return TRAVERSE_CONTINUE;
2727 }
2728
2729 case Expression::EXPRESSION_VAR_REFERENCE:
2730 if ((*pexpr)->var_expression()->named_object() == this->no_)
2731 break;
2732 return TRAVERSE_CONTINUE;
2733 case Expression::EXPRESSION_FUNC_REFERENCE:
2734 if ((*pexpr)->func_expression()->named_object() == this->no_)
2735 break;
2736 return TRAVERSE_CONTINUE;
2737 default:
2738 return TRAVERSE_CONTINUE;
2739 }
2740 this->found_ = true;
2741 return TRAVERSE_EXIT;
2742 }
2743
2744 // The nil value.
2745
2746 class Nil_expression : public Expression
2747 {
2748 public:
2749 Nil_expression(Location location)
2750 : Expression(EXPRESSION_NIL, location)
2751 { }
2752
2753 static Expression*
2754 do_import(Import*);
2755
2756 protected:
2757 bool
2758 do_is_constant() const
2759 { return true; }
2760
2761 Type*
2762 do_type()
2763 { return Type::make_nil_type(); }
2764
2765 void
2766 do_determine_type(const Type_context*)
2767 { }
2768
2769 Expression*
2770 do_copy()
2771 { return this; }
2772
2773 tree
2774 do_get_tree(Translate_context*)
2775 { return null_pointer_node; }
2776
2777 void
2778 do_export(Export* exp) const
2779 { exp->write_c_string("nil"); }
2780
2781 void
2782 do_dump_expression(Ast_dump_context* ast_dump_context) const
2783 { ast_dump_context->ostream() << "nil"; }
2784 };
2785
2786 // Import a nil expression.
2787
2788 Expression*
2789 Nil_expression::do_import(Import* imp)
2790 {
2791 imp->require_c_string("nil");
2792 return Expression::make_nil(imp->location());
2793 }
2794
2795 // Make a nil expression.
2796
2797 Expression*
2798 Expression::make_nil(Location location)
2799 {
2800 return new Nil_expression(location);
2801 }
2802
2803 // The value of the predeclared constant iota. This is little more
2804 // than a marker. This will be lowered to an integer in
2805 // Const_expression::do_lower, which is where we know the value that
2806 // it should have.
2807
2808 class Iota_expression : public Parser_expression
2809 {
2810 public:
2811 Iota_expression(Location location)
2812 : Parser_expression(EXPRESSION_IOTA, location)
2813 { }
2814
2815 protected:
2816 Expression*
2817 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
2818 { go_unreachable(); }
2819
2820 // There should only ever be one of these.
2821 Expression*
2822 do_copy()
2823 { go_unreachable(); }
2824
2825 void
2826 do_dump_expression(Ast_dump_context* ast_dump_context) const
2827 { ast_dump_context->ostream() << "iota"; }
2828 };
2829
2830 // Make an iota expression. This is only called for one case: the
2831 // value of the predeclared constant iota.
2832
2833 Expression*
2834 Expression::make_iota()
2835 {
2836 static Iota_expression iota_expression(Linemap::unknown_location());
2837 return &iota_expression;
2838 }
2839
2840 // A type conversion expression.
2841
2842 class Type_conversion_expression : public Expression
2843 {
2844 public:
2845 Type_conversion_expression(Type* type, Expression* expr,
2846 Location location)
2847 : Expression(EXPRESSION_CONVERSION, location),
2848 type_(type), expr_(expr), may_convert_function_types_(false)
2849 { }
2850
2851 // Return the type to which we are converting.
2852 Type*
2853 type() const
2854 { return this->type_; }
2855
2856 // Return the expression which we are converting.
2857 Expression*
2858 expr() const
2859 { return this->expr_; }
2860
2861 // Permit converting from one function type to another. This is
2862 // used internally for method expressions.
2863 void
2864 set_may_convert_function_types()
2865 {
2866 this->may_convert_function_types_ = true;
2867 }
2868
2869 // Import a type conversion expression.
2870 static Expression*
2871 do_import(Import*);
2872
2873 protected:
2874 int
2875 do_traverse(Traverse* traverse);
2876
2877 Expression*
2878 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2879
2880 bool
2881 do_is_constant() const
2882 { return this->expr_->is_constant(); }
2883
2884 bool
2885 do_numeric_constant_value(Numeric_constant*) const;
2886
2887 bool
2888 do_string_constant_value(std::string*) const;
2889
2890 Type*
2891 do_type()
2892 { return this->type_; }
2893
2894 void
2895 do_determine_type(const Type_context*)
2896 {
2897 Type_context subcontext(this->type_, false);
2898 this->expr_->determine_type(&subcontext);
2899 }
2900
2901 void
2902 do_check_types(Gogo*);
2903
2904 Expression*
2905 do_copy()
2906 {
2907 return new Type_conversion_expression(this->type_, this->expr_->copy(),
2908 this->location());
2909 }
2910
2911 tree
2912 do_get_tree(Translate_context* context);
2913
2914 void
2915 do_export(Export*) const;
2916
2917 void
2918 do_dump_expression(Ast_dump_context*) const;
2919
2920 private:
2921 // The type to convert to.
2922 Type* type_;
2923 // The expression to convert.
2924 Expression* expr_;
2925 // True if this is permitted to convert function types. This is
2926 // used internally for method expressions.
2927 bool may_convert_function_types_;
2928 };
2929
2930 // Traversal.
2931
2932 int
2933 Type_conversion_expression::do_traverse(Traverse* traverse)
2934 {
2935 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
2936 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
2937 return TRAVERSE_EXIT;
2938 return TRAVERSE_CONTINUE;
2939 }
2940
2941 // Convert to a constant at lowering time.
2942
2943 Expression*
2944 Type_conversion_expression::do_lower(Gogo*, Named_object*,
2945 Statement_inserter*, int)
2946 {
2947 Type* type = this->type_;
2948 Expression* val = this->expr_;
2949 Location location = this->location();
2950
2951 if (type->is_numeric_type())
2952 {
2953 Numeric_constant nc;
2954 if (val->numeric_constant_value(&nc))
2955 {
2956 if (!nc.set_type(type, true, location))
2957 return Expression::make_error(location);
2958 return nc.expression(location);
2959 }
2960 }
2961
2962 if (type->is_slice_type())
2963 {
2964 Type* element_type = type->array_type()->element_type()->forwarded();
2965 bool is_byte = (element_type->integer_type() != NULL
2966 && element_type->integer_type()->is_byte());
2967 bool is_rune = (element_type->integer_type() != NULL
2968 && element_type->integer_type()->is_rune());
2969 if (is_byte || is_rune)
2970 {
2971 std::string s;
2972 if (val->string_constant_value(&s))
2973 {
2974 Expression_list* vals = new Expression_list();
2975 if (is_byte)
2976 {
2977 for (std::string::const_iterator p = s.begin();
2978 p != s.end();
2979 p++)
2980 {
2981 mpz_t val;
2982 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
2983 Expression* v = Expression::make_integer(&val,
2984 element_type,
2985 location);
2986 vals->push_back(v);
2987 mpz_clear(val);
2988 }
2989 }
2990 else
2991 {
2992 const char *p = s.data();
2993 const char *pend = s.data() + s.length();
2994 while (p < pend)
2995 {
2996 unsigned int c;
2997 int adv = Lex::fetch_char(p, &c);
2998 if (adv == 0)
2999 {
3000 warning_at(this->location(), 0,
3001 "invalid UTF-8 encoding");
3002 adv = 1;
3003 }
3004 p += adv;
3005 mpz_t val;
3006 mpz_init_set_ui(val, c);
3007 Expression* v = Expression::make_integer(&val,
3008 element_type,
3009 location);
3010 vals->push_back(v);
3011 mpz_clear(val);
3012 }
3013 }
3014
3015 return Expression::make_slice_composite_literal(type, vals,
3016 location);
3017 }
3018 }
3019 }
3020
3021 return this;
3022 }
3023
3024 // Return the constant numeric value if there is one.
3025
3026 bool
3027 Type_conversion_expression::do_numeric_constant_value(
3028 Numeric_constant* nc) const
3029 {
3030 if (!this->type_->is_numeric_type())
3031 return false;
3032 if (!this->expr_->numeric_constant_value(nc))
3033 return false;
3034 return nc->set_type(this->type_, false, this->location());
3035 }
3036
3037 // Return the constant string value if there is one.
3038
3039 bool
3040 Type_conversion_expression::do_string_constant_value(std::string* val) const
3041 {
3042 if (this->type_->is_string_type()
3043 && this->expr_->type()->integer_type() != NULL)
3044 {
3045 Numeric_constant nc;
3046 if (this->expr_->numeric_constant_value(&nc))
3047 {
3048 unsigned long ival;
3049 if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID)
3050 {
3051 val->clear();
3052 Lex::append_char(ival, true, val, this->location());
3053 return true;
3054 }
3055 }
3056 }
3057
3058 // FIXME: Could handle conversion from const []int here.
3059
3060 return false;
3061 }
3062
3063 // Check that types are convertible.
3064
3065 void
3066 Type_conversion_expression::do_check_types(Gogo*)
3067 {
3068 Type* type = this->type_;
3069 Type* expr_type = this->expr_->type();
3070 std::string reason;
3071
3072 if (type->is_error() || expr_type->is_error())
3073 {
3074 this->set_is_error();
3075 return;
3076 }
3077
3078 if (this->may_convert_function_types_
3079 && type->function_type() != NULL
3080 && expr_type->function_type() != NULL)
3081 return;
3082
3083 if (Type::are_convertible(type, expr_type, &reason))
3084 return;
3085
3086 error_at(this->location(), "%s", reason.c_str());
3087 this->set_is_error();
3088 }
3089
3090 // Get a tree for a type conversion.
3091
3092 tree
3093 Type_conversion_expression::do_get_tree(Translate_context* context)
3094 {
3095 Gogo* gogo = context->gogo();
3096 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
3097 tree expr_tree = this->expr_->get_tree(context);
3098
3099 if (type_tree == error_mark_node
3100 || expr_tree == error_mark_node
3101 || TREE_TYPE(expr_tree) == error_mark_node)
3102 return error_mark_node;
3103
3104 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3105 return fold_convert(type_tree, expr_tree);
3106
3107 Type* type = this->type_;
3108 Type* expr_type = this->expr_->type();
3109 tree ret;
3110 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3111 ret = Expression::convert_for_assignment(context, type, expr_type,
3112 expr_tree, this->location());
3113 else if (type->integer_type() != NULL)
3114 {
3115 if (expr_type->integer_type() != NULL
3116 || expr_type->float_type() != NULL
3117 || expr_type->is_unsafe_pointer_type())
3118 ret = fold(convert_to_integer(type_tree, expr_tree));
3119 else
3120 go_unreachable();
3121 }
3122 else if (type->float_type() != NULL)
3123 {
3124 if (expr_type->integer_type() != NULL
3125 || expr_type->float_type() != NULL)
3126 ret = fold(convert_to_real(type_tree, expr_tree));
3127 else
3128 go_unreachable();
3129 }
3130 else if (type->complex_type() != NULL)
3131 {
3132 if (expr_type->complex_type() != NULL)
3133 ret = fold(convert_to_complex(type_tree, expr_tree));
3134 else
3135 go_unreachable();
3136 }
3137 else if (type->is_string_type()
3138 && expr_type->integer_type() != NULL)
3139 {
3140 expr_tree = fold_convert(integer_type_node, expr_tree);
3141 if (host_integerp(expr_tree, 0))
3142 {
3143 HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3144 std::string s;
3145 Lex::append_char(intval, true, &s, this->location());
3146 Expression* se = Expression::make_string(s, this->location());
3147 return se->get_tree(context);
3148 }
3149
3150 static tree int_to_string_fndecl;
3151 ret = Gogo::call_builtin(&int_to_string_fndecl,
3152 this->location(),
3153 "__go_int_to_string",
3154 1,
3155 type_tree,
3156 integer_type_node,
3157 fold_convert(integer_type_node, expr_tree));
3158 }
3159 else if (type->is_string_type() && expr_type->is_slice_type())
3160 {
3161 if (!DECL_P(expr_tree))
3162 expr_tree = save_expr(expr_tree);
3163 Array_type* a = expr_type->array_type();
3164 Type* e = a->element_type()->forwarded();
3165 go_assert(e->integer_type() != NULL);
3166 tree valptr = fold_convert(const_ptr_type_node,
3167 a->value_pointer_tree(gogo, expr_tree));
3168 tree len = a->length_tree(gogo, expr_tree);
3169 len = fold_convert_loc(this->location().gcc_location(), integer_type_node,
3170 len);
3171 if (e->integer_type()->is_byte())
3172 {
3173 static tree byte_array_to_string_fndecl;
3174 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3175 this->location(),
3176 "__go_byte_array_to_string",
3177 2,
3178 type_tree,
3179 const_ptr_type_node,
3180 valptr,
3181 integer_type_node,
3182 len);
3183 }
3184 else
3185 {
3186 go_assert(e->integer_type()->is_rune());
3187 static tree int_array_to_string_fndecl;
3188 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3189 this->location(),
3190 "__go_int_array_to_string",
3191 2,
3192 type_tree,
3193 const_ptr_type_node,
3194 valptr,
3195 integer_type_node,
3196 len);
3197 }
3198 }
3199 else if (type->is_slice_type() && expr_type->is_string_type())
3200 {
3201 Type* e = type->array_type()->element_type()->forwarded();
3202 go_assert(e->integer_type() != NULL);
3203 if (e->integer_type()->is_byte())
3204 {
3205 tree string_to_byte_array_fndecl = NULL_TREE;
3206 ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3207 this->location(),
3208 "__go_string_to_byte_array",
3209 1,
3210 type_tree,
3211 TREE_TYPE(expr_tree),
3212 expr_tree);
3213 }
3214 else
3215 {
3216 go_assert(e->integer_type()->is_rune());
3217 tree string_to_int_array_fndecl = NULL_TREE;
3218 ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3219 this->location(),
3220 "__go_string_to_int_array",
3221 1,
3222 type_tree,
3223 TREE_TYPE(expr_tree),
3224 expr_tree);
3225 }
3226 }
3227 else if ((type->is_unsafe_pointer_type()
3228 && expr_type->points_to() != NULL)
3229 || (expr_type->is_unsafe_pointer_type()
3230 && type->points_to() != NULL))
3231 ret = fold_convert(type_tree, expr_tree);
3232 else if (type->is_unsafe_pointer_type()
3233 && expr_type->integer_type() != NULL)
3234 ret = convert_to_pointer(type_tree, expr_tree);
3235 else if (this->may_convert_function_types_
3236 && type->function_type() != NULL
3237 && expr_type->function_type() != NULL)
3238 ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3239 expr_tree);
3240 else
3241 ret = Expression::convert_for_assignment(context, type, expr_type,
3242 expr_tree, this->location());
3243
3244 return ret;
3245 }
3246
3247 // Output a type conversion in a constant expression.
3248
3249 void
3250 Type_conversion_expression::do_export(Export* exp) const
3251 {
3252 exp->write_c_string("convert(");
3253 exp->write_type(this->type_);
3254 exp->write_c_string(", ");
3255 this->expr_->export_expression(exp);
3256 exp->write_c_string(")");
3257 }
3258
3259 // Import a type conversion or a struct construction.
3260
3261 Expression*
3262 Type_conversion_expression::do_import(Import* imp)
3263 {
3264 imp->require_c_string("convert(");
3265 Type* type = imp->read_type();
3266 imp->require_c_string(", ");
3267 Expression* val = Expression::import_expression(imp);
3268 imp->require_c_string(")");
3269 return Expression::make_cast(type, val, imp->location());
3270 }
3271
3272 // Dump ast representation for a type conversion expression.
3273
3274 void
3275 Type_conversion_expression::do_dump_expression(
3276 Ast_dump_context* ast_dump_context) const
3277 {
3278 ast_dump_context->dump_type(this->type_);
3279 ast_dump_context->ostream() << "(";
3280 ast_dump_context->dump_expression(this->expr_);
3281 ast_dump_context->ostream() << ") ";
3282 }
3283
3284 // Make a type cast expression.
3285
3286 Expression*
3287 Expression::make_cast(Type* type, Expression* val, Location location)
3288 {
3289 if (type->is_error_type() || val->is_error_expression())
3290 return Expression::make_error(location);
3291 return new Type_conversion_expression(type, val, location);
3292 }
3293
3294 // An unsafe type conversion, used to pass values to builtin functions.
3295
3296 class Unsafe_type_conversion_expression : public Expression
3297 {
3298 public:
3299 Unsafe_type_conversion_expression(Type* type, Expression* expr,
3300 Location location)
3301 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3302 type_(type), expr_(expr)
3303 { }
3304
3305 protected:
3306 int
3307 do_traverse(Traverse* traverse);
3308
3309 Type*
3310 do_type()
3311 { return this->type_; }
3312
3313 void
3314 do_determine_type(const Type_context*)
3315 { this->expr_->determine_type_no_context(); }
3316
3317 Expression*
3318 do_copy()
3319 {
3320 return new Unsafe_type_conversion_expression(this->type_,
3321 this->expr_->copy(),
3322 this->location());
3323 }
3324
3325 tree
3326 do_get_tree(Translate_context*);
3327
3328 void
3329 do_dump_expression(Ast_dump_context*) const;
3330
3331 private:
3332 // The type to convert to.
3333 Type* type_;
3334 // The expression to convert.
3335 Expression* expr_;
3336 };
3337
3338 // Traversal.
3339
3340 int
3341 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3342 {
3343 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3344 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3345 return TRAVERSE_EXIT;
3346 return TRAVERSE_CONTINUE;
3347 }
3348
3349 // Convert to backend representation.
3350
3351 tree
3352 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3353 {
3354 // We are only called for a limited number of cases.
3355
3356 Type* t = this->type_;
3357 Type* et = this->expr_->type();
3358
3359 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
3360 tree expr_tree = this->expr_->get_tree(context);
3361 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3362 return error_mark_node;
3363
3364 Location loc = this->location();
3365
3366 bool use_view_convert = false;
3367 if (t->is_slice_type())
3368 {
3369 go_assert(et->is_slice_type());
3370 use_view_convert = true;
3371 }
3372 else if (t->map_type() != NULL)
3373 go_assert(et->map_type() != NULL);
3374 else if (t->channel_type() != NULL)
3375 go_assert(et->channel_type() != NULL);
3376 else if (t->points_to() != NULL)
3377 go_assert(et->points_to() != NULL || et->is_nil_type());
3378 else if (et->is_unsafe_pointer_type())
3379 go_assert(t->points_to() != NULL);
3380 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3381 {
3382 go_assert(et->interface_type() != NULL
3383 && !et->interface_type()->is_empty());
3384 use_view_convert = true;
3385 }
3386 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3387 {
3388 go_assert(et->interface_type() != NULL
3389 && et->interface_type()->is_empty());
3390 use_view_convert = true;
3391 }
3392 else if (t->integer_type() != NULL)
3393 {
3394 go_assert(et->is_boolean_type()
3395 || et->integer_type() != NULL
3396 || et->function_type() != NULL
3397 || et->points_to() != NULL
3398 || et->map_type() != NULL
3399 || et->channel_type() != NULL);
3400 return convert_to_integer(type_tree, expr_tree);
3401 }
3402 else
3403 go_unreachable();
3404
3405 if (use_view_convert)
3406 return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3407 expr_tree);
3408 else
3409 return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
3410 }
3411
3412 // Dump ast representation for an unsafe type conversion expression.
3413
3414 void
3415 Unsafe_type_conversion_expression::do_dump_expression(
3416 Ast_dump_context* ast_dump_context) const
3417 {
3418 ast_dump_context->dump_type(this->type_);
3419 ast_dump_context->ostream() << "(";
3420 ast_dump_context->dump_expression(this->expr_);
3421 ast_dump_context->ostream() << ") ";
3422 }
3423
3424 // Make an unsafe type conversion expression.
3425
3426 Expression*
3427 Expression::make_unsafe_cast(Type* type, Expression* expr,
3428 Location location)
3429 {
3430 return new Unsafe_type_conversion_expression(type, expr, location);
3431 }
3432
3433 // Unary expressions.
3434
3435 class Unary_expression : public Expression
3436 {
3437 public:
3438 Unary_expression(Operator op, Expression* expr, Location location)
3439 : Expression(EXPRESSION_UNARY, location),
3440 op_(op), escapes_(true), create_temp_(false), expr_(expr)
3441 { }
3442
3443 // Return the operator.
3444 Operator
3445 op() const
3446 { return this->op_; }
3447
3448 // Return the operand.
3449 Expression*
3450 operand() const
3451 { return this->expr_; }
3452
3453 // Record that an address expression does not escape.
3454 void
3455 set_does_not_escape()
3456 {
3457 go_assert(this->op_ == OPERATOR_AND);
3458 this->escapes_ = false;
3459 }
3460
3461 // Record that this is an address expression which should create a
3462 // temporary variable if necessary. This is used for method calls.
3463 void
3464 set_create_temp()
3465 {
3466 go_assert(this->op_ == OPERATOR_AND);
3467 this->create_temp_ = true;
3468 }
3469
3470 // Apply unary opcode OP to UNC, setting NC. Return true if this
3471 // could be done, false if not. Issue errors for overflow.
3472 static bool
3473 eval_constant(Operator op, const Numeric_constant* unc,
3474 Location, Numeric_constant* nc);
3475
3476 static Expression*
3477 do_import(Import*);
3478
3479 protected:
3480 int
3481 do_traverse(Traverse* traverse)
3482 { return Expression::traverse(&this->expr_, traverse); }
3483
3484 Expression*
3485 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3486
3487 bool
3488 do_is_constant() const;
3489
3490 bool
3491 do_numeric_constant_value(Numeric_constant*) const;
3492
3493 Type*
3494 do_type();
3495
3496 void
3497 do_determine_type(const Type_context*);
3498
3499 void
3500 do_check_types(Gogo*);
3501
3502 Expression*
3503 do_copy()
3504 {
3505 return Expression::make_unary(this->op_, this->expr_->copy(),
3506 this->location());
3507 }
3508
3509 bool
3510 do_must_eval_subexpressions_in_order(int*) const
3511 { return this->op_ == OPERATOR_MULT; }
3512
3513 bool
3514 do_is_addressable() const
3515 { return this->op_ == OPERATOR_MULT; }
3516
3517 tree
3518 do_get_tree(Translate_context*);
3519
3520 void
3521 do_export(Export*) const;
3522
3523 void
3524 do_dump_expression(Ast_dump_context*) const;
3525
3526 private:
3527 // The unary operator to apply.
3528 Operator op_;
3529 // Normally true. False if this is an address expression which does
3530 // not escape the current function.
3531 bool escapes_;
3532 // True if this is an address expression which should create a
3533 // temporary variable if necessary.
3534 bool create_temp_;
3535 // The operand.
3536 Expression* expr_;
3537 };
3538
3539 // If we are taking the address of a composite literal, and the
3540 // contents are not constant, then we want to make a heap composite
3541 // instead.
3542
3543 Expression*
3544 Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
3545 {
3546 Location loc = this->location();
3547 Operator op = this->op_;
3548 Expression* expr = this->expr_;
3549
3550 if (op == OPERATOR_MULT && expr->is_type_expression())
3551 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3552
3553 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3554 // moving x to the heap. FIXME: Is it worth doing a real escape
3555 // analysis here? This case is found in math/unsafe.go and is
3556 // therefore worth special casing.
3557 if (op == OPERATOR_MULT)
3558 {
3559 Expression* e = expr;
3560 while (e->classification() == EXPRESSION_CONVERSION)
3561 {
3562 Type_conversion_expression* te
3563 = static_cast<Type_conversion_expression*>(e);
3564 e = te->expr();
3565 }
3566
3567 if (e->classification() == EXPRESSION_UNARY)
3568 {
3569 Unary_expression* ue = static_cast<Unary_expression*>(e);
3570 if (ue->op_ == OPERATOR_AND)
3571 {
3572 if (e == expr)
3573 {
3574 // *&x == x.
3575 return ue->expr_;
3576 }
3577 ue->set_does_not_escape();
3578 }
3579 }
3580 }
3581
3582 // Catching an invalid indirection of unsafe.Pointer here avoid
3583 // having to deal with TYPE_VOID in other places.
3584 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3585 {
3586 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3587 return Expression::make_error(this->location());
3588 }
3589
3590 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR)
3591 {
3592 Numeric_constant nc;
3593 if (expr->numeric_constant_value(&nc))
3594 {
3595 Numeric_constant result;
3596 if (Unary_expression::eval_constant(op, &nc, loc, &result))
3597 return result.expression(loc);
3598 }
3599 }
3600
3601 return this;
3602 }
3603
3604 // Return whether a unary expression is a constant.
3605
3606 bool
3607 Unary_expression::do_is_constant() const
3608 {
3609 if (this->op_ == OPERATOR_MULT)
3610 {
3611 // Indirecting through a pointer is only constant if the object
3612 // to which the expression points is constant, but we currently
3613 // have no way to determine that.
3614 return false;
3615 }
3616 else if (this->op_ == OPERATOR_AND)
3617 {
3618 // Taking the address of a variable is constant if it is a
3619 // global variable, not constant otherwise. In other cases
3620 // taking the address is probably not a constant.
3621 Var_expression* ve = this->expr_->var_expression();
3622 if (ve != NULL)
3623 {
3624 Named_object* no = ve->named_object();
3625 return no->is_variable() && no->var_value()->is_global();
3626 }
3627 return false;
3628 }
3629 else
3630 return this->expr_->is_constant();
3631 }
3632
3633 // Apply unary opcode OP to UNC, setting NC. Return true if this
3634 // could be done, false if not. Issue errors for overflow.
3635
3636 bool
3637 Unary_expression::eval_constant(Operator op, const Numeric_constant* unc,
3638 Location location, Numeric_constant* nc)
3639 {
3640 switch (op)
3641 {
3642 case OPERATOR_PLUS:
3643 *nc = *unc;
3644 return true;
3645
3646 case OPERATOR_MINUS:
3647 if (unc->is_int() || unc->is_rune())
3648 break;
3649 else if (unc->is_float())
3650 {
3651 mpfr_t uval;
3652 unc->get_float(&uval);
3653 mpfr_t val;
3654 mpfr_init(val);
3655 mpfr_neg(val, uval, GMP_RNDN);
3656 nc->set_float(unc->type(), val);
3657 mpfr_clear(uval);
3658 mpfr_clear(val);
3659 return true;
3660 }
3661 else if (unc->is_complex())
3662 {
3663 mpfr_t ureal, uimag;
3664 unc->get_complex(&ureal, &uimag);
3665 mpfr_t real, imag;
3666 mpfr_init(real);
3667 mpfr_init(imag);
3668 mpfr_neg(real, ureal, GMP_RNDN);
3669 mpfr_neg(imag, uimag, GMP_RNDN);
3670 nc->set_complex(unc->type(), real, imag);
3671 mpfr_clear(ureal);
3672 mpfr_clear(uimag);
3673 mpfr_clear(real);
3674 mpfr_clear(imag);
3675 return true;
3676 }
3677 else
3678 go_unreachable();
3679
3680 case OPERATOR_XOR:
3681 break;
3682
3683 case OPERATOR_NOT:
3684 case OPERATOR_AND:
3685 case OPERATOR_MULT:
3686 return false;
3687
3688 default:
3689 go_unreachable();
3690 }
3691
3692 if (!unc->is_int() && !unc->is_rune())
3693 return false;
3694
3695 mpz_t uval;
3696 if (unc->is_rune())
3697 unc->get_rune(&uval);
3698 else
3699 unc->get_int(&uval);
3700 mpz_t val;
3701 mpz_init(val);
3702
3703 switch (op)
3704 {
3705 case OPERATOR_MINUS:
3706 mpz_neg(val, uval);
3707 break;
3708
3709 case OPERATOR_NOT:
3710 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3711 break;
3712
3713 case OPERATOR_XOR:
3714 {
3715 Type* utype = unc->type();
3716 if (utype->integer_type() == NULL
3717 || utype->integer_type()->is_abstract())
3718 mpz_com(val, uval);
3719 else
3720 {
3721 // The number of HOST_WIDE_INTs that it takes to represent
3722 // UVAL.
3723 size_t count = ((mpz_sizeinbase(uval, 2)
3724 + HOST_BITS_PER_WIDE_INT
3725 - 1)
3726 / HOST_BITS_PER_WIDE_INT);
3727
3728 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3729 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3730
3731 size_t obits = utype->integer_type()->bits();
3732
3733 if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0)
3734 {
3735 mpz_t adj;
3736 mpz_init_set_ui(adj, 1);
3737 mpz_mul_2exp(adj, adj, obits);
3738 mpz_add(uval, uval, adj);
3739 mpz_clear(adj);
3740 }
3741
3742 size_t ecount;
3743 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3744 go_assert(ecount <= count);
3745
3746 // Trim down to the number of words required by the type.
3747 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3748 / HOST_BITS_PER_WIDE_INT);
3749 go_assert(ocount <= count);
3750
3751 for (size_t i = 0; i < ocount; ++i)
3752 phwi[i] = ~phwi[i];
3753
3754 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3755 if (clearbits != 0)
3756 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3757 >> clearbits);
3758
3759 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3760
3761 if (!utype->integer_type()->is_unsigned()
3762 && mpz_tstbit(val, obits - 1))
3763 {
3764 mpz_t adj;
3765 mpz_init_set_ui(adj, 1);
3766 mpz_mul_2exp(adj, adj, obits);
3767 mpz_sub(val, val, adj);
3768 mpz_clear(adj);
3769 }
3770
3771 delete[] phwi;
3772 }
3773 }
3774 break;
3775
3776 default:
3777 go_unreachable();
3778 }
3779
3780 if (unc->is_rune())
3781 nc->set_rune(NULL, val);
3782 else
3783 nc->set_int(NULL, val);
3784
3785 mpz_clear(uval);
3786 mpz_clear(val);
3787
3788 return nc->set_type(unc->type(), true, location);
3789 }
3790
3791 // Return the integral constant value of a unary expression, if it has one.
3792
3793 bool
3794 Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const
3795 {
3796 Numeric_constant unc;
3797 if (!this->expr_->numeric_constant_value(&unc))
3798 return false;
3799 return Unary_expression::eval_constant(this->op_, &unc, this->location(),
3800 nc);
3801 }
3802
3803 // Return the type of a unary expression.
3804
3805 Type*
3806 Unary_expression::do_type()
3807 {
3808 switch (this->op_)
3809 {
3810 case OPERATOR_PLUS:
3811 case OPERATOR_MINUS:
3812 case OPERATOR_NOT:
3813 case OPERATOR_XOR:
3814 return this->expr_->type();
3815
3816 case OPERATOR_AND:
3817 return Type::make_pointer_type(this->expr_->type());
3818
3819 case OPERATOR_MULT:
3820 {
3821 Type* subtype = this->expr_->type();
3822 Type* points_to = subtype->points_to();
3823 if (points_to == NULL)
3824 return Type::make_error_type();
3825 return points_to;
3826 }
3827
3828 default:
3829 go_unreachable();
3830 }
3831 }
3832
3833 // Determine abstract types for a unary expression.
3834
3835 void
3836 Unary_expression::do_determine_type(const Type_context* context)
3837 {
3838 switch (this->op_)
3839 {
3840 case OPERATOR_PLUS:
3841 case OPERATOR_MINUS:
3842 case OPERATOR_NOT:
3843 case OPERATOR_XOR:
3844 this->expr_->determine_type(context);
3845 break;
3846
3847 case OPERATOR_AND:
3848 // Taking the address of something.
3849 {
3850 Type* subtype = (context->type == NULL
3851 ? NULL
3852 : context->type->points_to());
3853 Type_context subcontext(subtype, false);
3854 this->expr_->determine_type(&subcontext);
3855 }
3856 break;
3857
3858 case OPERATOR_MULT:
3859 // Indirecting through a pointer.
3860 {
3861 Type* subtype = (context->type == NULL
3862 ? NULL
3863 : Type::make_pointer_type(context->type));
3864 Type_context subcontext(subtype, false);
3865 this->expr_->determine_type(&subcontext);
3866 }
3867 break;
3868
3869 default:
3870 go_unreachable();
3871 }
3872 }
3873
3874 // Check types for a unary expression.
3875
3876 void
3877 Unary_expression::do_check_types(Gogo*)
3878 {
3879 Type* type = this->expr_->type();
3880 if (type->is_error())
3881 {
3882 this->set_is_error();
3883 return;
3884 }
3885
3886 switch (this->op_)
3887 {
3888 case OPERATOR_PLUS:
3889 case OPERATOR_MINUS:
3890 if (type->integer_type() == NULL
3891 && type->float_type() == NULL
3892 && type->complex_type() == NULL)
3893 this->report_error(_("expected numeric type"));
3894 break;
3895
3896 case OPERATOR_NOT:
3897 if (!type->is_boolean_type())
3898 this->report_error(_("expected boolean type"));
3899 break;
3900
3901 case OPERATOR_XOR:
3902 if (type->integer_type() == NULL
3903 && !type->is_boolean_type())
3904 this->report_error(_("expected integer or boolean type"));
3905 break;
3906
3907 case OPERATOR_AND:
3908 if (!this->expr_->is_addressable())
3909 {
3910 if (!this->create_temp_)
3911 this->report_error(_("invalid operand for unary %<&%>"));
3912 }
3913 else
3914 this->expr_->address_taken(this->escapes_);
3915 break;
3916
3917 case OPERATOR_MULT:
3918 // Indirecting through a pointer.
3919 if (type->points_to() == NULL)
3920 this->report_error(_("expected pointer"));
3921 break;
3922
3923 default:
3924 go_unreachable();
3925 }
3926 }
3927
3928 // Get a tree for a unary expression.
3929
3930 tree
3931 Unary_expression::do_get_tree(Translate_context* context)
3932 {
3933 Location loc = this->location();
3934
3935 // Taking the address of a set-and-use-temporary expression requires
3936 // setting the temporary and then taking the address.
3937 if (this->op_ == OPERATOR_AND)
3938 {
3939 Set_and_use_temporary_expression* sut =
3940 this->expr_->set_and_use_temporary_expression();
3941 if (sut != NULL)
3942 {
3943 Temporary_statement* temp = sut->temporary();
3944 Bvariable* bvar = temp->get_backend_variable(context);
3945 tree var_tree = var_to_tree(bvar);
3946 Expression* val = sut->expression();
3947 tree val_tree = val->get_tree(context);
3948 if (var_tree == error_mark_node || val_tree == error_mark_node)
3949 return error_mark_node;
3950 tree addr_tree = build_fold_addr_expr_loc(loc.gcc_location(),
3951 var_tree);
3952 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
3953 TREE_TYPE(addr_tree),
3954 build2_loc(sut->location().gcc_location(),
3955 MODIFY_EXPR, void_type_node,
3956 var_tree, val_tree),
3957 addr_tree);
3958 }
3959 }
3960
3961 tree expr = this->expr_->get_tree(context);
3962 if (expr == error_mark_node)
3963 return error_mark_node;
3964
3965 switch (this->op_)
3966 {
3967 case OPERATOR_PLUS:
3968 return expr;
3969
3970 case OPERATOR_MINUS:
3971 {
3972 tree type = TREE_TYPE(expr);
3973 tree compute_type = excess_precision_type(type);
3974 if (compute_type != NULL_TREE)
3975 expr = ::convert(compute_type, expr);
3976 tree ret = fold_build1_loc(loc.gcc_location(), NEGATE_EXPR,
3977 (compute_type != NULL_TREE
3978 ? compute_type
3979 : type),
3980 expr);
3981 if (compute_type != NULL_TREE)
3982 ret = ::convert(type, ret);
3983 return ret;
3984 }
3985
3986 case OPERATOR_NOT:
3987 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
3988 return fold_build1_loc(loc.gcc_location(), TRUTH_NOT_EXPR,
3989 TREE_TYPE(expr), expr);
3990 else
3991 return fold_build2_loc(loc.gcc_location(), NE_EXPR, boolean_type_node,
3992 expr, build_int_cst(TREE_TYPE(expr), 0));
3993
3994 case OPERATOR_XOR:
3995 return fold_build1_loc(loc.gcc_location(), BIT_NOT_EXPR, TREE_TYPE(expr),
3996 expr);
3997
3998 case OPERATOR_AND:
3999 if (!this->create_temp_)
4000 {
4001 // We should not see a non-constant constructor here; cases
4002 // where we would see one should have been moved onto the
4003 // heap at parse time. Taking the address of a nonconstant
4004 // constructor will not do what the programmer expects.
4005 go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4006 go_assert(TREE_CODE(expr) != ADDR_EXPR);
4007 }
4008
4009 // Build a decl for a constant constructor.
4010 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4011 {
4012 tree decl = build_decl(this->location().gcc_location(), VAR_DECL,
4013 create_tmp_var_name("C"), TREE_TYPE(expr));
4014 DECL_EXTERNAL(decl) = 0;
4015 TREE_PUBLIC(decl) = 0;
4016 TREE_READONLY(decl) = 1;
4017 TREE_CONSTANT(decl) = 1;
4018 TREE_STATIC(decl) = 1;
4019 TREE_ADDRESSABLE(decl) = 1;
4020 DECL_ARTIFICIAL(decl) = 1;
4021 DECL_INITIAL(decl) = expr;
4022 rest_of_decl_compilation(decl, 1, 0);
4023 expr = decl;
4024 }
4025
4026 if (this->create_temp_
4027 && !TREE_ADDRESSABLE(TREE_TYPE(expr))
4028 && (TREE_CODE(expr) == CONST_DECL || !DECL_P(expr))
4029 && TREE_CODE(expr) != INDIRECT_REF
4030 && TREE_CODE(expr) != COMPONENT_REF)
4031 {
4032 if (current_function_decl != NULL)
4033 {
4034 tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr));
4035 DECL_IGNORED_P(tmp) = 1;
4036 DECL_INITIAL(tmp) = expr;
4037 TREE_ADDRESSABLE(tmp) = 1;
4038 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4039 build_pointer_type(TREE_TYPE(expr)),
4040 build1_loc(loc.gcc_location(), DECL_EXPR,
4041 void_type_node, tmp),
4042 build_fold_addr_expr_loc(loc.gcc_location(),
4043 tmp));
4044 }
4045 else
4046 {
4047 tree tmp = build_decl(loc.gcc_location(), VAR_DECL,
4048 create_tmp_var_name("A"), TREE_TYPE(expr));
4049 DECL_EXTERNAL(tmp) = 0;
4050 TREE_PUBLIC(tmp) = 0;
4051 TREE_STATIC(tmp) = 1;
4052 DECL_ARTIFICIAL(tmp) = 1;
4053 TREE_ADDRESSABLE(tmp) = 1;
4054 tree make_tmp;
4055 if (!TREE_CONSTANT(expr))
4056 make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR,
4057 void_type_node, tmp, expr);
4058 else
4059 {
4060 TREE_READONLY(tmp) = 1;
4061 TREE_CONSTANT(tmp) = 1;
4062 DECL_INITIAL(tmp) = expr;
4063 make_tmp = NULL_TREE;
4064 }
4065 rest_of_decl_compilation(tmp, 1, 0);
4066 tree addr = build_fold_addr_expr_loc(loc.gcc_location(), tmp);
4067 if (make_tmp == NULL_TREE)
4068 return addr;
4069 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4070 TREE_TYPE(addr), make_tmp, addr);
4071 }
4072 }
4073
4074 return build_fold_addr_expr_loc(loc.gcc_location(), expr);
4075
4076 case OPERATOR_MULT:
4077 {
4078 go_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
4079
4080 // If we are dereferencing the pointer to a large struct, we
4081 // need to check for nil. We don't bother to check for small
4082 // structs because we expect the system to crash on a nil
4083 // pointer dereference.
4084 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr));
4085 if (!VOID_TYPE_P(target_type_tree))
4086 {
4087 HOST_WIDE_INT s = int_size_in_bytes(target_type_tree);
4088 if (s == -1 || s >= 4096)
4089 {
4090 if (!DECL_P(expr))
4091 expr = save_expr(expr);
4092 tree compare = fold_build2_loc(loc.gcc_location(), EQ_EXPR,
4093 boolean_type_node,
4094 expr,
4095 fold_convert(TREE_TYPE(expr),
4096 null_pointer_node));
4097 tree crash = Gogo::runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
4098 loc);
4099 expr = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4100 TREE_TYPE(expr), build3(COND_EXPR,
4101 void_type_node,
4102 compare, crash,
4103 NULL_TREE),
4104 expr);
4105 }
4106 }
4107
4108 // If the type of EXPR is a recursive pointer type, then we
4109 // need to insert a cast before indirecting.
4110 if (VOID_TYPE_P(target_type_tree))
4111 {
4112 Type* pt = this->expr_->type()->points_to();
4113 tree ind = type_to_tree(pt->get_backend(context->gogo()));
4114 expr = fold_convert_loc(loc.gcc_location(),
4115 build_pointer_type(ind), expr);
4116 }
4117
4118 return build_fold_indirect_ref_loc(loc.gcc_location(), expr);
4119 }
4120
4121 default:
4122 go_unreachable();
4123 }
4124 }
4125
4126 // Export a unary expression.
4127
4128 void
4129 Unary_expression::do_export(Export* exp) const
4130 {
4131 switch (this->op_)
4132 {
4133 case OPERATOR_PLUS:
4134 exp->write_c_string("+ ");
4135 break;
4136 case OPERATOR_MINUS:
4137 exp->write_c_string("- ");
4138 break;
4139 case OPERATOR_NOT:
4140 exp->write_c_string("! ");
4141 break;
4142 case OPERATOR_XOR:
4143 exp->write_c_string("^ ");
4144 break;
4145 case OPERATOR_AND:
4146 case OPERATOR_MULT:
4147 default:
4148 go_unreachable();
4149 }
4150 this->expr_->export_expression(exp);
4151 }
4152
4153 // Import a unary expression.
4154
4155 Expression*
4156 Unary_expression::do_import(Import* imp)
4157 {
4158 Operator op;
4159 switch (imp->get_char())
4160 {
4161 case '+':
4162 op = OPERATOR_PLUS;
4163 break;
4164 case '-':
4165 op = OPERATOR_MINUS;
4166 break;
4167 case '!':
4168 op = OPERATOR_NOT;
4169 break;
4170 case '^':
4171 op = OPERATOR_XOR;
4172 break;
4173 default:
4174 go_unreachable();
4175 }
4176 imp->require_c_string(" ");
4177 Expression* expr = Expression::import_expression(imp);
4178 return Expression::make_unary(op, expr, imp->location());
4179 }
4180
4181 // Dump ast representation of an unary expression.
4182
4183 void
4184 Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
4185 {
4186 ast_dump_context->dump_operator(this->op_);
4187 ast_dump_context->ostream() << "(";
4188 ast_dump_context->dump_expression(this->expr_);
4189 ast_dump_context->ostream() << ") ";
4190 }
4191
4192 // Make a unary expression.
4193
4194 Expression*
4195 Expression::make_unary(Operator op, Expression* expr, Location location)
4196 {
4197 return new Unary_expression(op, expr, location);
4198 }
4199
4200 // If this is an indirection through a pointer, return the expression
4201 // being pointed through. Otherwise return this.
4202
4203 Expression*
4204 Expression::deref()
4205 {
4206 if (this->classification_ == EXPRESSION_UNARY)
4207 {
4208 Unary_expression* ue = static_cast<Unary_expression*>(this);
4209 if (ue->op() == OPERATOR_MULT)
4210 return ue->operand();
4211 }
4212 return this;
4213 }
4214
4215 // Class Binary_expression.
4216
4217 // Traversal.
4218
4219 int
4220 Binary_expression::do_traverse(Traverse* traverse)
4221 {
4222 int t = Expression::traverse(&this->left_, traverse);
4223 if (t == TRAVERSE_EXIT)
4224 return TRAVERSE_EXIT;
4225 return Expression::traverse(&this->right_, traverse);
4226 }
4227
4228 // Return the type to use for a binary operation on operands of
4229 // LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
4230 // such may be NULL or abstract.
4231
4232 bool
4233 Binary_expression::operation_type(Operator op, Type* left_type,
4234 Type* right_type, Type** result_type)
4235 {
4236 if (left_type != right_type
4237 && !left_type->is_abstract()
4238 && !right_type->is_abstract()
4239 && left_type->base() != right_type->base()
4240 && op != OPERATOR_LSHIFT
4241 && op != OPERATOR_RSHIFT)
4242 {
4243 // May be a type error--let it be diagnosed elsewhere.
4244 return false;
4245 }
4246
4247 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
4248 {
4249 if (left_type->integer_type() != NULL)
4250 *result_type = left_type;
4251 else
4252 *result_type = Type::make_abstract_integer_type();
4253 }
4254 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
4255 *result_type = left_type;
4256 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
4257 *result_type = right_type;
4258 else if (!left_type->is_abstract())
4259 *result_type = left_type;
4260 else if (!right_type->is_abstract())
4261 *result_type = right_type;
4262 else if (left_type->complex_type() != NULL)
4263 *result_type = left_type;
4264 else if (right_type->complex_type() != NULL)
4265 *result_type = right_type;
4266 else if (left_type->float_type() != NULL)
4267 *result_type = left_type;
4268 else if (right_type->float_type() != NULL)
4269 *result_type = right_type;
4270 else if (left_type->integer_type() != NULL
4271 && left_type->integer_type()->is_rune())
4272 *result_type = left_type;
4273 else if (right_type->integer_type() != NULL
4274 && right_type->integer_type()->is_rune())
4275 *result_type = right_type;
4276 else
4277 *result_type = left_type;
4278
4279 return true;
4280 }
4281
4282 // Convert an integer comparison code and an operator to a boolean
4283 // value.
4284
4285 bool
4286 Binary_expression::cmp_to_bool(Operator op, int cmp)
4287 {
4288 switch (op)
4289 {
4290 case OPERATOR_EQEQ:
4291 return cmp == 0;
4292 break;
4293 case OPERATOR_NOTEQ:
4294 return cmp != 0;
4295 break;
4296 case OPERATOR_LT:
4297 return cmp < 0;
4298 break;
4299 case OPERATOR_LE:
4300 return cmp <= 0;
4301 case OPERATOR_GT:
4302 return cmp > 0;
4303 case OPERATOR_GE:
4304 return cmp >= 0;
4305 default:
4306 go_unreachable();
4307 }
4308 }
4309
4310 // Compare constants according to OP.
4311
4312 bool
4313 Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc,
4314 Numeric_constant* right_nc,
4315 Location location, bool* result)
4316 {
4317 Type* left_type = left_nc->type();
4318 Type* right_type = right_nc->type();
4319
4320 Type* type;
4321 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4322 return false;
4323
4324 // When comparing an untyped operand to a typed operand, we are
4325 // effectively coercing the untyped operand to the other operand's
4326 // type, so make sure that is valid.
4327 if (!left_nc->set_type(type, true, location)
4328 || !right_nc->set_type(type, true, location))
4329 return false;
4330
4331 bool ret;
4332 int cmp;
4333 if (type->complex_type() != NULL)
4334 {
4335 if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ)
4336 return false;
4337 ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp);
4338 }
4339 else if (type->float_type() != NULL)
4340 ret = Binary_expression::compare_float(left_nc, right_nc, &cmp);
4341 else
4342 ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp);
4343
4344 if (ret)
4345 *result = Binary_expression::cmp_to_bool(op, cmp);
4346
4347 return ret;
4348 }
4349
4350 // Compare integer constants.
4351
4352 bool
4353 Binary_expression::compare_integer(const Numeric_constant* left_nc,
4354 const Numeric_constant* right_nc,
4355 int* cmp)
4356 {
4357 mpz_t left_val;
4358 if (!left_nc->to_int(&left_val))
4359 return false;
4360 mpz_t right_val;
4361 if (!right_nc->to_int(&right_val))
4362 {
4363 mpz_clear(left_val);
4364 return false;
4365 }
4366
4367 *cmp = mpz_cmp(left_val, right_val);
4368
4369 mpz_clear(left_val);
4370 mpz_clear(right_val);
4371
4372 return true;
4373 }
4374
4375 // Compare floating point constants.
4376
4377 bool
4378 Binary_expression::compare_float(const Numeric_constant* left_nc,
4379 const Numeric_constant* right_nc,
4380 int* cmp)
4381 {
4382 mpfr_t left_val;
4383 if (!left_nc->to_float(&left_val))
4384 return false;
4385 mpfr_t right_val;
4386 if (!right_nc->to_float(&right_val))
4387 {
4388 mpfr_clear(left_val);
4389 return false;
4390 }
4391
4392 // We already coerced both operands to the same type. If that type
4393 // is not an abstract type, we need to round the values accordingly.
4394 Type* type = left_nc->type();
4395 if (!type->is_abstract() && type->float_type() != NULL)
4396 {
4397 int bits = type->float_type()->bits();
4398 mpfr_prec_round(left_val, bits, GMP_RNDN);
4399 mpfr_prec_round(right_val, bits, GMP_RNDN);
4400 }
4401
4402 *cmp = mpfr_cmp(left_val, right_val);
4403
4404 mpfr_clear(left_val);
4405 mpfr_clear(right_val);
4406
4407 return true;
4408 }
4409
4410 // Compare complex constants. Complex numbers may only be compared
4411 // for equality.
4412
4413 bool
4414 Binary_expression::compare_complex(const Numeric_constant* left_nc,
4415 const Numeric_constant* right_nc,
4416 int* cmp)
4417 {
4418 mpfr_t left_real, left_imag;
4419 if (!left_nc->to_complex(&left_real, &left_imag))
4420 return false;
4421 mpfr_t right_real, right_imag;
4422 if (!right_nc->to_complex(&right_real, &right_imag))
4423 {
4424 mpfr_clear(left_real);
4425 mpfr_clear(left_imag);
4426 return false;
4427 }
4428
4429 // We already coerced both operands to the same type. If that type
4430 // is not an abstract type, we need to round the values accordingly.
4431 Type* type = left_nc->type();
4432 if (!type->is_abstract() && type->complex_type() != NULL)
4433 {
4434 int bits = type->complex_type()->bits();
4435 mpfr_prec_round(left_real, bits / 2, GMP_RNDN);
4436 mpfr_prec_round(left_imag, bits / 2, GMP_RNDN);
4437 mpfr_prec_round(right_real, bits / 2, GMP_RNDN);
4438 mpfr_prec_round(right_imag, bits / 2, GMP_RNDN);
4439 }
4440
4441 *cmp = (mpfr_cmp(left_real, right_real) != 0
4442 || mpfr_cmp(left_imag, right_imag) != 0);
4443
4444 mpfr_clear(left_real);
4445 mpfr_clear(left_imag);
4446 mpfr_clear(right_real);
4447 mpfr_clear(right_imag);
4448
4449 return true;
4450 }
4451
4452 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
4453 // true if this could be done, false if not. Issue errors at LOCATION
4454 // as appropriate.
4455
4456 bool
4457 Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc,
4458 Numeric_constant* right_nc,
4459 Location location, Numeric_constant* nc)
4460 {
4461 switch (op)
4462 {
4463 case OPERATOR_OROR:
4464 case OPERATOR_ANDAND:
4465 case OPERATOR_EQEQ:
4466 case OPERATOR_NOTEQ:
4467 case OPERATOR_LT:
4468 case OPERATOR_LE:
4469 case OPERATOR_GT:
4470 case OPERATOR_GE:
4471 // These return boolean values, not numeric.
4472 return false;
4473 default:
4474 break;
4475 }
4476
4477 Type* left_type = left_nc->type();
4478 Type* right_type = right_nc->type();
4479
4480 Type* type;
4481 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4482 return false;
4483
4484 bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT;
4485
4486 // When combining an untyped operand with a typed operand, we are
4487 // effectively coercing the untyped operand to the other operand's
4488 // type, so make sure that is valid.
4489 if (!left_nc->set_type(type, true, location))
4490 return false;
4491 if (!is_shift && !right_nc->set_type(type, true, location))
4492 return false;
4493
4494 bool r;
4495 if (type->complex_type() != NULL)
4496 r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc);
4497 else if (type->float_type() != NULL)
4498 r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc);
4499 else
4500 r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc);
4501
4502 if (r)
4503 r = nc->set_type(type, true, location);
4504
4505 return r;
4506 }
4507
4508 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4509 // integer operations. Return true if this could be done, false if
4510 // not.
4511
4512 bool
4513 Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc,
4514 const Numeric_constant* right_nc,
4515 Location location, Numeric_constant* nc)
4516 {
4517 mpz_t left_val;
4518 if (!left_nc->to_int(&left_val))
4519 return false;
4520 mpz_t right_val;
4521 if (!right_nc->to_int(&right_val))
4522 {
4523 mpz_clear(left_val);
4524 return false;
4525 }
4526
4527 mpz_t val;
4528 mpz_init(val);
4529
4530 switch (op)
4531 {
4532 case OPERATOR_PLUS:
4533 mpz_add(val, left_val, right_val);
4534 break;
4535 case OPERATOR_MINUS:
4536 mpz_sub(val, left_val, right_val);
4537 break;
4538 case OPERATOR_OR:
4539 mpz_ior(val, left_val, right_val);
4540 break;
4541 case OPERATOR_XOR:
4542 mpz_xor(val, left_val, right_val);
4543 break;
4544 case OPERATOR_MULT:
4545 mpz_mul(val, left_val, right_val);
4546 break;
4547 case OPERATOR_DIV:
4548 if (mpz_sgn(right_val) != 0)
4549 mpz_tdiv_q(val, left_val, right_val);
4550 else
4551 {
4552 error_at(location, "division by zero");
4553 mpz_set_ui(val, 0);
4554 }
4555 break;
4556 case OPERATOR_MOD:
4557 if (mpz_sgn(right_val) != 0)
4558 mpz_tdiv_r(val, left_val, right_val);
4559 else
4560 {
4561 error_at(location, "division by zero");
4562 mpz_set_ui(val, 0);
4563 }
4564 break;
4565 case OPERATOR_LSHIFT:
4566 {
4567 unsigned long shift = mpz_get_ui(right_val);
4568 if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000)
4569 mpz_mul_2exp(val, left_val, shift);
4570 else
4571 {
4572 error_at(location, "shift count overflow");
4573 mpz_set_ui(val, 0);
4574 }
4575 break;
4576 }
4577 break;
4578 case OPERATOR_RSHIFT:
4579 {
4580 unsigned long shift = mpz_get_ui(right_val);
4581 if (mpz_cmp_ui(right_val, shift) != 0)
4582 {
4583 error_at(location, "shift count overflow");
4584 mpz_set_ui(val, 0);
4585 }
4586 else
4587 {
4588 if (mpz_cmp_ui(left_val, 0) >= 0)
4589 mpz_tdiv_q_2exp(val, left_val, shift);
4590 else
4591 mpz_fdiv_q_2exp(val, left_val, shift);
4592 }
4593 break;
4594 }
4595 break;
4596 case OPERATOR_AND:
4597 mpz_and(val, left_val, right_val);
4598 break;
4599 case OPERATOR_BITCLEAR:
4600 {
4601 mpz_t tval;
4602 mpz_init(tval);
4603 mpz_com(tval, right_val);
4604 mpz_and(val, left_val, tval);
4605 mpz_clear(tval);
4606 }
4607 break;
4608 default:
4609 go_unreachable();
4610 }
4611
4612 mpz_clear(left_val);
4613 mpz_clear(right_val);
4614
4615 if (left_nc->is_rune()
4616 || (op != OPERATOR_LSHIFT
4617 && op != OPERATOR_RSHIFT
4618 && right_nc->is_rune()))
4619 nc->set_rune(NULL, val);
4620 else
4621 nc->set_int(NULL, val);
4622
4623 mpz_clear(val);
4624
4625 return true;
4626 }
4627
4628 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4629 // floating point operations. Return true if this could be done,
4630 // false if not.
4631
4632 bool
4633 Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc,
4634 const Numeric_constant* right_nc,
4635 Location location, Numeric_constant* nc)
4636 {
4637 mpfr_t left_val;
4638 if (!left_nc->to_float(&left_val))
4639 return false;
4640 mpfr_t right_val;
4641 if (!right_nc->to_float(&right_val))
4642 {
4643 mpfr_clear(left_val);
4644 return false;
4645 }
4646
4647 mpfr_t val;
4648 mpfr_init(val);
4649
4650 bool ret = true;
4651 switch (op)
4652 {
4653 case OPERATOR_PLUS:
4654 mpfr_add(val, left_val, right_val, GMP_RNDN);
4655 break;
4656 case OPERATOR_MINUS:
4657 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4658 break;
4659 case OPERATOR_OR:
4660 case OPERATOR_XOR:
4661 case OPERATOR_AND:
4662 case OPERATOR_BITCLEAR:
4663 case OPERATOR_MOD:
4664 case OPERATOR_LSHIFT:
4665 case OPERATOR_RSHIFT:
4666 mpfr_set_ui(val, 0, GMP_RNDN);
4667 ret = false;
4668 break;
4669 case OPERATOR_MULT:
4670 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4671 break;
4672 case OPERATOR_DIV:
4673 if (!mpfr_zero_p(right_val))
4674 mpfr_div(val, left_val, right_val, GMP_RNDN);
4675 else
4676 {
4677 error_at(location, "division by zero");
4678 mpfr_set_ui(val, 0, GMP_RNDN);
4679 }
4680 break;
4681 default:
4682 go_unreachable();
4683 }
4684
4685 mpfr_clear(left_val);
4686 mpfr_clear(right_val);
4687
4688 nc->set_float(NULL, val);
4689 mpfr_clear(val);
4690
4691 return ret;
4692 }
4693
4694 // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4695 // complex operations. Return true if this could be done, false if
4696 // not.
4697
4698 bool
4699 Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc,
4700 const Numeric_constant* right_nc,
4701 Location location, Numeric_constant* nc)
4702 {
4703 mpfr_t left_real, left_imag;
4704 if (!left_nc->to_complex(&left_real, &left_imag))
4705 return false;
4706 mpfr_t right_real, right_imag;
4707 if (!right_nc->to_complex(&right_real, &right_imag))
4708 {
4709 mpfr_clear(left_real);
4710 mpfr_clear(left_imag);
4711 return false;
4712 }
4713
4714 mpfr_t real, imag;
4715 mpfr_init(real);
4716 mpfr_init(imag);
4717
4718 bool ret = true;
4719 switch (op)
4720 {
4721 case OPERATOR_PLUS:
4722 mpfr_add(real, left_real, right_real, GMP_RNDN);
4723 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4724 break;
4725 case OPERATOR_MINUS:
4726 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4727 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4728 break;
4729 case OPERATOR_OR:
4730 case OPERATOR_XOR:
4731 case OPERATOR_AND:
4732 case OPERATOR_BITCLEAR:
4733 case OPERATOR_MOD:
4734 case OPERATOR_LSHIFT:
4735 case OPERATOR_RSHIFT:
4736 mpfr_set_ui(real, 0, GMP_RNDN);
4737 mpfr_set_ui(imag, 0, GMP_RNDN);
4738 ret = false;
4739 break;
4740 case OPERATOR_MULT:
4741 {
4742 // You might think that multiplying two complex numbers would
4743 // be simple, and you would be right, until you start to think
4744 // about getting the right answer for infinity. If one
4745 // operand here is infinity and the other is anything other
4746 // than zero or NaN, then we are going to wind up subtracting
4747 // two infinity values. That will give us a NaN, but the
4748 // correct answer is infinity.
4749
4750 mpfr_t lrrr;
4751 mpfr_init(lrrr);
4752 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4753
4754 mpfr_t lrri;
4755 mpfr_init(lrri);
4756 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4757
4758 mpfr_t lirr;
4759 mpfr_init(lirr);
4760 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4761
4762 mpfr_t liri;
4763 mpfr_init(liri);
4764 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4765
4766 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4767 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4768
4769 // If we get NaN on both sides, check whether it should really
4770 // be infinity. The rule is that if either side of the
4771 // complex number is infinity, then the whole value is
4772 // infinity, even if the other side is NaN. So the only case
4773 // we have to fix is the one in which both sides are NaN.
4774 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4775 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4776 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4777 {
4778 bool is_infinity = false;
4779
4780 mpfr_t lr;
4781 mpfr_t li;
4782 mpfr_init_set(lr, left_real, GMP_RNDN);
4783 mpfr_init_set(li, left_imag, GMP_RNDN);
4784
4785 mpfr_t rr;
4786 mpfr_t ri;
4787 mpfr_init_set(rr, right_real, GMP_RNDN);
4788 mpfr_init_set(ri, right_imag, GMP_RNDN);
4789
4790 // If the left side is infinity, then the result is
4791 // infinity.
4792 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
4793 {
4794 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
4795 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4796 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
4797 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4798 if (mpfr_nan_p(rr))
4799 {
4800 mpfr_set_ui(rr, 0, GMP_RNDN);
4801 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4802 }
4803 if (mpfr_nan_p(ri))
4804 {
4805 mpfr_set_ui(ri, 0, GMP_RNDN);
4806 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4807 }
4808 is_infinity = true;
4809 }
4810
4811 // If the right side is infinity, then the result is
4812 // infinity.
4813 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
4814 {
4815 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4816 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4817 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4818 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4819 if (mpfr_nan_p(lr))
4820 {
4821 mpfr_set_ui(lr, 0, GMP_RNDN);
4822 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4823 }
4824 if (mpfr_nan_p(li))
4825 {
4826 mpfr_set_ui(li, 0, GMP_RNDN);
4827 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4828 }
4829 is_infinity = true;
4830 }
4831
4832 // If we got an overflow in the intermediate computations,
4833 // then the result is infinity.
4834 if (!is_infinity
4835 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
4836 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
4837 {
4838 if (mpfr_nan_p(lr))
4839 {
4840 mpfr_set_ui(lr, 0, GMP_RNDN);
4841 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
4842 }
4843 if (mpfr_nan_p(li))
4844 {
4845 mpfr_set_ui(li, 0, GMP_RNDN);
4846 mpfr_copysign(li, li, left_imag, GMP_RNDN);
4847 }
4848 if (mpfr_nan_p(rr))
4849 {
4850 mpfr_set_ui(rr, 0, GMP_RNDN);
4851 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
4852 }
4853 if (mpfr_nan_p(ri))
4854 {
4855 mpfr_set_ui(ri, 0, GMP_RNDN);
4856 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
4857 }
4858 is_infinity = true;
4859 }
4860
4861 if (is_infinity)
4862 {
4863 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
4864 mpfr_mul(lrri, lr, ri, GMP_RNDN);
4865 mpfr_mul(lirr, li, rr, GMP_RNDN);
4866 mpfr_mul(liri, li, ri, GMP_RNDN);
4867 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4868 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4869 mpfr_set_inf(real, mpfr_sgn(real));
4870 mpfr_set_inf(imag, mpfr_sgn(imag));
4871 }
4872
4873 mpfr_clear(lr);
4874 mpfr_clear(li);
4875 mpfr_clear(rr);
4876 mpfr_clear(ri);
4877 }
4878
4879 mpfr_clear(lrrr);
4880 mpfr_clear(lrri);
4881 mpfr_clear(lirr);
4882 mpfr_clear(liri);
4883 }
4884 break;
4885 case OPERATOR_DIV:
4886 {
4887 // For complex division we want to avoid having an
4888 // intermediate overflow turn the whole result in a NaN. We
4889 // scale the values to try to avoid this.
4890
4891 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
4892 {
4893 error_at(location, "division by zero");
4894 mpfr_set_ui(real, 0, GMP_RNDN);
4895 mpfr_set_ui(imag, 0, GMP_RNDN);
4896 break;
4897 }
4898
4899 mpfr_t rra;
4900 mpfr_t ria;
4901 mpfr_init(rra);
4902 mpfr_init(ria);
4903 mpfr_abs(rra, right_real, GMP_RNDN);
4904 mpfr_abs(ria, right_imag, GMP_RNDN);
4905 mpfr_t t;
4906 mpfr_init(t);
4907 mpfr_max(t, rra, ria, GMP_RNDN);
4908
4909 mpfr_t rr;
4910 mpfr_t ri;
4911 mpfr_init_set(rr, right_real, GMP_RNDN);
4912 mpfr_init_set(ri, right_imag, GMP_RNDN);
4913 long ilogbw = 0;
4914 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
4915 {
4916 ilogbw = mpfr_get_exp(t);
4917 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
4918 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
4919 }
4920
4921 mpfr_t denom;
4922 mpfr_init(denom);
4923 mpfr_mul(denom, rr, rr, GMP_RNDN);
4924 mpfr_mul(t, ri, ri, GMP_RNDN);
4925 mpfr_add(denom, denom, t, GMP_RNDN);
4926
4927 mpfr_mul(real, left_real, rr, GMP_RNDN);
4928 mpfr_mul(t, left_imag, ri, GMP_RNDN);
4929 mpfr_add(real, real, t, GMP_RNDN);
4930 mpfr_div(real, real, denom, GMP_RNDN);
4931 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
4932
4933 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
4934 mpfr_mul(t, left_real, ri, GMP_RNDN);
4935 mpfr_sub(imag, imag, t, GMP_RNDN);
4936 mpfr_div(imag, imag, denom, GMP_RNDN);
4937 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
4938
4939 // If we wind up with NaN on both sides, check whether we
4940 // should really have infinity. The rule is that if either
4941 // side of the complex number is infinity, then the whole
4942 // value is infinity, even if the other side is NaN. So the
4943 // only case we have to fix is the one in which both sides are
4944 // NaN.
4945 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4946 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4947 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4948 {
4949 if (mpfr_zero_p(denom))
4950 {
4951 mpfr_set_inf(real, mpfr_sgn(rr));
4952 mpfr_mul(real, real, left_real, GMP_RNDN);
4953 mpfr_set_inf(imag, mpfr_sgn(rr));
4954 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
4955 }
4956 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
4957 && mpfr_number_p(rr) && mpfr_number_p(ri))
4958 {
4959 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
4960 mpfr_copysign(t, t, left_real, GMP_RNDN);
4961
4962 mpfr_t t2;
4963 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
4964 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
4965
4966 mpfr_t t3;
4967 mpfr_init(t3);
4968 mpfr_mul(t3, t, rr, GMP_RNDN);
4969
4970 mpfr_t t4;
4971 mpfr_init(t4);
4972 mpfr_mul(t4, t2, ri, GMP_RNDN);
4973
4974 mpfr_add(t3, t3, t4, GMP_RNDN);
4975 mpfr_set_inf(real, mpfr_sgn(t3));
4976
4977 mpfr_mul(t3, t2, rr, GMP_RNDN);
4978 mpfr_mul(t4, t, ri, GMP_RNDN);
4979 mpfr_sub(t3, t3, t4, GMP_RNDN);
4980 mpfr_set_inf(imag, mpfr_sgn(t3));
4981
4982 mpfr_clear(t2);
4983 mpfr_clear(t3);
4984 mpfr_clear(t4);
4985 }
4986 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
4987 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
4988 {
4989 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
4990 mpfr_copysign(t, t, rr, GMP_RNDN);
4991
4992 mpfr_t t2;
4993 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
4994 mpfr_copysign(t2, t2, ri, GMP_RNDN);
4995
4996 mpfr_t t3;
4997 mpfr_init(t3);
4998 mpfr_mul(t3, left_real, t, GMP_RNDN);
4999
5000 mpfr_t t4;
5001 mpfr_init(t4);
5002 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5003
5004 mpfr_add(t3, t3, t4, GMP_RNDN);
5005 mpfr_set_ui(real, 0, GMP_RNDN);
5006 mpfr_mul(real, real, t3, GMP_RNDN);
5007
5008 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5009 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5010 mpfr_sub(t3, t3, t4, GMP_RNDN);
5011 mpfr_set_ui(imag, 0, GMP_RNDN);
5012 mpfr_mul(imag, imag, t3, GMP_RNDN);
5013
5014 mpfr_clear(t2);
5015 mpfr_clear(t3);
5016 mpfr_clear(t4);
5017 }
5018 }
5019
5020 mpfr_clear(denom);
5021 mpfr_clear(rr);
5022 mpfr_clear(ri);
5023 mpfr_clear(t);
5024 mpfr_clear(rra);
5025 mpfr_clear(ria);
5026 }
5027 break;
5028 default:
5029 go_unreachable();
5030 }
5031
5032 mpfr_clear(left_real);
5033 mpfr_clear(left_imag);
5034 mpfr_clear(right_real);
5035 mpfr_clear(right_imag);
5036
5037 nc->set_complex(NULL, real, imag);
5038 mpfr_clear(real);
5039 mpfr_clear(imag);
5040
5041 return ret;
5042 }
5043
5044 // Lower a binary expression. We have to evaluate constant
5045 // expressions now, in order to implement Go's unlimited precision
5046 // constants.
5047
5048 Expression*
5049 Binary_expression::do_lower(Gogo* gogo, Named_object*,
5050 Statement_inserter* inserter, int)
5051 {
5052 Location location = this->location();
5053 Operator op = this->op_;
5054 Expression* left = this->left_;
5055 Expression* right = this->right_;
5056
5057 const bool is_comparison = (op == OPERATOR_EQEQ
5058 || op == OPERATOR_NOTEQ
5059 || op == OPERATOR_LT
5060 || op == OPERATOR_LE
5061 || op == OPERATOR_GT
5062 || op == OPERATOR_GE);
5063
5064 // Numeric constant expressions.
5065 {
5066 Numeric_constant left_nc;
5067 Numeric_constant right_nc;
5068 if (left->numeric_constant_value(&left_nc)
5069 && right->numeric_constant_value(&right_nc))
5070 {
5071 if (is_comparison)
5072 {
5073 bool result;
5074 if (!Binary_expression::compare_constant(op, &left_nc,
5075 &right_nc, location,
5076 &result))
5077 return this;
5078 return Expression::make_cast(Type::make_boolean_type(),
5079 Expression::make_boolean(result,
5080 location),
5081 location);
5082 }
5083 else
5084 {
5085 Numeric_constant nc;
5086 if (!Binary_expression::eval_constant(op, &left_nc, &right_nc,
5087 location, &nc))
5088 return this;
5089 return nc.expression(location);
5090 }
5091 }
5092 }
5093
5094 // String constant expressions.
5095 if (left->type()->is_string_type() && right->type()->is_string_type())
5096 {
5097 std::string left_string;
5098 std::string right_string;
5099 if (left->string_constant_value(&left_string)
5100 && right->string_constant_value(&right_string))
5101 {
5102 if (op == OPERATOR_PLUS)
5103 return Expression::make_string(left_string + right_string,
5104 location);
5105 else if (is_comparison)
5106 {
5107 int cmp = left_string.compare(right_string);
5108 bool r = Binary_expression::cmp_to_bool(op, cmp);
5109 return Expression::make_boolean(r, location);
5110 }
5111 }
5112 }
5113
5114 // Lower struct and array comparisons.
5115 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5116 {
5117 if (left->type()->struct_type() != NULL)
5118 return this->lower_struct_comparison(gogo, inserter);
5119 else if (left->type()->array_type() != NULL
5120 && !left->type()->is_slice_type())
5121 return this->lower_array_comparison(gogo, inserter);
5122 }
5123
5124 return this;
5125 }
5126
5127 // Lower a struct comparison.
5128
5129 Expression*
5130 Binary_expression::lower_struct_comparison(Gogo* gogo,
5131 Statement_inserter* inserter)
5132 {
5133 Struct_type* st = this->left_->type()->struct_type();
5134 Struct_type* st2 = this->right_->type()->struct_type();
5135 if (st2 == NULL)
5136 return this;
5137 if (st != st2 && !Type::are_identical(st, st2, false, NULL))
5138 return this;
5139 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5140 this->right_->type(), NULL))
5141 return this;
5142
5143 // See if we can compare using memcmp. As a heuristic, we use
5144 // memcmp rather than field references and comparisons if there are
5145 // more than two fields.
5146 if (st->compare_is_identity(gogo) && st->total_field_count() > 2)
5147 return this->lower_compare_to_memcmp(gogo, inserter);
5148
5149 Location loc = this->location();
5150
5151 Expression* left = this->left_;
5152 Temporary_statement* left_temp = NULL;
5153 if (left->var_expression() == NULL
5154 && left->temporary_reference_expression() == NULL)
5155 {
5156 left_temp = Statement::make_temporary(left->type(), NULL, loc);
5157 inserter->insert(left_temp);
5158 left = Expression::make_set_and_use_temporary(left_temp, left, loc);
5159 }
5160
5161 Expression* right = this->right_;
5162 Temporary_statement* right_temp = NULL;
5163 if (right->var_expression() == NULL
5164 && right->temporary_reference_expression() == NULL)
5165 {
5166 right_temp = Statement::make_temporary(right->type(), NULL, loc);
5167 inserter->insert(right_temp);
5168 right = Expression::make_set_and_use_temporary(right_temp, right, loc);
5169 }
5170
5171 Expression* ret = Expression::make_boolean(true, loc);
5172 const Struct_field_list* fields = st->fields();
5173 unsigned int field_index = 0;
5174 for (Struct_field_list::const_iterator pf = fields->begin();
5175 pf != fields->end();
5176 ++pf, ++field_index)
5177 {
5178 if (field_index > 0)
5179 {
5180 if (left_temp == NULL)
5181 left = left->copy();
5182 else
5183 left = Expression::make_temporary_reference(left_temp, loc);
5184 if (right_temp == NULL)
5185 right = right->copy();
5186 else
5187 right = Expression::make_temporary_reference(right_temp, loc);
5188 }
5189 Expression* f1 = Expression::make_field_reference(left, field_index,
5190 loc);
5191 Expression* f2 = Expression::make_field_reference(right, field_index,
5192 loc);
5193 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc);
5194 ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc);
5195 }
5196
5197 if (this->op_ == OPERATOR_NOTEQ)
5198 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5199
5200 return ret;
5201 }
5202
5203 // Lower an array comparison.
5204
5205 Expression*
5206 Binary_expression::lower_array_comparison(Gogo* gogo,
5207 Statement_inserter* inserter)
5208 {
5209 Array_type* at = this->left_->type()->array_type();
5210 Array_type* at2 = this->right_->type()->array_type();
5211 if (at2 == NULL)
5212 return this;
5213 if (at != at2 && !Type::are_identical(at, at2, false, NULL))
5214 return this;
5215 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5216 this->right_->type(), NULL))
5217 return this;
5218
5219 // Call memcmp directly if possible. This may let the middle-end
5220 // optimize the call.
5221 if (at->compare_is_identity(gogo))
5222 return this->lower_compare_to_memcmp(gogo, inserter);
5223
5224 // Call the array comparison function.
5225 Named_object* hash_fn;
5226 Named_object* equal_fn;
5227 at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL,
5228 &hash_fn, &equal_fn);
5229
5230 Location loc = this->location();
5231
5232 Expression* func = Expression::make_func_reference(equal_fn, NULL, loc);
5233
5234 Expression_list* args = new Expression_list();
5235 args->push_back(this->operand_address(inserter, this->left_));
5236 args->push_back(this->operand_address(inserter, this->right_));
5237 args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE));
5238
5239 Expression* ret = Expression::make_call(func, args, false, loc);
5240
5241 if (this->op_ == OPERATOR_NOTEQ)
5242 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5243
5244 return ret;
5245 }
5246
5247 // Lower a struct or array comparison to a call to memcmp.
5248
5249 Expression*
5250 Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter)
5251 {
5252 Location loc = this->location();
5253
5254 Expression* a1 = this->operand_address(inserter, this->left_);
5255 Expression* a2 = this->operand_address(inserter, this->right_);
5256 Expression* len = Expression::make_type_info(this->left_->type(),
5257 TYPE_INFO_SIZE);
5258
5259 Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len);
5260
5261 mpz_t zval;
5262 mpz_init_set_ui(zval, 0);
5263 Expression* zero = Expression::make_integer(&zval, NULL, loc);
5264 mpz_clear(zval);
5265
5266 return Expression::make_binary(this->op_, call, zero, loc);
5267 }
5268
5269 // Return the address of EXPR, cast to unsafe.Pointer.
5270
5271 Expression*
5272 Binary_expression::operand_address(Statement_inserter* inserter,
5273 Expression* expr)
5274 {
5275 Location loc = this->location();
5276
5277 if (!expr->is_addressable())
5278 {
5279 Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL,
5280 loc);
5281 inserter->insert(temp);
5282 expr = Expression::make_set_and_use_temporary(temp, expr, loc);
5283 }
5284 expr = Expression::make_unary(OPERATOR_AND, expr, loc);
5285 static_cast<Unary_expression*>(expr)->set_does_not_escape();
5286 Type* void_type = Type::make_void_type();
5287 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
5288 return Expression::make_cast(unsafe_pointer_type, expr, loc);
5289 }
5290
5291 // Return the numeric constant value, if it has one.
5292
5293 bool
5294 Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const
5295 {
5296 Numeric_constant left_nc;
5297 if (!this->left_->numeric_constant_value(&left_nc))
5298 return false;
5299 Numeric_constant right_nc;
5300 if (!this->right_->numeric_constant_value(&right_nc))
5301 return false;
5302 return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc,
5303 this->location(), nc);
5304 }
5305
5306 // Note that the value is being discarded.
5307
5308 void
5309 Binary_expression::do_discarding_value()
5310 {
5311 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
5312 this->right_->discarding_value();
5313 else
5314 this->unused_value_error();
5315 }
5316
5317 // Get type.
5318
5319 Type*
5320 Binary_expression::do_type()
5321 {
5322 if (this->classification() == EXPRESSION_ERROR)
5323 return Type::make_error_type();
5324
5325 switch (this->op_)
5326 {
5327 case OPERATOR_EQEQ:
5328 case OPERATOR_NOTEQ:
5329 case OPERATOR_LT:
5330 case OPERATOR_LE:
5331 case OPERATOR_GT:
5332 case OPERATOR_GE:
5333 if (this->type_ == NULL)
5334 this->type_ = Type::make_boolean_type();
5335 return this->type_;
5336
5337 case OPERATOR_PLUS:
5338 case OPERATOR_MINUS:
5339 case OPERATOR_OR:
5340 case OPERATOR_XOR:
5341 case OPERATOR_MULT:
5342 case OPERATOR_DIV:
5343 case OPERATOR_MOD:
5344 case OPERATOR_AND:
5345 case OPERATOR_BITCLEAR:
5346 case OPERATOR_OROR:
5347 case OPERATOR_ANDAND:
5348 {
5349 Type* type;
5350 if (!Binary_expression::operation_type(this->op_,
5351 this->left_->type(),
5352 this->right_->type(),
5353 &type))
5354 return Type::make_error_type();
5355 return type;
5356 }
5357
5358 case OPERATOR_LSHIFT:
5359 case OPERATOR_RSHIFT:
5360 return this->left_->type();
5361
5362 default:
5363 go_unreachable();
5364 }
5365 }
5366
5367 // Set type for a binary expression.
5368
5369 void
5370 Binary_expression::do_determine_type(const Type_context* context)
5371 {
5372 Type* tleft = this->left_->type();
5373 Type* tright = this->right_->type();
5374
5375 // Both sides should have the same type, except for the shift
5376 // operations. For a comparison, we should ignore the incoming
5377 // type.
5378
5379 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5380 || this->op_ == OPERATOR_RSHIFT);
5381
5382 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5383 || this->op_ == OPERATOR_NOTEQ
5384 || this->op_ == OPERATOR_LT
5385 || this->op_ == OPERATOR_LE
5386 || this->op_ == OPERATOR_GT
5387 || this->op_ == OPERATOR_GE);
5388
5389 Type_context subcontext(*context);
5390
5391 if (is_comparison)
5392 {
5393 // In a comparison, the context does not determine the types of
5394 // the operands.
5395 subcontext.type = NULL;
5396 }
5397
5398 // Set the context for the left hand operand.
5399 if (is_shift_op)
5400 {
5401 // The right hand operand of a shift plays no role in
5402 // determining the type of the left hand operand.
5403 }
5404 else if (!tleft->is_abstract())
5405 subcontext.type = tleft;
5406 else if (!tright->is_abstract())
5407 subcontext.type = tright;
5408 else if (subcontext.type == NULL)
5409 {
5410 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5411 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5412 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5413 {
5414 // Both sides have an abstract integer, abstract float, or
5415 // abstract complex type. Just let CONTEXT determine
5416 // whether they may remain abstract or not.
5417 }
5418 else if (tleft->complex_type() != NULL)
5419 subcontext.type = tleft;
5420 else if (tright->complex_type() != NULL)
5421 subcontext.type = tright;
5422 else if (tleft->float_type() != NULL)
5423 subcontext.type = tleft;
5424 else if (tright->float_type() != NULL)
5425 subcontext.type = tright;
5426 else
5427 subcontext.type = tleft;
5428
5429 if (subcontext.type != NULL && !context->may_be_abstract)
5430 subcontext.type = subcontext.type->make_non_abstract_type();
5431 }
5432
5433 this->left_->determine_type(&subcontext);
5434
5435 if (is_shift_op)
5436 {
5437 // We may have inherited an unusable type for the shift operand.
5438 // Give a useful error if that happened.
5439 if (tleft->is_abstract()
5440 && subcontext.type != NULL
5441 && (this->left_->type()->integer_type() == NULL
5442 || (subcontext.type->integer_type() == NULL
5443 && subcontext.type->float_type() == NULL
5444 && subcontext.type->complex_type() == NULL)))
5445 this->report_error(("invalid context-determined non-integer type "
5446 "for shift operand"));
5447
5448 // The context for the right hand operand is the same as for the
5449 // left hand operand, except for a shift operator.
5450 subcontext.type = Type::lookup_integer_type("uint");
5451 subcontext.may_be_abstract = false;
5452 }
5453
5454 this->right_->determine_type(&subcontext);
5455
5456 if (is_comparison)
5457 {
5458 if (this->type_ != NULL && !this->type_->is_abstract())
5459 ;
5460 else if (context->type != NULL && context->type->is_boolean_type())
5461 this->type_ = context->type;
5462 else if (!context->may_be_abstract)
5463 this->type_ = Type::lookup_bool_type();
5464 }
5465 }
5466
5467 // Report an error if the binary operator OP does not support TYPE.
5468 // OTYPE is the type of the other operand. Return whether the
5469 // operation is OK. This should not be used for shift.
5470
5471 bool
5472 Binary_expression::check_operator_type(Operator op, Type* type, Type* otype,
5473 Location location)
5474 {
5475 switch (op)
5476 {
5477 case OPERATOR_OROR:
5478 case OPERATOR_ANDAND:
5479 if (!type->is_boolean_type())
5480 {
5481 error_at(location, "expected boolean type");
5482 return false;
5483 }
5484 break;
5485
5486 case OPERATOR_EQEQ:
5487 case OPERATOR_NOTEQ:
5488 {
5489 std::string reason;
5490 if (!Type::are_compatible_for_comparison(true, type, otype, &reason))
5491 {
5492 error_at(location, "%s", reason.c_str());
5493 return false;
5494 }
5495 }
5496 break;
5497
5498 case OPERATOR_LT:
5499 case OPERATOR_LE:
5500 case OPERATOR_GT:
5501 case OPERATOR_GE:
5502 {
5503 std::string reason;
5504 if (!Type::are_compatible_for_comparison(false, type, otype, &reason))
5505 {
5506 error_at(location, "%s", reason.c_str());
5507 return false;
5508 }
5509 }
5510 break;
5511
5512 case OPERATOR_PLUS:
5513 case OPERATOR_PLUSEQ:
5514 if (type->integer_type() == NULL
5515 && type->float_type() == NULL
5516 && type->complex_type() == NULL
5517 && !type->is_string_type())
5518 {
5519 error_at(location,
5520 "expected integer, floating, complex, or string type");
5521 return false;
5522 }
5523 break;
5524
5525 case OPERATOR_MINUS:
5526 case OPERATOR_MINUSEQ:
5527 case OPERATOR_MULT:
5528 case OPERATOR_MULTEQ:
5529 case OPERATOR_DIV:
5530 case OPERATOR_DIVEQ:
5531 if (type->integer_type() == NULL
5532 && type->float_type() == NULL
5533 && type->complex_type() == NULL)
5534 {
5535 error_at(location, "expected integer, floating, or complex type");
5536 return false;
5537 }
5538 break;
5539
5540 case OPERATOR_MOD:
5541 case OPERATOR_MODEQ:
5542 case OPERATOR_OR:
5543 case OPERATOR_OREQ:
5544 case OPERATOR_AND:
5545 case OPERATOR_ANDEQ:
5546 case OPERATOR_XOR:
5547 case OPERATOR_XOREQ:
5548 case OPERATOR_BITCLEAR:
5549 case OPERATOR_BITCLEAREQ:
5550 if (type->integer_type() == NULL)
5551 {
5552 error_at(location, "expected integer type");
5553 return false;
5554 }
5555 break;
5556
5557 default:
5558 go_unreachable();
5559 }
5560
5561 return true;
5562 }
5563
5564 // Check types.
5565
5566 void
5567 Binary_expression::do_check_types(Gogo*)
5568 {
5569 if (this->classification() == EXPRESSION_ERROR)
5570 return;
5571
5572 Type* left_type = this->left_->type();
5573 Type* right_type = this->right_->type();
5574 if (left_type->is_error() || right_type->is_error())
5575 {
5576 this->set_is_error();
5577 return;
5578 }
5579
5580 if (this->op_ == OPERATOR_EQEQ
5581 || this->op_ == OPERATOR_NOTEQ
5582 || this->op_ == OPERATOR_LT
5583 || this->op_ == OPERATOR_LE
5584 || this->op_ == OPERATOR_GT
5585 || this->op_ == OPERATOR_GE)
5586 {
5587 if (!Type::are_assignable(left_type, right_type, NULL)
5588 && !Type::are_assignable(right_type, left_type, NULL))
5589 {
5590 this->report_error(_("incompatible types in binary expression"));
5591 return;
5592 }
5593 if (!Binary_expression::check_operator_type(this->op_, left_type,
5594 right_type,
5595 this->location())
5596 || !Binary_expression::check_operator_type(this->op_, right_type,
5597 left_type,
5598 this->location()))
5599 {
5600 this->set_is_error();
5601 return;
5602 }
5603 }
5604 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5605 {
5606 if (!Type::are_compatible_for_binop(left_type, right_type))
5607 {
5608 this->report_error(_("incompatible types in binary expression"));
5609 return;
5610 }
5611 if (!Binary_expression::check_operator_type(this->op_, left_type,
5612 right_type,
5613 this->location()))
5614 {
5615 this->set_is_error();
5616 return;
5617 }
5618 }
5619 else
5620 {
5621 if (left_type->integer_type() == NULL)
5622 this->report_error(_("shift of non-integer operand"));
5623
5624 if (!right_type->is_abstract()
5625 && (right_type->integer_type() == NULL
5626 || !right_type->integer_type()->is_unsigned()))
5627 this->report_error(_("shift count not unsigned integer"));
5628 else
5629 {
5630 Numeric_constant nc;
5631 if (this->right_->numeric_constant_value(&nc))
5632 {
5633 mpz_t val;
5634 if (!nc.to_int(&val))
5635 this->report_error(_("shift count not unsigned integer"));
5636 else
5637 {
5638 if (mpz_sgn(val) < 0)
5639 {
5640 this->report_error(_("negative shift count"));
5641 mpz_set_ui(val, 0);
5642 Location rloc = this->right_->location();
5643 this->right_ = Expression::make_integer(&val, right_type,
5644 rloc);
5645 }
5646 mpz_clear(val);
5647 }
5648 }
5649 }
5650 }
5651 }
5652
5653 // Get a tree for a binary expression.
5654
5655 tree
5656 Binary_expression::do_get_tree(Translate_context* context)
5657 {
5658 tree left = this->left_->get_tree(context);
5659 tree right = this->right_->get_tree(context);
5660
5661 if (left == error_mark_node || right == error_mark_node)
5662 return error_mark_node;
5663
5664 enum tree_code code;
5665 bool use_left_type = true;
5666 bool is_shift_op = false;
5667 bool is_idiv_op = false;
5668 switch (this->op_)
5669 {
5670 case OPERATOR_EQEQ:
5671 case OPERATOR_NOTEQ:
5672 case OPERATOR_LT:
5673 case OPERATOR_LE:
5674 case OPERATOR_GT:
5675 case OPERATOR_GE:
5676 return Expression::comparison_tree(context, this->type_, this->op_,
5677 this->left_->type(), left,
5678 this->right_->type(), right,
5679 this->location());
5680
5681 case OPERATOR_OROR:
5682 code = TRUTH_ORIF_EXPR;
5683 use_left_type = false;
5684 break;
5685 case OPERATOR_ANDAND:
5686 code = TRUTH_ANDIF_EXPR;
5687 use_left_type = false;
5688 break;
5689 case OPERATOR_PLUS:
5690 code = PLUS_EXPR;
5691 break;
5692 case OPERATOR_MINUS:
5693 code = MINUS_EXPR;
5694 break;
5695 case OPERATOR_OR:
5696 code = BIT_IOR_EXPR;
5697 break;
5698 case OPERATOR_XOR:
5699 code = BIT_XOR_EXPR;
5700 break;
5701 case OPERATOR_MULT:
5702 code = MULT_EXPR;
5703 break;
5704 case OPERATOR_DIV:
5705 {
5706 Type *t = this->left_->type();
5707 if (t->float_type() != NULL || t->complex_type() != NULL)
5708 code = RDIV_EXPR;
5709 else
5710 {
5711 code = TRUNC_DIV_EXPR;
5712 is_idiv_op = true;
5713 }
5714 }
5715 break;
5716 case OPERATOR_MOD:
5717 code = TRUNC_MOD_EXPR;
5718 is_idiv_op = true;
5719 break;
5720 case OPERATOR_LSHIFT:
5721 code = LSHIFT_EXPR;
5722 is_shift_op = true;
5723 break;
5724 case OPERATOR_RSHIFT:
5725 code = RSHIFT_EXPR;
5726 is_shift_op = true;
5727 break;
5728 case OPERATOR_AND:
5729 code = BIT_AND_EXPR;
5730 break;
5731 case OPERATOR_BITCLEAR:
5732 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
5733 code = BIT_AND_EXPR;
5734 break;
5735 default:
5736 go_unreachable();
5737 }
5738
5739 location_t gccloc = this->location().gcc_location();
5740 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
5741
5742 if (this->left_->type()->is_string_type())
5743 {
5744 go_assert(this->op_ == OPERATOR_PLUS);
5745 Type* st = Type::make_string_type();
5746 tree string_type = type_to_tree(st->get_backend(context->gogo()));
5747 static tree string_plus_decl;
5748 return Gogo::call_builtin(&string_plus_decl,
5749 this->location(),
5750 "__go_string_plus",
5751 2,
5752 string_type,
5753 string_type,
5754 left,
5755 string_type,
5756 right);
5757 }
5758
5759 tree compute_type = excess_precision_type(type);
5760 if (compute_type != NULL_TREE)
5761 {
5762 left = ::convert(compute_type, left);
5763 right = ::convert(compute_type, right);
5764 }
5765
5766 tree eval_saved = NULL_TREE;
5767 if (is_shift_op
5768 || (is_idiv_op && (go_check_divide_zero || go_check_divide_overflow)))
5769 {
5770 // Make sure the values are evaluated.
5771 if (!DECL_P(left))
5772 {
5773 left = save_expr(left);
5774 eval_saved = left;
5775 }
5776 if (!DECL_P(right))
5777 {
5778 right = save_expr(right);
5779 if (eval_saved == NULL_TREE)
5780 eval_saved = right;
5781 else
5782 eval_saved = fold_build2_loc(gccloc, COMPOUND_EXPR,
5783 void_type_node, eval_saved, right);
5784 }
5785 }
5786
5787 tree ret = fold_build2_loc(gccloc, code,
5788 compute_type != NULL_TREE ? compute_type : type,
5789 left, right);
5790
5791 if (compute_type != NULL_TREE)
5792 ret = ::convert(type, ret);
5793
5794 // In Go, a shift larger than the size of the type is well-defined.
5795 // This is not true in GENERIC, so we need to insert a conditional.
5796 if (is_shift_op)
5797 {
5798 go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
5799 go_assert(this->left_->type()->integer_type() != NULL);
5800 int bits = TYPE_PRECISION(TREE_TYPE(left));
5801
5802 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
5803 build_int_cst_type(TREE_TYPE(right), bits));
5804
5805 tree overflow_result = fold_convert_loc(gccloc, TREE_TYPE(left),
5806 integer_zero_node);
5807 if (this->op_ == OPERATOR_RSHIFT
5808 && !this->left_->type()->integer_type()->is_unsigned())
5809 {
5810 tree neg =
5811 fold_build2_loc(gccloc, LT_EXPR, boolean_type_node,
5812 left,
5813 fold_convert_loc(gccloc, TREE_TYPE(left),
5814 integer_zero_node));
5815 tree neg_one =
5816 fold_build2_loc(gccloc, MINUS_EXPR, TREE_TYPE(left),
5817 fold_convert_loc(gccloc, TREE_TYPE(left),
5818 integer_zero_node),
5819 fold_convert_loc(gccloc, TREE_TYPE(left),
5820 integer_one_node));
5821 overflow_result =
5822 fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left),
5823 neg, neg_one, overflow_result);
5824 }
5825
5826 ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left),
5827 compare, ret, overflow_result);
5828
5829 if (eval_saved != NULL_TREE)
5830 ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret),
5831 eval_saved, ret);
5832 }
5833
5834 // Add checks for division by zero and division overflow as needed.
5835 if (is_idiv_op)
5836 {
5837 if (go_check_divide_zero)
5838 {
5839 // right == 0
5840 tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node,
5841 right,
5842 fold_convert_loc(gccloc,
5843 TREE_TYPE(right),
5844 integer_zero_node));
5845
5846 // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO), 0
5847 int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO;
5848 tree panic = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret),
5849 Gogo::runtime_error(errcode,
5850 this->location()),
5851 fold_convert_loc(gccloc, TREE_TYPE(ret),
5852 integer_zero_node));
5853
5854 // right == 0 ? (__go_runtime_error(...), 0) : ret
5855 ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
5856 check, panic, ret);
5857 }
5858
5859 if (go_check_divide_overflow)
5860 {
5861 // right == -1
5862 // FIXME: It would be nice to say that this test is expected
5863 // to return false.
5864 tree m1 = integer_minus_one_node;
5865 tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node,
5866 right,
5867 fold_convert_loc(gccloc,
5868 TREE_TYPE(right),
5869 m1));
5870
5871 tree overflow;
5872 if (TYPE_UNSIGNED(TREE_TYPE(ret)))
5873 {
5874 // An unsigned -1 is the largest possible number, so
5875 // dividing is always 1 or 0.
5876 tree cmp = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node,
5877 left, right);
5878 if (this->op_ == OPERATOR_DIV)
5879 overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
5880 cmp,
5881 fold_convert_loc(gccloc,
5882 TREE_TYPE(ret),
5883 integer_one_node),
5884 fold_convert_loc(gccloc,
5885 TREE_TYPE(ret),
5886 integer_zero_node));
5887 else
5888 overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
5889 cmp,
5890 fold_convert_loc(gccloc,
5891 TREE_TYPE(ret),
5892 integer_zero_node),
5893 left);
5894 }
5895 else
5896 {
5897 // Computing left / -1 is the same as computing - left,
5898 // which does not overflow since Go sets -fwrapv.
5899 if (this->op_ == OPERATOR_DIV)
5900 overflow = fold_build1_loc(gccloc, NEGATE_EXPR, TREE_TYPE(left),
5901 left);
5902 else
5903 overflow = integer_zero_node;
5904 }
5905 overflow = fold_convert_loc(gccloc, TREE_TYPE(ret), overflow);
5906
5907 // right == -1 ? - left : ret
5908 ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
5909 check, overflow, ret);
5910 }
5911
5912 if (eval_saved != NULL_TREE)
5913 ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret),
5914 eval_saved, ret);
5915 }
5916
5917 return ret;
5918 }
5919
5920 // Export a binary expression.
5921
5922 void
5923 Binary_expression::do_export(Export* exp) const
5924 {
5925 exp->write_c_string("(");
5926 this->left_->export_expression(exp);
5927 switch (this->op_)
5928 {
5929 case OPERATOR_OROR:
5930 exp->write_c_string(" || ");
5931 break;
5932 case OPERATOR_ANDAND:
5933 exp->write_c_string(" && ");
5934 break;
5935 case OPERATOR_EQEQ:
5936 exp->write_c_string(" == ");
5937 break;
5938 case OPERATOR_NOTEQ:
5939 exp->write_c_string(" != ");
5940 break;
5941 case OPERATOR_LT:
5942 exp->write_c_string(" < ");
5943 break;
5944 case OPERATOR_LE:
5945 exp->write_c_string(" <= ");
5946 break;
5947 case OPERATOR_GT:
5948 exp->write_c_string(" > ");
5949 break;
5950 case OPERATOR_GE:
5951 exp->write_c_string(" >= ");
5952 break;
5953 case OPERATOR_PLUS:
5954 exp->write_c_string(" + ");
5955 break;
5956 case OPERATOR_MINUS:
5957 exp->write_c_string(" - ");
5958 break;
5959 case OPERATOR_OR:
5960 exp->write_c_string(" | ");
5961 break;
5962 case OPERATOR_XOR:
5963 exp->write_c_string(" ^ ");
5964 break;
5965 case OPERATOR_MULT:
5966 exp->write_c_string(" * ");
5967 break;
5968 case OPERATOR_DIV:
5969 exp->write_c_string(" / ");
5970 break;
5971 case OPERATOR_MOD:
5972 exp->write_c_string(" % ");
5973 break;
5974 case OPERATOR_LSHIFT:
5975 exp->write_c_string(" << ");
5976 break;
5977 case OPERATOR_RSHIFT:
5978 exp->write_c_string(" >> ");
5979 break;
5980 case OPERATOR_AND:
5981 exp->write_c_string(" & ");
5982 break;
5983 case OPERATOR_BITCLEAR:
5984 exp->write_c_string(" &^ ");
5985 break;
5986 default:
5987 go_unreachable();
5988 }
5989 this->right_->export_expression(exp);
5990 exp->write_c_string(")");
5991 }
5992
5993 // Import a binary expression.
5994
5995 Expression*
5996 Binary_expression::do_import(Import* imp)
5997 {
5998 imp->require_c_string("(");
5999
6000 Expression* left = Expression::import_expression(imp);
6001
6002 Operator op;
6003 if (imp->match_c_string(" || "))
6004 {
6005 op = OPERATOR_OROR;
6006 imp->advance(4);
6007 }
6008 else if (imp->match_c_string(" && "))
6009 {
6010 op = OPERATOR_ANDAND;
6011 imp->advance(4);
6012 }
6013 else if (imp->match_c_string(" == "))
6014 {
6015 op = OPERATOR_EQEQ;
6016 imp->advance(4);
6017 }
6018 else if (imp->match_c_string(" != "))
6019 {
6020 op = OPERATOR_NOTEQ;
6021 imp->advance(4);
6022 }
6023 else if (imp->match_c_string(" < "))
6024 {
6025 op = OPERATOR_LT;
6026 imp->advance(3);
6027 }
6028 else if (imp->match_c_string(" <= "))
6029 {
6030 op = OPERATOR_LE;
6031 imp->advance(4);
6032 }
6033 else if (imp->match_c_string(" > "))
6034 {
6035 op = OPERATOR_GT;
6036 imp->advance(3);
6037 }
6038 else if (imp->match_c_string(" >= "))
6039 {
6040 op = OPERATOR_GE;
6041 imp->advance(4);
6042 }
6043 else if (imp->match_c_string(" + "))
6044 {
6045 op = OPERATOR_PLUS;
6046 imp->advance(3);
6047 }
6048 else if (imp->match_c_string(" - "))
6049 {
6050 op = OPERATOR_MINUS;
6051 imp->advance(3);
6052 }
6053 else if (imp->match_c_string(" | "))
6054 {
6055 op = OPERATOR_OR;
6056 imp->advance(3);
6057 }
6058 else if (imp->match_c_string(" ^ "))
6059 {
6060 op = OPERATOR_XOR;
6061 imp->advance(3);
6062 }
6063 else if (imp->match_c_string(" * "))
6064 {
6065 op = OPERATOR_MULT;
6066 imp->advance(3);
6067 }
6068 else if (imp->match_c_string(" / "))
6069 {
6070 op = OPERATOR_DIV;
6071 imp->advance(3);
6072 }
6073 else if (imp->match_c_string(" % "))
6074 {
6075 op = OPERATOR_MOD;
6076 imp->advance(3);
6077 }
6078 else if (imp->match_c_string(" << "))
6079 {
6080 op = OPERATOR_LSHIFT;
6081 imp->advance(4);
6082 }
6083 else if (imp->match_c_string(" >> "))
6084 {
6085 op = OPERATOR_RSHIFT;
6086 imp->advance(4);
6087 }
6088 else if (imp->match_c_string(" & "))
6089 {
6090 op = OPERATOR_AND;
6091 imp->advance(3);
6092 }
6093 else if (imp->match_c_string(" &^ "))
6094 {
6095 op = OPERATOR_BITCLEAR;
6096 imp->advance(4);
6097 }
6098 else
6099 {
6100 error_at(imp->location(), "unrecognized binary operator");
6101 return Expression::make_error(imp->location());
6102 }
6103
6104 Expression* right = Expression::import_expression(imp);
6105
6106 imp->require_c_string(")");
6107
6108 return Expression::make_binary(op, left, right, imp->location());
6109 }
6110
6111 // Dump ast representation of a binary expression.
6112
6113 void
6114 Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
6115 {
6116 ast_dump_context->ostream() << "(";
6117 ast_dump_context->dump_expression(this->left_);
6118 ast_dump_context->ostream() << " ";
6119 ast_dump_context->dump_operator(this->op_);
6120 ast_dump_context->ostream() << " ";
6121 ast_dump_context->dump_expression(this->right_);
6122 ast_dump_context->ostream() << ") ";
6123 }
6124
6125 // Make a binary expression.
6126
6127 Expression*
6128 Expression::make_binary(Operator op, Expression* left, Expression* right,
6129 Location location)
6130 {
6131 return new Binary_expression(op, left, right, location);
6132 }
6133
6134 // Implement a comparison.
6135
6136 tree
6137 Expression::comparison_tree(Translate_context* context, Type* result_type,
6138 Operator op, Type* left_type, tree left_tree,
6139 Type* right_type, tree right_tree,
6140 Location location)
6141 {
6142 enum tree_code code;
6143 switch (op)
6144 {
6145 case OPERATOR_EQEQ:
6146 code = EQ_EXPR;
6147 break;
6148 case OPERATOR_NOTEQ:
6149 code = NE_EXPR;
6150 break;
6151 case OPERATOR_LT:
6152 code = LT_EXPR;
6153 break;
6154 case OPERATOR_LE:
6155 code = LE_EXPR;
6156 break;
6157 case OPERATOR_GT:
6158 code = GT_EXPR;
6159 break;
6160 case OPERATOR_GE:
6161 code = GE_EXPR;
6162 break;
6163 default:
6164 go_unreachable();
6165 }
6166
6167 if (left_type->is_string_type() && right_type->is_string_type())
6168 {
6169 Type* st = Type::make_string_type();
6170 tree string_type = type_to_tree(st->get_backend(context->gogo()));
6171 static tree string_compare_decl;
6172 left_tree = Gogo::call_builtin(&string_compare_decl,
6173 location,
6174 "__go_strcmp",
6175 2,
6176 integer_type_node,
6177 string_type,
6178 left_tree,
6179 string_type,
6180 right_tree);
6181 right_tree = build_int_cst_type(integer_type_node, 0);
6182 }
6183 else if ((left_type->interface_type() != NULL
6184 && right_type->interface_type() == NULL
6185 && !right_type->is_nil_type())
6186 || (left_type->interface_type() == NULL
6187 && !left_type->is_nil_type()
6188 && right_type->interface_type() != NULL))
6189 {
6190 // Comparing an interface value to a non-interface value.
6191 if (left_type->interface_type() == NULL)
6192 {
6193 std::swap(left_type, right_type);
6194 std::swap(left_tree, right_tree);
6195 }
6196
6197 // The right operand is not an interface. We need to take its
6198 // address if it is not a pointer.
6199 tree make_tmp;
6200 tree arg;
6201 if (right_type->points_to() != NULL)
6202 {
6203 make_tmp = NULL_TREE;
6204 arg = right_tree;
6205 }
6206 else if (TREE_ADDRESSABLE(TREE_TYPE(right_tree))
6207 || (TREE_CODE(right_tree) != CONST_DECL
6208 && DECL_P(right_tree)))
6209 {
6210 make_tmp = NULL_TREE;
6211 arg = build_fold_addr_expr_loc(location.gcc_location(), right_tree);
6212 if (DECL_P(right_tree))
6213 TREE_ADDRESSABLE(right_tree) = 1;
6214 }
6215 else
6216 {
6217 tree tmp = create_tmp_var(TREE_TYPE(right_tree),
6218 get_name(right_tree));
6219 DECL_IGNORED_P(tmp) = 0;
6220 DECL_INITIAL(tmp) = right_tree;
6221 TREE_ADDRESSABLE(tmp) = 1;
6222 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
6223 SET_EXPR_LOCATION(make_tmp, location.gcc_location());
6224 arg = build_fold_addr_expr_loc(location.gcc_location(), tmp);
6225 }
6226 arg = fold_convert_loc(location.gcc_location(), ptr_type_node, arg);
6227
6228 tree descriptor = right_type->type_descriptor_pointer(context->gogo(),
6229 location);
6230
6231 if (left_type->interface_type()->is_empty())
6232 {
6233 static tree empty_interface_value_compare_decl;
6234 left_tree = Gogo::call_builtin(&empty_interface_value_compare_decl,
6235 location,
6236 "__go_empty_interface_value_compare",
6237 3,
6238 integer_type_node,
6239 TREE_TYPE(left_tree),
6240 left_tree,
6241 TREE_TYPE(descriptor),
6242 descriptor,
6243 ptr_type_node,
6244 arg);
6245 if (left_tree == error_mark_node)
6246 return error_mark_node;
6247 // This can panic if the type is not comparable.
6248 TREE_NOTHROW(empty_interface_value_compare_decl) = 0;
6249 }
6250 else
6251 {
6252 static tree interface_value_compare_decl;
6253 left_tree = Gogo::call_builtin(&interface_value_compare_decl,
6254 location,
6255 "__go_interface_value_compare",
6256 3,
6257 integer_type_node,
6258 TREE_TYPE(left_tree),
6259 left_tree,
6260 TREE_TYPE(descriptor),
6261 descriptor,
6262 ptr_type_node,
6263 arg);
6264 if (left_tree == error_mark_node)
6265 return error_mark_node;
6266 // This can panic if the type is not comparable.
6267 TREE_NOTHROW(interface_value_compare_decl) = 0;
6268 }
6269 right_tree = build_int_cst_type(integer_type_node, 0);
6270
6271 if (make_tmp != NULL_TREE)
6272 left_tree = build2(COMPOUND_EXPR, TREE_TYPE(left_tree), make_tmp,
6273 left_tree);
6274 }
6275 else if (left_type->interface_type() != NULL
6276 && right_type->interface_type() != NULL)
6277 {
6278 if (left_type->interface_type()->is_empty()
6279 && right_type->interface_type()->is_empty())
6280 {
6281 static tree empty_interface_compare_decl;
6282 left_tree = Gogo::call_builtin(&empty_interface_compare_decl,
6283 location,
6284 "__go_empty_interface_compare",
6285 2,
6286 integer_type_node,
6287 TREE_TYPE(left_tree),
6288 left_tree,
6289 TREE_TYPE(right_tree),
6290 right_tree);
6291 if (left_tree == error_mark_node)
6292 return error_mark_node;
6293 // This can panic if the type is uncomparable.
6294 TREE_NOTHROW(empty_interface_compare_decl) = 0;
6295 }
6296 else if (!left_type->interface_type()->is_empty()
6297 && !right_type->interface_type()->is_empty())
6298 {
6299 static tree interface_compare_decl;
6300 left_tree = Gogo::call_builtin(&interface_compare_decl,
6301 location,
6302 "__go_interface_compare",
6303 2,
6304 integer_type_node,
6305 TREE_TYPE(left_tree),
6306 left_tree,
6307 TREE_TYPE(right_tree),
6308 right_tree);
6309 if (left_tree == error_mark_node)
6310 return error_mark_node;
6311 // This can panic if the type is uncomparable.
6312 TREE_NOTHROW(interface_compare_decl) = 0;
6313 }
6314 else
6315 {
6316 if (left_type->interface_type()->is_empty())
6317 {
6318 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
6319 std::swap(left_type, right_type);
6320 std::swap(left_tree, right_tree);
6321 }
6322 go_assert(!left_type->interface_type()->is_empty());
6323 go_assert(right_type->interface_type()->is_empty());
6324 static tree interface_empty_compare_decl;
6325 left_tree = Gogo::call_builtin(&interface_empty_compare_decl,
6326 location,
6327 "__go_interface_empty_compare",
6328 2,
6329 integer_type_node,
6330 TREE_TYPE(left_tree),
6331 left_tree,
6332 TREE_TYPE(right_tree),
6333 right_tree);
6334 if (left_tree == error_mark_node)
6335 return error_mark_node;
6336 // This can panic if the type is uncomparable.
6337 TREE_NOTHROW(interface_empty_compare_decl) = 0;
6338 }
6339
6340 right_tree = build_int_cst_type(integer_type_node, 0);
6341 }
6342
6343 if (left_type->is_nil_type()
6344 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6345 {
6346 std::swap(left_type, right_type);
6347 std::swap(left_tree, right_tree);
6348 }
6349
6350 if (right_type->is_nil_type())
6351 {
6352 if (left_type->array_type() != NULL
6353 && left_type->array_type()->length() == NULL)
6354 {
6355 Array_type* at = left_type->array_type();
6356 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6357 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6358 }
6359 else if (left_type->interface_type() != NULL)
6360 {
6361 // An interface is nil if the first field is nil.
6362 tree left_type_tree = TREE_TYPE(left_tree);
6363 go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
6364 tree field = TYPE_FIELDS(left_type_tree);
6365 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6366 field, NULL_TREE);
6367 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6368 }
6369 else
6370 {
6371 go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
6372 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6373 }
6374 }
6375
6376 if (left_tree == error_mark_node || right_tree == error_mark_node)
6377 return error_mark_node;
6378
6379 tree result_type_tree;
6380 if (result_type == NULL)
6381 result_type_tree = boolean_type_node;
6382 else
6383 result_type_tree = type_to_tree(result_type->get_backend(context->gogo()));
6384
6385 tree ret = fold_build2(code, result_type_tree, left_tree, right_tree);
6386 if (CAN_HAVE_LOCATION_P(ret))
6387 SET_EXPR_LOCATION(ret, location.gcc_location());
6388 return ret;
6389 }
6390
6391 // Class Bound_method_expression.
6392
6393 // Traversal.
6394
6395 int
6396 Bound_method_expression::do_traverse(Traverse* traverse)
6397 {
6398 return Expression::traverse(&this->expr_, traverse);
6399 }
6400
6401 // Return the type of a bound method expression. The type of this
6402 // object is really the type of the method with no receiver. We
6403 // should be able to get away with just returning the type of the
6404 // method.
6405
6406 Type*
6407 Bound_method_expression::do_type()
6408 {
6409 if (this->method_->is_function())
6410 return this->method_->func_value()->type();
6411 else if (this->method_->is_function_declaration())
6412 return this->method_->func_declaration_value()->type();
6413 else
6414 return Type::make_error_type();
6415 }
6416
6417 // Determine the types of a method expression.
6418
6419 void
6420 Bound_method_expression::do_determine_type(const Type_context*)
6421 {
6422 Function_type* fntype = this->type()->function_type();
6423 if (fntype == NULL || !fntype->is_method())
6424 this->expr_->determine_type_no_context();
6425 else
6426 {
6427 Type_context subcontext(fntype->receiver()->type(), false);
6428 this->expr_->determine_type(&subcontext);
6429 }
6430 }
6431
6432 // Check the types of a method expression.
6433
6434 void
6435 Bound_method_expression::do_check_types(Gogo*)
6436 {
6437 if (!this->method_->is_function()
6438 && !this->method_->is_function_declaration())
6439 this->report_error(_("object is not a method"));
6440 else
6441 {
6442 Type* rtype = this->type()->function_type()->receiver()->type()->deref();
6443 Type* etype = (this->expr_type_ != NULL
6444 ? this->expr_type_
6445 : this->expr_->type());
6446 etype = etype->deref();
6447 if (!Type::are_identical(rtype, etype, true, NULL))
6448 this->report_error(_("method type does not match object type"));
6449 }
6450 }
6451
6452 // Get the tree for a method expression. There is no standard tree
6453 // representation for this. The only places it may currently be used
6454 // are in a Call_expression or a Go_statement, which will take it
6455 // apart directly. So this has nothing to do at present.
6456
6457 tree
6458 Bound_method_expression::do_get_tree(Translate_context*)
6459 {
6460 error_at(this->location(), "reference to method other than calling it");
6461 return error_mark_node;
6462 }
6463
6464 // Dump ast representation of a bound method expression.
6465
6466 void
6467 Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
6468 const
6469 {
6470 if (this->expr_type_ != NULL)
6471 ast_dump_context->ostream() << "(";
6472 ast_dump_context->dump_expression(this->expr_);
6473 if (this->expr_type_ != NULL)
6474 {
6475 ast_dump_context->ostream() << ":";
6476 ast_dump_context->dump_type(this->expr_type_);
6477 ast_dump_context->ostream() << ")";
6478 }
6479
6480 ast_dump_context->ostream() << "." << this->method_->name();
6481 }
6482
6483 // Make a method expression.
6484
6485 Bound_method_expression*
6486 Expression::make_bound_method(Expression* expr, Named_object* method,
6487 Location location)
6488 {
6489 return new Bound_method_expression(expr, method, location);
6490 }
6491
6492 // Class Builtin_call_expression. This is used for a call to a
6493 // builtin function.
6494
6495 class Builtin_call_expression : public Call_expression
6496 {
6497 public:
6498 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
6499 bool is_varargs, Location location);
6500
6501 protected:
6502 // This overrides Call_expression::do_lower.
6503 Expression*
6504 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
6505
6506 bool
6507 do_is_constant() const;
6508
6509 bool
6510 do_numeric_constant_value(Numeric_constant*) const;
6511
6512 void
6513 do_discarding_value();
6514
6515 Type*
6516 do_type();
6517
6518 void
6519 do_determine_type(const Type_context*);
6520
6521 void
6522 do_check_types(Gogo*);
6523
6524 Expression*
6525 do_copy()
6526 {
6527 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
6528 this->args()->copy(),
6529 this->is_varargs(),
6530 this->location());
6531 }
6532
6533 tree
6534 do_get_tree(Translate_context*);
6535
6536 void
6537 do_export(Export*) const;
6538
6539 virtual bool
6540 do_is_recover_call() const;
6541
6542 virtual void
6543 do_set_recover_arg(Expression*);
6544
6545 private:
6546 // The builtin functions.
6547 enum Builtin_function_code
6548 {
6549 BUILTIN_INVALID,
6550
6551 // Predeclared builtin functions.
6552 BUILTIN_APPEND,
6553 BUILTIN_CAP,
6554 BUILTIN_CLOSE,
6555 BUILTIN_COMPLEX,
6556 BUILTIN_COPY,
6557 BUILTIN_DELETE,
6558 BUILTIN_IMAG,
6559 BUILTIN_LEN,
6560 BUILTIN_MAKE,
6561 BUILTIN_NEW,
6562 BUILTIN_PANIC,
6563 BUILTIN_PRINT,
6564 BUILTIN_PRINTLN,
6565 BUILTIN_REAL,
6566 BUILTIN_RECOVER,
6567
6568 // Builtin functions from the unsafe package.
6569 BUILTIN_ALIGNOF,
6570 BUILTIN_OFFSETOF,
6571 BUILTIN_SIZEOF
6572 };
6573
6574 Expression*
6575 one_arg() const;
6576
6577 bool
6578 check_one_arg();
6579
6580 static Type*
6581 real_imag_type(Type*);
6582
6583 static Type*
6584 complex_type(Type*);
6585
6586 Expression*
6587 lower_make();
6588
6589 bool
6590 check_int_value(Expression*);
6591
6592 // A pointer back to the general IR structure. This avoids a global
6593 // variable, or passing it around everywhere.
6594 Gogo* gogo_;
6595 // The builtin function being called.
6596 Builtin_function_code code_;
6597 // Used to stop endless loops when the length of an array uses len
6598 // or cap of the array itself.
6599 mutable bool seen_;
6600 };
6601
6602 Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
6603 Expression* fn,
6604 Expression_list* args,
6605 bool is_varargs,
6606 Location location)
6607 : Call_expression(fn, args, is_varargs, location),
6608 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
6609 {
6610 Func_expression* fnexp = this->fn()->func_expression();
6611 go_assert(fnexp != NULL);
6612 const std::string& name(fnexp->named_object()->name());
6613 if (name == "append")
6614 this->code_ = BUILTIN_APPEND;
6615 else if (name == "cap")
6616 this->code_ = BUILTIN_CAP;
6617 else if (name == "close")
6618 this->code_ = BUILTIN_CLOSE;
6619 else if (name == "complex")
6620 this->code_ = BUILTIN_COMPLEX;
6621 else if (name == "copy")
6622 this->code_ = BUILTIN_COPY;
6623 else if (name == "delete")
6624 this->code_ = BUILTIN_DELETE;
6625 else if (name == "imag")
6626 this->code_ = BUILTIN_IMAG;
6627 else if (name == "len")
6628 this->code_ = BUILTIN_LEN;
6629 else if (name == "make")
6630 this->code_ = BUILTIN_MAKE;
6631 else if (name == "new")
6632 this->code_ = BUILTIN_NEW;
6633 else if (name == "panic")
6634 this->code_ = BUILTIN_PANIC;
6635 else if (name == "print")
6636 this->code_ = BUILTIN_PRINT;
6637 else if (name == "println")
6638 this->code_ = BUILTIN_PRINTLN;
6639 else if (name == "real")
6640 this->code_ = BUILTIN_REAL;
6641 else if (name == "recover")
6642 this->code_ = BUILTIN_RECOVER;
6643 else if (name == "Alignof")
6644 this->code_ = BUILTIN_ALIGNOF;
6645 else if (name == "Offsetof")
6646 this->code_ = BUILTIN_OFFSETOF;
6647 else if (name == "Sizeof")
6648 this->code_ = BUILTIN_SIZEOF;
6649 else
6650 go_unreachable();
6651 }
6652
6653 // Return whether this is a call to recover. This is a virtual
6654 // function called from the parent class.
6655
6656 bool
6657 Builtin_call_expression::do_is_recover_call() const
6658 {
6659 if (this->classification() == EXPRESSION_ERROR)
6660 return false;
6661 return this->code_ == BUILTIN_RECOVER;
6662 }
6663
6664 // Set the argument for a call to recover.
6665
6666 void
6667 Builtin_call_expression::do_set_recover_arg(Expression* arg)
6668 {
6669 const Expression_list* args = this->args();
6670 go_assert(args == NULL || args->empty());
6671 Expression_list* new_args = new Expression_list();
6672 new_args->push_back(arg);
6673 this->set_args(new_args);
6674 }
6675
6676 // A traversal class which looks for a call expression.
6677
6678 class Find_call_expression : public Traverse
6679 {
6680 public:
6681 Find_call_expression()
6682 : Traverse(traverse_expressions),
6683 found_(false)
6684 { }
6685
6686 int
6687 expression(Expression**);
6688
6689 bool
6690 found()
6691 { return this->found_; }
6692
6693 private:
6694 bool found_;
6695 };
6696
6697 int
6698 Find_call_expression::expression(Expression** pexpr)
6699 {
6700 if ((*pexpr)->call_expression() != NULL)
6701 {
6702 this->found_ = true;
6703 return TRAVERSE_EXIT;
6704 }
6705 return TRAVERSE_CONTINUE;
6706 }
6707
6708 // Lower a builtin call expression. This turns new and make into
6709 // specific expressions. We also convert to a constant if we can.
6710
6711 Expression*
6712 Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function,
6713 Statement_inserter* inserter, int)
6714 {
6715 if (this->classification() == EXPRESSION_ERROR)
6716 return this;
6717
6718 Location loc = this->location();
6719
6720 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
6721 {
6722 this->report_error(_("invalid use of %<...%> with builtin function"));
6723 return Expression::make_error(loc);
6724 }
6725
6726 if (this->is_constant())
6727 {
6728 // We can only lower len and cap if there are no function calls
6729 // in the arguments. Otherwise we have to make the call.
6730 if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP)
6731 {
6732 Expression* arg = this->one_arg();
6733 if (arg != NULL && !arg->is_constant())
6734 {
6735 Find_call_expression find_call;
6736 Expression::traverse(&arg, &find_call);
6737 if (find_call.found())
6738 return this;
6739 }
6740 }
6741
6742 Numeric_constant nc;
6743 if (this->numeric_constant_value(&nc))
6744 return nc.expression(loc);
6745 }
6746
6747 switch (this->code_)
6748 {
6749 default:
6750 break;
6751
6752 case BUILTIN_NEW:
6753 {
6754 const Expression_list* args = this->args();
6755 if (args == NULL || args->size() < 1)
6756 this->report_error(_("not enough arguments"));
6757 else if (args->size() > 1)
6758 this->report_error(_("too many arguments"));
6759 else
6760 {
6761 Expression* arg = args->front();
6762 if (!arg->is_type_expression())
6763 {
6764 error_at(arg->location(), "expected type");
6765 this->set_is_error();
6766 }
6767 else
6768 return Expression::make_allocation(arg->type(), loc);
6769 }
6770 }
6771 break;
6772
6773 case BUILTIN_MAKE:
6774 return this->lower_make();
6775
6776 case BUILTIN_RECOVER:
6777 if (function != NULL)
6778 function->func_value()->set_calls_recover();
6779 else
6780 {
6781 // Calling recover outside of a function always returns the
6782 // nil empty interface.
6783 Type* eface = Type::make_empty_interface_type(loc);
6784 return Expression::make_cast(eface, Expression::make_nil(loc), loc);
6785 }
6786 break;
6787
6788 case BUILTIN_APPEND:
6789 {
6790 // Lower the varargs.
6791 const Expression_list* args = this->args();
6792 if (args == NULL || args->empty())
6793 return this;
6794 Type* slice_type = args->front()->type();
6795 if (!slice_type->is_slice_type())
6796 {
6797 error_at(args->front()->location(), "argument 1 must be a slice");
6798 this->set_is_error();
6799 return this;
6800 }
6801 Type* element_type = slice_type->array_type()->element_type();
6802 this->lower_varargs(gogo, function, inserter,
6803 Type::make_array_type(element_type, NULL),
6804 2);
6805 }
6806 break;
6807
6808 case BUILTIN_DELETE:
6809 {
6810 // Lower to a runtime function call.
6811 const Expression_list* args = this->args();
6812 if (args == NULL || args->size() < 2)
6813 this->report_error(_("not enough arguments"));
6814 else if (args->size() > 2)
6815 this->report_error(_("too many arguments"));
6816 else if (args->front()->type()->map_type() == NULL)
6817 this->report_error(_("argument 1 must be a map"));
6818 else
6819 {
6820 // Since this function returns no value it must appear in
6821 // a statement by itself, so we don't have to worry about
6822 // order of evaluation of values around it. Evaluate the
6823 // map first to get order of evaluation right.
6824 Map_type* mt = args->front()->type()->map_type();
6825 Temporary_statement* map_temp =
6826 Statement::make_temporary(mt, args->front(), loc);
6827 inserter->insert(map_temp);
6828
6829 Temporary_statement* key_temp =
6830 Statement::make_temporary(mt->key_type(), args->back(), loc);
6831 inserter->insert(key_temp);
6832
6833 Expression* e1 = Expression::make_temporary_reference(map_temp,
6834 loc);
6835 Expression* e2 = Expression::make_temporary_reference(key_temp,
6836 loc);
6837 e2 = Expression::make_unary(OPERATOR_AND, e2, loc);
6838 return Runtime::make_call(Runtime::MAPDELETE, this->location(),
6839 2, e1, e2);
6840 }
6841 }
6842 break;
6843 }
6844
6845 return this;
6846 }
6847
6848 // Lower a make expression.
6849
6850 Expression*
6851 Builtin_call_expression::lower_make()
6852 {
6853 Location loc = this->location();
6854
6855 const Expression_list* args = this->args();
6856 if (args == NULL || args->size() < 1)
6857 {
6858 this->report_error(_("not enough arguments"));
6859 return Expression::make_error(this->location());
6860 }
6861
6862 Expression_list::const_iterator parg = args->begin();
6863
6864 Expression* first_arg = *parg;
6865 if (!first_arg->is_type_expression())
6866 {
6867 error_at(first_arg->location(), "expected type");
6868 this->set_is_error();
6869 return Expression::make_error(this->location());
6870 }
6871 Type* type = first_arg->type();
6872
6873 bool is_slice = false;
6874 bool is_map = false;
6875 bool is_chan = false;
6876 if (type->is_slice_type())
6877 is_slice = true;
6878 else if (type->map_type() != NULL)
6879 is_map = true;
6880 else if (type->channel_type() != NULL)
6881 is_chan = true;
6882 else
6883 {
6884 this->report_error(_("invalid type for make function"));
6885 return Expression::make_error(this->location());
6886 }
6887
6888 bool have_big_args = false;
6889 Type* uintptr_type = Type::lookup_integer_type("uintptr");
6890 int uintptr_bits = uintptr_type->integer_type()->bits();
6891
6892 ++parg;
6893 Expression* len_arg;
6894 if (parg == args->end())
6895 {
6896 if (is_slice)
6897 {
6898 this->report_error(_("length required when allocating a slice"));
6899 return Expression::make_error(this->location());
6900 }
6901
6902 mpz_t zval;
6903 mpz_init_set_ui(zval, 0);
6904 len_arg = Expression::make_integer(&zval, NULL, loc);
6905 mpz_clear(zval);
6906 }
6907 else
6908 {
6909 len_arg = *parg;
6910 if (!this->check_int_value(len_arg))
6911 {
6912 this->report_error(_("bad size for make"));
6913 return Expression::make_error(this->location());
6914 }
6915 if (len_arg->type()->integer_type() != NULL
6916 && len_arg->type()->integer_type()->bits() > uintptr_bits)
6917 have_big_args = true;
6918 ++parg;
6919 }
6920
6921 Expression* cap_arg = NULL;
6922 if (is_slice && parg != args->end())
6923 {
6924 cap_arg = *parg;
6925 if (!this->check_int_value(cap_arg))
6926 {
6927 this->report_error(_("bad capacity when making slice"));
6928 return Expression::make_error(this->location());
6929 }
6930 if (cap_arg->type()->integer_type() != NULL
6931 && cap_arg->type()->integer_type()->bits() > uintptr_bits)
6932 have_big_args = true;
6933 ++parg;
6934 }
6935
6936 if (parg != args->end())
6937 {
6938 this->report_error(_("too many arguments to make"));
6939 return Expression::make_error(this->location());
6940 }
6941
6942 Location type_loc = first_arg->location();
6943 Expression* type_arg;
6944 if (is_slice || is_chan)
6945 type_arg = Expression::make_type_descriptor(type, type_loc);
6946 else if (is_map)
6947 type_arg = Expression::make_map_descriptor(type->map_type(), type_loc);
6948 else
6949 go_unreachable();
6950
6951 Expression* call;
6952 if (is_slice)
6953 {
6954 if (cap_arg == NULL)
6955 call = Runtime::make_call((have_big_args
6956 ? Runtime::MAKESLICE1BIG
6957 : Runtime::MAKESLICE1),
6958 loc, 2, type_arg, len_arg);
6959 else
6960 call = Runtime::make_call((have_big_args
6961 ? Runtime::MAKESLICE2BIG
6962 : Runtime::MAKESLICE2),
6963 loc, 3, type_arg, len_arg, cap_arg);
6964 }
6965 else if (is_map)
6966 call = Runtime::make_call((have_big_args
6967 ? Runtime::MAKEMAPBIG
6968 : Runtime::MAKEMAP),
6969 loc, 2, type_arg, len_arg);
6970 else if (is_chan)
6971 call = Runtime::make_call((have_big_args
6972 ? Runtime::MAKECHANBIG
6973 : Runtime::MAKECHAN),
6974 loc, 2, type_arg, len_arg);
6975 else
6976 go_unreachable();
6977
6978 return Expression::make_unsafe_cast(type, call, loc);
6979 }
6980
6981 // Return whether an expression has an integer value. Report an error
6982 // if not. This is used when handling calls to the predeclared make
6983 // function.
6984
6985 bool
6986 Builtin_call_expression::check_int_value(Expression* e)
6987 {
6988 if (e->type()->integer_type() != NULL)
6989 return true;
6990
6991 // Check for a floating point constant with integer value.
6992 Numeric_constant nc;
6993 mpz_t ival;
6994 if (e->numeric_constant_value(&nc) && nc.to_int(&ival))
6995 {
6996 mpz_clear(ival);
6997 return true;
6998 }
6999
7000 return false;
7001 }
7002
7003 // Return the type of the real or imag functions, given the type of
7004 // the argument. We need to map complex to float, complex64 to
7005 // float32, and complex128 to float64, so it has to be done by name.
7006 // This returns NULL if it can't figure out the type.
7007
7008 Type*
7009 Builtin_call_expression::real_imag_type(Type* arg_type)
7010 {
7011 if (arg_type == NULL || arg_type->is_abstract())
7012 return NULL;
7013 Named_type* nt = arg_type->named_type();
7014 if (nt == NULL)
7015 return NULL;
7016 while (nt->real_type()->named_type() != NULL)
7017 nt = nt->real_type()->named_type();
7018 if (nt->name() == "complex64")
7019 return Type::lookup_float_type("float32");
7020 else if (nt->name() == "complex128")
7021 return Type::lookup_float_type("float64");
7022 else
7023 return NULL;
7024 }
7025
7026 // Return the type of the complex function, given the type of one of the
7027 // argments. Like real_imag_type, we have to map by name.
7028
7029 Type*
7030 Builtin_call_expression::complex_type(Type* arg_type)
7031 {
7032 if (arg_type == NULL || arg_type->is_abstract())
7033 return NULL;
7034 Named_type* nt = arg_type->named_type();
7035 if (nt == NULL)
7036 return NULL;
7037 while (nt->real_type()->named_type() != NULL)
7038 nt = nt->real_type()->named_type();
7039 if (nt->name() == "float32")
7040 return Type::lookup_complex_type("complex64");
7041 else if (nt->name() == "float64")
7042 return Type::lookup_complex_type("complex128");
7043 else
7044 return NULL;
7045 }
7046
7047 // Return a single argument, or NULL if there isn't one.
7048
7049 Expression*
7050 Builtin_call_expression::one_arg() const
7051 {
7052 const Expression_list* args = this->args();
7053 if (args == NULL || args->size() != 1)
7054 return NULL;
7055 return args->front();
7056 }
7057
7058 // Return whether this is constant: len of a string, or len or cap of
7059 // a fixed array, or unsafe.Sizeof, unsafe.Offsetof, unsafe.Alignof.
7060
7061 bool
7062 Builtin_call_expression::do_is_constant() const
7063 {
7064 switch (this->code_)
7065 {
7066 case BUILTIN_LEN:
7067 case BUILTIN_CAP:
7068 {
7069 if (this->seen_)
7070 return false;
7071
7072 Expression* arg = this->one_arg();
7073 if (arg == NULL)
7074 return false;
7075 Type* arg_type = arg->type();
7076
7077 if (arg_type->points_to() != NULL
7078 && arg_type->points_to()->array_type() != NULL
7079 && !arg_type->points_to()->is_slice_type())
7080 arg_type = arg_type->points_to();
7081
7082 if (arg_type->array_type() != NULL
7083 && arg_type->array_type()->length() != NULL)
7084 return true;
7085
7086 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7087 {
7088 this->seen_ = true;
7089 bool ret = arg->is_constant();
7090 this->seen_ = false;
7091 return ret;
7092 }
7093 }
7094 break;
7095
7096 case BUILTIN_SIZEOF:
7097 case BUILTIN_ALIGNOF:
7098 return this->one_arg() != NULL;
7099
7100 case BUILTIN_OFFSETOF:
7101 {
7102 Expression* arg = this->one_arg();
7103 if (arg == NULL)
7104 return false;
7105 return arg->field_reference_expression() != NULL;
7106 }
7107
7108 case BUILTIN_COMPLEX:
7109 {
7110 const Expression_list* args = this->args();
7111 if (args != NULL && args->size() == 2)
7112 return args->front()->is_constant() && args->back()->is_constant();
7113 }
7114 break;
7115
7116 case BUILTIN_REAL:
7117 case BUILTIN_IMAG:
7118 {
7119 Expression* arg = this->one_arg();
7120 return arg != NULL && arg->is_constant();
7121 }
7122
7123 default:
7124 break;
7125 }
7126
7127 return false;
7128 }
7129
7130 // Return a numeric constant if possible.
7131
7132 bool
7133 Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const
7134 {
7135 if (this->code_ == BUILTIN_LEN
7136 || this->code_ == BUILTIN_CAP)
7137 {
7138 Expression* arg = this->one_arg();
7139 if (arg == NULL)
7140 return false;
7141 Type* arg_type = arg->type();
7142
7143 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7144 {
7145 std::string sval;
7146 if (arg->string_constant_value(&sval))
7147 {
7148 nc->set_unsigned_long(Type::lookup_integer_type("int"),
7149 sval.length());
7150 return true;
7151 }
7152 }
7153
7154 if (arg_type->points_to() != NULL
7155 && arg_type->points_to()->array_type() != NULL
7156 && !arg_type->points_to()->is_slice_type())
7157 arg_type = arg_type->points_to();
7158
7159 if (arg_type->array_type() != NULL
7160 && arg_type->array_type()->length() != NULL)
7161 {
7162 if (this->seen_)
7163 return false;
7164 Expression* e = arg_type->array_type()->length();
7165 this->seen_ = true;
7166 bool r = e->numeric_constant_value(nc);
7167 this->seen_ = false;
7168 if (r)
7169 {
7170 if (!nc->set_type(Type::lookup_integer_type("int"), false,
7171 this->location()))
7172 r = false;
7173 }
7174 return r;
7175 }
7176 }
7177 else if (this->code_ == BUILTIN_SIZEOF
7178 || this->code_ == BUILTIN_ALIGNOF)
7179 {
7180 Expression* arg = this->one_arg();
7181 if (arg == NULL)
7182 return false;
7183 Type* arg_type = arg->type();
7184 if (arg_type->is_error())
7185 return false;
7186 if (arg_type->is_abstract())
7187 return false;
7188 if (arg_type->named_type() != NULL)
7189 arg_type->named_type()->convert(this->gogo_);
7190
7191 unsigned int ret;
7192 if (this->code_ == BUILTIN_SIZEOF)
7193 {
7194 if (!arg_type->backend_type_size(this->gogo_, &ret))
7195 return false;
7196 }
7197 else if (this->code_ == BUILTIN_ALIGNOF)
7198 {
7199 if (arg->field_reference_expression() == NULL)
7200 {
7201 if (!arg_type->backend_type_align(this->gogo_, &ret))
7202 return false;
7203 }
7204 else
7205 {
7206 // Calling unsafe.Alignof(s.f) returns the alignment of
7207 // the type of f when it is used as a field in a struct.
7208 if (!arg_type->backend_type_field_align(this->gogo_, &ret))
7209 return false;
7210 }
7211 }
7212 else
7213 go_unreachable();
7214
7215 nc->set_unsigned_long(Type::lookup_integer_type("uintptr"),
7216 static_cast<unsigned long>(ret));
7217 return true;
7218 }
7219 else if (this->code_ == BUILTIN_OFFSETOF)
7220 {
7221 Expression* arg = this->one_arg();
7222 if (arg == NULL)
7223 return false;
7224 Field_reference_expression* farg = arg->field_reference_expression();
7225 if (farg == NULL)
7226 return false;
7227 Expression* struct_expr = farg->expr();
7228 Type* st = struct_expr->type();
7229 if (st->struct_type() == NULL)
7230 return false;
7231 if (st->named_type() != NULL)
7232 st->named_type()->convert(this->gogo_);
7233 unsigned int offset;
7234 if (!st->struct_type()->backend_field_offset(this->gogo_,
7235 farg->field_index(),
7236 &offset))
7237 return false;
7238 nc->set_unsigned_long(Type::lookup_integer_type("uintptr"),
7239 static_cast<unsigned long>(offset));
7240 return true;
7241 }
7242 else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7243 {
7244 Expression* arg = this->one_arg();
7245 if (arg == NULL)
7246 return false;
7247
7248 Numeric_constant argnc;
7249 if (!arg->numeric_constant_value(&argnc))
7250 return false;
7251
7252 mpfr_t real;
7253 mpfr_t imag;
7254 if (!argnc.to_complex(&real, &imag))
7255 return false;
7256
7257 Type* type = Builtin_call_expression::real_imag_type(argnc.type());
7258 if (this->code_ == BUILTIN_REAL)
7259 nc->set_float(type, real);
7260 else
7261 nc->set_float(type, imag);
7262 return true;
7263 }
7264 else if (this->code_ == BUILTIN_COMPLEX)
7265 {
7266 const Expression_list* args = this->args();
7267 if (args == NULL || args->size() != 2)
7268 return false;
7269
7270 Numeric_constant rnc;
7271 if (!args->front()->numeric_constant_value(&rnc))
7272 return false;
7273 Numeric_constant inc;
7274 if (!args->back()->numeric_constant_value(&inc))
7275 return false;
7276
7277 if (rnc.type() != NULL
7278 && !rnc.type()->is_abstract()
7279 && inc.type() != NULL
7280 && !inc.type()->is_abstract()
7281 && !Type::are_identical(rnc.type(), inc.type(), false, NULL))
7282 return false;
7283
7284 mpfr_t r;
7285 if (!rnc.to_float(&r))
7286 return false;
7287 mpfr_t i;
7288 if (!inc.to_float(&i))
7289 {
7290 mpfr_clear(r);
7291 return false;
7292 }
7293
7294 Type* arg_type = rnc.type();
7295 if (arg_type == NULL || arg_type->is_abstract())
7296 arg_type = inc.type();
7297
7298 Type* type = Builtin_call_expression::complex_type(arg_type);
7299 nc->set_complex(type, r, i);
7300
7301 mpfr_clear(r);
7302 mpfr_clear(i);
7303
7304 return true;
7305 }
7306
7307 return false;
7308 }
7309
7310 // Give an error if we are discarding the value of an expression which
7311 // should not normally be discarded. We don't give an error for
7312 // discarding the value of an ordinary function call, but we do for
7313 // builtin functions, purely for consistency with the gc compiler.
7314
7315 void
7316 Builtin_call_expression::do_discarding_value()
7317 {
7318 switch (this->code_)
7319 {
7320 case BUILTIN_INVALID:
7321 default:
7322 go_unreachable();
7323
7324 case BUILTIN_APPEND:
7325 case BUILTIN_CAP:
7326 case BUILTIN_COMPLEX:
7327 case BUILTIN_IMAG:
7328 case BUILTIN_LEN:
7329 case BUILTIN_MAKE:
7330 case BUILTIN_NEW:
7331 case BUILTIN_REAL:
7332 case BUILTIN_ALIGNOF:
7333 case BUILTIN_OFFSETOF:
7334 case BUILTIN_SIZEOF:
7335 this->unused_value_error();
7336 break;
7337
7338 case BUILTIN_CLOSE:
7339 case BUILTIN_COPY:
7340 case BUILTIN_DELETE:
7341 case BUILTIN_PANIC:
7342 case BUILTIN_PRINT:
7343 case BUILTIN_PRINTLN:
7344 case BUILTIN_RECOVER:
7345 break;
7346 }
7347 }
7348
7349 // Return the type.
7350
7351 Type*
7352 Builtin_call_expression::do_type()
7353 {
7354 switch (this->code_)
7355 {
7356 case BUILTIN_INVALID:
7357 default:
7358 go_unreachable();
7359
7360 case BUILTIN_NEW:
7361 case BUILTIN_MAKE:
7362 {
7363 const Expression_list* args = this->args();
7364 if (args == NULL || args->empty())
7365 return Type::make_error_type();
7366 return Type::make_pointer_type(args->front()->type());
7367 }
7368
7369 case BUILTIN_CAP:
7370 case BUILTIN_COPY:
7371 case BUILTIN_LEN:
7372 return Type::lookup_integer_type("int");
7373
7374 case BUILTIN_ALIGNOF:
7375 case BUILTIN_OFFSETOF:
7376 case BUILTIN_SIZEOF:
7377 return Type::lookup_integer_type("uintptr");
7378
7379 case BUILTIN_CLOSE:
7380 case BUILTIN_DELETE:
7381 case BUILTIN_PANIC:
7382 case BUILTIN_PRINT:
7383 case BUILTIN_PRINTLN:
7384 return Type::make_void_type();
7385
7386 case BUILTIN_RECOVER:
7387 return Type::make_empty_interface_type(Linemap::predeclared_location());
7388
7389 case BUILTIN_APPEND:
7390 {
7391 const Expression_list* args = this->args();
7392 if (args == NULL || args->empty())
7393 return Type::make_error_type();
7394 return args->front()->type();
7395 }
7396
7397 case BUILTIN_REAL:
7398 case BUILTIN_IMAG:
7399 {
7400 Expression* arg = this->one_arg();
7401 if (arg == NULL)
7402 return Type::make_error_type();
7403 Type* t = arg->type();
7404 if (t->is_abstract())
7405 t = t->make_non_abstract_type();
7406 t = Builtin_call_expression::real_imag_type(t);
7407 if (t == NULL)
7408 t = Type::make_error_type();
7409 return t;
7410 }
7411
7412 case BUILTIN_COMPLEX:
7413 {
7414 const Expression_list* args = this->args();
7415 if (args == NULL || args->size() != 2)
7416 return Type::make_error_type();
7417 Type* t = args->front()->type();
7418 if (t->is_abstract())
7419 {
7420 t = args->back()->type();
7421 if (t->is_abstract())
7422 t = t->make_non_abstract_type();
7423 }
7424 t = Builtin_call_expression::complex_type(t);
7425 if (t == NULL)
7426 t = Type::make_error_type();
7427 return t;
7428 }
7429 }
7430 }
7431
7432 // Determine the type.
7433
7434 void
7435 Builtin_call_expression::do_determine_type(const Type_context* context)
7436 {
7437 if (!this->determining_types())
7438 return;
7439
7440 this->fn()->determine_type_no_context();
7441
7442 const Expression_list* args = this->args();
7443
7444 bool is_print;
7445 Type* arg_type = NULL;
7446 switch (this->code_)
7447 {
7448 case BUILTIN_PRINT:
7449 case BUILTIN_PRINTLN:
7450 // Do not force a large integer constant to "int".
7451 is_print = true;
7452 break;
7453
7454 case BUILTIN_REAL:
7455 case BUILTIN_IMAG:
7456 arg_type = Builtin_call_expression::complex_type(context->type);
7457 is_print = false;
7458 break;
7459
7460 case BUILTIN_COMPLEX:
7461 {
7462 // For the complex function the type of one operand can
7463 // determine the type of the other, as in a binary expression.
7464 arg_type = Builtin_call_expression::real_imag_type(context->type);
7465 if (args != NULL && args->size() == 2)
7466 {
7467 Type* t1 = args->front()->type();
7468 Type* t2 = args->front()->type();
7469 if (!t1->is_abstract())
7470 arg_type = t1;
7471 else if (!t2->is_abstract())
7472 arg_type = t2;
7473 }
7474 is_print = false;
7475 }
7476 break;
7477
7478 default:
7479 is_print = false;
7480 break;
7481 }
7482
7483 if (args != NULL)
7484 {
7485 for (Expression_list::const_iterator pa = args->begin();
7486 pa != args->end();
7487 ++pa)
7488 {
7489 Type_context subcontext;
7490 subcontext.type = arg_type;
7491
7492 if (is_print)
7493 {
7494 // We want to print large constants, we so can't just
7495 // use the appropriate nonabstract type. Use uint64 for
7496 // an integer if we know it is nonnegative, otherwise
7497 // use int64 for a integer, otherwise use float64 for a
7498 // float or complex128 for a complex.
7499 Type* want_type = NULL;
7500 Type* atype = (*pa)->type();
7501 if (atype->is_abstract())
7502 {
7503 if (atype->integer_type() != NULL)
7504 {
7505 Numeric_constant nc;
7506 if (this->numeric_constant_value(&nc))
7507 {
7508 mpz_t val;
7509 if (nc.to_int(&val))
7510 {
7511 if (mpz_sgn(val) >= 0)
7512 want_type = Type::lookup_integer_type("uint64");
7513 mpz_clear(val);
7514 }
7515 }
7516 if (want_type == NULL)
7517 want_type = Type::lookup_integer_type("int64");
7518 }
7519 else if (atype->float_type() != NULL)
7520 want_type = Type::lookup_float_type("float64");
7521 else if (atype->complex_type() != NULL)
7522 want_type = Type::lookup_complex_type("complex128");
7523 else if (atype->is_abstract_string_type())
7524 want_type = Type::lookup_string_type();
7525 else if (atype->is_abstract_boolean_type())
7526 want_type = Type::lookup_bool_type();
7527 else
7528 go_unreachable();
7529 subcontext.type = want_type;
7530 }
7531 }
7532
7533 (*pa)->determine_type(&subcontext);
7534 }
7535 }
7536 }
7537
7538 // If there is exactly one argument, return true. Otherwise give an
7539 // error message and return false.
7540
7541 bool
7542 Builtin_call_expression::check_one_arg()
7543 {
7544 const Expression_list* args = this->args();
7545 if (args == NULL || args->size() < 1)
7546 {
7547 this->report_error(_("not enough arguments"));
7548 return false;
7549 }
7550 else if (args->size() > 1)
7551 {
7552 this->report_error(_("too many arguments"));
7553 return false;
7554 }
7555 if (args->front()->is_error_expression()
7556 || args->front()->type()->is_error())
7557 {
7558 this->set_is_error();
7559 return false;
7560 }
7561 return true;
7562 }
7563
7564 // Check argument types for a builtin function.
7565
7566 void
7567 Builtin_call_expression::do_check_types(Gogo*)
7568 {
7569 if (this->is_error_expression())
7570 return;
7571 switch (this->code_)
7572 {
7573 case BUILTIN_INVALID:
7574 case BUILTIN_NEW:
7575 case BUILTIN_MAKE:
7576 case BUILTIN_DELETE:
7577 return;
7578
7579 case BUILTIN_LEN:
7580 case BUILTIN_CAP:
7581 {
7582 // The single argument may be either a string or an array or a
7583 // map or a channel, or a pointer to a closed array.
7584 if (this->check_one_arg())
7585 {
7586 Type* arg_type = this->one_arg()->type();
7587 if (arg_type->points_to() != NULL
7588 && arg_type->points_to()->array_type() != NULL
7589 && !arg_type->points_to()->is_slice_type())
7590 arg_type = arg_type->points_to();
7591 if (this->code_ == BUILTIN_CAP)
7592 {
7593 if (!arg_type->is_error()
7594 && arg_type->array_type() == NULL
7595 && arg_type->channel_type() == NULL)
7596 this->report_error(_("argument must be array or slice "
7597 "or channel"));
7598 }
7599 else
7600 {
7601 if (!arg_type->is_error()
7602 && !arg_type->is_string_type()
7603 && arg_type->array_type() == NULL
7604 && arg_type->map_type() == NULL
7605 && arg_type->channel_type() == NULL)
7606 this->report_error(_("argument must be string or "
7607 "array or slice or map or channel"));
7608 }
7609 }
7610 }
7611 break;
7612
7613 case BUILTIN_PRINT:
7614 case BUILTIN_PRINTLN:
7615 {
7616 const Expression_list* args = this->args();
7617 if (args == NULL)
7618 {
7619 if (this->code_ == BUILTIN_PRINT)
7620 warning_at(this->location(), 0,
7621 "no arguments for builtin function %<%s%>",
7622 (this->code_ == BUILTIN_PRINT
7623 ? "print"
7624 : "println"));
7625 }
7626 else
7627 {
7628 for (Expression_list::const_iterator p = args->begin();
7629 p != args->end();
7630 ++p)
7631 {
7632 Type* type = (*p)->type();
7633 if (type->is_error()
7634 || type->is_string_type()
7635 || type->integer_type() != NULL
7636 || type->float_type() != NULL
7637 || type->complex_type() != NULL
7638 || type->is_boolean_type()
7639 || type->points_to() != NULL
7640 || type->interface_type() != NULL
7641 || type->channel_type() != NULL
7642 || type->map_type() != NULL
7643 || type->function_type() != NULL
7644 || type->is_slice_type())
7645 ;
7646 else if ((*p)->is_type_expression())
7647 {
7648 // If this is a type expression it's going to give
7649 // an error anyhow, so we don't need one here.
7650 }
7651 else
7652 this->report_error(_("unsupported argument type to "
7653 "builtin function"));
7654 }
7655 }
7656 }
7657 break;
7658
7659 case BUILTIN_CLOSE:
7660 if (this->check_one_arg())
7661 {
7662 if (this->one_arg()->type()->channel_type() == NULL)
7663 this->report_error(_("argument must be channel"));
7664 else if (!this->one_arg()->type()->channel_type()->may_send())
7665 this->report_error(_("cannot close receive-only channel"));
7666 }
7667 break;
7668
7669 case BUILTIN_PANIC:
7670 case BUILTIN_SIZEOF:
7671 case BUILTIN_ALIGNOF:
7672 this->check_one_arg();
7673 break;
7674
7675 case BUILTIN_RECOVER:
7676 if (this->args() != NULL && !this->args()->empty())
7677 this->report_error(_("too many arguments"));
7678 break;
7679
7680 case BUILTIN_OFFSETOF:
7681 if (this->check_one_arg())
7682 {
7683 Expression* arg = this->one_arg();
7684 if (arg->field_reference_expression() == NULL)
7685 this->report_error(_("argument must be a field reference"));
7686 }
7687 break;
7688
7689 case BUILTIN_COPY:
7690 {
7691 const Expression_list* args = this->args();
7692 if (args == NULL || args->size() < 2)
7693 {
7694 this->report_error(_("not enough arguments"));
7695 break;
7696 }
7697 else if (args->size() > 2)
7698 {
7699 this->report_error(_("too many arguments"));
7700 break;
7701 }
7702 Type* arg1_type = args->front()->type();
7703 Type* arg2_type = args->back()->type();
7704 if (arg1_type->is_error() || arg2_type->is_error())
7705 break;
7706
7707 Type* e1;
7708 if (arg1_type->is_slice_type())
7709 e1 = arg1_type->array_type()->element_type();
7710 else
7711 {
7712 this->report_error(_("left argument must be a slice"));
7713 break;
7714 }
7715
7716 if (arg2_type->is_slice_type())
7717 {
7718 Type* e2 = arg2_type->array_type()->element_type();
7719 if (!Type::are_identical(e1, e2, true, NULL))
7720 this->report_error(_("element types must be the same"));
7721 }
7722 else if (arg2_type->is_string_type())
7723 {
7724 if (e1->integer_type() == NULL || !e1->integer_type()->is_byte())
7725 this->report_error(_("first argument must be []byte"));
7726 }
7727 else
7728 this->report_error(_("second argument must be slice or string"));
7729 }
7730 break;
7731
7732 case BUILTIN_APPEND:
7733 {
7734 const Expression_list* args = this->args();
7735 if (args == NULL || args->size() < 2)
7736 {
7737 this->report_error(_("not enough arguments"));
7738 break;
7739 }
7740 if (args->size() > 2)
7741 {
7742 this->report_error(_("too many arguments"));
7743 break;
7744 }
7745 if (args->front()->type()->is_error()
7746 || args->back()->type()->is_error())
7747 break;
7748
7749 Array_type* at = args->front()->type()->array_type();
7750 Type* e = at->element_type();
7751
7752 // The language permits appending a string to a []byte, as a
7753 // special case.
7754 if (args->back()->type()->is_string_type())
7755 {
7756 if (e->integer_type() != NULL && e->integer_type()->is_byte())
7757 break;
7758 }
7759
7760 // The language says that the second argument must be
7761 // assignable to a slice of the element type of the first
7762 // argument. We already know the first argument is a slice
7763 // type.
7764 Type* arg2_type = Type::make_array_type(e, NULL);
7765 std::string reason;
7766 if (!Type::are_assignable(arg2_type, args->back()->type(), &reason))
7767 {
7768 if (reason.empty())
7769 this->report_error(_("argument 2 has invalid type"));
7770 else
7771 {
7772 error_at(this->location(), "argument 2 has invalid type (%s)",
7773 reason.c_str());
7774 this->set_is_error();
7775 }
7776 }
7777 break;
7778 }
7779
7780 case BUILTIN_REAL:
7781 case BUILTIN_IMAG:
7782 if (this->check_one_arg())
7783 {
7784 if (this->one_arg()->type()->complex_type() == NULL)
7785 this->report_error(_("argument must have complex type"));
7786 }
7787 break;
7788
7789 case BUILTIN_COMPLEX:
7790 {
7791 const Expression_list* args = this->args();
7792 if (args == NULL || args->size() < 2)
7793 this->report_error(_("not enough arguments"));
7794 else if (args->size() > 2)
7795 this->report_error(_("too many arguments"));
7796 else if (args->front()->is_error_expression()
7797 || args->front()->type()->is_error()
7798 || args->back()->is_error_expression()
7799 || args->back()->type()->is_error())
7800 this->set_is_error();
7801 else if (!Type::are_identical(args->front()->type(),
7802 args->back()->type(), true, NULL))
7803 this->report_error(_("complex arguments must have identical types"));
7804 else if (args->front()->type()->float_type() == NULL)
7805 this->report_error(_("complex arguments must have "
7806 "floating-point type"));
7807 }
7808 break;
7809
7810 default:
7811 go_unreachable();
7812 }
7813 }
7814
7815 // Return the tree for a builtin function.
7816
7817 tree
7818 Builtin_call_expression::do_get_tree(Translate_context* context)
7819 {
7820 Gogo* gogo = context->gogo();
7821 Location location = this->location();
7822 switch (this->code_)
7823 {
7824 case BUILTIN_INVALID:
7825 case BUILTIN_NEW:
7826 case BUILTIN_MAKE:
7827 go_unreachable();
7828
7829 case BUILTIN_LEN:
7830 case BUILTIN_CAP:
7831 {
7832 const Expression_list* args = this->args();
7833 go_assert(args != NULL && args->size() == 1);
7834 Expression* arg = *args->begin();
7835 Type* arg_type = arg->type();
7836
7837 if (this->seen_)
7838 {
7839 go_assert(saw_errors());
7840 return error_mark_node;
7841 }
7842 this->seen_ = true;
7843
7844 tree arg_tree = arg->get_tree(context);
7845
7846 this->seen_ = false;
7847
7848 if (arg_tree == error_mark_node)
7849 return error_mark_node;
7850
7851 if (arg_type->points_to() != NULL)
7852 {
7853 arg_type = arg_type->points_to();
7854 go_assert(arg_type->array_type() != NULL
7855 && !arg_type->is_slice_type());
7856 go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7857 arg_tree = build_fold_indirect_ref(arg_tree);
7858 }
7859
7860 tree val_tree;
7861 if (this->code_ == BUILTIN_LEN)
7862 {
7863 if (arg_type->is_string_type())
7864 val_tree = String_type::length_tree(gogo, arg_tree);
7865 else if (arg_type->array_type() != NULL)
7866 {
7867 if (this->seen_)
7868 {
7869 go_assert(saw_errors());
7870 return error_mark_node;
7871 }
7872 this->seen_ = true;
7873 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
7874 this->seen_ = false;
7875 }
7876 else if (arg_type->map_type() != NULL)
7877 {
7878 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
7879 static tree map_len_fndecl;
7880 val_tree = Gogo::call_builtin(&map_len_fndecl,
7881 location,
7882 "__go_map_len",
7883 1,
7884 integer_type_node,
7885 arg_type_tree,
7886 arg_tree);
7887 }
7888 else if (arg_type->channel_type() != NULL)
7889 {
7890 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
7891 static tree chan_len_fndecl;
7892 val_tree = Gogo::call_builtin(&chan_len_fndecl,
7893 location,
7894 "__go_chan_len",
7895 1,
7896 integer_type_node,
7897 arg_type_tree,
7898 arg_tree);
7899 }
7900 else
7901 go_unreachable();
7902 }
7903 else
7904 {
7905 if (arg_type->array_type() != NULL)
7906 {
7907 if (this->seen_)
7908 {
7909 go_assert(saw_errors());
7910 return error_mark_node;
7911 }
7912 this->seen_ = true;
7913 val_tree = arg_type->array_type()->capacity_tree(gogo,
7914 arg_tree);
7915 this->seen_ = false;
7916 }
7917 else if (arg_type->channel_type() != NULL)
7918 {
7919 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
7920 static tree chan_cap_fndecl;
7921 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
7922 location,
7923 "__go_chan_cap",
7924 1,
7925 integer_type_node,
7926 arg_type_tree,
7927 arg_tree);
7928 }
7929 else
7930 go_unreachable();
7931 }
7932
7933 if (val_tree == error_mark_node)
7934 return error_mark_node;
7935
7936 Type* int_type = Type::lookup_integer_type("int");
7937 tree type_tree = type_to_tree(int_type->get_backend(gogo));
7938 if (type_tree == TREE_TYPE(val_tree))
7939 return val_tree;
7940 else
7941 return fold(convert_to_integer(type_tree, val_tree));
7942 }
7943
7944 case BUILTIN_PRINT:
7945 case BUILTIN_PRINTLN:
7946 {
7947 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
7948 tree stmt_list = NULL_TREE;
7949
7950 const Expression_list* call_args = this->args();
7951 if (call_args != NULL)
7952 {
7953 for (Expression_list::const_iterator p = call_args->begin();
7954 p != call_args->end();
7955 ++p)
7956 {
7957 if (is_ln && p != call_args->begin())
7958 {
7959 static tree print_space_fndecl;
7960 tree call = Gogo::call_builtin(&print_space_fndecl,
7961 location,
7962 "__go_print_space",
7963 0,
7964 void_type_node);
7965 if (call == error_mark_node)
7966 return error_mark_node;
7967 append_to_statement_list(call, &stmt_list);
7968 }
7969
7970 Type* type = (*p)->type();
7971
7972 tree arg = (*p)->get_tree(context);
7973 if (arg == error_mark_node)
7974 return error_mark_node;
7975
7976 tree* pfndecl;
7977 const char* fnname;
7978 if (type->is_string_type())
7979 {
7980 static tree print_string_fndecl;
7981 pfndecl = &print_string_fndecl;
7982 fnname = "__go_print_string";
7983 }
7984 else if (type->integer_type() != NULL
7985 && type->integer_type()->is_unsigned())
7986 {
7987 static tree print_uint64_fndecl;
7988 pfndecl = &print_uint64_fndecl;
7989 fnname = "__go_print_uint64";
7990 Type* itype = Type::lookup_integer_type("uint64");
7991 Btype* bitype = itype->get_backend(gogo);
7992 arg = fold_convert_loc(location.gcc_location(),
7993 type_to_tree(bitype), arg);
7994 }
7995 else if (type->integer_type() != NULL)
7996 {
7997 static tree print_int64_fndecl;
7998 pfndecl = &print_int64_fndecl;
7999 fnname = "__go_print_int64";
8000 Type* itype = Type::lookup_integer_type("int64");
8001 Btype* bitype = itype->get_backend(gogo);
8002 arg = fold_convert_loc(location.gcc_location(),
8003 type_to_tree(bitype), arg);
8004 }
8005 else if (type->float_type() != NULL)
8006 {
8007 static tree print_double_fndecl;
8008 pfndecl = &print_double_fndecl;
8009 fnname = "__go_print_double";
8010 arg = fold_convert_loc(location.gcc_location(),
8011 double_type_node, arg);
8012 }
8013 else if (type->complex_type() != NULL)
8014 {
8015 static tree print_complex_fndecl;
8016 pfndecl = &print_complex_fndecl;
8017 fnname = "__go_print_complex";
8018 arg = fold_convert_loc(location.gcc_location(),
8019 complex_double_type_node, arg);
8020 }
8021 else if (type->is_boolean_type())
8022 {
8023 static tree print_bool_fndecl;
8024 pfndecl = &print_bool_fndecl;
8025 fnname = "__go_print_bool";
8026 }
8027 else if (type->points_to() != NULL
8028 || type->channel_type() != NULL
8029 || type->map_type() != NULL
8030 || type->function_type() != NULL)
8031 {
8032 static tree print_pointer_fndecl;
8033 pfndecl = &print_pointer_fndecl;
8034 fnname = "__go_print_pointer";
8035 arg = fold_convert_loc(location.gcc_location(),
8036 ptr_type_node, arg);
8037 }
8038 else if (type->interface_type() != NULL)
8039 {
8040 if (type->interface_type()->is_empty())
8041 {
8042 static tree print_empty_interface_fndecl;
8043 pfndecl = &print_empty_interface_fndecl;
8044 fnname = "__go_print_empty_interface";
8045 }
8046 else
8047 {
8048 static tree print_interface_fndecl;
8049 pfndecl = &print_interface_fndecl;
8050 fnname = "__go_print_interface";
8051 }
8052 }
8053 else if (type->is_slice_type())
8054 {
8055 static tree print_slice_fndecl;
8056 pfndecl = &print_slice_fndecl;
8057 fnname = "__go_print_slice";
8058 }
8059 else
8060 {
8061 go_assert(saw_errors());
8062 return error_mark_node;
8063 }
8064
8065 tree call = Gogo::call_builtin(pfndecl,
8066 location,
8067 fnname,
8068 1,
8069 void_type_node,
8070 TREE_TYPE(arg),
8071 arg);
8072 if (call == error_mark_node)
8073 return error_mark_node;
8074 append_to_statement_list(call, &stmt_list);
8075 }
8076 }
8077
8078 if (is_ln)
8079 {
8080 static tree print_nl_fndecl;
8081 tree call = Gogo::call_builtin(&print_nl_fndecl,
8082 location,
8083 "__go_print_nl",
8084 0,
8085 void_type_node);
8086 if (call == error_mark_node)
8087 return error_mark_node;
8088 append_to_statement_list(call, &stmt_list);
8089 }
8090
8091 return stmt_list;
8092 }
8093
8094 case BUILTIN_PANIC:
8095 {
8096 const Expression_list* args = this->args();
8097 go_assert(args != NULL && args->size() == 1);
8098 Expression* arg = args->front();
8099 tree arg_tree = arg->get_tree(context);
8100 if (arg_tree == error_mark_node)
8101 return error_mark_node;
8102 Type *empty =
8103 Type::make_empty_interface_type(Linemap::predeclared_location());
8104 arg_tree = Expression::convert_for_assignment(context, empty,
8105 arg->type(),
8106 arg_tree, location);
8107 static tree panic_fndecl;
8108 tree call = Gogo::call_builtin(&panic_fndecl,
8109 location,
8110 "__go_panic",
8111 1,
8112 void_type_node,
8113 TREE_TYPE(arg_tree),
8114 arg_tree);
8115 if (call == error_mark_node)
8116 return error_mark_node;
8117 // This function will throw an exception.
8118 TREE_NOTHROW(panic_fndecl) = 0;
8119 // This function will not return.
8120 TREE_THIS_VOLATILE(panic_fndecl) = 1;
8121 return call;
8122 }
8123
8124 case BUILTIN_RECOVER:
8125 {
8126 // The argument is set when building recover thunks. It's a
8127 // boolean value which is true if we can recover a value now.
8128 const Expression_list* args = this->args();
8129 go_assert(args != NULL && args->size() == 1);
8130 Expression* arg = args->front();
8131 tree arg_tree = arg->get_tree(context);
8132 if (arg_tree == error_mark_node)
8133 return error_mark_node;
8134
8135 Type *empty =
8136 Type::make_empty_interface_type(Linemap::predeclared_location());
8137 tree empty_tree = type_to_tree(empty->get_backend(context->gogo()));
8138
8139 Type* nil_type = Type::make_nil_type();
8140 Expression* nil = Expression::make_nil(location);
8141 tree nil_tree = nil->get_tree(context);
8142 tree empty_nil_tree = Expression::convert_for_assignment(context,
8143 empty,
8144 nil_type,
8145 nil_tree,
8146 location);
8147
8148 // We need to handle a deferred call to recover specially,
8149 // because it changes whether it can recover a panic or not.
8150 // See test7 in test/recover1.go.
8151 tree call;
8152 if (this->is_deferred())
8153 {
8154 static tree deferred_recover_fndecl;
8155 call = Gogo::call_builtin(&deferred_recover_fndecl,
8156 location,
8157 "__go_deferred_recover",
8158 0,
8159 empty_tree);
8160 }
8161 else
8162 {
8163 static tree recover_fndecl;
8164 call = Gogo::call_builtin(&recover_fndecl,
8165 location,
8166 "__go_recover",
8167 0,
8168 empty_tree);
8169 }
8170 if (call == error_mark_node)
8171 return error_mark_node;
8172 return fold_build3_loc(location.gcc_location(), COND_EXPR, empty_tree,
8173 arg_tree, call, empty_nil_tree);
8174 }
8175
8176 case BUILTIN_CLOSE:
8177 {
8178 const Expression_list* args = this->args();
8179 go_assert(args != NULL && args->size() == 1);
8180 Expression* arg = args->front();
8181 tree arg_tree = arg->get_tree(context);
8182 if (arg_tree == error_mark_node)
8183 return error_mark_node;
8184 static tree close_fndecl;
8185 return Gogo::call_builtin(&close_fndecl,
8186 location,
8187 "__go_builtin_close",
8188 1,
8189 void_type_node,
8190 TREE_TYPE(arg_tree),
8191 arg_tree);
8192 }
8193
8194 case BUILTIN_SIZEOF:
8195 case BUILTIN_OFFSETOF:
8196 case BUILTIN_ALIGNOF:
8197 {
8198 Numeric_constant nc;
8199 unsigned long val;
8200 if (!this->numeric_constant_value(&nc)
8201 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
8202 {
8203 go_assert(saw_errors());
8204 return error_mark_node;
8205 }
8206 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8207 tree type = type_to_tree(uintptr_type->get_backend(gogo));
8208 return build_int_cst(type, val);
8209 }
8210
8211 case BUILTIN_COPY:
8212 {
8213 const Expression_list* args = this->args();
8214 go_assert(args != NULL && args->size() == 2);
8215 Expression* arg1 = args->front();
8216 Expression* arg2 = args->back();
8217
8218 tree arg1_tree = arg1->get_tree(context);
8219 tree arg2_tree = arg2->get_tree(context);
8220 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8221 return error_mark_node;
8222
8223 Type* arg1_type = arg1->type();
8224 Array_type* at = arg1_type->array_type();
8225 arg1_tree = save_expr(arg1_tree);
8226 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
8227 tree arg1_len = at->length_tree(gogo, arg1_tree);
8228 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
8229 return error_mark_node;
8230
8231 Type* arg2_type = arg2->type();
8232 tree arg2_val;
8233 tree arg2_len;
8234 if (arg2_type->is_slice_type())
8235 {
8236 at = arg2_type->array_type();
8237 arg2_tree = save_expr(arg2_tree);
8238 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8239 arg2_len = at->length_tree(gogo, arg2_tree);
8240 }
8241 else
8242 {
8243 arg2_tree = save_expr(arg2_tree);
8244 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8245 arg2_len = String_type::length_tree(gogo, arg2_tree);
8246 }
8247 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8248 return error_mark_node;
8249
8250 arg1_len = save_expr(arg1_len);
8251 arg2_len = save_expr(arg2_len);
8252 tree len = fold_build3_loc(location.gcc_location(), COND_EXPR,
8253 TREE_TYPE(arg1_len),
8254 fold_build2_loc(location.gcc_location(),
8255 LT_EXPR, boolean_type_node,
8256 arg1_len, arg2_len),
8257 arg1_len, arg2_len);
8258 len = save_expr(len);
8259
8260 Type* element_type = at->element_type();
8261 Btype* element_btype = element_type->get_backend(gogo);
8262 tree element_type_tree = type_to_tree(element_btype);
8263 if (element_type_tree == error_mark_node)
8264 return error_mark_node;
8265 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8266 tree bytecount = fold_convert_loc(location.gcc_location(),
8267 TREE_TYPE(element_size), len);
8268 bytecount = fold_build2_loc(location.gcc_location(), MULT_EXPR,
8269 TREE_TYPE(element_size),
8270 bytecount, element_size);
8271 bytecount = fold_convert_loc(location.gcc_location(), size_type_node,
8272 bytecount);
8273
8274 arg1_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8275 arg1_val);
8276 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8277 arg2_val);
8278
8279 static tree copy_fndecl;
8280 tree call = Gogo::call_builtin(&copy_fndecl,
8281 location,
8282 "__go_copy",
8283 3,
8284 void_type_node,
8285 ptr_type_node,
8286 arg1_val,
8287 ptr_type_node,
8288 arg2_val,
8289 size_type_node,
8290 bytecount);
8291 if (call == error_mark_node)
8292 return error_mark_node;
8293
8294 return fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
8295 TREE_TYPE(len), call, len);
8296 }
8297
8298 case BUILTIN_APPEND:
8299 {
8300 const Expression_list* args = this->args();
8301 go_assert(args != NULL && args->size() == 2);
8302 Expression* arg1 = args->front();
8303 Expression* arg2 = args->back();
8304
8305 tree arg1_tree = arg1->get_tree(context);
8306 tree arg2_tree = arg2->get_tree(context);
8307 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8308 return error_mark_node;
8309
8310 Array_type* at = arg1->type()->array_type();
8311 Type* element_type = at->element_type()->forwarded();
8312
8313 tree arg2_val;
8314 tree arg2_len;
8315 tree element_size;
8316 if (arg2->type()->is_string_type()
8317 && element_type->integer_type() != NULL
8318 && element_type->integer_type()->is_byte())
8319 {
8320 arg2_tree = save_expr(arg2_tree);
8321 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8322 arg2_len = String_type::length_tree(gogo, arg2_tree);
8323 element_size = size_int(1);
8324 }
8325 else
8326 {
8327 arg2_tree = Expression::convert_for_assignment(context, at,
8328 arg2->type(),
8329 arg2_tree,
8330 location);
8331 if (arg2_tree == error_mark_node)
8332 return error_mark_node;
8333
8334 arg2_tree = save_expr(arg2_tree);
8335
8336 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8337 arg2_len = at->length_tree(gogo, arg2_tree);
8338
8339 Btype* element_btype = element_type->get_backend(gogo);
8340 tree element_type_tree = type_to_tree(element_btype);
8341 if (element_type_tree == error_mark_node)
8342 return error_mark_node;
8343 element_size = TYPE_SIZE_UNIT(element_type_tree);
8344 }
8345
8346 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8347 arg2_val);
8348 arg2_len = fold_convert_loc(location.gcc_location(), size_type_node,
8349 arg2_len);
8350 element_size = fold_convert_loc(location.gcc_location(), size_type_node,
8351 element_size);
8352
8353 if (arg2_val == error_mark_node
8354 || arg2_len == error_mark_node
8355 || element_size == error_mark_node)
8356 return error_mark_node;
8357
8358 // We rebuild the decl each time since the slice types may
8359 // change.
8360 tree append_fndecl = NULL_TREE;
8361 return Gogo::call_builtin(&append_fndecl,
8362 location,
8363 "__go_append",
8364 4,
8365 TREE_TYPE(arg1_tree),
8366 TREE_TYPE(arg1_tree),
8367 arg1_tree,
8368 ptr_type_node,
8369 arg2_val,
8370 size_type_node,
8371 arg2_len,
8372 size_type_node,
8373 element_size);
8374 }
8375
8376 case BUILTIN_REAL:
8377 case BUILTIN_IMAG:
8378 {
8379 const Expression_list* args = this->args();
8380 go_assert(args != NULL && args->size() == 1);
8381 Expression* arg = args->front();
8382 tree arg_tree = arg->get_tree(context);
8383 if (arg_tree == error_mark_node)
8384 return error_mark_node;
8385 go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
8386 if (this->code_ == BUILTIN_REAL)
8387 return fold_build1_loc(location.gcc_location(), REALPART_EXPR,
8388 TREE_TYPE(TREE_TYPE(arg_tree)),
8389 arg_tree);
8390 else
8391 return fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
8392 TREE_TYPE(TREE_TYPE(arg_tree)),
8393 arg_tree);
8394 }
8395
8396 case BUILTIN_COMPLEX:
8397 {
8398 const Expression_list* args = this->args();
8399 go_assert(args != NULL && args->size() == 2);
8400 tree r = args->front()->get_tree(context);
8401 tree i = args->back()->get_tree(context);
8402 if (r == error_mark_node || i == error_mark_node)
8403 return error_mark_node;
8404 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
8405 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
8406 go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8407 return fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
8408 build_complex_type(TREE_TYPE(r)),
8409 r, i);
8410 }
8411
8412 default:
8413 go_unreachable();
8414 }
8415 }
8416
8417 // We have to support exporting a builtin call expression, because
8418 // code can set a constant to the result of a builtin expression.
8419
8420 void
8421 Builtin_call_expression::do_export(Export* exp) const
8422 {
8423 Numeric_constant nc;
8424 if (!this->numeric_constant_value(&nc))
8425 {
8426 error_at(this->location(), "value is not constant");
8427 return;
8428 }
8429
8430 if (nc.is_int())
8431 {
8432 mpz_t val;
8433 nc.get_int(&val);
8434 Integer_expression::export_integer(exp, val);
8435 mpz_clear(val);
8436 }
8437 else if (nc.is_float())
8438 {
8439 mpfr_t fval;
8440 nc.get_float(&fval);
8441 Float_expression::export_float(exp, fval);
8442 mpfr_clear(fval);
8443 }
8444 else if (nc.is_complex())
8445 {
8446 mpfr_t real;
8447 mpfr_t imag;
8448 Complex_expression::export_complex(exp, real, imag);
8449 mpfr_clear(real);
8450 mpfr_clear(imag);
8451 }
8452 else
8453 go_unreachable();
8454
8455 // A trailing space lets us reliably identify the end of the number.
8456 exp->write_c_string(" ");
8457 }
8458
8459 // Class Call_expression.
8460
8461 // Traversal.
8462
8463 int
8464 Call_expression::do_traverse(Traverse* traverse)
8465 {
8466 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
8467 return TRAVERSE_EXIT;
8468 if (this->args_ != NULL)
8469 {
8470 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
8471 return TRAVERSE_EXIT;
8472 }
8473 return TRAVERSE_CONTINUE;
8474 }
8475
8476 // Lower a call statement.
8477
8478 Expression*
8479 Call_expression::do_lower(Gogo* gogo, Named_object* function,
8480 Statement_inserter* inserter, int)
8481 {
8482 Location loc = this->location();
8483
8484 // A type cast can look like a function call.
8485 if (this->fn_->is_type_expression()
8486 && this->args_ != NULL
8487 && this->args_->size() == 1)
8488 return Expression::make_cast(this->fn_->type(), this->args_->front(),
8489 loc);
8490
8491 // Recognize a call to a builtin function.
8492 Func_expression* fne = this->fn_->func_expression();
8493 if (fne != NULL
8494 && fne->named_object()->is_function_declaration()
8495 && fne->named_object()->func_declaration_value()->type()->is_builtin())
8496 return new Builtin_call_expression(gogo, this->fn_, this->args_,
8497 this->is_varargs_, loc);
8498
8499 // Handle an argument which is a call to a function which returns
8500 // multiple results.
8501 if (this->args_ != NULL
8502 && this->args_->size() == 1
8503 && this->args_->front()->call_expression() != NULL
8504 && this->fn_->type()->function_type() != NULL)
8505 {
8506 Function_type* fntype = this->fn_->type()->function_type();
8507 size_t rc = this->args_->front()->call_expression()->result_count();
8508 if (rc > 1
8509 && fntype->parameters() != NULL
8510 && (fntype->parameters()->size() == rc
8511 || (fntype->is_varargs()
8512 && fntype->parameters()->size() - 1 <= rc)))
8513 {
8514 Call_expression* call = this->args_->front()->call_expression();
8515 Expression_list* args = new Expression_list;
8516 for (size_t i = 0; i < rc; ++i)
8517 args->push_back(Expression::make_call_result(call, i));
8518 // We can't return a new call expression here, because this
8519 // one may be referenced by Call_result expressions. We
8520 // also can't delete the old arguments, because we may still
8521 // traverse them somewhere up the call stack. FIXME.
8522 this->args_ = args;
8523 }
8524 }
8525
8526 // If this call returns multiple results, create a temporary
8527 // variable for each result.
8528 size_t rc = this->result_count();
8529 if (rc > 1 && this->results_ == NULL)
8530 {
8531 std::vector<Temporary_statement*>* temps =
8532 new std::vector<Temporary_statement*>;
8533 temps->reserve(rc);
8534 const Typed_identifier_list* results =
8535 this->fn_->type()->function_type()->results();
8536 for (Typed_identifier_list::const_iterator p = results->begin();
8537 p != results->end();
8538 ++p)
8539 {
8540 Temporary_statement* temp = Statement::make_temporary(p->type(),
8541 NULL, loc);
8542 inserter->insert(temp);
8543 temps->push_back(temp);
8544 }
8545 this->results_ = temps;
8546 }
8547
8548 // Handle a call to a varargs function by packaging up the extra
8549 // parameters.
8550 if (this->fn_->type()->function_type() != NULL
8551 && this->fn_->type()->function_type()->is_varargs())
8552 {
8553 Function_type* fntype = this->fn_->type()->function_type();
8554 const Typed_identifier_list* parameters = fntype->parameters();
8555 go_assert(parameters != NULL && !parameters->empty());
8556 Type* varargs_type = parameters->back().type();
8557 this->lower_varargs(gogo, function, inserter, varargs_type,
8558 parameters->size());
8559 }
8560
8561 // If this is call to a method, call the method directly passing the
8562 // object as the first parameter.
8563 Bound_method_expression* bme = this->fn_->bound_method_expression();
8564 if (bme != NULL)
8565 {
8566 Named_object* method = bme->method();
8567 Expression* first_arg = bme->first_argument();
8568
8569 // We always pass a pointer when calling a method.
8570 if (first_arg->type()->points_to() == NULL
8571 && !first_arg->type()->is_error())
8572 {
8573 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
8574 // We may need to create a temporary variable so that we can
8575 // take the address. We can't do that here because it will
8576 // mess up the order of evaluation.
8577 Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
8578 ue->set_create_temp();
8579 }
8580
8581 // If we are calling a method which was inherited from an
8582 // embedded struct, and the method did not get a stub, then the
8583 // first type may be wrong.
8584 Type* fatype = bme->first_argument_type();
8585 if (fatype != NULL)
8586 {
8587 if (fatype->points_to() == NULL)
8588 fatype = Type::make_pointer_type(fatype);
8589 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
8590 }
8591
8592 Expression_list* new_args = new Expression_list();
8593 new_args->push_back(first_arg);
8594 if (this->args_ != NULL)
8595 {
8596 for (Expression_list::const_iterator p = this->args_->begin();
8597 p != this->args_->end();
8598 ++p)
8599 new_args->push_back(*p);
8600 }
8601
8602 // We have to change in place because this structure may be
8603 // referenced by Call_result_expressions. We can't delete the
8604 // old arguments, because we may be traversing them up in some
8605 // caller. FIXME.
8606 this->args_ = new_args;
8607 this->fn_ = Expression::make_func_reference(method, NULL,
8608 bme->location());
8609 }
8610
8611 return this;
8612 }
8613
8614 // Lower a call to a varargs function. FUNCTION is the function in
8615 // which the call occurs--it's not the function we are calling.
8616 // VARARGS_TYPE is the type of the varargs parameter, a slice type.
8617 // PARAM_COUNT is the number of parameters of the function we are
8618 // calling; the last of these parameters will be the varargs
8619 // parameter.
8620
8621 void
8622 Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8623 Statement_inserter* inserter,
8624 Type* varargs_type, size_t param_count)
8625 {
8626 if (this->varargs_are_lowered_)
8627 return;
8628
8629 Location loc = this->location();
8630
8631 go_assert(param_count > 0);
8632 go_assert(varargs_type->is_slice_type());
8633
8634 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
8635 if (arg_count < param_count - 1)
8636 {
8637 // Not enough arguments; will be caught in check_types.
8638 return;
8639 }
8640
8641 Expression_list* old_args = this->args_;
8642 Expression_list* new_args = new Expression_list();
8643 bool push_empty_arg = false;
8644 if (old_args == NULL || old_args->empty())
8645 {
8646 go_assert(param_count == 1);
8647 push_empty_arg = true;
8648 }
8649 else
8650 {
8651 Expression_list::const_iterator pa;
8652 int i = 1;
8653 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
8654 {
8655 if (static_cast<size_t>(i) == param_count)
8656 break;
8657 new_args->push_back(*pa);
8658 }
8659
8660 // We have reached the varargs parameter.
8661
8662 bool issued_error = false;
8663 if (pa == old_args->end())
8664 push_empty_arg = true;
8665 else if (pa + 1 == old_args->end() && this->is_varargs_)
8666 new_args->push_back(*pa);
8667 else if (this->is_varargs_)
8668 {
8669 if ((*pa)->type()->is_slice_type())
8670 this->report_error(_("too many arguments"));
8671 else
8672 {
8673 error_at(this->location(),
8674 _("invalid use of %<...%> with non-slice"));
8675 this->set_is_error();
8676 }
8677 return;
8678 }
8679 else
8680 {
8681 Type* element_type = varargs_type->array_type()->element_type();
8682 Expression_list* vals = new Expression_list;
8683 for (; pa != old_args->end(); ++pa, ++i)
8684 {
8685 // Check types here so that we get a better message.
8686 Type* patype = (*pa)->type();
8687 Location paloc = (*pa)->location();
8688 if (!this->check_argument_type(i, element_type, patype,
8689 paloc, issued_error))
8690 continue;
8691 vals->push_back(*pa);
8692 }
8693 Expression* val =
8694 Expression::make_slice_composite_literal(varargs_type, vals, loc);
8695 gogo->lower_expression(function, inserter, &val);
8696 new_args->push_back(val);
8697 }
8698 }
8699
8700 if (push_empty_arg)
8701 new_args->push_back(Expression::make_nil(loc));
8702
8703 // We can't return a new call expression here, because this one may
8704 // be referenced by Call_result expressions. FIXME. We can't
8705 // delete OLD_ARGS because we may have both a Call_expression and a
8706 // Builtin_call_expression which refer to them. FIXME.
8707 this->args_ = new_args;
8708 this->varargs_are_lowered_ = true;
8709 }
8710
8711 // Get the function type. This can return NULL in error cases.
8712
8713 Function_type*
8714 Call_expression::get_function_type() const
8715 {
8716 return this->fn_->type()->function_type();
8717 }
8718
8719 // Return the number of values which this call will return.
8720
8721 size_t
8722 Call_expression::result_count() const
8723 {
8724 const Function_type* fntype = this->get_function_type();
8725 if (fntype == NULL)
8726 return 0;
8727 if (fntype->results() == NULL)
8728 return 0;
8729 return fntype->results()->size();
8730 }
8731
8732 // Return the temporary which holds a result.
8733
8734 Temporary_statement*
8735 Call_expression::result(size_t i) const
8736 {
8737 if (this->results_ == NULL || this->results_->size() <= i)
8738 {
8739 go_assert(saw_errors());
8740 return NULL;
8741 }
8742 return (*this->results_)[i];
8743 }
8744
8745 // Return whether this is a call to the predeclared function recover.
8746
8747 bool
8748 Call_expression::is_recover_call() const
8749 {
8750 return this->do_is_recover_call();
8751 }
8752
8753 // Set the argument to the recover function.
8754
8755 void
8756 Call_expression::set_recover_arg(Expression* arg)
8757 {
8758 this->do_set_recover_arg(arg);
8759 }
8760
8761 // Virtual functions also implemented by Builtin_call_expression.
8762
8763 bool
8764 Call_expression::do_is_recover_call() const
8765 {
8766 return false;
8767 }
8768
8769 void
8770 Call_expression::do_set_recover_arg(Expression*)
8771 {
8772 go_unreachable();
8773 }
8774
8775 // We have found an error with this call expression; return true if
8776 // we should report it.
8777
8778 bool
8779 Call_expression::issue_error()
8780 {
8781 if (this->issued_error_)
8782 return false;
8783 else
8784 {
8785 this->issued_error_ = true;
8786 return true;
8787 }
8788 }
8789
8790 // Get the type.
8791
8792 Type*
8793 Call_expression::do_type()
8794 {
8795 if (this->type_ != NULL)
8796 return this->type_;
8797
8798 Type* ret;
8799 Function_type* fntype = this->get_function_type();
8800 if (fntype == NULL)
8801 return Type::make_error_type();
8802
8803 const Typed_identifier_list* results = fntype->results();
8804 if (results == NULL)
8805 ret = Type::make_void_type();
8806 else if (results->size() == 1)
8807 ret = results->begin()->type();
8808 else
8809 ret = Type::make_call_multiple_result_type(this);
8810
8811 this->type_ = ret;
8812
8813 return this->type_;
8814 }
8815
8816 // Determine types for a call expression. We can use the function
8817 // parameter types to set the types of the arguments.
8818
8819 void
8820 Call_expression::do_determine_type(const Type_context*)
8821 {
8822 if (!this->determining_types())
8823 return;
8824
8825 this->fn_->determine_type_no_context();
8826 Function_type* fntype = this->get_function_type();
8827 const Typed_identifier_list* parameters = NULL;
8828 if (fntype != NULL)
8829 parameters = fntype->parameters();
8830 if (this->args_ != NULL)
8831 {
8832 Typed_identifier_list::const_iterator pt;
8833 if (parameters != NULL)
8834 pt = parameters->begin();
8835 bool first = true;
8836 for (Expression_list::const_iterator pa = this->args_->begin();
8837 pa != this->args_->end();
8838 ++pa)
8839 {
8840 if (first)
8841 {
8842 first = false;
8843 // If this is a method, the first argument is the
8844 // receiver.
8845 if (fntype != NULL && fntype->is_method())
8846 {
8847 Type* rtype = fntype->receiver()->type();
8848 // The receiver is always passed as a pointer.
8849 if (rtype->points_to() == NULL)
8850 rtype = Type::make_pointer_type(rtype);
8851 Type_context subcontext(rtype, false);
8852 (*pa)->determine_type(&subcontext);
8853 continue;
8854 }
8855 }
8856
8857 if (parameters != NULL && pt != parameters->end())
8858 {
8859 Type_context subcontext(pt->type(), false);
8860 (*pa)->determine_type(&subcontext);
8861 ++pt;
8862 }
8863 else
8864 (*pa)->determine_type_no_context();
8865 }
8866 }
8867 }
8868
8869 // Called when determining types for a Call_expression. Return true
8870 // if we should go ahead, false if they have already been determined.
8871
8872 bool
8873 Call_expression::determining_types()
8874 {
8875 if (this->types_are_determined_)
8876 return false;
8877 else
8878 {
8879 this->types_are_determined_ = true;
8880 return true;
8881 }
8882 }
8883
8884 // Check types for parameter I.
8885
8886 bool
8887 Call_expression::check_argument_type(int i, const Type* parameter_type,
8888 const Type* argument_type,
8889 Location argument_location,
8890 bool issued_error)
8891 {
8892 std::string reason;
8893 bool ok;
8894 if (this->are_hidden_fields_ok_)
8895 ok = Type::are_assignable_hidden_ok(parameter_type, argument_type,
8896 &reason);
8897 else
8898 ok = Type::are_assignable(parameter_type, argument_type, &reason);
8899 if (!ok)
8900 {
8901 if (!issued_error)
8902 {
8903 if (reason.empty())
8904 error_at(argument_location, "argument %d has incompatible type", i);
8905 else
8906 error_at(argument_location,
8907 "argument %d has incompatible type (%s)",
8908 i, reason.c_str());
8909 }
8910 this->set_is_error();
8911 return false;
8912 }
8913 return true;
8914 }
8915
8916 // Check types.
8917
8918 void
8919 Call_expression::do_check_types(Gogo*)
8920 {
8921 if (this->classification() == EXPRESSION_ERROR)
8922 return;
8923
8924 Function_type* fntype = this->get_function_type();
8925 if (fntype == NULL)
8926 {
8927 if (!this->fn_->type()->is_error())
8928 this->report_error(_("expected function"));
8929 return;
8930 }
8931
8932 bool is_method = fntype->is_method();
8933 if (is_method)
8934 {
8935 go_assert(this->args_ != NULL && !this->args_->empty());
8936 Type* rtype = fntype->receiver()->type();
8937 Expression* first_arg = this->args_->front();
8938 // The language permits copying hidden fields for a method
8939 // receiver. We dereference the values since receivers are
8940 // always passed as pointers.
8941 std::string reason;
8942 if (!Type::are_assignable_hidden_ok(rtype->deref(),
8943 first_arg->type()->deref(),
8944 &reason))
8945 {
8946 if (reason.empty())
8947 this->report_error(_("incompatible type for receiver"));
8948 else
8949 {
8950 error_at(this->location(),
8951 "incompatible type for receiver (%s)",
8952 reason.c_str());
8953 this->set_is_error();
8954 }
8955 }
8956 }
8957
8958 // Note that varargs was handled by the lower_varargs() method, so
8959 // we don't have to worry about it here unless something is wrong.
8960 if (this->is_varargs_ && !this->varargs_are_lowered_)
8961 {
8962 if (!fntype->is_varargs())
8963 {
8964 error_at(this->location(),
8965 _("invalid use of %<...%> calling non-variadic function"));
8966 this->set_is_error();
8967 return;
8968 }
8969 }
8970
8971 const Typed_identifier_list* parameters = fntype->parameters();
8972 if (this->args_ == NULL)
8973 {
8974 if (parameters != NULL && !parameters->empty())
8975 this->report_error(_("not enough arguments"));
8976 }
8977 else if (parameters == NULL)
8978 {
8979 if (!is_method || this->args_->size() > 1)
8980 this->report_error(_("too many arguments"));
8981 }
8982 else
8983 {
8984 int i = 0;
8985 Expression_list::const_iterator pa = this->args_->begin();
8986 if (is_method)
8987 ++pa;
8988 for (Typed_identifier_list::const_iterator pt = parameters->begin();
8989 pt != parameters->end();
8990 ++pt, ++pa, ++i)
8991 {
8992 if (pa == this->args_->end())
8993 {
8994 this->report_error(_("not enough arguments"));
8995 return;
8996 }
8997 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
8998 (*pa)->location(), false);
8999 }
9000 if (pa != this->args_->end())
9001 this->report_error(_("too many arguments"));
9002 }
9003 }
9004
9005 // Return whether we have to use a temporary variable to ensure that
9006 // we evaluate this call expression in order. If the call returns no
9007 // results then it will inevitably be executed last.
9008
9009 bool
9010 Call_expression::do_must_eval_in_order() const
9011 {
9012 return this->result_count() > 0;
9013 }
9014
9015 // Get the function and the first argument to use when calling an
9016 // interface method.
9017
9018 tree
9019 Call_expression::interface_method_function(
9020 Translate_context* context,
9021 Interface_field_reference_expression* interface_method,
9022 tree* first_arg_ptr)
9023 {
9024 tree expr = interface_method->expr()->get_tree(context);
9025 if (expr == error_mark_node)
9026 return error_mark_node;
9027 expr = save_expr(expr);
9028 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
9029 if (first_arg == error_mark_node)
9030 return error_mark_node;
9031 *first_arg_ptr = first_arg;
9032 return interface_method->get_function_tree(context, expr);
9033 }
9034
9035 // Build the call expression.
9036
9037 tree
9038 Call_expression::do_get_tree(Translate_context* context)
9039 {
9040 if (this->tree_ != NULL_TREE)
9041 return this->tree_;
9042
9043 Function_type* fntype = this->get_function_type();
9044 if (fntype == NULL)
9045 return error_mark_node;
9046
9047 if (this->fn_->is_error_expression())
9048 return error_mark_node;
9049
9050 Gogo* gogo = context->gogo();
9051 Location location = this->location();
9052
9053 Func_expression* func = this->fn_->func_expression();
9054 Interface_field_reference_expression* interface_method =
9055 this->fn_->interface_field_reference_expression();
9056 const bool has_closure = func != NULL && func->closure() != NULL;
9057 const bool is_interface_method = interface_method != NULL;
9058
9059 int nargs;
9060 tree* args;
9061 if (this->args_ == NULL || this->args_->empty())
9062 {
9063 nargs = is_interface_method ? 1 : 0;
9064 args = nargs == 0 ? NULL : new tree[nargs];
9065 }
9066 else if (fntype->parameters() == NULL || fntype->parameters()->empty())
9067 {
9068 // Passing a receiver parameter.
9069 go_assert(!is_interface_method
9070 && fntype->is_method()
9071 && this->args_->size() == 1);
9072 nargs = 1;
9073 args = new tree[nargs];
9074 args[0] = this->args_->front()->get_tree(context);
9075 }
9076 else
9077 {
9078 const Typed_identifier_list* params = fntype->parameters();
9079
9080 nargs = this->args_->size();
9081 int i = is_interface_method ? 1 : 0;
9082 nargs += i;
9083 args = new tree[nargs];
9084
9085 Typed_identifier_list::const_iterator pp = params->begin();
9086 Expression_list::const_iterator pe = this->args_->begin();
9087 if (!is_interface_method && fntype->is_method())
9088 {
9089 args[i] = (*pe)->get_tree(context);
9090 ++pe;
9091 ++i;
9092 }
9093 for (; pe != this->args_->end(); ++pe, ++pp, ++i)
9094 {
9095 go_assert(pp != params->end());
9096 tree arg_val = (*pe)->get_tree(context);
9097 args[i] = Expression::convert_for_assignment(context,
9098 pp->type(),
9099 (*pe)->type(),
9100 arg_val,
9101 location);
9102 if (args[i] == error_mark_node)
9103 {
9104 delete[] args;
9105 return error_mark_node;
9106 }
9107 }
9108 go_assert(pp == params->end());
9109 go_assert(i == nargs);
9110 }
9111
9112 tree rettype = TREE_TYPE(TREE_TYPE(type_to_tree(fntype->get_backend(gogo))));
9113 if (rettype == error_mark_node)
9114 {
9115 delete[] args;
9116 return error_mark_node;
9117 }
9118
9119 tree fn;
9120 if (has_closure)
9121 fn = func->get_tree_without_closure(gogo);
9122 else if (!is_interface_method)
9123 fn = this->fn_->get_tree(context);
9124 else
9125 fn = this->interface_method_function(context, interface_method, &args[0]);
9126
9127 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
9128 {
9129 delete[] args;
9130 return error_mark_node;
9131 }
9132
9133 tree fndecl = fn;
9134 if (TREE_CODE(fndecl) == ADDR_EXPR)
9135 fndecl = TREE_OPERAND(fndecl, 0);
9136
9137 // Add a type cast in case the type of the function is a recursive
9138 // type which refers to itself.
9139 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
9140 {
9141 tree fnt = type_to_tree(fntype->get_backend(gogo));
9142 if (fnt == error_mark_node)
9143 return error_mark_node;
9144 fn = fold_convert_loc(location.gcc_location(), fnt, fn);
9145 }
9146
9147 // This is to support builtin math functions when using 80387 math.
9148 tree excess_type = NULL_TREE;
9149 if (optimize
9150 && TREE_CODE(fndecl) == FUNCTION_DECL
9151 && DECL_IS_BUILTIN(fndecl)
9152 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
9153 && nargs > 0
9154 && ((SCALAR_FLOAT_TYPE_P(rettype)
9155 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
9156 || (COMPLEX_FLOAT_TYPE_P(rettype)
9157 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
9158 {
9159 excess_type = excess_precision_type(TREE_TYPE(args[0]));
9160 if (excess_type != NULL_TREE)
9161 {
9162 tree excess_fndecl = mathfn_built_in(excess_type,
9163 DECL_FUNCTION_CODE(fndecl));
9164 if (excess_fndecl == NULL_TREE)
9165 excess_type = NULL_TREE;
9166 else
9167 {
9168 fn = build_fold_addr_expr_loc(location.gcc_location(),
9169 excess_fndecl);
9170 for (int i = 0; i < nargs; ++i)
9171 {
9172 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
9173 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
9174 args[i] = ::convert(excess_type, args[i]);
9175 }
9176 }
9177 }
9178 }
9179
9180 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
9181 fn, nargs, args);
9182 delete[] args;
9183
9184 SET_EXPR_LOCATION(ret, location.gcc_location());
9185
9186 if (has_closure)
9187 {
9188 tree closure_tree = func->closure()->get_tree(context);
9189 if (closure_tree != error_mark_node)
9190 CALL_EXPR_STATIC_CHAIN(ret) = closure_tree;
9191 }
9192
9193 // If this is a recursive function type which returns itself, as in
9194 // type F func() F
9195 // we have used ptr_type_node for the return type. Add a cast here
9196 // to the correct type.
9197 if (TREE_TYPE(ret) == ptr_type_node)
9198 {
9199 tree t = type_to_tree(this->type()->base()->get_backend(gogo));
9200 ret = fold_convert_loc(location.gcc_location(), t, ret);
9201 }
9202
9203 if (excess_type != NULL_TREE)
9204 {
9205 // Calling convert here can undo our excess precision change.
9206 // That may or may not be a bug in convert_to_real.
9207 ret = build1(NOP_EXPR, rettype, ret);
9208 }
9209
9210 if (this->results_ != NULL)
9211 ret = this->set_results(context, ret);
9212
9213 this->tree_ = ret;
9214
9215 return ret;
9216 }
9217
9218 // Set the result variables if this call returns multiple results.
9219
9220 tree
9221 Call_expression::set_results(Translate_context* context, tree call_tree)
9222 {
9223 tree stmt_list = NULL_TREE;
9224
9225 call_tree = save_expr(call_tree);
9226
9227 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9228 {
9229 go_assert(saw_errors());
9230 return call_tree;
9231 }
9232
9233 Location loc = this->location();
9234 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9235 size_t rc = this->result_count();
9236 for (size_t i = 0; i < rc; ++i, field = DECL_CHAIN(field))
9237 {
9238 go_assert(field != NULL_TREE);
9239
9240 Temporary_statement* temp = this->result(i);
9241 if (temp == NULL)
9242 {
9243 go_assert(saw_errors());
9244 return error_mark_node;
9245 }
9246 Temporary_reference_expression* ref =
9247 Expression::make_temporary_reference(temp, loc);
9248 ref->set_is_lvalue();
9249 tree temp_tree = ref->get_tree(context);
9250 if (temp_tree == error_mark_node)
9251 return error_mark_node;
9252
9253 tree val_tree = build3_loc(loc.gcc_location(), COMPONENT_REF,
9254 TREE_TYPE(field), call_tree, field, NULL_TREE);
9255 tree set_tree = build2_loc(loc.gcc_location(), MODIFY_EXPR,
9256 void_type_node, temp_tree, val_tree);
9257
9258 append_to_statement_list(set_tree, &stmt_list);
9259 }
9260 go_assert(field == NULL_TREE);
9261
9262 return save_expr(stmt_list);
9263 }
9264
9265 // Dump ast representation for a call expressin.
9266
9267 void
9268 Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
9269 {
9270 this->fn_->dump_expression(ast_dump_context);
9271 ast_dump_context->ostream() << "(";
9272 if (args_ != NULL)
9273 ast_dump_context->dump_expression_list(this->args_);
9274
9275 ast_dump_context->ostream() << ") ";
9276 }
9277
9278 // Make a call expression.
9279
9280 Call_expression*
9281 Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
9282 Location location)
9283 {
9284 return new Call_expression(fn, args, is_varargs, location);
9285 }
9286
9287 // A single result from a call which returns multiple results.
9288
9289 class Call_result_expression : public Expression
9290 {
9291 public:
9292 Call_result_expression(Call_expression* call, unsigned int index)
9293 : Expression(EXPRESSION_CALL_RESULT, call->location()),
9294 call_(call), index_(index)
9295 { }
9296
9297 protected:
9298 int
9299 do_traverse(Traverse*);
9300
9301 Type*
9302 do_type();
9303
9304 void
9305 do_determine_type(const Type_context*);
9306
9307 void
9308 do_check_types(Gogo*);
9309
9310 Expression*
9311 do_copy()
9312 {
9313 return new Call_result_expression(this->call_->call_expression(),
9314 this->index_);
9315 }
9316
9317 bool
9318 do_must_eval_in_order() const
9319 { return true; }
9320
9321 tree
9322 do_get_tree(Translate_context*);
9323
9324 void
9325 do_dump_expression(Ast_dump_context*) const;
9326
9327 private:
9328 // The underlying call expression.
9329 Expression* call_;
9330 // Which result we want.
9331 unsigned int index_;
9332 };
9333
9334 // Traverse a call result.
9335
9336 int
9337 Call_result_expression::do_traverse(Traverse* traverse)
9338 {
9339 if (traverse->remember_expression(this->call_))
9340 {
9341 // We have already traversed the call expression.
9342 return TRAVERSE_CONTINUE;
9343 }
9344 return Expression::traverse(&this->call_, traverse);
9345 }
9346
9347 // Get the type.
9348
9349 Type*
9350 Call_result_expression::do_type()
9351 {
9352 if (this->classification() == EXPRESSION_ERROR)
9353 return Type::make_error_type();
9354
9355 // THIS->CALL_ can be replaced with a temporary reference due to
9356 // Call_expression::do_must_eval_in_order when there is an error.
9357 Call_expression* ce = this->call_->call_expression();
9358 if (ce == NULL)
9359 {
9360 this->set_is_error();
9361 return Type::make_error_type();
9362 }
9363 Function_type* fntype = ce->get_function_type();
9364 if (fntype == NULL)
9365 {
9366 if (ce->issue_error())
9367 {
9368 if (!ce->fn()->type()->is_error())
9369 this->report_error(_("expected function"));
9370 }
9371 this->set_is_error();
9372 return Type::make_error_type();
9373 }
9374 const Typed_identifier_list* results = fntype->results();
9375 if (results == NULL || results->size() < 2)
9376 {
9377 if (ce->issue_error())
9378 this->report_error(_("number of results does not match "
9379 "number of values"));
9380 return Type::make_error_type();
9381 }
9382 Typed_identifier_list::const_iterator pr = results->begin();
9383 for (unsigned int i = 0; i < this->index_; ++i)
9384 {
9385 if (pr == results->end())
9386 break;
9387 ++pr;
9388 }
9389 if (pr == results->end())
9390 {
9391 if (ce->issue_error())
9392 this->report_error(_("number of results does not match "
9393 "number of values"));
9394 return Type::make_error_type();
9395 }
9396 return pr->type();
9397 }
9398
9399 // Check the type. Just make sure that we trigger the warning in
9400 // do_type.
9401
9402 void
9403 Call_result_expression::do_check_types(Gogo*)
9404 {
9405 this->type();
9406 }
9407
9408 // Determine the type. We have nothing to do here, but the 0 result
9409 // needs to pass down to the caller.
9410
9411 void
9412 Call_result_expression::do_determine_type(const Type_context*)
9413 {
9414 this->call_->determine_type_no_context();
9415 }
9416
9417 // Return the tree. We just refer to the temporary set by the call
9418 // expression. We don't do this at lowering time because it makes it
9419 // hard to evaluate the call at the right time.
9420
9421 tree
9422 Call_result_expression::do_get_tree(Translate_context* context)
9423 {
9424 Call_expression* ce = this->call_->call_expression();
9425 if (ce == NULL)
9426 {
9427 go_assert(this->call_->is_error_expression());
9428 return error_mark_node;
9429 }
9430 Temporary_statement* ts = ce->result(this->index_);
9431 if (ts == NULL)
9432 {
9433 go_assert(saw_errors());
9434 return error_mark_node;
9435 }
9436 Expression* ref = Expression::make_temporary_reference(ts, this->location());
9437 return ref->get_tree(context);
9438 }
9439
9440 // Dump ast representation for a call result expression.
9441
9442 void
9443 Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
9444 const
9445 {
9446 // FIXME: Wouldn't it be better if the call is assigned to a temporary
9447 // (struct) and the fields are referenced instead.
9448 ast_dump_context->ostream() << this->index_ << "@(";
9449 ast_dump_context->dump_expression(this->call_);
9450 ast_dump_context->ostream() << ")";
9451 }
9452
9453 // Make a reference to a single result of a call which returns
9454 // multiple results.
9455
9456 Expression*
9457 Expression::make_call_result(Call_expression* call, unsigned int index)
9458 {
9459 return new Call_result_expression(call, index);
9460 }
9461
9462 // Class Index_expression.
9463
9464 // Traversal.
9465
9466 int
9467 Index_expression::do_traverse(Traverse* traverse)
9468 {
9469 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
9470 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
9471 || (this->end_ != NULL
9472 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT))
9473 return TRAVERSE_EXIT;
9474 return TRAVERSE_CONTINUE;
9475 }
9476
9477 // Lower an index expression. This converts the generic index
9478 // expression into an array index, a string index, or a map index.
9479
9480 Expression*
9481 Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
9482 {
9483 Location location = this->location();
9484 Expression* left = this->left_;
9485 Expression* start = this->start_;
9486 Expression* end = this->end_;
9487
9488 Type* type = left->type();
9489 if (type->is_error())
9490 return Expression::make_error(location);
9491 else if (left->is_type_expression())
9492 {
9493 error_at(location, "attempt to index type expression");
9494 return Expression::make_error(location);
9495 }
9496 else if (type->array_type() != NULL)
9497 return Expression::make_array_index(left, start, end, location);
9498 else if (type->points_to() != NULL
9499 && type->points_to()->array_type() != NULL
9500 && !type->points_to()->is_slice_type())
9501 {
9502 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
9503 location);
9504 return Expression::make_array_index(deref, start, end, location);
9505 }
9506 else if (type->is_string_type())
9507 return Expression::make_string_index(left, start, end, location);
9508 else if (type->map_type() != NULL)
9509 {
9510 if (end != NULL)
9511 {
9512 error_at(location, "invalid slice of map");
9513 return Expression::make_error(location);
9514 }
9515 Map_index_expression* ret = Expression::make_map_index(left, start,
9516 location);
9517 if (this->is_lvalue_)
9518 ret->set_is_lvalue();
9519 return ret;
9520 }
9521 else
9522 {
9523 error_at(location,
9524 "attempt to index object which is not array, string, or map");
9525 return Expression::make_error(location);
9526 }
9527 }
9528
9529 // Write an indexed expression (expr[expr:expr] or expr[expr]) to a
9530 // dump context
9531
9532 void
9533 Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
9534 const Expression* expr,
9535 const Expression* start,
9536 const Expression* end)
9537 {
9538 expr->dump_expression(ast_dump_context);
9539 ast_dump_context->ostream() << "[";
9540 start->dump_expression(ast_dump_context);
9541 if (end != NULL)
9542 {
9543 ast_dump_context->ostream() << ":";
9544 end->dump_expression(ast_dump_context);
9545 }
9546 ast_dump_context->ostream() << "]";
9547 }
9548
9549 // Dump ast representation for an index expression.
9550
9551 void
9552 Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
9553 const
9554 {
9555 Index_expression::dump_index_expression(ast_dump_context, this->left_,
9556 this->start_, this->end_);
9557 }
9558
9559 // Make an index expression.
9560
9561 Expression*
9562 Expression::make_index(Expression* left, Expression* start, Expression* end,
9563 Location location)
9564 {
9565 return new Index_expression(left, start, end, location);
9566 }
9567
9568 // An array index. This is used for both indexing and slicing.
9569
9570 class Array_index_expression : public Expression
9571 {
9572 public:
9573 Array_index_expression(Expression* array, Expression* start,
9574 Expression* end, Location location)
9575 : Expression(EXPRESSION_ARRAY_INDEX, location),
9576 array_(array), start_(start), end_(end), type_(NULL)
9577 { }
9578
9579 protected:
9580 int
9581 do_traverse(Traverse*);
9582
9583 Type*
9584 do_type();
9585
9586 void
9587 do_determine_type(const Type_context*);
9588
9589 void
9590 do_check_types(Gogo*);
9591
9592 Expression*
9593 do_copy()
9594 {
9595 return Expression::make_array_index(this->array_->copy(),
9596 this->start_->copy(),
9597 (this->end_ == NULL
9598 ? NULL
9599 : this->end_->copy()),
9600 this->location());
9601 }
9602
9603 bool
9604 do_must_eval_subexpressions_in_order(int* skip) const
9605 {
9606 *skip = 1;
9607 return true;
9608 }
9609
9610 bool
9611 do_is_addressable() const;
9612
9613 void
9614 do_address_taken(bool escapes)
9615 { this->array_->address_taken(escapes); }
9616
9617 tree
9618 do_get_tree(Translate_context*);
9619
9620 void
9621 do_dump_expression(Ast_dump_context*) const;
9622
9623 private:
9624 // The array we are getting a value from.
9625 Expression* array_;
9626 // The start or only index.
9627 Expression* start_;
9628 // The end index of a slice. This may be NULL for a simple array
9629 // index, or it may be a nil expression for the length of the array.
9630 Expression* end_;
9631 // The type of the expression.
9632 Type* type_;
9633 };
9634
9635 // Array index traversal.
9636
9637 int
9638 Array_index_expression::do_traverse(Traverse* traverse)
9639 {
9640 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
9641 return TRAVERSE_EXIT;
9642 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
9643 return TRAVERSE_EXIT;
9644 if (this->end_ != NULL)
9645 {
9646 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
9647 return TRAVERSE_EXIT;
9648 }
9649 return TRAVERSE_CONTINUE;
9650 }
9651
9652 // Return the type of an array index.
9653
9654 Type*
9655 Array_index_expression::do_type()
9656 {
9657 if (this->type_ == NULL)
9658 {
9659 Array_type* type = this->array_->type()->array_type();
9660 if (type == NULL)
9661 this->type_ = Type::make_error_type();
9662 else if (this->end_ == NULL)
9663 this->type_ = type->element_type();
9664 else if (type->is_slice_type())
9665 {
9666 // A slice of a slice has the same type as the original
9667 // slice.
9668 this->type_ = this->array_->type()->deref();
9669 }
9670 else
9671 {
9672 // A slice of an array is a slice.
9673 this->type_ = Type::make_array_type(type->element_type(), NULL);
9674 }
9675 }
9676 return this->type_;
9677 }
9678
9679 // Set the type of an array index.
9680
9681 void
9682 Array_index_expression::do_determine_type(const Type_context*)
9683 {
9684 this->array_->determine_type_no_context();
9685 this->start_->determine_type_no_context();
9686 if (this->end_ != NULL)
9687 this->end_->determine_type_no_context();
9688 }
9689
9690 // Check types of an array index.
9691
9692 void
9693 Array_index_expression::do_check_types(Gogo*)
9694 {
9695 if (this->start_->type()->integer_type() == NULL)
9696 this->report_error(_("index must be integer"));
9697 if (this->end_ != NULL
9698 && this->end_->type()->integer_type() == NULL
9699 && !this->end_->type()->is_error()
9700 && !this->end_->is_nil_expression()
9701 && !this->end_->is_error_expression())
9702 this->report_error(_("slice end must be integer"));
9703
9704 Array_type* array_type = this->array_->type()->array_type();
9705 if (array_type == NULL)
9706 {
9707 go_assert(this->array_->type()->is_error());
9708 return;
9709 }
9710
9711 unsigned int int_bits =
9712 Type::lookup_integer_type("int")->integer_type()->bits();
9713
9714 Numeric_constant lvalnc;
9715 mpz_t lval;
9716 bool lval_valid = (array_type->length() != NULL
9717 && array_type->length()->numeric_constant_value(&lvalnc)
9718 && lvalnc.to_int(&lval));
9719 Numeric_constant inc;
9720 mpz_t ival;
9721 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
9722 {
9723 if (mpz_sgn(ival) < 0
9724 || mpz_sizeinbase(ival, 2) >= int_bits
9725 || (lval_valid
9726 && (this->end_ == NULL
9727 ? mpz_cmp(ival, lval) >= 0
9728 : mpz_cmp(ival, lval) > 0)))
9729 {
9730 error_at(this->start_->location(), "array index out of bounds");
9731 this->set_is_error();
9732 }
9733 mpz_clear(ival);
9734 }
9735 if (this->end_ != NULL && !this->end_->is_nil_expression())
9736 {
9737 Numeric_constant enc;
9738 mpz_t eval;
9739 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
9740 {
9741 if (mpz_sgn(eval) < 0
9742 || mpz_sizeinbase(eval, 2) >= int_bits
9743 || (lval_valid && mpz_cmp(eval, lval) > 0))
9744 {
9745 error_at(this->end_->location(), "array index out of bounds");
9746 this->set_is_error();
9747 }
9748 mpz_clear(eval);
9749 }
9750 }
9751 if (lval_valid)
9752 mpz_clear(lval);
9753
9754 // A slice of an array requires an addressable array. A slice of a
9755 // slice is always possible.
9756 if (this->end_ != NULL && !array_type->is_slice_type())
9757 {
9758 if (!this->array_->is_addressable())
9759 this->report_error(_("slice of unaddressable value"));
9760 else
9761 this->array_->address_taken(true);
9762 }
9763 }
9764
9765 // Return whether this expression is addressable.
9766
9767 bool
9768 Array_index_expression::do_is_addressable() const
9769 {
9770 // A slice expression is not addressable.
9771 if (this->end_ != NULL)
9772 return false;
9773
9774 // An index into a slice is addressable.
9775 if (this->array_->type()->is_slice_type())
9776 return true;
9777
9778 // An index into an array is addressable if the array is
9779 // addressable.
9780 return this->array_->is_addressable();
9781 }
9782
9783 // Get a tree for an array index.
9784
9785 tree
9786 Array_index_expression::do_get_tree(Translate_context* context)
9787 {
9788 Gogo* gogo = context->gogo();
9789 Location loc = this->location();
9790
9791 Array_type* array_type = this->array_->type()->array_type();
9792 if (array_type == NULL)
9793 {
9794 go_assert(this->array_->type()->is_error());
9795 return error_mark_node;
9796 }
9797
9798 tree type_tree = type_to_tree(array_type->get_backend(gogo));
9799 if (type_tree == error_mark_node)
9800 return error_mark_node;
9801
9802 tree array_tree = this->array_->get_tree(context);
9803 if (array_tree == error_mark_node)
9804 return error_mark_node;
9805
9806 if (array_type->length() == NULL && !DECL_P(array_tree))
9807 array_tree = save_expr(array_tree);
9808
9809 tree length_tree = NULL_TREE;
9810 if (this->end_ == NULL || this->end_->is_nil_expression())
9811 {
9812 length_tree = array_type->length_tree(gogo, array_tree);
9813 if (length_tree == error_mark_node)
9814 return error_mark_node;
9815 length_tree = save_expr(length_tree);
9816 }
9817
9818 tree capacity_tree = NULL_TREE;
9819 if (this->end_ != NULL)
9820 {
9821 capacity_tree = array_type->capacity_tree(gogo, array_tree);
9822 if (capacity_tree == error_mark_node)
9823 return error_mark_node;
9824 capacity_tree = save_expr(capacity_tree);
9825 }
9826
9827 tree length_type = (length_tree != NULL_TREE
9828 ? TREE_TYPE(length_tree)
9829 : TREE_TYPE(capacity_tree));
9830
9831 tree bad_index = boolean_false_node;
9832
9833 tree start_tree = this->start_->get_tree(context);
9834 if (start_tree == error_mark_node)
9835 return error_mark_node;
9836 if (!DECL_P(start_tree))
9837 start_tree = save_expr(start_tree);
9838 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
9839 start_tree = convert_to_integer(length_type, start_tree);
9840
9841 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
9842 loc);
9843
9844 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
9845 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
9846 boolean_type_node, bad_index,
9847 fold_build2_loc(loc.gcc_location(),
9848 (this->end_ == NULL
9849 ? GE_EXPR
9850 : GT_EXPR),
9851 boolean_type_node, start_tree,
9852 (this->end_ == NULL
9853 ? length_tree
9854 : capacity_tree)));
9855
9856 int code = (array_type->length() != NULL
9857 ? (this->end_ == NULL
9858 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
9859 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
9860 : (this->end_ == NULL
9861 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
9862 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
9863 tree crash = Gogo::runtime_error(code, loc);
9864
9865 if (this->end_ == NULL)
9866 {
9867 // Simple array indexing. This has to return an l-value, so
9868 // wrap the index check into START_TREE.
9869 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
9870 build3(COND_EXPR, void_type_node,
9871 bad_index, crash, NULL_TREE),
9872 start_tree);
9873 start_tree = fold_convert_loc(loc.gcc_location(), sizetype, start_tree);
9874
9875 if (array_type->length() != NULL)
9876 {
9877 // Fixed array.
9878 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
9879 start_tree, NULL_TREE, NULL_TREE);
9880 }
9881 else
9882 {
9883 // Open array.
9884 tree values = array_type->value_pointer_tree(gogo, array_tree);
9885 Type* element_type = array_type->element_type();
9886 Btype* belement_type = element_type->get_backend(gogo);
9887 tree element_type_tree = type_to_tree(belement_type);
9888 if (element_type_tree == error_mark_node)
9889 return error_mark_node;
9890 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9891 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
9892 start_tree, element_size);
9893 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
9894 TREE_TYPE(values), values, offset);
9895 return build_fold_indirect_ref(ptr);
9896 }
9897 }
9898
9899 // Array slice.
9900
9901 tree end_tree;
9902 if (this->end_->is_nil_expression())
9903 end_tree = length_tree;
9904 else
9905 {
9906 end_tree = this->end_->get_tree(context);
9907 if (end_tree == error_mark_node)
9908 return error_mark_node;
9909 if (!DECL_P(end_tree))
9910 end_tree = save_expr(end_tree);
9911 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
9912 end_tree = convert_to_integer(length_type, end_tree);
9913
9914 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
9915 loc);
9916
9917 end_tree = fold_convert_loc(loc.gcc_location(), length_type, end_tree);
9918
9919 tree bad_end = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
9920 boolean_type_node,
9921 fold_build2_loc(loc.gcc_location(),
9922 LT_EXPR, boolean_type_node,
9923 end_tree, start_tree),
9924 fold_build2_loc(loc.gcc_location(),
9925 GT_EXPR, boolean_type_node,
9926 end_tree, capacity_tree));
9927 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
9928 boolean_type_node, bad_index, bad_end);
9929 }
9930
9931 Type* element_type = array_type->element_type();
9932 tree element_type_tree = type_to_tree(element_type->get_backend(gogo));
9933 if (element_type_tree == error_mark_node)
9934 return error_mark_node;
9935 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
9936
9937 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
9938 fold_convert_loc(loc.gcc_location(), sizetype,
9939 start_tree),
9940 element_size);
9941
9942 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
9943 if (value_pointer == error_mark_node)
9944 return error_mark_node;
9945
9946 value_pointer = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
9947 TREE_TYPE(value_pointer),
9948 value_pointer, offset);
9949
9950 tree result_length_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
9951 length_type, end_tree, start_tree);
9952
9953 tree result_capacity_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
9954 length_type, capacity_tree,
9955 start_tree);
9956
9957 tree struct_tree = type_to_tree(this->type()->get_backend(gogo));
9958 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
9959
9960 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
9961
9962 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
9963 tree field = TYPE_FIELDS(struct_tree);
9964 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
9965 elt->index = field;
9966 elt->value = value_pointer;
9967
9968 elt = VEC_quick_push(constructor_elt, init, NULL);
9969 field = DECL_CHAIN(field);
9970 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
9971 elt->index = field;
9972 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
9973 result_length_tree);
9974
9975 elt = VEC_quick_push(constructor_elt, init, NULL);
9976 field = DECL_CHAIN(field);
9977 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
9978 elt->index = field;
9979 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
9980 result_capacity_tree);
9981
9982 tree constructor = build_constructor(struct_tree, init);
9983
9984 if (TREE_CONSTANT(value_pointer)
9985 && TREE_CONSTANT(result_length_tree)
9986 && TREE_CONSTANT(result_capacity_tree))
9987 TREE_CONSTANT(constructor) = 1;
9988
9989 return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
9990 TREE_TYPE(constructor),
9991 build3(COND_EXPR, void_type_node,
9992 bad_index, crash, NULL_TREE),
9993 constructor);
9994 }
9995
9996 // Dump ast representation for an array index expression.
9997
9998 void
9999 Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10000 const
10001 {
10002 Index_expression::dump_index_expression(ast_dump_context, this->array_,
10003 this->start_, this->end_);
10004 }
10005
10006 // Make an array index expression. END may be NULL.
10007
10008 Expression*
10009 Expression::make_array_index(Expression* array, Expression* start,
10010 Expression* end, Location location)
10011 {
10012 return new Array_index_expression(array, start, end, location);
10013 }
10014
10015 // A string index. This is used for both indexing and slicing.
10016
10017 class String_index_expression : public Expression
10018 {
10019 public:
10020 String_index_expression(Expression* string, Expression* start,
10021 Expression* end, Location location)
10022 : Expression(EXPRESSION_STRING_INDEX, location),
10023 string_(string), start_(start), end_(end)
10024 { }
10025
10026 protected:
10027 int
10028 do_traverse(Traverse*);
10029
10030 Type*
10031 do_type();
10032
10033 void
10034 do_determine_type(const Type_context*);
10035
10036 void
10037 do_check_types(Gogo*);
10038
10039 Expression*
10040 do_copy()
10041 {
10042 return Expression::make_string_index(this->string_->copy(),
10043 this->start_->copy(),
10044 (this->end_ == NULL
10045 ? NULL
10046 : this->end_->copy()),
10047 this->location());
10048 }
10049
10050 bool
10051 do_must_eval_subexpressions_in_order(int* skip) const
10052 {
10053 *skip = 1;
10054 return true;
10055 }
10056
10057 tree
10058 do_get_tree(Translate_context*);
10059
10060 void
10061 do_dump_expression(Ast_dump_context*) const;
10062
10063 private:
10064 // The string we are getting a value from.
10065 Expression* string_;
10066 // The start or only index.
10067 Expression* start_;
10068 // The end index of a slice. This may be NULL for a single index,
10069 // or it may be a nil expression for the length of the string.
10070 Expression* end_;
10071 };
10072
10073 // String index traversal.
10074
10075 int
10076 String_index_expression::do_traverse(Traverse* traverse)
10077 {
10078 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
10079 return TRAVERSE_EXIT;
10080 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10081 return TRAVERSE_EXIT;
10082 if (this->end_ != NULL)
10083 {
10084 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10085 return TRAVERSE_EXIT;
10086 }
10087 return TRAVERSE_CONTINUE;
10088 }
10089
10090 // Return the type of a string index.
10091
10092 Type*
10093 String_index_expression::do_type()
10094 {
10095 if (this->end_ == NULL)
10096 return Type::lookup_integer_type("uint8");
10097 else
10098 return this->string_->type();
10099 }
10100
10101 // Determine the type of a string index.
10102
10103 void
10104 String_index_expression::do_determine_type(const Type_context*)
10105 {
10106 this->string_->determine_type_no_context();
10107 this->start_->determine_type_no_context();
10108 if (this->end_ != NULL)
10109 this->end_->determine_type_no_context();
10110 }
10111
10112 // Check types of a string index.
10113
10114 void
10115 String_index_expression::do_check_types(Gogo*)
10116 {
10117 if (this->start_->type()->integer_type() == NULL)
10118 this->report_error(_("index must be integer"));
10119 if (this->end_ != NULL
10120 && this->end_->type()->integer_type() == NULL
10121 && !this->end_->is_nil_expression())
10122 this->report_error(_("slice end must be integer"));
10123
10124 std::string sval;
10125 bool sval_valid = this->string_->string_constant_value(&sval);
10126
10127 Numeric_constant inc;
10128 mpz_t ival;
10129 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
10130 {
10131 if (mpz_sgn(ival) < 0
10132 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
10133 {
10134 error_at(this->start_->location(), "string index out of bounds");
10135 this->set_is_error();
10136 }
10137 mpz_clear(ival);
10138 }
10139 if (this->end_ != NULL && !this->end_->is_nil_expression())
10140 {
10141 Numeric_constant enc;
10142 mpz_t eval;
10143 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
10144 {
10145 if (mpz_sgn(eval) < 0
10146 || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0))
10147 {
10148 error_at(this->end_->location(), "string index out of bounds");
10149 this->set_is_error();
10150 }
10151 mpz_clear(eval);
10152 }
10153 }
10154 }
10155
10156 // Get a tree for a string index.
10157
10158 tree
10159 String_index_expression::do_get_tree(Translate_context* context)
10160 {
10161 Location loc = this->location();
10162
10163 tree string_tree = this->string_->get_tree(context);
10164 if (string_tree == error_mark_node)
10165 return error_mark_node;
10166
10167 if (this->string_->type()->points_to() != NULL)
10168 string_tree = build_fold_indirect_ref(string_tree);
10169 if (!DECL_P(string_tree))
10170 string_tree = save_expr(string_tree);
10171 tree string_type = TREE_TYPE(string_tree);
10172
10173 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
10174 length_tree = save_expr(length_tree);
10175 tree length_type = TREE_TYPE(length_tree);
10176
10177 tree bad_index = boolean_false_node;
10178
10179 tree start_tree = this->start_->get_tree(context);
10180 if (start_tree == error_mark_node)
10181 return error_mark_node;
10182 if (!DECL_P(start_tree))
10183 start_tree = save_expr(start_tree);
10184 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
10185 start_tree = convert_to_integer(length_type, start_tree);
10186
10187 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
10188 loc);
10189
10190 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
10191
10192 int code = (this->end_ == NULL
10193 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
10194 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
10195 tree crash = Gogo::runtime_error(code, loc);
10196
10197 if (this->end_ == NULL)
10198 {
10199 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10200 boolean_type_node, bad_index,
10201 fold_build2_loc(loc.gcc_location(), GE_EXPR,
10202 boolean_type_node,
10203 start_tree, length_tree));
10204
10205 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
10206 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
10207 TREE_TYPE(bytes_tree),
10208 bytes_tree,
10209 fold_convert_loc(loc.gcc_location(), sizetype,
10210 start_tree));
10211 tree index = build_fold_indirect_ref_loc(loc.gcc_location(), ptr);
10212
10213 return build2(COMPOUND_EXPR, TREE_TYPE(index),
10214 build3(COND_EXPR, void_type_node,
10215 bad_index, crash, NULL_TREE),
10216 index);
10217 }
10218 else
10219 {
10220 tree end_tree;
10221 if (this->end_->is_nil_expression())
10222 end_tree = build_int_cst(length_type, -1);
10223 else
10224 {
10225 end_tree = this->end_->get_tree(context);
10226 if (end_tree == error_mark_node)
10227 return error_mark_node;
10228 if (!DECL_P(end_tree))
10229 end_tree = save_expr(end_tree);
10230 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
10231 end_tree = convert_to_integer(length_type, end_tree);
10232
10233 bad_index = Expression::check_bounds(end_tree, length_type,
10234 bad_index, loc);
10235
10236 end_tree = fold_convert_loc(loc.gcc_location(), length_type,
10237 end_tree);
10238 }
10239
10240 static tree strslice_fndecl;
10241 tree ret = Gogo::call_builtin(&strslice_fndecl,
10242 loc,
10243 "__go_string_slice",
10244 3,
10245 string_type,
10246 string_type,
10247 string_tree,
10248 length_type,
10249 start_tree,
10250 length_type,
10251 end_tree);
10252 if (ret == error_mark_node)
10253 return error_mark_node;
10254 // This will panic if the bounds are out of range for the
10255 // string.
10256 TREE_NOTHROW(strslice_fndecl) = 0;
10257
10258 if (bad_index == boolean_false_node)
10259 return ret;
10260 else
10261 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
10262 build3(COND_EXPR, void_type_node,
10263 bad_index, crash, NULL_TREE),
10264 ret);
10265 }
10266 }
10267
10268 // Dump ast representation for a string index expression.
10269
10270 void
10271 String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10272 const
10273 {
10274 Index_expression::dump_index_expression(ast_dump_context, this->string_,
10275 this->start_, this->end_);
10276 }
10277
10278 // Make a string index expression. END may be NULL.
10279
10280 Expression*
10281 Expression::make_string_index(Expression* string, Expression* start,
10282 Expression* end, Location location)
10283 {
10284 return new String_index_expression(string, start, end, location);
10285 }
10286
10287 // Class Map_index.
10288
10289 // Get the type of the map.
10290
10291 Map_type*
10292 Map_index_expression::get_map_type() const
10293 {
10294 Map_type* mt = this->map_->type()->deref()->map_type();
10295 if (mt == NULL)
10296 go_assert(saw_errors());
10297 return mt;
10298 }
10299
10300 // Map index traversal.
10301
10302 int
10303 Map_index_expression::do_traverse(Traverse* traverse)
10304 {
10305 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
10306 return TRAVERSE_EXIT;
10307 return Expression::traverse(&this->index_, traverse);
10308 }
10309
10310 // Return the type of a map index.
10311
10312 Type*
10313 Map_index_expression::do_type()
10314 {
10315 Map_type* mt = this->get_map_type();
10316 if (mt == NULL)
10317 return Type::make_error_type();
10318 Type* type = mt->val_type();
10319 // If this map index is in a tuple assignment, we actually return a
10320 // pointer to the value type. Tuple_map_assignment_statement is
10321 // responsible for handling this correctly. We need to get the type
10322 // right in case this gets assigned to a temporary variable.
10323 if (this->is_in_tuple_assignment_)
10324 type = Type::make_pointer_type(type);
10325 return type;
10326 }
10327
10328 // Fix the type of a map index.
10329
10330 void
10331 Map_index_expression::do_determine_type(const Type_context*)
10332 {
10333 this->map_->determine_type_no_context();
10334 Map_type* mt = this->get_map_type();
10335 Type* key_type = mt == NULL ? NULL : mt->key_type();
10336 Type_context subcontext(key_type, false);
10337 this->index_->determine_type(&subcontext);
10338 }
10339
10340 // Check types of a map index.
10341
10342 void
10343 Map_index_expression::do_check_types(Gogo*)
10344 {
10345 std::string reason;
10346 Map_type* mt = this->get_map_type();
10347 if (mt == NULL)
10348 return;
10349 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
10350 {
10351 if (reason.empty())
10352 this->report_error(_("incompatible type for map index"));
10353 else
10354 {
10355 error_at(this->location(), "incompatible type for map index (%s)",
10356 reason.c_str());
10357 this->set_is_error();
10358 }
10359 }
10360 }
10361
10362 // Get a tree for a map index.
10363
10364 tree
10365 Map_index_expression::do_get_tree(Translate_context* context)
10366 {
10367 Map_type* type = this->get_map_type();
10368 if (type == NULL)
10369 return error_mark_node;
10370
10371 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
10372 if (valptr == error_mark_node)
10373 return error_mark_node;
10374 valptr = save_expr(valptr);
10375
10376 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
10377
10378 if (this->is_lvalue_)
10379 return build_fold_indirect_ref(valptr);
10380 else if (this->is_in_tuple_assignment_)
10381 {
10382 // Tuple_map_assignment_statement is responsible for using this
10383 // appropriately.
10384 return valptr;
10385 }
10386 else
10387 {
10388 Gogo* gogo = context->gogo();
10389 Btype* val_btype = type->val_type()->get_backend(gogo);
10390 Bexpression* val_zero = gogo->backend()->zero_expression(val_btype);
10391 return fold_build3(COND_EXPR, val_type_tree,
10392 fold_build2(EQ_EXPR, boolean_type_node, valptr,
10393 fold_convert(TREE_TYPE(valptr),
10394 null_pointer_node)),
10395 expr_to_tree(val_zero),
10396 build_fold_indirect_ref(valptr));
10397 }
10398 }
10399
10400 // Get a tree for the map index. This returns a tree which evaluates
10401 // to a pointer to a value. The pointer will be NULL if the key is
10402 // not in the map.
10403
10404 tree
10405 Map_index_expression::get_value_pointer(Translate_context* context,
10406 bool insert)
10407 {
10408 Map_type* type = this->get_map_type();
10409 if (type == NULL)
10410 return error_mark_node;
10411
10412 tree map_tree = this->map_->get_tree(context);
10413 tree index_tree = this->index_->get_tree(context);
10414 index_tree = Expression::convert_for_assignment(context, type->key_type(),
10415 this->index_->type(),
10416 index_tree,
10417 this->location());
10418 if (map_tree == error_mark_node || index_tree == error_mark_node)
10419 return error_mark_node;
10420
10421 if (this->map_->type()->points_to() != NULL)
10422 map_tree = build_fold_indirect_ref(map_tree);
10423
10424 // We need to pass in a pointer to the key, so stuff it into a
10425 // variable.
10426 tree tmp;
10427 tree make_tmp;
10428 if (current_function_decl != NULL)
10429 {
10430 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
10431 DECL_IGNORED_P(tmp) = 0;
10432 DECL_INITIAL(tmp) = index_tree;
10433 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
10434 TREE_ADDRESSABLE(tmp) = 1;
10435 }
10436 else
10437 {
10438 tmp = build_decl(this->location().gcc_location(), VAR_DECL,
10439 create_tmp_var_name("M"),
10440 TREE_TYPE(index_tree));
10441 DECL_EXTERNAL(tmp) = 0;
10442 TREE_PUBLIC(tmp) = 0;
10443 TREE_STATIC(tmp) = 1;
10444 DECL_ARTIFICIAL(tmp) = 1;
10445 if (!TREE_CONSTANT(index_tree))
10446 make_tmp = fold_build2_loc(this->location().gcc_location(),
10447 INIT_EXPR, void_type_node,
10448 tmp, index_tree);
10449 else
10450 {
10451 TREE_READONLY(tmp) = 1;
10452 TREE_CONSTANT(tmp) = 1;
10453 DECL_INITIAL(tmp) = index_tree;
10454 make_tmp = NULL_TREE;
10455 }
10456 rest_of_decl_compilation(tmp, 1, 0);
10457 }
10458 tree tmpref =
10459 fold_convert_loc(this->location().gcc_location(), const_ptr_type_node,
10460 build_fold_addr_expr_loc(this->location().gcc_location(),
10461 tmp));
10462
10463 static tree map_index_fndecl;
10464 tree call = Gogo::call_builtin(&map_index_fndecl,
10465 this->location(),
10466 "__go_map_index",
10467 3,
10468 const_ptr_type_node,
10469 TREE_TYPE(map_tree),
10470 map_tree,
10471 const_ptr_type_node,
10472 tmpref,
10473 boolean_type_node,
10474 (insert
10475 ? boolean_true_node
10476 : boolean_false_node));
10477 if (call == error_mark_node)
10478 return error_mark_node;
10479 // This can panic on a map of interface type if the interface holds
10480 // an uncomparable or unhashable type.
10481 TREE_NOTHROW(map_index_fndecl) = 0;
10482
10483 Type* val_type = type->val_type();
10484 tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo()));
10485 if (val_type_tree == error_mark_node)
10486 return error_mark_node;
10487 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
10488
10489 tree ret = fold_convert_loc(this->location().gcc_location(),
10490 ptr_val_type_tree, call);
10491 if (make_tmp != NULL_TREE)
10492 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
10493 return ret;
10494 }
10495
10496 // Dump ast representation for a map index expression
10497
10498 void
10499 Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10500 const
10501 {
10502 Index_expression::dump_index_expression(ast_dump_context,
10503 this->map_, this->index_, NULL);
10504 }
10505
10506 // Make a map index expression.
10507
10508 Map_index_expression*
10509 Expression::make_map_index(Expression* map, Expression* index,
10510 Location location)
10511 {
10512 return new Map_index_expression(map, index, location);
10513 }
10514
10515 // Class Field_reference_expression.
10516
10517 // Return the type of a field reference.
10518
10519 Type*
10520 Field_reference_expression::do_type()
10521 {
10522 Type* type = this->expr_->type();
10523 if (type->is_error())
10524 return type;
10525 Struct_type* struct_type = type->struct_type();
10526 go_assert(struct_type != NULL);
10527 return struct_type->field(this->field_index_)->type();
10528 }
10529
10530 // Check the types for a field reference.
10531
10532 void
10533 Field_reference_expression::do_check_types(Gogo*)
10534 {
10535 Type* type = this->expr_->type();
10536 if (type->is_error())
10537 return;
10538 Struct_type* struct_type = type->struct_type();
10539 go_assert(struct_type != NULL);
10540 go_assert(struct_type->field(this->field_index_) != NULL);
10541 }
10542
10543 // Get a tree for a field reference.
10544
10545 tree
10546 Field_reference_expression::do_get_tree(Translate_context* context)
10547 {
10548 tree struct_tree = this->expr_->get_tree(context);
10549 if (struct_tree == error_mark_node
10550 || TREE_TYPE(struct_tree) == error_mark_node)
10551 return error_mark_node;
10552 go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
10553 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
10554 if (field == NULL_TREE)
10555 {
10556 // This can happen for a type which refers to itself indirectly
10557 // and then turns out to be erroneous.
10558 go_assert(saw_errors());
10559 return error_mark_node;
10560 }
10561 for (unsigned int i = this->field_index_; i > 0; --i)
10562 {
10563 field = DECL_CHAIN(field);
10564 go_assert(field != NULL_TREE);
10565 }
10566 if (TREE_TYPE(field) == error_mark_node)
10567 return error_mark_node;
10568 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
10569 NULL_TREE);
10570 }
10571
10572 // Dump ast representation for a field reference expression.
10573
10574 void
10575 Field_reference_expression::do_dump_expression(
10576 Ast_dump_context* ast_dump_context) const
10577 {
10578 this->expr_->dump_expression(ast_dump_context);
10579 ast_dump_context->ostream() << "." << this->field_index_;
10580 }
10581
10582 // Make a reference to a qualified identifier in an expression.
10583
10584 Field_reference_expression*
10585 Expression::make_field_reference(Expression* expr, unsigned int field_index,
10586 Location location)
10587 {
10588 return new Field_reference_expression(expr, field_index, location);
10589 }
10590
10591 // Class Interface_field_reference_expression.
10592
10593 // Return a tree for the pointer to the function to call.
10594
10595 tree
10596 Interface_field_reference_expression::get_function_tree(Translate_context*,
10597 tree expr)
10598 {
10599 if (this->expr_->type()->points_to() != NULL)
10600 expr = build_fold_indirect_ref(expr);
10601
10602 tree expr_type = TREE_TYPE(expr);
10603 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10604
10605 tree field = TYPE_FIELDS(expr_type);
10606 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
10607
10608 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10609 go_assert(POINTER_TYPE_P(TREE_TYPE(table)));
10610
10611 table = build_fold_indirect_ref(table);
10612 go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
10613
10614 std::string name = Gogo::unpack_hidden_name(this->name_);
10615 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
10616 field != NULL_TREE;
10617 field = DECL_CHAIN(field))
10618 {
10619 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
10620 break;
10621 }
10622 go_assert(field != NULL_TREE);
10623
10624 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
10625 }
10626
10627 // Return a tree for the first argument to pass to the interface
10628 // function.
10629
10630 tree
10631 Interface_field_reference_expression::get_underlying_object_tree(
10632 Translate_context*,
10633 tree expr)
10634 {
10635 if (this->expr_->type()->points_to() != NULL)
10636 expr = build_fold_indirect_ref(expr);
10637
10638 tree expr_type = TREE_TYPE(expr);
10639 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
10640
10641 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
10642 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
10643
10644 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
10645 }
10646
10647 // Traversal.
10648
10649 int
10650 Interface_field_reference_expression::do_traverse(Traverse* traverse)
10651 {
10652 return Expression::traverse(&this->expr_, traverse);
10653 }
10654
10655 // Return the type of an interface field reference.
10656
10657 Type*
10658 Interface_field_reference_expression::do_type()
10659 {
10660 Type* expr_type = this->expr_->type();
10661
10662 Type* points_to = expr_type->points_to();
10663 if (points_to != NULL)
10664 expr_type = points_to;
10665
10666 Interface_type* interface_type = expr_type->interface_type();
10667 if (interface_type == NULL)
10668 return Type::make_error_type();
10669
10670 const Typed_identifier* method = interface_type->find_method(this->name_);
10671 if (method == NULL)
10672 return Type::make_error_type();
10673
10674 return method->type();
10675 }
10676
10677 // Determine types.
10678
10679 void
10680 Interface_field_reference_expression::do_determine_type(const Type_context*)
10681 {
10682 this->expr_->determine_type_no_context();
10683 }
10684
10685 // Check the types for an interface field reference.
10686
10687 void
10688 Interface_field_reference_expression::do_check_types(Gogo*)
10689 {
10690 Type* type = this->expr_->type();
10691
10692 Type* points_to = type->points_to();
10693 if (points_to != NULL)
10694 type = points_to;
10695
10696 Interface_type* interface_type = type->interface_type();
10697 if (interface_type == NULL)
10698 {
10699 if (!type->is_error_type())
10700 this->report_error(_("expected interface or pointer to interface"));
10701 }
10702 else
10703 {
10704 const Typed_identifier* method =
10705 interface_type->find_method(this->name_);
10706 if (method == NULL)
10707 {
10708 error_at(this->location(), "method %qs not in interface",
10709 Gogo::message_name(this->name_).c_str());
10710 this->set_is_error();
10711 }
10712 }
10713 }
10714
10715 // Get a tree for a reference to a field in an interface. There is no
10716 // standard tree type representation for this: it's a function
10717 // attached to its first argument, like a Bound_method_expression.
10718 // The only places it may currently be used are in a Call_expression
10719 // or a Go_statement, which will take it apart directly. So this has
10720 // nothing to do at present.
10721
10722 tree
10723 Interface_field_reference_expression::do_get_tree(Translate_context*)
10724 {
10725 error_at(this->location(), "reference to method other than calling it");
10726 return error_mark_node;
10727 }
10728
10729 // Dump ast representation for an interface field reference.
10730
10731 void
10732 Interface_field_reference_expression::do_dump_expression(
10733 Ast_dump_context* ast_dump_context) const
10734 {
10735 this->expr_->dump_expression(ast_dump_context);
10736 ast_dump_context->ostream() << "." << this->name_;
10737 }
10738
10739 // Make a reference to a field in an interface.
10740
10741 Expression*
10742 Expression::make_interface_field_reference(Expression* expr,
10743 const std::string& field,
10744 Location location)
10745 {
10746 return new Interface_field_reference_expression(expr, field, location);
10747 }
10748
10749 // A general selector. This is a Parser_expression for LEFT.NAME. It
10750 // is lowered after we know the type of the left hand side.
10751
10752 class Selector_expression : public Parser_expression
10753 {
10754 public:
10755 Selector_expression(Expression* left, const std::string& name,
10756 Location location)
10757 : Parser_expression(EXPRESSION_SELECTOR, location),
10758 left_(left), name_(name)
10759 { }
10760
10761 protected:
10762 int
10763 do_traverse(Traverse* traverse)
10764 { return Expression::traverse(&this->left_, traverse); }
10765
10766 Expression*
10767 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
10768
10769 Expression*
10770 do_copy()
10771 {
10772 return new Selector_expression(this->left_->copy(), this->name_,
10773 this->location());
10774 }
10775
10776 void
10777 do_dump_expression(Ast_dump_context* ast_dump_context) const;
10778
10779 private:
10780 Expression*
10781 lower_method_expression(Gogo*);
10782
10783 // The expression on the left hand side.
10784 Expression* left_;
10785 // The name on the right hand side.
10786 std::string name_;
10787 };
10788
10789 // Lower a selector expression once we know the real type of the left
10790 // hand side.
10791
10792 Expression*
10793 Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*,
10794 int)
10795 {
10796 Expression* left = this->left_;
10797 if (left->is_type_expression())
10798 return this->lower_method_expression(gogo);
10799 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
10800 this->location());
10801 }
10802
10803 // Lower a method expression T.M or (*T).M. We turn this into a
10804 // function literal.
10805
10806 Expression*
10807 Selector_expression::lower_method_expression(Gogo* gogo)
10808 {
10809 Location location = this->location();
10810 Type* type = this->left_->type();
10811 const std::string& name(this->name_);
10812
10813 bool is_pointer;
10814 if (type->points_to() == NULL)
10815 is_pointer = false;
10816 else
10817 {
10818 is_pointer = true;
10819 type = type->points_to();
10820 }
10821 Named_type* nt = type->named_type();
10822 if (nt == NULL)
10823 {
10824 error_at(location,
10825 ("method expression requires named type or "
10826 "pointer to named type"));
10827 return Expression::make_error(location);
10828 }
10829
10830 bool is_ambiguous;
10831 Method* method = nt->method_function(name, &is_ambiguous);
10832 const Typed_identifier* imethod = NULL;
10833 if (method == NULL && !is_pointer)
10834 {
10835 Interface_type* it = nt->interface_type();
10836 if (it != NULL)
10837 imethod = it->find_method(name);
10838 }
10839
10840 if (method == NULL && imethod == NULL)
10841 {
10842 if (!is_ambiguous)
10843 error_at(location, "type %<%s%s%> has no method %<%s%>",
10844 is_pointer ? "*" : "",
10845 nt->message_name().c_str(),
10846 Gogo::message_name(name).c_str());
10847 else
10848 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
10849 Gogo::message_name(name).c_str(),
10850 is_pointer ? "*" : "",
10851 nt->message_name().c_str());
10852 return Expression::make_error(location);
10853 }
10854
10855 if (method != NULL && !is_pointer && !method->is_value_method())
10856 {
10857 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
10858 nt->message_name().c_str(),
10859 Gogo::message_name(name).c_str());
10860 return Expression::make_error(location);
10861 }
10862
10863 // Build a new function type in which the receiver becomes the first
10864 // argument.
10865 Function_type* method_type;
10866 if (method != NULL)
10867 {
10868 method_type = method->type();
10869 go_assert(method_type->is_method());
10870 }
10871 else
10872 {
10873 method_type = imethod->type()->function_type();
10874 go_assert(method_type != NULL && !method_type->is_method());
10875 }
10876
10877 const char* const receiver_name = "$this";
10878 Typed_identifier_list* parameters = new Typed_identifier_list();
10879 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
10880 location));
10881
10882 const Typed_identifier_list* method_parameters = method_type->parameters();
10883 if (method_parameters != NULL)
10884 {
10885 int i = 0;
10886 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
10887 p != method_parameters->end();
10888 ++p, ++i)
10889 {
10890 if (!p->name().empty())
10891 parameters->push_back(*p);
10892 else
10893 {
10894 char buf[20];
10895 snprintf(buf, sizeof buf, "$param%d", i);
10896 parameters->push_back(Typed_identifier(buf, p->type(),
10897 p->location()));
10898 }
10899 }
10900 }
10901
10902 const Typed_identifier_list* method_results = method_type->results();
10903 Typed_identifier_list* results;
10904 if (method_results == NULL)
10905 results = NULL;
10906 else
10907 {
10908 results = new Typed_identifier_list();
10909 for (Typed_identifier_list::const_iterator p = method_results->begin();
10910 p != method_results->end();
10911 ++p)
10912 results->push_back(*p);
10913 }
10914
10915 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
10916 location);
10917 if (method_type->is_varargs())
10918 fntype->set_is_varargs();
10919
10920 // We generate methods which always takes a pointer to the receiver
10921 // as their first argument. If this is for a pointer type, we can
10922 // simply reuse the existing function. We use an internal hack to
10923 // get the right type.
10924
10925 if (method != NULL && is_pointer)
10926 {
10927 Named_object* mno = (method->needs_stub_method()
10928 ? method->stub_object()
10929 : method->named_object());
10930 Expression* f = Expression::make_func_reference(mno, NULL, location);
10931 f = Expression::make_cast(fntype, f, location);
10932 Type_conversion_expression* tce =
10933 static_cast<Type_conversion_expression*>(f);
10934 tce->set_may_convert_function_types();
10935 return f;
10936 }
10937
10938 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
10939 location);
10940
10941 Named_object* vno = gogo->lookup(receiver_name, NULL);
10942 go_assert(vno != NULL);
10943 Expression* ve = Expression::make_var_reference(vno, location);
10944 Expression* bm;
10945 if (method != NULL)
10946 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
10947 else
10948 bm = Expression::make_interface_field_reference(ve, name, location);
10949
10950 // Even though we found the method above, if it has an error type we
10951 // may see an error here.
10952 if (bm->is_error_expression())
10953 {
10954 gogo->finish_function(location);
10955 return bm;
10956 }
10957
10958 Expression_list* args;
10959 if (parameters->size() <= 1)
10960 args = NULL;
10961 else
10962 {
10963 args = new Expression_list();
10964 Typed_identifier_list::const_iterator p = parameters->begin();
10965 ++p;
10966 for (; p != parameters->end(); ++p)
10967 {
10968 vno = gogo->lookup(p->name(), NULL);
10969 go_assert(vno != NULL);
10970 args->push_back(Expression::make_var_reference(vno, location));
10971 }
10972 }
10973
10974 gogo->start_block(location);
10975
10976 Call_expression* call = Expression::make_call(bm, args,
10977 method_type->is_varargs(),
10978 location);
10979
10980 size_t count = call->result_count();
10981 Statement* s;
10982 if (count == 0)
10983 s = Statement::make_statement(call, true);
10984 else
10985 {
10986 Expression_list* retvals = new Expression_list();
10987 if (count <= 1)
10988 retvals->push_back(call);
10989 else
10990 {
10991 for (size_t i = 0; i < count; ++i)
10992 retvals->push_back(Expression::make_call_result(call, i));
10993 }
10994 s = Statement::make_return_statement(retvals, location);
10995 }
10996 gogo->add_statement(s);
10997
10998 Block* b = gogo->finish_block(location);
10999
11000 gogo->add_block(b, location);
11001
11002 // Lower the call in case there are multiple results.
11003 gogo->lower_block(no, b);
11004
11005 gogo->finish_function(location);
11006
11007 return Expression::make_func_reference(no, NULL, location);
11008 }
11009
11010 // Dump the ast for a selector expression.
11011
11012 void
11013 Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11014 const
11015 {
11016 ast_dump_context->dump_expression(this->left_);
11017 ast_dump_context->ostream() << ".";
11018 ast_dump_context->ostream() << this->name_;
11019 }
11020
11021 // Make a selector expression.
11022
11023 Expression*
11024 Expression::make_selector(Expression* left, const std::string& name,
11025 Location location)
11026 {
11027 return new Selector_expression(left, name, location);
11028 }
11029
11030 // Implement the builtin function new.
11031
11032 class Allocation_expression : public Expression
11033 {
11034 public:
11035 Allocation_expression(Type* type, Location location)
11036 : Expression(EXPRESSION_ALLOCATION, location),
11037 type_(type)
11038 { }
11039
11040 protected:
11041 int
11042 do_traverse(Traverse* traverse)
11043 { return Type::traverse(this->type_, traverse); }
11044
11045 Type*
11046 do_type()
11047 { return Type::make_pointer_type(this->type_); }
11048
11049 void
11050 do_determine_type(const Type_context*)
11051 { }
11052
11053 Expression*
11054 do_copy()
11055 { return new Allocation_expression(this->type_, this->location()); }
11056
11057 tree
11058 do_get_tree(Translate_context*);
11059
11060 void
11061 do_dump_expression(Ast_dump_context*) const;
11062
11063 private:
11064 // The type we are allocating.
11065 Type* type_;
11066 };
11067
11068 // Return a tree for an allocation expression.
11069
11070 tree
11071 Allocation_expression::do_get_tree(Translate_context* context)
11072 {
11073 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
11074 if (type_tree == error_mark_node)
11075 return error_mark_node;
11076 tree size_tree = TYPE_SIZE_UNIT(type_tree);
11077 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
11078 this->location());
11079 if (space == error_mark_node)
11080 return error_mark_node;
11081 return fold_convert(build_pointer_type(type_tree), space);
11082 }
11083
11084 // Dump ast representation for an allocation expression.
11085
11086 void
11087 Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11088 const
11089 {
11090 ast_dump_context->ostream() << "new(";
11091 ast_dump_context->dump_type(this->type_);
11092 ast_dump_context->ostream() << ")";
11093 }
11094
11095 // Make an allocation expression.
11096
11097 Expression*
11098 Expression::make_allocation(Type* type, Location location)
11099 {
11100 return new Allocation_expression(type, location);
11101 }
11102
11103 // Construct a struct.
11104
11105 class Struct_construction_expression : public Expression
11106 {
11107 public:
11108 Struct_construction_expression(Type* type, Expression_list* vals,
11109 Location location)
11110 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
11111 type_(type), vals_(vals), traverse_order_(NULL)
11112 { }
11113
11114 // Set the traversal order, used to ensure that we implement the
11115 // order of evaluation rules. Takes ownership of the argument.
11116 void
11117 set_traverse_order(std::vector<int>* traverse_order)
11118 { this->traverse_order_ = traverse_order; }
11119
11120 // Return whether this is a constant initializer.
11121 bool
11122 is_constant_struct() const;
11123
11124 protected:
11125 int
11126 do_traverse(Traverse* traverse);
11127
11128 Type*
11129 do_type()
11130 { return this->type_; }
11131
11132 void
11133 do_determine_type(const Type_context*);
11134
11135 void
11136 do_check_types(Gogo*);
11137
11138 Expression*
11139 do_copy()
11140 {
11141 Struct_construction_expression* ret =
11142 new Struct_construction_expression(this->type_, this->vals_->copy(),
11143 this->location());
11144 if (this->traverse_order_ != NULL)
11145 ret->set_traverse_order(this->traverse_order_);
11146 return ret;
11147 }
11148
11149 tree
11150 do_get_tree(Translate_context*);
11151
11152 void
11153 do_export(Export*) const;
11154
11155 void
11156 do_dump_expression(Ast_dump_context*) const;
11157
11158 private:
11159 // The type of the struct to construct.
11160 Type* type_;
11161 // The list of values, in order of the fields in the struct. A NULL
11162 // entry means that the field should be zero-initialized.
11163 Expression_list* vals_;
11164 // If not NULL, the order in which to traverse vals_. This is used
11165 // so that we implement the order of evaluation rules correctly.
11166 std::vector<int>* traverse_order_;
11167 };
11168
11169 // Traversal.
11170
11171 int
11172 Struct_construction_expression::do_traverse(Traverse* traverse)
11173 {
11174 if (this->vals_ != NULL)
11175 {
11176 if (this->traverse_order_ == NULL)
11177 {
11178 if (this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11179 return TRAVERSE_EXIT;
11180 }
11181 else
11182 {
11183 for (std::vector<int>::const_iterator p =
11184 this->traverse_order_->begin();
11185 p != this->traverse_order_->end();
11186 ++p)
11187 {
11188 if (Expression::traverse(&this->vals_->at(*p), traverse)
11189 == TRAVERSE_EXIT)
11190 return TRAVERSE_EXIT;
11191 }
11192 }
11193 }
11194 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11195 return TRAVERSE_EXIT;
11196 return TRAVERSE_CONTINUE;
11197 }
11198
11199 // Return whether this is a constant initializer.
11200
11201 bool
11202 Struct_construction_expression::is_constant_struct() const
11203 {
11204 if (this->vals_ == NULL)
11205 return true;
11206 for (Expression_list::const_iterator pv = this->vals_->begin();
11207 pv != this->vals_->end();
11208 ++pv)
11209 {
11210 if (*pv != NULL
11211 && !(*pv)->is_constant()
11212 && (!(*pv)->is_composite_literal()
11213 || (*pv)->is_nonconstant_composite_literal()))
11214 return false;
11215 }
11216
11217 const Struct_field_list* fields = this->type_->struct_type()->fields();
11218 for (Struct_field_list::const_iterator pf = fields->begin();
11219 pf != fields->end();
11220 ++pf)
11221 {
11222 // There are no constant constructors for interfaces.
11223 if (pf->type()->interface_type() != NULL)
11224 return false;
11225 }
11226
11227 return true;
11228 }
11229
11230 // Final type determination.
11231
11232 void
11233 Struct_construction_expression::do_determine_type(const Type_context*)
11234 {
11235 if (this->vals_ == NULL)
11236 return;
11237 const Struct_field_list* fields = this->type_->struct_type()->fields();
11238 Expression_list::const_iterator pv = this->vals_->begin();
11239 for (Struct_field_list::const_iterator pf = fields->begin();
11240 pf != fields->end();
11241 ++pf, ++pv)
11242 {
11243 if (pv == this->vals_->end())
11244 return;
11245 if (*pv != NULL)
11246 {
11247 Type_context subcontext(pf->type(), false);
11248 (*pv)->determine_type(&subcontext);
11249 }
11250 }
11251 // Extra values are an error we will report elsewhere; we still want
11252 // to determine the type to avoid knockon errors.
11253 for (; pv != this->vals_->end(); ++pv)
11254 (*pv)->determine_type_no_context();
11255 }
11256
11257 // Check types.
11258
11259 void
11260 Struct_construction_expression::do_check_types(Gogo*)
11261 {
11262 if (this->vals_ == NULL)
11263 return;
11264
11265 Struct_type* st = this->type_->struct_type();
11266 if (this->vals_->size() > st->field_count())
11267 {
11268 this->report_error(_("too many expressions for struct"));
11269 return;
11270 }
11271
11272 const Struct_field_list* fields = st->fields();
11273 Expression_list::const_iterator pv = this->vals_->begin();
11274 int i = 0;
11275 for (Struct_field_list::const_iterator pf = fields->begin();
11276 pf != fields->end();
11277 ++pf, ++pv, ++i)
11278 {
11279 if (pv == this->vals_->end())
11280 {
11281 this->report_error(_("too few expressions for struct"));
11282 break;
11283 }
11284
11285 if (*pv == NULL)
11286 continue;
11287
11288 std::string reason;
11289 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
11290 {
11291 if (reason.empty())
11292 error_at((*pv)->location(),
11293 "incompatible type for field %d in struct construction",
11294 i + 1);
11295 else
11296 error_at((*pv)->location(),
11297 ("incompatible type for field %d in "
11298 "struct construction (%s)"),
11299 i + 1, reason.c_str());
11300 this->set_is_error();
11301 }
11302 }
11303 go_assert(pv == this->vals_->end());
11304 }
11305
11306 // Return a tree for constructing a struct.
11307
11308 tree
11309 Struct_construction_expression::do_get_tree(Translate_context* context)
11310 {
11311 Gogo* gogo = context->gogo();
11312
11313 if (this->vals_ == NULL)
11314 {
11315 Btype* btype = this->type_->get_backend(gogo);
11316 return expr_to_tree(gogo->backend()->zero_expression(btype));
11317 }
11318
11319 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
11320 if (type_tree == error_mark_node)
11321 return error_mark_node;
11322 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11323
11324 bool is_constant = true;
11325 const Struct_field_list* fields = this->type_->struct_type()->fields();
11326 VEC(constructor_elt,gc)* elts = VEC_alloc(constructor_elt, gc,
11327 fields->size());
11328 Struct_field_list::const_iterator pf = fields->begin();
11329 Expression_list::const_iterator pv = this->vals_->begin();
11330 for (tree field = TYPE_FIELDS(type_tree);
11331 field != NULL_TREE;
11332 field = DECL_CHAIN(field), ++pf)
11333 {
11334 go_assert(pf != fields->end());
11335
11336 Btype* fbtype = pf->type()->get_backend(gogo);
11337
11338 tree val;
11339 if (pv == this->vals_->end())
11340 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
11341 else if (*pv == NULL)
11342 {
11343 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
11344 ++pv;
11345 }
11346 else
11347 {
11348 val = Expression::convert_for_assignment(context, pf->type(),
11349 (*pv)->type(),
11350 (*pv)->get_tree(context),
11351 this->location());
11352 ++pv;
11353 }
11354
11355 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
11356 return error_mark_node;
11357
11358 constructor_elt* elt = VEC_quick_push(constructor_elt, elts, NULL);
11359 elt->index = field;
11360 elt->value = val;
11361 if (!TREE_CONSTANT(val))
11362 is_constant = false;
11363 }
11364 go_assert(pf == fields->end());
11365
11366 tree ret = build_constructor(type_tree, elts);
11367 if (is_constant)
11368 TREE_CONSTANT(ret) = 1;
11369 return ret;
11370 }
11371
11372 // Export a struct construction.
11373
11374 void
11375 Struct_construction_expression::do_export(Export* exp) const
11376 {
11377 exp->write_c_string("convert(");
11378 exp->write_type(this->type_);
11379 for (Expression_list::const_iterator pv = this->vals_->begin();
11380 pv != this->vals_->end();
11381 ++pv)
11382 {
11383 exp->write_c_string(", ");
11384 if (*pv != NULL)
11385 (*pv)->export_expression(exp);
11386 }
11387 exp->write_c_string(")");
11388 }
11389
11390 // Dump ast representation of a struct construction expression.
11391
11392 void
11393 Struct_construction_expression::do_dump_expression(
11394 Ast_dump_context* ast_dump_context) const
11395 {
11396 ast_dump_context->dump_type(this->type_);
11397 ast_dump_context->ostream() << "{";
11398 ast_dump_context->dump_expression_list(this->vals_);
11399 ast_dump_context->ostream() << "}";
11400 }
11401
11402 // Make a struct composite literal. This used by the thunk code.
11403
11404 Expression*
11405 Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
11406 Location location)
11407 {
11408 go_assert(type->struct_type() != NULL);
11409 return new Struct_construction_expression(type, vals, location);
11410 }
11411
11412 // Construct an array. This class is not used directly; instead we
11413 // use the child classes, Fixed_array_construction_expression and
11414 // Open_array_construction_expression.
11415
11416 class Array_construction_expression : public Expression
11417 {
11418 protected:
11419 Array_construction_expression(Expression_classification classification,
11420 Type* type,
11421 const std::vector<unsigned long>* indexes,
11422 Expression_list* vals, Location location)
11423 : Expression(classification, location),
11424 type_(type), indexes_(indexes), vals_(vals)
11425 { go_assert(indexes == NULL || indexes->size() == vals->size()); }
11426
11427 public:
11428 // Return whether this is a constant initializer.
11429 bool
11430 is_constant_array() const;
11431
11432 // Return the number of elements.
11433 size_t
11434 element_count() const
11435 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
11436
11437 protected:
11438 int
11439 do_traverse(Traverse* traverse);
11440
11441 Type*
11442 do_type()
11443 { return this->type_; }
11444
11445 void
11446 do_determine_type(const Type_context*);
11447
11448 void
11449 do_check_types(Gogo*);
11450
11451 void
11452 do_export(Export*) const;
11453
11454 // The indexes.
11455 const std::vector<unsigned long>*
11456 indexes()
11457 { return this->indexes_; }
11458
11459 // The list of values.
11460 Expression_list*
11461 vals()
11462 { return this->vals_; }
11463
11464 // Get a constructor tree for the array values.
11465 tree
11466 get_constructor_tree(Translate_context* context, tree type_tree);
11467
11468 void
11469 do_dump_expression(Ast_dump_context*) const;
11470
11471 private:
11472 // The type of the array to construct.
11473 Type* type_;
11474 // The list of indexes into the array, one for each value. This may
11475 // be NULL, in which case the indexes start at zero and increment.
11476 const std::vector<unsigned long>* indexes_;
11477 // The list of values. This may be NULL if there are no values.
11478 Expression_list* vals_;
11479 };
11480
11481 // Traversal.
11482
11483 int
11484 Array_construction_expression::do_traverse(Traverse* traverse)
11485 {
11486 if (this->vals_ != NULL
11487 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11488 return TRAVERSE_EXIT;
11489 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11490 return TRAVERSE_EXIT;
11491 return TRAVERSE_CONTINUE;
11492 }
11493
11494 // Return whether this is a constant initializer.
11495
11496 bool
11497 Array_construction_expression::is_constant_array() const
11498 {
11499 if (this->vals_ == NULL)
11500 return true;
11501
11502 // There are no constant constructors for interfaces.
11503 if (this->type_->array_type()->element_type()->interface_type() != NULL)
11504 return false;
11505
11506 for (Expression_list::const_iterator pv = this->vals_->begin();
11507 pv != this->vals_->end();
11508 ++pv)
11509 {
11510 if (*pv != NULL
11511 && !(*pv)->is_constant()
11512 && (!(*pv)->is_composite_literal()
11513 || (*pv)->is_nonconstant_composite_literal()))
11514 return false;
11515 }
11516 return true;
11517 }
11518
11519 // Final type determination.
11520
11521 void
11522 Array_construction_expression::do_determine_type(const Type_context*)
11523 {
11524 if (this->vals_ == NULL)
11525 return;
11526 Type_context subcontext(this->type_->array_type()->element_type(), false);
11527 for (Expression_list::const_iterator pv = this->vals_->begin();
11528 pv != this->vals_->end();
11529 ++pv)
11530 {
11531 if (*pv != NULL)
11532 (*pv)->determine_type(&subcontext);
11533 }
11534 }
11535
11536 // Check types.
11537
11538 void
11539 Array_construction_expression::do_check_types(Gogo*)
11540 {
11541 if (this->vals_ == NULL)
11542 return;
11543
11544 Array_type* at = this->type_->array_type();
11545 int i = 0;
11546 Type* element_type = at->element_type();
11547 for (Expression_list::const_iterator pv = this->vals_->begin();
11548 pv != this->vals_->end();
11549 ++pv, ++i)
11550 {
11551 if (*pv != NULL
11552 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
11553 {
11554 error_at((*pv)->location(),
11555 "incompatible type for element %d in composite literal",
11556 i + 1);
11557 this->set_is_error();
11558 }
11559 }
11560 }
11561
11562 // Get a constructor tree for the array values.
11563
11564 tree
11565 Array_construction_expression::get_constructor_tree(Translate_context* context,
11566 tree type_tree)
11567 {
11568 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
11569 (this->vals_ == NULL
11570 ? 0
11571 : this->vals_->size()));
11572 Type* element_type = this->type_->array_type()->element_type();
11573 bool is_constant = true;
11574 if (this->vals_ != NULL)
11575 {
11576 size_t i = 0;
11577 std::vector<unsigned long>::const_iterator pi;
11578 if (this->indexes_ != NULL)
11579 pi = this->indexes_->begin();
11580 for (Expression_list::const_iterator pv = this->vals_->begin();
11581 pv != this->vals_->end();
11582 ++pv, ++i)
11583 {
11584 if (this->indexes_ != NULL)
11585 go_assert(pi != this->indexes_->end());
11586 constructor_elt* elt = VEC_quick_push(constructor_elt, values, NULL);
11587
11588 if (this->indexes_ == NULL)
11589 elt->index = size_int(i);
11590 else
11591 elt->index = size_int(*pi);
11592
11593 if (*pv == NULL)
11594 {
11595 Gogo* gogo = context->gogo();
11596 Btype* ebtype = element_type->get_backend(gogo);
11597 Bexpression *zv = gogo->backend()->zero_expression(ebtype);
11598 elt->value = expr_to_tree(zv);
11599 }
11600 else
11601 {
11602 tree value_tree = (*pv)->get_tree(context);
11603 elt->value = Expression::convert_for_assignment(context,
11604 element_type,
11605 (*pv)->type(),
11606 value_tree,
11607 this->location());
11608 }
11609 if (elt->value == error_mark_node)
11610 return error_mark_node;
11611 if (!TREE_CONSTANT(elt->value))
11612 is_constant = false;
11613 if (this->indexes_ != NULL)
11614 ++pi;
11615 }
11616 if (this->indexes_ != NULL)
11617 go_assert(pi == this->indexes_->end());
11618 }
11619
11620 tree ret = build_constructor(type_tree, values);
11621 if (is_constant)
11622 TREE_CONSTANT(ret) = 1;
11623 return ret;
11624 }
11625
11626 // Export an array construction.
11627
11628 void
11629 Array_construction_expression::do_export(Export* exp) const
11630 {
11631 exp->write_c_string("convert(");
11632 exp->write_type(this->type_);
11633 if (this->vals_ != NULL)
11634 {
11635 std::vector<unsigned long>::const_iterator pi;
11636 if (this->indexes_ != NULL)
11637 pi = this->indexes_->begin();
11638 for (Expression_list::const_iterator pv = this->vals_->begin();
11639 pv != this->vals_->end();
11640 ++pv)
11641 {
11642 exp->write_c_string(", ");
11643
11644 if (this->indexes_ != NULL)
11645 {
11646 char buf[100];
11647 snprintf(buf, sizeof buf, "%lu", *pi);
11648 exp->write_c_string(buf);
11649 exp->write_c_string(":");
11650 }
11651
11652 if (*pv != NULL)
11653 (*pv)->export_expression(exp);
11654
11655 if (this->indexes_ != NULL)
11656 ++pi;
11657 }
11658 }
11659 exp->write_c_string(")");
11660 }
11661
11662 // Dump ast representation of an array construction expressin.
11663
11664 void
11665 Array_construction_expression::do_dump_expression(
11666 Ast_dump_context* ast_dump_context) const
11667 {
11668 Expression* length = this->type_->array_type()->length();
11669
11670 ast_dump_context->ostream() << "[" ;
11671 if (length != NULL)
11672 {
11673 ast_dump_context->dump_expression(length);
11674 }
11675 ast_dump_context->ostream() << "]" ;
11676 ast_dump_context->dump_type(this->type_);
11677 ast_dump_context->ostream() << "{" ;
11678 if (this->indexes_ == NULL)
11679 ast_dump_context->dump_expression_list(this->vals_);
11680 else
11681 {
11682 Expression_list::const_iterator pv = this->vals_->begin();
11683 for (std::vector<unsigned long>::const_iterator pi =
11684 this->indexes_->begin();
11685 pi != this->indexes_->end();
11686 ++pi, ++pv)
11687 {
11688 if (pi != this->indexes_->begin())
11689 ast_dump_context->ostream() << ", ";
11690 ast_dump_context->ostream() << *pi << ':';
11691 ast_dump_context->dump_expression(*pv);
11692 }
11693 }
11694 ast_dump_context->ostream() << "}" ;
11695
11696 }
11697
11698 // Construct a fixed array.
11699
11700 class Fixed_array_construction_expression :
11701 public Array_construction_expression
11702 {
11703 public:
11704 Fixed_array_construction_expression(Type* type,
11705 const std::vector<unsigned long>* indexes,
11706 Expression_list* vals, Location location)
11707 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
11708 type, indexes, vals, location)
11709 { go_assert(type->array_type() != NULL && !type->is_slice_type()); }
11710
11711 protected:
11712 Expression*
11713 do_copy()
11714 {
11715 return new Fixed_array_construction_expression(this->type(),
11716 this->indexes(),
11717 (this->vals() == NULL
11718 ? NULL
11719 : this->vals()->copy()),
11720 this->location());
11721 }
11722
11723 tree
11724 do_get_tree(Translate_context*);
11725 };
11726
11727 // Return a tree for constructing a fixed array.
11728
11729 tree
11730 Fixed_array_construction_expression::do_get_tree(Translate_context* context)
11731 {
11732 Type* type = this->type();
11733 Btype* btype = type->get_backend(context->gogo());
11734 return this->get_constructor_tree(context, type_to_tree(btype));
11735 }
11736
11737 // Construct an open array.
11738
11739 class Open_array_construction_expression : public Array_construction_expression
11740 {
11741 public:
11742 Open_array_construction_expression(Type* type,
11743 const std::vector<unsigned long>* indexes,
11744 Expression_list* vals, Location location)
11745 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
11746 type, indexes, vals, location)
11747 { go_assert(type->is_slice_type()); }
11748
11749 protected:
11750 // Note that taking the address of an open array literal is invalid.
11751
11752 Expression*
11753 do_copy()
11754 {
11755 return new Open_array_construction_expression(this->type(),
11756 this->indexes(),
11757 (this->vals() == NULL
11758 ? NULL
11759 : this->vals()->copy()),
11760 this->location());
11761 }
11762
11763 tree
11764 do_get_tree(Translate_context*);
11765 };
11766
11767 // Return a tree for constructing an open array.
11768
11769 tree
11770 Open_array_construction_expression::do_get_tree(Translate_context* context)
11771 {
11772 Array_type* array_type = this->type()->array_type();
11773 if (array_type == NULL)
11774 {
11775 go_assert(this->type()->is_error());
11776 return error_mark_node;
11777 }
11778
11779 Type* element_type = array_type->element_type();
11780 Btype* belement_type = element_type->get_backend(context->gogo());
11781 tree element_type_tree = type_to_tree(belement_type);
11782 if (element_type_tree == error_mark_node)
11783 return error_mark_node;
11784
11785 tree values;
11786 tree length_tree;
11787 if (this->vals() == NULL || this->vals()->empty())
11788 {
11789 // We need to create a unique value.
11790 tree max = size_int(0);
11791 tree constructor_type = build_array_type(element_type_tree,
11792 build_index_type(max));
11793 if (constructor_type == error_mark_node)
11794 return error_mark_node;
11795 VEC(constructor_elt,gc)* vec = VEC_alloc(constructor_elt, gc, 1);
11796 constructor_elt* elt = VEC_quick_push(constructor_elt, vec, NULL);
11797 elt->index = size_int(0);
11798 Gogo* gogo = context->gogo();
11799 Btype* btype = element_type->get_backend(gogo);
11800 elt->value = expr_to_tree(gogo->backend()->zero_expression(btype));
11801 values = build_constructor(constructor_type, vec);
11802 if (TREE_CONSTANT(elt->value))
11803 TREE_CONSTANT(values) = 1;
11804 length_tree = size_int(0);
11805 }
11806 else
11807 {
11808 unsigned long max_index;
11809 if (this->indexes() == NULL)
11810 max_index = this->vals()->size() - 1;
11811 else
11812 max_index = this->indexes()->back();
11813 tree max_tree = size_int(max_index);
11814 tree constructor_type = build_array_type(element_type_tree,
11815 build_index_type(max_tree));
11816 if (constructor_type == error_mark_node)
11817 return error_mark_node;
11818 values = this->get_constructor_tree(context, constructor_type);
11819 length_tree = size_int(max_index + 1);
11820 }
11821
11822 if (values == error_mark_node)
11823 return error_mark_node;
11824
11825 bool is_constant_initializer = TREE_CONSTANT(values);
11826
11827 // We have to copy the initial values into heap memory if we are in
11828 // a function or if the values are not constants. We also have to
11829 // copy them if they may contain pointers in a non-constant context,
11830 // as otherwise the garbage collector won't see them.
11831 bool copy_to_heap = (context->function() != NULL
11832 || !is_constant_initializer
11833 || (element_type->has_pointer()
11834 && !context->is_const()));
11835
11836 if (is_constant_initializer)
11837 {
11838 tree tmp = build_decl(this->location().gcc_location(), VAR_DECL,
11839 create_tmp_var_name("C"), TREE_TYPE(values));
11840 DECL_EXTERNAL(tmp) = 0;
11841 TREE_PUBLIC(tmp) = 0;
11842 TREE_STATIC(tmp) = 1;
11843 DECL_ARTIFICIAL(tmp) = 1;
11844 if (copy_to_heap)
11845 {
11846 // If we are not copying the value to the heap, we will only
11847 // initialize the value once, so we can use this directly
11848 // rather than copying it. In that case we can't make it
11849 // read-only, because the program is permitted to change it.
11850 TREE_READONLY(tmp) = 1;
11851 TREE_CONSTANT(tmp) = 1;
11852 }
11853 DECL_INITIAL(tmp) = values;
11854 rest_of_decl_compilation(tmp, 1, 0);
11855 values = tmp;
11856 }
11857
11858 tree space;
11859 tree set;
11860 if (!copy_to_heap)
11861 {
11862 // the initializer will only run once.
11863 space = build_fold_addr_expr(values);
11864 set = NULL_TREE;
11865 }
11866 else
11867 {
11868 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
11869 space = context->gogo()->allocate_memory(element_type, memsize,
11870 this->location());
11871 space = save_expr(space);
11872
11873 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
11874 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
11875 s);
11876 TREE_THIS_NOTRAP(ref) = 1;
11877 set = build2(MODIFY_EXPR, void_type_node, ref, values);
11878 }
11879
11880 // Build a constructor for the open array.
11881
11882 tree type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
11883 if (type_tree == error_mark_node)
11884 return error_mark_node;
11885 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
11886
11887 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
11888
11889 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
11890 tree field = TYPE_FIELDS(type_tree);
11891 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
11892 elt->index = field;
11893 elt->value = fold_convert(TREE_TYPE(field), space);
11894
11895 elt = VEC_quick_push(constructor_elt, init, NULL);
11896 field = DECL_CHAIN(field);
11897 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
11898 elt->index = field;
11899 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11900
11901 elt = VEC_quick_push(constructor_elt, init, NULL);
11902 field = DECL_CHAIN(field);
11903 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
11904 elt->index = field;
11905 elt->value = fold_convert(TREE_TYPE(field), length_tree);
11906
11907 tree constructor = build_constructor(type_tree, init);
11908 if (constructor == error_mark_node)
11909 return error_mark_node;
11910 if (!copy_to_heap)
11911 TREE_CONSTANT(constructor) = 1;
11912
11913 if (set == NULL_TREE)
11914 return constructor;
11915 else
11916 return build2(COMPOUND_EXPR, type_tree, set, constructor);
11917 }
11918
11919 // Make a slice composite literal. This is used by the type
11920 // descriptor code.
11921
11922 Expression*
11923 Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
11924 Location location)
11925 {
11926 go_assert(type->is_slice_type());
11927 return new Open_array_construction_expression(type, NULL, vals, location);
11928 }
11929
11930 // Construct a map.
11931
11932 class Map_construction_expression : public Expression
11933 {
11934 public:
11935 Map_construction_expression(Type* type, Expression_list* vals,
11936 Location location)
11937 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
11938 type_(type), vals_(vals)
11939 { go_assert(vals == NULL || vals->size() % 2 == 0); }
11940
11941 protected:
11942 int
11943 do_traverse(Traverse* traverse);
11944
11945 Type*
11946 do_type()
11947 { return this->type_; }
11948
11949 void
11950 do_determine_type(const Type_context*);
11951
11952 void
11953 do_check_types(Gogo*);
11954
11955 Expression*
11956 do_copy()
11957 {
11958 return new Map_construction_expression(this->type_, this->vals_->copy(),
11959 this->location());
11960 }
11961
11962 tree
11963 do_get_tree(Translate_context*);
11964
11965 void
11966 do_export(Export*) const;
11967
11968 void
11969 do_dump_expression(Ast_dump_context*) const;
11970
11971 private:
11972 // The type of the map to construct.
11973 Type* type_;
11974 // The list of values.
11975 Expression_list* vals_;
11976 };
11977
11978 // Traversal.
11979
11980 int
11981 Map_construction_expression::do_traverse(Traverse* traverse)
11982 {
11983 if (this->vals_ != NULL
11984 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
11985 return TRAVERSE_EXIT;
11986 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
11987 return TRAVERSE_EXIT;
11988 return TRAVERSE_CONTINUE;
11989 }
11990
11991 // Final type determination.
11992
11993 void
11994 Map_construction_expression::do_determine_type(const Type_context*)
11995 {
11996 if (this->vals_ == NULL)
11997 return;
11998
11999 Map_type* mt = this->type_->map_type();
12000 Type_context key_context(mt->key_type(), false);
12001 Type_context val_context(mt->val_type(), false);
12002 for (Expression_list::const_iterator pv = this->vals_->begin();
12003 pv != this->vals_->end();
12004 ++pv)
12005 {
12006 (*pv)->determine_type(&key_context);
12007 ++pv;
12008 (*pv)->determine_type(&val_context);
12009 }
12010 }
12011
12012 // Check types.
12013
12014 void
12015 Map_construction_expression::do_check_types(Gogo*)
12016 {
12017 if (this->vals_ == NULL)
12018 return;
12019
12020 Map_type* mt = this->type_->map_type();
12021 int i = 0;
12022 Type* key_type = mt->key_type();
12023 Type* val_type = mt->val_type();
12024 for (Expression_list::const_iterator pv = this->vals_->begin();
12025 pv != this->vals_->end();
12026 ++pv, ++i)
12027 {
12028 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
12029 {
12030 error_at((*pv)->location(),
12031 "incompatible type for element %d key in map construction",
12032 i + 1);
12033 this->set_is_error();
12034 }
12035 ++pv;
12036 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
12037 {
12038 error_at((*pv)->location(),
12039 ("incompatible type for element %d value "
12040 "in map construction"),
12041 i + 1);
12042 this->set_is_error();
12043 }
12044 }
12045 }
12046
12047 // Return a tree for constructing a map.
12048
12049 tree
12050 Map_construction_expression::do_get_tree(Translate_context* context)
12051 {
12052 Gogo* gogo = context->gogo();
12053 Location loc = this->location();
12054
12055 Map_type* mt = this->type_->map_type();
12056
12057 // Build a struct to hold the key and value.
12058 tree struct_type = make_node(RECORD_TYPE);
12059
12060 Type* key_type = mt->key_type();
12061 tree id = get_identifier("__key");
12062 tree key_type_tree = type_to_tree(key_type->get_backend(gogo));
12063 if (key_type_tree == error_mark_node)
12064 return error_mark_node;
12065 tree key_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12066 key_type_tree);
12067 DECL_CONTEXT(key_field) = struct_type;
12068 TYPE_FIELDS(struct_type) = key_field;
12069
12070 Type* val_type = mt->val_type();
12071 id = get_identifier("__val");
12072 tree val_type_tree = type_to_tree(val_type->get_backend(gogo));
12073 if (val_type_tree == error_mark_node)
12074 return error_mark_node;
12075 tree val_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
12076 val_type_tree);
12077 DECL_CONTEXT(val_field) = struct_type;
12078 DECL_CHAIN(key_field) = val_field;
12079
12080 layout_type(struct_type);
12081
12082 bool is_constant = true;
12083 size_t i = 0;
12084 tree valaddr;
12085 tree make_tmp;
12086
12087 if (this->vals_ == NULL || this->vals_->empty())
12088 {
12089 valaddr = null_pointer_node;
12090 make_tmp = NULL_TREE;
12091 }
12092 else
12093 {
12094 VEC(constructor_elt,gc)* values = VEC_alloc(constructor_elt, gc,
12095 this->vals_->size() / 2);
12096
12097 for (Expression_list::const_iterator pv = this->vals_->begin();
12098 pv != this->vals_->end();
12099 ++pv, ++i)
12100 {
12101 bool one_is_constant = true;
12102
12103 VEC(constructor_elt,gc)* one = VEC_alloc(constructor_elt, gc, 2);
12104
12105 constructor_elt* elt = VEC_quick_push(constructor_elt, one, NULL);
12106 elt->index = key_field;
12107 tree val_tree = (*pv)->get_tree(context);
12108 elt->value = Expression::convert_for_assignment(context, key_type,
12109 (*pv)->type(),
12110 val_tree, loc);
12111 if (elt->value == error_mark_node)
12112 return error_mark_node;
12113 if (!TREE_CONSTANT(elt->value))
12114 one_is_constant = false;
12115
12116 ++pv;
12117
12118 elt = VEC_quick_push(constructor_elt, one, NULL);
12119 elt->index = val_field;
12120 val_tree = (*pv)->get_tree(context);
12121 elt->value = Expression::convert_for_assignment(context, val_type,
12122 (*pv)->type(),
12123 val_tree, loc);
12124 if (elt->value == error_mark_node)
12125 return error_mark_node;
12126 if (!TREE_CONSTANT(elt->value))
12127 one_is_constant = false;
12128
12129 elt = VEC_quick_push(constructor_elt, values, NULL);
12130 elt->index = size_int(i);
12131 elt->value = build_constructor(struct_type, one);
12132 if (one_is_constant)
12133 TREE_CONSTANT(elt->value) = 1;
12134 else
12135 is_constant = false;
12136 }
12137
12138 tree index_type = build_index_type(size_int(i - 1));
12139 tree array_type = build_array_type(struct_type, index_type);
12140 tree init = build_constructor(array_type, values);
12141 if (is_constant)
12142 TREE_CONSTANT(init) = 1;
12143 tree tmp;
12144 if (current_function_decl != NULL)
12145 {
12146 tmp = create_tmp_var(array_type, get_name(array_type));
12147 DECL_INITIAL(tmp) = init;
12148 make_tmp = fold_build1_loc(loc.gcc_location(), DECL_EXPR,
12149 void_type_node, tmp);
12150 TREE_ADDRESSABLE(tmp) = 1;
12151 }
12152 else
12153 {
12154 tmp = build_decl(loc.gcc_location(), VAR_DECL,
12155 create_tmp_var_name("M"), array_type);
12156 DECL_EXTERNAL(tmp) = 0;
12157 TREE_PUBLIC(tmp) = 0;
12158 TREE_STATIC(tmp) = 1;
12159 DECL_ARTIFICIAL(tmp) = 1;
12160 if (!TREE_CONSTANT(init))
12161 make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR,
12162 void_type_node, tmp, init);
12163 else
12164 {
12165 TREE_READONLY(tmp) = 1;
12166 TREE_CONSTANT(tmp) = 1;
12167 DECL_INITIAL(tmp) = init;
12168 make_tmp = NULL_TREE;
12169 }
12170 rest_of_decl_compilation(tmp, 1, 0);
12171 }
12172
12173 valaddr = build_fold_addr_expr(tmp);
12174 }
12175
12176 tree descriptor = mt->map_descriptor_pointer(gogo, loc);
12177
12178 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
12179 if (type_tree == error_mark_node)
12180 return error_mark_node;
12181
12182 static tree construct_map_fndecl;
12183 tree call = Gogo::call_builtin(&construct_map_fndecl,
12184 loc,
12185 "__go_construct_map",
12186 6,
12187 type_tree,
12188 TREE_TYPE(descriptor),
12189 descriptor,
12190 sizetype,
12191 size_int(i),
12192 sizetype,
12193 TYPE_SIZE_UNIT(struct_type),
12194 sizetype,
12195 byte_position(val_field),
12196 sizetype,
12197 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
12198 const_ptr_type_node,
12199 fold_convert(const_ptr_type_node, valaddr));
12200 if (call == error_mark_node)
12201 return error_mark_node;
12202
12203 tree ret;
12204 if (make_tmp == NULL)
12205 ret = call;
12206 else
12207 ret = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, type_tree,
12208 make_tmp, call);
12209 return ret;
12210 }
12211
12212 // Export an array construction.
12213
12214 void
12215 Map_construction_expression::do_export(Export* exp) const
12216 {
12217 exp->write_c_string("convert(");
12218 exp->write_type(this->type_);
12219 for (Expression_list::const_iterator pv = this->vals_->begin();
12220 pv != this->vals_->end();
12221 ++pv)
12222 {
12223 exp->write_c_string(", ");
12224 (*pv)->export_expression(exp);
12225 }
12226 exp->write_c_string(")");
12227 }
12228
12229 // Dump ast representation for a map construction expression.
12230
12231 void
12232 Map_construction_expression::do_dump_expression(
12233 Ast_dump_context* ast_dump_context) const
12234 {
12235 ast_dump_context->ostream() << "{" ;
12236 ast_dump_context->dump_expression_list(this->vals_, true);
12237 ast_dump_context->ostream() << "}";
12238 }
12239
12240 // A general composite literal. This is lowered to a type specific
12241 // version.
12242
12243 class Composite_literal_expression : public Parser_expression
12244 {
12245 public:
12246 Composite_literal_expression(Type* type, int depth, bool has_keys,
12247 Expression_list* vals, Location location)
12248 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
12249 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys)
12250 { }
12251
12252 protected:
12253 int
12254 do_traverse(Traverse* traverse);
12255
12256 Expression*
12257 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
12258
12259 Expression*
12260 do_copy()
12261 {
12262 return new Composite_literal_expression(this->type_, this->depth_,
12263 this->has_keys_,
12264 (this->vals_ == NULL
12265 ? NULL
12266 : this->vals_->copy()),
12267 this->location());
12268 }
12269
12270 void
12271 do_dump_expression(Ast_dump_context*) const;
12272
12273 private:
12274 Expression*
12275 lower_struct(Gogo*, Type*);
12276
12277 Expression*
12278 lower_array(Type*);
12279
12280 Expression*
12281 make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
12282
12283 Expression*
12284 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
12285
12286 // The type of the composite literal.
12287 Type* type_;
12288 // The depth within a list of composite literals within a composite
12289 // literal, when the type is omitted.
12290 int depth_;
12291 // The values to put in the composite literal.
12292 Expression_list* vals_;
12293 // If this is true, then VALS_ is a list of pairs: a key and a
12294 // value. In an array initializer, a missing key will be NULL.
12295 bool has_keys_;
12296 };
12297
12298 // Traversal.
12299
12300 int
12301 Composite_literal_expression::do_traverse(Traverse* traverse)
12302 {
12303 if (this->vals_ != NULL
12304 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12305 return TRAVERSE_EXIT;
12306 return Type::traverse(this->type_, traverse);
12307 }
12308
12309 // Lower a generic composite literal into a specific version based on
12310 // the type.
12311
12312 Expression*
12313 Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
12314 Statement_inserter* inserter, int)
12315 {
12316 Type* type = this->type_;
12317
12318 for (int depth = this->depth_; depth > 0; --depth)
12319 {
12320 if (type->array_type() != NULL)
12321 type = type->array_type()->element_type();
12322 else if (type->map_type() != NULL)
12323 type = type->map_type()->val_type();
12324 else
12325 {
12326 if (!type->is_error())
12327 error_at(this->location(),
12328 ("may only omit types within composite literals "
12329 "of slice, array, or map type"));
12330 return Expression::make_error(this->location());
12331 }
12332 }
12333
12334 Type *pt = type->points_to();
12335 bool is_pointer = false;
12336 if (pt != NULL)
12337 {
12338 is_pointer = true;
12339 type = pt;
12340 }
12341
12342 Expression* ret;
12343 if (type->is_error())
12344 return Expression::make_error(this->location());
12345 else if (type->struct_type() != NULL)
12346 ret = this->lower_struct(gogo, type);
12347 else if (type->array_type() != NULL)
12348 ret = this->lower_array(type);
12349 else if (type->map_type() != NULL)
12350 ret = this->lower_map(gogo, function, inserter, type);
12351 else
12352 {
12353 error_at(this->location(),
12354 ("expected struct, slice, array, or map type "
12355 "for composite literal"));
12356 return Expression::make_error(this->location());
12357 }
12358
12359 if (is_pointer)
12360 ret = Expression::make_heap_composite(ret, this->location());
12361
12362 return ret;
12363 }
12364
12365 // Lower a struct composite literal.
12366
12367 Expression*
12368 Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
12369 {
12370 Location location = this->location();
12371 Struct_type* st = type->struct_type();
12372 if (this->vals_ == NULL || !this->has_keys_)
12373 {
12374 if (this->vals_ != NULL
12375 && !this->vals_->empty()
12376 && type->named_type() != NULL
12377 && type->named_type()->named_object()->package() != NULL)
12378 {
12379 for (Struct_field_list::const_iterator pf = st->fields()->begin();
12380 pf != st->fields()->end();
12381 ++pf)
12382 {
12383 if (Gogo::is_hidden_name(pf->field_name()))
12384 error_at(this->location(),
12385 "assignment of unexported field %qs in %qs literal",
12386 Gogo::message_name(pf->field_name()).c_str(),
12387 type->named_type()->message_name().c_str());
12388 }
12389 }
12390
12391 return new Struct_construction_expression(type, this->vals_, location);
12392 }
12393
12394 size_t field_count = st->field_count();
12395 std::vector<Expression*> vals(field_count);
12396 std::vector<int>* traverse_order = new(std::vector<int>);
12397 Expression_list::const_iterator p = this->vals_->begin();
12398 while (p != this->vals_->end())
12399 {
12400 Expression* name_expr = *p;
12401
12402 ++p;
12403 go_assert(p != this->vals_->end());
12404 Expression* val = *p;
12405
12406 ++p;
12407
12408 if (name_expr == NULL)
12409 {
12410 error_at(val->location(), "mixture of field and value initializers");
12411 return Expression::make_error(location);
12412 }
12413
12414 bool bad_key = false;
12415 std::string name;
12416 const Named_object* no = NULL;
12417 switch (name_expr->classification())
12418 {
12419 case EXPRESSION_UNKNOWN_REFERENCE:
12420 name = name_expr->unknown_expression()->name();
12421 break;
12422
12423 case EXPRESSION_CONST_REFERENCE:
12424 no = static_cast<Const_expression*>(name_expr)->named_object();
12425 break;
12426
12427 case EXPRESSION_TYPE:
12428 {
12429 Type* t = name_expr->type();
12430 Named_type* nt = t->named_type();
12431 if (nt == NULL)
12432 bad_key = true;
12433 else
12434 no = nt->named_object();
12435 }
12436 break;
12437
12438 case EXPRESSION_VAR_REFERENCE:
12439 no = name_expr->var_expression()->named_object();
12440 break;
12441
12442 case EXPRESSION_FUNC_REFERENCE:
12443 no = name_expr->func_expression()->named_object();
12444 break;
12445
12446 case EXPRESSION_UNARY:
12447 // If there is a local variable around with the same name as
12448 // the field, and this occurs in the closure, then the
12449 // parser may turn the field reference into an indirection
12450 // through the closure. FIXME: This is a mess.
12451 {
12452 bad_key = true;
12453 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
12454 if (ue->op() == OPERATOR_MULT)
12455 {
12456 Field_reference_expression* fre =
12457 ue->operand()->field_reference_expression();
12458 if (fre != NULL)
12459 {
12460 Struct_type* st =
12461 fre->expr()->type()->deref()->struct_type();
12462 if (st != NULL)
12463 {
12464 const Struct_field* sf = st->field(fre->field_index());
12465 name = sf->field_name();
12466
12467 // See below. FIXME.
12468 if (!Gogo::is_hidden_name(name)
12469 && name[0] >= 'a'
12470 && name[0] <= 'z')
12471 {
12472 if (gogo->lookup_global(name.c_str()) != NULL)
12473 name = gogo->pack_hidden_name(name, false);
12474 }
12475
12476 char buf[20];
12477 snprintf(buf, sizeof buf, "%u", fre->field_index());
12478 size_t buflen = strlen(buf);
12479 if (name.compare(name.length() - buflen, buflen, buf)
12480 == 0)
12481 {
12482 name = name.substr(0, name.length() - buflen);
12483 bad_key = false;
12484 }
12485 }
12486 }
12487 }
12488 }
12489 break;
12490
12491 default:
12492 bad_key = true;
12493 break;
12494 }
12495 if (bad_key)
12496 {
12497 error_at(name_expr->location(), "expected struct field name");
12498 return Expression::make_error(location);
12499 }
12500
12501 if (no != NULL)
12502 {
12503 name = no->name();
12504
12505 // A predefined name won't be packed. If it starts with a
12506 // lower case letter we need to check for that case, because
12507 // the field name will be packed. FIXME.
12508 if (!Gogo::is_hidden_name(name)
12509 && name[0] >= 'a'
12510 && name[0] <= 'z')
12511 {
12512 Named_object* gno = gogo->lookup_global(name.c_str());
12513 if (gno == no)
12514 name = gogo->pack_hidden_name(name, false);
12515 }
12516 }
12517
12518 unsigned int index;
12519 const Struct_field* sf = st->find_local_field(name, &index);
12520 if (sf == NULL)
12521 {
12522 error_at(name_expr->location(), "unknown field %qs in %qs",
12523 Gogo::message_name(name).c_str(),
12524 (type->named_type() != NULL
12525 ? type->named_type()->message_name().c_str()
12526 : "unnamed struct"));
12527 return Expression::make_error(location);
12528 }
12529 if (vals[index] != NULL)
12530 {
12531 error_at(name_expr->location(),
12532 "duplicate value for field %qs in %qs",
12533 Gogo::message_name(name).c_str(),
12534 (type->named_type() != NULL
12535 ? type->named_type()->message_name().c_str()
12536 : "unnamed struct"));
12537 return Expression::make_error(location);
12538 }
12539
12540 if (type->named_type() != NULL
12541 && type->named_type()->named_object()->package() != NULL
12542 && Gogo::is_hidden_name(sf->field_name()))
12543 error_at(name_expr->location(),
12544 "assignment of unexported field %qs in %qs literal",
12545 Gogo::message_name(sf->field_name()).c_str(),
12546 type->named_type()->message_name().c_str());
12547
12548 vals[index] = val;
12549 traverse_order->push_back(index);
12550 }
12551
12552 Expression_list* list = new Expression_list;
12553 list->reserve(field_count);
12554 for (size_t i = 0; i < field_count; ++i)
12555 list->push_back(vals[i]);
12556
12557 Struct_construction_expression* ret =
12558 new Struct_construction_expression(type, list, location);
12559 ret->set_traverse_order(traverse_order);
12560 return ret;
12561 }
12562
12563 // Used to sort an index/value array.
12564
12565 class Index_value_compare
12566 {
12567 public:
12568 bool
12569 operator()(const std::pair<unsigned long, Expression*>& a,
12570 const std::pair<unsigned long, Expression*>& b)
12571 { return a.first < b.first; }
12572 };
12573
12574 // Lower an array composite literal.
12575
12576 Expression*
12577 Composite_literal_expression::lower_array(Type* type)
12578 {
12579 Location location = this->location();
12580 if (this->vals_ == NULL || !this->has_keys_)
12581 return this->make_array(type, NULL, this->vals_);
12582
12583 std::vector<unsigned long>* indexes = new std::vector<unsigned long>;
12584 indexes->reserve(this->vals_->size());
12585 bool indexes_out_of_order = false;
12586 Expression_list* vals = new Expression_list();
12587 vals->reserve(this->vals_->size());
12588 unsigned long index = 0;
12589 Expression_list::const_iterator p = this->vals_->begin();
12590 while (p != this->vals_->end())
12591 {
12592 Expression* index_expr = *p;
12593
12594 ++p;
12595 go_assert(p != this->vals_->end());
12596 Expression* val = *p;
12597
12598 ++p;
12599
12600 if (index_expr == NULL)
12601 {
12602 if (!indexes->empty())
12603 indexes->push_back(index);
12604 }
12605 else
12606 {
12607 if (indexes->empty() && !vals->empty())
12608 {
12609 for (size_t i = 0; i < vals->size(); ++i)
12610 indexes->push_back(i);
12611 }
12612
12613 Numeric_constant nc;
12614 if (!index_expr->numeric_constant_value(&nc))
12615 {
12616 error_at(index_expr->location(),
12617 "index expression is not integer constant");
12618 return Expression::make_error(location);
12619 }
12620
12621 switch (nc.to_unsigned_long(&index))
12622 {
12623 case Numeric_constant::NC_UL_VALID:
12624 break;
12625 case Numeric_constant::NC_UL_NOTINT:
12626 error_at(index_expr->location(),
12627 "index expression is not integer constant");
12628 return Expression::make_error(location);
12629 case Numeric_constant::NC_UL_NEGATIVE:
12630 error_at(index_expr->location(), "index expression is negative");
12631 return Expression::make_error(location);
12632 case Numeric_constant::NC_UL_BIG:
12633 error_at(index_expr->location(), "index value overflow");
12634 return Expression::make_error(location);
12635 default:
12636 go_unreachable();
12637 }
12638
12639 Named_type* ntype = Type::lookup_integer_type("int");
12640 Integer_type* inttype = ntype->integer_type();
12641 if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8)
12642 && index >> (inttype->bits() - 1) != 0)
12643 {
12644 error_at(index_expr->location(), "index value overflow");
12645 return Expression::make_error(location);
12646 }
12647
12648 if (std::find(indexes->begin(), indexes->end(), index)
12649 != indexes->end())
12650 {
12651 error_at(index_expr->location(), "duplicate value for index %lu",
12652 index);
12653 return Expression::make_error(location);
12654 }
12655
12656 if (!indexes->empty() && index < indexes->back())
12657 indexes_out_of_order = true;
12658
12659 indexes->push_back(index);
12660 }
12661
12662 vals->push_back(val);
12663
12664 ++index;
12665 }
12666
12667 if (indexes->empty())
12668 {
12669 delete indexes;
12670 indexes = NULL;
12671 }
12672
12673 if (indexes_out_of_order)
12674 {
12675 typedef std::vector<std::pair<unsigned long, Expression*> > V;
12676
12677 V v;
12678 v.reserve(indexes->size());
12679 std::vector<unsigned long>::const_iterator pi = indexes->begin();
12680 for (Expression_list::const_iterator pe = vals->begin();
12681 pe != vals->end();
12682 ++pe, ++pi)
12683 v.push_back(std::make_pair(*pi, *pe));
12684
12685 std::sort(v.begin(), v.end(), Index_value_compare());
12686
12687 delete indexes;
12688 delete vals;
12689 indexes = new std::vector<unsigned long>();
12690 indexes->reserve(v.size());
12691 vals = new Expression_list();
12692 vals->reserve(v.size());
12693
12694 for (V::const_iterator p = v.begin(); p != v.end(); ++p)
12695 {
12696 indexes->push_back(p->first);
12697 vals->push_back(p->second);
12698 }
12699 }
12700
12701 return this->make_array(type, indexes, vals);
12702 }
12703
12704 // Actually build the array composite literal. This handles
12705 // [...]{...}.
12706
12707 Expression*
12708 Composite_literal_expression::make_array(
12709 Type* type,
12710 const std::vector<unsigned long>* indexes,
12711 Expression_list* vals)
12712 {
12713 Location location = this->location();
12714 Array_type* at = type->array_type();
12715
12716 if (at->length() != NULL && at->length()->is_nil_expression())
12717 {
12718 size_t size;
12719 if (vals == NULL)
12720 size = 0;
12721 else if (indexes != NULL)
12722 size = indexes->back() + 1;
12723 else
12724 {
12725 size = vals->size();
12726 Integer_type* it = Type::lookup_integer_type("int")->integer_type();
12727 if (sizeof(size) <= static_cast<size_t>(it->bits() * 8)
12728 && size >> (it->bits() - 1) != 0)
12729 {
12730 error_at(location, "too many elements in composite literal");
12731 return Expression::make_error(location);
12732 }
12733 }
12734
12735 mpz_t vlen;
12736 mpz_init_set_ui(vlen, size);
12737 Expression* elen = Expression::make_integer(&vlen, NULL, location);
12738 mpz_clear(vlen);
12739 at = Type::make_array_type(at->element_type(), elen);
12740 type = at;
12741 }
12742 else if (at->length() != NULL
12743 && !at->length()->is_error_expression()
12744 && this->vals_ != NULL)
12745 {
12746 Numeric_constant nc;
12747 unsigned long val;
12748 if (at->length()->numeric_constant_value(&nc)
12749 && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID)
12750 {
12751 if (indexes == NULL)
12752 {
12753 if (this->vals_->size() > val)
12754 {
12755 error_at(location, "too many elements in composite literal");
12756 return Expression::make_error(location);
12757 }
12758 }
12759 else
12760 {
12761 unsigned long max = indexes->back();
12762 if (max >= val)
12763 {
12764 error_at(location,
12765 ("some element keys in composite literal "
12766 "are out of range"));
12767 return Expression::make_error(location);
12768 }
12769 }
12770 }
12771 }
12772
12773 if (at->length() != NULL)
12774 return new Fixed_array_construction_expression(type, indexes, vals,
12775 location);
12776 else
12777 return new Open_array_construction_expression(type, indexes, vals,
12778 location);
12779 }
12780
12781 // Lower a map composite literal.
12782
12783 Expression*
12784 Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
12785 Statement_inserter* inserter,
12786 Type* type)
12787 {
12788 Location location = this->location();
12789 if (this->vals_ != NULL)
12790 {
12791 if (!this->has_keys_)
12792 {
12793 error_at(location, "map composite literal must have keys");
12794 return Expression::make_error(location);
12795 }
12796
12797 for (Expression_list::iterator p = this->vals_->begin();
12798 p != this->vals_->end();
12799 p += 2)
12800 {
12801 if (*p == NULL)
12802 {
12803 ++p;
12804 error_at((*p)->location(),
12805 "map composite literal must have keys for every value");
12806 return Expression::make_error(location);
12807 }
12808 // Make sure we have lowered the key; it may not have been
12809 // lowered in order to handle keys for struct composite
12810 // literals. Lower it now to get the right error message.
12811 if ((*p)->unknown_expression() != NULL)
12812 {
12813 (*p)->unknown_expression()->clear_is_composite_literal_key();
12814 gogo->lower_expression(function, inserter, &*p);
12815 go_assert((*p)->is_error_expression());
12816 return Expression::make_error(location);
12817 }
12818 }
12819 }
12820
12821 return new Map_construction_expression(type, this->vals_, location);
12822 }
12823
12824 // Dump ast representation for a composite literal expression.
12825
12826 void
12827 Composite_literal_expression::do_dump_expression(
12828 Ast_dump_context* ast_dump_context) const
12829 {
12830 ast_dump_context->ostream() << "composite(";
12831 ast_dump_context->dump_type(this->type_);
12832 ast_dump_context->ostream() << ", {";
12833 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
12834 ast_dump_context->ostream() << "})";
12835 }
12836
12837 // Make a composite literal expression.
12838
12839 Expression*
12840 Expression::make_composite_literal(Type* type, int depth, bool has_keys,
12841 Expression_list* vals,
12842 Location location)
12843 {
12844 return new Composite_literal_expression(type, depth, has_keys, vals,
12845 location);
12846 }
12847
12848 // Return whether this expression is a composite literal.
12849
12850 bool
12851 Expression::is_composite_literal() const
12852 {
12853 switch (this->classification_)
12854 {
12855 case EXPRESSION_COMPOSITE_LITERAL:
12856 case EXPRESSION_STRUCT_CONSTRUCTION:
12857 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12858 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12859 case EXPRESSION_MAP_CONSTRUCTION:
12860 return true;
12861 default:
12862 return false;
12863 }
12864 }
12865
12866 // Return whether this expression is a composite literal which is not
12867 // constant.
12868
12869 bool
12870 Expression::is_nonconstant_composite_literal() const
12871 {
12872 switch (this->classification_)
12873 {
12874 case EXPRESSION_STRUCT_CONSTRUCTION:
12875 {
12876 const Struct_construction_expression *psce =
12877 static_cast<const Struct_construction_expression*>(this);
12878 return !psce->is_constant_struct();
12879 }
12880 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
12881 {
12882 const Fixed_array_construction_expression *pace =
12883 static_cast<const Fixed_array_construction_expression*>(this);
12884 return !pace->is_constant_array();
12885 }
12886 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
12887 {
12888 const Open_array_construction_expression *pace =
12889 static_cast<const Open_array_construction_expression*>(this);
12890 return !pace->is_constant_array();
12891 }
12892 case EXPRESSION_MAP_CONSTRUCTION:
12893 return true;
12894 default:
12895 return false;
12896 }
12897 }
12898
12899 // Return true if this is a reference to a local variable.
12900
12901 bool
12902 Expression::is_local_variable() const
12903 {
12904 const Var_expression* ve = this->var_expression();
12905 if (ve == NULL)
12906 return false;
12907 const Named_object* no = ve->named_object();
12908 return (no->is_result_variable()
12909 || (no->is_variable() && !no->var_value()->is_global()));
12910 }
12911
12912 // Class Type_guard_expression.
12913
12914 // Traversal.
12915
12916 int
12917 Type_guard_expression::do_traverse(Traverse* traverse)
12918 {
12919 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
12920 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12921 return TRAVERSE_EXIT;
12922 return TRAVERSE_CONTINUE;
12923 }
12924
12925 // Check types of a type guard expression. The expression must have
12926 // an interface type, but the actual type conversion is checked at run
12927 // time.
12928
12929 void
12930 Type_guard_expression::do_check_types(Gogo*)
12931 {
12932 Type* expr_type = this->expr_->type();
12933 if (expr_type->interface_type() == NULL)
12934 {
12935 if (!expr_type->is_error() && !this->type_->is_error())
12936 this->report_error(_("type assertion only valid for interface types"));
12937 this->set_is_error();
12938 }
12939 else if (this->type_->interface_type() == NULL)
12940 {
12941 std::string reason;
12942 if (!expr_type->interface_type()->implements_interface(this->type_,
12943 &reason))
12944 {
12945 if (!this->type_->is_error())
12946 {
12947 if (reason.empty())
12948 this->report_error(_("impossible type assertion: "
12949 "type does not implement interface"));
12950 else
12951 error_at(this->location(),
12952 ("impossible type assertion: "
12953 "type does not implement interface (%s)"),
12954 reason.c_str());
12955 }
12956 this->set_is_error();
12957 }
12958 }
12959 }
12960
12961 // Return a tree for a type guard expression.
12962
12963 tree
12964 Type_guard_expression::do_get_tree(Translate_context* context)
12965 {
12966 tree expr_tree = this->expr_->get_tree(context);
12967 if (expr_tree == error_mark_node)
12968 return error_mark_node;
12969 if (this->type_->interface_type() != NULL)
12970 return Expression::convert_interface_to_interface(context, this->type_,
12971 this->expr_->type(),
12972 expr_tree, true,
12973 this->location());
12974 else
12975 return Expression::convert_for_assignment(context, this->type_,
12976 this->expr_->type(), expr_tree,
12977 this->location());
12978 }
12979
12980 // Dump ast representation for a type guard expression.
12981
12982 void
12983 Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
12984 const
12985 {
12986 this->expr_->dump_expression(ast_dump_context);
12987 ast_dump_context->ostream() << ".";
12988 ast_dump_context->dump_type(this->type_);
12989 }
12990
12991 // Make a type guard expression.
12992
12993 Expression*
12994 Expression::make_type_guard(Expression* expr, Type* type,
12995 Location location)
12996 {
12997 return new Type_guard_expression(expr, type, location);
12998 }
12999
13000 // Class Heap_composite_expression.
13001
13002 // When you take the address of a composite literal, it is allocated
13003 // on the heap. This class implements that.
13004
13005 class Heap_composite_expression : public Expression
13006 {
13007 public:
13008 Heap_composite_expression(Expression* expr, Location location)
13009 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
13010 expr_(expr)
13011 { }
13012
13013 protected:
13014 int
13015 do_traverse(Traverse* traverse)
13016 { return Expression::traverse(&this->expr_, traverse); }
13017
13018 Type*
13019 do_type()
13020 { return Type::make_pointer_type(this->expr_->type()); }
13021
13022 void
13023 do_determine_type(const Type_context*)
13024 { this->expr_->determine_type_no_context(); }
13025
13026 Expression*
13027 do_copy()
13028 {
13029 return Expression::make_heap_composite(this->expr_->copy(),
13030 this->location());
13031 }
13032
13033 tree
13034 do_get_tree(Translate_context*);
13035
13036 // We only export global objects, and the parser does not generate
13037 // this in global scope.
13038 void
13039 do_export(Export*) const
13040 { go_unreachable(); }
13041
13042 void
13043 do_dump_expression(Ast_dump_context*) const;
13044
13045 private:
13046 // The composite literal which is being put on the heap.
13047 Expression* expr_;
13048 };
13049
13050 // Return a tree which allocates a composite literal on the heap.
13051
13052 tree
13053 Heap_composite_expression::do_get_tree(Translate_context* context)
13054 {
13055 tree expr_tree = this->expr_->get_tree(context);
13056 if (expr_tree == error_mark_node || TREE_TYPE(expr_tree) == error_mark_node)
13057 return error_mark_node;
13058 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
13059 go_assert(TREE_CODE(expr_size) == INTEGER_CST);
13060 tree space = context->gogo()->allocate_memory(this->expr_->type(),
13061 expr_size, this->location());
13062 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
13063 space = save_expr(space);
13064 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
13065 space);
13066 TREE_THIS_NOTRAP(ref) = 1;
13067 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
13068 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
13069 space);
13070 SET_EXPR_LOCATION(ret, this->location().gcc_location());
13071 return ret;
13072 }
13073
13074 // Dump ast representation for a heap composite expression.
13075
13076 void
13077 Heap_composite_expression::do_dump_expression(
13078 Ast_dump_context* ast_dump_context) const
13079 {
13080 ast_dump_context->ostream() << "&(";
13081 ast_dump_context->dump_expression(this->expr_);
13082 ast_dump_context->ostream() << ")";
13083 }
13084
13085 // Allocate a composite literal on the heap.
13086
13087 Expression*
13088 Expression::make_heap_composite(Expression* expr, Location location)
13089 {
13090 return new Heap_composite_expression(expr, location);
13091 }
13092
13093 // Class Receive_expression.
13094
13095 // Return the type of a receive expression.
13096
13097 Type*
13098 Receive_expression::do_type()
13099 {
13100 Channel_type* channel_type = this->channel_->type()->channel_type();
13101 if (channel_type == NULL)
13102 return Type::make_error_type();
13103 return channel_type->element_type();
13104 }
13105
13106 // Check types for a receive expression.
13107
13108 void
13109 Receive_expression::do_check_types(Gogo*)
13110 {
13111 Type* type = this->channel_->type();
13112 if (type->is_error())
13113 {
13114 this->set_is_error();
13115 return;
13116 }
13117 if (type->channel_type() == NULL)
13118 {
13119 this->report_error(_("expected channel"));
13120 return;
13121 }
13122 if (!type->channel_type()->may_receive())
13123 {
13124 this->report_error(_("invalid receive on send-only channel"));
13125 return;
13126 }
13127 }
13128
13129 // Get a tree for a receive expression.
13130
13131 tree
13132 Receive_expression::do_get_tree(Translate_context* context)
13133 {
13134 Location loc = this->location();
13135
13136 Channel_type* channel_type = this->channel_->type()->channel_type();
13137 if (channel_type == NULL)
13138 {
13139 go_assert(this->channel_->type()->is_error());
13140 return error_mark_node;
13141 }
13142
13143 Expression* td = Expression::make_type_descriptor(channel_type, loc);
13144 tree td_tree = td->get_tree(context);
13145
13146 Type* element_type = channel_type->element_type();
13147 Btype* element_type_btype = element_type->get_backend(context->gogo());
13148 tree element_type_tree = type_to_tree(element_type_btype);
13149
13150 tree channel = this->channel_->get_tree(context);
13151 if (element_type_tree == error_mark_node || channel == error_mark_node)
13152 return error_mark_node;
13153
13154 return Gogo::receive_from_channel(element_type_tree, td_tree, channel, loc);
13155 }
13156
13157 // Dump ast representation for a receive expression.
13158
13159 void
13160 Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
13161 {
13162 ast_dump_context->ostream() << " <- " ;
13163 ast_dump_context->dump_expression(channel_);
13164 }
13165
13166 // Make a receive expression.
13167
13168 Receive_expression*
13169 Expression::make_receive(Expression* channel, Location location)
13170 {
13171 return new Receive_expression(channel, location);
13172 }
13173
13174 // An expression which evaluates to a pointer to the type descriptor
13175 // of a type.
13176
13177 class Type_descriptor_expression : public Expression
13178 {
13179 public:
13180 Type_descriptor_expression(Type* type, Location location)
13181 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
13182 type_(type)
13183 { }
13184
13185 protected:
13186 Type*
13187 do_type()
13188 { return Type::make_type_descriptor_ptr_type(); }
13189
13190 void
13191 do_determine_type(const Type_context*)
13192 { }
13193
13194 Expression*
13195 do_copy()
13196 { return this; }
13197
13198 tree
13199 do_get_tree(Translate_context* context)
13200 {
13201 return this->type_->type_descriptor_pointer(context->gogo(),
13202 this->location());
13203 }
13204
13205 void
13206 do_dump_expression(Ast_dump_context*) const;
13207
13208 private:
13209 // The type for which this is the descriptor.
13210 Type* type_;
13211 };
13212
13213 // Dump ast representation for a type descriptor expression.
13214
13215 void
13216 Type_descriptor_expression::do_dump_expression(
13217 Ast_dump_context* ast_dump_context) const
13218 {
13219 ast_dump_context->dump_type(this->type_);
13220 }
13221
13222 // Make a type descriptor expression.
13223
13224 Expression*
13225 Expression::make_type_descriptor(Type* type, Location location)
13226 {
13227 return new Type_descriptor_expression(type, location);
13228 }
13229
13230 // An expression which evaluates to some characteristic of a type.
13231 // This is only used to initialize fields of a type descriptor. Using
13232 // a new expression class is slightly inefficient but gives us a good
13233 // separation between the frontend and the middle-end with regard to
13234 // how types are laid out.
13235
13236 class Type_info_expression : public Expression
13237 {
13238 public:
13239 Type_info_expression(Type* type, Type_info type_info)
13240 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()),
13241 type_(type), type_info_(type_info)
13242 { }
13243
13244 protected:
13245 Type*
13246 do_type();
13247
13248 void
13249 do_determine_type(const Type_context*)
13250 { }
13251
13252 Expression*
13253 do_copy()
13254 { return this; }
13255
13256 tree
13257 do_get_tree(Translate_context* context);
13258
13259 void
13260 do_dump_expression(Ast_dump_context*) const;
13261
13262 private:
13263 // The type for which we are getting information.
13264 Type* type_;
13265 // What information we want.
13266 Type_info type_info_;
13267 };
13268
13269 // The type is chosen to match what the type descriptor struct
13270 // expects.
13271
13272 Type*
13273 Type_info_expression::do_type()
13274 {
13275 switch (this->type_info_)
13276 {
13277 case TYPE_INFO_SIZE:
13278 return Type::lookup_integer_type("uintptr");
13279 case TYPE_INFO_ALIGNMENT:
13280 case TYPE_INFO_FIELD_ALIGNMENT:
13281 return Type::lookup_integer_type("uint8");
13282 default:
13283 go_unreachable();
13284 }
13285 }
13286
13287 // Return type information in GENERIC.
13288
13289 tree
13290 Type_info_expression::do_get_tree(Translate_context* context)
13291 {
13292 Btype* btype = this->type_->get_backend(context->gogo());
13293 Gogo* gogo = context->gogo();
13294 size_t val;
13295 switch (this->type_info_)
13296 {
13297 case TYPE_INFO_SIZE:
13298 val = gogo->backend()->type_size(btype);
13299 break;
13300 case TYPE_INFO_ALIGNMENT:
13301 val = gogo->backend()->type_alignment(btype);
13302 break;
13303 case TYPE_INFO_FIELD_ALIGNMENT:
13304 val = gogo->backend()->type_field_alignment(btype);
13305 break;
13306 default:
13307 go_unreachable();
13308 }
13309 tree val_type_tree = type_to_tree(this->type()->get_backend(gogo));
13310 go_assert(val_type_tree != error_mark_node);
13311 return build_int_cstu(val_type_tree, val);
13312 }
13313
13314 // Dump ast representation for a type info expression.
13315
13316 void
13317 Type_info_expression::do_dump_expression(
13318 Ast_dump_context* ast_dump_context) const
13319 {
13320 ast_dump_context->ostream() << "typeinfo(";
13321 ast_dump_context->dump_type(this->type_);
13322 ast_dump_context->ostream() << ",";
13323 ast_dump_context->ostream() <<
13324 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
13325 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
13326 : this->type_info_ == TYPE_INFO_SIZE ? "size "
13327 : "unknown");
13328 ast_dump_context->ostream() << ")";
13329 }
13330
13331 // Make a type info expression.
13332
13333 Expression*
13334 Expression::make_type_info(Type* type, Type_info type_info)
13335 {
13336 return new Type_info_expression(type, type_info);
13337 }
13338
13339 // An expression which evaluates to the offset of a field within a
13340 // struct. This, like Type_info_expression, q.v., is only used to
13341 // initialize fields of a type descriptor.
13342
13343 class Struct_field_offset_expression : public Expression
13344 {
13345 public:
13346 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
13347 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET,
13348 Linemap::predeclared_location()),
13349 type_(type), field_(field)
13350 { }
13351
13352 protected:
13353 Type*
13354 do_type()
13355 { return Type::lookup_integer_type("uintptr"); }
13356
13357 void
13358 do_determine_type(const Type_context*)
13359 { }
13360
13361 Expression*
13362 do_copy()
13363 { return this; }
13364
13365 tree
13366 do_get_tree(Translate_context* context);
13367
13368 void
13369 do_dump_expression(Ast_dump_context*) const;
13370
13371 private:
13372 // The type of the struct.
13373 Struct_type* type_;
13374 // The field.
13375 const Struct_field* field_;
13376 };
13377
13378 // Return a struct field offset in GENERIC.
13379
13380 tree
13381 Struct_field_offset_expression::do_get_tree(Translate_context* context)
13382 {
13383 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
13384 if (type_tree == error_mark_node)
13385 return error_mark_node;
13386
13387 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
13388 go_assert(val_type_tree != error_mark_node);
13389
13390 const Struct_field_list* fields = this->type_->fields();
13391 tree struct_field_tree = TYPE_FIELDS(type_tree);
13392 Struct_field_list::const_iterator p;
13393 for (p = fields->begin();
13394 p != fields->end();
13395 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
13396 {
13397 go_assert(struct_field_tree != NULL_TREE);
13398 if (&*p == this->field_)
13399 break;
13400 }
13401 go_assert(&*p == this->field_);
13402
13403 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
13404 byte_position(struct_field_tree));
13405 }
13406
13407 // Dump ast representation for a struct field offset expression.
13408
13409 void
13410 Struct_field_offset_expression::do_dump_expression(
13411 Ast_dump_context* ast_dump_context) const
13412 {
13413 ast_dump_context->ostream() << "unsafe.Offsetof(";
13414 ast_dump_context->dump_type(this->type_);
13415 ast_dump_context->ostream() << '.';
13416 ast_dump_context->ostream() <<
13417 Gogo::message_name(this->field_->field_name());
13418 ast_dump_context->ostream() << ")";
13419 }
13420
13421 // Make an expression for a struct field offset.
13422
13423 Expression*
13424 Expression::make_struct_field_offset(Struct_type* type,
13425 const Struct_field* field)
13426 {
13427 return new Struct_field_offset_expression(type, field);
13428 }
13429
13430 // An expression which evaluates to a pointer to the map descriptor of
13431 // a map type.
13432
13433 class Map_descriptor_expression : public Expression
13434 {
13435 public:
13436 Map_descriptor_expression(Map_type* type, Location location)
13437 : Expression(EXPRESSION_MAP_DESCRIPTOR, location),
13438 type_(type)
13439 { }
13440
13441 protected:
13442 Type*
13443 do_type()
13444 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
13445
13446 void
13447 do_determine_type(const Type_context*)
13448 { }
13449
13450 Expression*
13451 do_copy()
13452 { return this; }
13453
13454 tree
13455 do_get_tree(Translate_context* context)
13456 {
13457 return this->type_->map_descriptor_pointer(context->gogo(),
13458 this->location());
13459 }
13460
13461 void
13462 do_dump_expression(Ast_dump_context*) const;
13463
13464 private:
13465 // The type for which this is the descriptor.
13466 Map_type* type_;
13467 };
13468
13469 // Dump ast representation for a map descriptor expression.
13470
13471 void
13472 Map_descriptor_expression::do_dump_expression(
13473 Ast_dump_context* ast_dump_context) const
13474 {
13475 ast_dump_context->ostream() << "map_descriptor(";
13476 ast_dump_context->dump_type(this->type_);
13477 ast_dump_context->ostream() << ")";
13478 }
13479
13480 // Make a map descriptor expression.
13481
13482 Expression*
13483 Expression::make_map_descriptor(Map_type* type, Location location)
13484 {
13485 return new Map_descriptor_expression(type, location);
13486 }
13487
13488 // An expression which evaluates to the address of an unnamed label.
13489
13490 class Label_addr_expression : public Expression
13491 {
13492 public:
13493 Label_addr_expression(Label* label, Location location)
13494 : Expression(EXPRESSION_LABEL_ADDR, location),
13495 label_(label)
13496 { }
13497
13498 protected:
13499 Type*
13500 do_type()
13501 { return Type::make_pointer_type(Type::make_void_type()); }
13502
13503 void
13504 do_determine_type(const Type_context*)
13505 { }
13506
13507 Expression*
13508 do_copy()
13509 { return new Label_addr_expression(this->label_, this->location()); }
13510
13511 tree
13512 do_get_tree(Translate_context* context)
13513 {
13514 return expr_to_tree(this->label_->get_addr(context, this->location()));
13515 }
13516
13517 void
13518 do_dump_expression(Ast_dump_context* ast_dump_context) const
13519 { ast_dump_context->ostream() << this->label_->name(); }
13520
13521 private:
13522 // The label whose address we are taking.
13523 Label* label_;
13524 };
13525
13526 // Make an expression for the address of an unnamed label.
13527
13528 Expression*
13529 Expression::make_label_addr(Label* label, Location location)
13530 {
13531 return new Label_addr_expression(label, location);
13532 }
13533
13534 // Import an expression. This comes at the end in order to see the
13535 // various class definitions.
13536
13537 Expression*
13538 Expression::import_expression(Import* imp)
13539 {
13540 int c = imp->peek_char();
13541 if (imp->match_c_string("- ")
13542 || imp->match_c_string("! ")
13543 || imp->match_c_string("^ "))
13544 return Unary_expression::do_import(imp);
13545 else if (c == '(')
13546 return Binary_expression::do_import(imp);
13547 else if (imp->match_c_string("true")
13548 || imp->match_c_string("false"))
13549 return Boolean_expression::do_import(imp);
13550 else if (c == '"')
13551 return String_expression::do_import(imp);
13552 else if (c == '-' || (c >= '0' && c <= '9'))
13553 {
13554 // This handles integers, floats and complex constants.
13555 return Integer_expression::do_import(imp);
13556 }
13557 else if (imp->match_c_string("nil"))
13558 return Nil_expression::do_import(imp);
13559 else if (imp->match_c_string("convert"))
13560 return Type_conversion_expression::do_import(imp);
13561 else
13562 {
13563 error_at(imp->location(), "import error: expected expression");
13564 return Expression::make_error(imp->location());
13565 }
13566 }
13567
13568 // Class Expression_list.
13569
13570 // Traverse the list.
13571
13572 int
13573 Expression_list::traverse(Traverse* traverse)
13574 {
13575 for (Expression_list::iterator p = this->begin();
13576 p != this->end();
13577 ++p)
13578 {
13579 if (*p != NULL)
13580 {
13581 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
13582 return TRAVERSE_EXIT;
13583 }
13584 }
13585 return TRAVERSE_CONTINUE;
13586 }
13587
13588 // Copy the list.
13589
13590 Expression_list*
13591 Expression_list::copy()
13592 {
13593 Expression_list* ret = new Expression_list();
13594 for (Expression_list::iterator p = this->begin();
13595 p != this->end();
13596 ++p)
13597 {
13598 if (*p == NULL)
13599 ret->push_back(NULL);
13600 else
13601 ret->push_back((*p)->copy());
13602 }
13603 return ret;
13604 }
13605
13606 // Return whether an expression list has an error expression.
13607
13608 bool
13609 Expression_list::contains_error() const
13610 {
13611 for (Expression_list::const_iterator p = this->begin();
13612 p != this->end();
13613 ++p)
13614 if (*p != NULL && (*p)->is_error_expression())
13615 return true;
13616 return false;
13617 }
13618
13619 // Class Numeric_constant.
13620
13621 // Destructor.
13622
13623 Numeric_constant::~Numeric_constant()
13624 {
13625 this->clear();
13626 }
13627
13628 // Copy constructor.
13629
13630 Numeric_constant::Numeric_constant(const Numeric_constant& a)
13631 : classification_(a.classification_), type_(a.type_)
13632 {
13633 switch (a.classification_)
13634 {
13635 case NC_INVALID:
13636 break;
13637 case NC_INT:
13638 case NC_RUNE:
13639 mpz_init_set(this->u_.int_val, a.u_.int_val);
13640 break;
13641 case NC_FLOAT:
13642 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
13643 break;
13644 case NC_COMPLEX:
13645 mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real,
13646 GMP_RNDN);
13647 mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag,
13648 GMP_RNDN);
13649 break;
13650 default:
13651 go_unreachable();
13652 }
13653 }
13654
13655 // Assignment operator.
13656
13657 Numeric_constant&
13658 Numeric_constant::operator=(const Numeric_constant& a)
13659 {
13660 this->clear();
13661 this->classification_ = a.classification_;
13662 this->type_ = a.type_;
13663 switch (a.classification_)
13664 {
13665 case NC_INVALID:
13666 break;
13667 case NC_INT:
13668 case NC_RUNE:
13669 mpz_init_set(this->u_.int_val, a.u_.int_val);
13670 break;
13671 case NC_FLOAT:
13672 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
13673 break;
13674 case NC_COMPLEX:
13675 mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real,
13676 GMP_RNDN);
13677 mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag,
13678 GMP_RNDN);
13679 break;
13680 default:
13681 go_unreachable();
13682 }
13683 return *this;
13684 }
13685
13686 // Clear the contents.
13687
13688 void
13689 Numeric_constant::clear()
13690 {
13691 switch (this->classification_)
13692 {
13693 case NC_INVALID:
13694 break;
13695 case NC_INT:
13696 case NC_RUNE:
13697 mpz_clear(this->u_.int_val);
13698 break;
13699 case NC_FLOAT:
13700 mpfr_clear(this->u_.float_val);
13701 break;
13702 case NC_COMPLEX:
13703 mpfr_clear(this->u_.complex_val.real);
13704 mpfr_clear(this->u_.complex_val.imag);
13705 break;
13706 default:
13707 go_unreachable();
13708 }
13709 this->classification_ = NC_INVALID;
13710 }
13711
13712 // Set to an unsigned long value.
13713
13714 void
13715 Numeric_constant::set_unsigned_long(Type* type, unsigned long val)
13716 {
13717 this->clear();
13718 this->classification_ = NC_INT;
13719 this->type_ = type;
13720 mpz_init_set_ui(this->u_.int_val, val);
13721 }
13722
13723 // Set to an integer value.
13724
13725 void
13726 Numeric_constant::set_int(Type* type, const mpz_t val)
13727 {
13728 this->clear();
13729 this->classification_ = NC_INT;
13730 this->type_ = type;
13731 mpz_init_set(this->u_.int_val, val);
13732 }
13733
13734 // Set to a rune value.
13735
13736 void
13737 Numeric_constant::set_rune(Type* type, const mpz_t val)
13738 {
13739 this->clear();
13740 this->classification_ = NC_RUNE;
13741 this->type_ = type;
13742 mpz_init_set(this->u_.int_val, val);
13743 }
13744
13745 // Set to a floating point value.
13746
13747 void
13748 Numeric_constant::set_float(Type* type, const mpfr_t val)
13749 {
13750 this->clear();
13751 this->classification_ = NC_FLOAT;
13752 this->type_ = type;
13753 // Numeric constants do not have negative zero values, so remove
13754 // them here. They also don't have infinity or NaN values, but we
13755 // should never see them here.
13756 if (mpfr_zero_p(val))
13757 mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN);
13758 else
13759 mpfr_init_set(this->u_.float_val, val, GMP_RNDN);
13760 }
13761
13762 // Set to a complex value.
13763
13764 void
13765 Numeric_constant::set_complex(Type* type, const mpfr_t real, const mpfr_t imag)
13766 {
13767 this->clear();
13768 this->classification_ = NC_COMPLEX;
13769 this->type_ = type;
13770 mpfr_init_set(this->u_.complex_val.real, real, GMP_RNDN);
13771 mpfr_init_set(this->u_.complex_val.imag, imag, GMP_RNDN);
13772 }
13773
13774 // Get an int value.
13775
13776 void
13777 Numeric_constant::get_int(mpz_t* val) const
13778 {
13779 go_assert(this->is_int());
13780 mpz_init_set(*val, this->u_.int_val);
13781 }
13782
13783 // Get a rune value.
13784
13785 void
13786 Numeric_constant::get_rune(mpz_t* val) const
13787 {
13788 go_assert(this->is_rune());
13789 mpz_init_set(*val, this->u_.int_val);
13790 }
13791
13792 // Get a floating point value.
13793
13794 void
13795 Numeric_constant::get_float(mpfr_t* val) const
13796 {
13797 go_assert(this->is_float());
13798 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
13799 }
13800
13801 // Get a complex value.
13802
13803 void
13804 Numeric_constant::get_complex(mpfr_t* real, mpfr_t* imag) const
13805 {
13806 go_assert(this->is_complex());
13807 mpfr_init_set(*real, this->u_.complex_val.real, GMP_RNDN);
13808 mpfr_init_set(*imag, this->u_.complex_val.imag, GMP_RNDN);
13809 }
13810
13811 // Express value as unsigned long if possible.
13812
13813 Numeric_constant::To_unsigned_long
13814 Numeric_constant::to_unsigned_long(unsigned long* val) const
13815 {
13816 switch (this->classification_)
13817 {
13818 case NC_INT:
13819 case NC_RUNE:
13820 return this->mpz_to_unsigned_long(this->u_.int_val, val);
13821 case NC_FLOAT:
13822 return this->mpfr_to_unsigned_long(this->u_.float_val, val);
13823 case NC_COMPLEX:
13824 if (!mpfr_zero_p(this->u_.complex_val.imag))
13825 return NC_UL_NOTINT;
13826 return this->mpfr_to_unsigned_long(this->u_.complex_val.real, val);
13827 default:
13828 go_unreachable();
13829 }
13830 }
13831
13832 // Express integer value as unsigned long if possible.
13833
13834 Numeric_constant::To_unsigned_long
13835 Numeric_constant::mpz_to_unsigned_long(const mpz_t ival,
13836 unsigned long *val) const
13837 {
13838 if (mpz_sgn(ival) < 0)
13839 return NC_UL_NEGATIVE;
13840 unsigned long ui = mpz_get_ui(ival);
13841 if (mpz_cmp_ui(ival, ui) != 0)
13842 return NC_UL_BIG;
13843 *val = ui;
13844 return NC_UL_VALID;
13845 }
13846
13847 // Express floating point value as unsigned long if possible.
13848
13849 Numeric_constant::To_unsigned_long
13850 Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval,
13851 unsigned long *val) const
13852 {
13853 if (!mpfr_integer_p(fval))
13854 return NC_UL_NOTINT;
13855 mpz_t ival;
13856 mpz_init(ival);
13857 mpfr_get_z(ival, fval, GMP_RNDN);
13858 To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val);
13859 mpz_clear(ival);
13860 return ret;
13861 }
13862
13863 // Convert value to integer if possible.
13864
13865 bool
13866 Numeric_constant::to_int(mpz_t* val) const
13867 {
13868 switch (this->classification_)
13869 {
13870 case NC_INT:
13871 case NC_RUNE:
13872 mpz_init_set(*val, this->u_.int_val);
13873 return true;
13874 case NC_FLOAT:
13875 if (!mpfr_integer_p(this->u_.float_val))
13876 return false;
13877 mpz_init(*val);
13878 mpfr_get_z(*val, this->u_.float_val, GMP_RNDN);
13879 return true;
13880 case NC_COMPLEX:
13881 if (!mpfr_zero_p(this->u_.complex_val.imag)
13882 || !mpfr_integer_p(this->u_.complex_val.real))
13883 return false;
13884 mpz_init(*val);
13885 mpfr_get_z(*val, this->u_.complex_val.real, GMP_RNDN);
13886 return true;
13887 default:
13888 go_unreachable();
13889 }
13890 }
13891
13892 // Convert value to floating point if possible.
13893
13894 bool
13895 Numeric_constant::to_float(mpfr_t* val) const
13896 {
13897 switch (this->classification_)
13898 {
13899 case NC_INT:
13900 case NC_RUNE:
13901 mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN);
13902 return true;
13903 case NC_FLOAT:
13904 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
13905 return true;
13906 case NC_COMPLEX:
13907 if (!mpfr_zero_p(this->u_.complex_val.imag))
13908 return false;
13909 mpfr_init_set(*val, this->u_.complex_val.real, GMP_RNDN);
13910 return true;
13911 default:
13912 go_unreachable();
13913 }
13914 }
13915
13916 // Convert value to complex.
13917
13918 bool
13919 Numeric_constant::to_complex(mpfr_t* vr, mpfr_t* vi) const
13920 {
13921 switch (this->classification_)
13922 {
13923 case NC_INT:
13924 case NC_RUNE:
13925 mpfr_init_set_z(*vr, this->u_.int_val, GMP_RNDN);
13926 mpfr_init_set_ui(*vi, 0, GMP_RNDN);
13927 return true;
13928 case NC_FLOAT:
13929 mpfr_init_set(*vr, this->u_.float_val, GMP_RNDN);
13930 mpfr_init_set_ui(*vi, 0, GMP_RNDN);
13931 return true;
13932 case NC_COMPLEX:
13933 mpfr_init_set(*vr, this->u_.complex_val.real, GMP_RNDN);
13934 mpfr_init_set(*vi, this->u_.complex_val.imag, GMP_RNDN);
13935 return true;
13936 default:
13937 go_unreachable();
13938 }
13939 }
13940
13941 // Get the type.
13942
13943 Type*
13944 Numeric_constant::type() const
13945 {
13946 if (this->type_ != NULL)
13947 return this->type_;
13948 switch (this->classification_)
13949 {
13950 case NC_INT:
13951 return Type::make_abstract_integer_type();
13952 case NC_RUNE:
13953 return Type::make_abstract_character_type();
13954 case NC_FLOAT:
13955 return Type::make_abstract_float_type();
13956 case NC_COMPLEX:
13957 return Type::make_abstract_complex_type();
13958 default:
13959 go_unreachable();
13960 }
13961 }
13962
13963 // If the constant can be expressed in TYPE, then set the type of the
13964 // constant to TYPE and return true. Otherwise return false, and, if
13965 // ISSUE_ERROR is true, report an appropriate error message.
13966
13967 bool
13968 Numeric_constant::set_type(Type* type, bool issue_error, Location loc)
13969 {
13970 bool ret;
13971 if (type == NULL)
13972 ret = true;
13973 else if (type->integer_type() != NULL)
13974 ret = this->check_int_type(type->integer_type(), issue_error, loc);
13975 else if (type->float_type() != NULL)
13976 ret = this->check_float_type(type->float_type(), issue_error, loc);
13977 else if (type->complex_type() != NULL)
13978 ret = this->check_complex_type(type->complex_type(), issue_error, loc);
13979 else
13980 go_unreachable();
13981 if (ret)
13982 this->type_ = type;
13983 return ret;
13984 }
13985
13986 // Check whether the constant can be expressed in an integer type.
13987
13988 bool
13989 Numeric_constant::check_int_type(Integer_type* type, bool issue_error,
13990 Location location) const
13991 {
13992 mpz_t val;
13993 switch (this->classification_)
13994 {
13995 case NC_INT:
13996 case NC_RUNE:
13997 mpz_init_set(val, this->u_.int_val);
13998 break;
13999
14000 case NC_FLOAT:
14001 if (!mpfr_integer_p(this->u_.float_val))
14002 {
14003 if (issue_error)
14004 error_at(location, "floating point constant truncated to integer");
14005 return false;
14006 }
14007 mpz_init(val);
14008 mpfr_get_z(val, this->u_.float_val, GMP_RNDN);
14009 break;
14010
14011 case NC_COMPLEX:
14012 if (!mpfr_integer_p(this->u_.complex_val.real)
14013 || !mpfr_zero_p(this->u_.complex_val.imag))
14014 {
14015 if (issue_error)
14016 error_at(location, "complex constant truncated to integer");
14017 return false;
14018 }
14019 mpz_init(val);
14020 mpfr_get_z(val, this->u_.complex_val.real, GMP_RNDN);
14021 break;
14022
14023 default:
14024 go_unreachable();
14025 }
14026
14027 bool ret;
14028 if (type->is_abstract())
14029 ret = true;
14030 else
14031 {
14032 int bits = mpz_sizeinbase(val, 2);
14033 if (type->is_unsigned())
14034 {
14035 // For an unsigned type we can only accept a nonnegative
14036 // number, and we must be able to represents at least BITS.
14037 ret = mpz_sgn(val) >= 0 && bits <= type->bits();
14038 }
14039 else
14040 {
14041 // For a signed type we need an extra bit to indicate the
14042 // sign. We have to handle the most negative integer
14043 // specially.
14044 ret = (bits + 1 <= type->bits()
14045 || (bits <= type->bits()
14046 && mpz_sgn(val) < 0
14047 && (mpz_scan1(val, 0)
14048 == static_cast<unsigned long>(type->bits() - 1))
14049 && mpz_scan0(val, type->bits()) == ULONG_MAX));
14050 }
14051 }
14052
14053 if (!ret && issue_error)
14054 error_at(location, "integer constant overflow");
14055
14056 return ret;
14057 }
14058
14059 // Check whether the constant can be expressed in a floating point
14060 // type.
14061
14062 bool
14063 Numeric_constant::check_float_type(Float_type* type, bool issue_error,
14064 Location location) const
14065 {
14066 mpfr_t val;
14067 switch (this->classification_)
14068 {
14069 case NC_INT:
14070 case NC_RUNE:
14071 mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN);
14072 break;
14073
14074 case NC_FLOAT:
14075 mpfr_init_set(val, this->u_.float_val, GMP_RNDN);
14076 break;
14077
14078 case NC_COMPLEX:
14079 if (!mpfr_zero_p(this->u_.complex_val.imag))
14080 {
14081 if (issue_error)
14082 error_at(location, "complex constant truncated to float");
14083 return false;
14084 }
14085 mpfr_init_set(val, this->u_.complex_val.real, GMP_RNDN);
14086 break;
14087
14088 default:
14089 go_unreachable();
14090 }
14091
14092 bool ret;
14093 if (type->is_abstract())
14094 ret = true;
14095 else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
14096 {
14097 // A NaN or Infinity always fits in the range of the type.
14098 ret = true;
14099 }
14100 else
14101 {
14102 mp_exp_t exp = mpfr_get_exp(val);
14103 mp_exp_t max_exp;
14104 switch (type->bits())
14105 {
14106 case 32:
14107 max_exp = 128;
14108 break;
14109 case 64:
14110 max_exp = 1024;
14111 break;
14112 default:
14113 go_unreachable();
14114 }
14115
14116 ret = exp <= max_exp;
14117 }
14118
14119 mpfr_clear(val);
14120
14121 if (!ret && issue_error)
14122 error_at(location, "floating point constant overflow");
14123
14124 return ret;
14125 }
14126
14127 // Check whether the constant can be expressed in a complex type.
14128
14129 bool
14130 Numeric_constant::check_complex_type(Complex_type* type, bool issue_error,
14131 Location location) const
14132 {
14133 if (type->is_abstract())
14134 return true;
14135
14136 mp_exp_t max_exp;
14137 switch (type->bits())
14138 {
14139 case 64:
14140 max_exp = 128;
14141 break;
14142 case 128:
14143 max_exp = 1024;
14144 break;
14145 default:
14146 go_unreachable();
14147 }
14148
14149 mpfr_t real;
14150 switch (this->classification_)
14151 {
14152 case NC_INT:
14153 case NC_RUNE:
14154 mpfr_init_set_z(real, this->u_.int_val, GMP_RNDN);
14155 break;
14156
14157 case NC_FLOAT:
14158 mpfr_init_set(real, this->u_.float_val, GMP_RNDN);
14159 break;
14160
14161 case NC_COMPLEX:
14162 if (!mpfr_nan_p(this->u_.complex_val.imag)
14163 && !mpfr_inf_p(this->u_.complex_val.imag)
14164 && !mpfr_zero_p(this->u_.complex_val.imag))
14165 {
14166 if (mpfr_get_exp(this->u_.complex_val.imag) > max_exp)
14167 {
14168 if (issue_error)
14169 error_at(location, "complex imaginary part overflow");
14170 return false;
14171 }
14172 }
14173 mpfr_init_set(real, this->u_.complex_val.real, GMP_RNDN);
14174 break;
14175
14176 default:
14177 go_unreachable();
14178 }
14179
14180 bool ret;
14181 if (mpfr_nan_p(real) || mpfr_inf_p(real) || mpfr_zero_p(real))
14182 ret = true;
14183 else
14184 ret = mpfr_get_exp(real) <= max_exp;
14185
14186 mpfr_clear(real);
14187
14188 if (!ret && issue_error)
14189 error_at(location, "complex real part overflow");
14190
14191 return ret;
14192 }
14193
14194 // Return an Expression for this value.
14195
14196 Expression*
14197 Numeric_constant::expression(Location loc) const
14198 {
14199 switch (this->classification_)
14200 {
14201 case NC_INT:
14202 return Expression::make_integer(&this->u_.int_val, this->type_, loc);
14203 case NC_RUNE:
14204 return Expression::make_character(&this->u_.int_val, this->type_, loc);
14205 case NC_FLOAT:
14206 return Expression::make_float(&this->u_.float_val, this->type_, loc);
14207 case NC_COMPLEX:
14208 return Expression::make_complex(&this->u_.complex_val.real,
14209 &this->u_.complex_val.imag,
14210 this->type_, loc);
14211 default:
14212 go_unreachable();
14213 }
14214 }